diff --git a/core/libs/prx/libSceAgcDriver/CMakeLists.txt b/core/libs/prx/libSceAgcDriver/CMakeLists.txt index 73a58caafdd7..3fd28395a4d1 100644 --- a/core/libs/prx/libSceAgcDriver/CMakeLists.txt +++ b/core/libs/prx/libSceAgcDriver/CMakeLists.txt @@ -339,6 +339,11 @@ target_link_libraries(agc_shared_write_tests PRIVATE libSceAgcDriver libkernel l set_target_properties(agc_shared_write_tests PROPERTIES CXX_EXTENSIONS OFF) configure_windows_unwind(agc_shared_write_tests) +add_test_executable(agc_srt_execution_tests EXCLUDE_FROM_ALL tests/SrtExecution.cpp) +target_link_libraries(agc_srt_execution_tests PRIVATE shader_recompiler_agc_driver libc) +set_target_properties(agc_srt_execution_tests PROPERTIES CXX_EXTENSIONS OFF) +configure_windows_unwind(agc_srt_execution_tests) + add_test_executable(agc_write_tracking_tests EXCLUDE_FROM_ALL tests/WriteTracking.cpp Execution/src/GuestMemory.cpp @@ -1037,7 +1042,7 @@ if(BUILD_TESTING) if(WIN32) set_tests_properties(agc_shader_bindless agc_shader_srt_read_patching agc_shader_frontend_pair agc_shader_frontend_pair_off PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") endif() - foreach(agcTest IN ITEMS agc_shared_write_tests agc_driver_tests agc_shader_memory_tests agc_structurizer_tests agc_spirv_module_tests agc_dpp_write_tests agc_masked_select_tests agc_vector_window_tests agc_constant_bit_test_tests agc_scalar_select_mask_tests agc_vop1_control_decode_tests agc_validate_program_tests agc_pass_hazards_tests agc_shader_disk_cache_tests agc_shader_device_profile_tests agc_placeholder_images_tests agc_shader_control_flow_tests agc_driver_flip_tests agc_driver_pm4_tests agc_driver_graphics_tests agc_driver_bda_device_tests agc_driver_mesh_tests agc_driver_cmask_fast_clear_tests agc_formatted_buffer_once_tests ${agcExecutionTests}) + foreach(agcTest IN ITEMS agc_shared_write_tests agc_driver_tests agc_shader_memory_tests agc_structurizer_tests agc_spirv_module_tests agc_dpp_write_tests agc_masked_select_tests agc_vector_window_tests agc_constant_bit_test_tests agc_scalar_select_mask_tests agc_vop1_control_decode_tests agc_validate_program_tests agc_pass_hazards_tests agc_shader_disk_cache_tests agc_shader_device_profile_tests agc_placeholder_images_tests agc_shader_control_flow_tests agc_driver_flip_tests agc_driver_pm4_tests agc_driver_graphics_tests agc_driver_bda_device_tests agc_driver_mesh_tests agc_driver_cmask_fast_clear_tests agc_formatted_buffer_once_tests ${agcExecutionTests} agc_srt_execution_tests) if(agcTest STREQUAL "agc_driver_constant_store_tests" OR agcTest STREQUAL "agc_driver_flat_apertures_tests" OR agcTest STREQUAL "agc_driver_pipeline_optimized_readback_tests") continue() endif() @@ -1050,6 +1055,7 @@ if(BUILD_TESTING) "PATH=path_list_prepend:$") endif() endforeach() + set_tests_properties(agc_srt_execution PROPERTIES TIMEOUT 30) add_test(NAME agc_driver_emulated_color_compare_srgb_decode COMMAND agc_driver_emulated_color_compare_tests) set_tests_properties(agc_driver_emulated_color_compare_srgb_decode PROPERTIES ENVIRONMENT "ANYPS5_NO_SHADER_CACHE=1;APS5_SRGB_SHADER_DECODE=1" diff --git a/core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp b/core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp index 2a86162fae97..1b1ac12ff692 100644 --- a/core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp @@ -3184,6 +3184,7 @@ int main(int argc, char** argv) { require(!first.spirv.empty(), "empty compiled shader"); require(!first.cacheHit, "first shader compilation unexpectedly hit the cache"); const auto plan = GetResourcePlan(request); + require(!plan->guardedSrtSlots.empty() && plan->executionPlan == nullptr, "guarded scalar-address reads bypassed generic evaluation"); require(plan == GetResourcePlan(request), "resource plan was rebuilt"); const auto cached = Recompile(request); require(cached.cacheHit, "unchanged shader did not hit the cache"); diff --git a/core/libs/prx/libSceAgcDriver/tests/SrtExecution.cpp b/core/libs/prx/libSceAgcDriver/tests/SrtExecution.cpp new file mode 100644 index 000000000000..7e23b55fb3ff --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/SrtExecution.cpp @@ -0,0 +1,333 @@ +#include "Optimization/SrtWalker/SrtExecutionPlan.hpp" +#include "Optimization/ResourceProgram.hpp" +#include "BdaAbi.hpp" +#include +#include +#include "Optimization/SrtWalker/SrtDescriptorEvaluation.hpp" +#include "Optimization/SrtWalker/SrtFlatSlotClasses.hpp" +#include +#include +#include +#include +#include +#include +#include + +namespace { + +using namespace ShaderRecompiler; + +void require(bool value, const char* message) { + if (!value) throw std::runtime_error(message); +} + +struct Fixture { + IrResourcePlan plan; + + IrValue& Make(IrOpcode op, IrType type, std::initializer_list arguments = {}) { + require(arguments.size() == IrOpcodeOperandCount(op), "fixture operands disagree with the IR instruction schema"); + auto value = std::make_unique(op, type, static_cast(plan.valueStorage.size())); + for (auto* argument : arguments) value->AddArgument(argument); + plan.valueStorage.push_back(std::move(value)); + return *plan.valueStorage.back(); + } + + IrValue& Constant(std::uint32_t value) { + auto& constant = Make(IrOpcode::Void, IrType::U32); + constant.SetImmediateU32(value); + return constant; + } + + IrValue& User(std::uint32_t index) { + auto& reg = Make(IrOpcode::Void, IrType::ScalarReg); + reg.SetRegister({RegisterBank::Scalar, index + 8}); + return Make(IrOpcode::GetUserData, IrType::U32, {®}); + } + + explicit Fixture(bool buffer, std::uint32_t descriptors = 8) { + plan.userDataBase = 8; + plan.srtPlanComplete = true; + plan.resourceTrackingComplete = true; + MemoryInfo memory{}; + memory.kind = buffer ? ResourceKind::ScalarBuffer : ResourceKind::ScalarAddress; + plan.memoryInfo.push_back(memory); + auto& low = User(0); + auto& high = User(1); + auto& offset = User(2); + auto& handle = buffer ? Make(IrOpcode::GetBufferResource, IrType::BufferResource, {&low, &high, &User(3), &User(4)}) : Make(IrOpcode::GetAddressResource, IrType::AddressResource, {&low, &high}); + auto& srt = Make(IrOpcode::GetSrtResource, IrType::SrtResource); + auto& active = Make(IrOpcode::Void, IrType::Bool); + active.SetImmediateBool(true); + for (std::uint32_t i = 0; i < descriptors * 8 + 8; ++i) { + auto& relative = Make(IrOpcode::IAdd32, IrType::U32, {&offset, &Constant(i * 4)}); + auto& read = buffer ? Make(IrOpcode::ReadConstBuffer, IrType::U32, {&handle, &relative}) : Make(IrOpcode::LoadAddressU32, IrType::U32, {&handle, &relative, &Constant(0), &active}); + plan.srtReads.push_back({&read, i}); + if (i >= descriptors * 8) continue; + if (i % 8 == 0) { + plan.materializationSources.push_back(static_cast(plan.descriptorSources.size())); + plan.descriptorSources.push_back({}); + plan.descriptorSources.back().dwordCount = 8; + } + plan.descriptorSources.back().dwords[i % 8] = &Make(IrOpcode::ReadConst, IrType::U32, {&srt, &Constant(i)}); + } + plan.pureFlatSlots = Detail::ComputePureFlatSlots(plan); + plan.executionPlan = Detail::CompileSrtExecutionPlan(plan); + require(plan.executionPlan != nullptr, "ordinary descriptor expressions did not compile"); + } +}; + +struct Memory { + std::uint64_t reject = ~std::uint64_t{0}; + std::vector reads; + std::uint64_t inaccessible = ~std::uint64_t{0}; + + static bool Accessible(void* context, std::uint64_t address, std::uint64_t) { + return address != static_cast(context)->inaccessible; + } + bool record = true; + + static bool Read(void* context, std::uint64_t address, std::uint32_t* word) { + auto& memory = *static_cast(context); + if (memory.record) memory.reads.push_back(address); + if (address == memory.reject) return false; + *word = static_cast((address * 0x9e3779b9u) ^ (address >> 32u)); + return true; + } +}; + +void compare(Fixture& fixture, SrtRuntime runtime, bool expected = true, std::size_t expectedPoison = 0, std::uint32_t expectedNullRoots = 0, bool reportPoison = true, bool reportNullRoots = true) { + auto& memory = *static_cast(runtime.userContext); + const auto compiled = std::exchange(fixture.plan.executionPlan, nullptr); + std::vector reference, actual; + std::vector referenceFlat, actualFlat; + std::vector referenceActive, actualActive; + SrtReadTrace referenceTrace, actualTrace; + std::vector referencePoison{{~0u, ~0u, ~std::uint64_t{0}}}, actualPoison = referencePoison; + std::uint32_t referenceNullRoots = ~0u, actualNullRoots = ~0u; + runtime.readTrace = &referenceTrace; + memory.reads.clear(); + const bool referenceOk = Detail::EvaluateRuntimeSourcesImpl(fixture.plan, fixture.plan.materializationSources, runtime, reference, referenceFlat, true, {}, referenceActive, reportPoison ? &referencePoison : nullptr, reportNullRoots ? &referenceNullRoots : nullptr); + const auto reads = std::move(memory.reads); + fixture.plan.executionPlan = compiled; + runtime.readTrace = &actualTrace; + const bool actualOk = Detail::EvaluateRuntimeSourcesImpl(fixture.plan, fixture.plan.materializationSources, runtime, actual, actualFlat, true, {}, actualActive, reportPoison ? &actualPoison : nullptr, reportNullRoots ? &actualNullRoots : nullptr); + require(referenceOk == expected && actualOk == expected, "compiled evaluation changed success or failure"); + require(reference == actual && referenceFlat == actualFlat && referenceActive == actualActive, "compiled evaluation changed descriptors, flat values or active sources"); + require(referencePoison == actualPoison && referenceNullRoots == actualNullRoots, "compiled evaluation changed poison or null-root reporting"); + if (expected && reportPoison) require(actualPoison.size() == expectedPoison, "unexpected poisoned-read count"); + if (expected && expectedPoison == 1) require(actualPoison[0] == SrtReadPoison{8, 0x40, memory.inaccessible} && actualFlat[8] == 0, "poison did not retain its slot, PC, address and zero value"); + if (expected && reportNullRoots) require(actualNullRoots == expectedNullRoots, "unexpected null-root count"); + if (expectedNullRoots != 0) { + require(memory.reads.empty() && actualTrace.leaves.empty() && actualTrace.otherReads.empty(), "null-root evaluation read memory or recorded dependencies"); + require(std::ranges::all_of(actualFlat, [](auto word) { return word == 0; }), "null-root flat slots were not zero"); + } + require(reads == memory.reads, "compiled evaluation changed guest read order"); + require(referenceTrace.leaves == actualTrace.leaves && referenceTrace.otherReads == actualTrace.otherReads, "compiled evaluation changed read dependency classification"); +} + +void correctness() { + Memory memory; + std::array user{0x1000, 0, 0, 4096, 0}; + SrtRuntime runtime{user, 0x3000, Memory::Read, &memory}; + runtime.accessible = Memory::Accessible; + for (const bool buffer : {false, true}) { + Fixture fixture(buffer); + for (std::uint32_t iteration = 0; iteration < 128; ++iteration) { + user[0] = 0x1000 + iteration * 1024; + user[1] = iteration * 0x4000001u; + user[2] = iteration % 32; + compare(fixture, runtime); + } + user = {0x1000, 0, 0, 4096, 0}; + memory.reject = 0x1014; + compare(fixture, runtime, false); + memory.reject = ~std::uint64_t{0}; + fixture.plan.memoryInfo[0].offset = static_cast(-8); + fixture.plan.executionPlan = Detail::CompileSrtExecutionPlan(fixture.plan); + compare(fixture, runtime, !buffer); + user[0] = 4; + compare(fixture, runtime, false); + fixture.plan.memoryInfo[0].offset = 0; + fixture.plan.executionPlan = Detail::CompileSrtExecutionPlan(fixture.plan); + user = {0x1000, 0, 0, 8, 0}; + compare(fixture, runtime); + user = {0, 0, 0, 4096, 0}; + compare(fixture, runtime, true, 0, static_cast(fixture.plan.srtReads.size())); + compare(fixture, runtime, true, 0, 0, false, false); + compare(fixture, runtime, true, 0, 0, false, true); + compare(fixture, runtime, true, 0, 0, true, false); + user = {0x1000, 0, 0, 4096, 0}; + fixture.plan.guardedSrtSlots = {0}; + require(Detail::CompileSrtExecutionPlan(fixture.plan) == nullptr, "guarded flat slots selected compiled evaluation"); + compare(fixture, runtime); + fixture.plan.guardedSrtSlots.clear(); + fixture.plan.cleanFlatSlots = {1}; + require(Detail::CompileSrtExecutionPlan(fixture.plan) == nullptr, "specialized memory selected the ordinary execution plan"); + fixture.plan.cleanFlatSlots.clear(); + fixture.plan.controlFlow.push_back({&fixture.Constant(1), {}, {}}); + require(Detail::CompileSrtExecutionPlan(fixture.plan) == nullptr, "dynamic control flow selected the unconditional execution plan"); + fixture.plan.controlFlow = {{nullptr, {0}, {0}}, {nullptr, {}, {1}}}; + fixture.plan.executionPlan = Detail::CompileSrtExecutionPlan(fixture.plan); + user = {0x1000, 0, 0, 4096, 0}; + compare(fixture, runtime); + } + Fixture nested(false, 1); + auto& first = *nested.plan.srtReads[0].value; + auto& next = *nested.plan.srtReads[8].value; + auto& address = nested.Make(IrOpcode::GetAddressResource, IrType::AddressResource, {&first, &nested.Constant(0)}); + next.ReplaceArgument(0, &address); + nested.plan.pureFlatSlots = Detail::ComputePureFlatSlots(nested.plan); + nested.plan.executionPlan = Detail::CompileSrtExecutionPlan(nested.plan); + require(nested.plan.executionPlan == nullptr, "nested memory handle selected compiled evaluation"); + nested.plan.guardedSrtSlots = Detail::ComputeGuardedFlatSlots(nested.plan); + require(!nested.plan.guardedSrtSlots.empty(), "nested read has no guard slots"); + compare(nested, runtime); + std::uint32_t pointer = 0; + require(Memory::Read(&memory, user[0], &pointer), "fixture pointer read failed"); + memory.inaccessible = (pointer & ~std::uint64_t{3}) + 32; + auto flags = next.Flags(); + flags.pc = 0x40; + next.SetFlags(flags); + compare(nested, runtime, true, 1); + memory.inaccessible = ~std::uint64_t{0}; + nested.plan.guardedSrtSlots.clear(); + next.ReplaceArgument(0, &nested.Make(IrOpcode::GetAddressResource, IrType::AddressResource, {&next, &nested.Constant(0)})); + require(Detail::CompileSrtExecutionPlan(nested.plan) == nullptr, "cyclic expression was compiled"); + for (const bool buffer : {false, true}) { + Fixture malformed(buffer, 1); + malformed.plan.srtReads.front().value->AddArgument(&malformed.Constant(0)); + require(Detail::CompileSrtExecutionPlan(malformed.plan) == nullptr, "malformed memory operands selected compiled evaluation"); + } + Fixture generalAddress(false, 1); + generalAddress.plan.memoryInfo[0].kind = ResourceKind::Global; + require(Detail::CompileSrtExecutionPlan(generalAddress.plan) == nullptr, "non-scalar memory selected scalar descriptor evaluation"); + Fixture large(false, 32); + compare(large, runtime); + Fixture arithmetic(false, 1); + arithmetic.plan.descriptorSources[0].dwordCount = 1; + arithmetic.plan.srtReads.resize(1); + auto* expression = &arithmetic.Make(IrOpcode::CompositeConstructU64, IrType::U64, {&arithmetic.User(0), &arithmetic.User(1)}); + auto& operand = arithmetic.User(2); + for (const auto op : {IrOpcode::IAdd32, IrOpcode::IAdd64, IrOpcode::ISub32, IrOpcode::ISub64, IrOpcode::IMul32, IrOpcode::IMul64, IrOpcode::UMin32, IrOpcode::BitwiseAnd32, IrOpcode::BitwiseAnd64, IrOpcode::BitwiseOr32, IrOpcode::BitwiseXor32, IrOpcode::ShiftLeftLogical32, IrOpcode::ShiftLeftLogical64, IrOpcode::ShiftRightLogical32, IrOpcode::ShiftRightLogical64}) { + expression = &arithmetic.Make(op, IrType::U64, {expression, &operand}); + arithmetic.plan.srtReads[0].value = expression; + arithmetic.plan.executionPlan = Detail::CompileSrtExecutionPlan(arithmetic.plan); + require(arithmetic.plan.executionPlan != nullptr, "integer expression did not compile"); + for (const auto word : {0u, 1u, 31u, 32u, 63u, 64u, 0x80000000u, 0xffffffffu}) { + user = {word, ~word, word, 4096, 0}; + compare(arithmetic, runtime); + } + } + for (const auto op : {IrOpcode::CompositeExtractU64, IrOpcode::CompositeExtractU32x2}) { + auto& source = arithmetic.Make(op == IrOpcode::CompositeExtractU64 ? IrOpcode::CompositeConstructU64 : IrOpcode::IAddCarry32, IrType::U64, {&arithmetic.User(0), &arithmetic.User(1)}); + for (std::uint32_t component = 0; component < 2; ++component) { + arithmetic.plan.srtReads[0].value = &arithmetic.Make(op, IrType::U32, {&source, &arithmetic.Constant(component)}); + arithmetic.plan.executionPlan = Detail::CompileSrtExecutionPlan(arithmetic.plan); + require(arithmetic.plan.executionPlan != nullptr, "packed arithmetic did not compile"); + user = {0xffffffffu, 1u, 0, 4096, 0}; + compare(arithmetic, runtime); + } + } + arithmetic.plan.srtReads[0].value = &arithmetic.Make(IrOpcode::BitwiseNot32, IrType::U32, {&arithmetic.User(0)}); + arithmetic.plan.executionPlan = Detail::CompileSrtExecutionPlan(arithmetic.plan); + compare(arithmetic, runtime); + arithmetic.plan.srtReads[0].value = &arithmetic.Make(IrOpcode::SelectU32, IrType::U32, {&arithmetic.User(0), &arithmetic.User(1), &arithmetic.User(2)}); + require(Detail::CompileSrtExecutionPlan(arithmetic.plan) == nullptr, "unsupported expression bypassed the generic evaluator"); + std::cout << "Compiled SRT values, read ordering, dependencies, failures, null roots, poison, guards, cycles and generic fallback passed\n"; +} + +void prepared(bool benchmark) { + const std::array code{0xf4000084u, 0xfa000000u, 0x7e000202u, 0xf80008cfu, 0u, 0xbf810000u}; + const std::array capabilities{spv::CapabilityShader, spv::CapabilityImageGatherExtended, spv::CapabilityInt64, spv::CapabilityPhysicalStorageBufferAddresses, spv::CapabilityStorageBuffer8BitAccess}; + const std::array extensions{"SPV_KHR_physical_storage_buffer", "SPV_KHR_8bit_storage"}; + std::array user{0x1000, 0}; + RecompileRequest request{}; + request.shader = {ShaderStage::Vertex, 0x10000u, code, 0, {}}; + request.target.bdaAbiVersion = BdaAbi::Version; + request.target.supportedCapabilities = capabilities; + request.target.supportedExtensions = extensions; + request.target.vulkanVersion = 0x00401000u; + request.target.spirvVersion = 0x00010300u; + request.target.subgroupSize = 64; + request.context.waveSize = 64; + request.context.userDataBaseRegister = 8; + request.context.userData = user; + request.context.vertex = ShaderVertexStageInfo{}; + request.layout.pushConstantSizeBytes = 128; + const auto handle = PrepareShader(request); + const auto invocation = PreparedShaderInvocation::TryCreate(request, handle); + require(invocation.has_value(), "translated shader did not create a prepared invocation"); + Memory memory; + SrtRuntime runtime{user, request.shader.codeAddress, Memory::Read, &memory}; + runtime.accessible = Memory::Accessible; + const auto capture = invocation->Capture(runtime); + require(capture->plan->executionPlan != nullptr, "prepared root-only shader has no compiled plan"); + require(capture->snapshot.flattenedSrt.size() == 1 && capture->snapshot.flattenedSrt[0] == 0x779b9000u, "prepared root read returned the wrong word"); + user[0] = 0; + memory.reads.clear(); + const auto nullCapture = invocation->Capture(runtime); + require(nullCapture->snapshot.nullRootReads == 1 && nullCapture->snapshot.srtPoison.empty() && nullCapture->snapshot.flattenedSrt[0] == 0 && memory.reads.empty(), "prepared null root lost its reporting or touched memory"); + if (!benchmark) return; + memory.record = false; + std::vector samples; + std::uint64_t checksum = 0; + for (unsigned pass = 0; pass < 10; ++pass) { + const auto started = std::chrono::steady_clock::now(); + for (unsigned draw = 0; draw < 4096; ++draw) { + user[0] = 0x1000 + (draw % 32) * 4096; + checksum += invocation->Capture(runtime)->snapshot.flattenedSrt[0]; + } + if (pass != 0) samples.push_back(std::chrono::duration(std::chrono::steady_clock::now() - started).count() / 4096); + } + std::ranges::sort(samples); + std::cout << "Prepared capture (" << capture->plan->materializationSources.size() << " sources, " << capture->plan->srtReads.size() << " reads), " + << (std::getenv("APS5_NO_COMPILED_SRT") ? "generic" : "compiled") << ": median " << samples[4] << " us, p95 " << samples.back() << " us, checksum " << checksum << '\n'; +} + +void benchmark() { + for (const bool buffer : {false, true}) { + Fixture fixture(buffer); + const auto compiled = fixture.plan.executionPlan; + Memory memory; + memory.record = false; + std::array user{0x1000, 0, 0, 4096, 0}; + SrtRuntime runtime{user, 0x3000, Memory::Read, &memory}; + std::array, 2> samples; + std::uint64_t checksum = 0; + for (unsigned pass = 0; pass < 10; ++pass) { + for (unsigned step = 0; step < 2; ++step) { + const auto mode = (pass + step) % 2; + fixture.plan.executionPlan = mode ? compiled : nullptr; + const auto started = std::chrono::steady_clock::now(); + for (unsigned draw = 0; draw < 4096; ++draw) { + user[0] = 0x1000 + (draw % 32) * 4096; + std::vector values; + std::vector flat; + std::vector active; + std::vector poison; + std::uint32_t nullRoots = 0; + if (!Detail::EvaluateRuntimeSourcesImpl(fixture.plan, fixture.plan.materializationSources, runtime, values, flat, true, {}, active, &poison, &nullRoots)) throw std::runtime_error("benchmark evaluation failed"); + checksum += values[0].dwords[0] + flat.back(); + } + if (pass != 0) samples[mode].push_back(std::chrono::duration(std::chrono::steady_clock::now() - started).count() / 4096); + } + } + for (auto& sample : samples) std::ranges::sort(sample); + std::cout << (buffer ? "Scalar buffer" : "Scalar address") << " SRT: generic " << samples[0][4] << " us, compiled " << samples[1][4] << " us, p95 " << samples[0].back() << '/' << samples[1].back() << " us, checksum " << checksum << '\n'; + } +} + +} + +int main(int argc, char** argv) { + try { + const bool preparedBenchmark = argc == 2 && std::string_view(argv[1]) == "--prepared-benchmark"; + prepared(preparedBenchmark); + if (!preparedBenchmark) correctness(); + if (argc == 2 && std::string_view(argv[1]) == "--benchmark") benchmark(); + return 0; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; + } +} diff --git a/core/shader/recompiler/CMakeLists.txt b/core/shader/recompiler/CMakeLists.txt index 88aafc8fe04f..9183f6ed1c04 100644 --- a/core/shader/recompiler/CMakeLists.txt +++ b/core/shader/recompiler/CMakeLists.txt @@ -112,6 +112,7 @@ function(add_shader_recompiler target runtimeTarget) ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/Optimization/src/SrtWalker/SrtDescriptorEvaluation.cpp ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/Optimization/src/SrtWalker/SrtDiagnostics.cpp ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/Optimization/src/SrtWalker/SrtEvaluator.cpp + ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/Optimization/src/SrtWalker/SrtExecutionPlan.cpp ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/Optimization/src/SrtWalker/SrtFlatSlotClasses.cpp ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/Optimization/src/SrtWalker/SrtInstructionPredicates.cpp ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/Optimization/src/SrtWalker/SrtPlanBuilder.cpp diff --git a/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ResourcePlan.hpp b/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ResourcePlan.hpp index cb9565fb4fa3..2f19ef562ec6 100644 --- a/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ResourcePlan.hpp +++ b/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ResourcePlan.hpp @@ -15,6 +15,8 @@ namespace ShaderRecompiler { +namespace Detail { struct SrtExecutionPlan; } + struct DescriptorValue { std::array dwords = {}; std::uint32_t dwordCount = 0; @@ -72,6 +74,7 @@ struct IrResourcePlan { // One byte per srtReads slot, 1 when the CPU walk never consumes the slot's value (see // Detail::ComputePureFlatSlots): a driver may reuse a capture whose words differ only there. std::vector pureFlatSlots; + std::shared_ptr executionPlan; bool requiresSpecializationMemory = false; bool srtPlanComplete = false; bool resourceTrackingComplete = false; diff --git a/core/shader/recompiler/Optimization/include/Optimization/SrtWalker/SrtExecutionPlan.hpp b/core/shader/recompiler/Optimization/include/Optimization/SrtWalker/SrtExecutionPlan.hpp new file mode 100644 index 000000000000..1a7746b82eae --- /dev/null +++ b/core/shader/recompiler/Optimization/include/Optimization/SrtWalker/SrtExecutionPlan.hpp @@ -0,0 +1,35 @@ +#ifndef CORE_SHADER_RECOMPILER_OPTIMIZATION_SRTWALKER_SRTEXECUTIONPLAN_HPP +#define CORE_SHADER_RECOMPILER_OPTIMIZATION_SRTWALKER_SRTEXECUTIONPLAN_HPP + +#include "Optimization/SrtWalker.hpp" +#include +#include +#include + +namespace ShaderRecompiler::Detail { + +struct SrtExecutionPlan { + struct Operation { + IrOpcode opcode = IrOpcode::Void; + std::array arguments{}; + std::uint64_t immediate = 0; + std::uint32_t leafSlot = ~0u; + }; + struct Descriptor { + std::array words{}; + std::uint32_t count = 0; + }; + + std::vector operations; + std::vector descriptors; + std::vector flat; + std::vector active; + + bool Evaluate(const SrtRuntime& runtime, std::vector& results, std::vector& flattened, std::vector& activeSources, std::vector* poison, std::uint32_t* nullRootReads) const; +}; + +std::shared_ptr CompileSrtExecutionPlan(const IrResourcePlan& program); + +} + +#endif diff --git a/core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp b/core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp index 53118b192a2a..5f8f38078e81 100644 --- a/core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp +++ b/core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp @@ -1,5 +1,6 @@ #include "Optimization/ResourceMaterializer.hpp" #include "Optimization/SrtWalker/SrtFlatSlotClasses.hpp" +#include "Optimization/SrtWalker/SrtExecutionPlan.hpp" #include "Optimization/ShaderStageInputInfo.hpp" #include "RdnaDecoder/RdnaDescriptorFormat.hpp" #include "RdnaDecoder/RdnaImageOpDecoder.hpp" @@ -999,6 +1000,7 @@ IrResourcePlan ResourceMaterializer::ExtractPlan(const IrProgram& program) const for (const auto& sampler : plan.info.samplers) addSource(sampler.source); plan.pureFlatSlots = Detail::ComputePureFlatSlots(plan); plan.guardedSrtSlots = Detail::ComputeGuardedFlatSlots(plan); + plan.executionPlan = Detail::CompileSrtExecutionPlan(plan); return plan; } diff --git a/core/shader/recompiler/Optimization/src/SrtWalker/SrtDescriptorEvaluation.cpp b/core/shader/recompiler/Optimization/src/SrtWalker/SrtDescriptorEvaluation.cpp index bc89e71e9e03..09888fced4c0 100644 --- a/core/shader/recompiler/Optimization/src/SrtWalker/SrtDescriptorEvaluation.cpp +++ b/core/shader/recompiler/Optimization/src/SrtWalker/SrtDescriptorEvaluation.cpp @@ -1,9 +1,11 @@ #include "Optimization/SrtWalker/SrtDescriptorEvaluation.hpp" #include "Optimization/SrtWalker/SrtEvaluator.hpp" +#include "Optimization/SrtWalker/SrtExecutionPlan.hpp" #include "prx/libc/include/HostThreadLocal.hpp" #include #include +#include #include #include #include @@ -45,7 +47,7 @@ const DescriptorSource* Source(const IrResourcePlan& program, std::uint32_t sour } -bool EvaluateRuntimeSourcesImpl(const IrResourcePlan& program, std::span sources, const SrtRuntime& runtime, std::vector& results, std::vector& flat, bool evaluateFlat, std::span cleanFlatSlots, std::vector& activeSources, std::vector* poison, std::uint32_t* nullRootReads) { +static bool EvaluateRuntimeSourcesGeneric(const IrResourcePlan& program, std::span sources, const SrtRuntime& runtime, std::vector& results, std::vector& flat, bool evaluateFlat, std::span cleanFlatSlots, std::vector& activeSources, std::vector* poison, std::uint32_t* nullRootReads) { failureReason().clear(); static const bool debug = std::getenv("APS5_SRT_DEBUG") != nullptr; if (debug) { @@ -188,6 +190,81 @@ bool EvaluateRuntimeSourcesImpl(const IrResourcePlan& program, std::span> words; + std::size_t cursor = 0; + bool replay = false; + + static bool Accessible(void* context, std::uint64_t address, std::uint64_t size) { + const auto& runtime = static_cast(context)->runtime; + return runtime.accessible(runtime.userContext, address, size); + } + + static bool Read(void* context, std::uint64_t address, std::uint32_t* value) { + auto& state = *static_cast(context); + if (state.replay) { + if (state.cursor >= state.words.size() || state.words[state.cursor].first != address) return false; + *value = state.words[state.cursor++].second; + return true; + } + if (state.runtime.readMemory != nullptr) { + if (!state.runtime.readMemory(state.runtime.userContext, address, value)) return false; + } else std::memcpy(value, reinterpret_cast(address), sizeof(*value)); + state.words.emplace_back(address, *value); + return true; + } +}; + +bool verifyExecution(const IrResourcePlan& program, const SrtRuntime& runtime, std::vector& results, std::vector& flat, std::vector& activeSources, std::vector* poison, std::uint32_t* nullRootReads) { + RecordedReads reads{runtime}; + auto captured = runtime; + captured.userContext = &reads; + captured.readMemory = RecordedReads::Read; + if (runtime.accessible != nullptr) captured.accessible = RecordedReads::Accessible; + SrtReadTrace actualTrace, expectedTrace; + captured.readTrace = &actualTrace; + const bool evaluated = program.executionPlan->Evaluate(captured, results, flat, activeSources, poison, nullRootReads); + if (runtime.readTrace != nullptr) { + runtime.readTrace->leaves.insert(runtime.readTrace->leaves.end(), actualTrace.leaves.begin(), actualTrace.leaves.end()); + runtime.readTrace->otherReads.insert(runtime.readTrace->otherReads.end(), actualTrace.otherReads.begin(), actualTrace.otherReads.end()); + } + if (!evaluated) return Fail("compiled SRT inputs could not be read"); + reads.replay = true; + captured.readTrace = &expectedTrace; + std::vector expected; + std::vector expectedFlat; + std::vector expectedActive; + std::vector expectedPoison; + std::uint32_t expectedNullRoots = 0; + const bool matches = EvaluateRuntimeSourcesGeneric(program, program.materializationSources, captured, expected, expectedFlat, true, {}, expectedActive, poison != nullptr ? &expectedPoison : nullptr, nullRootReads != nullptr ? &expectedNullRoots : nullptr) + && reads.cursor == reads.words.size() && expected == results && expectedFlat == flat && expectedActive == activeSources + && (poison == nullptr || expectedPoison == *poison) && (nullRootReads == nullptr || expectedNullRoots == *nullRootReads) + && expectedTrace.leaves == actualTrace.leaves && expectedTrace.otherReads == actualTrace.otherReads; + if (!matches) return Fail("APS5_VERIFY_COMPILED_SRT: compiled evaluation differs from the generic walk"); + return true; +} + +} + +bool EvaluateRuntimeSourcesImpl(const IrResourcePlan& program, std::span sources, const SrtRuntime& runtime, std::vector& results, std::vector& flat, bool evaluateFlat, std::span cleanFlatSlots, std::vector& activeSources, std::vector* poison, std::uint32_t* nullRootReads) { + static const bool disablePlan = std::getenv("APS5_NO_COMPILED_SRT") != nullptr || std::getenv("APS5_SRT_DEBUG") != nullptr; + static const bool verify = std::getenv("APS5_VERIFY_COMPILED_SRT") != nullptr; + const bool compiled = !disablePlan && evaluateFlat && program.executionPlan != nullptr && program.guardedSrtSlots.empty() && sources.data() == program.materializationSources.data() && sources.size() == program.materializationSources.size() && std::ranges::none_of(cleanFlatSlots, [](auto clean) { return clean != 0; }); + if (verify) { + struct Counts { std::uint64_t total = 0, compiled = 0; }; + auto& counts = HostThreadLocal(); + counts.compiled += compiled; + if ((++counts.total & 0x3fffu) == 0) std::fprintf(stderr, "[srt-plan] %llu/%llu evaluations use compiled plans with generic verification\n", static_cast(counts.compiled), static_cast(counts.total)); + } + if (!compiled) return EvaluateRuntimeSourcesGeneric(program, sources, runtime, results, flat, evaluateFlat, cleanFlatSlots, activeSources, poison, nullRootReads); + failureReason().clear(); + if (verify) return verifyExecution(program, runtime, results, flat, activeSources, poison, nullRootReads); + return program.executionPlan->Evaluate(runtime, results, flat, activeSources, poison, nullRootReads) || Fail("compiled SRT inputs could not be read"); +} + const std::string& RuntimeSourceFailureReason() { return failureReason(); } diff --git a/core/shader/recompiler/Optimization/src/SrtWalker/SrtExecutionPlan.cpp b/core/shader/recompiler/Optimization/src/SrtWalker/SrtExecutionPlan.cpp new file mode 100644 index 000000000000..d72a9054de22 --- /dev/null +++ b/core/shader/recompiler/Optimization/src/SrtWalker/SrtExecutionPlan.cpp @@ -0,0 +1,290 @@ +#include "Optimization/SrtWalker/SrtExecutionPlan.hpp" +#include "Optimization/SrtWalker/SrtAddressArithmetic.hpp" +#include "Optimization/SrtWalker/SrtInstructionPredicates.hpp" +#include +#include +#include +#include +#include +#include + +namespace ShaderRecompiler::Detail { + +namespace { + +class Compiler { +public: + explicit Compiler(const IrResourcePlan& program) : program(program) {} + + std::shared_ptr Run() { + if (!program.guardedSrtSlots.empty() || !program.srtPlanComplete || program.requiresSpecializationMemory || std::ranges::any_of(program.cleanFlatSlots, [](auto value) { return value != 0; })) return nullptr; + result.active.assign(program.descriptorSources.size(), 1); + if (!program.controlFlow.empty()) { + for (const auto& block : program.controlFlow) { + if (block.condition != nullptr) return nullptr; + for (const auto source : block.sources) { + if (source >= result.active.size()) return nullptr; + result.active[source] = 0; + } + } + std::vector visited(program.controlFlow.size()); + std::vector pending{0}; + while (!pending.empty()) { + const auto index = pending.back(); + pending.pop_back(); + if (index >= visited.size()) return nullptr; + if (visited[index]) continue; + visited[index] = 1; + const auto& block = program.controlFlow[index]; + for (const auto source : block.sources) result.active[source] = 1; + pending.insert(pending.end(), block.successors.begin(), block.successors.end()); + } + } + for (const auto sourceIndex : program.materializationSources) { + if (sourceIndex >= program.descriptorSources.size()) return nullptr; + const auto& source = program.descriptorSources[sourceIndex]; + SrtExecutionPlan::Descriptor descriptor; + descriptor.count = source.dwordCount; + if (descriptor.count > descriptor.words.size()) return nullptr; + for (std::uint32_t i = 0; i < descriptor.count; ++i) { + const auto index = result.active[sourceIndex] ? compile(source.dwords[i]) : constant(0); + if (!index) return nullptr; + descriptor.words[i] = *index; + } + result.descriptors.push_back(descriptor); + } + result.flat.resize(program.srtReads.size()); + for (std::size_t slot = 0; slot < program.srtReads.size(); ++slot) { + const auto& read = program.srtReads[slot]; + if (read.value == nullptr || read.flatOffset != slot) return nullptr; + leaf = read.flatOffset < program.pureFlatSlots.size() && program.pureFlatSlots[read.flatOffset] != 0 ? read.value->Resolve() : nullptr; + leafSlot = read.flatOffset; + const auto index = compile(read.value); + if (!index) return nullptr; + result.flat[read.flatOffset] = *index; + } + return std::make_shared(std::move(result)); + } + +private: + std::optional constant(std::uint64_t value) { + const auto index = static_cast(result.operations.size()); + result.operations.push_back({IrOpcode::Void, {}, value}); + return index; + } + + std::optional compile(IrValue* raw) { + if (raw == nullptr) return {}; + const auto* value = raw->Resolve(); + if (const auto found = indices.find(value); found != indices.end()) return found->second; + if (result.operations.size() >= 4096 || visiting.size() >= 256 || !visiting.insert(value).second) return {}; + const auto index = compileValue(*value); + visiting.erase(value); + if (index) indices.emplace(value, *index); + return index; + } + + std::optional compileValue(const IrValue& value) { + if (value.HasImmediate()) { + switch (value.Type()) { + case IrType::Bool: return constant(value.ImmediateBool()); + case IrType::U8: return constant(value.ImmediateU8()); + case IrType::U16: return constant(value.ImmediateU16()); + case IrType::U32: return constant(value.ImmediateU32()); + case IrType::U64: return constant(value.ImmediateU64()); + case IrType::F32: return constant(std::bit_cast(value.ImmediateF32())); + default: return {}; + } + } + SrtExecutionPlan::Operation operation; + operation.opcode = value.Opcode(); + const auto argument = [&](std::size_t source, std::size_t target) { + if (source >= value.ArgumentCount()) return false; + const auto index = compile(value.Argument(source)); + if (!index) return false; + operation.arguments[target] = *index; + return true; + }; + switch (operation.opcode) { + case IrOpcode::GetUserData: { + if (value.ArgumentCount() != 1) return {}; + const auto reg = RegIndex(static_cast(value.Argument(0)->Register().index)); + if (reg < program.userDataBase) return {}; + operation.immediate = reg - program.userDataBase; + break; + } + case IrOpcode::GetShaderBase: break; + case IrOpcode::BitCastU32F32: + case IrOpcode::BitCastF32U32: + return value.ArgumentCount() == 1 ? compile(value.Argument(0)) : std::nullopt; + case IrOpcode::ReadConst: { + if (value.ArgumentCount() != 2) return {}; + const auto* slot = value.Argument(1)->Resolve(); + if (!slot->HasImmediate() || slot->Type() != IrType::U32 || slot->ImmediateU32() >= program.srtReads.size()) return {}; + return compile(program.srtReads[slot->ImmediateU32()].value); + } + case IrOpcode::LoadAddressU32: + case IrOpcode::ReadConstBuffer: { + if (!IsRawRead(program, value) || value.ArgumentCount() != IrOpcodeOperandCount(operation.opcode)) return {}; + const auto* handle = value.Argument(0)->Resolve(); + const bool buffer = operation.opcode == IrOpcode::ReadConstBuffer; + if (LoadedFromMemory(*handle) || handle->Opcode() == IrOpcode::Void || handle->ArgumentCount() < 2 || (buffer && handle->ArgumentCount() != 4)) return {}; + for (std::size_t i = 0; i < 2; ++i) { + const auto index = compile(handle->Argument(i)); + if (!index) return {}; + operation.arguments[i] = *index; + } + if (!argument(1, 2)) return {}; + if (buffer) { + for (std::size_t i = 2; i < 4; ++i) { + const auto index = compile(handle->Argument(i)); + if (!index) return {}; + operation.arguments[i + 1] = *index; + } + } + operation.immediate = program.memoryInfo[value.Flags().index].offset; + if (&value == leaf) operation.leafSlot = leafSlot; + break; + } + case IrOpcode::CompositeExtractU32x2: + case IrOpcode::CompositeExtractU64: { + if (value.ArgumentCount() != 2) return {}; + const auto* component = value.Argument(1)->Resolve(); + if (!component->HasImmediate() || component->Type() != IrType::U32 || component->ImmediateU32() >= 2) return {}; + operation.immediate = component->ImmediateU32() * 32u; + const auto* source = value.Argument(0)->Resolve(); + if (operation.opcode == IrOpcode::CompositeExtractU32x2) { + if (source->ArgumentCount() != 2) return {}; + if (source->Opcode() == IrOpcode::CompositeConstructU32x2) return compile(source->Argument(component->ImmediateU32())); + if (source->Opcode() != IrOpcode::IAddCarry32) return {}; + for (std::size_t i = 0; i < 2; ++i) { + const auto index = compile(source->Argument(i)); + if (!index) return {}; + operation.arguments[i] = *index; + } + } else if (!argument(0, 0)) return {}; + break; + } + case IrOpcode::BitwiseNot32: + if (!argument(0, 0)) return {}; + break; + case IrOpcode::CompositeConstructU64: + case IrOpcode::IAdd32: case IrOpcode::IAdd64: + case IrOpcode::ISub32: case IrOpcode::ISub64: + case IrOpcode::IMul32: case IrOpcode::IMul64: + case IrOpcode::UMin32: + case IrOpcode::BitwiseAnd32: case IrOpcode::BitwiseAnd64: + case IrOpcode::BitwiseOr32: case IrOpcode::BitwiseXor32: + case IrOpcode::ShiftLeftLogical32: case IrOpcode::ShiftLeftLogical64: + case IrOpcode::ShiftRightLogical32: case IrOpcode::ShiftRightLogical64: + if (!argument(0, 0) || !argument(1, 1)) return {}; + break; + default: return {}; + } + const auto index = static_cast(result.operations.size()); + result.operations.push_back(operation); + return index; + } + + const IrResourcePlan& program; + SrtExecutionPlan result; + std::unordered_map indices; + std::unordered_set visiting; + const IrValue* leaf = nullptr; + std::uint32_t leafSlot = 0; +}; + +} + +std::shared_ptr CompileSrtExecutionPlan(const IrResourcePlan& program) { + return Compiler(program).Run(); +} + +bool SrtExecutionPlan::Evaluate(const SrtRuntime& runtime, std::vector& results, std::vector& flattened, std::vector& activeSources, std::vector* poison, std::uint32_t* nullRootReads) const { + std::uint32_t nullRoots = 0; + std::array local; + std::vector overflow; + if (operations.size() > local.size()) overflow.resize(operations.size()); + auto* values = overflow.empty() ? local.data() : overflow.data(); + for (std::size_t index = 0; index < operations.size(); ++index) { + const auto& operation = operations[index]; + const auto arg = [&](std::size_t i) { return values[operation.arguments[i]]; }; + const auto u32 = [&](std::size_t i) { return static_cast(arg(i)); }; + auto& out = values[index]; + switch (operation.opcode) { + case IrOpcode::Void: out = operation.immediate; break; + case IrOpcode::GetUserData: + if (operation.immediate >= runtime.userData.size()) return false; + out = runtime.userData[operation.immediate]; + break; + case IrOpcode::GetShaderBase: out = runtime.shaderBase; break; + case IrOpcode::CompositeConstructU64: out = u32(0) | (std::uint64_t{u32(1)} << 32u); break; + case IrOpcode::CompositeExtractU64: out = static_cast(arg(0) >> operation.immediate); break; + case IrOpcode::CompositeExtractU32x2: out = static_cast((std::uint64_t{u32(0)} + u32(1)) >> operation.immediate); break; + case IrOpcode::IAdd32: out = u32(0) + u32(1); break; + case IrOpcode::IAdd64: out = arg(0) + arg(1); break; + case IrOpcode::ISub32: out = u32(0) - u32(1); break; + case IrOpcode::ISub64: out = arg(0) - arg(1); break; + case IrOpcode::IMul32: out = u32(0) * u32(1); break; + case IrOpcode::IMul64: out = arg(0) * arg(1); break; + case IrOpcode::UMin32: out = std::min(u32(0), u32(1)); break; + case IrOpcode::BitwiseAnd32: out = u32(0) & u32(1); break; + case IrOpcode::BitwiseAnd64: out = arg(0) & arg(1); break; + case IrOpcode::BitwiseOr32: out = u32(0) | u32(1); break; + case IrOpcode::BitwiseXor32: out = u32(0) ^ u32(1); break; + case IrOpcode::BitwiseNot32: out = ~u32(0); break; + case IrOpcode::ShiftLeftLogical32: out = u32(0) << (arg(1) & 31u); break; + case IrOpcode::ShiftLeftLogical64: out = arg(0) << (arg(1) & 63u); break; + case IrOpcode::ShiftRightLogical32: out = u32(0) >> (arg(1) & 31u); break; + case IrOpcode::ShiftRightLogical64: out = arg(0) >> (arg(1) & 63u); break; + case IrOpcode::LoadAddressU32: + case IrOpcode::ReadConstBuffer: { + const auto base = ((arg(1) << 32u) | u32(0)) & AddressMask; + const auto immediate = static_cast(static_cast(operation.immediate)); + std::uint64_t address = 0; + if (operation.opcode == IrOpcode::ReadConstBuffer) { + if (immediate < 0) return false; + const auto byteOffset = static_cast(immediate) + u32(2); + const auto aligned = byteOffset & ~std::uint64_t{3}; + const auto stride = (u32(1) >> 16u) & 0x3fffu; + const auto size = stride == 0 ? std::uint64_t{u32(3)} : std::uint64_t{stride} * u32(3); + if (aligned > size || size - aligned < 4) { + out = 0; + break; + } + address = ((base & ~std::uint64_t{3}) + byteOffset) & ~std::uint64_t{3}; + } else if (!AddSignedAddress(base & ~std::uint64_t{3}, (immediate & ~std::int64_t{3}) + (u32(2) & ~3u), address)) return false; + if (poison != nullptr && nullRootReads != nullptr && base == 0) { + ++nullRoots; + out = 0; + break; + } + if (auto* trace = runtime.readTrace; trace != nullptr) { + if (operation.leafSlot != ~0u) trace->leaves.emplace_back(operation.leafSlot, address); + else trace->otherReads.push_back(address); + } + std::uint32_t word = 0; + if (runtime.readMemory != nullptr) { + if (!runtime.readMemory(runtime.userContext, address, &word)) return false; + } else std::memcpy(&word, reinterpret_cast(address), sizeof(word)); + out = word; + break; + } + default: return false; + } + } + results.resize(descriptors.size()); + for (std::size_t i = 0; i < descriptors.size(); ++i) { + results[i] = {}; + results[i].dwordCount = descriptors[i].count; + for (std::uint32_t word = 0; word < descriptors[i].count; ++word) results[i].dwords[word] = static_cast(values[descriptors[i].words[word]]); + } + flattened.resize(flat.size()); + for (std::size_t i = 0; i < flat.size(); ++i) flattened[i] = static_cast(values[flat[i]]); + activeSources = active; + if (poison != nullptr) poison->clear(); + if (nullRootReads != nullptr) *nullRootReads = nullRoots; + return true; +} + +}