From e144f43f05543805f4ced8d2a98660cacd6671f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Franch?= Date: Thu, 1 Oct 2026 22:08:42 +0200 Subject: [PATCH] feat(shader): implement VOP3 v_add/sub_nc_i32 with clamp, v_mul_lo_u16, v_mad_u16/i16/u32_u16/i32_i16, v_ashrrev_i64 These VOP3 opcodes threw in the decoder. v_add_nc_i32 and v_sub_nc_i32 are what compilers emit for signed saturating add/sub, so their clamp saturates to the int32 range. The 16-bit mads honour op_sel on their 16-bit sources; the u32_u16/i32_i16 forms take a 32-bit addend and write all 32 bits, so they do not get the 16-bit destination select. Co-Authored-By: Claude --- .../tests/execution/Vop3Integer.cpp | 145 ++++++++++++++++++ .../include/RdnaDecoder/RdnaOpcode.hpp | 8 + .../recompiler/RdnaDecoder/src/RdnaOpcode.cpp | 8 + .../RdnaDecoder/src/RdnaVectorOpDecoder.cpp | 35 ++++- .../Translation/TranslationContext.hpp | 2 + .../Translation/src/IntegerInstructions.cpp | 28 ++++ .../Translation/src/VectorInstructions.cpp | 16 ++ 7 files changed, 238 insertions(+), 4 deletions(-) create mode 100644 core/libs/prx/libSceAgcDriver/tests/execution/Vop3Integer.cpp diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/Vop3Integer.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/Vop3Integer.cpp new file mode 100644 index 000000000..355272eaa --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/Vop3Integer.cpp @@ -0,0 +1,145 @@ +#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 = 64; +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 IntegerCode{ + 0x34020082, 0x34060084, 0xe0302000, 0x80000401, 0xe0302004, 0x80000501, 0xe0302008, 0x80000601, + 0x7e1a02ff, 0xabcd0000, 0x7e1c02ff, 0xabcd0000, 0x7e1e02ff, 0xabcd0000, 0x7e2a02ff, 0xabcd0000, + 0x7e2c02ff, 0xabcd0000, 0xbf8c3f70, 0xd77f000a, 0x00020b04, 0xd77f800b, 0x00020b04, 0xd776800c, + 0x00020b04, 0xd705000d, 0x00020b04, 0xd740000e, 0x041a0b04, 0xd75e000f, 0x041a0b04, 0xd7730010, + 0x041a0b04, 0xd7750011, 0x041a0b04, 0xd7010012, 0x00020906, 0xd7731814, 0x041a0b04, 0xd7402815, + 0x041a0b04, 0xd7051016, 0x00020b04, 0xd7760017, 0x00020b04, 0xe0702000, 0x80010a03, 0xe0702004, + 0x80010b03, 0xe0702008, 0x80010c03, 0xe070200c, 0x80010d03, 0xe0702010, 0x80010e03, 0xe0702014, + 0x80010f03, 0xe0702018, 0x80011003, 0xe070201c, 0x80011103, 0xe0702020, 0x80011203, 0xe0702024, + 0x80011303, 0xe0702028, 0x80011403, 0xe070202c, 0x80011503, 0xe0702030, 0x80011603, 0xe0702034, + 0x80011703, 0xbf810000, +}; + +constexpr std::array, 10> Edges{{ + {0x7fffffffu, 1u, 0u}, {0x80000000u, 1u, 63u}, {0x80000000u, 0xffffffffu, 32u}, {0x7fffffffu, 0x80000000u, 31u}, + {0xffffffffu, 0xffffffffu, 0xffffu}, {0x8000ffffu, 0x7fff8000u, 0x12345678u}, {0x40000000u, 0x40000000u, 1u}, + {0xc0000000u, 0x40000001u, 64u}, {0xffff0001u, 0x0001ffffu, 0x8000ffffu}, {0u, 0x80000000u, 0x7fff7fffu}, +}}; + +void FillInput() { + std::uint64_t state = 0x2545f4914f6cdd1dull; + 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); + } + } +} + +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}; +} + +void Run(AgcDriver::VulkanDevice& device) { + Output.fill(0xdeadbeefu); + 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::span code(IntegerCode); + 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; + const auto result = ShaderRecompiler::Recompile(request); + device.Dispatch(result, 1, 1, 1, {}, reinterpret_cast(code.data())); + device.WaitIdle(); +} + +std::uint32_t Saturate(std::int64_t value) { + constexpr std::int64_t low = std::numeric_limits::min(); + constexpr std::int64_t high = std::numeric_limits::max(); + return static_cast(std::clamp(value, low, high)); +} + +std::uint32_t Signed16(std::uint32_t value) { + return static_cast(static_cast(static_cast(value))); +} + +void Check() { + 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 al = a & 0xffffu; + const std::uint32_t ah = a >> 16u; + const std::uint32_t bl = b & 0xffffu; + const std::uint32_t bh = b >> 16u; + const std::int64_t sa = static_cast(a); + const std::int64_t sb = static_cast(b); + const auto shifted = static_cast(static_cast(a | (static_cast(b) << 32u)) >> (c & 63u)); + const std::array expected{ + a + b, + Saturate(sa + sb), + Saturate(sa - sb), + (al * bl) & 0xffffu, + (al * bl + (c & 0xffffu)) & 0xffffu, + (Signed16(a) * Signed16(b) + Signed16(c)) & 0xffffu, + al * bl + c, + Signed16(a) * Signed16(b) + c, + static_cast(shifted), + static_cast(shifted >> 32u), + ah * bh + c, + (ah * bl + (c >> 16u)) & 0xffffu, + (al * bh) & 0xffffu, + a - b, + }; + constexpr std::uint32_t halfResults = (1u << 3u) | (1u << 4u) | (1u << 5u) | (1u << 11u) | (1u << 12u); + for (std::uint32_t j = 0; j < expected.size(); ++j) { + const std::uint32_t mask = ((halfResults >> j) & 1u) != 0u ? 0xffffu : 0xffffffffu; + const std::uint32_t actual = Output[tid * Results + j] & mask; + Require(actual == expected[j], "vop3 integer: thread " + std::to_string(tid) + " result " + std::to_string(j) + " is " + std::to_string(actual) + ", expected " + std::to_string(expected[j])); + } + } +} + +} + +int main() { + try { + const auto device = OpenVulkanTestDevice(); + if (!device) return VulkanTestSkipped; + FillInput(); + Run(*device); + Check(); + std::puts("vop3 integer 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 52440fadb..551270965 100644 --- a/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp +++ b/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp @@ -348,6 +348,14 @@ enum class RdnaOpcode : std::uint16_t { VAshrrevI32, VLshlrevB64, VLshrrevB64, + VAshrrevI64, + VAddNcI32, + VSubNcI32, + VMulLoU16, + VMadU16, + VMadI16, + VMadU32U16, + VMadI32I16, VLshlrevB16, VLshrrevB16, VAshrrevI16, diff --git a/core/shader/recompiler/RdnaDecoder/src/RdnaOpcode.cpp b/core/shader/recompiler/RdnaDecoder/src/RdnaOpcode.cpp index 032c76070..4ac452f06 100644 --- a/core/shader/recompiler/RdnaDecoder/src/RdnaOpcode.cpp +++ b/core/shader/recompiler/RdnaDecoder/src/RdnaOpcode.cpp @@ -323,6 +323,14 @@ bool IsVectorAluOpcode(RdnaOpcode opcode) { case RdnaOpcode::VAshrrevI32: case RdnaOpcode::VLshlrevB64: case RdnaOpcode::VLshrrevB64: + case RdnaOpcode::VAshrrevI64: + case RdnaOpcode::VAddNcI32: + case RdnaOpcode::VSubNcI32: + case RdnaOpcode::VMulLoU16: + case RdnaOpcode::VMadU16: + case RdnaOpcode::VMadI16: + case RdnaOpcode::VMadU32U16: + case RdnaOpcode::VMadI32I16: case RdnaOpcode::VLshlrevB16: case RdnaOpcode::VLshrrevB16: case RdnaOpcode::VAshrrevI16: diff --git a/core/shader/recompiler/RdnaDecoder/src/RdnaVectorOpDecoder.cpp b/core/shader/recompiler/RdnaDecoder/src/RdnaVectorOpDecoder.cpp index a4d6ec2bc..b1d646901 100644 --- a/core/shader/recompiler/RdnaDecoder/src/RdnaVectorOpDecoder.cpp +++ b/core/shader/recompiler/RdnaDecoder/src/RdnaVectorOpDecoder.cpp @@ -358,6 +358,14 @@ constexpr VectorOpcodeInfo vop3Opcodes[] = { {0x16cu, RdnaOpcode::VMulHiI32}, {0x2ffu, RdnaOpcode::VLshlrevB64}, {0x300u, RdnaOpcode::VLshrrevB64}, + {0x301u, RdnaOpcode::VAshrrevI64}, + {0x305u, RdnaOpcode::VMulLoU16}, + {0x340u, RdnaOpcode::VMadU16}, + {0x35eu, RdnaOpcode::VMadI16}, + {0x373u, RdnaOpcode::VMadU32U16}, + {0x375u, RdnaOpcode::VMadI32I16}, + {0x376u, RdnaOpcode::VSubNcI32}, + {0x37fu, RdnaOpcode::VAddNcI32}, {0x303u, RdnaOpcode::VAddNcU16}, {0x304u, RdnaOpcode::VSubNcU16}, {0x307u, RdnaOpcode::VLshrrevB16}, @@ -514,12 +522,18 @@ bool isNativeVop3F16TernaryOpcode(RdnaOpcode opcode) { } bool isNativeVop3I16TernaryOpcode(RdnaOpcode opcode) { - return opcode == RdnaOpcode::VMed3I16; + return opcode == RdnaOpcode::VMed3I16 || opcode == RdnaOpcode::VMadU16 || opcode == RdnaOpcode::VMadI16 || + opcode == RdnaOpcode::VMadU32U16 || opcode == RdnaOpcode::VMadI32I16; +} + +bool usesSignedSaturateClamp(RdnaOpcode opcode) { + return opcode == RdnaOpcode::VAddNcI32 || opcode == RdnaOpcode::VSubNcI32; } bool isNativeVop3B16BinaryOpcode(RdnaOpcode opcode) { switch (opcode) { case RdnaOpcode::VAddNcU16: + case RdnaOpcode::VMulLoU16: case RdnaOpcode::VSubNcU16: case RdnaOpcode::VMaxU16: case RdnaOpcode::VMaxI16: @@ -1229,6 +1243,10 @@ std::uint32_t nativeVop3SourceCount(RdnaOpcode opcode) { case RdnaOpcode::VSubNcI16: case RdnaOpcode::VLshlrevB64: case RdnaOpcode::VLshrrevB64: + case RdnaOpcode::VAshrrevI64: + case RdnaOpcode::VAddNcI32: + case RdnaOpcode::VSubNcI32: + case RdnaOpcode::VMulLoU16: case RdnaOpcode::VLshlrevB16: case RdnaOpcode::VLshrrevB16: case RdnaOpcode::VAshrrevI16: @@ -1318,10 +1336,13 @@ void applyNativeVop3TernaryModifiers(RdnaInstruction& instruction, std::uint32_t void applyNativeVop3I16TernarySelectors(RdnaInstruction& instruction, std::uint32_t opSel) { RdnaOperand* sources[] = {&instruction.source0, &instruction.source1, &instruction.source2}; - for (std::uint32_t i = 0; i < 3u; ++i) { + const bool wideResult = instruction.op == RdnaOpcode::VMadU32U16 || instruction.op == RdnaOpcode::VMadI32I16; + for (std::uint32_t i = 0; i < (wideResult ? 2u : 3u); ++i) { sources[i]->opSel = ((opSel >> i) & 1u) != 0u; } - instruction.destination.sdwaSel = (opSel & 0x8u) != 0u ? 5u : 4u; + if (!wideResult) { + instruction.destination.sdwaSel = (opSel & 0x8u) != 0u ? 5u : 4u; + } } void applyNativeVop3B16BinaryModifiers(RdnaInstruction& instruction, std::uint32_t opSel) { @@ -1395,7 +1416,7 @@ bool supportsNativeVop3ResultModifiers(RdnaOpcode opcode) { } bool supportsNativeVop3Clamp(RdnaOpcode opcode) { - return supportsNativeVop3ResultModifiers(opcode) || usesInexactClampControl(opcode); + return supportsNativeVop3ResultModifiers(opcode) || usesInexactClampControl(opcode) || usesSignedSaturateClamp(opcode); } void checkNativeVop3Modifiers(RdnaOpcode opcode, bool permlane, bool carryInOut, bool scalarDst, std::uint32_t abs, std::uint32_t opSel, std::uint32_t clamp, std::uint32_t omod, std::uint32_t neg) { @@ -1429,6 +1450,12 @@ void checkNativeVop3Modifiers(RdnaOpcode opcode, bool permlane, bool carryInOut, } return; } + if (usesSignedSaturateClamp(opcode)) { + if (abs != 0u || opSel != 0u || omod != 0u || neg != 0u) { + throw std::invalid_argument("VOP3 source modifiers are not implemented"); + } + return; + } if (opcode == RdnaOpcode::VLdexpF32) { if ((abs & ~1u) != 0u || opSel != 0u || (neg & ~1u) != 0u) { throw std::invalid_argument("VOP3 source modifiers are not implemented"); diff --git a/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp b/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp index f9b643bf7..f184b6e4c 100644 --- a/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp +++ b/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp @@ -153,6 +153,8 @@ private: bool floatCube(const RdnaInstruction& inst, std::uint32_t resultKind); bool integer16Shift(const RdnaInstruction& inst, IrOpcode opcode, bool arithmetic); bool integer16Binary(const RdnaInstruction& inst, IrOpcode opcode, bool sign); + bool integer16Mad(const RdnaInstruction& inst, bool sign, bool wide); + bool vAddSubNcI32(const RdnaInstruction& inst, bool subtract); bool integer16Ternary(const RdnaInstruction& inst, IrOpcode opcode, bool sign); bool vMed3I16(const RdnaInstruction& inst); bool packedInteger16Shift(const RdnaInstruction& inst, IrOpcode opcode, bool arithmetic); diff --git a/core/shader/recompiler/Translation/src/IntegerInstructions.cpp b/core/shader/recompiler/Translation/src/IntegerInstructions.cpp index cccf494d0..a02a6082a 100644 --- a/core/shader/recompiler/Translation/src/IntegerInstructions.cpp +++ b/core/shader/recompiler/Translation/src/IntegerInstructions.cpp @@ -43,6 +43,34 @@ bool TranslationContext::integer16Ternary(const RdnaInstruction& inst, IrOpcode return true; } +bool TranslationContext::integer16Mad(const RdnaInstruction& inst, bool sign, bool wide) { + const IrU32 lhs = readU16AsU32(sourceAt(inst, 0u), sign); + const IrU32 rhs = readU16AsU32(sourceAt(inst, 1u), sign); + const IrU32 addend = wide ? readU32(sourceAt(inst, 2u)) : readU16AsU32(sourceAt(inst, 2u), sign); + const IrU32 result(ir.IAdd(ir.IMul(lhs.Value(), rhs.Value()), addend.Value())); + if (wide) { + writeOperand(inst.destination, &result.Value()); + } else { + write16Bits(inst.destination, IrU32(ir.BitwiseAnd(result.Value(), ir.Constant(0xffffu)))); + } + return true; +} + +bool TranslationContext::vAddSubNcI32(const RdnaInstruction& inst, bool subtract) { + const IrU32 lhs = readU32(sourceAt(inst, 0u)); + const IrU32 rhs = readU32(sourceAt(inst, 1u)); + IrU32 result(subtract ? ir.ISub(lhs.Value(), rhs.Value()) : ir.IAdd(lhs.Value(), rhs.Value())); + if (inst.destination.clamp) { + IrValue& sameSign = subtract ? ir.BitwiseXor(lhs.Value(), rhs.Value()) : ir.BitwiseXor(rhs.Value(), result.Value()); + IrValue& flipped = ir.BitwiseAnd(ir.BitwiseXor(lhs.Value(), result.Value()), sameSign); + const IrU1 overflow(ir.INotEqual(ir.ShiftRightLogical(flipped, ir.Constant(31u)), ir.Constant(0u))); + IrValue& saturated = ir.BitwiseXor(ir.ShiftRightArithmetic(lhs.Value(), ir.Constant(31u)), ir.Constant(0x7fffffffu)); + result = IrU32(ir.Select(overflow.Value(), saturated, result.Value())); + } + writeOperand(inst.destination, &result.Value()); + return true; +} + bool TranslationContext::packedInteger16Shift(const RdnaInstruction& inst, IrOpcode opcode, bool arithmetic) { const auto translateLane = [&](bool highLane) { const IrU32 count(ir.BitwiseAnd(readU16LaneAsU32(sourceAt(inst, 0u), highLane, false).Value(), ir.Constant(15u))); diff --git a/core/shader/recompiler/Translation/src/VectorInstructions.cpp b/core/shader/recompiler/Translation/src/VectorInstructions.cpp index d1351b06b..250083378 100644 --- a/core/shader/recompiler/Translation/src/VectorInstructions.cpp +++ b/core/shader/recompiler/Translation/src/VectorInstructions.cpp @@ -665,6 +665,22 @@ bool TranslationContext::emitVector(const RdnaInstruction& inst) { return simpleInteger(inst, IrOpcode::ShiftLeftLogical64, IrType::U64, true, false, false); case RdnaOpcode::VLshrrevB64: return simpleInteger(inst, IrOpcode::ShiftRightLogical64, IrType::U64, true, false, false); + case RdnaOpcode::VAshrrevI64: + return simpleInteger(inst, IrOpcode::ShiftRightArithmetic64, IrType::U64, true, false, false); + case RdnaOpcode::VAddNcI32: + return vAddSubNcI32(inst, false); + case RdnaOpcode::VSubNcI32: + return vAddSubNcI32(inst, true); + case RdnaOpcode::VMulLoU16: + return integer16Binary(inst, IrOpcode::IMul32, false); + case RdnaOpcode::VMadU16: + return integer16Mad(inst, false, false); + case RdnaOpcode::VMadI16: + return integer16Mad(inst, true, false); + case RdnaOpcode::VMadU32U16: + return integer16Mad(inst, false, true); + case RdnaOpcode::VMadI32I16: + return integer16Mad(inst, true, true); case RdnaOpcode::VXnorB32: return composedIntegerBinary(inst, IrOpcode::BitwiseXor32, false, true, false); case RdnaOpcode::VAndOrB32: