From d32eb5d3096a86cd9af87afef728bce8831fac8b Mon Sep 17 00:00:00 2001 From: Nahuel Genchi <65604505+NahuelGenchi@users.noreply.github.com> Date: Sun, 4 Oct 2026 21:47:22 -0300 Subject: [PATCH] fix(shader): apply the DX9 zero rule to v_fma_legacy_f32 VOP3 0x140 was translated as a plain fma, so a zero source times an infinity or a NaN gave NaN instead of the addend. A zero source now gives +0 + src2, the same rule v_mul_legacy_f32 and v_mac_legacy_f32 already follow. The opcode also takes neg and abs on its sources. --- .../tests/execution/FmaLegacy.cpp | 144 ++++++++++++++++++ .../RdnaDecoder/src/RdnaVectorOpDecoder.cpp | 1 + .../Translation/TranslationContext.hpp | 1 + .../Translation/src/FloatInstructions.cpp | 11 ++ .../Translation/src/VectorInstructions.cpp | 3 +- 5 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 core/libs/prx/libSceAgcDriver/tests/execution/FmaLegacy.cpp diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/FmaLegacy.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/FmaLegacy.cpp new file mode 100644 index 000000000..e9c867787 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/FmaLegacy.cpp @@ -0,0 +1,144 @@ +#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 +#include + +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 = 8; +alignas(256) std::array Input{}; +alignas(256) std::array Output{}; + +alignas(256) constexpr std::array FmaLegacyCode{ + 0x34020083, 0x34060083, 0xe0302000, 0x80000401, 0xe0302004, 0x80000501, 0xe0302008, 0x80000601, + 0xbf8c3f70, 0xd540000a, 0x041a0b04, 0xd540000b, 0x241a0b04, 0xd540010c, 0x841a0b04, 0xd540000d, + 0x041a0a80, 0xd540020e, 0x641a0b04, 0xe0702000, 0x80010a03, 0xe0702004, 0x80010b03, 0xe0702008, + 0x80010c03, 0xe070200c, 0x80010d03, 0xe0702010, 0x80010e03, 0xbf810000, +}; + +struct Row { + std::uint32_t a, b, c; +}; + +constexpr std::array Rows{{ + {0x00000000u, 0x7f800000u, 0x3f800000u}, + {0x80000000u, 0x7f800000u, 0xc0200000u}, + {0x7f800000u, 0x00000000u, 0x3f400000u}, + {0xff800000u, 0x80000000u, 0x40400000u}, + {0x00000000u, 0x7fc00000u, 0x3fc00000u}, + {0x7fc00000u, 0x80000000u, 0xc0800000u}, + {0x00000000u, 0x40000000u, 0x7f800000u}, + {0x80000000u, 0xc0400000u, 0xff800000u}, + {0x00000000u, 0x3f800000u, 0x7fc00000u}, + {0x3fc00000u, 0x40100000u, 0xbf400000u}, + {0xc0c00000u, 0x3f000000u, 0x41200000u}, + {0x7f800000u, 0x40000000u, 0x3f800000u}, + {0x7f800000u, 0xbf800000u, 0x7f800000u}, + {0x7fc00000u, 0x3f800000u, 0x3f800000u}, + {0x40400000u, 0x40800000u, 0xc1400000u}, + {0x7f7fffffu, 0x40000000u, 0x3f800000u}, + {0x3ec00000u, 0xc1480000u, 0x42c88000u}, + {0xc4800000u, 0xbd800000u, 0xc2800000u}, + {0x40e00000u, 0x41100000u, 0x3f000000u}, + {0x80000000u, 0x80000000u, 0x40a00000u}, +}}; + +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) { + Input.fill(0u); + for (std::uint32_t tid = 0; tid < Rows.size(); ++tid) { + const auto& row = Rows[tid]; + const std::array words{row.a, row.b, row.c}; + std::copy(words.begin(), words.end(), Input.begin() + tid * Inputs); + } + 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(FmaLegacyCode); + 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::string Hex(std::uint32_t value) { + char text[16]; + std::snprintf(text, sizeof(text), "0x%08x", value); + return text; +} + +bool IsNan(std::uint32_t bits) { + return (bits & 0x7fffffffu) > 0x7f800000u; +} + +std::uint32_t FmaLegacy(float a, float b, float c) { + if (a == 0.0f || b == 0.0f) { + return std::bit_cast(0.0f + c); + } + return std::bit_cast(std::fma(a, b, c)); +} + +void Check() { + constexpr std::array names{ + "v_fma_legacy_f32", "v_fma_legacy_f32 -src0", "v_fma_legacy_f32 |src0| -src2", "v_fma_legacy_f32 src0=0", "v_fma_legacy_f32 -src0 -|src1|", + }; + for (std::uint32_t tid = 0; tid < Rows.size(); ++tid) { + const auto& row = Rows[tid]; + const float a = std::bit_cast(row.a); + const float b = std::bit_cast(row.b); + const float c = std::bit_cast(row.c); + const std::array expected{ + FmaLegacy(a, b, c), FmaLegacy(-a, b, c), FmaLegacy(std::fabs(a), b, -c), FmaLegacy(0.0f, b, c), FmaLegacy(-a, -std::fabs(b), c), + }; + for (std::uint32_t j = 0; j < expected.size(); ++j) { + const auto actual = Output[tid * Results + j]; + const bool matches = actual == expected[j] || (IsNan(actual) && IsNan(expected[j])); + Require(matches, std::string("fma legacy: lane ") + std::to_string(tid) + " (" + Hex(row.a) + ", " + Hex(row.b) + ", " + Hex(row.c) + ") " + names[j] + " is " + Hex(actual) + ", expected " + Hex(expected[j])); + } + } +} + +} + +int main() { + try { + const auto device = OpenVulkanTestDevice(); + if (!device) return VulkanTestSkipped; + Run(*device); + Check(); + std::puts("fma legacy tests passed"); + return 0; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; + } +} diff --git a/core/shader/recompiler/RdnaDecoder/src/RdnaVectorOpDecoder.cpp b/core/shader/recompiler/RdnaDecoder/src/RdnaVectorOpDecoder.cpp index dfce3c276..878b9c801 100644 --- a/core/shader/recompiler/RdnaDecoder/src/RdnaVectorOpDecoder.cpp +++ b/core/shader/recompiler/RdnaDecoder/src/RdnaVectorOpDecoder.cpp @@ -1763,6 +1763,7 @@ bool supportsNativeVop3SourceModifiers(RdnaOpcode opcode) { case RdnaOpcode::VMaxF16: case RdnaOpcode::VMacF32: case RdnaOpcode::VMadF32: + case RdnaOpcode::VMadLegacyF32: case RdnaOpcode::VFmaF32: case RdnaOpcode::VPackB32F16: case RdnaOpcode::VCubeidF32: diff --git a/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp b/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp index 35d84281e..033a135aa 100644 --- a/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp +++ b/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp @@ -161,6 +161,7 @@ private: bool vDivFmasF32(const RdnaInstruction& inst); bool vDivFixupF32(const RdnaInstruction& inst); bool vFrexpMantF32(const RdnaInstruction& inst); + bool vFmaLegacyF32(const RdnaInstruction& inst); IrU32 readF16Bits(const RdnaOperand& operand); IrU32 normF16(IrU32 bits, bool signedValue); bool vLdexpF16(const RdnaInstruction& inst); diff --git a/core/shader/recompiler/Translation/src/FloatInstructions.cpp b/core/shader/recompiler/Translation/src/FloatInstructions.cpp index 4307ca9c9..5b4e4e1f2 100644 --- a/core/shader/recompiler/Translation/src/FloatInstructions.cpp +++ b/core/shader/recompiler/Translation/src/FloatInstructions.cpp @@ -394,6 +394,17 @@ bool TranslationContext::floatTernary(const RdnaInstruction& inst, IrOpcode opco return true; } +bool TranslationContext::vFmaLegacyF32(const RdnaInstruction& inst) { + IrValue* lhs = readOperand(sourceAt(inst, 0u), IrType::F32); + IrValue* rhs = readOperand(sourceAt(inst, 1u), IrType::F32); + IrValue* addend = readOperand(sourceAt(inst, 2u), IrType::F32); + const auto isZero = [&](IrValue* value) { return IrU1(ir.IEqual(ir.BitwiseAnd(ir.BitCastU32(*value), ir.Constant(0x7fffffffu)), ir.Constant(0u))); }; + const IrU1 zero(ir.LogicalOr(isZero(lhs).Value(), isZero(rhs).Value())); + const auto factor = [&](IrValue* value) { return &ir.Emit(IrOpcode::SelectF32, IrType::F32, {&zero.Value(), &ir.ConstantF32(0.0f), value}); }; + writeOperand(inst.destination, &ir.Emit(IrOpcode::FPFma32, IrType::F32, {factor(lhs), factor(rhs), addend})); + return true; +} + bool TranslationContext::vFrexpMantF32(const RdnaInstruction& inst) { const IrU32 bits = readU32(sourceAt(inst, 0u)); const IrU32 exponent(ir.Emit(IrOpcode::BitFieldUExtract, IrType::U32, {&bits.Value(), &ir.Constant(23u), &ir.Constant(8u)})); diff --git a/core/shader/recompiler/Translation/src/VectorInstructions.cpp b/core/shader/recompiler/Translation/src/VectorInstructions.cpp index 9e2d42450..d66cbdfd1 100644 --- a/core/shader/recompiler/Translation/src/VectorInstructions.cpp +++ b/core/shader/recompiler/Translation/src/VectorInstructions.cpp @@ -939,9 +939,10 @@ bool TranslationContext::emitVector(const RdnaInstruction& inst) { case RdnaOpcode::VMadmkF32: case RdnaOpcode::VMadakF32: case RdnaOpcode::VMadF32: - case RdnaOpcode::VMadLegacyF32: case RdnaOpcode::VFmaF32: return floatTernary(inst, roundsProductSeparately(inst) ? IrOpcode::FPMad32 : IrOpcode::FPFma32, false, true); + case RdnaOpcode::VMadLegacyF32: + return vFmaLegacyF32(inst); case RdnaOpcode::VMin3F32: return floatTernary(inst, IrOpcode::FPMinTri32, false, false); case RdnaOpcode::VMax3F32: