Fixed ifdefs (#91), added preliminary android support to vulkan example base (#97)

This commit is contained in:
saschawillems 2016-03-20 14:55:46 +01:00
parent 92cdb2060e
commit f13614e6d2
2 changed files with 64 additions and 27 deletions

View file

@ -22,10 +22,12 @@ VkResult VulkanExampleBase::createInstance(bool enableValidation)
std::vector<const char*> enabledExtensions = { VK_KHR_SURFACE_EXTENSION_NAME }; std::vector<const char*> 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); enabledExtensions.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
#else #elif defined(__ANDROID__)
// todo : linux/android enabledExtensions.push_back(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME);
#elif defined(__linux__)
enabledExtensions.push_back(VK_KHR_XCB_SURFACE_EXTENSION_NAME); enabledExtensions.push_back(VK_KHR_XCB_SURFACE_EXTENSION_NAME);
#endif #endif
@ -196,7 +198,6 @@ void VulkanExampleBase::prepare()
{ {
vkDebug::setupDebugging(instance, VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT, NULL); vkDebug::setupDebugging(instance, VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT, NULL);
} }
createCommandPool(); createCommandPool();
createSetupCommandBuffer(); createSetupCommandBuffer();
setupSwapChain(); setupSwapChain();
@ -271,6 +272,7 @@ VkBool32 VulkanExampleBase::createBuffer(VkBufferUsageFlags usage, VkDeviceSize
} }
} }
#ifndef __ANDROID__
void VulkanExampleBase::loadMesh( void VulkanExampleBase::loadMesh(
const char * filename, const char * filename,
vkMeshLoader::MeshBuffer * meshBuffer, vkMeshLoader::MeshBuffer * meshBuffer,
@ -290,10 +292,11 @@ void VulkanExampleBase::loadMesh(
delete(mesh); delete(mesh);
} }
#endif
void VulkanExampleBase::renderLoop() void VulkanExampleBase::renderLoop()
{ {
#ifdef _WIN32 #if defined(_WIN32)
MSG msg; MSG msg;
while (TRUE) while (TRUE)
{ {
@ -333,7 +336,9 @@ void VulkanExampleBase::renderLoop()
frameCounter = 0.0f; frameCounter = 0.0f;
} }
} }
#else #elif defined(__ANDROID__)
// todo : Android renderlopp
#elif defined(__linux__)
xcb_flush(connection); xcb_flush(connection);
while (!quit) while (!quit)
{ {
@ -369,7 +374,7 @@ void VulkanExampleBase::renderLoop()
fpsTimer = 0.0f; fpsTimer = 0.0f;
frameCounter = 0.0f; frameCounter = 0.0f;
} }
} }
#endif #endif
} }
@ -465,7 +470,7 @@ VkSubmitInfo VulkanExampleBase::prepareSubmitInfo(
VulkanExampleBase::VulkanExampleBase(bool enableValidation) VulkanExampleBase::VulkanExampleBase(bool enableValidation)
{ {
// Check for validation command line flag // Check for validation command line flag
#ifdef _WIN32 #if defined(_WIN32)
for (int32_t i = 0; i < __argc; i++) for (int32_t i = 0; i < __argc; i++)
{ {
if (__argv[i] == std::string("-validation")) if (__argv[i] == std::string("-validation"))
@ -473,15 +478,22 @@ VulkanExampleBase::VulkanExampleBase(bool enableValidation)
enableValidation = true; enableValidation = true;
} }
} }
#endif #elif defined(__ANDROID__)
// Vulkan library is loaded dynamically on Android
#ifndef _WIN32 bool libLoaded = loadVulkanLibrary();
assert(libLoaded);
#elif defined(__linux__)
initxcbConnection(); initxcbConnection();
#endif #endif
#if !defined(__ANDROID__)
// Android Vulkan initialization is handled in APP_CMD_INIT_WINDOW event
initVulkan(enableValidation); initVulkan(enableValidation);
#endif
#if defined(_WIN32)
// Enable console if validation is active // Enable console if validation is active
// Debug message callback will output to it // Debug message callback will output to it
#ifdef _WIN32
if (enableValidation) if (enableValidation)
{ {
setupConsole("VulkanExample"); setupConsole("VulkanExample");
@ -535,10 +547,14 @@ VulkanExampleBase::~VulkanExampleBase()
vkDestroyInstance(instance, nullptr); vkDestroyInstance(instance, nullptr);
#ifndef _WIN32 #if defined(__linux)
#if defined(__ANDROID__)
// todo : android cleanup (if required)
#else
xcb_destroy_window(connection, window); xcb_destroy_window(connection, window);
xcb_disconnect(connection); xcb_disconnect(connection);
#endif #endif
#endif
} }
void VulkanExampleBase::initVulkan(bool enableValidation) 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"); vkTools::exitFatal("Could not create Vulkan instance : \n" + vkTools::errorString(err), "Fatal error");
} }
#if defined(__ANDROID__)
loadVulkanFunctions(instance);
#endif
// Physical device // Physical device
uint32_t gpuCount = 0; uint32_t gpuCount = 0;
// Get number of available physical devices // Get number of available physical devices
@ -602,6 +622,10 @@ void VulkanExampleBase::initVulkan(bool enableValidation)
vkGetPhysicalDeviceProperties(physicalDevice, &deviceProperties); vkGetPhysicalDeviceProperties(physicalDevice, &deviceProperties);
#if defined(__ANDROID__)
LOGD(deviceProperties.deviceName);
#endif
// Gather physical device memory properties // Gather physical device memory properties
vkGetPhysicalDeviceMemoryProperties(physicalDevice, &deviceMemoryProperties); vkGetPhysicalDeviceMemoryProperties(physicalDevice, &deviceMemoryProperties);
@ -636,7 +660,7 @@ void VulkanExampleBase::initVulkan(bool enableValidation)
submitInfo.pSignalSemaphores = &semaphores.renderComplete; submitInfo.pSignalSemaphores = &semaphores.renderComplete;
} }
#ifdef _WIN32 #if defined(_WIN32)
// Win32 : Sets up a console window and redirects standard output to it // Win32 : Sets up a console window and redirects standard output to it
void VulkanExampleBase::setupConsole(std::string title) void VulkanExampleBase::setupConsole(std::string title)
{ {
@ -835,9 +859,9 @@ void VulkanExampleBase::handleMessages(HWND hWnd, UINT uMsg, WPARAM wParam, LPAR
break; break;
} }
} }
#elif defined(__ANDROID__)
#else // todo : Android event handling
#elif defined(__linux__)
// Set up a window using XCB and request event types // Set up a window using XCB and request event types
xcb_window_t VulkanExampleBase::setupWindow() xcb_window_t VulkanExampleBase::setupWindow()
{ {
@ -1149,9 +1173,11 @@ void VulkanExampleBase::setupRenderPass()
void VulkanExampleBase::initSwapchain() void VulkanExampleBase::initSwapchain()
{ {
#ifdef _WIN32 #if defined(_WIN32)
swapChain.initSurface(windowInstance, window); swapChain.initSurface(windowInstance, window);
#else #elif defined(__ANDROID__)
swapChain.initSurface(window);
#elif defined(__linux__)
swapChain.initSurface(connection, window); swapChain.initSurface(connection, window);
#endif #endif
} }

View file

@ -13,8 +13,10 @@
#include <windows.h> #include <windows.h>
#include <fcntl.h> #include <fcntl.h>
#include <io.h> #include <io.h>
#else #elif defined(__ANDROID__)
// todo : split linux xcb/x11 and android #include <android/native_activity.h>
#include "vulkanandroid.h"
#elif defined(__linux__)
#include <xcb/xcb.h> #include <xcb/xcb.h>
#endif #endif
@ -34,7 +36,9 @@
#include "vulkanswapchain.hpp" #include "vulkanswapchain.hpp"
#include "vulkanTextureLoader.hpp" #include "vulkanTextureLoader.hpp"
#ifndef __ANDROID__
#include "vulkanMeshLoader.hpp" #include "vulkanMeshLoader.hpp"
#endif
class VulkanExampleBase class VulkanExampleBase
{ {
@ -146,10 +150,12 @@ public:
} depthStencil; } depthStencil;
// OS specific // OS specific
#ifdef _WIN32 #if defined(_WIN32)
HWND window; HWND window;
HINSTANCE windowInstance; HINSTANCE windowInstance;
#else #elif defined(__ANDROID__)
ANativeWindow* window;
#elif defined(__linux__)
struct { struct {
bool left = false; bool left = false;
bool right = false; bool right = false;
@ -159,7 +165,7 @@ public:
xcb_screen_t *screen; xcb_screen_t *screen;
xcb_window_t window; xcb_window_t window;
xcb_intern_atom_reply_t *atom_wm_delete_window; xcb_intern_atom_reply_t *atom_wm_delete_window;
#endif #endif
VulkanExampleBase(bool enableValidation); VulkanExampleBase(bool enableValidation);
VulkanExampleBase() : VulkanExampleBase(false) {}; VulkanExampleBase() : VulkanExampleBase(false) {};
@ -168,15 +174,18 @@ public:
// Setup the vulkan instance, enable required extensions and connect to the physical device (GPU) // Setup the vulkan instance, enable required extensions and connect to the physical device (GPU)
void initVulkan(bool enableValidation); void initVulkan(bool enableValidation);
#ifdef _WIN32 #if defined(_WIN32)
void setupConsole(std::string title); void setupConsole(std::string title);
HWND setupWindow(HINSTANCE hinstance, WNDPROC wndproc); HWND setupWindow(HINSTANCE hinstance, WNDPROC wndproc);
void handleMessages(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); 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(); xcb_window_t setupWindow();
void initxcbConnection(); void initxcbConnection();
void handleEvent(const xcb_generic_event_t *event); void handleEvent(const xcb_generic_event_t *event);
#endif #endif
// Pure virtual render function (override in derived class) // Pure virtual render function (override in derived class)
virtual void render() = 0; virtual void render() = 0;
// Called when view change occurs // Called when view change occurs
@ -243,12 +252,14 @@ public:
VkDescriptorBufferInfo *descriptor); VkDescriptorBufferInfo *descriptor);
// Load a mesh (using ASSIMP) and create vulkan vertex and index buffers with given vertex layout // 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( void loadMesh(
const char *filename, const char *filename,
vkMeshLoader::MeshBuffer *meshBuffer, vkMeshLoader::MeshBuffer *meshBuffer,
std::vector<vkMeshLoader::VertexLayout> vertexLayout, std::vector<vkMeshLoader::VertexLayout> vertexLayout,
float scale); float scale);
#endif
// Start the main render loop // Start the main render loop
void renderLoop(); void renderLoop();