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 // note that we pass here window dimensions ignoring Graphic3d_RenderingParams::RenderResolutionScale
882 myBVHSelector.SetViewVolume (myCamera);
883 myBVHSelector.SetViewportSize (myWindow->Width(), myWindow->Height(), myRenderParams.ResolutionRatio());
884 myBVHSelector.CacheClipPtsProjections();
886 const Handle(OpenGl_ShaderManager)& aManager = aContext->ShaderManager();
887 if (StateInfo (myCurrLightSourceState, aManager->LightSourceState().Index()) != myLastLightSourceState)
889 aManager->UpdateLightSourceStateTo (myShadingModel == Graphic3d_TOSM_NONE ? &myNoShadingLight : &myLights);
890 myLastLightSourceState = StateInfo (myCurrLightSourceState, aManager->LightSourceState().Index());
893 // Update matrices if camera has changed.
894 Graphic3d_WorldViewProjState aWVPState = myCamera->WorldViewProjState();
895 if (myWorldViewProjState != aWVPState)
898 myWorldViewProjState = aWVPState;
901 myLocalOrigin.SetCoord (0.0, 0.0, 0.0);
902 aContext->ProjectionState.SetCurrent (myCamera->ProjectionMatrixF());
903 aContext->WorldViewState .SetCurrent (myCamera->OrientationMatrixF());
904 aContext->ApplyProjectionMatrix();
905 aContext->ApplyWorldViewMatrix();
906 if (aManager->ModelWorldState().Index() == 0)
908 aContext->ShaderManager()->UpdateModelWorldStateTo (OpenGl_Mat4());
911 // ====================================
912 // Step 2: Redraw background
913 // ====================================
916 if (!theToDrawImmediate)
918 drawBackground (myWorkspace);
921 #if !defined(GL_ES_VERSION_2_0)
922 // Switch off lighting by default
923 if (aContext->core11 != NULL
924 && aContext->caps->ffpEnable)
926 glDisable(GL_LIGHTING);
930 // =================================
931 // Step 3: Redraw main plane
932 // =================================
934 // Setup face culling
935 GLboolean isCullFace = GL_FALSE;
936 if (myBackfacing != Graphic3d_TOBM_AUTOMATIC)
938 isCullFace = glIsEnabled (GL_CULL_FACE);
939 if (myBackfacing == Graphic3d_TOBM_DISABLE)
941 glEnable (GL_CULL_FACE);
942 glCullFace (GL_BACK);
945 glDisable (GL_CULL_FACE);
948 #if !defined(GL_ES_VERSION_2_0)
949 // if the view is scaled normal vectors are scaled to unit
950 // length for correct displaying of shaded objects
951 const gp_Pnt anAxialScale = myCamera->AxialScale();
952 if (anAxialScale.X() != 1.F ||
953 anAxialScale.Y() != 1.F ||
954 anAxialScale.Z() != 1.F)
956 aContext->SetGlNormalizeEnabled (Standard_True);
960 aContext->SetGlNormalizeEnabled (Standard_False);
963 // Apply InteriorShadingMethod
964 if (aContext->core11 != NULL)
966 aContext->core11->glShadeModel (myShadingModel == Graphic3d_TOSM_FACET
967 || myShadingModel == Graphic3d_TOSM_NONE ? GL_FLAT : GL_SMOOTH);
971 aManager->SetShadingModel (myShadingModel);
974 if (theProjection == Graphic3d_Camera::Projection_MonoLeftEye)
976 aContext->ProjectionState.SetCurrent (myCamera->ProjectionStereoLeftF());
977 aContext->ApplyProjectionMatrix();
979 else if (theProjection == Graphic3d_Camera::Projection_MonoRightEye)
981 aContext->ProjectionState.SetCurrent (myCamera->ProjectionStereoRightF());
982 aContext->ApplyProjectionMatrix();
985 myWorkspace->SetEnvironmentTexture (myTextureEnv);
987 renderScene (theProjection, theOutputFBO, theOitAccumFbo, theToDrawImmediate);
989 myWorkspace->SetEnvironmentTexture (Handle(OpenGl_TextureSet)());
991 // ===============================
993 // ===============================
995 // Resetting GL parameters according to the default aspects
996 // in order to synchronize GL state with the graphic driver state
997 // before drawing auxiliary stuff (trihedrons, overlayer)
998 myWorkspace->ResetAppliedAspect();
1001 if (!theToDrawImmediate)
1003 renderTrihedron (myWorkspace);
1005 // Restore face culling
1006 if (myBackfacing != Graphic3d_TOBM_AUTOMATIC)
1010 glEnable (GL_CULL_FACE);
1011 glCullFace (GL_BACK);
1014 glDisable (GL_CULL_FACE);
1018 // reset FFP state for safety
1019 aContext->BindProgram (Handle(OpenGl_ShaderProgram)());
1020 if (aContext->caps->ffpEnable)
1022 aContext->ShaderManager()->PushState (Handle(OpenGl_ShaderProgram)());
1025 // ==============================================================
1026 // Step 6: Keep shader manager informed about last View
1027 // ==============================================================
1029 if (!aManager.IsNull())
1031 aManager->SetLastView (this);
1035 // =======================================================================
1036 // function : InvalidateBVHData
1038 // =======================================================================
1039 void OpenGl_View::InvalidateBVHData (const Graphic3d_ZLayerId theLayerId)
1041 myZLayers.InvalidateBVHData (theLayerId);
1044 //=======================================================================
1045 //function : renderStructs
1047 //=======================================================================
1048 void OpenGl_View::renderStructs (Graphic3d_Camera::Projection theProjection,
1049 OpenGl_FrameBuffer* theReadDrawFbo,
1050 OpenGl_FrameBuffer* theOitAccumFbo,
1051 const Standard_Boolean theToDrawImmediate)
1053 if ( myZLayers.NbStructures() <= 0 )
1056 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
1057 Standard_Boolean toRenderGL = theToDrawImmediate ||
1058 myRenderParams.Method != Graphic3d_RM_RAYTRACING ||
1059 myRaytraceInitStatus == OpenGl_RT_FAIL ||
1062 myZLayers.UpdateCulling (myWorkspace, theToDrawImmediate);
1066 toRenderGL = !initRaytraceResources (aCtx) ||
1067 !updateRaytraceGeometry (OpenGl_GUM_CHECK, myId, aCtx);
1069 toRenderGL |= !myIsRaytraceDataValid; // if no ray-trace data use OpenGL
1073 const Standard_Integer aSizeX = theReadDrawFbo != NULL ? theReadDrawFbo->GetVPSizeX() : myWindow->Width();
1074 const Standard_Integer aSizeY = theReadDrawFbo != NULL ? theReadDrawFbo->GetVPSizeY() : myWindow->Height();
1075 myOpenGlFBO ->InitLazy (aCtx, aSizeX, aSizeY, myFboColorFormat, myFboDepthFormat, 0);
1077 if (myRaytraceFilter.IsNull())
1078 myRaytraceFilter = new OpenGl_RaytraceFilter;
1080 myRaytraceFilter->SetPrevRenderFilter (myWorkspace->GetRenderFilter());
1082 if (theReadDrawFbo != NULL)
1083 theReadDrawFbo->UnbindBuffer (aCtx);
1085 // Prepare preliminary OpenGL output
1086 if (aCtx->arbFBOBlit != NULL)
1088 // Render bottom OSD layer
1089 myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_Bottom, theReadDrawFbo, theOitAccumFbo);
1091 myWorkspace->SetRenderFilter (myRaytraceFilter);
1093 if (theReadDrawFbo != NULL)
1095 theReadDrawFbo->BindDrawBuffer (aCtx);
1099 aCtx->arbFBO->glBindFramebuffer (GL_DRAW_FRAMEBUFFER, 0);
1102 // Render non-polygonal elements in default layer
1103 myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_Default, theReadDrawFbo, theOitAccumFbo);
1105 myWorkspace->SetRenderFilter (myRaytraceFilter->PrevRenderFilter());
1108 if (theReadDrawFbo != NULL)
1110 theReadDrawFbo->BindBuffer (aCtx);
1114 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, 0);
1117 // Reset OpenGl aspects state to default to avoid enabling of
1118 // backface culling which is not supported in ray-tracing.
1119 myWorkspace->ResetAppliedAspect();
1121 // Ray-tracing polygonal primitive arrays
1122 raytrace (aSizeX, aSizeY, theProjection, theReadDrawFbo, aCtx);
1124 // Render upper (top and topmost) OpenGL layers
1125 myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_Upper, theReadDrawFbo, theOitAccumFbo);
1129 // Redraw 3D scene using OpenGL in standard
1130 // mode or in case of ray-tracing failure
1133 myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_All, theReadDrawFbo, theOitAccumFbo);
1135 // Set flag that scene was redrawn by standard pipeline
1136 myWasRedrawnGL = Standard_True;
1140 //=======================================================================
1141 //function : renderTrihedron
1143 //=======================================================================
1144 void OpenGl_View::renderTrihedron (const Handle(OpenGl_Workspace) &theWorkspace)
1146 if (myToShowGradTrihedron)
1148 myGraduatedTrihedron.Render (theWorkspace);
1152 // =======================================================================
1153 // function : Invalidate
1155 // =======================================================================
1156 void OpenGl_View::Invalidate()
1158 myBackBufferRestored = Standard_False;
1161 //=======================================================================
1162 //function : renderScene
1164 //=======================================================================
1165 void OpenGl_View::renderScene (Graphic3d_Camera::Projection theProjection,
1166 OpenGl_FrameBuffer* theReadDrawFbo,
1167 OpenGl_FrameBuffer* theOitAccumFbo,
1168 const Standard_Boolean theToDrawImmediate)
1170 const Handle(OpenGl_Context)& aContext = myWorkspace->GetGlContext();
1172 // Specify clipping planes in view transformation space
1173 aContext->ChangeClipping().Reset (aContext, myClipPlanes);
1174 if (!myClipPlanes.IsNull()
1175 && !myClipPlanes->IsEmpty())
1177 aContext->ShaderManager()->UpdateClippingState();
1180 renderStructs (theProjection, theReadDrawFbo, theOitAccumFbo, theToDrawImmediate);
1181 aContext->BindTextures (Handle(OpenGl_TextureSet)());
1183 // Apply restored view matrix.
1184 aContext->ApplyWorldViewMatrix();
1186 aContext->ChangeClipping().Reset (aContext, Handle(Graphic3d_SequenceOfHClipPlane)());
1187 if (!myClipPlanes.IsNull()
1188 && !myClipPlanes->IsEmpty())
1190 aContext->ShaderManager()->RevertClippingState();
1194 // =======================================================================
1195 // function : bindDefaultFbo
1197 // =======================================================================
1198 void OpenGl_View::bindDefaultFbo (OpenGl_FrameBuffer* theCustomFbo)
1200 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
1201 OpenGl_FrameBuffer* anFbo = (theCustomFbo != NULL && theCustomFbo->IsValid())
1203 : (!aCtx->DefaultFrameBuffer().IsNull()
1204 && aCtx->DefaultFrameBuffer()->IsValid()
1205 ? aCtx->DefaultFrameBuffer().operator->()
1209 anFbo->BindBuffer (aCtx);
1210 anFbo->SetupViewport (aCtx);
1214 #if !defined(GL_ES_VERSION_2_0)
1215 aCtx->SetReadDrawBuffer (GL_BACK);
1217 if (aCtx->arbFBO != NULL)
1219 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
1222 const Standard_Integer aViewport[4] = { 0, 0, myWindow->Width(), myWindow->Height() };
1223 aCtx->ResizeViewport (aViewport);
1227 // =======================================================================
1228 // function : initBlitQuad
1230 // =======================================================================
1231 OpenGl_VertexBuffer* OpenGl_View::initBlitQuad (const Standard_Boolean theToFlip)
1233 OpenGl_VertexBuffer* aVerts = NULL;
1236 aVerts = &myFullScreenQuad;
1237 if (!aVerts->IsValid())
1239 OpenGl_Vec4 aQuad[4] =
1241 OpenGl_Vec4( 1.0f, -1.0f, 1.0f, 0.0f),
1242 OpenGl_Vec4( 1.0f, 1.0f, 1.0f, 1.0f),
1243 OpenGl_Vec4(-1.0f, -1.0f, 0.0f, 0.0f),
1244 OpenGl_Vec4(-1.0f, 1.0f, 0.0f, 1.0f)
1246 aVerts->Init (myWorkspace->GetGlContext(), 4, 4, aQuad[0].GetData());
1251 aVerts = &myFullScreenQuadFlip;
1252 if (!aVerts->IsValid())
1254 OpenGl_Vec4 aQuad[4] =
1256 OpenGl_Vec4( 1.0f, -1.0f, 1.0f, 1.0f),
1257 OpenGl_Vec4( 1.0f, 1.0f, 1.0f, 0.0f),
1258 OpenGl_Vec4(-1.0f, -1.0f, 0.0f, 1.0f),
1259 OpenGl_Vec4(-1.0f, 1.0f, 0.0f, 0.0f)
1261 aVerts->Init (myWorkspace->GetGlContext(), 4, 4, aQuad[0].GetData());
1267 // =======================================================================
1268 // function : blitBuffers
1270 // =======================================================================
1271 bool OpenGl_View::blitBuffers (OpenGl_FrameBuffer* theReadFbo,
1272 OpenGl_FrameBuffer* theDrawFbo,
1273 const Standard_Boolean theToFlip)
1275 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
1276 const Standard_Integer aReadSizeX = theReadFbo != NULL ? theReadFbo->GetVPSizeX() : myWindow->Width();
1277 const Standard_Integer aReadSizeY = theReadFbo != NULL ? theReadFbo->GetVPSizeY() : myWindow->Height();
1278 const Standard_Integer aDrawSizeX = theDrawFbo != NULL ? theDrawFbo->GetVPSizeX() : myWindow->Width();
1279 const Standard_Integer aDrawSizeY = theDrawFbo != NULL ? theDrawFbo->GetVPSizeY() : myWindow->Height();
1280 if (theReadFbo == NULL || aCtx->IsFeedback())
1284 else if (theReadFbo == theDrawFbo)
1289 // clear destination before blitting
1290 if (theDrawFbo != NULL
1291 && theDrawFbo->IsValid())
1293 theDrawFbo->BindBuffer (aCtx);
1297 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
1299 const Standard_Integer aViewport[4] = { 0, 0, aDrawSizeX, aDrawSizeY };
1300 aCtx->ResizeViewport (aViewport);
1302 #if !defined(GL_ES_VERSION_2_0)
1303 aCtx->core20fwd->glClearDepth (1.0);
1305 aCtx->core20fwd->glClearDepthf (1.0f);
1307 aCtx->core20fwd->glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
1309 if (aCtx->arbFBOBlit != NULL
1310 && theReadFbo->NbSamples() != 0)
1312 GLbitfield aCopyMask = 0;
1313 theReadFbo->BindReadBuffer (aCtx);
1314 if (theDrawFbo != NULL
1315 && theDrawFbo->IsValid())
1317 theDrawFbo->BindDrawBuffer (aCtx);
1318 if (theDrawFbo->HasColor()
1319 && theReadFbo->HasColor())
1321 aCopyMask |= GL_COLOR_BUFFER_BIT;
1323 if (theDrawFbo->HasDepth()
1324 && theReadFbo->HasDepth())
1326 aCopyMask |= GL_DEPTH_BUFFER_BIT;
1331 if (theReadFbo->HasColor())
1333 aCopyMask |= GL_COLOR_BUFFER_BIT;
1335 if (theReadFbo->HasDepth())
1337 aCopyMask |= GL_DEPTH_BUFFER_BIT;
1339 aCtx->arbFBO->glBindFramebuffer (GL_DRAW_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
1342 // we don't copy stencil buffer here... does it matter for performance?
1343 aCtx->arbFBOBlit->glBlitFramebuffer (0, 0, aReadSizeX, aReadSizeY,
1344 0, 0, aDrawSizeX, aDrawSizeY,
1345 aCopyMask, GL_NEAREST);
1346 const int anErr = ::glGetError();
1347 if (anErr != GL_NO_ERROR)
1349 // glBlitFramebuffer() might fail in several cases:
1350 // - Both FBOs have MSAA and they are samples number does not match.
1351 // OCCT checks that this does not happen,
1352 // however some graphics drivers provide an option for overriding MSAA.
1353 // In this case window MSAA might be non-zero (and application can not check it)
1354 // and might not match MSAA of our offscreen FBOs.
1355 // - Pixel formats of FBOs do not match.
1356 // This also might happen with window has pixel format,
1357 // e.g. Mesa fails blitting RGBA8 -> RGB8 while other drivers support this conversion.
1358 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "FBO blitting has failed [Error #" + anErr + "]\n"
1359 + " Please check your graphics driver settings or try updating driver.";
1360 if (theReadFbo->NbSamples() != 0)
1362 myToDisableMSAA = true;
1363 aMsg += "\n MSAA settings should not be overridden by driver!";
1365 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1366 GL_DEBUG_TYPE_ERROR,
1368 GL_DEBUG_SEVERITY_HIGH,
1372 if (theDrawFbo != NULL
1373 && theDrawFbo->IsValid())
1375 theDrawFbo->BindBuffer (aCtx);
1379 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
1384 aCtx->core20fwd->glDepthFunc (GL_ALWAYS);
1385 aCtx->core20fwd->glDepthMask (GL_TRUE);
1386 aCtx->core20fwd->glEnable (GL_DEPTH_TEST);
1387 #if defined(GL_ES_VERSION_2_0)
1388 if (!aCtx->IsGlGreaterEqual (3, 0)
1389 && !aCtx->extFragDepth)
1391 aCtx->core20fwd->glDisable (GL_DEPTH_TEST);
1395 aCtx->BindTextures (Handle(OpenGl_TextureSet)());
1397 const Graphic3d_TypeOfTextureFilter aFilter = (aDrawSizeX == aReadSizeX && aDrawSizeY == aReadSizeY) ? Graphic3d_TOTF_NEAREST : Graphic3d_TOTF_BILINEAR;
1398 const GLint aFilterGl = aFilter == Graphic3d_TOTF_NEAREST ? GL_NEAREST : GL_LINEAR;
1400 OpenGl_VertexBuffer* aVerts = initBlitQuad (theToFlip);
1401 const Handle(OpenGl_ShaderManager)& aManager = aCtx->ShaderManager();
1402 if (aVerts->IsValid()
1403 && aManager->BindFboBlitProgram())
1405 theReadFbo->ColorTexture()->Bind (aCtx, Graphic3d_TextureUnit_0);
1406 if (theReadFbo->ColorTexture()->Sampler()->Parameters()->Filter() != aFilter)
1408 theReadFbo->ColorTexture()->Sampler()->Parameters()->SetFilter (aFilter);
1409 aCtx->core20fwd->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, aFilterGl);
1410 aCtx->core20fwd->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, aFilterGl);
1413 theReadFbo->DepthStencilTexture()->Bind (aCtx, Graphic3d_TextureUnit_1);
1414 if (theReadFbo->DepthStencilTexture()->Sampler()->Parameters()->Filter() != aFilter)
1416 theReadFbo->DepthStencilTexture()->Sampler()->Parameters()->SetFilter (aFilter);
1417 aCtx->core20fwd->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, aFilterGl);
1418 aCtx->core20fwd->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, aFilterGl);
1421 aVerts->BindVertexAttrib (aCtx, Graphic3d_TOA_POS);
1423 aCtx->core20fwd->glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
1425 aVerts->UnbindVertexAttrib (aCtx, Graphic3d_TOA_POS);
1426 theReadFbo->DepthStencilTexture()->Unbind (aCtx, Graphic3d_TextureUnit_1);
1427 theReadFbo->ColorTexture() ->Unbind (aCtx, Graphic3d_TextureUnit_0);
1428 aCtx->BindProgram (NULL);
1432 TCollection_ExtendedString aMsg = TCollection_ExtendedString()
1433 + "Error! FBO blitting has failed";
1434 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1435 GL_DEBUG_TYPE_ERROR,
1437 GL_DEBUG_SEVERITY_HIGH,
1439 myHasFboBlit = Standard_False;
1440 theReadFbo->Release (aCtx.operator->());
1447 // =======================================================================
1448 // function : drawStereoPair
1450 // =======================================================================
1451 void OpenGl_View::drawStereoPair (OpenGl_FrameBuffer* theDrawFbo)
1453 const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext();
1454 bindDefaultFbo (theDrawFbo);
1455 OpenGl_FrameBuffer* aPair[2] =
1457 myImmediateSceneFbos[0]->IsValid() ? myImmediateSceneFbos[0].operator->() : NULL,
1458 myImmediateSceneFbos[1]->IsValid() ? myImmediateSceneFbos[1].operator->() : NULL
1460 if (aPair[0] == NULL
1462 || !myTransientDrawToFront)
1464 aPair[0] = myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL;
1465 aPair[1] = myMainSceneFbos[1]->IsValid() ? myMainSceneFbos[1].operator->() : NULL;
1468 if (aPair[0] == NULL
1469 || aPair[1] == NULL)
1474 if (aPair[0]->NbSamples() != 0)
1476 // resolve MSAA buffers before drawing
1477 if (!myOpenGlFBO ->InitLazy (aCtx, aPair[0]->GetVPSizeX(), aPair[0]->GetVPSizeY(), myFboColorFormat, myFboDepthFormat, 0)
1478 || !myOpenGlFBO2->InitLazy (aCtx, aPair[0]->GetVPSizeX(), aPair[0]->GetVPSizeY(), myFboColorFormat, 0, 0))
1480 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1481 GL_DEBUG_TYPE_ERROR,
1483 GL_DEBUG_SEVERITY_HIGH,
1484 "Error! Unable to allocate FBO for blitting stereo pair");
1485 bindDefaultFbo (theDrawFbo);
1489 if (!blitBuffers (aPair[0], myOpenGlFBO .operator->(), Standard_False)
1490 || !blitBuffers (aPair[1], myOpenGlFBO2.operator->(), Standard_False))
1492 bindDefaultFbo (theDrawFbo);
1496 aPair[0] = myOpenGlFBO .operator->();
1497 aPair[1] = myOpenGlFBO2.operator->();
1498 bindDefaultFbo (theDrawFbo);
1503 Standard_Integer left;
1504 Standard_Integer top;
1505 Standard_Integer right;
1506 Standard_Integer bottom;
1507 Standard_Integer dx() { return right - left; }
1508 Standard_Integer dy() { return bottom - top; }
1511 myWindow->PlatformWindow()->Position (aGeom.left, aGeom.top, aGeom.right, aGeom.bottom);
1513 Standard_Boolean toReverse = myRenderParams.ToReverseStereo;
1514 const Standard_Boolean isOddY = (aGeom.top + aGeom.dy()) % 2 == 1;
1515 const Standard_Boolean isOddX = aGeom.left % 2 == 1;
1517 && (myRenderParams.StereoMode == Graphic3d_StereoMode_RowInterlaced
1518 || myRenderParams.StereoMode == Graphic3d_StereoMode_ChessBoard))
1520 toReverse = !toReverse;
1523 && (myRenderParams.StereoMode == Graphic3d_StereoMode_ColumnInterlaced
1524 || myRenderParams.StereoMode == Graphic3d_StereoMode_ChessBoard))
1526 toReverse = !toReverse;
1531 std::swap (aPair[0], aPair[1]);
1534 aCtx->core20fwd->glDepthFunc (GL_ALWAYS);
1535 aCtx->core20fwd->glDepthMask (GL_TRUE);
1536 aCtx->core20fwd->glEnable (GL_DEPTH_TEST);
1538 aCtx->BindTextures (Handle(OpenGl_TextureSet)());
1539 OpenGl_VertexBuffer* aVerts = initBlitQuad (myToFlipOutput);
1541 const Handle(OpenGl_ShaderManager)& aManager = aCtx->ShaderManager();
1542 if (aVerts->IsValid()
1543 && aManager->BindStereoProgram (myRenderParams.StereoMode))
1545 if (myRenderParams.StereoMode == Graphic3d_StereoMode_Anaglyph)
1547 OpenGl_Mat4 aFilterL, aFilterR;
1548 aFilterL.SetDiagonal (Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f));
1549 aFilterR.SetDiagonal (Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f));
1550 switch (myRenderParams.AnaglyphFilter)
1552 case Graphic3d_RenderingParams::Anaglyph_RedCyan_Simple:
1554 aFilterL.SetRow (0, Graphic3d_Vec4 (1.0f, 0.0f, 0.0f, 0.0f));
1555 aFilterR.SetRow (1, Graphic3d_Vec4 (0.0f, 1.0f, 0.0f, 0.0f));
1556 aFilterR.SetRow (2, Graphic3d_Vec4 (0.0f, 0.0f, 1.0f, 0.0f));
1559 case Graphic3d_RenderingParams::Anaglyph_RedCyan_Optimized:
1561 aFilterL.SetRow (0, Graphic3d_Vec4 ( 0.4154f, 0.4710f, 0.16666667f, 0.0f));
1562 aFilterL.SetRow (1, Graphic3d_Vec4 (-0.0458f, -0.0484f, -0.0257f, 0.0f));
1563 aFilterL.SetRow (2, Graphic3d_Vec4 (-0.0547f, -0.0615f, 0.0128f, 0.0f));
1564 aFilterL.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f));
1565 aFilterR.SetRow (0, Graphic3d_Vec4 (-0.01090909f, -0.03636364f, -0.00606061f, 0.0f));
1566 aFilterR.SetRow (1, Graphic3d_Vec4 ( 0.37560000f, 0.73333333f, 0.01111111f, 0.0f));
1567 aFilterR.SetRow (2, Graphic3d_Vec4 (-0.06510000f, -0.12870000f, 1.29710000f, 0.0f));
1568 aFilterR.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f));
1571 case Graphic3d_RenderingParams::Anaglyph_YellowBlue_Simple:
1573 aFilterL.SetRow (0, Graphic3d_Vec4 (1.0f, 0.0f, 0.0f, 0.0f));
1574 aFilterL.SetRow (1, Graphic3d_Vec4 (0.0f, 1.0f, 0.0f, 0.0f));
1575 aFilterR.SetRow (2, Graphic3d_Vec4 (0.0f, 0.0f, 1.0f, 0.0f));
1578 case Graphic3d_RenderingParams::Anaglyph_YellowBlue_Optimized:
1580 aFilterL.SetRow (0, Graphic3d_Vec4 ( 1.062f, -0.205f, 0.299f, 0.0f));
1581 aFilterL.SetRow (1, Graphic3d_Vec4 (-0.026f, 0.908f, 0.068f, 0.0f));
1582 aFilterL.SetRow (2, Graphic3d_Vec4 (-0.038f, -0.173f, 0.022f, 0.0f));
1583 aFilterL.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f));
1584 aFilterR.SetRow (0, Graphic3d_Vec4 (-0.016f, -0.123f, -0.017f, 0.0f));
1585 aFilterR.SetRow (1, Graphic3d_Vec4 ( 0.006f, 0.062f, -0.017f, 0.0f));
1586 aFilterR.SetRow (2, Graphic3d_Vec4 ( 0.094f, 0.185f, 0.911f, 0.0f));
1587 aFilterR.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f));
1590 case Graphic3d_RenderingParams::Anaglyph_GreenMagenta_Simple:
1592 aFilterR.SetRow (0, Graphic3d_Vec4 (1.0f, 0.0f, 0.0f, 0.0f));
1593 aFilterL.SetRow (1, Graphic3d_Vec4 (0.0f, 1.0f, 0.0f, 0.0f));
1594 aFilterR.SetRow (2, Graphic3d_Vec4 (0.0f, 0.0f, 1.0f, 0.0f));
1597 case Graphic3d_RenderingParams::Anaglyph_UserDefined:
1599 aFilterL = myRenderParams.AnaglyphLeft;
1600 aFilterR = myRenderParams.AnaglyphRight;
1604 aCtx->ActiveProgram()->SetUniform (aCtx, "uMultL", aFilterL);
1605 aCtx->ActiveProgram()->SetUniform (aCtx, "uMultR", aFilterR);
1608 aPair[0]->ColorTexture()->Bind (aCtx, Graphic3d_TextureUnit_0);
1609 aPair[1]->ColorTexture()->Bind (aCtx, Graphic3d_TextureUnit_1);
1610 aVerts->BindVertexAttrib (aCtx, 0);
1612 aCtx->core20fwd->glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
1614 aVerts->UnbindVertexAttrib (aCtx, 0);
1615 aPair[1]->ColorTexture()->Unbind (aCtx, Graphic3d_TextureUnit_1);
1616 aPair[0]->ColorTexture()->Unbind (aCtx, Graphic3d_TextureUnit_0);
1620 TCollection_ExtendedString aMsg = TCollection_ExtendedString()
1621 + "Error! Anaglyph has failed";
1622 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1623 GL_DEBUG_TYPE_ERROR,
1625 GL_DEBUG_SEVERITY_HIGH,
1630 // =======================================================================
1631 // function : copyBackToFront
1633 // =======================================================================
1634 bool OpenGl_View::copyBackToFront()
1636 myIsImmediateDrawn = Standard_False;
1637 #if !defined(GL_ES_VERSION_2_0)
1638 const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext();
1639 if (aCtx->core11 == NULL)
1644 OpenGl_Mat4 aProjectMat;
1645 Graphic3d_TransformUtils::Ortho2D (aProjectMat,
1646 0.0f, static_cast<GLfloat> (myWindow->Width()),
1647 0.0f, static_cast<GLfloat> (myWindow->Height()));
1649 aCtx->WorldViewState.Push();
1650 aCtx->ProjectionState.Push();
1652 aCtx->WorldViewState.SetIdentity();
1653 aCtx->ProjectionState.SetCurrent (aProjectMat);
1655 aCtx->ApplyProjectionMatrix();
1656 aCtx->ApplyWorldViewMatrix();
1658 // synchronize FFP state before copying pixels
1659 aCtx->BindProgram (Handle(OpenGl_ShaderProgram)());
1660 aCtx->ShaderManager()->PushState (Handle(OpenGl_ShaderProgram)());
1661 aCtx->DisableFeatures();
1663 switch (aCtx->DrawBuffer())
1667 aCtx->SetReadBuffer (GL_BACK_LEFT);
1668 aCtx->SetDrawBuffer (GL_FRONT_LEFT);
1673 aCtx->SetReadBuffer (GL_BACK_RIGHT);
1674 aCtx->SetDrawBuffer (GL_FRONT_RIGHT);
1679 aCtx->SetReadBuffer (GL_BACK);
1680 aCtx->SetDrawBuffer (GL_FRONT);
1685 aCtx->core11->glRasterPos2i (0, 0);
1686 aCtx->core11->glCopyPixels (0, 0, myWindow->Width() + 1, myWindow->Height() + 1, GL_COLOR);
1687 //aCtx->core11->glCopyPixels (0, 0, myWidth + 1, myHeight + 1, GL_DEPTH);
1689 aCtx->EnableFeatures();
1691 aCtx->WorldViewState.Pop();
1692 aCtx->ProjectionState.Pop();
1693 aCtx->ApplyProjectionMatrix();
1695 // read/write from front buffer now
1696 aCtx->SetReadBuffer (aCtx->DrawBuffer());
1703 // =======================================================================
1704 // function : checkOitCompatibility
1706 // =======================================================================
1707 Standard_Boolean OpenGl_View::checkOitCompatibility (const Handle(OpenGl_Context)& theGlContext,
1708 const Standard_Boolean theMSAA)
1710 // determine if OIT is supported by current OpenGl context
1711 Standard_Boolean& aToDisableOIT = theMSAA ? myToDisableMSAA : myToDisableOIT;
1714 return Standard_False;
1717 TCollection_ExtendedString aCompatibilityMsg;
1718 if (theGlContext->hasFloatBuffer == OpenGl_FeatureNotAvailable
1719 && theGlContext->hasHalfFloatBuffer == OpenGl_FeatureNotAvailable)
1721 aCompatibilityMsg += "OpenGL context does not support floating-point RGBA color buffer format.\n";
1723 if (theMSAA && theGlContext->hasSampleVariables == OpenGl_FeatureNotAvailable)
1725 aCompatibilityMsg += "Current version of GLSL does not support built-in sample variables.\n";
1727 if (theGlContext->hasDrawBuffers == OpenGl_FeatureNotAvailable)
1729 aCompatibilityMsg += "OpenGL context does not support multiple draw buffers.\n";
1731 if (aCompatibilityMsg.IsEmpty())
1733 return Standard_True;
1736 aCompatibilityMsg += " Blended order-independent transparency will not be available.\n";
1737 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1738 GL_DEBUG_TYPE_ERROR,
1740 GL_DEBUG_SEVERITY_HIGH,
1743 aToDisableOIT = Standard_True;
1744 return Standard_False;
1747 // =======================================================================
1748 // function : chooseOitColorConfiguration
1750 // =======================================================================
1751 bool OpenGl_View::chooseOitColorConfiguration (const Handle(OpenGl_Context)& theGlContext,
1752 const Standard_Integer theConfigIndex,
1753 OpenGl_ColorFormats& theFormats)
1756 switch (theConfigIndex)
1758 case 0: // choose best applicable color format combination
1760 theFormats.Append (theGlContext->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable ? GL_RGBA16F : GL_RGBA32F);
1761 theFormats.Append (theGlContext->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable ? GL_R16F : GL_R32F);
1764 case 1: // choose non-optimal applicable color format combination
1766 theFormats.Append (theGlContext->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable ? GL_RGBA16F : GL_RGBA32F);
1767 theFormats.Append (theGlContext->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable ? GL_RGBA16F : GL_RGBA32F);
1771 return false; // color combination does not exist