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 Graphics::TextureBinding* bindings, uint32_t count)
327 // for each texture allocate slot
328 for(auto i = 0u; i < count; ++i)
330 auto& binding = bindings[i];
332 // Resize binding array if needed
333 if(mImpl->mCurrentTextureBindings.size() <= binding.binding)
335 mImpl->mCurrentTextureBindings.resize(binding.binding + 1);
337 // Store the binding details
338 mImpl->mCurrentTextureBindings[binding.binding] = binding;
342 void Context::BindVertexBuffers(const GLES::VertexBufferBindingDescriptor* bindings, uint32_t count)
344 if(count > mImpl->mCurrentVertexBufferBindings.size())
346 mImpl->mCurrentVertexBufferBindings.resize(count);
348 // Copy only set slots
349 std::copy_if(bindings, bindings + count, mImpl->mCurrentVertexBufferBindings.begin(), [](auto& item) {
350 return (nullptr != item.buffer);
354 void Context::BindIndexBuffer(const IndexBufferBindingDescriptor& indexBufferBinding)
356 mImpl->mCurrentIndexBufferBinding = indexBufferBinding;
359 void Context::BindPipeline(const GLES::Pipeline* newPipeline)
361 DALI_ASSERT_ALWAYS(newPipeline && "Invalid pipeline");
362 mImpl->mNewPipeline = &newPipeline->GetPipeline();
365 void Context::BindUniformBuffers(const UniformBufferBindingDescriptor* uboBindings,
367 const UniformBufferBindingDescriptor& standaloneBindings)
369 if(standaloneBindings.buffer)
371 mImpl->mCurrentStandaloneUBOBinding = standaloneBindings;
374 if(uboCount >= mImpl->mCurrentUBOBindings.size())
376 mImpl->mCurrentUBOBindings.resize(uboCount + 1);
379 auto it = uboBindings;
380 for(auto i = 0u; i < uboCount; ++i)
384 mImpl->mCurrentUBOBindings[i] = *it;
389 void Context::ResolveBlendState()
391 const auto& currentBlendState = mImpl->mCurrentPipeline ? mImpl->mCurrentPipeline->GetCreateInfo().colorBlendState : nullptr;
392 const auto& newBlendState = mImpl->mNewPipeline->GetCreateInfo().colorBlendState;
394 // TODO: prevent leaking the state
400 auto& gl = *mImpl->mController.GetGL();
402 if(!currentBlendState || currentBlendState->blendEnable != newBlendState->blendEnable)
404 if(newBlendState->blendEnable != mImpl->mGlStateCache.mBlendEnabled)
406 mImpl->mGlStateCache.mBlendEnabled = newBlendState->blendEnable;
407 newBlendState->blendEnable ? gl.Enable(GL_BLEND) : gl.Disable(GL_BLEND);
411 if(!newBlendState->blendEnable)
416 BlendFactor newSrcRGB(newBlendState->srcColorBlendFactor);
417 BlendFactor newDstRGB(newBlendState->dstColorBlendFactor);
418 BlendFactor newSrcAlpha(newBlendState->srcAlphaBlendFactor);
419 BlendFactor newDstAlpha(newBlendState->dstAlphaBlendFactor);
421 if(!currentBlendState ||
422 currentBlendState->srcColorBlendFactor != newSrcRGB ||
423 currentBlendState->dstColorBlendFactor != newDstRGB ||
424 currentBlendState->srcAlphaBlendFactor != newSrcAlpha ||
425 currentBlendState->dstAlphaBlendFactor != newDstAlpha)
427 if((mImpl->mGlStateCache.mBlendFuncSeparateSrcRGB != newSrcRGB) ||
428 (mImpl->mGlStateCache.mBlendFuncSeparateDstRGB != newDstRGB) ||
429 (mImpl->mGlStateCache.mBlendFuncSeparateSrcAlpha != newSrcAlpha) ||
430 (mImpl->mGlStateCache.mBlendFuncSeparateDstAlpha != newDstAlpha))
432 mImpl->mGlStateCache.mBlendFuncSeparateSrcRGB = newSrcRGB;
433 mImpl->mGlStateCache.mBlendFuncSeparateDstRGB = newDstRGB;
434 mImpl->mGlStateCache.mBlendFuncSeparateSrcAlpha = newSrcAlpha;
435 mImpl->mGlStateCache.mBlendFuncSeparateDstAlpha = newDstAlpha;
437 if(newSrcRGB == newSrcAlpha && newDstRGB == newDstAlpha)
439 gl.BlendFunc(GLBlendFunc(newSrcRGB), GLBlendFunc(newDstRGB));
443 gl.BlendFuncSeparate(GLBlendFunc(newSrcRGB), GLBlendFunc(newDstRGB), GLBlendFunc(newSrcAlpha), GLBlendFunc(newDstAlpha));
448 if(!currentBlendState ||
449 currentBlendState->colorBlendOp != newBlendState->colorBlendOp ||
450 currentBlendState->alphaBlendOp != newBlendState->alphaBlendOp)
452 if(mImpl->mGlStateCache.mBlendEquationSeparateModeRGB != newBlendState->colorBlendOp ||
453 mImpl->mGlStateCache.mBlendEquationSeparateModeAlpha != newBlendState->alphaBlendOp)
455 mImpl->mGlStateCache.mBlendEquationSeparateModeRGB = newBlendState->colorBlendOp;
456 mImpl->mGlStateCache.mBlendEquationSeparateModeAlpha = newBlendState->alphaBlendOp;
458 if(newBlendState->colorBlendOp == newBlendState->alphaBlendOp)
460 gl.BlendEquation(GLBlendOp(newBlendState->colorBlendOp));
464 gl.BlendEquationSeparate(GLBlendOp(newBlendState->colorBlendOp), GLBlendOp(newBlendState->alphaBlendOp));
470 void Context::ResolveRasterizationState()
472 const auto& currentRasterizationState = mImpl->mCurrentPipeline ? mImpl->mCurrentPipeline->GetCreateInfo().rasterizationState : nullptr;
473 const auto& newRasterizationState = mImpl->mNewPipeline->GetCreateInfo().rasterizationState;
475 // TODO: prevent leaking the state
476 if(!newRasterizationState)
481 auto& gl = *mImpl->mController.GetGL();
483 if(!currentRasterizationState ||
484 currentRasterizationState->cullMode != newRasterizationState->cullMode)
486 if(mImpl->mGlStateCache.mCullFaceMode != newRasterizationState->cullMode)
488 mImpl->mGlStateCache.mCullFaceMode = newRasterizationState->cullMode;
489 if(newRasterizationState->cullMode == CullMode::NONE)
491 gl.Disable(GL_CULL_FACE);
495 gl.Enable(GL_CULL_FACE);
496 gl.CullFace(GLCullMode(newRasterizationState->cullMode));
500 // TODO: implement polygon mode (fill, line, points)
501 // seems like we don't support it (no glPolygonMode())
504 void Context::ResolveUniformBuffers()
506 // Resolve standalone uniforms if we have binding
507 if(mImpl->mCurrentStandaloneUBOBinding.buffer)
509 ResolveStandaloneUniforms();
513 void Context::ResolveStandaloneUniforms()
515 // Find reflection for program
516 const auto program = static_cast<const GLES::Program*>(mImpl->mNewPipeline->GetCreateInfo().programState->program);
517 const auto ptr = reinterpret_cast<const char*>(mImpl->mCurrentStandaloneUBOBinding.buffer->GetCPUAllocatedAddress()) + mImpl->mCurrentStandaloneUBOBinding.offset;
519 // Update program uniforms
520 program->GetImplementation()->UpdateStandaloneUniformBlock(ptr);
523 void Context::BeginRenderPass(const BeginRenderPassDescriptor& renderPassBegin)
525 auto& renderPass = *renderPassBegin.renderPass;
526 auto& renderTarget = *renderPassBegin.renderTarget;
528 const auto& targetInfo = renderTarget.GetCreateInfo();
530 auto& gl = *mImpl->mController.GetGL();
532 if(targetInfo.surface)
535 BindFrameBuffer(GL_FRAMEBUFFER, 0);
537 else if(targetInfo.framebuffer)
539 // bind framebuffer and swap.
540 renderTarget.GetFramebuffer()->Bind();
543 // clear (ideally cache the setup)
545 // In GL we assume that the last attachment is depth/stencil (we may need
546 // to cache extra information inside GLES RenderTarget if we want to be
547 // more specific in case of MRT)
549 const auto& attachments = *renderPass.GetCreateInfo().attachments;
550 const auto& color0 = attachments[0];
552 if(color0.loadOp == AttachmentLoadOp::CLEAR)
554 mask |= GL_COLOR_BUFFER_BIT;
557 // Something goes wrong here if Alpha mask is GL_TRUE
560 const auto clearValues = renderPassBegin.clearValues.Ptr();
562 if(!mImpl->mGlStateCache.mClearColorSet ||
563 mImpl->mGlStateCache.mClearColor.r != clearValues[0].color.r ||
564 mImpl->mGlStateCache.mClearColor.g != clearValues[0].color.g ||
565 mImpl->mGlStateCache.mClearColor.b != clearValues[0].color.b ||
566 mImpl->mGlStateCache.mClearColor.a != clearValues[0].color.a)
568 gl.ClearColor(clearValues[0].color.r,
569 clearValues[0].color.g,
570 clearValues[0].color.b,
571 clearValues[0].color.a);
573 mImpl->mGlStateCache.mClearColorSet = true;
574 mImpl->mGlStateCache.mClearColor = Vector4(clearValues[0].color.r,
575 clearValues[0].color.g,
576 clearValues[0].color.b,
577 clearValues[0].color.a);
581 // check for depth stencil
582 if(attachments.size() > 1)
584 const auto& depthStencil = attachments.back();
585 if(depthStencil.loadOp == AttachmentLoadOp::CLEAR)
587 if(!mImpl->mGlStateCache.mDepthMaskEnabled)
589 mImpl->mGlStateCache.mDepthMaskEnabled = true;
592 mask |= GL_DEPTH_BUFFER_BIT;
594 if(depthStencil.stencilLoadOp == AttachmentLoadOp::CLEAR)
596 if(mImpl->mGlStateCache.mStencilMask != 0xFF)
598 mImpl->mGlStateCache.mStencilMask = 0xFF;
599 gl.StencilMask(0xFF);
601 mask |= GL_STENCIL_BUFFER_BIT;
605 SetScissorTestEnabled(true);
606 gl.Scissor(renderPassBegin.renderArea.x, renderPassBegin.renderArea.y, renderPassBegin.renderArea.width, renderPassBegin.renderArea.height);
607 ClearBuffer(mask, true);
608 SetScissorTestEnabled(false);
610 mImpl->mCurrentRenderPass = &renderPass;
611 mImpl->mCurrentRenderTarget = &renderTarget;
614 void Context::EndRenderPass()
616 if(mImpl->mCurrentRenderTarget)
618 if(mImpl->mCurrentRenderTarget->GetFramebuffer())
620 auto& gl = *mImpl->mController.GetGL();
626 void Context::ClearState()
628 mImpl->mCurrentTextureBindings.clear();
631 void Context::ColorMask(bool enabled)
633 if(enabled != mImpl->mGlStateCache.mColorMask)
635 mImpl->mGlStateCache.mColorMask = enabled;
637 auto& gl = *mImpl->mController.GetGL();
638 gl.ColorMask(enabled, enabled, enabled, enabled);
642 void Context::ClearStencilBuffer()
644 ClearBuffer(GL_STENCIL_BUFFER_BIT, false);
647 void Context::ClearDepthBuffer()
649 ClearBuffer(GL_DEPTH_BUFFER_BIT, false);
652 void Context::ClearBuffer(uint32_t mask, bool forceClear)
654 mask = mImpl->mGlStateCache.mFrameBufferStateCache.GetClearMask(mask, forceClear, mImpl->mGlStateCache.mScissorTestEnabled);
657 auto& gl = *mImpl->mController.GetGL();
662 void Context::InvalidateDepthStencilBuffers()
664 auto& gl = *mImpl->mController.GetGL();
666 GLenum attachments[] = {GL_DEPTH, GL_STENCIL};
667 gl.InvalidateFramebuffer(GL_FRAMEBUFFER, 2, attachments);
670 void Context::SetScissorTestEnabled(bool scissorEnabled)
672 if(mImpl->mGlStateCache.mScissorTestEnabled != scissorEnabled)
674 mImpl->mGlStateCache.mScissorTestEnabled = scissorEnabled;
676 auto& gl = *mImpl->mController.GetGL();
679 gl.Enable(GL_SCISSOR_TEST);
683 gl.Disable(GL_SCISSOR_TEST);
688 void Context::SetStencilTestEnable(bool stencilEnable)
690 if(stencilEnable != mImpl->mGlStateCache.mStencilBufferEnabled)
692 mImpl->mGlStateCache.mStencilBufferEnabled = stencilEnable;
694 auto& gl = *mImpl->mController.GetGL();
697 gl.Enable(GL_STENCIL_TEST);
701 gl.Disable(GL_STENCIL_TEST);
706 void Context::StencilMask(uint32_t writeMask)
708 if(writeMask != mImpl->mGlStateCache.mStencilMask)
710 mImpl->mGlStateCache.mStencilMask = writeMask;
712 auto& gl = *mImpl->mController.GetGL();
713 gl.StencilMask(writeMask);
717 void Context::StencilFunc(Graphics::CompareOp compareOp,
719 uint32_t compareMask)
721 if(compareOp != mImpl->mGlStateCache.mStencilFunc ||
722 reference != mImpl->mGlStateCache.mStencilFuncRef ||
723 compareMask != mImpl->mGlStateCache.mStencilFuncMask)
725 mImpl->mGlStateCache.mStencilFunc = compareOp;
726 mImpl->mGlStateCache.mStencilFuncRef = reference;
727 mImpl->mGlStateCache.mStencilFuncMask = compareMask;
729 auto& gl = *mImpl->mController.GetGL();
730 gl.StencilFunc(GLCompareOp(compareOp).op, reference, compareMask);
734 void Context::StencilOp(Graphics::StencilOp failOp,
735 Graphics::StencilOp depthFailOp,
736 Graphics::StencilOp passOp)
738 if(failOp != mImpl->mGlStateCache.mStencilOpFail ||
739 depthFailOp != mImpl->mGlStateCache.mStencilOpDepthFail ||
740 passOp != mImpl->mGlStateCache.mStencilOpDepthPass)
742 mImpl->mGlStateCache.mStencilOpFail = failOp;
743 mImpl->mGlStateCache.mStencilOpDepthFail = depthFailOp;
744 mImpl->mGlStateCache.mStencilOpDepthPass = passOp;
746 auto& gl = *mImpl->mController.GetGL();
747 gl.StencilOp(GLStencilOp(failOp).op, GLStencilOp(depthFailOp).op, GLStencilOp(passOp).op);
751 void Context::SetDepthCompareOp(Graphics::CompareOp compareOp)
753 if(compareOp != mImpl->mGlStateCache.mDepthFunction)
755 mImpl->mGlStateCache.mDepthFunction = compareOp;
756 auto& gl = *mImpl->mController.GetGL();
757 gl.DepthFunc(GLCompareOp(compareOp).op);
761 void Context::SetDepthTestEnable(bool depthTestEnable)
763 if(depthTestEnable != mImpl->mGlStateCache.mDepthBufferEnabled)
765 mImpl->mGlStateCache.mDepthBufferEnabled = depthTestEnable;
767 auto& gl = *mImpl->mController.GetGL();
770 gl.Enable(GL_DEPTH_TEST);
774 gl.Disable(GL_DEPTH_TEST);
779 void Context::SetDepthWriteEnable(bool depthWriteEnable)
781 if(depthWriteEnable != mImpl->mGlStateCache.mDepthMaskEnabled)
783 mImpl->mGlStateCache.mDepthMaskEnabled = depthWriteEnable;
785 auto& gl = *mImpl->mController.GetGL();
786 gl.DepthMask(depthWriteEnable);
790 void Context::ActiveTexture(uint32_t textureBindingIndex)
792 if(mImpl->mGlStateCache.mActiveTextureUnit != textureBindingIndex)
794 mImpl->mGlStateCache.mActiveTextureUnit = textureBindingIndex;
796 auto& gl = *mImpl->mController.GetGL();
797 gl.ActiveTexture(GL_TEXTURE0 + textureBindingIndex);
801 void Context::BindTexture(GLenum target, BoundTextureType textureTypeId, uint32_t textureId)
803 uint32_t typeId = static_cast<uint32_t>(textureTypeId);
804 if(mImpl->mGlStateCache.mBoundTextureId[mImpl->mGlStateCache.mActiveTextureUnit][typeId] != textureId)
806 mImpl->mGlStateCache.mBoundTextureId[mImpl->mGlStateCache.mActiveTextureUnit][typeId] = textureId;
808 auto& gl = *mImpl->mController.GetGL();
809 gl.BindTexture(target, textureId);
813 void Context::GenerateMipmap(GLenum target)
815 auto& gl = *mImpl->mController.GetGL();
816 gl.GenerateMipmap(target);
819 void Context::BindBuffer(GLenum target, uint32_t bufferId)
821 if(mImpl->mGlStateCache.mBoundArrayBufferId != bufferId)
823 mImpl->mGlStateCache.mBoundArrayBufferId = bufferId;
825 auto& gl = *mImpl->mController.GetGL();
826 gl.BindBuffer(target, bufferId);
830 void Context::DrawBuffers(uint32_t count, const GLenum* buffers)
832 mImpl->mGlStateCache.mFrameBufferStateCache.DrawOperation(mImpl->mGlStateCache.mColorMask,
833 mImpl->mGlStateCache.DepthBufferWriteEnabled(),
834 mImpl->mGlStateCache.StencilBufferWriteEnabled());
836 auto& gl = *mImpl->mController.GetGL();
837 gl.DrawBuffers(count, buffers);
840 void Context::BindFrameBuffer(GLenum target, uint32_t bufferId)
842 mImpl->mGlStateCache.mFrameBufferStateCache.SetCurrentFrameBuffer(bufferId);
844 auto& gl = *mImpl->mController.GetGL();
845 gl.BindFramebuffer(target, bufferId);
848 void Context::GenFramebuffers(uint32_t count, uint32_t* framebuffers)
850 auto& gl = *mImpl->mController.GetGL();
851 gl.GenFramebuffers(count, framebuffers);
853 mImpl->mGlStateCache.mFrameBufferStateCache.FrameBuffersCreated(count, framebuffers);
856 void Context::DeleteFramebuffers(uint32_t count, uint32_t* framebuffers)
858 mImpl->mGlStateCache.mFrameBufferStateCache.FrameBuffersDeleted(count, framebuffers);
860 auto& gl = *mImpl->mController.GetGL();
861 gl.DeleteFramebuffers(count, framebuffers);
864 GLStateCache& Context::GetGLStateCache()
866 return mImpl->mGlStateCache;
869 void Context::GlContextCreated()
871 if(!mImpl->mGlContextCreated)
873 mImpl->mGlContextCreated = true;
875 // Set the initial GL state
876 mImpl->InitializeGlState();
880 void Context::GlContextDestroyed()
882 mImpl->mGlContextCreated = false;
885 void Context::InvalidateCachedPipeline(GLES::Pipeline* pipeline)
887 // Since the pipeline is deleted, invalidate the cached pipeline.
888 if(mImpl->mCurrentPipeline == &pipeline->GetPipeline())
890 mImpl->mCurrentPipeline = nullptr;
894 } // namespace Dali::Graphics::GLES