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 <Graphic3d_GraphicDriver.hxx>
22 #include <Graphic3d_StructureManager.hxx>
23 #include <Graphic3d_TextureParams.hxx>
24 #include <Graphic3d_Texture2Dmanual.hxx>
25 #include <Graphic3d_TransformUtils.hxx>
26 #include <Image_AlienPixMap.hxx>
28 #include <NCollection_Mat4.hxx>
30 #include <OpenGl_Context.hxx>
31 #include <OpenGl_FrameStats.hxx>
32 #include <OpenGl_Matrix.hxx>
33 #include <OpenGl_Workspace.hxx>
34 #include <OpenGl_View.hxx>
35 #include <OpenGl_GraduatedTrihedron.hxx>
36 #include <OpenGl_PrimitiveArray.hxx>
37 #include <OpenGl_ShaderManager.hxx>
38 #include <OpenGl_ShaderProgram.hxx>
39 #include <OpenGl_Structure.hxx>
40 #include <OpenGl_ArbFBO.hxx>
42 #include "../Textures/Textures_EnvLUT.pxx"
46 //! Format Frame Buffer format for logging messages.
47 static TCollection_AsciiString printFboFormat (const Handle(OpenGl_FrameBuffer)& theFbo)
49 return TCollection_AsciiString() + theFbo->GetInitVPSizeX() + "x" + theFbo->GetInitVPSizeY() + "@" + theFbo->NbSamples();
52 //! Return TRUE if Frame Buffer initialized has failed with the same parameters.
53 static bool checkWasFailedFbo (const Handle(OpenGl_FrameBuffer)& theFboToCheck,
54 Standard_Integer theSizeX,
55 Standard_Integer theSizeY,
56 Standard_Integer theNbSamples)
58 return !theFboToCheck->IsValid()
59 && theFboToCheck->GetInitVPSizeX() == theSizeX
60 && theFboToCheck->GetInitVPSizeY() == theSizeY
61 && theFboToCheck->NbSamples() == theNbSamples;
64 //! Return TRUE if Frame Buffer initialized has failed with the same parameters.
65 static bool checkWasFailedFbo (const Handle(OpenGl_FrameBuffer)& theFboToCheck,
66 const Handle(OpenGl_FrameBuffer)& theFboRef)
68 return checkWasFailedFbo (theFboToCheck, theFboRef->GetVPSizeX(), theFboRef->GetVPSizeY(), theFboRef->NbSamples());
72 //=======================================================================
73 //function : drawBackground
75 //=======================================================================
76 void OpenGl_View::drawBackground (const Handle(OpenGl_Workspace)& theWorkspace)
78 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
79 const Standard_Boolean wasUsedZBuffer = theWorkspace->SetUseZBuffer (Standard_False);
82 aCtx->core11fwd->glDisable (GL_DEPTH_TEST);
85 if (myBackgroundType == Graphic3d_TOB_CUBEMAP)
87 Graphic3d_Camera aCamera (theWorkspace->View()->Camera());
88 aCamera.SetZRange (0.01, 1.0); // is needed to avoid perspective camera exception
89 aCamera.SetProjectionType (Graphic3d_Camera::Projection_Perspective);
91 aCtx->ProjectionState.Push();
92 aCtx->ProjectionState.SetCurrent (aCamera.ProjectionMatrixF());
94 myCubeMapParams->Aspect()->ShaderProgram()->PushVariableInt ("uZCoeff", myBackgroundCubeMap->ZIsInverted() ? -1 : 1);
95 myCubeMapParams->Aspect()->ShaderProgram()->PushVariableInt ("uYCoeff", myBackgroundCubeMap->IsTopDown() ? 1 : -1);
96 const OpenGl_Aspects* anOldAspectFace = theWorkspace->SetAspects (myCubeMapParams);
98 myBackgrounds[Graphic3d_TOB_CUBEMAP]->Render (theWorkspace);
100 aCtx->ProjectionState.Pop();
101 aCtx->ApplyProjectionMatrix();
102 theWorkspace->SetAspects (anOldAspectFace);
104 else if (myBackgroundType == Graphic3d_TOB_GRADIENT
105 || myBackgroundType == Graphic3d_TOB_TEXTURE)
107 // Drawing background gradient if:
108 // - gradient fill type is not Aspect_GFM_NONE and
109 // - either background texture is no specified or it is drawn in Aspect_FM_CENTERED mode
110 if (myBackgrounds[Graphic3d_TOB_GRADIENT]->IsDefined()
111 && (!myTextureParams->Aspect()->ToMapTexture()
112 || myBackgrounds[Graphic3d_TOB_TEXTURE]->TextureFillMethod() == Aspect_FM_CENTERED
113 || myBackgrounds[Graphic3d_TOB_TEXTURE]->TextureFillMethod() == Aspect_FM_NONE))
115 #if !defined(GL_ES_VERSION_2_0)
116 GLint aShadingModelOld = GL_SMOOTH;
117 if (aCtx->core11 != NULL
118 && aCtx->caps->ffpEnable)
120 aCtx->core11fwd->glDisable (GL_LIGHTING);
121 aCtx->core11fwd->glGetIntegerv (GL_SHADE_MODEL, &aShadingModelOld);
122 aCtx->core11->glShadeModel (GL_SMOOTH);
126 myBackgrounds[Graphic3d_TOB_GRADIENT]->Render(theWorkspace);
128 #if !defined(GL_ES_VERSION_2_0)
129 if (aCtx->core11 != NULL
130 && aCtx->caps->ffpEnable)
132 aCtx->core11->glShadeModel (aShadingModelOld);
137 // Drawing background image if it is defined
138 // (texture is defined and fill type is not Aspect_FM_NONE)
139 if (myBackgrounds[Graphic3d_TOB_TEXTURE]->IsDefined()
140 && myTextureParams->Aspect()->ToMapTexture())
142 aCtx->core11fwd->glDisable (GL_BLEND);
144 const OpenGl_Aspects* anOldAspectFace = theWorkspace->SetAspects (myTextureParams);
145 myBackgrounds[Graphic3d_TOB_TEXTURE]->Render (theWorkspace);
146 theWorkspace->SetAspects (anOldAspectFace);
152 theWorkspace->SetUseZBuffer (Standard_True);
153 aCtx->core11fwd->glEnable (GL_DEPTH_TEST);
157 //=======================================================================
160 //=======================================================================
161 void OpenGl_View::Redraw()
163 const Standard_Boolean wasDisabledMSAA = myToDisableMSAA;
164 const Standard_Boolean hadFboBlit = myHasFboBlit;
165 if (myRenderParams.Method == Graphic3d_RM_RAYTRACING
166 && !myCaps->vboDisable
167 && !myCaps->keepArrayData)
169 // caps are shared across all views, thus we need to invalidate all of them
170 // if (myWasRedrawnGL) { myStructureManager->SetDeviceLost(); }
171 myDriver->setDeviceLost();
172 myCaps->keepArrayData = Standard_True;
175 if (!myWorkspace->Activate())
180 myWindow->SetSwapInterval();
183 const Graphic3d_StereoMode aStereoMode = myRenderParams.StereoMode;
184 Graphic3d_Camera::Projection aProjectType = myCamera->ProjectionType();
185 const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext();
186 aCtx->FrameStats()->FrameStart (myWorkspace->View(), false);
187 aCtx->SetLineFeather (myRenderParams.LineFeather);
189 const Standard_Integer anSRgbState = aCtx->ToRenderSRGB() ? 1 : 0;
190 if (mySRgbState != -1
191 && mySRgbState != anSRgbState)
193 releaseSrgbResources (aCtx);
194 initTextureEnv (aCtx);
196 mySRgbState = anSRgbState;
197 aCtx->ShaderManager()->UpdateSRgbState();
199 // release pending GL resources
200 aCtx->ReleaseDelayed();
202 // fetch OpenGl context state
205 OpenGl_FrameBuffer* aFrameBuffer = myFBO.get();
206 bool toSwap = aCtx->IsRender()
207 && !aCtx->caps->buffersNoSwap
208 && aFrameBuffer == NULL;
210 const Standard_Integer aSizeX = aFrameBuffer != NULL ? aFrameBuffer->GetVPSizeX() : myWindow->Width();
211 const Standard_Integer aSizeY = aFrameBuffer != NULL ? aFrameBuffer->GetVPSizeY() : myWindow->Height();
212 const Standard_Integer aRendSizeX = Standard_Integer(myRenderParams.RenderResolutionScale * aSizeX + 0.5f);
213 const Standard_Integer aRendSizeY = Standard_Integer(myRenderParams.RenderResolutionScale * aSizeY + 0.5f);
219 myBackBufferRestored = Standard_False;
220 myIsImmediateDrawn = Standard_False;
224 // determine multisampling parameters
225 Standard_Integer aNbSamples = !myToDisableMSAA && aSizeX == aRendSizeX
226 ? Max (Min (myRenderParams.NbMsaaSamples, aCtx->MaxMsaaSamples()), 0)
230 aNbSamples = OpenGl_Context::GetPowerOfTwo (aNbSamples, aCtx->MaxMsaaSamples());
233 bool toUseOit = myRenderParams.TransparencyMethod == Graphic3d_RTM_BLEND_OIT
234 && checkOitCompatibility (aCtx, aNbSamples > 0);
236 const bool toInitImmediateFbo = myTransientDrawToFront
237 && (!aCtx->caps->useSystemBuffer || (toUseOit && HasImmediateStructures()));
239 if ( aFrameBuffer == NULL
240 && !aCtx->DefaultFrameBuffer().IsNull()
241 && aCtx->DefaultFrameBuffer()->IsValid())
243 aFrameBuffer = aCtx->DefaultFrameBuffer().operator->();
247 && (myTransientDrawToFront
248 || aProjectType == Graphic3d_Camera::Projection_Stereo
251 || aSizeX != aRendSizeX))
253 if (myMainSceneFbos[0]->GetVPSizeX() != aRendSizeX
254 || myMainSceneFbos[0]->GetVPSizeY() != aRendSizeY
255 || myMainSceneFbos[0]->NbSamples() != aNbSamples)
257 if (!myTransientDrawToFront)
259 myImmediateSceneFbos[0]->Release (aCtx.operator->());
260 myImmediateSceneFbos[1]->Release (aCtx.operator->());
261 myImmediateSceneFbos[0]->ChangeViewport (0, 0);
262 myImmediateSceneFbos[1]->ChangeViewport (0, 0);
265 // prepare FBOs containing main scene
266 // for further blitting and rendering immediate presentations on top
267 if (aCtx->core20fwd != NULL)
269 const bool wasFailedMain0 = checkWasFailedFbo (myMainSceneFbos[0], aRendSizeX, aRendSizeY, aNbSamples);
270 if (!myMainSceneFbos[0]->Init (aCtx, aRendSizeX, aRendSizeY, myFboColorFormat, myFboDepthFormat, aNbSamples)
273 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "Error! Main FBO "
274 + printFboFormat (myMainSceneFbos[0]) + " initialization has failed";
275 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
279 if (myMainSceneFbos[0]->IsValid() && (toInitImmediateFbo || myImmediateSceneFbos[0]->IsValid()))
281 const bool wasFailedImm0 = checkWasFailedFbo (myImmediateSceneFbos[0], myMainSceneFbos[0]);
282 if (!myImmediateSceneFbos[0]->InitLazy (aCtx, *myMainSceneFbos[0])
285 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "Error! Immediate FBO "
286 + printFboFormat (myImmediateSceneFbos[0]) + " initialization has failed";
287 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
293 myMainSceneFbos [0]->Release (aCtx.operator->());
294 myMainSceneFbos [1]->Release (aCtx.operator->());
295 myImmediateSceneFbos[0]->Release (aCtx.operator->());
296 myImmediateSceneFbos[1]->Release (aCtx.operator->());
297 myMainSceneFbos [0]->ChangeViewport (0, 0);
298 myMainSceneFbos [1]->ChangeViewport (0, 0);
299 myImmediateSceneFbos[0]->ChangeViewport (0, 0);
300 myImmediateSceneFbos[1]->ChangeViewport (0, 0);
303 if (aProjectType == Graphic3d_Camera::Projection_Stereo
304 && myMainSceneFbos[0]->IsValid())
306 const bool wasFailedMain1 = checkWasFailedFbo (myMainSceneFbos[1], myMainSceneFbos[0]);
307 if (!myMainSceneFbos[1]->InitLazy (aCtx, *myMainSceneFbos[0])
310 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "Error! Main FBO (second) "
311 + printFboFormat (myMainSceneFbos[1]) + " initialization has failed";
312 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
314 if (!myMainSceneFbos[1]->IsValid())
317 aProjectType = Graphic3d_Camera::Projection_Perspective;
319 else if (!myTransientDrawToFront)
323 else if (!aCtx->HasStereoBuffers() || aStereoMode != Graphic3d_StereoMode_QuadBuffer)
325 const bool wasFailedImm0 = checkWasFailedFbo (myImmediateSceneFbos[0], myMainSceneFbos[0]);
326 const bool wasFailedImm1 = checkWasFailedFbo (myImmediateSceneFbos[1], myMainSceneFbos[0]);
327 if (!myImmediateSceneFbos[0]->InitLazy (aCtx, *myMainSceneFbos[0])
330 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "Error! Immediate FBO (first) "
331 + printFboFormat (myImmediateSceneFbos[0]) + " initialization has failed";
332 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
334 if (!myImmediateSceneFbos[1]->InitLazy (aCtx, *myMainSceneFbos[0])
337 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "Error! Immediate FBO (first) "
338 + printFboFormat (myImmediateSceneFbos[1]) + " initialization has failed";
339 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
341 if (!myImmediateSceneFbos[0]->IsValid()
342 || !myImmediateSceneFbos[1]->IsValid())
344 aProjectType = Graphic3d_Camera::Projection_Perspective;
349 // process PBR environment
350 if (myShadingModel == Graphic3d_TOSM_PBR
351 || myShadingModel == Graphic3d_TOSM_PBR_FACET)
353 if (!myPBREnvironment.IsNull()
354 && myPBREnvironment->SizesAreDifferent (myRenderParams.PbrEnvPow2Size,
355 myRenderParams.PbrEnvSpecMapNbLevels))
357 myPBREnvironment->Release (aCtx.get());
358 myPBREnvironment.Nullify();
359 myPBREnvState = OpenGl_PBREnvState_NONEXISTENT;
360 myPBREnvRequest = OpenGl_PBREnvRequest_BAKE;
364 if (myPBREnvState == OpenGl_PBREnvState_NONEXISTENT
367 myPBREnvironment = OpenGl_PBREnvironment::Create (aCtx, myRenderParams.PbrEnvPow2Size, myRenderParams.PbrEnvSpecMapNbLevels);
368 myPBREnvState = myPBREnvironment.IsNull() ? OpenGl_PBREnvState_UNAVAILABLE : OpenGl_PBREnvState_CREATED;
369 if (myPBREnvState == OpenGl_PBREnvState_CREATED)
371 Handle(OpenGl_Texture) anEnvLUT;
372 static const TCollection_AsciiString THE_SHARED_ENV_LUT_KEY("EnvLUT");
373 if (!aCtx->GetResource (THE_SHARED_ENV_LUT_KEY, anEnvLUT))
375 Handle(Graphic3d_TextureParams) aParams = new Graphic3d_TextureParams();
376 aParams->SetFilter (Graphic3d_TOTF_BILINEAR);
377 aParams->SetRepeat (Standard_False);
378 aParams->SetTextureUnit (aCtx->PBREnvLUTTexUnit());
379 anEnvLUT = new OpenGl_Texture(THE_SHARED_ENV_LUT_KEY, aParams);
380 Handle(Image_PixMap) aPixMap = new Image_PixMap();
381 aPixMap->InitWrapper (Image_Format_RGF, (Standard_Byte*)Textures_EnvLUT, Textures_EnvLUTSize, Textures_EnvLUTSize);
382 OpenGl_TextureFormat aTexFormat = OpenGl_TextureFormat::FindFormat (aCtx, aPixMap->Format(), false);
383 #if defined(GL_ES_VERSION_2_0)
384 // GL_RG32F is not texture-filterable format on OpenGL ES without OES_texture_float_linear extension.
385 // GL_RG16F is texture-filterable since OpenGL ES 3.0 and can be initialized from 32-bit floats.
386 // Note that it is expected that GL_RG16F has enough precision for this table, so that it can be used also on desktop OpenGL.
387 //if (!aCtx->hasTexFloatLinear)
388 aTexFormat.SetInternalFormat (GL_RG16F);
390 if (!aTexFormat.IsValid()
391 || !anEnvLUT->Init (aCtx, aTexFormat, Graphic3d_Vec2i((Standard_Integer)Textures_EnvLUTSize), Graphic3d_TOT_2D, aPixMap.get()))
393 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, "Failed allocation of LUT for PBR");
396 aCtx->ShareResource (THE_SHARED_ENV_LUT_KEY, anEnvLUT);
398 if (!anEnvLUT.IsNull())
400 anEnvLUT->Bind (aCtx);
402 myWorkspace->ApplyAspects();
405 processPBREnvRequest (aCtx);
408 // create color and coverage accumulation buffers required for OIT algorithm
411 Standard_Integer anFboIt = 0;
412 for (; anFboIt < 2; ++anFboIt)
414 Handle(OpenGl_FrameBuffer)& aMainSceneFbo = myMainSceneFbos [anFboIt];
415 Handle(OpenGl_FrameBuffer)& aMainSceneFboOit = myMainSceneFbosOit [anFboIt];
416 Handle(OpenGl_FrameBuffer)& anImmediateSceneFbo = myImmediateSceneFbos [anFboIt];
417 Handle(OpenGl_FrameBuffer)& anImmediateSceneFboOit = myImmediateSceneFbosOit[anFboIt];
418 if (aMainSceneFbo->IsValid()
419 && (aMainSceneFboOit->GetVPSizeX() != aRendSizeX
420 || aMainSceneFboOit->GetVPSizeY() != aRendSizeY
421 || aMainSceneFboOit->NbSamples() != aNbSamples))
423 Standard_Integer aColorConfig = 0;
424 for (;;) // seemly responding to driver limitation (GL_FRAMEBUFFER_UNSUPPORTED)
426 if (myFboOitColorConfig.IsEmpty())
428 if (!chooseOitColorConfiguration (aCtx, aColorConfig++, myFboOitColorConfig))
433 if (aMainSceneFboOit->Init (aCtx, aRendSizeX, aRendSizeY, myFboOitColorConfig, aMainSceneFbo->DepthStencilTexture(), aNbSamples))
437 myFboOitColorConfig.Clear();
439 if (!aMainSceneFboOit->IsValid())
444 else if (!aMainSceneFbo->IsValid())
446 aMainSceneFboOit->Release (aCtx.operator->());
447 aMainSceneFboOit->ChangeViewport (0, 0);
450 if (anImmediateSceneFbo->IsValid()
451 && (anImmediateSceneFboOit->GetVPSizeX() != aRendSizeX
452 || anImmediateSceneFboOit->GetVPSizeY() != aRendSizeY
453 || anImmediateSceneFboOit->NbSamples() != aNbSamples))
455 if (!anImmediateSceneFboOit->Init (aCtx, aRendSizeX, aRendSizeY, myFboOitColorConfig,
456 anImmediateSceneFbo->DepthStencilTexture(), aNbSamples))
461 else if (!anImmediateSceneFbo->IsValid())
463 anImmediateSceneFboOit->Release (aCtx.operator->());
464 anImmediateSceneFboOit->ChangeViewport (0, 0);
467 if (anFboIt == 0) // only the first OIT framebuffer is mandatory
469 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH,
470 "Initialization of float texture framebuffer for use with\n"
471 " blended order-independent transparency rendering algorithm has failed.\n"
472 " Blended order-independent transparency will not be available.\n");
475 myToDisableOITMSAA = Standard_True;
479 myToDisableOIT = Standard_True;
484 if (!toUseOit && myMainSceneFbosOit[0]->IsValid())
486 myMainSceneFbosOit [0]->Release (aCtx.operator->());
487 myMainSceneFbosOit [1]->Release (aCtx.operator->());
488 myImmediateSceneFbosOit[0]->Release (aCtx.operator->());
489 myImmediateSceneFbosOit[1]->Release (aCtx.operator->());
490 myMainSceneFbosOit [0]->ChangeViewport (0, 0);
491 myMainSceneFbosOit [1]->ChangeViewport (0, 0);
492 myImmediateSceneFbosOit[0]->ChangeViewport (0, 0);
493 myImmediateSceneFbosOit[1]->ChangeViewport (0, 0);
496 if (aProjectType == Graphic3d_Camera::Projection_Stereo)
498 OpenGl_FrameBuffer* aMainFbos[2] =
500 myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL,
501 myMainSceneFbos[1]->IsValid() ? myMainSceneFbos[1].operator->() : NULL
503 OpenGl_FrameBuffer* aMainFbosOit[2] =
505 myMainSceneFbosOit[0]->IsValid() ? myMainSceneFbosOit[0].operator->() : NULL,
506 myMainSceneFbosOit[1]->IsValid() ? myMainSceneFbosOit[1].operator->() :
507 myMainSceneFbosOit[0]->IsValid() ? myMainSceneFbosOit[0].operator->() : NULL
510 OpenGl_FrameBuffer* anImmFbos[2] =
512 myImmediateSceneFbos[0]->IsValid() ? myImmediateSceneFbos[0].operator->() : NULL,
513 myImmediateSceneFbos[1]->IsValid() ? myImmediateSceneFbos[1].operator->() : NULL
515 OpenGl_FrameBuffer* anImmFbosOit[2] =
517 myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL,
518 myImmediateSceneFbosOit[1]->IsValid() ? myImmediateSceneFbosOit[1].operator->() :
519 myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL
522 if (!myTransientDrawToFront)
524 anImmFbos [0] = aMainFbos [0];
525 anImmFbos [1] = aMainFbos [1];
526 anImmFbosOit[0] = aMainFbosOit[0];
527 anImmFbosOit[1] = aMainFbosOit[1];
529 else if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip
530 || aStereoMode == Graphic3d_StereoMode_QuadBuffer)
532 anImmFbos [0] = NULL;
533 anImmFbos [1] = NULL;
534 anImmFbosOit[0] = NULL;
535 anImmFbosOit[1] = NULL;
538 #if !defined(GL_ES_VERSION_2_0)
539 aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_LEFT : GL_BACK);
541 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
542 aMainFbos[0] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
544 redraw (Graphic3d_Camera::Projection_MonoLeftEye, aMainFbos[0], aMainFbosOit[0]);
545 myBackBufferRestored = Standard_True;
546 myIsImmediateDrawn = Standard_False;
547 #if !defined(GL_ES_VERSION_2_0)
548 aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_LEFT : GL_BACK);
550 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
551 anImmFbos[0] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
552 if (!redrawImmediate (Graphic3d_Camera::Projection_MonoLeftEye, aMainFbos[0], anImmFbos[0], anImmFbosOit[0]))
556 else if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip && toSwap)
561 #if !defined(GL_ES_VERSION_2_0)
562 aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_RIGHT : GL_BACK);
564 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
565 aMainFbos[1] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
567 redraw (Graphic3d_Camera::Projection_MonoRightEye, aMainFbos[1], aMainFbosOit[1]);
568 myBackBufferRestored = Standard_True;
569 myIsImmediateDrawn = Standard_False;
570 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
571 anImmFbos[1] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
572 if (!redrawImmediate (Graphic3d_Camera::Projection_MonoRightEye, aMainFbos[1], anImmFbos[1], anImmFbosOit[1]))
577 if (anImmFbos[0] != NULL)
579 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(), 1.0f);
580 drawStereoPair (aFrameBuffer);
585 OpenGl_FrameBuffer* aMainFbo = myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : aFrameBuffer;
586 OpenGl_FrameBuffer* aMainFboOit = myMainSceneFbosOit[0]->IsValid() ? myMainSceneFbosOit[0].operator->() : NULL;
587 OpenGl_FrameBuffer* anImmFbo = aFrameBuffer;
588 OpenGl_FrameBuffer* anImmFboOit = NULL;
589 if (!myTransientDrawToFront)
592 anImmFboOit = aMainFboOit;
594 else if (myImmediateSceneFbos[0]->IsValid())
596 anImmFbo = myImmediateSceneFbos[0].operator->();
597 anImmFboOit = myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL;
600 #if !defined(GL_ES_VERSION_2_0)
601 if (aMainFbo == NULL)
603 aCtx->SetReadDrawBuffer (GL_BACK);
606 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
607 aMainFbo != aFrameBuffer ? myRenderParams.RenderResolutionScale : 1.0f);
609 redraw (aProjectType, aMainFbo, aMainFboOit);
610 myBackBufferRestored = Standard_True;
611 myIsImmediateDrawn = Standard_False;
612 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
613 anImmFbo != aFrameBuffer ? myRenderParams.RenderResolutionScale : 1.0f);
614 if (!redrawImmediate (aProjectType, aMainFbo, anImmFbo, anImmFboOit))
620 && anImmFbo != aFrameBuffer)
622 blitBuffers (anImmFbo, aFrameBuffer, myToFlipOutput);
626 if (myRenderParams.Method == Graphic3d_RM_RAYTRACING
627 && myRenderParams.IsGlobalIlluminationEnabled)
635 if (wasDisabledMSAA != myToDisableMSAA
636 || hadFboBlit != myHasFboBlit)
642 // reset state for safety
643 aCtx->BindProgram (Handle(OpenGl_ShaderProgram)());
644 if (aCtx->caps->ffpEnable)
646 aCtx->ShaderManager()->PushState (Handle(OpenGl_ShaderProgram)());
653 if (!myMainSceneFbos[0]->IsValid())
655 myBackBufferRestored = Standard_False;
660 aCtx->core11fwd->glFlush();
663 // reset render mode state
665 aCtx->FrameStats()->FrameEnd (myWorkspace->View(), false);
667 myWasRedrawnGL = Standard_True;
670 // =======================================================================
671 // function : RedrawImmediate
673 // =======================================================================
674 void OpenGl_View::RedrawImmediate()
676 if (!myWorkspace->Activate())
679 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
680 if (!myTransientDrawToFront
681 || !myBackBufferRestored
682 || (aCtx->caps->buffersNoSwap && !myMainSceneFbos[0]->IsValid()))
688 const Graphic3d_StereoMode aStereoMode = myRenderParams.StereoMode;
689 Graphic3d_Camera::Projection aProjectType = myCamera->ProjectionType();
690 OpenGl_FrameBuffer* aFrameBuffer = myFBO.get();
691 aCtx->FrameStats()->FrameStart (myWorkspace->View(), true);
693 if ( aFrameBuffer == NULL
694 && !aCtx->DefaultFrameBuffer().IsNull()
695 && aCtx->DefaultFrameBuffer()->IsValid())
697 aFrameBuffer = aCtx->DefaultFrameBuffer().operator->();
700 if (aProjectType == Graphic3d_Camera::Projection_Stereo)
702 if (myMainSceneFbos[0]->IsValid()
703 && !myMainSceneFbos[1]->IsValid())
705 aProjectType = Graphic3d_Camera::Projection_Perspective;
710 if (aProjectType == Graphic3d_Camera::Projection_Stereo)
712 OpenGl_FrameBuffer* aMainFbos[2] =
714 myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL,
715 myMainSceneFbos[1]->IsValid() ? myMainSceneFbos[1].operator->() : NULL
717 OpenGl_FrameBuffer* anImmFbos[2] =
719 myImmediateSceneFbos[0]->IsValid() ? myImmediateSceneFbos[0].operator->() : NULL,
720 myImmediateSceneFbos[1]->IsValid() ? myImmediateSceneFbos[1].operator->() : NULL
722 OpenGl_FrameBuffer* anImmFbosOit[2] =
724 myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL,
725 myImmediateSceneFbosOit[1]->IsValid() ? myImmediateSceneFbosOit[1].operator->() :
726 myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL
728 if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip
729 || aStereoMode == Graphic3d_StereoMode_QuadBuffer)
733 anImmFbosOit[0] = NULL;
734 anImmFbosOit[1] = NULL;
737 if (aCtx->arbFBO != NULL)
739 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
741 #if !defined(GL_ES_VERSION_2_0)
742 if (anImmFbos[0] == NULL)
744 aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_LEFT : GL_BACK);
748 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
749 anImmFbos[0] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
750 toSwap = redrawImmediate (Graphic3d_Camera::Projection_MonoLeftEye,
754 Standard_True) || toSwap;
755 if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip
757 && !aCtx->caps->buffersNoSwap)
762 if (aCtx->arbFBO != NULL)
764 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
766 #if !defined(GL_ES_VERSION_2_0)
767 if (anImmFbos[1] == NULL)
769 aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_RIGHT : GL_BACK);
772 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
773 anImmFbos[1] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
774 toSwap = redrawImmediate (Graphic3d_Camera::Projection_MonoRightEye,
778 Standard_True) || toSwap;
779 if (anImmFbos[0] != NULL)
781 drawStereoPair (aFrameBuffer);
786 OpenGl_FrameBuffer* aMainFbo = myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL;
787 OpenGl_FrameBuffer* anImmFbo = aFrameBuffer;
788 OpenGl_FrameBuffer* anImmFboOit = NULL;
789 if (myImmediateSceneFbos[0]->IsValid())
791 anImmFbo = myImmediateSceneFbos[0].operator->();
792 anImmFboOit = myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL;
794 #if !defined(GL_ES_VERSION_2_0)
795 if (aMainFbo == NULL)
797 aCtx->SetReadDrawBuffer (GL_BACK);
800 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
801 anImmFbo != aFrameBuffer ? myRenderParams.RenderResolutionScale : 1.0f);
802 toSwap = redrawImmediate (aProjectType,
806 Standard_True) || toSwap;
808 && anImmFbo != aFrameBuffer)
810 blitBuffers (anImmFbo, aFrameBuffer, myToFlipOutput);
817 // reset state for safety
818 aCtx->BindProgram (Handle(OpenGl_ShaderProgram)());
819 if (aCtx->caps->ffpEnable)
821 aCtx->ShaderManager()->PushState (Handle(OpenGl_ShaderProgram)());
824 if (toSwap && !aCtx->caps->buffersNoSwap)
830 aCtx->core11fwd->glFlush();
832 aCtx->FrameStats()->FrameEnd (myWorkspace->View(), true);
834 myWasRedrawnGL = Standard_True;
837 // =======================================================================
840 // =======================================================================
841 void OpenGl_View::redraw (const Graphic3d_Camera::Projection theProjection,
842 OpenGl_FrameBuffer* theReadDrawFbo,
843 OpenGl_FrameBuffer* theOitAccumFbo)
845 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
846 if (theReadDrawFbo != NULL)
848 theReadDrawFbo->BindBuffer (aCtx);
849 theReadDrawFbo->SetupViewport (aCtx);
853 const Standard_Integer aViewport[4] = { 0, 0, myWindow->Width(), myWindow->Height() };
854 aCtx->ResizeViewport (aViewport);
857 // request reset of material
858 aCtx->ShaderManager()->UpdateMaterialState();
860 myWorkspace->UseZBuffer() = Standard_True;
861 myWorkspace->UseDepthWrite() = Standard_True;
862 GLbitfield toClear = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT;
863 glDepthFunc (GL_LEQUAL);
864 glDepthMask (GL_TRUE);
865 glEnable (GL_DEPTH_TEST);
867 #if !defined(GL_ES_VERSION_2_0)
870 glClearDepthf (1.0f);
873 const OpenGl_Vec4 aBgColor = aCtx->Vec4FromQuantityColor (myBgColor);
874 glClearColor (aBgColor.r(), aBgColor.g(), aBgColor.b(), 0.0f);
878 render (theProjection, theReadDrawFbo, theOitAccumFbo, Standard_False);
881 // =======================================================================
882 // function : redrawMonoImmediate
884 // =======================================================================
885 bool OpenGl_View::redrawImmediate (const Graphic3d_Camera::Projection theProjection,
886 OpenGl_FrameBuffer* theReadFbo,
887 OpenGl_FrameBuffer* theDrawFbo,
888 OpenGl_FrameBuffer* theOitAccumFbo,
889 const Standard_Boolean theIsPartialUpdate)
891 const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext();
892 GLboolean toCopyBackToFront = GL_FALSE;
893 if (theDrawFbo == theReadFbo
894 && theDrawFbo != NULL)
896 myBackBufferRestored = Standard_False;
898 else if (theReadFbo != NULL
899 && theReadFbo->IsValid()
902 if (!blitBuffers (theReadFbo, theDrawFbo))
907 else if (theDrawFbo == NULL)
909 #if !defined(GL_ES_VERSION_2_0)
910 aCtx->core11fwd->glGetBooleanv (GL_DOUBLEBUFFER, &toCopyBackToFront);
912 if (toCopyBackToFront
913 && myTransientDrawToFront)
915 if (!HasImmediateStructures()
916 && !theIsPartialUpdate)
918 // prefer Swap Buffers within Redraw in compatibility mode (without FBO)
921 if (!copyBackToFront())
923 toCopyBackToFront = GL_FALSE;
924 myBackBufferRestored = Standard_False;
929 toCopyBackToFront = GL_FALSE;
930 myBackBufferRestored = Standard_False;
935 myBackBufferRestored = Standard_False;
937 myIsImmediateDrawn = Standard_True;
939 myWorkspace->UseZBuffer() = Standard_True;
940 myWorkspace->UseDepthWrite() = Standard_True;
941 glDepthFunc (GL_LEQUAL);
942 glDepthMask (GL_TRUE);
943 glEnable (GL_DEPTH_TEST);
944 #if !defined(GL_ES_VERSION_2_0)
947 glClearDepthf (1.0f);
950 render (theProjection, theDrawFbo, theOitAccumFbo, Standard_True);
952 return !toCopyBackToFront;
955 //=======================================================================
958 //=======================================================================
959 void OpenGl_View::render (Graphic3d_Camera::Projection theProjection,
960 OpenGl_FrameBuffer* theOutputFBO,
961 OpenGl_FrameBuffer* theOitAccumFbo,
962 const Standard_Boolean theToDrawImmediate)
964 // ==================================
965 // Step 1: Prepare for render
966 // ==================================
968 const Handle(OpenGl_Context)& aContext = myWorkspace->GetGlContext();
969 aContext->SetAllowSampleAlphaToCoverage (myRenderParams.ToEnableAlphaToCoverage
970 && theOutputFBO != NULL
971 && theOutputFBO->NbSamples() != 0);
973 #if !defined(GL_ES_VERSION_2_0)
974 // Disable current clipping planes
975 if (aContext->core11 != NULL)
977 const Standard_Integer aMaxPlanes = aContext->MaxClipPlanes();
978 for (Standard_Integer aClipPlaneId = GL_CLIP_PLANE0; aClipPlaneId < GL_CLIP_PLANE0 + aMaxPlanes; ++aClipPlaneId)
980 aContext->core11fwd->glDisable (aClipPlaneId);
985 // update states of OpenGl_BVHTreeSelector (frustum culling algorithm);
986 // note that we pass here window dimensions ignoring Graphic3d_RenderingParams::RenderResolutionScale
987 myBVHSelector.SetViewVolume (myCamera);
988 myBVHSelector.SetViewportSize (myWindow->Width(), myWindow->Height(), myRenderParams.ResolutionRatio());
989 myBVHSelector.CacheClipPtsProjections();
991 const Handle(OpenGl_ShaderManager)& aManager = aContext->ShaderManager();
992 const Handle(Graphic3d_LightSet)& aLights = myShadingModel == Graphic3d_TOSM_UNLIT ? myNoShadingLight : myLights;
993 Standard_Size aLightsRevision = 0;
994 if (!aLights.IsNull())
996 aLightsRevision = aLights->UpdateRevision();
998 if (StateInfo (myCurrLightSourceState, aManager->LightSourceState().Index()) != myLastLightSourceState
999 || aLightsRevision != myLightsRevision)
1001 myLightsRevision = aLightsRevision;
1002 aManager->UpdateLightSourceStateTo (aLights, SpecIBLMapLevels());
1003 myLastLightSourceState = StateInfo (myCurrLightSourceState, aManager->LightSourceState().Index());
1006 // Update matrices if camera has changed.
1007 Graphic3d_WorldViewProjState aWVPState = myCamera->WorldViewProjState();
1008 if (myWorldViewProjState != aWVPState)
1011 myWorldViewProjState = aWVPState;
1014 myLocalOrigin.SetCoord (0.0, 0.0, 0.0);
1015 aContext->ProjectionState.SetCurrent (myCamera->ProjectionMatrixF());
1016 aContext->WorldViewState .SetCurrent (myCamera->OrientationMatrixF());
1017 aContext->ApplyProjectionMatrix();
1018 aContext->ApplyWorldViewMatrix();
1019 if (aManager->ModelWorldState().Index() == 0)
1021 aContext->ShaderManager()->UpdateModelWorldStateTo (OpenGl_Mat4());
1024 // ====================================
1025 // Step 2: Redraw background
1026 // ====================================
1028 // Render background
1029 if (!theToDrawImmediate)
1031 drawBackground (myWorkspace);
1034 #if !defined(GL_ES_VERSION_2_0)
1035 // Switch off lighting by default
1036 if (aContext->core11 != NULL
1037 && aContext->caps->ffpEnable)
1039 glDisable(GL_LIGHTING);
1043 // =================================
1044 // Step 3: Redraw main plane
1045 // =================================
1047 // Setup face culling
1048 GLboolean isCullFace = GL_FALSE;
1049 if (myBackfacing != Graphic3d_TOBM_AUTOMATIC)
1051 isCullFace = glIsEnabled (GL_CULL_FACE);
1052 if (myBackfacing == Graphic3d_TOBM_DISABLE)
1054 glEnable (GL_CULL_FACE);
1055 glCullFace (GL_BACK);
1058 glDisable (GL_CULL_FACE);
1061 #if !defined(GL_ES_VERSION_2_0)
1062 // if the view is scaled normal vectors are scaled to unit
1063 // length for correct displaying of shaded objects
1064 const gp_Pnt anAxialScale = myCamera->AxialScale();
1065 if (anAxialScale.X() != 1.F ||
1066 anAxialScale.Y() != 1.F ||
1067 anAxialScale.Z() != 1.F)
1069 aContext->SetGlNormalizeEnabled (Standard_True);
1073 aContext->SetGlNormalizeEnabled (Standard_False);
1076 // Apply InteriorShadingMethod
1077 if (aContext->core11 != NULL)
1079 aContext->core11->glShadeModel (myShadingModel == Graphic3d_TOSM_FACET
1080 || myShadingModel == Graphic3d_TOSM_UNLIT ? GL_FLAT : GL_SMOOTH);
1084 aManager->SetShadingModel (OpenGl_ShaderManager::PBRShadingModelFallback (myShadingModel, checkPBRAvailability()));
1087 if (theProjection == Graphic3d_Camera::Projection_MonoLeftEye)
1089 aContext->ProjectionState.SetCurrent (myCamera->ProjectionStereoLeftF());
1090 aContext->ApplyProjectionMatrix();
1092 else if (theProjection == Graphic3d_Camera::Projection_MonoRightEye)
1094 aContext->ProjectionState.SetCurrent (myCamera->ProjectionStereoRightF());
1095 aContext->ApplyProjectionMatrix();
1098 myWorkspace->SetEnvironmentTexture (myTextureEnv);
1100 renderScene (theProjection, theOutputFBO, theOitAccumFbo, theToDrawImmediate);
1102 myWorkspace->SetEnvironmentTexture (Handle(OpenGl_TextureSet)());
1104 // ===============================
1105 // Step 4: Trihedron
1106 // ===============================
1108 // Resetting GL parameters according to the default aspects
1109 // in order to synchronize GL state with the graphic driver state
1110 // before drawing auxiliary stuff (trihedrons, overlayer)
1111 myWorkspace->ResetAppliedAspect();
1114 if (!theToDrawImmediate)
1116 renderTrihedron (myWorkspace);
1118 // Restore face culling
1119 if (myBackfacing != Graphic3d_TOBM_AUTOMATIC)
1123 glEnable (GL_CULL_FACE);
1124 glCullFace (GL_BACK);
1127 glDisable (GL_CULL_FACE);
1135 myWorkspace->ResetAppliedAspect();
1136 aContext->SetAllowSampleAlphaToCoverage (false);
1137 aContext->SetSampleAlphaToCoverage (false);
1139 // reset FFP state for safety
1140 aContext->BindProgram (Handle(OpenGl_ShaderProgram)());
1141 if (aContext->caps->ffpEnable)
1143 aContext->ShaderManager()->PushState (Handle(OpenGl_ShaderProgram)());
1146 // ==============================================================
1147 // Step 6: Keep shader manager informed about last View
1148 // ==============================================================
1150 if (!aManager.IsNull())
1152 aManager->SetLastView (this);
1156 // =======================================================================
1157 // function : InvalidateBVHData
1159 // =======================================================================
1160 void OpenGl_View::InvalidateBVHData (const Graphic3d_ZLayerId theLayerId)
1162 myZLayers.InvalidateBVHData (theLayerId);
1165 //=======================================================================
1166 //function : renderStructs
1168 //=======================================================================
1169 void OpenGl_View::renderStructs (Graphic3d_Camera::Projection theProjection,
1170 OpenGl_FrameBuffer* theReadDrawFbo,
1171 OpenGl_FrameBuffer* theOitAccumFbo,
1172 const Standard_Boolean theToDrawImmediate)
1174 myZLayers.UpdateCulling (myWorkspace, theToDrawImmediate);
1175 if ( myZLayers.NbStructures() <= 0 )
1178 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
1179 Standard_Boolean toRenderGL = theToDrawImmediate ||
1180 myRenderParams.Method != Graphic3d_RM_RAYTRACING ||
1181 myRaytraceInitStatus == OpenGl_RT_FAIL ||
1186 const Standard_Integer aSizeX = theReadDrawFbo != NULL ? theReadDrawFbo->GetVPSizeX() : myWindow->Width();
1187 const Standard_Integer aSizeY = theReadDrawFbo != NULL ? theReadDrawFbo->GetVPSizeY() : myWindow->Height();
1189 toRenderGL = !initRaytraceResources (aSizeX, aSizeY, aCtx)
1190 || !updateRaytraceGeometry (OpenGl_GUM_CHECK, myId, aCtx);
1192 toRenderGL |= !myIsRaytraceDataValid; // if no ray-trace data use OpenGL
1196 myOpenGlFBO ->InitLazy (aCtx, aSizeX, aSizeY, myFboColorFormat, myFboDepthFormat, 0);
1198 if (theReadDrawFbo != NULL)
1199 theReadDrawFbo->UnbindBuffer (aCtx);
1201 // Prepare preliminary OpenGL output
1202 if (aCtx->arbFBOBlit != NULL)
1204 // Render bottom OSD layer
1205 myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_Bottom, theReadDrawFbo, theOitAccumFbo);
1207 const Standard_Integer aPrevFilter = myWorkspace->RenderFilter() & ~(Standard_Integer )(OpenGl_RenderFilter_NonRaytraceableOnly);
1208 myWorkspace->SetRenderFilter (aPrevFilter | OpenGl_RenderFilter_NonRaytraceableOnly);
1210 if (theReadDrawFbo != NULL)
1212 theReadDrawFbo->BindDrawBuffer (aCtx);
1216 aCtx->arbFBO->glBindFramebuffer (GL_DRAW_FRAMEBUFFER, 0);
1217 aCtx->SetFrameBufferSRGB (false);
1220 // Render non-polygonal elements in default layer
1221 myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_RayTracable, theReadDrawFbo, theOitAccumFbo);
1223 myWorkspace->SetRenderFilter (aPrevFilter);
1226 if (theReadDrawFbo != NULL)
1228 theReadDrawFbo->BindBuffer (aCtx);
1232 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, 0);
1233 aCtx->SetFrameBufferSRGB (false);
1236 // Reset OpenGl aspects state to default to avoid enabling of
1237 // backface culling which is not supported in ray-tracing.
1238 myWorkspace->ResetAppliedAspect();
1240 // Ray-tracing polygonal primitive arrays
1241 raytrace (aSizeX, aSizeY, theProjection, theReadDrawFbo, aCtx);
1243 // Render upper (top and topmost) OpenGL layers
1244 myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_Upper, theReadDrawFbo, theOitAccumFbo);
1248 // Redraw 3D scene using OpenGL in standard
1249 // mode or in case of ray-tracing failure
1252 myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_All, theReadDrawFbo, theOitAccumFbo);
1254 // Set flag that scene was redrawn by standard pipeline
1255 myWasRedrawnGL = Standard_True;
1259 //=======================================================================
1260 //function : renderTrihedron
1262 //=======================================================================
1263 void OpenGl_View::renderTrihedron (const Handle(OpenGl_Workspace) &theWorkspace)
1265 if (myToShowGradTrihedron)
1267 myGraduatedTrihedron.Render (theWorkspace);
1271 //=======================================================================
1272 //function : renderFrameStats
1274 //=======================================================================
1275 void OpenGl_View::renderFrameStats()
1277 if (myRenderParams.ToShowStats
1278 && myRenderParams.CollectedStats != Graphic3d_RenderingParams::PerfCounters_NONE)
1280 myFrameStatsPrs.Update (myWorkspace);
1281 myFrameStatsPrs.Render (myWorkspace);
1285 // =======================================================================
1286 // function : Invalidate
1288 // =======================================================================
1289 void OpenGl_View::Invalidate()
1291 myBackBufferRestored = Standard_False;
1294 //=======================================================================
1295 //function : renderScene
1297 //=======================================================================
1298 void OpenGl_View::renderScene (Graphic3d_Camera::Projection theProjection,
1299 OpenGl_FrameBuffer* theReadDrawFbo,
1300 OpenGl_FrameBuffer* theOitAccumFbo,
1301 const Standard_Boolean theToDrawImmediate)
1303 const Handle(OpenGl_Context)& aContext = myWorkspace->GetGlContext();
1305 // Specify clipping planes in view transformation space
1306 aContext->ChangeClipping().Reset (myClipPlanes);
1307 if (!myClipPlanes.IsNull()
1308 && !myClipPlanes->IsEmpty())
1310 aContext->ShaderManager()->UpdateClippingState();
1313 renderStructs (theProjection, theReadDrawFbo, theOitAccumFbo, theToDrawImmediate);
1314 aContext->BindTextures (Handle(OpenGl_TextureSet)());
1316 // Apply restored view matrix.
1317 aContext->ApplyWorldViewMatrix();
1319 aContext->ChangeClipping().Reset (Handle(Graphic3d_SequenceOfHClipPlane)());
1320 if (!myClipPlanes.IsNull()
1321 && !myClipPlanes->IsEmpty())
1323 aContext->ShaderManager()->RevertClippingState();
1327 // =======================================================================
1328 // function : bindDefaultFbo
1330 // =======================================================================
1331 void OpenGl_View::bindDefaultFbo (OpenGl_FrameBuffer* theCustomFbo)
1333 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
1334 OpenGl_FrameBuffer* anFbo = (theCustomFbo != NULL && theCustomFbo->IsValid())
1336 : (!aCtx->DefaultFrameBuffer().IsNull()
1337 && aCtx->DefaultFrameBuffer()->IsValid()
1338 ? aCtx->DefaultFrameBuffer().operator->()
1342 anFbo->BindBuffer (aCtx);
1343 anFbo->SetupViewport (aCtx);
1347 #if !defined(GL_ES_VERSION_2_0)
1348 aCtx->SetReadDrawBuffer (GL_BACK);
1350 if (aCtx->arbFBO != NULL)
1352 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
1355 const Standard_Integer aViewport[4] = { 0, 0, myWindow->Width(), myWindow->Height() };
1356 aCtx->ResizeViewport (aViewport);
1360 // =======================================================================
1361 // function : initBlitQuad
1363 // =======================================================================
1364 OpenGl_VertexBuffer* OpenGl_View::initBlitQuad (const Standard_Boolean theToFlip)
1366 OpenGl_VertexBuffer* aVerts = NULL;
1369 aVerts = &myFullScreenQuad;
1370 if (!aVerts->IsValid())
1372 OpenGl_Vec4 aQuad[4] =
1374 OpenGl_Vec4( 1.0f, -1.0f, 1.0f, 0.0f),
1375 OpenGl_Vec4( 1.0f, 1.0f, 1.0f, 1.0f),
1376 OpenGl_Vec4(-1.0f, -1.0f, 0.0f, 0.0f),
1377 OpenGl_Vec4(-1.0f, 1.0f, 0.0f, 1.0f)
1379 aVerts->Init (myWorkspace->GetGlContext(), 4, 4, aQuad[0].GetData());
1384 aVerts = &myFullScreenQuadFlip;
1385 if (!aVerts->IsValid())
1387 OpenGl_Vec4 aQuad[4] =
1389 OpenGl_Vec4( 1.0f, -1.0f, 1.0f, 1.0f),
1390 OpenGl_Vec4( 1.0f, 1.0f, 1.0f, 0.0f),
1391 OpenGl_Vec4(-1.0f, -1.0f, 0.0f, 1.0f),
1392 OpenGl_Vec4(-1.0f, 1.0f, 0.0f, 0.0f)
1394 aVerts->Init (myWorkspace->GetGlContext(), 4, 4, aQuad[0].GetData());
1400 // =======================================================================
1401 // function : blitBuffers
1403 // =======================================================================
1404 bool OpenGl_View::blitBuffers (OpenGl_FrameBuffer* theReadFbo,
1405 OpenGl_FrameBuffer* theDrawFbo,
1406 const Standard_Boolean theToFlip)
1408 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
1409 const Standard_Integer aReadSizeX = theReadFbo != NULL ? theReadFbo->GetVPSizeX() : myWindow->Width();
1410 const Standard_Integer aReadSizeY = theReadFbo != NULL ? theReadFbo->GetVPSizeY() : myWindow->Height();
1411 const Standard_Integer aDrawSizeX = theDrawFbo != NULL ? theDrawFbo->GetVPSizeX() : myWindow->Width();
1412 const Standard_Integer aDrawSizeY = theDrawFbo != NULL ? theDrawFbo->GetVPSizeY() : myWindow->Height();
1413 if (theReadFbo == NULL || aCtx->IsFeedback())
1417 else if (theReadFbo == theDrawFbo)
1422 // clear destination before blitting
1423 if (theDrawFbo != NULL
1424 && theDrawFbo->IsValid())
1426 theDrawFbo->BindBuffer (aCtx);
1430 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
1431 aCtx->SetFrameBufferSRGB (false);
1433 const Standard_Integer aViewport[4] = { 0, 0, aDrawSizeX, aDrawSizeY };
1434 aCtx->ResizeViewport (aViewport);
1436 #if !defined(GL_ES_VERSION_2_0)
1437 aCtx->core20fwd->glClearDepth (1.0);
1439 aCtx->core20fwd->glClearDepthf (1.0f);
1441 aCtx->core20fwd->glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
1443 const bool toApplyGamma = aCtx->ToRenderSRGB() != aCtx->IsFrameBufferSRGB();
1444 if (aCtx->arbFBOBlit != NULL
1446 && theReadFbo->NbSamples() != 0)
1448 GLbitfield aCopyMask = 0;
1449 theReadFbo->BindReadBuffer (aCtx);
1450 if (theDrawFbo != NULL
1451 && theDrawFbo->IsValid())
1453 theDrawFbo->BindDrawBuffer (aCtx);
1454 if (theDrawFbo->HasColor()
1455 && theReadFbo->HasColor())
1457 aCopyMask |= GL_COLOR_BUFFER_BIT;
1459 if (theDrawFbo->HasDepth()
1460 && theReadFbo->HasDepth())
1462 aCopyMask |= GL_DEPTH_BUFFER_BIT;
1467 if (theReadFbo->HasColor())
1469 aCopyMask |= GL_COLOR_BUFFER_BIT;
1471 if (theReadFbo->HasDepth())
1473 aCopyMask |= GL_DEPTH_BUFFER_BIT;
1475 aCtx->arbFBO->glBindFramebuffer (GL_DRAW_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
1476 aCtx->SetFrameBufferSRGB (false);
1479 // we don't copy stencil buffer here... does it matter for performance?
1480 aCtx->arbFBOBlit->glBlitFramebuffer (0, 0, aReadSizeX, aReadSizeY,
1481 0, 0, aDrawSizeX, aDrawSizeY,
1482 aCopyMask, GL_NEAREST);
1483 const int anErr = ::glGetError();
1484 if (anErr != GL_NO_ERROR)
1486 // glBlitFramebuffer() might fail in several cases:
1487 // - Both FBOs have MSAA and they are samples number does not match.
1488 // OCCT checks that this does not happen,
1489 // however some graphics drivers provide an option for overriding MSAA.
1490 // In this case window MSAA might be non-zero (and application can not check it)
1491 // and might not match MSAA of our offscreen FBOs.
1492 // - Pixel formats of FBOs do not match.
1493 // This also might happen with window has pixel format,
1494 // e.g. Mesa fails blitting RGBA8 -> RGB8 while other drivers support this conversion.
1495 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "FBO blitting has failed [Error #" + anErr + "]\n"
1496 + " Please check your graphics driver settings or try updating driver.";
1497 if (theReadFbo->NbSamples() != 0)
1499 myToDisableMSAA = true;
1500 aMsg += "\n MSAA settings should not be overridden by driver!";
1502 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1503 GL_DEBUG_TYPE_ERROR,
1505 GL_DEBUG_SEVERITY_HIGH,
1509 if (theDrawFbo != NULL
1510 && theDrawFbo->IsValid())
1512 theDrawFbo->BindBuffer (aCtx);
1516 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
1517 aCtx->SetFrameBufferSRGB (false);
1522 aCtx->core20fwd->glDepthFunc (GL_ALWAYS);
1523 aCtx->core20fwd->glDepthMask (GL_TRUE);
1524 aCtx->core20fwd->glEnable (GL_DEPTH_TEST);
1525 #if defined(GL_ES_VERSION_2_0)
1526 if (!aCtx->IsGlGreaterEqual (3, 0)
1527 && !aCtx->extFragDepth)
1529 aCtx->core20fwd->glDisable (GL_DEPTH_TEST);
1533 aCtx->BindTextures (Handle(OpenGl_TextureSet)());
1535 const Graphic3d_TypeOfTextureFilter aFilter = (aDrawSizeX == aReadSizeX && aDrawSizeY == aReadSizeY) ? Graphic3d_TOTF_NEAREST : Graphic3d_TOTF_BILINEAR;
1536 const GLint aFilterGl = aFilter == Graphic3d_TOTF_NEAREST ? GL_NEAREST : GL_LINEAR;
1538 OpenGl_VertexBuffer* aVerts = initBlitQuad (theToFlip);
1539 const Handle(OpenGl_ShaderManager)& aManager = aCtx->ShaderManager();
1540 if (aVerts->IsValid()
1541 && aManager->BindFboBlitProgram (theReadFbo != NULL ? theReadFbo->NbSamples() : 0, toApplyGamma))
1543 aCtx->SetSampleAlphaToCoverage (false);
1544 theReadFbo->ColorTexture()->Bind (aCtx, Graphic3d_TextureUnit_0);
1545 if (theReadFbo->ColorTexture()->Sampler()->Parameters()->Filter() != aFilter)
1547 theReadFbo->ColorTexture()->Sampler()->Parameters()->SetFilter (aFilter);
1548 aCtx->core20fwd->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, aFilterGl);
1549 aCtx->core20fwd->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, aFilterGl);
1552 theReadFbo->DepthStencilTexture()->Bind (aCtx, Graphic3d_TextureUnit_1);
1553 if (theReadFbo->DepthStencilTexture()->Sampler()->Parameters()->Filter() != aFilter)
1555 theReadFbo->DepthStencilTexture()->Sampler()->Parameters()->SetFilter (aFilter);
1556 aCtx->core20fwd->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, aFilterGl);
1557 aCtx->core20fwd->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, aFilterGl);
1560 aVerts->BindVertexAttrib (aCtx, Graphic3d_TOA_POS);
1562 aCtx->core20fwd->glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
1564 aVerts->UnbindVertexAttrib (aCtx, Graphic3d_TOA_POS);
1565 theReadFbo->DepthStencilTexture()->Unbind (aCtx, Graphic3d_TextureUnit_1);
1566 theReadFbo->ColorTexture() ->Unbind (aCtx, Graphic3d_TextureUnit_0);
1567 aCtx->BindProgram (NULL);
1571 TCollection_ExtendedString aMsg = TCollection_ExtendedString()
1572 + "Error! FBO blitting has failed";
1573 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1574 GL_DEBUG_TYPE_ERROR,
1576 GL_DEBUG_SEVERITY_HIGH,
1578 myHasFboBlit = Standard_False;
1579 theReadFbo->Release (aCtx.operator->());
1586 // =======================================================================
1587 // function : drawStereoPair
1589 // =======================================================================
1590 void OpenGl_View::drawStereoPair (OpenGl_FrameBuffer* theDrawFbo)
1592 const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext();
1593 bindDefaultFbo (theDrawFbo);
1594 OpenGl_FrameBuffer* aPair[2] =
1596 myImmediateSceneFbos[0]->IsValid() ? myImmediateSceneFbos[0].operator->() : NULL,
1597 myImmediateSceneFbos[1]->IsValid() ? myImmediateSceneFbos[1].operator->() : NULL
1599 if (aPair[0] == NULL
1601 || !myTransientDrawToFront)
1603 aPair[0] = myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL;
1604 aPair[1] = myMainSceneFbos[1]->IsValid() ? myMainSceneFbos[1].operator->() : NULL;
1607 if (aPair[0] == NULL
1608 || aPair[1] == NULL)
1613 if (aPair[0]->NbSamples() != 0)
1615 // resolve MSAA buffers before drawing
1616 if (!myOpenGlFBO ->InitLazy (aCtx, aPair[0]->GetVPSizeX(), aPair[0]->GetVPSizeY(), myFboColorFormat, myFboDepthFormat, 0)
1617 || !myOpenGlFBO2->InitLazy (aCtx, aPair[0]->GetVPSizeX(), aPair[0]->GetVPSizeY(), myFboColorFormat, 0, 0))
1619 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1620 GL_DEBUG_TYPE_ERROR,
1622 GL_DEBUG_SEVERITY_HIGH,
1623 "Error! Unable to allocate FBO for blitting stereo pair");
1624 bindDefaultFbo (theDrawFbo);
1628 if (!blitBuffers (aPair[0], myOpenGlFBO .operator->(), Standard_False)
1629 || !blitBuffers (aPair[1], myOpenGlFBO2.operator->(), Standard_False))
1631 bindDefaultFbo (theDrawFbo);
1635 aPair[0] = myOpenGlFBO .operator->();
1636 aPair[1] = myOpenGlFBO2.operator->();
1637 bindDefaultFbo (theDrawFbo);
1642 Standard_Integer left;
1643 Standard_Integer top;
1644 Standard_Integer right;
1645 Standard_Integer bottom;
1646 Standard_Integer dx() { return right - left; }
1647 Standard_Integer dy() { return bottom - top; }
1650 myWindow->PlatformWindow()->Position (aGeom.left, aGeom.top, aGeom.right, aGeom.bottom);
1652 Standard_Boolean toReverse = myRenderParams.ToReverseStereo;
1653 const Standard_Boolean isOddY = (aGeom.top + aGeom.dy()) % 2 == 1;
1654 const Standard_Boolean isOddX = aGeom.left % 2 == 1;
1656 && (myRenderParams.StereoMode == Graphic3d_StereoMode_RowInterlaced
1657 || myRenderParams.StereoMode == Graphic3d_StereoMode_ChessBoard))
1659 toReverse = !toReverse;
1662 && (myRenderParams.StereoMode == Graphic3d_StereoMode_ColumnInterlaced
1663 || myRenderParams.StereoMode == Graphic3d_StereoMode_ChessBoard))
1665 toReverse = !toReverse;
1670 std::swap (aPair[0], aPair[1]);
1673 aCtx->core20fwd->glDepthFunc (GL_ALWAYS);
1674 aCtx->core20fwd->glDepthMask (GL_TRUE);
1675 aCtx->core20fwd->glEnable (GL_DEPTH_TEST);
1677 aCtx->BindTextures (Handle(OpenGl_TextureSet)());
1678 OpenGl_VertexBuffer* aVerts = initBlitQuad (myToFlipOutput);
1680 const Handle(OpenGl_ShaderManager)& aManager = aCtx->ShaderManager();
1681 if (aVerts->IsValid()
1682 && aManager->BindStereoProgram (myRenderParams.StereoMode))
1684 if (myRenderParams.StereoMode == Graphic3d_StereoMode_Anaglyph)
1686 OpenGl_Mat4 aFilterL, aFilterR;
1687 aFilterL.SetDiagonal (Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f));
1688 aFilterR.SetDiagonal (Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f));
1689 switch (myRenderParams.AnaglyphFilter)
1691 case Graphic3d_RenderingParams::Anaglyph_RedCyan_Simple:
1693 aFilterL.SetRow (0, Graphic3d_Vec4 (1.0f, 0.0f, 0.0f, 0.0f));
1694 aFilterR.SetRow (1, Graphic3d_Vec4 (0.0f, 1.0f, 0.0f, 0.0f));
1695 aFilterR.SetRow (2, Graphic3d_Vec4 (0.0f, 0.0f, 1.0f, 0.0f));
1698 case Graphic3d_RenderingParams::Anaglyph_RedCyan_Optimized:
1700 aFilterL.SetRow (0, Graphic3d_Vec4 ( 0.4154f, 0.4710f, 0.16666667f, 0.0f));
1701 aFilterL.SetRow (1, Graphic3d_Vec4 (-0.0458f, -0.0484f, -0.0257f, 0.0f));
1702 aFilterL.SetRow (2, Graphic3d_Vec4 (-0.0547f, -0.0615f, 0.0128f, 0.0f));
1703 aFilterL.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f));
1704 aFilterR.SetRow (0, Graphic3d_Vec4 (-0.01090909f, -0.03636364f, -0.00606061f, 0.0f));
1705 aFilterR.SetRow (1, Graphic3d_Vec4 ( 0.37560000f, 0.73333333f, 0.01111111f, 0.0f));
1706 aFilterR.SetRow (2, Graphic3d_Vec4 (-0.06510000f, -0.12870000f, 1.29710000f, 0.0f));
1707 aFilterR.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f));
1710 case Graphic3d_RenderingParams::Anaglyph_YellowBlue_Simple:
1712 aFilterL.SetRow (0, Graphic3d_Vec4 (1.0f, 0.0f, 0.0f, 0.0f));
1713 aFilterL.SetRow (1, Graphic3d_Vec4 (0.0f, 1.0f, 0.0f, 0.0f));
1714 aFilterR.SetRow (2, Graphic3d_Vec4 (0.0f, 0.0f, 1.0f, 0.0f));
1717 case Graphic3d_RenderingParams::Anaglyph_YellowBlue_Optimized:
1719 aFilterL.SetRow (0, Graphic3d_Vec4 ( 1.062f, -0.205f, 0.299f, 0.0f));
1720 aFilterL.SetRow (1, Graphic3d_Vec4 (-0.026f, 0.908f, 0.068f, 0.0f));
1721 aFilterL.SetRow (2, Graphic3d_Vec4 (-0.038f, -0.173f, 0.022f, 0.0f));
1722 aFilterL.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f));
1723 aFilterR.SetRow (0, Graphic3d_Vec4 (-0.016f, -0.123f, -0.017f, 0.0f));
1724 aFilterR.SetRow (1, Graphic3d_Vec4 ( 0.006f, 0.062f, -0.017f, 0.0f));
1725 aFilterR.SetRow (2, Graphic3d_Vec4 ( 0.094f, 0.185f, 0.911f, 0.0f));
1726 aFilterR.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f));
1729 case Graphic3d_RenderingParams::Anaglyph_GreenMagenta_Simple:
1731 aFilterR.SetRow (0, Graphic3d_Vec4 (1.0f, 0.0f, 0.0f, 0.0f));
1732 aFilterL.SetRow (1, Graphic3d_Vec4 (0.0f, 1.0f, 0.0f, 0.0f));
1733 aFilterR.SetRow (2, Graphic3d_Vec4 (0.0f, 0.0f, 1.0f, 0.0f));
1736 case Graphic3d_RenderingParams::Anaglyph_UserDefined:
1738 aFilterL = myRenderParams.AnaglyphLeft;
1739 aFilterR = myRenderParams.AnaglyphRight;
1743 aCtx->ActiveProgram()->SetUniform (aCtx, "uMultL", aFilterL);
1744 aCtx->ActiveProgram()->SetUniform (aCtx, "uMultR", aFilterR);
1747 aPair[0]->ColorTexture()->Bind (aCtx, Graphic3d_TextureUnit_0);
1748 aPair[1]->ColorTexture()->Bind (aCtx, Graphic3d_TextureUnit_1);
1749 aVerts->BindVertexAttrib (aCtx, 0);
1751 aCtx->core20fwd->glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
1753 aVerts->UnbindVertexAttrib (aCtx, 0);
1754 aPair[1]->ColorTexture()->Unbind (aCtx, Graphic3d_TextureUnit_1);
1755 aPair[0]->ColorTexture()->Unbind (aCtx, Graphic3d_TextureUnit_0);
1759 TCollection_ExtendedString aMsg = TCollection_ExtendedString()
1760 + "Error! Anaglyph has failed";
1761 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1762 GL_DEBUG_TYPE_ERROR,
1764 GL_DEBUG_SEVERITY_HIGH,
1769 // =======================================================================
1770 // function : copyBackToFront
1772 // =======================================================================
1773 bool OpenGl_View::copyBackToFront()
1775 myIsImmediateDrawn = Standard_False;
1776 #if !defined(GL_ES_VERSION_2_0)
1777 const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext();
1778 if (aCtx->core11 == NULL)
1783 OpenGl_Mat4 aProjectMat;
1784 Graphic3d_TransformUtils::Ortho2D (aProjectMat,
1785 0.0f, static_cast<GLfloat> (myWindow->Width()),
1786 0.0f, static_cast<GLfloat> (myWindow->Height()));
1788 aCtx->WorldViewState.Push();
1789 aCtx->ProjectionState.Push();
1791 aCtx->WorldViewState.SetIdentity();
1792 aCtx->ProjectionState.SetCurrent (aProjectMat);
1794 aCtx->ApplyProjectionMatrix();
1795 aCtx->ApplyWorldViewMatrix();
1797 // synchronize FFP state before copying pixels
1798 aCtx->BindProgram (Handle(OpenGl_ShaderProgram)());
1799 aCtx->ShaderManager()->PushState (Handle(OpenGl_ShaderProgram)());
1800 aCtx->DisableFeatures();
1802 switch (aCtx->DrawBuffer())
1806 aCtx->SetReadBuffer (GL_BACK_LEFT);
1807 aCtx->SetDrawBuffer (GL_FRONT_LEFT);
1812 aCtx->SetReadBuffer (GL_BACK_RIGHT);
1813 aCtx->SetDrawBuffer (GL_FRONT_RIGHT);
1818 aCtx->SetReadBuffer (GL_BACK);
1819 aCtx->SetDrawBuffer (GL_FRONT);
1824 aCtx->core11->glRasterPos2i (0, 0);
1825 aCtx->core11->glCopyPixels (0, 0, myWindow->Width() + 1, myWindow->Height() + 1, GL_COLOR);
1826 //aCtx->core11->glCopyPixels (0, 0, myWidth + 1, myHeight + 1, GL_DEPTH);
1828 aCtx->EnableFeatures();
1830 aCtx->WorldViewState.Pop();
1831 aCtx->ProjectionState.Pop();
1832 aCtx->ApplyProjectionMatrix();
1834 // read/write from front buffer now
1835 aCtx->SetReadBuffer (aCtx->DrawBuffer());
1842 // =======================================================================
1843 // function : checkOitCompatibility
1845 // =======================================================================
1846 Standard_Boolean OpenGl_View::checkOitCompatibility (const Handle(OpenGl_Context)& theGlContext,
1847 const Standard_Boolean theMSAA)
1849 // determine if OIT is supported by current OpenGl context
1850 Standard_Boolean& aToDisableOIT = theMSAA ? myToDisableMSAA : myToDisableOIT;
1853 return Standard_False;
1856 TCollection_ExtendedString aCompatibilityMsg;
1857 if (theGlContext->hasFloatBuffer == OpenGl_FeatureNotAvailable
1858 && theGlContext->hasHalfFloatBuffer == OpenGl_FeatureNotAvailable)
1860 aCompatibilityMsg += "OpenGL context does not support floating-point RGBA color buffer format.\n";
1862 if (theMSAA && theGlContext->hasSampleVariables == OpenGl_FeatureNotAvailable)
1864 aCompatibilityMsg += "Current version of GLSL does not support built-in sample variables.\n";
1866 if (theGlContext->hasDrawBuffers == OpenGl_FeatureNotAvailable)
1868 aCompatibilityMsg += "OpenGL context does not support multiple draw buffers.\n";
1870 if (aCompatibilityMsg.IsEmpty())
1872 return Standard_True;
1875 aCompatibilityMsg += " Blended order-independent transparency will not be available.\n";
1876 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1877 GL_DEBUG_TYPE_ERROR,
1879 GL_DEBUG_SEVERITY_HIGH,
1882 aToDisableOIT = Standard_True;
1883 return Standard_False;
1886 // =======================================================================
1887 // function : chooseOitColorConfiguration
1889 // =======================================================================
1890 bool OpenGl_View::chooseOitColorConfiguration (const Handle(OpenGl_Context)& theGlContext,
1891 const Standard_Integer theConfigIndex,
1892 OpenGl_ColorFormats& theFormats)
1895 switch (theConfigIndex)
1897 case 0: // choose best applicable color format combination
1899 theFormats.Append (theGlContext->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable ? GL_RGBA16F : GL_RGBA32F);
1900 theFormats.Append (theGlContext->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable ? GL_R16F : GL_R32F);
1903 case 1: // choose non-optimal applicable color format combination
1905 theFormats.Append (theGlContext->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable ? GL_RGBA16F : GL_RGBA32F);
1906 theFormats.Append (theGlContext->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable ? GL_RGBA16F : GL_RGBA32F);
1910 return false; // color combination does not exist
1913 // =======================================================================
1914 // function : checkPBRAvailability
1916 // =======================================================================
1917 Standard_Boolean OpenGl_View::checkPBRAvailability() const
1919 return myWorkspace->GetGlContext()->HasPBR()
1920 && !myPBREnvironment.IsNull();
1923 // =======================================================================
1924 // function : bakePBREnvironment
1926 // =======================================================================
1927 void OpenGl_View::bakePBREnvironment (const Handle(OpenGl_Context)& theCtx)
1929 const Handle(OpenGl_TextureSet)& aTextureSet = myCubeMapParams->TextureSet (theCtx);
1930 if (!aTextureSet.IsNull()
1931 && !aTextureSet->IsEmpty())
1933 myPBREnvironment->Bake (theCtx,
1934 aTextureSet->First(),
1935 myBackgroundCubeMap->ZIsInverted(),
1936 myBackgroundCubeMap->IsTopDown(),
1937 myRenderParams.PbrEnvBakingDiffNbSamples,
1938 myRenderParams.PbrEnvBakingSpecNbSamples,
1939 myRenderParams.PbrEnvBakingProbability);
1943 myPBREnvironment->Clear (theCtx);
1947 // =======================================================================
1948 // function : clearPBREnvironment
1950 // =======================================================================
1951 void OpenGl_View::clearPBREnvironment (const Handle(OpenGl_Context)& theCtx)
1953 myPBREnvironment->Clear (theCtx);
1956 // =======================================================================
1957 // function : clearPBREnvironment
1959 // =======================================================================
1960 void OpenGl_View::processPBREnvRequest (const Handle(OpenGl_Context)& theCtx)
1962 if (myPBREnvState == OpenGl_PBREnvState_CREATED)
1964 switch (myPBREnvRequest)
1966 case OpenGl_PBREnvRequest_NONE: return;
1967 case OpenGl_PBREnvRequest_BAKE: bakePBREnvironment (theCtx); break;
1968 case OpenGl_PBREnvRequest_CLEAR: clearPBREnvironment (theCtx); break;
1971 myPBREnvRequest = OpenGl_PBREnvRequest_NONE;