983bc9cdc6107e8c6df2e2283d386d08917eda89
[platform/core/uifw/dali-adaptor.git] / dali / internal / graphics / gles-impl / gles-graphics-command-buffer.h
1 #ifndef DALI_GRAPHICS_GLES_COMMAND_BUFFER_H
2 #define DALI_GRAPHICS_GLES_COMMAND_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
21 // EXTERNAL INCLUDES
22 #include <dali/graphics-api/graphics-command-buffer-create-info.h>
23 #include <dali/graphics-api/graphics-command-buffer.h>
24 #include <dali/graphics-api/graphics-types.h>
25
26 // INTERNAL INCLUDES
27 #include "gles-graphics-resource.h"
28 #include "gles-graphics-types.h"
29 #include "gles-sync-object.h"
30
31 #include <cstring>
32
33 namespace Dali::Graphics::GLES
34 {
35 class Pipeline;
36 class RenderPass;
37 class Framebuffer;
38 class CommandBuffer;
39 class CommandPool;
40 enum class CommandType
41 {
42   FLUSH,
43   BIND_TEXTURES,
44   BIND_SAMPLERS,
45   BIND_VERTEX_BUFFERS,
46   BIND_INDEX_BUFFER,
47   BIND_UNIFORM_BUFFER,
48   BIND_PIPELINE,
49   DRAW,
50   DRAW_INDEXED,
51   DRAW_INDEXED_INDIRECT,
52   SET_SCISSOR,
53   SET_SCISSOR_TEST,
54   SET_VIEWPORT,
55   BEGIN_RENDERPASS,
56   END_RENDERPASS,
57   EXECUTE_COMMAND_BUFFERS,
58   PRESENT_RENDER_TARGET,
59   SET_COLOR_MASK,
60   CLEAR_STENCIL_BUFFER,
61   CLEAR_DEPTH_BUFFER,
62   SET_STENCIL_TEST_ENABLE,
63   SET_STENCIL_WRITE_MASK,
64   SET_STENCIL_OP,
65   SET_STENCIL_FUNC,
66   SET_DEPTH_COMPARE_OP,
67   SET_DEPTH_TEST_ENABLE,
68   SET_DEPTH_WRITE_ENABLE,
69 };
70
71 /**
72  * @brief Helper function to invoke destructor on anonymous struct
73  */
74 template<class T>
75 static void InvokeDestructor(T& object)
76 {
77   object.~T();
78 }
79
80 /**
81  * Command structure allocates memory to store a single command
82  */
83 struct Command
84 {
85   Command()
86   {
87   }
88
89   explicit Command(CommandType commandType)
90   {
91     type = commandType;
92   }
93
94   ~Command() = default;
95
96   /**
97    * @brief Copy constructor
98    * @param[in] rhs Command
99    */
100   Command(const Command& rhs) = default;
101   Command& operator=(const Command& rhs) = default;
102   Command(Command&& rhs) noexcept        = delete;
103   Command& operator=(Command&& rhs) = delete;
104
105   CommandType type{CommandType::FLUSH}; ///< Type of command
106
107   union
108   {
109     struct
110     {
111       IndirectPtr<Graphics::TextureBinding> textureBindings;
112       uint32_t                              textureBindingsCount;
113     } bindTextures{};
114
115     // BindSampler command
116     struct
117     {
118       IndirectPtr<Graphics::SamplerBinding> samplerBindings;
119       uint32_t                              samplerBindingsCount;
120     } bindSamplers;
121
122     struct
123     {
124       using Binding = GLES::VertexBufferBindingDescriptor;
125       IndirectPtr<Binding> vertexBufferBindings;
126       uint32_t             vertexBufferBindingsCount;
127     } bindVertexBuffers;
128
129     struct : public IndexBufferBindingDescriptor
130     {
131     } bindIndexBuffer;
132
133     struct
134     {
135       IndirectPtr<UniformBufferBindingDescriptor> uniformBufferBindings;
136       uint32_t                                    uniformBufferBindingsCount;
137       UniformBufferBindingDescriptor              standaloneUniformsBufferBinding{};
138     } bindUniformBuffers;
139
140     struct
141     {
142       const GLES::Pipeline* pipeline{nullptr};
143     } bindPipeline;
144
145     struct : public DrawCallDescriptor
146     {
147     } draw;
148
149     struct
150     {
151       Graphics::Rect2D region;
152     } scissor;
153
154     struct
155     {
156       bool enable;
157     } scissorTest;
158
159     struct
160     {
161       Graphics::Viewport region;
162     } viewport;
163
164     struct BeginRenderPassDescriptor
165       beginRenderPass;
166
167     struct
168     {
169       Graphics::SyncObject* syncObject;
170     } endRenderPass;
171
172     struct
173     {
174       IndirectPtr<const GLES::CommandBuffer*> buffers;
175       uint32_t                                buffersCount;
176     } executeCommandBuffers;
177
178     struct
179     {
180       GLES::RenderTarget* targetToPresent;
181     } presentRenderTarget;
182
183     struct
184     {
185       Graphics::CompareOp compareOp;
186       bool                testEnabled;
187       bool                writeEnabled;
188     } depth;
189
190     struct
191     {
192       Graphics::StencilOp failOp;
193       Graphics::StencilOp passOp;
194       Graphics::StencilOp depthFailOp;
195     } stencilOp;
196
197     struct
198     {
199       uint32_t mask;
200     } stencilWriteMask;
201
202     struct
203     {
204       uint32_t            compareMask;
205       Graphics::CompareOp compareOp;
206       uint32_t            reference;
207     } stencilFunc;
208
209     struct
210     {
211       bool enabled;
212     } stencilTest;
213
214     struct
215     {
216       bool enabled;
217     } colorMask;
218   };
219 };
220
221 using CommandBufferResource = Resource<Graphics::CommandBuffer, Graphics::CommandBufferCreateInfo>;
222
223 class CommandBuffer : public CommandBufferResource
224 {
225 public:
226   CommandBuffer(const Graphics::CommandBufferCreateInfo& createInfo, EglGraphicsController& controller);
227
228   ~CommandBuffer() override;
229
230   /**
231    * @copydoc Dali::Graphics::CommandBuffer::BindVertexBuffers
232    */
233   void BindVertexBuffers(uint32_t                                    firstBinding,
234                          const std::vector<const Graphics::Buffer*>& buffers,
235                          const std::vector<uint32_t>&                offsets) override;
236
237   /**
238    * @copydoc Dali::Graphics::CommandBuffer::BindUniformBuffers
239    */
240   void BindUniformBuffers(const std::vector<Graphics::UniformBufferBinding>& bindings) override;
241
242   /**
243    * @copydoc Dali::Graphics::CommandBuffer::BindPipeline
244    */
245   void BindPipeline(const Graphics::Pipeline& pipeline) override;
246
247   /**
248    * @copydoc Dali::Graphics::CommandBuffer::BindTextures
249    */
250   void BindTextures(const std::vector<TextureBinding>& textureBindings) override;
251
252   /**
253    * @copydoc Dali::Graphics::CommandBuffer::BindSamplers
254    */
255   void BindSamplers(const std::vector<SamplerBinding>& samplerBindings) override;
256
257   /**
258    * @copydoc Dali::Graphics::CommandBuffer::BindPushConstants
259    */
260   void BindPushConstants(void*    data,
261                          uint32_t size,
262                          uint32_t binding) override;
263
264   /**
265    * @copydoc Dali::Graphics::CommandBuffer::BindIndexBuffer
266    */
267   void BindIndexBuffer(const Graphics::Buffer& buffer,
268                        uint32_t                offset,
269                        Format                  format) override;
270
271   /**
272    * @copydoc Dali::Graphics::CommandBuffer::BeginRenderPass
273    */
274   void BeginRenderPass(
275     Graphics::RenderPass*          renderPass,
276     Graphics::RenderTarget*        renderTarget,
277     Rect2D                         renderArea,
278     const std::vector<ClearValue>& clearValues) override;
279
280   /**
281    * @copydoc Dali::Graphics::CommandBuffer::EndRenderPass
282    */
283   void EndRenderPass(Graphics::SyncObject* syncObject) override;
284
285   /**
286    * @copydoc Dali::Graphics::CommandBuffer::ExecuteCommandBuffers
287    */
288   void ExecuteCommandBuffers(std::vector<const Graphics::CommandBuffer*>&& commandBuffers) override;
289
290   /**
291    * @copydoc Dali::Graphics::CommandBuffer::Draw
292    */
293   void Draw(
294     uint32_t vertexCount,
295     uint32_t instanceCount,
296     uint32_t firstVertex,
297     uint32_t firstInstance) override;
298
299   /**
300    * @copydoc Dali::Graphics::CommandBuffer::DrawIndexed
301    */
302   void DrawIndexed(
303     uint32_t indexCount,
304     uint32_t instanceCount,
305     uint32_t firstIndex,
306     int32_t  vertexOffset,
307     uint32_t firstInstance) override;
308
309   /**
310    * @copydoc Dali::Graphics::CommandBuffer::DrawIndexedIndirect
311    */
312   void DrawIndexedIndirect(
313     Graphics::Buffer& buffer,
314     uint32_t          offset,
315     uint32_t          drawCount,
316     uint32_t          stride) override;
317
318   /**
319    * @copydoc Dali::Graphics::CommandBuffer::Reset
320    */
321   void Reset() override;
322
323   /**
324    * @copydoc Dali::Graphics::CommandBuffer::SetScissor
325    */
326   void SetScissor(Graphics::Rect2D value) override;
327
328   /**
329    * @copydoc Dali::Graphics::CommandBuffer::SetScissorTestEnable
330    */
331   void SetScissorTestEnable(bool value) override;
332
333   /**
334    * @copydoc Dali::Graphics::CommandBuffer::SetViewport
335    */
336   void SetViewport(Viewport value) override;
337
338   /**
339    * @copydoc Dali::Graphics::CommandBuffer::SetViewportEnable
340    */
341   void SetViewportEnable(bool value) override;
342
343   /**
344    * @copydoc Dali::Graphics::CommandBuffer::SetColorMask
345    */
346   void SetColorMask(bool enabled) override;
347
348   /**
349    * @copydoc Dali::Graphics::CommandBuffer::ClearStencilBuffer
350    */
351   void ClearStencilBuffer() override;
352
353   /**
354    * @copydoc Dali::Graphics::CommandBuffer::SetStencilTestEnable
355    */
356   void SetStencilTestEnable(bool stencilEnable) override;
357
358   /**
359    * @copydoc Dali::Graphics::CommandBuffer::SetStencilWriteMask
360    */
361   void SetStencilWriteMask(uint32_t writeMask) override;
362
363   /**
364    * @copydoc Dali::Graphics::CommandBuffer::SetStencilOp
365    */
366   void SetStencilOp(Graphics::StencilOp failOp,
367                     Graphics::StencilOp passOp,
368                     Graphics::StencilOp depthFailOp) override;
369
370   /**
371    * @copydoc Dali::Graphics::CommandBuffer::SetStencilFunc
372    */
373   void SetStencilFunc(Graphics::CompareOp compareOp,
374                       uint32_t            reference,
375                       uint32_t            compareMask) override;
376
377   /**
378    * @copydoc Dali::Graphics::CommandBuffer::SetDepthCompareOp
379    */
380   void SetDepthCompareOp(Graphics::CompareOp compareOp) override;
381
382   /**
383    * @copydoc Dali::Graphics::CommandBuffer::SetDepthTestEnable
384    */
385   void SetDepthTestEnable(bool depthTestEnable) override;
386
387   /**
388    * @copydoc Dali::Graphics::CommandBuffer::SetDepthWriteEnable
389    */
390   void SetDepthWriteEnable(bool depthWriteEnable) override;
391
392   /**
393    * @copydoc Dali::Graphics::CommandBuffer::ClearDepthBuffer
394    */
395   void ClearDepthBuffer() override;
396
397   /**
398    * @brief Presents specified render target
399    *
400    * @param[in] renderTarget Valid pointer to a RenderTarget
401    *
402    * It's internal command that schedules presentation of
403    * specified render target.
404    */
405   void PresentRenderTarget(GLES::RenderTarget* renderTarget);
406
407   /**
408    * @brief Returns pointer to the list of command and size of the list
409    *
410    * @param[out] size Size of the list
411    * @return Valid pointer to the list of commands
412    */
413   [[nodiscard]] const Command* GetCommands(uint32_t& size) const;
414
415   /**
416    * @brief Destroy the associated resources
417    */
418   void DestroyResource() override;
419
420   /**
421    * @brief Initialize associated resources
422    */
423   bool InitializeResource() override;
424
425   /**
426    * @brief Add this resource to the discard queue
427    */
428   void DiscardResource() override;
429
430 private:
431   std::unique_ptr<CommandPool> mCommandPool; ///< Pool of commands and transient memory
432 };
433 } // namespace Dali::Graphics::GLES
434
435 #endif