1 #ifndef DALI_GRAPHICS_API_RENDER_COMMAND_H
2 #define DALI_GRAPHICS_API_RENDER_COMMAND_H
5 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
47 * @brief Interface class for RenderCommand types in the graphics API.
53 static constexpr uint32_t BINDING_INDEX_DONT_CARE { 0xffffffff };
55 enum class InputAttributeRate
75 * Describes buffer attribute binding
77 struct VertexAttributeBufferBinding
79 VertexAttributeBufferBinding() = default;
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 };
87 uint32_t binding { 0u };
88 void* pNext{ nullptr };
90 VertexAttributeBufferBinding& SetBuffer( Accessor<Buffer> value )
95 VertexAttributeBufferBinding& SetLocation( uint32_t value )
100 VertexAttributeBufferBinding& SetOffset( uint32_t value )
105 VertexAttributeBufferBinding& SetStride( uint32_t value )
110 VertexAttributeBufferBinding& SetBinding( uint32_t value )
115 VertexAttributeBufferBinding& SetInputAttributeRate( InputAttributeRate value)
123 * Structure describes uniform buffer binding
125 struct UniformBufferBinding
127 UniformBufferBinding() :
128 buffer( nullptr ), offset( 0u ), dataSize( 0u ), binding( 0u ) {}
129 Accessor<Buffer> buffer;
133 void* pNext{ nullptr };
135 UniformBufferBinding& SetBuffer( Accessor<Buffer> value )
140 UniformBufferBinding& SetOffset( uint32_t value )
145 UniformBufferBinding& SetDataSize( uint32_t value )
150 UniformBufferBinding& SetBinding( uint32_t value )
160 struct TextureBinding
162 Accessor<Texture> texture;
163 Accessor<Sampler> sampler;
165 void* pNext{ nullptr };
166 TextureBinding() : texture(nullptr), sampler( nullptr ), binding( 0u ) {}
168 TextureBinding& SetTexture( Accessor<Texture> value )
173 TextureBinding& SetSampler( Accessor<Sampler> value )
178 TextureBinding& SetBinding( uint32_t value )
188 struct SamplerBinding
190 Accessor<Sampler> sampler;
192 void* pNext{ nullptr };
193 SamplerBinding& SetSampler( Accessor<Sampler> value )
198 SamplerBinding& SetBinding( uint32_t value )
208 struct IndexBufferBinding
210 Accessor<Buffer> buffer { nullptr };
211 uint32_t offset { 0u };
212 IndexType type { IndexType::INDEX_TYPE_UINT16 };
213 void* pNext{ nullptr };
214 IndexBufferBinding() = default;
216 IndexBufferBinding& SetBuffer( Accessor<Buffer> value )
222 IndexBufferBinding& SetOffset( uint32_t value )
228 IndexBufferBinding& SetType( IndexType value )
235 struct RenderTargetBinding
237 Accessor<Framebuffer> framebuffer { nullptr };
238 std::vector<Framebuffer::ClearColor> clearColors {};
239 Framebuffer::DepthStencilClearColor dsClearColor {};
240 void* pNext{ nullptr };
241 RenderTargetBinding() = default;
243 RenderTargetBinding& SetFramebuffer( Accessor<Framebuffer> value )
249 RenderTargetBinding& SetClearColors( std::vector<Framebuffer::ClearColor>&& value )
255 RenderTargetBinding& SetFramebuffer( Framebuffer::DepthStencilClearColor value )
257 dsClearColor = value;
265 drawType( DrawType::UNDEFINED_DRAW ){}
269 uint32_t firstVertex;
274 uint32_t vertexCount;
275 uint32_t indicesCount;
277 uint32_t firstInstance;
278 uint32_t instanceCount;
279 void* pNext{ nullptr };
280 DrawCommand& SetDrawType( DrawType value )
285 DrawCommand& SetFirstVertex( uint32_t value )
290 DrawCommand& SetFirstIndex( uint32_t value )
295 DrawCommand& SetVertexCount( uint32_t value )
300 DrawCommand& SetIndicesCount( uint32_t value )
302 indicesCount = value;
305 DrawCommand& SetFirstInstance( uint32_t value )
307 firstInstance = value;
310 DrawCommand& SetInstanceCount( uint32_t value )
312 instanceCount = value;
320 struct PushConstantsBinding
325 void* pNext{ nullptr };
326 PushConstantsBinding() = default;
328 PushConstantsBinding& SetData( void* value )
333 PushConstantsBinding& SetSize( uint32_t value )
338 PushConstantsBinding& SetBinding( uint32_t value )
350 RenderState() = default;
352 Accessor<Shader> shader { nullptr };
354 RenderState& SetShader( Accessor<Shader> value )
359 void* pNext{ nullptr };
363 : mVertexBufferBindings(),
364 mUniformBufferBindings(),
366 mIndexBufferBinding(),
367 mRenderTargetBinding(),
369 mPushConstantsBindings(),
374 // derived types should not be moved direcly to prevent slicing
375 RenderCommand( RenderCommand&& ) = default;
376 RenderCommand& operator=( RenderCommand&& ) = default;
379 RenderCommand( const RenderCommand& ) = delete;
380 RenderCommand& operator=( const RenderCommand& ) = delete;
382 virtual ~RenderCommand() = default;
385 * Resource binding API
387 RenderCommand& BindVertexBuffers( std::vector<VertexAttributeBufferBinding>&& bindings )
389 mVertexBufferBindings = std::move( bindings );
393 RenderCommand& BindUniformBuffers( std::vector<UniformBufferBinding>&& bindings )
395 mUniformBufferBindings = std::move( bindings );
399 RenderCommand& BindTextures( std::vector<TextureBinding>&& bindings )
401 mTextureBindings = std::move( bindings );
405 RenderCommand& BindSamplers( std::vector<SamplerBinding>&& bindings )
407 mSamplerBindings = std::move( bindings );
411 RenderCommand& PushConstants( std::vector<PushConstantsBinding>&& bindings )
413 mPushConstantsBindings = bindings;
417 RenderCommand& BindRenderState( RenderState&& renderState )
419 mRenderState = renderState;
423 RenderCommand& Draw( DrawCommand&& drawCommand )
425 mDrawCommand = drawCommand;
429 static std::vector<VertexAttributeBufferBinding> NewVertexAttributeBufferBindings()
431 return std::vector<VertexAttributeBufferBinding>{};
435 * Makes a copy ( or moves ) all bindings from the source array
439 static std::vector<VertexAttributeBufferBinding> NewVertexAttributeBufferBindings( std::vector<VertexAttributeBufferBinding>&& bindings )
441 return std::vector<VertexAttributeBufferBinding>{ std::move(bindings) };
444 static std::vector<VertexAttributeBufferBinding> NewVertexAttributeBufferBindings( const std::vector<VertexAttributeBufferBinding>& bindings )
446 return std::vector<VertexAttributeBufferBinding>{ bindings };
449 static std::vector<TextureBinding> NewTextureBindings()
451 return std::vector<TextureBinding>{};
454 static std::vector<PushConstantsBinding> NewPushConstantsBindings( uint32_t count )
456 auto retval = std::vector<PushConstantsBinding>{};
457 retval.resize( count );
462 const std::vector<VertexAttributeBufferBinding>& GetVertexBufferBindings() const
464 return mVertexBufferBindings;
467 const auto& GetUniformBufferBindings() const
469 return mUniformBufferBindings;
472 const std::vector<TextureBinding>& GetTextureBindings() const
474 return mTextureBindings;
477 const auto& GetIndexBufferBinding() const
479 return mIndexBufferBinding;
482 const auto& GetRenderTargetBinding() const
484 return mRenderTargetBinding;
487 const DrawCommand& GetDrawCommand() const
492 const std::vector<PushConstantsBinding>& GetPushConstantsBindings() const
494 return mPushConstantsBindings;
497 const RenderState& GetRenderState() const
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;
516 using RenderCommandBuilder = Utility::Builder<RenderCommand>;
519 } // namespace Graphics
522 #endif // DALI_GRAPHICS_API_RENDER_COMMAND_H