Skip to content

Commit 84a7c4e

Browse files
authored
(macOS) Support loading VK ICDs
1 parent a3f7c0d commit 84a7c4e

File tree

9 files changed

+57
-11
lines changed

9 files changed

+57
-11
lines changed

.ci/build-mac-arm64.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK=1
66
export HOMEBREW_NO_ENV_HINTS=1
77
export HOMEBREW_NO_INSTALL_CLEANUP=1
88

9-
brew install -f --overwrite --quiet pipenv googletest ffmpeg@5 "llvm@$LLVM_COMPILER_VER" glew sdl3 vulkan-headers
9+
brew install -f --overwrite --quiet pipenv googletest ffmpeg@5 "llvm@$LLVM_COMPILER_VER" glew sdl3 vulkan-headers vulkan-loader
1010
brew link -f --quiet "llvm@$LLVM_COMPILER_VER" ffmpeg@5
1111

1212
# moltenvk based on commit for 1.4.0 release
@@ -66,8 +66,7 @@ export LD_LIBRARY_PATH="$BREW_PATH/lib"
6666

6767
export VULKAN_SDK
6868
VULKAN_SDK="$BREW_PATH/opt/molten-vk"
69-
ln -s "$VULKAN_SDK/lib/libMoltenVK.dylib" "$VULKAN_SDK/lib/libvulkan.dylib" || true
70-
export VK_ICD_FILENAMES="$VULKAN_SDK/share/vulkan/icd.d/MoltenVK_icd.json"
69+
ln -s "$BREW_PATH/opt/vulkan-loader/lib/libvulkan.dylib" "$VULKAN_SDK/lib/libvulkan.dylib" || true
7170

7271
export LLVM_DIR
7372
LLVM_DIR="$BREW_PATH/opt/llvm@$LLVM_COMPILER_VER"

.ci/deploy-mac-arm64.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ echo "AVVER=$AVVER" >> ../.ci/ci-vars.env
1515

1616
cd bin
1717
mkdir "rpcs3.app/Contents/lib/" || true
18+
mkdir -p "rpcs3.app/Contents/Resources/vulkan/icd.d" || true
19+
cp -LR "$BREW_PATH/lib/libMoltenVK.dylib" "rpcs3.app/Contents/Resources/vulkan/icd.d"
20+
cp -LR "$BREW_PATH/etc/vulkan/icd.d/MoltenVK_icd.json" "rpcs3.app/Contents/Resources/vulkan/icd.d"
21+
sed -i '' "s/..\/..\/..\/lib\/libMoltenVK.dylib/.\/libMoltenVK.dylib/g" "rpcs3.app/Contents/Resources/vulkan/icd.d/MoltenVK_icd.json"
1822

1923
cp "$(realpath /opt/homebrew/opt/llvm@$LLVM_COMPILER_VER/lib/c++/libc++abi.1.0.dylib)" "rpcs3.app/Contents/Frameworks/libc++abi.1.dylib"
2024
cp "$(realpath /opt/homebrew/lib/libsharpyuv.0.dylib)" "rpcs3.app/Contents/lib/libsharpyuv.0.dylib"

rpcs3/Emu/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,11 @@ if(TARGET 3rdparty_vulkan)
622622
RSX/VK/VKTextureCache.cpp
623623
RSX/VK/VulkanAPI.cpp
624624
)
625+
if(APPLE)
626+
target_sources(rpcs3_emu PRIVATE
627+
RSX/VK/vkutils/metal_layer.mm
628+
)
629+
endif()
625630
endif()
626631

627632
find_package(Threads REQUIRED)

rpcs3/Emu/RSX/VK/VulkanAPI.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#ifdef _WIN32
55
#define VK_USE_PLATFORM_WIN32_KHR
66
#elif defined(__APPLE__)
7-
#define VK_USE_PLATFORM_MACOS_MVK
7+
#define VK_USE_PLATFORM_METAL_EXT
88
#elif defined(ANDROID)
99
#define VK_USE_PLATFORM_ANDROID_KHR
1010
#else
@@ -29,7 +29,7 @@
2929

3030
// Undefine header configuration variables
3131
#undef VK_USE_PLATFORM_WIN32_KHR
32-
#undef VK_USE_PLATFORM_MACOS_MVK
32+
#undef VK_USE_PLATFORM_METAL_EXT
3333
#undef VK_USE_PLATFORM_ANDROID_KHR
3434
#undef VK_USE_PLATFORM_XLIB_KHR
3535
#undef VK_USE_PLATFORM_WAYLAND_KHR

rpcs3/Emu/RSX/VK/vkutils/device.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
#include "util/logs.hpp"
44
#include "Emu/system_config.h"
55
#include <vulkan/vulkan_core.h>
6+
#ifdef __APPLE__
7+
#include <vulkan/vulkan_beta.h>
8+
#endif
69

710
namespace vk
811
{
@@ -555,6 +558,10 @@ namespace vk
555558
{
556559
requested_extensions.push_back(VK_EXT_DEVICE_FAULT_EXTENSION_NAME);
557560
}
561+
562+
#ifdef __APPLE__
563+
requested_extensions.push_back(VK_KHR_PORTABILITY_SUBSET_EXTENSION_NAME);
564+
#endif
558565

559566
enabled_features.robustBufferAccess = VK_TRUE;
560567
enabled_features.fullDrawIndexUint32 = VK_TRUE;

rpcs3/Emu/RSX/VK/vkutils/instance.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,11 @@ namespace vk
124124
}
125125

126126
#ifdef __APPLE__
127+
extensions.push_back(VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME);
128+
extensions.push_back(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME);
127129
if (support.is_supported(VK_EXT_LAYER_SETTINGS_EXTENSION_NAME))
128130
{
129131
extensions.push_back(VK_EXT_LAYER_SETTINGS_EXTENSION_NAME);
130-
layers.push_back(kMVKMoltenVKDriverLayerName);
131132

132133
mvk_settings.push_back(VkLayerSettingEXT{ kMVKMoltenVKDriverLayerName, "MVK_CONFIG_RESUME_LOST_DEVICE", VK_LAYER_SETTING_TYPE_BOOL32_EXT, 1, &setting_true });
133134
mvk_settings.push_back(VkLayerSettingEXT{ kMVKMoltenVKDriverLayerName, "MVK_CONFIG_FAST_MATH_ENABLED", VK_LAYER_SETTING_TYPE_INT32_EXT, 1, &setting_fast_math });
@@ -154,7 +155,7 @@ namespace vk
154155
#ifdef _WIN32
155156
extensions.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
156157
#elif defined(__APPLE__)
157-
extensions.push_back(VK_MVK_MACOS_SURFACE_EXTENSION_NAME);
158+
extensions.push_back(VK_EXT_METAL_SURFACE_EXTENSION_NAME);
158159
#else
159160
bool found_surface_ext = false;
160161
#ifdef HAVE_X11
@@ -187,15 +188,32 @@ namespace vk
187188
if (g_cfg.video.debug_output)
188189
layers.push_back("VK_LAYER_KHRONOS_validation");
189190
}
191+
#ifdef __APPLE__
192+
// MoltenVK's ICD will not be detected without these extensions enabled.
193+
else
194+
{
195+
extensions_loaded = true;
196+
extensions.push_back(VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME);
197+
extensions.push_back(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME);
198+
}
199+
#endif
190200

191201
VkInstanceCreateInfo instance_info = {};
192202
instance_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
193203
instance_info.pApplicationInfo = &app;
194204
instance_info.enabledLayerCount = static_cast<u32>(layers.size());
195205
instance_info.ppEnabledLayerNames = layers.data();
206+
#ifdef __APPLE__
207+
instance_info.enabledExtensionCount = static_cast<u32>(extensions.size());
208+
instance_info.ppEnabledExtensionNames = extensions.data();
209+
#else
196210
instance_info.enabledExtensionCount = fast ? 0 : static_cast<u32>(extensions.size());
197211
instance_info.ppEnabledExtensionNames = fast ? nullptr : extensions.data();
212+
#endif
198213
instance_info.pNext = next_info;
214+
#ifdef __APPLE__
215+
instance_info.flags |= VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR;
216+
#endif
199217

200218
if (VkResult result = vkCreateInstance(&instance_info, nullptr, &m_instance); result != VK_SUCCESS)
201219
{
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#pragma once
2+
void* GetCAMetalLayerFromMetalView(void* view);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma GCC diagnostic push
2+
#pragma GCC diagnostic ignored "-Wold-style-cast"
3+
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
4+
#pragma GCC diagnostic ignored "-Wmissing-declarations"
5+
#import <Foundation/Foundation.h>
6+
#import <AppKit/AppKit.h>
7+
#import <QuartzCore/QuartzCore.h>
8+
9+
void* GetCAMetalLayerFromMetalView(void* view) { return ((NSView*)view).layer; }
10+
#pragma GCC diagnostic pop

rpcs3/Emu/RSX/VK/vkutils/swapchain_macos.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include "swapchain_core.h"
4+
#include "metal_layer.h"
45

56
namespace vk
67
{
@@ -12,11 +13,11 @@ namespace vk
1213
VkSurfaceKHR make_WSI_surface(VkInstance vk_instance, display_handle_t window_handle, WSI_config* /*config*/)
1314
{
1415
VkSurfaceKHR result = VK_NULL_HANDLE;
15-
VkMacOSSurfaceCreateInfoMVK createInfo = {};
16-
createInfo.sType = VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK;
17-
createInfo.pView = window_handle;
16+
VkMetalSurfaceCreateInfoEXT createInfo = {};
17+
createInfo.sType = VK_STRUCTURE_TYPE_METAL_SURFACE_CREATE_INFO_EXT;
18+
createInfo.pLayer = GetCAMetalLayerFromMetalView(window_handle);
1819

19-
CHECK_RESULT(vkCreateMacOSSurfaceMVK(vk_instance, &createInfo, NULL, &result));
20+
CHECK_RESULT(vkCreateMetalSurfaceEXT(vk_instance, &createInfo, NULL, &result));
2021
return result;
2122
}
2223
#endif

0 commit comments

Comments
 (0)