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_Sampler.hxx>
32 #include <OpenGl_ShaderManager.hxx>
33 #include <OpenGl_Workspace.hxx>
34 #include <OpenGl_AspectFace.hxx>
35 #include <Graphic3d_TransformUtils.hxx>
36 #include <Graphic3d_RenderingParams.hxx>
38 #include <Message_Messenger.hxx>
40 #include <NCollection_Vector.hxx>
42 #include <Standard_ProgramError.hxx>
44 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Context,Standard_Transient)
49 #pragma comment(lib, "libEGL.lib")
53 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
55 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
58 #include <OpenGL/OpenGL.h>
59 #include <CoreGraphics/CoreGraphics.h>
62 #include <GL/glx.h> // glXGetProcAddress()
68 #pragma comment (lib, "gl2ps.lib")
75 static const Handle(OpenGl_Resource) NULL_GL_RESOURCE;
76 static const OpenGl_Mat4 THE_IDENTITY_MATRIX;
78 //! Add key-value pair to the dictionary.
79 static void addInfo (TColStd_IndexedDataMapOfStringString& theDict,
80 const TCollection_AsciiString& theKey,
81 const TCollection_AsciiString& theValue)
83 theDict.ChangeFromIndex (theDict.Add (theKey, theValue)) = theValue;
86 //! Add key-value pair to the dictionary.
87 static void addInfo (TColStd_IndexedDataMapOfStringString& theDict,
88 const TCollection_AsciiString& theKey,
91 TCollection_AsciiString aValue (theValue != NULL ? theValue : "");
92 theDict.ChangeFromIndex (theDict.Add (theKey, aValue)) = aValue;
96 // =======================================================================
97 // function : OpenGl_Context
99 // =======================================================================
100 OpenGl_Context::OpenGl_Context (const Handle(OpenGl_Caps)& theCaps)
119 caps (!theCaps.IsNull() ? theCaps : new OpenGl_Caps()),
120 #if defined(GL_ES_VERSION_2_0)
121 hasHighp (Standard_False),
122 hasUintIndex(Standard_False),
123 hasTexRGBA8(Standard_False),
125 hasHighp (Standard_True),
126 hasUintIndex(Standard_True),
127 hasTexRGBA8(Standard_True),
129 hasDrawBuffers (OpenGl_FeatureNotAvailable),
130 hasFloatBuffer (OpenGl_FeatureNotAvailable),
131 hasHalfFloatBuffer (OpenGl_FeatureNotAvailable),
132 hasSampleVariables (OpenGl_FeatureNotAvailable),
133 arbDrawBuffers (Standard_False),
134 arbNPTW (Standard_False),
135 arbTexRG (Standard_False),
136 arbTexFloat (Standard_False),
137 arbSamplerObject (NULL),
138 arbTexBindless (NULL),
140 arbTboRGB32 (Standard_False),
145 arbSampleShading (Standard_False),
146 extFragDepth (Standard_False),
147 extDrawBuffers (Standard_False),
149 extBgra(Standard_False),
150 extAnis(Standard_False),
151 extPDS (Standard_False),
152 atiMem (Standard_False),
153 nvxMem (Standard_False),
154 oesSampleVariables (Standard_False),
155 oesStdDerivatives (Standard_False),
156 mySharedResources (new OpenGl_ResourcesMap()),
157 myDelayed (new OpenGl_DelayReleaseMap()),
158 myUnusedResources (new OpenGl_ResourcesStack()),
160 myGlLibHandle (NULL),
161 myFuncs (new OpenGl_GlFunctions()),
163 myTexClamp (GL_CLAMP_TO_EDGE),
165 myMaxTexCombined (1),
168 myMaxDrawBuffers (1),
169 myMaxColorAttachments (1),
172 myIsInitialized (Standard_False),
173 myIsStereoBuffers (Standard_False),
174 myIsGlNormalizeEnabled (Standard_False),
175 myHasRayTracing (Standard_False),
176 myHasRayTracingTextures (Standard_False),
177 myHasRayTracingAdaptiveSampling (Standard_False),
178 #if !defined(GL_ES_VERSION_2_0)
179 myPointSpriteOrig (GL_UPPER_LEFT),
180 myRenderMode (GL_RENDER),
181 myPolygonMode (GL_FILL),
183 myPointSpriteOrig (0),
187 myToCullBackFaces (false),
191 myIsGlDebugCtx (Standard_False),
192 myResolution (Graphic3d_RenderingParams::THE_DEFAULT_RESOLUTION),
193 myResolutionRatio (1.0f),
194 myLineWidthScale (1.0f),
195 myRenderScale (1.0f),
196 myRenderScaleInv (1.0f)
202 myViewportVirt[0] = 0;
203 myViewportVirt[1] = 0;
204 myViewportVirt[2] = 0;
205 myViewportVirt[3] = 0;
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 !defined(GL_ES_VERSION_2_0)
438 const Standard_Integer aDrawBuffer = !myIsStereoBuffers ? stereoToMonoBuffer (theDrawBuffers[anI]) : theDrawBuffers[anI];
440 const Standard_Integer aDrawBuffer = theDrawBuffers[anI];
442 if (aDrawBuffer < GL_COLOR_ATTACHMENT0 && aDrawBuffer != GL_NONE)
444 useDefaultFbo = Standard_True;
446 else if (aDrawBuffer != GL_NONE)
448 myDrawBuffers.SetValue (anI, aDrawBuffer);
451 if (arbFBO != NULL && useDefaultFbo)
453 arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
456 myFuncs->glDrawBuffers (theNb, (const GLenum*)theDrawBuffers);
459 // =======================================================================
460 // function : SetCullBackFaces
462 // =======================================================================
463 void OpenGl_Context::SetCullBackFaces (bool theToEnable)
465 if (myToCullBackFaces == theToEnable)
470 myToCullBackFaces = theToEnable;
473 //glCullFace (GL_BACK); GL_BACK by default
474 core11fwd->glEnable (GL_CULL_FACE);
478 core11fwd->glDisable (GL_CULL_FACE);
482 // =======================================================================
483 // function : FetchState
485 // =======================================================================
486 void OpenGl_Context::FetchState()
488 #if !defined(GL_ES_VERSION_2_0)
489 // cache feedback mode state
492 ::glGetIntegerv (GL_RENDER_MODE, &myRenderMode);
495 // cache read buffers state
496 ::glGetIntegerv (GL_READ_BUFFER, &myReadBuffer);
498 // cache draw buffers state
499 myDrawBuffers.Clear();
501 if (myMaxDrawBuffers == 1)
503 Standard_Integer aDrawBuffer;
505 ::glGetIntegerv (GL_DRAW_BUFFER, &aDrawBuffer);
507 if (aDrawBuffer != GL_NONE)
509 myDrawBuffers.SetValue (0, aDrawBuffer);
514 Standard_Integer aDrawBuffer;
516 for (Standard_Integer anI = 0; anI < myMaxDrawBuffers; ++anI)
518 ::glGetIntegerv (GL_DRAW_BUFFER0 + anI, &aDrawBuffer);
520 if (aDrawBuffer != GL_NONE)
522 myDrawBuffers.SetValue (anI, aDrawBuffer);
529 // =======================================================================
532 // =======================================================================
533 void OpenGl_Context::Share (const Handle(OpenGl_Context)& theShareCtx)
535 if (!theShareCtx.IsNull())
537 mySharedResources = theShareCtx->mySharedResources;
538 myDelayed = theShareCtx->myDelayed;
539 myUnusedResources = theShareCtx->myUnusedResources;
540 myShaderManager = theShareCtx->myShaderManager;
544 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
546 // =======================================================================
547 // function : IsCurrent
549 // =======================================================================
550 Standard_Boolean OpenGl_Context::IsCurrent() const
552 #if defined(HAVE_EGL)
553 if ((EGLDisplay )myDisplay == EGL_NO_DISPLAY
554 || (EGLSurface )myWindow == EGL_NO_SURFACE
555 || (EGLContext )myGContext == EGL_NO_CONTEXT)
557 return Standard_False;
560 return (((EGLDisplay )myDisplay == eglGetCurrentDisplay())
561 && ((EGLContext )myGContext == eglGetCurrentContext())
562 && ((EGLSurface )myWindow == eglGetCurrentSurface (EGL_DRAW)));
563 #elif defined(_WIN32)
564 if (myWindowDC == NULL || myGContext == NULL)
566 return Standard_False;
568 return (( (HDC )myWindowDC == wglGetCurrentDC())
569 && ((HGLRC )myGContext == wglGetCurrentContext()));
571 if (myDisplay == NULL || myWindow == 0 || myGContext == 0)
573 return Standard_False;
576 return ( ((Display* )myDisplay == glXGetCurrentDisplay())
577 && ((GLXContext )myGContext == glXGetCurrentContext())
578 && ((GLXDrawable )myWindow == glXGetCurrentDrawable()));
582 // =======================================================================
583 // function : MakeCurrent
585 // =======================================================================
586 Standard_Boolean OpenGl_Context::MakeCurrent()
588 #if defined(HAVE_EGL)
589 if ((EGLDisplay )myDisplay == EGL_NO_DISPLAY
590 || (EGLSurface )myWindow == EGL_NO_SURFACE
591 || (EGLContext )myGContext == EGL_NO_CONTEXT)
593 Standard_ProgramError_Raise_if (myIsInitialized, "OpenGl_Context::Init() should be called before!");
594 return Standard_False;
597 if (eglMakeCurrent ((EGLDisplay )myDisplay, (EGLSurface )myWindow, (EGLSurface )myWindow, (EGLContext )myGContext) != EGL_TRUE)
599 // if there is no current context it might be impossible to use glGetError() correctly
600 PushMessage (GL_DEBUG_SOURCE_WINDOW_SYSTEM, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH,
601 "eglMakeCurrent() has failed!");
602 myIsInitialized = Standard_False;
603 return Standard_False;
605 #elif defined(_WIN32)
606 if (myWindowDC == NULL || myGContext == NULL)
608 Standard_ProgramError_Raise_if (myIsInitialized, "OpenGl_Context::Init() should be called before!");
609 return Standard_False;
612 // technically it should be safe to activate already bound GL context
613 // however some drivers (Intel etc.) may FAIL doing this for unknown reason
616 myShaderManager->SetContext (this);
617 return Standard_True;
619 else if (wglMakeCurrent ((HDC )myWindowDC, (HGLRC )myGContext) != TRUE)
621 // notice that glGetError() couldn't be used here!
622 wchar_t* aMsgBuff = NULL;
623 DWORD anErrorCode = GetLastError();
624 FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
625 NULL, anErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (wchar_t* )&aMsgBuff, 0, NULL);
626 TCollection_ExtendedString aMsg ("wglMakeCurrent() has failed. ");
627 if (aMsgBuff != NULL)
629 aMsg += (Standard_ExtString )aMsgBuff;
630 LocalFree (aMsgBuff);
632 PushMessage (GL_DEBUG_SOURCE_WINDOW_SYSTEM, GL_DEBUG_TYPE_ERROR, (unsigned int )anErrorCode, GL_DEBUG_SEVERITY_HIGH, aMsg);
633 myIsInitialized = Standard_False;
634 return Standard_False;
637 if (myDisplay == NULL || myWindow == 0 || myGContext == 0)
639 Standard_ProgramError_Raise_if (myIsInitialized, "OpenGl_Context::Init() should be called before!");
640 return Standard_False;
643 if (!glXMakeCurrent ((Display* )myDisplay, (GLXDrawable )myWindow, (GLXContext )myGContext))
645 // if there is no current context it might be impossible to use glGetError() correctly
646 PushMessage (GL_DEBUG_SOURCE_WINDOW_SYSTEM, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH,
647 "glXMakeCurrent() has failed!");
648 myIsInitialized = Standard_False;
649 return Standard_False;
652 myShaderManager->SetContext (this);
653 return Standard_True;
656 // =======================================================================
657 // function : SwapBuffers
659 // =======================================================================
660 void OpenGl_Context::SwapBuffers()
662 #if defined(HAVE_EGL)
663 if ((EGLSurface )myWindow != EGL_NO_SURFACE)
665 eglSwapBuffers ((EGLDisplay )myDisplay, (EGLSurface )myWindow);
667 #elif defined(_WIN32)
668 if ((HDC )myWindowDC != NULL)
670 ::SwapBuffers ((HDC )myWindowDC);
674 if ((Display* )myDisplay != NULL)
676 glXSwapBuffers ((Display* )myDisplay, (GLXDrawable )myWindow);
683 // =======================================================================
684 // function : SetSwapInterval
686 // =======================================================================
687 Standard_Boolean OpenGl_Context::SetSwapInterval (const Standard_Integer theInterval)
689 #if defined(HAVE_EGL)
690 if (::eglSwapInterval ((EGLDisplay )myDisplay, theInterval) == EGL_TRUE)
692 return Standard_True;
694 #elif defined(_WIN32)
695 if (myFuncs->wglSwapIntervalEXT != NULL)
697 myFuncs->wglSwapIntervalEXT (theInterval);
698 return Standard_True;
700 #elif defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
702 #elif defined(__APPLE__)
703 if (::CGLSetParameter (CGLGetCurrentContext(), kCGLCPSwapInterval, &theInterval) == kCGLNoError)
705 return Standard_True;
708 if (theInterval == -1
709 && myFuncs->glXSwapIntervalEXT != NULL)
711 typedef int (*glXSwapIntervalEXT_t_x)(Display* theDisplay, GLXDrawable theDrawable, int theInterval);
712 glXSwapIntervalEXT_t_x aFuncPtr = (glXSwapIntervalEXT_t_x )myFuncs->glXSwapIntervalEXT;
713 aFuncPtr ((Display* )myDisplay, (GLXDrawable )myWindow, theInterval);
714 return Standard_True;
716 else if (myFuncs->glXSwapIntervalSGI != NULL)
718 myFuncs->glXSwapIntervalSGI (theInterval);
719 return Standard_True;
722 return Standard_False;
725 // =======================================================================
726 // function : findProc
728 // =======================================================================
729 void* OpenGl_Context::findProc (const char* theFuncName)
731 #if defined(HAVE_EGL)
732 return (void* )eglGetProcAddress (theFuncName);
733 #elif defined(_WIN32)
734 return (void* )wglGetProcAddress (theFuncName);
735 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
736 return (myGlLibHandle != NULL) ? dlsym (myGlLibHandle, theFuncName) : NULL;
738 return (void* )glXGetProcAddress ((const GLubyte* )theFuncName);
742 // =======================================================================
743 // function : CheckExtension
745 // =======================================================================
746 Standard_Boolean OpenGl_Context::CheckExtension (const char* theExtName) const
748 if (theExtName == NULL)
751 std::cerr << "CheckExtension called with NULL string!\n";
753 return Standard_False;
756 #if !defined(GL_ES_VERSION_2_0)
757 // available since OpenGL 3.0
758 // and the ONLY way to check extensions with OpenGL 3.1+ core profile
759 if (IsGlGreaterEqual (3, 0)
760 && myFuncs->glGetStringi != NULL)
763 ::glGetIntegerv (GL_NUM_EXTENSIONS, &anExtNb);
764 const size_t anExtNameLen = strlen (theExtName);
765 for (GLint anIter = 0; anIter < anExtNb; ++anIter)
767 const char* anExtension = (const char* )myFuncs->glGetStringi (GL_EXTENSIONS, (GLuint )anIter);
768 const size_t aTestExtNameLen = strlen (anExtension);
769 if (aTestExtNameLen == anExtNameLen
770 && strncmp (anExtension, theExtName, anExtNameLen) == 0)
772 return Standard_True;
775 return Standard_False;
779 // use old way with huge string for all extensions
780 const char* anExtString = (const char* )glGetString (GL_EXTENSIONS);
781 if (anExtString == NULL)
783 Messenger()->Send ("TKOpenGL: glGetString (GL_EXTENSIONS) has returned NULL! No GL context?", Message_Warning);
784 return Standard_False;
786 return CheckExtension (anExtString, theExtName);
789 // =======================================================================
790 // function : CheckExtension
792 // =======================================================================
793 Standard_Boolean OpenGl_Context::CheckExtension (const char* theExtString,
794 const char* theExtName)
796 if (theExtString == NULL)
798 return Standard_False;
801 // Search for theExtName in the extensions string.
802 // Use of strstr() is not sufficient because extension names can be prefixes of other extension names.
803 char* aPtrIter = (char* )theExtString;
804 const char* aPtrEnd = aPtrIter + strlen (theExtString);
805 const size_t anExtNameLen = strlen (theExtName);
806 while (aPtrIter < aPtrEnd)
808 const size_t n = strcspn (aPtrIter, " ");
809 if ((n == anExtNameLen) && (strncmp (aPtrIter, theExtName, anExtNameLen) == 0))
811 return Standard_True;
815 return Standard_False;
818 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
820 // =======================================================================
823 // =======================================================================
824 Standard_Boolean OpenGl_Context::Init (const Standard_Boolean theIsCoreProfile)
828 return Standard_True;
831 #if defined(HAVE_EGL)
832 myDisplay = (Aspect_Display )eglGetCurrentDisplay();
833 myGContext = (Aspect_RenderingContext )eglGetCurrentContext();
834 myWindow = (Aspect_Drawable )eglGetCurrentSurface(EGL_DRAW);
835 #elif defined(_WIN32)
836 myWindowDC = (Aspect_Handle )wglGetCurrentDC();
837 myGContext = (Aspect_RenderingContext )wglGetCurrentContext();
839 myDisplay = (Aspect_Display )glXGetCurrentDisplay();
840 myGContext = (Aspect_RenderingContext )glXGetCurrentContext();
841 myWindow = (Aspect_Drawable )glXGetCurrentDrawable();
843 if (myGContext == NULL)
845 return Standard_False;
848 init (theIsCoreProfile);
849 myIsInitialized = Standard_True;
850 return Standard_True;
855 // =======================================================================
858 // =======================================================================
859 #if defined(HAVE_EGL)
860 Standard_Boolean OpenGl_Context::Init (const Aspect_Drawable theEglSurface,
861 const Aspect_Display theEglDisplay,
862 const Aspect_RenderingContext theEglContext,
863 const Standard_Boolean theIsCoreProfile)
864 #elif defined(_WIN32)
865 Standard_Boolean OpenGl_Context::Init (const Aspect_Handle theWindow,
866 const Aspect_Handle theWindowDC,
867 const Aspect_RenderingContext theGContext,
868 const Standard_Boolean theIsCoreProfile)
869 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
871 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
872 Standard_Boolean OpenGl_Context::Init (EAGLContext* theGContext,
873 const Standard_Boolean theIsCoreProfile)
875 Standard_Boolean OpenGl_Context::Init (NSOpenGLContext* theGContext,
876 const Standard_Boolean theIsCoreProfile)
880 Standard_Boolean OpenGl_Context::Init (const Aspect_Drawable theWindow,
881 const Aspect_Display theDisplay,
882 const Aspect_RenderingContext theGContext,
883 const Standard_Boolean theIsCoreProfile)
886 Standard_ProgramError_Raise_if (myIsInitialized, "OpenGl_Context::Init() should be called only once!");
887 #if defined(HAVE_EGL)
888 myWindow = theEglSurface;
889 myGContext = theEglContext;
890 myDisplay = theEglDisplay;
891 #elif defined(_WIN32)
892 myWindow = theWindow;
893 myGContext = theGContext;
894 myWindowDC = theWindowDC;
895 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
896 myGContext = theGContext;
898 myWindow = theWindow;
899 myGContext = theGContext;
900 myDisplay = theDisplay;
902 if (myGContext == NULL || !MakeCurrent())
904 return Standard_False;
907 init (theIsCoreProfile);
908 myIsInitialized = Standard_True;
909 return Standard_True;
912 // =======================================================================
913 // function : ResetErrors
915 // =======================================================================
916 bool OpenGl_Context::ResetErrors (const bool theToPrintErrors)
919 int anErr = ::glGetError();
920 const bool hasError = anErr != GL_NO_ERROR;
921 if (!theToPrintErrors)
923 for (; anErr != GL_NO_ERROR && aPrevErr != anErr; aPrevErr = anErr, anErr = ::glGetError())
930 for (; anErr != GL_NO_ERROR && aPrevErr != anErr; aPrevErr = anErr, anErr = ::glGetError())
932 TCollection_ExtendedString anErrId;
935 case GL_INVALID_ENUM: anErrId = "GL_INVALID_ENUM"; break;
936 case GL_INVALID_VALUE: anErrId = "GL_INVALID_VALUE"; break;
937 case GL_INVALID_OPERATION: anErrId = "GL_INVALID_OPERATION"; break;
938 #ifdef GL_STACK_OVERFLOW
939 case GL_STACK_OVERFLOW: anErrId = "GL_STACK_OVERFLOW"; break;
940 case GL_STACK_UNDERFLOW: anErrId = "GL_STACK_UNDERFLOW"; break;
942 case GL_OUT_OF_MEMORY: anErrId = "GL_OUT_OF_MEMORY"; break;
943 case GL_INVALID_FRAMEBUFFER_OPERATION:
944 anErrId = "GL_INVALID_FRAMEBUFFER_OPERATION";
947 anErrId = TCollection_ExtendedString("#") + anErr;
951 const TCollection_ExtendedString aMsg = TCollection_ExtendedString ("Unhandled GL error: ") + anErrId;
952 PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_OTHER, 0, GL_DEBUG_SEVERITY_LOW, aMsg);
957 // =======================================================================
958 // function : ReadGlVersion
960 // =======================================================================
961 void OpenGl_Context::ReadGlVersion (Standard_Integer& theGlVerMajor,
962 Standard_Integer& theGlVerMinor)
968 #ifdef GL_MAJOR_VERSION
969 // available since OpenGL 3.0 and OpenGL 3.0 ES
970 GLint aMajor = 0, aMinor = 0;
971 glGetIntegerv (GL_MAJOR_VERSION, &aMajor);
972 glGetIntegerv (GL_MINOR_VERSION, &aMinor);
973 // glGetError() sometimes does not report an error here even if
974 // GL does not know GL_MAJOR_VERSION and GL_MINOR_VERSION constants.
975 // This happens on some renderers like e.g. Cygwin MESA.
976 // Thus checking additionally if GL has put anything to
977 // the output variables.
978 if (::glGetError() == GL_NO_ERROR && aMajor != 0 && aMinor != 0)
980 theGlVerMajor = aMajor;
981 theGlVerMinor = aMinor;
984 for (GLenum anErr = ::glGetError(), aPrevErr = GL_NO_ERROR;; aPrevErr = anErr, anErr = ::glGetError())
986 if (anErr == GL_NO_ERROR
987 || anErr == aPrevErr)
994 // Read version string.
995 // Notice that only first two numbers split by point '2.1 XXXXX' are significant.
996 // Following trash (after space) is vendor-specific.
997 // New drivers also returns micro version of GL like '3.3.0' which has no meaning
998 // and should be considered as vendor-specific too.
999 const char* aVerStr = (const char* )glGetString (GL_VERSION);
1000 if (aVerStr == NULL || *aVerStr == '\0')
1002 // invalid GL context
1006 //#if defined(GL_ES_VERSION_2_0)
1007 // skip "OpenGL ES-** " section
1008 for (; *aVerStr != '\0'; ++aVerStr)
1010 if (*aVerStr >= '0' && *aVerStr <= '9')
1017 // parse string for major number
1020 size_t aMajIter = 0;
1021 while (aVerStr[aMajIter] >= '0' && aVerStr[aMajIter] <= '9')
1025 if (aMajIter == 0 || aMajIter >= sizeof(aMajorStr))
1029 memcpy (aMajorStr, aVerStr, aMajIter);
1030 aMajorStr[aMajIter] = '\0';
1032 // parse string for minor number
1033 aVerStr += aMajIter + 1;
1034 size_t aMinIter = 0;
1035 while (aVerStr[aMinIter] >= '0' && aVerStr[aMinIter] <= '9')
1039 if (aMinIter == 0 || aMinIter >= sizeof(aMinorStr))
1043 memcpy (aMinorStr, aVerStr, aMinIter);
1044 aMinorStr[aMinIter] = '\0';
1047 theGlVerMajor = atoi (aMajorStr);
1048 theGlVerMinor = atoi (aMinorStr);
1050 if (theGlVerMajor <= 0)
1057 static Standard_CString THE_DBGMSG_UNKNOWN = "UNKNOWN";
1058 static Standard_CString THE_DBGMSG_SOURCES[] =
1060 ".OpenGL", // GL_DEBUG_SOURCE_API
1061 ".WinSystem", // GL_DEBUG_SOURCE_WINDOW_SYSTEM
1062 ".GLSL", // GL_DEBUG_SOURCE_SHADER_COMPILER
1063 ".3rdParty", // GL_DEBUG_SOURCE_THIRD_PARTY
1064 "", // GL_DEBUG_SOURCE_APPLICATION
1065 ".Other" // GL_DEBUG_SOURCE_OTHER
1068 static Standard_CString THE_DBGMSG_TYPES[] =
1070 "Error", // GL_DEBUG_TYPE_ERROR
1071 "Deprecated", // GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR
1072 "Undef. behavior", // GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR
1073 "Portability", // GL_DEBUG_TYPE_PORTABILITY
1074 "Performance", // GL_DEBUG_TYPE_PERFORMANCE
1075 "Other" // GL_DEBUG_TYPE_OTHER
1078 static Standard_CString THE_DBGMSG_SEV_HIGH = "High"; // GL_DEBUG_SEVERITY_HIGH
1079 static Standard_CString THE_DBGMSG_SEV_MEDIUM = "Medium"; // GL_DEBUG_SEVERITY_MEDIUM
1080 static Standard_CString THE_DBGMSG_SEV_LOW = "Low"; // GL_DEBUG_SEVERITY_LOW
1082 //! Callback for GL_ARB_debug_output extension
1083 static void APIENTRY debugCallbackWrap(unsigned int theSource,
1084 unsigned int theType,
1086 unsigned int theSeverity,
1088 const char* theMessage,
1089 const void* theUserParam)
1091 OpenGl_Context* aCtx = (OpenGl_Context* )theUserParam;
1092 aCtx->PushMessage (theSource, theType, theId, theSeverity, theMessage);
1095 // =======================================================================
1096 // function : PushMessage
1098 // =======================================================================
1099 void OpenGl_Context::PushMessage (const unsigned int theSource,
1100 const unsigned int theType,
1101 const unsigned int theId,
1102 const unsigned int theSeverity,
1103 const TCollection_ExtendedString& theMessage)
1105 if (caps->suppressExtraMsg
1106 && theSource >= GL_DEBUG_SOURCE_API
1107 && theSource <= GL_DEBUG_SOURCE_OTHER
1108 && myFilters[theSource - GL_DEBUG_SOURCE_API].Contains (theId))
1113 Standard_CString& aSrc = (theSource >= GL_DEBUG_SOURCE_API
1114 && theSource <= GL_DEBUG_SOURCE_OTHER)
1115 ? THE_DBGMSG_SOURCES[theSource - GL_DEBUG_SOURCE_API]
1116 : THE_DBGMSG_UNKNOWN;
1117 Standard_CString& aType = (theType >= GL_DEBUG_TYPE_ERROR
1118 && theType <= GL_DEBUG_TYPE_OTHER)
1119 ? THE_DBGMSG_TYPES[theType - GL_DEBUG_TYPE_ERROR]
1120 : THE_DBGMSG_UNKNOWN;
1121 Standard_CString& aSev = theSeverity == GL_DEBUG_SEVERITY_HIGH
1122 ? THE_DBGMSG_SEV_HIGH
1123 : (theSeverity == GL_DEBUG_SEVERITY_MEDIUM
1124 ? THE_DBGMSG_SEV_MEDIUM
1125 : THE_DBGMSG_SEV_LOW);
1126 Message_Gravity aGrav = theSeverity == GL_DEBUG_SEVERITY_HIGH
1128 : (theSeverity == GL_DEBUG_SEVERITY_MEDIUM
1132 TCollection_ExtendedString aMsg;
1133 aMsg += "TKOpenGl"; aMsg += aSrc;
1134 aMsg += " | Type: "; aMsg += aType;
1135 aMsg += " | ID: "; aMsg += (Standard_Integer )theId;
1136 aMsg += " | Severity: "; aMsg += aSev;
1137 aMsg += " | Message:\n ";
1139 Messenger()->Send (aMsg, aGrav);
1142 // =======================================================================
1143 // function : ExcludeMessage
1145 // ======================================================================
1146 Standard_Boolean OpenGl_Context::ExcludeMessage (const unsigned int theSource,
1147 const unsigned int theId)
1149 return theSource >= GL_DEBUG_SOURCE_API
1150 && theSource <= GL_DEBUG_SOURCE_OTHER
1151 && myFilters[theSource - GL_DEBUG_SOURCE_API].Add (theId);
1154 // =======================================================================
1155 // function : IncludeMessage
1157 // ======================================================================
1158 Standard_Boolean OpenGl_Context::IncludeMessage (const unsigned int theSource,
1159 const unsigned int theId)
1161 return theSource >= GL_DEBUG_SOURCE_API
1162 && theSource <= GL_DEBUG_SOURCE_OTHER
1163 && myFilters[theSource - GL_DEBUG_SOURCE_API].Remove (theId);
1166 // =======================================================================
1167 // function : checkWrongVersion
1169 // ======================================================================
1170 void OpenGl_Context::checkWrongVersion (const Standard_Integer theGlVerMajor,
1171 const Standard_Integer theGlVerMinor)
1173 if (!IsGlGreaterEqual (theGlVerMajor, theGlVerMinor))
1178 TCollection_ExtendedString aMsg = TCollection_ExtendedString()
1179 + "Error! OpenGL context reports version "
1180 + myGlVerMajor + "." + myGlVerMinor
1181 + " but does not export required functions for "
1182 + theGlVerMajor + "." + theGlVerMinor;
1183 PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1184 GL_DEBUG_TYPE_ERROR,
1186 GL_DEBUG_SEVERITY_HIGH,
1190 // =======================================================================
1193 // =======================================================================
1194 void OpenGl_Context::init (const Standard_Boolean theIsCoreProfile)
1199 myMaxMsaaSamples = 0;
1200 myMaxDrawBuffers = 1;
1201 myMaxColorAttachments = 1;
1202 ReadGlVersion (myGlVerMajor, myGlVerMinor);
1203 myVendor = (const char* )::glGetString (GL_VENDOR);
1204 if (!caps->ffpEnable
1205 && !IsGlGreaterEqual (2, 0))
1207 caps->ffpEnable = true;
1208 TCollection_ExtendedString aMsg =
1209 TCollection_ExtendedString("OpenGL driver is too old! Context info:\n")
1210 + " Vendor: " + (const char* )::glGetString (GL_VENDOR) + "\n"
1211 + " Renderer: " + (const char* )::glGetString (GL_RENDERER) + "\n"
1212 + " Version: " + (const char* )::glGetString (GL_VERSION) + "\n"
1213 + " Fallback using deprecated fixed-function pipeline.\n"
1214 + " Visualization might work incorrectly.\n"
1215 " Consider upgrading the graphics driver.";
1216 PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
1219 #if defined(GL_ES_VERSION_2_0)
1220 (void )theIsCoreProfile;
1221 const bool isCoreProfile = false;
1224 if (myVendor.Search ("NVIDIA") != -1)
1226 // Buffer detailed info: Buffer object 1 (bound to GL_ARRAY_BUFFER_ARB, usage hint is GL_STATIC_DRAW)
1227 // will use VIDEO memory as the source for buffer object operations.
1228 ExcludeMessage (GL_DEBUG_SOURCE_API, 131185);
1230 if (IsGlGreaterEqual (3, 0))
1232 // retrieve auxiliary function in advance
1233 FindProc ("glGetStringi", myFuncs->glGetStringi);
1236 bool isCoreProfile = false;
1237 if (IsGlGreaterEqual (3, 2))
1239 isCoreProfile = (theIsCoreProfile == Standard_True);
1241 // detect Core profile
1245 ::glGetIntegerv (GL_CONTEXT_PROFILE_MASK, &aProfile);
1246 isCoreProfile = (aProfile & GL_CONTEXT_CORE_PROFILE_BIT) != 0;
1254 core11 = (OpenGl_GlCore11* )(&(*myFuncs));
1256 core11fwd = (OpenGl_GlCore11Fwd* )(&(*myFuncs));
1274 arbTboRGB32 = Standard_False;
1282 //! Make record shorter to retrieve function pointer using variable with same name
1283 #define FindProcShort(theFunc) FindProc(#theFunc, myFuncs->theFunc)
1285 #if defined(GL_ES_VERSION_2_0)
1287 hasTexRGBA8 = IsGlGreaterEqual (3, 0)
1288 || CheckExtension ("GL_OES_rgb8_rgba8");
1289 // NPOT textures has limited support within OpenGL ES 2.0
1290 // which are relaxed by OpenGL ES 3.0 or some extensions
1291 //arbNPTW = IsGlGreaterEqual (3, 0)
1292 // || CheckExtension ("GL_OES_texture_npot")
1293 // || CheckExtension ("GL_NV_texture_npot_2D_mipmap");
1294 arbNPTW = Standard_True;
1295 arbTexRG = IsGlGreaterEqual (3, 0)
1296 || CheckExtension ("GL_EXT_texture_rg");
1297 extBgra = CheckExtension ("GL_EXT_texture_format_BGRA8888");
1298 extAnis = CheckExtension ("GL_EXT_texture_filter_anisotropic");
1299 extPDS = IsGlGreaterEqual (3, 0)
1300 || CheckExtension ("GL_OES_packed_depth_stencil");
1302 core11fwd = (OpenGl_GlCore11Fwd* )(&(*myFuncs));
1303 if (IsGlGreaterEqual (2, 0))
1305 // enable compatible functions
1306 core20 = (OpenGl_GlCore20* )(&(*myFuncs));
1307 core20fwd = (OpenGl_GlCore20Fwd* )(&(*myFuncs));
1308 core15fwd = (OpenGl_GlCore15Fwd* )(&(*myFuncs));
1309 arbFBO = (OpenGl_ArbFBO* )(&(*myFuncs));
1311 if (IsGlGreaterEqual (3, 0)
1312 && FindProcShort (glBlitFramebuffer))
1314 arbFBOBlit = (OpenGl_ArbFBOBlit* )(&(*myFuncs));
1316 if (IsGlGreaterEqual (3, 0)
1317 && FindProcShort (glGenSamplers)
1318 && FindProcShort (glDeleteSamplers)
1319 && FindProcShort (glIsSampler)
1320 && FindProcShort (glBindSampler)
1321 && FindProcShort (glSamplerParameteri)
1322 && FindProcShort (glSamplerParameteriv)
1323 && FindProcShort (glSamplerParameterf)
1324 && FindProcShort (glSamplerParameterfv)
1325 && FindProcShort (glGetSamplerParameteriv)
1326 && FindProcShort (glGetSamplerParameterfv))
1327 //&& FindProcShort (glSamplerParameterIiv) // only on Desktop or with extensions GL_OES_texture_border_clamp/GL_EXT_texture_border_clamp
1328 //&& FindProcShort (glSamplerParameterIuiv)
1329 //&& FindProcShort (glGetSamplerParameterIiv)
1330 //&& FindProcShort (glGetSamplerParameterIuiv))
1332 arbSamplerObject = (OpenGl_ArbSamplerObject* )(&(*myFuncs));
1334 extFragDepth = !IsGlGreaterEqual(3, 0)
1335 && CheckExtension ("GL_EXT_frag_depth");
1336 if (IsGlGreaterEqual (3, 1)
1337 && FindProc ("glTexStorage2DMultisample", myFuncs->glTexStorage2DMultisample))
1339 // MSAA RenderBuffers have been defined in OpenGL ES 3.0,
1340 // but MSAA Textures - only in OpenGL ES 3.1+
1341 ::glGetIntegerv (GL_MAX_SAMPLES, &myMaxMsaaSamples);
1344 hasUintIndex = IsGlGreaterEqual (3, 0)
1345 || CheckExtension ("GL_OES_element_index_uint");
1346 hasHighp = CheckExtension ("GL_OES_fragment_precision_high");
1347 GLint aRange[2] = {0, 0};
1349 ::glGetShaderPrecisionFormat (GL_FRAGMENT_SHADER, GL_HIGH_FLOAT, aRange, &aPrec);
1352 hasHighp = Standard_True;
1355 arbTexFloat = IsGlGreaterEqual (3, 0)
1356 && FindProc ("glTexImage3D", myFuncs->glTexImage3D);
1358 const Standard_Boolean hasTexBuffer32 = IsGlGreaterEqual (3, 2) && FindProc ("glTexBuffer", myFuncs->glTexBuffer);
1359 const Standard_Boolean hasExtTexBuffer = CheckExtension ("GL_EXT_texture_buffer") && FindProc ("glTexBufferEXT", myFuncs->glTexBuffer);
1361 if (hasTexBuffer32 || hasExtTexBuffer)
1363 arbTBO = reinterpret_cast<OpenGl_ArbTBO*> (myFuncs.get());
1366 // initialize debug context extension
1367 if (CheckExtension ("GL_KHR_debug"))
1369 // this functionality become a part of OpenGL ES 3.2
1371 // According to GL_KHR_debug spec, all functions should have KHR suffix.
1372 // However, some implementations can export these functions without suffix.
1373 if (FindProc ("glDebugMessageControlKHR", myFuncs->glDebugMessageControl)
1374 && FindProc ("glDebugMessageInsertKHR", myFuncs->glDebugMessageInsert)
1375 && FindProc ("glDebugMessageCallbackKHR", myFuncs->glDebugMessageCallback)
1376 && FindProc ("glGetDebugMessageLogKHR", myFuncs->glGetDebugMessageLog))
1378 arbDbg = (OpenGl_ArbDbg* )(&(*myFuncs));
1382 && caps->contextDebug)
1384 // setup default callback
1385 myIsGlDebugCtx = Standard_True;
1386 arbDbg->glDebugMessageCallback (debugCallbackWrap, this);
1387 ::glEnable (GL_DEBUG_OUTPUT);
1388 if (caps->contextSyncDebug)
1390 // note that some broken implementations (e.g. simulators) might generate error message on this call
1391 ::glEnable (GL_DEBUG_OUTPUT_SYNCHRONOUS);
1396 extDrawBuffers = CheckExtension ("GL_EXT_draw_buffers") && FindProc ("glDrawBuffersEXT", myFuncs->glDrawBuffers);
1397 arbDrawBuffers = CheckExtension ("GL_ARB_draw_buffers") && FindProc ("glDrawBuffersARB", myFuncs->glDrawBuffers);
1399 if (IsGlGreaterEqual (3, 0) && FindProc ("glDrawBuffers", myFuncs->glDrawBuffers))
1401 hasDrawBuffers = OpenGl_FeatureInCore;
1403 else if (extDrawBuffers || arbDrawBuffers)
1405 hasDrawBuffers = OpenGl_FeatureInExtensions;
1408 hasFloatBuffer = IsGlGreaterEqual (3, 2) ? OpenGl_FeatureInCore :
1409 CheckExtension ("GL_EXT_color_buffer_float") ? OpenGl_FeatureInExtensions
1410 : OpenGl_FeatureNotAvailable;
1411 hasHalfFloatBuffer = IsGlGreaterEqual (3, 2) ? OpenGl_FeatureInCore :
1412 CheckExtension ("GL_EXT_color_buffer_half_float") ? OpenGl_FeatureInExtensions
1413 : OpenGl_FeatureNotAvailable;
1415 oesSampleVariables = CheckExtension ("GL_OES_sample_variables");
1416 oesStdDerivatives = CheckExtension ("GL_OES_standard_derivatives");
1417 hasSampleVariables = IsGlGreaterEqual (3, 2) ? OpenGl_FeatureInCore :
1418 oesSampleVariables ? OpenGl_FeatureInExtensions
1419 : OpenGl_FeatureNotAvailable;
1422 myTexClamp = IsGlGreaterEqual (1, 2) ? GL_CLAMP_TO_EDGE : GL_CLAMP;
1424 hasTexRGBA8 = Standard_True;
1425 arbDrawBuffers = CheckExtension ("GL_ARB_draw_buffers");
1426 arbNPTW = CheckExtension ("GL_ARB_texture_non_power_of_two");
1427 arbTexFloat = IsGlGreaterEqual (3, 0)
1428 || CheckExtension ("GL_ARB_texture_float");
1429 arbSampleShading = CheckExtension ("GL_ARB_sample_shading");
1430 extBgra = CheckExtension ("GL_EXT_bgra");
1431 extAnis = CheckExtension ("GL_EXT_texture_filter_anisotropic");
1432 extPDS = CheckExtension ("GL_EXT_packed_depth_stencil");
1433 atiMem = CheckExtension ("GL_ATI_meminfo");
1434 nvxMem = CheckExtension ("GL_NVX_gpu_memory_info");
1436 hasDrawBuffers = IsGlGreaterEqual (2, 0) ? OpenGl_FeatureInCore :
1437 arbDrawBuffers ? OpenGl_FeatureInExtensions
1438 : OpenGl_FeatureNotAvailable;
1440 hasFloatBuffer = hasHalfFloatBuffer = IsGlGreaterEqual (3, 0) ? OpenGl_FeatureInCore :
1441 CheckExtension ("GL_ARB_color_buffer_float") ? OpenGl_FeatureInExtensions
1442 : OpenGl_FeatureNotAvailable;
1444 hasSampleVariables = IsGlGreaterEqual (4, 0) ? OpenGl_FeatureInCore :
1445 arbSampleShading ? OpenGl_FeatureInExtensions
1446 : OpenGl_FeatureNotAvailable;
1448 GLint aStereo = GL_FALSE;
1449 glGetIntegerv (GL_STEREO, &aStereo);
1450 myIsStereoBuffers = aStereo == 1;
1452 // get number of maximum clipping planes
1453 glGetIntegerv (GL_MAX_CLIP_PLANES, &myMaxClipPlanes);
1458 glGetIntegerv (GL_MAX_DRAW_BUFFERS, &myMaxDrawBuffers);
1459 glGetIntegerv (GL_MAX_COLOR_ATTACHMENTS, &myMaxColorAttachments);
1462 glGetIntegerv (GL_MAX_TEXTURE_SIZE, &myMaxTexDim);
1463 if (IsGlGreaterEqual (1, 5))
1465 glGetIntegerv (GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &myMaxTexCombined);
1470 glGetIntegerv (GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &myAnisoMax);
1473 myClippingState.Init (myMaxClipPlanes);
1475 #if !defined(GL_ES_VERSION_2_0)
1493 // retrieve platform-dependent extensions
1494 #if defined(HAVE_EGL)
1496 #elif defined(_WIN32)
1497 if (FindProcShort (wglGetExtensionsStringARB))
1499 const char* aWglExts = myFuncs->wglGetExtensionsStringARB (wglGetCurrentDC());
1500 if (CheckExtension (aWglExts, "WGL_EXT_swap_control"))
1502 FindProcShort (wglSwapIntervalEXT);
1504 if (CheckExtension (aWglExts, "WGL_ARB_pixel_format"))
1506 FindProcShort (wglChoosePixelFormatARB);
1508 if (CheckExtension (aWglExts, "WGL_ARB_create_context_profile"))
1510 FindProcShort (wglCreateContextAttribsARB);
1512 if (CheckExtension (aWglExts, "WGL_NV_DX_interop"))
1514 FindProcShort (wglDXSetResourceShareHandleNV);
1515 FindProcShort (wglDXOpenDeviceNV);
1516 FindProcShort (wglDXCloseDeviceNV);
1517 FindProcShort (wglDXRegisterObjectNV);
1518 FindProcShort (wglDXUnregisterObjectNV);
1519 FindProcShort (wglDXObjectAccessNV);
1520 FindProcShort (wglDXLockObjectsNV);
1521 FindProcShort (wglDXUnlockObjectsNV);
1523 if (CheckExtension (aWglExts, "WGL_AMD_gpu_association"))
1525 FindProcShort (wglGetGPUIDsAMD);
1526 FindProcShort (wglGetGPUInfoAMD);
1527 FindProcShort (wglGetContextGPUIDAMD);
1530 #elif defined(__APPLE__)
1533 const char* aGlxExts = ::glXQueryExtensionsString ((Display* )myDisplay, DefaultScreen ((Display* )myDisplay));
1534 if (CheckExtension (aGlxExts, "GLX_EXT_swap_control"))
1536 FindProcShort (glXSwapIntervalEXT);
1538 if (CheckExtension (aGlxExts, "GLX_SGI_swap_control"))
1540 FindProcShort (glXSwapIntervalSGI);
1542 if (CheckExtension (aGlxExts, "GLX_MESA_query_renderer"))
1544 FindProcShort (glXQueryRendererIntegerMESA);
1545 FindProcShort (glXQueryCurrentRendererIntegerMESA);
1546 FindProcShort (glXQueryRendererStringMESA);
1547 FindProcShort (glXQueryCurrentRendererStringMESA);
1549 //extSwapTear = CheckExtension (aGlxExts, "GLX_EXT_swap_control_tear");
1552 // load OpenGL 1.2 new functions
1553 has12 = IsGlGreaterEqual (1, 2)
1554 && FindProcShort (glBlendColor)
1555 && FindProcShort (glBlendEquation)
1556 && FindProcShort (glDrawRangeElements)
1557 && FindProcShort (glTexImage3D)
1558 && FindProcShort (glTexSubImage3D)
1559 && FindProcShort (glCopyTexSubImage3D);
1561 // load OpenGL 1.3 new functions
1562 has13 = IsGlGreaterEqual (1, 3)
1563 && FindProcShort (glActiveTexture)
1564 && FindProcShort (glSampleCoverage)
1565 && FindProcShort (glCompressedTexImage3D)
1566 && FindProcShort (glCompressedTexImage2D)
1567 && FindProcShort (glCompressedTexImage1D)
1568 && FindProcShort (glCompressedTexSubImage3D)
1569 && FindProcShort (glCompressedTexSubImage2D)
1570 && FindProcShort (glCompressedTexSubImage1D)
1571 && FindProcShort (glGetCompressedTexImage);
1576 && FindProcShort (glClientActiveTexture)
1577 && FindProcShort (glMultiTexCoord1d)
1578 && FindProcShort (glMultiTexCoord1dv)
1579 && FindProcShort (glMultiTexCoord1f)
1580 && FindProcShort (glMultiTexCoord1fv)
1581 && FindProcShort (glMultiTexCoord1i)
1582 && FindProcShort (glMultiTexCoord1iv)
1583 && FindProcShort (glMultiTexCoord1s)
1584 && FindProcShort (glMultiTexCoord1sv)
1585 && FindProcShort (glMultiTexCoord2d)
1586 && FindProcShort (glMultiTexCoord2dv)
1587 && FindProcShort (glMultiTexCoord2f)
1588 && FindProcShort (glMultiTexCoord2fv)
1589 && FindProcShort (glMultiTexCoord2i)
1590 && FindProcShort (glMultiTexCoord2iv)
1591 && FindProcShort (glMultiTexCoord2s)
1592 && FindProcShort (glMultiTexCoord2sv)
1593 && FindProcShort (glMultiTexCoord3d)
1594 && FindProcShort (glMultiTexCoord3dv)
1595 && FindProcShort (glMultiTexCoord3f)
1596 && FindProcShort (glMultiTexCoord3fv)
1597 && FindProcShort (glMultiTexCoord3i)
1598 && FindProcShort (glMultiTexCoord3iv)
1599 && FindProcShort (glMultiTexCoord3s)
1600 && FindProcShort (glMultiTexCoord3sv)
1601 && FindProcShort (glMultiTexCoord4d)
1602 && FindProcShort (glMultiTexCoord4dv)
1603 && FindProcShort (glMultiTexCoord4f)
1604 && FindProcShort (glMultiTexCoord4fv)
1605 && FindProcShort (glMultiTexCoord4i)
1606 && FindProcShort (glMultiTexCoord4iv)
1607 && FindProcShort (glMultiTexCoord4s)
1608 && FindProcShort (glMultiTexCoord4sv)
1609 && FindProcShort (glLoadTransposeMatrixf)
1610 && FindProcShort (glLoadTransposeMatrixd)
1611 && FindProcShort (glMultTransposeMatrixf)
1612 && FindProcShort (glMultTransposeMatrixd);
1615 // load OpenGL 1.4 new functions
1616 has14 = IsGlGreaterEqual (1, 4)
1617 && FindProcShort (glBlendFuncSeparate)
1618 && FindProcShort (glMultiDrawArrays)
1619 && FindProcShort (glMultiDrawElements)
1620 && FindProcShort (glPointParameterf)
1621 && FindProcShort (glPointParameterfv)
1622 && FindProcShort (glPointParameteri)
1623 && FindProcShort (glPointParameteriv);
1625 // load OpenGL 1.5 new functions
1626 has15 = IsGlGreaterEqual (1, 5)
1627 && FindProcShort (glGenQueries)
1628 && FindProcShort (glDeleteQueries)
1629 && FindProcShort (glIsQuery)
1630 && FindProcShort (glBeginQuery)
1631 && FindProcShort (glEndQuery)
1632 && FindProcShort (glGetQueryiv)
1633 && FindProcShort (glGetQueryObjectiv)
1634 && FindProcShort (glGetQueryObjectuiv)
1635 && FindProcShort (glBindBuffer)
1636 && FindProcShort (glDeleteBuffers)
1637 && FindProcShort (glGenBuffers)
1638 && FindProcShort (glIsBuffer)
1639 && FindProcShort (glBufferData)
1640 && FindProcShort (glBufferSubData)
1641 && FindProcShort (glGetBufferSubData)
1642 && FindProcShort (glMapBuffer)
1643 && FindProcShort (glUnmapBuffer)
1644 && FindProcShort (glGetBufferParameteriv)
1645 && FindProcShort (glGetBufferPointerv);
1647 // load OpenGL 2.0 new functions
1648 has20 = IsGlGreaterEqual (2, 0)
1649 && FindProcShort (glBlendEquationSeparate)
1650 && FindProcShort (glDrawBuffers)
1651 && FindProcShort (glStencilOpSeparate)
1652 && FindProcShort (glStencilFuncSeparate)
1653 && FindProcShort (glStencilMaskSeparate)
1654 && FindProcShort (glAttachShader)
1655 && FindProcShort (glBindAttribLocation)
1656 && FindProcShort (glCompileShader)
1657 && FindProcShort (glCreateProgram)
1658 && FindProcShort (glCreateShader)
1659 && FindProcShort (glDeleteProgram)
1660 && FindProcShort (glDeleteShader)
1661 && FindProcShort (glDetachShader)
1662 && FindProcShort (glDisableVertexAttribArray)
1663 && FindProcShort (glEnableVertexAttribArray)
1664 && FindProcShort (glGetActiveAttrib)
1665 && FindProcShort (glGetActiveUniform)
1666 && FindProcShort (glGetAttachedShaders)
1667 && FindProcShort (glGetAttribLocation)
1668 && FindProcShort (glGetProgramiv)
1669 && FindProcShort (glGetProgramInfoLog)
1670 && FindProcShort (glGetShaderiv)
1671 && FindProcShort (glGetShaderInfoLog)
1672 && FindProcShort (glGetShaderSource)
1673 && FindProcShort (glGetUniformLocation)
1674 && FindProcShort (glGetUniformfv)
1675 && FindProcShort (glGetUniformiv)
1676 && FindProcShort (glGetVertexAttribdv)
1677 && FindProcShort (glGetVertexAttribfv)
1678 && FindProcShort (glGetVertexAttribiv)
1679 && FindProcShort (glGetVertexAttribPointerv)
1680 && FindProcShort (glIsProgram)
1681 && FindProcShort (glIsShader)
1682 && FindProcShort (glLinkProgram)
1683 && FindProcShort (glShaderSource)
1684 && FindProcShort (glUseProgram)
1685 && FindProcShort (glUniform1f)
1686 && FindProcShort (glUniform2f)
1687 && FindProcShort (glUniform3f)
1688 && FindProcShort (glUniform4f)
1689 && FindProcShort (glUniform1i)
1690 && FindProcShort (glUniform2i)
1691 && FindProcShort (glUniform3i)
1692 && FindProcShort (glUniform4i)
1693 && FindProcShort (glUniform1fv)
1694 && FindProcShort (glUniform2fv)
1695 && FindProcShort (glUniform3fv)
1696 && FindProcShort (glUniform4fv)
1697 && FindProcShort (glUniform1iv)
1698 && FindProcShort (glUniform2iv)
1699 && FindProcShort (glUniform3iv)
1700 && FindProcShort (glUniform4iv)
1701 && FindProcShort (glUniformMatrix2fv)
1702 && FindProcShort (glUniformMatrix3fv)
1703 && FindProcShort (glUniformMatrix4fv)
1704 && FindProcShort (glValidateProgram)
1705 && FindProcShort (glVertexAttrib1d)
1706 && FindProcShort (glVertexAttrib1dv)
1707 && FindProcShort (glVertexAttrib1f)
1708 && FindProcShort (glVertexAttrib1fv)
1709 && FindProcShort (glVertexAttrib1s)
1710 && FindProcShort (glVertexAttrib1sv)
1711 && FindProcShort (glVertexAttrib2d)
1712 && FindProcShort (glVertexAttrib2dv)
1713 && FindProcShort (glVertexAttrib2f)
1714 && FindProcShort (glVertexAttrib2fv)
1715 && FindProcShort (glVertexAttrib2s)
1716 && FindProcShort (glVertexAttrib2sv)
1717 && FindProcShort (glVertexAttrib3d)
1718 && FindProcShort (glVertexAttrib3dv)
1719 && FindProcShort (glVertexAttrib3f)
1720 && FindProcShort (glVertexAttrib3fv)
1721 && FindProcShort (glVertexAttrib3s)
1722 && FindProcShort (glVertexAttrib3sv)
1723 && FindProcShort (glVertexAttrib4Nbv)
1724 && FindProcShort (glVertexAttrib4Niv)
1725 && FindProcShort (glVertexAttrib4Nsv)
1726 && FindProcShort (glVertexAttrib4Nub)
1727 && FindProcShort (glVertexAttrib4Nubv)
1728 && FindProcShort (glVertexAttrib4Nuiv)
1729 && FindProcShort (glVertexAttrib4Nusv)
1730 && FindProcShort (glVertexAttrib4bv)
1731 && FindProcShort (glVertexAttrib4d)
1732 && FindProcShort (glVertexAttrib4dv)
1733 && FindProcShort (glVertexAttrib4f)
1734 && FindProcShort (glVertexAttrib4fv)
1735 && FindProcShort (glVertexAttrib4iv)
1736 && FindProcShort (glVertexAttrib4s)
1737 && FindProcShort (glVertexAttrib4sv)
1738 && FindProcShort (glVertexAttrib4ubv)
1739 && FindProcShort (glVertexAttrib4uiv)
1740 && FindProcShort (glVertexAttrib4usv)
1741 && FindProcShort (glVertexAttribPointer);
1743 // load OpenGL 2.1 new functions
1744 has21 = IsGlGreaterEqual (2, 1)
1745 && FindProcShort (glUniformMatrix2x3fv)
1746 && FindProcShort (glUniformMatrix3x2fv)
1747 && FindProcShort (glUniformMatrix2x4fv)
1748 && FindProcShort (glUniformMatrix4x2fv)
1749 && FindProcShort (glUniformMatrix3x4fv)
1750 && FindProcShort (glUniformMatrix4x3fv);
1752 // load GL_ARB_framebuffer_object (added to OpenGL 3.0 core)
1753 const bool hasFBO = (IsGlGreaterEqual (3, 0) || CheckExtension ("GL_ARB_framebuffer_object"))
1754 && FindProcShort (glIsRenderbuffer)
1755 && FindProcShort (glBindRenderbuffer)
1756 && FindProcShort (glDeleteRenderbuffers)
1757 && FindProcShort (glGenRenderbuffers)
1758 && FindProcShort (glRenderbufferStorage)
1759 && FindProcShort (glGetRenderbufferParameteriv)
1760 && FindProcShort (glIsFramebuffer)
1761 && FindProcShort (glBindFramebuffer)
1762 && FindProcShort (glDeleteFramebuffers)
1763 && FindProcShort (glGenFramebuffers)
1764 && FindProcShort (glCheckFramebufferStatus)
1765 && FindProcShort (glFramebufferTexture1D)
1766 && FindProcShort (glFramebufferTexture2D)
1767 && FindProcShort (glFramebufferTexture3D)
1768 && FindProcShort (glFramebufferRenderbuffer)
1769 && FindProcShort (glGetFramebufferAttachmentParameteriv)
1770 && FindProcShort (glGenerateMipmap)
1771 && FindProcShort (glBlitFramebuffer)
1772 && FindProcShort (glRenderbufferStorageMultisample)
1773 && FindProcShort (glFramebufferTextureLayer);
1775 // load GL_ARB_vertex_array_object (added to OpenGL 3.0 core)
1776 const bool hasVAO = (IsGlGreaterEqual (3, 0) || CheckExtension ("GL_ARB_vertex_array_object"))
1777 && FindProcShort (glBindVertexArray)
1778 && FindProcShort (glDeleteVertexArrays)
1779 && FindProcShort (glGenVertexArrays)
1780 && FindProcShort (glIsVertexArray);
1782 // load GL_ARB_map_buffer_range (added to OpenGL 3.0 core)
1783 const bool hasMapBufferRange = (IsGlGreaterEqual (3, 0) || CheckExtension ("GL_ARB_map_buffer_range"))
1784 && FindProcShort (glMapBufferRange)
1785 && FindProcShort (glFlushMappedBufferRange);
1787 // load OpenGL 3.0 new functions
1788 has30 = IsGlGreaterEqual (3, 0)
1791 && hasMapBufferRange
1792 && FindProcShort (glColorMaski)
1793 && FindProcShort (glGetBooleani_v)
1794 && FindProcShort (glGetIntegeri_v)
1795 && FindProcShort (glEnablei)
1796 && FindProcShort (glDisablei)
1797 && FindProcShort (glIsEnabledi)
1798 && FindProcShort (glBeginTransformFeedback)
1799 && FindProcShort (glEndTransformFeedback)
1800 && FindProcShort (glBindBufferRange)
1801 && FindProcShort (glBindBufferBase)
1802 && FindProcShort (glTransformFeedbackVaryings)
1803 && FindProcShort (glGetTransformFeedbackVarying)
1804 && FindProcShort (glClampColor)
1805 && FindProcShort (glBeginConditionalRender)
1806 && FindProcShort (glEndConditionalRender)
1807 && FindProcShort (glVertexAttribIPointer)
1808 && FindProcShort (glGetVertexAttribIiv)
1809 && FindProcShort (glGetVertexAttribIuiv)
1810 && FindProcShort (glVertexAttribI1i)
1811 && FindProcShort (glVertexAttribI2i)
1812 && FindProcShort (glVertexAttribI3i)
1813 && FindProcShort (glVertexAttribI4i)
1814 && FindProcShort (glVertexAttribI1ui)
1815 && FindProcShort (glVertexAttribI2ui)
1816 && FindProcShort (glVertexAttribI3ui)
1817 && FindProcShort (glVertexAttribI4ui)
1818 && FindProcShort (glVertexAttribI1iv)
1819 && FindProcShort (glVertexAttribI2iv)
1820 && FindProcShort (glVertexAttribI3iv)
1821 && FindProcShort (glVertexAttribI4iv)
1822 && FindProcShort (glVertexAttribI1uiv)
1823 && FindProcShort (glVertexAttribI2uiv)
1824 && FindProcShort (glVertexAttribI3uiv)
1825 && FindProcShort (glVertexAttribI4uiv)
1826 && FindProcShort (glVertexAttribI4bv)
1827 && FindProcShort (glVertexAttribI4sv)
1828 && FindProcShort (glVertexAttribI4ubv)
1829 && FindProcShort (glVertexAttribI4usv)
1830 && FindProcShort (glGetUniformuiv)
1831 && FindProcShort (glBindFragDataLocation)
1832 && FindProcShort (glGetFragDataLocation)
1833 && FindProcShort (glUniform1ui)
1834 && FindProcShort (glUniform2ui)
1835 && FindProcShort (glUniform3ui)
1836 && FindProcShort (glUniform4ui)
1837 && FindProcShort (glUniform1uiv)
1838 && FindProcShort (glUniform2uiv)
1839 && FindProcShort (glUniform3uiv)
1840 && FindProcShort (glUniform4uiv)
1841 && FindProcShort (glTexParameterIiv)
1842 && FindProcShort (glTexParameterIuiv)
1843 && FindProcShort (glGetTexParameterIiv)
1844 && FindProcShort (glGetTexParameterIuiv)
1845 && FindProcShort (glClearBufferiv)
1846 && FindProcShort (glClearBufferuiv)
1847 && FindProcShort (glClearBufferfv)
1848 && FindProcShort (glClearBufferfi)
1849 && FindProcShort (glGetStringi);
1851 // load GL_ARB_uniform_buffer_object (added to OpenGL 3.1 core)
1852 const bool hasUBO = (IsGlGreaterEqual (3, 1) || CheckExtension ("GL_ARB_uniform_buffer_object"))
1853 && FindProcShort (glGetUniformIndices)
1854 && FindProcShort (glGetActiveUniformsiv)
1855 && FindProcShort (glGetActiveUniformName)
1856 && FindProcShort (glGetUniformBlockIndex)
1857 && FindProcShort (glGetActiveUniformBlockiv)
1858 && FindProcShort (glGetActiveUniformBlockName)
1859 && FindProcShort (glUniformBlockBinding);
1861 // load GL_ARB_copy_buffer (added to OpenGL 3.1 core)
1862 const bool hasCopyBufSubData = (IsGlGreaterEqual (3, 1) || CheckExtension ("GL_ARB_copy_buffer"))
1863 && FindProcShort (glCopyBufferSubData);
1867 // NPOT textures are required by OpenGL 2.0 specifications
1868 // but doesn't hardware accelerated by some ancient OpenGL 2.1 hardware (GeForce FX, RadeOn 9700 etc.)
1869 arbNPTW = Standard_True;
1870 arbTexRG = Standard_True;
1873 // load OpenGL 3.1 new functions
1874 has31 = IsGlGreaterEqual (3, 1)
1876 && hasCopyBufSubData
1877 && FindProcShort (glDrawArraysInstanced)
1878 && FindProcShort (glDrawElementsInstanced)
1879 && FindProcShort (glTexBuffer)
1880 && FindProcShort (glPrimitiveRestartIndex);
1882 // load GL_ARB_draw_elements_base_vertex (added to OpenGL 3.2 core)
1883 const bool hasDrawElemsBaseVert = (IsGlGreaterEqual (3, 2) || CheckExtension ("GL_ARB_draw_elements_base_vertex"))
1884 && FindProcShort (glDrawElementsBaseVertex)
1885 && FindProcShort (glDrawRangeElementsBaseVertex)
1886 && FindProcShort (glDrawElementsInstancedBaseVertex)
1887 && FindProcShort (glMultiDrawElementsBaseVertex);
1889 // load GL_ARB_provoking_vertex (added to OpenGL 3.2 core)
1890 const bool hasProvokingVert = (IsGlGreaterEqual (3, 2) || CheckExtension ("GL_ARB_provoking_vertex"))
1891 && FindProcShort (glProvokingVertex);
1893 // load GL_ARB_sync (added to OpenGL 3.2 core)
1894 const bool hasSync = (IsGlGreaterEqual (3, 2) || CheckExtension ("GL_ARB_sync"))
1895 && FindProcShort (glFenceSync)
1896 && FindProcShort (glIsSync)
1897 && FindProcShort (glDeleteSync)
1898 && FindProcShort (glClientWaitSync)
1899 && FindProcShort (glWaitSync)
1900 && FindProcShort (glGetInteger64v)
1901 && FindProcShort (glGetSynciv);
1903 // load GL_ARB_texture_multisample (added to OpenGL 3.2 core)
1904 const bool hasTextureMultisample = (IsGlGreaterEqual (3, 2) || CheckExtension ("GL_ARB_texture_multisample"))
1905 && FindProcShort (glTexImage2DMultisample)
1906 && FindProcShort (glTexImage3DMultisample)
1907 && FindProcShort (glGetMultisamplefv)
1908 && FindProcShort (glSampleMaski);
1910 // load OpenGL 3.2 new functions
1911 has32 = IsGlGreaterEqual (3, 2)
1912 && hasDrawElemsBaseVert
1915 && hasTextureMultisample
1916 && FindProcShort (glGetInteger64i_v)
1917 && FindProcShort (glGetBufferParameteri64v)
1918 && FindProcShort (glFramebufferTexture);
1920 // load GL_ARB_blend_func_extended (added to OpenGL 3.3 core)
1921 const bool hasBlendFuncExtended = (IsGlGreaterEqual (3, 3) || CheckExtension ("GL_ARB_blend_func_extended"))
1922 && FindProcShort (glBindFragDataLocationIndexed)
1923 && FindProcShort (glGetFragDataIndex);
1925 // load GL_ARB_sampler_objects (added to OpenGL 3.3 core)
1926 const bool hasSamplerObjects = (IsGlGreaterEqual (3, 3) || CheckExtension ("GL_ARB_sampler_objects"))
1927 && FindProcShort (glGenSamplers)
1928 && FindProcShort (glDeleteSamplers)
1929 && FindProcShort (glIsSampler)
1930 && FindProcShort (glBindSampler)
1931 && FindProcShort (glSamplerParameteri)
1932 && FindProcShort (glSamplerParameteriv)
1933 && FindProcShort (glSamplerParameterf)
1934 && FindProcShort (glSamplerParameterfv)
1935 && FindProcShort (glSamplerParameterIiv)
1936 && FindProcShort (glSamplerParameterIuiv)
1937 && FindProcShort (glGetSamplerParameteriv)
1938 && FindProcShort (glGetSamplerParameterIiv)
1939 && FindProcShort (glGetSamplerParameterfv)
1940 && FindProcShort (glGetSamplerParameterIuiv);
1941 if (hasSamplerObjects)
1943 arbSamplerObject = (OpenGl_ArbSamplerObject* )(&(*myFuncs));
1946 // load GL_ARB_timer_query (added to OpenGL 3.3 core)
1947 const bool hasTimerQuery = (IsGlGreaterEqual (3, 3) || CheckExtension ("GL_ARB_timer_query"))
1948 && FindProcShort (glQueryCounter)
1949 && FindProcShort (glGetQueryObjecti64v)
1950 && FindProcShort (glGetQueryObjectui64v);
1952 // load GL_ARB_vertex_type_2_10_10_10_rev (added to OpenGL 3.3 core)
1953 const bool hasVertType21010101rev = (IsGlGreaterEqual (3, 3) || CheckExtension ("GL_ARB_vertex_type_2_10_10_10_rev"))
1954 && FindProcShort (glVertexAttribP1ui)
1955 && FindProcShort (glVertexAttribP1uiv)
1956 && FindProcShort (glVertexAttribP2ui)
1957 && FindProcShort (glVertexAttribP2uiv)
1958 && FindProcShort (glVertexAttribP3ui)
1959 && FindProcShort (glVertexAttribP3uiv)
1960 && FindProcShort (glVertexAttribP4ui)
1961 && FindProcShort (glVertexAttribP4uiv);
1963 if ( hasVertType21010101rev
1966 // load deprecated functions
1967 const bool hasVertType21010101revExt =
1968 FindProcShort (glVertexP2ui)
1969 && FindProcShort (glVertexP2uiv)
1970 && FindProcShort (glVertexP3ui)
1971 && FindProcShort (glVertexP3uiv)
1972 && FindProcShort (glVertexP4ui)
1973 && FindProcShort (glVertexP4uiv)
1974 && FindProcShort (glTexCoordP1ui)
1975 && FindProcShort (glTexCoordP1uiv)
1976 && FindProcShort (glTexCoordP2ui)
1977 && FindProcShort (glTexCoordP2uiv)
1978 && FindProcShort (glTexCoordP3ui)
1979 && FindProcShort (glTexCoordP3uiv)
1980 && FindProcShort (glTexCoordP4ui)
1981 && FindProcShort (glTexCoordP4uiv)
1982 && FindProcShort (glMultiTexCoordP1ui)
1983 && FindProcShort (glMultiTexCoordP1uiv)
1984 && FindProcShort (glMultiTexCoordP2ui)
1985 && FindProcShort (glMultiTexCoordP2uiv)
1986 && FindProcShort (glMultiTexCoordP3ui)
1987 && FindProcShort (glMultiTexCoordP3uiv)
1988 && FindProcShort (glMultiTexCoordP4ui)
1989 && FindProcShort (glMultiTexCoordP4uiv)
1990 && FindProcShort (glNormalP3ui)
1991 && FindProcShort (glNormalP3uiv)
1992 && FindProcShort (glColorP3ui)
1993 && FindProcShort (glColorP3uiv)
1994 && FindProcShort (glColorP4ui)
1995 && FindProcShort (glColorP4uiv)
1996 && FindProcShort (glSecondaryColorP3ui)
1997 && FindProcShort (glSecondaryColorP3uiv);
1998 (void )hasVertType21010101revExt;
2001 // load OpenGL 3.3 extra functions
2002 has33 = IsGlGreaterEqual (3, 3)
2003 && hasBlendFuncExtended
2004 && hasSamplerObjects
2006 && hasVertType21010101rev
2007 && FindProcShort (glVertexAttribDivisor);
2009 // load GL_ARB_draw_indirect (added to OpenGL 4.0 core)
2010 const bool hasDrawIndirect = (IsGlGreaterEqual (4, 0) || CheckExtension ("GL_ARB_draw_indirect"))
2011 && FindProcShort (glDrawArraysIndirect)
2012 && FindProcShort (glDrawElementsIndirect);
2014 // load GL_ARB_gpu_shader_fp64 (added to OpenGL 4.0 core)
2015 const bool hasShaderFP64 = (IsGlGreaterEqual (4, 0) || CheckExtension ("GL_ARB_gpu_shader_fp64"))
2016 && FindProcShort (glUniform1d)
2017 && FindProcShort (glUniform2d)
2018 && FindProcShort (glUniform3d)
2019 && FindProcShort (glUniform4d)
2020 && FindProcShort (glUniform1dv)
2021 && FindProcShort (glUniform2dv)
2022 && FindProcShort (glUniform3dv)
2023 && FindProcShort (glUniform4dv)
2024 && FindProcShort (glUniformMatrix2dv)
2025 && FindProcShort (glUniformMatrix3dv)
2026 && FindProcShort (glUniformMatrix4dv)
2027 && FindProcShort (glUniformMatrix2x3dv)
2028 && FindProcShort (glUniformMatrix2x4dv)
2029 && FindProcShort (glUniformMatrix3x2dv)
2030 && FindProcShort (glUniformMatrix3x4dv)
2031 && FindProcShort (glUniformMatrix4x2dv)
2032 && FindProcShort (glUniformMatrix4x3dv)
2033 && FindProcShort (glGetUniformdv);
2035 // load GL_ARB_shader_subroutine (added to OpenGL 4.0 core)
2036 const bool hasShaderSubroutine = (IsGlGreaterEqual (4, 0) || CheckExtension ("GL_ARB_shader_subroutine"))
2037 && FindProcShort (glGetSubroutineUniformLocation)
2038 && FindProcShort (glGetSubroutineIndex)
2039 && FindProcShort (glGetActiveSubroutineUniformiv)
2040 && FindProcShort (glGetActiveSubroutineUniformName)
2041 && FindProcShort (glGetActiveSubroutineName)
2042 && FindProcShort (glUniformSubroutinesuiv)
2043 && FindProcShort (glGetUniformSubroutineuiv)
2044 && FindProcShort (glGetProgramStageiv);
2046 // load GL_ARB_tessellation_shader (added to OpenGL 4.0 core)
2047 const bool hasTessellationShader = (IsGlGreaterEqual (4, 0) || CheckExtension ("GL_ARB_tessellation_shader"))
2048 && FindProcShort (glPatchParameteri)
2049 && FindProcShort (glPatchParameterfv);
2051 // load GL_ARB_transform_feedback2 (added to OpenGL 4.0 core)
2052 const bool hasTrsfFeedback2 = (IsGlGreaterEqual (4, 0) || CheckExtension ("GL_ARB_transform_feedback2"))
2053 && FindProcShort (glBindTransformFeedback)
2054 && FindProcShort (glDeleteTransformFeedbacks)
2055 && FindProcShort (glGenTransformFeedbacks)
2056 && FindProcShort (glIsTransformFeedback)
2057 && FindProcShort (glPauseTransformFeedback)
2058 && FindProcShort (glResumeTransformFeedback)
2059 && FindProcShort (glDrawTransformFeedback);
2061 // load GL_ARB_transform_feedback3 (added to OpenGL 4.0 core)
2062 const bool hasTrsfFeedback3 = (IsGlGreaterEqual (4, 0) || CheckExtension ("GL_ARB_transform_feedback3"))
2063 && FindProcShort (glDrawTransformFeedbackStream)
2064 && FindProcShort (glBeginQueryIndexed)
2065 && FindProcShort (glEndQueryIndexed)
2066 && FindProcShort (glGetQueryIndexediv);
2068 // load OpenGL 4.0 new functions
2069 has40 = IsGlGreaterEqual (4, 0)
2072 && hasShaderSubroutine
2073 && hasTessellationShader
2076 && FindProcShort (glMinSampleShading)
2077 && FindProcShort (glBlendEquationi)
2078 && FindProcShort (glBlendEquationSeparatei)
2079 && FindProcShort (glBlendFunci)
2080 && FindProcShort (glBlendFuncSeparatei);
2082 // load GL_ARB_ES2_compatibility (added to OpenGL 4.1 core)
2083 const bool hasES2Compatibility = (IsGlGreaterEqual (4, 1) || CheckExtension ("GL_ARB_ES2_compatibility"))
2084 && FindProcShort (glReleaseShaderCompiler)
2085 && FindProcShort (glShaderBinary)
2086 && FindProcShort (glGetShaderPrecisionFormat)
2087 && FindProcShort (glDepthRangef)
2088 && FindProcShort (glClearDepthf);
2090 // load GL_ARB_get_program_binary (added to OpenGL 4.1 core)
2091 const bool hasGetProgramBinary = (IsGlGreaterEqual (4, 1) || CheckExtension ("GL_ARB_get_program_binary"))
2092 && FindProcShort (glGetProgramBinary)
2093 && FindProcShort (glProgramBinary)
2094 && FindProcShort (glProgramParameteri);
2097 // load GL_ARB_separate_shader_objects (added to OpenGL 4.1 core)
2098 const bool hasSeparateShaderObjects = (IsGlGreaterEqual (4, 1) || CheckExtension ("GL_ARB_separate_shader_objects"))
2099 && FindProcShort (glUseProgramStages)
2100 && FindProcShort (glActiveShaderProgram)
2101 && FindProcShort (glCreateShaderProgramv)
2102 && FindProcShort (glBindProgramPipeline)
2103 && FindProcShort (glDeleteProgramPipelines)
2104 && FindProcShort (glGenProgramPipelines)
2105 && FindProcShort (glIsProgramPipeline)
2106 && FindProcShort (glGetProgramPipelineiv)
2107 && FindProcShort (glProgramUniform1i)
2108 && FindProcShort (glProgramUniform1iv)
2109 && FindProcShort (glProgramUniform1f)
2110 && FindProcShort (glProgramUniform1fv)
2111 && FindProcShort (glProgramUniform1d)
2112 && FindProcShort (glProgramUniform1dv)
2113 && FindProcShort (glProgramUniform1ui)
2114 && FindProcShort (glProgramUniform1uiv)
2115 && FindProcShort (glProgramUniform2i)
2116 && FindProcShort (glProgramUniform2iv)
2117 && FindProcShort (glProgramUniform2f)
2118 && FindProcShort (glProgramUniform2fv)
2119 && FindProcShort (glProgramUniform2d)
2120 && FindProcShort (glProgramUniform2dv)
2121 && FindProcShort (glProgramUniform2ui)
2122 && FindProcShort (glProgramUniform2uiv)
2123 && FindProcShort (glProgramUniform3i)
2124 && FindProcShort (glProgramUniform3iv)
2125 && FindProcShort (glProgramUniform3f)
2126 && FindProcShort (glProgramUniform3fv)
2127 && FindProcShort (glProgramUniform3d)
2128 && FindProcShort (glProgramUniform3dv)
2129 && FindProcShort (glProgramUniform3ui)
2130 && FindProcShort (glProgramUniform3uiv)
2131 && FindProcShort (glProgramUniform4i)
2132 && FindProcShort (glProgramUniform4iv)
2133 && FindProcShort (glProgramUniform4f)
2134 && FindProcShort (glProgramUniform4fv)
2135 && FindProcShort (glProgramUniform4d)
2136 && FindProcShort (glProgramUniform4dv)
2137 && FindProcShort (glProgramUniform4ui)
2138 && FindProcShort (glProgramUniform4uiv)
2139 && FindProcShort (glProgramUniformMatrix2fv)
2140 && FindProcShort (glProgramUniformMatrix3fv)
2141 && FindProcShort (glProgramUniformMatrix4fv)
2142 && FindProcShort (glProgramUniformMatrix2dv)
2143 && FindProcShort (glProgramUniformMatrix3dv)
2144 && FindProcShort (glProgramUniformMatrix4dv)
2145 && FindProcShort (glProgramUniformMatrix2x3fv)
2146 && FindProcShort (glProgramUniformMatrix3x2fv)
2147 && FindProcShort (glProgramUniformMatrix2x4fv)
2148 && FindProcShort (glProgramUniformMatrix4x2fv)
2149 && FindProcShort (glProgramUniformMatrix3x4fv)
2150 && FindProcShort (glProgramUniformMatrix4x3fv)
2151 && FindProcShort (glProgramUniformMatrix2x3dv)
2152 && FindProcShort (glProgramUniformMatrix3x2dv)
2153 && FindProcShort (glProgramUniformMatrix2x4dv)
2154 && FindProcShort (glProgramUniformMatrix4x2dv)
2155 && FindProcShort (glProgramUniformMatrix3x4dv)
2156 && FindProcShort (glProgramUniformMatrix4x3dv)
2157 && FindProcShort (glValidateProgramPipeline)
2158 && FindProcShort (glGetProgramPipelineInfoLog);
2160 // load GL_ARB_vertex_attrib_64bit (added to OpenGL 4.1 core)
2161 const bool hasVertAttrib64bit = (IsGlGreaterEqual (4, 1) || CheckExtension ("GL_ARB_vertex_attrib_64bit"))
2162 && FindProcShort (glVertexAttribL1d)
2163 && FindProcShort (glVertexAttribL2d)
2164 && FindProcShort (glVertexAttribL3d)
2165 && FindProcShort (glVertexAttribL4d)
2166 && FindProcShort (glVertexAttribL1dv)
2167 && FindProcShort (glVertexAttribL2dv)
2168 && FindProcShort (glVertexAttribL3dv)
2169 && FindProcShort (glVertexAttribL4dv)
2170 && FindProcShort (glVertexAttribLPointer)
2171 && FindProcShort (glGetVertexAttribLdv);
2173 // load GL_ARB_viewport_array (added to OpenGL 4.1 core)
2174 const bool hasViewportArray = (IsGlGreaterEqual (4, 1) || CheckExtension ("GL_ARB_viewport_array"))
2175 && FindProcShort (glViewportArrayv)
2176 && FindProcShort (glViewportIndexedf)
2177 && FindProcShort (glViewportIndexedfv)
2178 && FindProcShort (glScissorArrayv)
2179 && FindProcShort (glScissorIndexed)
2180 && FindProcShort (glScissorIndexedv)
2181 && FindProcShort (glDepthRangeArrayv)
2182 && FindProcShort (glDepthRangeIndexed)
2183 && FindProcShort (glGetFloati_v)
2184 && FindProcShort (glGetDoublei_v);
2186 has41 = IsGlGreaterEqual (4, 1)
2187 && hasES2Compatibility
2188 && hasGetProgramBinary
2189 && hasSeparateShaderObjects
2190 && hasVertAttrib64bit
2191 && hasViewportArray;
2193 // load GL_ARB_base_instance (added to OpenGL 4.2 core)
2194 const bool hasBaseInstance = (IsGlGreaterEqual (4, 2) || CheckExtension ("GL_ARB_base_instance"))
2195 && FindProcShort (glDrawArraysInstancedBaseInstance)
2196 && FindProcShort (glDrawElementsInstancedBaseInstance)
2197 && FindProcShort (glDrawElementsInstancedBaseVertexBaseInstance);
2199 // load GL_ARB_transform_feedback_instanced (added to OpenGL 4.2 core)
2200 const bool hasTrsfFeedbackInstanced = (IsGlGreaterEqual (4, 2) || CheckExtension ("GL_ARB_transform_feedback_instanced"))
2201 && FindProcShort (glDrawTransformFeedbackInstanced)
2202 && FindProcShort (glDrawTransformFeedbackStreamInstanced);
2204 // load GL_ARB_internalformat_query (added to OpenGL 4.2 core)
2205 const bool hasInternalFormatQuery = (IsGlGreaterEqual (4, 2) || CheckExtension ("GL_ARB_internalformat_query"))
2206 && FindProcShort (glGetInternalformativ);
2208 // load GL_ARB_shader_atomic_counters (added to OpenGL 4.2 core)
2209 const bool hasShaderAtomicCounters = (IsGlGreaterEqual (4, 2) || CheckExtension ("GL_ARB_shader_atomic_counters"))
2210 && FindProcShort (glGetActiveAtomicCounterBufferiv);
2212 // load GL_ARB_shader_image_load_store (added to OpenGL 4.2 core)
2213 const bool hasShaderImgLoadStore = (IsGlGreaterEqual (4, 2) || CheckExtension ("GL_ARB_shader_image_load_store"))
2214 && FindProcShort (glBindImageTexture)
2215 && FindProcShort (glMemoryBarrier);
2217 // load GL_ARB_texture_storage (added to OpenGL 4.2 core)
2218 const bool hasTextureStorage = (IsGlGreaterEqual (4, 2) || CheckExtension ("GL_ARB_texture_storage"))
2219 && FindProcShort (glTexStorage1D)
2220 && FindProcShort (glTexStorage2D)
2221 && FindProcShort (glTexStorage3D);
2223 has42 = IsGlGreaterEqual (4, 2)
2225 && hasTrsfFeedbackInstanced
2226 && hasInternalFormatQuery
2227 && hasShaderAtomicCounters
2228 && hasShaderImgLoadStore
2229 && hasTextureStorage;
2231 has43 = IsGlGreaterEqual (4, 3)
2232 && FindProcShort (glClearBufferData)
2233 && FindProcShort (glClearBufferSubData)
2234 && FindProcShort (glDispatchCompute)
2235 && FindProcShort (glDispatchComputeIndirect)
2236 && FindProcShort (glCopyImageSubData)
2237 && FindProcShort (glFramebufferParameteri)
2238 && FindProcShort (glGetFramebufferParameteriv)
2239 && FindProcShort (glGetInternalformati64v)
2240 && FindProcShort (glInvalidateTexSubImage)
2241 && FindProcShort (glInvalidateTexImage)
2242 && FindProcShort (glInvalidateBufferSubData)
2243 && FindProcShort (glInvalidateBufferData)
2244 && FindProcShort (glInvalidateFramebuffer)
2245 && FindProcShort (glInvalidateSubFramebuffer)
2246 && FindProcShort (glMultiDrawArraysIndirect)
2247 && FindProcShort (glMultiDrawElementsIndirect)
2248 && FindProcShort (glGetProgramInterfaceiv)
2249 && FindProcShort (glGetProgramResourceIndex)
2250 && FindProcShort (glGetProgramResourceName)
2251 && FindProcShort (glGetProgramResourceiv)
2252 && FindProcShort (glGetProgramResourceLocation)
2253 && FindProcShort (glGetProgramResourceLocationIndex)
2254 && FindProcShort (glShaderStorageBlockBinding)
2255 && FindProcShort (glTexBufferRange)
2256 && FindProcShort (glTexStorage2DMultisample)
2257 && FindProcShort (glTexStorage3DMultisample)
2258 && FindProcShort (glTextureView)
2259 && FindProcShort (glBindVertexBuffer)
2260 && FindProcShort (glVertexAttribFormat)
2261 && FindProcShort (glVertexAttribIFormat)
2262 && FindProcShort (glVertexAttribLFormat)
2263 && FindProcShort (glVertexAttribBinding)
2264 && FindProcShort (glVertexBindingDivisor)
2265 && FindProcShort (glDebugMessageControl)
2266 && FindProcShort (glDebugMessageInsert)
2267 && FindProcShort (glDebugMessageCallback)
2268 && FindProcShort (glGetDebugMessageLog)
2269 && FindProcShort (glPushDebugGroup)
2270 && FindProcShort (glPopDebugGroup)
2271 && FindProcShort (glObjectLabel)
2272 && FindProcShort (glGetObjectLabel)
2273 && FindProcShort (glObjectPtrLabel)
2274 && FindProcShort (glGetObjectPtrLabel);
2276 // load GL_ARB_clear_texture (added to OpenGL 4.4 core)
2277 bool arbTexClear = (IsGlGreaterEqual (4, 4) || CheckExtension ("GL_ARB_clear_texture"))
2278 && FindProcShort (glClearTexImage)
2279 && FindProcShort (glClearTexSubImage);
2281 has44 = IsGlGreaterEqual (4, 4)
2283 && FindProcShort (glBufferStorage)
2284 && FindProcShort (glBindBuffersBase)
2285 && FindProcShort (glBindBuffersRange)
2286 && FindProcShort (glBindTextures)
2287 && FindProcShort (glBindSamplers)
2288 && FindProcShort (glBindImageTextures)
2289 && FindProcShort (glBindVertexBuffers);
2291 // initialize debug context extension
2292 if (CheckExtension ("GL_ARB_debug_output"))
2297 arbDbg = (OpenGl_ArbDbg* )(&(*myFuncs));
2299 else if (FindProc ("glDebugMessageControlARB", myFuncs->glDebugMessageControl)
2300 && FindProc ("glDebugMessageInsertARB", myFuncs->glDebugMessageInsert)
2301 && FindProc ("glDebugMessageCallbackARB", myFuncs->glDebugMessageCallback)
2302 && FindProc ("glGetDebugMessageLogARB", myFuncs->glGetDebugMessageLog))
2304 arbDbg = (OpenGl_ArbDbg* )(&(*myFuncs));
2308 && caps->contextDebug)
2310 // setup default callback
2311 myIsGlDebugCtx = Standard_True;
2312 arbDbg->glDebugMessageCallback (debugCallbackWrap, this);
2315 ::glEnable (GL_DEBUG_OUTPUT);
2317 if (caps->contextSyncDebug)
2319 ::glEnable (GL_DEBUG_OUTPUT_SYNCHRONOUS);
2324 // initialize TBO extension (ARB)
2326 && CheckExtension ("GL_ARB_texture_buffer_object")
2327 && FindProc ("glTexBufferARB", myFuncs->glTexBuffer))
2329 arbTBO = (OpenGl_ArbTBO* )(&(*myFuncs));
2331 arbTboRGB32 = CheckExtension ("GL_ARB_texture_buffer_object_rgb32");
2333 // initialize hardware instancing extension (ARB)
2335 && CheckExtension ("GL_ARB_draw_instanced")
2336 && FindProc ("glDrawArraysInstancedARB", myFuncs->glDrawArraysInstanced)
2337 && FindProc ("glDrawElementsInstancedARB", myFuncs->glDrawElementsInstanced))
2339 arbIns = (OpenGl_ArbIns* )(&(*myFuncs));
2342 // initialize FBO extension (ARB)
2345 arbFBO = (OpenGl_ArbFBO* )(&(*myFuncs));
2346 arbFBOBlit = (OpenGl_ArbFBOBlit* )(&(*myFuncs));
2347 extPDS = Standard_True; // extension for EXT, but part of ARB
2350 // initialize GS extension (EXT)
2351 if (CheckExtension ("GL_EXT_geometry_shader4")
2352 && FindProcShort (glProgramParameteriEXT))
2354 extGS = (OpenGl_ExtGS* )(&(*myFuncs));
2357 // initialize bindless texture extension (ARB)
2358 if (CheckExtension ("GL_ARB_bindless_texture")
2359 && FindProcShort (glGetTextureHandleARB)
2360 && FindProcShort (glGetTextureSamplerHandleARB)
2361 && FindProcShort (glMakeTextureHandleResidentARB)
2362 && FindProcShort (glMakeTextureHandleNonResidentARB)
2363 && FindProcShort (glGetImageHandleARB)
2364 && FindProcShort (glMakeImageHandleResidentARB)
2365 && FindProcShort (glMakeImageHandleNonResidentARB)
2366 && FindProcShort (glUniformHandleui64ARB)
2367 && FindProcShort (glUniformHandleui64vARB)
2368 && FindProcShort (glProgramUniformHandleui64ARB)
2369 && FindProcShort (glProgramUniformHandleui64vARB)
2370 && FindProcShort (glIsTextureHandleResidentARB)
2371 && FindProcShort (glIsImageHandleResidentARB)
2372 && FindProcShort (glVertexAttribL1ui64ARB)
2373 && FindProcShort (glVertexAttribL1ui64vARB)
2374 && FindProcShort (glGetVertexAttribLui64vARB))
2376 arbTexBindless = (OpenGl_ArbTexBindless* )(&(*myFuncs));
2381 checkWrongVersion (1, 2);
2388 checkWrongVersion (1, 3);
2395 checkWrongVersion (1, 4);
2402 checkWrongVersion (1, 5);
2409 core15 = (OpenGl_GlCore15* )(&(*myFuncs));
2411 core15fwd = (OpenGl_GlCore15Fwd* )(&(*myFuncs));
2415 checkWrongVersion (2, 0);
2421 const char* aGlslVer = (const char* )::glGetString (GL_SHADING_LANGUAGE_VERSION);
2422 if (aGlslVer == NULL
2423 || *aGlslVer == '\0')
2425 // broken context has been detected
2426 TCollection_ExtendedString aMsg = TCollection_ExtendedString()
2427 + "Error! OpenGL context reports version "
2428 + myGlVerMajor + "." + myGlVerMinor
2429 + " but reports wrong GLSL version";
2430 PushMessage (GL_DEBUG_SOURCE_APPLICATION,
2431 GL_DEBUG_TYPE_ERROR,
2433 GL_DEBUG_SEVERITY_HIGH,
2442 core20 = (OpenGl_GlCore20* )(&(*myFuncs));
2444 core20fwd = (OpenGl_GlCore20Fwd* )(&(*myFuncs));
2448 checkWrongVersion (2, 1);
2456 checkWrongVersion (3, 0);
2462 // MSAA RenderBuffers have been defined in OpenGL 3.0,
2463 // but MSAA Textures - only in OpenGL 3.2+
2465 && CheckExtension ("GL_ARB_texture_multisample")
2466 && FindProcShort (glTexImage2DMultisample))
2468 GLint aNbColorSamples = 0, aNbDepthSamples = 0;
2469 ::glGetIntegerv (GL_MAX_COLOR_TEXTURE_SAMPLES, &aNbColorSamples);
2470 ::glGetIntegerv (GL_MAX_DEPTH_TEXTURE_SAMPLES, &aNbDepthSamples);
2471 myMaxMsaaSamples = Min (aNbColorSamples, aNbDepthSamples);
2474 && CheckExtension ("GL_ARB_texture_storage_multisample")
2475 && FindProcShort (glTexStorage2DMultisample))
2482 checkWrongVersion (3, 1);
2487 arbTBO = (OpenGl_ArbTBO* )(&(*myFuncs));
2488 arbIns = (OpenGl_ArbIns* )(&(*myFuncs));
2490 // check whether ray tracing mode is supported
2491 myHasRayTracing = has31
2493 && arbFBOBlit != NULL;
2495 // check whether textures in ray tracing mode are supported
2496 myHasRayTracingTextures = myHasRayTracing
2497 && arbTexBindless != NULL;
2499 // check whether adaptive screen sampling in ray tracing mode is supported
2500 myHasRayTracingAdaptiveSampling = myHasRayTracing
2502 && CheckExtension ("GL_NV_shader_atomic_float");
2506 checkWrongVersion (3, 2);
2511 core32 = (OpenGl_GlCore32* )(&(*myFuncs));
2514 core32->glGenVertexArrays (1, &myDefaultVao);
2518 core32back = (OpenGl_GlCore32Back* )(&(*myFuncs));
2520 ::glGetIntegerv (GL_MAX_SAMPLES, &myMaxMsaaSamples);
2524 checkWrongVersion (3, 3);
2529 core33 = (OpenGl_GlCore33* )(&(*myFuncs));
2532 core33back = (OpenGl_GlCore33Back* )(&(*myFuncs));
2537 checkWrongVersion (4, 0);
2542 arbTboRGB32 = Standard_True; // in core since OpenGL 4.0
2546 checkWrongVersion (4, 1);
2551 core41 = (OpenGl_GlCore41* )(&(*myFuncs));
2554 core41back = (OpenGl_GlCore41Back* )(&(*myFuncs));
2559 checkWrongVersion (4, 2);
2564 core42 = (OpenGl_GlCore42* )(&(*myFuncs));
2567 core42back = (OpenGl_GlCore42Back* )(&(*myFuncs));
2572 checkWrongVersion (4, 3);
2577 core43 = (OpenGl_GlCore43* )(&(*myFuncs));
2580 core43back = (OpenGl_GlCore43Back* )(&(*myFuncs));
2585 checkWrongVersion (4, 4);
2590 core44 = (OpenGl_GlCore44* )(&(*myFuncs));
2593 core44back = (OpenGl_GlCore44Back* )(&(*myFuncs));
2598 // =======================================================================
2599 // function : MemoryInfo
2601 // =======================================================================
2602 Standard_Size OpenGl_Context::AvailableMemory() const
2604 #if !defined(GL_ES_VERSION_2_0)
2607 // this is actually information for VBO pool
2608 // however because pools are mostly shared
2609 // it can be used for total GPU memory estimations
2612 glGetIntegerv (GL_VBO_FREE_MEMORY_ATI, aMemInfo);
2613 // returned value is in KiB, however this maybe changed in future
2614 return Standard_Size(aMemInfo[0]) * 1024;
2618 // current available dedicated video memory (in KiB), currently unused GPU memory
2620 glGetIntegerv (GL_GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX, &aMemInfo);
2621 return Standard_Size(aMemInfo) * 1024;
2627 // =======================================================================
2628 // function : MemoryInfo
2630 // =======================================================================
2631 TCollection_AsciiString OpenGl_Context::MemoryInfo() const
2633 TColStd_IndexedDataMapOfStringString aDict;
2636 TCollection_AsciiString aText;
2637 for (TColStd_IndexedDataMapOfStringString::Iterator anIter (aDict); anIter.More(); anIter.Next())
2639 if (!aText.IsEmpty())
2643 aText += TCollection_AsciiString(" ") + anIter.Key() + ": " + anIter.Value();
2648 // =======================================================================
2649 // function : MemoryInfo
2651 // =======================================================================
2652 void OpenGl_Context::MemoryInfo (TColStd_IndexedDataMapOfStringString& theDict) const
2654 #if defined(GL_ES_VERSION_2_0)
2656 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
2657 GLint aGlRendId = 0;
2658 CGLGetParameter (CGLGetCurrentContext(), kCGLCPCurrentRendererID, &aGlRendId);
2660 CGLRendererInfoObj aRendObj = NULL;
2661 CGOpenGLDisplayMask aDispMask = CGDisplayIDToOpenGLDisplayMask (kCGDirectMainDisplay);
2663 CGLQueryRendererInfo (aDispMask, &aRendObj, &aRendNb);
2664 for (GLint aRendIter = 0; aRendIter < aRendNb; ++aRendIter)
2667 if (CGLDescribeRenderer (aRendObj, aRendIter, kCGLRPRendererID, &aRendId) != kCGLNoError
2668 || aRendId != aGlRendId)
2673 //kCGLRPVideoMemoryMegabytes = 131;
2674 //kCGLRPTextureMemoryMegabytes = 132;
2676 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
2677 if (CGLDescribeRenderer(aRendObj, aRendIter, kCGLRPVideoMemoryMegabytes, &aVMem) == kCGLNoError)
2679 addInfo (theDict, "GPU memory", TCollection_AsciiString() + aVMem + " MiB");
2681 if (CGLDescribeRenderer(aRendObj, aRendIter, kCGLRPTextureMemoryMegabytes, &aVMem) == kCGLNoError)
2683 addInfo (theDict, "GPU Texture memory", TCollection_AsciiString() + aVMem + " MiB");
2686 if (CGLDescribeRenderer(aRendObj, aRendIter, kCGLRPVideoMemory, &aVMem) == kCGLNoError)
2688 addInfo (theDict, "GPU memory", TCollection_AsciiString() + (aVMem / (1024 * 1024)) + " MiB");
2690 if (CGLDescribeRenderer(aRendObj, aRendIter, kCGLRPTextureMemory, &aVMem) == kCGLNoError)
2692 addInfo (theDict, "GPU Texture memory", TCollection_AsciiString() + (aVMem / (1024 * 1024)) + " MiB");
2698 #if !defined(GL_ES_VERSION_2_0)
2702 memset (aValues, 0, sizeof(aValues));
2703 glGetIntegerv (GL_VBO_FREE_MEMORY_ATI, aValues);
2705 // total memory free in the pool
2706 addInfo (theDict, "GPU free memory", TCollection_AsciiString() + (aValues[0] / 1024) + " MiB");
2708 if (aValues[1] != aValues[0])
2710 // largest available free block in the pool
2711 addInfo (theDict, "Largest free block", TCollection_AsciiString() + (aValues[1] / 1024) + " MiB");
2713 if (aValues[2] != aValues[0])
2715 // total auxiliary memory free
2716 addInfo (theDict, "Free auxiliary memory", TCollection_AsciiString() + (aValues[2] / 1024) + " MiB");
2721 //current available dedicated video memory (in KiB), currently unused GPU memory
2723 glGetIntegerv (GL_GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX, &aValue);
2724 addInfo (theDict, "GPU free memory", TCollection_AsciiString() + (aValue / 1024) + " MiB");
2726 // dedicated video memory, total size (in KiB) of the GPU memory
2727 GLint aDedicated = 0;
2728 glGetIntegerv (GL_GPU_MEMORY_INFO_DEDICATED_VIDMEM_NVX, &aDedicated);
2729 addInfo (theDict, "GPU memory", TCollection_AsciiString() + (aDedicated / 1024) + " MiB");
2731 // total available memory, total size (in KiB) of the memory available for allocations
2732 glGetIntegerv (GL_GPU_MEMORY_INFO_TOTAL_AVAILABLE_MEMORY_NVX, &aValue);
2733 if (aValue != aDedicated)
2735 // different only for special configurations
2736 addInfo (theDict, "Total memory", TCollection_AsciiString() + (aValue / 1024) + " MiB");
2740 else if (myFuncs->wglGetGPUInfoAMD != NULL
2741 && myFuncs->wglGetContextGPUIDAMD != NULL)
2743 GLuint aTotalMemMiB = 0;
2744 UINT anAmdId = myFuncs->wglGetContextGPUIDAMD ((HGLRC )myGContext);
2747 if (myFuncs->wglGetGPUInfoAMD (anAmdId, WGL_GPU_RAM_AMD, GL_UNSIGNED_INT, sizeof(aTotalMemMiB), &aTotalMemMiB) > 0)
2749 addInfo (theDict, "GPU memory", TCollection_AsciiString() + (int )aTotalMemMiB + " MiB");
2756 #if !defined(GL_ES_VERSION_2_0) && !defined(__APPLE__) && !defined(_WIN32)
2757 // GLX_RENDERER_VENDOR_ID_MESA
2758 if (myFuncs->glXQueryCurrentRendererIntegerMESA != NULL)
2760 unsigned int aVMemMiB = 0;
2761 if (myFuncs->glXQueryCurrentRendererIntegerMESA (GLX_RENDERER_VIDEO_MEMORY_MESA, &aVMemMiB) != False)
2763 addInfo (theDict, "GPU memory", TCollection_AsciiString() + int(aVMemMiB) + " MiB");
2769 // =======================================================================
2770 // function : DiagnosticInfo
2772 // =======================================================================
2773 void OpenGl_Context::DiagnosticInformation (TColStd_IndexedDataMapOfStringString& theDict,
2774 Graphic3d_DiagnosticInfo theFlags) const
2776 if ((theFlags & Graphic3d_DiagnosticInfo_NativePlatform) != 0)
2778 #if defined(HAVE_EGL)
2779 addInfo (theDict, "EGLVersion", ::eglQueryString ((EGLDisplay )myDisplay, EGL_VERSION));
2780 addInfo (theDict, "EGLVendor", ::eglQueryString ((EGLDisplay )myDisplay, EGL_VENDOR));
2781 addInfo (theDict, "EGLClientAPIs", ::eglQueryString ((EGLDisplay )myDisplay, EGL_CLIENT_APIS));
2782 if ((theFlags & Graphic3d_DiagnosticInfo_Extensions) != 0)
2784 addInfo (theDict, "EGLExtensions", ::eglQueryString ((EGLDisplay )myDisplay, EGL_EXTENSIONS));
2786 #elif defined(_WIN32)
2787 if ((theFlags & Graphic3d_DiagnosticInfo_Extensions) != 0
2788 && myFuncs->wglGetExtensionsStringARB != NULL)
2790 const char* aWglExts = myFuncs->wglGetExtensionsStringARB ((HDC )myWindowDC);
2791 addInfo (theDict, "WGLExtensions", aWglExts);
2793 #elif defined(__APPLE__)
2796 Display* aDisplay = (Display*)myDisplay;
2797 const int aScreen = DefaultScreen(aDisplay);
2798 addInfo (theDict, "GLXDirectRendering", ::glXIsDirect (aDisplay, (GLXContext )myGContext) ? "Yes" : "No");
2799 addInfo (theDict, "GLXVendor", ::glXQueryServerString (aDisplay, aScreen, GLX_VENDOR));
2800 addInfo (theDict, "GLXVersion", ::glXQueryServerString (aDisplay, aScreen, GLX_VERSION));
2801 if ((theFlags & Graphic3d_DiagnosticInfo_Extensions) != 0)
2803 const char* aGlxExts = ::glXQueryExtensionsString (aDisplay, aScreen);
2804 addInfo(theDict, "GLXExtensions", aGlxExts);
2807 addInfo (theDict, "GLXClientVendor", ::glXGetClientString (aDisplay, GLX_VENDOR));
2808 addInfo (theDict, "GLXClientVersion", ::glXGetClientString (aDisplay, GLX_VERSION));
2809 if ((theFlags & Graphic3d_DiagnosticInfo_Extensions) != 0)
2811 addInfo (theDict, "GLXClientExtensions", ::glXGetClientString (aDisplay, GLX_EXTENSIONS));
2816 if ((theFlags & Graphic3d_DiagnosticInfo_Device) != 0)
2818 addInfo (theDict, "GLvendor", (const char*)::glGetString (GL_VENDOR));
2819 addInfo (theDict, "GLdevice", (const char*)::glGetString (GL_RENDERER));
2820 addInfo (theDict, "GLversion", (const char*)::glGetString (GL_VERSION));
2821 if (IsGlGreaterEqual (2, 0))
2823 addInfo (theDict, "GLSLversion", (const char*)::glGetString (GL_SHADING_LANGUAGE_VERSION));
2827 addInfo (theDict, "GLdebug", "ON");
2831 if ((theFlags & Graphic3d_DiagnosticInfo_Limits) != 0)
2833 addInfo (theDict, "Max texture size", TCollection_AsciiString(myMaxTexDim));
2834 addInfo (theDict, "Max combined texture units", TCollection_AsciiString(myMaxTexCombined));
2835 addInfo (theDict, "Max MSAA samples", TCollection_AsciiString(myMaxMsaaSamples));
2838 if ((theFlags & Graphic3d_DiagnosticInfo_FrameBuffer) != 0)
2840 GLint aViewport[4] = {};
2841 ::glGetIntegerv (GL_VIEWPORT, aViewport);
2842 addInfo (theDict, "Viewport", TCollection_AsciiString() + aViewport[2] + "x" + aViewport[3]);
2845 if ((theFlags & Graphic3d_DiagnosticInfo_Memory) != 0)
2847 MemoryInfo (theDict);
2850 if ((theFlags & Graphic3d_DiagnosticInfo_Extensions) != 0)
2852 #if !defined(GL_ES_VERSION_2_0)
2853 if (IsGlGreaterEqual (3, 0)
2854 && myFuncs->glGetStringi != NULL)
2856 TCollection_AsciiString anExtList;
2858 ::glGetIntegerv (GL_NUM_EXTENSIONS, &anExtNb);
2859 for (GLint anIter = 0; anIter < anExtNb; ++anIter)
2861 const char* anExtension = (const char*)myFuncs->glGetStringi (GL_EXTENSIONS, (GLuint)anIter);
2862 if (!anExtList.IsEmpty())
2866 anExtList += anExtension;
2868 addInfo(theDict, "GLextensions", anExtList);
2873 addInfo (theDict, "GLextensions", (const char*)::glGetString (GL_EXTENSIONS));
2878 // =======================================================================
2879 // function : GetResource
2881 // =======================================================================
2882 const Handle(OpenGl_Resource)& OpenGl_Context::GetResource (const TCollection_AsciiString& theKey) const
2884 return mySharedResources->IsBound (theKey) ? mySharedResources->Find (theKey) : NULL_GL_RESOURCE;
2887 // =======================================================================
2888 // function : ShareResource
2890 // =======================================================================
2891 Standard_Boolean OpenGl_Context::ShareResource (const TCollection_AsciiString& theKey,
2892 const Handle(OpenGl_Resource)& theResource)
2894 if (theKey.IsEmpty() || theResource.IsNull())
2896 return Standard_False;
2898 return mySharedResources->Bind (theKey, theResource);
2901 // =======================================================================
2902 // function : ReleaseResource
2904 // =======================================================================
2905 void OpenGl_Context::ReleaseResource (const TCollection_AsciiString& theKey,
2906 const Standard_Boolean theToDelay)
2908 if (!mySharedResources->IsBound (theKey))
2912 const Handle(OpenGl_Resource)& aRes = mySharedResources->Find (theKey);
2913 if (aRes->GetRefCount() > 1)
2920 myDelayed->Bind (theKey, 1);
2924 aRes->Release (this);
2925 mySharedResources->UnBind (theKey);
2929 // =======================================================================
2930 // function : ReleaseDelayed
2932 // =======================================================================
2933 void OpenGl_Context::ReleaseDelayed()
2935 // release queued elements
2936 while (!myUnusedResources->IsEmpty())
2938 myUnusedResources->First()->Release (this);
2939 myUnusedResources->RemoveFirst();
2942 // release delayed shared resources
2943 NCollection_Vector<TCollection_AsciiString> aDeadList;
2944 for (NCollection_DataMap<TCollection_AsciiString, Standard_Integer>::Iterator anIter (*myDelayed);
2945 anIter.More(); anIter.Next())
2947 if (++anIter.ChangeValue() <= 2)
2949 continue; // postpone release one more frame to ensure noone use it periodically
2952 const TCollection_AsciiString& aKey = anIter.Key();
2953 if (!mySharedResources->IsBound (aKey))
2955 // mixed unshared strategy delayed/undelayed was used!
2956 aDeadList.Append (aKey);
2960 const Handle(OpenGl_Resource)& aRes = mySharedResources->ChangeFind (aKey);
2961 if (aRes->GetRefCount() > 1)
2963 // should be only 1 instance in mySharedResources
2964 // if not - resource was reused again
2965 aDeadList.Append (aKey);
2969 // release resource if no one requiested it more than 2 redraw calls
2970 aRes->Release (this);
2971 mySharedResources->UnBind (aKey);
2972 aDeadList.Append (aKey);
2975 for (Standard_Integer anIter = 0; anIter < aDeadList.Length(); ++anIter)
2977 myDelayed->UnBind (aDeadList.Value (anIter));
2981 // =======================================================================
2982 // function : BindTextures
2984 // =======================================================================
2985 Handle(OpenGl_TextureSet) OpenGl_Context::BindTextures (const Handle(OpenGl_TextureSet)& theTextures)
2987 if (myActiveTextures == theTextures)
2989 return myActiveTextures;
2992 Handle(OpenGl_Context) aThisCtx (this);
2993 OpenGl_TextureSet::Iterator aTextureIterOld (myActiveTextures), aTextureIterNew (theTextures);
2996 if (!aTextureIterNew.More())
2998 for (; aTextureIterOld.More(); aTextureIterOld.Next())
3000 if (const Handle(OpenGl_Texture)& aTextureOld = aTextureIterOld.Value())
3002 #if !defined(GL_ES_VERSION_2_0)
3005 OpenGl_Sampler::resetGlobalTextureParams (aThisCtx, *aTextureOld, aTextureOld->Sampler()->Parameters());
3008 aTextureOld->Unbind (aThisCtx);
3014 const Handle(OpenGl_Texture)& aTextureNew = aTextureIterNew.Value();
3015 if (aTextureIterOld.More())
3017 const Handle(OpenGl_Texture)& aTextureOld = aTextureIterOld.Value();
3018 if (aTextureNew == aTextureOld)
3020 aTextureIterNew.Next();
3021 aTextureIterOld.Next();
3024 else if (aTextureNew.IsNull()
3025 || !aTextureNew->IsValid())
3027 if (!aTextureOld.IsNull())
3029 #if !defined(GL_ES_VERSION_2_0)
3032 OpenGl_Sampler::resetGlobalTextureParams (aThisCtx, *aTextureOld, aTextureOld->Sampler()->Parameters());
3035 aTextureOld->Unbind (aThisCtx);
3038 aTextureIterNew.Next();
3039 aTextureIterOld.Next();
3043 aTextureIterOld.Next();
3045 if (aTextureNew.IsNull())
3047 aTextureIterNew.Next();
3051 const Graphic3d_TextureUnit aTexUnit = aTextureNew->Sampler()->Parameters()->TextureUnit();
3052 if (aTexUnit >= myMaxTexCombined)
3054 PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH,
3055 TCollection_AsciiString("Texture unit ") + aTexUnit + " for " + aTextureNew->ResourceId() + " exceeds hardware limit " + myMaxTexCombined);
3056 aTextureIterNew.Next();
3060 aTextureNew->Bind (aThisCtx);
3061 if (aTextureNew->Sampler()->ToUpdateParameters())
3063 if (aTextureNew->Sampler()->IsImmutable())
3065 aTextureNew->Sampler()->Init (aThisCtx, *aTextureNew);
3069 OpenGl_Sampler::applySamplerParams (aThisCtx, aTextureNew->Sampler()->Parameters(), aTextureNew->Sampler().get(), aTextureNew->GetTarget(), aTextureNew->HasMipmaps());
3072 #if !defined(GL_ES_VERSION_2_0)
3075 OpenGl_Sampler::applyGlobalTextureParams (aThisCtx, *aTextureNew, aTextureNew->Sampler()->Parameters());
3078 aTextureIterNew.Next();
3081 Handle(OpenGl_TextureSet) anOldTextures = myActiveTextures;
3082 myActiveTextures = theTextures;
3083 return anOldTextures;
3086 // =======================================================================
3087 // function : BindProgram
3089 // =======================================================================
3090 Standard_Boolean OpenGl_Context::BindProgram (const Handle(OpenGl_ShaderProgram)& theProgram)
3092 if (core20fwd == NULL)
3094 return Standard_False;
3096 else if (myActiveProgram == theProgram)
3098 return Standard_True;
3101 if (theProgram.IsNull()
3102 || !theProgram->IsValid())
3104 if (!myActiveProgram.IsNull())
3106 core20fwd->glUseProgram (OpenGl_ShaderProgram::NO_PROGRAM);
3107 myActiveProgram.Nullify();
3109 return Standard_False;
3112 myActiveProgram = theProgram;
3113 core20fwd->glUseProgram (theProgram->ProgramId());
3114 return Standard_True;
3117 // =======================================================================
3118 // function : BindDefaultVao
3120 // =======================================================================
3121 void OpenGl_Context::BindDefaultVao()
3123 #if !defined(GL_ES_VERSION_2_0)
3124 if (myDefaultVao == 0
3130 core32->glBindVertexArray (myDefaultVao);
3134 // =======================================================================
3135 // function : SetDefaultFrameBuffer
3137 // =======================================================================
3138 Handle(OpenGl_FrameBuffer) OpenGl_Context::SetDefaultFrameBuffer (const Handle(OpenGl_FrameBuffer)& theFbo)
3140 Handle(OpenGl_FrameBuffer) aFbo = myDefaultFbo;
3141 myDefaultFbo = theFbo;
3145 // =======================================================================
3146 // function : SetShadingMaterial
3148 // =======================================================================
3149 void OpenGl_Context::SetShadingMaterial (const OpenGl_AspectFace* theAspect,
3150 const Handle(Graphic3d_PresentationAttributes)& theHighlight)
3152 const Handle(Graphic3d_AspectFillArea3d)& anAspect = (!theHighlight.IsNull() && !theHighlight->BasicFillAreaAspect().IsNull())
3153 ? theHighlight->BasicFillAreaAspect()
3154 : theAspect->Aspect();
3156 const bool toDistinguish = anAspect->Distinguish();
3157 const bool toMapTexture = anAspect->ToMapTexture();
3158 const Graphic3d_MaterialAspect& aMatFrontSrc = anAspect->FrontMaterial();
3159 const Graphic3d_MaterialAspect& aMatBackSrc = toDistinguish
3160 ? anAspect->BackMaterial()
3162 const Quantity_Color& aFrontIntColor = anAspect->InteriorColor();
3163 const Quantity_Color& aBackIntColor = toDistinguish
3164 ? anAspect->BackInteriorColor()
3167 myMatFront.Init (aMatFrontSrc, aFrontIntColor);
3170 myMatBack.Init (aMatBackSrc, aBackIntColor);
3174 myMatBack = myMatFront;
3177 if (!theHighlight.IsNull()
3178 && theHighlight->BasicFillAreaAspect().IsNull())
3180 myMatFront.SetColor (theHighlight->ColorRGBA());
3181 myMatBack .SetColor (theHighlight->ColorRGBA());
3184 Standard_ShortReal anAlphaFront = 1.0f;
3185 Standard_ShortReal anAlphaBack = 1.0f;
3186 if (CheckIsTransparent (theAspect, theHighlight, anAlphaFront, anAlphaBack))
3188 myMatFront.Diffuse.a() = anAlphaFront;
3189 myMatBack .Diffuse.a() = anAlphaBack;
3192 // do not update material properties in case of zero reflection mode,
3193 // because GL lighting will be disabled by OpenGl_PrimitiveArray::DrawArray() anyway.
3194 if (theAspect->IsNoLighting())
3199 if (myMatFront == myShaderManager->MaterialState().FrontMaterial()
3200 && myMatBack == myShaderManager->MaterialState().BackMaterial()
3201 && toDistinguish == myShaderManager->MaterialState().ToDistinguish()
3202 && toMapTexture == myShaderManager->MaterialState().ToMapTexture())
3207 myShaderManager->UpdateMaterialStateTo (myMatFront, myMatBack, toDistinguish, toMapTexture);
3210 // =======================================================================
3211 // function : CheckIsTransparent
3213 // =======================================================================
3214 Standard_Boolean OpenGl_Context::CheckIsTransparent (const OpenGl_AspectFace* theAspect,
3215 const Handle(Graphic3d_PresentationAttributes)& theHighlight,
3216 Standard_ShortReal& theAlphaFront,
3217 Standard_ShortReal& theAlphaBack)
3219 const Handle(Graphic3d_AspectFillArea3d)& anAspect = (!theHighlight.IsNull() && !theHighlight->BasicFillAreaAspect().IsNull())
3220 ? theHighlight->BasicFillAreaAspect()
3221 : theAspect->Aspect();
3223 const bool toDistinguish = anAspect->Distinguish();
3224 const Graphic3d_MaterialAspect& aMatFrontSrc = anAspect->FrontMaterial();
3225 const Graphic3d_MaterialAspect& aMatBackSrc = toDistinguish
3226 ? anAspect->BackMaterial()
3229 // handling transparency
3230 if (!theHighlight.IsNull()
3231 && theHighlight->BasicFillAreaAspect().IsNull())
3233 theAlphaFront = theHighlight->ColorRGBA().Alpha();
3234 theAlphaBack = theHighlight->ColorRGBA().Alpha();
3238 theAlphaFront = aMatFrontSrc.Alpha();
3239 theAlphaBack = aMatBackSrc .Alpha();
3242 const bool isTransparent = theAlphaFront < 1.0f
3243 || theAlphaBack < 1.0f;
3244 return isTransparent;
3247 // =======================================================================
3248 // function : SetColor4fv
3250 // =======================================================================
3251 void OpenGl_Context::SetColor4fv (const OpenGl_Vec4& theColor)
3253 if (!myActiveProgram.IsNull())
3255 myActiveProgram->SetUniform (this, myActiveProgram->GetStateLocation (OpenGl_OCCT_COLOR), theColor);
3257 #if !defined(GL_ES_VERSION_2_0)
3258 else if (core11 != NULL)
3260 core11->glColor4fv (theColor.GetData());
3265 // =======================================================================
3266 // function : SetTypeOfLine
3268 // =======================================================================
3269 void OpenGl_Context::SetTypeOfLine (const Aspect_TypeOfLine theType,
3270 const Standard_ShortReal theFactor)
3272 Standard_Integer aPattern = 0xFFFF;
3275 case Aspect_TOL_DASH:
3280 case Aspect_TOL_DOT:
3285 case Aspect_TOL_DOTDASH:
3290 case Aspect_TOL_EMPTY:
3291 case Aspect_TOL_SOLID:
3296 case Aspect_TOL_USERDEFINED:
3303 if (!myActiveProgram.IsNull())
3305 myActiveProgram->SetUniform (this, "uPattern", aPattern);
3306 myActiveProgram->SetUniform (this, "uFactor", theFactor);
3310 #if !defined(GL_ES_VERSION_2_0)
3311 if (aPattern != 0xFFFF)
3316 gl2psEnable (GL2PS_LINE_STIPPLE);
3322 core11fwd->glEnable (GL_LINE_STIPPLE);
3324 core11->glLineStipple (static_cast<GLint> (theFactor),
3325 static_cast<GLushort> (aPattern));
3332 core11fwd->glDisable (GL_LINE_STIPPLE);
3338 gl2psDisable (GL2PS_LINE_STIPPLE);
3345 // =======================================================================
3346 // function : SetLineWidth
3348 // =======================================================================
3349 void OpenGl_Context::SetLineWidth (const Standard_ShortReal theWidth)
3353 // glLineWidth() is still defined within Core Profile, but has no effect with values != 1.0f
3354 core11fwd->glLineWidth (theWidth * myLineWidthScale);
3359 gl2psLineWidth (theWidth);
3364 // =======================================================================
3365 // function : SetTextureMatrix
3367 // =======================================================================
3368 void OpenGl_Context::SetTextureMatrix (const Handle(Graphic3d_TextureParams)& theParams)
3370 if (theParams.IsNull())
3374 else if (!myActiveProgram.IsNull())
3376 const GLint aUniLoc = myActiveProgram->GetStateLocation (OpenGl_OCCT_TEXTURE_TRSF2D);
3377 if (aUniLoc == OpenGl_ShaderProgram::INVALID_LOCATION)
3382 // pack transformation parameters
3383 OpenGl_Vec4 aTrsf[2];
3384 aTrsf[0].x() = -theParams->Translation().x();
3385 aTrsf[0].y() = -theParams->Translation().y();
3386 aTrsf[0].zw() = theParams->Scale();
3387 aTrsf[1].x() = std::sin (-theParams->Rotation() * static_cast<float> (M_PI / 180.0));
3388 aTrsf[1].y() = std::cos (-theParams->Rotation() * static_cast<float> (M_PI / 180.0));
3389 myActiveProgram->SetUniform (this, aUniLoc, 2, aTrsf);
3393 #if !defined(GL_ES_VERSION_2_0)
3396 GLint aMatrixMode = GL_TEXTURE;
3397 ::glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode);
3399 core11->glMatrixMode (GL_TEXTURE);
3400 OpenGl_Mat4 aTextureMat;
3401 const Graphic3d_Vec2& aScale = theParams->Scale();
3402 const Graphic3d_Vec2& aTrans = theParams->Translation();
3403 Graphic3d_TransformUtils::Scale (aTextureMat, aScale.x(), aScale.y(), 1.0f);
3404 Graphic3d_TransformUtils::Translate (aTextureMat, -aTrans.x(), -aTrans.y(), 0.0f);
3405 Graphic3d_TransformUtils::Rotate (aTextureMat, -theParams->Rotation(), 0.0f, 0.0f, 1.0f);
3406 core11->glLoadMatrixf (aTextureMat);
3407 core11->glMatrixMode (aMatrixMode);
3412 // =======================================================================
3413 // function : SetPointSize
3415 // =======================================================================
3416 void OpenGl_Context::SetPointSize (const Standard_ShortReal theSize)
3418 if (!myActiveProgram.IsNull())
3420 myActiveProgram->SetUniform (this, myActiveProgram->GetStateLocation (OpenGl_OCCT_POINT_SIZE), theSize);
3421 #if !defined(GL_ES_VERSION_2_0)
3422 //myContext->core11fwd->glEnable (GL_VERTEX_PROGRAM_POINT_SIZE);
3425 #if !defined(GL_ES_VERSION_2_0)
3428 core11fwd->glPointSize (theSize);
3429 if (core20fwd != NULL)
3431 //myContext->core11fwd->glDisable (GL_VERTEX_PROGRAM_POINT_SIZE);
3437 // =======================================================================
3438 // function : SetPointSpriteOrigin
3440 // =======================================================================
3441 void OpenGl_Context::SetPointSpriteOrigin()
3443 #if !defined(GL_ES_VERSION_2_0)
3444 if (core15fwd == NULL)
3449 const int aNewState = !myActiveProgram.IsNull() ? GL_UPPER_LEFT : GL_LOWER_LEFT;
3450 if (myPointSpriteOrig != aNewState)
3452 myPointSpriteOrig = aNewState;
3453 core15fwd->glPointParameteri (GL_POINT_SPRITE_COORD_ORIGIN, aNewState);
3458 // =======================================================================
3459 // function : SetGlNormalizeEnabled
3461 // =======================================================================
3462 Standard_Boolean OpenGl_Context::SetGlNormalizeEnabled (Standard_Boolean isEnabled)
3464 if (isEnabled == myIsGlNormalizeEnabled)
3466 return myIsGlNormalizeEnabled;
3469 Standard_Boolean anOldGlNormalize = myIsGlNormalizeEnabled;
3471 myIsGlNormalizeEnabled = isEnabled;
3473 #if !defined(GL_ES_VERSION_2_0)
3478 ::glEnable (GL_NORMALIZE);
3482 ::glDisable (GL_NORMALIZE);
3487 return anOldGlNormalize;
3490 // =======================================================================
3491 // function : SetPolygonMode
3493 // =======================================================================
3494 Standard_Integer OpenGl_Context::SetPolygonMode (const Standard_Integer theMode)
3496 if (myPolygonMode == theMode)
3498 return myPolygonMode;
3501 const Standard_Integer anOldPolygonMode = myPolygonMode;
3503 myPolygonMode = theMode;
3505 #if !defined(GL_ES_VERSION_2_0)
3506 ::glPolygonMode (GL_FRONT_AND_BACK, (GLenum)theMode);
3509 return anOldPolygonMode;
3512 // =======================================================================
3513 // function : SetPolygonHatchEnabled
3515 // =======================================================================
3516 bool OpenGl_Context::SetPolygonHatchEnabled (const bool theIsEnabled)
3518 if (myHatchStyles.IsNull())
3522 else if (myHatchStyles->IsEnabled() == theIsEnabled)
3524 return theIsEnabled;
3527 return myHatchStyles->SetEnabled (this, theIsEnabled);
3530 // =======================================================================
3531 // function : SetPolygonHatchStyle
3533 // =======================================================================
3534 Standard_Integer OpenGl_Context::SetPolygonHatchStyle (const Handle(Graphic3d_HatchStyle)& theStyle)
3536 if (theStyle.IsNull())
3541 if (myHatchStyles.IsNull())
3543 if (!GetResource ("OpenGl_LineAttributes", myHatchStyles))
3545 // share and register for release once the resource is no longer used
3546 myHatchStyles = new OpenGl_LineAttributes();
3547 ShareResource ("OpenGl_LineAttributes", myHatchStyles);
3550 if (myHatchStyles->TypeOfHatch() == theStyle->HatchType())
3552 return theStyle->HatchType();
3555 return myHatchStyles->SetTypeOfHatch (this, theStyle);
3558 // =======================================================================
3559 // function : ApplyModelWorldMatrix
3561 // =======================================================================
3562 void OpenGl_Context::ApplyModelWorldMatrix()
3564 if (myShaderManager->ModelWorldState().ModelWorldMatrix() != ModelWorldState.Current())
3566 myShaderManager->UpdateModelWorldStateTo (ModelWorldState.Current());
3570 // =======================================================================
3571 // function : ApplyWorldViewMatrix
3573 // =======================================================================
3574 void OpenGl_Context::ApplyWorldViewMatrix()
3576 if (myShaderManager->ModelWorldState().ModelWorldMatrix() != THE_IDENTITY_MATRIX)
3578 myShaderManager->UpdateModelWorldStateTo (THE_IDENTITY_MATRIX);
3580 if (myShaderManager->WorldViewState().WorldViewMatrix() != WorldViewState.Current())
3582 myShaderManager->UpdateWorldViewStateTo (WorldViewState.Current());
3586 // =======================================================================
3587 // function : ApplyModelViewMatrix
3589 // =======================================================================
3590 void OpenGl_Context::ApplyModelViewMatrix()
3592 if (myShaderManager->ModelWorldState().ModelWorldMatrix() != ModelWorldState.Current())
3594 myShaderManager->UpdateModelWorldStateTo (ModelWorldState.Current());
3596 if (myShaderManager->WorldViewState().WorldViewMatrix() != WorldViewState.Current())
3598 myShaderManager->UpdateWorldViewStateTo (WorldViewState.Current());
3602 // =======================================================================
3603 // function : ApplyProjectionMatrix
3605 // =======================================================================
3606 void OpenGl_Context::ApplyProjectionMatrix()
3608 if (myShaderManager->ProjectionState().ProjectionMatrix() != ProjectionState.Current())
3610 myShaderManager->UpdateProjectionStateTo (ProjectionState.Current());
3614 // =======================================================================
3615 // function : EnableFeatures
3617 // =======================================================================
3618 void OpenGl_Context::EnableFeatures() const
3623 // =======================================================================
3624 // function : DisableFeatures
3626 // =======================================================================
3627 void OpenGl_Context::DisableFeatures() const
3629 // Disable stuff that's likely to slow down glDrawPixels.
3630 glDisable(GL_DITHER);
3631 glDisable(GL_BLEND);
3632 glDisable(GL_DEPTH_TEST);
3633 glDisable(GL_STENCIL_TEST);
3635 #if !defined(GL_ES_VERSION_2_0)
3641 glDisable(GL_TEXTURE_1D);
3642 glDisable(GL_TEXTURE_2D);
3644 glDisable(GL_LIGHTING);
3645 glDisable(GL_ALPHA_TEST);
3647 glDisable(GL_LOGIC_OP);
3649 glPixelTransferi(GL_MAP_COLOR, GL_FALSE);
3650 glPixelTransferi(GL_RED_SCALE, 1);
3651 glPixelTransferi(GL_RED_BIAS, 0);
3652 glPixelTransferi(GL_GREEN_SCALE, 1);
3653 glPixelTransferi(GL_GREEN_BIAS, 0);
3654 glPixelTransferi(GL_BLUE_SCALE, 1);
3655 glPixelTransferi(GL_BLUE_BIAS, 0);
3656 glPixelTransferi(GL_ALPHA_SCALE, 1);
3657 glPixelTransferi(GL_ALPHA_BIAS, 0);
3659 if ((myGlVerMajor >= 1) && (myGlVerMinor >= 2))
3661 if (CheckExtension ("GL_CONVOLUTION_1D_EXT"))
3662 glDisable(GL_CONVOLUTION_1D_EXT);
3664 if (CheckExtension ("GL_CONVOLUTION_2D_EXT"))
3665 glDisable(GL_CONVOLUTION_2D_EXT);
3667 if (CheckExtension ("GL_SEPARABLE_2D_EXT"))
3668 glDisable(GL_SEPARABLE_2D_EXT);
3670 if (CheckExtension ("GL_SEPARABLE_2D_EXT"))
3671 glDisable(GL_HISTOGRAM_EXT);
3673 if (CheckExtension ("GL_MINMAX_EXT"))
3674 glDisable(GL_MINMAX_EXT);
3676 if (CheckExtension ("GL_TEXTURE_3D_EXT"))
3677 glDisable(GL_TEXTURE_3D_EXT);