diff --git a/core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp b/core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp index f45f4ec9a397..24cc7bbc9171 100644 --- a/core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp @@ -2593,6 +2593,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); diff --git a/core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_image_table_tests.cmake b/core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_image_table_tests.cmake index b7b31a0c8e9c..8f22912a3e70 100644 --- a/core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_image_table_tests.cmake +++ b/core/libs/prx/libSceAgcDriver/tests/cmake/agc_driver_image_table_tests.cmake @@ -12,10 +12,20 @@ if(BUILD_TESTING) add_test(NAME agc_driver_pointer_phi_high_table COMMAND agc_driver_image_table_tests --pointer-phi-high) 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_phi_bit_field_table COMMAND agc_driver_image_table_tests --pointer-phi-bit-field) + add_test(NAME agc_driver_pointer_phi_mask_shift_table COMMAND agc_driver_image_table_tests --pointer-phi-mask-shift) + add_test(NAME agc_driver_pointer_phi_scalar_bfe_table COMMAND agc_driver_image_table_tests --pointer-phi-scalar-bfe) 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_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) add_test(NAME agc_driver_nested_image_table_shared_scan COMMAND agc_driver_image_table_tests --nested-shared-scan) - 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_nested_image_table_shared_scan 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_phi_bit_field_table agc_driver_pointer_phi_mask_shift_table agc_driver_pointer_phi_scalar_bfe_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_edited_table agc_driver_pointer_chase_words_table agc_driver_pointer_vsharp_fixed_table agc_driver_pointer_vsharp_fixed_offset_table agc_driver_nested_image_table_shared_scan 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_nested_image_table_shared_scan 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_phi_bit_field_table agc_driver_pointer_phi_mask_shift_table agc_driver_pointer_phi_scalar_bfe_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_edited_table agc_driver_pointer_chase_words_table agc_driver_pointer_vsharp_fixed_table agc_driver_pointer_vsharp_fixed_offset_table agc_driver_nested_image_table_shared_scan PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() endif() diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp index c0a4837bc51e..fff84679ccfb 100644 --- a/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/execution/ImageTable.cpp @@ -440,6 +440,26 @@ alignas(256) constexpr std::array PointerLaneNotCode{ 0x7e120202u, 0x34101286u, 0x4a101100u, 0x34101084u, 0xe0781000u, 0x80070408u, 0xbf810000u, }; +alignas(256) constexpr std::array PointerBitFieldCode{ + 0xf4080100u, 0xfa000000u, 0xf4080200u, 0xfa000010u, 0xf4080700u, 0xfa000020u, 0xf4080800u, 0xfa000030u, + 0x8f108502u, 0xf42c0404u, 0x20000000u, 0x360200bfu, 0x7e020d01u, 0x060202f0u, 0x100202ffu, 0x3c800000u, + 0x060202f2u, 0x7e0402ffu, 0x3e000000u, 0xbf8cc07fu, 0x7d8800a0u, 0x7e140210u, 0x02061411u, 0xbeae047eu, + 0x7e300503u, 0xd4c20030u, 0x00020618u, 0xbeb22430u, 0x8f198418u, 0xf4240b02u, 0x32000000u, 0xd4c10034u, + 0x00002900u, 0xbf8cc07fu, 0xbf068112u, 0xbf840003u, 0x902c8413u, 0x872cff2cu, 0x00000fffu, 0xf40c0916u, + 0xfa000040u, 0xbf8cc07fu, 0xf09c0f08u, 0x01090401u, 0x8afe3032u, 0xbf89ffeau, 0xbefe042eu, 0x7e120202u, + 0x34101286u, 0x4a101100u, 0x34101084u, 0xe0781000u, 0x80070408u, 0xbf810000u, +}; + +constexpr std::array PointerBitFieldVariant(std::uint32_t first, std::uint32_t second, std::uint32_t third) { + auto code = PointerBitFieldCode; + code[36] = first; + code[37] = second; + code[38] = third; + return code; +} + +alignas(256) constexpr std::array PointerMaskShiftCode = PointerBitFieldVariant(0x872cff13u, 0x00000fffu, 0x902c842cu); +alignas(256) constexpr std::array PointerScalarBfeCode = PointerBitFieldVariant(0x93acff13u, 0x000c0004u, 0xbf800000u); alignas(256) constexpr std::array PointerVsharpSelectCode{ 0xf4080100u, 0xfa000000u, 0xf4080200u, 0xfa000010u, 0xf4080700u, 0xfa000020u, 0xf4080800u, 0xfa000030u, 0x8f108502u, 0xf42c0404u, 0x20000000u, 0x360200bfu, 0x7e020d01u, 0x060202f0u, 0x100202ffu, 0x3c800000u, @@ -450,6 +470,76 @@ 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, +}; +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 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 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 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, + 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, +}; alignas(256) constexpr std::array NestedSiblingRecordCode{ 0xf4080100u, 0xfa000000u, 0xf4080200u, 0xfa000010u, 0xf4080700u, 0xfa000020u, 0xf4080800u, 0xfa000030u, @@ -546,6 +636,7 @@ struct Outcome { std::vector> samplers; std::vector> samplerTable; std::vector tableMap; + std::vector> imageTableRanges; std::vector>> imageTableReadRanges; }; @@ -573,6 +664,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; @@ -2178,7 +2270,7 @@ void RunPointerArmTests(AgcDriver::VulkanDevice& device, bool copies) { std::puts("pointer table with a second record read: a T# through a record pointer whose low word passes a second read keyed by another lane checked, by the pointer's value"); } -enum class PointerMask { LaneAnd, High, HighSecond, HighSecondLane, LaneNot }; +enum class PointerMask { LaneAnd, High, HighSecond, HighSecondLane, LaneNot, BitField, MaskShift, ScalarBfe }; void RunPointerMaskTests(AgcDriver::VulkanDevice& device, PointerMask shape) { Fill(); @@ -2188,16 +2280,18 @@ void RunPointerMaskTests(AgcDriver::VulkanDevice& device, PointerMask shape) { Keys = {}; const std::array images{Image(FloatTexels.data(), Format32Float), Image(OtherFloatTexels.data(), Format32Float)}; for (std::uint32_t record = 0; record < 2u; ++record) std::copy(images[record].begin(), images[record].end(), records[record].begin() + 16u); - const std::uint32_t pointerWord = shape == PointerMask::LaneNot ? 0u : 2u; + const bool bitField = shape == PointerMask::BitField || shape == PointerMask::MaskShift || shape == PointerMask::ScalarBfe; + const std::uint32_t pointerWord = shape == PointerMask::LaneNot || bitField ? 0u : 2u; + const std::uint64_t recordsAddress = bitField ? 0x7f0000001000ull : reinterpret_cast(records.data()); for (std::uint32_t record = 0; record < 3u; ++record) { - const auto pointer = reinterpret_cast(records[record % 2u].data()); + const auto pointer = recordsAddress + (record % 2u) * sizeof(records[0]); Table[record * 4u + pointerWord] = static_cast(pointer); Table[record * 4u + pointerWord + 1u] = static_cast(pointer >> 32u); } const auto secondBuffer = Buffer(second.data() + 8u, 32u); std::copy(secondBuffer.begin(), secondBuffer.end(), second.begin()); - std::span code = shape == PointerMask::LaneAnd ? std::span(PointerLaneAndCode) : shape == PointerMask::High ? std::span(PointerHighCode) : shape == PointerMask::LaneNot ? std::span(PointerLaneNotCode) : shape == PointerMask::HighSecondLane ? std::span(PointerHighSecondLaneCode) : std::span(PointerHighSecondCode); - const std::string name = shape == PointerMask::LaneAnd ? "pointer table with lane-mask ANDs" : shape == PointerMask::High ? "pointer table with a high-word Phi" : shape == PointerMask::LaneNot ? "pointer table with a lane bit cleared from a lane mask" : shape == PointerMask::HighSecondLane ? "pointer table with a second-buffer high word under a lane-mask low word" : "pointer table with a high word from a second buffer"; + std::span code = shape == PointerMask::MaskShift ? std::span(PointerMaskShiftCode) : shape == PointerMask::ScalarBfe ? std::span(PointerScalarBfeCode) : shape == PointerMask::BitField ? std::span(PointerBitFieldCode) : shape == PointerMask::LaneAnd ? std::span(PointerLaneAndCode) : shape == PointerMask::High ? std::span(PointerHighCode) : shape == PointerMask::LaneNot ? std::span(PointerLaneNotCode) : shape == PointerMask::HighSecondLane ? std::span(PointerHighSecondLaneCode) : std::span(PointerHighSecondCode); + const std::string name = shape == PointerMask::MaskShift ? "pointer table with a masked-then-shifted low word" : shape == PointerMask::ScalarBfe ? "pointer table with an s_bfe_u32 low word" : shape == PointerMask::BitField ? "pointer table with a bit-field low word" : shape == PointerMask::LaneAnd ? "pointer table with lane-mask ANDs" : shape == PointerMask::High ? "pointer table with a high-word Phi" : shape == PointerMask::LaneNot ? "pointer table with a lane bit cleared from a lane mask" : shape == PointerMask::HighSecondLane ? "pointer table with a second-buffer high word under a lane-mask low word" : "pointer table with a high word from a second buffer"; const auto run = [&] { const auto table = Buffer(Table.data(), 64u); const auto keys = Buffer(Keys.data(), static_cast(sizeof(Keys))); @@ -2215,7 +2309,7 @@ void RunPointerMaskTests(AgcDriver::VulkanDevice& device, PointerMask shape) { {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))}, + {recordsAddress, std::as_bytes(std::span(records))}, {secondAddress, std::as_bytes(std::span(second))}, }; const auto inputTable = Table; @@ -2240,7 +2334,7 @@ void RunPointerMaskTests(AgcDriver::VulkanDevice& device, PointerMask shape) { const auto taken = run(); Require(!Faulted(taken), name + ": 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 through the record's pointer in a waterfall loop"); - const bool recordsBelow4GiB = (reinterpret_cast(records.data()) >> 32u) == 0u; + const bool recordsBelow4GiB = (recordsAddress >> 32u) == 0u; const auto outside = [&](std::uint32_t selector, std::uint32_t arm, std::uint32_t variant, const std::string& what) { Keys[selector] = arm; Keys[5] = variant; @@ -2252,6 +2346,14 @@ void RunPointerMaskTests(AgcDriver::VulkanDevice& device, PointerMask shape) { Require(untouched(faulted), name + ": " + what + " published copied output"); Require(faulted.artifactId == taken.artifactId, name + ": " + what + " recompiled the shader"); }; + if (bitField) { + Keys[3] = 0x0abcdef0u; + outside(2u, 1u, 0u, "a bit field of a key word as the low word"); + Keys[3] = 0u; + outside(2u, 1u, 0u, "a zero bit field as the low word"); + std::puts(shape == PointerMask::MaskShift ? "pointer table with a masked-then-shifted low word: a T# through a record pointer whose low word passes a Phi with (key word & 0xfff) >> 4 checked, and the bit-field arm faults outside the snapshot" : shape == PointerMask::ScalarBfe ? "pointer table with an s_bfe_u32 low word: a T# through a record pointer whose low word passes a Phi with s_bfe_u32 (key word, offset 4, width 12) checked, and the bit-field arm faults outside the snapshot" : "pointer table with a bit-field low word: a T# through a record pointer whose low word passes a Phi with (key word >> 4) & 0xfff checked, and the bit-field arm faults outside the snapshot"); + return; + } outside(2u, 1u, 0u, "a constant low word"); if (shape == PointerMask::LaneAnd) { outside(2u, 2u, 0u, "a lane-mask Phi ANDed with a lane mask"); @@ -2358,6 +2460,568 @@ 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 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"); + 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]; + } + 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"); +} + +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 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"); +} + +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 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{}; + 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); @@ -2736,6 +3400,18 @@ int main(int argc, char** argv) { RunPointerMaskTests(*device, PointerMask::HighSecondLane); return 0; } + if (argc == 2 && std::strcmp(argv[1], "--pointer-phi-bit-field") == 0) { + RunPointerMaskTests(*device, PointerMask::BitField); + return 0; + } + if (argc == 2 && std::strcmp(argv[1], "--pointer-phi-mask-shift") == 0) { + RunPointerMaskTests(*device, PointerMask::MaskShift); + return 0; + } + if (argc == 2 && std::strcmp(argv[1], "--pointer-phi-scalar-bfe") == 0) { + RunPointerMaskTests(*device, PointerMask::ScalarBfe); + return 0; + } if (argc == 2 && std::strcmp(argv[1], "--pointer-phi-lane-not") == 0) { RunPointerMaskTests(*device, PointerMask::LaneNot); return 0; @@ -2744,6 +3420,34 @@ int main(int argc, char** argv) { RunPointerVsharpTests(*device); return 0; } + if (argc == 2 && std::strcmp(argv[1], "--pointer-hop") == 0) { + 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-chase-split") == 0) { + RunPointerChaseSplitTests(*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-chase-edited") == 0) { + RunPointerChaseEditedTests(*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; + } if (argc == 2 && std::strcmp(argv[1], "--nested-shared-scan") == 0) { RunNestedRecordTests(*device, NestedCase::SharedScan); 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 714c782bf4f3..0ff7dffe6ff6 100644 --- a/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ControlFlowInfo.hpp +++ b/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ControlFlowInfo.hpp @@ -35,6 +35,28 @@ struct PointerHop { bool operator==(const PointerHop& other) const = default; }; +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::uint32_t readHighImmediate = 0; + std::array edits {}; + + bool operator==(const PointerChase& other) const = default; +}; + struct TableColumn { std::uint32_t heapSource = 0; std::uint32_t stride = 0; @@ -51,6 +73,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 +82,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 f45c55004794..ca5928b9794a 100644 --- a/core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp +++ b/core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp @@ -1195,6 +1195,57 @@ class TableSnapshotter { parentScans.push_back({std::move(scan), records.base, records.size, 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] = chase.edits[dword].Apply(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.readHighImmediate, buffer.GetSize()); + 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); + } + + 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(); @@ -1217,38 +1268,18 @@ class TableSnapshotter { std::size_t slotCount = 16u; while (slotCount < static_cast(records.keys) * 2u) slotCount *= 2u; pointerSlots.assign(slotCount, {0u, NoRecord}); - 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); - auto slot = static_cast((pointer * 0x9e3779b97f4a7c15ull) >> 32u) & (slotCount - 1u); - while (pointerSlots[slot].second != NoRecord && pointerSlots[slot].first != pointer) slot = (slot + 1u) & (slotCount - 1u); - if (const auto seen = pointerSlots[slot].second; seen != NoRecord) { - records.states[record] = records.states[seen]; - records.words[record] = records.words[seen]; - if (column.buffer || column.pointerKeyed) records.pointers[record] = records.pointers[seen]; - continue; - } - pointerSlots[slot] = {pointer, record}; - 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(); @@ -1265,13 +1296,13 @@ class TableSnapshotter { const bool onePage = ((addresses[column.dwordCount - 1u] & ~(PageBytes - 1u)) == firstPage) && AddressAccessible(addresses[0]) && wholePages.find(firstPage)->second; for (std::uint32_t dword = onePage ? column.dwordCount : 0u; dword < column.dwordCount; dword++) { if (!AddressAccessible(addresses[dword])) { - records.states[record] = RecordState::Unmapped; + records.states[entry] = RecordState::Unmapped; break; } } indirectDword = 0u; accessibleChecks = 0u; - if (records.states[record] == RecordState::Read) { + if (records.states[entry] == RecordState::Read) { if (runtime.descriptorScope != nullptr) static_cast(runtime.descriptorScope(runtime.userContext, static_cast(column.dwordCount) * sizeof(std::uint32_t))); const bool ok = runtime.readWords(runtime.userContext, addresses[0], value.dwords.data(), column.dwordCount); if (runtime.descriptorScope != nullptr) static_cast(runtime.descriptorScope(runtime.userContext, 0u)); @@ -1285,19 +1316,42 @@ 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); + if (records.states[entry] != RecordState::Read) return; + 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); + auto slot = static_cast((pointer * 0x9e3779b97f4a7c15ull) >> 32u) & (slotCount - 1u); + while (pointerSlots[slot].second != NoRecord && pointerSlots[slot].first != pointer) slot = (slot + 1u) & (slotCount - 1u); + if (const auto seen = pointerSlots[slot].second; seen != NoRecord) { + records.states[record] = records.states[seen]; + records.words[record] = records.words[seen]; + if (column.buffer || column.pointerKeyed) records.pointers[record] = records.pointers[seen]; + continue; + } + pointerSlots[slot] = {pointer, record}; + 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 591a4f578139..a1ba7cdcaaa4 100644 --- a/core/shader/recompiler/Optimization/src/ResourceTracker.cpp +++ b/core/shader/recompiler/Optimization/src/ResourceTracker.cpp @@ -932,7 +932,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(SplitImmediateAddend(offset, addend), key, stride) && (address || !MatchFirstLaneOffset(offset, key, stride))) { return false; } @@ -940,7 +946,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; @@ -1098,6 +1106,20 @@ class Tracker { return !reads.empty(); } + static bool BitFieldWord(const IrValue& word) { + if (word.ArgumentCount() != 2u) return false; + std::uint32_t operand = 0; + if (word.Opcode() == IrOpcode::ShiftRightLogical32) { + const IrValue* masked = word.Argument(0)->Resolve(); + return immediateU32(word.Argument(1), operand) && operand != 0u && operand < 32u && masked->Opcode() == IrOpcode::BitwiseAnd32 && masked->ArgumentCount() == 2u && (immediateU32(masked->Argument(0), operand) || immediateU32(masked->Argument(1), operand)); + } + if (word.Opcode() != IrOpcode::BitwiseAnd32) return false; + const bool left = immediateU32(word.Argument(0), operand); + if (!left && !immediateU32(word.Argument(1), operand)) return false; + const IrValue* shifted = word.Argument(left ? 1u : 0u)->Resolve(); + return shifted->Opcode() == IrOpcode::ShiftRightLogical32 && shifted->ArgumentCount() == 2u && immediateU32(shifted->Argument(1), operand) && operand != 0u && operand < 32u; + } + static bool PointerFreeWord(const IrValue& word) { return (word.HasImmediate() && word.Type() == IrType::U32) || LaneMaskOperand(&word); } @@ -1119,37 +1141,168 @@ 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; - if (paired && lowWord->Argument(index)->Resolve()->HasImmediate() && edge->Resolve()->HasImmediate()) 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; + } + + 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 = EditedWord(handle.Argument(dword), chase.edits[dword]); + 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; + } + + 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; + 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 { + 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.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; + }; + 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) || BitFieldWord(*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; + if (paired && lowWord->Argument(index)->Resolve()->HasImmediate() && edge->Resolve()->HasImmediate()) 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; + } + 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; + } + 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(); } @@ -1415,10 +1568,12 @@ class Tracker { const bool buffer = handle.Opcode() == IrOpcode::GetBufferResource; const bool merged = pointer->Argument(0)->Resolve()->IsPhi() && UnwrapTableRead(pointer->Argument(0)) == nullptr; 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; + 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() : UnwrapTableRead(pointer->Argument(0)); if (low == nullptr) return false; auto* high = pointerKeyed ? pointer->Argument(1)->Resolve() : UnwrapTableRead(pointer->Argument(1)); @@ -1502,6 +1657,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 3b6ec9c66d38..377fde184f68 100644 --- a/core/shader/recompiler/ShaderDiskCache.cpp +++ b/core/shader/recompiler/ShaderDiskCache.cpp @@ -114,8 +114,8 @@ static_assert(sizeof(IrResourcePlan) == 728, "IrResourcePlan changed: update Enc static_assert(sizeof(IrValue) == 144, "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) == 224, "DescriptorSource changed: update the plan encoder"); -static_assert(sizeof(TableColumn) == 120, "TableColumn changed: update the plan encoder"); +static_assert(sizeof(DescriptorSource) == 248, "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"); @@ -155,7 +155,7 @@ static_assert(fieldCount() == 25, "IrResourcePlan changed: updat static_assert(fieldCount() == 2, "GuestRegister changed: update the plan value encoder"); static_assert(fieldCount() == 32, "MemoryInfo changed: update the plan encoder"); static_assert(fieldCount() == 4, "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"); @@ -1238,6 +1238,19 @@ 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); + chases.Value(chase.readHighImmediate); + 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); out.Value(column.foldedCarry); out.Value(column.selected); @@ -1405,6 +1418,19 @@ bool decodePlan(Reader& reader, IrResourcePlan& plan) { hops.Value(hop.offset); hops.Value(hop.immediate); }); + 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); + chases.Value(chase.readHighImmediate); + 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); in.Value(column.foldedCarry); in.Value(column.selected); @@ -1453,6 +1479,9 @@ 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::any_of(column.pointerChases, [](const PointerChase& chase) { return chase.readHighImmediate % sizeof(std::uint32_t) != 0u || chase.readHighImmediate == chase.readImmediate; })) 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/core/shader/recompiler/ShaderDiskCache.hpp b/core/shader/recompiler/ShaderDiskCache.hpp index c05773ee146a..43dfa71e9d4a 100644 --- a/core/shader/recompiler/ShaderDiskCache.hpp +++ b/core/shader/recompiler/ShaderDiskCache.hpp @@ -17,7 +17,7 @@ struct NanSpeculationConfig; namespace ShaderRecompiler::ShaderDiskCache { -inline constexpr std::uint32_t FormatVersion = 98; +inline constexpr std::uint32_t FormatVersion = 99; enum class LoadStatus { Loaded, diff --git a/docs/dev/debt/shader.md b/docs/dev/debt/shader.md index 9dc256475b43..ed6cd942ee2f 100644 --- a/docs/dev/debt/shader.md +++ b/docs/dev/debt/shader.md @@ -84,6 +84,8 @@ - [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; parent columns that read the same records and differ only in where their V# sits behind the record's pointer are charged for those records once. It 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. A T# reached through a nested child is materialized from its raw words, without registered `resourceViews`, so a 1D array past `maxImageArrayLayers` keeps its raw descriptor there. 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 real record pointer derived by a shift and a mask is no longer refused at compile time in a pointer-keyed [image table](../../../core/shader/recompiler/Optimization/src/ResourceTracker.cpp): the walk skips an edge whose low word is a bit field of any word, `(x >> s) & m` or `(x & m) >> s` with constant `s` from 1 to 31 and constant `m`, together with its high word. The skipped edge contributes no capture candidate; the caller still needs the other edges' record reads, so the change can replace a compile-time refusal only when otherwise valid record candidates remain. At run time a dispatch that selects an uncaptured non-null pointer faults "outside the snapshot" unless a table fault takes precedence. A null pointer follows the existing lookup: a matching captured entry is used; otherwise `TableOutsideCode` gives an `unmapped` fault, with a table fault taking precedence. A bit field as the high word paired with a record-read low word stays refused. +- 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, and so does a table read at `(key << n) & mask` with such a key. 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 128 images or 256 sampled image/sampler pairs binds, for the heaps with the most such reads, the union of their keys in one table instead. - A shader plans at most 64 [samplers](../../../core/shader/recompiler/Optimization/src/ResourceTracker.cpp) (`ShaderInfo::MaxSamplers`, the size of the runtime ABI's `ShaderData` sampler metadata, which [`BindingAllocator`](../../../core/shader/recompiler/Optimization/src/BindingAllocator.cpp) holds every shader to), at most 32 of them direct (each takes two elements of the 64-element sampler heap), and at most 256 image/sampler pairs (`ShaderInfo::MaxSampledPairs`); past them it refuses with `sampler resource limit exceeded`, `shader sampler pairs exceed runtime heap capacity` or `sampled image/sampler pair limit exceeded`. The image-to-sampler masks are 64-bit, one bit per sampler heap element or per sampler, so more samplers need a larger `ShaderData` and wider masks. Ratchet & Clank: Rift Apart (PPSA01474) plans up to 63 samplers and 112 pairs in one shader. - [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.