From 6681187712a3d31b833358f6f006bc1eab34e8f6 Mon Sep 17 00:00:00 2001 From: Yunus Shaikh <75296019+EagleBearerYS@users.noreply.github.com> Date: Wed, 21 Oct 2026 21:36:16 +0000 Subject: [PATCH 01/24] fix(shader): follow pointer hops after a pointer-keyed record pointer TryMakeIndirectColumn refused every pointer hop once the record's pointer words were merged, so a T# read through a pointer loaded from a pointer-keyed record pointer stayed unplanned (Gran Turismo 7's World Map compute shaders, issue #6191). Only buffer handles still refuse hops: the materializer keys the records by the innermost pointer before it follows the hops, and the plan's parent pointer is that innermost one. --- core/libs/prx/libSceAgcDriver/CMakeLists.txt | 5 +- .../tests/execution/ImageTable.cpp | 97 +++++++++++++++++++ .../Optimization/src/ResourceTracker.cpp | 2 +- 3 files changed, 101 insertions(+), 3 deletions(-) diff --git a/core/libs/prx/libSceAgcDriver/CMakeLists.txt b/core/libs/prx/libSceAgcDriver/CMakeLists.txt index 07e4d72eb89f..5a6df642dad0 100644 --- a/core/libs/prx/libSceAgcDriver/CMakeLists.txt +++ b/core/libs/prx/libSceAgcDriver/CMakeLists.txt @@ -2450,9 +2450,10 @@ if(BUILD_TESTING) add_test(NAME agc_driver_pointer_phi_high_second_lane_table COMMAND agc_driver_image_table_tests --pointer-phi-high-second-lane) add_test(NAME agc_driver_pointer_phi_lane_not_table COMMAND agc_driver_image_table_tests --pointer-phi-lane-not) add_test(NAME agc_driver_pointer_vsharp_select_table COMMAND agc_driver_image_table_tests --pointer-vsharp-select) - set_tests_properties(agc_driver_nested_image_table agc_driver_nested_image_table_large agc_driver_nested_image_table_budget agc_driver_nested_sampler_table agc_driver_pointer_phi_sampler_table agc_driver_pointer_phi_ballot_table agc_driver_pointer_phi_copies_table agc_driver_pointer_phi_lane_and_table agc_driver_pointer_phi_high_table agc_driver_pointer_phi_high_second_table agc_driver_pointer_phi_high_second_lane_table agc_driver_pointer_phi_lane_not_table agc_driver_pointer_vsharp_select_table PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_NO_PIPELINE_JOURNAL=1" SKIP_RETURN_CODE 77 TIMEOUT 30 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") + add_test(NAME agc_driver_pointer_hop_table COMMAND agc_driver_image_table_tests --pointer-hop) + set_tests_properties(agc_driver_nested_image_table agc_driver_nested_image_table_large agc_driver_nested_image_table_budget agc_driver_nested_sampler_table agc_driver_pointer_phi_sampler_table agc_driver_pointer_phi_ballot_table agc_driver_pointer_phi_copies_table agc_driver_pointer_phi_lane_and_table agc_driver_pointer_phi_high_table agc_driver_pointer_phi_high_second_table agc_driver_pointer_phi_high_second_lane_table agc_driver_pointer_phi_lane_not_table agc_driver_pointer_vsharp_select_table agc_driver_pointer_hop_table PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_NO_PIPELINE_JOURNAL=1" SKIP_RETURN_CODE 77 TIMEOUT 30 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") if(WIN32) - set_tests_properties(agc_driver_nested_image_table agc_driver_nested_image_table_large agc_driver_nested_image_table_budget agc_driver_nested_sampler_table agc_driver_pointer_phi_sampler_table agc_driver_pointer_phi_ballot_table agc_driver_pointer_phi_copies_table agc_driver_pointer_phi_lane_and_table agc_driver_pointer_phi_high_table agc_driver_pointer_phi_high_second_table agc_driver_pointer_phi_high_second_lane_table agc_driver_pointer_phi_lane_not_table agc_driver_pointer_vsharp_select_table PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") + set_tests_properties(agc_driver_nested_image_table agc_driver_nested_image_table_large agc_driver_nested_image_table_budget agc_driver_nested_sampler_table agc_driver_pointer_phi_sampler_table agc_driver_pointer_phi_ballot_table agc_driver_pointer_phi_copies_table agc_driver_pointer_phi_lane_and_table agc_driver_pointer_phi_high_table agc_driver_pointer_phi_high_second_table agc_driver_pointer_phi_high_second_lane_table agc_driver_pointer_phi_lane_not_table agc_driver_pointer_vsharp_select_table agc_driver_pointer_hop_table PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() add_test(NAME agc_pipeline_journal COMMAND agc_driver_graphics_tests --pipeline-journal) add_test(NAME agc_pipeline_journal_long_paths COMMAND agc_driver_graphics_tests --pipeline-journal-long-paths) diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp index f1b32368e681..e35cd06654e8 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp @@ -415,6 +415,16 @@ alignas(256) constexpr std::array PointerVsharpSelectCode{ 0xf42c091au, 0x34000000u, 0xbf8cc07fu, 0xf09c0f08u, 0x01090401u, 0x8afe3032u, 0xbf89ffedu, 0xbefe042eu, 0x7e120202u, 0x34101286u, 0x4a101100u, 0x34101084u, 0xe0781000u, 0x80070408u, 0xbf810000u, }; +alignas(256) constexpr std::array PointerHopCode{ + 0xf4080100u, 0xfa000000u, 0xf4080200u, 0xfa000010u, 0xf4080700u, 0xfa000020u, 0xf4080800u, 0xfa000030u, + 0x8f108402u, 0xf4280404u, 0x20000000u, 0x360200bfu, 0x7e020d01u, 0x060202f0u, 0x100202ffu, 0x3c800000u, + 0x060202f2u, 0x7e0402ffu, 0x3e000000u, 0xbf8cc07fu, 0x7d8800a0u, 0x7e140210u, 0x02061411u, 0xbeae047eu, + 0x7e280503u, 0xd4c20030u, 0x00020614u, 0xbeb22430u, 0x8f158414u, 0xf4240b02u, 0x2a000008u, 0xd4c50034u, + 0x00020680u, 0xbf8cc07fu, 0xbf068112u, 0xbf840003u, 0xbeac03ffu, 0x000000eau, 0xbf820003u, 0xbf068212u, + 0xbf840001u, 0xbeac0334u, 0xf4040e16u, 0xfa000000u, 0xbf8cc07fu, 0xf40c091cu, 0xfa000340u, 0xbf8cc07fu, + 0xf09c0f08u, 0x01090401u, 0x8afe3032u, 0xbf89ffe4u, 0xbefe042eu, 0x7e120202u, 0x34101286u, 0x4a101100u, + 0x34101084u, 0xe0781000u, 0x80070408u, 0xbf810000u, +}; constexpr std::array PointClamp{0x92u, (4u * 256u) << 12u, 0u, 0u}; constexpr std::array LinearWrap{0u, (4u * 256u) << 12u, (1u << 20u) | (1u << 22u), 0u}; @@ -2185,6 +2195,89 @@ void RunPointerVsharpTests(AgcDriver::VulkanDevice& device) { std::puts("pointer V# table: a T# read through a V# loaded through a record pointer keyed by a first-lane read of a select checked, by the records' values"); } +void RunPointerHopTests(AgcDriver::VulkanDevice& device) { + Fill(); + alignas(256) std::array, 2> links{}; + alignas(256) std::array, 2> targets{}; + Table = {}; + Keys = {}; + const std::array images{Image(FloatTexels.data(), Format32Float), Image(OtherFloatTexels.data(), Format32Float)}; + const auto link = [&](std::uint32_t from, std::uint32_t to) { + const auto pointer = reinterpret_cast(targets[to].data()); + links[from][0] = static_cast(pointer); + links[from][1] = static_cast(pointer >> 32u); + }; + for (std::uint32_t target = 0; target < 2u; ++target) { + std::copy(images[target].begin(), images[target].end(), targets[target].begin() + 0x340u / 4u); + link(target, target); + } + for (std::uint32_t record = 0; record < 3u; ++record) { + const auto pointer = reinterpret_cast(links[record % 2u].data()); + Table[record * 4u + 2u] = static_cast(pointer); + Table[record * 4u + 3u] = static_cast(pointer >> 32u); + } + const std::string name = "pointer table with a pointer hop"; + const auto run = [&] { + const auto table = Buffer(Table.data(), 64u); + const auto keys = Buffer(Keys.data(), static_cast(sizeof(Keys))); + const auto output = Buffer(Output.data(), static_cast(sizeof(Output))); + Srt = {}; + std::copy(table.begin(), table.end(), Srt.begin()); + std::copy(keys.begin(), keys.end(), Srt.begin() + 4u); + std::copy(output.begin(), output.end(), Srt.begin() + 8u); + std::copy(PointWrap.begin(), PointWrap.end(), Srt.begin() + 12u); + const std::span code(PointerHopCode); + const std::vector memory{ + {reinterpret_cast(code.data()), std::as_bytes(code)}, + {reinterpret_cast(Srt.data()), std::as_bytes(std::span(Srt))}, + {reinterpret_cast(Table.data()), std::as_bytes(std::span(Table))}, + {reinterpret_cast(Keys.data()), std::as_bytes(std::span(Keys))}, + {reinterpret_cast(links.data()), std::as_bytes(std::span(links))}, + {reinterpret_cast(targets.data()), std::as_bytes(std::span(targets))}, + }; + const auto inputTable = Table; + const auto inputLinks = links; + const auto inputTargets = targets; + auto result = Execute(device, code, Srt.data(), memory, 4u, Lanes, 64u); + Require(Table == inputTable && links == inputLinks && targets == inputTargets, name + ": dispatch modified its input memory"); + Require(std::all_of(Output.begin() + 4u * Lanes * 4u, Output.end(), [](auto word) { return word == 0xdeadbeefu; }), name + ": dispatch wrote past its output"); + return result; + }; + const std::array, 4> keys{{{1u, 2u}, {2u, 1u}, {1u, 1u}, {2u, 2u}}}; + for (std::uint32_t group = 0; group < 4u; ++group) { + Keys[group * 4u] = keys[group][0]; + Keys[group * 4u + 1u] = keys[group][1]; + } + std::array targetOf{0u, 1u}; + const auto sampled = [&](std::uint32_t group) { + return std::function([&, group](std::uint32_t x) { + const auto record = keys[group][x < 32u ? 0u : 1u]; + return std::bit_cast(targetOf[record % 2u] != 0u ? OtherFloatTexels[x] : FloatTexels[x]); + }); + }; + const auto untouched = [](const Outcome& outcome) { return std::all_of(outcome.words.begin(), outcome.words.end(), [](auto word) { return word == 0xdeadbeefu; }); }; + const auto taken = run(); + Require(!Faulted(taken), name + ": the T# behind the record pointer's hop faulted:\n" + taken.log); + for (std::uint32_t group = 0; group < 4u; ++group) RequireRed(taken, group, Lanes, sampled(group), name + ": the T# read through the pointer loaded from the record's pointer in a waterfall loop"); + const auto outside = [&](std::uint32_t arm, const std::string& what) { + Keys[2] = arm; + const auto faulted = run(); + Keys[2] = 0u; + Require(Faulted(faulted) && faulted.log.find("outside the snapshot") != std::string::npos, name + ": " + what + " did not fault as outside the snapshot:\n" + faulted.log); + Require(untouched(faulted), name + ": " + what + " published copied output"); + Require(faulted.artifactId == taken.artifactId, name + ": " + what + " recompiled the shader"); + }; + outside(1u, "a constant pointer word"); + outside(2u, "a lane-mask pointer word"); + link(1u, 0u); + targetOf[1] = 0u; + const auto moved = run(); + Require(!Faulted(moved), name + ": the T# behind a moved hop faulted:\n" + moved.log); + Require(moved.artifactId == taken.artifactId, name + ": moving the pointer behind a record's pointer recompiled the shader"); + for (std::uint32_t group = 0; group < 4u; ++group) RequireRed(moved, group, Lanes, sampled(group), name + ": the T# after the pointer behind record 1's pointer moved to the other T#"); + std::puts("pointer table with a pointer hop: a T# read through a pointer loaded from a record pointer whose low word passes Phis with a constant and a Ballot word checked, by the records' values"); +} + void RunSplitRecordTests(AgcDriver::VulkanDevice& device) { Fill(); const auto floatImage = Image(FloatTexels.data(), Format32Float); @@ -2450,6 +2543,10 @@ int main(int argc, char** argv) { RunPointerVsharpTests(*device); return 0; } + if (argc == 2 && std::strcmp(argv[1], "--pointer-hop") == 0) { + RunPointerHopTests(*device); + return 0; + } RunTests(*device); RunBorderSwizzleTests(*device); RunAnisoOverrideTests(*device); diff --git a/core/shader/recompiler/Optimization/src/ResourceTracker.cpp b/core/shader/recompiler/Optimization/src/ResourceTracker.cpp index 19cbd9b97fde..dcc56de21c11 100644 --- a/core/shader/recompiler/Optimization/src/ResourceTracker.cpp +++ b/core/shader/recompiler/Optimization/src/ResourceTracker.cpp @@ -988,7 +988,7 @@ class Tracker { const bool buffer = handle.Opcode() == IrOpcode::GetBufferResource; const bool merged = pointer->Argument(0)->Resolve()->IsPhi(); const bool pointerKeyed = merged && !buffer; - if (!hops.empty() && (buffer || merged)) return false; + if (!hops.empty() && buffer) return false; std::vector lowReads; std::vector highReads; if (pointerKeyed ? !PointerRecordPairs(pointer->Argument(0), pointer->Argument(1), lowReads, highReads) : merged && !PointerRecordReads(pointer->Argument(0), lowReads)) return false; From 0843cca39d4fdc5ac5855d3497c0befb71fcd761 Mon Sep 17 00:00:00 2001 From: hugodearaujo <175634979+hugodearaujo@users.noreply.github.com> Date: Wed, 21 Oct 2026 21:37:28 +0000 Subject: [PATCH 02/24] fix(shader): read a T# at a fixed offset of a pointer-loaded V# as a one-key child From hugodearaujo's change on #8939 (Marvel's Spider-Man 2, PPSA03016). --- .../recompiler/Optimization/src/ResourceTracker.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/core/shader/recompiler/Optimization/src/ResourceTracker.cpp b/core/shader/recompiler/Optimization/src/ResourceTracker.cpp index dcc56de21c11..fba2fc696798 100644 --- a/core/shader/recompiler/Optimization/src/ResourceTracker.cpp +++ b/core/shader/recompiler/Optimization/src/ResourceTracker.cpp @@ -703,7 +703,13 @@ class Tracker { if (address && immediateU32(offset, descriptorOffset)) { return TryMakeIndirectColumn(handle, *heapHandle, sampler, words, descriptorOffset, first, plan); } - if (!MatchAffineOffset(offset, key, stride, addend)) { + std::uint32_t fixedOffset = 0; + const bool fixed = !address && !buffer && immediateU32(offset, fixedOffset); + if (fixed) { + key = &m_builder.Constant(0u); + stride = width * static_cast(sizeof(std::uint32_t)); + addend = fixedOffset; + } else if (!MatchAffineOffset(offset, key, stride, addend)) { if (!MatchMaskedOffset(offset, key, stride)) { return false; } @@ -711,7 +717,9 @@ class Tracker { } DescriptorSource heapSource; std::uint32_t heapIndex = 0; - if (!MakeRuntimeSource(*heapHandle, address ? IrOpcode::GetAddressResource : IrOpcode::GetBufferResource, address ? 2u : 4u, heapIndex, heapSource)) { + if (MakeRuntimeSource(*heapHandle, address ? IrOpcode::GetAddressResource : IrOpcode::GetBufferResource, address ? 2u : 4u, heapIndex, heapSource)) { + if (fixed) return false; + } else { if (address || buffer || heapHandle->Opcode() != IrOpcode::GetBufferResource || !std::ranges::all_of(heapHandle->Uses(), [&](const IrValue* use) { return std::ranges::find(plan.reads, use) != plan.reads.end(); })) return false; const auto sameHandle = [&](const IrValue* user) { if (user->Opcode() != handle.Opcode() || user->ArgumentCount() != handle.ArgumentCount()) return false; From c00cb0f875201a771403eddfd864ad2e5ac21b07 Mon Sep 17 00:00:00 2001 From: Yunus Shaikh <75296019+EagleBearerYS@users.noreply.github.com> Date: Wed, 21 Oct 2026 21:39:16 +0000 Subject: [PATCH 03/24] test(shader): read a T# at a fixed offset of a pointer-loaded V# Spider-Man 2's shape: a record keyed by a first-lane read, its dwords 2-3 a pointer, a V# loaded at that pointer plus 0xa0, and the T# read at offset 0 or 0x40 of that V#'s buffer with no second key. Two records point at V#s of different T#s; a moved record pointer and a moved V# must change the result without recompiling. --- core/libs/prx/libSceAgcDriver/CMakeLists.txt | 6 +- .../tests/execution/ImageTable.cpp | 104 ++++++++++++++++++ 2 files changed, 108 insertions(+), 2 deletions(-) diff --git a/core/libs/prx/libSceAgcDriver/CMakeLists.txt b/core/libs/prx/libSceAgcDriver/CMakeLists.txt index 5a6df642dad0..e72d2cd94a9e 100644 --- a/core/libs/prx/libSceAgcDriver/CMakeLists.txt +++ b/core/libs/prx/libSceAgcDriver/CMakeLists.txt @@ -2451,9 +2451,11 @@ if(BUILD_TESTING) add_test(NAME agc_driver_pointer_phi_lane_not_table COMMAND agc_driver_image_table_tests --pointer-phi-lane-not) add_test(NAME agc_driver_pointer_vsharp_select_table COMMAND agc_driver_image_table_tests --pointer-vsharp-select) add_test(NAME agc_driver_pointer_hop_table COMMAND agc_driver_image_table_tests --pointer-hop) - set_tests_properties(agc_driver_nested_image_table agc_driver_nested_image_table_large agc_driver_nested_image_table_budget agc_driver_nested_sampler_table agc_driver_pointer_phi_sampler_table agc_driver_pointer_phi_ballot_table agc_driver_pointer_phi_copies_table agc_driver_pointer_phi_lane_and_table agc_driver_pointer_phi_high_table agc_driver_pointer_phi_high_second_table agc_driver_pointer_phi_high_second_lane_table agc_driver_pointer_phi_lane_not_table agc_driver_pointer_vsharp_select_table agc_driver_pointer_hop_table PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_NO_PIPELINE_JOURNAL=1" SKIP_RETURN_CODE 77 TIMEOUT 30 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") + add_test(NAME agc_driver_pointer_vsharp_fixed_table COMMAND agc_driver_image_table_tests --pointer-vsharp-fixed) + add_test(NAME agc_driver_pointer_vsharp_fixed_offset_table COMMAND agc_driver_image_table_tests --pointer-vsharp-fixed-offset) + set_tests_properties(agc_driver_nested_image_table agc_driver_nested_image_table_large agc_driver_nested_image_table_budget agc_driver_nested_sampler_table agc_driver_pointer_phi_sampler_table agc_driver_pointer_phi_ballot_table agc_driver_pointer_phi_copies_table agc_driver_pointer_phi_lane_and_table agc_driver_pointer_phi_high_table agc_driver_pointer_phi_high_second_table agc_driver_pointer_phi_high_second_lane_table agc_driver_pointer_phi_lane_not_table agc_driver_pointer_vsharp_select_table agc_driver_pointer_hop_table agc_driver_pointer_vsharp_fixed_table agc_driver_pointer_vsharp_fixed_offset_table PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_NO_PIPELINE_JOURNAL=1" SKIP_RETURN_CODE 77 TIMEOUT 30 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") if(WIN32) - set_tests_properties(agc_driver_nested_image_table agc_driver_nested_image_table_large agc_driver_nested_image_table_budget agc_driver_nested_sampler_table agc_driver_pointer_phi_sampler_table agc_driver_pointer_phi_ballot_table agc_driver_pointer_phi_copies_table agc_driver_pointer_phi_lane_and_table agc_driver_pointer_phi_high_table agc_driver_pointer_phi_high_second_table agc_driver_pointer_phi_high_second_lane_table agc_driver_pointer_phi_lane_not_table agc_driver_pointer_vsharp_select_table agc_driver_pointer_hop_table PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") + set_tests_properties(agc_driver_nested_image_table agc_driver_nested_image_table_large agc_driver_nested_image_table_budget agc_driver_nested_sampler_table agc_driver_pointer_phi_sampler_table agc_driver_pointer_phi_ballot_table agc_driver_pointer_phi_copies_table agc_driver_pointer_phi_lane_and_table agc_driver_pointer_phi_high_table agc_driver_pointer_phi_high_second_table agc_driver_pointer_phi_high_second_lane_table agc_driver_pointer_phi_lane_not_table agc_driver_pointer_vsharp_select_table agc_driver_pointer_hop_table agc_driver_pointer_vsharp_fixed_table agc_driver_pointer_vsharp_fixed_offset_table PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() add_test(NAME agc_pipeline_journal COMMAND agc_driver_graphics_tests --pipeline-journal) add_test(NAME agc_pipeline_journal_long_paths COMMAND agc_driver_graphics_tests --pipeline-journal-long-paths) diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp index e35cd06654e8..0b9d83f672eb 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp @@ -425,6 +425,24 @@ alignas(256) constexpr std::array PointerHopCode{ 0xf09c0f08u, 0x01090401u, 0x8afe3032u, 0xbf89ffe4u, 0xbefe042eu, 0x7e120202u, 0x34101286u, 0x4a101100u, 0x34101084u, 0xe0781000u, 0x80070408u, 0xbf810000u, }; +alignas(256) constexpr std::array PointerFixedVsharpCode{ + 0xf4080100u, 0xfa000000u, 0xf4080200u, 0xfa000010u, 0xf4080700u, 0xfa000020u, 0xf4080800u, 0xfa000030u, + 0x8f108402u, 0xf4280404u, 0x20000000u, 0x360200bfu, 0x7e020d01u, 0x060202f0u, 0x100202ffu, 0x3c800000u, + 0x060202f2u, 0x7e0402ffu, 0x3e000000u, 0xbf8cc07fu, 0x7d8800a0u, 0x7e140210u, 0x02061411u, 0xbeae047eu, + 0x7e280503u, 0xd4c20030u, 0x00020614u, 0xbeb22430u, 0x8f158414u, 0xf4240b02u, 0x2a000008u, 0xbf8cc07fu, + 0xf4080d16u, 0xfa0000a0u, 0xbf8cc07fu, 0xf42c091au, 0xfa000000u, 0xbf8cc07fu, 0xf09c0f08u, 0x01090401u, + 0x8afe3032u, 0xbf89ffeeu, 0xbefe042eu, 0x7e120202u, 0x34101286u, 0x4a101100u, 0x34101084u, 0xe0781000u, + 0x80070408u, 0xbf810000u, +}; +alignas(256) constexpr std::array PointerFixedVsharpOffsetCode{ + 0xf4080100u, 0xfa000000u, 0xf4080200u, 0xfa000010u, 0xf4080700u, 0xfa000020u, 0xf4080800u, 0xfa000030u, + 0x8f108402u, 0xf4280404u, 0x20000000u, 0x360200bfu, 0x7e020d01u, 0x060202f0u, 0x100202ffu, 0x3c800000u, + 0x060202f2u, 0x7e0402ffu, 0x3e000000u, 0xbf8cc07fu, 0x7d8800a0u, 0x7e140210u, 0x02061411u, 0xbeae047eu, + 0x7e280503u, 0xd4c20030u, 0x00020614u, 0xbeb22430u, 0x8f158414u, 0xf4240b02u, 0x2a000008u, 0xbf8cc07fu, + 0xf4080d16u, 0xfa0000a0u, 0xbf8cc07fu, 0xf42c091au, 0xfa000040u, 0xbf8cc07fu, 0xf09c0f08u, 0x01090401u, + 0x8afe3032u, 0xbf89ffeeu, 0xbefe042eu, 0x7e120202u, 0x34101286u, 0x4a101100u, 0x34101084u, 0xe0781000u, + 0x80070408u, 0xbf810000u, +}; constexpr std::array PointClamp{0x92u, (4u * 256u) << 12u, 0u, 0u}; constexpr std::array LinearWrap{0u, (4u * 256u) << 12u, (1u << 20u) | (1u << 22u), 0u}; @@ -2278,6 +2296,84 @@ void RunPointerHopTests(AgcDriver::VulkanDevice& device) { std::puts("pointer table with a pointer hop: a T# read through a pointer loaded from a record pointer whose low word passes Phis with a constant and a Ballot word checked, by the records' values"); } +void RunPointerFixedVsharpTests(AgcDriver::VulkanDevice& device, std::uint32_t descriptorOffset) { + Fill(); + alignas(256) std::array, 2> records{}; + alignas(256) std::array, 2> palettes{}; + Table = {}; + Keys = {}; + const std::array images{Image(FloatTexels.data(), Format32Float), Image(OtherFloatTexels.data(), Format32Float)}; + const auto point = [&](std::uint32_t record, std::uint32_t palette) { + const auto descriptor = Buffer(palettes[palette].data(), static_cast(sizeof(palettes[palette]))); + std::copy(descriptor.begin(), descriptor.end(), records[record].begin() + 40u); + }; + for (std::uint32_t palette = 0; palette < 2u; ++palette) { + std::copy(images[palette].begin(), images[palette].end(), palettes[palette].begin() + descriptorOffset / 4u); + point(palette, palette); + } + const auto link = [&](std::uint32_t record, std::uint32_t target) { + const auto pointer = reinterpret_cast(records[target].data()); + Table[record * 4u + 2u] = static_cast(pointer); + Table[record * 4u + 3u] = static_cast(pointer >> 32u); + }; + for (std::uint32_t record = 0; record < 3u; ++record) link(record, record % 2u); + const std::string name = std::string("pointer V# table with a T# at fixed offset ") + Hex(descriptorOffset); + const auto run = [&] { + const auto table = Buffer(Table.data(), 64u); + const auto keys = Buffer(Keys.data(), static_cast(sizeof(Keys))); + const auto output = Buffer(Output.data(), static_cast(sizeof(Output))); + Srt = {}; + std::copy(table.begin(), table.end(), Srt.begin()); + std::copy(keys.begin(), keys.end(), Srt.begin() + 4u); + std::copy(output.begin(), output.end(), Srt.begin() + 8u); + std::copy(PointWrap.begin(), PointWrap.end(), Srt.begin() + 12u); + const std::span code = descriptorOffset == 0u ? std::span(PointerFixedVsharpCode) : std::span(PointerFixedVsharpOffsetCode); + const std::vector memory{ + {reinterpret_cast(code.data()), std::as_bytes(code)}, + {reinterpret_cast(Srt.data()), std::as_bytes(std::span(Srt))}, + {reinterpret_cast(Table.data()), std::as_bytes(std::span(Table))}, + {reinterpret_cast(Keys.data()), std::as_bytes(std::span(Keys))}, + {reinterpret_cast(records.data()), std::as_bytes(std::span(records))}, + {reinterpret_cast(palettes.data()), std::as_bytes(std::span(palettes))}, + }; + const auto inputTable = Table; + const auto inputRecords = records; + const auto inputPalettes = palettes; + auto result = Execute(device, code, Srt.data(), memory, 4u, Lanes, 64u); + Require(Table == inputTable && records == inputRecords && palettes == inputPalettes, name + ": dispatch modified its input memory"); + Require(std::all_of(Output.begin() + 4u * Lanes * 4u, Output.end(), [](auto word) { return word == 0xdeadbeefu; }), name + ": dispatch wrote past its output"); + return result; + }; + const std::array, 4> keys{{{1u, 2u}, {2u, 1u}, {1u, 1u}, {2u, 2u}}}; + for (std::uint32_t group = 0; group < 4u; ++group) { + Keys[group * 4u] = keys[group][0]; + Keys[group * 4u + 1u] = keys[group][1]; + } + std::array paletteOf{0u, 1u, 0u}; + const auto sampled = [&](std::uint32_t group) { + return std::function([&, group](std::uint32_t x) { + const auto record = keys[group][x < 32u ? 0u : 1u]; + return std::bit_cast(paletteOf[record] != 0u ? OtherFloatTexels[x] : FloatTexels[x]); + }); + }; + const auto taken = run(); + Require(!Faulted(taken), name + ": the T# read through the V# behind the record's pointer faulted:\n" + taken.log); + for (std::uint32_t group = 0; group < 4u; ++group) RequireRed(taken, group, Lanes, sampled(group), name + ": the T# read at a fixed offset of the V# behind the record's pointer in a waterfall loop"); + link(1u, 0u); + paletteOf[1] = 0u; + const auto moved = run(); + Require(!Faulted(moved), name + ": the T# behind a moved record pointer faulted:\n" + moved.log); + Require(moved.artifactId == taken.artifactId, name + ": moving a record's pointer recompiled the shader"); + for (std::uint32_t group = 0; group < 4u; ++group) RequireRed(moved, group, Lanes, sampled(group), name + ": the T# after record 1's pointer moved to the other V#"); + point(0u, 1u); + paletteOf = {1u, 1u, 1u}; + const auto repointed = run(); + Require(!Faulted(repointed), name + ": the T# behind a moved V# faulted:\n" + repointed.log); + Require(repointed.artifactId == taken.artifactId, name + ": moving the V# behind a record's pointer recompiled the shader"); + for (std::uint32_t group = 0; group < 4u; ++group) RequireRed(repointed, group, Lanes, sampled(group), name + ": the T# after the V# behind records 0 and 1 moved to the other palette"); + std::puts((name + ": a T# read at a fixed offset of a V# loaded through a record pointer checked, by the records' values").c_str()); +} + void RunSplitRecordTests(AgcDriver::VulkanDevice& device) { Fill(); const auto floatImage = Image(FloatTexels.data(), Format32Float); @@ -2547,6 +2643,14 @@ int main(int argc, char** argv) { RunPointerHopTests(*device); return 0; } + if (argc == 2 && std::strcmp(argv[1], "--pointer-vsharp-fixed") == 0) { + RunPointerFixedVsharpTests(*device, 0u); + return 0; + } + if (argc == 2 && std::strcmp(argv[1], "--pointer-vsharp-fixed-offset") == 0) { + RunPointerFixedVsharpTests(*device, 0x40u); + return 0; + } RunTests(*device); RunBorderSwizzleTests(*device); RunAnisoOverrideTests(*device); From 6a72841083bf4e8da213cabf74619dd3f4102a14 Mon Sep 17 00:00:00 2001 From: Yunus Shaikh <75296019+EagleBearerYS@users.noreply.github.com> Date: Thu, 22 Oct 2026 02:30:10 +0000 Subject: [PATCH 04/24] fix(shader): follow a conditional pointer chase through a V# behind a pointer-keyed record pointer A T# read at a pointer that a shader chases conditionally from a pointer-keyed record pointer, loading a V# at a fixed offset of the pointer and reading the next pointer from that buffer, stayed unplanned: the merged pointer's chase arms read another buffer at another offset than the record word, so PointerRecordRead refused them (Ratchet & Clank: Rift Apart stops at compute LoadAddressU32 with this shape). The walk now takes such an arm when its V# is read from a pointer that is the record pointer or another chase arm, records each distinct chase as a hop of the column, and refuses a chase that reaches itself or a depth over 4. The snapshot then also captures every record pointer's chase closure up to that depth as pointer-keyed entries, each with the T# at its own offset, so the GPU's lookup finds the pointer the shader reached. --- core/libs/prx/libSceAgcDriver/CMakeLists.txt | 5 +- .../tests/execution/ImageTable.cpp | 142 +++++++++++++++ .../IrMetadata/ControlFlowInfo.hpp | 15 +- .../Optimization/src/ResourceMaterializer.cpp | 90 +++++++-- .../Optimization/src/ResourceTracker.cpp | 171 +++++++++++++++--- core/shader/recompiler/ShaderDiskCache.cpp | 21 ++- core/shader/recompiler/ShaderDiskCache.hpp | 2 +- docs/dev/debt/shader.md | 1 + 8 files changed, 393 insertions(+), 54 deletions(-) diff --git a/core/libs/prx/libSceAgcDriver/CMakeLists.txt b/core/libs/prx/libSceAgcDriver/CMakeLists.txt index e72d2cd94a9e..2af4527c9624 100644 --- a/core/libs/prx/libSceAgcDriver/CMakeLists.txt +++ b/core/libs/prx/libSceAgcDriver/CMakeLists.txt @@ -2451,11 +2451,12 @@ if(BUILD_TESTING) add_test(NAME agc_driver_pointer_phi_lane_not_table COMMAND agc_driver_image_table_tests --pointer-phi-lane-not) add_test(NAME agc_driver_pointer_vsharp_select_table COMMAND agc_driver_image_table_tests --pointer-vsharp-select) add_test(NAME agc_driver_pointer_hop_table COMMAND agc_driver_image_table_tests --pointer-hop) + add_test(NAME agc_driver_pointer_chase_table COMMAND agc_driver_image_table_tests --pointer-chase) add_test(NAME agc_driver_pointer_vsharp_fixed_table COMMAND agc_driver_image_table_tests --pointer-vsharp-fixed) add_test(NAME agc_driver_pointer_vsharp_fixed_offset_table COMMAND agc_driver_image_table_tests --pointer-vsharp-fixed-offset) - set_tests_properties(agc_driver_nested_image_table agc_driver_nested_image_table_large agc_driver_nested_image_table_budget agc_driver_nested_sampler_table agc_driver_pointer_phi_sampler_table agc_driver_pointer_phi_ballot_table agc_driver_pointer_phi_copies_table agc_driver_pointer_phi_lane_and_table agc_driver_pointer_phi_high_table agc_driver_pointer_phi_high_second_table agc_driver_pointer_phi_high_second_lane_table agc_driver_pointer_phi_lane_not_table agc_driver_pointer_vsharp_select_table agc_driver_pointer_hop_table agc_driver_pointer_vsharp_fixed_table agc_driver_pointer_vsharp_fixed_offset_table PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_NO_PIPELINE_JOURNAL=1" SKIP_RETURN_CODE 77 TIMEOUT 30 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") + set_tests_properties(agc_driver_nested_image_table agc_driver_nested_image_table_large agc_driver_nested_image_table_budget agc_driver_nested_sampler_table agc_driver_pointer_phi_sampler_table agc_driver_pointer_phi_ballot_table agc_driver_pointer_phi_copies_table agc_driver_pointer_phi_lane_and_table agc_driver_pointer_phi_high_table agc_driver_pointer_phi_high_second_table agc_driver_pointer_phi_high_second_lane_table agc_driver_pointer_phi_lane_not_table agc_driver_pointer_vsharp_select_table agc_driver_pointer_hop_table agc_driver_pointer_chase_table agc_driver_pointer_vsharp_fixed_table agc_driver_pointer_vsharp_fixed_offset_table PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_NO_PIPELINE_JOURNAL=1" SKIP_RETURN_CODE 77 TIMEOUT 30 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") if(WIN32) - set_tests_properties(agc_driver_nested_image_table agc_driver_nested_image_table_large agc_driver_nested_image_table_budget agc_driver_nested_sampler_table agc_driver_pointer_phi_sampler_table agc_driver_pointer_phi_ballot_table agc_driver_pointer_phi_copies_table agc_driver_pointer_phi_lane_and_table agc_driver_pointer_phi_high_table agc_driver_pointer_phi_high_second_table agc_driver_pointer_phi_high_second_lane_table agc_driver_pointer_phi_lane_not_table agc_driver_pointer_vsharp_select_table agc_driver_pointer_hop_table agc_driver_pointer_vsharp_fixed_table agc_driver_pointer_vsharp_fixed_offset_table PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") + set_tests_properties(agc_driver_nested_image_table agc_driver_nested_image_table_large agc_driver_nested_image_table_budget agc_driver_nested_sampler_table agc_driver_pointer_phi_sampler_table agc_driver_pointer_phi_ballot_table agc_driver_pointer_phi_copies_table agc_driver_pointer_phi_lane_and_table agc_driver_pointer_phi_high_table agc_driver_pointer_phi_high_second_table agc_driver_pointer_phi_high_second_lane_table agc_driver_pointer_phi_lane_not_table agc_driver_pointer_vsharp_select_table agc_driver_pointer_hop_table agc_driver_pointer_chase_table agc_driver_pointer_vsharp_fixed_table agc_driver_pointer_vsharp_fixed_offset_table PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() add_test(NAME agc_pipeline_journal COMMAND agc_driver_graphics_tests --pipeline-journal) add_test(NAME agc_pipeline_journal_long_paths COMMAND agc_driver_graphics_tests --pipeline-journal-long-paths) diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp index 0b9d83f672eb..cb0ac2e8629e 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp @@ -425,6 +425,39 @@ alignas(256) constexpr std::array PointerHopCode{ 0xf09c0f08u, 0x01090401u, 0x8afe3032u, 0xbf89ffe4u, 0xbefe042eu, 0x7e120202u, 0x34101286u, 0x4a101100u, 0x34101084u, 0xe0781000u, 0x80070408u, 0xbf810000u, }; +alignas(256) constexpr std::array PointerChaseCode{ + 0xf4080100u, 0xfa000000u, 0xf4080200u, 0xfa000010u, 0xf4080700u, 0xfa000020u, 0xf4080800u, 0xfa000030u, + 0x8f108402u, 0xf4280404u, 0x20000000u, 0x360200bfu, 0x7e020d01u, 0x060202f0u, 0x100202ffu, 0x3c800000u, + 0x060202f2u, 0x7e0402ffu, 0x3e000000u, 0xbf8cc07fu, 0x7d8800a0u, 0x7e140210u, 0x02061411u, 0xbeae047eu, + 0x7e280503u, 0xd4c20030u, 0x00020614u, 0xbeb22430u, 0x8f158414u, 0xf4240b02u, 0x2a000008u, 0xbf8cc07fu, + 0xbf0d8012u, 0xbf840006u, 0xf4080f16u, 0xfa000060u, 0xbf8cc07fu, 0xf4240b1eu, 0xfa000000u, 0xbf8cc07fu, + 0xbf0d8112u, 0xbf840006u, 0xf4081016u, 0xfa000060u, 0xbf8cc07fu, 0xf4240b20u, 0xfa000000u, 0xbf8cc07fu, + 0xf40c0916u, 0xfa000000u, 0xbf8cc07fu, 0xf09c0f08u, 0x01090401u, 0x8afe3032u, 0xbf89ffe1u, 0xbefe042eu, + 0x7e120202u, 0x34101286u, 0x4a101100u, 0x34101084u, 0xe0781000u, 0x80070408u, 0xbf810000u, +}; +alignas(256) constexpr std::array PointerChaseLoopCode{ + 0xf4080100u, 0xfa000000u, 0xf4080200u, 0xfa000010u, 0xf4080700u, 0xfa000020u, 0xf4080800u, 0xfa000030u, + 0x8f108402u, 0xf4280404u, 0x20000000u, 0x360200bfu, 0x7e020d01u, 0x060202f0u, 0x100202ffu, 0x3c800000u, + 0x060202f2u, 0x7e0402ffu, 0x3e000000u, 0xbf8cc07fu, 0x7d8800a0u, 0x7e140210u, 0x02061411u, 0xbeae047eu, + 0x7e280503u, 0xd4c20030u, 0x00020614u, 0xbeb22430u, 0x8f158414u, 0xf4240b02u, 0x2a000008u, 0xbf8cc07fu, + 0xbe960312u, 0xbf068016u, 0xbf850008u, 0xf4080f16u, 0xfa000060u, 0xbf8cc07fu, 0xf4240b1eu, 0xfa000000u, + 0xbf8cc07fu, 0x80968116u, 0xbf82fff6u, 0xf40c0916u, 0xfa000000u, 0xbf8cc07fu, 0xf09c0f08u, 0x01090401u, + 0x8afe3032u, 0xbf89ffe6u, 0xbefe042eu, 0x7e120202u, 0x34101286u, 0x4a101100u, 0x34101084u, 0xe0781000u, + 0x80070408u, 0xbf810000u, +}; +alignas(256) constexpr std::array PointerChaseDeepCode{ + 0xf4080100u, 0xfa000000u, 0xf4080200u, 0xfa000010u, 0xf4080700u, 0xfa000020u, 0xf4080800u, 0xfa000030u, + 0x8f108402u, 0xf4280404u, 0x20000000u, 0x360200bfu, 0x7e020d01u, 0x060202f0u, 0x100202ffu, 0x3c800000u, + 0x060202f2u, 0x7e0402ffu, 0x3e000000u, 0xbf8cc07fu, 0x7d8800a0u, 0x7e140210u, 0x02061411u, 0xbeae047eu, + 0x7e280503u, 0xd4c20030u, 0x00020614u, 0xbeb22430u, 0x8f158414u, 0xf4240b02u, 0x2a000008u, 0xbf8cc07fu, + 0xbf0d8012u, 0xbf840006u, 0xf4080f16u, 0xfa000060u, 0xbf8cc07fu, 0xf4240b1eu, 0xfa000000u, 0xbf8cc07fu, + 0xbf0d8112u, 0xbf840006u, 0xf4081016u, 0xfa000060u, 0xbf8cc07fu, 0xf4240b20u, 0xfa000000u, 0xbf8cc07fu, + 0xbf0d8212u, 0xbf840006u, 0xf4081116u, 0xfa000060u, 0xbf8cc07fu, 0xf4240b22u, 0xfa000000u, 0xbf8cc07fu, + 0xbf0d8312u, 0xbf840006u, 0xf4081216u, 0xfa000060u, 0xbf8cc07fu, 0xf4240b24u, 0xfa000000u, 0xbf8cc07fu, + 0xbf0d8412u, 0xbf840006u, 0xf4081316u, 0xfa000060u, 0xbf8cc07fu, 0xf4240b26u, 0xfa000000u, 0xbf8cc07fu, + 0xf40c0916u, 0xfa000000u, 0xbf8cc07fu, 0xf09c0f08u, 0x01090401u, 0x8afe3032u, 0xbf89ffc9u, 0xbefe042eu, + 0x7e120202u, 0x34101286u, 0x4a101100u, 0x34101084u, 0xe0781000u, 0x80070408u, 0xbf810000u, +}; alignas(256) constexpr std::array PointerFixedVsharpCode{ 0xf4080100u, 0xfa000000u, 0xf4080200u, 0xfa000010u, 0xf4080700u, 0xfa000020u, 0xf4080800u, 0xfa000030u, 0x8f108402u, 0xf4280404u, 0x20000000u, 0x360200bfu, 0x7e020d01u, 0x060202f0u, 0x100202ffu, 0x3c800000u, @@ -2296,6 +2329,111 @@ void RunPointerHopTests(AgcDriver::VulkanDevice& device) { std::puts("pointer table with a pointer hop: a T# read through a pointer loaded from a record pointer whose low word passes Phis with a constant and a Ballot word checked, by the records' values"); } +void RunPointerChaseTests(AgcDriver::VulkanDevice& device) { + Fill(); + alignas(256) std::array, 4> nodes{}; + alignas(256) std::array, 4> links{}; + alignas(256) std::array unsnapshotted{}; + Table = {}; + Keys = {}; + const std::array images{Image(FloatTexels.data(), Format32Float), Image(OtherFloatTexels.data(), Format32Float)}; + const std::array imageOf{0u, 1u, 0u, 1u}; + std::array next{}; + const auto address = [](const void* data) { return static_cast(reinterpret_cast(data)); }; + const auto chase = [&](std::uint32_t from, std::uint32_t to, std::uint32_t* link) { + link[0] = static_cast(address(nodes[to].data())); + link[1] = static_cast(address(nodes[to].data()) >> 32u); + const auto descriptor = Buffer(link, 8u); + std::copy(descriptor.begin(), descriptor.end(), nodes[from].begin() + 0x60u / 4u); + next[from] = to; + }; + for (std::uint32_t node = 0; node < 4u; ++node) { + std::copy(images[imageOf[node]].begin(), images[imageOf[node]].end(), nodes[node].begin()); + chase(node, node == 3u ? 3u : (node + 1u) % 3u, links[node].data()); + } + const std::array recordNode{2u, 0u, 1u, 2u}; + for (std::uint32_t record = 0; record < 4u; ++record) { + Table[record * 4u + 2u] = static_cast(address(nodes[recordNode[record]].data())); + Table[record * 4u + 3u] = static_cast(address(nodes[recordNode[record]].data()) >> 32u); + } + const std::string name = "pointer table with a conditional V# chase"; + struct Registration { + void* data; + Registration(void* data, std::size_t bytes) : data(data) { GuestAllocations::Mutation().Add(data, bytes, true, true, true); } + ~Registration() { GuestAllocations::Mutation().Remove(data); } + }; + const Registration registeredNodes(nodes.data(), sizeof(nodes)); + const Registration registeredLinks(links.data(), sizeof(links)); + const Registration registeredUnsnapshotted(unsnapshotted.data(), sizeof(unsnapshotted)); + const auto run = [&](std::span code) { + const auto table = Buffer(Table.data(), 64u); + const auto keys = Buffer(Keys.data(), static_cast(sizeof(Keys))); + const auto output = Buffer(Output.data(), static_cast(sizeof(Output))); + Srt = {}; + std::copy(table.begin(), table.end(), Srt.begin()); + std::copy(keys.begin(), keys.end(), Srt.begin() + 4u); + std::copy(output.begin(), output.end(), Srt.begin() + 8u); + std::copy(PointWrap.begin(), PointWrap.end(), Srt.begin() + 12u); + const std::vector memory{ + {reinterpret_cast(code.data()), std::as_bytes(code)}, + {reinterpret_cast(Srt.data()), std::as_bytes(std::span(Srt))}, + {reinterpret_cast(Table.data()), std::as_bytes(std::span(Table))}, + {reinterpret_cast(Keys.data()), std::as_bytes(std::span(Keys))}, + {reinterpret_cast(nodes.data()), std::as_bytes(std::span(nodes))}, + {reinterpret_cast(links.data()), std::as_bytes(std::span(links))}, + }; + const auto inputTable = Table; + const auto inputNodes = nodes; + const auto inputLinks = links; + auto result = Execute(device, code, Srt.data(), memory, 4u, Lanes, 64u); + Require(Table == inputTable && nodes == inputNodes && links == inputLinks, name + ": dispatch modified its input memory"); + Require(std::all_of(Output.begin() + 4u * Lanes * 4u, Output.end(), [](auto word) { return word == 0xdeadbeefu; }), name + ": dispatch wrote past its output"); + return result; + }; + const std::array, 4> keys{{{1u, 2u}, {2u, 1u}, {1u, 1u}, {2u, 2u}}}; + for (std::uint32_t group = 0; group < 4u; ++group) { + Keys[group * 4u] = keys[group][0]; + Keys[group * 4u + 1u] = keys[group][1]; + Keys[group * 4u + 2u] = group; + } + const auto sampled = [&](std::uint32_t group) { + return std::function([&, group](std::uint32_t x) { + auto node = recordNode[keys[group][x < 32u ? 0u : 1u]]; + for (std::uint32_t step = 0; step < 2u; ++step) { + if ((group >> step) & 1u) node = next[node]; + } + return std::bit_cast(imageOf[node] != 0u ? OtherFloatTexels[x] : FloatTexels[x]); + }); + }; + const auto untouched = [](const Outcome& outcome) { return std::all_of(outcome.words.begin(), outcome.words.end(), [](auto word) { return word == 0xdeadbeefu; }); }; + const auto taken = run(PointerChaseCode); + Require(!Faulted(taken), name + ": the T# at the end of the chase faulted:\n" + taken.log); + for (std::uint32_t group = 0; group < 4u; ++group) RequireRed(taken, group, Lanes, sampled(group), name + ": the T# at the pointer reached by the chase steps taken in group " + std::to_string(group)); + chase(0u, 3u, links[0].data()); + const auto moved = run(PointerChaseCode); + Require(!Faulted(moved), name + ": the T# behind a moved chase link faulted:\n" + moved.log); + Require(moved.artifactId == taken.artifactId, name + ": moving a chased link recompiled the shader"); + for (std::uint32_t group = 0; group < 4u; ++group) RequireRed(moved, group, Lanes, sampled(group), name + ": the T# after node 0's link moved to node 3 in group " + std::to_string(group)); + chase(0u, 1u, links[0].data()); + chase(1u, 3u, unsnapshotted.data()); + const auto outside = run(PointerChaseCode); + Require(Faulted(outside) && outside.log.find("outside the snapshot") != std::string::npos, name + ": a link outside the snapshot did not fault as outside the snapshot:\n" + outside.log); + Require(untouched(outside), name + ": a link outside the snapshot published copied output"); + Require(outside.artifactId == taken.artifactId, name + ": a link outside the snapshot recompiled the shader"); + const auto refused = [&](std::span code, const std::string& what) { + std::string refusal; + try { + static_cast(run(code)); + } catch (const std::runtime_error& error) { + refusal = error.what(); + } + Require(refusal.find("not a valid runtime value") != std::string::npos, name + ": " + what + " was not refused by resource planning: " + refusal); + }; + refused(PointerChaseLoopCode, "a chase repeated in a loop"); + refused(PointerChaseDeepCode, "five conditional chase steps"); + std::puts("pointer table with a conditional V# chase: a T# at a record pointer chased up to twice through a V# at the pointer plus 0x60 checked for every taken and skipped step, a moved link and a link outside the snapshot, and a chase in a loop and five chase steps refused"); +} + void RunPointerFixedVsharpTests(AgcDriver::VulkanDevice& device, std::uint32_t descriptorOffset) { Fill(); alignas(256) std::array, 2> records{}; @@ -2643,6 +2781,10 @@ int main(int argc, char** argv) { RunPointerHopTests(*device); return 0; } + if (argc == 2 && std::strcmp(argv[1], "--pointer-chase") == 0) { + RunPointerChaseTests(*device); + return 0; + } if (argc == 2 && std::strcmp(argv[1], "--pointer-vsharp-fixed") == 0) { RunPointerFixedVsharpTests(*device, 0u); return 0; diff --git a/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ControlFlowInfo.hpp b/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ControlFlowInfo.hpp index d114f59fc3e1..7e869d531b7a 100644 --- a/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ControlFlowInfo.hpp +++ b/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ControlFlowInfo.hpp @@ -35,6 +35,17 @@ struct PointerHop { bool operator==(const PointerHop& other) const = default; }; +inline constexpr std::uint32_t MaxPointerChaseDepth = 4; + +struct PointerChase { + std::uint32_t offset = 0; + std::uint32_t immediate = 0; + std::uint32_t readOffset = 0; + std::uint32_t readImmediate = 0; + + bool operator==(const PointerChase& other) const = default; +}; + struct TableColumn { std::uint32_t heapSource = 0; std::uint32_t stride = 0; @@ -51,6 +62,8 @@ struct TableColumn { std::uint32_t indirectOffset = 0; std::uint32_t indirectImmediate = 0; std::vector pointerHops; + std::vector pointerChases; + std::uint32_t pointerChaseDepth = 0; std::uint32_t foldedAddend = 0; std::uint32_t foldedCarry = 0; bool selected = false; @@ -58,7 +71,7 @@ struct TableColumn { bool pointerKeyed = false; bool operator==(const TableColumn& other) const { - return heapSource == other.heapSource && stride == other.stride && addend == other.addend && offset == other.offset && dwordCount == other.dwordCount && sampler == other.sampler && address == other.address && buffer == other.buffer && indirect == other.indirect && indirectOffset == other.indirectOffset && indirectImmediate == other.indirectImmediate && pointerHops == other.pointerHops && foldedAddend == other.foldedAddend && foldedCarry == other.foldedCarry && selected == other.selected && selection == other.selection && pointerKeyed == other.pointerKeyed; + return heapSource == other.heapSource && stride == other.stride && addend == other.addend && offset == other.offset && dwordCount == other.dwordCount && sampler == other.sampler && address == other.address && buffer == other.buffer && indirect == other.indirect && indirectOffset == other.indirectOffset && indirectImmediate == other.indirectImmediate && pointerHops == other.pointerHops && pointerChases == other.pointerChases && pointerChaseDepth == other.pointerChaseDepth && foldedAddend == other.foldedAddend && foldedCarry == other.foldedCarry && selected == other.selected && selection == other.selection && pointerKeyed == other.pointerKeyed; } }; diff --git a/core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp b/core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp index ec5f4d77ca45..f481f175c7ed 100644 --- a/core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp +++ b/core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -949,6 +950,55 @@ class TableSnapshotter { if (column.buffer) parentKeysRemaining -= records.keys; } + std::optional Chase(std::uint64_t pointer, const PointerChase& chase) { + DescriptorValue descriptor; + descriptor.dwordCount = 4u; + for (std::uint32_t dword = 0; dword < 4u; dword++) { + const auto address = ImageTableAbi::ScalarAddressDword(pointer, chase.offset, chase.immediate + dword * 4u); + if (!address.has_value() || (*address & 3u) != 0u || !AddressAccessible(*address)) return std::nullopt; + descriptor.dwords[dword] = Read(*address); + descriptor.availableDwordMask |= static_cast(1u << dword); + tables.ranges.emplace_back(*address, sizeof(std::uint32_t)); + } + const auto buffer = decodeBufferDescriptor(descriptor); + if (buffer.Type() != 0u) return std::nullopt; + const auto low = ImageTableAbi::ScalarBufferDword(chase.readOffset, chase.readImmediate, buffer.GetSize()); + const auto high = ImageTableAbi::ScalarBufferDword(chase.readOffset, chase.readImmediate + 4u, buffer.GetSize()); + if (!low.has_value() || !high.has_value() || !AddressAccessible(buffer.Base48() + *low) || !AddressAccessible(buffer.Base48() + *high)) return std::nullopt; + tables.ranges.emplace_back(buffer.Base48() + *low, sizeof(std::uint32_t)); + tables.ranges.emplace_back(buffer.Base48() + *high, sizeof(std::uint32_t)); + return (static_cast(Read(buffer.Base48() + *high)) << 32u) | Read(buffer.Base48() + *low); + } + + template + void ChaseClosure(const TableColumn& column, ColumnRecords& records, std::uint32_t limit, const ReadEntry& readEntry) { + std::unordered_set known; + std::vector frontier; + for (std::uint32_t record = 0; record < records.keys; record++) { + const auto pointer = records.pointers[record]; + if (pointer != ImageTableAbi::UncapturedPointer && known.insert(pointer).second) frontier.push_back(pointer); + } + for (std::uint32_t depth = 0; depth < column.pointerChaseDepth && !frontier.empty(); depth++) { + std::vector reached; + for (const auto pointer : frontier) { + for (const auto& chase : column.pointerChases) { + const auto next = Chase(pointer, chase); + if (!next.has_value() || *next == 0u || !known.insert(*next).second) continue; + if (records.keys >= limit) { + records.outside = true; + continue; + } + records.states.push_back(RecordState::Read); + records.words.push_back(0u); + records.pointers.push_back(*next); + readEntry(records.keys++, *next); + reached.push_back(*next); + } + } + frontier = std::move(reached); + } + } + void ReadColumn(const TableColumn& column, ColumnRecords& records, std::uint32_t limit) { stamp = {}; comparedRuns.clear(); @@ -968,29 +1018,18 @@ class TableSnapshotter { if (column.indirect) { unread(); if (column.buffer || column.pointerKeyed) records.pointers.assign(records.keys, ImageTableAbi::UncapturedPointer); - for (std::uint32_t record = 0; record < records.keys; record++) { - if (records.states[record] != RecordState::Read) continue; - const auto offset = column.addend + record * column.stride; - const auto lowWord = ImageTableAbi::ScalarBufferDword(offset, column.offset, records.size); - const auto highWord = ImageTableAbi::ScalarBufferDword(offset, column.offset + 4u, records.size); - if (!lowWord.has_value() || !highWord.has_value() || !PageAccessible(records.base + *lowWord, records.base, end) || !PageAccessible(records.base + *highWord, records.base, end)) { - records.states[record] = RecordState::Unmapped; - continue; - } - std::uint64_t pointer = (static_cast(Read(records.base + *highWord)) << 32u) | Read(records.base + *lowWord); - if (column.pointerKeyed) records.pointers[record] = pointer; + const auto readEntry = [&](std::uint32_t entry, std::uint64_t pointer) { for (const auto& hop : column.pointerHops) { const auto low = ImageTableAbi::ScalarAddressDword(pointer, hop.offset, hop.immediate); const auto high = ImageTableAbi::ScalarAddressDword(pointer, hop.offset, hop.immediate + 4u); if (!low.has_value() || !high.has_value() || (*low & 3u) != 0u || *high != *low + sizeof(std::uint32_t) || !AddressAccessible(*low) || !AddressAccessible(*high)) { - records.states[record] = RecordState::Unmapped; - break; + records.states[entry] = RecordState::Unmapped; + return; } pointer = (static_cast(Read(*high)) << 32u) | Read(*low); tables.ranges.emplace_back(*low, 2u * sizeof(std::uint32_t)); } - if (records.states[record] != RecordState::Read) continue; - if (column.buffer) records.pointers[record] = pointer; + if (column.buffer) records.pointers[entry] = pointer; DescriptorValue value; value.dwordCount = column.sampler || column.buffer ? 4u : 8u; std::uint64_t first = std::numeric_limits::max(); @@ -999,19 +1038,32 @@ class TableSnapshotter { auto address = ImageTableAbi::ScalarAddressDword(pointer, column.indirectOffset, column.indirectImmediate + dword * 4u); if (address.has_value() && (*address & 3u) != 0u) address.reset(); if (!address.has_value() || !AddressAccessible(*address)) { - records.states[record] = RecordState::Unmapped; - break; + records.states[entry] = RecordState::Unmapped; + return; } value.dwords[dword] = Read(*address); value.availableDwordMask |= static_cast(1u << dword); first = std::min(first, *address); past = std::max(past, *address + sizeof(std::uint32_t)); } - if (records.states[record] != RecordState::Read) continue; - records.words[record] = WordsIndex(records, value); + records.words[entry] = WordsIndex(records, value); tables.ranges.emplace_back(first, past - first); if (column.buffer) records.readRanges.emplace_back(first, past - first); + }; + for (std::uint32_t record = 0; record < records.keys; record++) { + if (records.states[record] != RecordState::Read) continue; + const auto offset = column.addend + record * column.stride; + const auto lowWord = ImageTableAbi::ScalarBufferDword(offset, column.offset, records.size); + const auto highWord = ImageTableAbi::ScalarBufferDword(offset, column.offset + 4u, records.size); + if (!lowWord.has_value() || !highWord.has_value() || !PageAccessible(records.base + *lowWord, records.base, end) || !PageAccessible(records.base + *highWord, records.base, end)) { + records.states[record] = RecordState::Unmapped; + continue; + } + const std::uint64_t pointer = (static_cast(Read(records.base + *highWord)) << 32u) | Read(records.base + *lowWord); + if (column.pointerKeyed) records.pointers[record] = pointer; + readEntry(record, pointer); } + if (column.pointerKeyed && column.pointerChaseDepth != 0u) ChaseClosure(column, records, limit, readEntry); return; } const bool memoized = TableSnapshotReuse() && !records.narrowed && records.keys != 0u; diff --git a/core/shader/recompiler/Optimization/src/ResourceTracker.cpp b/core/shader/recompiler/Optimization/src/ResourceTracker.cpp index fba2fc696798..877753529263 100644 --- a/core/shader/recompiler/Optimization/src/ResourceTracker.cpp +++ b/core/shader/recompiler/Optimization/src/ResourceTracker.cpp @@ -898,36 +898,147 @@ class Tracker { return true; } - bool PointerRecordPairs(IrValue* low, IrValue* high, std::vector& lowReads, std::vector& highReads) const { - std::vector> pending{{low, high}}; - std::set> seen; - while (!pending.empty()) { - auto* lowWord = pending.back().first->Resolve(); - auto* highWord = pending.back().second->Resolve(); - pending.pop_back(); - if (!seen.emplace(lowWord, highWord).second || PointerFreeWord(*lowWord)) continue; - if (lowWord->IsPhi()) { - const bool paired = highWord->IsPhi() && highWord->Parent() == lowWord->Parent(); - for (std::size_t index = 0; index < lowWord->ArgumentCount(); ++index) { - IrValue* edge = paired ? PhiIncoming(*highWord, lowWord->PhiBlock(index)) : highWord; - if (edge == nullptr) return false; - pending.emplace_back(lowWord->Argument(index), edge); - } - continue; - } - if (IrValue* word = MaskedRecordWord(*lowWord)) { - pending.emplace_back(word, highWord); - continue; - } - if (!PointerRecordRead(*lowWord, lowReads)) return false; - if (highWord->IsPhi()) { - for (std::size_t index = 0; index < highWord->ArgumentCount(); ++index) pending.emplace_back(lowWord, highWord->Argument(index)); - } else if (IrValue* word = MaskedRecordWord(*highWord)) { - pending.emplace_back(lowWord, word); - } else if (!PointerFreeWord(*highWord) && !PointerRecordRead(*highWord, highReads)) { + struct ChaseSite { + IrValue* handle = nullptr; + IrValue* pointer = nullptr; + PointerChase chase; + std::vector inner; + }; + + bool ChaseRead(IrValue& read, IrValue*& handle, std::uint32_t& readOffset, std::uint32_t& readImmediate) const { + std::uint32_t memory = 0; + const MemoryInfo* info = ScalarReadMemory(read, memory); + if (info == nullptr || !MemoryIndexBelongsTo(memory, read) || !immediateU32(read.Argument(1), readOffset)) return false; + handle = read.Argument(0)->Resolve(); + readImmediate = info->offset; + return handle->Opcode() == IrOpcode::GetBufferResource && handle->ArgumentCount() == 4u; + } + + bool ChaseVsharp(IrValue& handle, IrValue*& pointer, PointerChase& chase) const { + for (std::uint32_t dword = 0; dword < 4u; dword++) { + IrValue* word = handle.Argument(dword)->Resolve(); + std::uint32_t memoryIndex = 0; + const MemoryInfo* memory = ScalarAddressMemory(*word, memoryIndex); + std::uint32_t offset = 0; + if (memory == nullptr || !MemoryIndexBelongsTo(memoryIndex, *word) || !immediateU32(word->Argument(1), offset)) return false; + IrValue* address = word->Argument(0)->Resolve(); + if (address->Opcode() != IrOpcode::GetAddressResource || address->ArgumentCount() != 2u) return false; + if (dword == 0u) { + pointer = address; + chase.offset = offset; + chase.immediate = memory->offset; + } else if (offset != chase.offset || memory->offset != chase.immediate + dword * static_cast(sizeof(std::uint32_t)) || (address != pointer && !EquivalentValue(m_program.Resources(), pointer, address))) { return false; } } + return true; + } + + bool ChaseHighRead(IrValue& read, const IrValue& handle, std::uint32_t readOffset, std::uint32_t readImmediate) const { + IrValue* highHandle = nullptr; + std::uint32_t highOffset = 0; + std::uint32_t highImmediate = 0; + return ChaseRead(read, highHandle, highOffset, highImmediate) && (highHandle == &handle || EquivalentValue(m_program.Resources(), &handle, highHandle)) && highOffset == readOffset && highImmediate == readImmediate + static_cast(sizeof(std::uint32_t)); + } + + bool PointerRecordPairs(IrValue* low, IrValue* high, std::vector& lowReads, std::vector& highReads, std::vector& chases, std::uint32_t& chaseDepth) const { + std::vector sites; + std::vector looseHandles; + const auto site = [&](IrValue& handle, IrValue& pointer, const PointerChase& chase) { + const auto found = std::ranges::find_if(sites, [&](const ChaseSite& known) { return known.handle == &handle || EquivalentValue(m_program.Resources(), known.handle, &handle); }); + if (found != sites.end()) return static_cast(found - sites.begin()); + sites.push_back(ChaseSite{&handle, &pointer, chase, {}}); + return sites.size() - 1u; + }; + const auto walk = [&](IrValue* rootLow, IrValue* rootHigh, std::vector& reached) { + std::vector> pending{{rootLow, rootHigh}}; + std::set> seen; + while (!pending.empty()) { + auto* lowWord = pending.back().first->Resolve(); + auto* highWord = pending.back().second->Resolve(); + pending.pop_back(); + if (!seen.emplace(lowWord, highWord).second || PointerFreeWord(*lowWord)) continue; + if (lowWord->IsPhi()) { + const bool paired = highWord->IsPhi() && highWord->Parent() == lowWord->Parent(); + for (std::size_t index = 0; index < lowWord->ArgumentCount(); ++index) { + IrValue* edge = paired ? PhiIncoming(*highWord, lowWord->PhiBlock(index)) : highWord; + if (edge == nullptr) return false; + pending.emplace_back(lowWord->Argument(index), edge); + } + continue; + } + if (IrValue* word = MaskedRecordWord(*lowWord)) { + pending.emplace_back(word, highWord); + continue; + } + IrValue* handle = nullptr; + IrValue* pointer = nullptr; + PointerChase chase; + std::uint32_t looseOffset = 0; + std::uint32_t looseImmediate = 0; + if (ChaseRead(*lowWord, handle, chase.readOffset, chase.readImmediate) && ChaseVsharp(*handle, pointer, chase)) { + if (highWord->IsPhi()) { + for (std::size_t index = 0; index < highWord->ArgumentCount(); ++index) pending.emplace_back(lowWord, highWord->Argument(index)); + continue; + } + if (!ChaseHighRead(*highWord, *handle, chase.readOffset, chase.readImmediate)) return false; + const auto index = site(*handle, *pointer, chase); + if (std::ranges::find(reached, index) == reached.end()) reached.push_back(index); + continue; + } + if (!PointerRecordRead(*lowWord, lowReads)) return false; + if (highWord->IsPhi()) { + for (std::size_t index = 0; index < highWord->ArgumentCount(); ++index) pending.emplace_back(lowWord, highWord->Argument(index)); + } else if (IrValue* word = MaskedRecordWord(*highWord)) { + pending.emplace_back(lowWord, word); + } else if (PointerFreeWord(*highWord) || PointerRecordRead(*highWord, highReads)) { + continue; + } else if (IrValue* highHandle = nullptr; ChaseRead(*highWord, highHandle, looseOffset, looseImmediate)) { + looseHandles.push_back(highHandle); + } else { + return false; + } + } + return true; + }; + std::vector roots; + if (!walk(low, high, roots)) return false; + for (std::size_t index = 0; index < sites.size(); ++index) { + std::vector inner; + if (!walk(sites[index].pointer->Argument(0), sites[index].pointer->Argument(1), inner)) return false; + sites[index].inner = std::move(inner); + } + if (!std::ranges::all_of(looseHandles, [&](const IrValue* handle) { + return std::ranges::any_of(sites, [&](const ChaseSite& known) { return known.handle == handle || EquivalentValue(m_program.Resources(), known.handle, handle); }); + })) return false; + std::vector depths(sites.size(), 0u); + std::vector states(sites.size(), 0u); + const auto depthOf = [&](const auto& self, std::size_t index) -> std::optional { + if (states[index] == 2u) return depths[index]; + if (states[index] == 1u) return std::nullopt; + states[index] = 1u; + std::uint32_t deepest = 0; + for (const auto inner : sites[index].inner) { + const auto depth = self(self, inner); + if (!depth.has_value()) return std::nullopt; + deepest = std::max(deepest, *depth); + } + states[index] = 2u; + depths[index] = deepest + 1u; + return depths[index]; + }; + chaseDepth = 0; + for (const auto root : roots) { + const auto depth = depthOf(depthOf, root); + if (!depth.has_value()) return false; + chaseDepth = std::max(chaseDepth, *depth); + } + if (chaseDepth > MaxPointerChaseDepth) return false; + chases.clear(); + for (const auto& known : sites) { + if (std::ranges::find(chases, known.chase) == chases.end()) chases.push_back(known.chase); + } + if (chaseDepth == 0u) chases.clear(); return !lowReads.empty() && !highReads.empty(); } @@ -999,7 +1110,9 @@ class Tracker { if (!hops.empty() && buffer) return false; std::vector lowReads; std::vector highReads; - if (pointerKeyed ? !PointerRecordPairs(pointer->Argument(0), pointer->Argument(1), lowReads, highReads) : merged && !PointerRecordReads(pointer->Argument(0), lowReads)) return false; + std::vector chases; + std::uint32_t chaseDepth = 0; + if (pointerKeyed ? !PointerRecordPairs(pointer->Argument(0), pointer->Argument(1), lowReads, highReads, chases, chaseDepth) : merged && !PointerRecordReads(pointer->Argument(0), lowReads)) return false; auto* low = merged ? lowReads.back() : pointer->Argument(0)->Resolve(); auto* high = pointer->Argument(1)->Resolve(); std::uint32_t first = 0; @@ -1062,6 +1175,8 @@ class Tracker { column.indirectImmediate = descriptorImmediate; column.pointerKeyed = pointerKeyed; column.pointerHops = std::move(hops); + column.pointerChases = std::move(chases); + column.pointerChaseDepth = chaseDepth; const auto domain = merged ? std::optional{} : MatchKeyDomain(key); DescriptorSource columnSource = heapSource; columnSource.tableColumn = column; diff --git a/core/shader/recompiler/ShaderDiskCache.cpp b/core/shader/recompiler/ShaderDiskCache.cpp index 4797fa2e2417..4ee4f3e111a1 100644 --- a/core/shader/recompiler/ShaderDiskCache.cpp +++ b/core/shader/recompiler/ShaderDiskCache.cpp @@ -108,8 +108,8 @@ static_assert(sizeof(IrResourcePlan) == 728, "IrResourcePlan changed: update Enc static_assert(sizeof(IrValue) == 128, "IrValue changed: update the plan value encoder"); static_assert(sizeof(GuestRegister) == 8, "GuestRegister changed: update the plan value encoder"); static_assert(sizeof(MemoryInfo) == 80, "MemoryInfo changed: update the plan encoder"); -static_assert(sizeof(DescriptorSource) == 200, "DescriptorSource changed: update the plan encoder"); -static_assert(sizeof(TableColumn) == 120, "TableColumn changed: update the plan encoder"); +static_assert(sizeof(DescriptorSource) == 224, "DescriptorSource changed: update the plan encoder"); +static_assert(sizeof(TableColumn) == 144, "TableColumn changed: update the plan encoder"); static_assert(sizeof(KeyDomain) == 16, "KeyDomain changed: update the plan encoder"); static_assert(sizeof(ResourceBlock) == 104, "ResourceBlock changed: update the plan encoder"); static_assert(sizeof(SrtRead) == 16, "SrtRead changed: update the plan encoder"); @@ -149,7 +149,7 @@ static_assert(fieldCount() == 24, "IrResourcePlan changed: updat static_assert(fieldCount() == 2, "GuestRegister changed: update the plan value encoder"); static_assert(fieldCount() == 31, "MemoryInfo changed: update the plan encoder"); static_assert(fieldCount() == 3, "DescriptorSource changed: update the plan encoder"); -static_assert(fieldCount() == 20, "TableColumn changed: update the plan encoder"); +static_assert(fieldCount() == 22, "TableColumn changed: update the plan encoder"); static_assert(fieldCount() == 4, "KeyDomain changed: update the plan encoder"); static_assert(fieldCount() == 5, "ResourceBlock changed: update the plan encoder"); static_assert(fieldCount() == 2, "SrtRead changed: update the plan encoder"); @@ -1093,6 +1093,13 @@ bool encodePlan(Writer& writer, const IrResourcePlan& plan) { hops.Value(hop.offset); hops.Value(hop.immediate); }); + out.List(column.pointerChases, [](Writer& chases, const PointerChase& chase) { + chases.Value(chase.offset); + chases.Value(chase.immediate); + chases.Value(chase.readOffset); + chases.Value(chase.readImmediate); + }); + out.Value(column.pointerChaseDepth); out.Value(column.foldedAddend); out.Value(column.foldedCarry); out.Value(column.selected); @@ -1257,6 +1264,13 @@ bool decodePlan(Reader& reader, IrResourcePlan& plan) { hops.Value(hop.offset); hops.Value(hop.immediate); }); + in.List(column.pointerChases, 16, [](Reader& chases, PointerChase& chase) { + chases.Value(chase.offset); + chases.Value(chase.immediate); + chases.Value(chase.readOffset); + chases.Value(chase.readImmediate); + }); + in.Value(column.pointerChaseDepth); in.Value(column.foldedAddend); in.Value(column.foldedCarry); in.Value(column.selected); @@ -1303,6 +1317,7 @@ bool decodePlan(Reader& reader, IrResourcePlan& plan) { const auto& parent = plan.descriptorSources[column.heapSource].tableColumn; if (parent.has_value() && (column.buffer || column.address || column.indirect || !parent->buffer || !parent->indirect || plan.descriptorSources[parent->heapSource].tableColumn.has_value())) return false; if (column.pointerKeyed && (!column.indirect || column.buffer || parent.has_value())) return false; + if (column.pointerChases.empty() != (column.pointerChaseDepth == 0u) || column.pointerChaseDepth > MaxPointerChaseDepth || (column.pointerChaseDepth != 0u && !column.pointerKeyed)) return false; } if (!std::ranges::all_of(plan.info.buffers, [&](const BufferResource& buffer) { return knownSource(buffer.source); })) return false; if (!std::ranges::all_of(plan.info.images, [&](const ImageResource& image) { return knownSource(image.source); })) return false; diff --git a/core/shader/recompiler/ShaderDiskCache.hpp b/core/shader/recompiler/ShaderDiskCache.hpp index d007fc126a7a..d61a2b9e2002 100644 --- a/core/shader/recompiler/ShaderDiskCache.hpp +++ b/core/shader/recompiler/ShaderDiskCache.hpp @@ -13,7 +13,7 @@ namespace ShaderRecompiler::ShaderDiskCache { -inline constexpr std::uint32_t FormatVersion = 46; +inline constexpr std::uint32_t FormatVersion = 47; enum class LoadStatus { Loaded, diff --git a/docs/dev/debt/shader.md b/docs/dev/debt/shader.md index da23615fbaa8..37b7d1555711 100644 --- a/docs/dev/debt/shader.md +++ b/docs/dev/debt/shader.md @@ -42,6 +42,7 @@ - [Nested image tables](../../../core/shader/recompiler/Optimization/src/ResourceTracker.cpp) support a V# loaded through a pointer from one scalar-buffer record origin, followed by an independently indexed T# or S#. A low-pointer Phi may merge that origin with constants; the GPU looks up the actual 64-bit pointer and faults if it was not captured. Other origins, shared V# consumers, T# or S# words read by anything but their descriptor handle, and deeper buffer nesting remain refused. A snapshot scans at most 524288 parent records and holds 1048576 inner key slots across its nested columns; equal V#s share a child snapshot within a column. The final image-table map is limited to 32 MiB. Synthetic readback covers the domains reported in #6191; complete title behavior remains unverified. - [Image tables](../../../core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp) read by `s_load` through a 64-bit pointer hold the keys the key's known bits allow, at most 65536; a key past them, or an entry at a wrapping, unaligned or unmapped address, faults when selected. - An [image table](../../../core/shader/recompiler/Optimization/src/ResourceTracker.cpp) T# or S# read through a pointer that a scalar-buffer record holds may have that pointer's low word merged by a Phi with constants, with lane masks (words of a `Ballot`, the waterfall loop's lane bit, and ANDs, ORs and NOTs of them), with the record word ANDed with a lane mask, and with reads of the same record word under another key, and its high word merged the same way from the record's next word. The two words are walked together, edge by edge, and an edge whose low word is a constant or a lane mask is skipped along with its high word. The snapshot then captures the pointer of every record, at most 65536, and the GPU looks up the actual 64-bit pointer; one that was not captured, such as a lane mask, faults when selected. Other origins of the two words remain refused, among them a high word read from a second buffer on an edge whose low word is the record word. +- A pointer-keyed [image table](../../../core/shader/recompiler/Optimization/src/ResourceTracker.cpp) chase that reaches itself through a Phi (a loop), or passes more than 4 chase sites, stays refused. A chase arm is two words a scalar buffer load reads at a fixed offset through a V# that `s_load` reads at a fixed offset of the record pointer or of another chase arm; on an edge whose low word is a chase arm, the high word must be that arm's own read. The [snapshot](../../../core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp) captures, for every record pointer, the pointers the chases reach up to the shader's depth, each with its own T# or S#. A V# that is not a buffer, or a read past the V# or outside the snapshot, ends that chase, and the GPU faults when it selects the pointer reached there. - An [image table](../../../core/shader/recompiler/Optimization/src/ResourceTracker.cpp) whose key is neither a V#-relative scalar read nor a value the SRT walker evaluates (for example a shifted or masked runtime-offset V# read, a per-lane value made uniform by `v_readfirstlane`, or a loop-carried phi) binds every record of the snapshot domain instead of the selected ones. So does a table read at `(key << n) & mask` with such a key: a key domain would select records by the unmasked key, so that key is not narrowed. A key the walker evaluates selects its one record instead, unless the shader may write the memory the key is read from at run time (the same buffer, or any memory when the read or a write goes through a pointer or a GPU-only descriptor); then the column binds the whole domain too. A write to the key's memory through another V# over the same bytes is not detected. A shader whose selected records would pass 64 images or 64 sampled image/sampler pairs binds, for the heaps with the most such reads, the union of their keys in one table instead. - [Image table pointer indexing](../../../core/shader/recompiler/Optimization/src/ResourceTracker.cpp) propagates 32-bit value bounds through `ReadFirstLane` and `SelectU32` so waterfall key masks keep their bit domain. The SRT walker validates runtime select conditions whose active lane mask implies the condition through `LogicalAnd` conjunctions, assuming unselected inactive lanes need not be uniform across the full wave. The same assumption lets the two `s_load`s of one T# record match when one offset is added before `v_readfirstlane` and the other after (`ReadFirstLane(Select(mask, (key << 5) + 16, old), mask)` against `ReadFirstLane(key, mask) << 5`): with no active lane the hardware reads lane 0 of the inactive arm, and the table still binds the whole record. - An [image table](../../../core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp) T# of an MSAA or FMASK image, or of a color format under a depth compare, faults when selected: tables have no multisampled descriptors and no emulated color compare. From 2c1a65045c55be38c1d96fe99d4a679adb2e35d4 Mon Sep 17 00:00:00 2001 From: Yunus Shaikh <75296019+EagleBearerYS@users.noreply.github.com> Date: Thu, 22 Oct 2026 10:48:20 +0000 Subject: [PATCH 05/24] fix(shader): keep one chase site per V# word pair PointerRecordPairs found an existing chase site by its V# alone. A second chase arm that reads its next pointer through the same V# at other words therefore kept the first arm's PointerChase, and the snapshot never followed the second arm: a T# behind a node whose V# holds two links (words 0 and 1, words 2 and 3), each taken in its own lanes, faulted as outside the snapshot when the second link was taken. Sites are now keyed by the V# and the whole PointerChase, so each word pair of one V# is its own chase in the column and in the snapshot's closure. This follows the chase work for Ratchet & Clank: Rift Apart (PPSA01474); its trace has not shown this shape yet. --- core/libs/prx/libSceAgcDriver/CMakeLists.txt | 5 +- .../tests/execution/ImageTable.cpp | 101 ++++++++++++++++++ .../Optimization/src/ResourceTracker.cpp | 2 +- 3 files changed, 105 insertions(+), 3 deletions(-) diff --git a/core/libs/prx/libSceAgcDriver/CMakeLists.txt b/core/libs/prx/libSceAgcDriver/CMakeLists.txt index 2af4527c9624..9ee845bbcb89 100644 --- a/core/libs/prx/libSceAgcDriver/CMakeLists.txt +++ b/core/libs/prx/libSceAgcDriver/CMakeLists.txt @@ -2452,11 +2452,12 @@ if(BUILD_TESTING) add_test(NAME agc_driver_pointer_vsharp_select_table COMMAND agc_driver_image_table_tests --pointer-vsharp-select) add_test(NAME agc_driver_pointer_hop_table COMMAND agc_driver_image_table_tests --pointer-hop) add_test(NAME agc_driver_pointer_chase_table COMMAND agc_driver_image_table_tests --pointer-chase) + add_test(NAME agc_driver_pointer_chase_words_table COMMAND agc_driver_image_table_tests --pointer-chase-words) add_test(NAME agc_driver_pointer_vsharp_fixed_table COMMAND agc_driver_image_table_tests --pointer-vsharp-fixed) add_test(NAME agc_driver_pointer_vsharp_fixed_offset_table COMMAND agc_driver_image_table_tests --pointer-vsharp-fixed-offset) - set_tests_properties(agc_driver_nested_image_table agc_driver_nested_image_table_large agc_driver_nested_image_table_budget agc_driver_nested_sampler_table agc_driver_pointer_phi_sampler_table agc_driver_pointer_phi_ballot_table agc_driver_pointer_phi_copies_table agc_driver_pointer_phi_lane_and_table agc_driver_pointer_phi_high_table agc_driver_pointer_phi_high_second_table agc_driver_pointer_phi_high_second_lane_table agc_driver_pointer_phi_lane_not_table agc_driver_pointer_vsharp_select_table agc_driver_pointer_hop_table agc_driver_pointer_chase_table agc_driver_pointer_vsharp_fixed_table agc_driver_pointer_vsharp_fixed_offset_table PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_NO_PIPELINE_JOURNAL=1" SKIP_RETURN_CODE 77 TIMEOUT 30 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") + set_tests_properties(agc_driver_nested_image_table agc_driver_nested_image_table_large agc_driver_nested_image_table_budget agc_driver_nested_sampler_table agc_driver_pointer_phi_sampler_table agc_driver_pointer_phi_ballot_table agc_driver_pointer_phi_copies_table agc_driver_pointer_phi_lane_and_table agc_driver_pointer_phi_high_table agc_driver_pointer_phi_high_second_table agc_driver_pointer_phi_high_second_lane_table agc_driver_pointer_phi_lane_not_table agc_driver_pointer_vsharp_select_table agc_driver_pointer_hop_table agc_driver_pointer_chase_table agc_driver_pointer_chase_words_table agc_driver_pointer_vsharp_fixed_table agc_driver_pointer_vsharp_fixed_offset_table PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_NO_PIPELINE_JOURNAL=1" SKIP_RETURN_CODE 77 TIMEOUT 30 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") if(WIN32) - set_tests_properties(agc_driver_nested_image_table agc_driver_nested_image_table_large agc_driver_nested_image_table_budget agc_driver_nested_sampler_table agc_driver_pointer_phi_sampler_table agc_driver_pointer_phi_ballot_table agc_driver_pointer_phi_copies_table agc_driver_pointer_phi_lane_and_table agc_driver_pointer_phi_high_table agc_driver_pointer_phi_high_second_table agc_driver_pointer_phi_high_second_lane_table agc_driver_pointer_phi_lane_not_table agc_driver_pointer_vsharp_select_table agc_driver_pointer_hop_table agc_driver_pointer_chase_table agc_driver_pointer_vsharp_fixed_table agc_driver_pointer_vsharp_fixed_offset_table PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") + set_tests_properties(agc_driver_nested_image_table agc_driver_nested_image_table_large agc_driver_nested_image_table_budget agc_driver_nested_sampler_table agc_driver_pointer_phi_sampler_table agc_driver_pointer_phi_ballot_table agc_driver_pointer_phi_copies_table agc_driver_pointer_phi_lane_and_table agc_driver_pointer_phi_high_table agc_driver_pointer_phi_high_second_table agc_driver_pointer_phi_high_second_lane_table agc_driver_pointer_phi_lane_not_table agc_driver_pointer_vsharp_select_table agc_driver_pointer_hop_table agc_driver_pointer_chase_table agc_driver_pointer_chase_words_table agc_driver_pointer_vsharp_fixed_table agc_driver_pointer_vsharp_fixed_offset_table PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() add_test(NAME agc_pipeline_journal COMMAND agc_driver_graphics_tests --pipeline-journal) add_test(NAME agc_pipeline_journal_long_paths COMMAND agc_driver_graphics_tests --pipeline-journal-long-paths) diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp index cb0ac2e8629e..6bf0187ef6df 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp @@ -458,6 +458,16 @@ alignas(256) constexpr std::array PointerChaseDeepCode{ 0xf40c0916u, 0xfa000000u, 0xbf8cc07fu, 0xf09c0f08u, 0x01090401u, 0x8afe3032u, 0xbf89ffc9u, 0xbefe042eu, 0x7e120202u, 0x34101286u, 0x4a101100u, 0x34101084u, 0xe0781000u, 0x80070408u, 0xbf810000u, }; +alignas(256) constexpr std::array PointerChaseWordsCode{ + 0xf4080100u, 0xfa000000u, 0xf4080200u, 0xfa000010u, 0xf4080700u, 0xfa000020u, 0xf4080800u, 0xfa000030u, + 0x8f108402u, 0xf4280404u, 0x20000000u, 0x360200bfu, 0x7e020d01u, 0x060202f0u, 0x100202ffu, 0x3c800000u, + 0x060202f2u, 0x7e0402ffu, 0x3e000000u, 0xbf8cc07fu, 0x7d8800a0u, 0x7e140210u, 0x02061411u, 0xbeae047eu, + 0x7e280503u, 0xd4c20030u, 0x00020614u, 0xbeb22430u, 0x8f158414u, 0xf4240b02u, 0x2a000008u, 0xbf8cc07fu, + 0xf4080f16u, 0xfa000060u, 0xbf8cc07fu, 0xbf0d8012u, 0xbf840004u, 0xf4240b1eu, 0xfa000000u, 0xbf8cc07fu, + 0xbf820005u, 0xbf0d8112u, 0xbf840003u, 0xf4240b1eu, 0xfa000008u, 0xbf8cc07fu, 0xf40c0916u, 0xfa000000u, + 0xbf8cc07fu, 0xf09c0f08u, 0x01090401u, 0x8afe3032u, 0xbf89ffe3u, 0xbefe042eu, 0x7e120202u, 0x34101286u, + 0x4a101100u, 0x34101084u, 0xe0781000u, 0x80070408u, 0xbf810000u, +}; alignas(256) constexpr std::array PointerFixedVsharpCode{ 0xf4080100u, 0xfa000000u, 0xf4080200u, 0xfa000010u, 0xf4080700u, 0xfa000020u, 0xf4080800u, 0xfa000030u, 0x8f108402u, 0xf4280404u, 0x20000000u, 0x360200bfu, 0x7e020d01u, 0x060202f0u, 0x100202ffu, 0x3c800000u, @@ -2434,6 +2444,93 @@ void RunPointerChaseTests(AgcDriver::VulkanDevice& device) { std::puts("pointer table with a conditional V# chase: a T# at a record pointer chased up to twice through a V# at the pointer plus 0x60 checked for every taken and skipped step, a moved link and a link outside the snapshot, and a chase in a loop and five chase steps refused"); } +void RunPointerChaseWordsTests(AgcDriver::VulkanDevice& device) { + Fill(); + alignas(256) std::array, 6> nodes{}; + alignas(256) std::array, 2> links{}; + Table = {}; + Keys = {}; + const std::array images{Image(FloatTexels.data(), Format32Float), Image(OtherFloatTexels.data(), Format32Float)}; + const std::array imageOf{0u, 1u, 0u, 1u, 1u, 0u}; + std::array first{}; + std::array second{}; + const auto address = [](const void* data) { return static_cast(reinterpret_cast(data)); }; + const auto link = [&](std::uint32_t from, std::uint32_t word, std::uint32_t to) { + links[from][word] = static_cast(address(nodes[to].data())); + links[from][word + 1u] = static_cast(address(nodes[to].data()) >> 32u); + (word == 0u ? first : second)[from] = to; + }; + for (std::uint32_t node = 0; node < 6u; ++node) std::copy(images[imageOf[node]].begin(), images[imageOf[node]].end(), nodes[node].begin()); + for (std::uint32_t node = 0; node < 2u; ++node) { + const auto descriptor = Buffer(links[node].data(), 16u); + std::copy(descriptor.begin(), descriptor.end(), nodes[node].begin() + 0x60u / 4u); + link(node, 0u, node + 2u); + link(node, 2u, node + 4u); + } + const std::array recordNode{0u, 0u, 1u, 0u}; + for (std::uint32_t record = 0; record < 4u; ++record) { + Table[record * 4u + 2u] = static_cast(address(nodes[recordNode[record]].data())); + Table[record * 4u + 3u] = static_cast(address(nodes[recordNode[record]].data()) >> 32u); + } + const std::string name = "pointer table with two chase links in one V#"; + struct Registration { + void* data; + Registration(void* data, std::size_t bytes) : data(data) { GuestAllocations::Mutation().Add(data, bytes, true, true, true); } + ~Registration() { GuestAllocations::Mutation().Remove(data); } + }; + const Registration registeredNodes(nodes.data(), sizeof(nodes)); + const Registration registeredLinks(links.data(), sizeof(links)); + const auto run = [&](std::span code) { + const auto table = Buffer(Table.data(), 64u); + const auto keys = Buffer(Keys.data(), static_cast(sizeof(Keys))); + const auto output = Buffer(Output.data(), static_cast(sizeof(Output))); + Srt = {}; + std::copy(table.begin(), table.end(), Srt.begin()); + std::copy(keys.begin(), keys.end(), Srt.begin() + 4u); + std::copy(output.begin(), output.end(), Srt.begin() + 8u); + std::copy(PointWrap.begin(), PointWrap.end(), Srt.begin() + 12u); + const std::vector memory{ + {reinterpret_cast(code.data()), std::as_bytes(code)}, + {reinterpret_cast(Srt.data()), std::as_bytes(std::span(Srt))}, + {reinterpret_cast(Table.data()), std::as_bytes(std::span(Table))}, + {reinterpret_cast(Keys.data()), std::as_bytes(std::span(Keys))}, + {reinterpret_cast(nodes.data()), std::as_bytes(std::span(nodes))}, + {reinterpret_cast(links.data()), std::as_bytes(std::span(links))}, + }; + const auto inputTable = Table; + const auto inputNodes = nodes; + const auto inputLinks = links; + auto result = Execute(device, code, Srt.data(), memory, 4u, Lanes, 64u); + Require(Table == inputTable && nodes == inputNodes && links == inputLinks, name + ": dispatch modified its input memory"); + Require(std::all_of(Output.begin() + 4u * Lanes * 4u, Output.end(), [](auto word) { return word == 0xdeadbeefu; }), name + ": dispatch wrote past its output"); + return result; + }; + const std::array, 4> keys{{{1u, 2u}, {2u, 1u}, {1u, 1u}, {2u, 2u}}}; + const std::array steps{1u, 2u, 0u, 2u}; + for (std::uint32_t group = 0; group < 4u; ++group) { + Keys[group * 4u] = keys[group][0]; + Keys[group * 4u + 1u] = keys[group][1]; + Keys[group * 4u + 2u] = steps[group]; + } + const auto sampled = [&](std::uint32_t group) { + return std::function([&, group](std::uint32_t x) { + auto node = recordNode[keys[group][x < 32u ? 0u : 1u]]; + if ((steps[group] & 1u) != 0u) node = first[node]; + else if ((steps[group] & 2u) != 0u) node = second[node]; + return std::bit_cast(imageOf[node] != 0u ? OtherFloatTexels[x] : FloatTexels[x]); + }); + }; + const auto taken = run(PointerChaseWordsCode); + Require(!Faulted(taken), name + ": the T# behind one of the two links faulted:\n" + taken.log); + for (std::uint32_t group = 0; group < 4u; ++group) RequireRed(taken, group, Lanes, sampled(group), name + ": the T# at the pointer read from the link taken in group " + std::to_string(group)); + link(0u, 2u, 2u); + const auto moved = run(PointerChaseWordsCode); + Require(!Faulted(moved), name + ": the T# behind a moved second link faulted:\n" + moved.log); + Require(moved.artifactId == taken.artifactId, name + ": moving the second link recompiled the shader"); + for (std::uint32_t group = 0; group < 4u; ++group) RequireRed(moved, group, Lanes, sampled(group), name + ": the T# after node 0's second link moved to node 2 in group " + std::to_string(group)); + std::puts("pointer table with two chase links in one V#: a T# at a record pointer chased through words 0 and 1 or words 2 and 3 of one V# at the pointer plus 0x60, each taken in its own groups, checked with a moved second link"); +} + void RunPointerFixedVsharpTests(AgcDriver::VulkanDevice& device, std::uint32_t descriptorOffset) { Fill(); alignas(256) std::array, 2> records{}; @@ -2785,6 +2882,10 @@ int main(int argc, char** argv) { RunPointerChaseTests(*device); return 0; } + if (argc == 2 && std::strcmp(argv[1], "--pointer-chase-words") == 0) { + RunPointerChaseWordsTests(*device); + return 0; + } if (argc == 2 && std::strcmp(argv[1], "--pointer-vsharp-fixed") == 0) { RunPointerFixedVsharpTests(*device, 0u); return 0; diff --git a/core/shader/recompiler/Optimization/src/ResourceTracker.cpp b/core/shader/recompiler/Optimization/src/ResourceTracker.cpp index 877753529263..e0533136152d 100644 --- a/core/shader/recompiler/Optimization/src/ResourceTracker.cpp +++ b/core/shader/recompiler/Optimization/src/ResourceTracker.cpp @@ -945,7 +945,7 @@ class Tracker { std::vector sites; std::vector looseHandles; const auto site = [&](IrValue& handle, IrValue& pointer, const PointerChase& chase) { - const auto found = std::ranges::find_if(sites, [&](const ChaseSite& known) { return known.handle == &handle || EquivalentValue(m_program.Resources(), known.handle, &handle); }); + const auto found = std::ranges::find_if(sites, [&](const ChaseSite& known) { return known.chase == chase && (known.handle == &handle || EquivalentValue(m_program.Resources(), known.handle, &handle)); }); if (found != sites.end()) return static_cast(found - sites.begin()); sites.push_back(ChaseSite{&handle, &pointer, chase, {}}); return sites.size() - 1u; From 7ab0765c3779364355682708d970548d9a13a08f Mon Sep 17 00:00:00 2001 From: Yunus Shaikh <75296019+EagleBearerYS@users.noreply.github.com> Date: Thu, 22 Oct 2026 10:46:23 +0000 Subject: [PATCH 06/24] fix(shader): read a chased pointer's high word at its own offset A pointer chase arm whose next pointer is rebuilt from two words that are not adjacent stayed unplanned: ChaseHighRead took the high word only at the low word's immediate plus 4, so the walk refused the arm. Ratchet & Clank: Rift Apart (PPSA01474) stops this way at compute LoadAddressU32@pc=0x13bc0 with "GetImageResource dword 0 is not a valid runtime value": after two chase steps through a V# at the pointer plus 0x60, its third step loads a V# at the pointer plus 0x110, reads 16 dwords from 0x40 with one s_buffer_load_dwordx16 and rebuilds the next pointer from dword 5 (0x54) and dword 11 (0x6c). PointerChase now keeps the high word's own immediate, readHighImmediate. The walk takes a high read through the low read's V# (or an equivalent one) at the same buffer offset when its immediate is 4-aligned and differs from the low word's, and the snapshot reads the next pointer's high word at that immediate. The shader disk cache stores the field and refuses a plan whose high immediate is unaligned or equals the low one (format 48). Suggested by Tamalero's trace on #9633 (issuecomment-6338256607), which also proposed the readHighImmediate field. --- core/libs/prx/libSceAgcDriver/CMakeLists.txt | 5 +- .../tests/execution/ImageTable.cpp | 174 ++++++++++++++++++ .../IrMetadata/ControlFlowInfo.hpp | 1 + .../Optimization/src/ResourceMaterializer.cpp | 2 +- .../Optimization/src/ResourceTracker.cpp | 10 +- core/shader/recompiler/ShaderDiskCache.cpp | 3 + core/shader/recompiler/ShaderDiskCache.hpp | 2 +- docs/dev/debt/shader.md | 2 +- 8 files changed, 191 insertions(+), 8 deletions(-) diff --git a/core/libs/prx/libSceAgcDriver/CMakeLists.txt b/core/libs/prx/libSceAgcDriver/CMakeLists.txt index 9ee845bbcb89..fdfa720b13d7 100644 --- a/core/libs/prx/libSceAgcDriver/CMakeLists.txt +++ b/core/libs/prx/libSceAgcDriver/CMakeLists.txt @@ -2452,12 +2452,13 @@ if(BUILD_TESTING) add_test(NAME agc_driver_pointer_vsharp_select_table COMMAND agc_driver_image_table_tests --pointer-vsharp-select) add_test(NAME agc_driver_pointer_hop_table COMMAND agc_driver_image_table_tests --pointer-hop) add_test(NAME agc_driver_pointer_chase_table COMMAND agc_driver_image_table_tests --pointer-chase) + add_test(NAME agc_driver_pointer_chase_split_table COMMAND agc_driver_image_table_tests --pointer-chase-split) add_test(NAME agc_driver_pointer_chase_words_table COMMAND agc_driver_image_table_tests --pointer-chase-words) add_test(NAME agc_driver_pointer_vsharp_fixed_table COMMAND agc_driver_image_table_tests --pointer-vsharp-fixed) add_test(NAME agc_driver_pointer_vsharp_fixed_offset_table COMMAND agc_driver_image_table_tests --pointer-vsharp-fixed-offset) - set_tests_properties(agc_driver_nested_image_table agc_driver_nested_image_table_large agc_driver_nested_image_table_budget agc_driver_nested_sampler_table agc_driver_pointer_phi_sampler_table agc_driver_pointer_phi_ballot_table agc_driver_pointer_phi_copies_table agc_driver_pointer_phi_lane_and_table agc_driver_pointer_phi_high_table agc_driver_pointer_phi_high_second_table agc_driver_pointer_phi_high_second_lane_table agc_driver_pointer_phi_lane_not_table agc_driver_pointer_vsharp_select_table agc_driver_pointer_hop_table agc_driver_pointer_chase_table agc_driver_pointer_chase_words_table agc_driver_pointer_vsharp_fixed_table agc_driver_pointer_vsharp_fixed_offset_table PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_NO_PIPELINE_JOURNAL=1" SKIP_RETURN_CODE 77 TIMEOUT 30 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") + set_tests_properties(agc_driver_nested_image_table agc_driver_nested_image_table_large agc_driver_nested_image_table_budget agc_driver_nested_sampler_table agc_driver_pointer_phi_sampler_table agc_driver_pointer_phi_ballot_table agc_driver_pointer_phi_copies_table agc_driver_pointer_phi_lane_and_table agc_driver_pointer_phi_high_table agc_driver_pointer_phi_high_second_table agc_driver_pointer_phi_high_second_lane_table agc_driver_pointer_phi_lane_not_table agc_driver_pointer_vsharp_select_table agc_driver_pointer_hop_table agc_driver_pointer_chase_table agc_driver_pointer_chase_split_table agc_driver_pointer_chase_words_table agc_driver_pointer_vsharp_fixed_table agc_driver_pointer_vsharp_fixed_offset_table PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_NO_PIPELINE_JOURNAL=1" SKIP_RETURN_CODE 77 TIMEOUT 30 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") if(WIN32) - set_tests_properties(agc_driver_nested_image_table agc_driver_nested_image_table_large agc_driver_nested_image_table_budget agc_driver_nested_sampler_table agc_driver_pointer_phi_sampler_table agc_driver_pointer_phi_ballot_table agc_driver_pointer_phi_copies_table agc_driver_pointer_phi_lane_and_table agc_driver_pointer_phi_high_table agc_driver_pointer_phi_high_second_table agc_driver_pointer_phi_high_second_lane_table agc_driver_pointer_phi_lane_not_table agc_driver_pointer_vsharp_select_table agc_driver_pointer_hop_table agc_driver_pointer_chase_table agc_driver_pointer_chase_words_table agc_driver_pointer_vsharp_fixed_table agc_driver_pointer_vsharp_fixed_offset_table PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") + set_tests_properties(agc_driver_nested_image_table agc_driver_nested_image_table_large agc_driver_nested_image_table_budget agc_driver_nested_sampler_table agc_driver_pointer_phi_sampler_table agc_driver_pointer_phi_ballot_table agc_driver_pointer_phi_copies_table agc_driver_pointer_phi_lane_and_table agc_driver_pointer_phi_high_table agc_driver_pointer_phi_high_second_table agc_driver_pointer_phi_high_second_lane_table agc_driver_pointer_phi_lane_not_table agc_driver_pointer_vsharp_select_table agc_driver_pointer_hop_table agc_driver_pointer_chase_table agc_driver_pointer_chase_split_table agc_driver_pointer_chase_words_table agc_driver_pointer_vsharp_fixed_table agc_driver_pointer_vsharp_fixed_offset_table PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() add_test(NAME agc_pipeline_journal COMMAND agc_driver_graphics_tests --pipeline-journal) add_test(NAME agc_pipeline_journal_long_paths COMMAND agc_driver_graphics_tests --pipeline-journal-long-paths) diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp index 6bf0187ef6df..0f2da8f0933f 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp @@ -458,6 +458,54 @@ alignas(256) constexpr std::array PointerChaseDeepCode{ 0xf40c0916u, 0xfa000000u, 0xbf8cc07fu, 0xf09c0f08u, 0x01090401u, 0x8afe3032u, 0xbf89ffc9u, 0xbefe042eu, 0x7e120202u, 0x34101286u, 0x4a101100u, 0x34101084u, 0xe0781000u, 0x80070408u, 0xbf810000u, }; +alignas(256) constexpr std::array PointerChaseSplitCode{ + 0xf4080100u, 0xfa000000u, 0xf4080200u, 0xfa000010u, 0xf4080700u, 0xfa000020u, 0xf4080800u, 0xfa000030u, + 0x8f108402u, 0xf4280404u, 0x20000000u, 0x360200bfu, 0x7e020d01u, 0x060202f0u, 0x100202ffu, 0x3c800000u, + 0x060202f2u, 0x7e0402ffu, 0x3e000000u, 0xbf8cc07fu, 0x7d8800a0u, 0x7e140210u, 0x02061411u, 0xbeae047eu, + 0x7e280503u, 0xd4c20030u, 0x00020614u, 0xbeb22430u, 0x8f158414u, 0xf4240b02u, 0x2a000008u, 0xbf8cc07fu, + 0xbf0d8012u, 0xbf840006u, 0xf4080f16u, 0xfa000060u, 0xbf8cc07fu, 0xf4240b1eu, 0xfa000000u, 0xbf8cc07fu, + 0xbf0d8112u, 0xbf840006u, 0xf4081016u, 0xfa000060u, 0xbf8cc07fu, 0xf4240b20u, 0xfa000000u, 0xbf8cc07fu, + 0xbf0d8212u, 0xbf840008u, 0xf4081116u, 0xfa000110u, 0xbf8cc07fu, 0xf4301222u, 0xfa000040u, 0xbf8cc07fu, + 0xbeac034du, 0xbead0353u, 0xf40c0916u, 0xfa000000u, 0xbf8cc07fu, 0xf09c0f08u, 0x01090401u, 0x8afe3032u, + 0xbf89ffd7u, 0xbefe042eu, 0x7e120202u, 0x34101286u, 0x4a101100u, 0x34101084u, 0xe0781000u, 0x80070408u, + 0xbf810000u, +}; +alignas(256) constexpr std::array PointerChaseSplitHandleCode{ + 0xf4080100u, 0xfa000000u, 0xf4080200u, 0xfa000010u, 0xf4080700u, 0xfa000020u, 0xf4080800u, 0xfa000030u, + 0x8f108402u, 0xf4280404u, 0x20000000u, 0x360200bfu, 0x7e020d01u, 0x060202f0u, 0x100202ffu, 0x3c800000u, + 0x060202f2u, 0x7e0402ffu, 0x3e000000u, 0xbf8cc07fu, 0x7d8800a0u, 0x7e140210u, 0x02061411u, 0xbeae047eu, + 0x7e280503u, 0xd4c20030u, 0x00020614u, 0xbeb22430u, 0x8f158414u, 0xf4240b02u, 0x2a000008u, 0xbf8cc07fu, + 0xbf0d8012u, 0xbf840006u, 0xf4080f16u, 0xfa000060u, 0xbf8cc07fu, 0xf4240b1eu, 0xfa000000u, 0xbf8cc07fu, + 0xbf0d8112u, 0xbf840006u, 0xf4081016u, 0xfa000060u, 0xbf8cc07fu, 0xf4240b20u, 0xfa000000u, 0xbf8cc07fu, + 0xbf0d8212u, 0xbf84000cu, 0xf4081116u, 0xfa000110u, 0xf4081616u, 0xfa000120u, 0xbf8cc07fu, 0xf4301222u, + 0xfa000040u, 0xf420172cu, 0xfa00006cu, 0xbf8cc07fu, 0xbeac034du, 0xbead035cu, 0xf40c0916u, 0xfa000000u, + 0xbf8cc07fu, 0xf09c0f08u, 0x01090401u, 0x8afe3032u, 0xbf89ffd3u, 0xbefe042eu, 0x7e120202u, 0x34101286u, + 0x4a101100u, 0x34101084u, 0xe0781000u, 0x80070408u, 0xbf810000u, +}; +alignas(256) constexpr std::array PointerChaseSplitOffsetCode{ + 0xf4080100u, 0xfa000000u, 0xf4080200u, 0xfa000010u, 0xf4080700u, 0xfa000020u, 0xf4080800u, 0xfa000030u, + 0x8f108402u, 0xf4280404u, 0x20000000u, 0x360200bfu, 0x7e020d01u, 0x060202f0u, 0x100202ffu, 0x3c800000u, + 0x060202f2u, 0x7e0402ffu, 0x3e000000u, 0xbf8cc07fu, 0x7d8800a0u, 0x7e140210u, 0x02061411u, 0xbeae047eu, + 0x7e280503u, 0xd4c20030u, 0x00020614u, 0xbeb22430u, 0x8f158414u, 0xf4240b02u, 0x2a000008u, 0xbf8cc07fu, + 0xbf0d8012u, 0xbf840006u, 0xf4080f16u, 0xfa000060u, 0xbf8cc07fu, 0xf4240b1eu, 0xfa000000u, 0xbf8cc07fu, + 0xbf0d8112u, 0xbf840006u, 0xf4081016u, 0xfa000060u, 0xbf8cc07fu, 0xf4240b20u, 0xfa000000u, 0xbf8cc07fu, + 0xbf0d8212u, 0xbf84000bu, 0xf4081116u, 0xfa000110u, 0xbf8cc07fu, 0xb05d0010u, 0xf4301222u, 0xfa000040u, + 0xf4201722u, 0xba00005cu, 0xbf8cc07fu, 0xbeac034du, 0xbead035cu, 0xf40c0916u, 0xfa000000u, 0xbf8cc07fu, + 0xf09c0f08u, 0x01090401u, 0x8afe3032u, 0xbf89ffd4u, 0xbefe042eu, 0x7e120202u, 0x34101286u, 0x4a101100u, + 0x34101084u, 0xe0781000u, 0x80070408u, 0xbf810000u, +}; +alignas(256) constexpr std::array PointerChaseSplitWordCode{ + 0xf4080100u, 0xfa000000u, 0xf4080200u, 0xfa000010u, 0xf4080700u, 0xfa000020u, 0xf4080800u, 0xfa000030u, + 0x8f108402u, 0xf4280404u, 0x20000000u, 0x360200bfu, 0x7e020d01u, 0x060202f0u, 0x100202ffu, 0x3c800000u, + 0x060202f2u, 0x7e0402ffu, 0x3e000000u, 0xbf8cc07fu, 0x7d8800a0u, 0x7e140210u, 0x02061411u, 0xbeae047eu, + 0x7e280503u, 0xd4c20030u, 0x00020614u, 0xbeb22430u, 0x8f158414u, 0xf4240b02u, 0x2a000008u, 0xbf8cc07fu, + 0xbf0d8012u, 0xbf840006u, 0xf4080f16u, 0xfa000060u, 0xbf8cc07fu, 0xf4240b1eu, 0xfa000000u, 0xbf8cc07fu, + 0xbf0d8112u, 0xbf840006u, 0xf4081016u, 0xfa000060u, 0xbf8cc07fu, 0xf4240b20u, 0xfa000000u, 0xbf8cc07fu, + 0xbf0d8212u, 0xbf840008u, 0xf4081116u, 0xfa000110u, 0xbf8cc07fu, 0xf4301222u, 0xfa000040u, 0xbf8cc07fu, + 0xbeac034du, 0xbead034du, 0xf40c0916u, 0xfa000000u, 0xbf8cc07fu, 0xf09c0f08u, 0x01090401u, 0x8afe3032u, + 0xbf89ffd7u, 0xbefe042eu, 0x7e120202u, 0x34101286u, 0x4a101100u, 0x34101084u, 0xe0781000u, 0x80070408u, + 0xbf810000u, +}; alignas(256) constexpr std::array PointerChaseWordsCode{ 0xf4080100u, 0xfa000000u, 0xf4080200u, 0xfa000010u, 0xf4080700u, 0xfa000020u, 0xf4080800u, 0xfa000030u, 0x8f108402u, 0xf4280404u, 0x20000000u, 0x360200bfu, 0x7e020d01u, 0x060202f0u, 0x100202ffu, 0x3c800000u, @@ -2444,6 +2492,128 @@ void RunPointerChaseTests(AgcDriver::VulkanDevice& device) { std::puts("pointer table with a conditional V# chase: a T# at a record pointer chased up to twice through a V# at the pointer plus 0x60 checked for every taken and skipped step, a moved link and a link outside the snapshot, and a chase in a loop and five chase steps refused"); } +void RunPointerChaseSplitTests(AgcDriver::VulkanDevice& device) { + Fill(); + alignas(256) std::array, 8> nodes{}; + alignas(256) std::array, 8> nearLinks{}; + alignas(256) std::array, 8> farLinks{}; + alignas(256) std::array unsnapshotted{}; + Table = {}; + Keys = {}; + const std::array images{Image(FloatTexels.data(), Format32Float), Image(OtherFloatTexels.data(), Format32Float)}; + const std::array imageOf{0u, 1u, 0u, 1u, 1u, 0u, 1u, 0u}; + std::array nearNext{}; + std::array farNext{}; + const auto address = [](const void* data) { return static_cast(reinterpret_cast(data)); }; + const auto linkNear = [&](std::uint32_t from, std::uint32_t to, std::uint32_t* link) { + link[0] = static_cast(address(nodes[to].data())); + link[1] = static_cast(address(nodes[to].data()) >> 32u); + const auto descriptor = Buffer(link, 8u); + std::copy(descriptor.begin(), descriptor.end(), nodes[from].begin() + 0x60u / 4u); + nearNext[from] = to; + }; + const auto linkFar = [&](std::uint32_t from, std::uint32_t to, std::uint32_t* link) { + link[0x54u / 4u] = static_cast(address(nodes[to].data())); + link[0x6cu / 4u] = static_cast(address(nodes[to].data()) >> 32u); + const auto descriptor = Buffer(link, 0x80u); + std::copy(descriptor.begin(), descriptor.end(), nodes[from].begin() + 0x110u / 4u); + farNext[from] = to; + }; + const std::array nearTo{1u, 2u, 3u, 0u, 0u, 0u, 0u, 7u}; + const std::array farTo{4u, 5u, 6u, 0u, 0u, 0u, 0u, 7u}; + for (std::uint32_t node = 0; node < 8u; ++node) { + std::copy(images[imageOf[node]].begin(), images[imageOf[node]].end(), nodes[node].begin()); + linkNear(node, nearTo[node], nearLinks[node].data()); + linkFar(node, farTo[node], farLinks[node].data()); + } + const std::array recordNode{0u, 0u, 3u, 0u}; + for (std::uint32_t record = 0; record < 4u; ++record) { + Table[record * 4u + 2u] = static_cast(address(nodes[recordNode[record]].data())); + Table[record * 4u + 3u] = static_cast(address(nodes[recordNode[record]].data()) >> 32u); + } + const std::string name = "pointer table with a split-word V# chase"; + struct Registration { + void* data; + Registration(void* data, std::size_t bytes) : data(data) { GuestAllocations::Mutation().Add(data, bytes, true, true, true); } + ~Registration() { GuestAllocations::Mutation().Remove(data); } + }; + const Registration registeredNodes(nodes.data(), sizeof(nodes)); + const Registration registeredNearLinks(nearLinks.data(), sizeof(nearLinks)); + const Registration registeredFarLinks(farLinks.data(), sizeof(farLinks)); + const Registration registeredUnsnapshotted(unsnapshotted.data(), sizeof(unsnapshotted)); + const auto run = [&](std::span code) { + const auto table = Buffer(Table.data(), 64u); + const auto keys = Buffer(Keys.data(), static_cast(sizeof(Keys))); + const auto output = Buffer(Output.data(), static_cast(sizeof(Output))); + Srt = {}; + std::copy(table.begin(), table.end(), Srt.begin()); + std::copy(keys.begin(), keys.end(), Srt.begin() + 4u); + std::copy(output.begin(), output.end(), Srt.begin() + 8u); + std::copy(PointWrap.begin(), PointWrap.end(), Srt.begin() + 12u); + const std::vector memory{ + {reinterpret_cast(code.data()), std::as_bytes(code)}, + {reinterpret_cast(Srt.data()), std::as_bytes(std::span(Srt))}, + {reinterpret_cast(Table.data()), std::as_bytes(std::span(Table))}, + {reinterpret_cast(Keys.data()), std::as_bytes(std::span(Keys))}, + {reinterpret_cast(nodes.data()), std::as_bytes(std::span(nodes))}, + {reinterpret_cast(nearLinks.data()), std::as_bytes(std::span(nearLinks))}, + {reinterpret_cast(farLinks.data()), std::as_bytes(std::span(farLinks))}, + }; + const auto inputTable = Table; + const auto inputNodes = nodes; + const auto inputNearLinks = nearLinks; + const auto inputFarLinks = farLinks; + auto result = Execute(device, code, Srt.data(), memory, 4u, Lanes, 64u); + Require(Table == inputTable && nodes == inputNodes && nearLinks == inputNearLinks && farLinks == inputFarLinks, name + ": dispatch modified its input memory"); + Require(std::all_of(Output.begin() + 4u * Lanes * 4u, Output.end(), [](auto word) { return word == 0xdeadbeefu; }), name + ": dispatch wrote past its output"); + return result; + }; + const std::array, 4> keys{{{1u, 2u}, {2u, 1u}, {1u, 1u}, {2u, 2u}}}; + const std::array steps{7u, 4u, 3u, 5u}; + for (std::uint32_t group = 0; group < 4u; ++group) { + Keys[group * 4u] = keys[group][0]; + Keys[group * 4u + 1u] = keys[group][1]; + Keys[group * 4u + 2u] = steps[group]; + } + const auto sampled = [&](std::uint32_t group) { + return std::function([&, group](std::uint32_t x) { + auto node = recordNode[keys[group][x < 32u ? 0u : 1u]]; + if ((steps[group] & 1u) != 0u) node = nearNext[node]; + if ((steps[group] & 2u) != 0u) node = nearNext[node]; + if ((steps[group] & 4u) != 0u) node = farNext[node]; + return std::bit_cast(imageOf[node] != 0u ? OtherFloatTexels[x] : FloatTexels[x]); + }); + }; + const auto untouched = [](const Outcome& outcome) { return std::all_of(outcome.words.begin(), outcome.words.end(), [](auto word) { return word == 0xdeadbeefu; }); }; + const auto refused = [&](std::span code, const std::string& what) { + std::string refusal; + try { + static_cast(run(code)); + } catch (const std::runtime_error& error) { + refusal = error.what(); + } + Require(refusal.find("not a valid runtime value") != std::string::npos, name + ": " + what + " was not refused by resource planning: " + refusal); + }; + refused(PointerChaseSplitHandleCode, "a high word read through another V#"); + refused(PointerChaseSplitOffsetCode, "a high word read at another buffer offset"); + refused(PointerChaseSplitWordCode, "a high word read at the low word"); + const auto taken = run(PointerChaseSplitCode); + Require(!Faulted(taken), name + ": the T# at the end of the chase faulted:\n" + taken.log); + for (std::uint32_t group = 0; group < 4u; ++group) RequireRed(taken, group, Lanes, sampled(group), name + ": the T# at the pointer reached by the chase steps taken in group " + std::to_string(group)); + linkFar(0u, 2u, farLinks[0].data()); + const auto moved = run(PointerChaseSplitCode); + Require(!Faulted(moved), name + ": the T# behind a moved split-word link faulted:\n" + moved.log); + Require(moved.artifactId == taken.artifactId, name + ": moving a split-word link recompiled the shader"); + for (std::uint32_t group = 0; group < 4u; ++group) RequireRed(moved, group, Lanes, sampled(group), name + ": the T# after node 0's split-word link moved to node 2 in group " + std::to_string(group)); + linkFar(0u, 4u, farLinks[0].data()); + linkFar(1u, 7u, unsnapshotted.data()); + const auto outside = run(PointerChaseSplitCode); + Require(Faulted(outside) && outside.log.find("outside the snapshot") != std::string::npos, name + ": a split-word link outside the snapshot did not fault as outside the snapshot:\n" + outside.log); + Require(untouched(outside), name + ": a split-word link outside the snapshot published copied output"); + Require(outside.artifactId == taken.artifactId, name + ": a split-word link outside the snapshot recompiled the shader"); + std::puts("pointer table with a split-word V# chase: a T# at a record pointer chased twice through a V# at the pointer plus 0x60 and once through a V# at the pointer plus 0x110 whose next pointer is dwords 5 and 11 of one 16-dword load from 0x40, checked for every taken and skipped step with a node only the third chase step reaches, a moved link and a link outside the snapshot, and a high word through another V#, at another buffer offset or at the low word refused"); +} + void RunPointerChaseWordsTests(AgcDriver::VulkanDevice& device) { Fill(); alignas(256) std::array, 6> nodes{}; @@ -2882,6 +3052,10 @@ int main(int argc, char** argv) { RunPointerChaseTests(*device); return 0; } + if (argc == 2 && std::strcmp(argv[1], "--pointer-chase-split") == 0) { + RunPointerChaseSplitTests(*device); + return 0; + } if (argc == 2 && std::strcmp(argv[1], "--pointer-chase-words") == 0) { RunPointerChaseWordsTests(*device); return 0; diff --git a/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ControlFlowInfo.hpp b/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ControlFlowInfo.hpp index 7e869d531b7a..01264a76f99b 100644 --- a/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ControlFlowInfo.hpp +++ b/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ControlFlowInfo.hpp @@ -42,6 +42,7 @@ struct PointerChase { std::uint32_t immediate = 0; std::uint32_t readOffset = 0; std::uint32_t readImmediate = 0; + std::uint32_t readHighImmediate = 0; bool operator==(const PointerChase& other) const = default; }; diff --git a/core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp b/core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp index f481f175c7ed..eb3ef3503958 100644 --- a/core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp +++ b/core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp @@ -963,7 +963,7 @@ class TableSnapshotter { const auto buffer = decodeBufferDescriptor(descriptor); if (buffer.Type() != 0u) return std::nullopt; const auto low = ImageTableAbi::ScalarBufferDword(chase.readOffset, chase.readImmediate, buffer.GetSize()); - const auto high = ImageTableAbi::ScalarBufferDword(chase.readOffset, chase.readImmediate + 4u, buffer.GetSize()); + const auto high = ImageTableAbi::ScalarBufferDword(chase.readOffset, chase.readHighImmediate, buffer.GetSize()); if (!low.has_value() || !high.has_value() || !AddressAccessible(buffer.Base48() + *low) || !AddressAccessible(buffer.Base48() + *high)) return std::nullopt; tables.ranges.emplace_back(buffer.Base48() + *low, sizeof(std::uint32_t)); tables.ranges.emplace_back(buffer.Base48() + *high, sizeof(std::uint32_t)); diff --git a/core/shader/recompiler/Optimization/src/ResourceTracker.cpp b/core/shader/recompiler/Optimization/src/ResourceTracker.cpp index e0533136152d..42f81d0e3106 100644 --- a/core/shader/recompiler/Optimization/src/ResourceTracker.cpp +++ b/core/shader/recompiler/Optimization/src/ResourceTracker.cpp @@ -934,11 +934,13 @@ class Tracker { return true; } - bool ChaseHighRead(IrValue& read, const IrValue& handle, std::uint32_t readOffset, std::uint32_t readImmediate) const { + std::optional ChaseHighRead(IrValue& read, const IrValue& handle, std::uint32_t readOffset, std::uint32_t readImmediate) const { IrValue* highHandle = nullptr; std::uint32_t highOffset = 0; std::uint32_t highImmediate = 0; - return ChaseRead(read, highHandle, highOffset, highImmediate) && (highHandle == &handle || EquivalentValue(m_program.Resources(), &handle, highHandle)) && highOffset == readOffset && highImmediate == readImmediate + static_cast(sizeof(std::uint32_t)); + if (!ChaseRead(read, highHandle, highOffset, highImmediate) || (highHandle != &handle && !EquivalentValue(m_program.Resources(), &handle, highHandle))) return std::nullopt; + if (highOffset != readOffset || highImmediate % sizeof(std::uint32_t) != 0u || highImmediate == readImmediate) return std::nullopt; + return highImmediate; } bool PointerRecordPairs(IrValue* low, IrValue* high, std::vector& lowReads, std::vector& highReads, std::vector& chases, std::uint32_t& chaseDepth) const { @@ -981,7 +983,9 @@ class Tracker { for (std::size_t index = 0; index < highWord->ArgumentCount(); ++index) pending.emplace_back(lowWord, highWord->Argument(index)); continue; } - if (!ChaseHighRead(*highWord, *handle, chase.readOffset, chase.readImmediate)) return false; + const auto highImmediate = ChaseHighRead(*highWord, *handle, chase.readOffset, chase.readImmediate); + if (!highImmediate.has_value()) return false; + chase.readHighImmediate = *highImmediate; const auto index = site(*handle, *pointer, chase); if (std::ranges::find(reached, index) == reached.end()) reached.push_back(index); continue; diff --git a/core/shader/recompiler/ShaderDiskCache.cpp b/core/shader/recompiler/ShaderDiskCache.cpp index 4ee4f3e111a1..3e373d0f4cf8 100644 --- a/core/shader/recompiler/ShaderDiskCache.cpp +++ b/core/shader/recompiler/ShaderDiskCache.cpp @@ -1098,6 +1098,7 @@ bool encodePlan(Writer& writer, const IrResourcePlan& plan) { chases.Value(chase.immediate); chases.Value(chase.readOffset); chases.Value(chase.readImmediate); + chases.Value(chase.readHighImmediate); }); out.Value(column.pointerChaseDepth); out.Value(column.foldedAddend); @@ -1269,6 +1270,7 @@ bool decodePlan(Reader& reader, IrResourcePlan& plan) { chases.Value(chase.immediate); chases.Value(chase.readOffset); chases.Value(chase.readImmediate); + chases.Value(chase.readHighImmediate); }); in.Value(column.pointerChaseDepth); in.Value(column.foldedAddend); @@ -1318,6 +1320,7 @@ bool decodePlan(Reader& reader, IrResourcePlan& plan) { if (parent.has_value() && (column.buffer || column.address || column.indirect || !parent->buffer || !parent->indirect || plan.descriptorSources[parent->heapSource].tableColumn.has_value())) return false; if (column.pointerKeyed && (!column.indirect || column.buffer || parent.has_value())) return false; if (column.pointerChases.empty() != (column.pointerChaseDepth == 0u) || column.pointerChaseDepth > MaxPointerChaseDepth || (column.pointerChaseDepth != 0u && !column.pointerKeyed)) return false; + if (std::ranges::any_of(column.pointerChases, [](const PointerChase& chase) { return chase.readHighImmediate % sizeof(std::uint32_t) != 0u || chase.readHighImmediate == chase.readImmediate; })) return false; } if (!std::ranges::all_of(plan.info.buffers, [&](const BufferResource& buffer) { return knownSource(buffer.source); })) return false; if (!std::ranges::all_of(plan.info.images, [&](const ImageResource& image) { return knownSource(image.source); })) return false; diff --git a/core/shader/recompiler/ShaderDiskCache.hpp b/core/shader/recompiler/ShaderDiskCache.hpp index d61a2b9e2002..dff4f070d585 100644 --- a/core/shader/recompiler/ShaderDiskCache.hpp +++ b/core/shader/recompiler/ShaderDiskCache.hpp @@ -13,7 +13,7 @@ namespace ShaderRecompiler::ShaderDiskCache { -inline constexpr std::uint32_t FormatVersion = 47; +inline constexpr std::uint32_t FormatVersion = 48; enum class LoadStatus { Loaded, diff --git a/docs/dev/debt/shader.md b/docs/dev/debt/shader.md index 37b7d1555711..8b5e55ab01a5 100644 --- a/docs/dev/debt/shader.md +++ b/docs/dev/debt/shader.md @@ -42,7 +42,7 @@ - [Nested image tables](../../../core/shader/recompiler/Optimization/src/ResourceTracker.cpp) support a V# loaded through a pointer from one scalar-buffer record origin, followed by an independently indexed T# or S#. A low-pointer Phi may merge that origin with constants; the GPU looks up the actual 64-bit pointer and faults if it was not captured. Other origins, shared V# consumers, T# or S# words read by anything but their descriptor handle, and deeper buffer nesting remain refused. A snapshot scans at most 524288 parent records and holds 1048576 inner key slots across its nested columns; equal V#s share a child snapshot within a column. The final image-table map is limited to 32 MiB. Synthetic readback covers the domains reported in #6191; complete title behavior remains unverified. - [Image tables](../../../core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp) read by `s_load` through a 64-bit pointer hold the keys the key's known bits allow, at most 65536; a key past them, or an entry at a wrapping, unaligned or unmapped address, faults when selected. - An [image table](../../../core/shader/recompiler/Optimization/src/ResourceTracker.cpp) T# or S# read through a pointer that a scalar-buffer record holds may have that pointer's low word merged by a Phi with constants, with lane masks (words of a `Ballot`, the waterfall loop's lane bit, and ANDs, ORs and NOTs of them), with the record word ANDed with a lane mask, and with reads of the same record word under another key, and its high word merged the same way from the record's next word. The two words are walked together, edge by edge, and an edge whose low word is a constant or a lane mask is skipped along with its high word. The snapshot then captures the pointer of every record, at most 65536, and the GPU looks up the actual 64-bit pointer; one that was not captured, such as a lane mask, faults when selected. Other origins of the two words remain refused, among them a high word read from a second buffer on an edge whose low word is the record word. -- A pointer-keyed [image table](../../../core/shader/recompiler/Optimization/src/ResourceTracker.cpp) chase that reaches itself through a Phi (a loop), or passes more than 4 chase sites, stays refused. A chase arm is two words a scalar buffer load reads at a fixed offset through a V# that `s_load` reads at a fixed offset of the record pointer or of another chase arm; on an edge whose low word is a chase arm, the high word must be that arm's own read. The [snapshot](../../../core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp) captures, for every record pointer, the pointers the chases reach up to the shader's depth, each with its own T# or S#. A V# that is not a buffer, or a read past the V# or outside the snapshot, ends that chase, and the GPU faults when it selects the pointer reached there. +- A pointer-keyed [image table](../../../core/shader/recompiler/Optimization/src/ResourceTracker.cpp) chase that reaches itself through a Phi (a loop), or passes more than 4 chase sites, stays refused. A chase arm is two words read through one V# at one buffer offset, each word at its own fixed immediate of the scalar buffer load (the high word need not follow the low one), where the V# is one that `s_load` reads at a fixed offset of the record pointer or of another chase arm; on an edge whose low word is a chase arm, the high word must be read through that arm's V# at that arm's buffer offset. The [snapshot](../../../core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp) captures, for every record pointer, the pointers the chases reach up to the shader's depth, each with its own T# or S#. A V# that is not a buffer, or a read past the V# or outside the snapshot, ends that chase, and the GPU faults when it selects the pointer reached there. - An [image table](../../../core/shader/recompiler/Optimization/src/ResourceTracker.cpp) whose key is neither a V#-relative scalar read nor a value the SRT walker evaluates (for example a shifted or masked runtime-offset V# read, a per-lane value made uniform by `v_readfirstlane`, or a loop-carried phi) binds every record of the snapshot domain instead of the selected ones. So does a table read at `(key << n) & mask` with such a key: a key domain would select records by the unmasked key, so that key is not narrowed. A key the walker evaluates selects its one record instead, unless the shader may write the memory the key is read from at run time (the same buffer, or any memory when the read or a write goes through a pointer or a GPU-only descriptor); then the column binds the whole domain too. A write to the key's memory through another V# over the same bytes is not detected. A shader whose selected records would pass 64 images or 64 sampled image/sampler pairs binds, for the heaps with the most such reads, the union of their keys in one table instead. - [Image table pointer indexing](../../../core/shader/recompiler/Optimization/src/ResourceTracker.cpp) propagates 32-bit value bounds through `ReadFirstLane` and `SelectU32` so waterfall key masks keep their bit domain. The SRT walker validates runtime select conditions whose active lane mask implies the condition through `LogicalAnd` conjunctions, assuming unselected inactive lanes need not be uniform across the full wave. The same assumption lets the two `s_load`s of one T# record match when one offset is added before `v_readfirstlane` and the other after (`ReadFirstLane(Select(mask, (key << 5) + 16, old), mask)` against `ReadFirstLane(key, mask) << 5`): with no active lane the hardware reads lane 0 of the inactive arm, and the table still binds the whole record. - An [image table](../../../core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp) T# of an MSAA or FMASK image, or of a color format under a depth compare, faults when selected: tables have no multisampled descriptors and no emulated color compare. From 52ae6543ae09390a5524c5c5a0d67dd3f998126e Mon Sep 17 00:00:00 2001 From: Yunus Shaikh <75296019+EagleBearerYS@users.noreply.github.com> Date: Sat, 24 Oct 2026 16:19:26 -0700 Subject: [PATCH 07/24] fix(shader): give up a pointer chase whose chased V# has an unaligned base Chase() decoded four words at the chased pointer as a V# and read the next pointer at its base plus the load's offsets, checking only that the address was accessible. When those words are not a buffer descriptor, the base can be unaligned, and the read threw "invalid shader memory read address" for the whole capture. Ratchet & Clank: Rift Apart (PPSA01474) stopped this way after 781 s, on a V# read as 4f81f1f 0 ffffffff 0. Every other runtime-address read in TableSnapshotter treats an unaligned address as unresolved; Chase() now does too, so the step is not captured, and a dispatch that takes it faults as outside the snapshot, like a link to memory the snapshot cannot read. --- .../tests/execution/ImageTable.cpp | 29 ++++++++++++++++++- .../Optimization/src/ResourceMaterializer.cpp | 4 ++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp index 8d33836995c5..e99ddc8e1471 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp @@ -638,6 +638,7 @@ struct Outcome { std::vector> samplers; std::vector> samplerTable; std::vector tableMap; + std::vector> imageTableRanges; std::vector>> imageTableReadRanges; }; @@ -665,6 +666,7 @@ Outcome Execute(AgcDriver::VulkanDevice& device, std::span outcome.imageTableFaults = result.imageTableFaults; outcome.poisonedSrtReads = result.poisonedSrtReads; outcome.cacheable = AgcDriver::DriverDetail::CacheableResult(result); + outcome.imageTableRanges = result.imageTableRanges; outcome.imageTableReadRanges = result.imageTableReadRanges; for (const auto& binding : result.bindings) { if (binding.role == ShaderRecompiler::DescriptorRole::ImageTable || binding.role == ShaderRecompiler::DescriptorRole::ImageStorageTable) outcome.imageTableCount = binding.count; @@ -2543,6 +2545,31 @@ void RunPointerChaseTests(AgcDriver::VulkanDevice& device) { Require(Faulted(outside) && outside.log.find("outside the snapshot") != std::string::npos, name + ": a link outside the snapshot did not fault as outside the snapshot:\n" + outside.log); Require(untouched(outside), name + ": a link outside the snapshot published copied output"); Require(outside.artifactId == taken.artifactId, name + ": a link outside the snapshot recompiled the shader"); + chase(1u, 2u, links[1].data()); + const std::array, 4> speculativeKeys{{{1u, 2u}, {2u, 1u}, {1u, 1u}, {1u, 1u}}}; + for (std::uint32_t group = 0; group < 4u; ++group) { + Keys[group * 4u] = speculativeKeys[group][0]; + Keys[group * 4u + 1u] = speculativeKeys[group][1]; + } + nodes[2][0x60u / 4u] += 1u; + const auto unaligned = run(PointerChaseCode); + Require(!Faulted(unaligned), name + ": a V# with an unaligned base on a step no group takes stopped the dispatch:\n" + unaligned.log); + Require(std::ranges::none_of(unaligned.imageTableRanges, [](const auto& range) { return (range.first & 3u) != 0u; }), name + ": the snapshot read guest memory through a V# with an unaligned base on a step no group takes"); + for (std::uint32_t group = 0; group < 4u; ++group) { + const auto expected = std::function([&, group](std::uint32_t x) { + auto node = recordNode[speculativeKeys[group][x < 32u ? 0u : 1u]]; + for (std::uint32_t step = 0; step < 2u; ++step) { + if ((group >> step) & 1u) node = next[node]; + } + return std::bit_cast(imageOf[node] != 0u ? OtherFloatTexels[x] : FloatTexels[x]); + }); + RequireRed(unaligned, group, Lanes, expected, name + ": the T# beside a V# with an unaligned base on a step no group takes, in group " + std::to_string(group)); + } + chase(2u, 0u, links[2].data()); + for (std::uint32_t group = 0; group < 4u; ++group) { + Keys[group * 4u] = keys[group][0]; + Keys[group * 4u + 1u] = keys[group][1]; + } const auto refused = [&](std::span code, const std::string& what) { std::string refusal; try { @@ -2554,7 +2581,7 @@ void RunPointerChaseTests(AgcDriver::VulkanDevice& device) { }; refused(PointerChaseLoopCode, "a chase repeated in a loop"); refused(PointerChaseDeepCode, "five conditional chase steps"); - std::puts("pointer table with a conditional V# chase: a T# at a record pointer chased up to twice through a V# at the pointer plus 0x60 checked for every taken and skipped step, a moved link and a link outside the snapshot, and a chase in a loop and five chase steps refused"); + std::puts("pointer table with a conditional V# chase: a T# at a record pointer chased up to twice through a V# at the pointer plus 0x60 checked for every taken and skipped step, a moved link, a link outside the snapshot and a V# with an unaligned base on a step no group takes, and a chase in a loop and five chase steps refused"); } void RunPointerChaseSplitTests(AgcDriver::VulkanDevice& device) { diff --git a/core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp b/core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp index cd1b4fb6b7e4..3498af001682 100644 --- a/core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp +++ b/core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp @@ -994,7 +994,9 @@ class TableSnapshotter { if (buffer.Type() != 0u) return std::nullopt; const auto low = ImageTableAbi::ScalarBufferDword(chase.readOffset, chase.readImmediate, buffer.GetSize()); const auto high = ImageTableAbi::ScalarBufferDword(chase.readOffset, chase.readHighImmediate, buffer.GetSize()); - if (!low.has_value() || !high.has_value() || !AddressAccessible(buffer.Base48() + *low) || !AddressAccessible(buffer.Base48() + *high)) return std::nullopt; + if (!low.has_value() || !high.has_value()) return std::nullopt; + if (((buffer.Base48() + *low) & 3u) != 0u || ((buffer.Base48() + *high) & 3u) != 0u) return std::nullopt; + if (!AddressAccessible(buffer.Base48() + *low) || !AddressAccessible(buffer.Base48() + *high)) return std::nullopt; tables.ranges.emplace_back(buffer.Base48() + *low, sizeof(std::uint32_t)); tables.ranges.emplace_back(buffer.Base48() + *high, sizeof(std::uint32_t)); return (static_cast(Read(buffer.Base48() + *high)) << 32u) | Read(buffer.Base48() + *low); From 62992d7437c404aed453580f01be17bc9dbcdcc1 Mon Sep 17 00:00:00 2001 From: Yunus Shaikh <75296019+EagleBearerYS@users.noreply.github.com> Date: Tue, 27 Oct 2026 21:54:51 -0700 Subject: [PATCH 08/24] fix(shader): give this PR's recompiler change its own disk cache format, 53 The format ladder along the stack is #8939 51 and this PR 53, so a cache written by #8939's recompiler is not read as this one's. --- core/shader/recompiler/ShaderDiskCache.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/shader/recompiler/ShaderDiskCache.hpp b/core/shader/recompiler/ShaderDiskCache.hpp index dad5115a4d9c..fc2b6bea030d 100644 --- a/core/shader/recompiler/ShaderDiskCache.hpp +++ b/core/shader/recompiler/ShaderDiskCache.hpp @@ -13,7 +13,7 @@ namespace ShaderRecompiler::ShaderDiskCache { -inline constexpr std::uint32_t FormatVersion = 51; +inline constexpr std::uint32_t FormatVersion = 53; enum class LoadStatus { Loaded, From 68d04647f3a2a35e915117a3e2729fc8ae5f7c0c Mon Sep 17 00:00:00 2001 From: Yunus Shaikh <75296019+EagleBearerYS@users.noreply.github.com> Date: Tue, 27 Oct 2026 21:55:22 -0700 Subject: [PATCH 09/24] fix(shader): give this PR's recompiler change its own disk cache format, 55 The format ladder along the stack is #9457 53 and this PR 55, so a cache written by #9457's recompiler is not read as this one's. --- core/shader/recompiler/ShaderDiskCache.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/shader/recompiler/ShaderDiskCache.hpp b/core/shader/recompiler/ShaderDiskCache.hpp index dad5115a4d9c..41fad42aa6c6 100644 --- a/core/shader/recompiler/ShaderDiskCache.hpp +++ b/core/shader/recompiler/ShaderDiskCache.hpp @@ -13,7 +13,7 @@ namespace ShaderRecompiler::ShaderDiskCache { -inline constexpr std::uint32_t FormatVersion = 51; +inline constexpr std::uint32_t FormatVersion = 55; enum class LoadStatus { Loaded, From fc0c03d48e6c8130c72edf1a0cb81e631f6184f1 Mon Sep 17 00:00:00 2001 From: Yunus Shaikh <75296019+EagleBearerYS@users.noreply.github.com> Date: Tue, 27 Oct 2026 21:55:53 -0700 Subject: [PATCH 10/24] fix(shader): give this PR's recompiler change its own disk cache format, 56 The format ladder along the stack is #9633 55 and this PR 56, so a cache written by #9633's recompiler is not read as this one's. --- core/shader/recompiler/ShaderDiskCache.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/shader/recompiler/ShaderDiskCache.hpp b/core/shader/recompiler/ShaderDiskCache.hpp index 41fad42aa6c6..3e43cbb22130 100644 --- a/core/shader/recompiler/ShaderDiskCache.hpp +++ b/core/shader/recompiler/ShaderDiskCache.hpp @@ -13,7 +13,7 @@ namespace ShaderRecompiler::ShaderDiskCache { -inline constexpr std::uint32_t FormatVersion = 55; +inline constexpr std::uint32_t FormatVersion = 56; enum class LoadStatus { Loaded, From 6c139245aa04aaba0e57c4009fd091312c4ef7ba Mon Sep 17 00:00:00 2001 From: Yunus Shaikh <75296019+EagleBearerYS@users.noreply.github.com> Date: Wed, 28 Oct 2026 01:30:14 -0700 Subject: [PATCH 11/24] fix(shader): follow a pointer chase through a V# whose words the shader edits Ghostrunner (PPSA03685) compute shader 0x200c520000 reads its T# pointer as Phi(R, H(R)). R is a pointer-keyed record pointer. H(R) is read at 0x30 of a buffer whose V# comes from R+0x30, with word 1 ANDed with 0xffff, word 2 shifted left by 4 and word 3 ORed with 0x30000000 before use (TheSovietWaffle's IR after srt on #8939). The chase took V# words only as read, so that arm fell through to the record-column check and the request stopped at LoadAddressU32@pc=0x0002ef78 with "GetImageResource dword 0 is not a valid runtime value". A chased V# word may now be the read word shifted left, ANDed or ORed by one constant. The chase keeps that edit per word, the snapshot applies it to the words it reads before it decodes the V#, and the shader disk cache stores it. The cache format stays 55: the source version already keeps this recompiler's caches apart. agc_driver_pointer_chase_edited_table models the arm with a V# at R+0x30 that covers the read at 0x30 only after the shader's edits, checked for taken and skipped chase steps and a moved link. --- core/libs/prx/libSceAgcDriver/CMakeLists.txt | 5 +- .../tests/execution/ImageTable.cpp | 99 +++++++++++++++++++ .../IrMetadata/ControlFlowInfo.hpp | 10 ++ .../Optimization/src/ResourceMaterializer.cpp | 2 +- .../Optimization/src/ResourceTracker.cpp | 18 +++- core/shader/recompiler/ShaderDiskCache.cpp | 13 ++- docs/dev/debt/shader.md | 2 +- 7 files changed, 143 insertions(+), 6 deletions(-) diff --git a/core/libs/prx/libSceAgcDriver/CMakeLists.txt b/core/libs/prx/libSceAgcDriver/CMakeLists.txt index 984565bf99ca..4910156ad98f 100644 --- a/core/libs/prx/libSceAgcDriver/CMakeLists.txt +++ b/core/libs/prx/libSceAgcDriver/CMakeLists.txt @@ -2931,11 +2931,12 @@ if(BUILD_TESTING) add_test(NAME agc_driver_pointer_hop_table COMMAND agc_driver_image_table_tests --pointer-hop) add_test(NAME agc_driver_pointer_chase_table COMMAND agc_driver_image_table_tests --pointer-chase) add_test(NAME agc_driver_pointer_chase_words_table COMMAND agc_driver_image_table_tests --pointer-chase-words) + add_test(NAME agc_driver_pointer_chase_edited_table COMMAND agc_driver_image_table_tests --pointer-chase-edited) add_test(NAME agc_driver_pointer_vsharp_fixed_table COMMAND agc_driver_image_table_tests --pointer-vsharp-fixed) add_test(NAME agc_driver_pointer_vsharp_fixed_offset_table COMMAND agc_driver_image_table_tests --pointer-vsharp-fixed-offset) - set_tests_properties(agc_driver_nested_image_table agc_driver_nested_image_table_large agc_driver_nested_image_table_budget agc_driver_nested_sampler_table agc_driver_pointer_phi_sampler_table agc_driver_pointer_phi_ballot_table agc_driver_pointer_phi_copies_table agc_driver_pointer_phi_lane_and_table agc_driver_pointer_phi_high_table agc_driver_pointer_phi_high_second_lane_table agc_driver_pointer_phi_lane_not_table agc_driver_pointer_vsharp_select_table agc_driver_pointer_hop_table agc_driver_pointer_chase_table agc_driver_pointer_chase_words_table agc_driver_pointer_vsharp_fixed_table agc_driver_pointer_vsharp_fixed_offset_table PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_NO_PIPELINE_JOURNAL=1" SKIP_RETURN_CODE 77 TIMEOUT 30 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") + set_tests_properties(agc_driver_nested_image_table agc_driver_nested_image_table_large agc_driver_nested_image_table_budget agc_driver_nested_sampler_table agc_driver_pointer_phi_sampler_table agc_driver_pointer_phi_ballot_table agc_driver_pointer_phi_copies_table agc_driver_pointer_phi_lane_and_table agc_driver_pointer_phi_high_table agc_driver_pointer_phi_high_second_lane_table agc_driver_pointer_phi_lane_not_table agc_driver_pointer_vsharp_select_table agc_driver_pointer_hop_table agc_driver_pointer_chase_table agc_driver_pointer_chase_words_table agc_driver_pointer_chase_edited_table agc_driver_pointer_vsharp_fixed_table agc_driver_pointer_vsharp_fixed_offset_table PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_NO_PIPELINE_JOURNAL=1" SKIP_RETURN_CODE 77 TIMEOUT 30 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") if(WIN32) - set_tests_properties(agc_driver_nested_image_table agc_driver_nested_image_table_large agc_driver_nested_image_table_budget agc_driver_nested_sampler_table agc_driver_pointer_phi_sampler_table agc_driver_pointer_phi_ballot_table agc_driver_pointer_phi_copies_table agc_driver_pointer_phi_lane_and_table agc_driver_pointer_phi_high_table agc_driver_pointer_phi_high_second_lane_table agc_driver_pointer_phi_lane_not_table agc_driver_pointer_vsharp_select_table agc_driver_pointer_hop_table agc_driver_pointer_chase_table agc_driver_pointer_chase_words_table agc_driver_pointer_vsharp_fixed_table agc_driver_pointer_vsharp_fixed_offset_table PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") + set_tests_properties(agc_driver_nested_image_table agc_driver_nested_image_table_large agc_driver_nested_image_table_budget agc_driver_nested_sampler_table agc_driver_pointer_phi_sampler_table agc_driver_pointer_phi_ballot_table agc_driver_pointer_phi_copies_table agc_driver_pointer_phi_lane_and_table agc_driver_pointer_phi_high_table agc_driver_pointer_phi_high_second_lane_table agc_driver_pointer_phi_lane_not_table agc_driver_pointer_vsharp_select_table agc_driver_pointer_hop_table agc_driver_pointer_chase_table agc_driver_pointer_chase_words_table agc_driver_pointer_chase_edited_table agc_driver_pointer_vsharp_fixed_table agc_driver_pointer_vsharp_fixed_offset_table PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() add_test(NAME agc_pipeline_journal COMMAND agc_driver_graphics_tests --pipeline-journal) add_test(NAME agc_pipeline_journal_long_paths COMMAND agc_driver_graphics_tests --pipeline-journal-long-paths) diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp index 2e17723cb512..a94720d5eb72 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp @@ -492,6 +492,16 @@ alignas(256) constexpr std::array PointerChaseWordsCode{ 0xbf8cc07fu, 0xf09c0f08u, 0x01090401u, 0x8afe3032u, 0xbf89ffe3u, 0xbefe042eu, 0x7e120202u, 0x34101286u, 0x4a101100u, 0x34101084u, 0xe0781000u, 0x80070408u, 0xbf810000u, }; +alignas(256) constexpr std::array PointerChaseEditedCode{ + 0xf4080100u, 0xfa000000u, 0xf4080200u, 0xfa000010u, 0xf4080700u, 0xfa000020u, 0xf4080800u, 0xfa000030u, + 0x8f108402u, 0xf4280404u, 0x20000000u, 0x360200bfu, 0x7e020d01u, 0x060202f0u, 0x100202ffu, 0x3c800000u, + 0x060202f2u, 0x7e0402ffu, 0x3e000000u, 0xbf8cc07fu, 0x7d8800a0u, 0x7e140210u, 0x02061411u, 0xbeae047eu, + 0x7e280503u, 0xd4c20030u, 0x00020614u, 0xbeb22430u, 0x8f158414u, 0xf4240b02u, 0x2a000008u, 0xbf8cc07fu, + 0xbf0d8012u, 0xbf84000bu, 0xf4080f16u, 0xfa000030u, 0xbf8cc07fu, 0x873dff3du, 0x0000ffffu, 0x8f3e843eu, + 0x883fff3fu, 0x30000000u, 0xf4240b1eu, 0xfa000030u, 0xbf8cc07fu, 0xf40c0916u, 0xfa000000u, 0xbf8cc07fu, + 0xf09c0f08u, 0x01090401u, 0x8afe3032u, 0xbf89ffe4u, 0xbefe042eu, 0x7e120202u, 0x34101286u, 0x4a101100u, + 0x34101084u, 0xe0781000u, 0x80070408u, 0xbf810000u, +}; alignas(256) constexpr std::array PointerFixedVsharpCode{ 0xf4080100u, 0xfa000000u, 0xf4080200u, 0xfa000010u, 0xf4080700u, 0xfa000020u, 0xf4080800u, 0xfa000030u, 0x8f108402u, 0xf4280404u, 0x20000000u, 0x360200bfu, 0x7e020d01u, 0x060202f0u, 0x100202ffu, 0x3c800000u, @@ -2606,6 +2616,91 @@ void RunPointerChaseWordsTests(AgcDriver::VulkanDevice& device) { std::puts("pointer table with two chase links in one V#: a T# at a record pointer chased through words 0 and 1 or words 2 and 3 of one V# at the pointer plus 0x60, each taken in its own groups, checked with a moved second link"); } +void RunPointerChaseEditedTests(AgcDriver::VulkanDevice& device) { + Fill(); + alignas(256) std::array, 4> nodes{}; + alignas(256) std::array, 4> links{}; + Table = {}; + Keys = {}; + const std::array images{Image(FloatTexels.data(), Format32Float), Image(OtherFloatTexels.data(), Format32Float)}; + const std::array imageOf{0u, 1u, 0u, 1u}; + std::array next{}; + const auto address = [](const void* data) { return static_cast(reinterpret_cast(data)); }; + const auto chase = [&](std::uint32_t from, std::uint32_t to) { + links[from][0x30u / 4u] = static_cast(address(nodes[to].data())); + links[from][0x34u / 4u] = static_cast(address(nodes[to].data()) >> 32u); + auto descriptor = Buffer(links[from].data(), 4u); + descriptor[1] |= 4u << 16u; + descriptor[3] &= ~0x30000000u; + std::copy(descriptor.begin(), descriptor.end(), nodes[from].begin() + 0x30u / 4u); + next[from] = to; + }; + for (std::uint32_t node = 0; node < 4u; ++node) { + std::copy(images[imageOf[node]].begin(), images[imageOf[node]].end(), nodes[node].begin()); + chase(node, node == 3u ? 3u : (node + 1u) % 3u); + } + const std::array recordNode{2u, 0u, 1u, 2u}; + for (std::uint32_t record = 0; record < 4u; ++record) { + Table[record * 4u + 2u] = static_cast(address(nodes[recordNode[record]].data())); + Table[record * 4u + 3u] = static_cast(address(nodes[recordNode[record]].data()) >> 32u); + } + const std::string name = "pointer table with an edited chase V#"; + struct Registration { + void* data; + Registration(void* data, std::size_t bytes) : data(data) { GuestAllocations::Mutation().Add(data, bytes, true, true, true); } + ~Registration() { GuestAllocations::Mutation().Remove(data); } + }; + const Registration registeredNodes(nodes.data(), sizeof(nodes)); + const Registration registeredLinks(links.data(), sizeof(links)); + const auto run = [&](std::span code) { + const auto table = Buffer(Table.data(), 64u); + const auto keys = Buffer(Keys.data(), static_cast(sizeof(Keys))); + const auto output = Buffer(Output.data(), static_cast(sizeof(Output))); + Srt = {}; + std::copy(table.begin(), table.end(), Srt.begin()); + std::copy(keys.begin(), keys.end(), Srt.begin() + 4u); + std::copy(output.begin(), output.end(), Srt.begin() + 8u); + std::copy(PointWrap.begin(), PointWrap.end(), Srt.begin() + 12u); + const std::vector memory{ + {reinterpret_cast(code.data()), std::as_bytes(code)}, + {reinterpret_cast(Srt.data()), std::as_bytes(std::span(Srt))}, + {reinterpret_cast(Table.data()), std::as_bytes(std::span(Table))}, + {reinterpret_cast(Keys.data()), std::as_bytes(std::span(Keys))}, + {reinterpret_cast(nodes.data()), std::as_bytes(std::span(nodes))}, + {reinterpret_cast(links.data()), std::as_bytes(std::span(links))}, + }; + const auto inputTable = Table; + const auto inputNodes = nodes; + const auto inputLinks = links; + auto result = Execute(device, code, Srt.data(), memory, 4u, Lanes, 64u); + Require(Table == inputTable && nodes == inputNodes && links == inputLinks, name + ": dispatch modified its input memory"); + Require(std::all_of(Output.begin() + 4u * Lanes * 4u, Output.end(), [](auto word) { return word == 0xdeadbeefu; }), name + ": dispatch wrote past its output"); + return result; + }; + const std::array, 4> keys{{{1u, 2u}, {2u, 1u}, {1u, 1u}, {2u, 2u}}}; + for (std::uint32_t group = 0; group < 4u; ++group) { + Keys[group * 4u] = keys[group][0]; + Keys[group * 4u + 1u] = keys[group][1]; + Keys[group * 4u + 2u] = group; + } + const auto sampled = [&](std::uint32_t group) { + return std::function([&, group](std::uint32_t x) { + auto node = recordNode[keys[group][x < 32u ? 0u : 1u]]; + if ((group & 1u) != 0u) node = next[node]; + return std::bit_cast(imageOf[node] != 0u ? OtherFloatTexels[x] : FloatTexels[x]); + }); + }; + const auto taken = run(PointerChaseEditedCode); + Require(!Faulted(taken), name + ": the T# behind the edited V# faulted:\n" + taken.log); + for (std::uint32_t group = 0; group < 4u; ++group) RequireRed(taken, group, Lanes, sampled(group), name + ": the T# at the record pointer or at the pointer read through the edited V# in group " + std::to_string(group)); + chase(0u, 3u); + const auto moved = run(PointerChaseEditedCode); + Require(!Faulted(moved), name + ": the T# behind a moved link faulted:\n" + moved.log); + Require(moved.artifactId == taken.artifactId, name + ": moving a link behind the edited V# recompiled the shader"); + for (std::uint32_t group = 0; group < 4u; ++group) RequireRed(moved, group, Lanes, sampled(group), name + ": the T# after node 0's link moved to node 3 in group " + std::to_string(group)); + std::puts("pointer table with an edited chase V#: a T# at a record pointer or at the pointer read at 0x30 through a V# from the record pointer plus 0x30 whose words 1, 2 and 3 the shader ANDs with 0xffff, shifts left by 4 and ORs with 0x30000000, a V# that covers the read only after those edits, checked for taken and skipped steps and a moved link"); +} + void RunPointerFixedVsharpTests(AgcDriver::VulkanDevice& device, std::uint32_t descriptorOffset) { Fill(); alignas(256) std::array, 2> records{}; @@ -3009,6 +3104,10 @@ int main(int argc, char** argv) { RunPointerChaseWordsTests(*device); return 0; } + if (argc == 2 && std::strcmp(argv[1], "--pointer-chase-edited") == 0) { + RunPointerChaseEditedTests(*device); + return 0; + } if (argc == 2 && std::strcmp(argv[1], "--pointer-vsharp-fixed") == 0) { RunPointerFixedVsharpTests(*device, 0u); return 0; diff --git a/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ControlFlowInfo.hpp b/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ControlFlowInfo.hpp index 7e869d531b7a..edc4fd46e293 100644 --- a/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ControlFlowInfo.hpp +++ b/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ControlFlowInfo.hpp @@ -37,11 +37,21 @@ struct PointerHop { inline constexpr std::uint32_t MaxPointerChaseDepth = 4; +struct DescriptorWordEdit { + std::uint32_t shift = 0; + std::uint32_t mask = 0xffffffffu; + std::uint32_t bits = 0; + + [[nodiscard]] std::uint32_t Apply(std::uint32_t word) const { return ((word << shift) & mask) | bits; } + bool operator==(const DescriptorWordEdit& other) const = default; +}; + struct PointerChase { std::uint32_t offset = 0; std::uint32_t immediate = 0; std::uint32_t readOffset = 0; std::uint32_t readImmediate = 0; + std::array edits {}; bool operator==(const PointerChase& other) const = default; }; diff --git a/core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp b/core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp index efe83b718c2f..b9dd80318568 100644 --- a/core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp +++ b/core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp @@ -1075,7 +1075,7 @@ class TableSnapshotter { for (std::uint32_t dword = 0; dword < 4u; dword++) { const auto address = ImageTableAbi::ScalarAddressDword(pointer, chase.offset, chase.immediate + dword * 4u); if (!address.has_value() || (*address & 3u) != 0u || !AddressAccessible(*address)) return std::nullopt; - descriptor.dwords[dword] = Read(*address); + descriptor.dwords[dword] = chase.edits[dword].Apply(Read(*address)); descriptor.availableDwordMask |= static_cast(1u << dword); tables.ranges.emplace_back(*address, sizeof(std::uint32_t)); } diff --git a/core/shader/recompiler/Optimization/src/ResourceTracker.cpp b/core/shader/recompiler/Optimization/src/ResourceTracker.cpp index 500e95f63ceb..6ea7907ad534 100644 --- a/core/shader/recompiler/Optimization/src/ResourceTracker.cpp +++ b/core/shader/recompiler/Optimization/src/ResourceTracker.cpp @@ -1023,9 +1023,25 @@ class Tracker { return handle->Opcode() == IrOpcode::GetBufferResource && handle->ArgumentCount() == 4u; } + static IrValue* EditedWord(IrValue* word, DescriptorWordEdit& edit) { + word = word->Resolve(); + std::uint32_t operand = 0; + if (word->ArgumentCount() != 2u) return word; + if (word->Opcode() == IrOpcode::ShiftLeftLogical32) { + if (!immediateU32(word->Argument(1), operand) || operand > 31u) return word; + edit.shift = operand; + return word->Argument(0)->Resolve(); + } + if (word->Opcode() != IrOpcode::BitwiseAnd32 && word->Opcode() != IrOpcode::BitwiseOr32) return word; + const bool left = immediateU32(word->Argument(0), operand); + if (!left && !immediateU32(word->Argument(1), operand)) return word; + (word->Opcode() == IrOpcode::BitwiseAnd32 ? edit.mask : edit.bits) = operand; + return word->Argument(left ? 1u : 0u)->Resolve(); + } + bool ChaseVsharp(IrValue& handle, IrValue*& pointer, PointerChase& chase) const { for (std::uint32_t dword = 0; dword < 4u; dword++) { - IrValue* word = handle.Argument(dword)->Resolve(); + IrValue* word = EditedWord(handle.Argument(dword), chase.edits[dword]); std::uint32_t memoryIndex = 0; const MemoryInfo* memory = ScalarAddressMemory(*word, memoryIndex); std::uint32_t offset = 0; diff --git a/core/shader/recompiler/ShaderDiskCache.cpp b/core/shader/recompiler/ShaderDiskCache.cpp index ed6dc611d2d8..5e5f06bb3445 100644 --- a/core/shader/recompiler/ShaderDiskCache.cpp +++ b/core/shader/recompiler/ShaderDiskCache.cpp @@ -1166,6 +1166,11 @@ bool encodePlan(Writer& writer, const IrResourcePlan& plan) { chases.Value(chase.immediate); chases.Value(chase.readOffset); chases.Value(chase.readImmediate); + for (const auto& edit : chase.edits) { + chases.Value(edit.shift); + chases.Value(edit.mask); + chases.Value(edit.bits); + } }); out.Value(column.pointerChaseDepth); out.Value(column.foldedAddend); @@ -1332,11 +1337,16 @@ bool decodePlan(Reader& reader, IrResourcePlan& plan) { hops.Value(hop.offset); hops.Value(hop.immediate); }); - in.List(column.pointerChases, 16, [](Reader& chases, PointerChase& chase) { + in.List(column.pointerChases, 64, [](Reader& chases, PointerChase& chase) { chases.Value(chase.offset); chases.Value(chase.immediate); chases.Value(chase.readOffset); chases.Value(chase.readImmediate); + for (auto& edit : chase.edits) { + chases.Value(edit.shift); + chases.Value(edit.mask); + chases.Value(edit.bits); + } }); in.Value(column.pointerChaseDepth); in.Value(column.foldedAddend); @@ -1386,6 +1396,7 @@ bool decodePlan(Reader& reader, IrResourcePlan& plan) { if (parent.has_value() && (column.buffer || column.address || column.indirect || !parent->buffer || !parent->indirect || plan.descriptorSources[parent->heapSource].tableColumn.has_value())) return false; if (column.pointerKeyed && (!column.indirect || column.buffer || parent.has_value())) return false; if (column.pointerChases.empty() != (column.pointerChaseDepth == 0u) || column.pointerChaseDepth > MaxPointerChaseDepth || (column.pointerChaseDepth != 0u && !column.pointerKeyed)) return false; + if (std::ranges::any_of(column.pointerChases, [](const PointerChase& chase) { return std::ranges::any_of(chase.edits, [](const DescriptorWordEdit& edit) { return edit.shift > 31u; }); })) return false; } if (!std::ranges::all_of(plan.info.buffers, [&](const BufferResource& buffer) { return knownSource(buffer.source); })) return false; if (!std::ranges::all_of(plan.info.images, [&](const ImageResource& image) { return knownSource(image.source); })) return false; diff --git a/docs/dev/debt/shader.md b/docs/dev/debt/shader.md index 55531d232fce..ddfd28ea78ed 100644 --- a/docs/dev/debt/shader.md +++ b/docs/dev/debt/shader.md @@ -53,7 +53,7 @@ - [Nested image tables](../../../core/shader/recompiler/Optimization/src/ResourceTracker.cpp) support a V# loaded through a pointer from one scalar-buffer record origin, followed by an independently indexed T# or S#. A low-pointer Phi may merge that origin with constants; the GPU looks up the actual 64-bit pointer and faults if it was not captured. Other origins, shared V# consumers, T# or S# words read by anything but their descriptor handle, and deeper buffer nesting remain refused. A snapshot scans at most 524288 parent records and holds 1048576 inner key slots across its nested columns; equal V#s share a child snapshot within a column. The final image-table map is limited to 32 MiB. Synthetic readback covers the domains reported in #6191; complete title behavior remains unverified. - [Image tables](../../../core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp) read by `s_load` through a 64-bit pointer hold the keys the key's known bits allow, at most 65536; a key past them, or an entry at a wrapping, unaligned or unmapped address, faults when selected. - An [image table](../../../core/shader/recompiler/Optimization/src/ResourceTracker.cpp) T# or S# read through a pointer that a scalar-buffer record holds may have that pointer's low word merged by a Phi with constants, with lane masks (words of a `Ballot`, the waterfall loop's lane bit, and ANDs, ORs and NOTs of them), with the record word ANDed with a lane mask, and with reads of the same record word under another key, and its high word merged the same way from the record's next word. The two words are walked together, edge by edge, and an edge whose low word is a constant or a lane mask is skipped along with its high word. The snapshot then captures the pointer of every record, at most 65536, and the GPU looks up the actual 64-bit pointer; one that was not captured, such as a lane mask, faults when selected. Other origins of the two words remain refused, among them a high word read from a second buffer on an edge whose low word is the record word. -- A pointer-keyed [image table](../../../core/shader/recompiler/Optimization/src/ResourceTracker.cpp) chase that reaches itself through a Phi (a loop), or passes more than 4 chase sites, stays refused. A chase arm is two words a scalar buffer load reads at a fixed offset through a V# that `s_load` reads at a fixed offset of the record pointer or of another chase arm; on an edge whose low word is a chase arm, the high word must be that arm's own read. The [snapshot](../../../core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp) captures, for every record pointer, the pointers the chases reach up to the shader's depth, each with its own T# or S#. A V# that is not a buffer, or a read past the V# or outside the snapshot, ends that chase, and the GPU faults when it selects the pointer reached there. +- A pointer-keyed [image table](../../../core/shader/recompiler/Optimization/src/ResourceTracker.cpp) chase that reaches itself through a Phi (a loop), or passes more than 4 chase sites, stays refused. A chase arm is two words a scalar buffer load reads at a fixed offset through a V# that `s_load` reads at a fixed offset of the record pointer or of another chase arm, each V# word either as read or after one shift left, AND or OR by a constant, which the snapshot applies to the words it reads; other edits of a V# word stay refused. On an edge whose low word is a chase arm, the high word must be that arm's own read. The [snapshot](../../../core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp) captures, for every record pointer, the pointers the chases reach up to the shader's depth, each with its own T# or S#. A V# that is not a buffer, or a read past the V# or outside the snapshot, ends that chase, and the GPU faults when it selects the pointer reached there. - An [image table](../../../core/shader/recompiler/Optimization/src/ResourceTracker.cpp) whose key is neither a V#-relative scalar read nor a value the SRT walker evaluates (for example a shifted or masked runtime-offset V# read, a per-lane value made uniform by `v_readfirstlane`, or a loop-carried phi) binds every record of the snapshot domain instead of the selected ones. So does a table read at `(key << n) & mask` with such a key: a key domain would select records by the unmasked key, so that key is not narrowed. A key the walker evaluates selects its one record instead, unless the shader may write the memory the key is read from at run time (the same buffer, or any memory when the read or a write goes through a pointer or a GPU-only descriptor); then the column binds the whole domain too. A write to the key's memory through another V# over the same bytes is not detected. A shader whose selected records would pass 64 images or 64 sampled image/sampler pairs binds, for the heaps with the most such reads, the union of their keys in one table instead. - [Image table pointer indexing](../../../core/shader/recompiler/Optimization/src/ResourceTracker.cpp) propagates 32-bit value bounds through `ReadFirstLane` and `SelectU32` so waterfall key masks keep their bit domain. The SRT walker validates runtime select conditions whose active lane mask implies the condition through `LogicalAnd` conjunctions, assuming unselected inactive lanes need not be uniform across the full wave. The same assumption lets the two `s_load`s of one T# record match when one offset is added before `v_readfirstlane` and the other after (`ReadFirstLane(Select(mask, (key << 5) + 16, old), mask)` against `ReadFirstLane(key, mask) << 5`): with no active lane the hardware reads lane 0 of the inactive arm, and the table still binds the whole record. - An [image table](../../../core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp) T# of an MSAA or FMASK image, or of a color format under a depth compare, faults when selected: tables have no multisampled descriptors and no emulated color compare. From 19bed84013a299568240ea9e761664ff81193007 Mon Sep 17 00:00:00 2001 From: Yunus Shaikh <75296019+EagleBearerYS@users.noreply.github.com> Date: Thu, 29 Oct 2026 03:46:06 -0700 Subject: [PATCH 12/24] fix(shader): give this PR's recompiler change its own disk cache format, 55 #8939 now takes 53, and 54 is held by #10489, so this PR takes 55. --- core/shader/recompiler/ShaderDiskCache.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/shader/recompiler/ShaderDiskCache.hpp b/core/shader/recompiler/ShaderDiskCache.hpp index fc2b6bea030d..41fad42aa6c6 100644 --- a/core/shader/recompiler/ShaderDiskCache.hpp +++ b/core/shader/recompiler/ShaderDiskCache.hpp @@ -13,7 +13,7 @@ namespace ShaderRecompiler::ShaderDiskCache { -inline constexpr std::uint32_t FormatVersion = 53; +inline constexpr std::uint32_t FormatVersion = 55; enum class LoadStatus { Loaded, From b6c114935d00371b791f76265aea1b78f055a1bb Mon Sep 17 00:00:00 2001 From: Yunus Shaikh <75296019+EagleBearerYS@users.noreply.github.com> Date: Thu, 29 Oct 2026 03:47:24 -0700 Subject: [PATCH 13/24] fix(shader): give this PR's recompiler change its own disk cache format, 56 #9457 now takes 55, so this PR takes 56. --- core/shader/recompiler/ShaderDiskCache.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/shader/recompiler/ShaderDiskCache.hpp b/core/shader/recompiler/ShaderDiskCache.hpp index 41fad42aa6c6..3e43cbb22130 100644 --- a/core/shader/recompiler/ShaderDiskCache.hpp +++ b/core/shader/recompiler/ShaderDiskCache.hpp @@ -13,7 +13,7 @@ namespace ShaderRecompiler::ShaderDiskCache { -inline constexpr std::uint32_t FormatVersion = 55; +inline constexpr std::uint32_t FormatVersion = 56; enum class LoadStatus { Loaded, From ee8767ff0de59e6db33a0c9ac941792b30915d77 Mon Sep 17 00:00:00 2001 From: Yunus Shaikh <75296019+EagleBearerYS@users.noreply.github.com> Date: Thu, 29 Oct 2026 03:58:55 -0700 Subject: [PATCH 14/24] test(shader): stop requiring a looped or five-step pointer chase to be refused The conditional pointer chase test required two valid shaders to throw "not a valid runtime value": one that repeats the chase in a loop and one with five conditional chase steps. Neither shape is implemented, and the test policy keeps tests from pinning valid but unimplemented shapes to a throw, so both checks, their helper and their shader words go. The taken and skipped steps, the moved link and the link outside the snapshot are still checked. --- .../tests/execution/ImageTable.cpp | 36 +------------------ 1 file changed, 1 insertion(+), 35 deletions(-) diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp index 24b4fcb8883e..166e97b9d978 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp @@ -459,29 +459,6 @@ alignas(256) constexpr std::array PointerChaseCode{ 0xf40c0916u, 0xfa000000u, 0xbf8cc07fu, 0xf09c0f08u, 0x01090401u, 0x8afe3032u, 0xbf89ffe1u, 0xbefe042eu, 0x7e120202u, 0x34101286u, 0x4a101100u, 0x34101084u, 0xe0781000u, 0x80070408u, 0xbf810000u, }; -alignas(256) constexpr std::array PointerChaseLoopCode{ - 0xf4080100u, 0xfa000000u, 0xf4080200u, 0xfa000010u, 0xf4080700u, 0xfa000020u, 0xf4080800u, 0xfa000030u, - 0x8f108402u, 0xf4280404u, 0x20000000u, 0x360200bfu, 0x7e020d01u, 0x060202f0u, 0x100202ffu, 0x3c800000u, - 0x060202f2u, 0x7e0402ffu, 0x3e000000u, 0xbf8cc07fu, 0x7d8800a0u, 0x7e140210u, 0x02061411u, 0xbeae047eu, - 0x7e280503u, 0xd4c20030u, 0x00020614u, 0xbeb22430u, 0x8f158414u, 0xf4240b02u, 0x2a000008u, 0xbf8cc07fu, - 0xbe960312u, 0xbf068016u, 0xbf850008u, 0xf4080f16u, 0xfa000060u, 0xbf8cc07fu, 0xf4240b1eu, 0xfa000000u, - 0xbf8cc07fu, 0x80968116u, 0xbf82fff6u, 0xf40c0916u, 0xfa000000u, 0xbf8cc07fu, 0xf09c0f08u, 0x01090401u, - 0x8afe3032u, 0xbf89ffe6u, 0xbefe042eu, 0x7e120202u, 0x34101286u, 0x4a101100u, 0x34101084u, 0xe0781000u, - 0x80070408u, 0xbf810000u, -}; -alignas(256) constexpr std::array PointerChaseDeepCode{ - 0xf4080100u, 0xfa000000u, 0xf4080200u, 0xfa000010u, 0xf4080700u, 0xfa000020u, 0xf4080800u, 0xfa000030u, - 0x8f108402u, 0xf4280404u, 0x20000000u, 0x360200bfu, 0x7e020d01u, 0x060202f0u, 0x100202ffu, 0x3c800000u, - 0x060202f2u, 0x7e0402ffu, 0x3e000000u, 0xbf8cc07fu, 0x7d8800a0u, 0x7e140210u, 0x02061411u, 0xbeae047eu, - 0x7e280503u, 0xd4c20030u, 0x00020614u, 0xbeb22430u, 0x8f158414u, 0xf4240b02u, 0x2a000008u, 0xbf8cc07fu, - 0xbf0d8012u, 0xbf840006u, 0xf4080f16u, 0xfa000060u, 0xbf8cc07fu, 0xf4240b1eu, 0xfa000000u, 0xbf8cc07fu, - 0xbf0d8112u, 0xbf840006u, 0xf4081016u, 0xfa000060u, 0xbf8cc07fu, 0xf4240b20u, 0xfa000000u, 0xbf8cc07fu, - 0xbf0d8212u, 0xbf840006u, 0xf4081116u, 0xfa000060u, 0xbf8cc07fu, 0xf4240b22u, 0xfa000000u, 0xbf8cc07fu, - 0xbf0d8312u, 0xbf840006u, 0xf4081216u, 0xfa000060u, 0xbf8cc07fu, 0xf4240b24u, 0xfa000000u, 0xbf8cc07fu, - 0xbf0d8412u, 0xbf840006u, 0xf4081316u, 0xfa000060u, 0xbf8cc07fu, 0xf4240b26u, 0xfa000000u, 0xbf8cc07fu, - 0xf40c0916u, 0xfa000000u, 0xbf8cc07fu, 0xf09c0f08u, 0x01090401u, 0x8afe3032u, 0xbf89ffc9u, 0xbefe042eu, - 0x7e120202u, 0x34101286u, 0x4a101100u, 0x34101084u, 0xe0781000u, 0x80070408u, 0xbf810000u, -}; alignas(256) constexpr std::array PointerChaseWordsCode{ 0xf4080100u, 0xfa000000u, 0xf4080200u, 0xfa000010u, 0xf4080700u, 0xfa000020u, 0xf4080800u, 0xfa000030u, 0x8f108402u, 0xf4280404u, 0x20000000u, 0x360200bfu, 0x7e020d01u, 0x060202f0u, 0x100202ffu, 0x3c800000u, @@ -2501,18 +2478,7 @@ void RunPointerChaseTests(AgcDriver::VulkanDevice& device) { Require(Faulted(outside) && outside.log.find("outside the snapshot") != std::string::npos, name + ": a link outside the snapshot did not fault as outside the snapshot:\n" + outside.log); Require(untouched(outside), name + ": a link outside the snapshot published copied output"); Require(outside.artifactId == taken.artifactId, name + ": a link outside the snapshot recompiled the shader"); - const auto refused = [&](std::span code, const std::string& what) { - std::string refusal; - try { - static_cast(run(code)); - } catch (const std::runtime_error& error) { - refusal = error.what(); - } - Require(refusal.find("not a valid runtime value") != std::string::npos, name + ": " + what + " was not refused by resource planning: " + refusal); - }; - refused(PointerChaseLoopCode, "a chase repeated in a loop"); - refused(PointerChaseDeepCode, "five conditional chase steps"); - std::puts("pointer table with a conditional V# chase: a T# at a record pointer chased up to twice through a V# at the pointer plus 0x60 checked for every taken and skipped step, a moved link and a link outside the snapshot, and a chase in a loop and five chase steps refused"); + std::puts("pointer table with a conditional V# chase: a T# at a record pointer chased up to twice through a V# at the pointer plus 0x60 checked for every taken and skipped step, a moved link and a link outside the snapshot"); } void RunPointerChaseWordsTests(AgcDriver::VulkanDevice& device) { From f6b5d58d10abfb63db4d9cb4eaebef0000f717ad Mon Sep 17 00:00:00 2001 From: Yunus Shaikh <75296019+EagleBearerYS@users.noreply.github.com> Date: Thu, 29 Oct 2026 04:00:43 -0700 Subject: [PATCH 15/24] fix(shader): give this PR's recompiler change its own disk cache format, 57 #9633 now takes 56, so this PR takes 57. --- core/shader/recompiler/ShaderDiskCache.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/shader/recompiler/ShaderDiskCache.hpp b/core/shader/recompiler/ShaderDiskCache.hpp index 3e43cbb22130..12df2ae98d01 100644 --- a/core/shader/recompiler/ShaderDiskCache.hpp +++ b/core/shader/recompiler/ShaderDiskCache.hpp @@ -13,7 +13,7 @@ namespace ShaderRecompiler::ShaderDiskCache { -inline constexpr std::uint32_t FormatVersion = 56; +inline constexpr std::uint32_t FormatVersion = 57; enum class LoadStatus { Loaded, From c4f25ba3fc7dcf52255f7d12f657ea4c885368a9 Mon Sep 17 00:00:00 2001 From: Yunus Shaikh <75296019+EagleBearerYS@users.noreply.github.com> Date: Thu, 29 Oct 2026 04:00:45 -0700 Subject: [PATCH 16/24] test(shader): stop requiring split high-word pointer chases to be refused The split-word pointer chase test required three valid shaders to throw "not a valid runtime value": a high word read through another V#, at another buffer offset, and at the low word. None of these shapes is implemented, and the test policy keeps tests from pinning valid but unimplemented shapes to a throw, so the three checks, their helper and their shader words go. The taken and skipped steps, the node only the third step reaches, the moved link and the link outside the snapshot are still checked. --- .../tests/execution/ImageTable.cpp | 50 +------------------ 1 file changed, 1 insertion(+), 49 deletions(-) diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp index 26067869058e..82e747f0f651 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp @@ -471,42 +471,6 @@ alignas(256) constexpr std::array PointerChaseSplitCode{ 0xbf89ffd7u, 0xbefe042eu, 0x7e120202u, 0x34101286u, 0x4a101100u, 0x34101084u, 0xe0781000u, 0x80070408u, 0xbf810000u, }; -alignas(256) constexpr std::array PointerChaseSplitHandleCode{ - 0xf4080100u, 0xfa000000u, 0xf4080200u, 0xfa000010u, 0xf4080700u, 0xfa000020u, 0xf4080800u, 0xfa000030u, - 0x8f108402u, 0xf4280404u, 0x20000000u, 0x360200bfu, 0x7e020d01u, 0x060202f0u, 0x100202ffu, 0x3c800000u, - 0x060202f2u, 0x7e0402ffu, 0x3e000000u, 0xbf8cc07fu, 0x7d8800a0u, 0x7e140210u, 0x02061411u, 0xbeae047eu, - 0x7e280503u, 0xd4c20030u, 0x00020614u, 0xbeb22430u, 0x8f158414u, 0xf4240b02u, 0x2a000008u, 0xbf8cc07fu, - 0xbf0d8012u, 0xbf840006u, 0xf4080f16u, 0xfa000060u, 0xbf8cc07fu, 0xf4240b1eu, 0xfa000000u, 0xbf8cc07fu, - 0xbf0d8112u, 0xbf840006u, 0xf4081016u, 0xfa000060u, 0xbf8cc07fu, 0xf4240b20u, 0xfa000000u, 0xbf8cc07fu, - 0xbf0d8212u, 0xbf84000cu, 0xf4081116u, 0xfa000110u, 0xf4081616u, 0xfa000120u, 0xbf8cc07fu, 0xf4301222u, - 0xfa000040u, 0xf420172cu, 0xfa00006cu, 0xbf8cc07fu, 0xbeac034du, 0xbead035cu, 0xf40c0916u, 0xfa000000u, - 0xbf8cc07fu, 0xf09c0f08u, 0x01090401u, 0x8afe3032u, 0xbf89ffd3u, 0xbefe042eu, 0x7e120202u, 0x34101286u, - 0x4a101100u, 0x34101084u, 0xe0781000u, 0x80070408u, 0xbf810000u, -}; -alignas(256) constexpr std::array PointerChaseSplitOffsetCode{ - 0xf4080100u, 0xfa000000u, 0xf4080200u, 0xfa000010u, 0xf4080700u, 0xfa000020u, 0xf4080800u, 0xfa000030u, - 0x8f108402u, 0xf4280404u, 0x20000000u, 0x360200bfu, 0x7e020d01u, 0x060202f0u, 0x100202ffu, 0x3c800000u, - 0x060202f2u, 0x7e0402ffu, 0x3e000000u, 0xbf8cc07fu, 0x7d8800a0u, 0x7e140210u, 0x02061411u, 0xbeae047eu, - 0x7e280503u, 0xd4c20030u, 0x00020614u, 0xbeb22430u, 0x8f158414u, 0xf4240b02u, 0x2a000008u, 0xbf8cc07fu, - 0xbf0d8012u, 0xbf840006u, 0xf4080f16u, 0xfa000060u, 0xbf8cc07fu, 0xf4240b1eu, 0xfa000000u, 0xbf8cc07fu, - 0xbf0d8112u, 0xbf840006u, 0xf4081016u, 0xfa000060u, 0xbf8cc07fu, 0xf4240b20u, 0xfa000000u, 0xbf8cc07fu, - 0xbf0d8212u, 0xbf84000bu, 0xf4081116u, 0xfa000110u, 0xbf8cc07fu, 0xb05d0010u, 0xf4301222u, 0xfa000040u, - 0xf4201722u, 0xba00005cu, 0xbf8cc07fu, 0xbeac034du, 0xbead035cu, 0xf40c0916u, 0xfa000000u, 0xbf8cc07fu, - 0xf09c0f08u, 0x01090401u, 0x8afe3032u, 0xbf89ffd4u, 0xbefe042eu, 0x7e120202u, 0x34101286u, 0x4a101100u, - 0x34101084u, 0xe0781000u, 0x80070408u, 0xbf810000u, -}; -alignas(256) constexpr std::array PointerChaseSplitWordCode{ - 0xf4080100u, 0xfa000000u, 0xf4080200u, 0xfa000010u, 0xf4080700u, 0xfa000020u, 0xf4080800u, 0xfa000030u, - 0x8f108402u, 0xf4280404u, 0x20000000u, 0x360200bfu, 0x7e020d01u, 0x060202f0u, 0x100202ffu, 0x3c800000u, - 0x060202f2u, 0x7e0402ffu, 0x3e000000u, 0xbf8cc07fu, 0x7d8800a0u, 0x7e140210u, 0x02061411u, 0xbeae047eu, - 0x7e280503u, 0xd4c20030u, 0x00020614u, 0xbeb22430u, 0x8f158414u, 0xf4240b02u, 0x2a000008u, 0xbf8cc07fu, - 0xbf0d8012u, 0xbf840006u, 0xf4080f16u, 0xfa000060u, 0xbf8cc07fu, 0xf4240b1eu, 0xfa000000u, 0xbf8cc07fu, - 0xbf0d8112u, 0xbf840006u, 0xf4081016u, 0xfa000060u, 0xbf8cc07fu, 0xf4240b20u, 0xfa000000u, 0xbf8cc07fu, - 0xbf0d8212u, 0xbf840008u, 0xf4081116u, 0xfa000110u, 0xbf8cc07fu, 0xf4301222u, 0xfa000040u, 0xbf8cc07fu, - 0xbeac034du, 0xbead034du, 0xf40c0916u, 0xfa000000u, 0xbf8cc07fu, 0xf09c0f08u, 0x01090401u, 0x8afe3032u, - 0xbf89ffd7u, 0xbefe042eu, 0x7e120202u, 0x34101286u, 0x4a101100u, 0x34101084u, 0xe0781000u, 0x80070408u, - 0xbf810000u, -}; alignas(256) constexpr std::array PointerChaseWordsCode{ 0xf4080100u, 0xfa000000u, 0xf4080200u, 0xfa000010u, 0xf4080700u, 0xfa000020u, 0xf4080800u, 0xfa000030u, 0x8f108402u, 0xf4280404u, 0x20000000u, 0x360200bfu, 0x7e020d01u, 0x060202f0u, 0x100202ffu, 0x3c800000u, @@ -2649,18 +2613,6 @@ void RunPointerChaseSplitTests(AgcDriver::VulkanDevice& device) { }); }; const auto untouched = [](const Outcome& outcome) { return std::all_of(outcome.words.begin(), outcome.words.end(), [](auto word) { return word == 0xdeadbeefu; }); }; - const auto refused = [&](std::span code, const std::string& what) { - std::string refusal; - try { - static_cast(run(code)); - } catch (const std::runtime_error& error) { - refusal = error.what(); - } - Require(refusal.find("not a valid runtime value") != std::string::npos, name + ": " + what + " was not refused by resource planning: " + refusal); - }; - refused(PointerChaseSplitHandleCode, "a high word read through another V#"); - refused(PointerChaseSplitOffsetCode, "a high word read at another buffer offset"); - refused(PointerChaseSplitWordCode, "a high word read at the low word"); const auto taken = run(PointerChaseSplitCode); Require(!Faulted(taken), name + ": the T# at the end of the chase faulted:\n" + taken.log); for (std::uint32_t group = 0; group < 4u; ++group) RequireRed(taken, group, Lanes, sampled(group), name + ": the T# at the pointer reached by the chase steps taken in group " + std::to_string(group)); @@ -2675,7 +2627,7 @@ void RunPointerChaseSplitTests(AgcDriver::VulkanDevice& device) { Require(Faulted(outside) && outside.log.find("outside the snapshot") != std::string::npos, name + ": a split-word link outside the snapshot did not fault as outside the snapshot:\n" + outside.log); Require(untouched(outside), name + ": a split-word link outside the snapshot published copied output"); Require(outside.artifactId == taken.artifactId, name + ": a split-word link outside the snapshot recompiled the shader"); - std::puts("pointer table with a split-word V# chase: a T# at a record pointer chased twice through a V# at the pointer plus 0x60 and once through a V# at the pointer plus 0x110 whose next pointer is dwords 5 and 11 of one 16-dword load from 0x40, checked for every taken and skipped step with a node only the third chase step reaches, a moved link and a link outside the snapshot, and a high word through another V#, at another buffer offset or at the low word refused"); + std::puts("pointer table with a split-word V# chase: a T# at a record pointer chased twice through a V# at the pointer plus 0x60 and once through a V# at the pointer plus 0x110 whose next pointer is dwords 5 and 11 of one 16-dword load from 0x40, checked for every taken and skipped step with a node only the third chase step reaches, a moved link and a link outside the snapshot"); } void RunPointerChaseWordsTests(AgcDriver::VulkanDevice& device) { From eac5dce947f09e4ecd65120724042f7993dd56c5 Mon Sep 17 00:00:00 2001 From: Yunus Shaikh <75296019+EagleBearerYS@users.noreply.github.com> Date: Thu, 29 Oct 2026 13:03:52 -0700 Subject: [PATCH 17/24] docs(debt): name the pointer-chase refusals for an unaligned base and a shared immediate Since 52ae6543ae a chase ends at a chased V# whose base leaves the low or high word's address unaligned, even inside the snapshot, and the tracker refuses a high word read at the low word's own immediate. Neither was in the chase entry. The entry now leads with what stays refused and where a chase ends, including the unmeasured case of a console that reads the dword-aligned address, and stays under the 1000-character limit. --- docs/dev/debt/shader.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/dev/debt/shader.md b/docs/dev/debt/shader.md index 213fcca32582..cf14e1257927 100644 --- a/docs/dev/debt/shader.md +++ b/docs/dev/debt/shader.md @@ -53,7 +53,7 @@ - [Nested image tables](../../../core/shader/recompiler/Optimization/src/ResourceTracker.cpp) support a V# loaded through a pointer from one scalar-buffer record origin, followed by an independently indexed T# or S#. A low-pointer Phi may merge that origin with constants; the GPU looks up the actual 64-bit pointer and faults if it was not captured. Other origins, shared V# consumers, T# or S# words read by anything but their descriptor handle, and deeper buffer nesting remain refused. A snapshot scans at most 524288 parent records and holds 1048576 inner key slots across its nested columns; equal V#s share a child snapshot within a column. The final image-table map is limited to 32 MiB. Synthetic readback covers the domains reported in #6191; complete title behavior remains unverified. - [Image tables](../../../core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp) read by `s_load` through a 64-bit pointer hold the keys the key's known bits allow, at most 65536; a key past them, or an entry at a wrapping, unaligned or unmapped address, faults when selected. - An [image table](../../../core/shader/recompiler/Optimization/src/ResourceTracker.cpp) T# or S# read through a pointer that a scalar-buffer record holds may have that pointer's low word merged by a Phi with constants, with lane masks (words of a `Ballot`, the waterfall loop's lane bit, and ANDs, ORs and NOTs of them), with the record word ANDed with a lane mask, and with reads of the same record word under another key, and its high word merged the same way from the record's next word. The two words are walked together, edge by edge, and an edge whose low word is a constant or a lane mask is skipped along with its high word. The snapshot then captures the pointer of every record, at most 65536, and the GPU looks up the actual 64-bit pointer; one that was not captured, such as a lane mask, faults when selected. Other origins of the two words remain refused, among them a high word read from a second buffer on an edge whose low word is the record word. -- A pointer-keyed [image table](../../../core/shader/recompiler/Optimization/src/ResourceTracker.cpp) chase that reaches itself through a Phi (a loop), or passes more than 4 chase sites, stays refused. A chase arm is two words a scalar buffer load reads at one offset, each at its own fixed immediate, through a V# that `s_load` reads at a fixed offset of the record pointer or of another chase arm; each V# word may be shifted left, ANDed or ORed once by a constant, as the snapshot does too; other edits stay refused. On an edge whose low word is a chase arm, the high word must be read through that arm's V# at its buffer offset. The [snapshot](../../../core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp) captures, for every record pointer, the pointers the chases reach up to the shader's depth, each with its own T# or S#. A V# that is not a buffer, or a read past the V# or outside the snapshot, ends that chase, and the GPU faults when it selects the pointer reached there. +- A pointer-keyed [image table](../../../core/shader/recompiler/Optimization/src/ResourceTracker.cpp) chase stays refused when it loops through a Phi, passes more than 4 chase sites, edits a V# word other than by one constant left shift, AND or OR, or reads the high word at the low word's immediate or through another V# or buffer offset. The [snapshot](../../../core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp) ends a chase at a V# that is not a buffer, at a read past the V# or outside the snapshot, and at a chased V# base that leaves either word's address unaligned; the GPU faults when it selects the pointer reached there. Whether the console reads the dword-aligned address instead is not measured. A chase arm is two words read by one scalar buffer load offset, each at its own immediate, through a V# that `s_load` reads at a fixed offset of the record pointer or of another arm. The snapshot captures the pointers the chases reach, up to the shader's depth. - An [image table](../../../core/shader/recompiler/Optimization/src/ResourceTracker.cpp) whose key is neither a V#-relative scalar read nor a value the SRT walker evaluates (for example a shifted or masked runtime-offset V# read, a per-lane value made uniform by `v_readfirstlane`, or a loop-carried phi) binds every record of the snapshot domain instead of the selected ones. So does a table read at `(key << n) & mask` with such a key: a key domain would select records by the unmasked key, so that key is not narrowed. A key the walker evaluates selects its one record instead, unless the shader may write the memory the key is read from at run time (the same buffer, or any memory when the read or a write goes through a pointer or a GPU-only descriptor); then the column binds the whole domain too. A write to the key's memory through another V# over the same bytes is not detected. A shader whose selected records would pass 64 images or 64 sampled image/sampler pairs binds, for the heaps with the most such reads, the union of their keys in one table instead. - [Image table pointer indexing](../../../core/shader/recompiler/Optimization/src/ResourceTracker.cpp) propagates 32-bit value bounds through `ReadFirstLane` and `SelectU32` so waterfall key masks keep their bit domain. The SRT walker validates runtime select conditions whose active lane mask implies the condition through `LogicalAnd` conjunctions, assuming unselected inactive lanes need not be uniform across the full wave. The same assumption lets the two `s_load`s of one T# record match when one offset is added before `v_readfirstlane` and the other after (`ReadFirstLane(Select(mask, (key << 5) + 16, old), mask)` against `ReadFirstLane(key, mask) << 5`): with no active lane the hardware reads lane 0 of the inactive arm, and the table still binds the whole record. - An [image table](../../../core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp) T# of an MSAA or FMASK image, or of a color format under a depth compare, faults when selected: tables have no multisampled descriptors and no emulated color compare. From babbb0eafd8b2f519c90e8b2fb19fa74373824eb Mon Sep 17 00:00:00 2001 From: Yunus Shaikh <75296019+EagleBearerYS@users.noreply.github.com> Date: Thu, 29 Oct 2026 13:00:20 -0700 Subject: [PATCH 18/24] test(shader): round-trip a pointer chase whose high word has its own immediate The chase tests run without the shader disk cache, so nothing checked that a cached plan keeps readHighImmediate. A pointer-keyed column with one chase whose high word is read 16 bytes past its low word now goes through EncodePlan and DecodePlan and must come back equal. --- core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp b/core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp index 55b145e9b7f3..0a0606996b54 100644 --- a/core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp @@ -2364,6 +2364,15 @@ void verifyPlanRoundTrip() { const auto pointerBytes = encodedPlan(pointerTable); IrResourcePlan pointerDecoded; require(pointerBytes != handBytes && ShaderDiskCache::DecodePlan(pointerBytes, pointerDecoded) && samePlan(pointerTable, pointerDecoded) && pointerDecoded.descriptorSources[1].tableColumn->pointerKeyed, "the pointer-keyed column changed in a disk-cache round trip"); + auto chaseTable = handBuiltPlan(); + auto& chaseColumn = *chaseTable.descriptorSources[1].tableColumn; + chaseColumn.indirect = true; + chaseColumn.pointerKeyed = true; + chaseColumn.pointerChaseDepth = 1u; + chaseColumn.pointerChases = {PointerChase{8u, 16u, 4u, 24u, 40u, {}}}; + const auto chaseBytes = encodedPlan(chaseTable); + IrResourcePlan chaseDecoded; + require(chaseBytes != pointerBytes && ShaderDiskCache::DecodePlan(chaseBytes, chaseDecoded) && samePlan(chaseTable, chaseDecoded) && chaseDecoded.descriptorSources[1].tableColumn->pointerChases == chaseColumn.pointerChases, "a pointer chase whose high word has its own immediate changed in a disk-cache round trip"); PlanFixture handFixture; handFixture.userData = {static_cast(PlanSrtAddress), static_cast(PlanSrtAddress >> 32u), 0u, 0u}; std::vector handSrt(16, 0u); From 49f6939fc0be3055e4b64a569b83cc1852916bd5 Mon Sep 17 00:00:00 2001 From: Yunus Shaikh <75296019+EagleBearerYS@users.noreply.github.com> Date: Sun, 1 Nov 2026 09:59:54 -0800 Subject: [PATCH 19/24] fix(shader): give this PR's recompiler change its own disk cache format, 67 #8939 now takes 66, and no open pull request uses a number above it, so this PR takes 67. --- core/shader/recompiler/ShaderDiskCache.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/shader/recompiler/ShaderDiskCache.hpp b/core/shader/recompiler/ShaderDiskCache.hpp index 41fad42aa6c6..0f8a38b5a9f2 100644 --- a/core/shader/recompiler/ShaderDiskCache.hpp +++ b/core/shader/recompiler/ShaderDiskCache.hpp @@ -13,7 +13,7 @@ namespace ShaderRecompiler::ShaderDiskCache { -inline constexpr std::uint32_t FormatVersion = 55; +inline constexpr std::uint32_t FormatVersion = 67; enum class LoadStatus { Loaded, From 280d6d6c445d1822a9c6d5256b3b48dbfe5d20f5 Mon Sep 17 00:00:00 2001 From: Yunus Shaikh <75296019+EagleBearerYS@users.noreply.github.com> Date: Mon, 2 Nov 2026 00:40:59 -0800 Subject: [PATCH 20/24] fix(shader): give this PR's recompiler change its own disk cache format, 70 #9457 is at 67, so this PR takes 70, above it and every other open PR's number. --- core/shader/recompiler/ShaderDiskCache.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/shader/recompiler/ShaderDiskCache.hpp b/core/shader/recompiler/ShaderDiskCache.hpp index 3e43cbb22130..749a82f98dc4 100644 --- a/core/shader/recompiler/ShaderDiskCache.hpp +++ b/core/shader/recompiler/ShaderDiskCache.hpp @@ -13,7 +13,7 @@ namespace ShaderRecompiler::ShaderDiskCache { -inline constexpr std::uint32_t FormatVersion = 56; +inline constexpr std::uint32_t FormatVersion = 70; enum class LoadStatus { Loaded, From 4699b25e2dd9daae64f2d4acac6444f05ce87dca Mon Sep 17 00:00:00 2001 From: Yunus Shaikh <75296019+EagleBearerYS@users.noreply.github.com> Date: Mon, 2 Nov 2026 00:43:16 -0800 Subject: [PATCH 21/24] fix(shader): give this PR's recompiler change its own disk cache format, 72 #9633 now takes 70, so this PR takes 72, above it and every other open PR's number. --- core/shader/recompiler/ShaderDiskCache.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/shader/recompiler/ShaderDiskCache.hpp b/core/shader/recompiler/ShaderDiskCache.hpp index 12df2ae98d01..f928fc60ff54 100644 --- a/core/shader/recompiler/ShaderDiskCache.hpp +++ b/core/shader/recompiler/ShaderDiskCache.hpp @@ -13,7 +13,7 @@ namespace ShaderRecompiler::ShaderDiskCache { -inline constexpr std::uint32_t FormatVersion = 57; +inline constexpr std::uint32_t FormatVersion = 72; enum class LoadStatus { Loaded, From 5be1abeb415b150a073e6040415ede465c0695a9 Mon Sep 17 00:00:00 2001 From: Yunus Shaikh <75296019+EagleBearerYS@users.noreply.github.com> Date: Mon, 9 Nov 2026 19:19:36 -0800 Subject: [PATCH 22/24] fix(shader): give this PR's recompiler change its own disk cache format, 81 #8939 now takes 80, above this PR's 67, so this PR takes 81, the lowest number above it that no open PR uses, and a cache written by another branch's recompiler is not read as this one's. --- core/shader/recompiler/ShaderDiskCache.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/shader/recompiler/ShaderDiskCache.hpp b/core/shader/recompiler/ShaderDiskCache.hpp index 0b6a0cbdeed9..353b54af5b6a 100644 --- a/core/shader/recompiler/ShaderDiskCache.hpp +++ b/core/shader/recompiler/ShaderDiskCache.hpp @@ -13,7 +13,7 @@ namespace ShaderRecompiler::ShaderDiskCache { -inline constexpr std::uint32_t FormatVersion = 80; +inline constexpr std::uint32_t FormatVersion = 81; enum class LoadStatus { Loaded, From b55705b34fe8e0a5e62b60e963c8a0ced9643450 Mon Sep 17 00:00:00 2001 From: Yunus Shaikh <75296019+EagleBearerYS@users.noreply.github.com> Date: Mon, 9 Nov 2026 19:19:50 -0800 Subject: [PATCH 23/24] fix(shader): give this PR's recompiler change its own disk cache format, 82 #9457 now takes 81, above this PR's 70, so this PR takes 82, the lowest number above it that no open PR uses, and a cache written by another branch's recompiler is not read as this one's. --- core/shader/recompiler/ShaderDiskCache.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/shader/recompiler/ShaderDiskCache.hpp b/core/shader/recompiler/ShaderDiskCache.hpp index 353b54af5b6a..ae3b60b6f6a2 100644 --- a/core/shader/recompiler/ShaderDiskCache.hpp +++ b/core/shader/recompiler/ShaderDiskCache.hpp @@ -13,7 +13,7 @@ namespace ShaderRecompiler::ShaderDiskCache { -inline constexpr std::uint32_t FormatVersion = 81; +inline constexpr std::uint32_t FormatVersion = 82; enum class LoadStatus { Loaded, From aec64b8636648bd2c162b68aea7c6b6ea7018093 Mon Sep 17 00:00:00 2001 From: Yunus Shaikh <75296019+EagleBearerYS@users.noreply.github.com> Date: Mon, 9 Nov 2026 19:20:08 -0800 Subject: [PATCH 24/24] fix(shader): give this PR's recompiler change its own disk cache format, 83 #9633 now takes 82, above this PR's 72, so this PR takes 83, the lowest number above it that no open PR uses, and a cache written by another branch's recompiler is not read as this one's. --- core/shader/recompiler/ShaderDiskCache.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/shader/recompiler/ShaderDiskCache.hpp b/core/shader/recompiler/ShaderDiskCache.hpp index ae3b60b6f6a2..1e7e98089cf2 100644 --- a/core/shader/recompiler/ShaderDiskCache.hpp +++ b/core/shader/recompiler/ShaderDiskCache.hpp @@ -13,7 +13,7 @@ namespace ShaderRecompiler::ShaderDiskCache { -inline constexpr std::uint32_t FormatVersion = 82; +inline constexpr std::uint32_t FormatVersion = 83; enum class LoadStatus { Loaded,