mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
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.
This commit is contained in:
@@ -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 <algorithm>
|
||||
#include <array>
|
||||
#include <bit>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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<std::uint32_t, Threads * Inputs> Input{};
|
||||
alignas(256) std::array<std::uint32_t, Threads * Results> Output{};
|
||||
|
||||
alignas(256) constexpr std::array<std::uint32_t, 30> 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<Row, 20> 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<std::uint32_t, 4> BufferDescriptor(const void* data, std::uint32_t count) {
|
||||
const auto address = reinterpret_cast<std::uintptr_t>(data);
|
||||
return {static_cast<std::uint32_t>(address), static_cast<std::uint32_t>((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<std::uint32_t, 3> words{row.a, row.b, row.c};
|
||||
std::copy(words.begin(), words.end(), Input.begin() + tid * Inputs);
|
||||
}
|
||||
Output.fill(0xdeadbeefu);
|
||||
std::vector<std::uint32_t> userData(8, 0u);
|
||||
const auto input = BufferDescriptor(Input.data(), static_cast<std::uint32_t>(Input.size()));
|
||||
const auto output = BufferDescriptor(Output.data(), static_cast<std::uint32_t>(Output.size()));
|
||||
std::copy(input.begin(), input.end(), userData.begin());
|
||||
std::copy(output.begin(), output.end(), userData.begin() + 4);
|
||||
const std::span<const std::uint32_t> code(FmaLegacyCode);
|
||||
const std::array<ShaderRecompiler::MemoryRegion, 1> memory{{{reinterpret_cast<std::uintptr_t>(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<std::uintptr_t>(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<std::uintptr_t>(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<std::uint32_t>(0.0f + c);
|
||||
}
|
||||
return std::bit_cast<std::uint32_t>(std::fma(a, b, c));
|
||||
}
|
||||
|
||||
void Check() {
|
||||
constexpr std::array<const char*, 5> 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<float>(row.a);
|
||||
const float b = std::bit_cast<float>(row.b);
|
||||
const float c = std::bit_cast<float>(row.c);
|
||||
const std::array<std::uint32_t, 5> 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;
|
||||
}
|
||||
}
|
||||
@@ -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:
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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)}));
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user