// Copyright 2019 Dolphin Emulator Project // Licensed under GPLv2+ // Refer to the license.txt file included. #pragma once #include "../types.h" #include "../windows_headers.h" #include #include #include #include #include namespace D3D12 { // This class provides an abstraction for D3D12 descriptor heaps. struct DescriptorHandle final { enum : u32 { INVALID_INDEX = 0xFFFFFFFF }; D3D12_CPU_DESCRIPTOR_HANDLE cpu_handle{}; D3D12_GPU_DESCRIPTOR_HANDLE gpu_handle{}; u32 index = INVALID_INDEX; ALWAYS_INLINE operator bool() const { return index != INVALID_INDEX; } ALWAYS_INLINE operator D3D12_CPU_DESCRIPTOR_HANDLE() const { return cpu_handle; } ALWAYS_INLINE operator D3D12_GPU_DESCRIPTOR_HANDLE() const { return gpu_handle; } ALWAYS_INLINE void Clear() { cpu_handle = {}; gpu_handle = {}; index = INVALID_INDEX; } }; class DescriptorHeapManager final { public: DescriptorHeapManager(); ~DescriptorHeapManager(); 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, u32 count = 1); void Free(DescriptorHandle* handle, u32 count = 1); void Free(u32 index, u32 count = 1); private: Microsoft::WRL::ComPtr m_descriptor_heap; u32 m_num_descriptors = 0; u32 m_descriptor_increment_size = 0; D3D12_CPU_DESCRIPTOR_HANDLE m_heap_base_cpu = {}; D3D12_GPU_DESCRIPTOR_HANDLE m_heap_base_gpu = {}; static constexpr u32 BITSET_SIZE = 1024; using BitSetType = std::bitset; std::vector m_free_slots = {}; }; } // namespace D3D12