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/adaptor-framework/render-surface-interface.h>
20 #include <dali/integration-api/gl-abstraction.h>
21 #include <dali/integration-api/gl-defines.h>
22 #include <dali/internal/graphics/common/graphics-interface.h>
24 #include "egl-graphics-controller.h"
25 #include "gles-graphics-buffer.h"
26 #include "gles-graphics-pipeline.h"
27 #include "gles-graphics-program.h"
28 #include "gles-graphics-render-pass.h"
29 #include "gles-graphics-render-target.h"
31 namespace Dali::Graphics::GLES
35 Impl(EglGraphicsController& controller)
36 : mController(controller)
42 EglGraphicsController& mController;
44 const GLES::Pipeline* mCurrentPipeline{nullptr}; ///< Currently bound pipeline
45 const GLES::Pipeline* mNewPipeline{nullptr}; ///< New pipeline to be set on flush
47 std::vector<Graphics::TextureBinding> mCurrentTextureBindings{};
48 std::vector<Graphics::SamplerBinding> mCurrentSamplerBindings{};
49 GLES::IndexBufferBindingDescriptor mCurrentIndexBufferBinding{};
51 struct VertexBufferBinding
53 GLES::Buffer* buffer{nullptr};
57 // Currently bound buffers
58 std::vector<VertexBufferBindingDescriptor> mCurrentVertexBufferBindings{};
60 // Currently bound UBOs (check if it's needed per program!)
61 std::vector<UniformBufferBindingDescriptor> mCurrentUBOBindings{};
62 UniformBufferBindingDescriptor mCurrentStandaloneUBOBinding{};
64 // Current render pass and render target
65 const GLES::RenderTarget* mCurrentRenderTarget{nullptr};
66 const GLES::RenderPass* mCurrentRenderPass{nullptr};
69 Context::Context(EglGraphicsController& controller)
71 mImpl = std::make_unique<Impl>(controller);
74 Context::~Context() = default;
76 void Context::Flush(bool reset, const GLES::DrawCallDescriptor& drawCall)
78 auto& gl = *mImpl->mController.GetGL();
81 if(mImpl->mNewPipeline)
83 // Execute states if different
84 mImpl->mCurrentPipeline = mImpl->mNewPipeline;
85 mImpl->mNewPipeline = nullptr;
87 mImpl->mCurrentPipeline->GetPipeline().Bind(nullptr);
92 // Resolve rasterization state
93 ResolveRasterizationState();
95 // Resolve uniform buffers
96 ResolveUniformBuffers();
99 for(const auto& binding : mImpl->mCurrentTextureBindings)
101 auto texture = const_cast<GLES::Texture*>(static_cast<const GLES::Texture*>(binding.texture));
103 // Texture may not have been initialized yet...(tbm_surface timing issue?)
104 if(!texture->GetGLTexture())
106 // Attempt to reinitialize
107 // @todo need to put this somewhere else where it isn't const.
108 // Maybe post it back on end of initialize queue if initialization fails?
109 texture->InitializeResource();
112 texture->Bind(binding);
113 texture->Prepare(); // @todo also non-const.
116 // for each attribute bind vertices
117 const auto& pipelineState = mImpl->mCurrentPipeline->GetCreateInfo();
118 const auto& vi = pipelineState.vertexInputState;
119 for(const auto& attr : vi->attributes)
122 gl.EnableVertexAttribArray(attr.location);
123 const auto& bufferSlot = mImpl->mCurrentVertexBufferBindings[attr.binding];
124 const auto& bufferBinding = vi->bufferBindings[attr.binding];
126 auto glesBuffer = bufferSlot.buffer->GetGLBuffer();
129 gl.BindBuffer(GL_ARRAY_BUFFER, glesBuffer);
130 gl.VertexAttribPointer(attr.location,
131 GLVertexFormat(attr.format).size,
132 GLVertexFormat(attr.format).format,
134 bufferBinding.stride,
135 reinterpret_cast<void*>(attr.offset));
139 const auto& ia = mImpl->mCurrentPipeline->GetCreateInfo().inputAssemblyState;
144 switch(drawCall.type)
146 case DrawCallDescriptor::Type::DRAW:
148 gl.DrawArrays(GLESTopology(ia->topology),
149 drawCall.draw.firstVertex,
150 drawCall.draw.vertexCount);
153 case DrawCallDescriptor::Type::DRAW_INDEXED:
155 const auto& binding = mImpl->mCurrentIndexBufferBinding;
156 const auto* glesBuffer = binding.buffer;
157 gl.BindBuffer(GL_ELEMENT_ARRAY_BUFFER, glesBuffer->GetGLBuffer());
158 auto indexBufferFormat = GLIndexFormat(binding.format).format;
159 gl.DrawElements(GLESTopology(ia->topology),
160 drawCall.drawIndexed.indexCount,
162 reinterpret_cast<void*>(binding.offset));
165 case DrawCallDescriptor::Type::DRAW_INDEXED_INDIRECT:
174 void Context::BindTextures(const std::vector<Graphics::TextureBinding>& bindings)
176 // for each texture allocate slot
177 for(const auto& binding : bindings)
179 // Resize binding array if needed
180 if(mImpl->mCurrentTextureBindings.size() <= binding.binding)
182 mImpl->mCurrentTextureBindings.resize(binding.binding + 1);
184 // Store the binding details
185 mImpl->mCurrentTextureBindings[binding.binding] = binding;
189 void Context::BindVertexBuffers(const std::vector<GLES::VertexBufferBindingDescriptor>& bindings)
191 if(bindings.size() > mImpl->mCurrentVertexBufferBindings.size())
193 mImpl->mCurrentVertexBufferBindings.resize(bindings.size());
195 // Copy only set slots
196 std::copy_if(bindings.begin(), bindings.end(), mImpl->mCurrentVertexBufferBindings.begin(), [](auto& item) {
197 return (nullptr != item.buffer);
201 void Context::BindIndexBuffer(const IndexBufferBindingDescriptor& indexBufferBinding)
203 mImpl->mCurrentIndexBufferBinding = indexBufferBinding;
206 void Context::BindPipeline(const GLES::Pipeline* newPipeline)
208 mImpl->mNewPipeline = newPipeline;
211 void Context::BindUniformBuffers(const std::vector<UniformBufferBindingDescriptor>& uboBindings,
212 const UniformBufferBindingDescriptor& standaloneBindings)
214 if(standaloneBindings.buffer)
216 mImpl->mCurrentStandaloneUBOBinding = standaloneBindings;
219 if(uboBindings.size() >= mImpl->mCurrentUBOBindings.size())
221 mImpl->mCurrentUBOBindings.resize(uboBindings.size() + 1);
224 auto it = uboBindings.begin();
225 for(auto i = 0u; i < uboBindings.size(); ++i)
229 mImpl->mCurrentUBOBindings[i] = *it;
234 void Context::ResolveBlendState()
236 const auto& state = mImpl->mCurrentPipeline->GetCreateInfo();
237 const auto& bs = state.colorBlendState;
238 auto& gl = *mImpl->mController.GetGL();
240 // TODO: prevent leaking the state
246 bs->blendEnable ? gl.Enable(GL_BLEND) : gl.Disable(GL_BLEND);
252 gl.BlendFunc(GLBlendFunc(bs->srcColorBlendFactor), GLBlendFunc(bs->dstColorBlendFactor));
254 if((GLBlendFunc(bs->srcColorBlendFactor) == GLBlendFunc(bs->srcAlphaBlendFactor)) &&
255 (GLBlendFunc(bs->dstColorBlendFactor) == GLBlendFunc(bs->dstAlphaBlendFactor)))
257 gl.BlendFunc(GLBlendFunc(bs->srcColorBlendFactor), GLBlendFunc(bs->dstColorBlendFactor));
261 gl.BlendFuncSeparate(GLBlendFunc(bs->srcColorBlendFactor),
262 GLBlendFunc(bs->dstColorBlendFactor),
263 GLBlendFunc(bs->srcAlphaBlendFactor),
264 GLBlendFunc(bs->dstAlphaBlendFactor));
266 if(GLBlendOp(bs->colorBlendOp) == GLBlendOp(bs->alphaBlendOp))
268 gl.BlendEquation(GLBlendOp(bs->colorBlendOp));
272 gl.BlendEquationSeparate(GLBlendOp(bs->colorBlendOp), GLBlendOp(bs->alphaBlendOp));
276 void Context::ResolveRasterizationState()
278 const auto& state = mImpl->mCurrentPipeline->GetCreateInfo();
279 const auto& rs = state.rasterizationState;
280 auto& gl = *mImpl->mController.GetGL();
282 // TODO: prevent leaking the state
288 if(rs->cullMode == CullMode::NONE)
290 gl.Disable(GL_CULL_FACE);
294 gl.Enable(GL_CULL_FACE);
295 gl.CullFace(GLCullMode(rs->cullMode));
298 // TODO: implement polygon mode (fill, line, points)
299 // seems like we don't support it (no glPolygonMode())
302 void Context::ResolveUniformBuffers()
304 // Resolve standalone uniforms if we have binding
305 if(mImpl->mCurrentStandaloneUBOBinding.buffer)
307 ResolveStandaloneUniforms();
311 void Context::ResolveStandaloneUniforms()
313 auto& gl = *mImpl->mController.GetGL();
315 // Find reflection for program
316 const auto program = static_cast<const GLES::Program*>(mImpl->mCurrentPipeline->GetCreateInfo().programState->program);
318 const auto& reflection = program->GetReflection();
320 auto extraInfos = reflection.GetStandaloneUniformExtraInfo();
322 const auto ptr = reinterpret_cast<const char*>(mImpl->mCurrentStandaloneUBOBinding.buffer->GetCPUAllocatedAddress());
324 for(const auto& info : extraInfos)
326 auto type = GLTypeConversion(info.type).type;
327 auto offset = info.offset;
330 case GLType::FLOAT_VEC2:
332 gl.Uniform2fv(info.location, info.arraySize, reinterpret_cast<const float*>(&ptr[offset]));
335 case GLType::FLOAT_VEC3:
337 gl.Uniform3fv(info.location, info.arraySize, reinterpret_cast<const float*>(&ptr[offset]));
340 case GLType::FLOAT_VEC4:
342 gl.Uniform4fv(info.location, info.arraySize, reinterpret_cast<const float*>(&ptr[offset]));
345 case GLType::INT_VEC2:
347 gl.Uniform2iv(info.location, info.arraySize, reinterpret_cast<const GLint*>(&ptr[offset]));
350 case GLType::INT_VEC3:
352 gl.Uniform3iv(info.location, info.arraySize, reinterpret_cast<const GLint*>(&ptr[offset]));
355 case GLType::INT_VEC4:
357 gl.Uniform4iv(info.location, info.arraySize, reinterpret_cast<const GLint*>(&ptr[offset]));
362 // not supported by DALi
365 case GLType::BOOL_VEC2:
367 // not supported by DALi
370 case GLType::BOOL_VEC3:
372 // not supported by DALi
375 case GLType::BOOL_VEC4:
377 // not supported by DALi
382 gl.Uniform1fv(info.location, info.arraySize, reinterpret_cast<const float*>(&ptr[offset]));
385 case GLType::FLOAT_MAT2:
387 gl.UniformMatrix2fv(info.location, info.arraySize, GL_FALSE, reinterpret_cast<const float*>(&ptr[offset]));
390 case GLType::FLOAT_MAT3:
392 gl.UniformMatrix3fv(info.location, info.arraySize, GL_FALSE, reinterpret_cast<const float*>(&ptr[offset]));
395 case GLType::FLOAT_MAT4:
397 gl.UniformMatrix4fv(info.location, info.arraySize, GL_FALSE, reinterpret_cast<const float*>(&ptr[offset]));
400 case GLType::SAMPLER_2D:
404 case GLType::SAMPLER_CUBE:
415 void Context::BeginRenderPass(const BeginRenderPassDescriptor& renderPassBegin)
417 auto& renderPass = *renderPassBegin.renderPass;
418 auto& renderTarget = *renderPassBegin.renderTarget;
420 const auto& targetInfo = renderTarget.GetCreateInfo();
422 auto& gl = *mImpl->mController.GetGL();
423 auto& graphics = *mImpl->mController.GetGraphicsInterface();
425 if(targetInfo.surface)
428 gl.BindFramebuffer(GL_FRAMEBUFFER, 0);
430 else if(targetInfo.framebuffer)
432 // bind framebuffer and swap.
433 renderTarget.GetFramebuffer()->Bind();
436 // clear (ideally cache the setup)
438 // In GL we assume that the last attachment is depth/stencil (we may need
439 // to cache extra information inside GLES RenderTarget if we want to be
440 // more specific in case of MRT)
442 // For GLES2.0 we clear only a single color attachment
443 if(mImpl->mController.GetGLESVersion() == GLESVersion::GLES_20)
445 const auto& attachments = *renderPass.GetCreateInfo().attachments;
446 const auto& color0 = attachments[0];
448 if(color0.loadOp == AttachmentLoadOp::CLEAR)
450 mask |= GL_COLOR_BUFFER_BIT;
452 // Set clear color (todo: cache it!)
453 // Something goes wrong here if Alpha mask is GL_TRUE
454 gl.ColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
455 gl.ClearColor(renderPassBegin.clearValues[0].color.r,
456 renderPassBegin.clearValues[0].color.g,
457 renderPassBegin.clearValues[0].color.b,
458 renderPassBegin.clearValues[0].color.a);
461 // check for depth stencil
462 if(attachments.size() > 1)
464 const auto& depthStencil = attachments.back();
465 if(depthStencil.loadOp == AttachmentLoadOp::CLEAR)
467 mask |= GL_DEPTH_BUFFER_BIT;
469 if(depthStencil.stencilLoadOp == AttachmentLoadOp::CLEAR)
471 mask |= GL_STENCIL_BUFFER_BIT;
475 gl.Enable(GL_SCISSOR_TEST);
476 gl.Scissor(renderPassBegin.renderArea.x, renderPassBegin.renderArea.y, renderPassBegin.renderArea.width, renderPassBegin.renderArea.height);
478 gl.Disable(GL_SCISSOR_TEST);
481 mImpl->mCurrentRenderPass = &renderPass;
482 mImpl->mCurrentRenderTarget = &renderTarget;
485 void Context::EndRenderPass()
487 if(mImpl->mCurrentRenderTarget)
489 if(mImpl->mCurrentRenderTarget->GetFramebuffer())
491 auto& gl = *mImpl->mController.GetGL();
497 void Context::ClearState()
499 mImpl->mCurrentTextureBindings.clear();
502 } // namespace Dali::Graphics::GLES