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_AspectLine.hxx>
31 #include <OpenGl_Context.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>
44 //! Format Frame Buffer format for logging messages.
45 static TCollection_AsciiString printFboFormat (const Handle(OpenGl_FrameBuffer)& theFbo)
47 return TCollection_AsciiString() + theFbo->GetInitVPSizeX() + "x" + theFbo->GetInitVPSizeY() + "@" + theFbo->NbSamples();
50 //! Return TRUE if Frame Buffer initialized has failed with the same parameters.
51 static bool checkWasFailedFbo (const Handle(OpenGl_FrameBuffer)& theFboToCheck,
52 Standard_Integer theSizeX,
53 Standard_Integer theSizeY,
54 Standard_Integer theNbSamples)
56 return !theFboToCheck->IsValid()
57 && theFboToCheck->GetInitVPSizeX() == theSizeX
58 && theFboToCheck->GetInitVPSizeY() == theSizeY
59 && theFboToCheck->NbSamples() == theNbSamples;
62 //! Return TRUE if Frame Buffer initialized has failed with the same parameters.
63 static bool checkWasFailedFbo (const Handle(OpenGl_FrameBuffer)& theFboToCheck,
64 const Handle(OpenGl_FrameBuffer)& theFboRef)
66 return checkWasFailedFbo (theFboToCheck, theFboRef->GetVPSizeX(), theFboRef->GetVPSizeY(), theFboRef->NbSamples());
70 //=======================================================================
71 //function : drawBackground
73 //=======================================================================
74 void OpenGl_View::drawBackground (const Handle(OpenGl_Workspace)& theWorkspace)
76 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
78 if (!myBgTextureArray->IsDefined() // no texture
79 && !myBgGradientArray->IsDefined()) // no gradient
84 const Standard_Boolean wasUsedZBuffer = theWorkspace->SetUseZBuffer (Standard_False);
87 aCtx->core11fwd->glDisable (GL_DEPTH_TEST);
90 // Drawing background gradient if:
91 // - gradient fill type is not Aspect_GFM_NONE and
92 // - either background texture is no specified or it is drawn in Aspect_FM_CENTERED mode
93 if (myBgGradientArray->IsDefined()
94 && (!myTextureParams->Aspect()->ToMapTexture()
95 || myBgTextureArray->TextureFillMethod() == Aspect_FM_CENTERED
96 || myBgTextureArray->TextureFillMethod() == Aspect_FM_NONE))
98 #if !defined(GL_ES_VERSION_2_0)
99 GLint aShadingModelOld = GL_SMOOTH;
100 if (aCtx->core11 != NULL
101 && aCtx->caps->ffpEnable)
103 aCtx->core11fwd->glDisable (GL_LIGHTING);
104 aCtx->core11fwd->glGetIntegerv (GL_SHADE_MODEL, &aShadingModelOld);
105 aCtx->core11->glShadeModel (GL_SMOOTH);
109 myBgGradientArray->Render (theWorkspace);
111 #if !defined(GL_ES_VERSION_2_0)
112 if (aCtx->core11 != NULL
113 && aCtx->caps->ffpEnable)
115 aCtx->core11->glShadeModel (aShadingModelOld);
120 // Drawing background image if it is defined
121 // (texture is defined and fill type is not Aspect_FM_NONE)
122 if (myBgTextureArray->IsDefined()
123 && myTextureParams->Aspect()->ToMapTexture())
125 aCtx->core11fwd->glDisable (GL_BLEND);
127 const OpenGl_AspectFace* anOldAspectFace = theWorkspace->SetAspectFace (myTextureParams);
128 myBgTextureArray->Render (theWorkspace);
129 theWorkspace->SetAspectFace (anOldAspectFace);
134 theWorkspace->SetUseZBuffer (Standard_True);
135 aCtx->core11fwd->glEnable (GL_DEPTH_TEST);
139 //=======================================================================
142 //=======================================================================
143 void OpenGl_View::Redraw()
145 const Standard_Boolean wasDisabledMSAA = myToDisableMSAA;
146 const Standard_Boolean hadFboBlit = myHasFboBlit;
147 if (myRenderParams.Method == Graphic3d_RM_RAYTRACING
148 && !myCaps->vboDisable
149 && !myCaps->keepArrayData)
151 // caps are shared across all views, thus we need to invalidate all of them
152 // if (myWasRedrawnGL) { myStructureManager->SetDeviceLost(); }
153 myDriver->setDeviceLost();
154 myCaps->keepArrayData = Standard_True;
157 if (!myWorkspace->Activate())
162 myWindow->SetSwapInterval();
165 const Graphic3d_StereoMode aStereoMode = myRenderParams.StereoMode;
166 Graphic3d_Camera::Projection aProjectType = myCamera->ProjectionType();
167 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
169 // release pending GL resources
170 aCtx->ReleaseDelayed();
172 // fetch OpenGl context state
175 OpenGl_FrameBuffer* aFrameBuffer = myFBO.operator->();
176 bool toSwap = aCtx->IsRender()
177 && !aCtx->caps->buffersNoSwap
178 && aFrameBuffer == NULL;
180 Standard_Integer aSizeX = aFrameBuffer != NULL ? aFrameBuffer->GetVPSizeX() : myWindow->Width();
181 Standard_Integer aSizeY = aFrameBuffer != NULL ? aFrameBuffer->GetVPSizeY() : myWindow->Height();
182 Standard_Integer aRendSizeX = Standard_Integer(myRenderParams.RenderResolutionScale * aSizeX + 0.5f);
183 Standard_Integer aRendSizeY = Standard_Integer(myRenderParams.RenderResolutionScale * aSizeY + 0.5f);
185 // determine multisampling parameters
186 Standard_Integer aNbSamples = !myToDisableMSAA && aSizeX == aRendSizeX
187 ? Max (Min (myRenderParams.NbMsaaSamples, aCtx->MaxMsaaSamples()), 0)
191 aNbSamples = OpenGl_Context::GetPowerOfTwo (aNbSamples, aCtx->MaxMsaaSamples());
194 bool toUseOit = myRenderParams.TransparencyMethod == Graphic3d_RTM_BLEND_OIT
195 && checkOitCompatibility (aCtx, aNbSamples > 0);
197 const bool toInitImmediateFbo = myTransientDrawToFront
198 && (!aCtx->caps->useSystemBuffer || (toUseOit && HasImmediateStructures()));
200 if ( aFrameBuffer == NULL
201 && !aCtx->DefaultFrameBuffer().IsNull()
202 && aCtx->DefaultFrameBuffer()->IsValid())
204 aFrameBuffer = aCtx->DefaultFrameBuffer().operator->();
208 && (myTransientDrawToFront
209 || aProjectType == Graphic3d_Camera::Projection_Stereo
212 || aSizeX != aRendSizeX))
214 if (myMainSceneFbos[0]->GetVPSizeX() != aRendSizeX
215 || myMainSceneFbos[0]->GetVPSizeY() != aRendSizeY
216 || myMainSceneFbos[0]->NbSamples() != aNbSamples)
218 if (!myTransientDrawToFront)
220 myImmediateSceneFbos[0]->Release (aCtx.operator->());
221 myImmediateSceneFbos[1]->Release (aCtx.operator->());
222 myImmediateSceneFbos[0]->ChangeViewport (0, 0);
223 myImmediateSceneFbos[1]->ChangeViewport (0, 0);
226 // prepare FBOs containing main scene
227 // for further blitting and rendering immediate presentations on top
228 if (aCtx->core20fwd != NULL)
230 const bool wasFailedMain0 = checkWasFailedFbo (myMainSceneFbos[0], aRendSizeX, aRendSizeY, aNbSamples);
231 if (!myMainSceneFbos[0]->Init (aCtx, aRendSizeX, aRendSizeY, myFboColorFormat, myFboDepthFormat, aNbSamples)
234 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "Error! Main FBO "
235 + printFboFormat (myMainSceneFbos[0]) + " initialization has failed";
236 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
240 if (myMainSceneFbos[0]->IsValid() && (toInitImmediateFbo || myImmediateSceneFbos[0]->IsValid()))
242 const bool wasFailedImm0 = checkWasFailedFbo (myImmediateSceneFbos[0], myMainSceneFbos[0]);
243 if (!myImmediateSceneFbos[0]->InitLazy (aCtx, *myMainSceneFbos[0])
246 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "Error! Immediate FBO "
247 + printFboFormat (myImmediateSceneFbos[0]) + " initialization has failed";
248 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
254 myMainSceneFbos [0]->Release (aCtx.operator->());
255 myMainSceneFbos [1]->Release (aCtx.operator->());
256 myImmediateSceneFbos[0]->Release (aCtx.operator->());
257 myImmediateSceneFbos[1]->Release (aCtx.operator->());
258 myMainSceneFbos [0]->ChangeViewport (0, 0);
259 myMainSceneFbos [1]->ChangeViewport (0, 0);
260 myImmediateSceneFbos[0]->ChangeViewport (0, 0);
261 myImmediateSceneFbos[1]->ChangeViewport (0, 0);
264 if (aProjectType == Graphic3d_Camera::Projection_Stereo
265 && myMainSceneFbos[0]->IsValid())
267 const bool wasFailedMain1 = checkWasFailedFbo (myMainSceneFbos[1], myMainSceneFbos[0]);
268 if (!myMainSceneFbos[1]->InitLazy (aCtx, *myMainSceneFbos[0])
271 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "Error! Main FBO (second) "
272 + printFboFormat (myMainSceneFbos[1]) + " initialization has failed";
273 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
275 if (!myMainSceneFbos[1]->IsValid())
278 aProjectType = Graphic3d_Camera::Projection_Perspective;
280 else if (!myTransientDrawToFront)
284 else if (!aCtx->HasStereoBuffers() || aStereoMode != Graphic3d_StereoMode_QuadBuffer)
286 const bool wasFailedImm0 = checkWasFailedFbo (myImmediateSceneFbos[0], myMainSceneFbos[0]);
287 const bool wasFailedImm1 = checkWasFailedFbo (myImmediateSceneFbos[1], myMainSceneFbos[0]);
288 if (!myImmediateSceneFbos[0]->InitLazy (aCtx, *myMainSceneFbos[0])
291 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "Error! Immediate FBO (first) "
292 + printFboFormat (myImmediateSceneFbos[0]) + " initialization has failed";
293 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
295 if (!myImmediateSceneFbos[1]->InitLazy (aCtx, *myMainSceneFbos[0])
298 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "Error! Immediate FBO (first) "
299 + printFboFormat (myImmediateSceneFbos[1]) + " initialization has failed";
300 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
302 if (!myImmediateSceneFbos[0]->IsValid()
303 || !myImmediateSceneFbos[1]->IsValid())
305 aProjectType = Graphic3d_Camera::Projection_Perspective;
310 // create color and coverage accumulation buffers required for OIT algorithm
313 Standard_Integer anFboIt = 0;
314 for (; anFboIt < 2; ++anFboIt)
316 Handle(OpenGl_FrameBuffer)& aMainSceneFbo = myMainSceneFbos [anFboIt];
317 Handle(OpenGl_FrameBuffer)& aMainSceneFboOit = myMainSceneFbosOit [anFboIt];
318 Handle(OpenGl_FrameBuffer)& anImmediateSceneFbo = myImmediateSceneFbos [anFboIt];
319 Handle(OpenGl_FrameBuffer)& anImmediateSceneFboOit = myImmediateSceneFbosOit[anFboIt];
320 if (aMainSceneFbo->IsValid()
321 && (aMainSceneFboOit->GetVPSizeX() != aRendSizeX
322 || aMainSceneFboOit->GetVPSizeY() != aRendSizeY
323 || aMainSceneFboOit->NbSamples() != aNbSamples))
325 Standard_Integer aColorConfig = 0;
326 for (;;) // seemly responding to driver limitation (GL_FRAMEBUFFER_UNSUPPORTED)
328 if (myFboOitColorConfig.IsEmpty())
330 if (!chooseOitColorConfiguration (aCtx, aColorConfig++, myFboOitColorConfig))
335 if (aMainSceneFboOit->Init (aCtx, aRendSizeX, aRendSizeY, myFboOitColorConfig, aMainSceneFbo->DepthStencilTexture(), aNbSamples))
339 myFboOitColorConfig.Clear();
341 if (!aMainSceneFboOit->IsValid())
346 else if (!aMainSceneFbo->IsValid())
348 aMainSceneFboOit->Release (aCtx.operator->());
349 aMainSceneFboOit->ChangeViewport (0, 0);
352 if (anImmediateSceneFbo->IsValid()
353 && (anImmediateSceneFboOit->GetVPSizeX() != aRendSizeX
354 || anImmediateSceneFboOit->GetVPSizeY() != aRendSizeY
355 || anImmediateSceneFboOit->NbSamples() != aNbSamples))
357 if (!anImmediateSceneFboOit->Init (aCtx, aRendSizeX, aRendSizeY, myFboOitColorConfig,
358 anImmediateSceneFbo->DepthStencilTexture(), aNbSamples))
363 else if (!anImmediateSceneFbo->IsValid())
365 anImmediateSceneFboOit->Release (aCtx.operator->());
366 anImmediateSceneFboOit->ChangeViewport (0, 0);
369 if (anFboIt == 0) // only the first OIT framebuffer is mandatory
371 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH,
372 "Initialization of float texture framebuffer for use with\n"
373 " blended order-independent transparency rendering algorithm has failed.\n"
374 " Blended order-independent transparency will not be available.\n");
377 myToDisableOITMSAA = Standard_True;
381 myToDisableOIT = Standard_True;
386 if (!toUseOit && myMainSceneFbosOit[0]->IsValid())
388 myMainSceneFbosOit [0]->Release (aCtx.operator->());
389 myMainSceneFbosOit [1]->Release (aCtx.operator->());
390 myImmediateSceneFbosOit[0]->Release (aCtx.operator->());
391 myImmediateSceneFbosOit[1]->Release (aCtx.operator->());
392 myMainSceneFbosOit [0]->ChangeViewport (0, 0);
393 myMainSceneFbosOit [1]->ChangeViewport (0, 0);
394 myImmediateSceneFbosOit[0]->ChangeViewport (0, 0);
395 myImmediateSceneFbosOit[1]->ChangeViewport (0, 0);
398 if (aProjectType == Graphic3d_Camera::Projection_Stereo)
400 OpenGl_FrameBuffer* aMainFbos[2] =
402 myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL,
403 myMainSceneFbos[1]->IsValid() ? myMainSceneFbos[1].operator->() : NULL
405 OpenGl_FrameBuffer* aMainFbosOit[2] =
407 myMainSceneFbosOit[0]->IsValid() ? myMainSceneFbosOit[0].operator->() : NULL,
408 myMainSceneFbosOit[1]->IsValid() ? myMainSceneFbosOit[1].operator->() :
409 myMainSceneFbosOit[0]->IsValid() ? myMainSceneFbosOit[0].operator->() : NULL
412 OpenGl_FrameBuffer* anImmFbos[2] =
414 myImmediateSceneFbos[0]->IsValid() ? myImmediateSceneFbos[0].operator->() : NULL,
415 myImmediateSceneFbos[1]->IsValid() ? myImmediateSceneFbos[1].operator->() : NULL
417 OpenGl_FrameBuffer* anImmFbosOit[2] =
419 myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL,
420 myImmediateSceneFbosOit[1]->IsValid() ? myImmediateSceneFbosOit[1].operator->() :
421 myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL
424 if (!myTransientDrawToFront)
426 anImmFbos [0] = aMainFbos [0];
427 anImmFbos [1] = aMainFbos [1];
428 anImmFbosOit[0] = aMainFbosOit[0];
429 anImmFbosOit[1] = aMainFbosOit[1];
431 else if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip
432 || aStereoMode == Graphic3d_StereoMode_QuadBuffer)
434 anImmFbos [0] = NULL;
435 anImmFbos [1] = NULL;
436 anImmFbosOit[0] = NULL;
437 anImmFbosOit[1] = NULL;
440 #if !defined(GL_ES_VERSION_2_0)
441 aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_LEFT : GL_BACK);
443 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
444 aMainFbos[0] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
446 redraw (Graphic3d_Camera::Projection_MonoLeftEye, aMainFbos[0], aMainFbosOit[0]);
447 myBackBufferRestored = Standard_True;
448 myIsImmediateDrawn = Standard_False;
449 #if !defined(GL_ES_VERSION_2_0)
450 aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_LEFT : GL_BACK);
452 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
453 anImmFbos[0] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
454 if (!redrawImmediate (Graphic3d_Camera::Projection_MonoLeftEye, aMainFbos[0], anImmFbos[0], anImmFbosOit[0]))
458 else if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip && toSwap)
463 #if !defined(GL_ES_VERSION_2_0)
464 aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_RIGHT : GL_BACK);
466 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
467 aMainFbos[1] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
469 redraw (Graphic3d_Camera::Projection_MonoRightEye, aMainFbos[1], aMainFbosOit[1]);
470 myBackBufferRestored = Standard_True;
471 myIsImmediateDrawn = Standard_False;
472 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
473 anImmFbos[1] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
474 if (!redrawImmediate (Graphic3d_Camera::Projection_MonoRightEye, aMainFbos[1], anImmFbos[1], anImmFbosOit[1]))
479 if (anImmFbos[0] != NULL)
481 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(), 1.0f);
482 drawStereoPair (aFrameBuffer);
487 OpenGl_FrameBuffer* aMainFbo = myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : aFrameBuffer;
488 OpenGl_FrameBuffer* aMainFboOit = myMainSceneFbosOit[0]->IsValid() ? myMainSceneFbosOit[0].operator->() : NULL;
489 OpenGl_FrameBuffer* anImmFbo = aFrameBuffer;
490 OpenGl_FrameBuffer* anImmFboOit = NULL;
491 if (!myTransientDrawToFront)
494 anImmFboOit = aMainFboOit;
496 else if (myImmediateSceneFbos[0]->IsValid())
498 anImmFbo = myImmediateSceneFbos[0].operator->();
499 anImmFboOit = myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL;
502 #if !defined(GL_ES_VERSION_2_0)
503 if (aMainFbo == NULL)
505 aCtx->SetReadDrawBuffer (GL_BACK);
508 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
509 aMainFbo != aFrameBuffer ? myRenderParams.RenderResolutionScale : 1.0f);
511 redraw (aProjectType, aMainFbo, aMainFboOit);
512 myBackBufferRestored = Standard_True;
513 myIsImmediateDrawn = Standard_False;
514 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
515 anImmFbo != aFrameBuffer ? myRenderParams.RenderResolutionScale : 1.0f);
516 if (!redrawImmediate (aProjectType, aMainFbo, anImmFbo, anImmFboOit))
522 && anImmFbo != aFrameBuffer)
524 blitBuffers (anImmFbo, aFrameBuffer, myToFlipOutput);
528 if (myRenderParams.Method == Graphic3d_RM_RAYTRACING
529 && myRenderParams.IsGlobalIlluminationEnabled)
537 if (wasDisabledMSAA != myToDisableMSAA
538 || hadFboBlit != myHasFboBlit)
544 // reset state for safety
545 aCtx->BindProgram (Handle(OpenGl_ShaderProgram)());
546 if (aCtx->caps->ffpEnable)
548 aCtx->ShaderManager()->PushState (Handle(OpenGl_ShaderProgram)());
555 if (!myMainSceneFbos[0]->IsValid())
557 myBackBufferRestored = Standard_False;
562 aCtx->core11fwd->glFlush();
565 // reset render mode state
568 myWasRedrawnGL = Standard_True;
571 // =======================================================================
572 // function : RedrawImmediate
574 // =======================================================================
575 void OpenGl_View::RedrawImmediate()
577 if (!myWorkspace->Activate())
580 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
581 if (!myTransientDrawToFront
582 || !myBackBufferRestored
583 || (aCtx->caps->buffersNoSwap && !myMainSceneFbos[0]->IsValid()))
589 const Graphic3d_StereoMode aStereoMode = myRenderParams.StereoMode;
590 Graphic3d_Camera::Projection aProjectType = myCamera->ProjectionType();
591 OpenGl_FrameBuffer* aFrameBuffer = myFBO.operator->();
593 if ( aFrameBuffer == NULL
594 && !aCtx->DefaultFrameBuffer().IsNull()
595 && aCtx->DefaultFrameBuffer()->IsValid())
597 aFrameBuffer = aCtx->DefaultFrameBuffer().operator->();
600 if (aProjectType == Graphic3d_Camera::Projection_Stereo)
602 if (myMainSceneFbos[0]->IsValid()
603 && !myMainSceneFbos[1]->IsValid())
605 aProjectType = Graphic3d_Camera::Projection_Perspective;
610 if (aProjectType == Graphic3d_Camera::Projection_Stereo)
612 OpenGl_FrameBuffer* aMainFbos[2] =
614 myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL,
615 myMainSceneFbos[1]->IsValid() ? myMainSceneFbos[1].operator->() : NULL
617 OpenGl_FrameBuffer* anImmFbos[2] =
619 myImmediateSceneFbos[0]->IsValid() ? myImmediateSceneFbos[0].operator->() : NULL,
620 myImmediateSceneFbos[1]->IsValid() ? myImmediateSceneFbos[1].operator->() : NULL
622 OpenGl_FrameBuffer* anImmFbosOit[2] =
624 myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL,
625 myImmediateSceneFbosOit[1]->IsValid() ? myImmediateSceneFbosOit[1].operator->() :
626 myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL
628 if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip
629 || aStereoMode == Graphic3d_StereoMode_QuadBuffer)
633 anImmFbosOit[0] = NULL;
634 anImmFbosOit[1] = NULL;
637 if (aCtx->arbFBO != NULL)
639 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
641 #if !defined(GL_ES_VERSION_2_0)
642 if (anImmFbos[0] == NULL)
644 aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_LEFT : GL_BACK);
648 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
649 anImmFbos[0] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
650 toSwap = redrawImmediate (Graphic3d_Camera::Projection_MonoLeftEye,
654 Standard_True) || toSwap;
655 if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip
657 && !aCtx->caps->buffersNoSwap)
662 if (aCtx->arbFBO != NULL)
664 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
666 #if !defined(GL_ES_VERSION_2_0)
667 if (anImmFbos[1] == NULL)
669 aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_RIGHT : GL_BACK);
672 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
673 anImmFbos[1] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
674 toSwap = redrawImmediate (Graphic3d_Camera::Projection_MonoRightEye,
678 Standard_True) || toSwap;
679 if (anImmFbos[0] != NULL)
681 drawStereoPair (aFrameBuffer);
686 OpenGl_FrameBuffer* aMainFbo = myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL;
687 OpenGl_FrameBuffer* anImmFbo = aFrameBuffer;
688 OpenGl_FrameBuffer* anImmFboOit = NULL;
689 if (myImmediateSceneFbos[0]->IsValid())
691 anImmFbo = myImmediateSceneFbos[0].operator->();
692 anImmFboOit = myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL;
694 #if !defined(GL_ES_VERSION_2_0)
695 if (aMainFbo == NULL)
697 aCtx->SetReadDrawBuffer (GL_BACK);
700 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
701 anImmFbo != aFrameBuffer ? myRenderParams.RenderResolutionScale : 1.0f);
702 toSwap = redrawImmediate (aProjectType,
706 Standard_True) || toSwap;
708 && anImmFbo != aFrameBuffer)
710 blitBuffers (anImmFbo, aFrameBuffer, myToFlipOutput);
717 // reset state for safety
718 aCtx->BindProgram (Handle(OpenGl_ShaderProgram)());
719 if (aCtx->caps->ffpEnable)
721 aCtx->ShaderManager()->PushState (Handle(OpenGl_ShaderProgram)());
724 if (toSwap && !aCtx->caps->buffersNoSwap)
730 aCtx->core11fwd->glFlush();
733 myWasRedrawnGL = Standard_True;
736 // =======================================================================
739 // =======================================================================
740 void OpenGl_View::redraw (const Graphic3d_Camera::Projection theProjection,
741 OpenGl_FrameBuffer* theReadDrawFbo,
742 OpenGl_FrameBuffer* theOitAccumFbo)
744 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
745 if (theReadDrawFbo != NULL)
747 theReadDrawFbo->BindBuffer (aCtx);
748 theReadDrawFbo->SetupViewport (aCtx);
752 const Standard_Integer aViewport[4] = { 0, 0, myWindow->Width(), myWindow->Height() };
753 aCtx->ResizeViewport (aViewport);
756 // request reset of material
757 aCtx->ShaderManager()->UpdateMaterialState();
759 myWorkspace->UseZBuffer() = Standard_True;
760 myWorkspace->UseDepthWrite() = Standard_True;
761 GLbitfield toClear = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT;
762 glDepthFunc (GL_LEQUAL);
763 glDepthMask (GL_TRUE);
764 glEnable (GL_DEPTH_TEST);
766 #if !defined(GL_ES_VERSION_2_0)
769 glClearDepthf (1.0f);
772 const OpenGl_Vec4& aBgColor = myBgColor;
773 glClearColor (aBgColor.r(), aBgColor.g(), aBgColor.b(), 0.0f);
777 render (theProjection, theReadDrawFbo, theOitAccumFbo, Standard_False);
780 // =======================================================================
781 // function : redrawMonoImmediate
783 // =======================================================================
784 bool OpenGl_View::redrawImmediate (const Graphic3d_Camera::Projection theProjection,
785 OpenGl_FrameBuffer* theReadFbo,
786 OpenGl_FrameBuffer* theDrawFbo,
787 OpenGl_FrameBuffer* theOitAccumFbo,
788 const Standard_Boolean theIsPartialUpdate)
790 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
791 GLboolean toCopyBackToFront = GL_FALSE;
792 if (theDrawFbo == theReadFbo
793 && theDrawFbo != NULL)
795 myBackBufferRestored = Standard_False;
797 else if (theReadFbo != NULL
798 && theReadFbo->IsValid()
801 if (!blitBuffers (theReadFbo, theDrawFbo))
806 else if (theDrawFbo == NULL)
808 #if !defined(GL_ES_VERSION_2_0)
809 aCtx->core11fwd->glGetBooleanv (GL_DOUBLEBUFFER, &toCopyBackToFront);
811 if (toCopyBackToFront
812 && myTransientDrawToFront)
814 if (!HasImmediateStructures()
815 && !theIsPartialUpdate)
817 // prefer Swap Buffers within Redraw in compatibility mode (without FBO)
820 if (!copyBackToFront())
822 toCopyBackToFront = GL_FALSE;
823 myBackBufferRestored = Standard_False;
828 myBackBufferRestored = Standard_False;
833 myBackBufferRestored = Standard_False;
835 myIsImmediateDrawn = Standard_True;
837 myWorkspace->UseZBuffer() = Standard_True;
838 myWorkspace->UseDepthWrite() = Standard_True;
839 glDepthFunc (GL_LEQUAL);
840 glDepthMask (GL_TRUE);
841 glEnable (GL_DEPTH_TEST);
842 #if !defined(GL_ES_VERSION_2_0)
845 glClearDepthf (1.0f);
848 render (theProjection, theDrawFbo, theOitAccumFbo, Standard_True);
850 return !toCopyBackToFront;
853 //=======================================================================
856 //=======================================================================
857 void OpenGl_View::render (Graphic3d_Camera::Projection theProjection,
858 OpenGl_FrameBuffer* theOutputFBO,
859 OpenGl_FrameBuffer* theOitAccumFbo,
860 const Standard_Boolean theToDrawImmediate)
862 // ==================================
863 // Step 1: Prepare for render
864 // ==================================
866 const Handle(OpenGl_Context)& aContext = myWorkspace->GetGlContext();
868 #if !defined(GL_ES_VERSION_2_0)
869 // Disable current clipping planes
870 if (aContext->core11 != NULL)
872 const Standard_Integer aMaxPlanes = aContext->MaxClipPlanes();
873 for (Standard_Integer aClipPlaneId = GL_CLIP_PLANE0; aClipPlaneId < GL_CLIP_PLANE0 + aMaxPlanes; ++aClipPlaneId)
875 aContext->core11fwd->glDisable (aClipPlaneId);
880 // Update states of OpenGl_BVHTreeSelector (frustum culling algorithm).
881 myBVHSelector.SetViewVolume (myCamera);
882 myBVHSelector.SetViewportSize (myWindow->Width(), myWindow->Height());
884 const Handle(OpenGl_ShaderManager)& aManager = aContext->ShaderManager();
885 if (StateInfo (myCurrLightSourceState, aManager->LightSourceState().Index()) != myLastLightSourceState)
887 aManager->UpdateLightSourceStateTo (myShadingModel == Graphic3d_TOSM_NONE ? &myNoShadingLight : &myLights);
888 myLastLightSourceState = StateInfo (myCurrLightSourceState, aManager->LightSourceState().Index());
891 // Update matrices if camera has changed.
892 Graphic3d_WorldViewProjState aWVPState = myCamera->WorldViewProjState();
893 if (myWorldViewProjState != aWVPState)
896 myWorldViewProjState = aWVPState;
899 myLocalOrigin.SetCoord (0.0, 0.0, 0.0);
900 aContext->ProjectionState.SetCurrent (myCamera->ProjectionMatrixF());
901 aContext->WorldViewState .SetCurrent (myCamera->OrientationMatrixF());
902 aContext->ApplyProjectionMatrix();
903 aContext->ApplyWorldViewMatrix();
904 if (aManager->ModelWorldState().Index() == 0)
906 aContext->ShaderManager()->UpdateModelWorldStateTo (OpenGl_Mat4());
909 // ====================================
910 // Step 2: Redraw background
911 // ====================================
914 if (!theToDrawImmediate)
916 drawBackground (myWorkspace);
919 #if !defined(GL_ES_VERSION_2_0)
920 // Switch off lighting by default
921 if (aContext->core11 != NULL
922 && aContext->caps->ffpEnable)
924 glDisable(GL_LIGHTING);
928 // =================================
929 // Step 3: Redraw main plane
930 // =================================
932 // Setup face culling
933 GLboolean isCullFace = GL_FALSE;
934 if (myBackfacing != Graphic3d_TOBM_AUTOMATIC)
936 isCullFace = glIsEnabled (GL_CULL_FACE);
937 if (myBackfacing == Graphic3d_TOBM_DISABLE)
939 glEnable (GL_CULL_FACE);
940 glCullFace (GL_BACK);
943 glDisable (GL_CULL_FACE);
946 #if !defined(GL_ES_VERSION_2_0)
947 // if the view is scaled normal vectors are scaled to unit
948 // length for correct displaying of shaded objects
949 const gp_Pnt anAxialScale = myCamera->AxialScale();
950 if (anAxialScale.X() != 1.F ||
951 anAxialScale.Y() != 1.F ||
952 anAxialScale.Z() != 1.F)
954 aContext->SetGlNormalizeEnabled (Standard_True);
958 aContext->SetGlNormalizeEnabled (Standard_False);
961 // Apply InteriorShadingMethod
962 if (aContext->core11 != NULL)
964 aContext->core11->glShadeModel (myShadingModel == Graphic3d_TOSM_FACET
965 || myShadingModel == Graphic3d_TOSM_NONE ? GL_FLAT : GL_SMOOTH);
969 aManager->SetShadingModel (myShadingModel);
972 if (theProjection == Graphic3d_Camera::Projection_MonoLeftEye)
974 aContext->ProjectionState.SetCurrent (myCamera->ProjectionStereoLeftF());
975 aContext->ApplyProjectionMatrix();
977 else if (theProjection == Graphic3d_Camera::Projection_MonoRightEye)
979 aContext->ProjectionState.SetCurrent (myCamera->ProjectionStereoRightF());
980 aContext->ApplyProjectionMatrix();
983 myWorkspace->SetEnvironmentTexture (myTextureEnv);
985 renderScene (theProjection, theOutputFBO, theOitAccumFbo, theToDrawImmediate);
987 myWorkspace->SetEnvironmentTexture (Handle(OpenGl_TextureSet)());
989 // ===============================
991 // ===============================
993 // Resetting GL parameters according to the default aspects
994 // in order to synchronize GL state with the graphic driver state
995 // before drawing auxiliary stuff (trihedrons, overlayer)
996 myWorkspace->ResetAppliedAspect();
999 if (!theToDrawImmediate)
1001 renderTrihedron (myWorkspace);
1003 // Restore face culling
1004 if (myBackfacing != Graphic3d_TOBM_AUTOMATIC)
1008 glEnable (GL_CULL_FACE);
1009 glCullFace (GL_BACK);
1012 glDisable (GL_CULL_FACE);
1016 // reset FFP state for safety
1017 aContext->BindProgram (Handle(OpenGl_ShaderProgram)());
1018 if (aContext->caps->ffpEnable)
1020 aContext->ShaderManager()->PushState (Handle(OpenGl_ShaderProgram)());
1023 // ==============================================================
1024 // Step 6: Keep shader manager informed about last View
1025 // ==============================================================
1027 if (!aManager.IsNull())
1029 aManager->SetLastView (this);
1033 // =======================================================================
1034 // function : InvalidateBVHData
1036 // =======================================================================
1037 void OpenGl_View::InvalidateBVHData (const Graphic3d_ZLayerId theLayerId)
1039 myZLayers.InvalidateBVHData (theLayerId);
1042 //=======================================================================
1043 //function : renderStructs
1045 //=======================================================================
1046 void OpenGl_View::renderStructs (Graphic3d_Camera::Projection theProjection,
1047 OpenGl_FrameBuffer* theReadDrawFbo,
1048 OpenGl_FrameBuffer* theOitAccumFbo,
1049 const Standard_Boolean theToDrawImmediate)
1051 if ( myZLayers.NbStructures() <= 0 )
1054 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
1055 Standard_Boolean toRenderGL = theToDrawImmediate ||
1056 myRenderParams.Method != Graphic3d_RM_RAYTRACING ||
1057 myRaytraceInitStatus == OpenGl_RT_FAIL ||
1062 toRenderGL = !initRaytraceResources (aCtx) ||
1063 !updateRaytraceGeometry (OpenGl_GUM_CHECK, myId, aCtx);
1065 toRenderGL |= !myIsRaytraceDataValid; // if no ray-trace data use OpenGL
1069 const Standard_Integer aSizeX = theReadDrawFbo != NULL ? theReadDrawFbo->GetVPSizeX() : myWindow->Width();
1070 const Standard_Integer aSizeY = theReadDrawFbo != NULL ? theReadDrawFbo->GetVPSizeY() : myWindow->Height();
1071 myOpenGlFBO ->InitLazy (aCtx, aSizeX, aSizeY, myFboColorFormat, myFboDepthFormat, 0);
1073 if (myRaytraceFilter.IsNull())
1074 myRaytraceFilter = new OpenGl_RaytraceFilter;
1076 myRaytraceFilter->SetPrevRenderFilter (myWorkspace->GetRenderFilter());
1078 if (theReadDrawFbo != NULL)
1079 theReadDrawFbo->UnbindBuffer (aCtx);
1081 // Prepare preliminary OpenGL output
1082 if (aCtx->arbFBOBlit != NULL)
1084 // Render bottom OSD layer
1085 myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_Bottom, theReadDrawFbo, theOitAccumFbo);
1087 myWorkspace->SetRenderFilter (myRaytraceFilter);
1089 if (theReadDrawFbo != NULL)
1091 theReadDrawFbo->BindDrawBuffer (aCtx);
1095 aCtx->arbFBO->glBindFramebuffer (GL_DRAW_FRAMEBUFFER, 0);
1098 // Render non-polygonal elements in default layer
1099 myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_Default, theReadDrawFbo, theOitAccumFbo);
1101 myWorkspace->SetRenderFilter (myRaytraceFilter->PrevRenderFilter());
1104 if (theReadDrawFbo != NULL)
1106 theReadDrawFbo->BindBuffer (aCtx);
1110 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, 0);
1113 // Reset OpenGl aspects state to default to avoid enabling of
1114 // backface culling which is not supported in ray-tracing.
1115 myWorkspace->ResetAppliedAspect();
1117 // Ray-tracing polygonal primitive arrays
1118 raytrace (aSizeX, aSizeY, theProjection, theReadDrawFbo, aCtx);
1120 // Render upper (top and topmost) OpenGL layers
1121 myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_Upper, theReadDrawFbo, theOitAccumFbo);
1125 // Redraw 3D scene using OpenGL in standard
1126 // mode or in case of ray-tracing failure
1129 myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_All, theReadDrawFbo, theOitAccumFbo);
1131 // Set flag that scene was redrawn by standard pipeline
1132 myWasRedrawnGL = Standard_True;
1136 //=======================================================================
1137 //function : renderTrihedron
1139 //=======================================================================
1140 void OpenGl_View::renderTrihedron (const Handle(OpenGl_Workspace) &theWorkspace)
1142 if (myToShowGradTrihedron)
1144 myGraduatedTrihedron.Render (theWorkspace);
1148 // =======================================================================
1149 // function : Invalidate
1151 // =======================================================================
1152 void OpenGl_View::Invalidate()
1154 myBackBufferRestored = Standard_False;
1157 //=======================================================================
1158 //function : renderScene
1160 //=======================================================================
1161 void OpenGl_View::renderScene (Graphic3d_Camera::Projection theProjection,
1162 OpenGl_FrameBuffer* theReadDrawFbo,
1163 OpenGl_FrameBuffer* theOitAccumFbo,
1164 const Standard_Boolean theToDrawImmediate)
1166 const Handle(OpenGl_Context)& aContext = myWorkspace->GetGlContext();
1168 // Specify clipping planes in view transformation space
1169 aContext->ChangeClipping().Reset (aContext, myClipPlanes);
1170 if (!myClipPlanes.IsNull()
1171 && !myClipPlanes->IsEmpty())
1173 aContext->ShaderManager()->UpdateClippingState();
1176 renderStructs (theProjection, theReadDrawFbo, theOitAccumFbo, theToDrawImmediate);
1177 aContext->BindTextures (Handle(OpenGl_TextureSet)());
1179 // Apply restored view matrix.
1180 aContext->ApplyWorldViewMatrix();
1182 aContext->ChangeClipping().Reset (aContext, Handle(Graphic3d_SequenceOfHClipPlane)());
1183 if (!myClipPlanes.IsNull()
1184 && !myClipPlanes->IsEmpty())
1186 aContext->ShaderManager()->RevertClippingState();
1190 // =======================================================================
1191 // function : bindDefaultFbo
1193 // =======================================================================
1194 void OpenGl_View::bindDefaultFbo (OpenGl_FrameBuffer* theCustomFbo)
1196 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
1197 OpenGl_FrameBuffer* anFbo = (theCustomFbo != NULL && theCustomFbo->IsValid())
1199 : (!aCtx->DefaultFrameBuffer().IsNull()
1200 && aCtx->DefaultFrameBuffer()->IsValid()
1201 ? aCtx->DefaultFrameBuffer().operator->()
1205 anFbo->BindBuffer (aCtx);
1206 anFbo->SetupViewport (aCtx);
1210 #if !defined(GL_ES_VERSION_2_0)
1211 aCtx->SetReadDrawBuffer (GL_BACK);
1213 if (aCtx->arbFBO != NULL)
1215 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
1218 const Standard_Integer aViewport[4] = { 0, 0, myWindow->Width(), myWindow->Height() };
1219 aCtx->ResizeViewport (aViewport);
1223 // =======================================================================
1224 // function : initBlitQuad
1226 // =======================================================================
1227 OpenGl_VertexBuffer* OpenGl_View::initBlitQuad (const Standard_Boolean theToFlip)
1229 OpenGl_VertexBuffer* aVerts = NULL;
1232 aVerts = &myFullScreenQuad;
1233 if (!aVerts->IsValid())
1235 OpenGl_Vec4 aQuad[4] =
1237 OpenGl_Vec4( 1.0f, -1.0f, 1.0f, 0.0f),
1238 OpenGl_Vec4( 1.0f, 1.0f, 1.0f, 1.0f),
1239 OpenGl_Vec4(-1.0f, -1.0f, 0.0f, 0.0f),
1240 OpenGl_Vec4(-1.0f, 1.0f, 0.0f, 1.0f)
1242 aVerts->Init (myWorkspace->GetGlContext(), 4, 4, aQuad[0].GetData());
1247 aVerts = &myFullScreenQuadFlip;
1248 if (!aVerts->IsValid())
1250 OpenGl_Vec4 aQuad[4] =
1252 OpenGl_Vec4( 1.0f, -1.0f, 1.0f, 1.0f),
1253 OpenGl_Vec4( 1.0f, 1.0f, 1.0f, 0.0f),
1254 OpenGl_Vec4(-1.0f, -1.0f, 0.0f, 1.0f),
1255 OpenGl_Vec4(-1.0f, 1.0f, 0.0f, 0.0f)
1257 aVerts->Init (myWorkspace->GetGlContext(), 4, 4, aQuad[0].GetData());
1263 // =======================================================================
1264 // function : blitBuffers
1266 // =======================================================================
1267 bool OpenGl_View::blitBuffers (OpenGl_FrameBuffer* theReadFbo,
1268 OpenGl_FrameBuffer* theDrawFbo,
1269 const Standard_Boolean theToFlip)
1271 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
1272 const Standard_Integer aReadSizeX = theReadFbo != NULL ? theReadFbo->GetVPSizeX() : myWindow->Width();
1273 const Standard_Integer aReadSizeY = theReadFbo != NULL ? theReadFbo->GetVPSizeY() : myWindow->Height();
1274 const Standard_Integer aDrawSizeX = theDrawFbo != NULL ? theDrawFbo->GetVPSizeX() : myWindow->Width();
1275 const Standard_Integer aDrawSizeY = theDrawFbo != NULL ? theDrawFbo->GetVPSizeY() : myWindow->Height();
1276 if (theReadFbo == NULL || aCtx->IsFeedback())
1280 else if (theReadFbo == theDrawFbo)
1285 // clear destination before blitting
1286 if (theDrawFbo != NULL
1287 && theDrawFbo->IsValid())
1289 theDrawFbo->BindBuffer (aCtx);
1293 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
1295 const Standard_Integer aViewport[4] = { 0, 0, aDrawSizeX, aDrawSizeY };
1296 aCtx->ResizeViewport (aViewport);
1298 #if !defined(GL_ES_VERSION_2_0)
1299 aCtx->core20fwd->glClearDepth (1.0);
1301 aCtx->core20fwd->glClearDepthf (1.0f);
1303 aCtx->core20fwd->glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
1305 if (aCtx->arbFBOBlit != NULL
1306 && theReadFbo->NbSamples() != 0)
1308 GLbitfield aCopyMask = 0;
1309 theReadFbo->BindReadBuffer (aCtx);
1310 if (theDrawFbo != NULL
1311 && theDrawFbo->IsValid())
1313 theDrawFbo->BindDrawBuffer (aCtx);
1314 if (theDrawFbo->HasColor()
1315 && theReadFbo->HasColor())
1317 aCopyMask |= GL_COLOR_BUFFER_BIT;
1319 if (theDrawFbo->HasDepth()
1320 && theReadFbo->HasDepth())
1322 aCopyMask |= GL_DEPTH_BUFFER_BIT;
1327 if (theReadFbo->HasColor())
1329 aCopyMask |= GL_COLOR_BUFFER_BIT;
1331 if (theReadFbo->HasDepth())
1333 aCopyMask |= GL_DEPTH_BUFFER_BIT;
1335 aCtx->arbFBO->glBindFramebuffer (GL_DRAW_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
1338 // we don't copy stencil buffer here... does it matter for performance?
1339 aCtx->arbFBOBlit->glBlitFramebuffer (0, 0, aReadSizeX, aReadSizeY,
1340 0, 0, aDrawSizeX, aDrawSizeY,
1341 aCopyMask, GL_NEAREST);
1342 const int anErr = ::glGetError();
1343 if (anErr != GL_NO_ERROR)
1345 // glBlitFramebuffer() might fail in several cases:
1346 // - Both FBOs have MSAA and they are samples number does not match.
1347 // OCCT checks that this does not happen,
1348 // however some graphics drivers provide an option for overriding MSAA.
1349 // In this case window MSAA might be non-zero (and application can not check it)
1350 // and might not match MSAA of our offscreen FBOs.
1351 // - Pixel formats of FBOs do not match.
1352 // This also might happen with window has pixel format,
1353 // e.g. Mesa fails blitting RGBA8 -> RGB8 while other drivers support this conversion.
1354 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "FBO blitting has failed [Error #" + anErr + "]\n"
1355 + " Please check your graphics driver settings or try updating driver.";
1356 if (theReadFbo->NbSamples() != 0)
1358 myToDisableMSAA = true;
1359 aMsg += "\n MSAA settings should not be overridden by driver!";
1361 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1362 GL_DEBUG_TYPE_ERROR,
1364 GL_DEBUG_SEVERITY_HIGH,
1368 if (theDrawFbo != NULL
1369 && theDrawFbo->IsValid())
1371 theDrawFbo->BindBuffer (aCtx);
1375 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
1380 aCtx->core20fwd->glDepthFunc (GL_ALWAYS);
1381 aCtx->core20fwd->glDepthMask (GL_TRUE);
1382 aCtx->core20fwd->glEnable (GL_DEPTH_TEST);
1383 #if defined(GL_ES_VERSION_2_0)
1384 if (!aCtx->IsGlGreaterEqual (3, 0)
1385 && !aCtx->extFragDepth)
1387 aCtx->core20fwd->glDisable (GL_DEPTH_TEST);
1391 aCtx->BindTextures (Handle(OpenGl_TextureSet)());
1393 const Graphic3d_TypeOfTextureFilter aFilter = (aDrawSizeX == aReadSizeX && aDrawSizeY == aReadSizeY) ? Graphic3d_TOTF_NEAREST : Graphic3d_TOTF_BILINEAR;
1394 const GLint aFilterGl = aFilter == Graphic3d_TOTF_NEAREST ? GL_NEAREST : GL_LINEAR;
1396 OpenGl_VertexBuffer* aVerts = initBlitQuad (theToFlip);
1397 const Handle(OpenGl_ShaderManager)& aManager = aCtx->ShaderManager();
1398 if (aVerts->IsValid()
1399 && aManager->BindFboBlitProgram())
1401 theReadFbo->ColorTexture()->Bind (aCtx, Graphic3d_TextureUnit_0);
1402 if (theReadFbo->ColorTexture()->Sampler()->Parameters()->Filter() != aFilter)
1404 theReadFbo->ColorTexture()->Sampler()->Parameters()->SetFilter (aFilter);
1405 aCtx->core20fwd->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, aFilterGl);
1406 aCtx->core20fwd->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, aFilterGl);
1409 theReadFbo->DepthStencilTexture()->Bind (aCtx, Graphic3d_TextureUnit_1);
1410 if (theReadFbo->DepthStencilTexture()->Sampler()->Parameters()->Filter() != aFilter)
1412 theReadFbo->DepthStencilTexture()->Sampler()->Parameters()->SetFilter (aFilter);
1413 aCtx->core20fwd->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, aFilterGl);
1414 aCtx->core20fwd->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, aFilterGl);
1417 aVerts->BindVertexAttrib (aCtx, Graphic3d_TOA_POS);
1419 aCtx->core20fwd->glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
1421 aVerts->UnbindVertexAttrib (aCtx, Graphic3d_TOA_POS);
1422 theReadFbo->DepthStencilTexture()->Unbind (aCtx, Graphic3d_TextureUnit_1);
1423 theReadFbo->ColorTexture() ->Unbind (aCtx, Graphic3d_TextureUnit_0);
1424 aCtx->BindProgram (NULL);
1428 TCollection_ExtendedString aMsg = TCollection_ExtendedString()
1429 + "Error! FBO blitting has failed";
1430 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1431 GL_DEBUG_TYPE_ERROR,
1433 GL_DEBUG_SEVERITY_HIGH,
1435 myHasFboBlit = Standard_False;
1436 theReadFbo->Release (aCtx.operator->());
1443 // =======================================================================
1444 // function : drawStereoPair
1446 // =======================================================================
1447 void OpenGl_View::drawStereoPair (OpenGl_FrameBuffer* theDrawFbo)
1449 const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext();
1450 bindDefaultFbo (theDrawFbo);
1451 OpenGl_FrameBuffer* aPair[2] =
1453 myImmediateSceneFbos[0]->IsValid() ? myImmediateSceneFbos[0].operator->() : NULL,
1454 myImmediateSceneFbos[1]->IsValid() ? myImmediateSceneFbos[1].operator->() : NULL
1456 if (aPair[0] == NULL
1458 || !myTransientDrawToFront)
1460 aPair[0] = myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL;
1461 aPair[1] = myMainSceneFbos[1]->IsValid() ? myMainSceneFbos[1].operator->() : NULL;
1464 if (aPair[0] == NULL
1465 || aPair[1] == NULL)
1470 if (aPair[0]->NbSamples() != 0)
1472 // resolve MSAA buffers before drawing
1473 if (!myOpenGlFBO ->InitLazy (aCtx, aPair[0]->GetVPSizeX(), aPair[0]->GetVPSizeY(), myFboColorFormat, myFboDepthFormat, 0)
1474 || !myOpenGlFBO2->InitLazy (aCtx, aPair[0]->GetVPSizeX(), aPair[0]->GetVPSizeY(), myFboColorFormat, 0, 0))
1476 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1477 GL_DEBUG_TYPE_ERROR,
1479 GL_DEBUG_SEVERITY_HIGH,
1480 "Error! Unable to allocate FBO for blitting stereo pair");
1481 bindDefaultFbo (theDrawFbo);
1485 if (!blitBuffers (aPair[0], myOpenGlFBO .operator->(), Standard_False)
1486 || !blitBuffers (aPair[1], myOpenGlFBO2.operator->(), Standard_False))
1488 bindDefaultFbo (theDrawFbo);
1492 aPair[0] = myOpenGlFBO .operator->();
1493 aPair[1] = myOpenGlFBO2.operator->();
1494 bindDefaultFbo (theDrawFbo);
1499 Standard_Integer left;
1500 Standard_Integer top;
1501 Standard_Integer right;
1502 Standard_Integer bottom;
1503 Standard_Integer dx() { return right - left; }
1504 Standard_Integer dy() { return bottom - top; }
1507 myWindow->PlatformWindow()->Position (aGeom.left, aGeom.top, aGeom.right, aGeom.bottom);
1509 Standard_Boolean toReverse = myRenderParams.ToReverseStereo;
1510 const Standard_Boolean isOddY = (aGeom.top + aGeom.dy()) % 2 == 1;
1511 const Standard_Boolean isOddX = aGeom.left % 2 == 1;
1513 && (myRenderParams.StereoMode == Graphic3d_StereoMode_RowInterlaced
1514 || myRenderParams.StereoMode == Graphic3d_StereoMode_ChessBoard))
1516 toReverse = !toReverse;
1519 && (myRenderParams.StereoMode == Graphic3d_StereoMode_ColumnInterlaced
1520 || myRenderParams.StereoMode == Graphic3d_StereoMode_ChessBoard))
1522 toReverse = !toReverse;
1527 std::swap (aPair[0], aPair[1]);
1530 aCtx->core20fwd->glDepthFunc (GL_ALWAYS);
1531 aCtx->core20fwd->glDepthMask (GL_TRUE);
1532 aCtx->core20fwd->glEnable (GL_DEPTH_TEST);
1534 aCtx->BindTextures (Handle(OpenGl_TextureSet)());
1535 OpenGl_VertexBuffer* aVerts = initBlitQuad (myToFlipOutput);
1537 const Handle(OpenGl_ShaderManager)& aManager = aCtx->ShaderManager();
1538 if (aVerts->IsValid()
1539 && aManager->BindStereoProgram (myRenderParams.StereoMode))
1541 if (myRenderParams.StereoMode == Graphic3d_StereoMode_Anaglyph)
1543 OpenGl_Mat4 aFilterL, aFilterR;
1544 aFilterL.SetDiagonal (Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f));
1545 aFilterR.SetDiagonal (Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f));
1546 switch (myRenderParams.AnaglyphFilter)
1548 case Graphic3d_RenderingParams::Anaglyph_RedCyan_Simple:
1550 aFilterL.SetRow (0, Graphic3d_Vec4 (1.0f, 0.0f, 0.0f, 0.0f));
1551 aFilterR.SetRow (1, Graphic3d_Vec4 (0.0f, 1.0f, 0.0f, 0.0f));
1552 aFilterR.SetRow (2, Graphic3d_Vec4 (0.0f, 0.0f, 1.0f, 0.0f));
1555 case Graphic3d_RenderingParams::Anaglyph_RedCyan_Optimized:
1557 aFilterL.SetRow (0, Graphic3d_Vec4 ( 0.4154f, 0.4710f, 0.16666667f, 0.0f));
1558 aFilterL.SetRow (1, Graphic3d_Vec4 (-0.0458f, -0.0484f, -0.0257f, 0.0f));
1559 aFilterL.SetRow (2, Graphic3d_Vec4 (-0.0547f, -0.0615f, 0.0128f, 0.0f));
1560 aFilterL.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f));
1561 aFilterR.SetRow (0, Graphic3d_Vec4 (-0.01090909f, -0.03636364f, -0.00606061f, 0.0f));
1562 aFilterR.SetRow (1, Graphic3d_Vec4 ( 0.37560000f, 0.73333333f, 0.01111111f, 0.0f));
1563 aFilterR.SetRow (2, Graphic3d_Vec4 (-0.06510000f, -0.12870000f, 1.29710000f, 0.0f));
1564 aFilterR.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f));
1567 case Graphic3d_RenderingParams::Anaglyph_YellowBlue_Simple:
1569 aFilterL.SetRow (0, Graphic3d_Vec4 (1.0f, 0.0f, 0.0f, 0.0f));
1570 aFilterL.SetRow (1, Graphic3d_Vec4 (0.0f, 1.0f, 0.0f, 0.0f));
1571 aFilterR.SetRow (2, Graphic3d_Vec4 (0.0f, 0.0f, 1.0f, 0.0f));
1574 case Graphic3d_RenderingParams::Anaglyph_YellowBlue_Optimized:
1576 aFilterL.SetRow (0, Graphic3d_Vec4 ( 1.062f, -0.205f, 0.299f, 0.0f));
1577 aFilterL.SetRow (1, Graphic3d_Vec4 (-0.026f, 0.908f, 0.068f, 0.0f));
1578 aFilterL.SetRow (2, Graphic3d_Vec4 (-0.038f, -0.173f, 0.022f, 0.0f));
1579 aFilterL.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f));
1580 aFilterR.SetRow (0, Graphic3d_Vec4 (-0.016f, -0.123f, -0.017f, 0.0f));
1581 aFilterR.SetRow (1, Graphic3d_Vec4 ( 0.006f, 0.062f, -0.017f, 0.0f));
1582 aFilterR.SetRow (2, Graphic3d_Vec4 ( 0.094f, 0.185f, 0.911f, 0.0f));
1583 aFilterR.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f));
1586 case Graphic3d_RenderingParams::Anaglyph_GreenMagenta_Simple:
1588 aFilterR.SetRow (0, Graphic3d_Vec4 (1.0f, 0.0f, 0.0f, 0.0f));
1589 aFilterL.SetRow (1, Graphic3d_Vec4 (0.0f, 1.0f, 0.0f, 0.0f));
1590 aFilterR.SetRow (2, Graphic3d_Vec4 (0.0f, 0.0f, 1.0f, 0.0f));
1593 case Graphic3d_RenderingParams::Anaglyph_UserDefined:
1595 aFilterL = myRenderParams.AnaglyphLeft;
1596 aFilterR = myRenderParams.AnaglyphRight;
1600 aCtx->ActiveProgram()->SetUniform (aCtx, "uMultL", aFilterL);
1601 aCtx->ActiveProgram()->SetUniform (aCtx, "uMultR", aFilterR);
1604 aPair[0]->ColorTexture()->Bind (aCtx, Graphic3d_TextureUnit_0);
1605 aPair[1]->ColorTexture()->Bind (aCtx, Graphic3d_TextureUnit_1);
1606 aVerts->BindVertexAttrib (aCtx, 0);
1608 aCtx->core20fwd->glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
1610 aVerts->UnbindVertexAttrib (aCtx, 0);
1611 aPair[1]->ColorTexture()->Unbind (aCtx, Graphic3d_TextureUnit_1);
1612 aPair[0]->ColorTexture()->Unbind (aCtx, Graphic3d_TextureUnit_0);
1616 TCollection_ExtendedString aMsg = TCollection_ExtendedString()
1617 + "Error! Anaglyph has failed";
1618 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1619 GL_DEBUG_TYPE_ERROR,
1621 GL_DEBUG_SEVERITY_HIGH,
1626 // =======================================================================
1627 // function : copyBackToFront
1629 // =======================================================================
1630 bool OpenGl_View::copyBackToFront()
1632 myIsImmediateDrawn = Standard_False;
1633 #if !defined(GL_ES_VERSION_2_0)
1634 const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext();
1635 if (aCtx->core11 == NULL)
1640 OpenGl_Mat4 aProjectMat;
1641 Graphic3d_TransformUtils::Ortho2D (aProjectMat,
1642 0.0f, static_cast<GLfloat> (myWindow->Width()),
1643 0.0f, static_cast<GLfloat> (myWindow->Height()));
1645 aCtx->WorldViewState.Push();
1646 aCtx->ProjectionState.Push();
1648 aCtx->WorldViewState.SetIdentity();
1649 aCtx->ProjectionState.SetCurrent (aProjectMat);
1651 aCtx->ApplyProjectionMatrix();
1652 aCtx->ApplyWorldViewMatrix();
1654 // synchronize FFP state before copying pixels
1655 aCtx->BindProgram (Handle(OpenGl_ShaderProgram)());
1656 aCtx->ShaderManager()->PushState (Handle(OpenGl_ShaderProgram)());
1657 aCtx->DisableFeatures();
1659 switch (aCtx->DrawBuffer())
1663 aCtx->SetReadBuffer (GL_BACK_LEFT);
1664 aCtx->SetDrawBuffer (GL_FRONT_LEFT);
1669 aCtx->SetReadBuffer (GL_BACK_RIGHT);
1670 aCtx->SetDrawBuffer (GL_FRONT_RIGHT);
1675 aCtx->SetReadBuffer (GL_BACK);
1676 aCtx->SetDrawBuffer (GL_FRONT);
1681 aCtx->core11->glRasterPos2i (0, 0);
1682 aCtx->core11->glCopyPixels (0, 0, myWindow->Width() + 1, myWindow->Height() + 1, GL_COLOR);
1683 //aCtx->core11->glCopyPixels (0, 0, myWidth + 1, myHeight + 1, GL_DEPTH);
1685 aCtx->EnableFeatures();
1687 aCtx->WorldViewState.Pop();
1688 aCtx->ProjectionState.Pop();
1689 aCtx->ApplyProjectionMatrix();
1691 // read/write from front buffer now
1692 aCtx->SetReadBuffer (aCtx->DrawBuffer());
1699 // =======================================================================
1700 // function : checkOitCompatibility
1702 // =======================================================================
1703 Standard_Boolean OpenGl_View::checkOitCompatibility (const Handle(OpenGl_Context)& theGlContext,
1704 const Standard_Boolean theMSAA)
1706 // determine if OIT is supported by current OpenGl context
1707 Standard_Boolean& aToDisableOIT = theMSAA ? myToDisableMSAA : myToDisableOIT;
1710 return Standard_False;
1713 TCollection_ExtendedString aCompatibilityMsg;
1714 if (theGlContext->hasFloatBuffer == OpenGl_FeatureNotAvailable
1715 && theGlContext->hasHalfFloatBuffer == OpenGl_FeatureNotAvailable)
1717 aCompatibilityMsg += "OpenGL context does not support floating-point RGBA color buffer format.\n";
1719 if (theMSAA && theGlContext->hasSampleVariables == OpenGl_FeatureNotAvailable)
1721 aCompatibilityMsg += "Current version of GLSL does not support built-in sample variables.\n";
1723 if (theGlContext->hasDrawBuffers == OpenGl_FeatureNotAvailable)
1725 aCompatibilityMsg += "OpenGL context does not support multiple draw buffers.\n";
1727 if (aCompatibilityMsg.IsEmpty())
1729 return Standard_True;
1732 aCompatibilityMsg += " Blended order-independent transparency will not be available.\n";
1733 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1734 GL_DEBUG_TYPE_ERROR,
1736 GL_DEBUG_SEVERITY_HIGH,
1739 aToDisableOIT = Standard_True;
1740 return Standard_False;
1743 // =======================================================================
1744 // function : chooseOitColorConfiguration
1746 // =======================================================================
1747 bool OpenGl_View::chooseOitColorConfiguration (const Handle(OpenGl_Context)& theGlContext,
1748 const Standard_Integer theConfigIndex,
1749 OpenGl_ColorFormats& theFormats)
1752 switch (theConfigIndex)
1754 case 0: // choose best applicable color format combination
1756 theFormats.Append (theGlContext->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable ? GL_RGBA16F : GL_RGBA32F);
1757 theFormats.Append (theGlContext->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable ? GL_R16F : GL_R32F);
1760 case 1: // choose non-optimal applicable color format combination
1762 theFormats.Append (theGlContext->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable ? GL_RGBA16F : GL_RGBA32F);
1763 theFormats.Append (theGlContext->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable ? GL_RGBA16F : GL_RGBA32F);
1767 return false; // color combination does not exist