0025147: Visualization, TKOpenGl - support EGL as alternative to GLX
[occt.git] / src / OpenGl / OpenGl_Context.cxx
CommitLineData
6aca4d39 1// Created on: 2012-01-26
b311480e 2// Created by: Kirill GAVRILOV
6aca4d39 3// Copyright (c) 2012-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 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
973c2be1 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
b311480e 15
58655684 16#if defined(_WIN32)
5f8b738e 17 #include <windows.h>
18#endif
19
2166f0fa
SK
20#include <OpenGl_Context.hxx>
21
5e27df78 22#include <OpenGl_ArbTBO.hxx>
23#include <OpenGl_ArbIns.hxx>
58655684 24#include <OpenGl_ArbDbg.hxx>
01ca42b2 25#include <OpenGl_ArbFBO.hxx>
37eb4787 26#include <OpenGl_ExtGS.hxx>
5f8b738e 27#include <OpenGl_GlCore20.hxx>
30f0ad28 28#include <OpenGl_ShaderManager.hxx>
5f8b738e 29
cbf18624 30#include <Message_Messenger.hxx>
30f0ad28 31
a174a3c5 32#include <NCollection_Vector.hxx>
33
2bd4c032 34#include <Standard_ProgramError.hxx>
35
da8bb41d 36#if defined(HAVE_EGL)
37 #include <EGL/egl.h>
38 #ifdef _MSC_VER
39 #pragma comment(lib, "libEGL.lib")
40 #endif
41#elif defined(_WIN32)
5f8b738e 42 //
43#elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
44 #include <dlfcn.h>
45#else
46 #include <GL/glx.h> // glXGetProcAddress()
47#endif
48
f0430952 49// GL_NVX_gpu_memory_info
50#ifndef GL_GPU_MEMORY_INFO_DEDICATED_VIDMEM_NVX
51 enum
52 {
53 GL_GPU_MEMORY_INFO_DEDICATED_VIDMEM_NVX = 0x9047,
54 GL_GPU_MEMORY_INFO_TOTAL_AVAILABLE_MEMORY_NVX = 0x9048,
55 GL_GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX = 0x9049,
56 GL_GPU_MEMORY_INFO_EVICTION_COUNT_NVX = 0x904A,
57 GL_GPU_MEMORY_INFO_EVICTED_MEMORY_NVX = 0x904B
58 };
59#endif
60
2166f0fa
SK
61IMPLEMENT_STANDARD_HANDLE (OpenGl_Context, Standard_Transient)
62IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Context, Standard_Transient)
63
5e27df78 64namespace
65{
66 static const Handle(OpenGl_Resource) NULL_GL_RESOURCE;
01ca42b2 67}
5e27df78 68
2166f0fa
SK
69// =======================================================================
70// function : OpenGl_Context
71// purpose :
72// =======================================================================
58655684 73OpenGl_Context::OpenGl_Context (const Handle(OpenGl_Caps)& theCaps)
01ca42b2 74: core11 (NULL),
75 core11fwd (NULL),
76 core15 (NULL),
77 core15fwd (NULL),
78 core20 (NULL),
79 core20fwd (NULL),
80 core32 (NULL),
81 core32back (NULL),
82 core41 (NULL),
83 core41back (NULL),
84 core42 (NULL),
85 core42back (NULL),
86 core43 (NULL),
87 core43back (NULL),
88 core44 (NULL),
89 core44back (NULL),
58655684 90 caps (!theCaps.IsNull() ? theCaps : new OpenGl_Caps()),
ca3c13d1 91#if defined(GL_ES_VERSION_2_0)
92 hasHighp (Standard_False),
93 hasTexRGBA8(Standard_False),
94#else
95 hasHighp (Standard_True),
96 hasTexRGBA8(Standard_True),
97#endif
bf75be98 98 arbNPTW(Standard_False),
5e27df78 99 arbTBO (NULL),
100 arbIns (NULL),
58655684 101 arbDbg (NULL),
01ca42b2 102 arbFBO (NULL),
37eb4787 103 extGS (NULL),
bf75be98 104 extBgra(Standard_False),
105 extAnis(Standard_False),
30f0ad28 106 extPDS (Standard_False),
f0430952 107 atiMem (Standard_False),
108 nvxMem (Standard_False),
5e27df78 109 mySharedResources (new OpenGl_ResourcesMap()),
a174a3c5 110 myDelayed (new OpenGl_DelayReleaseMap()),
3125ebb6 111 myUnusedResources (new OpenGl_ResourcesStack()),
4269bd1b 112 myClippingState (),
5f8b738e 113 myGlLibHandle (NULL),
01ca42b2 114 myFuncs (new OpenGl_GlFunctions()),
2f6cb3ac 115 myAnisoMax (1),
ca3c13d1 116 myTexClamp (GL_CLAMP_TO_EDGE),
eafb234b 117 myMaxTexDim (1024),
4269bd1b 118 myMaxClipPlanes (6),
5f8b738e 119 myGlVerMajor (0),
120 myGlVerMinor (0),
b5ac8292 121 myIsInitialized (Standard_False),
122 myIsStereoBuffers (Standard_False),
ca3c13d1 123#if !defined(GL_ES_VERSION_2_0)
7d3e64ef 124 myRenderMode (GL_RENDER),
ca3c13d1 125#else
126 myRenderMode (0),
127#endif
b5ac8292 128 myDrawBuffer (0)
2166f0fa 129{
5f8b738e 130#if defined(MAC_OS_X_VERSION_10_3) && !defined(MACOSX_USE_GLX)
131 // Vendors can not extend functionality on this system
132 // and developers are limited to OpenGL support provided by Mac OS X SDK.
133 // We retrieve function pointers from system library
134 // to generalize extensions support on all platforms.
135 // In this way we also reach binary compatibility benefit between OS releases
136 // if some newest functionality is optionally used.
137 // Notice that GL version / extension availability checks are required
138 // because function pointers may be available but not functionality itself
139 // (depends on renderer).
140 myGlLibHandle = dlopen ("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL", RTLD_LAZY);
141#endif
01ca42b2 142 memset (myFuncs.operator->(), 0, sizeof(OpenGl_GlFunctions));
30f0ad28 143 myShaderManager = new OpenGl_ShaderManager (this);
2166f0fa
SK
144}
145
146// =======================================================================
147// function : ~OpenGl_Context
148// purpose :
149// =======================================================================
150OpenGl_Context::~OpenGl_Context()
151{
5e27df78 152 // release clean up queue
153 ReleaseDelayed();
154
155 // release shared resources if any
156 if (((const Handle(Standard_Transient)& )mySharedResources)->GetRefCount() <= 1)
157 {
392ac980 158 myShaderManager.Nullify();
5e27df78 159 for (NCollection_DataMap<TCollection_AsciiString, Handle(OpenGl_Resource)>::Iterator anIter (*mySharedResources);
160 anIter.More(); anIter.Next())
161 {
162 anIter.Value()->Release (this);
163 }
164 }
392ac980 165 else
166 {
167 myShaderManager->SetContext (NULL);
168 }
5e27df78 169 mySharedResources.Nullify();
a174a3c5 170 myDelayed.Nullify();
5e27df78 171
ca3c13d1 172#if !defined(GL_ES_VERSION_2_0)
cbf18624 173 if (arbDbg != NULL
f8c8ba7a 174 && caps->contextDebug
175 && IsValid())
cbf18624 176 {
177 // reset callback
178 void* aPtr = NULL;
179 glGetPointerv (GL_DEBUG_CALLBACK_USER_PARAM_ARB, &aPtr);
180 if (aPtr == this)
181 {
182 arbDbg->glDebugMessageCallbackARB (NULL, NULL);
183 }
184 }
ca3c13d1 185#endif
5f8b738e 186}
187
188// =======================================================================
bf75be98 189// function : MaxDegreeOfAnisotropy
190// purpose :
191// =======================================================================
192Standard_Integer OpenGl_Context::MaxDegreeOfAnisotropy() const
193{
194 return myAnisoMax;
195}
196
197// =======================================================================
198// function : MaxTextureSize
199// purpose :
200// =======================================================================
201Standard_Integer OpenGl_Context::MaxTextureSize() const
202{
203 return myMaxTexDim;
204}
205
206// =======================================================================
4269bd1b 207// function : MaxClipPlanes
208// purpose :
209// =======================================================================
210Standard_Integer OpenGl_Context::MaxClipPlanes() const
211{
212 return myMaxClipPlanes;
213}
214
215// =======================================================================
b5ac8292 216// function : SetDrawBufferLeft
217// purpose :
218// =======================================================================
219void OpenGl_Context::SetDrawBufferLeft()
220{
ca3c13d1 221#if !defined(GL_ES_VERSION_2_0)
b5ac8292 222 switch (myDrawBuffer)
223 {
224 case GL_BACK_RIGHT :
225 case GL_BACK :
226 glDrawBuffer (GL_BACK_LEFT);
227 myDrawBuffer = GL_BACK_LEFT;
228 break;
229
230 case GL_FRONT_RIGHT :
231 case GL_FRONT :
232 glDrawBuffer (GL_FRONT_LEFT);
233 myDrawBuffer = GL_FRONT_LEFT;
234 break;
235
236 case GL_FRONT_AND_BACK :
237 case GL_RIGHT :
238 glDrawBuffer (GL_LEFT);
239 myDrawBuffer = GL_LEFT;
240 break;
241 }
ca3c13d1 242#endif
b5ac8292 243}
244
245// =======================================================================
246// function : SetDrawBufferRight
247// purpose :
248// =======================================================================
249void OpenGl_Context::SetDrawBufferRight()
250{
ca3c13d1 251#if !defined(GL_ES_VERSION_2_0)
b5ac8292 252 switch (myDrawBuffer)
253 {
254 case GL_BACK_LEFT :
255 case GL_BACK :
256 glDrawBuffer (GL_BACK_RIGHT);
257 myDrawBuffer = GL_BACK_RIGHT;
258 break;
259
260 case GL_FRONT_LEFT :
261 case GL_FRONT :
262 glDrawBuffer (GL_FRONT_RIGHT);
263 myDrawBuffer = GL_FRONT_RIGHT;
264 break;
265
266 case GL_FRONT_AND_BACK :
267 case GL_LEFT :
268 glDrawBuffer (GL_RIGHT);
269 myDrawBuffer = GL_RIGHT;
270 break;
271 }
ca3c13d1 272#endif
b5ac8292 273}
274
275// =======================================================================
276// function : SetDrawBufferMono
277// purpose :
278// =======================================================================
279void OpenGl_Context::SetDrawBufferMono()
280{
ca3c13d1 281#if !defined(GL_ES_VERSION_2_0)
b5ac8292 282 switch (myDrawBuffer)
283 {
284 case GL_BACK_LEFT :
285 case GL_BACK_RIGHT :
286 glDrawBuffer (GL_BACK);
287 myDrawBuffer = GL_BACK;
288 break;
289
290 case GL_FRONT_LEFT :
291 case GL_FRONT_RIGHT :
292 glDrawBuffer (GL_FRONT);
293 myDrawBuffer = GL_FRONT;
294 break;
295
296 case GL_LEFT :
297 case GL_RIGHT :
298 glDrawBuffer (GL_FRONT_AND_BACK);
299 myDrawBuffer = GL_FRONT_AND_BACK;
300 break;
301 }
ca3c13d1 302#endif
b5ac8292 303}
304
305// =======================================================================
306// function : FetchState
307// purpose :
308// =======================================================================
309void OpenGl_Context::FetchState()
310{
ca3c13d1 311#if !defined(GL_ES_VERSION_2_0)
b5ac8292 312 // cache feedback mode state
313 glGetIntegerv (GL_RENDER_MODE, &myRenderMode);
314
315 // cache draw buffer state
316 glGetIntegerv (GL_DRAW_BUFFER, &myDrawBuffer);
ca3c13d1 317#endif
b5ac8292 318}
319
320// =======================================================================
5e27df78 321// function : Share
322// purpose :
323// =======================================================================
324void OpenGl_Context::Share (const Handle(OpenGl_Context)& theShareCtx)
325{
326 if (!theShareCtx.IsNull())
327 {
328 mySharedResources = theShareCtx->mySharedResources;
a174a3c5 329 myDelayed = theShareCtx->myDelayed;
3125ebb6 330 myUnusedResources = theShareCtx->myUnusedResources;
392ac980 331 myShaderManager = theShareCtx->myShaderManager;
5e27df78 332 }
333}
334
4fe56619 335#if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
336
5e27df78 337// =======================================================================
86fa64d9 338// function : IsCurrent
339// purpose :
340// =======================================================================
341Standard_Boolean OpenGl_Context::IsCurrent() const
342{
da8bb41d 343#if defined(HAVE_EGL)
344 if ((EGLDisplay )myDisplay == EGL_NO_DISPLAY
345 || (EGLSurface )myWindow == EGL_NO_SURFACE
346 || (EGLContext )myGContext == EGL_NO_CONTEXT)
347 {
348 return Standard_False;
349 }
350
351 return (((EGLDisplay )myDisplay == eglGetCurrentDisplay())
352 && ((EGLContext )myGContext == eglGetCurrentContext())
353 && ((EGLSurface )myWindow == eglGetCurrentSurface (EGL_DRAW)));
354#elif defined(_WIN32)
86fa64d9 355 if (myWindowDC == NULL || myGContext == NULL)
356 {
357 return Standard_False;
358 }
359 return (( (HDC )myWindowDC == wglGetCurrentDC())
360 && ((HGLRC )myGContext == wglGetCurrentContext()));
361#else
362 if (myDisplay == NULL || myWindow == 0 || myGContext == 0)
363 {
364 return Standard_False;
365 }
366
367 return ( ((Display* )myDisplay == glXGetCurrentDisplay())
368 && ((GLXContext )myGContext == glXGetCurrentContext())
369 && ((GLXDrawable )myWindow == glXGetCurrentDrawable()));
370#endif
371}
372
373// =======================================================================
2bd4c032 374// function : MakeCurrent
375// purpose :
376// =======================================================================
377Standard_Boolean OpenGl_Context::MakeCurrent()
378{
da8bb41d 379#if defined(HAVE_EGL)
380 if ((EGLDisplay )myDisplay == EGL_NO_DISPLAY
381 || (EGLSurface )myWindow == EGL_NO_SURFACE
382 || (EGLContext )myGContext == EGL_NO_CONTEXT)
383 {
384 Standard_ProgramError_Raise_if (myIsInitialized, "OpenGl_Context::Init() should be called before!");
385 return Standard_False;
386 }
387
388 if (eglMakeCurrent ((EGLDisplay )myDisplay, (EGLSurface )myWindow, (EGLSurface )myWindow, (EGLContext )myGContext) != EGL_TRUE)
389 {
390 // if there is no current context it might be impossible to use glGetError() correctly
391 PushMessage (GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB, GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB,
392 "eglMakeCurrent() has failed!");
393 myIsInitialized = Standard_False;
394 return Standard_False;
395 }
396#elif defined(_WIN32)
86fa64d9 397 if (myWindowDC == NULL || myGContext == NULL)
2bd4c032 398 {
86fa64d9 399 Standard_ProgramError_Raise_if (myIsInitialized, "OpenGl_Context::Init() should be called before!");
400 return Standard_False;
401 }
402
403 // technically it should be safe to activate already bound GL context
404 // however some drivers (Intel etc.) may FAIL doing this for unknown reason
405 if (IsCurrent())
406 {
392ac980 407 myShaderManager->SetContext (this);
86fa64d9 408 return Standard_True;
409 }
410 else if (wglMakeCurrent ((HDC )myWindowDC, (HGLRC )myGContext) != TRUE)
411 {
412 // notice that glGetError() couldn't be used here!
413 wchar_t* aMsgBuff = NULL;
414 DWORD anErrorCode = GetLastError();
415 FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
416 NULL, anErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (wchar_t* )&aMsgBuff, 0, NULL);
cbf18624 417 TCollection_ExtendedString aMsg ("wglMakeCurrent() has failed. ");
86fa64d9 418 if (aMsgBuff != NULL)
419 {
cbf18624 420 aMsg += (Standard_ExtString )aMsgBuff;
86fa64d9 421 LocalFree (aMsgBuff);
422 }
cbf18624 423 PushMessage (GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB, GL_DEBUG_TYPE_ERROR_ARB, (unsigned int )anErrorCode, GL_DEBUG_SEVERITY_HIGH_ARB, aMsg);
fd4a6963 424 myIsInitialized = Standard_False;
2bd4c032 425 return Standard_False;
426 }
427#else
86fa64d9 428 if (myDisplay == NULL || myWindow == 0 || myGContext == 0)
429 {
430 Standard_ProgramError_Raise_if (myIsInitialized, "OpenGl_Context::Init() should be called before!");
431 return Standard_False;
432 }
433
434 if (!glXMakeCurrent ((Display* )myDisplay, (GLXDrawable )myWindow, (GLXContext )myGContext))
2bd4c032 435 {
436 // if there is no current context it might be impossible to use glGetError() correctly
cbf18624 437 PushMessage (GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB, GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB,
438 "glXMakeCurrent() has failed!");
fd4a6963 439 myIsInitialized = Standard_False;
2bd4c032 440 return Standard_False;
441 }
442#endif
392ac980 443 myShaderManager->SetContext (this);
2bd4c032 444 return Standard_True;
445}
446
447// =======================================================================
5e27df78 448// function : SwapBuffers
449// purpose :
450// =======================================================================
451void OpenGl_Context::SwapBuffers()
452{
da8bb41d 453#if defined(HAVE_EGL)
454 if ((EGLSurface )myWindow != EGL_NO_SURFACE)
455 {
456 eglSwapBuffers ((EGLDisplay )myDisplay, (EGLSurface )myWindow);
457 }
458#elif defined(_WIN32)
5e27df78 459 if ((HDC )myWindowDC != NULL)
460 {
461 ::SwapBuffers ((HDC )myWindowDC);
462 glFlush();
463 }
464#else
465 if ((Display* )myDisplay != NULL)
466 {
467 glXSwapBuffers ((Display* )myDisplay, (GLXDrawable )myWindow);
468 }
469#endif
470}
471
4fe56619 472#endif // __APPLE__
473
5e27df78 474// =======================================================================
5f8b738e 475// function : findProc
476// purpose :
477// =======================================================================
478void* OpenGl_Context::findProc (const char* theFuncName)
479{
da8bb41d 480#if defined(HAVE_EGL)
481 return (void* )eglGetProcAddress (theFuncName);
482#elif defined(_WIN32)
5f8b738e 483 return wglGetProcAddress (theFuncName);
484#elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
485 return (myGlLibHandle != NULL) ? dlsym (myGlLibHandle, theFuncName) : NULL;
486#else
487 return (void* )glXGetProcAddress ((const GLubyte* )theFuncName);
488#endif
2166f0fa
SK
489}
490
491// =======================================================================
492// function : CheckExtension
493// purpose :
494// =======================================================================
2bd4c032 495Standard_Boolean OpenGl_Context::CheckExtension (const char* theExtName) const
2166f0fa 496{
5f8b738e 497 if (theExtName == NULL)
498 {
499 std::cerr << "CheckExtension called with NULL string!\n";
500 return Standard_False;
501 }
5f8b738e 502
503 // available since OpenGL 3.0
504 // and the ONLY way to check extensions with OpenGL 3.1+ core profile
2bd4c032 505 /**if (IsGlGreaterEqual (3, 0))
5f8b738e 506 {
507 GLint anExtNb = 0;
508 glGetIntegerv (GL_NUM_EXTENSIONS, &anExtNb);
509 for (GLint anIter = 0; anIter < anExtNb; ++anIter)
510 {
511 const char* anExtension = (const char* )core30->glGetStringi (GL_EXTENSIONS, (GLuint )anIter);
512 if (anExtension[anExtNameLen] == '\0' &&
513 strncmp (anExtension, theExtName, anExtNameLen) == 0)
514 {
515 return Standard_True;
516 }
517 }
518 return Standard_False;
519 }*/
520
521 // use old way with huge string for all extensions
522 const char* anExtString = (const char* )glGetString (GL_EXTENSIONS);
523 if (anExtString == NULL)
524 {
7e7c2f0b 525 Messenger()->Send ("TKOpenGL: glGetString (GL_EXTENSIONS) has returned NULL! No GL context?", Message_Warning);
2166f0fa
SK
526 return Standard_False;
527 }
58655684 528 return CheckExtension (anExtString, theExtName);
529}
530
531// =======================================================================
532// function : CheckExtension
533// purpose :
534// =======================================================================
535Standard_Boolean OpenGl_Context::CheckExtension (const char* theExtString,
536 const char* theExtName)
537{
538 if (theExtString == NULL)
539 {
540 return Standard_False;
541 }
2166f0fa
SK
542
543 // Search for theExtName in the extensions string.
544 // Use of strstr() is not sufficient because extension names can be prefixes of other extension names.
58655684 545 char* aPtrIter = (char* )theExtString;
546 const char* aPtrEnd = aPtrIter + strlen (theExtString);
547 const size_t anExtNameLen = strlen (theExtName);
2166f0fa
SK
548 while (aPtrIter < aPtrEnd)
549 {
6a7d83c4 550 const size_t n = strcspn (aPtrIter, " ");
5f8b738e 551 if ((n == anExtNameLen) && (strncmp (aPtrIter, theExtName, anExtNameLen) == 0))
552 {
2166f0fa 553 return Standard_True;
5f8b738e 554 }
2166f0fa
SK
555 aPtrIter += (n + 1);
556 }
557 return Standard_False;
558}
559
4fe56619 560#if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
561
2166f0fa
SK
562// =======================================================================
563// function : Init
564// purpose :
565// =======================================================================
f0430952 566Standard_Boolean OpenGl_Context::Init()
2166f0fa 567{
2bd4c032 568 if (myIsInitialized)
5f8b738e 569 {
f0430952 570 return Standard_True;
5f8b738e 571 }
2bd4c032 572
da8bb41d 573#if defined(HAVE_EGL)
574 myDisplay = (Aspect_Display )eglGetCurrentDisplay();
575 myGContext = (Aspect_RenderingContext )eglGetCurrentContext();
576 myWindow = (Aspect_Drawable )eglGetCurrentSurface(EGL_DRAW);
577#elif defined(_WIN32)
2bd4c032 578 myWindowDC = (Aspect_Handle )wglGetCurrentDC();
579 myGContext = (Aspect_RenderingContext )wglGetCurrentContext();
580#else
581 myDisplay = (Aspect_Display )glXGetCurrentDisplay();
582 myGContext = (Aspect_RenderingContext )glXGetCurrentContext();
583 myWindow = (Aspect_Drawable )glXGetCurrentDrawable();
584#endif
f0430952 585 if (myGContext == NULL)
586 {
587 return Standard_False;
588 }
2bd4c032 589
590 init();
591 myIsInitialized = Standard_True;
f0430952 592 return Standard_True;
2bd4c032 593}
594
4fe56619 595#endif // __APPLE__
596
2bd4c032 597// =======================================================================
598// function : Init
599// purpose :
600// =======================================================================
da8bb41d 601#if defined(HAVE_EGL)
602Standard_Boolean OpenGl_Context::Init (const Aspect_Drawable theEglSurface,
603 const Aspect_Display theEglDisplay,
604 const Aspect_RenderingContext theEglContext)
605#elif defined(_WIN32)
f0430952 606Standard_Boolean OpenGl_Context::Init (const Aspect_Handle theWindow,
607 const Aspect_Handle theWindowDC,
608 const Aspect_RenderingContext theGContext)
4fe56619 609#elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
610Standard_Boolean OpenGl_Context::Init (const void* theGContext)
2bd4c032 611#else
f0430952 612Standard_Boolean OpenGl_Context::Init (const Aspect_Drawable theWindow,
613 const Aspect_Display theDisplay,
614 const Aspect_RenderingContext theGContext)
2bd4c032 615#endif
616{
617 Standard_ProgramError_Raise_if (myIsInitialized, "OpenGl_Context::Init() should be called only once!");
da8bb41d 618#if defined(HAVE_EGL)
619 myWindow = theEglSurface;
620 myGContext = theEglContext;
621 myDisplay = theEglDisplay;
622#elif defined(_WIN32)
2bd4c032 623 myWindow = theWindow;
624 myGContext = theGContext;
2bd4c032 625 myWindowDC = theWindowDC;
4fe56619 626#elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
627 myGContext = (void* )theGContext;
2bd4c032 628#else
4fe56619 629 myWindow = theWindow;
630 myGContext = theGContext;
2bd4c032 631 myDisplay = theDisplay;
632#endif
86fa64d9 633 if (myGContext == NULL || !MakeCurrent())
f0430952 634 {
635 return Standard_False;
636 }
2bd4c032 637
638 init();
639 myIsInitialized = Standard_True;
f0430952 640 return Standard_True;
5f8b738e 641}
642
643// =======================================================================
644// function : ResetErrors
645// purpose :
646// =======================================================================
647void OpenGl_Context::ResetErrors()
648{
649 while (glGetError() != GL_NO_ERROR)
650 {
651 //
652 }
653}
654
655// =======================================================================
656// function : readGlVersion
657// purpose :
658// =======================================================================
659void OpenGl_Context::readGlVersion()
660{
661 // reset values
662 myGlVerMajor = 0;
663 myGlVerMinor = 0;
664
ca3c13d1 665#ifdef GL_MAJOR_VERSION
666 // available since OpenGL 3.0 and OpenGL 3.0 ES
86325709 667 GLint aMajor = 0, aMinor = 0;
5f8b738e 668 glGetIntegerv (GL_MAJOR_VERSION, &aMajor);
669 glGetIntegerv (GL_MINOR_VERSION, &aMinor);
30f0ad28 670 // glGetError() sometimes does not report an error here even if
86325709 671 // GL does not know GL_MAJOR_VERSION and GL_MINOR_VERSION constants.
ca3c13d1 672 // This happens on some renderers like e.g. Cygwin MESA.
86325709 673 // Thus checking additionally if GL has put anything to
674 // the output variables.
675 if (glGetError() == GL_NO_ERROR && aMajor != 0 && aMinor != 0)
5f8b738e 676 {
677 myGlVerMajor = aMajor;
678 myGlVerMinor = aMinor;
679 return;
680 }
681 ResetErrors();
ca3c13d1 682#endif
5f8b738e 683
684 // Read version string.
ca3c13d1 685 // Notice that only first two numbers split by point '2.1 XXXXX' are significant.
5f8b738e 686 // Following trash (after space) is vendor-specific.
687 // New drivers also returns micro version of GL like '3.3.0' which has no meaning
688 // and should be considered as vendor-specific too.
689 const char* aVerStr = (const char* )glGetString (GL_VERSION);
690 if (aVerStr == NULL || *aVerStr == '\0')
691 {
692 // invalid GL context
693 return;
694 }
695
ca3c13d1 696//#if defined(GL_ES_VERSION_2_0)
697 // skip "OpenGL ES-** " section
698 for (; *aVerStr != '\0'; ++aVerStr)
699 {
700 if (*aVerStr >= '0' && *aVerStr <= '9')
701 {
702 break;
703 }
704 }
705//#endif
706
5f8b738e 707 // parse string for major number
708 char aMajorStr[32];
709 char aMinorStr[32];
710 size_t aMajIter = 0;
711 while (aVerStr[aMajIter] >= '0' && aVerStr[aMajIter] <= '9')
712 {
713 ++aMajIter;
714 }
715 if (aMajIter == 0 || aMajIter >= sizeof(aMajorStr))
716 {
717 return;
718 }
719 memcpy (aMajorStr, aVerStr, aMajIter);
720 aMajorStr[aMajIter] = '\0';
721
722 // parse string for minor number
86325709 723 aVerStr += aMajIter + 1;
724 size_t aMinIter = 0;
5f8b738e 725 while (aVerStr[aMinIter] >= '0' && aVerStr[aMinIter] <= '9')
726 {
727 ++aMinIter;
728 }
86325709 729 if (aMinIter == 0 || aMinIter >= sizeof(aMinorStr))
5f8b738e 730 {
731 return;
732 }
86325709 733 memcpy (aMinorStr, aVerStr, aMinIter);
734 aMinorStr[aMinIter] = '\0';
5f8b738e 735
736 // read numbers
737 myGlVerMajor = atoi (aMajorStr);
738 myGlVerMinor = atoi (aMinorStr);
739
740 if (myGlVerMajor <= 0)
741 {
742 myGlVerMajor = 0;
743 myGlVerMinor = 0;
744 }
745}
746
58655684 747static Standard_CString THE_DBGMSG_UNKNOWN = "UNKNOWN";
748static Standard_CString THE_DBGMSG_SOURCES[] =
749{
cbf18624 750 ".OpenGL", // GL_DEBUG_SOURCE_API_ARB
751 ".WinSystem", // GL_DEBUG_SOURCE_WINDOW_SYSTEM_ARB
752 ".GLSL", // GL_DEBUG_SOURCE_SHADER_COMPILER_ARB
753 ".3rdParty", // GL_DEBUG_SOURCE_THIRD_PARTY_ARB
754 "", // GL_DEBUG_SOURCE_APPLICATION_ARB
755 ".Other" // GL_DEBUG_SOURCE_OTHER_ARB
58655684 756};
757
758static Standard_CString THE_DBGMSG_TYPES[] =
759{
cbf18624 760 "Error", // GL_DEBUG_TYPE_ERROR_ARB
761 "Deprecated", // GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB
762 "Undef. behavior", // GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB
763 "Portability", // GL_DEBUG_TYPE_PORTABILITY_ARB
764 "Performance", // GL_DEBUG_TYPE_PERFORMANCE_ARB
765 "Other" // GL_DEBUG_TYPE_OTHER_ARB
58655684 766};
767
768static Standard_CString THE_DBGMSG_SEV_HIGH = "High"; // GL_DEBUG_SEVERITY_HIGH_ARB
769static Standard_CString THE_DBGMSG_SEV_MEDIUM = "Medium"; // GL_DEBUG_SEVERITY_MEDIUM_ARB
770static Standard_CString THE_DBGMSG_SEV_LOW = "Low"; // GL_DEBUG_SEVERITY_LOW_ARB
771
ca3c13d1 772#if !defined(GL_ES_VERSION_2_0)
cbf18624 773//! Callback for GL_ARB_debug_output extension
58655684 774static void APIENTRY debugCallbackWrap(unsigned int theSource,
775 unsigned int theType,
776 unsigned int theId,
777 unsigned int theSeverity,
778 int /*theLength*/,
779 const char* theMessage,
9293178b 780 const void* theUserParam)
cbf18624 781{
782 OpenGl_Context* aCtx = (OpenGl_Context* )theUserParam;
783 aCtx->PushMessage (theSource, theType, theId, theSeverity, theMessage);
784}
ca3c13d1 785#endif
cbf18624 786
787// =======================================================================
788// function : PushMessage
789// purpose :
790// =======================================================================
791void OpenGl_Context::PushMessage (const unsigned int theSource,
792 const unsigned int theType,
793 const unsigned int theId,
794 const unsigned int theSeverity,
795 const TCollection_ExtendedString& theMessage)
58655684 796{
797 //OpenGl_Context* aCtx = (OpenGl_Context* )theUserParam;
798 Standard_CString& aSrc = (theSource >= GL_DEBUG_SOURCE_API_ARB
01ca42b2 799 && theSource <= GL_DEBUG_SOURCE_OTHER_ARB)
58655684 800 ? THE_DBGMSG_SOURCES[theSource - GL_DEBUG_SOURCE_API_ARB]
801 : THE_DBGMSG_UNKNOWN;
802 Standard_CString& aType = (theType >= GL_DEBUG_TYPE_ERROR_ARB
01ca42b2 803 && theType <= GL_DEBUG_TYPE_OTHER_ARB)
58655684 804 ? THE_DBGMSG_TYPES[theType - GL_DEBUG_TYPE_ERROR_ARB]
805 : THE_DBGMSG_UNKNOWN;
806 Standard_CString& aSev = theSeverity == GL_DEBUG_SEVERITY_HIGH_ARB
807 ? THE_DBGMSG_SEV_HIGH
808 : (theSeverity == GL_DEBUG_SEVERITY_MEDIUM_ARB
809 ? THE_DBGMSG_SEV_MEDIUM
810 : THE_DBGMSG_SEV_LOW);
cbf18624 811 Message_Gravity aGrav = theSeverity == GL_DEBUG_SEVERITY_HIGH_ARB
812 ? Message_Alarm
813 : (theSeverity == GL_DEBUG_SEVERITY_MEDIUM_ARB
814 ? Message_Warning
815 : Message_Info);
816
817 TCollection_ExtendedString aMsg;
818 aMsg += "TKOpenGl"; aMsg += aSrc;
819 aMsg += " | Type: "; aMsg += aType;
820 aMsg += " | ID: "; aMsg += (Standard_Integer )theId;
821 aMsg += " | Severity: "; aMsg += aSev;
822 aMsg += " | Message:\n ";
823 aMsg += theMessage;
7e7c2f0b 824 Messenger()->Send (aMsg, aGrav);
58655684 825}
826
5f8b738e 827// =======================================================================
828// function : init
829// purpose :
830// =======================================================================
831void OpenGl_Context::init()
832{
833 // read version
834 readGlVersion();
835
01ca42b2 836 core11 = (OpenGl_GlCore11* )(&(*myFuncs));
837 core11fwd = (OpenGl_GlCore11Fwd* )(&(*myFuncs));
838 core15 = NULL;
839 core15fwd = NULL;
840 core20 = NULL;
841 core20fwd = NULL;
842 core32 = NULL;
843 core32back = NULL;
844 core41 = NULL;
845 core41back = NULL;
846 core42 = NULL;
847 core42back = NULL;
848 core43 = NULL;
849 core43back = NULL;
850 core44 = NULL;
851 core44back = NULL;
852 arbTBO = NULL;
853 arbIns = NULL;
854 arbDbg = NULL;
855 arbFBO = NULL;
856 extGS = NULL;
857
ca3c13d1 858#if defined(GL_ES_VERSION_2_0)
859
860 hasTexRGBA8 = IsGlGreaterEqual (3, 0)
861 || CheckExtension ("GL_OES_rgb8_rgba8");
862 arbNPTW = IsGlGreaterEqual (3, 0)
863 || CheckExtension ("GL_OES_texture_npot");
864 arbTexRG = IsGlGreaterEqual (3, 0)
865 || CheckExtension ("GL_EXT_texture_rg");
866 extBgra = CheckExtension ("GL_EXT_texture_format_BGRA8888");
867 extAnis = CheckExtension ("GL_EXT_texture_filter_anisotropic");
868 extPDS = CheckExtension ("GL_OES_packed_depth_stencil");
869
870 core11fwd = (OpenGl_GlCore11Fwd* )(&(*myFuncs));
871 if (IsGlGreaterEqual (2, 0))
872 {
873 // enable compatible functions
874 core20 = (OpenGl_GlCore20* )(&(*myFuncs));
875 core20fwd = (OpenGl_GlCore20Fwd* )(&(*myFuncs));
876 core15fwd = (OpenGl_GlCore15Fwd* )(&(*myFuncs));
877 arbFBO = (OpenGl_ArbFBO* )(&(*myFuncs));
878 }
879
880 hasHighp = CheckExtension ("OES_fragment_precision_high");
881 GLint aRange[2] = {0, 0};
882 GLint aPrec [2] = {0, 0};
883 ::glGetShaderPrecisionFormat (GL_FRAGMENT_SHADER, GL_HIGH_FLOAT, aRange, aPrec);
884 if (aPrec[1] != 0)
885 {
886 hasHighp = Standard_True;
887 }
888#else
889
890 myTexClamp = IsGlGreaterEqual (1, 2) ? GL_CLAMP_TO_EDGE : GL_CLAMP;
891
892 hasTexRGBA8 = Standard_True;
bf75be98 893 arbNPTW = CheckExtension ("GL_ARB_texture_non_power_of_two");
894 extBgra = CheckExtension ("GL_EXT_bgra");
895 extAnis = CheckExtension ("GL_EXT_texture_filter_anisotropic");
b859a34d 896 extPDS = CheckExtension ("GL_EXT_packed_depth_stencil");
bf75be98 897 atiMem = CheckExtension ("GL_ATI_meminfo");
898 nvxMem = CheckExtension ("GL_NVX_gpu_memory_info");
899
ca3c13d1 900 GLint aStereo = GL_FALSE;
b5ac8292 901 glGetIntegerv (GL_STEREO, &aStereo);
902 myIsStereoBuffers = aStereo == 1;
903
ca3c13d1 904 // get number of maximum clipping planes
905 glGetIntegerv (GL_MAX_CLIP_PLANES, &myMaxClipPlanes);
906#endif
907
908 glGetIntegerv (GL_MAX_TEXTURE_SIZE, &myMaxTexDim);
909
bf75be98 910 if (extAnis)
911 {
912 glGetIntegerv (GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &myAnisoMax);
913 }
f0430952 914
4269bd1b 915 myClippingState.Init (myMaxClipPlanes);
916
ca3c13d1 917#if !defined(GL_ES_VERSION_2_0)
918
01ca42b2 919 bool has12 = false;
920 bool has13 = false;
921 bool has14 = false;
922 bool has15 = false;
923 bool has20 = false;
924 bool has21 = false;
925 bool has30 = false;
926 bool has31 = false;
927 bool has32 = false;
928 bool has33 = false;
929 bool has40 = false;
930 bool has41 = false;
931 bool has42 = false;
932 bool has43 = false;
933 bool has44 = false;
934
935 //! Make record shorter to retrieve function pointer using variable with same name
936 #define FindProcShort(theFunc) FindProc(#theFunc, myFuncs->theFunc)
937
938 // retrieve platform-dependent extensions
da8bb41d 939#if defined(_WIN32) && !defined(HAVE_EGL)
01ca42b2 940 if (FindProcShort (wglGetExtensionsStringARB))
941 {
942 const char* aWglExts = myFuncs->wglGetExtensionsStringARB (wglGetCurrentDC());
943 if (CheckExtension (aWglExts, "WGL_EXT_swap_control"))
944 {
945 FindProcShort (wglSwapIntervalEXT);
946 }
947 if (CheckExtension (aWglExts, "WGL_ARB_pixel_format"))
948 {
949 FindProcShort (wglChoosePixelFormatARB);
950 }
951 if (CheckExtension (aWglExts, "WGL_ARB_create_context_profile"))
952 {
953 FindProcShort (wglCreateContextAttribsARB);
954 }
955 if (CheckExtension (aWglExts, "WGL_NV_DX_interop"))
956 {
957 FindProcShort (wglDXSetResourceShareHandleNV);
958 FindProcShort (wglDXOpenDeviceNV);
959 FindProcShort (wglDXCloseDeviceNV);
960 FindProcShort (wglDXRegisterObjectNV);
961 FindProcShort (wglDXUnregisterObjectNV);
962 FindProcShort (wglDXObjectAccessNV);
963 FindProcShort (wglDXLockObjectsNV);
964 FindProcShort (wglDXUnlockObjectsNV);
965 }
966 }
967#endif
968
58655684 969 // initialize debug context extension
970 if (CheckExtension ("GL_ARB_debug_output"))
971 {
01ca42b2 972 arbDbg = NULL;
973 if (FindProcShort (glDebugMessageControlARB)
974 && FindProcShort (glDebugMessageInsertARB)
975 && FindProcShort (glDebugMessageCallbackARB)
976 && FindProcShort (glGetDebugMessageLogARB))
58655684 977 {
01ca42b2 978 arbDbg = (OpenGl_ArbDbg* )(&(*myFuncs));
58655684 979 }
980 if (arbDbg != NULL
981 && caps->contextDebug)
982 {
983 // setup default callback
984 arbDbg->glDebugMessageCallbackARB (debugCallbackWrap, this);
fcdbe201 985 #ifdef DEB
58655684 986 glEnable (GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
fcdbe201 987 #endif
58655684 988 }
989 }
990
01ca42b2 991 // load OpenGL 1.2 new functions
992 has12 = IsGlGreaterEqual (1, 2)
993 && FindProcShort (glBlendColor)
994 && FindProcShort (glBlendEquation)
995 && FindProcShort (glDrawRangeElements)
996 && FindProcShort (glTexImage3D)
997 && FindProcShort (glTexSubImage3D)
998 && FindProcShort (glCopyTexSubImage3D);
999
1000 // load OpenGL 1.3 new functions
1001 has13 = IsGlGreaterEqual (1, 3)
1002 && FindProcShort (glActiveTexture)
1003 && FindProcShort (glSampleCoverage)
1004 && FindProcShort (glCompressedTexImage3D)
1005 && FindProcShort (glCompressedTexImage2D)
1006 && FindProcShort (glCompressedTexImage1D)
1007 && FindProcShort (glCompressedTexSubImage3D)
1008 && FindProcShort (glCompressedTexSubImage2D)
1009 && FindProcShort (glCompressedTexSubImage1D)
1010 && FindProcShort (glGetCompressedTexImage)
1011 && FindProcShort (glClientActiveTexture)
1012 && FindProcShort (glMultiTexCoord1d)
1013 && FindProcShort (glMultiTexCoord1dv)
1014 && FindProcShort (glMultiTexCoord1f)
1015 && FindProcShort (glMultiTexCoord1fv)
1016 && FindProcShort (glMultiTexCoord1i)
1017 && FindProcShort (glMultiTexCoord1iv)
1018 && FindProcShort (glMultiTexCoord1s)
1019 && FindProcShort (glMultiTexCoord1sv)
1020 && FindProcShort (glMultiTexCoord2d)
1021 && FindProcShort (glMultiTexCoord2dv)
1022 && FindProcShort (glMultiTexCoord2f)
1023 && FindProcShort (glMultiTexCoord2fv)
1024 && FindProcShort (glMultiTexCoord2i)
1025 && FindProcShort (glMultiTexCoord2iv)
1026 && FindProcShort (glMultiTexCoord2s)
1027 && FindProcShort (glMultiTexCoord2sv)
1028 && FindProcShort (glMultiTexCoord3d)
1029 && FindProcShort (glMultiTexCoord3dv)
1030 && FindProcShort (glMultiTexCoord3f)
1031 && FindProcShort (glMultiTexCoord3fv)
1032 && FindProcShort (glMultiTexCoord3i)
1033 && FindProcShort (glMultiTexCoord3iv)
1034 && FindProcShort (glMultiTexCoord3s)
1035 && FindProcShort (glMultiTexCoord3sv)
1036 && FindProcShort (glMultiTexCoord4d)
1037 && FindProcShort (glMultiTexCoord4dv)
1038 && FindProcShort (glMultiTexCoord4f)
1039 && FindProcShort (glMultiTexCoord4fv)
1040 && FindProcShort (glMultiTexCoord4i)
1041 && FindProcShort (glMultiTexCoord4iv)
1042 && FindProcShort (glMultiTexCoord4s)
1043 && FindProcShort (glMultiTexCoord4sv)
1044 && FindProcShort (glLoadTransposeMatrixf)
1045 && FindProcShort (glLoadTransposeMatrixd)
1046 && FindProcShort (glMultTransposeMatrixf)
1047 && FindProcShort (glMultTransposeMatrixd);
1048
1049 // load OpenGL 1.4 new functions
1050 has14 = IsGlGreaterEqual (1, 4)
1051 && FindProcShort (glBlendFuncSeparate)
1052 && FindProcShort (glMultiDrawArrays)
1053 && FindProcShort (glMultiDrawElements)
1054 && FindProcShort (glPointParameterf)
1055 && FindProcShort (glPointParameterfv)
1056 && FindProcShort (glPointParameteri)
1057 && FindProcShort (glPointParameteriv);
1058
1059 // load OpenGL 1.5 new functions
1060 has15 = IsGlGreaterEqual (1, 5)
1061 && FindProcShort (glGenQueries)
1062 && FindProcShort (glDeleteQueries)
1063 && FindProcShort (glIsQuery)
1064 && FindProcShort (glBeginQuery)
1065 && FindProcShort (glEndQuery)
1066 && FindProcShort (glGetQueryiv)
1067 && FindProcShort (glGetQueryObjectiv)
1068 && FindProcShort (glGetQueryObjectuiv)
1069 && FindProcShort (glBindBuffer)
1070 && FindProcShort (glDeleteBuffers)
1071 && FindProcShort (glGenBuffers)
1072 && FindProcShort (glIsBuffer)
1073 && FindProcShort (glBufferData)
1074 && FindProcShort (glBufferSubData)
1075 && FindProcShort (glGetBufferSubData)
1076 && FindProcShort (glMapBuffer)
1077 && FindProcShort (glUnmapBuffer)
1078 && FindProcShort (glGetBufferParameteriv)
1079 && FindProcShort (glGetBufferPointerv);
1080
1081 // load OpenGL 2.0 new functions
1082 has20 = IsGlGreaterEqual (2, 0)
1083 && FindProcShort (glBlendEquationSeparate)
1084 && FindProcShort (glDrawBuffers)
1085 && FindProcShort (glStencilOpSeparate)
1086 && FindProcShort (glStencilFuncSeparate)
1087 && FindProcShort (glStencilMaskSeparate)
1088 && FindProcShort (glAttachShader)
1089 && FindProcShort (glBindAttribLocation)
1090 && FindProcShort (glCompileShader)
1091 && FindProcShort (glCreateProgram)
1092 && FindProcShort (glCreateShader)
1093 && FindProcShort (glDeleteProgram)
1094 && FindProcShort (glDeleteShader)
1095 && FindProcShort (glDetachShader)
1096 && FindProcShort (glDisableVertexAttribArray)
1097 && FindProcShort (glEnableVertexAttribArray)
1098 && FindProcShort (glGetActiveAttrib)
1099 && FindProcShort (glGetActiveUniform)
1100 && FindProcShort (glGetAttachedShaders)
1101 && FindProcShort (glGetAttribLocation)
1102 && FindProcShort (glGetProgramiv)
1103 && FindProcShort (glGetProgramInfoLog)
1104 && FindProcShort (glGetShaderiv)
1105 && FindProcShort (glGetShaderInfoLog)
1106 && FindProcShort (glGetShaderSource)
1107 && FindProcShort (glGetUniformLocation)
1108 && FindProcShort (glGetUniformfv)
1109 && FindProcShort (glGetUniformiv)
1110 && FindProcShort (glGetVertexAttribdv)
1111 && FindProcShort (glGetVertexAttribfv)
1112 && FindProcShort (glGetVertexAttribiv)
1113 && FindProcShort (glGetVertexAttribPointerv)
1114 && FindProcShort (glIsProgram)
1115 && FindProcShort (glIsShader)
1116 && FindProcShort (glLinkProgram)
1117 && FindProcShort (glShaderSource)
1118 && FindProcShort (glUseProgram)
1119 && FindProcShort (glUniform1f)
1120 && FindProcShort (glUniform2f)
1121 && FindProcShort (glUniform3f)
1122 && FindProcShort (glUniform4f)
1123 && FindProcShort (glUniform1i)
1124 && FindProcShort (glUniform2i)
1125 && FindProcShort (glUniform3i)
1126 && FindProcShort (glUniform4i)
1127 && FindProcShort (glUniform1fv)
1128 && FindProcShort (glUniform2fv)
1129 && FindProcShort (glUniform3fv)
1130 && FindProcShort (glUniform4fv)
1131 && FindProcShort (glUniform1iv)
1132 && FindProcShort (glUniform2iv)
1133 && FindProcShort (glUniform3iv)
1134 && FindProcShort (glUniform4iv)
1135 && FindProcShort (glUniformMatrix2fv)
1136 && FindProcShort (glUniformMatrix3fv)
1137 && FindProcShort (glUniformMatrix4fv)
1138 && FindProcShort (glValidateProgram)
1139 && FindProcShort (glVertexAttrib1d)
1140 && FindProcShort (glVertexAttrib1dv)
1141 && FindProcShort (glVertexAttrib1f)
1142 && FindProcShort (glVertexAttrib1fv)
1143 && FindProcShort (glVertexAttrib1s)
1144 && FindProcShort (glVertexAttrib1sv)
1145 && FindProcShort (glVertexAttrib2d)
1146 && FindProcShort (glVertexAttrib2dv)
1147 && FindProcShort (glVertexAttrib2f)
1148 && FindProcShort (glVertexAttrib2fv)
1149 && FindProcShort (glVertexAttrib2s)
1150 && FindProcShort (glVertexAttrib2sv)
1151 && FindProcShort (glVertexAttrib3d)
1152 && FindProcShort (glVertexAttrib3dv)
1153 && FindProcShort (glVertexAttrib3f)
1154 && FindProcShort (glVertexAttrib3fv)
1155 && FindProcShort (glVertexAttrib3s)
1156 && FindProcShort (glVertexAttrib3sv)
1157 && FindProcShort (glVertexAttrib4Nbv)
1158 && FindProcShort (glVertexAttrib4Niv)
1159 && FindProcShort (glVertexAttrib4Nsv)
1160 && FindProcShort (glVertexAttrib4Nub)
1161 && FindProcShort (glVertexAttrib4Nubv)
1162 && FindProcShort (glVertexAttrib4Nuiv)
1163 && FindProcShort (glVertexAttrib4Nusv)
1164 && FindProcShort (glVertexAttrib4bv)
1165 && FindProcShort (glVertexAttrib4d)
1166 && FindProcShort (glVertexAttrib4dv)
1167 && FindProcShort (glVertexAttrib4f)
1168 && FindProcShort (glVertexAttrib4fv)
1169 && FindProcShort (glVertexAttrib4iv)
1170 && FindProcShort (glVertexAttrib4s)
1171 && FindProcShort (glVertexAttrib4sv)
1172 && FindProcShort (glVertexAttrib4ubv)
1173 && FindProcShort (glVertexAttrib4uiv)
1174 && FindProcShort (glVertexAttrib4usv)
1175 && FindProcShort (glVertexAttribPointer);
1176
1177 // load OpenGL 2.1 new functions
1178 has21 = IsGlGreaterEqual (2, 1)
1179 && FindProcShort (glUniformMatrix2x3fv)
1180 && FindProcShort (glUniformMatrix3x2fv)
1181 && FindProcShort (glUniformMatrix2x4fv)
1182 && FindProcShort (glUniformMatrix4x2fv)
1183 && FindProcShort (glUniformMatrix3x4fv)
1184 && FindProcShort (glUniformMatrix4x3fv);
1185
1186 // load GL_ARB_framebuffer_object (added to OpenGL 3.0 core)
1187 const bool hasFBO = (IsGlGreaterEqual (3, 0) || CheckExtension ("GL_ARB_framebuffer_object"))
1188 && FindProcShort (glIsRenderbuffer)
1189 && FindProcShort (glBindRenderbuffer)
1190 && FindProcShort (glDeleteRenderbuffers)
1191 && FindProcShort (glGenRenderbuffers)
1192 && FindProcShort (glRenderbufferStorage)
1193 && FindProcShort (glGetRenderbufferParameteriv)
1194 && FindProcShort (glIsFramebuffer)
1195 && FindProcShort (glBindFramebuffer)
1196 && FindProcShort (glDeleteFramebuffers)
1197 && FindProcShort (glGenFramebuffers)
1198 && FindProcShort (glCheckFramebufferStatus)
1199 && FindProcShort (glFramebufferTexture1D)
1200 && FindProcShort (glFramebufferTexture2D)
1201 && FindProcShort (glFramebufferTexture3D)
1202 && FindProcShort (glFramebufferRenderbuffer)
1203 && FindProcShort (glGetFramebufferAttachmentParameteriv)
1204 && FindProcShort (glGenerateMipmap)
1205 && FindProcShort (glBlitFramebuffer)
1206 && FindProcShort (glRenderbufferStorageMultisample)
1207 && FindProcShort (glFramebufferTextureLayer);
1208
1209 // load GL_ARB_vertex_array_object (added to OpenGL 3.0 core)
1210 const bool hasVAO = (IsGlGreaterEqual (3, 0) || CheckExtension ("GL_ARB_vertex_array_object"))
1211 && FindProcShort (glBindVertexArray)
1212 && FindProcShort (glDeleteVertexArrays)
1213 && FindProcShort (glGenVertexArrays)
1214 && FindProcShort (glIsVertexArray);
1215
1216 // load GL_ARB_map_buffer_range (added to OpenGL 3.0 core)
1217 const bool hasMapBufferRange = (IsGlGreaterEqual (3, 0) || CheckExtension ("GL_ARB_map_buffer_range"))
1218 && FindProcShort (glMapBufferRange)
1219 && FindProcShort (glFlushMappedBufferRange);
1220
1221 // load OpenGL 3.0 new functions
1222 has30 = IsGlGreaterEqual (3, 0)
1223 && hasFBO
1224 && hasVAO
1225 && hasMapBufferRange
1226 && FindProcShort (glColorMaski)
1227 && FindProcShort (glGetBooleani_v)
1228 && FindProcShort (glGetIntegeri_v)
1229 && FindProcShort (glEnablei)
1230 && FindProcShort (glDisablei)
1231 && FindProcShort (glIsEnabledi)
1232 && FindProcShort (glBeginTransformFeedback)
1233 && FindProcShort (glEndTransformFeedback)
1234 && FindProcShort (glBindBufferRange)
1235 && FindProcShort (glBindBufferBase)
1236 && FindProcShort (glTransformFeedbackVaryings)
1237 && FindProcShort (glGetTransformFeedbackVarying)
1238 && FindProcShort (glClampColor)
1239 && FindProcShort (glBeginConditionalRender)
1240 && FindProcShort (glEndConditionalRender)
1241 && FindProcShort (glVertexAttribIPointer)
1242 && FindProcShort (glGetVertexAttribIiv)
1243 && FindProcShort (glGetVertexAttribIuiv)
1244 && FindProcShort (glVertexAttribI1i)
1245 && FindProcShort (glVertexAttribI2i)
1246 && FindProcShort (glVertexAttribI3i)
1247 && FindProcShort (glVertexAttribI4i)
1248 && FindProcShort (glVertexAttribI1ui)
1249 && FindProcShort (glVertexAttribI2ui)
1250 && FindProcShort (glVertexAttribI3ui)
1251 && FindProcShort (glVertexAttribI4ui)
1252 && FindProcShort (glVertexAttribI1iv)
1253 && FindProcShort (glVertexAttribI2iv)
1254 && FindProcShort (glVertexAttribI3iv)
1255 && FindProcShort (glVertexAttribI4iv)
1256 && FindProcShort (glVertexAttribI1uiv)
1257 && FindProcShort (glVertexAttribI2uiv)
1258 && FindProcShort (glVertexAttribI3uiv)
1259 && FindProcShort (glVertexAttribI4uiv)
1260 && FindProcShort (glVertexAttribI4bv)
1261 && FindProcShort (glVertexAttribI4sv)
1262 && FindProcShort (glVertexAttribI4ubv)
1263 && FindProcShort (glVertexAttribI4usv)
1264 && FindProcShort (glGetUniformuiv)
1265 && FindProcShort (glBindFragDataLocation)
1266 && FindProcShort (glGetFragDataLocation)
1267 && FindProcShort (glUniform1ui)
1268 && FindProcShort (glUniform2ui)
1269 && FindProcShort (glUniform3ui)
1270 && FindProcShort (glUniform4ui)
1271 && FindProcShort (glUniform1uiv)
1272 && FindProcShort (glUniform2uiv)
1273 && FindProcShort (glUniform3uiv)
1274 && FindProcShort (glUniform4uiv)
1275 && FindProcShort (glTexParameterIiv)
1276 && FindProcShort (glTexParameterIuiv)
1277 && FindProcShort (glGetTexParameterIiv)
1278 && FindProcShort (glGetTexParameterIuiv)
1279 && FindProcShort (glClearBufferiv)
1280 && FindProcShort (glClearBufferuiv)
1281 && FindProcShort (glClearBufferfv)
1282 && FindProcShort (glClearBufferfi)
1283 && FindProcShort (glGetStringi);
1284
1285 // load GL_ARB_uniform_buffer_object (added to OpenGL 3.1 core)
1286 const bool hasUBO = (IsGlGreaterEqual (3, 1) || CheckExtension ("GL_ARB_uniform_buffer_object"))
1287 && FindProcShort (glGetUniformIndices)
1288 && FindProcShort (glGetActiveUniformsiv)
1289 && FindProcShort (glGetActiveUniformName)
1290 && FindProcShort (glGetUniformBlockIndex)
1291 && FindProcShort (glGetActiveUniformBlockiv)
1292 && FindProcShort (glGetActiveUniformBlockName)
1293 && FindProcShort (glUniformBlockBinding);
1294
1295 // load GL_ARB_copy_buffer (added to OpenGL 3.1 core)
1296 const bool hasCopyBufSubData = (IsGlGreaterEqual (3, 1) || CheckExtension ("GL_ARB_copy_buffer"))
1297 && FindProcShort (glCopyBufferSubData);
1298
1299 if (has30)
2166f0fa 1300 {
01ca42b2 1301 // NPOT textures are required by OpenGL 2.0 specifications
1302 // but doesn't hardware accelerated by some ancient OpenGL 2.1 hardware (GeForce FX, RadeOn 9700 etc.)
1303 arbNPTW = Standard_True;
1304 arbTexRG = Standard_True;
2166f0fa
SK
1305 }
1306
01ca42b2 1307 // load OpenGL 3.1 new functions
1308 has31 = IsGlGreaterEqual (3, 1)
1309 && hasUBO
1310 && hasCopyBufSubData
1311 && FindProcShort (glDrawArraysInstanced)
1312 && FindProcShort (glDrawElementsInstanced)
1313 && FindProcShort (glTexBuffer)
1314 && FindProcShort (glPrimitiveRestartIndex);
1315
1316 // load GL_ARB_draw_elements_base_vertex (added to OpenGL 3.2 core)
1317 const bool hasDrawElemsBaseVert = (IsGlGreaterEqual (3, 2) || CheckExtension ("GL_ARB_draw_elements_base_vertex"))
1318 && FindProcShort (glDrawElementsBaseVertex)
1319 && FindProcShort (glDrawRangeElementsBaseVertex)
1320 && FindProcShort (glDrawElementsInstancedBaseVertex)
1321 && FindProcShort (glMultiDrawElementsBaseVertex);
1322
1323 // load GL_ARB_provoking_vertex (added to OpenGL 3.2 core)
1324 const bool hasProvokingVert = (IsGlGreaterEqual (3, 2) || CheckExtension ("GL_ARB_provoking_vertex"))
1325 && FindProcShort (glProvokingVertex);
1326
1327 // load GL_ARB_sync (added to OpenGL 3.2 core)
1328 const bool hasSync = (IsGlGreaterEqual (3, 2) || CheckExtension ("GL_ARB_sync"))
1329 && FindProcShort (glFenceSync)
1330 && FindProcShort (glIsSync)
1331 && FindProcShort (glDeleteSync)
1332 && FindProcShort (glClientWaitSync)
1333 && FindProcShort (glWaitSync)
1334 && FindProcShort (glGetInteger64v)
1335 && FindProcShort (glGetSynciv);
1336
1337 // load GL_ARB_texture_multisample (added to OpenGL 3.2 core)
1338 const bool hasTextureMultisample = (IsGlGreaterEqual (3, 2) || CheckExtension ("GL_ARB_texture_multisample"))
1339 && FindProcShort (glTexImage2DMultisample)
1340 && FindProcShort (glTexImage3DMultisample)
1341 && FindProcShort (glGetMultisamplefv)
1342 && FindProcShort (glSampleMaski);
1343
1344 // load OpenGL 3.2 new functions
1345 has32 = IsGlGreaterEqual (3, 2)
1346 && hasDrawElemsBaseVert
1347 && hasProvokingVert
1348 && hasSync
1349 && hasTextureMultisample
1350 && FindProcShort (glGetInteger64i_v)
1351 && FindProcShort (glGetBufferParameteri64v)
1352 && FindProcShort (glFramebufferTexture);
1353
1354 // load GL_ARB_blend_func_extended (added to OpenGL 3.3 core)
1355 const bool hasBlendFuncExtended = (IsGlGreaterEqual (3, 3) || CheckExtension ("GL_ARB_blend_func_extended"))
1356 && FindProcShort (glBindFragDataLocationIndexed)
1357 && FindProcShort (glGetFragDataIndex);
1358
1359 // load GL_ARB_sampler_objects (added to OpenGL 3.3 core)
1360 const bool hasSamplerObjects = (IsGlGreaterEqual (3, 3) || CheckExtension ("GL_ARB_sampler_objects"))
1361 && FindProcShort (glGenSamplers)
1362 && FindProcShort (glDeleteSamplers)
1363 && FindProcShort (glIsSampler)
1364 && FindProcShort (glBindSampler)
1365 && FindProcShort (glSamplerParameteri)
1366 && FindProcShort (glSamplerParameteriv)
1367 && FindProcShort (glSamplerParameterf)
1368 && FindProcShort (glSamplerParameterfv)
1369 && FindProcShort (glSamplerParameterIiv)
1370 && FindProcShort (glSamplerParameterIuiv)
1371 && FindProcShort (glGetSamplerParameteriv)
1372 && FindProcShort (glGetSamplerParameterIiv)
1373 && FindProcShort (glGetSamplerParameterfv)
1374 && FindProcShort (glGetSamplerParameterIuiv);
1375
1376 // load GL_ARB_timer_query (added to OpenGL 3.3 core)
1377 const bool hasTimerQuery = (IsGlGreaterEqual (3, 3) || CheckExtension ("GL_ARB_timer_query"))
1378 && FindProcShort (glQueryCounter)
1379 && FindProcShort (glGetQueryObjecti64v)
1380 && FindProcShort (glGetQueryObjectui64v);
1381
1382 // load GL_ARB_vertex_type_2_10_10_10_rev (added to OpenGL 3.3 core)
1383 const bool hasVertType21010101rev = (IsGlGreaterEqual (3, 3) || CheckExtension ("GL_ARB_vertex_type_2_10_10_10_rev"))
1384 && FindProcShort (glVertexP2ui)
1385 && FindProcShort (glVertexP2uiv)
1386 && FindProcShort (glVertexP3ui)
1387 && FindProcShort (glVertexP3uiv)
1388 && FindProcShort (glVertexP4ui)
1389 && FindProcShort (glVertexP4uiv)
1390 && FindProcShort (glTexCoordP1ui)
1391 && FindProcShort (glTexCoordP1uiv)
1392 && FindProcShort (glTexCoordP2ui)
1393 && FindProcShort (glTexCoordP2uiv)
1394 && FindProcShort (glTexCoordP3ui)
1395 && FindProcShort (glTexCoordP3uiv)
1396 && FindProcShort (glTexCoordP4ui)
1397 && FindProcShort (glTexCoordP4uiv)
1398 && FindProcShort (glMultiTexCoordP1ui)
1399 && FindProcShort (glMultiTexCoordP1uiv)
1400 && FindProcShort (glMultiTexCoordP2ui)
1401 && FindProcShort (glMultiTexCoordP2uiv)
1402 && FindProcShort (glMultiTexCoordP3ui)
1403 && FindProcShort (glMultiTexCoordP3uiv)
1404 && FindProcShort (glMultiTexCoordP4ui)
1405 && FindProcShort (glMultiTexCoordP4uiv)
1406 && FindProcShort (glNormalP3ui)
1407 && FindProcShort (glNormalP3uiv)
1408 && FindProcShort (glColorP3ui)
1409 && FindProcShort (glColorP3uiv)
1410 && FindProcShort (glColorP4ui)
1411 && FindProcShort (glColorP4uiv)
1412 && FindProcShort (glSecondaryColorP3ui)
1413 && FindProcShort (glSecondaryColorP3uiv)
1414 && FindProcShort (glVertexAttribP1ui)
1415 && FindProcShort (glVertexAttribP1uiv)
1416 && FindProcShort (glVertexAttribP2ui)
1417 && FindProcShort (glVertexAttribP2uiv)
1418 && FindProcShort (glVertexAttribP3ui)
1419 && FindProcShort (glVertexAttribP3uiv)
1420 && FindProcShort (glVertexAttribP4ui)
1421 && FindProcShort (glVertexAttribP4uiv);
1422
1423 // load OpenGL 3.3 extra functions
1424 has33 = IsGlGreaterEqual (3, 3)
1425 && hasBlendFuncExtended
1426 && hasSamplerObjects
1427 && hasTimerQuery
1428 && hasVertType21010101rev
1429 && FindProcShort (glVertexAttribDivisor);
1430
1431 // load GL_ARB_draw_indirect (added to OpenGL 4.0 core)
1432 const bool hasDrawIndirect = (IsGlGreaterEqual (4, 0) || CheckExtension ("GL_ARB_draw_indirect"))
1433 && FindProcShort (glDrawArraysIndirect)
1434 && FindProcShort (glDrawElementsIndirect);
1435
1436 // load GL_ARB_gpu_shader_fp64 (added to OpenGL 4.0 core)
1437 const bool hasShaderFP64 = (IsGlGreaterEqual (4, 0) || CheckExtension ("GL_ARB_gpu_shader_fp64"))
1438 && FindProcShort (glUniform1d)
1439 && FindProcShort (glUniform2d)
1440 && FindProcShort (glUniform3d)
1441 && FindProcShort (glUniform4d)
1442 && FindProcShort (glUniform1dv)
1443 && FindProcShort (glUniform2dv)
1444 && FindProcShort (glUniform3dv)
1445 && FindProcShort (glUniform4dv)
1446 && FindProcShort (glUniformMatrix2dv)
1447 && FindProcShort (glUniformMatrix3dv)
1448 && FindProcShort (glUniformMatrix4dv)
1449 && FindProcShort (glUniformMatrix2x3dv)
1450 && FindProcShort (glUniformMatrix2x4dv)
1451 && FindProcShort (glUniformMatrix3x2dv)
1452 && FindProcShort (glUniformMatrix3x4dv)
1453 && FindProcShort (glUniformMatrix4x2dv)
1454 && FindProcShort (glUniformMatrix4x3dv)
1455 && FindProcShort (glGetUniformdv);
1456
1457 // load GL_ARB_shader_subroutine (added to OpenGL 4.0 core)
1458 const bool hasShaderSubroutine = (IsGlGreaterEqual (4, 0) || CheckExtension ("GL_ARB_shader_subroutine"))
1459 && FindProcShort (glGetSubroutineUniformLocation)
1460 && FindProcShort (glGetSubroutineIndex)
1461 && FindProcShort (glGetActiveSubroutineUniformiv)
1462 && FindProcShort (glGetActiveSubroutineUniformName)
1463 && FindProcShort (glGetActiveSubroutineName)
1464 && FindProcShort (glUniformSubroutinesuiv)
1465 && FindProcShort (glGetUniformSubroutineuiv)
1466 && FindProcShort (glGetProgramStageiv);
1467
1468 // load GL_ARB_tessellation_shader (added to OpenGL 4.0 core)
1469 const bool hasTessellationShader = (IsGlGreaterEqual (4, 0) || CheckExtension ("GL_ARB_tessellation_shader"))
1470 && FindProcShort (glPatchParameteri)
1471 && FindProcShort (glPatchParameterfv);
1472
1473 // load GL_ARB_transform_feedback2 (added to OpenGL 4.0 core)
1474 const bool hasTrsfFeedback2 = (IsGlGreaterEqual (4, 0) || CheckExtension ("GL_ARB_transform_feedback2"))
1475 && FindProcShort (glBindTransformFeedback)
1476 && FindProcShort (glDeleteTransformFeedbacks)
1477 && FindProcShort (glGenTransformFeedbacks)
1478 && FindProcShort (glIsTransformFeedback)
1479 && FindProcShort (glPauseTransformFeedback)
1480 && FindProcShort (glResumeTransformFeedback)
1481 && FindProcShort (glDrawTransformFeedback);
1482
1483 // load GL_ARB_transform_feedback3 (added to OpenGL 4.0 core)
1484 const bool hasTrsfFeedback3 = (IsGlGreaterEqual (4, 0) || CheckExtension ("GL_ARB_transform_feedback3"))
1485 && FindProcShort (glDrawTransformFeedbackStream)
1486 && FindProcShort (glBeginQueryIndexed)
1487 && FindProcShort (glEndQueryIndexed)
1488 && FindProcShort (glGetQueryIndexediv);
1489
1490 // load OpenGL 4.0 new functions
1491 has40 = IsGlGreaterEqual (4, 0)
1492 && hasDrawIndirect
1493 && hasShaderFP64
1494 && hasShaderSubroutine
1495 && hasTessellationShader
1496 && hasTrsfFeedback2
1497 && hasTrsfFeedback3
1498 && FindProcShort (glMinSampleShading)
1499 && FindProcShort (glBlendEquationi)
1500 && FindProcShort (glBlendEquationSeparatei)
1501 && FindProcShort (glBlendFunci)
1502 && FindProcShort (glBlendFuncSeparatei);
1503
1504 // load GL_ARB_ES2_compatibility (added to OpenGL 4.1 core)
1505 const bool hasES2Compatibility = (IsGlGreaterEqual (4, 1) || CheckExtension ("GL_ARB_ES2_compatibility"))
1506 && FindProcShort (glReleaseShaderCompiler)
1507 && FindProcShort (glShaderBinary)
1508 && FindProcShort (glGetShaderPrecisionFormat)
1509 && FindProcShort (glDepthRangef)
1510 && FindProcShort (glClearDepthf);
1511
1512 // load GL_ARB_get_program_binary (added to OpenGL 4.1 core)
1513 const bool hasGetProgramBinary = (IsGlGreaterEqual (4, 1) || CheckExtension ("GL_ARB_get_program_binary"))
1514 && FindProcShort (glGetProgramBinary)
1515 && FindProcShort (glProgramBinary)
1516 && FindProcShort (glProgramParameteri);
1517
1518
1519 // load GL_ARB_separate_shader_objects (added to OpenGL 4.1 core)
1520 const bool hasSeparateShaderObjects = (IsGlGreaterEqual (4, 1) || CheckExtension ("GL_ARB_separate_shader_objects"))
1521 && FindProcShort (glUseProgramStages)
1522 && FindProcShort (glActiveShaderProgram)
1523 && FindProcShort (glCreateShaderProgramv)
1524 && FindProcShort (glBindProgramPipeline)
1525 && FindProcShort (glDeleteProgramPipelines)
1526 && FindProcShort (glGenProgramPipelines)
1527 && FindProcShort (glIsProgramPipeline)
1528 && FindProcShort (glGetProgramPipelineiv)
1529 && FindProcShort (glProgramUniform1i)
1530 && FindProcShort (glProgramUniform1iv)
1531 && FindProcShort (glProgramUniform1f)
1532 && FindProcShort (glProgramUniform1fv)
1533 && FindProcShort (glProgramUniform1d)
1534 && FindProcShort (glProgramUniform1dv)
1535 && FindProcShort (glProgramUniform1ui)
1536 && FindProcShort (glProgramUniform1uiv)
1537 && FindProcShort (glProgramUniform2i)
1538 && FindProcShort (glProgramUniform2iv)
1539 && FindProcShort (glProgramUniform2f)
1540 && FindProcShort (glProgramUniform2fv)
1541 && FindProcShort (glProgramUniform2d)
1542 && FindProcShort (glProgramUniform2dv)
1543 && FindProcShort (glProgramUniform2ui)
1544 && FindProcShort (glProgramUniform2uiv)
1545 && FindProcShort (glProgramUniform3i)
1546 && FindProcShort (glProgramUniform3iv)
1547 && FindProcShort (glProgramUniform3f)
1548 && FindProcShort (glProgramUniform3fv)
1549 && FindProcShort (glProgramUniform3d)
1550 && FindProcShort (glProgramUniform3dv)
1551 && FindProcShort (glProgramUniform3ui)
1552 && FindProcShort (glProgramUniform3uiv)
1553 && FindProcShort (glProgramUniform4i)
1554 && FindProcShort (glProgramUniform4iv)
1555 && FindProcShort (glProgramUniform4f)
1556 && FindProcShort (glProgramUniform4fv)
1557 && FindProcShort (glProgramUniform4d)
1558 && FindProcShort (glProgramUniform4dv)
1559 && FindProcShort (glProgramUniform4ui)
1560 && FindProcShort (glProgramUniform4uiv)
1561 && FindProcShort (glProgramUniformMatrix2fv)
1562 && FindProcShort (glProgramUniformMatrix3fv)
1563 && FindProcShort (glProgramUniformMatrix4fv)
1564 && FindProcShort (glProgramUniformMatrix2dv)
1565 && FindProcShort (glProgramUniformMatrix3dv)
1566 && FindProcShort (glProgramUniformMatrix4dv)
1567 && FindProcShort (glProgramUniformMatrix2x3fv)
1568 && FindProcShort (glProgramUniformMatrix3x2fv)
1569 && FindProcShort (glProgramUniformMatrix2x4fv)
1570 && FindProcShort (glProgramUniformMatrix4x2fv)
1571 && FindProcShort (glProgramUniformMatrix3x4fv)
1572 && FindProcShort (glProgramUniformMatrix4x3fv)
1573 && FindProcShort (glProgramUniformMatrix2x3dv)
1574 && FindProcShort (glProgramUniformMatrix3x2dv)
1575 && FindProcShort (glProgramUniformMatrix2x4dv)
1576 && FindProcShort (glProgramUniformMatrix4x2dv)
1577 && FindProcShort (glProgramUniformMatrix3x4dv)
1578 && FindProcShort (glProgramUniformMatrix4x3dv)
1579 && FindProcShort (glValidateProgramPipeline)
1580 && FindProcShort (glGetProgramPipelineInfoLog);
1581
1582 // load GL_ARB_vertex_attrib_64bit (added to OpenGL 4.1 core)
1583 const bool hasVertAttrib64bit = (IsGlGreaterEqual (4, 1) || CheckExtension ("GL_ARB_vertex_attrib_64bit"))
1584 && FindProcShort (glVertexAttribL1d)
1585 && FindProcShort (glVertexAttribL2d)
1586 && FindProcShort (glVertexAttribL3d)
1587 && FindProcShort (glVertexAttribL4d)
1588 && FindProcShort (glVertexAttribL1dv)
1589 && FindProcShort (glVertexAttribL2dv)
1590 && FindProcShort (glVertexAttribL3dv)
1591 && FindProcShort (glVertexAttribL4dv)
1592 && FindProcShort (glVertexAttribLPointer)
1593 && FindProcShort (glGetVertexAttribLdv);
1594
1595 // load GL_ARB_viewport_array (added to OpenGL 4.1 core)
1596 const bool hasViewportArray = (IsGlGreaterEqual (4, 1) || CheckExtension ("GL_ARB_viewport_array"))
1597 && FindProcShort (glViewportArrayv)
1598 && FindProcShort (glViewportIndexedf)
1599 && FindProcShort (glViewportIndexedfv)
1600 && FindProcShort (glScissorArrayv)
1601 && FindProcShort (glScissorIndexed)
1602 && FindProcShort (glScissorIndexedv)
1603 && FindProcShort (glDepthRangeArrayv)
1604 && FindProcShort (glDepthRangeIndexed)
1605 && FindProcShort (glGetFloati_v)
1606 && FindProcShort (glGetDoublei_v);
1607
1608 has41 = IsGlGreaterEqual (4, 1)
1609 && hasES2Compatibility
1610 && hasGetProgramBinary
1611 && hasSeparateShaderObjects
1612 && hasVertAttrib64bit
1613 && hasViewportArray;
1614
1615 // load GL_ARB_base_instance (added to OpenGL 4.2 core)
1616 const bool hasBaseInstance = (IsGlGreaterEqual (4, 2) || CheckExtension ("GL_ARB_base_instance"))
1617 && FindProcShort (glDrawArraysInstancedBaseInstance)
1618 && FindProcShort (glDrawElementsInstancedBaseInstance)
1619 && FindProcShort (glDrawElementsInstancedBaseVertexBaseInstance);
1620
1621 // load GL_ARB_transform_feedback_instanced (added to OpenGL 4.2 core)
1622 const bool hasTrsfFeedbackInstanced = (IsGlGreaterEqual (4, 2) || CheckExtension ("GL_ARB_transform_feedback_instanced"))
1623 && FindProcShort (glDrawTransformFeedbackInstanced)
1624 && FindProcShort (glDrawTransformFeedbackStreamInstanced);
1625
1626 // load GL_ARB_internalformat_query (added to OpenGL 4.2 core)
1627 const bool hasInternalFormatQuery = (IsGlGreaterEqual (4, 2) || CheckExtension ("GL_ARB_internalformat_query"))
1628 && FindProcShort (glGetInternalformativ);
1629
1630 // load GL_ARB_shader_atomic_counters (added to OpenGL 4.2 core)
1631 const bool hasShaderAtomicCounters = (IsGlGreaterEqual (4, 2) || CheckExtension ("GL_ARB_shader_atomic_counters"))
1632 && FindProcShort (glGetActiveAtomicCounterBufferiv);
1633
1634 // load GL_ARB_shader_image_load_store (added to OpenGL 4.2 core)
1635 const bool hasShaderImgLoadStore = (IsGlGreaterEqual (4, 2) || CheckExtension ("GL_ARB_shader_image_load_store"))
1636 && FindProcShort (glBindImageTexture)
1637 && FindProcShort (glMemoryBarrier);
1638
1639 // load GL_ARB_texture_storage (added to OpenGL 4.2 core)
1640 const bool hasTextureStorage = (IsGlGreaterEqual (4, 2) || CheckExtension ("GL_ARB_texture_storage"))
1641 && FindProcShort (glTexStorage1D)
1642 && FindProcShort (glTexStorage2D)
1643 && FindProcShort (glTexStorage3D)
1644 && FindProcShort (glTextureStorage1DEXT)
1645 && FindProcShort (glTextureStorage2DEXT)
1646 && FindProcShort (glTextureStorage3DEXT);
1647
1648 has42 = IsGlGreaterEqual (4, 2)
1649 && hasBaseInstance
1650 && hasTrsfFeedbackInstanced
1651 && hasInternalFormatQuery
1652 && hasShaderAtomicCounters
1653 && hasShaderImgLoadStore
1654 && hasTextureStorage;
1655
1656 has43 = IsGlGreaterEqual (4, 3)
1657 && FindProcShort (glClearBufferData)
1658 && FindProcShort (glClearBufferSubData)
1659 && FindProcShort (glDispatchCompute)
1660 && FindProcShort (glDispatchComputeIndirect)
1661 && FindProcShort (glCopyImageSubData)
1662 && FindProcShort (glFramebufferParameteri)
1663 && FindProcShort (glGetFramebufferParameteriv)
1664 && FindProcShort (glGetInternalformati64v)
1665 && FindProcShort (glInvalidateTexSubImage)
1666 && FindProcShort (glInvalidateTexImage)
1667 && FindProcShort (glInvalidateBufferSubData)
1668 && FindProcShort (glInvalidateBufferData)
1669 && FindProcShort (glInvalidateFramebuffer)
1670 && FindProcShort (glInvalidateSubFramebuffer)
1671 && FindProcShort (glMultiDrawArraysIndirect)
1672 && FindProcShort (glMultiDrawElementsIndirect)
1673 && FindProcShort (glGetProgramInterfaceiv)
1674 && FindProcShort (glGetProgramResourceIndex)
1675 && FindProcShort (glGetProgramResourceName)
1676 && FindProcShort (glGetProgramResourceiv)
1677 && FindProcShort (glGetProgramResourceLocation)
1678 && FindProcShort (glGetProgramResourceLocationIndex)
1679 && FindProcShort (glShaderStorageBlockBinding)
1680 && FindProcShort (glTexBufferRange)
1681 && FindProcShort (glTexStorage2DMultisample)
1682 && FindProcShort (glTexStorage3DMultisample)
1683 && FindProcShort (glTextureView)
1684 && FindProcShort (glBindVertexBuffer)
1685 && FindProcShort (glVertexAttribFormat)
1686 && FindProcShort (glVertexAttribIFormat)
1687 && FindProcShort (glVertexAttribLFormat)
1688 && FindProcShort (glVertexAttribBinding)
1689 && FindProcShort (glVertexBindingDivisor)
1690 && FindProcShort (glDebugMessageControl)
1691 && FindProcShort (glDebugMessageInsert)
1692 && FindProcShort (glDebugMessageCallback)
1693 && FindProcShort (glGetDebugMessageLog)
1694 && FindProcShort (glPushDebugGroup)
1695 && FindProcShort (glPopDebugGroup)
1696 && FindProcShort (glObjectLabel)
1697 && FindProcShort (glGetObjectLabel)
1698 && FindProcShort (glObjectPtrLabel)
1699 && FindProcShort (glGetObjectPtrLabel);
1700
1701 // load GL_ARB_clear_texture (added to OpenGL 4.4 core)
1702 bool arbTexClear = (IsGlGreaterEqual (4, 4) || CheckExtension ("GL_ARB_clear_texture"))
1703 && FindProcShort (glClearTexImage)
1704 && FindProcShort (glClearTexSubImage);
1705
1706 has44 = IsGlGreaterEqual (4, 4)
1707 && arbTexClear
1708 && FindProcShort (glBufferStorage)
1709 && FindProcShort (glBindBuffersBase)
1710 && FindProcShort (glBindBuffersRange)
1711 && FindProcShort (glBindTextures)
1712 && FindProcShort (glBindSamplers)
1713 && FindProcShort (glBindImageTextures)
1714 && FindProcShort (glBindVertexBuffers);
1715
5e27df78 1716 // initialize TBO extension (ARB)
01ca42b2 1717 if (!has31
1718 && CheckExtension ("GL_ARB_texture_buffer_object")
1719 && FindProc ("glTexBufferARB", myFuncs->glTexBuffer))
5e27df78 1720 {
01ca42b2 1721 arbTBO = (OpenGl_ArbTBO* )(&(*myFuncs));
5e27df78 1722 }
1723
1724 // initialize hardware instancing extension (ARB)
01ca42b2 1725 if (!has31
1726 && CheckExtension ("GL_ARB_draw_instanced")
1727 && FindProc ("glDrawArraysInstancedARB", myFuncs->glDrawArraysInstanced)
1728 && FindProc ("glDrawElementsInstancedARB", myFuncs->glDrawElementsInstanced))
5e27df78 1729 {
01ca42b2 1730 arbIns = (OpenGl_ArbIns* )(&(*myFuncs));
5e27df78 1731 }
1732
01ca42b2 1733 // initialize FBO extension (ARB)
1734 if (hasFBO)
2166f0fa 1735 {
01ca42b2 1736 arbFBO = (OpenGl_ArbFBO* )(&(*myFuncs));
1737 extPDS = Standard_True; // extension for EXT, but part of ARB
2166f0fa 1738 }
5f8b738e 1739
37eb4787 1740 // initialize GS extension (EXT)
01ca42b2 1741 if (CheckExtension ("GL_EXT_geometry_shader4")
1742 && FindProcShort (glProgramParameteriEXT))
37eb4787 1743 {
01ca42b2 1744 extGS = (OpenGl_ExtGS* )(&(*myFuncs));
37eb4787 1745 }
1746
01ca42b2 1747 if (!has12)
86325709 1748 {
1749 myGlVerMajor = 1;
1750 myGlVerMinor = 1;
1751 return;
5f8b738e 1752 }
01ca42b2 1753 else if (!has13)
5f8b738e 1754 {
01ca42b2 1755 myGlVerMajor = 1;
1756 myGlVerMinor = 2;
1757 return;
5f8b738e 1758 }
01ca42b2 1759 else if (!has14)
5f8b738e 1760 {
86325709 1761 myGlVerMajor = 1;
01ca42b2 1762 myGlVerMinor = 3;
86325709 1763 return;
5f8b738e 1764 }
01ca42b2 1765 else if (!has15)
86325709 1766 {
01ca42b2 1767 myGlVerMajor = 1;
1768 myGlVerMinor = 4;
1769 return;
5f8b738e 1770 }
01ca42b2 1771 core15 = (OpenGl_GlCore15* )(&(*myFuncs));
1772 core15fwd = (OpenGl_GlCore15Fwd* )(&(*myFuncs));
1773
1774 if (!has20)
86325709 1775 {
1776 myGlVerMajor = 1;
01ca42b2 1777 myGlVerMinor = 5;
86325709 1778 return;
5f8b738e 1779 }
01ca42b2 1780
1781 core20 = (OpenGl_GlCore20* )(&(*myFuncs));
1782 core20fwd = (OpenGl_GlCore20Fwd* )(&(*myFuncs));
1783
1784 if (!has21)
5f8b738e 1785 {
01ca42b2 1786 myGlVerMajor = 2;
1787 myGlVerMinor = 0;
1788 return;
86325709 1789 }
01ca42b2 1790
1791 if (!has30)
86325709 1792 {
01ca42b2 1793 myGlVerMajor = 2;
1794 myGlVerMinor = 1;
86325709 1795 return;
1796 }
01ca42b2 1797
1798 if (!has31)
1799 {
1800 myGlVerMajor = 3;
1801 myGlVerMinor = 0;
1802 return;
1803 }
1804 arbTBO = (OpenGl_ArbTBO* )(&(*myFuncs));
1805 arbIns = (OpenGl_ArbIns* )(&(*myFuncs));
1806
1807 if (!has32)
86325709 1808 {
01ca42b2 1809 myGlVerMajor = 3;
1810 myGlVerMinor = 1;
1811 return;
86325709 1812 }
01ca42b2 1813 core32 = (OpenGl_GlCore32* )(&(*myFuncs));
1814 core32back = (OpenGl_GlCore32Back* )(&(*myFuncs));
1815
1816 if (!has33)
86325709 1817 {
01ca42b2 1818 myGlVerMajor = 3;
1819 myGlVerMinor = 2;
1820 return;
86325709 1821 }
01ca42b2 1822
1823 if (!has40)
86325709 1824 {
01ca42b2 1825 myGlVerMajor = 3;
1826 myGlVerMinor = 3;
1827 return;
1828 }
1829
1830 if (!has41)
1831 {
1832 myGlVerMajor = 4;
1833 myGlVerMinor = 0;
1834 return;
1835 }
1836 core41 = (OpenGl_GlCore41* )(&(*myFuncs));
1837 core41back = (OpenGl_GlCore41Back* )(&(*myFuncs));
1838
1839 if(!has42)
1840 {
1841 myGlVerMajor = 4;
1842 myGlVerMinor = 1;
1843 return;
1844 }
1845 core42 = (OpenGl_GlCore42* )(&(*myFuncs));
1846 core42back = (OpenGl_GlCore42Back* )(&(*myFuncs));
1847
1848 if(!has43)
1849 {
1850 myGlVerMajor = 4;
1851 myGlVerMinor = 2;
1852 return;
1853 }
1854 core43 = (OpenGl_GlCore43* )(&(*myFuncs));
1855 core43back = (OpenGl_GlCore43Back* )(&(*myFuncs));
1856
1857 if (!has44)
1858 {
1859 myGlVerMajor = 4;
1860 myGlVerMinor = 3;
1861 return;
5f8b738e 1862 }
01ca42b2 1863 core44 = (OpenGl_GlCore44* )(&(*myFuncs));
1864 core44back = (OpenGl_GlCore44Back* )(&(*myFuncs));
ca3c13d1 1865#endif
2166f0fa 1866}
f0430952 1867
1868// =======================================================================
1869// function : MemoryInfo
1870// purpose :
1871// =======================================================================
1872Standard_Size OpenGl_Context::AvailableMemory() const
1873{
ca3c13d1 1874#if !defined(GL_ES_VERSION_2_0)
f0430952 1875 if (atiMem)
1876 {
1877 // this is actually information for VBO pool
1878 // however because pools are mostly shared
1879 // it can be used for total GPU memory estimations
1880 GLint aMemInfo[4];
1881 aMemInfo[0] = 0;
1882 glGetIntegerv (GL_VBO_FREE_MEMORY_ATI, aMemInfo);
1883 // returned value is in KiB, however this maybe changed in future
1884 return Standard_Size(aMemInfo[0]) * 1024;
1885 }
1886 else if (nvxMem)
1887 {
1888 // current available dedicated video memory (in KiB), currently unused GPU memory
1889 GLint aMemInfo = 0;
1890 glGetIntegerv (GL_GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX, &aMemInfo);
1891 return Standard_Size(aMemInfo) * 1024;
1892 }
ca3c13d1 1893#endif
f0430952 1894 return 0;
1895}
1896
1897// =======================================================================
1898// function : MemoryInfo
1899// purpose :
1900// =======================================================================
1901TCollection_AsciiString OpenGl_Context::MemoryInfo() const
1902{
1903 TCollection_AsciiString anInfo;
ca3c13d1 1904#if !defined(GL_ES_VERSION_2_0)
f0430952 1905 if (atiMem)
1906 {
1907 GLint aValues[4];
1908 memset (aValues, 0, sizeof(aValues));
1909 glGetIntegerv (GL_VBO_FREE_MEMORY_ATI, aValues);
1910
1911 // total memory free in the pool
1912 anInfo += TCollection_AsciiString (" GPU free memory: ") + (aValues[0] / 1024) + " MiB\n";
1913
1914 // largest available free block in the pool
1915 anInfo += TCollection_AsciiString (" Largest free block: ") + (aValues[1] / 1024) + " MiB\n";
1916 if (aValues[2] != aValues[0])
1917 {
1918 // total auxiliary memory free
1919 anInfo += TCollection_AsciiString (" Free memory: ") + (aValues[2] / 1024) + " MiB\n";
1920 }
1921 }
1922 else if (nvxMem)
1923 {
1924 //current available dedicated video memory (in KiB), currently unused GPU memory
1925 GLint aValue = 0;
1926 glGetIntegerv (GL_GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX, &aValue);
1927 anInfo += TCollection_AsciiString (" GPU free memory: ") + (aValue / 1024) + " MiB\n";
1928
1929 // dedicated video memory, total size (in KiB) of the GPU memory
1930 GLint aDedicated = 0;
1931 glGetIntegerv (GL_GPU_MEMORY_INFO_DEDICATED_VIDMEM_NVX, &aDedicated);
1932 anInfo += TCollection_AsciiString (" GPU memory: ") + (aDedicated / 1024) + " MiB\n";
1933
1934 // total available memory, total size (in KiB) of the memory available for allocations
1935 glGetIntegerv (GL_GPU_MEMORY_INFO_TOTAL_AVAILABLE_MEMORY_NVX, &aValue);
1936 if (aValue != aDedicated)
1937 {
1938 // different only for special configurations
1939 anInfo += TCollection_AsciiString (" Total memory: ") + (aValue / 1024) + " MiB\n";
1940 }
1941 }
ca3c13d1 1942#endif
f0430952 1943 return anInfo;
1944}
5e27df78 1945
1946
1947// =======================================================================
1948// function : GetResource
1949// purpose :
1950// =======================================================================
1951const Handle(OpenGl_Resource)& OpenGl_Context::GetResource (const TCollection_AsciiString& theKey) const
1952{
1953 return mySharedResources->IsBound (theKey) ? mySharedResources->Find (theKey) : NULL_GL_RESOURCE;
1954}
1955
1956// =======================================================================
1957// function : ShareResource
1958// purpose :
1959// =======================================================================
1960Standard_Boolean OpenGl_Context::ShareResource (const TCollection_AsciiString& theKey,
1961 const Handle(OpenGl_Resource)& theResource)
1962{
1963 if (theKey.IsEmpty() || theResource.IsNull())
1964 {
1965 return Standard_False;
1966 }
1967 return mySharedResources->Bind (theKey, theResource);
1968}
1969
1970// =======================================================================
1971// function : ReleaseResource
1972// purpose :
1973// =======================================================================
a174a3c5 1974void OpenGl_Context::ReleaseResource (const TCollection_AsciiString& theKey,
1975 const Standard_Boolean theToDelay)
5e27df78 1976{
1977 if (!mySharedResources->IsBound (theKey))
1978 {
1979 return;
1980 }
1981 const Handle(OpenGl_Resource)& aRes = mySharedResources->Find (theKey);
1982 if (aRes->GetRefCount() > 1)
1983 {
1984 return;
1985 }
1986
a174a3c5 1987 if (theToDelay)
1988 {
1989 myDelayed->Bind (theKey, 1);
1990 }
1991 else
1992 {
1993 aRes->Release (this);
1994 mySharedResources->UnBind (theKey);
1995 }
5e27df78 1996}
1997
1998// =======================================================================
1999// function : DelayedRelease
2000// purpose :
2001// =======================================================================
2002void OpenGl_Context::DelayedRelease (Handle(OpenGl_Resource)& theResource)
2003{
3125ebb6 2004 myUnusedResources->Prepend (theResource);
5e27df78 2005 theResource.Nullify();
2006}
2007
2008// =======================================================================
2009// function : ReleaseDelayed
2010// purpose :
2011// =======================================================================
2012void OpenGl_Context::ReleaseDelayed()
2013{
a174a3c5 2014 // release queued elements
3125ebb6 2015 while (!myUnusedResources->IsEmpty())
5e27df78 2016 {
3125ebb6 2017 myUnusedResources->First()->Release (this);
2018 myUnusedResources->RemoveFirst();
5e27df78 2019 }
a174a3c5 2020
265d4508 2021 // release delayed shared resources
a174a3c5 2022 NCollection_Vector<TCollection_AsciiString> aDeadList;
2023 for (NCollection_DataMap<TCollection_AsciiString, Standard_Integer>::Iterator anIter (*myDelayed);
2024 anIter.More(); anIter.Next())
2025 {
2026 if (++anIter.ChangeValue() <= 2)
2027 {
2028 continue; // postpone release one more frame to ensure noone use it periodically
2029 }
2030
2031 const TCollection_AsciiString& aKey = anIter.Key();
2032 if (!mySharedResources->IsBound (aKey))
2033 {
2034 // mixed unshared strategy delayed/undelayed was used!
2035 aDeadList.Append (aKey);
2036 continue;
2037 }
2038
2039 Handle(OpenGl_Resource)& aRes = mySharedResources->ChangeFind (aKey);
2040 if (aRes->GetRefCount() > 1)
2041 {
2042 // should be only 1 instance in mySharedResources
2043 // if not - resource was reused again
2044 aDeadList.Append (aKey);
2045 continue;
2046 }
2047
2048 // release resource if no one requiested it more than 2 redraw calls
2049 aRes->Release (this);
2050 mySharedResources->UnBind (aKey);
2051 aDeadList.Append (aKey);
2052 }
2053
2054 for (Standard_Integer anIter = 0; anIter < aDeadList.Length(); ++anIter)
2055 {
2056 myDelayed->UnBind (aDeadList.Value (anIter));
2057 }
5e27df78 2058}
7d3e64ef 2059
2060// =======================================================================
2061// function : BindProgram
2062// purpose :
2063// =======================================================================
2064void OpenGl_Context::BindProgram (const Handle(OpenGl_ShaderProgram)& theProgram)
2065{
2066 if (theProgram.IsNull()
2067 || !theProgram->IsValid())
2068 {
2069 if (!myActiveProgram.IsNull())
2070 {
2071 core20fwd->glUseProgram (OpenGl_ShaderProgram::NO_PROGRAM);
2072 myActiveProgram.Nullify();
2073 }
2074 return;
2075 }
2076
2077 myActiveProgram = theProgram;
2078 core20fwd->glUseProgram (theProgram->ProgramId());
2079}