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,
57 * @brief Helper function to invoke destructor on anonymous struct
60 static void InvokeDestructor(T& object)
66 * Command structure allocates memory to store a single command
72 Command(CommandType commandType)
77 case CommandType::BIND_VERTEX_BUFFERS:
79 new(&bindVertexBuffers) decltype(bindVertexBuffers);
82 case CommandType::BIND_TEXTURES:
84 new(&bindTextures) decltype(bindTextures);
87 case CommandType::BEGIN_RENDERPASS:
90 new(&beginRenderPass) decltype(beginRenderPass);
103 case CommandType::BIND_VERTEX_BUFFERS:
105 InvokeDestructor(bindVertexBuffers);
108 case CommandType::BIND_TEXTURES:
110 InvokeDestructor(bindTextures);
113 case CommandType::BEGIN_RENDERPASS:
116 InvokeDestructor(beginRenderPass);
126 * @brief Copy constructor
127 * @param[in] rhs Command
129 Command(const Command& rhs)
133 case CommandType::BIND_VERTEX_BUFFERS:
135 new(&bindVertexBuffers) decltype(bindVertexBuffers);
136 bindVertexBuffers = rhs.bindVertexBuffers;
139 case CommandType::BIND_INDEX_BUFFER:
141 bindIndexBuffer = rhs.bindIndexBuffer;
144 case CommandType::BIND_SAMPLERS:
146 bindSamplers = rhs.bindSamplers;
149 case CommandType::BIND_TEXTURES:
151 new(&bindTextures) decltype(bindTextures);
152 bindTextures = rhs.bindTextures;
155 case CommandType::BIND_PIPELINE:
157 bindPipeline = rhs.bindPipeline;
160 case CommandType::BIND_UNIFORM_BUFFER:
162 bindUniformBuffers = rhs.bindUniformBuffers;
165 case CommandType::DRAW:
167 draw.type = rhs.draw.type;
168 draw.draw = rhs.draw.draw;
171 case CommandType::DRAW_INDEXED:
173 draw.type = rhs.draw.type;
174 draw.drawIndexed = rhs.draw.drawIndexed;
177 case CommandType::DRAW_INDEXED_INDIRECT:
179 draw.type = rhs.draw.type;
180 draw.drawIndexedIndirect = rhs.draw.drawIndexedIndirect;
183 case CommandType::BEGIN_RENDERPASS:
185 new(&beginRenderPass) BeginRenderPassDescriptor(rhs.beginRenderPass);
188 case CommandType::END_RENDERPASS:
192 case CommandType::EXECUTE_COMMAND_BUFFERS:
194 executeCommandBuffers = rhs.executeCommandBuffers;
197 case CommandType::FLUSH:
202 case CommandType::SET_SCISSOR:
204 scissor.region = rhs.scissor.region;
207 case CommandType::SET_SCISSOR_TEST:
209 scissorTest.enable = rhs.scissorTest.enable;
212 case CommandType::SET_VIEWPORT:
214 viewport.region = rhs.viewport.region;
222 * @brief Copy constructor
223 * @param[in] rhs Command
225 Command(Command&& rhs) noexcept
229 case CommandType::BIND_VERTEX_BUFFERS:
231 new(&bindVertexBuffers) decltype(bindVertexBuffers);
232 bindVertexBuffers = std::move(rhs.bindVertexBuffers);
235 case CommandType::BIND_INDEX_BUFFER:
237 bindIndexBuffer = rhs.bindIndexBuffer;
240 case CommandType::BIND_UNIFORM_BUFFER:
242 bindUniformBuffers = std::move(rhs.bindUniformBuffers);
245 case CommandType::BIND_SAMPLERS:
247 bindSamplers = std::move(rhs.bindSamplers);
250 case CommandType::BIND_TEXTURES:
252 new(&bindTextures) decltype(bindTextures);
253 bindTextures = std::move(rhs.bindTextures);
256 case CommandType::BIND_PIPELINE:
258 bindPipeline = rhs.bindPipeline;
261 case CommandType::DRAW:
263 draw.type = rhs.draw.type;
264 draw.draw = rhs.draw.draw;
267 case CommandType::DRAW_INDEXED:
269 draw.type = rhs.draw.type;
270 draw.drawIndexed = rhs.draw.drawIndexed;
273 case CommandType::DRAW_INDEXED_INDIRECT:
275 draw.type = rhs.draw.type;
276 draw.drawIndexedIndirect = rhs.draw.drawIndexedIndirect;
279 case CommandType::BEGIN_RENDERPASS:
281 new(&beginRenderPass) BeginRenderPassDescriptor(std::move(rhs.beginRenderPass));
284 case CommandType::END_RENDERPASS:
288 case CommandType::EXECUTE_COMMAND_BUFFERS:
290 executeCommandBuffers = std::move(rhs.executeCommandBuffers);
293 case CommandType::FLUSH:
298 case CommandType::SET_SCISSOR:
300 scissor.region = rhs.scissor.region;
303 case CommandType::SET_SCISSOR_TEST:
305 scissorTest.enable = rhs.scissorTest.enable;
308 case CommandType::SET_VIEWPORT:
310 viewport.region = rhs.viewport.region;
317 CommandType type{CommandType::FLUSH}; ///< Type of command
323 std::vector<Graphics::TextureBinding> textureBindings;
326 // BindSampler command
329 std::vector<Graphics::SamplerBinding> samplerBindings;
334 using Binding = GLES::VertexBufferBindingDescriptor;
335 std::vector<Binding> vertexBufferBindings;
338 struct : public IndexBufferBindingDescriptor
344 std::vector<UniformBufferBindingDescriptor> uniformBufferBindings{};
345 UniformBufferBindingDescriptor standaloneUniformsBufferBinding{};
346 } bindUniformBuffers;
350 const GLES::Pipeline* pipeline{nullptr};
353 struct : public DrawCallDescriptor
359 Graphics::Rect2D region;
369 Graphics::Viewport region;
372 struct BeginRenderPassDescriptor
381 std::vector<GLES::CommandBuffer*> buffers;
382 } executeCommandBuffers;
386 using CommandBufferResource = Resource<Graphics::CommandBuffer, Graphics::CommandBufferCreateInfo>;
388 class CommandBuffer : public CommandBufferResource
391 CommandBuffer(const Graphics::CommandBufferCreateInfo& createInfo, EglGraphicsController& controller);
393 ~CommandBuffer() override;
395 void BindVertexBuffers(uint32_t firstBinding,
396 std::vector<const Graphics::Buffer*> buffers,
397 std::vector<uint32_t> offsets) override;
399 void BindUniformBuffers(const std::vector<Graphics::UniformBufferBinding>& bindings) override;
401 void BindPipeline(const Graphics::Pipeline& pipeline) override;
403 void BindTextures(std::vector<TextureBinding>& textureBindings) override;
405 void BindSamplers(std::vector<SamplerBinding>& samplerBindings) override;
407 void BindPushConstants(void* data,
409 uint32_t binding) override;
411 void BindIndexBuffer(const Graphics::Buffer& buffer,
413 Format format) override;
415 void BeginRenderPass(
416 Graphics::RenderPass* renderPass,
417 Graphics::RenderTarget* renderTarget,
419 std::vector<ClearValue> clearValues) override;
422 * @brief Ends current render pass
424 * This command must be issued in order to finalize the render pass.
425 * It's up to the implementation whether anything has to be done but
426 * the Controller may use end RP marker in order to resolve resource
427 * dependencies (for example, to know when target texture is ready
428 * before passing it to another render pass).
430 void EndRenderPass() override;
432 void ExecuteCommandBuffers(std::vector<Graphics::CommandBuffer*>&& commandBuffers) override;
435 uint32_t vertexCount,
436 uint32_t instanceCount,
437 uint32_t firstVertex,
438 uint32_t firstInstance) override;
442 uint32_t instanceCount,
444 int32_t vertexOffset,
445 uint32_t firstInstance) override;
447 void DrawIndexedIndirect(
448 Graphics::Buffer& buffer,
451 uint32_t stride) override;
453 void Reset() override;
455 void SetScissor(Graphics::Rect2D value) override;
457 void SetScissorTestEnable(bool value) override;
459 void SetViewport(Viewport value) override;
461 void SetViewportEnable(bool value) override;
463 [[nodiscard]] const std::vector<Command>& GetCommands() const;
465 void DestroyResource() override;
466 bool InitializeResource() override;
468 void DiscardResource() override;
471 std::vector<Command> mCommands;
473 } // namespace Dali::Graphics::GLES