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"
33 namespace Dali::Graphics::GLES
37 Impl(EglGraphicsController& controller)
38 : mController(controller)
45 * Binds (and creates) VAO
47 * VAO is fixed per program so it has to be created only once assuming
48 * that VertexInputState has been set correctly for the pipeline.
51 void BindProgramVAO(GLES::ProgramImpl* program, const VertexInputState& vertexInputState)
53 auto& gl = *mController.GetGL();
54 auto iter = mProgramVAOMap.find(program);
55 if(iter != mProgramVAOMap.end())
57 if(mProgramVAOCurrentState != iter->second)
59 mProgramVAOCurrentState = iter->second;
60 gl.BindVertexArray(iter->second);
66 gl.GenVertexArrays(1, &vao);
67 gl.BindVertexArray(vao);
68 mProgramVAOMap[program] = vao;
69 for(const auto& attr : vertexInputState.attributes)
71 gl.EnableVertexAttribArray(attr.location);
74 mProgramVAOCurrentState = vao;
78 * Sets the initial GL state.
80 void InitializeGlState()
82 auto& gl = *mController.GetGL();
84 mGlStateCache.mClearColorSet = false;
85 mGlStateCache.mColorMask = true;
86 mGlStateCache.mStencilMask = 0xFF;
87 mGlStateCache.mBlendEnabled = false;
88 mGlStateCache.mDepthBufferEnabled = false;
89 mGlStateCache.mDepthMaskEnabled = false;
90 mGlStateCache.mScissorTestEnabled = false;
91 mGlStateCache.mStencilBufferEnabled = false;
93 gl.Disable(GL_DITHER);
95 mGlStateCache.mBoundArrayBufferId = 0;
96 mGlStateCache.mBoundElementArrayBufferId = 0;
97 mGlStateCache.mActiveTextureUnit = 0;
99 mGlStateCache.mBlendFuncSeparateSrcRGB = BlendFactor::ONE;
100 mGlStateCache.mBlendFuncSeparateDstRGB = BlendFactor::ZERO;
101 mGlStateCache.mBlendFuncSeparateSrcAlpha = BlendFactor::ONE;
102 mGlStateCache.mBlendFuncSeparateDstAlpha = BlendFactor::ZERO;
104 // initial state is GL_FUNC_ADD for both RGB and Alpha blend modes
105 mGlStateCache.mBlendEquationSeparateModeRGB = BlendOp::ADD;
106 mGlStateCache.mBlendEquationSeparateModeAlpha = BlendOp::ADD;
108 mGlStateCache.mCullFaceMode = CullMode::NONE; //By default cullface is disabled, front face is set to CCW and cull face is set to back
110 //Initialze vertex attribute cache
111 memset(&mGlStateCache.mVertexAttributeCachedState, 0, sizeof(mGlStateCache.mVertexAttributeCachedState));
112 memset(&mGlStateCache.mVertexAttributeCurrentState, 0, sizeof(mGlStateCache.mVertexAttributeCurrentState));
114 //Initialize bound 2d texture cache
115 memset(&mGlStateCache.mBoundTextureId, 0, sizeof(mGlStateCache.mBoundTextureId));
117 mGlStateCache.mFrameBufferStateCache.Reset();
121 * Flushes vertex attribute location changes to the driver
123 void FlushVertexAttributeLocations()
125 auto& gl = *mController.GetGL();
127 for(unsigned int i = 0; i < MAX_ATTRIBUTE_CACHE_SIZE; ++i)
129 // see if the cached state is different to the actual state
130 if(mGlStateCache.mVertexAttributeCurrentState[i] != mGlStateCache.mVertexAttributeCachedState[i])
132 // it's different so make the change to the driver and update the cached state
133 mGlStateCache.mVertexAttributeCurrentState[i] = mGlStateCache.mVertexAttributeCachedState[i];
135 if(mGlStateCache.mVertexAttributeCurrentState[i])
137 gl.EnableVertexAttribArray(i);
141 gl.DisableVertexAttribArray(i);
148 * Either enables or disables a vertex attribute location in the cache
149 * The cahnges won't take affect until FlushVertexAttributeLocations is called
150 * @param location attribute location
151 * @param state attribute state
153 void SetVertexAttributeLocation(unsigned int location, bool state)
155 auto& gl = *mController.GetGL();
157 if(location >= MAX_ATTRIBUTE_CACHE_SIZE)
159 // not cached, make the gl call through context
162 gl.EnableVertexAttribArray(location);
166 gl.DisableVertexAttribArray(location);
171 // set the cached state, it will be set at the next draw call
172 // if it's different from the current driver state
173 mGlStateCache.mVertexAttributeCachedState[location] = state;
177 EglGraphicsController& mController;
179 const GLES::PipelineImpl* mCurrentPipeline{nullptr}; ///< Currently bound pipeline
180 const GLES::PipelineImpl* mNewPipeline{nullptr}; ///< New pipeline to be set on flush
182 std::vector<Graphics::TextureBinding> mCurrentTextureBindings{};
183 std::vector<Graphics::SamplerBinding> mCurrentSamplerBindings{};
184 GLES::IndexBufferBindingDescriptor mCurrentIndexBufferBinding{};
186 struct VertexBufferBinding
188 GLES::Buffer* buffer{nullptr};
192 // Currently bound buffers
193 std::vector<VertexBufferBindingDescriptor> mCurrentVertexBufferBindings{};
195 // Currently bound UBOs (check if it's needed per program!)
196 std::vector<UniformBufferBindingDescriptor> mCurrentUBOBindings{};
197 UniformBufferBindingDescriptor mCurrentStandaloneUBOBinding{};
199 // Current render pass and render target
200 const GLES::RenderTarget* mCurrentRenderTarget{nullptr};
201 const GLES::RenderPass* mCurrentRenderPass{nullptr};
203 // Each context must have own VAOs as they cannot be shared
204 std::map<GLES::ProgramImpl*, uint32_t> mProgramVAOMap; ///< GL program-VAO map
205 uint32_t mProgramVAOCurrentState{0u}; ///< Currently bound VAO
206 GLStateCache mGlStateCache{}; ///< GL status cache
208 bool mGlContextCreated{false}; ///< True if the OpenGL context has been created
211 Context::Context(EglGraphicsController& controller)
213 mImpl = std::make_unique<Impl>(controller);
216 Context::~Context() = default;
218 void Context::Flush(bool reset, const GLES::DrawCallDescriptor& drawCall)
220 auto& gl = *mImpl->mController.GetGL();
222 static const bool hasGLES3(mImpl->mController.GetGLESVersion() >= GLESVersion::GLES_30);
224 // early out if neither current nor new pipelines are set
225 // this behaviour may be valid so no assert
226 if(!mImpl->mCurrentPipeline && !mImpl->mNewPipeline)
231 // Execute states if pipeline is changed
232 const auto currentProgram = mImpl->mCurrentPipeline ? static_cast<const GLES::Program*>(mImpl->mCurrentPipeline->GetCreateInfo().programState->program) : nullptr;
234 // case when new pipeline has been set
235 const GLES::Program* newProgram = nullptr;
237 if(mImpl->mNewPipeline)
239 newProgram = static_cast<const GLES::Program*>(mImpl->mNewPipeline->GetCreateInfo().programState->program);
242 if(mImpl->mNewPipeline && mImpl->mCurrentPipeline != mImpl->mNewPipeline)
244 if(!currentProgram || currentProgram->GetImplementation()->GetGlProgram() != newProgram->GetImplementation()->GetGlProgram())
246 mImpl->mNewPipeline->Bind(newProgram->GetImplementation()->GetGlProgram());
252 // Resolve rasterization state
253 ResolveRasterizationState();
256 // Resolve uniform buffers
257 ResolveUniformBuffers();
260 // Map binding# to sampler location
261 const auto& reflection = !newProgram ? currentProgram->GetReflection() : newProgram->GetReflection();
262 const auto& samplers = reflection.GetSamplers();
263 for(const auto& binding : mImpl->mCurrentTextureBindings)
265 auto texture = const_cast<GLES::Texture*>(static_cast<const GLES::Texture*>(binding.texture));
267 // Texture may not have been initialized yet...(tbm_surface timing issue?)
268 if(!texture->GetGLTexture())
270 // Attempt to reinitialize
271 // @todo need to put this somewhere else where it isn't const.
272 // Maybe post it back on end of initialize queue if initialization fails?
273 texture->InitializeResource();
276 texture->Bind(binding);
278 texture->Prepare(); // @todo also non-const.
280 if(binding.binding < samplers.size()) // binding maps to texture unit. (texture bindings should also be in binding order)
282 // Offset is set to the lexical offset within the frag shader, map it to the texture unit
283 // @todo Explicitly set the texture unit through the graphics interface
284 gl.Uniform1i(samplers[binding.binding].location, samplers[binding.binding].offset);
288 // for each attribute bind vertices
290 const auto& pipelineState = mImpl->mNewPipeline ? mImpl->mNewPipeline->GetCreateInfo() : mImpl->mCurrentPipeline->GetCreateInfo();
291 const auto& vertexInputState = pipelineState.vertexInputState;
295 mImpl->BindProgramVAO(static_cast<const GLES::Program*>(pipelineState.programState->program)->GetImplementation(), *vertexInputState);
298 for(const auto& attr : vertexInputState->attributes)
303 mImpl->SetVertexAttributeLocation(attr.location, true);
306 const auto& bufferSlot = mImpl->mCurrentVertexBufferBindings[attr.binding];
307 const auto& bufferBinding = vertexInputState->bufferBindings[attr.binding];
309 auto glesBuffer = bufferSlot.buffer->GetGLBuffer();
312 BindBuffer(GL_ARRAY_BUFFER, glesBuffer);
314 gl.VertexAttribPointer(attr.location,
315 GLVertexFormat(attr.format).size,
316 GLVertexFormat(attr.format).format,
318 bufferBinding.stride,
319 reinterpret_cast<void*>(attr.offset));
323 const auto& ia = pipelineState.inputAssemblyState;
328 switch(drawCall.type)
330 case DrawCallDescriptor::Type::DRAW:
332 mImpl->mGlStateCache.mFrameBufferStateCache.DrawOperation(mImpl->mGlStateCache.mColorMask,
333 mImpl->mGlStateCache.DepthBufferWriteEnabled(),
334 mImpl->mGlStateCache.StencilBufferWriteEnabled());
335 // For GLES3+ we use VAO, for GLES2 internal cache
338 mImpl->FlushVertexAttributeLocations();
341 gl.DrawArrays(GLESTopology(ia->topology),
342 drawCall.draw.firstVertex,
343 drawCall.draw.vertexCount);
346 case DrawCallDescriptor::Type::DRAW_INDEXED:
348 const auto& binding = mImpl->mCurrentIndexBufferBinding;
349 BindBuffer(GL_ELEMENT_ARRAY_BUFFER, binding.buffer->GetGLBuffer());
351 mImpl->mGlStateCache.mFrameBufferStateCache.DrawOperation(mImpl->mGlStateCache.mColorMask,
352 mImpl->mGlStateCache.DepthBufferWriteEnabled(),
353 mImpl->mGlStateCache.StencilBufferWriteEnabled());
355 // For GLES3+ we use VAO, for GLES2 internal cache
358 mImpl->FlushVertexAttributeLocations();
361 auto indexBufferFormat = GLIndexFormat(binding.format).format;
362 gl.DrawElements(GLESTopology(ia->topology),
363 drawCall.drawIndexed.indexCount,
365 reinterpret_cast<void*>(binding.offset));
368 case DrawCallDescriptor::Type::DRAW_INDEXED_INDIRECT:
377 if(mImpl->mNewPipeline)
379 mImpl->mCurrentPipeline = mImpl->mNewPipeline;
380 mImpl->mNewPipeline = nullptr;
384 void Context::BindTextures(const Graphics::TextureBinding* bindings, uint32_t count)
386 // for each texture allocate slot
387 for(auto i = 0u; i < count; ++i)
389 auto& binding = bindings[i];
391 // Resize binding array if needed
392 if(mImpl->mCurrentTextureBindings.size() <= binding.binding)
394 mImpl->mCurrentTextureBindings.resize(binding.binding + 1);
396 // Store the binding details
397 mImpl->mCurrentTextureBindings[binding.binding] = binding;
401 void Context::BindVertexBuffers(const GLES::VertexBufferBindingDescriptor* bindings, uint32_t count)
403 if(count > mImpl->mCurrentVertexBufferBindings.size())
405 mImpl->mCurrentVertexBufferBindings.resize(count);
407 // Copy only set slots
408 std::copy_if(bindings, bindings + count, mImpl->mCurrentVertexBufferBindings.begin(), [](auto& item) {
409 return (nullptr != item.buffer);
413 void Context::BindIndexBuffer(const IndexBufferBindingDescriptor& indexBufferBinding)
415 mImpl->mCurrentIndexBufferBinding = indexBufferBinding;
418 void Context::BindPipeline(const GLES::Pipeline* newPipeline)
420 DALI_ASSERT_ALWAYS(newPipeline && "Invalid pipeline");
421 mImpl->mNewPipeline = &newPipeline->GetPipeline();
424 void Context::BindUniformBuffers(const UniformBufferBindingDescriptor* uboBindings,
426 const UniformBufferBindingDescriptor& standaloneBindings)
428 if(standaloneBindings.buffer)
430 mImpl->mCurrentStandaloneUBOBinding = standaloneBindings;
433 if(uboCount >= mImpl->mCurrentUBOBindings.size())
435 mImpl->mCurrentUBOBindings.resize(uboCount + 1);
438 auto it = uboBindings;
439 for(auto i = 0u; i < uboCount; ++i)
443 mImpl->mCurrentUBOBindings[i] = *it;
448 void Context::ResolveBlendState()
450 const auto& currentBlendState = mImpl->mCurrentPipeline ? mImpl->mCurrentPipeline->GetCreateInfo().colorBlendState : nullptr;
451 const auto& newBlendState = mImpl->mNewPipeline->GetCreateInfo().colorBlendState;
453 // TODO: prevent leaking the state
459 auto& gl = *mImpl->mController.GetGL();
461 if(!currentBlendState || currentBlendState->blendEnable != newBlendState->blendEnable)
463 if(newBlendState->blendEnable != mImpl->mGlStateCache.mBlendEnabled)
465 mImpl->mGlStateCache.mBlendEnabled = newBlendState->blendEnable;
466 newBlendState->blendEnable ? gl.Enable(GL_BLEND) : gl.Disable(GL_BLEND);
470 if(!newBlendState->blendEnable)
475 BlendFactor newSrcRGB(newBlendState->srcColorBlendFactor);
476 BlendFactor newDstRGB(newBlendState->dstColorBlendFactor);
477 BlendFactor newSrcAlpha(newBlendState->srcAlphaBlendFactor);
478 BlendFactor newDstAlpha(newBlendState->dstAlphaBlendFactor);
480 if(!currentBlendState ||
481 currentBlendState->srcColorBlendFactor != newSrcRGB ||
482 currentBlendState->dstColorBlendFactor != newDstRGB ||
483 currentBlendState->srcAlphaBlendFactor != newSrcAlpha ||
484 currentBlendState->dstAlphaBlendFactor != newDstAlpha)
486 if((mImpl->mGlStateCache.mBlendFuncSeparateSrcRGB != newSrcRGB) ||
487 (mImpl->mGlStateCache.mBlendFuncSeparateDstRGB != newDstRGB) ||
488 (mImpl->mGlStateCache.mBlendFuncSeparateSrcAlpha != newSrcAlpha) ||
489 (mImpl->mGlStateCache.mBlendFuncSeparateDstAlpha != newDstAlpha))
491 mImpl->mGlStateCache.mBlendFuncSeparateSrcRGB = newSrcRGB;
492 mImpl->mGlStateCache.mBlendFuncSeparateDstRGB = newDstRGB;
493 mImpl->mGlStateCache.mBlendFuncSeparateSrcAlpha = newSrcAlpha;
494 mImpl->mGlStateCache.mBlendFuncSeparateDstAlpha = newDstAlpha;
496 if(newSrcRGB == newSrcAlpha && newDstRGB == newDstAlpha)
498 gl.BlendFunc(GLBlendFunc(newSrcRGB), GLBlendFunc(newDstRGB));
502 gl.BlendFuncSeparate(GLBlendFunc(newSrcRGB), GLBlendFunc(newDstRGB), GLBlendFunc(newSrcAlpha), GLBlendFunc(newDstAlpha));
507 if(!currentBlendState ||
508 currentBlendState->colorBlendOp != newBlendState->colorBlendOp ||
509 currentBlendState->alphaBlendOp != newBlendState->alphaBlendOp)
511 if(mImpl->mGlStateCache.mBlendEquationSeparateModeRGB != newBlendState->colorBlendOp ||
512 mImpl->mGlStateCache.mBlendEquationSeparateModeAlpha != newBlendState->alphaBlendOp)
514 mImpl->mGlStateCache.mBlendEquationSeparateModeRGB = newBlendState->colorBlendOp;
515 mImpl->mGlStateCache.mBlendEquationSeparateModeAlpha = newBlendState->alphaBlendOp;
517 if(newBlendState->colorBlendOp == newBlendState->alphaBlendOp)
519 gl.BlendEquation(GLBlendOp(newBlendState->colorBlendOp));
520 if(newBlendState->colorBlendOp >= Graphics::ADVANCED_BLEND_OPTIONS_START)
527 gl.BlendEquationSeparate(GLBlendOp(newBlendState->colorBlendOp), GLBlendOp(newBlendState->alphaBlendOp));
533 void Context::ResolveRasterizationState()
535 const auto& currentRasterizationState = mImpl->mCurrentPipeline ? mImpl->mCurrentPipeline->GetCreateInfo().rasterizationState : nullptr;
536 const auto& newRasterizationState = mImpl->mNewPipeline->GetCreateInfo().rasterizationState;
538 // TODO: prevent leaking the state
539 if(!newRasterizationState)
544 auto& gl = *mImpl->mController.GetGL();
546 if(!currentRasterizationState ||
547 currentRasterizationState->cullMode != newRasterizationState->cullMode)
549 if(mImpl->mGlStateCache.mCullFaceMode != newRasterizationState->cullMode)
551 mImpl->mGlStateCache.mCullFaceMode = newRasterizationState->cullMode;
552 if(newRasterizationState->cullMode == CullMode::NONE)
554 gl.Disable(GL_CULL_FACE);
558 gl.Enable(GL_CULL_FACE);
559 gl.CullFace(GLCullMode(newRasterizationState->cullMode));
563 // TODO: implement polygon mode (fill, line, points)
564 // seems like we don't support it (no glPolygonMode())
567 void Context::ResolveUniformBuffers()
569 // Resolve standalone uniforms if we have binding
570 if(mImpl->mCurrentStandaloneUBOBinding.buffer)
572 ResolveStandaloneUniforms();
576 void Context::ResolveStandaloneUniforms()
578 // Find reflection for program
579 const auto program = static_cast<const GLES::Program*>(mImpl->mNewPipeline->GetCreateInfo().programState->program);
580 const auto ptr = reinterpret_cast<const char*>(mImpl->mCurrentStandaloneUBOBinding.buffer->GetCPUAllocatedAddress()) + mImpl->mCurrentStandaloneUBOBinding.offset;
582 // Update program uniforms
583 program->GetImplementation()->UpdateStandaloneUniformBlock(ptr);
586 void Context::BeginRenderPass(const BeginRenderPassDescriptor& renderPassBegin)
588 auto& renderPass = *renderPassBegin.renderPass;
589 auto& renderTarget = *renderPassBegin.renderTarget;
591 const auto& targetInfo = renderTarget.GetCreateInfo();
593 auto& gl = *mImpl->mController.GetGL();
595 if(targetInfo.surface)
598 BindFrameBuffer(GL_FRAMEBUFFER, 0);
600 else if(targetInfo.framebuffer)
602 // bind framebuffer and swap.
603 renderTarget.GetFramebuffer()->Bind();
606 // clear (ideally cache the setup)
608 // In GL we assume that the last attachment is depth/stencil (we may need
609 // to cache extra information inside GLES RenderTarget if we want to be
610 // more specific in case of MRT)
612 const auto& attachments = *renderPass.GetCreateInfo().attachments;
613 const auto& color0 = attachments[0];
615 if(color0.loadOp == AttachmentLoadOp::CLEAR)
617 mask |= GL_COLOR_BUFFER_BIT;
620 // Something goes wrong here if Alpha mask is GL_TRUE
623 const auto clearValues = renderPassBegin.clearValues.Ptr();
625 if(!mImpl->mGlStateCache.mClearColorSet ||
626 mImpl->mGlStateCache.mClearColor.r != clearValues[0].color.r ||
627 mImpl->mGlStateCache.mClearColor.g != clearValues[0].color.g ||
628 mImpl->mGlStateCache.mClearColor.b != clearValues[0].color.b ||
629 mImpl->mGlStateCache.mClearColor.a != clearValues[0].color.a)
631 gl.ClearColor(clearValues[0].color.r,
632 clearValues[0].color.g,
633 clearValues[0].color.b,
634 clearValues[0].color.a);
636 mImpl->mGlStateCache.mClearColorSet = true;
637 mImpl->mGlStateCache.mClearColor = Vector4(clearValues[0].color.r,
638 clearValues[0].color.g,
639 clearValues[0].color.b,
640 clearValues[0].color.a);
644 // check for depth stencil
645 if(attachments.size() > 1)
647 const auto& depthStencil = attachments.back();
648 if(depthStencil.loadOp == AttachmentLoadOp::CLEAR)
650 if(!mImpl->mGlStateCache.mDepthMaskEnabled)
652 mImpl->mGlStateCache.mDepthMaskEnabled = true;
655 mask |= GL_DEPTH_BUFFER_BIT;
657 if(depthStencil.stencilLoadOp == AttachmentLoadOp::CLEAR)
659 if(mImpl->mGlStateCache.mStencilMask != 0xFF)
661 mImpl->mGlStateCache.mStencilMask = 0xFF;
662 gl.StencilMask(0xFF);
664 mask |= GL_STENCIL_BUFFER_BIT;
668 SetScissorTestEnabled(true);
669 gl.Scissor(renderPassBegin.renderArea.x, renderPassBegin.renderArea.y, renderPassBegin.renderArea.width, renderPassBegin.renderArea.height);
670 ClearBuffer(mask, true);
671 SetScissorTestEnabled(false);
673 mImpl->mCurrentRenderPass = &renderPass;
674 mImpl->mCurrentRenderTarget = &renderTarget;
677 void Context::EndRenderPass()
679 if(mImpl->mCurrentRenderTarget)
681 if(mImpl->mCurrentRenderTarget->GetFramebuffer())
683 auto& gl = *mImpl->mController.GetGL();
689 void Context::ClearState()
691 mImpl->mCurrentTextureBindings.clear();
694 void Context::ColorMask(bool enabled)
696 if(enabled != mImpl->mGlStateCache.mColorMask)
698 mImpl->mGlStateCache.mColorMask = enabled;
700 auto& gl = *mImpl->mController.GetGL();
701 gl.ColorMask(enabled, enabled, enabled, enabled);
705 void Context::ClearStencilBuffer()
707 ClearBuffer(GL_STENCIL_BUFFER_BIT, false);
710 void Context::ClearDepthBuffer()
712 ClearBuffer(GL_DEPTH_BUFFER_BIT, false);
715 void Context::ClearBuffer(uint32_t mask, bool forceClear)
717 mask = mImpl->mGlStateCache.mFrameBufferStateCache.GetClearMask(mask, forceClear, mImpl->mGlStateCache.mScissorTestEnabled);
720 auto& gl = *mImpl->mController.GetGL();
725 void Context::InvalidateDepthStencilBuffers()
727 auto& gl = *mImpl->mController.GetGL();
729 GLenum attachments[] = {GL_DEPTH, GL_STENCIL};
730 gl.InvalidateFramebuffer(GL_FRAMEBUFFER, 2, attachments);
733 void Context::SetScissorTestEnabled(bool scissorEnabled)
735 if(mImpl->mGlStateCache.mScissorTestEnabled != scissorEnabled)
737 mImpl->mGlStateCache.mScissorTestEnabled = scissorEnabled;
739 auto& gl = *mImpl->mController.GetGL();
742 gl.Enable(GL_SCISSOR_TEST);
746 gl.Disable(GL_SCISSOR_TEST);
751 void Context::SetStencilTestEnable(bool stencilEnable)
753 if(stencilEnable != mImpl->mGlStateCache.mStencilBufferEnabled)
755 mImpl->mGlStateCache.mStencilBufferEnabled = stencilEnable;
757 auto& gl = *mImpl->mController.GetGL();
760 gl.Enable(GL_STENCIL_TEST);
764 gl.Disable(GL_STENCIL_TEST);
769 void Context::StencilMask(uint32_t writeMask)
771 if(writeMask != mImpl->mGlStateCache.mStencilMask)
773 mImpl->mGlStateCache.mStencilMask = writeMask;
775 auto& gl = *mImpl->mController.GetGL();
776 gl.StencilMask(writeMask);
780 void Context::StencilFunc(Graphics::CompareOp compareOp,
782 uint32_t compareMask)
784 if(compareOp != mImpl->mGlStateCache.mStencilFunc ||
785 reference != mImpl->mGlStateCache.mStencilFuncRef ||
786 compareMask != mImpl->mGlStateCache.mStencilFuncMask)
788 mImpl->mGlStateCache.mStencilFunc = compareOp;
789 mImpl->mGlStateCache.mStencilFuncRef = reference;
790 mImpl->mGlStateCache.mStencilFuncMask = compareMask;
792 auto& gl = *mImpl->mController.GetGL();
793 gl.StencilFunc(GLCompareOp(compareOp).op, reference, compareMask);
797 void Context::StencilOp(Graphics::StencilOp failOp,
798 Graphics::StencilOp depthFailOp,
799 Graphics::StencilOp passOp)
801 if(failOp != mImpl->mGlStateCache.mStencilOpFail ||
802 depthFailOp != mImpl->mGlStateCache.mStencilOpDepthFail ||
803 passOp != mImpl->mGlStateCache.mStencilOpDepthPass)
805 mImpl->mGlStateCache.mStencilOpFail = failOp;
806 mImpl->mGlStateCache.mStencilOpDepthFail = depthFailOp;
807 mImpl->mGlStateCache.mStencilOpDepthPass = passOp;
809 auto& gl = *mImpl->mController.GetGL();
810 gl.StencilOp(GLStencilOp(failOp).op, GLStencilOp(depthFailOp).op, GLStencilOp(passOp).op);
814 void Context::SetDepthCompareOp(Graphics::CompareOp compareOp)
816 if(compareOp != mImpl->mGlStateCache.mDepthFunction)
818 mImpl->mGlStateCache.mDepthFunction = compareOp;
819 auto& gl = *mImpl->mController.GetGL();
820 gl.DepthFunc(GLCompareOp(compareOp).op);
824 void Context::SetDepthTestEnable(bool depthTestEnable)
826 if(depthTestEnable != mImpl->mGlStateCache.mDepthBufferEnabled)
828 mImpl->mGlStateCache.mDepthBufferEnabled = depthTestEnable;
830 auto& gl = *mImpl->mController.GetGL();
833 gl.Enable(GL_DEPTH_TEST);
837 gl.Disable(GL_DEPTH_TEST);
842 void Context::SetDepthWriteEnable(bool depthWriteEnable)
844 if(depthWriteEnable != mImpl->mGlStateCache.mDepthMaskEnabled)
846 mImpl->mGlStateCache.mDepthMaskEnabled = depthWriteEnable;
848 auto& gl = *mImpl->mController.GetGL();
849 gl.DepthMask(depthWriteEnable);
853 void Context::ActiveTexture(uint32_t textureBindingIndex)
855 if(mImpl->mGlStateCache.mActiveTextureUnit != textureBindingIndex)
857 mImpl->mGlStateCache.mActiveTextureUnit = textureBindingIndex;
859 auto& gl = *mImpl->mController.GetGL();
860 gl.ActiveTexture(GL_TEXTURE0 + textureBindingIndex);
864 void Context::BindTexture(GLenum target, BoundTextureType textureTypeId, uint32_t textureId)
866 uint32_t typeId = static_cast<uint32_t>(textureTypeId);
867 if(mImpl->mGlStateCache.mActiveTextureUnit >= MAX_TEXTURE_UNITS || typeId >= MAX_TEXTURE_TARGET)
869 DALI_LOG_ERROR("Invalid index (%d, %d)\n", mImpl->mGlStateCache.mActiveTextureUnit, typeId);
873 if(mImpl->mGlStateCache.mBoundTextureId[mImpl->mGlStateCache.mActiveTextureUnit][typeId] != textureId)
875 mImpl->mGlStateCache.mBoundTextureId[mImpl->mGlStateCache.mActiveTextureUnit][typeId] = textureId;
877 auto& gl = *mImpl->mController.GetGL();
878 gl.BindTexture(target, textureId);
882 void Context::GenerateMipmap(GLenum target)
884 auto& gl = *mImpl->mController.GetGL();
885 gl.GenerateMipmap(target);
888 void Context::BindBuffer(GLenum target, uint32_t bufferId)
890 if(mImpl->mGlStateCache.mBoundArrayBufferId != bufferId)
892 mImpl->mGlStateCache.mBoundArrayBufferId = bufferId;
894 auto& gl = *mImpl->mController.GetGL();
895 gl.BindBuffer(target, bufferId);
899 void Context::DrawBuffers(uint32_t count, const GLenum* buffers)
901 mImpl->mGlStateCache.mFrameBufferStateCache.DrawOperation(mImpl->mGlStateCache.mColorMask,
902 mImpl->mGlStateCache.DepthBufferWriteEnabled(),
903 mImpl->mGlStateCache.StencilBufferWriteEnabled());
905 auto& gl = *mImpl->mController.GetGL();
906 gl.DrawBuffers(count, buffers);
909 void Context::BindFrameBuffer(GLenum target, uint32_t bufferId)
911 mImpl->mGlStateCache.mFrameBufferStateCache.SetCurrentFrameBuffer(bufferId);
913 auto& gl = *mImpl->mController.GetGL();
914 gl.BindFramebuffer(target, bufferId);
917 void Context::GenFramebuffers(uint32_t count, uint32_t* framebuffers)
919 auto& gl = *mImpl->mController.GetGL();
920 gl.GenFramebuffers(count, framebuffers);
922 mImpl->mGlStateCache.mFrameBufferStateCache.FrameBuffersCreated(count, framebuffers);
925 void Context::DeleteFramebuffers(uint32_t count, uint32_t* framebuffers)
927 mImpl->mGlStateCache.mFrameBufferStateCache.FrameBuffersDeleted(count, framebuffers);
929 auto& gl = *mImpl->mController.GetGL();
930 gl.DeleteFramebuffers(count, framebuffers);
933 GLStateCache& Context::GetGLStateCache()
935 return mImpl->mGlStateCache;
938 void Context::GlContextCreated()
940 if(!mImpl->mGlContextCreated)
942 mImpl->mGlContextCreated = true;
944 // Set the initial GL state
945 mImpl->InitializeGlState();
949 void Context::GlContextDestroyed()
951 mImpl->mGlContextCreated = false;
954 void Context::InvalidateCachedPipeline(GLES::Pipeline* pipeline)
956 // Since the pipeline is deleted, invalidate the cached pipeline.
957 if(mImpl->mCurrentPipeline == &pipeline->GetPipeline())
959 mImpl->mCurrentPipeline = nullptr;
963 } // namespace Dali::Graphics::GLES