Vulkan surface now initializing on MacOS

Updated Volk library
This commit is contained in:
2024-01-11 23:05:13 -08:00
parent 8214cf475a
commit 2105e9359e
12 changed files with 31 additions and 56 deletions

View File

@@ -136,7 +136,7 @@ if(ANDROID)
elseif(WIN32) elseif(WIN32)
target_compile_definitions(vulkan INTERFACE VK_USE_PLATFORM_WIN32_KHR) target_compile_definitions(vulkan INTERFACE VK_USE_PLATFORM_WIN32_KHR)
elseif(APPLE) elseif(APPLE)
target_compile_definitions(vulkan INTERFACE VK_USE_PLATFORM_MACOS_MVK) target_compile_definitions(vulkan INTERFACE VK_USE_PLATFORM_METAL_EXT)
elseif(UNIX) elseif(UNIX)
# See whether X11 is available. If not, fall back to direct-to-display mode. # See whether X11 is available. If not, fall back to direct-to-display mode.
find_package(X11 QUIET) find_package(X11 QUIET)

View File

@@ -839,14 +839,13 @@ KrResult KRContext::createWindowSurface(const KrCreateWindowSurfaceInfo* createW
if (!m_deviceManager->haveDevice()) { if (!m_deviceManager->haveDevice()) {
return KR_ERROR_NO_DEVICE; return KR_ERROR_NO_DEVICE;
} }
#if defined(WIN32) || defined(__APPLE__)
const std::lock_guard<std::mutex> surfaceLock(KRContext::g_SurfaceInfoMutex); const std::lock_guard<std::mutex> surfaceLock(KRContext::g_SurfaceInfoMutex);
const std::lock_guard<std::mutex> deviceLock(KRContext::g_DeviceInfoMutex); const std::lock_guard<std::mutex> deviceLock(KRContext::g_DeviceInfoMutex);
#ifdef WIN32
HWND hWnd = static_cast<HWND>(createWindowSurfaceInfo->hWnd);
KrSurfaceHandle surfaceHandle = 0; KrSurfaceHandle surfaceHandle = 0;
KrResult result = m_surfaceManager->create(hWnd, surfaceHandle); KrResult result = m_surfaceManager->create(createWindowSurfaceInfo->platformHandle, surfaceHandle);
if (result != KR_SUCCESS) { if (result != KR_SUCCESS) {
return result; return result;
} }
@@ -891,4 +890,4 @@ KrResult KRContext::getMappedResource(KrResourceMapIndex resourceHandle, KRResou
return KR_ERROR_NOT_MAPPED; return KR_ERROR_NOT_MAPPED;
} }
return KR_SUCCESS; return KR_SUCCESS;
} }

View File

@@ -35,20 +35,10 @@
using namespace hydra; using namespace hydra;
#if defined(WIN32) KRSurface::KRSurface(KRContext& context, KrSurfaceHandle handle, void* platformHandle)
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
: KRContextObject(context) : KRContextObject(context)
, m_handle(handle) , m_handle(handle)
#ifdef WIN32 , m_platformHandle(platformHandle)
, m_hWnd(hWnd)
#elif defined(__APPLE__)
, m_layer(layer)
#endif
, m_deviceHandle(0) , m_deviceHandle(0)
, m_surface(VK_NULL_HANDLE) , m_surface(VK_NULL_HANDLE)
, m_imageAvailableSemaphores{VK_NULL_HANDLE} , m_imageAvailableSemaphores{VK_NULL_HANDLE}
@@ -73,14 +63,17 @@ KrResult KRSurface::initialize()
VkWin32SurfaceCreateInfoKHR createInfo{}; VkWin32SurfaceCreateInfoKHR createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR; createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
createInfo.hinstance = GetModuleHandle(nullptr); createInfo.hinstance = GetModuleHandle(nullptr);
createInfo.hwnd = m_hWnd; createInfo.hwnd = static_cast<HWND>(m_platformHandle);
if (vkCreateWin32SurfaceKHR(m_pContext->getDeviceManager()->getVulkanInstance(), &createInfo, nullptr, &m_surface) != VK_SUCCESS) { if (vkCreateWin32SurfaceKHR(m_pContext->getDeviceManager()->getVulkanInstance(), &createInfo, nullptr, &m_surface) != VK_SUCCESS) {
return KR_ERROR_VULKAN; return KR_ERROR_VULKAN;
} }
#elif defined(__APPLE__) #elif defined(__APPLE__)
VkMetalSurfaceCreateInfoEXT createInfo{}; VkMetalSurfaceCreateInfoEXT createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_METAL_SURFACE_CREATE_INFO_EXT; createInfo.sType = VK_STRUCTURE_TYPE_METAL_SURFACE_CREATE_INFO_EXT;
createInfo.pLayer = m_layer; // CAMetalLayer createInfo.pLayer = static_cast<CAMetalLayer*>(m_platformHandle);
if (vkCreateMetalSurfaceEXT(m_pContext->getDeviceManager()->getVulkanInstance(), &createInfo, nullptr, &m_surface) != VK_SUCCESS) {
return KR_ERROR_VULKAN;
}
#else #else
#error Unsupported #error Unsupported
#endif #endif

View File

@@ -41,13 +41,7 @@ class KRSwapchain;
class KRSurface : public KRContextObject class KRSurface : public KRContextObject
{ {
public: public:
#if defined(WIN32) KRSurface(KRContext& context, KrSurfaceHandle handle, void* platformHandle);
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(); ~KRSurface();
void destroy(); void destroy();
uint32_t getWidth() const; uint32_t getWidth() const;
@@ -69,11 +63,7 @@ public:
void endFrame(); void endFrame();
KrSurfaceHandle m_handle; KrSurfaceHandle m_handle;
#ifdef WIN32 void* m_platformHandle;
HWND m_hWnd;
#elif defined(__APPLE__)
CAMetalLayer* m_layer;
#endif
KrDeviceHandle m_deviceHandle; KrDeviceHandle m_deviceHandle;
VkSurfaceKHR m_surface; VkSurfaceKHR m_surface;

View File

@@ -48,12 +48,11 @@ void KRSurfaceManager::destroySurfaces()
m_surfaces.clear(); m_surfaces.clear();
} }
#ifdef WIN32 KrResult KRSurfaceManager::create(void* platformHandle, KrSurfaceHandle& surfaceHandle)
KrResult KRSurfaceManager::create(HWND hWnd, KrSurfaceHandle& surfaceHandle)
{ {
surfaceHandle = 0; surfaceHandle = 0;
std::unique_ptr<KRSurface> surface = std::make_unique<KRSurface>(*m_pContext, m_topSurfaceHandle, hWnd); std::unique_ptr<KRSurface> surface = std::make_unique<KRSurface>(*m_pContext, m_topSurfaceHandle, platformHandle);
KrResult initialize_result = surface->initialize(); KrResult initialize_result = surface->initialize();
if (initialize_result != KR_SUCCESS) { if (initialize_result != KR_SUCCESS) {
@@ -66,8 +65,6 @@ KrResult KRSurfaceManager::create(HWND hWnd, KrSurfaceHandle& surfaceHandle)
return KR_SUCCESS; return KR_SUCCESS;
} }
#endif
KrResult KRSurfaceManager::destroy(KrSurfaceHandle& surfaceHandle) KrResult KRSurfaceManager::destroy(KrSurfaceHandle& surfaceHandle)
{ {
auto itr = m_surfaces.find(surfaceHandle); auto itr = m_surfaces.find(surfaceHandle);

View File

@@ -41,9 +41,7 @@ class KRSurfaceManager : KRContextObject
public: public:
KRSurfaceManager(KRContext& context); KRSurfaceManager(KRContext& context);
~KRSurfaceManager(); ~KRSurfaceManager();
#ifdef WIN32 KrResult create(void* platformHandle, KrSurfaceHandle& surfaceHandle);
KrResult create(HWND hWnd, KrSurfaceHandle& surfaceHandle);
#endif
KRSurface& get(KrSurfaceHandle surfaceHandle); KRSurface& get(KrSurfaceHandle surfaceHandle);
KrResult destroy(KrSurfaceHandle& surfaceHandle); KrResult destroy(KrSurfaceHandle& surfaceHandle);
unordered_map<KrSurfaceHandle, std::unique_ptr<KRSurface>>& getSurfaces(); unordered_map<KrSurfaceHandle, std::unique_ptr<KRSurface>>& getSurfaces();

View File

@@ -132,11 +132,7 @@ typedef struct
{ {
KrStructureType sType; KrStructureType sType;
KrSurfaceMapIndex surfaceHandle; KrSurfaceMapIndex surfaceHandle;
#if defined(_WIN32) || defined(_WIN64) void* platformHandle; // Can static cast to HWND on Windows and CAMetalLayer* on macOS
void* hWnd; // Can static cast to HWND
#elif defined(__APPLE__)
void* view; // Can static cast to NSView
#endif
} KrCreateWindowSurfaceInfo; } KrCreateWindowSurfaceInfo;
typedef struct typedef struct

View File

@@ -27,6 +27,8 @@ if (WIN32)
else(WIN32) else(WIN32)
add_executable(kraken_cube main_macos.mm harness.cpp hello_cube.cpp) add_executable(kraken_cube main_macos.mm harness.cpp hello_cube.cpp)
set(CMAKE_CXX_COMPILER "clang++") set(CMAKE_CXX_COMPILER "clang++")
list (APPEND EXTRA_LIBS "-framework QuartzCore")
list (APPEND EXTRA_LIBS "-framework Metal")
endif(WIN32) endif(WIN32)
add_dependencies(kraken_cube kraken_cube_assets) add_dependencies(kraken_cube kraken_cube_assets)

View File

@@ -34,7 +34,7 @@
#include "kraken.h" #include "kraken.h"
#include "hello_cube.h" #include "hello_cube.h"
bool test_init(void* windowHandle) bool test_init(void* platformHandle)
{ {
KrInitializeInfo init_info = {}; KrInitializeInfo init_info = {};
init_info.sType = KR_STRUCTURE_TYPE_INITIALIZE; init_info.sType = KR_STRUCTURE_TYPE_INITIALIZE;
@@ -50,11 +50,7 @@ bool test_init(void* windowHandle)
KrCreateWindowSurfaceInfo create_surface_info = {}; KrCreateWindowSurfaceInfo create_surface_info = {};
create_surface_info.sType = KR_STRUCTURE_TYPE_CREATE_WINDOW_SURFACE; create_surface_info.sType = KR_STRUCTURE_TYPE_CREATE_WINDOW_SURFACE;
create_surface_info.surfaceHandle = 1; create_surface_info.surfaceHandle = 1;
#if defined(WIN32) || defined(WIN64) create_surface_info.platformHandle = platformHandle;
create_surface_info.hWnd = windowHandle;
#elif defined(__APPLE__)
create_surface_info.view = windowHandle;
#endif
res = KrCreateWindowSurface(&create_surface_info); res = KrCreateWindowSurface(&create_surface_info);
if (res != KR_SUCCESS) { if (res != KR_SUCCESS) {
//printf("Failed to create window surface.\n"); //printf("Failed to create window surface.\n");

View File

@@ -31,5 +31,5 @@
#pragma once #pragma once
bool test_init(void* windowHandle); bool test_init(void* platformHandle);
bool test_shutdown(); bool test_shutdown();

View File

@@ -53,7 +53,7 @@ bool smoke_load()
if (res != KR_SUCCESS) { if (res != KR_SUCCESS) {
//printf("Failed to load resource: %s\n", arg); //printf("Failed to load resource: %s\n", arg);
KrShutdown(); KrShutdown();
return 1; return false;
} }
/* /*
@@ -84,4 +84,6 @@ bool smoke_load()
// create_camera_info.node.camera.skybox_texture = kSkyboxTextureResourceHandle; // create_camera_info.node.camera.skybox_texture = kSkyboxTextureResourceHandle;
res = KrCreateNode(&create_camera_info); res = KrCreateNode(&create_camera_info);
assert(res == KR_SUCCESS); assert(res == KR_SUCCESS);
return true;
} }

View File

@@ -30,6 +30,7 @@
// //
#import "Cocoa/Cocoa.h" #import "Cocoa/Cocoa.h"
#import "QuartzCore/CAMetalLayer.h"
#include "kraken.h" #include "kraken.h"
#include "harness.h" #include "harness.h"
@@ -59,10 +60,11 @@ int main(int argc, const char * argv[])
[windowController autorelease]; [windowController autorelease];
NSView *view = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 640, 480)]; 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]; view.layer.backgroundColor = [[NSColor purpleColor] CGColor];
if (!test_init(static_cast<void*>(view))) { if (!test_init(static_cast<void*>(view.layer))) {
return 1; return 1;
} }