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