1 // Created on: 2012-01-26
2 // Created by: Kirill GAVRILOV
3 // Copyright (c) 2012-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
20 #include <OpenGl_Context.hxx>
22 #include <OpenGl_ArbTBO.hxx>
23 #include <OpenGl_ArbIns.hxx>
24 #include <OpenGl_ArbDbg.hxx>
25 #include <OpenGl_ArbFBO.hxx>
26 #include <OpenGl_ExtGS.hxx>
27 #include <OpenGl_ArbTexBindless.hxx>
28 #include <OpenGl_GlCore44.hxx>
29 #include <OpenGl_FrameBuffer.hxx>
30 #include <OpenGl_Sampler.hxx>
31 #include <OpenGl_ShaderManager.hxx>
32 #include <OpenGl_Workspace.hxx>
33 #include <OpenGl_AspectFace.hxx>
34 #include <Graphic3d_TransformUtils.hxx>
35 #include <Graphic3d_RenderingParams.hxx>
37 #include <Message_Messenger.hxx>
39 #include <NCollection_Vector.hxx>
41 #include <Standard_ProgramError.hxx>
43 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Context,Standard_Transient)
48 #pragma comment(lib, "libEGL.lib")
52 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
54 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
57 #include <OpenGL/OpenGL.h>
58 #include <CoreGraphics/CoreGraphics.h>
61 #include <GL/glx.h> // glXGetProcAddress()
67 #pragma comment (lib, "gl2ps.lib")
74 static const Handle(OpenGl_Resource) NULL_GL_RESOURCE;
75 static const OpenGl_Mat4 THE_IDENTITY_MATRIX;
77 //! Add key-value pair to the dictionary.
78 static void addInfo (TColStd_IndexedDataMapOfStringString& theDict,
79 const TCollection_AsciiString& theKey,
80 const TCollection_AsciiString& theValue)
82 theDict.ChangeFromIndex (theDict.Add (theKey, theValue)) = theValue;
85 //! Add key-value pair to the dictionary.
86 static void addInfo (TColStd_IndexedDataMapOfStringString& theDict,
87 const TCollection_AsciiString& theKey,
90 TCollection_AsciiString aValue (theValue != NULL ? theValue : "");
91 theDict.ChangeFromIndex (theDict.Add (theKey, aValue)) = aValue;
95 // =======================================================================
96 // function : OpenGl_Context
98 // =======================================================================
99 OpenGl_Context::OpenGl_Context (const Handle(OpenGl_Caps)& theCaps)
118 caps (!theCaps.IsNull() ? theCaps : new OpenGl_Caps()),
119 #if defined(GL_ES_VERSION_2_0)
120 hasHighp (Standard_False),
121 hasUintIndex(Standard_False),
122 hasTexRGBA8(Standard_False),
124 hasHighp (Standard_True),
125 hasUintIndex(Standard_True),
126 hasTexRGBA8(Standard_True),
128 hasDrawBuffers (OpenGl_FeatureNotAvailable),
129 hasFloatBuffer (OpenGl_FeatureNotAvailable),
130 hasHalfFloatBuffer (OpenGl_FeatureNotAvailable),
131 hasSampleVariables (OpenGl_FeatureNotAvailable),
132 arbDrawBuffers (Standard_False),
133 arbNPTW (Standard_False),
134 arbTexRG (Standard_False),
135 arbTexFloat (Standard_False),
136 arbTexBindless (NULL),
138 arbTboRGB32 (Standard_False),
143 arbSampleShading (Standard_False),
144 extFragDepth (Standard_False),
145 extDrawBuffers (Standard_False),
147 extBgra(Standard_False),
148 extAnis(Standard_False),
149 extPDS (Standard_False),
150 atiMem (Standard_False),
151 nvxMem (Standard_False),
152 oesSampleVariables (Standard_False),
153 mySharedResources (new OpenGl_ResourcesMap()),
154 myDelayed (new OpenGl_DelayReleaseMap()),
155 myUnusedResources (new OpenGl_ResourcesStack()),
157 myGlLibHandle (NULL),
158 myFuncs (new OpenGl_GlFunctions()),
160 myTexClamp (GL_CLAMP_TO_EDGE),
164 myMaxDrawBuffers (1),
165 myMaxColorAttachments (1),
168 myIsInitialized (Standard_False),
169 myIsStereoBuffers (Standard_False),
170 myIsGlNormalizeEnabled (Standard_False),
171 myHasRayTracing (Standard_False),
172 myHasRayTracingTextures (Standard_False),
173 myHasRayTracingAdaptiveSampling (Standard_False),
174 #if !defined(GL_ES_VERSION_2_0)
175 myPointSpriteOrig (GL_UPPER_LEFT),
176 myRenderMode (GL_RENDER),
177 myPolygonMode (GL_FILL),
179 myPointSpriteOrig (0),
183 myToCullBackFaces (false),
187 myIsGlDebugCtx (Standard_False),
188 myResolution (Graphic3d_RenderingParams::THE_DEFAULT_RESOLUTION),
189 myResolutionRatio (1.0f),
190 myLineWidthScale (1.0f),
191 myRenderScale (1.0f),
192 myRenderScaleInv (1.0f)
198 myViewportVirt[0] = 0;
199 myViewportVirt[1] = 0;
200 myViewportVirt[2] = 0;
201 myViewportVirt[3] = 0;
203 // system-dependent fields
204 #if defined(HAVE_EGL)
205 myDisplay = (Aspect_Display )EGL_NO_DISPLAY;
206 myWindow = (Aspect_Drawable )EGL_NO_SURFACE;
207 myGContext = (Aspect_RenderingContext )EGL_NO_CONTEXT;
208 #elif defined(_WIN32)
212 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
213 // Vendors can not extend functionality on this system
214 // and developers are limited to OpenGL support provided by Mac OS X SDK.
215 // We retrieve function pointers from system library
216 // to generalize extensions support on all platforms.
217 // In this way we also reach binary compatibility benefit between OS releases
218 // if some newest functionality is optionally used.
219 // Notice that GL version / extension availability checks are required
220 // because function pointers may be available but not functionality itself
221 // (depends on renderer).
222 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
224 myGlLibHandle = dlopen ("/System/Library/Frameworks/OpenGLES.framework/OpenGLES", RTLD_LAZY);
227 myGlLibHandle = dlopen ("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL", RTLD_LAZY);
235 memset (myFuncs.operator->(), 0, sizeof(OpenGl_GlFunctions));
236 myShaderManager = new OpenGl_ShaderManager (this);
239 // =======================================================================
240 // function : ~OpenGl_Context
242 // =======================================================================
243 OpenGl_Context::~OpenGl_Context()
245 // release clean up queue
248 #if !defined(GL_ES_VERSION_2_0)
249 // release default VAO
250 if (myDefaultVao != 0
254 core32->glDeleteVertexArrays (1, &myDefaultVao);
259 // release default FBO
260 if (!myDefaultFbo.IsNull())
262 myDefaultFbo->Release (this);
263 myDefaultFbo.Nullify();
266 // release shared resources if any
267 if (mySharedResources->GetRefCount() <= 1)
269 myShaderManager.Nullify();
270 for (NCollection_DataMap<TCollection_AsciiString, Handle(OpenGl_Resource)>::Iterator anIter (*mySharedResources);
271 anIter.More(); anIter.Next())
273 anIter.Value()->Release (this);
276 // release delayed resources added during deletion of shared resources
277 while (!myUnusedResources->IsEmpty())
279 myUnusedResources->First()->Release (this);
280 myUnusedResources->RemoveFirst();
283 else if (myShaderManager->IsSameContext (this))
285 myShaderManager->SetContext (NULL);
287 mySharedResources.Nullify();
290 // release sampler object
291 if (!myTexSampler.IsNull())
293 myTexSampler->Release (this);
301 #if !defined(GL_ES_VERSION_2_0)
303 glGetPointerv (GL_DEBUG_CALLBACK_USER_PARAM, &aPtr);
307 arbDbg->glDebugMessageCallback (NULL, NULL);
309 myIsGlDebugCtx = Standard_False;
313 // =======================================================================
314 // function : forcedRelease
316 // =======================================================================
317 void OpenGl_Context::forcedRelease()
320 for (NCollection_DataMap<TCollection_AsciiString, Handle(OpenGl_Resource)>::Iterator anIter (*mySharedResources);
321 anIter.More(); anIter.Next())
323 anIter.Value()->Release (this);
325 mySharedResources->Clear();
326 myShaderManager->clear();
327 myShaderManager->SetContext (NULL);
329 // release delayed resources added during deletion of shared resources
330 while (!myUnusedResources->IsEmpty())
332 myUnusedResources->First()->Release (this);
333 myUnusedResources->RemoveFirst();
337 // =======================================================================
338 // function : ResizeViewport
340 // =======================================================================
341 void OpenGl_Context::ResizeViewport (const Standard_Integer* theRect)
343 core11fwd->glViewport (theRect[0], theRect[1], theRect[2], theRect[3]);
344 myViewport[0] = theRect[0];
345 myViewport[1] = theRect[1];
346 myViewport[2] = theRect[2];
347 myViewport[3] = theRect[3];
348 if (HasRenderScale())
350 myViewportVirt[0] = Standard_Integer(theRect[0] * myRenderScaleInv);
351 myViewportVirt[1] = Standard_Integer(theRect[1] * myRenderScaleInv);
352 myViewportVirt[2] = Standard_Integer(theRect[2] * myRenderScaleInv);
353 myViewportVirt[3] = Standard_Integer(theRect[3] * myRenderScaleInv);
357 myViewportVirt[0] = theRect[0];
358 myViewportVirt[1] = theRect[1];
359 myViewportVirt[2] = theRect[2];
360 myViewportVirt[3] = theRect[3];
364 #if !defined(GL_ES_VERSION_2_0)
365 inline Standard_Integer stereoToMonoBuffer (const Standard_Integer theBuffer)
381 // =======================================================================
382 // function : SetReadBuffer
384 // =======================================================================
385 void OpenGl_Context::SetReadBuffer (const Standard_Integer theReadBuffer)
387 #if !defined(GL_ES_VERSION_2_0)
388 myReadBuffer = !myIsStereoBuffers ? stereoToMonoBuffer (theReadBuffer) : theReadBuffer;
389 if (myReadBuffer < GL_COLOR_ATTACHMENT0
392 arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
394 ::glReadBuffer (myReadBuffer);
396 (void )theReadBuffer;
400 // =======================================================================
401 // function : SetDrawBuffer
403 // =======================================================================
404 void OpenGl_Context::SetDrawBuffer (const Standard_Integer theDrawBuffer)
406 #if !defined(GL_ES_VERSION_2_0)
407 const Standard_Integer aDrawBuffer = !myIsStereoBuffers ? stereoToMonoBuffer (theDrawBuffer) : theDrawBuffer;
408 if (aDrawBuffer < GL_COLOR_ATTACHMENT0
411 arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
413 ::glDrawBuffer (aDrawBuffer);
415 myDrawBuffers.Clear();
417 if (aDrawBuffer != GL_NONE)
419 myDrawBuffers.SetValue (0, aDrawBuffer);
422 (void )theDrawBuffer;
426 // =======================================================================
427 // function : SetDrawBuffers
429 // =======================================================================
430 void OpenGl_Context::SetDrawBuffers (const Standard_Integer theNb, const Standard_Integer* theDrawBuffers)
432 Standard_ASSERT_RETURN (hasDrawBuffers, "Multiple draw buffers feature is not supported by the context", Standard_ASSERT_DO_NOTHING());
434 myDrawBuffers.Clear();
436 Standard_Boolean useDefaultFbo = Standard_False;
437 for (Standard_Integer anI = 0; anI < theNb; ++anI)
439 #if !defined(GL_ES_VERSION_2_0)
440 const Standard_Integer aDrawBuffer = !myIsStereoBuffers ? stereoToMonoBuffer (theDrawBuffers[anI]) : theDrawBuffers[anI];
442 const Standard_Integer aDrawBuffer = theDrawBuffers[anI];
444 if (aDrawBuffer < GL_COLOR_ATTACHMENT0 && aDrawBuffer != GL_NONE)
446 useDefaultFbo = Standard_True;
448 else if (aDrawBuffer != GL_NONE)
450 myDrawBuffers.SetValue (anI, aDrawBuffer);
453 if (arbFBO != NULL && useDefaultFbo)
455 arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
458 myFuncs->glDrawBuffers (theNb, (const GLenum*)theDrawBuffers);
461 // =======================================================================
462 // function : SetCullBackFaces
464 // =======================================================================
465 void OpenGl_Context::SetCullBackFaces (bool theToEnable)
467 if (myToCullBackFaces == theToEnable)
472 myToCullBackFaces = theToEnable;
475 //glCullFace (GL_BACK); GL_BACK by default
476 core11fwd->glEnable (GL_CULL_FACE);
480 core11fwd->glDisable (GL_CULL_FACE);
484 // =======================================================================
485 // function : FetchState
487 // =======================================================================
488 void OpenGl_Context::FetchState()
490 #if !defined(GL_ES_VERSION_2_0)
491 // cache feedback mode state
494 ::glGetIntegerv (GL_RENDER_MODE, &myRenderMode);
497 // cache read buffers state
498 ::glGetIntegerv (GL_READ_BUFFER, &myReadBuffer);
500 // cache draw buffers state
501 myDrawBuffers.Clear();
503 if (myMaxDrawBuffers == 1)
505 Standard_Integer aDrawBuffer;
507 ::glGetIntegerv (GL_DRAW_BUFFER, &aDrawBuffer);
509 if (aDrawBuffer != GL_NONE)
511 myDrawBuffers.SetValue (0, aDrawBuffer);
516 Standard_Integer aDrawBuffer;
518 for (Standard_Integer anI = 0; anI < myMaxDrawBuffers; ++anI)
520 ::glGetIntegerv (GL_DRAW_BUFFER0 + anI, &aDrawBuffer);
522 if (aDrawBuffer != GL_NONE)
524 myDrawBuffers.SetValue (anI, aDrawBuffer);
531 // =======================================================================
534 // =======================================================================
535 void OpenGl_Context::Share (const Handle(OpenGl_Context)& theShareCtx)
537 if (!theShareCtx.IsNull())
539 mySharedResources = theShareCtx->mySharedResources;
540 myDelayed = theShareCtx->myDelayed;
541 myUnusedResources = theShareCtx->myUnusedResources;
542 myShaderManager = theShareCtx->myShaderManager;
546 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
548 // =======================================================================
549 // function : IsCurrent
551 // =======================================================================
552 Standard_Boolean OpenGl_Context::IsCurrent() const
554 #if defined(HAVE_EGL)
555 if ((EGLDisplay )myDisplay == EGL_NO_DISPLAY
556 || (EGLSurface )myWindow == EGL_NO_SURFACE
557 || (EGLContext )myGContext == EGL_NO_CONTEXT)
559 return Standard_False;
562 return (((EGLDisplay )myDisplay == eglGetCurrentDisplay())
563 && ((EGLContext )myGContext == eglGetCurrentContext())
564 && ((EGLSurface )myWindow == eglGetCurrentSurface (EGL_DRAW)));
565 #elif defined(_WIN32)
566 if (myWindowDC == NULL || myGContext == NULL)
568 return Standard_False;
570 return (( (HDC )myWindowDC == wglGetCurrentDC())
571 && ((HGLRC )myGContext == wglGetCurrentContext()));
573 if (myDisplay == NULL || myWindow == 0 || myGContext == 0)
575 return Standard_False;
578 return ( ((Display* )myDisplay == glXGetCurrentDisplay())
579 && ((GLXContext )myGContext == glXGetCurrentContext())
580 && ((GLXDrawable )myWindow == glXGetCurrentDrawable()));
584 // =======================================================================
585 // function : MakeCurrent
587 // =======================================================================
588 Standard_Boolean OpenGl_Context::MakeCurrent()
590 #if defined(HAVE_EGL)
591 if ((EGLDisplay )myDisplay == EGL_NO_DISPLAY
592 || (EGLSurface )myWindow == EGL_NO_SURFACE
593 || (EGLContext )myGContext == EGL_NO_CONTEXT)
595 Standard_ProgramError_Raise_if (myIsInitialized, "OpenGl_Context::Init() should be called before!");
596 return Standard_False;
599 if (eglMakeCurrent ((EGLDisplay )myDisplay, (EGLSurface )myWindow, (EGLSurface )myWindow, (EGLContext )myGContext) != EGL_TRUE)
601 // if there is no current context it might be impossible to use glGetError() correctly
602 PushMessage (GL_DEBUG_SOURCE_WINDOW_SYSTEM, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH,
603 "eglMakeCurrent() has failed!");
604 myIsInitialized = Standard_False;
605 return Standard_False;
607 #elif defined(_WIN32)
608 if (myWindowDC == NULL || myGContext == NULL)
610 Standard_ProgramError_Raise_if (myIsInitialized, "OpenGl_Context::Init() should be called before!");
611 return Standard_False;
614 // technically it should be safe to activate already bound GL context
615 // however some drivers (Intel etc.) may FAIL doing this for unknown reason
618 myShaderManager->SetContext (this);
619 return Standard_True;
621 else if (wglMakeCurrent ((HDC )myWindowDC, (HGLRC )myGContext) != TRUE)
623 // notice that glGetError() couldn't be used here!
624 wchar_t* aMsgBuff = NULL;
625 DWORD anErrorCode = GetLastError();
626 FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
627 NULL, anErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (wchar_t* )&aMsgBuff, 0, NULL);
628 TCollection_ExtendedString aMsg ("wglMakeCurrent() has failed. ");
629 if (aMsgBuff != NULL)
631 aMsg += (Standard_ExtString )aMsgBuff;
632 LocalFree (aMsgBuff);
634 PushMessage (GL_DEBUG_SOURCE_WINDOW_SYSTEM, GL_DEBUG_TYPE_ERROR, (unsigned int )anErrorCode, GL_DEBUG_SEVERITY_HIGH, aMsg);
635 myIsInitialized = Standard_False;
636 return Standard_False;
639 if (myDisplay == NULL || myWindow == 0 || myGContext == 0)
641 Standard_ProgramError_Raise_if (myIsInitialized, "OpenGl_Context::Init() should be called before!");
642 return Standard_False;
645 if (!glXMakeCurrent ((Display* )myDisplay, (GLXDrawable )myWindow, (GLXContext )myGContext))
647 // if there is no current context it might be impossible to use glGetError() correctly
648 PushMessage (GL_DEBUG_SOURCE_WINDOW_SYSTEM, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH,
649 "glXMakeCurrent() has failed!");
650 myIsInitialized = Standard_False;
651 return Standard_False;
654 myShaderManager->SetContext (this);
655 return Standard_True;
658 // =======================================================================
659 // function : SwapBuffers
661 // =======================================================================
662 void OpenGl_Context::SwapBuffers()
664 #if defined(HAVE_EGL)
665 if ((EGLSurface )myWindow != EGL_NO_SURFACE)
667 eglSwapBuffers ((EGLDisplay )myDisplay, (EGLSurface )myWindow);
669 #elif defined(_WIN32)
670 if ((HDC )myWindowDC != NULL)
672 ::SwapBuffers ((HDC )myWindowDC);
676 if ((Display* )myDisplay != NULL)
678 glXSwapBuffers ((Display* )myDisplay, (GLXDrawable )myWindow);
685 // =======================================================================
686 // function : SetSwapInterval
688 // =======================================================================
689 Standard_Boolean OpenGl_Context::SetSwapInterval (const Standard_Integer theInterval)
691 #if defined(HAVE_EGL)
692 if (::eglSwapInterval ((EGLDisplay )myDisplay, theInterval) == EGL_TRUE)
694 return Standard_True;
696 #elif defined(_WIN32)
697 if (myFuncs->wglSwapIntervalEXT != NULL)
699 myFuncs->wglSwapIntervalEXT (theInterval);
700 return Standard_True;
702 #elif defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
704 #elif defined(__APPLE__)
705 if (::CGLSetParameter (CGLGetCurrentContext(), kCGLCPSwapInterval, &theInterval) == kCGLNoError)
707 return Standard_True;
710 if (theInterval == -1
711 && myFuncs->glXSwapIntervalEXT != NULL)
713 typedef int (*glXSwapIntervalEXT_t_x)(Display* theDisplay, GLXDrawable theDrawable, int theInterval);
714 glXSwapIntervalEXT_t_x aFuncPtr = (glXSwapIntervalEXT_t_x )myFuncs->glXSwapIntervalEXT;
715 aFuncPtr ((Display* )myDisplay, (GLXDrawable )myWindow, theInterval);
716 return Standard_True;
718 else if (myFuncs->glXSwapIntervalSGI != NULL)
720 myFuncs->glXSwapIntervalSGI (theInterval);
721 return Standard_True;
724 return Standard_False;
727 // =======================================================================
728 // function : findProc
730 // =======================================================================
731 void* OpenGl_Context::findProc (const char* theFuncName)
733 #if defined(HAVE_EGL)
734 return (void* )eglGetProcAddress (theFuncName);
735 #elif defined(_WIN32)
736 return (void* )wglGetProcAddress (theFuncName);
737 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
738 return (myGlLibHandle != NULL) ? dlsym (myGlLibHandle, theFuncName) : NULL;
740 return (void* )glXGetProcAddress ((const GLubyte* )theFuncName);
744 // =======================================================================
745 // function : CheckExtension
747 // =======================================================================
748 Standard_Boolean OpenGl_Context::CheckExtension (const char* theExtName) const
750 if (theExtName == NULL)
753 std::cerr << "CheckExtension called with NULL string!\n";
755 return Standard_False;
758 #if !defined(GL_ES_VERSION_2_0)
759 // available since OpenGL 3.0
760 // and the ONLY way to check extensions with OpenGL 3.1+ core profile
761 if (IsGlGreaterEqual (3, 0)
762 && myFuncs->glGetStringi != NULL)
765 ::glGetIntegerv (GL_NUM_EXTENSIONS, &anExtNb);
766 const size_t anExtNameLen = strlen (theExtName);
767 for (GLint anIter = 0; anIter < anExtNb; ++anIter)
769 const char* anExtension = (const char* )myFuncs->glGetStringi (GL_EXTENSIONS, (GLuint )anIter);
770 const size_t aTestExtNameLen = strlen (anExtension);
771 if (aTestExtNameLen == anExtNameLen
772 && strncmp (anExtension, theExtName, anExtNameLen) == 0)
774 return Standard_True;
777 return Standard_False;
781 // use old way with huge string for all extensions
782 const char* anExtString = (const char* )glGetString (GL_EXTENSIONS);
783 if (anExtString == NULL)
785 Messenger()->Send ("TKOpenGL: glGetString (GL_EXTENSIONS) has returned NULL! No GL context?", Message_Warning);
786 return Standard_False;
788 return CheckExtension (anExtString, theExtName);
791 // =======================================================================
792 // function : CheckExtension
794 // =======================================================================
795 Standard_Boolean OpenGl_Context::CheckExtension (const char* theExtString,
796 const char* theExtName)
798 if (theExtString == NULL)
800 return Standard_False;
803 // Search for theExtName in the extensions string.
804 // Use of strstr() is not sufficient because extension names can be prefixes of other extension names.
805 char* aPtrIter = (char* )theExtString;
806 const char* aPtrEnd = aPtrIter + strlen (theExtString);
807 const size_t anExtNameLen = strlen (theExtName);
808 while (aPtrIter < aPtrEnd)
810 const size_t n = strcspn (aPtrIter, " ");
811 if ((n == anExtNameLen) && (strncmp (aPtrIter, theExtName, anExtNameLen) == 0))
813 return Standard_True;
817 return Standard_False;
820 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
822 // =======================================================================
825 // =======================================================================
826 Standard_Boolean OpenGl_Context::Init (const Standard_Boolean theIsCoreProfile)
830 return Standard_True;
833 #if defined(HAVE_EGL)
834 myDisplay = (Aspect_Display )eglGetCurrentDisplay();
835 myGContext = (Aspect_RenderingContext )eglGetCurrentContext();
836 myWindow = (Aspect_Drawable )eglGetCurrentSurface(EGL_DRAW);
837 #elif defined(_WIN32)
838 myWindowDC = (Aspect_Handle )wglGetCurrentDC();
839 myGContext = (Aspect_RenderingContext )wglGetCurrentContext();
841 myDisplay = (Aspect_Display )glXGetCurrentDisplay();
842 myGContext = (Aspect_RenderingContext )glXGetCurrentContext();
843 myWindow = (Aspect_Drawable )glXGetCurrentDrawable();
845 if (myGContext == NULL)
847 return Standard_False;
850 init (theIsCoreProfile);
851 myIsInitialized = Standard_True;
852 return Standard_True;
857 // =======================================================================
860 // =======================================================================
861 #if defined(HAVE_EGL)
862 Standard_Boolean OpenGl_Context::Init (const Aspect_Drawable theEglSurface,
863 const Aspect_Display theEglDisplay,
864 const Aspect_RenderingContext theEglContext,
865 const Standard_Boolean theIsCoreProfile)
866 #elif defined(_WIN32)
867 Standard_Boolean OpenGl_Context::Init (const Aspect_Handle theWindow,
868 const Aspect_Handle theWindowDC,
869 const Aspect_RenderingContext theGContext,
870 const Standard_Boolean theIsCoreProfile)
871 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
873 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
874 Standard_Boolean OpenGl_Context::Init (EAGLContext* theGContext,
875 const Standard_Boolean theIsCoreProfile)
877 Standard_Boolean OpenGl_Context::Init (NSOpenGLContext* theGContext,
878 const Standard_Boolean theIsCoreProfile)
882 Standard_Boolean OpenGl_Context::Init (const Aspect_Drawable theWindow,
883 const Aspect_Display theDisplay,
884 const Aspect_RenderingContext theGContext,
885 const Standard_Boolean theIsCoreProfile)
888 Standard_ProgramError_Raise_if (myIsInitialized, "OpenGl_Context::Init() should be called only once!");
889 #if defined(HAVE_EGL)
890 myWindow = theEglSurface;
891 myGContext = theEglContext;
892 myDisplay = theEglDisplay;
893 #elif defined(_WIN32)
894 myWindow = theWindow;
895 myGContext = theGContext;
896 myWindowDC = theWindowDC;
897 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
898 myGContext = theGContext;
900 myWindow = theWindow;
901 myGContext = theGContext;
902 myDisplay = theDisplay;
904 if (myGContext == NULL || !MakeCurrent())
906 return Standard_False;
909 init (theIsCoreProfile);
910 myIsInitialized = Standard_True;
911 return Standard_True;
914 // =======================================================================
915 // function : ResetErrors
917 // =======================================================================
918 bool OpenGl_Context::ResetErrors (const bool theToPrintErrors)
921 int anErr = ::glGetError();
922 const bool hasError = anErr != GL_NO_ERROR;
923 if (!theToPrintErrors)
925 for (; anErr != GL_NO_ERROR && aPrevErr != anErr; aPrevErr = anErr, anErr = ::glGetError())
932 for (; anErr != GL_NO_ERROR && aPrevErr != anErr; aPrevErr = anErr, anErr = ::glGetError())
934 TCollection_ExtendedString anErrId;
937 case GL_INVALID_ENUM: anErrId = "GL_INVALID_ENUM"; break;
938 case GL_INVALID_VALUE: anErrId = "GL_INVALID_VALUE"; break;
939 case GL_INVALID_OPERATION: anErrId = "GL_INVALID_OPERATION"; break;
940 #ifdef GL_STACK_OVERFLOW
941 case GL_STACK_OVERFLOW: anErrId = "GL_STACK_OVERFLOW"; break;
942 case GL_STACK_UNDERFLOW: anErrId = "GL_STACK_UNDERFLOW"; break;
944 case GL_OUT_OF_MEMORY: anErrId = "GL_OUT_OF_MEMORY"; break;
945 case GL_INVALID_FRAMEBUFFER_OPERATION:
946 anErrId = "GL_INVALID_FRAMEBUFFER_OPERATION";
949 anErrId = TCollection_ExtendedString("#") + anErr;
953 const TCollection_ExtendedString aMsg = TCollection_ExtendedString ("Unhandled GL error: ") + anErrId;
954 PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_OTHER, 0, GL_DEBUG_SEVERITY_LOW, aMsg);
959 // =======================================================================
960 // function : ReadGlVersion
962 // =======================================================================
963 void OpenGl_Context::ReadGlVersion (Standard_Integer& theGlVerMajor,
964 Standard_Integer& theGlVerMinor)
970 #ifdef GL_MAJOR_VERSION
971 // available since OpenGL 3.0 and OpenGL 3.0 ES
972 GLint aMajor = 0, aMinor = 0;
973 glGetIntegerv (GL_MAJOR_VERSION, &aMajor);
974 glGetIntegerv (GL_MINOR_VERSION, &aMinor);
975 // glGetError() sometimes does not report an error here even if
976 // GL does not know GL_MAJOR_VERSION and GL_MINOR_VERSION constants.
977 // This happens on some renderers like e.g. Cygwin MESA.
978 // Thus checking additionally if GL has put anything to
979 // the output variables.
980 if (::glGetError() == GL_NO_ERROR && aMajor != 0 && aMinor != 0)
982 theGlVerMajor = aMajor;
983 theGlVerMinor = aMinor;
986 for (GLenum anErr = ::glGetError(), aPrevErr = GL_NO_ERROR;; aPrevErr = anErr, anErr = ::glGetError())
988 if (anErr == GL_NO_ERROR
989 || anErr == aPrevErr)
996 // Read version string.
997 // Notice that only first two numbers split by point '2.1 XXXXX' are significant.
998 // Following trash (after space) is vendor-specific.
999 // New drivers also returns micro version of GL like '3.3.0' which has no meaning
1000 // and should be considered as vendor-specific too.
1001 const char* aVerStr = (const char* )glGetString (GL_VERSION);
1002 if (aVerStr == NULL || *aVerStr == '\0')
1004 // invalid GL context
1008 //#if defined(GL_ES_VERSION_2_0)
1009 // skip "OpenGL ES-** " section
1010 for (; *aVerStr != '\0'; ++aVerStr)
1012 if (*aVerStr >= '0' && *aVerStr <= '9')
1019 // parse string for major number
1022 size_t aMajIter = 0;
1023 while (aVerStr[aMajIter] >= '0' && aVerStr[aMajIter] <= '9')
1027 if (aMajIter == 0 || aMajIter >= sizeof(aMajorStr))
1031 memcpy (aMajorStr, aVerStr, aMajIter);
1032 aMajorStr[aMajIter] = '\0';
1034 // parse string for minor number
1035 aVerStr += aMajIter + 1;
1036 size_t aMinIter = 0;
1037 while (aVerStr[aMinIter] >= '0' && aVerStr[aMinIter] <= '9')
1041 if (aMinIter == 0 || aMinIter >= sizeof(aMinorStr))
1045 memcpy (aMinorStr, aVerStr, aMinIter);
1046 aMinorStr[aMinIter] = '\0';
1049 theGlVerMajor = atoi (aMajorStr);
1050 theGlVerMinor = atoi (aMinorStr);
1052 if (theGlVerMajor <= 0)
1059 static Standard_CString THE_DBGMSG_UNKNOWN = "UNKNOWN";
1060 static Standard_CString THE_DBGMSG_SOURCES[] =
1062 ".OpenGL", // GL_DEBUG_SOURCE_API
1063 ".WinSystem", // GL_DEBUG_SOURCE_WINDOW_SYSTEM
1064 ".GLSL", // GL_DEBUG_SOURCE_SHADER_COMPILER
1065 ".3rdParty", // GL_DEBUG_SOURCE_THIRD_PARTY
1066 "", // GL_DEBUG_SOURCE_APPLICATION
1067 ".Other" // GL_DEBUG_SOURCE_OTHER
1070 static Standard_CString THE_DBGMSG_TYPES[] =
1072 "Error", // GL_DEBUG_TYPE_ERROR
1073 "Deprecated", // GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR
1074 "Undef. behavior", // GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR
1075 "Portability", // GL_DEBUG_TYPE_PORTABILITY
1076 "Performance", // GL_DEBUG_TYPE_PERFORMANCE
1077 "Other" // GL_DEBUG_TYPE_OTHER
1080 static Standard_CString THE_DBGMSG_SEV_HIGH = "High"; // GL_DEBUG_SEVERITY_HIGH
1081 static Standard_CString THE_DBGMSG_SEV_MEDIUM = "Medium"; // GL_DEBUG_SEVERITY_MEDIUM
1082 static Standard_CString THE_DBGMSG_SEV_LOW = "Low"; // GL_DEBUG_SEVERITY_LOW
1084 //! Callback for GL_ARB_debug_output extension
1085 static void APIENTRY debugCallbackWrap(unsigned int theSource,
1086 unsigned int theType,
1088 unsigned int theSeverity,
1090 const char* theMessage,
1091 const void* theUserParam)
1093 OpenGl_Context* aCtx = (OpenGl_Context* )theUserParam;
1094 aCtx->PushMessage (theSource, theType, theId, theSeverity, theMessage);
1097 // =======================================================================
1098 // function : PushMessage
1100 // =======================================================================
1101 void OpenGl_Context::PushMessage (const unsigned int theSource,
1102 const unsigned int theType,
1103 const unsigned int theId,
1104 const unsigned int theSeverity,
1105 const TCollection_ExtendedString& theMessage)
1107 if (caps->suppressExtraMsg
1108 && theSource >= GL_DEBUG_SOURCE_API
1109 && theSource <= GL_DEBUG_SOURCE_OTHER
1110 && myFilters[theSource - GL_DEBUG_SOURCE_API].Contains (theId))
1115 Standard_CString& aSrc = (theSource >= GL_DEBUG_SOURCE_API
1116 && theSource <= GL_DEBUG_SOURCE_OTHER)
1117 ? THE_DBGMSG_SOURCES[theSource - GL_DEBUG_SOURCE_API]
1118 : THE_DBGMSG_UNKNOWN;
1119 Standard_CString& aType = (theType >= GL_DEBUG_TYPE_ERROR
1120 && theType <= GL_DEBUG_TYPE_OTHER)
1121 ? THE_DBGMSG_TYPES[theType - GL_DEBUG_TYPE_ERROR]
1122 : THE_DBGMSG_UNKNOWN;
1123 Standard_CString& aSev = theSeverity == GL_DEBUG_SEVERITY_HIGH
1124 ? THE_DBGMSG_SEV_HIGH
1125 : (theSeverity == GL_DEBUG_SEVERITY_MEDIUM
1126 ? THE_DBGMSG_SEV_MEDIUM
1127 : THE_DBGMSG_SEV_LOW);
1128 Message_Gravity aGrav = theSeverity == GL_DEBUG_SEVERITY_HIGH
1130 : (theSeverity == GL_DEBUG_SEVERITY_MEDIUM
1134 TCollection_ExtendedString aMsg;
1135 aMsg += "TKOpenGl"; aMsg += aSrc;
1136 aMsg += " | Type: "; aMsg += aType;
1137 aMsg += " | ID: "; aMsg += (Standard_Integer )theId;
1138 aMsg += " | Severity: "; aMsg += aSev;
1139 aMsg += " | Message:\n ";
1141 Messenger()->Send (aMsg, aGrav);
1144 // =======================================================================
1145 // function : ExcludeMessage
1147 // ======================================================================
1148 Standard_Boolean OpenGl_Context::ExcludeMessage (const unsigned int theSource,
1149 const unsigned int theId)
1151 return theSource >= GL_DEBUG_SOURCE_API
1152 && theSource <= GL_DEBUG_SOURCE_OTHER
1153 && myFilters[theSource - GL_DEBUG_SOURCE_API].Add (theId);
1156 // =======================================================================
1157 // function : IncludeMessage
1159 // ======================================================================
1160 Standard_Boolean OpenGl_Context::IncludeMessage (const unsigned int theSource,
1161 const unsigned int theId)
1163 return theSource >= GL_DEBUG_SOURCE_API
1164 && theSource <= GL_DEBUG_SOURCE_OTHER
1165 && myFilters[theSource - GL_DEBUG_SOURCE_API].Remove (theId);
1168 // =======================================================================
1169 // function : checkWrongVersion
1171 // ======================================================================
1172 void OpenGl_Context::checkWrongVersion (const Standard_Integer theGlVerMajor,
1173 const Standard_Integer theGlVerMinor)
1175 if (!IsGlGreaterEqual (theGlVerMajor, theGlVerMinor))
1180 TCollection_ExtendedString aMsg = TCollection_ExtendedString()
1181 + "Error! OpenGL context reports version "
1182 + myGlVerMajor + "." + myGlVerMinor
1183 + " but does not export required functions for "
1184 + theGlVerMajor + "." + theGlVerMinor;
1185 PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1186 GL_DEBUG_TYPE_ERROR,
1188 GL_DEBUG_SEVERITY_HIGH,
1192 // =======================================================================
1195 // =======================================================================
1196 void OpenGl_Context::init (const Standard_Boolean theIsCoreProfile)
1201 myMaxMsaaSamples = 0;
1202 myMaxDrawBuffers = 1;
1203 myMaxColorAttachments = 1;
1204 ReadGlVersion (myGlVerMajor, myGlVerMinor);
1205 myVendor = (const char* )::glGetString (GL_VENDOR);
1206 if (!caps->ffpEnable
1207 && !IsGlGreaterEqual (2, 0))
1209 caps->ffpEnable = true;
1210 TCollection_ExtendedString aMsg =
1211 TCollection_ExtendedString("OpenGL driver is too old! Context info:\n")
1212 + " Vendor: " + (const char* )::glGetString (GL_VENDOR) + "\n"
1213 + " Renderer: " + (const char* )::glGetString (GL_RENDERER) + "\n"
1214 + " Version: " + (const char* )::glGetString (GL_VERSION) + "\n"
1215 + " Fallback using deprecated fixed-function pipeline.\n"
1216 + " Visualization might work incorrectly.\n"
1217 " Consider upgrading the graphics driver.";
1218 PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
1221 #if defined(GL_ES_VERSION_2_0)
1222 (void )theIsCoreProfile;
1223 const bool isCoreProfile = false;
1226 if (myVendor.Search ("NVIDIA") != -1)
1228 // Buffer detailed info: Buffer object 1 (bound to GL_ARRAY_BUFFER_ARB, usage hint is GL_STATIC_DRAW)
1229 // will use VIDEO memory as the source for buffer object operations.
1230 ExcludeMessage (GL_DEBUG_SOURCE_API, 131185);
1232 if (IsGlGreaterEqual (3, 0))
1234 // retrieve auxiliary function in advance
1235 FindProc ("glGetStringi", myFuncs->glGetStringi);
1238 bool isCoreProfile = false;
1239 if (IsGlGreaterEqual (3, 2))
1241 isCoreProfile = (theIsCoreProfile == Standard_True);
1243 // detect Core profile
1247 ::glGetIntegerv (GL_CONTEXT_PROFILE_MASK, &aProfile);
1248 isCoreProfile = (aProfile & GL_CONTEXT_CORE_PROFILE_BIT) != 0;
1256 core11 = (OpenGl_GlCore11* )(&(*myFuncs));
1258 core11fwd = (OpenGl_GlCore11Fwd* )(&(*myFuncs));
1276 arbTboRGB32 = Standard_False;
1284 #if defined(GL_ES_VERSION_2_0)
1286 hasTexRGBA8 = IsGlGreaterEqual (3, 0)
1287 || CheckExtension ("GL_OES_rgb8_rgba8");
1288 // NPOT textures has limited support within OpenGL ES 2.0
1289 // which are relaxed by OpenGL ES 3.0 or some extensions
1290 //arbNPTW = IsGlGreaterEqual (3, 0)
1291 // || CheckExtension ("GL_OES_texture_npot")
1292 // || CheckExtension ("GL_NV_texture_npot_2D_mipmap");
1293 arbNPTW = Standard_True;
1294 arbTexRG = IsGlGreaterEqual (3, 0)
1295 || CheckExtension ("GL_EXT_texture_rg");
1296 extBgra = CheckExtension ("GL_EXT_texture_format_BGRA8888");
1297 extAnis = CheckExtension ("GL_EXT_texture_filter_anisotropic");
1298 extPDS = IsGlGreaterEqual (3, 0)
1299 || CheckExtension ("GL_OES_packed_depth_stencil");
1301 core11fwd = (OpenGl_GlCore11Fwd* )(&(*myFuncs));
1302 if (IsGlGreaterEqual (2, 0))
1304 // enable compatible functions
1305 core20 = (OpenGl_GlCore20* )(&(*myFuncs));
1306 core20fwd = (OpenGl_GlCore20Fwd* )(&(*myFuncs));
1307 core15fwd = (OpenGl_GlCore15Fwd* )(&(*myFuncs));
1308 arbFBO = (OpenGl_ArbFBO* )(&(*myFuncs));
1310 if (IsGlGreaterEqual (3, 0)
1311 && FindProc ("glBlitFramebuffer", myFuncs->glBlitFramebuffer))
1313 arbFBOBlit = (OpenGl_ArbFBOBlit* )(&(*myFuncs));
1315 extFragDepth = !IsGlGreaterEqual(3, 0)
1316 && CheckExtension ("GL_EXT_frag_depth");
1317 if (IsGlGreaterEqual (3, 1)
1318 && FindProc ("glTexStorage2DMultisample", myFuncs->glTexStorage2DMultisample))
1320 // MSAA RenderBuffers have been defined in OpenGL ES 3.0,
1321 // but MSAA Textures - only in OpenGL ES 3.1+
1322 ::glGetIntegerv (GL_MAX_SAMPLES, &myMaxMsaaSamples);
1325 hasUintIndex = IsGlGreaterEqual (3, 0)
1326 || CheckExtension ("GL_OES_element_index_uint");
1327 hasHighp = CheckExtension ("GL_OES_fragment_precision_high");
1328 GLint aRange[2] = {0, 0};
1330 ::glGetShaderPrecisionFormat (GL_FRAGMENT_SHADER, GL_HIGH_FLOAT, aRange, &aPrec);
1333 hasHighp = Standard_True;
1336 arbTexFloat = IsGlGreaterEqual (3, 0)
1337 && FindProc ("glTexImage3D", myFuncs->glTexImage3D);
1339 const Standard_Boolean hasTexBuffer32 = IsGlGreaterEqual (3, 2) && FindProc ("glTexBuffer", myFuncs->glTexBuffer);
1340 const Standard_Boolean hasExtTexBuffer = CheckExtension ("GL_EXT_texture_buffer") && FindProc ("glTexBufferEXT", myFuncs->glTexBuffer);
1342 if (hasTexBuffer32 || hasExtTexBuffer)
1344 arbTBO = reinterpret_cast<OpenGl_ArbTBO*> (myFuncs.get());
1347 // initialize debug context extension
1348 if (CheckExtension ("GL_KHR_debug"))
1350 // this functionality become a part of OpenGL ES 3.2
1352 // According to GL_KHR_debug spec, all functions should have KHR suffix.
1353 // However, some implementations can export these functions without suffix.
1354 if (FindProc ("glDebugMessageControlKHR", myFuncs->glDebugMessageControl)
1355 && FindProc ("glDebugMessageInsertKHR", myFuncs->glDebugMessageInsert)
1356 && FindProc ("glDebugMessageCallbackKHR", myFuncs->glDebugMessageCallback)
1357 && FindProc ("glGetDebugMessageLogKHR", myFuncs->glGetDebugMessageLog))
1359 arbDbg = (OpenGl_ArbDbg* )(&(*myFuncs));
1363 && caps->contextDebug)
1365 // setup default callback
1366 myIsGlDebugCtx = Standard_True;
1367 arbDbg->glDebugMessageCallback (debugCallbackWrap, this);
1368 ::glEnable (GL_DEBUG_OUTPUT);
1369 if (caps->contextSyncDebug)
1371 // note that some broken implementations (e.g. simulators) might generate error message on this call
1372 ::glEnable (GL_DEBUG_OUTPUT_SYNCHRONOUS);
1377 extDrawBuffers = CheckExtension ("GL_EXT_draw_buffers") && FindProc ("glDrawBuffersEXT", myFuncs->glDrawBuffers);
1378 arbDrawBuffers = CheckExtension ("GL_ARB_draw_buffers") && FindProc ("glDrawBuffersARB", myFuncs->glDrawBuffers);
1380 if (IsGlGreaterEqual (3, 0) && FindProc ("glDrawBuffers", myFuncs->glDrawBuffers))
1382 hasDrawBuffers = OpenGl_FeatureInCore;
1384 else if (extDrawBuffers || arbDrawBuffers)
1386 hasDrawBuffers = OpenGl_FeatureInExtensions;
1389 hasFloatBuffer = IsGlGreaterEqual (3, 2) ? OpenGl_FeatureInCore :
1390 CheckExtension ("GL_EXT_color_buffer_float") ? OpenGl_FeatureInExtensions
1391 : OpenGl_FeatureNotAvailable;
1392 hasHalfFloatBuffer = IsGlGreaterEqual (3, 2) ? OpenGl_FeatureInCore :
1393 CheckExtension ("GL_EXT_color_buffer_half_float") ? OpenGl_FeatureInExtensions
1394 : OpenGl_FeatureNotAvailable;
1396 oesSampleVariables = CheckExtension ("GL_OES_sample_variables");
1397 hasSampleVariables = IsGlGreaterEqual (3, 2) ? OpenGl_FeatureInCore :
1398 oesSampleVariables ? OpenGl_FeatureInExtensions
1399 : OpenGl_FeatureNotAvailable;
1402 myTexClamp = IsGlGreaterEqual (1, 2) ? GL_CLAMP_TO_EDGE : GL_CLAMP;
1404 hasTexRGBA8 = Standard_True;
1405 arbDrawBuffers = CheckExtension ("GL_ARB_draw_buffers");
1406 arbNPTW = CheckExtension ("GL_ARB_texture_non_power_of_two");
1407 arbTexFloat = IsGlGreaterEqual (3, 0)
1408 || CheckExtension ("GL_ARB_texture_float");
1409 arbSampleShading = CheckExtension ("GL_ARB_sample_shading");
1410 extBgra = CheckExtension ("GL_EXT_bgra");
1411 extAnis = CheckExtension ("GL_EXT_texture_filter_anisotropic");
1412 extPDS = CheckExtension ("GL_EXT_packed_depth_stencil");
1413 atiMem = CheckExtension ("GL_ATI_meminfo");
1414 nvxMem = CheckExtension ("GL_NVX_gpu_memory_info");
1416 hasDrawBuffers = IsGlGreaterEqual (2, 0) ? OpenGl_FeatureInCore :
1417 arbDrawBuffers ? OpenGl_FeatureInExtensions
1418 : OpenGl_FeatureNotAvailable;
1420 hasFloatBuffer = hasHalfFloatBuffer = IsGlGreaterEqual (3, 0) ? OpenGl_FeatureInCore :
1421 CheckExtension ("GL_ARB_color_buffer_float") ? OpenGl_FeatureInExtensions
1422 : OpenGl_FeatureNotAvailable;
1424 hasSampleVariables = IsGlGreaterEqual (4, 0) ? OpenGl_FeatureInCore :
1425 arbSampleShading ? OpenGl_FeatureInExtensions
1426 : OpenGl_FeatureNotAvailable;
1428 GLint aStereo = GL_FALSE;
1429 glGetIntegerv (GL_STEREO, &aStereo);
1430 myIsStereoBuffers = aStereo == 1;
1432 // get number of maximum clipping planes
1433 glGetIntegerv (GL_MAX_CLIP_PLANES, &myMaxClipPlanes);
1438 glGetIntegerv (GL_MAX_DRAW_BUFFERS, &myMaxDrawBuffers);
1439 glGetIntegerv (GL_MAX_COLOR_ATTACHMENTS, &myMaxColorAttachments);
1442 glGetIntegerv (GL_MAX_TEXTURE_SIZE, &myMaxTexDim);
1446 glGetIntegerv (GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &myAnisoMax);
1449 myClippingState.Init (myMaxClipPlanes);
1451 #if !defined(GL_ES_VERSION_2_0)
1469 //! Make record shorter to retrieve function pointer using variable with same name
1470 #define FindProcShort(theFunc) FindProc(#theFunc, myFuncs->theFunc)
1472 // retrieve platform-dependent extensions
1473 #if defined(HAVE_EGL)
1475 #elif defined(_WIN32)
1476 if (FindProcShort (wglGetExtensionsStringARB))
1478 const char* aWglExts = myFuncs->wglGetExtensionsStringARB (wglGetCurrentDC());
1479 if (CheckExtension (aWglExts, "WGL_EXT_swap_control"))
1481 FindProcShort (wglSwapIntervalEXT);
1483 if (CheckExtension (aWglExts, "WGL_ARB_pixel_format"))
1485 FindProcShort (wglChoosePixelFormatARB);
1487 if (CheckExtension (aWglExts, "WGL_ARB_create_context_profile"))
1489 FindProcShort (wglCreateContextAttribsARB);
1491 if (CheckExtension (aWglExts, "WGL_NV_DX_interop"))
1493 FindProcShort (wglDXSetResourceShareHandleNV);
1494 FindProcShort (wglDXOpenDeviceNV);
1495 FindProcShort (wglDXCloseDeviceNV);
1496 FindProcShort (wglDXRegisterObjectNV);
1497 FindProcShort (wglDXUnregisterObjectNV);
1498 FindProcShort (wglDXObjectAccessNV);
1499 FindProcShort (wglDXLockObjectsNV);
1500 FindProcShort (wglDXUnlockObjectsNV);
1502 if (CheckExtension (aWglExts, "WGL_AMD_gpu_association"))
1504 FindProcShort (wglGetGPUIDsAMD);
1505 FindProcShort (wglGetGPUInfoAMD);
1506 FindProcShort (wglGetContextGPUIDAMD);
1509 #elif defined(__APPLE__)
1512 const char* aGlxExts = ::glXQueryExtensionsString ((Display* )myDisplay, DefaultScreen ((Display* )myDisplay));
1513 if (CheckExtension (aGlxExts, "GLX_EXT_swap_control"))
1515 FindProcShort (glXSwapIntervalEXT);
1517 if (CheckExtension (aGlxExts, "GLX_SGI_swap_control"))
1519 FindProcShort (glXSwapIntervalSGI);
1521 if (CheckExtension (aGlxExts, "GLX_MESA_query_renderer"))
1523 FindProcShort (glXQueryRendererIntegerMESA);
1524 FindProcShort (glXQueryCurrentRendererIntegerMESA);
1525 FindProcShort (glXQueryRendererStringMESA);
1526 FindProcShort (glXQueryCurrentRendererStringMESA);
1528 //extSwapTear = CheckExtension (aGlxExts, "GLX_EXT_swap_control_tear");
1531 // load OpenGL 1.2 new functions
1532 has12 = IsGlGreaterEqual (1, 2)
1533 && FindProcShort (glBlendColor)
1534 && FindProcShort (glBlendEquation)
1535 && FindProcShort (glDrawRangeElements)
1536 && FindProcShort (glTexImage3D)
1537 && FindProcShort (glTexSubImage3D)
1538 && FindProcShort (glCopyTexSubImage3D);
1540 // load OpenGL 1.3 new functions
1541 has13 = IsGlGreaterEqual (1, 3)
1542 && FindProcShort (glActiveTexture)
1543 && FindProcShort (glSampleCoverage)
1544 && FindProcShort (glCompressedTexImage3D)
1545 && FindProcShort (glCompressedTexImage2D)
1546 && FindProcShort (glCompressedTexImage1D)
1547 && FindProcShort (glCompressedTexSubImage3D)
1548 && FindProcShort (glCompressedTexSubImage2D)
1549 && FindProcShort (glCompressedTexSubImage1D)
1550 && FindProcShort (glGetCompressedTexImage);
1555 && FindProcShort (glClientActiveTexture)
1556 && FindProcShort (glMultiTexCoord1d)
1557 && FindProcShort (glMultiTexCoord1dv)
1558 && FindProcShort (glMultiTexCoord1f)
1559 && FindProcShort (glMultiTexCoord1fv)
1560 && FindProcShort (glMultiTexCoord1i)
1561 && FindProcShort (glMultiTexCoord1iv)
1562 && FindProcShort (glMultiTexCoord1s)
1563 && FindProcShort (glMultiTexCoord1sv)
1564 && FindProcShort (glMultiTexCoord2d)
1565 && FindProcShort (glMultiTexCoord2dv)
1566 && FindProcShort (glMultiTexCoord2f)
1567 && FindProcShort (glMultiTexCoord2fv)
1568 && FindProcShort (glMultiTexCoord2i)
1569 && FindProcShort (glMultiTexCoord2iv)
1570 && FindProcShort (glMultiTexCoord2s)
1571 && FindProcShort (glMultiTexCoord2sv)
1572 && FindProcShort (glMultiTexCoord3d)
1573 && FindProcShort (glMultiTexCoord3dv)
1574 && FindProcShort (glMultiTexCoord3f)
1575 && FindProcShort (glMultiTexCoord3fv)
1576 && FindProcShort (glMultiTexCoord3i)
1577 && FindProcShort (glMultiTexCoord3iv)
1578 && FindProcShort (glMultiTexCoord3s)
1579 && FindProcShort (glMultiTexCoord3sv)
1580 && FindProcShort (glMultiTexCoord4d)
1581 && FindProcShort (glMultiTexCoord4dv)
1582 && FindProcShort (glMultiTexCoord4f)
1583 && FindProcShort (glMultiTexCoord4fv)
1584 && FindProcShort (glMultiTexCoord4i)
1585 && FindProcShort (glMultiTexCoord4iv)
1586 && FindProcShort (glMultiTexCoord4s)
1587 && FindProcShort (glMultiTexCoord4sv)
1588 && FindProcShort (glLoadTransposeMatrixf)
1589 && FindProcShort (glLoadTransposeMatrixd)
1590 && FindProcShort (glMultTransposeMatrixf)
1591 && FindProcShort (glMultTransposeMatrixd);
1594 // load OpenGL 1.4 new functions
1595 has14 = IsGlGreaterEqual (1, 4)
1596 && FindProcShort (glBlendFuncSeparate)
1597 && FindProcShort (glMultiDrawArrays)
1598 && FindProcShort (glMultiDrawElements)
1599 && FindProcShort (glPointParameterf)
1600 && FindProcShort (glPointParameterfv)
1601 && FindProcShort (glPointParameteri)
1602 && FindProcShort (glPointParameteriv);
1604 // load OpenGL 1.5 new functions
1605 has15 = IsGlGreaterEqual (1, 5)
1606 && FindProcShort (glGenQueries)
1607 && FindProcShort (glDeleteQueries)
1608 && FindProcShort (glIsQuery)
1609 && FindProcShort (glBeginQuery)
1610 && FindProcShort (glEndQuery)
1611 && FindProcShort (glGetQueryiv)
1612 && FindProcShort (glGetQueryObjectiv)
1613 && FindProcShort (glGetQueryObjectuiv)
1614 && FindProcShort (glBindBuffer)
1615 && FindProcShort (glDeleteBuffers)
1616 && FindProcShort (glGenBuffers)
1617 && FindProcShort (glIsBuffer)
1618 && FindProcShort (glBufferData)
1619 && FindProcShort (glBufferSubData)
1620 && FindProcShort (glGetBufferSubData)
1621 && FindProcShort (glMapBuffer)
1622 && FindProcShort (glUnmapBuffer)
1623 && FindProcShort (glGetBufferParameteriv)
1624 && FindProcShort (glGetBufferPointerv);
1626 // load OpenGL 2.0 new functions
1627 has20 = IsGlGreaterEqual (2, 0)
1628 && FindProcShort (glBlendEquationSeparate)
1629 && FindProcShort (glDrawBuffers)
1630 && FindProcShort (glStencilOpSeparate)
1631 && FindProcShort (glStencilFuncSeparate)
1632 && FindProcShort (glStencilMaskSeparate)
1633 && FindProcShort (glAttachShader)
1634 && FindProcShort (glBindAttribLocation)
1635 && FindProcShort (glCompileShader)
1636 && FindProcShort (glCreateProgram)
1637 && FindProcShort (glCreateShader)
1638 && FindProcShort (glDeleteProgram)
1639 && FindProcShort (glDeleteShader)
1640 && FindProcShort (glDetachShader)
1641 && FindProcShort (glDisableVertexAttribArray)
1642 && FindProcShort (glEnableVertexAttribArray)
1643 && FindProcShort (glGetActiveAttrib)
1644 && FindProcShort (glGetActiveUniform)
1645 && FindProcShort (glGetAttachedShaders)
1646 && FindProcShort (glGetAttribLocation)
1647 && FindProcShort (glGetProgramiv)
1648 && FindProcShort (glGetProgramInfoLog)
1649 && FindProcShort (glGetShaderiv)
1650 && FindProcShort (glGetShaderInfoLog)
1651 && FindProcShort (glGetShaderSource)
1652 && FindProcShort (glGetUniformLocation)
1653 && FindProcShort (glGetUniformfv)
1654 && FindProcShort (glGetUniformiv)
1655 && FindProcShort (glGetVertexAttribdv)
1656 && FindProcShort (glGetVertexAttribfv)
1657 && FindProcShort (glGetVertexAttribiv)
1658 && FindProcShort (glGetVertexAttribPointerv)
1659 && FindProcShort (glIsProgram)
1660 && FindProcShort (glIsShader)
1661 && FindProcShort (glLinkProgram)
1662 && FindProcShort (glShaderSource)
1663 && FindProcShort (glUseProgram)
1664 && FindProcShort (glUniform1f)
1665 && FindProcShort (glUniform2f)
1666 && FindProcShort (glUniform3f)
1667 && FindProcShort (glUniform4f)
1668 && FindProcShort (glUniform1i)
1669 && FindProcShort (glUniform2i)
1670 && FindProcShort (glUniform3i)
1671 && FindProcShort (glUniform4i)
1672 && FindProcShort (glUniform1fv)
1673 && FindProcShort (glUniform2fv)
1674 && FindProcShort (glUniform3fv)
1675 && FindProcShort (glUniform4fv)
1676 && FindProcShort (glUniform1iv)
1677 && FindProcShort (glUniform2iv)
1678 && FindProcShort (glUniform3iv)
1679 && FindProcShort (glUniform4iv)
1680 && FindProcShort (glUniformMatrix2fv)
1681 && FindProcShort (glUniformMatrix3fv)
1682 && FindProcShort (glUniformMatrix4fv)
1683 && FindProcShort (glValidateProgram)
1684 && FindProcShort (glVertexAttrib1d)
1685 && FindProcShort (glVertexAttrib1dv)
1686 && FindProcShort (glVertexAttrib1f)
1687 && FindProcShort (glVertexAttrib1fv)
1688 && FindProcShort (glVertexAttrib1s)
1689 && FindProcShort (glVertexAttrib1sv)
1690 && FindProcShort (glVertexAttrib2d)
1691 && FindProcShort (glVertexAttrib2dv)
1692 && FindProcShort (glVertexAttrib2f)
1693 && FindProcShort (glVertexAttrib2fv)
1694 && FindProcShort (glVertexAttrib2s)
1695 && FindProcShort (glVertexAttrib2sv)
1696 && FindProcShort (glVertexAttrib3d)
1697 && FindProcShort (glVertexAttrib3dv)
1698 && FindProcShort (glVertexAttrib3f)
1699 && FindProcShort (glVertexAttrib3fv)
1700 && FindProcShort (glVertexAttrib3s)
1701 && FindProcShort (glVertexAttrib3sv)
1702 && FindProcShort (glVertexAttrib4Nbv)
1703 && FindProcShort (glVertexAttrib4Niv)
1704 && FindProcShort (glVertexAttrib4Nsv)
1705 && FindProcShort (glVertexAttrib4Nub)
1706 && FindProcShort (glVertexAttrib4Nubv)
1707 && FindProcShort (glVertexAttrib4Nuiv)
1708 && FindProcShort (glVertexAttrib4Nusv)
1709 && FindProcShort (glVertexAttrib4bv)
1710 && FindProcShort (glVertexAttrib4d)
1711 && FindProcShort (glVertexAttrib4dv)
1712 && FindProcShort (glVertexAttrib4f)
1713 && FindProcShort (glVertexAttrib4fv)
1714 && FindProcShort (glVertexAttrib4iv)
1715 && FindProcShort (glVertexAttrib4s)
1716 && FindProcShort (glVertexAttrib4sv)
1717 && FindProcShort (glVertexAttrib4ubv)
1718 && FindProcShort (glVertexAttrib4uiv)
1719 && FindProcShort (glVertexAttrib4usv)
1720 && FindProcShort (glVertexAttribPointer);
1722 // load OpenGL 2.1 new functions
1723 has21 = IsGlGreaterEqual (2, 1)
1724 && FindProcShort (glUniformMatrix2x3fv)
1725 && FindProcShort (glUniformMatrix3x2fv)
1726 && FindProcShort (glUniformMatrix2x4fv)
1727 && FindProcShort (glUniformMatrix4x2fv)
1728 && FindProcShort (glUniformMatrix3x4fv)
1729 && FindProcShort (glUniformMatrix4x3fv);
1731 // load GL_ARB_framebuffer_object (added to OpenGL 3.0 core)
1732 const bool hasFBO = (IsGlGreaterEqual (3, 0) || CheckExtension ("GL_ARB_framebuffer_object"))
1733 && FindProcShort (glIsRenderbuffer)
1734 && FindProcShort (glBindRenderbuffer)
1735 && FindProcShort (glDeleteRenderbuffers)
1736 && FindProcShort (glGenRenderbuffers)
1737 && FindProcShort (glRenderbufferStorage)
1738 && FindProcShort (glGetRenderbufferParameteriv)
1739 && FindProcShort (glIsFramebuffer)
1740 && FindProcShort (glBindFramebuffer)
1741 && FindProcShort (glDeleteFramebuffers)
1742 && FindProcShort (glGenFramebuffers)
1743 && FindProcShort (glCheckFramebufferStatus)
1744 && FindProcShort (glFramebufferTexture1D)
1745 && FindProcShort (glFramebufferTexture2D)
1746 && FindProcShort (glFramebufferTexture3D)
1747 && FindProcShort (glFramebufferRenderbuffer)
1748 && FindProcShort (glGetFramebufferAttachmentParameteriv)
1749 && FindProcShort (glGenerateMipmap)
1750 && FindProcShort (glBlitFramebuffer)
1751 && FindProcShort (glRenderbufferStorageMultisample)
1752 && FindProcShort (glFramebufferTextureLayer);
1754 // load GL_ARB_vertex_array_object (added to OpenGL 3.0 core)
1755 const bool hasVAO = (IsGlGreaterEqual (3, 0) || CheckExtension ("GL_ARB_vertex_array_object"))
1756 && FindProcShort (glBindVertexArray)
1757 && FindProcShort (glDeleteVertexArrays)
1758 && FindProcShort (glGenVertexArrays)
1759 && FindProcShort (glIsVertexArray);
1761 // load GL_ARB_map_buffer_range (added to OpenGL 3.0 core)
1762 const bool hasMapBufferRange = (IsGlGreaterEqual (3, 0) || CheckExtension ("GL_ARB_map_buffer_range"))
1763 && FindProcShort (glMapBufferRange)
1764 && FindProcShort (glFlushMappedBufferRange);
1766 // load OpenGL 3.0 new functions
1767 has30 = IsGlGreaterEqual (3, 0)
1770 && hasMapBufferRange
1771 && FindProcShort (glColorMaski)
1772 && FindProcShort (glGetBooleani_v)
1773 && FindProcShort (glGetIntegeri_v)
1774 && FindProcShort (glEnablei)
1775 && FindProcShort (glDisablei)
1776 && FindProcShort (glIsEnabledi)
1777 && FindProcShort (glBeginTransformFeedback)
1778 && FindProcShort (glEndTransformFeedback)
1779 && FindProcShort (glBindBufferRange)
1780 && FindProcShort (glBindBufferBase)
1781 && FindProcShort (glTransformFeedbackVaryings)
1782 && FindProcShort (glGetTransformFeedbackVarying)
1783 && FindProcShort (glClampColor)
1784 && FindProcShort (glBeginConditionalRender)
1785 && FindProcShort (glEndConditionalRender)
1786 && FindProcShort (glVertexAttribIPointer)
1787 && FindProcShort (glGetVertexAttribIiv)
1788 && FindProcShort (glGetVertexAttribIuiv)
1789 && FindProcShort (glVertexAttribI1i)
1790 && FindProcShort (glVertexAttribI2i)
1791 && FindProcShort (glVertexAttribI3i)
1792 && FindProcShort (glVertexAttribI4i)
1793 && FindProcShort (glVertexAttribI1ui)
1794 && FindProcShort (glVertexAttribI2ui)
1795 && FindProcShort (glVertexAttribI3ui)
1796 && FindProcShort (glVertexAttribI4ui)
1797 && FindProcShort (glVertexAttribI1iv)
1798 && FindProcShort (glVertexAttribI2iv)
1799 && FindProcShort (glVertexAttribI3iv)
1800 && FindProcShort (glVertexAttribI4iv)
1801 && FindProcShort (glVertexAttribI1uiv)
1802 && FindProcShort (glVertexAttribI2uiv)
1803 && FindProcShort (glVertexAttribI3uiv)
1804 && FindProcShort (glVertexAttribI4uiv)
1805 && FindProcShort (glVertexAttribI4bv)
1806 && FindProcShort (glVertexAttribI4sv)
1807 && FindProcShort (glVertexAttribI4ubv)
1808 && FindProcShort (glVertexAttribI4usv)
1809 && FindProcShort (glGetUniformuiv)
1810 && FindProcShort (glBindFragDataLocation)
1811 && FindProcShort (glGetFragDataLocation)
1812 && FindProcShort (glUniform1ui)
1813 && FindProcShort (glUniform2ui)
1814 && FindProcShort (glUniform3ui)
1815 && FindProcShort (glUniform4ui)
1816 && FindProcShort (glUniform1uiv)
1817 && FindProcShort (glUniform2uiv)
1818 && FindProcShort (glUniform3uiv)
1819 && FindProcShort (glUniform4uiv)
1820 && FindProcShort (glTexParameterIiv)
1821 && FindProcShort (glTexParameterIuiv)
1822 && FindProcShort (glGetTexParameterIiv)
1823 && FindProcShort (glGetTexParameterIuiv)
1824 && FindProcShort (glClearBufferiv)
1825 && FindProcShort (glClearBufferuiv)
1826 && FindProcShort (glClearBufferfv)
1827 && FindProcShort (glClearBufferfi)
1828 && FindProcShort (glGetStringi);
1830 // load GL_ARB_uniform_buffer_object (added to OpenGL 3.1 core)
1831 const bool hasUBO = (IsGlGreaterEqual (3, 1) || CheckExtension ("GL_ARB_uniform_buffer_object"))
1832 && FindProcShort (glGetUniformIndices)
1833 && FindProcShort (glGetActiveUniformsiv)
1834 && FindProcShort (glGetActiveUniformName)
1835 && FindProcShort (glGetUniformBlockIndex)
1836 && FindProcShort (glGetActiveUniformBlockiv)
1837 && FindProcShort (glGetActiveUniformBlockName)
1838 && FindProcShort (glUniformBlockBinding);
1840 // load GL_ARB_copy_buffer (added to OpenGL 3.1 core)
1841 const bool hasCopyBufSubData = (IsGlGreaterEqual (3, 1) || CheckExtension ("GL_ARB_copy_buffer"))
1842 && FindProcShort (glCopyBufferSubData);
1846 // NPOT textures are required by OpenGL 2.0 specifications
1847 // but doesn't hardware accelerated by some ancient OpenGL 2.1 hardware (GeForce FX, RadeOn 9700 etc.)
1848 arbNPTW = Standard_True;
1849 arbTexRG = Standard_True;
1852 // load OpenGL 3.1 new functions
1853 has31 = IsGlGreaterEqual (3, 1)
1855 && hasCopyBufSubData
1856 && FindProcShort (glDrawArraysInstanced)
1857 && FindProcShort (glDrawElementsInstanced)
1858 && FindProcShort (glTexBuffer)
1859 && FindProcShort (glPrimitiveRestartIndex);
1861 // load GL_ARB_draw_elements_base_vertex (added to OpenGL 3.2 core)
1862 const bool hasDrawElemsBaseVert = (IsGlGreaterEqual (3, 2) || CheckExtension ("GL_ARB_draw_elements_base_vertex"))
1863 && FindProcShort (glDrawElementsBaseVertex)
1864 && FindProcShort (glDrawRangeElementsBaseVertex)
1865 && FindProcShort (glDrawElementsInstancedBaseVertex)
1866 && FindProcShort (glMultiDrawElementsBaseVertex);
1868 // load GL_ARB_provoking_vertex (added to OpenGL 3.2 core)
1869 const bool hasProvokingVert = (IsGlGreaterEqual (3, 2) || CheckExtension ("GL_ARB_provoking_vertex"))
1870 && FindProcShort (glProvokingVertex);
1872 // load GL_ARB_sync (added to OpenGL 3.2 core)
1873 const bool hasSync = (IsGlGreaterEqual (3, 2) || CheckExtension ("GL_ARB_sync"))
1874 && FindProcShort (glFenceSync)
1875 && FindProcShort (glIsSync)
1876 && FindProcShort (glDeleteSync)
1877 && FindProcShort (glClientWaitSync)
1878 && FindProcShort (glWaitSync)
1879 && FindProcShort (glGetInteger64v)
1880 && FindProcShort (glGetSynciv);
1882 // load GL_ARB_texture_multisample (added to OpenGL 3.2 core)
1883 const bool hasTextureMultisample = (IsGlGreaterEqual (3, 2) || CheckExtension ("GL_ARB_texture_multisample"))
1884 && FindProcShort (glTexImage2DMultisample)
1885 && FindProcShort (glTexImage3DMultisample)
1886 && FindProcShort (glGetMultisamplefv)
1887 && FindProcShort (glSampleMaski);
1889 // load OpenGL 3.2 new functions
1890 has32 = IsGlGreaterEqual (3, 2)
1891 && hasDrawElemsBaseVert
1894 && hasTextureMultisample
1895 && FindProcShort (glGetInteger64i_v)
1896 && FindProcShort (glGetBufferParameteri64v)
1897 && FindProcShort (glFramebufferTexture);
1899 // load GL_ARB_blend_func_extended (added to OpenGL 3.3 core)
1900 const bool hasBlendFuncExtended = (IsGlGreaterEqual (3, 3) || CheckExtension ("GL_ARB_blend_func_extended"))
1901 && FindProcShort (glBindFragDataLocationIndexed)
1902 && FindProcShort (glGetFragDataIndex);
1904 // load GL_ARB_sampler_objects (added to OpenGL 3.3 core)
1905 const bool hasSamplerObjects = (IsGlGreaterEqual (3, 3) || CheckExtension ("GL_ARB_sampler_objects"))
1906 && FindProcShort (glGenSamplers)
1907 && FindProcShort (glDeleteSamplers)
1908 && FindProcShort (glIsSampler)
1909 && FindProcShort (glBindSampler)
1910 && FindProcShort (glSamplerParameteri)
1911 && FindProcShort (glSamplerParameteriv)
1912 && FindProcShort (glSamplerParameterf)
1913 && FindProcShort (glSamplerParameterfv)
1914 && FindProcShort (glSamplerParameterIiv)
1915 && FindProcShort (glSamplerParameterIuiv)
1916 && FindProcShort (glGetSamplerParameteriv)
1917 && FindProcShort (glGetSamplerParameterIiv)
1918 && FindProcShort (glGetSamplerParameterfv)
1919 && FindProcShort (glGetSamplerParameterIuiv);
1921 // load GL_ARB_timer_query (added to OpenGL 3.3 core)
1922 const bool hasTimerQuery = (IsGlGreaterEqual (3, 3) || CheckExtension ("GL_ARB_timer_query"))
1923 && FindProcShort (glQueryCounter)
1924 && FindProcShort (glGetQueryObjecti64v)
1925 && FindProcShort (glGetQueryObjectui64v);
1927 // load GL_ARB_vertex_type_2_10_10_10_rev (added to OpenGL 3.3 core)
1928 const bool hasVertType21010101rev = (IsGlGreaterEqual (3, 3) || CheckExtension ("GL_ARB_vertex_type_2_10_10_10_rev"))
1929 && FindProcShort (glVertexAttribP1ui)
1930 && FindProcShort (glVertexAttribP1uiv)
1931 && FindProcShort (glVertexAttribP2ui)
1932 && FindProcShort (glVertexAttribP2uiv)
1933 && FindProcShort (glVertexAttribP3ui)
1934 && FindProcShort (glVertexAttribP3uiv)
1935 && FindProcShort (glVertexAttribP4ui)
1936 && FindProcShort (glVertexAttribP4uiv);
1938 if ( hasVertType21010101rev
1941 // load deprecated functions
1942 const bool hasVertType21010101revExt =
1943 FindProcShort (glVertexP2ui)
1944 && FindProcShort (glVertexP2uiv)
1945 && FindProcShort (glVertexP3ui)
1946 && FindProcShort (glVertexP3uiv)
1947 && FindProcShort (glVertexP4ui)
1948 && FindProcShort (glVertexP4uiv)
1949 && FindProcShort (glTexCoordP1ui)
1950 && FindProcShort (glTexCoordP1uiv)
1951 && FindProcShort (glTexCoordP2ui)
1952 && FindProcShort (glTexCoordP2uiv)
1953 && FindProcShort (glTexCoordP3ui)
1954 && FindProcShort (glTexCoordP3uiv)
1955 && FindProcShort (glTexCoordP4ui)
1956 && FindProcShort (glTexCoordP4uiv)
1957 && FindProcShort (glMultiTexCoordP1ui)
1958 && FindProcShort (glMultiTexCoordP1uiv)
1959 && FindProcShort (glMultiTexCoordP2ui)
1960 && FindProcShort (glMultiTexCoordP2uiv)
1961 && FindProcShort (glMultiTexCoordP3ui)
1962 && FindProcShort (glMultiTexCoordP3uiv)
1963 && FindProcShort (glMultiTexCoordP4ui)
1964 && FindProcShort (glMultiTexCoordP4uiv)
1965 && FindProcShort (glNormalP3ui)
1966 && FindProcShort (glNormalP3uiv)
1967 && FindProcShort (glColorP3ui)
1968 && FindProcShort (glColorP3uiv)
1969 && FindProcShort (glColorP4ui)
1970 && FindProcShort (glColorP4uiv)
1971 && FindProcShort (glSecondaryColorP3ui)
1972 && FindProcShort (glSecondaryColorP3uiv);
1973 (void )hasVertType21010101revExt;
1976 // load OpenGL 3.3 extra functions
1977 has33 = IsGlGreaterEqual (3, 3)
1978 && hasBlendFuncExtended
1979 && hasSamplerObjects
1981 && hasVertType21010101rev
1982 && FindProcShort (glVertexAttribDivisor);
1984 // load GL_ARB_draw_indirect (added to OpenGL 4.0 core)
1985 const bool hasDrawIndirect = (IsGlGreaterEqual (4, 0) || CheckExtension ("GL_ARB_draw_indirect"))
1986 && FindProcShort (glDrawArraysIndirect)
1987 && FindProcShort (glDrawElementsIndirect);
1989 // load GL_ARB_gpu_shader_fp64 (added to OpenGL 4.0 core)
1990 const bool hasShaderFP64 = (IsGlGreaterEqual (4, 0) || CheckExtension ("GL_ARB_gpu_shader_fp64"))
1991 && FindProcShort (glUniform1d)
1992 && FindProcShort (glUniform2d)
1993 && FindProcShort (glUniform3d)
1994 && FindProcShort (glUniform4d)
1995 && FindProcShort (glUniform1dv)
1996 && FindProcShort (glUniform2dv)
1997 && FindProcShort (glUniform3dv)
1998 && FindProcShort (glUniform4dv)
1999 && FindProcShort (glUniformMatrix2dv)
2000 && FindProcShort (glUniformMatrix3dv)
2001 && FindProcShort (glUniformMatrix4dv)
2002 && FindProcShort (glUniformMatrix2x3dv)
2003 && FindProcShort (glUniformMatrix2x4dv)
2004 && FindProcShort (glUniformMatrix3x2dv)
2005 && FindProcShort (glUniformMatrix3x4dv)
2006 && FindProcShort (glUniformMatrix4x2dv)
2007 && FindProcShort (glUniformMatrix4x3dv)
2008 && FindProcShort (glGetUniformdv);
2010 // load GL_ARB_shader_subroutine (added to OpenGL 4.0 core)
2011 const bool hasShaderSubroutine = (IsGlGreaterEqual (4, 0) || CheckExtension ("GL_ARB_shader_subroutine"))
2012 && FindProcShort (glGetSubroutineUniformLocation)
2013 && FindProcShort (glGetSubroutineIndex)
2014 && FindProcShort (glGetActiveSubroutineUniformiv)
2015 && FindProcShort (glGetActiveSubroutineUniformName)
2016 && FindProcShort (glGetActiveSubroutineName)
2017 && FindProcShort (glUniformSubroutinesuiv)
2018 && FindProcShort (glGetUniformSubroutineuiv)
2019 && FindProcShort (glGetProgramStageiv);
2021 // load GL_ARB_tessellation_shader (added to OpenGL 4.0 core)
2022 const bool hasTessellationShader = (IsGlGreaterEqual (4, 0) || CheckExtension ("GL_ARB_tessellation_shader"))
2023 && FindProcShort (glPatchParameteri)
2024 && FindProcShort (glPatchParameterfv);
2026 // load GL_ARB_transform_feedback2 (added to OpenGL 4.0 core)
2027 const bool hasTrsfFeedback2 = (IsGlGreaterEqual (4, 0) || CheckExtension ("GL_ARB_transform_feedback2"))
2028 && FindProcShort (glBindTransformFeedback)
2029 && FindProcShort (glDeleteTransformFeedbacks)
2030 && FindProcShort (glGenTransformFeedbacks)
2031 && FindProcShort (glIsTransformFeedback)
2032 && FindProcShort (glPauseTransformFeedback)
2033 && FindProcShort (glResumeTransformFeedback)
2034 && FindProcShort (glDrawTransformFeedback);
2036 // load GL_ARB_transform_feedback3 (added to OpenGL 4.0 core)
2037 const bool hasTrsfFeedback3 = (IsGlGreaterEqual (4, 0) || CheckExtension ("GL_ARB_transform_feedback3"))
2038 && FindProcShort (glDrawTransformFeedbackStream)
2039 && FindProcShort (glBeginQueryIndexed)
2040 && FindProcShort (glEndQueryIndexed)
2041 && FindProcShort (glGetQueryIndexediv);
2043 // load OpenGL 4.0 new functions
2044 has40 = IsGlGreaterEqual (4, 0)
2047 && hasShaderSubroutine
2048 && hasTessellationShader
2051 && FindProcShort (glMinSampleShading)
2052 && FindProcShort (glBlendEquationi)
2053 && FindProcShort (glBlendEquationSeparatei)
2054 && FindProcShort (glBlendFunci)
2055 && FindProcShort (glBlendFuncSeparatei);
2057 // load GL_ARB_ES2_compatibility (added to OpenGL 4.1 core)
2058 const bool hasES2Compatibility = (IsGlGreaterEqual (4, 1) || CheckExtension ("GL_ARB_ES2_compatibility"))
2059 && FindProcShort (glReleaseShaderCompiler)
2060 && FindProcShort (glShaderBinary)
2061 && FindProcShort (glGetShaderPrecisionFormat)
2062 && FindProcShort (glDepthRangef)
2063 && FindProcShort (glClearDepthf);
2065 // load GL_ARB_get_program_binary (added to OpenGL 4.1 core)
2066 const bool hasGetProgramBinary = (IsGlGreaterEqual (4, 1) || CheckExtension ("GL_ARB_get_program_binary"))
2067 && FindProcShort (glGetProgramBinary)
2068 && FindProcShort (glProgramBinary)
2069 && FindProcShort (glProgramParameteri);
2072 // load GL_ARB_separate_shader_objects (added to OpenGL 4.1 core)
2073 const bool hasSeparateShaderObjects = (IsGlGreaterEqual (4, 1) || CheckExtension ("GL_ARB_separate_shader_objects"))
2074 && FindProcShort (glUseProgramStages)
2075 && FindProcShort (glActiveShaderProgram)
2076 && FindProcShort (glCreateShaderProgramv)
2077 && FindProcShort (glBindProgramPipeline)
2078 && FindProcShort (glDeleteProgramPipelines)
2079 && FindProcShort (glGenProgramPipelines)
2080 && FindProcShort (glIsProgramPipeline)
2081 && FindProcShort (glGetProgramPipelineiv)
2082 && FindProcShort (glProgramUniform1i)
2083 && FindProcShort (glProgramUniform1iv)
2084 && FindProcShort (glProgramUniform1f)
2085 && FindProcShort (glProgramUniform1fv)
2086 && FindProcShort (glProgramUniform1d)
2087 && FindProcShort (glProgramUniform1dv)
2088 && FindProcShort (glProgramUniform1ui)
2089 && FindProcShort (glProgramUniform1uiv)
2090 && FindProcShort (glProgramUniform2i)
2091 && FindProcShort (glProgramUniform2iv)
2092 && FindProcShort (glProgramUniform2f)
2093 && FindProcShort (glProgramUniform2fv)
2094 && FindProcShort (glProgramUniform2d)
2095 && FindProcShort (glProgramUniform2dv)
2096 && FindProcShort (glProgramUniform2ui)
2097 && FindProcShort (glProgramUniform2uiv)
2098 && FindProcShort (glProgramUniform3i)
2099 && FindProcShort (glProgramUniform3iv)
2100 && FindProcShort (glProgramUniform3f)
2101 && FindProcShort (glProgramUniform3fv)
2102 && FindProcShort (glProgramUniform3d)
2103 && FindProcShort (glProgramUniform3dv)
2104 && FindProcShort (glProgramUniform3ui)
2105 && FindProcShort (glProgramUniform3uiv)
2106 && FindProcShort (glProgramUniform4i)
2107 && FindProcShort (glProgramUniform4iv)
2108 && FindProcShort (glProgramUniform4f)
2109 && FindProcShort (glProgramUniform4fv)
2110 && FindProcShort (glProgramUniform4d)
2111 && FindProcShort (glProgramUniform4dv)
2112 && FindProcShort (glProgramUniform4ui)
2113 && FindProcShort (glProgramUniform4uiv)
2114 && FindProcShort (glProgramUniformMatrix2fv)
2115 && FindProcShort (glProgramUniformMatrix3fv)
2116 && FindProcShort (glProgramUniformMatrix4fv)
2117 && FindProcShort (glProgramUniformMatrix2dv)
2118 && FindProcShort (glProgramUniformMatrix3dv)
2119 && FindProcShort (glProgramUniformMatrix4dv)
2120 && FindProcShort (glProgramUniformMatrix2x3fv)
2121 && FindProcShort (glProgramUniformMatrix3x2fv)
2122 && FindProcShort (glProgramUniformMatrix2x4fv)
2123 && FindProcShort (glProgramUniformMatrix4x2fv)
2124 && FindProcShort (glProgramUniformMatrix3x4fv)
2125 && FindProcShort (glProgramUniformMatrix4x3fv)
2126 && FindProcShort (glProgramUniformMatrix2x3dv)
2127 && FindProcShort (glProgramUniformMatrix3x2dv)
2128 && FindProcShort (glProgramUniformMatrix2x4dv)
2129 && FindProcShort (glProgramUniformMatrix4x2dv)
2130 && FindProcShort (glProgramUniformMatrix3x4dv)
2131 && FindProcShort (glProgramUniformMatrix4x3dv)
2132 && FindProcShort (glValidateProgramPipeline)
2133 && FindProcShort (glGetProgramPipelineInfoLog);
2135 // load GL_ARB_vertex_attrib_64bit (added to OpenGL 4.1 core)
2136 const bool hasVertAttrib64bit = (IsGlGreaterEqual (4, 1) || CheckExtension ("GL_ARB_vertex_attrib_64bit"))
2137 && FindProcShort (glVertexAttribL1d)
2138 && FindProcShort (glVertexAttribL2d)
2139 && FindProcShort (glVertexAttribL3d)
2140 && FindProcShort (glVertexAttribL4d)
2141 && FindProcShort (glVertexAttribL1dv)
2142 && FindProcShort (glVertexAttribL2dv)
2143 && FindProcShort (glVertexAttribL3dv)
2144 && FindProcShort (glVertexAttribL4dv)
2145 && FindProcShort (glVertexAttribLPointer)
2146 && FindProcShort (glGetVertexAttribLdv);
2148 // load GL_ARB_viewport_array (added to OpenGL 4.1 core)
2149 const bool hasViewportArray = (IsGlGreaterEqual (4, 1) || CheckExtension ("GL_ARB_viewport_array"))
2150 && FindProcShort (glViewportArrayv)
2151 && FindProcShort (glViewportIndexedf)
2152 && FindProcShort (glViewportIndexedfv)
2153 && FindProcShort (glScissorArrayv)
2154 && FindProcShort (glScissorIndexed)
2155 && FindProcShort (glScissorIndexedv)
2156 && FindProcShort (glDepthRangeArrayv)
2157 && FindProcShort (glDepthRangeIndexed)
2158 && FindProcShort (glGetFloati_v)
2159 && FindProcShort (glGetDoublei_v);
2161 has41 = IsGlGreaterEqual (4, 1)
2162 && hasES2Compatibility
2163 && hasGetProgramBinary
2164 && hasSeparateShaderObjects
2165 && hasVertAttrib64bit
2166 && hasViewportArray;
2168 // load GL_ARB_base_instance (added to OpenGL 4.2 core)
2169 const bool hasBaseInstance = (IsGlGreaterEqual (4, 2) || CheckExtension ("GL_ARB_base_instance"))
2170 && FindProcShort (glDrawArraysInstancedBaseInstance)
2171 && FindProcShort (glDrawElementsInstancedBaseInstance)
2172 && FindProcShort (glDrawElementsInstancedBaseVertexBaseInstance);
2174 // load GL_ARB_transform_feedback_instanced (added to OpenGL 4.2 core)
2175 const bool hasTrsfFeedbackInstanced = (IsGlGreaterEqual (4, 2) || CheckExtension ("GL_ARB_transform_feedback_instanced"))
2176 && FindProcShort (glDrawTransformFeedbackInstanced)
2177 && FindProcShort (glDrawTransformFeedbackStreamInstanced);
2179 // load GL_ARB_internalformat_query (added to OpenGL 4.2 core)
2180 const bool hasInternalFormatQuery = (IsGlGreaterEqual (4, 2) || CheckExtension ("GL_ARB_internalformat_query"))
2181 && FindProcShort (glGetInternalformativ);
2183 // load GL_ARB_shader_atomic_counters (added to OpenGL 4.2 core)
2184 const bool hasShaderAtomicCounters = (IsGlGreaterEqual (4, 2) || CheckExtension ("GL_ARB_shader_atomic_counters"))
2185 && FindProcShort (glGetActiveAtomicCounterBufferiv);
2187 // load GL_ARB_shader_image_load_store (added to OpenGL 4.2 core)
2188 const bool hasShaderImgLoadStore = (IsGlGreaterEqual (4, 2) || CheckExtension ("GL_ARB_shader_image_load_store"))
2189 && FindProcShort (glBindImageTexture)
2190 && FindProcShort (glMemoryBarrier);
2192 // load GL_ARB_texture_storage (added to OpenGL 4.2 core)
2193 const bool hasTextureStorage = (IsGlGreaterEqual (4, 2) || CheckExtension ("GL_ARB_texture_storage"))
2194 && FindProcShort (glTexStorage1D)
2195 && FindProcShort (glTexStorage2D)
2196 && FindProcShort (glTexStorage3D);
2198 has42 = IsGlGreaterEqual (4, 2)
2200 && hasTrsfFeedbackInstanced
2201 && hasInternalFormatQuery
2202 && hasShaderAtomicCounters
2203 && hasShaderImgLoadStore
2204 && hasTextureStorage;
2206 has43 = IsGlGreaterEqual (4, 3)
2207 && FindProcShort (glClearBufferData)
2208 && FindProcShort (glClearBufferSubData)
2209 && FindProcShort (glDispatchCompute)
2210 && FindProcShort (glDispatchComputeIndirect)
2211 && FindProcShort (glCopyImageSubData)
2212 && FindProcShort (glFramebufferParameteri)
2213 && FindProcShort (glGetFramebufferParameteriv)
2214 && FindProcShort (glGetInternalformati64v)
2215 && FindProcShort (glInvalidateTexSubImage)
2216 && FindProcShort (glInvalidateTexImage)
2217 && FindProcShort (glInvalidateBufferSubData)
2218 && FindProcShort (glInvalidateBufferData)
2219 && FindProcShort (glInvalidateFramebuffer)
2220 && FindProcShort (glInvalidateSubFramebuffer)
2221 && FindProcShort (glMultiDrawArraysIndirect)
2222 && FindProcShort (glMultiDrawElementsIndirect)
2223 && FindProcShort (glGetProgramInterfaceiv)
2224 && FindProcShort (glGetProgramResourceIndex)
2225 && FindProcShort (glGetProgramResourceName)
2226 && FindProcShort (glGetProgramResourceiv)
2227 && FindProcShort (glGetProgramResourceLocation)
2228 && FindProcShort (glGetProgramResourceLocationIndex)
2229 && FindProcShort (glShaderStorageBlockBinding)
2230 && FindProcShort (glTexBufferRange)
2231 && FindProcShort (glTexStorage2DMultisample)
2232 && FindProcShort (glTexStorage3DMultisample)
2233 && FindProcShort (glTextureView)
2234 && FindProcShort (glBindVertexBuffer)
2235 && FindProcShort (glVertexAttribFormat)
2236 && FindProcShort (glVertexAttribIFormat)
2237 && FindProcShort (glVertexAttribLFormat)
2238 && FindProcShort (glVertexAttribBinding)
2239 && FindProcShort (glVertexBindingDivisor)
2240 && FindProcShort (glDebugMessageControl)
2241 && FindProcShort (glDebugMessageInsert)
2242 && FindProcShort (glDebugMessageCallback)
2243 && FindProcShort (glGetDebugMessageLog)
2244 && FindProcShort (glPushDebugGroup)
2245 && FindProcShort (glPopDebugGroup)
2246 && FindProcShort (glObjectLabel)
2247 && FindProcShort (glGetObjectLabel)
2248 && FindProcShort (glObjectPtrLabel)
2249 && FindProcShort (glGetObjectPtrLabel);
2251 // load GL_ARB_clear_texture (added to OpenGL 4.4 core)
2252 bool arbTexClear = (IsGlGreaterEqual (4, 4) || CheckExtension ("GL_ARB_clear_texture"))
2253 && FindProcShort (glClearTexImage)
2254 && FindProcShort (glClearTexSubImage);
2256 has44 = IsGlGreaterEqual (4, 4)
2258 && FindProcShort (glBufferStorage)
2259 && FindProcShort (glBindBuffersBase)
2260 && FindProcShort (glBindBuffersRange)
2261 && FindProcShort (glBindTextures)
2262 && FindProcShort (glBindSamplers)
2263 && FindProcShort (glBindImageTextures)
2264 && FindProcShort (glBindVertexBuffers);
2266 // initialize debug context extension
2267 if (CheckExtension ("GL_ARB_debug_output"))
2272 arbDbg = (OpenGl_ArbDbg* )(&(*myFuncs));
2274 else if (FindProc ("glDebugMessageControlARB", myFuncs->glDebugMessageControl)
2275 && FindProc ("glDebugMessageInsertARB", myFuncs->glDebugMessageInsert)
2276 && FindProc ("glDebugMessageCallbackARB", myFuncs->glDebugMessageCallback)
2277 && FindProc ("glGetDebugMessageLogARB", myFuncs->glGetDebugMessageLog))
2279 arbDbg = (OpenGl_ArbDbg* )(&(*myFuncs));
2283 && caps->contextDebug)
2285 // setup default callback
2286 myIsGlDebugCtx = Standard_True;
2287 arbDbg->glDebugMessageCallback (debugCallbackWrap, this);
2290 ::glEnable (GL_DEBUG_OUTPUT);
2292 if (caps->contextSyncDebug)
2294 ::glEnable (GL_DEBUG_OUTPUT_SYNCHRONOUS);
2299 // initialize TBO extension (ARB)
2301 && CheckExtension ("GL_ARB_texture_buffer_object")
2302 && FindProc ("glTexBufferARB", myFuncs->glTexBuffer))
2304 arbTBO = (OpenGl_ArbTBO* )(&(*myFuncs));
2306 arbTboRGB32 = CheckExtension ("GL_ARB_texture_buffer_object_rgb32");
2308 // initialize hardware instancing extension (ARB)
2310 && CheckExtension ("GL_ARB_draw_instanced")
2311 && FindProc ("glDrawArraysInstancedARB", myFuncs->glDrawArraysInstanced)
2312 && FindProc ("glDrawElementsInstancedARB", myFuncs->glDrawElementsInstanced))
2314 arbIns = (OpenGl_ArbIns* )(&(*myFuncs));
2317 // initialize FBO extension (ARB)
2320 arbFBO = (OpenGl_ArbFBO* )(&(*myFuncs));
2321 arbFBOBlit = (OpenGl_ArbFBOBlit* )(&(*myFuncs));
2322 extPDS = Standard_True; // extension for EXT, but part of ARB
2325 // initialize GS extension (EXT)
2326 if (CheckExtension ("GL_EXT_geometry_shader4")
2327 && FindProcShort (glProgramParameteriEXT))
2329 extGS = (OpenGl_ExtGS* )(&(*myFuncs));
2332 // initialize bindless texture extension (ARB)
2333 if (CheckExtension ("GL_ARB_bindless_texture")
2334 && FindProcShort (glGetTextureHandleARB)
2335 && FindProcShort (glGetTextureSamplerHandleARB)
2336 && FindProcShort (glMakeTextureHandleResidentARB)
2337 && FindProcShort (glMakeTextureHandleNonResidentARB)
2338 && FindProcShort (glGetImageHandleARB)
2339 && FindProcShort (glMakeImageHandleResidentARB)
2340 && FindProcShort (glMakeImageHandleNonResidentARB)
2341 && FindProcShort (glUniformHandleui64ARB)
2342 && FindProcShort (glUniformHandleui64vARB)
2343 && FindProcShort (glProgramUniformHandleui64ARB)
2344 && FindProcShort (glProgramUniformHandleui64vARB)
2345 && FindProcShort (glIsTextureHandleResidentARB)
2346 && FindProcShort (glIsImageHandleResidentARB)
2347 && FindProcShort (glVertexAttribL1ui64ARB)
2348 && FindProcShort (glVertexAttribL1ui64vARB)
2349 && FindProcShort (glGetVertexAttribLui64vARB))
2351 arbTexBindless = (OpenGl_ArbTexBindless* )(&(*myFuncs));
2356 checkWrongVersion (1, 2);
2363 checkWrongVersion (1, 3);
2370 checkWrongVersion (1, 4);
2377 checkWrongVersion (1, 5);
2384 core15 = (OpenGl_GlCore15* )(&(*myFuncs));
2386 core15fwd = (OpenGl_GlCore15Fwd* )(&(*myFuncs));
2390 checkWrongVersion (2, 0);
2396 const char* aGlslVer = (const char* )::glGetString (GL_SHADING_LANGUAGE_VERSION);
2397 if (aGlslVer == NULL
2398 || *aGlslVer == '\0')
2400 // broken context has been detected
2401 TCollection_ExtendedString aMsg = TCollection_ExtendedString()
2402 + "Error! OpenGL context reports version "
2403 + myGlVerMajor + "." + myGlVerMinor
2404 + " but reports wrong GLSL version";
2405 PushMessage (GL_DEBUG_SOURCE_APPLICATION,
2406 GL_DEBUG_TYPE_ERROR,
2408 GL_DEBUG_SEVERITY_HIGH,
2417 core20 = (OpenGl_GlCore20* )(&(*myFuncs));
2419 core20fwd = (OpenGl_GlCore20Fwd* )(&(*myFuncs));
2423 checkWrongVersion (2, 1);
2431 checkWrongVersion (3, 0);
2437 // MSAA RenderBuffers have been defined in OpenGL 3.0,
2438 // but MSAA Textures - only in OpenGL 3.2+
2440 && CheckExtension ("GL_ARB_texture_multisample")
2441 && FindProcShort (glTexImage2DMultisample))
2443 GLint aNbColorSamples = 0, aNbDepthSamples = 0;
2444 ::glGetIntegerv (GL_MAX_COLOR_TEXTURE_SAMPLES, &aNbColorSamples);
2445 ::glGetIntegerv (GL_MAX_DEPTH_TEXTURE_SAMPLES, &aNbDepthSamples);
2446 myMaxMsaaSamples = Min (aNbColorSamples, aNbDepthSamples);
2449 && CheckExtension ("GL_ARB_texture_storage_multisample")
2450 && FindProcShort (glTexStorage2DMultisample))
2457 checkWrongVersion (3, 1);
2462 arbTBO = (OpenGl_ArbTBO* )(&(*myFuncs));
2463 arbIns = (OpenGl_ArbIns* )(&(*myFuncs));
2465 // check whether ray tracing mode is supported
2466 myHasRayTracing = has31
2468 && arbFBOBlit != NULL;
2470 // check whether textures in ray tracing mode are supported
2471 myHasRayTracingTextures = myHasRayTracing
2472 && arbTexBindless != NULL;
2474 // check whether adaptive screen sampling in ray tracing mode is supported
2475 myHasRayTracingAdaptiveSampling = myHasRayTracing
2477 && CheckExtension ("GL_NV_shader_atomic_float");
2481 checkWrongVersion (3, 2);
2486 core32 = (OpenGl_GlCore32* )(&(*myFuncs));
2489 core32->glGenVertexArrays (1, &myDefaultVao);
2493 core32back = (OpenGl_GlCore32Back* )(&(*myFuncs));
2495 ::glGetIntegerv (GL_MAX_SAMPLES, &myMaxMsaaSamples);
2499 checkWrongVersion (3, 3);
2504 core33 = (OpenGl_GlCore33* )(&(*myFuncs));
2507 core33back = (OpenGl_GlCore33Back* )(&(*myFuncs));
2510 // initialize sampler object
2511 myTexSampler = new OpenGl_Sampler();
2512 myTexSampler->Init (*this);
2516 checkWrongVersion (4, 0);
2521 arbTboRGB32 = Standard_True; // in core since OpenGL 4.0
2525 checkWrongVersion (4, 1);
2530 core41 = (OpenGl_GlCore41* )(&(*myFuncs));
2533 core41back = (OpenGl_GlCore41Back* )(&(*myFuncs));
2538 checkWrongVersion (4, 2);
2543 core42 = (OpenGl_GlCore42* )(&(*myFuncs));
2546 core42back = (OpenGl_GlCore42Back* )(&(*myFuncs));
2551 checkWrongVersion (4, 3);
2556 core43 = (OpenGl_GlCore43* )(&(*myFuncs));
2559 core43back = (OpenGl_GlCore43Back* )(&(*myFuncs));
2564 checkWrongVersion (4, 4);
2569 core44 = (OpenGl_GlCore44* )(&(*myFuncs));
2572 core44back = (OpenGl_GlCore44Back* )(&(*myFuncs));
2577 // =======================================================================
2578 // function : MemoryInfo
2580 // =======================================================================
2581 Standard_Size OpenGl_Context::AvailableMemory() const
2583 #if !defined(GL_ES_VERSION_2_0)
2586 // this is actually information for VBO pool
2587 // however because pools are mostly shared
2588 // it can be used for total GPU memory estimations
2591 glGetIntegerv (GL_VBO_FREE_MEMORY_ATI, aMemInfo);
2592 // returned value is in KiB, however this maybe changed in future
2593 return Standard_Size(aMemInfo[0]) * 1024;
2597 // current available dedicated video memory (in KiB), currently unused GPU memory
2599 glGetIntegerv (GL_GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX, &aMemInfo);
2600 return Standard_Size(aMemInfo) * 1024;
2606 // =======================================================================
2607 // function : MemoryInfo
2609 // =======================================================================
2610 TCollection_AsciiString OpenGl_Context::MemoryInfo() const
2612 TColStd_IndexedDataMapOfStringString aDict;
2615 TCollection_AsciiString aText;
2616 for (TColStd_IndexedDataMapOfStringString::Iterator anIter (aDict); anIter.More(); anIter.Next())
2618 if (!aText.IsEmpty())
2622 aText += TCollection_AsciiString(" ") + anIter.Key() + ": " + anIter.Value();
2627 // =======================================================================
2628 // function : MemoryInfo
2630 // =======================================================================
2631 void OpenGl_Context::MemoryInfo (TColStd_IndexedDataMapOfStringString& theDict) const
2633 #if defined(GL_ES_VERSION_2_0)
2635 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
2636 GLint aGlRendId = 0;
2637 CGLGetParameter (CGLGetCurrentContext(), kCGLCPCurrentRendererID, &aGlRendId);
2639 CGLRendererInfoObj aRendObj = NULL;
2640 CGOpenGLDisplayMask aDispMask = CGDisplayIDToOpenGLDisplayMask (kCGDirectMainDisplay);
2642 CGLQueryRendererInfo (aDispMask, &aRendObj, &aRendNb);
2643 for (GLint aRendIter = 0; aRendIter < aRendNb; ++aRendIter)
2646 if (CGLDescribeRenderer (aRendObj, aRendIter, kCGLRPRendererID, &aRendId) != kCGLNoError
2647 || aRendId != aGlRendId)
2652 //kCGLRPVideoMemoryMegabytes = 131;
2653 //kCGLRPTextureMemoryMegabytes = 132;
2655 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
2656 if (CGLDescribeRenderer(aRendObj, aRendIter, kCGLRPVideoMemoryMegabytes, &aVMem) == kCGLNoError)
2658 addInfo (theDict, "GPU memory", TCollection_AsciiString() + aVMem + " MiB");
2660 if (CGLDescribeRenderer(aRendObj, aRendIter, kCGLRPTextureMemoryMegabytes, &aVMem) == kCGLNoError)
2662 addInfo (theDict, "GPU Texture memory", TCollection_AsciiString() + aVMem + " MiB");
2665 if (CGLDescribeRenderer(aRendObj, aRendIter, kCGLRPVideoMemory, &aVMem) == kCGLNoError)
2667 addInfo (theDict, "GPU memory", TCollection_AsciiString() + (aVMem / (1024 * 1024)) + " MiB");
2669 if (CGLDescribeRenderer(aRendObj, aRendIter, kCGLRPTextureMemory, &aVMem) == kCGLNoError)
2671 addInfo (theDict, "GPU Texture memory", TCollection_AsciiString() + (aVMem / (1024 * 1024)) + " MiB");
2677 #if !defined(GL_ES_VERSION_2_0)
2681 memset (aValues, 0, sizeof(aValues));
2682 glGetIntegerv (GL_VBO_FREE_MEMORY_ATI, aValues);
2684 // total memory free in the pool
2685 addInfo (theDict, "GPU free memory", TCollection_AsciiString() + (aValues[0] / 1024) + " MiB");
2687 if (aValues[1] != aValues[0])
2689 // largest available free block in the pool
2690 addInfo (theDict, "Largest free block", TCollection_AsciiString() + (aValues[1] / 1024) + " MiB");
2692 if (aValues[2] != aValues[0])
2694 // total auxiliary memory free
2695 addInfo (theDict, "Free auxiliary memory", TCollection_AsciiString() + (aValues[2] / 1024) + " MiB");
2700 //current available dedicated video memory (in KiB), currently unused GPU memory
2702 glGetIntegerv (GL_GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX, &aValue);
2703 addInfo (theDict, "GPU free memory", TCollection_AsciiString() + (aValue / 1024) + " MiB");
2705 // dedicated video memory, total size (in KiB) of the GPU memory
2706 GLint aDedicated = 0;
2707 glGetIntegerv (GL_GPU_MEMORY_INFO_DEDICATED_VIDMEM_NVX, &aDedicated);
2708 addInfo (theDict, "GPU memory", TCollection_AsciiString() + (aDedicated / 1024) + " MiB");
2710 // total available memory, total size (in KiB) of the memory available for allocations
2711 glGetIntegerv (GL_GPU_MEMORY_INFO_TOTAL_AVAILABLE_MEMORY_NVX, &aValue);
2712 if (aValue != aDedicated)
2714 // different only for special configurations
2715 addInfo (theDict, "Total memory", TCollection_AsciiString() + (aValue / 1024) + " MiB");
2719 else if (myFuncs->wglGetGPUInfoAMD != NULL
2720 && myFuncs->wglGetContextGPUIDAMD != NULL)
2722 GLuint aTotalMemMiB = 0;
2723 UINT anAmdId = myFuncs->wglGetContextGPUIDAMD ((HGLRC )myGContext);
2726 if (myFuncs->wglGetGPUInfoAMD (anAmdId, WGL_GPU_RAM_AMD, GL_UNSIGNED_INT, sizeof(aTotalMemMiB), &aTotalMemMiB) > 0)
2728 addInfo (theDict, "GPU memory", TCollection_AsciiString() + (int )aTotalMemMiB + " MiB");
2735 #if !defined(GL_ES_VERSION_2_0) && !defined(__APPLE__) && !defined(_WIN32)
2736 // GLX_RENDERER_VENDOR_ID_MESA
2737 if (myFuncs->glXQueryCurrentRendererIntegerMESA != NULL)
2739 unsigned int aVMemMiB = 0;
2740 if (myFuncs->glXQueryCurrentRendererIntegerMESA (GLX_RENDERER_VIDEO_MEMORY_MESA, &aVMemMiB) != False)
2742 addInfo (theDict, "GPU memory", TCollection_AsciiString() + int(aVMemMiB) + " MiB");
2748 // =======================================================================
2749 // function : DiagnosticInfo
2751 // =======================================================================
2752 void OpenGl_Context::DiagnosticInformation (TColStd_IndexedDataMapOfStringString& theDict,
2753 Graphic3d_DiagnosticInfo theFlags) const
2755 if ((theFlags & Graphic3d_DiagnosticInfo_NativePlatform) != 0)
2757 #if defined(HAVE_EGL)
2758 addInfo (theDict, "EGLVersion", ::eglQueryString ((Aspect_Display)myDisplay, EGL_VERSION));
2759 addInfo (theDict, "EGLVendor", ::eglQueryString ((Aspect_Display)myDisplay, EGL_VENDOR));
2760 addInfo (theDict, "EGLClientAPIs", ::eglQueryString ((Aspect_Display)myDisplay, EGL_CLIENT_APIS));
2761 if ((theFlags & Graphic3d_DiagnosticInfo_Extensions) != 0)
2763 addInfo (theDict, "EGLExtensions", ::eglQueryString ((Aspect_Display)myDisplay, EGL_EXTENSIONS));
2765 #elif defined(_WIN32)
2766 if ((theFlags & Graphic3d_DiagnosticInfo_Extensions) != 0
2767 && myFuncs->wglGetExtensionsStringARB != NULL)
2769 const char* aWglExts = myFuncs->wglGetExtensionsStringARB ((HDC )myWindowDC);
2770 addInfo (theDict, "WGLExtensions", aWglExts);
2772 #elif defined(__APPLE__)
2775 Display* aDisplay = (Display*)myDisplay;
2776 const int aScreen = DefaultScreen(aDisplay);
2777 addInfo (theDict, "GLXDirectRendering", ::glXIsDirect (aDisplay, (GLXContext )myGContext) ? "Yes" : "No");
2778 addInfo (theDict, "GLXVendor", ::glXQueryServerString (aDisplay, aScreen, GLX_VENDOR));
2779 addInfo (theDict, "GLXVersion", ::glXQueryServerString (aDisplay, aScreen, GLX_VERSION));
2780 if ((theFlags & Graphic3d_DiagnosticInfo_Extensions) != 0)
2782 const char* aGlxExts = ::glXQueryExtensionsString (aDisplay, aScreen);
2783 addInfo(theDict, "GLXExtensions", aGlxExts);
2786 addInfo (theDict, "GLXClientVendor", ::glXGetClientString (aDisplay, GLX_VENDOR));
2787 addInfo (theDict, "GLXClientVersion", ::glXGetClientString (aDisplay, GLX_VERSION));
2788 if ((theFlags & Graphic3d_DiagnosticInfo_Extensions) != 0)
2790 addInfo (theDict, "GLXClientExtensions", ::glXGetClientString (aDisplay, GLX_EXTENSIONS));
2795 if ((theFlags & Graphic3d_DiagnosticInfo_Device) != 0)
2797 addInfo (theDict, "GLvendor", (const char*)::glGetString (GL_VENDOR));
2798 addInfo (theDict, "GLdevice", (const char*)::glGetString (GL_RENDERER));
2799 addInfo (theDict, "GLversion", (const char*)::glGetString (GL_VERSION));
2800 addInfo (theDict, "GLSLversion", (const char*)::glGetString (GL_SHADING_LANGUAGE_VERSION));
2803 addInfo (theDict, "GLdebug", "ON");
2807 if ((theFlags & Graphic3d_DiagnosticInfo_Limits) != 0)
2809 addInfo (theDict, "Max texture size", TCollection_AsciiString(myMaxTexDim));
2810 addInfo (theDict, "Max MSAA samples", TCollection_AsciiString(myMaxMsaaSamples));
2813 if ((theFlags & Graphic3d_DiagnosticInfo_FrameBuffer) != 0)
2815 GLint aViewport[4] = {};
2816 ::glGetIntegerv (GL_VIEWPORT, aViewport);
2817 addInfo (theDict, "Viewport", TCollection_AsciiString() + aViewport[2] + "x" + aViewport[3]);
2820 if ((theFlags & Graphic3d_DiagnosticInfo_Memory) != 0)
2822 MemoryInfo (theDict);
2825 if ((theFlags & Graphic3d_DiagnosticInfo_Extensions) != 0)
2827 #if !defined(GL_ES_VERSION_2_0)
2828 if (IsGlGreaterEqual (3, 0)
2829 && myFuncs->glGetStringi != NULL)
2831 TCollection_AsciiString anExtList;
2833 ::glGetIntegerv (GL_NUM_EXTENSIONS, &anExtNb);
2834 for (GLint anIter = 0; anIter < anExtNb; ++anIter)
2836 const char* anExtension = (const char*)myFuncs->glGetStringi (GL_EXTENSIONS, (GLuint)anIter);
2837 if (!anExtList.IsEmpty())
2841 anExtList += anExtension;
2843 addInfo(theDict, "GLextensions", anExtList);
2848 addInfo (theDict, "GLextensions", (const char*)::glGetString (GL_EXTENSIONS));
2853 // =======================================================================
2854 // function : GetResource
2856 // =======================================================================
2857 const Handle(OpenGl_Resource)& OpenGl_Context::GetResource (const TCollection_AsciiString& theKey) const
2859 return mySharedResources->IsBound (theKey) ? mySharedResources->Find (theKey) : NULL_GL_RESOURCE;
2862 // =======================================================================
2863 // function : ShareResource
2865 // =======================================================================
2866 Standard_Boolean OpenGl_Context::ShareResource (const TCollection_AsciiString& theKey,
2867 const Handle(OpenGl_Resource)& theResource)
2869 if (theKey.IsEmpty() || theResource.IsNull())
2871 return Standard_False;
2873 return mySharedResources->Bind (theKey, theResource);
2876 // =======================================================================
2877 // function : ReleaseResource
2879 // =======================================================================
2880 void OpenGl_Context::ReleaseResource (const TCollection_AsciiString& theKey,
2881 const Standard_Boolean theToDelay)
2883 if (!mySharedResources->IsBound (theKey))
2887 auto& aRes = mySharedResources->Find (theKey);
2888 if (aRes->GetRefCount() > 1)
2895 myDelayed->Bind (theKey, 1);
2899 aRes->Release (this);
2900 mySharedResources->UnBind (theKey);
2904 // =======================================================================
2905 // function : ReleaseDelayed
2907 // =======================================================================
2908 void OpenGl_Context::ReleaseDelayed()
2910 // release queued elements
2911 while (!myUnusedResources->IsEmpty())
2913 myUnusedResources->First()->Release (this);
2914 myUnusedResources->RemoveFirst();
2917 // release delayed shared resources
2918 NCollection_Vector<TCollection_AsciiString> aDeadList;
2919 for (NCollection_DataMap<TCollection_AsciiString, Standard_Integer>::Iterator anIter (*myDelayed);
2920 anIter.More(); anIter.Next())
2922 if (++anIter.ChangeValue() <= 2)
2924 continue; // postpone release one more frame to ensure noone use it periodically
2927 const TCollection_AsciiString& aKey = anIter.Key();
2928 if (!mySharedResources->IsBound (aKey))
2930 // mixed unshared strategy delayed/undelayed was used!
2931 aDeadList.Append (aKey);
2935 auto& aRes = mySharedResources->ChangeFind (aKey);
2936 if (aRes->GetRefCount() > 1)
2938 // should be only 1 instance in mySharedResources
2939 // if not - resource was reused again
2940 aDeadList.Append (aKey);
2944 // release resource if no one requiested it more than 2 redraw calls
2945 aRes->Release (this);
2946 mySharedResources->UnBind (aKey);
2947 aDeadList.Append (aKey);
2950 for (Standard_Integer anIter = 0; anIter < aDeadList.Length(); ++anIter)
2952 myDelayed->UnBind (aDeadList.Value (anIter));
2956 // =======================================================================
2957 // function : BindProgram
2959 // =======================================================================
2960 Standard_Boolean OpenGl_Context::BindProgram (const Handle(OpenGl_ShaderProgram)& theProgram)
2962 if (core20fwd == NULL)
2964 return Standard_False;
2966 else if (myActiveProgram == theProgram)
2968 return Standard_True;
2971 if (theProgram.IsNull()
2972 || !theProgram->IsValid())
2974 if (!myActiveProgram.IsNull())
2976 core20fwd->glUseProgram (OpenGl_ShaderProgram::NO_PROGRAM);
2977 myActiveProgram.Nullify();
2979 return Standard_False;
2982 myActiveProgram = theProgram;
2983 core20fwd->glUseProgram (theProgram->ProgramId());
2984 return Standard_True;
2987 // =======================================================================
2988 // function : BindDefaultVao
2990 // =======================================================================
2991 void OpenGl_Context::BindDefaultVao()
2993 #if !defined(GL_ES_VERSION_2_0)
2994 if (myDefaultVao == 0
3000 core32->glBindVertexArray (myDefaultVao);
3004 // =======================================================================
3005 // function : SetDefaultFrameBuffer
3007 // =======================================================================
3008 Handle(OpenGl_FrameBuffer) OpenGl_Context::SetDefaultFrameBuffer (const Handle(OpenGl_FrameBuffer)& theFbo)
3010 Handle(OpenGl_FrameBuffer) aFbo = myDefaultFbo;
3011 myDefaultFbo = theFbo;
3015 // =======================================================================
3016 // function : SetShadingMaterial
3018 // =======================================================================
3019 void OpenGl_Context::SetShadingMaterial (const OpenGl_AspectFace* theAspect,
3020 const Handle(Graphic3d_PresentationAttributes)& theHighlight)
3022 const Handle(Graphic3d_AspectFillArea3d)& anAspect = (!theHighlight.IsNull() && !theHighlight->BasicFillAreaAspect().IsNull())
3023 ? theHighlight->BasicFillAreaAspect()
3024 : theAspect->Aspect();
3026 const bool toDistinguish = anAspect->Distinguish();
3027 const bool toMapTexture = anAspect->ToMapTexture();
3028 const Graphic3d_MaterialAspect& aMatFrontSrc = anAspect->FrontMaterial();
3029 const Graphic3d_MaterialAspect& aMatBackSrc = toDistinguish
3030 ? anAspect->BackMaterial()
3032 const Quantity_Color& aFrontIntColor = anAspect->InteriorColor();
3033 const Quantity_Color& aBackIntColor = toDistinguish
3034 ? anAspect->BackInteriorColor()
3037 myMatFront.Init (aMatFrontSrc, aFrontIntColor);
3040 myMatBack.Init (aMatBackSrc, aBackIntColor);
3044 myMatBack = myMatFront;
3047 if (!theHighlight.IsNull()
3048 && theHighlight->BasicFillAreaAspect().IsNull())
3050 myMatFront.SetColor (theHighlight->ColorRGBA());
3051 myMatBack .SetColor (theHighlight->ColorRGBA());
3054 Standard_ShortReal aTranspFront = 0.f;
3055 Standard_ShortReal aTranspBack = 0.f;
3056 if (CheckIsTransparent (theAspect, theHighlight, aTranspFront, aTranspBack))
3058 myMatFront.Diffuse.a() = 1.0f - aTranspFront;
3059 myMatBack .Diffuse.a() = 1.0f - aTranspBack;
3062 // do not update material properties in case of zero reflection mode,
3063 // because GL lighting will be disabled by OpenGl_PrimitiveArray::DrawArray() anyway.
3064 if (theAspect->IsNoLighting())
3069 if (myMatFront == myShaderManager->MaterialState().FrontMaterial()
3070 && myMatBack == myShaderManager->MaterialState().BackMaterial()
3071 && toDistinguish == myShaderManager->MaterialState().ToDistinguish()
3072 && toMapTexture == myShaderManager->MaterialState().ToMapTexture())
3077 myShaderManager->UpdateMaterialStateTo (myMatFront, myMatBack, toDistinguish, toMapTexture);
3080 // =======================================================================
3081 // function : CheckIsTransparent
3083 // =======================================================================
3084 Standard_Boolean OpenGl_Context::CheckIsTransparent (const OpenGl_AspectFace* theAspect,
3085 const Handle(Graphic3d_PresentationAttributes)& theHighlight,
3086 Standard_ShortReal& theTranspFront,
3087 Standard_ShortReal& theTranspBack)
3089 const Handle(Graphic3d_AspectFillArea3d)& anAspect = (!theHighlight.IsNull() && !theHighlight->BasicFillAreaAspect().IsNull())
3090 ? theHighlight->BasicFillAreaAspect()
3091 : theAspect->Aspect();
3093 const bool toDistinguish = anAspect->Distinguish();
3094 const Graphic3d_MaterialAspect& aMatFrontSrc = anAspect->FrontMaterial();
3095 const Graphic3d_MaterialAspect& aMatBackSrc = toDistinguish
3096 ? anAspect->BackMaterial()
3099 // handling transparency
3100 theTranspFront = aMatFrontSrc.Transparency();
3101 theTranspBack = aMatBackSrc .Transparency();
3102 if (!theHighlight.IsNull()
3103 && theHighlight->BasicFillAreaAspect().IsNull())
3105 theTranspFront = theHighlight->Transparency();
3106 theTranspBack = theHighlight->Transparency();
3109 return theTranspFront != 0.f
3110 || theTranspBack != 0.f;
3113 // =======================================================================
3114 // function : SetColor4fv
3116 // =======================================================================
3117 void OpenGl_Context::SetColor4fv (const OpenGl_Vec4& theColor)
3119 if (!myActiveProgram.IsNull())
3121 myActiveProgram->SetUniform (this, myActiveProgram->GetStateLocation (OpenGl_OCCT_COLOR), theColor);
3123 #if !defined(GL_ES_VERSION_2_0)
3124 else if (core11 != NULL)
3126 core11->glColor4fv (theColor.GetData());
3131 // =======================================================================
3132 // function : SetTypeOfLine
3134 // =======================================================================
3135 void OpenGl_Context::SetTypeOfLine (const Aspect_TypeOfLine theType,
3136 const Standard_ShortReal theFactor)
3138 Standard_Integer aPattern = 0xFFFF;
3141 case Aspect_TOL_DASH:
3146 case Aspect_TOL_DOT:
3151 case Aspect_TOL_DOTDASH:
3156 case Aspect_TOL_EMPTY:
3157 case Aspect_TOL_SOLID:
3162 case Aspect_TOL_USERDEFINED:
3169 if (!myActiveProgram.IsNull())
3171 myActiveProgram->SetUniform (this, "uPattern", aPattern);
3172 myActiveProgram->SetUniform (this, "uFactor", theFactor);
3176 #if !defined(GL_ES_VERSION_2_0)
3177 if (aPattern != 0xFFFF)
3182 gl2psEnable (GL2PS_LINE_STIPPLE);
3188 core11fwd->glEnable (GL_LINE_STIPPLE);
3190 core11->glLineStipple (static_cast<GLint> (theFactor),
3191 static_cast<GLushort> (aPattern));
3198 core11fwd->glDisable (GL_LINE_STIPPLE);
3204 gl2psDisable (GL2PS_LINE_STIPPLE);
3211 // =======================================================================
3212 // function : SetLineWidth
3214 // =======================================================================
3215 void OpenGl_Context::SetLineWidth (const Standard_ShortReal theWidth)
3219 // glLineWidth() is still defined within Core Profile, but has no effect with values != 1.0f
3220 core11fwd->glLineWidth (theWidth * myLineWidthScale);
3225 gl2psLineWidth (theWidth);
3230 // =======================================================================
3231 // function : SetTextureMatrix
3233 // =======================================================================
3234 void OpenGl_Context::SetTextureMatrix (const Handle(Graphic3d_TextureParams)& theParams)
3236 if (theParams.IsNull())
3240 else if (!myActiveProgram.IsNull())
3242 const GLint aUniLoc = myActiveProgram->GetStateLocation (OpenGl_OCCT_TEXTURE_TRSF2D);
3243 if (aUniLoc == OpenGl_ShaderProgram::INVALID_LOCATION)
3248 // pack transformation parameters
3249 OpenGl_Vec4 aTrsf[2];
3250 aTrsf[0].xy() = theParams->Translation();
3251 aTrsf[0].zw() = theParams->Scale();
3252 aTrsf[1].x() = std::sin (-theParams->Rotation() * static_cast<float> (M_PI / 180.0));
3253 aTrsf[1].y() = std::cos (-theParams->Rotation() * static_cast<float> (M_PI / 180.0));
3254 myActiveProgram->SetUniform (this, aUniLoc, 2, aTrsf);
3258 #if !defined(GL_ES_VERSION_2_0)
3261 GLint aMatrixMode = GL_TEXTURE;
3262 ::glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode);
3264 core11->glMatrixMode (GL_TEXTURE);
3265 OpenGl_Mat4 aTextureMat;
3266 const Graphic3d_Vec2& aScale = theParams->Scale();
3267 const Graphic3d_Vec2& aTrans = theParams->Translation();
3268 Graphic3d_TransformUtils::Scale (aTextureMat, aScale.x(), aScale.y(), 1.0f);
3269 Graphic3d_TransformUtils::Translate (aTextureMat, -aTrans.x(), -aTrans.y(), 0.0f);
3270 Graphic3d_TransformUtils::Rotate (aTextureMat, -theParams->Rotation(), 0.0f, 0.0f, 1.0f);
3271 core11->glLoadMatrixf (aTextureMat);
3272 core11->glMatrixMode (aMatrixMode);
3277 // =======================================================================
3278 // function : SetPointSize
3280 // =======================================================================
3281 void OpenGl_Context::SetPointSize (const Standard_ShortReal theSize)
3283 if (!myActiveProgram.IsNull())
3285 myActiveProgram->SetUniform (this, myActiveProgram->GetStateLocation (OpenGl_OCCT_POINT_SIZE), theSize);
3286 #if !defined(GL_ES_VERSION_2_0)
3287 //myContext->core11fwd->glEnable (GL_VERTEX_PROGRAM_POINT_SIZE);
3290 #if !defined(GL_ES_VERSION_2_0)
3293 core11fwd->glPointSize (theSize);
3294 if (core20fwd != NULL)
3296 //myContext->core11fwd->glDisable (GL_VERTEX_PROGRAM_POINT_SIZE);
3302 // =======================================================================
3303 // function : SetPointSpriteOrigin
3305 // =======================================================================
3306 void OpenGl_Context::SetPointSpriteOrigin()
3308 #if !defined(GL_ES_VERSION_2_0)
3309 if (core15fwd == NULL)
3314 const int aNewState = !myActiveProgram.IsNull() ? GL_UPPER_LEFT : GL_LOWER_LEFT;
3315 if (myPointSpriteOrig != aNewState)
3317 myPointSpriteOrig = aNewState;
3318 core15fwd->glPointParameteri (GL_POINT_SPRITE_COORD_ORIGIN, aNewState);
3323 // =======================================================================
3324 // function : SetGlNormalizeEnabled
3326 // =======================================================================
3327 Standard_Boolean OpenGl_Context::SetGlNormalizeEnabled (Standard_Boolean isEnabled)
3329 if (isEnabled == myIsGlNormalizeEnabled)
3331 return myIsGlNormalizeEnabled;
3334 Standard_Boolean anOldGlNormalize = myIsGlNormalizeEnabled;
3336 myIsGlNormalizeEnabled = isEnabled;
3338 #if !defined(GL_ES_VERSION_2_0)
3343 ::glEnable (GL_NORMALIZE);
3347 ::glDisable (GL_NORMALIZE);
3352 return anOldGlNormalize;
3355 // =======================================================================
3356 // function : SetPolygonMode
3358 // =======================================================================
3359 Standard_Integer OpenGl_Context::SetPolygonMode (const Standard_Integer theMode)
3361 if (myPolygonMode == theMode)
3363 return myPolygonMode;
3366 const Standard_Integer anOldPolygonMode = myPolygonMode;
3368 myPolygonMode = theMode;
3370 #if !defined(GL_ES_VERSION_2_0)
3371 ::glPolygonMode (GL_FRONT_AND_BACK, (GLenum)theMode);
3374 return anOldPolygonMode;
3377 // =======================================================================
3378 // function : SetPolygonHatchEnabled
3380 // =======================================================================
3381 bool OpenGl_Context::SetPolygonHatchEnabled (const bool theIsEnabled)
3383 if (myHatchStyles.IsNull())
3387 else if (myHatchStyles->IsEnabled() == theIsEnabled)
3389 return theIsEnabled;
3392 return myHatchStyles->SetEnabled (this, theIsEnabled);
3395 // =======================================================================
3396 // function : SetPolygonHatchStyle
3398 // =======================================================================
3399 Standard_Integer OpenGl_Context::SetPolygonHatchStyle (const Handle(Graphic3d_HatchStyle)& theStyle)
3401 if (theStyle.IsNull())
3406 if (myHatchStyles.IsNull())
3408 if (!GetResource ("OpenGl_LineAttributes", myHatchStyles))
3410 // share and register for release once the resource is no longer used
3411 myHatchStyles = new OpenGl_LineAttributes();
3412 ShareResource ("OpenGl_LineAttributes", myHatchStyles);
3415 if (myHatchStyles->TypeOfHatch() == theStyle->HatchType())
3417 return theStyle->HatchType();
3420 return myHatchStyles->SetTypeOfHatch (this, theStyle);
3423 // =======================================================================
3424 // function : ApplyModelWorldMatrix
3426 // =======================================================================
3427 void OpenGl_Context::ApplyModelWorldMatrix()
3429 if (myShaderManager->ModelWorldState().ModelWorldMatrix() != ModelWorldState.Current())
3431 myShaderManager->UpdateModelWorldStateTo (ModelWorldState.Current());
3435 // =======================================================================
3436 // function : ApplyWorldViewMatrix
3438 // =======================================================================
3439 void OpenGl_Context::ApplyWorldViewMatrix()
3441 if (myShaderManager->ModelWorldState().ModelWorldMatrix() != THE_IDENTITY_MATRIX)
3443 myShaderManager->UpdateModelWorldStateTo (THE_IDENTITY_MATRIX);
3445 if (myShaderManager->WorldViewState().WorldViewMatrix() != WorldViewState.Current())
3447 myShaderManager->UpdateWorldViewStateTo (WorldViewState.Current());
3451 // =======================================================================
3452 // function : ApplyModelViewMatrix
3454 // =======================================================================
3455 void OpenGl_Context::ApplyModelViewMatrix()
3457 if (myShaderManager->ModelWorldState().ModelWorldMatrix() != ModelWorldState.Current())
3459 myShaderManager->UpdateModelWorldStateTo (ModelWorldState.Current());
3461 if (myShaderManager->WorldViewState().WorldViewMatrix() != WorldViewState.Current())
3463 myShaderManager->UpdateWorldViewStateTo (WorldViewState.Current());
3467 // =======================================================================
3468 // function : ApplyProjectionMatrix
3470 // =======================================================================
3471 void OpenGl_Context::ApplyProjectionMatrix()
3473 if (myShaderManager->ProjectionState().ProjectionMatrix() != ProjectionState.Current())
3475 myShaderManager->UpdateProjectionStateTo (ProjectionState.Current());
3479 // =======================================================================
3480 // function : EnableFeatures
3482 // =======================================================================
3483 void OpenGl_Context::EnableFeatures() const
3488 // =======================================================================
3489 // function : DisableFeatures
3491 // =======================================================================
3492 void OpenGl_Context::DisableFeatures() const
3494 #if !defined(GL_ES_VERSION_2_0)
3495 glPixelTransferi (GL_MAP_COLOR, GL_FALSE);
3499 * Disable stuff that's likely to slow down glDrawPixels.
3500 * (Omit as much of this as possible, when you know in advance
3501 * that the OpenGL state will already be set correctly.)
3503 glDisable(GL_DITHER);
3504 glDisable(GL_BLEND);
3505 glDisable(GL_DEPTH_TEST);
3506 glDisable(GL_TEXTURE_2D);
3507 glDisable(GL_STENCIL_TEST);
3509 #if !defined(GL_ES_VERSION_2_0)
3510 glDisable(GL_LIGHTING);
3511 glDisable(GL_ALPHA_TEST);
3513 glDisable(GL_LOGIC_OP);
3514 glDisable(GL_TEXTURE_1D);
3516 glPixelTransferi(GL_MAP_COLOR, GL_FALSE);
3517 glPixelTransferi(GL_RED_SCALE, 1);
3518 glPixelTransferi(GL_RED_BIAS, 0);
3519 glPixelTransferi(GL_GREEN_SCALE, 1);
3520 glPixelTransferi(GL_GREEN_BIAS, 0);
3521 glPixelTransferi(GL_BLUE_SCALE, 1);
3522 glPixelTransferi(GL_BLUE_BIAS, 0);
3523 glPixelTransferi(GL_ALPHA_SCALE, 1);
3524 glPixelTransferi(GL_ALPHA_BIAS, 0);
3527 * Disable extensions that could slow down glDrawPixels.
3528 * (Actually, you should check for the presence of the proper
3529 * extension before making these calls. I've omitted that
3530 * code for simplicity.)
3533 if ((myGlVerMajor >= 1) && (myGlVerMinor >= 2))
3535 #ifdef GL_EXT_convolution
3536 if (CheckExtension ("GL_CONVOLUTION_1D_EXT"))
3537 glDisable(GL_CONVOLUTION_1D_EXT);
3539 if (CheckExtension ("GL_CONVOLUTION_2D_EXT"))
3540 glDisable(GL_CONVOLUTION_2D_EXT);
3542 if (CheckExtension ("GL_SEPARABLE_2D_EXT"))
3543 glDisable(GL_SEPARABLE_2D_EXT);
3546 #ifdef GL_EXT_histogram
3547 if (CheckExtension ("GL_SEPARABLE_2D_EXT"))
3548 glDisable(GL_HISTOGRAM_EXT);
3550 if (CheckExtension ("GL_MINMAX_EXT"))
3551 glDisable(GL_MINMAX_EXT);
3554 #ifdef GL_EXT_texture3D
3555 if (CheckExtension ("GL_TEXTURE_3D_EXT"))
3556 glDisable(GL_TEXTURE_3D_EXT);