Diligent Engine API Reference
GLContextState.h
1 /* Copyright 2015-2018 Egor Yusov
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
10  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
11  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS.
12  *
13  * In no event and under no legal theory, whether in tort (including negligence),
14  * contract, or otherwise, unless required by applicable law (such as deliberate
15  * and grossly negligent acts) or agreed to in writing, shall any Contributor be
16  * liable for any damages, including any direct, indirect, special, incidental,
17  * or consequential damages of any character arising as a result of this License or
18  * out of the use or inability to use the software (including but not limited to damages
19  * for loss of goodwill, work stoppage, computer failure or malfunction, or any and
20  * all other commercial damages or losses), even if such Contributor has been advised
21  * of the possibility of such damages.
22  */
23 
24 #pragma once
25 
26 #include "GraphicsTypes.h"
27 #include "GLObjectWrapper.h"
28 #include "UniqueIdentifier.h"
29 #include "GLContext.h"
30 
31 namespace Diligent
32 {
33 
34 class GLContextState
35 {
36 public:
37  GLContextState(class RenderDeviceGLImpl *pDeviceGL);
38 
39  void SetProgram( const GLObjectWrappers::GLProgramObj &GLProgram );
40  void SetPipeline( const GLObjectWrappers::GLPipelineObj &GLPipeline );
41  void BindVAO( const GLObjectWrappers::GLVertexArrayObj &VAO );
42  void BindFBO( const GLObjectWrappers::GLFrameBufferObj &FBO );
43  void SetActiveTexture( Int32 Index );
44  void BindTexture( Int32 Index, GLenum BindTarget, const GLObjectWrappers::GLTextureObj &Tex);
45  void BindSampler( Uint32 Index, const GLObjectWrappers::GLSamplerObj &GLSampler);
46  void BindImage( Uint32 Index, class TextureViewGLImpl *pTexView, GLint MipLevel, GLboolean IsLayered, GLint Layer, GLenum Access, GLenum Format );
47  void EnsureMemoryBarrier(Uint32 RequiredBarriers, class AsyncWritableResource *pRes = nullptr);
48  void SetPendingMemoryBarriers( Uint32 PendingBarriers );
49 
50  void EnableDepthTest( Bool bEnable );
51  void EnableDepthWrites( Bool bEnable );
52  void SetDepthFunc(COMPARISON_FUNCTION CmpFunc);
53  void EnableStencilTest( Bool bEnable );
54  void SetStencilWriteMask( Uint8 StencilWriteMask );
55  void SetStencilRef( GLenum Face, Int32 Ref );
56  void SetStencilFunc( GLenum Face, COMPARISON_FUNCTION Func, Int32 Ref, Uint32 Mask );
57  void SetStencilOp( GLenum Face, STENCIL_OP StencilFailOp, STENCIL_OP StencilDepthFailOp, STENCIL_OP StencilPassOp );
58 
59  void SetFillMode( FILL_MODE FillMode );
60  void SetCullMode( CULL_MODE CullMode );
61  void SetFrontFace( Bool FrontCounterClockwise );
62  void SetDepthBias( float DepthBias, float fSlopeScaledDepthBias );
63  void SetDepthClamp( Bool bEnableDepthClamp );
64  void EnableScissorTest( Bool bEnableScissorTest );
65 
66  void SetBlendFactors(const float *BlendFactors);
67  void SetBlendState(const BlendStateDesc &BSDsc, Uint32 SampleMask);
68 
69  Bool GetDepthWritesEnabled(){ return m_DSState.m_DepthWritesEnableState; }
70  Bool GetScissorTestEnabled(){ return m_RSState.ScissorTestEnable; }
71  void GetColorWriteMask( Uint32 RTIndex, Uint32 &WriteMask, Bool &bIsIndependent );
72  void SetColorWriteMask( Uint32 RTIndex, Uint32 WriteMask, Bool bIsIndependent );
73 
74  void SetNumPatchVertices( Int32 NumVertices);
75  void Invalidate();
76 
77  void SetCurrentGLContext(GLContext::NativeGLContextType Context) { m_CurrentGLContext = Context; }
78  GLContext::NativeGLContextType GetCurrentGLContext()const { return m_CurrentGLContext; }
79 
80 private:
81  // It is unsafe to use GL handle to keep track of bound objects
82  // When an object is released, GL is free to reuse its handle for
83  // the new created objects.
84  // Even using pointers is not safe as when an object is created,
85  // the system can reuse the same address
86  // The safest way is to keep global unique ID for all objects
87 
88  Diligent::UniqueIdentifier m_GLProgId = -1;
89  Diligent::UniqueIdentifier m_GLPipelineId = -1;
90  Diligent::UniqueIdentifier m_VAOId = -1;
91  Diligent::UniqueIdentifier m_FBOId = -1;
92  std::vector< Diligent::UniqueIdentifier > m_BoundTextures;
93  std::vector< Diligent::UniqueIdentifier > m_BoundSamplers;
94  struct BoundImageInfo
95  {
96  Diligent::UniqueIdentifier InterfaceID = -1;
97  GLint MipLevel = 0;
98  GLboolean IsLayered = 0;
99  GLint Layer = 0;
100  GLenum Access = 0;
101  GLenum Format = 0;
102 
103  BoundImageInfo() {};
104 
105  BoundImageInfo( Diligent::UniqueIdentifier _UniqueID,
106  GLint _MipLevel,
107  GLboolean _IsLayered,
108  GLint _Layer,
109  GLenum _Access,
110  GLenum _Format) :
111  InterfaceID (_UniqueID ),
112  MipLevel (_MipLevel ),
113  IsLayered (_IsLayered),
114  Layer (_Layer ),
115  Access (_Access ),
116  Format (_Format )
117  {}
118 
119  bool operator==(const BoundImageInfo &rhs)const
120  {
121  return InterfaceID == rhs.InterfaceID &&
122  MipLevel == rhs.MipLevel &&
123  IsLayered == rhs.IsLayered &&
124  Layer == rhs.Layer &&
125  Access == rhs.Access &&
126  Format == rhs.Format;
127  }
128  };
129  std::vector< BoundImageInfo > m_BoundImages;
130 
131  Uint32 m_PendingMemoryBarriers = 0;
132 
133  class EnableStateHelper
134  {
135  public:
136  enum class ENABLE_STATE : Int32
137  {
138  UNKNOWN,
139  ENABLED,
140  DISABLED
141  };
142 
143  bool operator == (bool bEnabled)const
144  {
145  return (bEnabled && m_EnableState == ENABLE_STATE::ENABLED) ||
146  (!bEnabled && m_EnableState == ENABLE_STATE::DISABLED);
147  }
148  bool operator != (bool bEnabled) const
149  {
150  return !(*this == bEnabled);
151  }
152 
153  const EnableStateHelper& operator = (bool bEnabled)
154  {
155  m_EnableState = bEnabled ? ENABLE_STATE::ENABLED : ENABLE_STATE::DISABLED;
156  return *this;
157  }
158 
159  operator bool()const
160  {
161  return m_EnableState == ENABLE_STATE::ENABLED;
162  }
163 
164  private:
165  ENABLE_STATE m_EnableState = ENABLE_STATE::UNKNOWN;
166  };
167 
168  struct DepthStencilGLState
169  {
170  EnableStateHelper m_DepthEnableState;
171  EnableStateHelper m_DepthWritesEnableState;
173  EnableStateHelper m_StencilTestEnableState;
174  Uint16 m_StencilReadMask = 0xFFFF;
175  Uint16 m_StencilWriteMask = 0xFFFF;
176  struct StencilOpState
177  {
179  STENCIL_OP StencilFailOp = STENCIL_OP_UNDEFINED;
180  STENCIL_OP StencilDepthFailOp = STENCIL_OP_UNDEFINED;
181  STENCIL_OP StencilPassOp = STENCIL_OP_UNDEFINED;
182  Int32 Ref = std::numeric_limits<Int32>::min();
183  Uint32 Mask = static_cast<Uint32>(-1);
184  }m_StencilOpState[2];
185  }m_DSState;
186 
187  struct RasterizerGLState
188  {
189  FILL_MODE FillMode = FILL_MODE_UNDEFINED;
190  CULL_MODE CullMode = CULL_MODE_UNDEFINED;
191  EnableStateHelper FrontCounterClockwise;
192  float fDepthBias = std::numeric_limits<float>::max();
193  float fSlopeScaledDepthBias = std::numeric_limits<float>::max();
194  EnableStateHelper DepthClampEnable;
195  EnableStateHelper ScissorTestEnable;
196  }m_RSState;
197 
198  struct ContextCaps
199  {
200  bool bFillModeSelectionSupported = True;
201  GLint m_iMaxCombinedTexUnits = 0;
202  }m_Caps;
203 
204  Uint32 m_ColorWriteMasks[MaxRenderTargets] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
205  EnableStateHelper m_bIndependentWriteMasks;
206  Int32 m_iActiveTexture = -1;
207  Int32 m_NumPatchVertices = -1;
208 
209  GLContext::NativeGLContextType m_CurrentGLContext = {};
210 };
211 
212 }
STENCIL_OP
Stencil operation.
Definition: DepthStencilState.h:42
Undefined fill mode.
Definition: RasterizerState.h:43
Namespace for the OpenGL implementation of the graphics engine.
Definition: BufferD3D11Impl.h:34
static constexpr Uint32 MaxRenderTargets
Maximum number of simultaneous render targets.
Definition: Constants.h:36
Undefined cull mode.
Definition: RasterizerState.h:66
Unknown comparison function.
Definition: GraphicsTypes.h:850
FILL_MODE
Fill mode.
Definition: RasterizerState.h:40
Undefined operation.
Definition: DepthStencilState.h:45
COMPARISON_FUNCTION
Comparison function.
Definition: GraphicsTypes.h:847
CULL_MODE
Cull mode.
Definition: RasterizerState.h:63