Added RenderPass and RenderTarget support
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-frame-buffer.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 // CLASS HEADER
18 #include <dali/internal/render/renderers/render-frame-buffer.h>
19
20 // INTERNAL INCLUDES
21 #include <dali/internal/render/renderers/render-texture.h>
22
23 namespace Dali
24 {
25 namespace Internal
26 {
27 namespace Render
28 {
29 FrameBuffer::FrameBuffer(uint32_t width, uint32_t height, Mask attachments)
30 : mWidth(width),
31   mHeight(height),
32   mDepthBuffer(attachments & Dali::FrameBuffer::Attachment::DEPTH),
33   mStencilBuffer(attachments & Dali::FrameBuffer::Attachment::STENCIL)
34 {
35 }
36
37 FrameBuffer::~FrameBuffer() = default;
38
39 void FrameBuffer::Destroy()
40 {
41   mGraphicsObject.reset();
42 }
43
44 void FrameBuffer::Initialize(Graphics::Controller& graphicsController)
45 {
46   mGraphicsController = &graphicsController;
47 }
48
49 void FrameBuffer::AttachColorTexture(Render::Texture* texture, uint32_t mipmapLevel, uint32_t layer)
50 {
51   if(texture)
52   {
53     if(!texture->GetGraphicsObject())
54     {
55       texture->Create(0 | Graphics::TextureUsageFlagBits::COLOR_ATTACHMENT | Graphics::TextureUsageFlagBits::SAMPLE);
56     }
57
58     uint32_t                  attachmentId = mCreateInfo.colorAttachments.size();
59     Graphics::ColorAttachment colorAttachment{attachmentId, texture->GetGraphicsObject(), layer, mipmapLevel};
60     mCreateInfo.colorAttachments.push_back(colorAttachment);
61   }
62 }
63
64 void FrameBuffer::AttachDepthTexture(Render::Texture* texture, uint32_t mipmapLevel)
65 {
66   if(texture && mDepthBuffer)
67   {
68     if(!texture->GetGraphicsObject())
69     {
70       texture->Create(0 | Graphics::TextureUsageFlagBits::DEPTH_STENCIL_ATTACHMENT | Graphics::TextureUsageFlagBits::SAMPLE);
71     }
72
73     mCreateInfo.depthStencilAttachment.depthTexture = texture->GetGraphicsObject();
74     mCreateInfo.depthStencilAttachment.depthLevel   = mipmapLevel;
75   }
76 }
77
78 void FrameBuffer::AttachDepthStencilTexture(Render::Texture* texture, uint32_t mipmapLevel)
79 {
80   if(texture && mStencilBuffer)
81   {
82     if(!texture->GetGraphicsObject())
83     {
84       texture->Create(0 | Graphics::TextureUsageFlagBits::DEPTH_STENCIL_ATTACHMENT | Graphics::TextureUsageFlagBits::SAMPLE);
85     }
86     mCreateInfo.depthStencilAttachment.stencilTexture = texture->GetGraphicsObject();
87     mCreateInfo.depthStencilAttachment.stencilLevel   = mipmapLevel;
88   }
89 }
90
91 void FrameBuffer::Bind()
92 {
93   if(!mGraphicsObject)
94   {
95     mGraphicsObject = mGraphicsController->CreateFramebuffer(mCreateInfo, std::move(mGraphicsObject));
96
97     // Create render target
98     Graphics::RenderTargetCreateInfo rtInfo{};
99     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) );
104
105     std::vector<Graphics::AttachmentDescription> attachmentDescriptions;
106
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 )
113     {
114       if(att.texture)
115       {
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();
121       }
122     }
123
124     if(mCreateInfo.depthStencilAttachment.depthTexture)
125     {
126       Graphics::AttachmentDescription depthStencilDesc{};
127       depthStencilDesc.SetStencilLoadOp( Graphics::AttachmentLoadOp::CLEAR )
128       .SetStoreOp( Graphics::AttachmentStoreOp::DONT_CARE );
129       if(mCreateInfo.depthStencilAttachment.stencilTexture)
130       {
131         depthStencilDesc.SetStencilLoadOp( Graphics::AttachmentLoadOp::CLEAR)
132         .SetStoreOp( Graphics::AttachmentStoreOp::DONT_CARE);
133       }
134       mClearValues.emplace_back();
135       attachmentDescriptions.push_back(depthStencilDesc);
136     }
137
138     Graphics::RenderPassCreateInfo rpInfo{};
139     rpInfo.SetAttachments( attachmentDescriptions );
140
141     // Add default render pass (loadOp = clear)
142     mRenderPass.emplace_back( mGraphicsController->CreateRenderPass( rpInfo, nullptr ) );
143
144     // Add default render pass (loadOp = dontcare)
145     attachmentDescriptions[0].SetLoadOp( Graphics::AttachmentLoadOp::DONT_CARE );
146     mRenderPass.emplace_back( mGraphicsController->CreateRenderPass( rpInfo, nullptr ) );
147   }
148 }
149
150 uint32_t FrameBuffer::GetWidth() const
151 {
152   return mWidth;
153 }
154
155 uint32_t FrameBuffer::GetHeight() const
156 {
157   return mHeight;
158 }
159
160 [[nodiscard]] Graphics::RenderPass* FrameBuffer::GetGraphicsRenderPass( Graphics::AttachmentLoadOp colorLoadOp,
161                                                                         Graphics::AttachmentStoreOp colorStoreOp ) const
162 {
163   // clear only when requested
164   if( colorLoadOp == Graphics::AttachmentLoadOp::CLEAR )
165   {
166     return mRenderPass[0].get();
167   }
168   else
169   {
170     return mRenderPass[1].get();
171   }
172 }
173
174
175 } // namespace Render
176
177 } // namespace Internal
178
179 } // namespace Dali