From 4eeecc16af590d0ab2e50134f4a9f0b331cbc032 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Mon, 19 Oct 2026 02:41:24 +1000 Subject: [PATCH] D3D12Device: Fix sampler group cache going stale and never resetting Destroyed samplers' indices could be reused, returning the old sampler. The reset threshold counted groups, which could never reach it before the heap filled, causing endless submits in PreDrawCheck(). --- src/util/d3d12_descriptor_heap_manager.h | 4 ++-- src/util/d3d12_device.cpp | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/util/d3d12_descriptor_heap_manager.h b/src/util/d3d12_descriptor_heap_manager.h index 60012e39f..ceeaf83f1 100644 --- a/src/util/d3d12_descriptor_heap_manager.h +++ b/src/util/d3d12_descriptor_heap_manager.h @@ -94,7 +94,7 @@ public: bool Allocate(u32 num_handles, D3D12DescriptorHandle* out_base_handle); void Reset(); -private: +protected: Microsoft::WRL::ComPtr m_descriptor_heap; u32 m_descriptor_increment_size = 0; u32 m_num_descriptors = 0; @@ -258,5 +258,5 @@ bool D3D12GroupedSamplerAllocator::ShouldReset() const { // We only reset the sampler heap if more than half of the descriptors are used. // This saves descriptor copying when there isn't a large number of sampler configs per frame. - return m_groups.size() >= (D3D12_MAX_SHADER_VISIBLE_SAMPLER_HEAP_SIZE / 2); + return (m_current_offset >= (m_num_descriptors / 2)); } diff --git a/src/util/d3d12_device.cpp b/src/util/d3d12_device.cpp index 8deb5d108..83d33914f 100644 --- a/src/util/d3d12_device.cpp +++ b/src/util/d3d12_device.cpp @@ -2201,6 +2201,10 @@ void D3D12Device::UnbindSampler(D3D12Sampler* sampler) m_dirty_flags |= DIRTY_FLAG_SAMPLERS; } } + + // Groups are keyed by descriptor index, which can be reused by a new sampler. + for (CommandList& res : m_command_lists) + res.sampler_allocator.InvalidateCache(); } void D3D12Device::RenderTextureMipmap(D3D12Texture* texture, u32 layer, u32 dst_level, u32 dst_width, u32 dst_height,