From 5582c993e2f3cde46d765bf664c53559e97c4f93 Mon Sep 17 00:00:00 2001 From: Chad Date: Tue, 6 Oct 2026 15:20:53 -0500 Subject: [PATCH] feat(shader): implement ds_wrxchg2_rtn_b64 and ds_wrxchg2st64_rtn_b64 DS opcodes 0x6e and 0x6f exchange two qwords at offset0 and offset1 scaled by 8 (st64: 512) and return both old values. Each address is exchanged with its own 64-bit LDS exchange on the locked 64-bit path. Equal offsets and a destination past v255 throw, as for the b32 forms; 64-bit GDS throws like the other 64-bit DS atomics. Co-Authored-By: Claude Opus 5.5 --- .../tests/execution/DsWrxchg2B64.cpp | 136 ++++++++++++++++++ .../include/RdnaDecoder/RdnaOpcode.hpp | 2 + .../RdnaDecoder/src/RdnaMemoryOpDecoder.cpp | 12 +- .../Translation/src/InstructionTranslator.cpp | 4 +- .../MemoryInstructionDispatch.cpp | 2 + .../SharedMemoryInstructions.cpp | 21 +-- docs/dev/TechnicalDebt.md | 1 + 7 files changed, 165 insertions(+), 13 deletions(-) create mode 100644 core/libs/prx/libSceAgcDriver/tests/execution/DsWrxchg2B64.cpp diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/DsWrxchg2B64.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/DsWrxchg2B64.cpp new file mode 100644 index 000000000..b07927ea4 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/DsWrxchg2B64.cpp @@ -0,0 +1,136 @@ +#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 + +namespace { + +using AgcDriver::Graphics::Require; +using ShaderRecompiler::ShaderStage; + +constexpr std::uint32_t Threads = 32; +constexpr std::uint32_t Inputs = 8; +constexpr std::uint32_t Results = 16; +constexpr std::uint32_t LdsDwords = 3072; +alignas(256) std::array Input{}; +alignas(256) std::array Output{}; + +alignas(256) constexpr auto Code = std::to_array({ + 0x34020085, 0xe0381000, 0x80000201, 0xe0381010, 0x80000601, + 0x345c0085, 0x4a5c5cff, 0x00000480, 0x345e0083, 0x4a5e5eff, 0x00000880, + 0xbf8c3f70, + 0xd9380301, 0x0004022e, 0xd9b80301, 0x1208062e, 0xd9dc0301, 0x1600002e, + 0xd93c0100, 0x0004022f, 0xd9bc0100, 0x1a08062f, 0xd9e00100, 0x1e00002f, + 0x34020086, + 0xbf8cc07f, + 0xe0781000, 0x80011201, 0xe0781010, 0x80011601, 0xe0781020, 0x80011a01, 0xe0781030, 0x80011e01, + 0xbf810000, +}); + +alignas(256) constexpr auto CoincidingOffsets = std::to_array({0xd9b80101, 0x1208062e, 0xbf810000}); +alignas(256) constexpr auto CoincidingOffsetsSt64 = std::to_array({0xd9bc0000, 0x1a08062f, 0xbf810000}); +alignas(256) constexpr auto DestinationOverflow = std::to_array({0xd9b80301, 0xfd08062e, 0xbf810000}); + +void Fill(std::uint32_t tid, std::uint32_t* words) { + std::uint64_t seed = (tid + 1u) * 0x9e3779b97f4a7c15ull; + for (std::uint32_t i = 0; i < Inputs; ++i) { + seed = seed * 0xbf58476d1ce4e5b9ull + 0x94d049bb133111ebull; + words[i] = static_cast(seed >> 32u); + } +} + +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; +} + +template +void Translate(AgcDriver::VulkanDevice& device, std::span code, TUse&& use) { + 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::array memory{{{reinterpret_cast(code.data()), std::as_bytes(code)}}}; + const ShaderRecompiler::ShaderComputeStageInfo compute{{Threads, 1, 1}, LdsDwords, {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; + use(ShaderRecompiler::Recompile(request)); +} + +void Run(AgcDriver::VulkanDevice& device) { + for (std::uint32_t tid = 0; tid < Threads; ++tid) Fill(tid, &Input[tid * Inputs]); + Output.fill(0xdeadbeefu); + Translate(device, Code, [&](const auto& result) { + device.Dispatch(result, 1, 1, 1, {}, reinterpret_cast(Code.data())); + device.WaitIdle(); + }); +} + +void Reject(AgcDriver::VulkanDevice& device, std::span code, const std::string& reason) { + std::string error; + try { + Translate(device, code, [](const auto&) {}); + } catch (const std::exception& exception) { + error = exception.what(); + } + Require(error.find(reason) != std::string::npos, "ds wrxchg2 b64: expected a rejection for \"" + reason + "\", got \"" + error + "\""); +} + +void CheckRejections(AgcDriver::VulkanDevice& device) { + Reject(device, CoincidingOffsets, "DS write exchange of one location through both offsets is not supported"); + Reject(device, CoincidingOffsetsSt64, "DS write exchange of one location through both offsets is not supported"); + Reject(device, DestinationOverflow, "DS write exchange destination register range overflow"); +} + +void Check() { + constexpr std::array names{"ds_wrxchg2_rtn_b64", "ds_wrxchg2st64_rtn_b64"}; + 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]; + for (std::uint32_t op = 0; op < 2u; ++op) { + for (std::uint32_t i = 0; i < 4u; ++i) { + const auto returned = out[op * 8u + i]; + const auto stored = out[op * 8u + 4u + i]; + Require(returned == in[i], std::string("ds wrxchg2 b64: lane ") + std::to_string(tid) + " " + names[op] + " returned dword " + std::to_string(i) + " is " + Hex(returned) + ", expected " + Hex(in[i])); + Require(stored == in[4u + i], std::string("ds wrxchg2 b64: lane ") + std::to_string(tid) + " " + names[op] + " stored dword " + std::to_string(i) + " is " + Hex(stored) + ", expected " + Hex(in[4u + i])); + } + } + } +} + +} + +int main() { + try { + const auto device = OpenVulkanTestDevice(); + if (!device) return VulkanTestSkipped; + Run(*device); + Check(); + CheckRejections(*device); + std::puts("ds wrxchg2 b64 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 5899f0335..9cc00be2f 100644 --- a/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp +++ b/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp @@ -861,6 +861,8 @@ enum class RdnaOpcode : std::uint16_t { DsWrxchgRtnB32, DsWrxchg2RtnB32, DsWrxchg2st64RtnB32, + DsWrxchg2RtnB64, + DsWrxchg2st64RtnB64, DsMinF32, DsMaxF32, DsSwizzleB32, diff --git a/core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp b/core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp index f8c180d05..3f155040a 100644 --- a/core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp +++ b/core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp @@ -319,6 +319,8 @@ constexpr MemoryOpcodeInfo dsOpcodes[] = { {0x4du, RdnaOpcode::DsWriteB64, 2, 32, false, false, false}, {0x4eu, RdnaOpcode::DsWrite2B64, 4, 32, false, false, false}, {0x4fu, RdnaOpcode::DsWrite2st64B64, 4, 32, false, false, false}, + {0x6eu, RdnaOpcode::DsWrxchg2RtnB64, 4, 32, false, false, false}, + {0x6fu, RdnaOpcode::DsWrxchg2st64RtnB64, 4, 32, false, false, false}, {0x76u, RdnaOpcode::DsReadB64, 2, 32, false, false, false}, {0x77u, RdnaOpcode::DsRead2B64, 4, 32, false, false, false}, {0x78u, RdnaOpcode::DsRead2st64B64, 4, 32, false, false, false}, @@ -535,6 +537,8 @@ std::uint32_t dsSourceCount(RdnaOpcode opcode) { switch (opcode) { case RdnaOpcode::DsWrxchg2RtnB32: case RdnaOpcode::DsWrxchg2st64RtnB32: + case RdnaOpcode::DsWrxchg2RtnB64: + case RdnaOpcode::DsWrxchg2st64RtnB64: case RdnaOpcode::DsWrite2B32: case RdnaOpcode::DsWrite2st64B32: case RdnaOpcode::DsWrite2B64: @@ -903,11 +907,11 @@ RdnaInstruction DecodeRdnaDs(std::uint32_t programCounter, std::span 256u) { throw std::runtime_error("DS write exchange destination register range overflow"); } } @@ -933,10 +937,10 @@ RdnaInstruction DecodeRdnaDs(std::uint32_t programCounter, std::span