From d9ac21c6c95c04a8700e0f3e0956fc5b7efabb19 Mon Sep 17 00:00:00 2001 From: Dean Galvin Date: Mon, 28 Sep 2026 23:12:47 +1300 Subject: [PATCH] fix(shader): make s_getpc_b64 return the shader's absolute address s_getpc_b64 produced the next instruction's offset from the start of the shader, so code that addresses data stored behind itself built V#s at a tiny address. Astro Bot's video-frame NV12->RGB pass draws its quad with an NGG shader that fetches the four vertices from after its own code (s_getpc_b64; s_add_u32 0x98; s_addc_u32) - the buffer resolved to 0xf0, every vertex read as zero, the quad never rasterized and the PlayStation Studios logo video stayed black. The result is now GetShaderBase + offset. The resource walk already evaluates GetShaderBase per request from the code address, so the V# names the real data and the same code at another address still shares its compiled variant. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01ButHENCbXXt8h4EZuWUabJ --- .../libSceAgcDriver/tests/ShaderMemory.cpp | 46 +++++++++++++++++++ .../src/ControlFlowInstructions.cpp | 3 +- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp b/core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp index 030579c1c..5067f59d9 100644 --- a/core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp @@ -360,6 +360,51 @@ void verifyBindlessTable() { fillSrt(4u); } + +void verifyProgramCounterRelativeData() { + using namespace ShaderRecompiler; + static const std::array code{ + 0xbe801f00u, + 0x800000ffu, 52u, + 0x82010180u, + 0xb0020010u, + 0xbe8303ffu, 0x10005004u, + 0xf4200100u, 0xfa000000u, + 0xbf8cc07fu, + 0x7e000204u, + 0xf80008cfu, 0u, + 0xbf810000u, + 0x3f800000u + }; + const auto codeAddress = reinterpret_cast(code.data()); + RecompileRequest request{}; + request.shader = {ShaderStage::Vertex, codeAddress, code, 0, {}}; + request.context.waveSize = 64; + request.context.userDataBaseRegister = 8; + request.context.vertex = ShaderVertexStageInfo{}; + request.target.vulkanVersion = 0x00401000u; + request.target.spirvVersion = 0x00010300u; + request.target.subgroupSize = 64; + request.target.fragmentShaderBarycentricEnabled = false; + request.layout.pushConstantSizeBytes = 128; + const auto dataBase = [](const RecompileResult& result) { + for (const auto& binding : result.bindings) { + if (binding.role != DescriptorRole::GuestBuffers || binding.guestDescriptor.size() < 4u) continue; + return static_cast(binding.guestDescriptor[0]) | (static_cast(binding.guestDescriptor[1] & 0xffffu) << 32u); + } + throw std::runtime_error("program counter data: no guest buffer was bound"); + }; + AgcDriver::ShaderMemory memory({}); + static_cast(memory.Capture(request)); + request.context.memory = memory.Regions(); + const auto first = Recompile(request); + require(dataBase(first) == codeAddress + 56u, "program counter data: the V# does not name the data at the shader's address"); + auto relocated = request; + relocated.shader.codeAddress += 0x1000u; + const auto moved = Recompile(relocated); + require(moved.cacheHit, "program counter data: relocating the shader recompiled it"); + require(dataBase(moved) == codeAddress + 0x1000u + 56u, "program counter data: the relocated shader bound the old address"); +} } int main() { @@ -368,6 +413,7 @@ int main() { verifyRegisterSources(); verifyPureFlatSlots(); verifyBindlessTable(); + verifyProgramCounterRelativeData(); #if ANYPS5_ENABLE_SPIRV_TOOLS const std::vector minimalSpirv{ 0x07230203u, 0x00010000u, 0u, 5u, 0u, diff --git a/core/shader/recompiler/Translation/src/ControlFlowInstructions.cpp b/core/shader/recompiler/Translation/src/ControlFlowInstructions.cpp index 16f9423d6..7835f97a0 100644 --- a/core/shader/recompiler/Translation/src/ControlFlowInstructions.cpp +++ b/core/shader/recompiler/Translation/src/ControlFlowInstructions.cpp @@ -187,7 +187,8 @@ void TranslationContext::sInstPrefetch() { } void TranslationContext::sGetpcB64(const RdnaInstruction& inst) { - const IrU64 pc(ir.ConstantU64(static_cast(currentProgramCounter) + 4u)); + IrValue& base = ir.Emit(IrOpcode::GetShaderBase, IrType::U64, {}); + const IrU64 pc(ir.Emit(IrOpcode::IAdd64, IrType::U64, {&base, &ir.ConstantU64(static_cast(currentProgramCounter) + 4u)})); writeU32Pair(inst.destination, extractU64(pc)); }