From aff9f1c25f0fac6f91099d51a8cfcc666bff38cf Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Fri, 30 Jul 2021 00:40:51 +1000 Subject: [PATCH] D3D12: Add helper for offsetting descriptors --- src/common/d3d12/descriptor_heap_manager.cpp | 32 ++++++++++++++------ src/common/d3d12/descriptor_heap_manager.h | 24 ++++++++++++--- 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/src/common/d3d12/descriptor_heap_manager.cpp b/src/common/d3d12/descriptor_heap_manager.cpp index 573e47514..5fbe7d78f 100644 --- a/src/common/d3d12/descriptor_heap_manager.cpp +++ b/src/common/d3d12/descriptor_heap_manager.cpp @@ -51,7 +51,7 @@ void DescriptorHeapManager::Destroy() m_free_slots.clear(); } -bool DescriptorHeapManager::Allocate(DescriptorHandle* handle) +bool DescriptorHeapManager::Allocate(DescriptorHandle* handle, u32 count /* = 1 */) { // Start past the temporary slots, no point in searching those. for (u32 group = 0; group < m_free_slots.size(); group++) @@ -64,11 +64,22 @@ bool DescriptorHeapManager::Allocate(DescriptorHandle* handle) for (; bit < BITSET_SIZE; bit++) { if (bs[bit]) - break; + { + u32 offset; + for (offset = 0; offset < count; offset++) + { + if (!bs[bit + offset]) + break; + } + + if (offset == count) + break; + } } u32 index = group * BITSET_SIZE + bit; - bs[bit] = false; + for (u32 offset = 0; offset < count; offset++) + bs[bit + offset] = false; handle->index = index; handle->cpu_handle.ptr = m_heap_base_cpu.ptr + index * m_descriptor_increment_size; @@ -80,21 +91,24 @@ bool DescriptorHeapManager::Allocate(DescriptorHandle* handle) return false; } -void DescriptorHeapManager::Free(u32 index) +void DescriptorHeapManager::Free(u32 index, u32 count /* = 1 */) { Assert(index < m_num_descriptors); - u32 group = index / BITSET_SIZE; - u32 bit = index % BITSET_SIZE; - m_free_slots[group][bit] = true; + for (u32 i = 0; i < count; i++, index++) + { + u32 group = index / BITSET_SIZE; + u32 bit = index % BITSET_SIZE; + m_free_slots[group][bit] = true; + } } -void DescriptorHeapManager::Free(DescriptorHandle* handle) +void DescriptorHeapManager::Free(DescriptorHandle* handle, u32 count /* = 1 */) { if (handle->index == DescriptorHandle::INVALID_INDEX) return; - Free(handle->index); + Free(handle->index, count); handle->Clear(); } diff --git a/src/common/d3d12/descriptor_heap_manager.h b/src/common/d3d12/descriptor_heap_manager.h index 4be5144ed..8cd80c98f 100644 --- a/src/common/d3d12/descriptor_heap_manager.h +++ b/src/common/d3d12/descriptor_heap_manager.h @@ -44,15 +44,29 @@ public: DescriptorHeapManager(); ~DescriptorHeapManager(); - ID3D12DescriptorHeap* GetDescriptorHeap() const { return m_descriptor_heap.Get(); } - u32 GetDescriptorIncrementSize() const { return m_descriptor_increment_size; } + ALWAYS_INLINE ID3D12DescriptorHeap* GetDescriptorHeap() const { return m_descriptor_heap.Get(); } + ALWAYS_INLINE u32 GetDescriptorIncrementSize() const { return m_descriptor_increment_size; } + + ALWAYS_INLINE D3D12_CPU_DESCRIPTOR_HANDLE OffsetCPUHandle(D3D12_CPU_DESCRIPTOR_HANDLE handle, u32 count) const + { + D3D12_CPU_DESCRIPTOR_HANDLE ret; + ret.ptr = handle.ptr + m_descriptor_increment_size * count; + return ret; + } + + ALWAYS_INLINE D3D12_GPU_DESCRIPTOR_HANDLE OffsetGPUHandle(D3D12_GPU_DESCRIPTOR_HANDLE handle, u32 count) const + { + D3D12_GPU_DESCRIPTOR_HANDLE ret; + ret.ptr = handle.ptr + m_descriptor_increment_size * count; + return ret; + } bool Create(ID3D12Device* device, D3D12_DESCRIPTOR_HEAP_TYPE type, u32 num_descriptors, bool shader_visible); void Destroy(); - bool Allocate(DescriptorHandle* handle); - void Free(DescriptorHandle* handle); - void Free(u32 index); + bool Allocate(DescriptorHandle* handle, u32 count = 1); + void Free(DescriptorHandle* handle, u32 count = 1); + void Free(u32 index, u32 count = 1); private: Microsoft::WRL::ComPtr m_descriptor_heap;