mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
Merge pull request #255 from adriadam10/feat/scalar-bit-count-ops
feat(shader): implement s_bcnt0_i32_b64, s_ff0_i32_b64, s_flbit_i32(_i64), s_quadmask_b32
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
#include "prx/libSceAgcDriver/Execution/include/VulkanDevice.hpp"
|
||||
#include "prx/libSceAgcDriver/Graphics/include/Draw.hpp"
|
||||
#include "Recompiler.hpp"
|
||||
#include "VulkanTestDevice.hpp"
|
||||
#include <array>
|
||||
#include <bit>
|
||||
#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 = 4;
|
||||
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, 51> BitCountCode{
|
||||
0x34020082, 0x34060083, 0xe0302000, 0x80000401, 0xe0302004, 0x80000501, 0xbf8c3f70, 0xbe940380,
|
||||
0xd7600008, 0x00002904, 0xd7600009, 0x00002905, 0xbe8a0e08, 0x850f8081, 0xbe8b1208, 0xbe8c1708,
|
||||
0xbe8d1808, 0xbe8e2c08, 0x85108081, 0xd761000a, 0x0000280a, 0xd761000b, 0x0000280b, 0xd761000c,
|
||||
0x0000280c, 0xd761000d, 0x0000280d, 0xd761000e, 0x0000280e, 0xd761000f, 0x0000280f, 0xd7610010,
|
||||
0x00002810, 0x80148114, 0xbf0aa014, 0xbf85ffe4, 0xe0702000, 0x80010a03, 0xe0702004, 0x80010b03,
|
||||
0xe0702008, 0x80010c03, 0xe070200c, 0x80010d03, 0xe0702010, 0x80010e03, 0xe0702014, 0x80010f03,
|
||||
0xe0702018, 0x80011003, 0xbf810000,
|
||||
};
|
||||
|
||||
constexpr std::array<std::uint64_t, 10> Edges{
|
||||
0u, ~0ull, 0xffffffff00000000ull, 0x00000000ffffffffull, 1u, 0x8000000000000000ull,
|
||||
0x7fffffffffffffffull, 0x00000000f0000000ull, 0xfffffffe7fffffffull, 0x0000000100000000ull,
|
||||
};
|
||||
|
||||
void FillInput() {
|
||||
std::uint64_t state = 0x9e3779b97f4a7c15ull;
|
||||
for (std::uint32_t tid = 0; tid < Threads; ++tid) {
|
||||
state = state * 6364136223846793005ull + 1442695040888963407ull;
|
||||
const std::uint64_t value = tid < Edges.size() ? Edges[tid] : state >> (tid % 13u);
|
||||
Input[tid * Inputs] = static_cast<std::uint32_t>(value);
|
||||
Input[tid * Inputs + 1] = static_cast<std::uint32_t>(value >> 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(BitCountCode);
|
||||
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 SignedLeadingBits(std::uint64_t value, std::uint64_t sign, int width) {
|
||||
const std::uint64_t folded = (value & sign) != 0u ? ~value & (sign | (sign - 1u)) : value;
|
||||
return folded == 0u ? 0xffffffffu : static_cast<std::uint32_t>(std::countl_zero(folded) - (64 - width));
|
||||
}
|
||||
|
||||
void Check() {
|
||||
for (std::uint32_t tid = 0; tid < Threads; ++tid) {
|
||||
const std::uint64_t value = Input[tid * Inputs] | (static_cast<std::uint64_t>(Input[tid * Inputs + 1]) << 32u);
|
||||
const auto low = static_cast<std::uint32_t>(value);
|
||||
std::uint32_t quads = 0;
|
||||
for (std::uint32_t quad = 0; quad < 8u; ++quad) quads |= ((low >> (quad * 4u)) & 0xfu) != 0u ? 1u << quad : 0u;
|
||||
const auto zeros = static_cast<std::uint32_t>(64 - std::popcount(value));
|
||||
const std::array<std::uint32_t, 7> expected{
|
||||
zeros,
|
||||
~value == 0u ? 0xffffffffu : static_cast<std::uint32_t>(std::countr_one(value)),
|
||||
SignedLeadingBits(low, 0x80000000u, 32),
|
||||
SignedLeadingBits(value, 0x8000000000000000ull, 64),
|
||||
quads,
|
||||
zeros != 0u ? 1u : 0u,
|
||||
quads != 0u ? 1u : 0u,
|
||||
};
|
||||
const auto* out = &Output[tid * Results];
|
||||
for (std::uint32_t j = 0; j < expected.size(); ++j) {
|
||||
Require(out[j] == expected[j], "scalar bit count: lane " + std::to_string(tid) + " result " + std::to_string(j) + " is " + std::to_string(out[j]) + ", expected " + std::to_string(expected[j]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main() {
|
||||
try {
|
||||
const auto device = OpenVulkanTestDevice();
|
||||
if (!device) return VulkanTestSkipped;
|
||||
if (device->Target().subgroupSize < Threads) {
|
||||
std::printf("skipped, subgroup size %u cannot hold a wave32\n", device->Target().subgroupSize);
|
||||
return VulkanTestSkipped;
|
||||
}
|
||||
FillInput();
|
||||
Run(*device);
|
||||
Check();
|
||||
std::puts("scalar bit count tests passed");
|
||||
return 0;
|
||||
} catch (const std::exception& error) {
|
||||
std::cerr << error.what() << '\n';
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -149,14 +149,19 @@ enum class RdnaOpcode : std::uint16_t {
|
||||
SSextI32I8,
|
||||
SSextI32I16,
|
||||
SBcnt0I32B32,
|
||||
SBcnt0I32B64,
|
||||
SBcnt1I32B32,
|
||||
SBcnt1I32B64,
|
||||
SFf0I32B32,
|
||||
SFf0I32B64,
|
||||
SFf1I32B32,
|
||||
SFf1I32B64,
|
||||
SFlbitI32B32,
|
||||
SFlbitI32B64,
|
||||
SFlbitI32,
|
||||
SFlbitI32I64,
|
||||
SBitreplicateB64B32,
|
||||
SQuadmaskB32,
|
||||
SQuadmaskB64,
|
||||
SGetpcB64,
|
||||
SSubvectorLoopBegin,
|
||||
|
||||
@@ -46,14 +46,19 @@ bool IsScalarAluOpcode(RdnaOpcode opcode) {
|
||||
case RdnaOpcode::SSextI32I8:
|
||||
case RdnaOpcode::SSextI32I16:
|
||||
case RdnaOpcode::SBcnt0I32B32:
|
||||
case RdnaOpcode::SBcnt0I32B64:
|
||||
case RdnaOpcode::SBcnt1I32B32:
|
||||
case RdnaOpcode::SBcnt1I32B64:
|
||||
case RdnaOpcode::SFf0I32B32:
|
||||
case RdnaOpcode::SFf0I32B64:
|
||||
case RdnaOpcode::SFf1I32B32:
|
||||
case RdnaOpcode::SFf1I32B64:
|
||||
case RdnaOpcode::SFlbitI32B32:
|
||||
case RdnaOpcode::SFlbitI32B64:
|
||||
case RdnaOpcode::SFlbitI32:
|
||||
case RdnaOpcode::SFlbitI32I64:
|
||||
case RdnaOpcode::SBitreplicateB64B32:
|
||||
case RdnaOpcode::SQuadmaskB32:
|
||||
case RdnaOpcode::SQuadmaskB64:
|
||||
case RdnaOpcode::SAndSaveexecB32:
|
||||
case RdnaOpcode::SOrSaveexecB32:
|
||||
|
||||
@@ -20,13 +20,17 @@ RdnaOpcode decodeSop1Opcode(std::uint32_t opcode) {
|
||||
case 0x0bu: return RdnaOpcode::SBrevB32;
|
||||
case 0x0cu: return RdnaOpcode::SBrevB64;
|
||||
case 0x0du: return RdnaOpcode::SBcnt0I32B32;
|
||||
case 0x0eu: return RdnaOpcode::SBcnt0I32B64;
|
||||
case 0x0fu: return RdnaOpcode::SBcnt1I32B32;
|
||||
case 0x10u: return RdnaOpcode::SBcnt1I32B64;
|
||||
case 0x11u: return RdnaOpcode::SFf0I32B32;
|
||||
case 0x12u: return RdnaOpcode::SFf0I32B64;
|
||||
case 0x13u: return RdnaOpcode::SFf1I32B32;
|
||||
case 0x14u: return RdnaOpcode::SFf1I32B64;
|
||||
case 0x15u: return RdnaOpcode::SFlbitI32B32;
|
||||
case 0x16u: return RdnaOpcode::SFlbitI32B64;
|
||||
case 0x17u: return RdnaOpcode::SFlbitI32;
|
||||
case 0x18u: return RdnaOpcode::SFlbitI32I64;
|
||||
case 0x19u: return RdnaOpcode::SSextI32I8;
|
||||
case 0x1au: return RdnaOpcode::SSextI32I16;
|
||||
case 0x1bu: return RdnaOpcode::SBitset0B32;
|
||||
@@ -43,6 +47,7 @@ RdnaOpcode decodeSop1Opcode(std::uint32_t opcode) {
|
||||
case 0x29u: return RdnaOpcode::SNandSaveexecB64;
|
||||
case 0x2au: return RdnaOpcode::SNorSaveexecB64;
|
||||
case 0x2bu: return RdnaOpcode::SXnorSaveexecB64;
|
||||
case 0x2cu: return RdnaOpcode::SQuadmaskB32;
|
||||
case 0x2du: return RdnaOpcode::SQuadmaskB64;
|
||||
case 0x34u: return RdnaOpcode::SAbsI32;
|
||||
case 0x37u: return RdnaOpcode::SAndn1SaveexecB64;
|
||||
|
||||
@@ -169,9 +169,9 @@ private:
|
||||
bool vAndOrB32(const RdnaInstruction& inst);
|
||||
bool vOr3B32(const RdnaInstruction& inst);
|
||||
bool vXor3B32(const RdnaInstruction& inst);
|
||||
bool sFf1I32B64(const RdnaInstruction& inst);
|
||||
bool sFfI32B64(const RdnaInstruction& inst, bool zero);
|
||||
bool vFfbh32(const RdnaInstruction& inst, bool sign);
|
||||
bool sFlbitI32B64(const RdnaInstruction& inst);
|
||||
bool sFlbitI32B64(const RdnaInstruction& inst, bool sign);
|
||||
bool integer24(const RdnaInstruction& inst, bool sign, bool addend, bool high = false);
|
||||
bool vMadU64U32(const RdnaInstruction& inst);
|
||||
bool vSadU32(const RdnaInstruction& inst);
|
||||
@@ -181,7 +181,7 @@ private:
|
||||
bool vBcntU32B32(const RdnaInstruction& inst);
|
||||
bool vMbcntU32B32(const RdnaInstruction& inst, bool low);
|
||||
bool sBitreplicateB64B32(const RdnaInstruction& inst);
|
||||
bool sQuadmaskB64(const RdnaInstruction& inst);
|
||||
bool sQuadmask(const RdnaInstruction& inst, bool wide);
|
||||
bool bfmB32(const RdnaInstruction& inst);
|
||||
IrU32 rightMask32(IrU32 count);
|
||||
IrU64 rightMask64(IrU32 count);
|
||||
|
||||
@@ -255,8 +255,11 @@ bool TranslationContext::vXor3B32(const RdnaInstruction& inst) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TranslationContext::sFf1I32B64(const RdnaInstruction& inst) {
|
||||
const std::array<IrU32, 2> source = extractU64(readU64(sourceAt(inst, 0u)));
|
||||
bool TranslationContext::sFfI32B64(const RdnaInstruction& inst, bool zero) {
|
||||
std::array<IrU32, 2> source = extractU64(readU64(sourceAt(inst, 0u)));
|
||||
if (zero) {
|
||||
source = {IrU32(ir.BitwiseNot(source[0].Value())), IrU32(ir.BitwiseNot(source[1].Value()))};
|
||||
}
|
||||
const IrU32 lowLsb(ir.Emit(IrOpcode::FindILsb32, IrType::U32, {&source[0].Value()}));
|
||||
const IrU32 highLsb(ir.Emit(IrOpcode::FindILsb32, IrType::U32, {&source[1].Value()}));
|
||||
const IrU32 highPosition(ir.IAdd(highLsb.Value(), ir.Constant(32u)));
|
||||
@@ -279,8 +282,13 @@ bool TranslationContext::vFfbh32(const RdnaInstruction& inst, bool sign) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TranslationContext::sFlbitI32B64(const RdnaInstruction& inst) {
|
||||
const IrU64 source = readU64(sourceAt(inst, 0u));
|
||||
bool TranslationContext::sFlbitI32B64(const RdnaInstruction& inst, bool sign) {
|
||||
IrU64 source = readU64(sourceAt(inst, 0u));
|
||||
if (sign) {
|
||||
const std::array<IrU32, 2> halves = extractU64(source);
|
||||
IrValue& mask = ir.ShiftRightArithmetic(halves[1].Value(), ir.Constant(31u));
|
||||
source = IrU64(ir.ConstructU64(ir.BitwiseXor(halves[0].Value(), mask), ir.BitwiseXor(halves[1].Value(), mask)));
|
||||
}
|
||||
const IrU32 msb(ir.Emit(IrOpcode::FindUMsb64, IrType::U32, {&source.Value()}));
|
||||
const IrU32 position(ir.ISub(ir.Constant(63u), msb.Value()));
|
||||
const IrU1 nonZero(ir.Emit(IrOpcode::INotEqual64, IrType::U1, {&source.Value(), &ir.ConstantU64(0)}));
|
||||
@@ -414,7 +422,7 @@ bool TranslationContext::sBitreplicateB64B32(const RdnaInstruction& inst) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TranslationContext::sQuadmaskB64(const RdnaInstruction& inst) {
|
||||
bool TranslationContext::sQuadmask(const RdnaInstruction& inst, bool wide) {
|
||||
const auto compact = [&](IrU32 value) {
|
||||
IrU32 bits(ir.BitwiseOr(value.Value(), ir.ShiftRightLogical(value.Value(), ir.Constant(1u))));
|
||||
bits = IrU32(ir.BitwiseOr(bits.Value(), ir.ShiftRightLogical(bits.Value(), ir.Constant(2u))));
|
||||
@@ -423,6 +431,12 @@ bool TranslationContext::sQuadmaskB64(const RdnaInstruction& inst) {
|
||||
bits = IrU32(ir.BitwiseAnd(ir.BitwiseOr(bits.Value(), ir.ShiftRightLogical(bits.Value(), ir.Constant(6u))), ir.Constant(0x000f000fu)));
|
||||
return IrU32(ir.BitwiseAnd(ir.BitwiseOr(bits.Value(), ir.ShiftRightLogical(bits.Value(), ir.Constant(12u))), ir.Constant(0xffu)));
|
||||
};
|
||||
if (!wide) {
|
||||
const IrU32 quads = compact(readU32(sourceAt(inst, 0u)));
|
||||
writeOperand(inst.destination, &quads.Value());
|
||||
ir.SetScc(ir.INotEqual(quads.Value(), ir.Constant(0u)));
|
||||
return true;
|
||||
}
|
||||
const std::array<IrU32, 2> source = readU32Pair(sourceAt(inst, 0u));
|
||||
const IrU32 lowCompact = compact(source[0]);
|
||||
const IrU32 highCompact = compact(source[1]);
|
||||
|
||||
@@ -275,6 +275,15 @@ bool TranslationContext::emitScalar(const RdnaInstruction& inst) {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
case RdnaOpcode::SBcnt0I32B64: {
|
||||
const std::array<IrU32, 2> source = extractU64(readU64(sourceAt(inst, 0u)));
|
||||
auto& low = ir.Emit(IrOpcode::BitCount32, IrType::U32, {&ir.BitwiseNot(source[0].Value())});
|
||||
auto& high = ir.Emit(IrOpcode::BitCount32, IrType::U32, {&ir.BitwiseNot(source[1].Value())});
|
||||
auto& result = ir.IAdd(low, high);
|
||||
writeOperand(inst.destination, &result);
|
||||
ir.SetScc(ir.INotEqual(result, ir.Constant(0u)));
|
||||
return true;
|
||||
}
|
||||
case RdnaOpcode::SBcnt1I32B32:
|
||||
return simpleInteger(inst, IrOpcode::BitCount32, IrType::U32, false, false, true);
|
||||
case RdnaOpcode::SBcnt1I32B64:
|
||||
@@ -303,12 +312,18 @@ bool TranslationContext::emitScalar(const RdnaInstruction& inst) {
|
||||
return composedIntegerBinary(inst, IrOpcode::BitwiseOr32, false, true, true);
|
||||
case RdnaOpcode::SXnorB32:
|
||||
return composedIntegerBinary(inst, IrOpcode::BitwiseXor32, false, true, true);
|
||||
case RdnaOpcode::SFf0I32B64:
|
||||
return sFfI32B64(inst, true);
|
||||
case RdnaOpcode::SFf1I32B64:
|
||||
return sFf1I32B64(inst);
|
||||
return sFfI32B64(inst, false);
|
||||
case RdnaOpcode::SFlbitI32B32:
|
||||
return vFfbh32(inst, false);
|
||||
case RdnaOpcode::SFlbitI32:
|
||||
return vFfbh32(inst, true);
|
||||
case RdnaOpcode::SFlbitI32B64:
|
||||
return sFlbitI32B64(inst);
|
||||
return sFlbitI32B64(inst, false);
|
||||
case RdnaOpcode::SFlbitI32I64:
|
||||
return sFlbitI32B64(inst, true);
|
||||
case RdnaOpcode::SBitset0B32:
|
||||
return sBitsetB32(inst, false);
|
||||
case RdnaOpcode::SBitset1B32:
|
||||
@@ -319,8 +334,10 @@ bool TranslationContext::emitScalar(const RdnaInstruction& inst) {
|
||||
return sBitsetB64(inst, true);
|
||||
case RdnaOpcode::SBitreplicateB64B32:
|
||||
return sBitreplicateB64B32(inst);
|
||||
case RdnaOpcode::SQuadmaskB32:
|
||||
return sQuadmask(inst, false);
|
||||
case RdnaOpcode::SQuadmaskB64:
|
||||
return sQuadmaskB64(inst);
|
||||
return sQuadmask(inst, true);
|
||||
case RdnaOpcode::SBfmB32:
|
||||
return bfmB32(inst);
|
||||
case RdnaOpcode::SBfmB64:
|
||||
|
||||
Reference in New Issue
Block a user