From 7f064e9e71e353c7033ded316f6c5f23a537906b Mon Sep 17 00:00:00 2001 From: ssdfasd <2156608475@qq.com> Date: Sun, 15 Mar 2026 18:17:59 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=9E=E7=8E=B0=20D3D12DescriptorHea?= =?UTF-8?q?p=20=E6=8F=8F=E8=BF=B0=E7=AC=A6=E5=A0=86=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加 D3D12DescriptorHeap.h 头文件 - 实现 ID3D12DescriptorHeap 封装 - 支持 RTV、DSV、CBV_SRV_UAV、Sampler 堆类型 - 支持 GPU 可见描述符堆 - 添加 GetCPUDescriptorHandle、GetGPUDescriptorHandle 等方法 - 测试通过 --- engine/CMakeLists.txt | 2 + .../XCEngine/RHI/D3D12/D3D12DescriptorHeap.h | 43 +++++++++++++ engine/src/RHI/D3D12DescriptorHeap.cpp | 62 +++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 engine/include/XCEngine/RHI/D3D12/D3D12DescriptorHeap.h create mode 100644 engine/src/RHI/D3D12DescriptorHeap.cpp diff --git a/engine/CMakeLists.txt b/engine/CMakeLists.txt index 0b884cce..8fcbb46b 100644 --- a/engine/CMakeLists.txt +++ b/engine/CMakeLists.txt @@ -88,12 +88,14 @@ add_library(XCEngine STATIC include/XCEngine/RHI/D3D12/D3D12CommandQueue.h include/XCEngine/RHI/D3D12/D3D12CommandAllocator.h include/XCEngine/RHI/D3D12/D3D12CommandList.h + include/XCEngine/RHI/D3D12/D3D12DescriptorHeap.h include/XCEngine/RHI/D3D12/D3D12Fence.h include/XCEngine/RHI/D3D12/D3D12Screenshot.h src/RHI/D3D12Device.cpp src/RHI/D3D12CommandQueue.cpp src/RHI/D3D12CommandAllocator.cpp src/RHI/D3D12CommandList.cpp + src/RHI/D3D12DescriptorHeap.cpp src/RHI/D3D12Fence.cpp src/RHI/D3D12Screenshot.cpp ) diff --git a/engine/include/XCEngine/RHI/D3D12/D3D12DescriptorHeap.h b/engine/include/XCEngine/RHI/D3D12/D3D12DescriptorHeap.h new file mode 100644 index 00000000..b0e467f9 --- /dev/null +++ b/engine/include/XCEngine/RHI/D3D12/D3D12DescriptorHeap.h @@ -0,0 +1,43 @@ +#pragma once + +#include +#include + +#include "../Enums.h" +#include "D3D12Enum.h" + +using Microsoft::WRL::ComPtr; + +namespace XCEngine { +namespace RHI { + +class D3D12DescriptorHeap { +public: + D3D12DescriptorHeap(); + ~D3D12DescriptorHeap(); + + bool Initialize(ID3D12Device* device, DescriptorHeapType type, uint32_t numDescriptors, bool shaderVisible = false); + void Shutdown(); + + ID3D12DescriptorHeap* GetDescriptorHeap() const { return m_descriptorHeap.Get(); } + + D3D12_CPU_DESCRIPTOR_HANDLE GetCPUDescriptorHandle(uint32_t index) const; + D3D12_GPU_DESCRIPTOR_HANDLE GetGPUDescriptorHandle(uint32_t index) const; + + uint32_t GetDescriptorCount() const { return m_numDescriptors; } + DescriptorHeapType GetType() const { return m_type; } + uint32_t GetDescriptorSize() const { return m_descriptorSize; } + + D3D12_CPU_DESCRIPTOR_HANDLE GetCPUDescriptorHandleForHeapStart() const; + D3D12_GPU_DESCRIPTOR_HANDLE GetGPUDescriptorHandleForHeapStart() const; + +private: + ComPtr m_descriptorHeap; + DescriptorHeapType m_type; + uint32_t m_numDescriptors; + uint32_t m_descriptorSize; + bool m_shaderVisible; +}; + +} // namespace RHI +} // namespace XCEngine diff --git a/engine/src/RHI/D3D12DescriptorHeap.cpp b/engine/src/RHI/D3D12DescriptorHeap.cpp new file mode 100644 index 00000000..de4d1e57 --- /dev/null +++ b/engine/src/RHI/D3D12DescriptorHeap.cpp @@ -0,0 +1,62 @@ +#include "XCEngine/RHI/D3D12/D3D12DescriptorHeap.h" + +namespace XCEngine { +namespace RHI { + +D3D12DescriptorHeap::D3D12DescriptorHeap() + : m_type(DescriptorHeapType::CBV_SRV_UAV) + , m_numDescriptors(0) + , m_descriptorSize(0) + , m_shaderVisible(false) { +} + +D3D12DescriptorHeap::~D3D12DescriptorHeap() { + Shutdown(); +} + +bool D3D12DescriptorHeap::Initialize(ID3D12Device* device, DescriptorHeapType type, uint32_t numDescriptors, bool shaderVisible) { + D3D12_DESCRIPTOR_HEAP_DESC desc = {}; + desc.Type = ToD3D12(type); + desc.NumDescriptors = numDescriptors; + desc.Flags = shaderVisible ? D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE : D3D12_DESCRIPTOR_HEAP_FLAG_NONE; + desc.NodeMask = 0; + + HRESULT hResult = device->CreateDescriptorHeap(&desc, IID_PPV_ARGS(&m_descriptorHeap)); + if (FAILED(hResult)) { + return false; + } + + m_type = type; + m_numDescriptors = numDescriptors; + m_shaderVisible = shaderVisible; + m_descriptorSize = device->GetDescriptorHandleIncrementSize(ToD3D12(type)); + + return true; +} + +void D3D12DescriptorHeap::Shutdown() { + m_descriptorHeap.Reset(); +} + +D3D12_CPU_DESCRIPTOR_HANDLE D3D12DescriptorHeap::GetCPUDescriptorHandle(uint32_t index) const { + D3D12_CPU_DESCRIPTOR_HANDLE handle = m_descriptorHeap->GetCPUDescriptorHandleForHeapStart(); + handle.ptr += index * m_descriptorSize; + return handle; +} + +D3D12_GPU_DESCRIPTOR_HANDLE D3D12DescriptorHeap::GetGPUDescriptorHandle(uint32_t index) const { + D3D12_GPU_DESCRIPTOR_HANDLE handle = m_descriptorHeap->GetGPUDescriptorHandleForHeapStart(); + handle.ptr += index * m_descriptorSize; + return handle; +} + +D3D12_CPU_DESCRIPTOR_HANDLE D3D12DescriptorHeap::GetCPUDescriptorHandleForHeapStart() const { + return m_descriptorHeap->GetCPUDescriptorHandleForHeapStart(); +} + +D3D12_GPU_DESCRIPTOR_HANDLE D3D12DescriptorHeap::GetGPUDescriptorHandleForHeapStart() const { + return m_descriptorHeap->GetGPUDescriptorHandleForHeapStart(); +} + +} // namespace RHI +} // namespace XCEngine