Add IRHIDevice interface implementation to D3D12Device
- D3D12Device now inherits from IRHIDevice - Implement factory methods: CreateCommandQueue, CreateCommandList, CreateFence, etc. - Make D3D12CommandQueue implement ICommandQueue - Add backward-compatible overloads for existing main.cpp code - Remove duplicate Viewport/Rect definitions from D3D12CommandList.h - Update main.cpp to use IRHIDevice* pointer
This commit is contained in:
@@ -13,6 +13,8 @@
|
||||
#include <cstring>
|
||||
#include "stbi/stb_image.h"
|
||||
#include "XCEngine/RHI/Enums.h"
|
||||
#include "XCEngine/RHI/Types.h"
|
||||
#include "XCEngine/RHI/RHIDevice.h"
|
||||
#include "XCEngine/RHI/D3D12/D3D12Enum.h"
|
||||
#include "XCEngine/RHI/D3D12/D3D12Device.h"
|
||||
#include "XCEngine/RHI/D3D12/D3D12CommandQueue.h"
|
||||
@@ -54,7 +56,8 @@ void Log(const char* format, ...) {
|
||||
//=================================================================================
|
||||
// D3D12 核心全局对象 (最小渲染所需)
|
||||
//=================================================================================
|
||||
XCEngine::RHI::D3D12Device gDevice;
|
||||
XCEngine::RHI::IRHIDevice* gDevice = nullptr;
|
||||
XCEngine::RHI::D3D12Device gD3D12Device; // 底层实现
|
||||
XCEngine::RHI::D3D12CommandQueue gCommandQueue;
|
||||
XCEngine::RHI::D3D12SwapChain gSwapChain;
|
||||
|
||||
@@ -166,7 +169,7 @@ public:
|
||||
mVertexCount = temp;
|
||||
mVertexData = new StaticMeshComponentVertexData[mVertexCount];
|
||||
fread(mVertexData, 1, sizeof(StaticMeshComponentVertexData) * mVertexCount, pFile);
|
||||
mVBO.InitializeWithData(gDevice.GetDevice(), inCommandList, mVertexData,
|
||||
mVBO.InitializeWithData(gD3D12Device.GetDevice(), inCommandList, mVertexData,
|
||||
sizeof(StaticMeshComponentVertexData) * mVertexCount,
|
||||
D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER);
|
||||
mVBOView.BufferLocation = mVBO.GetGPUVirtualAddress();
|
||||
@@ -185,7 +188,7 @@ public:
|
||||
submesh->mIndexCount = temp;
|
||||
unsigned int* indexes = new unsigned int[temp];
|
||||
fread(indexes, 1, sizeof(unsigned int) * temp, pFile);
|
||||
submesh->mIBO.InitializeWithData(gDevice.GetDevice(), inCommandList, indexes,
|
||||
submesh->mIBO.InitializeWithData(gD3D12Device.GetDevice(), inCommandList, indexes,
|
||||
sizeof(unsigned int) * temp,
|
||||
D3D12_RESOURCE_STATE_INDEX_BUFFER);
|
||||
|
||||
@@ -269,7 +272,7 @@ ID3D12RootSignature* InitRootSignature() {
|
||||
rootSignatureDesc.pStaticSamplers = samplerDesc;
|
||||
rootSignatureDesc.Flags = D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT;
|
||||
|
||||
gRootSignature.Initialize(gDevice.GetDevice(), rootSignatureDesc);
|
||||
gRootSignature.Initialize(gD3D12Device.GetDevice(), rootSignatureDesc);
|
||||
|
||||
return gRootSignature.GetRootSignature();
|
||||
}
|
||||
@@ -327,7 +330,7 @@ ID3D12PipelineState* CreatePSO(ID3D12RootSignature* inID3D12RootSignature,
|
||||
psoDesc.NumRenderTargets = 1;
|
||||
ID3D12PipelineState* d3d12PSO = nullptr;
|
||||
|
||||
gPipelineState.Initialize(gDevice.GetDevice(), psoDesc);
|
||||
gPipelineState.Initialize(gD3D12Device.GetDevice(), psoDesc);
|
||||
if (!gPipelineState.GetPipelineState()) {
|
||||
return nullptr;
|
||||
}
|
||||
@@ -349,12 +352,15 @@ ID3D12PipelineState* CreatePSO(ID3D12RootSignature* inID3D12RootSignature,
|
||||
// 10. 创建Fence (同步)
|
||||
//=================================================================================
|
||||
bool InitD3D12(HWND inHWND, int inWidth, int inHeight) {
|
||||
if (!gDevice.Initialize()) {
|
||||
if (!gD3D12Device.Initialize()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ID3D12Device* device = gDevice.GetDevice();
|
||||
IDXGIFactory4* dxgiFactory = gDevice.GetFactory();
|
||||
// 获取抽象接口指针
|
||||
gDevice = &gD3D12Device;
|
||||
|
||||
ID3D12Device* device = gD3D12Device.GetDevice();
|
||||
IDXGIFactory4* dxgiFactory = gD3D12Device.GetFactory();
|
||||
|
||||
if (!gCommandQueue.Initialize(device, XCEngine::RHI::CommandQueueType::Direct)) {
|
||||
return false;
|
||||
@@ -382,13 +388,13 @@ bool InitD3D12(HWND inHWND, int inWidth, int inHeight) {
|
||||
d3dDescriptorHeapDescRTV.NumDescriptors = 2;
|
||||
d3dDescriptorHeapDescRTV.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
|
||||
gSwapChainRTVHeap.Initialize(device, XCEngine::RHI::DescriptorHeapType::RTV, 2);
|
||||
gRTVDescriptorSize = gDevice.GetDescriptorHandleIncrementSize(XCEngine::RHI::DescriptorHeapType::RTV);
|
||||
gRTVDescriptorSize = gD3D12Device.GetDescriptorHandleIncrementSize(XCEngine::RHI::DescriptorHeapType::RTV);
|
||||
|
||||
D3D12_DESCRIPTOR_HEAP_DESC d3dDescriptorHeapDescDSV = {};
|
||||
d3dDescriptorHeapDescDSV.NumDescriptors = 1;
|
||||
d3dDescriptorHeapDescDSV.Type = D3D12_DESCRIPTOR_HEAP_TYPE_DSV;
|
||||
gSwapChainDSVHeap.Initialize(device, XCEngine::RHI::DescriptorHeapType::DSV, 1);
|
||||
gDSVDescriptorSize = gDevice.GetDescriptorHandleIncrementSize(XCEngine::RHI::DescriptorHeapType::DSV);
|
||||
gDSVDescriptorSize = gD3D12Device.GetDescriptorHandleIncrementSize(XCEngine::RHI::DescriptorHeapType::DSV);
|
||||
|
||||
D3D12_CPU_DESCRIPTOR_HANDLE rtvHeapStart = gSwapChainRTVHeap.GetCPUDescriptorHandleForHeapStart();
|
||||
for (int i = 0; i < 2; i++) {
|
||||
@@ -478,7 +484,7 @@ void EndRenderToSwapChain(XCEngine::RHI::D3D12CommandList& inCommandList) {
|
||||
bool SaveScreenshot(const char* filename, int width, int height) {
|
||||
Log("[DEBUG] SaveScreenshot: start\n");
|
||||
|
||||
ID3D12Device* device = gDevice.GetDevice();
|
||||
ID3D12Device* device = gD3D12Device.GetDevice();
|
||||
ID3D12CommandQueue* queue = gCommandQueue.GetCommandQueue();
|
||||
ID3D12Resource* renderTarget = gColorRTs[gCurrentRTIndex].GetResource();
|
||||
|
||||
@@ -573,7 +579,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
|
||||
gPixelShader.CompileFromFile(L"Res/Shader/gs.hlsl", "MainPS", "ps_5_1");
|
||||
ID3D12PipelineState* pso = CreatePSO(rootSignature, gVertexShader.GetBytecode(), gPixelShader.GetBytecode(), gGeometryShader.GetBytecode());
|
||||
|
||||
gConstantBuffer.Initialize(gDevice.GetDevice(), 65536, D3D12_RESOURCE_STATE_GENERIC_READ, D3D12_HEAP_TYPE_UPLOAD);
|
||||
gConstantBuffer.Initialize(gD3D12Device.GetDevice(), 65536, D3D12_RESOURCE_STATE_GENERIC_READ, D3D12_HEAP_TYPE_UPLOAD);
|
||||
DirectX::XMMATRIX projectionMatrix = DirectX::XMMatrixPerspectiveFovLH(
|
||||
(45.0f * 3.141592f) / 180.0f, 1280.0f / 720.0f, 0.1f, 1000.0f);
|
||||
DirectX::XMMATRIX viewMatrix = DirectX::XMMatrixIdentity();
|
||||
@@ -602,7 +608,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
|
||||
gConstantBuffer.GetResource()->Unmap(0, nullptr);
|
||||
}
|
||||
|
||||
gMaterialBuffer.Initialize(gDevice.GetDevice(), 65536, D3D12_RESOURCE_STATE_GENERIC_READ, D3D12_HEAP_TYPE_UPLOAD);
|
||||
gMaterialBuffer.Initialize(gD3D12Device.GetDevice(), 65536, D3D12_RESOURCE_STATE_GENERIC_READ, D3D12_HEAP_TYPE_UPLOAD);
|
||||
struct MaterialData {
|
||||
float r;
|
||||
};
|
||||
@@ -621,11 +627,11 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
|
||||
int imageWidth, imageHeight, imageChannel;
|
||||
stbi_uc* pixels = stbi_load("Res/Image/earth_d.jpg", &imageWidth, &imageHeight, &imageChannel, 4);
|
||||
Log("[DEBUG] Texture loaded: width=%d, height=%d, channels=%d, pixels=%p\n", imageWidth, imageHeight, imageChannel, pixels);
|
||||
gTexture.InitializeFromData(gDevice.GetDevice(), gCommandList.GetCommandList(), pixels,
|
||||
gTexture.InitializeFromData(gD3D12Device.GetDevice(), gCommandList.GetCommandList(), pixels,
|
||||
imageWidth, imageHeight, DXGI_FORMAT_R8G8B8A8_UNORM);
|
||||
ID3D12Resource* texture = gTexture.GetResource();
|
||||
delete[] pixels;
|
||||
ID3D12Device* d3dDevice = gDevice.GetDevice();
|
||||
ID3D12Device* d3dDevice = gD3D12Device.GetDevice();
|
||||
|
||||
XCEngine::RHI::D3D12DescriptorHeap srvHeap;
|
||||
srvHeap.Initialize(d3dDevice, XCEngine::RHI::DescriptorHeapType::CBV_SRV_UAV, 3, true);
|
||||
@@ -640,7 +646,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
|
||||
|
||||
D3D12_CPU_DESCRIPTOR_HANDLE srvHeapPtr = srvHeap.GetCPUDescriptorHandleForHeapStart();
|
||||
gTextureSRV.InitializeAt(d3dDevice, texture, srvHeapPtr, &srvDesc);
|
||||
srvHeapPtr.ptr += gDevice.GetDescriptorHandleIncrementSize(XCEngine::RHI::DescriptorHeapType::CBV_SRV_UAV);
|
||||
srvHeapPtr.ptr += gD3D12Device.GetDescriptorHandleIncrementSize(XCEngine::RHI::DescriptorHeapType::CBV_SRV_UAV);
|
||||
|
||||
EndCommandList();
|
||||
WaitForCompletionOfCommandList();
|
||||
|
||||
Reference in New Issue
Block a user