0026122: Visualization, TKOpenGl - clipping and capping is broken when ffp is disable...
[occt.git] / src / OpenGl / OpenGl_Context.hxx
index f0ce577..074f467 100644 (file)
 #include <Aspect_Drawable.hxx>
 #include <Aspect_Display.hxx>
 #include <Aspect_RenderingContext.hxx>
-#include <Handle_OpenGl_Context.hxx>
-#include <Handle_OpenGl_ShaderManager.hxx>
+#include <Aspect_TypeOfLine.hxx>
 #include <NCollection_DataMap.hxx>
 #include <NCollection_Map.hxx>
 #include <NCollection_Handle.hxx>
-#include <NCollection_Queue.hxx>
+#include <NCollection_List.hxx>
 #include <Message.hxx>
 #include <OpenGl_Caps.hxx>
+#include <OpenGl_MatrixState.hxx>
+#include <OpenGl_Vec.hxx>
 #include <OpenGl_Resource.hxx>
 #include <Standard_Transient.hxx>
 #include <TCollection_AsciiString.hxx>
-#include <Handle_OpenGl_Context.hxx>
+#include <TColStd_PackedMapOfInteger.hxx>
 #include <OpenGl_Clipping.hxx>
 #include <OpenGl_GlCore11.hxx>
+#include <OpenGl_ShaderProgram.hxx>
+
+#include <NCollection_Shared.hxx>
 
 //! Forward declarations
+#if defined(__APPLE__)
+  #import <TargetConditionals.h>
+  #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
+    #ifdef __OBJC__
+      @class EAGLContext;
+    #else
+      struct EAGLContext;
+    #endif
+  #else
+    #ifdef __OBJC__
+      @class NSOpenGLContext;
+    #else
+      struct NSOpenGLContext;
+    #endif
+  #endif
+#endif
+
 struct OpenGl_GlFunctions;
 struct OpenGl_ArbTBO;
 struct OpenGl_ArbIns;
 struct OpenGl_ArbDbg;
 struct OpenGl_ArbFBO;
+struct OpenGl_ArbFBOBlit;
 struct OpenGl_ExtGS;
+struct OpenGl_ArbTexBindless;
 
 template<typename theBaseClass_t> struct OpenGl_TmplCore12;
 typedef OpenGl_TmplCore12<OpenGl_GlCore11>     OpenGl_GlCore12;
@@ -102,11 +125,17 @@ template<typename theBaseClass_t> struct OpenGl_TmplCore44;
 typedef OpenGl_TmplCore44<OpenGl_GlCore43Back> OpenGl_GlCore44Back;
 typedef OpenGl_TmplCore44<OpenGl_GlCore43>     OpenGl_GlCore44;
 
+class OpenGl_ShaderManager;
+class OpenGl_Sampler;
+class OpenGl_FrameBuffer;
+
+class OpenGl_Context;
+DEFINE_STANDARD_HANDLE(OpenGl_Context, Standard_Transient)
+
 //! This class generalize access to the GL context and available extensions.
 //!
-//! Functions are grouped into structures and accessed as fields.
-//! You should check the group for NULL before usage (if group is not NULL
-//! then all functions are available):
+//! Functions related to specific OpenGL version or extension are grouped into structures which can be accessed as fields of this class.
+//! The most simple way to check that required functionality is available - is NULL check for the group:
 //! @code
 //!   if (myContext->core20 != NULL)
 //!   {
@@ -119,9 +148,24 @@ typedef OpenGl_TmplCore44<OpenGl_GlCore43>     OpenGl_GlCore44;
 //!   }
 //! @endcode
 //!
-//! Current implementation provide access to OpenGL core functionality up to 2.0 version
-//! (core12, core13, core14, core15, fields core20).
-//! within several extensions (arbTBO, arbFBO, etc.).
+//! Current implementation provide access to OpenGL core functionality up to 4.4 version (core12, core13, core14, core15, fields core20)
+//! as well as several extensions (arbTBO, arbFBO, etc.).
+//!
+//! OpenGL context might be initialized in Core Profile. In this case deprecated functionality become unavailable.
+//! To make code easily adaptable to wide range of OpenGL versions, function sets related to each version has two kinds of suffixes:
+//!  - "back" for version 3.2+.
+//!     Represents function set for Backward-Compatible Profile.
+//!     Function sets without this suffix represents core profile.
+//!  - "fwd"  for version 3.0-.
+//!     Represents non-deprecated function set of earlier OpenGL versions, which are still available within OpenGL 3.2 Core Profile.
+//!     Function sets without this suffix represents complete list of functions related to specific OpenGL version.
+//!
+//! To select which core** function set should be used in specific case:
+//!  - Determine the minimal OpenGL version required for implemented functionality and use it to access all functions.
+//!    For example, if algorithm requires OpenGL 2.1+, it is better to write core20fwd->glEnable() rather than core11fwd->glEnable() for uniformity.
+//!  - If functionality will work within Core Profile, use function sets with appropriate suffix.
+//!  - Validate minimal requirements at initialization/creation time and omit checks within code where algorithm should be already initialized.
+//!    Properly escape code incompatible with Core Profile. The simplest way to check Core Profile is "if (core11 == NULL)".
 //!
 //! Simplified extensions classification:
 //!  - prefixed with NV, AMD, ATI are vendor-specific (however may be provided by other vendors in some cases);
@@ -132,9 +176,8 @@ typedef OpenGl_TmplCore44<OpenGl_GlCore43>     OpenGl_GlCore44;
 //! In this case developer should be careful because different specification may differ
 //! in aspects (like enumeration values and error-handling).
 //!
-//! Notice that some systems provide mechanisms to simultaneously incorporate with GL contexts
-//! with different capabilities. Thats why OpenGl_Context should be initialized and used
-//! for each GL context individually.
+//! Notice that some systems provide mechanisms to simultaneously incorporate with GL contexts with different capabilities.
+//! For this reason OpenGl_Context should be initialized and used for each GL context independently.
 class OpenGl_Context : public Standard_Transient
 {
 public:
@@ -164,13 +207,16 @@ public:
   //! Destructor.
   Standard_EXPORT virtual ~OpenGl_Context();
 
+  //! Release all resources, including shared ones
+  Standard_EXPORT void forcedRelease();
+
   //! Share GL context resources.
   //! theShareCtx - handle to context to retrieve handles to shared resources.
   Standard_EXPORT void Share (const Handle(OpenGl_Context)& theShareCtx);
 
   //! Initialize class from currently bound OpenGL context. Method should be called only once.
   //! @return false if no GL context is bound to the current thread
-  Standard_EXPORT Standard_Boolean Init();
+  Standard_EXPORT Standard_Boolean Init (const Standard_Boolean theIsCoreProfile = Standard_False);
 
   //! @return true if this context is valid (has been initialized)
   inline Standard_Boolean IsValid() const
@@ -178,12 +224,20 @@ public:
     return myIsInitialized;
   }
 
-#if defined(_WIN32)
+#if defined(HAVE_EGL)
+  //! Initialize class from specified surface and rendering context. Method should be called only once.
+  //! @return false if OpenGL context can not be bound to specified surface
+  Standard_EXPORT Standard_Boolean Init (const Aspect_Drawable         theEglSurface,
+                                         const Aspect_Display          theEglDisplay,
+                                         const Aspect_RenderingContext theEglContext,
+                                         const Standard_Boolean        theIsCoreProfile = Standard_False);
+#elif defined(_WIN32)
   //! Initialize class from specified window and rendering context. Method should be called only once.
   //! @return false if OpenGL context can not be bound to specified window
   Standard_EXPORT Standard_Boolean Init (const Aspect_Handle           theWindow,
                                          const Aspect_Handle           theWindowDC,
-                                         const Aspect_RenderingContext theGContext);
+                                         const Aspect_RenderingContext theGContext,
+                                         const Standard_Boolean        theIsCoreProfile = Standard_False);
 
   //! @return the window handle (HWND) currently bound to this OpenGL context
   inline Aspect_Handle Window() const
@@ -192,14 +246,25 @@ public:
   }
 
 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
+  #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
+
+  //! Initialize class from specified OpenGL ES context (EAGLContext). Method should be called only once.
+  Standard_EXPORT Standard_Boolean Init (EAGLContext*                  theGContext,
+                                         const Standard_Boolean        theIsCoreProfile = Standard_False);
+  #else
+
   //! Initialize class from specified OpenGL context (NSOpenGLContext). Method should be called only once.
-  Standard_EXPORT Standard_Boolean Init (const void*                   theGContext);
+  Standard_EXPORT Standard_Boolean Init (NSOpenGLContext*              theGContext,
+                                         const Standard_Boolean        theIsCoreProfile = Standard_False);
+  #endif
 #else
+
   //! Initialize class from specified window and rendering context. Method should be called only once.
   //! @return false if OpenGL context can not be bound to specified window
   Standard_EXPORT Standard_Boolean Init (const Aspect_Drawable         theWindow,
                                          const Aspect_Display          theDisplay,
-                                         const Aspect_RenderingContext theGContext);
+                                         const Aspect_RenderingContext theGContext,
+                                         const Standard_Boolean        theIsCoreProfile = Standard_False);
 
   //! @return the window handle (GLXDrawable) currently bound to this OpenGL context
   inline Aspect_Drawable Window() const
@@ -208,6 +273,10 @@ public:
   }
 #endif
 
+  //! Read OpenGL version information from active context.
+  Standard_EXPORT static void ReadGlVersion (Standard_Integer& theGlVerMajor,
+                                             Standard_Integer& theGlVerMinor);
+
   //! Check if theExtName extension is supported by active GL context.
   Standard_EXPORT Standard_Boolean CheckExtension (const char* theExtName) const;
 
@@ -235,6 +304,9 @@ public:
         || (myGlVerMajor == theVerMajor && myGlVerMinor >= theVerMinor);
   }
 
+  //! Access entire map of loaded OpenGL functions.
+  const OpenGl_GlFunctions* Functions() const { return myFuncs.operator->(); }
+
   //! Clean up errors stack for this GL context (glGetError() in loop).
   Standard_EXPORT void ResetErrors();
 
@@ -250,16 +322,27 @@ public:
   //! Swap front/back buffers for this GL context (should be activated before!).
   Standard_EXPORT void SwapBuffers();
 
+  //! Setup swap interval (VSync).
+  Standard_EXPORT Standard_Boolean SetSwapInterval (const Standard_Integer theInterval);
+
   //! Return true if active mode is GL_RENDER (cached state)
   Standard_Boolean IsRender() const
   {
+  #if !defined(GL_ES_VERSION_2_0)
     return myRenderMode == GL_RENDER;
+  #else
+    return Standard_True;
+  #endif
   }
 
   //! Return true if active mode is GL_FEEDBACK (cached state)
   Standard_Boolean IsFeedback() const
   {
+  #if !defined(GL_ES_VERSION_2_0)
     return myRenderMode == GL_FEEDBACK;
+  #else
+    return Standard_False;
+  #endif
   }
 
   //! This function retrieves information from GL about free GPU memory that is:
@@ -334,6 +417,9 @@ public:
 
 public:
 
+  //! Either GL_CLAMP_TO_EDGE (1.2+) or GL_CLAMP (1.1).
+  Standard_Integer TextureWrapClamp() const { return myTexClamp; }
+
   //! @return maximum degree of anisotropy texture filter
   Standard_EXPORT Standard_Integer MaxDegreeOfAnisotropy() const;
 
@@ -346,10 +432,36 @@ public:
   //! @return value for GL_MAX_CLIP_PLANES
   Standard_EXPORT Standard_Integer MaxClipPlanes() const;
 
+  //! Returns true if VBO is supported and permitted.
+  inline bool ToUseVbo() const
+  {
+    return core15fwd != NULL
+       && !caps->vboDisable;
+  }
+
+  //! @return cached state of GL_NORMALIZE.
+  Standard_Boolean IsGlNormalizeEnabled() const { return myIsGlNormalizeEnabled; }
+
+  //! Sets GL_NORMALIZE enabled or disabled.
+  //! @return old value of the flag
+  Standard_EXPORT Standard_Boolean SetGlNormalizeEnabled (Standard_Boolean isEnabled);
+
+  //! Applies matrix stored in ModelWorldState to OpenGl.
+  Standard_EXPORT void ApplyModelWorldMatrix();
+
+  //! Applies matrix stored in WorldViewState to OpenGl.
+  Standard_EXPORT void ApplyWorldViewMatrix();
+
+  //! Applies combination of matrices stored in ModelWorldState and WorldViewState to OpenGl.
+  Standard_EXPORT void ApplyModelViewMatrix();
+
+  //! Applies matrix stored in ProjectionState to OpenGl.
+  Standard_EXPORT void ApplyProjectionMatrix();
+
 public:
 
   //! @return messenger instance
-  inline const Handle_Message_Messenger& Messenger() const
+  inline const Handle(Message_Messenger)& Messenger() const
   {
     return ::Message::DefaultMessenger();
   }
@@ -366,29 +478,45 @@ public:
                                     const unsigned int theSeverity,
                                     const TCollection_ExtendedString& theMessage);
 
+  //! Adds a filter for messages with theId and theSource (GL_DEBUG_SOURCE_)
+  Standard_EXPORT Standard_Boolean ExcludeMessage (const unsigned int theSource,
+                                                   const unsigned int theId);
 
+  //! Removes a filter for messages with theId and theSource (GL_DEBUG_SOURCE_)
+  Standard_EXPORT Standard_Boolean IncludeMessage (const unsigned int theSource,
+                                                   const unsigned int theId);
 
   //! @return true if OpenGl context supports left and
   //! right rendering buffers.
   Standard_Boolean HasStereoBuffers() const
   {
+  #if !defined(GL_ES_VERSION_2_0)
     return myIsStereoBuffers;
+  #else
+    return Standard_False;
+  #endif
   }
 
-  //! Switch to left stereographic rendering buffer.
-  //! This method can be used to keep unchanged choise
-  //! of front/back/both buffer rendering.
-  Standard_EXPORT void SetDrawBufferLeft();
+public: //! @name methods to alter or retrieve current state
+
+  //! Return active read buffer.
+  Standard_Integer ReadBuffer() { return myReadBuffer; }
 
-  //! Switch to right stereographic rendering buffer.
-  //! This method can be used to keep unchanged choise
-  //! of front/back/both buffer rendering.
-  Standard_EXPORT void SetDrawBufferRight();
+  //! Switch read buffer, wrapper for ::glReadBuffer().
+  Standard_EXPORT void SetReadBuffer (const Standard_Integer theReadBuffer);
 
-  //! Switch to non-stereographic rendering buffer.
-  //! This method can be used to keep unchanged choise
-  //! of front/back/both buffer rendering.
-  Standard_EXPORT void SetDrawBufferMono();
+  //! Return active draw buffer.
+  Standard_Integer DrawBuffer() { return myDrawBuffer; }
+
+  //! Switch draw buffer, wrapper for ::glDrawBuffer().
+  Standard_EXPORT void SetDrawBuffer (const Standard_Integer theDrawBuffer);
+
+  //! Switch read/draw buffers.
+  void SetReadDrawBuffer (const Standard_Integer theBuffer)
+  {
+    SetReadBuffer (theBuffer);
+    SetDrawBuffer (theBuffer);
+  }
 
   //! Fetch OpenGl context state. This class tracks value of several OpenGl
   //! state variables. Consulting the cached values is quicker than
@@ -396,16 +524,76 @@ public:
   //! OpenGl state variables has a possibility of being out-of-date.
   Standard_EXPORT void FetchState();
 
+  //! @return active GLSL program
+  const Handle(OpenGl_ShaderProgram)& ActiveProgram() const
+  {
+    return myActiveProgram;
+  }
+
+  //! @return OpenGL sampler object used to override default texture parameters
+  const Handle(OpenGl_Sampler)& TextureSampler()
+  {
+    return myTexSampler;
+  }
+
+  //! Bind specified program to current context,
+  //! or unbind previous one when NULL specified.
+  //! @return true if some program is bound to context
+  Standard_EXPORT Standard_Boolean BindProgram (const Handle(OpenGl_ShaderProgram)& theProgram);
+
+  //! Setup current color.
+  Standard_EXPORT void SetColor4fv (const OpenGl_Vec4& theColor);
+
+  //! Setup type of line.
+  Standard_EXPORT void SetTypeOfLine (const Aspect_TypeOfLine  theType,
+                                      const Standard_ShortReal theFactor = 1.0f);
+
+  //! Setup width of line.
+  Standard_EXPORT void SetLineWidth (const Standard_ShortReal theWidth);
+
+  //! Setup point size.
+  Standard_EXPORT void SetPointSize (const Standard_ShortReal theSize);
+
+  //! Setup texture matrix to active GLSL program or to FFP global state using glMatrixMode (GL_TEXTURE).
+  Standard_EXPORT void SetTextureMatrix (const Handle(Graphic3d_TextureParams)& theParams);
+
+  //! Bind default Vertex Array Object
+  Standard_EXPORT void BindDefaultVao();
+
+  //! Default Frame Buffer Object.
+  const Handle(OpenGl_FrameBuffer)& DefaultFrameBuffer() const
+  {
+    return myDefaultFbo;
+  }
+
+  //! Setup new Default Frame Buffer Object and return previously set.
+  //! This call doesn't change Active FBO!
+  Standard_EXPORT Handle(OpenGl_FrameBuffer) SetDefaultFrameBuffer (const Handle(OpenGl_FrameBuffer)& theFbo);
+
+  //! Return debug context initialization state.
+  Standard_Boolean IsDebugContext() const
+  {
+    return myIsGlDebugCtx;
+  }
+
+  Standard_EXPORT void EnableFeatures() const;
+
+  Standard_EXPORT void DisableFeatures() const;
+
 private:
 
   //! Wrapper to system function to retrieve GL function pointer by name.
   Standard_EXPORT void* findProc (const char* theFuncName);
 
-  //! Read OpenGL version information from active context.
-  Standard_EXPORT void readGlVersion();
+  //! Print error if not all functions have been exported by context for reported version.
+  //! Note that this will never happen when using GLX, since returned functions can not be validated.
+  //! @param theGlVerMajor the OpenGL major version with missing functions
+  //! @param theGlVerMinor the OpenGL minor version with missing functions
+  Standard_EXPORT void checkWrongVersion (const Standard_Integer theGlVerMajor,
+                                          const Standard_Integer theGlVerMinor);
 
   //! Private initialization function that should be called only once.
-  Standard_EXPORT void init();
+  Standard_EXPORT void init (const Standard_Boolean theIsCoreProfile);
 
 public: //! @name core profiles
 
@@ -417,6 +605,8 @@ public: //! @name core profiles
   OpenGl_GlCore20Fwd*  core20fwd;  //!< OpenGL 2.0 without deprecated entry points
   OpenGl_GlCore32*     core32;     //!< OpenGL 3.2 core profile
   OpenGl_GlCore32Back* core32back; //!< OpenGL 3.2 backward compatibility profile
+  OpenGl_GlCore33*     core33;     //!< OpenGL 3.3 core profile
+  OpenGl_GlCore33Back* core33back; //!< OpenGL 3.3 backward compatibility profile
   OpenGl_GlCore41*     core41;     //!< OpenGL 4.1 core profile
   OpenGl_GlCore41Back* core41back; //!< OpenGL 4.1 backward compatibility profile
   OpenGl_GlCore42*     core42;     //!< OpenGL 4.2 core profile
@@ -430,27 +620,41 @@ public: //! @name core profiles
 
 public: //! @name extensions
 
-  Standard_Boolean arbNPTW; //!< GL_ARB_texture_non_power_of_two
-  Standard_Boolean arbTexRG;//!< GL_ARB_texture_rg
-  OpenGl_ArbTBO*   arbTBO;  //!< GL_ARB_texture_buffer_object
-  OpenGl_ArbIns*   arbIns;  //!< GL_ARB_draw_instanced
-  OpenGl_ArbDbg*   arbDbg;  //!< GL_ARB_debug_output
-  OpenGl_ArbFBO*   arbFBO;  //!< GL_ARB_framebuffer_object
-  OpenGl_ExtGS*    extGS;   //!< GL_EXT_geometry_shader4
-  Standard_Boolean extBgra; //!< GL_EXT_bgra
-  Standard_Boolean extAnis; //!< GL_EXT_texture_filter_anisotropic
-  Standard_Boolean extPDS;  //!< GL_EXT_packed_depth_stencil
-  Standard_Boolean atiMem;  //!< GL_ATI_meminfo
-  Standard_Boolean nvxMem;  //!< GL_NVX_gpu_memory_info
+  Standard_Boolean       hasHighp;       //!< highp in GLSL ES fragment shader is supported
+  Standard_Boolean       hasUintIndex;   //!< GLuint for index buffer is supported (always available on desktop; on OpenGL ES - since 3.0 or as extension GL_OES_element_index_uint)
+  Standard_Boolean       hasTexRGBA8;    //!< always available on desktop; on OpenGL ES - since 3.0 or as extension GL_OES_rgb8_rgba8
+  Standard_Boolean       arbNPTW;        //!< GL_ARB_texture_non_power_of_two
+  Standard_Boolean       arbTexRG;       //!< GL_ARB_texture_rg
+  OpenGl_ArbTexBindless* arbTexBindless; //!< GL_ARB_bindless_texture
+  OpenGl_ArbTBO*         arbTBO;         //!< GL_ARB_texture_buffer_object
+  Standard_Boolean       arbTboRGB32;    //!< GL_ARB_texture_buffer_object_rgb32 (3-component TBO), in core since 4.0
+  OpenGl_ArbIns*         arbIns;         //!< GL_ARB_draw_instanced
+  OpenGl_ArbDbg*         arbDbg;         //!< GL_ARB_debug_output
+  OpenGl_ArbFBO*         arbFBO;         //!< GL_ARB_framebuffer_object
+  OpenGl_ArbFBOBlit*     arbFBOBlit;     //!< glBlitFramebuffer function, moved out from OpenGl_ArbFBO structure for compatibility with OpenGL ES 2.0
+  OpenGl_ExtGS*          extGS;          //!< GL_EXT_geometry_shader4
+  Standard_Boolean       extBgra;        //!< GL_EXT_bgra or GL_EXT_texture_format_BGRA8888 on OpenGL ES
+  Standard_Boolean       extAnis;        //!< GL_EXT_texture_filter_anisotropic
+  Standard_Boolean       extPDS;         //!< GL_EXT_packed_depth_stencil
+  Standard_Boolean       atiMem;         //!< GL_ATI_meminfo
+  Standard_Boolean       nvxMem;         //!< GL_NVX_gpu_memory_info
 
 private: // system-dependent fields
 
-#if defined(_WIN32)
+#if defined(HAVE_EGL)
+  Aspect_Drawable         myWindow;   //!< EGL surface                   : EGLSurface
+  Aspect_Display          myDisplay;  //!< EGL connection to the Display : EGLDisplay
+  Aspect_RenderingContext myGContext; //!< EGL rendering context         : EGLContext
+#elif defined(_WIN32)
   Aspect_Handle           myWindow;   //!< window handle (owner of GL context) : HWND
   Aspect_Handle           myWindowDC; //!< Device Descriptor handle : HDC
   Aspect_RenderingContext myGContext; //!< Rendering Context handle : HGLRC
 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
-  void*                   myGContext; //!< Rendering Context handle : NSOpenGLContext
+  #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
+  EAGLContext*            myGContext;    //!< Rendering Context handle
+  #else
+  NSOpenGLContext*        myGContext; //!< Rendering Context handle
+  #endif
 #else
   Aspect_Drawable         myWindow;   //!< window handle (owner of GL context) : GLXDrawable
   Aspect_Display          myDisplay;  //!< connection to the X-server : Display*
@@ -459,34 +663,51 @@ private: // system-dependent fields
 
 private: // context info
 
-  typedef NCollection_DataMap<TCollection_AsciiString, Standard_Integer> OpenGl_DelayReleaseMap;
-  typedef NCollection_Handle<OpenGl_DelayReleaseMap> Handle(OpenGl_DelayReleaseMap);
-  typedef NCollection_DataMap<TCollection_AsciiString, Handle(OpenGl_Resource)> OpenGl_ResourcesMap;
-  typedef NCollection_Handle<OpenGl_ResourcesMap> Handle(OpenGl_ResourcesMap);
-  typedef NCollection_Queue<Handle(OpenGl_Resource)> OpenGl_ResourcesQueue;
-  typedef NCollection_Handle<OpenGl_ResourcesQueue> Handle(OpenGl_ResourcesQueue);
+  typedef NCollection_Shared< NCollection_DataMap<TCollection_AsciiString, Standard_Integer> > OpenGl_DelayReleaseMap;
+  typedef NCollection_Shared< NCollection_DataMap<TCollection_AsciiString, Handle(OpenGl_Resource)> > OpenGl_ResourcesMap;
+  typedef NCollection_Shared< NCollection_List<Handle(OpenGl_Resource)> > OpenGl_ResourcesStack;
 
   Handle(OpenGl_ResourcesMap)    mySharedResources; //!< shared resources with unique identification key
   Handle(OpenGl_DelayReleaseMap) myDelayed;         //!< shared resources for delayed release
-  Handle(OpenGl_ResourcesQueue)  myReleaseQueue;    //!< queue of resources for delayed clean up
+  Handle(OpenGl_ResourcesStack)  myUnusedResources; //!< stack of resources for delayed clean up
 
   OpenGl_Clipping myClippingState; //!< state of clip planes
 
-  void*            myGlLibHandle;     //!< optional handle to GL library
+  void*            myGlLibHandle;          //!< optional handle to GL library
   NCollection_Handle<OpenGl_GlFunctions>
-                   myFuncs;           //!< mega structure for all GL functions
-  Standard_Integer myAnisoMax;        //!< maximum level of anisotropy texture filter
-  Standard_Integer myMaxTexDim;       //!< value for GL_MAX_TEXTURE_SIZE
-  Standard_Integer myMaxClipPlanes;   //!< value for GL_MAX_CLIP_PLANES
-  Standard_Integer myGlVerMajor;      //!< cached GL version major number
-  Standard_Integer myGlVerMinor;      //!< cached GL version minor number
-  Standard_Integer myRenderMode;      //!< value for active rendering mode
-  Standard_Boolean myIsInitialized;   //!< flag indicates initialization state
-  Standard_Boolean myIsStereoBuffers; //!< context supports stereo buffering
-  Standard_Integer myDrawBuffer;      //!< current draw buffer.
+                   myFuncs;                //!< mega structure for all GL functions
+  Standard_Integer myAnisoMax;             //!< maximum level of anisotropy texture filter
+  Standard_Integer myTexClamp;             //!< either GL_CLAMP_TO_EDGE (1.2+) or GL_CLAMP (1.1)
+  Standard_Integer myMaxTexDim;            //!< value for GL_MAX_TEXTURE_SIZE
+  Standard_Integer myMaxClipPlanes;        //!< value for GL_MAX_CLIP_PLANES
+  Standard_Integer myGlVerMajor;           //!< cached GL version major number
+  Standard_Integer myGlVerMinor;           //!< cached GL version minor number
+  Standard_Boolean myIsInitialized;        //!< flag indicates initialization state
+  Standard_Boolean myIsStereoBuffers;      //!< context supports stereo buffering
+  Standard_Boolean myIsGlNormalizeEnabled; //!< GL_NORMALIZE flag
+                                           //!< Used to tell OpenGl that normals should be normalized
 
   Handle(OpenGl_ShaderManager) myShaderManager; //! support object for managing shader programs
 
+private: //! @name fields tracking current state
+
+  Handle(OpenGl_ShaderProgram) myActiveProgram; //!< currently active GLSL program
+  Handle(OpenGl_Sampler)       myTexSampler;    //!< currently active sampler object
+  Handle(OpenGl_FrameBuffer)   myDefaultFbo;    //!< default Frame Buffer Object
+  Standard_Integer             myRenderMode;    //!< value for active rendering mode
+  Standard_Integer             myReadBuffer;    //!< current read buffer
+  Standard_Integer             myDrawBuffer;    //!< current draw buffer
+  unsigned int                 myDefaultVao;    //!< default Vertex Array Object
+  Standard_Boolean             myIsGlDebugCtx;  //!< debug context initialization state
+  TCollection_AsciiString      myVendor;        //!< Graphics Driver's vendor
+  TColStd_PackedMapOfInteger   myFilters[6];    //!< messages suppressing filter (for sources from GL_DEBUG_SOURCE_API_ARB to GL_DEBUG_SOURCE_OTHER_ARB)
+
+public:
+
+  OpenGl_MatrixState<Standard_ShortReal> ModelWorldState; //!< state of orientation matrix
+  OpenGl_MatrixState<Standard_ShortReal> WorldViewState;  //!< state of orientation matrix
+  OpenGl_MatrixState<Standard_ShortReal> ProjectionState; //!< state of projection  matrix
+
 private:
 
   //! Copying allowed only within Handles
@@ -495,7 +716,7 @@ private:
 
 public:
 
-  DEFINE_STANDARD_RTTI(OpenGl_Context) // Type definition
+  DEFINE_STANDARD_RTTI(OpenGl_Context, Standard_Transient) // Type definition
 
   friend class OpenGl_Window;