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));
523 gl.BlendEquationSeparate(GLBlendOp(newBlendState->colorBlendOp), GLBlendOp(newBlendState->alphaBlendOp));
529 void Context::ResolveRasterizationState()
531 const auto& currentRasterizationState = mImpl->mCurrentPipeline ? mImpl->mCurrentPipeline->GetCreateInfo().rasterizationState : nullptr;
532 const auto& newRasterizationState = mImpl->mNewPipeline->GetCreateInfo().rasterizationState;
534 // TODO: prevent leaking the state
535 if(!newRasterizationState)
540 auto& gl = *mImpl->mController.GetGL();
542 if(!currentRasterizationState ||
543 currentRasterizationState->cullMode != newRasterizationState->cullMode)
545 if(mImpl->mGlStateCache.mCullFaceMode != newRasterizationState->cullMode)
547 mImpl->mGlStateCache.mCullFaceMode = newRasterizationState->cullMode;
548 if(newRasterizationState->cullMode == CullMode::NONE)
550 gl.Disable(GL_CULL_FACE);
554 gl.Enable(GL_CULL_FACE);
555 gl.CullFace(GLCullMode(newRasterizationState->cullMode));
559 // TODO: implement polygon mode (fill, line, points)
560 // seems like we don't support it (no glPolygonMode())
563 void Context::ResolveUniformBuffers()
565 // Resolve standalone uniforms if we have binding
566 if(mImpl->mCurrentStandaloneUBOBinding.buffer)
568 ResolveStandaloneUniforms();
572 void Context::ResolveStandaloneUniforms()
574 // Find reflection for program
575 const auto program = static_cast<const GLES::Program*>(mImpl->mNewPipeline->GetCreateInfo().programState->program);
576 const auto ptr = reinterpret_cast<const char*>(mImpl->mCurrentStandaloneUBOBinding.buffer->GetCPUAllocatedAddress()) + mImpl->mCurrentStandaloneUBOBinding.offset;
578 // Update program uniforms
579 program->GetImplementation()->UpdateStandaloneUniformBlock(ptr);
582 void Context::BeginRenderPass(const BeginRenderPassDescriptor& renderPassBegin)
584 auto& renderPass = *renderPassBegin.renderPass;
585 auto& renderTarget = *renderPassBegin.renderTarget;
587 const auto& targetInfo = renderTarget.GetCreateInfo();
589 auto& gl = *mImpl->mController.GetGL();
591 if(targetInfo.surface)
594 BindFrameBuffer(GL_FRAMEBUFFER, 0);
596 else if(targetInfo.framebuffer)
598 // bind framebuffer and swap.
599 renderTarget.GetFramebuffer()->Bind();
602 // clear (ideally cache the setup)
604 // In GL we assume that the last attachment is depth/stencil (we may need
605 // to cache extra information inside GLES RenderTarget if we want to be
606 // more specific in case of MRT)
608 const auto& attachments = *renderPass.GetCreateInfo().attachments;
609 const auto& color0 = attachments[0];
611 if(color0.loadOp == AttachmentLoadOp::CLEAR)
613 mask |= GL_COLOR_BUFFER_BIT;
616 // Something goes wrong here if Alpha mask is GL_TRUE
619 const auto clearValues = renderPassBegin.clearValues.Ptr();
621 if(!mImpl->mGlStateCache.mClearColorSet ||
622 mImpl->mGlStateCache.mClearColor.r != clearValues[0].color.r ||
623 mImpl->mGlStateCache.mClearColor.g != clearValues[0].color.g ||
624 mImpl->mGlStateCache.mClearColor.b != clearValues[0].color.b ||
625 mImpl->mGlStateCache.mClearColor.a != clearValues[0].color.a)
627 gl.ClearColor(clearValues[0].color.r,
628 clearValues[0].color.g,
629 clearValues[0].color.b,
630 clearValues[0].color.a);
632 mImpl->mGlStateCache.mClearColorSet = true;
633 mImpl->mGlStateCache.mClearColor = Vector4(clearValues[0].color.r,
634 clearValues[0].color.g,
635 clearValues[0].color.b,
636 clearValues[0].color.a);
640 // check for depth stencil
641 if(attachments.size() > 1)
643 const auto& depthStencil = attachments.back();
644 if(depthStencil.loadOp == AttachmentLoadOp::CLEAR)
646 if(!mImpl->mGlStateCache.mDepthMaskEnabled)
648 mImpl->mGlStateCache.mDepthMaskEnabled = true;
651 mask |= GL_DEPTH_BUFFER_BIT;
653 if(depthStencil.stencilLoadOp == AttachmentLoadOp::CLEAR)
655 if(mImpl->mGlStateCache.mStencilMask != 0xFF)
657 mImpl->mGlStateCache.mStencilMask = 0xFF;
658 gl.StencilMask(0xFF);
660 mask |= GL_STENCIL_BUFFER_BIT;
664 SetScissorTestEnabled(true);
665 gl.Scissor(renderPassBegin.renderArea.x, renderPassBegin.renderArea.y, renderPassBegin.renderArea.width, renderPassBegin.renderArea.height);
666 ClearBuffer(mask, true);
667 SetScissorTestEnabled(false);
669 mImpl->mCurrentRenderPass = &renderPass;
670 mImpl->mCurrentRenderTarget = &renderTarget;
673 void Context::EndRenderPass()
675 if(mImpl->mCurrentRenderTarget)
677 if(mImpl->mCurrentRenderTarget->GetFramebuffer())
679 auto& gl = *mImpl->mController.GetGL();
685 void Context::ClearState()
687 mImpl->mCurrentTextureBindings.clear();
690 void Context::ColorMask(bool enabled)
692 if(enabled != mImpl->mGlStateCache.mColorMask)
694 mImpl->mGlStateCache.mColorMask = enabled;
696 auto& gl = *mImpl->mController.GetGL();
697 gl.ColorMask(enabled, enabled, enabled, enabled);
701 void Context::ClearStencilBuffer()
703 ClearBuffer(GL_STENCIL_BUFFER_BIT, false);
706 void Context::ClearDepthBuffer()
708 ClearBuffer(GL_DEPTH_BUFFER_BIT, false);
711 void Context::ClearBuffer(uint32_t mask, bool forceClear)
713 mask = mImpl->mGlStateCache.mFrameBufferStateCache.GetClearMask(mask, forceClear, mImpl->mGlStateCache.mScissorTestEnabled);
716 auto& gl = *mImpl->mController.GetGL();
721 void Context::InvalidateDepthStencilBuffers()
723 auto& gl = *mImpl->mController.GetGL();
725 GLenum attachments[] = {GL_DEPTH, GL_STENCIL};
726 gl.InvalidateFramebuffer(GL_FRAMEBUFFER, 2, attachments);
729 void Context::SetScissorTestEnabled(bool scissorEnabled)
731 if(mImpl->mGlStateCache.mScissorTestEnabled != scissorEnabled)
733 mImpl->mGlStateCache.mScissorTestEnabled = scissorEnabled;
735 auto& gl = *mImpl->mController.GetGL();
738 gl.Enable(GL_SCISSOR_TEST);
742 gl.Disable(GL_SCISSOR_TEST);
747 void Context::SetStencilTestEnable(bool stencilEnable)
749 if(stencilEnable != mImpl->mGlStateCache.mStencilBufferEnabled)
751 mImpl->mGlStateCache.mStencilBufferEnabled = stencilEnable;
753 auto& gl = *mImpl->mController.GetGL();
756 gl.Enable(GL_STENCIL_TEST);
760 gl.Disable(GL_STENCIL_TEST);
765 void Context::StencilMask(uint32_t writeMask)
767 if(writeMask != mImpl->mGlStateCache.mStencilMask)
769 mImpl->mGlStateCache.mStencilMask = writeMask;
771 auto& gl = *mImpl->mController.GetGL();
772 gl.StencilMask(writeMask);
776 void Context::StencilFunc(Graphics::CompareOp compareOp,
778 uint32_t compareMask)
780 if(compareOp != mImpl->mGlStateCache.mStencilFunc ||
781 reference != mImpl->mGlStateCache.mStencilFuncRef ||
782 compareMask != mImpl->mGlStateCache.mStencilFuncMask)
784 mImpl->mGlStateCache.mStencilFunc = compareOp;
785 mImpl->mGlStateCache.mStencilFuncRef = reference;
786 mImpl->mGlStateCache.mStencilFuncMask = compareMask;
788 auto& gl = *mImpl->mController.GetGL();
789 gl.StencilFunc(GLCompareOp(compareOp).op, reference, compareMask);
793 void Context::StencilOp(Graphics::StencilOp failOp,
794 Graphics::StencilOp depthFailOp,
795 Graphics::StencilOp passOp)
797 if(failOp != mImpl->mGlStateCache.mStencilOpFail ||
798 depthFailOp != mImpl->mGlStateCache.mStencilOpDepthFail ||
799 passOp != mImpl->mGlStateCache.mStencilOpDepthPass)
801 mImpl->mGlStateCache.mStencilOpFail = failOp;
802 mImpl->mGlStateCache.mStencilOpDepthFail = depthFailOp;
803 mImpl->mGlStateCache.mStencilOpDepthPass = passOp;
805 auto& gl = *mImpl->mController.GetGL();
806 gl.StencilOp(GLStencilOp(failOp).op, GLStencilOp(depthFailOp).op, GLStencilOp(passOp).op);
810 void Context::SetDepthCompareOp(Graphics::CompareOp compareOp)
812 if(compareOp != mImpl->mGlStateCache.mDepthFunction)
814 mImpl->mGlStateCache.mDepthFunction = compareOp;
815 auto& gl = *mImpl->mController.GetGL();
816 gl.DepthFunc(GLCompareOp(compareOp).op);
820 void Context::SetDepthTestEnable(bool depthTestEnable)
822 if(depthTestEnable != mImpl->mGlStateCache.mDepthBufferEnabled)
824 mImpl->mGlStateCache.mDepthBufferEnabled = depthTestEnable;
826 auto& gl = *mImpl->mController.GetGL();
829 gl.Enable(GL_DEPTH_TEST);
833 gl.Disable(GL_DEPTH_TEST);
838 void Context::SetDepthWriteEnable(bool depthWriteEnable)
840 if(depthWriteEnable != mImpl->mGlStateCache.mDepthMaskEnabled)
842 mImpl->mGlStateCache.mDepthMaskEnabled = depthWriteEnable;
844 auto& gl = *mImpl->mController.GetGL();
845 gl.DepthMask(depthWriteEnable);
849 void Context::ActiveTexture(uint32_t textureBindingIndex)
851 if(mImpl->mGlStateCache.mActiveTextureUnit != textureBindingIndex)
853 mImpl->mGlStateCache.mActiveTextureUnit = textureBindingIndex;
855 auto& gl = *mImpl->mController.GetGL();
856 gl.ActiveTexture(GL_TEXTURE0 + textureBindingIndex);
860 void Context::BindTexture(GLenum target, BoundTextureType textureTypeId, uint32_t textureId)
862 uint32_t typeId = static_cast<uint32_t>(textureTypeId);
863 if(mImpl->mGlStateCache.mBoundTextureId[mImpl->mGlStateCache.mActiveTextureUnit][typeId] != textureId)
865 mImpl->mGlStateCache.mBoundTextureId[mImpl->mGlStateCache.mActiveTextureUnit][typeId] = textureId;
867 auto& gl = *mImpl->mController.GetGL();
868 gl.BindTexture(target, textureId);
872 void Context::GenerateMipmap(GLenum target)
874 auto& gl = *mImpl->mController.GetGL();
875 gl.GenerateMipmap(target);
878 void Context::BindBuffer(GLenum target, uint32_t bufferId)
880 if(mImpl->mGlStateCache.mBoundArrayBufferId != bufferId)
882 mImpl->mGlStateCache.mBoundArrayBufferId = bufferId;
884 auto& gl = *mImpl->mController.GetGL();
885 gl.BindBuffer(target, bufferId);
889 void Context::DrawBuffers(uint32_t count, const GLenum* buffers)
891 mImpl->mGlStateCache.mFrameBufferStateCache.DrawOperation(mImpl->mGlStateCache.mColorMask,
892 mImpl->mGlStateCache.DepthBufferWriteEnabled(),
893 mImpl->mGlStateCache.StencilBufferWriteEnabled());
895 auto& gl = *mImpl->mController.GetGL();
896 gl.DrawBuffers(count, buffers);
899 void Context::BindFrameBuffer(GLenum target, uint32_t bufferId)
901 mImpl->mGlStateCache.mFrameBufferStateCache.SetCurrentFrameBuffer(bufferId);
903 auto& gl = *mImpl->mController.GetGL();
904 gl.BindFramebuffer(target, bufferId);
907 void Context::GenFramebuffers(uint32_t count, uint32_t* framebuffers)
909 auto& gl = *mImpl->mController.GetGL();
910 gl.GenFramebuffers(count, framebuffers);
912 mImpl->mGlStateCache.mFrameBufferStateCache.FrameBuffersCreated(count, framebuffers);
915 void Context::DeleteFramebuffers(uint32_t count, uint32_t* framebuffers)
917 mImpl->mGlStateCache.mFrameBufferStateCache.FrameBuffersDeleted(count, framebuffers);
919 auto& gl = *mImpl->mController.GetGL();
920 gl.DeleteFramebuffers(count, framebuffers);
923 GLStateCache& Context::GetGLStateCache()
925 return mImpl->mGlStateCache;
928 void Context::GlContextCreated()
930 if(!mImpl->mGlContextCreated)
932 mImpl->mGlContextCreated = true;
934 // Set the initial GL state
935 mImpl->InitializeGlState();
939 void Context::GlContextDestroyed()
941 mImpl->mGlContextCreated = false;
944 void Context::InvalidateCachedPipeline(GLES::Pipeline* pipeline)
946 // Since the pipeline is deleted, invalidate the cached pipeline.
947 if(mImpl->mCurrentPipeline == &pipeline->GetPipeline())
949 mImpl->mCurrentPipeline = nullptr;
953 } // namespace Dali::Graphics::GLES