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
32 CommandBuffer::CommandBuffer(const Graphics::CommandBufferCreateInfo& createInfo, EglGraphicsController& controller)
33 : CommandBufferResource(createInfo, controller)
37 CommandBuffer::~CommandBuffer() = default;
39 void CommandBuffer::BindVertexBuffers(uint32_t firstBinding,
40 std::vector<const Graphics::Buffer*> buffers,
41 std::vector<uint32_t> offsets)
43 mCommands.emplace_back(CommandType::BIND_VERTEX_BUFFERS);
44 auto& bindings = mCommands.back().bindVertexBuffers.vertexBufferBindings;
45 if(bindings.size() < firstBinding + buffers.size())
47 bindings.resize(firstBinding + buffers.size());
48 auto index = firstBinding;
49 for(auto& buf : buffers)
51 bindings[index].buffer = static_cast<const GLES::Buffer*>(buf);
52 bindings[index].offset = offsets[index - firstBinding];
58 void CommandBuffer::BindUniformBuffers(const std::vector<Graphics::UniformBufferBinding>& bindings)
60 mCommands.emplace_back(CommandType::BIND_UNIFORM_BUFFER);
61 auto& cmd = mCommands.back();
62 auto& bindCmd = cmd.bindUniformBuffers;
63 for(const auto& binding : bindings)
67 auto glesBuffer = static_cast<const GLES::Buffer*>(binding.buffer);
68 if(glesBuffer->IsCPUAllocated()) // standalone uniforms
70 bindCmd.standaloneUniformsBufferBinding.buffer = glesBuffer;
71 bindCmd.standaloneUniformsBufferBinding.offset = binding.offset;
72 bindCmd.standaloneUniformsBufferBinding.binding = binding.binding;
73 bindCmd.standaloneUniformsBufferBinding.emulated = true;
75 else // Bind regular UBO
77 // resize binding slots
78 if(binding.binding >= bindCmd.uniformBufferBindings.size())
80 bindCmd.uniformBufferBindings.resize(binding.binding + 1);
82 auto& slot = bindCmd.uniformBufferBindings[binding.binding];
83 slot.buffer = glesBuffer;
84 slot.offset = binding.offset;
85 slot.binding = binding.binding;
86 slot.emulated = false;
92 void CommandBuffer::BindPipeline(const Graphics::Pipeline& pipeline)
94 mCommands.emplace_back(CommandType::BIND_PIPELINE);
95 mCommands.back().bindPipeline.pipeline = static_cast<const GLES::Pipeline*>(&pipeline);
98 void CommandBuffer::BindTextures(std::vector<TextureBinding>& textureBindings)
100 mCommands.emplace_back(CommandType::BIND_TEXTURES);
101 mCommands.back().bindTextures.textureBindings = std::move(textureBindings);
104 void CommandBuffer::BindSamplers(std::vector<SamplerBinding>& samplerBindings)
106 mCommands.emplace_back(CommandType::BIND_SAMPLERS);
107 mCommands.back().bindSamplers.samplerBindings = std::move(samplerBindings);
110 void CommandBuffer::BindPushConstants(void* data,
116 void CommandBuffer::BindIndexBuffer(const Graphics::Buffer& buffer,
120 mCommands.emplace_back(CommandType::BIND_INDEX_BUFFER);
121 mCommands.back().bindIndexBuffer.buffer = static_cast<const GLES::Buffer*>(&buffer);
122 mCommands.back().bindIndexBuffer.offset = offset;
123 mCommands.back().bindIndexBuffer.format = format;
126 void CommandBuffer::BeginRenderPass(
127 Graphics::RenderPass* renderPass,
128 Graphics::RenderTarget* renderTarget,
130 std::vector<ClearValue> clearValues)
132 mCommands.emplace_back(CommandType::BEGIN_RENDERPASS);
133 auto& cmd = mCommands.back();
134 cmd.beginRenderPass.renderPass = static_cast<GLES::RenderPass*>(renderPass);
135 cmd.beginRenderPass.renderTarget = static_cast<GLES::RenderTarget*>(renderTarget);
136 cmd.beginRenderPass.renderArea = renderArea;
137 cmd.beginRenderPass.clearValues = clearValues;
140 void CommandBuffer::EndRenderPass()
142 mCommands.emplace_back(CommandType::END_RENDERPASS);
145 void CommandBuffer::ExecuteCommandBuffers(std::vector<Graphics::CommandBuffer*>&& commandBuffers)
147 mCommands.emplace_back(CommandType::EXECUTE_COMMAND_BUFFERS);
148 auto& cmd = mCommands.back();
149 cmd.executeCommandBuffers.buffers.reserve(commandBuffers.size());
150 for(auto&& item : commandBuffers)
152 cmd.executeCommandBuffers.buffers.emplace_back(static_cast<GLES::CommandBuffer*>(item));
156 void CommandBuffer::Draw(
157 uint32_t vertexCount,
158 uint32_t instanceCount,
159 uint32_t firstVertex,
160 uint32_t firstInstance)
162 mCommands.emplace_back(CommandType::DRAW);
163 auto& cmd = mCommands.back().draw;
164 cmd.type = DrawCallDescriptor::Type::DRAW;
165 cmd.draw.vertexCount = vertexCount;
166 cmd.draw.instanceCount = instanceCount;
167 cmd.draw.firstInstance = firstInstance;
168 cmd.draw.firstVertex = firstVertex;
171 void CommandBuffer::DrawIndexed(
173 uint32_t instanceCount,
175 int32_t vertexOffset,
176 uint32_t firstInstance)
178 mCommands.emplace_back(CommandType::DRAW_INDEXED);
179 auto& cmd = mCommands.back().draw;
180 cmd.type = DrawCallDescriptor::Type::DRAW_INDEXED;
181 cmd.drawIndexed.firstIndex = firstIndex;
182 cmd.drawIndexed.firstInstance = firstInstance;
183 cmd.drawIndexed.indexCount = indexCount;
184 cmd.drawIndexed.vertexOffset = vertexOffset;
185 cmd.drawIndexed.instanceCount = instanceCount;
188 void CommandBuffer::DrawIndexedIndirect(
189 Graphics::Buffer& buffer,
194 mCommands.emplace_back(CommandType::DRAW_INDEXED_INDIRECT);
195 auto& cmd = mCommands.back().draw;
196 cmd.type = DrawCallDescriptor::Type::DRAW_INDEXED_INDIRECT;
197 cmd.drawIndexedIndirect.buffer = static_cast<const GLES::Buffer*>(&buffer);
198 cmd.drawIndexedIndirect.offset = offset;
199 cmd.drawIndexedIndirect.drawCount = drawCount;
200 cmd.drawIndexedIndirect.stride = stride;
203 void CommandBuffer::Reset()
208 void CommandBuffer::SetScissor(Graphics::Rect2D value)
210 mCommands.emplace_back(CommandType::SET_SCISSOR);
211 mCommands.back().scissor.region = value;
214 void CommandBuffer::SetScissorTestEnable(bool value)
216 mCommands.emplace_back(CommandType::SET_SCISSOR_TEST);
217 mCommands.back().scissorTest.enable = value;
220 void CommandBuffer::SetViewport(Viewport value)
222 mCommands.emplace_back(CommandType::SET_VIEWPORT);
223 mCommands.back().viewport.region = value;
226 void CommandBuffer::SetViewportEnable(bool value)
228 // There is no GL equivalent
231 [[nodiscard]] const std::vector<Command>& CommandBuffer::GetCommands() const
236 void CommandBuffer::DestroyResource()
241 bool CommandBuffer::InitializeResource()
247 void CommandBuffer::DiscardResource()
249 GetController().DiscardResource(this);
252 } // namespace Dali::Graphics::GLES