From a48a9eabf7289a43fd962ee7ca0c2010a2cef757 Mon Sep 17 00:00:00 2001 From: Pacuka <69674426+Pacuka@users.noreply.github.com> Date: Sun, 4 Oct 2026 20:57:01 +0200 Subject: [PATCH] fix(agc): keep the pixel register fallbacks to the null program and refuse its color writes Co-Authored-By: Claude Opus 5.5 --- .../Execution/src/Driver/Draw/DrawDecode.cpp | 7 +++++- .../Graphics/include/ShaderInputState.hpp | 2 +- .../Graphics/include/State.hpp | 2 ++ .../Graphics/src/ShaderInputState.cpp | 8 +++---- .../libSceAgcDriver/Graphics/src/State.cpp | 23 ++++++++++++++++++- .../prx/libSceAgcDriver/tests/Graphics.cpp | 18 +++++++++++---- 6 files changed, 49 insertions(+), 11 deletions(-) diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawDecode.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawDecode.cpp index c913e0202..42a10f51d 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawDecode.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawDecode.cpp @@ -89,9 +89,14 @@ std::shared_ptr Driver::decodeDraw(const QueueState& queue, const Su } else { append(0xc8, 2, Stage::Vertex, 0x8b, 0x8c, Role::Main); } + const bool nullPixel = Graphics::PixelProgramUnset(queue); + if (nullPixel) { + const auto rejection = Graphics::NullPixelProgramRejection(queue); + require(rejection.empty(), rejection.c_str()); + } append(0x008, 1, Stage::Fragment, 0x00b, 0x00c, Role::Fragment); programs.back().firstUserSgpr = 0; - product->pixel = Graphics::DecodePixelStageInfo(queue.context, Graphics::ExportMappings(graphics)); + product->pixel = Graphics::DecodePixelStageInfo(queue.context, Graphics::ExportMappings(graphics), nullPixel); return product; } } diff --git a/core/libs/prx/libSceAgcDriver/Graphics/include/ShaderInputState.hpp b/core/libs/prx/libSceAgcDriver/Graphics/include/ShaderInputState.hpp index df16329a9..d54fba676 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/include/ShaderInputState.hpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/include/ShaderInputState.hpp @@ -10,7 +10,7 @@ namespace AgcDriver::Graphics { -ShaderRecompiler::ShaderPixelStageInfo DecodePixelStageInfo(const Registers& context, const std::array& exportMappings); +ShaderRecompiler::ShaderPixelStageInfo DecodePixelStageInfo(const Registers& context, const std::array& exportMappings, bool nullProgram = false); ShaderRecompiler::ShaderComputeStageInfo DecodeComputeStageInfo(const Registers& shader); // A guest range DecodeVertexStageInfo read (an attribute word, a vertex V#) with the bytes as read: // the draw cache validates them by value with the stage's capture (design_cpu_final rule RD). diff --git a/core/libs/prx/libSceAgcDriver/Graphics/include/State.hpp b/core/libs/prx/libSceAgcDriver/Graphics/include/State.hpp index 9c49f888d..dc8a45223 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/include/State.hpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/include/State.hpp @@ -108,6 +108,8 @@ std::optional DecodeColorMetadataPass(const QueueState& queue // this precheck covers, evaluated without exceptions before the draw is decoded; empty when they // pass (DecodeState still checks everything). A register a rule needs that is absent is no verdict. std::string DrawRejection(const QueueState& queue, bool indexed); +bool PixelProgramUnset(const QueueState& queue); +std::string NullPixelProgramRejection(const QueueState& queue); // The recording facade of the draw decoders (design_cpu_final M8, step 8a): every register read // of DecodeShaderStages, DecodeState, DrawRejection, DecodePixelStageInfo and Driver::draw's diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderInputState.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderInputState.cpp index 09b5c58af..44ee5932f 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderInputState.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderInputState.cpp @@ -91,14 +91,14 @@ ShaderRecompiler::ShaderComputeStageInfo DecodeComputeStageInfo(const Registers& }; } -ShaderRecompiler::ShaderPixelStageInfo DecodePixelStageInfo(const Registers& context, const std::array& exportMappings) { +ShaderRecompiler::ShaderPixelStageInfo DecodePixelStageInfo(const Registers& context, const std::array& exportMappings, bool nullProgram) { const auto inControl = read(context, spiPsInControl, RegisterBank::Context); const auto inputNum = inControl & 0x3Fu; if (inputNum > 32u) { throw std::runtime_error("AGC graphics: SPI_PS_IN_CONTROL input count exceeds 32"); } - const auto ena = readOr(context, spiPsInputEna, RegisterBank::Context, defaultPixelInputs); - const auto addr = readOr(context, spiPsInputAddr, RegisterBank::Context, defaultPixelInputs); + const auto ena = nullProgram ? readOr(context, spiPsInputEna, RegisterBank::Context, defaultPixelInputs) : read(context, spiPsInputEna, RegisterBank::Context); + const auto addr = nullProgram ? readOr(context, spiPsInputAddr, RegisterBank::Context, defaultPixelInputs) : read(context, spiPsInputAddr, RegisterBank::Context); const auto activeInputs = ena & addr; using ShaderRecompiler::PixelInput; using ShaderRecompiler::PixelInputBit; @@ -121,7 +121,7 @@ ShaderRecompiler::ShaderPixelStageInfo DecodePixelStageInfo(const Registers& con if (((shaderControl >> 13u) & 0x3u) != 0) { throw std::runtime_error("AGC graphics: DB_SHADER_CONTROL.CONSERVATIVE_Z_EXPORT is unsupported"); } - const auto colFormat = readOr(context, spiShaderColFormat, RegisterBank::Context, 0u); + const auto colFormat = nullProgram ? readOr(context, spiShaderColFormat, RegisterBank::Context, 0u) : read(context, spiShaderColFormat, RegisterBank::Context); std::array targetOutputMode{}; for (std::uint32_t i = 0; i < 8u; ++i) { targetOutputMode[i] = static_cast((colFormat >> (4u * i)) & 0xFu); diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp index 4ee1ec8b3..27de97ca1 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp @@ -506,7 +506,7 @@ State DecodeState(const QueueState& queue) { // CB_COLOR_CONTROL mode 0 disables color writes, which only matters when a target is written. if (const auto colorControl = read(cx, 0x202); !colorControlSupported(colorControl, result.hasColorTarget)) throw std::runtime_error(colorControlMessage(colorControl)); zero(cx, 0x1c4, ~0u, "depth or sample-mask export"); - const auto exportFormat = read(cx, 0x1c5); + const auto exportFormat = result.hasColorTarget ? read(cx, 0x1c5) : 0u; APS5_LOG_OUT_DEBUG("Export format=%u", exportFormat); // SPI_SHADER_POS_FORMAT: POS0 must be a 4-component position; later vectors carry the misc/clip // exports that PA_CL_VS_OUT_CNTL validation above already limits to ignored layer/viewport data. @@ -754,6 +754,27 @@ std::string DrawRejection(const QueueState& queue, bool indexed) { std::uint32_t targetMask = 0, shaderMask = 0; if (value(cx, 0x8e, targetMask) && value(cx, 0x8f, shaderMask) && value(cx, 0x202, word) && !colorControlSupported(word, (targetMask & shaderMask) != 0)) return colorControlMessage(word); if (auto reason = nonzero(cx, 0x1c4, ~0u, "depth or sample-mask export"); !reason.empty()) return reason; + if (PixelProgramUnset(queue)) return NullPixelProgramRejection(queue); + for (const auto offset : {0x1b3u, 0x1b4u, 0x1c5u}) { + if (find(cx, offset) != cx.end()) continue; + char text[64]; + std::snprintf(text, sizeof(text), "AGC graphics: missing register at DWORD 0x%x", offset); + return text; + } + return {}; +} + +bool PixelProgramUnset(const QueueState& queue) { + const auto low = find(queue.shader, 0x008, RegisterBank::Shader); + const auto high = find(queue.shader, 0x009, RegisterBank::Shader); + return low != queue.shader.end() && high != queue.shader.end() && low->second == 0 && high->second == 0; +} + +std::string NullPixelProgramRejection(const QueueState& queue) { + const auto targetMask = find(queue.context, 0x8e); + const auto shaderMask = find(queue.context, 0x8f); + if (targetMask == queue.context.end() || shaderMask == queue.context.end()) return "AGC graphics: a draw without a pixel program needs CB_TARGET_MASK and CB_SHADER_MASK"; + if ((targetMask->second & shaderMask->second) != 0) return "AGC graphics: a draw without a pixel program writes color"; return {}; } diff --git a/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp b/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp index d4c052bc0..2da119a57 100644 --- a/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp @@ -170,11 +170,21 @@ void stateTests() { queue = makeState(); queue.context.erase(0x1b3); queue.context.erase(0x1b4); - Require(AgcDriver::Graphics::DrawRejection(queue, true).empty(), "a bank without SPI_PS_INPUT_ENA/ADDR was rejected"); - state = AgcDriver::Graphics::DecodeState(queue); queue.context.erase(0x1c5); - const auto unset = AgcDriver::Graphics::DecodePixelStageInfo(queue.context, AgcDriver::Graphics::ExportMappings(state)); - Require(unset.inputAddr == ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::PerspectiveCenter), "unset pixel inputs did not read as PERSP_CENTER_ENA"); + queue.shader[0x008] = 0x100; + queue.shader[0x009] = 0; + Require(!AgcDriver::Graphics::PixelProgramUnset(queue), "a pixel program address was read as unset"); + Require(AgcDriver::Graphics::DrawRejection(queue, true).find("missing register at DWORD 0x1b3") != std::string::npos, "a real pixel program without SPI_PS_INPUT_ENA was accepted"); + expectFailure([&] { AgcDriver::Graphics::DecodePixelStageInfo(queue.context, std::array{0xe4u, 0xe4u, 0xe4u, 0xe4u, 0xe4u, 0xe4u, 0xe4u, 0xe4u}); }, "missing register"); + queue.shader[0x008] = 0; + Require(AgcDriver::Graphics::PixelProgramUnset(queue), "a zero pixel program address was not read as unset"); + Require(AgcDriver::Graphics::DrawRejection(queue, true).find("writes color") != std::string::npos, "a draw without a pixel program that writes color was accepted"); + queue.context[0x8e] = 0; + Require(AgcDriver::Graphics::DrawRejection(queue, true).empty(), "a depth-only draw without a pixel program was rejected"); + state = AgcDriver::Graphics::DecodeState(queue); + Require(!state.hasColorTarget, "a depth-only draw without a pixel program decoded a color target"); + const auto unset = AgcDriver::Graphics::DecodePixelStageInfo(queue.context, AgcDriver::Graphics::ExportMappings(state), true); + Require(unset.inputAddr == ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::PerspectiveCenter), "unset pixel inputs of the null program did not read as PERSP_CENTER_ENA"); for (const auto mode : unset.targetOutputMode) Require(mode == 0, "an unset SPI_SHADER_COL_FORMAT exported a color"); }