From f5619c4dcc7c4a4792c7636230b1521a4759b19d Mon Sep 17 00:00:00 2001 From: Kikoarl <7354010+Kikoarl@users.noreply.github.com> Date: Tue, 6 Oct 2026 15:45:51 +0200 Subject: [PATCH] feat(shader): implement S_CALL_B64 --- core/libs/prx/libSceAgcDriver/CMakeLists.txt | 8 ++ core/libs/tests/ScalarCall.cpp | 73 +++++++++++++++++++ .../include/RdnaDecoder/RdnaOpcode.hpp | 1 + .../RdnaDecoder/src/RdnaScalarOpDecoder.cpp | 5 +- .../Translation/src/ScalarInstructions.cpp | 2 + 5 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 core/libs/tests/ScalarCall.cpp diff --git a/core/libs/prx/libSceAgcDriver/CMakeLists.txt b/core/libs/prx/libSceAgcDriver/CMakeLists.txt index 615b0d15e..a9ce856b1 100644 --- a/core/libs/prx/libSceAgcDriver/CMakeLists.txt +++ b/core/libs/prx/libSceAgcDriver/CMakeLists.txt @@ -780,6 +780,14 @@ if(BUILD_TESTING) set_tests_properties(scalar_scratch_load PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() + add_test_executable(scalar_call_tests ${LIBS_INCLUDE_DIR}/tests/ScalarCall.cpp) + target_link_libraries(scalar_call_tests PRIVATE shader_recompiler_agc_driver libc) + configure_windows_unwind(scalar_call_tests) + add_test(NAME scalar_call COMMAND scalar_call_tests) + if(WIN32) + set_tests_properties(scalar_call PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() add_test_executable(image_sample_gather_tests ${LIBS_INCLUDE_DIR}/tests/ImageSampleGather.cpp) target_link_libraries(image_sample_gather_tests PRIVATE shader_recompiler_agc_driver libc) configure_windows_unwind(image_sample_gather_tests) diff --git a/core/libs/tests/ScalarCall.cpp b/core/libs/tests/ScalarCall.cpp new file mode 100644 index 000000000..bbc7fe30a --- /dev/null +++ b/core/libs/tests/ScalarCall.cpp @@ -0,0 +1,73 @@ +#include "Translation/TranslationContext.hpp" +#include "Translation/InstructionTranslator.hpp" +#include "RdnaDecoder/RdnaScalarOpDecoder.hpp" +#include "RdnaDecoder/RdnaInstructionDecoder.hpp" +#include "ControlFlow/GraphBuilder.hpp" +#include +#include +#include +#include + +using namespace ShaderRecompiler; +static void Require(bool value) { if (!value) throw std::runtime_error("scalar call regression"); } + +static void CheckDecodeAndBranch() { + const std::array code{0xbb040008u}; + const RdnaInstruction instruction = DecodeRdnaSopk(0u, code, 0u); + Require(instruction.op == RdnaOpcode::SCallB64); + Require(instruction.family == RdnaInstructionFamily::SOPK); + Require(instruction.destination.kind == RdnaOperandKind::ScalarRegister); + Require(instruction.destination.reg == 4u); + Require(instruction.dataDwordCount == 2u); + Require(instruction.branchTarget == 36u); +} + +static void CheckNegativeOffset() { + const std::uint32_t word = 0xb8000000u | (0x16u << 23u) | (2u << 16u) | static_cast(-4); + const std::array code{word}; + const RdnaInstruction instruction = DecodeRdnaSopk(16u, code, 0u); + Require(instruction.op == RdnaOpcode::SCallB64); + Require(instruction.destination.reg == 2u); + Require(instruction.branchTarget == 4u); +} + +static void CheckTranslationThrows() { + const std::array code{0xbb040008u}; + const RdnaInstruction instruction = DecodeRdnaSopk(0u, code, 0u); + IrProgram program; + auto& block = program.CreateBlock(); + program.SetEntryBlock(block); + TranslationContext context(program, block, 256); + bool threw = false; + try { + context.TranslateInstruction(instruction); + } catch (const std::runtime_error&) { + threw = true; + } + Require(threw); +} + +static void CheckProgramGraphBuilderAndTranslationThrows() { + const std::vector code{0xbb040001u, 0xbf810000u, 0xbf810000u}; + const auto decoded = RdnaInstructionDecoder{}.Decode(code); + const auto cfg = GraphBuilder{}.Build(decoded); + ShaderComputeInputInfo computeInfo{}; + TranslateOptions options{}; + options.stage = ShaderStageKind::Compute; + options.inputInfo.compute = &computeInfo; + bool threw = false; + try { + static_cast(InstructionTranslator{}.Translate(decoded, cfg, options)); + } catch (const std::runtime_error& err) { + threw = std::string(err.what()).find("s_call_b64 at pc 0 is not implemented") != std::string::npos; + } + Require(threw); +} + +int main() { + CheckDecodeAndBranch(); + CheckNegativeOffset(); + CheckTranslationThrows(); + CheckProgramGraphBuilderAndTranslationThrows(); + return 0; +} diff --git a/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp b/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp index 5899f0335..f04285526 100644 --- a/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp +++ b/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp @@ -203,6 +203,7 @@ enum class RdnaOpcode : std::uint16_t { SMovrelsd2B32, SQuadmaskB64, SGetpcB64, + SCallB64, SSubvectorLoopBegin, SSubvectorLoopEnd, SAndSaveexecB32, diff --git a/core/shader/recompiler/RdnaDecoder/src/RdnaScalarOpDecoder.cpp b/core/shader/recompiler/RdnaDecoder/src/RdnaScalarOpDecoder.cpp index 059235a8d..f07f2a207 100644 --- a/core/shader/recompiler/RdnaDecoder/src/RdnaScalarOpDecoder.cpp +++ b/core/shader/recompiler/RdnaDecoder/src/RdnaScalarOpDecoder.cpp @@ -179,6 +179,7 @@ RdnaOpcode decodeSopkOpcode(std::uint32_t opcode) { case 0x12u: return RdnaOpcode::SGetregB32; case 0x13u: return RdnaOpcode::SSetregB32; case 0x15u: return RdnaOpcode::SSetregImm32B32; + case 0x16u: return RdnaOpcode::SCallB64; case 0x17u: return RdnaOpcode::SWaitcnt; case 0x18u: return RdnaOpcode::SWaitcnt; case 0x19u: return RdnaOpcode::SWaitcnt; @@ -285,6 +286,7 @@ std::uint32_t scalarDestinationDwordCount(RdnaOpcode opcode) { case RdnaOpcode::SLshlB64: case RdnaOpcode::SLshrB64: case RdnaOpcode::SBfmB64: + case RdnaOpcode::SCallB64: case RdnaOpcode::SBfeU64: return 2u; default: return 1u; } @@ -411,8 +413,9 @@ RdnaInstruction DecodeRdnaSopk(std::uint32_t programCounter, std::span(immediate * 4); return instruction; } diff --git a/core/shader/recompiler/Translation/src/ScalarInstructions.cpp b/core/shader/recompiler/Translation/src/ScalarInstructions.cpp index 505d91545..89d5912f4 100644 --- a/core/shader/recompiler/Translation/src/ScalarInstructions.cpp +++ b/core/shader/recompiler/Translation/src/ScalarInstructions.cpp @@ -29,6 +29,8 @@ bool TranslationContext::emitScalar(const RdnaInstruction& inst) { return true; case RdnaOpcode::SSetpcB64: return true; + case RdnaOpcode::SCallB64: + throw std::runtime_error("s_call_b64 at pc " + std::to_string(inst.programCounter) + " is not implemented"); case RdnaOpcode::SSubvectorLoopBegin: sSubvectorLoop(inst, true); return true;