Skip to content

Commit 185ac9f

Browse files
Add support for Metal on macOS.
Co-authored-by: Isaac Marovitz <[email protected]>
1 parent 3c1badf commit 185ac9f

File tree

73 files changed

+1226
-154
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+1226
-154
lines changed

.gitmodules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
url = https://github.com/redorav/ddspp.git
77
[submodule "tools/XenosRecomp"]
88
path = tools/XenosRecomp
9-
url = https://github.com/hedge-dev/XenosRecomp.git
9+
url = https://github.com/squidbus/XenosRecomp.git
1010
[submodule "UnleashedRecompResources"]
1111
path = UnleashedRecompResources
1212
url = https://github.com/hedge-dev/UnleashedRecompResources.git

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ if (UNLEASHED_RECOMP_ARCHITECTURE STREQUAL "x86_64" OR UNLEASHED_RECOMP_ARCHITEC
4040
)
4141
endif()
4242

43+
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
44+
# Normally only defined by Visual Studio, added for consistency
45+
add_compile_definitions(_DEBUG)
46+
endif()
47+
4348
add_subdirectory(${UNLEASHED_RECOMP_THIRDPARTY_ROOT})
4449
add_subdirectory(${UNLEASHED_RECOMP_TOOLS_ROOT})
4550

UnleashedRecomp/CMakeLists.txt

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ if (WIN32)
44
option(UNLEASHED_RECOMP_D3D12 "Add D3D12 support for rendering" ON)
55
endif()
66

7+
if (APPLE)
8+
option(UNLEASHED_RECOMP_METAL "Add Metal support for rendering" ON)
9+
endif()
10+
711
if (CMAKE_SYSTEM_NAME MATCHES "Linux")
812
option(UNLEASHED_RECOMP_FLATPAK "Configure the build for Flatpak compatibility." OFF)
913
endif()
@@ -255,6 +259,10 @@ if (NOT ${CMAKE_BUILD_TYPE} MATCHES "Release")
255259
set(SHOW_GIT_INFO_AND_BUILD_TYPE 1)
256260
endif()
257261

262+
if (UNLEASHED_RECOMP_METAL)
263+
set(XCRUN_TOOL "/usr/bin/xcrun")
264+
endif()
265+
258266
GenerateVersionSources(
259267
OUTPUT_DIR ${PROJECT_SOURCE_DIR}
260268
VERSION_TXT ${VERSION_TXT}
@@ -375,6 +383,10 @@ if (UNLEASHED_RECOMP_D3D12)
375383
)
376384
endif()
377385

386+
if (UNLEASHED_RECOMP_METAL)
387+
target_compile_definitions(UnleashedRecomp PRIVATE UNLEASHED_RECOMP_METAL)
388+
endif()
389+
378390
if (WIN32)
379391
target_link_libraries(UnleashedRecomp PRIVATE
380392
comctl32
@@ -418,22 +430,37 @@ endif()
418430
target_precompile_headers(UnleashedRecomp PUBLIC ${UNLEASHED_RECOMP_PRECOMPILED_HEADERS})
419431

420432
function(compile_shader FILE_PATH TARGET_NAME)
421-
set(FILE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/gpu/shader/${FILE_PATH}.hlsl)
422-
cmake_path(GET FILE_PATH STEM VARIABLE_NAME)
433+
set(HLSL_FILE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/gpu/shader/hlsl/${FILE_PATH}.hlsl)
434+
set(MSL_FILE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/gpu/shader/msl/${FILE_PATH}.metal)
435+
cmake_path(GET HLSL_FILE_PATH STEM HLSL_NAME)
436+
cmake_path(GET MSL_FILE_PATH STEM MSL_NAME)
437+
if (UNLEASHED_RECOMP_METAL)
438+
add_custom_command(
439+
OUTPUT ${MSL_FILE_PATH}.ir
440+
COMMAND ${XCRUN_TOOL} -sdk macosx metal -o ${MSL_FILE_PATH}.ir -c ${MSL_FILE_PATH} -D__air__ -frecord-sources -gline-tables-only
441+
DEPENDS ${MSL_FILE_PATH}
442+
)
443+
add_custom_command(
444+
OUTPUT ${MSL_FILE_PATH}.metallib
445+
COMMAND ${XCRUN_TOOL} -sdk macosx metallib -o ${MSL_FILE_PATH}.metallib ${MSL_FILE_PATH}.ir
446+
DEPENDS ${MSL_FILE_PATH}.ir
447+
)
448+
BIN2C(TARGET_OBJ UnleashedRecomp SOURCE_FILE "${MSL_FILE_PATH}.metallib" DEST_FILE "${MSL_FILE_PATH}.metallib" ARRAY_NAME "g_${MSL_NAME}_air")
449+
endif()
423450
if (UNLEASHED_RECOMP_D3D12)
424451
add_custom_command(
425-
OUTPUT ${FILE_PATH}.dxil.h
426-
COMMAND ${DIRECTX_DXC_TOOL} -T ${TARGET_NAME} -HV 2021 -all-resources-bound -Wno-ignored-attributes -Fh ${FILE_PATH}.dxil.h ${FILE_PATH} -Vn g_${VARIABLE_NAME}_dxil
427-
DEPENDS ${FILE_PATH}
452+
OUTPUT ${HLSL_FILE_PATH}.dxil.h
453+
COMMAND ${DIRECTX_DXC_TOOL} -T ${TARGET_NAME} -HV 2021 -all-resources-bound -Wno-ignored-attributes -E shaderMain -Fh ${HLSL_FILE_PATH}.dxil.h ${HLSL_FILE_PATH} -Vn g_${HLSL_NAME}_dxil
454+
DEPENDS ${HLSL_FILE_PATH}
428455
)
429-
target_sources(UnleashedRecomp PRIVATE ${FILE_PATH}.dxil.h)
456+
target_sources(UnleashedRecomp PRIVATE ${HLSL_FILE_PATH}.dxil.h)
430457
endif()
431458
add_custom_command(
432-
OUTPUT ${FILE_PATH}.spirv.h
433-
COMMAND ${DIRECTX_DXC_TOOL} -T ${TARGET_NAME} -HV 2021 -all-resources-bound -spirv -fvk-use-dx-layout ${ARGN} -Fh ${FILE_PATH}.spirv.h ${FILE_PATH} -Vn g_${VARIABLE_NAME}_spirv
434-
DEPENDS ${FILE_PATH}
459+
OUTPUT ${HLSL_FILE_PATH}.spirv.h
460+
COMMAND ${DIRECTX_DXC_TOOL} -T ${TARGET_NAME} -HV 2021 -all-resources-bound -spirv -fvk-use-dx-layout ${ARGN} -E shaderMain -Fh ${HLSL_FILE_PATH}.spirv.h ${HLSL_FILE_PATH} -Vn g_${HLSL_NAME}_spirv
461+
DEPENDS ${HLSL_FILE_PATH}
435462
)
436-
target_sources(UnleashedRecomp PRIVATE ${FILE_PATH}.spirv.h)
463+
target_sources(UnleashedRecomp PRIVATE ${HLSL_FILE_PATH}.spirv.h)
437464
endfunction()
438465

439466
function(compile_vertex_shader FILE_PATH)

UnleashedRecomp/gpu/imgui/imgui_common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#define IMGUI_SHADER_MODIFIER_RECTANGLE_BEVEL 10
1414
#define IMGUI_SHADER_MODIFIER_LOW_QUALITY_TEXT 11
1515

16-
#ifdef __cplusplus
16+
#if defined(__cplusplus) && !defined(__air__)
1717

1818
enum class ImGuiCallback : int32_t
1919
{
File renamed without changes.

UnleashedRecomp/gpu/shader/blend_color_alpha_ps.hlsl renamed to UnleashedRecomp/gpu/shader/hlsl/blend_color_alpha_ps.hlsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "../../../tools/XenosRecomp/XenosRecomp/shader_common.h"
1+
#include "../../../../tools/XenosRecomp/XenosRecomp/shader_common.h"
22

33
#ifdef __spirv__
44

@@ -22,7 +22,7 @@ cbuffer SharedConstants : register(b2, space4)
2222

2323
#endif
2424

25-
float4 main(
25+
float4 shaderMain(
2626
in float4 iPos : SV_Position,
2727
in float4 iTexCoord0 : TEXCOORD0) : SV_Target0
2828
{

UnleashedRecomp/gpu/shader/copy_color_ps.hlsl renamed to UnleashedRecomp/gpu/shader/hlsl/copy_color_ps.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Texture2D<float4> g_Texture2DDescriptorHeap[] : register(t0, space0);
44

5-
float4 main(in float4 position : SV_Position) : SV_Target
5+
float4 shaderMain(in float4 position : SV_Position) : SV_Target
66
{
77
return g_Texture2DDescriptorHeap[g_PushConstants.ResourceDescriptorIndex].Load(int3(position.xy, 0));
88
}

UnleashedRecomp/gpu/shader/copy_depth_ps.hlsl renamed to UnleashedRecomp/gpu/shader/hlsl/copy_depth_ps.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Texture2D<float> g_Texture2DDescriptorHeap[] : register(t0, space0);
44

5-
float main(in float4 position : SV_Position) : SV_Depth
5+
float shaderMain(in float4 position : SV_Position) : SV_Depth
66
{
77
return g_Texture2DDescriptorHeap[g_PushConstants.ResourceDescriptorIndex].Load(int3(position.xy, 0));
88
}

UnleashedRecomp/gpu/shader/copy_vs.hlsl renamed to UnleashedRecomp/gpu/shader/hlsl/copy_vs.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
void main(in uint vertexId : SV_VertexID, out float4 position : SV_Position, out float2 texCoord : TEXCOORD)
1+
void shaderMain(in uint vertexId : SV_VertexID, out float4 position : SV_Position, out float2 texCoord : TEXCOORD)
22
{
33
texCoord = float2((vertexId << 1) & 2, vertexId & 2);
44
position = float4(texCoord * float2(2.0, -2.0) + float2(-1.0, 1.0), 0.0, 1.0);

0 commit comments

Comments
 (0)