feat: 实现D3D12CommandQueue和D3D12CommandAllocator

- 添加D3D12CommandQueue类封装ID3D12CommandQueue
- 添加D3D12CommandAllocator类封装ID3D12CommandAllocator
- 在D3D12Enum.h中添加CommandQueueType转换函数
- 在CMake中添加Res文件夹自动拷贝到输出目录
- 更新测试项目使用新的封装类
This commit is contained in:
2026-03-15 03:15:12 +08:00
parent cba4f9c838
commit 8fb11dc650
8 changed files with 205 additions and 11 deletions

View File

@@ -26,3 +26,10 @@ target_link_libraries(D3D12 PRIVATE
winmm
XCEngine
)
# Copy Res folder to output directory after build
add_custom_command(TARGET D3D12 POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
${CMAKE_SOURCE_DIR}/tests/D3D12/Res
$<TARGET_FILE_DIR:D3D12>/Res
)

View File

@@ -14,6 +14,8 @@
#include "XCEngine/RHI/Enums.h"
#include "XCEngine/RHI/D3D12/D3D12Enum.h"
#include "XCEngine/RHI/D3D12/D3D12Device.h"
#include "XCEngine/RHI/D3D12/D3D12CommandQueue.h"
#include "XCEngine/RHI/D3D12/D3D12CommandAllocator.h"
using namespace XCEngine::RHI;
@@ -26,7 +28,7 @@ using namespace XCEngine::RHI;
// D3D12 核心全局对象 (最小渲染所需)
//=================================================================================
XCEngine::RHI::D3D12Device gDevice;
ID3D12CommandQueue* gCommandQueue = nullptr;
XCEngine::RHI::D3D12CommandQueue gCommandQueue;
IDXGISwapChain3* gSwapChain = nullptr;
// 渲染目标 (SwapChain的后台Buffer)
@@ -41,7 +43,7 @@ UINT gRTVDescriptorSize = 0;
UINT gDSVDescriptorSize = 0;
// 命令相关
ID3D12CommandAllocator* gCommandAllocator = nullptr;
XCEngine::RHI::D3D12CommandAllocator gCommandAllocator;
ID3D12GraphicsCommandList* gCommandList = nullptr;
// 同步对象
@@ -567,11 +569,10 @@ bool InitD3D12(HWND inHWND, int inWidth, int inHeight) {
ID3D12Device* device = gDevice.GetDevice();
IDXGIFactory4* dxgiFactory = gDevice.GetFactory();
D3D12_COMMAND_QUEUE_DESC d3d12CommandQueueDesc = {};
HRESULT hResult = device->CreateCommandQueue(&d3d12CommandQueueDesc, IID_PPV_ARGS(&gCommandQueue));
if (FAILED(hResult)) {
if (!gCommandQueue.Initialize(device, XCEngine::RHI::CommandQueueType::Direct)) {
return false;
}
DXGI_SWAP_CHAIN_DESC swapChainDesc = {};
swapChainDesc.BufferCount = 2;
swapChainDesc.BufferDesc = {};
@@ -585,7 +586,7 @@ bool InitD3D12(HWND inHWND, int inWidth, int inHeight) {
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
IDXGISwapChain* swapChain = nullptr;
dxgiFactory->CreateSwapChain(gCommandQueue, &swapChainDesc, &swapChain);
dxgiFactory->CreateSwapChain(gCommandQueue.GetCommandQueue(), &swapChainDesc, &swapChain);
gSwapChain = static_cast<IDXGISwapChain3*>(swapChain);
D3D12_HEAP_PROPERTIES d3dHeapProperties = {};
@@ -642,8 +643,8 @@ bool InitD3D12(HWND inHWND, int inWidth, int inHeight) {
device->CreateDepthStencilView(gDSRT, &d3dDSViewDesc, gSwapChainDSVHeap->GetCPUDescriptorHandleForHeapStart());
device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&gCommandAllocator));
device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, gCommandAllocator, nullptr, IID_PPV_ARGS(&gCommandList));
gCommandAllocator.Initialize(device, XCEngine::RHI::CommandQueueType::Direct);
device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, gCommandAllocator.GetCommandAllocator(), nullptr, IID_PPV_ARGS(&gCommandList));
device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&gFence));
gFenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
@@ -655,7 +656,7 @@ bool InitD3D12(HWND inHWND, int inWidth, int inHeight) {
// 命令相关辅助函数
//=================================================================================
ID3D12CommandAllocator* GetCommandAllocator() {
return gCommandAllocator;
return gCommandAllocator.GetCommandAllocator();
}
ID3D12GraphicsCommandList* GetCommandList() {
@@ -676,9 +677,9 @@ void WaitForCompletionOfCommandList() {
void EndCommandList() {
gCommandList->Close();
ID3D12CommandList* ppCommandLists[] = { gCommandList };
gCommandQueue->ExecuteCommandLists(1, ppCommandLists);
gCommandQueue.ExecuteCommandLists(1, ppCommandLists);
gFenceValue += 1;
gCommandQueue->Signal(gFence, gFenceValue);
gCommandQueue.Signal(gFence, gFenceValue);
}
//=================================================================================