From 4f42fb230277b67ec8ce3493d7dcb43026412fc6 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Sat, 4 Jan 2025 10:55:55 +0100 Subject: [PATCH] Update other graphics apis to match missing vertex shader inputs merging --- Source/Engine/Graphics/DynamicBuffer.cpp | 5 +++++ Source/Engine/Graphics/DynamicBuffer.h | 2 ++ Source/Engine/Graphics/Shaders/GPUVertexLayout.cpp | 8 +++----- Source/Engine/Graphics/Shaders/GPUVertexLayout.h | 2 +- .../Engine/GraphicsDevice/DirectX/DX11/GPUShaderDX11.cpp | 4 ++-- .../GraphicsDevice/DirectX/DX12/GPUPipelineStateDX12.cpp | 3 +-- .../GraphicsDevice/Vulkan/GPUPipelineStateVulkan.cpp | 3 +-- 7 files changed, 15 insertions(+), 12 deletions(-) diff --git a/Source/Engine/Graphics/DynamicBuffer.cpp b/Source/Engine/Graphics/DynamicBuffer.cpp index 67192a821..dcd885a42 100644 --- a/Source/Engine/Graphics/DynamicBuffer.cpp +++ b/Source/Engine/Graphics/DynamicBuffer.cpp @@ -67,6 +67,11 @@ void DynamicBuffer::Dispose() Data.Resize(0); } +GPUVertexLayout* DynamicVertexBuffer::GetLayout() const +{ + return _layout ? _layout : (GetBuffer() ? GetBuffer()->GetVertexLayout() : nullptr); +} + void DynamicVertexBuffer::SetLayout(GPUVertexLayout* layout) { _layout = layout; diff --git a/Source/Engine/Graphics/DynamicBuffer.h b/Source/Engine/Graphics/DynamicBuffer.h index 6cc72d26b..fffb4bba7 100644 --- a/Source/Engine/Graphics/DynamicBuffer.h +++ b/Source/Engine/Graphics/DynamicBuffer.h @@ -144,6 +144,8 @@ public: { } + // Gets the vertex buffer layout. + GPUVertexLayout* GetLayout() const; // Sets the vertex buffer layout. void SetLayout(GPUVertexLayout* layout); diff --git a/Source/Engine/Graphics/Shaders/GPUVertexLayout.cpp b/Source/Engine/Graphics/Shaders/GPUVertexLayout.cpp index edf3b9921..524b0d4cd 100644 --- a/Source/Engine/Graphics/Shaders/GPUVertexLayout.cpp +++ b/Source/Engine/Graphics/Shaders/GPUVertexLayout.cpp @@ -208,12 +208,10 @@ GPUVertexLayout* GPUVertexLayout::Get(const Span& layouts) return result; } -GPUVertexLayout* GPUVertexLayout::Merge(GPUVertexLayout* base, const GPUVertexLayout* reference) +GPUVertexLayout* GPUVertexLayout::Merge(GPUVertexLayout* base, GPUVertexLayout* reference) { - if (!reference || !base || base == reference) - return base; - GPUVertexLayout* result = base; - if (base && reference) + GPUVertexLayout* result = base ? base : reference; + if (base && reference && base != reference) { bool anyMissing = false; const Elements& baseElements = base->GetElements(); diff --git a/Source/Engine/Graphics/Shaders/GPUVertexLayout.h b/Source/Engine/Graphics/Shaders/GPUVertexLayout.h index 9bf606ec1..9212626a3 100644 --- a/Source/Engine/Graphics/Shaders/GPUVertexLayout.h +++ b/Source/Engine/Graphics/Shaders/GPUVertexLayout.h @@ -75,7 +75,7 @@ public: /// The list of vertex buffers for the layout. /// The list of reference inputs. /// Vertex layout object. Doesn't need to be cleared as it's cached for an application lifetime. - static GPUVertexLayout* Merge(GPUVertexLayout* base, const GPUVertexLayout* reference); + static GPUVertexLayout* Merge(GPUVertexLayout* base, GPUVertexLayout* reference); public: // [GPUResource] diff --git a/Source/Engine/GraphicsDevice/DirectX/DX11/GPUShaderDX11.cpp b/Source/Engine/GraphicsDevice/DirectX/DX11/GPUShaderDX11.cpp index 7dcfb0d31..eb49171dc 100644 --- a/Source/Engine/GraphicsDevice/DirectX/DX11/GPUShaderDX11.cpp +++ b/Source/Engine/GraphicsDevice/DirectX/DX11/GPUShaderDX11.cpp @@ -26,8 +26,8 @@ ID3D11InputLayout* GPUShaderProgramVSDX11::GetInputLayout(GPUVertexLayoutDX11* v vertexLayout = (GPUVertexLayoutDX11*)Layout; if (vertexLayout && vertexLayout->InputElementsCount) { - auto actualLayout = (GPUVertexLayoutDX11*)GPUVertexLayout::Merge(vertexLayout, Layout); - LOG_DIRECTX_RESULT(vertexLayout->GetDevice()->GetDevice()->CreateInputLayout(actualLayout->InputElements, actualLayout->InputElementsCount, Bytecode.Get(), Bytecode.Length(), &inputLayout)); + auto mergedVertexLayout = (GPUVertexLayoutDX11*)GPUVertexLayout::Merge(vertexLayout, Layout); + LOG_DIRECTX_RESULT(vertexLayout->GetDevice()->GetDevice()->CreateInputLayout(mergedVertexLayout->InputElements, mergedVertexLayout->InputElementsCount, Bytecode.Get(), Bytecode.Length(), &inputLayout)); } _cache.Add(vertexLayout, inputLayout); } diff --git a/Source/Engine/GraphicsDevice/DirectX/DX12/GPUPipelineStateDX12.cpp b/Source/Engine/GraphicsDevice/DirectX/DX12/GPUPipelineStateDX12.cpp index ecfce4eca..7653992ce 100644 --- a/Source/Engine/GraphicsDevice/DirectX/DX12/GPUPipelineStateDX12.cpp +++ b/Source/Engine/GraphicsDevice/DirectX/DX12/GPUPipelineStateDX12.cpp @@ -49,8 +49,6 @@ bool GPUPipelineStateDX12::IsValid() const ID3D12PipelineState* GPUPipelineStateDX12::GetState(GPUTextureViewDX12* depth, int32 rtCount, GPUTextureViewDX12** rtHandles, GPUVertexLayoutDX12* vertexLayout) { ASSERT(depth || rtCount); - if (!vertexLayout) - vertexLayout = VertexLayout; // Prepare key GPUPipelineStateKeyDX12 key; @@ -85,6 +83,7 @@ ID3D12PipelineState* GPUPipelineStateDX12::GetState(GPUTextureViewDX12* depth, i _desc.SampleDesc.Quality = key.MSAA == MSAALevel::None ? 0 : GPUDeviceDX12::GetMaxMSAAQuality((int32)key.MSAA); _desc.SampleMask = D3D12_DEFAULT_SAMPLE_MASK; _desc.DSVFormat = RenderToolsDX::ToDxgiFormat(PixelFormatExtensions::FindDepthStencilFormat(key.DepthFormat)); + vertexLayout = (GPUVertexLayoutDX12*)GPUVertexLayout::Merge(vertexLayout, VertexLayout); _desc.InputLayout.pInputElementDescs = vertexLayout ? vertexLayout->InputElements : nullptr; _desc.InputLayout.NumElements = vertexLayout ? vertexLayout->InputElementsCount : 0; diff --git a/Source/Engine/GraphicsDevice/Vulkan/GPUPipelineStateVulkan.cpp b/Source/Engine/GraphicsDevice/Vulkan/GPUPipelineStateVulkan.cpp index 2b50ef626..7f3896038 100644 --- a/Source/Engine/GraphicsDevice/Vulkan/GPUPipelineStateVulkan.cpp +++ b/Source/Engine/GraphicsDevice/Vulkan/GPUPipelineStateVulkan.cpp @@ -205,8 +205,6 @@ PipelineLayoutVulkan* GPUPipelineStateVulkan::GetLayout() VkPipeline GPUPipelineStateVulkan::GetState(RenderPassVulkan* renderPass, GPUVertexLayoutVulkan* vertexLayout) { ASSERT(renderPass); - if (!vertexLayout) - vertexLayout = VertexShaderLayout; // Try reuse cached version VkPipeline pipeline = VK_NULL_HANDLE; @@ -224,6 +222,7 @@ VkPipeline GPUPipelineStateVulkan::GetState(RenderPassVulkan* renderPass, GPUVer PROFILE_CPU_NAMED("Create Pipeline"); // Bind vertex input + vertexLayout = (GPUVertexLayoutVulkan*)GPUVertexLayout::Merge(vertexLayout, VertexShaderLayout); _desc.pVertexInputState = vertexLayout ? &vertexLayout->CreateInfo : nullptr; // Update description to match the pipeline