1 // Created on: 2012-01-26
2 // Created by: Kirill GAVRILOV
3 // Copyright (c) 2012 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
24 #include <OpenGl_Context.hxx>
26 #include <OpenGl_ArbVBO.hxx>
27 #include <OpenGl_ArbTBO.hxx>
28 #include <OpenGl_ArbIns.hxx>
29 #include <OpenGl_ArbDbg.hxx>
30 #include <OpenGl_ExtFBO.hxx>
31 #include <OpenGl_ExtGS.hxx>
32 #include <OpenGl_GlCore20.hxx>
34 #include <NCollection_Vector.hxx>
36 #include <Standard_ProgramError.hxx>
40 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
43 #include <GL/glx.h> // glXGetProcAddress()
46 // GL_NVX_gpu_memory_info
47 #ifndef GL_GPU_MEMORY_INFO_DEDICATED_VIDMEM_NVX
50 GL_GPU_MEMORY_INFO_DEDICATED_VIDMEM_NVX = 0x9047,
51 GL_GPU_MEMORY_INFO_TOTAL_AVAILABLE_MEMORY_NVX = 0x9048,
52 GL_GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX = 0x9049,
53 GL_GPU_MEMORY_INFO_EVICTION_COUNT_NVX = 0x904A,
54 GL_GPU_MEMORY_INFO_EVICTED_MEMORY_NVX = 0x904B
58 IMPLEMENT_STANDARD_HANDLE (OpenGl_Context, Standard_Transient)
59 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Context, Standard_Transient)
61 //! Make record shorter to retrieve function pointer using variable with same name
62 #define FindProcShort(theStruct, theFunc) FindProc(#theFunc, theStruct->theFunc)
66 static const Handle(OpenGl_Resource) NULL_GL_RESOURCE;
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 atiMem (Standard_False),
90 nvxMem (Standard_False),
91 mySharedResources (new OpenGl_ResourcesMap()),
92 myDelayed (new OpenGl_DelayReleaseMap()),
93 myReleaseQueue (new OpenGl_ResourcesQueue()),
100 myIsFeedback (Standard_False),
101 myIsInitialized (Standard_False)
103 #if defined(MAC_OS_X_VERSION_10_3) && !defined(MACOSX_USE_GLX)
104 // Vendors can not extend functionality on this system
105 // and developers are limited to OpenGL support provided by Mac OS X SDK.
106 // We retrieve function pointers from system library
107 // to generalize extensions support on all platforms.
108 // In this way we also reach binary compatibility benefit between OS releases
109 // if some newest functionality is optionally used.
110 // Notice that GL version / extension availability checks are required
111 // because function pointers may be available but not functionality itself
112 // (depends on renderer).
113 myGlLibHandle = dlopen ("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL", RTLD_LAZY);
117 // =======================================================================
118 // function : ~OpenGl_Context
120 // =======================================================================
121 OpenGl_Context::~OpenGl_Context()
123 // release clean up queue
126 // release shared resources if any
127 if (((const Handle(Standard_Transient)& )mySharedResources)->GetRefCount() <= 1)
129 for (NCollection_DataMap<TCollection_AsciiString, Handle(OpenGl_Resource)>::Iterator anIter (*mySharedResources);
130 anIter.More(); anIter.Next())
132 anIter.Value()->Release (this);
135 mySharedResources.Nullify();
147 // =======================================================================
148 // function : MaxDegreeOfAnisotropy
150 // =======================================================================
151 Standard_Integer OpenGl_Context::MaxDegreeOfAnisotropy() const
156 // =======================================================================
157 // function : MaxTextureSize
159 // =======================================================================
160 Standard_Integer OpenGl_Context::MaxTextureSize() const
165 // =======================================================================
168 // =======================================================================
169 void OpenGl_Context::Share (const Handle(OpenGl_Context)& theShareCtx)
171 if (!theShareCtx.IsNull())
173 mySharedResources = theShareCtx->mySharedResources;
174 myDelayed = theShareCtx->myDelayed;
175 myReleaseQueue = theShareCtx->myReleaseQueue;
179 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
181 // =======================================================================
182 // function : IsCurrent
184 // =======================================================================
185 Standard_Boolean OpenGl_Context::IsCurrent() const
188 if (myWindowDC == NULL || myGContext == NULL)
190 return Standard_False;
192 return (( (HDC )myWindowDC == wglGetCurrentDC())
193 && ((HGLRC )myGContext == wglGetCurrentContext()));
195 if (myDisplay == NULL || myWindow == 0 || myGContext == 0)
197 return Standard_False;
200 return ( ((Display* )myDisplay == glXGetCurrentDisplay())
201 && ((GLXContext )myGContext == glXGetCurrentContext())
202 && ((GLXDrawable )myWindow == glXGetCurrentDrawable()));
206 // =======================================================================
207 // function : MakeCurrent
209 // =======================================================================
210 Standard_Boolean OpenGl_Context::MakeCurrent()
213 if (myWindowDC == NULL || myGContext == NULL)
215 Standard_ProgramError_Raise_if (myIsInitialized, "OpenGl_Context::Init() should be called before!");
216 return Standard_False;
219 // technically it should be safe to activate already bound GL context
220 // however some drivers (Intel etc.) may FAIL doing this for unknown reason
223 return Standard_True;
225 else if (wglMakeCurrent ((HDC )myWindowDC, (HGLRC )myGContext) != TRUE)
227 // notice that glGetError() couldn't be used here!
228 wchar_t* aMsgBuff = NULL;
229 DWORD anErrorCode = GetLastError();
230 FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
231 NULL, anErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (wchar_t* )&aMsgBuff, 0, NULL);
232 if (aMsgBuff != NULL)
234 std::wcerr << L"OpenGL interface: wglMakeCurrent() failed. " << aMsgBuff << L" (" << int(anErrorCode) << L")\n";
235 LocalFree (aMsgBuff);
239 std::wcerr << L"OpenGL interface: wglMakeCurrent() failed with #" << int(anErrorCode) << L" error code\n";
241 return Standard_False;
244 if (myDisplay == NULL || myWindow == 0 || myGContext == 0)
246 Standard_ProgramError_Raise_if (myIsInitialized, "OpenGl_Context::Init() should be called before!");
247 return Standard_False;
250 if (!glXMakeCurrent ((Display* )myDisplay, (GLXDrawable )myWindow, (GLXContext )myGContext))
252 // if there is no current context it might be impossible to use glGetError() correctly
253 //std::cerr << "glXMakeCurrent() failed!\n";
254 return Standard_False;
257 return Standard_True;
260 // =======================================================================
261 // function : SwapBuffers
263 // =======================================================================
264 void OpenGl_Context::SwapBuffers()
267 if ((HDC )myWindowDC != NULL)
269 ::SwapBuffers ((HDC )myWindowDC);
273 if ((Display* )myDisplay != NULL)
275 glXSwapBuffers ((Display* )myDisplay, (GLXDrawable )myWindow);
282 // =======================================================================
283 // function : findProc
285 // =======================================================================
286 void* OpenGl_Context::findProc (const char* theFuncName)
289 return wglGetProcAddress (theFuncName);
290 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
291 return (myGlLibHandle != NULL) ? dlsym (myGlLibHandle, theFuncName) : NULL;
293 return (void* )glXGetProcAddress ((const GLubyte* )theFuncName);
297 // =======================================================================
298 // function : CheckExtension
300 // =======================================================================
301 Standard_Boolean OpenGl_Context::CheckExtension (const char* theExtName) const
303 if (theExtName == NULL)
305 std::cerr << "CheckExtension called with NULL string!\n";
306 return Standard_False;
309 // available since OpenGL 3.0
310 // and the ONLY way to check extensions with OpenGL 3.1+ core profile
311 /**if (IsGlGreaterEqual (3, 0))
314 glGetIntegerv (GL_NUM_EXTENSIONS, &anExtNb);
315 for (GLint anIter = 0; anIter < anExtNb; ++anIter)
317 const char* anExtension = (const char* )core30->glGetStringi (GL_EXTENSIONS, (GLuint )anIter);
318 if (anExtension[anExtNameLen] == '\0' &&
319 strncmp (anExtension, theExtName, anExtNameLen) == 0)
321 return Standard_True;
324 return Standard_False;
327 // use old way with huge string for all extensions
328 const char* anExtString = (const char* )glGetString (GL_EXTENSIONS);
329 if (anExtString == NULL)
331 std::cerr << "glGetString (GL_EXTENSIONS) returns NULL! No GL context?\n";
332 return Standard_False;
334 return CheckExtension (anExtString, theExtName);
337 // =======================================================================
338 // function : CheckExtension
340 // =======================================================================
341 Standard_Boolean OpenGl_Context::CheckExtension (const char* theExtString,
342 const char* theExtName)
344 if (theExtString == NULL)
346 return Standard_False;
349 // Search for theExtName in the extensions string.
350 // Use of strstr() is not sufficient because extension names can be prefixes of other extension names.
351 char* aPtrIter = (char* )theExtString;
352 const char* aPtrEnd = aPtrIter + strlen (theExtString);
353 const size_t anExtNameLen = strlen (theExtName);
354 while (aPtrIter < aPtrEnd)
356 const size_t n = strcspn (aPtrIter, " ");
357 if ((n == anExtNameLen) && (strncmp (aPtrIter, theExtName, anExtNameLen) == 0))
359 return Standard_True;
363 return Standard_False;
366 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
368 // =======================================================================
371 // =======================================================================
372 Standard_Boolean OpenGl_Context::Init()
376 return Standard_True;
380 myWindowDC = (Aspect_Handle )wglGetCurrentDC();
381 myGContext = (Aspect_RenderingContext )wglGetCurrentContext();
383 myDisplay = (Aspect_Display )glXGetCurrentDisplay();
384 myGContext = (Aspect_RenderingContext )glXGetCurrentContext();
385 myWindow = (Aspect_Drawable )glXGetCurrentDrawable();
387 if (myGContext == NULL)
389 return Standard_False;
393 myIsInitialized = Standard_True;
394 return Standard_True;
399 // =======================================================================
402 // =======================================================================
404 Standard_Boolean OpenGl_Context::Init (const Aspect_Handle theWindow,
405 const Aspect_Handle theWindowDC,
406 const Aspect_RenderingContext theGContext)
407 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
408 Standard_Boolean OpenGl_Context::Init (const void* theGContext)
410 Standard_Boolean OpenGl_Context::Init (const Aspect_Drawable theWindow,
411 const Aspect_Display theDisplay,
412 const Aspect_RenderingContext theGContext)
415 Standard_ProgramError_Raise_if (myIsInitialized, "OpenGl_Context::Init() should be called only once!");
417 myWindow = theWindow;
418 myGContext = theGContext;
419 myWindowDC = theWindowDC;
420 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
421 myGContext = (void* )theGContext;
423 myWindow = theWindow;
424 myGContext = theGContext;
425 myDisplay = theDisplay;
427 if (myGContext == NULL || !MakeCurrent())
429 return Standard_False;
433 myIsInitialized = Standard_True;
434 return Standard_True;
437 // =======================================================================
438 // function : ResetErrors
440 // =======================================================================
441 void OpenGl_Context::ResetErrors()
443 while (glGetError() != GL_NO_ERROR)
449 // =======================================================================
450 // function : readGlVersion
452 // =======================================================================
453 void OpenGl_Context::readGlVersion()
459 // available since OpenGL 3.0
460 GLint aMajor = 0, aMinor = 0;
461 glGetIntegerv (GL_MAJOR_VERSION, &aMajor);
462 glGetIntegerv (GL_MINOR_VERSION, &aMinor);
463 // glGetError() sometimes does not report an error here even if
464 // GL does not know GL_MAJOR_VERSION and GL_MINOR_VERSION constants.
465 // This happens on some rendereres like e.g. Cygwin MESA.
466 // Thus checking additionally if GL has put anything to
467 // the output variables.
468 if (glGetError() == GL_NO_ERROR && aMajor != 0 && aMinor != 0)
470 myGlVerMajor = aMajor;
471 myGlVerMinor = aMinor;
476 // Read version string.
477 // Notice that only first two numbers splitted by point '2.1 XXXXX' are significant.
478 // Following trash (after space) is vendor-specific.
479 // New drivers also returns micro version of GL like '3.3.0' which has no meaning
480 // and should be considered as vendor-specific too.
481 const char* aVerStr = (const char* )glGetString (GL_VERSION);
482 if (aVerStr == NULL || *aVerStr == '\0')
484 // invalid GL context
488 // parse string for major number
492 while (aVerStr[aMajIter] >= '0' && aVerStr[aMajIter] <= '9')
496 if (aMajIter == 0 || aMajIter >= sizeof(aMajorStr))
500 memcpy (aMajorStr, aVerStr, aMajIter);
501 aMajorStr[aMajIter] = '\0';
503 // parse string for minor number
504 aVerStr += aMajIter + 1;
506 while (aVerStr[aMinIter] >= '0' && aVerStr[aMinIter] <= '9')
510 if (aMinIter == 0 || aMinIter >= sizeof(aMinorStr))
514 memcpy (aMinorStr, aVerStr, aMinIter);
515 aMinorStr[aMinIter] = '\0';
518 myGlVerMajor = atoi (aMajorStr);
519 myGlVerMinor = atoi (aMinorStr);
521 if (myGlVerMajor <= 0)
528 static Standard_CString THE_DBGMSG_UNKNOWN = "UNKNOWN";
529 static Standard_CString THE_DBGMSG_SOURCES[] =
531 "OpenGL", // GL_DEBUG_SOURCE_API_ARB
532 "Window System", // GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB
533 "Shader Compiler", // GL_DEBUG_SOURCE_SHADER_COMPILER_ARB
534 "Third Party", // GL_DEBUG_SOURCE_THIRD_PARTY_ARB
535 "Application", // GL_DEBUG_SOURCE_APPLICATION_ARB
536 "Other" // GL_DEBUG_SOURCE_OTHER_ARB
539 static Standard_CString THE_DBGMSG_TYPES[] =
541 "Error", // GL_DEBUG_TYPE_ERROR_ARB
542 "Deprecated", // GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
543 "Undefined behavior", // GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
544 "Portability", // GL_DEBUG_TYPE_PORTABILITY_ARB
545 "Performance", // GL_DEBUG_TYPE_PERFORMANCE_ARB
546 "Other" // GL_DEBUG_TYPE_OTHER_ARB
549 static Standard_CString THE_DBGMSG_SEV_HIGH = "High"; // GL_DEBUG_SEVERITY_HIGH_ARB
550 static Standard_CString THE_DBGMSG_SEV_MEDIUM = "Medium"; // GL_DEBUG_SEVERITY_MEDIUM_ARB
551 static Standard_CString THE_DBGMSG_SEV_LOW = "Low"; // GL_DEBUG_SEVERITY_LOW_ARB
553 static void APIENTRY debugCallbackWrap(unsigned int theSource,
554 unsigned int theType,
556 unsigned int theSeverity,
558 const char* theMessage,
559 void* /*theUserParam*/)
561 //OpenGl_Context* aCtx = (OpenGl_Context* )theUserParam;
562 Standard_CString& aSrc = (theSource >= GL_DEBUG_SOURCE_API_ARB
563 && theSource <= GL_DEBUG_SOURCE_OTHER_ARB)
564 ? THE_DBGMSG_SOURCES[theSource - GL_DEBUG_SOURCE_API_ARB]
565 : THE_DBGMSG_UNKNOWN;
566 Standard_CString& aType = (theType >= GL_DEBUG_TYPE_ERROR_ARB
567 && theType <= GL_DEBUG_TYPE_OTHER_ARB)
568 ? THE_DBGMSG_TYPES[theType - GL_DEBUG_TYPE_ERROR_ARB]
569 : THE_DBGMSG_UNKNOWN;
570 Standard_CString& aSev = theSeverity == GL_DEBUG_SEVERITY_HIGH_ARB
571 ? THE_DBGMSG_SEV_HIGH
572 : (theSeverity == GL_DEBUG_SEVERITY_MEDIUM_ARB
573 ? THE_DBGMSG_SEV_MEDIUM
574 : THE_DBGMSG_SEV_LOW);
575 std::cerr << "Source:" << aSrc
576 << " | Type:" << aType
578 << " | Severity:" << aSev
579 << " | Message:\n " << theMessage << "\n";
582 // =======================================================================
585 // =======================================================================
586 void OpenGl_Context::init()
591 arbNPTW = CheckExtension ("GL_ARB_texture_non_power_of_two");
592 extBgra = CheckExtension ("GL_EXT_bgra");
593 extAnis = CheckExtension ("GL_EXT_texture_filter_anisotropic");
594 atiMem = CheckExtension ("GL_ATI_meminfo");
595 nvxMem = CheckExtension ("GL_NVX_gpu_memory_info");
597 glGetIntegerv (GL_MAX_TEXTURE_SIZE, &myMaxTexDim);
600 glGetIntegerv (GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &myAnisoMax);
603 // initialize debug context extension
604 if (CheckExtension ("GL_ARB_debug_output"))
606 arbDbg = new OpenGl_ArbDbg();
607 memset (arbDbg, 0, sizeof(OpenGl_ArbDbg)); // nullify whole structure
608 if (!FindProcShort (arbDbg, glDebugMessageControlARB)
609 || !FindProcShort (arbDbg, glDebugMessageInsertARB)
610 || !FindProcShort (arbDbg, glDebugMessageCallbackARB)
611 || !FindProcShort (arbDbg, glGetDebugMessageLogARB))
617 && caps->contextDebug)
619 // setup default callback
620 arbDbg->glDebugMessageCallbackARB (debugCallbackWrap, this);
621 glEnable (GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
625 // initialize VBO extension (ARB)
626 if (CheckExtension ("GL_ARB_vertex_buffer_object"))
628 arbVBO = new OpenGl_ArbVBO();
629 memset (arbVBO, 0, sizeof(OpenGl_ArbVBO)); // nullify whole structure
630 if (!FindProcShort (arbVBO, glGenBuffersARB)
631 || !FindProcShort (arbVBO, glBindBufferARB)
632 || !FindProcShort (arbVBO, glBufferDataARB)
633 || !FindProcShort (arbVBO, glDeleteBuffersARB))
640 // initialize TBO extension (ARB)
641 if (CheckExtension ("GL_ARB_texture_buffer_object"))
643 arbTBO = new OpenGl_ArbTBO();
644 memset (arbTBO, 0, sizeof(OpenGl_ArbTBO)); // nullify whole structure
645 if (!FindProcShort (arbTBO, glTexBufferARB))
652 // initialize hardware instancing extension (ARB)
653 if (CheckExtension ("GL_ARB_draw_instanced"))
655 arbIns = new OpenGl_ArbIns();
656 memset (arbIns, 0, sizeof(OpenGl_ArbIns)); // nullify whole structure
657 if (!FindProcShort (arbIns, glDrawArraysInstancedARB)
658 || !FindProcShort (arbIns, glDrawElementsInstancedARB))
665 // initialize FBO extension (EXT)
666 if (CheckExtension ("GL_EXT_framebuffer_object"))
668 extFBO = new OpenGl_ExtFBO();
669 memset (extFBO, 0, sizeof(OpenGl_ExtFBO)); // nullify whole structure
670 if (!FindProcShort (extFBO, glGenFramebuffersEXT)
671 || !FindProcShort (extFBO, glDeleteFramebuffersEXT)
672 || !FindProcShort (extFBO, glBindFramebufferEXT)
673 || !FindProcShort (extFBO, glFramebufferTexture2DEXT)
674 || !FindProcShort (extFBO, glCheckFramebufferStatusEXT)
675 || !FindProcShort (extFBO, glGenRenderbuffersEXT)
676 || !FindProcShort (extFBO, glDeleteRenderbuffersEXT)
677 || !FindProcShort (extFBO, glBindRenderbufferEXT)
678 || !FindProcShort (extFBO, glRenderbufferStorageEXT)
679 || !FindProcShort (extFBO, glFramebufferRenderbufferEXT)
680 || !FindProcShort (extFBO, glGenerateMipmapEXT))
687 // initialize GS extension (EXT)
688 if (CheckExtension ("GL_EXT_geometry_shader4"))
690 extGS = new OpenGl_ExtGS();
691 memset (extGS, 0, sizeof(OpenGl_ExtGS)); // nullify whole structure
692 if (!FindProcShort (extGS, glProgramParameteriEXT))
699 myGlCore20 = new OpenGl_GlCore20();
700 memset (myGlCore20, 0, sizeof(OpenGl_GlCore20)); // nullify whole structure
702 // Check if OpenGL 1.2 core functionality is actually present
703 Standard_Boolean hasGlCore12 = IsGlGreaterEqual (1, 2)
704 && FindProcShort (myGlCore20, glBlendColor)
705 && FindProcShort (myGlCore20, glBlendEquation)
706 && FindProcShort (myGlCore20, glDrawRangeElements)
707 && FindProcShort (myGlCore20, glTexImage3D)
708 && FindProcShort (myGlCore20, glTexSubImage3D)
709 && FindProcShort (myGlCore20, glCopyTexSubImage3D);
711 // Check if OpenGL 1.3 core functionality is actually present
712 Standard_Boolean hasGlCore13 = IsGlGreaterEqual (1, 3)
713 && FindProcShort (myGlCore20, glActiveTexture)
714 && FindProcShort (myGlCore20, glSampleCoverage)
715 && FindProcShort (myGlCore20, glCompressedTexImage3D)
716 && FindProcShort (myGlCore20, glCompressedTexImage2D)
717 && FindProcShort (myGlCore20, glCompressedTexImage1D)
718 && FindProcShort (myGlCore20, glCompressedTexSubImage3D)
719 && FindProcShort (myGlCore20, glCompressedTexSubImage2D)
720 && FindProcShort (myGlCore20, glCompressedTexSubImage1D)
721 && FindProcShort (myGlCore20, glGetCompressedTexImage)
723 && FindProcShort (myGlCore20, glClientActiveTexture)
724 && FindProcShort (myGlCore20, glMultiTexCoord1d)
725 && FindProcShort (myGlCore20, glMultiTexCoord1dv)
726 && FindProcShort (myGlCore20, glMultiTexCoord1f)
727 && FindProcShort (myGlCore20, glMultiTexCoord1fv)
728 && FindProcShort (myGlCore20, glMultiTexCoord1i)
729 && FindProcShort (myGlCore20, glMultiTexCoord1iv)
730 && FindProcShort (myGlCore20, glMultiTexCoord1s)
731 && FindProcShort (myGlCore20, glMultiTexCoord1sv)
732 && FindProcShort (myGlCore20, glMultiTexCoord2d)
733 && FindProcShort (myGlCore20, glMultiTexCoord2dv)
734 && FindProcShort (myGlCore20, glMultiTexCoord2f)
735 && FindProcShort (myGlCore20, glMultiTexCoord2fv)
736 && FindProcShort (myGlCore20, glMultiTexCoord2i)
737 && FindProcShort (myGlCore20, glMultiTexCoord2iv)
738 && FindProcShort (myGlCore20, glMultiTexCoord2s)
739 && FindProcShort (myGlCore20, glMultiTexCoord2sv)
740 && FindProcShort (myGlCore20, glMultiTexCoord3d)
741 && FindProcShort (myGlCore20, glMultiTexCoord3dv)
742 && FindProcShort (myGlCore20, glMultiTexCoord3f)
743 && FindProcShort (myGlCore20, glMultiTexCoord3fv)
744 && FindProcShort (myGlCore20, glMultiTexCoord3i)
745 && FindProcShort (myGlCore20, glMultiTexCoord3iv)
746 && FindProcShort (myGlCore20, glMultiTexCoord3s)
747 && FindProcShort (myGlCore20, glMultiTexCoord3sv)
748 && FindProcShort (myGlCore20, glMultiTexCoord4d)
749 && FindProcShort (myGlCore20, glMultiTexCoord4dv)
750 && FindProcShort (myGlCore20, glMultiTexCoord4f)
751 && FindProcShort (myGlCore20, glMultiTexCoord4fv)
752 && FindProcShort (myGlCore20, glMultiTexCoord4i)
753 && FindProcShort (myGlCore20, glMultiTexCoord4iv)
754 && FindProcShort (myGlCore20, glMultiTexCoord4s)
755 && FindProcShort (myGlCore20, glMultiTexCoord4sv)
756 && FindProcShort (myGlCore20, glLoadTransposeMatrixf)
757 && FindProcShort (myGlCore20, glLoadTransposeMatrixd)
758 && FindProcShort (myGlCore20, glMultTransposeMatrixf)
759 && FindProcShort (myGlCore20, glMultTransposeMatrixd);
761 // Check if OpenGL 1.4 core functionality is actually present
762 Standard_Boolean hasGlCore14 = IsGlGreaterEqual (1, 4)
763 && FindProcShort (myGlCore20, glBlendFuncSeparate)
764 && FindProcShort (myGlCore20, glMultiDrawArrays)
765 && FindProcShort (myGlCore20, glMultiDrawElements)
766 && FindProcShort (myGlCore20, glPointParameterf)
767 && FindProcShort (myGlCore20, glPointParameterfv)
768 && FindProcShort (myGlCore20, glPointParameteri)
769 && FindProcShort (myGlCore20, glPointParameteriv);
771 // Check if OpenGL 1.5 core functionality is actually present
772 Standard_Boolean hasGlCore15 = IsGlGreaterEqual (1, 5)
773 && FindProcShort (myGlCore20, glGenQueries)
774 && FindProcShort (myGlCore20, glDeleteQueries)
775 && FindProcShort (myGlCore20, glIsQuery)
776 && FindProcShort (myGlCore20, glBeginQuery)
777 && FindProcShort (myGlCore20, glEndQuery)
778 && FindProcShort (myGlCore20, glGetQueryiv)
779 && FindProcShort (myGlCore20, glGetQueryObjectiv)
780 && FindProcShort (myGlCore20, glGetQueryObjectuiv)
781 && FindProcShort (myGlCore20, glBindBuffer)
782 && FindProcShort (myGlCore20, glDeleteBuffers)
783 && FindProcShort (myGlCore20, glGenBuffers)
784 && FindProcShort (myGlCore20, glIsBuffer)
785 && FindProcShort (myGlCore20, glBufferData)
786 && FindProcShort (myGlCore20, glBufferSubData)
787 && FindProcShort (myGlCore20, glGetBufferSubData)
788 && FindProcShort (myGlCore20, glMapBuffer)
789 && FindProcShort (myGlCore20, glUnmapBuffer)
790 && FindProcShort (myGlCore20, glGetBufferParameteriv)
791 && FindProcShort (myGlCore20, glGetBufferPointerv);
793 // Check if OpenGL 2.0 core functionality is actually present
794 Standard_Boolean hasGlCore20 = IsGlGreaterEqual (2, 0)
795 && FindProcShort (myGlCore20, glBlendEquationSeparate)
796 && FindProcShort (myGlCore20, glDrawBuffers)
797 && FindProcShort (myGlCore20, glStencilOpSeparate)
798 && FindProcShort (myGlCore20, glStencilFuncSeparate)
799 && FindProcShort (myGlCore20, glStencilMaskSeparate)
800 && FindProcShort (myGlCore20, glAttachShader)
801 && FindProcShort (myGlCore20, glBindAttribLocation)
802 && FindProcShort (myGlCore20, glCompileShader)
803 && FindProcShort (myGlCore20, glCreateProgram)
804 && FindProcShort (myGlCore20, glCreateShader)
805 && FindProcShort (myGlCore20, glDeleteProgram)
806 && FindProcShort (myGlCore20, glDeleteShader)
807 && FindProcShort (myGlCore20, glDetachShader)
808 && FindProcShort (myGlCore20, glDisableVertexAttribArray)
809 && FindProcShort (myGlCore20, glEnableVertexAttribArray)
810 && FindProcShort (myGlCore20, glGetActiveAttrib)
811 && FindProcShort (myGlCore20, glGetActiveUniform)
812 && FindProcShort (myGlCore20, glGetAttachedShaders)
813 && FindProcShort (myGlCore20, glGetAttribLocation)
814 && FindProcShort (myGlCore20, glGetProgramiv)
815 && FindProcShort (myGlCore20, glGetProgramInfoLog)
816 && FindProcShort (myGlCore20, glGetShaderiv)
817 && FindProcShort (myGlCore20, glGetShaderInfoLog)
818 && FindProcShort (myGlCore20, glGetShaderSource)
819 && FindProcShort (myGlCore20, glGetUniformLocation)
820 && FindProcShort (myGlCore20, glGetUniformfv)
821 && FindProcShort (myGlCore20, glGetUniformiv)
822 && FindProcShort (myGlCore20, glGetVertexAttribdv)
823 && FindProcShort (myGlCore20, glGetVertexAttribfv)
824 && FindProcShort (myGlCore20, glGetVertexAttribiv)
825 && FindProcShort (myGlCore20, glGetVertexAttribPointerv)
826 && FindProcShort (myGlCore20, glIsProgram)
827 && FindProcShort (myGlCore20, glIsShader)
828 && FindProcShort (myGlCore20, glLinkProgram)
829 && FindProcShort (myGlCore20, glShaderSource)
830 && FindProcShort (myGlCore20, glUseProgram)
831 && FindProcShort (myGlCore20, glUniform1f)
832 && FindProcShort (myGlCore20, glUniform2f)
833 && FindProcShort (myGlCore20, glUniform3f)
834 && FindProcShort (myGlCore20, glUniform4f)
835 && FindProcShort (myGlCore20, glUniform1i)
836 && FindProcShort (myGlCore20, glUniform2i)
837 && FindProcShort (myGlCore20, glUniform3i)
838 && FindProcShort (myGlCore20, glUniform4i)
839 && FindProcShort (myGlCore20, glUniform1fv)
840 && FindProcShort (myGlCore20, glUniform2fv)
841 && FindProcShort (myGlCore20, glUniform3fv)
842 && FindProcShort (myGlCore20, glUniform4fv)
843 && FindProcShort (myGlCore20, glUniform1iv)
844 && FindProcShort (myGlCore20, glUniform2iv)
845 && FindProcShort (myGlCore20, glUniform3iv)
846 && FindProcShort (myGlCore20, glUniform4iv)
847 && FindProcShort (myGlCore20, glUniformMatrix2fv)
848 && FindProcShort (myGlCore20, glUniformMatrix3fv)
849 && FindProcShort (myGlCore20, glUniformMatrix4fv)
850 && FindProcShort (myGlCore20, glValidateProgram)
851 && FindProcShort (myGlCore20, glVertexAttrib1d)
852 && FindProcShort (myGlCore20, glVertexAttrib1dv)
853 && FindProcShort (myGlCore20, glVertexAttrib1f)
854 && FindProcShort (myGlCore20, glVertexAttrib1fv)
855 && FindProcShort (myGlCore20, glVertexAttrib1s)
856 && FindProcShort (myGlCore20, glVertexAttrib1sv)
857 && FindProcShort (myGlCore20, glVertexAttrib2d)
858 && FindProcShort (myGlCore20, glVertexAttrib2dv)
859 && FindProcShort (myGlCore20, glVertexAttrib2f)
860 && FindProcShort (myGlCore20, glVertexAttrib2fv)
861 && FindProcShort (myGlCore20, glVertexAttrib2s)
862 && FindProcShort (myGlCore20, glVertexAttrib2sv)
863 && FindProcShort (myGlCore20, glVertexAttrib3d)
864 && FindProcShort (myGlCore20, glVertexAttrib3dv)
865 && FindProcShort (myGlCore20, glVertexAttrib3f)
866 && FindProcShort (myGlCore20, glVertexAttrib3fv)
867 && FindProcShort (myGlCore20, glVertexAttrib3s)
868 && FindProcShort (myGlCore20, glVertexAttrib3sv)
869 && FindProcShort (myGlCore20, glVertexAttrib4Nbv)
870 && FindProcShort (myGlCore20, glVertexAttrib4Niv)
871 && FindProcShort (myGlCore20, glVertexAttrib4Nsv)
872 && FindProcShort (myGlCore20, glVertexAttrib4Nub)
873 && FindProcShort (myGlCore20, glVertexAttrib4Nubv)
874 && FindProcShort (myGlCore20, glVertexAttrib4Nuiv)
875 && FindProcShort (myGlCore20, glVertexAttrib4Nusv)
876 && FindProcShort (myGlCore20, glVertexAttrib4bv)
877 && FindProcShort (myGlCore20, glVertexAttrib4d)
878 && FindProcShort (myGlCore20, glVertexAttrib4dv)
879 && FindProcShort (myGlCore20, glVertexAttrib4f)
880 && FindProcShort (myGlCore20, glVertexAttrib4fv)
881 && FindProcShort (myGlCore20, glVertexAttrib4iv)
882 && FindProcShort (myGlCore20, glVertexAttrib4s)
883 && FindProcShort (myGlCore20, glVertexAttrib4sv)
884 && FindProcShort (myGlCore20, glVertexAttrib4ubv)
885 && FindProcShort (myGlCore20, glVertexAttrib4uiv)
886 && FindProcShort (myGlCore20, glVertexAttrib4usv)
887 && FindProcShort (myGlCore20, glVertexAttribPointer);
939 // =======================================================================
940 // function : IsFeedback
942 // =======================================================================
943 Standard_Boolean OpenGl_Context::IsFeedback() const
948 // =======================================================================
949 // function : SetFeedback
951 // =======================================================================
952 void OpenGl_Context::SetFeedback (const Standard_Boolean theFeedbackOn)
954 myIsFeedback = theFeedbackOn;
957 // =======================================================================
958 // function : MemoryInfo
960 // =======================================================================
961 Standard_Size OpenGl_Context::AvailableMemory() const
965 // this is actually information for VBO pool
966 // however because pools are mostly shared
967 // it can be used for total GPU memory estimations
970 glGetIntegerv (GL_VBO_FREE_MEMORY_ATI, aMemInfo);
971 // returned value is in KiB, however this maybe changed in future
972 return Standard_Size(aMemInfo[0]) * 1024;
976 // current available dedicated video memory (in KiB), currently unused GPU memory
978 glGetIntegerv (GL_GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX, &aMemInfo);
979 return Standard_Size(aMemInfo) * 1024;
984 // =======================================================================
985 // function : MemoryInfo
987 // =======================================================================
988 TCollection_AsciiString OpenGl_Context::MemoryInfo() const
990 TCollection_AsciiString anInfo;
994 memset (aValues, 0, sizeof(aValues));
995 glGetIntegerv (GL_VBO_FREE_MEMORY_ATI, aValues);
997 // total memory free in the pool
998 anInfo += TCollection_AsciiString (" GPU free memory: ") + (aValues[0] / 1024) + " MiB\n";
1000 // largest available free block in the pool
1001 anInfo += TCollection_AsciiString (" Largest free block: ") + (aValues[1] / 1024) + " MiB\n";
1002 if (aValues[2] != aValues[0])
1004 // total auxiliary memory free
1005 anInfo += TCollection_AsciiString (" Free memory: ") + (aValues[2] / 1024) + " MiB\n";
1010 //current available dedicated video memory (in KiB), currently unused GPU memory
1012 glGetIntegerv (GL_GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX, &aValue);
1013 anInfo += TCollection_AsciiString (" GPU free memory: ") + (aValue / 1024) + " MiB\n";
1015 // dedicated video memory, total size (in KiB) of the GPU memory
1016 GLint aDedicated = 0;
1017 glGetIntegerv (GL_GPU_MEMORY_INFO_DEDICATED_VIDMEM_NVX, &aDedicated);
1018 anInfo += TCollection_AsciiString (" GPU memory: ") + (aDedicated / 1024) + " MiB\n";
1020 // total available memory, total size (in KiB) of the memory available for allocations
1021 glGetIntegerv (GL_GPU_MEMORY_INFO_TOTAL_AVAILABLE_MEMORY_NVX, &aValue);
1022 if (aValue != aDedicated)
1024 // different only for special configurations
1025 anInfo += TCollection_AsciiString (" Total memory: ") + (aValue / 1024) + " MiB\n";
1032 // =======================================================================
1033 // function : GetResource
1035 // =======================================================================
1036 const Handle(OpenGl_Resource)& OpenGl_Context::GetResource (const TCollection_AsciiString& theKey) const
1038 return mySharedResources->IsBound (theKey) ? mySharedResources->Find (theKey) : NULL_GL_RESOURCE;
1041 // =======================================================================
1042 // function : ShareResource
1044 // =======================================================================
1045 Standard_Boolean OpenGl_Context::ShareResource (const TCollection_AsciiString& theKey,
1046 const Handle(OpenGl_Resource)& theResource)
1048 if (theKey.IsEmpty() || theResource.IsNull())
1050 return Standard_False;
1052 return mySharedResources->Bind (theKey, theResource);
1055 // =======================================================================
1056 // function : ReleaseResource
1058 // =======================================================================
1059 void OpenGl_Context::ReleaseResource (const TCollection_AsciiString& theKey,
1060 const Standard_Boolean theToDelay)
1062 if (!mySharedResources->IsBound (theKey))
1066 const Handle(OpenGl_Resource)& aRes = mySharedResources->Find (theKey);
1067 if (aRes->GetRefCount() > 1)
1074 myDelayed->Bind (theKey, 1);
1078 aRes->Release (this);
1079 mySharedResources->UnBind (theKey);
1083 // =======================================================================
1084 // function : DelayedRelease
1086 // =======================================================================
1087 void OpenGl_Context::DelayedRelease (Handle(OpenGl_Resource)& theResource)
1089 myReleaseQueue->Push (theResource);
1090 theResource.Nullify();
1093 // =======================================================================
1094 // function : ReleaseDelayed
1096 // =======================================================================
1097 void OpenGl_Context::ReleaseDelayed()
1099 // release queued elements
1100 while (!myReleaseQueue->IsEmpty())
1102 myReleaseQueue->Front()->Release (this);
1103 myReleaseQueue->Pop();
1106 // release delayed shared resoruces
1107 NCollection_Vector<TCollection_AsciiString> aDeadList;
1108 for (NCollection_DataMap<TCollection_AsciiString, Standard_Integer>::Iterator anIter (*myDelayed);
1109 anIter.More(); anIter.Next())
1111 if (++anIter.ChangeValue() <= 2)
1113 continue; // postpone release one more frame to ensure noone use it periodically
1116 const TCollection_AsciiString& aKey = anIter.Key();
1117 if (!mySharedResources->IsBound (aKey))
1119 // mixed unshared strategy delayed/undelayed was used!
1120 aDeadList.Append (aKey);
1124 Handle(OpenGl_Resource)& aRes = mySharedResources->ChangeFind (aKey);
1125 if (aRes->GetRefCount() > 1)
1127 // should be only 1 instance in mySharedResources
1128 // if not - resource was reused again
1129 aDeadList.Append (aKey);
1133 // release resource if no one requiested it more than 2 redraw calls
1134 aRes->Release (this);
1135 mySharedResources->UnBind (aKey);
1136 aDeadList.Append (aKey);
1139 for (Standard_Integer anIter = 0; anIter < aDeadList.Length(); ++anIter)
1141 myDelayed->UnBind (aDeadList.Value (anIter));