Added local pipeline cache on the dali-core side
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-frame-buffer.cpp
index b88a805..73101e1 100644 (file)
@@ -18,6 +18,7 @@
 #include <dali/internal/render/renderers/render-frame-buffer.h>
 
 // INTERNAL INCLUDES
+#include <dali/integration-api/debug.h>
 #include <dali/internal/render/renderers/render-texture.h>
 
 namespace Dali
@@ -32,6 +33,16 @@ FrameBuffer::FrameBuffer(uint32_t width, uint32_t height, Mask attachments)
   mDepthBuffer(attachments & Dali::FrameBuffer::Attachment::DEPTH),
   mStencilBuffer(attachments & Dali::FrameBuffer::Attachment::STENCIL)
 {
+  mCreateInfo.size.width  = width;
+  mCreateInfo.size.height = height;
+  if(mDepthBuffer)
+  {
+    mCreateInfo.depthStencilAttachment.depthUsage = Graphics::DepthStencilAttachment::Usage::WRITE;
+  }
+  if(mStencilBuffer)
+  {
+    mCreateInfo.depthStencilAttachment.stencilUsage = Graphics::DepthStencilAttachment::Usage::WRITE;
+  }
 }
 
 FrameBuffer::~FrameBuffer() = default;
@@ -63,7 +74,7 @@ void FrameBuffer::AttachColorTexture(Render::Texture* texture, uint32_t mipmapLe
 
 void FrameBuffer::AttachDepthTexture(Render::Texture* texture, uint32_t mipmapLevel)
 {
-  if(texture && mDepthBuffer)
+  if(texture)
   {
     if(!texture->GetGraphicsObject())
     {
@@ -71,29 +82,97 @@ void FrameBuffer::AttachDepthTexture(Render::Texture* texture, uint32_t mipmapLe
     }
 
     mCreateInfo.depthStencilAttachment.depthTexture = texture->GetGraphicsObject();
+    mCreateInfo.depthStencilAttachment.depthUsage   = Graphics::DepthStencilAttachment::Usage::WRITE;
     mCreateInfo.depthStencilAttachment.depthLevel   = mipmapLevel;
   }
 }
 
 void FrameBuffer::AttachDepthStencilTexture(Render::Texture* texture, uint32_t mipmapLevel)
 {
-  if(texture && mStencilBuffer)
+  if(texture)
   {
     if(!texture->GetGraphicsObject())
     {
       texture->Create(0 | Graphics::TextureUsageFlagBits::DEPTH_STENCIL_ATTACHMENT | Graphics::TextureUsageFlagBits::SAMPLE);
     }
     mCreateInfo.depthStencilAttachment.stencilTexture = texture->GetGraphicsObject();
+    mCreateInfo.depthStencilAttachment.stencilUsage   = Graphics::DepthStencilAttachment::Usage::WRITE;
     mCreateInfo.depthStencilAttachment.stencilLevel   = mipmapLevel;
   }
 }
 
-void FrameBuffer::Bind()
+bool FrameBuffer::CreateGraphicsObjects()
 {
+  bool created = false;
+
   if(!mGraphicsObject)
   {
-    mGraphicsObject = mGraphicsController->CreateFramebuffer(mCreateInfo, nullptr);
+    // Only create a graphics object if there are attachments for it to render into
+    if(mCreateInfo.colorAttachments.empty() &&
+       mCreateInfo.depthStencilAttachment.depthTexture == nullptr &&
+       mCreateInfo.depthStencilAttachment.stencilTexture == nullptr &&
+       !mDepthBuffer && !mStencilBuffer)
+    {
+      DALI_LOG_ERROR("Attempting to bind a framebuffer with no attachments\n");
+    }
+    else
+    {
+      mGraphicsObject = mGraphicsController->CreateFramebuffer(mCreateInfo, std::move(mGraphicsObject));
+
+      // Create render target
+      Graphics::RenderTargetCreateInfo rtInfo{};
+      rtInfo
+        .SetFramebuffer(mGraphicsObject.get())
+        .SetExtent({mWidth, mHeight})
+        .SetPreTransform(0 | Graphics::RenderTargetTransformFlagBits::TRANSFORM_IDENTITY_BIT);
+      mRenderTarget = mGraphicsController->CreateRenderTarget(rtInfo, std::move(mRenderTarget));
+
+      std::vector<Graphics::AttachmentDescription> attachmentDescriptions;
+
+      // Default behaviour for color attachments is to CLEAR and STORE
+      mClearValues.clear();
+      for(auto& attachments : mCreateInfo.colorAttachments)
+      {
+        if(attachments.texture)
+        {
+          Graphics::AttachmentDescription desc{};
+          desc.SetLoadOp(Graphics::AttachmentLoadOp::CLEAR);
+          desc.SetStoreOp(Graphics::AttachmentStoreOp::STORE);
+          attachmentDescriptions.push_back(desc);
+          mClearValues.emplace_back();
+        }
+      }
+
+      if(mCreateInfo.depthStencilAttachment.depthTexture || mCreateInfo.depthStencilAttachment.stencilTexture ||
+         mDepthBuffer || mStencilBuffer)
+      {
+        Graphics::AttachmentDescription depthStencilDesc{};
+        depthStencilDesc.SetLoadOp(Graphics::AttachmentLoadOp::CLEAR)
+          .SetStoreOp(Graphics::AttachmentStoreOp::DONT_CARE);
+
+        if(mCreateInfo.depthStencilAttachment.stencilTexture || mStencilBuffer)
+        {
+          depthStencilDesc.SetStencilLoadOp(Graphics::AttachmentLoadOp::CLEAR)
+            .SetStencilStoreOp(Graphics::AttachmentStoreOp::DONT_CARE);
+        }
+        mClearValues.emplace_back();
+        attachmentDescriptions.push_back(depthStencilDesc);
+      }
+
+      Graphics::RenderPassCreateInfo rpInfo{};
+      rpInfo.SetAttachments(attachmentDescriptions);
+
+      // Add default render pass (loadOp = clear)
+      mRenderPass.emplace_back(mGraphicsController->CreateRenderPass(rpInfo, nullptr));
+
+      // Add default render pass (loadOp = dontcare)
+      attachmentDescriptions[0].SetLoadOp(Graphics::AttachmentLoadOp::DONT_CARE);
+      mRenderPass.emplace_back(mGraphicsController->CreateRenderPass(rpInfo, nullptr));
+
+      created = true;
+    }
   }
+  return created;
 }
 
 uint32_t FrameBuffer::GetWidth() const
@@ -106,6 +185,20 @@ uint32_t FrameBuffer::GetHeight() const
   return mHeight;
 }
 
+[[nodiscard]] Graphics::RenderPass* FrameBuffer::GetGraphicsRenderPass(Graphics::AttachmentLoadOp  colorLoadOp,
+                                                                       Graphics::AttachmentStoreOp colorStoreOp) const
+{
+  // clear only when requested
+  if(colorLoadOp == Graphics::AttachmentLoadOp::CLEAR)
+  {
+    return mRenderPass[0].get();
+  }
+  else
+  {
+    return mRenderPass[1].get();
+  }
+}
+
 } // namespace Render
 
 } // namespace Internal