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.
18 #elif defined(__APPLE__)
19 // macOS 10.4 deprecated OpenGL framework - suppress useless warnings
20 #define GL_SILENCE_DEPRECATION
23 #include <OpenGl_Context.hxx>
25 #include <OpenGl_ArbTBO.hxx>
26 #include <OpenGl_ArbIns.hxx>
27 #include <OpenGl_ArbDbg.hxx>
28 #include <OpenGl_ArbFBO.hxx>
29 #include <OpenGl_ExtGS.hxx>
30 #include <OpenGl_ArbSamplerObject.hxx>
31 #include <OpenGl_ArbTexBindless.hxx>
32 #include <OpenGl_GlCore46.hxx>
33 #include <OpenGl_FrameBuffer.hxx>
34 #include <OpenGl_FrameStats.hxx>
35 #include <OpenGl_Sampler.hxx>
36 #include <OpenGl_ShaderManager.hxx>
37 #include <OpenGl_TextureSetPairIterator.hxx>
38 #include <OpenGl_Workspace.hxx>
39 #include <OpenGl_Aspects.hxx>
41 #include <Graphic3d_TransformUtils.hxx>
42 #include <Graphic3d_RenderingParams.hxx>
43 #include <Image_SupportedFormats.hxx>
44 #include <Message_Messenger.hxx>
45 #include <NCollection_Vector.hxx>
46 #include <Standard_ProgramError.hxx>
47 #include <Standard_WarningDisableFunctionCast.hxx>
49 #if defined(_WIN32) && defined(max)
54 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Context,Standard_Transient)
59 #pragma comment(lib, "libEGL.lib")
63 #elif defined(HAVE_XLIB)
64 #include <GL/glx.h> // glXGetProcAddress()
65 #elif defined(__APPLE__)
67 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
70 #include <OpenGL/OpenGL.h>
71 #include <CoreGraphics/CoreGraphics.h>
78 #include <emscripten.h>
79 #include <emscripten/html5.h>
82 #if defined(HAVE_GLES2) || defined(OCCT_UWP) || defined(__ANDROID__) || defined(__QNX__) || defined(__EMSCRIPTEN__) || (defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE)
88 static const Handle(OpenGl_Resource) NULL_GL_RESOURCE;
89 static const OpenGl_Mat4 THE_IDENTITY_MATRIX;
91 //! Add key-value pair to the dictionary.
92 static void addInfo (TColStd_IndexedDataMapOfStringString& theDict,
93 const TCollection_AsciiString& theKey,
94 const TCollection_AsciiString& theValue)
96 theDict.ChangeFromIndex (theDict.Add (theKey, theValue)) = theValue;
99 //! Add key-value pair to the dictionary.
100 static void addInfo (TColStd_IndexedDataMapOfStringString& theDict,
101 const TCollection_AsciiString& theKey,
102 const char* theValue)
104 TCollection_AsciiString aValue (theValue != NULL ? theValue : "");
105 theDict.ChangeFromIndex (theDict.Add (theKey, aValue)) = aValue;
109 // =======================================================================
110 // function : OpenGl_Context
112 // =======================================================================
113 OpenGl_Context::OpenGl_Context (const Handle(OpenGl_Caps)& theCaps)
129 caps (!theCaps.IsNull() ? theCaps : new OpenGl_Caps()),
130 hasGetBufferData (Standard_False),
131 #if defined(OCC_USE_GLES2)
132 hasPackRowLength (Standard_False),
133 hasUnpackRowLength (Standard_False),
134 hasHighp (Standard_False),
135 hasUintIndex(Standard_False),
136 hasTexRGBA8(Standard_False),
138 hasPackRowLength (Standard_True),
139 hasUnpackRowLength (Standard_True),
140 hasHighp (Standard_True),
141 hasUintIndex(Standard_True),
142 hasTexRGBA8(Standard_True),
144 hasTexFloatLinear (Standard_False),
145 hasTexSRGB (Standard_False),
146 hasFboSRGB (Standard_False),
147 hasSRGBControl (Standard_False),
148 hasFboRenderMipmap (Standard_False),
149 #if defined(OCC_USE_GLES2)
150 hasFlatShading (OpenGl_FeatureNotAvailable),
152 hasFlatShading (OpenGl_FeatureInCore),
154 hasGlslBitwiseOps (OpenGl_FeatureNotAvailable),
155 hasDrawBuffers (OpenGl_FeatureNotAvailable),
156 hasFloatBuffer (OpenGl_FeatureNotAvailable),
157 hasHalfFloatBuffer (OpenGl_FeatureNotAvailable),
158 hasSampleVariables (OpenGl_FeatureNotAvailable),
159 hasGeometryStage (OpenGl_FeatureNotAvailable),
160 arbDrawBuffers (Standard_False),
161 arbNPTW (Standard_False),
162 arbTexRG (Standard_False),
163 arbTexFloat (Standard_False),
164 arbSamplerObject (NULL),
165 arbTexBindless (NULL),
167 arbTboRGB32 (Standard_False),
168 arbClipControl (Standard_False),
173 arbSampleShading (Standard_False),
174 arbDepthClamp (Standard_False),
175 extFragDepth (Standard_False),
176 extDrawBuffers (Standard_False),
178 extBgra(Standard_False),
179 extTexR16(Standard_False),
180 extAnis(Standard_False),
181 extPDS (Standard_False),
182 atiMem (Standard_False),
183 nvxMem (Standard_False),
184 oesSampleVariables (Standard_False),
185 oesStdDerivatives (Standard_False),
189 mySharedResources (new OpenGl_ResourcesMap()),
190 myDelayed (new OpenGl_DelayReleaseMap()),
191 myUnusedResources (new OpenGl_ResourcesStack()),
193 myGlLibHandle (NULL),
194 myFuncs (new OpenGl_GlFunctions()),
196 #if defined(OCC_USE_GLES2)
197 Aspect_GraphicsLibrary_OpenGLES
199 Aspect_GraphicsLibrary_OpenGL
202 mySupportedFormats (new Image_SupportedFormats()),
204 myTexClamp (GL_CLAMP_TO_EDGE),
206 myMaxTexCombined (1),
207 myMaxTexUnitsFFP (1),
208 myMaxDumpSizeX (1024),
209 myMaxDumpSizeY (1024),
212 myMaxDrawBuffers (1),
213 myMaxColorAttachments (1),
216 myIsInitialized (Standard_False),
217 myIsStereoBuffers (Standard_False),
218 myHasMsaaTextures (Standard_False),
219 myIsGlNormalizeEnabled (Standard_False),
220 mySpriteTexUnit (Graphic3d_TextureUnit_PointSprite),
221 myHasRayTracing (Standard_False),
222 myHasRayTracingTextures (Standard_False),
223 myHasRayTracingAdaptiveSampling (Standard_False),
224 myHasRayTracingAdaptiveSamplingAtomic (Standard_False),
225 myHasPBR (Standard_False),
226 myPBREnvLUTTexUnit (Graphic3d_TextureUnit_PbrEnvironmentLUT),
227 myPBRDiffIBLMapSHTexUnit (Graphic3d_TextureUnit_PbrIblDiffuseSH),
228 myPBRSpecIBLMapTexUnit (Graphic3d_TextureUnit_PbrIblSpecular),
229 myShadowMapTexUnit (Graphic3d_TextureUnit_ShadowMap),
230 myDepthPeelingDepthTexUnit (Graphic3d_TextureUnit_DepthPeelingDepth),
231 myDepthPeelingFrontColorTexUnit (Graphic3d_TextureUnit_DepthPeelingFrontColor),
232 myFrameStats (new OpenGl_FrameStats()),
233 myActiveMockTextures (0),
234 myActiveHatchType (Aspect_HS_SOLID),
235 myHatchIsEnabled (false),
236 myPointSpriteOrig (GL_UPPER_LEFT),
237 myRenderMode (GL_RENDER),
238 myShadeModel (GL_SMOOTH),
239 myPolygonMode (GL_FILL),
240 myToCullBackFaces (false),
242 myDrawBuffers (0, 7),
245 myAlphaToCoverage (false),
246 myIsGlDebugCtx (false),
247 myIsWindowDeepColor (false),
248 myIsSRgbWindow (false),
249 myIsSRgbActive (false),
250 myResolution (Graphic3d_RenderingParams::THE_DEFAULT_RESOLUTION),
251 myResolutionRatio (1.0f),
252 myLineWidthScale (1.0f),
253 myLineFeather (1.0f),
254 myRenderScale (1.0f),
255 myRenderScaleInv (1.0f)
261 myViewportVirt[0] = 0;
262 myViewportVirt[1] = 0;
263 myViewportVirt[2] = 0;
264 myViewportVirt[3] = 0;
266 myPolygonOffset.Mode = Aspect_POM_Off;
267 myPolygonOffset.Factor = 0.0f;
268 myPolygonOffset.Units = 0.0f;
270 // system-dependent fields
271 #if defined(HAVE_EGL)
272 myDisplay = (Aspect_Display )EGL_NO_DISPLAY;
273 myWindow = (Aspect_Drawable )EGL_NO_SURFACE;
274 myGContext = (Aspect_RenderingContext )EGL_NO_CONTEXT;
275 #elif defined(__APPLE__) && !defined(HAVE_XLIB)
276 // Vendors can not extend functionality on this system
277 // and developers are limited to OpenGL support provided by Mac OS X SDK.
278 // We retrieve function pointers from system library
279 // to generalize extensions support on all platforms.
280 // In this way we also reach binary compatibility benefit between OS releases
281 // if some newest functionality is optionally used.
282 // Notice that GL version / extension availability checks are required
283 // because function pointers may be available but not functionality itself
284 // (depends on renderer).
285 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
286 myGlLibHandle = dlopen ("/System/Library/Frameworks/OpenGLES.framework/OpenGLES", RTLD_LAZY);
288 myGlLibHandle = dlopen ("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL", RTLD_LAZY);
292 memset (myFuncs.get(), 0, sizeof(OpenGl_GlFunctions));
293 myShaderManager = new OpenGl_ShaderManager (this);
296 // =======================================================================
297 // function : ~OpenGl_Context
299 // =======================================================================
300 OpenGl_Context::~OpenGl_Context()
302 // release clean up queue
305 // release default VAO
306 if (myDefaultVao != 0
310 core32->glDeleteVertexArrays (1, &myDefaultVao);
314 // release mock textures
315 if (!myTextureRgbaBlack.IsNull())
317 myTextureRgbaBlack->Release (this);
318 myTextureRgbaBlack.Nullify();
320 if (!myTextureRgbaWhite.IsNull())
322 myTextureRgbaWhite->Release (this);
323 myTextureRgbaWhite.Nullify();
326 // release default FBO
327 if (!myDefaultFbo.IsNull())
329 myDefaultFbo->Release (this);
330 myDefaultFbo.Nullify();
333 // release shared resources if any
334 if (mySharedResources->GetRefCount() <= 1)
336 myShaderManager.Nullify();
337 for (NCollection_DataMap<TCollection_AsciiString, Handle(OpenGl_Resource)>::Iterator anIter (*mySharedResources);
338 anIter.More(); anIter.Next())
340 anIter.Value()->Release (this);
343 // release delayed resources added during deletion of shared resources
344 while (!myUnusedResources->IsEmpty())
346 myUnusedResources->First()->Release (this);
347 myUnusedResources->RemoveFirst();
350 else if (myShaderManager->IsSameContext (this))
352 myShaderManager->SetContext (NULL);
354 mySharedResources.Nullify();
363 if (myGapi == Aspect_GraphicsLibrary_OpenGL)
365 myFuncs->glGetPointerv (GL_DEBUG_CALLBACK_USER_PARAM, &aPtr);
368 || myGapi != Aspect_GraphicsLibrary_OpenGL)
370 arbDbg->glDebugMessageCallback (NULL, NULL);
372 myIsGlDebugCtx = Standard_False;
376 // =======================================================================
377 // function : forcedRelease
379 // =======================================================================
380 void OpenGl_Context::forcedRelease()
383 for (NCollection_DataMap<TCollection_AsciiString, Handle(OpenGl_Resource)>::Iterator anIter (*mySharedResources);
384 anIter.More(); anIter.Next())
386 anIter.Value()->Release (this);
388 mySharedResources->Clear();
389 myShaderManager->clear();
390 myShaderManager->SetContext (NULL);
392 // release delayed resources added during deletion of shared resources
393 while (!myUnusedResources->IsEmpty())
395 myUnusedResources->First()->Release (this);
396 myUnusedResources->RemoveFirst();
400 // =======================================================================
401 // function : ResizeViewport
403 // =======================================================================
404 void OpenGl_Context::ResizeViewport (const Standard_Integer* theRect)
406 core11fwd->glViewport (theRect[0], theRect[1], theRect[2], theRect[3]);
407 myViewport[0] = theRect[0];
408 myViewport[1] = theRect[1];
409 myViewport[2] = theRect[2];
410 myViewport[3] = theRect[3];
411 if (HasRenderScale())
413 myViewportVirt[0] = Standard_Integer(theRect[0] * myRenderScaleInv);
414 myViewportVirt[1] = Standard_Integer(theRect[1] * myRenderScaleInv);
415 myViewportVirt[2] = Standard_Integer(theRect[2] * myRenderScaleInv);
416 myViewportVirt[3] = Standard_Integer(theRect[3] * myRenderScaleInv);
420 myViewportVirt[0] = theRect[0];
421 myViewportVirt[1] = theRect[1];
422 myViewportVirt[2] = theRect[2];
423 myViewportVirt[3] = theRect[3];
427 static Standard_Integer stereoToMonoBuffer (const Standard_Integer theBuffer)
442 // =======================================================================
443 // function : SetReadBuffer
445 // =======================================================================
446 void OpenGl_Context::SetReadBuffer (const Standard_Integer theReadBuffer)
448 if (myGapi == Aspect_GraphicsLibrary_OpenGLES)
453 myReadBuffer = !myIsStereoBuffers ? stereoToMonoBuffer (theReadBuffer) : theReadBuffer;
454 if (myReadBuffer < GL_COLOR_ATTACHMENT0
457 arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
459 core11fwd->glReadBuffer (myReadBuffer);
462 // =======================================================================
463 // function : SetDrawBuffer
465 // =======================================================================
466 void OpenGl_Context::SetDrawBuffer (const Standard_Integer theDrawBuffer)
468 if (myGapi == Aspect_GraphicsLibrary_OpenGLES)
473 const Standard_Integer aDrawBuffer = !myIsStereoBuffers ? stereoToMonoBuffer (theDrawBuffer) : theDrawBuffer;
474 if (aDrawBuffer < GL_COLOR_ATTACHMENT0
477 arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
479 core11fwd->glDrawBuffer (aDrawBuffer);
481 myDrawBuffers.Init (GL_NONE);
482 myDrawBuffers.SetValue (0, aDrawBuffer);
485 // =======================================================================
486 // function : SetDrawBuffers
488 // =======================================================================
489 void OpenGl_Context::SetDrawBuffers (const Standard_Integer theNb, const Standard_Integer* theDrawBuffers)
491 Standard_ASSERT_RETURN (hasDrawBuffers, "Multiple draw buffers feature is not supported by the context", Standard_ASSERT_DO_NOTHING());
493 if (myDrawBuffers.Length() < theNb)
495 // should actually never happen here
496 myDrawBuffers.Resize (0, theNb - 1, false);
498 myDrawBuffers.Init (GL_NONE);
500 Standard_Boolean useDefaultFbo = Standard_False;
501 for (Standard_Integer anI = 0; anI < theNb; ++anI)
503 if (theDrawBuffers[anI] < GL_COLOR_ATTACHMENT0 && theDrawBuffers[anI] != GL_NONE)
505 useDefaultFbo = Standard_True;
509 myDrawBuffers.SetValue (anI, theDrawBuffers[anI]);
512 if (arbFBO != NULL && useDefaultFbo)
514 arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
517 myFuncs->glDrawBuffers (theNb, (const GLenum*)theDrawBuffers);
520 // =======================================================================
521 // function : SetFrameBufferSRGB
523 // =======================================================================
524 void OpenGl_Context::SetFrameBufferSRGB (bool theIsFbo, bool theIsFboSRgb)
528 myIsSRgbActive = false;
531 myIsSRgbActive = ToRenderSRGB()
532 && (theIsFbo || myIsSRgbWindow)
541 core11fwd->glEnable (GL_FRAMEBUFFER_SRGB);
545 core11fwd->glDisable (GL_FRAMEBUFFER_SRGB);
549 // =======================================================================
550 // function : SetCullBackFaces
552 // =======================================================================
553 void OpenGl_Context::SetCullBackFaces (bool theToEnable)
555 if (myToCullBackFaces == theToEnable)
560 myToCullBackFaces = theToEnable;
563 //glCullFace (GL_BACK); GL_BACK by default
564 core11fwd->glEnable (GL_CULL_FACE);
568 core11fwd->glDisable (GL_CULL_FACE);
572 // =======================================================================
573 // function : FetchState
575 // =======================================================================
576 void OpenGl_Context::FetchState()
578 if (myGapi == Aspect_GraphicsLibrary_OpenGLES)
583 // cache feedback mode state
584 if (core11ffp != NULL)
586 core11fwd->glGetIntegerv (GL_RENDER_MODE, &myRenderMode);
587 core11fwd->glGetIntegerv (GL_SHADE_MODEL, &myShadeModel);
590 // cache read buffers state
591 core11fwd->glGetIntegerv (GL_READ_BUFFER, &myReadBuffer);
593 // cache draw buffers state
594 if (myDrawBuffers.Length() < myMaxDrawBuffers)
596 myDrawBuffers.Resize (0, myMaxDrawBuffers - 1, false);
598 myDrawBuffers.Init (GL_NONE);
600 Standard_Integer aDrawBuffer = GL_NONE;
601 if (myMaxDrawBuffers == 1)
603 core11fwd->glGetIntegerv (GL_DRAW_BUFFER, &aDrawBuffer);
604 myDrawBuffers.SetValue (0, aDrawBuffer);
608 for (Standard_Integer anI = 0; anI < myMaxDrawBuffers; ++anI)
610 core11fwd->glGetIntegerv (GL_DRAW_BUFFER0 + anI, &aDrawBuffer);
611 myDrawBuffers.SetValue (anI, aDrawBuffer);
616 // =======================================================================
619 // =======================================================================
620 void OpenGl_Context::Share (const Handle(OpenGl_Context)& theShareCtx)
622 if (!theShareCtx.IsNull())
624 mySharedResources = theShareCtx->mySharedResources;
625 myDelayed = theShareCtx->myDelayed;
626 myUnusedResources = theShareCtx->myUnusedResources;
627 myShaderManager = theShareCtx->myShaderManager;
631 #if !defined(__APPLE__) || defined(HAVE_XLIB)
633 // =======================================================================
634 // function : IsCurrent
636 // =======================================================================
637 Standard_Boolean OpenGl_Context::IsCurrent() const
639 #if defined(HAVE_EGL)
640 if ((EGLDisplay )myDisplay == EGL_NO_DISPLAY
641 || (EGLContext )myGContext == EGL_NO_CONTEXT)
643 return Standard_False;
646 return (((EGLDisplay )myDisplay == eglGetCurrentDisplay())
647 && ((EGLContext )myGContext == eglGetCurrentContext())
648 && ((EGLSurface )myWindow == eglGetCurrentSurface (EGL_DRAW)));
649 #elif defined(_WIN32)
650 if (myDisplay == NULL || myGContext == NULL)
652 return Standard_False;
654 return (( (HDC )myDisplay == wglGetCurrentDC())
655 && ((HGLRC )myGContext == wglGetCurrentContext()));
656 #elif defined(HAVE_XLIB)
657 if (myDisplay == NULL || myWindow == 0 || myGContext == 0)
659 return Standard_False;
662 return ( ((Display* )myDisplay == glXGetCurrentDisplay())
663 && ((GLXContext )myGContext == glXGetCurrentContext())
664 && ((GLXDrawable )myWindow == glXGetCurrentDrawable()));
666 return Standard_False;
670 // =======================================================================
671 // function : MakeCurrent
673 // =======================================================================
674 Standard_Boolean OpenGl_Context::MakeCurrent()
676 #if defined(HAVE_EGL)
677 if ((EGLDisplay )myDisplay == EGL_NO_DISPLAY
678 || (EGLContext )myGContext == EGL_NO_CONTEXT)
680 Standard_ProgramError_Raise_if (myIsInitialized, "OpenGl_Context::Init() should be called before!");
681 return Standard_False;
684 if (eglMakeCurrent ((EGLDisplay )myDisplay, (EGLSurface )myWindow, (EGLSurface )myWindow, (EGLContext )myGContext) != EGL_TRUE)
686 // if there is no current context it might be impossible to use glGetError() correctly
687 PushMessage (GL_DEBUG_SOURCE_WINDOW_SYSTEM, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH,
688 "eglMakeCurrent() has failed!");
689 myIsInitialized = Standard_False;
690 return Standard_False;
692 #elif defined(_WIN32)
693 if (myDisplay == NULL || myGContext == NULL)
695 Standard_ProgramError_Raise_if (myIsInitialized, "OpenGl_Context::Init() should be called before!");
696 return Standard_False;
699 // technically it should be safe to activate already bound GL context
700 // however some drivers (Intel etc.) may FAIL doing this for unknown reason
703 myShaderManager->SetContext (this);
704 return Standard_True;
706 else if (wglMakeCurrent ((HDC )myDisplay, (HGLRC )myGContext) != TRUE)
708 // notice that glGetError() couldn't be used here!
709 wchar_t* aMsgBuff = NULL;
710 DWORD anErrorCode = GetLastError();
711 FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
712 NULL, anErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (wchar_t* )&aMsgBuff, 0, NULL);
713 TCollection_ExtendedString aMsg ("wglMakeCurrent() has failed. ");
714 if (aMsgBuff != NULL)
716 aMsg += (Standard_ExtString )aMsgBuff;
717 LocalFree (aMsgBuff);
719 PushMessage (GL_DEBUG_SOURCE_WINDOW_SYSTEM, GL_DEBUG_TYPE_ERROR, (unsigned int )anErrorCode, GL_DEBUG_SEVERITY_HIGH, aMsg);
720 myIsInitialized = Standard_False;
721 return Standard_False;
723 #elif defined(HAVE_XLIB)
724 if (myDisplay == NULL || myWindow == 0 || myGContext == 0)
726 Standard_ProgramError_Raise_if (myIsInitialized, "OpenGl_Context::Init() should be called before!");
727 return Standard_False;
730 if (!glXMakeCurrent ((Display* )myDisplay, (GLXDrawable )myWindow, (GLXContext )myGContext))
732 // if there is no current context it might be impossible to use glGetError() correctly
733 PushMessage (GL_DEBUG_SOURCE_WINDOW_SYSTEM, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH,
734 "glXMakeCurrent() has failed!");
735 myIsInitialized = Standard_False;
736 return Standard_False;
740 if (!myIsInitialized)
742 throw Standard_ProgramError ("OpenGl_Context::Init() should be called before!");
745 myShaderManager->SetContext (this);
746 return Standard_True;
749 // =======================================================================
750 // function : SwapBuffers
752 // =======================================================================
753 void OpenGl_Context::SwapBuffers()
755 #if defined(HAVE_EGL)
756 if ((EGLSurface )myWindow != EGL_NO_SURFACE)
758 eglSwapBuffers ((EGLDisplay )myDisplay, (EGLSurface )myWindow);
760 #elif defined(_WIN32)
761 if ((HDC )myDisplay != NULL)
763 ::SwapBuffers ((HDC )myDisplay);
764 core11fwd->glFlush();
766 #elif defined(HAVE_XLIB)
767 if ((Display* )myDisplay != NULL)
769 glXSwapBuffers ((Display* )myDisplay, (GLXDrawable )myWindow);
778 // =======================================================================
779 // function : SetSwapInterval
781 // =======================================================================
782 Standard_Boolean OpenGl_Context::SetSwapInterval (const Standard_Integer theInterval)
784 #if defined(HAVE_EGL)
785 if (::eglSwapInterval ((EGLDisplay )myDisplay, theInterval) == EGL_TRUE)
787 return Standard_True;
789 #elif defined(_WIN32)
790 if (myFuncs->wglSwapIntervalEXT != NULL)
792 myFuncs->wglSwapIntervalEXT (theInterval);
793 return Standard_True;
795 #elif defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
796 (void )theInterval; // vsync cannot be turned OFF on iOS
797 #elif defined(__APPLE__)
798 if (::CGLSetParameter (CGLGetCurrentContext(), kCGLCPSwapInterval, &theInterval) == kCGLNoError)
800 return Standard_True;
802 #elif defined(HAVE_XLIB)
803 if (theInterval == -1
804 && myFuncs->glXSwapIntervalEXT != NULL)
806 typedef int (*glXSwapIntervalEXT_t_x)(Display* theDisplay, GLXDrawable theDrawable, int theInterval);
807 glXSwapIntervalEXT_t_x aFuncPtr = (glXSwapIntervalEXT_t_x )myFuncs->glXSwapIntervalEXT;
808 aFuncPtr ((Display* )myDisplay, (GLXDrawable )myWindow, theInterval);
809 return Standard_True;
811 else if (myFuncs->glXSwapIntervalSGI != NULL)
813 myFuncs->glXSwapIntervalSGI (theInterval);
814 return Standard_True;
819 return Standard_False;
822 // =======================================================================
823 // function : findProc
825 // =======================================================================
826 void* OpenGl_Context::findProc (const char* theFuncName)
828 #if defined(HAVE_EGL)
829 return (void* )eglGetProcAddress (theFuncName);
830 #elif defined(_WIN32)
831 return (void* )wglGetProcAddress (theFuncName);
832 #elif defined(HAVE_XLIB)
833 return (void* )glXGetProcAddress ((const GLubyte* )theFuncName);
834 #elif defined(__APPLE__)
835 return (myGlLibHandle != NULL) ? dlsym (myGlLibHandle, theFuncName) : NULL;
842 // =======================================================================
843 // function : CheckExtension
845 // =======================================================================
846 Standard_Boolean OpenGl_Context::CheckExtension (const char* theExtName) const
848 if (theExtName == NULL)
851 std::cerr << "CheckExtension called with NULL string!\n";
853 return Standard_False;
855 else if (caps->contextNoExtensions)
857 return Standard_False;
860 // available since OpenGL 3.0
861 // and the ONLY way to check extensions with OpenGL 3.1+ core profile
862 if (myGapi == Aspect_GraphicsLibrary_OpenGL
863 && IsGlGreaterEqual (3, 0)
864 && myFuncs->glGetStringi != NULL)
867 core11fwd->glGetIntegerv (GL_NUM_EXTENSIONS, &anExtNb);
868 const size_t anExtNameLen = strlen (theExtName);
869 for (GLint anIter = 0; anIter < anExtNb; ++anIter)
871 const char* anExtension = (const char* )myFuncs->glGetStringi (GL_EXTENSIONS, (GLuint )anIter);
872 const size_t aTestExtNameLen = strlen (anExtension);
873 if (aTestExtNameLen == anExtNameLen
874 && strncmp (anExtension, theExtName, anExtNameLen) == 0)
876 return Standard_True;
879 return Standard_False;
882 // use old way with huge string for all extensions
883 const char* anExtString = (const char* )core11fwd->glGetString (GL_EXTENSIONS);
884 if (anExtString == NULL)
886 Messenger()->Send ("TKOpenGL: glGetString (GL_EXTENSIONS) has returned NULL! No GL context?", Message_Warning);
887 return Standard_False;
889 if (!CheckExtension (anExtString, theExtName))
891 return Standard_False;
894 #ifdef __EMSCRIPTEN__
895 //! Check if WebGL extension is available and activate it
896 //! (usage of extension without activation will generate errors).
897 if (EMSCRIPTEN_WEBGL_CONTEXT_HANDLE aWebGlCtx = emscripten_webgl_get_current_context())
899 if (emscripten_webgl_enable_extension (aWebGlCtx, theExtName))
901 return Standard_True;
904 return Standard_False;
906 return Standard_True;
910 // =======================================================================
911 // function : CheckExtension
913 // =======================================================================
914 Standard_Boolean OpenGl_Context::CheckExtension (const char* theExtString,
915 const char* theExtName)
917 if (theExtString == NULL)
919 return Standard_False;
922 // Search for theExtName in the extensions string.
923 // Use of strstr() is not sufficient because extension names can be prefixes of other extension names.
924 char* aPtrIter = (char* )theExtString;
925 const char* aPtrEnd = aPtrIter + strlen (theExtString);
926 const size_t anExtNameLen = strlen (theExtName);
927 while (aPtrIter < aPtrEnd)
929 const size_t n = strcspn (aPtrIter, " ");
930 if ((n == anExtNameLen) && (strncmp (aPtrIter, theExtName, anExtNameLen) == 0))
932 return Standard_True;
936 return Standard_False;
939 #if !defined(__APPLE__) || defined(HAVE_XLIB)
941 // =======================================================================
944 // =======================================================================
945 Standard_Boolean OpenGl_Context::Init (const Standard_Boolean theIsCoreProfile)
949 return Standard_True;
952 #if defined(HAVE_EGL)
953 myDisplay = (Aspect_Display )eglGetCurrentDisplay();
954 myGContext = (Aspect_RenderingContext )eglGetCurrentContext();
955 myWindow = (Aspect_Drawable )eglGetCurrentSurface(EGL_DRAW);
956 #elif defined(_WIN32)
957 myDisplay = (Aspect_Handle )wglGetCurrentDC();
958 myGContext = (Aspect_RenderingContext )wglGetCurrentContext();
959 #elif defined(HAVE_XLIB)
960 myDisplay = (Aspect_Display )glXGetCurrentDisplay();
961 myGContext = (Aspect_RenderingContext )glXGetCurrentContext();
962 myWindow = (Aspect_Drawable )glXGetCurrentDrawable();
966 if (myGContext == NULL)
968 return Standard_False;
971 init (theIsCoreProfile);
972 myIsInitialized = Standard_True;
973 return Standard_True;
978 // =======================================================================
981 // =======================================================================
982 Standard_Boolean OpenGl_Context::Init (const Aspect_Drawable theSurface,
983 const Aspect_Display theDisplay,
984 const Aspect_RenderingContext theContext,
985 const Standard_Boolean theIsCoreProfile)
987 Standard_ProgramError_Raise_if (myIsInitialized, "OpenGl_Context::Init() should be called only once!");
988 myWindow = theSurface;
989 myDisplay = theDisplay;
990 myGContext = theContext;
991 if (myGContext == NULL || !MakeCurrent())
993 return Standard_False;
996 init (theIsCoreProfile);
997 myIsInitialized = Standard_True;
998 return Standard_True;
1001 // =======================================================================
1002 // function : FormatGlEnumHex
1004 // =======================================================================
1005 TCollection_AsciiString OpenGl_Context::FormatGlEnumHex (int theGlEnum)
1008 Sprintf (aBuff, theGlEnum < (int )std::numeric_limits<uint16_t>::max()
1010 : "0x%08X", theGlEnum);
1014 // =======================================================================
1015 // function : FormatSize
1017 // =======================================================================
1018 TCollection_AsciiString OpenGl_Context::FormatSize (Standard_Size theSize)
1021 Sprintf (aBuff, "%" PRIu64, (uint64_t )theSize);
1025 // =======================================================================
1026 // function : FormatPointer
1028 // =======================================================================
1029 TCollection_AsciiString OpenGl_Context::FormatPointer (const void* thePtr)
1032 Sprintf (aBuff, "0x%" PRIXPTR, (uintptr_t )thePtr);
1036 // =======================================================================
1037 // function : FormatGlError
1039 // =======================================================================
1040 TCollection_AsciiString OpenGl_Context::FormatGlError (int theGlError)
1044 case GL_INVALID_ENUM: return "GL_INVALID_ENUM";
1045 case GL_INVALID_VALUE: return "GL_INVALID_VALUE";
1046 case GL_INVALID_OPERATION: return "GL_INVALID_OPERATION";
1047 case GL_STACK_OVERFLOW: return "GL_STACK_OVERFLOW";
1048 case GL_STACK_UNDERFLOW: return "GL_STACK_UNDERFLOW";
1049 case GL_OUT_OF_MEMORY: return "GL_OUT_OF_MEMORY";
1050 case GL_INVALID_FRAMEBUFFER_OPERATION: return "GL_INVALID_FRAMEBUFFER_OPERATION";
1052 return FormatGlEnumHex (theGlError);
1055 // =======================================================================
1056 // function : ResetErrors
1058 // =======================================================================
1059 bool OpenGl_Context::ResetErrors (const bool theToPrintErrors)
1062 int anErr = core11fwd->glGetError();
1063 const bool hasError = anErr != GL_NO_ERROR;
1064 if (!theToPrintErrors)
1066 for (; anErr != GL_NO_ERROR && aPrevErr != anErr; aPrevErr = anErr, anErr = core11fwd->glGetError())
1073 for (; anErr != GL_NO_ERROR && aPrevErr != anErr; aPrevErr = anErr, anErr = core11fwd->glGetError())
1075 const TCollection_ExtendedString aMsg = TCollection_ExtendedString ("Unhandled GL error: ") + FormatGlError (anErr);
1076 PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_OTHER, 0, GL_DEBUG_SEVERITY_LOW, aMsg);
1081 // =======================================================================
1082 // function : ReadGlVersion
1084 // =======================================================================
1085 void OpenGl_Context::ReadGlVersion (Standard_Integer& theGlVerMajor,
1086 Standard_Integer& theGlVerMinor)
1088 OpenGl_GlFunctions::readGlVersion (theGlVerMajor, theGlVerMinor);
1091 static Standard_CString THE_DBGMSG_UNKNOWN = "UNKNOWN";
1092 static Standard_CString THE_DBGMSG_SOURCES[] =
1094 ".OpenGL", // GL_DEBUG_SOURCE_API
1095 ".WinSystem", // GL_DEBUG_SOURCE_WINDOW_SYSTEM
1096 ".GLSL", // GL_DEBUG_SOURCE_SHADER_COMPILER
1097 ".3rdParty", // GL_DEBUG_SOURCE_THIRD_PARTY
1098 "", // GL_DEBUG_SOURCE_APPLICATION
1099 ".Other" // GL_DEBUG_SOURCE_OTHER
1102 static Standard_CString THE_DBGMSG_TYPES[] =
1104 "Error", // GL_DEBUG_TYPE_ERROR
1105 "Deprecated", // GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR
1106 "Undef. behavior", // GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR
1107 "Portability", // GL_DEBUG_TYPE_PORTABILITY
1108 "Performance", // GL_DEBUG_TYPE_PERFORMANCE
1109 "Other" // GL_DEBUG_TYPE_OTHER
1112 static Standard_CString THE_DBGMSG_SEV_HIGH = "High"; // GL_DEBUG_SEVERITY_HIGH
1113 static Standard_CString THE_DBGMSG_SEV_MEDIUM = "Medium"; // GL_DEBUG_SEVERITY_MEDIUM
1114 static Standard_CString THE_DBGMSG_SEV_LOW = "Low"; // GL_DEBUG_SEVERITY_LOW
1116 //! Callback for GL_ARB_debug_output extension
1117 static void APIENTRY debugCallbackWrap(unsigned int theSource,
1118 unsigned int theType,
1120 unsigned int theSeverity,
1122 const char* theMessage,
1123 const void* theUserParam)
1125 OpenGl_Context* aCtx = (OpenGl_Context* )theUserParam;
1126 aCtx->PushMessage (theSource, theType, theId, theSeverity, theMessage);
1129 // =======================================================================
1130 // function : PushMessage
1132 // =======================================================================
1133 void OpenGl_Context::PushMessage (const unsigned int theSource,
1134 const unsigned int theType,
1135 const unsigned int theId,
1136 const unsigned int theSeverity,
1137 const TCollection_ExtendedString& theMessage)
1139 if (caps->suppressExtraMsg
1140 && theSource >= GL_DEBUG_SOURCE_API
1141 && theSource <= GL_DEBUG_SOURCE_OTHER
1142 && myFilters[theSource - GL_DEBUG_SOURCE_API].Contains (theId))
1147 Standard_CString& aSrc = (theSource >= GL_DEBUG_SOURCE_API
1148 && theSource <= GL_DEBUG_SOURCE_OTHER)
1149 ? THE_DBGMSG_SOURCES[theSource - GL_DEBUG_SOURCE_API]
1150 : THE_DBGMSG_UNKNOWN;
1151 Standard_CString& aType = (theType >= GL_DEBUG_TYPE_ERROR
1152 && theType <= GL_DEBUG_TYPE_OTHER)
1153 ? THE_DBGMSG_TYPES[theType - GL_DEBUG_TYPE_ERROR]
1154 : THE_DBGMSG_UNKNOWN;
1155 Standard_CString& aSev = theSeverity == GL_DEBUG_SEVERITY_HIGH
1156 ? THE_DBGMSG_SEV_HIGH
1157 : (theSeverity == GL_DEBUG_SEVERITY_MEDIUM
1158 ? THE_DBGMSG_SEV_MEDIUM
1159 : THE_DBGMSG_SEV_LOW);
1160 Message_Gravity aGrav = theSeverity == GL_DEBUG_SEVERITY_HIGH
1162 : (theSeverity == GL_DEBUG_SEVERITY_MEDIUM
1166 TCollection_ExtendedString aMsg;
1167 aMsg += "TKOpenGl"; aMsg += aSrc;
1168 aMsg += " | Type: "; aMsg += aType;
1169 aMsg += " | ID: "; aMsg += (Standard_Integer )theId;
1170 aMsg += " | Severity: "; aMsg += aSev;
1171 aMsg += " | Message:\n ";
1173 Messenger()->Send (aMsg, aGrav);
1176 // =======================================================================
1177 // function : ExcludeMessage
1179 // ======================================================================
1180 Standard_Boolean OpenGl_Context::ExcludeMessage (const unsigned int theSource,
1181 const unsigned int theId)
1183 return theSource >= GL_DEBUG_SOURCE_API
1184 && theSource <= GL_DEBUG_SOURCE_OTHER
1185 && myFilters[theSource - GL_DEBUG_SOURCE_API].Add (theId);
1188 // =======================================================================
1189 // function : IncludeMessage
1191 // ======================================================================
1192 Standard_Boolean OpenGl_Context::IncludeMessage (const unsigned int theSource,
1193 const unsigned int theId)
1195 return theSource >= GL_DEBUG_SOURCE_API
1196 && theSource <= GL_DEBUG_SOURCE_OTHER
1197 && myFilters[theSource - GL_DEBUG_SOURCE_API].Remove (theId);
1200 // =======================================================================
1201 // function : checkWrongVersion
1203 // ======================================================================
1204 void OpenGl_Context::checkWrongVersion (Standard_Integer theGlVerMajor, Standard_Integer theGlVerMinor,
1205 const char* theLastFailedProc)
1207 if (!IsGlGreaterEqual (theGlVerMajor, theGlVerMinor))
1212 PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH,
1213 TCollection_AsciiString()
1214 + "Error! OpenGL context reports version "
1215 + myGlVerMajor + "." + myGlVerMinor
1216 + " but does not export required functions for " + theGlVerMajor + "." + theGlVerMinor
1217 + " (" + (theLastFailedProc != NULL ? theLastFailedProc : "") + ")\n"
1218 + "Please report this issue to OpenGL driver vendor '" + myVendor + "'");
1220 // lower internal version
1221 if (theGlVerMinor > 0)
1223 myGlVerMajor = theGlVerMajor;
1224 myGlVerMinor = theGlVerMinor - 1;
1228 if (myGapi == Aspect_GraphicsLibrary_OpenGLES)
1230 switch (theGlVerMajor)
1232 case 3: myGlVerMajor = 2; myGlVerMinor = 0; return;
1237 switch (theGlVerMajor)
1239 case 2: myGlVerMajor = 1; myGlVerMinor = 5; return;
1240 case 3: myGlVerMajor = 2; myGlVerMinor = 1; return;
1241 case 4: myGlVerMajor = 3; myGlVerMinor = 3; return;
1246 // =======================================================================
1249 // =======================================================================
1250 void OpenGl_Context::init (const Standard_Boolean theIsCoreProfile)
1255 myHasMsaaTextures = false;
1256 myMaxMsaaSamples = 0;
1257 myMaxDrawBuffers = 1;
1258 myMaxColorAttachments = 1;
1260 OpenGl_GlFunctions::readGlVersion (myGlVerMajor, myGlVerMinor);
1261 mySupportedFormats->Clear();
1263 if (caps->contextMajorVersionUpper != -1)
1265 // synthetically restrict OpenGL version for testing
1266 Standard_Integer aCtxVer[2] = { myGlVerMajor, myGlVerMinor };
1267 bool isLowered = false;
1268 if (myGlVerMajor > caps->contextMajorVersionUpper)
1271 myGlVerMajor = caps->contextMajorVersionUpper;
1272 if (myGapi == Aspect_GraphicsLibrary_OpenGLES)
1274 switch (myGlVerMajor)
1276 case 2: myGlVerMinor = 0; break;
1281 switch (myGlVerMajor)
1283 case 1: myGlVerMinor = 5; break;
1284 case 2: myGlVerMinor = 1; break;
1285 case 3: myGlVerMinor = 3; break;
1289 if (caps->contextMinorVersionUpper != -1
1290 && myGlVerMinor > caps->contextMinorVersionUpper)
1293 myGlVerMinor = caps->contextMinorVersionUpper;
1297 PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_MEDIUM,
1298 TCollection_AsciiString ("OpenGL version ") + aCtxVer[0] + "." + aCtxVer[1]
1299 + " has been lowered to " + myGlVerMajor + "." + myGlVerMinor);
1303 myFuncs->load (*this, theIsCoreProfile);
1305 if (!caps->ffpEnable
1306 && !IsGlGreaterEqual (2, 0))
1308 caps->ffpEnable = true;
1309 TCollection_ExtendedString aMsg =
1310 TCollection_ExtendedString("OpenGL driver is too old! Context info:\n")
1311 + " Vendor: " + (const char* )core11fwd->glGetString (GL_VENDOR) + "\n"
1312 + " Renderer: " + (const char* )core11fwd->glGetString (GL_RENDERER) + "\n"
1313 + " Version: " + (const char* )core11fwd->glGetString (GL_VERSION) + "\n"
1314 + " Fallback using deprecated fixed-function pipeline.\n"
1315 + " Visualization might work incorrectly.\n"
1316 " Consider upgrading the graphics driver.";
1317 PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
1320 myVendor = (const char* )core11fwd->glGetString (GL_VENDOR);
1321 myVendor.LowerCase();
1322 if (myVendor.Search ("nvidia") != -1)
1324 // Buffer detailed info: Buffer object 1 (bound to GL_ARRAY_BUFFER_ARB, usage hint is GL_STATIC_DRAW)
1325 // will use VIDEO memory as the source for buffer object operations.
1326 ExcludeMessage (GL_DEBUG_SOURCE_API, 131185);
1329 // setup shader generator
1330 myShaderManager->SetGapiVersion (myGlVerMajor, myGlVerMinor);
1331 myShaderManager->SetEmulateDepthClamp (!arbDepthClamp);
1333 // workaround Adreno driver bug computing reversed normal using dFdx/dFdy
1334 bool toReverseDFdxSign = myGapi == Aspect_GraphicsLibrary_OpenGLES
1335 && myVendor.Search("qualcomm") != -1;
1336 myShaderManager->SetFlatShading (hasFlatShading != OpenGl_FeatureNotAvailable, toReverseDFdxSign);
1337 myShaderManager->SetUseRedAlpha (myGapi != Aspect_GraphicsLibrary_OpenGLES
1338 && core11ffp == NULL);
1339 #define checkGlslExtensionShort(theName) myShaderManager->EnableGlslExtension (Graphic3d_GlslExtension_ ## theName, CheckExtension (#theName))
1340 if (myGapi == Aspect_GraphicsLibrary_OpenGLES)
1342 checkGlslExtensionShort(GL_OES_standard_derivatives);
1343 checkGlslExtensionShort(GL_EXT_shader_texture_lod);
1344 checkGlslExtensionShort(GL_EXT_frag_depth);
1348 checkGlslExtensionShort(GL_EXT_gpu_shader4);
1351 // initialize debug context extension
1353 && caps->contextDebug)
1355 // setup default callback
1356 myIsGlDebugCtx = Standard_True;
1357 arbDbg->glDebugMessageCallback (debugCallbackWrap, this);
1358 if (myGapi == Aspect_GraphicsLibrary_OpenGLES)
1360 core11fwd->glEnable (GL_DEBUG_OUTPUT);
1362 else if (core43 != NULL)
1364 core11fwd->glEnable (GL_DEBUG_OUTPUT);
1366 if (caps->contextSyncDebug)
1368 // note that some broken implementations (e.g. simulators) might generate error message on this call
1369 core11fwd->glEnable (GL_DEBUG_OUTPUT_SYNCHRONOUS);
1375 core11fwd->glGetIntegerv (GL_MAX_DRAW_BUFFERS, &myMaxDrawBuffers);
1376 core11fwd->glGetIntegerv (GL_MAX_COLOR_ATTACHMENTS, &myMaxColorAttachments);
1377 if (myDrawBuffers.Length() < myMaxDrawBuffers)
1379 myDrawBuffers.Resize (0, myMaxDrawBuffers - 1, false);
1383 core11fwd->glGetIntegerv (GL_MAX_TEXTURE_SIZE, &myMaxTexDim);
1384 if (IsGlGreaterEqual (1, 3) && core11ffp != NULL)
1386 // this is a maximum of texture units for FFP functionality,
1387 // usually smaller than combined texture units available for GLSL
1388 core11fwd->glGetIntegerv (GL_MAX_TEXTURE_UNITS, &myMaxTexUnitsFFP);
1389 myMaxTexCombined = myMaxTexUnitsFFP;
1391 if (IsGlGreaterEqual (2, 0))
1393 core11fwd->glGetIntegerv (GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &myMaxTexCombined);
1395 mySpriteTexUnit = myMaxTexCombined >= 2
1396 ? Graphic3d_TextureUnit_PointSprite
1397 : Graphic3d_TextureUnit_0;
1399 GLint aMaxVPortSize[2] = {0, 0};
1400 core11fwd->glGetIntegerv (GL_MAX_VIEWPORT_DIMS, aMaxVPortSize);
1401 myMaxDumpSizeX = Min (aMaxVPortSize[0], myMaxTexDim);
1402 myMaxDumpSizeY = Min (aMaxVPortSize[1], myMaxTexDim);
1403 if (myVendor == "intel")
1405 // Intel drivers have known bug with empty dump for images with width>=5462
1406 myMaxDumpSizeX = Min (myMaxDumpSizeX, 4096);
1411 core11fwd->glGetIntegerv (GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &myAnisoMax);
1414 myClippingState.Init();
1416 if (myGapi == Aspect_GraphicsLibrary_OpenGLES)
1418 if (IsGlGreaterEqual (3, 0))
1420 // MSAA RenderBuffers have been defined in OpenGL ES 3.0, but MSAA Textures - only in OpenGL ES 3.1+
1421 myHasMsaaTextures = IsGlGreaterEqual (3, 1)
1422 && myFuncs->glTexStorage2DMultisample != NULL;
1423 core11fwd->glGetIntegerv (GL_MAX_SAMPLES, &myMaxMsaaSamples);
1426 else if (core30 != NULL)
1428 // MSAA RenderBuffers have been defined in OpenGL 3.0, but MSAA Textures - only in OpenGL 3.2+
1431 myHasMsaaTextures = true;
1432 core11fwd->glGetIntegerv (GL_MAX_SAMPLES, &myMaxMsaaSamples);
1434 else if (CheckExtension ("GL_ARB_texture_multisample")
1435 && myFuncs->glTexImage2DMultisample != NULL)
1437 myHasMsaaTextures = true;
1438 GLint aNbColorSamples = 0, aNbDepthSamples = 0;
1439 core11fwd->glGetIntegerv (GL_MAX_COLOR_TEXTURE_SAMPLES, &aNbColorSamples);
1440 core11fwd->glGetIntegerv (GL_MAX_DEPTH_TEXTURE_SAMPLES, &aNbDepthSamples);
1441 myMaxMsaaSamples = Min (aNbColorSamples, aNbDepthSamples);
1444 if (myMaxMsaaSamples <= 1)
1446 myHasMsaaTextures = false;
1449 if (myGapi != Aspect_GraphicsLibrary_OpenGLES)
1451 if (core32 != NULL && core11ffp == NULL)
1453 core32->glGenVertexArrays (1, &myDefaultVao);
1456 myTexClamp = IsGlGreaterEqual (1, 2) ? GL_CLAMP_TO_EDGE : GL_CLAMP;
1458 GLint aStereo = GL_FALSE;
1459 core11fwd->glGetIntegerv (GL_STEREO, &aStereo);
1460 myIsStereoBuffers = aStereo == 1;
1462 // get number of maximum clipping planes
1463 core11fwd->glGetIntegerv (GL_MAX_CLIP_PLANES, &myMaxClipPlanes);
1466 if (myGapi == Aspect_GraphicsLibrary_OpenGLES)
1468 // check whether ray tracing mode is supported
1469 myHasRayTracing = IsGlGreaterEqual (3, 2);
1470 myHasRayTracingTextures = myHasRayTracingAdaptiveSampling = myHasRayTracingAdaptiveSamplingAtomic = false;
1474 // check whether ray tracing mode is supported
1475 myHasRayTracing = IsGlGreaterEqual (3, 1)
1477 && arbFBOBlit != NULL;
1479 // check whether textures in ray tracing mode are supported
1480 myHasRayTracingTextures = myHasRayTracing
1481 && arbTexBindless != NULL;
1483 // check whether adaptive screen sampling in ray tracing mode is supported
1484 myHasRayTracingAdaptiveSampling = myHasRayTracing
1486 myHasRayTracingAdaptiveSamplingAtomic = myHasRayTracingAdaptiveSampling
1487 && CheckExtension ("GL_NV_shader_atomic_float");
1493 // Detect if window buffer is considered by OpenGL as sRGB-ready
1494 // (linear RGB color written by shader is automatically converted into sRGB)
1495 // or not (offscreen FBO should be blit into window buffer with gamma correction).
1496 const GLenum aDefWinBuffer = myGapi == Aspect_GraphicsLibrary_OpenGLES ? GL_BACK : GL_BACK_LEFT;
1497 GLint aWinColorEncoding = 0; // GL_LINEAR
1498 bool toSkipCheck = false;
1499 if (myGapi == Aspect_GraphicsLibrary_OpenGLES)
1501 toSkipCheck = !IsGlGreaterEqual (3, 0);
1505 arbFBO->glGetFramebufferAttachmentParameteriv (GL_FRAMEBUFFER, aDefWinBuffer, GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING, &aWinColorEncoding);
1508 myIsSRgbWindow = aWinColorEncoding == GL_SRGB;
1510 // On desktop OpenGL, pixel formats are almost always sRGB-ready, even when not requested;
1511 // it is safe behavior on desktop where GL_FRAMEBUFFER_SRGB is disabled by default
1512 // (contrary to OpenGL ES, where it is enabled by default).
1513 // NVIDIA drivers, however, always return GL_LINEAR even for sRGB-ready pixel formats on Windows platform,
1514 // while AMD and Intel report GL_SRGB as expected.
1515 // macOS drivers seems to be also report GL_LINEAR even for [NSColorSpace sRGBColorSpace].
1516 if (myGapi != Aspect_GraphicsLibrary_OpenGLES)
1519 myIsSRgbWindow = true;
1522 && myVendor.Search ("nvidia") != -1)
1524 myIsSRgbWindow = true;
1528 if (!myIsSRgbWindow)
1530 Message::SendTrace ("OpenGl_Context, warning: window buffer is not sRGB-ready.\n"
1531 "Check OpenGL window creation parameters for optimal performance.");
1535 Graphic3d_Vec4i aWinBitsRGBA;
1536 Graphic3d_Vec2i aWinBitsDepthStencil;
1537 WindowBufferBits (aWinBitsRGBA, aWinBitsDepthStencil);
1538 myIsWindowDeepColor = aWinBitsRGBA.r() >= 10;
1541 mySupportedFormats->Add (Image_Format_Gray);
1542 mySupportedFormats->Add (Image_Format_Alpha);
1543 mySupportedFormats->Add (Image_Format_RGB);
1544 mySupportedFormats->Add (Image_Format_RGB32);
1545 mySupportedFormats->Add (Image_Format_RGBA);
1548 if (myGapi != Aspect_GraphicsLibrary_OpenGLES)
1550 // no BGR on OpenGL ES - only BGRA as extension
1551 mySupportedFormats->Add (Image_Format_BGR);
1553 mySupportedFormats->Add (Image_Format_BGR32);
1554 mySupportedFormats->Add (Image_Format_BGRA);
1558 mySupportedFormats->Add (Image_Format_Gray16);
1562 mySupportedFormats->Add (Image_Format_GrayF);
1563 mySupportedFormats->Add (Image_Format_AlphaF);
1564 mySupportedFormats->Add (Image_Format_RGBF);
1565 mySupportedFormats->Add (Image_Format_RGBAF);
1566 if (hasHalfFloatBuffer != OpenGl_FeatureNotAvailable)
1568 mySupportedFormats->Add (Image_Format_RGBAF_half);
1572 mySupportedFormats->Add (Image_Format_RGF);
1573 if (hasHalfFloatBuffer != OpenGl_FeatureNotAvailable)
1575 mySupportedFormats->Add (Image_Format_GrayF_half);
1576 mySupportedFormats->Add (Image_Format_RGF_half);
1581 if (myGapi != Aspect_GraphicsLibrary_OpenGLES)
1583 mySupportedFormats->Add (Image_Format_BGRF);
1585 mySupportedFormats->Add (Image_Format_BGRAF);
1589 #ifdef __EMSCRIPTEN__
1590 if (CheckExtension ("GL_WEBGL_compressed_texture_s3tc")) // GL_WEBGL_compressed_texture_s3tc_srgb for sRGB formats
1592 mySupportedFormats->Add (Image_CompressedFormat_RGB_S3TC_DXT1);
1593 mySupportedFormats->Add (Image_CompressedFormat_RGBA_S3TC_DXT1);
1594 mySupportedFormats->Add (Image_CompressedFormat_RGBA_S3TC_DXT3);
1595 mySupportedFormats->Add (Image_CompressedFormat_RGBA_S3TC_DXT5);
1598 && CheckExtension ("GL_WEBGL_depth_texture"))
1600 extPDS = true; // WebGL 1.0 extension (in WebGL 2.0 core)
1603 if (CheckExtension ("GL_EXT_texture_compression_s3tc")) // GL_EXT_texture_sRGB for sRGB formats
1605 mySupportedFormats->Add (Image_CompressedFormat_RGB_S3TC_DXT1);
1606 mySupportedFormats->Add (Image_CompressedFormat_RGBA_S3TC_DXT1);
1607 mySupportedFormats->Add (Image_CompressedFormat_RGBA_S3TC_DXT3);
1608 mySupportedFormats->Add (Image_CompressedFormat_RGBA_S3TC_DXT5);
1612 if (CheckExtension ("GL_EXT_texture_compression_dxt1"))
1614 mySupportedFormats->Add (Image_CompressedFormat_RGB_S3TC_DXT1);
1615 mySupportedFormats->Add (Image_CompressedFormat_RGBA_S3TC_DXT1);
1617 if (CheckExtension ("GL_ANGLE_texture_compression_dxt3"))
1619 mySupportedFormats->Add (Image_CompressedFormat_RGBA_S3TC_DXT3);
1621 if (CheckExtension ("GL_ANGLE_texture_compression_dxt5"))
1623 mySupportedFormats->Add (Image_CompressedFormat_RGBA_S3TC_DXT5);
1628 // check whether PBR shading model is supported
1631 && myMaxTexCombined >= 4
1634 if (myGapi == Aspect_GraphicsLibrary_OpenGLES)
1636 myHasPBR = IsGlGreaterEqual (3, 0)
1638 // || CheckExtension ("GL_EXT_shader_texture_lod") fallback is used when extension is unavailable
1642 myHasPBR = IsGlGreaterEqual (3, 0)
1643 || (IsGlGreaterEqual (2, 1) && CheckExtension ("GL_EXT_gpu_shader4"));
1647 myDepthPeelingDepthTexUnit = static_cast<Graphic3d_TextureUnit>(myMaxTexCombined + Graphic3d_TextureUnit_DepthPeelingDepth); // -6
1648 myDepthPeelingFrontColorTexUnit = static_cast<Graphic3d_TextureUnit>(myMaxTexCombined + Graphic3d_TextureUnit_DepthPeelingFrontColor); // -5
1649 myShadowMapTexUnit = static_cast<Graphic3d_TextureUnit>(myMaxTexCombined + Graphic3d_TextureUnit_ShadowMap); // -4
1650 myPBREnvLUTTexUnit = static_cast<Graphic3d_TextureUnit>(myMaxTexCombined + Graphic3d_TextureUnit_PbrEnvironmentLUT); // -3
1651 myPBRDiffIBLMapSHTexUnit = static_cast<Graphic3d_TextureUnit>(myMaxTexCombined + Graphic3d_TextureUnit_PbrIblDiffuseSH); // -2
1652 myPBRSpecIBLMapTexUnit = static_cast<Graphic3d_TextureUnit>(myMaxTexCombined + Graphic3d_TextureUnit_PbrIblSpecular); // -1
1655 myDepthPeelingDepthTexUnit = static_cast<Graphic3d_TextureUnit>(myDepthPeelingDepthTexUnit + 3);
1656 myDepthPeelingFrontColorTexUnit = static_cast<Graphic3d_TextureUnit>(myDepthPeelingFrontColorTexUnit + 3);
1657 myShadowMapTexUnit = static_cast<Graphic3d_TextureUnit>(myShadowMapTexUnit + 3);
1661 // =======================================================================
1662 // function : MemoryInfo
1664 // =======================================================================
1665 Standard_Size OpenGl_Context::AvailableMemory() const
1669 // this is actually information for VBO pool
1670 // however because pools are mostly shared
1671 // it can be used for total GPU memory estimations
1675 core11fwd->glGetIntegerv (GL_VBO_FREE_MEMORY_ATI, aMemInfo);
1676 // returned value is in KiB, however this maybe changed in future
1677 return Standard_Size(aMemInfo[0]) * 1024;
1681 // current available dedicated video memory (in KiB), currently unused GPU memory
1683 core11fwd->glGetIntegerv (GL_GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX, &aMemInfo);
1684 return Standard_Size(aMemInfo) * 1024;
1689 // =======================================================================
1690 // function : MemoryInfo
1692 // =======================================================================
1693 TCollection_AsciiString OpenGl_Context::MemoryInfo() const
1695 TColStd_IndexedDataMapOfStringString aDict;
1698 TCollection_AsciiString aText;
1699 for (TColStd_IndexedDataMapOfStringString::Iterator anIter (aDict); anIter.More(); anIter.Next())
1701 if (!aText.IsEmpty())
1705 aText += TCollection_AsciiString(" ") + anIter.Key() + ": " + anIter.Value();
1710 // =======================================================================
1711 // function : MemoryInfo
1713 // =======================================================================
1714 void OpenGl_Context::MemoryInfo (TColStd_IndexedDataMapOfStringString& theDict) const
1716 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
1718 #elif defined(__APPLE__) && !defined(HAVE_XLIB)
1719 GLint aGlRendId = 0;
1720 CGLGetParameter (CGLGetCurrentContext(), kCGLCPCurrentRendererID, &aGlRendId);
1722 CGLRendererInfoObj aRendObj = NULL;
1723 CGOpenGLDisplayMask aDispMask = CGDisplayIDToOpenGLDisplayMask (kCGDirectMainDisplay);
1725 CGLQueryRendererInfo (aDispMask, &aRendObj, &aRendNb);
1726 for (GLint aRendIter = 0; aRendIter < aRendNb; ++aRendIter)
1729 if (CGLDescribeRenderer (aRendObj, aRendIter, kCGLRPRendererID, &aRendId) != kCGLNoError
1730 || aRendId != aGlRendId)
1735 //kCGLRPVideoMemoryMegabytes = 131;
1736 //kCGLRPTextureMemoryMegabytes = 132;
1738 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
1739 if (CGLDescribeRenderer(aRendObj, aRendIter, kCGLRPVideoMemoryMegabytes, &aVMem) == kCGLNoError)
1741 addInfo (theDict, "GPU memory", TCollection_AsciiString() + aVMem + " MiB");
1743 if (CGLDescribeRenderer(aRendObj, aRendIter, kCGLRPTextureMemoryMegabytes, &aVMem) == kCGLNoError)
1745 addInfo (theDict, "GPU Texture memory", TCollection_AsciiString() + aVMem + " MiB");
1748 if (CGLDescribeRenderer(aRendObj, aRendIter, kCGLRPVideoMemory, &aVMem) == kCGLNoError)
1750 addInfo (theDict, "GPU memory", TCollection_AsciiString() + (aVMem / (1024 * 1024)) + " MiB");
1752 if (CGLDescribeRenderer(aRendObj, aRendIter, kCGLRPTextureMemory, &aVMem) == kCGLNoError)
1754 addInfo (theDict, "GPU Texture memory", TCollection_AsciiString() + (aVMem / (1024 * 1024)) + " MiB");
1763 memset (aValues, 0, sizeof(aValues));
1764 core11fwd->glGetIntegerv (GL_VBO_FREE_MEMORY_ATI, aValues);
1766 // total memory free in the pool
1767 addInfo (theDict, "GPU free memory", TCollection_AsciiString() + (aValues[0] / 1024) + " MiB");
1769 if (aValues[1] != aValues[0])
1771 // largest available free block in the pool
1772 addInfo (theDict, "Largest free block", TCollection_AsciiString() + (aValues[1] / 1024) + " MiB");
1774 if (aValues[2] != aValues[0])
1776 // total auxiliary memory free
1777 addInfo (theDict, "Free auxiliary memory", TCollection_AsciiString() + (aValues[2] / 1024) + " MiB");
1782 //current available dedicated video memory (in KiB), currently unused GPU memory
1784 core11fwd->glGetIntegerv (GL_GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX, &aValue);
1785 addInfo (theDict, "GPU free memory", TCollection_AsciiString() + (aValue / 1024) + " MiB");
1787 // dedicated video memory, total size (in KiB) of the GPU memory
1788 GLint aDedicated = 0;
1789 core11fwd->glGetIntegerv (GL_GPU_MEMORY_INFO_DEDICATED_VIDMEM_NVX, &aDedicated);
1790 addInfo (theDict, "GPU memory", TCollection_AsciiString() + (aDedicated / 1024) + " MiB");
1792 // total available memory, total size (in KiB) of the memory available for allocations
1793 core11fwd->glGetIntegerv (GL_GPU_MEMORY_INFO_TOTAL_AVAILABLE_MEMORY_NVX, &aValue);
1794 if (aValue != aDedicated)
1796 // different only for special configurations
1797 addInfo (theDict, "Total memory", TCollection_AsciiString() + (aValue / 1024) + " MiB");
1801 else if (myFuncs->wglGetGPUInfoAMD != NULL
1802 && myFuncs->wglGetContextGPUIDAMD != NULL)
1804 GLuint aTotalMemMiB = 0;
1805 UINT anAmdId = myFuncs->wglGetContextGPUIDAMD ((HGLRC )myGContext);
1808 if (myFuncs->wglGetGPUInfoAMD (anAmdId, WGL_GPU_RAM_AMD, GL_UNSIGNED_INT, sizeof(aTotalMemMiB), &aTotalMemMiB) > 0)
1810 addInfo (theDict, "GPU memory", TCollection_AsciiString() + (int )aTotalMemMiB + " MiB");
1816 #if defined(HAVE_XLIB) && !defined(__APPLE__) && !defined(_WIN32)
1817 // GLX_RENDERER_VENDOR_ID_MESA
1818 if (myFuncs->glXQueryCurrentRendererIntegerMESA != NULL)
1820 unsigned int aVMemMiB = 0;
1821 if (myFuncs->glXQueryCurrentRendererIntegerMESA (GLX_RENDERER_VIDEO_MEMORY_MESA, &aVMemMiB) != 0)
1823 addInfo (theDict, "GPU memory", TCollection_AsciiString() + int(aVMemMiB) + " MiB");
1829 // =======================================================================
1830 // function : WindowBufferBits
1832 // =======================================================================
1833 void OpenGl_Context::WindowBufferBits (Graphic3d_Vec4i& theColorBits,
1834 Graphic3d_Vec2i& theDepthStencilBits) const
1836 if (core11ffp != NULL
1837 || myGapi == Aspect_GraphicsLibrary_OpenGLES)
1839 // removed from core with no working alternative
1840 core11fwd->glGetIntegerv (GL_RED_BITS, &theColorBits.r());
1841 core11fwd->glGetIntegerv (GL_GREEN_BITS, &theColorBits.g());
1842 core11fwd->glGetIntegerv (GL_BLUE_BITS, &theColorBits.b());
1843 core11fwd->glGetIntegerv (GL_ALPHA_BITS, &theColorBits.a());
1844 core11fwd->glGetIntegerv (GL_DEPTH_BITS, &theDepthStencilBits[0]);
1845 core11fwd->glGetIntegerv (GL_STENCIL_BITS, &theDepthStencilBits[1]);
1849 #if defined(HAVE_EGL)
1851 #elif defined(_WIN32)
1852 const int aPixFrmtIndex = GetPixelFormat ((HDC )myDisplay);
1853 PIXELFORMATDESCRIPTOR aFormat;
1854 memset (&aFormat, 0, sizeof(aFormat));
1855 aFormat.nSize = sizeof(aFormat);
1856 DescribePixelFormat ((HDC )myDisplay, aPixFrmtIndex, sizeof(PIXELFORMATDESCRIPTOR), &aFormat);
1857 theColorBits.SetValues (aFormat.cRedBits, aFormat.cGreenBits, aFormat.cBlueBits, aFormat.cAlphaBits);
1858 theDepthStencilBits.SetValues (aFormat.cDepthBits, aFormat.cStencilBits);
1859 #elif defined(HAVE_XLIB)
1860 Display* aDisplay = (Display* )myDisplay;
1861 XWindowAttributes aWinAttribs;
1862 XGetWindowAttributes (aDisplay, (::Window )myWindow, &aWinAttribs);
1863 XVisualInfo aVisInfo;
1864 aVisInfo.visualid = aWinAttribs.visual->visualid;
1865 aVisInfo.screen = DefaultScreen(aDisplay);
1867 std::unique_ptr<XVisualInfo, int(*)(void*)> aVis (XGetVisualInfo (aDisplay, VisualIDMask | VisualScreenMask, &aVisInfo, &aNbItems), &XFree);
1868 if (aVis.get() != NULL)
1870 glXGetConfig (aDisplay, aVis.get(), GLX_RED_SIZE, &theColorBits.r());
1871 glXGetConfig (aDisplay, aVis.get(), GLX_GREEN_SIZE, &theColorBits.g());
1872 glXGetConfig (aDisplay, aVis.get(), GLX_BLUE_SIZE, &theColorBits.b());
1873 glXGetConfig (aDisplay, aVis.get(), GLX_ALPHA_SIZE, &theColorBits.a());
1874 glXGetConfig (aDisplay, aVis.get(), GLX_DEPTH_SIZE, &theDepthStencilBits[0]);
1875 glXGetConfig (aDisplay, aVis.get(), GLX_STENCIL_SIZE, &theDepthStencilBits[1]);
1881 // =======================================================================
1882 // function : DiagnosticInfo
1884 // =======================================================================
1885 void OpenGl_Context::DiagnosticInformation (TColStd_IndexedDataMapOfStringString& theDict,
1886 Graphic3d_DiagnosticInfo theFlags) const
1888 if ((theFlags & Graphic3d_DiagnosticInfo_NativePlatform) != 0)
1890 #if defined(HAVE_EGL)
1891 addInfo (theDict, "EGLVersion", ::eglQueryString ((EGLDisplay )myDisplay, EGL_VERSION));
1892 addInfo (theDict, "EGLVendor", ::eglQueryString ((EGLDisplay )myDisplay, EGL_VENDOR));
1893 addInfo (theDict, "EGLClientAPIs", ::eglQueryString ((EGLDisplay )myDisplay, EGL_CLIENT_APIS));
1894 if ((theFlags & Graphic3d_DiagnosticInfo_Extensions) != 0)
1896 addInfo (theDict, "EGLExtensions", ::eglQueryString ((EGLDisplay )myDisplay, EGL_EXTENSIONS));
1898 #elif defined(_WIN32)
1899 if ((theFlags & Graphic3d_DiagnosticInfo_Extensions) != 0
1900 && myFuncs->wglGetExtensionsStringARB != NULL)
1902 const char* aWglExts = myFuncs->wglGetExtensionsStringARB ((HDC )myDisplay);
1903 addInfo (theDict, "WGLExtensions", aWglExts);
1905 #elif defined(HAVE_XLIB)
1906 Display* aDisplay = (Display*)myDisplay;
1907 const int aScreen = DefaultScreen(aDisplay);
1908 addInfo (theDict, "GLXDirectRendering", ::glXIsDirect (aDisplay, (GLXContext )myGContext) ? "Yes" : "No");
1909 addInfo (theDict, "GLXVendor", ::glXQueryServerString (aDisplay, aScreen, GLX_VENDOR));
1910 addInfo (theDict, "GLXVersion", ::glXQueryServerString (aDisplay, aScreen, GLX_VERSION));
1911 if ((theFlags & Graphic3d_DiagnosticInfo_Extensions) != 0)
1913 const char* aGlxExts = ::glXQueryExtensionsString (aDisplay, aScreen);
1914 addInfo(theDict, "GLXExtensions", aGlxExts);
1917 addInfo (theDict, "GLXClientVendor", ::glXGetClientString (aDisplay, GLX_VENDOR));
1918 addInfo (theDict, "GLXClientVersion", ::glXGetClientString (aDisplay, GLX_VERSION));
1919 if ((theFlags & Graphic3d_DiagnosticInfo_Extensions) != 0)
1921 addInfo (theDict, "GLXClientExtensions", ::glXGetClientString (aDisplay, GLX_EXTENSIONS));
1928 if ((theFlags & Graphic3d_DiagnosticInfo_Device) != 0)
1930 Standard_Integer aDriverVer[2] = {};
1931 OpenGl_GlFunctions::readGlVersion (aDriverVer[0], aDriverVer[1]);
1932 addInfo (theDict, "GLvendor", (const char*)core11fwd->glGetString (GL_VENDOR));
1933 addInfo (theDict, "GLdevice", (const char*)core11fwd->glGetString (GL_RENDERER));
1934 #ifdef __EMSCRIPTEN__
1935 if (CheckExtension ("GL_WEBGL_debug_renderer_info"))
1937 if (const char* aVendor = (const char*)core11fwd->glGetString (0x9245))
1939 addInfo (theDict, "GLunmaskedVendor", aVendor);
1941 if (const char* aDevice = (const char*)core11fwd->glGetString (0x9246))
1943 addInfo (theDict, "GLunmaskedDevice", aDevice);
1948 addInfo (theDict, "GLversion", (const char*)core11fwd->glGetString (GL_VERSION));
1949 if (myGlVerMajor != aDriverVer[0]
1950 || myGlVerMinor != aDriverVer[1])
1952 addInfo (theDict, "GLversionOcct", TCollection_AsciiString (myGlVerMajor) + "." + TCollection_AsciiString (myGlVerMinor));
1954 if (IsGlGreaterEqual (2, 0))
1956 addInfo (theDict, "GLSLversion", (const char*)core11fwd->glGetString (GL_SHADING_LANGUAGE_VERSION));
1960 addInfo (theDict, "GLdebug", "ON");
1964 if ((theFlags & Graphic3d_DiagnosticInfo_Limits) != 0)
1966 addInfo (theDict, "Max texture size", TCollection_AsciiString(myMaxTexDim));
1967 addInfo (theDict, "Max FBO dump size", TCollection_AsciiString() + myMaxDumpSizeX + "x" + myMaxDumpSizeY);
1968 addInfo (theDict, "Max combined texture units", TCollection_AsciiString(myMaxTexCombined));
1969 addInfo (theDict, "Max MSAA samples", TCollection_AsciiString(myMaxMsaaSamples));
1972 if ((theFlags & Graphic3d_DiagnosticInfo_FrameBuffer) != 0)
1974 GLint aViewport[4] = {};
1975 core11fwd->glGetIntegerv (GL_VIEWPORT, aViewport);
1976 addInfo (theDict, "Viewport", TCollection_AsciiString() + aViewport[2] + "x" + aViewport[3]);
1978 Graphic3d_Vec4i aWinBitsRGBA;
1979 Graphic3d_Vec2i aWinBitsDepthStencil;
1980 WindowBufferBits (aWinBitsRGBA, aWinBitsDepthStencil);
1981 addInfo (theDict, "Window buffer",
1982 TCollection_AsciiString() + "RGB" + aWinBitsRGBA.r() + " ALPHA" + aWinBitsRGBA.a()
1983 + " DEPTH" + aWinBitsDepthStencil[0] + " STENCIL" + aWinBitsDepthStencil[1]);
1986 if ((theFlags & Graphic3d_DiagnosticInfo_Memory) != 0)
1988 MemoryInfo (theDict);
1991 if ((theFlags & Graphic3d_DiagnosticInfo_Extensions) != 0)
1993 if (myGapi != Aspect_GraphicsLibrary_OpenGLES
1994 && IsGlGreaterEqual (3, 0)
1995 && myFuncs->glGetStringi != NULL)
1997 TCollection_AsciiString anExtList;
1999 core11fwd->glGetIntegerv (GL_NUM_EXTENSIONS, &anExtNb);
2000 for (GLint anIter = 0; anIter < anExtNb; ++anIter)
2002 const char* anExtension = (const char*)myFuncs->glGetStringi (GL_EXTENSIONS, (GLuint)anIter);
2003 if (!anExtList.IsEmpty())
2007 anExtList += anExtension;
2009 addInfo(theDict, "GLextensions", anExtList);
2013 addInfo (theDict, "GLextensions", (const char*)core11fwd->glGetString (GL_EXTENSIONS));
2018 // =======================================================================
2019 // function : GetResource
2021 // =======================================================================
2022 const Handle(OpenGl_Resource)& OpenGl_Context::GetResource (const TCollection_AsciiString& theKey) const
2024 return mySharedResources->IsBound (theKey) ? mySharedResources->Find (theKey) : NULL_GL_RESOURCE;
2027 // =======================================================================
2028 // function : ShareResource
2030 // =======================================================================
2031 Standard_Boolean OpenGl_Context::ShareResource (const TCollection_AsciiString& theKey,
2032 const Handle(OpenGl_Resource)& theResource)
2034 if (theKey.IsEmpty() || theResource.IsNull())
2036 return Standard_False;
2038 return mySharedResources->Bind (theKey, theResource);
2041 // =======================================================================
2042 // function : ReleaseResource
2044 // =======================================================================
2045 void OpenGl_Context::ReleaseResource (const TCollection_AsciiString& theKey,
2046 const Standard_Boolean theToDelay)
2048 if (!mySharedResources->IsBound (theKey))
2052 const Handle(OpenGl_Resource)& aRes = mySharedResources->Find (theKey);
2053 if (aRes->GetRefCount() > 1)
2060 myDelayed->Bind (theKey, 1);
2064 aRes->Release (this);
2065 mySharedResources->UnBind (theKey);
2069 // =======================================================================
2070 // function : ReleaseDelayed
2072 // =======================================================================
2073 void OpenGl_Context::ReleaseDelayed()
2075 // release queued elements
2076 while (!myUnusedResources->IsEmpty())
2078 myUnusedResources->First()->Release (this);
2079 myUnusedResources->RemoveFirst();
2082 // release delayed shared resources
2083 NCollection_Vector<TCollection_AsciiString> aDeadList;
2084 for (NCollection_DataMap<TCollection_AsciiString, Standard_Integer>::Iterator anIter (*myDelayed);
2085 anIter.More(); anIter.Next())
2087 if (++anIter.ChangeValue() <= 2)
2089 continue; // postpone release one more frame to ensure no one uses it periodically
2092 const TCollection_AsciiString& aKey = anIter.Key();
2093 if (!mySharedResources->IsBound (aKey))
2095 // mixed unshared strategy delayed/undelayed was used!
2096 aDeadList.Append (aKey);
2100 const Handle(OpenGl_Resource)& aRes = mySharedResources->ChangeFind (aKey);
2101 if (aRes->GetRefCount() > 1)
2103 // should be only 1 instance in mySharedResources
2104 // if not - resource was reused again
2105 aDeadList.Append (aKey);
2109 // release resource if no one requested it more than 2 redraw calls
2110 aRes->Release (this);
2111 mySharedResources->UnBind (aKey);
2112 aDeadList.Append (aKey);
2115 for (Standard_Integer anIter = 0; anIter < aDeadList.Length(); ++anIter)
2117 myDelayed->UnBind (aDeadList.Value (anIter));
2121 // =======================================================================
2122 // function : BindTextures
2124 // =======================================================================
2125 Handle(OpenGl_TextureSet) OpenGl_Context::BindTextures (const Handle(OpenGl_TextureSet)& theTextures,
2126 const Handle(OpenGl_ShaderProgram)& theProgram)
2128 const Standard_Integer aTextureSetBits = !theTextures.IsNull() ? theTextures->TextureSetBits() : 0;
2129 const Standard_Integer aProgramBits = !theProgram.IsNull() ? theProgram->TextureSetBits() : 0;
2130 Standard_Integer aMissingBits = aProgramBits & ~aTextureSetBits;
2131 if (aMissingBits != 0
2132 && myTextureRgbaBlack.IsNull())
2134 // allocate mock textures
2135 myTextureRgbaBlack = new OpenGl_Texture();
2136 myTextureRgbaWhite = new OpenGl_Texture();
2137 Image_PixMap anImage;
2138 anImage.InitZero (Image_Format_RGBA, 2, 2, 0, (Standard_Byte )0);
2139 if (!myTextureRgbaBlack->Init (this, OpenGl_TextureFormat::Create<GLubyte, 4>(), Graphic3d_Vec2i (2, 2), Graphic3d_TypeOfTexture_2D, &anImage))
2141 PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_HIGH,
2142 "Error: unable to create unit mock PBR texture map.");
2144 anImage.InitZero (Image_Format_RGBA, 2, 2, 0, (Standard_Byte )255);
2145 if (!myTextureRgbaWhite->Init (this, OpenGl_TextureFormat::Create<GLubyte, 4>(), Graphic3d_Vec2i (2, 2), Graphic3d_TypeOfTexture_2D, &anImage))
2147 PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_HIGH,
2148 "Error: unable to create normal mock PBR texture map.");
2152 Handle(OpenGl_TextureSet) anOldTextures = myActiveTextures;
2153 if (myActiveTextures != theTextures)
2155 Handle(OpenGl_Context) aThisCtx (this);
2156 for (OpenGl_TextureSetPairIterator aSlotIter (myActiveTextures, theTextures); aSlotIter.More(); aSlotIter.Next())
2158 const Graphic3d_TextureUnit aTexUnit = aSlotIter.Unit();
2159 const OpenGl_Texture* aTextureOld = aSlotIter.Texture1();
2160 const OpenGl_Texture* aTextureNew = aSlotIter.Texture2();
2161 if (aTextureNew == aTextureOld)
2166 if (aTextureNew != NULL
2167 && aTextureNew->IsValid())
2169 if (aTexUnit >= myMaxTexCombined)
2171 PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH,
2172 TCollection_AsciiString("Texture unit ") + aTexUnit + " for " + aTextureNew->ResourceId() + " exceeds hardware limit " + myMaxTexCombined);
2176 aTextureNew->Bind (aThisCtx, aTexUnit);
2177 if (aTextureNew->Sampler()->ToUpdateParameters())
2179 if (aTextureNew->Sampler()->IsImmutable())
2181 aTextureNew->Sampler()->Init (aThisCtx, *aTextureNew);
2185 OpenGl_Sampler::applySamplerParams (aThisCtx, aTextureNew->Sampler()->Parameters(), aTextureNew->Sampler().get(), aTextureNew->GetTarget(), aTextureNew->MaxMipmapLevel());
2188 if (core11ffp != NULL)
2190 OpenGl_Sampler::applyGlobalTextureParams (aThisCtx, *aTextureNew, aTextureNew->Sampler()->Parameters());
2193 else if (aTextureOld != NULL
2194 && aTextureOld->IsValid())
2196 aTextureOld->Unbind (aThisCtx, aTexUnit);
2197 if (core11ffp != NULL)
2199 OpenGl_Sampler::resetGlobalTextureParams (aThisCtx, *aTextureOld, aTextureOld->Sampler()->Parameters());
2203 myActiveTextures = theTextures;
2206 if (myActiveMockTextures != aMissingBits)
2208 myActiveMockTextures = aMissingBits;
2209 for (Standard_Integer aBitIter = 0; aMissingBits != 0; ++aBitIter)
2211 Standard_Integer aUnitMask = 1 << aBitIter;
2212 if ((aUnitMask & aMissingBits) != 0)
2214 aMissingBits = aMissingBits & ~aUnitMask;
2215 if (aBitIter == Graphic3d_TextureUnit_Normal)
2217 myTextureRgbaBlack->Bind (this, static_cast<Graphic3d_TextureUnit>(aBitIter));
2221 myTextureRgbaWhite->Bind (this, static_cast<Graphic3d_TextureUnit>(aBitIter));
2227 return anOldTextures;
2230 // =======================================================================
2231 // function : BindProgram
2233 // =======================================================================
2234 Standard_Boolean OpenGl_Context::BindProgram (const Handle(OpenGl_ShaderProgram)& theProgram)
2236 if (core20fwd == NULL)
2238 return Standard_False;
2240 else if (myActiveProgram == theProgram)
2242 return Standard_True;
2245 if (theProgram.IsNull()
2246 || !theProgram->IsValid())
2248 if (!myActiveProgram.IsNull())
2250 core20fwd->glUseProgram (OpenGl_ShaderProgram::NO_PROGRAM);
2251 myActiveProgram.Nullify();
2253 return Standard_False;
2256 myActiveProgram = theProgram;
2257 core20fwd->glUseProgram (theProgram->ProgramId());
2258 return Standard_True;
2261 // =======================================================================
2262 // function : BindDefaultVao
2264 // =======================================================================
2265 void OpenGl_Context::BindDefaultVao()
2267 if (myDefaultVao == 0
2273 core32->glBindVertexArray (myDefaultVao);
2276 // =======================================================================
2277 // function : SetDefaultFrameBuffer
2279 // =======================================================================
2280 Handle(OpenGl_FrameBuffer) OpenGl_Context::SetDefaultFrameBuffer (const Handle(OpenGl_FrameBuffer)& theFbo)
2282 Handle(OpenGl_FrameBuffer) aFbo = myDefaultFbo;
2283 myDefaultFbo = theFbo;
2287 // =======================================================================
2288 // function : IsRender
2290 // =======================================================================
2291 Standard_Boolean OpenGl_Context::IsRender() const
2293 return myRenderMode == GL_RENDER;
2296 // =======================================================================
2297 // function : IsFeedback
2299 // =======================================================================
2300 Standard_Boolean OpenGl_Context::IsFeedback() const
2302 return myRenderMode == GL_FEEDBACK;
2305 // =======================================================================
2306 // function : SetShadingMaterial
2308 // =======================================================================
2309 void OpenGl_Context::SetShadingMaterial (const OpenGl_Aspects* theAspect,
2310 const Handle(Graphic3d_PresentationAttributes)& theHighlight)
2312 const Handle(Graphic3d_Aspects)& anAspect = (!theHighlight.IsNull() && !theHighlight->BasicFillAreaAspect().IsNull())
2313 ? (const Handle(Graphic3d_Aspects)& )theHighlight->BasicFillAreaAspect()
2314 : theAspect->Aspect();
2316 const bool toDistinguish = anAspect->Distinguish();
2317 const bool toMapTexture = anAspect->ToMapTexture();
2318 const Graphic3d_MaterialAspect& aMatFrontSrc = anAspect->FrontMaterial();
2319 const Graphic3d_MaterialAspect& aMatBackSrc = toDistinguish
2320 ? anAspect->BackMaterial()
2322 const Quantity_Color& aFrontIntColor = anAspect->InteriorColor();
2323 const Quantity_Color& aBackIntColor = toDistinguish
2324 ? anAspect->BackInteriorColor()
2327 myMaterial.Init (*this, aMatFrontSrc, aFrontIntColor, aMatBackSrc, aBackIntColor);
2328 if (!theHighlight.IsNull()
2329 && theHighlight->BasicFillAreaAspect().IsNull())
2331 myMaterial.SetColor (theHighlight->ColorRGBA().GetRGB());
2332 myMaterial.SetColor (theHighlight->ColorRGBA().GetRGB());
2335 float anAlphaFront = 1.0f, anAlphaBack = 1.0f;
2336 if (CheckIsTransparent (theAspect, theHighlight, anAlphaFront, anAlphaBack))
2338 myMaterial.Common[0].Diffuse.a() = anAlphaFront;
2339 myMaterial.Common[1].Diffuse.a() = anAlphaBack;
2341 myMaterial.Pbr[0].BaseColor.a() = anAlphaFront;
2342 myMaterial.Pbr[1].BaseColor.a() = anAlphaBack;
2345 // do not update material properties in case of zero reflection mode,
2346 // because GL lighting will be disabled by OpenGl_PrimitiveArray::DrawArray() anyway.
2347 const OpenGl_MaterialState& aMatState = myShaderManager->MaterialState();
2348 float anAlphaCutoff = (anAspect->AlphaMode() == Graphic3d_AlphaMode_Mask
2349 || anAspect->AlphaMode() == Graphic3d_AlphaMode_MaskBlend)
2350 ? anAspect->AlphaCutoff()
2352 if (anAspect->ToDrawEdges())
2354 if (anAspect->InteriorStyle() == Aspect_IS_EMPTY
2355 || (anAspect->InteriorStyle() == Aspect_IS_SOLID
2356 && anAspect->EdgeColorRGBA().Alpha() < 1.0f))
2358 anAlphaCutoff = 0.285f;
2361 if (theAspect->ShadingModel() == Graphic3d_TypeOfShadingModel_Unlit)
2363 if (anAlphaCutoff == aMatState.AlphaCutoff())
2368 else if (myMaterial.IsEqual (aMatState.Material())
2369 && toDistinguish == aMatState.ToDistinguish()
2370 && toMapTexture == aMatState.ToMapTexture()
2371 && anAlphaCutoff == aMatState.AlphaCutoff())
2376 myShaderManager->UpdateMaterialStateTo (myMaterial, anAlphaCutoff, toDistinguish, toMapTexture);
2379 // =======================================================================
2380 // function : CheckIsTransparent
2382 // =======================================================================
2383 Standard_Boolean OpenGl_Context::CheckIsTransparent (const OpenGl_Aspects* theAspect,
2384 const Handle(Graphic3d_PresentationAttributes)& theHighlight,
2385 Standard_ShortReal& theAlphaFront,
2386 Standard_ShortReal& theAlphaBack)
2388 const Handle(Graphic3d_Aspects)& anAspect = (!theHighlight.IsNull() && !theHighlight->BasicFillAreaAspect().IsNull())
2389 ? (const Handle(Graphic3d_Aspects)& )theHighlight->BasicFillAreaAspect()
2390 : theAspect->Aspect();
2392 const bool toDistinguish = anAspect->Distinguish();
2393 const Graphic3d_MaterialAspect& aMatFrontSrc = anAspect->FrontMaterial();
2394 const Graphic3d_MaterialAspect& aMatBackSrc = toDistinguish
2395 ? anAspect->BackMaterial()
2398 // handling transparency
2399 if (!theHighlight.IsNull()
2400 && theHighlight->BasicFillAreaAspect().IsNull())
2402 theAlphaFront = theHighlight->ColorRGBA().Alpha();
2403 theAlphaBack = theHighlight->ColorRGBA().Alpha();
2407 theAlphaFront = aMatFrontSrc.Alpha();
2408 theAlphaBack = aMatBackSrc .Alpha();
2411 if (anAspect->AlphaMode() == Graphic3d_AlphaMode_BlendAuto)
2413 return theAlphaFront < 1.0f
2414 || theAlphaBack < 1.0f;
2416 // Graphic3d_AlphaMode_Mask and Graphic3d_AlphaMode_MaskBlend are not considered transparent here
2417 return anAspect->AlphaMode() == Graphic3d_AlphaMode_Blend;
2420 // =======================================================================
2421 // function : SetColor4fv
2423 // =======================================================================
2424 void OpenGl_Context::SetColor4fv (const OpenGl_Vec4& theColor)
2426 if (!myActiveProgram.IsNull())
2428 if (const OpenGl_ShaderUniformLocation& aLoc = myActiveProgram->GetStateLocation (OpenGl_OCCT_COLOR))
2430 myActiveProgram->SetUniform (this, aLoc, Vec4FromQuantityColor (theColor));
2433 else if (core11ffp != NULL)
2435 core11ffp->glColor4fv (theColor.GetData());
2439 // =======================================================================
2440 // function : SetTypeOfLine
2442 // =======================================================================
2443 void OpenGl_Context::SetTypeOfLine (const Aspect_TypeOfLine theType,
2444 const Standard_ShortReal theFactor)
2446 SetLineStipple (theFactor, Graphic3d_Aspects::DefaultLinePatternForType (theType));
2449 // =======================================================================
2450 // function : SetLineStipple
2452 // =======================================================================
2453 void OpenGl_Context::SetLineStipple (const Standard_ShortReal theFactor,
2454 const uint16_t thePattern)
2456 if (!myActiveProgram.IsNull())
2458 if (const OpenGl_ShaderUniformLocation aPatternLoc = myActiveProgram->GetStateLocation (OpenGl_OCCT_LINE_STIPPLE_PATTERN))
2460 if (hasGlslBitwiseOps != OpenGl_FeatureNotAvailable)
2462 myActiveProgram->SetUniform (this, aPatternLoc, (Standard_Integer )thePattern);
2466 Standard_Integer aPatArr[16] = {};
2467 for (unsigned int aBit = 0; aBit < 16; ++aBit)
2469 aPatArr[aBit] = ((unsigned int)(thePattern) & (1U << aBit)) != 0 ? 1 : 0;
2471 myActiveProgram->SetUniform (this, aPatternLoc, 16, aPatArr);
2473 myActiveProgram->SetUniform (this, myActiveProgram->GetStateLocation (OpenGl_OCCT_LINE_STIPPLE_FACTOR), theFactor);
2478 if (core11ffp != NULL)
2480 if (thePattern != 0xFFFF)
2482 core11fwd->glEnable (GL_LINE_STIPPLE);
2483 core11ffp->glLineStipple (static_cast<GLint> (theFactor),
2484 static_cast<GLushort> (thePattern));
2488 core11fwd->glDisable (GL_LINE_STIPPLE);
2493 // =======================================================================
2494 // function : SetLineWidth
2496 // =======================================================================
2497 void OpenGl_Context::SetLineWidth (const Standard_ShortReal theWidth)
2499 if (myGapi == Aspect_GraphicsLibrary_OpenGLES
2500 || core11ffp != NULL)
2502 // glLineWidth() is still defined within Core Profile, but has no effect with values != 1.0f
2503 core11fwd->glLineWidth (theWidth * myLineWidthScale);
2507 // =======================================================================
2508 // function : SetTextureMatrix
2510 // =======================================================================
2511 void OpenGl_Context::SetTextureMatrix (const Handle(Graphic3d_TextureParams)& theParams,
2512 const Standard_Boolean theIsTopDown)
2514 if (theParams.IsNull())
2519 const Graphic3d_Vec2& aScale = theParams->Scale();
2520 const Graphic3d_Vec2& aTrans = theParams->Translation();
2521 if (!myActiveProgram.IsNull())
2523 const GLint aUniLoc = myActiveProgram->GetStateLocation (OpenGl_OCCT_TEXTURE_TRSF2D);
2524 if (aUniLoc == OpenGl_ShaderProgram::INVALID_LOCATION)
2529 // pack transformation parameters
2530 OpenGl_Vec4 aTrsf[2] =
2532 OpenGl_Vec4 (-aTrans.x(), -aTrans.y(), aScale.x(), aScale.y()),
2533 OpenGl_Vec4 (static_cast<float> (std::sin (-theParams->Rotation() * M_PI / 180.0)),
2534 static_cast<float> (std::cos (-theParams->Rotation() * M_PI / 180.0)),
2537 if (caps->isTopDownTextureUV != theIsTopDown)
2540 aTrsf[0].y() = -aTrans.y() + 1.0f / aScale.y();
2541 aTrsf[0].w() = -aScale.y();
2543 myActiveProgram->SetUniform (this, aUniLoc, 2, aTrsf);
2547 if (core11ffp != NULL)
2549 GLint aMatrixMode = GL_TEXTURE;
2550 core11fwd->glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode);
2552 core11ffp->glMatrixMode (GL_TEXTURE);
2553 OpenGl_Mat4 aTextureMat;
2554 if (caps->isTopDownTextureUV != theIsTopDown)
2557 Graphic3d_TransformUtils::Scale (aTextureMat, aScale.x(), -aScale.y(), 1.0f);
2558 Graphic3d_TransformUtils::Translate (aTextureMat, -aTrans.x(), -aTrans.y() + 1.0f / aScale.y(), 0.0f);
2562 Graphic3d_TransformUtils::Scale (aTextureMat, aScale.x(), aScale.y(), 1.0f);
2563 Graphic3d_TransformUtils::Translate (aTextureMat, -aTrans.x(), -aTrans.y(), 0.0f);
2565 Graphic3d_TransformUtils::Rotate (aTextureMat, -theParams->Rotation(), 0.0f, 0.0f, 1.0f);
2566 core11ffp->glLoadMatrixf (aTextureMat.GetData());
2567 core11ffp->glMatrixMode (aMatrixMode);
2571 // =======================================================================
2572 // function : SetPointSize
2574 // =======================================================================
2575 void OpenGl_Context::SetPointSize (const Standard_ShortReal theSize)
2577 if (!myActiveProgram.IsNull())
2579 myActiveProgram->SetUniform (this, myActiveProgram->GetStateLocation (OpenGl_OCCT_POINT_SIZE), theSize);
2580 //if (myGapi == Aspect_GraphicsLibrary_OpenGL)
2581 //core11fwd->glEnable (GL_VERTEX_PROGRAM_POINT_SIZE);
2585 if (myGapi != Aspect_GraphicsLibrary_OpenGLES)
2587 core11fwd->glPointSize (theSize);
2588 if (core20fwd != NULL)
2590 //core11fwd->glDisable (GL_VERTEX_PROGRAM_POINT_SIZE);
2595 // =======================================================================
2596 // function : SetPointSpriteOrigin
2598 // =======================================================================
2599 void OpenGl_Context::SetPointSpriteOrigin()
2601 if (myGapi == Aspect_GraphicsLibrary_OpenGLES
2602 || core15fwd == NULL)
2607 const int aNewState = !myActiveProgram.IsNull() ? GL_UPPER_LEFT : GL_LOWER_LEFT;
2608 if (myPointSpriteOrig != aNewState)
2610 myPointSpriteOrig = aNewState;
2611 core15fwd->glPointParameteri (GL_POINT_SPRITE_COORD_ORIGIN, aNewState);
2615 // =======================================================================
2616 // function : SetGlNormalizeEnabled
2618 // =======================================================================
2619 Standard_Boolean OpenGl_Context::SetGlNormalizeEnabled (Standard_Boolean isEnabled)
2621 if (isEnabled == myIsGlNormalizeEnabled)
2623 return myIsGlNormalizeEnabled;
2626 Standard_Boolean anOldGlNormalize = myIsGlNormalizeEnabled;
2627 myIsGlNormalizeEnabled = isEnabled;
2629 if (core11ffp != NULL)
2633 core11fwd->glEnable (GL_NORMALIZE);
2637 core11fwd->glDisable (GL_NORMALIZE);
2641 return anOldGlNormalize;
2644 // =======================================================================
2645 // function : SetShadeModel
2647 // =======================================================================
2648 void OpenGl_Context::SetShadeModel (Graphic3d_TypeOfShadingModel theModel)
2650 if (core11ffp != NULL)
2652 const Standard_Integer aModel = theModel == Graphic3d_TypeOfShadingModel_PhongFacet
2653 || theModel == Graphic3d_TypeOfShadingModel_PbrFacet ? GL_FLAT : GL_SMOOTH;
2654 if (myShadeModel == aModel)
2658 myShadeModel = aModel;
2659 core11ffp->glShadeModel (aModel);
2663 // =======================================================================
2664 // function : SetPolygonMode
2666 // =======================================================================
2667 Standard_Integer OpenGl_Context::SetPolygonMode (const Standard_Integer theMode)
2669 if (myPolygonMode == theMode)
2671 return myPolygonMode;
2674 const Standard_Integer anOldPolygonMode = myPolygonMode;
2675 myPolygonMode = theMode;
2676 if (myGapi != Aspect_GraphicsLibrary_OpenGLES)
2678 core11fwd->glPolygonMode (GL_FRONT_AND_BACK, (GLenum)theMode);
2680 return anOldPolygonMode;
2683 // =======================================================================
2684 // function : SetPolygonHatchEnabled
2686 // =======================================================================
2687 bool OpenGl_Context::SetPolygonHatchEnabled (const bool theIsEnabled)
2689 if (core11ffp == NULL)
2693 else if (myHatchIsEnabled == theIsEnabled)
2695 return theIsEnabled;
2698 const bool anOldIsEnabled = myHatchIsEnabled;
2700 && myActiveHatchType != Aspect_HS_SOLID)
2702 core11fwd->glEnable (GL_POLYGON_STIPPLE);
2706 core11fwd->glDisable (GL_POLYGON_STIPPLE);
2709 myHatchIsEnabled = theIsEnabled;
2710 return anOldIsEnabled;
2713 // =======================================================================
2714 // function : SetPolygonHatchStyle
2716 // =======================================================================
2717 Standard_Integer OpenGl_Context::SetPolygonHatchStyle (const Handle(Graphic3d_HatchStyle)& theStyle)
2719 const Standard_Integer aNewStyle = !theStyle.IsNull() ? theStyle->HatchType() : Aspect_HS_SOLID;
2720 if (myActiveHatchType == aNewStyle
2721 || core11ffp == NULL)
2723 return myActiveHatchType;
2726 if (aNewStyle == Aspect_HS_SOLID)
2728 if (myHatchIsEnabled)
2730 core11fwd->glDisable (GL_POLYGON_STIPPLE);
2732 return myActiveHatchType;
2735 if (myHatchStyles.IsNull()
2736 && !GetResource ("OpenGl_LineAttributes", myHatchStyles))
2738 // share and register for release once the resource is no longer used
2739 myHatchStyles = new OpenGl_LineAttributes();
2740 ShareResource ("OpenGl_LineAttributes", myHatchStyles);
2743 const Standard_Integer anOldType = myActiveHatchType;
2744 myActiveHatchType = aNewStyle;
2745 myHatchStyles->SetTypeOfHatch (this, theStyle);
2746 if (myHatchIsEnabled
2747 && anOldType == Aspect_HS_SOLID)
2749 core11fwd->glEnable (GL_POLYGON_STIPPLE);
2754 // =======================================================================
2755 // function : SetPolygonOffset
2757 // =======================================================================
2758 void OpenGl_Context::SetPolygonOffset (const Graphic3d_PolygonOffset& theOffset)
2760 const bool toFillOld = (myPolygonOffset.Mode & Aspect_POM_Fill) == Aspect_POM_Fill;
2761 const bool toFillNew = (theOffset.Mode & Aspect_POM_Fill) == Aspect_POM_Fill;
2762 if (toFillNew != toFillOld)
2766 core11fwd->glEnable (GL_POLYGON_OFFSET_FILL);
2770 core11fwd->glDisable (GL_POLYGON_OFFSET_FILL);
2774 if (myGapi != Aspect_GraphicsLibrary_OpenGLES)
2776 const bool toLineOld = (myPolygonOffset.Mode & Aspect_POM_Line) == Aspect_POM_Line;
2777 const bool toLineNew = (theOffset.Mode & Aspect_POM_Line) == Aspect_POM_Line;
2778 if (toLineNew != toLineOld)
2782 core11fwd->glEnable (GL_POLYGON_OFFSET_LINE);
2786 core11fwd->glDisable (GL_POLYGON_OFFSET_LINE);
2790 const bool toPointOld = (myPolygonOffset.Mode & Aspect_POM_Point) == Aspect_POM_Point;
2791 const bool toPointNew = (theOffset.Mode & Aspect_POM_Point) == Aspect_POM_Point;
2792 if (toPointNew != toPointOld)
2796 core11fwd->glEnable (GL_POLYGON_OFFSET_POINT);
2800 core11fwd->glDisable (GL_POLYGON_OFFSET_POINT);
2805 if (myPolygonOffset.Factor != theOffset.Factor
2806 || myPolygonOffset.Units != theOffset.Units)
2808 core11fwd->glPolygonOffset (theOffset.Factor, theOffset.Units);
2810 myPolygonOffset = theOffset;
2813 // =======================================================================
2814 // function : SetCamera
2816 // =======================================================================
2817 void OpenGl_Context::SetCamera (const Handle(Graphic3d_Camera)& theCamera)
2819 myCamera = theCamera;
2820 if (!theCamera.IsNull())
2822 ProjectionState.SetCurrent (theCamera->ProjectionMatrixF());
2823 WorldViewState .SetCurrent (theCamera->OrientationMatrixF());
2824 ApplyProjectionMatrix();
2825 ApplyWorldViewMatrix();
2829 // =======================================================================
2830 // function : ApplyModelWorldMatrix
2832 // =======================================================================
2833 void OpenGl_Context::ApplyModelWorldMatrix()
2835 if (myShaderManager->ModelWorldState().ModelWorldMatrix() != ModelWorldState.Current())
2837 myShaderManager->UpdateModelWorldStateTo (ModelWorldState.Current());
2841 // =======================================================================
2842 // function : ApplyWorldViewMatrix
2844 // =======================================================================
2845 void OpenGl_Context::ApplyWorldViewMatrix()
2847 if (myShaderManager->ModelWorldState().ModelWorldMatrix() != THE_IDENTITY_MATRIX)
2849 myShaderManager->UpdateModelWorldStateTo (THE_IDENTITY_MATRIX);
2851 if (myShaderManager->WorldViewState().WorldViewMatrix() != WorldViewState.Current())
2853 myShaderManager->UpdateWorldViewStateTo (WorldViewState.Current());
2857 // =======================================================================
2858 // function : ApplyModelViewMatrix
2860 // =======================================================================
2861 void OpenGl_Context::ApplyModelViewMatrix()
2863 if (myShaderManager->ModelWorldState().ModelWorldMatrix() != ModelWorldState.Current())
2865 myShaderManager->UpdateModelWorldStateTo (ModelWorldState.Current());
2867 if (myShaderManager->WorldViewState().WorldViewMatrix() != WorldViewState.Current())
2869 myShaderManager->UpdateWorldViewStateTo (WorldViewState.Current());
2873 // =======================================================================
2874 // function : ApplyProjectionMatrix
2876 // =======================================================================
2877 void OpenGl_Context::ApplyProjectionMatrix()
2879 if (myShaderManager->ProjectionState().ProjectionMatrix() != ProjectionState.Current())
2881 myShaderManager->UpdateProjectionStateTo (ProjectionState.Current());
2885 // =======================================================================
2886 // function : EnableFeatures
2888 // =======================================================================
2889 void OpenGl_Context::EnableFeatures() const
2894 // =======================================================================
2895 // function : DisableFeatures
2897 // =======================================================================
2898 void OpenGl_Context::DisableFeatures() const
2900 // Disable stuff that's likely to slow down glDrawPixels.
2901 core11fwd->glDisable(GL_DITHER);
2902 core11fwd->glDisable(GL_BLEND);
2903 core11fwd->glDisable(GL_DEPTH_TEST);
2904 core11fwd->glDisable(GL_STENCIL_TEST);
2906 if (core11ffp == NULL)
2911 core11fwd->glDisable(GL_TEXTURE_1D);
2912 core11fwd->glDisable(GL_TEXTURE_2D);
2914 core11fwd->glDisable(GL_LIGHTING);
2915 core11fwd->glDisable(GL_ALPHA_TEST);
2916 core11fwd->glDisable(GL_FOG);
2917 core11fwd->glDisable(GL_LOGIC_OP);
2919 core11ffp->glPixelTransferi (GL_MAP_COLOR, GL_FALSE);
2920 core11ffp->glPixelTransferi (GL_RED_SCALE, 1);
2921 core11ffp->glPixelTransferi (GL_RED_BIAS, 0);
2922 core11ffp->glPixelTransferi (GL_GREEN_SCALE, 1);
2923 core11ffp->glPixelTransferi (GL_GREEN_BIAS, 0);
2924 core11ffp->glPixelTransferi (GL_BLUE_SCALE, 1);
2925 core11ffp->glPixelTransferi (GL_BLUE_BIAS, 0);
2926 core11ffp->glPixelTransferi (GL_ALPHA_SCALE, 1);
2927 core11ffp->glPixelTransferi (GL_ALPHA_BIAS, 0);
2929 if (IsGlGreaterEqual (1, 2))
2931 if (CheckExtension ("GL_CONVOLUTION_1D_EXT"))
2933 core11fwd->glDisable(GL_CONVOLUTION_1D_EXT);
2935 if (CheckExtension ("GL_CONVOLUTION_2D_EXT"))
2937 core11fwd->glDisable(GL_CONVOLUTION_2D_EXT);
2939 if (CheckExtension ("GL_SEPARABLE_2D_EXT"))
2941 core11fwd->glDisable(GL_SEPARABLE_2D_EXT);
2943 if (CheckExtension ("GL_SEPARABLE_2D_EXT"))
2945 core11fwd->glDisable(GL_HISTOGRAM_EXT);
2947 if (CheckExtension ("GL_MINMAX_EXT"))
2949 core11fwd->glDisable(GL_MINMAX_EXT);
2951 if (CheckExtension ("GL_TEXTURE_3D_EXT"))
2953 core11fwd->glDisable(GL_TEXTURE_3D_EXT);
2958 // =======================================================================
2959 // function : SetColorMaskRGBA
2961 // =======================================================================
2962 void OpenGl_Context::SetColorMaskRGBA (const NCollection_Vec4<bool>& theVal)
2964 core11fwd->glColorMask (theVal.r() ? GL_TRUE : GL_FALSE,
2965 theVal.g() ? GL_TRUE : GL_FALSE,
2966 theVal.b() ? GL_TRUE : GL_FALSE,
2967 theVal.a() ? GL_TRUE : GL_FALSE);
2968 myColorMask = theVal;
2971 // =======================================================================
2972 // function : SetColorMask
2974 // =======================================================================
2975 bool OpenGl_Context::SetColorMask (bool theToWriteColor)
2977 const bool anOldValue = myColorMask.r();
2978 myColorMask.SetValues (theToWriteColor, theToWriteColor, theToWriteColor, caps->buffersOpaqueAlpha ? false : theToWriteColor);
2979 const GLboolean toWrite = theToWriteColor ? GL_TRUE : GL_FALSE;
2980 core11fwd->glColorMask (toWrite, toWrite, toWrite, myColorMask.a() ? GL_TRUE : GL_FALSE);
2984 // =======================================================================
2985 // function : SetSampleAlphaToCoverage
2987 // =======================================================================
2988 bool OpenGl_Context::SetSampleAlphaToCoverage (bool theToEnable)
2990 bool toEnable = myAllowAlphaToCov && theToEnable;
2991 if (myAlphaToCoverage == toEnable)
2993 return myAlphaToCoverage;
2996 if (core15fwd != NULL)
3000 //core15fwd->core15fwd->glSampleCoverage (1.0f, GL_FALSE);
3001 core15fwd->glEnable (GL_SAMPLE_ALPHA_TO_COVERAGE);
3005 core15fwd->glDisable (GL_SAMPLE_ALPHA_TO_COVERAGE);
3009 const bool anOldValue = myAlphaToCoverage;
3010 myAlphaToCoverage = toEnable;
3014 // =======================================================================
3015 // function : GetBufferSubData
3017 // =======================================================================
3018 bool OpenGl_Context::GetBufferSubData (unsigned int theTarget, intptr_t theOffset, intptr_t theSize, void* theData)
3020 if (!hasGetBufferData)
3024 #ifdef __EMSCRIPTEN__
3027 Module.ctx.getBufferSubData($0, $1, HEAPU8.subarray($2, $2 + $3));
3028 }, theTarget, theOffset, theData, theSize);
3030 #elif defined(OCC_USE_GLES2)
3031 if (void* aData = core30->glMapBufferRange (theTarget, theOffset, theSize, GL_MAP_READ_BIT))
3033 memcpy (theData, aData, theSize);
3034 core30->glUnmapBuffer (theTarget);
3039 core15fwd->glGetBufferSubData (theTarget, theOffset, theSize, theData);
3044 // =======================================================================
3045 // function : DumpJson
3047 // =======================================================================
3048 void OpenGl_Context::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
3050 OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
3052 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myAnisoMax)
3053 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myTexClamp)
3054 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myMaxTexDim)
3055 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myMaxTexCombined)
3056 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myMaxDumpSizeX)
3057 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myMaxDumpSizeY)
3058 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myMaxClipPlanes)
3059 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myMaxMsaaSamples)
3060 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myMaxDrawBuffers)
3061 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myMaxColorAttachments)
3062 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myGlVerMajor)
3063 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myGlVerMinor)
3064 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myIsInitialized)
3065 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myIsStereoBuffers)
3066 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myIsGlNormalizeEnabled)
3068 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myHasRayTracing)
3069 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myHasRayTracingTextures)
3070 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myHasRayTracingAdaptiveSampling)
3071 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myHasRayTracingAdaptiveSamplingAtomic)
3073 for (int i = 0; i < 4; i++)
3075 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myViewport[i])
3078 for (int i = 0; i < 4; i++)
3080 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myViewportVirt[i])
3083 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myPointSpriteOrig)
3084 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myRenderMode)
3085 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myPolygonMode)
3086 OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &myPolygonOffset)
3087 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myToCullBackFaces)
3088 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myReadBuffer)
3090 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myDefaultVao)
3091 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myColorMask)
3092 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myAllowAlphaToCov)
3093 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myAlphaToCoverage)
3094 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myIsGlDebugCtx)
3096 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myResolution)
3097 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myResolutionRatio)
3099 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myLineWidthScale)
3100 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myLineFeather)
3101 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myRenderScale)
3102 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myRenderScaleInv)
3104 OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &ModelWorldState)
3105 OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &WorldViewState)
3106 OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &ProjectionState)
3109 // =======================================================================
3110 // function : DumpJsonOpenGlState
3112 // =======================================================================
3113 void OpenGl_Context::DumpJsonOpenGlState (Standard_OStream& theOStream, Standard_Integer)
3115 GLboolean isEnableBlend = core11fwd->glIsEnabled (GL_BLEND);
3116 GLboolean isEnableCullFace = core11fwd->glIsEnabled (GL_CULL_FACE);
3117 GLboolean isEnableDepthTest = core11fwd->glIsEnabled (GL_DEPTH_TEST);
3119 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, isEnableBlend)
3120 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, isEnableCullFace)
3121 OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, isEnableDepthTest)