[Vulkan] graphics controller, multiple pipelines
[platform/core/uifw/dali-core.git] / dali / graphics-api / graphics-api-render-command.h
1 #ifndef DALI_GRAPHICS_API_RENDER_COMMAND_H
2 #define DALI_GRAPHICS_API_RENDER_COMMAND_H
3
4 /*
5  * Copyright (c) 2018 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 <tuple>
23 #include <utility>
24 #include <vector>
25
26 // INTERNAL INCLUDES
27 #include <dali/graphics-api/graphics-api-generic-buffer.h>
28 #include <dali/graphics-api/utility/utility-builder.h>
29 #include <dali/graphics-api/utility/utility-strong-type.h>
30 #include <dali/graphics-api/graphics-api-shader-details.h>
31 #include <dali/graphics-api/graphics-api-accessor.h>
32 #include <dali/graphics-api/graphics-api-framebuffer.h>
33 #include <dali/graphics-api/graphics-api-buffer.h>
34
35 namespace Dali
36 {
37 namespace Graphics
38 {
39 namespace API
40 {
41 class Shader;
42 class Texture;
43 class Buffer;
44 class Sampler;
45
46 /**
47  * @brief Interface class for RenderCommand types in the graphics API.
48  */
49 class RenderCommand
50 {
51 public:
52
53   static constexpr uint32_t BINDING_INDEX_DONT_CARE { 0xffffffff };
54
55   enum class InputAttributeRate
56   {
57     PER_VERTEX,
58     PER_INSTANCE
59   };
60
61   enum class IndexType
62   {
63     INDEX_TYPE_UINT16,
64     INDEX_TYPE_UINT32,
65   };
66
67   enum class DrawType
68   {
69     UNDEFINED_DRAW,
70     VERTEX_DRAW,
71     INDEXED_DRAW,
72   };
73
74   /**
75    * Describes buffer attribute binding
76    */
77   struct VertexAttributeBufferBinding
78   {
79     VertexAttributeBufferBinding() = default;
80
81     Accessor<Buffer> buffer{ nullptr };
82     uint32_t location { 0u };
83     uint32_t offset { 0u };
84     uint32_t stride { 0u };
85     InputAttributeRate rate { InputAttributeRate::PER_VERTEX };
86
87     uint32_t binding { 0u };
88     void*    pNext{ nullptr };
89
90     VertexAttributeBufferBinding& SetBuffer( Accessor<Buffer> value )
91     {
92       buffer = value;
93       return *this;
94     }
95     VertexAttributeBufferBinding& SetLocation( uint32_t value )
96     {
97       location = value;
98       return *this;
99     }
100     VertexAttributeBufferBinding& SetOffset( uint32_t value )
101     {
102       offset = value;
103       return *this;
104     }
105     VertexAttributeBufferBinding& SetStride( uint32_t value )
106     {
107       stride = value;
108       return *this;
109     }
110     VertexAttributeBufferBinding& SetBinding( uint32_t value )
111     {
112       binding = value;
113       return *this;
114     }
115     VertexAttributeBufferBinding& SetInputAttributeRate( InputAttributeRate value)
116     {
117       rate = value;
118       return *this;
119     }
120   };
121
122   /**
123    * Structure describes uniform buffer binding
124    */
125   struct UniformBufferBinding
126   {
127     UniformBufferBinding() :
128     buffer( nullptr ), offset( 0u ), dataSize( 0u ), binding( 0u ) {}
129     Accessor<Buffer> buffer;
130     uint32_t offset;
131     uint32_t dataSize;
132     uint32_t binding;
133     void*    pNext{ nullptr };
134
135     UniformBufferBinding& SetBuffer( Accessor<Buffer> value )
136     {
137       buffer = value;
138       return *this;
139     }
140     UniformBufferBinding& SetOffset( uint32_t value )
141     {
142       offset = value;
143       return *this;
144     }
145     UniformBufferBinding& SetDataSize( uint32_t value )
146     {
147       dataSize = value;
148       return *this;
149     }
150     UniformBufferBinding& SetBinding( uint32_t value )
151     {
152       binding = value;
153       return *this;
154     }
155   };
156
157   /**
158    *
159    */
160   struct TextureBinding
161   {
162     Accessor<Texture> texture;
163     Accessor<Sampler> sampler;
164     uint32_t binding;
165     void*    pNext{ nullptr };
166     TextureBinding() : texture(nullptr), sampler( nullptr ), binding( 0u ) {}
167
168     TextureBinding& SetTexture( Accessor<Texture> value )
169     {
170       texture = value;
171       return *this;
172     }
173     TextureBinding& SetSampler( Accessor<Sampler> value )
174     {
175       sampler = value;
176       return *this;
177     }
178     TextureBinding& SetBinding( uint32_t value )
179     {
180       binding = value;
181       return *this;
182     }
183   };
184
185   /**
186    *
187    */
188   struct SamplerBinding
189   {
190     Accessor<Sampler> sampler;
191     uint32_t binding;
192     void*    pNext{ nullptr };
193     SamplerBinding& SetSampler( Accessor<Sampler> value )
194     {
195       sampler = value;
196       return *this;
197     }
198     SamplerBinding& SetBinding( uint32_t value )
199     {
200       binding = value;
201       return *this;
202     }
203   };
204
205   /**
206    *
207    */
208   struct IndexBufferBinding
209   {
210     Accessor<Buffer> buffer { nullptr };
211     uint32_t offset { 0u };
212     IndexType type { IndexType::INDEX_TYPE_UINT16 };
213     void*    pNext{ nullptr };
214     IndexBufferBinding() = default;
215
216     IndexBufferBinding& SetBuffer( Accessor<Buffer> value )
217     {
218       buffer = value;
219       return *this;
220     }
221
222     IndexBufferBinding& SetOffset( uint32_t value )
223     {
224       offset = value;
225       return *this;
226     }
227
228     IndexBufferBinding& SetType( IndexType value )
229     {
230       type = value;
231       return *this;
232     }
233   };
234
235   struct RenderTargetBinding
236   {
237     Accessor<Framebuffer>                 framebuffer { nullptr };
238     std::vector<Framebuffer::ClearColor>  clearColors {};
239     Framebuffer::DepthStencilClearColor   dsClearColor {};
240     void*    pNext{ nullptr };
241     RenderTargetBinding() = default;
242
243     RenderTargetBinding& SetFramebuffer( Accessor<Framebuffer> value )
244     {
245       framebuffer = value;
246       return *this;
247     }
248
249     RenderTargetBinding& SetClearColors( std::vector<Framebuffer::ClearColor>&& value )
250     {
251       clearColors = value;
252       return *this;
253     }
254
255     RenderTargetBinding& SetFramebuffer( Framebuffer::DepthStencilClearColor value )
256     {
257       dsClearColor = value;
258       return *this;
259     }
260   };
261
262   struct DrawCommand
263   {
264     DrawCommand() :
265      drawType( DrawType::UNDEFINED_DRAW ){}
266     DrawType drawType;
267     union
268     {
269       uint32_t firstVertex;
270       uint32_t firstIndex;
271     };
272     union
273     {
274       uint32_t vertexCount;
275       uint32_t indicesCount;
276     };
277     uint32_t firstInstance;
278     uint32_t instanceCount;
279     void*    pNext{ nullptr };
280     DrawCommand& SetDrawType( DrawType value )
281     {
282       drawType = value;
283       return *this;
284     }
285     DrawCommand& SetFirstVertex( uint32_t value )
286     {
287       firstVertex = value;
288       return *this;
289     }
290     DrawCommand& SetFirstIndex( uint32_t value )
291     {
292       firstIndex = value;
293       return *this;
294     }
295     DrawCommand& SetVertexCount( uint32_t value )
296     {
297       vertexCount = value;
298       return *this;
299     }
300     DrawCommand& SetIndicesCount( uint32_t value )
301     {
302       indicesCount = value;
303       return *this;
304     }
305     DrawCommand& SetFirstInstance( uint32_t value )
306     {
307       firstInstance = value;
308       return *this;
309     }
310     DrawCommand& SetInstanceCount( uint32_t value )
311     {
312       instanceCount = value;
313       return *this;
314     }
315   };
316
317   /**
318    *
319    */
320   struct PushConstantsBinding
321   {
322     void*     data;
323     uint32_t  size;
324     uint32_t  binding;
325     void*    pNext{ nullptr };
326     PushConstantsBinding() = default;
327
328     PushConstantsBinding& SetData( void* value )
329     {
330       data = value;
331       return *this;
332     }
333     PushConstantsBinding& SetSize( uint32_t value )
334     {
335       size = value;
336       return *this;
337     }
338     PushConstantsBinding& SetBinding( uint32_t value )
339     {
340       binding = value;
341       return *this;
342     }
343   };
344
345   /**
346    *
347    */
348   struct RenderState
349   {
350     RenderState() = default;
351
352     Accessor<Shader> shader { nullptr };
353
354     RenderState& SetShader( Accessor<Shader> value )
355     {
356       shader = value;
357       return *this;
358     }
359     void*    pNext{ nullptr };
360   };
361
362   RenderCommand()
363   : mVertexBufferBindings(),
364     mUniformBufferBindings(),
365     mTextureBindings(),
366     mIndexBufferBinding(),
367     mRenderTargetBinding(),
368     mDrawCommand(),
369     mPushConstantsBindings(),
370     mRenderState()
371   {
372   }
373
374   // derived types should not be moved direcly to prevent slicing
375   RenderCommand( RenderCommand&& ) = default;
376   RenderCommand& operator=( RenderCommand&& ) = default;
377
378   // not copyable
379   RenderCommand( const RenderCommand& ) = delete;
380   RenderCommand& operator=( const RenderCommand& ) = delete;
381
382   virtual ~RenderCommand() = default;
383
384   /**
385    * Resource binding API
386    */
387   RenderCommand& BindVertexBuffers( std::vector<VertexAttributeBufferBinding>&& bindings )
388   {
389     mVertexBufferBindings = std::move( bindings );
390     return *this;
391   }
392
393   RenderCommand& BindUniformBuffers( std::vector<UniformBufferBinding>&& bindings )
394   {
395     mUniformBufferBindings = std::move( bindings );
396     return *this;
397   }
398
399   RenderCommand& BindTextures( std::vector<TextureBinding>&& bindings )
400   {
401     mTextureBindings = std::move( bindings );
402     return *this;
403   }
404
405   RenderCommand& BindSamplers( std::vector<SamplerBinding>&& bindings )
406   {
407     mSamplerBindings = std::move( bindings );
408     return *this;
409   }
410
411   RenderCommand& PushConstants( std::vector<PushConstantsBinding>&& bindings )
412   {
413     mPushConstantsBindings = bindings;
414     return *this;
415   }
416
417   RenderCommand& BindRenderState( RenderState&& renderState )
418   {
419     mRenderState = renderState;
420     return *this;
421   }
422
423   RenderCommand& Draw( DrawCommand&& drawCommand )
424   {
425     mDrawCommand = drawCommand;
426     return *this;
427   }
428
429   static std::vector<VertexAttributeBufferBinding> NewVertexAttributeBufferBindings()
430   {
431     return std::vector<VertexAttributeBufferBinding>{};
432   }
433
434   /**
435    * Makes a copy ( or moves ) all bindings from the source array
436    * @param bindings
437    * @return
438    */
439   static std::vector<VertexAttributeBufferBinding> NewVertexAttributeBufferBindings( std::vector<VertexAttributeBufferBinding>&& bindings )
440   {
441     return std::vector<VertexAttributeBufferBinding>{ std::move(bindings) };
442   }
443
444   static std::vector<VertexAttributeBufferBinding> NewVertexAttributeBufferBindings( const std::vector<VertexAttributeBufferBinding>& bindings )
445   {
446     return std::vector<VertexAttributeBufferBinding>{ bindings };
447   }
448
449   static std::vector<TextureBinding> NewTextureBindings()
450   {
451     return std::vector<TextureBinding>{};
452   }
453
454   static std::vector<PushConstantsBinding> NewPushConstantsBindings( uint32_t count )
455   {
456     auto retval = std::vector<PushConstantsBinding>{};
457     retval.resize( count );
458     return retval;
459   }
460
461   // Getters
462   const std::vector<VertexAttributeBufferBinding>& GetVertexBufferBindings() const
463   {
464     return mVertexBufferBindings;
465   }
466
467   const auto& GetUniformBufferBindings() const
468   {
469     return mUniformBufferBindings;
470   }
471
472   const std::vector<TextureBinding>& GetTextureBindings() const
473   {
474     return mTextureBindings;
475   }
476
477   const auto& GetIndexBufferBinding() const
478   {
479     return mIndexBufferBinding;
480   }
481
482   const auto& GetRenderTargetBinding() const
483   {
484     return mRenderTargetBinding;
485   }
486
487   const DrawCommand& GetDrawCommand() const
488   {
489     return mDrawCommand;
490   }
491
492   const std::vector<PushConstantsBinding>& GetPushConstantsBindings() const
493   {
494     return mPushConstantsBindings;
495   }
496
497   const RenderState& GetRenderState() const
498   {
499     return mRenderState;
500   }
501
502 protected:
503
504   std::vector<VertexAttributeBufferBinding> mVertexBufferBindings;
505   std::vector<UniformBufferBinding>         mUniformBufferBindings;
506   std::vector<TextureBinding>               mTextureBindings;
507   std::vector<SamplerBinding>               mSamplerBindings;
508   IndexBufferBinding                        mIndexBufferBinding;
509   RenderTargetBinding                       mRenderTargetBinding;
510   DrawCommand                               mDrawCommand;
511   std::vector<PushConstantsBinding>         mPushConstantsBindings;
512   RenderState                               mRenderState;
513
514 };
515
516 using RenderCommandBuilder = Utility::Builder<RenderCommand>;
517
518 } // namespace API
519 } // namespace Graphics
520 } // namespace Dali
521
522 #endif // DALI_GRAPHICS_API_RENDER_COMMAND_H