1 #ifndef DALI_GRAPHICS_GLES_CONTEXT_STATE_CACHE_H
2 #define DALI_GRAPHICS_GLES_CONTEXT_STATE_CACHE_H
5 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #include <dali/graphics-api/graphics-types.h>
23 #include <dali/integration-api/gl-abstraction.h>
24 #include <dali/integration-api/gl-defines.h>
27 #include "gles-framebuffer-state-cache.h"
29 namespace Dali::Graphics
35 static constexpr unsigned int MAX_TEXTURE_UNITS = 32; // As what is defined in gl-defines.h, which is more than DALi uses anyways
36 static constexpr unsigned int MAX_TEXTURE_TARGET = 4; // We only support GL_TEXTURE_2D, GL_TEXTURE_3D, GL_TEXTURE_CUBE_MAP and GL_TEXTURE_EXTERNAL_OES
37 static constexpr unsigned int MAX_ATTRIBUTE_CACHE_SIZE = 8; // Size of the VertexAttributeArray enables
41 * @brief Cache of GL State per context
46 * Reset the cached texture ids.
48 void ResetTextureCache()
50 // reset the cached texture id's in case the driver re-uses them
51 // when creating new textures
52 for(unsigned int i = 0; i < MAX_TEXTURE_UNITS; ++i)
54 for(unsigned int j = 0; j < MAX_TEXTURE_TARGET; ++j)
56 mBoundTextureId[i][j] = 0;
62 * Reset the cached buffer ids.
64 void ResetBufferCache()
66 // reset the cached buffer id's
67 // fixes problem where some drivers will a generate a buffer with the
68 // same id, as the last deleted buffer id.
69 mBoundArrayBufferId = 0;
70 mBoundElementArrayBufferId = 0;
74 * @return true if next draw operation will write to depth buffer
76 bool DepthBufferWriteEnabled() const
78 return mDepthBufferEnabled && mDepthMaskEnabled;
82 * @return true if next draw operation will write to stencil buffer
84 bool StencilBufferWriteEnabled() const
86 return mStencilBufferEnabled && (mStencilMask > 0);
89 // glEnable/glDisable states
90 bool mColorMask{true};
91 GLuint mStencilMask{0xFF};
92 bool mBlendEnabled{false};
93 bool mDepthBufferEnabled{false};
94 bool mDepthMaskEnabled{false};
95 bool mScissorTestEnabled{false};
96 bool mStencilBufferEnabled{false};
97 bool mClearColorSet{false};
99 // glBindBuffer() state
100 GLuint mBoundArrayBufferId{0}; ///< The ID passed to glBindBuffer(GL_ARRAY_BUFFER)
101 GLuint mBoundElementArrayBufferId{0}; ///< The ID passed to glBindBuffer(GL_ELEMENT_ARRAY_BUFFER)
103 // glBindTexture() state
104 GLenum mActiveTextureUnit{MAX_TEXTURE_UNITS};
105 GLuint mBoundTextureId[MAX_TEXTURE_UNITS][MAX_TEXTURE_TARGET]; ///< The ID passed to glBindTexture()
107 // glBlendFuncSeparate() state
108 BlendFactor mBlendFuncSeparateSrcRGB{BlendFactor::ONE}; ///< The srcRGB parameter passed to glBlendFuncSeparate()
109 BlendFactor mBlendFuncSeparateDstRGB{BlendFactor::ZERO}; ///< The dstRGB parameter passed to glBlendFuncSeparate()
110 BlendFactor mBlendFuncSeparateSrcAlpha{BlendFactor::ONE}; ///< The srcAlpha parameter passed to glBlendFuncSeparate()
111 BlendFactor mBlendFuncSeparateDstAlpha{BlendFactor::ZERO}; ///< The dstAlpha parameter passed to glBlendFuncSeparate()
113 // glBlendEquationSeparate state
114 BlendOp mBlendEquationSeparateModeRGB{BlendOp::ADD}; ///< Controls RGB blend mode
115 BlendOp mBlendEquationSeparateModeAlpha{BlendOp::ADD}; ///< Controls Alpha blend mode
117 // glStencilFunc() and glStencilOp() state.
118 CompareOp mStencilFunc{CompareOp::ALWAYS};
119 GLuint mStencilFuncRef{0};
120 GLuint mStencilFuncMask{0xFFFFFFFF};
121 StencilOp mStencilOpFail{StencilOp::KEEP};
122 StencilOp mStencilOpDepthFail{StencilOp::KEEP};
123 StencilOp mStencilOpDepthPass{StencilOp::KEEP};
125 CompareOp mDepthFunction{CompareOp::LESS}; ///The depth function
127 Vector4 mClearColor{Color::WHITE}; ///< clear color. never used until it's been set by the user
129 CullMode mCullFaceMode{CullMode::NONE}; ///< Face culling mode
131 // Vertex Attribute Buffer enable caching
132 bool mVertexAttributeCachedState[MAX_ATTRIBUTE_CACHE_SIZE]; ///< Value cache for Enable Vertex Attribute
133 bool mVertexAttributeCurrentState[MAX_ATTRIBUTE_CACHE_SIZE]; ///< Current state on the driver for Enable Vertex Attribute
135 FrameBufferStateCache mFrameBufferStateCache{}; ///< frame buffer state cache
140 } // namespace Dali::Graphics
142 #endif // DALI_GRAPHICS_GLES_CONTEXT_STATE_CACHE_H