1 // Created on: 2014-01-26
2 // Created by: Kirill GAVRILOV
3 // Copyright (c) 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
8 // under the terms of the GNU Lesser General Public 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_ArbVBO.hxx>
23 #include <OpenGl_ArbTBO.hxx>
24 #include <OpenGl_ArbIns.hxx>
25 #include <OpenGl_ArbDbg.hxx>
26 #include <OpenGl_ExtFBO.hxx>
27 #include <OpenGl_ExtGS.hxx>
28 #include <OpenGl_GlCore20.hxx>
29 #include <OpenGl_ShaderManager.hxx>
31 #include <Message_Messenger.hxx>
33 #include <NCollection_Vector.hxx>
35 #include <Standard_ProgramError.hxx>
39 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
42 #include <GL/glx.h> // glXGetProcAddress()
45 // GL_NVX_gpu_memory_info
46 #ifndef GL_GPU_MEMORY_INFO_DEDICATED_VIDMEM_NVX
49 GL_GPU_MEMORY_INFO_DEDICATED_VIDMEM_NVX = 0x9047,
50 GL_GPU_MEMORY_INFO_TOTAL_AVAILABLE_MEMORY_NVX = 0x9048,
51 GL_GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX = 0x9049,
52 GL_GPU_MEMORY_INFO_EVICTION_COUNT_NVX = 0x904A,
53 GL_GPU_MEMORY_INFO_EVICTED_MEMORY_NVX = 0x904B
57 IMPLEMENT_STANDARD_HANDLE (OpenGl_Context, Standard_Transient)
58 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Context, Standard_Transient)
60 //! Make record shorter to retrieve function pointer using variable with same name
61 #define FindProcShort(theStruct, theFunc) FindProc(#theFunc, theStruct->theFunc)
65 static const Handle(OpenGl_Resource) NULL_GL_RESOURCE;
66 static const GLdouble OpenGl_DefaultPlaneEq[] = {0.0, 0.0, 0.0, 0.0};
69 // =======================================================================
70 // function : OpenGl_Context
72 // =======================================================================
73 OpenGl_Context::OpenGl_Context (const Handle(OpenGl_Caps)& theCaps)
79 caps (!theCaps.IsNull() ? theCaps : new OpenGl_Caps()),
80 arbNPTW(Standard_False),
87 extBgra(Standard_False),
88 extAnis(Standard_False),
89 extPDS (Standard_False),
90 atiMem (Standard_False),
91 nvxMem (Standard_False),
92 mySharedResources (new OpenGl_ResourcesMap()),
93 myDelayed (new OpenGl_DelayReleaseMap()),
94 myReleaseQueue (new OpenGl_ResourcesQueue()),
103 myIsFeedback (Standard_False),
104 myIsInitialized (Standard_False)
106 #if defined(MAC_OS_X_VERSION_10_3) && !defined(MACOSX_USE_GLX)
107 // Vendors can not extend functionality on this system
108 // and developers are limited to OpenGL support provided by Mac OS X SDK.
109 // We retrieve function pointers from system library
110 // to generalize extensions support on all platforms.
111 // In this way we also reach binary compatibility benefit between OS releases
112 // if some newest functionality is optionally used.
113 // Notice that GL version / extension availability checks are required
114 // because function pointers may be available but not functionality itself
115 // (depends on renderer).
116 myGlLibHandle = dlopen ("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL", RTLD_LAZY);
119 myShaderManager = new OpenGl_ShaderManager (this);
122 // =======================================================================
123 // function : ~OpenGl_Context
125 // =======================================================================
126 OpenGl_Context::~OpenGl_Context()
128 // release clean up queue
131 // release shared resources if any
132 if (((const Handle(Standard_Transient)& )mySharedResources)->GetRefCount() <= 1)
134 myShaderManager.Nullify();
135 for (NCollection_DataMap<TCollection_AsciiString, Handle(OpenGl_Resource)>::Iterator anIter (*mySharedResources);
136 anIter.More(); anIter.Next())
138 anIter.Value()->Release (this);
143 myShaderManager->SetContext (NULL);
145 mySharedResources.Nullify();
149 && caps->contextDebug)
153 glGetPointerv (GL_DEBUG_CALLBACK_USER_PARAM_ARB, &aPtr);
156 arbDbg->glDebugMessageCallbackARB (NULL, NULL);
169 // =======================================================================
170 // function : MaxDegreeOfAnisotropy
172 // =======================================================================
173 Standard_Integer OpenGl_Context::MaxDegreeOfAnisotropy() const
178 // =======================================================================
179 // function : MaxTextureSize
181 // =======================================================================
182 Standard_Integer OpenGl_Context::MaxTextureSize() const
187 // =======================================================================
188 // function : MaxClipPlanes
190 // =======================================================================
191 Standard_Integer OpenGl_Context::MaxClipPlanes() const
193 return myMaxClipPlanes;
196 // =======================================================================
199 // =======================================================================
200 void OpenGl_Context::Share (const Handle(OpenGl_Context)& theShareCtx)
202 if (!theShareCtx.IsNull())
204 mySharedResources = theShareCtx->mySharedResources;
205 myDelayed = theShareCtx->myDelayed;
206 myReleaseQueue = theShareCtx->myReleaseQueue;
207 myShaderManager = theShareCtx->myShaderManager;
211 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
213 // =======================================================================
214 // function : IsCurrent
216 // =======================================================================
217 Standard_Boolean OpenGl_Context::IsCurrent() const
220 if (myWindowDC == NULL || myGContext == NULL)
222 return Standard_False;
224 return (( (HDC )myWindowDC == wglGetCurrentDC())
225 && ((HGLRC )myGContext == wglGetCurrentContext()));
227 if (myDisplay == NULL || myWindow == 0 || myGContext == 0)
229 return Standard_False;
232 return ( ((Display* )myDisplay == glXGetCurrentDisplay())
233 && ((GLXContext )myGContext == glXGetCurrentContext())
234 && ((GLXDrawable )myWindow == glXGetCurrentDrawable()));
238 // =======================================================================
239 // function : MakeCurrent
241 // =======================================================================
242 Standard_Boolean OpenGl_Context::MakeCurrent()
245 if (myWindowDC == NULL || myGContext == NULL)
247 Standard_ProgramError_Raise_if (myIsInitialized, "OpenGl_Context::Init() should be called before!");
248 return Standard_False;
251 // technically it should be safe to activate already bound GL context
252 // however some drivers (Intel etc.) may FAIL doing this for unknown reason
255 myShaderManager->SetContext (this);
256 return Standard_True;
258 else if (wglMakeCurrent ((HDC )myWindowDC, (HGLRC )myGContext) != TRUE)
260 // notice that glGetError() couldn't be used here!
261 wchar_t* aMsgBuff = NULL;
262 DWORD anErrorCode = GetLastError();
263 FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
264 NULL, anErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (wchar_t* )&aMsgBuff, 0, NULL);
265 TCollection_ExtendedString aMsg ("wglMakeCurrent() has failed. ");
266 if (aMsgBuff != NULL)
268 aMsg += (Standard_ExtString )aMsgBuff;
269 LocalFree (aMsgBuff);
271 PushMessage (GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB, GL_DEBUG_TYPE_ERROR_ARB, (unsigned int )anErrorCode, GL_DEBUG_SEVERITY_HIGH_ARB, aMsg);
272 myIsInitialized = Standard_False;
273 return Standard_False;
276 if (myDisplay == NULL || myWindow == 0 || myGContext == 0)
278 Standard_ProgramError_Raise_if (myIsInitialized, "OpenGl_Context::Init() should be called before!");
279 return Standard_False;
282 if (!glXMakeCurrent ((Display* )myDisplay, (GLXDrawable )myWindow, (GLXContext )myGContext))
284 // if there is no current context it might be impossible to use glGetError() correctly
285 PushMessage (GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB, GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB,
286 "glXMakeCurrent() has failed!");
287 myIsInitialized = Standard_False;
288 return Standard_False;
291 myShaderManager->SetContext (this);
292 return Standard_True;
295 // =======================================================================
296 // function : SwapBuffers
298 // =======================================================================
299 void OpenGl_Context::SwapBuffers()
302 if ((HDC )myWindowDC != NULL)
304 ::SwapBuffers ((HDC )myWindowDC);
308 if ((Display* )myDisplay != NULL)
310 glXSwapBuffers ((Display* )myDisplay, (GLXDrawable )myWindow);
317 // =======================================================================
318 // function : findProc
320 // =======================================================================
321 void* OpenGl_Context::findProc (const char* theFuncName)
324 return wglGetProcAddress (theFuncName);
325 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
326 return (myGlLibHandle != NULL) ? dlsym (myGlLibHandle, theFuncName) : NULL;
328 return (void* )glXGetProcAddress ((const GLubyte* )theFuncName);
332 // =======================================================================
333 // function : CheckExtension
335 // =======================================================================
336 Standard_Boolean OpenGl_Context::CheckExtension (const char* theExtName) const
338 if (theExtName == NULL)
340 std::cerr << "CheckExtension called with NULL string!\n";
341 return Standard_False;
344 // available since OpenGL 3.0
345 // and the ONLY way to check extensions with OpenGL 3.1+ core profile
346 /**if (IsGlGreaterEqual (3, 0))
349 glGetIntegerv (GL_NUM_EXTENSIONS, &anExtNb);
350 for (GLint anIter = 0; anIter < anExtNb; ++anIter)
352 const char* anExtension = (const char* )core30->glGetStringi (GL_EXTENSIONS, (GLuint )anIter);
353 if (anExtension[anExtNameLen] == '\0' &&
354 strncmp (anExtension, theExtName, anExtNameLen) == 0)
356 return Standard_True;
359 return Standard_False;
362 // use old way with huge string for all extensions
363 const char* anExtString = (const char* )glGetString (GL_EXTENSIONS);
364 if (anExtString == NULL)
366 Messanger()->Send ("TKOpenGL: glGetString (GL_EXTENSIONS) has returned NULL! No GL context?", Message_Warning);
367 return Standard_False;
369 return CheckExtension (anExtString, theExtName);
372 // =======================================================================
373 // function : CheckExtension
375 // =======================================================================
376 Standard_Boolean OpenGl_Context::CheckExtension (const char* theExtString,
377 const char* theExtName)
379 if (theExtString == NULL)
381 return Standard_False;
384 // Search for theExtName in the extensions string.
385 // Use of strstr() is not sufficient because extension names can be prefixes of other extension names.
386 char* aPtrIter = (char* )theExtString;
387 const char* aPtrEnd = aPtrIter + strlen (theExtString);
388 const size_t anExtNameLen = strlen (theExtName);
389 while (aPtrIter < aPtrEnd)
391 const size_t n = strcspn (aPtrIter, " ");
392 if ((n == anExtNameLen) && (strncmp (aPtrIter, theExtName, anExtNameLen) == 0))
394 return Standard_True;
398 return Standard_False;
401 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
403 // =======================================================================
406 // =======================================================================
407 Standard_Boolean OpenGl_Context::Init()
411 return Standard_True;
415 myWindowDC = (Aspect_Handle )wglGetCurrentDC();
416 myGContext = (Aspect_RenderingContext )wglGetCurrentContext();
418 myDisplay = (Aspect_Display )glXGetCurrentDisplay();
419 myGContext = (Aspect_RenderingContext )glXGetCurrentContext();
420 myWindow = (Aspect_Drawable )glXGetCurrentDrawable();
422 if (myGContext == NULL)
424 return Standard_False;
428 myIsInitialized = Standard_True;
429 return Standard_True;
434 // =======================================================================
437 // =======================================================================
439 Standard_Boolean OpenGl_Context::Init (const Aspect_Handle theWindow,
440 const Aspect_Handle theWindowDC,
441 const Aspect_RenderingContext theGContext)
442 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
443 Standard_Boolean OpenGl_Context::Init (const void* theGContext)
445 Standard_Boolean OpenGl_Context::Init (const Aspect_Drawable theWindow,
446 const Aspect_Display theDisplay,
447 const Aspect_RenderingContext theGContext)
450 Standard_ProgramError_Raise_if (myIsInitialized, "OpenGl_Context::Init() should be called only once!");
452 myWindow = theWindow;
453 myGContext = theGContext;
454 myWindowDC = theWindowDC;
455 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
456 myGContext = (void* )theGContext;
458 myWindow = theWindow;
459 myGContext = theGContext;
460 myDisplay = theDisplay;
462 if (myGContext == NULL || !MakeCurrent())
464 return Standard_False;
468 myIsInitialized = Standard_True;
469 return Standard_True;
472 // =======================================================================
473 // function : ResetErrors
475 // =======================================================================
476 void OpenGl_Context::ResetErrors()
478 while (glGetError() != GL_NO_ERROR)
484 // =======================================================================
485 // function : readGlVersion
487 // =======================================================================
488 void OpenGl_Context::readGlVersion()
494 // available since OpenGL 3.0
495 GLint aMajor = 0, aMinor = 0;
496 glGetIntegerv (GL_MAJOR_VERSION, &aMajor);
497 glGetIntegerv (GL_MINOR_VERSION, &aMinor);
498 // glGetError() sometimes does not report an error here even if
499 // GL does not know GL_MAJOR_VERSION and GL_MINOR_VERSION constants.
500 // This happens on some rendereres like e.g. Cygwin MESA.
501 // Thus checking additionally if GL has put anything to
502 // the output variables.
503 if (glGetError() == GL_NO_ERROR && aMajor != 0 && aMinor != 0)
505 myGlVerMajor = aMajor;
506 myGlVerMinor = aMinor;
511 // Read version string.
512 // Notice that only first two numbers splitted by point '2.1 XXXXX' are significant.
513 // Following trash (after space) is vendor-specific.
514 // New drivers also returns micro version of GL like '3.3.0' which has no meaning
515 // and should be considered as vendor-specific too.
516 const char* aVerStr = (const char* )glGetString (GL_VERSION);
517 if (aVerStr == NULL || *aVerStr == '\0')
519 // invalid GL context
523 // parse string for major number
527 while (aVerStr[aMajIter] >= '0' && aVerStr[aMajIter] <= '9')
531 if (aMajIter == 0 || aMajIter >= sizeof(aMajorStr))
535 memcpy (aMajorStr, aVerStr, aMajIter);
536 aMajorStr[aMajIter] = '\0';
538 // parse string for minor number
539 aVerStr += aMajIter + 1;
541 while (aVerStr[aMinIter] >= '0' && aVerStr[aMinIter] <= '9')
545 if (aMinIter == 0 || aMinIter >= sizeof(aMinorStr))
549 memcpy (aMinorStr, aVerStr, aMinIter);
550 aMinorStr[aMinIter] = '\0';
553 myGlVerMajor = atoi (aMajorStr);
554 myGlVerMinor = atoi (aMinorStr);
556 if (myGlVerMajor <= 0)
563 static Standard_CString THE_DBGMSG_UNKNOWN = "UNKNOWN";
564 static Standard_CString THE_DBGMSG_SOURCES[] =
566 ".OpenGL", // GL_DEBUG_SOURCE_API_ARB
567 ".WinSystem", // GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB
568 ".GLSL", // GL_DEBUG_SOURCE_SHADER_COMPILER_ARB
569 ".3rdParty", // GL_DEBUG_SOURCE_THIRD_PARTY_ARB
570 "", // GL_DEBUG_SOURCE_APPLICATION_ARB
571 ".Other" // GL_DEBUG_SOURCE_OTHER_ARB
574 static Standard_CString THE_DBGMSG_TYPES[] =
576 "Error", // GL_DEBUG_TYPE_ERROR_ARB
577 "Deprecated", // GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
578 "Undef. behavior", // GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
579 "Portability", // GL_DEBUG_TYPE_PORTABILITY_ARB
580 "Performance", // GL_DEBUG_TYPE_PERFORMANCE_ARB
581 "Other" // GL_DEBUG_TYPE_OTHER_ARB
584 static Standard_CString THE_DBGMSG_SEV_HIGH = "High"; // GL_DEBUG_SEVERITY_HIGH_ARB
585 static Standard_CString THE_DBGMSG_SEV_MEDIUM = "Medium"; // GL_DEBUG_SEVERITY_MEDIUM_ARB
586 static Standard_CString THE_DBGMSG_SEV_LOW = "Low"; // GL_DEBUG_SEVERITY_LOW_ARB
588 //! Callback for GL_ARB_debug_output extension
589 static void APIENTRY debugCallbackWrap(unsigned int theSource,
590 unsigned int theType,
592 unsigned int theSeverity,
594 const char* theMessage,
595 const void* theUserParam)
597 OpenGl_Context* aCtx = (OpenGl_Context* )theUserParam;
598 aCtx->PushMessage (theSource, theType, theId, theSeverity, theMessage);
601 // =======================================================================
602 // function : PushMessage
604 // =======================================================================
605 void OpenGl_Context::PushMessage (const unsigned int theSource,
606 const unsigned int theType,
607 const unsigned int theId,
608 const unsigned int theSeverity,
609 const TCollection_ExtendedString& theMessage)
611 //OpenGl_Context* aCtx = (OpenGl_Context* )theUserParam;
612 Standard_CString& aSrc = (theSource >= GL_DEBUG_SOURCE_API_ARB
613 && theSource <= GL_DEBUG_SOURCE_OTHER_ARB)
614 ? THE_DBGMSG_SOURCES[theSource - GL_DEBUG_SOURCE_API_ARB]
615 : THE_DBGMSG_UNKNOWN;
616 Standard_CString& aType = (theType >= GL_DEBUG_TYPE_ERROR_ARB
617 && theType <= GL_DEBUG_TYPE_OTHER_ARB)
618 ? THE_DBGMSG_TYPES[theType - GL_DEBUG_TYPE_ERROR_ARB]
619 : THE_DBGMSG_UNKNOWN;
620 Standard_CString& aSev = theSeverity == GL_DEBUG_SEVERITY_HIGH_ARB
621 ? THE_DBGMSG_SEV_HIGH
622 : (theSeverity == GL_DEBUG_SEVERITY_MEDIUM_ARB
623 ? THE_DBGMSG_SEV_MEDIUM
624 : THE_DBGMSG_SEV_LOW);
625 Message_Gravity aGrav = theSeverity == GL_DEBUG_SEVERITY_HIGH_ARB
627 : (theSeverity == GL_DEBUG_SEVERITY_MEDIUM_ARB
631 TCollection_ExtendedString aMsg;
632 aMsg += "TKOpenGl"; aMsg += aSrc;
633 aMsg += " | Type: "; aMsg += aType;
634 aMsg += " | ID: "; aMsg += (Standard_Integer )theId;
635 aMsg += " | Severity: "; aMsg += aSev;
636 aMsg += " | Message:\n ";
639 Messanger()->Send (aMsg, aGrav);
642 // =======================================================================
645 // =======================================================================
646 void OpenGl_Context::init()
651 arbNPTW = CheckExtension ("GL_ARB_texture_non_power_of_two");
652 extBgra = CheckExtension ("GL_EXT_bgra");
653 extAnis = CheckExtension ("GL_EXT_texture_filter_anisotropic");
654 extPDS = CheckExtension ("GL_EXT_packed_depth_stencil");
655 atiMem = CheckExtension ("GL_ATI_meminfo");
656 nvxMem = CheckExtension ("GL_NVX_gpu_memory_info");
658 // get number of maximum clipping planes
659 glGetIntegerv (GL_MAX_CLIP_PLANES, &myMaxClipPlanes);
660 glGetIntegerv (GL_MAX_TEXTURE_SIZE, &myMaxTexDim);
663 glGetIntegerv (GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &myAnisoMax);
666 myClippingState.Init (myMaxClipPlanes);
668 // initialize debug context extension
669 if (CheckExtension ("GL_ARB_debug_output"))
671 arbDbg = new OpenGl_ArbDbg();
672 memset (arbDbg, 0, sizeof(OpenGl_ArbDbg)); // nullify whole structure
673 if (!FindProcShort (arbDbg, glDebugMessageControlARB)
674 || !FindProcShort (arbDbg, glDebugMessageInsertARB)
675 || !FindProcShort (arbDbg, glDebugMessageCallbackARB)
676 || !FindProcShort (arbDbg, glGetDebugMessageLogARB))
682 && caps->contextDebug)
684 // setup default callback
685 arbDbg->glDebugMessageCallbackARB (debugCallbackWrap, this);
687 glEnable (GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
692 // initialize VBO extension (ARB)
693 if (CheckExtension ("GL_ARB_vertex_buffer_object"))
695 arbVBO = new OpenGl_ArbVBO();
696 memset (arbVBO, 0, sizeof(OpenGl_ArbVBO)); // nullify whole structure
697 if (!FindProcShort (arbVBO, glGenBuffersARB)
698 || !FindProcShort (arbVBO, glBindBufferARB)
699 || !FindProcShort (arbVBO, glBufferDataARB)
700 || !FindProcShort (arbVBO, glDeleteBuffersARB))
707 // initialize TBO extension (ARB)
708 if (CheckExtension ("GL_ARB_texture_buffer_object"))
710 arbTBO = new OpenGl_ArbTBO();
711 memset (arbTBO, 0, sizeof(OpenGl_ArbTBO)); // nullify whole structure
712 if (!FindProcShort (arbTBO, glTexBufferARB))
719 // initialize hardware instancing extension (ARB)
720 if (CheckExtension ("GL_ARB_draw_instanced"))
722 arbIns = new OpenGl_ArbIns();
723 memset (arbIns, 0, sizeof(OpenGl_ArbIns)); // nullify whole structure
724 if (!FindProcShort (arbIns, glDrawArraysInstancedARB)
725 || !FindProcShort (arbIns, glDrawElementsInstancedARB))
732 // initialize FBO extension (EXT)
733 if (CheckExtension ("GL_EXT_framebuffer_object"))
735 extFBO = new OpenGl_ExtFBO();
736 memset (extFBO, 0, sizeof(OpenGl_ExtFBO)); // nullify whole structure
737 if (!FindProcShort (extFBO, glGenFramebuffersEXT)
738 || !FindProcShort (extFBO, glDeleteFramebuffersEXT)
739 || !FindProcShort (extFBO, glBindFramebufferEXT)
740 || !FindProcShort (extFBO, glFramebufferTexture2DEXT)
741 || !FindProcShort (extFBO, glCheckFramebufferStatusEXT)
742 || !FindProcShort (extFBO, glGenRenderbuffersEXT)
743 || !FindProcShort (extFBO, glDeleteRenderbuffersEXT)
744 || !FindProcShort (extFBO, glBindRenderbufferEXT)
745 || !FindProcShort (extFBO, glRenderbufferStorageEXT)
746 || !FindProcShort (extFBO, glFramebufferRenderbufferEXT)
747 || !FindProcShort (extFBO, glGenerateMipmapEXT))
754 // initialize GS extension (EXT)
755 if (CheckExtension ("GL_EXT_geometry_shader4"))
757 extGS = new OpenGl_ExtGS();
758 memset (extGS, 0, sizeof(OpenGl_ExtGS)); // nullify whole structure
759 if (!FindProcShort (extGS, glProgramParameteriEXT))
766 myGlCore20 = new OpenGl_GlCore20();
767 memset (myGlCore20, 0, sizeof(OpenGl_GlCore20)); // nullify whole structure
769 // Check if OpenGL 1.2 core functionality is actually present
770 Standard_Boolean hasGlCore12 = IsGlGreaterEqual (1, 2)
771 && FindProcShort (myGlCore20, glBlendColor)
772 && FindProcShort (myGlCore20, glBlendEquation)
773 && FindProcShort (myGlCore20, glDrawRangeElements)
774 && FindProcShort (myGlCore20, glTexImage3D)
775 && FindProcShort (myGlCore20, glTexSubImage3D)
776 && FindProcShort (myGlCore20, glCopyTexSubImage3D);
778 // Check if OpenGL 1.3 core functionality is actually present
779 Standard_Boolean hasGlCore13 = IsGlGreaterEqual (1, 3)
780 && FindProcShort (myGlCore20, glActiveTexture)
781 && FindProcShort (myGlCore20, glSampleCoverage)
782 && FindProcShort (myGlCore20, glCompressedTexImage3D)
783 && FindProcShort (myGlCore20, glCompressedTexImage2D)
784 && FindProcShort (myGlCore20, glCompressedTexImage1D)
785 && FindProcShort (myGlCore20, glCompressedTexSubImage3D)
786 && FindProcShort (myGlCore20, glCompressedTexSubImage2D)
787 && FindProcShort (myGlCore20, glCompressedTexSubImage1D)
788 && FindProcShort (myGlCore20, glGetCompressedTexImage)
790 && FindProcShort (myGlCore20, glClientActiveTexture)
791 && FindProcShort (myGlCore20, glMultiTexCoord1d)
792 && FindProcShort (myGlCore20, glMultiTexCoord1dv)
793 && FindProcShort (myGlCore20, glMultiTexCoord1f)
794 && FindProcShort (myGlCore20, glMultiTexCoord1fv)
795 && FindProcShort (myGlCore20, glMultiTexCoord1i)
796 && FindProcShort (myGlCore20, glMultiTexCoord1iv)
797 && FindProcShort (myGlCore20, glMultiTexCoord1s)
798 && FindProcShort (myGlCore20, glMultiTexCoord1sv)
799 && FindProcShort (myGlCore20, glMultiTexCoord2d)
800 && FindProcShort (myGlCore20, glMultiTexCoord2dv)
801 && FindProcShort (myGlCore20, glMultiTexCoord2f)
802 && FindProcShort (myGlCore20, glMultiTexCoord2fv)
803 && FindProcShort (myGlCore20, glMultiTexCoord2i)
804 && FindProcShort (myGlCore20, glMultiTexCoord2iv)
805 && FindProcShort (myGlCore20, glMultiTexCoord2s)
806 && FindProcShort (myGlCore20, glMultiTexCoord2sv)
807 && FindProcShort (myGlCore20, glMultiTexCoord3d)
808 && FindProcShort (myGlCore20, glMultiTexCoord3dv)
809 && FindProcShort (myGlCore20, glMultiTexCoord3f)
810 && FindProcShort (myGlCore20, glMultiTexCoord3fv)
811 && FindProcShort (myGlCore20, glMultiTexCoord3i)
812 && FindProcShort (myGlCore20, glMultiTexCoord3iv)
813 && FindProcShort (myGlCore20, glMultiTexCoord3s)
814 && FindProcShort (myGlCore20, glMultiTexCoord3sv)
815 && FindProcShort (myGlCore20, glMultiTexCoord4d)
816 && FindProcShort (myGlCore20, glMultiTexCoord4dv)
817 && FindProcShort (myGlCore20, glMultiTexCoord4f)
818 && FindProcShort (myGlCore20, glMultiTexCoord4fv)
819 && FindProcShort (myGlCore20, glMultiTexCoord4i)
820 && FindProcShort (myGlCore20, glMultiTexCoord4iv)
821 && FindProcShort (myGlCore20, glMultiTexCoord4s)
822 && FindProcShort (myGlCore20, glMultiTexCoord4sv)
823 && FindProcShort (myGlCore20, glLoadTransposeMatrixf)
824 && FindProcShort (myGlCore20, glLoadTransposeMatrixd)
825 && FindProcShort (myGlCore20, glMultTransposeMatrixf)
826 && FindProcShort (myGlCore20, glMultTransposeMatrixd);
828 // Check if OpenGL 1.4 core functionality is actually present
829 Standard_Boolean hasGlCore14 = IsGlGreaterEqual (1, 4)
830 && FindProcShort (myGlCore20, glBlendFuncSeparate)
831 && FindProcShort (myGlCore20, glMultiDrawArrays)
832 && FindProcShort (myGlCore20, glMultiDrawElements)
833 && FindProcShort (myGlCore20, glPointParameterf)
834 && FindProcShort (myGlCore20, glPointParameterfv)
835 && FindProcShort (myGlCore20, glPointParameteri)
836 && FindProcShort (myGlCore20, glPointParameteriv);
838 // Check if OpenGL 1.5 core functionality is actually present
839 Standard_Boolean hasGlCore15 = IsGlGreaterEqual (1, 5)
840 && FindProcShort (myGlCore20, glGenQueries)
841 && FindProcShort (myGlCore20, glDeleteQueries)
842 && FindProcShort (myGlCore20, glIsQuery)
843 && FindProcShort (myGlCore20, glBeginQuery)
844 && FindProcShort (myGlCore20, glEndQuery)
845 && FindProcShort (myGlCore20, glGetQueryiv)
846 && FindProcShort (myGlCore20, glGetQueryObjectiv)
847 && FindProcShort (myGlCore20, glGetQueryObjectuiv)
848 && FindProcShort (myGlCore20, glBindBuffer)
849 && FindProcShort (myGlCore20, glDeleteBuffers)
850 && FindProcShort (myGlCore20, glGenBuffers)
851 && FindProcShort (myGlCore20, glIsBuffer)
852 && FindProcShort (myGlCore20, glBufferData)
853 && FindProcShort (myGlCore20, glBufferSubData)
854 && FindProcShort (myGlCore20, glGetBufferSubData)
855 && FindProcShort (myGlCore20, glMapBuffer)
856 && FindProcShort (myGlCore20, glUnmapBuffer)
857 && FindProcShort (myGlCore20, glGetBufferParameteriv)
858 && FindProcShort (myGlCore20, glGetBufferPointerv);
860 // Check if OpenGL 2.0 core functionality is actually present
861 Standard_Boolean hasGlCore20 = IsGlGreaterEqual (2, 0)
862 && FindProcShort (myGlCore20, glBlendEquationSeparate)
863 && FindProcShort (myGlCore20, glDrawBuffers)
864 && FindProcShort (myGlCore20, glStencilOpSeparate)
865 && FindProcShort (myGlCore20, glStencilFuncSeparate)
866 && FindProcShort (myGlCore20, glStencilMaskSeparate)
867 && FindProcShort (myGlCore20, glAttachShader)
868 && FindProcShort (myGlCore20, glBindAttribLocation)
869 && FindProcShort (myGlCore20, glCompileShader)
870 && FindProcShort (myGlCore20, glCreateProgram)
871 && FindProcShort (myGlCore20, glCreateShader)
872 && FindProcShort (myGlCore20, glDeleteProgram)
873 && FindProcShort (myGlCore20, glDeleteShader)
874 && FindProcShort (myGlCore20, glDetachShader)
875 && FindProcShort (myGlCore20, glDisableVertexAttribArray)
876 && FindProcShort (myGlCore20, glEnableVertexAttribArray)
877 && FindProcShort (myGlCore20, glGetActiveAttrib)
878 && FindProcShort (myGlCore20, glGetActiveUniform)
879 && FindProcShort (myGlCore20, glGetAttachedShaders)
880 && FindProcShort (myGlCore20, glGetAttribLocation)
881 && FindProcShort (myGlCore20, glGetProgramiv)
882 && FindProcShort (myGlCore20, glGetProgramInfoLog)
883 && FindProcShort (myGlCore20, glGetShaderiv)
884 && FindProcShort (myGlCore20, glGetShaderInfoLog)
885 && FindProcShort (myGlCore20, glGetShaderSource)
886 && FindProcShort (myGlCore20, glGetUniformLocation)
887 && FindProcShort (myGlCore20, glGetUniformfv)
888 && FindProcShort (myGlCore20, glGetUniformiv)
889 && FindProcShort (myGlCore20, glGetVertexAttribdv)
890 && FindProcShort (myGlCore20, glGetVertexAttribfv)
891 && FindProcShort (myGlCore20, glGetVertexAttribiv)
892 && FindProcShort (myGlCore20, glGetVertexAttribPointerv)
893 && FindProcShort (myGlCore20, glIsProgram)
894 && FindProcShort (myGlCore20, glIsShader)
895 && FindProcShort (myGlCore20, glLinkProgram)
896 && FindProcShort (myGlCore20, glShaderSource)
897 && FindProcShort (myGlCore20, glUseProgram)
898 && FindProcShort (myGlCore20, glUniform1f)
899 && FindProcShort (myGlCore20, glUniform2f)
900 && FindProcShort (myGlCore20, glUniform3f)
901 && FindProcShort (myGlCore20, glUniform4f)
902 && FindProcShort (myGlCore20, glUniform1i)
903 && FindProcShort (myGlCore20, glUniform2i)
904 && FindProcShort (myGlCore20, glUniform3i)
905 && FindProcShort (myGlCore20, glUniform4i)
906 && FindProcShort (myGlCore20, glUniform1fv)
907 && FindProcShort (myGlCore20, glUniform2fv)
908 && FindProcShort (myGlCore20, glUniform3fv)
909 && FindProcShort (myGlCore20, glUniform4fv)
910 && FindProcShort (myGlCore20, glUniform1iv)
911 && FindProcShort (myGlCore20, glUniform2iv)
912 && FindProcShort (myGlCore20, glUniform3iv)
913 && FindProcShort (myGlCore20, glUniform4iv)
914 && FindProcShort (myGlCore20, glUniformMatrix2fv)
915 && FindProcShort (myGlCore20, glUniformMatrix3fv)
916 && FindProcShort (myGlCore20, glUniformMatrix4fv)
917 && FindProcShort (myGlCore20, glValidateProgram)
918 && FindProcShort (myGlCore20, glVertexAttrib1d)
919 && FindProcShort (myGlCore20, glVertexAttrib1dv)
920 && FindProcShort (myGlCore20, glVertexAttrib1f)
921 && FindProcShort (myGlCore20, glVertexAttrib1fv)
922 && FindProcShort (myGlCore20, glVertexAttrib1s)
923 && FindProcShort (myGlCore20, glVertexAttrib1sv)
924 && FindProcShort (myGlCore20, glVertexAttrib2d)
925 && FindProcShort (myGlCore20, glVertexAttrib2dv)
926 && FindProcShort (myGlCore20, glVertexAttrib2f)
927 && FindProcShort (myGlCore20, glVertexAttrib2fv)
928 && FindProcShort (myGlCore20, glVertexAttrib2s)
929 && FindProcShort (myGlCore20, glVertexAttrib2sv)
930 && FindProcShort (myGlCore20, glVertexAttrib3d)
931 && FindProcShort (myGlCore20, glVertexAttrib3dv)
932 && FindProcShort (myGlCore20, glVertexAttrib3f)
933 && FindProcShort (myGlCore20, glVertexAttrib3fv)
934 && FindProcShort (myGlCore20, glVertexAttrib3s)
935 && FindProcShort (myGlCore20, glVertexAttrib3sv)
936 && FindProcShort (myGlCore20, glVertexAttrib4Nbv)
937 && FindProcShort (myGlCore20, glVertexAttrib4Niv)
938 && FindProcShort (myGlCore20, glVertexAttrib4Nsv)
939 && FindProcShort (myGlCore20, glVertexAttrib4Nub)
940 && FindProcShort (myGlCore20, glVertexAttrib4Nubv)
941 && FindProcShort (myGlCore20, glVertexAttrib4Nuiv)
942 && FindProcShort (myGlCore20, glVertexAttrib4Nusv)
943 && FindProcShort (myGlCore20, glVertexAttrib4bv)
944 && FindProcShort (myGlCore20, glVertexAttrib4d)
945 && FindProcShort (myGlCore20, glVertexAttrib4dv)
946 && FindProcShort (myGlCore20, glVertexAttrib4f)
947 && FindProcShort (myGlCore20, glVertexAttrib4fv)
948 && FindProcShort (myGlCore20, glVertexAttrib4iv)
949 && FindProcShort (myGlCore20, glVertexAttrib4s)
950 && FindProcShort (myGlCore20, glVertexAttrib4sv)
951 && FindProcShort (myGlCore20, glVertexAttrib4ubv)
952 && FindProcShort (myGlCore20, glVertexAttrib4uiv)
953 && FindProcShort (myGlCore20, glVertexAttrib4usv)
954 && FindProcShort (myGlCore20, glVertexAttribPointer);
1003 core20 = myGlCore20;
1006 // =======================================================================
1007 // function : IsFeedback
1009 // =======================================================================
1010 Standard_Boolean OpenGl_Context::IsFeedback() const
1012 return myIsFeedback;
1015 // =======================================================================
1016 // function : SetFeedback
1018 // =======================================================================
1019 void OpenGl_Context::SetFeedback (const Standard_Boolean theFeedbackOn)
1021 myIsFeedback = theFeedbackOn;
1024 // =======================================================================
1025 // function : MemoryInfo
1027 // =======================================================================
1028 Standard_Size OpenGl_Context::AvailableMemory() const
1032 // this is actually information for VBO pool
1033 // however because pools are mostly shared
1034 // it can be used for total GPU memory estimations
1037 glGetIntegerv (GL_VBO_FREE_MEMORY_ATI, aMemInfo);
1038 // returned value is in KiB, however this maybe changed in future
1039 return Standard_Size(aMemInfo[0]) * 1024;
1043 // current available dedicated video memory (in KiB), currently unused GPU memory
1045 glGetIntegerv (GL_GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX, &aMemInfo);
1046 return Standard_Size(aMemInfo) * 1024;
1051 // =======================================================================
1052 // function : MemoryInfo
1054 // =======================================================================
1055 TCollection_AsciiString OpenGl_Context::MemoryInfo() const
1057 TCollection_AsciiString anInfo;
1061 memset (aValues, 0, sizeof(aValues));
1062 glGetIntegerv (GL_VBO_FREE_MEMORY_ATI, aValues);
1064 // total memory free in the pool
1065 anInfo += TCollection_AsciiString (" GPU free memory: ") + (aValues[0] / 1024) + " MiB\n";
1067 // largest available free block in the pool
1068 anInfo += TCollection_AsciiString (" Largest free block: ") + (aValues[1] / 1024) + " MiB\n";
1069 if (aValues[2] != aValues[0])
1071 // total auxiliary memory free
1072 anInfo += TCollection_AsciiString (" Free memory: ") + (aValues[2] / 1024) + " MiB\n";
1077 //current available dedicated video memory (in KiB), currently unused GPU memory
1079 glGetIntegerv (GL_GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX, &aValue);
1080 anInfo += TCollection_AsciiString (" GPU free memory: ") + (aValue / 1024) + " MiB\n";
1082 // dedicated video memory, total size (in KiB) of the GPU memory
1083 GLint aDedicated = 0;
1084 glGetIntegerv (GL_GPU_MEMORY_INFO_DEDICATED_VIDMEM_NVX, &aDedicated);
1085 anInfo += TCollection_AsciiString (" GPU memory: ") + (aDedicated / 1024) + " MiB\n";
1087 // total available memory, total size (in KiB) of the memory available for allocations
1088 glGetIntegerv (GL_GPU_MEMORY_INFO_TOTAL_AVAILABLE_MEMORY_NVX, &aValue);
1089 if (aValue != aDedicated)
1091 // different only for special configurations
1092 anInfo += TCollection_AsciiString (" Total memory: ") + (aValue / 1024) + " MiB\n";
1099 // =======================================================================
1100 // function : GetResource
1102 // =======================================================================
1103 const Handle(OpenGl_Resource)& OpenGl_Context::GetResource (const TCollection_AsciiString& theKey) const
1105 return mySharedResources->IsBound (theKey) ? mySharedResources->Find (theKey) : NULL_GL_RESOURCE;
1108 // =======================================================================
1109 // function : ShareResource
1111 // =======================================================================
1112 Standard_Boolean OpenGl_Context::ShareResource (const TCollection_AsciiString& theKey,
1113 const Handle(OpenGl_Resource)& theResource)
1115 if (theKey.IsEmpty() || theResource.IsNull())
1117 return Standard_False;
1119 return mySharedResources->Bind (theKey, theResource);
1122 // =======================================================================
1123 // function : ReleaseResource
1125 // =======================================================================
1126 void OpenGl_Context::ReleaseResource (const TCollection_AsciiString& theKey,
1127 const Standard_Boolean theToDelay)
1129 if (!mySharedResources->IsBound (theKey))
1133 const Handle(OpenGl_Resource)& aRes = mySharedResources->Find (theKey);
1134 if (aRes->GetRefCount() > 1)
1141 myDelayed->Bind (theKey, 1);
1145 aRes->Release (this);
1146 mySharedResources->UnBind (theKey);
1150 // =======================================================================
1151 // function : DelayedRelease
1153 // =======================================================================
1154 void OpenGl_Context::DelayedRelease (Handle(OpenGl_Resource)& theResource)
1156 myReleaseQueue->Push (theResource);
1157 theResource.Nullify();
1160 // =======================================================================
1161 // function : ReleaseDelayed
1163 // =======================================================================
1164 void OpenGl_Context::ReleaseDelayed()
1166 // release queued elements
1167 while (!myReleaseQueue->IsEmpty())
1169 myReleaseQueue->Front()->Release (this);
1170 myReleaseQueue->Pop();
1173 // release delayed shared resoruces
1174 NCollection_Vector<TCollection_AsciiString> aDeadList;
1175 for (NCollection_DataMap<TCollection_AsciiString, Standard_Integer>::Iterator anIter (*myDelayed);
1176 anIter.More(); anIter.Next())
1178 if (++anIter.ChangeValue() <= 2)
1180 continue; // postpone release one more frame to ensure noone use it periodically
1183 const TCollection_AsciiString& aKey = anIter.Key();
1184 if (!mySharedResources->IsBound (aKey))
1186 // mixed unshared strategy delayed/undelayed was used!
1187 aDeadList.Append (aKey);
1191 Handle(OpenGl_Resource)& aRes = mySharedResources->ChangeFind (aKey);
1192 if (aRes->GetRefCount() > 1)
1194 // should be only 1 instance in mySharedResources
1195 // if not - resource was reused again
1196 aDeadList.Append (aKey);
1200 // release resource if no one requiested it more than 2 redraw calls
1201 aRes->Release (this);
1202 mySharedResources->UnBind (aKey);
1203 aDeadList.Append (aKey);
1206 for (Standard_Integer anIter = 0; anIter < aDeadList.Length(); ++anIter)
1208 myDelayed->UnBind (aDeadList.Value (anIter));