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 <noreply@anthropic.com>
This commit is contained in:
Chad
2026-10-06 15:20:53 -05:00
committed by Adrià Franch
parent 878e6a6b41
commit 5582c993e2
7 changed files with 165 additions and 13 deletions
@@ -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 <algorithm>
#include <array>
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <span>
#include <string>
#include <vector>
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<std::uint32_t, Threads * Inputs> Input{};
alignas(256) std::array<std::uint32_t, Threads * Results> Output{};
alignas(256) constexpr auto Code = std::to_array<std::uint32_t>({
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<std::uint32_t>({0xd9b80101, 0x1208062e, 0xbf810000});
alignas(256) constexpr auto CoincidingOffsetsSt64 = std::to_array<std::uint32_t>({0xd9bc0000, 0x1a08062f, 0xbf810000});
alignas(256) constexpr auto DestinationOverflow = std::to_array<std::uint32_t>({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<std::uint32_t>(seed >> 32u);
}
}
std::array<std::uint32_t, 4> BufferDescriptor(const void* data, std::uint32_t bytes) {
const auto address = reinterpret_cast<std::uintptr_t>(data);
return {static_cast<std::uint32_t>(address), static_cast<std::uint32_t>((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<typename TUse>
void Translate(AgcDriver::VulkanDevice& device, std::span<const std::uint32_t> code, TUse&& use) {
std::vector<std::uint32_t> userData(8, 0u);
const auto input = BufferDescriptor(Input.data(), static_cast<std::uint32_t>(Input.size() * 4u));
const auto output = BufferDescriptor(Output.data(), static_cast<std::uint32_t>(Output.size() * 4u));
std::copy(input.begin(), input.end(), userData.begin());
std::copy(output.begin(), output.end(), userData.begin() + 4);
const std::array<ShaderRecompiler::MemoryRegion, 1> memory{{{reinterpret_cast<std::uintptr_t>(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<std::uintptr_t>(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<std::uintptr_t>(Code.data()));
device.WaitIdle();
});
}
void Reject(AgcDriver::VulkanDevice& device, std::span<const std::uint32_t> 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<const char*, 2> 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;
}
}
@@ -861,6 +861,8 @@ enum class RdnaOpcode : std::uint16_t {
DsWrxchgRtnB32,
DsWrxchg2RtnB32,
DsWrxchg2st64RtnB32,
DsWrxchg2RtnB64,
DsWrxchg2st64RtnB64,
DsMinF32,
DsMaxF32,
DsSwizzleB32,
@@ -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<const std::
if (gds && (info.opcode == RdnaOpcode::DsSwizzleB32 || info.opcode == RdnaOpcode::DsBpermuteB32 || info.opcode == RdnaOpcode::DsPermuteB32 || info.opcode == RdnaOpcode::DsWriteAddtidB32 || info.opcode == RdnaOpcode::DsReadAddtidB32)) {
throw std::runtime_error("DS lane operation is available only for LDS");
}
if (info.opcode == RdnaOpcode::DsWrxchg2RtnB32 || info.opcode == RdnaOpcode::DsWrxchg2st64RtnB32) {
if (info.opcode == RdnaOpcode::DsWrxchg2RtnB32 || info.opcode == RdnaOpcode::DsWrxchg2st64RtnB32 || info.opcode == RdnaOpcode::DsWrxchg2RtnB64 || info.opcode == RdnaOpcode::DsWrxchg2st64RtnB64) {
if (offset0 == offset1) {
throw std::runtime_error("DS write exchange of one location through both offsets is not supported");
}
if (vdst == 255u) {
if (vdst + info.dataDwords > 256u) {
throw std::runtime_error("DS write exchange destination register range overflow");
}
}
@@ -933,10 +937,10 @@ RdnaInstruction DecodeRdnaDs(std::uint32_t programCounter, std::span<const std::
} else if (instruction.op == RdnaOpcode::DsWrite2st64B32 || instruction.op == RdnaOpcode::DsRead2st64B32 || instruction.op == RdnaOpcode::DsWrxchg2st64RtnB32) {
instruction.memoryOffset = offset0 * 256u;
instruction.secondaryOffset = offset1 * 256u;
} else if (instruction.op == RdnaOpcode::DsWrite2B64 || instruction.op == RdnaOpcode::DsRead2B64) {
} else if (instruction.op == RdnaOpcode::DsWrite2B64 || instruction.op == RdnaOpcode::DsRead2B64 || instruction.op == RdnaOpcode::DsWrxchg2RtnB64) {
instruction.memoryOffset = offset0 * 8u;
instruction.secondaryOffset = offset1 * 8u;
} else if (instruction.op == RdnaOpcode::DsWrite2st64B64 || instruction.op == RdnaOpcode::DsRead2st64B64) {
} else if (instruction.op == RdnaOpcode::DsWrite2st64B64 || instruction.op == RdnaOpcode::DsRead2st64B64 || instruction.op == RdnaOpcode::DsWrxchg2st64RtnB64) {
instruction.memoryOffset = offset0 * 512u;
instruction.secondaryOffset = offset1 * 512u;
}
@@ -167,7 +167,9 @@ void includeInstructionVectorRegisters(const RdnaInstruction& instruction, std::
case RdnaOpcode::DsWrite2B32:
case RdnaOpcode::DsWrite2st64B32:
case RdnaOpcode::DsWrite2B64:
case RdnaOpcode::DsWrite2st64B64: {
case RdnaOpcode::DsWrite2st64B64:
case RdnaOpcode::DsWrxchg2RtnB64:
case RdnaOpcode::DsWrxchg2st64RtnB64: {
const std::uint32_t width = std::max(instruction.dataDwordCount / 2u, 1u);
includeVector(instruction.source1, width);
includeVector(instruction.source2, width);
@@ -475,6 +475,8 @@ bool TranslationContext::emitMemory(const RdnaInstruction& inst) {
return dsWrite2(inst);
case RdnaOpcode::DsWrxchg2RtnB32:
case RdnaOpcode::DsWrxchg2st64RtnB32:
case RdnaOpcode::DsWrxchg2RtnB64:
case RdnaOpcode::DsWrxchg2st64RtnB64:
return dsWrxchg2(inst);
case RdnaOpcode::DsWriteB8:
case RdnaOpcode::DsWriteB16:
@@ -44,19 +44,24 @@ bool TranslationContext::dsAtomic(const RdnaInstruction& inst, IrOpcode opcode,
bool TranslationContext::dsWrxchg2(const RdnaInstruction& inst) {
MemoryInfo first = sharedMemoryInfoFromInstruction(inst);
first.dataDwords = 1u;
first.componentCount = 1u;
const std::uint32_t width = first.dataDwords / 2u;
if (width == 2u && inst.gds) {
throw std::runtime_error("64-bit GDS atomics are not supported");
}
first.dataDwords = width;
first.componentCount = width;
MemoryInfo second = first;
second.offset = first.secondaryOffset;
const IrU32 address = readU32(inst.source0);
const IrU32 firstValue = readU32(inst.source1);
const IrU32 secondValue = readU32(inst.source2);
const auto data = [&](const RdnaOperand& operand) { return width == 2u ? &readU64(operand).Value() : &readU32(operand).Value(); };
IrValue* firstValue = data(inst.source1);
IrValue* secondValue = data(inst.source2);
IrValue& active = ir.GetExec();
const IrOpcode opcode = IrOpcode::SharedAtomicSwap32;
IrValue& firstOld = ir.Emit(opcode, IrOpcodeType(opcode), {&address.Value(), &firstValue.Value(), &active}, addMemoryInfo(first, inst.programCounter));
IrValue& secondOld = ir.Emit(opcode, IrOpcodeType(opcode), {&address.Value(), &secondValue.Value(), &active}, addMemoryInfo(second, inst.programCounter));
const IrOpcode opcode = width == 2u ? IrOpcode::SharedAtomicSwap64 : IrOpcode::SharedAtomicSwap32;
IrValue& firstOld = ir.Emit(opcode, IrOpcodeType(opcode), {&address.Value(), firstValue, &active}, addMemoryInfo(first, inst.programCounter));
IrValue& secondOld = ir.Emit(opcode, IrOpcodeType(opcode), {&address.Value(), secondValue, &active}, addMemoryInfo(second, inst.programCounter));
writeOperand(inst.destination, &firstOld);
writeOperand(offsetOperand(inst.destination, 1u), &secondOld);
writeOperand(offsetOperand(inst.destination, width), &secondOld);
return true;
}
+1
View File
@@ -219,6 +219,7 @@ 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.