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 //=======================================================================
134 //=======================================================================
135 void OpenGl_View::Redraw()
137 const Standard_Boolean wasDisabledMSAA = myToDisableMSAA;
138 const Standard_Boolean hadFboBlit = myHasFboBlit;
139 if (myRenderParams.Method == Graphic3d_RM_RAYTRACING
140 && !myCaps->vboDisable
141 && !myCaps->keepArrayData)
143 // caps are shared across all views, thus we need to invalidate all of them
144 // if (myWasRedrawnGL) { myStructureManager->SetDeviceLost(); }
145 myDriver->setDeviceLost();
146 myCaps->keepArrayData = Standard_True;
149 if (!myWorkspace->Activate())
154 // implicitly disable VSync when using HMD composer (can be mirrored in window for debugging)
155 myWindow->SetSwapInterval (IsActiveXR());
158 const Graphic3d_StereoMode aStereoMode = myRenderParams.StereoMode;
159 Graphic3d_Camera::Projection aProjectType = myCamera->ProjectionType();
160 const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext();
161 aCtx->FrameStats()->FrameStart (myWorkspace->View(), false);
162 aCtx->SetLineFeather (myRenderParams.LineFeather);
164 const Standard_Integer anSRgbState = aCtx->ToRenderSRGB() ? 1 : 0;
165 if (mySRgbState != -1
166 && mySRgbState != anSRgbState)
168 releaseSrgbResources (aCtx);
169 initTextureEnv (aCtx);
171 mySRgbState = anSRgbState;
172 aCtx->ShaderManager()->UpdateSRgbState();
174 // release pending GL resources
175 aCtx->ReleaseDelayed();
177 // fetch OpenGl context state
180 OpenGl_FrameBuffer* aFrameBuffer = myFBO.get();
181 bool toSwap = aCtx->IsRender()
182 && !aCtx->caps->buffersNoSwap
183 && aFrameBuffer == NULL
184 && (!IsActiveXR() || myRenderParams.ToMirrorComposer);
186 Standard_Integer aSizeX = myWindow->Width();
187 Standard_Integer aSizeY = myWindow->Height();
188 if (aFrameBuffer != NULL)
190 aSizeX = aFrameBuffer->GetVPSizeX();
191 aSizeY = aFrameBuffer->GetVPSizeY();
193 else if (IsActiveXR())
195 aSizeX = myXRSession->RecommendedViewport().x();
196 aSizeY = myXRSession->RecommendedViewport().y();
199 const Standard_Integer aRendSizeX = Standard_Integer(myRenderParams.RenderResolutionScale * aSizeX + 0.5f);
200 const Standard_Integer aRendSizeY = Standard_Integer(myRenderParams.RenderResolutionScale * aSizeY + 0.5f);
206 myBackBufferRestored = Standard_False;
207 myIsImmediateDrawn = Standard_False;
211 // determine multisampling parameters
212 Standard_Integer aNbSamples = !myToDisableMSAA && aSizeX == aRendSizeX
213 ? Max (Min (myRenderParams.NbMsaaSamples, aCtx->MaxMsaaSamples()), 0)
217 aNbSamples = OpenGl_Context::GetPowerOfTwo (aNbSamples, aCtx->MaxMsaaSamples());
220 bool toUseOit = myRenderParams.TransparencyMethod == Graphic3d_RTM_BLEND_OIT
221 && checkOitCompatibility (aCtx, aNbSamples > 0);
223 const bool toInitImmediateFbo = myTransientDrawToFront
224 && (!aCtx->caps->useSystemBuffer || (toUseOit && HasImmediateStructures()));
226 if ( aFrameBuffer == NULL
227 && !aCtx->DefaultFrameBuffer().IsNull()
228 && aCtx->DefaultFrameBuffer()->IsValid())
230 aFrameBuffer = aCtx->DefaultFrameBuffer().operator->();
234 && (myTransientDrawToFront
235 || aProjectType == Graphic3d_Camera::Projection_Stereo
238 || aSizeX != aRendSizeX))
240 if (myMainSceneFbos[0]->GetVPSizeX() != aRendSizeX
241 || myMainSceneFbos[0]->GetVPSizeY() != aRendSizeY
242 || myMainSceneFbos[0]->NbSamples() != aNbSamples)
244 if (!myTransientDrawToFront)
246 myImmediateSceneFbos[0]->Release (aCtx.operator->());
247 myImmediateSceneFbos[1]->Release (aCtx.operator->());
248 myImmediateSceneFbos[0]->ChangeViewport (0, 0);
249 myImmediateSceneFbos[1]->ChangeViewport (0, 0);
252 // prepare FBOs containing main scene
253 // for further blitting and rendering immediate presentations on top
254 if (aCtx->core20fwd != NULL)
256 const bool wasFailedMain0 = checkWasFailedFbo (myMainSceneFbos[0], aRendSizeX, aRendSizeY, aNbSamples);
257 if (!myMainSceneFbos[0]->Init (aCtx, aRendSizeX, aRendSizeY, myFboColorFormat, myFboDepthFormat, aNbSamples)
260 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "Error! Main FBO "
261 + printFboFormat (myMainSceneFbos[0]) + " initialization has failed";
262 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
266 if (myMainSceneFbos[0]->IsValid() && (toInitImmediateFbo || myImmediateSceneFbos[0]->IsValid()))
268 const bool wasFailedImm0 = checkWasFailedFbo (myImmediateSceneFbos[0], myMainSceneFbos[0]);
269 if (!myImmediateSceneFbos[0]->InitLazy (aCtx, *myMainSceneFbos[0])
272 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "Error! Immediate FBO "
273 + printFboFormat (myImmediateSceneFbos[0]) + " initialization has failed";
274 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
280 myMainSceneFbos [0]->Release (aCtx.operator->());
281 myMainSceneFbos [1]->Release (aCtx.operator->());
282 myImmediateSceneFbos[0]->Release (aCtx.operator->());
283 myImmediateSceneFbos[1]->Release (aCtx.operator->());
284 myXrSceneFbo ->Release (aCtx.operator->());
285 myMainSceneFbos [0]->ChangeViewport (0, 0);
286 myMainSceneFbos [1]->ChangeViewport (0, 0);
287 myImmediateSceneFbos[0]->ChangeViewport (0, 0);
288 myImmediateSceneFbos[1]->ChangeViewport (0, 0);
289 myXrSceneFbo ->ChangeViewport (0, 0);
292 bool hasXRBlitFbo = false;
293 if (aProjectType == Graphic3d_Camera::Projection_Stereo
295 && myMainSceneFbos[0]->IsValid())
298 || aSizeX != aRendSizeX)
300 hasXRBlitFbo = myXrSceneFbo->InitLazy (aCtx, aSizeX, aSizeY, myFboColorFormat, myFboDepthFormat, 0);
303 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "Error! VR FBO "
304 + printFboFormat (myXrSceneFbo) + " initialization has failed";
305 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
309 else if (aProjectType == Graphic3d_Camera::Projection_Stereo
310 && myMainSceneFbos[0]->IsValid())
312 const bool wasFailedMain1 = checkWasFailedFbo (myMainSceneFbos[1], myMainSceneFbos[0]);
313 if (!myMainSceneFbos[1]->InitLazy (aCtx, *myMainSceneFbos[0])
316 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "Error! Main FBO (second) "
317 + printFboFormat (myMainSceneFbos[1]) + " initialization has failed";
318 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
320 if (!myMainSceneFbos[1]->IsValid())
323 aProjectType = Graphic3d_Camera::Projection_Perspective;
325 else if (!myTransientDrawToFront)
329 else if (!aCtx->HasStereoBuffers() || aStereoMode != Graphic3d_StereoMode_QuadBuffer)
331 const bool wasFailedImm0 = checkWasFailedFbo (myImmediateSceneFbos[0], myMainSceneFbos[0]);
332 const bool wasFailedImm1 = checkWasFailedFbo (myImmediateSceneFbos[1], myMainSceneFbos[0]);
333 if (!myImmediateSceneFbos[0]->InitLazy (aCtx, *myMainSceneFbos[0])
336 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "Error! Immediate FBO (first) "
337 + printFboFormat (myImmediateSceneFbos[0]) + " initialization has failed";
338 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
340 if (!myImmediateSceneFbos[1]->InitLazy (aCtx, *myMainSceneFbos[0])
343 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "Error! Immediate FBO (first) "
344 + printFboFormat (myImmediateSceneFbos[1]) + " initialization has failed";
345 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
347 if (!myImmediateSceneFbos[0]->IsValid()
348 || !myImmediateSceneFbos[1]->IsValid())
350 aProjectType = Graphic3d_Camera::Projection_Perspective;
356 myXrSceneFbo->Release (aCtx.get());
357 myXrSceneFbo->ChangeViewport (0, 0);
360 // process PBR environment
361 if (myShadingModel == Graphic3d_TOSM_PBR
362 || myShadingModel == Graphic3d_TOSM_PBR_FACET)
364 if (!myPBREnvironment.IsNull()
365 && myPBREnvironment->SizesAreDifferent (myRenderParams.PbrEnvPow2Size,
366 myRenderParams.PbrEnvSpecMapNbLevels))
368 myPBREnvironment->Release (aCtx.get());
369 myPBREnvironment.Nullify();
370 myPBREnvState = OpenGl_PBREnvState_NONEXISTENT;
371 myPBREnvRequest = OpenGl_PBREnvRequest_BAKE;
375 if (myPBREnvState == OpenGl_PBREnvState_NONEXISTENT
378 myPBREnvironment = OpenGl_PBREnvironment::Create (aCtx, myRenderParams.PbrEnvPow2Size, myRenderParams.PbrEnvSpecMapNbLevels);
379 myPBREnvState = myPBREnvironment.IsNull() ? OpenGl_PBREnvState_UNAVAILABLE : OpenGl_PBREnvState_CREATED;
380 if (myPBREnvState == OpenGl_PBREnvState_CREATED)
382 Handle(OpenGl_Texture) anEnvLUT;
383 static const TCollection_AsciiString THE_SHARED_ENV_LUT_KEY("EnvLUT");
384 if (!aCtx->GetResource (THE_SHARED_ENV_LUT_KEY, anEnvLUT))
386 Handle(Graphic3d_TextureParams) aParams = new Graphic3d_TextureParams();
387 aParams->SetFilter (Graphic3d_TOTF_BILINEAR);
388 aParams->SetRepeat (Standard_False);
389 aParams->SetTextureUnit (aCtx->PBREnvLUTTexUnit());
390 anEnvLUT = new OpenGl_Texture(THE_SHARED_ENV_LUT_KEY, aParams);
391 Handle(Image_PixMap) aPixMap = new Image_PixMap();
392 aPixMap->InitWrapper (Image_Format_RGF, (Standard_Byte*)Textures_EnvLUT, Textures_EnvLUTSize, Textures_EnvLUTSize);
393 OpenGl_TextureFormat aTexFormat = OpenGl_TextureFormat::FindFormat (aCtx, aPixMap->Format(), false);
394 #if defined(GL_ES_VERSION_2_0)
395 // GL_RG32F is not texture-filterable format on OpenGL ES without OES_texture_float_linear extension.
396 // GL_RG16F is texture-filterable since OpenGL ES 3.0 and can be initialized from 32-bit floats.
397 // Note that it is expected that GL_RG16F has enough precision for this table, so that it can be used also on desktop OpenGL.
398 //if (!aCtx->hasTexFloatLinear)
399 aTexFormat.SetInternalFormat (GL_RG16F);
401 if (!aTexFormat.IsValid()
402 || !anEnvLUT->Init (aCtx, aTexFormat, Graphic3d_Vec2i((Standard_Integer)Textures_EnvLUTSize), Graphic3d_TOT_2D, aPixMap.get()))
404 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, "Failed allocation of LUT for PBR");
407 aCtx->ShareResource (THE_SHARED_ENV_LUT_KEY, anEnvLUT);
409 if (!anEnvLUT.IsNull())
411 anEnvLUT->Bind (aCtx);
413 myWorkspace->ApplyAspects();
416 processPBREnvRequest (aCtx);
419 // create color and coverage accumulation buffers required for OIT algorithm
422 Standard_Integer anFboIt = 0;
423 for (; anFboIt < 2; ++anFboIt)
425 Handle(OpenGl_FrameBuffer)& aMainSceneFbo = myMainSceneFbos [anFboIt];
426 Handle(OpenGl_FrameBuffer)& aMainSceneFboOit = myMainSceneFbosOit [anFboIt];
427 Handle(OpenGl_FrameBuffer)& anImmediateSceneFbo = myImmediateSceneFbos [anFboIt];
428 Handle(OpenGl_FrameBuffer)& anImmediateSceneFboOit = myImmediateSceneFbosOit[anFboIt];
429 if (aMainSceneFbo->IsValid()
430 && (aMainSceneFboOit->GetVPSizeX() != aRendSizeX
431 || aMainSceneFboOit->GetVPSizeY() != aRendSizeY
432 || aMainSceneFboOit->NbSamples() != aNbSamples))
434 Standard_Integer aColorConfig = 0;
435 for (;;) // seemly responding to driver limitation (GL_FRAMEBUFFER_UNSUPPORTED)
437 if (myFboOitColorConfig.IsEmpty())
439 if (!chooseOitColorConfiguration (aCtx, aColorConfig++, myFboOitColorConfig))
444 if (aMainSceneFboOit->Init (aCtx, aRendSizeX, aRendSizeY, myFboOitColorConfig, aMainSceneFbo->DepthStencilTexture(), aNbSamples))
448 myFboOitColorConfig.Clear();
450 if (!aMainSceneFboOit->IsValid())
455 else if (!aMainSceneFbo->IsValid())
457 aMainSceneFboOit->Release (aCtx.operator->());
458 aMainSceneFboOit->ChangeViewport (0, 0);
461 if (anImmediateSceneFbo->IsValid()
462 && (anImmediateSceneFboOit->GetVPSizeX() != aRendSizeX
463 || anImmediateSceneFboOit->GetVPSizeY() != aRendSizeY
464 || anImmediateSceneFboOit->NbSamples() != aNbSamples))
466 if (!anImmediateSceneFboOit->Init (aCtx, aRendSizeX, aRendSizeY, myFboOitColorConfig,
467 anImmediateSceneFbo->DepthStencilTexture(), aNbSamples))
472 else if (!anImmediateSceneFbo->IsValid())
474 anImmediateSceneFboOit->Release (aCtx.operator->());
475 anImmediateSceneFboOit->ChangeViewport (0, 0);
478 if (anFboIt == 0) // only the first OIT framebuffer is mandatory
480 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH,
481 "Initialization of float texture framebuffer for use with\n"
482 " blended order-independent transparency rendering algorithm has failed.\n"
483 " Blended order-independent transparency will not be available.\n");
486 myToDisableOITMSAA = Standard_True;
490 myToDisableOIT = Standard_True;
495 if (!toUseOit && myMainSceneFbosOit[0]->IsValid())
497 myMainSceneFbosOit [0]->Release (aCtx.operator->());
498 myMainSceneFbosOit [1]->Release (aCtx.operator->());
499 myImmediateSceneFbosOit[0]->Release (aCtx.operator->());
500 myImmediateSceneFbosOit[1]->Release (aCtx.operator->());
501 myMainSceneFbosOit [0]->ChangeViewport (0, 0);
502 myMainSceneFbosOit [1]->ChangeViewport (0, 0);
503 myImmediateSceneFbosOit[0]->ChangeViewport (0, 0);
504 myImmediateSceneFbosOit[1]->ChangeViewport (0, 0);
507 if (aProjectType == Graphic3d_Camera::Projection_Stereo)
509 OpenGl_FrameBuffer* aMainFbos[2] =
511 myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL,
512 myMainSceneFbos[1]->IsValid() ? myMainSceneFbos[1].operator->() : NULL
514 OpenGl_FrameBuffer* aMainFbosOit[2] =
516 myMainSceneFbosOit[0]->IsValid() ? myMainSceneFbosOit[0].operator->() : NULL,
517 myMainSceneFbosOit[1]->IsValid() ? myMainSceneFbosOit[1].operator->() :
518 myMainSceneFbosOit[0]->IsValid() ? myMainSceneFbosOit[0].operator->() : NULL
521 OpenGl_FrameBuffer* anImmFbos[2] =
523 myImmediateSceneFbos[0]->IsValid() ? myImmediateSceneFbos[0].operator->() : NULL,
524 myImmediateSceneFbos[1]->IsValid() ? myImmediateSceneFbos[1].operator->() : NULL
526 OpenGl_FrameBuffer* anImmFbosOit[2] =
528 myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL,
529 myImmediateSceneFbosOit[1]->IsValid() ? myImmediateSceneFbosOit[1].operator->() :
530 myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL
535 // use single frame for both views - caching main scene content makes no sense
536 // when head position is expected to be updated each frame redraw with high accuracy
537 aMainFbos[1] = aMainFbos[0];
538 aMainFbosOit[1] = aMainFbosOit[0];
539 anImmFbos[0] = aMainFbos[0];
540 anImmFbos[1] = aMainFbos[1];
541 anImmFbosOit[0] = aMainFbosOit[0];
542 anImmFbosOit[1] = aMainFbosOit[1];
544 else if (!myTransientDrawToFront)
546 anImmFbos [0] = aMainFbos [0];
547 anImmFbos [1] = aMainFbos [1];
548 anImmFbosOit[0] = aMainFbosOit[0];
549 anImmFbosOit[1] = aMainFbosOit[1];
551 else if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip
552 || aStereoMode == Graphic3d_StereoMode_QuadBuffer)
554 anImmFbos [0] = NULL;
555 anImmFbos [1] = NULL;
556 anImmFbosOit[0] = NULL;
557 anImmFbosOit[1] = NULL;
560 #if !defined(GL_ES_VERSION_2_0)
561 aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_LEFT : GL_BACK);
563 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
564 aMainFbos[0] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
566 redraw (Graphic3d_Camera::Projection_MonoLeftEye, aMainFbos[0], aMainFbosOit[0]);
567 myBackBufferRestored = Standard_True;
568 myIsImmediateDrawn = Standard_False;
569 #if !defined(GL_ES_VERSION_2_0)
570 aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_LEFT : GL_BACK);
572 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
573 anImmFbos[0] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
574 if (!redrawImmediate (Graphic3d_Camera::Projection_MonoLeftEye, aMainFbos[0], anImmFbos[0], anImmFbosOit[0]))
578 else if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip && toSwap)
585 // push Left frame to HMD display composer
586 OpenGl_FrameBuffer* anXRFbo = hasXRBlitFbo ? myXrSceneFbo.get() : aMainFbos[0];
587 if (anXRFbo != aMainFbos[0])
589 blitBuffers (aMainFbos[0], anXRFbo); // resize or resolve MSAA samples
591 #if !defined(GL_ES_VERSION_2_0)
592 const Aspect_GraphicsLibrary aGraphicsLib = Aspect_GraphicsLibrary_OpenGL;
594 const Aspect_GraphicsLibrary aGraphicsLib = Aspect_GraphicsLibrary_OpenGLES;
596 myXRSession->SubmitEye ((void* )(size_t )anXRFbo->ColorTexture()->TextureId(),
597 aGraphicsLib, Aspect_ColorSpace_sRGB, Aspect_Eye_Left);
600 #if !defined(GL_ES_VERSION_2_0)
601 aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_RIGHT : GL_BACK);
603 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
604 aMainFbos[1] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
606 redraw (Graphic3d_Camera::Projection_MonoRightEye, aMainFbos[1], aMainFbosOit[1]);
607 myBackBufferRestored = Standard_True;
608 myIsImmediateDrawn = Standard_False;
609 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
610 anImmFbos[1] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
611 if (!redrawImmediate (Graphic3d_Camera::Projection_MonoRightEye, aMainFbos[1], anImmFbos[1], anImmFbosOit[1]))
618 // push Right frame to HMD display composer
619 OpenGl_FrameBuffer* anXRFbo = hasXRBlitFbo ? myXrSceneFbo.get() : aMainFbos[1];
620 if (anXRFbo != aMainFbos[1])
622 blitBuffers (aMainFbos[1], anXRFbo); // resize or resolve MSAA samples
624 #if !defined(GL_ES_VERSION_2_0)
625 const Aspect_GraphicsLibrary aGraphicsLib = Aspect_GraphicsLibrary_OpenGL;
627 const Aspect_GraphicsLibrary aGraphicsLib = Aspect_GraphicsLibrary_OpenGLES;
629 myXRSession->SubmitEye ((void* )(size_t )anXRFbo->ColorTexture()->TextureId(),
630 aGraphicsLib, Aspect_ColorSpace_sRGB, Aspect_Eye_Right);
633 if (myRenderParams.ToMirrorComposer)
635 blitBuffers (anXRFbo, aFrameBuffer, myToFlipOutput);
638 else if (anImmFbos[0] != NULL)
640 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(), 1.0f);
641 drawStereoPair (aFrameBuffer);
646 OpenGl_FrameBuffer* aMainFbo = myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : aFrameBuffer;
647 OpenGl_FrameBuffer* aMainFboOit = myMainSceneFbosOit[0]->IsValid() ? myMainSceneFbosOit[0].operator->() : NULL;
648 OpenGl_FrameBuffer* anImmFbo = aFrameBuffer;
649 OpenGl_FrameBuffer* anImmFboOit = NULL;
650 if (!myTransientDrawToFront)
653 anImmFboOit = aMainFboOit;
655 else if (myImmediateSceneFbos[0]->IsValid())
657 anImmFbo = myImmediateSceneFbos[0].operator->();
658 anImmFboOit = myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL;
661 #if !defined(GL_ES_VERSION_2_0)
662 if (aMainFbo == NULL)
664 aCtx->SetReadDrawBuffer (GL_BACK);
667 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
668 aMainFbo != aFrameBuffer ? myRenderParams.RenderResolutionScale : 1.0f);
670 redraw (aProjectType, aMainFbo, aMainFboOit);
671 myBackBufferRestored = Standard_True;
672 myIsImmediateDrawn = Standard_False;
673 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
674 anImmFbo != aFrameBuffer ? myRenderParams.RenderResolutionScale : 1.0f);
675 if (!redrawImmediate (aProjectType, aMainFbo, anImmFbo, anImmFboOit))
681 && anImmFbo != aFrameBuffer)
683 blitBuffers (anImmFbo, aFrameBuffer, myToFlipOutput);
687 if (myRenderParams.Method == Graphic3d_RM_RAYTRACING
688 && myRenderParams.IsGlobalIlluminationEnabled)
696 if (wasDisabledMSAA != myToDisableMSAA
697 || hadFboBlit != myHasFboBlit)
703 // reset state for safety
704 aCtx->BindProgram (Handle(OpenGl_ShaderProgram)());
705 if (aCtx->caps->ffpEnable)
707 aCtx->ShaderManager()->PushState (Handle(OpenGl_ShaderProgram)());
714 if (!myMainSceneFbos[0]->IsValid())
716 myBackBufferRestored = Standard_False;
721 aCtx->core11fwd->glFlush();
724 // reset render mode state
726 aCtx->FrameStats()->FrameEnd (myWorkspace->View(), false);
728 myWasRedrawnGL = Standard_True;
731 // =======================================================================
732 // function : RedrawImmediate
734 // =======================================================================
735 void OpenGl_View::RedrawImmediate()
737 if (!myWorkspace->Activate())
740 // no special handling of HMD display, since it will force full Redraw() due to no frame caching (myBackBufferRestored)
741 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
742 if (!myTransientDrawToFront
743 || !myBackBufferRestored
744 || (aCtx->caps->buffersNoSwap && !myMainSceneFbos[0]->IsValid()))
750 const Graphic3d_StereoMode aStereoMode = myRenderParams.StereoMode;
751 Graphic3d_Camera::Projection aProjectType = myCamera->ProjectionType();
752 OpenGl_FrameBuffer* aFrameBuffer = myFBO.get();
753 aCtx->FrameStats()->FrameStart (myWorkspace->View(), true);
755 if ( aFrameBuffer == NULL
756 && !aCtx->DefaultFrameBuffer().IsNull()
757 && aCtx->DefaultFrameBuffer()->IsValid())
759 aFrameBuffer = aCtx->DefaultFrameBuffer().operator->();
762 if (aProjectType == Graphic3d_Camera::Projection_Stereo)
764 if (myMainSceneFbos[0]->IsValid()
765 && !myMainSceneFbos[1]->IsValid())
767 aProjectType = Graphic3d_Camera::Projection_Perspective;
772 if (aProjectType == Graphic3d_Camera::Projection_Stereo)
774 OpenGl_FrameBuffer* aMainFbos[2] =
776 myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL,
777 myMainSceneFbos[1]->IsValid() ? myMainSceneFbos[1].operator->() : NULL
779 OpenGl_FrameBuffer* anImmFbos[2] =
781 myImmediateSceneFbos[0]->IsValid() ? myImmediateSceneFbos[0].operator->() : NULL,
782 myImmediateSceneFbos[1]->IsValid() ? myImmediateSceneFbos[1].operator->() : NULL
784 OpenGl_FrameBuffer* anImmFbosOit[2] =
786 myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL,
787 myImmediateSceneFbosOit[1]->IsValid() ? myImmediateSceneFbosOit[1].operator->() :
788 myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL
790 if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip
791 || aStereoMode == Graphic3d_StereoMode_QuadBuffer)
795 anImmFbosOit[0] = NULL;
796 anImmFbosOit[1] = NULL;
799 if (aCtx->arbFBO != NULL)
801 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
803 #if !defined(GL_ES_VERSION_2_0)
804 if (anImmFbos[0] == NULL)
806 aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_LEFT : GL_BACK);
810 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
811 anImmFbos[0] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
812 toSwap = redrawImmediate (Graphic3d_Camera::Projection_MonoLeftEye,
816 Standard_True) || toSwap;
817 if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip
819 && !aCtx->caps->buffersNoSwap)
824 if (aCtx->arbFBO != NULL)
826 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
828 #if !defined(GL_ES_VERSION_2_0)
829 if (anImmFbos[1] == NULL)
831 aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_RIGHT : GL_BACK);
834 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
835 anImmFbos[1] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
836 toSwap = redrawImmediate (Graphic3d_Camera::Projection_MonoRightEye,
840 Standard_True) || toSwap;
841 if (anImmFbos[0] != NULL)
843 drawStereoPair (aFrameBuffer);
848 OpenGl_FrameBuffer* aMainFbo = myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL;
849 OpenGl_FrameBuffer* anImmFbo = aFrameBuffer;
850 OpenGl_FrameBuffer* anImmFboOit = NULL;
851 if (myImmediateSceneFbos[0]->IsValid())
853 anImmFbo = myImmediateSceneFbos[0].operator->();
854 anImmFboOit = myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL;
856 #if !defined(GL_ES_VERSION_2_0)
857 if (aMainFbo == NULL)
859 aCtx->SetReadDrawBuffer (GL_BACK);
862 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
863 anImmFbo != aFrameBuffer ? myRenderParams.RenderResolutionScale : 1.0f);
864 toSwap = redrawImmediate (aProjectType,
868 Standard_True) || toSwap;
870 && anImmFbo != aFrameBuffer)
872 blitBuffers (anImmFbo, aFrameBuffer, myToFlipOutput);
879 // reset state for safety
880 aCtx->BindProgram (Handle(OpenGl_ShaderProgram)());
881 if (aCtx->caps->ffpEnable)
883 aCtx->ShaderManager()->PushState (Handle(OpenGl_ShaderProgram)());
886 if (toSwap && !aCtx->caps->buffersNoSwap)
892 aCtx->core11fwd->glFlush();
894 aCtx->FrameStats()->FrameEnd (myWorkspace->View(), true);
896 myWasRedrawnGL = Standard_True;
899 // =======================================================================
902 // =======================================================================
903 void OpenGl_View::redraw (const Graphic3d_Camera::Projection theProjection,
904 OpenGl_FrameBuffer* theReadDrawFbo,
905 OpenGl_FrameBuffer* theOitAccumFbo)
907 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
908 if (theReadDrawFbo != NULL)
910 theReadDrawFbo->BindBuffer (aCtx);
911 theReadDrawFbo->SetupViewport (aCtx);
915 const Standard_Integer aViewport[4] = { 0, 0, myWindow->Width(), myWindow->Height() };
916 aCtx->ResizeViewport (aViewport);
919 // request reset of material
920 aCtx->ShaderManager()->UpdateMaterialState();
922 myWorkspace->UseZBuffer() = Standard_True;
923 myWorkspace->UseDepthWrite() = Standard_True;
924 GLbitfield toClear = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT;
925 glDepthFunc (GL_LEQUAL);
926 glDepthMask (GL_TRUE);
927 glEnable (GL_DEPTH_TEST);
929 #if !defined(GL_ES_VERSION_2_0)
932 glClearDepthf (1.0f);
935 const OpenGl_Vec4 aBgColor = aCtx->Vec4FromQuantityColor (myBgColor);
936 glClearColor (aBgColor.r(), aBgColor.g(), aBgColor.b(), 0.0f);
940 render (theProjection, theReadDrawFbo, theOitAccumFbo, Standard_False);
943 // =======================================================================
944 // function : redrawMonoImmediate
946 // =======================================================================
947 bool OpenGl_View::redrawImmediate (const Graphic3d_Camera::Projection theProjection,
948 OpenGl_FrameBuffer* theReadFbo,
949 OpenGl_FrameBuffer* theDrawFbo,
950 OpenGl_FrameBuffer* theOitAccumFbo,
951 const Standard_Boolean theIsPartialUpdate)
953 const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext();
954 GLboolean toCopyBackToFront = GL_FALSE;
955 if (theDrawFbo == theReadFbo
956 && theDrawFbo != NULL
957 && theDrawFbo->IsValid())
959 myBackBufferRestored = Standard_False;
960 theDrawFbo->BindBuffer (aCtx);
962 else if (theReadFbo != NULL
963 && theReadFbo->IsValid()
966 if (!blitBuffers (theReadFbo, theDrawFbo))
971 else if (theDrawFbo == NULL)
973 #if !defined(GL_ES_VERSION_2_0)
974 aCtx->core11fwd->glGetBooleanv (GL_DOUBLEBUFFER, &toCopyBackToFront);
976 if (toCopyBackToFront
977 && myTransientDrawToFront)
979 if (!HasImmediateStructures()
980 && !theIsPartialUpdate)
982 // prefer Swap Buffers within Redraw in compatibility mode (without FBO)
985 if (!copyBackToFront())
987 toCopyBackToFront = GL_FALSE;
988 myBackBufferRestored = Standard_False;
993 toCopyBackToFront = GL_FALSE;
994 myBackBufferRestored = Standard_False;
999 myBackBufferRestored = Standard_False;
1001 myIsImmediateDrawn = Standard_True;
1003 myWorkspace->UseZBuffer() = Standard_True;
1004 myWorkspace->UseDepthWrite() = Standard_True;
1005 glDepthFunc (GL_LEQUAL);
1006 glDepthMask (GL_TRUE);
1007 glEnable (GL_DEPTH_TEST);
1008 #if !defined(GL_ES_VERSION_2_0)
1011 glClearDepthf (1.0f);
1014 render (theProjection, theDrawFbo, theOitAccumFbo, Standard_True);
1016 return !toCopyBackToFront;
1019 //=======================================================================
1022 //=======================================================================
1023 void OpenGl_View::render (Graphic3d_Camera::Projection theProjection,
1024 OpenGl_FrameBuffer* theOutputFBO,
1025 OpenGl_FrameBuffer* theOitAccumFbo,
1026 const Standard_Boolean theToDrawImmediate)
1028 // ==================================
1029 // Step 1: Prepare for render
1030 // ==================================
1032 const Handle(OpenGl_Context)& aContext = myWorkspace->GetGlContext();
1033 aContext->SetAllowSampleAlphaToCoverage (myRenderParams.ToEnableAlphaToCoverage
1034 && theOutputFBO != NULL
1035 && theOutputFBO->NbSamples() != 0);
1037 #if !defined(GL_ES_VERSION_2_0)
1038 // Disable current clipping planes
1039 if (aContext->core11 != NULL)
1041 const Standard_Integer aMaxPlanes = aContext->MaxClipPlanes();
1042 for (Standard_Integer aClipPlaneId = GL_CLIP_PLANE0; aClipPlaneId < GL_CLIP_PLANE0 + aMaxPlanes; ++aClipPlaneId)
1044 aContext->core11fwd->glDisable (aClipPlaneId);
1049 // update states of OpenGl_BVHTreeSelector (frustum culling algorithm);
1050 // note that we pass here window dimensions ignoring Graphic3d_RenderingParams::RenderResolutionScale
1051 myBVHSelector.SetViewVolume (myCamera);
1052 myBVHSelector.SetViewportSize (myWindow->Width(), myWindow->Height(), myRenderParams.ResolutionRatio());
1053 myBVHSelector.CacheClipPtsProjections();
1055 const Handle(OpenGl_ShaderManager)& aManager = aContext->ShaderManager();
1056 const Handle(Graphic3d_LightSet)& aLights = myShadingModel == Graphic3d_TOSM_UNLIT ? myNoShadingLight : myLights;
1057 Standard_Size aLightsRevision = 0;
1058 if (!aLights.IsNull())
1060 aLightsRevision = aLights->UpdateRevision();
1062 if (StateInfo (myCurrLightSourceState, aManager->LightSourceState().Index()) != myLastLightSourceState
1063 || aLightsRevision != myLightsRevision)
1065 myLightsRevision = aLightsRevision;
1066 aManager->UpdateLightSourceStateTo (aLights, SpecIBLMapLevels());
1067 myLastLightSourceState = StateInfo (myCurrLightSourceState, aManager->LightSourceState().Index());
1070 // Update matrices if camera has changed.
1071 Graphic3d_WorldViewProjState aWVPState = myCamera->WorldViewProjState();
1072 if (myWorldViewProjState != aWVPState)
1075 myWorldViewProjState = aWVPState;
1078 myLocalOrigin.SetCoord (0.0, 0.0, 0.0);
1079 aContext->SetCamera (myCamera);
1080 if (aManager->ModelWorldState().Index() == 0)
1082 aContext->ShaderManager()->UpdateModelWorldStateTo (OpenGl_Mat4());
1085 // ====================================
1086 // Step 2: Redraw background
1087 // ====================================
1089 // Render background
1090 if (!theToDrawImmediate)
1092 drawBackground (myWorkspace, theProjection);
1095 #if !defined(GL_ES_VERSION_2_0)
1096 // Switch off lighting by default
1097 if (aContext->core11 != NULL
1098 && aContext->caps->ffpEnable)
1100 glDisable(GL_LIGHTING);
1104 // =================================
1105 // Step 3: Redraw main plane
1106 // =================================
1108 // Setup face culling
1109 GLboolean isCullFace = GL_FALSE;
1110 if (myBackfacing != Graphic3d_TOBM_AUTOMATIC)
1112 isCullFace = glIsEnabled (GL_CULL_FACE);
1113 if (myBackfacing == Graphic3d_TOBM_DISABLE)
1115 glEnable (GL_CULL_FACE);
1116 glCullFace (GL_BACK);
1119 glDisable (GL_CULL_FACE);
1122 #if !defined(GL_ES_VERSION_2_0)
1123 // if the view is scaled normal vectors are scaled to unit
1124 // length for correct displaying of shaded objects
1125 const gp_Pnt anAxialScale = aContext->Camera()->AxialScale();
1126 if (anAxialScale.X() != 1.F ||
1127 anAxialScale.Y() != 1.F ||
1128 anAxialScale.Z() != 1.F)
1130 aContext->SetGlNormalizeEnabled (Standard_True);
1134 aContext->SetGlNormalizeEnabled (Standard_False);
1138 aManager->SetShadingModel (OpenGl_ShaderManager::PBRShadingModelFallback (myShadingModel, checkPBRAvailability()));
1141 if (theProjection == Graphic3d_Camera::Projection_MonoLeftEye)
1143 aContext->ProjectionState.SetCurrent (aContext->Camera()->ProjectionStereoLeftF());
1144 aContext->ApplyProjectionMatrix();
1146 else if (theProjection == Graphic3d_Camera::Projection_MonoRightEye)
1148 aContext->ProjectionState.SetCurrent (aContext->Camera()->ProjectionStereoRightF());
1149 aContext->ApplyProjectionMatrix();
1152 myWorkspace->SetEnvironmentTexture (myTextureEnv);
1154 renderScene (theProjection, theOutputFBO, theOitAccumFbo, theToDrawImmediate);
1156 myWorkspace->SetEnvironmentTexture (Handle(OpenGl_TextureSet)());
1158 // ===============================
1159 // Step 4: Trihedron
1160 // ===============================
1162 // Resetting GL parameters according to the default aspects
1163 // in order to synchronize GL state with the graphic driver state
1164 // before drawing auxiliary stuff (trihedrons, overlayer)
1165 myWorkspace->ResetAppliedAspect();
1168 if (!theToDrawImmediate)
1170 renderTrihedron (myWorkspace);
1172 // Restore face culling
1173 if (myBackfacing != Graphic3d_TOBM_AUTOMATIC)
1177 glEnable (GL_CULL_FACE);
1178 glCullFace (GL_BACK);
1181 glDisable (GL_CULL_FACE);
1189 myWorkspace->ResetAppliedAspect();
1190 aContext->SetAllowSampleAlphaToCoverage (false);
1191 aContext->SetSampleAlphaToCoverage (false);
1193 // reset FFP state for safety
1194 aContext->BindProgram (Handle(OpenGl_ShaderProgram)());
1195 if (aContext->caps->ffpEnable)
1197 aContext->ShaderManager()->PushState (Handle(OpenGl_ShaderProgram)());
1200 // ==============================================================
1201 // Step 6: Keep shader manager informed about last View
1202 // ==============================================================
1204 if (!aManager.IsNull())
1206 aManager->SetLastView (this);
1210 // =======================================================================
1211 // function : InvalidateBVHData
1213 // =======================================================================
1214 void OpenGl_View::InvalidateBVHData (const Graphic3d_ZLayerId theLayerId)
1216 myZLayers.InvalidateBVHData (theLayerId);
1219 //=======================================================================
1220 //function : renderStructs
1222 //=======================================================================
1223 void OpenGl_View::renderStructs (Graphic3d_Camera::Projection theProjection,
1224 OpenGl_FrameBuffer* theReadDrawFbo,
1225 OpenGl_FrameBuffer* theOitAccumFbo,
1226 const Standard_Boolean theToDrawImmediate)
1228 myZLayers.UpdateCulling (myWorkspace, theToDrawImmediate);
1229 if ( myZLayers.NbStructures() <= 0 )
1232 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
1233 Standard_Boolean toRenderGL = theToDrawImmediate ||
1234 myRenderParams.Method != Graphic3d_RM_RAYTRACING ||
1235 myRaytraceInitStatus == OpenGl_RT_FAIL ||
1240 const Standard_Integer aSizeX = theReadDrawFbo != NULL ? theReadDrawFbo->GetVPSizeX() : myWindow->Width();
1241 const Standard_Integer aSizeY = theReadDrawFbo != NULL ? theReadDrawFbo->GetVPSizeY() : myWindow->Height();
1243 toRenderGL = !initRaytraceResources (aSizeX, aSizeY, aCtx)
1244 || !updateRaytraceGeometry (OpenGl_GUM_CHECK, myId, aCtx);
1246 toRenderGL |= !myIsRaytraceDataValid; // if no ray-trace data use OpenGL
1250 myOpenGlFBO ->InitLazy (aCtx, aSizeX, aSizeY, myFboColorFormat, myFboDepthFormat, 0);
1252 if (theReadDrawFbo != NULL)
1253 theReadDrawFbo->UnbindBuffer (aCtx);
1255 // Prepare preliminary OpenGL output
1256 if (aCtx->arbFBOBlit != NULL)
1258 // Render bottom OSD layer
1259 myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_Bottom, theReadDrawFbo, theOitAccumFbo);
1261 const Standard_Integer aPrevFilter = myWorkspace->RenderFilter() & ~(Standard_Integer )(OpenGl_RenderFilter_NonRaytraceableOnly);
1262 myWorkspace->SetRenderFilter (aPrevFilter | OpenGl_RenderFilter_NonRaytraceableOnly);
1264 if (theReadDrawFbo != NULL)
1266 theReadDrawFbo->BindDrawBuffer (aCtx);
1270 aCtx->arbFBO->glBindFramebuffer (GL_DRAW_FRAMEBUFFER, 0);
1271 aCtx->SetFrameBufferSRGB (false);
1274 // Render non-polygonal elements in default layer
1275 myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_RayTracable, theReadDrawFbo, theOitAccumFbo);
1277 myWorkspace->SetRenderFilter (aPrevFilter);
1280 if (theReadDrawFbo != NULL)
1282 theReadDrawFbo->BindBuffer (aCtx);
1286 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, 0);
1287 aCtx->SetFrameBufferSRGB (false);
1290 // Reset OpenGl aspects state to default to avoid enabling of
1291 // backface culling which is not supported in ray-tracing.
1292 myWorkspace->ResetAppliedAspect();
1294 // Ray-tracing polygonal primitive arrays
1295 raytrace (aSizeX, aSizeY, theProjection, theReadDrawFbo, aCtx);
1297 // Render upper (top and topmost) OpenGL layers
1298 myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_Upper, theReadDrawFbo, theOitAccumFbo);
1302 // Redraw 3D scene using OpenGL in standard
1303 // mode or in case of ray-tracing failure
1306 myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_All, theReadDrawFbo, theOitAccumFbo);
1308 // Set flag that scene was redrawn by standard pipeline
1309 myWasRedrawnGL = Standard_True;
1313 //=======================================================================
1314 //function : renderTrihedron
1316 //=======================================================================
1317 void OpenGl_View::renderTrihedron (const Handle(OpenGl_Workspace) &theWorkspace)
1319 if (myToShowGradTrihedron)
1321 myGraduatedTrihedron.Render (theWorkspace);
1325 //=======================================================================
1326 //function : renderFrameStats
1328 //=======================================================================
1329 void OpenGl_View::renderFrameStats()
1331 if (myRenderParams.ToShowStats
1332 && myRenderParams.CollectedStats != Graphic3d_RenderingParams::PerfCounters_NONE)
1334 myFrameStatsPrs.Update (myWorkspace);
1335 myFrameStatsPrs.Render (myWorkspace);
1339 // =======================================================================
1340 // function : Invalidate
1342 // =======================================================================
1343 void OpenGl_View::Invalidate()
1345 myBackBufferRestored = Standard_False;
1348 //=======================================================================
1349 //function : renderScene
1351 //=======================================================================
1352 void OpenGl_View::renderScene (Graphic3d_Camera::Projection theProjection,
1353 OpenGl_FrameBuffer* theReadDrawFbo,
1354 OpenGl_FrameBuffer* theOitAccumFbo,
1355 const Standard_Boolean theToDrawImmediate)
1357 const Handle(OpenGl_Context)& aContext = myWorkspace->GetGlContext();
1359 // Specify clipping planes in view transformation space
1360 aContext->ChangeClipping().Reset (myClipPlanes);
1361 if (!myClipPlanes.IsNull()
1362 && !myClipPlanes->IsEmpty())
1364 aContext->ShaderManager()->UpdateClippingState();
1367 renderStructs (theProjection, theReadDrawFbo, theOitAccumFbo, theToDrawImmediate);
1368 aContext->BindTextures (Handle(OpenGl_TextureSet)(), Handle(OpenGl_ShaderProgram)());
1370 // Apply restored view matrix.
1371 aContext->ApplyWorldViewMatrix();
1373 aContext->ChangeClipping().Reset (Handle(Graphic3d_SequenceOfHClipPlane)());
1374 if (!myClipPlanes.IsNull()
1375 && !myClipPlanes->IsEmpty())
1377 aContext->ShaderManager()->RevertClippingState();
1381 // =======================================================================
1382 // function : bindDefaultFbo
1384 // =======================================================================
1385 void OpenGl_View::bindDefaultFbo (OpenGl_FrameBuffer* theCustomFbo)
1387 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
1388 OpenGl_FrameBuffer* anFbo = (theCustomFbo != NULL && theCustomFbo->IsValid())
1390 : (!aCtx->DefaultFrameBuffer().IsNull()
1391 && aCtx->DefaultFrameBuffer()->IsValid()
1392 ? aCtx->DefaultFrameBuffer().operator->()
1396 anFbo->BindBuffer (aCtx);
1397 anFbo->SetupViewport (aCtx);
1401 #if !defined(GL_ES_VERSION_2_0)
1402 aCtx->SetReadDrawBuffer (GL_BACK);
1404 if (aCtx->arbFBO != NULL)
1406 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
1409 const Standard_Integer aViewport[4] = { 0, 0, myWindow->Width(), myWindow->Height() };
1410 aCtx->ResizeViewport (aViewport);
1414 // =======================================================================
1415 // function : initBlitQuad
1417 // =======================================================================
1418 OpenGl_VertexBuffer* OpenGl_View::initBlitQuad (const Standard_Boolean theToFlip)
1420 OpenGl_VertexBuffer* aVerts = NULL;
1423 aVerts = &myFullScreenQuad;
1424 if (!aVerts->IsValid())
1426 OpenGl_Vec4 aQuad[4] =
1428 OpenGl_Vec4( 1.0f, -1.0f, 1.0f, 0.0f),
1429 OpenGl_Vec4( 1.0f, 1.0f, 1.0f, 1.0f),
1430 OpenGl_Vec4(-1.0f, -1.0f, 0.0f, 0.0f),
1431 OpenGl_Vec4(-1.0f, 1.0f, 0.0f, 1.0f)
1433 aVerts->Init (myWorkspace->GetGlContext(), 4, 4, aQuad[0].GetData());
1438 aVerts = &myFullScreenQuadFlip;
1439 if (!aVerts->IsValid())
1441 OpenGl_Vec4 aQuad[4] =
1443 OpenGl_Vec4( 1.0f, -1.0f, 1.0f, 1.0f),
1444 OpenGl_Vec4( 1.0f, 1.0f, 1.0f, 0.0f),
1445 OpenGl_Vec4(-1.0f, -1.0f, 0.0f, 1.0f),
1446 OpenGl_Vec4(-1.0f, 1.0f, 0.0f, 0.0f)
1448 aVerts->Init (myWorkspace->GetGlContext(), 4, 4, aQuad[0].GetData());
1454 // =======================================================================
1455 // function : blitBuffers
1457 // =======================================================================
1458 bool OpenGl_View::blitBuffers (OpenGl_FrameBuffer* theReadFbo,
1459 OpenGl_FrameBuffer* theDrawFbo,
1460 const Standard_Boolean theToFlip)
1462 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
1463 const Standard_Integer aReadSizeX = theReadFbo != NULL ? theReadFbo->GetVPSizeX() : myWindow->Width();
1464 const Standard_Integer aReadSizeY = theReadFbo != NULL ? theReadFbo->GetVPSizeY() : myWindow->Height();
1465 const Standard_Integer aDrawSizeX = theDrawFbo != NULL ? theDrawFbo->GetVPSizeX() : myWindow->Width();
1466 const Standard_Integer aDrawSizeY = theDrawFbo != NULL ? theDrawFbo->GetVPSizeY() : myWindow->Height();
1467 if (theReadFbo == NULL || aCtx->IsFeedback())
1471 else if (theReadFbo == theDrawFbo)
1476 // clear destination before blitting
1477 if (theDrawFbo != NULL
1478 && theDrawFbo->IsValid())
1480 theDrawFbo->BindBuffer (aCtx);
1484 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
1485 aCtx->SetFrameBufferSRGB (false);
1487 const Standard_Integer aViewport[4] = { 0, 0, aDrawSizeX, aDrawSizeY };
1488 aCtx->ResizeViewport (aViewport);
1490 #if !defined(GL_ES_VERSION_2_0)
1491 aCtx->core20fwd->glClearDepth (1.0);
1493 aCtx->core20fwd->glClearDepthf (1.0f);
1495 aCtx->core20fwd->glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
1497 const bool toApplyGamma = aCtx->ToRenderSRGB() != aCtx->IsFrameBufferSRGB();
1498 if (aCtx->arbFBOBlit != NULL
1500 && theReadFbo->NbSamples() != 0)
1502 GLbitfield aCopyMask = 0;
1503 theReadFbo->BindReadBuffer (aCtx);
1504 if (theDrawFbo != NULL
1505 && theDrawFbo->IsValid())
1507 theDrawFbo->BindDrawBuffer (aCtx);
1508 if (theDrawFbo->HasColor()
1509 && theReadFbo->HasColor())
1511 aCopyMask |= GL_COLOR_BUFFER_BIT;
1513 if (theDrawFbo->HasDepth()
1514 && theReadFbo->HasDepth())
1516 aCopyMask |= GL_DEPTH_BUFFER_BIT;
1521 if (theReadFbo->HasColor())
1523 aCopyMask |= GL_COLOR_BUFFER_BIT;
1525 if (theReadFbo->HasDepth())
1527 aCopyMask |= GL_DEPTH_BUFFER_BIT;
1529 aCtx->arbFBO->glBindFramebuffer (GL_DRAW_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
1530 aCtx->SetFrameBufferSRGB (false);
1533 // we don't copy stencil buffer here... does it matter for performance?
1534 aCtx->arbFBOBlit->glBlitFramebuffer (0, 0, aReadSizeX, aReadSizeY,
1535 0, 0, aDrawSizeX, aDrawSizeY,
1536 aCopyMask, GL_NEAREST);
1537 const int anErr = ::glGetError();
1538 if (anErr != GL_NO_ERROR)
1540 // glBlitFramebuffer() might fail in several cases:
1541 // - Both FBOs have MSAA and they are samples number does not match.
1542 // OCCT checks that this does not happen,
1543 // however some graphics drivers provide an option for overriding MSAA.
1544 // In this case window MSAA might be non-zero (and application can not check it)
1545 // and might not match MSAA of our offscreen FBOs.
1546 // - Pixel formats of FBOs do not match.
1547 // This also might happen with window has pixel format,
1548 // e.g. Mesa fails blitting RGBA8 -> RGB8 while other drivers support this conversion.
1549 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "FBO blitting has failed [Error #" + anErr + "]\n"
1550 + " Please check your graphics driver settings or try updating driver.";
1551 if (theReadFbo->NbSamples() != 0)
1553 myToDisableMSAA = true;
1554 aMsg += "\n MSAA settings should not be overridden by driver!";
1556 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1557 GL_DEBUG_TYPE_ERROR,
1559 GL_DEBUG_SEVERITY_HIGH,
1563 if (theDrawFbo != NULL
1564 && theDrawFbo->IsValid())
1566 theDrawFbo->BindBuffer (aCtx);
1570 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
1571 aCtx->SetFrameBufferSRGB (false);
1576 aCtx->core20fwd->glDepthFunc (GL_ALWAYS);
1577 aCtx->core20fwd->glDepthMask (GL_TRUE);
1578 aCtx->core20fwd->glEnable (GL_DEPTH_TEST);
1579 #if defined(GL_ES_VERSION_2_0)
1580 if (!aCtx->IsGlGreaterEqual (3, 0)
1581 && !aCtx->extFragDepth)
1583 aCtx->core20fwd->glDisable (GL_DEPTH_TEST);
1587 aCtx->BindTextures (Handle(OpenGl_TextureSet)(), Handle(OpenGl_ShaderProgram)());
1589 const Graphic3d_TypeOfTextureFilter aFilter = (aDrawSizeX == aReadSizeX && aDrawSizeY == aReadSizeY) ? Graphic3d_TOTF_NEAREST : Graphic3d_TOTF_BILINEAR;
1590 const GLint aFilterGl = aFilter == Graphic3d_TOTF_NEAREST ? GL_NEAREST : GL_LINEAR;
1592 OpenGl_VertexBuffer* aVerts = initBlitQuad (theToFlip);
1593 const Handle(OpenGl_ShaderManager)& aManager = aCtx->ShaderManager();
1594 if (aVerts->IsValid()
1595 && aManager->BindFboBlitProgram (theReadFbo != NULL ? theReadFbo->NbSamples() : 0, toApplyGamma))
1597 aCtx->SetSampleAlphaToCoverage (false);
1598 theReadFbo->ColorTexture()->Bind (aCtx, Graphic3d_TextureUnit_0);
1599 if (theReadFbo->ColorTexture()->Sampler()->Parameters()->Filter() != aFilter)
1601 theReadFbo->ColorTexture()->Sampler()->Parameters()->SetFilter (aFilter);
1602 aCtx->core20fwd->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, aFilterGl);
1603 aCtx->core20fwd->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, aFilterGl);
1606 theReadFbo->DepthStencilTexture()->Bind (aCtx, Graphic3d_TextureUnit_1);
1607 if (theReadFbo->DepthStencilTexture()->Sampler()->Parameters()->Filter() != aFilter)
1609 theReadFbo->DepthStencilTexture()->Sampler()->Parameters()->SetFilter (aFilter);
1610 aCtx->core20fwd->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, aFilterGl);
1611 aCtx->core20fwd->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, aFilterGl);
1614 aVerts->BindVertexAttrib (aCtx, Graphic3d_TOA_POS);
1616 aCtx->core20fwd->glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
1618 aVerts->UnbindVertexAttrib (aCtx, Graphic3d_TOA_POS);
1619 theReadFbo->DepthStencilTexture()->Unbind (aCtx, Graphic3d_TextureUnit_1);
1620 theReadFbo->ColorTexture() ->Unbind (aCtx, Graphic3d_TextureUnit_0);
1621 aCtx->BindProgram (NULL);
1625 TCollection_ExtendedString aMsg = TCollection_ExtendedString()
1626 + "Error! FBO blitting has failed";
1627 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1628 GL_DEBUG_TYPE_ERROR,
1630 GL_DEBUG_SEVERITY_HIGH,
1632 myHasFboBlit = Standard_False;
1633 theReadFbo->Release (aCtx.operator->());
1640 // =======================================================================
1641 // function : drawStereoPair
1643 // =======================================================================
1644 void OpenGl_View::drawStereoPair (OpenGl_FrameBuffer* theDrawFbo)
1646 const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext();
1647 bindDefaultFbo (theDrawFbo);
1648 OpenGl_FrameBuffer* aPair[2] =
1650 myImmediateSceneFbos[0]->IsValid() ? myImmediateSceneFbos[0].operator->() : NULL,
1651 myImmediateSceneFbos[1]->IsValid() ? myImmediateSceneFbos[1].operator->() : NULL
1653 if (aPair[0] == NULL
1655 || !myTransientDrawToFront)
1657 aPair[0] = myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL;
1658 aPair[1] = myMainSceneFbos[1]->IsValid() ? myMainSceneFbos[1].operator->() : NULL;
1661 if (aPair[0] == NULL
1662 || aPair[1] == NULL)
1667 if (aPair[0]->NbSamples() != 0)
1669 // resolve MSAA buffers before drawing
1670 if (!myOpenGlFBO ->InitLazy (aCtx, aPair[0]->GetVPSizeX(), aPair[0]->GetVPSizeY(), myFboColorFormat, myFboDepthFormat, 0)
1671 || !myOpenGlFBO2->InitLazy (aCtx, aPair[0]->GetVPSizeX(), aPair[0]->GetVPSizeY(), myFboColorFormat, 0, 0))
1673 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1674 GL_DEBUG_TYPE_ERROR,
1676 GL_DEBUG_SEVERITY_HIGH,
1677 "Error! Unable to allocate FBO for blitting stereo pair");
1678 bindDefaultFbo (theDrawFbo);
1682 if (!blitBuffers (aPair[0], myOpenGlFBO .operator->(), Standard_False)
1683 || !blitBuffers (aPair[1], myOpenGlFBO2.operator->(), Standard_False))
1685 bindDefaultFbo (theDrawFbo);
1689 aPair[0] = myOpenGlFBO .operator->();
1690 aPair[1] = myOpenGlFBO2.operator->();
1691 bindDefaultFbo (theDrawFbo);
1696 Standard_Integer left;
1697 Standard_Integer top;
1698 Standard_Integer right;
1699 Standard_Integer bottom;
1700 Standard_Integer dx() { return right - left; }
1701 Standard_Integer dy() { return bottom - top; }
1704 myWindow->PlatformWindow()->Position (aGeom.left, aGeom.top, aGeom.right, aGeom.bottom);
1706 Standard_Boolean toReverse = myRenderParams.ToReverseStereo;
1707 const Standard_Boolean isOddY = (aGeom.top + aGeom.dy()) % 2 == 1;
1708 const Standard_Boolean isOddX = aGeom.left % 2 == 1;
1710 && (myRenderParams.StereoMode == Graphic3d_StereoMode_RowInterlaced
1711 || myRenderParams.StereoMode == Graphic3d_StereoMode_ChessBoard))
1713 toReverse = !toReverse;
1716 && (myRenderParams.StereoMode == Graphic3d_StereoMode_ColumnInterlaced
1717 || myRenderParams.StereoMode == Graphic3d_StereoMode_ChessBoard))
1719 toReverse = !toReverse;
1724 std::swap (aPair[0], aPair[1]);
1727 aCtx->core20fwd->glDepthFunc (GL_ALWAYS);
1728 aCtx->core20fwd->glDepthMask (GL_TRUE);
1729 aCtx->core20fwd->glEnable (GL_DEPTH_TEST);
1731 aCtx->BindTextures (Handle(OpenGl_TextureSet)(), Handle(OpenGl_ShaderProgram)());
1732 OpenGl_VertexBuffer* aVerts = initBlitQuad (myToFlipOutput);
1734 const Handle(OpenGl_ShaderManager)& aManager = aCtx->ShaderManager();
1735 if (aVerts->IsValid()
1736 && aManager->BindStereoProgram (myRenderParams.StereoMode))
1738 if (myRenderParams.StereoMode == Graphic3d_StereoMode_Anaglyph)
1740 OpenGl_Mat4 aFilterL, aFilterR;
1741 aFilterL.SetDiagonal (Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f));
1742 aFilterR.SetDiagonal (Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f));
1743 switch (myRenderParams.AnaglyphFilter)
1745 case Graphic3d_RenderingParams::Anaglyph_RedCyan_Simple:
1747 aFilterL.SetRow (0, Graphic3d_Vec4 (1.0f, 0.0f, 0.0f, 0.0f));
1748 aFilterR.SetRow (1, Graphic3d_Vec4 (0.0f, 1.0f, 0.0f, 0.0f));
1749 aFilterR.SetRow (2, Graphic3d_Vec4 (0.0f, 0.0f, 1.0f, 0.0f));
1752 case Graphic3d_RenderingParams::Anaglyph_RedCyan_Optimized:
1754 aFilterL.SetRow (0, Graphic3d_Vec4 ( 0.4154f, 0.4710f, 0.16666667f, 0.0f));
1755 aFilterL.SetRow (1, Graphic3d_Vec4 (-0.0458f, -0.0484f, -0.0257f, 0.0f));
1756 aFilterL.SetRow (2, Graphic3d_Vec4 (-0.0547f, -0.0615f, 0.0128f, 0.0f));
1757 aFilterL.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f));
1758 aFilterR.SetRow (0, Graphic3d_Vec4 (-0.01090909f, -0.03636364f, -0.00606061f, 0.0f));
1759 aFilterR.SetRow (1, Graphic3d_Vec4 ( 0.37560000f, 0.73333333f, 0.01111111f, 0.0f));
1760 aFilterR.SetRow (2, Graphic3d_Vec4 (-0.06510000f, -0.12870000f, 1.29710000f, 0.0f));
1761 aFilterR.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f));
1764 case Graphic3d_RenderingParams::Anaglyph_YellowBlue_Simple:
1766 aFilterL.SetRow (0, Graphic3d_Vec4 (1.0f, 0.0f, 0.0f, 0.0f));
1767 aFilterL.SetRow (1, Graphic3d_Vec4 (0.0f, 1.0f, 0.0f, 0.0f));
1768 aFilterR.SetRow (2, Graphic3d_Vec4 (0.0f, 0.0f, 1.0f, 0.0f));
1771 case Graphic3d_RenderingParams::Anaglyph_YellowBlue_Optimized:
1773 aFilterL.SetRow (0, Graphic3d_Vec4 ( 1.062f, -0.205f, 0.299f, 0.0f));
1774 aFilterL.SetRow (1, Graphic3d_Vec4 (-0.026f, 0.908f, 0.068f, 0.0f));
1775 aFilterL.SetRow (2, Graphic3d_Vec4 (-0.038f, -0.173f, 0.022f, 0.0f));
1776 aFilterL.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f));
1777 aFilterR.SetRow (0, Graphic3d_Vec4 (-0.016f, -0.123f, -0.017f, 0.0f));
1778 aFilterR.SetRow (1, Graphic3d_Vec4 ( 0.006f, 0.062f, -0.017f, 0.0f));
1779 aFilterR.SetRow (2, Graphic3d_Vec4 ( 0.094f, 0.185f, 0.911f, 0.0f));
1780 aFilterR.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f));
1783 case Graphic3d_RenderingParams::Anaglyph_GreenMagenta_Simple:
1785 aFilterR.SetRow (0, Graphic3d_Vec4 (1.0f, 0.0f, 0.0f, 0.0f));
1786 aFilterL.SetRow (1, Graphic3d_Vec4 (0.0f, 1.0f, 0.0f, 0.0f));
1787 aFilterR.SetRow (2, Graphic3d_Vec4 (0.0f, 0.0f, 1.0f, 0.0f));
1790 case Graphic3d_RenderingParams::Anaglyph_UserDefined:
1792 aFilterL = myRenderParams.AnaglyphLeft;
1793 aFilterR = myRenderParams.AnaglyphRight;
1797 aCtx->ActiveProgram()->SetUniform (aCtx, "uMultL", aFilterL);
1798 aCtx->ActiveProgram()->SetUniform (aCtx, "uMultR", aFilterR);
1801 aPair[0]->ColorTexture()->Bind (aCtx, Graphic3d_TextureUnit_0);
1802 aPair[1]->ColorTexture()->Bind (aCtx, Graphic3d_TextureUnit_1);
1803 aVerts->BindVertexAttrib (aCtx, 0);
1805 aCtx->core20fwd->glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
1807 aVerts->UnbindVertexAttrib (aCtx, 0);
1808 aPair[1]->ColorTexture()->Unbind (aCtx, Graphic3d_TextureUnit_1);
1809 aPair[0]->ColorTexture()->Unbind (aCtx, Graphic3d_TextureUnit_0);
1813 TCollection_ExtendedString aMsg = TCollection_ExtendedString()
1814 + "Error! Anaglyph has failed";
1815 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1816 GL_DEBUG_TYPE_ERROR,
1818 GL_DEBUG_SEVERITY_HIGH,
1823 // =======================================================================
1824 // function : copyBackToFront
1826 // =======================================================================
1827 bool OpenGl_View::copyBackToFront()
1829 myIsImmediateDrawn = Standard_False;
1830 #if !defined(GL_ES_VERSION_2_0)
1831 const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext();
1832 if (aCtx->core11 == NULL)
1837 OpenGl_Mat4 aProjectMat;
1838 Graphic3d_TransformUtils::Ortho2D (aProjectMat,
1839 0.0f, static_cast<GLfloat> (myWindow->Width()),
1840 0.0f, static_cast<GLfloat> (myWindow->Height()));
1842 aCtx->WorldViewState.Push();
1843 aCtx->ProjectionState.Push();
1845 aCtx->WorldViewState.SetIdentity();
1846 aCtx->ProjectionState.SetCurrent (aProjectMat);
1848 aCtx->ApplyProjectionMatrix();
1849 aCtx->ApplyWorldViewMatrix();
1851 // synchronize FFP state before copying pixels
1852 aCtx->BindProgram (Handle(OpenGl_ShaderProgram)());
1853 aCtx->ShaderManager()->PushState (Handle(OpenGl_ShaderProgram)());
1854 aCtx->DisableFeatures();
1856 switch (aCtx->DrawBuffer())
1860 aCtx->SetReadBuffer (GL_BACK_LEFT);
1861 aCtx->SetDrawBuffer (GL_FRONT_LEFT);
1866 aCtx->SetReadBuffer (GL_BACK_RIGHT);
1867 aCtx->SetDrawBuffer (GL_FRONT_RIGHT);
1872 aCtx->SetReadBuffer (GL_BACK);
1873 aCtx->SetDrawBuffer (GL_FRONT);
1878 aCtx->core11->glRasterPos2i (0, 0);
1879 aCtx->core11->glCopyPixels (0, 0, myWindow->Width() + 1, myWindow->Height() + 1, GL_COLOR);
1880 //aCtx->core11->glCopyPixels (0, 0, myWidth + 1, myHeight + 1, GL_DEPTH);
1882 aCtx->EnableFeatures();
1884 aCtx->WorldViewState.Pop();
1885 aCtx->ProjectionState.Pop();
1886 aCtx->ApplyProjectionMatrix();
1888 // read/write from front buffer now
1889 aCtx->SetReadBuffer (aCtx->DrawBuffer());
1896 // =======================================================================
1897 // function : checkOitCompatibility
1899 // =======================================================================
1900 Standard_Boolean OpenGl_View::checkOitCompatibility (const Handle(OpenGl_Context)& theGlContext,
1901 const Standard_Boolean theMSAA)
1903 // determine if OIT is supported by current OpenGl context
1904 Standard_Boolean& aToDisableOIT = theMSAA ? myToDisableMSAA : myToDisableOIT;
1907 return Standard_False;
1910 TCollection_ExtendedString aCompatibilityMsg;
1911 if (theGlContext->hasFloatBuffer == OpenGl_FeatureNotAvailable
1912 && theGlContext->hasHalfFloatBuffer == OpenGl_FeatureNotAvailable)
1914 aCompatibilityMsg += "OpenGL context does not support floating-point RGBA color buffer format.\n";
1916 if (theMSAA && theGlContext->hasSampleVariables == OpenGl_FeatureNotAvailable)
1918 aCompatibilityMsg += "Current version of GLSL does not support built-in sample variables.\n";
1920 if (theGlContext->hasDrawBuffers == OpenGl_FeatureNotAvailable)
1922 aCompatibilityMsg += "OpenGL context does not support multiple draw buffers.\n";
1924 if (aCompatibilityMsg.IsEmpty())
1926 return Standard_True;
1929 aCompatibilityMsg += " Blended order-independent transparency will not be available.\n";
1930 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1931 GL_DEBUG_TYPE_ERROR,
1933 GL_DEBUG_SEVERITY_HIGH,
1936 aToDisableOIT = Standard_True;
1937 return Standard_False;
1940 // =======================================================================
1941 // function : chooseOitColorConfiguration
1943 // =======================================================================
1944 bool OpenGl_View::chooseOitColorConfiguration (const Handle(OpenGl_Context)& theGlContext,
1945 const Standard_Integer theConfigIndex,
1946 OpenGl_ColorFormats& theFormats)
1949 switch (theConfigIndex)
1951 case 0: // choose best applicable color format combination
1953 theFormats.Append (theGlContext->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable ? GL_RGBA16F : GL_RGBA32F);
1954 theFormats.Append (theGlContext->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable ? GL_R16F : GL_R32F);
1957 case 1: // choose non-optimal applicable color format combination
1959 theFormats.Append (theGlContext->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable ? GL_RGBA16F : GL_RGBA32F);
1960 theFormats.Append (theGlContext->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable ? GL_RGBA16F : GL_RGBA32F);
1964 return false; // color combination does not exist
1967 // =======================================================================
1968 // function : checkPBRAvailability
1970 // =======================================================================
1971 Standard_Boolean OpenGl_View::checkPBRAvailability() const
1973 return myWorkspace->GetGlContext()->HasPBR()
1974 && !myPBREnvironment.IsNull();
1977 // =======================================================================
1978 // function : bakePBREnvironment
1980 // =======================================================================
1981 void OpenGl_View::bakePBREnvironment (const Handle(OpenGl_Context)& theCtx)
1983 const Handle(OpenGl_TextureSet)& aTextureSet = myCubeMapParams->TextureSet (theCtx);
1984 if (!aTextureSet.IsNull()
1985 && !aTextureSet->IsEmpty())
1987 myPBREnvironment->Bake (theCtx,
1988 aTextureSet->First(),
1989 myBackgroundCubeMap->ZIsInverted(),
1990 myBackgroundCubeMap->IsTopDown(),
1991 myRenderParams.PbrEnvBakingDiffNbSamples,
1992 myRenderParams.PbrEnvBakingSpecNbSamples,
1993 myRenderParams.PbrEnvBakingProbability);
1997 myPBREnvironment->Clear (theCtx);
2001 // =======================================================================
2002 // function : clearPBREnvironment
2004 // =======================================================================
2005 void OpenGl_View::clearPBREnvironment (const Handle(OpenGl_Context)& theCtx)
2007 myPBREnvironment->Clear (theCtx);
2010 // =======================================================================
2011 // function : clearPBREnvironment
2013 // =======================================================================
2014 void OpenGl_View::processPBREnvRequest (const Handle(OpenGl_Context)& theCtx)
2016 if (myPBREnvState == OpenGl_PBREnvState_CREATED)
2018 switch (myPBREnvRequest)
2020 case OpenGl_PBREnvRequest_NONE: return;
2021 case OpenGl_PBREnvRequest_BAKE: bakePBREnvironment (theCtx); break;
2022 case OpenGl_PBREnvRequest_CLEAR: clearPBREnvironment (theCtx); break;
2025 myPBREnvRequest = OpenGl_PBREnvRequest_NONE;