[Vulkan] Cleanup after removing rendering backend
[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-shader-details.h>
28 #include <dali/graphics-api/graphics-api-accessor.h>
29 #include <dali/graphics-api/graphics-api-framebuffer.h>
30 #include <dali/graphics-api/graphics-api-buffer.h>
31
32 namespace Dali
33 {
34 namespace Graphics
35 {
36 namespace API
37 {
38 class Shader;
39 class Texture;
40 class Buffer;
41 class Sampler;
42 class Pipeline;
43
44
45 /**
46  * @brief Interface class for RenderCommand types in the graphics API.
47  *
48  * @startuml
49  *
50  * skinparam defaultFontName Ubuntu Mono
51  * class RenderCommand {
52  *  -- protected --
53  *  #VertexAttributeBufferBinding[]  mVertexBufferBindings
54  *  #UniformBufferBinding[]          mUniformBufferBindings
55  *  #TextureBinding[]                mTextureBindings
56  *  #SamplerBinding[]                mSamplerBindings
57  *  #IndexBufferBinding              mIndexBufferBinding
58  *  #RenderTargetBinding             mRenderTargetBinding
59  *  #DrawCommand                     mDrawCommand
60  *  #PushConstantsBinding[]          mPushConstantsBindings
61  *  #RenderState                     mRenderState
62  *
63  * -- public API --
64  *  +RenderCommand& BindVertextBuffers()
65  *  +RenderCommand& BindUniformBuffers()
66  *  +RenderCommand& BindTextures()
67  *  +RenderCommand& BindSamplers()
68  *  +RenderCommand& PushConstants()
69  *  +RenderCommand& BindRenderState()
70  *  +RenderCommand& Draw()
71  *  -- static API ( helper functions )--
72  *  {static} NewVertexAttributeBufferBindings()
73  *  {static} NewVertexAttributeBufferBindings()
74  *  {static} NewVertexAttributeBufferBindings()
75  *  {static} NewTextureBindings()
76  *  {static} NewPushConstantsBindings()
77  * }
78  *
79  * class VertexAttributeBufferBinding {
80  * Accessor<Buffer>   buffer
81  * uint32_t           location
82  * uint32_t           offset
83  * uint32_t           stride
84  * InputAttributeRate rate
85  * void*              pNext
86  * }
87  *
88  * class UniformBufferBinding {
89  *   #Accessor<Buffer> buffer
90  *   #uint32_t         offset
91  *   #uint32_t         dataSize
92  *   #uint32_t         binding
93  *   #void*            pNext
94  * }
95  *
96   class IndexBufferBinding {
97     #Accessor<Buffer> buffer
98     #uint32_t         offset
99     #IndexType        type
100     #void*            pNext
101   }
102
103   class RenderTargetBinding {
104     #Accessor<Framebuffer>                 framebuffer
105     #std::vector<Framebuffer::ClearColor>  clearColors
106     #Framebuffer::DepthStencilClearColor   dsClearColor
107     #void*                                 pNext
108   }
109
110   class DrawCommand {
111       #DrawType drawType;
112       #uint32_t firstVertex
113       #uint32_t firstIndex
114       #uint32_t vertexCount
115       #uint32_t indicesCount
116       #uint32_t firstInstance
117       #uint32_t instanceCount
118       #void*    pNext
119   }
120
121   class PushConstantsBinding {
122     #void*     data
123     #uint32_t  size
124     #uint32_t  binding
125     #void*    pNext
126   }
127
128   class RenderState {
129     #Accessor<Shader> shader
130     #void*    pNext
131   }
132
133   class TextureBinding {
134     #Accessor<Texture> texture
135     #Accessor<Sampler> sampler
136     #uint32_t          binding
137     #void*             pNext
138   }
139
140   class SamplerBinding {
141     #Accessor<Sampler> sampler
142     #uint32_t binding
143     #void*    pNext
144   }
145
146  note as RenderStateWIP
147  Other render states like
148  blending etc. should be added
149  as public fields of this structure.
150  end note
151
152  RenderStateWIP .. RenderState
153
154  * note as N1
155  * Each state is described as POD
156  * structure which is a Vulkan
157  * approach.
158  * end note
159  *
160  * note as N2
161  * Field pNext may be used by the
162  * implementation to pass additional
163  * data.
164  * end note
165  *
166  * N2 .. VertexAttributeBufferBinding::pNext
167  * N1 .. VertexAttributeBufferBinding
168  * N1 .. UniformBufferBinding
169  * N1 .. IndexBufferBinding
170  * N1 .. RenderTargetBinding
171  *
172
173  *
174  * RenderCommand *-right- VertexAttributeBufferBinding
175  * RenderCommand *-right- UniformBufferBinding
176  * RenderCommand *-left- IndexBufferBinding
177  * RenderCommand *-left- RenderTargetBinding
178  * RenderCommand *-up- RenderState
179  * RenderCommand *-up- DrawCommand
180  * RenderCommand *-down- PushConstantsBinding
181  * RenderCommand *-down- SamplerBinding
182  * RenderCommand *-down- TextureBinding
183  * @enduml
184  */
185 class RenderCommand
186 {
187 public:
188
189   static constexpr uint32_t BINDING_INDEX_DONT_CARE { 0xffffffff };
190
191   enum class InputAttributeRate
192   {
193     PER_VERTEX,
194     PER_INSTANCE
195   };
196
197   enum class IndexType
198   {
199     INDEX_TYPE_UINT16,
200     INDEX_TYPE_UINT32,
201   };
202
203   enum class DrawType
204   {
205     UNDEFINED_DRAW,
206     VERTEX_DRAW,
207     INDEXED_DRAW,
208   };
209
210   /**
211    * Describes buffer attribute binding
212    *
213    */
214   struct VertexAttributeBufferBinding
215   {
216     VertexAttributeBufferBinding() = default;
217
218     Accessor<Buffer> buffer{ nullptr };
219     uint32_t location { 0u };
220     uint32_t offset { 0u };
221     uint32_t stride { 0u };
222     InputAttributeRate rate { InputAttributeRate::PER_VERTEX };
223
224     uint32_t binding { 0u };
225     void*    pNext{ nullptr };
226
227     VertexAttributeBufferBinding& SetBuffer( Accessor<Buffer> value )
228     {
229       buffer = value;
230       return *this;
231     }
232     VertexAttributeBufferBinding& SetLocation( uint32_t value )
233     {
234       location = value;
235       return *this;
236     }
237     VertexAttributeBufferBinding& SetOffset( uint32_t value )
238     {
239       offset = value;
240       return *this;
241     }
242     VertexAttributeBufferBinding& SetStride( uint32_t value )
243     {
244       stride = value;
245       return *this;
246     }
247     VertexAttributeBufferBinding& SetBinding( uint32_t value )
248     {
249       binding = value;
250       return *this;
251     }
252     VertexAttributeBufferBinding& SetInputAttributeRate( InputAttributeRate value)
253     {
254       rate = value;
255       return *this;
256     }
257   };
258
259   /**
260    * Structure describes uniform buffer binding
261    */
262   struct UniformBufferBinding
263   {
264     UniformBufferBinding() :
265     buffer( nullptr ), offset( 0u ), dataSize( 0u ), binding( 0u ) {}
266     Accessor<Buffer> buffer;
267     uint32_t offset;
268     uint32_t dataSize;
269     uint32_t binding;
270     void*    pNext{ nullptr };
271
272     UniformBufferBinding& SetBuffer( Accessor<Buffer> value )
273     {
274       buffer = value;
275       return *this;
276     }
277     UniformBufferBinding& SetOffset( uint32_t value )
278     {
279       offset = value;
280       return *this;
281     }
282     UniformBufferBinding& SetDataSize( uint32_t value )
283     {
284       dataSize = value;
285       return *this;
286     }
287     UniformBufferBinding& SetBinding( uint32_t value )
288     {
289       binding = value;
290       return *this;
291     }
292   };
293
294   /**
295    *
296    */
297   struct TextureBinding
298   {
299     Accessor<Texture> texture;
300     Accessor<Sampler> sampler;
301     uint32_t binding;
302     void*    pNext{ nullptr };
303     TextureBinding() : texture(nullptr), sampler( nullptr ), binding( 0u ) {}
304
305     TextureBinding& SetTexture( Accessor<Texture> value )
306     {
307       texture = value;
308       return *this;
309     }
310     TextureBinding& SetSampler( Accessor<Sampler> value )
311     {
312       sampler = value;
313       return *this;
314     }
315     TextureBinding& SetBinding( uint32_t value )
316     {
317       binding = value;
318       return *this;
319     }
320   };
321
322   /**
323    *
324    */
325   struct SamplerBinding
326   {
327     Accessor<Sampler> sampler;
328     uint32_t binding;
329     void*    pNext{ nullptr };
330     SamplerBinding& SetSampler( Accessor<Sampler> value )
331     {
332       sampler = value;
333       return *this;
334     }
335     SamplerBinding& SetBinding( uint32_t value )
336     {
337       binding = value;
338       return *this;
339     }
340   };
341
342   /**
343    *
344    */
345   struct IndexBufferBinding
346   {
347     Accessor<Buffer> buffer { nullptr };
348     uint32_t offset { 0u };
349     IndexType type { IndexType::INDEX_TYPE_UINT16 };
350     void*    pNext{ nullptr };
351     IndexBufferBinding() = default;
352
353     IndexBufferBinding& SetBuffer( Accessor<Buffer> value )
354     {
355       buffer = value;
356       return *this;
357     }
358
359     IndexBufferBinding& SetOffset( uint32_t value )
360     {
361       offset = value;
362       return *this;
363     }
364
365     IndexBufferBinding& SetType( IndexType value )
366     {
367       type = value;
368       return *this;
369     }
370   };
371
372   struct RenderTargetBinding
373   {
374     Accessor<Framebuffer>                 framebuffer { nullptr };
375     std::vector<Framebuffer::ClearColor>  clearColors {};
376     Framebuffer::DepthStencilClearColor   dsClearColor {};
377     void*    pNext{ nullptr };
378     RenderTargetBinding() = default;
379
380     RenderTargetBinding& SetFramebuffer( Accessor<Framebuffer> value )
381     {
382       framebuffer = value;
383       return *this;
384     }
385
386     RenderTargetBinding& SetClearColors( std::vector<Framebuffer::ClearColor>&& value )
387     {
388       clearColors = value;
389       return *this;
390     }
391
392     RenderTargetBinding& SetFramebuffer( Framebuffer::DepthStencilClearColor value )
393     {
394       dsClearColor = value;
395       return *this;
396     }
397   };
398
399   struct DrawCommand
400   {
401     DrawCommand() :
402      drawType( DrawType::UNDEFINED_DRAW ){}
403     DrawType drawType;
404     union
405     {
406       uint32_t firstVertex;
407       uint32_t firstIndex;
408     };
409     union
410     {
411       uint32_t vertexCount;
412       uint32_t indicesCount;
413     };
414     uint32_t firstInstance;
415     uint32_t instanceCount;
416     void*    pNext{ nullptr };
417     DrawCommand& SetDrawType( DrawType value )
418     {
419       drawType = value;
420       return *this;
421     }
422     DrawCommand& SetFirstVertex( uint32_t value )
423     {
424       firstVertex = value;
425       return *this;
426     }
427     DrawCommand& SetFirstIndex( uint32_t value )
428     {
429       firstIndex = value;
430       return *this;
431     }
432     DrawCommand& SetVertexCount( uint32_t value )
433     {
434       vertexCount = value;
435       return *this;
436     }
437     DrawCommand& SetIndicesCount( uint32_t value )
438     {
439       indicesCount = value;
440       return *this;
441     }
442     DrawCommand& SetFirstInstance( uint32_t value )
443     {
444       firstInstance = value;
445       return *this;
446     }
447     DrawCommand& SetInstanceCount( uint32_t value )
448     {
449       instanceCount = value;
450       return *this;
451     }
452   };
453
454   /**
455    *
456    */
457   struct PushConstantsBinding
458   {
459     void*     data;
460     uint32_t  size;
461     uint32_t  binding;
462     void*    pNext{ nullptr };
463     PushConstantsBinding() = default;
464
465     PushConstantsBinding& SetData( void* value )
466     {
467       data = value;
468       return *this;
469     }
470     PushConstantsBinding& SetSize( uint32_t value )
471     {
472       size = value;
473       return *this;
474     }
475     PushConstantsBinding& SetBinding( uint32_t value )
476     {
477       binding = value;
478       return *this;
479     }
480   };
481
482   /**
483    *
484    */
485   struct RenderState
486   {
487     struct BlendState
488     {
489       bool blendingEnabled { false };
490     };
491
492     RenderState() = default;
493
494     Accessor<Shader> shader { nullptr };
495     BlendState blendState {};
496
497     RenderState& SetShader( Accessor<Shader> value )
498     {
499       shader = value;
500       return *this;
501     }
502
503     RenderState& SetBlendState( BlendState value )
504     {
505       blendState = value;
506       return *this;
507     }
508
509     void*    pNext{ nullptr };
510   };
511
512   RenderCommand()
513   : mVertexBufferBindings(),
514     mUniformBufferBindings(),
515     mTextureBindings(),
516     mIndexBufferBinding(),
517     mRenderTargetBinding(),
518     mDrawCommand(),
519     mPushConstantsBindings(),
520     mRenderState(),
521     mPipeline( nullptr )
522   {
523   }
524
525   // derived types should not be moved direcly to prevent slicing
526   RenderCommand( RenderCommand&& ) = default;
527   RenderCommand& operator=( RenderCommand&& ) = default;
528
529   // not copyable
530   RenderCommand( const RenderCommand& ) = delete;
531   RenderCommand& operator=( const RenderCommand& ) = delete;
532
533   virtual ~RenderCommand() = default;
534
535   /**
536    * Resource binding API
537    */
538   RenderCommand& BindVertexBuffers( std::vector<VertexAttributeBufferBinding>&& bindings )
539   {
540     mVertexBufferBindings = std::move( bindings );
541     return *this;
542   }
543
544   RenderCommand& BindUniformBuffers( std::vector<UniformBufferBinding>&& bindings )
545   {
546     mUniformBufferBindings = std::move( bindings );
547     return *this;
548   }
549
550   RenderCommand& BindTextures( std::vector<TextureBinding>&& bindings )
551   {
552     mTextureBindings = std::move( bindings );
553     return *this;
554   }
555
556   RenderCommand& BindSamplers( std::vector<SamplerBinding>&& bindings )
557   {
558     mSamplerBindings = std::move( bindings );
559     return *this;
560   }
561
562   RenderCommand& PushConstants( std::vector<PushConstantsBinding>&& bindings )
563   {
564     mPushConstantsBindings = bindings;
565     return *this;
566   }
567
568   RenderCommand& BindRenderState( RenderState& renderState )
569   {
570     mRenderState = renderState;
571     return *this;
572   }
573
574   RenderCommand& BindRenderTarget( const RenderTargetBinding& binding )
575   {
576     mRenderTargetBinding = binding;
577     return *this;
578   }
579
580   RenderCommand& BindRenderTarget( RenderTargetBinding&& binding )
581   {
582     mRenderTargetBinding = std::move(binding);
583     return *this;
584   }
585
586   RenderCommand& Draw( DrawCommand&& drawCommand )
587   {
588     mDrawCommand = drawCommand;
589     return *this;
590   }
591
592   RenderCommand& BindPipeline( const Accessor<Pipeline>& pipeline )
593   {
594     mPipeline = pipeline;
595     return *this;
596   }
597
598   static std::vector<VertexAttributeBufferBinding> NewVertexAttributeBufferBindings()
599   {
600     return std::vector<VertexAttributeBufferBinding>{};
601   }
602
603   /**
604    * Makes a copy ( or moves ) all bindings from the source array
605    * @param bindings
606    * @return
607    */
608   static std::vector<VertexAttributeBufferBinding> NewVertexAttributeBufferBindings( std::vector<VertexAttributeBufferBinding>&& bindings )
609   {
610     return std::vector<VertexAttributeBufferBinding>{ std::move(bindings) };
611   }
612
613   static std::vector<VertexAttributeBufferBinding> NewVertexAttributeBufferBindings( const std::vector<VertexAttributeBufferBinding>& bindings )
614   {
615     return std::vector<VertexAttributeBufferBinding>{ bindings };
616   }
617
618   static std::vector<TextureBinding> NewTextureBindings()
619   {
620     return std::vector<TextureBinding>{};
621   }
622
623   static std::vector<PushConstantsBinding> NewPushConstantsBindings( uint32_t count )
624   {
625     auto retval = std::vector<PushConstantsBinding>{};
626     retval.resize( count );
627     return retval;
628   }
629
630   // Getters
631   const std::vector<VertexAttributeBufferBinding>& GetVertexBufferBindings() const
632   {
633     return mVertexBufferBindings;
634   }
635
636   const auto& GetUniformBufferBindings() const
637   {
638     return mUniformBufferBindings;
639   }
640
641   const std::vector<TextureBinding>& GetTextureBindings() const
642   {
643     return mTextureBindings;
644   }
645
646   const auto& GetIndexBufferBinding() const
647   {
648     return mIndexBufferBinding;
649   }
650
651   const auto& GetRenderTargetBinding() const
652   {
653     return mRenderTargetBinding;
654   }
655
656   const DrawCommand& GetDrawCommand() const
657   {
658     return mDrawCommand;
659   }
660
661   const std::vector<PushConstantsBinding>& GetPushConstantsBindings() const
662   {
663     return mPushConstantsBindings;
664   }
665
666   const RenderState& GetRenderState() const
667   {
668     return mRenderState;
669   }
670
671   Accessor<Pipeline> GetPipeline() const
672   {
673     return mPipeline;
674   }
675
676 protected:
677
678   std::vector<VertexAttributeBufferBinding> mVertexBufferBindings;
679   std::vector<UniformBufferBinding>         mUniformBufferBindings;
680   std::vector<TextureBinding>               mTextureBindings;
681   std::vector<SamplerBinding>               mSamplerBindings;
682   IndexBufferBinding                        mIndexBufferBinding;
683   RenderTargetBinding                       mRenderTargetBinding;
684   DrawCommand                               mDrawCommand;
685   std::vector<PushConstantsBinding>         mPushConstantsBindings;
686   RenderState                               mRenderState;
687   Accessor<Pipeline>                        mPipeline;
688
689 };
690
691 } // namespace API
692 } // namespace Graphics
693 } // namespace Dali
694
695
696 #endif // DALI_GRAPHICS_API_RENDER_COMMAND_H
697
698
699 /**
700  * @brief Interface class for RenderCommand types in the graphics API.
701  *
702  * @startuml
703  *
704  * skinparam defaultFontName Ubuntu Mono
705  * class RenderCommand {
706  *  -- protected --
707  *  #VertexAttributeBufferBinding[]  mVertexBufferBindings
708  *  #UniformBufferBinding[]          mUniformBufferBindings
709  *  #TextureBinding[]                mTextureBindings
710  *  #SamplerBinding[]                mSamplerBindings
711  *  #IndexBufferBinding              mIndexBufferBinding
712  *  #RenderTargetBinding             mRenderTargetBinding
713  *  #DrawCommand                     mDrawCommand
714  *  #PushConstantsBinding[]          mPushConstantsBindings
715  *  #RenderState                     mRenderState
716  *
717  * -- public API --
718  *  +RenderCommand& BindVertextBuffers()
719  *  +RenderCommand& BindUniformBuffers()
720  *  +RenderCommand& BindTextures()
721  *  +RenderCommand& BindSamplers()
722  *  +RenderCommand& PushConstants()
723  *  +RenderCommand& BindRenderState()
724  *  +RenderCommand& Draw()
725  *  -- static API ( helper functions )--
726  *  {static} NewVertexAttributeBufferBindings()
727  *  {static} NewVertexAttributeBufferBindings()
728  *  {static} NewVertexAttributeBufferBindings()
729  *  {static} NewTextureBindings()
730  *  {static} NewPushConstantsBindings()
731  * }
732  *
733  * class VertexAttributeBufferBinding {
734  * Accessor<Buffer>   buffer
735  * uint32_t           location
736  * uint32_t           offset
737  * uint32_t           stride
738  * InputAttributeRate rate
739  * void*              pNext
740  * }
741  *
742  * class UniformBufferBinding {
743  *   #Accessor<Buffer> buffer
744  *   #uint32_t         offset
745  *   #uint32_t         dataSize
746  *   #uint32_t         binding
747  *   #void*            pNext
748  * }
749  *
750   class IndexBufferBinding {
751     #Accessor<Buffer> buffer
752     #uint32_t         offset
753     #IndexType        type
754     #void*            pNext
755   }
756
757   class RenderTargetBinding {
758     #Accessor<Framebuffer>                 framebuffer
759     #std::vector<Framebuffer::ClearColor>  clearColors
760     #Framebuffer::DepthStencilClearColor   dsClearColor
761     #void*                                 pNext
762   }
763
764   class DrawCommand {
765       #DrawType drawType;
766       #uint32_t firstVertex
767       #uint32_t firstIndex
768       #uint32_t vertexCount
769       #uint32_t indicesCount
770       #uint32_t firstInstance
771       #uint32_t instanceCount
772       #void*    pNext
773   }
774
775   class PushConstantsBinding {
776     #void*     data
777     #uint32_t  size
778     #uint32_t  binding
779     #void*    pNext
780   }
781
782   class RenderState {
783     #Accessor<Shader> shader
784     #void*    pNext
785   }
786
787   class TextureBinding {
788     #Accessor<Texture> texture
789     #Accessor<Sampler> sampler
790     #uint32_t          binding
791     #void*             pNext
792   }
793
794   class SamplerBinding {
795     #Accessor<Sampler> sampler
796     #uint32_t binding
797     #void*    pNext
798   }
799
800  note as RenderStateWIP
801  Other render states like
802  blending etc. should be added
803  as public fields of this structure.
804  end note
805
806  RenderStateWIP .. RenderState
807
808  * note as N1
809  * Each state is described as POD
810  * structure which is a Vulkan
811  * approach.
812  * end note
813  *
814  * note as N2
815  * Field pNext may be used by the
816  * implementation to pass additional
817  * data.
818  * end note
819  *
820  * N2 .. VertexAttributeBufferBinding::pNext
821  * N1 .. VertexAttributeBufferBinding
822  * N1 .. UniformBufferBinding
823  * N1 .. IndexBufferBinding
824  * N1 .. RenderTargetBinding
825  *
826
827  *
828  * RenderCommand *-right- VertexAttributeBufferBinding
829  * RenderCommand *-right- UniformBufferBinding
830  * RenderCommand *-left- IndexBufferBinding
831  * RenderCommand *-left- RenderTargetBinding
832  * RenderCommand *-up- RenderState
833  * RenderCommand *-up- DrawCommand
834  * RenderCommand *-down- PushConstantsBinding
835  * RenderCommand *-down- SamplerBinding
836  * RenderCommand *-down- TextureBinding
837  * @enduml
838  */