From 8bd272fc362713d13407b53c38cc7babe7f0b5c3 Mon Sep 17 00:00:00 2001 From: Michael Lentine Date: Mon, 18 Apr 2016 10:14:32 -0500 Subject: [PATCH] Make tri work on android. --- demos/android/AndroidManifest.xml | 22 +++++ demos/android/include/tri.frag.h | 95 +++++++++++++++++++ demos/android/include/tri.vert.h | 128 ++++++++++++++++++++++++++ demos/android/jni/Android.mk | 43 +++++++++ demos/android/jni/Application.mk | 22 +++++ demos/tri.c | 187 +++++++++++++++++++++++++++++++++----- 6 files changed, 472 insertions(+), 25 deletions(-) create mode 100644 demos/android/AndroidManifest.xml create mode 100644 demos/android/include/tri.frag.h create mode 100644 demos/android/include/tri.vert.h create mode 100644 demos/android/jni/Android.mk create mode 100644 demos/android/jni/Application.mk diff --git a/demos/android/AndroidManifest.xml b/demos/android/AndroidManifest.xml new file mode 100644 index 0000000..d23e9a4 --- /dev/null +++ b/demos/android/AndroidManifest.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/demos/android/include/tri.frag.h b/demos/android/include/tri.frag.h new file mode 100644 index 0000000..9def3a3 --- /dev/null +++ b/demos/android/include/tri.frag.h @@ -0,0 +1,95 @@ +#include + +#if 0 +../tri.frag +Warning, version 400 is not yet complete; most version-specific features are present, but some are missing. + + +Linked fragment stage: + + +// Module Version 10000 +// Generated by (magic number): 80001 +// Id's are bound by 20 + + Capability Shader + 1: ExtInstImport "GLSL.std.450" + MemoryModel Logical GLSL450 + EntryPoint Fragment 4 "main" 9 17 + ExecutionMode 4 OriginUpperLeft + Source GLSL 400 + SourceExtension "GL_ARB_separate_shader_objects" + SourceExtension "GL_ARB_shading_language_420pack" + Name 4 "main" + Name 9 "uFragColor" + Name 13 "tex" + Name 17 "texcoord" + Decorate 9(uFragColor) Location 0 + Decorate 13(tex) DescriptorSet 0 + Decorate 13(tex) Binding 0 + Decorate 17(texcoord) Location 0 + 2: TypeVoid + 3: TypeFunction 2 + 6: TypeFloat 32 + 7: TypeVector 6(float) 4 + 8: TypePointer Output 7(fvec4) + 9(uFragColor): 8(ptr) Variable Output + 10: TypeImage 6(float) 2D sampled format:Unknown + 11: TypeSampledImage 10 + 12: TypePointer UniformConstant 11 + 13(tex): 12(ptr) Variable UniformConstant + 15: TypeVector 6(float) 2 + 16: TypePointer Input 15(fvec2) + 17(texcoord): 16(ptr) Variable Input + 4(main): 2 Function None 3 + 5: Label + 14: 11 Load 13(tex) + 18: 15(fvec2) Load 17(texcoord) + 19: 7(fvec4) ImageSampleImplicitLod 14 18 + Store 9(uFragColor) 19 + Return + FunctionEnd +#endif + +static const uint32_t tri_frag[157] = { + 0x07230203, 0x00010000, 0x00080001, 0x00000014, + 0x00000000, 0x00020011, 0x00000001, 0x0006000b, + 0x00000001, 0x4c534c47, 0x6474732e, 0x3035342e, + 0x00000000, 0x0003000e, 0x00000000, 0x00000001, + 0x0007000f, 0x00000004, 0x00000004, 0x6e69616d, + 0x00000000, 0x00000009, 0x00000011, 0x00030010, + 0x00000004, 0x00000007, 0x00030003, 0x00000002, + 0x00000190, 0x00090004, 0x415f4c47, 0x735f4252, + 0x72617065, 0x5f657461, 0x64616873, 0x6f5f7265, + 0x63656a62, 0x00007374, 0x00090004, 0x415f4c47, + 0x735f4252, 0x69646168, 0x6c5f676e, 0x75676e61, + 0x5f656761, 0x70303234, 0x006b6361, 0x00040005, + 0x00000004, 0x6e69616d, 0x00000000, 0x00050005, + 0x00000009, 0x61724675, 0x6c6f4367, 0x0000726f, + 0x00030005, 0x0000000d, 0x00786574, 0x00050005, + 0x00000011, 0x63786574, 0x64726f6f, 0x00000000, + 0x00040047, 0x00000009, 0x0000001e, 0x00000000, + 0x00040047, 0x0000000d, 0x00000022, 0x00000000, + 0x00040047, 0x0000000d, 0x00000021, 0x00000000, + 0x00040047, 0x00000011, 0x0000001e, 0x00000000, + 0x00020013, 0x00000002, 0x00030021, 0x00000003, + 0x00000002, 0x00030016, 0x00000006, 0x00000020, + 0x00040017, 0x00000007, 0x00000006, 0x00000004, + 0x00040020, 0x00000008, 0x00000003, 0x00000007, + 0x0004003b, 0x00000008, 0x00000009, 0x00000003, + 0x00090019, 0x0000000a, 0x00000006, 0x00000001, + 0x00000000, 0x00000000, 0x00000000, 0x00000001, + 0x00000000, 0x0003001b, 0x0000000b, 0x0000000a, + 0x00040020, 0x0000000c, 0x00000000, 0x0000000b, + 0x0004003b, 0x0000000c, 0x0000000d, 0x00000000, + 0x00040017, 0x0000000f, 0x00000006, 0x00000002, + 0x00040020, 0x00000010, 0x00000001, 0x0000000f, + 0x0004003b, 0x00000010, 0x00000011, 0x00000001, + 0x00050036, 0x00000002, 0x00000004, 0x00000000, + 0x00000003, 0x000200f8, 0x00000005, 0x0004003d, + 0x0000000b, 0x0000000e, 0x0000000d, 0x0004003d, + 0x0000000f, 0x00000012, 0x00000011, 0x00050057, + 0x00000007, 0x00000013, 0x0000000e, 0x00000012, + 0x0003003e, 0x00000009, 0x00000013, 0x000100fd, + 0x00010038, +}; diff --git a/demos/android/include/tri.vert.h b/demos/android/include/tri.vert.h new file mode 100644 index 0000000..e9728fd --- /dev/null +++ b/demos/android/include/tri.vert.h @@ -0,0 +1,128 @@ +#include + +#if 0 +../tri.vert +Warning, version 400 is not yet complete; most version-specific features are present, but some are missing. + + +Linked vertex stage: + + +// Module Version 10000 +// Generated by (magic number): 80001 +// Id's are bound by 27 + + Capability Shader + Capability ClipDistance + 1: ExtInstImport "GLSL.std.450" + MemoryModel Logical GLSL450 + EntryPoint Vertex 4 "main" 9 11 19 23 + Source GLSL 400 + SourceExtension "GL_ARB_separate_shader_objects" + SourceExtension "GL_ARB_shading_language_420pack" + Name 4 "main" + Name 9 "texcoord" + Name 11 "attr" + Name 17 "gl_PerVertex" + MemberName 17(gl_PerVertex) 0 "gl_Position" + MemberName 17(gl_PerVertex) 1 "gl_PointSize" + MemberName 17(gl_PerVertex) 2 "gl_ClipDistance" + Name 19 "" + Name 23 "pos" + Decorate 9(texcoord) Location 0 + Decorate 11(attr) Location 1 + MemberDecorate 17(gl_PerVertex) 0 BuiltIn Position + MemberDecorate 17(gl_PerVertex) 1 BuiltIn PointSize + MemberDecorate 17(gl_PerVertex) 2 BuiltIn ClipDistance + Decorate 17(gl_PerVertex) Block + Decorate 23(pos) Location 0 + 2: TypeVoid + 3: TypeFunction 2 + 6: TypeFloat 32 + 7: TypeVector 6(float) 2 + 8: TypePointer Output 7(fvec2) + 9(texcoord): 8(ptr) Variable Output + 10: TypePointer Input 7(fvec2) + 11(attr): 10(ptr) Variable Input + 13: TypeVector 6(float) 4 + 14: TypeInt 32 0 + 15: 14(int) Constant 1 + 16: TypeArray 6(float) 15 +17(gl_PerVertex): TypeStruct 13(fvec4) 6(float) 16 + 18: TypePointer Output 17(gl_PerVertex) + 19: 18(ptr) Variable Output + 20: TypeInt 32 1 + 21: 20(int) Constant 0 + 22: TypePointer Input 13(fvec4) + 23(pos): 22(ptr) Variable Input + 25: TypePointer Output 13(fvec4) + 4(main): 2 Function None 3 + 5: Label + 12: 7(fvec2) Load 11(attr) + Store 9(texcoord) 12 + 24: 13(fvec4) Load 23(pos) + 26: 25(ptr) AccessChain 19 21 + Store 26 24 + Return + FunctionEnd +#endif + +static const uint32_t tri_vert[228] = { + 0x07230203, 0x00010000, 0x00080001, 0x0000001b, + 0x00000000, 0x00020011, 0x00000001, 0x00020011, + 0x00000020, 0x0006000b, 0x00000001, 0x4c534c47, + 0x6474732e, 0x3035342e, 0x00000000, 0x0003000e, + 0x00000000, 0x00000001, 0x0009000f, 0x00000000, + 0x00000004, 0x6e69616d, 0x00000000, 0x00000009, + 0x0000000b, 0x00000013, 0x00000017, 0x00030003, + 0x00000002, 0x00000190, 0x00090004, 0x415f4c47, + 0x735f4252, 0x72617065, 0x5f657461, 0x64616873, + 0x6f5f7265, 0x63656a62, 0x00007374, 0x00090004, + 0x415f4c47, 0x735f4252, 0x69646168, 0x6c5f676e, + 0x75676e61, 0x5f656761, 0x70303234, 0x006b6361, + 0x00040005, 0x00000004, 0x6e69616d, 0x00000000, + 0x00050005, 0x00000009, 0x63786574, 0x64726f6f, + 0x00000000, 0x00040005, 0x0000000b, 0x72747461, + 0x00000000, 0x00060005, 0x00000011, 0x505f6c67, + 0x65567265, 0x78657472, 0x00000000, 0x00060006, + 0x00000011, 0x00000000, 0x505f6c67, 0x7469736f, + 0x006e6f69, 0x00070006, 0x00000011, 0x00000001, + 0x505f6c67, 0x746e696f, 0x657a6953, 0x00000000, + 0x00070006, 0x00000011, 0x00000002, 0x435f6c67, + 0x4470696c, 0x61747369, 0x0065636e, 0x00030005, + 0x00000013, 0x00000000, 0x00030005, 0x00000017, + 0x00736f70, 0x00040047, 0x00000009, 0x0000001e, + 0x00000000, 0x00040047, 0x0000000b, 0x0000001e, + 0x00000001, 0x00050048, 0x00000011, 0x00000000, + 0x0000000b, 0x00000000, 0x00050048, 0x00000011, + 0x00000001, 0x0000000b, 0x00000001, 0x00050048, + 0x00000011, 0x00000002, 0x0000000b, 0x00000003, + 0x00030047, 0x00000011, 0x00000002, 0x00040047, + 0x00000017, 0x0000001e, 0x00000000, 0x00020013, + 0x00000002, 0x00030021, 0x00000003, 0x00000002, + 0x00030016, 0x00000006, 0x00000020, 0x00040017, + 0x00000007, 0x00000006, 0x00000002, 0x00040020, + 0x00000008, 0x00000003, 0x00000007, 0x0004003b, + 0x00000008, 0x00000009, 0x00000003, 0x00040020, + 0x0000000a, 0x00000001, 0x00000007, 0x0004003b, + 0x0000000a, 0x0000000b, 0x00000001, 0x00040017, + 0x0000000d, 0x00000006, 0x00000004, 0x00040015, + 0x0000000e, 0x00000020, 0x00000000, 0x0004002b, + 0x0000000e, 0x0000000f, 0x00000001, 0x0004001c, + 0x00000010, 0x00000006, 0x0000000f, 0x0005001e, + 0x00000011, 0x0000000d, 0x00000006, 0x00000010, + 0x00040020, 0x00000012, 0x00000003, 0x00000011, + 0x0004003b, 0x00000012, 0x00000013, 0x00000003, + 0x00040015, 0x00000014, 0x00000020, 0x00000001, + 0x0004002b, 0x00000014, 0x00000015, 0x00000000, + 0x00040020, 0x00000016, 0x00000001, 0x0000000d, + 0x0004003b, 0x00000016, 0x00000017, 0x00000001, + 0x00040020, 0x00000019, 0x00000003, 0x0000000d, + 0x00050036, 0x00000002, 0x00000004, 0x00000000, + 0x00000003, 0x000200f8, 0x00000005, 0x0004003d, + 0x00000007, 0x0000000c, 0x0000000b, 0x0003003e, + 0x00000009, 0x0000000c, 0x0004003d, 0x0000000d, + 0x00000018, 0x00000017, 0x00050041, 0x00000019, + 0x0000001a, 0x00000013, 0x00000015, 0x0003003e, + 0x0000001a, 0x00000018, 0x000100fd, 0x00010038, +}; diff --git a/demos/android/jni/Android.mk b/demos/android/jni/Android.mk new file mode 100644 index 0000000..3f8031e --- /dev/null +++ b/demos/android/jni/Android.mk @@ -0,0 +1,43 @@ +# Copyright 2015 The Android Open Source Project +# Copyright (C) 2015 Valve Corporation + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +LOCAL_PATH := $(abspath $(call my-dir)) +SRC_DIR := $(LOCAL_PATH)/../../../ +DEMO_DIR := $(SRC_DIR)/demos + +include $(CLEAR_VARS) +LOCAL_MODULE := Tri +LOCAL_SRC_FILES += $(DEMO_DIR)/tri.c +LOCAL_SRC_FILES += $(DEMO_DIR)/HelpersDispatchTable.cpp +LOCAL_C_INCLUDES += $(SRC_DIR)/include \ + $(DEMO_DIR)/android/include \ + $(SRC_DIR)/libs +LOCAL_CFLAGS += -DVK_USE_PLATFORM_ANDROID_KHR +LOCAL_WHOLE_STATIC_LIBRARIES += android_native_app_glue +LOCAL_LDLIBS := -llog -landroid -lvulkan +include $(BUILD_SHARED_LIBRARY) + +include $(CLEAR_VARS) +LOCAL_MODULE := Cube +LOCAL_SRC_FILES += $(DEMO_DIR)/cube.c +LOCAL_SRC_FILES += $(DEMO_DIR)/HelpersDispatchTable.cpp +LOCAL_C_INCLUDES += $(SRC_DIR)/include \ + $(SRC_DIR)/libs +LOCAL_CPPFLAGS += -DVK_USE_PLATFORM_ANDROID_KHR +LOCAL_WHOLE_STATIC_LIBRARIES += android_native_app_glue +LOCAL_LDLIBS := -llog -landroid -lvulkan +include $(BUILD_SHARED_LIBRARY) + +$(call import-module,android/native_app_glue) diff --git a/demos/android/jni/Application.mk b/demos/android/jni/Application.mk new file mode 100644 index 0000000..ff0ca07 --- /dev/null +++ b/demos/android/jni/Application.mk @@ -0,0 +1,22 @@ +# Copyright 2015 The Android Open Source Project +# Copyright (C) 2015 Valve Corporation + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +APP_ABI := armeabi-v7a arm64-v8a x86 x86_64 mips mips64 +APP_PLATFORM := android-23 +APP_STL := gnustl_static +APP_MODULES := Tri +APP_CPPFLAGS += -std=c++11 -fexceptions -Wall -Werror -Wextra -Wno-unused-parameter -DVK_NO_PROTOTYES -DGLM_FORCE_RADIANS +APP_CFLAGS += -Wall -Werror -Wextra -Wno-unused-parameter -DVK_NO_PROTOTYES -DGLM_FORCE_RADIANS +NDK_TOOLCHAIN_VERSION := clang diff --git a/demos/tri.c b/demos/tri.c index a1d4222..867c85a 100644 --- a/demos/tri.c +++ b/demos/tri.c @@ -65,15 +65,21 @@ MessageBox(NULL, err_msg, err_class, MB_OK); \ exit(1); \ } while (0) -#else // _WIN32 - +#elif defined __ANDROID__ +#include +#define ERR_EXIT(err_msg, err_class) \ + do { \ + ((void)__android_log_print(ANDROID_LOG_INFO, "Tri", err_msg)); \ + exit(1); \ + } while (0) +#else #define ERR_EXIT(err_msg, err_class) \ do { \ printf(err_msg); \ fflush(stdout); \ exit(1); \ } while (0) -#endif // _WIN32 +#endif #define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \ { \ @@ -167,17 +173,19 @@ typedef struct _SwapchainBuffers { } SwapchainBuffers; struct demo { -#ifdef _WIN32 +#if defined(VK_USE_PLATFORM_WIN32_KHR) #define APP_NAME_STR_LEN 80 HINSTANCE connection; // hInstance - Windows Instance char name[APP_NAME_STR_LEN]; // Name to put on the window/icon HWND window; // hWnd - window handle -#else // _WIN32 +#elif defined(VK_USE_PLATFORM_XCB_KHR) xcb_connection_t *connection; xcb_screen_t *screen; xcb_window_t window; xcb_intern_atom_reply_t *atom_wm_delete_window; -#endif // _WIN32 +#elif defined(VK_USE_PLATFORM_ANDROID_KHR) + ANativeWindow* window; +#endif VkSurfaceKHR surface; bool prepared; bool use_staging_buffer; @@ -1206,6 +1214,8 @@ static void demo_prepare_render_pass(struct demo *demo) { assert(!err); } +//TODO: Merge shader reading +#ifndef __ANDROID__ static VkShaderModule demo_prepare_shader_module(struct demo *demo, const void *code, size_t size) { VkShaderModuleCreateInfo moduleCreateInfo; @@ -1248,8 +1258,19 @@ char *demo_read_spv(const char *filename, size_t *psize) { fclose(fp); return shader_code; } +#endif static VkShaderModule demo_prepare_vs(struct demo *demo) { +#ifdef __ANDROID__ + VkShaderModuleCreateInfo sh_info = {}; + sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; + +#include "tri.vert.h" + sh_info.codeSize = sizeof(tri_vert); + sh_info.pCode = tri_vert; + VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->vert_shader_module); + assert(!err); +#else void *vertShaderCode; size_t size = 0; @@ -1259,11 +1280,22 @@ static VkShaderModule demo_prepare_vs(struct demo *demo) { demo_prepare_shader_module(demo, vertShaderCode, size); free(vertShaderCode); +#endif return demo->vert_shader_module; } static VkShaderModule demo_prepare_fs(struct demo *demo) { +#ifdef __ANDROID__ + VkShaderModuleCreateInfo sh_info = {}; + sh_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; + +#include "tri.frag.h" + sh_info.codeSize = sizeof(tri_frag); + sh_info.pCode = tri_frag; + VkResult U_ASSERT_ONLY err = vkCreateShaderModule(demo->device, &sh_info, NULL, &demo->frag_shader_module); + assert(!err); +#else void *fragShaderCode; size_t size; @@ -1273,6 +1305,7 @@ static VkShaderModule demo_prepare_fs(struct demo *demo) { demo_prepare_shader_module(demo, fragShaderCode, size); free(fragShaderCode); +#endif return demo->frag_shader_module; } @@ -1514,7 +1547,7 @@ static void demo_prepare(struct demo *demo) { demo->prepared = true; } -#ifdef _WIN32 +#if defined(VK_USE_PLATFORM_WIN32_KHR) static void demo_run(struct demo *demo) { if (!demo->prepared) return; @@ -1612,8 +1645,7 @@ static void demo_create_window(struct demo *demo) { exit(1); } } -#else // _WIN32 - +#elif defined(VK_USE_PLATFORM_XCB_KHR) static void demo_handle_event(struct demo *demo, const xcb_generic_event_t *event) { switch (event->response_type & 0x7f) { @@ -1712,7 +1744,22 @@ static void demo_create_window(struct demo *demo) { xcb_map_window(demo->connection, demo->window); } -#endif // _WIN32 +#elif defined(VK_USE_PLATFORM_ANDROID_KHR) +static void demo_run(struct demo *demo) { + if (!demo->prepared) + return; + demo_draw(demo); + + if (demo->depthStencil > 0.99f) + demo->depthIncrement = -0.001f; + if (demo->depthStencil < 0.8f) + demo->depthIncrement = 0.001f; + + demo->depthStencil += demo->depthIncrement; + + demo->curFrame++; +} +#endif /* * Return 1 (true) if all layer names specified in check_names @@ -1830,21 +1877,28 @@ static void demo_init_vk(struct demo *demo) { demo->extension_names[demo->enabled_extension_count++] = VK_KHR_SURFACE_EXTENSION_NAME; } -#ifdef _WIN32 +#if defined(VK_USE_PLATFORM_WIN32_KHR) if (!strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) { platformSurfaceExtFound = 1; demo->extension_names[demo->enabled_extension_count++] = VK_KHR_WIN32_SURFACE_EXTENSION_NAME; } -#else // _WIN32 +#elif defined(VK_USE_PLATFORM_XCB_KHR) if (!strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME, instance_extensions[i].extensionName)) { platformSurfaceExtFound = 1; demo->extension_names[demo->enabled_extension_count++] = VK_KHR_XCB_SURFACE_EXTENSION_NAME; } -#endif // _WIN32 +#elif defined(VK_USE_PLATFORM_ANDROID_KHR) + if (!strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME, + instance_extensions[i].extensionName)) { + platformSurfaceExtFound = 1; + demo->extension_names[demo->enabled_extension_count++] = + VK_KHR_ANDROID_SURFACE_EXTENSION_NAME; + } +#endif if (!strcmp(VK_EXT_DEBUG_REPORT_EXTENSION_NAME, instance_extensions[i].extensionName)) { if (demo->validate) { @@ -1868,7 +1922,7 @@ static void demo_init_vk(struct demo *demo) { "vkCreateInstance Failure"); } if (!platformSurfaceExtFound) { -#ifdef _WIN32 +#if defined(VK_USE_PLATFORM_WIN32_KHR) ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find " "the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME " extension.\n\nDo you have a compatible " @@ -1876,7 +1930,7 @@ static void demo_init_vk(struct demo *demo) { "look at the Getting Started guide for additional " "information.\n", "vkCreateInstance Failure"); -#else // _WIN32 +#elif defined(VK_USE_PLATFORM_XCB_KHR) ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find " "the " VK_KHR_XCB_SURFACE_EXTENSION_NAME " extension.\n\nDo you have a compatible " @@ -1884,7 +1938,15 @@ static void demo_init_vk(struct demo *demo) { "look at the Getting Started guide for additional " "information.\n", "vkCreateInstance Failure"); -#endif // _WIN32 +#elif defined(VK_USE_PLATFORM_ANDROID_KHR) + ERR_EXIT("vkEnumerateInstanceExtensionProperties failed to find " + "the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME + " extension.\n\nDo you have a compatible " + "Vulkan installable client driver (ICD) installed?\nPlease " + "look at the Getting Started guide for additional " + "information.\n", + "vkCreateInstance Failure"); +#endif } const VkApplicationInfo app = { .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, @@ -2095,10 +2157,13 @@ static void demo_init_vk(struct demo *demo) { VkPhysicalDeviceFeatures features; vkGetPhysicalDeviceFeatures(demo->gpu, &features); + //TODO: Enable this +#ifndef __ANDROID__ if (!features.shaderClipDistance) { ERR_EXIT("Required device feature `shaderClipDistance` not supported\n", "GetPhysicalDeviceFeatures failure"); } +#endif // Graphics queue and MemMgr queue can be separate. // TODO: Add support for separate queues, including synchronization, @@ -2144,7 +2209,7 @@ static void demo_init_vk_swapchain(struct demo *demo) { uint32_t i; // Create a WSI surface for the window: -#ifdef _WIN32 +#if defined(VK_USE_PLATFORM_WIN32_KHR) VkWin32SurfaceCreateInfoKHR createInfo; createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR; createInfo.pNext = NULL; @@ -2154,8 +2219,7 @@ static void demo_init_vk_swapchain(struct demo *demo) { err = vkCreateWin32SurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface); - -#else // _WIN32 +#elif defined(VK_USE_PLATFORM_XCB_KHR) VkXcbSurfaceCreateInfoKHR createInfo; createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR; createInfo.pNext = NULL; @@ -2164,7 +2228,15 @@ static void demo_init_vk_swapchain(struct demo *demo) { createInfo.window = demo->window; err = vkCreateXcbSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface); -#endif // _WIN32 +#elif defined(VK_USE_PLATFORM_ANDROID_KHR) + VkAndroidSurfaceCreateInfoKHR createInfo; + createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR; + createInfo.pNext = NULL; + createInfo.flags = 0; + createInfo.window = (ANativeWindow*)(demo->window); + + err = vkCreateAndroidSurfaceKHR(demo->inst, &createInfo, NULL, &demo->surface); +#endif // Iterate over each queue to learn whether it supports presenting: VkBool32 *supportsPresent = @@ -2256,7 +2328,7 @@ static void demo_init_vk_swapchain(struct demo *demo) { } static void demo_init_connection(struct demo *demo) { -#ifndef _WIN32 +#if defined(VK_USE_PLATFORM_XCB_KHR) const xcb_setup_t *setup; xcb_screen_iterator_t iter; int scr; @@ -2371,7 +2443,7 @@ static void demo_cleanup(struct demo *demo) { free(demo->queue_props); -#ifndef _WIN32 +#if defined(VK_USE_PLATFORM_XCB_KHR) xcb_destroy_window(demo->connection, demo->window); xcb_disconnect(demo->connection); free(demo->atom_wm_delete_window); @@ -2433,7 +2505,7 @@ static void demo_resize(struct demo *demo) { demo_prepare(demo); } -#ifdef _WIN32 +#if defined(VK_USE_PLATFORM_WIN32_KHR) // Include header required for parsing the command line options. #include @@ -2513,7 +2585,71 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, return (int)msg.wParam; } -#else // _WIN32 + +#elif defined(VK_USE_PLATFORM_ANDROID_KHR) + +#include +#include +static bool initialized = false; +static bool active = false; +struct demo demo; + +static int32_t processInput(struct android_app* app, AInputEvent* event) { + return 0; +} + +static void processCommand(struct android_app* app, int32_t cmd) { + switch(cmd) { + case APP_CMD_INIT_WINDOW: { + if (app->window) { + demo_init(&demo, 0, NULL); + demo.window = (void*)app->window; + demo_init_vk_swapchain(&demo); + demo_prepare(&demo); + initialized = true; + } + break; + } + case APP_CMD_GAINED_FOCUS: { + active = true; + break; + } + case APP_CMD_LOST_FOCUS: { + active = false; + break; + } + } +} + +void android_main(struct android_app *app) +{ + app_dummy(); + + app->onAppCmd = processCommand; + app->onInputEvent = processInput; + + while(1) { + int events; + struct android_poll_source* source; + while (ALooper_pollAll(active ? 0 : -1, NULL, &events, (void**)&source) >= 0) { + if (source) { + source->process(app, source); + } + + if (app->destroyRequested != 0) { + demo_cleanup(&demo); + return; + } + } + if (initialized && active) { + demo_run(&demo); + } + } + +} + +#elif defined(VK_USE_PLATFORM_XCB_KHR) + int main(const int argc, const char *argv[]) { struct demo demo; @@ -2528,4 +2664,5 @@ int main(const int argc, const char *argv[]) { return validation_error; } -#endif // _WIN32 + +#endif -- 2.7.4