From 4a4a1eddc1c920133b343fab9bf2d172163688e2 Mon Sep 17 00:00:00 2001 From: DotDebian Date: Fri, 2 Oct 2026 18:57:06 +0200 Subject: [PATCH] feat(shader): implement v_lerp_u8 Decode VOP3 0x14d and translate it as the per-byte average of S0 and S1, rounded up where bit 0 of the matching S2 byte is set and truncated otherwise. Co-Authored-By: Claude Opus 5.5 --- .../tests/execution/Vop3Lerp.cpp | 137 ++++++++++++++++++ .../include/RdnaDecoder/RdnaOpcode.hpp | 1 + .../recompiler/RdnaDecoder/src/RdnaOpcode.cpp | 1 + .../RdnaDecoder/src/RdnaVectorOpDecoder.cpp | 1 + .../Translation/TranslationContext.hpp | 1 + .../Translation/src/IntegerInstructions.cpp | 17 +++ .../Translation/src/VectorInstructions.cpp | 2 + 7 files changed, 160 insertions(+) create mode 100644 core/libs/prx/libSceAgcDriver/tests/execution/Vop3Lerp.cpp diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/Vop3Lerp.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/Vop3Lerp.cpp new file mode 100644 index 000000000..dbfd4c211 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/Vop3Lerp.cpp @@ -0,0 +1,137 @@ +#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 = 0x01fe01feu; +alignas(256) std::array Input{}; +alignas(256) std::array Output{}; + +alignas(256) constexpr std::array LerpCode{ + 0x34020082, 0x34060084, 0xe0302000, 0x80000401, 0xe0302004, 0x80000501, 0xe0302008, 0x80000601, + 0xbf8c3f70, 0xd54d000a, 0x041a0b04, 0xd54d000b, 0x041a0905, 0xd54d000c, 0x02020b04, 0xd54d000d, + 0x03060b04, 0xd54d000e, 0x03fe0b04, Literal, 0xd54d000f, 0x041a0ac1, 0xd54d0010, 0x041a0904, + 0x7e220304, 0xd54d0011, 0x041a0b11, 0xe0702000, 0x80010a03, 0xe0702004, 0x80010b03, 0xe0702008, + 0x80010c03, 0xe070200c, 0x80010d03, 0xe0702010, 0x80010e03, 0xe0702014, 0x80010f03, 0xe0702018, + 0x80011003, 0xe070201c, 0x80011103, 0xbf810000, +}; + +constexpr std::array, 12> Edges{{ + {0xffffffffu, 0xffffffffu, 0xffffffffu, 0xffffffffu}, {0xffffffffu, 0u, 0x01010101u, 0x80808080u}, {0xffffffffu, 0u, 0u, 0x7f7f7f7fu}, + {0u, 0u, 0xffffffffu, 0u}, {0x01010101u, 0u, 0x01010101u, 0x01010101u}, {0x01010101u, 0u, 0xfefefefeu, 0u}, + {0xff00ff00u, 0x00ff00ffu, 0x01000100u, 0x807f807fu}, {0x80808080u, 0x7f7f7f7fu, 0x00010001u, 0x7f807f80u}, + {0x000000ffu, 0x0000ff00u, 0x00010000u, 0x00007f7fu}, {0x12345678u, 0x9abcdef0u, 0xffffffffu, 0x56789ab4u}, + {0xfe01fe01u, 0x01fe01feu, 0x80808080u, 0x7f7f7f7fu}, {0u, 0u, 0u, 0u}, +}}; + +void FillInput() { + std::uint64_t state = 0xd1b54a32d192ed03ull; + 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}; +} + +std::string Hex(std::uint32_t value) { + char text[16]; + std::snprintf(text, sizeof(text), "0x%08x", value); + return text; +} + +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(LerpCode); + 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 Lerp(std::uint32_t lhs, std::uint32_t rhs, std::uint32_t rounding) { + std::uint32_t result = 0u; + for (std::uint32_t offset = 0; offset < 32u; offset += 8u) { + const std::uint32_t sum = ((lhs >> offset) & 0xffu) + ((rhs >> offset) & 0xffu) + ((rounding >> offset) & 1u); + result |= (sum >> 1u) << offset; + } + return result; +} + +void Check() { + constexpr std::array names{ + "v_lerp_u8", "v_lerp_u8 with swapped sources", "v_lerp_u8 truncating", "v_lerp_u8 rounding every byte", "v_lerp_u8 with a literal round mode", + "v_lerp_u8 with an inline constant", "v_lerp_u8 of a value with itself", "v_lerp_u8 into its first source", + }; + 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::array expected{ + tid < Edges.size() ? Edges[tid][3] : Lerp(a, b, c), + Lerp(a, b, c), + Lerp(a, b, 0u), + Lerp(a, b, 0xffffffffu), + Lerp(a, b, Literal), + Lerp(0xffffffffu, b, c), + a, + Lerp(a, b, 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(); + std::puts("vop3 lerp 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..b4b992117 100644 --- a/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp +++ b/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp @@ -309,6 +309,7 @@ enum class RdnaOpcode : std::uint16_t { VLshlAddU32, VAddLshlU32, VPermB32, + VLerpU8, VXadU32, VLshlOrB32, VAndOrB32, diff --git a/core/shader/recompiler/RdnaDecoder/src/RdnaOpcode.cpp b/core/shader/recompiler/RdnaDecoder/src/RdnaOpcode.cpp index fcf4df05b..8f130aef6 100644 --- a/core/shader/recompiler/RdnaDecoder/src/RdnaOpcode.cpp +++ b/core/shader/recompiler/RdnaDecoder/src/RdnaOpcode.cpp @@ -284,6 +284,7 @@ bool IsVectorAluOpcode(RdnaOpcode opcode) { case RdnaOpcode::VLshlAddU32: case RdnaOpcode::VAddLshlU32: case RdnaOpcode::VPermB32: + case RdnaOpcode::VLerpU8: 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 5b664ec8b..3d8fd1bdc 100644 --- a/core/shader/recompiler/RdnaDecoder/src/RdnaVectorOpDecoder.cpp +++ b/core/shader/recompiler/RdnaDecoder/src/RdnaVectorOpDecoder.cpp @@ -437,6 +437,7 @@ constexpr VectorOpcodeInfo vop3Opcodes[] = { {0x314u, RdnaOpcode::VLshlrevB16}, {0x319u, RdnaOpcode::VSubrevI32}, {0x344u, RdnaOpcode::VPermB32}, + {0x14du, RdnaOpcode::VLerpU8}, {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 84096a2d8..b91e3606e 100644 --- a/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp +++ b/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp @@ -197,6 +197,7 @@ private: bool vLshlAddU32(const RdnaInstruction& inst); bool vAddLshlU32(const RdnaInstruction& inst); bool vPermB32(const RdnaInstruction& inst); + bool vLerpU8(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 a02a6082a..f685fc640 100644 --- a/core/shader/recompiler/Translation/src/IntegerInstructions.cpp +++ b/core/shader/recompiler/Translation/src/IntegerInstructions.cpp @@ -601,6 +601,23 @@ bool TranslationContext::vPermB32(const RdnaInstruction& inst) { return true; } +bool TranslationContext::vLerpU8(const RdnaInstruction& inst) { + const IrU32 lhs = readU32(sourceAt(inst, 0u)); + const IrU32 rhs = readU32(sourceAt(inst, 1u)); + const IrU32 rounding = readU32(sourceAt(inst, 2u)); + IrU32 result(ir.Constant(0u)); + for (std::uint32_t offset = 0u; offset < 32u; offset += 8u) { + const IrU32 lhsByte(ir.Emit(IrOpcode::BitFieldUExtract, IrType::U32, {&lhs.Value(), &ir.Constant(offset), &ir.Constant(8u)})); + const IrU32 rhsByte(ir.Emit(IrOpcode::BitFieldUExtract, IrType::U32, {&rhs.Value(), &ir.Constant(offset), &ir.Constant(8u)})); + const IrU32 roundUp(ir.Emit(IrOpcode::BitFieldUExtract, IrType::U32, {&rounding.Value(), &ir.Constant(offset), &ir.Constant(1u)})); + const IrU32 sum(ir.IAdd(ir.IAdd(lhsByte.Value(), rhsByte.Value()), roundUp.Value())); + const IrU32 average(ir.ShiftRightLogical(sum.Value(), ir.Constant(1u))); + result = IrU32(ir.BitwiseOr(result.Value(), ir.ShiftLeftLogical(average.Value(), ir.Constant(offset)))); + } + writeOperand(inst.destination, &result.Value()); + 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 9b6710ea9..310cee9d3 100644 --- a/core/shader/recompiler/Translation/src/VectorInstructions.cpp +++ b/core/shader/recompiler/Translation/src/VectorInstructions.cpp @@ -885,6 +885,8 @@ bool TranslationContext::emitVector(const RdnaInstruction& inst) { return vAddLshlU32(inst); case RdnaOpcode::VPermB32: return vPermB32(inst); + case RdnaOpcode::VLerpU8: + return vLerpU8(inst); case RdnaOpcode::VXadU32: return vXadU32(inst); case RdnaOpcode::VLshlOrB32: