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/internal/render/renderers/render-texture.h>
29 FrameBuffer::FrameBuffer(uint32_t width, uint32_t height, Mask attachments)
32 mDepthBuffer(attachments & Dali::FrameBuffer::Attachment::DEPTH),
33 mStencilBuffer(attachments & Dali::FrameBuffer::Attachment::STENCIL)
37 FrameBuffer::~FrameBuffer() = default;
39 void FrameBuffer::Destroy()
41 mGraphicsObject.reset();
44 void FrameBuffer::Initialize(Graphics::Controller& graphicsController)
46 mGraphicsController = &graphicsController;
49 void FrameBuffer::AttachColorTexture(Render::Texture* texture, uint32_t mipmapLevel, uint32_t layer)
53 if(!texture->GetGraphicsObject())
55 texture->Create(0 | Graphics::TextureUsageFlagBits::COLOR_ATTACHMENT | Graphics::TextureUsageFlagBits::SAMPLE);
58 uint32_t attachmentId = mCreateInfo.colorAttachments.size();
59 Graphics::ColorAttachment colorAttachment{attachmentId, texture->GetGraphicsObject(), layer, mipmapLevel};
60 mCreateInfo.colorAttachments.push_back(colorAttachment);
64 void FrameBuffer::AttachDepthTexture(Render::Texture* texture, uint32_t mipmapLevel)
66 if(texture && mDepthBuffer)
68 if(!texture->GetGraphicsObject())
70 texture->Create(0 | Graphics::TextureUsageFlagBits::DEPTH_STENCIL_ATTACHMENT | Graphics::TextureUsageFlagBits::SAMPLE);
73 mCreateInfo.depthStencilAttachment.depthTexture = texture->GetGraphicsObject();
74 mCreateInfo.depthStencilAttachment.depthLevel = mipmapLevel;
78 void FrameBuffer::AttachDepthStencilTexture(Render::Texture* texture, uint32_t mipmapLevel)
80 if(texture && mStencilBuffer)
82 if(!texture->GetGraphicsObject())
84 texture->Create(0 | Graphics::TextureUsageFlagBits::DEPTH_STENCIL_ATTACHMENT | Graphics::TextureUsageFlagBits::SAMPLE);
86 mCreateInfo.depthStencilAttachment.stencilTexture = texture->GetGraphicsObject();
87 mCreateInfo.depthStencilAttachment.stencilLevel = mipmapLevel;
91 void FrameBuffer::Bind()
95 mGraphicsObject = mGraphicsController->CreateFramebuffer(mCreateInfo, std::move(mGraphicsObject));
97 // Create render target
98 Graphics::RenderTargetCreateInfo rtInfo{};
100 .SetFramebuffer( mGraphicsObject.get() )
101 .SetExtent( {mWidth, mHeight} )
102 .SetPreTransform( 0 | Graphics::RenderTargetTransformFlagBits::TRANSFORM_IDENTITY_BIT );
103 mRenderTarget = mGraphicsController->CreateRenderTarget( rtInfo, std::move(mRenderTarget) );
105 std::vector<Graphics::AttachmentDescription> attachmentDescriptions;
107 // Default behaviour for color attachments is to CLEAR and STORE
108 //@todo Ideally, we should create new render pass whenever
109 // the loadop, storeop changes and the list of such renderpasses
110 // should be managed accordingly (as in Vulkan)
111 mClearValues.clear();
112 for(auto& att: mCreateInfo.colorAttachments )
116 Graphics::AttachmentDescription desc{};
117 desc.SetLoadOp(Graphics::AttachmentLoadOp::CLEAR);
118 desc.SetStoreOp(Graphics::AttachmentStoreOp::STORE);
119 attachmentDescriptions.push_back(desc);
120 mClearValues.emplace_back();
124 if(mCreateInfo.depthStencilAttachment.depthTexture)
126 Graphics::AttachmentDescription depthStencilDesc{};
127 depthStencilDesc.SetStencilLoadOp( Graphics::AttachmentLoadOp::CLEAR )
128 .SetStoreOp( Graphics::AttachmentStoreOp::DONT_CARE );
129 if(mCreateInfo.depthStencilAttachment.stencilTexture)
131 depthStencilDesc.SetStencilLoadOp( Graphics::AttachmentLoadOp::CLEAR)
132 .SetStoreOp( Graphics::AttachmentStoreOp::DONT_CARE);
134 mClearValues.emplace_back();
135 attachmentDescriptions.push_back(depthStencilDesc);
138 Graphics::RenderPassCreateInfo rpInfo{};
139 rpInfo.SetAttachments( attachmentDescriptions );
141 // Add default render pass (loadOp = clear)
142 mRenderPass.emplace_back( mGraphicsController->CreateRenderPass( rpInfo, nullptr ) );
144 // Add default render pass (loadOp = dontcare)
145 attachmentDescriptions[0].SetLoadOp( Graphics::AttachmentLoadOp::DONT_CARE );
146 mRenderPass.emplace_back( mGraphicsController->CreateRenderPass( rpInfo, nullptr ) );
150 uint32_t FrameBuffer::GetWidth() const
155 uint32_t FrameBuffer::GetHeight() const
160 [[nodiscard]] Graphics::RenderPass* FrameBuffer::GetGraphicsRenderPass( Graphics::AttachmentLoadOp colorLoadOp,
161 Graphics::AttachmentStoreOp colorStoreOp ) const
163 // clear only when requested
164 if( colorLoadOp == Graphics::AttachmentLoadOp::CLEAR )
166 return mRenderPass[0].get();
170 return mRenderPass[1].get();
175 } // namespace Render
177 } // namespace Internal