Skip to content

Commit f7f38f7

Browse files
committed
draw example build refactor
1 parent 189c453 commit f7f38f7

File tree

32 files changed

+636
-238
lines changed

32 files changed

+636
-238
lines changed

README.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
- vulcandraw
55
- vulcancube
66

7+
Using CGO then requare C/C++ compiler on path.
8+
79
## Supported platforms
810

911
- Windows (GLFW)
@@ -18,18 +20,18 @@
1820
### GLFW v3.3
1921

2022
A standard `go run main.go` / `go build` in each of the glfw demo folders should work out of the box for most platforms.
23+
Optionally make file can help.
2124

22-
For macOS / iOS the MoltenVK.framework needs to be installed in the /Library/Frameworks folder for the build to find it. It can be downloaded as part of he Vulkan SDK: https://vulkan.lunarg.com/sdk/home
25+
For macOS / iOS the [MoltenVK](https://github.com/KhronosGroup/MoltenVK) dylib needs to be installed.
26+
On macOS you can use [Homebrew](https://docs.brew.sh/Installation) and install `molten-vk` package like `brew install molten-vk`.
2327

2428
### Manual configuration
2529

26-
For **OS X / macOS** you'll need to install the latest GLFW 3.3 from master https://github.com/glfw/glfw
30+
For **OS X / macOS** you'll need to install. the latest GLFW 3.3 from master https://github.com/glfw/glfw
2731
and prepare MoltenVK https://moltengl.com/moltenvk/ SDK beforehand so CMake could find it.
2832

2933
There is a Makefile https://github.com/vulkan-go/demos/blob/master/vulkancube/vulkancube_desktop/Makefile to show how to properly invoke `go install` specifying the path to GLFW.
3034

31-
For **Windows** you don't need to compile MoltenVK and can use GLFW 3.2.1 distro from the site http://www.glfw.org then just specify paths in Makefile or run commands by hand, specifying paths to GLFW distro folders.
32-
3335
Make sure your graphics card and driver are supported:
3436

3537
- https://developer.nvidia.com/vulkan-driver
@@ -39,6 +41,18 @@ In all cases you will run `XXX_desktop` demos.
3941

4042
## How to run on Android
4143

44+
Prerequisites are
45+
- installed [Android SDK](https://developer.android.com/studio/releases/platform-tools)
46+
- installed [Android NDK](https://developer.android.com/ndk/downloads)
47+
- installed [Android Studio](https://developer.android.com/studio) recommended
48+
- installed application "make"
49+
- set system variable [ANDROID_HOME](https://developer.android.com/studio/command-line/variables)
50+
- set system variable [NDK](https://developer.android.com/ndk/guides/other_build_systems)
51+
- set system variable [HOST_TAG](https://developer.android.com/ndk/guides/other_build_systems)
52+
53+
In the "android" folder is "make" file which run as `make all` will clean, build and make application APK file in ./android/app/build/outputs/apk/debug.
54+
Using Android Studio is very easy to deploy APK file into physical device or emulator.
55+
4256
Refer to [xlab/android-go/examples/minimal](https://github.com/xlab/android-go/tree/master/examples/minimal)
4357

4458
## License

vulkandraw/vulkandraw.go

Lines changed: 22 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,7 @@ func VulkanInit(v *VulkanDeviceInfo, s *VulkanSwapchainInfo,
132132
check(ret, "vk.CreateSemaphore")
133133
}
134134

135-
func VulkanDrawFrame(v VulkanDeviceInfo,
136-
s VulkanSwapchainInfo, r VulkanRenderInfo) bool {
135+
func DrawFrame(v VulkanDeviceInfo, s VulkanSwapchainInfo, r VulkanRenderInfo) bool {
137136
var nextIdx uint32
138137

139138
// Phase 1: vk.AcquireNextImage
@@ -158,6 +157,9 @@ func VulkanDrawFrame(v VulkanDeviceInfo,
158157
SType: vk.StructureTypeSubmitInfo,
159158
WaitSemaphoreCount: 1,
160159
PWaitSemaphores: r.semaphores,
160+
PWaitDstStageMask: []vk.PipelineStageFlags{
161+
vk.PipelineStageFlags(vk.PipelineStageColorAttachmentOutputBit),
162+
},
161163
CommandBufferCount: 1,
162164
PCommandBuffers: r.cmdBuffers[nextIdx:],
163165
}}
@@ -218,8 +220,8 @@ func CreateRenderer(device vk.Device, displayFormat vk.Format) (VulkanRenderInfo
218220
StoreOp: vk.AttachmentStoreOpStore,
219221
StencilLoadOp: vk.AttachmentLoadOpDontCare,
220222
StencilStoreOp: vk.AttachmentStoreOpDontCare,
221-
InitialLayout: vk.ImageLayoutColorAttachmentOptimal,
222-
FinalLayout: vk.ImageLayoutColorAttachmentOptimal,
223+
InitialLayout: vk.ImageLayoutUndefined,
224+
FinalLayout: vk.ImageLayoutPresentSrc,
223225
}}
224226
colorAttachments := []vk.AttachmentReference{{
225227
Attachment: 0,
@@ -257,30 +259,21 @@ func CreateRenderer(device vk.Device, displayFormat vk.Format) (VulkanRenderInfo
257259
return r, nil
258260
}
259261

260-
func NewVulkanDevice(appInfo *vk.ApplicationInfo, window uintptr, instanceExtensions []string, createSurfaceFunc func(interface{}) uintptr) (VulkanDeviceInfo, error) {
262+
func NewVulkanDevice(appInfo *vk.ApplicationInfo, instanceExtensions []string, createSurfaceFunc func(vk.Instance) vk.Surface) (VulkanDeviceInfo, error) {
261263
// Phase 1: vk.CreateInstance with vk.InstanceCreateInfo
262264

263265
existingExtensions := getInstanceExtensions()
264266
log.Println("[INFO] Instance extensions:", existingExtensions)
265267

266-
// instanceExtensions := vk.GetRequiredInstanceExtensions()
268+
//TODO deprecated extension, use VK_EXT_debug_utils instead - https://developer.android.com/ndk/guides/graphics/validation-layer
267269
if enableDebug {
268-
instanceExtensions = append(instanceExtensions,
269-
"VK_EXT_debug_report\x00")
270+
instanceExtensions = append(instanceExtensions, "VK_EXT_debug_report\x00")
270271
}
271272

272-
// ANDROID:
273-
// these layers must be included in APK,
274-
// see Android.mk and ValidationLayers.mk
275-
instanceLayers := []string{
276-
// "VK_LAYER_GOOGLE_threading\x00",
277-
// "VK_LAYER_LUNARG_parameter_validation\x00",
278-
// "VK_LAYER_LUNARG_object_tracker\x00",
279-
// "VK_LAYER_LUNARG_core_validation\x00",
280-
// "VK_LAYER_LUNARG_api_dump\x00",
281-
// "VK_LAYER_LUNARG_image\x00",
282-
// "VK_LAYER_LUNARG_swapchain\x00",
283-
// "VK_LAYER_GOOGLE_unique_objects\x00",
273+
// ANDROID: these layers must be included in APK
274+
var instanceLayers []string
275+
if enableDebug {
276+
instanceLayers = append(instanceLayers, "VK_LAYER_KHRONOS_validation\x00")
284277
}
285278

286279
instanceCreateInfo := vk.InstanceCreateInfo{
@@ -302,13 +295,8 @@ func NewVulkanDevice(appInfo *vk.ApplicationInfo, window uintptr, instanceExtens
302295

303296
// Phase 2: vk.CreateAndroidSurface with vk.AndroidSurfaceCreateInfo
304297

305-
v.Surface = vk.SurfaceFromPointer(createSurfaceFunc(v.Instance))
306-
// err = vk.Error(vk.CreateWindowSurface(v.Instance, window, nil, &v.Surface))
307-
if err != nil {
308-
vk.DestroyInstance(v.Instance, nil)
309-
err = fmt.Errorf("vkCreateWindowSurface failed with %s", err)
310-
return v, err
311-
}
298+
v.Surface = createSurfaceFunc(v.Instance)
299+
312300
if v.gpuDevices, err = getPhysicalDevices(v.Instance); err != nil {
313301
v.gpuDevices = nil
314302
vk.DestroySurface(v.Instance, v.Surface, nil)
@@ -321,19 +309,8 @@ func NewVulkanDevice(appInfo *vk.ApplicationInfo, window uintptr, instanceExtens
321309

322310
// Phase 3: vk.CreateDevice with vk.DeviceCreateInfo (a logical device)
323311

324-
// ANDROID:
325-
// these layers must be included in APK,
326-
// see Android.mk and ValidationLayers.mk
327-
deviceLayers := []string{
328-
// "VK_LAYER_GOOGLE_threading\x00",
329-
// "VK_LAYER_LUNARG_parameter_validation\x00",
330-
// "VK_LAYER_LUNARG_object_tracker\x00",
331-
// "VK_LAYER_LUNARG_core_validation\x00",
332-
// "VK_LAYER_LUNARG_api_dump\x00",
333-
// "VK_LAYER_LUNARG_image\x00",
334-
// "VK_LAYER_LUNARG_swapchain\x00",
335-
// "VK_LAYER_GOOGLE_unique_objects\x00",
336-
}
312+
//TODO Device layers are deprecated
313+
//deviceLayers := []string{}
337314

338315
queueCreateInfos := []vk.DeviceQueueCreateInfo{{
339316
SType: vk.StructureTypeDeviceQueueCreateInfo,
@@ -349,8 +326,8 @@ func NewVulkanDevice(appInfo *vk.ApplicationInfo, window uintptr, instanceExtens
349326
PQueueCreateInfos: queueCreateInfos,
350327
EnabledExtensionCount: uint32(len(deviceExtensions)),
351328
PpEnabledExtensionNames: deviceExtensions,
352-
EnabledLayerCount: uint32(len(deviceLayers)),
353-
PpEnabledLayerNames: deviceLayers,
329+
//EnabledLayerCount: uint32(len(deviceLayers)),
330+
//PpEnabledLayerNames: deviceLayers,
354331
}
355332
var device vk.Device // we choose the first GPU available for this device
356333
err = vk.Error(vk.CreateDevice(v.gpuDevices[0], &deviceCreateInfo, nil, &device))
@@ -511,6 +488,7 @@ func (v *VulkanDeviceInfo) CreateSwapchain() (VulkanSwapchainInfo, error) {
511488
PresentMode: vk.PresentModeFifo,
512489
OldSwapchain: vk.NullSwapchain,
513490
Clipped: vk.False,
491+
CompositeAlpha: vk.CompositeAlphaInheritBit,
514492
}
515493
s.Swapchains = make([]vk.Swapchain, 1)
516494
err = vk.Error(vk.CreateSwapchain(v.Device, &swapchainCreateInfo, nil, &(s.Swapchains[0])))
@@ -585,7 +563,7 @@ func (s *VulkanSwapchainInfo) CreateFramebuffers(renderPass vk.RenderPass, depth
585563
SType: vk.StructureTypeFramebufferCreateInfo,
586564
RenderPass: renderPass,
587565
Layers: 1,
588-
AttachmentCount: 1, // 2 if has depthView
566+
AttachmentCount: 1, // 2 if it has depthView
589567
PAttachments: attachments,
590568
Width: s.DisplaySize.Width,
591569
Height: s.DisplaySize.Height,
@@ -823,7 +801,7 @@ func CreateGraphicsPipeline(device vk.Device,
823801
inputAssemblyState := vk.PipelineInputAssemblyStateCreateInfo{
824802
SType: vk.StructureTypePipelineInputAssemblyStateCreateInfo,
825803
Topology: vk.PrimitiveTopologyTriangleList,
826-
PrimitiveRestartEnable: vk.True,
804+
PrimitiveRestartEnable: vk.False,
827805
}
828806
vertexInputBindings := []vk.VertexInputBindingDescription{{
829807
Binding: 0,

vulkandraw/vulkandraw_android/.gitignore

Lines changed: 0 additions & 10 deletions
This file was deleted.

vulkandraw/vulkandraw_android/Makefile

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,48 @@
1-
OS=linux
2-
ANDROID_API ?= 23
3-
ANDROID_TOOLCHAIN_DIR ?= $(ANDROID_NDK_HOME)/toolchains/llvm/prebuilt/$(OS)-x86_64/bin
1+
HOST_TAG ?= linux-x86_64
2+
API_LEVEL ?= 26
3+
TOOLCHAIN ?= $(NDK)/toolchains/llvm/prebuilt/$(HOST_TAG)/bin
44

5-
all: build apk
5+
all: clean build apk
6+
echo "WORK DONE"
67

7-
build:
8-
mkdir -p android/jni/lib
9-
CC="$(ANDROID_TOOLCHAIN_DIR)/aarch64-linux-android$(ANDROID_API)-clang" \
10-
CXX="$(ANDROID_TOOLCHAIN_DIR)/aarch64-linux-android$(ANDROID_API)-clang++" \
8+
build: build32 build64 build-amd64
9+
echo "builded all"
10+
11+
build32:
12+
echo "compile for old phones with arm (32bit)"
13+
CC="$(TOOLCHAIN)/armv7a-linux-androideabi$(API_LEVEL)-clang" \
14+
CXX="$(TOOLCHAIN)/armv7a-linux-androideabi$(API_LEVEL)-clang++" \
15+
GOOS=android \
16+
GOARCH=arm \
17+
CGO_ENABLED=1 \
18+
go build -buildmode=c-shared -o android/app/src/main/jniLibs/armeabi-v7a/libvulkandraw.so
19+
20+
build64:
21+
echo "compile for arm64 (64bit)"
22+
CC="$(TOOLCHAIN)/aarch64-linux-android$(API_LEVEL)-clang" \
23+
CXX="$(TOOLCHAIN)/aarch64-linux-android$(API_LEVEL)-clang++" \
1124
GOOS=android \
1225
GOARCH=arm64 \
1326
CGO_ENABLED=1 \
14-
go build -buildmode=c-shared -o android/jni/lib/libvulkandraw.so
15-
16-
apk:
17-
cd android && make
27+
go build -buildmode=c-shared -o android/app/src/main/jniLibs/arm64-v8a/libvulkandraw.so
28+
29+
build-amd64:
30+
echo "compile for emulator"
31+
CC="$(TOOLCHAIN)/x86_64-linux-android$(API_LEVEL)-clang" \
32+
CXX="$(TOOLCHAIN)/x86_64-linux-android$(API_LEVEL)-clang++" \
33+
GOOS=android \
34+
GOARCH=amd64 \
35+
CGO_ENABLED=1 \
36+
go build -buildmode=c-shared -o android/app/src/main/jniLibs/x86_64/libvulkandraw.so
1837

1938
clean:
20-
cd android && make clean
39+
echo "delete generated files"
40+
rm -rf android/app/build
41+
rm -rf android/app/build-native
2142

22-
install:
23-
cd android && make install
43+
apk:
44+
echo "run gradle"
45+
cd android; ./gradlew -q clean assembleDebug > /dev/null 2>&1
2446

2547
listen:
2648
adb logcat -c
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#
2+
# https://help.github.com/articles/dealing-with-line-endings/
3+
#
4+
# These are explicitly windows files and should use crlf
5+
*.bat text eol=crlf
6+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Ignore Gradle project-specific cache directory
2+
.gradle
3+
4+
# Ignore Gradle build output directory
5+
build
6+
build-native
7+
local.properties

vulkandraw/vulkandraw_android/android/AndroidManifest.xml

Lines changed: 0 additions & 20 deletions
This file was deleted.

vulkandraw/vulkandraw_android/android/Makefile

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
# Mention the cmake version name.
3+
cmake_minimum_required(VERSION 3.4.1)
4+
5+
project( VulkanDraw )
6+
7+
# Application
8+
add_library(vulkandraw SHARED IMPORTED)
9+
10+
set_target_properties( vulkandraw
11+
PROPERTIES IMPORTED_LOCATION
12+
${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libvulkandraw.so
13+
)

0 commit comments

Comments
 (0)