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.
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 #include "gles-graphics-program.h"
26 namespace Dali::Graphics::GLES
30 Impl(EglGraphicsController& controller)
31 : mController(controller)
37 EglGraphicsController& mController;
39 const GLES::Pipeline* mCurrentPipeline{nullptr}; ///< Currently bound pipeline
40 const GLES::Pipeline* mNewPipeline{nullptr}; ///< New pipeline to be set on flush
42 std::vector<Graphics::TextureBinding> mCurrentTextureBindings{};
43 std::vector<Graphics::SamplerBinding> mCurrentSamplerBindings{};
44 GLES::IndexBufferBindingDescriptor mCurrentIndexBufferBinding{};
46 struct VertexBufferBinding
48 GLES::Buffer* buffer{nullptr};
52 // Currently bound buffers
53 std::vector<VertexBufferBindingDescriptor> mCurrentVertexBufferBindings{};
55 // Currently bound UBOs (check if it's needed per program!)
56 std::vector<UniformBufferBindingDescriptor> mCurrentUBOBindings{};
57 UniformBufferBindingDescriptor mCurrentStandaloneUBOBinding{};
60 Context::Context(EglGraphicsController& controller)
62 mImpl = std::make_unique<Impl>(controller);
65 Context::~Context() = default;
67 void Context::Flush(bool reset, const GLES::DrawCallDescriptor& drawCall)
69 auto& gl = *mImpl->mController.GetGL();
72 if(mImpl->mNewPipeline)
74 // Execute states if different
75 mImpl->mCurrentPipeline = mImpl->mNewPipeline;
76 mImpl->mNewPipeline = nullptr;
78 mImpl->mCurrentPipeline->GetPipeline().Bind(nullptr);
83 // Resolve rasterization state
84 ResolveRasterizationState();
86 // Resolve uniform buffers
87 ResolveUniformBuffers();
90 for(const auto& binding : mImpl->mCurrentTextureBindings)
92 auto texture = const_cast<GLES::Texture*>(static_cast<const GLES::Texture*>(binding.texture));
94 // Texture may not have been initialized yet...(tbm_surface timing issue?)
95 if(!texture->GetGLTexture())
97 // Attempt to reinitialize
98 // @todo need to put this somewhere else where it isn't const.
99 // Maybe post it back on end of initialize queue if initialization fails?
100 texture->InitializeResource();
103 texture->Bind(binding);
104 texture->Prepare(); // @todo also non-const.
107 // for each attribute bind vertices
108 const auto& pipelineState = mImpl->mCurrentPipeline->GetCreateInfo();
109 const auto& vi = pipelineState.vertexInputState;
110 for(const auto& attr : vi->attributes)
113 gl.EnableVertexAttribArray(attr.location);
114 const auto& bufferSlot = mImpl->mCurrentVertexBufferBindings[attr.binding];
115 const auto& bufferBinding = vi->bufferBindings[attr.binding];
117 auto glesBuffer = bufferSlot.buffer->GetGLBuffer();
120 gl.BindBuffer(GL_ARRAY_BUFFER, glesBuffer);
121 gl.VertexAttribPointer(attr.location,
122 GLVertexFormat(attr.format).size,
123 GLVertexFormat(attr.format).format,
125 bufferBinding.stride,
126 reinterpret_cast<void*>(attr.offset));
130 const auto& ia = mImpl->mCurrentPipeline->GetCreateInfo().inputAssemblyState;
135 switch(drawCall.type)
137 case DrawCallDescriptor::Type::DRAW:
139 gl.DrawArrays(GLESTopology(ia->topology),
140 drawCall.draw.firstVertex,
141 drawCall.draw.vertexCount);
144 case DrawCallDescriptor::Type::DRAW_INDEXED:
146 const auto& binding = mImpl->mCurrentIndexBufferBinding;
147 const auto* glesBuffer = binding.buffer;
148 gl.BindBuffer(GL_ELEMENT_ARRAY_BUFFER, glesBuffer->GetGLBuffer());
149 auto indexBufferFormat = GLIndexFormat(binding.format).format;
150 gl.DrawElements(GLESTopology(ia->topology),
151 drawCall.drawIndexed.indexCount,
153 reinterpret_cast<void*>(binding.offset));
156 case DrawCallDescriptor::Type::DRAW_INDEXED_INDIRECT:
163 void Context::BindTextures(const std::vector<Graphics::TextureBinding>& bindings)
165 // for each texture allocate slot
166 for(const auto& binding : bindings)
168 // Resize binding array if needed
169 if(mImpl->mCurrentTextureBindings.size() <= binding.binding)
171 mImpl->mCurrentTextureBindings.resize(binding.binding + 1);
173 // Store the binding details
174 mImpl->mCurrentTextureBindings[binding.binding] = binding;
178 void Context::BindVertexBuffers(const std::vector<GLES::VertexBufferBindingDescriptor>& bindings)
180 if(bindings.size() > mImpl->mCurrentVertexBufferBindings.size())
182 mImpl->mCurrentVertexBufferBindings.resize(bindings.size());
184 // Copy only set slots
185 std::copy_if(bindings.begin(), bindings.end(), mImpl->mCurrentVertexBufferBindings.begin(), [](auto& item) {
186 return (nullptr != item.buffer);
190 void Context::BindIndexBuffer(const IndexBufferBindingDescriptor& indexBufferBinding)
192 mImpl->mCurrentIndexBufferBinding = indexBufferBinding;
195 void Context::BindPipeline(const GLES::Pipeline* newPipeline)
197 mImpl->mNewPipeline = newPipeline;
200 void Context::BindUniformBuffers(const std::vector<UniformBufferBindingDescriptor>& uboBindings,
201 const UniformBufferBindingDescriptor& standaloneBindings)
203 if(standaloneBindings.buffer)
205 mImpl->mCurrentStandaloneUBOBinding = standaloneBindings;
208 if(uboBindings.size() >= mImpl->mCurrentUBOBindings.size())
210 mImpl->mCurrentUBOBindings.resize(uboBindings.size() + 1);
213 auto it = uboBindings.begin();
214 for(auto i = 0u; i < uboBindings.size(); ++i)
218 mImpl->mCurrentUBOBindings[i] = *it;
223 void Context::ResolveBlendState()
225 const auto& state = mImpl->mCurrentPipeline->GetCreateInfo();
226 const auto& bs = state.colorBlendState;
227 auto& gl = *mImpl->mController.GetGL();
229 // TODO: prevent leaking the state
235 bs->blendEnable ? gl.Enable(GL_BLEND) : gl.Disable(GL_BLEND);
241 gl.BlendFunc(GLBlendFunc(bs->srcColorBlendFactor), GLBlendFunc(bs->dstColorBlendFactor));
243 if((GLBlendFunc(bs->srcColorBlendFactor) == GLBlendFunc(bs->srcAlphaBlendFactor)) &&
244 (GLBlendFunc(bs->dstColorBlendFactor) == GLBlendFunc(bs->dstAlphaBlendFactor)))
246 gl.BlendFunc(GLBlendFunc(bs->srcColorBlendFactor), GLBlendFunc(bs->dstColorBlendFactor));
250 gl.BlendFuncSeparate(GLBlendFunc(bs->srcColorBlendFactor),
251 GLBlendFunc(bs->dstColorBlendFactor),
252 GLBlendFunc(bs->srcAlphaBlendFactor),
253 GLBlendFunc(bs->dstAlphaBlendFactor));
255 if(GLBlendOp(bs->colorBlendOp) == GLBlendOp(bs->alphaBlendOp))
257 gl.BlendEquation(GLBlendOp(bs->colorBlendOp));
261 gl.BlendEquationSeparate(GLBlendOp(bs->colorBlendOp), GLBlendOp(bs->alphaBlendOp));
265 void Context::ResolveRasterizationState()
267 const auto& state = mImpl->mCurrentPipeline->GetCreateInfo();
268 const auto& rs = state.rasterizationState;
269 auto& gl = *mImpl->mController.GetGL();
271 // TODO: prevent leaking the state
277 if(rs->cullMode == CullMode::NONE)
279 gl.Disable(GL_CULL_FACE);
283 gl.Enable(GL_CULL_FACE);
284 gl.CullFace(GLCullMode(rs->cullMode));
287 // TODO: implement polygon mode (fill, line, points)
288 // seems like we don't support it (no glPolygonMode())
291 void Context::ResolveUniformBuffers()
293 // Resolve standalone uniforms if we have binding
294 if(mImpl->mCurrentStandaloneUBOBinding.buffer)
296 ResolveStandaloneUniforms();
300 void Context::ResolveStandaloneUniforms()
302 auto& gl = *mImpl->mController.GetGL();
304 // Find reflection for program
305 const auto program = static_cast<const GLES::Program*>(mImpl->mCurrentPipeline->GetCreateInfo().programState->program);
307 const auto& reflection = program->GetReflection();
309 auto extraInfos = reflection.GetStandaloneUniformExtraInfo();
311 const auto ptr = reinterpret_cast<const char*>(mImpl->mCurrentStandaloneUBOBinding.buffer->GetCPUAllocatedAddress());
313 for(const auto& info : extraInfos)
315 auto type = GLTypeConversion(info.type).type;
316 auto offset = info.offset;
319 case GLType::FLOAT_VEC2:
321 gl.Uniform2fv(info.location, info.arraySize, reinterpret_cast<const float*>(&ptr[offset]));
324 case GLType::FLOAT_VEC3:
326 gl.Uniform3fv(info.location, info.arraySize, reinterpret_cast<const float*>(&ptr[offset]));
329 case GLType::FLOAT_VEC4:
331 gl.Uniform4fv(info.location, info.arraySize, reinterpret_cast<const float*>(&ptr[offset]));
334 case GLType::INT_VEC2:
336 gl.Uniform2iv(info.location, info.arraySize, reinterpret_cast<const GLint*>(&ptr[offset]));
339 case GLType::INT_VEC3:
341 gl.Uniform3iv(info.location, info.arraySize, reinterpret_cast<const GLint*>(&ptr[offset]));
344 case GLType::INT_VEC4:
346 gl.Uniform4iv(info.location, info.arraySize, reinterpret_cast<const GLint*>(&ptr[offset]));
351 // not supported by DALi
354 case GLType::BOOL_VEC2:
356 // not supported by DALi
359 case GLType::BOOL_VEC3:
361 // not supported by DALi
364 case GLType::BOOL_VEC4:
366 // not supported by DALi
371 gl.Uniform1fv(info.location, info.arraySize, reinterpret_cast<const float*>(&ptr[offset]));
374 case GLType::FLOAT_MAT2:
376 gl.UniformMatrix2fv(info.location, info.arraySize, GL_FALSE, reinterpret_cast<const float*>(&ptr[offset]));
379 case GLType::FLOAT_MAT3:
381 gl.UniformMatrix3fv(info.location, info.arraySize, GL_FALSE, reinterpret_cast<const float*>(&ptr[offset]));
384 case GLType::FLOAT_MAT4:
386 gl.UniformMatrix4fv(info.location, info.arraySize, GL_FALSE, reinterpret_cast<const float*>(&ptr[offset]));
389 case GLType::SAMPLER_2D:
393 case GLType::SAMPLER_CUBE:
404 } // namespace Dali::Graphics::GLES