2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "gles-graphics-command-buffer.h"
22 #include "egl-graphics-controller.h"
23 #include "gles-graphics-buffer.h"
24 #include "gles-graphics-framebuffer.h"
25 #include "gles-graphics-pipeline.h"
26 #include "gles-graphics-render-pass.h"
27 #include "gles-graphics-render-target.h"
28 #include "gles-graphics-texture.h"
30 namespace Dali::Graphics::GLES
34 static const uint32_t COMMAND_POOL_DEFAULT_INCREMENT = 1024 * 32; // 32kb banks
35 static const uint32_t MEMORY_POOL_DEFAULT_INCREMENT = 1024; // 4kb memory pool increment
36 static const uint32_t MEMORY_POOL_DEFAULT_ALIGNMENT = 64; // 64bytes alignment
51 uint32_t dataSize{0u};
52 uint32_t capacity{0u};
53 inline T& operator[](int index) const
65 inline void resize(int newSize)
67 ptr = reinterpret_cast<T*>(realloc(ptr, newSize * sizeof(T)));
72 inline T* data() const
77 inline uint32_t size() const
82 // This memory pool guarantees all items will be placed
83 // in the continuous memory area but returned pointers are relative
84 // and require translation before using
85 template<class T, int Increment, int Alignment = 0>
89 inline T& operator[](int index)
93 MemoryPool() = default;
95 IndirectPtr<T> Allocate(uint32_t count)
98 if(fixedCapacity && data.size() != fixedCapacity)
100 data.resize(fixedCapacity);
101 totalCapacity = data.size();
104 // or resize dynamically
105 else if(data.size() <= offset + count)
107 data.resize(data.size() + Increment);
109 // update base pointer, required for address translation
110 totalCapacity = data.size();
113 basePtr = data.data();
115 IndirectPtr<T> retval{uint32_t(uintptr_t(&data[offset]) - uintptr_t(basePtr)), &basePtr};
119 // align offset if needed (only if type size is 1)
120 if(Alignment && sizeof(T) == 1)
122 offset = ((offset / Alignment) * Alignment) + ((offset % Alignment) ? Alignment : 0);
134 // Discards all data and storage
144 uint32_t totalCapacity{0u};
146 uint32_t increment{Increment};
147 uint32_t alignment{Alignment};
148 uint32_t fixedCapacity{0u};
149 void* basePtr{nullptr};
152 MemoryPool<uint8_t, MEMORY_POOL_DEFAULT_INCREMENT, MEMORY_POOL_DEFAULT_ALIGNMENT> memoryPool;
153 MemoryPool<Command, COMMAND_POOL_DEFAULT_INCREMENT, MEMORY_POOL_DEFAULT_ALIGNMENT> commandPool;
156 CommandPool() = default;
157 CommandPool(uint32_t fixedCapacity)
159 commandPool.fixedCapacity = fixedCapacity;
160 memoryPool.fixedCapacity = fixedCapacity * 1024;
164 * Return value may become invalid if pool is resized (by allocating another command)
168 Command* AllocateCommand(CommandType type)
170 auto command = commandPool.Allocate(1);
171 command->type = type;
172 auto* cmd = command.Ptr();
177 IndirectPtr<T> Allocate(uint32_t count)
179 const auto typeSize = sizeof(T);
180 const auto memoryRequired = typeSize * count;
181 auto ptr = memoryPool.Allocate(memoryRequired);
183 // Convert generic pointer and return
184 return IndirectPtr<T>{ptr.ptr, ptr.base};
187 // New (should not be needed)
189 T* New(uint32_t count)
191 auto ptr = Allocate<T>(count);
192 for(auto i = 0u; i < count; ++i)
199 // TODO: explicit delete?
200 void Rollback(bool discard)
209 commandPool.Rollback();
210 memoryPool.Rollback();
214 const Command* GetCommands(uint32_t& size) const
216 size = commandPool.size;
217 return commandPool.data.ptr;
221 CommandBuffer::CommandBuffer(const Graphics::CommandBufferCreateInfo& createInfo, EglGraphicsController& controller)
222 : CommandBufferResource(createInfo, controller)
224 mCommandPool = std::make_unique<CommandPool>(createInfo.fixedCapacity);
227 CommandBuffer::~CommandBuffer() = default;
229 void CommandBuffer::BindVertexBuffers(uint32_t firstBinding,
230 std::vector<const Graphics::Buffer*> buffers,
231 std::vector<uint32_t> offsets)
233 auto command = mCommandPool->AllocateCommand(CommandType::BIND_VERTEX_BUFFERS);
234 command->bindVertexBuffers.vertexBufferBindingsCount = firstBinding + buffers.size();
235 auto pBindings = mCommandPool->Allocate<GLES::VertexBufferBindingDescriptor>(firstBinding + buffers.size());
237 command->bindVertexBuffers.vertexBufferBindings = pBindings;
238 auto index = firstBinding;
239 for(auto& buf : buffers)
241 pBindings[index].buffer = static_cast<const GLES::Buffer*>(buf);
242 pBindings[index].offset = offsets[index - firstBinding];
247 void CommandBuffer::BindUniformBuffers(const std::vector<Graphics::UniformBufferBinding>& bindings)
249 auto command = mCommandPool->AllocateCommand(CommandType::BIND_UNIFORM_BUFFER);
251 auto& cmd = *command;
252 auto& bindCmd = cmd.bindUniformBuffers;
254 // temporary static set of binding slots (thread local)
255 static const auto MAX_UNIFORM_BUFFER_BINDINGS = 64; // TODO: this should be read from introspection
257 // TODO: could use vector?
258 static thread_local UniformBufferBindingDescriptor sTempBindings[MAX_UNIFORM_BUFFER_BINDINGS];
260 // reset temp bindings
261 memset(sTempBindings, 0, sizeof(UniformBufferBindingDescriptor) * MAX_UNIFORM_BUFFER_BINDINGS);
263 auto maxBinding = 0u;
265 // find max binding and standalone UBO
266 for(const auto& binding : bindings)
270 auto glesBuffer = static_cast<const GLES::Buffer*>(binding.buffer);
271 if(glesBuffer->IsCPUAllocated()) // standalone uniforms
273 bindCmd.standaloneUniformsBufferBinding.buffer = glesBuffer;
274 bindCmd.standaloneUniformsBufferBinding.offset = binding.offset;
275 bindCmd.standaloneUniformsBufferBinding.binding = binding.binding;
276 bindCmd.standaloneUniformsBufferBinding.emulated = true;
278 else // Bind regular UBO
280 auto& slot = sTempBindings[binding.binding];
281 slot.buffer = glesBuffer;
282 slot.offset = binding.offset;
283 slot.binding = binding.binding;
284 slot.emulated = false;
285 maxBinding = std::max(maxBinding, binding.binding);
289 bindCmd.uniformBufferBindings = nullptr;
290 bindCmd.uniformBufferBindingsCount = 0u;
295 auto destBindings = mCommandPool->Allocate<UniformBufferBindingDescriptor>(maxBinding + 1);
297 memcpy(destBindings.Ptr(), sTempBindings, sizeof(UniformBufferBindingDescriptor) * (maxBinding + 1));
298 bindCmd.uniformBufferBindings = destBindings;
299 bindCmd.uniformBufferBindingsCount = maxBinding + 1;
303 void CommandBuffer::BindPipeline(const Graphics::Pipeline& pipeline)
305 auto command = mCommandPool->AllocateCommand(CommandType::BIND_PIPELINE);
306 command->bindPipeline.pipeline = static_cast<const GLES::Pipeline*>(&pipeline);
309 void CommandBuffer::BindTextures(std::vector<TextureBinding>& textureBindings)
311 auto command = mCommandPool->AllocateCommand(CommandType::BIND_TEXTURES);
312 auto& bindTexturesCmd = command->bindTextures;
313 bindTexturesCmd.textureBindings = mCommandPool->Allocate<TextureBinding>(textureBindings.size());
314 bindTexturesCmd.textureBindingsCount = textureBindings.size();
315 memcpy(bindTexturesCmd.textureBindings.Ptr(), textureBindings.data(), sizeof(TextureBinding) * textureBindings.size());
318 void CommandBuffer::BindSamplers(std::vector<SamplerBinding>& samplerBindings)
320 auto command = mCommandPool->AllocateCommand(CommandType::BIND_SAMPLERS);
321 auto& bindSamplersCmd = command->bindSamplers;
322 bindSamplersCmd.samplerBindings = mCommandPool->Allocate<SamplerBinding>(samplerBindings.size());
323 bindSamplersCmd.samplerBindingsCount = samplerBindings.size();
324 memcpy(bindSamplersCmd.samplerBindings.Ptr(), samplerBindings.data(), sizeof(TextureBinding) * samplerBindings.size());
327 void CommandBuffer::BindPushConstants(void* data,
333 void CommandBuffer::BindIndexBuffer(const Graphics::Buffer& buffer,
337 auto command = mCommandPool->AllocateCommand(CommandType::BIND_INDEX_BUFFER);
338 command->bindIndexBuffer.buffer = static_cast<const GLES::Buffer*>(&buffer);
339 command->bindIndexBuffer.offset = offset;
340 command->bindIndexBuffer.format = format;
343 void CommandBuffer::BeginRenderPass(
344 Graphics::RenderPass* renderPass,
345 Graphics::RenderTarget* renderTarget,
347 std::vector<ClearValue> clearValues)
349 auto command = mCommandPool->AllocateCommand(CommandType::BEGIN_RENDERPASS);
350 auto& cmd = *command;
351 cmd.beginRenderPass.renderPass = static_cast<GLES::RenderPass*>(renderPass);
352 cmd.beginRenderPass.renderTarget = static_cast<GLES::RenderTarget*>(renderTarget);
353 cmd.beginRenderPass.renderArea = renderArea;
355 cmd.beginRenderPass.clearValues = mCommandPool->Allocate<ClearValue>(clearValues.size());
356 memcpy(cmd.beginRenderPass.clearValues.Ptr(), clearValues.data(), sizeof(ClearValue) * clearValues.size());
357 cmd.beginRenderPass.clearValuesCount = clearValues.size();
360 void CommandBuffer::EndRenderPass(Graphics::SyncObject* syncObject)
362 auto command = mCommandPool->AllocateCommand(CommandType::END_RENDERPASS);
363 command->endRenderPass.syncObject = static_cast<GLES::SyncObject*>(syncObject);
366 void CommandBuffer::ExecuteCommandBuffers(std::vector<const Graphics::CommandBuffer*>&& commandBuffers)
368 auto command = mCommandPool->AllocateCommand(CommandType::EXECUTE_COMMAND_BUFFERS);
369 auto& cmd = command->executeCommandBuffers;
370 cmd.buffers = mCommandPool->Allocate<const GLES::CommandBuffer*>(commandBuffers.size());
371 cmd.buffersCount = commandBuffers.size();
372 for(auto i = 0u; i < cmd.buffersCount; ++i)
374 cmd.buffers[i] = static_cast<const GLES::CommandBuffer*>(commandBuffers[i]);
378 void CommandBuffer::Draw(
379 uint32_t vertexCount,
380 uint32_t instanceCount,
381 uint32_t firstVertex,
382 uint32_t firstInstance)
384 auto command = mCommandPool->AllocateCommand(CommandType::DRAW);
385 auto& cmd = command->draw;
386 cmd.type = DrawCallDescriptor::Type::DRAW;
387 cmd.draw.vertexCount = vertexCount;
388 cmd.draw.instanceCount = instanceCount;
389 cmd.draw.firstInstance = firstInstance;
390 cmd.draw.firstVertex = firstVertex;
393 void CommandBuffer::DrawIndexed(
395 uint32_t instanceCount,
397 int32_t vertexOffset,
398 uint32_t firstInstance)
400 auto command = mCommandPool->AllocateCommand(CommandType::DRAW_INDEXED);
401 auto& cmd = command->draw;
402 cmd.type = DrawCallDescriptor::Type::DRAW_INDEXED;
403 cmd.drawIndexed.firstIndex = firstIndex;
404 cmd.drawIndexed.firstInstance = firstInstance;
405 cmd.drawIndexed.indexCount = indexCount;
406 cmd.drawIndexed.vertexOffset = vertexOffset;
407 cmd.drawIndexed.instanceCount = instanceCount;
410 void CommandBuffer::DrawIndexedIndirect(
411 Graphics::Buffer& buffer,
416 auto command = mCommandPool->AllocateCommand(CommandType::DRAW_INDEXED_INDIRECT);
417 auto& cmd = command->draw;
418 cmd.type = DrawCallDescriptor::Type::DRAW_INDEXED_INDIRECT;
419 cmd.drawIndexedIndirect.buffer = static_cast<const GLES::Buffer*>(&buffer);
420 cmd.drawIndexedIndirect.offset = offset;
421 cmd.drawIndexedIndirect.drawCount = drawCount;
422 cmd.drawIndexedIndirect.stride = stride;
425 void CommandBuffer::Reset()
427 mCommandPool->Rollback(false);
430 void CommandBuffer::SetScissor(Graphics::Rect2D value)
432 auto command = mCommandPool->AllocateCommand(CommandType::SET_SCISSOR);
433 command->scissor.region = value;
436 void CommandBuffer::SetScissorTestEnable(bool value)
438 auto command = mCommandPool->AllocateCommand(CommandType::SET_SCISSOR_TEST);
439 command->scissorTest.enable = value;
442 void CommandBuffer::SetViewport(Viewport value)
444 auto command = mCommandPool->AllocateCommand(CommandType::SET_VIEWPORT);
445 command->viewport.region = value;
448 void CommandBuffer::SetViewportEnable(bool value)
450 // There is no GL equivalent
453 void CommandBuffer::SetColorMask(bool enabled)
455 auto command = mCommandPool->AllocateCommand(CommandType::SET_COLOR_MASK);
456 command->colorMask.enabled = enabled;
459 void CommandBuffer::ClearStencilBuffer()
461 mCommandPool->AllocateCommand(CommandType::CLEAR_STENCIL_BUFFER);
464 void CommandBuffer::SetStencilTestEnable(bool stencilEnable)
466 auto command = mCommandPool->AllocateCommand(CommandType::SET_STENCIL_TEST_ENABLE);
467 command->stencilTest.enabled = stencilEnable;
470 void CommandBuffer::SetStencilWriteMask(uint32_t writeMask)
472 auto command = mCommandPool->AllocateCommand(CommandType::SET_STENCIL_WRITE_MASK);
473 command->stencilWriteMask.mask = writeMask;
476 void CommandBuffer::SetStencilOp(Graphics::StencilOp failOp,
477 Graphics::StencilOp passOp,
478 Graphics::StencilOp depthFailOp)
480 auto command = mCommandPool->AllocateCommand(CommandType::SET_STENCIL_OP);
481 auto& cmd = command->stencilOp;
484 cmd.depthFailOp = depthFailOp;
487 void CommandBuffer::SetStencilFunc(Graphics::CompareOp compareOp,
489 uint32_t compareMask)
491 auto command = mCommandPool->AllocateCommand(CommandType::SET_STENCIL_FUNC);
492 auto& cmd = command->stencilFunc;
493 cmd.compareOp = compareOp;
494 cmd.compareMask = compareMask;
495 cmd.reference = reference;
498 void CommandBuffer::SetDepthCompareOp(Graphics::CompareOp compareOp)
500 auto command = mCommandPool->AllocateCommand(CommandType::SET_DEPTH_COMPARE_OP);
501 command->depth.compareOp = compareOp;
504 void CommandBuffer::SetDepthTestEnable(bool depthTestEnable)
506 auto command = mCommandPool->AllocateCommand(CommandType::SET_DEPTH_TEST_ENABLE);
507 command->depth.testEnabled = depthTestEnable;
509 void CommandBuffer::SetDepthWriteEnable(bool depthWriteEnable)
511 auto command = mCommandPool->AllocateCommand(CommandType::SET_DEPTH_WRITE_ENABLE);
512 command->depth.writeEnabled = depthWriteEnable;
514 void CommandBuffer::ClearDepthBuffer()
516 mCommandPool->AllocateCommand(CommandType::CLEAR_DEPTH_BUFFER);
519 void CommandBuffer::PresentRenderTarget(GLES::RenderTarget* renderTarget)
521 auto command = mCommandPool->AllocateCommand(CommandType::PRESENT_RENDER_TARGET);
522 command->presentRenderTarget.targetToPresent = renderTarget;
525 [[nodiscard]] const Command* CommandBuffer::GetCommands(uint32_t& size) const
527 return mCommandPool->GetCommands(size);
530 void CommandBuffer::DestroyResource()
535 bool CommandBuffer::InitializeResource()
541 void CommandBuffer::DiscardResource()
543 GetController().DiscardResource(this);
546 } // namespace Dali::Graphics::GLES