Added CullFace and blending modes 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   // Blend state
75   ResolveBlendState();
76
77   // Resolve rasterization state
78   ResolveRasterizationState();
79
80   // Bind textures
81   for(const auto& binding : mImpl->mCurrentTextureBindings)
82   {
83     gl.ActiveTexture(GL_TEXTURE0 + binding.binding);
84     gl.BindTexture(GL_TEXTURE_2D,
85                    static_cast<const GLES::Texture*>(binding.texture)
86                      ->GetGLTexture());
87   }
88
89   // for each attribute bind vertices
90   const auto& pipelineState = mImpl->mCurrentPipeline->GetCreateInfo();
91   const auto& vi            = pipelineState.vertexInputState;
92   for(const auto& attr : vi->attributes)
93   {
94     // Enable location
95     gl.EnableVertexAttribArray(attr.location);
96     const auto& bufferSlot    = mImpl->mCurrentVertexBufferBindings[attr.binding];
97     const auto& bufferBinding = vi->bufferBindings[attr.binding];
98
99     auto glesBuffer = bufferSlot.buffer->GetGLBuffer();
100
101     // Bind buffer
102     gl.BindBuffer(GL_ARRAY_BUFFER, glesBuffer);
103     gl.VertexAttribPointer(attr.location,
104                            GLVertexFormat(attr.format).size,
105                            GLVertexFormat(attr.format).format,
106                            GL_FALSE,
107                            bufferBinding.stride,
108                            reinterpret_cast<void*>(attr.offset));
109   }
110
111   // Resolve topology
112   const auto& ia = mImpl->mCurrentPipeline->GetCreateInfo().inputAssemblyState;
113
114   // Resolve draw call
115   switch(drawCall.type)
116   {
117     case DrawCallDescriptor::Type::DRAW:
118     {
119       gl.DrawArrays(GLESTopology(ia->topology),
120                     drawCall.draw.firstVertex,
121                     drawCall.draw.vertexCount);
122       break;
123     }
124     case DrawCallDescriptor::Type::DRAW_INDEXED:
125     {
126       const auto& binding    = mImpl->mCurrentIndexBufferBinding;
127       const auto* glesBuffer = binding.buffer;
128       gl.BindBuffer(GL_ELEMENT_ARRAY_BUFFER, glesBuffer->GetGLBuffer());
129       auto indexBufferFormat = GLIndexFormat(binding.format).format;
130       gl.DrawElements(GLESTopology(ia->topology),
131                       drawCall.drawIndexed.indexCount,
132                       indexBufferFormat,
133                       reinterpret_cast<void*>(binding.offset));
134       break;
135     }
136     case DrawCallDescriptor::Type::DRAW_INDEXED_INDIRECT:
137     {
138       break;
139     }
140   }
141 }
142
143 void Context::BindTextures(const std::vector<Graphics::TextureBinding>& bindings)
144 {
145   // for each texture allocate slot
146   for(const auto& binding : bindings)
147   {
148     // Resize binding array if needed
149     if(mImpl->mCurrentTextureBindings.size() <= binding.binding)
150     {
151       mImpl->mCurrentTextureBindings.resize(binding.binding + 1);
152     }
153     // Store the binding details
154     mImpl->mCurrentTextureBindings[binding.binding] = binding;
155   }
156 }
157
158 void Context::BindVertexBuffers(const std::vector<GLES::VertexBufferBindingDescriptor>& bindings)
159 {
160   if(bindings.size() > mImpl->mCurrentVertexBufferBindings.size())
161   {
162     mImpl->mCurrentVertexBufferBindings.resize(bindings.size());
163   }
164   // Copy only set slots
165   std::copy_if(bindings.begin(), bindings.end(), mImpl->mCurrentVertexBufferBindings.begin(), [](auto& item) {
166     return (nullptr != item.buffer);
167   });
168 }
169
170 void Context::BindIndexBuffer(const IndexBufferBindingDescriptor& indexBufferBinding)
171 {
172   mImpl->mCurrentIndexBufferBinding = indexBufferBinding;
173 }
174
175 void Context::BindPipeline(const GLES::Pipeline* newPipeline)
176 {
177   mImpl->mNewPipeline = newPipeline;
178 }
179
180 void Context::ResolveBlendState()
181 {
182   const auto& state = mImpl->mCurrentPipeline->GetCreateInfo();
183   const auto& bs    = state.colorBlendState;
184   auto&       gl    = *mImpl->mController.GetGL();
185
186   // TODO: prevent leaking the state
187   if(!bs)
188   {
189     return;
190   }
191
192   bs->blendEnable ? gl.Enable(GL_BLEND) : gl.Disable(GL_BLEND);
193   if(!bs->blendEnable)
194   {
195     return;
196   }
197
198   gl.BlendFunc(GLBlendFunc(bs->srcColorBlendFactor), GLBlendFunc(bs->dstColorBlendFactor));
199
200   if((GLBlendFunc(bs->srcColorBlendFactor) == GLBlendFunc(bs->srcAlphaBlendFactor)) &&
201      (GLBlendFunc(bs->dstColorBlendFactor) == GLBlendFunc(bs->dstAlphaBlendFactor)))
202   {
203     gl.BlendFunc(GLBlendFunc(bs->srcColorBlendFactor), GLBlendFunc(bs->dstColorBlendFactor));
204   }
205   else
206   {
207     gl.BlendFuncSeparate(GLBlendFunc(bs->srcColorBlendFactor),
208                          GLBlendFunc(bs->dstColorBlendFactor),
209                          GLBlendFunc(bs->srcAlphaBlendFactor),
210                          GLBlendFunc(bs->dstAlphaBlendFactor));
211   }
212   if(GLBlendOp(bs->colorBlendOp) == GLBlendOp(bs->alphaBlendOp))
213   {
214     gl.BlendEquation(GLBlendOp(bs->colorBlendOp));
215   }
216   else
217   {
218     gl.BlendEquationSeparate(GLBlendOp(bs->colorBlendOp), GLBlendOp(bs->alphaBlendOp));
219   }
220 }
221
222 void Context::ResolveRasterizationState()
223 {
224   const auto& state = mImpl->mCurrentPipeline->GetCreateInfo();
225   const auto& rs    = state.rasterizationState;
226   auto&       gl    = *mImpl->mController.GetGL();
227
228   // TODO: prevent leaking the state
229   if(!rs)
230   {
231     return;
232   }
233
234   if(rs->cullMode == CullMode::NONE)
235   {
236     gl.Disable(GL_CULL_FACE);
237   }
238   else
239   {
240     gl.Enable(GL_CULL_FACE);
241     gl.CullFace(GLCullMode(rs->cullMode));
242   }
243
244   // TODO: implement polygon mode (fill, line, points)
245   //       seems like we don't support it (no glPolygonMode())
246 }
247
248 } // namespace Dali::Graphics::GLES