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.h>
25 #include "egl-graphics-controller.h"
26 #include "gles-graphics-buffer.h"
27 #include "gles-graphics-pipeline.h"
28 #include "gles-graphics-types.h"
30 namespace Dali::Graphics::GLES
35 enum class CommandType
46 DRAW_INDEXED_INDIRECT,
53 * Command structure allocates memory to store a single command
66 * @brief Copy constructor
67 * @param[in] rhs Command
69 Command(const Command& rhs)
73 case CommandType::BIND_VERTEX_BUFFERS:
75 bindVertexBuffers = rhs.bindVertexBuffers;
78 case CommandType::BIND_INDEX_BUFFER:
80 bindIndexBuffer = rhs.bindIndexBuffer;
83 case CommandType::BIND_SAMPLERS:
85 bindSamplers = rhs.bindSamplers;
88 case CommandType::BIND_TEXTURES:
90 bindTextures = rhs.bindTextures;
93 case CommandType::BIND_PIPELINE:
95 bindPipeline = rhs.bindPipeline;
98 case CommandType::BIND_UNIFORM_BUFFER:
100 bindUniformBuffers = rhs.bindUniformBuffers;
103 case CommandType::DRAW:
105 draw.type = rhs.draw.type;
106 draw.draw = rhs.draw.draw;
109 case CommandType::DRAW_INDEXED:
111 draw.type = rhs.draw.type;
112 draw.drawIndexed = rhs.draw.drawIndexed;
115 case CommandType::DRAW_INDEXED_INDIRECT:
117 draw.type = rhs.draw.type;
118 draw.drawIndexedIndirect = rhs.draw.drawIndexedIndirect;
121 case CommandType::FLUSH:
126 case CommandType::SET_SCISSOR:
128 scissor.region = rhs.scissor.region;
131 case CommandType::SET_SCISSOR_TEST:
133 scissorTest.enable = rhs.scissorTest.enable;
136 case CommandType::SET_VIEWPORT:
138 viewport.region = rhs.viewport.region;
146 * @brief Copy constructor
147 * @param[in] rhs Command
149 Command(Command&& rhs) noexcept
153 case CommandType::BIND_VERTEX_BUFFERS:
155 bindVertexBuffers = std::move(rhs.bindVertexBuffers);
158 case CommandType::BIND_INDEX_BUFFER:
160 bindIndexBuffer = rhs.bindIndexBuffer;
163 case CommandType::BIND_UNIFORM_BUFFER:
165 bindUniformBuffers = std::move(rhs.bindUniformBuffers);
168 case CommandType::BIND_SAMPLERS:
170 bindSamplers = std::move(rhs.bindSamplers);
173 case CommandType::BIND_TEXTURES:
175 bindTextures = std::move(rhs.bindTextures);
178 case CommandType::BIND_PIPELINE:
180 bindPipeline = rhs.bindPipeline;
183 case CommandType::DRAW:
185 draw.type = rhs.draw.type;
186 draw.draw = rhs.draw.draw;
189 case CommandType::DRAW_INDEXED:
191 draw.type = rhs.draw.type;
192 draw.drawIndexed = rhs.draw.drawIndexed;
195 case CommandType::DRAW_INDEXED_INDIRECT:
197 draw.type = rhs.draw.type;
198 draw.drawIndexedIndirect = rhs.draw.drawIndexedIndirect;
201 case CommandType::FLUSH:
206 case CommandType::SET_SCISSOR:
208 scissor.region = rhs.scissor.region;
211 case CommandType::SET_SCISSOR_TEST:
213 scissorTest.enable = rhs.scissorTest.enable;
216 case CommandType::SET_VIEWPORT:
218 viewport.region = rhs.viewport.region;
225 CommandType type{CommandType::FLUSH}; ///< Type of command
231 std::vector<Graphics::TextureBinding> textureBindings;
234 // BindSampler command
237 std::vector<Graphics::SamplerBinding> samplerBindings;
242 using Binding = GLES::VertexBufferBindingDescriptor;
243 std::vector<Binding> vertexBufferBindings;
246 struct : public IndexBufferBindingDescriptor
252 std::vector<UniformBufferBindingDescriptor> uniformBufferBindings{};
253 UniformBufferBindingDescriptor standaloneUniformsBufferBinding{};
254 } bindUniformBuffers;
258 const GLES::Pipeline* pipeline{nullptr};
261 struct : public DrawCallDescriptor
267 Graphics::Rect2D region;
277 Graphics::Viewport region;
282 using CommandBufferResource = Resource<Graphics::CommandBuffer, Graphics::CommandBufferCreateInfo>;
284 class CommandBuffer : public CommandBufferResource
287 CommandBuffer(const Graphics::CommandBufferCreateInfo& createInfo, EglGraphicsController& controller)
288 : CommandBufferResource(createInfo, controller)
292 ~CommandBuffer() override = default;
294 void BindVertexBuffers(uint32_t firstBinding,
295 std::vector<const Graphics::Buffer*> buffers,
296 std::vector<uint32_t> offsets) override
298 mCommands.emplace_back();
299 mCommands.back().type = CommandType::BIND_VERTEX_BUFFERS;
300 auto& bindings = mCommands.back().bindVertexBuffers.vertexBufferBindings;
301 if(bindings.size() < firstBinding + buffers.size())
303 bindings.resize(firstBinding + buffers.size());
304 auto index = firstBinding;
305 for(auto& buf : buffers)
307 bindings[index].buffer = static_cast<const GLES::Buffer*>(buf);
308 bindings[index].offset = offsets[index - firstBinding];
314 void BindUniformBuffers(const std::vector<Graphics::UniformBufferBinding>& bindings) override
316 mCommands.emplace_back();
317 auto& cmd = mCommands.back();
318 cmd.type = CommandType::BIND_UNIFORM_BUFFER;
319 auto& bindCmd = cmd.bindUniformBuffers;
320 for(const auto& binding : bindings)
324 auto glesBuffer = static_cast<const GLES::Buffer*>(binding.buffer);
325 if(glesBuffer->IsCPUAllocated()) // standalone uniforms
327 bindCmd.standaloneUniformsBufferBinding.buffer = glesBuffer;
328 bindCmd.standaloneUniformsBufferBinding.offset = binding.offset;
329 bindCmd.standaloneUniformsBufferBinding.binding = binding.binding;
330 bindCmd.standaloneUniformsBufferBinding.emulated = true;
332 else // Bind regular UBO
334 // resize binding slots
335 if(binding.binding >= bindCmd.uniformBufferBindings.size())
337 bindCmd.uniformBufferBindings.resize(binding.binding + 1);
339 auto& slot = bindCmd.uniformBufferBindings[binding.binding];
340 slot.buffer = glesBuffer;
341 slot.offset = binding.offset;
342 slot.binding = binding.binding;
343 slot.emulated = false;
349 void BindPipeline(const Graphics::Pipeline& pipeline) override
351 mCommands.emplace_back();
352 mCommands.back().type = CommandType::BIND_PIPELINE;
353 mCommands.back().bindPipeline.pipeline = static_cast<const GLES::Pipeline*>(&pipeline);
356 void BindTextures(std::vector<TextureBinding>& textureBindings) override
358 mCommands.emplace_back();
359 mCommands.back().type = CommandType::BIND_TEXTURES;
360 mCommands.back().bindTextures.textureBindings = std::move(textureBindings);
363 void BindSamplers(std::vector<SamplerBinding>& samplerBindings) override
365 mCommands.emplace_back();
366 mCommands.back().bindSamplers.samplerBindings = std::move(samplerBindings);
369 void BindPushConstants(void* data,
371 uint32_t binding) override
375 void BindIndexBuffer(const Graphics::Buffer& buffer,
377 Format format) override
379 mCommands.emplace_back();
380 mCommands.back().type = CommandType::BIND_INDEX_BUFFER;
381 mCommands.back().bindIndexBuffer.buffer = static_cast<const GLES::Buffer*>(&buffer);
382 mCommands.back().bindIndexBuffer.offset = offset;
383 mCommands.back().bindIndexBuffer.format = format;
386 void BeginRenderPass(
387 Graphics::RenderPass& renderPass,
388 Graphics::RenderTarget& renderTarget,
390 std::vector<ClearValue> clearValues) override
395 * @brief Ends current render pass
397 * This command must be issued in order to finalize the render pass.
398 * It's up to the implementation whether anything has to be done but
399 * the Controller may use end RP marker in order to resolve resource
400 * dependencies (for example, to know when target texture is ready
401 * before passing it to another render pass).
403 void EndRenderPass() override
408 uint32_t vertexCount,
409 uint32_t instanceCount,
410 uint32_t firstVertex,
411 uint32_t firstInstance) override
413 mCommands.emplace_back();
414 mCommands.back().type = CommandType::DRAW;
415 auto& cmd = mCommands.back().draw;
416 cmd.type = DrawCallDescriptor::Type::DRAW;
417 cmd.draw.vertexCount = vertexCount;
418 cmd.draw.instanceCount = instanceCount;
419 cmd.draw.firstInstance = firstInstance;
420 cmd.draw.firstVertex = firstVertex;
425 uint32_t instanceCount,
427 int32_t vertexOffset,
428 uint32_t firstInstance) override
430 mCommands.emplace_back();
431 mCommands.back().type = CommandType::DRAW_INDEXED;
432 auto& cmd = mCommands.back().draw;
433 cmd.type = DrawCallDescriptor::Type::DRAW_INDEXED;
434 cmd.drawIndexed.firstIndex = firstIndex;
435 cmd.drawIndexed.firstInstance = firstInstance;
436 cmd.drawIndexed.indexCount = indexCount;
437 cmd.drawIndexed.vertexOffset = vertexOffset;
438 cmd.drawIndexed.instanceCount = instanceCount;
441 void DrawIndexedIndirect(
442 Graphics::Buffer& buffer,
445 uint32_t stride) override
447 mCommands.emplace_back();
448 mCommands.back().type = CommandType::DRAW_INDEXED_INDIRECT;
449 auto& cmd = mCommands.back().draw;
450 cmd.type = DrawCallDescriptor::Type::DRAW_INDEXED_INDIRECT;
451 cmd.drawIndexedIndirect.buffer = static_cast<const GLES::Buffer*>(&buffer);
452 cmd.drawIndexedIndirect.offset = offset;
453 cmd.drawIndexedIndirect.drawCount = drawCount;
454 cmd.drawIndexedIndirect.stride = stride;
457 void Reset() override
462 void SetScissor(Graphics::Rect2D value) override
464 mCommands.emplace_back();
465 mCommands.back().type = CommandType::SET_SCISSOR;
466 mCommands.back().scissor.region = value;
469 void SetScissorTestEnable(bool value) override
471 mCommands.emplace_back();
472 mCommands.back().type = CommandType::SET_SCISSOR_TEST;
473 mCommands.back().scissorTest.enable = value;
476 void SetViewport(Viewport value) override
478 mCommands.emplace_back();
479 mCommands.back().type = CommandType::SET_VIEWPORT;
480 mCommands.back().viewport.region = value;
483 void SetViewportEnable(bool value) override
485 // There is no GL equivalent
488 [[nodiscard]] const std::vector<Command>& GetCommands() const
493 void DestroyResource() override
498 bool InitializeResource() override
504 void DiscardResource() override
510 std::vector<Command> mCommands;
512 } // namespace Dali::Graphics::GLES