mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
feat(shader): implement s_getreg_b32 for the MODE round mode fields
SOPK 0x12 threw in the decoder. The recompiler emits round to nearest even, which s_setreg_imm32_b32 already requires, so reads that stay within MODE bits 0..3 return 0. Other hardware registers and fields throw with the register and bits read. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -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, "HW_ID");
|
||||
std::puts("s_getreg_b32 tests passed");
|
||||
return 0;
|
||||
} catch (const std::exception& error) {
|
||||
std::cerr << error.what() << '\n';
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -897,6 +897,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())));
|
||||
|
||||
Reference in New Issue
Block a user