Merge pull request #577 from CnRJay/feat/s-getreg-mode

feat(shader): implement s_getreg_b32 for the MODE round mode fields
This commit is contained in:
Dean
2026-10-05 14:29:24 +13:00
committed by GitHub
6 changed files with 121 additions and 1 deletions
@@ -0,0 +1,102 @@
#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 <cstdint>
#include <cstdio>
#include <iostream>
#include <span>
#include <string>
#include <vector>
namespace {
using AgcDriver::Graphics::Require;
using ShaderRecompiler::ShaderStage;
constexpr std::uint32_t Threads = 32;
constexpr std::uint32_t Results = 16;
alignas(256) std::array<std::uint32_t, Threads * Results> Output{};
alignas(256) constexpr std::array<std::uint32_t, 14> Code{
0x34060086, 0xbe9403ff, 0x55555555, 0xbe9503ff, 0x55555555, 0xb9141801, 0xb9150881, 0x7e140214,
0x7e160215, 0xe0701000, 0x80010a03, 0xe0701004, 0x80010b03, 0xbf810000,
};
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;
}
ShaderRecompiler::RecompileRequest Request(AgcDriver::VulkanDevice& device, std::span<const std::uint32_t> code, std::span<const std::uint32_t> userData, std::span<const ShaderRecompiler::MemoryRegion> memory) {
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;
return request;
}
void ExpectRefused(AgcDriver::VulkanDevice& device, std::uint32_t word, const char* what) {
alignas(256) const std::array<std::uint32_t, 2> code{word, 0xbf810000u};
const std::array<ShaderRecompiler::MemoryRegion, 1> memory{{{reinterpret_cast<std::uintptr_t>(code.data()), std::as_bytes(std::span<const std::uint32_t>(code))}}};
const std::vector<std::uint32_t> userData(8, 0u);
std::string refusal;
try {
static_cast<void>(ShaderRecompiler::Recompile(Request(device, code, userData, memory)));
} catch (const std::exception& error) {
refusal = error.what();
}
Require(refusal.find("only the MODE round mode fields are modeled") != std::string::npos, std::string("s_getreg_b32 reading ") + what + " was not refused");
}
void Run(AgcDriver::VulkanDevice& device) {
Output.fill(0xdeadbeefu);
std::vector<std::uint32_t> userData(8, 0u);
const auto output = BufferDescriptor(Output.data(), static_cast<std::uint32_t>(Output.size() * 4u));
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 auto result = ShaderRecompiler::Recompile(Request(device, code, userData, memory));
device.Dispatch(result, 1, 1, 1, {}, reinterpret_cast<std::uintptr_t>(code.data()));
device.WaitIdle();
}
void Check() {
constexpr std::array<const char*, 2> names{"hwreg(MODE, 0, 4)", "hwreg(MODE, 2, 2)"};
for (std::uint32_t tid = 0; tid < Threads; ++tid) {
for (std::uint32_t index = 0; index < names.size(); ++index) {
const std::uint32_t actual = Output[tid * Results + index];
Require(actual == 0u, std::string("s_getreg_b32: lane ") + std::to_string(tid) + " " + names[index] + " is " + Hex(actual) + ", expected 0x00000000");
}
}
}
}
int main() {
try {
const auto device = OpenVulkanTestDevice();
if (!device) return VulkanTestSkipped;
Run(*device);
Check();
ExpectRefused(*device, 0xb9141901u, "the MODE denormal fields");
ExpectRefused(*device, 0xb914f804u, "hardware register 4");
std::puts("s_getreg_b32 tests passed");
return 0;
} catch (const std::exception& error) {
std::cerr << error.what() << '\n';
return 1;
}
}
@@ -934,6 +934,7 @@ enum class RdnaOpcode : std::uint16_t {
VInterpMovF32,
SWaitcntDepctr,
SSendmsg,
SGetregB32,
SSetregB32,
SSetregImm32B32,
SCmovkI32,
@@ -175,6 +175,7 @@ RdnaOpcode decodeSopkOpcode(std::uint32_t opcode) {
case 0x0eu: return RdnaOpcode::SCmpLeU32;
case 0x0fu: return RdnaOpcode::SAddI32;
case 0x10u: return RdnaOpcode::SMulkI32;
case 0x12u: return RdnaOpcode::SGetregB32;
case 0x13u: return RdnaOpcode::SSetregB32;
case 0x15u: return RdnaOpcode::SSetregImm32B32;
case 0x17u: return RdnaOpcode::SWaitcnt;
@@ -417,6 +418,11 @@ RdnaInstruction DecodeRdnaSopk(std::uint32_t programCounter, std::span<const std
instruction.sourceCount = 1;
return instruction;
}
if (instruction.op == RdnaOpcode::SGetregB32) {
instruction.destination = DecodeRdnaScalarDestination(scalarRegister, programCounter);
instruction.source0.value = word & 0xffffu;
return instruction;
}
if (instruction.op == RdnaOpcode::SSetregB32) {
instruction.destination.kind = RdnaOperandKind::Null;
instruction.source1.kind = RdnaOperandKind::LiteralConstant;
@@ -319,6 +319,7 @@ EmbeddedFetchPlan EmbeddedVertexFetchAnalyzer::Analyze(const RdnaProgram& progra
dst.constant = inst.source0.value;
}
break;
case RdnaOpcode::SGetregB32:
case RdnaOpcode::SCmovkI32:
if (isScalarOperand(inst.destination)) {
clearScalarRange(sgprs, inst.destination, 1u);
@@ -66,6 +66,16 @@ bool TranslationContext::emitScalar(const RdnaInstruction& inst) {
emitControlNop();
return true;
}
case RdnaOpcode::SGetregB32: {
const std::uint32_t field = inst.source0.value;
const std::uint32_t offset = (field >> 6u) & 0x1fu;
const std::uint32_t size = ((field >> 11u) & 0x1fu) + 1u;
if ((field & 0x3fu) != 1u || offset + size > 4u) {
throw std::runtime_error("s_getreg_b32 at pc " + std::to_string(inst.programCounter) + " reads hardware register " + std::to_string(field & 0x3fu) + " bits " + std::to_string(offset) + ".." + std::to_string(offset + size - 1u) + ": only the MODE round mode fields are modeled");
}
writeRawU32(inst.destination, IrU32(ir.Constant(0u)));
return true;
}
case RdnaOpcode::SCmovkI32: {
const IrU32 previous = readU32(inst.destination);
writeRawU32(inst.destination, IrU32(ir.Select(ir.GetScc(), ir.Constant(inst.source0.value), previous.Value())));
+1 -1
View File
@@ -72,7 +72,7 @@ Throughout the project, every function at every stage either **does exactly what
### Functional
- The shader recompiler [ignores `s_setreg_b32`](../../core/shader/recompiler/Translation/src/ScalarInstructions.cpp): writes to MODE (float rounding and denormal controls) and the other hardware registers have no effect.
- The shader recompiler [ignores `s_setreg_b32`](../../core/shader/recompiler/Translation/src/ScalarInstructions.cpp): writes to MODE (float rounding and denormal controls) and the other hardware registers have no effect, and `s_getreg_b32` reads of the MODE round fields report round to nearest even even after `s_setreg_b32` wrote another mode. The initial round mode from the shader's program registers is not read either.
- [Shader recompilation](../../core/shader/recompiler/Recompiler.cpp) currently occurs right before it was transferred to Vulkan with caching, but should be moved to the [relinker](../../core/relinker/main.cpp) stage. For this purpose, [shader/recompiler](../../core/shader/recompiler) was written completely independently from [libs/prx](../../core/libs/prx).
- [v_fma_f64](../../core/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterFloat64.cpp) is fused exactly only for normal inputs: with a subnormal input it is a separate multiply and add, which rounds twice.
- The executable file that [relinker](../../core/relinker/elfpatcher/src/windows/WindowsPeWriter.cpp) generates opens the console when launched, which is inconvenient for playability.