Add MultiSampling FrameBuffer implement.
[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) 2022 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, set multi sampling level,
66    * generates 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 Sets the level of multisampling in the frame buffer. This API is valid only GL_EXT_multisampled_render_to_texture supported.
109    * @param[in] multiSamplingLevel The level of multisampling
110    */
111   void SetMultiSamplingLevel(uint8_t multiSamplingLevel);
112
113   /**
114    * @brief Get the number of textures bound to this frame buffer as color attachments.
115    * @return The number of color attachments.
116    */
117   uint8_t GetColorAttachmentCount() const
118   {
119     return mCreateInfo.colorAttachments.size();
120   }
121
122   /**
123    * @brief Get the texture bound to this frame buffer as color attachment @a index.
124    * @return The texture.
125    */
126   Graphics::Texture* GetTexture(uint8_t index)
127   {
128     return mCreateInfo.colorAttachments[index].texture;
129   };
130
131   Graphics::Framebuffer* GetGraphicsObject()
132   {
133     return mGraphicsObject.get();
134   }
135
136   [[nodiscard]] Graphics::RenderTarget* GetGraphicsRenderTarget() const
137   {
138     return mRenderTarget.get();
139   }
140
141   [[nodiscard]] Graphics::RenderPass* GetGraphicsRenderPass(Graphics::AttachmentLoadOp  colorLoadOp,
142                                                             Graphics::AttachmentStoreOp colorStoreOp) const;
143
144   /**
145    * The function returns initialized array of clear values
146    * which then can be modified and assed to BeginRenderPass()
147    * command.
148    */
149   [[nodiscard]] auto& GetGraphicsRenderPassClearValues()
150   {
151     return mClearValues;
152   }
153
154 private:
155   /**
156    * @brief Undefined copy constructor. FrameBuffer cannot be copied
157    */
158   FrameBuffer(const FrameBuffer& rhs) = delete;
159
160   /**
161    * @brief Undefined assignment operator. FrameBuffer cannot be copied
162    */
163   FrameBuffer& operator=(const FrameBuffer& rhs) = delete;
164
165 private:
166   Graphics::Controller*                      mGraphicsController{nullptr};
167   Graphics::UniquePtr<Graphics::Framebuffer> mGraphicsObject{nullptr};
168
169   Graphics::FramebufferCreateInfo mCreateInfo;
170
171   // Render pass and render target
172
173   /**
174    * Render passes are created on fly depending on Load and Store operations
175    * The default render pass (most likely to be used) is the load = CLEAR
176    * amd store = STORE for color attachment.
177    */
178   std::vector<Graphics::UniquePtr<Graphics::RenderPass>> mRenderPass{};
179   Graphics::UniquePtr<Graphics::RenderTarget>            mRenderTarget{nullptr};
180
181   // clear colors
182   std::vector<Graphics::ClearValue> mClearValues{};
183
184   uint32_t mWidth;
185   uint32_t mHeight;
186
187   bool mDepthBuffer;
188   bool mStencilBuffer;
189 };
190
191 } // namespace Render
192
193 } // namespace Internal
194
195 } // namespace Dali
196
197 #endif // DALI_INTERNAL_RENDER_FRAME_BUFFER_H