2 * Copyright (c) 2022 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>
38 namespace Dali::Graphics::GLES
42 explicit Impl(EglGraphicsController& controller)
43 : mController(controller)
50 * Binds (and creates) VAO
52 * VAO is fixed per program so it has to be created only once assuming
53 * that VertexInputState has been set correctly for the pipeline.
56 void BindProgramVAO(GLES::ProgramImpl* program, const VertexInputState& vertexInputState)
58 auto& gl = *mController.GetGL();
59 auto iter = mProgramVAOMap.find(program);
60 if(iter != mProgramVAOMap.end())
62 if(mProgramVAOCurrentState != iter->second)
64 mProgramVAOCurrentState = iter->second;
65 gl.BindVertexArray(iter->second);
71 gl.GenVertexArrays(1, &vao);
72 gl.BindVertexArray(vao);
73 mProgramVAOMap[program] = vao;
74 for(const auto& attr : vertexInputState.attributes)
76 gl.EnableVertexAttribArray(attr.location);
79 mProgramVAOCurrentState = vao;
83 * Sets the initial GL state.
85 void InitializeGlState()
87 auto& gl = *mController.GetGL();
89 mGlStateCache.mClearColorSet = false;
90 mGlStateCache.mColorMask = true;
91 mGlStateCache.mStencilMask = 0xFF;
92 mGlStateCache.mBlendEnabled = false;
93 mGlStateCache.mDepthBufferEnabled = false;
94 mGlStateCache.mDepthMaskEnabled = false;
95 mGlStateCache.mScissorTestEnabled = false;
96 mGlStateCache.mStencilBufferEnabled = false;
98 gl.Disable(GL_DITHER);
100 mGlStateCache.mBoundArrayBufferId = 0;
101 mGlStateCache.mBoundElementArrayBufferId = 0;
102 mGlStateCache.mActiveTextureUnit = 0;
104 mGlStateCache.mBlendFuncSeparateSrcRGB = BlendFactor::ONE;
105 mGlStateCache.mBlendFuncSeparateDstRGB = BlendFactor::ZERO;
106 mGlStateCache.mBlendFuncSeparateSrcAlpha = BlendFactor::ONE;
107 mGlStateCache.mBlendFuncSeparateDstAlpha = BlendFactor::ZERO;
109 // initial state is GL_FUNC_ADD for both RGB and Alpha blend modes
110 mGlStateCache.mBlendEquationSeparateModeRGB = BlendOp::ADD;
111 mGlStateCache.mBlendEquationSeparateModeAlpha = BlendOp::ADD;
113 mGlStateCache.mCullFaceMode = CullMode::NONE; //By default cullface is disabled, front face is set to CCW and cull face is set to back
115 //Initialze vertex attribute cache
116 memset(&mGlStateCache.mVertexAttributeCachedState, 0, sizeof(mGlStateCache.mVertexAttributeCachedState));
117 memset(&mGlStateCache.mVertexAttributeCurrentState, 0, sizeof(mGlStateCache.mVertexAttributeCurrentState));
119 //Initialize bound 2d texture cache
120 memset(&mGlStateCache.mBoundTextureId, 0, sizeof(mGlStateCache.mBoundTextureId));
122 mGlStateCache.mFrameBufferStateCache.Reset();
126 * Flushes vertex attribute location changes to the driver
128 void FlushVertexAttributeLocations()
130 auto& gl = *mController.GetGL();
132 for(unsigned int i = 0; i < MAX_ATTRIBUTE_CACHE_SIZE; ++i)
134 // see if the cached state is different to the actual state
135 if(mGlStateCache.mVertexAttributeCurrentState[i] != mGlStateCache.mVertexAttributeCachedState[i])
137 // it's different so make the change to the driver and update the cached state
138 mGlStateCache.mVertexAttributeCurrentState[i] = mGlStateCache.mVertexAttributeCachedState[i];
140 if(mGlStateCache.mVertexAttributeCurrentState[i])
142 gl.EnableVertexAttribArray(i);
146 gl.DisableVertexAttribArray(i);
153 * Either enables or disables a vertex attribute location in the cache
154 * The cahnges won't take affect until FlushVertexAttributeLocations is called
155 * @param location attribute location
156 * @param state attribute state
158 void SetVertexAttributeLocation(unsigned int location, bool state)
160 auto& gl = *mController.GetGL();
162 if(location >= MAX_ATTRIBUTE_CACHE_SIZE)
164 // not cached, make the gl call through context
167 gl.EnableVertexAttribArray(location);
171 gl.DisableVertexAttribArray(location);
176 // set the cached state, it will be set at the next draw call
177 // if it's different from the current driver state
178 mGlStateCache.mVertexAttributeCachedState[location] = state;
182 EglGraphicsController& mController;
184 const GLES::PipelineImpl* mCurrentPipeline{nullptr}; ///< Currently bound pipeline
185 const GLES::PipelineImpl* mNewPipeline{nullptr}; ///< New pipeline to be set on flush
187 std::vector<Graphics::TextureBinding> mCurrentTextureBindings{};
188 std::vector<Graphics::SamplerBinding> mCurrentSamplerBindings{};
189 GLES::IndexBufferBindingDescriptor mCurrentIndexBufferBinding{};
191 struct VertexBufferBinding
193 GLES::Buffer* buffer{nullptr};
197 // Currently bound buffers
198 std::vector<VertexBufferBindingDescriptor> mCurrentVertexBufferBindings{};
200 // Currently bound UBOs (check if it's needed per program!)
201 std::vector<UniformBufferBindingDescriptor> mCurrentUBOBindings{};
202 UniformBufferBindingDescriptor mCurrentStandaloneUBOBinding{};
204 // Current render pass and render target
205 const GLES::RenderTarget* mCurrentRenderTarget{nullptr};
206 const GLES::RenderPass* mCurrentRenderPass{nullptr};
208 // Each context must have own VAOs as they cannot be shared
209 std::map<GLES::ProgramImpl*, uint32_t> mProgramVAOMap; ///< GL program-VAO map
210 uint32_t mProgramVAOCurrentState{0u}; ///< Currently bound VAO
211 GLStateCache mGlStateCache{}; ///< GL status cache
213 bool mGlContextCreated{false}; ///< True if the OpenGL context has been created
215 EGLContext mNativeDrawContext{0u}; ///< Native rendering EGL context compatible with window context
217 EGLSurface mCacheDrawReadSurface{0u}; ///< cached 'read' surface
218 EGLSurface mCacheDrawWriteSurface{0u}; ///< cached 'write' surface
219 EGLContext mCacheEGLGraphicsContext{0u}; ///< cached window context
222 Context::Context(EglGraphicsController& controller)
224 mImpl = std::make_unique<Impl>(controller);
229 // Destroy native rendering context if one exists
230 if(mImpl->mNativeDrawContext)
232 eglDestroyContext(eglGetCurrentDisplay(), mImpl->mNativeDrawContext);
233 mImpl->mNativeDrawContext = EGL_NO_CONTEXT;
237 void Context::Flush(bool reset, const GLES::DrawCallDescriptor& drawCall, GLES::TextureDependencyChecker& dependencyChecker)
239 auto& gl = *mImpl->mController.GetGL();
241 static const bool hasGLES3(mImpl->mController.GetGLESVersion() >= GLESVersion::GLES_30);
243 // early out if neither current nor new pipelines are set
244 // this behaviour may be valid so no assert
245 if(!mImpl->mCurrentPipeline && !mImpl->mNewPipeline)
250 // Execute states if pipeline is changed
251 const auto currentProgram = mImpl->mCurrentPipeline ? static_cast<const GLES::Program*>(mImpl->mCurrentPipeline->GetCreateInfo().programState->program) : nullptr;
253 // case when new pipeline has been set
254 const GLES::Program* newProgram = nullptr;
256 if(mImpl->mNewPipeline)
258 newProgram = static_cast<const GLES::Program*>(mImpl->mNewPipeline->GetCreateInfo().programState->program);
261 if(!currentProgram && !newProgram)
263 // Early out if we have no program for this pipeline.
264 DALI_LOG_ERROR("No program defined for pipeline\n");
268 if(mImpl->mNewPipeline && mImpl->mCurrentPipeline != mImpl->mNewPipeline)
270 if(!currentProgram || currentProgram->GetImplementation()->GetGlProgram() != newProgram->GetImplementation()->GetGlProgram())
272 mImpl->mNewPipeline->Bind(newProgram->GetImplementation()->GetGlProgram());
278 // Resolve rasterization state
279 ResolveRasterizationState();
282 // Resolve uniform buffers
283 ResolveUniformBuffers();
286 // Map binding# to sampler location
287 const auto& reflection = !newProgram ? currentProgram->GetReflection() : newProgram->GetReflection();
288 const auto& samplers = reflection.GetSamplers();
289 for(const auto& binding : mImpl->mCurrentTextureBindings)
291 auto texture = const_cast<GLES::Texture*>(static_cast<const GLES::Texture*>(binding.texture));
293 // Texture may not have been initialized yet...(tbm_surface timing issue?)
294 if(!texture->GetGLTexture())
296 // Attempt to reinitialize
297 // @todo need to put this somewhere else where it isn't const.
298 // Maybe post it back on end of initialize queue if initialization fails?
299 texture->InitializeResource();
302 // Warning, this may cause glWaitSync to occur on the GPU.
303 dependencyChecker.CheckNeedsSync(this, texture);
305 texture->Bind(binding);
307 texture->Prepare(); // @todo also non-const.
309 if(binding.binding < samplers.size()) // binding maps to texture unit. (texture bindings should also be in binding order)
311 // Offset is set to the lexical offset within the frag shader, map it to the texture unit
312 // @todo Explicitly set the texture unit through the graphics interface
313 gl.Uniform1i(samplers[binding.binding].location, samplers[binding.binding].offset);
317 // for each attribute bind vertices
319 const auto& pipelineState = mImpl->mNewPipeline ? mImpl->mNewPipeline->GetCreateInfo() : mImpl->mCurrentPipeline->GetCreateInfo();
320 const auto& vertexInputState = pipelineState.vertexInputState;
324 mImpl->BindProgramVAO(static_cast<const GLES::Program*>(pipelineState.programState->program)->GetImplementation(), *vertexInputState);
327 for(const auto& attr : vertexInputState->attributes)
332 mImpl->SetVertexAttributeLocation(attr.location, true);
335 const auto& bufferSlot = mImpl->mCurrentVertexBufferBindings[attr.binding];
336 const auto& bufferBinding = vertexInputState->bufferBindings[attr.binding];
338 auto glesBuffer = bufferSlot.buffer->GetGLBuffer();
341 BindBuffer(GL_ARRAY_BUFFER, glesBuffer);
343 gl.VertexAttribPointer(attr.location,
344 GLVertexFormat(attr.format).size,
345 GLVertexFormat(attr.format).format,
347 bufferBinding.stride,
348 reinterpret_cast<void*>(attr.offset));
352 const auto& ia = pipelineState.inputAssemblyState;
357 switch(drawCall.type)
359 case DrawCallDescriptor::Type::DRAW:
361 mImpl->mGlStateCache.mFrameBufferStateCache.DrawOperation(mImpl->mGlStateCache.mColorMask,
362 mImpl->mGlStateCache.DepthBufferWriteEnabled(),
363 mImpl->mGlStateCache.StencilBufferWriteEnabled());
364 // For GLES3+ we use VAO, for GLES2 internal cache
367 mImpl->FlushVertexAttributeLocations();
370 gl.DrawArrays(GLESTopology(ia->topology),
371 drawCall.draw.firstVertex,
372 drawCall.draw.vertexCount);
375 case DrawCallDescriptor::Type::DRAW_INDEXED:
377 const auto& binding = mImpl->mCurrentIndexBufferBinding;
378 BindBuffer(GL_ELEMENT_ARRAY_BUFFER, binding.buffer->GetGLBuffer());
380 mImpl->mGlStateCache.mFrameBufferStateCache.DrawOperation(mImpl->mGlStateCache.mColorMask,
381 mImpl->mGlStateCache.DepthBufferWriteEnabled(),
382 mImpl->mGlStateCache.StencilBufferWriteEnabled());
384 // For GLES3+ we use VAO, for GLES2 internal cache
387 mImpl->FlushVertexAttributeLocations();
390 auto indexBufferFormat = GLIndexFormat(binding.format).format;
391 gl.DrawElements(GLESTopology(ia->topology),
392 drawCall.drawIndexed.indexCount,
394 reinterpret_cast<void*>(binding.offset));
397 case DrawCallDescriptor::Type::DRAW_INDEXED_INDIRECT:
406 if(mImpl->mNewPipeline)
408 mImpl->mCurrentPipeline = mImpl->mNewPipeline;
409 mImpl->mNewPipeline = nullptr;
413 void Context::BindTextures(const Graphics::TextureBinding* bindings, uint32_t count)
415 // for each texture allocate slot
416 for(auto i = 0u; i < count; ++i)
418 auto& binding = bindings[i];
420 // Resize binding array if needed
421 if(mImpl->mCurrentTextureBindings.size() <= binding.binding)
423 mImpl->mCurrentTextureBindings.resize(binding.binding + 1);
425 // Store the binding details
426 mImpl->mCurrentTextureBindings[binding.binding] = binding;
430 void Context::BindVertexBuffers(const GLES::VertexBufferBindingDescriptor* bindings, uint32_t count)
432 if(count > mImpl->mCurrentVertexBufferBindings.size())
434 mImpl->mCurrentVertexBufferBindings.resize(count);
436 // Copy only set slots
437 std::copy_if(bindings, bindings + count, mImpl->mCurrentVertexBufferBindings.begin(), [](auto& item) {
438 return (nullptr != item.buffer);
442 void Context::BindIndexBuffer(const IndexBufferBindingDescriptor& indexBufferBinding)
444 mImpl->mCurrentIndexBufferBinding = indexBufferBinding;
447 void Context::BindPipeline(const GLES::Pipeline* newPipeline)
449 DALI_ASSERT_ALWAYS(newPipeline && "Invalid pipeline");
450 mImpl->mNewPipeline = &newPipeline->GetPipeline();
453 void Context::BindUniformBuffers(const UniformBufferBindingDescriptor* uboBindings,
455 const UniformBufferBindingDescriptor& standaloneBindings)
457 if(standaloneBindings.buffer)
459 mImpl->mCurrentStandaloneUBOBinding = standaloneBindings;
462 if(uboCount >= mImpl->mCurrentUBOBindings.size())
464 mImpl->mCurrentUBOBindings.resize(uboCount + 1);
467 auto it = uboBindings;
468 for(auto i = 0u; i < uboCount; ++i)
472 mImpl->mCurrentUBOBindings[i] = *it;
477 void Context::ResolveBlendState()
479 const auto& currentBlendState = mImpl->mCurrentPipeline ? mImpl->mCurrentPipeline->GetCreateInfo().colorBlendState : nullptr;
480 const auto& newBlendState = mImpl->mNewPipeline->GetCreateInfo().colorBlendState;
482 // TODO: prevent leaking the state
488 auto& gl = *mImpl->mController.GetGL();
490 if(!currentBlendState || currentBlendState->blendEnable != newBlendState->blendEnable)
492 if(newBlendState->blendEnable != mImpl->mGlStateCache.mBlendEnabled)
494 mImpl->mGlStateCache.mBlendEnabled = newBlendState->blendEnable;
495 newBlendState->blendEnable ? gl.Enable(GL_BLEND) : gl.Disable(GL_BLEND);
499 if(!newBlendState->blendEnable)
504 BlendFactor newSrcRGB(newBlendState->srcColorBlendFactor);
505 BlendFactor newDstRGB(newBlendState->dstColorBlendFactor);
506 BlendFactor newSrcAlpha(newBlendState->srcAlphaBlendFactor);
507 BlendFactor newDstAlpha(newBlendState->dstAlphaBlendFactor);
509 if(!currentBlendState ||
510 currentBlendState->srcColorBlendFactor != newSrcRGB ||
511 currentBlendState->dstColorBlendFactor != newDstRGB ||
512 currentBlendState->srcAlphaBlendFactor != newSrcAlpha ||
513 currentBlendState->dstAlphaBlendFactor != newDstAlpha)
515 if((mImpl->mGlStateCache.mBlendFuncSeparateSrcRGB != newSrcRGB) ||
516 (mImpl->mGlStateCache.mBlendFuncSeparateDstRGB != newDstRGB) ||
517 (mImpl->mGlStateCache.mBlendFuncSeparateSrcAlpha != newSrcAlpha) ||
518 (mImpl->mGlStateCache.mBlendFuncSeparateDstAlpha != newDstAlpha))
520 mImpl->mGlStateCache.mBlendFuncSeparateSrcRGB = newSrcRGB;
521 mImpl->mGlStateCache.mBlendFuncSeparateDstRGB = newDstRGB;
522 mImpl->mGlStateCache.mBlendFuncSeparateSrcAlpha = newSrcAlpha;
523 mImpl->mGlStateCache.mBlendFuncSeparateDstAlpha = newDstAlpha;
525 if(newSrcRGB == newSrcAlpha && newDstRGB == newDstAlpha)
527 gl.BlendFunc(GLBlendFunc(newSrcRGB), GLBlendFunc(newDstRGB));
531 gl.BlendFuncSeparate(GLBlendFunc(newSrcRGB), GLBlendFunc(newDstRGB), GLBlendFunc(newSrcAlpha), GLBlendFunc(newDstAlpha));
536 if(!currentBlendState ||
537 currentBlendState->colorBlendOp != newBlendState->colorBlendOp ||
538 currentBlendState->alphaBlendOp != newBlendState->alphaBlendOp)
540 if(mImpl->mGlStateCache.mBlendEquationSeparateModeRGB != newBlendState->colorBlendOp ||
541 mImpl->mGlStateCache.mBlendEquationSeparateModeAlpha != newBlendState->alphaBlendOp)
543 mImpl->mGlStateCache.mBlendEquationSeparateModeRGB = newBlendState->colorBlendOp;
544 mImpl->mGlStateCache.mBlendEquationSeparateModeAlpha = newBlendState->alphaBlendOp;
546 if(newBlendState->colorBlendOp == newBlendState->alphaBlendOp)
548 gl.BlendEquation(GLBlendOp(newBlendState->colorBlendOp));
549 if(newBlendState->colorBlendOp >= Graphics::ADVANCED_BLEND_OPTIONS_START)
556 gl.BlendEquationSeparate(GLBlendOp(newBlendState->colorBlendOp), GLBlendOp(newBlendState->alphaBlendOp));
562 void Context::ResolveRasterizationState()
564 const auto& currentRasterizationState = mImpl->mCurrentPipeline ? mImpl->mCurrentPipeline->GetCreateInfo().rasterizationState : nullptr;
565 const auto& newRasterizationState = mImpl->mNewPipeline->GetCreateInfo().rasterizationState;
567 // TODO: prevent leaking the state
568 if(!newRasterizationState)
573 auto& gl = *mImpl->mController.GetGL();
575 if(!currentRasterizationState ||
576 currentRasterizationState->cullMode != newRasterizationState->cullMode)
578 if(mImpl->mGlStateCache.mCullFaceMode != newRasterizationState->cullMode)
580 mImpl->mGlStateCache.mCullFaceMode = newRasterizationState->cullMode;
581 if(newRasterizationState->cullMode == CullMode::NONE)
583 gl.Disable(GL_CULL_FACE);
587 gl.Enable(GL_CULL_FACE);
588 gl.CullFace(GLCullMode(newRasterizationState->cullMode));
592 // TODO: implement polygon mode (fill, line, points)
593 // seems like we don't support it (no glPolygonMode())
596 void Context::ResolveUniformBuffers()
598 // Resolve standalone uniforms if we have binding
599 if(mImpl->mCurrentStandaloneUBOBinding.buffer)
601 ResolveStandaloneUniforms();
605 void Context::ResolveStandaloneUniforms()
607 // Find reflection for program
608 const GLES::Program* program{nullptr};
610 if(mImpl->mNewPipeline)
612 program = static_cast<const GLES::Program*>(mImpl->mNewPipeline->GetCreateInfo().programState->program);
614 else if(mImpl->mCurrentPipeline)
616 program = static_cast<const GLES::Program*>(mImpl->mCurrentPipeline->GetCreateInfo().programState->program);
621 const auto ptr = reinterpret_cast<const char*>(mImpl->mCurrentStandaloneUBOBinding.buffer->GetCPUAllocatedAddress()) + mImpl->mCurrentStandaloneUBOBinding.offset;
622 // Update program uniforms
623 program->GetImplementation()->UpdateStandaloneUniformBlock(ptr);
627 void Context::BeginRenderPass(const BeginRenderPassDescriptor& renderPassBegin)
629 auto& renderPass = *renderPassBegin.renderPass;
630 auto& renderTarget = *renderPassBegin.renderTarget;
632 const auto& targetInfo = renderTarget.GetCreateInfo();
634 auto& gl = *mImpl->mController.GetGL();
636 if(targetInfo.surface)
639 BindFrameBuffer(GL_FRAMEBUFFER, 0);
641 else if(targetInfo.framebuffer)
643 // bind framebuffer and swap.
644 auto framebuffer = renderTarget.GetFramebuffer();
648 // clear (ideally cache the setup)
650 // In GL we assume that the last attachment is depth/stencil (we may need
651 // to cache extra information inside GLES RenderTarget if we want to be
652 // more specific in case of MRT)
654 const auto& attachments = *renderPass.GetCreateInfo().attachments;
655 const auto& color0 = attachments[0];
658 if(color0.loadOp == AttachmentLoadOp::CLEAR)
660 mask |= GL_COLOR_BUFFER_BIT;
663 // Something goes wrong here if Alpha mask is GL_TRUE
666 const auto clearValues = renderPassBegin.clearValues.Ptr();
668 if(!Dali::Equals(mImpl->mGlStateCache.mClearColor.r, clearValues[0].color.r) ||
669 !Dali::Equals(mImpl->mGlStateCache.mClearColor.g, clearValues[0].color.g) ||
670 !Dali::Equals(mImpl->mGlStateCache.mClearColor.b, clearValues[0].color.b) ||
671 !Dali::Equals(mImpl->mGlStateCache.mClearColor.a, clearValues[0].color.a) ||
672 !mImpl->mGlStateCache.mClearColorSet)
674 gl.ClearColor(clearValues[0].color.r,
675 clearValues[0].color.g,
676 clearValues[0].color.b,
677 clearValues[0].color.a);
679 mImpl->mGlStateCache.mClearColorSet = true;
680 mImpl->mGlStateCache.mClearColor = Vector4(clearValues[0].color.r,
681 clearValues[0].color.g,
682 clearValues[0].color.b,
683 clearValues[0].color.a);
687 // check for depth stencil
688 if(attachments.size() > 1)
690 const auto& depthStencil = attachments.back();
691 if(depthStencil.loadOp == AttachmentLoadOp::CLEAR)
693 if(!mImpl->mGlStateCache.mDepthMaskEnabled)
695 mImpl->mGlStateCache.mDepthMaskEnabled = true;
698 mask |= GL_DEPTH_BUFFER_BIT;
700 if(depthStencil.stencilLoadOp == AttachmentLoadOp::CLEAR)
702 if(mImpl->mGlStateCache.mStencilMask != 0xFF)
704 mImpl->mGlStateCache.mStencilMask = 0xFF;
705 gl.StencilMask(0xFF);
707 mask |= GL_STENCIL_BUFFER_BIT;
711 SetScissorTestEnabled(true);
712 gl.Scissor(renderPassBegin.renderArea.x, renderPassBegin.renderArea.y, renderPassBegin.renderArea.width, renderPassBegin.renderArea.height);
713 ClearBuffer(mask, true);
714 SetScissorTestEnabled(false);
716 mImpl->mCurrentRenderPass = &renderPass;
717 mImpl->mCurrentRenderTarget = &renderTarget;
720 void Context::EndRenderPass(GLES::TextureDependencyChecker& dependencyChecker)
722 if(mImpl->mCurrentRenderTarget)
724 GLES::Framebuffer* framebuffer = mImpl->mCurrentRenderTarget->GetFramebuffer();
727 auto& gl = *mImpl->mController.GetGL();
730 /* @todo Full dependency checking would need to store textures in Begin, and create
731 * fence objects here; but we're going to draw all fbos on shared context in serial,
732 * so no real need (yet). Might want to consider ensuring order of render passes,
733 * but that needs doing in the controller, and would need doing before ProcessCommandQueues.
735 * Currently up to the client to create render tasks in the right order.
738 /* Create fence sync objects. Other contexts can then wait on these fences before reading
741 dependencyChecker.AddTextures(this, framebuffer);
746 void Context::ClearState()
748 mImpl->mCurrentTextureBindings.clear();
751 void Context::ColorMask(bool enabled)
753 if(enabled != mImpl->mGlStateCache.mColorMask)
755 mImpl->mGlStateCache.mColorMask = enabled;
757 auto& gl = *mImpl->mController.GetGL();
758 gl.ColorMask(enabled, enabled, enabled, enabled);
762 void Context::ClearStencilBuffer()
764 ClearBuffer(GL_STENCIL_BUFFER_BIT, false);
767 void Context::ClearDepthBuffer()
769 ClearBuffer(GL_DEPTH_BUFFER_BIT, false);
772 void Context::ClearBuffer(uint32_t mask, bool forceClear)
774 mask = mImpl->mGlStateCache.mFrameBufferStateCache.GetClearMask(mask, forceClear, mImpl->mGlStateCache.mScissorTestEnabled);
777 auto& gl = *mImpl->mController.GetGL();
782 void Context::InvalidateDepthStencilBuffers()
784 auto& gl = *mImpl->mController.GetGL();
786 GLenum attachments[] = {GL_DEPTH, GL_STENCIL};
787 gl.InvalidateFramebuffer(GL_FRAMEBUFFER, 2, attachments);
790 void Context::SetScissorTestEnabled(bool scissorEnabled)
792 if(mImpl->mGlStateCache.mScissorTestEnabled != scissorEnabled)
794 mImpl->mGlStateCache.mScissorTestEnabled = scissorEnabled;
796 auto& gl = *mImpl->mController.GetGL();
799 gl.Enable(GL_SCISSOR_TEST);
803 gl.Disable(GL_SCISSOR_TEST);
808 void Context::SetStencilTestEnable(bool stencilEnable)
810 if(stencilEnable != mImpl->mGlStateCache.mStencilBufferEnabled)
812 mImpl->mGlStateCache.mStencilBufferEnabled = stencilEnable;
814 auto& gl = *mImpl->mController.GetGL();
817 gl.Enable(GL_STENCIL_TEST);
821 gl.Disable(GL_STENCIL_TEST);
826 void Context::StencilMask(uint32_t writeMask)
828 if(writeMask != mImpl->mGlStateCache.mStencilMask)
830 mImpl->mGlStateCache.mStencilMask = writeMask;
832 auto& gl = *mImpl->mController.GetGL();
833 gl.StencilMask(writeMask);
837 void Context::StencilFunc(Graphics::CompareOp compareOp,
839 uint32_t compareMask)
841 if(compareOp != mImpl->mGlStateCache.mStencilFunc ||
842 reference != mImpl->mGlStateCache.mStencilFuncRef ||
843 compareMask != mImpl->mGlStateCache.mStencilFuncMask)
845 mImpl->mGlStateCache.mStencilFunc = compareOp;
846 mImpl->mGlStateCache.mStencilFuncRef = reference;
847 mImpl->mGlStateCache.mStencilFuncMask = compareMask;
849 auto& gl = *mImpl->mController.GetGL();
850 gl.StencilFunc(GLCompareOp(compareOp).op, reference, compareMask);
854 void Context::StencilOp(Graphics::StencilOp failOp,
855 Graphics::StencilOp depthFailOp,
856 Graphics::StencilOp passOp)
858 if(failOp != mImpl->mGlStateCache.mStencilOpFail ||
859 depthFailOp != mImpl->mGlStateCache.mStencilOpDepthFail ||
860 passOp != mImpl->mGlStateCache.mStencilOpDepthPass)
862 mImpl->mGlStateCache.mStencilOpFail = failOp;
863 mImpl->mGlStateCache.mStencilOpDepthFail = depthFailOp;
864 mImpl->mGlStateCache.mStencilOpDepthPass = passOp;
866 auto& gl = *mImpl->mController.GetGL();
867 gl.StencilOp(GLStencilOp(failOp).op, GLStencilOp(depthFailOp).op, GLStencilOp(passOp).op);
871 void Context::SetDepthCompareOp(Graphics::CompareOp compareOp)
873 if(compareOp != mImpl->mGlStateCache.mDepthFunction)
875 mImpl->mGlStateCache.mDepthFunction = compareOp;
876 auto& gl = *mImpl->mController.GetGL();
877 gl.DepthFunc(GLCompareOp(compareOp).op);
881 void Context::SetDepthTestEnable(bool depthTestEnable)
883 if(depthTestEnable != mImpl->mGlStateCache.mDepthBufferEnabled)
885 mImpl->mGlStateCache.mDepthBufferEnabled = depthTestEnable;
887 auto& gl = *mImpl->mController.GetGL();
890 gl.Enable(GL_DEPTH_TEST);
894 gl.Disable(GL_DEPTH_TEST);
899 void Context::SetDepthWriteEnable(bool depthWriteEnable)
901 if(depthWriteEnable != mImpl->mGlStateCache.mDepthMaskEnabled)
903 mImpl->mGlStateCache.mDepthMaskEnabled = depthWriteEnable;
905 auto& gl = *mImpl->mController.GetGL();
906 gl.DepthMask(depthWriteEnable);
910 void Context::ActiveTexture(uint32_t textureBindingIndex)
912 if(mImpl->mGlStateCache.mActiveTextureUnit != textureBindingIndex)
914 mImpl->mGlStateCache.mActiveTextureUnit = textureBindingIndex;
916 auto& gl = *mImpl->mController.GetGL();
917 gl.ActiveTexture(GL_TEXTURE0 + textureBindingIndex);
921 void Context::BindTexture(GLenum target, BoundTextureType textureTypeId, uint32_t textureId)
923 uint32_t typeId = static_cast<uint32_t>(textureTypeId);
924 if(mImpl->mGlStateCache.mBoundTextureId[mImpl->mGlStateCache.mActiveTextureUnit][typeId] != textureId)
926 mImpl->mGlStateCache.mBoundTextureId[mImpl->mGlStateCache.mActiveTextureUnit][typeId] = textureId;
928 auto& gl = *mImpl->mController.GetGL();
929 gl.BindTexture(target, textureId);
933 void Context::GenerateMipmap(GLenum target)
935 auto& gl = *mImpl->mController.GetGL();
936 gl.GenerateMipmap(target);
939 void Context::BindBuffer(GLenum target, uint32_t bufferId)
941 if(mImpl->mGlStateCache.mBoundArrayBufferId != bufferId)
943 mImpl->mGlStateCache.mBoundArrayBufferId = bufferId;
945 auto& gl = *mImpl->mController.GetGL();
946 gl.BindBuffer(target, bufferId);
950 void Context::DrawBuffers(uint32_t count, const GLenum* buffers)
952 mImpl->mGlStateCache.mFrameBufferStateCache.DrawOperation(mImpl->mGlStateCache.mColorMask,
953 mImpl->mGlStateCache.DepthBufferWriteEnabled(),
954 mImpl->mGlStateCache.StencilBufferWriteEnabled());
956 auto& gl = *mImpl->mController.GetGL();
957 gl.DrawBuffers(count, buffers);
960 void Context::BindFrameBuffer(GLenum target, uint32_t bufferId)
962 mImpl->mGlStateCache.mFrameBufferStateCache.SetCurrentFrameBuffer(bufferId);
964 auto& gl = *mImpl->mController.GetGL();
965 gl.BindFramebuffer(target, bufferId);
968 void Context::GenFramebuffers(uint32_t count, uint32_t* framebuffers)
970 auto& gl = *mImpl->mController.GetGL();
971 gl.GenFramebuffers(count, framebuffers);
973 mImpl->mGlStateCache.mFrameBufferStateCache.FrameBuffersCreated(count, framebuffers);
976 void Context::DeleteFramebuffers(uint32_t count, uint32_t* framebuffers)
978 mImpl->mGlStateCache.mFrameBufferStateCache.FrameBuffersDeleted(count, framebuffers);
980 auto& gl = *mImpl->mController.GetGL();
981 gl.DeleteFramebuffers(count, framebuffers);
984 GLStateCache& Context::GetGLStateCache()
986 return mImpl->mGlStateCache;
989 void Context::GlContextCreated()
991 if(!mImpl->mGlContextCreated)
993 mImpl->mGlContextCreated = true;
995 // Set the initial GL state
996 mImpl->InitializeGlState();
1000 void Context::GlContextDestroyed()
1002 mImpl->mGlContextCreated = false;
1005 void Context::InvalidateCachedPipeline(GLES::Pipeline* pipeline)
1007 // Since the pipeline is deleted, invalidate the cached pipeline.
1008 if(mImpl->mCurrentPipeline == &pipeline->GetPipeline())
1010 mImpl->mCurrentPipeline = nullptr;
1014 void Context::PrepareForNativeRendering()
1016 // this should be pretty much constant
1017 auto display = eglGetCurrentDisplay();
1018 auto drawSurface = eglGetCurrentSurface(EGL_DRAW);
1019 auto readSurface = eglGetCurrentSurface(EGL_READ);
1020 auto context = eglGetCurrentContext();
1022 // push the surface and context data to the impl
1023 // It's needed to restore context
1024 if(!mImpl->mCacheEGLGraphicsContext)
1026 mImpl->mCacheDrawWriteSurface = drawSurface;
1027 mImpl->mCacheDrawReadSurface = readSurface;
1028 mImpl->mCacheEGLGraphicsContext = context;
1031 if(!mImpl->mNativeDrawContext)
1033 EGLint configId{0u};
1034 eglQueryContext(display, mImpl->mController.GetSharedContext(), EGL_CONFIG_ID, &configId);
1036 EGLint configAttribs[3];
1037 configAttribs[0] = EGL_CONFIG_ID;
1038 configAttribs[1] = configId;
1039 configAttribs[2] = EGL_NONE;
1043 if(eglChooseConfig(display, configAttribs, &config, 1, &numConfigs) != EGL_TRUE)
1045 DALI_LOG_ERROR("eglChooseConfig failed!\n");
1049 auto version = int(mImpl->mController.GetGLESVersion());
1051 std::vector<EGLint> attribs;
1052 attribs.push_back(EGL_CONTEXT_MAJOR_VERSION_KHR);
1053 attribs.push_back(version / 10);
1054 attribs.push_back(EGL_CONTEXT_MINOR_VERSION_KHR);
1055 attribs.push_back(version % 10);
1056 attribs.push_back(EGL_NONE);
1058 mImpl->mNativeDrawContext = eglCreateContext(display, config, mImpl->mController.GetSharedContext(), attribs.data());
1059 if(mImpl->mNativeDrawContext == EGL_NO_CONTEXT)
1061 DALI_LOG_ERROR("eglCreateContext failed!\n");
1066 eglMakeCurrent(display, drawSurface, readSurface, mImpl->mNativeDrawContext);
1069 void Context::RestoreFromNativeRendering()
1071 auto display = eglGetCurrentDisplay();
1073 // bring back original context
1074 eglMakeCurrent(display, mImpl->mCacheDrawWriteSurface, mImpl->mCacheDrawReadSurface, mImpl->mCacheEGLGraphicsContext);
1077 } // namespace Dali::Graphics::GLES