Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion core/libs/prx/libSceAgcDriver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:$<TARGET_FILE_DIR:libc>")
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()
Expand All @@ -1050,6 +1055,7 @@ if(BUILD_TESTING)
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
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"
Expand Down
1 change: 1 addition & 0 deletions core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
333 changes: 333 additions & 0 deletions core/libs/prx/libSceAgcDriver/tests/SrtExecution.cpp

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions core/shader/recompiler/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

namespace ShaderRecompiler {

namespace Detail { struct SrtExecutionPlan; }

struct DescriptorValue {
std::array<std::uint32_t, 8> dwords = {};
std::uint32_t dwordCount = 0;
Expand Down Expand Up @@ -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<std::uint8_t> pureFlatSlots;
std::shared_ptr<const Detail::SrtExecutionPlan> executionPlan;
bool requiresSpecializationMemory = false;
bool srtPlanComplete = false;
bool resourceTrackingComplete = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <array>
#include <memory>
#include <vector>

namespace ShaderRecompiler::Detail {

struct SrtExecutionPlan {
struct Operation {
IrOpcode opcode = IrOpcode::Void;
std::array<std::uint32_t, 5> arguments{};
std::uint64_t immediate = 0;
std::uint32_t leafSlot = ~0u;
};
struct Descriptor {
std::array<std::uint32_t, 8> words{};
std::uint32_t count = 0;
};

std::vector<Operation> operations;
std::vector<Descriptor> descriptors;
std::vector<std::uint32_t> flat;
std::vector<std::uint8_t> active;

bool Evaluate(const SrtRuntime& runtime, std::vector<DescriptorValue>& results, std::vector<std::uint32_t>& flattened, std::vector<std::uint8_t>& activeSources, std::vector<SrtReadPoison>* poison, std::uint32_t* nullRootReads) const;
};

std::shared_ptr<const SrtExecutionPlan> CompileSrtExecutionPlan(const IrResourcePlan& program);

}

#endif
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <utility>
Expand Down Expand Up @@ -45,7 +47,7 @@ const DescriptorSource* Source(const IrResourcePlan& program, std::uint32_t sour

}

bool EvaluateRuntimeSourcesImpl(const IrResourcePlan& program, std::span<const std::uint32_t> sources, const SrtRuntime& runtime, std::vector<DescriptorValue>& results, std::vector<std::uint32_t>& flat, bool evaluateFlat, std::span<const std::uint8_t> cleanFlatSlots, std::vector<std::uint8_t>& activeSources, std::vector<SrtReadPoison>* poison, std::uint32_t* nullRootReads) {
static bool EvaluateRuntimeSourcesGeneric(const IrResourcePlan& program, std::span<const std::uint32_t> sources, const SrtRuntime& runtime, std::vector<DescriptorValue>& results, std::vector<std::uint32_t>& flat, bool evaluateFlat, std::span<const std::uint8_t> cleanFlatSlots, std::vector<std::uint8_t>& activeSources, std::vector<SrtReadPoison>* poison, std::uint32_t* nullRootReads) {
failureReason().clear();
static const bool debug = std::getenv("APS5_SRT_DEBUG") != nullptr;
if (debug) {
Expand Down Expand Up @@ -188,6 +190,81 @@ bool EvaluateRuntimeSourcesImpl(const IrResourcePlan& program, std::span<const s
return true;
}

namespace {

struct RecordedReads {
const SrtRuntime& runtime;
std::vector<std::pair<std::uint64_t, std::uint32_t>> 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<RecordedReads*>(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<RecordedReads*>(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<const void*>(address), sizeof(*value));
state.words.emplace_back(address, *value);
return true;
}
};

bool verifyExecution(const IrResourcePlan& program, const SrtRuntime& runtime, std::vector<DescriptorValue>& results, std::vector<std::uint32_t>& flat, std::vector<std::uint8_t>& activeSources, std::vector<SrtReadPoison>* 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<DescriptorValue> expected;
std::vector<std::uint32_t> expectedFlat;
std::vector<std::uint8_t> expectedActive;
std::vector<SrtReadPoison> 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<const std::uint32_t> sources, const SrtRuntime& runtime, std::vector<DescriptorValue>& results, std::vector<std::uint32_t>& flat, bool evaluateFlat, std::span<const std::uint8_t> cleanFlatSlots, std::vector<std::uint8_t>& activeSources, std::vector<SrtReadPoison>* 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, Counts>();
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<unsigned long long>(counts.compiled), static_cast<unsigned long long>(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();
}
Expand Down
Loading
Loading