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-pipeline.h"
25 #include "gles-graphics-texture.h"
27 namespace Dali::Graphics::GLES
29 CommandBuffer::CommandBuffer(const Graphics::CommandBufferCreateInfo& createInfo, EglGraphicsController& controller)
30 : CommandBufferResource(createInfo, controller)
34 CommandBuffer::~CommandBuffer() = default;
36 void CommandBuffer::BindVertexBuffers(uint32_t firstBinding,
37 std::vector<const Graphics::Buffer*> buffers,
38 std::vector<uint32_t> offsets)
40 mCommands.emplace_back(CommandType::BIND_VERTEX_BUFFERS);
41 auto& bindings = mCommands.back().bindVertexBuffers.vertexBufferBindings;
42 if(bindings.size() < firstBinding + buffers.size())
44 bindings.resize(firstBinding + buffers.size());
45 auto index = firstBinding;
46 for(auto& buf : buffers)
48 bindings[index].buffer = static_cast<const GLES::Buffer*>(buf);
49 bindings[index].offset = offsets[index - firstBinding];
55 void CommandBuffer::BindUniformBuffers(const std::vector<Graphics::UniformBufferBinding>& bindings)
57 mCommands.emplace_back(CommandType::BIND_UNIFORM_BUFFER);
58 auto& cmd = mCommands.back();
59 auto& bindCmd = cmd.bindUniformBuffers;
60 for(const auto& binding : bindings)
64 auto glesBuffer = static_cast<const GLES::Buffer*>(binding.buffer);
65 if(glesBuffer->IsCPUAllocated()) // standalone uniforms
67 bindCmd.standaloneUniformsBufferBinding.buffer = glesBuffer;
68 bindCmd.standaloneUniformsBufferBinding.offset = binding.offset;
69 bindCmd.standaloneUniformsBufferBinding.binding = binding.binding;
70 bindCmd.standaloneUniformsBufferBinding.emulated = true;
72 else // Bind regular UBO
74 // resize binding slots
75 if(binding.binding >= bindCmd.uniformBufferBindings.size())
77 bindCmd.uniformBufferBindings.resize(binding.binding + 1);
79 auto& slot = bindCmd.uniformBufferBindings[binding.binding];
80 slot.buffer = glesBuffer;
81 slot.offset = binding.offset;
82 slot.binding = binding.binding;
83 slot.emulated = false;
89 void CommandBuffer::BindPipeline(const Graphics::Pipeline& pipeline)
91 mCommands.emplace_back(CommandType::BIND_PIPELINE);
92 mCommands.back().bindPipeline.pipeline = static_cast<const GLES::Pipeline*>(&pipeline);
95 void CommandBuffer::BindTextures(std::vector<TextureBinding>& textureBindings)
97 mCommands.emplace_back(CommandType::BIND_TEXTURES);
98 mCommands.back().bindTextures.textureBindings = std::move(textureBindings);
101 void CommandBuffer::BindSamplers(std::vector<SamplerBinding>& samplerBindings)
103 mCommands.emplace_back(CommandType::BIND_SAMPLERS);
104 mCommands.back().bindSamplers.samplerBindings = std::move(samplerBindings);
107 void CommandBuffer::BindPushConstants(void* data,
113 void CommandBuffer::BindIndexBuffer(const Graphics::Buffer& buffer,
117 mCommands.emplace_back(CommandType::BIND_INDEX_BUFFER);
118 mCommands.back().bindIndexBuffer.buffer = static_cast<const GLES::Buffer*>(&buffer);
119 mCommands.back().bindIndexBuffer.offset = offset;
120 mCommands.back().bindIndexBuffer.format = format;
123 void CommandBuffer::BeginRenderPass(
124 Graphics::RenderPass& renderPass,
125 Graphics::RenderTarget& renderTarget,
127 std::vector<ClearValue> clearValues)
132 * @brief Ends current render pass
134 * This command must be issued in order to finalize the render pass.
135 * It's up to the implementation whether anything has to be done but
136 * the Controller may use end RP marker in order to resolve resource
137 * dependencies (for example, to know when target texture is ready
138 * before passing it to another render pass).
140 void CommandBuffer::EndRenderPass()
144 void CommandBuffer::Draw(
145 uint32_t vertexCount,
146 uint32_t instanceCount,
147 uint32_t firstVertex,
148 uint32_t firstInstance)
150 mCommands.emplace_back(CommandType::DRAW);
151 auto& cmd = mCommands.back().draw;
152 cmd.type = DrawCallDescriptor::Type::DRAW;
153 cmd.draw.vertexCount = vertexCount;
154 cmd.draw.instanceCount = instanceCount;
155 cmd.draw.firstInstance = firstInstance;
156 cmd.draw.firstVertex = firstVertex;
159 void CommandBuffer::DrawIndexed(
161 uint32_t instanceCount,
163 int32_t vertexOffset,
164 uint32_t firstInstance)
166 mCommands.emplace_back(CommandType::DRAW_INDEXED);
167 auto& cmd = mCommands.back().draw;
168 cmd.type = DrawCallDescriptor::Type::DRAW_INDEXED;
169 cmd.drawIndexed.firstIndex = firstIndex;
170 cmd.drawIndexed.firstInstance = firstInstance;
171 cmd.drawIndexed.indexCount = indexCount;
172 cmd.drawIndexed.vertexOffset = vertexOffset;
173 cmd.drawIndexed.instanceCount = instanceCount;
176 void CommandBuffer::DrawIndexedIndirect(
177 Graphics::Buffer& buffer,
182 mCommands.emplace_back(CommandType::DRAW_INDEXED_INDIRECT);
183 auto& cmd = mCommands.back().draw;
184 cmd.type = DrawCallDescriptor::Type::DRAW_INDEXED_INDIRECT;
185 cmd.drawIndexedIndirect.buffer = static_cast<const GLES::Buffer*>(&buffer);
186 cmd.drawIndexedIndirect.offset = offset;
187 cmd.drawIndexedIndirect.drawCount = drawCount;
188 cmd.drawIndexedIndirect.stride = stride;
191 void CommandBuffer::Reset()
196 void CommandBuffer::SetScissor(Graphics::Rect2D value)
198 mCommands.emplace_back(CommandType::SET_SCISSOR);
199 mCommands.back().scissor.region = value;
202 void CommandBuffer::SetScissorTestEnable(bool value)
204 mCommands.emplace_back(CommandType::SET_SCISSOR_TEST);
205 mCommands.back().scissorTest.enable = value;
208 void CommandBuffer::SetViewport(Viewport value)
210 mCommands.emplace_back(CommandType::SET_VIEWPORT);
211 mCommands.back().viewport.region = value;
214 void CommandBuffer::SetViewportEnable(bool value)
216 // There is no GL equivalent
219 [[nodiscard]] const std::vector<Command>& CommandBuffer::GetCommands() const
224 void CommandBuffer::DestroyResource()
229 bool CommandBuffer::InitializeResource()
235 void CommandBuffer::DiscardResource()
237 GetController().DiscardResource(this);
240 } // namespace Dali::Graphics::GLES