diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/DsPermute.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/DsPermute.cpp new file mode 100644 index 000000000..59177cf64 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/DsPermute.cpp @@ -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 +#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 = 4; +constexpr std::uint32_t Results = 16; +alignas(256) std::array Input{}; +alignas(256) std::array Output{}; + +alignas(256) constexpr std::array 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 BufferDescriptor(const void* data, std::uint32_t bytes) { + const auto address = reinterpret_cast(data); + return {static_cast(address), static_cast((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 userData(8, 0u); + const auto input = BufferDescriptor(Input.data(), static_cast(Input.size() * 4u)); + const auto output = BufferDescriptor(Output.data(), static_cast(Output.size() * 4u)); + std::copy(input.begin(), input.end(), userData.begin()); + std::copy(output.begin(), output.end(), userData.begin() + 4); + const std::span code(Code); + const std::array memory{{{reinterpret_cast(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(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(); +} + +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; + } +} diff --git a/core/shader/recompiler/ControlFlow/src/GraphBuilder.cpp b/core/shader/recompiler/ControlFlow/src/GraphBuilder.cpp index aa8f07281..9d9ef50cb 100644 --- a/core/shader/recompiler/ControlFlow/src/GraphBuilder.cpp +++ b/core/shader/recompiler/ControlFlow/src/GraphBuilder.cpp @@ -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: diff --git a/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrOpcode.hpp b/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrOpcode.hpp index 5c9d1bc1d..2e69db9ca 100644 --- a/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrOpcode.hpp +++ b/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrOpcode.hpp @@ -249,6 +249,7 @@ enum class IrOpcode : std::uint16_t { WriteLane, Permlane16U32, BpermuteU32, + PermuteU32, GetSrtResource, GetBufferResource, GetAddressResource, diff --git a/core/shader/recompiler/IntermediateRepresentation/src/IrOpcode.cpp b/core/shader/recompiler/IntermediateRepresentation/src/IrOpcode.cpp index 7edb0ef10..56b68a966 100644 --- a/core/shader/recompiler/IntermediateRepresentation/src/IrOpcode.cpp +++ b/core/shader/recompiler/IntermediateRepresentation/src/IrOpcode.cpp @@ -290,6 +290,7 @@ constexpr std::array(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), diff --git a/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp b/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp index 65ae52da9..3e25e2885 100644 --- a/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp +++ b/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp @@ -710,6 +710,7 @@ enum class RdnaOpcode : std::uint16_t { DsMaxF32, DsSwizzleB32, DsBpermuteB32, + DsPermuteB32, DsConsume, DsAppend, DsReadI8, diff --git a/core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp b/core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp index f5ebd6200..18c388df8 100644 --- a/core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp +++ b/core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp @@ -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= 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) { diff --git a/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterInstructions.hpp b/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterInstructions.hpp index ad70a7343..5168ca80d 100644 --- a/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterInstructions.hpp +++ b/core/shader/recompiler/SpirvBackend/include/SpirvBackend/SpirvEmitterInstructions.hpp @@ -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); diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cpp index adf77d248..8ed46f17e 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterMath.cpp @@ -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)); diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvAnalysis.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvAnalysis.cpp index 67b56daf9..62304fcf9 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvAnalysis.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvAnalysis.cpp @@ -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; diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp index 5bb992ec5..f92f674d0 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvFlowEmitter.cpp @@ -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); diff --git a/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp b/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp index 00f729539..2e0d481b0 100644 --- a/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp +++ b/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp @@ -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); diff --git a/core/shader/recompiler/Translation/src/MemoryInstructions/MemoryInstructionDispatch.cpp b/core/shader/recompiler/Translation/src/MemoryInstructions/MemoryInstructionDispatch.cpp index 90017a870..7478c8e8f 100644 --- a/core/shader/recompiler/Translation/src/MemoryInstructions/MemoryInstructionDispatch.cpp +++ b/core/shader/recompiler/Translation/src/MemoryInstructions/MemoryInstructionDispatch.cpp @@ -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: diff --git a/core/shader/recompiler/Translation/src/MemoryInstructions/SharedMemoryInstructions.cpp b/core/shader/recompiler/Translation/src/MemoryInstructions/SharedMemoryInstructions.cpp index b460bcd96..019d6596a 100644 --- a/core/shader/recompiler/Translation/src/MemoryInstructions/SharedMemoryInstructions.cpp +++ b/core/shader/recompiler/Translation/src/MemoryInstructions/SharedMemoryInstructions.cpp @@ -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; +} + }