Skip to content

Commit

Permalink
Metal patches for Clang 9 build
Browse files Browse the repository at this point in the history
  • Loading branch information
TellowKrinkle committed Dec 25, 2021
1 parent 4ca9126 commit b2c574a
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 30 deletions.
9 changes: 7 additions & 2 deletions pcsx2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1430,6 +1430,7 @@ elseif(APPLE)
PCAP::PCAP
LibXml2::LibXml2
)
target_link_options(PCSX2_FLAGS INTERFACE "SHELL:-weak_framework Metal" "SHELL:-framework QuartzCore")
else()
target_link_libraries(PCSX2_FLAGS INTERFACE
GTK::gtk
Expand Down Expand Up @@ -1525,14 +1526,18 @@ if (APPLE)
$<$<NOT:$<CONFIG:Release,MinSizeRel>>:-M0>
)
set(std macos-metal2.0)
set(target air64-apple-macos10.13)
if (${CMAKE_CXX_COMPILER_VERSION} VERSION_LESS 10)
set(target)
else()
set(target -target air64-apple-macos10.13)
endif()
foreach(shader IN LISTS pcsx2GSMetalShaders)
set(shaderOut ${CMAKE_CURRENT_BINARY_DIR}/${shader}.air)
list(APPEND pcsx2GSMetalShaderOut ${shaderOut})
get_filename_component(shaderDir ${shaderOut} DIRECTORY)
add_custom_command(OUTPUT ${shaderOut}
COMMAND ${CMAKE_COMMAND} -E make_directory ${shaderDir}
COMMAND xcrun metal ${flags} -std=${std} -target ${target} -o ${shaderOut} -c ${CMAKE_CURRENT_SOURCE_DIR}/${shader}
COMMAND xcrun metal ${flags} -std=${std} ${target} -o ${shaderOut} -c ${CMAKE_CURRENT_SOURCE_DIR}/${shader}
DEPENDS ${shader} GS/Renderers/Metal/GSMTLSharedHeader.h GS/Renderers/Metal/GSMTLShaderCommon.h
)
endforeach()
Expand Down
5 changes: 4 additions & 1 deletion pcsx2/GS/GS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,10 @@ int _GSopen(const WindowInfo& wi, const char* title, GSRendererType renderer, in
dev = makeGSDeviceMTL();
s_renderer_name = "Metal";
renderer_name = "Metal";
break;
if (dev)
break;
fprintf(stderr, "Metal renderer not available! Falling back to OpenGL!\n");
[[fallthrough]];
#endif
case GSRendererType::OGL_HW:
dev = new GSDeviceOGL();
Expand Down
4 changes: 2 additions & 2 deletions pcsx2/GS/Renderers/Metal/GSDeviceMTL.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ struct std::hash<PipelineSelectorMTL>
}
};

class GSScopedDebugGroupMTL
class API_AVAILABLE(macos(10.13)) GSScopedDebugGroupMTL
{
id<MTLCommandBuffer> m_buffer;
public:
Expand All @@ -124,7 +124,7 @@ class GSScopedDebugGroupMTL

class GSTextureMTL;

class GSDeviceMTL final : public GSDevice
class API_AVAILABLE(macos(10.13)) GSDeviceMTL final : public GSDevice
{
public:
using DepthStencilSelector = GSHWDrawConfig::DepthStencilSelector;
Expand Down
63 changes: 40 additions & 23 deletions pcsx2/GS/Renderers/Metal/GSDeviceMTL.mm
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,27 @@

GSDevice* makeGSDeviceMTL()
{
return new GSDeviceMTL();
if (@available(macOS 10.13, *))
return new GSDeviceMTL();
return nullptr;
}

std::vector<std::string> getMTLAdapters(size_t* default_adapter_idx)
{
if (default_adapter_idx)
*default_adapter_idx = 0;
std::vector<std::string> ret;
@autoreleasepool
if (@available(macOS 10.13, *))
{
id<MTLDevice> default_adapter = MTLCreateSystemDefaultDevice();
for (id<MTLDevice> dev in MTLCopyAllDevices())
@autoreleasepool
{
if (dev == default_adapter && default_adapter_idx)
*default_adapter_idx = ret.size();
ret.push_back([[dev name] UTF8String]);
id<MTLDevice> default_adapter = MTLCreateSystemDefaultDevice();
for (id<MTLDevice> dev in MTLCopyAllDevices())
{
if (dev == default_adapter && default_adapter_idx)
*default_adapter_idx = ret.size();
ret.push_back([[dev name] UTF8String]);
}
}
}
return ret;
Expand Down Expand Up @@ -690,17 +695,17 @@ static void setFnConstantI(MTLFunctionConstantValues* fc, unsigned int value, GS

if (char* env = getenv("MTL_UNIFIED_MEMORY"))
m_unified_memory = env[0] == '1' || env[0] == 'y' || env[0] == 'Y';
else if (@available(macOS 10.15, iOS 13.0, *))
m_unified_memory = [m_dev hasUnifiedMemory];
// else if (@available(macOS 10.15, iOS 13.0, *))
// m_unified_memory = [m_dev hasUnifiedMemory];
else
m_unified_memory = false;

m_max_texsize = 8192;
if ([m_dev supportsFeatureSet:MTLFeatureSet_macOS_GPUFamily1_v1])
m_max_texsize = 16384;
if (@available(macOS 10.15, iOS 13.0, *))
if ([m_dev supportsFamily:MTLGPUFamilyApple3])
m_max_texsize = 16384;
// if (@available(macOS 10.15, iOS 13.0, *))
// if ([m_dev supportsFamily:MTLGPUFamilyApple3])
// m_max_texsize = 16384;

if (!(m_dev && m_layer && m_view))
return false;
Expand Down Expand Up @@ -1323,13 +1328,13 @@ static void setFnConstantI(MTLFunctionConstantValues* fc, unsigned int value, GS

static void textureBarrier(id<MTLRenderCommandEncoder> enc)
{
if (@available(macOS 10.14, *)) {
[enc memoryBarrierWithScope:MTLBarrierScopeRenderTargets
afterStages:MTLRenderStageVertex
beforeStages:MTLRenderStageFragment];
} else {
// if (@available(macOS 10.14, *)) {
// [enc memoryBarrierWithScope:MTLBarrierScopeRenderTargets
// afterStages:MTLRenderStageVertex
// beforeStages:MTLRenderStageFragment];
// } else {
[enc textureBarrier];
}
// }
}

void GSDeviceMTL::SetTexture(MainRenderEncoder& enc, GSTexture* tex, int pos)
Expand Down Expand Up @@ -1382,10 +1387,22 @@ static void textureBarrier(id<MTLRenderCommandEncoder> enc)
if (!has_scissor)
return;
has_scissor = false;
GSVector4i size = GSVector4i(0);
if (color_target) size = size.max_u32(GSVector4i(color_target ->GetSize()));
if (depth_target) size = size.max_u32(GSVector4i(depth_target ->GetSize()));
if (stencil_target) size = size.max_u32(GSVector4i(stencil_target->GetSize()));
GSVector2i size = GSVector2i(0);
if (color_target)
{
size.x = std::max(size.x, color_target->GetSize().x);
size.y = std::max(size.y, color_target->GetSize().y);
}
if (depth_target)
{
size.x = std::max(size.x, depth_target->GetSize().x);
size.y = std::max(size.y, depth_target->GetSize().y);
}
if (stencil_target)
{
size.x = std::max(size.x, stencil_target->GetSize().x);
size.y = std::max(size.y, stencil_target->GetSize().y);
}
MTLScissorRect r;
r.x = 0;
r.y = 0;
Expand Down Expand Up @@ -1523,7 +1540,7 @@ static GSMTLMainPSUniform convertCB(const GSHWDrawConfig::PSConstantBuffer& cb,
EndScene();
}

static id<MTLTexture> getTexture(GSTexture* tex)
static API_AVAILABLE(macos(10.13)) id<MTLTexture> getTexture(GSTexture* tex)
{
return tex ? static_cast<GSTextureMTL*>(tex)->GetTexture() : nil;
}
Expand Down
2 changes: 1 addition & 1 deletion pcsx2/GS/Renderers/Metal/GSMTLSharedHeader.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ struct GSMTLMainVSUniform
vector_float2 vertex_offset;
vector_float2 texture_offset;
vector_float2 texture_scale;
uint max_depth;
unsigned int max_depth;
};

struct GSMTLMainPSUniform
Expand Down
2 changes: 1 addition & 1 deletion pcsx2/GS/Renderers/Metal/GSTextureMTL.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

class GSDeviceMTL;

class GSTextureMTL : public GSTexture
class API_AVAILABLE(macos(10.13)) GSTextureMTL : public GSTexture
{
GSDeviceMTL* m_dev;
id<MTLTexture> m_texture;
Expand Down
9 changes: 9 additions & 0 deletions pcsx2/GS/Renderers/OpenGL/GSDeviceOGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2060,10 +2060,19 @@ static GSDeviceOGL::PSConstantBuffer convertCB(const GSHWDrawConfig::PSConstantB
const GSVector4i ditherHi = dither.sll16( 9).sra16( 5);
dither = ditherLow.blend8(ditherHi, GSVector4i(0xFF00FF00));

#if _M_SSE >= 0x401
out.DitherMatrix[0] = GSVector4(dither.xxxx().i8to32());
out.DitherMatrix[1] = GSVector4(dither.yyyy().i8to32());
out.DitherMatrix[2] = GSVector4(dither.zzzz().i8to32());
out.DitherMatrix[3] = GSVector4(dither.wwww().i8to32());
#else
const GSVector4i dl = dither.upl8(dither);
const GSVector4i dh = dither.uph8(dither);
out.DitherMatrix[0] = GSVector4(dl.upl8(dl).sra32(24));
out.DitherMatrix[1] = GSVector4(dl.uph8(dl).sra32(24));
out.DitherMatrix[2] = GSVector4(dh.upl8(dh).sra32(24));
out.DitherMatrix[3] = GSVector4(dh.uph8(dh).sra32(24));
#endif

return out;
}
Expand Down

0 comments on commit b2c574a

Please sign in to comment.