diff --git a/base/vulkanexamplebase.cpp b/base/vulkanexamplebase.cpp index 7c1372cd..6a901058 100644 --- a/base/vulkanexamplebase.cpp +++ b/base/vulkanexamplebase.cpp @@ -22,10 +22,12 @@ VkResult VulkanExampleBase::createInstance(bool enableValidation) std::vector enabledExtensions = { VK_KHR_SURFACE_EXTENSION_NAME }; -#ifdef _WIN32 + // Enable surface extensions depending on os +#if defined(_WIN32) enabledExtensions.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME); -#else - // todo : linux/android +#elif defined(__ANDROID__) + enabledExtensions.push_back(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME); +#elif defined(__linux__) enabledExtensions.push_back(VK_KHR_XCB_SURFACE_EXTENSION_NAME); #endif @@ -196,7 +198,6 @@ void VulkanExampleBase::prepare() { vkDebug::setupDebugging(instance, VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT, NULL); } - createCommandPool(); createSetupCommandBuffer(); setupSwapChain(); @@ -271,6 +272,7 @@ VkBool32 VulkanExampleBase::createBuffer(VkBufferUsageFlags usage, VkDeviceSize } } +#ifndef __ANDROID__ void VulkanExampleBase::loadMesh( const char * filename, vkMeshLoader::MeshBuffer * meshBuffer, @@ -290,10 +292,11 @@ void VulkanExampleBase::loadMesh( delete(mesh); } +#endif void VulkanExampleBase::renderLoop() { -#ifdef _WIN32 +#if defined(_WIN32) MSG msg; while (TRUE) { @@ -333,7 +336,9 @@ void VulkanExampleBase::renderLoop() frameCounter = 0.0f; } } -#else +#elif defined(__ANDROID__) + // todo : Android renderlopp +#elif defined(__linux__) xcb_flush(connection); while (!quit) { @@ -369,7 +374,7 @@ void VulkanExampleBase::renderLoop() fpsTimer = 0.0f; frameCounter = 0.0f; } -} + } #endif } @@ -465,7 +470,7 @@ VkSubmitInfo VulkanExampleBase::prepareSubmitInfo( VulkanExampleBase::VulkanExampleBase(bool enableValidation) { // Check for validation command line flag -#ifdef _WIN32 +#if defined(_WIN32) for (int32_t i = 0; i < __argc; i++) { if (__argv[i] == std::string("-validation")) @@ -473,15 +478,22 @@ VulkanExampleBase::VulkanExampleBase(bool enableValidation) enableValidation = true; } } -#endif - -#ifndef _WIN32 +#elif defined(__ANDROID__) + // Vulkan library is loaded dynamically on Android + bool libLoaded = loadVulkanLibrary(); + assert(libLoaded); +#elif defined(__linux__) initxcbConnection(); #endif + +#if !defined(__ANDROID__) + // Android Vulkan initialization is handled in APP_CMD_INIT_WINDOW event initVulkan(enableValidation); +#endif + +#if defined(_WIN32) // Enable console if validation is active // Debug message callback will output to it -#ifdef _WIN32 if (enableValidation) { setupConsole("VulkanExample"); @@ -535,10 +547,14 @@ VulkanExampleBase::~VulkanExampleBase() vkDestroyInstance(instance, nullptr); -#ifndef _WIN32 +#if defined(__linux) +#if defined(__ANDROID__) + // todo : android cleanup (if required) +#else xcb_destroy_window(connection, window); xcb_disconnect(connection); #endif +#endif } void VulkanExampleBase::initVulkan(bool enableValidation) @@ -552,6 +568,10 @@ void VulkanExampleBase::initVulkan(bool enableValidation) vkTools::exitFatal("Could not create Vulkan instance : \n" + vkTools::errorString(err), "Fatal error"); } +#if defined(__ANDROID__) + loadVulkanFunctions(instance); +#endif + // Physical device uint32_t gpuCount = 0; // Get number of available physical devices @@ -602,6 +622,10 @@ void VulkanExampleBase::initVulkan(bool enableValidation) vkGetPhysicalDeviceProperties(physicalDevice, &deviceProperties); +#if defined(__ANDROID__) + LOGD(deviceProperties.deviceName); +#endif + // Gather physical device memory properties vkGetPhysicalDeviceMemoryProperties(physicalDevice, &deviceMemoryProperties); @@ -636,7 +660,7 @@ void VulkanExampleBase::initVulkan(bool enableValidation) submitInfo.pSignalSemaphores = &semaphores.renderComplete; } -#ifdef _WIN32 +#if defined(_WIN32) // Win32 : Sets up a console window and redirects standard output to it void VulkanExampleBase::setupConsole(std::string title) { @@ -835,9 +859,9 @@ void VulkanExampleBase::handleMessages(HWND hWnd, UINT uMsg, WPARAM wParam, LPAR break; } } - -#else - +#elif defined(__ANDROID__) + // todo : Android event handling +#elif defined(__linux__) // Set up a window using XCB and request event types xcb_window_t VulkanExampleBase::setupWindow() { @@ -1149,9 +1173,11 @@ void VulkanExampleBase::setupRenderPass() void VulkanExampleBase::initSwapchain() { -#ifdef _WIN32 +#if defined(_WIN32) swapChain.initSurface(windowInstance, window); -#else +#elif defined(__ANDROID__) + swapChain.initSurface(window); +#elif defined(__linux__) swapChain.initSurface(connection, window); #endif } diff --git a/base/vulkanexamplebase.h b/base/vulkanexamplebase.h index efaf9f0a..de1807d3 100644 --- a/base/vulkanexamplebase.h +++ b/base/vulkanexamplebase.h @@ -13,8 +13,10 @@ #include #include #include -#else -// todo : split linux xcb/x11 and android +#elif defined(__ANDROID__) +#include +#include "vulkanandroid.h" +#elif defined(__linux__) #include #endif @@ -34,7 +36,9 @@ #include "vulkanswapchain.hpp" #include "vulkanTextureLoader.hpp" +#ifndef __ANDROID__ #include "vulkanMeshLoader.hpp" +#endif class VulkanExampleBase { @@ -146,10 +150,12 @@ public: } depthStencil; // OS specific -#ifdef _WIN32 +#if defined(_WIN32) HWND window; HINSTANCE windowInstance; -#else +#elif defined(__ANDROID__) + ANativeWindow* window; +#elif defined(__linux__) struct { bool left = false; bool right = false; @@ -159,7 +165,7 @@ public: xcb_screen_t *screen; xcb_window_t window; xcb_intern_atom_reply_t *atom_wm_delete_window; -#endif +#endif VulkanExampleBase(bool enableValidation); VulkanExampleBase() : VulkanExampleBase(false) {}; @@ -168,15 +174,18 @@ public: // Setup the vulkan instance, enable required extensions and connect to the physical device (GPU) void initVulkan(bool enableValidation); -#ifdef _WIN32 +#if defined(_WIN32) void setupConsole(std::string title); HWND setupWindow(HINSTANCE hinstance, WNDPROC wndproc); void handleMessages(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); -#else +#elif defined(__ANDROID__) + // todo : add event handler for Android +#elif defined(__linux__) xcb_window_t setupWindow(); void initxcbConnection(); void handleEvent(const xcb_generic_event_t *event); #endif + // Pure virtual render function (override in derived class) virtual void render() = 0; // Called when view change occurs @@ -243,12 +252,14 @@ public: VkDescriptorBufferInfo *descriptor); // Load a mesh (using ASSIMP) and create vulkan vertex and index buffers with given vertex layout + // todo : mesh loader not yet enabled for Android +#ifndef __ANDROID__ void loadMesh( const char *filename, vkMeshLoader::MeshBuffer *meshBuffer, std::vector vertexLayout, float scale); - +#endif // Start the main render loop void renderLoop();