Merge "Modify ecore_wl2_window_geometry_set() with window rotation angle" into devel...
[platform/core/uifw/dali-adaptor.git] / dali / internal / graphics / gles-impl / gles-graphics-command-buffer.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include "gles-graphics-command-buffer.h"
20
21 // INTERNAL INCLUDES
22 #include "egl-graphics-controller.h"
23 #include "gles-graphics-buffer.h"
24 #include "gles-graphics-pipeline.h"
25 #include "gles-graphics-texture.h"
26
27 namespace Dali::Graphics::GLES
28 {
29 CommandBuffer::CommandBuffer(const Graphics::CommandBufferCreateInfo& createInfo, EglGraphicsController& controller)
30 : CommandBufferResource(createInfo, controller)
31 {
32 }
33
34 CommandBuffer::~CommandBuffer() = default;
35
36 void CommandBuffer::BindVertexBuffers(uint32_t                             firstBinding,
37                                       std::vector<const Graphics::Buffer*> buffers,
38                                       std::vector<uint32_t>                offsets)
39 {
40   mCommands.emplace_back(CommandType::BIND_VERTEX_BUFFERS);
41   auto& bindings = mCommands.back().bindVertexBuffers.vertexBufferBindings;
42   if(bindings.size() < firstBinding + buffers.size())
43   {
44     bindings.resize(firstBinding + buffers.size());
45     auto index = firstBinding;
46     for(auto& buf : buffers)
47     {
48       bindings[index].buffer = static_cast<const GLES::Buffer*>(buf);
49       bindings[index].offset = offsets[index - firstBinding];
50       index++;
51     }
52   }
53 }
54
55 void CommandBuffer::BindUniformBuffers(const std::vector<Graphics::UniformBufferBinding>& bindings)
56 {
57   mCommands.emplace_back(CommandType::BIND_UNIFORM_BUFFER);
58   auto& cmd     = mCommands.back();
59   auto& bindCmd = cmd.bindUniformBuffers;
60   for(const auto& binding : bindings)
61   {
62     if(binding.buffer)
63     {
64       auto glesBuffer = static_cast<const GLES::Buffer*>(binding.buffer);
65       if(glesBuffer->IsCPUAllocated()) // standalone uniforms
66       {
67         bindCmd.standaloneUniformsBufferBinding.buffer   = glesBuffer;
68         bindCmd.standaloneUniformsBufferBinding.offset   = binding.offset;
69         bindCmd.standaloneUniformsBufferBinding.binding  = binding.binding;
70         bindCmd.standaloneUniformsBufferBinding.emulated = true;
71       }
72       else // Bind regular UBO
73       {
74         // resize binding slots
75         if(binding.binding >= bindCmd.uniformBufferBindings.size())
76         {
77           bindCmd.uniformBufferBindings.resize(binding.binding + 1);
78         }
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;
84       }
85     }
86   }
87 }
88
89 void CommandBuffer::BindPipeline(const Graphics::Pipeline& pipeline)
90 {
91   mCommands.emplace_back(CommandType::BIND_PIPELINE);
92   mCommands.back().bindPipeline.pipeline = static_cast<const GLES::Pipeline*>(&pipeline);
93 }
94
95 void CommandBuffer::BindTextures(std::vector<TextureBinding>& textureBindings)
96 {
97   mCommands.emplace_back(CommandType::BIND_TEXTURES);
98   mCommands.back().bindTextures.textureBindings = std::move(textureBindings);
99 }
100
101 void CommandBuffer::BindSamplers(std::vector<SamplerBinding>& samplerBindings)
102 {
103   mCommands.emplace_back(CommandType::BIND_SAMPLERS);
104   mCommands.back().bindSamplers.samplerBindings = std::move(samplerBindings);
105 }
106
107 void CommandBuffer::BindPushConstants(void*    data,
108                                       uint32_t size,
109                                       uint32_t binding)
110 {
111 }
112
113 void CommandBuffer::BindIndexBuffer(const Graphics::Buffer& buffer,
114                                     uint32_t                offset,
115                                     Format                  format)
116 {
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;
121 }
122
123 void CommandBuffer::BeginRenderPass(
124   Graphics::RenderPass&   renderPass,
125   Graphics::RenderTarget& renderTarget,
126   Extent2D                renderArea,
127   std::vector<ClearValue> clearValues)
128 {
129 }
130
131 /**
132  * @brief Ends current render pass
133  *
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).
139  */
140 void CommandBuffer::EndRenderPass()
141 {
142 }
143
144 void CommandBuffer::Draw(
145   uint32_t vertexCount,
146   uint32_t instanceCount,
147   uint32_t firstVertex,
148   uint32_t firstInstance)
149 {
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;
157 }
158
159 void CommandBuffer::DrawIndexed(
160   uint32_t indexCount,
161   uint32_t instanceCount,
162   uint32_t firstIndex,
163   int32_t  vertexOffset,
164   uint32_t firstInstance)
165 {
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;
174 }
175
176 void CommandBuffer::DrawIndexedIndirect(
177   Graphics::Buffer& buffer,
178   uint32_t          offset,
179   uint32_t          drawCount,
180   uint32_t          stride)
181 {
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;
189 }
190
191 void CommandBuffer::Reset()
192 {
193   mCommands.clear();
194 }
195
196 void CommandBuffer::SetScissor(Graphics::Rect2D value)
197 {
198   mCommands.emplace_back(CommandType::SET_SCISSOR);
199   mCommands.back().scissor.region = value;
200 }
201
202 void CommandBuffer::SetScissorTestEnable(bool value)
203 {
204   mCommands.emplace_back(CommandType::SET_SCISSOR_TEST);
205   mCommands.back().scissorTest.enable = value;
206 }
207
208 void CommandBuffer::SetViewport(Viewport value)
209 {
210   mCommands.emplace_back(CommandType::SET_VIEWPORT);
211   mCommands.back().viewport.region = value;
212 }
213
214 void CommandBuffer::SetViewportEnable(bool value)
215 {
216   // There is no GL equivalent
217 }
218
219 [[nodiscard]] const std::vector<Command>& CommandBuffer::GetCommands() const
220 {
221   return mCommands;
222 }
223
224 void CommandBuffer::DestroyResource()
225 {
226   // Nothing to do
227 }
228
229 bool CommandBuffer::InitializeResource()
230 {
231   // Nothing to do
232   return true;
233 }
234
235 void CommandBuffer::DiscardResource()
236 {
237   GetController().DiscardResource(this);
238 }
239
240 } // namespace Dali::Graphics::GLES