Commit | Line | Data |
---|---|---|
6aca4d39 | 1 | // Created on: 2012-01-26 |
b311480e | 2 | // Created by: Kirill GAVRILOV |
6aca4d39 | 3 | // Copyright (c) 2012-2014 OPEN CASCADE SAS |
b311480e | 4 | // |
973c2be1 | 5 | // This file is part of Open CASCADE Technology software library. |
b311480e | 6 | // |
d5f74e42 | 7 | // This library is free software; you can redistribute it and/or modify it under |
8 | // the terms of the GNU Lesser General Public License version 2.1 as published | |
973c2be1 | 9 | // by the Free Software Foundation, with special exception defined in the file |
10 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT | |
11 | // distribution for complete text of the license and disclaimer of any warranty. | |
b311480e | 12 | // |
973c2be1 | 13 | // Alternatively, this file may be used under the terms of Open CASCADE |
14 | // commercial license or contractual agreement. | |
b311480e | 15 | |
58655684 | 16 | #if defined(_WIN32) |
5f8b738e | 17 | #include <windows.h> |
18 | #endif | |
19 | ||
2166f0fa SK |
20 | #include <OpenGl_Context.hxx> |
21 | ||
5e27df78 | 22 | #include <OpenGl_ArbTBO.hxx> |
23 | #include <OpenGl_ArbIns.hxx> | |
58655684 | 24 | #include <OpenGl_ArbDbg.hxx> |
01ca42b2 | 25 | #include <OpenGl_ArbFBO.hxx> |
37eb4787 | 26 | #include <OpenGl_ExtGS.hxx> |
cc8cbabe | 27 | #include <OpenGl_ArbSamplerObject.hxx> |
25ef750e | 28 | #include <OpenGl_ArbTexBindless.hxx> |
952a0565 | 29 | #include <OpenGl_GlCore46.hxx> |
a2e4f780 | 30 | #include <OpenGl_FrameBuffer.hxx> |
15669413 | 31 | #include <OpenGl_FrameStats.hxx> |
25ef750e | 32 | #include <OpenGl_Sampler.hxx> |
30f0ad28 | 33 | #include <OpenGl_ShaderManager.hxx> |
d850414a | 34 | #include <OpenGl_TextureSetPairIterator.hxx> |
299e0ab9 | 35 | #include <OpenGl_Workspace.hxx> |
bf5f0ca2 | 36 | #include <OpenGl_Aspects.hxx> |
158f2931 | 37 | |
79f4f036 | 38 | #include <Graphic3d_TransformUtils.hxx> |
56689b27 | 39 | #include <Graphic3d_RenderingParams.hxx> |
faff3767 | 40 | #include <Image_SupportedFormats.hxx> |
cbf18624 | 41 | #include <Message_Messenger.hxx> |
a174a3c5 | 42 | #include <NCollection_Vector.hxx> |
2bd4c032 | 43 | #include <Standard_ProgramError.hxx> |
158f2931 | 44 | #include <Standard_WarningDisableFunctionCast.hxx> |
2bd4c032 | 45 | |
a46ab511 | 46 | #if defined(_WIN32) && defined(max) |
47 | #undef max | |
48 | #endif | |
49 | #include <limits> | |
50 | ||
f5f4ebd0 | 51 | IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Context,Standard_Transient) |
52 | ||
da8bb41d | 53 | #if defined(HAVE_EGL) |
54 | #include <EGL/egl.h> | |
55 | #ifdef _MSC_VER | |
56 | #pragma comment(lib, "libEGL.lib") | |
57 | #endif | |
58 | #elif defined(_WIN32) | |
5f8b738e | 59 | // |
60 | #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX) | |
61 | #include <dlfcn.h> | |
f978241f | 62 | #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE |
63 | // | |
64 | #else | |
65 | #include <OpenGL/OpenGL.h> | |
26d9c835 | 66 | #include <CoreGraphics/CoreGraphics.h> |
f978241f | 67 | #endif |
5f8b738e | 68 | #else |
69 | #include <GL/glx.h> // glXGetProcAddress() | |
70 | #endif | |
71 | ||
565baee6 | 72 | #ifdef __EMSCRIPTEN__ |
d4cefcc0 | 73 | #include <emscripten.h> |
565baee6 | 74 | #include <emscripten/html5.h> |
75 | ||
76 | //! Check if WebGL extension is available and activate it | |
77 | //! (usage of extension without activation will generate errors). | |
78 | static bool checkEnableWebGlExtension (const OpenGl_Context& theCtx, | |
79 | const char* theExtName) | |
80 | { | |
81 | if (!theCtx.CheckExtension (theExtName)) | |
82 | { | |
83 | return false; | |
84 | } | |
85 | if (EMSCRIPTEN_WEBGL_CONTEXT_HANDLE aWebGlCtx = emscripten_webgl_get_current_context()) | |
86 | { | |
87 | if (emscripten_webgl_enable_extension (aWebGlCtx, theExtName)) | |
88 | { | |
89 | return true; | |
90 | } | |
91 | } | |
92 | return false; | |
93 | } | |
94 | #endif | |
95 | ||
5e27df78 | 96 | namespace |
97 | { | |
98 | static const Handle(OpenGl_Resource) NULL_GL_RESOURCE; | |
458e3c0d | 99 | static const OpenGl_Mat4 THE_IDENTITY_MATRIX; |
26d9c835 | 100 | |
101 | //! Add key-value pair to the dictionary. | |
102 | static void addInfo (TColStd_IndexedDataMapOfStringString& theDict, | |
103 | const TCollection_AsciiString& theKey, | |
104 | const TCollection_AsciiString& theValue) | |
105 | { | |
106 | theDict.ChangeFromIndex (theDict.Add (theKey, theValue)) = theValue; | |
107 | } | |
108 | ||
109 | //! Add key-value pair to the dictionary. | |
110 | static void addInfo (TColStd_IndexedDataMapOfStringString& theDict, | |
111 | const TCollection_AsciiString& theKey, | |
112 | const char* theValue) | |
113 | { | |
114 | TCollection_AsciiString aValue (theValue != NULL ? theValue : ""); | |
115 | theDict.ChangeFromIndex (theDict.Add (theKey, aValue)) = aValue; | |
116 | } | |
01ca42b2 | 117 | } |
5e27df78 | 118 | |
2166f0fa SK |
119 | // ======================================================================= |
120 | // function : OpenGl_Context | |
121 | // purpose : | |
122 | // ======================================================================= | |
58655684 | 123 | OpenGl_Context::OpenGl_Context (const Handle(OpenGl_Caps)& theCaps) |
43eddb47 | 124 | : core11ffp (NULL), |
01ca42b2 | 125 | core11fwd (NULL), |
126 | core15 (NULL), | |
01ca42b2 | 127 | core20 (NULL), |
872f98d9 | 128 | core30 (NULL), |
01ca42b2 | 129 | core32 (NULL), |
25ef750e | 130 | core33 (NULL), |
01ca42b2 | 131 | core41 (NULL), |
01ca42b2 | 132 | core42 (NULL), |
01ca42b2 | 133 | core43 (NULL), |
01ca42b2 | 134 | core44 (NULL), |
9491df8c | 135 | core45 (NULL), |
952a0565 | 136 | core46 (NULL), |
43eddb47 | 137 | core15fwd (NULL), |
138 | core20fwd (NULL), | |
58655684 | 139 | caps (!theCaps.IsNull() ? theCaps : new OpenGl_Caps()), |
d4cefcc0 | 140 | hasGetBufferData (Standard_False), |
ca3c13d1 | 141 | #if defined(GL_ES_VERSION_2_0) |
e1d17ceb | 142 | hasPackRowLength (Standard_False), |
143 | hasUnpackRowLength (Standard_False), | |
ca3c13d1 | 144 | hasHighp (Standard_False), |
e99a2f7c | 145 | hasUintIndex(Standard_False), |
ca3c13d1 | 146 | hasTexRGBA8(Standard_False), |
147 | #else | |
e1d17ceb | 148 | hasPackRowLength (Standard_True), |
149 | hasUnpackRowLength (Standard_True), | |
ca3c13d1 | 150 | hasHighp (Standard_True), |
e99a2f7c | 151 | hasUintIndex(Standard_True), |
ca3c13d1 | 152 | hasTexRGBA8(Standard_True), |
ba00aab7 | 153 | #endif |
67312b79 | 154 | hasTexFloatLinear (Standard_False), |
ba00aab7 | 155 | hasTexSRGB (Standard_False), |
156 | hasFboSRGB (Standard_False), | |
157 | hasSRGBControl (Standard_False), | |
8f8fe4a9 | 158 | hasFboRenderMipmap (Standard_False), |
ba00aab7 | 159 | #if defined(GL_ES_VERSION_2_0) |
160 | hasFlatShading (OpenGl_FeatureNotAvailable), | |
161 | #else | |
c39bb31b | 162 | hasFlatShading (OpenGl_FeatureInCore), |
ca3c13d1 | 163 | #endif |
59515ca6 | 164 | hasGlslBitwiseOps (OpenGl_FeatureNotAvailable), |
a1073ae2 | 165 | hasDrawBuffers (OpenGl_FeatureNotAvailable), |
166 | hasFloatBuffer (OpenGl_FeatureNotAvailable), | |
167 | hasHalfFloatBuffer (OpenGl_FeatureNotAvailable), | |
168 | hasSampleVariables (OpenGl_FeatureNotAvailable), | |
2a332745 | 169 | hasGeometryStage (OpenGl_FeatureNotAvailable), |
a1073ae2 | 170 | arbDrawBuffers (Standard_False), |
25ef750e | 171 | arbNPTW (Standard_False), |
172 | arbTexRG (Standard_False), | |
74fb257d | 173 | arbTexFloat (Standard_False), |
cc8cbabe | 174 | arbSamplerObject (NULL), |
25ef750e | 175 | arbTexBindless (NULL), |
5e27df78 | 176 | arbTBO (NULL), |
25ef750e | 177 | arbTboRGB32 (Standard_False), |
e70625d6 | 178 | arbClipControl (Standard_False), |
5e27df78 | 179 | arbIns (NULL), |
58655684 | 180 | arbDbg (NULL), |
01ca42b2 | 181 | arbFBO (NULL), |
b86bb3df | 182 | arbFBOBlit (NULL), |
a1073ae2 | 183 | arbSampleShading (Standard_False), |
4b52faa5 | 184 | arbDepthClamp (Standard_False), |
1ce0716b | 185 | extFragDepth (Standard_False), |
a1073ae2 | 186 | extDrawBuffers (Standard_False), |
37eb4787 | 187 | extGS (NULL), |
bf75be98 | 188 | extBgra(Standard_False), |
189 | extAnis(Standard_False), | |
30f0ad28 | 190 | extPDS (Standard_False), |
f0430952 | 191 | atiMem (Standard_False), |
192 | nvxMem (Standard_False), | |
a1073ae2 | 193 | oesSampleVariables (Standard_False), |
8c3237d4 | 194 | oesStdDerivatives (Standard_False), |
5e27df78 | 195 | mySharedResources (new OpenGl_ResourcesMap()), |
a174a3c5 | 196 | myDelayed (new OpenGl_DelayReleaseMap()), |
3125ebb6 | 197 | myUnusedResources (new OpenGl_ResourcesStack()), |
4269bd1b | 198 | myClippingState (), |
5f8b738e | 199 | myGlLibHandle (NULL), |
01ca42b2 | 200 | myFuncs (new OpenGl_GlFunctions()), |
faff3767 | 201 | mySupportedFormats (new Image_SupportedFormats()), |
2f6cb3ac | 202 | myAnisoMax (1), |
ca3c13d1 | 203 | myTexClamp (GL_CLAMP_TO_EDGE), |
eafb234b | 204 | myMaxTexDim (1024), |
cc8cbabe | 205 | myMaxTexCombined (1), |
5c225e8e | 206 | myMaxTexUnitsFFP (1), |
6997ff1c | 207 | myMaxDumpSizeX (1024), |
208 | myMaxDumpSizeY (1024), | |
4269bd1b | 209 | myMaxClipPlanes (6), |
3c4b62a4 | 210 | myMaxMsaaSamples(0), |
a1073ae2 | 211 | myMaxDrawBuffers (1), |
212 | myMaxColorAttachments (1), | |
5f8b738e | 213 | myGlVerMajor (0), |
214 | myGlVerMinor (0), | |
b5ac8292 | 215 | myIsInitialized (Standard_False), |
216 | myIsStereoBuffers (Standard_False), | |
7d9e854b | 217 | myIsGlNormalizeEnabled (Standard_False), |
72f6dc61 | 218 | mySpriteTexUnit (Graphic3d_TextureUnit_PointSprite), |
3a9b5dc8 | 219 | myHasRayTracing (Standard_False), |
220 | myHasRayTracingTextures (Standard_False), | |
221 | myHasRayTracingAdaptiveSampling (Standard_False), | |
e084dbbc | 222 | myHasRayTracingAdaptiveSamplingAtomic (Standard_False), |
67312b79 | 223 | myHasPBR (Standard_False), |
72f6dc61 | 224 | myPBREnvLUTTexUnit (Graphic3d_TextureUnit_PbrEnvironmentLUT), |
225 | myPBRDiffIBLMapSHTexUnit (Graphic3d_TextureUnit_PbrIblDiffuseSH), | |
226 | myPBRSpecIBLMapTexUnit (Graphic3d_TextureUnit_PbrIblSpecular), | |
d84e8669 | 227 | myShadowMapTexUnit (Graphic3d_TextureUnit_ShadowMap), |
78c4e836 | 228 | myDepthPeelingDepthTexUnit (Graphic3d_TextureUnit_DepthPeelingDepth), |
229 | myDepthPeelingFrontColorTexUnit (Graphic3d_TextureUnit_DepthPeelingFrontColor), | |
15669413 | 230 | myFrameStats (new OpenGl_FrameStats()), |
72f6dc61 | 231 | myActiveMockTextures (0), |
76fada68 | 232 | myActiveHatchType (Aspect_HS_SOLID), |
233 | myHatchIsEnabled (false), | |
ca3c13d1 | 234 | #if !defined(GL_ES_VERSION_2_0) |
fd59283a | 235 | myPointSpriteOrig (GL_UPPER_LEFT), |
7d3e64ef | 236 | myRenderMode (GL_RENDER), |
08669adf | 237 | myShadeModel (GL_SMOOTH), |
6d0e6be5 | 238 | myPolygonMode (GL_FILL), |
ca3c13d1 | 239 | #else |
fd59283a | 240 | myPointSpriteOrig (0), |
ca3c13d1 | 241 | myRenderMode (0), |
08669adf | 242 | myShadeModel (0), |
6d0e6be5 | 243 | myPolygonMode (0), |
ca3c13d1 | 244 | #endif |
b6472664 | 245 | myToCullBackFaces (false), |
38a0206f | 246 | myReadBuffer (0), |
c3487460 | 247 | myDrawBuffers (0, 7), |
4e1523ef | 248 | myDefaultVao (0), |
f88457e6 | 249 | myColorMask (true), |
c40eb6b9 | 250 | myAlphaToCoverage (false), |
ba00aab7 | 251 | myIsGlDebugCtx (false), |
252 | myIsSRgbWindow (false), | |
56689b27 | 253 | myResolution (Graphic3d_RenderingParams::THE_DEFAULT_RESOLUTION), |
254 | myResolutionRatio (1.0f), | |
255 | myLineWidthScale (1.0f), | |
2a332745 | 256 | myLineFeather (1.0f), |
56689b27 | 257 | myRenderScale (1.0f), |
258 | myRenderScaleInv (1.0f) | |
2166f0fa | 259 | { |
3bffef55 | 260 | myViewport[0] = 0; |
261 | myViewport[1] = 0; | |
262 | myViewport[2] = 0; | |
263 | myViewport[3] = 0; | |
56689b27 | 264 | myViewportVirt[0] = 0; |
265 | myViewportVirt[1] = 0; | |
266 | myViewportVirt[2] = 0; | |
267 | myViewportVirt[3] = 0; | |
3bffef55 | 268 | |
8d1a539c | 269 | myPolygonOffset.Mode = Aspect_POM_Off; |
270 | myPolygonOffset.Factor = 0.0f; | |
271 | myPolygonOffset.Units = 0.0f; | |
272 | ||
4e1523ef | 273 | // system-dependent fields |
274 | #if defined(HAVE_EGL) | |
275 | myDisplay = (Aspect_Display )EGL_NO_DISPLAY; | |
276 | myWindow = (Aspect_Drawable )EGL_NO_SURFACE; | |
458c2c58 | 277 | myGContext = (Aspect_RenderingContext )EGL_NO_CONTEXT; |
4e1523ef | 278 | #elif defined(_WIN32) |
279 | myWindow = NULL; | |
280 | myWindowDC = NULL; | |
281 | myGContext = NULL; | |
282 | #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX) | |
5f8b738e | 283 | // Vendors can not extend functionality on this system |
284 | // and developers are limited to OpenGL support provided by Mac OS X SDK. | |
285 | // We retrieve function pointers from system library | |
286 | // to generalize extensions support on all platforms. | |
287 | // In this way we also reach binary compatibility benefit between OS releases | |
288 | // if some newest functionality is optionally used. | |
289 | // Notice that GL version / extension availability checks are required | |
290 | // because function pointers may be available but not functionality itself | |
291 | // (depends on renderer). | |
a2e4f780 | 292 | #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE |
293 | myGContext = NULL; | |
294 | myGlLibHandle = dlopen ("/System/Library/Frameworks/OpenGLES.framework/OpenGLES", RTLD_LAZY); | |
295 | #else | |
4e1523ef | 296 | myGContext = NULL; |
5f8b738e | 297 | myGlLibHandle = dlopen ("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL", RTLD_LAZY); |
a2e4f780 | 298 | #endif |
4e1523ef | 299 | #else |
300 | myDisplay = NULL; | |
301 | myWindow = 0; | |
302 | myGContext = 0; | |
5f8b738e | 303 | #endif |
a2e4f780 | 304 | |
01ca42b2 | 305 | memset (myFuncs.operator->(), 0, sizeof(OpenGl_GlFunctions)); |
30f0ad28 | 306 | myShaderManager = new OpenGl_ShaderManager (this); |
2166f0fa SK |
307 | } |
308 | ||
309 | // ======================================================================= | |
310 | // function : ~OpenGl_Context | |
311 | // purpose : | |
312 | // ======================================================================= | |
313 | OpenGl_Context::~OpenGl_Context() | |
314 | { | |
5e27df78 | 315 | // release clean up queue |
316 | ReleaseDelayed(); | |
317 | ||
4e1523ef | 318 | #if !defined(GL_ES_VERSION_2_0) |
319 | // release default VAO | |
320 | if (myDefaultVao != 0 | |
321 | && IsValid() | |
322 | && core32 != NULL) | |
323 | { | |
324 | core32->glDeleteVertexArrays (1, &myDefaultVao); | |
325 | } | |
326 | myDefaultVao = 0; | |
327 | #endif | |
328 | ||
72f6dc61 | 329 | // release mock textures |
330 | if (!myTextureRgbaBlack.IsNull()) | |
331 | { | |
332 | myTextureRgbaBlack->Release (this); | |
333 | myTextureRgbaBlack.Nullify(); | |
334 | } | |
335 | if (!myTextureRgbaWhite.IsNull()) | |
336 | { | |
337 | myTextureRgbaWhite->Release (this); | |
338 | myTextureRgbaWhite.Nullify(); | |
339 | } | |
340 | ||
a2e4f780 | 341 | // release default FBO |
342 | if (!myDefaultFbo.IsNull()) | |
343 | { | |
344 | myDefaultFbo->Release (this); | |
345 | myDefaultFbo.Nullify(); | |
346 | } | |
347 | ||
5e27df78 | 348 | // release shared resources if any |
4796758e | 349 | if (mySharedResources->GetRefCount() <= 1) |
5e27df78 | 350 | { |
392ac980 | 351 | myShaderManager.Nullify(); |
5e27df78 | 352 | for (NCollection_DataMap<TCollection_AsciiString, Handle(OpenGl_Resource)>::Iterator anIter (*mySharedResources); |
353 | anIter.More(); anIter.Next()) | |
354 | { | |
355 | anIter.Value()->Release (this); | |
356 | } | |
e1c659da | 357 | |
358 | // release delayed resources added during deletion of shared resources | |
359 | while (!myUnusedResources->IsEmpty()) | |
360 | { | |
361 | myUnusedResources->First()->Release (this); | |
362 | myUnusedResources->RemoveFirst(); | |
363 | } | |
5e27df78 | 364 | } |
c357e426 | 365 | else if (myShaderManager->IsSameContext (this)) |
392ac980 | 366 | { |
367 | myShaderManager->SetContext (NULL); | |
368 | } | |
5e27df78 | 369 | mySharedResources.Nullify(); |
a174a3c5 | 370 | myDelayed.Nullify(); |
5e27df78 | 371 | |
cbf18624 | 372 | if (arbDbg != NULL |
4e1523ef | 373 | && myIsGlDebugCtx |
f8c8ba7a | 374 | && IsValid()) |
cbf18624 | 375 | { |
376 | // reset callback | |
0deb6f04 | 377 | #if !defined(GL_ES_VERSION_2_0) |
cbf18624 | 378 | void* aPtr = NULL; |
3b523c4c | 379 | glGetPointerv (GL_DEBUG_CALLBACK_USER_PARAM, &aPtr); |
cbf18624 | 380 | if (aPtr == this) |
0deb6f04 | 381 | #endif |
cbf18624 | 382 | { |
0deb6f04 | 383 | arbDbg->glDebugMessageCallback (NULL, NULL); |
cbf18624 | 384 | } |
4e1523ef | 385 | myIsGlDebugCtx = Standard_False; |
cbf18624 | 386 | } |
5f8b738e | 387 | } |
388 | ||
05e2200b | 389 | // ======================================================================= |
390 | // function : forcedRelease | |
391 | // purpose : | |
392 | // ======================================================================= | |
393 | void OpenGl_Context::forcedRelease() | |
394 | { | |
395 | ReleaseDelayed(); | |
396 | for (NCollection_DataMap<TCollection_AsciiString, Handle(OpenGl_Resource)>::Iterator anIter (*mySharedResources); | |
397 | anIter.More(); anIter.Next()) | |
398 | { | |
399 | anIter.Value()->Release (this); | |
400 | } | |
401 | mySharedResources->Clear(); | |
402 | myShaderManager->clear(); | |
403 | myShaderManager->SetContext (NULL); | |
e1c659da | 404 | |
405 | // release delayed resources added during deletion of shared resources | |
406 | while (!myUnusedResources->IsEmpty()) | |
407 | { | |
408 | myUnusedResources->First()->Release (this); | |
409 | myUnusedResources->RemoveFirst(); | |
410 | } | |
05e2200b | 411 | } |
412 | ||
3bffef55 | 413 | // ======================================================================= |
414 | // function : ResizeViewport | |
415 | // purpose : | |
416 | // ======================================================================= | |
417 | void OpenGl_Context::ResizeViewport (const Standard_Integer* theRect) | |
418 | { | |
419 | core11fwd->glViewport (theRect[0], theRect[1], theRect[2], theRect[3]); | |
420 | myViewport[0] = theRect[0]; | |
421 | myViewport[1] = theRect[1]; | |
422 | myViewport[2] = theRect[2]; | |
423 | myViewport[3] = theRect[3]; | |
56689b27 | 424 | if (HasRenderScale()) |
425 | { | |
426 | myViewportVirt[0] = Standard_Integer(theRect[0] * myRenderScaleInv); | |
427 | myViewportVirt[1] = Standard_Integer(theRect[1] * myRenderScaleInv); | |
428 | myViewportVirt[2] = Standard_Integer(theRect[2] * myRenderScaleInv); | |
429 | myViewportVirt[3] = Standard_Integer(theRect[3] * myRenderScaleInv); | |
430 | } | |
431 | else | |
432 | { | |
433 | myViewportVirt[0] = theRect[0]; | |
434 | myViewportVirt[1] = theRect[1]; | |
435 | myViewportVirt[2] = theRect[2]; | |
436 | myViewportVirt[3] = theRect[3]; | |
437 | } | |
3bffef55 | 438 | } |
439 | ||
ca3c13d1 | 440 | #if !defined(GL_ES_VERSION_2_0) |
38a0206f | 441 | inline Standard_Integer stereoToMonoBuffer (const Standard_Integer theBuffer) |
442 | { | |
443 | switch (theBuffer) | |
b5ac8292 | 444 | { |
38a0206f | 445 | case GL_BACK_LEFT: |
446 | case GL_BACK_RIGHT: | |
447 | return GL_BACK; | |
448 | case GL_FRONT_LEFT: | |
449 | case GL_FRONT_RIGHT: | |
450 | return GL_FRONT; | |
451 | default: | |
452 | return theBuffer; | |
b5ac8292 | 453 | } |
454 | } | |
38a0206f | 455 | #endif |
b5ac8292 | 456 | |
457 | // ======================================================================= | |
38a0206f | 458 | // function : SetReadBuffer |
b5ac8292 | 459 | // purpose : |
460 | // ======================================================================= | |
38a0206f | 461 | void OpenGl_Context::SetReadBuffer (const Standard_Integer theReadBuffer) |
b5ac8292 | 462 | { |
ca3c13d1 | 463 | #if !defined(GL_ES_VERSION_2_0) |
38a0206f | 464 | myReadBuffer = !myIsStereoBuffers ? stereoToMonoBuffer (theReadBuffer) : theReadBuffer; |
465 | if (myReadBuffer < GL_COLOR_ATTACHMENT0 | |
466 | && arbFBO != NULL) | |
b5ac8292 | 467 | { |
38a0206f | 468 | arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER); |
b5ac8292 | 469 | } |
38a0206f | 470 | ::glReadBuffer (myReadBuffer); |
471 | #else | |
472 | (void )theReadBuffer; | |
ca3c13d1 | 473 | #endif |
b5ac8292 | 474 | } |
475 | ||
476 | // ======================================================================= | |
38a0206f | 477 | // function : SetDrawBuffer |
b5ac8292 | 478 | // purpose : |
479 | // ======================================================================= | |
38a0206f | 480 | void OpenGl_Context::SetDrawBuffer (const Standard_Integer theDrawBuffer) |
b5ac8292 | 481 | { |
ca3c13d1 | 482 | #if !defined(GL_ES_VERSION_2_0) |
a1073ae2 | 483 | const Standard_Integer aDrawBuffer = !myIsStereoBuffers ? stereoToMonoBuffer (theDrawBuffer) : theDrawBuffer; |
484 | if (aDrawBuffer < GL_COLOR_ATTACHMENT0 | |
38a0206f | 485 | && arbFBO != NULL) |
b5ac8292 | 486 | { |
38a0206f | 487 | arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER); |
b5ac8292 | 488 | } |
a1073ae2 | 489 | ::glDrawBuffer (aDrawBuffer); |
490 | ||
c3487460 | 491 | myDrawBuffers.Init (GL_NONE); |
492 | myDrawBuffers.SetValue (0, aDrawBuffer); | |
38a0206f | 493 | #else |
494 | (void )theDrawBuffer; | |
ca3c13d1 | 495 | #endif |
b5ac8292 | 496 | } |
497 | ||
a1073ae2 | 498 | // ======================================================================= |
499 | // function : SetDrawBuffers | |
500 | // purpose : | |
501 | // ======================================================================= | |
502 | void OpenGl_Context::SetDrawBuffers (const Standard_Integer theNb, const Standard_Integer* theDrawBuffers) | |
503 | { | |
504 | Standard_ASSERT_RETURN (hasDrawBuffers, "Multiple draw buffers feature is not supported by the context", Standard_ASSERT_DO_NOTHING()); | |
505 | ||
c3487460 | 506 | if (myDrawBuffers.Length() < theNb) |
507 | { | |
508 | // should actually never happen here | |
509 | myDrawBuffers.Resize (0, theNb - 1, false); | |
510 | } | |
511 | myDrawBuffers.Init (GL_NONE); | |
a1073ae2 | 512 | |
513 | Standard_Boolean useDefaultFbo = Standard_False; | |
514 | for (Standard_Integer anI = 0; anI < theNb; ++anI) | |
515 | { | |
b17e5bae | 516 | if (theDrawBuffers[anI] < GL_COLOR_ATTACHMENT0 && theDrawBuffers[anI] != GL_NONE) |
a1073ae2 | 517 | { |
518 | useDefaultFbo = Standard_True; | |
519 | } | |
c3487460 | 520 | else |
a1073ae2 | 521 | { |
b17e5bae | 522 | myDrawBuffers.SetValue (anI, theDrawBuffers[anI]); |
a1073ae2 | 523 | } |
524 | } | |
525 | if (arbFBO != NULL && useDefaultFbo) | |
526 | { | |
527 | arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER); | |
528 | } | |
529 | ||
530 | myFuncs->glDrawBuffers (theNb, (const GLenum*)theDrawBuffers); | |
531 | } | |
532 | ||
ba00aab7 | 533 | // ======================================================================= |
534 | // function : SetFrameBufferSRGB | |
535 | // purpose : | |
536 | // ======================================================================= | |
e5c11edd | 537 | void OpenGl_Context::SetFrameBufferSRGB (bool theIsFbo, bool theIsFboSRgb) |
ba00aab7 | 538 | { |
539 | if (!hasFboSRGB) | |
540 | { | |
541 | myIsSRgbActive = false; | |
542 | return; | |
543 | } | |
544 | myIsSRgbActive = ToRenderSRGB() | |
e5c11edd | 545 | && (theIsFbo || myIsSRgbWindow) |
546 | && theIsFboSRgb; | |
ba00aab7 | 547 | if (!hasSRGBControl) |
548 | { | |
549 | return; | |
550 | } | |
551 | ||
552 | if (myIsSRgbActive) | |
553 | { | |
554 | core11fwd->glEnable (GL_FRAMEBUFFER_SRGB); | |
555 | } | |
556 | else | |
557 | { | |
558 | core11fwd->glDisable (GL_FRAMEBUFFER_SRGB); | |
559 | } | |
560 | } | |
561 | ||
b6472664 | 562 | // ======================================================================= |
563 | // function : SetCullBackFaces | |
564 | // purpose : | |
565 | // ======================================================================= | |
566 | void OpenGl_Context::SetCullBackFaces (bool theToEnable) | |
567 | { | |
568 | if (myToCullBackFaces == theToEnable) | |
569 | { | |
570 | return; | |
571 | } | |
572 | ||
573 | myToCullBackFaces = theToEnable; | |
574 | if (theToEnable) | |
575 | { | |
576 | //glCullFace (GL_BACK); GL_BACK by default | |
577 | core11fwd->glEnable (GL_CULL_FACE); | |
578 | } | |
579 | else | |
580 | { | |
581 | core11fwd->glDisable (GL_CULL_FACE); | |
582 | } | |
583 | } | |
584 | ||
b5ac8292 | 585 | // ======================================================================= |
586 | // function : FetchState | |
587 | // purpose : | |
588 | // ======================================================================= | |
589 | void OpenGl_Context::FetchState() | |
590 | { | |
ca3c13d1 | 591 | #if !defined(GL_ES_VERSION_2_0) |
b5ac8292 | 592 | // cache feedback mode state |
43eddb47 | 593 | if (core11ffp != NULL) |
4e1523ef | 594 | { |
595 | ::glGetIntegerv (GL_RENDER_MODE, &myRenderMode); | |
08669adf | 596 | ::glGetIntegerv (GL_SHADE_MODEL, &myShadeModel); |
4e1523ef | 597 | } |
b5ac8292 | 598 | |
a1073ae2 | 599 | // cache read buffers state |
38a0206f | 600 | ::glGetIntegerv (GL_READ_BUFFER, &myReadBuffer); |
a1073ae2 | 601 | |
602 | // cache draw buffers state | |
c3487460 | 603 | if (myDrawBuffers.Length() < myMaxDrawBuffers) |
604 | { | |
605 | myDrawBuffers.Resize (0, myMaxDrawBuffers - 1, false); | |
606 | } | |
607 | myDrawBuffers.Init (GL_NONE); | |
a1073ae2 | 608 | |
c3487460 | 609 | Standard_Integer aDrawBuffer = GL_NONE; |
a1073ae2 | 610 | if (myMaxDrawBuffers == 1) |
611 | { | |
a1073ae2 | 612 | ::glGetIntegerv (GL_DRAW_BUFFER, &aDrawBuffer); |
c3487460 | 613 | myDrawBuffers.SetValue (0, aDrawBuffer); |
a1073ae2 | 614 | } |
615 | else | |
616 | { | |
a1073ae2 | 617 | for (Standard_Integer anI = 0; anI < myMaxDrawBuffers; ++anI) |
618 | { | |
619 | ::glGetIntegerv (GL_DRAW_BUFFER0 + anI, &aDrawBuffer); | |
c3487460 | 620 | myDrawBuffers.SetValue (anI, aDrawBuffer); |
a1073ae2 | 621 | } |
622 | } | |
ca3c13d1 | 623 | #endif |
b5ac8292 | 624 | } |
625 | ||
5e27df78 | 626 | // ======================================================================= |
627 | // function : Share | |
628 | // purpose : | |
629 | // ======================================================================= | |
630 | void OpenGl_Context::Share (const Handle(OpenGl_Context)& theShareCtx) | |
631 | { | |
632 | if (!theShareCtx.IsNull()) | |
633 | { | |
634 | mySharedResources = theShareCtx->mySharedResources; | |
a174a3c5 | 635 | myDelayed = theShareCtx->myDelayed; |
3125ebb6 | 636 | myUnusedResources = theShareCtx->myUnusedResources; |
392ac980 | 637 | myShaderManager = theShareCtx->myShaderManager; |
5e27df78 | 638 | } |
639 | } | |
640 | ||
4fe56619 | 641 | #if !defined(__APPLE__) || defined(MACOSX_USE_GLX) |
642 | ||
86fa64d9 | 643 | // ======================================================================= |
644 | // function : IsCurrent | |
645 | // purpose : | |
646 | // ======================================================================= | |
647 | Standard_Boolean OpenGl_Context::IsCurrent() const | |
648 | { | |
da8bb41d | 649 | #if defined(HAVE_EGL) |
650 | if ((EGLDisplay )myDisplay == EGL_NO_DISPLAY | |
da8bb41d | 651 | || (EGLContext )myGContext == EGL_NO_CONTEXT) |
652 | { | |
653 | return Standard_False; | |
654 | } | |
655 | ||
656 | return (((EGLDisplay )myDisplay == eglGetCurrentDisplay()) | |
657 | && ((EGLContext )myGContext == eglGetCurrentContext()) | |
658 | && ((EGLSurface )myWindow == eglGetCurrentSurface (EGL_DRAW))); | |
659 | #elif defined(_WIN32) | |
86fa64d9 | 660 | if (myWindowDC == NULL || myGContext == NULL) |
661 | { | |
662 | return Standard_False; | |
663 | } | |
664 | return (( (HDC )myWindowDC == wglGetCurrentDC()) | |
665 | && ((HGLRC )myGContext == wglGetCurrentContext())); | |
666 | #else | |
667 | if (myDisplay == NULL || myWindow == 0 || myGContext == 0) | |
668 | { | |
669 | return Standard_False; | |
670 | } | |
671 | ||
672 | return ( ((Display* )myDisplay == glXGetCurrentDisplay()) | |
673 | && ((GLXContext )myGContext == glXGetCurrentContext()) | |
674 | && ((GLXDrawable )myWindow == glXGetCurrentDrawable())); | |
675 | #endif | |
676 | } | |
677 | ||
2bd4c032 | 678 | // ======================================================================= |
679 | // function : MakeCurrent | |
680 | // purpose : | |
681 | // ======================================================================= | |
682 | Standard_Boolean OpenGl_Context::MakeCurrent() | |
683 | { | |
da8bb41d | 684 | #if defined(HAVE_EGL) |
685 | if ((EGLDisplay )myDisplay == EGL_NO_DISPLAY | |
da8bb41d | 686 | || (EGLContext )myGContext == EGL_NO_CONTEXT) |
687 | { | |
688 | Standard_ProgramError_Raise_if (myIsInitialized, "OpenGl_Context::Init() should be called before!"); | |
689 | return Standard_False; | |
690 | } | |
691 | ||
692 | if (eglMakeCurrent ((EGLDisplay )myDisplay, (EGLSurface )myWindow, (EGLSurface )myWindow, (EGLContext )myGContext) != EGL_TRUE) | |
693 | { | |
694 | // if there is no current context it might be impossible to use glGetError() correctly | |
3b523c4c | 695 | PushMessage (GL_DEBUG_SOURCE_WINDOW_SYSTEM, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, |
da8bb41d | 696 | "eglMakeCurrent() has failed!"); |
697 | myIsInitialized = Standard_False; | |
698 | return Standard_False; | |
699 | } | |
700 | #elif defined(_WIN32) | |
86fa64d9 | 701 | if (myWindowDC == NULL || myGContext == NULL) |
2bd4c032 | 702 | { |
86fa64d9 | 703 | Standard_ProgramError_Raise_if (myIsInitialized, "OpenGl_Context::Init() should be called before!"); |
704 | return Standard_False; | |
705 | } | |
706 | ||
707 | // technically it should be safe to activate already bound GL context | |
708 | // however some drivers (Intel etc.) may FAIL doing this for unknown reason | |
709 | if (IsCurrent()) | |
710 | { | |
392ac980 | 711 | myShaderManager->SetContext (this); |
86fa64d9 | 712 | return Standard_True; |
713 | } | |
714 | else if (wglMakeCurrent ((HDC )myWindowDC, (HGLRC )myGContext) != TRUE) | |
715 | { | |
716 | // notice that glGetError() couldn't be used here! | |
717 | wchar_t* aMsgBuff = NULL; | |
718 | DWORD anErrorCode = GetLastError(); | |
719 | FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, | |
720 | NULL, anErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (wchar_t* )&aMsgBuff, 0, NULL); | |
cbf18624 | 721 | TCollection_ExtendedString aMsg ("wglMakeCurrent() has failed. "); |
86fa64d9 | 722 | if (aMsgBuff != NULL) |
723 | { | |
cbf18624 | 724 | aMsg += (Standard_ExtString )aMsgBuff; |
86fa64d9 | 725 | LocalFree (aMsgBuff); |
726 | } | |
3b523c4c | 727 | PushMessage (GL_DEBUG_SOURCE_WINDOW_SYSTEM, GL_DEBUG_TYPE_ERROR, (unsigned int )anErrorCode, GL_DEBUG_SEVERITY_HIGH, aMsg); |
fd4a6963 | 728 | myIsInitialized = Standard_False; |
2bd4c032 | 729 | return Standard_False; |
730 | } | |
731 | #else | |
86fa64d9 | 732 | if (myDisplay == NULL || myWindow == 0 || myGContext == 0) |
733 | { | |
734 | Standard_ProgramError_Raise_if (myIsInitialized, "OpenGl_Context::Init() should be called before!"); | |
735 | return Standard_False; | |
736 | } | |
737 | ||
738 | if (!glXMakeCurrent ((Display* )myDisplay, (GLXDrawable )myWindow, (GLXContext )myGContext)) | |
2bd4c032 | 739 | { |
740 | // if there is no current context it might be impossible to use glGetError() correctly | |
3b523c4c | 741 | PushMessage (GL_DEBUG_SOURCE_WINDOW_SYSTEM, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, |
cbf18624 | 742 | "glXMakeCurrent() has failed!"); |
fd4a6963 | 743 | myIsInitialized = Standard_False; |
2bd4c032 | 744 | return Standard_False; |
745 | } | |
746 | #endif | |
392ac980 | 747 | myShaderManager->SetContext (this); |
2bd4c032 | 748 | return Standard_True; |
749 | } | |
750 | ||
5e27df78 | 751 | // ======================================================================= |
752 | // function : SwapBuffers | |
753 | // purpose : | |
754 | // ======================================================================= | |
755 | void OpenGl_Context::SwapBuffers() | |
756 | { | |
da8bb41d | 757 | #if defined(HAVE_EGL) |
758 | if ((EGLSurface )myWindow != EGL_NO_SURFACE) | |
759 | { | |
760 | eglSwapBuffers ((EGLDisplay )myDisplay, (EGLSurface )myWindow); | |
761 | } | |
762 | #elif defined(_WIN32) | |
5e27df78 | 763 | if ((HDC )myWindowDC != NULL) |
764 | { | |
765 | ::SwapBuffers ((HDC )myWindowDC); | |
766 | glFlush(); | |
767 | } | |
768 | #else | |
769 | if ((Display* )myDisplay != NULL) | |
770 | { | |
771 | glXSwapBuffers ((Display* )myDisplay, (GLXDrawable )myWindow); | |
772 | } | |
773 | #endif | |
774 | } | |
775 | ||
4fe56619 | 776 | #endif // __APPLE__ |
777 | ||
f978241f | 778 | // ======================================================================= |
779 | // function : SetSwapInterval | |
780 | // purpose : | |
781 | // ======================================================================= | |
782 | Standard_Boolean OpenGl_Context::SetSwapInterval (const Standard_Integer theInterval) | |
783 | { | |
784 | #if defined(HAVE_EGL) | |
785 | if (::eglSwapInterval ((EGLDisplay )myDisplay, theInterval) == EGL_TRUE) | |
786 | { | |
787 | return Standard_True; | |
788 | } | |
789 | #elif defined(_WIN32) | |
790 | if (myFuncs->wglSwapIntervalEXT != NULL) | |
791 | { | |
792 | myFuncs->wglSwapIntervalEXT (theInterval); | |
793 | return Standard_True; | |
794 | } | |
795 | #elif defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE | |
92cc34d7 | 796 | (void )theInterval; // vsync cannot be turned OFF on iOS |
f978241f | 797 | #elif defined(__APPLE__) |
798 | if (::CGLSetParameter (CGLGetCurrentContext(), kCGLCPSwapInterval, &theInterval) == kCGLNoError) | |
799 | { | |
800 | return Standard_True; | |
801 | } | |
802 | #else | |
803 | if (theInterval == -1 | |
804 | && myFuncs->glXSwapIntervalEXT != NULL) | |
805 | { | |
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; | |
810 | } | |
811 | else if (myFuncs->glXSwapIntervalSGI != NULL) | |
812 | { | |
813 | myFuncs->glXSwapIntervalSGI (theInterval); | |
814 | return Standard_True; | |
815 | } | |
816 | #endif | |
817 | return Standard_False; | |
818 | } | |
819 | ||
5f8b738e | 820 | // ======================================================================= |
821 | // function : findProc | |
822 | // purpose : | |
823 | // ======================================================================= | |
824 | void* OpenGl_Context::findProc (const char* theFuncName) | |
825 | { | |
da8bb41d | 826 | #if defined(HAVE_EGL) |
827 | return (void* )eglGetProcAddress (theFuncName); | |
828 | #elif defined(_WIN32) | |
7c65581d | 829 | return (void* )wglGetProcAddress (theFuncName); |
5f8b738e | 830 | #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX) |
831 | return (myGlLibHandle != NULL) ? dlsym (myGlLibHandle, theFuncName) : NULL; | |
832 | #else | |
833 | return (void* )glXGetProcAddress ((const GLubyte* )theFuncName); | |
834 | #endif | |
2166f0fa SK |
835 | } |
836 | ||
837 | // ======================================================================= | |
838 | // function : CheckExtension | |
839 | // purpose : | |
840 | // ======================================================================= | |
2bd4c032 | 841 | Standard_Boolean OpenGl_Context::CheckExtension (const char* theExtName) const |
2166f0fa | 842 | { |
5f8b738e | 843 | if (theExtName == NULL) |
844 | { | |
0797d9d3 | 845 | #ifdef OCCT_DEBUG |
5f8b738e | 846 | std::cerr << "CheckExtension called with NULL string!\n"; |
63c629aa | 847 | #endif |
5f8b738e | 848 | return Standard_False; |
849 | } | |
59515ca6 | 850 | else if (caps->contextNoExtensions) |
851 | { | |
852 | return Standard_False; | |
853 | } | |
5f8b738e | 854 | |
4e1523ef | 855 | #if !defined(GL_ES_VERSION_2_0) |
5f8b738e | 856 | // available since OpenGL 3.0 |
857 | // and the ONLY way to check extensions with OpenGL 3.1+ core profile | |
4e1523ef | 858 | if (IsGlGreaterEqual (3, 0) |
859 | && myFuncs->glGetStringi != NULL) | |
5f8b738e | 860 | { |
861 | GLint anExtNb = 0; | |
4e1523ef | 862 | ::glGetIntegerv (GL_NUM_EXTENSIONS, &anExtNb); |
863 | const size_t anExtNameLen = strlen (theExtName); | |
5f8b738e | 864 | for (GLint anIter = 0; anIter < anExtNb; ++anIter) |
865 | { | |
4e1523ef | 866 | const char* anExtension = (const char* )myFuncs->glGetStringi (GL_EXTENSIONS, (GLuint )anIter); |
867 | const size_t aTestExtNameLen = strlen (anExtension); | |
868 | if (aTestExtNameLen == anExtNameLen | |
869 | && strncmp (anExtension, theExtName, anExtNameLen) == 0) | |
5f8b738e | 870 | { |
871 | return Standard_True; | |
872 | } | |
873 | } | |
874 | return Standard_False; | |
4e1523ef | 875 | } |
876 | #endif | |
5f8b738e | 877 | |
878 | // use old way with huge string for all extensions | |
879 | const char* anExtString = (const char* )glGetString (GL_EXTENSIONS); | |
880 | if (anExtString == NULL) | |
881 | { | |
7e7c2f0b | 882 | Messenger()->Send ("TKOpenGL: glGetString (GL_EXTENSIONS) has returned NULL! No GL context?", Message_Warning); |
2166f0fa SK |
883 | return Standard_False; |
884 | } | |
58655684 | 885 | return CheckExtension (anExtString, theExtName); |
886 | } | |
887 | ||
888 | // ======================================================================= | |
889 | // function : CheckExtension | |
890 | // purpose : | |
891 | // ======================================================================= | |
892 | Standard_Boolean OpenGl_Context::CheckExtension (const char* theExtString, | |
893 | const char* theExtName) | |
894 | { | |
895 | if (theExtString == NULL) | |
896 | { | |
897 | return Standard_False; | |
898 | } | |
2166f0fa SK |
899 | |
900 | // Search for theExtName in the extensions string. | |
901 | // Use of strstr() is not sufficient because extension names can be prefixes of other extension names. | |
58655684 | 902 | char* aPtrIter = (char* )theExtString; |
903 | const char* aPtrEnd = aPtrIter + strlen (theExtString); | |
904 | const size_t anExtNameLen = strlen (theExtName); | |
2166f0fa SK |
905 | while (aPtrIter < aPtrEnd) |
906 | { | |
6a7d83c4 | 907 | const size_t n = strcspn (aPtrIter, " "); |
5f8b738e | 908 | if ((n == anExtNameLen) && (strncmp (aPtrIter, theExtName, anExtNameLen) == 0)) |
909 | { | |
2166f0fa | 910 | return Standard_True; |
5f8b738e | 911 | } |
2166f0fa SK |
912 | aPtrIter += (n + 1); |
913 | } | |
914 | return Standard_False; | |
915 | } | |
916 | ||
4fe56619 | 917 | #if !defined(__APPLE__) || defined(MACOSX_USE_GLX) |
918 | ||
2166f0fa SK |
919 | // ======================================================================= |
920 | // function : Init | |
921 | // purpose : | |
922 | // ======================================================================= | |
4e1523ef | 923 | Standard_Boolean OpenGl_Context::Init (const Standard_Boolean theIsCoreProfile) |
2166f0fa | 924 | { |
2bd4c032 | 925 | if (myIsInitialized) |
5f8b738e | 926 | { |
f0430952 | 927 | return Standard_True; |
5f8b738e | 928 | } |
2bd4c032 | 929 | |
da8bb41d | 930 | #if defined(HAVE_EGL) |
931 | myDisplay = (Aspect_Display )eglGetCurrentDisplay(); | |
932 | myGContext = (Aspect_RenderingContext )eglGetCurrentContext(); | |
933 | myWindow = (Aspect_Drawable )eglGetCurrentSurface(EGL_DRAW); | |
934 | #elif defined(_WIN32) | |
2bd4c032 | 935 | myWindowDC = (Aspect_Handle )wglGetCurrentDC(); |
936 | myGContext = (Aspect_RenderingContext )wglGetCurrentContext(); | |
937 | #else | |
938 | myDisplay = (Aspect_Display )glXGetCurrentDisplay(); | |
939 | myGContext = (Aspect_RenderingContext )glXGetCurrentContext(); | |
940 | myWindow = (Aspect_Drawable )glXGetCurrentDrawable(); | |
941 | #endif | |
f0430952 | 942 | if (myGContext == NULL) |
943 | { | |
944 | return Standard_False; | |
945 | } | |
2bd4c032 | 946 | |
4e1523ef | 947 | init (theIsCoreProfile); |
2bd4c032 | 948 | myIsInitialized = Standard_True; |
f0430952 | 949 | return Standard_True; |
2bd4c032 | 950 | } |
951 | ||
4fe56619 | 952 | #endif // __APPLE__ |
953 | ||
2bd4c032 | 954 | // ======================================================================= |
955 | // function : Init | |
956 | // purpose : | |
957 | // ======================================================================= | |
da8bb41d | 958 | #if defined(HAVE_EGL) |
959 | Standard_Boolean OpenGl_Context::Init (const Aspect_Drawable theEglSurface, | |
960 | const Aspect_Display theEglDisplay, | |
4e1523ef | 961 | const Aspect_RenderingContext theEglContext, |
962 | const Standard_Boolean theIsCoreProfile) | |
da8bb41d | 963 | #elif defined(_WIN32) |
f0430952 | 964 | Standard_Boolean OpenGl_Context::Init (const Aspect_Handle theWindow, |
965 | const Aspect_Handle theWindowDC, | |
4e1523ef | 966 | const Aspect_RenderingContext theGContext, |
967 | const Standard_Boolean theIsCoreProfile) | |
4fe56619 | 968 | #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX) |
a2e4f780 | 969 | |
970 | #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE | |
971 | Standard_Boolean OpenGl_Context::Init (EAGLContext* theGContext, | |
4e1523ef | 972 | const Standard_Boolean theIsCoreProfile) |
a2e4f780 | 973 | #else |
974 | Standard_Boolean OpenGl_Context::Init (NSOpenGLContext* theGContext, | |
975 | const Standard_Boolean theIsCoreProfile) | |
976 | #endif | |
977 | ||
2bd4c032 | 978 | #else |
f0430952 | 979 | Standard_Boolean OpenGl_Context::Init (const Aspect_Drawable theWindow, |
980 | const Aspect_Display theDisplay, | |
4e1523ef | 981 | const Aspect_RenderingContext theGContext, |
982 | const Standard_Boolean theIsCoreProfile) | |
2bd4c032 | 983 | #endif |
984 | { | |
985 | Standard_ProgramError_Raise_if (myIsInitialized, "OpenGl_Context::Init() should be called only once!"); | |
da8bb41d | 986 | #if defined(HAVE_EGL) |
987 | myWindow = theEglSurface; | |
988 | myGContext = theEglContext; | |
989 | myDisplay = theEglDisplay; | |
990 | #elif defined(_WIN32) | |
2bd4c032 | 991 | myWindow = theWindow; |
992 | myGContext = theGContext; | |
2bd4c032 | 993 | myWindowDC = theWindowDC; |
4fe56619 | 994 | #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX) |
a2e4f780 | 995 | myGContext = theGContext; |
2bd4c032 | 996 | #else |
4fe56619 | 997 | myWindow = theWindow; |
998 | myGContext = theGContext; | |
2bd4c032 | 999 | myDisplay = theDisplay; |
1000 | #endif | |
86fa64d9 | 1001 | if (myGContext == NULL || !MakeCurrent()) |
f0430952 | 1002 | { |
1003 | return Standard_False; | |
1004 | } | |
2bd4c032 | 1005 | |
4e1523ef | 1006 | init (theIsCoreProfile); |
2bd4c032 | 1007 | myIsInitialized = Standard_True; |
f0430952 | 1008 | return Standard_True; |
5f8b738e | 1009 | } |
1010 | ||
a46ab511 | 1011 | // ======================================================================= |
1012 | // function : FormatGlEnumHex | |
1013 | // purpose : | |
1014 | // ======================================================================= | |
1015 | TCollection_AsciiString OpenGl_Context::FormatGlEnumHex (int theGlEnum) | |
1016 | { | |
1017 | char aBuff[16]; | |
1018 | Sprintf (aBuff, theGlEnum < (int )std::numeric_limits<uint16_t>::max() | |
1019 | ? "0x%04X" | |
1020 | : "0x%08X", theGlEnum); | |
1021 | return aBuff; | |
1022 | } | |
1023 | ||
1024 | // ======================================================================= | |
1025 | // function : FormatSize | |
1026 | // purpose : | |
1027 | // ======================================================================= | |
1028 | TCollection_AsciiString OpenGl_Context::FormatSize (Standard_Size theSize) | |
1029 | { | |
1030 | char aBuff[32]; | |
1031 | Sprintf (aBuff, "%" PRIu64, (uint64_t )theSize); | |
1032 | return aBuff; | |
1033 | } | |
1034 | ||
1035 | // ======================================================================= | |
1036 | // function : FormatPointer | |
1037 | // purpose : | |
1038 | // ======================================================================= | |
1039 | TCollection_AsciiString OpenGl_Context::FormatPointer (const void* thePtr) | |
1040 | { | |
1041 | char aBuff[32]; | |
1042 | Sprintf (aBuff, "0x%" PRIXPTR, (uintptr_t )thePtr); | |
1043 | return aBuff; | |
1044 | } | |
1045 | ||
1046 | // ======================================================================= | |
1047 | // function : FormatGlError | |
1048 | // purpose : | |
1049 | // ======================================================================= | |
1050 | TCollection_AsciiString OpenGl_Context::FormatGlError (int theGlError) | |
1051 | { | |
1052 | switch (theGlError) | |
1053 | { | |
1054 | case GL_INVALID_ENUM: return "GL_INVALID_ENUM"; | |
1055 | case GL_INVALID_VALUE: return "GL_INVALID_VALUE"; | |
1056 | case GL_INVALID_OPERATION: return "GL_INVALID_OPERATION"; | |
1057 | #ifdef GL_STACK_OVERFLOW | |
1058 | case GL_STACK_OVERFLOW: return "GL_STACK_OVERFLOW"; | |
1059 | case GL_STACK_UNDERFLOW: return "GL_STACK_UNDERFLOW"; | |
1060 | #endif | |
1061 | case GL_OUT_OF_MEMORY: return "GL_OUT_OF_MEMORY"; | |
1062 | case GL_INVALID_FRAMEBUFFER_OPERATION: return "GL_INVALID_FRAMEBUFFER_OPERATION"; | |
1063 | } | |
1064 | return FormatGlEnumHex (theGlError); | |
1065 | } | |
1066 | ||
5f8b738e | 1067 | // ======================================================================= |
1068 | // function : ResetErrors | |
1069 | // purpose : | |
1070 | // ======================================================================= | |
f9f740d6 | 1071 | bool OpenGl_Context::ResetErrors (const bool theToPrintErrors) |
5f8b738e | 1072 | { |
3c4b62a4 | 1073 | int aPrevErr = 0; |
1074 | int anErr = ::glGetError(); | |
f9f740d6 | 1075 | const bool hasError = anErr != GL_NO_ERROR; |
3c4b62a4 | 1076 | if (!theToPrintErrors) |
5f8b738e | 1077 | { |
3c4b62a4 | 1078 | for (; anErr != GL_NO_ERROR && aPrevErr != anErr; aPrevErr = anErr, anErr = ::glGetError()) |
1079 | { | |
1080 | // | |
1081 | } | |
f9f740d6 | 1082 | return hasError; |
3c4b62a4 | 1083 | } |
1084 | ||
1085 | for (; anErr != GL_NO_ERROR && aPrevErr != anErr; aPrevErr = anErr, anErr = ::glGetError()) | |
1086 | { | |
a46ab511 | 1087 | const TCollection_ExtendedString aMsg = TCollection_ExtendedString ("Unhandled GL error: ") + FormatGlError (anErr); |
1088 | PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_OTHER, 0, GL_DEBUG_SEVERITY_LOW, aMsg); | |
1089 | } | |
1090 | return hasError; | |
1091 | } | |
1092 | ||
1093 | // ======================================================================= | |
1094 | // function : debugPrintError | |
1095 | // purpose : | |
1096 | // ======================================================================= | |
1097 | bool OpenGl_GlFunctions::debugPrintError (const char* theName) const | |
1098 | { | |
1099 | const int anErr = ::glGetError(); | |
1100 | if (anErr != GL_NO_ERROR) | |
1101 | { | |
1102 | Message::SendFail() << theName << "(), unhandled GL error: " << OpenGl_Context::FormatGlError (anErr); | |
1103 | // there is no glSetError(), just emulate non-clear state | |
3c4b62a4 | 1104 | switch (anErr) |
1105 | { | |
a46ab511 | 1106 | case GL_INVALID_VALUE: |
1107 | { | |
1108 | ::glLineWidth(-1.0f); | |
1109 | ::glLineWidth( 1.0f); | |
3c4b62a4 | 1110 | break; |
a46ab511 | 1111 | } |
3c4b62a4 | 1112 | default: |
a46ab511 | 1113 | case GL_INVALID_ENUM: |
1114 | { | |
1115 | ::glEnable (0xFFFF); | |
3c4b62a4 | 1116 | break; |
a46ab511 | 1117 | } |
3c4b62a4 | 1118 | } |
5f8b738e | 1119 | } |
a46ab511 | 1120 | return anErr != GL_NO_ERROR; |
5f8b738e | 1121 | } |
1122 | ||
1123 | // ======================================================================= | |
4e1523ef | 1124 | // function : ReadGlVersion |
5f8b738e | 1125 | // purpose : |
1126 | // ======================================================================= | |
4e1523ef | 1127 | void OpenGl_Context::ReadGlVersion (Standard_Integer& theGlVerMajor, |
1128 | Standard_Integer& theGlVerMinor) | |
5f8b738e | 1129 | { |
1130 | // reset values | |
4e1523ef | 1131 | theGlVerMajor = 0; |
1132 | theGlVerMinor = 0; | |
5f8b738e | 1133 | |
c64efd9e | 1134 | bool toCheckVer3 = true; |
1135 | #if defined(__EMSCRIPTEN__) | |
1136 | // WebGL 1.0 prints annoying invalid enumeration warnings to console. | |
1137 | toCheckVer3 = false; | |
1138 | if (EMSCRIPTEN_WEBGL_CONTEXT_HANDLE aWebGlCtx = emscripten_webgl_get_current_context()) | |
5f8b738e | 1139 | { |
c64efd9e | 1140 | EmscriptenWebGLContextAttributes anAttribs = {}; |
1141 | if (emscripten_webgl_get_context_attributes (aWebGlCtx, &anAttribs) == EMSCRIPTEN_RESULT_SUCCESS) | |
1142 | { | |
1143 | toCheckVer3 = anAttribs.majorVersion >= 2; | |
1144 | } | |
5f8b738e | 1145 | } |
c64efd9e | 1146 | #endif |
1147 | ||
1148 | // Available since OpenGL 3.0 and OpenGL ES 3.0. | |
1149 | if (toCheckVer3) | |
4e1523ef | 1150 | { |
c64efd9e | 1151 | GLint aMajor = 0, aMinor = 0; |
1152 | glGetIntegerv (GL_MAJOR_VERSION, &aMajor); | |
1153 | glGetIntegerv (GL_MINOR_VERSION, &aMinor); | |
1154 | // glGetError() sometimes does not report an error here even if | |
1155 | // GL does not know GL_MAJOR_VERSION and GL_MINOR_VERSION constants. | |
1156 | // This happens on some renderers like e.g. Cygwin MESA. | |
1157 | // Thus checking additionally if GL has put anything to | |
1158 | // the output variables. | |
1159 | if (::glGetError() == GL_NO_ERROR && aMajor != 0 && aMinor != 0) | |
4e1523ef | 1160 | { |
c64efd9e | 1161 | theGlVerMajor = aMajor; |
1162 | theGlVerMinor = aMinor; | |
1163 | return; | |
1164 | } | |
1165 | for (GLenum anErr = ::glGetError(), aPrevErr = GL_NO_ERROR;; aPrevErr = anErr, anErr = ::glGetError()) | |
1166 | { | |
1167 | if (anErr == GL_NO_ERROR | |
1168 | || anErr == aPrevErr) | |
1169 | { | |
1170 | break; | |
1171 | } | |
4e1523ef | 1172 | } |
1173 | } | |
5f8b738e | 1174 | |
1175 | // Read version string. | |
ca3c13d1 | 1176 | // Notice that only first two numbers split by point '2.1 XXXXX' are significant. |
5f8b738e | 1177 | // Following trash (after space) is vendor-specific. |
1178 | // New drivers also returns micro version of GL like '3.3.0' which has no meaning | |
1179 | // and should be considered as vendor-specific too. | |
1180 | const char* aVerStr = (const char* )glGetString (GL_VERSION); | |
1181 | if (aVerStr == NULL || *aVerStr == '\0') | |
1182 | { | |
1183 | // invalid GL context | |
1184 | return; | |
1185 | } | |
1186 | ||
ca3c13d1 | 1187 | //#if defined(GL_ES_VERSION_2_0) |
1188 | // skip "OpenGL ES-** " section | |
1189 | for (; *aVerStr != '\0'; ++aVerStr) | |
1190 | { | |
1191 | if (*aVerStr >= '0' && *aVerStr <= '9') | |
1192 | { | |
1193 | break; | |
1194 | } | |
1195 | } | |
1196 | //#endif | |
1197 | ||
5f8b738e | 1198 | // parse string for major number |
1199 | char aMajorStr[32]; | |
1200 | char aMinorStr[32]; | |
1201 | size_t aMajIter = 0; | |
1202 | while (aVerStr[aMajIter] >= '0' && aVerStr[aMajIter] <= '9') | |
1203 | { | |
1204 | ++aMajIter; | |
1205 | } | |
1206 | if (aMajIter == 0 || aMajIter >= sizeof(aMajorStr)) | |
1207 | { | |
1208 | return; | |
1209 | } | |
1210 | memcpy (aMajorStr, aVerStr, aMajIter); | |
1211 | aMajorStr[aMajIter] = '\0'; | |
1212 | ||
1213 | // parse string for minor number | |
86325709 | 1214 | aVerStr += aMajIter + 1; |
1215 | size_t aMinIter = 0; | |
5f8b738e | 1216 | while (aVerStr[aMinIter] >= '0' && aVerStr[aMinIter] <= '9') |
1217 | { | |
1218 | ++aMinIter; | |
1219 | } | |
86325709 | 1220 | if (aMinIter == 0 || aMinIter >= sizeof(aMinorStr)) |
5f8b738e | 1221 | { |
1222 | return; | |
1223 | } | |
86325709 | 1224 | memcpy (aMinorStr, aVerStr, aMinIter); |
1225 | aMinorStr[aMinIter] = '\0'; | |
5f8b738e | 1226 | |
1227 | // read numbers | |
4e1523ef | 1228 | theGlVerMajor = atoi (aMajorStr); |
1229 | theGlVerMinor = atoi (aMinorStr); | |
8f8fe4a9 | 1230 | #if defined(__EMSCRIPTEN__) |
1231 | if (theGlVerMajor >= 3) | |
1232 | { | |
1233 | if (!toCheckVer3 | |
1234 | || ::strstr (aVerStr, "WebGL 1.0") != NULL) | |
1235 | { | |
1236 | Message::SendWarning() << "Warning! OpenGL context reports version " << theGlVerMajor << "." << theGlVerMinor | |
1237 | << " but WebGL 2.0 was unavailable\n" | |
1238 | << "Fallback to OpenGL ES 2.0 will be used instead of reported version"; | |
1239 | theGlVerMajor = 2; | |
1240 | theGlVerMinor = 0; | |
1241 | } | |
1242 | } | |
1243 | #endif | |
5f8b738e | 1244 | |
4e1523ef | 1245 | if (theGlVerMajor <= 0) |
5f8b738e | 1246 | { |
4e1523ef | 1247 | theGlVerMajor = 0; |
1248 | theGlVerMinor = 0; | |
5f8b738e | 1249 | } |
1250 | } | |
1251 | ||
58655684 | 1252 | static Standard_CString THE_DBGMSG_UNKNOWN = "UNKNOWN"; |
1253 | static Standard_CString THE_DBGMSG_SOURCES[] = | |
1254 | { | |
3b523c4c | 1255 | ".OpenGL", // GL_DEBUG_SOURCE_API |
1256 | ".WinSystem", // GL_DEBUG_SOURCE_WINDOW_SYSTEM | |
1257 | ".GLSL", // GL_DEBUG_SOURCE_SHADER_COMPILER | |
1258 | ".3rdParty", // GL_DEBUG_SOURCE_THIRD_PARTY | |
1259 | "", // GL_DEBUG_SOURCE_APPLICATION | |
1260 | ".Other" // GL_DEBUG_SOURCE_OTHER | |
58655684 | 1261 | }; |
1262 | ||
1263 | static Standard_CString THE_DBGMSG_TYPES[] = | |
1264 | { | |
3b523c4c | 1265 | "Error", // GL_DEBUG_TYPE_ERROR |
1266 | "Deprecated", // GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR | |
1267 | "Undef. behavior", // GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR | |
1268 | "Portability", // GL_DEBUG_TYPE_PORTABILITY | |
1269 | "Performance", // GL_DEBUG_TYPE_PERFORMANCE | |
1270 | "Other" // GL_DEBUG_TYPE_OTHER | |
58655684 | 1271 | }; |
1272 | ||
3b523c4c | 1273 | static Standard_CString THE_DBGMSG_SEV_HIGH = "High"; // GL_DEBUG_SEVERITY_HIGH |
1274 | static Standard_CString THE_DBGMSG_SEV_MEDIUM = "Medium"; // GL_DEBUG_SEVERITY_MEDIUM | |
1275 | static Standard_CString THE_DBGMSG_SEV_LOW = "Low"; // GL_DEBUG_SEVERITY_LOW | |
58655684 | 1276 | |
cbf18624 | 1277 | //! Callback for GL_ARB_debug_output extension |
58655684 | 1278 | static void APIENTRY debugCallbackWrap(unsigned int theSource, |
1279 | unsigned int theType, | |
1280 | unsigned int theId, | |
1281 | unsigned int theSeverity, | |
1282 | int /*theLength*/, | |
1283 | const char* theMessage, | |
9293178b | 1284 | const void* theUserParam) |
cbf18624 | 1285 | { |
1286 | OpenGl_Context* aCtx = (OpenGl_Context* )theUserParam; | |
1287 | aCtx->PushMessage (theSource, theType, theId, theSeverity, theMessage); | |
1288 | } | |
1289 | ||
1290 | // ======================================================================= | |
1291 | // function : PushMessage | |
1292 | // purpose : | |
1293 | // ======================================================================= | |
1294 | void OpenGl_Context::PushMessage (const unsigned int theSource, | |
1295 | const unsigned int theType, | |
1296 | const unsigned int theId, | |
1297 | const unsigned int theSeverity, | |
1298 | const TCollection_ExtendedString& theMessage) | |
58655684 | 1299 | { |
c87535af | 1300 | if (caps->suppressExtraMsg |
3b523c4c | 1301 | && theSource >= GL_DEBUG_SOURCE_API |
1302 | && theSource <= GL_DEBUG_SOURCE_OTHER | |
1303 | && myFilters[theSource - GL_DEBUG_SOURCE_API].Contains (theId)) | |
c87535af | 1304 | { |
1305 | return; | |
1306 | } | |
1307 | ||
3b523c4c | 1308 | Standard_CString& aSrc = (theSource >= GL_DEBUG_SOURCE_API |
1309 | && theSource <= GL_DEBUG_SOURCE_OTHER) | |
1310 | ? THE_DBGMSG_SOURCES[theSource - GL_DEBUG_SOURCE_API] | |
58655684 | 1311 | : THE_DBGMSG_UNKNOWN; |
3b523c4c | 1312 | Standard_CString& aType = (theType >= GL_DEBUG_TYPE_ERROR |
1313 | && theType <= GL_DEBUG_TYPE_OTHER) | |
1314 | ? THE_DBGMSG_TYPES[theType - GL_DEBUG_TYPE_ERROR] | |
58655684 | 1315 | : THE_DBGMSG_UNKNOWN; |
3b523c4c | 1316 | Standard_CString& aSev = theSeverity == GL_DEBUG_SEVERITY_HIGH |
58655684 | 1317 | ? THE_DBGMSG_SEV_HIGH |
3b523c4c | 1318 | : (theSeverity == GL_DEBUG_SEVERITY_MEDIUM |
58655684 | 1319 | ? THE_DBGMSG_SEV_MEDIUM |
1320 | : THE_DBGMSG_SEV_LOW); | |
3b523c4c | 1321 | Message_Gravity aGrav = theSeverity == GL_DEBUG_SEVERITY_HIGH |
cbf18624 | 1322 | ? Message_Alarm |
3b523c4c | 1323 | : (theSeverity == GL_DEBUG_SEVERITY_MEDIUM |
cbf18624 | 1324 | ? Message_Warning |
1325 | : Message_Info); | |
1326 | ||
1327 | TCollection_ExtendedString aMsg; | |
1328 | aMsg += "TKOpenGl"; aMsg += aSrc; | |
1329 | aMsg += " | Type: "; aMsg += aType; | |
1330 | aMsg += " | ID: "; aMsg += (Standard_Integer )theId; | |
1331 | aMsg += " | Severity: "; aMsg += aSev; | |
1332 | aMsg += " | Message:\n "; | |
1333 | aMsg += theMessage; | |
7e7c2f0b | 1334 | Messenger()->Send (aMsg, aGrav); |
58655684 | 1335 | } |
1336 | ||
c87535af | 1337 | // ======================================================================= |
1338 | // function : ExcludeMessage | |
1339 | // purpose : | |
1340 | // ====================================================================== | |
1341 | Standard_Boolean OpenGl_Context::ExcludeMessage (const unsigned int theSource, | |
1342 | const unsigned int theId) | |
1343 | { | |
3b523c4c | 1344 | return theSource >= GL_DEBUG_SOURCE_API |
1345 | && theSource <= GL_DEBUG_SOURCE_OTHER | |
1346 | && myFilters[theSource - GL_DEBUG_SOURCE_API].Add (theId); | |
c87535af | 1347 | } |
1348 | ||
1349 | // ======================================================================= | |
1350 | // function : IncludeMessage | |
1351 | // purpose : | |
1352 | // ====================================================================== | |
1353 | Standard_Boolean OpenGl_Context::IncludeMessage (const unsigned int theSource, | |
1354 | const unsigned int theId) | |
1355 | { | |
3b523c4c | 1356 | return theSource >= GL_DEBUG_SOURCE_API |
1357 | && theSource <= GL_DEBUG_SOURCE_OTHER | |
1358 | && myFilters[theSource - GL_DEBUG_SOURCE_API].Remove (theId); | |
c87535af | 1359 | } |
1360 | ||
ee51a9fe | 1361 | // ======================================================================= |
1362 | // function : checkWrongVersion | |
1363 | // purpose : | |
1364 | // ====================================================================== | |
0ae9ac21 | 1365 | void OpenGl_Context::checkWrongVersion (Standard_Integer theGlVerMajor, Standard_Integer theGlVerMinor, |
1366 | const char* theLastFailedProc) | |
ee51a9fe | 1367 | { |
1368 | if (!IsGlGreaterEqual (theGlVerMajor, theGlVerMinor)) | |
1369 | { | |
1370 | return; | |
1371 | } | |
1372 | ||
0ae9ac21 | 1373 | PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, |
1374 | TCollection_AsciiString() | |
1375 | + "Error! OpenGL context reports version " | |
1376 | + myGlVerMajor + "." + myGlVerMinor | |
1377 | + " but does not export required functions for " + theGlVerMajor + "." + theGlVerMinor | |
1378 | + " (" + (theLastFailedProc != NULL ? theLastFailedProc : "") + ")\n" | |
1379 | + "Please report this issue to OpenGL driver vendor '" + myVendor + "'"); | |
1380 | ||
1381 | // lower internal version | |
1382 | if (theGlVerMinor > 0) | |
1383 | { | |
1384 | myGlVerMajor = theGlVerMajor; | |
1385 | myGlVerMinor = theGlVerMinor - 1; | |
1386 | return; | |
1387 | } | |
1388 | #if defined(GL_ES_VERSION_2_0) | |
1389 | switch (theGlVerMajor) | |
1390 | { | |
1391 | case 3: myGlVerMajor = 2; myGlVerMinor = 0; return; | |
1392 | } | |
1393 | #else | |
1394 | switch (theGlVerMajor) | |
1395 | { | |
1396 | case 2: myGlVerMajor = 1; myGlVerMinor = 5; return; | |
1397 | case 3: myGlVerMajor = 2; myGlVerMinor = 1; return; | |
1398 | case 4: myGlVerMajor = 3; myGlVerMinor = 3; return; | |
1399 | } | |
1400 | #endif | |
ee51a9fe | 1401 | } |
1402 | ||
5f8b738e | 1403 | // ======================================================================= |
1404 | // function : init | |
1405 | // purpose : | |
1406 | // ======================================================================= | |
4e1523ef | 1407 | void OpenGl_Context::init (const Standard_Boolean theIsCoreProfile) |
5f8b738e | 1408 | { |
1409 | // read version | |
4e1523ef | 1410 | myGlVerMajor = 0; |
1411 | myGlVerMinor = 0; | |
3c4b62a4 | 1412 | myMaxMsaaSamples = 0; |
a1073ae2 | 1413 | myMaxDrawBuffers = 1; |
1414 | myMaxColorAttachments = 1; | |
e44b849d | 1415 | myDefaultVao = 0; |
4e1523ef | 1416 | ReadGlVersion (myGlVerMajor, myGlVerMinor); |
c87535af | 1417 | myVendor = (const char* )::glGetString (GL_VENDOR); |
6997ff1c | 1418 | myVendor.LowerCase(); |
faff3767 | 1419 | mySupportedFormats->Clear(); |
faff3767 | 1420 | |
59515ca6 | 1421 | if (caps->contextMajorVersionUpper != -1) |
1422 | { | |
1423 | // synthetically restrict OpenGL version for testing | |
1424 | Standard_Integer aCtxVer[2] = { myGlVerMajor, myGlVerMinor }; | |
1425 | bool isLowered = false; | |
1426 | if (myGlVerMajor > caps->contextMajorVersionUpper) | |
1427 | { | |
1428 | isLowered = true; | |
1429 | myGlVerMajor = caps->contextMajorVersionUpper; | |
1430 | #if defined(GL_ES_VERSION_2_0) | |
1431 | switch (myGlVerMajor) | |
1432 | { | |
1433 | case 2: myGlVerMinor = 0; break; | |
1434 | } | |
1435 | #else | |
1436 | switch (myGlVerMajor) | |
1437 | { | |
1438 | case 1: myGlVerMinor = 5; break; | |
1439 | case 2: myGlVerMinor = 1; break; | |
1440 | case 3: myGlVerMinor = 3; break; | |
1441 | } | |
1442 | #endif | |
1443 | } | |
1444 | if (caps->contextMinorVersionUpper != -1 | |
1445 | && myGlVerMinor > caps->contextMinorVersionUpper) | |
1446 | { | |
1447 | isLowered = true; | |
1448 | myGlVerMinor = caps->contextMinorVersionUpper; | |
1449 | } | |
1450 | if (isLowered) | |
1451 | { | |
1452 | PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_MEDIUM, | |
1453 | TCollection_AsciiString ("OpenGL version ") + aCtxVer[0] + "." + aCtxVer[1] | |
1454 | + " has been lowered to " + myGlVerMajor + "." + myGlVerMinor); | |
1455 | } | |
1456 | } | |
1457 | ||
be375252 | 1458 | if (!caps->ffpEnable |
1459 | && !IsGlGreaterEqual (2, 0)) | |
1460 | { | |
1461 | caps->ffpEnable = true; | |
1462 | TCollection_ExtendedString aMsg = | |
1463 | TCollection_ExtendedString("OpenGL driver is too old! Context info:\n") | |
1464 | + " Vendor: " + (const char* )::glGetString (GL_VENDOR) + "\n" | |
1465 | + " Renderer: " + (const char* )::glGetString (GL_RENDERER) + "\n" | |
1466 | + " Version: " + (const char* )::glGetString (GL_VERSION) + "\n" | |
1467 | + " Fallback using deprecated fixed-function pipeline.\n" | |
1468 | + " Visualization might work incorrectly.\n" | |
1469 | " Consider upgrading the graphics driver."; | |
1470 | PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_HIGH, aMsg); | |
1471 | } | |
4e1523ef | 1472 | |
1473 | #if defined(GL_ES_VERSION_2_0) | |
1474 | (void )theIsCoreProfile; | |
1475 | const bool isCoreProfile = false; | |
1476 | #else | |
c87535af | 1477 | |
6997ff1c | 1478 | if (myVendor.Search ("nvidia") != -1) |
c87535af | 1479 | { |
1480 | // Buffer detailed info: Buffer object 1 (bound to GL_ARRAY_BUFFER_ARB, usage hint is GL_STATIC_DRAW) | |
1481 | // will use VIDEO memory as the source for buffer object operations. | |
3b523c4c | 1482 | ExcludeMessage (GL_DEBUG_SOURCE_API, 131185); |
c87535af | 1483 | } |
4e1523ef | 1484 | if (IsGlGreaterEqual (3, 0)) |
1485 | { | |
1486 | // retrieve auxiliary function in advance | |
1487 | FindProc ("glGetStringi", myFuncs->glGetStringi); | |
1488 | } | |
1489 | ||
d4271fe5 | 1490 | bool isCoreProfile = false; |
4e1523ef | 1491 | if (IsGlGreaterEqual (3, 2)) |
1492 | { | |
d4271fe5 | 1493 | isCoreProfile = (theIsCoreProfile == Standard_True); |
4e1523ef | 1494 | |
d4271fe5 | 1495 | // detect Core profile |
c6ad5e5f | 1496 | if (!isCoreProfile) |
4e1523ef | 1497 | { |
d4271fe5 | 1498 | GLint aProfile = 0; |
1499 | ::glGetIntegerv (GL_CONTEXT_PROFILE_MASK, &aProfile); | |
1500 | isCoreProfile = (aProfile & GL_CONTEXT_CORE_PROFILE_BIT) != 0; | |
4e1523ef | 1501 | } |
1502 | } | |
4e1523ef | 1503 | #endif |
1504 | ||
e44b849d | 1505 | myFuncs->load (*this, isCoreProfile); |
872f98d9 | 1506 | |
4bf072e4 | 1507 | // setup shader generator |
1508 | myShaderManager->SetGapiVersion (myGlVerMajor, myGlVerMinor); | |
1509 | myShaderManager->SetEmulateDepthClamp (!arbDepthClamp); | |
1510 | ||
1511 | bool toReverseDFdxSign = false; | |
1512 | #if defined(GL_ES_VERSION_2_0) | |
1513 | // workaround Adreno driver bug computing reversed normal using dFdx/dFdy | |
1514 | toReverseDFdxSign = myVendor.Search("qualcomm") != -1; | |
1515 | #endif | |
1516 | myShaderManager->SetFlatShading (hasFlatShading != OpenGl_FeatureNotAvailable, toReverseDFdxSign); | |
1517 | #if defined(GL_ES_VERSION_2_0) | |
1518 | myShaderManager->SetUseRedAlpha (false); | |
1519 | #else | |
43eddb47 | 1520 | myShaderManager->SetUseRedAlpha (core11ffp == NULL); |
4bf072e4 | 1521 | #endif |
1522 | #define checkGlslExtensionShort(theName) myShaderManager->EnableGlslExtension (Graphic3d_GlslExtension_ ## theName, CheckExtension (#theName)) | |
1523 | #if defined(GL_ES_VERSION_2_0) | |
1524 | checkGlslExtensionShort(GL_OES_standard_derivatives); | |
1525 | checkGlslExtensionShort(GL_EXT_shader_texture_lod); | |
1526 | checkGlslExtensionShort(GL_EXT_frag_depth); | |
1527 | #else | |
1528 | checkGlslExtensionShort(GL_EXT_gpu_shader4); | |
1529 | #endif | |
1530 | ||
0deb6f04 | 1531 | // initialize debug context extension |
e44b849d | 1532 | if (arbDbg != NULL |
1533 | && caps->contextDebug) | |
1534 | { | |
1535 | // setup default callback | |
1536 | myIsGlDebugCtx = Standard_True; | |
1537 | arbDbg->glDebugMessageCallback (debugCallbackWrap, this); | |
1538 | #if defined(GL_ES_VERSION_2_0) | |
1539 | ::glEnable (GL_DEBUG_OUTPUT); | |
1540 | #else | |
1541 | if (core43 != NULL) | |
872f98d9 | 1542 | { |
e44b849d | 1543 | ::glEnable (GL_DEBUG_OUTPUT); |
872f98d9 | 1544 | } |
e44b849d | 1545 | #endif |
1546 | if (caps->contextSyncDebug) | |
0deb6f04 | 1547 | { |
e44b849d | 1548 | // note that some broken implementations (e.g. simulators) might generate error message on this call |
1549 | ::glEnable (GL_DEBUG_OUTPUT_SYNCHRONOUS); | |
0deb6f04 | 1550 | } |
1551 | } | |
1552 | ||
a1073ae2 | 1553 | if (hasDrawBuffers) |
1554 | { | |
1555 | glGetIntegerv (GL_MAX_DRAW_BUFFERS, &myMaxDrawBuffers); | |
1556 | glGetIntegerv (GL_MAX_COLOR_ATTACHMENTS, &myMaxColorAttachments); | |
c3487460 | 1557 | if (myDrawBuffers.Length() < myMaxDrawBuffers) |
1558 | { | |
1559 | myDrawBuffers.Resize (0, myMaxDrawBuffers - 1, false); | |
1560 | } | |
a1073ae2 | 1561 | } |
1562 | ||
ca3c13d1 | 1563 | glGetIntegerv (GL_MAX_TEXTURE_SIZE, &myMaxTexDim); |
737e9a8d | 1564 | #if !defined(GL_ES_VERSION_2_0) |
43eddb47 | 1565 | if (IsGlGreaterEqual (1, 3) && core11ffp != NULL) |
737e9a8d | 1566 | { |
1567 | // this is a maximum of texture units for FFP functionality, | |
5c225e8e | 1568 | // usually smaller than combined texture units available for GLSL |
1569 | glGetIntegerv (GL_MAX_TEXTURE_UNITS, &myMaxTexUnitsFFP); | |
1570 | myMaxTexCombined = myMaxTexUnitsFFP; | |
737e9a8d | 1571 | } |
1572 | #endif | |
5c225e8e | 1573 | if (IsGlGreaterEqual (2, 0)) |
1574 | { | |
1575 | glGetIntegerv (GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &myMaxTexCombined); | |
1576 | } | |
737e9a8d | 1577 | mySpriteTexUnit = myMaxTexCombined >= 2 |
72f6dc61 | 1578 | ? Graphic3d_TextureUnit_PointSprite |
737e9a8d | 1579 | : Graphic3d_TextureUnit_0; |
ca3c13d1 | 1580 | |
6997ff1c | 1581 | GLint aMaxVPortSize[2] = {0, 0}; |
1582 | glGetIntegerv (GL_MAX_VIEWPORT_DIMS, aMaxVPortSize); | |
1583 | myMaxDumpSizeX = Min (aMaxVPortSize[0], myMaxTexDim); | |
1584 | myMaxDumpSizeY = Min (aMaxVPortSize[1], myMaxTexDim); | |
1585 | if (myVendor == "intel") | |
1586 | { | |
1587 | // Intel drivers have known bug with empty dump for images with width>=5462 | |
1588 | myMaxDumpSizeX = Min (myMaxDumpSizeX, 4096); | |
1589 | } | |
1590 | ||
bf75be98 | 1591 | if (extAnis) |
1592 | { | |
1593 | glGetIntegerv (GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &myAnisoMax); | |
1594 | } | |
f0430952 | 1595 | |
25c35042 | 1596 | myClippingState.Init(); |
4269bd1b | 1597 | |
e44b849d | 1598 | #if defined(GL_ES_VERSION_2_0) |
1599 | if (IsGlGreaterEqual (3, 1) | |
1600 | && myFuncs->glTexStorage2DMultisample != NULL) | |
0ae9ac21 | 1601 | { |
e44b849d | 1602 | // MSAA RenderBuffers have been defined in OpenGL ES 3.0, |
1603 | // but MSAA Textures - only in OpenGL ES 3.1+ | |
0ae9ac21 | 1604 | ::glGetIntegerv (GL_MAX_SAMPLES, &myMaxMsaaSamples); |
1605 | } | |
e44b849d | 1606 | #else |
43eddb47 | 1607 | if (core30 != NULL) |
0deb6f04 | 1608 | { |
e44b849d | 1609 | // MSAA RenderBuffers have been defined in OpenGL 3.0, |
1610 | // but MSAA Textures - only in OpenGL 3.2+ | |
1611 | if (core32 != NULL) | |
0deb6f04 | 1612 | { |
e44b849d | 1613 | ::glGetIntegerv (GL_MAX_SAMPLES, &myMaxMsaaSamples); |
0deb6f04 | 1614 | } |
e44b849d | 1615 | else if (CheckExtension ("GL_ARB_texture_multisample") |
1616 | && myFuncs->glTexImage2DMultisample != NULL) | |
0deb6f04 | 1617 | { |
e44b849d | 1618 | GLint aNbColorSamples = 0, aNbDepthSamples = 0; |
1619 | ::glGetIntegerv (GL_MAX_COLOR_TEXTURE_SAMPLES, &aNbColorSamples); | |
1620 | ::glGetIntegerv (GL_MAX_DEPTH_TEXTURE_SAMPLES, &aNbDepthSamples); | |
1621 | myMaxMsaaSamples = Min (aNbColorSamples, aNbDepthSamples); | |
0deb6f04 | 1622 | } |
1623 | } | |
e44b849d | 1624 | #endif |
0deb6f04 | 1625 | |
e44b849d | 1626 | #if !defined(GL_ES_VERSION_2_0) |
1627 | if (core32 != NULL && isCoreProfile) | |
2166f0fa | 1628 | { |
e44b849d | 1629 | core32->glGenVertexArrays (1, &myDefaultVao); |
2166f0fa | 1630 | } |
5f8b738e | 1631 | |
e44b849d | 1632 | myTexClamp = IsGlGreaterEqual (1, 2) ? GL_CLAMP_TO_EDGE : GL_CLAMP; |
37eb4787 | 1633 | |
e44b849d | 1634 | GLint aStereo = GL_FALSE; |
1635 | glGetIntegerv (GL_STEREO, &aStereo); | |
1636 | myIsStereoBuffers = aStereo == 1; | |
25ef750e | 1637 | |
e44b849d | 1638 | // get number of maximum clipping planes |
1639 | glGetIntegerv (GL_MAX_CLIP_PLANES, &myMaxClipPlanes); | |
93cdaa76 | 1640 | #endif |
01ca42b2 | 1641 | |
93cdaa76 | 1642 | #if defined(GL_ES_VERSION_2_0) |
1643 | // check whether ray tracing mode is supported | |
1644 | myHasRayTracing = IsGlGreaterEqual (3, 2); | |
1645 | myHasRayTracingTextures = myHasRayTracingAdaptiveSampling = myHasRayTracingAdaptiveSamplingAtomic = false; | |
1646 | #else | |
3a9b5dc8 | 1647 | // check whether ray tracing mode is supported |
e44b849d | 1648 | myHasRayTracing = IsGlGreaterEqual (3, 1) |
3a9b5dc8 | 1649 | && arbTboRGB32 |
1650 | && arbFBOBlit != NULL; | |
1651 | ||
1652 | // check whether textures in ray tracing mode are supported | |
1653 | myHasRayTracingTextures = myHasRayTracing | |
1654 | && arbTexBindless != NULL; | |
1655 | ||
1656 | // check whether adaptive screen sampling in ray tracing mode is supported | |
1657 | myHasRayTracingAdaptiveSampling = myHasRayTracing | |
e44b849d | 1658 | && core44 != NULL; |
e084dbbc | 1659 | myHasRayTracingAdaptiveSamplingAtomic = myHasRayTracingAdaptiveSampling |
1660 | && CheckExtension ("GL_NV_shader_atomic_float"); | |
ca3c13d1 | 1661 | #endif |
ba00aab7 | 1662 | |
1663 | if (arbFBO != NULL | |
1664 | && hasFboSRGB) | |
1665 | { | |
1666 | // Detect if window buffer is considered by OpenGL as sRGB-ready | |
1667 | // (linear RGB color written by shader is automatically converted into sRGB) | |
1668 | // or not (offscreen FBO should be blit into window buffer with gamma correction). | |
1669 | const GLenum aDefWinBuffer = | |
1670 | #if !defined(GL_ES_VERSION_2_0) | |
1671 | GL_BACK_LEFT; | |
1672 | #else | |
1673 | GL_BACK; | |
1674 | #endif | |
1675 | GLint aWinColorEncoding = 0; // GL_LINEAR | |
1676 | arbFBO->glGetFramebufferAttachmentParameteriv (GL_FRAMEBUFFER, aDefWinBuffer, GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING, &aWinColorEncoding); | |
1677 | ResetErrors (true); | |
1678 | myIsSRgbWindow = aWinColorEncoding == GL_SRGB; | |
1679 | ||
1680 | // On desktop OpenGL, pixel formats are almost always sRGB-ready, even when not requested; | |
1681 | // it is safe behavior on desktop where GL_FRAMEBUFFER_SRGB is disabled by default | |
1682 | // (contrary to OpenGL ES, where it is enabled by default). | |
1683 | // NVIDIA drivers, however, always return GL_LINEAR even for sRGB-ready pixel formats on Windows platform, | |
1684 | // while AMD and Intel report GL_SRGB as expected. | |
1685 | // macOS drivers seems to be also report GL_LINEAR even for [NSColorSpace sRGBColorSpace]. | |
1686 | #if !defined(GL_ES_VERSION_2_0) | |
1687 | #ifdef __APPLE__ | |
1688 | myIsSRgbWindow = true; | |
1689 | #else | |
1690 | if (!myIsSRgbWindow | |
1691 | && myVendor.Search ("nvidia") != -1) | |
1692 | { | |
1693 | myIsSRgbWindow = true; | |
1694 | } | |
1695 | #endif | |
1696 | #endif | |
1697 | if (!myIsSRgbWindow) | |
1698 | { | |
a87b1b37 | 1699 | Message::SendTrace ("OpenGl_Context, warning: window buffer is not sRGB-ready.\n" |
1700 | "Check OpenGL window creation parameters for optimal performance."); | |
ba00aab7 | 1701 | } |
1702 | } | |
67312b79 | 1703 | |
e44b849d | 1704 | // standard formats |
1705 | mySupportedFormats->Add (Image_Format_Gray); | |
1706 | mySupportedFormats->Add (Image_Format_Alpha); | |
1707 | mySupportedFormats->Add (Image_Format_RGB); | |
1708 | mySupportedFormats->Add (Image_Format_RGB32); | |
1709 | mySupportedFormats->Add (Image_Format_RGBA); | |
1710 | if (extBgra) | |
1711 | { | |
1712 | #if !defined(GL_ES_VERSION_2_0) | |
1713 | // no BGR on OpenGL ES - only BGRA as extension | |
1714 | mySupportedFormats->Add (Image_Format_BGR); | |
1715 | #endif | |
1716 | mySupportedFormats->Add (Image_Format_BGR32); | |
1717 | mySupportedFormats->Add (Image_Format_BGRA); | |
1718 | } | |
faff3767 | 1719 | if (arbTexFloat) |
1720 | { | |
1721 | mySupportedFormats->Add (Image_Format_GrayF); | |
1722 | mySupportedFormats->Add (Image_Format_AlphaF); | |
1723 | mySupportedFormats->Add (Image_Format_RGBF); | |
1724 | mySupportedFormats->Add (Image_Format_RGBAF); | |
1ac837b2 | 1725 | if (hasHalfFloatBuffer) |
1726 | { | |
1727 | mySupportedFormats->Add (Image_Format_RGBAF_half); | |
1728 | } | |
faff3767 | 1729 | if (arbTexRG) |
1730 | { | |
1731 | mySupportedFormats->Add (Image_Format_RGF); | |
1ac837b2 | 1732 | if (hasHalfFloatBuffer) |
1733 | { | |
1734 | mySupportedFormats->Add (Image_Format_RGF_half); | |
1735 | } | |
faff3767 | 1736 | } |
1737 | if (extBgra) | |
1738 | { | |
1739 | #if !defined(GL_ES_VERSION_2_0) | |
1740 | mySupportedFormats->Add (Image_Format_BGRF); | |
1741 | #endif | |
1742 | mySupportedFormats->Add (Image_Format_BGRAF); | |
1743 | } | |
1744 | } | |
1745 | ||
1746 | #ifdef __EMSCRIPTEN__ | |
1747 | if (checkEnableWebGlExtension (*this, "GL_WEBGL_compressed_texture_s3tc")) // GL_WEBGL_compressed_texture_s3tc_srgb for sRGB formats | |
1748 | { | |
1749 | mySupportedFormats->Add (Image_CompressedFormat_RGB_S3TC_DXT1); | |
1750 | mySupportedFormats->Add (Image_CompressedFormat_RGBA_S3TC_DXT1); | |
1751 | mySupportedFormats->Add (Image_CompressedFormat_RGBA_S3TC_DXT3); | |
1752 | mySupportedFormats->Add (Image_CompressedFormat_RGBA_S3TC_DXT5); | |
1753 | } | |
e44b849d | 1754 | if (!extPDS |
1755 | && checkEnableWebGlExtension (*this, "GL_WEBGL_depth_texture")) | |
1756 | { | |
1757 | extPDS = true; // WebGL 1.0 extension (in WebGL 2.0 core) | |
1758 | } | |
faff3767 | 1759 | #else |
1760 | if (CheckExtension ("GL_EXT_texture_compression_s3tc")) // GL_EXT_texture_sRGB for sRGB formats | |
1761 | { | |
1762 | mySupportedFormats->Add (Image_CompressedFormat_RGB_S3TC_DXT1); | |
1763 | mySupportedFormats->Add (Image_CompressedFormat_RGBA_S3TC_DXT1); | |
1764 | mySupportedFormats->Add (Image_CompressedFormat_RGBA_S3TC_DXT3); | |
1765 | mySupportedFormats->Add (Image_CompressedFormat_RGBA_S3TC_DXT5); | |
1766 | } | |
1767 | else | |
1768 | { | |
1769 | if (CheckExtension ("GL_EXT_texture_compression_dxt1")) | |
1770 | { | |
1771 | mySupportedFormats->Add (Image_CompressedFormat_RGB_S3TC_DXT1); | |
1772 | mySupportedFormats->Add (Image_CompressedFormat_RGBA_S3TC_DXT1); | |
1773 | } | |
1774 | if (CheckExtension ("GL_ANGLE_texture_compression_dxt3")) | |
1775 | { | |
1776 | mySupportedFormats->Add (Image_CompressedFormat_RGBA_S3TC_DXT3); | |
1777 | } | |
1778 | if (CheckExtension ("GL_ANGLE_texture_compression_dxt5")) | |
1779 | { | |
1780 | mySupportedFormats->Add (Image_CompressedFormat_RGBA_S3TC_DXT5); | |
1781 | } | |
1782 | } | |
1783 | #endif | |
1784 | ||
67312b79 | 1785 | // check whether PBR shading model is supported |
1786 | myHasPBR = arbFBO != NULL | |
1787 | && myMaxTexCombined >= 4 | |
67312b79 | 1788 | && arbTexFloat |
1789 | && (IsGlGreaterEqual (3, 0) | |
8f8fe4a9 | 1790 | #if defined(GL_ES_VERSION_2_0) |
1ac837b2 | 1791 | || hasHighp |
1792 | // || CheckExtension ("GL_EXT_shader_texture_lod") fallback is used when extension is unavailable | |
8f8fe4a9 | 1793 | #else |
67312b79 | 1794 | || (IsGlGreaterEqual (2, 1) && CheckExtension ("GL_EXT_gpu_shader4")) |
1795 | #endif | |
1796 | ); | |
78c4e836 | 1797 | |
1798 | myDepthPeelingDepthTexUnit = static_cast<Graphic3d_TextureUnit>(myMaxTexCombined + Graphic3d_TextureUnit_DepthPeelingDepth); // -6 | |
1799 | myDepthPeelingFrontColorTexUnit = static_cast<Graphic3d_TextureUnit>(myMaxTexCombined + Graphic3d_TextureUnit_DepthPeelingFrontColor); // -5 | |
1800 | myShadowMapTexUnit = static_cast<Graphic3d_TextureUnit>(myMaxTexCombined + Graphic3d_TextureUnit_ShadowMap); // -4 | |
1801 | myPBREnvLUTTexUnit = static_cast<Graphic3d_TextureUnit>(myMaxTexCombined + Graphic3d_TextureUnit_PbrEnvironmentLUT); // -3 | |
1802 | myPBRDiffIBLMapSHTexUnit = static_cast<Graphic3d_TextureUnit>(myMaxTexCombined + Graphic3d_TextureUnit_PbrIblDiffuseSH); // -2 | |
1803 | myPBRSpecIBLMapTexUnit = static_cast<Graphic3d_TextureUnit>(myMaxTexCombined + Graphic3d_TextureUnit_PbrIblSpecular); // -1 | |
1804 | if (!myHasPBR) | |
67312b79 | 1805 | { |
78c4e836 | 1806 | myDepthPeelingDepthTexUnit = static_cast<Graphic3d_TextureUnit>(myDepthPeelingDepthTexUnit + 3); |
1807 | myDepthPeelingFrontColorTexUnit = static_cast<Graphic3d_TextureUnit>(myDepthPeelingFrontColorTexUnit + 3); | |
1808 | myShadowMapTexUnit = static_cast<Graphic3d_TextureUnit>(myShadowMapTexUnit + 3); | |
67312b79 | 1809 | } |
2166f0fa | 1810 | } |
f0430952 | 1811 | |
1812 | // ======================================================================= | |
1813 | // function : MemoryInfo | |
1814 | // purpose : | |
1815 | // ======================================================================= | |
1816 | Standard_Size OpenGl_Context::AvailableMemory() const | |
1817 | { | |
ca3c13d1 | 1818 | #if !defined(GL_ES_VERSION_2_0) |
f0430952 | 1819 | if (atiMem) |
1820 | { | |
1821 | // this is actually information for VBO pool | |
1822 | // however because pools are mostly shared | |
1823 | // it can be used for total GPU memory estimations | |
1824 | GLint aMemInfo[4]; | |
1825 | aMemInfo[0] = 0; | |
1826 | glGetIntegerv (GL_VBO_FREE_MEMORY_ATI, aMemInfo); | |
1827 | // returned value is in KiB, however this maybe changed in future | |
1828 | return Standard_Size(aMemInfo[0]) * 1024; | |
1829 | } | |
1830 | else if (nvxMem) | |
1831 | { | |
1832 | // current available dedicated video memory (in KiB), currently unused GPU memory | |
1833 | GLint aMemInfo = 0; | |
1834 | glGetIntegerv (GL_GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX, &aMemInfo); | |
1835 | return Standard_Size(aMemInfo) * 1024; | |
1836 | } | |
ca3c13d1 | 1837 | #endif |
f0430952 | 1838 | return 0; |
1839 | } | |
1840 | ||
1841 | // ======================================================================= | |
1842 | // function : MemoryInfo | |
1843 | // purpose : | |
1844 | // ======================================================================= | |
1845 | TCollection_AsciiString OpenGl_Context::MemoryInfo() const | |
1846 | { | |
26d9c835 | 1847 | TColStd_IndexedDataMapOfStringString aDict; |
1848 | MemoryInfo (aDict); | |
1849 | ||
1850 | TCollection_AsciiString aText; | |
1851 | for (TColStd_IndexedDataMapOfStringString::Iterator anIter (aDict); anIter.More(); anIter.Next()) | |
1852 | { | |
1853 | if (!aText.IsEmpty()) | |
1854 | { | |
1855 | aText += "\n"; | |
1856 | } | |
1857 | aText += TCollection_AsciiString(" ") + anIter.Key() + ": " + anIter.Value(); | |
1858 | } | |
1859 | return aText; | |
1860 | } | |
1861 | ||
1862 | // ======================================================================= | |
1863 | // function : MemoryInfo | |
1864 | // purpose : | |
1865 | // ======================================================================= | |
1866 | void OpenGl_Context::MemoryInfo (TColStd_IndexedDataMapOfStringString& theDict) const | |
1867 | { | |
1868 | #if defined(GL_ES_VERSION_2_0) | |
1869 | (void )theDict; | |
1870 | #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX) | |
1871 | GLint aGlRendId = 0; | |
1872 | CGLGetParameter (CGLGetCurrentContext(), kCGLCPCurrentRendererID, &aGlRendId); | |
1873 | ||
1874 | CGLRendererInfoObj aRendObj = NULL; | |
1875 | CGOpenGLDisplayMask aDispMask = CGDisplayIDToOpenGLDisplayMask (kCGDirectMainDisplay); | |
1876 | GLint aRendNb = 0; | |
1877 | CGLQueryRendererInfo (aDispMask, &aRendObj, &aRendNb); | |
1878 | for (GLint aRendIter = 0; aRendIter < aRendNb; ++aRendIter) | |
1879 | { | |
1880 | GLint aRendId = 0; | |
1881 | if (CGLDescribeRenderer (aRendObj, aRendIter, kCGLRPRendererID, &aRendId) != kCGLNoError | |
1882 | || aRendId != aGlRendId) | |
1883 | { | |
1884 | continue; | |
1885 | } | |
1886 | ||
1887 | //kCGLRPVideoMemoryMegabytes = 131; | |
1888 | //kCGLRPTextureMemoryMegabytes = 132; | |
1889 | GLint aVMem = 0; | |
1890 | #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1070 | |
1891 | if (CGLDescribeRenderer(aRendObj, aRendIter, kCGLRPVideoMemoryMegabytes, &aVMem) == kCGLNoError) | |
1892 | { | |
1893 | addInfo (theDict, "GPU memory", TCollection_AsciiString() + aVMem + " MiB"); | |
1894 | } | |
1895 | if (CGLDescribeRenderer(aRendObj, aRendIter, kCGLRPTextureMemoryMegabytes, &aVMem) == kCGLNoError) | |
1896 | { | |
1897 | addInfo (theDict, "GPU Texture memory", TCollection_AsciiString() + aVMem + " MiB"); | |
1898 | } | |
1899 | #else | |
1900 | if (CGLDescribeRenderer(aRendObj, aRendIter, kCGLRPVideoMemory, &aVMem) == kCGLNoError) | |
1901 | { | |
1902 | addInfo (theDict, "GPU memory", TCollection_AsciiString() + (aVMem / (1024 * 1024)) + " MiB"); | |
1903 | } | |
1904 | if (CGLDescribeRenderer(aRendObj, aRendIter, kCGLRPTextureMemory, &aVMem) == kCGLNoError) | |
1905 | { | |
1906 | addInfo (theDict, "GPU Texture memory", TCollection_AsciiString() + (aVMem / (1024 * 1024)) + " MiB"); | |
1907 | } | |
1908 | #endif | |
1909 | } | |
1910 | #endif | |
1911 | ||
ca3c13d1 | 1912 | #if !defined(GL_ES_VERSION_2_0) |
f0430952 | 1913 | if (atiMem) |
1914 | { | |
1915 | GLint aValues[4]; | |
1916 | memset (aValues, 0, sizeof(aValues)); | |
1917 | glGetIntegerv (GL_VBO_FREE_MEMORY_ATI, aValues); | |
1918 | ||
1919 | // total memory free in the pool | |
26d9c835 | 1920 | addInfo (theDict, "GPU free memory", TCollection_AsciiString() + (aValues[0] / 1024) + " MiB"); |
f0430952 | 1921 | |
26d9c835 | 1922 | if (aValues[1] != aValues[0]) |
1923 | { | |
1924 | // largest available free block in the pool | |
1925 | addInfo (theDict, "Largest free block", TCollection_AsciiString() + (aValues[1] / 1024) + " MiB"); | |
1926 | } | |
f0430952 | 1927 | if (aValues[2] != aValues[0]) |
1928 | { | |
1929 | // total auxiliary memory free | |
26d9c835 | 1930 | addInfo (theDict, "Free auxiliary memory", TCollection_AsciiString() + (aValues[2] / 1024) + " MiB"); |
f0430952 | 1931 | } |
1932 | } | |
1933 | else if (nvxMem) | |
1934 | { | |
1935 | //current available dedicated video memory (in KiB), currently unused GPU memory | |
1936 | GLint aValue = 0; | |
1937 | glGetIntegerv (GL_GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX, &aValue); | |
26d9c835 | 1938 | addInfo (theDict, "GPU free memory", TCollection_AsciiString() + (aValue / 1024) + " MiB"); |
f0430952 | 1939 | |
1940 | // dedicated video memory, total size (in KiB) of the GPU memory | |
1941 | GLint aDedicated = 0; | |
1942 | glGetIntegerv (GL_GPU_MEMORY_INFO_DEDICATED_VIDMEM_NVX, &aDedicated); | |
26d9c835 | 1943 | addInfo (theDict, "GPU memory", TCollection_AsciiString() + (aDedicated / 1024) + " MiB"); |
f0430952 | 1944 | |
1945 | // total available memory, total size (in KiB) of the memory available for allocations | |
1946 | glGetIntegerv (GL_GPU_MEMORY_INFO_TOTAL_AVAILABLE_MEMORY_NVX, &aValue); | |
1947 | if (aValue != aDedicated) | |
1948 | { | |
1949 | // different only for special configurations | |
26d9c835 | 1950 | addInfo (theDict, "Total memory", TCollection_AsciiString() + (aValue / 1024) + " MiB"); |
1951 | } | |
1952 | } | |
2eacd0b8 | 1953 | #if defined(_WIN32) |
1954 | else if (myFuncs->wglGetGPUInfoAMD != NULL | |
1955 | && myFuncs->wglGetContextGPUIDAMD != NULL) | |
1956 | { | |
1957 | GLuint aTotalMemMiB = 0; | |
1958 | UINT anAmdId = myFuncs->wglGetContextGPUIDAMD ((HGLRC )myGContext); | |
1959 | if (anAmdId != 0) | |
1960 | { | |
1961 | if (myFuncs->wglGetGPUInfoAMD (anAmdId, WGL_GPU_RAM_AMD, GL_UNSIGNED_INT, sizeof(aTotalMemMiB), &aTotalMemMiB) > 0) | |
1962 | { | |
1963 | addInfo (theDict, "GPU memory", TCollection_AsciiString() + (int )aTotalMemMiB + " MiB"); | |
1964 | } | |
1965 | } | |
1966 | } | |
1967 | #endif | |
26d9c835 | 1968 | #endif |
1969 | ||
1970 | #if !defined(GL_ES_VERSION_2_0) && !defined(__APPLE__) && !defined(_WIN32) | |
1971 | // GLX_RENDERER_VENDOR_ID_MESA | |
1972 | if (myFuncs->glXQueryCurrentRendererIntegerMESA != NULL) | |
1973 | { | |
1974 | unsigned int aVMemMiB = 0; | |
1975 | if (myFuncs->glXQueryCurrentRendererIntegerMESA (GLX_RENDERER_VIDEO_MEMORY_MESA, &aVMemMiB) != False) | |
1976 | { | |
1977 | addInfo (theDict, "GPU memory", TCollection_AsciiString() + int(aVMemMiB) + " MiB"); | |
f0430952 | 1978 | } |
1979 | } | |
ca3c13d1 | 1980 | #endif |
f0430952 | 1981 | } |
5e27df78 | 1982 | |
26d9c835 | 1983 | // ======================================================================= |
1984 | // function : DiagnosticInfo | |
1985 | // purpose : | |
1986 | // ======================================================================= | |
1987 | void OpenGl_Context::DiagnosticInformation (TColStd_IndexedDataMapOfStringString& theDict, | |
1988 | Graphic3d_DiagnosticInfo theFlags) const | |
1989 | { | |
1990 | if ((theFlags & Graphic3d_DiagnosticInfo_NativePlatform) != 0) | |
1991 | { | |
1992 | #if defined(HAVE_EGL) | |
6cde53c4 | 1993 | addInfo (theDict, "EGLVersion", ::eglQueryString ((EGLDisplay )myDisplay, EGL_VERSION)); |
1994 | addInfo (theDict, "EGLVendor", ::eglQueryString ((EGLDisplay )myDisplay, EGL_VENDOR)); | |
1995 | addInfo (theDict, "EGLClientAPIs", ::eglQueryString ((EGLDisplay )myDisplay, EGL_CLIENT_APIS)); | |
26d9c835 | 1996 | if ((theFlags & Graphic3d_DiagnosticInfo_Extensions) != 0) |
1997 | { | |
6cde53c4 | 1998 | addInfo (theDict, "EGLExtensions", ::eglQueryString ((EGLDisplay )myDisplay, EGL_EXTENSIONS)); |
26d9c835 | 1999 | } |
2000 | #elif defined(_WIN32) | |
2001 | if ((theFlags & Graphic3d_DiagnosticInfo_Extensions) != 0 | |
2002 | && myFuncs->wglGetExtensionsStringARB != NULL) | |
2003 | { | |
2004 | const char* aWglExts = myFuncs->wglGetExtensionsStringARB ((HDC )myWindowDC); | |
2005 | addInfo (theDict, "WGLExtensions", aWglExts); | |
2006 | } | |
2007 | #elif defined(__APPLE__) | |
2008 | // | |
2009 | #else | |
2010 | Display* aDisplay = (Display*)myDisplay; | |
2011 | const int aScreen = DefaultScreen(aDisplay); | |
2012 | addInfo (theDict, "GLXDirectRendering", ::glXIsDirect (aDisplay, (GLXContext )myGContext) ? "Yes" : "No"); | |
2013 | addInfo (theDict, "GLXVendor", ::glXQueryServerString (aDisplay, aScreen, GLX_VENDOR)); | |
2014 | addInfo (theDict, "GLXVersion", ::glXQueryServerString (aDisplay, aScreen, GLX_VERSION)); | |
2015 | if ((theFlags & Graphic3d_DiagnosticInfo_Extensions) != 0) | |
2016 | { | |
2017 | const char* aGlxExts = ::glXQueryExtensionsString (aDisplay, aScreen); | |
2018 | addInfo(theDict, "GLXExtensions", aGlxExts); | |
2019 | } | |
2020 | ||
2021 | addInfo (theDict, "GLXClientVendor", ::glXGetClientString (aDisplay, GLX_VENDOR)); | |
2022 | addInfo (theDict, "GLXClientVersion", ::glXGetClientString (aDisplay, GLX_VERSION)); | |
2023 | if ((theFlags & Graphic3d_DiagnosticInfo_Extensions) != 0) | |
2024 | { | |
2025 | addInfo (theDict, "GLXClientExtensions", ::glXGetClientString (aDisplay, GLX_EXTENSIONS)); | |
2026 | } | |
2027 | #endif | |
2028 | } | |
2029 | ||
2030 | if ((theFlags & Graphic3d_DiagnosticInfo_Device) != 0) | |
2031 | { | |
59515ca6 | 2032 | Standard_Integer aDriverVer[2] = {}; |
2033 | ReadGlVersion (aDriverVer[0], aDriverVer[1]); | |
26d9c835 | 2034 | addInfo (theDict, "GLvendor", (const char*)::glGetString (GL_VENDOR)); |
2035 | addInfo (theDict, "GLdevice", (const char*)::glGetString (GL_RENDERER)); | |
565baee6 | 2036 | #ifdef __EMSCRIPTEN__ |
2037 | if (checkEnableWebGlExtension (*this, "GL_WEBGL_debug_renderer_info")) | |
2038 | { | |
2039 | if (const char* aVendor = (const char*)::glGetString (0x9245)) | |
2040 | { | |
2041 | addInfo (theDict, "GLunmaskedVendor", aVendor); | |
2042 | } | |
2043 | if (const char* aDevice = (const char*)::glGetString (0x9246)) | |
2044 | { | |
2045 | addInfo (theDict, "GLunmaskedDevice", aDevice); | |
2046 | } | |
2047 | } | |
2048 | #endif | |
2049 | ||
26d9c835 | 2050 | addInfo (theDict, "GLversion", (const char*)::glGetString (GL_VERSION)); |
59515ca6 | 2051 | if (myGlVerMajor != aDriverVer[0] |
2052 | || myGlVerMinor != aDriverVer[1]) | |
2053 | { | |
2054 | addInfo (theDict, "GLversionOcct", TCollection_AsciiString (myGlVerMajor) + "." + TCollection_AsciiString (myGlVerMinor)); | |
2055 | } | |
048e1b3b | 2056 | if (IsGlGreaterEqual (2, 0)) |
2057 | { | |
2058 | addInfo (theDict, "GLSLversion", (const char*)::glGetString (GL_SHADING_LANGUAGE_VERSION)); | |
2059 | } | |
26d9c835 | 2060 | if (myIsGlDebugCtx) |
2061 | { | |
2062 | addInfo (theDict, "GLdebug", "ON"); | |
2063 | } | |
2064 | } | |
2065 | ||
2066 | if ((theFlags & Graphic3d_DiagnosticInfo_Limits) != 0) | |
2067 | { | |
2068 | addInfo (theDict, "Max texture size", TCollection_AsciiString(myMaxTexDim)); | |
6997ff1c | 2069 | addInfo (theDict, "Max FBO dump size", TCollection_AsciiString() + myMaxDumpSizeX + "x" + myMaxDumpSizeY); |
cc8cbabe | 2070 | addInfo (theDict, "Max combined texture units", TCollection_AsciiString(myMaxTexCombined)); |
26d9c835 | 2071 | addInfo (theDict, "Max MSAA samples", TCollection_AsciiString(myMaxMsaaSamples)); |
2072 | } | |
2073 | ||
2074 | if ((theFlags & Graphic3d_DiagnosticInfo_FrameBuffer) != 0) | |
2075 | { | |
2076 | GLint aViewport[4] = {}; | |
2077 | ::glGetIntegerv (GL_VIEWPORT, aViewport); | |
2078 | addInfo (theDict, "Viewport", TCollection_AsciiString() + aViewport[2] + "x" + aViewport[3]); | |
2079 | } | |
2080 | ||
2081 | if ((theFlags & Graphic3d_DiagnosticInfo_Memory) != 0) | |
2082 | { | |
2083 | MemoryInfo (theDict); | |
2084 | } | |
2085 | ||
2086 | if ((theFlags & Graphic3d_DiagnosticInfo_Extensions) != 0) | |
2087 | { | |
2088 | #if !defined(GL_ES_VERSION_2_0) | |
2089 | if (IsGlGreaterEqual (3, 0) | |
2090 | && myFuncs->glGetStringi != NULL) | |
2091 | { | |
2092 | TCollection_AsciiString anExtList; | |
2093 | GLint anExtNb = 0; | |
2094 | ::glGetIntegerv (GL_NUM_EXTENSIONS, &anExtNb); | |
2095 | for (GLint anIter = 0; anIter < anExtNb; ++anIter) | |
2096 | { | |
2097 | const char* anExtension = (const char*)myFuncs->glGetStringi (GL_EXTENSIONS, (GLuint)anIter); | |
2098 | if (!anExtList.IsEmpty()) | |
2099 | { | |
2100 | anExtList += " "; | |
2101 | } | |
2102 | anExtList += anExtension; | |
2103 | } | |
2104 | addInfo(theDict, "GLextensions", anExtList); | |
2105 | } | |
2106 | else | |
2107 | #endif | |
2108 | { | |
2109 | addInfo (theDict, "GLextensions", (const char*)::glGetString (GL_EXTENSIONS)); | |
2110 | } | |
2111 | } | |
2112 | } | |
5e27df78 | 2113 | |
2114 | // ======================================================================= | |
2115 | // function : GetResource | |
2116 | // purpose : | |
2117 | // ======================================================================= | |
2118 | const Handle(OpenGl_Resource)& OpenGl_Context::GetResource (const TCollection_AsciiString& theKey) const | |
2119 | { | |
2120 | return mySharedResources->IsBound (theKey) ? mySharedResources->Find (theKey) : NULL_GL_RESOURCE; | |
2121 | } | |
2122 | ||
2123 | // ======================================================================= | |
2124 | // function : ShareResource | |
2125 | // purpose : | |
2126 | // ======================================================================= | |
2127 | Standard_Boolean OpenGl_Context::ShareResource (const TCollection_AsciiString& theKey, | |
2128 | const Handle(OpenGl_Resource)& theResource) | |
2129 | { | |
2130 | if (theKey.IsEmpty() || theResource.IsNull()) | |
2131 | { | |
2132 | return Standard_False; | |
2133 | } | |
2134 | return mySharedResources->Bind (theKey, theResource); | |
2135 | } | |
2136 | ||
2137 | // ======================================================================= | |
2138 | // function : ReleaseResource | |
2139 | // purpose : | |
2140 | // ======================================================================= | |
a174a3c5 | 2141 | void OpenGl_Context::ReleaseResource (const TCollection_AsciiString& theKey, |
2142 | const Standard_Boolean theToDelay) | |
5e27df78 | 2143 | { |
2144 | if (!mySharedResources->IsBound (theKey)) | |
2145 | { | |
2146 | return; | |
2147 | } | |
71c810df | 2148 | const Handle(OpenGl_Resource)& aRes = mySharedResources->Find (theKey); |
5e27df78 | 2149 | if (aRes->GetRefCount() > 1) |
2150 | { | |
2151 | return; | |
2152 | } | |
2153 | ||
a174a3c5 | 2154 | if (theToDelay) |
2155 | { | |
2156 | myDelayed->Bind (theKey, 1); | |
2157 | } | |
2158 | else | |
2159 | { | |
2160 | aRes->Release (this); | |
2161 | mySharedResources->UnBind (theKey); | |
2162 | } | |
5e27df78 | 2163 | } |
2164 | ||
2165 | // ======================================================================= | |
5e27df78 | 2166 | // function : ReleaseDelayed |
2167 | // purpose : | |
2168 | // ======================================================================= | |
2169 | void OpenGl_Context::ReleaseDelayed() | |
2170 | { | |
a174a3c5 | 2171 | // release queued elements |
3125ebb6 | 2172 | while (!myUnusedResources->IsEmpty()) |
5e27df78 | 2173 | { |
3125ebb6 | 2174 | myUnusedResources->First()->Release (this); |
2175 | myUnusedResources->RemoveFirst(); | |
5e27df78 | 2176 | } |
a174a3c5 | 2177 | |
265d4508 | 2178 | // release delayed shared resources |
a174a3c5 | 2179 | NCollection_Vector<TCollection_AsciiString> aDeadList; |
2180 | for (NCollection_DataMap<TCollection_AsciiString, Standard_Integer>::Iterator anIter (*myDelayed); | |
2181 | anIter.More(); anIter.Next()) | |
2182 | { | |
2183 | if (++anIter.ChangeValue() <= 2) | |
2184 | { | |
5e6e5914 | 2185 | continue; // postpone release one more frame to ensure no one uses it periodically |
a174a3c5 | 2186 | } |
2187 | ||
2188 | const TCollection_AsciiString& aKey = anIter.Key(); | |
2189 | if (!mySharedResources->IsBound (aKey)) | |
2190 | { | |
2191 | // mixed unshared strategy delayed/undelayed was used! | |
2192 | aDeadList.Append (aKey); | |
2193 | continue; | |
2194 | } | |
2195 | ||
71c810df | 2196 | const Handle(OpenGl_Resource)& aRes = mySharedResources->ChangeFind (aKey); |
a174a3c5 | 2197 | if (aRes->GetRefCount() > 1) |
2198 | { | |
2199 | // should be only 1 instance in mySharedResources | |
2200 | // if not - resource was reused again | |
2201 | aDeadList.Append (aKey); | |
2202 | continue; | |
2203 | } | |
2204 | ||
43eddb47 | 2205 | // release resource if no one requested it more than 2 redraw calls |
a174a3c5 | 2206 | aRes->Release (this); |
2207 | mySharedResources->UnBind (aKey); | |
2208 | aDeadList.Append (aKey); | |
2209 | } | |
2210 | ||
2211 | for (Standard_Integer anIter = 0; anIter < aDeadList.Length(); ++anIter) | |
2212 | { | |
2213 | myDelayed->UnBind (aDeadList.Value (anIter)); | |
2214 | } | |
5e27df78 | 2215 | } |
7d3e64ef | 2216 | |
cc8cbabe | 2217 | // ======================================================================= |
2218 | // function : BindTextures | |
2219 | // purpose : | |
2220 | // ======================================================================= | |
72f6dc61 | 2221 | Handle(OpenGl_TextureSet) OpenGl_Context::BindTextures (const Handle(OpenGl_TextureSet)& theTextures, |
2222 | const Handle(OpenGl_ShaderProgram)& theProgram) | |
cc8cbabe | 2223 | { |
72f6dc61 | 2224 | const Standard_Integer aTextureSetBits = !theTextures.IsNull() ? theTextures->TextureSetBits() : 0; |
2225 | const Standard_Integer aProgramBits = !theProgram.IsNull() ? theProgram->TextureSetBits() : 0; | |
2226 | Standard_Integer aMissingBits = aProgramBits & ~aTextureSetBits; | |
2227 | if (aMissingBits != 0 | |
2228 | && myTextureRgbaBlack.IsNull()) | |
cc8cbabe | 2229 | { |
72f6dc61 | 2230 | // allocate mock textures |
2231 | myTextureRgbaBlack = new OpenGl_Texture(); | |
2232 | myTextureRgbaWhite = new OpenGl_Texture(); | |
2233 | Image_PixMap anImage; | |
2234 | anImage.InitZero (Image_Format_RGBA, 2, 2, 0, (Standard_Byte )0); | |
2235 | if (!myTextureRgbaBlack->Init (this, OpenGl_TextureFormat::Create<GLubyte, 4>(), Graphic3d_Vec2i (2, 2), Graphic3d_TOT_2D, &anImage)) | |
2236 | { | |
2237 | PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_HIGH, | |
2238 | "Error: unable to create unit mock PBR texture map."); | |
2239 | } | |
2240 | anImage.InitZero (Image_Format_RGBA, 2, 2, 0, (Standard_Byte )255); | |
2241 | if (!myTextureRgbaWhite->Init (this, OpenGl_TextureFormat::Create<GLubyte, 4>(), Graphic3d_Vec2i (2, 2), Graphic3d_TOT_2D, &anImage)) | |
2242 | { | |
2243 | PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_HIGH, | |
2244 | "Error: unable to create normal mock PBR texture map."); | |
2245 | } | |
cc8cbabe | 2246 | } |
2247 | ||
72f6dc61 | 2248 | Handle(OpenGl_TextureSet) anOldTextures = myActiveTextures; |
2249 | if (myActiveTextures != theTextures) | |
cc8cbabe | 2250 | { |
72f6dc61 | 2251 | Handle(OpenGl_Context) aThisCtx (this); |
d850414a | 2252 | for (OpenGl_TextureSetPairIterator aSlotIter (myActiveTextures, theTextures); aSlotIter.More(); aSlotIter.Next()) |
cc8cbabe | 2253 | { |
d850414a | 2254 | const Graphic3d_TextureUnit aTexUnit = aSlotIter.Unit(); |
2255 | const OpenGl_Texture* aTextureOld = aSlotIter.Texture1(); | |
2256 | const OpenGl_Texture* aTextureNew = aSlotIter.Texture2(); | |
2257 | if (aTextureNew == aTextureOld) | |
cc8cbabe | 2258 | { |
d850414a | 2259 | continue; |
cc8cbabe | 2260 | } |
cc8cbabe | 2261 | |
d850414a | 2262 | if (aTextureNew != NULL |
2263 | && aTextureNew->IsValid()) | |
cc8cbabe | 2264 | { |
d850414a | 2265 | if (aTexUnit >= myMaxTexCombined) |
cc8cbabe | 2266 | { |
d850414a | 2267 | PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, |
2268 | TCollection_AsciiString("Texture unit ") + aTexUnit + " for " + aTextureNew->ResourceId() + " exceeds hardware limit " + myMaxTexCombined); | |
72f6dc61 | 2269 | continue; |
2270 | } | |
d850414a | 2271 | |
2272 | aTextureNew->Bind (aThisCtx, aTexUnit); | |
2273 | if (aTextureNew->Sampler()->ToUpdateParameters()) | |
72f6dc61 | 2274 | { |
d850414a | 2275 | if (aTextureNew->Sampler()->IsImmutable()) |
cc8cbabe | 2276 | { |
d850414a | 2277 | aTextureNew->Sampler()->Init (aThisCtx, *aTextureNew); |
2278 | } | |
2279 | else | |
2280 | { | |
faff3767 | 2281 | OpenGl_Sampler::applySamplerParams (aThisCtx, aTextureNew->Sampler()->Parameters(), aTextureNew->Sampler().get(), aTextureNew->GetTarget(), aTextureNew->MaxMipmapLevel()); |
cc8cbabe | 2282 | } |
72f6dc61 | 2283 | } |
d850414a | 2284 | #if !defined(GL_ES_VERSION_2_0) |
43eddb47 | 2285 | if (core11ffp != NULL) |
72f6dc61 | 2286 | { |
d850414a | 2287 | OpenGl_Sampler::applyGlobalTextureParams (aThisCtx, *aTextureNew, aTextureNew->Sampler()->Parameters()); |
72f6dc61 | 2288 | } |
d850414a | 2289 | #endif |
cc8cbabe | 2290 | } |
d850414a | 2291 | else if (aTextureOld != NULL |
2292 | && aTextureOld->IsValid()) | |
cc8cbabe | 2293 | { |
d850414a | 2294 | aTextureOld->Unbind (aThisCtx, aTexUnit); |
2295 | #if !defined(GL_ES_VERSION_2_0) | |
43eddb47 | 2296 | if (core11ffp != NULL) |
d850414a | 2297 | { |
2298 | OpenGl_Sampler::resetGlobalTextureParams (aThisCtx, *aTextureOld, aTextureOld->Sampler()->Parameters()); | |
2299 | } | |
2300 | #endif | |
cc8cbabe | 2301 | } |
2302 | } | |
72f6dc61 | 2303 | myActiveTextures = theTextures; |
2304 | } | |
2305 | ||
2306 | if (myActiveMockTextures != aMissingBits) | |
2307 | { | |
2308 | myActiveMockTextures = aMissingBits; | |
2309 | for (Standard_Integer aBitIter = 0; aMissingBits != 0; ++aBitIter) | |
cc8cbabe | 2310 | { |
72f6dc61 | 2311 | Standard_Integer aUnitMask = 1 << aBitIter; |
2312 | if ((aUnitMask & aMissingBits) != 0) | |
2313 | { | |
2314 | aMissingBits = aMissingBits & ~aUnitMask; | |
2315 | if (aBitIter == Graphic3d_TextureUnit_Normal) | |
2316 | { | |
2317 | myTextureRgbaBlack->Bind (this, static_cast<Graphic3d_TextureUnit>(aBitIter)); | |
2318 | } | |
2319 | else | |
2320 | { | |
2321 | myTextureRgbaWhite->Bind (this, static_cast<Graphic3d_TextureUnit>(aBitIter)); | |
2322 | } | |
2323 | } | |
cc8cbabe | 2324 | } |
cc8cbabe | 2325 | } |
2326 | ||
cc8cbabe | 2327 | return anOldTextures; |
2328 | } | |
2329 | ||
7d3e64ef | 2330 | // ======================================================================= |
2331 | // function : BindProgram | |
2332 | // purpose : | |
2333 | // ======================================================================= | |
8625ef7e | 2334 | Standard_Boolean OpenGl_Context::BindProgram (const Handle(OpenGl_ShaderProgram)& theProgram) |
7d3e64ef | 2335 | { |
8625ef7e | 2336 | if (core20fwd == NULL) |
2337 | { | |
2338 | return Standard_False; | |
2339 | } | |
8613985b | 2340 | else if (myActiveProgram == theProgram) |
2341 | { | |
2342 | return Standard_True; | |
2343 | } | |
8625ef7e | 2344 | |
7d3e64ef | 2345 | if (theProgram.IsNull() |
2346 | || !theProgram->IsValid()) | |
2347 | { | |
2348 | if (!myActiveProgram.IsNull()) | |
2349 | { | |
2350 | core20fwd->glUseProgram (OpenGl_ShaderProgram::NO_PROGRAM); | |
2351 | myActiveProgram.Nullify(); | |
2352 | } | |
8625ef7e | 2353 | return Standard_False; |
7d3e64ef | 2354 | } |
2355 | ||
2356 | myActiveProgram = theProgram; | |
2357 | core20fwd->glUseProgram (theProgram->ProgramId()); | |
8625ef7e | 2358 | return Standard_True; |
2359 | } | |
2360 | ||
4e1523ef | 2361 | // ======================================================================= |
2362 | // function : BindDefaultVao | |
2363 | // purpose : | |
2364 | // ======================================================================= | |
2365 | void OpenGl_Context::BindDefaultVao() | |
2366 | { | |
2367 | #if !defined(GL_ES_VERSION_2_0) | |
2368 | if (myDefaultVao == 0 | |
2369 | || core32 == NULL) | |
2370 | { | |
2371 | return; | |
2372 | } | |
2373 | ||
2374 | core32->glBindVertexArray (myDefaultVao); | |
2375 | #endif | |
2376 | } | |
2377 | ||
a2e4f780 | 2378 | // ======================================================================= |
2379 | // function : SetDefaultFrameBuffer | |
2380 | // purpose : | |
2381 | // ======================================================================= | |
2382 | Handle(OpenGl_FrameBuffer) OpenGl_Context::SetDefaultFrameBuffer (const Handle(OpenGl_FrameBuffer)& theFbo) | |
2383 | { | |
2384 | Handle(OpenGl_FrameBuffer) aFbo = myDefaultFbo; | |
2385 | myDefaultFbo = theFbo; | |
2386 | return aFbo; | |
2387 | } | |
2388 | ||
8625ef7e | 2389 | // ======================================================================= |
299e0ab9 | 2390 | // function : SetShadingMaterial |
2391 | // purpose : | |
2392 | // ======================================================================= | |
bf5f0ca2 | 2393 | void OpenGl_Context::SetShadingMaterial (const OpenGl_Aspects* theAspect, |
a1073ae2 | 2394 | const Handle(Graphic3d_PresentationAttributes)& theHighlight) |
299e0ab9 | 2395 | { |
bf5f0ca2 | 2396 | const Handle(Graphic3d_Aspects)& anAspect = (!theHighlight.IsNull() && !theHighlight->BasicFillAreaAspect().IsNull()) |
2397 | ? (const Handle(Graphic3d_Aspects)& )theHighlight->BasicFillAreaAspect() | |
2398 | : theAspect->Aspect(); | |
8613985b | 2399 | |
2400 | const bool toDistinguish = anAspect->Distinguish(); | |
2401 | const bool toMapTexture = anAspect->ToMapTexture(); | |
2402 | const Graphic3d_MaterialAspect& aMatFrontSrc = anAspect->FrontMaterial(); | |
2403 | const Graphic3d_MaterialAspect& aMatBackSrc = toDistinguish | |
2404 | ? anAspect->BackMaterial() | |
2405 | : aMatFrontSrc; | |
2406 | const Quantity_Color& aFrontIntColor = anAspect->InteriorColor(); | |
2407 | const Quantity_Color& aBackIntColor = toDistinguish | |
2408 | ? anAspect->BackInteriorColor() | |
2409 | : aFrontIntColor; | |
2410 | ||
941f6cae | 2411 | myMaterial.Init (*this, aMatFrontSrc, aFrontIntColor, aMatBackSrc, aBackIntColor); |
8613985b | 2412 | if (!theHighlight.IsNull() |
2413 | && theHighlight->BasicFillAreaAspect().IsNull()) | |
2414 | { | |
941f6cae | 2415 | myMaterial.SetColor (theHighlight->ColorRGBA().GetRGB()); |
2416 | myMaterial.SetColor (theHighlight->ColorRGBA().GetRGB()); | |
8613985b | 2417 | } |
a1073ae2 | 2418 | |
941f6cae | 2419 | float anAlphaFront = 1.0f, anAlphaBack = 1.0f; |
a71a71de | 2420 | if (CheckIsTransparent (theAspect, theHighlight, anAlphaFront, anAlphaBack)) |
8613985b | 2421 | { |
941f6cae | 2422 | myMaterial.Common[0].Diffuse.a() = anAlphaFront; |
2423 | myMaterial.Common[1].Diffuse.a() = anAlphaBack; | |
67312b79 | 2424 | |
941f6cae | 2425 | myMaterial.Pbr[0].BaseColor.a() = anAlphaFront; |
2426 | myMaterial.Pbr[1].BaseColor.a() = anAlphaBack; | |
299e0ab9 | 2427 | } |
8613985b | 2428 | |
2429 | // do not update material properties in case of zero reflection mode, | |
2430 | // because GL lighting will be disabled by OpenGl_PrimitiveArray::DrawArray() anyway. | |
c40eb6b9 | 2431 | const OpenGl_MaterialState& aMatState = myShaderManager->MaterialState(); |
33425a46 | 2432 | float anAlphaCutoff = (anAspect->AlphaMode() == Graphic3d_AlphaMode_Mask |
2433 | || anAspect->AlphaMode() == Graphic3d_AlphaMode_MaskBlend) | |
2a332745 | 2434 | ? anAspect->AlphaCutoff() |
2435 | : ShortRealLast(); | |
2436 | if (anAspect->ToDrawEdges()) | |
2437 | { | |
2438 | if (anAspect->InteriorStyle() == Aspect_IS_EMPTY | |
2439 | || (anAspect->InteriorStyle() == Aspect_IS_SOLID | |
2440 | && anAspect->EdgeColorRGBA().Alpha() < 1.0f)) | |
2441 | { | |
2442 | anAlphaCutoff = 0.285f; | |
2443 | } | |
2444 | } | |
dc89236f | 2445 | if (theAspect->ShadingModel() == Graphic3d_TOSM_UNLIT) |
8613985b | 2446 | { |
c40eb6b9 | 2447 | if (anAlphaCutoff == aMatState.AlphaCutoff()) |
2448 | { | |
2449 | return; | |
2450 | } | |
8613985b | 2451 | } |
941f6cae | 2452 | else if (myMaterial.IsEqual (aMatState.Material()) |
c40eb6b9 | 2453 | && toDistinguish == aMatState.ToDistinguish() |
2454 | && toMapTexture == aMatState.ToMapTexture() | |
2455 | && anAlphaCutoff == aMatState.AlphaCutoff()) | |
8613985b | 2456 | { |
2457 | return; | |
2458 | } | |
2459 | ||
941f6cae | 2460 | myShaderManager->UpdateMaterialStateTo (myMaterial, anAlphaCutoff, toDistinguish, toMapTexture); |
299e0ab9 | 2461 | } |
2462 | ||
a1073ae2 | 2463 | // ======================================================================= |
2464 | // function : CheckIsTransparent | |
2465 | // purpose : | |
2466 | // ======================================================================= | |
bf5f0ca2 | 2467 | Standard_Boolean OpenGl_Context::CheckIsTransparent (const OpenGl_Aspects* theAspect, |
a1073ae2 | 2468 | const Handle(Graphic3d_PresentationAttributes)& theHighlight, |
a71a71de | 2469 | Standard_ShortReal& theAlphaFront, |
2470 | Standard_ShortReal& theAlphaBack) | |
a1073ae2 | 2471 | { |
bf5f0ca2 | 2472 | const Handle(Graphic3d_Aspects)& anAspect = (!theHighlight.IsNull() && !theHighlight->BasicFillAreaAspect().IsNull()) |
2473 | ? (const Handle(Graphic3d_Aspects)& )theHighlight->BasicFillAreaAspect() | |
2474 | : theAspect->Aspect(); | |
a1073ae2 | 2475 | |
2476 | const bool toDistinguish = anAspect->Distinguish(); | |
2477 | const Graphic3d_MaterialAspect& aMatFrontSrc = anAspect->FrontMaterial(); | |
2478 | const Graphic3d_MaterialAspect& aMatBackSrc = toDistinguish | |
2479 | ? anAspect->BackMaterial() | |
2480 | : aMatFrontSrc; | |
2481 | ||
2482 | // handling transparency | |
a1073ae2 | 2483 | if (!theHighlight.IsNull() |
2484 | && theHighlight->BasicFillAreaAspect().IsNull()) | |
2485 | { | |
a71a71de | 2486 | theAlphaFront = theHighlight->ColorRGBA().Alpha(); |
2487 | theAlphaBack = theHighlight->ColorRGBA().Alpha(); | |
2488 | } | |
2489 | else | |
2490 | { | |
2491 | theAlphaFront = aMatFrontSrc.Alpha(); | |
2492 | theAlphaBack = aMatBackSrc .Alpha(); | |
a1073ae2 | 2493 | } |
2494 | ||
c40eb6b9 | 2495 | if (anAspect->AlphaMode() == Graphic3d_AlphaMode_BlendAuto) |
2496 | { | |
2497 | return theAlphaFront < 1.0f | |
2498 | || theAlphaBack < 1.0f; | |
2499 | } | |
33425a46 | 2500 | // Graphic3d_AlphaMode_Mask and Graphic3d_AlphaMode_MaskBlend are not considered transparent here |
c40eb6b9 | 2501 | return anAspect->AlphaMode() == Graphic3d_AlphaMode_Blend; |
a1073ae2 | 2502 | } |
2503 | ||
299e0ab9 | 2504 | // ======================================================================= |
8625ef7e | 2505 | // function : SetColor4fv |
2506 | // purpose : | |
2507 | // ======================================================================= | |
2508 | void OpenGl_Context::SetColor4fv (const OpenGl_Vec4& theColor) | |
2509 | { | |
2510 | if (!myActiveProgram.IsNull()) | |
2511 | { | |
ba00aab7 | 2512 | if (const OpenGl_ShaderUniformLocation& aLoc = myActiveProgram->GetStateLocation (OpenGl_OCCT_COLOR)) |
2513 | { | |
2514 | myActiveProgram->SetUniform (this, aLoc, Vec4FromQuantityColor (theColor)); | |
2515 | } | |
8625ef7e | 2516 | } |
2517 | #if !defined(GL_ES_VERSION_2_0) | |
43eddb47 | 2518 | else if (core11ffp != NULL) |
8625ef7e | 2519 | { |
43eddb47 | 2520 | core11ffp->glColor4fv (theColor.GetData()); |
8625ef7e | 2521 | } |
2522 | #endif | |
2523 | } | |
2524 | ||
ac116c22 | 2525 | // ======================================================================= |
2526 | // function : SetTypeOfLine | |
2527 | // purpose : | |
2528 | // ======================================================================= | |
2529 | void OpenGl_Context::SetTypeOfLine (const Aspect_TypeOfLine theType, | |
2530 | const Standard_ShortReal theFactor) | |
2531 | { | |
3f1675c9 | 2532 | SetLineStipple (theFactor, Graphic3d_Aspects::DefaultLinePatternForType (theType)); |
2533 | } | |
ac116c22 | 2534 | |
3f1675c9 | 2535 | // ======================================================================= |
2536 | // function : SetLineStipple | |
2537 | // purpose : | |
2538 | // ======================================================================= | |
2539 | void OpenGl_Context::SetLineStipple (const Standard_ShortReal theFactor, | |
2540 | const uint16_t thePattern) | |
2541 | { | |
c04c30b3 | 2542 | if (!myActiveProgram.IsNull()) |
ac116c22 | 2543 | { |
59515ca6 | 2544 | if (const OpenGl_ShaderUniformLocation aPatternLoc = myActiveProgram->GetStateLocation (OpenGl_OCCT_LINE_STIPPLE_PATTERN)) |
2545 | { | |
2546 | if (hasGlslBitwiseOps != OpenGl_FeatureNotAvailable) | |
2547 | { | |
3f1675c9 | 2548 | myActiveProgram->SetUniform (this, aPatternLoc, (Standard_Integer )thePattern); |
59515ca6 | 2549 | } |
2550 | else | |
2551 | { | |
2552 | Standard_Integer aPatArr[16] = {}; | |
2553 | for (unsigned int aBit = 0; aBit < 16; ++aBit) | |
2554 | { | |
3f1675c9 | 2555 | aPatArr[aBit] = ((unsigned int)(thePattern) & (1U << aBit)) != 0 ? 1 : 0; |
59515ca6 | 2556 | } |
2557 | myActiveProgram->SetUniform (this, aPatternLoc, 16, aPatArr); | |
2558 | } | |
2559 | myActiveProgram->SetUniform (this, myActiveProgram->GetStateLocation (OpenGl_OCCT_LINE_STIPPLE_FACTOR), theFactor); | |
2560 | } | |
ac116c22 | 2561 | return; |
2562 | } | |
2563 | ||
2564 | #if !defined(GL_ES_VERSION_2_0) | |
3f1675c9 | 2565 | if (thePattern != 0xFFFF) |
ac116c22 | 2566 | { |
43eddb47 | 2567 | if (core11ffp != NULL) |
ac116c22 | 2568 | { |
2569 | core11fwd->glEnable (GL_LINE_STIPPLE); | |
2570 | ||
43eddb47 | 2571 | core11ffp->glLineStipple (static_cast<GLint> (theFactor), |
2572 | static_cast<GLushort> (thePattern)); | |
ac116c22 | 2573 | } |
2574 | } | |
2575 | else | |
2576 | { | |
43eddb47 | 2577 | if (core11ffp != NULL) |
ac116c22 | 2578 | { |
2579 | core11fwd->glDisable (GL_LINE_STIPPLE); | |
2580 | } | |
ac116c22 | 2581 | } |
2582 | #endif | |
2583 | } | |
2584 | ||
2585 | // ======================================================================= | |
2586 | // function : SetLineWidth | |
2587 | // purpose : | |
2588 | // ======================================================================= | |
2589 | void OpenGl_Context::SetLineWidth (const Standard_ShortReal theWidth) | |
2590 | { | |
92996979 | 2591 | #if !defined(GL_ES_VERSION_2_0) |
43eddb47 | 2592 | if (core11ffp != NULL) |
92996979 | 2593 | #endif |
ac116c22 | 2594 | { |
2595 | // glLineWidth() is still defined within Core Profile, but has no effect with values != 1.0f | |
56689b27 | 2596 | core11fwd->glLineWidth (theWidth * myLineWidthScale); |
ac116c22 | 2597 | } |
ac116c22 | 2598 | } |
2599 | ||
79f4f036 | 2600 | // ======================================================================= |
2601 | // function : SetTextureMatrix | |
2602 | // purpose : | |
2603 | // ======================================================================= | |
faff3767 | 2604 | void OpenGl_Context::SetTextureMatrix (const Handle(Graphic3d_TextureParams)& theParams, |
2605 | const Standard_Boolean theIsTopDown) | |
79f4f036 | 2606 | { |
2607 | if (theParams.IsNull()) | |
2608 | { | |
2609 | return; | |
2610 | } | |
faff3767 | 2611 | |
2612 | const Graphic3d_Vec2& aScale = theParams->Scale(); | |
2613 | const Graphic3d_Vec2& aTrans = theParams->Translation(); | |
2614 | if (!myActiveProgram.IsNull()) | |
79f4f036 | 2615 | { |
2616 | const GLint aUniLoc = myActiveProgram->GetStateLocation (OpenGl_OCCT_TEXTURE_TRSF2D); | |
2617 | if (aUniLoc == OpenGl_ShaderProgram::INVALID_LOCATION) | |
2618 | { | |
2619 | return; | |
2620 | } | |
2621 | ||
2622 | // pack transformation parameters | |
bc379358 | 2623 | OpenGl_Vec4 aTrsf[2] = |
2624 | { | |
faff3767 | 2625 | OpenGl_Vec4 (-aTrans.x(), -aTrans.y(), aScale.x(), aScale.y()), |
bc379358 | 2626 | OpenGl_Vec4 (static_cast<float> (std::sin (-theParams->Rotation() * M_PI / 180.0)), |
2627 | static_cast<float> (std::cos (-theParams->Rotation() * M_PI / 180.0)), | |
2628 | 0.0f, 0.0f) | |
2629 | }; | |
faff3767 | 2630 | if (caps->isTopDownTextureUV != theIsTopDown) |
2631 | { | |
2632 | // flip V | |
2633 | aTrsf[0].y() = -aTrans.y() + 1.0f / aScale.y(); | |
2634 | aTrsf[0].w() = -aScale.y(); | |
2635 | } | |
79f4f036 | 2636 | myActiveProgram->SetUniform (this, aUniLoc, 2, aTrsf); |
2637 | return; | |
2638 | } | |
2639 | ||
2640 | #if !defined(GL_ES_VERSION_2_0) | |
43eddb47 | 2641 | if (core11ffp != NULL) |
79f4f036 | 2642 | { |
2643 | GLint aMatrixMode = GL_TEXTURE; | |
2644 | ::glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode); | |
2645 | ||
43eddb47 | 2646 | core11ffp->glMatrixMode (GL_TEXTURE); |
79f4f036 | 2647 | OpenGl_Mat4 aTextureMat; |
faff3767 | 2648 | if (caps->isTopDownTextureUV != theIsTopDown) |
2649 | { | |
2650 | // flip V | |
2651 | Graphic3d_TransformUtils::Scale (aTextureMat, aScale.x(), -aScale.y(), 1.0f); | |
2652 | Graphic3d_TransformUtils::Translate (aTextureMat, -aTrans.x(), -aTrans.y() + 1.0f / aScale.y(), 0.0f); | |
2653 | } | |
2654 | else | |
2655 | { | |
2656 | Graphic3d_TransformUtils::Scale (aTextureMat, aScale.x(), aScale.y(), 1.0f); | |
2657 | Graphic3d_TransformUtils::Translate (aTextureMat, -aTrans.x(), -aTrans.y(), 0.0f); | |
2658 | } | |
2659 | Graphic3d_TransformUtils::Rotate (aTextureMat, -theParams->Rotation(), 0.0f, 0.0f, 1.0f); | |
43eddb47 | 2660 | core11ffp->glLoadMatrixf (aTextureMat); |
2661 | core11ffp->glMatrixMode (aMatrixMode); | |
79f4f036 | 2662 | } |
2663 | #endif | |
2664 | } | |
2665 | ||
8625ef7e | 2666 | // ======================================================================= |
2667 | // function : SetPointSize | |
2668 | // purpose : | |
2669 | // ======================================================================= | |
2670 | void OpenGl_Context::SetPointSize (const Standard_ShortReal theSize) | |
2671 | { | |
2672 | if (!myActiveProgram.IsNull()) | |
2673 | { | |
2674 | myActiveProgram->SetUniform (this, myActiveProgram->GetStateLocation (OpenGl_OCCT_POINT_SIZE), theSize); | |
2675 | #if !defined(GL_ES_VERSION_2_0) | |
2676 | //myContext->core11fwd->glEnable (GL_VERTEX_PROGRAM_POINT_SIZE); | |
2677 | #endif | |
2678 | } | |
2679 | #if !defined(GL_ES_VERSION_2_0) | |
2680 | //else | |
2681 | { | |
2682 | core11fwd->glPointSize (theSize); | |
2683 | if (core20fwd != NULL) | |
2684 | { | |
2685 | //myContext->core11fwd->glDisable (GL_VERTEX_PROGRAM_POINT_SIZE); | |
2686 | } | |
2687 | } | |
2688 | #endif | |
7d3e64ef | 2689 | } |
7d9e854b | 2690 | |
fd59283a | 2691 | // ======================================================================= |
2692 | // function : SetPointSpriteOrigin | |
2693 | // purpose : | |
2694 | // ======================================================================= | |
2695 | void OpenGl_Context::SetPointSpriteOrigin() | |
2696 | { | |
2697 | #if !defined(GL_ES_VERSION_2_0) | |
2698 | if (core15fwd == NULL) | |
2699 | { | |
2700 | return; | |
2701 | } | |
2702 | ||
2703 | const int aNewState = !myActiveProgram.IsNull() ? GL_UPPER_LEFT : GL_LOWER_LEFT; | |
2704 | if (myPointSpriteOrig != aNewState) | |
2705 | { | |
2706 | myPointSpriteOrig = aNewState; | |
2707 | core15fwd->glPointParameteri (GL_POINT_SPRITE_COORD_ORIGIN, aNewState); | |
2708 | } | |
2709 | #endif | |
2710 | } | |
2711 | ||
7d9e854b | 2712 | // ======================================================================= |
2713 | // function : SetGlNormalizeEnabled | |
2714 | // purpose : | |
2715 | // ======================================================================= | |
2716 | Standard_Boolean OpenGl_Context::SetGlNormalizeEnabled (Standard_Boolean isEnabled) | |
2717 | { | |
2718 | if (isEnabled == myIsGlNormalizeEnabled) | |
2719 | { | |
2720 | return myIsGlNormalizeEnabled; | |
2721 | } | |
2722 | ||
2723 | Standard_Boolean anOldGlNormalize = myIsGlNormalizeEnabled; | |
7d9e854b | 2724 | myIsGlNormalizeEnabled = isEnabled; |
7d9e854b | 2725 | #if !defined(GL_ES_VERSION_2_0) |
43eddb47 | 2726 | if (core11ffp != NULL) |
7d9e854b | 2727 | { |
39070532 | 2728 | if (isEnabled) |
2729 | { | |
43eddb47 | 2730 | core11fwd->glEnable (GL_NORMALIZE); |
39070532 | 2731 | } |
2732 | else | |
2733 | { | |
43eddb47 | 2734 | core11fwd->glDisable (GL_NORMALIZE); |
39070532 | 2735 | } |
7d9e854b | 2736 | } |
2737 | #endif | |
2738 | ||
2739 | return anOldGlNormalize; | |
2740 | } | |
c827ea3a | 2741 | |
08669adf | 2742 | // ======================================================================= |
2743 | // function : SetShadeModel | |
2744 | // purpose : | |
2745 | // ======================================================================= | |
2746 | void OpenGl_Context::SetShadeModel (Graphic3d_TypeOfShadingModel theModel) | |
2747 | { | |
2748 | #if !defined(GL_ES_VERSION_2_0) | |
43eddb47 | 2749 | if (core11ffp != NULL) |
08669adf | 2750 | { |
2751 | const Standard_Integer aModel = theModel == Graphic3d_TOSM_FACET | |
2752 | || theModel == Graphic3d_TOSM_PBR_FACET ? GL_FLAT : GL_SMOOTH; | |
2753 | if (myShadeModel == aModel) | |
2754 | { | |
2755 | return; | |
2756 | } | |
2757 | myShadeModel = aModel; | |
43eddb47 | 2758 | core11ffp->glShadeModel (aModel); |
08669adf | 2759 | } |
2760 | #else | |
2761 | (void )theModel; | |
2762 | #endif | |
2763 | } | |
2764 | ||
6d0e6be5 | 2765 | // ======================================================================= |
2766 | // function : SetPolygonMode | |
2767 | // purpose : | |
2768 | // ======================================================================= | |
2769 | Standard_Integer OpenGl_Context::SetPolygonMode (const Standard_Integer theMode) | |
2770 | { | |
2771 | if (myPolygonMode == theMode) | |
2772 | { | |
2773 | return myPolygonMode; | |
2774 | } | |
2775 | ||
2776 | const Standard_Integer anOldPolygonMode = myPolygonMode; | |
2777 | ||
2778 | myPolygonMode = theMode; | |
2779 | ||
2780 | #if !defined(GL_ES_VERSION_2_0) | |
2781 | ::glPolygonMode (GL_FRONT_AND_BACK, (GLenum)theMode); | |
2782 | #endif | |
2783 | ||
2784 | return anOldPolygonMode; | |
2785 | } | |
2786 | ||
2787 | // ======================================================================= | |
2788 | // function : SetPolygonHatchEnabled | |
2789 | // purpose : | |
2790 | // ======================================================================= | |
2791 | bool OpenGl_Context::SetPolygonHatchEnabled (const bool theIsEnabled) | |
2792 | { | |
43eddb47 | 2793 | if (core11ffp == NULL) |
6d0e6be5 | 2794 | { |
2795 | return false; | |
2796 | } | |
76fada68 | 2797 | else if (myHatchIsEnabled == theIsEnabled) |
6d0e6be5 | 2798 | { |
2799 | return theIsEnabled; | |
2800 | } | |
2801 | ||
76fada68 | 2802 | const bool anOldIsEnabled = myHatchIsEnabled; |
2803 | #if !defined(GL_ES_VERSION_2_0) | |
2804 | if (theIsEnabled | |
2805 | && myActiveHatchType != Aspect_HS_SOLID) | |
2806 | { | |
2807 | core11fwd->glEnable (GL_POLYGON_STIPPLE); | |
2808 | } | |
2809 | else | |
2810 | { | |
2811 | core11fwd->glDisable (GL_POLYGON_STIPPLE); | |
2812 | } | |
2813 | #endif | |
2814 | myHatchIsEnabled = theIsEnabled; | |
2815 | return anOldIsEnabled; | |
6d0e6be5 | 2816 | } |
2817 | ||
2818 | // ======================================================================= | |
2819 | // function : SetPolygonHatchStyle | |
2820 | // purpose : | |
2821 | // ======================================================================= | |
640d5fe2 | 2822 | Standard_Integer OpenGl_Context::SetPolygonHatchStyle (const Handle(Graphic3d_HatchStyle)& theStyle) |
6d0e6be5 | 2823 | { |
76fada68 | 2824 | const Standard_Integer aNewStyle = !theStyle.IsNull() ? theStyle->HatchType() : Aspect_HS_SOLID; |
2825 | if (myActiveHatchType == aNewStyle | |
43eddb47 | 2826 | || core11ffp == NULL) |
ec7c343f | 2827 | { |
76fada68 | 2828 | return myActiveHatchType; |
ec7c343f | 2829 | } |
2830 | ||
76fada68 | 2831 | #if !defined(GL_ES_VERSION_2_0) |
2832 | if (aNewStyle == Aspect_HS_SOLID) | |
6d0e6be5 | 2833 | { |
76fada68 | 2834 | if (myHatchIsEnabled) |
6d0e6be5 | 2835 | { |
76fada68 | 2836 | core11fwd->glDisable (GL_POLYGON_STIPPLE); |
6d0e6be5 | 2837 | } |
76fada68 | 2838 | return myActiveHatchType; |
6d0e6be5 | 2839 | } |
76fada68 | 2840 | |
2841 | if (myHatchStyles.IsNull() | |
2842 | && !GetResource ("OpenGl_LineAttributes", myHatchStyles)) | |
6d0e6be5 | 2843 | { |
76fada68 | 2844 | // share and register for release once the resource is no longer used |
2845 | myHatchStyles = new OpenGl_LineAttributes(); | |
2846 | ShareResource ("OpenGl_LineAttributes", myHatchStyles); | |
6d0e6be5 | 2847 | } |
2848 | ||
76fada68 | 2849 | const Standard_Integer anOldType = myActiveHatchType; |
2850 | myActiveHatchType = aNewStyle; | |
2851 | myHatchStyles->SetTypeOfHatch (this, theStyle); | |
2852 | if (myHatchIsEnabled | |
2853 | && anOldType == Aspect_HS_SOLID) | |
2854 | { | |
2855 | core11fwd->glEnable (GL_POLYGON_STIPPLE); | |
2856 | } | |
2857 | return anOldType; | |
2858 | #else | |
2859 | return myActiveHatchType; | |
2860 | #endif | |
6d0e6be5 | 2861 | } |
2862 | ||
8d1a539c | 2863 | // ======================================================================= |
2864 | // function : SetPolygonOffset | |
2865 | // purpose : | |
2866 | // ======================================================================= | |
2867 | void OpenGl_Context::SetPolygonOffset (const Graphic3d_PolygonOffset& theOffset) | |
2868 | { | |
2869 | const bool toFillOld = (myPolygonOffset.Mode & Aspect_POM_Fill) == Aspect_POM_Fill; | |
2870 | const bool toFillNew = (theOffset.Mode & Aspect_POM_Fill) == Aspect_POM_Fill; | |
2871 | if (toFillNew != toFillOld) | |
2872 | { | |
2873 | if (toFillNew) | |
2874 | { | |
43eddb47 | 2875 | core11fwd->glEnable (GL_POLYGON_OFFSET_FILL); |
8d1a539c | 2876 | } |
2877 | else | |
2878 | { | |
43eddb47 | 2879 | core11fwd->glDisable (GL_POLYGON_OFFSET_FILL); |
8d1a539c | 2880 | } |
2881 | } | |
2882 | ||
2883 | #if !defined(GL_ES_VERSION_2_0) | |
2884 | const bool toLineOld = (myPolygonOffset.Mode & Aspect_POM_Line) == Aspect_POM_Line; | |
2885 | const bool toLineNew = (theOffset.Mode & Aspect_POM_Line) == Aspect_POM_Line; | |
2886 | if (toLineNew != toLineOld) | |
2887 | { | |
2888 | if (toLineNew) | |
2889 | { | |
43eddb47 | 2890 | core11fwd->glEnable (GL_POLYGON_OFFSET_LINE); |
8d1a539c | 2891 | } |
2892 | else | |
2893 | { | |
43eddb47 | 2894 | core11fwd->glDisable (GL_POLYGON_OFFSET_LINE); |
8d1a539c | 2895 | } |
2896 | } | |
2897 | ||
2898 | const bool toPointOld = (myPolygonOffset.Mode & Aspect_POM_Point) == Aspect_POM_Point; | |
2899 | const bool toPointNew = (theOffset.Mode & Aspect_POM_Point) == Aspect_POM_Point; | |
2900 | if (toPointNew != toPointOld) | |
2901 | { | |
2902 | if (toPointNew) | |
2903 | { | |
43eddb47 | 2904 | core11fwd->glEnable (GL_POLYGON_OFFSET_POINT); |
8d1a539c | 2905 | } |
2906 | else | |
2907 | { | |
43eddb47 | 2908 | core11fwd->glDisable (GL_POLYGON_OFFSET_POINT); |
8d1a539c | 2909 | } |
2910 | } | |
2911 | #endif | |
2912 | ||
2913 | if (myPolygonOffset.Factor != theOffset.Factor | |
2914 | || myPolygonOffset.Units != theOffset.Units) | |
2915 | { | |
43eddb47 | 2916 | core11fwd->glPolygonOffset (theOffset.Factor, theOffset.Units); |
8d1a539c | 2917 | } |
2918 | myPolygonOffset = theOffset; | |
2919 | } | |
2920 | ||
b40cdc2b | 2921 | // ======================================================================= |
2922 | // function : SetCamera | |
2923 | // purpose : | |
2924 | // ======================================================================= | |
2925 | void OpenGl_Context::SetCamera (const Handle(Graphic3d_Camera)& theCamera) | |
2926 | { | |
2927 | myCamera = theCamera; | |
2928 | if (!theCamera.IsNull()) | |
2929 | { | |
2930 | ProjectionState.SetCurrent (theCamera->ProjectionMatrixF()); | |
2931 | WorldViewState .SetCurrent (theCamera->OrientationMatrixF()); | |
2932 | ApplyProjectionMatrix(); | |
2933 | ApplyWorldViewMatrix(); | |
2934 | } | |
2935 | } | |
2936 | ||
c827ea3a | 2937 | // ======================================================================= |
2938 | // function : ApplyModelWorldMatrix | |
2939 | // purpose : | |
2940 | // ======================================================================= | |
2941 | void OpenGl_Context::ApplyModelWorldMatrix() | |
2942 | { | |
8613985b | 2943 | if (myShaderManager->ModelWorldState().ModelWorldMatrix() != ModelWorldState.Current()) |
c827ea3a | 2944 | { |
2945 | myShaderManager->UpdateModelWorldStateTo (ModelWorldState.Current()); | |
2946 | } | |
2947 | } | |
2948 | ||
2949 | // ======================================================================= | |
2950 | // function : ApplyWorldViewMatrix | |
2951 | // purpose : | |
2952 | // ======================================================================= | |
2953 | void OpenGl_Context::ApplyWorldViewMatrix() | |
2954 | { | |
8613985b | 2955 | if (myShaderManager->ModelWorldState().ModelWorldMatrix() != THE_IDENTITY_MATRIX) |
c827ea3a | 2956 | { |
8613985b | 2957 | myShaderManager->UpdateModelWorldStateTo (THE_IDENTITY_MATRIX); |
c827ea3a | 2958 | } |
8613985b | 2959 | if (myShaderManager->WorldViewState().WorldViewMatrix() != WorldViewState.Current()) |
c827ea3a | 2960 | { |
2961 | myShaderManager->UpdateWorldViewStateTo (WorldViewState.Current()); | |
2962 | } | |
2963 | } | |
2964 | ||
2965 | // ======================================================================= | |
2966 | // function : ApplyModelViewMatrix | |
2967 | // purpose : | |
2968 | // ======================================================================= | |
2969 | void OpenGl_Context::ApplyModelViewMatrix() | |
2970 | { | |
8613985b | 2971 | if (myShaderManager->ModelWorldState().ModelWorldMatrix() != ModelWorldState.Current()) |
c827ea3a | 2972 | { |
8613985b | 2973 | myShaderManager->UpdateModelWorldStateTo (ModelWorldState.Current()); |
c827ea3a | 2974 | } |
8613985b | 2975 | if (myShaderManager->WorldViewState().WorldViewMatrix() != WorldViewState.Current()) |
c827ea3a | 2976 | { |
8613985b | 2977 | myShaderManager->UpdateWorldViewStateTo (WorldViewState.Current()); |
c827ea3a | 2978 | } |
2979 | } | |
2980 | ||
2981 | // ======================================================================= | |
2982 | // function : ApplyProjectionMatrix | |
2983 | // purpose : | |
2984 | // ======================================================================= | |
2985 | void OpenGl_Context::ApplyProjectionMatrix() | |
2986 | { | |
8613985b | 2987 | if (myShaderManager->ProjectionState().ProjectionMatrix() != ProjectionState.Current()) |
c827ea3a | 2988 | { |
2989 | myShaderManager->UpdateProjectionStateTo (ProjectionState.Current()); | |
2990 | } | |
2991 | } | |
c357e426 | 2992 | |
c357e426 | 2993 | // ======================================================================= |
2994 | // function : EnableFeatures | |
2995 | // purpose : | |
2996 | // ======================================================================= | |
2997 | void OpenGl_Context::EnableFeatures() const | |
2998 | { | |
2999 | // | |
3000 | } | |
3001 | ||
3002 | // ======================================================================= | |
3003 | // function : DisableFeatures | |
3004 | // purpose : | |
3005 | // ======================================================================= | |
3006 | void OpenGl_Context::DisableFeatures() const | |
3007 | { | |
a0b49de4 | 3008 | // Disable stuff that's likely to slow down glDrawPixels. |
43eddb47 | 3009 | core11fwd->glDisable(GL_DITHER); |
3010 | core11fwd->glDisable(GL_BLEND); | |
3011 | core11fwd->glDisable(GL_DEPTH_TEST); | |
3012 | core11fwd->glDisable(GL_STENCIL_TEST); | |
c357e426 | 3013 | |
3014 | #if !defined(GL_ES_VERSION_2_0) | |
43eddb47 | 3015 | if (core11ffp == NULL) |
a0b49de4 | 3016 | { |
3017 | return; | |
3018 | } | |
3019 | ||
43eddb47 | 3020 | core11fwd->glDisable(GL_TEXTURE_1D); |
3021 | core11fwd->glDisable(GL_TEXTURE_2D); | |
a0b49de4 | 3022 | |
43eddb47 | 3023 | core11fwd->glDisable(GL_LIGHTING); |
3024 | core11fwd->glDisable(GL_ALPHA_TEST); | |
3025 | core11fwd->glDisable(GL_FOG); | |
3026 | core11fwd->glDisable(GL_LOGIC_OP); | |
c357e426 | 3027 | |
3028 | glPixelTransferi(GL_MAP_COLOR, GL_FALSE); | |
3029 | glPixelTransferi(GL_RED_SCALE, 1); | |
3030 | glPixelTransferi(GL_RED_BIAS, 0); | |
3031 | glPixelTransferi(GL_GREEN_SCALE, 1); | |
3032 | glPixelTransferi(GL_GREEN_BIAS, 0); | |
3033 | glPixelTransferi(GL_BLUE_SCALE, 1); | |
3034 | glPixelTransferi(GL_BLUE_BIAS, 0); | |
3035 | glPixelTransferi(GL_ALPHA_SCALE, 1); | |
3036 | glPixelTransferi(GL_ALPHA_BIAS, 0); | |
3037 | ||
c357e426 | 3038 | if ((myGlVerMajor >= 1) && (myGlVerMinor >= 2)) |
3039 | { | |
c357e426 | 3040 | if (CheckExtension ("GL_CONVOLUTION_1D_EXT")) |
43eddb47 | 3041 | core11fwd->glDisable(GL_CONVOLUTION_1D_EXT); |
c357e426 | 3042 | |
3043 | if (CheckExtension ("GL_CONVOLUTION_2D_EXT")) | |
43eddb47 | 3044 | core11fwd->glDisable(GL_CONVOLUTION_2D_EXT); |
c357e426 | 3045 | |
3046 | if (CheckExtension ("GL_SEPARABLE_2D_EXT")) | |
43eddb47 | 3047 | core11fwd->glDisable(GL_SEPARABLE_2D_EXT); |
c357e426 | 3048 | |
c357e426 | 3049 | if (CheckExtension ("GL_SEPARABLE_2D_EXT")) |
43eddb47 | 3050 | core11fwd->glDisable(GL_HISTOGRAM_EXT); |
c357e426 | 3051 | |
3052 | if (CheckExtension ("GL_MINMAX_EXT")) | |
43eddb47 | 3053 | core11fwd->glDisable(GL_MINMAX_EXT); |
c357e426 | 3054 | |
c357e426 | 3055 | if (CheckExtension ("GL_TEXTURE_3D_EXT")) |
43eddb47 | 3056 | core11fwd->glDisable(GL_TEXTURE_3D_EXT); |
c357e426 | 3057 | } |
3058 | #endif | |
3059 | } | |
f88457e6 | 3060 | |
31174e1a | 3061 | // ======================================================================= |
3062 | // function : SetColorMaskRGBA | |
3063 | // purpose : | |
3064 | // ======================================================================= | |
3065 | void OpenGl_Context::SetColorMaskRGBA (const NCollection_Vec4<bool>& theVal) | |
3066 | { | |
43eddb47 | 3067 | core11fwd->glColorMask (theVal.r() ? GL_TRUE : GL_FALSE, |
31174e1a | 3068 | theVal.g() ? GL_TRUE : GL_FALSE, |
3069 | theVal.b() ? GL_TRUE : GL_FALSE, | |
3070 | theVal.a() ? GL_TRUE : GL_FALSE); | |
3071 | myColorMask = theVal; | |
3072 | } | |
3073 | ||
f88457e6 | 3074 | // ======================================================================= |
3075 | // function : SetColorMask | |
3076 | // purpose : | |
3077 | // ======================================================================= | |
3078 | bool OpenGl_Context::SetColorMask (bool theToWriteColor) | |
3079 | { | |
31174e1a | 3080 | const bool anOldValue = myColorMask.r(); |
3081 | myColorMask.SetValues (theToWriteColor, theToWriteColor, theToWriteColor, caps->buffersOpaqueAlpha ? false : theToWriteColor); | |
f88457e6 | 3082 | const GLboolean toWrite = theToWriteColor ? GL_TRUE : GL_FALSE; |
43eddb47 | 3083 | core11fwd->glColorMask (toWrite, toWrite, toWrite, myColorMask.a() ? GL_TRUE : GL_FALSE); |
f88457e6 | 3084 | return anOldValue; |
3085 | } | |
c40eb6b9 | 3086 | |
3087 | // ======================================================================= | |
3088 | // function : SetSampleAlphaToCoverage | |
3089 | // purpose : | |
3090 | // ======================================================================= | |
3091 | bool OpenGl_Context::SetSampleAlphaToCoverage (bool theToEnable) | |
3092 | { | |
2a332745 | 3093 | bool toEnable = myAllowAlphaToCov && theToEnable; |
3094 | if (myAlphaToCoverage == toEnable) | |
c40eb6b9 | 3095 | { |
3096 | return myAlphaToCoverage; | |
3097 | } | |
3098 | ||
3099 | if (core15fwd != NULL) | |
3100 | { | |
2a332745 | 3101 | if (toEnable) |
c40eb6b9 | 3102 | { |
3103 | //core15fwd->core15fwd->glSampleCoverage (1.0f, GL_FALSE); | |
3104 | core15fwd->glEnable (GL_SAMPLE_ALPHA_TO_COVERAGE); | |
3105 | } | |
3106 | else | |
3107 | { | |
3108 | core15fwd->glDisable (GL_SAMPLE_ALPHA_TO_COVERAGE); | |
3109 | } | |
3110 | } | |
3111 | ||
3112 | const bool anOldValue = myAlphaToCoverage; | |
2a332745 | 3113 | myAlphaToCoverage = toEnable; |
c40eb6b9 | 3114 | return anOldValue; |
3115 | } | |
bc73b006 | 3116 | |
d4cefcc0 | 3117 | // ======================================================================= |
3118 | // function : GetBufferSubData | |
3119 | // purpose : | |
3120 | // ======================================================================= | |
3121 | bool OpenGl_Context::GetBufferSubData (GLenum theTarget, GLintptr theOffset, GLsizeiptr theSize, void* theData) | |
3122 | { | |
3123 | if (!hasGetBufferData) | |
3124 | { | |
3125 | return false; | |
3126 | } | |
3127 | #ifdef __EMSCRIPTEN__ | |
3128 | EM_ASM_( | |
3129 | { | |
3130 | Module.ctx.getBufferSubData($0, $1, HEAPU8.subarray($2, $2 + $3)); | |
3131 | }, theTarget, theOffset, theData, theSize); | |
3132 | return true; | |
3133 | #elif defined(GL_ES_VERSION_2_0) | |
43eddb47 | 3134 | if (void* aData = core30->glMapBufferRange (theTarget, theOffset, theSize, GL_MAP_READ_BIT)) |
d4cefcc0 | 3135 | { |
3136 | memcpy (theData, aData, theSize); | |
43eddb47 | 3137 | core30->glUnmapBuffer (theTarget); |
d4cefcc0 | 3138 | return true; |
3139 | } | |
3140 | return false; | |
3141 | #else | |
3142 | core15fwd->glGetBufferSubData (theTarget, theOffset, theSize, theData); | |
3143 | return true; | |
3144 | #endif | |
3145 | } | |
3146 | ||
bc73b006 | 3147 | // ======================================================================= |
3148 | // function : DumpJson | |
3149 | // purpose : | |
3150 | // ======================================================================= | |
3151 | void OpenGl_Context::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const | |
3152 | { | |
3153 | OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream) | |
3154 | ||
3155 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myAnisoMax) | |
3156 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myTexClamp) | |
3157 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myMaxTexDim) | |
3158 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myMaxTexCombined) | |
3159 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myMaxDumpSizeX) | |
3160 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myMaxDumpSizeY) | |
3161 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myMaxClipPlanes) | |
3162 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myMaxMsaaSamples) | |
3163 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myMaxDrawBuffers) | |
3164 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myMaxColorAttachments) | |
3165 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myGlVerMajor) | |
3166 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myGlVerMinor) | |
3167 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myIsInitialized) | |
3168 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myIsStereoBuffers) | |
3169 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myIsGlNormalizeEnabled) | |
3170 | ||
3171 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myHasRayTracing) | |
3172 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myHasRayTracingTextures) | |
3173 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myHasRayTracingAdaptiveSampling) | |
3174 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myHasRayTracingAdaptiveSamplingAtomic) | |
3175 | ||
3176 | for (int i = 0; i < 4; i++) | |
3177 | { | |
3178 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myViewport[i]) | |
3179 | } | |
3180 | ||
3181 | for (int i = 0; i < 4; i++) | |
3182 | { | |
3183 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myViewportVirt[i]) | |
3184 | } | |
3185 | ||
3186 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myPointSpriteOrig) | |
3187 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myRenderMode) | |
3188 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myPolygonMode) | |
3189 | OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &myPolygonOffset) | |
3190 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myToCullBackFaces) | |
3191 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myReadBuffer) | |
3192 | ||
3193 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myDefaultVao) | |
3194 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myColorMask) | |
3195 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myAllowAlphaToCov) | |
3196 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myAlphaToCoverage) | |
3197 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myIsGlDebugCtx) | |
3198 | ||
3199 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myResolution) | |
3200 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myResolutionRatio) | |
3201 | ||
3202 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myLineWidthScale) | |
3203 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myLineFeather) | |
3204 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myRenderScale) | |
3205 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myRenderScaleInv) | |
3206 | ||
3207 | OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &ModelWorldState) | |
3208 | OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &WorldViewState) | |
3209 | OCCT_DUMP_FIELD_VALUES_DUMPED (theOStream, theDepth, &ProjectionState) | |
3210 | } | |
3211 | ||
3212 | // ======================================================================= | |
3213 | // function : DumpJsonOpenGlState | |
3214 | // purpose : | |
3215 | // ======================================================================= | |
3216 | void OpenGl_Context::DumpJsonOpenGlState (Standard_OStream& theOStream, Standard_Integer) | |
3217 | { | |
3218 | GLboolean isEnableBlend = glIsEnabled (GL_BLEND); | |
3219 | GLboolean isEnableCullFace = glIsEnabled (GL_CULL_FACE); | |
3220 | GLboolean isEnableDepthTest = glIsEnabled (GL_DEPTH_TEST); | |
3221 | ||
3222 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, isEnableBlend) | |
3223 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, isEnableCullFace) | |
3224 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, isEnableDepthTest) | |
3225 | } | |
3226 |