mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
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 <noreply@anthropic.com>
This commit is contained in:
@@ -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 <algorithm>
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <limits>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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<std::uint32_t, Threads * Inputs> Input{};
|
||||
alignas(256) std::array<std::uint32_t, Threads * Results> Output{};
|
||||
|
||||
alignas(256) constexpr std::array<std::uint32_t, 74> 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<std::array<std::uint32_t, 3>, 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<std::uint32_t>(state >> 32u);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
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(IntegerCode);
|
||||
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::uint32_t Saturate(std::int64_t value) {
|
||||
constexpr std::int64_t low = std::numeric_limits<std::int32_t>::min();
|
||||
constexpr std::int64_t high = std::numeric_limits<std::int32_t>::max();
|
||||
return static_cast<std::uint32_t>(std::clamp(value, low, high));
|
||||
}
|
||||
|
||||
std::uint32_t Signed16(std::uint32_t value) {
|
||||
return static_cast<std::uint32_t>(static_cast<std::int32_t>(static_cast<std::int16_t>(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<std::int32_t>(a);
|
||||
const std::int64_t sb = static_cast<std::int32_t>(b);
|
||||
const auto shifted = static_cast<std::uint64_t>(static_cast<std::int64_t>(a | (static_cast<std::uint64_t>(b) << 32u)) >> (c & 63u));
|
||||
const std::array<std::uint32_t, 14> 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<std::uint32_t>(shifted),
|
||||
static_cast<std::uint32_t>(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;
|
||||
}
|
||||
}
|
||||
@@ -348,6 +348,14 @@ enum class RdnaOpcode : std::uint16_t {
|
||||
VAshrrevI32,
|
||||
VLshlrevB64,
|
||||
VLshrrevB64,
|
||||
VAshrrevI64,
|
||||
VAddNcI32,
|
||||
VSubNcI32,
|
||||
VMulLoU16,
|
||||
VMadU16,
|
||||
VMadI16,
|
||||
VMadU32U16,
|
||||
VMadI32I16,
|
||||
VLshlrevB16,
|
||||
VLshrrevB16,
|
||||
VAshrrevI16,
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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)));
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user