gpu: keep reused smaller image tables resident and drop them a frame after their last draw

This commit is contained in:
nmzik
2026-10-27 09:00:45 +01:00
parent 358333125d
commit 9d8c458535
4 changed files with 62 additions and 17 deletions
@@ -20,7 +20,7 @@ namespace {
namespace IR = ShaderRecompiler::IR; namespace IR = ShaderRecompiler::IR;
// Smaller tables are cheap to materialize on each draw and are often transient. // Smaller tables are often written for one draw, so they enter on a second draw of a frame.
constexpr uint64_t MinTableBytes = 64ull * 1024ull; constexpr uint64_t MinTableBytes = 64ull * 1024ull;
constexpr uint32_t ElementMask = (1u << IR::ImageHeapKindShift) - 1u; constexpr uint32_t ElementMask = (1u << IR::ImageHeapKindShift) - 1u;
@@ -133,12 +133,16 @@ ImageHeap::~ImageHeap() {
bool ImageHeap::Acquire(const IR::ImageHeapRequest& request, IR::ImageHeapView& result) { bool ImageHeap::Acquire(const IR::ImageHeapRequest& request, IR::ImageHeapView& result) {
KYTY_PROFILER_FUNCTION(); KYTY_PROFILER_FUNCTION();
const auto size = request.size & ~uint64_t {3}; const auto size = request.size & ~uint64_t {3};
if (size < MinTableBytes || request.count == 0u || request.step == 0u || if (request.count == 0u || request.step == 0u ||
((request.base | request.first | request.step) & 3u) != 0u) { ((request.base | request.first | request.step) & 3u) != 0u) {
return false; return false;
} }
auto memory = std::ranges::find_if( auto memory = std::ranges::find_if(
m_memories, [&](const auto& candidate) { return candidate->base == request.base; }); m_memories, [&](const auto& candidate) { return candidate->base == request.base; });
if (memory == m_memories.end() && size < MinTableBytes &&
m_sightings.try_emplace(request.base, m_draw).first->second == m_draw) {
return false;
}
// A table follows its descriptor's size, as the first program of a draw acquires it. // A table follows its descriptor's size, as the first program of a draw acquires it.
if (memory == m_memories.end() || ((*memory)->size != size && (*memory)->draw != m_draw)) { if (memory == m_memories.end() || ((*memory)->size != size && (*memory)->draw != m_draw)) {
if (!m_context.IsMapped(request.base, size)) return false; if (!m_context.IsMapped(request.base, size)) return false;
@@ -593,14 +597,32 @@ void ImageHeap::UnmapMemory(uint64_t address, uint64_t size) {
} else { } else {
m_unmapped.push_back({address, size}); m_unmapped.push_back({address, size});
} }
auto& scheduler = m_context.GetCommandScheduler();
std::scoped_lock lock {m_mutex}; std::scoped_lock lock {m_mutex};
std::erase_if(m_memories, [&](const std::unique_ptr<Memory>& memory) { std::erase_if(m_memories, [&](const std::unique_ptr<Memory>& memory) {
if (address >= memory->base + memory->size || memory->base >= address + size) { if (address >= memory->base + memory->size || memory->base >= address + size) {
return false; return false;
} }
Unwatch(*memory); Drop(*memory);
for (auto& view: memory->views) { return true;
});
}
void ImageHeap::RunGarbageCollector() {
// A smaller table rebuilt each frame moves to a new base. Larger ones keep their dead words.
std::scoped_lock lock {m_mutex};
std::erase_if(m_memories, [&](const std::unique_ptr<Memory>& memory) {
if (memory->size >= MinTableBytes || memory->draw > m_suspend_draw) return false;
Drop(*memory);
return true;
});
m_suspend_draw = m_draw;
m_sightings.clear();
}
void ImageHeap::Drop(Memory& memory) {
auto& scheduler = m_context.GetCommandScheduler();
Unwatch(memory);
for (auto& view: memory.views) {
std::erase(m_active, view.get()); std::erase(m_active, view.get());
for (const auto& [value, index]: view->elements) { for (const auto& [value, index]: view->elements) {
Unreference(m_elements[index]); Unreference(m_elements[index]);
@@ -611,8 +633,6 @@ void ImageHeap::UnmapMemory(uint64_t address, uint64_t size) {
scheduler.DeferOperation([buffer = std::move(view->buffer)] {}); scheduler.DeferOperation([buffer = std::move(view->buffer)] {});
} }
} }
return true;
});
} }
} // namespace Libs::Graphics } // namespace Libs::Graphics
@@ -26,7 +26,7 @@ struct ImageHeapTestAccess;
// of a table owns device slot words, one per table dword, that select an array element. CPU // of a table owns device slot words, one per table dword, that select an array element. CPU
// writes to a table fault in its snapshot, which each draw refreshes once; a view takes the // writes to a table fault in its snapshot, which each draw refreshes once; a view takes the
// changes when a draw first acquires it. Changes to the images behind elements bump // changes when a draw first acquires it. Changes to the images behind elements bump
// Image::heap_epoch. // Image::heap_epoch. A smaller table leaves at a suspend point if the frame did not acquire it.
class ImageHeap final: public ShaderRecompiler::IR::ImageHeapSource { class ImageHeap final: public ShaderRecompiler::IR::ImageHeapSource {
public: public:
ImageHeap(RenderContext& context, PageManager& page_manager); ImageHeap(RenderContext& context, PageManager& page_manager);
@@ -47,6 +47,8 @@ public:
// CPU write fault, on any thread. // CPU write fault, on any thread.
void InvalidateMemory(uint64_t address, uint64_t size); void InvalidateMemory(uint64_t address, uint64_t size);
void UnmapMemory(uint64_t address, uint64_t size); void UnmapMemory(uint64_t address, uint64_t size);
// At a suspend point.
void RunGarbageCollector();
private: private:
using DescriptorValue = ShaderRecompiler::IR::DescriptorValue; using DescriptorValue = ShaderRecompiler::IR::DescriptorValue;
@@ -107,6 +109,7 @@ private:
template <bool track> template <bool track>
void Watch(const Memory& memory, size_t begin, size_t end); void Watch(const Memory& memory, size_t begin, size_t end);
void Unwatch(const Memory& memory); void Unwatch(const Memory& memory);
void Drop(Memory& memory);
void UpdateProbes(View& view, uint64_t begin, uint64_t end); void UpdateProbes(View& view, uint64_t begin, uint64_t end);
void UpdateProbe(View& view, uint64_t probe); void UpdateProbe(View& view, uint64_t probe);
void Assign(View& view, size_t slot, uint32_t entry); void Assign(View& view, size_t slot, uint32_t entry);
@@ -126,8 +129,10 @@ private:
std::vector<std::unique_ptr<Memory>> m_memories; std::vector<std::unique_ptr<Memory>> m_memories;
std::vector<View*> m_active; // Acquired since the last commit. std::vector<View*> m_active; // Acquired since the last commit.
uint64_t m_draw = 1; uint64_t m_draw = 1;
uint64_t m_suspend_draw = 0; // m_draw at the last suspend point.
std::vector<Element> m_elements; std::vector<Element> m_elements;
std::vector<uint32_t> m_free; std::vector<uint32_t> m_free;
std::unordered_map<uint64_t, uint64_t> m_sightings; // Smaller tables: base, first draw.
std::deque<std::pair<uint64_t, uint32_t>> m_retired; // Tick, element. std::deque<std::pair<uint64_t, uint32_t>> m_retired; // Tick, element.
std::vector<std::pair<size_t, size_t>> m_runs; std::vector<std::pair<size_t, size_t>> m_runs;
std::vector<uint32_t> m_scratch; std::vector<uint32_t> m_scratch;
@@ -147,6 +147,7 @@ void RenderContext::RunGarbageCollector() {
m_fault_process_pending = false; m_fault_process_pending = false;
m_buffer_cache.ProcessFaultBuffer(); m_buffer_cache.ProcessFaultBuffer();
} }
m_image_heap.RunGarbageCollector();
m_texture_cache.ProcessDownloadImages(); m_texture_cache.ProcessDownloadImages();
m_texture_cache.RunGarbageCollector(); m_texture_cache.RunGarbageCollector();
m_buffer_cache.RunGarbageCollector(); m_buffer_cache.RunGarbageCollector();
+19
View File
@@ -5684,6 +5684,25 @@ public:
ImageHeapTestAccess::MemoryCount(heap) == 0u && ImageHeapTestAccess::MemoryCount(heap) == 0u &&
textures.GetImage(resident).heap_refs.load() == 0u, textures.GetImage(resident).heap_refs.load() == 0u,
"unmapping the table kept its images pinned"); "unmapping the table kept its images pinned");
// A smaller table enters once a second draw acquires it and leaves at the suspend point
// after a frame that did not acquire it. Larger tables stay.
const IR::ImageHeapRequest small{table, table_size / 2u, 32u, 0u, table_size / 64u, &sampled};
Require(name, "first small draw",
acquire(sampled, idle_table) && !heap.Acquire(small, view) &&
!heap.Acquire(small, view),
"a smaller table entered on its first draw");
draw();
Require(name, "second small draw", heap.Acquire(small, view),
"a smaller table was declined on its second draw");
context.RunGarbageCollector();
Require(name, "acquired frame", ImageHeapTestAccess::MemoryCount(heap) == 2u,
"a table left at the suspend point of a frame that acquired it");
draw();
context.RunGarbageCollector();
Require(name, "idle frame",
ImageHeapTestAccess::MemoryCount(heap) == 1u &&
textures.GetImage(resident).heap_refs.load() == 0u,
"a suspend point kept an idle smaller table pinned or dropped a larger one");
// The idle table stays watched; destroying the heap must unwatch it before the page // The idle table stays watched; destroying the heap must unwatch it before the page
// manager fails on live watches. // manager fails on live watches.
Require(name, "idle table", Require(name, "idle table",