From 9f2319b46eda1cecff980eb8472aedcc2d70dfa9 Mon Sep 17 00:00:00 2001 From: DotDebian Date: Fri, 2 Oct 2026 18:25:22 +0200 Subject: [PATCH] feat(shader): implement v_sad_u8, v_sad_hi_u8, v_sad_u16 and v_msad_u8 Decode VOP3 0x15a, 0x15b, 0x15c and 0x171 and translate them as the unsigned sum of absolute differences over the byte or word fields of the first two sources, accumulated into the third. v_sad_hi_u8 shifts the sum into the high half first, v_msad_u8 skips the fields whose reference byte in the second source is zero. Co-Authored-By: Claude Opus 5.5 --- .../tests/execution/Vop3Sad.cpp | 164 ++++++++++++++++++ .../include/RdnaDecoder/RdnaOpcode.hpp | 4 + .../recompiler/RdnaDecoder/src/RdnaOpcode.cpp | 4 + .../RdnaDecoder/src/RdnaVectorOpDecoder.cpp | 4 + .../Translation/TranslationContext.hpp | 1 + .../Translation/src/IntegerInstructions.cpp | 25 +++ .../Translation/src/VectorInstructions.cpp | 8 + 7 files changed, 210 insertions(+) create mode 100644 core/libs/prx/libSceAgcDriver/tests/execution/Vop3Sad.cpp diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/Vop3Sad.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/Vop3Sad.cpp new file mode 100644 index 000000000..00055340c --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/Vop3Sad.cpp @@ -0,0 +1,164 @@ +#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 + +namespace { + +using AgcDriver::Graphics::Require; +using ShaderRecompiler::ShaderStage; + +constexpr std::uint32_t Threads = 64; +constexpr std::uint32_t Inputs = 4; +constexpr std::uint32_t Results = 16; +constexpr std::uint32_t Literal = 0x8000ffffu; +alignas(256) std::array Input{}; +alignas(256) std::array Output{}; + +alignas(256) constexpr std::array SadCode{ + 0x34020082, 0x34060084, 0xe0302000, 0x80000401, 0xe0302004, 0x80000501, 0xe0302008, 0x80000601, + 0xbf8c3f70, 0xd55a000a, 0x041a0b04, 0xd55b000b, 0x041a0b04, 0xd55c000c, 0x041a0b04, 0xd571000d, + 0x041a0b04, 0xd55d000e, 0x041a0b04, 0xd55a000f, 0x04190104, 0xd55a0010, 0x041a0ac1, 0xd55b0011, + 0x042a0905, 0xd55c0012, 0x0419ff04, Literal, 0xd5710013, 0x041a0905, 0xe0702000, 0x80010a03, + 0xe0702004, 0x80010b03, 0xe0702008, 0x80010c03, 0xe070200c, 0x80010d03, 0xe0702010, 0x80010e03, + 0xe0702014, 0x80010f03, 0xe0702018, 0x80011003, 0xe070201c, 0x80011103, 0xe0702020, 0x80011203, + 0xe0702024, 0x80011303, 0xbf810000, +}; + +constexpr std::array, 12> Edges{{ + {0xffffffffu, 0u, 0u}, {0u, 0xffffffffu, 0xffffffffu}, {0xff00ff00u, 0x00ff00ffu, 0xfffffc04u}, {0x80000000u, 0x7fffffffu, 1u}, + {0x7f80ff00u, 0x807f00ffu, 0xffff0000u}, {0x01020304u, 0x04030201u, 0x10u}, {0x12345678u, 0x12345678u, 0x9abcdef0u}, + {0x80808080u, 0x7f7f7f7fu, 0u}, {0x8000ffffu, 0x7fff0000u, 0xfffe0002u}, {0x00ff0001u, 0x000000ffu, 0x7fffffffu}, + {0xffffffffu, 0x00ff0000u, 0x03fcu}, {0u, 0u, 0u}, +}}; + +void FillInput() { + std::uint64_t state = 0x9e3779b97f4a7c15ull; + for (std::uint32_t tid = 0; tid < Threads; ++tid) { + auto* words = &Input[tid * Inputs]; + for (std::uint32_t j = 0; j < 3u; ++j) { + state = state * 6364136223846793005ull + 1442695040888963407ull; + words[j] = tid < Edges.size() ? Edges[tid][j] : static_cast(state >> 32u); + } + if (tid >= Edges.size() && (tid & 1u) != 0u) { + words[1] &= (tid & 2u) != 0u ? 0x00ffff00u : 0xff0000ffu; + } + } +} + +std::array BufferDescriptor(const void* data, std::uint32_t count) { + const auto address = reinterpret_cast(data); + return {static_cast(address), static_cast((address >> 32u) & 0xffffu) | (4u << 16u), count, 0x01016facu}; +} + +std::string Hex(std::uint32_t value) { + char text[16]; + std::snprintf(text, sizeof(text), "0x%08x", value); + return text; +} + +auto Compile(AgcDriver::VulkanDevice& device, std::span code) { + std::vector userData(8, 0u); + const auto input = BufferDescriptor(Input.data(), static_cast(Input.size())); + const auto output = BufferDescriptor(Output.data(), static_cast(Output.size())); + 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}, 0, {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; + return ShaderRecompiler::Recompile(request); +} + +void Run(AgcDriver::VulkanDevice& device) { + Output.fill(0xdeadbeefu); + const std::span code(SadCode); + const auto result = Compile(device, code); + device.Dispatch(result, 1, 1, 1, {}, reinterpret_cast(code.data())); + device.WaitIdle(); +} + +void CheckClampRefused(AgcDriver::VulkanDevice& device) { + for (const std::uint32_t opcode : {0x15au, 0x15bu, 0x15cu, 0x171u}) { + alignas(256) const std::array code{0xd400800au | (opcode << 16u), 0x041a0b04u, 0xbf810000u}; + std::string refusal; + try { + static_cast(Compile(device, code)); + } catch (const std::exception& error) { + refusal = error.what(); + } + Require(refusal.find("VOP3 source modifiers are not implemented") != std::string::npos, "VOP3 opcode " + Hex(opcode) + " with clamp was not refused"); + } +} + +std::uint32_t Sad(std::uint32_t lhs, std::uint32_t rhs, std::uint32_t fieldBits, bool masked) { + const std::uint32_t fieldMask = (1u << fieldBits) - 1u; + std::uint32_t sum = 0u; + for (std::uint32_t offset = 0; offset < 32u; offset += fieldBits) { + const std::uint32_t left = (lhs >> offset) & fieldMask; + const std::uint32_t right = (rhs >> offset) & fieldMask; + if (masked && right == 0u) continue; + sum += left > right ? left - right : right - left; + } + return sum; +} + +void Check() { + constexpr std::array names{ + "v_sad_u8", "v_sad_hi_u8", "v_sad_u16", "v_msad_u8", "v_sad_u32", "v_sad_u8 with a zero reference", "v_sad_u8 with an inline constant", + "v_sad_hi_u8 accumulating into a v_sad_u8 result", "v_sad_u16 with a literal", "v_msad_u8 with swapped sources", + }; + for (std::uint32_t tid = 0; tid < Threads; ++tid) { + const auto* in = &Input[tid * Inputs]; + const std::uint32_t a = in[0]; + const std::uint32_t b = in[1]; + const std::uint32_t c = in[2]; + const std::uint32_t bytes = Sad(a, b, 8u, false) + c; + const std::array expected{ + bytes, + (Sad(a, b, 8u, false) << 16u) + c, + Sad(a, b, 16u, false) + c, + Sad(a, b, 8u, true) + c, + (a > b ? a - b : b - a) + c, + Sad(a, 0u, 8u, false) + c, + Sad(0xffffffffu, b, 8u, false) + c, + (Sad(b, a, 8u, false) << 16u) + bytes, + Sad(a, Literal, 16u, false) + c, + Sad(b, a, 8u, true) + c, + }; + for (std::uint32_t j = 0; j < expected.size(); ++j) { + const std::uint32_t actual = Output[tid * Results + j]; + Require(actual == expected[j], std::string(names[j]) + ": thread " + std::to_string(tid) + " (" + Hex(a) + ", " + Hex(b) + ", " + Hex(c) + ") is " + Hex(actual) + ", expected " + Hex(expected[j])); + } + } +} + +} + +int main() { + try { + const auto device = OpenVulkanTestDevice(); + if (!device) return VulkanTestSkipped; + FillInput(); + Run(*device); + Check(); + CheckClampRefused(*device); + std::puts("vop3 sad 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 a1e040990..5215f5119 100644 --- a/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp +++ b/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp @@ -57,6 +57,10 @@ enum class RdnaOpcode : std::uint16_t { VMulHiU32, VMadU64U32, VSadU32, + VSadU8, + VSadHiU8, + VSadU16, + VMsadU8, VAndB32, VOrB32, VXorB32, diff --git a/core/shader/recompiler/RdnaDecoder/src/RdnaOpcode.cpp b/core/shader/recompiler/RdnaDecoder/src/RdnaOpcode.cpp index fcf4df05b..26f0e43d0 100644 --- a/core/shader/recompiler/RdnaDecoder/src/RdnaOpcode.cpp +++ b/core/shader/recompiler/RdnaDecoder/src/RdnaOpcode.cpp @@ -158,6 +158,10 @@ bool IsVectorAluOpcode(RdnaOpcode opcode) { case RdnaOpcode::VMulHiU32: case RdnaOpcode::VMadU64U32: case RdnaOpcode::VSadU32: + case RdnaOpcode::VSadU8: + case RdnaOpcode::VSadHiU8: + case RdnaOpcode::VSadU16: + case RdnaOpcode::VMsadU8: case RdnaOpcode::VAndB32: case RdnaOpcode::VOrB32: case RdnaOpcode::VXorB32: diff --git a/core/shader/recompiler/RdnaDecoder/src/RdnaVectorOpDecoder.cpp b/core/shader/recompiler/RdnaDecoder/src/RdnaVectorOpDecoder.cpp index 5b664ec8b..0f76dc907 100644 --- a/core/shader/recompiler/RdnaDecoder/src/RdnaVectorOpDecoder.cpp +++ b/core/shader/recompiler/RdnaDecoder/src/RdnaVectorOpDecoder.cpp @@ -403,7 +403,11 @@ constexpr VectorOpcodeInfo vop3Opcodes[] = { {0x357u, RdnaOpcode::VMed3F16}, {0x358u, RdnaOpcode::VMed3I16}, {0x359u, RdnaOpcode::VMed3U16}, + {0x15au, RdnaOpcode::VSadU8}, + {0x15bu, RdnaOpcode::VSadHiU8}, + {0x15cu, RdnaOpcode::VSadU16}, {0x15du, RdnaOpcode::VSadU32}, + {0x171u, RdnaOpcode::VMsadU8}, {0x15eu, RdnaOpcode::VCvtPkU8F32}, {0x178u, RdnaOpcode::VXor3B32}, {0x12fu, RdnaOpcode::VCvtPkrtzF16F32}, diff --git a/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp b/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp index 84096a2d8..4609da6f9 100644 --- a/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp +++ b/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp @@ -175,6 +175,7 @@ private: bool integer24(const RdnaInstruction& inst, bool sign, bool addend, bool high = false); bool vMadU64U32(const RdnaInstruction& inst); bool vSadU32(const RdnaInstruction& inst); + bool subwordSad(const RdnaInstruction& inst, std::uint32_t fieldBits, std::uint32_t sumShift, bool masked); bool vAdd3U32(const RdnaInstruction& inst); bool sBitsetB32(const RdnaInstruction& inst, bool set); bool sBitsetB64(const RdnaInstruction& inst, bool set); diff --git a/core/shader/recompiler/Translation/src/IntegerInstructions.cpp b/core/shader/recompiler/Translation/src/IntegerInstructions.cpp index a02a6082a..7ef88fbb2 100644 --- a/core/shader/recompiler/Translation/src/IntegerInstructions.cpp +++ b/core/shader/recompiler/Translation/src/IntegerInstructions.cpp @@ -336,6 +336,31 @@ bool TranslationContext::vSadU32(const RdnaInstruction& inst) { return true; } +bool TranslationContext::subwordSad(const RdnaInstruction& inst, std::uint32_t fieldBits, std::uint32_t sumShift, bool masked) { + const IrU32 lhs = readU32(sourceAt(inst, 0u)); + const IrU32 rhs = readU32(sourceAt(inst, 1u)); + IrU32 sum(ir.Constant(0u)); + for (std::uint32_t offset = 0u; offset < 32u; offset += fieldBits) { + const IrU32 lhsField(ir.Emit(IrOpcode::BitFieldUExtract, IrType::U32, {&lhs.Value(), &ir.Constant(offset), &ir.Constant(fieldBits)})); + const IrU32 rhsField(ir.Emit(IrOpcode::BitFieldUExtract, IrType::U32, {&rhs.Value(), &ir.Constant(offset), &ir.Constant(fieldBits)})); + const IrU32 lo(ir.Emit(IrOpcode::UMin32, IrType::U32, {&lhsField.Value(), &rhsField.Value()})); + const IrU32 hi(ir.Emit(IrOpcode::UMax32, IrType::U32, {&lhsField.Value(), &rhsField.Value()})); + IrU32 difference(ir.ISub(hi.Value(), lo.Value())); + if (masked) { + const IrU1 reference(ir.INotEqual(rhsField.Value(), ir.Constant(0u))); + difference = IrU32(ir.Select(reference.Value(), difference.Value(), ir.Constant(0u))); + } + sum = IrU32(ir.IAdd(sum.Value(), difference.Value())); + } + if (sumShift != 0u) { + sum = IrU32(ir.ShiftLeftLogical(sum.Value(), ir.Constant(sumShift))); + } + const IrU32 addend = readU32(sourceAt(inst, 2u)); + const IrU32 result(ir.IAdd(sum.Value(), addend.Value())); + writeOperand(inst.destination, &result.Value()); + return true; +} + bool TranslationContext::vAdd3U32(const RdnaInstruction& inst) { const IrU32 lhs = readU32(sourceAt(inst, 0u)); const IrU32 rhs = readU32(sourceAt(inst, 1u)); diff --git a/core/shader/recompiler/Translation/src/VectorInstructions.cpp b/core/shader/recompiler/Translation/src/VectorInstructions.cpp index 9b6710ea9..80b5f6e3d 100644 --- a/core/shader/recompiler/Translation/src/VectorInstructions.cpp +++ b/core/shader/recompiler/Translation/src/VectorInstructions.cpp @@ -859,6 +859,14 @@ bool TranslationContext::emitVector(const RdnaInstruction& inst) { return vMadU64U32(inst); case RdnaOpcode::VSadU32: return vSadU32(inst); + case RdnaOpcode::VSadU8: + return subwordSad(inst, 8u, 0u, false); + case RdnaOpcode::VSadHiU8: + return subwordSad(inst, 8u, 16u, false); + case RdnaOpcode::VSadU16: + return subwordSad(inst, 16u, 0u, false); + case RdnaOpcode::VMsadU8: + return subwordSad(inst, 8u, 0u, true); case RdnaOpcode::VAdd3U32: return vAdd3U32(inst); case RdnaOpcode::VBcntU32B32: