Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ class DescriptorCache {
SetAllocation Allocate(VkDescriptorSetLayout layout, std::span<const VkDescriptorPoolSize> sizes, bool updateAfterBind = false, std::optional<std::uint32_t> variableCount = std::nullopt);
void Free(const SetAllocation& allocation, std::shared_ptr<const TableDescriptors> table = {}) noexcept;
SetAllocation AllocateRewritten(VkDescriptorSetLayout layout, std::span<const VkDescriptorPoolSize> sizes, bool updateAfterBind = false, std::optional<std::uint32_t> variableCount = std::nullopt);
bool TakeRewritten(VkDescriptorSetLayout layout, std::optional<std::uint32_t> variableCount, SetAllocation& taken);
void Recycle(VkDescriptorSetLayout layout, const SetAllocation& allocation) noexcept;
// APS5_PROFILE_DRAW counters: layouts served from the map / created, sets allocated, pools opened.
struct Stats {
Expand Down
63 changes: 37 additions & 26 deletions core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5626,15 +5626,18 @@ DescriptorCache::SetAllocation DescriptorCache::Allocate(VkDescriptorSetLayout l
return {set, pool, chain.size() - 1, updateAfterBind, VK_NULL_HANDLE, {}, variableCount, {}};
}

bool DescriptorCache::TakeRewritten(VkDescriptorSetLayout layout, std::optional<std::uint32_t> variableCount, SetAllocation& taken) {
std::lock_guard lock(mutex);
const auto found = recycled.find({layout, variableCount.value_or(0u)});
if (found == recycled.end() || found->second.empty()) return false;
taken = std::move(found->second.back());
found->second.pop_back();
return true;
}

DescriptorCache::SetAllocation DescriptorCache::AllocateRewritten(VkDescriptorSetLayout layout, std::span<const VkDescriptorPoolSize> sizes, bool updateAfterBind, std::optional<std::uint32_t> variableCount) {
{
std::lock_guard lock(mutex);
if (const auto found = recycled.find({layout, variableCount.value_or(0u)}); found != recycled.end() && !found->second.empty()) {
const auto allocation = found->second.back();
found->second.pop_back();
return allocation;
}
}
SetAllocation taken;
if (TakeRewritten(layout, variableCount, taken)) return taken;
return Allocate(layout, sizes, updateAfterBind, variableCount);
}

Expand Down Expand Up @@ -7492,32 +7495,40 @@ std::shared_ptr<ShaderResources::DrawBindings> ShaderResources::PrepareDrawBindi
void ShaderResources::writeDrawSets(DrawBindings& result, std::span<const std::size_t> selected, const BdaResources* faultRecord) const {
Require(context.descriptorCache != nullptr, "draw snapshots require a descriptor cache");
std::array<bool, 2> described{};
for (const auto& binding : bindings) described[setOf(binding)] = true;
thread_local std::array<std::vector<VkDescriptorPoolSize>, 2>* countsSlot = nullptr;
auto& counts = ShaderRecompiler::ThreadOwned(countsSlot);
for (auto& sizes : counts) sizes.clear();
for (const auto& binding : bindings) {
const auto set = setOf(binding);
described[set] = true;
const auto count = (binding.flags & VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT) != 0 ? std::max(1u, static_cast<std::uint32_t>(binding.imageAllocations.size())) : binding.layout.descriptorCount;
if (count == 0) continue;
auto& sizes = counts[set];
const auto held = std::find_if(sizes.begin(), sizes.end(), [&](const VkDescriptorPoolSize& entry) { return entry.type == binding.layout.descriptorType; });
if (held != sizes.end()) held->descriptorCount += count;
else sizes.push_back({binding.layout.descriptorType, count});
}
for (auto& sizes : counts) std::sort(sizes.begin(), sizes.end(), [](const VkDescriptorPoolSize& a, const VkDescriptorPoolSize& b) { return a.type < b.type; });
const auto sizesOf = [&](std::size_t set) { return std::span<const VkDescriptorPoolSize>(counts[set]); };
bool counted = false;
const auto sizesOf = [&](std::size_t set) {
auto& counts = ShaderRecompiler::ThreadOwned(countsSlot);
if (!counted) {
counted = true;
for (auto& sizes : counts) sizes.clear();
for (const auto& binding : bindings) {
const auto count = (binding.flags & VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT) != 0 ? std::max(1u, static_cast<std::uint32_t>(binding.imageAllocations.size())) : binding.layout.descriptorCount;
if (count == 0) continue;
auto& sizes = counts[setOf(binding)];
const auto held = std::find_if(sizes.begin(), sizes.end(), [&](const VkDescriptorPoolSize& entry) { return entry.type == binding.layout.descriptorType; });
if (held != sizes.end()) held->descriptorCount += count;
else sizes.push_back({binding.layout.descriptorType, count});
}
for (auto& sizes : counts) std::sort(sizes.begin(), sizes.end(), [](const VkDescriptorPoolSize& a, const VkDescriptorPoolSize& b) { return a.type < b.type; });
}
return std::span<const VkDescriptorPoolSize>(counts[set]);
};
const auto variableOf = [&](std::size_t set) { return tableBindings[set] != std::numeric_limits<std::size_t>::max() ? std::optional<std::uint32_t>(std::max(1u, static_cast<std::uint32_t>(bindings[tableBindings[set]].imageAllocations.size()))) : std::nullopt; };
result.cache = context.descriptorCache;
const auto take = [&](VkDescriptorSetLayout setLayout, std::size_t set, DescriptorCache::SetAllocation& slot) {
const auto variable = variableOf(set);
if (!result.cache->TakeRewritten(setLayout, variable, slot)) slot = result.cache->Allocate(setLayout, sizesOf(set), updateAfterBind, variable);
Require(slot.set != VK_NULL_HANDLE, "draw snapshot descriptor allocation failed");
};
if (described[0] || splitSets) {
result.allocation = result.cache->AllocateRewritten(_layout, sizesOf(0), updateAfterBind, variableOf(0));
take(_layout, 0, result.allocation);
result.layout = _layout;
Require(result.allocation.set != VK_NULL_HANDLE, "draw snapshot descriptor allocation failed");
}
if (described[1] || splitSets) {
result.fragmentAllocation = result.cache->AllocateRewritten(fragment.layout, sizesOf(1), updateAfterBind, variableOf(1));
take(fragment.layout, 1, result.fragmentAllocation);
result.fragmentLayout = fragment.layout;
Require(result.fragmentAllocation.set != VK_NULL_HANDLE, "draw snapshot descriptor allocation failed");
}
thread_local std::vector<VkDescriptorBufferInfo>* buffersSlot = nullptr;
auto& buffers = ShaderRecompiler::ThreadOwned(buffersSlot);
Expand Down
9 changes: 8 additions & 1 deletion core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8391,8 +8391,15 @@ void descriptorCacheTests() {
Require(larger.set != rewritten.set && mock.variableDescriptorCount == 5u, "a recycled descriptor set served a larger variable image table than it was allocated for");
const auto same = cache.AllocateRewritten(fittingLayout, fitting, true, 3u);
Require(same.set == rewritten.set, "a recycled descriptor set was not reused for a variable image table of its own size");
AgcDriver::Graphics::DescriptorCache::SetAllocation taken;
Require(!cache.TakeRewritten(fittingLayout, 3u, taken) && taken.set == VK_NULL_HANDLE, "an empty recycle list served a set");
cache.Recycle(fittingLayout, same);
Require(!cache.TakeRewritten(fittingLayout, 5u, taken) && taken.set == VK_NULL_HANDLE, "a recycled set served another variable image count");
Require(!cache.TakeRewritten(fittingLayout, std::nullopt, taken) && taken.set == VK_NULL_HANDLE, "a recycled set served a layout without a variable image table");
Require(cache.TakeRewritten(fittingLayout, 3u, taken) && taken.set == same.set && taken.pool == same.pool && taken.variableCount == std::optional<std::uint32_t>(3u), "a recycled set was not taken back whole");
Require(!cache.TakeRewritten(fittingLayout, 3u, taken), "a taken set was served twice");
cache.Free(larger);
cache.Free(same);
cache.Free(taken);
expectFailure([&] { cache.Allocate(fittingLayout, fitting, false, 3u); }, "descriptor set exceeds the device's per-set descriptor limit");
const std::array<VkDescriptorSetLayoutBinding, 1> excessiveBindings{{{0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 8193, VK_SHADER_STAGE_FRAGMENT_BIT, nullptr}}};
const std::array<VkDescriptorBindingFlags, 1> excessiveFlags{VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT};
Expand Down
Loading