Skip to content

Commit fed9573

Browse files
Vulkan: use VK_EXT_metal_surface extension on Apple platforms
1 parent 2ea77de commit fed9573

File tree

10 files changed

+99
-40
lines changed

10 files changed

+99
-40
lines changed

CMakeLists.txt

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -222,21 +222,28 @@ endif()
222222
if(PLATFORM_IOS OR PLATFORM_TVOS)
223223
if(VULKAN_SDK OR MOLTENVK_LIBRARY)
224224
if(NOT MOLTENVK_LIBRARY)
225-
set(MoltenVK_FRAMEWORK "${VULKAN_SDK}/MoltenVK/MoltenVK.xcframework")
226-
if(PLATFORM_IOS)
227-
if(CMAKE_OSX_SYSROOT STREQUAL "iphonesimulator" OR PLATFORM_IOS_SIMULATOR)
228-
set(MOLTENVK_LIBRARY "${MoltenVK_FRAMEWORK}/ios-arm64_x86_64-simulator/libMoltenVK.a")
229-
else()
230-
set(MOLTENVK_LIBRARY "${MoltenVK_FRAMEWORK}/ios-arm64/libMoltenVK.a")
231-
endif()
232-
elseif(PLATFORM_TVOS)
233-
if(CMAKE_OSX_SYSROOT STREQUAL "appletvsimulator" OR PLATFORM_TVOS_SIMULATOR)
234-
set(MOLTENVK_LIBRARY "${MoltenVK_FRAMEWORK}/tvos-arm64_x86_64-simulator/libMoltenVK.a")
225+
set(MoltenVK_FRAMEWORK "${VULKAN_SDK}/macOS/lib/MoltenVK.xcframework")
226+
if(NOT EXISTS "${MoltenVK_FRAMEWORK}")
227+
set(MoltenVK_FRAMEWORK "${VULKAN_SDK}/MoltenVK/MoltenVK.xcframework")
228+
endif()
229+
if(EXISTS "${MoltenVK_FRAMEWORK}")
230+
if(PLATFORM_IOS)
231+
if(CMAKE_OSX_SYSROOT STREQUAL "iphonesimulator" OR PLATFORM_IOS_SIMULATOR)
232+
set(MOLTENVK_LIBRARY "${MoltenVK_FRAMEWORK}/ios-arm64_x86_64-simulator/libMoltenVK.a")
233+
else()
234+
set(MOLTENVK_LIBRARY "${MoltenVK_FRAMEWORK}/ios-arm64/libMoltenVK.a")
235+
endif()
236+
elseif(PLATFORM_TVOS)
237+
if(CMAKE_OSX_SYSROOT STREQUAL "appletvsimulator" OR PLATFORM_TVOS_SIMULATOR)
238+
set(MOLTENVK_LIBRARY "${MoltenVK_FRAMEWORK}/tvos-arm64_x86_64-simulator/libMoltenVK.a")
239+
else()
240+
set(MOLTENVK_LIBRARY "${MoltenVK_FRAMEWORK}/tvos-arm64_arm64e/libMoltenVK.a")
241+
endif()
235242
else()
236-
set(MOLTENVK_LIBRARY "${MoltenVK_FRAMEWORK}/tvos-arm64_arm64e/libMoltenVK.a")
243+
message(FATAL_ERROR "Unexpected platform")
237244
endif()
238245
else()
239-
message(FATAL_ERROR "Unexpected platform")
246+
message(WARNING "Unable to find MoltenVK.xcframework at ${VULKAN_SDK}/macOS/lib or ${VULKAN_SDK}/MoltenVK")
240247
endif()
241248
endif()
242249

Graphics/GraphicsEngineVulkan/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,9 @@ if(PLATFORM_WIN32)
298298
elseif(PLATFORM_LINUX)
299299
set(PRIVATE_COMPILE_DEFINITIONS VK_USE_PLATFORM_XCB_KHR=1 VK_USE_PLATFORM_XLIB_KHR=1 DILIGENT_USE_VOLK=1)
300300
elseif(PLATFORM_MACOS)
301-
set(PRIVATE_COMPILE_DEFINITIONS VK_USE_PLATFORM_MACOS_MVK=1 DILIGENT_USE_VOLK=1)
301+
set(PRIVATE_COMPILE_DEFINITIONS VK_USE_PLATFORM_METAL_EXT=1 DILIGENT_USE_VOLK=1)
302302
elseif(PLATFORM_IOS OR PLATFORM_TVOS)
303-
set(PRIVATE_COMPILE_DEFINITIONS VK_USE_PLATFORM_IOS_MVK=1)
303+
set(PRIVATE_COMPILE_DEFINITIONS VK_USE_PLATFORM_METAL_EXT=1)
304304
elseif(PLATFORM_ANDROID)
305305
set(PRIVATE_COMPILE_DEFINITIONS VK_USE_PLATFORM_ANDROID_KHR=1 DILIGENT_USE_VOLK=1)
306306
else()

Graphics/GraphicsEngineVulkan/src/SwapChainVkImpl.cpp

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2023 Diligent Graphics LLC
2+
* Copyright 2019-2024 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -89,23 +89,14 @@ void SwapChainVkImpl::CreateSurface()
8989

9090
err = vkCreateAndroidSurfaceKHR(m_VulkanInstance->GetVkInstance(), &surfaceCreateInfo, NULL, &m_VkSurface);
9191
}
92-
#elif defined(VK_USE_PLATFORM_IOS_MVK)
93-
if (m_Window.pCALayer != nullptr)
92+
#elif defined(VK_USE_PLATFORM_METAL_EXT)
93+
if (void* pLayer = m_Window.GetLayer())
9494
{
95-
VkIOSSurfaceCreateInfoMVK surfaceCreateInfo{};
96-
surfaceCreateInfo.sType = VK_STRUCTURE_TYPE_IOS_SURFACE_CREATE_INFO_MVK;
97-
surfaceCreateInfo.pView = m_Window.pCALayer;
95+
VkMetalSurfaceCreateInfoEXT surfaceCreateInfo{};
96+
surfaceCreateInfo.sType = VK_STRUCTURE_TYPE_METAL_SURFACE_CREATE_INFO_EXT;
97+
surfaceCreateInfo.pLayer = pLayer;
9898

99-
err = vkCreateIOSSurfaceMVK(m_VulkanInstance->GetVkInstance(), &surfaceCreateInfo, nullptr, &m_VkSurface);
100-
}
101-
#elif defined(VK_USE_PLATFORM_MACOS_MVK)
102-
if (m_Window.pNSView != nullptr)
103-
{
104-
VkMacOSSurfaceCreateInfoMVK surfaceCreateInfo{};
105-
surfaceCreateInfo.sType = VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK;
106-
surfaceCreateInfo.pView = m_Window.pNSView;
107-
108-
err = vkCreateMacOSSurfaceMVK(m_VulkanInstance->GetVkInstance(), &surfaceCreateInfo, NULL, &m_VkSurface);
99+
err = vkCreateMetalSurfaceEXT(m_VulkanInstance->GetVkInstance(), &surfaceCreateInfo, NULL, &m_VkSurface);
109100
}
110101
#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
111102
if (m_Window.pDisplay != nullptr)
@@ -765,6 +756,17 @@ void SwapChainVkImpl::Present(Uint32 SyncInterval)
765756
m_SemaphoreIndex = m_SwapChainDesc.BufferCount - 1; // To start with 0 index when acquire next image
766757

767758
res = AcquireNextImage(pImmediateCtxVk);
759+
760+
#if PLATFORM_APPLE
761+
// For some reason, on MoltenVk we may get VK_SUBOPTIMAL_KHR first time we
762+
// acquire the image after the swap chain has been recreated.
763+
// Recreating it yet again seems to fix the problem.
764+
if (res == VK_SUBOPTIMAL_KHR)
765+
{
766+
RecreateVulkanSwapchain(pImmediateCtxVk);
767+
res = AcquireNextImage(pImmediateCtxVk);
768+
}
769+
#endif
768770
}
769771
DEV_CHECK_ERR(res == VK_SUCCESS, "Failed to acquire next swap chain image");
770772
}

Graphics/GraphicsEngineVulkan/src/VulkanUtilities/VulkanInstance.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 Diligent Graphics LLC
2+
* Copyright 2019-2024 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -246,11 +246,8 @@ VulkanInstance::VulkanInstance(const CreateInfo& CI) :
246246
#if defined(VK_USE_PLATFORM_XCB_KHR)
247247
InstanceExtensions.push_back(VK_KHR_XCB_SURFACE_EXTENSION_NAME);
248248
#endif
249-
#if defined(VK_USE_PLATFORM_IOS_MVK)
250-
InstanceExtensions.push_back(VK_MVK_IOS_SURFACE_EXTENSION_NAME);
251-
#endif
252-
#if defined(VK_USE_PLATFORM_MACOS_MVK)
253-
InstanceExtensions.push_back(VK_MVK_MACOS_SURFACE_EXTENSION_NAME);
249+
#if defined(VK_USE_PLATFORM_METAL_EXT)
250+
InstanceExtensions.push_back(VK_EXT_METAL_SURFACE_EXTENSION_NAME);
254251
#endif
255252
};
256253

Graphics/ShaderTools/src/GLSLangUtils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#include <memory>
3333
#include <array>
3434

35-
#if (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
35+
#ifdef VK_USE_PLATFORM_METAL_EXT
3636
# include <MoltenGLSLToSPIRVConverter/GLSLToSPIRVConverter.h>
3737
#else
3838
# ifndef ENABLE_HLSL

Platforms/Apple/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ set(SOURCE
2626
../Linux/src/LinuxFileSystem.cpp
2727
)
2828

29+
if(PLATFORM_MACOS)
30+
list(APPEND SOURCE src/MacOSNativeWindow.mm)
31+
endif()
32+
2933
add_library(Diligent-ApplePlatform ${SOURCE} ${INTERFACE} ${PLATFORM_INTERFACE_HEADERS})
3034
set_common_target_properties(Diligent-ApplePlatform)
3135

Platforms/Apple/interface/IOSNativeWindow.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 Diligent Graphics LLC
2+
* Copyright 2019-2024 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -42,6 +42,11 @@ struct IOSNativeWindow
4242
explicit IOSNativeWindow(void* _pCALayer) noexcept :
4343
pCALayer{_pCALayer}
4444
{}
45+
46+
void* GetLayer() const
47+
{
48+
return pCALayer;
49+
}
4550
#endif
4651
};
4752

Platforms/Apple/interface/MacOSNativeWindow.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 Diligent Graphics LLC
2+
* Copyright 2019-2024 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -42,6 +42,8 @@ struct MacOSNativeWindow
4242
explicit MacOSNativeWindow(void* _pNSView) noexcept :
4343
pNSView(_pNSView)
4444
{}
45+
46+
void* GetLayer() const;
4547
#endif
4648
};
4749

Platforms/Apple/interface/TVOSNativeWindow.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 Diligent Graphics LLC
2+
* Copyright 2019-2024 Diligent Graphics LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -41,6 +41,11 @@ struct TVOSNativeWindow
4141
explicit TVOSNativeWindow(void* _pCALayer) noexcept :
4242
pCALayer{_pCALayer}
4343
{}
44+
45+
void* GetLayer() const
46+
{
47+
return pCALayer;
48+
}
4449
#endif
4550
};
4651

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2024 Diligent Graphics LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS.
13+
*
14+
* In no event and under no legal theory, whether in tort (including negligence),
15+
* contract, or otherwise, unless required by applicable law (such as deliberate
16+
* and grossly negligent acts) or agreed to in writing, shall any Contributor be
17+
* liable for any damages, including any direct, indirect, special, incidental,
18+
* or consequential damages of any character arising as a result of this License or
19+
* out of the use or inability to use the software (including but not limited to damages
20+
* for loss of goodwill, work stoppage, computer failure or malfunction, or any and
21+
* all other commercial damages or losses), even if such Contributor has been advised
22+
* of the possibility of such damages.
23+
*/
24+
25+
#include "MacOSNativeWindow.h"
26+
27+
#import <AppKit/AppKit.h>
28+
29+
namespace Diligent
30+
{
31+
32+
void* MacOSNativeWindow::GetLayer() const
33+
{
34+
return pNSView ? (__bridge void*)((__bridge NSView*)pNSView).layer : nullptr;
35+
}
36+
37+
} // namespace Diligent

0 commit comments

Comments
 (0)