From c69af6cdca7aad719a4784e92a800eef31353c9f Mon Sep 17 00:00:00 2001 From: saschawillems Date: Thu, 24 Mar 2016 23:07:14 +0100 Subject: [PATCH] Added android support for bloom, computeshader and offscreen examples (#97) --- android/bloom/.gitignore | 10 ++++ android/bloom/AndroidManifest.xml | 27 ++++++++++ android/bloom/build.bat | 25 +++++++++ android/bloom/jni/Android.mk | 47 +++++++++++++++++ android/bloom/jni/Application.mk | 5 ++ android/build-bloom.bat | 1 + android/build-computeshader.bat | 1 + android/build-offscreen.bat | 1 + android/computeshader/.gitignore | 10 ++++ android/computeshader/AndroidManifest.xml | 27 ++++++++++ android/computeshader/build.bat | 21 ++++++++ android/computeshader/jni/Android.mk | 47 +++++++++++++++++ android/computeshader/jni/Application.mk | 5 ++ android/offscreen/.gitignore | 10 ++++ android/offscreen/AndroidManifest.xml | 27 ++++++++++ android/offscreen/build.bat | 24 +++++++++ android/offscreen/jni/Android.mk | 47 +++++++++++++++++ android/offscreen/jni/Application.mk | 4 ++ bloom/bloom.cpp | 62 +++++++++++++++-------- computeshader/computeshader.cpp | 57 +++++++++++++-------- offscreen/offscreen.cpp | 56 +++++++++++++------- 21 files changed, 452 insertions(+), 62 deletions(-) create mode 100644 android/bloom/.gitignore create mode 100644 android/bloom/AndroidManifest.xml create mode 100644 android/bloom/build.bat create mode 100644 android/bloom/jni/Android.mk create mode 100644 android/bloom/jni/Application.mk create mode 100644 android/build-bloom.bat create mode 100644 android/build-computeshader.bat create mode 100644 android/build-offscreen.bat create mode 100644 android/computeshader/.gitignore create mode 100644 android/computeshader/AndroidManifest.xml create mode 100644 android/computeshader/build.bat create mode 100644 android/computeshader/jni/Android.mk create mode 100644 android/computeshader/jni/Application.mk create mode 100644 android/offscreen/.gitignore create mode 100644 android/offscreen/AndroidManifest.xml create mode 100644 android/offscreen/build.bat create mode 100644 android/offscreen/jni/Android.mk create mode 100644 android/offscreen/jni/Application.mk diff --git a/android/bloom/.gitignore b/android/bloom/.gitignore new file mode 100644 index 00000000..7a5d249c --- /dev/null +++ b/android/bloom/.gitignore @@ -0,0 +1,10 @@ +/assets/ +/res/ +/bin/ +/libs/ +/obj/ +/build.xml +/local.properties +/project.properties +/proguard-project.txt +*.apk \ No newline at end of file diff --git a/android/bloom/AndroidManifest.xml b/android/bloom/AndroidManifest.xml new file mode 100644 index 00000000..cbfe4ab3 --- /dev/null +++ b/android/bloom/AndroidManifest.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/android/bloom/build.bat b/android/bloom/build.bat new file mode 100644 index 00000000..179854e3 --- /dev/null +++ b/android/bloom/build.bat @@ -0,0 +1,25 @@ +cd jni +call ndk-build +if %ERRORLEVEL% EQU 0 ( + echo ndk-build has failed, build cancelled + cd.. + + mkdir "assets\shaders\bloom" + xcopy "..\..\data\shaders\bloom\*.spv" "assets\shaders\bloom" /Y + + mkdir "assets\textures" + xcopy "..\..\data\textures\cubemap_space.ktx" "assets\textures" /Y + + mkdir "assets\models" + xcopy "..\..\data\models\retroufo.dae" "assets\models" /Y + xcopy "..\..\data\models\retroufo_glow.dae" "assets\models" /Y + xcopy "..\..\data\models\cube.obj" "assets\models" /Y + + mkdir "res\drawable" + xcopy "..\..\android\images\icon.png" "res\drawable" /Y + + call ant debug -Dout.final.file=vulkanBloom.apk +) ELSE ( + echo error : ndk-build failed with errors! + cd.. +) diff --git a/android/bloom/jni/Android.mk b/android/bloom/jni/Android.mk new file mode 100644 index 00000000..23fb9073 --- /dev/null +++ b/android/bloom/jni/Android.mk @@ -0,0 +1,47 @@ +LOCAL_PATH := $(call my-dir)/../../bloom + +# assimp + +include $(CLEAR_VARS) + +LOCAL_MODULE := assimp +LOCAL_SRC_FILES := $(LOCAL_PATH)/../../libs/assimp/$(TARGET_ARCH_ABI)/libassimp.a +include $(PREBUILT_STATIC_LIBRARY) + +# vulkan example + +DATADIR := $(LOCAL_PATH)/../../data + +include $(CLEAR_VARS) + +LOCAL_MODULE := vulkanBloom + +PROJECT_FILES := $(wildcard $(LOCAL_PATH)/../../bloom/*.cpp) +PROJECT_FILES += $(wildcard $(LOCAL_PATH)/../../base/*.cpp) + +LOCAL_CPPFLAGS := -std=c++11 +LOCAL_CPPFLAGS += -D__STDC_LIMIT_MACROS +LOCAL_CPPFLAGS += -DVK_NO_PROTOTYPES +LOCAL_CPPFLAGS += -DVK_USE_PLATFORM_ANDROID_KHR + +LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../external/ +LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../external/glm +LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../external/gli +LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../external/assimp +LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../base/ +#LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../base/android + +LOCAL_SRC_FILES := $(PROJECT_FILES) + +LOCAL_LDLIBS := -landroid -llog -lz + +LOCAL_DISABLE_FORMAT_STRING_CHECKS := true + +LOCAL_STATIC_LIBRARIES += android_native_app_glue +LOCAL_STATIC_LIBRARIES += cpufeatures +LOCAL_STATIC_LIBRARIES += libassimp + +include $(BUILD_SHARED_LIBRARY) + +$(call import-module, android/native_app_glue) +$(call import-module, android/cpufeatures) diff --git a/android/bloom/jni/Application.mk b/android/bloom/jni/Application.mk new file mode 100644 index 00000000..62020feb --- /dev/null +++ b/android/bloom/jni/Application.mk @@ -0,0 +1,5 @@ +APP_PLATFORM := android-19 +APP_ABI := armeabi-v7a +APP_STL := c++_static +APP_CPPFLAGS := -std=c++11 +NDK_TOOLCHAIN_VERSION := clang diff --git a/android/build-bloom.bat b/android/build-bloom.bat new file mode 100644 index 00000000..51c93f96 --- /dev/null +++ b/android/build-bloom.bat @@ -0,0 +1 @@ +_build bloom %1 \ No newline at end of file diff --git a/android/build-computeshader.bat b/android/build-computeshader.bat new file mode 100644 index 00000000..62e0bc83 --- /dev/null +++ b/android/build-computeshader.bat @@ -0,0 +1 @@ +_build computeshader %1 \ No newline at end of file diff --git a/android/build-offscreen.bat b/android/build-offscreen.bat new file mode 100644 index 00000000..0f4ff1d3 --- /dev/null +++ b/android/build-offscreen.bat @@ -0,0 +1 @@ +_build offscreen %1 \ No newline at end of file diff --git a/android/computeshader/.gitignore b/android/computeshader/.gitignore new file mode 100644 index 00000000..7a5d249c --- /dev/null +++ b/android/computeshader/.gitignore @@ -0,0 +1,10 @@ +/assets/ +/res/ +/bin/ +/libs/ +/obj/ +/build.xml +/local.properties +/project.properties +/proguard-project.txt +*.apk \ No newline at end of file diff --git a/android/computeshader/AndroidManifest.xml b/android/computeshader/AndroidManifest.xml new file mode 100644 index 00000000..cb92e435 --- /dev/null +++ b/android/computeshader/AndroidManifest.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/android/computeshader/build.bat b/android/computeshader/build.bat new file mode 100644 index 00000000..22e2f2f9 --- /dev/null +++ b/android/computeshader/build.bat @@ -0,0 +1,21 @@ +cd jni +call ndk-build +if %ERRORLEVEL% EQU 0 ( + echo ndk-build has failed, build cancelled + cd.. + + mkdir "assets\shaders\computeshader" + xcopy "..\..\data\shaders\computeshader\*.spv" "assets\shaders\computeshader" /Y + + mkdir "assets\textures" + xcopy "..\..\data\textures\igor_and_pal_rgba.ktx" "assets\textures" /Y + + + mkdir "res\drawable" + xcopy "..\..\android\images\icon.png" "res\drawable" /Y + + call ant debug -Dout.final.file=vulkanComputeshader.apk +) ELSE ( + echo error : ndk-build failed with errors! + cd.. +) diff --git a/android/computeshader/jni/Android.mk b/android/computeshader/jni/Android.mk new file mode 100644 index 00000000..e4e2533b --- /dev/null +++ b/android/computeshader/jni/Android.mk @@ -0,0 +1,47 @@ +LOCAL_PATH := $(call my-dir)/../../computeshader + +# assimp + +include $(CLEAR_VARS) + +LOCAL_MODULE := assimp +LOCAL_SRC_FILES := $(LOCAL_PATH)/../../libs/assimp/$(TARGET_ARCH_ABI)/libassimp.a +include $(PREBUILT_STATIC_LIBRARY) + +# vulkan example + +DATADIR := $(LOCAL_PATH)/../../data + +include $(CLEAR_VARS) + +LOCAL_MODULE := vulkanComputeshader + +PROJECT_FILES := $(wildcard $(LOCAL_PATH)/../../computeshader/*.cpp) +PROJECT_FILES += $(wildcard $(LOCAL_PATH)/../../base/*.cpp) + +LOCAL_CPPFLAGS := -std=c++11 +LOCAL_CPPFLAGS += -D__STDC_LIMIT_MACROS +LOCAL_CPPFLAGS += -DVK_NO_PROTOTYPES +LOCAL_CPPFLAGS += -DVK_USE_PLATFORM_ANDROID_KHR + +LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../external/ +LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../external/glm +LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../external/gli +LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../external/assimp +LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../base/ +#LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../base/android + +LOCAL_SRC_FILES := $(PROJECT_FILES) + +LOCAL_LDLIBS := -landroid -llog -lz + +LOCAL_DISABLE_FORMAT_STRING_CHECKS := true + +LOCAL_STATIC_LIBRARIES += android_native_app_glue +LOCAL_STATIC_LIBRARIES += cpufeatures +LOCAL_STATIC_LIBRARIES += libassimp + +include $(BUILD_SHARED_LIBRARY) + +$(call import-module, android/native_app_glue) +$(call import-module, android/cpufeatures) diff --git a/android/computeshader/jni/Application.mk b/android/computeshader/jni/Application.mk new file mode 100644 index 00000000..62020feb --- /dev/null +++ b/android/computeshader/jni/Application.mk @@ -0,0 +1,5 @@ +APP_PLATFORM := android-19 +APP_ABI := armeabi-v7a +APP_STL := c++_static +APP_CPPFLAGS := -std=c++11 +NDK_TOOLCHAIN_VERSION := clang diff --git a/android/offscreen/.gitignore b/android/offscreen/.gitignore new file mode 100644 index 00000000..7a5d249c --- /dev/null +++ b/android/offscreen/.gitignore @@ -0,0 +1,10 @@ +/assets/ +/res/ +/bin/ +/libs/ +/obj/ +/build.xml +/local.properties +/project.properties +/proguard-project.txt +*.apk \ No newline at end of file diff --git a/android/offscreen/AndroidManifest.xml b/android/offscreen/AndroidManifest.xml new file mode 100644 index 00000000..f8eda2a9 --- /dev/null +++ b/android/offscreen/AndroidManifest.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/android/offscreen/build.bat b/android/offscreen/build.bat new file mode 100644 index 00000000..547b7ae1 --- /dev/null +++ b/android/offscreen/build.bat @@ -0,0 +1,24 @@ +cd jni +call ndk-build +if %ERRORLEVEL% EQU 0 ( + echo ndk-build has failed, build cancelled + cd.. + + mkdir "assets\shaders\offscreen" + xcopy "..\..\data\shaders\offscreen\*.spv" "assets\shaders\offscreen" /Y + + mkdir "assets\textures" + xcopy "..\..\data\textures\darkmetal_bc3.ktx" "assets\textures" /Y + + mkdir "assets\models" + xcopy "..\..\data\models\plane.obj" "assets\models" /Y + xcopy "..\..\data\models\chinesedragon.dae" "assets\models" /Y + + mkdir "res\drawable" + xcopy "..\..\android\images\icon.png" "res\drawable" /Y + + call ant debug -Dout.final.file=vulkanOffscreen.apk +) ELSE ( + echo error : ndk-build failed with errors! + cd.. +) \ No newline at end of file diff --git a/android/offscreen/jni/Android.mk b/android/offscreen/jni/Android.mk new file mode 100644 index 00000000..2609bf2d --- /dev/null +++ b/android/offscreen/jni/Android.mk @@ -0,0 +1,47 @@ +LOCAL_PATH := $(call my-dir)/../../offscreen + +# assimp + +include $(CLEAR_VARS) + +LOCAL_MODULE := assimp +LOCAL_SRC_FILES := $(LOCAL_PATH)/../../libs/assimp/$(TARGET_ARCH_ABI)/libassimp.a +include $(PREBUILT_STATIC_LIBRARY) + +# vulkan example + +DATADIR := $(LOCAL_PATH)/../../data + +include $(CLEAR_VARS) + +LOCAL_MODULE := vulkanOffscreen + +PROJECT_FILES := $(wildcard $(LOCAL_PATH)/../../offscreen/*.cpp) +PROJECT_FILES += $(wildcard $(LOCAL_PATH)/../../base/*.cpp) + +LOCAL_CPPFLAGS := -std=c++11 +LOCAL_CPPFLAGS += -D__STDC_LIMIT_MACROS +LOCAL_CPPFLAGS += -DVK_NO_PROTOTYPES +LOCAL_CPPFLAGS += -DVK_USE_PLATFORM_ANDROID_KHR + +LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../external/ +LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../external/glm +LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../external/gli +LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../external/assimp +LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../base/ +#LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../base/android + +LOCAL_SRC_FILES := $(PROJECT_FILES) + +LOCAL_LDLIBS := -landroid -llog -lz + +LOCAL_DISABLE_FORMAT_STRING_CHECKS := true + +LOCAL_STATIC_LIBRARIES += android_native_app_glue +LOCAL_STATIC_LIBRARIES += cpufeatures +LOCAL_STATIC_LIBRARIES += libassimp + +include $(BUILD_SHARED_LIBRARY) + +$(call import-module, android/native_app_glue) +$(call import-module, android/cpufeatures) \ No newline at end of file diff --git a/android/offscreen/jni/Application.mk b/android/offscreen/jni/Application.mk new file mode 100644 index 00000000..b6572a33 --- /dev/null +++ b/android/offscreen/jni/Application.mk @@ -0,0 +1,4 @@ +APP_PLATFORM := android-19 +APP_ABI := armeabi-v7a +APP_STL := c++_static +NDK_TOOLCHAIN_VERSION := clang diff --git a/bloom/bloom.cpp b/bloom/bloom.cpp index 93577742..60b8c857 100644 --- a/bloom/bloom.cpp +++ b/bloom/bloom.cpp @@ -607,7 +607,7 @@ public: void loadTextures() { textureLoader->loadCubemap( - "./../data/textures/cubemap_space.ktx", + getAssetPath() + "textures/cubemap_space.ktx", VK_FORMAT_R8G8B8A8_UNORM, &textures.cubemap); } @@ -741,9 +741,9 @@ public: void loadMeshes() { - loadMesh("./../data/models/retroufo.X", &meshes.ufo, vertexLayout, 0.05f); - loadMesh("./../data/models/retroufo_glow.X", &meshes.ufoGlow, vertexLayout, 0.05f); - loadMesh("./../data/models/cube.obj", &meshes.skyBox, vertexLayout, 1.0f); + loadMesh(getAssetPath() + "models/retroufo.dae", &meshes.ufo, vertexLayout, 0.05f); + loadMesh(getAssetPath() + "models/retroufo_glow.dae", &meshes.ufoGlow, vertexLayout, 0.05f); + loadMesh(getAssetPath() + "models/cube.obj", &meshes.skyBox, vertexLayout, 1.0f); } // Setup vertices for a single uv-mapped quad @@ -1072,8 +1072,8 @@ public: // Vertical gauss blur // Load shaders - shaderStages[0] = loadShader("./../data/shaders/bloom/gaussblur.vert.spv", VK_SHADER_STAGE_VERTEX_BIT); - shaderStages[1] = loadShader("./../data/shaders/bloom/gaussblur.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT); + shaderStages[0] = loadShader(getAssetPath() + "shaders/bloom/gaussblur.vert.spv", VK_SHADER_STAGE_VERTEX_BIT); + shaderStages[1] = loadShader(getAssetPath() + "shaders/bloom/gaussblur.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT); VkGraphicsPipelineCreateInfo pipelineCreateInfo = vkTools::initializers::pipelineCreateInfo( @@ -1106,8 +1106,8 @@ public: assert(!err); // Phong pass (3D model) - shaderStages[0] = loadShader("./../data/shaders/bloom/phongpass.vert.spv", VK_SHADER_STAGE_VERTEX_BIT); - shaderStages[1] = loadShader("./../data/shaders/bloom/phongpass.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT); + shaderStages[0] = loadShader(getAssetPath() + "shaders/bloom/phongpass.vert.spv", VK_SHADER_STAGE_VERTEX_BIT); + shaderStages[1] = loadShader(getAssetPath() + "shaders/bloom/phongpass.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT); pipelineCreateInfo.layout = pipelineLayouts.scene; blendAttachmentState.blendEnable = VK_FALSE; @@ -1117,15 +1117,15 @@ public: assert(!err); // Color only pass (offscreen blur base) - shaderStages[0] = loadShader("./../data/shaders/bloom/colorpass.vert.spv", VK_SHADER_STAGE_VERTEX_BIT); - shaderStages[1] = loadShader("./../data/shaders/bloom/colorpass.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT); + shaderStages[0] = loadShader(getAssetPath() + "shaders/bloom/colorpass.vert.spv", VK_SHADER_STAGE_VERTEX_BIT); + shaderStages[1] = loadShader(getAssetPath() + "shaders/bloom/colorpass.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT); err = vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.colorPass); assert(!err); // Skybox (cubemap - shaderStages[0] = loadShader("./../data/shaders/bloom/skybox.vert.spv", VK_SHADER_STAGE_VERTEX_BIT); - shaderStages[1] = loadShader("./../data/shaders/bloom/skybox.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT); + shaderStages[0] = loadShader(getAssetPath() + "shaders/bloom/skybox.vert.spv", VK_SHADER_STAGE_VERTEX_BIT); + shaderStages[1] = loadShader(getAssetPath() + "shaders/bloom/skybox.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT); depthStencilState.depthWriteEnable = VK_FALSE; err = vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.skyBox); assert(!err); @@ -1305,8 +1305,7 @@ public: VulkanExample *vulkanExample; -#ifdef _WIN32 - +#if defined(_WIN32) LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { if (vulkanExample != NULL) @@ -1330,9 +1329,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) } return (DefWindowProc(hWnd, uMsg, wParam, lParam)); } - -#else - +#elif defined(__linux__) && !defined(__ANDROID__) static void handleEvent(const xcb_generic_event_t *event) { if (vulkanExample != NULL) @@ -1342,21 +1339,42 @@ static void handleEvent(const xcb_generic_event_t *event) } #endif -#ifdef _WIN32 +// Main entry point +#if defined(_WIN32) +// Windows entry point int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow) -#else +#elif defined(__ANDROID__) +// Android entry point +void android_main(android_app* state) +#elif defined(__linux__) +// Linux entry point int main(const int argc, const char *argv[]) #endif { +#if defined(__ANDROID__) + // Removing this may cause the compiler to omit the main entry point + // which would make the application crash at start + app_dummy(); +#endif vulkanExample = new VulkanExample(); -#ifdef _WIN32 +#if defined(_WIN32) vulkanExample->setupWindow(hInstance, WndProc); -#else +#elif defined(__ANDROID__) + // Attach vulkan example to global android application state + state->userData = vulkanExample; + state->onAppCmd = VulkanExample::handleAppCommand; + state->onInputEvent = VulkanExample::handleAppInput; + vulkanExample->androidApp = state; +#elif defined(__linux__) vulkanExample->setupWindow(); #endif +#if !defined(__ANDROID__) vulkanExample->initSwapchain(); vulkanExample->prepare(); +#endif vulkanExample->renderLoop(); +#if !defined(__ANDROID__) delete(vulkanExample); return 0; -} +#endif +} \ No newline at end of file diff --git a/computeshader/computeshader.cpp b/computeshader/computeshader.cpp index 9f1d3f4e..e5cf946a 100644 --- a/computeshader/computeshader.cpp +++ b/computeshader/computeshader.cpp @@ -182,7 +182,7 @@ public: void loadTextures() { textureLoader->loadTexture( - "./../data/textures/igor_and_pal_rgba.ktx", + getAssetPath() + "textures/igor_and_pal_rgba.ktx", VK_FORMAT_R8G8B8A8_UNORM, &textureColorMap, false, @@ -602,8 +602,8 @@ public: // Load shaders std::array shaderStages; - shaderStages[0] = loadShader("./../data/shaders/computeshader/texture.vert.spv", VK_SHADER_STAGE_VERTEX_BIT); - shaderStages[1] = loadShader("./../data/shaders/computeshader/texture.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT); + shaderStages[0] = loadShader(getAssetPath() + "shaders/computeshader/texture.vert.spv", VK_SHADER_STAGE_VERTEX_BIT); + shaderStages[1] = loadShader(getAssetPath() + "shaders/computeshader/texture.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT); VkGraphicsPipelineCreateInfo pipelineCreateInfo = vkTools::initializers::pipelineCreateInfo( @@ -721,7 +721,7 @@ public: for (auto& shaderName : shaderNames) { - std::string fileName = "./../data/shaders/computeshader/" + shaderName + ".comp.spv"; + std::string fileName = getAssetPath() + "shaders/computeshader/" + shaderName + ".comp.spv"; computePipelineCreateInfo.stage = loadShader(fileName.c_str(), VK_SHADER_STAGE_COMPUTE_BIT); VkPipeline pipeline; err = vkCreateComputePipelines(device, pipelineCache, 1, &computePipelineCreateInfo, nullptr, &pipeline); @@ -843,29 +843,23 @@ public: VulkanExample *vulkanExample; -#ifdef _WIN32 - +#if defined(_WIN32) LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { if (vulkanExample != NULL) { vulkanExample->handleMessages(hWnd, uMsg, wParam, lParam); - if (uMsg == WM_KEYDOWN) + switch (wParam) { - switch (wParam) - { - case VK_ADD: - case VK_SUBTRACT: - vulkanExample->switchComputePipeline((wParam == VK_ADD) ? 1 : -1); - break; - } + case VK_ADD: + case VK_SUBTRACT: + vulkanExample->switchComputePipeline((wParam == VK_ADD) ? 1 : -1); + break; } } return (DefWindowProc(hWnd, uMsg, wParam, lParam)); } - -#else - +#elif defined(__linux__) && !defined(__ANDROID__) static void handleEvent(const xcb_generic_event_t *event) { if (vulkanExample != NULL) @@ -875,21 +869,42 @@ static void handleEvent(const xcb_generic_event_t *event) } #endif -#ifdef _WIN32 +// Main entry point +#if defined(_WIN32) +// Windows entry point int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow) -#else +#elif defined(__ANDROID__) +// Android entry point +void android_main(android_app* state) +#elif defined(__linux__) +// Linux entry point int main(const int argc, const char *argv[]) #endif { +#if defined(__ANDROID__) + // Removing this may cause the compiler to omit the main entry point + // which would make the application crash at start + app_dummy(); +#endif vulkanExample = new VulkanExample(); -#ifdef _WIN32 +#if defined(_WIN32) vulkanExample->setupWindow(hInstance, WndProc); -#else +#elif defined(__ANDROID__) + // Attach vulkan example to global android application state + state->userData = vulkanExample; + state->onAppCmd = VulkanExample::handleAppCommand; + state->onInputEvent = VulkanExample::handleAppInput; + vulkanExample->androidApp = state; +#elif defined(__linux__) vulkanExample->setupWindow(); #endif +#if !defined(__ANDROID__) vulkanExample->initSwapchain(); vulkanExample->prepare(); +#endif vulkanExample->renderLoop(); +#if !defined(__ANDROID__) delete(vulkanExample); return 0; +#endif } diff --git a/offscreen/offscreen.cpp b/offscreen/offscreen.cpp index 0c1c2953..0d439798 100644 --- a/offscreen/offscreen.cpp +++ b/offscreen/offscreen.cpp @@ -633,14 +633,14 @@ public: void loadMeshes() { - loadMesh("./../data/models/plane.obj", &meshes.plane, vertexLayout, 0.4f); - loadMesh("./../data/models/chinesedragon.X", &meshes.example, vertexLayout, 0.3f); + loadMesh(getAssetPath() + "models/plane.obj", &meshes.plane, vertexLayout, 0.4f); + loadMesh(getAssetPath() + "models/chinesedragon.dae", &meshes.example, vertexLayout, 0.3f); } void loadTextures() { textureLoader->loadTexture( - "./../data/textures/darkmetal_bc3.ktx", + getAssetPath() + "textures/darkmetal_bc3.ktx", VK_FORMAT_BC3_UNORM_BLOCK, &textures.colorMap); } @@ -949,8 +949,8 @@ public: // Load shaders std::array shaderStages; - shaderStages[0] = loadShader("./../data/shaders/offscreen/quad.vert.spv", VK_SHADER_STAGE_VERTEX_BIT); - shaderStages[1] = loadShader("./../data/shaders/offscreen/quad.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT); + shaderStages[0] = loadShader(getAssetPath() + "shaders/offscreen/quad.vert.spv", VK_SHADER_STAGE_VERTEX_BIT); + shaderStages[1] = loadShader(getAssetPath() + "shaders/offscreen/quad.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT); VkGraphicsPipelineCreateInfo pipelineCreateInfo = vkTools::initializers::pipelineCreateInfo( @@ -973,15 +973,15 @@ public: assert(!err); // Mirror - shaderStages[0] = loadShader("./../data/shaders/offscreen/mirror.vert.spv", VK_SHADER_STAGE_VERTEX_BIT); - shaderStages[1] = loadShader("./../data/shaders/offscreen/mirror.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT); + shaderStages[0] = loadShader(getAssetPath() + "shaders/offscreen/mirror.vert.spv", VK_SHADER_STAGE_VERTEX_BIT); + shaderStages[1] = loadShader(getAssetPath() + "shaders/offscreen/mirror.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT); err = vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.mirror); assert(!err); // Solid shading pipeline - shaderStages[0] = loadShader("./../data/shaders/offscreen/offscreen.vert.spv", VK_SHADER_STAGE_VERTEX_BIT); - shaderStages[1] = loadShader("./../data/shaders/offscreen/offscreen.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT); + shaderStages[0] = loadShader(getAssetPath() + "shaders/offscreen/offscreen.vert.spv", VK_SHADER_STAGE_VERTEX_BIT); + shaderStages[1] = loadShader(getAssetPath() + "shaders/offscreen/offscreen.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT); pipelineCreateInfo.layout = pipelineLayouts.offscreen; @@ -1135,8 +1135,7 @@ public: VulkanExample *vulkanExample; -#ifdef _WIN32 - +#if defined(_WIN32) LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { if (vulkanExample != NULL) @@ -1145,9 +1144,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) } return (DefWindowProc(hWnd, uMsg, wParam, lParam)); } - -#else - +#elif defined(__linux__) && !defined(__ANDROID__) static void handleEvent(const xcb_generic_event_t *event) { if (vulkanExample != NULL) @@ -1157,21 +1154,42 @@ static void handleEvent(const xcb_generic_event_t *event) } #endif -#ifdef _WIN32 +// Main entry point +#if defined(_WIN32) +// Windows entry point int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow) -#else +#elif defined(__ANDROID__) +// Android entry point +void android_main(android_app* state) +#elif defined(__linux__) +// Linux entry point int main(const int argc, const char *argv[]) #endif { +#if defined(__ANDROID__) + // Removing this may cause the compiler to omit the main entry point + // which would make the application crash at start + app_dummy(); +#endif vulkanExample = new VulkanExample(); -#ifdef _WIN32 +#if defined(_WIN32) vulkanExample->setupWindow(hInstance, WndProc); -#else +#elif defined(__ANDROID__) + // Attach vulkan example to global android application state + state->userData = vulkanExample; + state->onAppCmd = VulkanExample::handleAppCommand; + state->onInputEvent = VulkanExample::handleAppInput; + vulkanExample->androidApp = state; +#elif defined(__linux__) vulkanExample->setupWindow(); #endif +#if !defined(__ANDROID__) vulkanExample->initSwapchain(); vulkanExample->prepare(); +#endif vulkanExample->renderLoop(); +#if !defined(__ANDROID__) delete(vulkanExample); return 0; -} +#endif +} \ No newline at end of file