2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <dali/internal/render/renderers/render-frame-buffer.h>
21 #include <dali/integration-api/debug.h>
22 #include <dali/internal/render/renderers/render-texture.h>
30 FrameBuffer::FrameBuffer(uint32_t width, uint32_t height, Mask attachments)
33 mDepthBuffer(attachments & Dali::FrameBuffer::Attachment::DEPTH),
34 mStencilBuffer(attachments & Dali::FrameBuffer::Attachment::STENCIL)
36 mCreateInfo.size.width = width;
37 mCreateInfo.size.height = height;
40 mCreateInfo.depthStencilAttachment.depthUsage = Graphics::DepthStencilAttachment::Usage::WRITE;
44 mCreateInfo.depthStencilAttachment.stencilUsage = Graphics::DepthStencilAttachment::Usage::WRITE;
48 FrameBuffer::~FrameBuffer() = default;
50 void FrameBuffer::Destroy()
52 mGraphicsObject.reset();
55 void FrameBuffer::Initialize(Graphics::Controller& graphicsController)
57 mGraphicsController = &graphicsController;
60 void FrameBuffer::AttachColorTexture(Render::Texture* texture, uint32_t mipmapLevel, uint32_t layer)
64 if(!texture->GetGraphicsObject())
66 texture->Create(0 | Graphics::TextureUsageFlagBits::COLOR_ATTACHMENT | Graphics::TextureUsageFlagBits::SAMPLE);
69 uint32_t attachmentId = mCreateInfo.colorAttachments.size();
70 Graphics::ColorAttachment colorAttachment{attachmentId, texture->GetGraphicsObject(), layer, mipmapLevel};
71 mCreateInfo.colorAttachments.push_back(colorAttachment);
75 void FrameBuffer::AttachDepthTexture(Render::Texture* texture, uint32_t mipmapLevel)
79 if(!texture->GetGraphicsObject())
81 texture->Create(0 | Graphics::TextureUsageFlagBits::DEPTH_STENCIL_ATTACHMENT | Graphics::TextureUsageFlagBits::SAMPLE);
84 mCreateInfo.depthStencilAttachment.depthTexture = texture->GetGraphicsObject();
85 mCreateInfo.depthStencilAttachment.depthUsage = Graphics::DepthStencilAttachment::Usage::WRITE;
86 mCreateInfo.depthStencilAttachment.depthLevel = mipmapLevel;
90 void FrameBuffer::AttachDepthStencilTexture(Render::Texture* texture, uint32_t mipmapLevel)
94 if(!texture->GetGraphicsObject())
96 texture->Create(0 | Graphics::TextureUsageFlagBits::DEPTH_STENCIL_ATTACHMENT | Graphics::TextureUsageFlagBits::SAMPLE);
98 mCreateInfo.depthStencilAttachment.stencilTexture = texture->GetGraphicsObject();
99 mCreateInfo.depthStencilAttachment.stencilUsage = Graphics::DepthStencilAttachment::Usage::WRITE;
100 mCreateInfo.depthStencilAttachment.stencilLevel = mipmapLevel;
104 bool FrameBuffer::CreateGraphicsObjects()
106 bool created = false;
110 // Only create a graphics object if there are attachments for it to render into
111 if(mCreateInfo.colorAttachments.empty() &&
112 mCreateInfo.depthStencilAttachment.depthTexture == nullptr &&
113 mCreateInfo.depthStencilAttachment.stencilTexture == nullptr &&
114 !mDepthBuffer && !mStencilBuffer)
116 DALI_LOG_ERROR("Attempting to bind a framebuffer with no attachments\n");
120 mGraphicsObject = mGraphicsController->CreateFramebuffer(mCreateInfo, std::move(mGraphicsObject));
122 // Create render target
123 Graphics::RenderTargetCreateInfo rtInfo{};
125 .SetFramebuffer(mGraphicsObject.get())
126 .SetExtent({mWidth, mHeight})
127 .SetPreTransform(0 | Graphics::RenderTargetTransformFlagBits::TRANSFORM_IDENTITY_BIT);
128 mRenderTarget = mGraphicsController->CreateRenderTarget(rtInfo, std::move(mRenderTarget));
130 std::vector<Graphics::AttachmentDescription> attachmentDescriptions;
132 // Default behaviour for color attachments is to CLEAR and STORE
133 mClearValues.clear();
134 for(auto& attachments : mCreateInfo.colorAttachments)
136 if(attachments.texture)
138 Graphics::AttachmentDescription desc{};
139 desc.SetLoadOp(Graphics::AttachmentLoadOp::CLEAR);
140 desc.SetStoreOp(Graphics::AttachmentStoreOp::STORE);
141 attachmentDescriptions.push_back(desc);
142 mClearValues.emplace_back();
146 if(mCreateInfo.depthStencilAttachment.depthTexture || mCreateInfo.depthStencilAttachment.stencilTexture ||
147 mDepthBuffer || mStencilBuffer)
149 Graphics::AttachmentDescription depthStencilDesc{};
150 depthStencilDesc.SetLoadOp(Graphics::AttachmentLoadOp::CLEAR)
151 .SetStoreOp(Graphics::AttachmentStoreOp::DONT_CARE);
153 if(mCreateInfo.depthStencilAttachment.stencilTexture || mStencilBuffer)
155 depthStencilDesc.SetStencilLoadOp(Graphics::AttachmentLoadOp::CLEAR)
156 .SetStencilStoreOp(Graphics::AttachmentStoreOp::DONT_CARE);
158 mClearValues.emplace_back();
159 attachmentDescriptions.push_back(depthStencilDesc);
162 Graphics::RenderPassCreateInfo rpInfo{};
163 rpInfo.SetAttachments(attachmentDescriptions);
165 // Add default render pass (loadOp = clear)
166 mRenderPass.emplace_back(mGraphicsController->CreateRenderPass(rpInfo, nullptr));
168 // Add default render pass (loadOp = dontcare)
169 attachmentDescriptions[0].SetLoadOp(Graphics::AttachmentLoadOp::DONT_CARE);
170 mRenderPass.emplace_back(mGraphicsController->CreateRenderPass(rpInfo, nullptr));
178 uint32_t FrameBuffer::GetWidth() const
183 uint32_t FrameBuffer::GetHeight() const
188 [[nodiscard]] Graphics::RenderPass* FrameBuffer::GetGraphicsRenderPass(Graphics::AttachmentLoadOp colorLoadOp,
189 Graphics::AttachmentStoreOp colorStoreOp) const
191 // clear only when requested
192 if(colorLoadOp == Graphics::AttachmentLoadOp::CLEAR)
194 return mRenderPass[0].get();
198 return mRenderPass[1].get();
202 } // namespace Render
204 } // namespace Internal