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)
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)

View File

@@ -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<std::mutex> surfaceLock(KRContext::g_SurfaceInfoMutex);
const std::lock_guard<std::mutex> deviceLock(KRContext::g_DeviceInfoMutex);
#ifdef WIN32
HWND hWnd = static_cast<HWND>(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;
}

View File

@@ -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<HWND>(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<CAMetalLayer*>(m_platformHandle);
if (vkCreateMetalSurfaceEXT(m_pContext->getDeviceManager()->getVulkanInstance(), &createInfo, nullptr, &m_surface) != VK_SUCCESS) {
return KR_ERROR_VULKAN;
}
#else
#error Unsupported
#endif

View File

@@ -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;

View File

@@ -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<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();
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);

View File

@@ -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<KrSurfaceHandle, std::unique_ptr<KRSurface>>& getSurfaces();

View File

@@ -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

View File

@@ -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)

View File

@@ -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");

View File

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

View File

@@ -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;
}

View File

@@ -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<void*>(view))) {
if (!test_init(static_cast<void*>(view.layer))) {
return 1;
}