1 // Created on: 2012-01-26
2 // Created by: Kirill GAVRILOV
3 // Copyright (c) 2012-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
20 #include <OpenGl_Context.hxx>
22 #include <OpenGl_ArbTBO.hxx>
23 #include <OpenGl_ArbIns.hxx>
24 #include <OpenGl_ArbDbg.hxx>
25 #include <OpenGl_ArbFBO.hxx>
26 #include <OpenGl_ExtGS.hxx>
27 #include <OpenGl_ArbSamplerObject.hxx>
28 #include <OpenGl_ArbTexBindless.hxx>
29 #include <OpenGl_GlCore46.hxx>
30 #include <OpenGl_FrameBuffer.hxx>
31 #include <OpenGl_FrameStats.hxx>
32 #include <OpenGl_Sampler.hxx>
33 #include <OpenGl_ShaderManager.hxx>
34 #include <OpenGl_TextureSetPairIterator.hxx>
35 #include <OpenGl_Workspace.hxx>
36 #include <OpenGl_Aspects.hxx>
38 #include <Graphic3d_TransformUtils.hxx>
39 #include <Graphic3d_RenderingParams.hxx>
40 #include <Image_SupportedFormats.hxx>
41 #include <Message_Messenger.hxx>
42 #include <NCollection_Vector.hxx>
43 #include <Standard_ProgramError.hxx>
44 #include <Standard_WarningDisableFunctionCast.hxx>
46 #if defined(_WIN32) && defined(max)
51 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Context,Standard_Transient)
56 #pragma comment(lib, "libEGL.lib")
60 #elif defined(HAVE_XLIB)
61 #include <GL/glx.h> // glXGetProcAddress()
62 #elif defined(__APPLE__)
64 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
67 #include <OpenGL/OpenGL.h>
68 #include <CoreGraphics/CoreGraphics.h>
75 #include <emscripten.h>
76 #include <emscripten/html5.h>
81 static const Handle(OpenGl_Resource) NULL_GL_RESOURCE;
82 static const OpenGl_Mat4 THE_IDENTITY_MATRIX;
84 //! Add key-value pair to the dictionary.
85 static void addInfo (TColStd_IndexedDataMapOfStringString& theDict,
86 const TCollection_AsciiString& theKey,
87 const TCollection_AsciiString& theValue)
89 theDict.ChangeFromIndex (theDict.Add (theKey, theValue)) = theValue;
92 //! Add key-value pair to the dictionary.
93 static void addInfo (TColStd_IndexedDataMapOfStringString& theDict,
94 const TCollection_AsciiString& theKey,
97 TCollection_AsciiString aValue (theValue != NULL ? theValue : "");
98 theDict.ChangeFromIndex (theDict.Add (theKey, aValue)) = aValue;
102 // =======================================================================
103 // function : OpenGl_Context
105 // =======================================================================
106 OpenGl_Context::OpenGl_Context (const Handle(OpenGl_Caps)& theCaps)
122 caps (!theCaps.IsNull() ? theCaps : new OpenGl_Caps()),
123 hasGetBufferData (Standard_False),
124 #if defined(GL_ES_VERSION_2_0)
125 hasPackRowLength (Standard_False),
126 hasUnpackRowLength (Standard_False),
127 hasHighp (Standard_False),
128 hasUintIndex(Standard_False),
129 hasTexRGBA8(Standard_False),
131 hasPackRowLength (Standard_True),
132 hasUnpackRowLength (Standard_True),
133 hasHighp (Standard_True),
134 hasUintIndex(Standard_True),
135 hasTexRGBA8(Standard_True),
137 hasTexFloatLinear (Standard_False),
138 hasTexSRGB (Standard_False),
139 hasFboSRGB (Standard_False),
140 hasSRGBControl (Standard_False),
141 hasFboRenderMipmap (Standard_False),
142 #if defined(GL_ES_VERSION_2_0)
143 hasFlatShading (OpenGl_FeatureNotAvailable),
145 hasFlatShading (OpenGl_FeatureInCore),
147 hasGlslBitwiseOps (OpenGl_FeatureNotAvailable),
148 hasDrawBuffers (OpenGl_FeatureNotAvailable),
149 hasFloatBuffer (OpenGl_FeatureNotAvailable),
150 hasHalfFloatBuffer (OpenGl_FeatureNotAvailable),
151 hasSampleVariables (OpenGl_FeatureNotAvailable),
152 hasGeometryStage (OpenGl_FeatureNotAvailable),
153 arbDrawBuffers (Standard_False),
154 arbNPTW (Standard_False),
155 arbTexRG (Standard_False),
156 arbTexFloat (Standard_False),
157 arbSamplerObject (NULL),
158 arbTexBindless (NULL),
160 arbTboRGB32 (Standard_False),
161 arbClipControl (Standard_False),
166 arbSampleShading (Standard_False),
167 arbDepthClamp (Standard_False),
168 extFragDepth (Standard_False),
169 extDrawBuffers (Standard_False),
171 extBgra(Standard_False),
172 extAnis(Standard_False),
173 extPDS (Standard_False),
174 atiMem (Standard_False),
175 nvxMem (Standard_False),
176 oesSampleVariables (Standard_False),
177 oesStdDerivatives (Standard_False),
181 mySharedResources (new OpenGl_ResourcesMap()),
182 myDelayed (new OpenGl_DelayReleaseMap()),
183 myUnusedResources (new OpenGl_ResourcesStack()),
185 myGlLibHandle (NULL),
186 myFuncs (new OpenGl_GlFunctions()),
187 mySupportedFormats (new Image_SupportedFormats()),
189 myTexClamp (GL_CLAMP_TO_EDGE),
191 myMaxTexCombined (1),
192 myMaxTexUnitsFFP (1),
193 myMaxDumpSizeX (1024),
194 myMaxDumpSizeY (1024),
197 myMaxDrawBuffers (1),
198 myMaxColorAttachments (1),
201 myIsInitialized (Standard_False),
202 myIsStereoBuffers (Standard_False),
203 myIsGlNormalizeEnabled (Standard_False),
204 mySpriteTexUnit (Graphic3d_TextureUnit_PointSprite),
205 myHasRayTracing (Standard_False),
206 myHasRayTracingTextures (Standard_False),
207 myHasRayTracingAdaptiveSampling (Standard_False),
208 myHasRayTracingAdaptiveSamplingAtomic (Standard_False),
209 myHasPBR (Standard_False),
210 myPBREnvLUTTexUnit (Graphic3d_TextureUnit_PbrEnvironmentLUT),
211 myPBRDiffIBLMapSHTexUnit (Graphic3d_TextureUnit_PbrIblDiffuseSH),
212 myPBRSpecIBLMapTexUnit (Graphic3d_TextureUnit_PbrIblSpecular),
213 myShadowMapTexUnit (Graphic3d_TextureUnit_ShadowMap),
214 myDepthPeelingDepthTexUnit (Graphic3d_TextureUnit_DepthPeelingDepth),
215 myDepthPeelingFrontColorTexUnit (Graphic3d_TextureUnit_DepthPeelingFrontColor),
216 myFrameStats (new OpenGl_FrameStats()),
217 myActiveMockTextures (0),
218 myActiveHatchType (Aspect_HS_SOLID),
219 myHatchIsEnabled (false),
220 #if !defined(GL_ES_VERSION_2_0)
221 myPointSpriteOrig (GL_UPPER_LEFT),
222 myRenderMode (GL_RENDER),
223 myShadeModel (GL_SMOOTH),
224 myPolygonMode (GL_FILL),
226 myPointSpriteOrig (0),
231 myToCullBackFaces (false),
233 myDrawBuffers (0, 7),
236 myAlphaToCoverage (false),
237 myIsGlDebugCtx (false),
238 myIsSRgbWindow (false),
239 myResolution (Graphic3d_RenderingParams::THE_DEFAULT_RESOLUTION),
240 myResolutionRatio (1.0f),
241 myLineWidthScale (1.0f),
242 myLineFeather (1.0f),
243 myRenderScale (1.0f),
244 myRenderScaleInv (1.0f)
250 myViewportVirt[0] = 0;
251 myViewportVirt[1] = 0;
252 myViewportVirt[2] = 0;
253 myViewportVirt[3] = 0;
255 myPolygonOffset.Mode = Aspect_POM_Off;
256 myPolygonOffset.Factor = 0.0f;
257 myPolygonOffset.Units = 0.0f;
259 // system-dependent fields
260 #if defined(HAVE_EGL)
261 myDisplay = (Aspect_Display )EGL_NO_DISPLAY;
262 myWindow = (Aspect_Drawable )EGL_NO_SURFACE;
263 myGContext = (Aspect_RenderingContext )EGL_NO_CONTEXT;
264 #elif defined(__APPLE__) && !defined(HAVE_XLIB)
265 // Vendors can not extend functionality on this system
266 // and developers are limited to OpenGL support provided by Mac OS X SDK.
267 // We retrieve function pointers from system library
268 // to generalize extensions support on all platforms.
269 // In this way we also reach binary compatibility benefit between OS releases
270 // if some newest functionality is optionally used.
271 // Notice that GL version / extension availability checks are required
272 // because function pointers may be available but not functionality itself
273 // (depends on renderer).
274 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
275 myGlLibHandle = dlopen ("/System/Library/Frameworks/OpenGLES.framework/OpenGLES", RTLD_LAZY);
277 myGlLibHandle = dlopen ("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL", RTLD_LAZY);
281 memset (myFuncs.operator->(), 0, sizeof(OpenGl_GlFunctions));
282 myShaderManager = new OpenGl_ShaderManager (this);
285 // =======================================================================
286 // function : ~OpenGl_Context
288 // =======================================================================
289 OpenGl_Context::~OpenGl_Context()
291 // release clean up queue
294 #if !defined(GL_ES_VERSION_2_0)
295 // release default VAO
296 if (myDefaultVao != 0
300 core32->glDeleteVertexArrays (1, &myDefaultVao);
305 // release mock textures
306 if (!myTextureRgbaBlack.IsNull())
308 myTextureRgbaBlack->Release (this);
309 myTextureRgbaBlack.Nullify();
311 if (!myTextureRgbaWhite.IsNull())
313 myTextureRgbaWhite->Release (this);
314 myTextureRgbaWhite.Nullify();
317 // release default FBO
318 if (!myDefaultFbo.IsNull())
320 myDefaultFbo->Release (this);
321 myDefaultFbo.Nullify();
324 // release shared resources if any
325 if (mySharedResources->GetRefCount() <= 1)
327 myShaderManager.Nullify();
328 for (NCollection_DataMap<TCollection_AsciiString, Handle(OpenGl_Resource)>::Iterator anIter (*mySharedResources);
329 anIter.More(); anIter.Next())
331 anIter.Value()->Release (this);
334 // release delayed resources added during deletion of shared resources
335 while (!myUnusedResources->IsEmpty())
337 myUnusedResources->First()->Release (this);
338 myUnusedResources->RemoveFirst();
341 else if (myShaderManager->IsSameContext (this))
343 myShaderManager->SetContext (NULL);
345 mySharedResources.Nullify();
353 #if !defined(GL_ES_VERSION_2_0)
355 glGetPointerv (GL_DEBUG_CALLBACK_USER_PARAM, &aPtr);
359 arbDbg->glDebugMessageCallback (NULL, NULL);
361 myIsGlDebugCtx = Standard_False;
365 // =======================================================================
366 // function : forcedRelease
368 // =======================================================================
369 void OpenGl_Context::forcedRelease()
372 for (NCollection_DataMap<TCollection_AsciiString, Handle(OpenGl_Resource)>::Iterator anIter (*mySharedResources);
373 anIter.More(); anIter.Next())
375 anIter.Value()->Release (this);
377 mySharedResources->Clear();
378 myShaderManager->clear();
379 myShaderManager->SetContext (NULL);
381 // release delayed resources added during deletion of shared resources
382 while (!myUnusedResources->IsEmpty())
384 myUnusedResources->First()->Release (this);
385 myUnusedResources->RemoveFirst();
389 // =======================================================================
390 // function : ResizeViewport
392 // =======================================================================
393 void OpenGl_Context::ResizeViewport (const Standard_Integer* theRect)
395 core11fwd->glViewport (theRect[0], theRect[1], theRect[2], theRect[3]);
396 myViewport[0] = theRect[0];
397 myViewport[1] = theRect[1];
398 myViewport[2] = theRect[2];
399 myViewport[3] = theRect[3];
400 if (HasRenderScale())
402 myViewportVirt[0] = Standard_Integer(theRect[0] * myRenderScaleInv);
403 myViewportVirt[1] = Standard_Integer(theRect[1] * myRenderScaleInv);
404 myViewportVirt[2] = Standard_Integer(theRect[2] * myRenderScaleInv);
405 myViewportVirt[3] = Standard_Integer(theRect[3] * myRenderScaleInv);
409 myViewportVirt[0] = theRect[0];
410 myViewportVirt[1] = theRect[1];
411 myViewportVirt[2] = theRect[2];
412 myViewportVirt[3] = theRect[3];
416 #if !defined(GL_ES_VERSION_2_0)
417 inline Standard_Integer stereoToMonoBuffer (const Standard_Integer theBuffer)
433 // =======================================================================
434 // function : SetReadBuffer
436 // =======================================================================
437 void OpenGl_Context::SetReadBuffer (const Standard_Integer theReadBuffer)
439 #if !defined(GL_ES_VERSION_2_0)
440 myReadBuffer = !myIsStereoBuffers ? stereoToMonoBuffer (theReadBuffer) : theReadBuffer;
441 if (myReadBuffer < GL_COLOR_ATTACHMENT0
444 arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
446 ::glReadBuffer (myReadBuffer);
448 (void )theReadBuffer;
452 // =======================================================================
453 // function : SetDrawBuffer
455 // =======================================================================
456 void OpenGl_Context::SetDrawBuffer (const Standard_Integer theDrawBuffer)
458 #if !defined(GL_ES_VERSION_2_0)
459 const Standard_Integer aDrawBuffer = !myIsStereoBuffers ? stereoToMonoBuffer (theDrawBuffer) : theDrawBuffer;
460 if (aDrawBuffer < GL_COLOR_ATTACHMENT0
463 arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
465 ::glDrawBuffer (aDrawBuffer);
467 myDrawBuffers.Init (GL_NONE);
468 myDrawBuffers.SetValue (0, aDrawBuffer);
470 (void )theDrawBuffer;
474 // =======================================================================
475 // function : SetDrawBuffers
477 // =======================================================================
478 void OpenGl_Context::SetDrawBuffers (const Standard_Integer theNb, const Standard_Integer* theDrawBuffers)
480 Standard_ASSERT_RETURN (hasDrawBuffers, "Multiple draw buffers feature is not supported by the context", Standard_ASSERT_DO_NOTHING());
482 if (myDrawBuffers.Length() < theNb)
484 // should actually never happen here
485 myDrawBuffers.Resize (0, theNb - 1, false);
487 myDrawBuffers.Init (GL_NONE);
489 Standard_Boolean useDefaultFbo = Standard_False;
490 for (Standard_Integer anI = 0; anI < theNb; ++anI)
492 if (theDrawBuffers[anI] < GL_COLOR_ATTACHMENT0 && theDrawBuffers[anI] != GL_NONE)
494 useDefaultFbo = Standard_True;
498 myDrawBuffers.SetValue (anI, theDrawBuffers[anI]);
501 if (arbFBO != NULL && useDefaultFbo)
503 arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
506 myFuncs->glDrawBuffers (theNb, (const GLenum*)theDrawBuffers);
509 // =======================================================================
510 // function : SetFrameBufferSRGB
512 // =======================================================================
513 void OpenGl_Context::SetFrameBufferSRGB (bool theIsFbo, bool theIsFboSRgb)
517 myIsSRgbActive = false;
520 myIsSRgbActive = ToRenderSRGB()
521 && (theIsFbo || myIsSRgbWindow)
530 core11fwd->glEnable (GL_FRAMEBUFFER_SRGB);
534 core11fwd->glDisable (GL_FRAMEBUFFER_SRGB);
538 // =======================================================================
539 // function : SetCullBackFaces
541 // =======================================================================
542 void OpenGl_Context::SetCullBackFaces (bool theToEnable)
544 if (myToCullBackFaces == theToEnable)
549 myToCullBackFaces = theToEnable;
552 //glCullFace (GL_BACK); GL_BACK by default
553 core11fwd->glEnable (GL_CULL_FACE);
557 core11fwd->glDisable (GL_CULL_FACE);
561 // =======================================================================
562 // function : FetchState
564 // =======================================================================
565 void OpenGl_Context::FetchState()
567 #if !defined(GL_ES_VERSION_2_0)
568 // cache feedback mode state
569 if (core11ffp != NULL)
571 ::glGetIntegerv (GL_RENDER_MODE, &myRenderMode);
572 ::glGetIntegerv (GL_SHADE_MODEL, &myShadeModel);
575 // cache read buffers state
576 ::glGetIntegerv (GL_READ_BUFFER, &myReadBuffer);
578 // cache draw buffers state
579 if (myDrawBuffers.Length() < myMaxDrawBuffers)
581 myDrawBuffers.Resize (0, myMaxDrawBuffers - 1, false);
583 myDrawBuffers.Init (GL_NONE);
585 Standard_Integer aDrawBuffer = GL_NONE;
586 if (myMaxDrawBuffers == 1)
588 ::glGetIntegerv (GL_DRAW_BUFFER, &aDrawBuffer);
589 myDrawBuffers.SetValue (0, aDrawBuffer);
593 for (Standard_Integer anI = 0; anI < myMaxDrawBuffers; ++anI)
595 ::glGetIntegerv (GL_DRAW_BUFFER0 + anI, &aDrawBuffer);
596 myDrawBuffers.SetValue (anI, aDrawBuffer);
602 // =======================================================================
605 // =======================================================================
606 void OpenGl_Context::Share (const Handle(OpenGl_Context)& theShareCtx)
608 if (!theShareCtx.IsNull())
610 mySharedResources = theShareCtx->mySharedResources;
611 myDelayed = theShareCtx->myDelayed;
612 myUnusedResources = theShareCtx->myUnusedResources;
613 myShaderManager = theShareCtx->myShaderManager;
617 #if !defined(__APPLE__) || defined(HAVE_XLIB)
619 // =======================================================================
620 // function : IsCurrent
622 // =======================================================================
623 Standard_Boolean OpenGl_Context::IsCurrent() const
625 #if defined(HAVE_EGL)
626 if ((EGLDisplay )myDisplay == EGL_NO_DISPLAY
627 || (EGLContext )myGContext == EGL_NO_CONTEXT)
629 return Standard_False;
632 return (((EGLDisplay )myDisplay == eglGetCurrentDisplay())
633 && ((EGLContext )myGContext == eglGetCurrentContext())
634 && ((EGLSurface )myWindow == eglGetCurrentSurface (EGL_DRAW)));
635 #elif defined(_WIN32)
636 if (myDisplay == NULL || myGContext == NULL)
638 return Standard_False;
640 return (( (HDC )myDisplay == wglGetCurrentDC())
641 && ((HGLRC )myGContext == wglGetCurrentContext()));
642 #elif defined(HAVE_XLIB)
643 if (myDisplay == NULL || myWindow == 0 || myGContext == 0)
645 return Standard_False;
648 return ( ((Display* )myDisplay == glXGetCurrentDisplay())
649 && ((GLXContext )myGContext == glXGetCurrentContext())
650 && ((GLXDrawable )myWindow == glXGetCurrentDrawable()));
652 return Standard_False;
656 // =======================================================================
657 // function : MakeCurrent
659 // =======================================================================
660 Standard_Boolean OpenGl_Context::MakeCurrent()
662 #if defined(HAVE_EGL)
663 if ((EGLDisplay )myDisplay == EGL_NO_DISPLAY
664 || (EGLContext )myGContext == EGL_NO_CONTEXT)
666 Standard_ProgramError_Raise_if (myIsInitialized, "OpenGl_Context::Init() should be called before!");
667 return Standard_False;
670 if (eglMakeCurrent ((EGLDisplay )myDisplay, (EGLSurface )myWindow, (EGLSurface )myWindow, (EGLContext )myGContext) != EGL_TRUE)
672 // if there is no current context it might be impossible to use glGetError() correctly
673 PushMessage (GL_DEBUG_SOURCE_WINDOW_SYSTEM, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH,
674 "eglMakeCurrent() has failed!");
675 myIsInitialized = Standard_False;
676 return Standard_False;
678 #elif defined(_WIN32)
679 if (myDisplay == NULL || myGContext == NULL)
681 Standard_ProgramError_Raise_if (myIsInitialized, "OpenGl_Context::Init() should be called before!");
682 return Standard_False;
685 // technically it should be safe to activate already bound GL context
686 // however some drivers (Intel etc.) may FAIL doing this for unknown reason
689 myShaderManager->SetContext (this);
690 return Standard_True;
692 else if (wglMakeCurrent ((HDC )myDisplay, (HGLRC )myGContext) != TRUE)
694 // notice that glGetError() couldn't be used here!
695 wchar_t* aMsgBuff = NULL;
696 DWORD anErrorCode = GetLastError();
697 FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
698 NULL, anErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (wchar_t* )&aMsgBuff, 0, NULL);
699 TCollection_ExtendedString aMsg ("wglMakeCurrent() has failed. ");
700 if (aMsgBuff != NULL)
702 aMsg += (Standard_ExtString )aMsgBuff;
703 LocalFree (aMsgBuff);
705 PushMessage (GL_DEBUG_SOURCE_WINDOW_SYSTEM, GL_DEBUG_TYPE_ERROR, (unsigned int )anErrorCode, GL_DEBUG_SEVERITY_HIGH, aMsg);
706 myIsInitialized = Standard_False;
707 return Standard_False;
709 #elif defined(HAVE_XLIB)
710 if (myDisplay == NULL || myWindow == 0 || myGContext == 0)
712 Standard_ProgramError_Raise_if (myIsInitialized, "OpenGl_Context::Init() should be called before!");
713 return Standard_False;
716 if (!glXMakeCurrent ((Display* )myDisplay, (GLXDrawable )myWindow, (GLXContext )myGContext))
718 // if there is no current context it might be impossible to use glGetError() correctly
719 PushMessage (GL_DEBUG_SOURCE_WINDOW_SYSTEM, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH,
720 "glXMakeCurrent() has failed!");
721 myIsInitialized = Standard_False;
722 return Standard_False;
726 if (!myIsInitialized)
728 throw Standard_ProgramError ("OpenGl_Context::Init() should be called before!");
731 myShaderManager->SetContext (this);
732 return Standard_True;
735 // =======================================================================
736 // function : SwapBuffers
738 // =======================================================================
739 void OpenGl_Context::SwapBuffers()
741 #if defined(HAVE_EGL)
742 if ((EGLSurface )myWindow != EGL_NO_SURFACE)
744 eglSwapBuffers ((EGLDisplay )myDisplay, (EGLSurface )myWindow);
746 #elif defined(_WIN32)
747 if ((HDC )myDisplay != NULL)
749 ::SwapBuffers ((HDC )myDisplay);
752 #elif defined(HAVE_XLIB)
753 if ((Display* )myDisplay != NULL)
755 glXSwapBuffers ((Display* )myDisplay, (GLXDrawable )myWindow);
764 // =======================================================================
765 // function : SetSwapInterval
767 // =======================================================================
768 Standard_Boolean OpenGl_Context::SetSwapInterval (const Standard_Integer theInterval)
770 #if defined(HAVE_EGL)
771 if (::eglSwapInterval ((EGLDisplay )myDisplay, theInterval) == EGL_TRUE)
773 return Standard_True;
775 #elif defined(_WIN32)
776 if (myFuncs->wglSwapIntervalEXT != NULL)
778 myFuncs->wglSwapIntervalEXT (theInterval);
779 return Standard_True;
781 #elif defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
782 (void )theInterval; // vsync cannot be turned OFF on iOS
783 #elif defined(__APPLE__)
784 if (::CGLSetParameter (CGLGetCurrentContext(), kCGLCPSwapInterval, &theInterval) == kCGLNoError)
786 return Standard_True;
788 #elif defined(HAVE_XLIB)
789 if (theInterval == -1
790 && myFuncs->glXSwapIntervalEXT != NULL)
792 typedef int (*glXSwapIntervalEXT_t_x)(Display* theDisplay, GLXDrawable theDrawable, int theInterval);
793 glXSwapIntervalEXT_t_x aFuncPtr = (glXSwapIntervalEXT_t_x )myFuncs->glXSwapIntervalEXT;
794 aFuncPtr ((Display* )myDisplay, (GLXDrawable )myWindow, theInterval);
795 return Standard_True;
797 else if (myFuncs->glXSwapIntervalSGI != NULL)
799 myFuncs->glXSwapIntervalSGI (theInterval);
800 return Standard_True;
805 return Standard_False;
808 // =======================================================================
809 // function : findProc
811 // =======================================================================
812 void* OpenGl_Context::findProc (const char* theFuncName)
814 #if defined(HAVE_EGL)
815 return (void* )eglGetProcAddress (theFuncName);
816 #elif defined(_WIN32)
817 return (void* )wglGetProcAddress (theFuncName);
818 #elif defined(HAVE_XLIB)
819 return (void* )glXGetProcAddress ((const GLubyte* )theFuncName);
820 #elif defined(__APPLE__)
821 return (myGlLibHandle != NULL) ? dlsym (myGlLibHandle, theFuncName) : NULL;
828 // =======================================================================
829 // function : CheckExtension
831 // =======================================================================
832 Standard_Boolean OpenGl_Context::CheckExtension (const char* theExtName) const
834 if (theExtName == NULL)
837 std::cerr << "CheckExtension called with NULL string!\n";
839 return Standard_False;
841 else if (caps->contextNoExtensions)
843 return Standard_False;
846 #if !defined(GL_ES_VERSION_2_0)
847 // available since OpenGL 3.0
848 // and the ONLY way to check extensions with OpenGL 3.1+ core profile
849 if (IsGlGreaterEqual (3, 0)
850 && myFuncs->glGetStringi != NULL)
853 ::glGetIntegerv (GL_NUM_EXTENSIONS, &anExtNb);
854 const size_t anExtNameLen = strlen (theExtName);
855 for (GLint anIter = 0; anIter < anExtNb; ++anIter)
857 const char* anExtension = (const char* )myFuncs->glGetStringi (GL_EXTENSIONS, (GLuint )anIter);
858 const size_t aTestExtNameLen = strlen (anExtension);
859 if (aTestExtNameLen == anExtNameLen
860 && strncmp (anExtension, theExtName, anExtNameLen) == 0)
862 return Standard_True;
865 return Standard_False;
869 // use old way with huge string for all extensions
870 const char* anExtString = (const char* )glGetString (GL_EXTENSIONS);
871 if (anExtString == NULL)
873 Messenger()->Send ("TKOpenGL: glGetString (GL_EXTENSIONS) has returned NULL! No GL context?", Message_Warning);
874 return Standard_False;
876 if (!CheckExtension (anExtString, theExtName))
878 return Standard_False;
881 #ifdef __EMSCRIPTEN__
882 //! Check if WebGL extension is available and activate it
883 //! (usage of extension without activation will generate errors).
884 if (EMSCRIPTEN_WEBGL_CONTEXT_HANDLE aWebGlCtx = emscripten_webgl_get_current_context())
886 if (emscripten_webgl_enable_extension (aWebGlCtx, theExtName))
888 return Standard_True;
891 return Standard_False;
893 return Standard_True;
897 // =======================================================================
898 // function : CheckExtension
900 // =======================================================================
901 Standard_Boolean OpenGl_Context::CheckExtension (const char* theExtString,
902 const char* theExtName)
904 if (theExtString == NULL)
906 return Standard_False;
909 // Search for theExtName in the extensions string.
910 // Use of strstr() is not sufficient because extension names can be prefixes of other extension names.
911 char* aPtrIter = (char* )theExtString;
912 const char* aPtrEnd = aPtrIter + strlen (theExtString);
913 const size_t anExtNameLen = strlen (theExtName);
914 while (aPtrIter < aPtrEnd)
916 const size_t n = strcspn (aPtrIter, " ");
917 if ((n == anExtNameLen) && (strncmp (aPtrIter, theExtName, anExtNameLen) == 0))
919 return Standard_True;
923 return Standard_False;
926 #if !defined(__APPLE__) || defined(HAVE_XLIB)
928 // =======================================================================
931 // =======================================================================
932 Standard_Boolean OpenGl_Context::Init (const Standard_Boolean theIsCoreProfile)
936 return Standard_True;
939 #if defined(HAVE_EGL)
940 myDisplay = (Aspect_Display )eglGetCurrentDisplay();
941 myGContext = (Aspect_RenderingContext )eglGetCurrentContext();
942 myWindow = (Aspect_Drawable )eglGetCurrentSurface(EGL_DRAW);
943 #elif defined(_WIN32)
944 myDisplay = (Aspect_Handle )wglGetCurrentDC();
945 myGContext = (Aspect_RenderingContext )wglGetCurrentContext();
946 #elif defined(HAVE_XLIB)
947 myDisplay = (Aspect_Display )glXGetCurrentDisplay();
948 myGContext = (Aspect_RenderingContext )glXGetCurrentContext();
949 myWindow = (Aspect_Drawable )glXGetCurrentDrawable();
953 if (myGContext == NULL)
955 return Standard_False;
958 init (theIsCoreProfile);
959 myIsInitialized = Standard_True;
960 return Standard_True;
965 // =======================================================================
968 // =======================================================================
969 Standard_Boolean OpenGl_Context::Init (const Aspect_Drawable theSurface,
970 const Aspect_Display theDisplay,
971 const Aspect_RenderingContext theContext,
972 const Standard_Boolean theIsCoreProfile)
974 Standard_ProgramError_Raise_if (myIsInitialized, "OpenGl_Context::Init() should be called only once!");
975 myWindow = theSurface;
976 myDisplay = theDisplay;
977 myGContext = theContext;
978 if (myGContext == NULL || !MakeCurrent())
980 return Standard_False;
983 init (theIsCoreProfile);
984 myIsInitialized = Standard_True;
985 return Standard_True;
988 // =======================================================================
989 // function : FormatGlEnumHex
991 // =======================================================================
992 TCollection_AsciiString OpenGl_Context::FormatGlEnumHex (int theGlEnum)
995 Sprintf (aBuff, theGlEnum < (int )std::numeric_limits<uint16_t>::max()
997 : "0x%08X", theGlEnum);
1001 // =======================================================================
1002 // function : FormatSize
1004 // =======================================================================
1005 TCollection_AsciiString OpenGl_Context::FormatSize (Standard_Size theSize)
1008 Sprintf (aBuff, "%" PRIu64, (uint64_t )theSize);
1012 // =======================================================================
1013 // function : FormatPointer
1015 // =======================================================================
1016 TCollection_AsciiString OpenGl_Context::FormatPointer (const void* thePtr)
1019 Sprintf (aBuff, "0x%" PRIXPTR, (uintptr_t )thePtr);
1023 // =======================================================================
1024 // function : FormatGlError
1026 // =======================================================================
1027 TCollection_AsciiString OpenGl_Context::FormatGlError (int theGlError)
1031 case GL_INVALID_ENUM: return "GL_INVALID_ENUM";
1032 case GL_INVALID_VALUE: return "GL_INVALID_VALUE";
1033 case GL_INVALID_OPERATION: return "GL_INVALID_OPERATION";
1034 #ifdef GL_STACK_OVERFLOW
1035 case GL_STACK_OVERFLOW: return "GL_STACK_OVERFLOW";
1036 case GL_STACK_UNDERFLOW: return "GL_STACK_UNDERFLOW";
1038 case GL_OUT_OF_MEMORY: return "GL_OUT_OF_MEMORY";
1039 case GL_INVALID_FRAMEBUFFER_OPERATION: return "GL_INVALID_FRAMEBUFFER_OPERATION";
1041 return FormatGlEnumHex (theGlError);
1044 // =======================================================================
1045 // function : ResetErrors
1047 // =======================================================================
1048 bool OpenGl_Context::ResetErrors (const bool theToPrintErrors)
1051 int anErr = ::glGetError();
1052 const bool hasError = anErr != GL_NO_ERROR;
1053 if (!theToPrintErrors)
1055 for (; anErr != GL_NO_ERROR && aPrevErr != anErr; aPrevErr = anErr, anErr = ::glGetError())
1062 for (; anErr != GL_NO_ERROR && aPrevErr != anErr; aPrevErr = anErr, anErr = ::glGetError())
1064 const TCollection_ExtendedString aMsg = TCollection_ExtendedString ("Unhandled GL error: ") + FormatGlError (anErr);
1065 PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_OTHER, 0, GL_DEBUG_SEVERITY_LOW, aMsg);
1070 // =======================================================================
1071 // function : debugPrintError
1073 // =======================================================================
1074 bool OpenGl_GlFunctions::debugPrintError (const char* theName) const
1076 const int anErr = ::glGetError();
1077 if (anErr != GL_NO_ERROR)
1079 Message::SendFail() << theName << "(), unhandled GL error: " << OpenGl_Context::FormatGlError (anErr);
1080 // there is no glSetError(), just emulate non-clear state
1083 case GL_INVALID_VALUE:
1085 ::glLineWidth(-1.0f);
1086 ::glLineWidth( 1.0f);
1090 case GL_INVALID_ENUM:
1092 ::glEnable (0xFFFF);
1097 return anErr != GL_NO_ERROR;
1100 // =======================================================================
1101 // function : ReadGlVersion
1103 // =======================================================================
1104 void OpenGl_Context::ReadGlVersion (Standard_Integer& theGlVerMajor,
1105 Standard_Integer& theGlVerMinor)
1111 bool toCheckVer3 = true;
1112 #if defined(__EMSCRIPTEN__)
1113 // WebGL 1.0 prints annoying invalid enumeration warnings to console.
1114 toCheckVer3 = false;
1115 if (EMSCRIPTEN_WEBGL_CONTEXT_HANDLE aWebGlCtx = emscripten_webgl_get_current_context())
1117 EmscriptenWebGLContextAttributes anAttribs = {};
1118 if (emscripten_webgl_get_context_attributes (aWebGlCtx, &anAttribs) == EMSCRIPTEN_RESULT_SUCCESS)
1120 toCheckVer3 = anAttribs.majorVersion >= 2;
1125 // Available since OpenGL 3.0 and OpenGL ES 3.0.
1128 GLint aMajor = 0, aMinor = 0;
1129 glGetIntegerv (GL_MAJOR_VERSION, &aMajor);
1130 glGetIntegerv (GL_MINOR_VERSION, &aMinor);
1131 // glGetError() sometimes does not report an error here even if
1132 // GL does not know GL_MAJOR_VERSION and GL_MINOR_VERSION constants.
1133 // This happens on some renderers like e.g. Cygwin MESA.
1134 // Thus checking additionally if GL has put anything to
1135 // the output variables.
1136 if (::glGetError() == GL_NO_ERROR && aMajor != 0 && aMinor != 0)
1138 theGlVerMajor = aMajor;
1139 theGlVerMinor = aMinor;
1142 for (GLenum anErr = ::glGetError(), aPrevErr = GL_NO_ERROR;; aPrevErr = anErr, anErr = ::glGetError())
1144 if (anErr == GL_NO_ERROR
1145 || anErr == aPrevErr)
1152 // Read version string.
1153 // Notice that only first two numbers split by point '2.1 XXXXX' are significant.
1154 // Following trash (after space) is vendor-specific.
1155 // New drivers also returns micro version of GL like '3.3.0' which has no meaning
1156 // and should be considered as vendor-specific too.
1157 const char* aVerStr = (const char* )glGetString (GL_VERSION);
1158 if (aVerStr == NULL || *aVerStr == '\0')
1160 // invalid GL context
1164 //#if defined(GL_ES_VERSION_2_0)
1165 // skip "OpenGL ES-** " section
1166 for (; *aVerStr != '\0'; ++aVerStr)
1168 if (*aVerStr >= '0' && *aVerStr <= '9')
1175 // parse string for major number
1178 size_t aMajIter = 0;
1179 while (aVerStr[aMajIter] >= '0' && aVerStr[aMajIter] <= '9')
1183 if (aMajIter == 0 || aMajIter >= sizeof(aMajorStr))
1187 memcpy (aMajorStr, aVerStr, aMajIter);
1188 aMajorStr[aMajIter] = '\0';
1190 // parse string for minor number
1191 aVerStr += aMajIter + 1;
1192 size_t aMinIter = 0;
1193 while (aVerStr[aMinIter] >= '0' && aVerStr[aMinIter] <= '9')
1197 if (aMinIter == 0 || aMinIter >= sizeof(aMinorStr))
1201 memcpy (aMinorStr, aVerStr, aMinIter);
1202 aMinorStr[aMinIter] = '\0';
1205 theGlVerMajor = atoi (aMajorStr);
1206 theGlVerMinor = atoi (aMinorStr);
1207 #if defined(__EMSCRIPTEN__)
1208 if (theGlVerMajor >= 3)
1211 || ::strstr (aVerStr, "WebGL 1.0") != NULL)
1213 Message::SendWarning() << "Warning! OpenGL context reports version " << theGlVerMajor << "." << theGlVerMinor
1214 << " but WebGL 2.0 was unavailable\n"
1215 << "Fallback to OpenGL ES 2.0 will be used instead of reported version";
1222 if (theGlVerMajor <= 0)
1229 static Standard_CString THE_DBGMSG_UNKNOWN = "UNKNOWN";
1230 static Standard_CString THE_DBGMSG_SOURCES[] =
1232 ".OpenGL", // GL_DEBUG_SOURCE_API
1233 ".WinSystem", // GL_DEBUG_SOURCE_WINDOW_SYSTEM
1234 ".GLSL", // GL_DEBUG_SOURCE_SHADER_COMPILER
1235 ".3rdParty", // GL_DEBUG_SOURCE_THIRD_PARTY
1236 "", // GL_DEBUG_SOURCE_APPLICATION
1237 ".Other" // GL_DEBUG_SOURCE_OTHER
1240 static Standard_CString THE_DBGMSG_TYPES[] =
1242 "Error", // GL_DEBUG_TYPE_ERROR
1243 "Deprecated", // GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR
1244 "Undef. behavior", // GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR
1245 "Portability", // GL_DEBUG_TYPE_PORTABILITY
1246 "Performance", // GL_DEBUG_TYPE_PERFORMANCE
1247 "Other" // GL_DEBUG_TYPE_OTHER
1250 static Standard_CString THE_DBGMSG_SEV_HIGH = "High"; // GL_DEBUG_SEVERITY_HIGH
1251 static Standard_CString THE_DBGMSG_SEV_MEDIUM = "Medium"; // GL_DEBUG_SEVERITY_MEDIUM
1252 static Standard_CString THE_DBGMSG_SEV_LOW = "Low"; // GL_DEBUG_SEVERITY_LOW
1254 //! Callback for GL_ARB_debug_output extension
1255 static void APIENTRY debugCallbackWrap(unsigned int theSource,
1256 unsigned int theType,
1258 unsigned int theSeverity,
1260 const char* theMessage,
1261 const void* theUserParam)
1263 OpenGl_Context* aCtx = (OpenGl_Context* )theUserParam;
1264 aCtx->PushMessage (theSource, theType, theId, theSeverity, theMessage);
1267 // =======================================================================
1268 // function : PushMessage
1270 // =======================================================================
1271 void OpenGl_Context::PushMessage (const unsigned int theSource,
1272 const unsigned int theType,
1273 const unsigned int theId,
1274 const unsigned int theSeverity,
1275 const TCollection_ExtendedString& theMessage)
1277 if (caps->suppressExtraMsg
1278 && theSource >= GL_DEBUG_SOURCE_API
1279 && theSource <= GL_DEBUG_SOURCE_OTHER
1280 && myFilters[theSource - GL_DEBUG_SOURCE_API].Contains (theId))
1285 Standard_CString& aSrc = (theSource >= GL_DEBUG_SOURCE_API
1286 && theSource <= GL_DEBUG_SOURCE_OTHER)
1287 ? THE_DBGMSG_SOURCES[theSource - GL_DEBUG_SOURCE_API]
1288 : THE_DBGMSG_UNKNOWN;
1289 Standard_CString& aType = (theType >= GL_DEBUG_TYPE_ERROR
1290 && theType <= GL_DEBUG_TYPE_OTHER)
1291 ? THE_DBGMSG_TYPES[theType - GL_DEBUG_TYPE_ERROR]
1292 : THE_DBGMSG_UNKNOWN;
1293 Standard_CString& aSev = theSeverity == GL_DEBUG_SEVERITY_HIGH
1294 ? THE_DBGMSG_SEV_HIGH
1295 : (theSeverity == GL_DEBUG_SEVERITY_MEDIUM
1296 ? THE_DBGMSG_SEV_MEDIUM
1297 : THE_DBGMSG_SEV_LOW);
1298 Message_Gravity aGrav = theSeverity == GL_DEBUG_SEVERITY_HIGH
1300 : (theSeverity == GL_DEBUG_SEVERITY_MEDIUM
1304 TCollection_ExtendedString aMsg;
1305 aMsg += "TKOpenGl"; aMsg += aSrc;
1306 aMsg += " | Type: "; aMsg += aType;
1307 aMsg += " | ID: "; aMsg += (Standard_Integer )theId;
1308 aMsg += " | Severity: "; aMsg += aSev;
1309 aMsg += " | Message:\n ";
1311 Messenger()->Send (aMsg, aGrav);
1314 // =======================================================================
1315 // function : ExcludeMessage
1317 // ======================================================================
1318 Standard_Boolean OpenGl_Context::ExcludeMessage (const unsigned int theSource,
1319 const unsigned int theId)
1321 return theSource >= GL_DEBUG_SOURCE_API
1322 && theSource <= GL_DEBUG_SOURCE_OTHER
1323 && myFilters[theSource - GL_DEBUG_SOURCE_API].Add (theId);
1326 // =======================================================================
1327 // function : IncludeMessage
1329 // ======================================================================
1330 Standard_Boolean OpenGl_Context::IncludeMessage (const unsigned int theSource,
1331 const unsigned int theId)
1333 return theSource >= GL_DEBUG_SOURCE_API
1334 && theSource <= GL_DEBUG_SOURCE_OTHER
1335 && myFilters[theSource - GL_DEBUG_SOURCE_API].Remove (theId);
1338 // =======================================================================
1339 // function : checkWrongVersion
1341 // ======================================================================
1342 void OpenGl_Context::checkWrongVersion (Standard_Integer theGlVerMajor, Standard_Integer theGlVerMinor,
1343 const char* theLastFailedProc)
1345 if (!IsGlGreaterEqual (theGlVerMajor, theGlVerMinor))
1350 PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH,
1351 TCollection_AsciiString()
1352 + "Error! OpenGL context reports version "
1353 + myGlVerMajor + "." + myGlVerMinor
1354 + " but does not export required functions for " + theGlVerMajor + "." + theGlVerMinor
1355 + " (" + (theLastFailedProc != NULL ? theLastFailedProc : "") + ")\n"
1356 + "Please report this issue to OpenGL driver vendor '" + myVendor + "'");
1358 // lower internal version
1359 if (theGlVerMinor > 0)
1361 myGlVerMajor = theGlVerMajor;
1362 myGlVerMinor = theGlVerMinor - 1;
1365 #if defined(GL_ES_VERSION_2_0)
1366 switch (theGlVerMajor)
1368 case 3: myGlVerMajor = 2; myGlVerMinor = 0; return;
1371 switch (theGlVerMajor)
1373 case 2: myGlVerMajor = 1; myGlVerMinor = 5; return;
1374 case 3: myGlVerMajor = 2; myGlVerMinor = 1; return;
1375 case 4: myGlVerMajor = 3; myGlVerMinor = 3; return;
1380 // =======================================================================
1383 // =======================================================================
1384 void OpenGl_Context::init (const Standard_Boolean theIsCoreProfile)
1389 myMaxMsaaSamples = 0;
1390 myMaxDrawBuffers = 1;
1391 myMaxColorAttachments = 1;
1393 ReadGlVersion (myGlVerMajor, myGlVerMinor);
1394 myVendor = (const char* )::glGetString (GL_VENDOR);
1395 myVendor.LowerCase();
1396 mySupportedFormats->Clear();
1398 if (caps->contextMajorVersionUpper != -1)
1400 // synthetically restrict OpenGL version for testing
1401 Standard_Integer aCtxVer[2] = { myGlVerMajor, myGlVerMinor };
1402 bool isLowered = false;
1403 if (myGlVerMajor > caps->contextMajorVersionUpper)
1406 myGlVerMajor = caps->contextMajorVersionUpper;
1407 #if defined(GL_ES_VERSION_2_0)
1408 switch (myGlVerMajor)
1410 case 2: myGlVerMinor = 0; break;
1413 switch (myGlVerMajor)
1415 case 1: myGlVerMinor = 5; break;
1416 case 2: myGlVerMinor = 1; break;
1417 case 3: myGlVerMinor = 3; break;
1421 if (caps->contextMinorVersionUpper != -1
1422 && myGlVerMinor > caps->contextMinorVersionUpper)
1425 myGlVerMinor = caps->contextMinorVersionUpper;
1429 PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_MEDIUM,
1430 TCollection_AsciiString ("OpenGL version ") + aCtxVer[0] + "." + aCtxVer[1]
1431 + " has been lowered to " + myGlVerMajor + "." + myGlVerMinor);
1435 if (!caps->ffpEnable
1436 && !IsGlGreaterEqual (2, 0))
1438 caps->ffpEnable = true;
1439 TCollection_ExtendedString aMsg =
1440 TCollection_ExtendedString("OpenGL driver is too old! Context info:\n")
1441 + " Vendor: " + (const char* )::glGetString (GL_VENDOR) + "\n"
1442 + " Renderer: " + (const char* )::glGetString (GL_RENDERER) + "\n"
1443 + " Version: " + (const char* )::glGetString (GL_VERSION) + "\n"
1444 + " Fallback using deprecated fixed-function pipeline.\n"
1445 + " Visualization might work incorrectly.\n"
1446 " Consider upgrading the graphics driver.";
1447 PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
1450 #if defined(GL_ES_VERSION_2_0)
1451 (void )theIsCoreProfile;
1452 const bool isCoreProfile = false;
1455 if (myVendor.Search ("nvidia") != -1)
1457 // Buffer detailed info: Buffer object 1 (bound to GL_ARRAY_BUFFER_ARB, usage hint is GL_STATIC_DRAW)
1458 // will use VIDEO memory as the source for buffer object operations.
1459 ExcludeMessage (GL_DEBUG_SOURCE_API, 131185);
1461 if (IsGlGreaterEqual (3, 0))
1463 // retrieve auxiliary function in advance
1464 FindProc ("glGetStringi", myFuncs->glGetStringi);
1467 bool isCoreProfile = false;
1468 if (IsGlGreaterEqual (3, 2))
1470 isCoreProfile = (theIsCoreProfile == Standard_True);
1472 // detect Core profile
1476 ::glGetIntegerv (GL_CONTEXT_PROFILE_MASK, &aProfile);
1477 isCoreProfile = (aProfile & GL_CONTEXT_CORE_PROFILE_BIT) != 0;
1482 myFuncs->load (*this, isCoreProfile);
1484 // setup shader generator
1485 myShaderManager->SetGapiVersion (myGlVerMajor, myGlVerMinor);
1486 myShaderManager->SetEmulateDepthClamp (!arbDepthClamp);
1488 bool toReverseDFdxSign = false;
1489 #if defined(GL_ES_VERSION_2_0)
1490 // workaround Adreno driver bug computing reversed normal using dFdx/dFdy
1491 toReverseDFdxSign = myVendor.Search("qualcomm") != -1;
1493 myShaderManager->SetFlatShading (hasFlatShading != OpenGl_FeatureNotAvailable, toReverseDFdxSign);
1494 #if defined(GL_ES_VERSION_2_0)
1495 myShaderManager->SetUseRedAlpha (false);
1497 myShaderManager->SetUseRedAlpha (core11ffp == NULL);
1499 #define checkGlslExtensionShort(theName) myShaderManager->EnableGlslExtension (Graphic3d_GlslExtension_ ## theName, CheckExtension (#theName))
1500 #if defined(GL_ES_VERSION_2_0)
1501 checkGlslExtensionShort(GL_OES_standard_derivatives);
1502 checkGlslExtensionShort(GL_EXT_shader_texture_lod);
1503 checkGlslExtensionShort(GL_EXT_frag_depth);
1505 checkGlslExtensionShort(GL_EXT_gpu_shader4);
1508 // initialize debug context extension
1510 && caps->contextDebug)
1512 // setup default callback
1513 myIsGlDebugCtx = Standard_True;
1514 arbDbg->glDebugMessageCallback (debugCallbackWrap, this);
1515 #if defined(GL_ES_VERSION_2_0)
1516 ::glEnable (GL_DEBUG_OUTPUT);
1520 ::glEnable (GL_DEBUG_OUTPUT);
1523 if (caps->contextSyncDebug)
1525 // note that some broken implementations (e.g. simulators) might generate error message on this call
1526 ::glEnable (GL_DEBUG_OUTPUT_SYNCHRONOUS);
1532 glGetIntegerv (GL_MAX_DRAW_BUFFERS, &myMaxDrawBuffers);
1533 glGetIntegerv (GL_MAX_COLOR_ATTACHMENTS, &myMaxColorAttachments);
1534 if (myDrawBuffers.Length() < myMaxDrawBuffers)
1536 myDrawBuffers.Resize (0, myMaxDrawBuffers - 1, false);
1540 glGetIntegerv (GL_MAX_TEXTURE_SIZE, &myMaxTexDim);
1541 #if !defined(GL_ES_VERSION_2_0)
1542 if (IsGlGreaterEqual (1, 3) && core11ffp != NULL)
1544 // this is a maximum of texture units for FFP functionality,
1545 // usually smaller than combined texture units available for GLSL
1546 glGetIntegerv (GL_MAX_TEXTURE_UNITS, &myMaxTexUnitsFFP);
1547 myMaxTexCombined = myMaxTexUnitsFFP;
1550 if (IsGlGreaterEqual (2, 0))
1552 glGetIntegerv (GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &myMaxTexCombined);
1554 mySpriteTexUnit = myMaxTexCombined >= 2
1555 ? Graphic3d_TextureUnit_PointSprite
1556 : Graphic3d_TextureUnit_0;
1558 GLint aMaxVPortSize[2] = {0, 0};
1559 glGetIntegerv (GL_MAX_VIEWPORT_DIMS, aMaxVPortSize);
1560 myMaxDumpSizeX = Min (aMaxVPortSize[0], myMaxTexDim);
1561 myMaxDumpSizeY = Min (aMaxVPortSize[1], myMaxTexDim);
1562 if (myVendor == "intel")
1564 // Intel drivers have known bug with empty dump for images with width>=5462
1565 myMaxDumpSizeX = Min (myMaxDumpSizeX, 4096);
1570 glGetIntegerv (GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &myAnisoMax);
1573 myClippingState.Init();
1575 #if defined(GL_ES_VERSION_2_0)
1576 if (IsGlGreaterEqual (3, 1)
1577 && myFuncs->glTexStorage2DMultisample != NULL)
1579 // MSAA RenderBuffers have been defined in OpenGL ES 3.0,
1580 // but MSAA Textures - only in OpenGL ES 3.1+
1581 ::glGetIntegerv (GL_MAX_SAMPLES, &myMaxMsaaSamples);
1586 // MSAA RenderBuffers have been defined in OpenGL 3.0,
1587 // but MSAA Textures - only in OpenGL 3.2+
1590 ::glGetIntegerv (GL_MAX_SAMPLES, &myMaxMsaaSamples);
1592 else if (CheckExtension ("GL_ARB_texture_multisample")
1593 && myFuncs->glTexImage2DMultisample != NULL)
1595 GLint aNbColorSamples = 0, aNbDepthSamples = 0;
1596 ::glGetIntegerv (GL_MAX_COLOR_TEXTURE_SAMPLES, &aNbColorSamples);
1597 ::glGetIntegerv (GL_MAX_DEPTH_TEXTURE_SAMPLES, &aNbDepthSamples);
1598 myMaxMsaaSamples = Min (aNbColorSamples, aNbDepthSamples);
1603 #if !defined(GL_ES_VERSION_2_0)
1604 if (core32 != NULL && isCoreProfile)
1606 core32->glGenVertexArrays (1, &myDefaultVao);
1609 myTexClamp = IsGlGreaterEqual (1, 2) ? GL_CLAMP_TO_EDGE : GL_CLAMP;
1611 GLint aStereo = GL_FALSE;
1612 glGetIntegerv (GL_STEREO, &aStereo);
1613 myIsStereoBuffers = aStereo == 1;
1615 // get number of maximum clipping planes
1616 glGetIntegerv (GL_MAX_CLIP_PLANES, &myMaxClipPlanes);
1619 #if defined(GL_ES_VERSION_2_0)
1620 // check whether ray tracing mode is supported
1621 myHasRayTracing = IsGlGreaterEqual (3, 2);
1622 myHasRayTracingTextures = myHasRayTracingAdaptiveSampling = myHasRayTracingAdaptiveSamplingAtomic = false;
1624 // check whether ray tracing mode is supported
1625 myHasRayTracing = IsGlGreaterEqual (3, 1)
1627 && arbFBOBlit != NULL;
1629 // check whether textures in ray tracing mode are supported
1630 myHasRayTracingTextures = myHasRayTracing
1631 && arbTexBindless != NULL;
1633 // check whether adaptive screen sampling in ray tracing mode is supported
1634 myHasRayTracingAdaptiveSampling = myHasRayTracing
1636 myHasRayTracingAdaptiveSamplingAtomic = myHasRayTracingAdaptiveSampling
1637 && CheckExtension ("GL_NV_shader_atomic_float");
1643 // Detect if window buffer is considered by OpenGL as sRGB-ready
1644 // (linear RGB color written by shader is automatically converted into sRGB)
1645 // or not (offscreen FBO should be blit into window buffer with gamma correction).
1646 const GLenum aDefWinBuffer =
1647 #if !defined(GL_ES_VERSION_2_0)
1652 GLint aWinColorEncoding = 0; // GL_LINEAR
1653 bool toSkipCheck = false;
1654 #if defined(GL_ES_VERSION_2_0)
1655 toSkipCheck = !IsGlGreaterEqual (3, 0);
1659 arbFBO->glGetFramebufferAttachmentParameteriv (GL_FRAMEBUFFER, aDefWinBuffer, GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING, &aWinColorEncoding);
1662 myIsSRgbWindow = aWinColorEncoding == GL_SRGB;
1664 // On desktop OpenGL, pixel formats are almost always sRGB-ready, even when not requested;
1665 // it is safe behavior on desktop where GL_FRAMEBUFFER_SRGB is disabled by default
1666 // (contrary to OpenGL ES, where it is enabled by default).
1667 // NVIDIA drivers, however, always return GL_LINEAR even for sRGB-ready pixel formats on Windows platform,
1668 // while AMD and Intel report GL_SRGB as expected.
1669 // macOS drivers seems to be also report GL_LINEAR even for [NSColorSpace sRGBColorSpace].
1670 #if !defined(GL_ES_VERSION_2_0)
1672 myIsSRgbWindow = true;
1675 && myVendor.Search ("nvidia") != -1)
1677 myIsSRgbWindow = true;
1681 if (!myIsSRgbWindow)
1683 Message::SendTrace ("OpenGl_Context, warning: window buffer is not sRGB-ready.\n"
1684 "Check OpenGL window creation parameters for optimal performance.");
1689 mySupportedFormats->Add (Image_Format_Gray);
1690 mySupportedFormats->Add (Image_Format_Alpha);
1691 mySupportedFormats->Add (Image_Format_RGB);
1692 mySupportedFormats->Add (Image_Format_RGB32);
1693 mySupportedFormats->Add (Image_Format_RGBA);
1696 #if !defined(GL_ES_VERSION_2_0)
1697 // no BGR on OpenGL ES - only BGRA as extension
1698 mySupportedFormats->Add (Image_Format_BGR);
1700 mySupportedFormats->Add (Image_Format_BGR32);
1701 mySupportedFormats->Add (Image_Format_BGRA);
1705 mySupportedFormats->Add (Image_Format_GrayF);
1706 mySupportedFormats->Add (Image_Format_AlphaF);
1707 mySupportedFormats->Add (Image_Format_RGBF);
1708 mySupportedFormats->Add (Image_Format_RGBAF);
1709 if (hasHalfFloatBuffer != OpenGl_FeatureNotAvailable)
1711 mySupportedFormats->Add (Image_Format_RGBAF_half);
1715 mySupportedFormats->Add (Image_Format_RGF);
1716 if (hasHalfFloatBuffer != OpenGl_FeatureNotAvailable)
1718 mySupportedFormats->Add (Image_Format_RGF_half);
1723 #if !defined(GL_ES_VERSION_2_0)
1724 mySupportedFormats->Add (Image_Format_BGRF);
1726 mySupportedFormats->Add (Image_Format_BGRAF);
1730 #ifdef __EMSCRIPTEN__
1731 if (CheckExtension ("GL_WEBGL_compressed_texture_s3tc")) // GL_WEBGL_compressed_texture_s3tc_srgb for sRGB formats
1733 mySupportedFormats->Add (Image_CompressedFormat_RGB_S3TC_DXT1);
1734 mySupportedFormats->Add (Image_CompressedFormat_RGBA_S3TC_DXT1);
1735 mySupportedFormats->Add (Image_CompressedFormat_RGBA_S3TC_DXT3);
1736 mySupportedFormats->Add (Image_CompressedFormat_RGBA_S3TC_DXT5);
1739 && CheckExtension ("GL_WEBGL_depth_texture"))
1741 extPDS = true; // WebGL 1.0 extension (in WebGL 2.0 core)
1744 if (CheckExtension ("GL_EXT_texture_compression_s3tc")) // GL_EXT_texture_sRGB for sRGB formats
1746 mySupportedFormats->Add (Image_CompressedFormat_RGB_S3TC_DXT1);
1747 mySupportedFormats->Add (Image_CompressedFormat_RGBA_S3TC_DXT1);
1748 mySupportedFormats->Add (Image_CompressedFormat_RGBA_S3TC_DXT3);
1749 mySupportedFormats->Add (Image_CompressedFormat_RGBA_S3TC_DXT5);
1753 if (CheckExtension ("GL_EXT_texture_compression_dxt1"))
1755 mySupportedFormats->Add (Image_CompressedFormat_RGB_S3TC_DXT1);
1756 mySupportedFormats->Add (Image_CompressedFormat_RGBA_S3TC_DXT1);
1758 if (CheckExtension ("GL_ANGLE_texture_compression_dxt3"))
1760 mySupportedFormats->Add (Image_CompressedFormat_RGBA_S3TC_DXT3);
1762 if (CheckExtension ("GL_ANGLE_texture_compression_dxt5"))
1764 mySupportedFormats->Add (Image_CompressedFormat_RGBA_S3TC_DXT5);
1769 // check whether PBR shading model is supported
1770 myHasPBR = arbFBO != NULL
1771 && myMaxTexCombined >= 4
1773 && (IsGlGreaterEqual (3, 0)
1774 #if defined(GL_ES_VERSION_2_0)
1776 // || CheckExtension ("GL_EXT_shader_texture_lod") fallback is used when extension is unavailable
1778 || (IsGlGreaterEqual (2, 1) && CheckExtension ("GL_EXT_gpu_shader4"))
1782 myDepthPeelingDepthTexUnit = static_cast<Graphic3d_TextureUnit>(myMaxTexCombined + Graphic3d_TextureUnit_DepthPeelingDepth); // -6
1783 myDepthPeelingFrontColorTexUnit = static_cast<Graphic3d_TextureUnit>(myMaxTexCombined + Graphic3d_TextureUnit_DepthPeelingFrontColor); // -5
1784 myShadowMapTexUnit = static_cast<Graphic3d_TextureUnit>(myMaxTexCombined + Graphic3d_TextureUnit_ShadowMap); // -4
1785 myPBREnvLUTTexUnit = static_cast<Graphic3d_TextureUnit>(myMaxTexCombined + Graphic3d_TextureUnit_PbrEnvironmentLUT); // -3
1786 myPBRDiffIBLMapSHTexUnit = static_cast<Graphic3d_TextureUnit>(myMaxTexCombined + Graphic3d_TextureUnit_PbrIblDiffuseSH); // -2
1787 myPBRSpecIBLMapTexUnit = static_cast<Graphic3d_TextureUnit>(myMaxTexCombined + Graphic3d_TextureUnit_PbrIblSpecular); // -1
1790 myDepthPeelingDepthTexUnit = static_cast<Graphic3d_TextureUnit>(myDepthPeelingDepthTexUnit + 3);
1791 myDepthPeelingFrontColorTexUnit = static_cast<Graphic3d_TextureUnit>(myDepthPeelingFrontColorTexUnit + 3);
1792 myShadowMapTexUnit = static_cast<Graphic3d_TextureUnit>(myShadowMapTexUnit + 3);
1796 // =======================================================================
1797 // function : MemoryInfo
1799 // =======================================================================
1800 Standard_Size OpenGl_Context::AvailableMemory() const
1802 #if !defined(GL_ES_VERSION_2_0)
1805 // this is actually information for VBO pool
1806 // however because pools are mostly shared
1807 // it can be used for total GPU memory estimations
1810 glGetIntegerv (GL_VBO_FREE_MEMORY_ATI, aMemInfo);
1811 // returned value is in KiB, however this maybe changed in future
1812 return Standard_Size(aMemInfo[0]) * 1024;
1816 // current available dedicated video memory (in KiB), currently unused GPU memory
1818 glGetIntegerv (GL_GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX, &aMemInfo);
1819 return Standard_Size(aMemInfo) * 1024;
1825 // =======================================================================
1826 // function : MemoryInfo
1828 // =======================================================================
1829 TCollection_AsciiString OpenGl_Context::MemoryInfo() const
1831 TColStd_IndexedDataMapOfStringString aDict;
1834 TCollection_AsciiString aText;
1835 for (TColStd_IndexedDataMapOfStringString::Iterator anIter (aDict); anIter.More(); anIter.Next())
1837 if (!aText.IsEmpty())
1841 aText += TCollection_AsciiString(" ") + anIter.Key() + ": " + anIter.Value();
1846 // =======================================================================
1847 // function : MemoryInfo
1849 // =======================================================================
1850 void OpenGl_Context::MemoryInfo (TColStd_IndexedDataMapOfStringString& theDict) const
1852 #if defined(GL_ES_VERSION_2_0)
1854 #elif defined(__APPLE__) && !defined(HAVE_XLIB)
1855 GLint aGlRendId = 0;
1856 CGLGetParameter (CGLGetCurrentContext(), kCGLCPCurrentRendererID, &aGlRendId);
1858 CGLRendererInfoObj aRendObj = NULL;
1859 CGOpenGLDisplayMask aDispMask = CGDisplayIDToOpenGLDisplayMask (kCGDirectMainDisplay);
1861 CGLQueryRendererInfo (aDispMask, &aRendObj, &aRendNb);
1862 for (GLint aRendIter = 0; aRendIter < aRendNb; ++aRendIter)
1865 if (CGLDescribeRenderer (aRendObj, aRendIter, kCGLRPRendererID, &aRendId) != kCGLNoError
1866 || aRendId != aGlRendId)
1871 //kCGLRPVideoMemoryMegabytes = 131;
1872 //kCGLRPTextureMemoryMegabytes = 132;
1874 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
1875 if (CGLDescribeRenderer(aRendObj, aRendIter, kCGLRPVideoMemoryMegabytes, &aVMem) == kCGLNoError)
1877 addInfo (theDict, "GPU memory", TCollection_AsciiString() + aVMem + " MiB");
1879 if (CGLDescribeRenderer(aRendObj, aRendIter, kCGLRPTextureMemoryMegabytes, &aVMem) == kCGLNoError)
1881 addInfo (theDict, "GPU Texture memory", TCollection_AsciiString() + aVMem + " MiB");
1884 if (CGLDescribeRenderer(aRendObj, aRendIter, kCGLRPVideoMemory, &aVMem) == kCGLNoError)
1886 addInfo (theDict, "GPU memory", TCollection_AsciiString() + (aVMem / (1024 * 1024)) + " MiB");
1888 if (CGLDescribeRenderer(aRendObj, aRendIter, kCGLRPTextureMemory, &aVMem) == kCGLNoError)
1890 addInfo (theDict, "GPU Texture memory", TCollection_AsciiString() + (aVMem / (1024 * 1024)) + " MiB");
1896 #if !defined(GL_ES_VERSION_2_0)
1900 memset (aValues, 0, sizeof(aValues));
1901 glGetIntegerv (GL_VBO_FREE_MEMORY_ATI, aValues);
1903 // total memory free in the pool
1904 addInfo (theDict, "GPU free memory", TCollection_AsciiString() + (aValues[0] / 1024) + " MiB");
1906 if (aValues[1] != aValues[0])
1908 // largest available free block in the pool
1909 addInfo (theDict, "Largest free block", TCollection_AsciiString() + (aValues[1] / 1024) + " MiB");
1911 if (aValues[2] != aValues[0])
1913 // total auxiliary memory free
1914 addInfo (theDict, "Free auxiliary memory", TCollection_AsciiString() + (aValues[2] / 1024) + " MiB");
1919 //current available dedicated video memory (in KiB), currently unused GPU memory
1921 glGetIntegerv (GL_GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX, &aValue);
1922 addInfo (theDict, "GPU free memory", TCollection_AsciiString() + (aValue / 1024) + " MiB");
1924 // dedicated video memory, total size (in KiB) of the GPU memory
1925 GLint aDedicated = 0;
1926 glGetIntegerv (GL_GPU_MEMORY_INFO_DEDICATED_VIDMEM_NVX, &aDedicated);
1927 addInfo (theDict, "GPU memory", TCollection_AsciiString() + (aDedicated / 1024) + " MiB");
1929 // total available memory, total size (in KiB) of the memory available for allocations
1930 glGetIntegerv (GL_GPU_MEMORY_INFO_TOTAL_AVAILABLE_MEMORY_NVX, &aValue);
1931 if (aValue != aDedicated)
1933 // different only for special configurations
1934 addInfo (theDict, "Total memory", TCollection_AsciiString() + (aValue / 1024) + " MiB");
1938 else if (myFuncs->wglGetGPUInfoAMD != NULL
1939 && myFuncs->wglGetContextGPUIDAMD != NULL)
1941 GLuint aTotalMemMiB = 0;
1942 UINT anAmdId = myFuncs->wglGetContextGPUIDAMD ((HGLRC )myGContext);
1945 if (myFuncs->wglGetGPUInfoAMD (anAmdId, WGL_GPU_RAM_AMD, GL_UNSIGNED_INT, sizeof(aTotalMemMiB), &aTotalMemMiB) > 0)
1947 addInfo (theDict, "GPU memory", TCollection_AsciiString() + (int )aTotalMemMiB + " MiB");
1954 #if !defined(GL_ES_VERSION_2_0) && !defined(__APPLE__) && !defined(_WIN32)
1955 // GLX_RENDERER_VENDOR_ID_MESA
1956 if (myFuncs->glXQueryCurrentRendererIntegerMESA != NULL)
1958 unsigned int aVMemMiB = 0;
1959 if (myFuncs->glXQueryCurrentRendererIntegerMESA (GLX_RENDERER_VIDEO_MEMORY_MESA, &aVMemMiB) != False)
1961 addInfo (theDict, "GPU memory", TCollection_AsciiString() + int(aVMemMiB) + " MiB");
1967 // =======================================================================
1968 // function : DiagnosticInfo
1970 // =======================================================================
1971 void OpenGl_Context::DiagnosticInformation (TColStd_IndexedDataMapOfStringString& theDict,
1972 Graphic3d_DiagnosticInfo theFlags) const
1974 if ((theFlags & Graphic3d_DiagnosticInfo_NativePlatform) != 0)
1976 #if defined(HAVE_EGL)
1977 addInfo (theDict, "EGLVersion", ::eglQueryString ((EGLDisplay )myDisplay, EGL_VERSION));
1978 addInfo (theDict, "EGLVendor", ::eglQueryString ((EGLDisplay )myDisplay, EGL_VENDOR));
1979 addInfo (theDict, "EGLClientAPIs", ::eglQueryString ((EGLDisplay )myDisplay, EGL_CLIENT_APIS));
1980 if ((theFlags & Graphic3d_DiagnosticInfo_Extensions) != 0)
1982 addInfo (theDict, "EGLExtensions", ::eglQueryString ((EGLDisplay )myDisplay, EGL_EXTENSIONS));
1984 #elif defined(_WIN32)
1985 if ((theFlags & Graphic3d_DiagnosticInfo_Extensions) != 0
1986 && myFuncs->wglGetExtensionsStringARB != NULL)
1988 const char* aWglExts = myFuncs->wglGetExtensionsStringARB ((HDC )myDisplay);
1989 addInfo (theDict, "WGLExtensions", aWglExts);
1991 #elif defined(HAVE_XLIB)
1992 Display* aDisplay = (Display*)myDisplay;
1993 const int aScreen = DefaultScreen(aDisplay);
1994 addInfo (theDict, "GLXDirectRendering", ::glXIsDirect (aDisplay, (GLXContext )myGContext) ? "Yes" : "No");
1995 addInfo (theDict, "GLXVendor", ::glXQueryServerString (aDisplay, aScreen, GLX_VENDOR));
1996 addInfo (theDict, "GLXVersion", ::glXQueryServerString (aDisplay, aScreen, GLX_VERSION));
1997 if ((theFlags & Graphic3d_DiagnosticInfo_Extensions) != 0)
1999 const char* aGlxExts = ::glXQueryExtensionsString (aDisplay, aScreen);
2000 addInfo(theDict, "GLXExtensions", aGlxExts);
2003 addInfo (theDict, "GLXClientVendor", ::glXGetClientString (aDisplay, GLX_VENDOR));
2004 addInfo (theDict, "GLXClientVersion", ::glXGetClientString (aDisplay, GLX_VERSION));
2005 if ((theFlags & Graphic3d_DiagnosticInfo_Extensions) != 0)
2007 addInfo (theDict, "GLXClientExtensions", ::glXGetClientString (aDisplay, GLX_EXTENSIONS));
2014 if ((theFlags & Graphic3d_DiagnosticInfo_Device) != 0)
2016 Standard_Integer aDriverVer[2] = {};
2017 ReadGlVersion (aDriverVer[0], aDriverVer[1]);
2018 addInfo (theDict, "GLvendor", (const char*)::glGetString (GL_VENDOR));
2019 addInfo (theDict, "GLdevice", (const char*)::glGetString (GL_RENDERER));
2020 #ifdef __EMSCRIPTEN__
2021 if (CheckExtension ("GL_WEBGL_debug_renderer_info"))
2023 if (const char* aVendor = (const char*)::glGetString (0x9245))
2025 addInfo (theDict, "GLunmaskedVendor", aVendor);
2027 if (const char* aDevice = (const char*)::glGetString (0x9246))
2029 addInfo (theDict, "GLunmaskedDevice", aDevice);
2034 addInfo (theDict, "GLversion", (const char*)::glGetString (GL_VERSION));
2035 if (myGlVerMajor != aDriverVer[0]
2036 || myGlVerMinor != aDriverVer[1])
2038 addInfo (theDict, "GLversionOcct", TCollection_AsciiString (myGlVerMajor) + "." + TCollection_AsciiString (myGlVerMinor));
2040 if (IsGlGreaterEqual (2, 0))
2042 addInfo (theDict, "GLSLversion", (const char*)::glGetString (GL_SHADING_LANGUAGE_VERSION));
2046 addInfo (theDict, "GLdebug", "ON");
2050 if ((theFlags & Graphic3d_DiagnosticInfo_Limits) != 0)
2052 addInfo (theDict, "Max texture size", TCollection_AsciiString(myMaxTexDim));
2053 addInfo (theDict, "Max FBO dump size", TCollection_AsciiString() + myMaxDumpSizeX + "x" + myMaxDumpSizeY);
2054 addInfo (theDict, "Max combined texture units", TCollection_AsciiString(myMaxTexCombined));
2055 addInfo (theDict, "Max MSAA samples", TCollection_AsciiString(myMaxMsaaSamples));
2058 if ((theFlags & Graphic3d_DiagnosticInfo_FrameBuffer) != 0)
2060 GLint aViewport[4] = {};
2061 ::glGetIntegerv (GL_VIEWPORT, aViewport);
2062 addInfo (theDict, "Viewport", TCollection_AsciiString() + aViewport[2] + "x" + aViewport[3]);
2065 if ((theFlags & Graphic3d_DiagnosticInfo_Memory) != 0)
2067 MemoryInfo (theDict);
2070 if ((theFlags & Graphic3d_DiagnosticInfo_Extensions) != 0)
2072 #if !defined(GL_ES_VERSION_2_0)
2073 if (IsGlGreaterEqual (3, 0)
2074 && myFuncs->glGetStringi != NULL)
2076 TCollection_AsciiString anExtList;
2078 ::glGetIntegerv (GL_NUM_EXTENSIONS, &anExtNb);
2079 for (GLint anIter = 0; anIter < anExtNb; ++anIter)
2081 const char* anExtension = (const char*)myFuncs->glGetStringi (GL_EXTENSIONS, (GLuint)anIter);
2082 if (!anExtList.IsEmpty())
2086 anExtList += anExtension;
2088 addInfo(theDict, "GLextensions", anExtList);
2093 addInfo (theDict, "GLextensions", (const char*)::glGetString (GL_EXTENSIONS));
2098 // =======================================================================
2099 // function : GetResource
2101 // =======================================================================
2102 const Handle(OpenGl_Resource)& OpenGl_Context::GetResource (const TCollection_AsciiString& theKey) const
2104 return mySharedResources->IsBound (theKey) ? mySharedResources->Find (theKey) : NULL_GL_RESOURCE;
2107 // =======================================================================
2108 // function : ShareResource
2110 // =======================================================================
2111 Standard_Boolean OpenGl_Context::ShareResource (const TCollection_AsciiString& theKey,
2112 const Handle(OpenGl_Resource)& theResource)
2114 if (theKey.IsEmpty() || theResource.IsNull())
2116 return Standard_False;
2118 return mySharedResources->Bind (theKey, theResource);
2121 // =======================================================================
2122 // function : ReleaseResource
2124 // =======================================================================
2125 void OpenGl_Context::ReleaseResource (const TCollection_AsciiString& theKey,
2126 const Standard_Boolean theToDelay)
2128 if (!mySharedResources->IsBound (theKey))
2132 const Handle(OpenGl_Resource)& aRes = mySharedResources->Find (theKey);
2133 if (aRes->GetRefCount() > 1)
2140 myDelayed->Bind (theKey, 1);
2144 aRes->Release (this);
2145 mySharedResources->UnBind (theKey);
2149 // =======================================================================
2150 // function : ReleaseDelayed
2152 // =======================================================================
2153 void OpenGl_Context::ReleaseDelayed()
2155 // release queued elements
2156 while (!myUnusedResources->IsEmpty())
2158 myUnusedResources->First()->Release (this);
2159 myUnusedResources->RemoveFirst();
2162 // release delayed shared resources
2163 NCollection_Vector<TCollection_AsciiString> aDeadList;
2164 for (NCollection_DataMap<TCollection_AsciiString, Standard_Integer>::Iterator anIter (*myDelayed);
2165 anIter.More(); anIter.Next())
2167 if (++anIter.ChangeValue() <= 2)
2169 continue; // postpone release one more frame to ensure no one uses it periodically
2172 const TCollection_AsciiString& aKey = anIter.Key();
2173 if (!mySharedResources->IsBound (aKey))
2175 // mixed unshared strategy delayed/undelayed was used!
2176 aDeadList.Append (aKey);
2180 const Handle(OpenGl_Resource)& aRes = mySharedResources->ChangeFind (aKey);
2181 if (aRes->GetRefCount() > 1)
2183 // should be only 1 instance in mySharedResources
2184 // if not - resource was reused again
2185 aDeadList.Append (aKey);
2189 // release resource if no one requested it more than 2 redraw calls
2190 aRes->Release (this);
2191 mySharedResources->UnBind (aKey);
2192 aDeadList.Append (aKey);
2195 for (Standard_Integer anIter = 0; anIter < aDeadList.Length(); ++anIter)
2197 myDelayed->UnBind (aDeadList.Value (anIter));
2201 // =======================================================================
2202 // function : BindTextures
2204 // =======================================================================
2205 Handle(OpenGl_TextureSet) OpenGl_Context::BindTextures (const Handle(OpenGl_TextureSet)& theTextures,
2206 const Handle(OpenGl_ShaderProgram)& theProgram)
2208 const Standard_Integer aTextureSetBits = !theTextures.IsNull() ? theTextures->TextureSetBits() : 0;
2209 const Standard_Integer aProgramBits = !theProgram.IsNull() ? theProgram->TextureSetBits() : 0;
2210 Standard_Integer aMissingBits = aProgramBits & ~aTextureSetBits;
2211 if (aMissingBits != 0
2212 && myTextureRgbaBlack.IsNull())
2214 // allocate mock textures
2215 myTextureRgbaBlack = new OpenGl_Texture();
2216 myTextureRgbaWhite = new OpenGl_Texture();
2217 Image_PixMap anImage;
2218 anImage.InitZero (Image_Format_RGBA, 2, 2, 0, (Standard_Byte )0);
2219 if (!myTextureRgbaBlack->Init (this, OpenGl_TextureFormat::Create<GLubyte, 4>(), Graphic3d_Vec2i (2, 2), Graphic3d_TOT_2D, &anImage))
2221 PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_HIGH,
2222 "Error: unable to create unit mock PBR texture map.");
2224 anImage.InitZero (Image_Format_RGBA, 2, 2, 0, (Standard_Byte )255);
2225 if (!myTextureRgbaWhite->Init (this, OpenGl_TextureFormat::Create<GLubyte, 4>(), Graphic3d_Vec2i (2, 2), Graphic3d_TOT_2D, &anImage))
2227 PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_HIGH,
2228 "Error: unable to create normal mock PBR texture map.");
2232 Handle(OpenGl_TextureSet) anOldTextures = myActiveTextures;
2233 if (myActiveTextures != theTextures)
2235 Handle(OpenGl_Context) aThisCtx (this);
2236 for (OpenGl_TextureSetPairIterator aSlotIter (myActiveTextures, theTextures); aSlotIter.More(); aSlotIter.Next())
2238 const Graphic3d_TextureUnit aTexUnit = aSlotIter.Unit();
2239 const OpenGl_Texture* aTextureOld = aSlotIter.Texture1();
2240 const OpenGl_Texture* aTextureNew = aSlotIter.Texture2();
2241 if (aTextureNew == aTextureOld)
2246 if (aTextureNew != NULL
2247 && aTextureNew->IsValid())
2249 if (aTexUnit >= myMaxTexCombined)
2251 PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH,
2252 TCollection_AsciiString("Texture unit ") + aTexUnit + " for " + aTextureNew->ResourceId() + " exceeds hardware limit " + myMaxTexCombined);
2256 aTextureNew->Bind (aThisCtx, aTexUnit);
2257 if (aTextureNew->Sampler()->ToUpdateParameters())
2259 if (aTextureNew->Sampler()->IsImmutable())
2261 aTextureNew->Sampler()->Init (aThisCtx, *aTextureNew);
2265 OpenGl_Sampler::applySamplerParams (aThisCtx, aTextureNew->Sampler()->Parameters(), aTextureNew->Sampler().get(), aTextureNew->GetTarget(), aTextureNew->MaxMipmapLevel());
2268 #if !defined(GL_ES_VERSION_2_0)
2269 if (core11ffp != NULL)
2271 OpenGl_Sampler::applyGlobalTextureParams (aThisCtx, *aTextureNew, aTextureNew->Sampler()->Parameters());
2275 else if (aTextureOld != NULL
2276 && aTextureOld->IsValid())
2278 aTextureOld->Unbind (aThisCtx, aTexUnit);
2279 #if !defined(GL_ES_VERSION_2_0)
2280 if (core11ffp != NULL)
2282 OpenGl_Sampler::resetGlobalTextureParams (aThisCtx, *aTextureOld, aTextureOld->Sampler()->Parameters());
2287 myActiveTextures = theTextures;
2290 if (myActiveMockTextures != aMissingBits)
2292 myActiveMockTextures = aMissingBits;
2293 for (Standard_Integer aBitIter = 0; aMissingBits != 0; ++aBitIter)
2295 Standard_Integer aUnitMask = 1 << aBitIter;
2296 if ((aUnitMask & aMissingBits) != 0)
2298 aMissingBits = aMissingBits & ~aUnitMask;
2299 if (aBitIter == Graphic3d_TextureUnit_Normal)
2301 myTextureRgbaBlack->Bind (this, static_cast<Graphic3d_TextureUnit>(aBitIter));
2305 myTextureRgbaWhite->Bind (this, static_cast<Graphic3d_TextureUnit>(aBitIter));
2311 return anOldTextures;
2314 // =======================================================================
2315 // function : BindProgram
2317 // =======================================================================
2318 Standard_Boolean OpenGl_Context::BindProgram (const Handle(OpenGl_ShaderProgram)& theProgram)
2320 if (core20fwd == NULL)
2322 return Standard_False;
2324 else if (myActiveProgram == theProgram)
2326 return Standard_True;
2329 if (theProgram.IsNull()
2330 || !theProgram->IsValid())
2332 if (!myActiveProgram.IsNull())
2334 core20fwd->glUseProgram (OpenGl_ShaderProgram::NO_PROGRAM);
2335 myActiveProgram.Nullify();
2337 return Standard_False;
2340 myActiveProgram = theProgram;
2341 core20fwd->glUseProgram (theProgram->ProgramId());
2342 return Standard_True;
2345 // =======================================================================
2346 // function : BindDefaultVao
2348 // =======================================================================
2349 void OpenGl_Context::BindDefaultVao()
2351 #if !defined(GL_ES_VERSION_2_0)
2352 if (myDefaultVao == 0
2358 core32->glBindVertexArray (myDefaultVao);
2362 // =======================================================================
2363 // function : SetDefaultFrameBuffer
2365 // =======================================================================
2366 Handle(OpenGl_FrameBuffer) OpenGl_Context::SetDefaultFrameBuffer (const Handle(OpenGl_FrameBuffer)& theFbo)
2368 Handle(OpenGl_FrameBuffer) aFbo = myDefaultFbo;
2369 myDefaultFbo = theFbo;
2373 // =======================================================================
2374 // function : SetShadingMaterial
2376 // =======================================================================
2377 void OpenGl_Context::SetShadingMaterial (const OpenGl_Aspects* theAspect,
2378 const Handle(Graphic3d_PresentationAttributes)& theHighlight)
2380 const Handle(Graphic3d_Aspects)& anAspect = (!theHighlight.IsNull() && !theHighlight->BasicFillAreaAspect().IsNull())
2381 ? (const Handle(Graphic3d_Aspects)& )theHighlight->BasicFillAreaAspect()
2382 : theAspect->Aspect();
2384 const bool toDistinguish = anAspect->Distinguish();
2385 const bool toMapTexture = anAspect->ToMapTexture();
2386 const Graphic3d_MaterialAspect& aMatFrontSrc = anAspect->FrontMaterial();
2387 const Graphic3d_MaterialAspect& aMatBackSrc = toDistinguish
2388 ? anAspect->BackMaterial()
2390 const Quantity_Color& aFrontIntColor = anAspect->InteriorColor();
2391 const Quantity_Color& aBackIntColor = toDistinguish
2392 ? anAspect->BackInteriorColor()
2395 myMaterial.Init (*this, aMatFrontSrc, aFrontIntColor, aMatBackSrc, aBackIntColor);
2396 if (!theHighlight.IsNull()
2397 && theHighlight->BasicFillAreaAspect().IsNull())
2399 myMaterial.SetColor (theHighlight->ColorRGBA().GetRGB());
2400 myMaterial.SetColor (theHighlight->ColorRGBA().GetRGB());
2403 float anAlphaFront = 1.0f, anAlphaBack = 1.0f;
2404 if (CheckIsTransparent (theAspect, theHighlight, anAlphaFront, anAlphaBack))
2406 myMaterial.Common[0].Diffuse.a() = anAlphaFront;
2407 myMaterial.Common[1].Diffuse.a() = anAlphaBack;
2409 myMaterial.Pbr[0].BaseColor.a() = anAlphaFront;
2410 myMaterial.Pbr[1].BaseColor.a() = anAlphaBack;
2413 // do not update material properties in case of zero reflection mode,
2414 // because GL lighting will be disabled by OpenGl_PrimitiveArray::DrawArray() anyway.
2415 const OpenGl_MaterialState& aMatState = myShaderManager->MaterialState();
2416 float anAlphaCutoff = (anAspect->AlphaMode() == Graphic3d_AlphaMode_Mask
2417 || anAspect->AlphaMode() == Graphic3d_AlphaMode_MaskBlend)
2418 ? anAspect->AlphaCutoff()
2420 if (anAspect->ToDrawEdges())
2422 if (anAspect->InteriorStyle() == Aspect_IS_EMPTY
2423 || (anAspect->InteriorStyle() == Aspect_IS_SOLID
2424 && anAspect->EdgeColorRGBA().Alpha() < 1.0f))
2426 anAlphaCutoff = 0.285f;
2429 if (theAspect->ShadingModel() == Graphic3d_TypeOfShadingModel_Unlit)
2431 if (anAlphaCutoff == aMatState.AlphaCutoff())
2436 else if (myMaterial.IsEqual (aMatState.Material())
2437 && toDistinguish == aMatState.ToDistinguish()
2438 && toMapTexture == aMatState.ToMapTexture()
2439 && anAlphaCutoff == aMatState.AlphaCutoff())
2444 myShaderManager->UpdateMaterialStateTo (myMaterial, anAlphaCutoff, toDistinguish, toMapTexture);
2447 // =======================================================================
2448 // function : CheckIsTransparent
2450 // =======================================================================
2451 Standard_Boolean OpenGl_Context::CheckIsTransparent (const OpenGl_Aspects* theAspect,
2452 const Handle(Graphic3d_PresentationAttributes)& theHighlight,
2453 Standard_ShortReal& theAlphaFront,
2454 Standard_ShortReal& theAlphaBack)
2456 const Handle(Graphic3d_Aspects)& anAspect = (!theHighlight.IsNull() && !theHighlight->BasicFillAreaAspect().IsNull())
2457 ? (const Handle(Graphic3d_Aspects)& )theHighlight->BasicFillAreaAspect()
2458 : theAspect->Aspect();
2460 const bool toDistinguish = anAspect->Distinguish();
2461 const Graphic3d_MaterialAspect& aMatFrontSrc = anAspect->FrontMaterial();
2462 const Graphic3d_MaterialAspect& aMatBackSrc = toDistinguish
2463 ? anAspect->BackMaterial()
2466 // handling transparency
2467 if (!theHighlight.IsNull()
2468 && theHighlight->BasicFillAreaAspect().IsNull())
2470 theAlphaFront = theHighlight->ColorRGBA().Alpha();
2471 theAlphaBack = theHighlight->ColorRGBA().Alpha();
2475 theAlphaFront = aMatFrontSrc.Alpha();
2476 theAlphaBack = aMatBackSrc .Alpha();
2479 if (anAspect->AlphaMode() == Graphic3d_AlphaMode_BlendAuto)
2481 return theAlphaFront < 1.0f
2482 || theAlphaBack < 1.0f;
2484 // Graphic3d_AlphaMode_Mask and Graphic3d_AlphaMode_MaskBlend are not considered transparent here
2485 return anAspect->AlphaMode() == Graphic3d_AlphaMode_Blend;
2488 // =======================================================================
2489 // function : SetColor4fv
2491 // =======================================================================
2492 void OpenGl_Context::SetColor4fv (const OpenGl_Vec4& theColor)
2494 if (!myActiveProgram.IsNull())
2496 if (const OpenGl_ShaderUniformLocation& aLoc = myActiveProgram->GetStateLocation (OpenGl_OCCT_COLOR))
2498 myActiveProgram->SetUniform (this, aLoc, Vec4FromQuantityColor (theColor));
2501 #if !defined(GL_ES_VERSION_2_0)
2502 else if (core11ffp != NULL)
2504 core11ffp->glColor4fv (theColor.GetData());
2509 // =======================================================================
2510 // function : SetTypeOfLine
2512 // =======================================================================
2513 void OpenGl_Context::SetTypeOfLine (const Aspect_TypeOfLine theType,
2514 const Standard_ShortReal theFactor)
2516 SetLineStipple (theFactor, Graphic3d_Aspects::DefaultLinePatternForType (theType));
2519 // =======================================================================
2520 // function : SetLineStipple
2522 // =======================================================================
2523 void OpenGl_Context::SetLineStipple (const Standard_ShortReal theFactor,
2524 const uint16_t thePattern)
2526 if (!myActiveProgram.IsNull())
2528 if (const OpenGl_ShaderUniformLocation aPatternLoc = myActiveProgram->GetStateLocation (OpenGl_OCCT_LINE_STIPPLE_PATTERN))
2530 if (hasGlslBitwiseOps != OpenGl_FeatureNotAvailable)
2532 myActiveProgram->SetUniform (this, aPatternLoc, (Standard_Integer )thePattern);
2536 Standard_Integer aPatArr[16] = {};
2537 for (unsigned int aBit = 0; aBit < 16; ++aBit)
2539 aPatArr[aBit] = ((unsigned int)(thePattern) & (1U << aBit)) != 0 ? 1 : 0;
2541 myActiveProgram->SetUniform (this, aPatternLoc, 16, aPatArr);
2543 myActiveProgram->SetUniform (this, myActiveProgram->GetStateLocation (OpenGl_OCCT_LINE_STIPPLE_FACTOR), theFactor);
2548 #if !defined(GL_ES_VERSION_2_0)
2549 if (thePattern != 0xFFFF)
2551 if (core11ffp != NULL)
2553 core11fwd->glEnable (GL_LINE_STIPPLE);
2555 core11ffp->glLineStipple (static_cast<GLint> (theFactor),
2556 static_cast<GLushort> (thePattern));
2561 if (core11ffp != NULL)
2563 core11fwd->glDisable (GL_LINE_STIPPLE);
2569 // =======================================================================
2570 // function : SetLineWidth
2572 // =======================================================================
2573 void OpenGl_Context::SetLineWidth (const Standard_ShortReal theWidth)
2575 #if !defined(GL_ES_VERSION_2_0)
2576 if (core11ffp != NULL)
2579 // glLineWidth() is still defined within Core Profile, but has no effect with values != 1.0f
2580 core11fwd->glLineWidth (theWidth * myLineWidthScale);
2584 // =======================================================================
2585 // function : SetTextureMatrix
2587 // =======================================================================
2588 void OpenGl_Context::SetTextureMatrix (const Handle(Graphic3d_TextureParams)& theParams,
2589 const Standard_Boolean theIsTopDown)
2591 if (theParams.IsNull())
2596 const Graphic3d_Vec2& aScale = theParams->Scale();
2597 const Graphic3d_Vec2& aTrans = theParams->Translation();
2598 if (!myActiveProgram.IsNull())
2600 const GLint aUniLoc = myActiveProgram->GetStateLocation (OpenGl_OCCT_TEXTURE_TRSF2D);
2601 if (aUniLoc == OpenGl_ShaderProgram::INVALID_LOCATION)
2606 // pack transformation parameters
2607 OpenGl_Vec4 aTrsf[2] =
2609 OpenGl_Vec4 (-aTrans.x(), -aTrans.y(), aScale.x(), aScale.y()),
2610 OpenGl_Vec4 (static_cast<float> (std::sin (-theParams->Rotation() * M_PI / 180.0)),
2611 static_cast<float> (std::cos (-theParams->Rotation() * M_PI / 180.0)),
2614 if (caps->isTopDownTextureUV != theIsTopDown)
2617 aTrsf[0].y() = -aTrans.y() + 1.0f / aScale.y();
2618 aTrsf[0].w() = -aScale.y();
2620 myActiveProgram->SetUniform (this, aUniLoc, 2, aTrsf);
2624 #if !defined(GL_ES_VERSION_2_0)
2625 if (core11ffp != NULL)
2627 GLint aMatrixMode = GL_TEXTURE;
2628 ::glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode);
2630 core11ffp->glMatrixMode (GL_TEXTURE);
2631 OpenGl_Mat4 aTextureMat;
2632 if (caps->isTopDownTextureUV != theIsTopDown)
2635 Graphic3d_TransformUtils::Scale (aTextureMat, aScale.x(), -aScale.y(), 1.0f);
2636 Graphic3d_TransformUtils::Translate (aTextureMat, -aTrans.x(), -aTrans.y() + 1.0f / aScale.y(), 0.0f);
2640 Graphic3d_TransformUtils::Scale (aTextureMat, aScale.x(), aScale.y(), 1.0f);
2641 Graphic3d_TransformUtils::Translate (aTextureMat, -aTrans.x(), -aTrans.y(), 0.0f);
2643 Graphic3d_TransformUtils::Rotate (aTextureMat, -theParams->Rotation(), 0.0f, 0.0f, 1.0f);
2644 core11ffp->glLoadMatrixf (aTextureMat.GetData());
2645 core11ffp->glMatrixMode (aMatrixMode);
2650 // =======================================================================
2651 // function : SetPointSize
2653 // =======================================================================
2654 void OpenGl_Context::SetPointSize (const Standard_ShortReal theSize)
2656 if (!myActiveProgram.IsNull())
2658 myActiveProgram->SetUniform (this, myActiveProgram->GetStateLocation (OpenGl_OCCT_POINT_SIZE), theSize);
2659 #if !defined(GL_ES_VERSION_2_0)
2660 //myContext->core11fwd->glEnable (GL_VERTEX_PROGRAM_POINT_SIZE);
2663 #if !defined(GL_ES_VERSION_2_0)
2666 core11fwd->glPointSize (theSize);
2667 if (core20fwd != NULL)
2669 //myContext->core11fwd->glDisable (GL_VERTEX_PROGRAM_POINT_SIZE);
2675 // =======================================================================
2676 // function : SetPointSpriteOrigin
2678 // =======================================================================
2679 void OpenGl_Context::SetPointSpriteOrigin()
2681 #if !defined(GL_ES_VERSION_2_0)
2682 if (core15fwd == NULL)
2687 const int aNewState = !myActiveProgram.IsNull() ? GL_UPPER_LEFT : GL_LOWER_LEFT;
2688 if (myPointSpriteOrig != aNewState)
2690 myPointSpriteOrig = aNewState;
2691 core15fwd->glPointParameteri (GL_POINT_SPRITE_COORD_ORIGIN, aNewState);
2696 // =======================================================================
2697 // function : SetGlNormalizeEnabled
2699 // =======================================================================
2700 Standard_Boolean OpenGl_Context::SetGlNormalizeEnabled (Standard_Boolean isEnabled)
2702 if (isEnabled == myIsGlNormalizeEnabled)
2704 return myIsGlNormalizeEnabled;
2707 Standard_Boolean anOldGlNormalize = myIsGlNormalizeEnabled;
2708 myIsGlNormalizeEnabled = isEnabled;
2709 #if !defined(GL_ES_VERSION_2_0)
2710 if (core11ffp != NULL)
2714 core11fwd->glEnable (GL_NORMALIZE);
2718 core11fwd->glDisable (GL_NORMALIZE);
2723 return anOldGlNormalize;
2726 // =======================================================================
2727 // function : SetShadeModel
2729 // =======================================================================
2730 void OpenGl_Context::SetShadeModel (Graphic3d_TypeOfShadingModel theModel)
2732 #if !defined(GL_ES_VERSION_2_0)
2733 if (core11ffp != NULL)
2735 const Standard_Integer aModel = theModel == Graphic3d_TypeOfShadingModel_PhongFacet
2736 || theModel == Graphic3d_TypeOfShadingModel_PbrFacet ? GL_FLAT : GL_SMOOTH;
2737 if (myShadeModel == aModel)
2741 myShadeModel = aModel;
2742 core11ffp->glShadeModel (aModel);
2749 // =======================================================================
2750 // function : SetPolygonMode
2752 // =======================================================================
2753 Standard_Integer OpenGl_Context::SetPolygonMode (const Standard_Integer theMode)
2755 if (myPolygonMode == theMode)
2757 return myPolygonMode;
2760 const Standard_Integer anOldPolygonMode = myPolygonMode;
2762 myPolygonMode = theMode;
2764 #if !defined(GL_ES_VERSION_2_0)
2765 ::glPolygonMode (GL_FRONT_AND_BACK, (GLenum)theMode);
2768 return anOldPolygonMode;
2771 // =======================================================================
2772 // function : SetPolygonHatchEnabled
2774 // =======================================================================
2775 bool OpenGl_Context::SetPolygonHatchEnabled (const bool theIsEnabled)
2777 if (core11ffp == NULL)
2781 else if (myHatchIsEnabled == theIsEnabled)
2783 return theIsEnabled;
2786 const bool anOldIsEnabled = myHatchIsEnabled;
2787 #if !defined(GL_ES_VERSION_2_0)
2789 && myActiveHatchType != Aspect_HS_SOLID)
2791 core11fwd->glEnable (GL_POLYGON_STIPPLE);
2795 core11fwd->glDisable (GL_POLYGON_STIPPLE);
2798 myHatchIsEnabled = theIsEnabled;
2799 return anOldIsEnabled;
2802 // =======================================================================
2803 // function : SetPolygonHatchStyle
2805 // =======================================================================
2806 Standard_Integer OpenGl_Context::SetPolygonHatchStyle (const Handle(Graphic3d_HatchStyle)& theStyle)
2808 const Standard_Integer aNewStyle = !theStyle.IsNull() ? theStyle->HatchType() : Aspect_HS_SOLID;
2809 if (myActiveHatchType == aNewStyle
2810 || core11ffp == NULL)
2812 return myActiveHatchType;
2815 #if !defined(GL_ES_VERSION_2_0)
2816 if (aNewStyle == Aspect_HS_SOLID)
2818 if (myHatchIsEnabled)
2820 core11fwd->glDisable (GL_POLYGON_STIPPLE);
2822 return myActiveHatchType;
2825 if (myHatchStyles.IsNull()
2826 && !GetResource ("OpenGl_LineAttributes", myHatchStyles))
2828 // share and register for release once the resource is no longer used
2829 myHatchStyles = new OpenGl_LineAttributes();
2830 ShareResource ("OpenGl_LineAttributes", myHatchStyles);
2833 const Standard_Integer anOldType = myActiveHatchType;
2834 myActiveHatchType = aNewStyle;
2835 myHatchStyles->SetTypeOfHatch (this, theStyle);
2836 if (myHatchIsEnabled
2837 && anOldType == Aspect_HS_SOLID)
2839 core11fwd->glEnable (GL_POLYGON_STIPPLE);
2843 return myActiveHatchType;
2847 // =======================================================================
2848 // function : SetPolygonOffset
2850 // =======================================================================
2851 void OpenGl_Context::SetPolygonOffset (const Graphic3d_PolygonOffset& theOffset)
2853 const bool toFillOld = (myPolygonOffset.Mode & Aspect_POM_Fill) == Aspect_POM_Fill;
2854 const bool toFillNew = (theOffset.Mode & Aspect_POM_Fill) == Aspect_POM_Fill;
2855 if (toFillNew != toFillOld)
2859 core11fwd->glEnable (GL_POLYGON_OFFSET_FILL);
2863 core11fwd->glDisable (GL_POLYGON_OFFSET_FILL);
2867 #if !defined(GL_ES_VERSION_2_0)
2868 const bool toLineOld = (myPolygonOffset.Mode & Aspect_POM_Line) == Aspect_POM_Line;
2869 const bool toLineNew = (theOffset.Mode & Aspect_POM_Line) == Aspect_POM_Line;
2870 if (toLineNew != toLineOld)
2874 core11fwd->glEnable (GL_POLYGON_OFFSET_LINE);
2878 core11fwd->glDisable (GL_POLYGON_OFFSET_LINE);
2882 const bool toPointOld = (myPolygonOffset.Mode & Aspect_POM_Point) == Aspect_POM_Point;
2883 const bool toPointNew = (theOffset.Mode & Aspect_POM_Point) == Aspect_POM_Point;
2884 if (toPointNew != toPointOld)
2888 core11fwd->glEnable (GL_POLYGON_OFFSET_POINT);
2892 core11fwd->glDisable (GL_POLYGON_OFFSET_POINT);
2897 if (myPolygonOffset.Factor != theOffset.Factor
2898 || myPolygonOffset.Units != theOffset.Units)
2900 core11fwd->glPolygonOffset (theOffset.Factor, theOffset.Units);
2902 myPolygonOffset = theOffset;
2905 // =======================================================================
2906 // function : SetCamera
2908 // =======================================================================
2909 void OpenGl_Context::SetCamera (const Handle(Graphic3d_Camera)& theCamera)
2911 myCamera = theCamera;
2912 if (!theCamera.IsNull())
2914 ProjectionState.SetCurrent (theCamera->ProjectionMatrixF());
2915 WorldViewState .SetCurrent (theCamera->OrientationMatrixF());
2916 ApplyProjectionMatrix();
2917 ApplyWorldViewMatrix();
2921 // =======================================================================
2922 // function : ApplyModelWorldMatrix
2924 // =======================================================================
2925 void OpenGl_Context::ApplyModelWorldMatrix()
2927 if (myShaderManager->ModelWorldState().ModelWorldMatrix() != ModelWorldState.Current())
2929 myShaderManager->UpdateModelWorldStateTo (ModelWorldState.Current());
2933 // =======================================================================
2934 // function : ApplyWorldViewMatrix
2936 // =======================================================================
2937 void OpenGl_Context::ApplyWorldViewMatrix()
2939 if (myShaderManager->ModelWorldState().ModelWorldMatrix() != THE_IDENTITY_MATRIX)
2941 myShaderManager->UpdateModelWorldStateTo (THE_IDENTITY_MATRIX);
2943 if (myShaderManager->WorldViewState().WorldViewMatrix() != WorldViewState.Current())
2945 myShaderManager->UpdateWorldViewStateTo (WorldViewState.Current());
2949 // =======================================================================
2950 // function : ApplyModelViewMatrix
2952 // =======================================================================
2953 void OpenGl_Context::ApplyModelViewMatrix()
2955 if (myShaderManager->ModelWorldState().ModelWorldMatrix() != ModelWorldState.Current())
2957 myShaderManager->UpdateModelWorldStateTo (ModelWorldState.Current());
2959 if (myShaderManager->WorldViewState().WorldViewMatrix() != WorldViewState.Current())
2961 myShaderManager->UpdateWorldViewStateTo (WorldViewState.Current());
2965 // =======================================================================
2966 // function : ApplyProjectionMatrix
2968 // =======================================================================
2969 void OpenGl_Context::ApplyProjectionMatrix()
2971 if (myShaderManager->ProjectionState().ProjectionMatrix() != ProjectionState.Current())
2973 myShaderManager->UpdateProjectionStateTo (ProjectionState.Current());
2977 // =======================================================================
2978 // function : EnableFeatures
2980 // =======================================================================
2981 void OpenGl_Context::EnableFeatures() const
2986 // =======================================================================
2987 // function : DisableFeatures
2989 // =======================================================================
2990 void OpenGl_Context::DisableFeatures() const
2992 // Disable stuff that's likely to slow down glDrawPixels.
2993 core11fwd->glDisable(GL_DITHER);
2994 core11fwd->glDisable(GL_BLEND);
2995 core11fwd->glDisable(GL_DEPTH_TEST);
2996 core11fwd->glDisable(GL_STENCIL_TEST);
2998 #if !defined(GL_ES_VERSION_2_0)
2999 if (core11ffp == NULL)
3004 core11fwd->glDisable(GL_TEXTURE_1D);
3005 core11fwd->glDisable(GL_TEXTURE_2D);
3007 core11fwd->glDisable(GL_LIGHTING);
3008 core11fwd->glDisable(GL_ALPHA_TEST);
3009 core11fwd->glDisable(GL_FOG);
3010 core11fwd->glDisable(GL_LOGIC_OP);
3012 glPixelTransferi(GL_MAP_COLOR, GL_FALSE);
3013 glPixelTransferi(GL_RED_SCALE, 1);
3014 glPixelTransferi(GL_RED_BIAS, 0);
3015 glPixelTransferi(GL_GREEN_SCALE, 1);
3016 glPixelTransferi(GL_GREEN_BIAS, 0);
3017 glPixelTransferi(GL_BLUE_SCALE, 1);
3018 glPixelTransferi(GL_BLUE_BIAS, 0);
3019 glPixelTransferi(GL_ALPHA_SCALE, 1);
3020 glPixelTransferi(GL_ALPHA_BIAS, 0);
3022 if ((myGlVerMajor >= 1) && (myGlVerMinor >= 2))
3024 if (CheckExtension ("GL_CONVOLUTION_1D_EXT"))
3025 core11fwd->glDisable(GL_CONVOLUTION_1D_EXT);
3027 if (CheckExtension ("GL_CONVOLUTION_2D_EXT"))
3028 core11fwd->glDisable(GL_CONVOLUTION_2D_EXT);
3030 if (CheckExtension ("GL_SEPARABLE_2D_EXT"))
3031 core11fwd->glDisable(GL_SEPARABLE_2D_EXT);
3033 if (CheckExtension ("GL_SEPARABLE_2D_EXT"))
3034 core11fwd->glDisable(GL_HISTOGRAM_EXT);
3036 if (CheckExtension ("GL_MINMAX_EXT"))
3037 core11fwd->glDisable(GL_MINMAX_EXT);
3039 if (CheckExtension ("GL_TEXTURE_3D_EXT"))
3040 core11fwd->glDisable(GL_TEXTURE_3D_EXT);
3045 // =======================================================================
3046 // function : SetColorMaskRGBA
3048 // =======================================================================
3049 void OpenGl_Context::SetColorMaskRGBA (const NCollection_Vec4<bool>& theVal)
3051 core11fwd->glColorMask (theVal.r() ? GL_TRUE : GL_FALSE,
3052 theVal.g() ? GL_TRUE : GL_FALSE,
3053 theVal.b() ? GL_TRUE : GL_FALSE,
3054 theVal.a() ? GL_TRUE : GL_FALSE);
3055 myColorMask = theVal;
3058 // =======================================================================
3059 // function : SetColorMask
3061 // =======================================================================
3062 bool OpenGl_Context::SetColorMask (bool theToWriteColor)
3064 const bool anOldValue = myColorMask.r();
3065 myColorMask.SetValues (theToWriteColor, theToWriteColor, theToWriteColor, caps->buffersOpaqueAlpha ? false : theToWriteColor);
3066 const GLboolean toWrite = theToWriteColor ? GL_TRUE : GL_FALSE;
3067 core11fwd->glColorMask (toWrite, toWrite, toWrite, myColorMask.a() ? GL_TRUE : GL_FALSE);
3071 // =======================================================================
3072 // function : SetSampleAlphaToCoverage
3074 // =======================================================================
3075 bool OpenGl_Context::SetSampleAlphaToCoverage (bool theToEnable)
3077 bool toEnable = myAllowAlphaToCov && theToEnable;
3078 if (myAlphaToCoverage == toEnable)
3080 return myAlphaToCoverage;
3083 if (core15fwd != NULL)
3087 //core15fwd->core15fwd->glSampleCoverage (1.0f, GL_FALSE);
3088 core15fwd->glEnable (GL_SAMPLE_ALPHA_TO_COVERAGE);
3092 core15fwd->glDisable (GL_SAMPLE_ALPHA_TO_COVERAGE);
3096 const bool anOldValue = myAlphaToCoverage;
3097 myAlphaToCoverage = toEnable;
3101 // =======================================================================
3102 // function : GetBufferSubData
3104 // =======================================================================
3105 bool OpenGl_Context::GetBufferSubData (GLenum theTarget, GLintptr theOffset, GLsizeiptr theSize, void* theData)
3107 if (!hasGetBufferData)
3111 #ifdef __EMSCRIPTEN__
3114 Module.ctx.getBufferSubData($0, $1, HEAPU8.subarray($2, $2 + $3));
3115 }, theTarget, theOffset, theData, theSize);
3117 #elif defined(GL_ES_VERSION_2_0)
3118 if (void* aData = core30->glMapBufferRange (theTarget, theOffset, theSize, GL_MAP_READ_BIT))
3120 memcpy (theData, aData, theSize);
3121 core30->glUnmapBuffer (theTarget);
3126 core15fwd->glGetBufferSubData (theTarget, theOffset, theSize, theData);
3131 // =======================================================================
3132 // function : DumpJson
3134 // =======================================================================
3135 void OpenGl_Context::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
3137 OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
3139 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myAnisoMax)
3140 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myTexClamp)
3141 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myMaxTexDim)
3142 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myMaxTexCombined)
3143 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myMaxDumpSizeX)
3144 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myMaxDumpSizeY)
3145 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myMaxClipPlanes)
3146 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myMaxMsaaSamples)
3147 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myMaxDrawBuffers)
3148 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myMaxColorAttachments)
3149 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myGlVerMajor)
3150 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myGlVerMinor)
3151 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myIsInitialized)
3152 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myIsStereoBuffers)
3153 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myIsGlNormalizeEnabled)
3155 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myHasRayTracing)
3156 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myHasRayTracingTextures)
3157 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myHasRayTracingAdaptiveSampling)
3158 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myHasRayTracingAdaptiveSamplingAtomic)
3160 for (int i = 0; i < 4; i++)
3162 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myViewport[i])
3165 for (int i = 0; i < 4; i++)
3167 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myViewportVirt[i])
3170 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myPointSpriteOrig)
3171 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myRenderMode)
3172 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myPolygonMode)
3173 OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &myPolygonOffset)
3174 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myToCullBackFaces)
3175 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myReadBuffer)
3177 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myDefaultVao)
3178 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myColorMask)
3179 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myAllowAlphaToCov)
3180 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myAlphaToCoverage)
3181 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myIsGlDebugCtx)
3183 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myResolution)
3184 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myResolutionRatio)
3186 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myLineWidthScale)
3187 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myLineFeather)
3188 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myRenderScale)
3189 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myRenderScaleInv)
3191 OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &ModelWorldState)
3192 OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &WorldViewState)
3193 OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &ProjectionState)
3196 // =======================================================================
3197 // function : DumpJsonOpenGlState
3199 // =======================================================================
3200 void OpenGl_Context::DumpJsonOpenGlState (Standard_OStream& theOStream, Standard_Integer)
3202 GLboolean isEnableBlend = glIsEnabled (GL_BLEND);
3203 GLboolean isEnableCullFace = glIsEnabled (GL_CULL_FACE);
3204 GLboolean isEnableDepthTest = glIsEnabled (GL_DEPTH_TEST);
3206 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, isEnableBlend)
3207 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, isEnableCullFace)
3208 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, isEnableDepthTest)