Cleaned up defines (use VK_PLATFORM_* for XCB and Android)

This commit is contained in:
saschawillems 2017-08-13 10:24:25 +02:00
parent adefd08735
commit 55fe613db3
2 changed files with 28 additions and 31 deletions

View file

@ -1,7 +1,7 @@
/* /*
* Vulkan Example base class * Vulkan Example base class
* *
* Copyright (C) 2016 by Sascha Willems - www.saschawillems.de * Copyright (C) 2016-2017 by Sascha Willems - www.saschawillems.de
* *
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) * This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
*/ */
@ -30,13 +30,13 @@ VkResult VulkanExampleBase::createInstance(bool enableValidation)
// Enable surface extensions depending on os // Enable surface extensions depending on os
#if defined(_WIN32) #if defined(_WIN32)
instanceExtensions.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME); instanceExtensions.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
#elif defined(__ANDROID__) #elif defined(VK_USE_PLATFORM_ANDROID_KHR)
instanceExtensions.push_back(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME); instanceExtensions.push_back(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME);
#elif defined(_DIRECT2DISPLAY) #elif defined(_DIRECT2DISPLAY)
instanceExtensions.push_back(VK_KHR_DISPLAY_EXTENSION_NAME); instanceExtensions.push_back(VK_KHR_DISPLAY_EXTENSION_NAME);
#elif defined(VK_USE_PLATFORM_WAYLAND_KHR) #elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
instanceExtensions.push_back(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME); instanceExtensions.push_back(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME);
#elif defined(__linux__) #elif defined(VK_USE_PLATFORM_XCB_KHR)
instanceExtensions.push_back(VK_KHR_XCB_SURFACE_EXTENSION_NAME); instanceExtensions.push_back(VK_KHR_XCB_SURFACE_EXTENSION_NAME);
#elif defined(VK_USE_PLATFORM_IOS_MVK) #elif defined(VK_USE_PLATFORM_IOS_MVK)
instanceExtensions.push_back(VK_MVK_IOS_SURFACE_EXTENSION_NAME); instanceExtensions.push_back(VK_MVK_IOS_SURFACE_EXTENSION_NAME);
@ -81,7 +81,7 @@ std::string VulkanExampleBase::getWindowTitle()
// iOS & macOS: VulkanExampleBase::getAssetPath() implemented externally to allow access to Objective-C components // iOS & macOS: VulkanExampleBase::getAssetPath() implemented externally to allow access to Objective-C components
const std::string VulkanExampleBase::getAssetPath() const std::string VulkanExampleBase::getAssetPath()
{ {
#if defined(__ANDROID__) #if defined(VK_USE_PLATFORM_ANDROID_KHR)
return ""; return "";
#else #else
return "./../data/"; return "./../data/";
@ -211,7 +211,7 @@ VkPipelineShaderStageCreateInfo VulkanExampleBase::loadShader(std::string fileNa
VkPipelineShaderStageCreateInfo shaderStage = {}; VkPipelineShaderStageCreateInfo shaderStage = {};
shaderStage.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; shaderStage.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
shaderStage.stage = stage; shaderStage.stage = stage;
#if defined(__ANDROID__) #if defined(VK_USE_PLATFORM_ANDROID_KHR)
shaderStage.module = vks::tools::loadShader(androidApp->activity->assetManager, fileName.c_str(), device); shaderStage.module = vks::tools::loadShader(androidApp->activity->assetManager, fileName.c_str(), device);
#else #else
shaderStage.module = vks::tools::loadShader(fileName.c_str(), device); shaderStage.module = vks::tools::loadShader(fileName.c_str(), device);
@ -325,7 +325,7 @@ void VulkanExampleBase::renderLoop()
} }
renderFrame(); renderFrame();
} }
#elif defined(__ANDROID__) #elif defined(VK_USE_PLATFORM_ANDROID_KHR)
while (1) while (1)
{ {
int ident; int ident;
@ -521,7 +521,7 @@ void VulkanExampleBase::renderLoop()
frameCounter = 0; frameCounter = 0;
} }
} }
#elif defined(__linux__) #elif defined(VK_USE_PLATFORM_XCB_KHR)
xcb_flush(connection); xcb_flush(connection);
while (!quit) while (!quit)
{ {
@ -591,7 +591,7 @@ void VulkanExampleBase::updateTextOverlay()
textOverlay->addText(ss.str(), 5.0f, 25.0f, VulkanTextOverlay::alignLeft); textOverlay->addText(ss.str(), 5.0f, 25.0f, VulkanTextOverlay::alignLeft);
std::string deviceName(deviceProperties.deviceName); std::string deviceName(deviceProperties.deviceName);
#if defined(__ANDROID__) #if defined(VK_USE_PLATFORM_ANDROID_KHR)
deviceName += " (" + androidProduct + ")"; deviceName += " (" + androidProduct + ")";
#endif #endif
textOverlay->addText(deviceName, 5.0f, 45.0f, VulkanTextOverlay::alignLeft); textOverlay->addText(deviceName, 5.0f, 45.0f, VulkanTextOverlay::alignLeft);
@ -657,7 +657,7 @@ void VulkanExampleBase::submitFrame()
VulkanExampleBase::VulkanExampleBase(bool enableValidation) VulkanExampleBase::VulkanExampleBase(bool enableValidation)
{ {
#if !defined(__ANDROID__) #if !defined(VK_USE_PLATFORM_ANDROID_KHR)
// Check for a valid asset path // Check for a valid asset path
struct stat info; struct stat info;
if (stat(getAssetPath().c_str(), &info) != 0) if (stat(getAssetPath().c_str(), &info) != 0)
@ -711,7 +711,7 @@ VulkanExampleBase::VulkanExampleBase(bool enableValidation)
} }
} }
#if defined(__ANDROID__) #if defined(VK_USE_PLATFORM_ANDROID_KHR)
// Vulkan library is loaded dynamically on Android // Vulkan library is loaded dynamically on Android
bool libLoaded = vks::android::loadVulkanLibrary(); bool libLoaded = vks::android::loadVulkanLibrary();
assert(libLoaded); assert(libLoaded);
@ -719,7 +719,7 @@ VulkanExampleBase::VulkanExampleBase(bool enableValidation)
#elif defined(VK_USE_PLATFORM_WAYLAND_KHR) #elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
initWaylandConnection(); initWaylandConnection();
#elif defined(__linux__) #elif defined(VK_USE_PLATFORM_XCB_KHR)
initxcbConnection(); initxcbConnection();
#endif #endif
@ -792,14 +792,12 @@ VulkanExampleBase::~VulkanExampleBase()
wl_compositor_destroy(compositor); wl_compositor_destroy(compositor);
wl_registry_destroy(registry); wl_registry_destroy(registry);
wl_display_disconnect(display); wl_display_disconnect(display);
#elif defined(__linux) #elif defined(VK_USE_PLATFORM_ANDROID_KHR)
#if defined(__ANDROID__)
// todo : android cleanup (if required) // todo : android cleanup (if required)
#else #elif defined(VK_USE_PLATFORM_XCB_KHR)
xcb_destroy_window(connection, window); xcb_destroy_window(connection, window);
xcb_disconnect(connection); xcb_disconnect(connection);
#endif #endif
#endif
} }
void VulkanExampleBase::initVulkan() void VulkanExampleBase::initVulkan()
@ -813,7 +811,7 @@ void VulkanExampleBase::initVulkan()
vks::tools::exitFatal("Could not create Vulkan instance : \n" + vks::tools::errorString(err), "Fatal error"); vks::tools::exitFatal("Could not create Vulkan instance : \n" + vks::tools::errorString(err), "Fatal error");
} }
#if defined(__ANDROID__) #if defined(VK_USE_PLATFORM_ANDROID_KHR)
vks::android::loadVulkanFunctions(instance); vks::android::loadVulkanFunctions(instance);
#endif #endif
@ -846,7 +844,7 @@ void VulkanExampleBase::initVulkan()
// Defaults to the first device unless specified by command line // Defaults to the first device unless specified by command line
uint32_t selectedDevice = 0; uint32_t selectedDevice = 0;
#if !defined(__ANDROID__) #if !defined(VK_USE_PLATFORM_ANDROID_KHR)
// GPU selection via command line argument // GPU selection via command line argument
for (size_t i = 0; i < args.size(); i++) for (size_t i = 0; i < args.size(); i++)
{ {
@ -948,7 +946,7 @@ void VulkanExampleBase::initVulkan()
submitInfo.signalSemaphoreCount = 1; submitInfo.signalSemaphoreCount = 1;
submitInfo.pSignalSemaphores = &semaphores.renderComplete; submitInfo.pSignalSemaphores = &semaphores.renderComplete;
#if defined(__ANDROID__) #if defined(VK_USE_PLATFORM_ANDROID_KHR)
// Get Android device name and manufacturer (to display along GPU name) // Get Android device name and manufacturer (to display along GPU name)
androidProduct = ""; androidProduct = "";
char prop[PROP_VALUE_MAX+1]; char prop[PROP_VALUE_MAX+1];
@ -1226,7 +1224,7 @@ void VulkanExampleBase::handleMessages(HWND hWnd, UINT uMsg, WPARAM wParam, LPAR
break; break;
} }
} }
#elif defined(__ANDROID__) #elif defined(VK_USE_PLATFORM_ANDROID_KHR)
int32_t VulkanExampleBase::handleAppInput(struct android_app* app, AInputEvent* event) int32_t VulkanExampleBase::handleAppInput(struct android_app* app, AInputEvent* event)
{ {
VulkanExampleBase* vulkanExample = reinterpret_cast<VulkanExampleBase*>(app->userData); VulkanExampleBase* vulkanExample = reinterpret_cast<VulkanExampleBase*>(app->userData);
@ -1701,7 +1699,7 @@ wl_shell_surface *VulkanExampleBase::setupWindow()
return shell_surface; return shell_surface;
} }
#elif defined(__linux__) #elif defined(VK_USE_PLATFORM_XCB_KHR)
static inline xcb_intern_atom_reply_t* intern_atom_helper(xcb_connection_t *conn, bool only_if_exists, const char *str) static inline xcb_intern_atom_reply_t* intern_atom_helper(xcb_connection_t *conn, bool only_if_exists, const char *str)
{ {
@ -2157,7 +2155,7 @@ void VulkanExampleBase::initSwapchain()
{ {
#if defined(_WIN32) #if defined(_WIN32)
swapChain.initSurface(windowInstance, window); swapChain.initSurface(windowInstance, window);
#elif defined(__ANDROID__) #elif defined(VK_USE_PLATFORM_ANDROID_KHR)
swapChain.initSurface(androidApp->window); swapChain.initSurface(androidApp->window);
#elif (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK)) #elif (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
swapChain.initSurface(view); swapChain.initSurface(view);
@ -2165,7 +2163,7 @@ void VulkanExampleBase::initSwapchain()
swapChain.initSurface(width, height); swapChain.initSurface(width, height);
#elif defined(VK_USE_PLATFORM_WAYLAND_KHR) #elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
swapChain.initSurface(display, surface); swapChain.initSurface(display, surface);
#elif defined(__linux__) #elif defined(VK_USE_PLATFORM_XCB_KHR)
swapChain.initSurface(connection, window); swapChain.initSurface(connection, window);
#endif #endif
} }

View file

@ -13,7 +13,7 @@
#include <windows.h> #include <windows.h>
#include <fcntl.h> #include <fcntl.h>
#include <io.h> #include <io.h>
#elif defined(__ANDROID__) #elif defined(VK_USE_PLATFORM_ANDROID_KHR)
#include <android/native_activity.h> #include <android/native_activity.h>
#include <android/asset_manager.h> #include <android/asset_manager.h>
#include <android_native_app_glue.h> #include <android_native_app_glue.h>
@ -23,7 +23,7 @@
#include <wayland-client.h> #include <wayland-client.h>
#elif defined(_DIRECT2DISPLAY) #elif defined(_DIRECT2DISPLAY)
// //
#elif defined(__linux__) #elif defined(VK_USE_PLATFORM_XCB_KHR)
#include <xcb/xcb.h> #include <xcb/xcb.h>
#endif #endif
@ -209,7 +209,7 @@ public:
#if defined(_WIN32) #if defined(_WIN32)
HWND window; HWND window;
HINSTANCE windowInstance; HINSTANCE windowInstance;
#elif defined(__ANDROID__) #elif defined(VK_USE_PLATFORM_ANDROID_KHR)
// true if application has focused, false if moved to background // true if application has focused, false if moved to background
bool focused = false; bool focused = false;
struct TouchPos { struct TouchPos {
@ -241,7 +241,7 @@ public:
} mouseButtons; } mouseButtons;
#elif defined(_DIRECT2DISPLAY) #elif defined(_DIRECT2DISPLAY)
bool quit = false; bool quit = false;
#elif defined(__linux__) #elif defined(VK_USE_PLATFORM_XCB_KHR)
struct { struct {
bool left = false; bool left = false;
bool right = false; bool right = false;
@ -267,7 +267,7 @@ public:
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);
#elif defined(__ANDROID__) #elif defined(VK_USE_PLATFORM_ANDROID_KHR)
static int32_t handleAppInput(struct android_app* app, AInputEvent* event); static int32_t handleAppInput(struct android_app* app, AInputEvent* event);
static void handleAppCommand(android_app* app, int32_t cmd); static void handleAppCommand(android_app* app, int32_t cmd);
#elif (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK)) #elif (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
@ -316,7 +316,7 @@ public:
#elif defined(_DIRECT2DISPLAY) #elif defined(_DIRECT2DISPLAY)
// //
#elif defined(__linux__) #elif defined(VK_USE_PLATFORM_XCB_KHR)
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);
@ -437,7 +437,7 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int) \
delete(vulkanExample); \ delete(vulkanExample); \
return 0; \ return 0; \
} }
#elif defined(__ANDROID__) #elif defined(VK_USE_PLATFORM_ANDROID_KHR)
// Android entry point // Android entry point
// A note on app_dummy(): This is required as the compiler may otherwise remove the main entry point of the application // A note on app_dummy(): This is required as the compiler may otherwise remove the main entry point of the application
#define VULKAN_EXAMPLE_MAIN() \ #define VULKAN_EXAMPLE_MAIN() \
@ -487,8 +487,7 @@ int main(const int argc, const char *argv[]) \
delete(vulkanExample); \ delete(vulkanExample); \
return 0; \ return 0; \
} }
#elif defined(__linux__) #elif defined(VK_USE_PLATFORM_XCB_KHR)
// Linux entry point
#define VULKAN_EXAMPLE_MAIN() \ #define VULKAN_EXAMPLE_MAIN() \
VulkanExample *vulkanExample; \ VulkanExample *vulkanExample; \
static void handleEvent(const xcb_generic_event_t *event) \ static void handleEvent(const xcb_generic_event_t *event) \