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-pipeline.h"
27 #include "gles-graphics-types.h"
29 namespace Dali::Graphics::GLES
36 enum class CommandType
51 * Command structure allocates memory to store a single command
64 * @brief Copy constructor
65 * @param[in] rhs Command
67 Command(const Command& rhs)
71 case CommandType::BIND_VERTEX_BUFFERS:
73 bindVertexBuffers = rhs.bindVertexBuffers;
76 case CommandType::BIND_INDEX_BUFFER:
78 bindIndexBuffer = rhs.bindIndexBuffer;
81 case CommandType::BIND_SAMPLERS:
83 bindSamplers = rhs.bindSamplers;
86 case CommandType::BIND_TEXTURES:
88 bindTextures = rhs.bindTextures;
91 case CommandType::BIND_PIPELINE:
93 bindPipeline = rhs.bindPipeline;
96 case CommandType::DRAW:
98 draw.type = rhs.draw.type;
99 draw.draw = rhs.draw.draw;
102 case CommandType::DRAW_INDEXED:
104 draw.type = rhs.draw.type;
105 draw.drawIndexed = rhs.draw.drawIndexed;
108 case CommandType::DRAW_INDEXED_INDIRECT:
110 draw.type = rhs.draw.type;
111 draw.drawIndexedIndirect = rhs.draw.drawIndexedIndirect;
114 case CommandType::FLUSH:
124 * @brief Copy constructor
125 * @param[in] rhs Command
127 Command(Command&& rhs) noexcept
131 case CommandType::BIND_VERTEX_BUFFERS:
133 bindVertexBuffers = std::move(rhs.bindVertexBuffers);
136 case CommandType::BIND_INDEX_BUFFER:
138 bindIndexBuffer = rhs.bindIndexBuffer;
141 case CommandType::BIND_UNIFORM_BUFFER:
143 bindUniformBuffers = std::move(rhs.bindUniformBuffers);
146 case CommandType::BIND_SAMPLERS:
148 bindSamplers = std::move(rhs.bindSamplers);
151 case CommandType::BIND_TEXTURES:
153 bindTextures = std::move(rhs.bindTextures);
156 case CommandType::BIND_PIPELINE:
158 bindPipeline = rhs.bindPipeline;
161 case CommandType::DRAW:
163 draw.type = rhs.draw.type;
164 draw.draw = rhs.draw.draw;
167 case CommandType::DRAW_INDEXED:
169 draw.type = rhs.draw.type;
170 draw.drawIndexed = rhs.draw.drawIndexed;
173 case CommandType::DRAW_INDEXED_INDIRECT:
175 draw.type = rhs.draw.type;
176 draw.drawIndexedIndirect = rhs.draw.drawIndexedIndirect;
179 case CommandType::FLUSH:
188 CommandType type{CommandType::FLUSH}; ///< Type of command
194 std::vector<Graphics::TextureBinding> textureBindings;
197 // BindSampler command
200 std::vector<Graphics::SamplerBinding> samplerBindings;
205 using Binding = GLES::VertexBufferBindingDescriptor;
206 std::vector<Binding> vertexBufferBindings;
209 struct : public IndexBufferBindingDescriptor
215 using Binding = GLES::UniformBufferBindingDescriptor;
216 std::vector<Binding> uniformBufferBindings;
217 } bindUniformBuffers;
221 const GLES::Pipeline* pipeline{nullptr};
224 struct : public DrawCallDescriptor
230 using CommandBufferResource = Resource<Graphics::CommandBuffer, Graphics::CommandBufferCreateInfo>;
232 class CommandBuffer : public CommandBufferResource
235 CommandBuffer(const Graphics::CommandBufferCreateInfo& createInfo, EglGraphicsController& controller)
236 : CommandBufferResource(createInfo, controller)
240 ~CommandBuffer() override = default;
242 void BindVertexBuffers(uint32_t firstBinding,
243 std::vector<const Graphics::Buffer*> buffers,
244 std::vector<uint32_t> offsets) override
246 mCommands.emplace_back();
247 mCommands.back().type = CommandType::BIND_VERTEX_BUFFERS;
248 auto& bindings = mCommands.back().bindVertexBuffers.vertexBufferBindings;
249 if(bindings.size() < firstBinding + buffers.size())
251 bindings.resize(firstBinding + buffers.size());
252 auto index = firstBinding;
253 for(auto& buf : buffers)
255 bindings[index].buffer = static_cast<const GLES::Buffer*>(buf);
256 bindings[index].offset = offsets[index - firstBinding];
262 void BindUniformBuffers(const std::vector<Graphics::UniformBufferBinding>& bindings) override
264 printf("BindUniformBuffers: bindings.size(): %lu\n", bindings.size());
266 mCommands.emplace_back();
267 mCommands.back().type = CommandType::BIND_UNIFORM_BUFFER;
268 auto& uniformBufferBindings = mCommands.back().bindUniformBuffers.uniformBufferBindings;
270 for(auto i = 0u; i < bindings.size(); ++i)
272 const auto& binding = bindings[i];
273 printf("bindings[%u]->buffer: %p, dataSize: %u, offset: %u, binding: %u\n", i, binding.buffer, binding.dataSize, binding.offset, binding.binding);
277 void BindPipeline(const Graphics::Pipeline& pipeline) override
279 mCommands.emplace_back();
280 mCommands.back().type = CommandType::BIND_PIPELINE;
281 mCommands.back().bindPipeline.pipeline = static_cast<const GLES::Pipeline*>(&pipeline);
284 void BindTextures(std::vector<TextureBinding>& textureBindings) override
286 mCommands.emplace_back();
287 mCommands.back().type = CommandType::BIND_TEXTURES;
288 mCommands.back().bindTextures.textureBindings = std::move(textureBindings);
291 void BindSamplers(std::vector<SamplerBinding>& samplerBindings) override
293 mCommands.emplace_back();
294 mCommands.back().bindSamplers.samplerBindings = std::move(samplerBindings);
297 void BindPushConstants(void* data,
299 uint32_t binding) override
303 void BindIndexBuffer(const Graphics::Buffer& buffer,
305 Format format) override
307 mCommands.emplace_back();
308 mCommands.back().type = CommandType::BIND_INDEX_BUFFER;
309 mCommands.back().bindIndexBuffer.buffer = static_cast<const GLES::Buffer*>(&buffer);
310 mCommands.back().bindIndexBuffer.offset = offset;
311 mCommands.back().bindIndexBuffer.format = format;
314 void BeginRenderPass(
315 Graphics::RenderPass& renderPass,
316 Graphics::RenderTarget& renderTarget,
318 std::vector<ClearValue> clearValues) override
323 * @brief Ends current render pass
325 * This command must be issued in order to finalize the render pass.
326 * It's up to the implementation whether anything has to be done but
327 * the Controller may use end RP marker in order to resolve resource
328 * dependencies (for example, to know when target texture is ready
329 * before passing it to another render pass).
331 void EndRenderPass() override
336 uint32_t vertexCount,
337 uint32_t instanceCount,
338 uint32_t firstVertex,
339 uint32_t firstInstance) override
341 mCommands.emplace_back();
342 mCommands.back().type = CommandType::DRAW;
343 auto& cmd = mCommands.back().draw;
344 cmd.type = DrawCallDescriptor::Type::DRAW;
345 cmd.draw.vertexCount = vertexCount;
346 cmd.draw.instanceCount = instanceCount;
347 cmd.draw.firstInstance = firstInstance;
348 cmd.draw.firstVertex = firstVertex;
353 uint32_t instanceCount,
355 int32_t vertexOffset,
356 uint32_t firstInstance) override
358 mCommands.emplace_back();
359 mCommands.back().type = CommandType::DRAW_INDEXED;
360 auto& cmd = mCommands.back().draw;
361 cmd.type = DrawCallDescriptor::Type::DRAW_INDEXED;
362 cmd.drawIndexed.firstIndex = firstIndex;
363 cmd.drawIndexed.firstInstance = firstInstance;
364 cmd.drawIndexed.indexCount = indexCount;
365 cmd.drawIndexed.vertexOffset = vertexOffset;
366 cmd.drawIndexed.instanceCount = instanceCount;
369 void DrawIndexedIndirect(
370 Graphics::Buffer& buffer,
373 uint32_t stride) override
375 mCommands.emplace_back();
376 mCommands.back().type = CommandType::DRAW_INDEXED_INDIRECT;
377 auto& cmd = mCommands.back().draw;
378 cmd.type = DrawCallDescriptor::Type::DRAW_INDEXED_INDIRECT;
379 cmd.drawIndexedIndirect.buffer = static_cast<const GLES::Buffer*>(&buffer);
380 cmd.drawIndexedIndirect.offset = offset;
381 cmd.drawIndexedIndirect.drawCount = drawCount;
382 cmd.drawIndexedIndirect.stride = stride;
385 void Reset(Graphics::CommandBuffer& commandBuffer) override
390 void SetScissor(Extent2D value) override
394 void SetScissorTestEnable(bool value) override
398 void SetViewport(Viewport value) override
402 void SetViewportEnable(bool value) override
406 [[nodiscard]] const std::vector<Command>& GetCommands() const
411 void DestroyResource() override
416 bool InitializeResource() override
422 void DiscardResource() override
428 std::vector<Command> mCommands;
430 } // namespace Dali::Graphics::GLES