Skip to content

Commit a50ce99

Browse files
committed
fix: writing to depth from a fragment shader
1 parent 1bcdade commit a50ce99

File tree

4 files changed

+25
-22
lines changed

4 files changed

+25
-22
lines changed

src/Cafe/HW/Latte/LegacyShaderDecompiler/LatteDecompiler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ struct LatteDecompilerShader
182182

183183
// analyzer stage (pixel outputs)
184184
uint32 pixelColorOutputMask{ 0 }; // from LSB to MSB, 1 bit per written output. 1 if written (indices of color attachments)
185+
// analyzer stage (depth write)
186+
bool depthWritten{ false };
185187
// analyzer stage (geometry shader parameters/inputs)
186188
uint32 ringParameterCount{ 0 };
187189
uint32 ringParameterCountFromPrevStage{ 0 }; // used in geometry shader to hold VS ringParameterCount

src/Cafe/HW/Latte/LegacyShaderDecompiler/LatteDecompilerAnalyzer.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -287,15 +287,15 @@ void LatteDecompiler_analyzeTEXClause(LatteDecompilerShaderContext* shaderContex
287287
LatteDecompilerShader* shader = shaderContext->shader;
288288
for(auto& texInstruction : cfInstruction->instructionsTEX)
289289
{
290-
if( texInstruction.opcode == GPU7_TEX_INST_SAMPLE ||
291-
texInstruction.opcode == GPU7_TEX_INST_SAMPLE_L ||
292-
texInstruction.opcode == GPU7_TEX_INST_SAMPLE_LB ||
293-
texInstruction.opcode == GPU7_TEX_INST_SAMPLE_LZ ||
294-
texInstruction.opcode == GPU7_TEX_INST_SAMPLE_C ||
290+
if( texInstruction.opcode == GPU7_TEX_INST_SAMPLE ||
291+
texInstruction.opcode == GPU7_TEX_INST_SAMPLE_L ||
292+
texInstruction.opcode == GPU7_TEX_INST_SAMPLE_LB ||
293+
texInstruction.opcode == GPU7_TEX_INST_SAMPLE_LZ ||
294+
texInstruction.opcode == GPU7_TEX_INST_SAMPLE_C ||
295295
texInstruction.opcode == GPU7_TEX_INST_SAMPLE_C_L ||
296296
texInstruction.opcode == GPU7_TEX_INST_SAMPLE_C_LZ ||
297-
texInstruction.opcode == GPU7_TEX_INST_FETCH4 ||
298-
texInstruction.opcode == GPU7_TEX_INST_SAMPLE_G ||
297+
texInstruction.opcode == GPU7_TEX_INST_FETCH4 ||
298+
texInstruction.opcode == GPU7_TEX_INST_SAMPLE_G ||
299299
texInstruction.opcode == GPU7_TEX_INST_LD )
300300
{
301301
if (texInstruction.textureFetch.textureIndex < 0 || texInstruction.textureFetch.textureIndex >= LATTE_NUM_MAX_TEX_UNITS)
@@ -313,7 +313,7 @@ void LatteDecompiler_analyzeTEXClause(LatteDecompilerShaderContext* shaderContex
313313
shader->textureUnitSamplerAssignment[texInstruction.textureFetch.textureIndex] = texInstruction.textureFetch.samplerIndex;
314314
if( texInstruction.opcode == GPU7_TEX_INST_SAMPLE_C || texInstruction.opcode == GPU7_TEX_INST_SAMPLE_C_L || texInstruction.opcode == GPU7_TEX_INST_SAMPLE_C_LZ)
315315
shader->textureUsesDepthCompare[texInstruction.textureFetch.textureIndex] = true;
316-
316+
317317
bool useTexelCoords = false;
318318
if (texInstruction.opcode == GPU7_TEX_INST_SAMPLE && (texInstruction.textureFetch.unnormalized[0] && texInstruction.textureFetch.unnormalized[1] && texInstruction.textureFetch.unnormalized[2] && texInstruction.textureFetch.unnormalized[3]))
319319
useTexelCoords = true;
@@ -393,7 +393,7 @@ void LatteDecompiler_analyzeExport(LatteDecompilerShaderContext* shaderContext,
393393
}
394394
else if( cfInstruction->exportType == 0 && cfInstruction->exportArrayBase == 61 )
395395
{
396-
// writes pixel depth
396+
shader->depthWritten = true;
397397
}
398398
else
399399
debugBreakpoint();
@@ -419,7 +419,7 @@ void LatteDecompiler_analyzeExport(LatteDecompilerShaderContext* shaderContext,
419419
void LatteDecompiler_analyzeSubroutine(LatteDecompilerShaderContext* shaderContext, uint32 cfAddr)
420420
{
421421
// analyze CF and clauses up to RET statement
422-
422+
423423
// todo - find cfInstruction index from cfAddr
424424
cemu_assert_debug(false);
425425

@@ -505,9 +505,9 @@ namespace LatteDecompiler
505505
decompilerContext->hasUniformVarBlock = true;
506506
else if (decompilerContext->shader->uniformMode == LATTE_DECOMPILER_UNIFORM_MODE_FULL_CFILE)
507507
decompilerContext->hasUniformVarBlock = true;
508-
509-
bool hasAnyViewportScaleDisabled =
510-
!decompilerContext->contextRegistersNew->PA_CL_VTE_CNTL.get_VPORT_X_SCALE_ENA() ||
508+
509+
bool hasAnyViewportScaleDisabled =
510+
!decompilerContext->contextRegistersNew->PA_CL_VTE_CNTL.get_VPORT_X_SCALE_ENA() ||
511511
!decompilerContext->contextRegistersNew->PA_CL_VTE_CNTL.get_VPORT_Y_SCALE_ENA() ||
512512
!decompilerContext->contextRegistersNew->PA_CL_VTE_CNTL.get_VPORT_Z_SCALE_ENA();
513513
// we currently only support all on/off. Individual component scaling is not supported
@@ -803,7 +803,7 @@ void LatteDecompiler_analyze(LatteDecompilerShaderContext* shaderContext, LatteD
803803

804804
for(sint32 i=0; i<LATTE_NUM_MAX_TEX_UNITS; i++)
805805
{
806-
if (!shaderContext->output->textureUnitMask[i])
806+
if (!shaderContext->output->textureUnitMask[i])
807807
{
808808
// texture unit not used
809809
shader->textureUnitDim[i] = (Latte::E_DIM)0xFF;

src/Cafe/HW/Latte/LegacyShaderDecompiler/LatteDecompilerEmitMSL.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2441,11 +2441,12 @@ static void _emitTEXSampleTextureCode(LatteDecompilerShaderContext* shaderContex
24412441
src->add(")");
24422442
}
24432443
}
2444-
else if( texOpcode == GPU7_TEX_INST_SAMPLE_LZ || texOpcode == GPU7_TEX_INST_SAMPLE_C_LZ )
2445-
{
2446-
// TODO: correct?
2447-
src->add(", level(0.0)");
2448-
}
2444+
// TODO: uncomment?
2445+
//else if( texOpcode == GPU7_TEX_INST_SAMPLE_LZ || texOpcode == GPU7_TEX_INST_SAMPLE_C_LZ )
2446+
//{
2447+
// // TODO: correct?
2448+
// src->add(", level(0.0)");
2449+
//}
24492450
}
24502451
// gradient parameters
24512452
if (texOpcode == GPU7_TEX_INST_SAMPLE_G)
@@ -3215,7 +3216,7 @@ static void _emitExportCode(LatteDecompilerShaderContext* shaderContext, LatteDe
32153216
cemu_assert_unimplemented(); // ukn
32163217
}
32173218

3218-
src->add("out.depth = ");
3219+
src->add("out.passDepth = ");
32193220
_emitExportGPRReadCode(shaderContext, cfInstruction, LATTE_DECOMPILER_DTYPE_FLOAT, 0);
32203221
src->add(".x");
32213222
src->add(";" _CRLF);

src/Cafe/HW/Latte/LegacyShaderDecompiler/LatteDecompilerEmitMSLHeader.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,9 @@ namespace LatteDecompiler
272272
}
273273

274274
// generate depth output for pixel shader
275-
if (decompilerContext->shader->pixelDepthOutputMask)
275+
if (decompilerContext->shader->depthWritten)
276276
{
277-
src->add("float passDepth [[depth(any)]];" _CRLF);
277+
src->add("float passDepth [[depth]];" _CRLF);
278278
}
279279

280280
src->add("};" _CRLF _CRLF);

0 commit comments

Comments
 (0)