diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/WaveIdInWorkgroup.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/WaveIdInWorkgroup.cpp new file mode 100644 index 000000000..e866b7be4 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/WaveIdInWorkgroup.cpp @@ -0,0 +1,111 @@ +#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 + +namespace { + +using AgcDriver::Graphics::Require; +using ShaderRecompiler::ShaderStage; + +constexpr std::uint32_t MaxThreads = 128; +constexpr std::uint32_t Untouched = 0xdeadbeefu; +alignas(256) std::array Output{}; + +alignas(256) constexpr std::array WaveIdCode{ + 0x34060283, 0x4a060700, 0x34080486, 0x4a060704, 0xf4a80100, 0x00000000, 0x7e080204, 0xe0702000, + 0x80000403, 0xbf810000, +}; + +struct Workgroup { + std::array threads; + std::uint32_t waveSize; + + [[nodiscard]] std::uint32_t ThreadCount() const { + return threads[0] * threads[1] * threads[2]; + } + [[nodiscard]] std::string Name() const { + return "wave" + std::to_string(waveSize) + " " + std::to_string(threads[0]) + "x" + std::to_string(threads[1]) + "x" + std::to_string(threads[2]); + } +}; + +constexpr std::array Workgroups{{ + {{128, 1, 1}, 32}, {{128, 1, 1}, 64}, {{8, 8, 2}, 32}, {{8, 8, 2}, 64}, {{8, 13, 1}, 32}, {{8, 13, 1}, 64}, +}}; + +std::array BufferDescriptor(const void* data, std::uint32_t count) { + const auto address = reinterpret_cast(data); + return {static_cast(address), static_cast((address >> 32u) & 0xffffu) | (4u << 16u), count, 0x01016facu}; +} + +void Run(AgcDriver::VulkanDevice& device, const Workgroup& workgroup) { + Output.fill(Untouched); + const auto output = BufferDescriptor(Output.data(), static_cast(Output.size())); + const std::vector userData(output.begin(), output.end()); + const std::span code(WaveIdCode); + const std::array memory{{{reinterpret_cast(code.data()), std::as_bytes(code)}}}; + const ShaderRecompiler::ShaderComputeStageInfo compute{workgroup.threads, 0, {false, false, false}, false, 3}; + ShaderRecompiler::RecompileRequest request{ + {ShaderStage::Compute, reinterpret_cast(code.data()), code, 0, {}}, + {workgroup.waveSize, 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(const Workgroup& workgroup) { + for (std::uint32_t tid = 0; tid < MaxThreads; ++tid) { + const auto expected = tid < workgroup.ThreadCount() ? tid / workgroup.waveSize : Untouched; + const auto actual = Output[tid]; + Require(actual == expected, "wave id in workgroup: " + workgroup.Name() + " thread " + std::to_string(tid) + " is " + std::to_string(actual) + ", expected " + std::to_string(expected)); + } +} + +void CheckRejectedOutsideCompute(const AgcDriver::VulkanDevice& device) { + alignas(256) static constexpr std::array vertexCode{0xf4a80100, 0x00000000, 0xbf810000}; + const std::vector userData(8, 0u); + const std::span code(vertexCode); + const std::array memory{{{reinterpret_cast(code.data()), std::as_bytes(code)}}}; + ShaderRecompiler::RecompileRequest request{ + {ShaderStage::Vertex, reinterpret_cast(code.data()), code, 0, {}}, + {64, 0, userData, std::nullopt, std::nullopt, ShaderRecompiler::ShaderVertexStageInfo{}, memory}, + device.Target(), + {0, 0, 0, 128} + }; + request.useCache = false; + std::string failure; + try { + static_cast(ShaderRecompiler::Recompile(request)); + } catch (const std::exception& error) { + failure = error.what(); + } + Require(failure.find("s_get_waveid_in_workgroup is supported only in compute shaders") != std::string::npos, "wave id in workgroup: a vertex shader was not rejected, got '" + failure + "'"); +} + +} + +int main() { + try { + const auto device = OpenVulkanTestDevice(); + if (!device) return VulkanTestSkipped; + for (const auto& workgroup : Workgroups) { + Run(*device, workgroup); + Check(workgroup); + } + CheckRejectedOutsideCompute(*device); + std::puts("wave id in workgroup tests passed"); + return 0; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; + } +} diff --git a/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp b/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp index a1e040990..c2e207b5e 100644 --- a/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp +++ b/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp @@ -537,6 +537,7 @@ enum class RdnaOpcode : std::uint16_t { SBufferLoadDwordx4, SBufferLoadDwordx8, SBufferLoadDwordx16, + SGetWaveidInWorkgroup, BufferStoreFormatXy, BufferStoreFormatXyz, BufferStoreFormatXyzw, diff --git a/core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp b/core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp index 068c2947e..991153a29 100644 --- a/core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp +++ b/core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp @@ -397,6 +397,11 @@ RdnaInstruction DecodeRdnaSmem(std::uint32_t programCounter, std::span> 6u) & 0x7Fu; const auto sbase = word0 & 0x3Fu; const auto soffsetCode = (word1 >> 25u) & 0x7Fu; + if (opcode == 0x2au) { + auto instruction = cacheControlInstruction(RdnaInstructionFamily::SMEM, RdnaOpcode::SGetWaveidInWorkgroup, opcode, programCounter, code, wordIndex); + instruction.destination = scalarDestination(sdst); + return instruction; + } if (const auto cacheOp = cacheControlOpcode(RdnaInstructionFamily::SMEM, opcode); cacheOp != RdnaOpcode::Invalid) { return cacheControlInstruction(RdnaInstructionFamily::SMEM, cacheOp, opcode, programCounter, code, wordIndex); } diff --git a/core/shader/recompiler/Translation/src/MemoryInstructions/MemoryInstructionDispatch.cpp b/core/shader/recompiler/Translation/src/MemoryInstructions/MemoryInstructionDispatch.cpp index f7f7c3af6..ab22018c1 100644 --- a/core/shader/recompiler/Translation/src/MemoryInstructions/MemoryInstructionDispatch.cpp +++ b/core/shader/recompiler/Translation/src/MemoryInstructions/MemoryInstructionDispatch.cpp @@ -1,6 +1,7 @@ #include "Translation/MemoryInstructions.hpp" #include "Translation/TranslationContext.hpp" #include +#include namespace ShaderRecompiler { @@ -30,6 +31,14 @@ bool TranslationContext::emitMemory(const RdnaInstruction& inst) { case RdnaOpcode::SBufferLoadDwordx8: case RdnaOpcode::SBufferLoadDwordx16: return sLoad(inst, false); + case RdnaOpcode::SGetWaveidInWorkgroup: { + if (program.Resources().stage != IrShaderStage::Compute) { + throw std::runtime_error("s_get_waveid_in_workgroup is supported only in compute shaders, at pc " + std::to_string(inst.programCounter)); + } + IrValue& localIndex = ir.Emit(IrOpcode::GetBuiltin, IrOpcodeType(IrOpcode::GetBuiltin), {&ir.Constant(static_cast(StageInputKind::LocalInvocationIndex)), &ir.Constant(0u)}); + writeOperand(inst.destination, &ir.Emit(IrOpcode::UDiv32, IrOpcodeType(IrOpcode::UDiv32), {&localIndex, &ir.Constant(program.WaveSize())})); + return true; + } case RdnaOpcode::BufferLoadFormatX: case RdnaOpcode::BufferLoadFormatXy: