Vulkan pipeline #2 17/317217/2
authorAdam Bialogonski <adam.b@samsung.com>
Thu, 5 Sep 2024 15:48:15 +0000 (16:48 +0100)
committerAdam Bialogonski <adam.b@samsung.com>
Fri, 6 Sep 2024 12:17:46 +0000 (13:17 +0100)
- Pipeline created with RenderTarget (requires core patch)
- Command buffer binds pipeline

Change-Id: Ibc9835b9191aa20aa4ac198d81557eef56bbbf86

dali/internal/graphics/vulkan-impl/vulkan-command-buffer-impl.cpp
dali/internal/graphics/vulkan-impl/vulkan-command-buffer-impl.h
dali/internal/graphics/vulkan-impl/vulkan-command-buffer.cpp
dali/internal/graphics/vulkan-impl/vulkan-framebuffer-impl.cpp
dali/internal/graphics/vulkan-impl/vulkan-framebuffer-impl.h
dali/internal/graphics/vulkan-impl/vulkan-pipeline-impl.cpp
dali/internal/graphics/vulkan-impl/vulkan-pipeline-impl.h
dali/internal/graphics/vulkan-impl/vulkan-pipeline.h

index 5dbae8c3dbae03fc2a86ff3ded8cbcfea7c875d8..c1b931aff1bdf4fa4deb37ba17e8a897fc08e522 100644 (file)
 // INTERNAL INCLUDES
 #include <dali/internal/graphics/vulkan-impl/vulkan-buffer-impl.h>
 #include <dali/internal/graphics/vulkan-impl/vulkan-buffer.h>
+#include <dali/internal/graphics/vulkan-impl/vulkan-command-buffer.h>
 #include <dali/internal/graphics/vulkan-impl/vulkan-command-pool-impl.h>
 #include <dali/internal/graphics/vulkan-impl/vulkan-framebuffer-impl.h>
 #include <dali/internal/graphics/vulkan-impl/vulkan-image-impl.h>
+#include <dali/internal/graphics/vulkan-impl/vulkan-pipeline-impl.h>
 #include <dali/internal/graphics/vulkan-impl/vulkan-swapchain-impl.h>
 #include <dali/internal/graphics/vulkan-impl/vulkan-types.h>
 #include <dali/internal/graphics/vulkan/vulkan-device.h>
@@ -33,7 +35,6 @@ namespace Graphics
 {
 namespace Vulkan
 {
-
 CommandBufferImpl::CommandBufferImpl(CommandPool&                         commandPool,
                                      uint32_t                             poolIndex,
                                      const vk::CommandBufferAllocateInfo& allocateInfo,
@@ -98,6 +99,22 @@ void CommandBufferImpl::Free()
   mGraphicsDevice->GetLogicalDevice().freeCommandBuffers(mOwnerCommandPool->GetVkHandle(), mCommandBuffer);
 }
 
+void CommandBufferImpl::BindPipeline(const Graphics::Pipeline* pipeline)
+{
+  assert(mCommandBuffer && "Invalid command buffer!");
+  assert(mRecording && "Can't bind pipeline when buffer isn't recording!");
+  assert(pipeline && "Can't bind null pipeline!");
+
+  auto& pipelineImpl = static_cast<const Vulkan::Pipeline*>(pipeline)->GetImpl();
+
+  // Bind if pipeline is ready (if nullptr, pipeline isn't ready).
+  // If pipeline is valid, bind it early
+  if(pipelineImpl.GetVkPipeline())
+  {
+    mCommandBuffer.bindPipeline(vk::PipelineBindPoint::eGraphics, pipelineImpl.GetVkPipeline());
+  }
+}
+
 vk::CommandBuffer CommandBufferImpl::GetVkHandle() const
 {
   return mCommandBuffer;
@@ -160,6 +177,32 @@ bool CommandBufferImpl::OnDestroy()
   return true;
 }
 
+void CommandBufferImpl::Draw(uint32_t vertexCount,
+                             uint32_t instanceCount,
+                             uint32_t firstVertex,
+                             uint32_t firstInstance)
+{
+}
+
+void CommandBufferImpl::DrawIndexed(uint32_t indexCount,
+                                    uint32_t instanceCount,
+                                    uint32_t firstIndex,
+                                    int32_t  vertexOffset,
+                                    uint32_t firstInstance)
+{
+}
+
+void CommandBufferImpl::DrawIndexedIndirect(Graphics::Buffer& buffer,
+                                            uint32_t          offset,
+                                            uint32_t          drawCount,
+                                            uint32_t          stride)
+{
+}
+
+void CommandBufferImpl::ExecuteCommandBuffers(std::vector<const Graphics::CommandBuffer*>&& commandBuffers)
+{
+}
+
 } // namespace Vulkan
 } // namespace Graphics
 } // namespace Dali
index e72147c9dc1941097de2e63f8a11bb109938bf27..6f9cda2f873113eb56daf4b7e9a7c7ea994aaac4 100644 (file)
@@ -29,6 +29,7 @@ namespace Dali::Graphics::Vulkan
 class Buffer;
 class Device;
 class CommandPool;
+class PipelineImpl;
 
 class CommandBufferImpl : public VkManaged
 {
@@ -53,6 +54,12 @@ public:
   /** Free command buffer */
   void Free();
 
+  /** Binds Vulkan pipeline */
+  void BindPipeline(const Graphics::Pipeline* pipeline);
+
+  /** Final validation of the pipeline */
+  void ValidatePipeline();
+
   /** Returns Vulkan object associated with the buffer */
   [[nodiscard]] vk::CommandBuffer GetVkHandle() const;
 
@@ -91,6 +98,27 @@ public:
    */
   bool OnDestroy() override;
 
+  void Draw(
+    uint32_t vertexCount,
+    uint32_t instanceCount,
+    uint32_t firstVertex,
+    uint32_t firstInstance);
+
+  void DrawIndexed(
+    uint32_t indexCount,
+    uint32_t instanceCount,
+    uint32_t firstIndex,
+    int32_t  vertexOffset,
+    uint32_t firstInstance);
+
+  void DrawIndexedIndirect(
+    Graphics::Buffer& buffer,
+    uint32_t          offset,
+    uint32_t          drawCount,
+    uint32_t          stride);
+
+  void ExecuteCommandBuffers(std::vector<const Graphics::CommandBuffer*>&& commandBuffers);
+
 private:
   /**
    * Returns allocation index
index ea3a8e8bc638c3f20a8a8d07fb7f79779e1b04bd..89043db2afb1745a68dd3f08261aa6214fbcfaff 100644 (file)
@@ -27,7 +27,6 @@
 
 namespace Dali::Graphics::Vulkan
 {
-
 CommandBuffer::CommandBuffer(const Graphics::CommandBufferCreateInfo& createInfo, VulkanGraphicsController& controller)
 : CommandBufferResource(createInfo, controller),
   mCommandBufferImpl(nullptr)
@@ -94,6 +93,7 @@ void CommandBuffer::BindUniformBuffers(const std::vector<UniformBufferBinding>&
 
 void CommandBuffer::BindPipeline(const Graphics::Pipeline& pipeline)
 {
+  mCommandBufferImpl->BindPipeline(&pipeline);
 }
 
 void CommandBuffer::BindTextures(const std::vector<TextureBinding>& textureBindings)
@@ -184,6 +184,7 @@ void CommandBuffer::Draw(uint32_t vertexCount,
                          uint32_t firstVertex,
                          uint32_t firstInstance)
 {
+  mCommandBufferImpl->Draw(vertexCount, instanceCount, firstVertex, firstInstance);
 }
 
 void CommandBuffer::DrawIndexed(uint32_t indexCount,
@@ -192,6 +193,7 @@ void CommandBuffer::DrawIndexed(uint32_t indexCount,
                                 int32_t  vertexOffset,
                                 uint32_t firstInstance)
 {
+  mCommandBufferImpl->DrawIndexed(indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
 }
 
 void CommandBuffer::DrawIndexedIndirect(Graphics::Buffer& buffer,
@@ -199,6 +201,7 @@ void CommandBuffer::DrawIndexedIndirect(Graphics::Buffer& buffer,
                                         uint32_t          drawCount,
                                         uint32_t          stride)
 {
+  mCommandBufferImpl->DrawIndexedIndirect(buffer, offset, drawCount, stride);
 }
 
 void CommandBuffer::DrawNative(const DrawNativeInfo* drawInfo)
index 4810bf3603b4607a41c5ac36cbb4872f41626f45..92f6c845ddc03947c472a97fdd40f0e3aeec6e57 100644 (file)
@@ -32,7 +32,6 @@ extern Debug::Filter* gVulkanFilter;
 
 namespace Dali::Graphics::Vulkan
 {
-
 FramebufferAttachment* FramebufferAttachment::NewColorAttachment(ImageView*          imageView,
                                                                  vk::ClearColorValue clearColorValue,
                                                                  bool                presentable)
@@ -133,8 +132,7 @@ FramebufferImpl* FramebufferImpl::New(
   std::transform(attachments.cbegin(),
                  attachments.cend(),
                  std::back_inserter(imageViewAttachments),
-                 [&](FramebufferAttachment* entry)
-                 {
+                 [&](FramebufferAttachment* entry) {
                    return entry->GetImageView()->GetVkHandle();
                  });
 
@@ -302,6 +300,20 @@ uint32_t FramebufferImpl::GetAttachmentCount(AttachmentType type) const
   return 0u;
 }
 
+uint32_t FramebufferImpl::GetRenderPassCount() const
+{
+  return uint32_t(mRenderPasses.size());
+}
+
+RenderPassImpl* FramebufferImpl::GetRenderPass(uint32_t index) const
+{
+  if(index < mRenderPasses.size())
+  {
+    return mRenderPasses[index].renderPassImpl;
+  }
+  return nullptr;
+}
+
 RenderPassImpl* FramebufferImpl::GetRenderPass(RenderPass* renderPass)
 {
   auto attachments  = renderPass->GetCreateInfo().attachments;
@@ -352,8 +364,7 @@ std::vector<vk::ClearValue> FramebufferImpl::GetClearValues() const
   std::transform(mAttachments.begin(), // @todo & color clear enabled / depth clear enabled
                  mAttachments.end(),
                  std::back_inserter(result),
-                 [](FramebufferAttachment* attachment)
-                 {
+                 [](FramebufferAttachment* attachment) {
                    return attachment->GetClearValue();
                  });
 
@@ -370,8 +381,7 @@ bool FramebufferImpl::OnDestroy()
 
   auto allocator = &mGraphicsDevice->GetAllocator();
 
-  mGraphicsDevice->DiscardResource([device, frameBuffer, renderPass, allocator]()
-                                   {
+  mGraphicsDevice->DiscardResource([device, frameBuffer, renderPass, allocator]() {
                                      DALI_LOG_INFO(gVulkanFilter, Debug::General, "Invoking deleter function: framebuffer->%p\n", static_cast<VkFramebuffer>(frameBuffer))
                                      device.destroyFramebuffer(frameBuffer, allocator);
 
index bcf567d7d139d34e70816c37edd8b119fcad8bfe..defb3f4618049e2032d445b723271bd26aa832ca 100644 (file)
@@ -116,6 +116,10 @@ public:
 
   [[nodiscard]] RenderPassImpl* GetRenderPass(RenderPass* renderPass); // May mutate mRenderPasses
 
+  [[nodiscard]] RenderPassImpl* GetRenderPass(uint32_t index) const;
+
+  [[nodiscard]] uint32_t GetRenderPassCount() const;
+
   [[nodiscard]] vk::Framebuffer GetVkHandle() const;
 
   [[nodiscard]] std::vector<vk::ClearValue> GetClearValues() const;
index 1e1080978b47afffe790f2d6fb87142db58247a1..8bb63caadd6a36d547bbf6a62ec03c193fd76385 100644 (file)
 #include <vulkan/vulkan.hpp>
 
 // INTERNAL INCLUDES
+#include <dali/internal/graphics/vulkan-impl/vulkan-framebuffer-impl.h>
 #include <dali/internal/graphics/vulkan-impl/vulkan-graphics-controller.h>
 #include <dali/internal/graphics/vulkan-impl/vulkan-program-impl.h>
+#include <dali/internal/graphics/vulkan-impl/vulkan-render-pass-impl.h>
 #include <dali/internal/graphics/vulkan/vulkan-device.h>
+#include <dali/internal/window-system/common/window-render-surface.h>
 
 namespace Dali::Graphics::Vulkan
 {
@@ -144,8 +147,8 @@ struct PipelineImpl::PipelineState
   RasterizationState rasterizationState;
   VertexInputState   vertexInputState;
   InputAssemblyState inputAssemblyState;
-
-  PipelineCache* pipelineCache{};
+  RenderTarget*      renderTarget;
+  PipelineCache*     pipelineCache{};
 };
 
 PipelineImpl::PipelineImpl(const Graphics::PipelineCreateInfo& createInfo, VulkanGraphicsController& controller, PipelineCache* pipelineCache)
@@ -163,6 +166,7 @@ PipelineImpl::PipelineImpl(const Graphics::PipelineCreateInfo& createInfo, Vulka
   CopyStateIfSet(createInfo.colorBlendState, mPipelineState->colorBlendState, &mCreateInfo.colorBlendState);
   CopyStateIfSet(createInfo.depthStencilState, mPipelineState->depthStencilState, &mCreateInfo.depthStencilState);
   CopyStateIfSet(createInfo.viewportState, mPipelineState->viewportState, &mCreateInfo.viewportState);
+  mCreateInfo.renderTarget = createInfo.renderTarget;
 
   InitializePipeline();
 }
@@ -181,6 +185,11 @@ void PipelineImpl::Bind()
 {
 }
 
+vk::Pipeline PipelineImpl::GetVkPipeline() const
+{
+  return mVkPipelines[0].pipeline;
+}
+
 void PipelineImpl::Retain()
 {
   //++mRefCount;
@@ -210,10 +219,6 @@ void PipelineImpl::InitializePipeline()
   gfxPipelineInfo.setBasePipelineHandle(nullptr);
   gfxPipelineInfo.setBasePipelineIndex(0);
 
-  // TODO: to resolve
-  gfxPipelineInfo.setRenderPass({});
-  gfxPipelineInfo.setSubpass({});
-
   // 1. PipelineVertexInputStateCreateInfo
   vk::PipelineVertexInputStateCreateInfo visInfo;
   InitializeVertexInputState(visInfo);
@@ -248,6 +253,7 @@ void PipelineImpl::InitializePipeline()
 
   // 8. PipelineColorBlendStateCreateInfo
   vk::PipelineColorBlendStateCreateInfo bsInfo;
+  InitializeColorBlendState(bsInfo);
   gfxPipelineInfo.setPColorBlendState(&bsInfo);
 
   // 9. PipelineDynamicStateCreateInfo
@@ -255,13 +261,62 @@ void PipelineImpl::InitializePipeline()
   dynInfo.setDynamicStates(mDynamicStates);
   gfxPipelineInfo.setPDynamicState(&dynInfo);
 
-  auto& allocator = mController.GetGraphicsDevice().GetAllocator();
+  auto& allocator   = mController.GetGraphicsDevice().GetAllocator();
+  auto  rtImpl      = static_cast<Vulkan::RenderTarget*>(mCreateInfo.renderTarget);
+  auto  framebuffer = rtImpl->GetFramebuffer();
+  auto  surface     = rtImpl->GetSurface();
 
-  VkAssert(vkDevice.createGraphicsPipelines(VK_NULL_HANDLE,
-                                            1,
-                                            &gfxPipelineInfo,
-                                            &allocator,
-                                            &mVkPipeline));
+  FramebufferImpl* fbImpl = nullptr;
+  if(surface)
+  {
+    auto& gfxDevice = mController.GetGraphicsDevice();
+    auto  surfaceId = static_cast<Internal::Adaptor::WindowRenderSurface*>(surface)->GetSurfaceId();
+    auto  swapchain = gfxDevice.GetSwapchainForSurfaceId(surfaceId);
+    fbImpl          = swapchain->GetCurrentFramebuffer();
+  }
+  else if(framebuffer)
+  {
+    fbImpl = framebuffer->GetImpl();
+  }
+
+  auto renderPassCount = fbImpl->GetRenderPassCount();
+  for(auto i = 0u; i < renderPassCount; ++i)
+  {
+    RenderPassImpl* impl       = fbImpl->GetRenderPass(i);
+    gfxPipelineInfo.renderPass = impl->GetVkHandle();
+    gfxPipelineInfo.subpass    = 0;
+
+    if(gfxPipelineInfo.pColorBlendState)
+    {
+      auto attachmentCount = impl->GetAttachments().size();
+
+      if(attachmentCount != mBlendStateAttachments.size())
+      {
+        // Make sure array is 1
+        mBlendStateAttachments.resize(1);
+
+        // make it the right size
+        mBlendStateAttachments.resize(attachmentCount);
+
+        // Fill with defaults
+        std::fill(mBlendStateAttachments.begin() + 1, mBlendStateAttachments.end(), mBlendStateAttachments[0]);
+        const_cast<vk::PipelineColorBlendStateCreateInfo*>(gfxPipelineInfo.pColorBlendState)->attachmentCount = attachmentCount;
+        const_cast<vk::PipelineColorBlendStateCreateInfo*>(gfxPipelineInfo.pColorBlendState)->pAttachments    = mBlendStateAttachments.data();
+      }
+    }
+
+    vk::Pipeline vkPipeline;
+    VkAssert(vkDevice.createGraphicsPipelines(VK_NULL_HANDLE,
+                                              1,
+                                              &gfxPipelineInfo,
+                                              &allocator,
+                                              &vkPipeline));
+
+    RenderPassPipelinePair item;
+    item.renderPass = nullptr;
+    item.pipeline   = vkPipeline;
+    mVkPipelines.emplace_back(item);
+  }
 }
 
 void PipelineImpl::InitializeVertexInputState(vk::PipelineVertexInputStateCreateInfo& out)
index c2893780371554d493ba68b82cff189be343da0f..61d9731312b4961149f1ea20b2902b8224e0542b 100644 (file)
@@ -27,6 +27,7 @@
 #include <dali/internal/graphics/vulkan-impl/vulkan-graphics-resource.h>
 #include <dali/internal/graphics/vulkan-impl/vulkan-pipeline.h>
 #include <dali/internal/graphics/vulkan-impl/vulkan-reflection.h>
+#include <dali/internal/graphics/vulkan-impl/vulkan-render-pass.h>
 
 namespace Dali::Graphics::Vulkan
 {
@@ -91,9 +92,11 @@ public:
    */
   [[nodiscard]] VulkanGraphicsController& GetController() const;
 
-private:
+  vk::Pipeline GetVkPipeline() const;
+
   void InitializePipeline();
 
+private:
   void InitializeVertexInputState(vk::PipelineVertexInputStateCreateInfo& out);
   void InitializeInputAssemblyState(vk::PipelineInputAssemblyStateCreateInfo& out) const;
   void InitializeViewportState(vk::PipelineViewportStateCreateInfo& out);
@@ -148,7 +151,13 @@ private:
 
   std::vector<vk::DynamicState> mDynamicStates;
 
-  vk::Pipeline mVkPipeline;
+  struct RenderPassPipelinePair
+  {
+    Graphics::RenderPass* renderPass;
+    vk::Pipeline          pipeline;
+  };
+
+  std::vector<RenderPassPipelinePair> mVkPipelines;
 };
 
 } // namespace Dali::Graphics::Vulkan
index 2a370bcfec8ae05050aa5d8e716bbaf1c8dc5df9..2e92309d240c51e96cea64da7f7f2915f9af3c15 100644 (file)
@@ -60,9 +60,9 @@ public:
    *
    * @return Valid pipeline implementation
    */
-  [[nodiscard]] auto& GetPipeline() const
+  [[nodiscard]] Vulkan::PipelineImpl& GetImpl() const
   {
-    return mPipeline;
+    return *mPipeline;
   }
 
   /**