From 730de17774aa73b63747d2ac311ab4dbc5bd14bb Mon Sep 17 00:00:00 2001 From: SamG-Coder <13449963+SamG-Coder@users.noreply.github.com> Date: Mon, 28 Sep 2026 19:35:18 +1000 Subject: [PATCH] fix(shader): support V_PERM_B32 byte selection --- .../include/RdnaDecoder/RdnaOpcode.hpp | 1 + .../recompiler/RdnaDecoder/src/RdnaOpcode.cpp | 1 + .../RdnaDecoder/src/RdnaVectorOpDecoder.cpp | 1 + .../Translation/TranslationContext.hpp | 1 + .../Translation/src/IntegerInstructions.cpp | 22 +++++++++++++++++++ .../Translation/src/VectorInstructions.cpp | 2 ++ 6 files changed, 28 insertions(+) diff --git a/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp b/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp index 76b6972ac..f06f8d362 100644 --- a/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp +++ b/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp @@ -284,6 +284,7 @@ enum class RdnaOpcode : std::uint16_t { VAdd3U32, VLshlAddU32, VAddLshlU32, + VPermB32, VXadU32, VLshlOrB32, VAndOrB32, diff --git a/core/shader/recompiler/RdnaDecoder/src/RdnaOpcode.cpp b/core/shader/recompiler/RdnaDecoder/src/RdnaOpcode.cpp index f77f40a5e..54ecc199c 100644 --- a/core/shader/recompiler/RdnaDecoder/src/RdnaOpcode.cpp +++ b/core/shader/recompiler/RdnaDecoder/src/RdnaOpcode.cpp @@ -257,6 +257,7 @@ bool IsVectorAluOpcode(RdnaOpcode opcode) { case RdnaOpcode::VAdd3U32: case RdnaOpcode::VLshlAddU32: case RdnaOpcode::VAddLshlU32: + case RdnaOpcode::VPermB32: case RdnaOpcode::VXadU32: case RdnaOpcode::VLshlOrB32: case RdnaOpcode::VAndOrB32: diff --git a/core/shader/recompiler/RdnaDecoder/src/RdnaVectorOpDecoder.cpp b/core/shader/recompiler/RdnaDecoder/src/RdnaVectorOpDecoder.cpp index 34326cc79..cb3fb15f1 100644 --- a/core/shader/recompiler/RdnaDecoder/src/RdnaVectorOpDecoder.cpp +++ b/core/shader/recompiler/RdnaDecoder/src/RdnaVectorOpDecoder.cpp @@ -355,6 +355,7 @@ constexpr VectorOpcodeInfo vop3Opcodes[] = { {0x311u, RdnaOpcode::VPackB32F16}, {0x314u, RdnaOpcode::VLshlrevB16}, {0x319u, RdnaOpcode::VSubrevI32}, + {0x344u, RdnaOpcode::VPermB32}, {0x345u, RdnaOpcode::VXadU32}, {0x346u, RdnaOpcode::VLshlAddU32}, {0x347u, RdnaOpcode::VAddLshlU32}, diff --git a/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp b/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp index 2aa9f3c36..3a5f8d709 100644 --- a/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp +++ b/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp @@ -186,6 +186,7 @@ private: bool vAlignbyteB32(const RdnaInstruction& inst); bool vLshlAddU32(const RdnaInstruction& inst); bool vAddLshlU32(const RdnaInstruction& inst); + bool vPermB32(const RdnaInstruction& inst); bool vXadU32(const RdnaInstruction& inst); bool vLshlOrB32(const RdnaInstruction& inst); bool vCndmaskB32(const RdnaInstruction& inst); diff --git a/core/shader/recompiler/Translation/src/IntegerInstructions.cpp b/core/shader/recompiler/Translation/src/IntegerInstructions.cpp index 7a3dffd48..310dae951 100644 --- a/core/shader/recompiler/Translation/src/IntegerInstructions.cpp +++ b/core/shader/recompiler/Translation/src/IntegerInstructions.cpp @@ -532,6 +532,28 @@ bool TranslationContext::vAddLshlU32(const RdnaInstruction& inst) { return true; } +bool TranslationContext::vPermB32(const RdnaInstruction& inst) { + const IrU32 high = readU32(sourceAt(inst, 0u)); + const IrU32 low = readU32(sourceAt(inst, 1u)); + const IrU32 selectors = readU32(sourceAt(inst, 2u)); + IrValue* result = &ir.Constant(0u); + for (std::uint32_t byte = 0u; byte < 4u; ++byte) { + IrValue& selector = ir.BitwiseAnd(ir.ShiftRightLogical(selectors.Value(), ir.Constant(byte * 8u)), ir.Constant(255u)); + IrValue& signIndex = ir.IAdd(ir.IMul(ir.BitwiseAnd(selector, ir.Constant(3u)), ir.Constant(2u)), ir.Constant(1u)); + IrValue& index = ir.Select(ir.ULessThan(selector, ir.Constant(8u)), selector, signIndex); + IrValue& word = ir.Select(ir.ULessThan(index, ir.Constant(4u)), low.Value(), high.Value()); + IrValue& shift = ir.IMul(ir.BitwiseAnd(index, ir.Constant(3u)), ir.Constant(8u)); + IrValue& value = ir.BitwiseAnd(ir.ShiftRightLogical(word, shift), ir.Constant(255u)); + IrValue& sign = ir.Select(ir.UGreaterThan(value, ir.Constant(127u)), ir.Constant(255u), ir.Constant(0u)); + IrValue& selected = ir.Select(ir.ULessThan(selector, ir.Constant(8u)), value, sign); + IrValue& fill = ir.Select(ir.IEqual(selector, ir.Constant(12u)), ir.Constant(0u), ir.Constant(255u)); + IrValue& output = ir.Select(ir.ULessThan(selector, ir.Constant(12u)), selected, fill); + result = &ir.BitwiseOr(*result, ir.ShiftLeftLogical(output, ir.Constant(byte * 8u))); + } + writeOperand(inst.destination, result); + return true; +} + bool TranslationContext::vXadU32(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 61eba45b1..ff327c922 100644 --- a/core/shader/recompiler/Translation/src/VectorInstructions.cpp +++ b/core/shader/recompiler/Translation/src/VectorInstructions.cpp @@ -673,6 +673,8 @@ bool TranslationContext::emitVector(const RdnaInstruction& inst) { return vLshlAddU32(inst); case RdnaOpcode::VAddLshlU32: return vAddLshlU32(inst); + case RdnaOpcode::VPermB32: + return vPermB32(inst); case RdnaOpcode::VXadU32: return vXadU32(inst); case RdnaOpcode::VLshlOrB32: