mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
feat(shader): implement ds_permute_b32
ds_permute_b32 is the forward (scatter) counterpart of ds_bpermute_b32: every active lane sends its data to lane ((addr + offset) >> 2) & 31 of its wave. As measured on hardware, the highest sending lane wins, an active lane nobody writes to gets 0 and inactive lanes neither send nor receive. It lowers to one shuffle of the data, address and EXEC bit per source lane, using the same wave base as ds_bpermute_b32. Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,173 @@
|
||||
#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 = 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, 37> Code{
|
||||
0x34020084, 0x34060086, 0xe0301000, 0x80000401, 0xe0301004, 0x80000501, 0xbf8c3f70, 0x7e1402ff,
|
||||
0xdeaddead, 0x7e1602ff, 0xdeaddead, 0x7e1802ff, 0xdeaddead, 0x7e1a02ff, 0xdeaddead, 0xdac80000,
|
||||
0x0a000504, 0xdacc0000, 0x0b000504, 0xdac80008, 0x0c000504, 0xbe94037e, 0xbefe03ff, 0x0000ffff,
|
||||
0xdac80000, 0x0d000504, 0xbefe0314, 0xbf8cc07f, 0xe0701000, 0x80010a03, 0xe0701004, 0x80010b03,
|
||||
0xe0701008, 0x80010c03, 0xe070100c, 0x80010d03, 0xbf810000,
|
||||
};
|
||||
|
||||
constexpr std::uint32_t Rows[32][2] = {
|
||||
{0x000000f4u, 0x00001000u},
|
||||
{0x000000dcu, 0x00001001u},
|
||||
{0x000000f2u, 0x00001002u},
|
||||
{0x000000d9u, 0x00001003u},
|
||||
{0x0000000eu, 0x00001004u},
|
||||
{0x00000017u, 0x00001005u},
|
||||
{0x00000015u, 0x00001006u},
|
||||
{0x0000005cu, 0x00001007u},
|
||||
{0x000000d5u, 0x00001008u},
|
||||
{0x0000002bu, 0x00001009u},
|
||||
{0x000000bcu, 0x0000100au},
|
||||
{0x000000cfu, 0x0000100bu},
|
||||
{0x000000abu, 0x0000100cu},
|
||||
{0x000000dau, 0x0000100du},
|
||||
{0x0000004eu, 0x0000100eu},
|
||||
{0x00000040u, 0x0000100fu},
|
||||
{0x0000009bu, 0x00001010u},
|
||||
{0x00000036u, 0x00001011u},
|
||||
{0x0000009bu, 0x00001012u},
|
||||
{0x00000009u, 0x00001013u},
|
||||
{0x00000094u, 0x00001014u},
|
||||
{0x000000aeu, 0x00001015u},
|
||||
{0x00000028u, 0x00001016u},
|
||||
{0x000000ffu, 0x00001017u},
|
||||
{0x0000006eu, 0x00001018u},
|
||||
{0x000000a3u, 0x00001019u},
|
||||
{0x00000064u, 0x0000101au},
|
||||
{0x000000cdu, 0x0000101bu},
|
||||
{0x000000b9u, 0x0000101cu},
|
||||
{0x000000dcu, 0x0000101du},
|
||||
{0x000000feu, 0x0000101eu},
|
||||
{0x00000082u, 0x0000101fu},
|
||||
};
|
||||
constexpr std::uint32_t Expected[32][4] = {
|
||||
{0x0000101fu, 0x0000101du, 0x00000000u, 0x00000000u},
|
||||
{0x00000000u, 0x00001017u, 0x0000101eu, 0x00000000u},
|
||||
{0x00001013u, 0x0000101cu, 0x0000101fu, 0x00000000u},
|
||||
{0x00001004u, 0x00001016u, 0x00000000u, 0x00001004u},
|
||||
{0x00000000u, 0x00001003u, 0x00001013u, 0x00000000u},
|
||||
{0x00001014u, 0x00001005u, 0x00001004u, 0x00001006u},
|
||||
{0x00001012u, 0x00001005u, 0x00000000u, 0x00000000u},
|
||||
{0x00000000u, 0x00001017u, 0x00001014u, 0x00000000u},
|
||||
{0x00001019u, 0x00001015u, 0x00001012u, 0x00000000u},
|
||||
{0x00000000u, 0x0000100au, 0x00000000u, 0x00000000u},
|
||||
{0x00001016u, 0x0000100fu, 0x00001019u, 0x0000100cu},
|
||||
{0x00001015u, 0x00001013u, 0x00000000u, 0x00000000u},
|
||||
{0x00000000u, 0x0000100au, 0x00001016u, 0x00000000u},
|
||||
{0x00001011u, 0x00001016u, 0x00001015u, 0x00000000u},
|
||||
{0x0000101cu, 0x00001013u, 0x00000000u, 0x00000000u},
|
||||
{0x0000100au, 0x00001010u, 0x00001011u, 0x0000100au},
|
||||
{0x0000100fu, 0x00001006u, 0x0000101cu, 0xdeaddeadu},
|
||||
{0x00000000u, 0x0000100du, 0x0000100au, 0xdeaddeadu},
|
||||
{0x00000000u, 0x00001006u, 0x0000100fu, 0xdeaddeadu},
|
||||
{0x0000101bu, 0x00001002u, 0x00000000u, 0xdeaddeadu},
|
||||
{0x00000000u, 0x00001005u, 0x00000000u, 0xdeaddeadu},
|
||||
{0x00001008u, 0x0000100bu, 0x0000101bu, 0xdeaddeadu},
|
||||
{0x0000100du, 0x0000100au, 0x00000000u, 0xdeaddeadu},
|
||||
{0x0000101du, 0x0000101fu, 0x00001008u, 0xdeaddeadu},
|
||||
{0x00000000u, 0x0000101bu, 0x0000100du, 0xdeaddeadu},
|
||||
{0x0000101au, 0x00001008u, 0x0000101du, 0xdeaddeadu},
|
||||
{0x00000000u, 0x00001019u, 0x00000000u, 0xdeaddeadu},
|
||||
{0x00001018u, 0x00001013u, 0x0000101au, 0xdeaddeadu},
|
||||
{0x00001002u, 0x0000100eu, 0x00000000u, 0xdeaddeadu},
|
||||
{0x00001000u, 0x00001017u, 0x00001018u, 0xdeaddeadu},
|
||||
{0x00000000u, 0x0000101fu, 0x00001002u, 0xdeaddeadu},
|
||||
{0x0000101eu, 0x00001000u, 0x00001000u, 0xdeaddeadu},
|
||||
};
|
||||
constexpr const char* Names[4] = {"permute", "bpermute", "permute_offset8", "permute_exec_low16"};
|
||||
|
||||
void Fill(std::uint32_t tid, std::uint32_t* words) {
|
||||
words[0] = Rows[tid][0];
|
||||
words[1] = Rows[tid][1];
|
||||
}
|
||||
|
||||
std::array<std::uint32_t, 4> BufferDescriptor(const void* data, std::uint32_t bytes) {
|
||||
const auto address = reinterpret_cast<std::uintptr_t>(data);
|
||||
return {static_cast<std::uint32_t>(address), static_cast<std::uint32_t>((address >> 32u) & 0xffffu), bytes, 0x01016facu};
|
||||
}
|
||||
|
||||
std::string Hex(std::uint32_t value) {
|
||||
char text[16];
|
||||
std::snprintf(text, sizeof(text), "0x%08x", value);
|
||||
return text;
|
||||
}
|
||||
|
||||
void Expect(std::uint32_t tid, std::uint32_t actual, std::uint32_t expected, const char* name) {
|
||||
Require(actual == expected, std::string("ds permute: lane ") + std::to_string(tid) + " " + name + " is " + Hex(actual) + ", expected " + Hex(expected));
|
||||
}
|
||||
|
||||
void Run(AgcDriver::VulkanDevice& device) {
|
||||
for (std::uint32_t tid = 0; tid < Threads; ++tid) Fill(tid, &Input[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() * 4u));
|
||||
const auto output = BufferDescriptor(Output.data(), static_cast<std::uint32_t>(Output.size() * 4u));
|
||||
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(Code);
|
||||
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}, 0u, {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();
|
||||
}
|
||||
|
||||
void Check() {
|
||||
for (std::uint32_t tid = 0; tid < Threads; ++tid) {
|
||||
const std::uint32_t* in = &Input[tid * Inputs];
|
||||
const std::uint32_t* out = &Output[tid * Results];
|
||||
for (std::uint32_t index = 0; index < 4; ++index) Expect(tid, out[index], Expected[tid][index], Names[index]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main() {
|
||||
try {
|
||||
const auto device = OpenVulkanTestDevice();
|
||||
if (!device) return VulkanTestSkipped;
|
||||
if (device->Target().subgroupSize < Threads) {
|
||||
std::printf("skipped, the device's subgroups are narrower than a wave (%u lanes)\n", device->Target().subgroupSize);
|
||||
return VulkanTestSkipped;
|
||||
}
|
||||
Run(*device);
|
||||
Check();
|
||||
std::puts("ds permute tests passed");
|
||||
return 0;
|
||||
} catch (const std::exception& error) {
|
||||
std::cerr << error.what() << '\n';
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -53,6 +53,8 @@ std::uint32_t estimatedSpirvWords(const RdnaInstruction& instruction) {
|
||||
case RdnaOpcode::DsSwizzleB32:
|
||||
case RdnaOpcode::DsBpermuteB32:
|
||||
return 120u;
|
||||
case RdnaOpcode::DsPermuteB32:
|
||||
return 1200u;
|
||||
case RdnaOpcode::ImageBvhIntersectRay:
|
||||
return 2000u;
|
||||
default:
|
||||
|
||||
+1
@@ -249,6 +249,7 @@ enum class IrOpcode : std::uint16_t {
|
||||
WriteLane,
|
||||
Permlane16U32,
|
||||
BpermuteU32,
|
||||
PermuteU32,
|
||||
GetSrtResource,
|
||||
GetBufferResource,
|
||||
GetAddressResource,
|
||||
|
||||
@@ -290,6 +290,7 @@ constexpr std::array<OpcodeMeta, static_cast<std::size_t>(IrOpcode::Count)> Meta
|
||||
makeMeta("WriteLane", U32, U32, U32, U32),
|
||||
makeMeta("Permlane16U32", U32, U32, U32, U32, U1),
|
||||
makeMeta("BpermuteU32", U32, U32, U32, U1),
|
||||
makeMeta("PermuteU32", U32, U32, U32, U1),
|
||||
makeMeta("GetSrtResource", SrtResource),
|
||||
makeMeta("GetBufferResource", BufferResource, U32, U32, U32, U32),
|
||||
makeMeta("GetAddressResource", AddressResource, U32, U32),
|
||||
|
||||
@@ -710,6 +710,7 @@ enum class RdnaOpcode : std::uint16_t {
|
||||
DsMaxF32,
|
||||
DsSwizzleB32,
|
||||
DsBpermuteB32,
|
||||
DsPermuteB32,
|
||||
DsConsume,
|
||||
DsAppend,
|
||||
DsReadI8,
|
||||
|
||||
@@ -165,6 +165,7 @@ constexpr MemoryOpcodeInfo dsOpcodes[] = {
|
||||
{0xa7u, RdnaOpcode::DsReadU16D16Hi, 1, 16, false, false, false},
|
||||
{0xb0u, RdnaOpcode::DsWriteAddtidB32, 1, 32, false, false, false},
|
||||
{0xb1u, RdnaOpcode::DsReadAddtidB32, 1, 32, false, false, false},
|
||||
{0xb2u, RdnaOpcode::DsPermuteB32, 1, 32, false, false, false},
|
||||
{0xb3u, RdnaOpcode::DsBpermuteB32, 1, 32, false, false, false},
|
||||
{0xdeu, RdnaOpcode::DsWriteB96, 3, 32, false, false, false},
|
||||
{0xdfu, RdnaOpcode::DsWriteB128, 4, 32, false, false, false},
|
||||
@@ -346,6 +347,7 @@ std::uint32_t dsSourceCount(RdnaOpcode opcode) {
|
||||
case RdnaOpcode::DsMinF32:
|
||||
case RdnaOpcode::DsMaxF32: return 2u;
|
||||
case RdnaOpcode::DsNop: return 0u;
|
||||
case RdnaOpcode::DsPermuteB32:
|
||||
case RdnaOpcode::DsBpermuteB32: return 2u;
|
||||
case RdnaOpcode::DsReadAddtidB32:
|
||||
case RdnaOpcode::DsConsume:
|
||||
@@ -621,7 +623,7 @@ RdnaInstruction DecodeRdnaDs(std::uint32_t programCounter, std::span<const std::
|
||||
if (info.opcode == RdnaOpcode::DsSwizzleB32 && combinedOffset >= 0xE000u) {
|
||||
throw std::runtime_error("DS swizzle FFT mode is not supported");
|
||||
}
|
||||
if (gds && (info.opcode == RdnaOpcode::DsSwizzleB32 || info.opcode == RdnaOpcode::DsBpermuteB32 || info.opcode == RdnaOpcode::DsWriteAddtidB32 || info.opcode == RdnaOpcode::DsReadAddtidB32)) {
|
||||
if (gds && (info.opcode == RdnaOpcode::DsSwizzleB32 || info.opcode == RdnaOpcode::DsBpermuteB32 || info.opcode == RdnaOpcode::DsPermuteB32 || info.opcode == RdnaOpcode::DsWriteAddtidB32 || info.opcode == RdnaOpcode::DsReadAddtidB32)) {
|
||||
throw std::runtime_error("DS lane operation is available only for LDS");
|
||||
}
|
||||
if (info.opcode == RdnaOpcode::DsWriteAddtidB32 && data1 != 0u) {
|
||||
|
||||
@@ -92,6 +92,7 @@ void EmitSharedFloatAtomic(SpirvValueEmitContext& ctx, const IrValue& inst);
|
||||
std::uint32_t EmitSharedIncDec(SpirvValueEmitContext& ctx, const IrValue& inst);
|
||||
std::uint32_t EmitAppendConsume(SpirvValueEmitContext& ctx, const IrValue& inst);
|
||||
std::uint32_t EmitBpermuteU32(SpirvValueEmitContext& ctx, const IrValue& inst);
|
||||
std::uint32_t EmitPermuteU32(SpirvValueEmitContext& ctx, const IrValue& inst);
|
||||
std::uint32_t EmitReadConst(SpirvValueEmitContext& ctx, const IrValue& inst);
|
||||
void EmitReadConstBuffer(SpirvValueEmitContext& ctx, const IrValue& inst);
|
||||
std::uint32_t EmitSwizzleU32(SpirvValueEmitContext& ctx, const IrValue& inst);
|
||||
|
||||
@@ -603,6 +603,24 @@ std::uint32_t EmitBpermuteU32(SpirvValueEmitContext& ctx, const IrValue& inst) {
|
||||
return EmitDsMaskedLaneRead(state, source, target, ctx.Arg(inst, 2));
|
||||
}
|
||||
|
||||
std::uint32_t EmitPermuteU32(SpirvValueEmitContext& ctx, const IrValue& inst) {
|
||||
auto& state = ctx.state;
|
||||
const auto lane = EmitSubgroupLocalInvocationId(state);
|
||||
const auto base = Binary(state, spv::OpBitwiseAnd, TypeU32(state), lane, ConstantU32(state, ~31u));
|
||||
const auto own = Binary(state, spv::OpBitwiseAnd, TypeU32(state), lane, ConstantU32(state, 31u));
|
||||
auto result = ConstantU32(state, 0u);
|
||||
for (std::uint32_t source = 0; source < 32u; ++source) {
|
||||
const auto from = Binary(state, spv::OpBitwiseOr, TypeU32(state), base, ConstantU32(state, source));
|
||||
const auto value = ctx.Shuffle(inst, 0, from);
|
||||
const auto address = ctx.Shuffle(inst, 1, from);
|
||||
const auto active = ctx.Shuffle(inst, 2, from);
|
||||
const auto target = Binary(state, spv::OpBitwiseAnd, TypeU32(state), Binary(state, spv::OpShiftRightLogical, TypeU32(state), address, ConstantU32(state, 2u)), ConstantU32(state, 31u));
|
||||
const auto hit = Binary(state, spv::OpLogicalAnd, TypeBool(state), active, Binary(state, spv::OpIEqual, TypeBool(state), target, own));
|
||||
result = Select(state, TypeU32(state), hit, value, result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::uint32_t EmitSwizzleU32(SpirvValueEmitContext& ctx, const IrValue& inst) {
|
||||
auto& state = ctx.state;
|
||||
state.module.AddFunction(spv::OpStore, ctx.scratchU32Variable, ctx.Arg(inst, 0));
|
||||
|
||||
@@ -154,6 +154,7 @@ SpirvRequirements AnalyzeProgramRequirements(const IrProgram& program) {
|
||||
break;
|
||||
}
|
||||
case IrOpcode::SwizzleU32:
|
||||
case IrOpcode::PermuteU32:
|
||||
case IrOpcode::BpermuteU32: {
|
||||
requirements.subgroupBallot = true;
|
||||
requirements.subgroupShuffle = true;
|
||||
|
||||
@@ -423,6 +423,7 @@ void EmitDirectInstruction(SpirvValueEmitContext& ctx, const IrValue& inst) {
|
||||
case IrOpcode::WriteLane: return Invoke(EmitWriteLane, ctx, inst);
|
||||
case IrOpcode::Permlane16U32: return Invoke(EmitPermlane16U32, ctx, inst);
|
||||
case IrOpcode::BpermuteU32: return Invoke(EmitBpermuteU32, ctx, inst);
|
||||
case IrOpcode::PermuteU32: return Invoke(EmitPermuteU32, ctx, inst);
|
||||
case IrOpcode::GetSrtResource: return Invoke(EmitGetSrtResource, ctx, inst);
|
||||
case IrOpcode::GetBufferResource: return Invoke(EmitGetBufferResource, ctx, inst);
|
||||
case IrOpcode::GetAddressResource: return Invoke(EmitGetAddressResource, ctx, inst);
|
||||
|
||||
@@ -109,6 +109,7 @@ private:
|
||||
bool dsAddtid(const RdnaInstruction& inst, bool write);
|
||||
bool dsSwizzleB32(const RdnaInstruction& inst);
|
||||
bool dsBpermuteB32(const RdnaInstruction& inst);
|
||||
bool dsPermuteB32(const RdnaInstruction& inst);
|
||||
IrF32 selectF32(IrU1 condition, IrF32 trueValue, IrF32 falseValue);
|
||||
IrU32 convertF32ToU32Saturated(IrF32 value, float upperBound, float safeUpper, std::uint32_t highResult);
|
||||
IrU32 convertF32ToI32Saturated(IrF32 value, float lowerBound, float upperBound, float safeUpper, std::uint32_t lowerResult, std::uint32_t upperResult);
|
||||
|
||||
+2
@@ -211,6 +211,8 @@ bool TranslationContext::emitMemory(const RdnaInstruction& inst) {
|
||||
return dsSwizzleB32(inst);
|
||||
case RdnaOpcode::DsBpermuteB32:
|
||||
return dsBpermuteB32(inst);
|
||||
case RdnaOpcode::DsPermuteB32:
|
||||
return dsPermuteB32(inst);
|
||||
case RdnaOpcode::DsConsume:
|
||||
return dsAppendConsume(inst, IrOpcode::DataConsume);
|
||||
case RdnaOpcode::DsAppend:
|
||||
|
||||
+10
@@ -228,4 +228,14 @@ bool TranslationContext::dsBpermuteB32(const RdnaInstruction& inst) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TranslationContext::dsPermuteB32(const RdnaInstruction& inst) {
|
||||
const IrU32 index = readU32(inst.source0);
|
||||
IrValue& address = ir.IAdd(index.Value(), ir.Constant(inst.memoryOffset));
|
||||
const IrU32 value = readU32(inst.source1);
|
||||
IrValue& active = ir.GetExec();
|
||||
IrValue& result = ir.Emit(IrOpcode::PermuteU32, IrOpcodeType(IrOpcode::PermuteU32), {&value.Value(), &address, &active});
|
||||
writeOperand(inst.destination, &result);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user