From 7050e88c497053c3343a715970ff9d07074ee694 Mon Sep 17 00:00:00 2001 From: ssdfasd <2156608475@qq.com> Date: Sun, 15 Mar 2026 18:10:16 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=B0=86=20tests/D3D12=20=E7=9A=84?= =?UTF-8?q?=20CommandList=20=E6=9B=BF=E6=8D=A2=E4=B8=BA=20D3D12CommandList?= =?UTF-8?q?=20=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将全局 ID3D12GraphicsCommandList 替换为 D3D12CommandList - 更新初始化、Reset、Close 调用 - 修复 Initialize 中不应调用 Close() 的问题 - 测试通过,截图与 GT.ppm 完全匹配 --- D3D12_engine_log.txt | 6 ++++++ .../XCEngine/RHI/D3D12/D3D12CommandList.h | 2 +- engine/src/RHI/D3D12CommandList.cpp | 17 +++-------------- tests/D3D12/main.cpp | 13 +++++++------ 4 files changed, 17 insertions(+), 21 deletions(-) diff --git a/D3D12_engine_log.txt b/D3D12_engine_log.txt index 0dc96b48..c0e78ebc 100644 --- a/D3D12_engine_log.txt +++ b/D3D12_engine_log.txt @@ -1,2 +1,8 @@ [2026-03-15 17:36:10] [DEBUG] [Rendering] [DEBUG] D3D12 Test Application Started +[2026-03-15 17:52:42] [DEBUG] [Rendering] [DEBUG] D3D12 Test Application Started + +[2026-03-15 17:52:53] [DEBUG] [Rendering] [DEBUG] D3D12 Test Application Started + +[2026-03-15 17:57:07] [DEBUG] [Rendering] [DEBUG] D3D12 Test Application Started + diff --git a/engine/include/XCEngine/RHI/D3D12/D3D12CommandList.h b/engine/include/XCEngine/RHI/D3D12/D3D12CommandList.h index fe41c5a5..5a5cb46e 100644 --- a/engine/include/XCEngine/RHI/D3D12/D3D12CommandList.h +++ b/engine/include/XCEngine/RHI/D3D12/D3D12CommandList.h @@ -41,7 +41,7 @@ public: D3D12CommandList(); ~D3D12CommandList(); - bool Initialize(ID3D12Device* device, CommandQueueType type = CommandQueueType::Direct); + bool Initialize(ID3D12Device* device, CommandQueueType type = CommandQueueType::Direct, ID3D12CommandAllocator* allocator = nullptr); void Shutdown(); void Reset(ID3D12CommandAllocator* allocator); diff --git a/engine/src/RHI/D3D12CommandList.cpp b/engine/src/RHI/D3D12CommandList.cpp index bb82e587..8b1cc00d 100644 --- a/engine/src/RHI/D3D12CommandList.cpp +++ b/engine/src/RHI/D3D12CommandList.cpp @@ -15,23 +15,13 @@ D3D12CommandList::~D3D12CommandList() { Shutdown(); } -bool D3D12CommandList::Initialize(ID3D12Device* device, CommandQueueType type) { +bool D3D12CommandList::Initialize(ID3D12Device* device, CommandQueueType type, ID3D12CommandAllocator* allocator) { D3D12_COMMAND_LIST_TYPE listType = ToD3D12(type); - ComPtr tempAllocator; - HRESULT hResult = device->CreateCommandAllocator( - listType, - IID_PPV_ARGS(&tempAllocator) - ); - - if (FAILED(hResult)) { - return false; - } - - hResult = device->CreateCommandList( + HRESULT hResult = device->CreateCommandList( 0, listType, - tempAllocator.Get(), + allocator, nullptr, IID_PPV_ARGS(&m_commandList) ); @@ -42,7 +32,6 @@ bool D3D12CommandList::Initialize(ID3D12Device* device, CommandQueueType type) { m_type = type; - m_commandList->Close(); return true; } diff --git a/tests/D3D12/main.cpp b/tests/D3D12/main.cpp index 3d3f2080..7673158f 100644 --- a/tests/D3D12/main.cpp +++ b/tests/D3D12/main.cpp @@ -17,6 +17,7 @@ #include "XCEngine/RHI/D3D12/D3D12Device.h" #include "XCEngine/RHI/D3D12/D3D12CommandQueue.h" #include "XCEngine/RHI/D3D12/D3D12CommandAllocator.h" +#include "XCEngine/RHI/D3D12/D3D12CommandList.h" #include "XCEngine/RHI/D3D12/D3D12Fence.h" #include "XCEngine/RHI/D3D12/D3D12Screenshot.h" #include "XCEngine/Debug/Logger.h" @@ -61,7 +62,7 @@ UINT gDSVDescriptorSize = 0; // 命令相关 XCEngine::RHI::D3D12CommandAllocator gCommandAllocator; -ID3D12GraphicsCommandList* gCommandList = nullptr; +XCEngine::RHI::D3D12CommandList gCommandList; // 同步对象 XCEngine::RHI::D3D12Fence gFence; @@ -660,7 +661,7 @@ bool InitD3D12(HWND inHWND, int inWidth, int inHeight) { device->CreateDepthStencilView(gDSRT, &d3dDSViewDesc, gSwapChainDSVHeap->GetCPUDescriptorHandleForHeapStart()); gCommandAllocator.Initialize(device, XCEngine::RHI::CommandQueueType::Direct); - device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, gCommandAllocator.GetCommandAllocator(), nullptr, IID_PPV_ARGS(&gCommandList)); + gCommandList.Initialize(device, XCEngine::RHI::CommandQueueType::Direct, gCommandAllocator.GetCommandAllocator()); gFence.Initialize(device, 0); @@ -675,7 +676,7 @@ ID3D12CommandAllocator* GetCommandAllocator() { } ID3D12GraphicsCommandList* GetCommandList() { - return gCommandList; + return gCommandList.GetCommandList(); } void WaitForCompletionOfCommandList() { @@ -694,8 +695,8 @@ void WaitForCompletionOfCommandList() { // 关闭CommandList → ExecuteCommandLists → Signal Fence //================================================================================= void EndCommandList() { - gCommandList->Close(); - ID3D12CommandList* ppCommandLists[] = { gCommandList }; + gCommandList.Close(); + ID3D12CommandList* ppCommandLists[] = { gCommandList.GetCommandList() }; gCommandQueue.ExecuteCommandLists(1, ppCommandLists); gFenceValue += 1; gCommandQueue.Signal(gFence.GetFence(), gFenceValue); @@ -942,7 +943,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine float timeSinceAppStartInSecond = float(timeSinceAppStartInMS) / 1000.0f; color[0] = timeSinceAppStartInSecond; commandAllocator->Reset(); - commandList->Reset(commandAllocator, nullptr); + gCommandList.Reset(gCommandAllocator.GetCommandAllocator()); BeginRenderToSwapChain(commandList); commandList->SetPipelineState(pso); commandList->SetGraphicsRootSignature(rootSignature);