Added RenderPass and RenderTarget support
[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] framebuffer valid framebuffer, must not be used with renderTarget
182    * @param[in] renderArea area to draw
183    * @param[in] clearValues clear values (compatible with renderpass spec)
184    */
185   virtual void BeginRenderPass(
186     RenderPass*             renderPass,
187     RenderTarget*           renderTarget,
188     Extent2D                renderArea,
189     std::vector<ClearValue> clearValues) = 0;
190
191   /**
192    * @brief Ends current render pass
193    *
194    * This command must be issued in order to finalize the render pass.
195    * It's up to the implementation whether anything has to be done but
196    * the Controller may use end RP marker in order to resolve resource
197    * dependencies (for example, to know when target texture is ready
198    * before passing it to another render pass).
199    */
200   virtual void EndRenderPass() = 0;
201
202   /**
203    * @brief Executes a list of secondary command buffers
204    *
205    * @param[in] commandBuffers List of buffers to execute
206    */
207   virtual void ExecuteCommandBuffers( std::vector<CommandBuffer*>&& commandBuffers ) = 0;
208
209   /**
210    * @brief Draw primitives
211    *
212    * @param[in] vertexCount number of vertices
213    * @param[in] instanceCount number of instances
214    * @param[in] firstVertex index of first vertex
215    * @param[in] firstInstance index of first instance
216    */
217   virtual void Draw(
218     uint32_t vertexCount,
219     uint32_t instanceCount,
220     uint32_t firstVertex,
221     uint32_t firstInstance) = 0;
222
223   /**
224    * @brief Draws indexed primitives
225    *
226    * @param[in] indexCount Number of indices
227    * @param[in] instanceCount Number of instances
228    * @param[in] firstIndex first index
229    * @param[in] vertexOffset offset of first vertex
230    * @param[in] firstInstance first instance
231    */
232   virtual void DrawIndexed(
233     uint32_t indexCount,
234     uint32_t instanceCount,
235     uint32_t firstIndex,
236     int32_t  vertexOffset,
237     uint32_t firstInstance) = 0;
238
239   /**
240    * @brief Draws indexed primitives indirectly
241    *
242    * Indirect draw uses additional buffer that holds render data.
243    *
244    * Indirect draw support depends on the hardware (most of modern hardware
245    * supports this drawing technique).
246    *
247    * @param[in] buffer Buffer containing draw parameters
248    * @param[in] offset Offset in bytes where parameters begin
249    * @param[in] drawCount number of draws to execute
250    * @param[in] stride stride between draw parameters
251    */
252   virtual void DrawIndexedIndirect(
253     Buffer&  buffer,
254     uint32_t offset,
255     uint32_t drawCount,
256     uint32_t stride) = 0;
257
258   /**
259    * @brief Resets CommandBuffer
260    *
261    * This function resets the command buffer and discards all previously
262    * recorded commands.
263    *
264    * Since the allocation may use internal memory pool of the CommandBuffer,
265    * resetting doesn't have to discard all the resources (for example, it doesn't
266    * need to destroy command but only move the pointer to the beginning of
267    * the command buffer).
268    *
269    * It is useful if the command buffer has to be re-recorded frequently, for example,
270    * every frame.
271    */
272   virtual void Reset() = 0;
273
274   /**
275    * @brief Changes scissor rect
276    *
277    * @param[in] value 2D scissor rectangle
278    */
279   virtual void SetScissor(Rect2D value) = 0;
280
281   /**
282    * @brief Enables/disables scissor test
283    *
284    * @param[in] value State of scissor test
285    */
286   virtual void SetScissorTestEnable(bool value) = 0;
287
288   /**
289    * @brief Sets viewport
290    *
291    * @param[in] value 2D viewport area
292    */
293   virtual void SetViewport(Viewport value) = 0;
294
295   /**
296    * @brief Sets whether the viewport should be changed
297    * @param[in] value state of viewport
298    */
299   virtual void SetViewportEnable(bool value) = 0;
300
301 protected:
302   CommandBuffer(CommandBuffer&&) = default;
303   CommandBuffer& operator=(CommandBuffer&&) = default;
304 };
305 } // Namespace Dali
306
307 #endif