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_ArbSamplerObject.hxx>
28 #include <OpenGl_ArbTexBindless.hxx>
29 #include <OpenGl_GlCore44.hxx>
30 #include <OpenGl_FrameBuffer.hxx>
31 #include <OpenGl_FrameStats.hxx>
32 #include <OpenGl_Sampler.hxx>
33 #include <OpenGl_ShaderManager.hxx>
34 #include <OpenGl_Workspace.hxx>
35 #include <OpenGl_AspectFace.hxx>
36 #include <Graphic3d_TransformUtils.hxx>
37 #include <Graphic3d_RenderingParams.hxx>
39 #include <Message_Messenger.hxx>
41 #include <NCollection_Vector.hxx>
43 #include <Standard_ProgramError.hxx>
45 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Context,Standard_Transient)
50 #pragma comment(lib, "libEGL.lib")
54 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
56 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
59 #include <OpenGL/OpenGL.h>
60 #include <CoreGraphics/CoreGraphics.h>
63 #include <GL/glx.h> // glXGetProcAddress()
68 static const Handle(OpenGl_Resource) NULL_GL_RESOURCE;
69 static const OpenGl_Mat4 THE_IDENTITY_MATRIX;
71 //! Add key-value pair to the dictionary.
72 static void addInfo (TColStd_IndexedDataMapOfStringString& theDict,
73 const TCollection_AsciiString& theKey,
74 const TCollection_AsciiString& theValue)
76 theDict.ChangeFromIndex (theDict.Add (theKey, theValue)) = theValue;
79 //! Add key-value pair to the dictionary.
80 static void addInfo (TColStd_IndexedDataMapOfStringString& theDict,
81 const TCollection_AsciiString& theKey,
84 TCollection_AsciiString aValue (theValue != NULL ? theValue : "");
85 theDict.ChangeFromIndex (theDict.Add (theKey, aValue)) = aValue;
89 // =======================================================================
90 // function : OpenGl_Context
92 // =======================================================================
93 OpenGl_Context::OpenGl_Context (const Handle(OpenGl_Caps)& theCaps)
112 caps (!theCaps.IsNull() ? theCaps : new OpenGl_Caps()),
113 #if defined(GL_ES_VERSION_2_0)
114 hasHighp (Standard_False),
115 hasUintIndex(Standard_False),
116 hasTexRGBA8(Standard_False),
118 hasHighp (Standard_True),
119 hasUintIndex(Standard_True),
120 hasTexRGBA8(Standard_True),
122 hasDrawBuffers (OpenGl_FeatureNotAvailable),
123 hasFloatBuffer (OpenGl_FeatureNotAvailable),
124 hasHalfFloatBuffer (OpenGl_FeatureNotAvailable),
125 hasSampleVariables (OpenGl_FeatureNotAvailable),
126 arbDrawBuffers (Standard_False),
127 arbNPTW (Standard_False),
128 arbTexRG (Standard_False),
129 arbTexFloat (Standard_False),
130 arbSamplerObject (NULL),
131 arbTexBindless (NULL),
133 arbTboRGB32 (Standard_False),
138 arbSampleShading (Standard_False),
139 extFragDepth (Standard_False),
140 extDrawBuffers (Standard_False),
142 extBgra(Standard_False),
143 extAnis(Standard_False),
144 extPDS (Standard_False),
145 atiMem (Standard_False),
146 nvxMem (Standard_False),
147 oesSampleVariables (Standard_False),
148 oesStdDerivatives (Standard_False),
149 mySharedResources (new OpenGl_ResourcesMap()),
150 myDelayed (new OpenGl_DelayReleaseMap()),
151 myUnusedResources (new OpenGl_ResourcesStack()),
153 myGlLibHandle (NULL),
154 myFuncs (new OpenGl_GlFunctions()),
156 myTexClamp (GL_CLAMP_TO_EDGE),
158 myMaxTexCombined (1),
161 myMaxDrawBuffers (1),
162 myMaxColorAttachments (1),
165 myIsInitialized (Standard_False),
166 myIsStereoBuffers (Standard_False),
167 myIsGlNormalizeEnabled (Standard_False),
168 myHasRayTracing (Standard_False),
169 myHasRayTracingTextures (Standard_False),
170 myHasRayTracingAdaptiveSampling (Standard_False),
171 myFrameStats (new OpenGl_FrameStats()),
172 #if !defined(GL_ES_VERSION_2_0)
173 myPointSpriteOrig (GL_UPPER_LEFT),
174 myRenderMode (GL_RENDER),
175 myPolygonMode (GL_FILL),
177 myPointSpriteOrig (0),
181 myToCullBackFaces (false),
186 myAlphaToCoverage (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 myPolygonOffset.Mode = Aspect_POM_Off;
204 myPolygonOffset.Factor = 0.0f;
205 myPolygonOffset.Units = 0.0f;
207 // system-dependent fields
208 #if defined(HAVE_EGL)
209 myDisplay = (Aspect_Display )EGL_NO_DISPLAY;
210 myWindow = (Aspect_Drawable )EGL_NO_SURFACE;
211 myGContext = (Aspect_RenderingContext )EGL_NO_CONTEXT;
212 #elif defined(_WIN32)
216 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
217 // Vendors can not extend functionality on this system
218 // and developers are limited to OpenGL support provided by Mac OS X SDK.
219 // We retrieve function pointers from system library
220 // to generalize extensions support on all platforms.
221 // In this way we also reach binary compatibility benefit between OS releases
222 // if some newest functionality is optionally used.
223 // Notice that GL version / extension availability checks are required
224 // because function pointers may be available but not functionality itself
225 // (depends on renderer).
226 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
228 myGlLibHandle = dlopen ("/System/Library/Frameworks/OpenGLES.framework/OpenGLES", RTLD_LAZY);
231 myGlLibHandle = dlopen ("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL", RTLD_LAZY);
239 memset (myFuncs.operator->(), 0, sizeof(OpenGl_GlFunctions));
240 myShaderManager = new OpenGl_ShaderManager (this);
243 // =======================================================================
244 // function : ~OpenGl_Context
246 // =======================================================================
247 OpenGl_Context::~OpenGl_Context()
249 // release clean up queue
252 #if !defined(GL_ES_VERSION_2_0)
253 // release default VAO
254 if (myDefaultVao != 0
258 core32->glDeleteVertexArrays (1, &myDefaultVao);
263 // release default FBO
264 if (!myDefaultFbo.IsNull())
266 myDefaultFbo->Release (this);
267 myDefaultFbo.Nullify();
270 // release shared resources if any
271 if (mySharedResources->GetRefCount() <= 1)
273 myShaderManager.Nullify();
274 for (NCollection_DataMap<TCollection_AsciiString, Handle(OpenGl_Resource)>::Iterator anIter (*mySharedResources);
275 anIter.More(); anIter.Next())
277 anIter.Value()->Release (this);
280 // release delayed resources added during deletion of shared resources
281 while (!myUnusedResources->IsEmpty())
283 myUnusedResources->First()->Release (this);
284 myUnusedResources->RemoveFirst();
287 else if (myShaderManager->IsSameContext (this))
289 myShaderManager->SetContext (NULL);
291 mySharedResources.Nullify();
299 #if !defined(GL_ES_VERSION_2_0)
301 glGetPointerv (GL_DEBUG_CALLBACK_USER_PARAM, &aPtr);
305 arbDbg->glDebugMessageCallback (NULL, NULL);
307 myIsGlDebugCtx = Standard_False;
311 // =======================================================================
312 // function : forcedRelease
314 // =======================================================================
315 void OpenGl_Context::forcedRelease()
318 for (NCollection_DataMap<TCollection_AsciiString, Handle(OpenGl_Resource)>::Iterator anIter (*mySharedResources);
319 anIter.More(); anIter.Next())
321 anIter.Value()->Release (this);
323 mySharedResources->Clear();
324 myShaderManager->clear();
325 myShaderManager->SetContext (NULL);
327 // release delayed resources added during deletion of shared resources
328 while (!myUnusedResources->IsEmpty())
330 myUnusedResources->First()->Release (this);
331 myUnusedResources->RemoveFirst();
335 // =======================================================================
336 // function : ResizeViewport
338 // =======================================================================
339 void OpenGl_Context::ResizeViewport (const Standard_Integer* theRect)
341 core11fwd->glViewport (theRect[0], theRect[1], theRect[2], theRect[3]);
342 myViewport[0] = theRect[0];
343 myViewport[1] = theRect[1];
344 myViewport[2] = theRect[2];
345 myViewport[3] = theRect[3];
346 if (HasRenderScale())
348 myViewportVirt[0] = Standard_Integer(theRect[0] * myRenderScaleInv);
349 myViewportVirt[1] = Standard_Integer(theRect[1] * myRenderScaleInv);
350 myViewportVirt[2] = Standard_Integer(theRect[2] * myRenderScaleInv);
351 myViewportVirt[3] = Standard_Integer(theRect[3] * myRenderScaleInv);
355 myViewportVirt[0] = theRect[0];
356 myViewportVirt[1] = theRect[1];
357 myViewportVirt[2] = theRect[2];
358 myViewportVirt[3] = theRect[3];
362 #if !defined(GL_ES_VERSION_2_0)
363 inline Standard_Integer stereoToMonoBuffer (const Standard_Integer theBuffer)
379 // =======================================================================
380 // function : SetReadBuffer
382 // =======================================================================
383 void OpenGl_Context::SetReadBuffer (const Standard_Integer theReadBuffer)
385 #if !defined(GL_ES_VERSION_2_0)
386 myReadBuffer = !myIsStereoBuffers ? stereoToMonoBuffer (theReadBuffer) : theReadBuffer;
387 if (myReadBuffer < GL_COLOR_ATTACHMENT0
390 arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
392 ::glReadBuffer (myReadBuffer);
394 (void )theReadBuffer;
398 // =======================================================================
399 // function : SetDrawBuffer
401 // =======================================================================
402 void OpenGl_Context::SetDrawBuffer (const Standard_Integer theDrawBuffer)
404 #if !defined(GL_ES_VERSION_2_0)
405 const Standard_Integer aDrawBuffer = !myIsStereoBuffers ? stereoToMonoBuffer (theDrawBuffer) : theDrawBuffer;
406 if (aDrawBuffer < GL_COLOR_ATTACHMENT0
409 arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
411 ::glDrawBuffer (aDrawBuffer);
413 myDrawBuffers.Clear();
415 if (aDrawBuffer != GL_NONE)
417 myDrawBuffers.SetValue (0, aDrawBuffer);
420 (void )theDrawBuffer;
424 // =======================================================================
425 // function : SetDrawBuffers
427 // =======================================================================
428 void OpenGl_Context::SetDrawBuffers (const Standard_Integer theNb, const Standard_Integer* theDrawBuffers)
430 Standard_ASSERT_RETURN (hasDrawBuffers, "Multiple draw buffers feature is not supported by the context", Standard_ASSERT_DO_NOTHING());
432 myDrawBuffers.Clear();
434 Standard_Boolean useDefaultFbo = Standard_False;
435 for (Standard_Integer anI = 0; anI < theNb; ++anI)
437 if (theDrawBuffers[anI] < GL_COLOR_ATTACHMENT0 && theDrawBuffers[anI] != GL_NONE)
439 useDefaultFbo = Standard_True;
441 else if (theDrawBuffers[anI] != GL_NONE)
443 myDrawBuffers.SetValue (anI, theDrawBuffers[anI]);
446 if (arbFBO != NULL && useDefaultFbo)
448 arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
451 myFuncs->glDrawBuffers (theNb, (const GLenum*)theDrawBuffers);
454 // =======================================================================
455 // function : SetCullBackFaces
457 // =======================================================================
458 void OpenGl_Context::SetCullBackFaces (bool theToEnable)
460 if (myToCullBackFaces == theToEnable)
465 myToCullBackFaces = theToEnable;
468 //glCullFace (GL_BACK); GL_BACK by default
469 core11fwd->glEnable (GL_CULL_FACE);
473 core11fwd->glDisable (GL_CULL_FACE);
477 // =======================================================================
478 // function : FetchState
480 // =======================================================================
481 void OpenGl_Context::FetchState()
483 #if !defined(GL_ES_VERSION_2_0)
484 // cache feedback mode state
487 ::glGetIntegerv (GL_RENDER_MODE, &myRenderMode);
490 // cache read buffers state
491 ::glGetIntegerv (GL_READ_BUFFER, &myReadBuffer);
493 // cache draw buffers state
494 myDrawBuffers.Clear();
496 if (myMaxDrawBuffers == 1)
498 Standard_Integer aDrawBuffer;
500 ::glGetIntegerv (GL_DRAW_BUFFER, &aDrawBuffer);
502 if (aDrawBuffer != GL_NONE)
504 myDrawBuffers.SetValue (0, aDrawBuffer);
509 Standard_Integer aDrawBuffer;
511 for (Standard_Integer anI = 0; anI < myMaxDrawBuffers; ++anI)
513 ::glGetIntegerv (GL_DRAW_BUFFER0 + anI, &aDrawBuffer);
515 if (aDrawBuffer != GL_NONE)
517 myDrawBuffers.SetValue (anI, aDrawBuffer);
524 // =======================================================================
527 // =======================================================================
528 void OpenGl_Context::Share (const Handle(OpenGl_Context)& theShareCtx)
530 if (!theShareCtx.IsNull())
532 mySharedResources = theShareCtx->mySharedResources;
533 myDelayed = theShareCtx->myDelayed;
534 myUnusedResources = theShareCtx->myUnusedResources;
535 myShaderManager = theShareCtx->myShaderManager;
539 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
541 // =======================================================================
542 // function : IsCurrent
544 // =======================================================================
545 Standard_Boolean OpenGl_Context::IsCurrent() const
547 #if defined(HAVE_EGL)
548 if ((EGLDisplay )myDisplay == EGL_NO_DISPLAY
549 || (EGLSurface )myWindow == EGL_NO_SURFACE
550 || (EGLContext )myGContext == EGL_NO_CONTEXT)
552 return Standard_False;
555 return (((EGLDisplay )myDisplay == eglGetCurrentDisplay())
556 && ((EGLContext )myGContext == eglGetCurrentContext())
557 && ((EGLSurface )myWindow == eglGetCurrentSurface (EGL_DRAW)));
558 #elif defined(_WIN32)
559 if (myWindowDC == NULL || myGContext == NULL)
561 return Standard_False;
563 return (( (HDC )myWindowDC == wglGetCurrentDC())
564 && ((HGLRC )myGContext == wglGetCurrentContext()));
566 if (myDisplay == NULL || myWindow == 0 || myGContext == 0)
568 return Standard_False;
571 return ( ((Display* )myDisplay == glXGetCurrentDisplay())
572 && ((GLXContext )myGContext == glXGetCurrentContext())
573 && ((GLXDrawable )myWindow == glXGetCurrentDrawable()));
577 // =======================================================================
578 // function : MakeCurrent
580 // =======================================================================
581 Standard_Boolean OpenGl_Context::MakeCurrent()
583 #if defined(HAVE_EGL)
584 if ((EGLDisplay )myDisplay == EGL_NO_DISPLAY
585 || (EGLSurface )myWindow == EGL_NO_SURFACE
586 || (EGLContext )myGContext == EGL_NO_CONTEXT)
588 Standard_ProgramError_Raise_if (myIsInitialized, "OpenGl_Context::Init() should be called before!");
589 return Standard_False;
592 if (eglMakeCurrent ((EGLDisplay )myDisplay, (EGLSurface )myWindow, (EGLSurface )myWindow, (EGLContext )myGContext) != EGL_TRUE)
594 // if there is no current context it might be impossible to use glGetError() correctly
595 PushMessage (GL_DEBUG_SOURCE_WINDOW_SYSTEM, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH,
596 "eglMakeCurrent() has failed!");
597 myIsInitialized = Standard_False;
598 return Standard_False;
600 #elif defined(_WIN32)
601 if (myWindowDC == NULL || myGContext == NULL)
603 Standard_ProgramError_Raise_if (myIsInitialized, "OpenGl_Context::Init() should be called before!");
604 return Standard_False;
607 // technically it should be safe to activate already bound GL context
608 // however some drivers (Intel etc.) may FAIL doing this for unknown reason
611 myShaderManager->SetContext (this);
612 return Standard_True;
614 else if (wglMakeCurrent ((HDC )myWindowDC, (HGLRC )myGContext) != TRUE)
616 // notice that glGetError() couldn't be used here!
617 wchar_t* aMsgBuff = NULL;
618 DWORD anErrorCode = GetLastError();
619 FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
620 NULL, anErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (wchar_t* )&aMsgBuff, 0, NULL);
621 TCollection_ExtendedString aMsg ("wglMakeCurrent() has failed. ");
622 if (aMsgBuff != NULL)
624 aMsg += (Standard_ExtString )aMsgBuff;
625 LocalFree (aMsgBuff);
627 PushMessage (GL_DEBUG_SOURCE_WINDOW_SYSTEM, GL_DEBUG_TYPE_ERROR, (unsigned int )anErrorCode, GL_DEBUG_SEVERITY_HIGH, aMsg);
628 myIsInitialized = Standard_False;
629 return Standard_False;
632 if (myDisplay == NULL || myWindow == 0 || myGContext == 0)
634 Standard_ProgramError_Raise_if (myIsInitialized, "OpenGl_Context::Init() should be called before!");
635 return Standard_False;
638 if (!glXMakeCurrent ((Display* )myDisplay, (GLXDrawable )myWindow, (GLXContext )myGContext))
640 // if there is no current context it might be impossible to use glGetError() correctly
641 PushMessage (GL_DEBUG_SOURCE_WINDOW_SYSTEM, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH,
642 "glXMakeCurrent() has failed!");
643 myIsInitialized = Standard_False;
644 return Standard_False;
647 myShaderManager->SetContext (this);
648 return Standard_True;
651 // =======================================================================
652 // function : SwapBuffers
654 // =======================================================================
655 void OpenGl_Context::SwapBuffers()
657 #if defined(HAVE_EGL)
658 if ((EGLSurface )myWindow != EGL_NO_SURFACE)
660 eglSwapBuffers ((EGLDisplay )myDisplay, (EGLSurface )myWindow);
662 #elif defined(_WIN32)
663 if ((HDC )myWindowDC != NULL)
665 ::SwapBuffers ((HDC )myWindowDC);
669 if ((Display* )myDisplay != NULL)
671 glXSwapBuffers ((Display* )myDisplay, (GLXDrawable )myWindow);
678 // =======================================================================
679 // function : SetSwapInterval
681 // =======================================================================
682 Standard_Boolean OpenGl_Context::SetSwapInterval (const Standard_Integer theInterval)
684 #if defined(HAVE_EGL)
685 if (::eglSwapInterval ((EGLDisplay )myDisplay, theInterval) == EGL_TRUE)
687 return Standard_True;
689 #elif defined(_WIN32)
690 if (myFuncs->wglSwapIntervalEXT != NULL)
692 myFuncs->wglSwapIntervalEXT (theInterval);
693 return Standard_True;
695 #elif defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
697 #elif defined(__APPLE__)
698 if (::CGLSetParameter (CGLGetCurrentContext(), kCGLCPSwapInterval, &theInterval) == kCGLNoError)
700 return Standard_True;
703 if (theInterval == -1
704 && myFuncs->glXSwapIntervalEXT != NULL)
706 typedef int (*glXSwapIntervalEXT_t_x)(Display* theDisplay, GLXDrawable theDrawable, int theInterval);
707 glXSwapIntervalEXT_t_x aFuncPtr = (glXSwapIntervalEXT_t_x )myFuncs->glXSwapIntervalEXT;
708 aFuncPtr ((Display* )myDisplay, (GLXDrawable )myWindow, theInterval);
709 return Standard_True;
711 else if (myFuncs->glXSwapIntervalSGI != NULL)
713 myFuncs->glXSwapIntervalSGI (theInterval);
714 return Standard_True;
717 return Standard_False;
720 // =======================================================================
721 // function : findProc
723 // =======================================================================
724 void* OpenGl_Context::findProc (const char* theFuncName)
726 #if defined(HAVE_EGL)
727 return (void* )eglGetProcAddress (theFuncName);
728 #elif defined(_WIN32)
729 return (void* )wglGetProcAddress (theFuncName);
730 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
731 return (myGlLibHandle != NULL) ? dlsym (myGlLibHandle, theFuncName) : NULL;
733 return (void* )glXGetProcAddress ((const GLubyte* )theFuncName);
737 // =======================================================================
738 // function : CheckExtension
740 // =======================================================================
741 Standard_Boolean OpenGl_Context::CheckExtension (const char* theExtName) const
743 if (theExtName == NULL)
746 std::cerr << "CheckExtension called with NULL string!\n";
748 return Standard_False;
751 #if !defined(GL_ES_VERSION_2_0)
752 // available since OpenGL 3.0
753 // and the ONLY way to check extensions with OpenGL 3.1+ core profile
754 if (IsGlGreaterEqual (3, 0)
755 && myFuncs->glGetStringi != NULL)
758 ::glGetIntegerv (GL_NUM_EXTENSIONS, &anExtNb);
759 const size_t anExtNameLen = strlen (theExtName);
760 for (GLint anIter = 0; anIter < anExtNb; ++anIter)
762 const char* anExtension = (const char* )myFuncs->glGetStringi (GL_EXTENSIONS, (GLuint )anIter);
763 const size_t aTestExtNameLen = strlen (anExtension);
764 if (aTestExtNameLen == anExtNameLen
765 && strncmp (anExtension, theExtName, anExtNameLen) == 0)
767 return Standard_True;
770 return Standard_False;
774 // use old way with huge string for all extensions
775 const char* anExtString = (const char* )glGetString (GL_EXTENSIONS);
776 if (anExtString == NULL)
778 Messenger()->Send ("TKOpenGL: glGetString (GL_EXTENSIONS) has returned NULL! No GL context?", Message_Warning);
779 return Standard_False;
781 return CheckExtension (anExtString, theExtName);
784 // =======================================================================
785 // function : CheckExtension
787 // =======================================================================
788 Standard_Boolean OpenGl_Context::CheckExtension (const char* theExtString,
789 const char* theExtName)
791 if (theExtString == NULL)
793 return Standard_False;
796 // Search for theExtName in the extensions string.
797 // Use of strstr() is not sufficient because extension names can be prefixes of other extension names.
798 char* aPtrIter = (char* )theExtString;
799 const char* aPtrEnd = aPtrIter + strlen (theExtString);
800 const size_t anExtNameLen = strlen (theExtName);
801 while (aPtrIter < aPtrEnd)
803 const size_t n = strcspn (aPtrIter, " ");
804 if ((n == anExtNameLen) && (strncmp (aPtrIter, theExtName, anExtNameLen) == 0))
806 return Standard_True;
810 return Standard_False;
813 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
815 // =======================================================================
818 // =======================================================================
819 Standard_Boolean OpenGl_Context::Init (const Standard_Boolean theIsCoreProfile)
823 return Standard_True;
826 #if defined(HAVE_EGL)
827 myDisplay = (Aspect_Display )eglGetCurrentDisplay();
828 myGContext = (Aspect_RenderingContext )eglGetCurrentContext();
829 myWindow = (Aspect_Drawable )eglGetCurrentSurface(EGL_DRAW);
830 #elif defined(_WIN32)
831 myWindowDC = (Aspect_Handle )wglGetCurrentDC();
832 myGContext = (Aspect_RenderingContext )wglGetCurrentContext();
834 myDisplay = (Aspect_Display )glXGetCurrentDisplay();
835 myGContext = (Aspect_RenderingContext )glXGetCurrentContext();
836 myWindow = (Aspect_Drawable )glXGetCurrentDrawable();
838 if (myGContext == NULL)
840 return Standard_False;
843 init (theIsCoreProfile);
844 myIsInitialized = Standard_True;
845 return Standard_True;
850 // =======================================================================
853 // =======================================================================
854 #if defined(HAVE_EGL)
855 Standard_Boolean OpenGl_Context::Init (const Aspect_Drawable theEglSurface,
856 const Aspect_Display theEglDisplay,
857 const Aspect_RenderingContext theEglContext,
858 const Standard_Boolean theIsCoreProfile)
859 #elif defined(_WIN32)
860 Standard_Boolean OpenGl_Context::Init (const Aspect_Handle theWindow,
861 const Aspect_Handle theWindowDC,
862 const Aspect_RenderingContext theGContext,
863 const Standard_Boolean theIsCoreProfile)
864 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
866 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
867 Standard_Boolean OpenGl_Context::Init (EAGLContext* theGContext,
868 const Standard_Boolean theIsCoreProfile)
870 Standard_Boolean OpenGl_Context::Init (NSOpenGLContext* theGContext,
871 const Standard_Boolean theIsCoreProfile)
875 Standard_Boolean OpenGl_Context::Init (const Aspect_Drawable theWindow,
876 const Aspect_Display theDisplay,
877 const Aspect_RenderingContext theGContext,
878 const Standard_Boolean theIsCoreProfile)
881 Standard_ProgramError_Raise_if (myIsInitialized, "OpenGl_Context::Init() should be called only once!");
882 #if defined(HAVE_EGL)
883 myWindow = theEglSurface;
884 myGContext = theEglContext;
885 myDisplay = theEglDisplay;
886 #elif defined(_WIN32)
887 myWindow = theWindow;
888 myGContext = theGContext;
889 myWindowDC = theWindowDC;
890 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
891 myGContext = theGContext;
893 myWindow = theWindow;
894 myGContext = theGContext;
895 myDisplay = theDisplay;
897 if (myGContext == NULL || !MakeCurrent())
899 return Standard_False;
902 init (theIsCoreProfile);
903 myIsInitialized = Standard_True;
904 return Standard_True;
907 // =======================================================================
908 // function : ResetErrors
910 // =======================================================================
911 bool OpenGl_Context::ResetErrors (const bool theToPrintErrors)
914 int anErr = ::glGetError();
915 const bool hasError = anErr != GL_NO_ERROR;
916 if (!theToPrintErrors)
918 for (; anErr != GL_NO_ERROR && aPrevErr != anErr; aPrevErr = anErr, anErr = ::glGetError())
925 for (; anErr != GL_NO_ERROR && aPrevErr != anErr; aPrevErr = anErr, anErr = ::glGetError())
927 TCollection_ExtendedString anErrId;
930 case GL_INVALID_ENUM: anErrId = "GL_INVALID_ENUM"; break;
931 case GL_INVALID_VALUE: anErrId = "GL_INVALID_VALUE"; break;
932 case GL_INVALID_OPERATION: anErrId = "GL_INVALID_OPERATION"; break;
933 #ifdef GL_STACK_OVERFLOW
934 case GL_STACK_OVERFLOW: anErrId = "GL_STACK_OVERFLOW"; break;
935 case GL_STACK_UNDERFLOW: anErrId = "GL_STACK_UNDERFLOW"; break;
937 case GL_OUT_OF_MEMORY: anErrId = "GL_OUT_OF_MEMORY"; break;
938 case GL_INVALID_FRAMEBUFFER_OPERATION:
939 anErrId = "GL_INVALID_FRAMEBUFFER_OPERATION";
942 anErrId = TCollection_ExtendedString("#") + anErr;
946 const TCollection_ExtendedString aMsg = TCollection_ExtendedString ("Unhandled GL error: ") + anErrId;
947 PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_OTHER, 0, GL_DEBUG_SEVERITY_LOW, aMsg);
952 // =======================================================================
953 // function : ReadGlVersion
955 // =======================================================================
956 void OpenGl_Context::ReadGlVersion (Standard_Integer& theGlVerMajor,
957 Standard_Integer& theGlVerMinor)
963 #ifdef GL_MAJOR_VERSION
964 // available since OpenGL 3.0 and OpenGL 3.0 ES
965 GLint aMajor = 0, aMinor = 0;
966 glGetIntegerv (GL_MAJOR_VERSION, &aMajor);
967 glGetIntegerv (GL_MINOR_VERSION, &aMinor);
968 // glGetError() sometimes does not report an error here even if
969 // GL does not know GL_MAJOR_VERSION and GL_MINOR_VERSION constants.
970 // This happens on some renderers like e.g. Cygwin MESA.
971 // Thus checking additionally if GL has put anything to
972 // the output variables.
973 if (::glGetError() == GL_NO_ERROR && aMajor != 0 && aMinor != 0)
975 theGlVerMajor = aMajor;
976 theGlVerMinor = aMinor;
979 for (GLenum anErr = ::glGetError(), aPrevErr = GL_NO_ERROR;; aPrevErr = anErr, anErr = ::glGetError())
981 if (anErr == GL_NO_ERROR
982 || anErr == aPrevErr)
989 // Read version string.
990 // Notice that only first two numbers split by point '2.1 XXXXX' are significant.
991 // Following trash (after space) is vendor-specific.
992 // New drivers also returns micro version of GL like '3.3.0' which has no meaning
993 // and should be considered as vendor-specific too.
994 const char* aVerStr = (const char* )glGetString (GL_VERSION);
995 if (aVerStr == NULL || *aVerStr == '\0')
997 // invalid GL context
1001 //#if defined(GL_ES_VERSION_2_0)
1002 // skip "OpenGL ES-** " section
1003 for (; *aVerStr != '\0'; ++aVerStr)
1005 if (*aVerStr >= '0' && *aVerStr <= '9')
1012 // parse string for major number
1015 size_t aMajIter = 0;
1016 while (aVerStr[aMajIter] >= '0' && aVerStr[aMajIter] <= '9')
1020 if (aMajIter == 0 || aMajIter >= sizeof(aMajorStr))
1024 memcpy (aMajorStr, aVerStr, aMajIter);
1025 aMajorStr[aMajIter] = '\0';
1027 // parse string for minor number
1028 aVerStr += aMajIter + 1;
1029 size_t aMinIter = 0;
1030 while (aVerStr[aMinIter] >= '0' && aVerStr[aMinIter] <= '9')
1034 if (aMinIter == 0 || aMinIter >= sizeof(aMinorStr))
1038 memcpy (aMinorStr, aVerStr, aMinIter);
1039 aMinorStr[aMinIter] = '\0';
1042 theGlVerMajor = atoi (aMajorStr);
1043 theGlVerMinor = atoi (aMinorStr);
1045 if (theGlVerMajor <= 0)
1052 static Standard_CString THE_DBGMSG_UNKNOWN = "UNKNOWN";
1053 static Standard_CString THE_DBGMSG_SOURCES[] =
1055 ".OpenGL", // GL_DEBUG_SOURCE_API
1056 ".WinSystem", // GL_DEBUG_SOURCE_WINDOW_SYSTEM
1057 ".GLSL", // GL_DEBUG_SOURCE_SHADER_COMPILER
1058 ".3rdParty", // GL_DEBUG_SOURCE_THIRD_PARTY
1059 "", // GL_DEBUG_SOURCE_APPLICATION
1060 ".Other" // GL_DEBUG_SOURCE_OTHER
1063 static Standard_CString THE_DBGMSG_TYPES[] =
1065 "Error", // GL_DEBUG_TYPE_ERROR
1066 "Deprecated", // GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR
1067 "Undef. behavior", // GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR
1068 "Portability", // GL_DEBUG_TYPE_PORTABILITY
1069 "Performance", // GL_DEBUG_TYPE_PERFORMANCE
1070 "Other" // GL_DEBUG_TYPE_OTHER
1073 static Standard_CString THE_DBGMSG_SEV_HIGH = "High"; // GL_DEBUG_SEVERITY_HIGH
1074 static Standard_CString THE_DBGMSG_SEV_MEDIUM = "Medium"; // GL_DEBUG_SEVERITY_MEDIUM
1075 static Standard_CString THE_DBGMSG_SEV_LOW = "Low"; // GL_DEBUG_SEVERITY_LOW
1077 //! Callback for GL_ARB_debug_output extension
1078 static void APIENTRY debugCallbackWrap(unsigned int theSource,
1079 unsigned int theType,
1081 unsigned int theSeverity,
1083 const char* theMessage,
1084 const void* theUserParam)
1086 OpenGl_Context* aCtx = (OpenGl_Context* )theUserParam;
1087 aCtx->PushMessage (theSource, theType, theId, theSeverity, theMessage);
1090 // =======================================================================
1091 // function : PushMessage
1093 // =======================================================================
1094 void OpenGl_Context::PushMessage (const unsigned int theSource,
1095 const unsigned int theType,
1096 const unsigned int theId,
1097 const unsigned int theSeverity,
1098 const TCollection_ExtendedString& theMessage)
1100 if (caps->suppressExtraMsg
1101 && theSource >= GL_DEBUG_SOURCE_API
1102 && theSource <= GL_DEBUG_SOURCE_OTHER
1103 && myFilters[theSource - GL_DEBUG_SOURCE_API].Contains (theId))
1108 Standard_CString& aSrc = (theSource >= GL_DEBUG_SOURCE_API
1109 && theSource <= GL_DEBUG_SOURCE_OTHER)
1110 ? THE_DBGMSG_SOURCES[theSource - GL_DEBUG_SOURCE_API]
1111 : THE_DBGMSG_UNKNOWN;
1112 Standard_CString& aType = (theType >= GL_DEBUG_TYPE_ERROR
1113 && theType <= GL_DEBUG_TYPE_OTHER)
1114 ? THE_DBGMSG_TYPES[theType - GL_DEBUG_TYPE_ERROR]
1115 : THE_DBGMSG_UNKNOWN;
1116 Standard_CString& aSev = theSeverity == GL_DEBUG_SEVERITY_HIGH
1117 ? THE_DBGMSG_SEV_HIGH
1118 : (theSeverity == GL_DEBUG_SEVERITY_MEDIUM
1119 ? THE_DBGMSG_SEV_MEDIUM
1120 : THE_DBGMSG_SEV_LOW);
1121 Message_Gravity aGrav = theSeverity == GL_DEBUG_SEVERITY_HIGH
1123 : (theSeverity == GL_DEBUG_SEVERITY_MEDIUM
1127 TCollection_ExtendedString aMsg;
1128 aMsg += "TKOpenGl"; aMsg += aSrc;
1129 aMsg += " | Type: "; aMsg += aType;
1130 aMsg += " | ID: "; aMsg += (Standard_Integer )theId;
1131 aMsg += " | Severity: "; aMsg += aSev;
1132 aMsg += " | Message:\n ";
1134 Messenger()->Send (aMsg, aGrav);
1137 // =======================================================================
1138 // function : ExcludeMessage
1140 // ======================================================================
1141 Standard_Boolean OpenGl_Context::ExcludeMessage (const unsigned int theSource,
1142 const unsigned int theId)
1144 return theSource >= GL_DEBUG_SOURCE_API
1145 && theSource <= GL_DEBUG_SOURCE_OTHER
1146 && myFilters[theSource - GL_DEBUG_SOURCE_API].Add (theId);
1149 // =======================================================================
1150 // function : IncludeMessage
1152 // ======================================================================
1153 Standard_Boolean OpenGl_Context::IncludeMessage (const unsigned int theSource,
1154 const unsigned int theId)
1156 return theSource >= GL_DEBUG_SOURCE_API
1157 && theSource <= GL_DEBUG_SOURCE_OTHER
1158 && myFilters[theSource - GL_DEBUG_SOURCE_API].Remove (theId);
1161 // =======================================================================
1162 // function : checkWrongVersion
1164 // ======================================================================
1165 void OpenGl_Context::checkWrongVersion (const Standard_Integer theGlVerMajor,
1166 const Standard_Integer theGlVerMinor)
1168 if (!IsGlGreaterEqual (theGlVerMajor, theGlVerMinor))
1173 TCollection_ExtendedString aMsg = TCollection_ExtendedString()
1174 + "Error! OpenGL context reports version "
1175 + myGlVerMajor + "." + myGlVerMinor
1176 + " but does not export required functions for "
1177 + theGlVerMajor + "." + theGlVerMinor;
1178 PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1179 GL_DEBUG_TYPE_ERROR,
1181 GL_DEBUG_SEVERITY_HIGH,
1185 // =======================================================================
1188 // =======================================================================
1189 void OpenGl_Context::init (const Standard_Boolean theIsCoreProfile)
1194 myMaxMsaaSamples = 0;
1195 myMaxDrawBuffers = 1;
1196 myMaxColorAttachments = 1;
1197 ReadGlVersion (myGlVerMajor, myGlVerMinor);
1198 myVendor = (const char* )::glGetString (GL_VENDOR);
1199 if (!caps->ffpEnable
1200 && !IsGlGreaterEqual (2, 0))
1202 caps->ffpEnable = true;
1203 TCollection_ExtendedString aMsg =
1204 TCollection_ExtendedString("OpenGL driver is too old! Context info:\n")
1205 + " Vendor: " + (const char* )::glGetString (GL_VENDOR) + "\n"
1206 + " Renderer: " + (const char* )::glGetString (GL_RENDERER) + "\n"
1207 + " Version: " + (const char* )::glGetString (GL_VERSION) + "\n"
1208 + " Fallback using deprecated fixed-function pipeline.\n"
1209 + " Visualization might work incorrectly.\n"
1210 " Consider upgrading the graphics driver.";
1211 PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
1214 #if defined(GL_ES_VERSION_2_0)
1215 (void )theIsCoreProfile;
1216 const bool isCoreProfile = false;
1219 if (myVendor.Search ("NVIDIA") != -1)
1221 // Buffer detailed info: Buffer object 1 (bound to GL_ARRAY_BUFFER_ARB, usage hint is GL_STATIC_DRAW)
1222 // will use VIDEO memory as the source for buffer object operations.
1223 ExcludeMessage (GL_DEBUG_SOURCE_API, 131185);
1225 if (IsGlGreaterEqual (3, 0))
1227 // retrieve auxiliary function in advance
1228 FindProc ("glGetStringi", myFuncs->glGetStringi);
1231 bool isCoreProfile = false;
1232 if (IsGlGreaterEqual (3, 2))
1234 isCoreProfile = (theIsCoreProfile == Standard_True);
1236 // detect Core profile
1240 ::glGetIntegerv (GL_CONTEXT_PROFILE_MASK, &aProfile);
1241 isCoreProfile = (aProfile & GL_CONTEXT_CORE_PROFILE_BIT) != 0;
1249 core11 = (OpenGl_GlCore11* )(&(*myFuncs));
1251 core11fwd = (OpenGl_GlCore11Fwd* )(&(*myFuncs));
1269 arbTboRGB32 = Standard_False;
1277 //! Make record shorter to retrieve function pointer using variable with same name
1278 #define FindProcShort(theFunc) FindProc(#theFunc, myFuncs->theFunc)
1280 #if defined(GL_ES_VERSION_2_0)
1282 hasTexRGBA8 = IsGlGreaterEqual (3, 0)
1283 || CheckExtension ("GL_OES_rgb8_rgba8");
1284 // NPOT textures has limited support within OpenGL ES 2.0
1285 // which are relaxed by OpenGL ES 3.0 or some extensions
1286 //arbNPTW = IsGlGreaterEqual (3, 0)
1287 // || CheckExtension ("GL_OES_texture_npot")
1288 // || CheckExtension ("GL_NV_texture_npot_2D_mipmap");
1289 arbNPTW = Standard_True;
1290 arbTexRG = IsGlGreaterEqual (3, 0)
1291 || CheckExtension ("GL_EXT_texture_rg");
1292 extBgra = CheckExtension ("GL_EXT_texture_format_BGRA8888");
1293 extAnis = CheckExtension ("GL_EXT_texture_filter_anisotropic");
1294 extPDS = IsGlGreaterEqual (3, 0)
1295 || CheckExtension ("GL_OES_packed_depth_stencil");
1297 core11fwd = (OpenGl_GlCore11Fwd* )(&(*myFuncs));
1298 if (IsGlGreaterEqual (2, 0))
1300 // enable compatible functions
1301 core20 = (OpenGl_GlCore20* )(&(*myFuncs));
1302 core20fwd = (OpenGl_GlCore20Fwd* )(&(*myFuncs));
1303 core15fwd = (OpenGl_GlCore15Fwd* )(&(*myFuncs));
1304 arbFBO = (OpenGl_ArbFBO* )(&(*myFuncs));
1306 if (IsGlGreaterEqual (3, 0)
1307 && FindProcShort (glBlitFramebuffer))
1309 arbFBOBlit = (OpenGl_ArbFBOBlit* )(&(*myFuncs));
1311 if (IsGlGreaterEqual (3, 0)
1312 && FindProcShort (glGenSamplers)
1313 && FindProcShort (glDeleteSamplers)
1314 && FindProcShort (glIsSampler)
1315 && FindProcShort (glBindSampler)
1316 && FindProcShort (glSamplerParameteri)
1317 && FindProcShort (glSamplerParameteriv)
1318 && FindProcShort (glSamplerParameterf)
1319 && FindProcShort (glSamplerParameterfv)
1320 && FindProcShort (glGetSamplerParameteriv)
1321 && FindProcShort (glGetSamplerParameterfv))
1322 //&& FindProcShort (glSamplerParameterIiv) // only on Desktop or with extensions GL_OES_texture_border_clamp/GL_EXT_texture_border_clamp
1323 //&& FindProcShort (glSamplerParameterIuiv)
1324 //&& FindProcShort (glGetSamplerParameterIiv)
1325 //&& FindProcShort (glGetSamplerParameterIuiv))
1327 arbSamplerObject = (OpenGl_ArbSamplerObject* )(&(*myFuncs));
1329 extFragDepth = !IsGlGreaterEqual(3, 0)
1330 && CheckExtension ("GL_EXT_frag_depth");
1331 if (IsGlGreaterEqual (3, 1)
1332 && FindProc ("glTexStorage2DMultisample", myFuncs->glTexStorage2DMultisample))
1334 // MSAA RenderBuffers have been defined in OpenGL ES 3.0,
1335 // but MSAA Textures - only in OpenGL ES 3.1+
1336 ::glGetIntegerv (GL_MAX_SAMPLES, &myMaxMsaaSamples);
1339 hasUintIndex = IsGlGreaterEqual (3, 0)
1340 || CheckExtension ("GL_OES_element_index_uint");
1341 hasHighp = CheckExtension ("GL_OES_fragment_precision_high");
1342 GLint aRange[2] = {0, 0};
1344 ::glGetShaderPrecisionFormat (GL_FRAGMENT_SHADER, GL_HIGH_FLOAT, aRange, &aPrec);
1347 hasHighp = Standard_True;
1350 arbTexFloat = IsGlGreaterEqual (3, 0)
1351 && FindProc ("glTexImage3D", myFuncs->glTexImage3D);
1353 const Standard_Boolean hasTexBuffer32 = IsGlGreaterEqual (3, 2) && FindProc ("glTexBuffer", myFuncs->glTexBuffer);
1354 const Standard_Boolean hasExtTexBuffer = CheckExtension ("GL_EXT_texture_buffer") && FindProc ("glTexBufferEXT", myFuncs->glTexBuffer);
1356 if (hasTexBuffer32 || hasExtTexBuffer)
1358 arbTBO = reinterpret_cast<OpenGl_ArbTBO*> (myFuncs.get());
1361 // initialize debug context extension
1362 if (CheckExtension ("GL_KHR_debug"))
1364 // this functionality become a part of OpenGL ES 3.2
1366 // According to GL_KHR_debug spec, all functions should have KHR suffix.
1367 // However, some implementations can export these functions without suffix.
1368 if (FindProc ("glDebugMessageControlKHR", myFuncs->glDebugMessageControl)
1369 && FindProc ("glDebugMessageInsertKHR", myFuncs->glDebugMessageInsert)
1370 && FindProc ("glDebugMessageCallbackKHR", myFuncs->glDebugMessageCallback)
1371 && FindProc ("glGetDebugMessageLogKHR", myFuncs->glGetDebugMessageLog))
1373 arbDbg = (OpenGl_ArbDbg* )(&(*myFuncs));
1377 && caps->contextDebug)
1379 // setup default callback
1380 myIsGlDebugCtx = Standard_True;
1381 arbDbg->glDebugMessageCallback (debugCallbackWrap, this);
1382 ::glEnable (GL_DEBUG_OUTPUT);
1383 if (caps->contextSyncDebug)
1385 // note that some broken implementations (e.g. simulators) might generate error message on this call
1386 ::glEnable (GL_DEBUG_OUTPUT_SYNCHRONOUS);
1391 extDrawBuffers = CheckExtension ("GL_EXT_draw_buffers") && FindProc ("glDrawBuffersEXT", myFuncs->glDrawBuffers);
1392 arbDrawBuffers = CheckExtension ("GL_ARB_draw_buffers") && FindProc ("glDrawBuffersARB", myFuncs->glDrawBuffers);
1394 if (IsGlGreaterEqual (3, 0) && FindProc ("glDrawBuffers", myFuncs->glDrawBuffers))
1396 hasDrawBuffers = OpenGl_FeatureInCore;
1398 else if (extDrawBuffers || arbDrawBuffers)
1400 hasDrawBuffers = OpenGl_FeatureInExtensions;
1403 hasFloatBuffer = IsGlGreaterEqual (3, 2) ? OpenGl_FeatureInCore :
1404 CheckExtension ("GL_EXT_color_buffer_float") ? OpenGl_FeatureInExtensions
1405 : OpenGl_FeatureNotAvailable;
1406 hasHalfFloatBuffer = IsGlGreaterEqual (3, 2) ? OpenGl_FeatureInCore :
1407 CheckExtension ("GL_EXT_color_buffer_half_float") ? OpenGl_FeatureInExtensions
1408 : OpenGl_FeatureNotAvailable;
1410 oesSampleVariables = CheckExtension ("GL_OES_sample_variables");
1411 oesStdDerivatives = CheckExtension ("GL_OES_standard_derivatives");
1412 hasSampleVariables = IsGlGreaterEqual (3, 2) ? OpenGl_FeatureInCore :
1413 oesSampleVariables ? OpenGl_FeatureInExtensions
1414 : OpenGl_FeatureNotAvailable;
1417 myTexClamp = IsGlGreaterEqual (1, 2) ? GL_CLAMP_TO_EDGE : GL_CLAMP;
1419 hasTexRGBA8 = Standard_True;
1420 arbDrawBuffers = CheckExtension ("GL_ARB_draw_buffers");
1421 arbNPTW = CheckExtension ("GL_ARB_texture_non_power_of_two");
1422 arbTexFloat = IsGlGreaterEqual (3, 0)
1423 || CheckExtension ("GL_ARB_texture_float");
1424 arbSampleShading = CheckExtension ("GL_ARB_sample_shading");
1425 extBgra = CheckExtension ("GL_EXT_bgra");
1426 extAnis = CheckExtension ("GL_EXT_texture_filter_anisotropic");
1427 extPDS = CheckExtension ("GL_EXT_packed_depth_stencil");
1428 atiMem = CheckExtension ("GL_ATI_meminfo");
1429 nvxMem = CheckExtension ("GL_NVX_gpu_memory_info");
1431 hasDrawBuffers = IsGlGreaterEqual (2, 0) ? OpenGl_FeatureInCore :
1432 arbDrawBuffers ? OpenGl_FeatureInExtensions
1433 : OpenGl_FeatureNotAvailable;
1435 hasFloatBuffer = hasHalfFloatBuffer = IsGlGreaterEqual (3, 0) ? OpenGl_FeatureInCore :
1436 CheckExtension ("GL_ARB_color_buffer_float") ? OpenGl_FeatureInExtensions
1437 : OpenGl_FeatureNotAvailable;
1439 hasSampleVariables = IsGlGreaterEqual (4, 0) ? OpenGl_FeatureInCore :
1440 arbSampleShading ? OpenGl_FeatureInExtensions
1441 : OpenGl_FeatureNotAvailable;
1443 GLint aStereo = GL_FALSE;
1444 glGetIntegerv (GL_STEREO, &aStereo);
1445 myIsStereoBuffers = aStereo == 1;
1447 // get number of maximum clipping planes
1448 glGetIntegerv (GL_MAX_CLIP_PLANES, &myMaxClipPlanes);
1453 glGetIntegerv (GL_MAX_DRAW_BUFFERS, &myMaxDrawBuffers);
1454 glGetIntegerv (GL_MAX_COLOR_ATTACHMENTS, &myMaxColorAttachments);
1457 glGetIntegerv (GL_MAX_TEXTURE_SIZE, &myMaxTexDim);
1458 if (IsGlGreaterEqual (1, 5))
1460 glGetIntegerv (GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &myMaxTexCombined);
1465 glGetIntegerv (GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &myAnisoMax);
1468 myClippingState.Init();
1470 #if !defined(GL_ES_VERSION_2_0)
1488 // retrieve platform-dependent extensions
1489 #if defined(HAVE_EGL)
1491 #elif defined(_WIN32)
1492 if (FindProcShort (wglGetExtensionsStringARB))
1494 const char* aWglExts = myFuncs->wglGetExtensionsStringARB (wglGetCurrentDC());
1495 if (CheckExtension (aWglExts, "WGL_EXT_swap_control"))
1497 FindProcShort (wglSwapIntervalEXT);
1499 if (CheckExtension (aWglExts, "WGL_ARB_pixel_format"))
1501 FindProcShort (wglChoosePixelFormatARB);
1503 if (CheckExtension (aWglExts, "WGL_ARB_create_context_profile"))
1505 FindProcShort (wglCreateContextAttribsARB);
1507 if (CheckExtension (aWglExts, "WGL_NV_DX_interop"))
1509 FindProcShort (wglDXSetResourceShareHandleNV);
1510 FindProcShort (wglDXOpenDeviceNV);
1511 FindProcShort (wglDXCloseDeviceNV);
1512 FindProcShort (wglDXRegisterObjectNV);
1513 FindProcShort (wglDXUnregisterObjectNV);
1514 FindProcShort (wglDXObjectAccessNV);
1515 FindProcShort (wglDXLockObjectsNV);
1516 FindProcShort (wglDXUnlockObjectsNV);
1518 if (CheckExtension (aWglExts, "WGL_AMD_gpu_association"))
1520 FindProcShort (wglGetGPUIDsAMD);
1521 FindProcShort (wglGetGPUInfoAMD);
1522 FindProcShort (wglGetContextGPUIDAMD);
1525 #elif defined(__APPLE__)
1528 const char* aGlxExts = ::glXQueryExtensionsString ((Display* )myDisplay, DefaultScreen ((Display* )myDisplay));
1529 if (CheckExtension (aGlxExts, "GLX_EXT_swap_control"))
1531 FindProcShort (glXSwapIntervalEXT);
1533 if (CheckExtension (aGlxExts, "GLX_SGI_swap_control"))
1535 FindProcShort (glXSwapIntervalSGI);
1537 if (CheckExtension (aGlxExts, "GLX_MESA_query_renderer"))
1539 FindProcShort (glXQueryRendererIntegerMESA);
1540 FindProcShort (glXQueryCurrentRendererIntegerMESA);
1541 FindProcShort (glXQueryRendererStringMESA);
1542 FindProcShort (glXQueryCurrentRendererStringMESA);
1544 //extSwapTear = CheckExtension (aGlxExts, "GLX_EXT_swap_control_tear");
1547 // load OpenGL 1.2 new functions
1548 has12 = IsGlGreaterEqual (1, 2)
1549 && FindProcShort (glBlendColor)
1550 && FindProcShort (glBlendEquation)
1551 && FindProcShort (glDrawRangeElements)
1552 && FindProcShort (glTexImage3D)
1553 && FindProcShort (glTexSubImage3D)
1554 && FindProcShort (glCopyTexSubImage3D);
1556 // load OpenGL 1.3 new functions
1557 has13 = IsGlGreaterEqual (1, 3)
1558 && FindProcShort (glActiveTexture)
1559 && FindProcShort (glSampleCoverage)
1560 && FindProcShort (glCompressedTexImage3D)
1561 && FindProcShort (glCompressedTexImage2D)
1562 && FindProcShort (glCompressedTexImage1D)
1563 && FindProcShort (glCompressedTexSubImage3D)
1564 && FindProcShort (glCompressedTexSubImage2D)
1565 && FindProcShort (glCompressedTexSubImage1D)
1566 && FindProcShort (glGetCompressedTexImage);
1571 && FindProcShort (glClientActiveTexture)
1572 && FindProcShort (glMultiTexCoord1d)
1573 && FindProcShort (glMultiTexCoord1dv)
1574 && FindProcShort (glMultiTexCoord1f)
1575 && FindProcShort (glMultiTexCoord1fv)
1576 && FindProcShort (glMultiTexCoord1i)
1577 && FindProcShort (glMultiTexCoord1iv)
1578 && FindProcShort (glMultiTexCoord1s)
1579 && FindProcShort (glMultiTexCoord1sv)
1580 && FindProcShort (glMultiTexCoord2d)
1581 && FindProcShort (glMultiTexCoord2dv)
1582 && FindProcShort (glMultiTexCoord2f)
1583 && FindProcShort (glMultiTexCoord2fv)
1584 && FindProcShort (glMultiTexCoord2i)
1585 && FindProcShort (glMultiTexCoord2iv)
1586 && FindProcShort (glMultiTexCoord2s)
1587 && FindProcShort (glMultiTexCoord2sv)
1588 && FindProcShort (glMultiTexCoord3d)
1589 && FindProcShort (glMultiTexCoord3dv)
1590 && FindProcShort (glMultiTexCoord3f)
1591 && FindProcShort (glMultiTexCoord3fv)
1592 && FindProcShort (glMultiTexCoord3i)
1593 && FindProcShort (glMultiTexCoord3iv)
1594 && FindProcShort (glMultiTexCoord3s)
1595 && FindProcShort (glMultiTexCoord3sv)
1596 && FindProcShort (glMultiTexCoord4d)
1597 && FindProcShort (glMultiTexCoord4dv)
1598 && FindProcShort (glMultiTexCoord4f)
1599 && FindProcShort (glMultiTexCoord4fv)
1600 && FindProcShort (glMultiTexCoord4i)
1601 && FindProcShort (glMultiTexCoord4iv)
1602 && FindProcShort (glMultiTexCoord4s)
1603 && FindProcShort (glMultiTexCoord4sv)
1604 && FindProcShort (glLoadTransposeMatrixf)
1605 && FindProcShort (glLoadTransposeMatrixd)
1606 && FindProcShort (glMultTransposeMatrixf)
1607 && FindProcShort (glMultTransposeMatrixd);
1610 // load OpenGL 1.4 new functions
1611 has14 = IsGlGreaterEqual (1, 4)
1612 && FindProcShort (glBlendFuncSeparate)
1613 && FindProcShort (glMultiDrawArrays)
1614 && FindProcShort (glMultiDrawElements)
1615 && FindProcShort (glPointParameterf)
1616 && FindProcShort (glPointParameterfv)
1617 && FindProcShort (glPointParameteri)
1618 && FindProcShort (glPointParameteriv);
1620 // load OpenGL 1.5 new functions
1621 has15 = IsGlGreaterEqual (1, 5)
1622 && FindProcShort (glGenQueries)
1623 && FindProcShort (glDeleteQueries)
1624 && FindProcShort (glIsQuery)
1625 && FindProcShort (glBeginQuery)
1626 && FindProcShort (glEndQuery)
1627 && FindProcShort (glGetQueryiv)
1628 && FindProcShort (glGetQueryObjectiv)
1629 && FindProcShort (glGetQueryObjectuiv)
1630 && FindProcShort (glBindBuffer)
1631 && FindProcShort (glDeleteBuffers)
1632 && FindProcShort (glGenBuffers)
1633 && FindProcShort (glIsBuffer)
1634 && FindProcShort (glBufferData)
1635 && FindProcShort (glBufferSubData)
1636 && FindProcShort (glGetBufferSubData)
1637 && FindProcShort (glMapBuffer)
1638 && FindProcShort (glUnmapBuffer)
1639 && FindProcShort (glGetBufferParameteriv)
1640 && FindProcShort (glGetBufferPointerv);
1642 // load OpenGL 2.0 new functions
1643 has20 = IsGlGreaterEqual (2, 0)
1644 && FindProcShort (glBlendEquationSeparate)
1645 && FindProcShort (glDrawBuffers)
1646 && FindProcShort (glStencilOpSeparate)
1647 && FindProcShort (glStencilFuncSeparate)
1648 && FindProcShort (glStencilMaskSeparate)
1649 && FindProcShort (glAttachShader)
1650 && FindProcShort (glBindAttribLocation)
1651 && FindProcShort (glCompileShader)
1652 && FindProcShort (glCreateProgram)
1653 && FindProcShort (glCreateShader)
1654 && FindProcShort (glDeleteProgram)
1655 && FindProcShort (glDeleteShader)
1656 && FindProcShort (glDetachShader)
1657 && FindProcShort (glDisableVertexAttribArray)
1658 && FindProcShort (glEnableVertexAttribArray)
1659 && FindProcShort (glGetActiveAttrib)
1660 && FindProcShort (glGetActiveUniform)
1661 && FindProcShort (glGetAttachedShaders)
1662 && FindProcShort (glGetAttribLocation)
1663 && FindProcShort (glGetProgramiv)
1664 && FindProcShort (glGetProgramInfoLog)
1665 && FindProcShort (glGetShaderiv)
1666 && FindProcShort (glGetShaderInfoLog)
1667 && FindProcShort (glGetShaderSource)
1668 && FindProcShort (glGetUniformLocation)
1669 && FindProcShort (glGetUniformfv)
1670 && FindProcShort (glGetUniformiv)
1671 && FindProcShort (glGetVertexAttribdv)
1672 && FindProcShort (glGetVertexAttribfv)
1673 && FindProcShort (glGetVertexAttribiv)
1674 && FindProcShort (glGetVertexAttribPointerv)
1675 && FindProcShort (glIsProgram)
1676 && FindProcShort (glIsShader)
1677 && FindProcShort (glLinkProgram)
1678 && FindProcShort (glShaderSource)
1679 && FindProcShort (glUseProgram)
1680 && FindProcShort (glUniform1f)
1681 && FindProcShort (glUniform2f)
1682 && FindProcShort (glUniform3f)
1683 && FindProcShort (glUniform4f)
1684 && FindProcShort (glUniform1i)
1685 && FindProcShort (glUniform2i)
1686 && FindProcShort (glUniform3i)
1687 && FindProcShort (glUniform4i)
1688 && FindProcShort (glUniform1fv)
1689 && FindProcShort (glUniform2fv)
1690 && FindProcShort (glUniform3fv)
1691 && FindProcShort (glUniform4fv)
1692 && FindProcShort (glUniform1iv)
1693 && FindProcShort (glUniform2iv)
1694 && FindProcShort (glUniform3iv)
1695 && FindProcShort (glUniform4iv)
1696 && FindProcShort (glUniformMatrix2fv)
1697 && FindProcShort (glUniformMatrix3fv)
1698 && FindProcShort (glUniformMatrix4fv)
1699 && FindProcShort (glValidateProgram)
1700 && FindProcShort (glVertexAttrib1d)
1701 && FindProcShort (glVertexAttrib1dv)
1702 && FindProcShort (glVertexAttrib1f)
1703 && FindProcShort (glVertexAttrib1fv)
1704 && FindProcShort (glVertexAttrib1s)
1705 && FindProcShort (glVertexAttrib1sv)
1706 && FindProcShort (glVertexAttrib2d)
1707 && FindProcShort (glVertexAttrib2dv)
1708 && FindProcShort (glVertexAttrib2f)
1709 && FindProcShort (glVertexAttrib2fv)
1710 && FindProcShort (glVertexAttrib2s)
1711 && FindProcShort (glVertexAttrib2sv)
1712 && FindProcShort (glVertexAttrib3d)
1713 && FindProcShort (glVertexAttrib3dv)
1714 && FindProcShort (glVertexAttrib3f)
1715 && FindProcShort (glVertexAttrib3fv)
1716 && FindProcShort (glVertexAttrib3s)
1717 && FindProcShort (glVertexAttrib3sv)
1718 && FindProcShort (glVertexAttrib4Nbv)
1719 && FindProcShort (glVertexAttrib4Niv)
1720 && FindProcShort (glVertexAttrib4Nsv)
1721 && FindProcShort (glVertexAttrib4Nub)
1722 && FindProcShort (glVertexAttrib4Nubv)
1723 && FindProcShort (glVertexAttrib4Nuiv)
1724 && FindProcShort (glVertexAttrib4Nusv)
1725 && FindProcShort (glVertexAttrib4bv)
1726 && FindProcShort (glVertexAttrib4d)
1727 && FindProcShort (glVertexAttrib4dv)
1728 && FindProcShort (glVertexAttrib4f)
1729 && FindProcShort (glVertexAttrib4fv)
1730 && FindProcShort (glVertexAttrib4iv)
1731 && FindProcShort (glVertexAttrib4s)
1732 && FindProcShort (glVertexAttrib4sv)
1733 && FindProcShort (glVertexAttrib4ubv)
1734 && FindProcShort (glVertexAttrib4uiv)
1735 && FindProcShort (glVertexAttrib4usv)
1736 && FindProcShort (glVertexAttribPointer);
1738 // load OpenGL 2.1 new functions
1739 has21 = IsGlGreaterEqual (2, 1)
1740 && FindProcShort (glUniformMatrix2x3fv)
1741 && FindProcShort (glUniformMatrix3x2fv)
1742 && FindProcShort (glUniformMatrix2x4fv)
1743 && FindProcShort (glUniformMatrix4x2fv)
1744 && FindProcShort (glUniformMatrix3x4fv)
1745 && FindProcShort (glUniformMatrix4x3fv);
1747 // load GL_ARB_framebuffer_object (added to OpenGL 3.0 core)
1748 const bool hasFBO = (IsGlGreaterEqual (3, 0) || CheckExtension ("GL_ARB_framebuffer_object"))
1749 && FindProcShort (glIsRenderbuffer)
1750 && FindProcShort (glBindRenderbuffer)
1751 && FindProcShort (glDeleteRenderbuffers)
1752 && FindProcShort (glGenRenderbuffers)
1753 && FindProcShort (glRenderbufferStorage)
1754 && FindProcShort (glGetRenderbufferParameteriv)
1755 && FindProcShort (glIsFramebuffer)
1756 && FindProcShort (glBindFramebuffer)
1757 && FindProcShort (glDeleteFramebuffers)
1758 && FindProcShort (glGenFramebuffers)
1759 && FindProcShort (glCheckFramebufferStatus)
1760 && FindProcShort (glFramebufferTexture1D)
1761 && FindProcShort (glFramebufferTexture2D)
1762 && FindProcShort (glFramebufferTexture3D)
1763 && FindProcShort (glFramebufferRenderbuffer)
1764 && FindProcShort (glGetFramebufferAttachmentParameteriv)
1765 && FindProcShort (glGenerateMipmap)
1766 && FindProcShort (glBlitFramebuffer)
1767 && FindProcShort (glRenderbufferStorageMultisample)
1768 && FindProcShort (glFramebufferTextureLayer);
1770 // load GL_ARB_vertex_array_object (added to OpenGL 3.0 core)
1771 const bool hasVAO = (IsGlGreaterEqual (3, 0) || CheckExtension ("GL_ARB_vertex_array_object"))
1772 && FindProcShort (glBindVertexArray)
1773 && FindProcShort (glDeleteVertexArrays)
1774 && FindProcShort (glGenVertexArrays)
1775 && FindProcShort (glIsVertexArray);
1777 // load GL_ARB_map_buffer_range (added to OpenGL 3.0 core)
1778 const bool hasMapBufferRange = (IsGlGreaterEqual (3, 0) || CheckExtension ("GL_ARB_map_buffer_range"))
1779 && FindProcShort (glMapBufferRange)
1780 && FindProcShort (glFlushMappedBufferRange);
1782 // load OpenGL 3.0 new functions
1783 has30 = IsGlGreaterEqual (3, 0)
1786 && hasMapBufferRange
1787 && FindProcShort (glColorMaski)
1788 && FindProcShort (glGetBooleani_v)
1789 && FindProcShort (glGetIntegeri_v)
1790 && FindProcShort (glEnablei)
1791 && FindProcShort (glDisablei)
1792 && FindProcShort (glIsEnabledi)
1793 && FindProcShort (glBeginTransformFeedback)
1794 && FindProcShort (glEndTransformFeedback)
1795 && FindProcShort (glBindBufferRange)
1796 && FindProcShort (glBindBufferBase)
1797 && FindProcShort (glTransformFeedbackVaryings)
1798 && FindProcShort (glGetTransformFeedbackVarying)
1799 && FindProcShort (glClampColor)
1800 && FindProcShort (glBeginConditionalRender)
1801 && FindProcShort (glEndConditionalRender)
1802 && FindProcShort (glVertexAttribIPointer)
1803 && FindProcShort (glGetVertexAttribIiv)
1804 && FindProcShort (glGetVertexAttribIuiv)
1805 && FindProcShort (glVertexAttribI1i)
1806 && FindProcShort (glVertexAttribI2i)
1807 && FindProcShort (glVertexAttribI3i)
1808 && FindProcShort (glVertexAttribI4i)
1809 && FindProcShort (glVertexAttribI1ui)
1810 && FindProcShort (glVertexAttribI2ui)
1811 && FindProcShort (glVertexAttribI3ui)
1812 && FindProcShort (glVertexAttribI4ui)
1813 && FindProcShort (glVertexAttribI1iv)
1814 && FindProcShort (glVertexAttribI2iv)
1815 && FindProcShort (glVertexAttribI3iv)
1816 && FindProcShort (glVertexAttribI4iv)
1817 && FindProcShort (glVertexAttribI1uiv)
1818 && FindProcShort (glVertexAttribI2uiv)
1819 && FindProcShort (glVertexAttribI3uiv)
1820 && FindProcShort (glVertexAttribI4uiv)
1821 && FindProcShort (glVertexAttribI4bv)
1822 && FindProcShort (glVertexAttribI4sv)
1823 && FindProcShort (glVertexAttribI4ubv)
1824 && FindProcShort (glVertexAttribI4usv)
1825 && FindProcShort (glGetUniformuiv)
1826 && FindProcShort (glBindFragDataLocation)
1827 && FindProcShort (glGetFragDataLocation)
1828 && FindProcShort (glUniform1ui)
1829 && FindProcShort (glUniform2ui)
1830 && FindProcShort (glUniform3ui)
1831 && FindProcShort (glUniform4ui)
1832 && FindProcShort (glUniform1uiv)
1833 && FindProcShort (glUniform2uiv)
1834 && FindProcShort (glUniform3uiv)
1835 && FindProcShort (glUniform4uiv)
1836 && FindProcShort (glTexParameterIiv)
1837 && FindProcShort (glTexParameterIuiv)
1838 && FindProcShort (glGetTexParameterIiv)
1839 && FindProcShort (glGetTexParameterIuiv)
1840 && FindProcShort (glClearBufferiv)
1841 && FindProcShort (glClearBufferuiv)
1842 && FindProcShort (glClearBufferfv)
1843 && FindProcShort (glClearBufferfi)
1844 && FindProcShort (glGetStringi);
1846 // load GL_ARB_uniform_buffer_object (added to OpenGL 3.1 core)
1847 const bool hasUBO = (IsGlGreaterEqual (3, 1) || CheckExtension ("GL_ARB_uniform_buffer_object"))
1848 && FindProcShort (glGetUniformIndices)
1849 && FindProcShort (glGetActiveUniformsiv)
1850 && FindProcShort (glGetActiveUniformName)
1851 && FindProcShort (glGetUniformBlockIndex)
1852 && FindProcShort (glGetActiveUniformBlockiv)
1853 && FindProcShort (glGetActiveUniformBlockName)
1854 && FindProcShort (glUniformBlockBinding);
1856 // load GL_ARB_copy_buffer (added to OpenGL 3.1 core)
1857 const bool hasCopyBufSubData = (IsGlGreaterEqual (3, 1) || CheckExtension ("GL_ARB_copy_buffer"))
1858 && FindProcShort (glCopyBufferSubData);
1862 // NPOT textures are required by OpenGL 2.0 specifications
1863 // but doesn't hardware accelerated by some ancient OpenGL 2.1 hardware (GeForce FX, RadeOn 9700 etc.)
1864 arbNPTW = Standard_True;
1865 arbTexRG = Standard_True;
1868 // load OpenGL 3.1 new functions
1869 has31 = IsGlGreaterEqual (3, 1)
1871 && hasCopyBufSubData
1872 && FindProcShort (glDrawArraysInstanced)
1873 && FindProcShort (glDrawElementsInstanced)
1874 && FindProcShort (glTexBuffer)
1875 && FindProcShort (glPrimitiveRestartIndex);
1877 // load GL_ARB_draw_elements_base_vertex (added to OpenGL 3.2 core)
1878 const bool hasDrawElemsBaseVert = (IsGlGreaterEqual (3, 2) || CheckExtension ("GL_ARB_draw_elements_base_vertex"))
1879 && FindProcShort (glDrawElementsBaseVertex)
1880 && FindProcShort (glDrawRangeElementsBaseVertex)
1881 && FindProcShort (glDrawElementsInstancedBaseVertex)
1882 && FindProcShort (glMultiDrawElementsBaseVertex);
1884 // load GL_ARB_provoking_vertex (added to OpenGL 3.2 core)
1885 const bool hasProvokingVert = (IsGlGreaterEqual (3, 2) || CheckExtension ("GL_ARB_provoking_vertex"))
1886 && FindProcShort (glProvokingVertex);
1888 // load GL_ARB_sync (added to OpenGL 3.2 core)
1889 const bool hasSync = (IsGlGreaterEqual (3, 2) || CheckExtension ("GL_ARB_sync"))
1890 && FindProcShort (glFenceSync)
1891 && FindProcShort (glIsSync)
1892 && FindProcShort (glDeleteSync)
1893 && FindProcShort (glClientWaitSync)
1894 && FindProcShort (glWaitSync)
1895 && FindProcShort (glGetInteger64v)
1896 && FindProcShort (glGetSynciv);
1898 // load GL_ARB_texture_multisample (added to OpenGL 3.2 core)
1899 const bool hasTextureMultisample = (IsGlGreaterEqual (3, 2) || CheckExtension ("GL_ARB_texture_multisample"))
1900 && FindProcShort (glTexImage2DMultisample)
1901 && FindProcShort (glTexImage3DMultisample)
1902 && FindProcShort (glGetMultisamplefv)
1903 && FindProcShort (glSampleMaski);
1905 // load OpenGL 3.2 new functions
1906 has32 = IsGlGreaterEqual (3, 2)
1907 && hasDrawElemsBaseVert
1910 && hasTextureMultisample
1911 && FindProcShort (glGetInteger64i_v)
1912 && FindProcShort (glGetBufferParameteri64v)
1913 && FindProcShort (glFramebufferTexture);
1915 // load GL_ARB_blend_func_extended (added to OpenGL 3.3 core)
1916 const bool hasBlendFuncExtended = (IsGlGreaterEqual (3, 3) || CheckExtension ("GL_ARB_blend_func_extended"))
1917 && FindProcShort (glBindFragDataLocationIndexed)
1918 && FindProcShort (glGetFragDataIndex);
1920 // load GL_ARB_sampler_objects (added to OpenGL 3.3 core)
1921 const bool hasSamplerObjects = (IsGlGreaterEqual (3, 3) || CheckExtension ("GL_ARB_sampler_objects"))
1922 && FindProcShort (glGenSamplers)
1923 && FindProcShort (glDeleteSamplers)
1924 && FindProcShort (glIsSampler)
1925 && FindProcShort (glBindSampler)
1926 && FindProcShort (glSamplerParameteri)
1927 && FindProcShort (glSamplerParameteriv)
1928 && FindProcShort (glSamplerParameterf)
1929 && FindProcShort (glSamplerParameterfv)
1930 && FindProcShort (glSamplerParameterIiv)
1931 && FindProcShort (glSamplerParameterIuiv)
1932 && FindProcShort (glGetSamplerParameteriv)
1933 && FindProcShort (glGetSamplerParameterIiv)
1934 && FindProcShort (glGetSamplerParameterfv)
1935 && FindProcShort (glGetSamplerParameterIuiv);
1936 if (hasSamplerObjects)
1938 arbSamplerObject = (OpenGl_ArbSamplerObject* )(&(*myFuncs));
1941 // load GL_ARB_timer_query (added to OpenGL 3.3 core)
1942 const bool hasTimerQuery = (IsGlGreaterEqual (3, 3) || CheckExtension ("GL_ARB_timer_query"))
1943 && FindProcShort (glQueryCounter)
1944 && FindProcShort (glGetQueryObjecti64v)
1945 && FindProcShort (glGetQueryObjectui64v);
1947 // load GL_ARB_vertex_type_2_10_10_10_rev (added to OpenGL 3.3 core)
1948 const bool hasVertType21010101rev = (IsGlGreaterEqual (3, 3) || CheckExtension ("GL_ARB_vertex_type_2_10_10_10_rev"))
1949 && FindProcShort (glVertexAttribP1ui)
1950 && FindProcShort (glVertexAttribP1uiv)
1951 && FindProcShort (glVertexAttribP2ui)
1952 && FindProcShort (glVertexAttribP2uiv)
1953 && FindProcShort (glVertexAttribP3ui)
1954 && FindProcShort (glVertexAttribP3uiv)
1955 && FindProcShort (glVertexAttribP4ui)
1956 && FindProcShort (glVertexAttribP4uiv);
1958 if ( hasVertType21010101rev
1961 // load deprecated functions
1962 const bool hasVertType21010101revExt =
1963 FindProcShort (glVertexP2ui)
1964 && FindProcShort (glVertexP2uiv)
1965 && FindProcShort (glVertexP3ui)
1966 && FindProcShort (glVertexP3uiv)
1967 && FindProcShort (glVertexP4ui)
1968 && FindProcShort (glVertexP4uiv)
1969 && FindProcShort (glTexCoordP1ui)
1970 && FindProcShort (glTexCoordP1uiv)
1971 && FindProcShort (glTexCoordP2ui)
1972 && FindProcShort (glTexCoordP2uiv)
1973 && FindProcShort (glTexCoordP3ui)
1974 && FindProcShort (glTexCoordP3uiv)
1975 && FindProcShort (glTexCoordP4ui)
1976 && FindProcShort (glTexCoordP4uiv)
1977 && FindProcShort (glMultiTexCoordP1ui)
1978 && FindProcShort (glMultiTexCoordP1uiv)
1979 && FindProcShort (glMultiTexCoordP2ui)
1980 && FindProcShort (glMultiTexCoordP2uiv)
1981 && FindProcShort (glMultiTexCoordP3ui)
1982 && FindProcShort (glMultiTexCoordP3uiv)
1983 && FindProcShort (glMultiTexCoordP4ui)
1984 && FindProcShort (glMultiTexCoordP4uiv)
1985 && FindProcShort (glNormalP3ui)
1986 && FindProcShort (glNormalP3uiv)
1987 && FindProcShort (glColorP3ui)
1988 && FindProcShort (glColorP3uiv)
1989 && FindProcShort (glColorP4ui)
1990 && FindProcShort (glColorP4uiv)
1991 && FindProcShort (glSecondaryColorP3ui)
1992 && FindProcShort (glSecondaryColorP3uiv);
1993 (void )hasVertType21010101revExt;
1996 // load OpenGL 3.3 extra functions
1997 has33 = IsGlGreaterEqual (3, 3)
1998 && hasBlendFuncExtended
1999 && hasSamplerObjects
2001 && hasVertType21010101rev
2002 && FindProcShort (glVertexAttribDivisor);
2004 // load GL_ARB_draw_indirect (added to OpenGL 4.0 core)
2005 const bool hasDrawIndirect = (IsGlGreaterEqual (4, 0) || CheckExtension ("GL_ARB_draw_indirect"))
2006 && FindProcShort (glDrawArraysIndirect)
2007 && FindProcShort (glDrawElementsIndirect);
2009 // load GL_ARB_gpu_shader_fp64 (added to OpenGL 4.0 core)
2010 const bool hasShaderFP64 = (IsGlGreaterEqual (4, 0) || CheckExtension ("GL_ARB_gpu_shader_fp64"))
2011 && FindProcShort (glUniform1d)
2012 && FindProcShort (glUniform2d)
2013 && FindProcShort (glUniform3d)
2014 && FindProcShort (glUniform4d)
2015 && FindProcShort (glUniform1dv)
2016 && FindProcShort (glUniform2dv)
2017 && FindProcShort (glUniform3dv)
2018 && FindProcShort (glUniform4dv)
2019 && FindProcShort (glUniformMatrix2dv)
2020 && FindProcShort (glUniformMatrix3dv)
2021 && FindProcShort (glUniformMatrix4dv)
2022 && FindProcShort (glUniformMatrix2x3dv)
2023 && FindProcShort (glUniformMatrix2x4dv)
2024 && FindProcShort (glUniformMatrix3x2dv)
2025 && FindProcShort (glUniformMatrix3x4dv)
2026 && FindProcShort (glUniformMatrix4x2dv)
2027 && FindProcShort (glUniformMatrix4x3dv)
2028 && FindProcShort (glGetUniformdv);
2030 // load GL_ARB_shader_subroutine (added to OpenGL 4.0 core)
2031 const bool hasShaderSubroutine = (IsGlGreaterEqual (4, 0) || CheckExtension ("GL_ARB_shader_subroutine"))
2032 && FindProcShort (glGetSubroutineUniformLocation)
2033 && FindProcShort (glGetSubroutineIndex)
2034 && FindProcShort (glGetActiveSubroutineUniformiv)
2035 && FindProcShort (glGetActiveSubroutineUniformName)
2036 && FindProcShort (glGetActiveSubroutineName)
2037 && FindProcShort (glUniformSubroutinesuiv)
2038 && FindProcShort (glGetUniformSubroutineuiv)
2039 && FindProcShort (glGetProgramStageiv);
2041 // load GL_ARB_tessellation_shader (added to OpenGL 4.0 core)
2042 const bool hasTessellationShader = (IsGlGreaterEqual (4, 0) || CheckExtension ("GL_ARB_tessellation_shader"))
2043 && FindProcShort (glPatchParameteri)
2044 && FindProcShort (glPatchParameterfv);
2046 // load GL_ARB_transform_feedback2 (added to OpenGL 4.0 core)
2047 const bool hasTrsfFeedback2 = (IsGlGreaterEqual (4, 0) || CheckExtension ("GL_ARB_transform_feedback2"))
2048 && FindProcShort (glBindTransformFeedback)
2049 && FindProcShort (glDeleteTransformFeedbacks)
2050 && FindProcShort (glGenTransformFeedbacks)
2051 && FindProcShort (glIsTransformFeedback)
2052 && FindProcShort (glPauseTransformFeedback)
2053 && FindProcShort (glResumeTransformFeedback)
2054 && FindProcShort (glDrawTransformFeedback);
2056 // load GL_ARB_transform_feedback3 (added to OpenGL 4.0 core)
2057 const bool hasTrsfFeedback3 = (IsGlGreaterEqual (4, 0) || CheckExtension ("GL_ARB_transform_feedback3"))
2058 && FindProcShort (glDrawTransformFeedbackStream)
2059 && FindProcShort (glBeginQueryIndexed)
2060 && FindProcShort (glEndQueryIndexed)
2061 && FindProcShort (glGetQueryIndexediv);
2063 // load OpenGL 4.0 new functions
2064 has40 = IsGlGreaterEqual (4, 0)
2067 && hasShaderSubroutine
2068 && hasTessellationShader
2071 && FindProcShort (glMinSampleShading)
2072 && FindProcShort (glBlendEquationi)
2073 && FindProcShort (glBlendEquationSeparatei)
2074 && FindProcShort (glBlendFunci)
2075 && FindProcShort (glBlendFuncSeparatei);
2077 // load GL_ARB_ES2_compatibility (added to OpenGL 4.1 core)
2078 const bool hasES2Compatibility = (IsGlGreaterEqual (4, 1) || CheckExtension ("GL_ARB_ES2_compatibility"))
2079 && FindProcShort (glReleaseShaderCompiler)
2080 && FindProcShort (glShaderBinary)
2081 && FindProcShort (glGetShaderPrecisionFormat)
2082 && FindProcShort (glDepthRangef)
2083 && FindProcShort (glClearDepthf);
2085 // load GL_ARB_get_program_binary (added to OpenGL 4.1 core)
2086 const bool hasGetProgramBinary = (IsGlGreaterEqual (4, 1) || CheckExtension ("GL_ARB_get_program_binary"))
2087 && FindProcShort (glGetProgramBinary)
2088 && FindProcShort (glProgramBinary)
2089 && FindProcShort (glProgramParameteri);
2092 // load GL_ARB_separate_shader_objects (added to OpenGL 4.1 core)
2093 const bool hasSeparateShaderObjects = (IsGlGreaterEqual (4, 1) || CheckExtension ("GL_ARB_separate_shader_objects"))
2094 && FindProcShort (glUseProgramStages)
2095 && FindProcShort (glActiveShaderProgram)
2096 && FindProcShort (glCreateShaderProgramv)
2097 && FindProcShort (glBindProgramPipeline)
2098 && FindProcShort (glDeleteProgramPipelines)
2099 && FindProcShort (glGenProgramPipelines)
2100 && FindProcShort (glIsProgramPipeline)
2101 && FindProcShort (glGetProgramPipelineiv)
2102 && FindProcShort (glProgramUniform1i)
2103 && FindProcShort (glProgramUniform1iv)
2104 && FindProcShort (glProgramUniform1f)
2105 && FindProcShort (glProgramUniform1fv)
2106 && FindProcShort (glProgramUniform1d)
2107 && FindProcShort (glProgramUniform1dv)
2108 && FindProcShort (glProgramUniform1ui)
2109 && FindProcShort (glProgramUniform1uiv)
2110 && FindProcShort (glProgramUniform2i)
2111 && FindProcShort (glProgramUniform2iv)
2112 && FindProcShort (glProgramUniform2f)
2113 && FindProcShort (glProgramUniform2fv)
2114 && FindProcShort (glProgramUniform2d)
2115 && FindProcShort (glProgramUniform2dv)
2116 && FindProcShort (glProgramUniform2ui)
2117 && FindProcShort (glProgramUniform2uiv)
2118 && FindProcShort (glProgramUniform3i)
2119 && FindProcShort (glProgramUniform3iv)
2120 && FindProcShort (glProgramUniform3f)
2121 && FindProcShort (glProgramUniform3fv)
2122 && FindProcShort (glProgramUniform3d)
2123 && FindProcShort (glProgramUniform3dv)
2124 && FindProcShort (glProgramUniform3ui)
2125 && FindProcShort (glProgramUniform3uiv)
2126 && FindProcShort (glProgramUniform4i)
2127 && FindProcShort (glProgramUniform4iv)
2128 && FindProcShort (glProgramUniform4f)
2129 && FindProcShort (glProgramUniform4fv)
2130 && FindProcShort (glProgramUniform4d)
2131 && FindProcShort (glProgramUniform4dv)
2132 && FindProcShort (glProgramUniform4ui)
2133 && FindProcShort (glProgramUniform4uiv)
2134 && FindProcShort (glProgramUniformMatrix2fv)
2135 && FindProcShort (glProgramUniformMatrix3fv)
2136 && FindProcShort (glProgramUniformMatrix4fv)
2137 && FindProcShort (glProgramUniformMatrix2dv)
2138 && FindProcShort (glProgramUniformMatrix3dv)
2139 && FindProcShort (glProgramUniformMatrix4dv)
2140 && FindProcShort (glProgramUniformMatrix2x3fv)
2141 && FindProcShort (glProgramUniformMatrix3x2fv)
2142 && FindProcShort (glProgramUniformMatrix2x4fv)
2143 && FindProcShort (glProgramUniformMatrix4x2fv)
2144 && FindProcShort (glProgramUniformMatrix3x4fv)
2145 && FindProcShort (glProgramUniformMatrix4x3fv)
2146 && FindProcShort (glProgramUniformMatrix2x3dv)
2147 && FindProcShort (glProgramUniformMatrix3x2dv)
2148 && FindProcShort (glProgramUniformMatrix2x4dv)
2149 && FindProcShort (glProgramUniformMatrix4x2dv)
2150 && FindProcShort (glProgramUniformMatrix3x4dv)
2151 && FindProcShort (glProgramUniformMatrix4x3dv)
2152 && FindProcShort (glValidateProgramPipeline)
2153 && FindProcShort (glGetProgramPipelineInfoLog);
2155 // load GL_ARB_vertex_attrib_64bit (added to OpenGL 4.1 core)
2156 const bool hasVertAttrib64bit = (IsGlGreaterEqual (4, 1) || CheckExtension ("GL_ARB_vertex_attrib_64bit"))
2157 && FindProcShort (glVertexAttribL1d)
2158 && FindProcShort (glVertexAttribL2d)
2159 && FindProcShort (glVertexAttribL3d)
2160 && FindProcShort (glVertexAttribL4d)
2161 && FindProcShort (glVertexAttribL1dv)
2162 && FindProcShort (glVertexAttribL2dv)
2163 && FindProcShort (glVertexAttribL3dv)
2164 && FindProcShort (glVertexAttribL4dv)
2165 && FindProcShort (glVertexAttribLPointer)
2166 && FindProcShort (glGetVertexAttribLdv);
2168 // load GL_ARB_viewport_array (added to OpenGL 4.1 core)
2169 const bool hasViewportArray = (IsGlGreaterEqual (4, 1) || CheckExtension ("GL_ARB_viewport_array"))
2170 && FindProcShort (glViewportArrayv)
2171 && FindProcShort (glViewportIndexedf)
2172 && FindProcShort (glViewportIndexedfv)
2173 && FindProcShort (glScissorArrayv)
2174 && FindProcShort (glScissorIndexed)
2175 && FindProcShort (glScissorIndexedv)
2176 && FindProcShort (glDepthRangeArrayv)
2177 && FindProcShort (glDepthRangeIndexed)
2178 && FindProcShort (glGetFloati_v)
2179 && FindProcShort (glGetDoublei_v);
2181 has41 = IsGlGreaterEqual (4, 1)
2182 && hasES2Compatibility
2183 && hasGetProgramBinary
2184 && hasSeparateShaderObjects
2185 && hasVertAttrib64bit
2186 && hasViewportArray;
2188 // load GL_ARB_base_instance (added to OpenGL 4.2 core)
2189 const bool hasBaseInstance = (IsGlGreaterEqual (4, 2) || CheckExtension ("GL_ARB_base_instance"))
2190 && FindProcShort (glDrawArraysInstancedBaseInstance)
2191 && FindProcShort (glDrawElementsInstancedBaseInstance)
2192 && FindProcShort (glDrawElementsInstancedBaseVertexBaseInstance);
2194 // load GL_ARB_transform_feedback_instanced (added to OpenGL 4.2 core)
2195 const bool hasTrsfFeedbackInstanced = (IsGlGreaterEqual (4, 2) || CheckExtension ("GL_ARB_transform_feedback_instanced"))
2196 && FindProcShort (glDrawTransformFeedbackInstanced)
2197 && FindProcShort (glDrawTransformFeedbackStreamInstanced);
2199 // load GL_ARB_internalformat_query (added to OpenGL 4.2 core)
2200 const bool hasInternalFormatQuery = (IsGlGreaterEqual (4, 2) || CheckExtension ("GL_ARB_internalformat_query"))
2201 && FindProcShort (glGetInternalformativ);
2203 // load GL_ARB_shader_atomic_counters (added to OpenGL 4.2 core)
2204 const bool hasShaderAtomicCounters = (IsGlGreaterEqual (4, 2) || CheckExtension ("GL_ARB_shader_atomic_counters"))
2205 && FindProcShort (glGetActiveAtomicCounterBufferiv);
2207 // load GL_ARB_shader_image_load_store (added to OpenGL 4.2 core)
2208 const bool hasShaderImgLoadStore = (IsGlGreaterEqual (4, 2) || CheckExtension ("GL_ARB_shader_image_load_store"))
2209 && FindProcShort (glBindImageTexture)
2210 && FindProcShort (glMemoryBarrier);
2212 // load GL_ARB_texture_storage (added to OpenGL 4.2 core)
2213 const bool hasTextureStorage = (IsGlGreaterEqual (4, 2) || CheckExtension ("GL_ARB_texture_storage"))
2214 && FindProcShort (glTexStorage1D)
2215 && FindProcShort (glTexStorage2D)
2216 && FindProcShort (glTexStorage3D);
2218 has42 = IsGlGreaterEqual (4, 2)
2220 && hasTrsfFeedbackInstanced
2221 && hasInternalFormatQuery
2222 && hasShaderAtomicCounters
2223 && hasShaderImgLoadStore
2224 && hasTextureStorage;
2226 has43 = IsGlGreaterEqual (4, 3)
2227 && FindProcShort (glClearBufferData)
2228 && FindProcShort (glClearBufferSubData)
2229 && FindProcShort (glDispatchCompute)
2230 && FindProcShort (glDispatchComputeIndirect)
2231 && FindProcShort (glCopyImageSubData)
2232 && FindProcShort (glFramebufferParameteri)
2233 && FindProcShort (glGetFramebufferParameteriv)
2234 && FindProcShort (glGetInternalformati64v)
2235 && FindProcShort (glInvalidateTexSubImage)
2236 && FindProcShort (glInvalidateTexImage)
2237 && FindProcShort (glInvalidateBufferSubData)
2238 && FindProcShort (glInvalidateBufferData)
2239 && FindProcShort (glInvalidateFramebuffer)
2240 && FindProcShort (glInvalidateSubFramebuffer)
2241 && FindProcShort (glMultiDrawArraysIndirect)
2242 && FindProcShort (glMultiDrawElementsIndirect)
2243 && FindProcShort (glGetProgramInterfaceiv)
2244 && FindProcShort (glGetProgramResourceIndex)
2245 && FindProcShort (glGetProgramResourceName)
2246 && FindProcShort (glGetProgramResourceiv)
2247 && FindProcShort (glGetProgramResourceLocation)
2248 && FindProcShort (glGetProgramResourceLocationIndex)
2249 && FindProcShort (glShaderStorageBlockBinding)
2250 && FindProcShort (glTexBufferRange)
2251 && FindProcShort (glTexStorage2DMultisample)
2252 && FindProcShort (glTexStorage3DMultisample)
2253 && FindProcShort (glTextureView)
2254 && FindProcShort (glBindVertexBuffer)
2255 && FindProcShort (glVertexAttribFormat)
2256 && FindProcShort (glVertexAttribIFormat)
2257 && FindProcShort (glVertexAttribLFormat)
2258 && FindProcShort (glVertexAttribBinding)
2259 && FindProcShort (glVertexBindingDivisor)
2260 && FindProcShort (glDebugMessageControl)
2261 && FindProcShort (glDebugMessageInsert)
2262 && FindProcShort (glDebugMessageCallback)
2263 && FindProcShort (glGetDebugMessageLog)
2264 && FindProcShort (glPushDebugGroup)
2265 && FindProcShort (glPopDebugGroup)
2266 && FindProcShort (glObjectLabel)
2267 && FindProcShort (glGetObjectLabel)
2268 && FindProcShort (glObjectPtrLabel)
2269 && FindProcShort (glGetObjectPtrLabel);
2271 // load GL_ARB_clear_texture (added to OpenGL 4.4 core)
2272 bool arbTexClear = (IsGlGreaterEqual (4, 4) || CheckExtension ("GL_ARB_clear_texture"))
2273 && FindProcShort (glClearTexImage)
2274 && FindProcShort (glClearTexSubImage);
2276 has44 = IsGlGreaterEqual (4, 4)
2278 && FindProcShort (glBufferStorage)
2279 && FindProcShort (glBindBuffersBase)
2280 && FindProcShort (glBindBuffersRange)
2281 && FindProcShort (glBindTextures)
2282 && FindProcShort (glBindSamplers)
2283 && FindProcShort (glBindImageTextures)
2284 && FindProcShort (glBindVertexBuffers);
2286 // initialize debug context extension
2287 if (CheckExtension ("GL_ARB_debug_output"))
2292 arbDbg = (OpenGl_ArbDbg* )(&(*myFuncs));
2294 else if (FindProc ("glDebugMessageControlARB", myFuncs->glDebugMessageControl)
2295 && FindProc ("glDebugMessageInsertARB", myFuncs->glDebugMessageInsert)
2296 && FindProc ("glDebugMessageCallbackARB", myFuncs->glDebugMessageCallback)
2297 && FindProc ("glGetDebugMessageLogARB", myFuncs->glGetDebugMessageLog))
2299 arbDbg = (OpenGl_ArbDbg* )(&(*myFuncs));
2303 && caps->contextDebug)
2305 // setup default callback
2306 myIsGlDebugCtx = Standard_True;
2307 arbDbg->glDebugMessageCallback (debugCallbackWrap, this);
2310 ::glEnable (GL_DEBUG_OUTPUT);
2312 if (caps->contextSyncDebug)
2314 ::glEnable (GL_DEBUG_OUTPUT_SYNCHRONOUS);
2319 // initialize TBO extension (ARB)
2321 && CheckExtension ("GL_ARB_texture_buffer_object")
2322 && FindProc ("glTexBufferARB", myFuncs->glTexBuffer))
2324 arbTBO = (OpenGl_ArbTBO* )(&(*myFuncs));
2326 arbTboRGB32 = CheckExtension ("GL_ARB_texture_buffer_object_rgb32");
2328 // initialize hardware instancing extension (ARB)
2330 && CheckExtension ("GL_ARB_draw_instanced")
2331 && FindProc ("glDrawArraysInstancedARB", myFuncs->glDrawArraysInstanced)
2332 && FindProc ("glDrawElementsInstancedARB", myFuncs->glDrawElementsInstanced))
2334 arbIns = (OpenGl_ArbIns* )(&(*myFuncs));
2337 // initialize FBO extension (ARB)
2340 arbFBO = (OpenGl_ArbFBO* )(&(*myFuncs));
2341 arbFBOBlit = (OpenGl_ArbFBOBlit* )(&(*myFuncs));
2342 extPDS = Standard_True; // extension for EXT, but part of ARB
2345 // initialize GS extension (EXT)
2346 if (CheckExtension ("GL_EXT_geometry_shader4")
2347 && FindProcShort (glProgramParameteriEXT))
2349 extGS = (OpenGl_ExtGS* )(&(*myFuncs));
2352 // initialize bindless texture extension (ARB)
2353 if (CheckExtension ("GL_ARB_bindless_texture")
2354 && FindProcShort (glGetTextureHandleARB)
2355 && FindProcShort (glGetTextureSamplerHandleARB)
2356 && FindProcShort (glMakeTextureHandleResidentARB)
2357 && FindProcShort (glMakeTextureHandleNonResidentARB)
2358 && FindProcShort (glGetImageHandleARB)
2359 && FindProcShort (glMakeImageHandleResidentARB)
2360 && FindProcShort (glMakeImageHandleNonResidentARB)
2361 && FindProcShort (glUniformHandleui64ARB)
2362 && FindProcShort (glUniformHandleui64vARB)
2363 && FindProcShort (glProgramUniformHandleui64ARB)
2364 && FindProcShort (glProgramUniformHandleui64vARB)
2365 && FindProcShort (glIsTextureHandleResidentARB)
2366 && FindProcShort (glIsImageHandleResidentARB)
2367 && FindProcShort (glVertexAttribL1ui64ARB)
2368 && FindProcShort (glVertexAttribL1ui64vARB)
2369 && FindProcShort (glGetVertexAttribLui64vARB))
2371 arbTexBindless = (OpenGl_ArbTexBindless* )(&(*myFuncs));
2376 checkWrongVersion (1, 2);
2383 checkWrongVersion (1, 3);
2390 checkWrongVersion (1, 4);
2397 checkWrongVersion (1, 5);
2404 core15 = (OpenGl_GlCore15* )(&(*myFuncs));
2406 core15fwd = (OpenGl_GlCore15Fwd* )(&(*myFuncs));
2410 checkWrongVersion (2, 0);
2416 const char* aGlslVer = (const char* )::glGetString (GL_SHADING_LANGUAGE_VERSION);
2417 if (aGlslVer == NULL
2418 || *aGlslVer == '\0')
2420 // broken context has been detected
2421 TCollection_ExtendedString aMsg = TCollection_ExtendedString()
2422 + "Error! OpenGL context reports version "
2423 + myGlVerMajor + "." + myGlVerMinor
2424 + " but reports wrong GLSL version";
2425 PushMessage (GL_DEBUG_SOURCE_APPLICATION,
2426 GL_DEBUG_TYPE_ERROR,
2428 GL_DEBUG_SEVERITY_HIGH,
2437 core20 = (OpenGl_GlCore20* )(&(*myFuncs));
2439 core20fwd = (OpenGl_GlCore20Fwd* )(&(*myFuncs));
2443 checkWrongVersion (2, 1);
2451 checkWrongVersion (3, 0);
2457 // MSAA RenderBuffers have been defined in OpenGL 3.0,
2458 // but MSAA Textures - only in OpenGL 3.2+
2460 && CheckExtension ("GL_ARB_texture_multisample")
2461 && FindProcShort (glTexImage2DMultisample))
2463 GLint aNbColorSamples = 0, aNbDepthSamples = 0;
2464 ::glGetIntegerv (GL_MAX_COLOR_TEXTURE_SAMPLES, &aNbColorSamples);
2465 ::glGetIntegerv (GL_MAX_DEPTH_TEXTURE_SAMPLES, &aNbDepthSamples);
2466 myMaxMsaaSamples = Min (aNbColorSamples, aNbDepthSamples);
2469 && CheckExtension ("GL_ARB_texture_storage_multisample")
2470 && FindProcShort (glTexStorage2DMultisample))
2477 checkWrongVersion (3, 1);
2482 arbTBO = (OpenGl_ArbTBO* )(&(*myFuncs));
2483 arbIns = (OpenGl_ArbIns* )(&(*myFuncs));
2485 // check whether ray tracing mode is supported
2486 myHasRayTracing = has31
2488 && arbFBOBlit != NULL;
2490 // check whether textures in ray tracing mode are supported
2491 myHasRayTracingTextures = myHasRayTracing
2492 && arbTexBindless != NULL;
2494 // check whether adaptive screen sampling in ray tracing mode is supported
2495 myHasRayTracingAdaptiveSampling = myHasRayTracing
2497 && CheckExtension ("GL_NV_shader_atomic_float");
2501 checkWrongVersion (3, 2);
2506 core32 = (OpenGl_GlCore32* )(&(*myFuncs));
2509 core32->glGenVertexArrays (1, &myDefaultVao);
2513 core32back = (OpenGl_GlCore32Back* )(&(*myFuncs));
2515 ::glGetIntegerv (GL_MAX_SAMPLES, &myMaxMsaaSamples);
2519 checkWrongVersion (3, 3);
2524 core33 = (OpenGl_GlCore33* )(&(*myFuncs));
2527 core33back = (OpenGl_GlCore33Back* )(&(*myFuncs));
2532 checkWrongVersion (4, 0);
2537 arbTboRGB32 = Standard_True; // in core since OpenGL 4.0
2541 checkWrongVersion (4, 1);
2546 core41 = (OpenGl_GlCore41* )(&(*myFuncs));
2549 core41back = (OpenGl_GlCore41Back* )(&(*myFuncs));
2554 checkWrongVersion (4, 2);
2559 core42 = (OpenGl_GlCore42* )(&(*myFuncs));
2562 core42back = (OpenGl_GlCore42Back* )(&(*myFuncs));
2567 checkWrongVersion (4, 3);
2572 core43 = (OpenGl_GlCore43* )(&(*myFuncs));
2575 core43back = (OpenGl_GlCore43Back* )(&(*myFuncs));
2580 checkWrongVersion (4, 4);
2585 core44 = (OpenGl_GlCore44* )(&(*myFuncs));
2588 core44back = (OpenGl_GlCore44Back* )(&(*myFuncs));
2593 // =======================================================================
2594 // function : MemoryInfo
2596 // =======================================================================
2597 Standard_Size OpenGl_Context::AvailableMemory() const
2599 #if !defined(GL_ES_VERSION_2_0)
2602 // this is actually information for VBO pool
2603 // however because pools are mostly shared
2604 // it can be used for total GPU memory estimations
2607 glGetIntegerv (GL_VBO_FREE_MEMORY_ATI, aMemInfo);
2608 // returned value is in KiB, however this maybe changed in future
2609 return Standard_Size(aMemInfo[0]) * 1024;
2613 // current available dedicated video memory (in KiB), currently unused GPU memory
2615 glGetIntegerv (GL_GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX, &aMemInfo);
2616 return Standard_Size(aMemInfo) * 1024;
2622 // =======================================================================
2623 // function : MemoryInfo
2625 // =======================================================================
2626 TCollection_AsciiString OpenGl_Context::MemoryInfo() const
2628 TColStd_IndexedDataMapOfStringString aDict;
2631 TCollection_AsciiString aText;
2632 for (TColStd_IndexedDataMapOfStringString::Iterator anIter (aDict); anIter.More(); anIter.Next())
2634 if (!aText.IsEmpty())
2638 aText += TCollection_AsciiString(" ") + anIter.Key() + ": " + anIter.Value();
2643 // =======================================================================
2644 // function : MemoryInfo
2646 // =======================================================================
2647 void OpenGl_Context::MemoryInfo (TColStd_IndexedDataMapOfStringString& theDict) const
2649 #if defined(GL_ES_VERSION_2_0)
2651 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
2652 GLint aGlRendId = 0;
2653 CGLGetParameter (CGLGetCurrentContext(), kCGLCPCurrentRendererID, &aGlRendId);
2655 CGLRendererInfoObj aRendObj = NULL;
2656 CGOpenGLDisplayMask aDispMask = CGDisplayIDToOpenGLDisplayMask (kCGDirectMainDisplay);
2658 CGLQueryRendererInfo (aDispMask, &aRendObj, &aRendNb);
2659 for (GLint aRendIter = 0; aRendIter < aRendNb; ++aRendIter)
2662 if (CGLDescribeRenderer (aRendObj, aRendIter, kCGLRPRendererID, &aRendId) != kCGLNoError
2663 || aRendId != aGlRendId)
2668 //kCGLRPVideoMemoryMegabytes = 131;
2669 //kCGLRPTextureMemoryMegabytes = 132;
2671 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
2672 if (CGLDescribeRenderer(aRendObj, aRendIter, kCGLRPVideoMemoryMegabytes, &aVMem) == kCGLNoError)
2674 addInfo (theDict, "GPU memory", TCollection_AsciiString() + aVMem + " MiB");
2676 if (CGLDescribeRenderer(aRendObj, aRendIter, kCGLRPTextureMemoryMegabytes, &aVMem) == kCGLNoError)
2678 addInfo (theDict, "GPU Texture memory", TCollection_AsciiString() + aVMem + " MiB");
2681 if (CGLDescribeRenderer(aRendObj, aRendIter, kCGLRPVideoMemory, &aVMem) == kCGLNoError)
2683 addInfo (theDict, "GPU memory", TCollection_AsciiString() + (aVMem / (1024 * 1024)) + " MiB");
2685 if (CGLDescribeRenderer(aRendObj, aRendIter, kCGLRPTextureMemory, &aVMem) == kCGLNoError)
2687 addInfo (theDict, "GPU Texture memory", TCollection_AsciiString() + (aVMem / (1024 * 1024)) + " MiB");
2693 #if !defined(GL_ES_VERSION_2_0)
2697 memset (aValues, 0, sizeof(aValues));
2698 glGetIntegerv (GL_VBO_FREE_MEMORY_ATI, aValues);
2700 // total memory free in the pool
2701 addInfo (theDict, "GPU free memory", TCollection_AsciiString() + (aValues[0] / 1024) + " MiB");
2703 if (aValues[1] != aValues[0])
2705 // largest available free block in the pool
2706 addInfo (theDict, "Largest free block", TCollection_AsciiString() + (aValues[1] / 1024) + " MiB");
2708 if (aValues[2] != aValues[0])
2710 // total auxiliary memory free
2711 addInfo (theDict, "Free auxiliary memory", TCollection_AsciiString() + (aValues[2] / 1024) + " MiB");
2716 //current available dedicated video memory (in KiB), currently unused GPU memory
2718 glGetIntegerv (GL_GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX, &aValue);
2719 addInfo (theDict, "GPU free memory", TCollection_AsciiString() + (aValue / 1024) + " MiB");
2721 // dedicated video memory, total size (in KiB) of the GPU memory
2722 GLint aDedicated = 0;
2723 glGetIntegerv (GL_GPU_MEMORY_INFO_DEDICATED_VIDMEM_NVX, &aDedicated);
2724 addInfo (theDict, "GPU memory", TCollection_AsciiString() + (aDedicated / 1024) + " MiB");
2726 // total available memory, total size (in KiB) of the memory available for allocations
2727 glGetIntegerv (GL_GPU_MEMORY_INFO_TOTAL_AVAILABLE_MEMORY_NVX, &aValue);
2728 if (aValue != aDedicated)
2730 // different only for special configurations
2731 addInfo (theDict, "Total memory", TCollection_AsciiString() + (aValue / 1024) + " MiB");
2735 else if (myFuncs->wglGetGPUInfoAMD != NULL
2736 && myFuncs->wglGetContextGPUIDAMD != NULL)
2738 GLuint aTotalMemMiB = 0;
2739 UINT anAmdId = myFuncs->wglGetContextGPUIDAMD ((HGLRC )myGContext);
2742 if (myFuncs->wglGetGPUInfoAMD (anAmdId, WGL_GPU_RAM_AMD, GL_UNSIGNED_INT, sizeof(aTotalMemMiB), &aTotalMemMiB) > 0)
2744 addInfo (theDict, "GPU memory", TCollection_AsciiString() + (int )aTotalMemMiB + " MiB");
2751 #if !defined(GL_ES_VERSION_2_0) && !defined(__APPLE__) && !defined(_WIN32)
2752 // GLX_RENDERER_VENDOR_ID_MESA
2753 if (myFuncs->glXQueryCurrentRendererIntegerMESA != NULL)
2755 unsigned int aVMemMiB = 0;
2756 if (myFuncs->glXQueryCurrentRendererIntegerMESA (GLX_RENDERER_VIDEO_MEMORY_MESA, &aVMemMiB) != False)
2758 addInfo (theDict, "GPU memory", TCollection_AsciiString() + int(aVMemMiB) + " MiB");
2764 // =======================================================================
2765 // function : DiagnosticInfo
2767 // =======================================================================
2768 void OpenGl_Context::DiagnosticInformation (TColStd_IndexedDataMapOfStringString& theDict,
2769 Graphic3d_DiagnosticInfo theFlags) const
2771 if ((theFlags & Graphic3d_DiagnosticInfo_NativePlatform) != 0)
2773 #if defined(HAVE_EGL)
2774 addInfo (theDict, "EGLVersion", ::eglQueryString ((EGLDisplay )myDisplay, EGL_VERSION));
2775 addInfo (theDict, "EGLVendor", ::eglQueryString ((EGLDisplay )myDisplay, EGL_VENDOR));
2776 addInfo (theDict, "EGLClientAPIs", ::eglQueryString ((EGLDisplay )myDisplay, EGL_CLIENT_APIS));
2777 if ((theFlags & Graphic3d_DiagnosticInfo_Extensions) != 0)
2779 addInfo (theDict, "EGLExtensions", ::eglQueryString ((EGLDisplay )myDisplay, EGL_EXTENSIONS));
2781 #elif defined(_WIN32)
2782 if ((theFlags & Graphic3d_DiagnosticInfo_Extensions) != 0
2783 && myFuncs->wglGetExtensionsStringARB != NULL)
2785 const char* aWglExts = myFuncs->wglGetExtensionsStringARB ((HDC )myWindowDC);
2786 addInfo (theDict, "WGLExtensions", aWglExts);
2788 #elif defined(__APPLE__)
2791 Display* aDisplay = (Display*)myDisplay;
2792 const int aScreen = DefaultScreen(aDisplay);
2793 addInfo (theDict, "GLXDirectRendering", ::glXIsDirect (aDisplay, (GLXContext )myGContext) ? "Yes" : "No");
2794 addInfo (theDict, "GLXVendor", ::glXQueryServerString (aDisplay, aScreen, GLX_VENDOR));
2795 addInfo (theDict, "GLXVersion", ::glXQueryServerString (aDisplay, aScreen, GLX_VERSION));
2796 if ((theFlags & Graphic3d_DiagnosticInfo_Extensions) != 0)
2798 const char* aGlxExts = ::glXQueryExtensionsString (aDisplay, aScreen);
2799 addInfo(theDict, "GLXExtensions", aGlxExts);
2802 addInfo (theDict, "GLXClientVendor", ::glXGetClientString (aDisplay, GLX_VENDOR));
2803 addInfo (theDict, "GLXClientVersion", ::glXGetClientString (aDisplay, GLX_VERSION));
2804 if ((theFlags & Graphic3d_DiagnosticInfo_Extensions) != 0)
2806 addInfo (theDict, "GLXClientExtensions", ::glXGetClientString (aDisplay, GLX_EXTENSIONS));
2811 if ((theFlags & Graphic3d_DiagnosticInfo_Device) != 0)
2813 addInfo (theDict, "GLvendor", (const char*)::glGetString (GL_VENDOR));
2814 addInfo (theDict, "GLdevice", (const char*)::glGetString (GL_RENDERER));
2815 addInfo (theDict, "GLversion", (const char*)::glGetString (GL_VERSION));
2816 if (IsGlGreaterEqual (2, 0))
2818 addInfo (theDict, "GLSLversion", (const char*)::glGetString (GL_SHADING_LANGUAGE_VERSION));
2822 addInfo (theDict, "GLdebug", "ON");
2826 if ((theFlags & Graphic3d_DiagnosticInfo_Limits) != 0)
2828 addInfo (theDict, "Max texture size", TCollection_AsciiString(myMaxTexDim));
2829 addInfo (theDict, "Max combined texture units", TCollection_AsciiString(myMaxTexCombined));
2830 addInfo (theDict, "Max MSAA samples", TCollection_AsciiString(myMaxMsaaSamples));
2833 if ((theFlags & Graphic3d_DiagnosticInfo_FrameBuffer) != 0)
2835 GLint aViewport[4] = {};
2836 ::glGetIntegerv (GL_VIEWPORT, aViewport);
2837 addInfo (theDict, "Viewport", TCollection_AsciiString() + aViewport[2] + "x" + aViewport[3]);
2840 if ((theFlags & Graphic3d_DiagnosticInfo_Memory) != 0)
2842 MemoryInfo (theDict);
2845 if ((theFlags & Graphic3d_DiagnosticInfo_Extensions) != 0)
2847 #if !defined(GL_ES_VERSION_2_0)
2848 if (IsGlGreaterEqual (3, 0)
2849 && myFuncs->glGetStringi != NULL)
2851 TCollection_AsciiString anExtList;
2853 ::glGetIntegerv (GL_NUM_EXTENSIONS, &anExtNb);
2854 for (GLint anIter = 0; anIter < anExtNb; ++anIter)
2856 const char* anExtension = (const char*)myFuncs->glGetStringi (GL_EXTENSIONS, (GLuint)anIter);
2857 if (!anExtList.IsEmpty())
2861 anExtList += anExtension;
2863 addInfo(theDict, "GLextensions", anExtList);
2868 addInfo (theDict, "GLextensions", (const char*)::glGetString (GL_EXTENSIONS));
2873 // =======================================================================
2874 // function : GetResource
2876 // =======================================================================
2877 const Handle(OpenGl_Resource)& OpenGl_Context::GetResource (const TCollection_AsciiString& theKey) const
2879 return mySharedResources->IsBound (theKey) ? mySharedResources->Find (theKey) : NULL_GL_RESOURCE;
2882 // =======================================================================
2883 // function : ShareResource
2885 // =======================================================================
2886 Standard_Boolean OpenGl_Context::ShareResource (const TCollection_AsciiString& theKey,
2887 const Handle(OpenGl_Resource)& theResource)
2889 if (theKey.IsEmpty() || theResource.IsNull())
2891 return Standard_False;
2893 return mySharedResources->Bind (theKey, theResource);
2896 // =======================================================================
2897 // function : ReleaseResource
2899 // =======================================================================
2900 void OpenGl_Context::ReleaseResource (const TCollection_AsciiString& theKey,
2901 const Standard_Boolean theToDelay)
2903 if (!mySharedResources->IsBound (theKey))
2907 const Handle(OpenGl_Resource)& aRes = mySharedResources->Find (theKey);
2908 if (aRes->GetRefCount() > 1)
2915 myDelayed->Bind (theKey, 1);
2919 aRes->Release (this);
2920 mySharedResources->UnBind (theKey);
2924 // =======================================================================
2925 // function : ReleaseDelayed
2927 // =======================================================================
2928 void OpenGl_Context::ReleaseDelayed()
2930 // release queued elements
2931 while (!myUnusedResources->IsEmpty())
2933 myUnusedResources->First()->Release (this);
2934 myUnusedResources->RemoveFirst();
2937 // release delayed shared resources
2938 NCollection_Vector<TCollection_AsciiString> aDeadList;
2939 for (NCollection_DataMap<TCollection_AsciiString, Standard_Integer>::Iterator anIter (*myDelayed);
2940 anIter.More(); anIter.Next())
2942 if (++anIter.ChangeValue() <= 2)
2944 continue; // postpone release one more frame to ensure noone use it periodically
2947 const TCollection_AsciiString& aKey = anIter.Key();
2948 if (!mySharedResources->IsBound (aKey))
2950 // mixed unshared strategy delayed/undelayed was used!
2951 aDeadList.Append (aKey);
2955 const Handle(OpenGl_Resource)& aRes = mySharedResources->ChangeFind (aKey);
2956 if (aRes->GetRefCount() > 1)
2958 // should be only 1 instance in mySharedResources
2959 // if not - resource was reused again
2960 aDeadList.Append (aKey);
2964 // release resource if no one requiested it more than 2 redraw calls
2965 aRes->Release (this);
2966 mySharedResources->UnBind (aKey);
2967 aDeadList.Append (aKey);
2970 for (Standard_Integer anIter = 0; anIter < aDeadList.Length(); ++anIter)
2972 myDelayed->UnBind (aDeadList.Value (anIter));
2976 // =======================================================================
2977 // function : BindTextures
2979 // =======================================================================
2980 Handle(OpenGl_TextureSet) OpenGl_Context::BindTextures (const Handle(OpenGl_TextureSet)& theTextures)
2982 if (myActiveTextures == theTextures)
2984 return myActiveTextures;
2987 Handle(OpenGl_Context) aThisCtx (this);
2988 OpenGl_TextureSet::Iterator aTextureIterOld (myActiveTextures), aTextureIterNew (theTextures);
2991 if (!aTextureIterNew.More())
2993 for (; aTextureIterOld.More(); aTextureIterOld.Next())
2995 if (const Handle(OpenGl_Texture)& aTextureOld = aTextureIterOld.Value())
2997 #if !defined(GL_ES_VERSION_2_0)
3000 OpenGl_Sampler::resetGlobalTextureParams (aThisCtx, *aTextureOld, aTextureOld->Sampler()->Parameters());
3003 aTextureOld->Unbind (aThisCtx);
3009 const Handle(OpenGl_Texture)& aTextureNew = aTextureIterNew.Value();
3010 if (aTextureIterOld.More())
3012 const Handle(OpenGl_Texture)& aTextureOld = aTextureIterOld.Value();
3013 if (aTextureNew == aTextureOld)
3015 aTextureIterNew.Next();
3016 aTextureIterOld.Next();
3019 else if (aTextureNew.IsNull()
3020 || !aTextureNew->IsValid())
3022 if (!aTextureOld.IsNull())
3024 #if !defined(GL_ES_VERSION_2_0)
3027 OpenGl_Sampler::resetGlobalTextureParams (aThisCtx, *aTextureOld, aTextureOld->Sampler()->Parameters());
3030 aTextureOld->Unbind (aThisCtx);
3033 aTextureIterNew.Next();
3034 aTextureIterOld.Next();
3038 aTextureIterOld.Next();
3040 if (aTextureNew.IsNull())
3042 aTextureIterNew.Next();
3046 const Graphic3d_TextureUnit aTexUnit = aTextureNew->Sampler()->Parameters()->TextureUnit();
3047 if (aTexUnit >= myMaxTexCombined)
3049 PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH,
3050 TCollection_AsciiString("Texture unit ") + aTexUnit + " for " + aTextureNew->ResourceId() + " exceeds hardware limit " + myMaxTexCombined);
3051 aTextureIterNew.Next();
3055 aTextureNew->Bind (aThisCtx);
3056 if (aTextureNew->Sampler()->ToUpdateParameters())
3058 if (aTextureNew->Sampler()->IsImmutable())
3060 aTextureNew->Sampler()->Init (aThisCtx, *aTextureNew);
3064 OpenGl_Sampler::applySamplerParams (aThisCtx, aTextureNew->Sampler()->Parameters(), aTextureNew->Sampler().get(), aTextureNew->GetTarget(), aTextureNew->HasMipmaps());
3067 #if !defined(GL_ES_VERSION_2_0)
3070 OpenGl_Sampler::applyGlobalTextureParams (aThisCtx, *aTextureNew, aTextureNew->Sampler()->Parameters());
3073 aTextureIterNew.Next();
3076 Handle(OpenGl_TextureSet) anOldTextures = myActiveTextures;
3077 myActiveTextures = theTextures;
3078 return anOldTextures;
3081 // =======================================================================
3082 // function : BindProgram
3084 // =======================================================================
3085 Standard_Boolean OpenGl_Context::BindProgram (const Handle(OpenGl_ShaderProgram)& theProgram)
3087 if (core20fwd == NULL)
3089 return Standard_False;
3091 else if (myActiveProgram == theProgram)
3093 return Standard_True;
3096 if (theProgram.IsNull()
3097 || !theProgram->IsValid())
3099 if (!myActiveProgram.IsNull())
3101 core20fwd->glUseProgram (OpenGl_ShaderProgram::NO_PROGRAM);
3102 myActiveProgram.Nullify();
3104 return Standard_False;
3107 myActiveProgram = theProgram;
3108 core20fwd->glUseProgram (theProgram->ProgramId());
3109 return Standard_True;
3112 // =======================================================================
3113 // function : BindDefaultVao
3115 // =======================================================================
3116 void OpenGl_Context::BindDefaultVao()
3118 #if !defined(GL_ES_VERSION_2_0)
3119 if (myDefaultVao == 0
3125 core32->glBindVertexArray (myDefaultVao);
3129 // =======================================================================
3130 // function : SetDefaultFrameBuffer
3132 // =======================================================================
3133 Handle(OpenGl_FrameBuffer) OpenGl_Context::SetDefaultFrameBuffer (const Handle(OpenGl_FrameBuffer)& theFbo)
3135 Handle(OpenGl_FrameBuffer) aFbo = myDefaultFbo;
3136 myDefaultFbo = theFbo;
3140 // =======================================================================
3141 // function : SetShadingMaterial
3143 // =======================================================================
3144 void OpenGl_Context::SetShadingMaterial (const OpenGl_AspectFace* theAspect,
3145 const Handle(Graphic3d_PresentationAttributes)& theHighlight)
3147 const Handle(Graphic3d_AspectFillArea3d)& anAspect = (!theHighlight.IsNull() && !theHighlight->BasicFillAreaAspect().IsNull())
3148 ? theHighlight->BasicFillAreaAspect()
3149 : theAspect->Aspect();
3151 const bool toDistinguish = anAspect->Distinguish();
3152 const bool toMapTexture = anAspect->ToMapTexture();
3153 const Graphic3d_MaterialAspect& aMatFrontSrc = anAspect->FrontMaterial();
3154 const Graphic3d_MaterialAspect& aMatBackSrc = toDistinguish
3155 ? anAspect->BackMaterial()
3157 const Quantity_Color& aFrontIntColor = anAspect->InteriorColor();
3158 const Quantity_Color& aBackIntColor = toDistinguish
3159 ? anAspect->BackInteriorColor()
3162 myMatFront.Init (aMatFrontSrc, aFrontIntColor);
3165 myMatBack.Init (aMatBackSrc, aBackIntColor);
3169 myMatBack = myMatFront;
3172 if (!theHighlight.IsNull()
3173 && theHighlight->BasicFillAreaAspect().IsNull())
3175 myMatFront.SetColor (theHighlight->ColorRGBA());
3176 myMatBack .SetColor (theHighlight->ColorRGBA());
3179 Standard_ShortReal anAlphaFront = 1.0f;
3180 Standard_ShortReal anAlphaBack = 1.0f;
3181 if (CheckIsTransparent (theAspect, theHighlight, anAlphaFront, anAlphaBack))
3183 myMatFront.Diffuse.a() = anAlphaFront;
3184 myMatBack .Diffuse.a() = anAlphaBack;
3187 // do not update material properties in case of zero reflection mode,
3188 // because GL lighting will be disabled by OpenGl_PrimitiveArray::DrawArray() anyway.
3189 const OpenGl_MaterialState& aMatState = myShaderManager->MaterialState();
3190 const float anAlphaCutoff = anAspect->AlphaMode() == Graphic3d_AlphaMode_Mask
3191 ? anAspect->AlphaCutoff()
3193 if (theAspect->ShadingModel() == Graphic3d_TOSM_UNLIT)
3195 if (anAlphaCutoff == aMatState.AlphaCutoff())
3200 else if (myMatFront == aMatState.FrontMaterial()
3201 && myMatBack == aMatState.BackMaterial()
3202 && toDistinguish == aMatState.ToDistinguish()
3203 && toMapTexture == aMatState.ToMapTexture()
3204 && anAlphaCutoff == aMatState.AlphaCutoff())
3209 myShaderManager->UpdateMaterialStateTo (myMatFront, myMatBack, anAlphaCutoff, toDistinguish, toMapTexture);
3212 // =======================================================================
3213 // function : CheckIsTransparent
3215 // =======================================================================
3216 Standard_Boolean OpenGl_Context::CheckIsTransparent (const OpenGl_AspectFace* theAspect,
3217 const Handle(Graphic3d_PresentationAttributes)& theHighlight,
3218 Standard_ShortReal& theAlphaFront,
3219 Standard_ShortReal& theAlphaBack)
3221 const Handle(Graphic3d_AspectFillArea3d)& anAspect = (!theHighlight.IsNull() && !theHighlight->BasicFillAreaAspect().IsNull())
3222 ? theHighlight->BasicFillAreaAspect()
3223 : theAspect->Aspect();
3225 const bool toDistinguish = anAspect->Distinguish();
3226 const Graphic3d_MaterialAspect& aMatFrontSrc = anAspect->FrontMaterial();
3227 const Graphic3d_MaterialAspect& aMatBackSrc = toDistinguish
3228 ? anAspect->BackMaterial()
3231 // handling transparency
3232 if (!theHighlight.IsNull()
3233 && theHighlight->BasicFillAreaAspect().IsNull())
3235 theAlphaFront = theHighlight->ColorRGBA().Alpha();
3236 theAlphaBack = theHighlight->ColorRGBA().Alpha();
3240 theAlphaFront = aMatFrontSrc.Alpha();
3241 theAlphaBack = aMatBackSrc .Alpha();
3244 if (anAspect->AlphaMode() == Graphic3d_AlphaMode_BlendAuto)
3246 return theAlphaFront < 1.0f
3247 || theAlphaBack < 1.0f;
3249 return anAspect->AlphaMode() == Graphic3d_AlphaMode_Blend;
3252 // =======================================================================
3253 // function : SetColor4fv
3255 // =======================================================================
3256 void OpenGl_Context::SetColor4fv (const OpenGl_Vec4& theColor)
3258 if (!myActiveProgram.IsNull())
3260 myActiveProgram->SetUniform (this, myActiveProgram->GetStateLocation (OpenGl_OCCT_COLOR), theColor);
3262 #if !defined(GL_ES_VERSION_2_0)
3263 else if (core11 != NULL)
3265 core11->glColor4fv (theColor.GetData());
3270 // =======================================================================
3271 // function : SetTypeOfLine
3273 // =======================================================================
3274 void OpenGl_Context::SetTypeOfLine (const Aspect_TypeOfLine theType,
3275 const Standard_ShortReal theFactor)
3277 Standard_Integer aPattern = 0xFFFF;
3280 case Aspect_TOL_DASH:
3285 case Aspect_TOL_DOT:
3290 case Aspect_TOL_DOTDASH:
3295 case Aspect_TOL_EMPTY:
3296 case Aspect_TOL_SOLID:
3301 case Aspect_TOL_USERDEFINED:
3308 if (!myActiveProgram.IsNull())
3310 myActiveProgram->SetUniform (this, "uPattern", aPattern);
3311 myActiveProgram->SetUniform (this, "uFactor", theFactor);
3315 #if !defined(GL_ES_VERSION_2_0)
3316 if (aPattern != 0xFFFF)
3320 core11fwd->glEnable (GL_LINE_STIPPLE);
3322 core11->glLineStipple (static_cast<GLint> (theFactor),
3323 static_cast<GLushort> (aPattern));
3330 core11fwd->glDisable (GL_LINE_STIPPLE);
3336 // =======================================================================
3337 // function : SetLineWidth
3339 // =======================================================================
3340 void OpenGl_Context::SetLineWidth (const Standard_ShortReal theWidth)
3344 // glLineWidth() is still defined within Core Profile, but has no effect with values != 1.0f
3345 core11fwd->glLineWidth (theWidth * myLineWidthScale);
3349 // =======================================================================
3350 // function : SetTextureMatrix
3352 // =======================================================================
3353 void OpenGl_Context::SetTextureMatrix (const Handle(Graphic3d_TextureParams)& theParams)
3355 if (theParams.IsNull())
3359 else if (!myActiveProgram.IsNull())
3361 const GLint aUniLoc = myActiveProgram->GetStateLocation (OpenGl_OCCT_TEXTURE_TRSF2D);
3362 if (aUniLoc == OpenGl_ShaderProgram::INVALID_LOCATION)
3367 // pack transformation parameters
3368 OpenGl_Vec4 aTrsf[2];
3369 aTrsf[0].x() = -theParams->Translation().x();
3370 aTrsf[0].y() = -theParams->Translation().y();
3371 aTrsf[0].zw() = theParams->Scale();
3372 aTrsf[1].x() = std::sin (-theParams->Rotation() * static_cast<float> (M_PI / 180.0));
3373 aTrsf[1].y() = std::cos (-theParams->Rotation() * static_cast<float> (M_PI / 180.0));
3374 myActiveProgram->SetUniform (this, aUniLoc, 2, aTrsf);
3378 #if !defined(GL_ES_VERSION_2_0)
3381 GLint aMatrixMode = GL_TEXTURE;
3382 ::glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode);
3384 core11->glMatrixMode (GL_TEXTURE);
3385 OpenGl_Mat4 aTextureMat;
3386 const Graphic3d_Vec2& aScale = theParams->Scale();
3387 const Graphic3d_Vec2& aTrans = theParams->Translation();
3388 Graphic3d_TransformUtils::Scale (aTextureMat, aScale.x(), aScale.y(), 1.0f);
3389 Graphic3d_TransformUtils::Translate (aTextureMat, -aTrans.x(), -aTrans.y(), 0.0f);
3390 Graphic3d_TransformUtils::Rotate (aTextureMat, -theParams->Rotation(), 0.0f, 0.0f, 1.0f);
3391 core11->glLoadMatrixf (aTextureMat);
3392 core11->glMatrixMode (aMatrixMode);
3397 // =======================================================================
3398 // function : SetPointSize
3400 // =======================================================================
3401 void OpenGl_Context::SetPointSize (const Standard_ShortReal theSize)
3403 if (!myActiveProgram.IsNull())
3405 myActiveProgram->SetUniform (this, myActiveProgram->GetStateLocation (OpenGl_OCCT_POINT_SIZE), theSize);
3406 #if !defined(GL_ES_VERSION_2_0)
3407 //myContext->core11fwd->glEnable (GL_VERTEX_PROGRAM_POINT_SIZE);
3410 #if !defined(GL_ES_VERSION_2_0)
3413 core11fwd->glPointSize (theSize);
3414 if (core20fwd != NULL)
3416 //myContext->core11fwd->glDisable (GL_VERTEX_PROGRAM_POINT_SIZE);
3422 // =======================================================================
3423 // function : SetPointSpriteOrigin
3425 // =======================================================================
3426 void OpenGl_Context::SetPointSpriteOrigin()
3428 #if !defined(GL_ES_VERSION_2_0)
3429 if (core15fwd == NULL)
3434 const int aNewState = !myActiveProgram.IsNull() ? GL_UPPER_LEFT : GL_LOWER_LEFT;
3435 if (myPointSpriteOrig != aNewState)
3437 myPointSpriteOrig = aNewState;
3438 core15fwd->glPointParameteri (GL_POINT_SPRITE_COORD_ORIGIN, aNewState);
3443 // =======================================================================
3444 // function : SetGlNormalizeEnabled
3446 // =======================================================================
3447 Standard_Boolean OpenGl_Context::SetGlNormalizeEnabled (Standard_Boolean isEnabled)
3449 if (isEnabled == myIsGlNormalizeEnabled)
3451 return myIsGlNormalizeEnabled;
3454 Standard_Boolean anOldGlNormalize = myIsGlNormalizeEnabled;
3456 myIsGlNormalizeEnabled = isEnabled;
3458 #if !defined(GL_ES_VERSION_2_0)
3463 ::glEnable (GL_NORMALIZE);
3467 ::glDisable (GL_NORMALIZE);
3472 return anOldGlNormalize;
3475 // =======================================================================
3476 // function : SetPolygonMode
3478 // =======================================================================
3479 Standard_Integer OpenGl_Context::SetPolygonMode (const Standard_Integer theMode)
3481 if (myPolygonMode == theMode)
3483 return myPolygonMode;
3486 const Standard_Integer anOldPolygonMode = myPolygonMode;
3488 myPolygonMode = theMode;
3490 #if !defined(GL_ES_VERSION_2_0)
3491 ::glPolygonMode (GL_FRONT_AND_BACK, (GLenum)theMode);
3494 return anOldPolygonMode;
3497 // =======================================================================
3498 // function : SetPolygonHatchEnabled
3500 // =======================================================================
3501 bool OpenGl_Context::SetPolygonHatchEnabled (const bool theIsEnabled)
3503 if (myHatchStyles.IsNull())
3507 else if (myHatchStyles->IsEnabled() == theIsEnabled)
3509 return theIsEnabled;
3512 return myHatchStyles->SetEnabled (this, theIsEnabled);
3515 // =======================================================================
3516 // function : SetPolygonHatchStyle
3518 // =======================================================================
3519 Standard_Integer OpenGl_Context::SetPolygonHatchStyle (const Handle(Graphic3d_HatchStyle)& theStyle)
3521 if (theStyle.IsNull())
3526 if (myHatchStyles.IsNull())
3528 if (!GetResource ("OpenGl_LineAttributes", myHatchStyles))
3530 // share and register for release once the resource is no longer used
3531 myHatchStyles = new OpenGl_LineAttributes();
3532 ShareResource ("OpenGl_LineAttributes", myHatchStyles);
3535 if (myHatchStyles->TypeOfHatch() == theStyle->HatchType())
3537 return theStyle->HatchType();
3540 return myHatchStyles->SetTypeOfHatch (this, theStyle);
3543 // =======================================================================
3544 // function : SetPolygonOffset
3546 // =======================================================================
3547 void OpenGl_Context::SetPolygonOffset (const Graphic3d_PolygonOffset& theOffset)
3549 const bool toFillOld = (myPolygonOffset.Mode & Aspect_POM_Fill) == Aspect_POM_Fill;
3550 const bool toFillNew = (theOffset.Mode & Aspect_POM_Fill) == Aspect_POM_Fill;
3551 if (toFillNew != toFillOld)
3555 glEnable (GL_POLYGON_OFFSET_FILL);
3559 glDisable (GL_POLYGON_OFFSET_FILL);
3563 #if !defined(GL_ES_VERSION_2_0)
3564 const bool toLineOld = (myPolygonOffset.Mode & Aspect_POM_Line) == Aspect_POM_Line;
3565 const bool toLineNew = (theOffset.Mode & Aspect_POM_Line) == Aspect_POM_Line;
3566 if (toLineNew != toLineOld)
3570 glEnable (GL_POLYGON_OFFSET_LINE);
3574 glDisable (GL_POLYGON_OFFSET_LINE);
3578 const bool toPointOld = (myPolygonOffset.Mode & Aspect_POM_Point) == Aspect_POM_Point;
3579 const bool toPointNew = (theOffset.Mode & Aspect_POM_Point) == Aspect_POM_Point;
3580 if (toPointNew != toPointOld)
3584 glEnable (GL_POLYGON_OFFSET_POINT);
3588 glDisable (GL_POLYGON_OFFSET_POINT);
3593 if (myPolygonOffset.Factor != theOffset.Factor
3594 || myPolygonOffset.Units != theOffset.Units)
3596 glPolygonOffset (theOffset.Factor, theOffset.Units);
3598 myPolygonOffset = theOffset;
3601 // =======================================================================
3602 // function : ApplyModelWorldMatrix
3604 // =======================================================================
3605 void OpenGl_Context::ApplyModelWorldMatrix()
3607 if (myShaderManager->ModelWorldState().ModelWorldMatrix() != ModelWorldState.Current())
3609 myShaderManager->UpdateModelWorldStateTo (ModelWorldState.Current());
3613 // =======================================================================
3614 // function : ApplyWorldViewMatrix
3616 // =======================================================================
3617 void OpenGl_Context::ApplyWorldViewMatrix()
3619 if (myShaderManager->ModelWorldState().ModelWorldMatrix() != THE_IDENTITY_MATRIX)
3621 myShaderManager->UpdateModelWorldStateTo (THE_IDENTITY_MATRIX);
3623 if (myShaderManager->WorldViewState().WorldViewMatrix() != WorldViewState.Current())
3625 myShaderManager->UpdateWorldViewStateTo (WorldViewState.Current());
3629 // =======================================================================
3630 // function : ApplyModelViewMatrix
3632 // =======================================================================
3633 void OpenGl_Context::ApplyModelViewMatrix()
3635 if (myShaderManager->ModelWorldState().ModelWorldMatrix() != ModelWorldState.Current())
3637 myShaderManager->UpdateModelWorldStateTo (ModelWorldState.Current());
3639 if (myShaderManager->WorldViewState().WorldViewMatrix() != WorldViewState.Current())
3641 myShaderManager->UpdateWorldViewStateTo (WorldViewState.Current());
3645 // =======================================================================
3646 // function : ApplyProjectionMatrix
3648 // =======================================================================
3649 void OpenGl_Context::ApplyProjectionMatrix()
3651 if (myShaderManager->ProjectionState().ProjectionMatrix() != ProjectionState.Current())
3653 myShaderManager->UpdateProjectionStateTo (ProjectionState.Current());
3657 // =======================================================================
3658 // function : EnableFeatures
3660 // =======================================================================
3661 void OpenGl_Context::EnableFeatures() const
3666 // =======================================================================
3667 // function : DisableFeatures
3669 // =======================================================================
3670 void OpenGl_Context::DisableFeatures() const
3672 // Disable stuff that's likely to slow down glDrawPixels.
3673 glDisable(GL_DITHER);
3674 glDisable(GL_BLEND);
3675 glDisable(GL_DEPTH_TEST);
3676 glDisable(GL_STENCIL_TEST);
3678 #if !defined(GL_ES_VERSION_2_0)
3684 glDisable(GL_TEXTURE_1D);
3685 glDisable(GL_TEXTURE_2D);
3687 glDisable(GL_LIGHTING);
3688 glDisable(GL_ALPHA_TEST);
3690 glDisable(GL_LOGIC_OP);
3692 glPixelTransferi(GL_MAP_COLOR, GL_FALSE);
3693 glPixelTransferi(GL_RED_SCALE, 1);
3694 glPixelTransferi(GL_RED_BIAS, 0);
3695 glPixelTransferi(GL_GREEN_SCALE, 1);
3696 glPixelTransferi(GL_GREEN_BIAS, 0);
3697 glPixelTransferi(GL_BLUE_SCALE, 1);
3698 glPixelTransferi(GL_BLUE_BIAS, 0);
3699 glPixelTransferi(GL_ALPHA_SCALE, 1);
3700 glPixelTransferi(GL_ALPHA_BIAS, 0);
3702 if ((myGlVerMajor >= 1) && (myGlVerMinor >= 2))
3704 if (CheckExtension ("GL_CONVOLUTION_1D_EXT"))
3705 glDisable(GL_CONVOLUTION_1D_EXT);
3707 if (CheckExtension ("GL_CONVOLUTION_2D_EXT"))
3708 glDisable(GL_CONVOLUTION_2D_EXT);
3710 if (CheckExtension ("GL_SEPARABLE_2D_EXT"))
3711 glDisable(GL_SEPARABLE_2D_EXT);
3713 if (CheckExtension ("GL_SEPARABLE_2D_EXT"))
3714 glDisable(GL_HISTOGRAM_EXT);
3716 if (CheckExtension ("GL_MINMAX_EXT"))
3717 glDisable(GL_MINMAX_EXT);
3719 if (CheckExtension ("GL_TEXTURE_3D_EXT"))
3720 glDisable(GL_TEXTURE_3D_EXT);
3725 // =======================================================================
3726 // function : SetColorMask
3728 // =======================================================================
3729 bool OpenGl_Context::SetColorMask (bool theToWriteColor)
3731 const GLboolean toWrite = theToWriteColor ? GL_TRUE : GL_FALSE;
3732 glColorMask (toWrite, toWrite, toWrite, toWrite);
3734 const bool anOldValue = myColorMask;
3735 myColorMask = theToWriteColor;
3739 // =======================================================================
3740 // function : SetSampleAlphaToCoverage
3742 // =======================================================================
3743 bool OpenGl_Context::SetSampleAlphaToCoverage (bool theToEnable)
3745 if (myAlphaToCoverage == theToEnable)
3747 return myAlphaToCoverage;
3750 if (core15fwd != NULL)
3754 //core15fwd->core15fwd->glSampleCoverage (1.0f, GL_FALSE);
3755 core15fwd->glEnable (GL_SAMPLE_ALPHA_TO_COVERAGE);
3759 core15fwd->glDisable (GL_SAMPLE_ALPHA_TO_COVERAGE);
3763 const bool anOldValue = myAlphaToCoverage;
3764 myAlphaToCoverage = theToEnable;