From f994cc6ab7e8c27ed2fe1998eef51c2ea66c4817 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Franch?= Date: Fri, 2 Oct 2026 08:52:42 +0200 Subject: [PATCH 1/2] feat(shader): implement s_cmovk_i32, s_version and s_setreg_imm32_b32 s_cmovk_i32 writes the sign-extended 16-bit immediate when SCC is set. s_version is a no-op marker. s_setreg_imm32_b32 takes its value from a literal dword and is ignored like s_setreg_b32; both are now listed as silent stubs in TechnicalDebt. Co-Authored-By: Claude --- .../tests/execution/ScalarSopkMisc.cpp | 98 +++++++++++++++++++ .../include/RdnaDecoder/RdnaOpcode.hpp | 3 + .../recompiler/RdnaDecoder/src/RdnaOpcode.cpp | 1 + .../RdnaDecoder/src/RdnaScalarOpDecoder.cpp | 18 +++- .../Translation/src/EmbeddedVertexFetch.cpp | 5 + .../Translation/src/ScalarInstructions.cpp | 7 ++ docs/dev/TechnicalDebt.md | 1 + 7 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 core/libs/prx/libSceAgcDriver/tests/execution/ScalarSopkMisc.cpp diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/ScalarSopkMisc.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/ScalarSopkMisc.cpp new file mode 100644 index 000000000..2d81e026e --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/ScalarSopkMisc.cpp @@ -0,0 +1,98 @@ +#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{ + 0x34060086, 0xbe9403ff, 0x11111111, 0xbe9503ff, 0x22222222, 0xbe9703ff, 0x33333333, 0xbf068080, + 0xb1148001, 0xbf068180, 0xb1157fff, 0xbf068080, 0xb1171234, 0xb0801234, 0xba801801, 0x0000000f, + 0xbe9603ff, 0x44444444, 0x7e140214, 0x7e160215, 0x7e180216, 0x7e1a0217, 0xe0701000, 0x80010a03, + 0xe0701004, 0x80010b03, 0xe0701008, 0x80010c03, 0xe070100c, 0x80010d03, 0xbf810000, +}; + +constexpr std::uint32_t Expected[4] = {0xffff8001u, 0x22222222u, 0x44444444u, 0x00001234u}; +constexpr const char* Names[4] = {"cmovk_taken", "cmovk_not_taken", "after_setreg_imm32", "cmovk_positive"}; + +void Fill(std::uint32_t, std::uint32_t*) {} + +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("scalar sopk misc: 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[index], Names[index]); + } +} + +} + +int main() { + try { + const auto device = OpenVulkanTestDevice(); + if (!device) return VulkanTestSkipped; + Run(*device); + Check(); + std::puts("scalar sopk misc 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 65ae52da9..d54e339d8 100644 --- a/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp +++ b/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp @@ -801,6 +801,9 @@ enum class RdnaOpcode : std::uint16_t { SWaitcntDepctr, SSendmsg, SSetregB32, + SSetregImm32B32, + SCmovkI32, + SVersion, SSleep, SWakeup, STrap, diff --git a/core/shader/recompiler/RdnaDecoder/src/RdnaOpcode.cpp b/core/shader/recompiler/RdnaDecoder/src/RdnaOpcode.cpp index 01506a024..bf38596f2 100644 --- a/core/shader/recompiler/RdnaDecoder/src/RdnaOpcode.cpp +++ b/core/shader/recompiler/RdnaDecoder/src/RdnaOpcode.cpp @@ -19,6 +19,7 @@ bool IsScalarAluOpcode(RdnaOpcode opcode) { case RdnaOpcode::SMovB32: case RdnaOpcode::SMovB64: case RdnaOpcode::SMovkI32: + case RdnaOpcode::SCmovkI32: case RdnaOpcode::SAddI32: case RdnaOpcode::SAddU32: case RdnaOpcode::SSubI32: diff --git a/core/shader/recompiler/RdnaDecoder/src/RdnaScalarOpDecoder.cpp b/core/shader/recompiler/RdnaDecoder/src/RdnaScalarOpDecoder.cpp index 17ec5a2cc..f5564a590 100644 --- a/core/shader/recompiler/RdnaDecoder/src/RdnaScalarOpDecoder.cpp +++ b/core/shader/recompiler/RdnaDecoder/src/RdnaScalarOpDecoder.cpp @@ -159,6 +159,8 @@ RdnaOpcode decodeSopcOpcode(std::uint32_t opcode) { RdnaOpcode decodeSopkOpcode(std::uint32_t opcode) { switch (opcode) { case 0x00u: return RdnaOpcode::SMovkI32; + case 0x01u: return RdnaOpcode::SVersion; + case 0x02u: return RdnaOpcode::SCmovkI32; case 0x03u: return RdnaOpcode::SCmpEqI32; case 0x04u: return RdnaOpcode::SCmpLgI32; case 0x05u: return RdnaOpcode::SCmpGtI32; @@ -174,6 +176,7 @@ RdnaOpcode decodeSopkOpcode(std::uint32_t opcode) { case 0x0fu: return RdnaOpcode::SAddI32; case 0x10u: return RdnaOpcode::SMulkI32; case 0x13u: return RdnaOpcode::SSetregB32; + case 0x15u: return RdnaOpcode::SSetregImm32B32; case 0x17u: return RdnaOpcode::SWaitcnt; case 0x18u: return RdnaOpcode::SWaitcnt; case 0x19u: return RdnaOpcode::SWaitcnt; @@ -382,7 +385,20 @@ RdnaInstruction DecodeRdnaSopk(std::uint32_t programCounter, std::span Date: Sun, 4 Oct 2026 08:28:52 +0200 Subject: [PATCH 2/2] fix(shader): translate s_setreg_imm32_b32 only for round to nearest even in MODE Any other field or value throws. The existing s_setreg_b32 no-op is recorded under Functional technical debt instead of silent stubs. Co-Authored-By: Claude --- .../tests/execution/ScalarSopkMisc.cpp | 40 ++++++++++++++----- .../Translation/src/ScalarInstructions.cpp | 13 +++++- docs/dev/TechnicalDebt.md | 2 +- 3 files changed, 43 insertions(+), 12 deletions(-) diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/ScalarSopkMisc.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/ScalarSopkMisc.cpp index 2d81e026e..dd2b48530 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/ScalarSopkMisc.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/ScalarSopkMisc.cpp @@ -25,11 +25,13 @@ alignas(256) std::array Output{}; alignas(256) constexpr std::array Code{ 0x34060086, 0xbe9403ff, 0x11111111, 0xbe9503ff, 0x22222222, 0xbe9703ff, 0x33333333, 0xbf068080, - 0xb1148001, 0xbf068180, 0xb1157fff, 0xbf068080, 0xb1171234, 0xb0801234, 0xba801801, 0x0000000f, + 0xb1148001, 0xbf068180, 0xb1157fff, 0xbf068080, 0xb1171234, 0xb0801234, 0xba801801, 0x00000000, 0xbe9603ff, 0x44444444, 0x7e140214, 0x7e160215, 0x7e180216, 0x7e1a0217, 0xe0701000, 0x80010a03, 0xe0701004, 0x80010b03, 0xe0701008, 0x80010c03, 0xe070100c, 0x80010d03, 0xbf810000, }; +alignas(256) constexpr std::array RoundTowardZeroCode{0xba801801, 0x0000000f, 0xbf810000}; + constexpr std::uint32_t Expected[4] = {0xffff8001u, 0x22222222u, 0x44444444u, 0x00001234u}; constexpr const char* Names[4] = {"cmovk_taken", "cmovk_not_taken", "after_setreg_imm32", "cmovk_positive"}; @@ -50,6 +52,31 @@ void Expect(std::uint32_t tid, std::uint32_t actual, std::uint32_t expected, con Require(actual == expected, std::string("scalar sopk misc: lane ") + std::to_string(tid) + " " + name + " is " + Hex(actual) + ", expected " + Hex(expected)); } +ShaderRecompiler::RecompileRequest Request(AgcDriver::VulkanDevice& device, std::span code, std::span userData, std::span memory) { + 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; + return request; +} + +void ExpectRefused(AgcDriver::VulkanDevice& device) { + const std::span code(RoundTowardZeroCode); + const std::array memory{{{reinterpret_cast(code.data()), std::as_bytes(code)}}}; + const std::vector userData(8, 0u); + bool refused = false; + try { + ShaderRecompiler::Recompile(Request(device, code, userData, memory)); + } catch (const std::exception&) { + refused = true; + } + Require(refused, "scalar sopk misc: s_setreg_imm32_b32 writing round toward zero to MODE was translated"); +} + void Run(AgcDriver::VulkanDevice& device) { for (std::uint32_t tid = 0; tid < Threads; ++tid) Fill(tid, &Input[tid * Inputs]); Output.fill(0xdeadbeefu); @@ -60,15 +87,7 @@ void Run(AgcDriver::VulkanDevice& device) { 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); + const auto result = ShaderRecompiler::Recompile(Request(device, code, userData, memory)); device.Dispatch(result, 1, 1, 1, {}, reinterpret_cast(code.data())); device.WaitIdle(); } @@ -89,6 +108,7 @@ int main() { if (!device) return VulkanTestSkipped; Run(*device); Check(); + ExpectRefused(*device); std::puts("scalar sopk misc tests passed"); return 0; } catch (const std::exception& error) { diff --git a/core/shader/recompiler/Translation/src/ScalarInstructions.cpp b/core/shader/recompiler/Translation/src/ScalarInstructions.cpp index 830750243..4c04ce7ce 100644 --- a/core/shader/recompiler/Translation/src/ScalarInstructions.cpp +++ b/core/shader/recompiler/Translation/src/ScalarInstructions.cpp @@ -1,6 +1,7 @@ #include "Translation/ScalarInstructions.hpp" #include "Translation/TranslationContext.hpp" #include +#include namespace ShaderRecompiler { @@ -51,10 +52,20 @@ bool TranslationContext::emitScalar(const RdnaInstruction& inst) { scalarSelect64(inst, inst.destination); return true; case RdnaOpcode::SSetregB32: - case RdnaOpcode::SSetregImm32B32: case RdnaOpcode::SVersion: emitControlNop(); return true; + case RdnaOpcode::SSetregImm32B32: { + const std::uint32_t field = inst.source1.value; + const std::uint32_t offset = (field >> 6u) & 0x1fu; + const std::uint32_t size = ((field >> 11u) & 0x1fu) + 1u; + const std::uint64_t written = static_cast(inst.source0.value) & ((std::uint64_t{1} << size) - 1u); + if ((field & 0x3fu) != 1u || offset + size > 4u || written != 0u) { + throw std::runtime_error("s_setreg_imm32_b32 at pc " + std::to_string(inst.programCounter) + " writes a hardware register field other than round to nearest even in MODE"); + } + emitControlNop(); + 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()))); diff --git a/docs/dev/TechnicalDebt.md b/docs/dev/TechnicalDebt.md index 1fb850716..091b824c7 100644 --- a/docs/dev/TechnicalDebt.md +++ b/docs/dev/TechnicalDebt.md @@ -16,7 +16,6 @@ Throughout the project, every function at every stage either **does exactly what - [libSceHmd](../../core/libs/prx/libSceHmd/Export.cpp) implements only the disconnected-headset path: initialization succeeds, device queries report `NotDetected`, and opening a device returns `DeviceDisconnected`. Headset support, tracking, rendering and additional HMD exports are not implemented; SDK-level ABI compatibility and in-game behaviour remain unverified. - [libSceNpCommerce](../../core/libs/prx/libSceNpCommerce/Export.cpp) - the PS Store icon show/hide calls do nothing - [sceVideoOutOpen](../../core/libs/prx/libSceVideoOut/src/Output.cpp) validates the priority and CPU affinity that the open param requests for the VideoOut service thread but does not apply them: the port's present and vblank threads are host threads, and guest priorities and affinities do not reach host scheduling -- The shader recompiler [ignores `s_setreg_b32` and `s_setreg_imm32_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 [skips baryctric coordinates](../../core/shader/recompiler/Recompiler.cpp) (is not even passed to SpirvTargetOptions at row 212). - [libSceAvPlayer](../../core/libs/prx/libSceAvPlayer/Export.cpp): `sceAvPlayerSetLogCallback` accepts a callback that is never called, as the player produces no log messages, and `sceAvPlayerSetAvailableBandwidth` has no effect, as it governs HLS sources, which `sceAvPlayerAddSource` does not implement. - [sceAvPlayerSetTrickSpeed](../../core/libs/prx/libSceAvPlayer/src/Source.cpp) (libSceAvPlayer) with a negative speed runs the clock backwards but delivers no frames; when a forward speed is set again, playback resumes from the rewound time. @@ -52,6 +51,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. - [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). - The executable file that [relinker](../../core/relinker/elfpatcher/src/windows/WindowsPeWriter.cpp) generates opens the console when launched, which is inconvenient for playability. - [libSceJpegEnc](../../core/libs/prx/libSceJpegEnc/Export.cpp) encodes 4:2:2 sampling requests with 4:2:0 chroma subsampling, and grayscale requests as a 3-component JPEG with neutral chroma instead of a single-component one: the [JPEG encoder](../../core/Decoder/Jpeg/src/Jpeg.cpp) (stb) only produces 3-component 4:2:0 and 4:4:4 images.