2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "gles-context.h"
19 #include <dali/integration-api/adaptor-framework/render-surface-interface.h>
20 #include <dali/integration-api/gl-abstraction.h>
21 #include <dali/integration-api/gl-defines.h>
22 #include <dali/internal/graphics/common/graphics-interface.h>
24 #include "egl-graphics-controller.h"
25 #include "gles-graphics-buffer.h"
26 #include "gles-graphics-pipeline.h"
27 #include "gles-graphics-program.h"
28 #include "gles-graphics-render-pass.h"
29 #include "gles-graphics-render-target.h"
31 namespace Dali::Graphics::GLES
35 Impl(EglGraphicsController& controller)
36 : mController(controller)
43 * Sets the initial GL state.
45 void InitializeGlState()
47 auto& gl = *mController.GetGL();
49 mGlStateCache.mClearColorSet = false;
50 mGlStateCache.mColorMask = true;
51 mGlStateCache.mStencilMask = 0xFF;
52 mGlStateCache.mBlendEnabled = false;
53 mGlStateCache.mDepthBufferEnabled = false;
54 mGlStateCache.mDepthMaskEnabled = false;
55 mGlStateCache.mScissorTestEnabled = false;
56 mGlStateCache.mStencilBufferEnabled = false;
58 gl.Disable(GL_DITHER);
60 mGlStateCache.mBoundArrayBufferId = 0;
61 mGlStateCache.mBoundElementArrayBufferId = 0;
62 mGlStateCache.mActiveTextureUnit = 0;
64 mGlStateCache.mBlendFuncSeparateSrcRGB = BlendFactor::ONE;
65 mGlStateCache.mBlendFuncSeparateDstRGB = BlendFactor::ZERO;
66 mGlStateCache.mBlendFuncSeparateSrcAlpha = BlendFactor::ONE;
67 mGlStateCache.mBlendFuncSeparateDstAlpha = BlendFactor::ZERO;
69 // initial state is GL_FUNC_ADD for both RGB and Alpha blend modes
70 mGlStateCache.mBlendEquationSeparateModeRGB = BlendOp::ADD;
71 mGlStateCache.mBlendEquationSeparateModeAlpha = BlendOp::ADD;
73 mGlStateCache.mCullFaceMode = CullMode::NONE; //By default cullface is disabled, front face is set to CCW and cull face is set to back
75 //Initialze vertex attribute cache
76 memset(&mGlStateCache.mVertexAttributeCachedState, 0, sizeof(mGlStateCache.mVertexAttributeCachedState));
77 memset(&mGlStateCache.mVertexAttributeCurrentState, 0, sizeof(mGlStateCache.mVertexAttributeCurrentState));
79 //Initialize bound 2d texture cache
80 memset(&mGlStateCache.mBoundTextureId, 0, sizeof(mGlStateCache.mBoundTextureId));
82 mGlStateCache.mFrameBufferStateCache.Reset();
86 * Flushes vertex attribute location changes to the driver
88 void FlushVertexAttributeLocations()
90 auto& gl = *mController.GetGL();
92 for(unsigned int i = 0; i < MAX_ATTRIBUTE_CACHE_SIZE; ++i)
94 // see if the cached state is different to the actual state
95 if(mGlStateCache.mVertexAttributeCurrentState[i] != mGlStateCache.mVertexAttributeCachedState[i])
97 // it's different so make the change to the driver and update the cached state
98 mGlStateCache.mVertexAttributeCurrentState[i] = mGlStateCache.mVertexAttributeCachedState[i];
100 if(mGlStateCache.mVertexAttributeCurrentState[i])
102 gl.EnableVertexAttribArray(i);
106 gl.DisableVertexAttribArray(i);
113 * Either enables or disables a vertex attribute location in the cache
114 * The cahnges won't take affect until FlushVertexAttributeLocations is called
115 * @param location attribute location
116 * @param state attribute state
118 void SetVertexAttributeLocation(unsigned int location, bool state)
120 auto& gl = *mController.GetGL();
122 if(location >= MAX_ATTRIBUTE_CACHE_SIZE)
124 // not cached, make the gl call through context
127 gl.EnableVertexAttribArray(location);
131 gl.DisableVertexAttribArray(location);
136 // set the cached state, it will be set at the next draw call
137 // if it's different from the current driver state
138 mGlStateCache.mVertexAttributeCachedState[location] = state;
142 EglGraphicsController& mController;
144 const GLES::PipelineImpl* mCurrentPipeline{nullptr}; ///< Currently bound pipeline
145 const GLES::PipelineImpl* mNewPipeline{nullptr}; ///< New pipeline to be set on flush
147 std::vector<Graphics::TextureBinding> mCurrentTextureBindings{};
148 std::vector<Graphics::SamplerBinding> mCurrentSamplerBindings{};
149 GLES::IndexBufferBindingDescriptor mCurrentIndexBufferBinding{};
151 struct VertexBufferBinding
153 GLES::Buffer* buffer{nullptr};
157 // Currently bound buffers
158 std::vector<VertexBufferBindingDescriptor> mCurrentVertexBufferBindings{};
160 // Currently bound UBOs (check if it's needed per program!)
161 std::vector<UniformBufferBindingDescriptor> mCurrentUBOBindings{};
162 UniformBufferBindingDescriptor mCurrentStandaloneUBOBinding{};
164 // Current render pass and render target
165 const GLES::RenderTarget* mCurrentRenderTarget{nullptr};
166 const GLES::RenderPass* mCurrentRenderPass{nullptr};
168 GLStateCache mGlStateCache{}; ///< GL status cache
170 bool mGlContextCreated{false}; ///< True if the OpenGL context has been created
173 Context::Context(EglGraphicsController& controller)
175 mImpl = std::make_unique<Impl>(controller);
178 Context::~Context() = default;
180 void Context::Flush(bool reset, const GLES::DrawCallDescriptor& drawCall)
182 auto& gl = *mImpl->mController.GetGL();
184 // early out if neither current nor new pipelines are set
185 // this behaviour may be valid so no assert
186 if(!mImpl->mCurrentPipeline && !mImpl->mNewPipeline)
191 // Execute states if pipeline is changed
192 const auto currentProgram = mImpl->mCurrentPipeline ? static_cast<const GLES::Program*>(mImpl->mCurrentPipeline->GetCreateInfo().programState->program) : nullptr;
194 // case when new pipeline has been set
195 const GLES::Program* newProgram = nullptr;
197 if(mImpl->mNewPipeline)
199 newProgram = static_cast<const GLES::Program*>(mImpl->mNewPipeline->GetCreateInfo().programState->program);
202 if(mImpl->mNewPipeline && mImpl->mCurrentPipeline != mImpl->mNewPipeline)
204 if(!currentProgram || currentProgram->GetImplementation()->GetGlProgram() != newProgram->GetImplementation()->GetGlProgram())
206 mImpl->mNewPipeline->Bind(newProgram->GetImplementation()->GetGlProgram());
212 // Resolve rasterization state
213 ResolveRasterizationState();
216 // Resolve uniform buffers
217 ResolveUniformBuffers();
220 // Map binding# to sampler location
221 const auto& reflection = !newProgram ? currentProgram->GetReflection() : newProgram->GetReflection();
222 const auto& samplers = reflection.GetSamplers();
223 for(const auto& binding : mImpl->mCurrentTextureBindings)
225 auto texture = const_cast<GLES::Texture*>(static_cast<const GLES::Texture*>(binding.texture));
227 // Texture may not have been initialized yet...(tbm_surface timing issue?)
228 if(!texture->GetGLTexture())
230 // Attempt to reinitialize
231 // @todo need to put this somewhere else where it isn't const.
232 // Maybe post it back on end of initialize queue if initialization fails?
233 texture->InitializeResource();
236 texture->Bind(binding);
238 texture->Prepare(); // @todo also non-const.
240 if(binding.binding < samplers.size()) // binding maps to texture unit. (texture bindings should also be in binding order)
242 // Offset is set to the lexical offset within the frag shader, map it to the texture unit
243 // @todo Explicitly set the texture unit through the graphics interface
244 gl.Uniform1i(samplers[binding.binding].location, samplers[binding.binding].offset);
248 // for each attribute bind vertices
249 const auto& pipelineState = mImpl->mNewPipeline ? mImpl->mNewPipeline->GetCreateInfo() : mImpl->mCurrentPipeline->GetCreateInfo();
250 const auto& vi = pipelineState.vertexInputState;
251 for(const auto& attr : vi->attributes)
254 mImpl->SetVertexAttributeLocation(attr.location, true);
256 const auto& bufferSlot = mImpl->mCurrentVertexBufferBindings[attr.binding];
257 const auto& bufferBinding = vi->bufferBindings[attr.binding];
259 auto glesBuffer = bufferSlot.buffer->GetGLBuffer();
262 BindBuffer(GL_ARRAY_BUFFER, glesBuffer);
264 gl.VertexAttribPointer(attr.location,
265 GLVertexFormat(attr.format).size,
266 GLVertexFormat(attr.format).format,
268 bufferBinding.stride,
269 reinterpret_cast<void*>(attr.offset));
273 const auto& ia = pipelineState.inputAssemblyState;
278 switch(drawCall.type)
280 case DrawCallDescriptor::Type::DRAW:
282 mImpl->mGlStateCache.mFrameBufferStateCache.DrawOperation(mImpl->mGlStateCache.mColorMask,
283 mImpl->mGlStateCache.DepthBufferWriteEnabled(),
284 mImpl->mGlStateCache.StencilBufferWriteEnabled());
285 mImpl->FlushVertexAttributeLocations();
287 gl.DrawArrays(GLESTopology(ia->topology),
288 drawCall.draw.firstVertex,
289 drawCall.draw.vertexCount);
292 case DrawCallDescriptor::Type::DRAW_INDEXED:
294 const auto& binding = mImpl->mCurrentIndexBufferBinding;
295 BindBuffer(GL_ELEMENT_ARRAY_BUFFER, binding.buffer->GetGLBuffer());
297 mImpl->mGlStateCache.mFrameBufferStateCache.DrawOperation(mImpl->mGlStateCache.mColorMask,
298 mImpl->mGlStateCache.DepthBufferWriteEnabled(),
299 mImpl->mGlStateCache.StencilBufferWriteEnabled());
300 mImpl->FlushVertexAttributeLocations();
302 auto indexBufferFormat = GLIndexFormat(binding.format).format;
303 gl.DrawElements(GLESTopology(ia->topology),
304 drawCall.drawIndexed.indexCount,
306 reinterpret_cast<void*>(binding.offset));
309 case DrawCallDescriptor::Type::DRAW_INDEXED_INDIRECT:
318 if(mImpl->mNewPipeline)
320 mImpl->mCurrentPipeline = mImpl->mNewPipeline;
321 mImpl->mNewPipeline = nullptr;
325 void Context::BindTextures(const std::vector<Graphics::TextureBinding>& bindings)
327 // for each texture allocate slot
328 for(const auto& binding : bindings)
330 // Resize binding array if needed
331 if(mImpl->mCurrentTextureBindings.size() <= binding.binding)
333 mImpl->mCurrentTextureBindings.resize(binding.binding + 1);
335 // Store the binding details
336 mImpl->mCurrentTextureBindings[binding.binding] = binding;
340 void Context::BindVertexBuffers(const std::vector<GLES::VertexBufferBindingDescriptor>& bindings)
342 if(bindings.size() > mImpl->mCurrentVertexBufferBindings.size())
344 mImpl->mCurrentVertexBufferBindings.resize(bindings.size());
346 // Copy only set slots
347 std::copy_if(bindings.begin(), bindings.end(), mImpl->mCurrentVertexBufferBindings.begin(), [](auto& item) {
348 return (nullptr != item.buffer);
352 void Context::BindIndexBuffer(const IndexBufferBindingDescriptor& indexBufferBinding)
354 mImpl->mCurrentIndexBufferBinding = indexBufferBinding;
357 void Context::BindPipeline(const GLES::Pipeline* newPipeline)
359 DALI_ASSERT_ALWAYS(newPipeline && "Invalid pipeline");
360 mImpl->mNewPipeline = &newPipeline->GetPipeline();
363 void Context::BindUniformBuffers(const std::vector<UniformBufferBindingDescriptor>& uboBindings,
364 const UniformBufferBindingDescriptor& standaloneBindings)
366 if(standaloneBindings.buffer)
368 mImpl->mCurrentStandaloneUBOBinding = standaloneBindings;
371 if(uboBindings.size() >= mImpl->mCurrentUBOBindings.size())
373 mImpl->mCurrentUBOBindings.resize(uboBindings.size() + 1);
376 auto it = uboBindings.begin();
377 for(auto i = 0u; i < uboBindings.size(); ++i)
381 mImpl->mCurrentUBOBindings[i] = *it;
386 void Context::ResolveBlendState()
388 const auto& currentBlendState = mImpl->mCurrentPipeline ? mImpl->mCurrentPipeline->GetCreateInfo().colorBlendState : nullptr;
389 const auto& newBlendState = mImpl->mNewPipeline->GetCreateInfo().colorBlendState;
391 // TODO: prevent leaking the state
397 auto& gl = *mImpl->mController.GetGL();
399 if(!currentBlendState || currentBlendState->blendEnable != newBlendState->blendEnable)
401 if(newBlendState->blendEnable != mImpl->mGlStateCache.mBlendEnabled)
403 mImpl->mGlStateCache.mBlendEnabled = newBlendState->blendEnable;
404 newBlendState->blendEnable ? gl.Enable(GL_BLEND) : gl.Disable(GL_BLEND);
408 if(!newBlendState->blendEnable)
413 BlendFactor newSrcRGB(newBlendState->srcColorBlendFactor);
414 BlendFactor newDstRGB(newBlendState->dstColorBlendFactor);
415 BlendFactor newSrcAlpha(newBlendState->srcAlphaBlendFactor);
416 BlendFactor newDstAlpha(newBlendState->dstAlphaBlendFactor);
418 if(!currentBlendState ||
419 currentBlendState->srcColorBlendFactor != newSrcRGB ||
420 currentBlendState->dstColorBlendFactor != newDstRGB ||
421 currentBlendState->srcAlphaBlendFactor != newSrcAlpha ||
422 currentBlendState->dstAlphaBlendFactor != newDstAlpha)
424 if((mImpl->mGlStateCache.mBlendFuncSeparateSrcRGB != newSrcRGB) ||
425 (mImpl->mGlStateCache.mBlendFuncSeparateDstRGB != newDstRGB) ||
426 (mImpl->mGlStateCache.mBlendFuncSeparateSrcAlpha != newSrcAlpha) ||
427 (mImpl->mGlStateCache.mBlendFuncSeparateDstAlpha != newDstAlpha))
429 mImpl->mGlStateCache.mBlendFuncSeparateSrcRGB = newSrcRGB;
430 mImpl->mGlStateCache.mBlendFuncSeparateDstRGB = newDstRGB;
431 mImpl->mGlStateCache.mBlendFuncSeparateSrcAlpha = newSrcAlpha;
432 mImpl->mGlStateCache.mBlendFuncSeparateDstAlpha = newDstAlpha;
434 if(newSrcRGB == newSrcAlpha && newDstRGB == newDstAlpha)
436 gl.BlendFunc(GLBlendFunc(newSrcRGB), GLBlendFunc(newDstRGB));
440 gl.BlendFuncSeparate(GLBlendFunc(newSrcRGB), GLBlendFunc(newDstRGB), GLBlendFunc(newSrcAlpha), GLBlendFunc(newDstAlpha));
445 if(!currentBlendState ||
446 currentBlendState->colorBlendOp != newBlendState->colorBlendOp ||
447 currentBlendState->alphaBlendOp != newBlendState->alphaBlendOp)
449 if(mImpl->mGlStateCache.mBlendEquationSeparateModeRGB != newBlendState->colorBlendOp ||
450 mImpl->mGlStateCache.mBlendEquationSeparateModeAlpha != newBlendState->alphaBlendOp)
452 mImpl->mGlStateCache.mBlendEquationSeparateModeRGB = newBlendState->colorBlendOp;
453 mImpl->mGlStateCache.mBlendEquationSeparateModeAlpha = newBlendState->alphaBlendOp;
455 if(newBlendState->colorBlendOp == newBlendState->alphaBlendOp)
457 gl.BlendEquation(GLBlendOp(newBlendState->colorBlendOp));
461 gl.BlendEquationSeparate(GLBlendOp(newBlendState->colorBlendOp), GLBlendOp(newBlendState->alphaBlendOp));
467 void Context::ResolveRasterizationState()
469 const auto& currentRasterizationState = mImpl->mCurrentPipeline ? mImpl->mCurrentPipeline->GetCreateInfo().rasterizationState : nullptr;
470 const auto& newRasterizationState = mImpl->mNewPipeline->GetCreateInfo().rasterizationState;
472 // TODO: prevent leaking the state
473 if(!newRasterizationState)
478 auto& gl = *mImpl->mController.GetGL();
480 if(!currentRasterizationState ||
481 currentRasterizationState->cullMode != newRasterizationState->cullMode)
483 if(mImpl->mGlStateCache.mCullFaceMode != newRasterizationState->cullMode)
485 mImpl->mGlStateCache.mCullFaceMode = newRasterizationState->cullMode;
486 if(newRasterizationState->cullMode == CullMode::NONE)
488 gl.Disable(GL_CULL_FACE);
492 gl.Enable(GL_CULL_FACE);
493 gl.CullFace(GLCullMode(newRasterizationState->cullMode));
497 // TODO: implement polygon mode (fill, line, points)
498 // seems like we don't support it (no glPolygonMode())
501 void Context::ResolveUniformBuffers()
503 // Resolve standalone uniforms if we have binding
504 if(mImpl->mCurrentStandaloneUBOBinding.buffer)
506 ResolveStandaloneUniforms();
510 void Context::ResolveStandaloneUniforms()
512 // Find reflection for program
513 const auto program = static_cast<const GLES::Program*>(mImpl->mNewPipeline->GetCreateInfo().programState->program);
514 const auto ptr = reinterpret_cast<const char*>(mImpl->mCurrentStandaloneUBOBinding.buffer->GetCPUAllocatedAddress()) + mImpl->mCurrentStandaloneUBOBinding.offset;
516 // Update program uniforms
517 program->GetImplementation()->UpdateStandaloneUniformBlock(ptr);
520 void Context::BeginRenderPass(const BeginRenderPassDescriptor& renderPassBegin)
522 auto& renderPass = *renderPassBegin.renderPass;
523 auto& renderTarget = *renderPassBegin.renderTarget;
525 const auto& targetInfo = renderTarget.GetCreateInfo();
527 auto& gl = *mImpl->mController.GetGL();
529 if(targetInfo.surface)
532 BindFrameBuffer(GL_FRAMEBUFFER, 0);
534 else if(targetInfo.framebuffer)
536 // bind framebuffer and swap.
537 renderTarget.GetFramebuffer()->Bind();
540 // clear (ideally cache the setup)
542 // In GL we assume that the last attachment is depth/stencil (we may need
543 // to cache extra information inside GLES RenderTarget if we want to be
544 // more specific in case of MRT)
546 const auto& attachments = *renderPass.GetCreateInfo().attachments;
547 const auto& color0 = attachments[0];
549 if(color0.loadOp == AttachmentLoadOp::CLEAR)
551 mask |= GL_COLOR_BUFFER_BIT;
554 // Something goes wrong here if Alpha mask is GL_TRUE
557 if(!mImpl->mGlStateCache.mClearColorSet ||
558 mImpl->mGlStateCache.mClearColor.r != renderPassBegin.clearValues[0].color.r ||
559 mImpl->mGlStateCache.mClearColor.g != renderPassBegin.clearValues[0].color.g ||
560 mImpl->mGlStateCache.mClearColor.b != renderPassBegin.clearValues[0].color.b ||
561 mImpl->mGlStateCache.mClearColor.a != renderPassBegin.clearValues[0].color.a)
563 gl.ClearColor(renderPassBegin.clearValues[0].color.r,
564 renderPassBegin.clearValues[0].color.g,
565 renderPassBegin.clearValues[0].color.b,
566 renderPassBegin.clearValues[0].color.a);
568 mImpl->mGlStateCache.mClearColorSet = true;
569 mImpl->mGlStateCache.mClearColor = Vector4(renderPassBegin.clearValues[0].color.r,
570 renderPassBegin.clearValues[0].color.g,
571 renderPassBegin.clearValues[0].color.b,
572 renderPassBegin.clearValues[0].color.a);
576 // check for depth stencil
577 if(attachments.size() > 1)
579 const auto& depthStencil = attachments.back();
580 if(depthStencil.loadOp == AttachmentLoadOp::CLEAR)
582 if(!mImpl->mGlStateCache.mDepthMaskEnabled)
584 mImpl->mGlStateCache.mDepthMaskEnabled = true;
587 mask |= GL_DEPTH_BUFFER_BIT;
589 if(depthStencil.stencilLoadOp == AttachmentLoadOp::CLEAR)
591 if(mImpl->mGlStateCache.mStencilMask != 0xFF)
593 mImpl->mGlStateCache.mStencilMask = 0xFF;
594 gl.StencilMask(0xFF);
596 mask |= GL_STENCIL_BUFFER_BIT;
600 SetScissorTestEnabled(true);
601 gl.Scissor(renderPassBegin.renderArea.x, renderPassBegin.renderArea.y, renderPassBegin.renderArea.width, renderPassBegin.renderArea.height);
602 ClearBuffer(mask, true);
603 SetScissorTestEnabled(false);
605 mImpl->mCurrentRenderPass = &renderPass;
606 mImpl->mCurrentRenderTarget = &renderTarget;
609 void Context::EndRenderPass()
611 if(mImpl->mCurrentRenderTarget)
613 if(mImpl->mCurrentRenderTarget->GetFramebuffer())
615 auto& gl = *mImpl->mController.GetGL();
621 void Context::ClearState()
623 mImpl->mCurrentTextureBindings.clear();
626 void Context::ColorMask(bool enabled)
628 if(enabled != mImpl->mGlStateCache.mColorMask)
630 mImpl->mGlStateCache.mColorMask = enabled;
632 auto& gl = *mImpl->mController.GetGL();
633 gl.ColorMask(enabled, enabled, enabled, enabled);
637 void Context::ClearStencilBuffer()
639 ClearBuffer(GL_STENCIL_BUFFER_BIT, false);
642 void Context::ClearDepthBuffer()
644 ClearBuffer(GL_DEPTH_BUFFER_BIT, false);
647 void Context::ClearBuffer(uint32_t mask, bool forceClear)
649 mask = mImpl->mGlStateCache.mFrameBufferStateCache.GetClearMask(mask, forceClear, mImpl->mGlStateCache.mScissorTestEnabled);
652 auto& gl = *mImpl->mController.GetGL();
657 void Context::SetScissorTestEnabled(bool scissorEnabled)
659 if(mImpl->mGlStateCache.mScissorTestEnabled != scissorEnabled)
661 mImpl->mGlStateCache.mScissorTestEnabled = scissorEnabled;
663 auto& gl = *mImpl->mController.GetGL();
666 gl.Enable(GL_SCISSOR_TEST);
670 gl.Disable(GL_SCISSOR_TEST);
675 void Context::SetStencilTestEnable(bool stencilEnable)
677 if(stencilEnable != mImpl->mGlStateCache.mStencilBufferEnabled)
679 mImpl->mGlStateCache.mStencilBufferEnabled = stencilEnable;
681 auto& gl = *mImpl->mController.GetGL();
684 gl.Enable(GL_STENCIL_TEST);
688 gl.Disable(GL_STENCIL_TEST);
693 void Context::StencilMask(uint32_t writeMask)
695 if(writeMask != mImpl->mGlStateCache.mStencilMask)
697 mImpl->mGlStateCache.mStencilMask = writeMask;
699 auto& gl = *mImpl->mController.GetGL();
700 gl.StencilMask(writeMask);
704 void Context::StencilFunc(Graphics::CompareOp compareOp,
706 uint32_t compareMask)
708 if(compareOp != mImpl->mGlStateCache.mStencilFunc ||
709 reference != mImpl->mGlStateCache.mStencilFuncRef ||
710 compareMask != mImpl->mGlStateCache.mStencilFuncMask)
712 mImpl->mGlStateCache.mStencilFunc = compareOp;
713 mImpl->mGlStateCache.mStencilFuncRef = reference;
714 mImpl->mGlStateCache.mStencilFuncMask = compareMask;
716 auto& gl = *mImpl->mController.GetGL();
717 gl.StencilFunc(GLCompareOp(compareOp).op, reference, compareMask);
721 void Context::StencilOp(Graphics::StencilOp failOp,
722 Graphics::StencilOp depthFailOp,
723 Graphics::StencilOp passOp)
725 if(failOp != mImpl->mGlStateCache.mStencilOpFail ||
726 depthFailOp != mImpl->mGlStateCache.mStencilOpDepthFail ||
727 passOp != mImpl->mGlStateCache.mStencilOpDepthPass)
729 mImpl->mGlStateCache.mStencilOpFail = failOp;
730 mImpl->mGlStateCache.mStencilOpDepthFail = depthFailOp;
731 mImpl->mGlStateCache.mStencilOpDepthPass = passOp;
733 auto& gl = *mImpl->mController.GetGL();
734 gl.StencilOp(GLStencilOp(failOp).op, GLStencilOp(depthFailOp).op, GLStencilOp(passOp).op);
738 void Context::SetDepthCompareOp(Graphics::CompareOp compareOp)
740 if(compareOp != mImpl->mGlStateCache.mDepthFunction)
742 mImpl->mGlStateCache.mDepthFunction = compareOp;
743 auto& gl = *mImpl->mController.GetGL();
744 gl.DepthFunc(GLCompareOp(compareOp).op);
748 void Context::SetDepthTestEnable(bool depthTestEnable)
750 if(depthTestEnable != mImpl->mGlStateCache.mDepthBufferEnabled)
752 mImpl->mGlStateCache.mDepthBufferEnabled = depthTestEnable;
754 auto& gl = *mImpl->mController.GetGL();
757 gl.Enable(GL_DEPTH_TEST);
761 gl.Disable(GL_DEPTH_TEST);
766 void Context::SetDepthWriteEnable(bool depthWriteEnable)
768 if(depthWriteEnable != mImpl->mGlStateCache.mDepthMaskEnabled)
770 mImpl->mGlStateCache.mDepthMaskEnabled = depthWriteEnable;
772 auto& gl = *mImpl->mController.GetGL();
773 gl.DepthMask(depthWriteEnable);
777 void Context::ActiveTexture(uint32_t textureBindingIndex)
779 if(mImpl->mGlStateCache.mActiveTextureUnit != textureBindingIndex)
781 mImpl->mGlStateCache.mActiveTextureUnit = textureBindingIndex;
783 auto& gl = *mImpl->mController.GetGL();
784 gl.ActiveTexture(GL_TEXTURE0 + textureBindingIndex);
788 void Context::BindTexture(GLenum target, BoundTextureType textureTypeId, uint32_t textureId)
790 uint32_t typeId = static_cast<uint32_t>(textureTypeId);
791 if(mImpl->mGlStateCache.mBoundTextureId[mImpl->mGlStateCache.mActiveTextureUnit][typeId] != textureId)
793 mImpl->mGlStateCache.mBoundTextureId[mImpl->mGlStateCache.mActiveTextureUnit][typeId] = textureId;
795 auto& gl = *mImpl->mController.GetGL();
796 gl.BindTexture(target, textureId);
800 void Context::GenerateMipmap(GLenum target)
802 auto& gl = *mImpl->mController.GetGL();
803 gl.GenerateMipmap(target);
806 void Context::BindBuffer(GLenum target, uint32_t bufferId)
808 if(mImpl->mGlStateCache.mBoundArrayBufferId != bufferId)
810 mImpl->mGlStateCache.mBoundArrayBufferId = bufferId;
812 auto& gl = *mImpl->mController.GetGL();
813 gl.BindBuffer(target, bufferId);
817 void Context::DrawBuffers(uint32_t count, const GLenum* buffers)
819 mImpl->mGlStateCache.mFrameBufferStateCache.DrawOperation(mImpl->mGlStateCache.mColorMask,
820 mImpl->mGlStateCache.DepthBufferWriteEnabled(),
821 mImpl->mGlStateCache.StencilBufferWriteEnabled());
823 auto& gl = *mImpl->mController.GetGL();
824 gl.DrawBuffers(count, buffers);
827 void Context::BindFrameBuffer(GLenum target, uint32_t bufferId)
829 mImpl->mGlStateCache.mFrameBufferStateCache.SetCurrentFrameBuffer(bufferId);
831 auto& gl = *mImpl->mController.GetGL();
832 gl.BindFramebuffer(target, bufferId);
835 void Context::GenFramebuffers(uint32_t count, uint32_t* framebuffers)
837 auto& gl = *mImpl->mController.GetGL();
838 gl.GenFramebuffers(count, framebuffers);
840 mImpl->mGlStateCache.mFrameBufferStateCache.FrameBuffersCreated(count, framebuffers);
843 void Context::DeleteFramebuffers(uint32_t count, uint32_t* framebuffers)
845 mImpl->mGlStateCache.mFrameBufferStateCache.FrameBuffersDeleted(count, framebuffers);
847 auto& gl = *mImpl->mController.GetGL();
848 gl.DeleteFramebuffers(count, framebuffers);
851 GLStateCache& Context::GetGLStateCache()
853 return mImpl->mGlStateCache;
856 void Context::GlContextCreated()
858 if(!mImpl->mGlContextCreated)
860 mImpl->mGlContextCreated = true;
862 // Set the initial GL state
863 mImpl->InitializeGlState();
867 void Context::GlContextDestroyed()
869 mImpl->mGlContextCreated = false;
872 void Context::InvalidateCachedPipeline(GLES::Pipeline* pipeline)
874 // Since the pipeline is deleted, invalidate the cached pipeline.
875 if(mImpl->mCurrentPipeline == &pipeline->GetPipeline())
877 mImpl->mCurrentPipeline = nullptr;
881 } // namespace Dali::Graphics::GLES