From f2fe3f71656d1682f963b22272d2be2adaf51966 Mon Sep 17 00:00:00 2001 From: TibixDev Date: Thu, 8 Oct 2026 03:39:05 +0300 Subject: [PATCH] fix(agc): reject depth maintenance before shader compilation --- .../src/Driver/Draw/DrawPrecheck.cpp | 2 ++ .../Graphics/include/State.hpp | 1 + .../libSceAgcDriver/Graphics/src/State.cpp | 8 ++++++ .../prx/libSceAgcDriver/tests/Graphics.cpp | 26 +++++++++++++++++++ 4 files changed, 37 insertions(+) diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawPrecheck.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawPrecheck.cpp index f2842ad61..84ed003cc 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawPrecheck.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Driver/Draw/DrawPrecheck.cpp @@ -23,6 +23,8 @@ std::optional Driver::precheckDraw(const QueueState& queue, const S localDevice->ColorMetadataPass(*pass); return DrawVerdict::Drawn; } + rejected = Graphics::DepthMaintenanceRejection(queue); + if (!rejected.empty()) return DrawVerdict::Rejected; static const bool traceIndirectEnabled = std::getenv("APS5_TRACE_INDIRECT_DRAWS") != nullptr; traceIndirect = traceIndirectEnabled; if (traceIndirect && drawParameters.indirect) std::fprintf(stderr, "[draw] indirect packet %s args 0x%llx count %u reached\n", Pm4::Name(packet[0]).c_str(), static_cast(drawParameters.indirect->arguments), drawParameters.indirect->count); diff --git a/core/libs/prx/libSceAgcDriver/Graphics/include/State.hpp b/core/libs/prx/libSceAgcDriver/Graphics/include/State.hpp index 8367436d9..3fddae363 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/include/State.hpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/include/State.hpp @@ -107,6 +107,7 @@ struct ColorMetadataPass { std::vector targets; }; std::optional DecodeColorMetadataPass(const QueueState& queue); +std::string DepthMaintenanceRejection(const QueueState& queue); // The message DecodeState (or the pixel stage decode after it) would throw for the register rules // 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. diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp index 53288a665..0af8616ab 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/State.cpp @@ -429,7 +429,14 @@ ShaderStages DecodeShaderStages(const QueueState& queue) { return result; } +std::string DepthMaintenanceRejection(const QueueState& queue) { + const auto control = find(queue.context, 0x000); + if (control == queue.context.end() || (control->second & ~0x2063u) == 0) return {}; + return zeroMessage(0x000, control->second, "DB_RENDER_CONTROL depth copy, resummarize or decompress"); +} + State DecodeState(const QueueState& queue) { + if (auto reason = DepthMaintenanceRejection(queue); !reason.empty()) throw std::runtime_error(reason); const auto& cx = queue.context; State result{}; result.stages = DecodeShaderStages(queue); @@ -743,6 +750,7 @@ std::optional DecodeColorMetadataPass(const QueueState& queue } std::string DrawRejection(const QueueState& queue, bool indexed) { + if (auto reason = DepthMaintenanceRejection(queue); !reason.empty()) return reason; const auto& cx = queue.context; // A register a rule needs that is absent gives no verdict here: DecodeState reports it. const auto value = [&](const Registers& registers, std::uint32_t offset, std::uint32_t& out) { diff --git a/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp b/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp index e6e2faefe..558d2a804 100644 --- a/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp @@ -582,6 +582,31 @@ void DepthStencilTests() { expectFailure([&] { AgcDriver::Graphics::DecodeState(queue); }, "DCC 3D color targets"); } +void depthMaintenanceTests() { + for (const auto mode : {0x4u, 0x8u, 0x10u, 0x80u, 0x100u, 0x1000u, 0x4000u}) { + for (const auto clear : {0u, 1u, 2u, 3u}) { + auto queue = makeState(); + queue.context[0x000] = mode | clear; + queue.context[0x200] = 0; + queue.context[0x8e] = 0; + queue.context[0x8f] = 0; + queue.shader.erase(0x8); + const auto reason = AgcDriver::Graphics::DepthMaintenanceRejection(queue); + Require(reason.find("DB_RENDER_CONTROL") != std::string::npos, "depth maintenance passed without depth tests or color writes"); + Require(AgcDriver::Graphics::DrawRejection(queue, false) == reason, "draw precheck did not reject depth maintenance first"); + expectFailure([&] { AgcDriver::Graphics::DecodeState(queue); }, reason); + } + } + for (const auto control : {0u, 1u, 2u, 3u, 0x20u, 0x40u, 0x2000u, 0x2063u}) { + auto queue = makeState(); + queue.context[0x000] = control; + Require(AgcDriver::Graphics::DepthMaintenanceRejection(queue).empty(), "ordinary depth controls were mistaken for maintenance"); + } + auto absent = makeState(); + absent.context.erase(0x000); + Require(AgcDriver::Graphics::DepthMaintenanceRejection(absent).empty(), "an absent depth control produced a maintenance verdict"); +} + // SPI_SHADER_Z_FORMAT (0x1c4) and the export enables of DB_SHADER_CONTROL (0x203): Z export needs a // format with a depth channel (1, 2, 3 or 32_ABGR 9), the sample mask needs 32_ABGR, and the // formats the export path does not lay out are refused. A missing 0x1c4 gives no verdict here. @@ -2138,6 +2163,7 @@ int main() { AgcDriver::Graphics::ValidateDepthBounds(unrestricted, bounded); } stateTests(); + depthMaintenanceTests(); hardwareScreenOffsetTests(); DepthClipTests(); DepthStencilTests();