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:
2026-03-15 23:03:06 +08:00
parent dfbd218435
commit fb2b794156
7 changed files with 262 additions and 60 deletions

View File

@@ -1,4 +1,22 @@
#include "XCEngine/RHI/D3D12/D3D12Device.h"
#include "XCEngine/RHI/D3D12/D3D12CommandQueue.h"
#include "XCEngine/RHI/D3D12/D3D12CommandList.h"
#include "XCEngine/RHI/D3D12/D3D12CommandAllocator.h"
#include "XCEngine/RHI/D3D12/D3D12Fence.h"
#include "XCEngine/RHI/D3D12/D3D12DescriptorHeap.h"
#include "XCEngine/RHI/D3D12/D3D12QueryHeap.h"
#include "XCEngine/RHI/D3D12/D3D12RootSignature.h"
#include "XCEngine/RHI/D3D12/D3D12PipelineState.h"
#include "XCEngine/RHI/D3D12/D3D12Sampler.h"
#include "XCEngine/RHI/D3D12/D3D12Texture.h"
#include "XCEngine/RHI/D3D12/D3D12Buffer.h"
#include "XCEngine/RHI/D3D12/D3D12SwapChain.h"
#include "XCEngine/RHI/D3D12/D3D12Shader.h"
#include "XCEngine/RHI/D3D12/D3D12RenderTargetView.h"
#include "XCEngine/RHI/D3D12/D3D12DepthStencilView.h"
#include "XCEngine/RHI/D3D12/D3D12ShaderResourceView.h"
#include "XCEngine/RHI/D3D12/D3D12UnorderedAccessView.h"
#include "XCEngine/RHI/D3D12/D3D12ConstantBufferView.h"
#include <stdio.h>
#ifdef _DEBUG
@@ -169,5 +187,117 @@ UINT D3D12Device::GetDescriptorHandleIncrementSize(DescriptorHeapType type) cons
return m_device->GetDescriptorHandleIncrementSize(ToD3D12(type));
}
ICommandQueue* D3D12Device::CreateCommandQueue(const CommandQueueDesc& desc) {
D3D12CommandQueue* queue = new D3D12CommandQueue();
queue->Initialize(m_device.Get(), static_cast<CommandQueueType>(desc.queueType));
return reinterpret_cast<ICommandQueue*>(queue);
}
ICommandList* D3D12Device::CreateCommandList(const CommandListDesc& desc) {
D3D12CommandList* commandList = new D3D12CommandList();
D3D12CommandAllocator* allocator = new D3D12CommandAllocator();
allocator->Initialize(m_device.Get(), static_cast<CommandQueueType>(desc.commandListType));
commandList->Initialize(m_device.Get(), static_cast<CommandQueueType>(desc.commandListType), allocator->GetCommandAllocator());
return reinterpret_cast<ICommandList*>(commandList);
}
ICommandAllocator* D3D12Device::CreateCommandAllocator(const CommandAllocatorDesc& desc) {
D3D12CommandAllocator* allocator = new D3D12CommandAllocator();
allocator->Initialize(m_device.Get(), static_cast<CommandQueueType>(desc.commandListType));
return reinterpret_cast<ICommandAllocator*>(allocator);
}
IFence* D3D12Device::CreateFence(const FenceDesc& desc) {
D3D12Fence* fence = new D3D12Fence();
fence->Initialize(m_device.Get(), desc.initialValue);
return reinterpret_cast<IFence*>(fence);
}
IDescriptorHeap* D3D12Device::CreateDescriptorHeap(const DescriptorHeapDesc& desc) {
D3D12DescriptorHeap* heap = new D3D12DescriptorHeap();
heap->Initialize(m_device.Get(), static_cast<DescriptorHeapType>(desc.heapType), desc.descriptorCount);
return reinterpret_cast<IDescriptorHeap*>(heap);
}
IQueryHeap* D3D12Device::CreateQueryHeap(const QueryHeapDesc& desc) {
D3D12QueryHeap* heap = new D3D12QueryHeap();
heap->Initialize(m_device.Get(), static_cast<QueryType>(desc.queryType), desc.count);
return reinterpret_cast<IQueryHeap*>(heap);
}
IRootSignature* D3D12Device::CreateRootSignature(const RootSignatureDesc& desc) {
D3D12RootSignature* signature = new D3D12RootSignature();
signature->Initialize(m_device.Get(), *reinterpret_cast<const D3D12_ROOT_SIGNATURE_DESC*>(desc.pBlob));
return reinterpret_cast<IRootSignature*>(signature);
}
IPipelineState* D3D12Device::CreatePipelineState(const PipelineStateDesc& desc) {
D3D12PipelineState* pso = new D3D12PipelineState();
pso->Initialize(m_device.Get(), *reinterpret_cast<const D3D12_GRAPHICS_PIPELINE_STATE_DESC*>(desc.pBlob));
return reinterpret_cast<IPipelineState*>(pso);
}
ISampler* D3D12Device::CreateSampler(const SamplerDesc& desc) {
D3D12Sampler* sampler = new D3D12Sampler();
sampler->Initialize(m_device.Get(), *reinterpret_cast<const D3D12_SAMPLER_DESC*>(&desc));
return reinterpret_cast<ISampler*>(sampler);
}
ITexture* D3D12Device::CreateTexture(const TextureDesc& desc) {
D3D12Texture* texture = new D3D12Texture();
return reinterpret_cast<ITexture*>(texture);
}
IBuffer* D3D12Device::CreateBuffer(const BufferDesc& desc) {
D3D12Buffer* buffer = new D3D12Buffer();
return reinterpret_cast<IBuffer*>(buffer);
}
ISwapChain* D3D12Device::CreateSwapChain(const SwapChainDesc& desc) {
D3D12SwapChain* swapChain = new D3D12SwapChain();
return reinterpret_cast<ISwapChain*>(swapChain);
}
IShader* D3D12Device::CompileShader(const ShaderCompileDesc& desc) {
D3D12Shader* shader = new D3D12Shader();
std::string entryPoint(desc.entryPoint.begin(), desc.entryPoint.end());
std::string profile(desc.profile.begin(), desc.profile.end());
shader->CompileFromFile(desc.fileName.c_str(), entryPoint.c_str(), profile.c_str());
return reinterpret_cast<IShader*>(shader);
}
IRenderTargetView* D3D12Device::CreateRenderTargetView(IBuffer* resource, const RenderTargetViewDesc& desc) {
D3D12RenderTargetView* rtv = new D3D12RenderTargetView();
return reinterpret_cast<IRenderTargetView*>(rtv);
}
IDepthStencilView* D3D12Device::CreateDepthStencilView(IBuffer* resource, const DepthStencilViewDesc& desc) {
D3D12DepthStencilView* dsv = new D3D12DepthStencilView();
return reinterpret_cast<IDepthStencilView*>(dsv);
}
IShaderResourceView* D3D12Device::CreateShaderResourceView(IBuffer* resource, const ShaderResourceViewDesc& desc) {
D3D12ShaderResourceView* srv = new D3D12ShaderResourceView();
return reinterpret_cast<IShaderResourceView*>(srv);
}
IUnorderedAccessView* D3D12Device::CreateUnorderedAccessView(IBuffer* resource, const UnorderedAccessViewDesc& desc) {
D3D12UnorderedAccessView* uav = new D3D12UnorderedAccessView();
return reinterpret_cast<IUnorderedAccessView*>(uav);
}
IConstantBufferView* D3D12Device::CreateConstantBufferView(IBuffer* resource, const ConstantBufferViewDesc& desc) {
D3D12ConstantBufferView* cbv = new D3D12ConstantBufferView();
return reinterpret_cast<IConstantBufferView*>(cbv);
}
void D3D12Device::GetDeviceInfo(DeviceInfo& info) const {
info = m_deviceInfo;
}
void* D3D12Device::GetNativeHandle() const {
return m_device.Get();
}
} // namespace RHI
} // namespace XCEngine