GLES Pipeline and draw call support
[platform/core/uifw/dali-adaptor.git] / dali / internal / graphics / gles-impl / gles-context.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 #include "gles-context.h"
19 #include <dali/integration-api/gl-abstraction.h>
20 #include <dali/integration-api/gl-defines.h>
21 #include "gles-graphics-buffer.h"
22 #include "gles-graphics-command-buffer.h"
23 #include "gles-graphics-pipeline.h"
24
25 namespace Dali::Graphics::GLES
26 {
27 struct Context::Impl
28 {
29   Impl(EglGraphicsController& controller)
30   : mController(controller)
31   {
32   }
33
34   ~Impl() = default;
35
36   EglGraphicsController& mController;
37
38   const GLES::Pipeline* mCurrentPipeline{nullptr}; ///< Currently bound pipeline
39   const GLES::Pipeline* mNewPipeline{nullptr};     ///< New pipeline to be set on flush
40
41   std::vector<Graphics::TextureBinding> mCurrentTextureBindings{};
42   std::vector<Graphics::SamplerBinding> mCurrentSamplerBindings{};
43   GLES::IndexBufferBindingDescriptor    mCurrentIndexBufferBinding{};
44
45   struct VertexBufferBinding
46   {
47     GLES::Buffer* buffer{nullptr};
48     uint32_t      offset{0u};
49   };
50
51   // Currently bound buffers
52   std::vector<VertexBufferBindingDescriptor> mCurrentVertexBufferBindings{};
53 };
54
55 Context::Context(EglGraphicsController& controller)
56 {
57   mImpl = std::make_unique<Impl>(controller);
58 }
59
60 Context::~Context() = default;
61
62 void Context::Flush(bool reset, const GLES::DrawCallDescriptor& drawCall)
63 {
64   auto& gl = *mImpl->mController.GetGL();
65
66   // Change pipeline
67   if(mImpl->mNewPipeline)
68   {
69     // Execute states if different
70     mImpl->mCurrentPipeline = mImpl->mNewPipeline;
71     mImpl->mNewPipeline     = nullptr;
72   }
73
74   // Bind textures
75   for(const auto& binding : mImpl->mCurrentTextureBindings)
76   {
77     gl.ActiveTexture(GL_TEXTURE0 + binding.binding);
78     gl.BindTexture(GL_TEXTURE_2D,
79                    static_cast<const GLES::Texture*>(binding.texture)
80                      ->GetGLTexture());
81   }
82
83   // for each attribute bind vertices
84   const auto& pipelineState = mImpl->mCurrentPipeline->GetCreateInfo();
85   const auto& vi            = pipelineState.vertexInputState;
86   for(const auto& attr : vi->attributes)
87   {
88     // Enable location
89     gl.EnableVertexAttribArray(attr.location);
90     const auto& bufferSlot    = mImpl->mCurrentVertexBufferBindings[attr.binding];
91     const auto& bufferBinding = vi->bufferBindings[attr.binding];
92
93     auto glesBuffer = bufferSlot.buffer->GetGLBuffer();
94
95     // Bind buffer
96     gl.BindBuffer(GL_ARRAY_BUFFER, glesBuffer);
97     gl.VertexAttribPointer(attr.location,
98                            GLVertexFormat(attr.format).size,
99                            GLVertexFormat(attr.format).format,
100                            GL_FALSE,
101                            bufferBinding.stride,
102                            reinterpret_cast<void*>(attr.offset));
103   }
104
105   // Resolve topology
106   const auto& ia = mImpl->mCurrentPipeline->GetCreateInfo().inputAssemblyState;
107   // Resolve drawcall
108
109   switch(drawCall.type)
110   {
111     case DrawCallDescriptor::Type::DRAW:
112     {
113       gl.DrawArrays(GLESTopology(ia->topology),
114                     drawCall.draw.firstVertex,
115                     drawCall.draw.vertexCount);
116       break;
117     }
118     case DrawCallDescriptor::Type::DRAW_INDEXED:
119     {
120       const auto& binding    = mImpl->mCurrentIndexBufferBinding;
121       const auto* glesBuffer = binding.buffer;
122       gl.BindBuffer(GL_ELEMENT_ARRAY_BUFFER, glesBuffer->GetGLBuffer());
123       auto indexBufferFormat = GLIndexFormat(binding.format).format;
124       gl.DrawElements(GLESTopology(ia->topology),
125                       drawCall.drawIndexed.indexCount,
126                       indexBufferFormat,
127                       reinterpret_cast<void*>(binding.offset));
128       break;
129     }
130     case DrawCallDescriptor::Type::DRAW_INDEXED_INDIRECT:
131     {
132       break;
133     }
134   }
135 }
136
137 void Context::BindTextures(const std::vector<Graphics::TextureBinding>& bindings)
138 {
139   // for each texture allocate slot
140   for(const auto& binding : bindings)
141   {
142     // Resize binding array if needed
143     if(mImpl->mCurrentTextureBindings.size() <= binding.binding)
144     {
145       mImpl->mCurrentTextureBindings.resize(binding.binding + 1);
146     }
147     // Store the binding details
148     mImpl->mCurrentTextureBindings[binding.binding] = binding;
149   }
150 }
151
152 void Context::BindVertexBuffers(const std::vector<GLES::VertexBufferBindingDescriptor>& bindings)
153 {
154   if(bindings.size() > mImpl->mCurrentVertexBufferBindings.size())
155   {
156     mImpl->mCurrentVertexBufferBindings.resize(bindings.size());
157   }
158   // Copy only set slots
159   std::copy_if(bindings.begin(), bindings.end(), mImpl->mCurrentVertexBufferBindings.begin(), [](auto& item) {
160     return (nullptr != item.buffer);
161   });
162 }
163
164 void Context::BindIndexBuffer(const IndexBufferBindingDescriptor& indexBufferBinding)
165 {
166   mImpl->mCurrentIndexBufferBinding = indexBufferBinding;
167 }
168
169 void Context::BindPipeline(const GLES::Pipeline* newPipeline)
170 {
171   mImpl->mNewPipeline = newPipeline;
172 }
173
174 } // namespace Dali::Graphics::GLES