From 6e26ec71030e680eede852ff9f71ac4eea4bd3eb Mon Sep 17 00:00:00 2001 From: ssdfasd <2156608475@qq.com> Date: Sun, 19 Apr 2026 00:30:42 +0800 Subject: [PATCH] refactor(mvs): simplify 3dgs d3d12 render path --- MVS/3DGS-D3D12/CMakeLists.txt | 4 - MVS/3DGS-D3D12/include/XC3DGSD3D12/App.h | 9 -- MVS/3DGS-D3D12/shaders/CompositePS.hlsl | 12 -- MVS/3DGS-D3D12/shaders/CompositeVS.hlsl | 12 -- MVS/3DGS-D3D12/shaders/DebugPointsVS.hlsl | 4 +- .../shaders/PrepareGaussiansCS.hlsl | 8 +- MVS/3DGS-D3D12/src/App.cpp | 132 +----------------- 7 files changed, 7 insertions(+), 174 deletions(-) delete mode 100644 MVS/3DGS-D3D12/shaders/CompositePS.hlsl delete mode 100644 MVS/3DGS-D3D12/shaders/CompositeVS.hlsl diff --git a/MVS/3DGS-D3D12/CMakeLists.txt b/MVS/3DGS-D3D12/CMakeLists.txt index a1df8573..83e4dbe4 100644 --- a/MVS/3DGS-D3D12/CMakeLists.txt +++ b/MVS/3DGS-D3D12/CMakeLists.txt @@ -42,8 +42,6 @@ add_executable(xc_3dgs_d3d12_mvs shaders/DeviceRadixSort.hlsl shaders/DebugPointsVS.hlsl shaders/DebugPointsPS.hlsl - shaders/CompositeVS.hlsl - shaders/CompositePS.hlsl ) set_source_files_properties( @@ -54,8 +52,6 @@ set_source_files_properties( shaders/DeviceRadixSort.hlsl shaders/DebugPointsVS.hlsl shaders/DebugPointsPS.hlsl - shaders/CompositeVS.hlsl - shaders/CompositePS.hlsl PROPERTIES HEADER_FILE_ONLY TRUE ) diff --git a/MVS/3DGS-D3D12/include/XC3DGSD3D12/App.h b/MVS/3DGS-D3D12/include/XC3DGSD3D12/App.h index 20c97a07..4282c018 100644 --- a/MVS/3DGS-D3D12/include/XC3DGSD3D12/App.h +++ b/MVS/3DGS-D3D12/include/XC3DGSD3D12/App.h @@ -67,12 +67,10 @@ private: bool InitializePreparePassResources(); bool InitializeSortResources(); bool InitializeDebugDrawResources(); - bool InitializeCompositeResources(); void ShutdownGaussianGpuResources(); void ShutdownPreparePassResources(); void ShutdownSortResources(); void ShutdownDebugDrawResources(); - void ShutdownCompositeResources(); void Shutdown(); bool CaptureSortSnapshot(); bool CapturePass3HistogramDebug(); @@ -139,13 +137,6 @@ private: XCEngine::RHI::RHIPipelineState* m_debugPipelineState = nullptr; XCEngine::RHI::RHIDescriptorPool* m_debugDescriptorPool = nullptr; XCEngine::RHI::RHIDescriptorSet* m_debugDescriptorSet = nullptr; - XCEngine::RHI::D3D12Texture m_splatRenderTargetTexture; - std::unique_ptr m_splatRenderTargetRtv; - std::unique_ptr m_splatRenderTargetSrv; - XCEngine::RHI::RHIPipelineLayout* m_compositePipelineLayout = nullptr; - XCEngine::RHI::RHIPipelineState* m_compositePipelineState = nullptr; - XCEngine::RHI::RHIDescriptorPool* m_compositeDescriptorPool = nullptr; - XCEngine::RHI::RHIDescriptorSet* m_compositeDescriptorSet = nullptr; XCEngine::RHI::D3D12Device m_device; XCEngine::RHI::D3D12CommandQueue m_commandQueue; diff --git a/MVS/3DGS-D3D12/shaders/CompositePS.hlsl b/MVS/3DGS-D3D12/shaders/CompositePS.hlsl deleted file mode 100644 index 3c83ed25..00000000 --- a/MVS/3DGS-D3D12/shaders/CompositePS.hlsl +++ /dev/null @@ -1,12 +0,0 @@ -Texture2D gSplatTexture : register(t0); - -struct PixelInput -{ - float4 position : SV_Position; -}; - -float4 MainPS(PixelInput input) : SV_Target0 -{ - float4 color = gSplatTexture.Load(int3(int2(input.position.xy), 0)); - return float4(color.rgb, color.a); -} diff --git a/MVS/3DGS-D3D12/shaders/CompositeVS.hlsl b/MVS/3DGS-D3D12/shaders/CompositeVS.hlsl deleted file mode 100644 index 7c4bd857..00000000 --- a/MVS/3DGS-D3D12/shaders/CompositeVS.hlsl +++ /dev/null @@ -1,12 +0,0 @@ -struct VertexOutput -{ - float4 position : SV_Position; -}; - -VertexOutput MainVS(uint vertexId : SV_VertexID) -{ - VertexOutput output = (VertexOutput)0; - float2 quadPosition = float2(vertexId & 1, (vertexId >> 1) & 1) * 4.0 - 1.0; - output.position = float4(quadPosition, 1.0, 1.0); - return output; -} diff --git a/MVS/3DGS-D3D12/shaders/DebugPointsVS.hlsl b/MVS/3DGS-D3D12/shaders/DebugPointsVS.hlsl index 62e32d9a..359fa4c5 100644 --- a/MVS/3DGS-D3D12/shaders/DebugPointsVS.hlsl +++ b/MVS/3DGS-D3D12/shaders/DebugPointsVS.hlsl @@ -37,8 +37,10 @@ VertexOutput MainVS(uint vertexId : SV_VertexID, uint instanceId : SV_InstanceID float2 quadPosition = float2(vertexId & 1, (vertexId >> 1) & 1) * 2.0 - 1.0; quadPosition *= 2.0; + float2 scaledAxis1 = view.axis1 * gSettings.w; + float2 scaledAxis2 = view.axis2 * gSettings.w; float2 deltaScreenPosition = - (quadPosition.x * view.axis1 + quadPosition.y * view.axis2) * 2.0 / gScreenParams.xy; + (quadPosition.x * scaledAxis1 + quadPosition.y * scaledAxis2) * 2.0 / gScreenParams.xy; output.position = view.clipPosition; output.position.xy += deltaScreenPosition * view.clipPosition.w; diff --git a/MVS/3DGS-D3D12/shaders/PrepareGaussiansCS.hlsl b/MVS/3DGS-D3D12/shaders/PrepareGaussiansCS.hlsl index ff5d3975..77c531b8 100644 --- a/MVS/3DGS-D3D12/shaders/PrepareGaussiansCS.hlsl +++ b/MVS/3DGS-D3D12/shaders/PrepareGaussiansCS.hlsl @@ -115,7 +115,7 @@ float3x3 CalcMatrixFromRotationScale(float4 rotation, float3 scale) 2 * (x * y + w * z), 1 - 2 * (x * x + z * z), 2 * (y * z - w * x), 2 * (x * z - w * y), 2 * (y * z + w * x), 1 - 2 * (x * x + y * y)); - return mul(rotationMatrix, scaleMatrix); + return mul(transpose(rotationMatrix), scaleMatrix); } void CalcCovariance3D(float3x3 rotationScaleMatrix, out float3 sigma0, out float3 sigma1) @@ -129,9 +129,8 @@ float3 CalcCovariance2D(float3 worldPosition, float3 covariance0, float3 covaria { float3 viewPosition = mul(float4(worldPosition, 1.0), gView).xyz; - float aspect = gProjection._m00 / gProjection._m11; float tanFovX = rcp(gProjection._m00); - float tanFovY = rcp(gProjection._m11 * aspect); + float tanFovY = rcp(gProjection._m11); float clampX = 1.3 * tanFovX; float clampY = 1.3 * tanFovY; viewPosition.x = clamp(viewPosition.x / viewPosition.z, -clampX, clampX) * viewPosition.z; @@ -253,9 +252,6 @@ void MainCS(uint3 dispatchThreadId : SV_DispatchThreadID) float3 covariance0; float3 covariance1; CalcCovariance3D(rotationScale, covariance0, covariance1); - float splatScaleSquared = gSettings.w * gSettings.w; - covariance0 *= splatScaleSquared; - covariance1 *= splatScaleSquared; float3 covariance2D = CalcCovariance2D(position, covariance0, covariance1); DecomposeCovariance(covariance2D, view.axis1, view.axis2); diff --git a/MVS/3DGS-D3D12/src/App.cpp b/MVS/3DGS-D3D12/src/App.cpp index 0a33ee0f..61d41115 100644 --- a/MVS/3DGS-D3D12/src/App.cpp +++ b/MVS/3DGS-D3D12/src/App.cpp @@ -134,7 +134,7 @@ void StoreMatrixTransposed(const DirectX::XMMATRIX& matrix, float* destination) DirectX::XMMATRIX BuildSortViewMatrix() { using namespace DirectX; const XMVECTOR eye = XMVectorSet(0.0f, 0.5f, 1.0f, 1.0f); - const XMVECTOR target = XMVectorSet(0.0f, 0.5f, -5.0f, 1.0f); + const XMVECTOR target = XMVectorSet(3.0f, 0.5f, 6.196152f, 1.0f); const XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); return XMMatrixLookAtRH(eye, target, up); } @@ -1140,118 +1140,6 @@ bool App::InitializeDebugDrawResources() { return true; } -bool App::InitializeCompositeResources() { - D3D12_RESOURCE_DESC splatRenderTargetDesc = {}; - splatRenderTargetDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; - splatRenderTargetDesc.Alignment = 0; - splatRenderTargetDesc.Width = static_cast(m_width); - splatRenderTargetDesc.Height = static_cast(m_height); - splatRenderTargetDesc.DepthOrArraySize = 1; - splatRenderTargetDesc.MipLevels = 1; - splatRenderTargetDesc.Format = DXGI_FORMAT_R16G16B16A16_FLOAT; - splatRenderTargetDesc.SampleDesc.Count = 1; - splatRenderTargetDesc.SampleDesc.Quality = 0; - splatRenderTargetDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; - splatRenderTargetDesc.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET; - - if (!m_splatRenderTargetTexture.Initialize( - m_device.GetDevice(), - splatRenderTargetDesc, - D3D12_RESOURCE_STATE_RENDER_TARGET)) { - m_lastErrorMessage = L"Failed to create the splat accumulation render target texture."; - return false; - } - m_splatRenderTargetTexture.SetState(ResourceStates::RenderTarget); - - ResourceViewDesc renderTargetViewDesc = {}; - renderTargetViewDesc.dimension = ResourceViewDimension::Texture2D; - renderTargetViewDesc.format = static_cast(Format::R16G16B16A16_Float); - m_splatRenderTargetRtv.reset(static_cast( - m_device.CreateRenderTargetView(&m_splatRenderTargetTexture, renderTargetViewDesc))); - if (!m_splatRenderTargetRtv) { - m_lastErrorMessage = L"Failed to create the splat accumulation render target view."; - return false; - } - - m_splatRenderTargetSrv.reset(static_cast( - m_device.CreateShaderResourceView(&m_splatRenderTargetTexture, renderTargetViewDesc))); - if (!m_splatRenderTargetSrv) { - m_lastErrorMessage = L"Failed to create the splat accumulation shader resource view."; - return false; - } - - DescriptorSetLayoutBinding bindings[1] = {}; - bindings[0].binding = 0; - bindings[0].type = static_cast(DescriptorType::SRV); - bindings[0].count = 1; - bindings[0].visibility = static_cast(ShaderVisibility::Pixel); - bindings[0].resourceDimension = ResourceViewDimension::Texture2D; - - DescriptorSetLayoutDesc setLayout = {}; - setLayout.bindings = bindings; - setLayout.bindingCount = 1; - - RHIPipelineLayoutDesc pipelineLayoutDesc = {}; - pipelineLayoutDesc.setLayouts = &setLayout; - pipelineLayoutDesc.setLayoutCount = 1; - - m_compositePipelineLayout = m_device.CreatePipelineLayout(pipelineLayoutDesc); - if (m_compositePipelineLayout == nullptr) { - m_lastErrorMessage = L"Failed to create the composite pipeline layout."; - return false; - } - - DescriptorPoolDesc poolDesc = {}; - poolDesc.type = DescriptorHeapType::CBV_SRV_UAV; - poolDesc.descriptorCount = 1; - poolDesc.shaderVisible = true; - m_compositeDescriptorPool = m_device.CreateDescriptorPool(poolDesc); - if (m_compositeDescriptorPool == nullptr) { - m_lastErrorMessage = L"Failed to create the composite descriptor pool."; - return false; - } - - m_compositeDescriptorSet = m_compositeDescriptorPool->AllocateSet(setLayout); - if (m_compositeDescriptorSet == nullptr) { - m_lastErrorMessage = L"Failed to allocate the composite descriptor set."; - return false; - } - m_compositeDescriptorSet->Update(0, m_splatRenderTargetSrv.get()); - - GraphicsPipelineDesc pipelineDesc = {}; - pipelineDesc.pipelineLayout = m_compositePipelineLayout; - pipelineDesc.topologyType = static_cast(PrimitiveTopologyType::Triangle); - pipelineDesc.renderTargetCount = 1; - pipelineDesc.renderTargetFormats[0] = static_cast(Format::R8G8B8A8_UNorm); - pipelineDesc.depthStencilFormat = static_cast(Format::Unknown); - pipelineDesc.sampleCount = 1; - pipelineDesc.sampleQuality = 0; - pipelineDesc.rasterizerState.cullMode = static_cast(CullMode::None); - pipelineDesc.depthStencilState.depthTestEnable = false; - pipelineDesc.depthStencilState.depthWriteEnable = false; - pipelineDesc.blendState.blendEnable = true; - pipelineDesc.blendState.srcBlend = static_cast(BlendFactor::SrcAlpha); - pipelineDesc.blendState.dstBlend = static_cast(BlendFactor::InvSrcAlpha); - pipelineDesc.blendState.srcBlendAlpha = static_cast(BlendFactor::One); - pipelineDesc.blendState.dstBlendAlpha = static_cast(BlendFactor::InvSrcAlpha); - - pipelineDesc.vertexShader.fileName = ResolveShaderPath(L"CompositeVS.hlsl").wstring(); - pipelineDesc.vertexShader.entryPoint = L"MainVS"; - pipelineDesc.vertexShader.profile = L"vs_5_0"; - - pipelineDesc.fragmentShader.fileName = ResolveShaderPath(L"CompositePS.hlsl").wstring(); - pipelineDesc.fragmentShader.entryPoint = L"MainPS"; - pipelineDesc.fragmentShader.profile = L"ps_5_0"; - - m_compositePipelineState = m_device.CreatePipelineState(pipelineDesc); - if (m_compositePipelineState == nullptr) { - m_lastErrorMessage = L"Failed to create the composite pipeline state."; - return false; - } - - return true; -} - void App::ShutdownGaussianGpuResources() { m_gaussianColorView.reset(); m_gaussianShView.reset(); @@ -1368,20 +1256,6 @@ void App::ShutdownDebugDrawResources() { ShutdownAndDelete(m_debugPipelineLayout); } -void App::ShutdownCompositeResources() { - if (m_compositeDescriptorSet != nullptr) { - m_compositeDescriptorSet->Shutdown(); - delete m_compositeDescriptorSet; - m_compositeDescriptorSet = nullptr; - } - ShutdownAndDelete(m_compositeDescriptorPool); - ShutdownAndDelete(m_compositePipelineState); - ShutdownAndDelete(m_compositePipelineLayout); - m_splatRenderTargetSrv.reset(); - m_splatRenderTargetRtv.reset(); - m_splatRenderTargetTexture.Shutdown(); -} - void App::Shutdown() { AppendTrace("Shutdown: begin"); if (!m_isInitialized && m_hwnd == nullptr) { @@ -1796,8 +1670,8 @@ void App::RenderFrame(bool captureScreenshot) { const int currentBackBufferIndex = m_swapChain.GetCurrentBackBufferIndex(); D3D12Texture& backBuffer = m_swapChain.GetBackBuffer(currentBackBufferIndex); + m_commandList.TransitionBarrier(backBuffer.GetResource(), ResourceStates::Present, ResourceStates::RenderTarget); - backBuffer.SetState(ResourceStates::RenderTarget); const CPUDescriptorHandle rtvCpuHandle = m_rtvHeap.GetCPUDescriptorHandle(currentBackBufferIndex); const CPUDescriptorHandle dsvCpuHandle = m_dsvHeap.GetCPUDescriptorHandle(0); @@ -2160,14 +2034,12 @@ void App::RenderFrame(bool captureScreenshot) { m_commandAllocator.Reset(); m_commandList.Reset(); m_commandList.TransitionBarrier(backBuffer.GetResource(), ResourceStates::RenderTarget, ResourceStates::Present); - backBuffer.SetState(ResourceStates::Present); m_commandList.Close(); void* presentCommandLists[] = { &m_commandList }; AppendTrace("RenderFrame: execute final present-transition list"); m_commandQueue.ExecuteCommandLists(1, presentCommandLists); } else { m_commandList.TransitionBarrier(backBuffer.GetResource(), ResourceStates::RenderTarget, ResourceStates::Present); - backBuffer.SetState(ResourceStates::Present); m_commandList.Close(); void* commandLists[] = { &m_commandList }; AppendTrace("RenderFrame: execute+present");