DirectRendering:
[platform/core/uifw/dali-core.git] / dali / graphics-api / graphics-command-buffer.h
1 #ifndef DALI_GRAPHICS_COMMAND_BUFFER_H
2 #define DALI_GRAPHICS_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 // INTERNAL INCLUDES
22 #include "graphics-types.h"
23
24 namespace Dali::Graphics
25 {
26 class Buffer;
27 class Pipeline;
28 class RenderTarget;
29 class RenderPass;
30 class Sampler;
31 class SyncObject;
32 class Texture;
33
34 /**
35  * @brief Uniform buffer bindings.
36  */
37 struct UniformBufferBinding
38 {
39   Buffer* buffer; // Buffer
40   union
41   {
42     void*    offsetPtr; // pointer to the client-side memory
43     uint32_t offset;    // Offset within buffer
44   };
45   uint32_t dataSize; // Size of data to bind
46   uint32_t binding;  // Binding index
47 };
48
49 /**
50  * @brief Texture bindings
51  *
52  * Additionally, sampler may be used in case of having combined
53  * image and sampler.
54  *
55  */
56 struct TextureBinding
57 {
58   const Texture* texture; // texture to be bound
59   const Sampler* sampler; // sampler to be bound
60   uint32_t       binding; // binding index
61 };
62
63 /**
64  * @brief Sampler binding
65  */
66 struct SamplerBinding
67 {
68   Sampler* sampler; // sampler to be bound
69   uint32_t binding; // binding index
70 };
71
72 /**
73  * @brief ClearValue contains an union of RGBA and depthStencil values.
74  */
75 struct ClearValue
76 {
77   union
78   {
79     struct
80     {
81       float r, g, b, a;
82     } color;
83     struct
84     {
85       float    depth;   // glClearDepthf
86       uint32_t stencil; // glClearStencil
87     } depthStencil;
88   };
89 };
90
91 // Native rendering (using native APIs)
92
93 enum class DrawNativeAPI
94 {
95   GLES,
96   UNDEFINED
97 };
98
99 struct DrawNativeInfo
100 {
101   DrawNativeAPI       api;      //< API used by the callback
102   Dali::CallbackBase* callback; //< Callback pointer
103   void*               userData; //< Data passed into the callback (unspecified type, callback should decode it)
104   void*               reserved; //< Reserved for internal use
105 };
106
107 /**
108  * @brief CommandBuffer contains a stream of commands to be executed
109  * by the controller.
110  */
111 class CommandBuffer
112 {
113 public:
114   CommandBuffer()          = default;
115   virtual ~CommandBuffer() = default;
116
117   // not copyable
118   CommandBuffer(const CommandBuffer&) = delete;
119   CommandBuffer& operator=(const CommandBuffer&) = delete;
120
121   /**
122    * @brief Binds vertex buffers
123    *
124    * The buffers and offsets arrays must be same length
125    *
126    * @param[in] firstBinding First binding index
127    * @param[in] buffers List of buffers to bind
128    * @param[in] offsets List of offsets for each buffer
129    */
130   virtual void BindVertexBuffers(uint32_t                          firstBinding,
131                                  const std::vector<const Buffer*>& buffers,
132                                  const std::vector<uint32_t>&      offsets) = 0;
133
134   /**
135    * @brief Binds uniform buffers
136    *
137    * @param[in] bindings List of uniform buffer bindings
138    */
139   virtual void BindUniformBuffers(const std::vector<UniformBufferBinding>& bindings) = 0;
140
141   /**
142    * @brief Binds pipeline
143    *
144    * @param[in] pipeline valid pipeline
145    */
146   virtual void BindPipeline(const Pipeline& pipeline) = 0;
147
148   /**
149    * @brief Binds textures
150    *
151    * @param[in] textureBindings List of texture bindings
152    */
153   virtual void BindTextures(const std::vector<TextureBinding>& textureBindings) = 0;
154
155   /**
156    * @brief Binds samplers
157    *
158    * @param[in] samplerBindings List of sampler bindings
159    */
160   virtual void BindSamplers(const std::vector<SamplerBinding>& samplerBindings) = 0;
161
162   /**
163    * @brief Binds buffer containing push constants
164    *
165    * @param[in] data pointer to the buffer
166    * @param[in] size size of data in bytes
167    * @param[in] binding push constants binding index
168    */
169   virtual void BindPushConstants(void*    data,
170                                  uint32_t size,
171                                  uint32_t binding) = 0;
172
173   /**
174    * @brief Binds index buffer
175    *
176    * Most commonly used formats:
177    * R32_UINT,
178    * R16_UINT
179    *
180    * @param[in] buffer Valid buffer
181    * @param[in] offset offset within buffer
182    * @param[in] format Format of index buffer
183    */
184   virtual void BindIndexBuffer(const Buffer& buffer,
185                                uint32_t      offset,
186                                Format        format) = 0;
187   /**
188    * @brief Begins render pass
189    *
190    * The function initialises rendering for specified RenderPass object
191    * onto renderTarget. renderArea defines the scissor rect. Depends on the
192    * renderPass spec, the clearValues may be used.
193    *
194    * Calling EndRenderPass() is necessary to finalize the render pass.
195    *
196    * @param[in] renderPass valid render pass object
197    * @param[in] renderTarget valid render target, must not be used when framebuffer set
198    * @param[in] renderArea area to draw (clear operation is affected)
199    * @param[in] clearValues clear values (compatible with renderpass spec)
200    */
201   virtual void BeginRenderPass(
202     RenderPass*                    renderPass,
203     RenderTarget*                  renderTarget,
204     Rect2D                         renderArea,
205     const std::vector<ClearValue>& clearValues) = 0;
206
207   /**
208    * @brief Ends current render pass
209    *
210    * This command must be issued in order to finalize the render pass.
211    * It's up to the implementation whether anything has to be done but
212    * the Controller may use end RP marker in order to resolve resource
213    * dependencies (for example, to know when target texture is ready
214    * before passing it to another render pass).
215    *
216    * The caller may query the sync object to determine when this render
217    * pass has actually finished on the GPU.
218    *
219    * @param[in] syncObject If non-null, this object will ensure an
220    * appropriate fence sync object is created after the render pass is
221    * executed.
222    */
223   virtual void EndRenderPass(Graphics::SyncObject* syncObject) = 0;
224
225   /**
226    * @brief Executes a list of secondary command buffers
227    *
228    * The secondary command buffers will be executed as a part of a primary
229    * command buffer that calls this function.
230    *
231    * @param[in] commandBuffers List of buffers to execute
232    */
233   virtual void ExecuteCommandBuffers(std::vector<const CommandBuffer*>&& commandBuffers) = 0;
234
235   /**
236    * @brief Draw primitives
237    *
238    * @param[in] vertexCount number of vertices
239    * @param[in] instanceCount number of instances
240    * @param[in] firstVertex index of first vertex
241    * @param[in] firstInstance index of first instance
242    */
243   virtual void Draw(
244     uint32_t vertexCount,
245     uint32_t instanceCount,
246     uint32_t firstVertex,
247     uint32_t firstInstance) = 0;
248
249   /**
250    * @brief Draws indexed primitives
251    *
252    * @param[in] indexCount Number of indices
253    * @param[in] instanceCount Number of instances
254    * @param[in] firstIndex first index
255    * @param[in] vertexOffset offset of first vertex
256    * @param[in] firstInstance first instance
257    */
258   virtual void DrawIndexed(
259     uint32_t indexCount,
260     uint32_t instanceCount,
261     uint32_t firstIndex,
262     int32_t  vertexOffset,
263     uint32_t firstInstance) = 0;
264
265   /**
266    * @brief Draws indexed primitives indirectly
267    *
268    * Indirect draw uses additional buffer that holds render data.
269    *
270    * Indirect draw support depends on the hardware (most of modern hardware
271    * supports this drawing technique).
272    *
273    * @param[in] buffer Buffer containing draw parameters
274    * @param[in] offset Offset in bytes where parameters begin
275    * @param[in] drawCount number of draws to execute
276    * @param[in] stride stride between draw parameters
277    */
278   virtual void DrawIndexedIndirect(
279     Buffer&  buffer,
280     uint32_t offset,
281     uint32_t drawCount,
282     uint32_t stride) = 0;
283
284   /**
285    * @brief Draws using native API (via callback)
286    *
287    * DrawNative should be use in order to acquire direct access to the
288    * graphics API like GL. Upon command execution, the backend will
289    * invoke given callback and pass API-specific arguments (for example,
290    * the GL callback will receive EGL context used for rendering).
291    *
292    * The client side must make sure the callback is valid for the
293    * time of execution.
294    *
295    * @param[in] drawInfo NativeDrawInfo structure
296    */
297   virtual void DrawNative(const DrawNativeInfo* drawInfo) = 0;
298
299   /**
300    * @brief Resets CommandBuffer
301    *
302    * This function resets the command buffer and discards all previously
303    * recorded commands.
304    *
305    * Since the allocation may use internal memory pool of the CommandBuffer,
306    * resetting doesn't have to discard all the resources (for example, it doesn't
307    * need to destroy command but only move the pointer to the beginning of
308    * the command buffer).
309    *
310    * It is useful if the command buffer has to be re-recorded frequently, for example,
311    * every frame.
312    */
313   virtual void Reset() = 0;
314
315   /**
316    * @brief Changes scissor rect
317    *
318    * @param[in] value 2D scissor rectangle
319    */
320   virtual void SetScissor(Rect2D value) = 0;
321
322   /**
323    * @brief Enables/disables scissor test
324    *
325    * @param[in] value State of scissor test
326    */
327   virtual void SetScissorTestEnable(bool value) = 0;
328
329   /**
330    * @brief Sets viewport
331    *
332    * @param[in] value 2D viewport area
333    */
334   virtual void SetViewport(Viewport value) = 0;
335
336   /**
337    * @brief Sets whether the viewport should be changed
338    * @param[in] value state of viewport
339    */
340   virtual void SetViewportEnable(bool value) = 0;
341
342   /**
343    * @brief Sets the color mask for all channels.
344    */
345   virtual void SetColorMask(bool enabled) = 0;
346
347   /**
348    * @brief Clears the stencil buffer (outside of BeginRenderPass) to the current stencil mask
349    */
350   virtual void ClearStencilBuffer() = 0;
351
352   /**
353    * @brief Clears the depth buffer (outside of BeginRenderPass) to the current depth mask
354    */
355   virtual void ClearDepthBuffer() = 0;
356
357   /**
358    * @brief Enable or disable the stencil test
359    *
360    * @param[in] stencilEnable whether stencil test should be enabled
361    */
362   virtual void SetStencilTestEnable(bool stencilEnable) = 0;
363
364   /**
365    * @brief The mask used for writing to the stencil buffer.
366    *
367    * It should be as wide as necessary for the stencil texture format.
368    * @param[in] writeMask The mask for wriing to / clearing the stencil buffer
369    */
370   virtual void SetStencilWriteMask(uint32_t writeMask) = 0;
371
372   /**
373    * @brief Setup the stencil function
374    *
375    * @param[in] compareOp How the stencil buffer, reference and compareMask are combined to determine whether to draw a pixel or not.
376    * @param[in] reference A reference value that is ANDed with the mask in the compare op.
377    * @param[in] compareMask The bitplanes from the stencil buffer that are active.
378    */
379   virtual void SetStencilFunc(Graphics::CompareOp compareOp,
380                               uint32_t            reference,
381                               uint32_t            compareMask) = 0;
382
383   /**
384    * @brief Set how subsequent draws will affect the stencil buffer.
385    * @param[in] failOp What happens to stencil buffer if drawing a pixel fails the stencil test
386    * @param[in] passOp What happens to stencil buffer if drawing a pixel passes stencil & depth test
387    * @param[in] depthFailOp What happens to stencil buffer if drawing a pixel passes stencil but fails depth test.
388    */
389   virtual void SetStencilOp(Graphics::StencilOp failOp,
390                             Graphics::StencilOp passOp,
391                             Graphics::StencilOp depthFailOp) = 0;
392
393   /**
394    * @brief Defines the comparison operator for passing the depth test.
395    *
396    * @param[in] compareOp The comparison operator
397    */
398   virtual void SetDepthCompareOp(Graphics::CompareOp compareOp) = 0;
399
400   /**
401    * @brief Enables depth testing
402    *
403    * @param[in] depthTestEnable True if depth testing will be enabled.
404    */
405   virtual void SetDepthTestEnable(bool depthTestEnable) = 0;
406
407   /**
408    * @brief Enables depth writing / clearing
409    *
410    * @param[in] depthWriteEnabled True if the depth buffer can be updated or cleared.
411    */
412   virtual void SetDepthWriteEnable(bool depthWriteEnable) = 0;
413
414 protected:
415   CommandBuffer(CommandBuffer&&) = default;
416   CommandBuffer& operator=(CommandBuffer&&) = default;
417 };
418 } // namespace Dali::Graphics
419
420 #endif