mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
feat(shader): implement s_get_waveid_in_workgroup
SMEM opcode 0x2a threw in the decoder. It carries only a scalar destination and returns the index of the current wave in its workgroup. A compute workgroup fills its waves in local invocation index order, the mapping the compute prologue already uses for the tg_size wave id, so the result is the local invocation index divided by the wave size. Other stages throw. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -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 <array>
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
|
||||
using AgcDriver::Graphics::Require;
|
||||
using ShaderRecompiler::ShaderStage;
|
||||
|
||||
constexpr std::uint32_t MaxThreads = 128;
|
||||
constexpr std::uint32_t Untouched = 0xdeadbeefu;
|
||||
alignas(256) std::array<std::uint32_t, MaxThreads> Output{};
|
||||
|
||||
alignas(256) constexpr std::array<std::uint32_t, 10> WaveIdCode{
|
||||
0x34060283, 0x4a060700, 0x34080486, 0x4a060704, 0xf4a80100, 0x00000000, 0x7e080204, 0xe0702000,
|
||||
0x80000403, 0xbf810000,
|
||||
};
|
||||
|
||||
struct Workgroup {
|
||||
std::array<std::uint32_t, 3> 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<Workgroup, 6> 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<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, const Workgroup& workgroup) {
|
||||
Output.fill(Untouched);
|
||||
const auto output = BufferDescriptor(Output.data(), static_cast<std::uint32_t>(Output.size()));
|
||||
const std::vector<std::uint32_t> userData(output.begin(), output.end());
|
||||
const std::span<const std::uint32_t> code(WaveIdCode);
|
||||
const std::array<ShaderRecompiler::MemoryRegion, 1> memory{{{reinterpret_cast<std::uintptr_t>(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<std::uintptr_t>(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<std::uintptr_t>(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<std::uint32_t, 3> vertexCode{0xf4a80100, 0x00000000, 0xbf810000};
|
||||
const std::vector<std::uint32_t> userData(8, 0u);
|
||||
const std::span<const std::uint32_t> code(vertexCode);
|
||||
const std::array<ShaderRecompiler::MemoryRegion, 1> memory{{{reinterpret_cast<std::uintptr_t>(code.data()), std::as_bytes(code)}}};
|
||||
ShaderRecompiler::RecompileRequest request{
|
||||
{ShaderStage::Vertex, reinterpret_cast<std::uintptr_t>(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<void>(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;
|
||||
}
|
||||
}
|
||||
@@ -537,6 +537,7 @@ enum class RdnaOpcode : std::uint16_t {
|
||||
SBufferLoadDwordx4,
|
||||
SBufferLoadDwordx8,
|
||||
SBufferLoadDwordx16,
|
||||
SGetWaveidInWorkgroup,
|
||||
BufferStoreFormatXy,
|
||||
BufferStoreFormatXyz,
|
||||
BufferStoreFormatXyzw,
|
||||
|
||||
@@ -397,6 +397,11 @@ RdnaInstruction DecodeRdnaSmem(std::uint32_t programCounter, std::span<const std
|
||||
const auto sdst = (word0 >> 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);
|
||||
}
|
||||
|
||||
+9
@@ -1,6 +1,7 @@
|
||||
#include "Translation/MemoryInstructions.hpp"
|
||||
#include "Translation/TranslationContext.hpp"
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
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<std::uint32_t>(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:
|
||||
|
||||
Reference in New Issue
Block a user