From ddd3140114e59bdbbc0f88c2f4268e3953ccf68c Mon Sep 17 00:00:00 2001 From: ssdfasd <2156608475@qq.com> Date: Sun, 15 Mar 2026 18:13:53 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=BB=A7=E7=BB=AD=E7=94=A8=20D3D12?= =?UTF-8?q?CommandList=20=E6=9B=BF=E6=8D=A2=E5=8E=9F=E7=94=9F=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加更多 wrapper 方法: SetDescriptorHeaps, SetGraphicsRootConstantBufferView, SetGraphicsRoot32BitConstants, SetGraphicsRootDescriptorTable, SetGraphicsRootShaderResourceView - 使用 wrapper 方法替换 main.cpp 中的原生 API 调用 - 测试通过 --- .../XCEngine/RHI/D3D12/D3D12CommandList.h | 6 +++++ engine/src/RHI/D3D12CommandList.cpp | 20 ++++++++++++++ tests/D3D12/main.cpp | 27 +++++++++---------- 3 files changed, 39 insertions(+), 14 deletions(-) diff --git a/engine/include/XCEngine/RHI/D3D12/D3D12CommandList.h b/engine/include/XCEngine/RHI/D3D12/D3D12CommandList.h index 5a5cb46e..40b0ff3e 100644 --- a/engine/include/XCEngine/RHI/D3D12/D3D12CommandList.h +++ b/engine/include/XCEngine/RHI/D3D12/D3D12CommandList.h @@ -67,9 +67,15 @@ public: void SetIndexBuffer(ID3D12Resource* buffer, uint64_t offset, Format indexFormat); void SetDescriptorHeap(ID3D12DescriptorHeap* heap); + void SetDescriptorHeaps(uint32_t count, ID3D12DescriptorHeap** heaps); void SetGraphicsDescriptorTable(uint32_t rootParameterIndex, D3D12_GPU_DESCRIPTOR_HANDLE baseHandle); void SetComputeDescriptorTable(uint32_t rootParameterIndex, D3D12_GPU_DESCRIPTOR_HANDLE baseHandle); + void SetGraphicsRootConstantBufferView(uint32_t rootParameterIndex, D3D12_GPU_VIRTUAL_ADDRESS bufferLocation); + void SetGraphicsRoot32BitConstants(uint32_t rootParameterIndex, uint32_t num32BitValuesToSet, const void* pSrcData, uint32_t destOffsetIn32BitValues); + void SetGraphicsRootDescriptorTable(uint32_t rootParameterIndex, D3D12_GPU_DESCRIPTOR_HANDLE baseDescriptor); + void SetGraphicsRootShaderResourceView(uint32_t rootParameterIndex, D3D12_GPU_VIRTUAL_ADDRESS shaderResource); + void SetStencilRef(uint32_t stencilRef); void SetBlendFactor(const float blendFactor[4]); void SetDepthBias(float depthBias, float slopeScaledDepthBias, float depthBiasClamp); diff --git a/engine/src/RHI/D3D12CommandList.cpp b/engine/src/RHI/D3D12CommandList.cpp index 8b1cc00d..c92ac967 100644 --- a/engine/src/RHI/D3D12CommandList.cpp +++ b/engine/src/RHI/D3D12CommandList.cpp @@ -189,6 +189,10 @@ void D3D12CommandList::SetDescriptorHeap(ID3D12DescriptorHeap* heap) { m_currentDescriptorHeap = heap; } +void D3D12CommandList::SetDescriptorHeaps(uint32_t count, ID3D12DescriptorHeap** heaps) { + m_commandList->SetDescriptorHeaps(count, heaps); +} + void D3D12CommandList::SetGraphicsDescriptorTable(uint32_t rootParameterIndex, D3D12_GPU_DESCRIPTOR_HANDLE baseHandle) { m_commandList->SetGraphicsRootDescriptorTable(rootParameterIndex, baseHandle); } @@ -197,6 +201,22 @@ void D3D12CommandList::SetComputeDescriptorTable(uint32_t rootParameterIndex, D3 m_commandList->SetComputeRootDescriptorTable(rootParameterIndex, baseHandle); } +void D3D12CommandList::SetGraphicsRootConstantBufferView(uint32_t rootParameterIndex, D3D12_GPU_VIRTUAL_ADDRESS bufferLocation) { + m_commandList->SetGraphicsRootConstantBufferView(rootParameterIndex, bufferLocation); +} + +void D3D12CommandList::SetGraphicsRoot32BitConstants(uint32_t rootParameterIndex, uint32_t num32BitValuesToSet, const void* pSrcData, uint32_t destOffsetIn32BitValues) { + m_commandList->SetGraphicsRoot32BitConstants(rootParameterIndex, num32BitValuesToSet, pSrcData, destOffsetIn32BitValues); +} + +void D3D12CommandList::SetGraphicsRootDescriptorTable(uint32_t rootParameterIndex, D3D12_GPU_DESCRIPTOR_HANDLE baseDescriptor) { + m_commandList->SetGraphicsRootDescriptorTable(rootParameterIndex, baseDescriptor); +} + +void D3D12CommandList::SetGraphicsRootShaderResourceView(uint32_t rootParameterIndex, D3D12_GPU_VIRTUAL_ADDRESS shaderResource) { + m_commandList->SetGraphicsRootShaderResourceView(rootParameterIndex, shaderResource); +} + void D3D12CommandList::SetStencilRef(uint32_t stencilRef) { m_commandList->OMSetStencilRef(stencilRef); } diff --git a/tests/D3D12/main.cpp b/tests/D3D12/main.cpp index 7673158f..f06f2750 100644 --- a/tests/D3D12/main.cpp +++ b/tests/D3D12/main.cpp @@ -842,10 +842,9 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine } InitD3D12(hwnd, 1280, 720); - ID3D12GraphicsCommandList* commandList = GetCommandList(); ID3D12CommandAllocator* commandAllocator = GetCommandAllocator(); StaticMeshComponent staticMeshComponent; - staticMeshComponent.InitFromFile(commandList, "Res/Model/Sphere.lhsm"); + staticMeshComponent.InitFromFile(gCommandList.GetCommandList(), "Res/Model/Sphere.lhsm"); ID3D12RootSignature* rootSignature = InitRootSignature(); D3D12_SHADER_BYTECODE vs, gs, ps; @@ -890,7 +889,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine int imageWidth, imageHeight, imageChannel; stbi_uc* pixels = stbi_load("Res/Image/earth_d.jpg", &imageWidth, &imageHeight, &imageChannel, 4); Log("[DEBUG] Texture loaded: width=%d, height=%d, channels=%d, pixels=%p\n", imageWidth, imageHeight, imageChannel, pixels); - ID3D12Resource* texture = CreateTexture2D(commandList, pixels, + ID3D12Resource* texture = CreateTexture2D(gCommandList.GetCommandList(), pixels, imageWidth * imageHeight * imageChannel, imageWidth, imageHeight, DXGI_FORMAT_R8G8B8A8_UNORM); delete[] pixels; ID3D12Device* d3dDevice = GetD3DDevice(); @@ -944,20 +943,20 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine color[0] = timeSinceAppStartInSecond; commandAllocator->Reset(); gCommandList.Reset(gCommandAllocator.GetCommandAllocator()); - BeginRenderToSwapChain(commandList); - commandList->SetPipelineState(pso); - commandList->SetGraphicsRootSignature(rootSignature); - commandList->SetDescriptorHeaps(_countof(descriptorHeaps), descriptorHeaps); - commandList->SetGraphicsRootConstantBufferView(0, cb->GetGPUVirtualAddress()); - commandList->SetGraphicsRoot32BitConstants(1, 4, color, 0); - commandList->SetGraphicsRootDescriptorTable(2, srvHeap->GetGPUDescriptorHandleForHeapStart()); - commandList->SetGraphicsRootShaderResourceView(3, sb->GetGPUVirtualAddress()); - commandList->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); - staticMeshComponent.Render(commandList); + BeginRenderToSwapChain(gCommandList.GetCommandList()); + gCommandList.SetPipelineState(pso); + gCommandList.SetRootSignature(rootSignature); + gCommandList.SetDescriptorHeaps(_countof(descriptorHeaps), descriptorHeaps); + gCommandList.SetGraphicsRootConstantBufferView(0, cb->GetGPUVirtualAddress()); + gCommandList.SetGraphicsRoot32BitConstants(1, 4, color, 0); + gCommandList.SetGraphicsRootDescriptorTable(2, srvHeap->GetGPUDescriptorHandleForHeapStart()); + gCommandList.SetGraphicsRootShaderResourceView(3, sb->GetGPUVirtualAddress()); + gCommandList.SetPrimitiveTopology(XCEngine::RHI::PrimitiveTopology::TriangleList); + staticMeshComponent.Render(gCommandList.GetCommandList()); // On screenshot frame, don't transition to PRESENT - keep RENDER_TARGET for screenshot if (frameCount != 30) { - EndRenderToSwapChain(commandList); + EndRenderToSwapChain(gCommandList.GetCommandList()); } EndCommandList();