mirror of
https://github.com/KytyPS5/KytyPS5.git
synced 2026-10-20 02:47:02 +02:00
Refresh cached resources in one SRT walk
This commit is contained in:
@@ -21,18 +21,6 @@ namespace {
|
||||
constexpr uint64_t AddressMask = 0x0000ffffffffffffull;
|
||||
constexpr uint64_t MaxIndirectImageProbes = 65536u;
|
||||
|
||||
struct IndirectImage {
|
||||
uint32_t resource = 0;
|
||||
std::vector<uint32_t> keys;
|
||||
std::vector<uint32_t> candidates;
|
||||
std::vector<DescriptorValue> descriptors;
|
||||
};
|
||||
|
||||
struct MaterializedSnapshot {
|
||||
ResourceSnapshot resources;
|
||||
std::vector<IndirectImage> indirect_images;
|
||||
};
|
||||
|
||||
bool SpecializationFail(std::string_view message) {
|
||||
std::fprintf(stderr, "shader resource specialization failed: %.*s\n",
|
||||
static_cast<int>(message.size()), message.data());
|
||||
@@ -198,159 +186,96 @@ bool ReadScalarBufferWord(const ShaderBufferResource& descriptor, uint32_t dynam
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MaterializeIndirectImage(const DescriptorSource::IndirectImage& indirect,
|
||||
const DescriptorValue& material_value,
|
||||
const DescriptorValue& heap_value, bool r128,
|
||||
const SrtRuntime& runtime, IndirectImage& result) {
|
||||
bool MaterializeIndirectImage(const ResourcePlan& program,
|
||||
const DescriptorSource::IndirectImage& indirect,
|
||||
const DescriptorValue& material_value,
|
||||
const DescriptorValue& heap_value, uint32_t image_index,
|
||||
const SrtRuntime& runtime, ResourceSnapshot& snapshot,
|
||||
ResourceSpecialization& specialization) {
|
||||
ShaderBufferResource material;
|
||||
ShaderBufferResource heap;
|
||||
if (!DecodeBufferDescriptor(material_value, material) ||
|
||||
!DecodeBufferDescriptor(heap_value, heap)) {
|
||||
!DecodeBufferDescriptor(heap_value, heap) || material.Stride() != indirect.selector_stride) {
|
||||
return false;
|
||||
}
|
||||
if (material.Stride() != indirect.selector_stride) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// S_BUFFER_LOAD ignores vector-buffer swizzle/add-thread fields. The shader computes the
|
||||
// record stride explicitly; enumerate every wrapped 32-bit offset that can pass bounds.
|
||||
const auto period = uint64_t {1} << 32u;
|
||||
const auto step = std::gcd<uint64_t>(indirect.selector_stride, period);
|
||||
const auto residue = static_cast<uint64_t>(indirect.selector_offset) % step;
|
||||
const auto size = material.GetSize();
|
||||
const auto limit = std::min<uint64_t>(UINT32_MAX, size + 3u);
|
||||
// Enumerate every wrapped scalar-buffer offset that can pass the descriptor bounds.
|
||||
const auto step = std::gcd<uint64_t>(indirect.selector_stride, uint64_t {1} << 32u);
|
||||
const auto residue = static_cast<uint64_t>(indirect.selector_offset) % step;
|
||||
const auto limit = std::min<uint64_t>(UINT32_MAX, material.GetSize() + 3u);
|
||||
const auto probe_count = residue <= limit ? (limit - residue) / step + 1u : 0u;
|
||||
if (probe_count > MaxIndirectImageProbes) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<uint32_t> keys {0u};
|
||||
std::unordered_set<uint32_t> seen {0u};
|
||||
auto& keys = program.material_keys;
|
||||
keys.clear();
|
||||
keys.reserve(static_cast<size_t>(probe_count) + 1u);
|
||||
seen.reserve(static_cast<size_t>(probe_count) + 1u);
|
||||
keys.push_back(0u);
|
||||
for (uint64_t offset = residue; offset <= limit && probe_count != 0u; offset += step) {
|
||||
uint32_t key = 0;
|
||||
if (!ReadScalarBufferWord(material, static_cast<uint32_t>(offset), 0u, runtime, key)) {
|
||||
return false;
|
||||
}
|
||||
if (seen.insert(key).second) {
|
||||
keys.push_back(key);
|
||||
}
|
||||
keys.push_back(key);
|
||||
if (limit - offset < step) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
std::ranges::sort(keys);
|
||||
keys.erase(std::unique(keys.begin(), keys.end()), keys.end());
|
||||
|
||||
IndirectImage next;
|
||||
next.keys = std::move(keys);
|
||||
next.candidates.reserve(next.keys.size());
|
||||
next.descriptors.reserve(
|
||||
std::min(next.keys.size(), static_cast<size_t>(ShaderInfo::MaxImages)));
|
||||
for (const auto key: next.keys) {
|
||||
const auto children_begin = snapshot.images.size();
|
||||
const auto mapping_offset = snapshot.flattened_srt.size();
|
||||
const auto root_image = specialization.images[image_index];
|
||||
snapshot.flattened_srt.resize(mapping_offset + 1u + keys.size() * 2u);
|
||||
snapshot.flattened_srt[mapping_offset] = static_cast<uint32_t>(keys.size());
|
||||
for (uint32_t entry = 0; entry < keys.size(); ++entry) {
|
||||
const auto key = keys[entry];
|
||||
DescriptorValue candidate;
|
||||
candidate.dword_count = 8u;
|
||||
candidate.dword_count = 8u;
|
||||
const auto heap_offset = key << 5u;
|
||||
for (uint32_t dword = 0; dword < candidate.dword_count; dword++) {
|
||||
for (uint32_t dword = 0; dword < candidate.dword_count; ++dword) {
|
||||
if (!ReadScalarBufferWord(heap, heap_offset, dword * sizeof(uint32_t), runtime,
|
||||
candidate.dwords[dword])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (NullImageDescriptor(candidate) || !ValidImageDescriptor(candidate, r128)) {
|
||||
if (NullImageDescriptor(candidate) ||
|
||||
!ValidImageDescriptor(candidate, program.info.images[image_index].r128)) {
|
||||
candidate.dwords.fill(0);
|
||||
}
|
||||
const auto found = std::ranges::find(next.descriptors, candidate);
|
||||
if (found == next.descriptors.end()) {
|
||||
if (next.descriptors.size() >= ShaderInfo::MaxImages) {
|
||||
return false;
|
||||
uint32_t ordinal = 0;
|
||||
if (entry == 0) {
|
||||
snapshot.images[image_index] = candidate;
|
||||
} else if (snapshot.images[image_index] != candidate) {
|
||||
const auto found = std::find(snapshot.images.begin() + children_begin,
|
||||
snapshot.images.end(), candidate);
|
||||
ordinal = static_cast<uint32_t>(found - snapshot.images.begin() - children_begin + 1u);
|
||||
if (found == snapshot.images.end()) {
|
||||
if (snapshot.images.size() >= ShaderInfo::MaxImages) {
|
||||
return false;
|
||||
}
|
||||
snapshot.images.push_back(candidate);
|
||||
auto child = root_image;
|
||||
child.indirect_root = image_index;
|
||||
specialization.images.push_back(child);
|
||||
}
|
||||
next.descriptors.push_back(candidate);
|
||||
next.candidates.push_back(static_cast<uint32_t>(next.descriptors.size() - 1u));
|
||||
} else {
|
||||
next.candidates.push_back(static_cast<uint32_t>(found - next.descriptors.begin()));
|
||||
}
|
||||
snapshot.flattened_srt[mapping_offset + 1u + entry * 2u] = key;
|
||||
snapshot.flattened_srt[mapping_offset + 2u + entry * 2u] = ordinal;
|
||||
}
|
||||
if (snapshot.images.size() == children_begin) {
|
||||
snapshot.flattened_srt.resize(mapping_offset);
|
||||
} else {
|
||||
auto& root = specialization.images[image_index];
|
||||
root.indirect_root = image_index;
|
||||
root.indirect_mapping_offset = static_cast<uint32_t>(mapping_offset);
|
||||
root.indirect_search_iterations = std::bit_width(keys.size());
|
||||
}
|
||||
result = std::move(next);
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
static bool MaterializeSnapshot(const ResourcePlan& program, const SrtRuntime& runtime,
|
||||
MaterializedSnapshot& snapshot) {
|
||||
if (!program.resource_tracking_complete) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (program.requires_specialization_memory && runtime.read_specialization_memory == nullptr) {
|
||||
return false;
|
||||
}
|
||||
std::vector<DescriptorValue> values;
|
||||
std::vector<uint32_t> flattened_srt;
|
||||
std::vector<uint8_t> active_sources;
|
||||
if (!EvaluateRuntimeSources(program, program.materialization_sources, runtime, values,
|
||||
flattened_srt, program.clean_flat_slots, active_sources)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto& next = snapshot.resources;
|
||||
const auto& fill = program.uniform_fill;
|
||||
const auto words = fill.fill.words;
|
||||
std::array<uint32_t, 4> stored {};
|
||||
if (words != 0 &&
|
||||
EvaluateUniformValues(program, std::span(fill.values).first(words), runtime,
|
||||
std::span(stored).first(words)) &&
|
||||
std::all_of(stored.begin(), stored.begin() + words,
|
||||
[&](uint32_t value) { return value == stored[0]; })) {
|
||||
next.uniform_fill = fill.fill;
|
||||
next.uniform_fill.value = stored[0];
|
||||
}
|
||||
auto cursor = values.begin();
|
||||
next.buffers.assign(cursor, cursor + program.info.buffers.size());
|
||||
cursor += program.info.buffers.size();
|
||||
next.flattened_srt = std::move(flattened_srt);
|
||||
next.images.resize(program.info.images.size());
|
||||
for (uint32_t image_index = 0; image_index < program.info.images.size(); image_index++) {
|
||||
const auto& image = program.info.images[image_index];
|
||||
const auto* source = Source(program, image.source);
|
||||
if (source != nullptr && source->indirect_image.has_value()) {
|
||||
if (!active_sources[image.source]) {
|
||||
next.images[image_index].dword_count = 8u;
|
||||
continue;
|
||||
}
|
||||
const std::array requests {source->indirect_image->material_source,
|
||||
source->indirect_image->heap_source};
|
||||
SrtRuntime clean_runtime = runtime;
|
||||
clean_runtime.read_memory = runtime.read_specialization_memory;
|
||||
std::vector<DescriptorValue> tables;
|
||||
if (!EvaluateDescriptorSources(program, requests, clean_runtime, tables)) {
|
||||
return false;
|
||||
}
|
||||
const auto& material = tables[0];
|
||||
const auto& heap = tables[1];
|
||||
IndirectImage table;
|
||||
if (!MaterializeIndirectImage(*source->indirect_image, material, heap, image.r128,
|
||||
runtime, table)) {
|
||||
return false;
|
||||
}
|
||||
next.images[image_index] = table.descriptors[table.candidates[0]];
|
||||
if (table.descriptors.size() > 1u) {
|
||||
table.resource = image_index;
|
||||
snapshot.indirect_images.push_back(std::move(table));
|
||||
}
|
||||
} else {
|
||||
auto descriptor = *cursor++;
|
||||
if (!ValidImageDescriptor(descriptor, image.r128)) {
|
||||
descriptor.dwords.fill(0);
|
||||
}
|
||||
next.images[image_index] = descriptor;
|
||||
}
|
||||
}
|
||||
next.samplers.assign(cursor, cursor + program.info.samplers.size());
|
||||
next.user_data.assign(runtime.user_data.begin(), runtime.user_data.end());
|
||||
return true;
|
||||
}
|
||||
|
||||
struct SamplerPlan {
|
||||
std::array<uint32_t, ShaderInfo::MaxSamplers> point_sampler {};
|
||||
uint32_t sampler_count = 0;
|
||||
@@ -390,68 +315,12 @@ private:
|
||||
template <typename Images>
|
||||
bool BuildSamplerPlan(const ShaderInfo& base, const Images& images, SamplerPlan& plan);
|
||||
|
||||
static bool BuildResourceSpecialization(const ResourcePlan& program, MaterializedSnapshot snapshot,
|
||||
ResourceSnapshot& specialized_snapshot,
|
||||
static bool BuildResourceSpecialization(const ResourcePlan& program, ResourceSnapshot& snapshot,
|
||||
ResourceSpecialization& specialization) {
|
||||
auto next_snapshot = std::move(snapshot.resources);
|
||||
ResourceSpecialization next_specialization;
|
||||
next_specialization.buffers.reserve(program.info.buffers.size());
|
||||
size_t image_count = program.info.images.size();
|
||||
size_t mapping_words = 0;
|
||||
for (const auto& table: snapshot.indirect_images) {
|
||||
if (table.resource >= program.info.images.size() || table.descriptors.size() < 2u ||
|
||||
image_count + table.descriptors.size() - 1u > ShaderInfo::MaxImages) {
|
||||
return SpecializationFail(
|
||||
"indirect image candidates exceed the dense image resource limit");
|
||||
}
|
||||
image_count += table.descriptors.size() - 1u;
|
||||
mapping_words += 1u + table.keys.size() * 2u;
|
||||
}
|
||||
next_snapshot.images.reserve(image_count);
|
||||
next_snapshot.flattened_srt.reserve(next_snapshot.flattened_srt.size() + mapping_words);
|
||||
next_specialization.images.reserve(image_count);
|
||||
for (const auto& image: program.info.images) {
|
||||
next_specialization.images.push_back({
|
||||
.numeric_class = image.numeric_class,
|
||||
.dimension = image.dimension,
|
||||
.mip_count = image.mip_count,
|
||||
.conversion_format = image.conversion_format,
|
||||
.shader_swizzle = image.shader_swizzle,
|
||||
.indirect_root = image.indirect_root,
|
||||
.indirect_mapping_offset = image.indirect_mapping_offset,
|
||||
.indirect_search_iterations = image.indirect_search_iterations,
|
||||
.cube = image.cube,
|
||||
});
|
||||
}
|
||||
for (const auto& table: snapshot.indirect_images) {
|
||||
const auto root_image = next_specialization.images[table.resource];
|
||||
for (uint32_t candidate = 1; candidate < table.descriptors.size(); candidate++) {
|
||||
auto image = root_image;
|
||||
image.indirect_root = table.resource;
|
||||
next_specialization.images.push_back(image);
|
||||
next_snapshot.images.push_back(table.descriptors[candidate]);
|
||||
}
|
||||
auto& root = next_specialization.images[table.resource];
|
||||
root.indirect_root = table.resource;
|
||||
root.indirect_mapping_offset = static_cast<uint32_t>(next_snapshot.flattened_srt.size());
|
||||
root.indirect_search_iterations = std::bit_width(table.keys.size());
|
||||
next_snapshot.flattened_srt.resize(next_snapshot.flattened_srt.size() + 1u +
|
||||
table.keys.size() * 2u);
|
||||
std::vector<uint32_t> order(table.keys.size());
|
||||
std::iota(order.begin(), order.end(), 0u);
|
||||
std::ranges::sort(order, {}, [&](uint32_t index) { return table.keys[index]; });
|
||||
next_snapshot.flattened_srt[root.indirect_mapping_offset] =
|
||||
static_cast<uint32_t>(table.keys.size());
|
||||
for (uint32_t entry = 0; entry < order.size(); entry++) {
|
||||
const auto source = order[entry];
|
||||
const auto offset = root.indirect_mapping_offset + 1u + entry * 2u;
|
||||
next_snapshot.flattened_srt[offset] = table.keys[source];
|
||||
next_snapshot.flattened_srt[offset + 1] = table.candidates[source];
|
||||
}
|
||||
next_snapshot.images[table.resource] = table.descriptors[0];
|
||||
}
|
||||
specialization.buffers.clear();
|
||||
specialization.buffers.reserve(program.info.buffers.size());
|
||||
for (uint32_t i = 0; i < program.info.buffers.size(); i++) {
|
||||
auto& descriptor_value = next_snapshot.buffers[i];
|
||||
auto& descriptor_value = snapshot.buffers[i];
|
||||
ShaderBufferResource descriptor;
|
||||
if (!DecodeBufferDescriptor(descriptor_value, descriptor)) {
|
||||
return SpecializationFail(fmt::format("buffer descriptor {} has invalid width", i));
|
||||
@@ -468,7 +337,7 @@ static bool BuildResourceSpecialization(const ResourcePlan& program, Materialize
|
||||
} else if (!swizzle) {
|
||||
packed_stride &= ~(3u << 16u);
|
||||
}
|
||||
next_specialization.buffers.push_back({
|
||||
specialization.buffers.push_back({
|
||||
.packed_stride = packed_stride,
|
||||
.descriptor_format = program.info.buffers[i].formatted
|
||||
? descriptor.Format()
|
||||
@@ -477,9 +346,9 @@ static bool BuildResourceSpecialization(const ResourcePlan& program, Materialize
|
||||
program.info.buffers[i].formatted ? descriptor.DstSelXYZW() : DstSel(4, 5, 6, 7),
|
||||
});
|
||||
}
|
||||
for (uint32_t i = 0; i < next_specialization.images.size(); i++) {
|
||||
const auto& descriptor = next_snapshot.images[i];
|
||||
auto& image = next_specialization.images[i];
|
||||
for (uint32_t i = 0; i < specialization.images.size(); i++) {
|
||||
const auto& descriptor = snapshot.images[i];
|
||||
auto& image = specialization.images[i];
|
||||
const auto base_index = i < program.info.images.size() ? i : image.indirect_root;
|
||||
if (base_index >= program.info.images.size()) {
|
||||
return SpecializationFail(fmt::format("image resource {} has an invalid root", i));
|
||||
@@ -554,42 +423,42 @@ static bool BuildResourceSpecialization(const ResourcePlan& program, Materialize
|
||||
static_cast<uint32_t>(format)));
|
||||
}
|
||||
}
|
||||
for (uint32_t root_index = 0; root_index < next_specialization.images.size(); root_index++) {
|
||||
auto& root = next_specialization.images[root_index];
|
||||
for (uint32_t root_index = 0; root_index < specialization.images.size(); root_index++) {
|
||||
auto& root = specialization.images[root_index];
|
||||
if (root.indirect_root != root_index) {
|
||||
continue;
|
||||
}
|
||||
const auto key_count = root.indirect_mapping_offset < next_snapshot.flattened_srt.size()
|
||||
? next_snapshot.flattened_srt[root.indirect_mapping_offset]
|
||||
const auto key_count = root.indirect_mapping_offset < snapshot.flattened_srt.size()
|
||||
? snapshot.flattened_srt[root.indirect_mapping_offset]
|
||||
: 0u;
|
||||
if (root.indirect_search_iterations == 0u || key_count < 2u ||
|
||||
static_cast<size_t>(root.indirect_mapping_offset) + 1u +
|
||||
static_cast<size_t>(key_count) * 2u >
|
||||
next_snapshot.flattened_srt.size()) {
|
||||
snapshot.flattened_srt.size()) {
|
||||
return SpecializationFail("indirect image specialization has an invalid key mapping");
|
||||
}
|
||||
uint32_t exemplar = ImageResource::NoIndirectImage;
|
||||
uint32_t resource_count = 0;
|
||||
for (uint32_t resource = 0; resource < next_specialization.images.size(); resource++) {
|
||||
if (next_specialization.images[resource].indirect_root != root_index) {
|
||||
for (uint32_t resource = 0; resource < specialization.images.size(); resource++) {
|
||||
if (specialization.images[resource].indirect_root != root_index) {
|
||||
continue;
|
||||
}
|
||||
resource_count++;
|
||||
if (exemplar == ImageResource::NoIndirectImage &&
|
||||
!NullImageDescriptor(next_snapshot.images[resource])) {
|
||||
!NullImageDescriptor(snapshot.images[resource])) {
|
||||
exemplar = resource;
|
||||
}
|
||||
}
|
||||
if (resource_count < 2u || exemplar == ImageResource::NoIndirectImage) {
|
||||
return SpecializationFail("indirect image specialization has no typed candidate");
|
||||
}
|
||||
const auto& image_class = next_specialization.images[exemplar];
|
||||
for (uint32_t candidate = 0; candidate < next_specialization.images.size(); candidate++) {
|
||||
auto& image = next_specialization.images[candidate];
|
||||
const auto& image_class = specialization.images[exemplar];
|
||||
for (uint32_t candidate = 0; candidate < specialization.images.size(); candidate++) {
|
||||
auto& image = specialization.images[candidate];
|
||||
if (image.indirect_root != root_index) {
|
||||
continue;
|
||||
}
|
||||
if (NullImageDescriptor(next_snapshot.images[candidate])) {
|
||||
if (NullImageDescriptor(snapshot.images[candidate])) {
|
||||
image.numeric_class = image_class.numeric_class;
|
||||
image.dimension = image_class.dimension;
|
||||
image.mip_count = image_class.mip_count;
|
||||
@@ -610,18 +479,16 @@ static bool BuildResourceSpecialization(const ResourcePlan& program, Materialize
|
||||
}
|
||||
}
|
||||
SamplerPlan sampler_plan;
|
||||
if (!BuildSamplerPlan(program.info, next_specialization.images, sampler_plan)) {
|
||||
if (!BuildSamplerPlan(program.info, specialization.images, sampler_plan)) {
|
||||
return SpecializationFail("specialized sampler layout exceeds its resource limit");
|
||||
}
|
||||
for (uint32_t index = 0; index < program.info.samplers.size(); index++) {
|
||||
const auto target = sampler_plan.point_sampler[index];
|
||||
if (target != UINT32_MAX && target >= program.info.samplers.size()) {
|
||||
next_snapshot.samplers.push_back(next_snapshot.samplers[index]);
|
||||
snapshot.samplers.push_back(snapshot.samplers[index]);
|
||||
}
|
||||
}
|
||||
ImageRemap(next_specialization).Apply(next_snapshot.images);
|
||||
specialization = std::move(next_specialization);
|
||||
specialized_snapshot = std::move(next_snapshot);
|
||||
ImageRemap(specialization).Apply(snapshot.images);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -935,28 +802,13 @@ ResourcePlan ExtractResourcePlan(const Program& program) {
|
||||
for (uint32_t i = 0; i < plan.uniform_fill.fill.words; ++i) {
|
||||
plan.uniform_fill.values[i] = Clone(plan.uniform_fill.values[i]);
|
||||
}
|
||||
plan.materialization_sources.reserve(plan.info.buffers.size() + plan.info.images.size() +
|
||||
plan.info.samplers.size());
|
||||
for (const auto& buffer: plan.info.buffers) {
|
||||
plan.materialization_sources.push_back(buffer.source);
|
||||
}
|
||||
for (const auto& image: plan.info.images) {
|
||||
const auto* source = Source(plan, image.source);
|
||||
if (source != nullptr && source->indirect_image.has_value()) {
|
||||
plan.requires_specialization_memory = true;
|
||||
} else {
|
||||
plan.materialization_sources.push_back(image.source);
|
||||
}
|
||||
}
|
||||
for (const auto& sampler: plan.info.samplers) {
|
||||
plan.materialization_sources.push_back(sampler.source);
|
||||
}
|
||||
plan.clean_flat_slots.resize(plan.srt_reads.size());
|
||||
for (const auto& image: plan.info.images) {
|
||||
const auto* source = Source(plan, image.source);
|
||||
if (source == nullptr || !source->indirect_image.has_value()) {
|
||||
continue;
|
||||
}
|
||||
plan.requires_specialization_memory = true;
|
||||
MarkCleanFlatSlots(plan, Source(plan, source->indirect_image->material_source),
|
||||
plan.clean_flat_slots);
|
||||
MarkCleanFlatSlots(plan, Source(plan, source->indirect_image->heap_source),
|
||||
@@ -967,11 +819,98 @@ ResourcePlan ExtractResourcePlan(const Program& program) {
|
||||
|
||||
bool MaterializeResources(const ResourcePlan& program, const SrtRuntime& runtime,
|
||||
ResourceSnapshot& snapshot, ResourceSpecialization& specialization) {
|
||||
MaterializedSnapshot materialized;
|
||||
if (!MaterializeSnapshot(program, runtime, materialized)) {
|
||||
if (!program.resource_tracking_complete ||
|
||||
(program.requires_specialization_memory && runtime.read_specialization_memory == nullptr)) {
|
||||
return false;
|
||||
}
|
||||
return BuildResourceSpecialization(program, std::move(materialized), snapshot, specialization);
|
||||
SrtWalker clean(program, CleanRuntime(runtime));
|
||||
SrtWalker walker(program, runtime, program.clean_flat_slots, &clean);
|
||||
const auto active = clean.FindActiveSources();
|
||||
if (!walker.RefreshFlatBuffer(snapshot.flattened_srt)) {
|
||||
return false;
|
||||
}
|
||||
snapshot.uniform_fill = {};
|
||||
const auto& fill = program.uniform_fill;
|
||||
const auto words = fill.fill.words;
|
||||
std::array<uint32_t, 4> stored {};
|
||||
bool uniform_fill = words != 0;
|
||||
for (uint32_t i = 0; i < words && uniform_fill; ++i) {
|
||||
uniform_fill = clean.Evaluate(fill.values[i], stored[i]) && stored[i] == stored[0];
|
||||
}
|
||||
if (uniform_fill) {
|
||||
snapshot.uniform_fill = fill.fill;
|
||||
snapshot.uniform_fill.value = stored[0];
|
||||
}
|
||||
const auto evaluate = [&](uint32_t source, DescriptorValue& value) {
|
||||
if (source >= program.descriptor_sources.size()) {
|
||||
return false;
|
||||
}
|
||||
if (active.empty() || active[source]) {
|
||||
return walker.EvaluateDescriptor(source, value);
|
||||
}
|
||||
value = {};
|
||||
value.dword_count = program.descriptor_sources[source].dword_count;
|
||||
return true;
|
||||
};
|
||||
snapshot.buffers.resize(program.info.buffers.size());
|
||||
for (uint32_t i = 0; i < program.info.buffers.size(); ++i) {
|
||||
if (!evaluate(program.info.buffers[i].source, snapshot.buffers[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
snapshot.images.resize(program.info.images.size());
|
||||
specialization.images.clear();
|
||||
specialization.images.reserve(program.info.images.size());
|
||||
for (const auto& image: program.info.images) {
|
||||
specialization.images.push_back({
|
||||
.numeric_class = image.numeric_class,
|
||||
.dimension = image.dimension,
|
||||
.mip_count = image.mip_count,
|
||||
.conversion_format = image.conversion_format,
|
||||
.shader_swizzle = image.shader_swizzle,
|
||||
.indirect_root = image.indirect_root,
|
||||
.indirect_mapping_offset = image.indirect_mapping_offset,
|
||||
.indirect_search_iterations = image.indirect_search_iterations,
|
||||
.cube = image.cube,
|
||||
});
|
||||
}
|
||||
for (uint32_t i = 0; i < program.info.images.size(); ++i) {
|
||||
const auto& image = program.info.images[i];
|
||||
const auto* source = Source(program, image.source);
|
||||
if (source == nullptr) {
|
||||
return false;
|
||||
}
|
||||
if (source->indirect_image.has_value()) {
|
||||
snapshot.images[i] = {.dword_count = 8u};
|
||||
if (!active.empty() && !active[image.source]) {
|
||||
continue;
|
||||
}
|
||||
const auto& indirect = *source->indirect_image;
|
||||
DescriptorValue material;
|
||||
DescriptorValue heap;
|
||||
if (!clean.EvaluateDescriptor(indirect.material_source, material) ||
|
||||
!clean.EvaluateDescriptor(indirect.heap_source, heap) ||
|
||||
!MaterializeIndirectImage(program, indirect, material, heap, i, runtime, snapshot,
|
||||
specialization)) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if (!evaluate(image.source, snapshot.images[i])) {
|
||||
return false;
|
||||
}
|
||||
if (!ValidImageDescriptor(snapshot.images[i], image.r128)) {
|
||||
snapshot.images[i].dwords.fill(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
snapshot.samplers.resize(program.info.samplers.size());
|
||||
for (uint32_t i = 0; i < program.info.samplers.size(); ++i) {
|
||||
if (!evaluate(program.info.samplers[i].source, snapshot.samplers[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
snapshot.user_data.assign(runtime.user_data.begin(), runtime.user_data.end());
|
||||
return BuildResourceSpecialization(program, snapshot, specialization);
|
||||
}
|
||||
|
||||
void ApplyResourceSpecialization(Program& program, const ResourceSpecialization& specialization) {
|
||||
|
||||
Reference in New Issue
Block a user