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 "egl-graphics-controller.h"
22 #include "gles-graphics-buffer.h"
23 #include "gles-graphics-pipeline.h"
24 #include "gles-graphics-program.h"
25 #include "gles-graphics-render-pass.h"
26 #include "gles-graphics-render-target.h"
28 namespace Dali::Graphics::GLES
32 Impl(EglGraphicsController& controller)
33 : mController(controller)
39 EglGraphicsController& mController;
41 const GLES::Pipeline* mCurrentPipeline{nullptr}; ///< Currently bound pipeline
42 const GLES::Pipeline* mNewPipeline{nullptr}; ///< New pipeline to be set on flush
44 std::vector<Graphics::TextureBinding> mCurrentTextureBindings{};
45 std::vector<Graphics::SamplerBinding> mCurrentSamplerBindings{};
46 GLES::IndexBufferBindingDescriptor mCurrentIndexBufferBinding{};
48 struct VertexBufferBinding
50 GLES::Buffer* buffer{nullptr};
54 // Currently bound buffers
55 std::vector<VertexBufferBindingDescriptor> mCurrentVertexBufferBindings{};
57 // Currently bound UBOs (check if it's needed per program!)
58 std::vector<UniformBufferBindingDescriptor> mCurrentUBOBindings{};
59 UniformBufferBindingDescriptor mCurrentStandaloneUBOBinding{};
61 // Current render pass and render target
62 const GLES::RenderTarget* mCurrentRenderTarget{nullptr};
63 const GLES::RenderPass* mCurrentRenderPass{nullptr};
66 Context::Context(EglGraphicsController& controller)
68 mImpl = std::make_unique<Impl>(controller);
71 Context::~Context() = default;
73 void Context::Flush(bool reset, const GLES::DrawCallDescriptor& drawCall)
75 auto& gl = *mImpl->mController.GetGL();
78 if(mImpl->mNewPipeline)
80 // Execute states if different
81 mImpl->mCurrentPipeline = mImpl->mNewPipeline;
82 mImpl->mNewPipeline = nullptr;
84 mImpl->mCurrentPipeline->GetPipeline().Bind(nullptr);
89 // Resolve rasterization state
90 ResolveRasterizationState();
92 // Resolve uniform buffers
93 ResolveUniformBuffers();
96 for(const auto& binding : mImpl->mCurrentTextureBindings)
98 auto texture = const_cast<GLES::Texture*>(static_cast<const GLES::Texture*>(binding.texture));
100 // Texture may not have been initialized yet...(tbm_surface timing issue?)
101 if(!texture->GetGLTexture())
103 // Attempt to reinitialize
104 // @todo need to put this somewhere else where it isn't const.
105 // Maybe post it back on end of initialize queue if initialization fails?
106 texture->InitializeResource();
109 texture->Bind(binding);
110 texture->Prepare(); // @todo also non-const.
113 // for each attribute bind vertices
114 const auto& pipelineState = mImpl->mCurrentPipeline->GetCreateInfo();
115 const auto& vi = pipelineState.vertexInputState;
116 for(const auto& attr : vi->attributes)
119 gl.EnableVertexAttribArray(attr.location);
120 const auto& bufferSlot = mImpl->mCurrentVertexBufferBindings[attr.binding];
121 const auto& bufferBinding = vi->bufferBindings[attr.binding];
123 auto glesBuffer = bufferSlot.buffer->GetGLBuffer();
126 gl.BindBuffer(GL_ARRAY_BUFFER, glesBuffer);
127 gl.VertexAttribPointer(attr.location,
128 GLVertexFormat(attr.format).size,
129 GLVertexFormat(attr.format).format,
131 bufferBinding.stride,
132 reinterpret_cast<void*>(attr.offset));
136 const auto& ia = mImpl->mCurrentPipeline->GetCreateInfo().inputAssemblyState;
141 switch(drawCall.type)
143 case DrawCallDescriptor::Type::DRAW:
145 gl.DrawArrays(GLESTopology(ia->topology),
146 drawCall.draw.firstVertex,
147 drawCall.draw.vertexCount);
150 case DrawCallDescriptor::Type::DRAW_INDEXED:
152 const auto& binding = mImpl->mCurrentIndexBufferBinding;
153 const auto* glesBuffer = binding.buffer;
154 gl.BindBuffer(GL_ELEMENT_ARRAY_BUFFER, glesBuffer->GetGLBuffer());
155 auto indexBufferFormat = GLIndexFormat(binding.format).format;
156 gl.DrawElements(GLESTopology(ia->topology),
157 drawCall.drawIndexed.indexCount,
159 reinterpret_cast<void*>(binding.offset));
162 case DrawCallDescriptor::Type::DRAW_INDEXED_INDIRECT:
171 void Context::BindTextures(const std::vector<Graphics::TextureBinding>& bindings)
173 // for each texture allocate slot
174 for(const auto& binding : bindings)
176 // Resize binding array if needed
177 if(mImpl->mCurrentTextureBindings.size() <= binding.binding)
179 mImpl->mCurrentTextureBindings.resize(binding.binding + 1);
181 // Store the binding details
182 mImpl->mCurrentTextureBindings[binding.binding] = binding;
186 void Context::BindVertexBuffers(const std::vector<GLES::VertexBufferBindingDescriptor>& bindings)
188 if(bindings.size() > mImpl->mCurrentVertexBufferBindings.size())
190 mImpl->mCurrentVertexBufferBindings.resize(bindings.size());
192 // Copy only set slots
193 std::copy_if(bindings.begin(), bindings.end(), mImpl->mCurrentVertexBufferBindings.begin(), [](auto& item) {
194 return (nullptr != item.buffer);
198 void Context::BindIndexBuffer(const IndexBufferBindingDescriptor& indexBufferBinding)
200 mImpl->mCurrentIndexBufferBinding = indexBufferBinding;
203 void Context::BindPipeline(const GLES::Pipeline* newPipeline)
205 mImpl->mNewPipeline = newPipeline;
208 void Context::BindUniformBuffers(const std::vector<UniformBufferBindingDescriptor>& uboBindings,
209 const UniformBufferBindingDescriptor& standaloneBindings)
211 if(standaloneBindings.buffer)
213 mImpl->mCurrentStandaloneUBOBinding = standaloneBindings;
216 if(uboBindings.size() >= mImpl->mCurrentUBOBindings.size())
218 mImpl->mCurrentUBOBindings.resize(uboBindings.size() + 1);
221 auto it = uboBindings.begin();
222 for(auto i = 0u; i < uboBindings.size(); ++i)
226 mImpl->mCurrentUBOBindings[i] = *it;
231 void Context::ResolveBlendState()
233 const auto& state = mImpl->mCurrentPipeline->GetCreateInfo();
234 const auto& bs = state.colorBlendState;
235 auto& gl = *mImpl->mController.GetGL();
237 // TODO: prevent leaking the state
243 bs->blendEnable ? gl.Enable(GL_BLEND) : gl.Disable(GL_BLEND);
249 gl.BlendFunc(GLBlendFunc(bs->srcColorBlendFactor), GLBlendFunc(bs->dstColorBlendFactor));
251 if((GLBlendFunc(bs->srcColorBlendFactor) == GLBlendFunc(bs->srcAlphaBlendFactor)) &&
252 (GLBlendFunc(bs->dstColorBlendFactor) == GLBlendFunc(bs->dstAlphaBlendFactor)))
254 gl.BlendFunc(GLBlendFunc(bs->srcColorBlendFactor), GLBlendFunc(bs->dstColorBlendFactor));
258 gl.BlendFuncSeparate(GLBlendFunc(bs->srcColorBlendFactor),
259 GLBlendFunc(bs->dstColorBlendFactor),
260 GLBlendFunc(bs->srcAlphaBlendFactor),
261 GLBlendFunc(bs->dstAlphaBlendFactor));
263 if(GLBlendOp(bs->colorBlendOp) == GLBlendOp(bs->alphaBlendOp))
265 gl.BlendEquation(GLBlendOp(bs->colorBlendOp));
269 gl.BlendEquationSeparate(GLBlendOp(bs->colorBlendOp), GLBlendOp(bs->alphaBlendOp));
273 void Context::ResolveRasterizationState()
275 const auto& state = mImpl->mCurrentPipeline->GetCreateInfo();
276 const auto& rs = state.rasterizationState;
277 auto& gl = *mImpl->mController.GetGL();
279 // TODO: prevent leaking the state
285 if(rs->cullMode == CullMode::NONE)
287 gl.Disable(GL_CULL_FACE);
291 gl.Enable(GL_CULL_FACE);
292 gl.CullFace(GLCullMode(rs->cullMode));
295 // TODO: implement polygon mode (fill, line, points)
296 // seems like we don't support it (no glPolygonMode())
299 void Context::ResolveUniformBuffers()
301 // Resolve standalone uniforms if we have binding
302 if(mImpl->mCurrentStandaloneUBOBinding.buffer)
304 ResolveStandaloneUniforms();
308 void Context::ResolveStandaloneUniforms()
310 auto& gl = *mImpl->mController.GetGL();
312 // Find reflection for program
313 const auto program = static_cast<const GLES::Program*>(mImpl->mCurrentPipeline->GetCreateInfo().programState->program);
315 const auto& reflection = program->GetReflection();
317 auto extraInfos = reflection.GetStandaloneUniformExtraInfo();
319 const auto ptr = reinterpret_cast<const char*>(mImpl->mCurrentStandaloneUBOBinding.buffer->GetCPUAllocatedAddress());
321 for(const auto& info : extraInfos)
323 auto type = GLTypeConversion(info.type).type;
324 auto offset = info.offset;
327 case GLType::FLOAT_VEC2:
329 gl.Uniform2fv(info.location, info.arraySize, reinterpret_cast<const float*>(&ptr[offset]));
332 case GLType::FLOAT_VEC3:
334 gl.Uniform3fv(info.location, info.arraySize, reinterpret_cast<const float*>(&ptr[offset]));
337 case GLType::FLOAT_VEC4:
339 gl.Uniform4fv(info.location, info.arraySize, reinterpret_cast<const float*>(&ptr[offset]));
342 case GLType::INT_VEC2:
344 gl.Uniform2iv(info.location, info.arraySize, reinterpret_cast<const GLint*>(&ptr[offset]));
347 case GLType::INT_VEC3:
349 gl.Uniform3iv(info.location, info.arraySize, reinterpret_cast<const GLint*>(&ptr[offset]));
352 case GLType::INT_VEC4:
354 gl.Uniform4iv(info.location, info.arraySize, reinterpret_cast<const GLint*>(&ptr[offset]));
359 // not supported by DALi
362 case GLType::BOOL_VEC2:
364 // not supported by DALi
367 case GLType::BOOL_VEC3:
369 // not supported by DALi
372 case GLType::BOOL_VEC4:
374 // not supported by DALi
379 gl.Uniform1fv(info.location, info.arraySize, reinterpret_cast<const float*>(&ptr[offset]));
382 case GLType::FLOAT_MAT2:
384 gl.UniformMatrix2fv(info.location, info.arraySize, GL_FALSE, reinterpret_cast<const float*>(&ptr[offset]));
387 case GLType::FLOAT_MAT3:
389 gl.UniformMatrix3fv(info.location, info.arraySize, GL_FALSE, reinterpret_cast<const float*>(&ptr[offset]));
392 case GLType::FLOAT_MAT4:
394 gl.UniformMatrix4fv(info.location, info.arraySize, GL_FALSE, reinterpret_cast<const float*>(&ptr[offset]));
397 case GLType::SAMPLER_2D:
401 case GLType::SAMPLER_CUBE:
412 void Context::BeginRenderPass(const BeginRenderPassDescriptor& renderPassBegin)
414 auto& renderPass = *renderPassBegin.renderPass;
415 auto& renderTarget = *renderPassBegin.renderTarget;
417 if(mImpl->mCurrentRenderPass == &renderPass &&
418 mImpl->mCurrentRenderTarget == &renderTarget)
422 const auto& passInfo = renderPass.GetCreateInfo();
423 const auto& targetInfo = renderTarget.GetCreateInfo();
425 auto& gl = *mImpl->mController.GetGL();
427 if(targetInfo.surface)
429 // switch context to surface bound
432 gl.BindFramebuffer(GL_FRAMEBUFFER, 0);
435 if(targetInfo.framebuffer)
437 // bind framebuffer and swap (if needed to shared context)
438 renderTarget.GetFramebuffer()->Bind();
441 // clear (ideally cache the setup)
443 // In GL we assume that the last attachment is depth/stencil (we may need
444 // to cache extra information inside GLES RenderTarget if we want to be
445 // more specific in case of MRT)
447 // For GLES2.0 we clear only a single color attachment
448 if(mImpl->mController.GetGLESVersion() == GLESVersion::GLES_20)
450 const auto& attachments = *renderPass.GetCreateInfo().attachments;
451 const auto& color0 = attachments[0];
453 if(color0.loadOp == AttachmentLoadOp::CLEAR)
455 mask |= GL_COLOR_BUFFER_BIT;
457 // Set clear color (todo: cache it!)
458 // Something goes wrong here if Alpha mask is GL_TRUE
459 gl.ColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_FALSE);
460 gl.ClearColor(renderPassBegin.clearValues[0].color.r,
461 renderPassBegin.clearValues[0].color.g,
462 renderPassBegin.clearValues[0].color.b,
463 renderPassBegin.clearValues[0].color.a);
466 // check for depth stencil
467 if(attachments.size() > 1)
469 const auto& depthStencil = attachments.back();
470 if(depthStencil.loadOp == AttachmentLoadOp::CLEAR)
472 mask |= GL_DEPTH_BUFFER_BIT;
474 if(depthStencil.stencilLoadOp == AttachmentLoadOp::CLEAR)
476 mask |= GL_STENCIL_BUFFER_BIT;
483 mImpl->mCurrentRenderPass = &renderPass;
484 mImpl->mCurrentRenderTarget = &renderTarget;
487 void Context::EndRenderPass()
491 void Context::ClearState()
493 mImpl->mCurrentTextureBindings.clear();
496 } // namespace Dali::Graphics::GLES