Skip to content
Open
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
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,9 @@ if(BUILD_TESTING)
add_test(NAME kyty_pipeline_stalls COMMAND ${Python3_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/tools/tests/test_kyty_pipeline_stalls.py)
set_tests_properties(kyty_pipeline_stalls PROPERTIES TIMEOUT 30)
add_test(NAME kyty_manifest_float_modes COMMAND ${Python3_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/tools/tests/test_kyty_manifest_float_modes.py)
set_tests_properties(kyty_manifest_float_modes PROPERTIES TIMEOUT 30)
add_test(NAME kyty_pgo COMMAND ${Python3_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/tools/tests/test_kyty_pgo.py)
set_tests_properties(kyty_pgo PROPERTIES TIMEOUT 30)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4191,6 +4191,7 @@ PipelineCache::Pipeline* PipelineCache::GetGraphicsPipeline(const CommandBuffer&
return &cached;
}
if (m_last_graphics_pipeline != nullptr && key == m_last_graphics_key) {
NoteFloatModes(*m_last_graphics_pipeline, key, command.GetShaders());
return m_last_graphics_pipeline;
}
if (const auto found = m_graphics_pipelines.find(key); found != m_graphics_pipelines.end()) {
Expand All @@ -4199,6 +4200,7 @@ PipelineCache::Pipeline* PipelineCache::GetGraphicsPipeline(const CommandBuffer&
if (!found->second.use_noted) {
NoteUse(found->second, stages);
}
NoteFloatModes(found->second, key, command.GetShaders());
return &found->second;
}
#if defined(ANYPS5_KYTY)
Expand Down Expand Up @@ -4263,6 +4265,7 @@ PipelineCache::Pipeline* PipelineCache::GetGraphicsPipeline(const CommandBuffer&
m_last_graphics_pipeline = &ready;
StorePipeline(key);
NoteUse(ready, stages);
NoteFloatModes(ready, key, command.GetShaders());
return &ready;
}
if (!m_async_pending_graphics.contains(key)) {
Expand Down Expand Up @@ -4305,6 +4308,7 @@ PipelineCache::Pipeline* PipelineCache::GetGraphicsPipeline(const CommandBuffer&
StorePipeline(key);
NoteCreatedPipeline(probe_started);
NoteUse(ready, stages);
NoteFloatModes(ready, key, command.GetShaders());
return &ready;
}
}
Expand Down Expand Up @@ -4402,6 +4406,7 @@ PipelineCache::Pipeline* PipelineCache::GetGraphicsPipeline(const CommandBuffer&
StorePipeline(key);
NoteCreatedPipeline(started);
NoteUse(pipeline, stages);
NoteFloatModes(pipeline, key, command.GetShaders());
m_last_graphics_key = key;
m_last_graphics_pipeline = &pipeline;

Expand Down
16 changes: 15 additions & 1 deletion core/kyty/graphics/host_gpu/renderer/pipeline/pipelineCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,17 @@ struct alignas(4) GraphicsPipelineKey {

static_assert(std::has_unique_object_representations_v<GraphicsPipelineKey>);

struct ManifestFloatMode {
uint8_t float_mode = 0;
bool ieee = false;
bool dx10_clamp = false;
bool recorded = false;

bool operator==(const ManifestFloatMode& other) const = default;
};

using ManifestFloatModes = std::array<ManifestFloatMode, 4>;

#if defined(ANYPS5_KYTY)
namespace HW {
struct ModeControl;
Expand Down Expand Up @@ -448,6 +459,8 @@ class PipelineCache {
bool uses_image_heap = false;
bool use_noted = false;
bool native_bindings = false;
ManifestFloatModes float_modes {};
uint64_t float_modes_generation = 0;
};

void GetGraphicsPrograms(const CommandBuffer& command, DrawRenderState& state);
Expand Down Expand Up @@ -875,7 +888,8 @@ class PipelineCache {
void RecordModule(const ProgramKey& key, const ShaderRecompiler::CompileOptions& options,
std::span<const uint32_t> code, uint32_t push_data_cursor,
const ShaderModule& module);
void RecordPipeline(const GraphicsPipelineKey& key);
bool RecordPipeline(const GraphicsPipelineKey& key, const ManifestFloatModes& float_modes);
void NoteFloatModes(Pipeline& pipeline, const GraphicsPipelineKey& key, const HW::Shader& shaders);
void RecordLayout(uint64_t key, const ShaderTessellationInputInfo& layout);
static void OnShaderMapped(void* userdata, uint64_t hash, std::span<const uint32_t> code);
bool ProvideCode(uint64_t hash, std::span<const uint32_t> code, bool from_guest);
Expand Down
90 changes: 79 additions & 11 deletions core/kyty/graphics/host_gpu/renderer/pipeline/pipelineManifest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,27 @@ std::vector<uint32_t> StaticKey(const ManifestModule& module) {
return key;
}

struct FloatModeMerge {
bool changed = false;
bool mixed = false;
};

FloatModeMerge MergeFloatModes(ManifestFloatModes& known, const ManifestFloatModes& seen) {
FloatModeMerge merge;
for (size_t i = 0; i < known.size(); i++) {
if (!seen[i].recorded) {
continue;
}
if (!known[i].recorded) {
known[i] = seen[i];
merge.changed = true;
} else if (known[i] != seen[i]) {
merge.mixed = true;
}
}
return merge;
}

}

struct PipelineCache::ManifestState {
Expand All @@ -167,7 +188,10 @@ struct PipelineCache::ManifestState {
std::deque<ManifestPipeline> warm_pipelines;
PipelineManifest recorded;
std::unordered_map<uint64_t, uint32_t> module_index;
std::unordered_set<uint64_t> pipeline_identities;
std::unordered_map<uint64_t, uint32_t> pipeline_index;
std::unordered_set<uint32_t> mixed_float_modes;
std::unordered_set<size_t> unlinked_pipelines;
std::atomic<uint64_t> module_generation {1};
std::unordered_set<uint64_t> layout_keys;
std::unordered_map<uint64_t, std::pair<uint32_t, bool>> module_records;
bool dirty = false;
Expand Down Expand Up @@ -569,8 +593,13 @@ void PipelineCache::OpenManifest() {
for (auto& stage: pipeline.stages) {
stage = stage == ManifestPipeline::NoStage ? stage : indices[stage];
}
if (state.pipeline_identities.insert(ManifestPipelineIdentity(pipeline)).second) {
const auto [found, inserted] = state.pipeline_index.try_emplace(
ManifestPipelineIdentity(pipeline), static_cast<uint32_t>(all.pipelines.size()));
if (inserted) {
all.pipelines.push_back(pipeline);
} else if (MergeFloatModes(all.pipelines[found->second].float_modes, pipeline.float_modes)
.mixed) {
state.mixed_float_modes.insert(found->second);
}
}
for (const auto& layout: loaded.tessellation_layouts) {
Expand Down Expand Up @@ -2554,6 +2583,7 @@ void PipelineCache::BuildManifestModule(uint32_t index, std::span<const uint32_t
if (module->plain != nullptr) {
state.module_records[module->plain->id] = {found->second, true};
}
state.module_generation.fetch_add(1, std::memory_order_release);
}
}
for (const auto pipeline: state.module_pipelines[index]) {
Expand Down Expand Up @@ -2861,32 +2891,60 @@ void PipelineCache::RecordModule(const ProgramKey& key,
if (module.plain != nullptr) {
state.module_records[module.plain->id] = {found->second, true};
}
state.module_generation.fetch_add(1, std::memory_order_release);
}

void PipelineCache::RecordPipeline(const GraphicsPipelineKey& key) {
if (m_manifest == nullptr || m_manifest->record_path.empty()) {
return;
}
bool PipelineCache::RecordPipeline(const GraphicsPipelineKey& key,
const ManifestFloatModes& float_modes) {
auto& state = *m_manifest;
ManifestPipeline record {.key = key};
ManifestPipeline record {.key = key, .float_modes = float_modes};
record.key.stage_ids = {};
const auto key_hash = GraphicsPipelineKeyHash {}(key);
std::scoped_lock lock(state.mutex);
for (size_t i = 0; i < key.stage_ids.size(); i++) {
if (key.stage_ids[i] == 0) {
continue;
}
const auto found = state.module_records.find(key.stage_ids[i]);
if (found == state.module_records.end() || (found->second.second && i != 3)) {
return;
state.unlinked_pipelines.insert(key_hash);
return false;
}
record.stages[i] = found->second.first;
record.plain_pixel = record.plain_pixel || found->second.second;
}
if (state.pipeline_identities.insert(ManifestPipelineIdentity(record)).second) {
state.unlinked_pipelines.erase(key_hash);
const auto [found, inserted] = state.pipeline_index.try_emplace(
ManifestPipelineIdentity(record), static_cast<uint32_t>(state.recorded.pipelines.size()));
if (inserted) {
state.recorded.pipelines.push_back(record);
state.added_pipelines++;
state.dirty = true;
return true;
}
const auto merge =
MergeFloatModes(state.recorded.pipelines[found->second].float_modes, float_modes);
state.dirty = state.dirty || merge.changed;
if (merge.mixed && state.mixed_float_modes.insert(found->second).second) {
state.dirty = true;
}
return true;
}

void PipelineCache::NoteFloatModes(Pipeline& pipeline, const GraphicsPipelineKey& key,
const HW::Shader& shaders) {
if (m_manifest == nullptr || m_manifest->record_path.empty()) {
return;
}
constexpr uint64_t Linked = UINT64_MAX;
const auto modes = ReadManifestFloatModes(shaders, key.stage_ids);
const auto generation = m_manifest->module_generation.load(std::memory_order_acquire);
if (modes == pipeline.float_modes &&
(pipeline.float_modes_generation == Linked || pipeline.float_modes_generation == generation)) {
return;
}
pipeline.float_modes = modes;
pipeline.float_modes_generation = RecordPipeline(key, modes) ? Linked : generation;
}

void PipelineCache::RecordLayout(uint64_t key, const ShaderTessellationInputInfo& layout) {
Expand All @@ -2909,6 +2967,8 @@ void PipelineCache::SaveManifestRecording() {
PipelineManifest manifest;
uint32_t added_modules = 0;
uint32_t added_pipelines = 0;
size_t mixed = 0;
size_t unlinked = 0;
{
std::scoped_lock lock(state.mutex);
if (!state.dirty) {
Expand All @@ -2917,8 +2977,14 @@ void PipelineCache::SaveManifestRecording() {
manifest = state.recorded;
added_modules = state.added_modules;
added_pipelines = state.added_pipelines;
mixed = state.mixed_float_modes.size();
unlinked = state.unlinked_pipelines.size();
state.dirty = false;
}
const auto with_modes =
std::ranges::count_if(manifest.pipelines, [](const ManifestPipeline& pipeline) {
return pipeline.float_modes[0].recorded;
});
const auto bytes = SaveManifest(manifest);
auto temporary = state.record_path;
temporary += ".tmp";
Expand All @@ -2934,9 +3000,11 @@ void PipelineCache::SaveManifestRecording() {
std::filesystem::rename(temporary, state.record_path, error);
ManifestLog(
"Pipeline manifest: wrote {} ({} modules, {} pipelines, {:.1f} KB; {} modules and {} "
"pipelines new in this session)",
"pipelines new in this session; float modes of {} pipelines, {} of them drawn with more "
"than one, {} drawn pipelines whose modules the manifest does not list)",
Common::PathToString(state.record_path), manifest.modules.size(), manifest.pipelines.size(),
static_cast<double>(bytes.size()) / 1024.0, added_modules, added_pipelines);
static_cast<double>(bytes.size()) / 1024.0, added_modules, added_pipelines, with_modes,
mixed, unlinked);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ struct ManifestPipeline {
std::array<uint32_t, 4> stages {NoStage, NoStage, NoStage, NoStage};
bool plain_pixel = false;
GraphicsPipelineKey key {};
ManifestFloatModes float_modes {};
bool from_assets = false;
};

Expand All @@ -74,6 +75,8 @@ bool LoadManifest(std::span<const uint8_t> bytes, PipelineManifest& manifest,
[[nodiscard]] uint64_t ManifestModuleIdentity(const ManifestModule& module,
std::span<const uint32_t> static_state);
[[nodiscard]] uint64_t ManifestPipelineIdentity(const ManifestPipeline& pipeline);
[[nodiscard]] ManifestFloatModes ReadManifestFloatModes(const HW::Shader& shaders,
const std::array<uint64_t, 4>& stage_ids);

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "graphics/host_gpu/renderer/pipeline/pipelineManifest.h"
#include "graphics/guest_gpu/hardwareContext.h"
#include "graphics/shader/recompiler/ir/Serialize.h"
#include "graphics/shader/shaderCompiler.h"

Expand Down Expand Up @@ -806,6 +807,10 @@ bool ReadModule(std::span<const std::string_view> tokens, ManifestModule& module

constexpr std::array<std::string_view, 4> PipelineStageKeys {"v", "h", "d", "p"};

std::string FloatModeKey(size_t stage) {
return fmt::format("f{}", PipelineStageKeys[stage]);
}

std::string PipelineText(const ManifestPipeline& pipeline,
std::span<const std::string> module_ids) {
[[maybe_unused]] const auto& [rendering_fields_a, rendering_fields_b, rendering_fields_c,
Expand All @@ -826,6 +831,13 @@ std::string PipelineText(const ManifestPipeline& pipeline,
w.Value(PipelineStageKeys[i], module_ids[pipeline.stages[i]]);
}
}
for (size_t i = 0; i < PipelineStageKeys.size(); i++) {
const auto& mode = pipeline.float_modes[i];
if (pipeline.stages[i] != ManifestPipeline::NoStage && mode.recorded) {
w.Value(FloatModeKey(i), fmt::format("{:x}.{:d}.{:d}", mode.float_mode, mode.ieee,
mode.dx10_clamp));
}
}
bool plain = pipeline.plain_pixel;
w.Flag("plain", plain);
const auto& rendering = pipeline.key.rendering;
Expand Down Expand Up @@ -920,6 +932,20 @@ bool ReadPipeline(std::span<const std::string_view> tokens,
pipeline.stages[i] = found->second;
}
}
for (size_t i = 0; i < PipelineStageKeys.size(); i++) {
const auto text = r.Find(FloatModeKey(i));
if (!text) continue;
const auto parts = Split(*text, '.');
uint64_t values[3] {};
r.ok = r.ok && pipeline.stages[i] != ManifestPipeline::NoStage && parts.size() == 3 &&
ParseNumber(parts[0], values[0], 16) && values[0] <= UINT8_MAX &&
ParseNumber(parts[1], values[1]) && values[1] <= 1 &&
ParseNumber(parts[2], values[2]) && values[2] <= 1;
pipeline.float_modes[i] = {.float_mode = static_cast<uint8_t>(values[0]),
.ieee = values[1] != 0,
.dx10_clamp = values[2] != 0,
.recorded = true};
}
r.Flag("plain", pipeline.plain_pixel);
auto& rendering = pipeline.key.rendering;
uint32_t count = 0;
Expand Down Expand Up @@ -1177,6 +1203,28 @@ uint64_t ManifestModuleIdentity(const ManifestModule& module,
return XXH3_64bits(writer.bytes.data(), writer.bytes.size());
}

ManifestFloatModes ReadManifestFloatModes(const HW::Shader& shaders,
const std::array<uint64_t, 4>& stage_ids) {
const auto read = [](const auto& rsrc1) {
return ManifestFloatMode {.float_mode = rsrc1.float_mode,
.ieee = rsrc1.ieee_mode,
.dx10_clamp = rsrc1.dx10_clamp,
.recorded = true};
};
const auto& vs = shaders.GetVs();
const bool tessellation = stage_ids[1] != 0;
const ManifestFloatModes registers {
tessellation ? read(vs.hs_regs.rsrc1) : read(vs.gs_regs.rsrc1), read(vs.hs_regs.rsrc1),
read(vs.gs_regs.rsrc1), read(shaders.GetPs().ps_regs.rsrc1)};
ManifestFloatModes modes {};
for (size_t i = 0; i < modes.size(); i++) {
if (stage_ids[i] != 0) {
modes[i] = registers[i];
}
}
return modes;
}

uint64_t ManifestPipelineIdentity(const ManifestPipeline& pipeline) {
auto key = pipeline.key;
key.stage_ids = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,6 @@ void PipelineCache::StoreModule(const ProgramKey& key, const Program& program,
}

void PipelineCache::StorePipeline(const GraphicsPipelineKey& key) {
RecordPipeline(key);
if (!m_store) {
return;
}
Expand Down
13 changes: 13 additions & 0 deletions core/libs/prx/libSceAgcDriver/KytyShim/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,19 @@ add_test(NAME kyty_shim_empty_buffer_variants_off COMMAND kyty_shim_empty_buffer
set_tests_properties(kyty_shim_empty_buffer_variants_off PROPERTIES TIMEOUT 20 ENVIRONMENT "KYTY_SHARE_EMPTY_BUFFERS=0"
PASS_REGULAR_EXPRESSION "kyty_shim_empty_buffer_variants_off: ok" FAIL_REGULAR_EXPRESSION "terminate called|FAILED")

if(NOT ANYPS5_KYTY_DLL)
add_test_executable(kyty_shim_manifest_float_modes_tests ${CMAKE_CURRENT_SOURCE_DIR}/ManifestFloatModes.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../src/Log.cpp)
target_include_directories(kyty_shim_manifest_float_modes_tests PRIVATE ${LIBS_INCLUDE_DIR})
configure_host_test_executable(kyty_shim_manifest_float_modes_tests)
if(MINGW)
target_link_libraries(kyty_shim_manifest_float_modes_tests PRIVATE stdc++ gcc_eh)
endif()
target_link_libraries(kyty_shim_manifest_float_modes_tests PRIVATE Kyty::gpu libkernel libc)
add_test(NAME kyty_shim_manifest_float_modes COMMAND kyty_shim_manifest_float_modes_tests)
set_tests_properties(kyty_shim_manifest_float_modes PROPERTIES TIMEOUT 20
PASS_REGULAR_EXPRESSION "kyty_shim_manifest_float_modes: ok" FAIL_REGULAR_EXPRESSION "terminate called|FAILED")
endif()

add_test_executable(kyty_shim_gpu_control_loads_tests ${CMAKE_CURRENT_SOURCE_DIR}/GpuControlLoads.cpp ${CMAKE_CURRENT_SOURCE_DIR}/../src/Log.cpp)
target_include_directories(kyty_shim_gpu_control_loads_tests PRIVATE ${LIBS_INCLUDE_DIR})
configure_host_test_executable(kyty_shim_gpu_control_loads_tests)
Expand Down
Loading
Loading