From 2a218d418f0e16b02ca7ef674d0f97231465f15b Mon Sep 17 00:00:00 2001 From: Samuliak Date: Fri, 17 Jan 2025 14:51:25 +0100 Subject: [PATCH] use depth mask when writing to depth --- src/Cafe/HW/Latte/Core/LatteShader.cpp | 7 +++++++ src/Cafe/HW/Latte/LegacyShaderDecompiler/LatteDecompiler.h | 4 ++-- .../LegacyShaderDecompiler/LatteDecompilerAnalyzer.cpp | 7 ++++--- .../LegacyShaderDecompiler/LatteDecompilerEmitMSL.cpp | 3 +++ .../LatteDecompilerEmitMSLHeader.hpp | 4 +--- 5 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/Cafe/HW/Latte/Core/LatteShader.cpp b/src/Cafe/HW/Latte/Core/LatteShader.cpp index 2e1b69ed..e0164584 100644 --- a/src/Cafe/HW/Latte/Core/LatteShader.cpp +++ b/src/Cafe/HW/Latte/Core/LatteShader.cpp @@ -678,6 +678,13 @@ uint64 LatteSHRC_CalcPSAuxHash(LatteDecompilerShader* pixelShader, uint32* conte auxHash = std::rotl(auxHash, 7); auxHash += (uint64)dataType; } + + bool hasDepthBuffer = LatteMRT::GetActiveDepthBufferMask(LatteGPUState.contextNew); + if (hasDepthBuffer) + { + auxHash = std::rotl(auxHash, 5); + auxHash += 13u; + } } #endif diff --git a/src/Cafe/HW/Latte/LegacyShaderDecompiler/LatteDecompiler.h b/src/Cafe/HW/Latte/LegacyShaderDecompiler/LatteDecompiler.h index 21f6d2b2..475bacb0 100644 --- a/src/Cafe/HW/Latte/LegacyShaderDecompiler/LatteDecompiler.h +++ b/src/Cafe/HW/Latte/LegacyShaderDecompiler/LatteDecompiler.h @@ -187,8 +187,8 @@ struct LatteDecompilerShader // analyzer stage (pixel outputs) uint32 pixelColorOutputMask{ 0 }; // from LSB to MSB, 1 bit per written output. 1 if written (indices of color attachments) - // analyzer stage (depth write) - bool depthWritten{ false }; + // analyzer stage (depth output) + bool depthMask{ false }; // analyzer stage (geometry shader parameters/inputs) uint32 ringParameterCount{ 0 }; uint32 ringParameterCountFromPrevStage{ 0 }; // used in geometry shader to hold VS ringParameterCount diff --git a/src/Cafe/HW/Latte/LegacyShaderDecompiler/LatteDecompilerAnalyzer.cpp b/src/Cafe/HW/Latte/LegacyShaderDecompiler/LatteDecompilerAnalyzer.cpp index 4d924e94..cbb2b3ff 100644 --- a/src/Cafe/HW/Latte/LegacyShaderDecompiler/LatteDecompilerAnalyzer.cpp +++ b/src/Cafe/HW/Latte/LegacyShaderDecompiler/LatteDecompilerAnalyzer.cpp @@ -387,7 +387,7 @@ void LatteDecompiler_analyzeExport(LatteDecompilerShaderContext* shaderContext, LatteDecompilerShader* shader = shaderContext->shader; if( shader->shaderType == LatteConst::ShaderType::Pixel ) { - if( cfInstruction->exportType == 0 && cfInstruction->exportArrayBase < 8 ) + if (cfInstruction->exportType == 0 && cfInstruction->exportArrayBase < 8) { // remember color outputs that are written for(uint32 i=0; i<(cfInstruction->exportBurstCount+1); i++) @@ -396,9 +396,10 @@ void LatteDecompiler_analyzeExport(LatteDecompilerShaderContext* shaderContext, shader->pixelColorOutputMask |= (1<exportType == 0 && cfInstruction->exportArrayBase == 61 ) + else if (cfInstruction->exportType == 0 && cfInstruction->exportArrayBase == 61) { - shader->depthWritten = true; + if (LatteMRT::GetActiveDepthBufferMask(*shaderContext->contextRegistersNew)) + shader->depthMask = true; } else debugBreakpoint(); diff --git a/src/Cafe/HW/Latte/LegacyShaderDecompiler/LatteDecompilerEmitMSL.cpp b/src/Cafe/HW/Latte/LegacyShaderDecompiler/LatteDecompilerEmitMSL.cpp index e14d494a..d3f2e3e8 100644 --- a/src/Cafe/HW/Latte/LegacyShaderDecompiler/LatteDecompilerEmitMSL.cpp +++ b/src/Cafe/HW/Latte/LegacyShaderDecompiler/LatteDecompilerEmitMSL.cpp @@ -3316,6 +3316,9 @@ static void _emitExportCode(LatteDecompilerShaderContext* shaderContext, LatteDe cemu_assert_unimplemented(); // ukn } + if (!shaderContext->shader->depthMask) + return; + src->add("out.passDepth = "); _emitExportGPRReadCode(shaderContext, cfInstruction, LATTE_DECOMPILER_DTYPE_FLOAT, 0); src->add(".x"); diff --git a/src/Cafe/HW/Latte/LegacyShaderDecompiler/LatteDecompilerEmitMSLHeader.hpp b/src/Cafe/HW/Latte/LegacyShaderDecompiler/LatteDecompilerEmitMSLHeader.hpp index 84722a24..ab890671 100644 --- a/src/Cafe/HW/Latte/LegacyShaderDecompiler/LatteDecompilerEmitMSLHeader.hpp +++ b/src/Cafe/HW/Latte/LegacyShaderDecompiler/LatteDecompilerEmitMSLHeader.hpp @@ -298,10 +298,8 @@ namespace LatteDecompiler } // generate depth output for pixel shader - if (decompilerContext->shader->depthWritten) - { + if (decompilerContext->shader->depthMask) src->add("float passDepth [[depth(any)]];" _CRLF); - } src->add("};" _CRLF _CRLF); }