Diligent Engine  v.2.4.g
GLContextState.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019-2021 Diligent Graphics LLC
3  * Copyright 2015-2019 Egor Yusov
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * In no event and under no legal theory, whether in tort (including negligence),
18  * contract, or otherwise, unless required by applicable law (such as deliberate
19  * and grossly negligent acts) or agreed to in writing, shall any Contributor be
20  * liable for any damages, including any direct, indirect, special, incidental,
21  * or consequential damages of any character arising as a result of this License or
22  * out of the use or inability to use the software (including but not limited to damages
23  * for loss of goodwill, work stoppage, computer failure or malfunction, or any and
24  * all other commercial damages or losses), even if such Contributor has been advised
25  * of the possibility of such damages.
26  */
27 
28 #pragma once
29 
30 #include "GraphicsTypes.h"
31 #include "GLObjectWrapper.hpp"
32 #include "UniqueIdentifier.hpp"
33 #include "GLContext.hpp"
35 
36 namespace Diligent
37 {
38 
40 {
41 public:
42  GLContextState(class RenderDeviceGLImpl* pDeviceGL);
43 
44  // clang-format off
45 
46  void SetProgram (const GLObjectWrappers::GLProgramObj& GLProgram);
47  void SetPipeline (const GLObjectWrappers::GLPipelineObj& GLPipeline);
50  void SetActiveTexture (Int32 Index);
51  void BindTexture (Int32 Index, GLenum BindTarget, const GLObjectWrappers::GLTextureObj& Tex);
52  void BindUniformBuffer (Int32 Index, const GLObjectWrappers::GLBufferObj& Buff);
53  void BindBuffer (GLenum BindTarget, const GLObjectWrappers::GLBufferObj& Buff, bool ResetVAO);
54  void BindSampler (Uint32 Index, const GLObjectWrappers::GLSamplerObj& GLSampler);
55  void BindImage (Uint32 Index, class TextureViewGLImpl* pTexView, GLint MipLevel, GLboolean IsLayered, GLint Layer, GLenum Access, GLenum Format);
56  void BindImage (Uint32 Index, class BufferViewGLImpl* pBuffView, GLenum Access, GLenum Format);
57  void BindStorageBlock (Int32 Index, const GLObjectWrappers::GLBufferObj& Buff, GLintptr Offset, GLsizeiptr Size);
58 
59  void EnsureMemoryBarrier(MEMORY_BARRIER RequiredBarriers, class AsyncWritableResource *pRes = nullptr);
60  void SetPendingMemoryBarriers(MEMORY_BARRIER PendingBarriers);
61 
62  void EnableDepthTest (bool bEnable);
63  void EnableDepthWrites (bool bEnable);
64  void SetDepthFunc (COMPARISON_FUNCTION CmpFunc);
65  void EnableStencilTest (bool bEnable);
66  void SetStencilWriteMask (Uint8 StencilWriteMask);
67  void SetStencilRef (GLenum Face, Int32 Ref);
68  void SetStencilFunc (GLenum Face, COMPARISON_FUNCTION Func, Int32 Ref, Uint32 Mask);
69  void SetStencilOp (GLenum Face, STENCIL_OP StencilFailOp, STENCIL_OP StencilDepthFailOp, STENCIL_OP StencilPassOp);
70  void SetFillMode (FILL_MODE FillMode);
71  void SetCullMode (CULL_MODE CullMode);
72  void SetFrontFace (bool FrontCounterClockwise);
73  void SetDepthBias (float DepthBias, float fSlopeScaledDepthBias);
74  void SetDepthClamp (bool bEnableDepthClamp);
75  void EnableScissorTest (bool bEnableScissorTest);
76 
77  void SetBlendFactors(const float* BlendFactors);
78  void SetBlendState(const BlendStateDesc& BSDsc, Uint32 SampleMask);
79 
80  Bool GetDepthWritesEnabled(){ return m_DSState.m_DepthWritesEnableState; }
81  Bool GetScissorTestEnabled(){ return m_RSState.ScissorTestEnable; }
82  void GetColorWriteMask(Uint32 RTIndex, Uint32& WriteMask, Bool& bIsIndependent);
83  void SetColorWriteMask(Uint32 RTIndex, Uint32 WriteMask, Bool bIsIndependent);
84 
85  void GetBoundImage(Uint32 Index, GLuint& GLHandle, GLint& MipLevel, GLboolean& IsLayered, GLint& Layer, GLenum& Access, GLenum& Format) const;
86 
87  // clang-format on
88 
89  void SetNumPatchVertices(Int32 NumVertices);
90  void Invalidate();
91 
93  {
94  m_VAOId = -1;
95  // Resetting VAO after that with BindVAO(GLVertexArrayObj::Null()) will still have the effect because
96  // null VAO's ID is 0, not -1.
97  }
98 
100  {
101  m_FBOId = -1;
102  }
103  bool IsValidVAOBound() const { return m_VAOId > 0; }
104 
105  void SetCurrentGLContext(GLContext::NativeGLContextType Context) { m_CurrentGLContext = Context; }
106 
107  GLContext::NativeGLContextType GetCurrentGLContext() const { return m_CurrentGLContext; }
108 
109  struct ContextCaps
110  {
113  GLint m_iMaxDrawBuffers = 0;
115  };
116  const ContextCaps& GetContextCaps() { return m_Caps; }
117 
118 private:
119  // It is unsafe to use GL handle to keep track of bound objects
120  // When an object is released, GL is free to reuse its handle for
121  // the new created objects.
122  // Even using pointers is not safe as when an object is created,
123  // the system can reuse the same address
124  // The safest way is to keep global unique ID for all objects
125 
126  UniqueIdentifier m_GLProgId = -1;
127  UniqueIdentifier m_GLPipelineId = -1;
128  UniqueIdentifier m_VAOId = -1;
129  UniqueIdentifier m_FBOId = -1;
130  std::vector<UniqueIdentifier> m_BoundTextures;
131  std::vector<UniqueIdentifier> m_BoundSamplers;
132  std::vector<UniqueIdentifier> m_BoundUniformBuffers;
133 
134  struct BoundImageInfo
135  {
136  UniqueIdentifier InterfaceID = -1;
137  GLuint GLHandle = 0;
138  GLint MipLevel = 0;
139  GLboolean IsLayered = 0;
140  GLint Layer = 0;
141  GLenum Access = 0;
142  GLenum Format = 0;
143 
144  BoundImageInfo(){};
145 
146  BoundImageInfo(UniqueIdentifier _UniqueID,
147  GLuint _GLHandle,
148  GLint _MipLevel,
149  GLboolean _IsLayered,
150  GLint _Layer,
151  GLenum _Access,
152  GLenum _Format) :
153  // clang-format off
154  InterfaceID{_UniqueID},
155  GLHandle {_GLHandle},
156  MipLevel {_MipLevel},
157  IsLayered {_IsLayered},
158  Layer {_Layer},
159  Access {_Access},
160  Format {_Format}
161  // clang-format on
162  {}
163 
164  bool operator==(const BoundImageInfo& rhs) const
165  {
166  // clang-format off
167  return InterfaceID == rhs.InterfaceID &&
168  GLHandle == rhs.GLHandle &&
169  MipLevel == rhs.MipLevel &&
170  IsLayered == rhs.IsLayered &&
171  Layer == rhs.Layer &&
172  Access == rhs.Access &&
173  Format == rhs.Format;
174  // clang-format on
175  }
176  };
177  std::vector<BoundImageInfo> m_BoundImages;
178 
179  struct BoundSSBOInfo
180  {
181  BoundSSBOInfo() {}
182  BoundSSBOInfo(UniqueIdentifier _BufferID,
183  GLintptr _Offset,
184  GLsizeiptr _Size) :
185  // clang-format off
186  BufferID{_BufferID},
187  Offset {_Offset},
188  Size {_Size}
189  // clang-format on
190  {}
191  UniqueIdentifier BufferID = -1;
192  GLintptr Offset = 0;
193  GLsizeiptr Size = 0;
194 
195  bool operator==(const BoundSSBOInfo& rhs) const
196  {
197  // clang-format off
198  return BufferID == rhs.BufferID &&
199  Offset == rhs.Offset &&
200  Size == rhs.Size;
201  // clang-format on
202  }
203  };
204  std::vector<BoundSSBOInfo> m_BoundStorageBlocks;
205 
206  MEMORY_BARRIER m_PendingMemoryBarriers = MEMORY_BARRIER_NONE;
207 
208  class EnableStateHelper
209  {
210  public:
211  enum class ENABLE_STATE : Int32
212  {
213  UNKNOWN,
214  ENABLED,
215  DISABLED
216  };
217 
218  bool operator==(bool bEnabled) const
219  {
220  return (bEnabled && m_EnableState == ENABLE_STATE::ENABLED) ||
221  (!bEnabled && m_EnableState == ENABLE_STATE::DISABLED);
222  }
223  bool operator!=(bool bEnabled) const
224  {
225  return !(*this == bEnabled);
226  }
227 
228  const EnableStateHelper& operator=(bool bEnabled)
229  {
230  m_EnableState = bEnabled ? ENABLE_STATE::ENABLED : ENABLE_STATE::DISABLED;
231  return *this;
232  }
233 
234  operator bool() const
235  {
236  return m_EnableState == ENABLE_STATE::ENABLED;
237  }
238 
239  private:
240  ENABLE_STATE m_EnableState = ENABLE_STATE::UNKNOWN;
241  };
242 
243  struct DepthStencilGLState
244  {
245  EnableStateHelper m_DepthEnableState;
246  EnableStateHelper m_DepthWritesEnableState;
248  EnableStateHelper m_StencilTestEnableState;
249  Uint16 m_StencilReadMask = 0xFFFF;
250  Uint16 m_StencilWriteMask = 0xFFFF;
252  {
258  Uint32 Mask = static_cast<Uint32>(-1);
259  } m_StencilOpState[2];
260  } m_DSState;
261 
262  struct RasterizerGLState
263  {
264  FILL_MODE FillMode = FILL_MODE_UNDEFINED;
265  CULL_MODE CullMode = CULL_MODE_UNDEFINED;
266  EnableStateHelper FrontCounterClockwise;
267  float fDepthBias = std::numeric_limits<float>::max();
268  float fSlopeScaledDepthBias = std::numeric_limits<float>::max();
269  EnableStateHelper DepthClampEnable;
270  EnableStateHelper ScissorTestEnable;
271  } m_RSState;
272 
273  ContextCaps m_Caps;
274 
275  Uint32 m_ColorWriteMasks[MAX_RENDER_TARGETS] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
276  EnableStateHelper m_bIndependentWriteMasks;
277  Int32 m_iActiveTexture = -1;
278  Int32 m_NumPatchVertices = -1;
279 
280  GLContext::NativeGLContextType m_CurrentGLContext = {};
281 };
282 
283 } // namespace Diligent
Diligent::GLContext::NativeGLContextType
EGLContext NativeGLContextType
Definition: GLContextAndroid.hpp:39
Diligent::GLContextState::GetColorWriteMask
void GetColorWriteMask(Uint32 RTIndex, Uint32 &WriteMask, Bool &bIsIndependent)
Definition: GLContextState.cpp:844
Diligent::GLContextState::DepthStencilGLState::StencilOpState::Ref
Int32 Ref
Definition: GLContextState.hpp:257
Diligent::GLContextState::ContextCaps::m_iMaxCombinedTexUnits
GLint m_iMaxCombinedTexUnits
Definition: GLContextState.hpp:112
Diligent::GLContextState::DepthStencilGLState::StencilOpState
Definition: GLContextState.hpp:251
Diligent::GLContextState::DepthStencilGLState::StencilOpState::StencilPassOp
STENCIL_OP StencilPassOp
Definition: GLContextState.hpp:256
Diligent::GLContextState::SetActiveTexture
void SetActiveTexture(Int32 Index)
Definition: GLContextState.cpp:197
Diligent::GLContextState::BindImage
void BindImage(Uint32 Index, class TextureViewGLImpl *pTexView, GLint MipLevel, GLboolean IsLayered, GLint Layer, GLenum Access, GLenum Format)
Definition: GLContextState.cpp:242
Diligent::GLContextState::DepthStencilGLState::StencilOpState::Func
COMPARISON_FUNCTION Func
Definition: GLContextState.hpp:253
Diligent::STENCIL_OP_UNDEFINED
@ STENCIL_OP_UNDEFINED
Undefined operation.
Definition: DepthStencilState.h:51
Diligent::min
Vector3< T > min(const Vector3< T > &a, const Vector3< T > &b)
Definition: BasicMath.hpp:1648
Diligent::TextureViewGLImpl
Texture view implementation in OpenGL backend.
Definition: TextureViewGLImpl.hpp:38
Diligent::GLContextState::SetStencilFunc
void SetStencilFunc(GLenum Face, COMPARISON_FUNCTION Func, Int32 Ref, Uint32 Mask)
Definition: GLContextState.cpp:512
Diligent::GLContextState::SetCullMode
void SetCullMode(CULL_MODE CullMode)
Definition: GLContextState.cpp:575
Diligent::GLContextState::EnableStencilTest
void EnableStencilTest(bool bEnable)
Definition: GLContextState.cpp:477
Diligent::GLContextState::SetColorWriteMask
void SetColorWriteMask(Uint32 RTIndex, Uint32 WriteMask, Bool bIsIndependent)
Definition: GLContextState.cpp:801
Diligent::GLContextState::EnableScissorTest
void EnableScissorTest(bool bEnableScissorTest)
Definition: GLContextState.cpp:668
Diligent::FILL_MODE
FILL_MODE
Fill mode.
Definition: RasterizerState.h:46
Diligent::GLContextState::ContextCaps
Definition: GLContextState.hpp:109
Diligent::operator==
bool operator==(const Plane3D &p1, const Plane3D &p2)
Definition: AdvancedMath.hpp:442
Diligent::GLContextState::BindBuffer
void BindBuffer(GLenum BindTarget, const GLObjectWrappers::GLBufferObj &Buff, bool ResetVAO)
Definition: GLContextState.cpp:365
Diligent::AsyncWritableResource
Definition: AsyncWritableResource.hpp:76
Diligent::GLContextState::SetDepthFunc
void SetDepthFunc(COMPARISON_FUNCTION CmpFunc)
Definition: GLContextState.cpp:466
Diligent::GLContextState::SetStencilWriteMask
void SetStencilWriteMask(Uint8 StencilWriteMask)
Definition: GLContextState.cpp:495
Diligent::GLContextState::SetDepthBias
void SetDepthBias(float DepthBias, float fSlopeScaledDepthBias)
Definition: GLContextState.cpp:609
Diligent::GLContextState::BindUniformBuffer
void BindUniformBuffer(Int32 Index, const GLObjectWrappers::GLBufferObj &Buff)
Definition: GLContextState.cpp:330
Diligent::max
Vector3< T > max(const Vector3< T > &a, const Vector3< T > &b)
Definition: BasicMath.hpp:1660
Diligent::GLContextState::BindSampler
void BindSampler(Uint32 Index, const GLObjectWrappers::GLSamplerObj &GLSampler)
Definition: GLContextState.cpp:232
Diligent::Int32
int32_t Int32
32-bit signed integer
Definition: BasicTypes.h:46
Diligent::GLContextState::GetBoundImage
void GetBoundImage(Uint32 Index, GLuint &GLHandle, GLint &MipLevel, GLboolean &IsLayered, GLint &Layer, GLenum &Access, GLenum &Format) const
Definition: GLContextState.cpp:300
Diligent::GLContextState::ContextCaps::bFillModeSelectionSupported
bool bFillModeSelectionSupported
Definition: GLContextState.hpp:111
Diligent::GLContextState::SetBlendFactors
void SetBlendFactors(const float *BlendFactors)
Definition: GLContextState.cpp:687
Diligent::GLContextState::InvalidateVAO
void InvalidateVAO()
Definition: GLContextState.hpp:92
Diligent::CULL_MODE
CULL_MODE
Cull mode.
Definition: RasterizerState.h:69
UniqueIdentifier.hpp
Diligent::GLContextState::Invalidate
void Invalidate()
Definition: GLContextState.cpp:77
Diligent::GLContextState::ContextCaps::m_iMaxDrawBuffers
GLint m_iMaxDrawBuffers
Definition: GLContextState.hpp:113
Diligent::COMPARISON_FUNC_UNKNOWN
@ COMPARISON_FUNC_UNKNOWN
Unknown comparison function.
Definition: GraphicsTypes.h:934
Diligent::GLContextState::ContextCaps::m_iMaxUniformBufferBindings
GLint m_iMaxUniformBufferBindings
Definition: GLContextState.hpp:114
Diligent::CULL_MODE_UNDEFINED
@ CULL_MODE_UNDEFINED
Undefined cull mode.
Definition: RasterizerState.h:72
Diligent::GLContextState::SetFrontFace
void SetFrontFace(bool FrontCounterClockwise)
Definition: GLContextState.cpp:598
Diligent::GLContextState::EnsureMemoryBarrier
void EnsureMemoryBarrier(MEMORY_BARRIER RequiredBarriers, class AsyncWritableResource *pRes=nullptr)
Definition: GLContextState.cpp:383
Diligent::Uint32
uint32_t Uint32
32-bit unsigned integer
Definition: BasicTypes.h:51
Diligent::Bool
bool Bool
Boolean.
Definition: BasicTypes.h:59
Diligent::COMPARISON_FUNCTION
COMPARISON_FUNCTION
Comparison function.
Definition: GraphicsTypes.h:931
Diligent::GLContextState::SetCurrentGLContext
void SetCurrentGLContext(GLContext::NativeGLContextType Context)
Definition: GLContextState.hpp:105
Diligent::GLContextState::BindVAO
void BindVAO(const GLObjectWrappers::GLVertexArrayObj &VAO)
Definition: GLContextState.cpp:159
Diligent::GLContextState::EnableDepthWrites
void EnableDepthWrites(bool bEnable)
Definition: GLContextState.cpp:455
Diligent::GLContextState::BindFBO
void BindFBO(const GLObjectWrappers::GLFrameBufferObj &FBO)
Definition: GLContextState.cpp:169
Diligent::MEMORY_BARRIER
MEMORY_BARRIER
Definition: AsyncWritableResource.hpp:33
Diligent::GLContextState::GetCurrentGLContext
GLContext::NativeGLContextType GetCurrentGLContext() const
Definition: GLContextState.hpp:107
Diligent::RenderDeviceGLImpl
Render device implementation in OpenGL backend.
Definition: RenderDeviceGLImpl.hpp:45
Diligent::GLContextState::SetProgram
void SetProgram(const GLObjectWrappers::GLProgramObj &GLProgram)
Definition: GLContextState.cpp:139
Diligent::GLContextState::SetPipeline
void SetPipeline(const GLObjectWrappers::GLPipelineObj &GLPipeline)
Definition: GLContextState.cpp:149
Diligent::GLContextState::SetStencilRef
void SetStencilRef(GLenum Face, Int32 Ref)
Definition: GLContextState.cpp:504
Diligent::GLContextState::InvalidateFBO
void InvalidateFBO()
Definition: GLContextState.hpp:99
Diligent::GLContextState::GetScissorTestEnabled
Bool GetScissorTestEnabled()
Definition: GLContextState.hpp:81
Diligent::STENCIL_OP
STENCIL_OP
Stencil operation.
Definition: DepthStencilState.h:48
Diligent::GLContextState::EnableDepthTest
void EnableDepthTest(bool bEnable)
Definition: GLContextState.cpp:437
GLContext.hpp
Diligent::GLContextState::IsValidVAOBound
bool IsValidVAOBound() const
Definition: GLContextState.hpp:103
Diligent::FILL_MODE_UNDEFINED
@ FILL_MODE_UNDEFINED
Undefined fill mode.
Definition: RasterizerState.h:49
AsyncWritableResource.hpp
Diligent::GLContextState::GetContextCaps
const ContextCaps & GetContextCaps()
Definition: GLContextState.hpp:116
Diligent::Uint16
uint16_t Uint16
16-bit unsigned integer
Definition: BasicTypes.h:52
GLObjectWrappers::GLObjWrapper
Definition: GLObjectWrapper.hpp:36
Diligent::GLContextState::SetNumPatchVertices
void SetNumPatchVertices(Int32 NumVertices)
Definition: GLContextState.cpp:852
Diligent::Uint8
uint8_t Uint8
8-bit unsigned integer
Definition: BasicTypes.h:53
Diligent::GLContextState::BindStorageBlock
void BindStorageBlock(Int32 Index, const GLObjectWrappers::GLBufferObj &Buff, GLintptr Offset, GLsizeiptr Size)
Definition: GLContextState.cpp:344
Diligent::GLContextState::SetStencilOp
void SetStencilOp(GLenum Face, STENCIL_OP StencilFailOp, STENCIL_OP StencilDepthFailOp, STENCIL_OP StencilPassOp)
Definition: GLContextState.cpp:527
Diligent::GLContextState::SetFillMode
void SetFillMode(FILL_MODE FillMode)
Definition: GLContextState.cpp:547
Diligent::BufferViewGLImpl
Buffer view implementation in OpenGL backend.
Definition: BufferViewGLImpl.hpp:38
Diligent::GLContextState::SetPendingMemoryBarriers
void SetPendingMemoryBarriers(MEMORY_BARRIER PendingBarriers)
Definition: GLContextState.cpp:432
Diligent::GLContextState
Definition: GLContextState.hpp:39
GraphicsTypes.h
Diligent::GLContextState::DepthStencilGLState::StencilOpState::StencilDepthFailOp
STENCIL_OP StencilDepthFailOp
Definition: GLContextState.hpp:255
Diligent::GLContextState::DepthStencilGLState::StencilOpState::Mask
Uint32 Mask
Definition: GLContextState.hpp:258
Diligent::GLContextState::BindTexture
void BindTexture(Int32 Index, GLenum BindTarget, const GLObjectWrappers::GLTextureObj &Tex)
Definition: GLContextState.cpp:213
Diligent::UniqueIdentifier
Int32 UniqueIdentifier
Definition: UniqueIdentifier.hpp:36
Diligent::GLContextState::GLContextState
GLContextState(class RenderDeviceGLImpl *pDeviceGL)
Definition: GLContextState.cpp:45
Diligent::GLContextState::SetDepthClamp
void SetDepthClamp(bool bEnableDepthClamp)
Definition: GLContextState.cpp:633
Diligent::MEMORY_BARRIER_NONE
@ MEMORY_BARRIER_NONE
Definition: AsyncWritableResource.hpp:35
Diligent::operator!=
bool operator!=(const STDAllocator< T, A > &left, const STDAllocator< U, A > &right)
Definition: STDAllocator.hpp:173
Diligent::BlendStateDesc
Blend state description.
Definition: BlendState.h:374
Diligent
The library uses Direct3D-style math:
Definition: AdvancedMath.hpp:37
Diligent::GLContextState::SetBlendState
void SetBlendState(const BlendStateDesc &BSDsc, Uint32 SampleMask)
Definition: GLContextState.cpp:693
Diligent::GLContextState::GetDepthWritesEnabled
Bool GetDepthWritesEnabled()
Definition: GLContextState.hpp:80
Diligent::GLContextState::DepthStencilGLState::StencilOpState::StencilFailOp
STENCIL_OP StencilFailOp
Definition: GLContextState.hpp:254
GLObjectWrapper.hpp