shader-decompiler: fix disabled rasterization errors

This commit is contained in:
Samuliak 2025-05-22 18:58:49 +02:00
parent bddf3159ee
commit b59d134e03
No known key found for this signature in database
2 changed files with 21 additions and 35 deletions

View file

@ -20,8 +20,6 @@
#define _CRLF "\r\n"
static bool rasterizationEnabled;
void LatteDecompiler_emitAttributeDecodeMSL(LatteDecompilerShader* shaderContext, StringBuf* src, LatteParsedFetchShaderAttribute_t* attrib);
/*
@ -3182,9 +3180,6 @@ static void _emitExportGPRReadCode(LatteDecompilerShaderContext* shaderContext,
static void _emitExportCode(LatteDecompilerShaderContext* shaderContext, LatteDecompilerCFInstruction* cfInstruction)
{
if (!rasterizationEnabled)
return;
StringBuf* src = shaderContext->shaderSource;
src->add("// export" _CRLF);
if(shaderContext->shaderType == LatteConst::ShaderType::Vertex )
@ -3412,9 +3407,6 @@ static void _emitCFRingWriteCode(LatteDecompilerShaderContext* shaderContext, La
return;
}
if (!rasterizationEnabled)
return;
if (shaderContext->shaderType == LatteConst::ShaderType::Vertex)
{
if (cfInstruction->memWriteElemSize != 3)
@ -3937,9 +3929,6 @@ void LatteDecompiler_emitMSLShader(LatteDecompilerShaderContext* shaderContext,
bool usesGeometryShader = UseGeometryShader(*shaderContext->contextRegistersNew, shaderContext->options->usesGeometryShader);
bool fetchVertexManually = (usesGeometryShader || (shaderContext->fetchShader && shaderContext->fetchShader->mtlFetchVertexManually));
// Rasterization
rasterizationEnabled = shaderContext->contextRegistersNew->IsRasterizationEnabled();
StringBuf* src = new StringBuf(1024*1024*12); // reserve 12MB for generated source (we resize-to-fit at the end)
shaderContext->shaderSource = src;
@ -3953,7 +3942,7 @@ void LatteDecompiler_emitMSLShader(LatteDecompilerShaderContext* shaderContext,
src->add("#include <metal_stdlib>" _CRLF);
src->add("using namespace metal;" _CRLF);
// header part (definitions for inputs and outputs)
LatteDecompiler::emitHeader(shaderContext, isRectVertexShader, usesGeometryShader, fetchVertexManually, rasterizationEnabled);
LatteDecompiler::emitHeader(shaderContext, isRectVertexShader, usesGeometryShader, fetchVertexManually);
// helper functions
LatteDecompiler_emitHelperFunctions(shaderContext, src);
const char* functionType = "";
@ -4114,8 +4103,8 @@ void LatteDecompiler_emitMSLShader(LatteDecompilerShaderContext* shaderContext,
else
{
functionType = "vertex";
if (rasterizationEnabled)
outputTypeName = "VertexOut";
if (shaderContext->contextRegistersNew->IsRasterizationEnabled())
outputTypeName = "Out";
else
outputTypeName = "void";
}
@ -4126,7 +4115,7 @@ void LatteDecompiler_emitMSLShader(LatteDecompilerShaderContext* shaderContext,
break;
case LatteConst::ShaderType::Pixel:
functionType = "fragment";
outputTypeName = "FragmentOut";
outputTypeName = "Out";
break;
}
// start of main
@ -4152,28 +4141,25 @@ void LatteDecompiler_emitMSLShader(LatteDecompilerShaderContext* shaderContext,
src->add("VertexIn in = fetchVertex(vid, iid, indexBuffer, indexType VERTEX_BUFFERS);" _CRLF);
// Output is defined as object payload
src->add("object_data VertexOut& out = objectPayload.vertexOut[tid];" _CRLF);
src->add("object_data Out& out = objectPayload.vertexOut[tid];" _CRLF);
}
else
{
// Fetch the input
src->add("VertexIn in = fetchVertex(vid, iid VERTEX_BUFFERS);" _CRLF);
if (rasterizationEnabled)
src->add("VertexOut out;" _CRLF);
src->add("Out out;" _CRLF);
}
}
else if (shader->shaderType == LatteConst::ShaderType::Geometry)
{
src->add("GeometryOut out;" _CRLF);
src->add("Out out;" _CRLF);
// The index of the current vertex that is being emitted
src->add("uint vertexIndex = 0;" _CRLF);
}
}
else
{
if (rasterizationEnabled)
src->addFmt("{} out;" _CRLF, outputTypeName);
src->addFmt("Out out;" _CRLF);
}
// variable definition
if (shaderContext->typeTracker.useArrayGPRs == false)
@ -4397,7 +4383,7 @@ void LatteDecompiler_emitMSLShader(LatteDecompilerShaderContext* shaderContext,
// vertex shader should write renderstate point size at the end if required but not modified by shader
if (shaderContext->analyzer.outputPointSize && !shaderContext->analyzer.writesPointSize)
{
if (shader->shaderType == LatteConst::ShaderType::Vertex && !shaderContext->options->usesGeometryShader && rasterizationEnabled)
if (shader->shaderType == LatteConst::ShaderType::Vertex && !shaderContext->options->usesGeometryShader)
src->add("out.pointSize = supportBuffer.pointSize;" _CRLF);
}
@ -4435,7 +4421,7 @@ void LatteDecompiler_emitMSLShader(LatteDecompilerShaderContext* shaderContext,
}
}
if (rasterizationEnabled && (!usesGeometryShader || shader->shaderType == LatteConst::ShaderType::Pixel))
if (shader->shaderType == LatteConst::ShaderType::Pixel || (shaderContext->contextRegistersNew->IsRasterizationEnabled() && !usesGeometryShader))
{
// Return
src->add("return out;" _CRLF);

View file

@ -179,7 +179,7 @@ namespace LatteDecompiler
{
auto* src = shaderContext->shaderSource;
src->add("struct VertexOut {" _CRLF);
src->add("struct Out {" _CRLF);
src->add("float4 position [[position]] [[invariant]];" _CRLF);
if (shaderContext->analyzer.outputPointSize)
src->add("float pointSize [[point_size]];" _CRLF);
@ -239,7 +239,7 @@ namespace LatteDecompiler
if (isRectVertexShader)
{
src->add("struct ObjectPayload {" _CRLF);
src->add("VertexOut vertexOut[VERTICES_PER_VERTEX_PRIMITIVE];" _CRLF);
src->add("Out vertexOut[VERTICES_PER_VERTEX_PRIMITIVE];" _CRLF);
src->add("};" _CRLF _CRLF);
}
}
@ -270,7 +270,7 @@ namespace LatteDecompiler
src->add("};" _CRLF _CRLF);
}
static void _emitInputsAndOutputs(LatteDecompilerShaderContext* decompilerContext, bool isRectVertexShader, bool usesGeometryShader, bool fetchVertexManually, bool rasterizationEnabled)
static void _emitInputsAndOutputs(LatteDecompilerShaderContext* decompilerContext, bool isRectVertexShader, bool usesGeometryShader, bool fetchVertexManually)
{
auto src = decompilerContext->shaderSource;
@ -282,7 +282,7 @@ namespace LatteDecompiler
{
_emitPSInputs(decompilerContext);
src->add("struct FragmentOut {" _CRLF);
src->add("struct Out {" _CRLF);
// generate pixel outputs for pixel shader
for (uint32 i = 0; i < LATTE_NUM_COLOR_TARGET; i++)
@ -306,14 +306,14 @@ namespace LatteDecompiler
if (!usesGeometryShader || isRectVertexShader)
{
if (decompilerContext->shaderType == LatteConst::ShaderType::Vertex && rasterizationEnabled)
if (decompilerContext->shaderType == LatteConst::ShaderType::Vertex)
_emitVSOutputs(decompilerContext, isRectVertexShader);
}
else
{
if (decompilerContext->shaderType == LatteConst::ShaderType::Vertex || decompilerContext->shaderType == LatteConst::ShaderType::Geometry)
{
src->add("struct VertexOut {" _CRLF);
src->add("struct Out {" _CRLF);
uint32 ringParameterCountVS2GS = 0;
if (decompilerContext->shaderType == LatteConst::ShaderType::Vertex)
{
@ -327,7 +327,7 @@ namespace LatteDecompiler
src->addFmt("int4 passParameterSem{};" _CRLF, f);
src->add("};" _CRLF _CRLF);
src->add("struct ObjectPayload {" _CRLF);
src->add("VertexOut vertexOut[VERTICES_PER_VERTEX_PRIMITIVE];" _CRLF);
src->add("Out vertexOut[VERTICES_PER_VERTEX_PRIMITIVE];" _CRLF);
src->add("};" _CRLF _CRLF);
}
if (decompilerContext->shaderType == LatteConst::ShaderType::Geometry)
@ -339,7 +339,7 @@ namespace LatteDecompiler
if (((decompilerContext->contextRegisters[mmSQ_GSVS_RING_ITEMSIZE] & 0x7FFF) & 0xF) != 0)
debugBreakpoint();
src->add("struct GeometryOut {" _CRLF);
src->add("struct Out {" _CRLF);
src->add("float4 position [[position]];" _CRLF);
for (sint32 p = 0; p < decompilerContext->parsedGSCopyShader->numParam; p++)
{
@ -352,12 +352,12 @@ namespace LatteDecompiler
const uint32 MAX_VERTEX_COUNT = 32;
// Define the mesh shader output type
src->addFmt("using MeshType = mesh<GeometryOut, void, {}, GET_PRIMITIVE_COUNT({}), topology::MTL_PRIMITIVE_TYPE>;" _CRLF, MAX_VERTEX_COUNT, MAX_VERTEX_COUNT);
src->addFmt("using MeshType = mesh<Out, void, {}, GET_PRIMITIVE_COUNT({}), topology::MTL_PRIMITIVE_TYPE>;" _CRLF, MAX_VERTEX_COUNT, MAX_VERTEX_COUNT);
}
}
}
static void emitHeader(LatteDecompilerShaderContext* decompilerContext, bool isRectVertexShader, bool usesGeometryShader, bool fetchVertexManually, bool rasterizationEnabled)
static void emitHeader(LatteDecompilerShaderContext* decompilerContext, bool isRectVertexShader, bool usesGeometryShader, bool fetchVertexManually)
{
auto src = decompilerContext->shaderSource;
@ -403,7 +403,7 @@ namespace LatteDecompiler
// uniform buffers
_emitUniformBuffers(decompilerContext);
// inputs and outputs
_emitInputsAndOutputs(decompilerContext, isRectVertexShader, usesGeometryShader, fetchVertexManually, rasterizationEnabled);
_emitInputsAndOutputs(decompilerContext, isRectVertexShader, usesGeometryShader, fetchVertexManually);
if (dump_shaders_enabled)
decompilerContext->shaderSource->add("// end of shader inputs/outputs" _CRLF);