feat: 实现D3D12Device类,整合D3D12Enum.h转换函数

- 简化D3D12Device,仅封装ID3D12Device和IDXGIFactory
- 将D3D12Common.h中的转换函数合并到D3D12Enum.h
- 添加ResourceStates枚举到Enums.h
- 更新测试项目使用新的D3D12Device类
- 更新CMake配置
This commit is contained in:
2026-03-15 03:02:15 +08:00
parent b2c7627a1b
commit cba4f9c838
8 changed files with 429 additions and 63 deletions

View File

@@ -13,6 +13,7 @@
#include "stbi/stb_image.h"
#include "XCEngine/RHI/Enums.h"
#include "XCEngine/RHI/D3D12/D3D12Enum.h"
#include "XCEngine/RHI/D3D12/D3D12Device.h"
using namespace XCEngine::RHI;
@@ -24,7 +25,7 @@ using namespace XCEngine::RHI;
//=================================================================================
// D3D12 核心全局对象 (最小渲染所需)
//=================================================================================
ID3D12Device* gD3D12Device = nullptr;
XCEngine::RHI::D3D12Device gDevice;
ID3D12CommandQueue* gCommandQueue = nullptr;
IDXGISwapChain3* gSwapChain = nullptr;
@@ -258,7 +259,7 @@ ID3D12RootSignature* InitRootSignature() {
ID3DBlob* signature;
HRESULT hResult = D3D12SerializeRootSignature(&rootSignatureDesc, D3D_ROOT_SIGNATURE_VERSION_1, &signature, nullptr);
ID3D12RootSignature* d3d12RootSignature;
gD3D12Device->CreateRootSignature(
gDevice.GetDevice()->CreateRootSignature(
0, signature->GetBufferPointer(), signature->GetBufferSize(),
IID_PPV_ARGS(&d3d12RootSignature));
@@ -311,7 +312,7 @@ ID3D12Resource* CreateConstantBufferObject(int inDataLen) {
d3d12ResourceDesc.Flags = D3D12_RESOURCE_FLAG_NONE;
ID3D12Resource* bufferObject = nullptr;
gD3D12Device->CreateCommittedResource(
gDevice.GetDevice()->CreateCommittedResource(
&d3dHeapProperties,
D3D12_HEAP_FLAG_NONE,
&d3d12ResourceDesc,
@@ -355,7 +356,7 @@ ID3D12Resource* CreateBufferObject(ID3D12GraphicsCommandList* inCommandList,
d3d12ResourceDesc.Flags = D3D12_RESOURCE_FLAG_NONE;
ID3D12Resource* bufferObject = nullptr;
gD3D12Device->CreateCommittedResource(
gDevice.GetDevice()->CreateCommittedResource(
&d3dHeapProperties,
D3D12_HEAP_FLAG_NONE,
&d3d12ResourceDesc,
@@ -368,13 +369,13 @@ ID3D12Resource* CreateBufferObject(ID3D12GraphicsCommandList* inCommandList,
UINT64 rowSizeInBytes = 0;
UINT rowUsed = 0;
D3D12_PLACED_SUBRESOURCE_FOOTPRINT subresourceFootprint;
gD3D12Device->GetCopyableFootprints(&d3d12ResourceDesc, 0, 1, 0,
gDevice.GetDevice()->GetCopyableFootprints(&d3d12ResourceDesc, 0, 1, 0,
&subresourceFootprint, &rowUsed, &rowSizeInBytes, &memorySizeUsed);
ID3D12Resource* tempBufferObject = nullptr;
d3dHeapProperties = {};
d3dHeapProperties.Type = D3D12_HEAP_TYPE_UPLOAD;
gD3D12Device->CreateCommittedResource(
gDevice.GetDevice()->CreateCommittedResource(
&d3dHeapProperties,
D3D12_HEAP_FLAG_NONE,
&d3d12ResourceDesc,
@@ -421,7 +422,7 @@ ID3D12Resource* CreateTexture2D(ID3D12GraphicsCommandList* inCommandList,
d3d12ResourceDesc.Flags = D3D12_RESOURCE_FLAG_NONE;
ID3D12Resource* texture = nullptr;
gD3D12Device->CreateCommittedResource(&d3dHeapProperties,
gDevice.GetDevice()->CreateCommittedResource(&d3dHeapProperties,
D3D12_HEAP_FLAG_NONE,
&d3d12ResourceDesc,
D3D12_RESOURCE_STATE_COPY_DEST,
@@ -433,7 +434,7 @@ ID3D12Resource* CreateTexture2D(ID3D12GraphicsCommandList* inCommandList,
UINT64 rowSizeInBytes = 0;
UINT rowUsed = 0;
D3D12_PLACED_SUBRESOURCE_FOOTPRINT subresourceFootprint;
gD3D12Device->GetCopyableFootprints(&d3d12ResourceDesc, 0, 1, 0,
gDevice.GetDevice()->GetCopyableFootprints(&d3d12ResourceDesc, 0, 1, 0,
&subresourceFootprint, &rowUsed, &rowSizeInBytes, &memorySizeUsed);
ID3D12Resource* tempBufferObject = nullptr;
@@ -453,7 +454,7 @@ ID3D12Resource* CreateTexture2D(ID3D12GraphicsCommandList* inCommandList,
d3d12TempResourceDesc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
d3d12TempResourceDesc.Flags = D3D12_RESOURCE_FLAG_NONE;
gD3D12Device->CreateCommittedResource(
gDevice.GetDevice()->CreateCommittedResource(
&d3dTempHeapProperties,
D3D12_HEAP_FLAG_NONE,
&d3d12TempResourceDesc,
@@ -537,7 +538,7 @@ ID3D12PipelineState* CreatePSO(ID3D12RootSignature* inID3D12RootSignature,
psoDesc.NumRenderTargets = 1;
ID3D12PipelineState* d3d12PSO = nullptr;
HRESULT hResult = gD3D12Device->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(&d3d12PSO));
HRESULT hResult = gDevice.GetDevice()->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(&d3d12PSO));
if (FAILED(hResult)) {
return nullptr;
}
@@ -559,47 +560,15 @@ ID3D12PipelineState* CreatePSO(ID3D12RootSignature* inID3D12RootSignature,
// 10. 创建Fence (同步)
//=================================================================================
bool InitD3D12(HWND inHWND, int inWidth, int inHeight) {
HRESULT hResult;
UINT dxgiFactoryFlags = 0;
#ifdef _DEBUG
{
ID3D12Debug* debugController = nullptr;
if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&debugController)))) {
debugController->EnableDebugLayer();
dxgiFactoryFlags |= DXGI_CREATE_FACTORY_DEBUG;
}
}
#endif
IDXGIFactory4* dxgiFactory;
hResult = CreateDXGIFactory2(dxgiFactoryFlags, IID_PPV_ARGS(&dxgiFactory));
if (FAILED(hResult)) {
return false;
}
IDXGIAdapter1* adapter;
int adapterIndex = 0;
bool adapterFound = false;
while (dxgiFactory->EnumAdapters1(adapterIndex, &adapter) != DXGI_ERROR_NOT_FOUND) {
DXGI_ADAPTER_DESC1 desc;
adapter->GetDesc1(&desc);
if (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE) {
continue;
}
hResult = D3D12CreateDevice(adapter, D3D_FEATURE_LEVEL_11_0, __uuidof(ID3D12Device), nullptr);
if (SUCCEEDED(hResult)) {
adapterFound = true;
break;
}
adapterIndex++;
}
if (false == adapterFound) {
return false;
}
hResult = D3D12CreateDevice(adapter, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&gD3D12Device));
if (FAILED(hResult)) {
if (!gDevice.Initialize()) {
return false;
}
ID3D12Device* device = gDevice.GetDevice();
IDXGIFactory4* dxgiFactory = gDevice.GetFactory();
D3D12_COMMAND_QUEUE_DESC d3d12CommandQueueDesc = {};
hResult = gD3D12Device->CreateCommandQueue(&d3d12CommandQueueDesc, IID_PPV_ARGS(&gCommandQueue));
HRESULT hResult = device->CreateCommandQueue(&d3d12CommandQueueDesc, IID_PPV_ARGS(&gCommandQueue));
if (FAILED(hResult)) {
return false;
}
@@ -640,7 +609,7 @@ bool InitD3D12(HWND inHWND, int inWidth, int inHeight) {
dsClearValue.DepthStencil.Depth = 1.0f;
dsClearValue.DepthStencil.Stencil = 0;
gD3D12Device->CreateCommittedResource(&d3dHeapProperties,
device->CreateCommittedResource(&d3dHeapProperties,
D3D12_HEAP_FLAG_NONE,
&d3d12ResourceDesc,
D3D12_RESOURCE_STATE_DEPTH_WRITE,
@@ -651,32 +620,32 @@ bool InitD3D12(HWND inHWND, int inWidth, int inHeight) {
D3D12_DESCRIPTOR_HEAP_DESC d3dDescriptorHeapDescRTV = {};
d3dDescriptorHeapDescRTV.NumDescriptors = 2;
d3dDescriptorHeapDescRTV.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
gD3D12Device->CreateDescriptorHeap(&d3dDescriptorHeapDescRTV, IID_PPV_ARGS(&gSwapChainRTVHeap));
gRTVDescriptorSize = gD3D12Device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
device->CreateDescriptorHeap(&d3dDescriptorHeapDescRTV, IID_PPV_ARGS(&gSwapChainRTVHeap));
gRTVDescriptorSize = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
D3D12_DESCRIPTOR_HEAP_DESC d3dDescriptorHeapDescDSV = {};
d3dDescriptorHeapDescDSV.NumDescriptors = 1;
d3dDescriptorHeapDescDSV.Type = D3D12_DESCRIPTOR_HEAP_TYPE_DSV;
gD3D12Device->CreateDescriptorHeap(&d3dDescriptorHeapDescDSV, IID_PPV_ARGS(&gSwapChainDSVHeap));
gDSVDescriptorSize = gD3D12Device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_DSV);
device->CreateDescriptorHeap(&d3dDescriptorHeapDescDSV, IID_PPV_ARGS(&gSwapChainDSVHeap));
gDSVDescriptorSize = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_DSV);
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;
rtvPointer.ptr = rtvHeapStart.ptr + i * gRTVDescriptorSize;
gD3D12Device->CreateRenderTargetView(gColorRTs[i], nullptr, rtvPointer);
device->CreateRenderTargetView(gColorRTs[i], nullptr, rtvPointer);
}
D3D12_DEPTH_STENCIL_VIEW_DESC d3dDSViewDesc = {};
d3dDSViewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
d3dDSViewDesc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2D;
gD3D12Device->CreateDepthStencilView(gDSRT, &d3dDSViewDesc, gSwapChainDSVHeap->GetCPUDescriptorHandleForHeapStart());
device->CreateDepthStencilView(gDSRT, &d3dDSViewDesc, gSwapChainDSVHeap->GetCPUDescriptorHandleForHeapStart());
gD3D12Device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&gCommandAllocator));
gD3D12Device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, gCommandAllocator, nullptr, IID_PPV_ARGS(&gCommandList));
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));
gD3D12Device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&gFence));
device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&gFence));
gFenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
return true;
@@ -757,7 +726,7 @@ void SwapD3D12Buffers() {
// 获取D3D12设备
//=================================================================================
ID3D12Device* GetD3DDevice() {
return gD3D12Device;
return gDevice.GetDevice();
}
//=================================================================================