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