From 2105e9359e25ecde7a06453e2eb5601f10ef13c4 Mon Sep 17 00:00:00 2001 From: Kearwood Gilbert Date: Thu, 11 Jan 2024 23:05:13 -0800 Subject: [PATCH] Vulkan surface now initializing on MacOS Updated Volk library --- CMakeLists.txt | 2 +- kraken/KRContext.cpp | 7 +++---- kraken/KRSurface.cpp | 25 +++++++++---------------- kraken/KRSurface.h | 14 ++------------ kraken/KRSurfaceManager.cpp | 7 ++----- kraken/KRSurfaceManager.h | 4 +--- kraken/public/kraken.h | 6 +----- tests/smoke/hello_cube/CMakeLists.txt | 2 ++ tests/smoke/hello_cube/harness.cpp | 8 ++------ tests/smoke/hello_cube/harness.h | 2 +- tests/smoke/hello_cube/hello_cube.cpp | 4 +++- tests/smoke/hello_cube/main_macos.mm | 6 ++++-- 12 files changed, 31 insertions(+), 56 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4510060..e2b8eb7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -136,7 +136,7 @@ if(ANDROID) elseif(WIN32) target_compile_definitions(vulkan INTERFACE VK_USE_PLATFORM_WIN32_KHR) elseif(APPLE) - target_compile_definitions(vulkan INTERFACE VK_USE_PLATFORM_MACOS_MVK) + target_compile_definitions(vulkan INTERFACE VK_USE_PLATFORM_METAL_EXT) elseif(UNIX) # See whether X11 is available. If not, fall back to direct-to-display mode. find_package(X11 QUIET) diff --git a/kraken/KRContext.cpp b/kraken/KRContext.cpp index b3a7edf..2ccb916 100755 --- a/kraken/KRContext.cpp +++ b/kraken/KRContext.cpp @@ -839,14 +839,13 @@ KrResult KRContext::createWindowSurface(const KrCreateWindowSurfaceInfo* createW if (!m_deviceManager->haveDevice()) { return KR_ERROR_NO_DEVICE; } +#if defined(WIN32) || defined(__APPLE__) const std::lock_guard surfaceLock(KRContext::g_SurfaceInfoMutex); const std::lock_guard deviceLock(KRContext::g_DeviceInfoMutex); -#ifdef WIN32 - HWND hWnd = static_cast(createWindowSurfaceInfo->hWnd); KrSurfaceHandle surfaceHandle = 0; - KrResult result = m_surfaceManager->create(hWnd, surfaceHandle); + KrResult result = m_surfaceManager->create(createWindowSurfaceInfo->platformHandle, surfaceHandle); if (result != KR_SUCCESS) { return result; } @@ -891,4 +890,4 @@ KrResult KRContext::getMappedResource(KrResourceMapIndex resourceHandle, KRResou return KR_ERROR_NOT_MAPPED; } return KR_SUCCESS; -} \ No newline at end of file +} diff --git a/kraken/KRSurface.cpp b/kraken/KRSurface.cpp index 288bd2f..7eaa7f3 100644 --- a/kraken/KRSurface.cpp +++ b/kraken/KRSurface.cpp @@ -35,20 +35,10 @@ using namespace hydra; -#if defined(WIN32) -KRSurface::KRSurface(KRContext& context, KrSurfaceHandle handle, HWND hWnd) -#elif defined(__APPLE__) -KRSurface::KRSurface(KRContext& context, KrSurfaceHandle handle, CAMetalLayer* layer) -#else -KRSurface::KRSurface(KRContext& context, KrSurfaceHandle handle) -#endif +KRSurface::KRSurface(KRContext& context, KrSurfaceHandle handle, void* platformHandle) : KRContextObject(context) , m_handle(handle) -#ifdef WIN32 - , m_hWnd(hWnd) -#elif defined(__APPLE__) - , m_layer(layer) -#endif + , m_platformHandle(platformHandle) , m_deviceHandle(0) , m_surface(VK_NULL_HANDLE) , m_imageAvailableSemaphores{VK_NULL_HANDLE} @@ -73,14 +63,17 @@ KrResult KRSurface::initialize() VkWin32SurfaceCreateInfoKHR createInfo{}; createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR; createInfo.hinstance = GetModuleHandle(nullptr); - createInfo.hwnd = m_hWnd; + createInfo.hwnd = static_cast(m_platformHandle); if (vkCreateWin32SurfaceKHR(m_pContext->getDeviceManager()->getVulkanInstance(), &createInfo, nullptr, &m_surface) != VK_SUCCESS) { return KR_ERROR_VULKAN; } #elif defined(__APPLE__) - VkMetalSurfaceCreateInfoEXT createInfo{}; - createInfo.sType = VK_STRUCTURE_TYPE_METAL_SURFACE_CREATE_INFO_EXT; - createInfo.pLayer = m_layer; // CAMetalLayer + VkMetalSurfaceCreateInfoEXT createInfo{}; + createInfo.sType = VK_STRUCTURE_TYPE_METAL_SURFACE_CREATE_INFO_EXT; + createInfo.pLayer = static_cast(m_platformHandle); + if (vkCreateMetalSurfaceEXT(m_pContext->getDeviceManager()->getVulkanInstance(), &createInfo, nullptr, &m_surface) != VK_SUCCESS) { + return KR_ERROR_VULKAN; + } #else #error Unsupported #endif diff --git a/kraken/KRSurface.h b/kraken/KRSurface.h index 52c4dba..83ddce0 100644 --- a/kraken/KRSurface.h +++ b/kraken/KRSurface.h @@ -41,13 +41,7 @@ class KRSwapchain; class KRSurface : public KRContextObject { public: -#if defined(WIN32) - KRSurface(KRContext& context, KrSurfaceHandle handle, HWND hWnd); -#elif defined(__APPLE__) - KRSurface(KRContext& context, KrSurfaceHandle handle, CAMetalLayer* layer); -#else - KRSurface(KRContext& context, KrSurfaceHandle handle); -#endif + KRSurface(KRContext& context, KrSurfaceHandle handle, void* platformHandle); ~KRSurface(); void destroy(); uint32_t getWidth() const; @@ -69,11 +63,7 @@ public: void endFrame(); KrSurfaceHandle m_handle; -#ifdef WIN32 - HWND m_hWnd; -#elif defined(__APPLE__) - CAMetalLayer* m_layer; -#endif + void* m_platformHandle; KrDeviceHandle m_deviceHandle; VkSurfaceKHR m_surface; diff --git a/kraken/KRSurfaceManager.cpp b/kraken/KRSurfaceManager.cpp index d116606..d2eb081 100644 --- a/kraken/KRSurfaceManager.cpp +++ b/kraken/KRSurfaceManager.cpp @@ -48,12 +48,11 @@ void KRSurfaceManager::destroySurfaces() m_surfaces.clear(); } -#ifdef WIN32 -KrResult KRSurfaceManager::create(HWND hWnd, KrSurfaceHandle& surfaceHandle) +KrResult KRSurfaceManager::create(void* platformHandle, KrSurfaceHandle& surfaceHandle) { surfaceHandle = 0; - std::unique_ptr surface = std::make_unique(*m_pContext, m_topSurfaceHandle, hWnd); + std::unique_ptr surface = std::make_unique(*m_pContext, m_topSurfaceHandle, platformHandle); KrResult initialize_result = surface->initialize(); if (initialize_result != KR_SUCCESS) { @@ -66,8 +65,6 @@ KrResult KRSurfaceManager::create(HWND hWnd, KrSurfaceHandle& surfaceHandle) return KR_SUCCESS; } -#endif - KrResult KRSurfaceManager::destroy(KrSurfaceHandle& surfaceHandle) { auto itr = m_surfaces.find(surfaceHandle); diff --git a/kraken/KRSurfaceManager.h b/kraken/KRSurfaceManager.h index 0d66460..375aead 100644 --- a/kraken/KRSurfaceManager.h +++ b/kraken/KRSurfaceManager.h @@ -41,9 +41,7 @@ class KRSurfaceManager : KRContextObject public: KRSurfaceManager(KRContext& context); ~KRSurfaceManager(); -#ifdef WIN32 - KrResult create(HWND hWnd, KrSurfaceHandle& surfaceHandle); -#endif + KrResult create(void* platformHandle, KrSurfaceHandle& surfaceHandle); KRSurface& get(KrSurfaceHandle surfaceHandle); KrResult destroy(KrSurfaceHandle& surfaceHandle); unordered_map>& getSurfaces(); diff --git a/kraken/public/kraken.h b/kraken/public/kraken.h index b22748b..07335e0 100644 --- a/kraken/public/kraken.h +++ b/kraken/public/kraken.h @@ -132,11 +132,7 @@ typedef struct { KrStructureType sType; KrSurfaceMapIndex surfaceHandle; -#if defined(_WIN32) || defined(_WIN64) - void* hWnd; // Can static cast to HWND -#elif defined(__APPLE__) - void* view; // Can static cast to NSView -#endif + void* platformHandle; // Can static cast to HWND on Windows and CAMetalLayer* on macOS } KrCreateWindowSurfaceInfo; typedef struct diff --git a/tests/smoke/hello_cube/CMakeLists.txt b/tests/smoke/hello_cube/CMakeLists.txt index 81e1b9b..5619dd8 100644 --- a/tests/smoke/hello_cube/CMakeLists.txt +++ b/tests/smoke/hello_cube/CMakeLists.txt @@ -27,6 +27,8 @@ if (WIN32) else(WIN32) add_executable(kraken_cube main_macos.mm harness.cpp hello_cube.cpp) set(CMAKE_CXX_COMPILER "clang++") + list (APPEND EXTRA_LIBS "-framework QuartzCore") + list (APPEND EXTRA_LIBS "-framework Metal") endif(WIN32) add_dependencies(kraken_cube kraken_cube_assets) diff --git a/tests/smoke/hello_cube/harness.cpp b/tests/smoke/hello_cube/harness.cpp index c13957a..1238e16 100644 --- a/tests/smoke/hello_cube/harness.cpp +++ b/tests/smoke/hello_cube/harness.cpp @@ -34,7 +34,7 @@ #include "kraken.h" #include "hello_cube.h" -bool test_init(void* windowHandle) +bool test_init(void* platformHandle) { KrInitializeInfo init_info = {}; init_info.sType = KR_STRUCTURE_TYPE_INITIALIZE; @@ -50,11 +50,7 @@ bool test_init(void* windowHandle) KrCreateWindowSurfaceInfo create_surface_info = {}; create_surface_info.sType = KR_STRUCTURE_TYPE_CREATE_WINDOW_SURFACE; create_surface_info.surfaceHandle = 1; -#if defined(WIN32) || defined(WIN64) - create_surface_info.hWnd = windowHandle; -#elif defined(__APPLE__) - create_surface_info.view = windowHandle; -#endif + create_surface_info.platformHandle = platformHandle; res = KrCreateWindowSurface(&create_surface_info); if (res != KR_SUCCESS) { //printf("Failed to create window surface.\n"); diff --git a/tests/smoke/hello_cube/harness.h b/tests/smoke/hello_cube/harness.h index 6660985..1e430d2 100644 --- a/tests/smoke/hello_cube/harness.h +++ b/tests/smoke/hello_cube/harness.h @@ -31,5 +31,5 @@ #pragma once -bool test_init(void* windowHandle); +bool test_init(void* platformHandle); bool test_shutdown(); diff --git a/tests/smoke/hello_cube/hello_cube.cpp b/tests/smoke/hello_cube/hello_cube.cpp index f1fb022..2763004 100644 --- a/tests/smoke/hello_cube/hello_cube.cpp +++ b/tests/smoke/hello_cube/hello_cube.cpp @@ -53,7 +53,7 @@ bool smoke_load() if (res != KR_SUCCESS) { //printf("Failed to load resource: %s\n", arg); KrShutdown(); - return 1; + return false; } /* @@ -84,4 +84,6 @@ bool smoke_load() // create_camera_info.node.camera.skybox_texture = kSkyboxTextureResourceHandle; res = KrCreateNode(&create_camera_info); assert(res == KR_SUCCESS); + + return true; } diff --git a/tests/smoke/hello_cube/main_macos.mm b/tests/smoke/hello_cube/main_macos.mm index 511dac4..883909b 100644 --- a/tests/smoke/hello_cube/main_macos.mm +++ b/tests/smoke/hello_cube/main_macos.mm @@ -30,6 +30,7 @@ // #import "Cocoa/Cocoa.h" +#import "QuartzCore/CAMetalLayer.h" #include "kraken.h" #include "harness.h" @@ -59,10 +60,11 @@ int main(int argc, const char * argv[]) [windowController autorelease]; NSView *view = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 640, 480)]; - [view setWantsLayer:YES]; + view.wantsLayer = YES; + view.layer = [CAMetalLayer layer]; view.layer.backgroundColor = [[NSColor purpleColor] CGColor]; - if (!test_init(static_cast(view))) { + if (!test_init(static_cast(view.layer))) { return 1; }