From d52028e196d08169095395c696b2d5bd77104f6e Mon Sep 17 00:00:00 2001 From: ssdfasd <2156608475@qq.com> Date: Sun, 15 Mar 2026 20:36:42 +0800 Subject: [PATCH] Update main.cpp to use D3D12CommandList wrapper methods, add SetRenderTargets and Clear overloads --- .../XCEngine/RHI/D3D12/D3D12CommandList.h | 3 ++ engine/src/RHI/D3D12CommandList.cpp | 12 ++++++ tests/D3D12/main.cpp | 40 +++++++++---------- 3 files changed, 34 insertions(+), 21 deletions(-) diff --git a/engine/include/XCEngine/RHI/D3D12/D3D12CommandList.h b/engine/include/XCEngine/RHI/D3D12/D3D12CommandList.h index 40b0ff3e..c56129c5 100644 --- a/engine/include/XCEngine/RHI/D3D12/D3D12CommandList.h +++ b/engine/include/XCEngine/RHI/D3D12/D3D12CommandList.h @@ -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); diff --git a/engine/src/RHI/D3D12CommandList.cpp b/engine/src/RHI/D3D12CommandList.cpp index c92ac967..f24c2f2f 100644 --- a/engine/src/RHI/D3D12CommandList.cpp +++ b/engine/src/RHI/D3D12CommandList.cpp @@ -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(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(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); } diff --git a/tests/D3D12/main.cpp b/tests/D3D12/main.cpp index 5c2ba90f..bb35b68c 100644 --- a/tests/D3D12/main.cpp +++ b/tests/D3D12/main.cpp @@ -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();