Compare commits

..

3 Commits

Author SHA1 Message Date
Caio Oliveira
513b81b965 [DONOTMERGE] add debug knobs for faster testing
* should work?

Signed-off-by: Caio Oliveira <caiooliveirafarias0@gmail.com>
2025-12-16 20:52:03 +01:00
Caio Oliveira
955cc70796 Fix license headers 2025-12-16 20:52:03 +01:00
wildcard
b55c8bb2e7 [Vulkan] speed up bank lookup with LRU cache, batch growth to 32
Adds a tiny 8 entry LRU cache(should be tested for optimal number)
Increase batch growth to 32 (should be tested to determine the difference between 16 and 32)
2025-12-16 20:52:03 +01:00
6 changed files with 113 additions and 61 deletions

View File

@@ -20,7 +20,7 @@
namespace Vulkan {
// Prefer small grow rates to avoid saturating the descriptor pool with barely used pipelines
constexpr size_t SETS_GROW_RATE = 16;
//constexpr size_t SETS_GROW_RATE = 32; //test difference between 16 and 32
constexpr s32 SCORE_THRESHOLD = 3;
struct DescriptorBank {
@@ -29,9 +29,12 @@ struct DescriptorBank {
};
bool DescriptorBankInfo::IsSuperset(const DescriptorBankInfo& subset) const noexcept {
return uniform_buffers >= subset.uniform_buffers && storage_buffers >= subset.storage_buffers &&
texture_buffers >= subset.texture_buffers && image_buffers >= subset.image_buffers &&
textures >= subset.textures && images >= subset.images;
return uniform_buffers >= subset.uniform_buffers &&
storage_buffers >= subset.storage_buffers &&
texture_buffers >= subset.texture_buffers &&
image_buffers >= subset.image_buffers &&
textures >= subset.textures &&
images >= subset.images;
}
template <typename Descriptors>
@@ -45,6 +48,19 @@ static u32 Accumulate(const Descriptors& descriptors) {
static DescriptorBankInfo MakeBankInfo(std::span<const Shader::Info> infos) {
DescriptorBankInfo bank;
if (infos.size() == 1) {
const auto& info = infos.front();
const auto acc = [](const auto& ds){ u32 c=0; for (const auto& d: ds) c+=d.count; return c; };
bank.uniform_buffers += acc(info.constant_buffer_descriptors);
bank.storage_buffers += acc(info.storage_buffers_descriptors);
bank.texture_buffers += acc(info.texture_buffer_descriptors);
bank.image_buffers += acc(info.image_buffer_descriptors);
bank.textures += acc(info.texture_descriptors);
bank.images += acc(info.image_descriptors);
bank.score = bank.uniform_buffers + bank.storage_buffers + bank.texture_buffers +
bank.image_buffers + bank.textures + bank.images;
return bank;
}
for (const Shader::Info& info : infos) {
bank.uniform_buffers += Accumulate(info.constant_buffer_descriptors);
bank.storage_buffers += Accumulate(info.storage_buffers_descriptors);
@@ -87,48 +103,58 @@ static void AllocatePool(const Device& device, DescriptorBank& bank) {
}));
}
static size_t GetGrowRate() {
if (Settings::getDebugKnobAt(0))
return 8;
else if (Settings::getDebugKnobAt(1))
return 16;
else if (Settings::getDebugKnobAt(2))
return 24;
else if (Settings::getDebugKnobAt(3))
return 32;
else if (Settings::getDebugKnobAt(4))
return 40;
else if (Settings::getDebugKnobAt(5))
return 48;
else if (Settings::getDebugKnobAt(6))
return 56;
else if (Settings::getDebugKnobAt(7))
return 64;
else
return 16;
}
DescriptorAllocator::DescriptorAllocator(const Device& device_, MasterSemaphore& master_semaphore_,
DescriptorBank& bank_, VkDescriptorSetLayout layout_)
: ResourcePool(master_semaphore_, SETS_GROW_RATE), device{&device_}, bank{&bank_},
layout{layout_} {}
: ResourcePool(master_semaphore_, /*SETS_GROW_RATE*/ GetGrowRate()), device{&device_}, bank{&bank_},
layout{layout_}, grow_rate{GetGrowRate()} {}
VkDescriptorSet DescriptorAllocator::Commit() {
const size_t index = CommitResource();
return sets[index / SETS_GROW_RATE][index % SETS_GROW_RATE];
}
VkDescriptorSet DescriptorAllocator::CommitWithTracking(u64 current_frame, const void* descriptor_data) {
const size_t index = CommitResource();
const size_t group = index / SETS_GROW_RATE;
const size_t slot = index % SETS_GROW_RATE;
set_states[group][slot].set = sets[group][slot];
set_states[group][slot].last_update_frame = current_frame;
set_states[group][slot].last_data_ptr = descriptor_data;
return set_states[group][slot].set;
}
bool DescriptorAllocator::NeedsUpdate(VkDescriptorSet set, u64 current_frame, const void* descriptor_data) const {
for (const auto& group : set_states)
for (const auto& st : group)
if (st.set == set) // Update if pointer changed or the set hasn't been updated this frame
return st.last_data_ptr != descriptor_data || st.last_update_frame != current_frame;
return true;
return sets[index / grow_rate][index % grow_rate];
}
void DescriptorAllocator::Allocate(size_t begin, size_t end) {
const size_t count = end - begin;
sets.push_back(AllocateDescriptors(count));
set_states.emplace_back(count); // create parallel state storage
sets.push_back(AllocateDescriptors(end - begin));
}
vk::DescriptorSets DescriptorAllocator::AllocateDescriptors(size_t count) {
const std::vector<VkDescriptorSetLayout> layouts(count, layout);
std::array<VkDescriptorSetLayout, 64> stack{};
const VkDescriptorSetLayout* p_layouts = nullptr;
std::vector<VkDescriptorSetLayout> heap;
if (count <= stack.size()) {
stack.fill(layout);
p_layouts = stack.data();
} else {
heap.assign(count, layout);
p_layouts = heap.data();
}
VkDescriptorSetAllocateInfo allocate_info{
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
.pNext = nullptr,
.descriptorPool = *bank->pools.back(),
.descriptorSetCount = static_cast<u32>(count),
.pSetLayouts = layouts.data(),
.pSetLayouts = p_layouts,
};
vk::DescriptorSets new_sets = bank->pools.back().Allocate(allocate_info);
if (!new_sets.IsOutOfPoolMemory()) {
@@ -166,21 +192,58 @@ DescriptorAllocator DescriptorPool::Allocator(VkDescriptorSetLayout layout,
}
DescriptorBank& DescriptorPool::Bank(const DescriptorBankInfo& reqs) {
{
std::scoped_lock lk(cache_mutex);
DescriptorBank* best = nullptr; u64 best_stamp = 0;
for (const auto& e : cache_) {
if (!e.bank) continue;
if (std::abs(e.info.score - reqs.score) < SCORE_THRESHOLD && e.info.IsSuperset(reqs)) {
if (e.stamp >= best_stamp) { best_stamp = e.stamp; best = e.bank; }
}
}
if (best) return *best;
}
std::shared_lock read_lock{banks_mutex};
const auto it = std::ranges::find_if(bank_infos, [&reqs](const DescriptorBankInfo& bank) {
return std::abs(bank.score - reqs.score) < SCORE_THRESHOLD && bank.IsSuperset(reqs);
});
if (it != bank_infos.end()) {
return *banks[std::distance(bank_infos.begin(), it)].get();
DescriptorBank& found = *banks[std::distance(bank_infos.begin(), it)].get();
read_lock.unlock();
// update cache
std::scoped_lock lk(cache_mutex);
size_t victim = 0; u64 oldest = UINT64_MAX;
for (size_t i=0;i<cache_.size();++i) if (cache_[i].stamp < oldest) { oldest = cache_[i].stamp; victim = i; }
cache_[victim] = CacheEntry{found.info, &found, ++cache_tick_};
return found;
}
read_lock.unlock();
std::unique_lock write_lock{banks_mutex};
auto it2 = std::ranges::find_if(bank_infos, [&reqs](const DescriptorBankInfo& bank) {
return std::abs(bank.score - reqs.score) < SCORE_THRESHOLD && bank.IsSuperset(reqs);
});
if (it2 != bank_infos.end()) {
DescriptorBank& found = *banks[std::distance(bank_infos.begin(), it2)].get();
// update cache
std::scoped_lock lk(cache_mutex);
size_t victim = 0; u64 oldest = UINT64_MAX;
for (size_t i=0;i<cache_.size();++i) if (cache_[i].stamp < oldest) { oldest = cache_[i].stamp; victim = i; }
cache_[victim] = CacheEntry{found.info, &found, ++cache_tick_};
return found;
}
bank_infos.push_back(reqs);
auto& bank = *banks.emplace_back(std::make_unique<DescriptorBank>());
bank.info = reqs;
AllocatePool(device, bank);
// update cache
{
std::scoped_lock lk(cache_mutex);
size_t victim = 0; u64 oldest = UINT64_MAX;
for (size_t i=0;i<cache_.size();++i) if (cache_[i].stamp < oldest) { oldest = cache_[i].stamp; victim = i; }
cache_[victim] = CacheEntry{bank.info, &bank, ++cache_tick_};
}
return bank;
}

View File

@@ -9,7 +9,8 @@
#include <shared_mutex>
#include <span>
#include <vector>
#include "common/common_types.h"
#include <array>
#include <mutex>
#include "shader_recompiler/shader_info.h"
#include "video_core/renderer_vulkan/vk_resource_pool.h"
#include "video_core/vulkan_common/vulkan_wrapper.h"
@@ -47,15 +48,7 @@ public:
DescriptorAllocator(const DescriptorAllocator&) = delete;
VkDescriptorSet Commit();
// commit + remember when/with what we last updated this set
struct DescriptorSetState {
VkDescriptorSet set = VK_NULL_HANDLE;
u64 last_update_frame = 0;
const void* last_data_ptr = nullptr; // fast pointer compare
};
VkDescriptorSet CommitWithTracking(u64 current_frame, const void* descriptor_data);
bool NeedsUpdate(VkDescriptorSet set, u64 current_frame, const void* descriptor_data) const;
private:
explicit DescriptorAllocator(const Device& device_, MasterSemaphore& master_semaphore_,
DescriptorBank& bank_, VkDescriptorSetLayout layout_);
@@ -69,7 +62,7 @@ private:
VkDescriptorSetLayout layout{};
std::vector<vk::DescriptorSets> sets;
std::vector<std::vector<DescriptorSetState>> set_states;
size_t grow_rate = 32;
};
class DescriptorPool {
@@ -87,6 +80,14 @@ public:
private:
DescriptorBank& Bank(const DescriptorBankInfo& reqs);
struct CacheEntry {
DescriptorBankInfo info{};
DescriptorBank* bank{nullptr};
u64 stamp{0};
};
std::mutex cache_mutex{};
std::array<CacheEntry, 8> cache_{}; //test and then adjust
u64 cache_tick_{0};
const Device& device;
MasterSemaphore& master_semaphore;

View File

@@ -515,20 +515,7 @@ void GraphicsPipeline::ConfigureDraw(const RescalingPushConstant& rescaling,
const bool update_rescaling{scheduler.UpdateRescaling(is_rescaling)};
const bool bind_pipeline{scheduler.UpdateGraphicsPipeline(this)};
const void* const descriptor_data{guest_descriptor_queue.UpdateData()};
// allocate/bind descriptor set and only update if needed
VkDescriptorSet descriptor_set = VK_NULL_HANDLE;
bool needs_update = false;
if (descriptor_set_layout && !uses_push_descriptor) {
descriptor_set = descriptor_allocator.CommitWithTracking(current_frame_number, descriptor_data);
needs_update = descriptor_allocator.NeedsUpdate(descriptor_set, current_frame_number, descriptor_data);
if (needs_update) {
const vk::Device& dev{device.GetLogical()};
dev.UpdateDescriptorSet(descriptor_set, *descriptor_update_template, descriptor_data);
}
}
scheduler.Record([this, descriptor_data, descriptor_set, bind_pipeline, rescaling_data = rescaling.Data(),
scheduler.Record([this, descriptor_data, bind_pipeline, rescaling_data = rescaling.Data(),
is_rescaling, update_rescaling,
uses_render_area = render_area.uses_render_area,
render_area_data = render_area.words](vk::CommandBuffer cmdbuf) {
@@ -554,8 +541,12 @@ void GraphicsPipeline::ConfigureDraw(const RescalingPushConstant& rescaling,
return;
}
if (uses_push_descriptor) {
cmdbuf.PushDescriptorSetWithTemplateKHR(*descriptor_update_template, *pipeline_layout, 0, descriptor_data);
cmdbuf.PushDescriptorSetWithTemplateKHR(*descriptor_update_template, *pipeline_layout,
0, descriptor_data);
} else {
const VkDescriptorSet descriptor_set{descriptor_allocator.Commit()};
const vk::Device& dev{device.GetLogical()};
dev.UpdateDescriptorSet(descriptor_set, *descriptor_update_template, descriptor_data);
cmdbuf.BindDescriptorSets(VK_PIPELINE_BIND_POINT_GRAPHICS, *pipeline_layout, 0,
descriptor_set, nullptr);
}

View File

@@ -116,7 +116,7 @@ public:
maxwell3d = maxwell3d_;
gpu_memory = gpu_memory_;
}
void SetFrameNumber(u64 frame) { current_frame_number = frame; }
private:
template <typename Spec>
bool ConfigureImpl(bool is_indexed);
@@ -160,7 +160,6 @@ private:
std::mutex build_mutex;
std::atomic_bool is_built{false};
bool uses_push_descriptor{false};
u64 current_frame_number{0};
};
} // namespace Vulkan

View File

@@ -234,7 +234,6 @@ void RasterizerVulkan::PrepareDraw(bool is_indexed, Func&& draw_func) {
std::scoped_lock lock{buffer_cache.mutex, texture_cache.mutex};
// update engine as channel may be different.
pipeline->SetEngine(maxwell3d, gpu_memory);
pipeline->SetFrameNumber(current_frame_number);
if (!pipeline->Configure(is_indexed))
return;
@@ -772,7 +771,6 @@ void RasterizerVulkan::FlushCommands() {
}
void RasterizerVulkan::TickFrame() {
current_frame_number++;
draw_counter = 0;
guest_descriptor_queue.TickFrame();
compute_pass_descriptor_queue.TickFrame();

View File

@@ -160,7 +160,7 @@ private:
void UpdateDynamicStates();
void HandleTransformFeedback();
u64 current_frame_number{0};
void UpdateViewportsState(Tegra::Engines::Maxwell3D::Regs& regs);
void UpdateScissorsState(Tegra::Engines::Maxwell3D::Regs& regs);
void UpdateDepthBias(Tegra::Engines::Maxwell3D::Regs& regs);