Skip to content

Commit

Permalink
draw example build refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
tomas-mraz committed Dec 8, 2022
1 parent 189c453 commit dd85aa7
Show file tree
Hide file tree
Showing 32 changed files with 639 additions and 238 deletions.
25 changes: 21 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
- vulcandraw
- vulcancube

Using CGO then requare C/C++ compiler on path.

## Supported platforms

- Windows (GLFW)
Expand All @@ -18,18 +20,18 @@
### GLFW v3.3

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

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
For macOS / iOS the [MoltenVK](https://github.com/KhronosGroup/MoltenVK) dylib needs to be installed.
On macOS you can use [Homebrew](https://docs.brew.sh/Installation) and install `molten-vk` package like `brew install molten-vk`.

### Manual configuration

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

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.

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.

Make sure your graphics card and driver are supported:

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

## How to run on Android

Prerequisites are
- installed [Android SDK](https://developer.android.com/studio/releases/platform-tools)
- installed [Android NDK](https://developer.android.com/ndk/downloads)
- installed application "make"
- set system variable [ANDROID_HOME](https://developer.android.com/studio/command-line/variables)
- set system variable [NDK](https://developer.android.com/ndk/guides/other_build_systems)
- set system variable [HOST_TAG](https://developer.android.com/ndk/guides/other_build_systems)

Recommended:
- installed [Android Studio](https://developer.android.com/studio)
- [validation layers binaries](https://github.com/KhronosGroup/Vulkan-ValidationLayers/releases)

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.
Using Android Studio is very easy to deploy APK file into physical device or emulator.

Refer to [xlab/android-go/examples/minimal](https://github.com/xlab/android-go/tree/master/examples/minimal)

## License
Expand Down
66 changes: 22 additions & 44 deletions vulkandraw/vulkandraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ func VulkanInit(v *VulkanDeviceInfo, s *VulkanSwapchainInfo,
check(ret, "vk.CreateSemaphore")
}

func VulkanDrawFrame(v VulkanDeviceInfo,
s VulkanSwapchainInfo, r VulkanRenderInfo) bool {
func DrawFrame(v VulkanDeviceInfo, s VulkanSwapchainInfo, r VulkanRenderInfo) bool {
var nextIdx uint32

// Phase 1: vk.AcquireNextImage
Expand All @@ -158,6 +157,9 @@ func VulkanDrawFrame(v VulkanDeviceInfo,
SType: vk.StructureTypeSubmitInfo,
WaitSemaphoreCount: 1,
PWaitSemaphores: r.semaphores,
PWaitDstStageMask: []vk.PipelineStageFlags{
vk.PipelineStageFlags(vk.PipelineStageColorAttachmentOutputBit),
},
CommandBufferCount: 1,
PCommandBuffers: r.cmdBuffers[nextIdx:],
}}
Expand Down Expand Up @@ -218,8 +220,8 @@ func CreateRenderer(device vk.Device, displayFormat vk.Format) (VulkanRenderInfo
StoreOp: vk.AttachmentStoreOpStore,
StencilLoadOp: vk.AttachmentLoadOpDontCare,
StencilStoreOp: vk.AttachmentStoreOpDontCare,
InitialLayout: vk.ImageLayoutColorAttachmentOptimal,
FinalLayout: vk.ImageLayoutColorAttachmentOptimal,
InitialLayout: vk.ImageLayoutUndefined,
FinalLayout: vk.ImageLayoutPresentSrc,
}}
colorAttachments := []vk.AttachmentReference{{
Attachment: 0,
Expand Down Expand Up @@ -257,30 +259,21 @@ func CreateRenderer(device vk.Device, displayFormat vk.Format) (VulkanRenderInfo
return r, nil
}

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

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

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

// ANDROID:
// these layers must be included in APK,
// see Android.mk and ValidationLayers.mk
instanceLayers := []string{
// "VK_LAYER_GOOGLE_threading\x00",
// "VK_LAYER_LUNARG_parameter_validation\x00",
// "VK_LAYER_LUNARG_object_tracker\x00",
// "VK_LAYER_LUNARG_core_validation\x00",
// "VK_LAYER_LUNARG_api_dump\x00",
// "VK_LAYER_LUNARG_image\x00",
// "VK_LAYER_LUNARG_swapchain\x00",
// "VK_LAYER_GOOGLE_unique_objects\x00",
// ANDROID: these layers must be included in APK
var instanceLayers []string
if enableDebug {
instanceLayers = append(instanceLayers, "VK_LAYER_KHRONOS_validation\x00")
}

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

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

v.Surface = vk.SurfaceFromPointer(createSurfaceFunc(v.Instance))
// err = vk.Error(vk.CreateWindowSurface(v.Instance, window, nil, &v.Surface))
if err != nil {
vk.DestroyInstance(v.Instance, nil)
err = fmt.Errorf("vkCreateWindowSurface failed with %s", err)
return v, err
}
v.Surface = createSurfaceFunc(v.Instance)

if v.gpuDevices, err = getPhysicalDevices(v.Instance); err != nil {
v.gpuDevices = nil
vk.DestroySurface(v.Instance, v.Surface, nil)
Expand All @@ -321,19 +309,8 @@ func NewVulkanDevice(appInfo *vk.ApplicationInfo, window uintptr, instanceExtens

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

// ANDROID:
// these layers must be included in APK,
// see Android.mk and ValidationLayers.mk
deviceLayers := []string{
// "VK_LAYER_GOOGLE_threading\x00",
// "VK_LAYER_LUNARG_parameter_validation\x00",
// "VK_LAYER_LUNARG_object_tracker\x00",
// "VK_LAYER_LUNARG_core_validation\x00",
// "VK_LAYER_LUNARG_api_dump\x00",
// "VK_LAYER_LUNARG_image\x00",
// "VK_LAYER_LUNARG_swapchain\x00",
// "VK_LAYER_GOOGLE_unique_objects\x00",
}
//TODO Device layers are deprecated
//deviceLayers := []string{}

queueCreateInfos := []vk.DeviceQueueCreateInfo{{
SType: vk.StructureTypeDeviceQueueCreateInfo,
Expand All @@ -349,8 +326,8 @@ func NewVulkanDevice(appInfo *vk.ApplicationInfo, window uintptr, instanceExtens
PQueueCreateInfos: queueCreateInfos,
EnabledExtensionCount: uint32(len(deviceExtensions)),
PpEnabledExtensionNames: deviceExtensions,
EnabledLayerCount: uint32(len(deviceLayers)),
PpEnabledLayerNames: deviceLayers,
//EnabledLayerCount: uint32(len(deviceLayers)),
//PpEnabledLayerNames: deviceLayers,
}
var device vk.Device // we choose the first GPU available for this device
err = vk.Error(vk.CreateDevice(v.gpuDevices[0], &deviceCreateInfo, nil, &device))
Expand Down Expand Up @@ -511,6 +488,7 @@ func (v *VulkanDeviceInfo) CreateSwapchain() (VulkanSwapchainInfo, error) {
PresentMode: vk.PresentModeFifo,
OldSwapchain: vk.NullSwapchain,
Clipped: vk.False,
CompositeAlpha: vk.CompositeAlphaInheritBit,
}
s.Swapchains = make([]vk.Swapchain, 1)
err = vk.Error(vk.CreateSwapchain(v.Device, &swapchainCreateInfo, nil, &(s.Swapchains[0])))
Expand Down Expand Up @@ -585,7 +563,7 @@ func (s *VulkanSwapchainInfo) CreateFramebuffers(renderPass vk.RenderPass, depth
SType: vk.StructureTypeFramebufferCreateInfo,
RenderPass: renderPass,
Layers: 1,
AttachmentCount: 1, // 2 if has depthView
AttachmentCount: 1, // 2 if it has depthView
PAttachments: attachments,
Width: s.DisplaySize.Width,
Height: s.DisplaySize.Height,
Expand Down Expand Up @@ -823,7 +801,7 @@ func CreateGraphicsPipeline(device vk.Device,
inputAssemblyState := vk.PipelineInputAssemblyStateCreateInfo{
SType: vk.StructureTypePipelineInputAssemblyStateCreateInfo,
Topology: vk.PrimitiveTopologyTriangleList,
PrimitiveRestartEnable: vk.True,
PrimitiveRestartEnable: vk.False,
}
vertexInputBindings := []vk.VertexInputBindingDescription{{
Binding: 0,
Expand Down
10 changes: 0 additions & 10 deletions vulkandraw/vulkandraw_android/.gitignore

This file was deleted.

52 changes: 37 additions & 15 deletions vulkandraw/vulkandraw_android/Makefile
Original file line number Diff line number Diff line change
@@ -1,26 +1,48 @@
OS=linux
ANDROID_API ?= 23
ANDROID_TOOLCHAIN_DIR ?= $(ANDROID_NDK_HOME)/toolchains/llvm/prebuilt/$(OS)-x86_64/bin
HOST_TAG ?= linux-x86_64
API_LEVEL ?= 26
TOOLCHAIN ?= $(NDK)/toolchains/llvm/prebuilt/$(HOST_TAG)/bin

all: build apk
all: clean build apk
echo "WORK DONE"

build:
mkdir -p android/jni/lib
CC="$(ANDROID_TOOLCHAIN_DIR)/aarch64-linux-android$(ANDROID_API)-clang" \
CXX="$(ANDROID_TOOLCHAIN_DIR)/aarch64-linux-android$(ANDROID_API)-clang++" \
build: build32 build64 build-amd64
echo "builded all"

build32:
echo "compile for old phones with arm (32bit)"
CC="$(TOOLCHAIN)/armv7a-linux-androideabi$(API_LEVEL)-clang" \
CXX="$(TOOLCHAIN)/armv7a-linux-androideabi$(API_LEVEL)-clang++" \
GOOS=android \
GOARCH=arm \
CGO_ENABLED=1 \
go build -buildmode=c-shared -o android/app/src/main/jniLibs/armeabi-v7a/libvulkandraw.so

build64:
echo "compile for arm64 (64bit)"
CC="$(TOOLCHAIN)/aarch64-linux-android$(API_LEVEL)-clang" \
CXX="$(TOOLCHAIN)/aarch64-linux-android$(API_LEVEL)-clang++" \
GOOS=android \
GOARCH=arm64 \
CGO_ENABLED=1 \
go build -buildmode=c-shared -o android/jni/lib/libvulkandraw.so

apk:
cd android && make
go build -buildmode=c-shared -o android/app/src/main/jniLibs/arm64-v8a/libvulkandraw.so

build-amd64:
echo "compile for emulator"
CC="$(TOOLCHAIN)/x86_64-linux-android$(API_LEVEL)-clang" \
CXX="$(TOOLCHAIN)/x86_64-linux-android$(API_LEVEL)-clang++" \
GOOS=android \
GOARCH=amd64 \
CGO_ENABLED=1 \
go build -buildmode=c-shared -o android/app/src/main/jniLibs/x86_64/libvulkandraw.so

clean:
cd android && make clean
echo "delete generated files"
rm -rf android/app/build
rm -rf android/app/build-native

install:
cd android && make install
apk:
echo "run gradle"
cd android; ./gradlew -q clean assembleDebug > /dev/null 2>&1

listen:
adb logcat -c
Expand Down
6 changes: 6 additions & 0 deletions vulkandraw/vulkandraw_android/android/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#
# https://help.github.com/articles/dealing-with-line-endings/
#
# These are explicitly windows files and should use crlf
*.bat text eol=crlf

7 changes: 7 additions & 0 deletions vulkandraw/vulkandraw_android/android/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Ignore Gradle project-specific cache directory
.gradle

# Ignore Gradle build output directory
build
build-native
local.properties
20 changes: 0 additions & 20 deletions vulkandraw/vulkandraw_android/android/AndroidManifest.xml

This file was deleted.

23 changes: 0 additions & 23 deletions vulkandraw/vulkandraw_android/android/Makefile

This file was deleted.

13 changes: 13 additions & 0 deletions vulkandraw/vulkandraw_android/android/app/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

# Mention the cmake version name.
cmake_minimum_required(VERSION 3.4.1)

project( VulkanDraw )

# Application
add_library(vulkandraw SHARED IMPORTED)

set_target_properties( vulkandraw
PROPERTIES IMPORTED_LOCATION
${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libvulkandraw.so
)
Loading

0 comments on commit dd85aa7

Please sign in to comment.