Merge pull request #111 from oneandonlydean/fix/s-getpc-absolute

fix(shader): make s_getpc_b64 return the shader's absolute address
This commit is contained in:
boykopovar
2026-09-29 13:11:55 +03:00
committed by GitHub
2 changed files with 48 additions and 1 deletions
@@ -360,6 +360,51 @@ void verifyBindlessTable() {
fillSrt(4u);
}
void verifyProgramCounterRelativeData() {
using namespace ShaderRecompiler;
static const std::array<std::uint32_t, 15> code{
0xbe801f00u,
0x800000ffu, 52u,
0x82010180u,
0xb0020010u,
0xbe8303ffu, 0x10005004u,
0xf4200100u, 0xfa000000u,
0xbf8cc07fu,
0x7e000204u,
0xf80008cfu, 0u,
0xbf810000u,
0x3f800000u
};
const auto codeAddress = reinterpret_cast<std::uintptr_t>(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<std::uint64_t>(binding.guestDescriptor[0]) | (static_cast<std::uint64_t>(binding.guestDescriptor[1] & 0xffffu) << 32u);
}
throw std::runtime_error("program counter data: no guest buffer was bound");
};
AgcDriver::ShaderMemory memory({});
static_cast<void>(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<std::uint32_t> minimalSpirv{
0x07230203u, 0x00010000u, 0u, 5u, 0u,
@@ -187,7 +187,8 @@ void TranslationContext::sInstPrefetch() {
}
void TranslationContext::sGetpcB64(const RdnaInstruction& inst) {
const IrU64 pc(ir.ConstantU64(static_cast<std::uint64_t>(currentProgramCounter) + 4u));
IrValue& base = ir.Emit(IrOpcode::GetShaderBase, IrType::U64, {});
const IrU64 pc(ir.Emit(IrOpcode::IAdd64, IrType::U64, {&base, &ir.ConstantU64(static_cast<std::uint64_t>(currentProgramCounter) + 4u)}));
writeU32Pair(inst.destination, extractU64(pc));
}