feat: 修复RHI渲染循环问题

- 修复RootSignature参数数量与HelloEarth一致
- 修复StaticMeshComponent中device为nullptr的问题
- 修复CommandList::Reset类型转换问题
- 修复RTV创建使用nullptr而不是rtvDesc
- 添加SwapChain的GetCurrentRenderTarget方法
- 修复DepthStencil创建问题(暂时跳过)
- 渲染循环基本可运行
This commit is contained in:
2026-03-14 03:13:10 +08:00
parent 5f12393424
commit 3ad317afb2
18 changed files with 271 additions and 17 deletions

View File

@@ -51,6 +51,8 @@ public:
virtual bool Resize(uint32_t width, uint32_t height) = 0;
virtual uint32_t GetCurrentBufferIndex() const = 0;
virtual void* GetBuffer(uint32_t index) = 0;
virtual void* GetCurrentRenderTarget() = 0;
virtual void* GetDepthStencil() = 0;
virtual void SetFullscreen(bool fullscreen) = 0;
virtual bool IsFullscreen() const = 0;
};

View File

@@ -437,6 +437,18 @@ void* D3D12SwapChain::GetBuffer(uint32_t index) {
return GetBufferResource(index);
}
void* D3D12SwapChain::GetCurrentRenderTarget() {
uint32_t index = GetCurrentBufferIndex();
D3D12_CPU_DESCRIPTOR_HANDLE handle = m_rtvHeap->GetNativeHeap()->GetCPUDescriptorHandleForHeapStart();
UINT rtvSize = m_device->GetD3D12Device()->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
handle.ptr += index * rtvSize;
return (void*)handle.ptr;
}
void* D3D12SwapChain::GetDepthStencil() {
return nullptr;
}
void D3D12SwapChain::SetFullscreen(bool fullscreen) {
m_fullscreen = fullscreen;
m_swapChain->SetFullscreenState(fullscreen, nullptr);
@@ -660,7 +672,9 @@ D3D12CommandList::~D3D12CommandList() {
}
void D3D12CommandList::Reset(void* allocator) {
ID3D12CommandAllocator* d3dAllocator = static_cast<ID3D12CommandAllocator*>(allocator);
ICommandAllocator* cmdAlloc = static_cast<ICommandAllocator*>(allocator);
D3D12CommandAllocator* d3d12Alloc = static_cast<D3D12CommandAllocator*>(cmdAlloc);
ID3D12CommandAllocator* d3dAllocator = d3d12Alloc->GetNativeAllocator();
m_commandList->Reset(d3dAllocator, nullptr);
}

View File

@@ -94,6 +94,8 @@ public:
bool Resize(uint32_t width, uint32_t height) override;
uint32_t GetCurrentBufferIndex() const override;
void* GetBuffer(uint32_t index) override;
void* GetCurrentRenderTarget() override;
void* GetDepthStencil() override;
void SetFullscreen(bool fullscreen) override;
bool IsFullscreen() const override;
@@ -172,6 +174,7 @@ public:
void* GetNativeCommandList() const override;
ID3D12GraphicsCommandList* GetNative() const { return m_commandList.Get(); }
D3D12Device* GetDevice() const { return m_device; }
private:
D3D12Device* m_device = nullptr;

View File

@@ -2,6 +2,8 @@
#include <RHI\D3D12\D3D12RHI.h>
#include <RHI\D3D12\D3D12Resources.h>
void EngineLog(const char* msg, ...);
namespace XCEngine {
namespace RHI {
@@ -59,15 +61,17 @@ void RenderContext::BeginFrame() {
uint32_t bufferIndex = m_swapChain->GetCurrentBufferIndex();
if (m_currentRenderTarget) delete m_currentRenderTarget;
CreateRenderTargetFromSwapChain(m_device, (D3D12SwapChain*)m_swapChain, bufferIndex, &m_currentRenderTarget);
D3D12CommandList* cmdList = (D3D12CommandList*)m_commandList;
ID3D12GraphicsCommandList* d3dCmdList = cmdList->GetNative();
void* currentRT = m_swapChain->GetCurrentRenderTarget();
void* currentDS = m_depthStencil->GetDSV();
ID3D12Resource* buffer = (ID3D12Resource*)m_swapChain->GetBuffer(bufferIndex);
D3D12_RESOURCE_BARRIER barrier = {};
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
barrier.Transition.pResource = ((D3D12RenderTarget*)m_currentRenderTarget)->GetNative();
barrier.Transition.pResource = buffer;
barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_PRESENT;
barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_RENDER_TARGET;
barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
@@ -78,26 +82,34 @@ void RenderContext::EndFrame() {
D3D12CommandList* cmdList = (D3D12CommandList*)m_commandList;
ID3D12GraphicsCommandList* d3dCmdList = cmdList->GetNative();
uint32_t bufferIndex = m_swapChain->GetCurrentBufferIndex();
ID3D12Resource* buffer = (ID3D12Resource*)m_swapChain->GetBuffer(bufferIndex);
D3D12_RESOURCE_BARRIER barrier = {};
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
barrier.Transition.pResource = ((D3D12RenderTarget*)m_currentRenderTarget)->GetNative();
barrier.Transition.pResource = buffer;
barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_RENDER_TARGET;
barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_PRESENT;
barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
d3dCmdList->ResourceBarrier(1, &barrier);
m_commandList->Close();
EngineLog("EndFrame: after Close");
ICommandQueue* queue = m_device->GetCommandQueue();
void* cmdListPtr = m_commandList->GetNativeCommandList();
queue->ExecuteCommandLists(&cmdListPtr, 1);
EngineLog("EndFrame: after Execute");
m_fenceValue++;
queue->Signal(m_fence, m_fenceValue);
EngineLog("EndFrame: before Wait, fenceValue=%llu", (unsigned long long)m_fenceValue);
m_fence->Wait(m_fenceValue);
EngineLog("EndFrame: after Wait");
}
void RenderContext::SetViewport(float width, float height) {
EngineLog("SetViewport: start");
Viewport viewport = {};
viewport.topLeftX = 0;
viewport.topLeftY = 0;
@@ -106,26 +118,37 @@ void RenderContext::SetViewport(float width, float height) {
viewport.minDepth = 0.0f;
viewport.maxDepth = 1.0f;
m_commandList->SetViewports(&viewport, 1);
EngineLog("SetViewport: done");
}
void RenderContext::SetScissor(int32_t width, int32_t height) {
EngineLog("SetScissor: start");
Rect scissor = {};
scissor.left = 0;
scissor.top = 0;
scissor.right = width;
scissor.bottom = height;
m_commandList->SetScissorRects(&scissor, 1);
EngineLog("SetScissor: done");
}
void RenderContext::ClearRenderTarget(const float color[4]) {
D3D12RenderTarget* rt = (D3D12RenderTarget*)m_currentRenderTarget;
rt->CreateRTV();
m_commandList->ClearRenderTargetView(rt->GetRTV(), color);
EngineLog("ClearRT: start");
void* rtv = m_swapChain->GetCurrentRenderTarget();
EngineLog("ClearRT: rtv=%p", rtv);
m_commandList->ClearRenderTargetView(rtv, color);
EngineLog("ClearRT: done");
}
void RenderContext::ClearDepthStencil() {
m_depthStencil->CreateDSV();
m_commandList->ClearDepthStencilView(m_depthStencil->GetDSV(), 1.0f, 0);
//EngineLog("ClearDS: start");
//EngineLog("ClearDS: m_depthStencil=%p", m_depthStencil);
//m_depthStencil->CreateDSV();
//EngineLog("ClearDS: after CreateDSV");
//void* dsv = m_depthStencil->GetDSV();
//EngineLog("ClearDS: dsv=%p", dsv);
//m_commandList->ClearDepthStencilView(dsv, 1.0f, 0);
//EngineLog("ClearDS: done");
}
void RenderContext::Present() {

View File

@@ -1,23 +1,31 @@
#include "D3D12Resources.h"
#include "Rendering\RenderTarget.h"
void EngineLog(const char* msg, ...);
namespace XCEngine {
namespace RHI {
void D3D12DepthStencil::CreateDSV() {
EngineLog("CreateDSV: start, m_device=%p, m_resource=%p", m_device, m_resource.Get());
if (m_dsvHandle.ptr != 0) return;
EngineLog("CreateDSV: calling CreateDepthStencilView");
D3D12_DEPTH_STENCIL_VIEW_DESC dsvDesc = {};
dsvDesc.Format = FormatToDXGIFormat(m_desc.format);
EngineLog("CreateDSV: format=%d", dsvDesc.Format);
dsvDesc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2D;
m_device->GetD3D12Device()->CreateDepthStencilView(m_resource.Get(), &dsvDesc, m_dsvHandle);
EngineLog("CreateDSV: done");
}
void D3D12RenderTarget::CreateRTV() {
EngineLog("CreateRTV: start, m_device=%p", m_device);
if (m_rtvHandle.ptr != 0) return;
D3D12_RENDER_TARGET_VIEW_DESC rtvDesc = {};
rtvDesc.Format = FormatToDXGIFormat(m_desc.format);
rtvDesc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2D;
m_device->GetD3D12Device()->CreateRenderTargetView(m_resource.Get(), &rtvDesc, m_rtvHandle);
ID3D12Device* d3dDevice = m_device->GetD3D12Device();
EngineLog("CreateRTV: d3dDevice=%p, m_resource=%p", d3dDevice, m_resource.Get());
EngineLog("CreateRTV: calling CreateRenderTargetView");
d3dDevice->CreateRenderTargetView(m_resource.Get(), nullptr, m_rtvHandle);
EngineLog("CreateRTV: done");
}
bool CreateDepthStencil(D3D12Device* device, uint32_t width, uint32_t height, Format format, IDepthStencil** outDepthStencil) {

View File

@@ -1,4 +1,5 @@
#include <Rendering\StaticMeshComponent.h>
#include <RHI\D3D12\D3D12RHI.h>
#include <RHI\D3D12\D3D12Resources.h>
#include <cstdio>
@@ -18,6 +19,9 @@ StaticMeshComponent::~StaticMeshComponent() {
}
bool StaticMeshComponent::Initialize(ICommandList* commandList, const char* filePath) {
D3D12CommandList* d3d12CmdList = static_cast<D3D12CommandList*>(commandList);
D3D12Device* device = d3d12CmdList->GetDevice();
FILE* file = nullptr;
if (fopen_s(&file, filePath, "rb") != 0) {
return false;
@@ -30,8 +34,6 @@ bool StaticMeshComponent::Initialize(ICommandList* commandList, const char* file
MeshVertex* vertices = new MeshVertex[vertexCount];
fread(vertices, sizeof(MeshVertex), vertexCount, file);
D3D12Device* device = nullptr;
CreateVertexBuffer(device, commandList, vertices, sizeof(MeshVertex) * vertexCount, sizeof(MeshVertex), &m_vertexBuffer);
delete[] vertices;