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

@@ -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();