From 64e83a0ec297d705acd68f752d48528f9d5eaccc Mon Sep 17 00:00:00 2001 From: Jamil Halabi Date: Tue, 15 Aug 2023 10:21:25 +0300 Subject: [PATCH] Fixed creating MoltenVk surface with agnostic backends such as SDL2 (#3158) --- src/renderer_vk.cpp | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/renderer_vk.cpp b/src/renderer_vk.cpp index d5832203c..d04a793ce 100644 --- a/src/renderer_vk.cpp +++ b/src/renderer_vk.cpp @@ -6832,17 +6832,32 @@ VK_DESTROY if (NULL != vkCreateMacOSSurfaceMVK) { NSWindow* window = (NSWindow*)(m_nwh); - NSView* contentView = (NSView*)window.contentView; - CAMetalLayer* layer = [CAMetalLayer layer]; + CAMetalLayer* layer = (CAMetalLayer*)(m_nwh); + + if ([window isKindOfClass:[NSWindow class]]) + { + NSView *contentView = (NSView *)window.contentView; + layer = [CAMetalLayer layer]; + + [contentView setWantsLayer : YES]; + [contentView setLayer : layer]; + } + else if ([layer isKindOfClass:[CAMetalLayer class]]) + { + NSView *contentView = (NSView *)layer.delegate; + window = contentView.window; + } + else + { + BX_WARN(0, "Unable to create MoltenVk surface. Please set platform data window to an NSWindow or CAMetalLayer"); + return result; + } if (m_resolution.reset & BGFX_RESET_HIDPI) { layer.contentsScale = [window backingScaleFactor]; } - [contentView setWantsLayer : YES]; - [contentView setLayer : layer]; - VkMacOSSurfaceCreateInfoMVK sci; sci.sType = VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK; sci.pNext = NULL;