Implement some more speed-up : Reserve TextureBindings + don't copy geometry
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-frame-buffer.h
1 #ifndef DALI_INTERNAL_RENDER_FRAME_BUFFER_H
2 #define DALI_INTERNAL_RENDER_FRAME_BUFFER_H
3
4 /*
5  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 // INTERNAL INCLUDES
21 #include <dali/devel-api/rendering/frame-buffer-devel.h>
22 #include <dali/internal/render/renderers/render-sampler.h>
23 #include <dali/public-api/rendering/frame-buffer.h>
24
25 namespace Dali
26 {
27 using Mask = Dali::FrameBuffer::Attachment::Mask;
28
29 namespace Internal
30 {
31 namespace Render
32 {
33 class Texture;
34
35 class FrameBuffer
36 {
37 public:
38   /**
39    * Constructor
40    */
41   FrameBuffer(uint32_t width, uint32_t height, Mask attachments);
42
43   /**
44    * Destructor
45    */
46   virtual ~FrameBuffer();
47
48   /**
49    * Creates a FrameBuffer object in the GPU.
50    * @param[in] graphicsController The Graphics Controller
51    */
52   virtual void Initialize(Graphics::Controller& graphicsController);
53
54   /**
55    * Deletes the framebuffer object from the GPU
56    */
57   virtual void Destroy();
58
59   /**
60    * @brief Create the graphics objects if needed.
61    *
62    * Doesn't re-create them if they are already generated, also doesn't
63    * check for new attachments.
64    *
65    * Creates the framebuffer, attaches color and depth textures, generates
66    * render target and render passes.
67    *
68    * @return true if there are attachments and the graphics objects have been created.
69    */
70   virtual bool CreateGraphicsObjects();
71
72   /**
73    * @brief Get the width of the FrameBuffer
74    * @return The width of the framebuffer
75    */
76   virtual uint32_t GetWidth() const;
77
78   /**
79    * @brief Get the height of the FrameBuffer
80    * @return The height of the framebuffer
81    */
82   virtual uint32_t GetHeight() const;
83
84   /**
85    * @brief Attaches a texture for the color rendering. This API is valid only for frame buffer with COLOR attachments.
86    * @param[in] texture The texture that will be used as output when rendering
87    * @param[in] mipmapLevel The mipmap of the texture to be attached
88    * @param[in] layer Indicates which layer of a cube map or array texture to attach. Unused for 2D textures
89    * @note A maximum of Dali::FrameBuffer::MAX_COLOR_ATTACHMENTS are supported.
90    */
91   void AttachColorTexture(Render::Texture* texture, uint32_t mipmapLevel, uint32_t layer);
92
93   /**
94    * @brief Attaches a texture for the depth rendering. This API is valid only for frame buffer with DEPTH attachments.
95    * @param[in] texture The texture that will be used as output when rendering
96    * @param[in] mipmapLevel The mipmap of the texture to be attached
97    */
98   void AttachDepthTexture(Render::Texture* texture, uint32_t mipmapLevel);
99
100   /**
101    * @brief Attaches a texture for the depth/stencil rendering. This API is valid only for frame buffer with DEPTH_STENCIL attachments.
102    * @param[in] texture The texture that will be used as output when rendering
103    * @param[in] mipmapLevel The mipmap of the texture to be attached
104    */
105   void AttachDepthStencilTexture(Render::Texture* texture, uint32_t mipmapLevel);
106
107   /**
108    * @brief Get the number of textures bound to this frame buffer as color attachments.
109    * @return The number of color attachments.
110    */
111   uint8_t GetColorAttachmentCount() const
112   {
113     return mCreateInfo.colorAttachments.size();
114   }
115
116   /**
117    * @brief Get the texture bound to this frame buffer as color attachment @a index.
118    * @return The texture.
119    */
120   Graphics::Texture* GetTexture(uint8_t index)
121   {
122     return mCreateInfo.colorAttachments[index].texture;
123   };
124
125   Graphics::Framebuffer* GetGraphicsObject()
126   {
127     return mGraphicsObject.get();
128   }
129
130   [[nodiscard]] Graphics::RenderTarget* GetGraphicsRenderTarget() const
131   {
132     return mRenderTarget.get();
133   }
134
135   [[nodiscard]] Graphics::RenderPass* GetGraphicsRenderPass(Graphics::AttachmentLoadOp  colorLoadOp,
136                                                             Graphics::AttachmentStoreOp colorStoreOp) const;
137
138   /**
139    * The function returns initialized array of clear values
140    * which then can be modified and assed to BeginRenderPass()
141    * command.
142    */
143   [[nodiscard]] auto& GetGraphicsRenderPassClearValues()
144   {
145     return mClearValues;
146   }
147
148 private:
149   /**
150    * @brief Undefined copy constructor. FrameBuffer cannot be copied
151    */
152   FrameBuffer(const FrameBuffer& rhs) = delete;
153
154   /**
155    * @brief Undefined assignment operator. FrameBuffer cannot be copied
156    */
157   FrameBuffer& operator=(const FrameBuffer& rhs) = delete;
158
159 private:
160   Graphics::Controller*                      mGraphicsController{nullptr};
161   Graphics::UniquePtr<Graphics::Framebuffer> mGraphicsObject{nullptr};
162
163   Graphics::FramebufferCreateInfo mCreateInfo;
164
165   // Render pass and render target
166
167   /**
168    * Render passes are created on fly depending on Load and Store operations
169    * The default render pass (most likely to be used) is the load = CLEAR
170    * amd store = STORE for color attachment.
171    */
172   std::vector<Graphics::UniquePtr<Graphics::RenderPass>> mRenderPass{};
173   Graphics::UniquePtr<Graphics::RenderTarget>            mRenderTarget{nullptr};
174
175   // clear colors
176   std::vector<Graphics::ClearValue> mClearValues{};
177
178   uint32_t mWidth;
179   uint32_t mHeight;
180
181   bool mDepthBuffer;
182   bool mStencilBuffer;
183 };
184
185 } // namespace Render
186
187 } // namespace Internal
188
189 } // namespace Dali
190
191 #endif // DALI_INTERNAL_RENDER_FRAME_BUFFER_H