Update main.cpp to use D3D12CommandList wrapper methods, add SetRenderTargets and Clear overloads

This commit is contained in:
2026-03-15 20:36:42 +08:00
parent 2a5fc4f0d4
commit d52028e196
3 changed files with 34 additions and 21 deletions

View File

@@ -61,6 +61,7 @@ public:
void SetScissorRects(uint32_t count, const Rect* rects);
void SetPrimitiveTopology(PrimitiveTopology topology);
void SetRenderTargets(uint32_t count, ID3D12Resource** renderTargets, ID3D12Resource* depthStencil = nullptr);
void SetRenderTargets(uint32_t count, const D3D12_CPU_DESCRIPTOR_HANDLE* renderTargetHandles, const D3D12_CPU_DESCRIPTOR_HANDLE* depthStencilHandle = nullptr);
void SetVertexBuffer(uint32_t slot, ID3D12Resource* buffer, uint64_t offset, uint32_t stride);
void SetVertexBuffers(uint32_t startSlot, uint32_t count, const D3D12_VERTEX_BUFFER_VIEW* views);
@@ -86,7 +87,9 @@ public:
void DrawIndexedInstancedIndirect(ID3D12Resource* argBuffer, uint64_t alignedByteOffset);
void ClearRenderTargetView(ID3D12Resource* renderTarget, const float color[4], uint32_t rectCount = 0, const D3D12_RECT* rects = nullptr);
void ClearRenderTargetView(D3D12_CPU_DESCRIPTOR_HANDLE renderTargetHandle, const float color[4], uint32_t rectCount = 0, const D3D12_RECT* rects = nullptr);
void ClearDepthStencilView(ID3D12Resource* depthStencil, uint32_t clearFlags, float depth = 1.0f, uint8_t stencil = 0, uint32_t rectCount = 0, const D3D12_RECT* rects = nullptr);
void ClearDepthStencilView(D3D12_CPU_DESCRIPTOR_HANDLE depthStencilHandle, uint32_t clearFlags, float depth = 1.0f, uint8_t stencil = 0, uint32_t rectCount = 0, const D3D12_RECT* rects = nullptr);
void ClearUnorderedAccessView(D3D12_GPU_DESCRIPTOR_HANDLE viewHandle, D3D12_CPU_DESCRIPTOR_HANDLE resourceHandle, ID3D12Resource* unorderedAccess, const float values[4], uint32_t rectCount = 0, const D3D12_RECT* rects = nullptr);
void CopyResource(ID3D12Resource* dst, ID3D12Resource* src);

View File

@@ -162,6 +162,10 @@ void D3D12CommandList::SetRenderTargets(uint32_t count, ID3D12Resource** renderT
m_commandList->OMSetRenderTargets(count, count > 0 ? rtvHandles.data() : nullptr, FALSE, depthStencil ? &dsvHandle : nullptr);
}
void D3D12CommandList::SetRenderTargets(uint32_t count, const D3D12_CPU_DESCRIPTOR_HANDLE* renderTargetHandles, const D3D12_CPU_DESCRIPTOR_HANDLE* depthStencilHandle) {
m_commandList->OMSetRenderTargets(count, renderTargetHandles, FALSE, depthStencilHandle);
}
void D3D12CommandList::SetVertexBuffer(uint32_t slot, ID3D12Resource* buffer, uint64_t offset, uint32_t stride) {
D3D12_VERTEX_BUFFER_VIEW view = {};
view.BufferLocation = buffer->GetGPUVirtualAddress() + offset;
@@ -249,11 +253,19 @@ void D3D12CommandList::ClearRenderTargetView(ID3D12Resource* renderTarget, const
m_commandList->ClearRenderTargetView(rtvHandle, color, rectCount, rects);
}
void D3D12CommandList::ClearRenderTargetView(D3D12_CPU_DESCRIPTOR_HANDLE renderTargetHandle, const float color[4], uint32_t rectCount, const D3D12_RECT* rects) {
m_commandList->ClearRenderTargetView(renderTargetHandle, color, rectCount, rects);
}
void D3D12CommandList::ClearDepthStencilView(ID3D12Resource* depthStencil, uint32_t clearFlags, float depth, uint8_t stencil, uint32_t rectCount, const D3D12_RECT* rects) {
D3D12_CPU_DESCRIPTOR_HANDLE dsvHandle = {};
m_commandList->ClearDepthStencilView(dsvHandle, static_cast<D3D12_CLEAR_FLAGS>(clearFlags), depth, stencil, rectCount, rects);
}
void D3D12CommandList::ClearDepthStencilView(D3D12_CPU_DESCRIPTOR_HANDLE depthStencilHandle, uint32_t clearFlags, float depth, uint8_t stencil, uint32_t rectCount, const D3D12_RECT* rects) {
m_commandList->ClearDepthStencilView(depthStencilHandle, static_cast<D3D12_CLEAR_FLAGS>(clearFlags), depth, stencil, rectCount, rects);
}
void D3D12CommandList::ClearUnorderedAccessView(D3D12_GPU_DESCRIPTOR_HANDLE viewHandle, D3D12_CPU_DESCRIPTOR_HANDLE resourceHandle, ID3D12Resource* unorderedAccess, const float values[4], uint32_t rectCount, const D3D12_RECT* rects) {
m_commandList->ClearUnorderedAccessViewFloat(viewHandle, resourceHandle, unorderedAccess, values, rectCount, rects);
}

View File

@@ -205,19 +205,19 @@ public:
fclose(pFile);
}
}
void Render(ID3D12GraphicsCommandList* inCommandList) {
void Render(XCEngine::RHI::D3D12CommandList& inCommandList) {
D3D12_VERTEX_BUFFER_VIEW vbos[] = {
mVBOView
};
inCommandList->IASetVertexBuffers(0, 1, vbos);
inCommandList.SetVertexBuffers(0, 1, vbos);
if (mSubMeshes.empty()) {
inCommandList->DrawInstanced(mVertexCount, 1, 0, 0);
inCommandList.Draw(mVertexCount, 1, 0, 0);
}
else {
for (auto iter = mSubMeshes.begin();
iter != mSubMeshes.end(); iter++) {
inCommandList->IASetIndexBuffer(&iter->second->mIBView);
inCommandList->DrawIndexedInstanced(iter->second->mIndexCount, 1, 0, 0, 0);
inCommandList.SetIndexBuffer(iter->second->mIBO.GetResource(), 0, XCEngine::RHI::Format::R32_UInt);
inCommandList.DrawIndexed(iter->second->mIndexCount, 1, 0, 0, 0);
}
}
}
@@ -472,30 +472,28 @@ void EndCommandList() {
// 4. 设置Viewport/Scissor
// 5. Clear Color/Depth
//=================================================================================
void BeginRenderToSwapChain(ID3D12GraphicsCommandList* inCommandList) {
void BeginRenderToSwapChain(XCEngine::RHI::D3D12CommandList& inCommandList) {
gCurrentRTIndex = gSwapChain.GetCurrentBackBufferIndex();
D3D12_RESOURCE_BARRIER barrier = InitResourceBarrier(gColorRTs[gCurrentRTIndex].GetResource(), D3D12_RESOURCE_STATE_PRESENT, D3D12_RESOURCE_STATE_RENDER_TARGET);
inCommandList->ResourceBarrier(1, &barrier);
inCommandList.TransitionBarrier(gColorRTs[gCurrentRTIndex].GetResource(), XCEngine::RHI::ResourceStates::Present, XCEngine::RHI::ResourceStates::RenderTarget);
D3D12_CPU_DESCRIPTOR_HANDLE colorRT, dsv;
dsv.ptr = gSwapChainDSVHeap.GetCPUDescriptorHandleForHeapStart().ptr;
colorRT.ptr = gSwapChainRTVHeap.GetCPUDescriptorHandleForHeapStart().ptr + gCurrentRTIndex * gRTVDescriptorSize;
inCommandList->OMSetRenderTargets(1, &colorRT, FALSE, &dsv);
D3D12_VIEWPORT viewport = { 0.0f,0.0f,1280.0f,720.0f };
D3D12_RECT scissorRect = { 0,0,1280,720 };
inCommandList->RSSetViewports(1, &viewport);
inCommandList->RSSetScissorRects(1, &scissorRect);
inCommandList.SetRenderTargets(1, &colorRT, &dsv);
XCEngine::RHI::Viewport viewport = { 0.0f, 0.0f, 1280.0f, 720.0f, 0.0f, 1.0f };
XCEngine::RHI::Rect scissorRect = { 0, 0, 1280, 720 };
inCommandList.SetViewport(viewport);
inCommandList.SetScissorRect(scissorRect);
const float clearColor[] = { 0.0f,0.0f,0.0f,1.0f };
inCommandList->ClearRenderTargetView(colorRT, clearColor, 0, nullptr);
inCommandList->ClearDepthStencilView(dsv, D3D12_CLEAR_FLAG_DEPTH | D3D12_CLEAR_FLAG_STENCIL, 1.0f, 0, 0, nullptr);
inCommandList.ClearRenderTargetView(colorRT, clearColor, 0, nullptr);
inCommandList.ClearDepthStencilView(dsv, D3D12_CLEAR_FLAG_DEPTH | D3D12_CLEAR_FLAG_STENCIL, 1.0f, 0, 0, nullptr);
}
//=================================================================================
// 结束渲染到SwapChain
// 状态转换: RENDER_TARGET → PRESENT
//=================================================================================
void EndRenderToSwapChain(ID3D12GraphicsCommandList* inCommandList) {
D3D12_RESOURCE_BARRIER barrier = InitResourceBarrier(gColorRTs[gCurrentRTIndex].GetResource(), D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_PRESENT);
inCommandList->ResourceBarrier(1, &barrier);
void EndRenderToSwapChain(XCEngine::RHI::D3D12CommandList& inCommandList) {
inCommandList.TransitionBarrier(gColorRTs[gCurrentRTIndex].GetResource(), XCEngine::RHI::ResourceStates::RenderTarget, XCEngine::RHI::ResourceStates::Present);
}
//=================================================================================
@@ -699,7 +697,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
color[0] = timeSinceAppStartInSecond;
commandAllocator->Reset();
gCommandList.Reset(gCommandAllocator.GetCommandAllocator());
BeginRenderToSwapChain(gCommandList.GetCommandList());
BeginRenderToSwapChain(gCommandList);
gCommandList.SetPipelineState(pso);
gCommandList.SetRootSignature(rootSignature);
gCommandList.SetDescriptorHeaps(_countof(descriptorHeaps), descriptorHeaps);
@@ -708,11 +706,11 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
gCommandList.SetGraphicsRootDescriptorTable(2, srvHeap.GetGPUDescriptorHandleForHeapStart());
gCommandList.SetGraphicsRootShaderResourceView(3, gMaterialBuffer.GetGPUVirtualAddress());
gCommandList.SetPrimitiveTopology(XCEngine::RHI::PrimitiveTopology::TriangleList);
staticMeshComponent.Render(gCommandList.GetCommandList());
staticMeshComponent.Render(gCommandList);
// On screenshot frame, don't transition to PRESENT - keep RENDER_TARGET for screenshot
if (frameCount != 30) {
EndRenderToSwapChain(gCommandList.GetCommandList());
EndRenderToSwapChain(gCommandList);
}
EndCommandList();