Skip to content

Commit 83da1b6

Browse files
TellowKrinklelightningterror
authored andcommitted
GS:MTL: AA1 Support
1 parent 226b4d2 commit 83da1b6

4 files changed

Lines changed: 179 additions & 47 deletions

File tree

pcsx2/GS/Renderers/Metal/GSDeviceMTL.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ class GSDeviceMTL final : public GSDevice
265265
MRCOwned<id<MTLRenderPipelineState>> m_shadeboost_pipeline;
266266
MRCOwned<id<MTLRenderPipelineState>> m_imgui_pipeline;
267267

268-
MRCOwned<id<MTLFunction>> m_hw_vs[1 << 5];
268+
MRCOwned<id<MTLFunction>> m_hw_vs[6 << 3];
269269
std::unordered_map<PSSelector, MRCOwned<id<MTLFunction>>> m_hw_ps;
270270
std::unordered_map<PipelineSelectorMTL, MRCOwned<id<MTLRenderPipelineState>>> m_hw_pipeline;
271271

@@ -290,7 +290,8 @@ class GSDeviceMTL final : public GSDevice
290290
GSTexture* depth_target = nullptr;
291291
GSTexture* stencil_target = nullptr;
292292
GSTexture* tex[GSMTLTextureIndexCount] = {};
293-
void* vertex_buffer = nullptr;
293+
id<MTLBuffer> vertex_buffer = nullptr;
294+
id<MTLBuffer> vs_index_buffer = nullptr;
294295
void* name = nullptr;
295296
struct Has
296297
{
@@ -430,6 +431,7 @@ class GSDeviceMTL final : public GSDevice
430431
void MRESetSampler(SamplerSelector sel);
431432
void MRESetTexture(GSTexture* tex, int pos);
432433
void MRESetVertices(id<MTLBuffer> buffer, size_t offset);
434+
void MRESetVSIndices(id<MTLBuffer> buffer, size_t offset);
433435
void MRESetScissor(const GSVector4i& scissor);
434436
void MREClearScissor();
435437
void MRESetCB(const GSHWDrawConfig::VSConstantBuffer& cb_vs);

pcsx2/GS/Renderers/Metal/GSDeviceMTL.mm

Lines changed: 58 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,7 @@ static bool getDepthFeedback(const GSMTLDevice& dev, bool fbfetch)
971971
m_features.cas_sharpening = true;
972972
m_features.test_and_sample_depth = true;
973973
m_features.depth_feedback = getDepthFeedback(m_dev, m_features.framebuffer_fetch);
974+
m_features.aa1 = GSConfig.HWAA1 && m_features.vs_expand;
974975
m_max_texture_size = m_dev.features.max_texsize;
975976

976977
// Init metal stuff
@@ -1115,13 +1116,8 @@ static bool getDepthFeedback(const GSMTLDevice& dev, bool fbfetch)
11151116
setFnConstantB(m_fn_constants, sel.fst, GSMTLConstantIndex_FST);
11161117
setFnConstantB(m_fn_constants, sel.iip, GSMTLConstantIndex_IIP);
11171118
setFnConstantB(m_fn_constants, sel.point_size, GSMTLConstantIndex_VS_POINT_SIZE);
1118-
NSString* shader = @"vs_main";
1119-
if (sel.expand != GSShader::VSExpand::None)
1120-
{
1121-
setFnConstantI(m_fn_constants, sel.expand, GSMTLConstantIndex_VS_EXPAND_TYPE);
1122-
shader = @"vs_main_expand";
1123-
}
1124-
m_hw_vs[i] = LoadShader(shader);
1119+
setFnConstantI(m_fn_constants, sel.expand, GSMTLConstantIndex_VS_EXPAND_TYPE);
1120+
m_hw_vs[i] = LoadShader(sel.expand == GSShader::VSExpand::None ? @"vs_main" : @"vs_main_expand");
11251121
}
11261122

11271123
// Init pipelines
@@ -1993,6 +1989,8 @@ static MTLBlendOperation ConvertBlendOp(GSDevice::BlendOp generic)
19931989
setFnConstantB(m_fn_constants, pssel.manual_lod, GSMTLConstantIndex_PS_MANUAL_LOD);
19941990
setFnConstantB(m_fn_constants, pssel.region_rect, GSMTLConstantIndex_PS_REGION_RECT);
19951991
setFnConstantI(m_fn_constants, pssel.scanmsk, GSMTLConstantIndex_PS_SCANMSK);
1992+
setFnConstantI(m_fn_constants, pssel.aa1, GSMTLConstantIndex_PS_AA1);
1993+
setFnConstantB(m_fn_constants, pssel.abe, GSMTLConstantIndex_PS_ABE);
19961994
setFnConstantI(m_fn_constants, pssel.sw_aniso, GSMTLConstantIndex_PS_SW_ANISO);
19971995
auto newps = LoadShader(@"ps_main");
19981996
ps = newps;
@@ -2086,9 +2084,9 @@ static void textureBarrier(id<MTLRenderCommandEncoder> enc)
20862084

20872085
void GSDeviceMTL::MRESetVertices(id<MTLBuffer> buffer, size_t offset)
20882086
{
2089-
if (m_current_render.vertex_buffer != (__bridge void*)buffer)
2087+
if (m_current_render.vertex_buffer != buffer)
20902088
{
2091-
m_current_render.vertex_buffer = (__bridge void*)buffer;
2089+
m_current_render.vertex_buffer = buffer;
20922090
[m_current_render.encoder setVertexBuffer:buffer offset:offset atIndex:GSMTLBufferIndexHWVertices];
20932091
}
20942092
else
@@ -2097,6 +2095,19 @@ static void textureBarrier(id<MTLRenderCommandEncoder> enc)
20972095
}
20982096
}
20992097

2098+
void GSDeviceMTL::MRESetVSIndices(id<MTLBuffer> buffer, size_t offset)
2099+
{
2100+
if (m_current_render.vs_index_buffer != buffer)
2101+
{
2102+
m_current_render.vs_index_buffer = buffer;
2103+
[m_current_render.encoder setVertexBuffer:buffer offset:offset atIndex:GSMTLBufferIndexHWIndices];
2104+
}
2105+
else
2106+
{
2107+
[m_current_render.encoder setVertexBufferOffset:offset atIndex:GSMTLBufferIndexHWIndices];
2108+
}
2109+
}
2110+
21002111
void GSDeviceMTL::MRESetScissor(const GSVector4i& scissor)
21012112
{
21022113
if (m_current_render.has.scissor && (m_current_render.scissor == scissor).alltrue())
@@ -2236,6 +2247,8 @@ static bool usesStencil(GSHWDrawConfig::DestinationAlphaMode dstalpha)
22362247
MRESetCB(config.cb_vs);
22372248
MRESetCB(config.cb_ps);
22382249
MRESetVertices(verts.gpu_buffer, verts.gpu_offset);
2250+
if (config.vs.UseVSExpandIndexBuffer())
2251+
MRESetVSIndices(verts.gpu_buffer, verts.gpu_offset + config.nverts * sizeof(*config.verts));
22392252
}
22402253

22412254
void GSDeviceMTL::RenderHW(GSHWDrawConfig& config)
@@ -2248,18 +2261,27 @@ static bool usesStencil(GSHWDrawConfig::DestinationAlphaMode dstalpha)
22482261
Map allocation = Allocate(m_vertex_upload_buf, vertsize + idxsize);
22492262
memcpy(allocation.cpu_buffer, config.verts, vertsize);
22502263

2251-
id<MTLBuffer> index_buffer;
2252-
size_t index_buffer_offset;
2264+
id<MTLBuffer> index_buffer = nil;
2265+
size_t index_buffer_offset = 0;
22532266
if (!config.vs.UseFixedExpandIndexBuffer())
22542267
{
22552268
memcpy(static_cast<u8*>(allocation.cpu_buffer) + vertsize, config.indices, idxsize);
2256-
index_buffer = allocation.gpu_buffer;
2257-
index_buffer_offset = allocation.gpu_offset + vertsize;
2269+
if (config.vs.UseVSExpandIndexBuffer())
2270+
{
2271+
// VS expand index buffer is bound to the VS instead of the input assembler
2272+
u32 expand = GetExpansionFactor(config.vs.expand);
2273+
config.nindices *= expand;
2274+
config.indices_per_prim *= expand;
2275+
}
2276+
else
2277+
{
2278+
index_buffer = allocation.gpu_buffer;
2279+
index_buffer_offset = allocation.gpu_offset + vertsize;
2280+
}
22582281
}
22592282
else
22602283
{
22612284
index_buffer = m_expand_index_buffer;
2262-
index_buffer_offset = 0;
22632285
}
22642286

22652287
FlushClears(config.tex);
@@ -2434,6 +2456,24 @@ static bool usesStencil(GSHWDrawConfig::DestinationAlphaMode dstalpha)
24342456
Recycle(primid_tex);
24352457
}}
24362458

2459+
static void EncodeDraw(id<MTLRenderCommandEncoder> enc, MTLPrimitiveType topology, size_t count, id<MTLBuffer> indices, size_t off, size_t base_vertex)
2460+
{
2461+
if (indices)
2462+
{
2463+
[enc drawIndexedPrimitives:topology
2464+
indexCount:count
2465+
indexType:MTLIndexTypeUInt16
2466+
indexBuffer:indices
2467+
indexBufferOffset:off + base_vertex * sizeof(uint16_t)];
2468+
}
2469+
else
2470+
{
2471+
[enc drawPrimitives:topology
2472+
vertexStart:base_vertex
2473+
vertexCount:count];
2474+
}
2475+
}
2476+
24372477
void GSDeviceMTL::SendHWDraw(GSHWDrawConfig& config, id<MTLRenderCommandEncoder> enc, id<MTLBuffer> buffer, size_t off, bool one_barrier, bool full_barrier)
24382478
{
24392479
MTLPrimitiveType topology;
@@ -2446,14 +2486,8 @@ static bool usesStencil(GSHWDrawConfig::DestinationAlphaMode dstalpha)
24462486

24472487
if (!m_features.texture_barrier) [[unlikely]]
24482488
{
2449-
[enc drawIndexedPrimitives:topology
2450-
indexCount:config.nindices
2451-
indexType:MTLIndexTypeUInt16
2452-
indexBuffer:buffer
2453-
indexBufferOffset:off];
2454-
2489+
EncodeDraw(enc, topology, config.nindices, buffer, off, 0);
24552490
g_perfmon.Put(GSPerfMon::DrawCalls, 1);
2456-
24572491
return;
24582492
}
24592493

@@ -2486,13 +2520,9 @@ static bool usesStencil(GSHWDrawConfig::DestinationAlphaMode dstalpha)
24862520

24872521
for (u32 n = 0, p = 0; n < draw_list_size; n++)
24882522
{
2489-
const u32 count = (*config.drawlist)[n] * indices_per_prim;
2523+
const size_t count = (*config.drawlist)[n] * indices_per_prim;
24902524
textureBarrier(enc);
2491-
[enc drawIndexedPrimitives:topology
2492-
indexCount:count
2493-
indexType:MTLIndexTypeUInt16
2494-
indexBuffer:buffer
2495-
indexBufferOffset:off + p * sizeof(*config.indices)];
2525+
EncodeDraw(enc, topology, count, buffer, off, p);
24962526
p += count;
24972527
}
24982528

@@ -2506,12 +2536,7 @@ static bool usesStencil(GSHWDrawConfig::DestinationAlphaMode dstalpha)
25062536
g_perfmon.Put(GSPerfMon::Barriers, 1);
25072537
}
25082538

2509-
[enc drawIndexedPrimitives:topology
2510-
indexCount:config.nindices
2511-
indexType:MTLIndexTypeUInt16
2512-
indexBuffer:buffer
2513-
indexBufferOffset:off];
2514-
2539+
EncodeDraw(enc, topology, config.nindices, buffer, off, 0);
25152540
g_perfmon.Put(GSPerfMon::DrawCalls, 1);
25162541
}
25172542

pcsx2/GS/Renderers/Metal/GSMTLSharedHeader.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ enum GSMTLBufferIndices
1111
GSMTLBufferIndexUniforms,
1212
GSMTLBufferIndexHWVertices,
1313
GSMTLBufferIndexHWUniforms,
14+
GSMTLBufferIndexHWIndices,
1415
};
1516

1617
enum GSMTLTextureIndex
@@ -216,5 +217,7 @@ enum GSMTLFnConstants
216217
GSMTLConstantIndex_PS_MANUAL_LOD,
217218
GSMTLConstantIndex_PS_REGION_RECT,
218219
GSMTLConstantIndex_PS_SCANMSK,
220+
GSMTLConstantIndex_PS_AA1,
221+
GSMTLConstantIndex_PS_ABE,
219222
GSMTLConstantIndex_PS_SW_ANISO,
220223
};

0 commit comments

Comments
 (0)