Added local pipeline cache on the dali-core side
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-frame-buffer.cpp
index 35fe545..73101e1 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -17,7 +17,8 @@
 // CLASS HEADER
 #include <dali/internal/render/renderers/render-frame-buffer.h>
 
-//INTERNAL INCLUDES
+// INTERNAL INCLUDES
+#include <dali/integration-api/debug.h>
 #include <dali/internal/render/renderers/render-texture.h>
 
 namespace Dali
@@ -26,87 +27,180 @@ namespace Internal
 {
 namespace Render
 {
-
-FrameBuffer::FrameBuffer( unsigned int width, unsigned int height, Format format )
-:mId( 0u ),
- mDepthBuffer( (format == Dali::FrameBuffer::COLOR_DEPTH || format == Dali::FrameBuffer::COLOR_DEPTH_STENCIL ) ? 1u : 0u ),
- mStencilBuffer( (format == Dali::FrameBuffer::COLOR_STENCIL || format == Dali::FrameBuffer::COLOR_DEPTH_STENCIL ) ? 1u : 0u ),
- mWidth( width ),
- mHeight( height )
+FrameBuffer::FrameBuffer(uint32_t width, uint32_t height, Mask attachments)
+: mWidth(width),
+  mHeight(height),
+  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()
-{}
+FrameBuffer::~FrameBuffer() = default;
 
-void FrameBuffer::Destroy( Context& context )
+void FrameBuffer::Destroy()
 {
-  if( mId )
-  {
-    context.DeleteFramebuffers( 1, &mId );
-  }
+  mGraphicsObject.reset();
 }
 
-void FrameBuffer::Initialize(Context& context)
+void FrameBuffer::Initialize(Graphics::Controller& graphicsController)
 {
-  context.GenFramebuffers( 1, &mId );
-  context.BindFramebuffer( GL_FRAMEBUFFER, mId );
+  mGraphicsController = &graphicsController;
+}
 
-  if( mDepthBuffer )
+void FrameBuffer::AttachColorTexture(Render::Texture* texture, uint32_t mipmapLevel, uint32_t layer)
+{
+  if(texture)
   {
-    //Create a depth render target
-    context.GenRenderbuffers(1, &mDepthBuffer );
-    context.BindRenderbuffer(GL_RENDERBUFFER, mDepthBuffer);
-    context.RenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, mWidth, mHeight);
-    context.FramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, mDepthBuffer);
+    if(!texture->GetGraphicsObject())
+    {
+      texture->Create(0 | Graphics::TextureUsageFlagBits::COLOR_ATTACHMENT | Graphics::TextureUsageFlagBits::SAMPLE);
+    }
+
+    uint32_t                  attachmentId = mCreateInfo.colorAttachments.size();
+    Graphics::ColorAttachment colorAttachment{attachmentId, texture->GetGraphicsObject(), layer, mipmapLevel};
+    mCreateInfo.colorAttachments.push_back(colorAttachment);
   }
+}
 
-  if( mStencilBuffer )
+void FrameBuffer::AttachDepthTexture(Render::Texture* texture, uint32_t mipmapLevel)
+{
+  if(texture)
   {
-    //Create a stencil render target
-    context.GenRenderbuffers(1, &mStencilBuffer );
-    context.BindRenderbuffer(GL_RENDERBUFFER, mStencilBuffer);
-    context.RenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, mWidth, mHeight);
-    context.FramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, mStencilBuffer );
+    if(!texture->GetGraphicsObject())
+    {
+      texture->Create(0 | Graphics::TextureUsageFlagBits::DEPTH_STENCIL_ATTACHMENT | Graphics::TextureUsageFlagBits::SAMPLE);
+    }
+
+    mCreateInfo.depthStencilAttachment.depthTexture = texture->GetGraphicsObject();
+    mCreateInfo.depthStencilAttachment.depthUsage   = Graphics::DepthStencilAttachment::Usage::WRITE;
+    mCreateInfo.depthStencilAttachment.depthLevel   = mipmapLevel;
   }
-
-  context.BindFramebuffer( GL_FRAMEBUFFER, 0 );
 }
 
-void FrameBuffer::AttachColorTexture( Context& context, Render::NewTexture* texture, unsigned int mipmapLevel, unsigned int layer )
+void FrameBuffer::AttachDepthStencilTexture(Render::Texture* texture, uint32_t mipmapLevel)
 {
-  context.BindFramebuffer( GL_FRAMEBUFFER, mId );
-
-  if( texture->GetType() == TextureType::TEXTURE_2D )
+  if(texture)
   {
-    context.FramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture->GetId(), mipmapLevel );
+    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;
   }
-  else
-  {
-    context.FramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + layer, texture->GetId(), mipmapLevel );
-  }
-
-  context.BindFramebuffer( GL_FRAMEBUFFER, 0 );
 }
 
-void FrameBuffer::Bind( Context& context )
+bool FrameBuffer::CreateGraphicsObjects()
 {
-  context.BindFramebuffer( GL_FRAMEBUFFER, mId );
+  bool created = false;
+
+  if(!mGraphicsObject)
+  {
+    // 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;
 }
 
-unsigned int FrameBuffer::GetWidth() const
+uint32_t FrameBuffer::GetWidth() const
 {
   return mWidth;
 }
 
-unsigned int FrameBuffer::GetHeight() const
+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();
+  }
+}
 
-} //Render
+} // namespace Render
 
-} //Internal
+} // namespace Internal
 
-} //Dali
+} // namespace Dali