Skip to content

Commit 490c47e

Browse files
committed
renderer_vulkan: Support loading Vulkan layers on macOS SDL build.
1 parent ee7fe30 commit 490c47e

File tree

6 files changed

+59
-46
lines changed

6 files changed

+59
-46
lines changed

.github/workflows/build.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,8 @@ jobs:
205205
run: |
206206
mkdir upload
207207
mv ${{github.workspace}}/build/shadps4 upload
208-
cp ${{github.workspace}}/build/externals/MoltenVK/libMoltenVK.dylib upload
209-
install_name_tool -add_rpath "@executable_path" upload/shadps4
208+
mv ${{github.workspace}}/build/MoltenVK_icd.json upload
209+
mv ${{github.workspace}}/build/libMoltenVK.dylib upload
210210
- uses: actions/upload-artifact@v4
211211
with:
212212
name: shadps4-macos-sdl-${{ needs.get-info.outputs.date }}-${{ needs.get-info.outputs.shorthash }}

CMakeLists.txt

+34-26
Original file line numberDiff line numberDiff line change
@@ -1083,34 +1083,42 @@ if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux" AND ENABLE_USERFAULTFD)
10831083
endif()
10841084

10851085
if (APPLE)
1086-
if (ENABLE_QT_GUI)
1087-
# Include MoltenVK in the app bundle, along with an ICD file so it can be found by the system Vulkan loader if used for loading layers.
1088-
set(MVK_ICD ${CMAKE_CURRENT_SOURCE_DIR}/externals/MoltenVK/MoltenVK_icd.json)
1089-
target_sources(shadps4 PRIVATE ${MVK_ICD})
1090-
set_source_files_properties(${MVK_ICD} PROPERTIES MACOSX_PACKAGE_LOCATION Resources/vulkan/icd.d)
1091-
1092-
set(MVK_DYLIB_SRC ${CMAKE_CURRENT_BINARY_DIR}/externals/MoltenVK/libMoltenVK.dylib)
1093-
set(MVK_DYLIB_DST ${CMAKE_CURRENT_BINARY_DIR}/shadps4.app/Contents/Frameworks/libMoltenVK.dylib)
1094-
add_custom_command(
1095-
OUTPUT ${MVK_DYLIB_DST}
1096-
DEPENDS ${MVK_DYLIB_SRC}
1097-
COMMAND cmake -E copy ${MVK_DYLIB_SRC} ${MVK_DYLIB_DST})
1098-
add_custom_target(CopyMoltenVK DEPENDS ${MVK_DYLIB_DST})
1099-
add_dependencies(CopyMoltenVK MoltenVK)
1100-
add_dependencies(shadps4 CopyMoltenVK)
1101-
set_property(TARGET shadps4 APPEND PROPERTY BUILD_RPATH "@executable_path/../Frameworks")
1102-
else()
1103-
# For non-bundled SDL build, just do a normal library link.
1104-
target_link_libraries(shadps4 PRIVATE MoltenVK)
1105-
endif()
1086+
# Include MoltenVK, along with an ICD file so it can be found by the system Vulkan loader if used for loading layers.
1087+
set(MVK_DYLIB_SRC ${CMAKE_CURRENT_BINARY_DIR}/externals/MoltenVK/libMoltenVK.dylib)
11061088

1107-
if (ARCHITECTURE STREQUAL "x86_64")
1108-
# Reserve system-managed memory space.
1109-
target_link_options(shadps4 PRIVATE -Wl,-no_pie,-no_fixup_chains,-no_huge,-pagezero_size,0x4000,-segaddr,TCB_SPACE,0x4000,-segaddr,SYSTEM_MANAGED,0x400000,-segaddr,SYSTEM_RESERVED,0x7FFFFC000,-image_base,0x20000000000)
1110-
endif()
1089+
if (ENABLE_QT_GUI)
1090+
set_property(TARGET shadps4 APPEND PROPERTY BUILD_RPATH "@executable_path/../Frameworks")
1091+
set(MVK_ICD_DST ${CMAKE_CURRENT_BINARY_DIR}/shadps4.app/Contents/Resources/vulkan/icd.d/MoltenVK_icd.json)
1092+
set(MVK_DYLIB_DST ${CMAKE_CURRENT_BINARY_DIR}/shadps4.app/Contents/Frameworks/libMoltenVK.dylib)
1093+
set(MVK_DYLIB_ICD_PATH "../../../Frameworks/libMoltenVK.dylib")
1094+
else()
1095+
set_property(TARGET shadps4 APPEND PROPERTY BUILD_RPATH "@executable_path")
1096+
set(MVK_ICD_DST ${CMAKE_CURRENT_BINARY_DIR}/MoltenVK_icd.json)
1097+
set(MVK_DYLIB_DST ${CMAKE_CURRENT_BINARY_DIR}/libMoltenVK.dylib)
1098+
set(MVK_DYLIB_ICD_PATH "./libMoltenVK.dylib")
1099+
endif()
1100+
1101+
set(MVK_ICD "\\\{ \\\"file_format_version\\\": \\\"1.0.0\\\", \\\"ICD\\\": \\\{ \\\"library_path\\\": \\\"${MVK_DYLIB_ICD_PATH}\\\", \\\"api_version\\\": \\\"1.2.0\\\", \\\"is_portability_driver\\\": true \\\} \\\}")
1102+
add_custom_command(
1103+
OUTPUT ${MVK_ICD_DST}
1104+
COMMAND ${CMAKE_COMMAND} -E echo ${MVK_ICD} > ${MVK_ICD_DST})
1105+
1106+
add_custom_command(
1107+
OUTPUT ${MVK_DYLIB_DST}
1108+
DEPENDS ${MVK_DYLIB_SRC}
1109+
COMMAND ${CMAKE_COMMAND} -E copy ${MVK_DYLIB_SRC} ${MVK_DYLIB_DST})
1110+
1111+
add_custom_target(CopyMoltenVK DEPENDS ${MVK_ICD_DST} ${MVK_DYLIB_DST})
1112+
add_dependencies(CopyMoltenVK MoltenVK)
1113+
add_dependencies(shadps4 CopyMoltenVK)
1114+
1115+
if (ARCHITECTURE STREQUAL "x86_64")
1116+
# Reserve system-managed memory space.
1117+
target_link_options(shadps4 PRIVATE -Wl,-no_pie,-no_fixup_chains,-no_huge,-pagezero_size,0x4000,-segaddr,TCB_SPACE,0x4000,-segaddr,SYSTEM_MANAGED,0x400000,-segaddr,SYSTEM_RESERVED,0x7FFFFC000,-image_base,0x20000000000)
1118+
endif()
11111119

1112-
# Replacement for std::chrono::time_zone
1113-
target_link_libraries(shadps4 PRIVATE date::date-tz)
1120+
# Replacement for std::chrono::time_zone
1121+
target_link_libraries(shadps4 PRIVATE date::date-tz)
11141122
endif()
11151123

11161124
if (NOT ENABLE_QT_GUI)

externals/MoltenVK/MoltenVK_icd.json

-8
This file was deleted.

externals/MoltenVK/SPIRV-Cross

Submodule SPIRV-Cross updated 71 files

src/video_core/renderer_vulkan/vk_platform.cpp

+21-8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
#include "sdl_window.h"
2323
#include "video_core/renderer_vulkan/vk_platform.h"
2424

25+
#ifdef __APPLE__
26+
#include <mach-o/dyld.h>
27+
#endif
28+
2529
namespace Vulkan {
2630

2731
static const char* const VALIDATION_LAYER_NAME = "VK_LAYER_KHRONOS_validation";
@@ -223,19 +227,28 @@ vk::UniqueInstance CreateInstance(Frontend::WindowSystemType window_type, bool e
223227
LOG_INFO(Render_Vulkan, "Creating vulkan instance");
224228

225229
#ifdef __APPLE__
226-
#ifdef ENABLE_QT_GUI
227230
// If the Vulkan loader exists in /usr/local/lib, give it priority. The Vulkan SDK
228-
// installs it here by default but it is not in the default library search path.
231+
// installs it here by default, but it is not in the default library search path.
229232
// The loader has a clause to check for it, but at a lower priority than the bundled
230233
// libMoltenVK.dylib, so we need to handle it ourselves to give it priority.
231234
static const std::string usr_local_path = "/usr/local/lib/libvulkan.dylib";
232-
static vk::detail::DynamicLoader dl = std::filesystem::exists(usr_local_path)
233-
? vk::detail::DynamicLoader(usr_local_path)
234-
: vk::detail::DynamicLoader();
235-
#else
236-
// TODO: Support layer loading in SDL build. For now just make sure we load the right MoltenVK.
237-
static vk::detail::DynamicLoader dl("libMoltenVK.dylib");
235+
static const auto loader_exists = std::filesystem::exists(usr_local_path);
236+
#ifndef ENABLE_QT_GUI
237+
if (loader_exists) {
238+
// Initialize the environment with the path to the MoltenVK ICD, so that the loader will
239+
// find it.
240+
static const auto icd_path = [] {
241+
char path[PATH_MAX];
242+
u32 length = PATH_MAX;
243+
_NSGetExecutablePath(path, &length);
244+
return std::filesystem::path(path).parent_path() / "MoltenVK_icd.json";
245+
}();
246+
setenv("VK_DRIVER_FILES", icd_path.c_str(), true);
247+
}
238248
#endif
249+
static vk::detail::DynamicLoader dl = loader_exists
250+
? vk::detail::DynamicLoader(usr_local_path)
251+
: vk::detail::DynamicLoader("libMoltenVK.dylib");
239252
#else
240253
static vk::detail::DynamicLoader dl;
241254
#endif

0 commit comments

Comments
 (0)