1 #ifndef DALI_GRAPHICS_GLES_COMMAND_BUFFER_H
2 #define DALI_GRAPHICS_GLES_COMMAND_BUFFER_H
5 * Copyright (c) 2021 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.
22 #include <dali/graphics-api/graphics-command-buffer-create-info.h>
23 #include <dali/graphics-api/graphics-command-buffer.h>
24 #include <dali/graphics-api/graphics-types.h>
27 #include "gles-graphics-resource.h"
28 #include "gles-graphics-types.h"
29 #include "gles-sync-object.h"
31 namespace Dali::Graphics::GLES
37 enum class CommandType
48 DRAW_INDEXED_INDIRECT,
54 EXECUTE_COMMAND_BUFFERS,
55 PRESENT_RENDER_TARGET,
59 SET_STENCIL_TEST_ENABLE,
60 SET_STENCIL_WRITE_MASK,
64 SET_DEPTH_TEST_ENABLE,
65 SET_DEPTH_WRITE_ENABLE,
69 * @brief Helper function to invoke destructor on anonymous struct
72 static void InvokeDestructor(T& object)
78 * Command structure allocates memory to store a single command
84 Command(CommandType commandType)
89 case CommandType::BIND_VERTEX_BUFFERS:
91 new(&bindVertexBuffers) decltype(bindVertexBuffers);
94 case CommandType::BIND_TEXTURES:
96 new(&bindTextures) decltype(bindTextures);
99 case CommandType::BEGIN_RENDERPASS:
102 new(&beginRenderPass) decltype(beginRenderPass);
115 case CommandType::BIND_VERTEX_BUFFERS:
117 InvokeDestructor(bindVertexBuffers);
120 case CommandType::BIND_TEXTURES:
122 InvokeDestructor(bindTextures);
125 case CommandType::BEGIN_RENDERPASS:
128 InvokeDestructor(beginRenderPass);
138 * @brief Copy constructor
139 * @param[in] rhs Command
141 Command(const Command& rhs)
145 case CommandType::BIND_VERTEX_BUFFERS:
147 new(&bindVertexBuffers) decltype(bindVertexBuffers);
148 bindVertexBuffers = rhs.bindVertexBuffers;
151 case CommandType::BIND_INDEX_BUFFER:
153 bindIndexBuffer = rhs.bindIndexBuffer;
156 case CommandType::BIND_SAMPLERS:
158 bindSamplers = rhs.bindSamplers;
161 case CommandType::BIND_TEXTURES:
163 new(&bindTextures) decltype(bindTextures);
164 bindTextures = rhs.bindTextures;
167 case CommandType::BIND_PIPELINE:
169 bindPipeline = rhs.bindPipeline;
172 case CommandType::BIND_UNIFORM_BUFFER:
174 bindUniformBuffers = rhs.bindUniformBuffers;
177 case CommandType::DRAW:
179 draw.type = rhs.draw.type;
180 draw.draw = rhs.draw.draw;
183 case CommandType::DRAW_INDEXED:
185 draw.type = rhs.draw.type;
186 draw.drawIndexed = rhs.draw.drawIndexed;
189 case CommandType::DRAW_INDEXED_INDIRECT:
191 draw.type = rhs.draw.type;
192 draw.drawIndexedIndirect = rhs.draw.drawIndexedIndirect;
195 case CommandType::BEGIN_RENDERPASS:
197 new(&beginRenderPass) BeginRenderPassDescriptor(rhs.beginRenderPass);
200 case CommandType::END_RENDERPASS:
202 endRenderPass.syncObject = rhs.endRenderPass.syncObject;
205 case CommandType::EXECUTE_COMMAND_BUFFERS:
207 executeCommandBuffers = rhs.executeCommandBuffers;
210 case CommandType::FLUSH:
215 case CommandType::SET_SCISSOR:
217 scissor.region = rhs.scissor.region;
220 case CommandType::SET_SCISSOR_TEST:
222 scissorTest.enable = rhs.scissorTest.enable;
225 case CommandType::SET_VIEWPORT:
227 viewport.region = rhs.viewport.region;
230 case CommandType::PRESENT_RENDER_TARGET:
232 presentRenderTarget = rhs.presentRenderTarget;
235 case CommandType::SET_COLOR_MASK:
237 colorMask.enabled = rhs.colorMask.enabled;
240 case CommandType::CLEAR_STENCIL_BUFFER:
244 case CommandType::CLEAR_DEPTH_BUFFER:
248 case CommandType::SET_STENCIL_TEST_ENABLE:
250 stencilTest.enabled = rhs.stencilTest.enabled;
253 case CommandType::SET_STENCIL_FUNC:
255 stencilFunc.compareMask = rhs.stencilFunc.compareMask;
256 stencilFunc.compareOp = rhs.stencilFunc.compareOp;
257 stencilFunc.reference = rhs.stencilFunc.reference;
260 case CommandType::SET_STENCIL_WRITE_MASK:
262 stencilWriteMask.mask = rhs.stencilWriteMask.mask;
265 case CommandType::SET_STENCIL_OP:
267 stencilOp.failOp = rhs.stencilOp.failOp;
268 stencilOp.depthFailOp = rhs.stencilOp.depthFailOp;
269 stencilOp.passOp = rhs.stencilOp.passOp;
273 case CommandType::SET_DEPTH_COMPARE_OP:
275 depth.compareOp = rhs.depth.compareOp;
278 case CommandType::SET_DEPTH_TEST_ENABLE:
280 depth.testEnabled = rhs.depth.testEnabled;
283 case CommandType::SET_DEPTH_WRITE_ENABLE:
285 depth.writeEnabled = rhs.depth.writeEnabled;
293 * @brief Move constructor
294 * @param[in] rhs Command
296 Command(Command&& rhs) noexcept
300 case CommandType::BIND_VERTEX_BUFFERS:
302 new(&bindVertexBuffers) decltype(bindVertexBuffers);
303 bindVertexBuffers = std::move(rhs.bindVertexBuffers);
306 case CommandType::BIND_INDEX_BUFFER:
308 bindIndexBuffer = rhs.bindIndexBuffer;
311 case CommandType::BIND_UNIFORM_BUFFER:
313 bindUniformBuffers = std::move(rhs.bindUniformBuffers);
316 case CommandType::BIND_SAMPLERS:
318 bindSamplers = std::move(rhs.bindSamplers);
321 case CommandType::BIND_TEXTURES:
323 new(&bindTextures) decltype(bindTextures);
324 bindTextures = std::move(rhs.bindTextures);
327 case CommandType::BIND_PIPELINE:
329 bindPipeline = rhs.bindPipeline;
332 case CommandType::DRAW:
334 draw.type = rhs.draw.type;
335 draw.draw = rhs.draw.draw;
338 case CommandType::DRAW_INDEXED:
340 draw.type = rhs.draw.type;
341 draw.drawIndexed = rhs.draw.drawIndexed;
344 case CommandType::DRAW_INDEXED_INDIRECT:
346 draw.type = rhs.draw.type;
347 draw.drawIndexedIndirect = rhs.draw.drawIndexedIndirect;
350 case CommandType::BEGIN_RENDERPASS:
352 new(&beginRenderPass) BeginRenderPassDescriptor(std::move(rhs.beginRenderPass));
355 case CommandType::END_RENDERPASS:
357 endRenderPass.syncObject = rhs.endRenderPass.syncObject;
360 case CommandType::EXECUTE_COMMAND_BUFFERS:
362 executeCommandBuffers = std::move(rhs.executeCommandBuffers);
365 case CommandType::FLUSH:
370 case CommandType::SET_SCISSOR:
372 scissor.region = rhs.scissor.region;
375 case CommandType::SET_SCISSOR_TEST:
377 scissorTest.enable = rhs.scissorTest.enable;
380 case CommandType::SET_VIEWPORT:
382 viewport.region = rhs.viewport.region;
385 case CommandType::PRESENT_RENDER_TARGET:
387 presentRenderTarget = rhs.presentRenderTarget;
390 case CommandType::SET_COLOR_MASK:
392 colorMask.enabled = rhs.colorMask.enabled;
395 case CommandType::CLEAR_STENCIL_BUFFER:
399 case CommandType::CLEAR_DEPTH_BUFFER:
403 case CommandType::SET_STENCIL_TEST_ENABLE:
405 stencilTest.enabled = rhs.stencilTest.enabled;
408 case CommandType::SET_STENCIL_FUNC:
410 stencilFunc.compareMask = rhs.stencilFunc.compareMask;
411 stencilFunc.compareOp = rhs.stencilFunc.compareOp;
412 stencilFunc.reference = rhs.stencilFunc.reference;
415 case CommandType::SET_STENCIL_WRITE_MASK:
417 stencilWriteMask.mask = rhs.stencilWriteMask.mask;
420 case CommandType::SET_STENCIL_OP:
422 stencilOp.failOp = rhs.stencilOp.failOp;
423 stencilOp.depthFailOp = rhs.stencilOp.depthFailOp;
424 stencilOp.passOp = rhs.stencilOp.passOp;
428 case CommandType::SET_DEPTH_COMPARE_OP:
430 depth.compareOp = rhs.depth.compareOp;
433 case CommandType::SET_DEPTH_TEST_ENABLE:
435 depth.testEnabled = rhs.depth.testEnabled;
438 case CommandType::SET_DEPTH_WRITE_ENABLE:
440 depth.writeEnabled = rhs.depth.writeEnabled;
447 CommandType type{CommandType::FLUSH}; ///< Type of command
453 std::vector<Graphics::TextureBinding> textureBindings;
456 // BindSampler command
459 std::vector<Graphics::SamplerBinding> samplerBindings;
464 using Binding = GLES::VertexBufferBindingDescriptor;
465 std::vector<Binding> vertexBufferBindings;
468 struct : public IndexBufferBindingDescriptor
474 std::vector<UniformBufferBindingDescriptor> uniformBufferBindings{};
475 UniformBufferBindingDescriptor standaloneUniformsBufferBinding{};
476 } bindUniformBuffers;
480 const GLES::Pipeline* pipeline{nullptr};
483 struct : public DrawCallDescriptor
489 Graphics::Rect2D region;
499 Graphics::Viewport region;
502 struct BeginRenderPassDescriptor
507 Graphics::SyncObject* syncObject;
512 std::vector<const GLES::CommandBuffer*> buffers;
513 } executeCommandBuffers;
517 GLES::RenderTarget* targetToPresent;
518 } presentRenderTarget;
522 Graphics::CompareOp compareOp;
529 Graphics::StencilOp failOp;
530 Graphics::StencilOp passOp;
531 Graphics::StencilOp depthFailOp;
541 uint32_t compareMask;
542 Graphics::CompareOp compareOp;
558 using CommandBufferResource = Resource<Graphics::CommandBuffer, Graphics::CommandBufferCreateInfo>;
560 class CommandBuffer : public CommandBufferResource
563 CommandBuffer(const Graphics::CommandBufferCreateInfo& createInfo, EglGraphicsController& controller);
565 ~CommandBuffer() override;
568 * @copydoc Dali::Graphics::CommandBuffer::BindVertexBuffers
570 void BindVertexBuffers(uint32_t firstBinding,
571 std::vector<const Graphics::Buffer*> buffers,
572 std::vector<uint32_t> offsets) override;
575 * @copydoc Dali::Graphics::CommandBuffer::BindUniformBuffers
577 void BindUniformBuffers(const std::vector<Graphics::UniformBufferBinding>& bindings) override;
580 * @copydoc Dali::Graphics::CommandBuffer::BindPipeline
582 void BindPipeline(const Graphics::Pipeline& pipeline) override;
585 * @copydoc Dali::Graphics::CommandBuffer::BindTextures
587 void BindTextures(std::vector<TextureBinding>& textureBindings) override;
590 * @copydoc Dali::Graphics::CommandBuffer::BindSamplers
592 void BindSamplers(std::vector<SamplerBinding>& samplerBindings) override;
595 * @copydoc Dali::Graphics::CommandBuffer::BindPushConstants
597 void BindPushConstants(void* data,
599 uint32_t binding) override;
602 * @copydoc Dali::Graphics::CommandBuffer::BindIndexBuffer
604 void BindIndexBuffer(const Graphics::Buffer& buffer,
606 Format format) override;
609 * @copydoc Dali::Graphics::CommandBuffer::BeginRenderPass
611 void BeginRenderPass(
612 Graphics::RenderPass* renderPass,
613 Graphics::RenderTarget* renderTarget,
615 std::vector<ClearValue> clearValues) override;
618 * @copydoc Dali::Graphics::CommandBuffer::EndRenderPass
620 void EndRenderPass(Graphics::SyncObject* syncObject) override;
623 * @copydoc Dali::Graphics::CommandBuffer::ExecuteCommandBuffers
625 void ExecuteCommandBuffers(std::vector<const Graphics::CommandBuffer*>&& commandBuffers) override;
628 * @copydoc Dali::Graphics::CommandBuffer::Draw
631 uint32_t vertexCount,
632 uint32_t instanceCount,
633 uint32_t firstVertex,
634 uint32_t firstInstance) override;
637 * @copydoc Dali::Graphics::CommandBuffer::DrawIndexed
641 uint32_t instanceCount,
643 int32_t vertexOffset,
644 uint32_t firstInstance) override;
647 * @copydoc Dali::Graphics::CommandBuffer::DrawIndexedIndirect
649 void DrawIndexedIndirect(
650 Graphics::Buffer& buffer,
653 uint32_t stride) override;
656 * @copydoc Dali::Graphics::CommandBuffer::Reset
658 void Reset() override;
661 * @copydoc Dali::Graphics::CommandBuffer::SetScissor
663 void SetScissor(Graphics::Rect2D value) override;
666 * @copydoc Dali::Graphics::CommandBuffer::SetScissorTestEnable
668 void SetScissorTestEnable(bool value) override;
671 * @copydoc Dali::Graphics::CommandBuffer::SetViewport
673 void SetViewport(Viewport value) override;
676 * @copydoc Dali::Graphics::CommandBuffer::SetViewportEnable
678 void SetViewportEnable(bool value) override;
681 * @copydoc Dali::Graphics::CommandBuffer::SetColorMask
683 void SetColorMask(bool enabled) override;
686 * @copydoc Dali::Graphics::CommandBuffer::ClearStencilBuffer
688 void ClearStencilBuffer() override;
691 * @copydoc Dali::Graphics::CommandBuffer::SetStencilTestEnable
693 void SetStencilTestEnable(bool stencilEnable) override;
696 * @copydoc Dali::Graphics::CommandBuffer::SetStencilWriteMask
698 void SetStencilWriteMask(uint32_t writeMask) override;
701 * @copydoc Dali::Graphics::CommandBuffer::SetStencilOp
703 void SetStencilOp(Graphics::StencilOp failOp,
704 Graphics::StencilOp passOp,
705 Graphics::StencilOp depthFailOp) override;
708 * @copydoc Dali::Graphics::CommandBuffer::SetStencilFunc
710 void SetStencilFunc(Graphics::CompareOp compareOp,
712 uint32_t compareMask) override;
715 * @copydoc Dali::Graphics::CommandBuffer::SetDepthCompareOp
717 void SetDepthCompareOp(Graphics::CompareOp compareOp) override;
720 * @copydoc Dali::Graphics::CommandBuffer::SetDepthTestEnable
722 void SetDepthTestEnable(bool depthTestEnable) override;
725 * @copydoc Dali::Graphics::CommandBuffer::SetDepthWriteEnable
727 void SetDepthWriteEnable(bool depthWriteEnable) override;
730 * @copydoc Dali::Graphics::CommandBuffer::ClearDepthBuffer
732 void ClearDepthBuffer() override;
735 * @brief Presents specified render target
737 * @param[in] renderTarget Valid pointer to a RenderTarget
739 * It's internal command that schedules presentation of
740 * specified render target.
742 void PresentRenderTarget(GLES::RenderTarget* renderTarget);
744 [[nodiscard]] const std::vector<Command>& GetCommands() const;
747 * @brief Destroy the associated resources
749 void DestroyResource() override;
752 * @brief Initialize associated resources
754 bool InitializeResource() override;
757 * @brief Add this resource to the discard queue
759 void DiscardResource() override;
762 std::vector<Command> mCommands; ///< List of commands in this command buffer
764 } // namespace Dali::Graphics::GLES