From 5582c993e2f3cde46d765bf664c53559e97c4f93 Mon Sep 17 00:00:00 2001 From: Chad Date: Tue, 6 Oct 2026 15:20:53 -0500 Subject: [PATCH 1/2] 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 Date: Wed, 7 Oct 2026 11:40:52 +0200 Subject: [PATCH 2/2] docs: describe the ds_wrxchg2 b64 forms in the measured b32 entry Co-Authored-By: Claude --- docs/dev/TechnicalDebt.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/dev/TechnicalDebt.md b/docs/dev/TechnicalDebt.md index 5b5005d6b..ea5145962 100644 --- a/docs/dev/TechnicalDebt.md +++ b/docs/dev/TechnicalDebt.md @@ -192,7 +192,7 @@ Throughout the project, every function at every stage either **does exactly what - [Unnormalized S#s](../../core/shader/recompiler/Optimization/src/DescriptorBindingBuilder.cpp) are accepted only when direct and used only by explicit-LOD `image_sample` without offset, comparison, derivatives or `_a`, on a single-level, single-layer 1D or 2D view of mip 0, even when the image is also sampled through a normalized S#. - An [unnormalized S#](../../core/libs/prx/libSceAgcDriver/Graphics/src/GuestSamplerResource.cpp) (libSceAgcDriver) throws with unequal minification and magnification filters, anisotropy, `TRUNC_COORD`, `MC_COORD_TRUNC` or X/Y clamping other than last-texel or border. - [Image atomics](../../core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp) on 32_SINT and 32_FLOAT surfaces act on the raw dword through an R32_UINT storage view; their float results are assumed to match those measured on 32_UINT surfaces. Other formats throw. -- [`ds_wrxchg2_rtn_b32` and `ds_wrxchg2st64_rtn_b32`](../../core/shader/recompiler/Translation/src/MemoryInstructions/SharedMemoryInstructions.cpp) are translated as two separate 32-bit atomic swaps per invocation. RDNA2 hardware (measured on gfx1035) performs every lane's first exchange before any lane's second one, so when lanes of the same wave or of different waves overlap addresses, the result can differ from hardware. `offset0 == offset1` throws, although on hardware it is two exchanges in order. +- [`ds_wrxchg2_rtn_b32`, `ds_wrxchg2st64_rtn_b32`, `ds_wrxchg2_rtn_b64` and `ds_wrxchg2st64_rtn_b64`](../../core/shader/recompiler/Translation/src/MemoryInstructions/SharedMemoryInstructions.cpp) are translated as two separate 32-bit or 64-bit atomic swaps per invocation. RDNA2 hardware (measured on gfx1035) performs every lane's first exchange before any lane's second one, so when lanes of the same wave or of different waves overlap addresses, the result can differ from hardware. `offset0 == offset1` throws, although on hardware it is two exchanges in order. - [64-bit image atomics](../../core/shader/recompiler/Translation/src/MemoryInstructions/ImageInstructions.cpp) (`image_atomic_*` with a two-dword data mask) need `VK_EXT_shader_image_atomic_int64` and act on 32_32 UINT, SINT and FLOAT surfaces through an R64_UINT storage view. 64-bit `image_atomic_inc`, `image_atomic_dec` and the float atomics throw, as does any other access in the same shader to an image it updates with 64-bit atomics. - The shader recompiler [ignores `s_setreg_b32`](../../core/shader/recompiler/Translation/src/ScalarInstructions.cpp): writes to MODE (float rounding and denormal controls) and the other hardware registers have no effect, and `s_getreg_b32` reads of the MODE round fields report round to nearest even even after `s_setreg_b32` wrote another mode. The initial round mode from the shader's program registers is not read either. - On a host subgroup narrower than 32 lanes, [`threadBit`](../../core/shader/recompiler/Translation/src/TranslationContext/ExecMask.cpp) reads an EXEC written as a value (`s_mov_b32`/`s_mov_b64`) at bit (subgroup invocation id) of EXEC_LO, so lanes at or above the subgroup size read a lower lane's bit. @@ -219,7 +219,6 @@ Throughout the project, every function at every stage either **does exactly what - [sceImeKeyboardGetInfo](../../core/libs/prx/libSceIme/Export.cpp) (libSceIme) returns `0x80bc0023` (no resource id) for every id while a keyboard is open, since the IME keyboard never reports a connected device. [sceImeKeyboardSetMode](../../core/libs/prx/libSceIme/Export.cpp) validates the mode bits but the mode has no effect, as no IME keyboard events are delivered. - [libSceAudiodec](../../core/libs/prx/libSceAudiodec/Export.cpp) throws for the 24-bit PCM word size (`iBwPcm` 0): its sample layout is unknown. ATRAC9 decoding is covered only by configuration and error tests, as no ATRAC9 encoder is available for a fixture. - [sceAjmDecMp3ParseFrame](../../core/libs/prx/libSceAjm.native/src/Ajm.cpp) (libSceAjm.native) throws when the original file length is requested (`parse_ofl` non-zero) for a layer other than III, for a CRC-protected frame, or for a VBRI header outside MPEG-1 stereo: shadPS4 looks for the tag after the CRC and after the side information, while LAME and FFmpeg place it without the CRC and VBRI always at byte 36. -- [ds_wrxchg2(st64)_rtn_b64](../../core/shader/recompiler/Translation/src/MemoryInstructions/SharedMemoryInstructions.cpp) exchange the two qwords as two separate 64-bit LDS exchanges, first then second: another lane can observe the first address written before the second. Whether the hardware exchanges the pair atomically is not documented. Equal offsets throw, as for the b32 forms - The [output modifier](../../core/shader/recompiler/Translation/src/TranslationContext/OperandAccess.cpp) (`mul:2`, `mul:4`, `div:2`) follows the hardware with the IEEE mode off and f32 denormals flushed, the LLVM graphics default; the shader's MODE register is not read, and the hardware ignores the modifier in the other modes. On f16 results it is ignored, as on the hardware while f16 denormals are enabled; with them flushed the hardware applies it. - [Comparison sampling of a color texture](../../core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp) (`image_sample_c`/`image_sample_c_lz` on a texture that is not R32 float or R16 unorm) is emulated in the shader, since Vulkan compares only depth formats: the red channel is fetched, compared and, for bilinear filtering, blended (compare, then filter). Only 2D and 2D array views, float/unorm/snorm formats, wrap, clamp-to-edge and clamp-to-border addressing (border red 0 for the black borders, 1 for opaque white) and point or bilinear filtering at one level are implemented; half-border addressing, the border color table, gradients, LOD bias, offsets, LOD clamps, gathers, other dimensions, anisotropy and mip filtering throw. Under bilinear filtering, taps past a clamp-to-border edge compare the border value and blend with the texel taps, so results near the edge depend on the filter weights. AMD documentation does not say how a color format's reference is handled: it is clamped to [0, 1] for unorm and [-1, 1] for snorm, as Vulkan does for unorm depth, and not clamped for float. Bilinear weights are full float, not the hardware's fixed-point subtexel weights - [libScePlayerInvitationDialog](../../core/libs/prx/libScePlayerInvitationDialog/libScePlayerInvitationDialog.cpp) simulates dialog completion without displaying UI or sending invitations, so `scePlayerInvitationDialogGetResult` reports `USER_CANCELED`; its parameter ABI remains unverified. The 40-byte result layout (error code, result, 32 reserved bytes) is taken from [SharpProspero](https://github.com/SvenGDK/SharpProspero/blob/main/src/SharpProspero/Interop/Dialog/PlayerInvitationDialog.cs) and the error codes from the other common dialogs, not confirmed on a title.