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;
563 void BindVertexBuffers(uint32_t firstBinding,
564 std::vector<const Graphics::Buffer*> buffers,
565 std::vector<uint32_t> offsets) override;
567 void BindUniformBuffers(const std::vector<Graphics::UniformBufferBinding>& bindings) override;
569 void BindPipeline(const Graphics::Pipeline& pipeline) override;
571 void BindTextures(std::vector<TextureBinding>& textureBindings) override;
573 void BindSamplers(std::vector<SamplerBinding>& samplerBindings) override;
575 void BindPushConstants(void* data,
577 uint32_t binding) override;
579 void BindIndexBuffer(const Graphics::Buffer& buffer,
581 Format format) override;
583 void BeginRenderPass(
584 Graphics::RenderPass* renderPass,
585 Graphics::RenderTarget* renderTarget,
587 std::vector<ClearValue> clearValues) override;
590 * @brief Ends current render pass
592 * This command must be issued in order to finalize the render pass.
593 * It's up to the implementation whether anything has to be done but
594 * the Controller may use end RP marker in order to resolve resource
595 * dependencies (for example, to know when target texture is ready
596 * before passing it to another render pass).
598 void EndRenderPass() override;
600 void ExecuteCommandBuffers(std::vector<const Graphics::CommandBuffer*>&& commandBuffers) override;
603 uint32_t vertexCount,
604 uint32_t instanceCount,
605 uint32_t firstVertex,
606 uint32_t firstInstance) override;
610 uint32_t instanceCount,
612 int32_t vertexOffset,
613 uint32_t firstInstance) override;
615 void DrawIndexedIndirect(
616 Graphics::Buffer& buffer,
619 uint32_t stride) override;
621 void Reset() override;
623 void SetScissor(Graphics::Rect2D value) override;
625 void SetScissorTestEnable(bool value) override;
627 void SetViewport(Viewport value) override;
629 void SetViewportEnable(bool value) override;
631 void SetColorMask(bool enabled) override;
633 void ClearStencilBuffer() override;
635 void SetStencilTestEnable(bool stencilEnable) override;
637 void SetStencilWriteMask(uint32_t writeMask) override;
639 void SetStencilOp(Graphics::StencilOp failOp,
640 Graphics::StencilOp passOp,
641 Graphics::StencilOp depthFailOp) override;
643 void SetStencilFunc(Graphics::CompareOp compareOp,
645 uint32_t compareMask) override;
647 void SetDepthCompareOp(Graphics::CompareOp compareOp) override;
649 void SetDepthTestEnable(bool depthTestEnable) override;
651 void SetDepthWriteEnable(bool depthWriteEnable) override;
653 void ClearDepthBuffer() override;
656 * @brief Presents specified render target
658 * @param[in] renderTarget Valid pointer to a RenderTarget
660 * It's internal command that schedules presentation of
661 * specified render target.
663 void PresentRenderTarget(GLES::RenderTarget* renderTarget);
665 [[nodiscard]] const std::vector<Command>& GetCommands() const;
667 void DestroyResource() override;
668 bool InitializeResource() override;
670 void DiscardResource() override;
673 std::vector<Command> mCommands;
675 } // namespace Dali::Graphics::GLES