0026122: Visualization, TKOpenGl - clipping and capping is broken when ffp is disable...
[occt.git] / src / OpenGl / OpenGl_Context.cxx
index 31c803b..2a960a2 100644 (file)
 #include <OpenGl_ArbDbg.hxx>
 #include <OpenGl_ArbFBO.hxx>
 #include <OpenGl_ExtGS.hxx>
-#include <OpenGl_GlCore20.hxx>
+#include <OpenGl_ArbTexBindless.hxx>
+#include <OpenGl_GlCore44.hxx>
+#include <OpenGl_FrameBuffer.hxx>
+#include <OpenGl_Sampler.hxx>
 #include <OpenGl_ShaderManager.hxx>
+#include <Graphic3d_TransformUtils.hxx>
 
 #include <Message_Messenger.hxx>
 
   //
 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
   #include <dlfcn.h>
+  #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
+    //
+  #else
+    #include <OpenGL/OpenGL.h>
+  #endif
 #else
   #include <GL/glx.h> // glXGetProcAddress()
 #endif
 
-// GL_NVX_gpu_memory_info
-#ifndef GL_GPU_MEMORY_INFO_DEDICATED_VIDMEM_NVX
-  enum
-  {
-    GL_GPU_MEMORY_INFO_DEDICATED_VIDMEM_NVX         = 0x9047,
-    GL_GPU_MEMORY_INFO_TOTAL_AVAILABLE_MEMORY_NVX   = 0x9048,
-    GL_GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX = 0x9049,
-    GL_GPU_MEMORY_INFO_EVICTION_COUNT_NVX           = 0x904A,
-    GL_GPU_MEMORY_INFO_EVICTED_MEMORY_NVX           = 0x904B
-  };
+#ifdef HAVE_GL2PS
+  #include <gl2ps.h>
+  #ifdef _MSC_VER
+    #pragma comment (lib, "gl2ps.lib")
+  #endif
 #endif
 
-IMPLEMENT_STANDARD_HANDLE (OpenGl_Context, Standard_Transient)
-IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Context, Standard_Transient)
 
 namespace
 {
@@ -79,6 +81,8 @@ OpenGl_Context::OpenGl_Context (const Handle(OpenGl_Caps)& theCaps)
   core20fwd  (NULL),
   core32     (NULL),
   core32back (NULL),
+  core33     (NULL),
+  core33back (NULL),
   core41     (NULL),
   core41back (NULL),
   core42     (NULL),
@@ -90,16 +94,22 @@ OpenGl_Context::OpenGl_Context (const Handle(OpenGl_Caps)& theCaps)
   caps   (!theCaps.IsNull() ? theCaps : new OpenGl_Caps()),
 #if defined(GL_ES_VERSION_2_0)
   hasHighp   (Standard_False),
+  hasUintIndex(Standard_False),
   hasTexRGBA8(Standard_False),
 #else
   hasHighp   (Standard_True),
+  hasUintIndex(Standard_True),
   hasTexRGBA8(Standard_True),
 #endif
-  arbNPTW(Standard_False),
+  arbNPTW  (Standard_False),
+  arbTexRG (Standard_False),
+  arbTexBindless (NULL),
   arbTBO (NULL),
+  arbTboRGB32 (Standard_False),
   arbIns (NULL),
   arbDbg (NULL),
   arbFBO (NULL),
+  arbFBOBlit (NULL),
   extGS  (NULL),
   extBgra(Standard_False),
   extAnis(Standard_False),
@@ -120,14 +130,27 @@ OpenGl_Context::OpenGl_Context (const Handle(OpenGl_Caps)& theCaps)
   myGlVerMinor (0),
   myIsInitialized (Standard_False),
   myIsStereoBuffers (Standard_False),
+  myIsGlNormalizeEnabled (Standard_False),
 #if !defined(GL_ES_VERSION_2_0)
   myRenderMode (GL_RENDER),
 #else
   myRenderMode (0),
 #endif
-  myDrawBuffer (0)
+  myReadBuffer (0),
+  myDrawBuffer (0),
+  myDefaultVao (0),
+  myIsGlDebugCtx (Standard_False)
 {
-#if defined(MAC_OS_X_VERSION_10_3) && !defined(MACOSX_USE_GLX)
+  // system-dependent fields
+#if defined(HAVE_EGL)
+  myDisplay  = (Aspect_Display          )EGL_NO_DISPLAY;
+  myWindow   = (Aspect_Drawable         )EGL_NO_SURFACE;
+  myGContext = (Aspect_RenderingContext )EGL_NO_CONTEXT;
+#elif defined(_WIN32)
+  myWindow   = NULL;
+  myWindowDC = NULL;
+  myGContext = NULL;
+#elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
   // Vendors can not extend functionality on this system
   // and developers are limited to OpenGL support provided by Mac OS X SDK.
   // We retrieve function pointers from system library
@@ -137,8 +160,19 @@ OpenGl_Context::OpenGl_Context (const Handle(OpenGl_Caps)& theCaps)
   // Notice that GL version / extension availability checks are required
   // because function pointers may be available but not functionality itself
   // (depends on renderer).
+#if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
+  myGContext    = NULL;
+  myGlLibHandle = dlopen ("/System/Library/Frameworks/OpenGLES.framework/OpenGLES", RTLD_LAZY);
+#else
+  myGContext    = NULL;
   myGlLibHandle = dlopen ("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL", RTLD_LAZY);
 #endif
+#else
+  myDisplay =  NULL;
+  myWindow   = 0;
+  myGContext = 0;
+#endif
+
   memset (myFuncs.operator->(), 0, sizeof(OpenGl_GlFunctions));
   myShaderManager = new OpenGl_ShaderManager (this);
 }
@@ -152,6 +186,24 @@ OpenGl_Context::~OpenGl_Context()
   // release clean up queue
   ReleaseDelayed();
 
+#if !defined(GL_ES_VERSION_2_0)
+  // release default VAO
+  if (myDefaultVao != 0
+   && IsValid()
+   && core32 != NULL)
+  {
+    core32->glDeleteVertexArrays (1, &myDefaultVao);
+  }
+  myDefaultVao = 0;
+#endif
+
+  // release default FBO
+  if (!myDefaultFbo.IsNull())
+  {
+    myDefaultFbo->Release (this);
+    myDefaultFbo.Nullify();
+  }
+
   // release shared resources if any
   if (((const Handle(Standard_Transient)& )mySharedResources)->GetRefCount() <= 1)
   {
@@ -161,17 +213,30 @@ OpenGl_Context::~OpenGl_Context()
     {
       anIter.Value()->Release (this);
     }
+
+    // release delayed resources added during deletion of shared resources
+    while (!myUnusedResources->IsEmpty())
+    {
+      myUnusedResources->First()->Release (this);
+      myUnusedResources->RemoveFirst();
+    }
   }
-  else
+  else if (myShaderManager->IsSameContext (this))
   {
     myShaderManager->SetContext (NULL);
   }
   mySharedResources.Nullify();
   myDelayed.Nullify();
 
+  // release sampler object
+  if (!myTexSampler.IsNull())
+  {
+    myTexSampler->Release (this);
+  }
+
 #if !defined(GL_ES_VERSION_2_0)
   if (arbDbg != NULL
-   && caps->contextDebug
+   && myIsGlDebugCtx
    && IsValid())
   {
     // reset callback
@@ -181,10 +246,35 @@ OpenGl_Context::~OpenGl_Context()
     {
       arbDbg->glDebugMessageCallbackARB (NULL, NULL);
     }
+    myIsGlDebugCtx = Standard_False;
   }
 #endif
 }
 
+// =======================================================================
+// function : forcedRelease
+// purpose  :
+// =======================================================================
+void OpenGl_Context::forcedRelease()
+{
+  ReleaseDelayed();
+  for (NCollection_DataMap<TCollection_AsciiString, Handle(OpenGl_Resource)>::Iterator anIter (*mySharedResources);
+       anIter.More(); anIter.Next())
+  {
+    anIter.Value()->Release (this);
+  }
+  mySharedResources->Clear();
+  myShaderManager->clear();
+  myShaderManager->SetContext (NULL);
+
+  // release delayed resources added during deletion of shared resources
+  while (!myUnusedResources->IsEmpty())
+  {
+    myUnusedResources->First()->Release (this);
+    myUnusedResources->RemoveFirst();
+  }
+}
+
 // =======================================================================
 // function : MaxDegreeOfAnisotropy
 // purpose  :
@@ -212,93 +302,58 @@ Standard_Integer OpenGl_Context::MaxClipPlanes() const
   return myMaxClipPlanes;
 }
 
-// =======================================================================
-// function : SetDrawBufferLeft
-// purpose  :
-// =======================================================================
-void OpenGl_Context::SetDrawBufferLeft()
-{
 #if !defined(GL_ES_VERSION_2_0)
-  switch (myDrawBuffer)
+inline Standard_Integer stereoToMonoBuffer (const Standard_Integer theBuffer)
+{
+  switch (theBuffer)
   {
-    case GL_BACK_RIGHT :
-    case GL_BACK :
-      glDrawBuffer (GL_BACK_LEFT);
-      myDrawBuffer = GL_BACK_LEFT;
-      break;
-
-    case GL_FRONT_RIGHT :
-    case GL_FRONT :
-      glDrawBuffer (GL_FRONT_LEFT);
-      myDrawBuffer = GL_FRONT_LEFT;
-      break;
-
-    case GL_FRONT_AND_BACK :
-    case GL_RIGHT :
-      glDrawBuffer (GL_LEFT);
-      myDrawBuffer = GL_LEFT;
-      break;
+    case GL_BACK_LEFT:
+    case GL_BACK_RIGHT:
+      return GL_BACK;
+    case GL_FRONT_LEFT:
+    case GL_FRONT_RIGHT:
+      return GL_FRONT;
+    default:
+      return theBuffer;
   }
-#endif
 }
+#endif
 
 // =======================================================================
-// function : SetDrawBufferRight
+// function : SetReadBuffer
 // purpose  :
 // =======================================================================
-void OpenGl_Context::SetDrawBufferRight()
+void OpenGl_Context::SetReadBuffer (const Standard_Integer theReadBuffer)
 {
 #if !defined(GL_ES_VERSION_2_0)
-  switch (myDrawBuffer)
+  myReadBuffer = !myIsStereoBuffers ? stereoToMonoBuffer (theReadBuffer) : theReadBuffer;
+  if (myReadBuffer < GL_COLOR_ATTACHMENT0
+   && arbFBO != NULL)
   {
-    case GL_BACK_LEFT :
-    case GL_BACK :
-      glDrawBuffer (GL_BACK_RIGHT);
-      myDrawBuffer = GL_BACK_RIGHT;
-      break;
-
-    case GL_FRONT_LEFT :
-    case GL_FRONT :
-      glDrawBuffer (GL_FRONT_RIGHT);
-      myDrawBuffer = GL_FRONT_RIGHT;
-      break;
-
-    case GL_FRONT_AND_BACK :
-    case GL_LEFT :
-      glDrawBuffer (GL_RIGHT);
-      myDrawBuffer = GL_RIGHT;
-      break;
+    arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
   }
+  ::glReadBuffer (myReadBuffer);
+#else
+  (void )theReadBuffer;
 #endif
 }
 
 // =======================================================================
-// function : SetDrawBufferMono
+// function : SetDrawBuffer
 // purpose  :
 // =======================================================================
-void OpenGl_Context::SetDrawBufferMono()
+void OpenGl_Context::SetDrawBuffer (const Standard_Integer theDrawBuffer)
 {
 #if !defined(GL_ES_VERSION_2_0)
-  switch (myDrawBuffer)
+  myDrawBuffer = !myIsStereoBuffers ? stereoToMonoBuffer (theDrawBuffer) : theDrawBuffer;
+  if (myDrawBuffer < GL_COLOR_ATTACHMENT0
+   && arbFBO != NULL)
   {
-    case GL_BACK_LEFT :
-    case GL_BACK_RIGHT :
-      glDrawBuffer (GL_BACK);
-      myDrawBuffer = GL_BACK;
-      break;
-
-    case GL_FRONT_LEFT :
-    case GL_FRONT_RIGHT :
-      glDrawBuffer (GL_FRONT);
-      myDrawBuffer = GL_FRONT;
-      break;
-
-    case GL_LEFT :
-    case GL_RIGHT :
-      glDrawBuffer (GL_FRONT_AND_BACK);
-      myDrawBuffer = GL_FRONT_AND_BACK;
-      break;
+    arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
   }
+  ::glDrawBuffer (myDrawBuffer);
+#else
+  (void )theDrawBuffer;
 #endif
 }
 
@@ -310,10 +365,14 @@ void OpenGl_Context::FetchState()
 {
 #if !defined(GL_ES_VERSION_2_0)
   // cache feedback mode state
-  glGetIntegerv (GL_RENDER_MODE, &myRenderMode);
+  if (core11 != NULL)
+  {
+    ::glGetIntegerv (GL_RENDER_MODE, &myRenderMode);
+  }
 
-  // cache draw buffer state
-  glGetIntegerv (GL_DRAW_BUFFER, &myDrawBuffer);
+  // cache buffers state
+  ::glGetIntegerv (GL_READ_BUFFER, &myReadBuffer);
+  ::glGetIntegerv (GL_DRAW_BUFFER, &myDrawBuffer);
 #endif
 }
 
@@ -471,6 +530,48 @@ void OpenGl_Context::SwapBuffers()
 
 #endif // __APPLE__
 
+// =======================================================================
+// function : SetSwapInterval
+// purpose  :
+// =======================================================================
+Standard_Boolean OpenGl_Context::SetSwapInterval (const Standard_Integer theInterval)
+{
+#if defined(HAVE_EGL)
+  if (::eglSwapInterval ((EGLDisplay )myDisplay, theInterval) == EGL_TRUE)
+  {
+    return Standard_True;
+  }
+#elif defined(_WIN32)
+  if (myFuncs->wglSwapIntervalEXT != NULL)
+  {
+    myFuncs->wglSwapIntervalEXT (theInterval);
+    return Standard_True;
+  }
+#elif defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
+  //
+#elif defined(__APPLE__)
+  if (::CGLSetParameter (CGLGetCurrentContext(), kCGLCPSwapInterval, &theInterval) == kCGLNoError)
+  {
+    return Standard_True;
+  }
+#else
+  if (theInterval == -1
+   && myFuncs->glXSwapIntervalEXT != NULL)
+  {
+    typedef int (*glXSwapIntervalEXT_t_x)(Display* theDisplay, GLXDrawable theDrawable, int theInterval);
+    glXSwapIntervalEXT_t_x aFuncPtr = (glXSwapIntervalEXT_t_x )myFuncs->glXSwapIntervalEXT;
+    aFuncPtr ((Display* )myDisplay, (GLXDrawable )myWindow, theInterval);
+    return Standard_True;
+  }
+  else if (myFuncs->glXSwapIntervalSGI != NULL)
+  {
+    myFuncs->glXSwapIntervalSGI (theInterval);
+    return Standard_True;
+  }
+#endif
+  return Standard_False;
+}
+
 // =======================================================================
 // function : findProc
 // purpose  :
@@ -496,27 +597,34 @@ Standard_Boolean OpenGl_Context::CheckExtension (const char* theExtName) const
 {
   if (theExtName  == NULL)
   {
+#ifdef OCCT_DEBUG
     std::cerr << "CheckExtension called with NULL string!\n";
+#endif
     return Standard_False;
   }
 
+#if !defined(GL_ES_VERSION_2_0)
   // available since OpenGL 3.0
   // and the ONLY way to check extensions with OpenGL 3.1+ core profile
-  /**if (IsGlGreaterEqual (3, 0))
+  if (IsGlGreaterEqual (3, 0)
+   && myFuncs->glGetStringi != NULL)
   {
     GLint anExtNb = 0;
-    glGetIntegerv (GL_NUM_EXTENSIONS, &anExtNb);
+    ::glGetIntegerv (GL_NUM_EXTENSIONS, &anExtNb);
+    const size_t anExtNameLen = strlen (theExtName);
     for (GLint anIter = 0; anIter < anExtNb; ++anIter)
     {
-      const char* anExtension = (const char* )core30->glGetStringi (GL_EXTENSIONS, (GLuint )anIter);
-      if (anExtension[anExtNameLen] == '\0' &&
-          strncmp (anExtension, theExtName, anExtNameLen) == 0)
+      const char* anExtension = (const char* )myFuncs->glGetStringi (GL_EXTENSIONS, (GLuint )anIter);
+      const size_t aTestExtNameLen = strlen (anExtension);
+      if (aTestExtNameLen == anExtNameLen
+       && strncmp (anExtension, theExtName, anExtNameLen) == 0)
       {
         return Standard_True;
       }
     }
     return Standard_False;
-  }*/
+  }
+#endif
 
   // use old way with huge string for all extensions
   const char* anExtString = (const char* )glGetString (GL_EXTENSIONS);
@@ -563,7 +671,7 @@ Standard_Boolean OpenGl_Context::CheckExtension (const char* theExtString,
 // function : Init
 // purpose  :
 // =======================================================================
-Standard_Boolean OpenGl_Context::Init()
+Standard_Boolean OpenGl_Context::Init (const Standard_Boolean theIsCoreProfile)
 {
   if (myIsInitialized)
   {
@@ -587,7 +695,7 @@ Standard_Boolean OpenGl_Context::Init()
     return Standard_False;
   }
 
-  init();
+  init (theIsCoreProfile);
   myIsInitialized = Standard_True;
   return Standard_True;
 }
@@ -601,17 +709,28 @@ Standard_Boolean OpenGl_Context::Init()
 #if defined(HAVE_EGL)
 Standard_Boolean OpenGl_Context::Init (const Aspect_Drawable         theEglSurface,
                                        const Aspect_Display          theEglDisplay,
-                                       const Aspect_RenderingContext theEglContext)
+                                       const Aspect_RenderingContext theEglContext,
+                                       const Standard_Boolean        theIsCoreProfile)
 #elif defined(_WIN32)
 Standard_Boolean OpenGl_Context::Init (const Aspect_Handle           theWindow,
                                        const Aspect_Handle           theWindowDC,
-                                       const Aspect_RenderingContext theGContext)
+                                       const Aspect_RenderingContext theGContext,
+                                       const Standard_Boolean        theIsCoreProfile)
 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
-Standard_Boolean OpenGl_Context::Init (const void*                   theGContext)
+
+#if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
+Standard_Boolean OpenGl_Context::Init (EAGLContext*                  theGContext,
+                                       const Standard_Boolean        theIsCoreProfile)
+#else
+Standard_Boolean OpenGl_Context::Init (NSOpenGLContext*              theGContext,
+                                       const Standard_Boolean        theIsCoreProfile)
+#endif
+
 #else
 Standard_Boolean OpenGl_Context::Init (const Aspect_Drawable         theWindow,
                                        const Aspect_Display          theDisplay,
-                                       const Aspect_RenderingContext theGContext)
+                                       const Aspect_RenderingContext theGContext,
+                                       const Standard_Boolean        theIsCoreProfile)
 #endif
 {
   Standard_ProgramError_Raise_if (myIsInitialized, "OpenGl_Context::Init() should be called only once!");
@@ -624,7 +743,7 @@ Standard_Boolean OpenGl_Context::Init (const Aspect_Drawable         theWindow,
   myGContext = theGContext;
   myWindowDC = theWindowDC;
 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
-  myGContext = (void* )theGContext;
+  myGContext = theGContext;
 #else
   myWindow   = theWindow;
   myGContext = theGContext;
@@ -635,7 +754,7 @@ Standard_Boolean OpenGl_Context::Init (const Aspect_Drawable         theWindow,
     return Standard_False;
   }
 
-  init();
+  init (theIsCoreProfile);
   myIsInitialized = Standard_True;
   return Standard_True;
 }
@@ -653,14 +772,15 @@ void OpenGl_Context::ResetErrors()
 }
 
 // =======================================================================
-// function : readGlVersion
+// function : ReadGlVersion
 // purpose  :
 // =======================================================================
-void OpenGl_Context::readGlVersion()
+void OpenGl_Context::ReadGlVersion (Standard_Integer& theGlVerMajor,
+                                    Standard_Integer& theGlVerMinor)
 {
   // reset values
-  myGlVerMajor = 0;
-  myGlVerMinor = 0;
+  theGlVerMajor = 0;
+  theGlVerMinor = 0;
 
 #ifdef GL_MAJOR_VERSION
   // available since OpenGL 3.0 and OpenGL 3.0 ES
@@ -672,13 +792,20 @@ void OpenGl_Context::readGlVersion()
   // This happens on some renderers like e.g. Cygwin MESA.
   // Thus checking additionally if GL has put anything to
   // the output variables.
-  if (glGetError() == GL_NO_ERROR && aMajor != 0 && aMinor != 0)
+  if (::glGetError() == GL_NO_ERROR && aMajor != 0 && aMinor != 0)
   {
-    myGlVerMajor = aMajor;
-    myGlVerMinor = aMinor;
+    theGlVerMajor = aMajor;
+    theGlVerMinor = aMinor;
     return;
   }
-  ResetErrors();
+  for (GLenum anErr = ::glGetError(), aPrevErr = GL_NO_ERROR;; aPrevErr = anErr, anErr = ::glGetError())
+  {
+    if (anErr == GL_NO_ERROR
+     || anErr == aPrevErr)
+    {
+      break;
+    }
+  }
 #endif
 
   // Read version string.
@@ -734,13 +861,13 @@ void OpenGl_Context::readGlVersion()
   aMinorStr[aMinIter] = '\0';
 
   // read numbers
-  myGlVerMajor = atoi (aMajorStr);
-  myGlVerMinor = atoi (aMinorStr);
+  theGlVerMajor = atoi (aMajorStr);
+  theGlVerMinor = atoi (aMinorStr);
 
-  if (myGlVerMajor <= 0)
+  if (theGlVerMajor <= 0)
   {
-    myGlVerMajor = 0;
-    myGlVerMinor = 0;
+    theGlVerMajor = 0;
+    theGlVerMinor = 0;
   }
 }
 
@@ -794,7 +921,14 @@ void OpenGl_Context::PushMessage (const unsigned int theSource,
                                   const unsigned int theSeverity,
                                   const TCollection_ExtendedString& theMessage)
 {
-  //OpenGl_Context* aCtx = (OpenGl_Context* )theUserParam;
+  if (caps->suppressExtraMsg
+   && theSource >= GL_DEBUG_SOURCE_API_ARB
+   && theSource <= GL_DEBUG_SOURCE_OTHER_ARB
+   && myFilters[theSource - GL_DEBUG_SOURCE_API_ARB].Contains (theId))
+  {
+    return;
+  }
+
   Standard_CString& aSrc = (theSource >= GL_DEBUG_SOURCE_API_ARB
                         && theSource <= GL_DEBUG_SOURCE_OTHER_ARB)
                          ? THE_DBGMSG_SOURCES[theSource - GL_DEBUG_SOURCE_API_ARB]
@@ -824,16 +958,103 @@ void OpenGl_Context::PushMessage (const unsigned int theSource,
   Messenger()->Send (aMsg, aGrav);
 }
 
+// =======================================================================
+// function : ExcludeMessage
+// purpose  :
+// ======================================================================
+Standard_Boolean OpenGl_Context::ExcludeMessage (const unsigned int theSource,
+                                                 const unsigned int theId)
+{
+  return theSource >= GL_DEBUG_SOURCE_API_ARB
+      && theSource <= GL_DEBUG_SOURCE_OTHER_ARB
+      && myFilters[theSource - GL_DEBUG_SOURCE_API_ARB].Add (theId);
+}
+
+// =======================================================================
+// function : IncludeMessage
+// purpose  :
+// ======================================================================
+Standard_Boolean OpenGl_Context::IncludeMessage (const unsigned int theSource,
+                                                 const unsigned int theId)
+{
+  return theSource >= GL_DEBUG_SOURCE_API_ARB
+      && theSource <= GL_DEBUG_SOURCE_OTHER_ARB
+      && myFilters[theSource - GL_DEBUG_SOURCE_API_ARB].Remove (theId);
+}
+
+// =======================================================================
+// function : checkWrongVersion
+// purpose  :
+// ======================================================================
+void OpenGl_Context::checkWrongVersion (const Standard_Integer theGlVerMajor,
+                                        const Standard_Integer theGlVerMinor)
+{
+  if (!IsGlGreaterEqual (theGlVerMajor, theGlVerMinor))
+  {
+    return;
+  }
+
+  TCollection_ExtendedString aMsg = TCollection_ExtendedString()
+    + "Error! OpenGL context reports version "
+    + myGlVerMajor  + "." + myGlVerMinor
+    + " but does not export required functions for "
+    + theGlVerMajor + "." + theGlVerMinor;
+  PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
+               GL_DEBUG_TYPE_ERROR_ARB,
+               0,
+               GL_DEBUG_SEVERITY_HIGH_ARB,
+               aMsg);
+}
+
 // =======================================================================
 // function : init
 // purpose  :
 // =======================================================================
-void OpenGl_Context::init()
+void OpenGl_Context::init (const Standard_Boolean theIsCoreProfile)
 {
   // read version
-  readGlVersion();
+  myGlVerMajor = 0;
+  myGlVerMinor = 0;
+  ReadGlVersion (myGlVerMajor, myGlVerMinor);
+  myVendor = (const char* )::glGetString (GL_VENDOR);
+
+#if defined(GL_ES_VERSION_2_0)
+  (void )theIsCoreProfile;
+  const bool isCoreProfile = false;
+#else
+
+  if (myVendor.Search ("NVIDIA") != -1)
+  {
+    // Buffer detailed info: Buffer object 1 (bound to GL_ARRAY_BUFFER_ARB, usage hint is GL_STATIC_DRAW)
+    // will use VIDEO memory as the source for buffer object operations.
+    ExcludeMessage (GL_DEBUG_SOURCE_API_ARB, 131185);
+  }
+  if (IsGlGreaterEqual (3, 0))
+  {
+    // retrieve auxiliary function in advance
+    FindProc ("glGetStringi", myFuncs->glGetStringi);
+  }
+
+  bool isCoreProfile = false;
+  if (IsGlGreaterEqual (3, 2))
+  {
+    isCoreProfile = (theIsCoreProfile == Standard_True);
 
-  core11     = (OpenGl_GlCore11*    )(&(*myFuncs));
+    // detect Core profile
+    if (!isCoreProfile)
+    {
+      GLint aProfile = 0;
+      ::glGetIntegerv (GL_CONTEXT_PROFILE_MASK, &aProfile);
+      isCoreProfile = (aProfile & GL_CONTEXT_CORE_PROFILE_BIT) != 0;
+    }
+  }
+#endif
+
+  core11     = NULL;
+  if (!isCoreProfile)
+  {
+    core11 = (OpenGl_GlCore11* )(&(*myFuncs));
+  }
   core11fwd  = (OpenGl_GlCore11Fwd* )(&(*myFuncs));
   core15     = NULL;
   core15fwd  = NULL;
@@ -841,6 +1062,8 @@ void OpenGl_Context::init()
   core20fwd  = NULL;
   core32     = NULL;
   core32back = NULL;
+  core33     = NULL;
+  core33back = NULL;
   core41     = NULL;
   core41back = NULL;
   core42     = NULL;
@@ -850,17 +1073,24 @@ void OpenGl_Context::init()
   core44     = NULL;
   core44back = NULL;
   arbTBO     = NULL;
+  arbTboRGB32 = Standard_False;
   arbIns     = NULL;
   arbDbg     = NULL;
   arbFBO     = NULL;
+  arbFBOBlit = NULL;
   extGS      = NULL;
+  myDefaultVao = 0;
 
 #if defined(GL_ES_VERSION_2_0)
 
   hasTexRGBA8 = IsGlGreaterEqual (3, 0)
              || CheckExtension ("GL_OES_rgb8_rgba8");
-  arbNPTW     = IsGlGreaterEqual (3, 0)
-             || CheckExtension ("GL_OES_texture_npot");
+  // NPOT textures has limited support within OpenGL ES 2.0
+  // which are relaxed by OpenGL ES 3.0 or some extensions
+  //arbNPTW     = IsGlGreaterEqual (3, 0)
+  //           || CheckExtension ("GL_OES_texture_npot")
+  //           || CheckExtension ("GL_NV_texture_npot_2D_mipmap");
+  arbNPTW     = Standard_True;
   arbTexRG    = IsGlGreaterEqual (3, 0)
              || CheckExtension ("GL_EXT_texture_rg");
   extBgra     = CheckExtension ("GL_EXT_texture_format_BGRA8888");
@@ -874,14 +1104,21 @@ void OpenGl_Context::init()
     core20    = (OpenGl_GlCore20*    )(&(*myFuncs));
     core20fwd = (OpenGl_GlCore20Fwd* )(&(*myFuncs));
     core15fwd = (OpenGl_GlCore15Fwd* )(&(*myFuncs));
-    arbFBO    = (OpenGl_ArbFBO* )(&(*myFuncs));
+    arbFBO    = (OpenGl_ArbFBO*      )(&(*myFuncs));
+  }
+  if (IsGlGreaterEqual (3, 0)
+   && FindProc ("glBlitFramebuffer", myFuncs->glBlitFramebuffer))
+  {
+    arbFBOBlit = (OpenGl_ArbFBOBlit* )(&(*myFuncs));
   }
 
-  hasHighp = CheckExtension ("OES_fragment_precision_high");
+  hasUintIndex = IsGlGreaterEqual (3, 0)
+              || CheckExtension ("GL_OES_element_index_uint");
+  hasHighp     = CheckExtension ("GL_OES_fragment_precision_high");
   GLint aRange[2] = {0, 0};
-  GLint aPrec [2] = {0, 0};
-  ::glGetShaderPrecisionFormat (GL_FRAGMENT_SHADER, GL_HIGH_FLOAT, aRange, aPrec);
-  if (aPrec[1] != 0)
+  GLint aPrec     = 0;
+  ::glGetShaderPrecisionFormat (GL_FRAGMENT_SHADER, GL_HIGH_FLOAT, aRange, &aPrec);
+  if (aPrec != 0)
   {
     hasHighp = Standard_True;
   }
@@ -935,8 +1172,10 @@ void OpenGl_Context::init()
   //! Make record shorter to retrieve function pointer using variable with same name
   #define FindProcShort(theFunc) FindProc(#theFunc, myFuncs->theFunc)
 
-    // retrieve platform-dependent extensions
-#if defined(_WIN32) && !defined(HAVE_EGL)
+  // retrieve platform-dependent extensions
+#if defined(HAVE_EGL)
+  //
+#elif defined(_WIN32)
   if (FindProcShort (wglGetExtensionsStringARB))
   {
     const char* aWglExts = myFuncs->wglGetExtensionsStringARB (wglGetCurrentDC());
@@ -964,6 +1203,19 @@ void OpenGl_Context::init()
       FindProcShort (wglDXUnlockObjectsNV);
     }
   }
+#elif defined(__APPLE__)
+    //
+#else
+    const char* aGlxExts = ::glXQueryExtensionsString ((Display* )myDisplay, DefaultScreen ((Display* )myDisplay));
+    if (CheckExtension (aGlxExts, "GLX_EXT_swap_control"))
+    {
+      FindProcShort (glXSwapIntervalEXT);
+    }
+    if (CheckExtension (aGlxExts, "GLX_SGI_swap_control"))
+    {
+      FindProcShort (glXSwapIntervalSGI);
+    }
+    //extSwapTear = CheckExtension (aGlxExts, "GLX_EXT_swap_control_tear");
 #endif
 
   // initialize debug context extension
@@ -981,10 +1233,12 @@ void OpenGl_Context::init()
      && caps->contextDebug)
     {
       // setup default callback
+      myIsGlDebugCtx = Standard_True;
       arbDbg->glDebugMessageCallbackARB (debugCallbackWrap, this);
-    #ifdef DEB
-      glEnable (GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
-    #endif
+      if (caps->contextSyncDebug)
+      {
+        ::glEnable (GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
+      }
     }
   }
 
@@ -1007,7 +1261,11 @@ void OpenGl_Context::init()
        && FindProcShort (glCompressedTexSubImage3D)
        && FindProcShort (glCompressedTexSubImage2D)
        && FindProcShort (glCompressedTexSubImage1D)
-       && FindProcShort (glGetCompressedTexImage)
+       && FindProcShort (glGetCompressedTexImage);
+
+  if (!isCoreProfile)
+  {
+    has13 = has13
        && FindProcShort (glClientActiveTexture)
        && FindProcShort (glMultiTexCoord1d)
        && FindProcShort (glMultiTexCoord1dv)
@@ -1045,6 +1303,7 @@ void OpenGl_Context::init()
        && FindProcShort (glLoadTransposeMatrixd)
        && FindProcShort (glMultTransposeMatrixf)
        && FindProcShort (glMultTransposeMatrixd);
+  }
 
   // load OpenGL 1.4 new functions
   has14 = IsGlGreaterEqual (1, 4)
@@ -1381,7 +1640,21 @@ void OpenGl_Context::init()
 
   // load GL_ARB_vertex_type_2_10_10_10_rev (added to OpenGL 3.3 core)
   const bool hasVertType21010101rev = (IsGlGreaterEqual (3, 3) || CheckExtension ("GL_ARB_vertex_type_2_10_10_10_rev"))
-       && FindProcShort (glVertexP2ui)
+       && FindProcShort (glVertexAttribP1ui)
+       && FindProcShort (glVertexAttribP1uiv)
+       && FindProcShort (glVertexAttribP2ui)
+       && FindProcShort (glVertexAttribP2uiv)
+       && FindProcShort (glVertexAttribP3ui)
+       && FindProcShort (glVertexAttribP3uiv)
+       && FindProcShort (glVertexAttribP4ui)
+       && FindProcShort (glVertexAttribP4uiv);
+
+  if ( hasVertType21010101rev
+   && !isCoreProfile)
+  {
+    // load deprecated functions
+    const bool hasVertType21010101revExt =
+          FindProcShort (glVertexP2ui)
        && FindProcShort (glVertexP2uiv)
        && FindProcShort (glVertexP3ui)
        && FindProcShort (glVertexP3uiv)
@@ -1410,15 +1683,9 @@ void OpenGl_Context::init()
        && FindProcShort (glColorP4ui)
        && FindProcShort (glColorP4uiv)
        && FindProcShort (glSecondaryColorP3ui)
-       && FindProcShort (glSecondaryColorP3uiv)
-       && FindProcShort (glVertexAttribP1ui)
-       && FindProcShort (glVertexAttribP1uiv)
-       && FindProcShort (glVertexAttribP2ui)
-       && FindProcShort (glVertexAttribP2uiv)
-       && FindProcShort (glVertexAttribP3ui)
-       && FindProcShort (glVertexAttribP3uiv)
-       && FindProcShort (glVertexAttribP4ui)
-       && FindProcShort (glVertexAttribP4uiv);
+       && FindProcShort (glSecondaryColorP3uiv);
+    (void )hasVertType21010101revExt;
+  }
 
   // load OpenGL 3.3 extra functions
   has33 = IsGlGreaterEqual (3, 3)
@@ -1640,10 +1907,7 @@ void OpenGl_Context::init()
   const bool hasTextureStorage = (IsGlGreaterEqual (4, 2) || CheckExtension ("GL_ARB_texture_storage"))
        && FindProcShort (glTexStorage1D)
        && FindProcShort (glTexStorage2D)
-       && FindProcShort (glTexStorage3D)
-       && FindProcShort (glTextureStorage1DEXT)
-       && FindProcShort (glTextureStorage2DEXT)
-       && FindProcShort (glTextureStorage3DEXT);
+       && FindProcShort (glTexStorage3D);
 
   has42 = IsGlGreaterEqual (4, 2)
        && hasBaseInstance
@@ -1720,6 +1984,7 @@ void OpenGl_Context::init()
   {
     arbTBO = (OpenGl_ArbTBO* )(&(*myFuncs));
   }
+  arbTboRGB32 = CheckExtension ("GL_ARB_texture_buffer_object_rgb32");
 
   // initialize hardware instancing extension (ARB)
   if (!has31
@@ -1733,7 +1998,8 @@ void OpenGl_Context::init()
   // initialize FBO extension (ARB)
   if (hasFBO)
   {
-    arbFBO = (OpenGl_ArbFBO* )(&(*myFuncs));
+    arbFBO     = (OpenGl_ArbFBO*     )(&(*myFuncs));
+    arbFBOBlit = (OpenGl_ArbFBOBlit* )(&(*myFuncs));
     extPDS = Standard_True; // extension for EXT, but part of ARB
   }
 
@@ -1744,45 +2010,98 @@ void OpenGl_Context::init()
     extGS = (OpenGl_ExtGS* )(&(*myFuncs));
   }
 
+  // initialize bindless texture extension (ARB)
+  if (CheckExtension ("GL_ARB_bindless_texture")
+   && FindProcShort (glGetTextureHandleARB)
+   && FindProcShort (glGetTextureSamplerHandleARB)
+   && FindProcShort (glMakeTextureHandleResidentARB)
+   && FindProcShort (glMakeTextureHandleNonResidentARB)
+   && FindProcShort (glGetImageHandleARB)
+   && FindProcShort (glMakeImageHandleResidentARB)
+   && FindProcShort (glMakeImageHandleNonResidentARB)
+   && FindProcShort (glUniformHandleui64ARB)
+   && FindProcShort (glUniformHandleui64vARB)
+   && FindProcShort (glProgramUniformHandleui64ARB)
+   && FindProcShort (glProgramUniformHandleui64vARB)
+   && FindProcShort (glIsTextureHandleResidentARB)
+   && FindProcShort (glIsImageHandleResidentARB)
+   && FindProcShort (glVertexAttribL1ui64ARB)
+   && FindProcShort (glVertexAttribL1ui64vARB)
+   && FindProcShort (glGetVertexAttribLui64vARB))
+  {
+    arbTexBindless = (OpenGl_ArbTexBindless* )(&(*myFuncs));
+  }
+
   if (!has12)
   {
+    checkWrongVersion (1, 2);
     myGlVerMajor = 1;
     myGlVerMinor = 1;
     return;
   }
   else if (!has13)
   {
+    checkWrongVersion (1, 3);
     myGlVerMajor = 1;
     myGlVerMinor = 2;
     return;
   }
   else if (!has14)
   {
+    checkWrongVersion (1, 4);
     myGlVerMajor = 1;
     myGlVerMinor = 3;
     return;
   }
   else if (!has15)
   {
+    checkWrongVersion (1, 5);
     myGlVerMajor = 1;
     myGlVerMinor = 4;
     return;
   }
-  core15    = (OpenGl_GlCore15*    )(&(*myFuncs));
+  if (!isCoreProfile)
+  {
+    core15 = (OpenGl_GlCore15* )(&(*myFuncs));
+  }
   core15fwd = (OpenGl_GlCore15Fwd* )(&(*myFuncs));
 
   if (!has20)
   {
+    checkWrongVersion (2, 0);
+    myGlVerMajor = 1;
+    myGlVerMinor = 5;
+    return;
+  }
+
+  const char* aGlslVer = (const char* )::glGetString (GL_SHADING_LANGUAGE_VERSION);
+  if (aGlslVer == NULL
+  || *aGlslVer == '\0')
+  {
+    // broken context has been detected
+    TCollection_ExtendedString aMsg = TCollection_ExtendedString()
+      + "Error! OpenGL context reports version "
+      + myGlVerMajor  + "." + myGlVerMinor
+      + " but reports wrong GLSL version";
+    PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
+                 GL_DEBUG_TYPE_ERROR_ARB,
+                 0,
+                 GL_DEBUG_SEVERITY_HIGH_ARB,
+                 aMsg);
     myGlVerMajor = 1;
     myGlVerMinor = 5;
     return;
   }
 
-  core20    = (OpenGl_GlCore20*    )(&(*myFuncs));
+  if (!isCoreProfile)
+  {
+    core20  = (OpenGl_GlCore20*    )(&(*myFuncs));
+  }
   core20fwd = (OpenGl_GlCore20Fwd* )(&(*myFuncs));
 
   if (!has21)
   {
+    checkWrongVersion (2, 1);
     myGlVerMajor = 2;
     myGlVerMinor = 0;
     return;
@@ -1790,6 +2109,7 @@ void OpenGl_Context::init()
 
   if (!has30)
   {
+    checkWrongVersion (3, 0);
     myGlVerMajor = 2;
     myGlVerMinor = 1;
     return;
@@ -1797,6 +2117,7 @@ void OpenGl_Context::init()
 
   if (!has31)
   {
+    checkWrongVersion (3, 1);
     myGlVerMajor = 3;
     myGlVerMinor = 0;
     return;
@@ -1806,62 +2127,98 @@ void OpenGl_Context::init()
 
   if (!has32)
   {
+    checkWrongVersion (3, 2);
     myGlVerMajor = 3;
     myGlVerMinor = 1;
     return;
   }
-  core32     = (OpenGl_GlCore32*     )(&(*myFuncs));
-  core32back = (OpenGl_GlCore32Back* )(&(*myFuncs));
+  core32 = (OpenGl_GlCore32* )(&(*myFuncs));
+  if (isCoreProfile)
+  {
+    core32->glGenVertexArrays (1, &myDefaultVao);
+  }
+  else
+  {
+    core32back = (OpenGl_GlCore32Back* )(&(*myFuncs));
+  }
 
   if (!has33)
   {
+    checkWrongVersion (3, 3);
     myGlVerMajor = 3;
     myGlVerMinor = 2;
     return;
   }
+  core33 = (OpenGl_GlCore33* )(&(*myFuncs));
+  if (!isCoreProfile)
+  {
+    core33back = (OpenGl_GlCore33Back* )(&(*myFuncs));
+  }
+
+  // initialize sampler object
+  myTexSampler = new OpenGl_Sampler();
+  myTexSampler->Init (*this);
 
   if (!has40)
   {
+    checkWrongVersion (4, 0);
     myGlVerMajor = 3;
     myGlVerMinor = 3;
     return;
   }
+  arbTboRGB32 = Standard_True; // in core since OpenGL 4.0
 
   if (!has41)
   {
+    checkWrongVersion (4, 1);
     myGlVerMajor = 4;
     myGlVerMinor = 0;
     return;
   }
-  core41     = (OpenGl_GlCore41*     )(&(*myFuncs));
-  core41back = (OpenGl_GlCore41Back* )(&(*myFuncs));
+  core41 = (OpenGl_GlCore41* )(&(*myFuncs));
+  if (!isCoreProfile)
+  {
+    core41back = (OpenGl_GlCore41Back* )(&(*myFuncs));
+  }
 
   if(!has42)
   {
+    checkWrongVersion (4, 2);
     myGlVerMajor = 4;
     myGlVerMinor = 1;
     return;
   }
-  core42     = (OpenGl_GlCore42*     )(&(*myFuncs));
-  core42back = (OpenGl_GlCore42Back* )(&(*myFuncs));
+  core42 = (OpenGl_GlCore42* )(&(*myFuncs));
+  if (!isCoreProfile)
+  {
+    core42back = (OpenGl_GlCore42Back* )(&(*myFuncs));
+  }
 
-  if(!has43)
+  if (!has43)
   {
+    checkWrongVersion (4, 3);
     myGlVerMajor = 4;
     myGlVerMinor = 2;
     return;
   }
-  core43     = (OpenGl_GlCore43*     )(&(*myFuncs));
-  core43back = (OpenGl_GlCore43Back* )(&(*myFuncs));
+  core43 = (OpenGl_GlCore43* )(&(*myFuncs));
+  if (!isCoreProfile)
+  {
+    core43back = (OpenGl_GlCore43Back* )(&(*myFuncs));
+  }
 
   if (!has44)
   {
+    checkWrongVersion (4, 4);
     myGlVerMajor = 4;
     myGlVerMinor = 3;
     return;
   }
-  core44     = (OpenGl_GlCore44*     )(&(*myFuncs));
-  core44back = (OpenGl_GlCore44Back* )(&(*myFuncs));
+  core44 = (OpenGl_GlCore44* )(&(*myFuncs));
+  if (!isCoreProfile)
+  {
+    core44back = (OpenGl_GlCore44Back* )(&(*myFuncs));
+  }
 #endif
 }
 
@@ -2061,8 +2418,13 @@ void OpenGl_Context::ReleaseDelayed()
 // function : BindProgram
 // purpose  :
 // =======================================================================
-void OpenGl_Context::BindProgram (const Handle(OpenGl_ShaderProgram)& theProgram)
+Standard_Boolean OpenGl_Context::BindProgram (const Handle(OpenGl_ShaderProgram)& theProgram)
 {
+  if (core20fwd == NULL)
+  {
+    return Standard_False;
+  }
+
   if (theProgram.IsNull()
   || !theProgram->IsValid())
   {
@@ -2071,9 +2433,424 @@ void OpenGl_Context::BindProgram (const Handle(OpenGl_ShaderProgram)& theProgram
       core20fwd->glUseProgram (OpenGl_ShaderProgram::NO_PROGRAM);
       myActiveProgram.Nullify();
     }
-    return;
+    return Standard_False;
   }
 
   myActiveProgram = theProgram;
   core20fwd->glUseProgram (theProgram->ProgramId());
+  return Standard_True;
+}
+
+// =======================================================================
+// function : BindDefaultVao
+// purpose  :
+// =======================================================================
+void OpenGl_Context::BindDefaultVao()
+{
+#if !defined(GL_ES_VERSION_2_0)
+  if (myDefaultVao == 0
+   || core32 == NULL)
+  {
+    return;
+  }
+
+  core32->glBindVertexArray (myDefaultVao);
+#endif
+}
+
+// =======================================================================
+// function : SetDefaultFrameBuffer
+// purpose  :
+// =======================================================================
+Handle(OpenGl_FrameBuffer) OpenGl_Context::SetDefaultFrameBuffer (const Handle(OpenGl_FrameBuffer)& theFbo)
+{
+  Handle(OpenGl_FrameBuffer) aFbo = myDefaultFbo;
+  myDefaultFbo = theFbo;
+  return aFbo;
+}
+
+// =======================================================================
+// function : SetColor4fv
+// purpose  :
+// =======================================================================
+void OpenGl_Context::SetColor4fv (const OpenGl_Vec4& theColor)
+{
+  if (!myActiveProgram.IsNull())
+  {
+    myActiveProgram->SetUniform (this, myActiveProgram->GetStateLocation (OpenGl_OCCT_COLOR), theColor);
+  }
+#if !defined(GL_ES_VERSION_2_0)
+  else if (core11 != NULL)
+  {
+    core11->glColor4fv (theColor.GetData());
+  }
+#endif
+}
+
+// =======================================================================
+// function : SetTypeOfLine
+// purpose  :
+// =======================================================================
+void OpenGl_Context::SetTypeOfLine (const Aspect_TypeOfLine  theType,
+                                    const Standard_ShortReal theFactor)
+{
+  Standard_Integer aPattern = 0xFFFF;
+  switch (theType)
+  {
+    case Aspect_TOL_DASH:
+    {
+      aPattern = 0xFFC0;
+      break;
+    }
+    case Aspect_TOL_DOT:
+    {
+      aPattern = 0xCCCC;
+      break;
+    }
+    case Aspect_TOL_DOTDASH:
+    {
+      aPattern = 0xFF18;
+      break;
+    }
+    case Aspect_TOL_SOLID:
+    {
+      aPattern = 0xFFFF;
+      break;
+    }
+    case Aspect_TOL_USERDEFINED:
+    {
+      aPattern = 0xFF24;
+      break;
+    }
+  }
+
+  if (!myActiveProgram.IsNull())
+  {
+    myActiveProgram->SetUniform (this, "uPattern", aPattern);
+    myActiveProgram->SetUniform (this, "uFactor",  theFactor);
+    return;
+  }
+
+#if !defined(GL_ES_VERSION_2_0)
+  if (theType != Aspect_TOL_SOLID)
+  {
+  #ifdef HAVE_GL2PS
+    if (IsFeedback())
+    {
+      gl2psEnable (GL2PS_LINE_STIPPLE);
+    }
+  #endif
+
+    if (core11 != NULL)
+    {
+      core11fwd->glEnable (GL_LINE_STIPPLE);
+
+      core11->glLineStipple (static_cast<GLint>    (theFactor),
+                             static_cast<GLushort> (aPattern));
+    }
+  }
+  else
+  {
+    if (core11 != NULL)
+    {
+      core11fwd->glDisable (GL_LINE_STIPPLE);
+    }
+
+  #ifdef HAVE_GL2PS
+    if (IsFeedback())
+    {
+      gl2psDisable (GL2PS_LINE_STIPPLE);
+    }
+  #endif
+  }
+#endif
+}
+
+// =======================================================================
+// function : SetLineWidth
+// purpose  :
+// =======================================================================
+void OpenGl_Context::SetLineWidth (const Standard_ShortReal theWidth)
+{
+  if (core11 != NULL)
+  {
+    // glLineWidth() is still defined within Core Profile, but has no effect with values != 1.0f
+    core11fwd->glLineWidth (theWidth);
+  }
+#ifdef HAVE_GL2PS
+  if (IsFeedback())
+  {
+    gl2psLineWidth (theWidth);
+  }
+#endif
+}
+
+// =======================================================================
+// function : SetTextureMatrix
+// purpose  :
+// =======================================================================
+void OpenGl_Context::SetTextureMatrix (const Handle(Graphic3d_TextureParams)& theParams)
+{
+  if (theParams.IsNull())
+  {
+    return;
+  }
+  else if (!myActiveProgram.IsNull())
+  {
+    const GLint aUniLoc = myActiveProgram->GetStateLocation (OpenGl_OCCT_TEXTURE_TRSF2D);
+    if (aUniLoc == OpenGl_ShaderProgram::INVALID_LOCATION)
+    {
+      return;
+    }
+
+    // pack transformation parameters
+    OpenGl_Vec4 aTrsf[2];
+    aTrsf[0].xy() = theParams->Translation();
+    aTrsf[0].zw() = theParams->Scale();
+    aTrsf[1].x()  = std::sin (-theParams->Rotation() * static_cast<float> (M_PI / 180.0));
+    aTrsf[1].y()  = std::cos (-theParams->Rotation() * static_cast<float> (M_PI / 180.0));
+    myActiveProgram->SetUniform (this, aUniLoc, 2, aTrsf);
+    return;
+  }
+
+#if !defined(GL_ES_VERSION_2_0)
+  if (core11 != NULL)
+  {
+    GLint aMatrixMode = GL_TEXTURE;
+    ::glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode);
+
+    core11->glMatrixMode (GL_TEXTURE);
+    OpenGl_Mat4 aTextureMat;
+    const Graphic3d_Vec2& aScale = theParams->Scale();
+    const Graphic3d_Vec2& aTrans = theParams->Translation();
+    Graphic3d_TransformUtils::Scale     (aTextureMat,  aScale.x(),  aScale.y(), 1.0f);
+    Graphic3d_TransformUtils::Translate (aTextureMat, -aTrans.x(), -aTrans.y(), 0.0f);
+    Graphic3d_TransformUtils::Rotate    (aTextureMat, -theParams->Rotation(), 0.0f, 0.0f, 1.0f);
+    core11->glLoadMatrixf (aTextureMat);
+    core11->glMatrixMode (aMatrixMode);
+  }
+#endif
+}
+
+// =======================================================================
+// function : SetPointSize
+// purpose  :
+// =======================================================================
+void OpenGl_Context::SetPointSize (const Standard_ShortReal theSize)
+{
+  if (!myActiveProgram.IsNull())
+  {
+    myActiveProgram->SetUniform (this, myActiveProgram->GetStateLocation (OpenGl_OCCT_POINT_SIZE), theSize);
+  #if !defined(GL_ES_VERSION_2_0)
+    //myContext->core11fwd->glEnable (GL_VERTEX_PROGRAM_POINT_SIZE);
+  #endif
+  }
+#if !defined(GL_ES_VERSION_2_0)
+  //else
+  {
+    core11fwd->glPointSize (theSize);
+    if (core20fwd != NULL)
+    {
+      //myContext->core11fwd->glDisable (GL_VERTEX_PROGRAM_POINT_SIZE);
+    }
+  }
+#endif
+}
+
+// =======================================================================
+// function : SetGlNormalizeEnabled
+// purpose  :
+// =======================================================================
+Standard_Boolean OpenGl_Context::SetGlNormalizeEnabled (Standard_Boolean isEnabled)
+{
+  if (isEnabled == myIsGlNormalizeEnabled)
+  {
+    return myIsGlNormalizeEnabled;
+  }
+
+  Standard_Boolean anOldGlNormalize = myIsGlNormalizeEnabled;
+
+  myIsGlNormalizeEnabled = isEnabled;
+
+#if !defined(GL_ES_VERSION_2_0)
+  if (core11 != NULL)
+  {
+    if (isEnabled)
+    {
+      ::glEnable  (GL_NORMALIZE);
+    }
+    else
+    {
+      ::glDisable (GL_NORMALIZE);
+    }
+  }
+#endif
+
+  return anOldGlNormalize;
+}
+
+// =======================================================================
+// function : ApplyModelWorldMatrix
+// purpose  :
+// =======================================================================
+void OpenGl_Context::ApplyModelWorldMatrix()
+{
+#if !defined(GL_ES_VERSION_2_0)
+  if (core11 != NULL)
+  {
+    core11->glMatrixMode (GL_MODELVIEW);
+    core11->glLoadMatrixf (ModelWorldState.Current());
+  }
+#endif
+
+  if (!myShaderManager->IsEmpty())
+  {
+    myShaderManager->UpdateModelWorldStateTo (ModelWorldState.Current());
+  }
+}
+
+// =======================================================================
+// function : ApplyWorldViewMatrix
+// purpose  :
+// =======================================================================
+void OpenGl_Context::ApplyWorldViewMatrix()
+{
+#if !defined(GL_ES_VERSION_2_0)
+  if (core11 != NULL)
+  {
+    core11->glMatrixMode (GL_MODELVIEW);
+    core11->glLoadMatrixf (WorldViewState.Current());
+  }
+#endif
+
+  if (!myShaderManager->IsEmpty())
+  {
+    myShaderManager->UpdateWorldViewStateTo (WorldViewState.Current());
+  }
+}
+
+// =======================================================================
+// function : ApplyModelViewMatrix
+// purpose  :
+// =======================================================================
+void OpenGl_Context::ApplyModelViewMatrix()
+{
+#if !defined(GL_ES_VERSION_2_0)
+  if (core11 != NULL)
+  {
+    OpenGl_Mat4 aModelView = WorldViewState.Current() * ModelWorldState.Current();
+    core11->glMatrixMode (GL_MODELVIEW);
+    core11->glLoadMatrixf (aModelView.GetData());
+  }
+#endif
+
+  if (!myShaderManager->IsEmpty())
+  {
+    myShaderManager->UpdateModelWorldStateTo (ModelWorldState.Current());
+    myShaderManager->UpdateWorldViewStateTo (WorldViewState.Current());
+  }
+}
+
+// =======================================================================
+// function : ApplyProjectionMatrix
+// purpose  :
+// =======================================================================
+void OpenGl_Context::ApplyProjectionMatrix()
+{
+#if !defined(GL_ES_VERSION_2_0)
+  if (core11 != NULL)
+  {
+    core11->glMatrixMode (GL_PROJECTION);
+    core11->glLoadMatrixf (ProjectionState.Current().GetData());
+  }
+#endif
+
+  if (!myShaderManager->IsEmpty())
+  {
+    myShaderManager->UpdateProjectionStateTo (ProjectionState.Current());
+  }
+}
+
+
+// =======================================================================
+// function : EnableFeatures
+// purpose  :
+// =======================================================================
+void OpenGl_Context::EnableFeatures() const
+{
+  //
+}
+
+// =======================================================================
+// function : DisableFeatures
+// purpose  :
+// =======================================================================
+void OpenGl_Context::DisableFeatures() const
+{
+#if !defined(GL_ES_VERSION_2_0)
+  glPixelTransferi (GL_MAP_COLOR, GL_FALSE);
+#endif
+
+  /*
+  * Disable stuff that's likely to slow down glDrawPixels.
+  * (Omit as much of this as possible, when you know in advance
+  * that the OpenGL state will already be set correctly.)
+  */
+  glDisable(GL_DITHER);
+  glDisable(GL_BLEND);
+  glDisable(GL_DEPTH_TEST);
+  glDisable(GL_TEXTURE_2D);
+  glDisable(GL_STENCIL_TEST);
+
+#if !defined(GL_ES_VERSION_2_0)
+  glDisable(GL_LIGHTING);
+  glDisable(GL_ALPHA_TEST);
+  glDisable(GL_FOG);
+  glDisable(GL_LOGIC_OP);
+  glDisable(GL_TEXTURE_1D);
+
+  glPixelTransferi(GL_MAP_COLOR, GL_FALSE);
+  glPixelTransferi(GL_RED_SCALE, 1);
+  glPixelTransferi(GL_RED_BIAS, 0);
+  glPixelTransferi(GL_GREEN_SCALE, 1);
+  glPixelTransferi(GL_GREEN_BIAS, 0);
+  glPixelTransferi(GL_BLUE_SCALE, 1);
+  glPixelTransferi(GL_BLUE_BIAS, 0);
+  glPixelTransferi(GL_ALPHA_SCALE, 1);
+  glPixelTransferi(GL_ALPHA_BIAS, 0);
+
+  /*
+  * Disable extensions that could slow down glDrawPixels.
+  * (Actually, you should check for the presence of the proper
+  * extension before making these calls.  I've omitted that
+  * code for simplicity.)
+  */
+
+  if ((myGlVerMajor >= 1) && (myGlVerMinor >= 2))
+  {
+#ifdef GL_EXT_convolution
+    if (CheckExtension ("GL_CONVOLUTION_1D_EXT"))
+      glDisable(GL_CONVOLUTION_1D_EXT);
+
+    if (CheckExtension ("GL_CONVOLUTION_2D_EXT"))
+      glDisable(GL_CONVOLUTION_2D_EXT);
+
+    if (CheckExtension ("GL_SEPARABLE_2D_EXT"))
+      glDisable(GL_SEPARABLE_2D_EXT);
+#endif
+
+#ifdef GL_EXT_histogram
+    if (CheckExtension ("GL_SEPARABLE_2D_EXT"))
+      glDisable(GL_HISTOGRAM_EXT);
+
+    if (CheckExtension ("GL_MINMAX_EXT"))
+      glDisable(GL_MINMAX_EXT);
+#endif
+
+#ifdef GL_EXT_texture3D
+    if (CheckExtension ("GL_TEXTURE_3D_EXT"))
+      glDisable(GL_TEXTURE_3D_EXT);
+#endif
+  }
+#endif
 }