Added local pipeline cache on the dali-core side
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-frame-buffer.cpp
index 733cbf4..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
@@ -26,133 +27,152 @@ namespace Internal
 {
 namespace Render
 {
-namespace
-{
-const GLenum COLOR_ATTACHMENTS[] =
-  {
-    GL_COLOR_ATTACHMENT0,
-    GL_COLOR_ATTACHMENT1,
-    GL_COLOR_ATTACHMENT2,
-    GL_COLOR_ATTACHMENT3,
-    GL_COLOR_ATTACHMENT4,
-    GL_COLOR_ATTACHMENT5,
-    GL_COLOR_ATTACHMENT6,
-    GL_COLOR_ATTACHMENT7,
-};
-}
-
 FrameBuffer::FrameBuffer(uint32_t width, uint32_t height, Mask attachments)
-: mId(0u),
-  mTextures{0u},
-  mDepthBuffer(attachments & Dali::FrameBuffer::Attachment::DEPTH),
-  mStencilBuffer(attachments & Dali::FrameBuffer::Attachment::STENCIL),
-  mWidth(width),
+: mWidth(width),
   mHeight(height),
-  mColorAttachmentCount(0u)
+  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;
 
-void FrameBuffer::Destroy(Context& context)
+void FrameBuffer::Destroy()
 {
-  if(mId)
-  {
-    context.DeleteFramebuffers(1, &mId);
-  }
+  mGraphicsObject.reset();
 }
 
-void FrameBuffer::GlContextDestroyed()
+void FrameBuffer::Initialize(Graphics::Controller& graphicsController)
 {
-  mId = 0u;
+  mGraphicsController = &graphicsController;
 }
 
-void FrameBuffer::Initialize(Context& context)
+void FrameBuffer::AttachColorTexture(Render::Texture* texture, uint32_t mipmapLevel, uint32_t layer)
 {
-  context.GenFramebuffers(1, &mId);
-  context.BindFramebuffer(GL_FRAMEBUFFER, mId);
-
-  if(mDepthBuffer)
+  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)
-  {
-    // 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);
-  }
-
-  context.BindFramebuffer(GL_FRAMEBUFFER, 0);
 }
 
-void FrameBuffer::AttachColorTexture(Context& context, Render::Texture* texture, uint32_t mipmapLevel, uint32_t layer)
+void FrameBuffer::AttachDepthTexture(Render::Texture* texture, uint32_t mipmapLevel)
 {
-  context.BindFramebuffer(GL_FRAMEBUFFER, mId);
-
-  mTextures[mColorAttachmentCount] = texture->GetGraphicsObject();
-  GLuint textureId                 = 0; //@todo Temp
-
-  // Create a color attachment.
-  const GLenum iAttachment = COLOR_ATTACHMENTS[mColorAttachmentCount];
-  if(texture->GetType() == TextureType::TEXTURE_2D)
-  {
-    context.FramebufferTexture2D(GL_FRAMEBUFFER, iAttachment, GL_TEXTURE_2D, textureId, mipmapLevel);
-  }
-  else
+  if(texture)
   {
-    context.FramebufferTexture2D(GL_FRAMEBUFFER, iAttachment, GL_TEXTURE_CUBE_MAP_POSITIVE_X + layer, textureId, mipmapLevel);
+    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;
   }
-
-  ++mColorAttachmentCount;
-  context.DrawBuffers(mColorAttachmentCount, COLOR_ATTACHMENTS);
-  DALI_ASSERT_DEBUG(context.CheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
-
-  context.BindFramebuffer(GL_FRAMEBUFFER, 0);
 }
 
-void FrameBuffer::AttachDepthTexture(Context& context, Render::Texture* texture, uint32_t mipmapLevel)
+void FrameBuffer::AttachDepthStencilTexture(Render::Texture* texture, uint32_t mipmapLevel)
 {
-  context.BindFramebuffer(GL_FRAMEBUFFER, mId);
-
-  //@todo Temp
-  GLuint textureId = 0;
-
-  // Create a depth attachment.
-  if(texture->GetType() == TextureType::TEXTURE_2D)
+  if(texture)
   {
-    context.FramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, textureId, 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;
   }
-
-  context.BindFramebuffer(GL_FRAMEBUFFER, 0);
 }
 
-void FrameBuffer::AttachDepthStencilTexture(Context& context, Render::Texture* texture, uint32_t mipmapLevel)
+bool FrameBuffer::CreateGraphicsObjects()
 {
-  context.BindFramebuffer(GL_FRAMEBUFFER, mId);
+  bool created = false;
 
-  //@todo temp
-  GLuint textureId = 0;
-
-  // Create a depth/stencil attachment.
-  if(texture->GetType() == TextureType::TEXTURE_2D)
+  if(!mGraphicsObject)
   {
-    context.FramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D, textureId, mipmapLevel);
+    // 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;
+    }
   }
-
-  context.BindFramebuffer(GL_FRAMEBUFFER, 0);
-}
-
-void FrameBuffer::Bind(Context& context)
-{
-  context.BindFramebuffer(GL_FRAMEBUFFER, mId);
+  return created;
 }
 
 uint32_t FrameBuffer::GetWidth() const
@@ -165,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