From 69e2d801d1d9c5a47541501102f7613237115fda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Franch?= Date: Sat, 3 Oct 2026 09:00:00 +0200 Subject: [PATCH] feat(shader): decode the s_endpgm variants, s_round_mode and SMEM probe/discard s_endpgm_saved and s_endpgm_ordered_ps_done end the wave like s_endpgm (checked on an RDNA2 GPU: nothing after them runs), so they decode to SEndpgm and the control flow graph treats them as terminators. s_atc_probe, s_atc_probe_buffer and s_dcache_discard(_x2) have no visible effect and translate to no-ops like the other scalar cache controls. s_round_mode is ignored like s_setreg MODE writes; listed in TechnicalDebt. Co-Authored-By: Claude --- .../tests/execution/ScalarProgramEnd.cpp | 96 +++++++++++++++++++ .../tests/execution/ScalarProgramEndSaved.cpp | 96 +++++++++++++++++++ .../include/RdnaDecoder/RdnaOpcode.hpp | 5 + .../RdnaDecoder/src/RdnaMemoryOpDecoder.cpp | 4 + .../RdnaDecoder/src/RdnaScalarOpDecoder.cpp | 5 +- .../MemoryInstructionDispatch.cpp | 4 + .../Translation/src/ScalarInstructions.cpp | 1 + docs/dev/TechnicalDebt.md | 1 + 8 files changed, 211 insertions(+), 1 deletion(-) create mode 100644 core/libs/prx/libSceAgcDriver/tests/execution/ScalarProgramEnd.cpp create mode 100644 core/libs/prx/libSceAgcDriver/tests/execution/ScalarProgramEndSaved.cpp diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/ScalarProgramEnd.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/ScalarProgramEnd.cpp new file mode 100644 index 000000000..d65402bd7 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/ScalarProgramEnd.cpp @@ -0,0 +1,96 @@ +#include "prx/libSceAgcDriver/Execution/include/VulkanDevice.hpp" +#include "prx/libSceAgcDriver/Graphics/include/Draw.hpp" +#include "Recompiler.hpp" +#include "VulkanTestDevice.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace { + +using AgcDriver::Graphics::Require; +using ShaderRecompiler::ShaderStage; + +constexpr std::uint32_t Threads = 32; +constexpr std::uint32_t Inputs = 4; +constexpr std::uint32_t Results = 16; +alignas(256) std::array Input{}; +alignas(256) std::array Output{}; + +alignas(256) constexpr std::array Code{ + 0x34020084, 0x34060086, 0xe0301000, 0x80000401, 0xbf8c3f70, 0xbfa40003, 0xf4980000, 0xfa000000, + 0xf49c0000, 0xfa000000, 0xf4a00000, 0xfa000000, 0xf4a40000, 0xfa000000, 0xbfa40000, 0x4a0a0881, + 0x7e0c02ff, 0x00000bad, 0xbf060000, 0xbf840007, 0xbf840003, 0xe0701000, 0x80010503, 0xbf9e0000, + 0xe0701000, 0x80010603, 0xbf9b0000, 0xe0701004, 0x80010603, 0xbf810000, +}; + +void Fill(std::uint32_t tid, std::uint32_t* words) { words[0] = tid * 0x01010101u + 7u; } + +std::array BufferDescriptor(const void* data, std::uint32_t bytes) { + const auto address = reinterpret_cast(data); + return {static_cast(address), static_cast((address >> 32u) & 0xffffu), bytes, 0x01016facu}; +} + +std::string Hex(std::uint32_t value) { + char text[16]; + std::snprintf(text, sizeof(text), "0x%08x", value); + return text; +} + +void Expect(std::uint32_t tid, std::uint32_t actual, std::uint32_t expected, const char* name) { + Require(actual == expected, std::string("scalar program end: lane ") + std::to_string(tid) + " " + name + " is " + Hex(actual) + ", expected " + Hex(expected)); +} + +void Run(AgcDriver::VulkanDevice& device) { + for (std::uint32_t tid = 0; tid < Threads; ++tid) Fill(tid, &Input[tid * Inputs]); + Output.fill(0xdeadbeefu); + std::vector userData(8, 0u); + const auto input = BufferDescriptor(Input.data(), static_cast(Input.size() * 4u)); + const auto output = BufferDescriptor(Output.data(), static_cast(Output.size() * 4u)); + std::copy(input.begin(), input.end(), userData.begin()); + std::copy(output.begin(), output.end(), userData.begin() + 4); + const std::span code(Code); + const std::array memory{{{reinterpret_cast(code.data()), std::as_bytes(code)}}}; + const ShaderRecompiler::ShaderComputeStageInfo compute{{Threads, 1, 1}, 0u, {false, false, false}, false, 1}; + ShaderRecompiler::RecompileRequest request{ + {ShaderStage::Compute, reinterpret_cast(code.data()), code, 0, {}}, + {32, 0, userData, compute, std::nullopt, std::nullopt, memory}, + device.Target(), + {0, 0, 0, 128} + }; + request.useCache = false; + const auto result = ShaderRecompiler::Recompile(request); + device.Dispatch(result, 1, 1, 1, {}, reinterpret_cast(code.data())); + device.WaitIdle(); +} + +void Check() { + for (std::uint32_t tid = 0; tid < Threads; ++tid) { + const std::uint32_t* in = &Input[tid * Inputs]; + const std::uint32_t* out = &Output[tid * Results]; + Expect(tid, out[0], in[0] + 1u, "store before the end"); + Expect(tid, out[1], 0xdeadbeefu, "nothing past the end"); + } +} + +} + +int main() { + try { + const auto device = OpenVulkanTestDevice(); + if (!device) return VulkanTestSkipped; + Run(*device); + Check(); + std::puts("scalar program end tests passed"); + return 0; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; + } +} diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/ScalarProgramEndSaved.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/ScalarProgramEndSaved.cpp new file mode 100644 index 000000000..1f54ffb13 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/ScalarProgramEndSaved.cpp @@ -0,0 +1,96 @@ +#include "prx/libSceAgcDriver/Execution/include/VulkanDevice.hpp" +#include "prx/libSceAgcDriver/Graphics/include/Draw.hpp" +#include "Recompiler.hpp" +#include "VulkanTestDevice.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace { + +using AgcDriver::Graphics::Require; +using ShaderRecompiler::ShaderStage; + +constexpr std::uint32_t Threads = 32; +constexpr std::uint32_t Inputs = 4; +constexpr std::uint32_t Results = 16; +alignas(256) std::array Input{}; +alignas(256) std::array Output{}; + +alignas(256) constexpr std::array Code{ + 0x34020084, 0x34060086, 0xe0301000, 0x80000401, 0xbf8c3f70, 0xbfa40003, 0xf4980000, 0xfa000000, + 0xf49c0000, 0xfa000000, 0xf4a00000, 0xfa000000, 0xf4a40000, 0xfa000000, 0xbfa40000, 0x4a0a0881, + 0x7e0c02ff, 0x00000bad, 0xbf060000, 0xbf840007, 0xbf840003, 0xe0701000, 0x80010503, 0xbf9b0000, + 0xe0701000, 0x80010603, 0xbf9e0000, 0xe0701004, 0x80010603, 0xbf810000, +}; + +void Fill(std::uint32_t tid, std::uint32_t* words) { words[0] = tid * 0x01010101u + 7u; } + +std::array BufferDescriptor(const void* data, std::uint32_t bytes) { + const auto address = reinterpret_cast(data); + return {static_cast(address), static_cast((address >> 32u) & 0xffffu), bytes, 0x01016facu}; +} + +std::string Hex(std::uint32_t value) { + char text[16]; + std::snprintf(text, sizeof(text), "0x%08x", value); + return text; +} + +void Expect(std::uint32_t tid, std::uint32_t actual, std::uint32_t expected, const char* name) { + Require(actual == expected, std::string("scalar program end saved: lane ") + std::to_string(tid) + " " + name + " is " + Hex(actual) + ", expected " + Hex(expected)); +} + +void Run(AgcDriver::VulkanDevice& device) { + for (std::uint32_t tid = 0; tid < Threads; ++tid) Fill(tid, &Input[tid * Inputs]); + Output.fill(0xdeadbeefu); + std::vector userData(8, 0u); + const auto input = BufferDescriptor(Input.data(), static_cast(Input.size() * 4u)); + const auto output = BufferDescriptor(Output.data(), static_cast(Output.size() * 4u)); + std::copy(input.begin(), input.end(), userData.begin()); + std::copy(output.begin(), output.end(), userData.begin() + 4); + const std::span code(Code); + const std::array memory{{{reinterpret_cast(code.data()), std::as_bytes(code)}}}; + const ShaderRecompiler::ShaderComputeStageInfo compute{{Threads, 1, 1}, 0u, {false, false, false}, false, 1}; + ShaderRecompiler::RecompileRequest request{ + {ShaderStage::Compute, reinterpret_cast(code.data()), code, 0, {}}, + {32, 0, userData, compute, std::nullopt, std::nullopt, memory}, + device.Target(), + {0, 0, 0, 128} + }; + request.useCache = false; + const auto result = ShaderRecompiler::Recompile(request); + device.Dispatch(result, 1, 1, 1, {}, reinterpret_cast(code.data())); + device.WaitIdle(); +} + +void Check() { + for (std::uint32_t tid = 0; tid < Threads; ++tid) { + const std::uint32_t* in = &Input[tid * Inputs]; + const std::uint32_t* out = &Output[tid * Results]; + Expect(tid, out[0], in[0] + 1u, "store before the end"); + Expect(tid, out[1], 0xdeadbeefu, "nothing past the end"); + } +} + +} + +int main() { + try { + const auto device = OpenVulkanTestDevice(); + if (!device) return VulkanTestSkipped; + Run(*device); + Check(); + std::puts("scalar program end saved tests passed"); + return 0; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; + } +} diff --git a/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp b/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp index 077686add..a60932b61 100644 --- a/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp +++ b/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp @@ -950,11 +950,16 @@ enum class RdnaOpcode : std::uint16_t { SIncperflevel, SDecperflevel, SWaitIdle, + SRoundMode, SGl1Inv, SDcacheInv, SDcacheWb, SMemtime, SMemrealtime, + SAtcProbe, + SAtcProbeBuffer, + SDcacheDiscard, + SDcacheDiscardX2, BufferGl0Inv, BufferGl1Inv, SDenormMode, diff --git a/core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp b/core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp index 2b5b1000c..52a0b30ba 100644 --- a/core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp +++ b/core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp @@ -563,6 +563,10 @@ RdnaOpcode cacheControlOpcode(RdnaInstructionFamily family, std::uint32_t opcode case 0x1fu: return RdnaOpcode::SGl1Inv; case 0x20u: return RdnaOpcode::SDcacheInv; case 0x21u: return RdnaOpcode::SDcacheWb; + case 0x26u: return RdnaOpcode::SAtcProbe; + case 0x27u: return RdnaOpcode::SAtcProbeBuffer; + case 0x28u: return RdnaOpcode::SDcacheDiscard; + case 0x29u: return RdnaOpcode::SDcacheDiscardX2; default: return RdnaOpcode::Invalid; } } diff --git a/core/shader/recompiler/RdnaDecoder/src/RdnaScalarOpDecoder.cpp b/core/shader/recompiler/RdnaDecoder/src/RdnaScalarOpDecoder.cpp index 04f82bdcc..09ef291c1 100644 --- a/core/shader/recompiler/RdnaDecoder/src/RdnaScalarOpDecoder.cpp +++ b/core/shader/recompiler/RdnaDecoder/src/RdnaScalarOpDecoder.cpp @@ -214,10 +214,13 @@ RdnaOpcode decodeSoppOpcode(std::uint32_t opcode) { case 0x18u: case 0x19u: case 0x1au: return RdnaOpcode::SCbranchCdbg; + case 0x1bu: + case 0x1eu: return RdnaOpcode::SEndpgm; case 0x20u: return RdnaOpcode::SInstPrefetch; case 0x21u: return RdnaOpcode::SClause; case 0x22u: return RdnaOpcode::SWaitIdle; case 0x23u: return RdnaOpcode::SWaitcntDepctr; + case 0x24u: return RdnaOpcode::SRoundMode; case 0x25u: return RdnaOpcode::SDenormMode; case 0x28u: return RdnaOpcode::STtracedata; default: throw std::invalid_argument("unsupported SOPP opcode " + std::to_string(opcode)); @@ -237,7 +240,7 @@ bool isSoppWaitOpcode(RdnaOpcode opcode) { opcode == RdnaOpcode::STrap || opcode == RdnaOpcode::STtracedata || opcode == RdnaOpcode::SInstPrefetch || opcode == RdnaOpcode::SClause || opcode == RdnaOpcode::SCbranchCdbg || opcode == RdnaOpcode::SIcacheInv || opcode == RdnaOpcode::SIncperflevel || opcode == RdnaOpcode::SDecperflevel || opcode == RdnaOpcode::SWaitIdle || - opcode == RdnaOpcode::SDenormMode; + opcode == RdnaOpcode::SRoundMode || opcode == RdnaOpcode::SDenormMode; } std::uint32_t scalarDestinationDwordCount(RdnaOpcode opcode) { diff --git a/core/shader/recompiler/Translation/src/MemoryInstructions/MemoryInstructionDispatch.cpp b/core/shader/recompiler/Translation/src/MemoryInstructions/MemoryInstructionDispatch.cpp index e7f6a8c6e..947df3fa4 100644 --- a/core/shader/recompiler/Translation/src/MemoryInstructions/MemoryInstructionDispatch.cpp +++ b/core/shader/recompiler/Translation/src/MemoryInstructions/MemoryInstructionDispatch.cpp @@ -14,6 +14,10 @@ bool TranslationContext::emitMemory(const RdnaInstruction& inst) { case RdnaOpcode::SGl1Inv: case RdnaOpcode::SDcacheInv: case RdnaOpcode::SDcacheWb: + case RdnaOpcode::SAtcProbe: + case RdnaOpcode::SAtcProbeBuffer: + case RdnaOpcode::SDcacheDiscard: + case RdnaOpcode::SDcacheDiscardX2: case RdnaOpcode::BufferGl0Inv: case RdnaOpcode::BufferGl1Inv: emitControlNop(); diff --git a/core/shader/recompiler/Translation/src/ScalarInstructions.cpp b/core/shader/recompiler/Translation/src/ScalarInstructions.cpp index 757379f4f..1cfa526ee 100644 --- a/core/shader/recompiler/Translation/src/ScalarInstructions.cpp +++ b/core/shader/recompiler/Translation/src/ScalarInstructions.cpp @@ -405,6 +405,7 @@ bool TranslationContext::emitScalar(const RdnaInstruction& inst) { case RdnaOpcode::SIcacheInv: case RdnaOpcode::SIncperflevel: case RdnaOpcode::SDecperflevel: + case RdnaOpcode::SRoundMode: emitControlNop(); return true; case RdnaOpcode::SDenormMode: diff --git a/docs/dev/TechnicalDebt.md b/docs/dev/TechnicalDebt.md index da31aebc2..6bc1649a1 100644 --- a/docs/dev/TechnicalDebt.md +++ b/docs/dev/TechnicalDebt.md @@ -19,6 +19,7 @@ Throughout the project, every function at every stage either **does exactly what - [libSceNpCommerce](../../core/libs/prx/libSceNpCommerce/Export.cpp) - the PS Store icon show/hide calls do nothing - [sceVideoOutOpen](../../core/libs/prx/libSceVideoOut/src/Output.cpp) validates the priority and CPU affinity that the open param requests for the VideoOut service thread but does not apply them: the port's present and vblank threads are host threads, and guest priorities and affinities do not reach host scheduling - The shader recompiler [skips baryctric coordinates](../../core/shader/recompiler/Recompiler.cpp) (is not even passed to SpirvTargetOptions at row 212). +- The shader recompiler [ignores `s_round_mode`](../../core/shader/recompiler/Translation/src/ScalarInstructions.cpp): float operations keep rounding to nearest even after it selects another rounding mode. - [libSceAvPlayer](../../core/libs/prx/libSceAvPlayer/Export.cpp): `sceAvPlayerSetLogCallback` accepts a callback that is never called, as the player produces no log messages, and `sceAvPlayerSetAvailableBandwidth` has no effect, as it governs HLS sources, which `sceAvPlayerAddSource` does not implement. - [sceAvPlayerSetTrickSpeed](../../core/libs/prx/libSceAvPlayer/src/Source.cpp) (libSceAvPlayer) with a negative speed runs the clock backwards but delivers no frames; when a forward speed is set again, playback resumes from the rewound time.