refactor: 将 tests/D3D12 的 CommandList 替换为 D3D12CommandList 类

- 将全局 ID3D12GraphicsCommandList 替换为 D3D12CommandList
- 更新初始化、Reset、Close 调用
- 修复 Initialize 中不应调用 Close() 的问题
- 测试通过,截图与 GT.ppm 完全匹配
This commit is contained in:
2026-03-15 18:10:16 +08:00
parent bf37b1c00c
commit 7050e88c49
4 changed files with 17 additions and 21 deletions

View File

@@ -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

View File

@@ -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);

View File

@@ -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<ID3D12CommandAllocator> 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;
}

View File

@@ -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);