GLES Texture and Buffer naive implementation
[platform/core/uifw/dali-adaptor.git] / dali / internal / graphics / gles-impl / gles-graphics-command-buffer.h
1 #ifndef DALI_GRAPHICS_GLES_COMMAND_BUFFER_H
2 #define DALI_GRAPHICS_GLES_COMMAND_BUFFER_H
3
4 /*
5  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/graphics-api/graphics-command-buffer.h>
23
24 // INTERNAL INCLUDES
25 #include "egl-graphics-controller.h"
26
27 namespace Dali
28 {
29 namespace Graphics
30 {
31 namespace GLES
32 {
33 class EglController;
34 class Texture;
35 class Buffer;
36 class Sampler;
37
38 enum class CommandType
39 {
40   FLUSH,
41   BIND_TEXTURES,
42   BIND_SAMPLERS,
43   BIND_VERTEX_BUFFERS,
44   BIND_INDEX_BUFFER
45 };
46
47 /**
48  * Command structure allocates memory to store a single command
49  */
50 struct Command
51 {
52   Command()
53   {
54   }
55
56   ~Command()
57   {
58   }
59
60   /**
61    * @brief Copy constructor
62    * @param[in] rhs Command
63    */
64   Command(const Command& rhs)
65   {
66     switch(rhs.type)
67     {
68       case CommandType::BIND_VERTEX_BUFFERS:
69       {
70         bindVertexBuffers = rhs.bindVertexBuffers;
71         break;
72       }
73       case CommandType::BIND_INDEX_BUFFER:
74       {
75         bindIndexBuffer = rhs.bindIndexBuffer;
76         break;
77       }
78       case CommandType::BIND_SAMPLERS:
79       {
80         bindSamplers = rhs.bindSamplers;
81         break;
82       }
83       case CommandType::BIND_TEXTURES:
84       {
85         bindTextures = rhs.bindTextures;
86         break;
87       }
88       case CommandType::FLUSH:
89       {
90         // Nothing to do
91         break;
92       }
93     }
94     type = rhs.type;
95   }
96
97   /**
98    * @brief Copy constructor
99    * @param[in] rhs Command
100    */
101   Command(Command&& rhs) noexcept
102   {
103     switch(rhs.type)
104     {
105       case CommandType::BIND_VERTEX_BUFFERS:
106       {
107         bindVertexBuffers = std::move(rhs.bindVertexBuffers);
108         break;
109       }
110       case CommandType::BIND_INDEX_BUFFER:
111       {
112         bindIndexBuffer = rhs.bindIndexBuffer;
113         break;
114       }
115       case CommandType::BIND_SAMPLERS:
116       {
117         bindSamplers = std::move(rhs.bindSamplers);
118         break;
119       }
120       case CommandType::BIND_TEXTURES:
121       {
122         bindTextures = std::move(rhs.bindTextures);
123         break;
124       }
125       case CommandType::FLUSH:
126       {
127         // Nothing to do
128         break;
129       }
130     }
131     type = rhs.type;
132   }
133
134   CommandType type{CommandType::FLUSH};
135
136   union
137   {
138     struct
139     {
140       std::vector<Graphics::TextureBinding> textureBindings;
141     } bindTextures{};
142
143     // BindSampler command
144     struct
145     {
146       std::vector<Graphics::SamplerBinding> samplerBindings;
147     } bindSamplers;
148
149     struct
150     {
151       struct Binding
152       {
153         const Graphics::Buffer* buffer{nullptr};
154         uint32_t                offset{0u};
155       };
156       std::vector<Binding> vertexBufferBindings;
157     } bindVertexBuffers;
158
159     struct
160     {
161       const Graphics::Buffer* buffer{nullptr};
162       uint32_t                offset{};
163       Graphics::Format        format{};
164     } bindIndexBuffer;
165   };
166 };
167
168 using CommandBufferResource = Resource<Graphics::CommandBuffer, Graphics::CommandBufferCreateInfo>;
169
170 class CommandBuffer : public CommandBufferResource
171 {
172 public:
173   CommandBuffer(const Graphics::CommandBufferCreateInfo& createInfo, EglGraphicsController& controller)
174   : CommandBufferResource(createInfo, controller)
175   {
176   }
177
178   void BindVertexBuffers(uint32_t                             firstBinding,
179                          std::vector<const Graphics::Buffer*> buffers,
180                          std::vector<uint32_t>                offsets) override
181   {
182     mCommands.emplace_back();
183     mCommands.back().type = CommandType::BIND_VERTEX_BUFFERS;
184     auto& bindings        = mCommands.back().bindVertexBuffers.vertexBufferBindings;
185     if(bindings.size() < firstBinding + buffers.size())
186     {
187       bindings.resize(firstBinding + buffers.size());
188       auto index = firstBinding;
189       for(auto& buf : buffers)
190       {
191         bindings[index].buffer = buf;
192         bindings[index].offset = offsets[index - firstBinding];
193         index++;
194       }
195     }
196   }
197
198   void BindUniformBuffers(const std::vector<Graphics::UniformBufferBinding>& bindings) override
199   {
200   }
201
202   void BindPipeline(const Graphics::Pipeline& pipeline) override
203   {
204   }
205
206   void BindTextures(std::vector<TextureBinding>& textureBindings) override
207   {
208     mCommands.emplace_back();
209     mCommands.back().type                         = CommandType::BIND_TEXTURES;
210     mCommands.back().bindTextures.textureBindings = std::move(textureBindings);
211   }
212
213   void BindSamplers(std::vector<SamplerBinding>& samplerBindings) override
214   {
215     mCommands.emplace_back();
216     mCommands.back().bindSamplers.samplerBindings = std::move(samplerBindings);
217   }
218
219   void BindPushConstants(void*    data,
220                          uint32_t size,
221                          uint32_t binding) override
222   {
223   }
224
225   void BindIndexBuffer(const Graphics::Buffer& buffer,
226                        uint32_t                offset,
227                        Format                  format) override
228   {
229     mCommands.emplace_back();
230     mCommands.back().type                   = CommandType::BIND_INDEX_BUFFER;
231     mCommands.back().bindIndexBuffer.buffer = &buffer;
232     mCommands.back().bindIndexBuffer.offset = offset;
233     mCommands.back().bindIndexBuffer.format = format;
234   }
235
236   void BeginRenderPass(
237     Graphics::RenderPass&   renderPass,
238     Graphics::RenderTarget& renderTarget,
239     Extent2D                renderArea,
240     std::vector<ClearValue> clearValues) override
241   {
242   }
243
244   /**
245    * @brief Ends current render pass
246    *
247    * This command must be issued in order to finalize the render pass.
248    * It's up to the implementation whether anything has to be done but
249    * the Controller may use end RP marker in order to resolve resource
250    * dependencies (for example, to know when target texture is ready
251    * before passing it to another render pass).
252    */
253   void EndRenderPass() override
254   {
255   }
256
257   void Draw(
258     uint32_t vertexCount,
259     uint32_t instanceCount,
260     uint32_t firstVertex,
261     uint32_t firstInstance) override
262   {
263   }
264
265   void DrawIndexed(
266     uint32_t indexCount,
267     uint32_t instanceCount,
268     uint32_t firstIndex,
269     int32_t  vertexOffset,
270     uint32_t firstInstance) override
271   {
272   }
273
274   void DrawIndexedIndirect(
275     Graphics::Buffer& buffer,
276     uint32_t          offset,
277     uint32_t          drawCount,
278     uint32_t          stride) override
279   {
280   }
281
282   void Reset(Graphics::CommandBuffer& commandBuffer) override
283   {
284     mCommands.clear();
285   }
286
287   void SetScissor(Extent2D value) override
288   {
289   }
290
291   void SetScissorTestEnable(bool value) override
292   {
293   }
294
295   void SetViewport(Viewport value) override
296   {
297   }
298
299   void SetViewportEnable(bool value) override
300   {
301   }
302
303   [[nodiscard]] const std::vector<Command>& GetCommands() const
304   {
305     return mCommands;
306   }
307
308   void DestroyResource() override
309   {
310     // Nothing to do
311   }
312
313   bool InitializeResource() override
314   {
315     // Nothing to do
316     return true;
317   }
318
319   void DiscardResource() override
320   {
321     // Nothing to do
322   }
323
324 private:
325   std::vector<Command> mCommands;
326 };
327 } // Namespace GLES
328 } // Namespace Graphics
329 } // Namespace Dali
330
331 #endif