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"
30 namespace Dali::Graphics::GLES
36 enum class CommandType
47 DRAW_INDEXED_INDIRECT,
53 EXECUTE_COMMAND_BUFFERS,
54 PRESENT_RENDER_TARGET,
58 SET_STENCIL_TEST_ENABLE,
59 SET_STENCIL_WRITE_MASK,
63 SET_DEPTH_TEST_ENABLE,
64 SET_DEPTH_WRITE_ENABLE,
68 * @brief Helper function to invoke destructor on anonymous struct
71 static void InvokeDestructor(T& object)
77 * Command structure allocates memory to store a single command
83 Command(CommandType commandType)
88 case CommandType::BIND_VERTEX_BUFFERS:
90 new(&bindVertexBuffers) decltype(bindVertexBuffers);
93 case CommandType::BIND_TEXTURES:
95 new(&bindTextures) decltype(bindTextures);
98 case CommandType::BEGIN_RENDERPASS:
101 new(&beginRenderPass) decltype(beginRenderPass);
114 case CommandType::BIND_VERTEX_BUFFERS:
116 InvokeDestructor(bindVertexBuffers);
119 case CommandType::BIND_TEXTURES:
121 InvokeDestructor(bindTextures);
124 case CommandType::BEGIN_RENDERPASS:
127 InvokeDestructor(beginRenderPass);
137 * @brief Copy constructor
138 * @param[in] rhs Command
140 Command(const Command& rhs)
144 case CommandType::BIND_VERTEX_BUFFERS:
146 new(&bindVertexBuffers) decltype(bindVertexBuffers);
147 bindVertexBuffers = rhs.bindVertexBuffers;
150 case CommandType::BIND_INDEX_BUFFER:
152 bindIndexBuffer = rhs.bindIndexBuffer;
155 case CommandType::BIND_SAMPLERS:
157 bindSamplers = rhs.bindSamplers;
160 case CommandType::BIND_TEXTURES:
162 new(&bindTextures) decltype(bindTextures);
163 bindTextures = rhs.bindTextures;
166 case CommandType::BIND_PIPELINE:
168 bindPipeline = rhs.bindPipeline;
171 case CommandType::BIND_UNIFORM_BUFFER:
173 bindUniformBuffers = rhs.bindUniformBuffers;
176 case CommandType::DRAW:
178 draw.type = rhs.draw.type;
179 draw.draw = rhs.draw.draw;
182 case CommandType::DRAW_INDEXED:
184 draw.type = rhs.draw.type;
185 draw.drawIndexed = rhs.draw.drawIndexed;
188 case CommandType::DRAW_INDEXED_INDIRECT:
190 draw.type = rhs.draw.type;
191 draw.drawIndexedIndirect = rhs.draw.drawIndexedIndirect;
194 case CommandType::BEGIN_RENDERPASS:
196 new(&beginRenderPass) BeginRenderPassDescriptor(rhs.beginRenderPass);
199 case CommandType::END_RENDERPASS:
203 case CommandType::EXECUTE_COMMAND_BUFFERS:
205 executeCommandBuffers = rhs.executeCommandBuffers;
208 case CommandType::FLUSH:
213 case CommandType::SET_SCISSOR:
215 scissor.region = rhs.scissor.region;
218 case CommandType::SET_SCISSOR_TEST:
220 scissorTest.enable = rhs.scissorTest.enable;
223 case CommandType::SET_VIEWPORT:
225 viewport.region = rhs.viewport.region;
228 case CommandType::PRESENT_RENDER_TARGET:
230 presentRenderTarget = rhs.presentRenderTarget;
233 case CommandType::SET_COLOR_MASK:
235 colorMask.enabled = rhs.colorMask.enabled;
238 case CommandType::CLEAR_STENCIL_BUFFER:
242 case CommandType::CLEAR_DEPTH_BUFFER:
246 case CommandType::SET_STENCIL_TEST_ENABLE:
248 stencilTest.enabled = rhs.stencilTest.enabled;
251 case CommandType::SET_STENCIL_FUNC:
253 stencilFunc.compareMask = rhs.stencilFunc.compareMask;
254 stencilFunc.compareOp = rhs.stencilFunc.compareOp;
255 stencilFunc.reference = rhs.stencilFunc.reference;
258 case CommandType::SET_STENCIL_WRITE_MASK:
260 stencilWriteMask.mask = rhs.stencilWriteMask.mask;
263 case CommandType::SET_STENCIL_OP:
265 stencilOp.failOp = rhs.stencilOp.failOp;
266 stencilOp.depthFailOp = rhs.stencilOp.depthFailOp;
267 stencilOp.passOp = rhs.stencilOp.passOp;
271 case CommandType::SET_DEPTH_COMPARE_OP:
273 depth.compareOp = rhs.depth.compareOp;
276 case CommandType::SET_DEPTH_TEST_ENABLE:
278 depth.testEnabled = rhs.depth.testEnabled;
281 case CommandType::SET_DEPTH_WRITE_ENABLE:
283 depth.writeEnabled = rhs.depth.writeEnabled;
291 * @brief Move constructor
292 * @param[in] rhs Command
294 Command(Command&& rhs) noexcept
298 case CommandType::BIND_VERTEX_BUFFERS:
300 new(&bindVertexBuffers) decltype(bindVertexBuffers);
301 bindVertexBuffers = std::move(rhs.bindVertexBuffers);
304 case CommandType::BIND_INDEX_BUFFER:
306 bindIndexBuffer = rhs.bindIndexBuffer;
309 case CommandType::BIND_UNIFORM_BUFFER:
311 bindUniformBuffers = std::move(rhs.bindUniformBuffers);
314 case CommandType::BIND_SAMPLERS:
316 bindSamplers = std::move(rhs.bindSamplers);
319 case CommandType::BIND_TEXTURES:
321 new(&bindTextures) decltype(bindTextures);
322 bindTextures = std::move(rhs.bindTextures);
325 case CommandType::BIND_PIPELINE:
327 bindPipeline = rhs.bindPipeline;
330 case CommandType::DRAW:
332 draw.type = rhs.draw.type;
333 draw.draw = rhs.draw.draw;
336 case CommandType::DRAW_INDEXED:
338 draw.type = rhs.draw.type;
339 draw.drawIndexed = rhs.draw.drawIndexed;
342 case CommandType::DRAW_INDEXED_INDIRECT:
344 draw.type = rhs.draw.type;
345 draw.drawIndexedIndirect = rhs.draw.drawIndexedIndirect;
348 case CommandType::BEGIN_RENDERPASS:
350 new(&beginRenderPass) BeginRenderPassDescriptor(std::move(rhs.beginRenderPass));
353 case CommandType::END_RENDERPASS:
357 case CommandType::EXECUTE_COMMAND_BUFFERS:
359 executeCommandBuffers = std::move(rhs.executeCommandBuffers);
362 case CommandType::FLUSH:
367 case CommandType::SET_SCISSOR:
369 scissor.region = rhs.scissor.region;
372 case CommandType::SET_SCISSOR_TEST:
374 scissorTest.enable = rhs.scissorTest.enable;
377 case CommandType::SET_VIEWPORT:
379 viewport.region = rhs.viewport.region;
382 case CommandType::PRESENT_RENDER_TARGET:
384 presentRenderTarget = rhs.presentRenderTarget;
387 case CommandType::SET_COLOR_MASK:
389 colorMask.enabled = rhs.colorMask.enabled;
392 case CommandType::CLEAR_STENCIL_BUFFER:
396 case CommandType::CLEAR_DEPTH_BUFFER:
400 case CommandType::SET_STENCIL_TEST_ENABLE:
402 stencilTest.enabled = rhs.stencilTest.enabled;
405 case CommandType::SET_STENCIL_FUNC:
407 stencilFunc.compareMask = rhs.stencilFunc.compareMask;
408 stencilFunc.compareOp = rhs.stencilFunc.compareOp;
409 stencilFunc.reference = rhs.stencilFunc.reference;
412 case CommandType::SET_STENCIL_WRITE_MASK:
414 stencilWriteMask.mask = rhs.stencilWriteMask.mask;
417 case CommandType::SET_STENCIL_OP:
419 stencilOp.failOp = rhs.stencilOp.failOp;
420 stencilOp.depthFailOp = rhs.stencilOp.depthFailOp;
421 stencilOp.passOp = rhs.stencilOp.passOp;
425 case CommandType::SET_DEPTH_COMPARE_OP:
427 depth.compareOp = rhs.depth.compareOp;
430 case CommandType::SET_DEPTH_TEST_ENABLE:
432 depth.testEnabled = rhs.depth.testEnabled;
435 case CommandType::SET_DEPTH_WRITE_ENABLE:
437 depth.writeEnabled = rhs.depth.writeEnabled;
444 CommandType type{CommandType::FLUSH}; ///< Type of command
450 std::vector<Graphics::TextureBinding> textureBindings;
453 // BindSampler command
456 std::vector<Graphics::SamplerBinding> samplerBindings;
461 using Binding = GLES::VertexBufferBindingDescriptor;
462 std::vector<Binding> vertexBufferBindings;
465 struct : public IndexBufferBindingDescriptor
471 std::vector<UniformBufferBindingDescriptor> uniformBufferBindings{};
472 UniformBufferBindingDescriptor standaloneUniformsBufferBinding{};
473 } bindUniformBuffers;
477 const GLES::Pipeline* pipeline{nullptr};
480 struct : public DrawCallDescriptor
486 Graphics::Rect2D region;
496 Graphics::Viewport region;
499 struct BeginRenderPassDescriptor
508 std::vector<const GLES::CommandBuffer*> buffers;
509 } executeCommandBuffers;
513 GLES::RenderTarget* targetToPresent;
514 } presentRenderTarget;
518 Graphics::CompareOp compareOp;
525 Graphics::StencilOp failOp;
526 Graphics::StencilOp passOp;
527 Graphics::StencilOp depthFailOp;
537 uint32_t compareMask;
538 Graphics::CompareOp compareOp;
554 using CommandBufferResource = Resource<Graphics::CommandBuffer, Graphics::CommandBufferCreateInfo>;
556 class CommandBuffer : public CommandBufferResource
559 CommandBuffer(const Graphics::CommandBufferCreateInfo& createInfo, EglGraphicsController& controller);
561 ~CommandBuffer() override;
564 * @copydoc Dali::Graphics::CommandBuffer::BindVertexBuffers
566 void BindVertexBuffers(uint32_t firstBinding,
567 std::vector<const Graphics::Buffer*> buffers,
568 std::vector<uint32_t> offsets) override;
571 * @copydoc Dali::Graphics::CommandBuffer::BindUniformBuffers
573 void BindUniformBuffers(const std::vector<Graphics::UniformBufferBinding>& bindings) override;
576 * @copydoc Dali::Graphics::CommandBuffer::BindPipeline
578 void BindPipeline(const Graphics::Pipeline& pipeline) override;
581 * @copydoc Dali::Graphics::CommandBuffer::BindTextures
583 void BindTextures(std::vector<TextureBinding>& textureBindings) override;
586 * @copydoc Dali::Graphics::CommandBuffer::BindSamplers
588 void BindSamplers(std::vector<SamplerBinding>& samplerBindings) override;
591 * @copydoc Dali::Graphics::CommandBuffer::BindPushConstants
593 void BindPushConstants(void* data,
595 uint32_t binding) override;
598 * @copydoc Dali::Graphics::CommandBuffer::BindIndexBuffer
600 void BindIndexBuffer(const Graphics::Buffer& buffer,
602 Format format) override;
605 * @copydoc Dali::Graphics::CommandBuffer::BeginRenderPass
607 void BeginRenderPass(
608 Graphics::RenderPass* renderPass,
609 Graphics::RenderTarget* renderTarget,
611 std::vector<ClearValue> clearValues) override;
614 * @copydoc Dali::Graphics::CommandBuffer::EndRenderPass
616 void EndRenderPass() override;
619 * @copydoc Dali::Graphics::CommandBuffer::ExecuteCommandBuffers
621 void ExecuteCommandBuffers(std::vector<const Graphics::CommandBuffer*>&& commandBuffers) override;
624 * @copydoc Dali::Graphics::CommandBuffer::Draw
627 uint32_t vertexCount,
628 uint32_t instanceCount,
629 uint32_t firstVertex,
630 uint32_t firstInstance) override;
633 * @copydoc Dali::Graphics::CommandBuffer::DrawIndexed
637 uint32_t instanceCount,
639 int32_t vertexOffset,
640 uint32_t firstInstance) override;
643 * @copydoc Dali::Graphics::CommandBuffer::DrawIndexedIndirect
645 void DrawIndexedIndirect(
646 Graphics::Buffer& buffer,
649 uint32_t stride) override;
652 * @copydoc Dali::Graphics::CommandBuffer::Reset
654 void Reset() override;
657 * @copydoc Dali::Graphics::CommandBuffer::SetScissor
659 void SetScissor(Graphics::Rect2D value) override;
662 * @copydoc Dali::Graphics::CommandBuffer::SetScissorTestEnable
664 void SetScissorTestEnable(bool value) override;
667 * @copydoc Dali::Graphics::CommandBuffer::SetViewport
669 void SetViewport(Viewport value) override;
672 * @copydoc Dali::Graphics::CommandBuffer::SetViewportEnable
674 void SetViewportEnable(bool value) override;
677 * @copydoc Dali::Graphics::CommandBuffer::SetColorMask
679 void SetColorMask(bool enabled) override;
682 * @copydoc Dali::Graphics::CommandBuffer::ClearStencilBuffer
684 void ClearStencilBuffer() override;
687 * @copydoc Dali::Graphics::CommandBuffer::SetStencilTestEnable
689 void SetStencilTestEnable(bool stencilEnable) override;
692 * @copydoc Dali::Graphics::CommandBuffer::SetStencilWriteMask
694 void SetStencilWriteMask(uint32_t writeMask) override;
697 * @copydoc Dali::Graphics::CommandBuffer::SetStencilOp
699 void SetStencilOp(Graphics::StencilOp failOp,
700 Graphics::StencilOp passOp,
701 Graphics::StencilOp depthFailOp) override;
704 * @copydoc Dali::Graphics::CommandBuffer::SetStencilFunc
706 void SetStencilFunc(Graphics::CompareOp compareOp,
708 uint32_t compareMask) override;
711 * @copydoc Dali::Graphics::CommandBuffer::SetDepthCompareOp
713 void SetDepthCompareOp(Graphics::CompareOp compareOp) override;
716 * @copydoc Dali::Graphics::CommandBuffer::SetDepthTestEnable
718 void SetDepthTestEnable(bool depthTestEnable) override;
721 * @copydoc Dali::Graphics::CommandBuffer::SetDepthWriteEnable
723 void SetDepthWriteEnable(bool depthWriteEnable) override;
726 * @copydoc Dali::Graphics::CommandBuffer::ClearDepthBuffer
728 void ClearDepthBuffer() override;
731 * @brief Presents specified render target
733 * @param[in] renderTarget Valid pointer to a RenderTarget
735 * It's internal command that schedules presentation of
736 * specified render target.
738 void PresentRenderTarget(GLES::RenderTarget* renderTarget);
740 [[nodiscard]] const std::vector<Command>& GetCommands() const;
743 * @brief Destroy the associated resources
745 void DestroyResource() override;
748 * @brief Initialize associated resources
750 bool InitializeResource() override;
753 * @brief Add this resource to the discard queue
755 void DiscardResource() override;
758 std::vector<Command> mCommands; ///< List of commands in this command buffer
760 } // namespace Dali::Graphics::GLES