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..dd2b48530 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/ScalarSopkMisc.cpp @@ -0,0 +1,118 @@ +#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, 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"}; + +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)); +} + +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); + 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 auto result = ShaderRecompiler::Recompile(Request(device, code, userData, memory)); + 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(); + ExpectRefused(*device); + 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 96e0b3d13..715deb82a 100644 --- a/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp +++ b/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp @@ -895,6 +895,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 07d242370..6d1e9548b 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 6d46f1617..15afeecfd 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; @@ -384,7 +387,20 @@ RdnaInstruction DecodeRdnaSopk(std::uint32_t programCounter, std::span> 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()))); + return true; + } case RdnaOpcode::SWaitcnt: emitWaitcnt(); return true; diff --git a/docs/dev/TechnicalDebt.md b/docs/dev/TechnicalDebt.md index 3f1653af7..caae840ae 100644 --- a/docs/dev/TechnicalDebt.md +++ b/docs/dev/TechnicalDebt.md @@ -53,6 +53,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). - [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.