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