2 * Copyright (c) 2023 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/debug.h>
21 #include <dali/integration-api/gl-abstraction.h>
22 #include <dali/integration-api/gl-defines.h>
23 #include <dali/internal/graphics/common/graphics-interface.h>
24 #include <dali/public-api/math/math-utils.h>
26 #include "egl-graphics-controller.h"
27 #include "gles-graphics-buffer.h"
28 #include "gles-graphics-pipeline.h"
29 #include "gles-graphics-program.h"
30 #include "gles-graphics-render-pass.h"
31 #include "gles-graphics-render-target.h"
32 #include "gles-texture-dependency-checker.h"
35 #include <EGL/eglext.h>
37 #include <unordered_map>
39 namespace Dali::Graphics::GLES
43 explicit Impl(EglGraphicsController& controller)
44 : mController(controller)
51 * Binds (and creates) VAO
53 * VAO is fixed per program so it has to be created only once assuming
54 * that VertexInputState has been set correctly for the pipeline.
57 void BindProgramVAO(const GLES::ProgramImpl* program, const VertexInputState& vertexInputState)
59 // Calculate attributes location hash unordered.
61 for(const auto& attr : vertexInputState.attributes)
63 // Make unordered hash value by location.
64 // Note : This hash function varified for locations only under < 20.
65 std::size_t salt = attr.location + 1;
66 hash += salt << (sizeof(std::size_t) * 6);
68 salt ^= attr.location;
69 hash += salt << (sizeof(std::size_t) * 4);
74 auto& gl = *mController.GetGL();
76 if(DALI_UNLIKELY(!mDiscardedVAOList.empty()))
78 gl.DeleteVertexArrays(static_cast<Dali::GLsizei>(mDiscardedVAOList.size()), mDiscardedVAOList.data());
79 mDiscardedVAOList.clear();
82 auto iter = mProgramVAOMap.find(program);
83 if(iter != mProgramVAOMap.end())
85 auto attributeIter = iter->second.find(hash);
86 if(attributeIter != iter->second.end())
88 if(mProgramVAOCurrentState != attributeIter->second)
90 mProgramVAOCurrentState = attributeIter->second;
91 gl.BindVertexArray(attributeIter->second);
93 // Binding VAO seems to reset the index buffer binding so the cache must be reset
94 mGlStateCache.mBoundElementArrayBufferId = 0;
101 gl.GenVertexArrays(1, &vao);
102 gl.BindVertexArray(vao);
104 // Binding VAO seems to reset the index buffer binding so the cache must be reset
105 mGlStateCache.mBoundElementArrayBufferId = 0;
107 mProgramVAOMap[program][hash] = vao;
108 for(const auto& attr : vertexInputState.attributes)
110 gl.EnableVertexAttribArray(attr.location);
113 mProgramVAOCurrentState = vao;
117 * Sets the initial GL state.
119 void InitializeGlState()
121 auto& gl = *mController.GetGL();
123 mGlStateCache.mClearColorSet = false;
124 mGlStateCache.mColorMask = true;
125 mGlStateCache.mStencilMask = 0xFF;
126 mGlStateCache.mBlendEnabled = false;
127 mGlStateCache.mDepthBufferEnabled = false;
128 mGlStateCache.mDepthMaskEnabled = false;
129 mGlStateCache.mScissorTestEnabled = false;
130 mGlStateCache.mStencilBufferEnabled = false;
132 gl.Disable(GL_DITHER);
134 mGlStateCache.mBoundArrayBufferId = 0;
135 mGlStateCache.mBoundElementArrayBufferId = 0;
136 mGlStateCache.mActiveTextureUnit = 0;
138 mGlStateCache.mBlendFuncSeparateSrcRGB = BlendFactor::ONE;
139 mGlStateCache.mBlendFuncSeparateDstRGB = BlendFactor::ZERO;
140 mGlStateCache.mBlendFuncSeparateSrcAlpha = BlendFactor::ONE;
141 mGlStateCache.mBlendFuncSeparateDstAlpha = BlendFactor::ZERO;
143 // initial state is GL_FUNC_ADD for both RGB and Alpha blend modes
144 mGlStateCache.mBlendEquationSeparateModeRGB = BlendOp::ADD;
145 mGlStateCache.mBlendEquationSeparateModeAlpha = BlendOp::ADD;
147 mGlStateCache.mCullFaceMode = CullMode::NONE; //By default cullface is disabled, front face is set to CCW and cull face is set to back
149 //Initialze vertex attribute cache
150 memset(&mGlStateCache.mVertexAttributeCachedState, 0, sizeof(mGlStateCache.mVertexAttributeCachedState));
151 memset(&mGlStateCache.mVertexAttributeCurrentState, 0, sizeof(mGlStateCache.mVertexAttributeCurrentState));
153 //Initialize bound 2d texture cache
154 memset(&mGlStateCache.mBoundTextureId, 0, sizeof(mGlStateCache.mBoundTextureId));
156 mGlStateCache.mFrameBufferStateCache.Reset();
160 * Flushes vertex attribute location changes to the driver
162 void FlushVertexAttributeLocations()
164 auto& gl = *mController.GetGL();
166 for(unsigned int i = 0; i < MAX_ATTRIBUTE_CACHE_SIZE; ++i)
168 // see if the cached state is different to the actual state
169 if(mGlStateCache.mVertexAttributeCurrentState[i] != mGlStateCache.mVertexAttributeCachedState[i])
171 // it's different so make the change to the driver and update the cached state
172 mGlStateCache.mVertexAttributeCurrentState[i] = mGlStateCache.mVertexAttributeCachedState[i];
174 if(mGlStateCache.mVertexAttributeCurrentState[i])
176 gl.EnableVertexAttribArray(i);
180 gl.DisableVertexAttribArray(i);
187 * Either enables or disables a vertex attribute location in the cache
188 * The cahnges won't take affect until FlushVertexAttributeLocations is called
189 * @param location attribute location
190 * @param state attribute state
192 void SetVertexAttributeLocation(unsigned int location, bool state)
194 auto& gl = *mController.GetGL();
196 if(location >= MAX_ATTRIBUTE_CACHE_SIZE)
198 // not cached, make the gl call through context
201 gl.EnableVertexAttribArray(location);
205 gl.DisableVertexAttribArray(location);
210 // set the cached state, it will be set at the next draw call
211 // if it's different from the current driver state
212 mGlStateCache.mVertexAttributeCachedState[location] = state;
216 EglGraphicsController& mController;
218 const GLES::PipelineImpl* mCurrentPipeline{nullptr}; ///< Currently bound pipeline
219 const GLES::PipelineImpl* mNewPipeline{nullptr}; ///< New pipeline to be set on flush
221 std::vector<Graphics::TextureBinding> mCurrentTextureBindings{};
222 std::vector<Graphics::SamplerBinding> mCurrentSamplerBindings{};
223 GLES::IndexBufferBindingDescriptor mCurrentIndexBufferBinding{};
225 struct VertexBufferBinding
227 GLES::Buffer* buffer{nullptr};
231 // Currently bound buffers
232 std::vector<VertexBufferBindingDescriptor> mCurrentVertexBufferBindings{};
234 // Currently bound UBOs (check if it's needed per program!)
235 std::vector<UniformBufferBindingDescriptor> mCurrentUBOBindings{};
236 UniformBufferBindingDescriptor mCurrentStandaloneUBOBinding{};
238 // Current render pass and render target
239 const GLES::RenderTarget* mCurrentRenderTarget{nullptr};
240 const GLES::RenderPass* mCurrentRenderPass{nullptr};
242 // Each context must have own VAOs as they cannot be shared
243 std::unordered_map<const GLES::ProgramImpl*, std::map<std::size_t, uint32_t>> mProgramVAOMap; ///< GL program-VAO map
244 uint32_t mProgramVAOCurrentState{0u}; ///< Currently bound VAO
245 GLStateCache mGlStateCache{}; ///< GL status cache
246 std::vector<Dali::GLuint> mDiscardedVAOList{};
248 bool mGlContextCreated{false}; ///< True if the OpenGL context has been created
250 EGLContext mNativeDrawContext{0u}; ///< Native rendering EGL context compatible with window context
252 EGLSurface mCacheDrawReadSurface{0u}; ///< cached 'read' surface
253 EGLSurface mCacheDrawWriteSurface{0u}; ///< cached 'write' surface
254 EGLContext mCacheEGLGraphicsContext{0u}; ///< cached window context
257 Context::Context(EglGraphicsController& controller)
259 mImpl = std::make_unique<Impl>(controller);
264 // Destroy native rendering context if one exists
265 if(mImpl->mNativeDrawContext)
267 eglDestroyContext(eglGetCurrentDisplay(), mImpl->mNativeDrawContext);
268 mImpl->mNativeDrawContext = EGL_NO_CONTEXT;
272 void Context::Flush(bool reset, const GLES::DrawCallDescriptor& drawCall, GLES::TextureDependencyChecker& dependencyChecker)
274 auto& gl = *mImpl->mController.GetGL();
276 static const bool hasGLES3(mImpl->mController.GetGLESVersion() >= GLESVersion::GLES_30);
278 // early out if neither current nor new pipelines are set
279 // this behaviour may be valid so no assert
280 if(!mImpl->mCurrentPipeline && !mImpl->mNewPipeline)
285 // Execute states if pipeline is changed
286 const auto currentProgram = mImpl->mCurrentPipeline ? static_cast<const GLES::Program*>(mImpl->mCurrentPipeline->GetCreateInfo().programState->program) : nullptr;
288 // case when new pipeline has been set
289 const GLES::Program* newProgram = nullptr;
291 if(mImpl->mNewPipeline)
293 newProgram = static_cast<const GLES::Program*>(mImpl->mNewPipeline->GetCreateInfo().programState->program);
296 if(!currentProgram && !newProgram)
298 // Early out if we have no program for this pipeline.
299 DALI_LOG_ERROR("No program defined for pipeline\n");
303 if(mImpl->mNewPipeline && mImpl->mCurrentPipeline != mImpl->mNewPipeline)
305 if(!currentProgram || currentProgram->GetImplementation()->GetGlProgram() != newProgram->GetImplementation()->GetGlProgram())
307 mImpl->mNewPipeline->Bind(newProgram->GetImplementation()->GetGlProgram());
313 // Resolve rasterization state
314 ResolveRasterizationState();
317 // Resolve uniform buffers
318 ResolveUniformBuffers();
321 // Map binding# to sampler location
322 const auto& reflection = !newProgram ? currentProgram->GetReflection() : newProgram->GetReflection();
323 const auto& samplers = reflection.GetSamplers();
325 uint32_t currentSampler = 0;
326 uint32_t currentElement = 0;
328 // @warning Assume that binding.binding is strictly linear in the same order as mCurrentTextureBindings
329 // elements. This avoids having to sort the bindings.
330 for(const auto& binding : mImpl->mCurrentTextureBindings)
332 if(currentSampler >= samplers.size())
334 // Don't bind more textures than there are active samplers.
338 auto texture = const_cast<GLES::Texture*>(static_cast<const GLES::Texture*>(binding.texture));
340 // Texture may not have been initialized yet...(tbm_surface timing issue?)
341 if(!texture->GetGLTexture())
343 texture->InitializeResource();
346 // Warning, this may cause glWaitSync to occur on the GPU.
347 dependencyChecker.CheckNeedsSync(this, texture);
348 texture->Bind(binding);
351 // @warning Assume that location of array elements is sequential.
352 // @warning GL does not guarantee this, but in practice, it is.
353 gl.Uniform1i(samplers[currentSampler].location + currentElement,
354 samplers[currentSampler].offset + currentElement);
356 if(currentElement >= samplers[currentSampler].elementCount)
363 // for each attribute bind vertices
365 const auto& pipelineState = mImpl->mNewPipeline ? mImpl->mNewPipeline->GetCreateInfo() : mImpl->mCurrentPipeline->GetCreateInfo();
366 const auto& vertexInputState = pipelineState.vertexInputState;
370 mImpl->BindProgramVAO(static_cast<const GLES::Program*>(pipelineState.programState->program)->GetImplementation(), *vertexInputState);
373 for(const auto& attr : vertexInputState->attributes)
378 mImpl->SetVertexAttributeLocation(attr.location, true);
381 const auto& bufferSlot = mImpl->mCurrentVertexBufferBindings[attr.binding];
382 const auto& bufferBinding = vertexInputState->bufferBindings[attr.binding];
384 auto glesBuffer = bufferSlot.buffer->GetGLBuffer();
387 BindBuffer(GL_ARRAY_BUFFER, glesBuffer);
389 gl.VertexAttribPointer(attr.location,
390 GLVertexFormat(attr.format).size,
391 GLVertexFormat(attr.format).format,
393 bufferBinding.stride,
394 reinterpret_cast<void*>(attr.offset));
398 const auto& ia = pipelineState.inputAssemblyState;
403 switch(drawCall.type)
405 case DrawCallDescriptor::Type::DRAW:
407 mImpl->mGlStateCache.mFrameBufferStateCache.DrawOperation(mImpl->mGlStateCache.mColorMask,
408 mImpl->mGlStateCache.DepthBufferWriteEnabled(),
409 mImpl->mGlStateCache.StencilBufferWriteEnabled());
410 // For GLES3+ we use VAO, for GLES2 internal cache
413 mImpl->FlushVertexAttributeLocations();
416 gl.DrawArrays(GLESTopology(ia->topology),
417 drawCall.draw.firstVertex,
418 drawCall.draw.vertexCount);
421 case DrawCallDescriptor::Type::DRAW_INDEXED:
423 const auto& binding = mImpl->mCurrentIndexBufferBinding;
424 BindBuffer(GL_ELEMENT_ARRAY_BUFFER, binding.buffer->GetGLBuffer());
426 mImpl->mGlStateCache.mFrameBufferStateCache.DrawOperation(mImpl->mGlStateCache.mColorMask,
427 mImpl->mGlStateCache.DepthBufferWriteEnabled(),
428 mImpl->mGlStateCache.StencilBufferWriteEnabled());
430 // For GLES3+ we use VAO, for GLES2 internal cache
433 mImpl->FlushVertexAttributeLocations();
436 auto indexBufferFormat = GLIndexFormat(binding.format).format;
437 gl.DrawElements(GLESTopology(ia->topology),
438 drawCall.drawIndexed.indexCount,
440 reinterpret_cast<void*>(binding.offset));
443 case DrawCallDescriptor::Type::DRAW_INDEXED_INDIRECT:
452 if(mImpl->mNewPipeline)
454 mImpl->mCurrentPipeline = mImpl->mNewPipeline;
455 mImpl->mNewPipeline = nullptr;
459 void Context::BindTextures(const Graphics::TextureBinding* bindings, uint32_t count)
461 // for each texture allocate slot
462 for(auto i = 0u; i < count; ++i)
464 auto& binding = bindings[i];
466 // Resize binding array if needed
467 if(mImpl->mCurrentTextureBindings.size() <= binding.binding)
469 mImpl->mCurrentTextureBindings.resize(binding.binding + 1);
471 // Store the binding details
472 mImpl->mCurrentTextureBindings[binding.binding] = binding;
476 void Context::BindVertexBuffers(const GLES::VertexBufferBindingDescriptor* bindings, uint32_t count)
478 if(count > mImpl->mCurrentVertexBufferBindings.size())
480 mImpl->mCurrentVertexBufferBindings.resize(count);
482 // Copy only set slots
483 std::copy_if(bindings, bindings + count, mImpl->mCurrentVertexBufferBindings.begin(), [](auto& item) {
484 return (nullptr != item.buffer);
488 void Context::BindIndexBuffer(const IndexBufferBindingDescriptor& indexBufferBinding)
490 mImpl->mCurrentIndexBufferBinding = indexBufferBinding;
493 void Context::BindPipeline(const GLES::Pipeline* newPipeline)
495 DALI_ASSERT_ALWAYS(newPipeline && "Invalid pipeline");
496 mImpl->mNewPipeline = &newPipeline->GetPipeline();
499 void Context::BindUniformBuffers(const UniformBufferBindingDescriptor* uboBindings,
501 const UniformBufferBindingDescriptor& standaloneBindings)
503 if(standaloneBindings.buffer)
505 mImpl->mCurrentStandaloneUBOBinding = standaloneBindings;
508 if(uboCount >= mImpl->mCurrentUBOBindings.size())
510 mImpl->mCurrentUBOBindings.resize(uboCount + 1);
513 auto it = uboBindings;
514 for(auto i = 0u; i < uboCount; ++i)
518 mImpl->mCurrentUBOBindings[i] = *it;
523 void Context::ResolveBlendState()
525 const auto& currentBlendState = mImpl->mCurrentPipeline ? mImpl->mCurrentPipeline->GetCreateInfo().colorBlendState : nullptr;
526 const auto& newBlendState = mImpl->mNewPipeline->GetCreateInfo().colorBlendState;
528 // TODO: prevent leaking the state
534 auto& gl = *mImpl->mController.GetGL();
536 if(!currentBlendState || currentBlendState->blendEnable != newBlendState->blendEnable)
538 if(newBlendState->blendEnable != mImpl->mGlStateCache.mBlendEnabled)
540 mImpl->mGlStateCache.mBlendEnabled = newBlendState->blendEnable;
541 newBlendState->blendEnable ? gl.Enable(GL_BLEND) : gl.Disable(GL_BLEND);
545 if(!newBlendState->blendEnable)
550 BlendFactor newSrcRGB(newBlendState->srcColorBlendFactor);
551 BlendFactor newDstRGB(newBlendState->dstColorBlendFactor);
552 BlendFactor newSrcAlpha(newBlendState->srcAlphaBlendFactor);
553 BlendFactor newDstAlpha(newBlendState->dstAlphaBlendFactor);
555 if(!currentBlendState ||
556 currentBlendState->srcColorBlendFactor != newSrcRGB ||
557 currentBlendState->dstColorBlendFactor != newDstRGB ||
558 currentBlendState->srcAlphaBlendFactor != newSrcAlpha ||
559 currentBlendState->dstAlphaBlendFactor != newDstAlpha)
561 if((mImpl->mGlStateCache.mBlendFuncSeparateSrcRGB != newSrcRGB) ||
562 (mImpl->mGlStateCache.mBlendFuncSeparateDstRGB != newDstRGB) ||
563 (mImpl->mGlStateCache.mBlendFuncSeparateSrcAlpha != newSrcAlpha) ||
564 (mImpl->mGlStateCache.mBlendFuncSeparateDstAlpha != newDstAlpha))
566 mImpl->mGlStateCache.mBlendFuncSeparateSrcRGB = newSrcRGB;
567 mImpl->mGlStateCache.mBlendFuncSeparateDstRGB = newDstRGB;
568 mImpl->mGlStateCache.mBlendFuncSeparateSrcAlpha = newSrcAlpha;
569 mImpl->mGlStateCache.mBlendFuncSeparateDstAlpha = newDstAlpha;
571 if(newSrcRGB == newSrcAlpha && newDstRGB == newDstAlpha)
573 gl.BlendFunc(GLBlendFunc(newSrcRGB), GLBlendFunc(newDstRGB));
577 gl.BlendFuncSeparate(GLBlendFunc(newSrcRGB), GLBlendFunc(newDstRGB), GLBlendFunc(newSrcAlpha), GLBlendFunc(newDstAlpha));
582 if(!currentBlendState ||
583 currentBlendState->colorBlendOp != newBlendState->colorBlendOp ||
584 currentBlendState->alphaBlendOp != newBlendState->alphaBlendOp)
586 if(mImpl->mGlStateCache.mBlendEquationSeparateModeRGB != newBlendState->colorBlendOp ||
587 mImpl->mGlStateCache.mBlendEquationSeparateModeAlpha != newBlendState->alphaBlendOp)
589 mImpl->mGlStateCache.mBlendEquationSeparateModeRGB = newBlendState->colorBlendOp;
590 mImpl->mGlStateCache.mBlendEquationSeparateModeAlpha = newBlendState->alphaBlendOp;
592 if(newBlendState->colorBlendOp == newBlendState->alphaBlendOp)
594 gl.BlendEquation(GLBlendOp(newBlendState->colorBlendOp));
595 if(newBlendState->colorBlendOp >= Graphics::ADVANCED_BLEND_OPTIONS_START)
602 gl.BlendEquationSeparate(GLBlendOp(newBlendState->colorBlendOp), GLBlendOp(newBlendState->alphaBlendOp));
608 void Context::ResolveRasterizationState()
610 const auto& currentRasterizationState = mImpl->mCurrentPipeline ? mImpl->mCurrentPipeline->GetCreateInfo().rasterizationState : nullptr;
611 const auto& newRasterizationState = mImpl->mNewPipeline->GetCreateInfo().rasterizationState;
613 // TODO: prevent leaking the state
614 if(!newRasterizationState)
619 auto& gl = *mImpl->mController.GetGL();
621 if(!currentRasterizationState ||
622 currentRasterizationState->cullMode != newRasterizationState->cullMode)
624 if(mImpl->mGlStateCache.mCullFaceMode != newRasterizationState->cullMode)
626 mImpl->mGlStateCache.mCullFaceMode = newRasterizationState->cullMode;
627 if(newRasterizationState->cullMode == CullMode::NONE)
629 gl.Disable(GL_CULL_FACE);
633 gl.Enable(GL_CULL_FACE);
634 gl.CullFace(GLCullMode(newRasterizationState->cullMode));
638 // TODO: implement polygon mode (fill, line, points)
639 // seems like we don't support it (no glPolygonMode())
642 void Context::ResolveUniformBuffers()
644 // Resolve standalone uniforms if we have binding
645 if(mImpl->mCurrentStandaloneUBOBinding.buffer)
647 ResolveStandaloneUniforms();
651 void Context::ResolveStandaloneUniforms()
653 // Find reflection for program
654 const GLES::Program* program{nullptr};
656 if(mImpl->mNewPipeline)
658 program = static_cast<const GLES::Program*>(mImpl->mNewPipeline->GetCreateInfo().programState->program);
660 else if(mImpl->mCurrentPipeline)
662 program = static_cast<const GLES::Program*>(mImpl->mCurrentPipeline->GetCreateInfo().programState->program);
667 const auto ptr = reinterpret_cast<const char*>(mImpl->mCurrentStandaloneUBOBinding.buffer->GetCPUAllocatedAddress()) + mImpl->mCurrentStandaloneUBOBinding.offset;
668 // Update program uniforms
669 program->GetImplementation()->UpdateStandaloneUniformBlock(ptr);
673 void Context::BeginRenderPass(const BeginRenderPassDescriptor& renderPassBegin)
675 auto& renderPass = *renderPassBegin.renderPass;
676 auto& renderTarget = *renderPassBegin.renderTarget;
678 const auto& targetInfo = renderTarget.GetCreateInfo();
680 auto& gl = *mImpl->mController.GetGL();
682 if(targetInfo.surface)
685 BindFrameBuffer(GL_FRAMEBUFFER, 0);
687 else if(targetInfo.framebuffer)
689 // bind framebuffer and swap.
690 auto framebuffer = renderTarget.GetFramebuffer();
694 // clear (ideally cache the setup)
696 // In GL we assume that the last attachment is depth/stencil (we may need
697 // to cache extra information inside GLES RenderTarget if we want to be
698 // more specific in case of MRT)
700 const auto& attachments = *renderPass.GetCreateInfo().attachments;
701 const auto& color0 = attachments[0];
704 if(color0.loadOp == AttachmentLoadOp::CLEAR)
706 mask |= GL_COLOR_BUFFER_BIT;
709 // Something goes wrong here if Alpha mask is GL_TRUE
712 const auto clearValues = renderPassBegin.clearValues.Ptr();
714 if(!Dali::Equals(mImpl->mGlStateCache.mClearColor.r, clearValues[0].color.r) ||
715 !Dali::Equals(mImpl->mGlStateCache.mClearColor.g, clearValues[0].color.g) ||
716 !Dali::Equals(mImpl->mGlStateCache.mClearColor.b, clearValues[0].color.b) ||
717 !Dali::Equals(mImpl->mGlStateCache.mClearColor.a, clearValues[0].color.a) ||
718 !mImpl->mGlStateCache.mClearColorSet)
720 gl.ClearColor(clearValues[0].color.r,
721 clearValues[0].color.g,
722 clearValues[0].color.b,
723 clearValues[0].color.a);
725 mImpl->mGlStateCache.mClearColorSet = true;
726 mImpl->mGlStateCache.mClearColor = Vector4(clearValues[0].color.r,
727 clearValues[0].color.g,
728 clearValues[0].color.b,
729 clearValues[0].color.a);
733 // check for depth stencil
734 if(attachments.size() > 1)
736 const auto& depthStencil = attachments.back();
737 if(depthStencil.loadOp == AttachmentLoadOp::CLEAR)
739 if(!mImpl->mGlStateCache.mDepthMaskEnabled)
741 mImpl->mGlStateCache.mDepthMaskEnabled = true;
744 mask |= GL_DEPTH_BUFFER_BIT;
746 if(depthStencil.stencilLoadOp == AttachmentLoadOp::CLEAR)
748 if(mImpl->mGlStateCache.mStencilMask != 0xFF)
750 mImpl->mGlStateCache.mStencilMask = 0xFF;
751 gl.StencilMask(0xFF);
753 mask |= GL_STENCIL_BUFFER_BIT;
757 SetScissorTestEnabled(true);
758 gl.Scissor(renderPassBegin.renderArea.x, renderPassBegin.renderArea.y, renderPassBegin.renderArea.width, renderPassBegin.renderArea.height);
759 ClearBuffer(mask, true);
760 SetScissorTestEnabled(false);
762 mImpl->mCurrentRenderPass = &renderPass;
763 mImpl->mCurrentRenderTarget = &renderTarget;
766 void Context::EndRenderPass(GLES::TextureDependencyChecker& dependencyChecker)
768 if(mImpl->mCurrentRenderTarget)
770 GLES::Framebuffer* framebuffer = mImpl->mCurrentRenderTarget->GetFramebuffer();
773 auto& gl = *mImpl->mController.GetGL();
776 if(framebuffer->CaptureRequested())
778 framebuffer->DrawRenderedBuffer();
781 /* @todo Full dependency checking would need to store textures in Begin, and create
782 * fence objects here; but we're going to draw all fbos on shared context in serial,
783 * so no real need (yet). Might want to consider ensuring order of render passes,
784 * but that needs doing in the controller, and would need doing before ProcessCommandQueues.
786 * Currently up to the client to create render tasks in the right order.
789 /* Create fence sync objects. Other contexts can then wait on these fences before reading
792 dependencyChecker.AddTextures(this, framebuffer);
797 void Context::ClearState()
799 mImpl->mCurrentTextureBindings.clear();
802 void Context::ColorMask(bool enabled)
804 if(enabled != mImpl->mGlStateCache.mColorMask)
806 mImpl->mGlStateCache.mColorMask = enabled;
808 auto& gl = *mImpl->mController.GetGL();
809 gl.ColorMask(enabled, enabled, enabled, enabled);
813 void Context::ClearStencilBuffer()
815 ClearBuffer(GL_STENCIL_BUFFER_BIT, false);
818 void Context::ClearDepthBuffer()
820 ClearBuffer(GL_DEPTH_BUFFER_BIT, false);
823 void Context::ClearBuffer(uint32_t mask, bool forceClear)
825 mask = mImpl->mGlStateCache.mFrameBufferStateCache.GetClearMask(mask, forceClear, mImpl->mGlStateCache.mScissorTestEnabled);
828 auto& gl = *mImpl->mController.GetGL();
833 void Context::InvalidateDepthStencilBuffers()
835 auto& gl = *mImpl->mController.GetGL();
837 GLenum attachments[] = {GL_DEPTH, GL_STENCIL};
838 gl.InvalidateFramebuffer(GL_FRAMEBUFFER, 2, attachments);
841 void Context::SetScissorTestEnabled(bool scissorEnabled)
843 if(mImpl->mGlStateCache.mScissorTestEnabled != scissorEnabled)
845 mImpl->mGlStateCache.mScissorTestEnabled = scissorEnabled;
847 auto& gl = *mImpl->mController.GetGL();
850 gl.Enable(GL_SCISSOR_TEST);
854 gl.Disable(GL_SCISSOR_TEST);
859 void Context::SetStencilTestEnable(bool stencilEnable)
861 if(stencilEnable != mImpl->mGlStateCache.mStencilBufferEnabled)
863 mImpl->mGlStateCache.mStencilBufferEnabled = stencilEnable;
865 auto& gl = *mImpl->mController.GetGL();
868 gl.Enable(GL_STENCIL_TEST);
872 gl.Disable(GL_STENCIL_TEST);
877 void Context::StencilMask(uint32_t writeMask)
879 if(writeMask != mImpl->mGlStateCache.mStencilMask)
881 mImpl->mGlStateCache.mStencilMask = writeMask;
883 auto& gl = *mImpl->mController.GetGL();
884 gl.StencilMask(writeMask);
888 void Context::StencilFunc(Graphics::CompareOp compareOp,
890 uint32_t compareMask)
892 if(compareOp != mImpl->mGlStateCache.mStencilFunc ||
893 reference != mImpl->mGlStateCache.mStencilFuncRef ||
894 compareMask != mImpl->mGlStateCache.mStencilFuncMask)
896 mImpl->mGlStateCache.mStencilFunc = compareOp;
897 mImpl->mGlStateCache.mStencilFuncRef = reference;
898 mImpl->mGlStateCache.mStencilFuncMask = compareMask;
900 auto& gl = *mImpl->mController.GetGL();
901 gl.StencilFunc(GLCompareOp(compareOp).op, reference, compareMask);
905 void Context::StencilOp(Graphics::StencilOp failOp,
906 Graphics::StencilOp depthFailOp,
907 Graphics::StencilOp passOp)
909 if(failOp != mImpl->mGlStateCache.mStencilOpFail ||
910 depthFailOp != mImpl->mGlStateCache.mStencilOpDepthFail ||
911 passOp != mImpl->mGlStateCache.mStencilOpDepthPass)
913 mImpl->mGlStateCache.mStencilOpFail = failOp;
914 mImpl->mGlStateCache.mStencilOpDepthFail = depthFailOp;
915 mImpl->mGlStateCache.mStencilOpDepthPass = passOp;
917 auto& gl = *mImpl->mController.GetGL();
918 gl.StencilOp(GLStencilOp(failOp).op, GLStencilOp(depthFailOp).op, GLStencilOp(passOp).op);
922 void Context::SetDepthCompareOp(Graphics::CompareOp compareOp)
924 if(compareOp != mImpl->mGlStateCache.mDepthFunction)
926 mImpl->mGlStateCache.mDepthFunction = compareOp;
927 auto& gl = *mImpl->mController.GetGL();
928 gl.DepthFunc(GLCompareOp(compareOp).op);
932 void Context::SetDepthTestEnable(bool depthTestEnable)
934 if(depthTestEnable != mImpl->mGlStateCache.mDepthBufferEnabled)
936 mImpl->mGlStateCache.mDepthBufferEnabled = depthTestEnable;
938 auto& gl = *mImpl->mController.GetGL();
941 gl.Enable(GL_DEPTH_TEST);
945 gl.Disable(GL_DEPTH_TEST);
950 void Context::SetDepthWriteEnable(bool depthWriteEnable)
952 if(depthWriteEnable != mImpl->mGlStateCache.mDepthMaskEnabled)
954 mImpl->mGlStateCache.mDepthMaskEnabled = depthWriteEnable;
956 auto& gl = *mImpl->mController.GetGL();
957 gl.DepthMask(depthWriteEnable);
961 void Context::ActiveTexture(uint32_t textureBindingIndex)
963 if(mImpl->mGlStateCache.mActiveTextureUnit != textureBindingIndex)
965 mImpl->mGlStateCache.mActiveTextureUnit = textureBindingIndex;
967 auto& gl = *mImpl->mController.GetGL();
968 gl.ActiveTexture(GL_TEXTURE0 + textureBindingIndex);
972 void Context::BindTexture(GLenum target, BoundTextureType textureTypeId, uint32_t textureId)
974 uint32_t typeId = static_cast<uint32_t>(textureTypeId);
975 if(mImpl->mGlStateCache.mBoundTextureId[mImpl->mGlStateCache.mActiveTextureUnit][typeId] != textureId)
977 mImpl->mGlStateCache.mBoundTextureId[mImpl->mGlStateCache.mActiveTextureUnit][typeId] = textureId;
979 auto& gl = *mImpl->mController.GetGL();
980 gl.BindTexture(target, textureId);
984 void Context::GenerateMipmap(GLenum target)
986 auto& gl = *mImpl->mController.GetGL();
987 gl.GenerateMipmap(target);
990 void Context::BindBuffer(GLenum target, uint32_t bufferId)
994 case GL_ARRAY_BUFFER:
996 if(mImpl->mGlStateCache.mBoundArrayBufferId == bufferId)
1000 mImpl->mGlStateCache.mBoundArrayBufferId = bufferId;
1003 case GL_ELEMENT_ARRAY_BUFFER:
1005 if(mImpl->mGlStateCache.mBoundElementArrayBufferId == bufferId)
1009 mImpl->mGlStateCache.mBoundElementArrayBufferId = bufferId;
1014 // Cache miss. Bind buffer.
1015 auto& gl = *mImpl->mController.GetGL();
1016 gl.BindBuffer(target, bufferId);
1019 void Context::DrawBuffers(uint32_t count, const GLenum* buffers)
1021 mImpl->mGlStateCache.mFrameBufferStateCache.DrawOperation(mImpl->mGlStateCache.mColorMask,
1022 mImpl->mGlStateCache.DepthBufferWriteEnabled(),
1023 mImpl->mGlStateCache.StencilBufferWriteEnabled());
1025 auto& gl = *mImpl->mController.GetGL();
1026 gl.DrawBuffers(count, buffers);
1029 void Context::BindFrameBuffer(GLenum target, uint32_t bufferId)
1031 mImpl->mGlStateCache.mFrameBufferStateCache.SetCurrentFrameBuffer(bufferId);
1033 auto& gl = *mImpl->mController.GetGL();
1034 gl.BindFramebuffer(target, bufferId);
1037 void Context::GenFramebuffers(uint32_t count, uint32_t* framebuffers)
1039 auto& gl = *mImpl->mController.GetGL();
1040 gl.GenFramebuffers(count, framebuffers);
1042 mImpl->mGlStateCache.mFrameBufferStateCache.FrameBuffersCreated(count, framebuffers);
1045 void Context::DeleteFramebuffers(uint32_t count, uint32_t* framebuffers)
1047 mImpl->mGlStateCache.mFrameBufferStateCache.FrameBuffersDeleted(count, framebuffers);
1049 auto& gl = *mImpl->mController.GetGL();
1050 gl.DeleteFramebuffers(count, framebuffers);
1053 GLStateCache& Context::GetGLStateCache()
1055 return mImpl->mGlStateCache;
1058 void Context::GlContextCreated()
1060 if(!mImpl->mGlContextCreated)
1062 mImpl->mGlContextCreated = true;
1064 // Set the initial GL state
1065 mImpl->InitializeGlState();
1069 void Context::GlContextDestroyed()
1071 mImpl->mGlContextCreated = false;
1074 void Context::InvalidateCachedPipeline(GLES::Pipeline* pipeline)
1076 // Since the pipeline is deleted, invalidate the cached pipeline.
1077 if(mImpl->mCurrentPipeline == &pipeline->GetPipeline())
1079 mImpl->mCurrentPipeline = nullptr;
1082 // Remove cached VAO map
1083 auto* gl = mImpl->mController.GetGL();
1086 const auto* program = pipeline->GetCreateInfo().programState->program;
1089 const auto* programImpl = static_cast<const GLES::Program*>(program)->GetImplementation();
1092 auto iter = mImpl->mProgramVAOMap.find(programImpl);
1093 if(iter != mImpl->mProgramVAOMap.end())
1095 for(auto& attributeHashPair : iter->second)
1097 auto vao = attributeHashPair.second;
1099 // Do not delete vao now. (Since Context might not be current.)
1100 mImpl->mDiscardedVAOList.emplace_back(vao);
1101 if(mImpl->mProgramVAOCurrentState == vao)
1103 mImpl->mProgramVAOCurrentState = 0u;
1107 // Clear cached Vertex buffer.
1108 mImpl->mCurrentVertexBufferBindings.clear();
1110 mImpl->mProgramVAOMap.erase(iter);
1117 void Context::PrepareForNativeRendering()
1119 // this should be pretty much constant
1120 auto display = eglGetCurrentDisplay();
1121 auto drawSurface = eglGetCurrentSurface(EGL_DRAW);
1122 auto readSurface = eglGetCurrentSurface(EGL_READ);
1123 auto context = eglGetCurrentContext();
1125 // push the surface and context data to the impl
1126 // It's needed to restore context
1127 if(!mImpl->mCacheEGLGraphicsContext)
1129 mImpl->mCacheDrawWriteSurface = drawSurface;
1130 mImpl->mCacheDrawReadSurface = readSurface;
1131 mImpl->mCacheEGLGraphicsContext = context;
1134 if(!mImpl->mNativeDrawContext)
1136 EGLint configId{0u};
1137 eglQueryContext(display, mImpl->mController.GetSharedContext(), EGL_CONFIG_ID, &configId);
1139 EGLint configAttribs[3];
1140 configAttribs[0] = EGL_CONFIG_ID;
1141 configAttribs[1] = configId;
1142 configAttribs[2] = EGL_NONE;
1146 if(eglChooseConfig(display, configAttribs, &config, 1, &numConfigs) != EGL_TRUE)
1148 DALI_LOG_ERROR("eglChooseConfig failed!\n");
1152 auto version = int(mImpl->mController.GetGLESVersion());
1154 std::vector<EGLint> attribs;
1155 attribs.push_back(EGL_CONTEXT_MAJOR_VERSION_KHR);
1156 attribs.push_back(version / 10);
1157 attribs.push_back(EGL_CONTEXT_MINOR_VERSION_KHR);
1158 attribs.push_back(version % 10);
1159 attribs.push_back(EGL_NONE);
1161 mImpl->mNativeDrawContext = eglCreateContext(display, config, mImpl->mController.GetSharedContext(), attribs.data());
1162 if(mImpl->mNativeDrawContext == EGL_NO_CONTEXT)
1164 DALI_LOG_ERROR("eglCreateContext failed!\n");
1169 eglMakeCurrent(display, drawSurface, readSurface, mImpl->mNativeDrawContext);
1172 void Context::RestoreFromNativeRendering()
1174 auto display = eglGetCurrentDisplay();
1176 // bring back original context
1177 eglMakeCurrent(display, mImpl->mCacheDrawWriteSurface, mImpl->mCacheDrawReadSurface, mImpl->mCacheEGLGraphicsContext);
1180 } // namespace Dali::Graphics::GLES