1 // Created on: 2011-09-20
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
19 #include <OpenGl_GlCore11.hxx>
21 #include <Aspect_XRSession.hxx>
22 #include <Graphic3d_GraphicDriver.hxx>
23 #include <Graphic3d_StructureManager.hxx>
24 #include <Graphic3d_TextureParams.hxx>
25 #include <Graphic3d_Texture2Dmanual.hxx>
26 #include <Graphic3d_TransformUtils.hxx>
27 #include <Image_AlienPixMap.hxx>
29 #include <NCollection_Mat4.hxx>
31 #include <OpenGl_Context.hxx>
32 #include <OpenGl_FrameStats.hxx>
33 #include <OpenGl_Matrix.hxx>
34 #include <OpenGl_Workspace.hxx>
35 #include <OpenGl_View.hxx>
36 #include <OpenGl_GraduatedTrihedron.hxx>
37 #include <OpenGl_PrimitiveArray.hxx>
38 #include <OpenGl_ShaderManager.hxx>
39 #include <OpenGl_ShaderProgram.hxx>
40 #include <OpenGl_Structure.hxx>
41 #include <OpenGl_ArbFBO.hxx>
43 #include "../Textures/Textures_EnvLUT.pxx"
47 //! Format Frame Buffer format for logging messages.
48 static TCollection_AsciiString printFboFormat (const Handle(OpenGl_FrameBuffer)& theFbo)
50 return TCollection_AsciiString() + theFbo->GetInitVPSizeX() + "x" + theFbo->GetInitVPSizeY() + "@" + theFbo->NbSamples();
53 //! Return TRUE if Frame Buffer initialized has failed with the same parameters.
54 static bool checkWasFailedFbo (const Handle(OpenGl_FrameBuffer)& theFboToCheck,
55 Standard_Integer theSizeX,
56 Standard_Integer theSizeY,
57 Standard_Integer theNbSamples)
59 return !theFboToCheck->IsValid()
60 && theFboToCheck->GetInitVPSizeX() == theSizeX
61 && theFboToCheck->GetInitVPSizeY() == theSizeY
62 && theFboToCheck->NbSamples() == theNbSamples;
65 //! Return TRUE if Frame Buffer initialized has failed with the same parameters.
66 static bool checkWasFailedFbo (const Handle(OpenGl_FrameBuffer)& theFboToCheck,
67 const Handle(OpenGl_FrameBuffer)& theFboRef)
69 return checkWasFailedFbo (theFboToCheck, theFboRef->GetVPSizeX(), theFboRef->GetVPSizeY(), theFboRef->NbSamples());
73 //=======================================================================
74 //function : drawBackground
76 //=======================================================================
77 void OpenGl_View::drawBackground (const Handle(OpenGl_Workspace)& theWorkspace,
78 Graphic3d_Camera::Projection theProjection)
80 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
81 const Standard_Boolean wasUsedZBuffer = theWorkspace->SetUseZBuffer (Standard_False);
84 aCtx->core11fwd->glDisable (GL_DEPTH_TEST);
87 if (myBackgroundType == Graphic3d_TOB_CUBEMAP)
89 myCubeMapParams->Aspect()->ShaderProgram()->PushVariableInt ("uZCoeff", myBackgroundCubeMap->ZIsInverted() ? -1 : 1);
90 myCubeMapParams->Aspect()->ShaderProgram()->PushVariableInt ("uYCoeff", myBackgroundCubeMap->IsTopDown() ? 1 : -1);
91 const OpenGl_Aspects* anOldAspectFace = theWorkspace->SetAspects (myCubeMapParams);
93 myBackgrounds[Graphic3d_TOB_CUBEMAP]->Render (theWorkspace, theProjection);
95 theWorkspace->SetAspects (anOldAspectFace);
97 else if (myBackgroundType == Graphic3d_TOB_GRADIENT
98 || myBackgroundType == Graphic3d_TOB_TEXTURE)
100 // Drawing background gradient if:
101 // - gradient fill type is not Aspect_GFM_NONE and
102 // - either background texture is no specified or it is drawn in Aspect_FM_CENTERED mode
103 if (myBackgrounds[Graphic3d_TOB_GRADIENT]->IsDefined()
104 && (!myTextureParams->Aspect()->ToMapTexture()
105 || myBackgrounds[Graphic3d_TOB_TEXTURE]->TextureFillMethod() == Aspect_FM_CENTERED
106 || myBackgrounds[Graphic3d_TOB_TEXTURE]->TextureFillMethod() == Aspect_FM_NONE))
108 myBackgrounds[Graphic3d_TOB_GRADIENT]->Render(theWorkspace, theProjection);
111 // Drawing background image if it is defined
112 // (texture is defined and fill type is not Aspect_FM_NONE)
113 if (myBackgrounds[Graphic3d_TOB_TEXTURE]->IsDefined()
114 && myTextureParams->Aspect()->ToMapTexture())
116 aCtx->core11fwd->glDisable (GL_BLEND);
118 const OpenGl_Aspects* anOldAspectFace = theWorkspace->SetAspects (myTextureParams);
119 myBackgrounds[Graphic3d_TOB_TEXTURE]->Render (theWorkspace, theProjection);
120 theWorkspace->SetAspects (anOldAspectFace);
126 theWorkspace->SetUseZBuffer (Standard_True);
127 aCtx->core11fwd->glEnable (GL_DEPTH_TEST);
131 //=======================================================================
132 //function : prepareFrameBuffers
134 //=======================================================================
135 bool OpenGl_View::prepareFrameBuffers (Graphic3d_Camera::Projection& theProj)
137 theProj = myCamera->ProjectionType();
138 const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext();
140 Standard_Integer aSizeX = 0, aSizeY = 0;
141 OpenGl_FrameBuffer* aFrameBuffer = myFBO.get();
142 if (aFrameBuffer != NULL)
144 aSizeX = aFrameBuffer->GetVPSizeX();
145 aSizeY = aFrameBuffer->GetVPSizeY();
147 else if (IsActiveXR())
149 aSizeX = myXRSession->RecommendedViewport().x();
150 aSizeY = myXRSession->RecommendedViewport().y();
154 aSizeX = myWindow->Width();
155 aSizeY = myWindow->Height();
158 const Standard_Integer aRendSizeX = Standard_Integer(myRenderParams.RenderResolutionScale * aSizeX + 0.5f);
159 const Standard_Integer aRendSizeY = Standard_Integer(myRenderParams.RenderResolutionScale * aSizeY + 0.5f);
165 myBackBufferRestored = Standard_False;
166 myIsImmediateDrawn = Standard_False;
170 // determine multisampling parameters
171 Standard_Integer aNbSamples = !myToDisableMSAA && aSizeX == aRendSizeX
172 ? Max (Min (myRenderParams.NbMsaaSamples, aCtx->MaxMsaaSamples()), 0)
176 aNbSamples = OpenGl_Context::GetPowerOfTwo (aNbSamples, aCtx->MaxMsaaSamples());
179 bool toUseOit = myRenderParams.TransparencyMethod == Graphic3d_RTM_BLEND_OIT
180 && checkOitCompatibility (aCtx, aNbSamples > 0);
182 const bool toInitImmediateFbo = myTransientDrawToFront
183 && (!aCtx->caps->useSystemBuffer || (toUseOit && HasImmediateStructures()));
185 if ( aFrameBuffer == NULL
186 && !aCtx->DefaultFrameBuffer().IsNull()
187 && aCtx->DefaultFrameBuffer()->IsValid())
189 aFrameBuffer = aCtx->DefaultFrameBuffer().operator->();
193 && (myTransientDrawToFront
194 || theProj == Graphic3d_Camera::Projection_Stereo
197 || aSizeX != aRendSizeX))
199 if (myMainSceneFbos[0]->GetVPSizeX() != aRendSizeX
200 || myMainSceneFbos[0]->GetVPSizeY() != aRendSizeY
201 || myMainSceneFbos[0]->NbSamples() != aNbSamples)
203 if (!myTransientDrawToFront)
205 myImmediateSceneFbos[0]->Release (aCtx.operator->());
206 myImmediateSceneFbos[1]->Release (aCtx.operator->());
207 myImmediateSceneFbos[0]->ChangeViewport (0, 0);
208 myImmediateSceneFbos[1]->ChangeViewport (0, 0);
211 // prepare FBOs containing main scene
212 // for further blitting and rendering immediate presentations on top
213 if (aCtx->core20fwd != NULL)
215 const bool wasFailedMain0 = checkWasFailedFbo (myMainSceneFbos[0], aRendSizeX, aRendSizeY, aNbSamples);
216 if (!myMainSceneFbos[0]->Init (aCtx, aRendSizeX, aRendSizeY, myFboColorFormat, myFboDepthFormat, aNbSamples)
219 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "Error! Main FBO "
220 + printFboFormat (myMainSceneFbos[0]) + " initialization has failed";
221 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
225 if (myMainSceneFbos[0]->IsValid() && (toInitImmediateFbo || myImmediateSceneFbos[0]->IsValid()))
227 const bool wasFailedImm0 = checkWasFailedFbo (myImmediateSceneFbos[0], myMainSceneFbos[0]);
228 if (!myImmediateSceneFbos[0]->InitLazy (aCtx, *myMainSceneFbos[0])
231 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "Error! Immediate FBO "
232 + printFboFormat (myImmediateSceneFbos[0]) + " initialization has failed";
233 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
239 myMainSceneFbos [0]->Release (aCtx.operator->());
240 myMainSceneFbos [1]->Release (aCtx.operator->());
241 myImmediateSceneFbos[0]->Release (aCtx.operator->());
242 myImmediateSceneFbos[1]->Release (aCtx.operator->());
243 myXrSceneFbo ->Release (aCtx.operator->());
244 myMainSceneFbos [0]->ChangeViewport (0, 0);
245 myMainSceneFbos [1]->ChangeViewport (0, 0);
246 myImmediateSceneFbos[0]->ChangeViewport (0, 0);
247 myImmediateSceneFbos[1]->ChangeViewport (0, 0);
248 myXrSceneFbo ->ChangeViewport (0, 0);
251 bool hasXRBlitFbo = false;
252 if (theProj == Graphic3d_Camera::Projection_Stereo
254 && myMainSceneFbos[0]->IsValid())
257 || aSizeX != aRendSizeX)
259 hasXRBlitFbo = myXrSceneFbo->InitLazy (aCtx, aSizeX, aSizeY, myFboColorFormat, myFboDepthFormat, 0);
262 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "Error! VR FBO "
263 + printFboFormat (myXrSceneFbo) + " initialization has failed";
264 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
268 else if (theProj == Graphic3d_Camera::Projection_Stereo
269 && myMainSceneFbos[0]->IsValid())
271 const bool wasFailedMain1 = checkWasFailedFbo (myMainSceneFbos[1], myMainSceneFbos[0]);
272 if (!myMainSceneFbos[1]->InitLazy (aCtx, *myMainSceneFbos[0])
275 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "Error! Main FBO (second) "
276 + printFboFormat (myMainSceneFbos[1]) + " initialization has failed";
277 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
279 if (!myMainSceneFbos[1]->IsValid())
282 theProj = Graphic3d_Camera::Projection_Perspective;
284 else if (!myTransientDrawToFront)
288 else if (!aCtx->HasStereoBuffers()
289 || myRenderParams.StereoMode != Graphic3d_StereoMode_QuadBuffer)
291 const bool wasFailedImm0 = checkWasFailedFbo (myImmediateSceneFbos[0], myMainSceneFbos[0]);
292 const bool wasFailedImm1 = checkWasFailedFbo (myImmediateSceneFbos[1], myMainSceneFbos[0]);
293 if (!myImmediateSceneFbos[0]->InitLazy (aCtx, *myMainSceneFbos[0])
296 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "Error! Immediate FBO (first) "
297 + printFboFormat (myImmediateSceneFbos[0]) + " initialization has failed";
298 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
300 if (!myImmediateSceneFbos[1]->InitLazy (aCtx, *myMainSceneFbos[0])
303 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "Error! Immediate FBO (first) "
304 + printFboFormat (myImmediateSceneFbos[1]) + " initialization has failed";
305 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
307 if (!myImmediateSceneFbos[0]->IsValid()
308 || !myImmediateSceneFbos[1]->IsValid())
310 theProj = Graphic3d_Camera::Projection_Perspective;
316 myXrSceneFbo->Release (aCtx.get());
317 myXrSceneFbo->ChangeViewport (0, 0);
320 // process PBR environment
321 if (myShadingModel == Graphic3d_TOSM_PBR
322 || myShadingModel == Graphic3d_TOSM_PBR_FACET)
324 if (!myPBREnvironment.IsNull()
325 && myPBREnvironment->SizesAreDifferent (myRenderParams.PbrEnvPow2Size,
326 myRenderParams.PbrEnvSpecMapNbLevels))
328 myPBREnvironment->Release (aCtx.get());
329 myPBREnvironment.Nullify();
330 myPBREnvState = OpenGl_PBREnvState_NONEXISTENT;
331 myPBREnvRequest = OpenGl_PBREnvRequest_BAKE;
335 if (myPBREnvState == OpenGl_PBREnvState_NONEXISTENT
338 myPBREnvironment = OpenGl_PBREnvironment::Create (aCtx, myRenderParams.PbrEnvPow2Size, myRenderParams.PbrEnvSpecMapNbLevels);
339 myPBREnvState = myPBREnvironment.IsNull() ? OpenGl_PBREnvState_UNAVAILABLE : OpenGl_PBREnvState_CREATED;
340 if (myPBREnvState == OpenGl_PBREnvState_CREATED)
342 Handle(OpenGl_Texture) anEnvLUT;
343 static const TCollection_AsciiString THE_SHARED_ENV_LUT_KEY("EnvLUT");
344 if (!aCtx->GetResource (THE_SHARED_ENV_LUT_KEY, anEnvLUT))
346 Handle(Graphic3d_TextureParams) aParams = new Graphic3d_TextureParams();
347 aParams->SetFilter (Graphic3d_TOTF_BILINEAR);
348 aParams->SetRepeat (Standard_False);
349 aParams->SetTextureUnit (aCtx->PBREnvLUTTexUnit());
350 anEnvLUT = new OpenGl_Texture(THE_SHARED_ENV_LUT_KEY, aParams);
351 Handle(Image_PixMap) aPixMap = new Image_PixMap();
352 aPixMap->InitWrapper (Image_Format_RGF, (Standard_Byte*)Textures_EnvLUT, Textures_EnvLUTSize, Textures_EnvLUTSize);
353 OpenGl_TextureFormat aTexFormat = OpenGl_TextureFormat::FindFormat (aCtx, aPixMap->Format(), false);
354 #if defined(GL_ES_VERSION_2_0)
355 // GL_RG32F is not texture-filterable format on OpenGL ES without OES_texture_float_linear extension.
356 // GL_RG16F is texture-filterable since OpenGL ES 3.0 and can be initialized from 32-bit floats.
357 // Note that it is expected that GL_RG16F has enough precision for this table, so that it can be used also on desktop OpenGL.
358 //if (!aCtx->hasTexFloatLinear)
359 aTexFormat.SetInternalFormat (GL_RG16F);
361 if (!aTexFormat.IsValid()
362 || !anEnvLUT->Init (aCtx, aTexFormat, Graphic3d_Vec2i((Standard_Integer)Textures_EnvLUTSize), Graphic3d_TOT_2D, aPixMap.get()))
364 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, "Failed allocation of LUT for PBR");
367 aCtx->ShareResource (THE_SHARED_ENV_LUT_KEY, anEnvLUT);
369 if (!anEnvLUT.IsNull())
371 anEnvLUT->Bind (aCtx);
373 myWorkspace->ApplyAspects();
376 processPBREnvRequest (aCtx);
379 // create color and coverage accumulation buffers required for OIT algorithm
382 Standard_Integer anFboIt = 0;
383 for (; anFboIt < 2; ++anFboIt)
385 Handle(OpenGl_FrameBuffer)& aMainSceneFbo = myMainSceneFbos [anFboIt];
386 Handle(OpenGl_FrameBuffer)& aMainSceneFboOit = myMainSceneFbosOit [anFboIt];
387 Handle(OpenGl_FrameBuffer)& anImmediateSceneFbo = myImmediateSceneFbos [anFboIt];
388 Handle(OpenGl_FrameBuffer)& anImmediateSceneFboOit = myImmediateSceneFbosOit[anFboIt];
389 if (aMainSceneFbo->IsValid()
390 && (aMainSceneFboOit->GetVPSizeX() != aRendSizeX
391 || aMainSceneFboOit->GetVPSizeY() != aRendSizeY
392 || aMainSceneFboOit->NbSamples() != aNbSamples))
394 Standard_Integer aColorConfig = 0;
395 for (;;) // seemly responding to driver limitation (GL_FRAMEBUFFER_UNSUPPORTED)
397 if (myFboOitColorConfig.IsEmpty())
399 if (!chooseOitColorConfiguration (aCtx, aColorConfig++, myFboOitColorConfig))
404 if (aMainSceneFboOit->Init (aCtx, aRendSizeX, aRendSizeY, myFboOitColorConfig, aMainSceneFbo->DepthStencilTexture(), aNbSamples))
408 myFboOitColorConfig.Clear();
410 if (!aMainSceneFboOit->IsValid())
415 else if (!aMainSceneFbo->IsValid())
417 aMainSceneFboOit->Release (aCtx.operator->());
418 aMainSceneFboOit->ChangeViewport (0, 0);
421 if (anImmediateSceneFbo->IsValid()
422 && (anImmediateSceneFboOit->GetVPSizeX() != aRendSizeX
423 || anImmediateSceneFboOit->GetVPSizeY() != aRendSizeY
424 || anImmediateSceneFboOit->NbSamples() != aNbSamples))
426 if (!anImmediateSceneFboOit->Init (aCtx, aRendSizeX, aRendSizeY, myFboOitColorConfig,
427 anImmediateSceneFbo->DepthStencilTexture(), aNbSamples))
432 else if (!anImmediateSceneFbo->IsValid())
434 anImmediateSceneFboOit->Release (aCtx.operator->());
435 anImmediateSceneFboOit->ChangeViewport (0, 0);
438 if (anFboIt == 0) // only the first OIT framebuffer is mandatory
440 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH,
441 "Initialization of float texture framebuffer for use with\n"
442 " blended order-independent transparency rendering algorithm has failed.\n"
443 " Blended order-independent transparency will not be available.\n");
446 myToDisableOITMSAA = Standard_True;
450 myToDisableOIT = Standard_True;
455 if (!toUseOit && myMainSceneFbosOit[0]->IsValid())
457 myMainSceneFbosOit [0]->Release (aCtx.operator->());
458 myMainSceneFbosOit [1]->Release (aCtx.operator->());
459 myImmediateSceneFbosOit[0]->Release (aCtx.operator->());
460 myImmediateSceneFbosOit[1]->Release (aCtx.operator->());
461 myMainSceneFbosOit [0]->ChangeViewport (0, 0);
462 myMainSceneFbosOit [1]->ChangeViewport (0, 0);
463 myImmediateSceneFbosOit[0]->ChangeViewport (0, 0);
464 myImmediateSceneFbosOit[1]->ChangeViewport (0, 0);
470 //=======================================================================
473 //=======================================================================
474 void OpenGl_View::Redraw()
476 const Standard_Boolean wasDisabledMSAA = myToDisableMSAA;
477 const Standard_Boolean hadFboBlit = myHasFboBlit;
478 if (myRenderParams.Method == Graphic3d_RM_RAYTRACING
479 && !myCaps->vboDisable
480 && !myCaps->keepArrayData)
482 // caps are shared across all views, thus we need to invalidate all of them
483 // if (myWasRedrawnGL) { myStructureManager->SetDeviceLost(); }
484 myDriver->setDeviceLost();
485 myCaps->keepArrayData = Standard_True;
488 if (!myWorkspace->Activate())
493 // implicitly disable VSync when using HMD composer (can be mirrored in window for debugging)
494 myWindow->SetSwapInterval (IsActiveXR());
497 const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext();
498 aCtx->FrameStats()->FrameStart (myWorkspace->View(), false);
499 aCtx->SetLineFeather (myRenderParams.LineFeather);
501 const Standard_Integer anSRgbState = aCtx->ToRenderSRGB() ? 1 : 0;
502 if (mySRgbState != -1
503 && mySRgbState != anSRgbState)
505 releaseSrgbResources (aCtx);
506 initTextureEnv (aCtx);
508 mySRgbState = anSRgbState;
509 aCtx->ShaderManager()->UpdateSRgbState();
511 // release pending GL resources
512 aCtx->ReleaseDelayed();
514 // fetch OpenGl context state
517 const Graphic3d_StereoMode aStereoMode = myRenderParams.StereoMode;
518 Graphic3d_Camera::Projection aProjectType = myCamera->ProjectionType();
519 if (!prepareFrameBuffers (aProjectType))
521 myBackBufferRestored = Standard_False;
522 myIsImmediateDrawn = Standard_False;
526 OpenGl_FrameBuffer* aFrameBuffer = myFBO.get();
527 bool toSwap = aCtx->IsRender()
528 && !aCtx->caps->buffersNoSwap
529 && aFrameBuffer == NULL
530 && (!IsActiveXR() || myRenderParams.ToMirrorComposer);
531 if ( aFrameBuffer == NULL
532 && !aCtx->DefaultFrameBuffer().IsNull()
533 && aCtx->DefaultFrameBuffer()->IsValid())
535 aFrameBuffer = aCtx->DefaultFrameBuffer().operator->();
538 if (aProjectType == Graphic3d_Camera::Projection_Stereo)
540 OpenGl_FrameBuffer* aMainFbos[2] =
542 myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL,
543 myMainSceneFbos[1]->IsValid() ? myMainSceneFbos[1].operator->() : NULL
545 OpenGl_FrameBuffer* aMainFbosOit[2] =
547 myMainSceneFbosOit[0]->IsValid() ? myMainSceneFbosOit[0].operator->() : NULL,
548 myMainSceneFbosOit[1]->IsValid() ? myMainSceneFbosOit[1].operator->() :
549 myMainSceneFbosOit[0]->IsValid() ? myMainSceneFbosOit[0].operator->() : NULL
552 OpenGl_FrameBuffer* anImmFbos[2] =
554 myImmediateSceneFbos[0]->IsValid() ? myImmediateSceneFbos[0].operator->() : NULL,
555 myImmediateSceneFbos[1]->IsValid() ? myImmediateSceneFbos[1].operator->() : NULL
557 OpenGl_FrameBuffer* anImmFbosOit[2] =
559 myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL,
560 myImmediateSceneFbosOit[1]->IsValid() ? myImmediateSceneFbosOit[1].operator->() :
561 myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL
566 // use single frame for both views - caching main scene content makes no sense
567 // when head position is expected to be updated each frame redraw with high accuracy
568 aMainFbos[1] = aMainFbos[0];
569 aMainFbosOit[1] = aMainFbosOit[0];
570 anImmFbos[0] = aMainFbos[0];
571 anImmFbos[1] = aMainFbos[1];
572 anImmFbosOit[0] = aMainFbosOit[0];
573 anImmFbosOit[1] = aMainFbosOit[1];
575 else if (!myTransientDrawToFront)
577 anImmFbos [0] = aMainFbos [0];
578 anImmFbos [1] = aMainFbos [1];
579 anImmFbosOit[0] = aMainFbosOit[0];
580 anImmFbosOit[1] = aMainFbosOit[1];
582 else if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip
583 || aStereoMode == Graphic3d_StereoMode_QuadBuffer)
585 anImmFbos [0] = NULL;
586 anImmFbos [1] = NULL;
587 anImmFbosOit[0] = NULL;
588 anImmFbosOit[1] = NULL;
591 #if !defined(GL_ES_VERSION_2_0)
592 aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_LEFT : GL_BACK);
594 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
595 aMainFbos[0] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
597 redraw (Graphic3d_Camera::Projection_MonoLeftEye, aMainFbos[0], aMainFbosOit[0]);
598 myBackBufferRestored = Standard_True;
599 myIsImmediateDrawn = Standard_False;
600 #if !defined(GL_ES_VERSION_2_0)
601 aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_LEFT : GL_BACK);
603 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
604 anImmFbos[0] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
605 if (!redrawImmediate (Graphic3d_Camera::Projection_MonoLeftEye, aMainFbos[0], anImmFbos[0], anImmFbosOit[0]))
609 else if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip && toSwap)
616 // push Left frame to HMD display composer
617 OpenGl_FrameBuffer* anXRFbo = myXrSceneFbo->IsValid() ? myXrSceneFbo.get() : aMainFbos[0];
618 if (anXRFbo != aMainFbos[0])
620 blitBuffers (aMainFbos[0], anXRFbo); // resize or resolve MSAA samples
622 #if !defined(GL_ES_VERSION_2_0)
623 const Aspect_GraphicsLibrary aGraphicsLib = Aspect_GraphicsLibrary_OpenGL;
625 const Aspect_GraphicsLibrary aGraphicsLib = Aspect_GraphicsLibrary_OpenGLES;
627 myXRSession->SubmitEye ((void* )(size_t )anXRFbo->ColorTexture()->TextureId(),
628 aGraphicsLib, Aspect_ColorSpace_sRGB, Aspect_Eye_Left);
631 #if !defined(GL_ES_VERSION_2_0)
632 aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_RIGHT : GL_BACK);
634 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
635 aMainFbos[1] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
637 redraw (Graphic3d_Camera::Projection_MonoRightEye, aMainFbos[1], aMainFbosOit[1]);
638 myBackBufferRestored = Standard_True;
639 myIsImmediateDrawn = Standard_False;
640 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
641 anImmFbos[1] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
642 if (!redrawImmediate (Graphic3d_Camera::Projection_MonoRightEye, aMainFbos[1], anImmFbos[1], anImmFbosOit[1]))
649 // push Right frame to HMD display composer
650 OpenGl_FrameBuffer* anXRFbo = myXrSceneFbo->IsValid() ? myXrSceneFbo.get() : aMainFbos[1];
651 if (anXRFbo != aMainFbos[1])
653 blitBuffers (aMainFbos[1], anXRFbo); // resize or resolve MSAA samples
655 #if !defined(GL_ES_VERSION_2_0)
656 const Aspect_GraphicsLibrary aGraphicsLib = Aspect_GraphicsLibrary_OpenGL;
658 const Aspect_GraphicsLibrary aGraphicsLib = Aspect_GraphicsLibrary_OpenGLES;
660 myXRSession->SubmitEye ((void* )(size_t )anXRFbo->ColorTexture()->TextureId(),
661 aGraphicsLib, Aspect_ColorSpace_sRGB, Aspect_Eye_Right);
664 if (myRenderParams.ToMirrorComposer)
666 blitBuffers (anXRFbo, aFrameBuffer, myToFlipOutput);
669 else if (anImmFbos[0] != NULL)
671 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(), 1.0f);
672 drawStereoPair (aFrameBuffer);
677 OpenGl_FrameBuffer* aMainFbo = myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : aFrameBuffer;
678 OpenGl_FrameBuffer* aMainFboOit = myMainSceneFbosOit[0]->IsValid() ? myMainSceneFbosOit[0].operator->() : NULL;
679 OpenGl_FrameBuffer* anImmFbo = aFrameBuffer;
680 OpenGl_FrameBuffer* anImmFboOit = NULL;
681 if (!myTransientDrawToFront)
684 anImmFboOit = aMainFboOit;
686 else if (myImmediateSceneFbos[0]->IsValid())
688 anImmFbo = myImmediateSceneFbos[0].operator->();
689 anImmFboOit = myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL;
692 #if !defined(GL_ES_VERSION_2_0)
693 if (aMainFbo == NULL)
695 aCtx->SetReadDrawBuffer (GL_BACK);
698 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
699 aMainFbo != aFrameBuffer ? myRenderParams.RenderResolutionScale : 1.0f);
701 redraw (aProjectType, aMainFbo, aMainFboOit);
702 myBackBufferRestored = Standard_True;
703 myIsImmediateDrawn = Standard_False;
704 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
705 anImmFbo != aFrameBuffer ? myRenderParams.RenderResolutionScale : 1.0f);
706 if (!redrawImmediate (aProjectType, aMainFbo, anImmFbo, anImmFboOit))
712 && anImmFbo != aFrameBuffer)
714 blitBuffers (anImmFbo, aFrameBuffer, myToFlipOutput);
718 if (myRenderParams.Method == Graphic3d_RM_RAYTRACING
719 && myRenderParams.IsGlobalIlluminationEnabled)
727 if (wasDisabledMSAA != myToDisableMSAA
728 || hadFboBlit != myHasFboBlit)
734 // reset state for safety
735 aCtx->BindProgram (Handle(OpenGl_ShaderProgram)());
736 if (aCtx->caps->ffpEnable)
738 aCtx->ShaderManager()->PushState (Handle(OpenGl_ShaderProgram)());
745 if (!myMainSceneFbos[0]->IsValid())
747 myBackBufferRestored = Standard_False;
752 aCtx->core11fwd->glFlush();
755 // reset render mode state
757 aCtx->FrameStats()->FrameEnd (myWorkspace->View(), false);
759 myWasRedrawnGL = Standard_True;
762 // =======================================================================
763 // function : RedrawImmediate
765 // =======================================================================
766 void OpenGl_View::RedrawImmediate()
768 if (!myWorkspace->Activate())
771 // no special handling of HMD display, since it will force full Redraw() due to no frame caching (myBackBufferRestored)
772 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
773 if (!myTransientDrawToFront
774 || !myBackBufferRestored
775 || (aCtx->caps->buffersNoSwap && !myMainSceneFbos[0]->IsValid()))
781 const Graphic3d_StereoMode aStereoMode = myRenderParams.StereoMode;
782 Graphic3d_Camera::Projection aProjectType = myCamera->ProjectionType();
783 OpenGl_FrameBuffer* aFrameBuffer = myFBO.get();
784 aCtx->FrameStats()->FrameStart (myWorkspace->View(), true);
786 if ( aFrameBuffer == NULL
787 && !aCtx->DefaultFrameBuffer().IsNull()
788 && aCtx->DefaultFrameBuffer()->IsValid())
790 aFrameBuffer = aCtx->DefaultFrameBuffer().operator->();
793 if (aProjectType == Graphic3d_Camera::Projection_Stereo)
795 if (myMainSceneFbos[0]->IsValid()
796 && !myMainSceneFbos[1]->IsValid())
798 aProjectType = Graphic3d_Camera::Projection_Perspective;
803 if (aProjectType == Graphic3d_Camera::Projection_Stereo)
805 OpenGl_FrameBuffer* aMainFbos[2] =
807 myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL,
808 myMainSceneFbos[1]->IsValid() ? myMainSceneFbos[1].operator->() : NULL
810 OpenGl_FrameBuffer* anImmFbos[2] =
812 myImmediateSceneFbos[0]->IsValid() ? myImmediateSceneFbos[0].operator->() : NULL,
813 myImmediateSceneFbos[1]->IsValid() ? myImmediateSceneFbos[1].operator->() : NULL
815 OpenGl_FrameBuffer* anImmFbosOit[2] =
817 myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL,
818 myImmediateSceneFbosOit[1]->IsValid() ? myImmediateSceneFbosOit[1].operator->() :
819 myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL
821 if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip
822 || aStereoMode == Graphic3d_StereoMode_QuadBuffer)
826 anImmFbosOit[0] = NULL;
827 anImmFbosOit[1] = NULL;
830 if (aCtx->arbFBO != NULL)
832 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
834 #if !defined(GL_ES_VERSION_2_0)
835 if (anImmFbos[0] == NULL)
837 aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_LEFT : GL_BACK);
841 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
842 anImmFbos[0] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
843 toSwap = redrawImmediate (Graphic3d_Camera::Projection_MonoLeftEye,
847 Standard_True) || toSwap;
848 if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip
850 && !aCtx->caps->buffersNoSwap)
855 if (aCtx->arbFBO != NULL)
857 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
859 #if !defined(GL_ES_VERSION_2_0)
860 if (anImmFbos[1] == NULL)
862 aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_RIGHT : GL_BACK);
865 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
866 anImmFbos[1] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
867 toSwap = redrawImmediate (Graphic3d_Camera::Projection_MonoRightEye,
871 Standard_True) || toSwap;
872 if (anImmFbos[0] != NULL)
874 drawStereoPair (aFrameBuffer);
879 OpenGl_FrameBuffer* aMainFbo = myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL;
880 OpenGl_FrameBuffer* anImmFbo = aFrameBuffer;
881 OpenGl_FrameBuffer* anImmFboOit = NULL;
882 if (myImmediateSceneFbos[0]->IsValid())
884 anImmFbo = myImmediateSceneFbos[0].operator->();
885 anImmFboOit = myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL;
887 #if !defined(GL_ES_VERSION_2_0)
888 if (aMainFbo == NULL)
890 aCtx->SetReadDrawBuffer (GL_BACK);
893 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
894 anImmFbo != aFrameBuffer ? myRenderParams.RenderResolutionScale : 1.0f);
895 toSwap = redrawImmediate (aProjectType,
899 Standard_True) || toSwap;
901 && anImmFbo != aFrameBuffer)
903 blitBuffers (anImmFbo, aFrameBuffer, myToFlipOutput);
910 // reset state for safety
911 aCtx->BindProgram (Handle(OpenGl_ShaderProgram)());
912 if (aCtx->caps->ffpEnable)
914 aCtx->ShaderManager()->PushState (Handle(OpenGl_ShaderProgram)());
917 if (toSwap && !aCtx->caps->buffersNoSwap)
923 aCtx->core11fwd->glFlush();
925 aCtx->FrameStats()->FrameEnd (myWorkspace->View(), true);
927 myWasRedrawnGL = Standard_True;
930 // =======================================================================
933 // =======================================================================
934 void OpenGl_View::redraw (const Graphic3d_Camera::Projection theProjection,
935 OpenGl_FrameBuffer* theReadDrawFbo,
936 OpenGl_FrameBuffer* theOitAccumFbo)
938 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
939 if (theReadDrawFbo != NULL)
941 theReadDrawFbo->BindBuffer (aCtx);
942 theReadDrawFbo->SetupViewport (aCtx);
946 const Standard_Integer aViewport[4] = { 0, 0, myWindow->Width(), myWindow->Height() };
947 aCtx->ResizeViewport (aViewport);
950 // request reset of material
951 aCtx->ShaderManager()->UpdateMaterialState();
953 myWorkspace->UseZBuffer() = Standard_True;
954 myWorkspace->UseDepthWrite() = Standard_True;
955 GLbitfield toClear = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT;
956 aCtx->core11fwd->glDepthFunc (GL_LEQUAL);
957 aCtx->core11fwd->glDepthMask (GL_TRUE);
958 aCtx->core11fwd->glEnable (GL_DEPTH_TEST);
960 aCtx->core11fwd->glClearDepth (1.0);
962 const OpenGl_Vec4 aBgColor = aCtx->Vec4FromQuantityColor (myBgColor);
963 aCtx->SetColorMaskRGBA (NCollection_Vec4<bool> (true)); // force writes into all components, including alpha
964 aCtx->core11fwd->glClearColor (aBgColor.r(), aBgColor.g(), aBgColor.b(), aCtx->caps->buffersOpaqueAlpha ? 1.0f : 0.0f);
965 aCtx->core11fwd->glClear (toClear);
966 aCtx->SetColorMask (true); // restore default alpha component write state
968 render (theProjection, theReadDrawFbo, theOitAccumFbo, Standard_False);
971 // =======================================================================
972 // function : redrawMonoImmediate
974 // =======================================================================
975 bool OpenGl_View::redrawImmediate (const Graphic3d_Camera::Projection theProjection,
976 OpenGl_FrameBuffer* theReadFbo,
977 OpenGl_FrameBuffer* theDrawFbo,
978 OpenGl_FrameBuffer* theOitAccumFbo,
979 const Standard_Boolean theIsPartialUpdate)
981 const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext();
982 GLboolean toCopyBackToFront = GL_FALSE;
983 if (theDrawFbo == theReadFbo
984 && theDrawFbo != NULL
985 && theDrawFbo->IsValid())
987 myBackBufferRestored = Standard_False;
988 theDrawFbo->BindBuffer (aCtx);
990 else if (theReadFbo != NULL
991 && theReadFbo->IsValid()
994 if (!blitBuffers (theReadFbo, theDrawFbo))
999 else if (theDrawFbo == NULL)
1001 #if !defined(GL_ES_VERSION_2_0)
1002 aCtx->core11fwd->glGetBooleanv (GL_DOUBLEBUFFER, &toCopyBackToFront);
1004 if (toCopyBackToFront
1005 && myTransientDrawToFront)
1007 if (!HasImmediateStructures()
1008 && !theIsPartialUpdate)
1010 // prefer Swap Buffers within Redraw in compatibility mode (without FBO)
1013 if (!copyBackToFront())
1015 toCopyBackToFront = GL_FALSE;
1016 myBackBufferRestored = Standard_False;
1021 toCopyBackToFront = GL_FALSE;
1022 myBackBufferRestored = Standard_False;
1027 myBackBufferRestored = Standard_False;
1029 myIsImmediateDrawn = Standard_True;
1031 myWorkspace->UseZBuffer() = Standard_True;
1032 myWorkspace->UseDepthWrite() = Standard_True;
1033 glDepthFunc (GL_LEQUAL);
1034 glDepthMask (GL_TRUE);
1035 glEnable (GL_DEPTH_TEST);
1036 #if !defined(GL_ES_VERSION_2_0)
1039 glClearDepthf (1.0f);
1042 render (theProjection, theDrawFbo, theOitAccumFbo, Standard_True);
1044 return !toCopyBackToFront;
1047 //=======================================================================
1050 //=======================================================================
1051 void OpenGl_View::render (Graphic3d_Camera::Projection theProjection,
1052 OpenGl_FrameBuffer* theOutputFBO,
1053 OpenGl_FrameBuffer* theOitAccumFbo,
1054 const Standard_Boolean theToDrawImmediate)
1056 // ==================================
1057 // Step 1: Prepare for render
1058 // ==================================
1060 const Handle(OpenGl_Context)& aContext = myWorkspace->GetGlContext();
1061 aContext->SetAllowSampleAlphaToCoverage (myRenderParams.ToEnableAlphaToCoverage
1062 && theOutputFBO != NULL
1063 && theOutputFBO->NbSamples() != 0);
1065 #if !defined(GL_ES_VERSION_2_0)
1066 // Disable current clipping planes
1067 if (aContext->core11 != NULL)
1069 const Standard_Integer aMaxPlanes = aContext->MaxClipPlanes();
1070 for (Standard_Integer aClipPlaneId = GL_CLIP_PLANE0; aClipPlaneId < GL_CLIP_PLANE0 + aMaxPlanes; ++aClipPlaneId)
1072 aContext->core11fwd->glDisable (aClipPlaneId);
1077 // update states of OpenGl_BVHTreeSelector (frustum culling algorithm);
1078 // note that we pass here window dimensions ignoring Graphic3d_RenderingParams::RenderResolutionScale
1079 myBVHSelector.SetViewVolume (myCamera);
1080 myBVHSelector.SetViewportSize (myWindow->Width(), myWindow->Height(), myRenderParams.ResolutionRatio());
1081 myBVHSelector.CacheClipPtsProjections();
1083 const Handle(OpenGl_ShaderManager)& aManager = aContext->ShaderManager();
1084 const Handle(Graphic3d_LightSet)& aLights = myShadingModel == Graphic3d_TOSM_UNLIT ? myNoShadingLight : myLights;
1085 Standard_Size aLightsRevision = 0;
1086 if (!aLights.IsNull())
1088 aLightsRevision = aLights->UpdateRevision();
1090 if (StateInfo (myCurrLightSourceState, aManager->LightSourceState().Index()) != myLastLightSourceState
1091 || aLightsRevision != myLightsRevision)
1093 myLightsRevision = aLightsRevision;
1094 aManager->UpdateLightSourceStateTo (aLights, SpecIBLMapLevels());
1095 myLastLightSourceState = StateInfo (myCurrLightSourceState, aManager->LightSourceState().Index());
1098 // Update matrices if camera has changed.
1099 Graphic3d_WorldViewProjState aWVPState = myCamera->WorldViewProjState();
1100 if (myWorldViewProjState != aWVPState)
1103 myWorldViewProjState = aWVPState;
1106 myLocalOrigin.SetCoord (0.0, 0.0, 0.0);
1107 aContext->SetCamera (myCamera);
1108 if (aManager->ModelWorldState().Index() == 0)
1110 aContext->ShaderManager()->UpdateModelWorldStateTo (OpenGl_Mat4());
1113 // ====================================
1114 // Step 2: Redraw background
1115 // ====================================
1117 // Render background
1118 if (!theToDrawImmediate)
1120 drawBackground (myWorkspace, theProjection);
1123 #if !defined(GL_ES_VERSION_2_0)
1124 // Switch off lighting by default
1125 if (aContext->core11 != NULL
1126 && aContext->caps->ffpEnable)
1128 glDisable(GL_LIGHTING);
1132 // =================================
1133 // Step 3: Redraw main plane
1134 // =================================
1136 // Setup face culling
1137 GLboolean isCullFace = GL_FALSE;
1138 if (myBackfacing != Graphic3d_TOBM_AUTOMATIC)
1140 isCullFace = glIsEnabled (GL_CULL_FACE);
1141 if (myBackfacing == Graphic3d_TOBM_DISABLE)
1143 glEnable (GL_CULL_FACE);
1144 glCullFace (GL_BACK);
1147 glDisable (GL_CULL_FACE);
1150 #if !defined(GL_ES_VERSION_2_0)
1151 // if the view is scaled normal vectors are scaled to unit
1152 // length for correct displaying of shaded objects
1153 const gp_Pnt anAxialScale = aContext->Camera()->AxialScale();
1154 if (anAxialScale.X() != 1.F ||
1155 anAxialScale.Y() != 1.F ||
1156 anAxialScale.Z() != 1.F)
1158 aContext->SetGlNormalizeEnabled (Standard_True);
1162 aContext->SetGlNormalizeEnabled (Standard_False);
1166 aManager->SetShadingModel (OpenGl_ShaderManager::PBRShadingModelFallback (myShadingModel, checkPBRAvailability()));
1169 if (theProjection == Graphic3d_Camera::Projection_MonoLeftEye)
1171 aContext->ProjectionState.SetCurrent (aContext->Camera()->ProjectionStereoLeftF());
1172 aContext->ApplyProjectionMatrix();
1174 else if (theProjection == Graphic3d_Camera::Projection_MonoRightEye)
1176 aContext->ProjectionState.SetCurrent (aContext->Camera()->ProjectionStereoRightF());
1177 aContext->ApplyProjectionMatrix();
1180 myWorkspace->SetEnvironmentTexture (myTextureEnv);
1182 renderScene (theProjection, theOutputFBO, theOitAccumFbo, theToDrawImmediate);
1184 myWorkspace->SetEnvironmentTexture (Handle(OpenGl_TextureSet)());
1186 // ===============================
1187 // Step 4: Trihedron
1188 // ===============================
1190 // Resetting GL parameters according to the default aspects
1191 // in order to synchronize GL state with the graphic driver state
1192 // before drawing auxiliary stuff (trihedrons, overlayer)
1193 myWorkspace->ResetAppliedAspect();
1196 if (!theToDrawImmediate)
1198 renderTrihedron (myWorkspace);
1200 // Restore face culling
1201 if (myBackfacing != Graphic3d_TOBM_AUTOMATIC)
1205 glEnable (GL_CULL_FACE);
1206 glCullFace (GL_BACK);
1209 glDisable (GL_CULL_FACE);
1217 myWorkspace->ResetAppliedAspect();
1218 aContext->SetAllowSampleAlphaToCoverage (false);
1219 aContext->SetSampleAlphaToCoverage (false);
1221 // reset FFP state for safety
1222 aContext->BindProgram (Handle(OpenGl_ShaderProgram)());
1223 if (aContext->caps->ffpEnable)
1225 aContext->ShaderManager()->PushState (Handle(OpenGl_ShaderProgram)());
1228 // ==============================================================
1229 // Step 6: Keep shader manager informed about last View
1230 // ==============================================================
1232 if (!aManager.IsNull())
1234 aManager->SetLastView (this);
1238 // =======================================================================
1239 // function : InvalidateBVHData
1241 // =======================================================================
1242 void OpenGl_View::InvalidateBVHData (const Graphic3d_ZLayerId theLayerId)
1244 myZLayers.InvalidateBVHData (theLayerId);
1247 //=======================================================================
1248 //function : renderStructs
1250 //=======================================================================
1251 void OpenGl_View::renderStructs (Graphic3d_Camera::Projection theProjection,
1252 OpenGl_FrameBuffer* theReadDrawFbo,
1253 OpenGl_FrameBuffer* theOitAccumFbo,
1254 const Standard_Boolean theToDrawImmediate)
1256 myZLayers.UpdateCulling (myWorkspace, theToDrawImmediate);
1257 if ( myZLayers.NbStructures() <= 0 )
1260 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
1261 Standard_Boolean toRenderGL = theToDrawImmediate ||
1262 myRenderParams.Method != Graphic3d_RM_RAYTRACING ||
1263 myRaytraceInitStatus == OpenGl_RT_FAIL ||
1268 const Standard_Integer aSizeX = theReadDrawFbo != NULL ? theReadDrawFbo->GetVPSizeX() : myWindow->Width();
1269 const Standard_Integer aSizeY = theReadDrawFbo != NULL ? theReadDrawFbo->GetVPSizeY() : myWindow->Height();
1271 toRenderGL = !initRaytraceResources (aSizeX, aSizeY, aCtx)
1272 || !updateRaytraceGeometry (OpenGl_GUM_CHECK, myId, aCtx);
1274 toRenderGL |= !myIsRaytraceDataValid; // if no ray-trace data use OpenGL
1278 myOpenGlFBO ->InitLazy (aCtx, aSizeX, aSizeY, myFboColorFormat, myFboDepthFormat, 0);
1280 if (theReadDrawFbo != NULL)
1281 theReadDrawFbo->UnbindBuffer (aCtx);
1283 // Prepare preliminary OpenGL output
1284 if (aCtx->arbFBOBlit != NULL)
1286 // Render bottom OSD layer
1287 myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_Bottom, theReadDrawFbo, theOitAccumFbo);
1289 const Standard_Integer aPrevFilter = myWorkspace->RenderFilter() & ~(Standard_Integer )(OpenGl_RenderFilter_NonRaytraceableOnly);
1290 myWorkspace->SetRenderFilter (aPrevFilter | OpenGl_RenderFilter_NonRaytraceableOnly);
1292 if (theReadDrawFbo != NULL)
1294 theReadDrawFbo->BindDrawBuffer (aCtx);
1298 aCtx->arbFBO->glBindFramebuffer (GL_DRAW_FRAMEBUFFER, 0);
1299 aCtx->SetFrameBufferSRGB (false);
1302 // Render non-polygonal elements in default layer
1303 myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_RayTracable, theReadDrawFbo, theOitAccumFbo);
1305 myWorkspace->SetRenderFilter (aPrevFilter);
1308 if (theReadDrawFbo != NULL)
1310 theReadDrawFbo->BindBuffer (aCtx);
1314 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, 0);
1315 aCtx->SetFrameBufferSRGB (false);
1318 // Reset OpenGl aspects state to default to avoid enabling of
1319 // backface culling which is not supported in ray-tracing.
1320 myWorkspace->ResetAppliedAspect();
1322 // Ray-tracing polygonal primitive arrays
1323 raytrace (aSizeX, aSizeY, theProjection, theReadDrawFbo, aCtx);
1325 // Render upper (top and topmost) OpenGL layers
1326 myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_Upper, theReadDrawFbo, theOitAccumFbo);
1330 // Redraw 3D scene using OpenGL in standard
1331 // mode or in case of ray-tracing failure
1334 myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_All, theReadDrawFbo, theOitAccumFbo);
1336 // Set flag that scene was redrawn by standard pipeline
1337 myWasRedrawnGL = Standard_True;
1341 //=======================================================================
1342 //function : renderTrihedron
1344 //=======================================================================
1345 void OpenGl_View::renderTrihedron (const Handle(OpenGl_Workspace) &theWorkspace)
1347 if (myToShowGradTrihedron)
1349 myGraduatedTrihedron.Render (theWorkspace);
1353 //=======================================================================
1354 //function : renderFrameStats
1356 //=======================================================================
1357 void OpenGl_View::renderFrameStats()
1359 if (myRenderParams.ToShowStats
1360 && myRenderParams.CollectedStats != Graphic3d_RenderingParams::PerfCounters_NONE)
1362 myFrameStatsPrs.Update (myWorkspace);
1363 myFrameStatsPrs.Render (myWorkspace);
1367 // =======================================================================
1368 // function : Invalidate
1370 // =======================================================================
1371 void OpenGl_View::Invalidate()
1373 myBackBufferRestored = Standard_False;
1376 //=======================================================================
1377 //function : renderScene
1379 //=======================================================================
1380 void OpenGl_View::renderScene (Graphic3d_Camera::Projection theProjection,
1381 OpenGl_FrameBuffer* theReadDrawFbo,
1382 OpenGl_FrameBuffer* theOitAccumFbo,
1383 const Standard_Boolean theToDrawImmediate)
1385 const Handle(OpenGl_Context)& aContext = myWorkspace->GetGlContext();
1387 // Specify clipping planes in view transformation space
1388 aContext->ChangeClipping().Reset (myClipPlanes);
1389 if (!myClipPlanes.IsNull()
1390 && !myClipPlanes->IsEmpty())
1392 aContext->ShaderManager()->UpdateClippingState();
1395 renderStructs (theProjection, theReadDrawFbo, theOitAccumFbo, theToDrawImmediate);
1396 aContext->BindTextures (Handle(OpenGl_TextureSet)(), Handle(OpenGl_ShaderProgram)());
1398 // Apply restored view matrix.
1399 aContext->ApplyWorldViewMatrix();
1401 aContext->ChangeClipping().Reset (Handle(Graphic3d_SequenceOfHClipPlane)());
1402 if (!myClipPlanes.IsNull()
1403 && !myClipPlanes->IsEmpty())
1405 aContext->ShaderManager()->RevertClippingState();
1409 // =======================================================================
1410 // function : bindDefaultFbo
1412 // =======================================================================
1413 void OpenGl_View::bindDefaultFbo (OpenGl_FrameBuffer* theCustomFbo)
1415 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
1416 OpenGl_FrameBuffer* anFbo = (theCustomFbo != NULL && theCustomFbo->IsValid())
1418 : (!aCtx->DefaultFrameBuffer().IsNull()
1419 && aCtx->DefaultFrameBuffer()->IsValid()
1420 ? aCtx->DefaultFrameBuffer().operator->()
1424 anFbo->BindBuffer (aCtx);
1425 anFbo->SetupViewport (aCtx);
1429 #if !defined(GL_ES_VERSION_2_0)
1430 aCtx->SetReadDrawBuffer (GL_BACK);
1432 if (aCtx->arbFBO != NULL)
1434 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
1437 const Standard_Integer aViewport[4] = { 0, 0, myWindow->Width(), myWindow->Height() };
1438 aCtx->ResizeViewport (aViewport);
1442 // =======================================================================
1443 // function : initBlitQuad
1445 // =======================================================================
1446 OpenGl_VertexBuffer* OpenGl_View::initBlitQuad (const Standard_Boolean theToFlip)
1448 OpenGl_VertexBuffer* aVerts = NULL;
1451 aVerts = &myFullScreenQuad;
1452 if (!aVerts->IsValid())
1454 OpenGl_Vec4 aQuad[4] =
1456 OpenGl_Vec4( 1.0f, -1.0f, 1.0f, 0.0f),
1457 OpenGl_Vec4( 1.0f, 1.0f, 1.0f, 1.0f),
1458 OpenGl_Vec4(-1.0f, -1.0f, 0.0f, 0.0f),
1459 OpenGl_Vec4(-1.0f, 1.0f, 0.0f, 1.0f)
1461 aVerts->Init (myWorkspace->GetGlContext(), 4, 4, aQuad[0].GetData());
1466 aVerts = &myFullScreenQuadFlip;
1467 if (!aVerts->IsValid())
1469 OpenGl_Vec4 aQuad[4] =
1471 OpenGl_Vec4( 1.0f, -1.0f, 1.0f, 1.0f),
1472 OpenGl_Vec4( 1.0f, 1.0f, 1.0f, 0.0f),
1473 OpenGl_Vec4(-1.0f, -1.0f, 0.0f, 1.0f),
1474 OpenGl_Vec4(-1.0f, 1.0f, 0.0f, 0.0f)
1476 aVerts->Init (myWorkspace->GetGlContext(), 4, 4, aQuad[0].GetData());
1482 // =======================================================================
1483 // function : blitBuffers
1485 // =======================================================================
1486 bool OpenGl_View::blitBuffers (OpenGl_FrameBuffer* theReadFbo,
1487 OpenGl_FrameBuffer* theDrawFbo,
1488 const Standard_Boolean theToFlip)
1490 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
1491 const Standard_Integer aReadSizeX = theReadFbo != NULL ? theReadFbo->GetVPSizeX() : myWindow->Width();
1492 const Standard_Integer aReadSizeY = theReadFbo != NULL ? theReadFbo->GetVPSizeY() : myWindow->Height();
1493 const Standard_Integer aDrawSizeX = theDrawFbo != NULL ? theDrawFbo->GetVPSizeX() : myWindow->Width();
1494 const Standard_Integer aDrawSizeY = theDrawFbo != NULL ? theDrawFbo->GetVPSizeY() : myWindow->Height();
1495 if (theReadFbo == NULL || aCtx->IsFeedback())
1499 else if (theReadFbo == theDrawFbo)
1504 // clear destination before blitting
1505 if (theDrawFbo != NULL
1506 && theDrawFbo->IsValid())
1508 theDrawFbo->BindBuffer (aCtx);
1512 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
1513 aCtx->SetFrameBufferSRGB (false);
1515 const Standard_Integer aViewport[4] = { 0, 0, aDrawSizeX, aDrawSizeY };
1516 aCtx->ResizeViewport (aViewport);
1518 aCtx->SetColorMaskRGBA (NCollection_Vec4<bool> (true)); // force writes into all components, including alpha
1519 aCtx->core20fwd->glClearDepth (1.0);
1520 aCtx->core20fwd->glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
1521 aCtx->SetColorMask (true); // restore default alpha component write state
1523 const bool toApplyGamma = aCtx->ToRenderSRGB() != aCtx->IsFrameBufferSRGB();
1524 if (aCtx->arbFBOBlit != NULL
1526 && theReadFbo->NbSamples() != 0)
1528 GLbitfield aCopyMask = 0;
1529 theReadFbo->BindReadBuffer (aCtx);
1530 if (theDrawFbo != NULL
1531 && theDrawFbo->IsValid())
1533 theDrawFbo->BindDrawBuffer (aCtx);
1534 if (theDrawFbo->HasColor()
1535 && theReadFbo->HasColor())
1537 aCopyMask |= GL_COLOR_BUFFER_BIT;
1539 if (theDrawFbo->HasDepth()
1540 && theReadFbo->HasDepth())
1542 aCopyMask |= GL_DEPTH_BUFFER_BIT;
1547 if (theReadFbo->HasColor())
1549 aCopyMask |= GL_COLOR_BUFFER_BIT;
1551 if (theReadFbo->HasDepth())
1553 aCopyMask |= GL_DEPTH_BUFFER_BIT;
1555 aCtx->arbFBO->glBindFramebuffer (GL_DRAW_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
1556 aCtx->SetFrameBufferSRGB (false);
1559 // we don't copy stencil buffer here... does it matter for performance?
1560 aCtx->arbFBOBlit->glBlitFramebuffer (0, 0, aReadSizeX, aReadSizeY,
1561 0, 0, aDrawSizeX, aDrawSizeY,
1562 aCopyMask, GL_NEAREST);
1563 const int anErr = ::glGetError();
1564 if (anErr != GL_NO_ERROR)
1566 // glBlitFramebuffer() might fail in several cases:
1567 // - Both FBOs have MSAA and they are samples number does not match.
1568 // OCCT checks that this does not happen,
1569 // however some graphics drivers provide an option for overriding MSAA.
1570 // In this case window MSAA might be non-zero (and application can not check it)
1571 // and might not match MSAA of our offscreen FBOs.
1572 // - Pixel formats of FBOs do not match.
1573 // This also might happen with window has pixel format,
1574 // e.g. Mesa fails blitting RGBA8 -> RGB8 while other drivers support this conversion.
1575 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "FBO blitting has failed [Error #" + anErr + "]\n"
1576 + " Please check your graphics driver settings or try updating driver.";
1577 if (theReadFbo->NbSamples() != 0)
1579 myToDisableMSAA = true;
1580 aMsg += "\n MSAA settings should not be overridden by driver!";
1582 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1583 GL_DEBUG_TYPE_ERROR,
1585 GL_DEBUG_SEVERITY_HIGH,
1589 if (theDrawFbo != NULL
1590 && theDrawFbo->IsValid())
1592 theDrawFbo->BindBuffer (aCtx);
1596 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
1597 aCtx->SetFrameBufferSRGB (false);
1602 aCtx->core20fwd->glDepthFunc (GL_ALWAYS);
1603 aCtx->core20fwd->glDepthMask (GL_TRUE);
1604 aCtx->core20fwd->glEnable (GL_DEPTH_TEST);
1605 #if defined(GL_ES_VERSION_2_0)
1606 if (!aCtx->IsGlGreaterEqual (3, 0)
1607 && !aCtx->extFragDepth)
1609 aCtx->core20fwd->glDisable (GL_DEPTH_TEST);
1613 aCtx->BindTextures (Handle(OpenGl_TextureSet)(), Handle(OpenGl_ShaderProgram)());
1615 const Graphic3d_TypeOfTextureFilter aFilter = (aDrawSizeX == aReadSizeX && aDrawSizeY == aReadSizeY) ? Graphic3d_TOTF_NEAREST : Graphic3d_TOTF_BILINEAR;
1616 const GLint aFilterGl = aFilter == Graphic3d_TOTF_NEAREST ? GL_NEAREST : GL_LINEAR;
1618 OpenGl_VertexBuffer* aVerts = initBlitQuad (theToFlip);
1619 const Handle(OpenGl_ShaderManager)& aManager = aCtx->ShaderManager();
1620 if (aVerts->IsValid()
1621 && aManager->BindFboBlitProgram (theReadFbo != NULL ? theReadFbo->NbSamples() : 0, toApplyGamma))
1623 aCtx->SetSampleAlphaToCoverage (false);
1624 theReadFbo->ColorTexture()->Bind (aCtx, Graphic3d_TextureUnit_0);
1625 if (theReadFbo->ColorTexture()->Sampler()->Parameters()->Filter() != aFilter)
1627 theReadFbo->ColorTexture()->Sampler()->Parameters()->SetFilter (aFilter);
1628 aCtx->core20fwd->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, aFilterGl);
1629 aCtx->core20fwd->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, aFilterGl);
1632 theReadFbo->DepthStencilTexture()->Bind (aCtx, Graphic3d_TextureUnit_1);
1633 if (theReadFbo->DepthStencilTexture()->Sampler()->Parameters()->Filter() != aFilter)
1635 theReadFbo->DepthStencilTexture()->Sampler()->Parameters()->SetFilter (aFilter);
1636 aCtx->core20fwd->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, aFilterGl);
1637 aCtx->core20fwd->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, aFilterGl);
1640 aVerts->BindVertexAttrib (aCtx, Graphic3d_TOA_POS);
1642 aCtx->core20fwd->glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
1644 aVerts->UnbindVertexAttrib (aCtx, Graphic3d_TOA_POS);
1645 theReadFbo->DepthStencilTexture()->Unbind (aCtx, Graphic3d_TextureUnit_1);
1646 theReadFbo->ColorTexture() ->Unbind (aCtx, Graphic3d_TextureUnit_0);
1647 aCtx->BindProgram (NULL);
1651 TCollection_ExtendedString aMsg = TCollection_ExtendedString()
1652 + "Error! FBO blitting has failed";
1653 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1654 GL_DEBUG_TYPE_ERROR,
1656 GL_DEBUG_SEVERITY_HIGH,
1658 myHasFboBlit = Standard_False;
1659 theReadFbo->Release (aCtx.operator->());
1666 // =======================================================================
1667 // function : drawStereoPair
1669 // =======================================================================
1670 void OpenGl_View::drawStereoPair (OpenGl_FrameBuffer* theDrawFbo)
1672 const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext();
1673 bindDefaultFbo (theDrawFbo);
1674 OpenGl_FrameBuffer* aPair[2] =
1676 myImmediateSceneFbos[0]->IsValid() ? myImmediateSceneFbos[0].operator->() : NULL,
1677 myImmediateSceneFbos[1]->IsValid() ? myImmediateSceneFbos[1].operator->() : NULL
1679 if (aPair[0] == NULL
1681 || !myTransientDrawToFront)
1683 aPair[0] = myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL;
1684 aPair[1] = myMainSceneFbos[1]->IsValid() ? myMainSceneFbos[1].operator->() : NULL;
1687 if (aPair[0] == NULL
1688 || aPair[1] == NULL)
1693 if (aPair[0]->NbSamples() != 0)
1695 // resolve MSAA buffers before drawing
1696 if (!myOpenGlFBO ->InitLazy (aCtx, aPair[0]->GetVPSizeX(), aPair[0]->GetVPSizeY(), myFboColorFormat, myFboDepthFormat, 0)
1697 || !myOpenGlFBO2->InitLazy (aCtx, aPair[0]->GetVPSizeX(), aPair[0]->GetVPSizeY(), myFboColorFormat, 0, 0))
1699 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1700 GL_DEBUG_TYPE_ERROR,
1702 GL_DEBUG_SEVERITY_HIGH,
1703 "Error! Unable to allocate FBO for blitting stereo pair");
1704 bindDefaultFbo (theDrawFbo);
1708 if (!blitBuffers (aPair[0], myOpenGlFBO .operator->(), Standard_False)
1709 || !blitBuffers (aPair[1], myOpenGlFBO2.operator->(), Standard_False))
1711 bindDefaultFbo (theDrawFbo);
1715 aPair[0] = myOpenGlFBO .operator->();
1716 aPair[1] = myOpenGlFBO2.operator->();
1717 bindDefaultFbo (theDrawFbo);
1722 Standard_Integer left;
1723 Standard_Integer top;
1724 Standard_Integer right;
1725 Standard_Integer bottom;
1726 Standard_Integer dx() { return right - left; }
1727 Standard_Integer dy() { return bottom - top; }
1730 myWindow->PlatformWindow()->Position (aGeom.left, aGeom.top, aGeom.right, aGeom.bottom);
1732 Standard_Boolean toReverse = myRenderParams.ToReverseStereo;
1733 const Standard_Boolean isOddY = (aGeom.top + aGeom.dy()) % 2 == 1;
1734 const Standard_Boolean isOddX = aGeom.left % 2 == 1;
1736 && (myRenderParams.StereoMode == Graphic3d_StereoMode_RowInterlaced
1737 || myRenderParams.StereoMode == Graphic3d_StereoMode_ChessBoard))
1739 toReverse = !toReverse;
1742 && (myRenderParams.StereoMode == Graphic3d_StereoMode_ColumnInterlaced
1743 || myRenderParams.StereoMode == Graphic3d_StereoMode_ChessBoard))
1745 toReverse = !toReverse;
1750 std::swap (aPair[0], aPair[1]);
1753 aCtx->core20fwd->glDepthFunc (GL_ALWAYS);
1754 aCtx->core20fwd->glDepthMask (GL_TRUE);
1755 aCtx->core20fwd->glEnable (GL_DEPTH_TEST);
1757 aCtx->BindTextures (Handle(OpenGl_TextureSet)(), Handle(OpenGl_ShaderProgram)());
1758 OpenGl_VertexBuffer* aVerts = initBlitQuad (myToFlipOutput);
1760 const Handle(OpenGl_ShaderManager)& aManager = aCtx->ShaderManager();
1761 if (aVerts->IsValid()
1762 && aManager->BindStereoProgram (myRenderParams.StereoMode))
1764 if (myRenderParams.StereoMode == Graphic3d_StereoMode_Anaglyph)
1766 OpenGl_Mat4 aFilterL, aFilterR;
1767 aFilterL.SetDiagonal (Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f));
1768 aFilterR.SetDiagonal (Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f));
1769 switch (myRenderParams.AnaglyphFilter)
1771 case Graphic3d_RenderingParams::Anaglyph_RedCyan_Simple:
1773 aFilterL.SetRow (0, Graphic3d_Vec4 (1.0f, 0.0f, 0.0f, 0.0f));
1774 aFilterR.SetRow (1, Graphic3d_Vec4 (0.0f, 1.0f, 0.0f, 0.0f));
1775 aFilterR.SetRow (2, Graphic3d_Vec4 (0.0f, 0.0f, 1.0f, 0.0f));
1778 case Graphic3d_RenderingParams::Anaglyph_RedCyan_Optimized:
1780 aFilterL.SetRow (0, Graphic3d_Vec4 ( 0.4154f, 0.4710f, 0.16666667f, 0.0f));
1781 aFilterL.SetRow (1, Graphic3d_Vec4 (-0.0458f, -0.0484f, -0.0257f, 0.0f));
1782 aFilterL.SetRow (2, Graphic3d_Vec4 (-0.0547f, -0.0615f, 0.0128f, 0.0f));
1783 aFilterL.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f));
1784 aFilterR.SetRow (0, Graphic3d_Vec4 (-0.01090909f, -0.03636364f, -0.00606061f, 0.0f));
1785 aFilterR.SetRow (1, Graphic3d_Vec4 ( 0.37560000f, 0.73333333f, 0.01111111f, 0.0f));
1786 aFilterR.SetRow (2, Graphic3d_Vec4 (-0.06510000f, -0.12870000f, 1.29710000f, 0.0f));
1787 aFilterR.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f));
1790 case Graphic3d_RenderingParams::Anaglyph_YellowBlue_Simple:
1792 aFilterL.SetRow (0, Graphic3d_Vec4 (1.0f, 0.0f, 0.0f, 0.0f));
1793 aFilterL.SetRow (1, Graphic3d_Vec4 (0.0f, 1.0f, 0.0f, 0.0f));
1794 aFilterR.SetRow (2, Graphic3d_Vec4 (0.0f, 0.0f, 1.0f, 0.0f));
1797 case Graphic3d_RenderingParams::Anaglyph_YellowBlue_Optimized:
1799 aFilterL.SetRow (0, Graphic3d_Vec4 ( 1.062f, -0.205f, 0.299f, 0.0f));
1800 aFilterL.SetRow (1, Graphic3d_Vec4 (-0.026f, 0.908f, 0.068f, 0.0f));
1801 aFilterL.SetRow (2, Graphic3d_Vec4 (-0.038f, -0.173f, 0.022f, 0.0f));
1802 aFilterL.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f));
1803 aFilterR.SetRow (0, Graphic3d_Vec4 (-0.016f, -0.123f, -0.017f, 0.0f));
1804 aFilterR.SetRow (1, Graphic3d_Vec4 ( 0.006f, 0.062f, -0.017f, 0.0f));
1805 aFilterR.SetRow (2, Graphic3d_Vec4 ( 0.094f, 0.185f, 0.911f, 0.0f));
1806 aFilterR.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f));
1809 case Graphic3d_RenderingParams::Anaglyph_GreenMagenta_Simple:
1811 aFilterR.SetRow (0, Graphic3d_Vec4 (1.0f, 0.0f, 0.0f, 0.0f));
1812 aFilterL.SetRow (1, Graphic3d_Vec4 (0.0f, 1.0f, 0.0f, 0.0f));
1813 aFilterR.SetRow (2, Graphic3d_Vec4 (0.0f, 0.0f, 1.0f, 0.0f));
1816 case Graphic3d_RenderingParams::Anaglyph_UserDefined:
1818 aFilterL = myRenderParams.AnaglyphLeft;
1819 aFilterR = myRenderParams.AnaglyphRight;
1823 aCtx->ActiveProgram()->SetUniform (aCtx, "uMultL", aFilterL);
1824 aCtx->ActiveProgram()->SetUniform (aCtx, "uMultR", aFilterR);
1827 aPair[0]->ColorTexture()->Bind (aCtx, Graphic3d_TextureUnit_0);
1828 aPair[1]->ColorTexture()->Bind (aCtx, Graphic3d_TextureUnit_1);
1829 aVerts->BindVertexAttrib (aCtx, 0);
1831 aCtx->core20fwd->glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
1833 aVerts->UnbindVertexAttrib (aCtx, 0);
1834 aPair[1]->ColorTexture()->Unbind (aCtx, Graphic3d_TextureUnit_1);
1835 aPair[0]->ColorTexture()->Unbind (aCtx, Graphic3d_TextureUnit_0);
1839 TCollection_ExtendedString aMsg = TCollection_ExtendedString()
1840 + "Error! Anaglyph has failed";
1841 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1842 GL_DEBUG_TYPE_ERROR,
1844 GL_DEBUG_SEVERITY_HIGH,
1849 // =======================================================================
1850 // function : copyBackToFront
1852 // =======================================================================
1853 bool OpenGl_View::copyBackToFront()
1855 myIsImmediateDrawn = Standard_False;
1856 #if !defined(GL_ES_VERSION_2_0)
1857 const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext();
1858 if (aCtx->core11 == NULL)
1863 OpenGl_Mat4 aProjectMat;
1864 Graphic3d_TransformUtils::Ortho2D (aProjectMat,
1865 0.0f, static_cast<GLfloat> (myWindow->Width()),
1866 0.0f, static_cast<GLfloat> (myWindow->Height()));
1868 aCtx->WorldViewState.Push();
1869 aCtx->ProjectionState.Push();
1871 aCtx->WorldViewState.SetIdentity();
1872 aCtx->ProjectionState.SetCurrent (aProjectMat);
1874 aCtx->ApplyProjectionMatrix();
1875 aCtx->ApplyWorldViewMatrix();
1877 // synchronize FFP state before copying pixels
1878 aCtx->BindProgram (Handle(OpenGl_ShaderProgram)());
1879 aCtx->ShaderManager()->PushState (Handle(OpenGl_ShaderProgram)());
1880 aCtx->DisableFeatures();
1882 switch (aCtx->DrawBuffer())
1886 aCtx->SetReadBuffer (GL_BACK_LEFT);
1887 aCtx->SetDrawBuffer (GL_FRONT_LEFT);
1892 aCtx->SetReadBuffer (GL_BACK_RIGHT);
1893 aCtx->SetDrawBuffer (GL_FRONT_RIGHT);
1898 aCtx->SetReadBuffer (GL_BACK);
1899 aCtx->SetDrawBuffer (GL_FRONT);
1904 aCtx->core11->glRasterPos2i (0, 0);
1905 aCtx->core11->glCopyPixels (0, 0, myWindow->Width() + 1, myWindow->Height() + 1, GL_COLOR);
1906 //aCtx->core11->glCopyPixels (0, 0, myWidth + 1, myHeight + 1, GL_DEPTH);
1908 aCtx->EnableFeatures();
1910 aCtx->WorldViewState.Pop();
1911 aCtx->ProjectionState.Pop();
1912 aCtx->ApplyProjectionMatrix();
1914 // read/write from front buffer now
1915 aCtx->SetReadBuffer (aCtx->DrawBuffer());
1922 // =======================================================================
1923 // function : checkOitCompatibility
1925 // =======================================================================
1926 Standard_Boolean OpenGl_View::checkOitCompatibility (const Handle(OpenGl_Context)& theGlContext,
1927 const Standard_Boolean theMSAA)
1929 // determine if OIT is supported by current OpenGl context
1930 Standard_Boolean& aToDisableOIT = theMSAA ? myToDisableMSAA : myToDisableOIT;
1933 return Standard_False;
1936 TCollection_ExtendedString aCompatibilityMsg;
1937 if (theGlContext->hasFloatBuffer == OpenGl_FeatureNotAvailable
1938 && theGlContext->hasHalfFloatBuffer == OpenGl_FeatureNotAvailable)
1940 aCompatibilityMsg += "OpenGL context does not support floating-point RGBA color buffer format.\n";
1942 if (theMSAA && theGlContext->hasSampleVariables == OpenGl_FeatureNotAvailable)
1944 aCompatibilityMsg += "Current version of GLSL does not support built-in sample variables.\n";
1946 if (theGlContext->hasDrawBuffers == OpenGl_FeatureNotAvailable)
1948 aCompatibilityMsg += "OpenGL context does not support multiple draw buffers.\n";
1950 if (aCompatibilityMsg.IsEmpty())
1952 return Standard_True;
1955 aCompatibilityMsg += " Blended order-independent transparency will not be available.\n";
1956 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1957 GL_DEBUG_TYPE_ERROR,
1959 GL_DEBUG_SEVERITY_HIGH,
1962 aToDisableOIT = Standard_True;
1963 return Standard_False;
1966 // =======================================================================
1967 // function : chooseOitColorConfiguration
1969 // =======================================================================
1970 bool OpenGl_View::chooseOitColorConfiguration (const Handle(OpenGl_Context)& theGlContext,
1971 const Standard_Integer theConfigIndex,
1972 OpenGl_ColorFormats& theFormats)
1975 switch (theConfigIndex)
1977 case 0: // choose best applicable color format combination
1979 theFormats.Append (theGlContext->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable ? GL_RGBA16F : GL_RGBA32F);
1980 theFormats.Append (theGlContext->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable ? GL_R16F : GL_R32F);
1983 case 1: // choose non-optimal applicable color format combination
1985 theFormats.Append (theGlContext->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable ? GL_RGBA16F : GL_RGBA32F);
1986 theFormats.Append (theGlContext->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable ? GL_RGBA16F : GL_RGBA32F);
1990 return false; // color combination does not exist
1993 // =======================================================================
1994 // function : checkPBRAvailability
1996 // =======================================================================
1997 Standard_Boolean OpenGl_View::checkPBRAvailability() const
1999 return myWorkspace->GetGlContext()->HasPBR()
2000 && !myPBREnvironment.IsNull();
2003 // =======================================================================
2004 // function : bakePBREnvironment
2006 // =======================================================================
2007 void OpenGl_View::bakePBREnvironment (const Handle(OpenGl_Context)& theCtx)
2009 const Handle(OpenGl_TextureSet)& aTextureSet = myCubeMapParams->TextureSet (theCtx);
2010 if (!aTextureSet.IsNull()
2011 && !aTextureSet->IsEmpty())
2013 myPBREnvironment->Bake (theCtx,
2014 aTextureSet->First(),
2015 myBackgroundCubeMap->ZIsInverted(),
2016 myBackgroundCubeMap->IsTopDown(),
2017 myRenderParams.PbrEnvBakingDiffNbSamples,
2018 myRenderParams.PbrEnvBakingSpecNbSamples,
2019 myRenderParams.PbrEnvBakingProbability);
2023 myPBREnvironment->Clear (theCtx);
2027 // =======================================================================
2028 // function : clearPBREnvironment
2030 // =======================================================================
2031 void OpenGl_View::clearPBREnvironment (const Handle(OpenGl_Context)& theCtx)
2033 myPBREnvironment->Clear (theCtx);
2036 // =======================================================================
2037 // function : clearPBREnvironment
2039 // =======================================================================
2040 void OpenGl_View::processPBREnvRequest (const Handle(OpenGl_Context)& theCtx)
2042 if (myPBREnvState == OpenGl_PBREnvState_CREATED)
2044 switch (myPBREnvRequest)
2046 case OpenGl_PBREnvRequest_NONE: return;
2047 case OpenGl_PBREnvRequest_BAKE: bakePBREnvironment (theCtx); break;
2048 case OpenGl_PBREnvRequest_CLEAR: clearPBREnvironment (theCtx); break;
2051 myPBREnvRequest = OpenGl_PBREnvRequest_NONE;