From 407fe0fd32ce0719bc6db647c1daf98f762685bb Mon Sep 17 00:00:00 2001 From: ssdfasd <2156608475@qq.com> Date: Sun, 15 Mar 2026 18:21:07 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=B0=86=20tests/D3D12=20=E7=9A=84?= =?UTF-8?q?=20DescriptorHeap=20=E6=9B=BF=E6=8D=A2=E4=B8=BA=20D3D12Descript?= =?UTF-8?q?orHeap?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - gSwapChainRTVHeap, gSwapChainDSVHeap 替换为 D3D12DescriptorHeap - srvHeap 替换为 D3D12DescriptorHeap - 使用 Initialize 方法替代原生 CreateDescriptorHeap - 测试通过 --- tests/D3D12/main.cpp | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/tests/D3D12/main.cpp b/tests/D3D12/main.cpp index f06f2750..6821bca5 100644 --- a/tests/D3D12/main.cpp +++ b/tests/D3D12/main.cpp @@ -18,6 +18,7 @@ #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" #include "XCEngine/Debug/Logger.h" @@ -55,8 +56,8 @@ ID3D12Resource* gColorRTs[2]; // 颜色缓冲 (双缓冲) int gCurrentRTIndex = 0; // 描述符堆 -ID3D12DescriptorHeap* gSwapChainRTVHeap = nullptr; // RTV堆 -ID3D12DescriptorHeap* gSwapChainDSVHeap = nullptr; // DSV堆 +XCEngine::RHI::D3D12DescriptorHeap gSwapChainRTVHeap; // RTV堆 +XCEngine::RHI::D3D12DescriptorHeap gSwapChainDSVHeap; // DSV堆 UINT gRTVDescriptorSize = 0; UINT gDSVDescriptorSize = 0; @@ -638,16 +639,16 @@ bool InitD3D12(HWND inHWND, int inWidth, int inHeight) { D3D12_DESCRIPTOR_HEAP_DESC d3dDescriptorHeapDescRTV = {}; d3dDescriptorHeapDescRTV.NumDescriptors = 2; d3dDescriptorHeapDescRTV.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV; - device->CreateDescriptorHeap(&d3dDescriptorHeapDescRTV, IID_PPV_ARGS(&gSwapChainRTVHeap)); + gSwapChainRTVHeap.Initialize(device, XCEngine::RHI::DescriptorHeapType::RTV, 2); gRTVDescriptorSize = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV); D3D12_DESCRIPTOR_HEAP_DESC d3dDescriptorHeapDescDSV = {}; d3dDescriptorHeapDescDSV.NumDescriptors = 1; d3dDescriptorHeapDescDSV.Type = D3D12_DESCRIPTOR_HEAP_TYPE_DSV; - device->CreateDescriptorHeap(&d3dDescriptorHeapDescDSV, IID_PPV_ARGS(&gSwapChainDSVHeap)); + gSwapChainDSVHeap.Initialize(device, XCEngine::RHI::DescriptorHeapType::DSV, 1); gDSVDescriptorSize = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_DSV); - D3D12_CPU_DESCRIPTOR_HANDLE rtvHeapStart = gSwapChainRTVHeap->GetCPUDescriptorHandleForHeapStart(); + D3D12_CPU_DESCRIPTOR_HANDLE rtvHeapStart = gSwapChainRTVHeap.GetCPUDescriptorHandleForHeapStart(); for (int i = 0; i < 2; i++) { gSwapChain->GetBuffer(i, IID_PPV_ARGS(&gColorRTs[i])); D3D12_CPU_DESCRIPTOR_HANDLE rtvPointer; @@ -658,7 +659,7 @@ bool InitD3D12(HWND inHWND, int inWidth, int inHeight) { d3dDSViewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; d3dDSViewDesc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2D; - device->CreateDepthStencilView(gDSRT, &d3dDSViewDesc, gSwapChainDSVHeap->GetCPUDescriptorHandleForHeapStart()); + device->CreateDepthStencilView(gDSRT, &d3dDSViewDesc, gSwapChainDSVHeap.GetCPUDescriptorHandleForHeapStart()); gCommandAllocator.Initialize(device, XCEngine::RHI::CommandQueueType::Direct); gCommandList.Initialize(device, XCEngine::RHI::CommandQueueType::Direct, gCommandAllocator.GetCommandAllocator()); @@ -715,8 +716,8 @@ void BeginRenderToSwapChain(ID3D12GraphicsCommandList* inCommandList) { D3D12_RESOURCE_BARRIER barrier = InitResourceBarrier(gColorRTs[gCurrentRTIndex], D3D12_RESOURCE_STATE_PRESENT, D3D12_RESOURCE_STATE_RENDER_TARGET); inCommandList->ResourceBarrier(1, &barrier); D3D12_CPU_DESCRIPTOR_HANDLE colorRT, dsv; - dsv.ptr = gSwapChainDSVHeap->GetCPUDescriptorHandleForHeapStart().ptr; - colorRT.ptr = gSwapChainRTVHeap->GetCPUDescriptorHandleForHeapStart().ptr + gCurrentRTIndex * gRTVDescriptorSize; + dsv.ptr = gSwapChainDSVHeap.GetCPUDescriptorHandleForHeapStart().ptr; + colorRT.ptr = gSwapChainRTVHeap.GetCPUDescriptorHandleForHeapStart().ptr + gCurrentRTIndex * gRTVDescriptorSize; inCommandList->OMSetRenderTargets(1, &colorRT, FALSE, &dsv); D3D12_VIEWPORT viewport = { 0.0f,0.0f,1280.0f,720.0f }; D3D12_RECT scissorRect = { 0,0,1280,720 }; @@ -894,14 +895,10 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine delete[] pixels; ID3D12Device* d3dDevice = GetD3DDevice(); - ID3D12DescriptorHeap* srvHeap = nullptr; - D3D12_DESCRIPTOR_HEAP_DESC d3dDescriptorHeapDescSRV = {}; - d3dDescriptorHeapDescSRV.NumDescriptors = 3; - d3dDescriptorHeapDescSRV.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV; - d3dDescriptorHeapDescSRV.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE; - d3dDevice->CreateDescriptorHeap(&d3dDescriptorHeapDescSRV, IID_PPV_ARGS(&srvHeap)); + XCEngine::RHI::D3D12DescriptorHeap srvHeap; + srvHeap.Initialize(d3dDevice, XCEngine::RHI::DescriptorHeapType::CBV_SRV_UAV, 3, true); - ID3D12DescriptorHeap* descriptorHeaps[] = { srvHeap }; + ID3D12DescriptorHeap* descriptorHeaps[] = { srvHeap.GetDescriptorHeap() }; D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc = {}; srvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; @@ -909,7 +906,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine srvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D; srvDesc.Texture2D.MipLevels = 1; - D3D12_CPU_DESCRIPTOR_HANDLE srvHeapPtr = srvHeap->GetCPUDescriptorHandleForHeapStart(); + D3D12_CPU_DESCRIPTOR_HANDLE srvHeapPtr = srvHeap.GetCPUDescriptorHandleForHeapStart(); d3dDevice->CreateShaderResourceView(texture, &srvDesc, srvHeapPtr); srvHeapPtr.ptr += d3dDevice->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); @@ -949,7 +946,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine gCommandList.SetDescriptorHeaps(_countof(descriptorHeaps), descriptorHeaps); gCommandList.SetGraphicsRootConstantBufferView(0, cb->GetGPUVirtualAddress()); gCommandList.SetGraphicsRoot32BitConstants(1, 4, color, 0); - gCommandList.SetGraphicsRootDescriptorTable(2, srvHeap->GetGPUDescriptorHandleForHeapStart()); + gCommandList.SetGraphicsRootDescriptorTable(2, srvHeap.GetGPUDescriptorHandleForHeapStart()); gCommandList.SetGraphicsRootShaderResourceView(3, sb->GetGPUVirtualAddress()); gCommandList.SetPrimitiveTopology(XCEngine::RHI::PrimitiveTopology::TriangleList); staticMeshComponent.Render(gCommandList.GetCommandList());