VulkanDevice: Fix for MacOS

This commit is contained in:
Stenzek 2023-08-24 18:02:55 +10:00
parent 64998b49dc
commit d750e4d417
4 changed files with 64 additions and 15 deletions

View file

@ -10,16 +10,21 @@ struct WindowInfo;
#import <Cocoa/Cocoa.h>
namespace CocoaTools {
NSString* StringViewToNSString(const std::string_view& str);
NSString* StringViewToNSString(const std::string_view& str);
}
#endif
namespace CocoaTools {
/// Add a handler to be run when macOS changes between dark and light themes
void AddThemeChangeHandler(void* ctx, void(handler)(void* ctx));
/// Add a handler to be run when macOS changes between dark and light themes
void AddThemeChangeHandler(void* ctx, void(handler)(void* ctx));
/// Remove a handler previously added using AddThemeChangeHandler with the given context
void RemoveThemeChangeHandler(void* ctx);
}
/// Remove a handler previously added using AddThemeChangeHandler with the given context
void RemoveThemeChangeHandler(void* ctx);
/// Creates metal layer on specified window surface.
bool CreateMetalLayer(WindowInfo* wi);
/// Destroys metal layer on specified window surface.
void DestroyMetalLayer(WindowInfo* wi);
} // namespace CocoaTools

View file

@ -147,3 +147,50 @@ void CocoaTools::RemoveThemeChangeHandler(void* ctx)
assert([NSThread isMainThread]);
[s_themeChangeHandler removeCallback:ctx];
}
bool CocoaTools::CreateMetalLayer(WindowInfo *wi)
{
// Punt off to main thread if we're not calling from it already.
if (![NSThread isMainThread])
{
bool ret;
dispatch_sync(dispatch_get_main_queue(), [&ret, wi]() {
ret = CreateMetalLayer(wi);
});
return ret;
}
CAMetalLayer* layer = [CAMetalLayer layer];
if (layer == nil)
{
Log_ErrorPrint("Failed to create CAMetalLayer");
return false;
}
NSView* view = (__bridge NSView*)wi->window_handle;
[view setWantsLayer:TRUE];
[view setLayer:layer];
[layer setContentsScale:[[[view window] screen] backingScaleFactor]];
wi->surface_handle = (__bridge void*)layer;
return true;
}
void CocoaTools::DestroyMetalLayer(WindowInfo *wi)
{
if (!wi->surface_handle)
return;
// Punt off to main thread if we're not calling from it already.
if (![NSThread isMainThread])
{
dispatch_sync(dispatch_get_main_queue(), [wi]() { DestroyMetalLayer(wi); });
return;
}
NSView* view = (__bridge NSView*)wi->window_handle;
CAMetalLayer* layer = (__bridge CAMetalLayer*)wi->surface_handle;
[view setLayer:nil];
[view setWantsLayer:NO];
[layer release];
}

View file

@ -48,6 +48,7 @@ enum : u32
MAX_DRAW_CALLS_PER_FRAME = 2048,
MAX_COMBINED_IMAGE_SAMPLER_DESCRIPTORS_PER_FRAME = GPUDevice::MAX_TEXTURE_SAMPLERS * MAX_DRAW_CALLS_PER_FRAME,
MAX_DESCRIPTOR_SETS_PER_FRAME = MAX_DRAW_CALLS_PER_FRAME,
MAX_SAMPLER_DESCRIPTORS = 8192,
VERTEX_BUFFER_SIZE = 32 * 1024 * 1024,
INDEX_BUFFER_SIZE = 16 * 1024 * 1024,
@ -741,6 +742,7 @@ bool VulkanDevice::CreatePersistentDescriptorPool()
{
static constexpr const VkDescriptorPoolSize pool_sizes[] = {
{VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 1},
{VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, MAX_SAMPLER_DESCRIPTORS},
{VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER, 16},
{VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 16},
};

View file

@ -16,6 +16,10 @@
#include <X11/Xlib.h>
#endif
#if defined(VK_USE_PLATFORM_METAL_EXT)
#include "util/cocoa_tools.h"
#endif
Log_SetChannel(VulkanDevice);
VulkanSwapChain::VulkanSwapChain(const WindowInfo& wi, VkSurfaceKHR surface, bool vsync,
@ -60,7 +64,6 @@ VkSurfaceKHR VulkanSwapChain::CreateVulkanSurface(VkInstance instance, VkPhysica
#if defined(VK_USE_PLATFORM_METAL_EXT)
if (wi->type == WindowInfo::Type::MacOS)
{
#if 0
// TODO: FIXME
if (!wi->surface_handle && !CocoaTools::CreateMetalLayer(wi))
return VK_NULL_HANDLE;
@ -77,10 +80,6 @@ VkSurfaceKHR VulkanSwapChain::CreateVulkanSurface(VkInstance instance, VkPhysica
}
return surface;
#else
Panic("Fixme");
return VK_NULL_HANDLE;
#endif
}
#endif
@ -156,12 +155,8 @@ void VulkanSwapChain::DestroyVulkanSurface(VkInstance instance, WindowInfo* wi,
vkDestroySurfaceKHR(VulkanDevice::GetInstance().GetVulkanInstance(), surface, nullptr);
#if defined(__APPLE__)
#if 0
if (wi->type == WindowInfo::Type::MacOS && wi->surface_handle)
CocoaTools::DestroyMetalLayer(wi);
#else
Panic("TODO");
#endif
#endif
}