Moving owner of secondary command buffers
[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) 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
21 // INTERNAL INCLUDES
22 #include "graphics-types.h"
23
24 namespace Dali::Graphics
25 {
26 class Buffer;
27 class Pipeline;
28 class Texture;
29 class Sampler;
30 class RenderTarget;
31 class RenderPass;
32
33 /**
34  * @brief Uniform buffer bindings.
35  */
36 struct UniformBufferBinding
37 {
38   Buffer* buffer; // Buffer
39   union
40   {
41     void*    offsetPtr; // pointer to the client-side memory
42     uint32_t offset;    // Offset within buffer
43   };
44   uint32_t dataSize; // Size of data to bind
45   uint32_t binding;  // Binding index
46 };
47
48 /**
49  * @brief Texture bindings
50  *
51  * Additionally, sampler may be used in case of having combined
52  * image and sampler.
53  *
54  */
55 struct TextureBinding
56 {
57   const Texture* texture; // texture to be bound
58   const Sampler* sampler; // sampler to be bound
59   uint32_t       binding; // binding index
60 };
61
62 /**
63  * @brief Sampler binding
64  */
65 struct SamplerBinding
66 {
67   Sampler* sampler; // sampler to be bound
68   uint32_t binding; // binding index
69 };
70
71 /**
72  * @brief ClearValue contains an union of RGBA and depthStencil values.
73  */
74 struct ClearValue
75 {
76   union
77   {
78     struct
79     {
80       float r, g, b, a;
81     } color;
82     struct
83     {
84       float    depth;
85       uint32_t stencil;
86     } depthStencil;
87   };
88 };
89
90 /**
91  * @brief CommandBuffer contains a stream of commands to be executed
92  * by the controller.
93  */
94 class CommandBuffer
95 {
96 public:
97   CommandBuffer()          = default;
98   virtual ~CommandBuffer() = default;
99
100   // not copyable
101   CommandBuffer(const CommandBuffer&) = delete;
102   CommandBuffer& operator=(const CommandBuffer&) = delete;
103
104   /**
105    * @brief Binds vertex buffers
106    *
107    * The buffers and offsets arrays must be same length
108    *
109    * @param[in] firstBinding First binding index
110    * @param[in] buffers List of buffers to bind
111    * @param[in] offsets List of offsets for each buffer
112    */
113   virtual void BindVertexBuffers(uint32_t                   firstBinding,
114                                  std::vector<const Buffer*> buffers,
115                                  std::vector<uint32_t>      offsets) = 0;
116
117   /**
118    * @brief Binds uniform buffers
119    *
120    * @param[in] bindings List of uniform buffer bindings
121    */
122   virtual void BindUniformBuffers(const std::vector<UniformBufferBinding>& bindings) = 0;
123
124   /**
125    * @brief Binds pipeline
126    *
127    * @param[in] pipeline valid pipeline
128    */
129   virtual void BindPipeline(const Pipeline& pipeline) = 0;
130
131   /**
132    * @brief Binds textures
133    *
134    * @param[in] textureBindings List of texture bindings
135    */
136   virtual void BindTextures(std::vector<TextureBinding>& textureBindings) = 0;
137
138   /**
139    * @brief Binds samplers
140    *
141    * @param[in] samplerBindings List of sampler bindings
142    */
143   virtual void BindSamplers(std::vector<SamplerBinding>& samplerBindings) = 0;
144
145   /**
146    * @brief Binds buffer containing push constants
147    *
148    * @param[in] data pointer to the buffer
149    * @param[in] size size of data in bytes
150    * @param[in] binding push constants binding index
151    */
152   virtual void BindPushConstants(void*    data,
153                                  uint32_t size,
154                                  uint32_t binding) = 0;
155
156   /**
157    * @brief Binds index buffer
158    *
159    * Most commonly used formats:
160    * R32_UINT,
161    * R16_UINT
162    *
163    * @param[in] buffer Valid buffer
164    * @param[in] offset offset within buffer
165    * @param[in] format Format of index buffer
166    */
167   virtual void BindIndexBuffer(const Buffer& buffer,
168                                uint32_t      offset,
169                                Format        format) = 0;
170   /**
171    * @brief Begins render pass
172    *
173    * The function initialises rendering for specified RenderPass object
174    * onto renderTarget. renderArea defines the scissor rect. Depends on the
175    * renderPass spec, the clearValues may be used.
176    *
177    * Calling EndRenderPass() is necessary to finalize the render pass.
178    *
179    * @param[in] renderPass valid render pass object
180    * @param[in] renderTarget valid render target, must not be used when framebuffer set
181    * @param[in] renderArea area to draw (clear operation is affected)
182    * @param[in] clearValues clear values (compatible with renderpass spec)
183    */
184   virtual void BeginRenderPass(
185     RenderPass*             renderPass,
186     RenderTarget*           renderTarget,
187     Rect2D                  renderArea,
188     std::vector<ClearValue> clearValues) = 0;
189
190   /**
191    * @brief Ends current render pass
192    *
193    * This command must be issued in order to finalize the render pass.
194    * It's up to the implementation whether anything has to be done but
195    * the Controller may use end RP marker in order to resolve resource
196    * dependencies (for example, to know when target texture is ready
197    * before passing it to another render pass).
198    */
199   virtual void EndRenderPass() = 0;
200
201   /**
202    * @brief Executes a list of secondary command buffers
203    *
204    * The secondary command buffers are executed as a part of a primary
205    * command buffer that calls this function.
206    *
207    * @param[in] commandBuffers List of buffers to execute
208    */
209   virtual void ExecuteCommandBuffers(std::vector<const CommandBuffer*>&& commandBuffers) = 0;
210
211   /**
212    * @brief Draw primitives
213    *
214    * @param[in] vertexCount number of vertices
215    * @param[in] instanceCount number of instances
216    * @param[in] firstVertex index of first vertex
217    * @param[in] firstInstance index of first instance
218    */
219   virtual void Draw(
220     uint32_t vertexCount,
221     uint32_t instanceCount,
222     uint32_t firstVertex,
223     uint32_t firstInstance) = 0;
224
225   /**
226    * @brief Draws indexed primitives
227    *
228    * @param[in] indexCount Number of indices
229    * @param[in] instanceCount Number of instances
230    * @param[in] firstIndex first index
231    * @param[in] vertexOffset offset of first vertex
232    * @param[in] firstInstance first instance
233    */
234   virtual void DrawIndexed(
235     uint32_t indexCount,
236     uint32_t instanceCount,
237     uint32_t firstIndex,
238     int32_t  vertexOffset,
239     uint32_t firstInstance) = 0;
240
241   /**
242    * @brief Draws indexed primitives indirectly
243    *
244    * Indirect draw uses additional buffer that holds render data.
245    *
246    * Indirect draw support depends on the hardware (most of modern hardware
247    * supports this drawing technique).
248    *
249    * @param[in] buffer Buffer containing draw parameters
250    * @param[in] offset Offset in bytes where parameters begin
251    * @param[in] drawCount number of draws to execute
252    * @param[in] stride stride between draw parameters
253    */
254   virtual void DrawIndexedIndirect(
255     Buffer&  buffer,
256     uint32_t offset,
257     uint32_t drawCount,
258     uint32_t stride) = 0;
259
260   /**
261    * @brief Resets CommandBuffer
262    *
263    * This function resets the command buffer and discards all previously
264    * recorded commands.
265    *
266    * Since the allocation may use internal memory pool of the CommandBuffer,
267    * resetting doesn't have to discard all the resources (for example, it doesn't
268    * need to destroy command but only move the pointer to the beginning of
269    * the command buffer).
270    *
271    * It is useful if the command buffer has to be re-recorded frequently, for example,
272    * every frame.
273    */
274   virtual void Reset() = 0;
275
276   /**
277    * @brief Changes scissor rect
278    *
279    * @param[in] value 2D scissor rectangle
280    */
281   virtual void SetScissor(Rect2D value) = 0;
282
283   /**
284    * @brief Enables/disables scissor test
285    *
286    * @param[in] value State of scissor test
287    */
288   virtual void SetScissorTestEnable(bool value) = 0;
289
290   /**
291    * @brief Sets viewport
292    *
293    * @param[in] value 2D viewport area
294    */
295   virtual void SetViewport(Viewport value) = 0;
296
297   /**
298    * @brief Sets whether the viewport should be changed
299    * @param[in] value state of viewport
300    */
301   virtual void SetViewportEnable(bool value) = 0;
302
303 protected:
304   CommandBuffer(CommandBuffer&&) = default;
305   CommandBuffer& operator=(CommandBuffer&&) = default;
306 };
307 } // namespace Dali::Graphics
308
309 #endif