703d1271838b7534737c8daa1362c92c2b6835c4
[occt.git] / src / OpenGl / OpenGl_View_Redraw.cxx
1 // Created on: 2011-09-20
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <stdio.h>
17 #include <stdlib.h>
18
19 #include <OpenGl_GlCore11.hxx>
20
21 #include <Graphic3d_GraphicDriver.hxx>
22 #include <Graphic3d_TextureParams.hxx>
23 #include <Graphic3d_Texture2Dmanual.hxx>
24 #include <Graphic3d_TransformUtils.hxx>
25 #include <Image_AlienPixMap.hxx>
26
27 #include <NCollection_Mat4.hxx>
28
29 #include <OpenGl_AspectLine.hxx>
30 #include <OpenGl_Context.hxx>
31 #include <OpenGl_Matrix.hxx>
32 #include <OpenGl_Workspace.hxx>
33 #include <OpenGl_View.hxx>
34 #include <OpenGl_GraduatedTrihedron.hxx>
35 #include <OpenGl_PrimitiveArray.hxx>
36 #include <OpenGl_ShaderManager.hxx>
37 #include <OpenGl_ShaderProgram.hxx>
38 #include <OpenGl_Structure.hxx>
39 #include <OpenGl_ArbFBO.hxx>
40
41 //=======================================================================
42 //function : drawBackground
43 //purpose  :
44 //=======================================================================
45 void OpenGl_View::drawBackground (const Handle(OpenGl_Workspace)& theWorkspace)
46 {
47   const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
48
49   if (!myBgTextureArray->IsDefined()   // no texture
50    && !myBgGradientArray->IsDefined()) // no gradient
51   {
52     return;
53   }
54
55   const Standard_Boolean wasUsedZBuffer = theWorkspace->SetUseZBuffer (Standard_False);
56   if (wasUsedZBuffer)
57   {
58     aCtx->core11fwd->glDisable (GL_DEPTH_TEST);
59   }
60
61   // Drawing background gradient if:
62   // - gradient fill type is not Aspect_GFM_NONE and
63   // - either background texture is no specified or it is drawn in Aspect_FM_CENTERED mode
64   if (myBgGradientArray->IsDefined()
65     && (!myTextureParams->Aspect()->ToMapTexture()
66       || myBgTextureArray->TextureFillMethod() == Aspect_FM_CENTERED
67       || myBgTextureArray->TextureFillMethod() == Aspect_FM_NONE))
68   {
69   #if !defined(GL_ES_VERSION_2_0)
70     GLint aShadingModelOld = GL_SMOOTH;
71     if (aCtx->core11 != NULL)
72     {
73       aCtx->core11fwd->glDisable (GL_LIGHTING);
74       aCtx->core11fwd->glGetIntegerv (GL_SHADE_MODEL, &aShadingModelOld);
75       aCtx->core11->glShadeModel (GL_SMOOTH);
76     }
77   #endif
78
79     myBgGradientArray->Render (theWorkspace);
80
81   #if !defined(GL_ES_VERSION_2_0)
82     if (aCtx->core11 != NULL)
83     {
84       aCtx->core11->glShadeModel (aShadingModelOld);
85     }
86   #endif
87   }
88
89   // Drawing background image if it is defined
90   // (texture is defined and fill type is not Aspect_FM_NONE)
91   if (myBgTextureArray->IsDefined()
92    && myTextureParams->Aspect()->ToMapTexture())
93   {
94     aCtx->core11fwd->glDisable (GL_BLEND);
95
96     const OpenGl_AspectFace* anOldAspectFace = theWorkspace->SetAspectFace (myTextureParams);
97     myBgTextureArray->Render (theWorkspace);
98     theWorkspace->SetAspectFace (anOldAspectFace);
99   }
100
101   if (wasUsedZBuffer)
102   {
103     theWorkspace->SetUseZBuffer (Standard_True);
104     aCtx->core11fwd->glEnable (GL_DEPTH_TEST);
105   }
106 }
107
108 //=======================================================================
109 //function : Redraw
110 //purpose  :
111 //=======================================================================
112 void OpenGl_View::Redraw()
113 {
114   const Standard_Boolean wasDisabledMSAA = myToDisableMSAA;
115   const Standard_Boolean hadFboBlit      = myHasFboBlit;
116   if (myRenderParams.Method == Graphic3d_RM_RAYTRACING
117   && !myCaps->vboDisable
118   && !myCaps->keepArrayData)
119   {
120     if (myWasRedrawnGL)
121     {
122       myDeviceLostFlag = Standard_True;
123     }
124
125     myCaps->keepArrayData = Standard_True;
126   }
127
128   if (!myWorkspace->Activate())
129   {
130     return;
131   }
132
133   myWindow->SetSwapInterval();
134
135   ++myFrameCounter;
136   const Graphic3d_StereoMode   aStereoMode  = myRenderParams.StereoMode;
137   Graphic3d_Camera::Projection aProjectType = myCamera->ProjectionType();
138   Handle(OpenGl_Context)       aCtx         = myWorkspace->GetGlContext();
139
140   // release pending GL resources
141   aCtx->ReleaseDelayed();
142
143   // fetch OpenGl context state
144   aCtx->FetchState();
145
146   OpenGl_FrameBuffer* aFrameBuffer = myFBO.operator->();
147   bool toSwap = aCtx->IsRender()
148             && !aCtx->caps->buffersNoSwap
149             &&  aFrameBuffer == NULL;
150
151   Standard_Integer aSizeX = aFrameBuffer != NULL ? aFrameBuffer->GetVPSizeX() : myWindow->Width();
152   Standard_Integer aSizeY = aFrameBuffer != NULL ? aFrameBuffer->GetVPSizeY() : myWindow->Height();
153   Standard_Integer aRendSizeX = Standard_Integer(myRenderParams.RenderResolutionScale * aSizeX + 0.5f);
154   Standard_Integer aRendSizeY = Standard_Integer(myRenderParams.RenderResolutionScale * aSizeY + 0.5f);
155
156   // determine multisampling parameters
157   Standard_Integer aNbSamples = !myToDisableMSAA && aSizeX == aRendSizeX
158                               ? Max (Min (myRenderParams.NbMsaaSamples, aCtx->MaxMsaaSamples()), 0)
159                               : 0;
160   if (aNbSamples != 0)
161   {
162     aNbSamples = OpenGl_Context::GetPowerOfTwo (aNbSamples, aCtx->MaxMsaaSamples());
163   }
164
165   bool toUseOit = myRenderParams.TransparencyMethod == Graphic3d_RTM_BLEND_OIT
166                && checkOitCompatibility (aCtx, aNbSamples > 0);
167
168   const bool toInitImmediateFbo = myTransientDrawToFront
169                                && (!aCtx->caps->useSystemBuffer || (toUseOit && HasImmediateStructures()));
170
171   if ( aFrameBuffer == NULL
172    && !aCtx->DefaultFrameBuffer().IsNull()
173    &&  aCtx->DefaultFrameBuffer()->IsValid())
174   {
175     aFrameBuffer = aCtx->DefaultFrameBuffer().operator->();
176   }
177
178   if (myHasFboBlit
179    && (myTransientDrawToFront
180     || aProjectType == Graphic3d_Camera::Projection_Stereo
181     || aNbSamples != 0
182     || toUseOit
183     || aSizeX != aRendSizeX))
184   {
185     if (myMainSceneFbos[0]->GetVPSizeX() != aRendSizeX
186      || myMainSceneFbos[0]->GetVPSizeY() != aRendSizeY
187      || myMainSceneFbos[0]->NbSamples()  != aNbSamples)
188     {
189       if (!myTransientDrawToFront)
190       {
191         myImmediateSceneFbos[0]->Release (aCtx.operator->());
192         myImmediateSceneFbos[1]->Release (aCtx.operator->());
193         myImmediateSceneFbos[0]->ChangeViewport (0, 0);
194         myImmediateSceneFbos[1]->ChangeViewport (0, 0);
195       }
196
197       // prepare FBOs containing main scene
198       // for further blitting and rendering immediate presentations on top
199       if (aCtx->core20fwd != NULL)
200       {
201         myMainSceneFbos[0]->Init (aCtx, aRendSizeX, aRendSizeY, myFboColorFormat, myFboDepthFormat, aNbSamples);
202       }
203     }
204     if (myMainSceneFbos[0]->IsValid() && (toInitImmediateFbo || myImmediateSceneFbos[0]->IsValid()))
205     {
206       myImmediateSceneFbos[0]->InitLazy (aCtx, *myMainSceneFbos[0]);
207     }
208   }
209   else
210   {
211     myMainSceneFbos     [0]->Release (aCtx.operator->());
212     myMainSceneFbos     [1]->Release (aCtx.operator->());
213     myImmediateSceneFbos[0]->Release (aCtx.operator->());
214     myImmediateSceneFbos[1]->Release (aCtx.operator->());
215     myMainSceneFbos     [0]->ChangeViewport (0, 0);
216     myMainSceneFbos     [1]->ChangeViewport (0, 0);
217     myImmediateSceneFbos[0]->ChangeViewport (0, 0);
218     myImmediateSceneFbos[1]->ChangeViewport (0, 0);
219   }
220
221   if (aProjectType == Graphic3d_Camera::Projection_Stereo
222    && myMainSceneFbos[0]->IsValid())
223   {
224     myMainSceneFbos[1]->InitLazy (aCtx, *myMainSceneFbos[0]);
225     if (!myMainSceneFbos[1]->IsValid())
226     {
227       // no enough memory?
228       aProjectType = Graphic3d_Camera::Projection_Perspective;
229     }
230     else if (!myTransientDrawToFront)
231     {
232       //
233     }
234     else if (!aCtx->HasStereoBuffers() || aStereoMode != Graphic3d_StereoMode_QuadBuffer)
235     {
236       myImmediateSceneFbos[0]->InitLazy (aCtx, *myMainSceneFbos[0]);
237       myImmediateSceneFbos[1]->InitLazy (aCtx, *myMainSceneFbos[0]);
238       if (!myImmediateSceneFbos[0]->IsValid()
239        || !myImmediateSceneFbos[1]->IsValid())
240       {
241         aProjectType = Graphic3d_Camera::Projection_Perspective;
242       }
243     }
244   }
245
246   // create color and coverage accumulation buffers required for OIT algorithm
247   if (toUseOit)
248   {
249     Standard_Integer anFboIt = 0;
250     for (; anFboIt < 2; ++anFboIt)
251     {
252       Handle(OpenGl_FrameBuffer)& aMainSceneFbo          = myMainSceneFbos        [anFboIt];
253       Handle(OpenGl_FrameBuffer)& aMainSceneFboOit       = myMainSceneFbosOit     [anFboIt];
254       Handle(OpenGl_FrameBuffer)& anImmediateSceneFbo    = myImmediateSceneFbos   [anFboIt];
255       Handle(OpenGl_FrameBuffer)& anImmediateSceneFboOit = myImmediateSceneFbosOit[anFboIt];
256       if (aMainSceneFbo->IsValid()
257        && (aMainSceneFboOit->GetVPSizeX() != aRendSizeX
258         || aMainSceneFboOit->GetVPSizeY() != aRendSizeY
259         || aMainSceneFboOit->NbSamples()  != aNbSamples))
260       {
261         Standard_Integer aColorConfig = 0;
262         for (;;) // seemly responding to driver limitation (GL_FRAMEBUFFER_UNSUPPORTED)
263         {
264           if (myFboOitColorConfig.IsEmpty())
265           {
266             if (!chooseOitColorConfiguration (aCtx, aColorConfig++, myFboOitColorConfig))
267             {
268               break;
269             }
270           }
271           if (aMainSceneFboOit->Init (aCtx, aRendSizeX, aRendSizeY, myFboOitColorConfig, aMainSceneFbo->DepthStencilTexture(), aNbSamples))
272           {
273             break;
274           }
275           myFboOitColorConfig.Clear();
276         }
277         if (!aMainSceneFboOit->IsValid())
278         {
279           break;
280         }
281       }
282       else if (!aMainSceneFbo->IsValid())
283       {
284         aMainSceneFboOit->Release (aCtx.operator->());
285         aMainSceneFboOit->ChangeViewport (0, 0);
286       }
287
288       if (anImmediateSceneFbo->IsValid()
289        && (anImmediateSceneFboOit->GetVPSizeX() != aRendSizeX
290         || anImmediateSceneFboOit->GetVPSizeY() != aRendSizeY
291         || anImmediateSceneFboOit->NbSamples()  != aNbSamples))
292       {
293         if (!anImmediateSceneFboOit->Init (aCtx, aRendSizeX, aRendSizeY, myFboOitColorConfig,
294                                            anImmediateSceneFbo->DepthStencilTexture(), aNbSamples))
295         {
296           break;
297         }
298       }
299       else if (!anImmediateSceneFbo->IsValid())
300       {
301         anImmediateSceneFboOit->Release (aCtx.operator->());
302         anImmediateSceneFboOit->ChangeViewport (0, 0);
303       }
304     }
305     if (anFboIt == 0) // only the first OIT framebuffer is mandatory
306     {
307       aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH,
308                          "Initialization of float texture framebuffer for use with\n"
309                          "  blended order-independent transparency rendering algorithm has failed.\n"
310                          "  Blended order-independent transparency will not be available.\n");
311       if (aNbSamples > 0)
312       {
313         myToDisableOITMSAA = Standard_True;
314       }
315       else
316       {
317         myToDisableOIT     = Standard_True;
318       }
319       toUseOit = false;
320     }
321   }
322   if (!toUseOit && myMainSceneFbosOit[0]->IsValid())
323   {
324     myMainSceneFbosOit     [0]->Release (aCtx.operator->());
325     myMainSceneFbosOit     [1]->Release (aCtx.operator->());
326     myImmediateSceneFbosOit[0]->Release (aCtx.operator->());
327     myImmediateSceneFbosOit[1]->Release (aCtx.operator->());
328     myMainSceneFbosOit     [0]->ChangeViewport (0, 0);
329     myMainSceneFbosOit     [1]->ChangeViewport (0, 0);
330     myImmediateSceneFbosOit[0]->ChangeViewport (0, 0);
331     myImmediateSceneFbosOit[1]->ChangeViewport (0, 0);
332   }
333
334   if (aProjectType == Graphic3d_Camera::Projection_Stereo)
335   {
336     OpenGl_FrameBuffer* aMainFbos[2] =
337     {
338       myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL,
339       myMainSceneFbos[1]->IsValid() ? myMainSceneFbos[1].operator->() : NULL
340     };
341     OpenGl_FrameBuffer* aMainFbosOit[2] =
342     {
343       myMainSceneFbosOit[0]->IsValid() ? myMainSceneFbosOit[0].operator->() : NULL,
344       myMainSceneFbosOit[1]->IsValid() ? myMainSceneFbosOit[1].operator->() :
345         myMainSceneFbosOit[0]->IsValid() ? myMainSceneFbosOit[0].operator->() : NULL
346     };
347
348     OpenGl_FrameBuffer* anImmFbos[2] =
349     {
350       myImmediateSceneFbos[0]->IsValid() ? myImmediateSceneFbos[0].operator->() : NULL,
351       myImmediateSceneFbos[1]->IsValid() ? myImmediateSceneFbos[1].operator->() : NULL
352     };
353     OpenGl_FrameBuffer* anImmFbosOit[2] =
354     {
355       myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL,
356       myImmediateSceneFbosOit[1]->IsValid() ? myImmediateSceneFbosOit[1].operator->() :
357         myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL
358     };
359
360     if (!myTransientDrawToFront)
361     {
362       anImmFbos   [0] = aMainFbos   [0];
363       anImmFbos   [1] = aMainFbos   [1];
364       anImmFbosOit[0] = aMainFbosOit[0];
365       anImmFbosOit[1] = aMainFbosOit[1];
366     }
367     else if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip
368           || aStereoMode == Graphic3d_StereoMode_QuadBuffer)
369     {
370       anImmFbos   [0] = NULL;
371       anImmFbos   [1] = NULL;
372       anImmFbosOit[0] = NULL;
373       anImmFbosOit[1] = NULL;
374     }
375
376   #if !defined(GL_ES_VERSION_2_0)
377     aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_LEFT : GL_BACK);
378   #endif
379     aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
380                          aMainFbos[0] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
381
382     redraw (Graphic3d_Camera::Projection_MonoLeftEye, aMainFbos[0], aMainFbosOit[0]);
383     myBackBufferRestored = Standard_True;
384     myIsImmediateDrawn   = Standard_False;
385   #if !defined(GL_ES_VERSION_2_0)
386     aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_LEFT : GL_BACK);
387   #endif
388     aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
389                          anImmFbos[0] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
390     if (!redrawImmediate (Graphic3d_Camera::Projection_MonoLeftEye, aMainFbos[0], anImmFbos[0], anImmFbosOit[0]))
391     {
392       toSwap = false;
393     }
394     else if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip && toSwap)
395     {
396       aCtx->SwapBuffers();
397     }
398
399   #if !defined(GL_ES_VERSION_2_0)
400     aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_RIGHT : GL_BACK);
401   #endif
402     aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
403                          aMainFbos[1] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
404
405     redraw (Graphic3d_Camera::Projection_MonoRightEye, aMainFbos[1], aMainFbosOit[1]);
406     myBackBufferRestored = Standard_True;
407     myIsImmediateDrawn   = Standard_False;
408     aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
409                          anImmFbos[1] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
410     if (!redrawImmediate (Graphic3d_Camera::Projection_MonoRightEye, aMainFbos[1], anImmFbos[1], anImmFbosOit[1]))
411     {
412       toSwap = false;
413     }
414
415     if (anImmFbos[0] != NULL)
416     {
417       aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(), 1.0f);
418       drawStereoPair (aFrameBuffer);
419     }
420   }
421   else
422   {
423     OpenGl_FrameBuffer* aMainFbo    = myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : aFrameBuffer;
424     OpenGl_FrameBuffer* aMainFboOit = myMainSceneFbosOit[0]->IsValid() ? myMainSceneFbosOit[0].operator->() : NULL;
425     OpenGl_FrameBuffer* anImmFbo    = aFrameBuffer;
426     OpenGl_FrameBuffer* anImmFboOit = NULL;
427     if (!myTransientDrawToFront)
428     {
429       anImmFbo    = aMainFbo;
430       anImmFboOit = aMainFboOit;
431     }
432     else if (myImmediateSceneFbos[0]->IsValid())
433     {
434       anImmFbo    = myImmediateSceneFbos[0].operator->();
435       anImmFboOit = myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL;
436     }
437
438   #if !defined(GL_ES_VERSION_2_0)
439     if (aMainFbo == NULL)
440     {
441       aCtx->SetReadDrawBuffer (GL_BACK);
442     }
443   #endif
444     aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
445                          aMainFbo != aFrameBuffer ? myRenderParams.RenderResolutionScale : 1.0f);
446
447     redraw (aProjectType, aMainFbo, aMainFboOit);
448     myBackBufferRestored = Standard_True;
449     myIsImmediateDrawn   = Standard_False;
450     aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
451                          anImmFbo != aFrameBuffer ? myRenderParams.RenderResolutionScale : 1.0f);
452     if (!redrawImmediate (aProjectType, aMainFbo, anImmFbo, anImmFboOit))
453     {
454       toSwap = false;
455     }
456
457     if (anImmFbo != NULL
458      && anImmFbo != aFrameBuffer)
459     {
460       blitBuffers (anImmFbo, aFrameBuffer, myToFlipOutput);
461     }
462   }
463
464   if (myRenderParams.Method == Graphic3d_RM_RAYTRACING
465    && myRenderParams.IsGlobalIlluminationEnabled)
466   {
467     myAccumFrames++;
468   }
469
470   // bind default FBO
471   bindDefaultFbo();
472
473   if (wasDisabledMSAA != myToDisableMSAA
474    || hadFboBlit      != myHasFboBlit)
475   {
476     // retry on error
477     Redraw();
478   }
479
480   // reset state for safety
481   aCtx->BindProgram (Handle(OpenGl_ShaderProgram)());
482   aCtx->ShaderManager()->PushState (Handle(OpenGl_ShaderProgram)());
483
484   // Swap the buffers
485   if (toSwap)
486   {
487     aCtx->SwapBuffers();
488     if (!myMainSceneFbos[0]->IsValid())
489     {
490       myBackBufferRestored = Standard_False;
491     }
492   }
493   else
494   {
495     aCtx->core11fwd->glFlush();
496   }
497
498   // reset render mode state
499   aCtx->FetchState();
500
501   myWasRedrawnGL = Standard_True;
502 }
503
504 // =======================================================================
505 // function : RedrawImmediate
506 // purpose  :
507 // =======================================================================
508 void OpenGl_View::RedrawImmediate()
509 {
510   if (!myWorkspace->Activate())
511     return;
512
513   Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
514   if (!myTransientDrawToFront
515    || !myBackBufferRestored
516    || (aCtx->caps->buffersNoSwap && !myMainSceneFbos[0]->IsValid()))
517   {
518     Redraw();
519     return;
520   }
521
522   const Graphic3d_StereoMode   aStereoMode  = myRenderParams.StereoMode;
523   Graphic3d_Camera::Projection aProjectType = myCamera->ProjectionType();
524   OpenGl_FrameBuffer*          aFrameBuffer = myFBO.operator->();
525
526   if ( aFrameBuffer == NULL
527    && !aCtx->DefaultFrameBuffer().IsNull()
528    &&  aCtx->DefaultFrameBuffer()->IsValid())
529   {
530     aFrameBuffer = aCtx->DefaultFrameBuffer().operator->();
531   }
532
533   if (aProjectType == Graphic3d_Camera::Projection_Stereo)
534   {
535     if (myMainSceneFbos[0]->IsValid()
536     && !myMainSceneFbos[1]->IsValid())
537     {
538       aProjectType = Graphic3d_Camera::Projection_Perspective;
539     }
540   }
541
542   bool toSwap = false;
543   if (aProjectType == Graphic3d_Camera::Projection_Stereo)
544   {
545     OpenGl_FrameBuffer* aMainFbos[2] =
546     {
547       myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL,
548       myMainSceneFbos[1]->IsValid() ? myMainSceneFbos[1].operator->() : NULL
549     };
550     OpenGl_FrameBuffer* anImmFbos[2] =
551     {
552       myImmediateSceneFbos[0]->IsValid() ? myImmediateSceneFbos[0].operator->() : NULL,
553       myImmediateSceneFbos[1]->IsValid() ? myImmediateSceneFbos[1].operator->() : NULL
554     };
555     OpenGl_FrameBuffer* anImmFbosOit[2] =
556     {
557       myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL,
558       myImmediateSceneFbosOit[1]->IsValid() ? myImmediateSceneFbosOit[1].operator->() :
559         myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL
560     };
561     if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip
562      || aStereoMode == Graphic3d_StereoMode_QuadBuffer)
563     {
564       anImmFbos[0]    = NULL;
565       anImmFbos[1]    = NULL;
566       anImmFbosOit[0] = NULL;
567       anImmFbosOit[1] = NULL;
568     }
569
570     if (aCtx->arbFBO != NULL)
571     {
572       aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
573     }
574   #if !defined(GL_ES_VERSION_2_0)
575     if (anImmFbos[0] == NULL)
576     {
577       aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_LEFT : GL_BACK);
578     }
579   #endif
580
581     aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
582                          anImmFbos[0] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
583     toSwap = redrawImmediate (Graphic3d_Camera::Projection_MonoLeftEye,
584                               aMainFbos[0],
585                               anImmFbos[0],
586                               anImmFbosOit[0],
587                               Standard_True) || toSwap;
588     if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip
589     &&  toSwap
590     && !aCtx->caps->buffersNoSwap)
591     {
592       aCtx->SwapBuffers();
593     }
594
595     if (aCtx->arbFBO != NULL)
596     {
597       aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
598     }
599   #if !defined(GL_ES_VERSION_2_0)
600     if (anImmFbos[1] == NULL)
601     {
602       aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_RIGHT : GL_BACK);
603     }
604   #endif
605     aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
606                          anImmFbos[1] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
607     toSwap = redrawImmediate (Graphic3d_Camera::Projection_MonoRightEye,
608                               aMainFbos[1],
609                               anImmFbos[1],
610                               anImmFbosOit[1],
611                               Standard_True) || toSwap;
612     if (anImmFbos[0] != NULL)
613     {
614       drawStereoPair (aFrameBuffer);
615     }
616   }
617   else
618   {
619     OpenGl_FrameBuffer* aMainFbo = myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL;
620     OpenGl_FrameBuffer* anImmFbo = aFrameBuffer;
621     OpenGl_FrameBuffer* anImmFboOit = NULL;
622     if (myImmediateSceneFbos[0]->IsValid())
623     {
624       anImmFbo    = myImmediateSceneFbos[0].operator->();
625       anImmFboOit = myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL;
626     }
627   #if !defined(GL_ES_VERSION_2_0)
628     if (aMainFbo == NULL)
629     {
630       aCtx->SetReadDrawBuffer (GL_BACK);
631     }
632   #endif
633     aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
634                          anImmFbo != aFrameBuffer ? myRenderParams.RenderResolutionScale : 1.0f);
635     toSwap = redrawImmediate (aProjectType,
636                               aMainFbo,
637                               anImmFbo,
638                               anImmFboOit,
639                               Standard_True) || toSwap;
640     if (anImmFbo != NULL
641      && anImmFbo != aFrameBuffer)
642     {
643       blitBuffers (anImmFbo, aFrameBuffer, myToFlipOutput);
644     }
645   }
646
647   // bind default FBO
648   bindDefaultFbo();
649
650   // reset state for safety
651   aCtx->BindProgram (Handle(OpenGl_ShaderProgram)());
652   aCtx->ShaderManager()->PushState (Handle(OpenGl_ShaderProgram)());
653
654   if (toSwap && !aCtx->caps->buffersNoSwap)
655   {
656     aCtx->SwapBuffers();
657   }
658   else
659   {
660     aCtx->core11fwd->glFlush();
661   }
662
663   myWasRedrawnGL = Standard_True;
664 }
665
666 // =======================================================================
667 // function : redraw
668 // purpose  :
669 // =======================================================================
670 void OpenGl_View::redraw (const Graphic3d_Camera::Projection theProjection,
671                           OpenGl_FrameBuffer*                theReadDrawFbo,
672                           OpenGl_FrameBuffer*                theOitAccumFbo)
673 {
674   Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
675   if (theReadDrawFbo != NULL)
676   {
677     theReadDrawFbo->BindBuffer    (aCtx);
678     theReadDrawFbo->SetupViewport (aCtx);
679   }
680   else
681   {
682     const Standard_Integer aViewport[4] = { 0, 0, myWindow->Width(), myWindow->Height() };
683     aCtx->ResizeViewport (aViewport);
684   }
685
686   // request reset of material
687   aCtx->ShaderManager()->UpdateMaterialState();
688
689   myWorkspace->UseZBuffer()    = Standard_True;
690   myWorkspace->UseDepthWrite() = Standard_True;
691   GLbitfield toClear = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT;
692   glDepthFunc (GL_LEQUAL);
693   glDepthMask (GL_TRUE);
694   glEnable (GL_DEPTH_TEST);
695
696 #if !defined(GL_ES_VERSION_2_0)
697   glClearDepth (1.0);
698 #else
699   glClearDepthf (1.0f);
700 #endif
701
702   const OpenGl_Vec4& aBgColor = myBgColor;
703   glClearColor (aBgColor.r(), aBgColor.g(), aBgColor.b(), 0.0f);
704
705   glClear (toClear);
706
707   render (theProjection, theReadDrawFbo, theOitAccumFbo, Standard_False);
708 }
709
710 // =======================================================================
711 // function : redrawMonoImmediate
712 // purpose  :
713 // =======================================================================
714 bool OpenGl_View::redrawImmediate (const Graphic3d_Camera::Projection theProjection,
715                                    OpenGl_FrameBuffer*                theReadFbo,
716                                    OpenGl_FrameBuffer*                theDrawFbo,
717                                    OpenGl_FrameBuffer*                theOitAccumFbo,
718                                    const Standard_Boolean             theIsPartialUpdate)
719 {
720   Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
721   GLboolean toCopyBackToFront = GL_FALSE;
722   if (theDrawFbo == theReadFbo
723    && theDrawFbo != NULL)
724   {
725     myBackBufferRestored = Standard_False;
726   }
727   else if (theReadFbo != NULL
728         && theReadFbo->IsValid()
729         && aCtx->IsRender())
730   {
731     if (!blitBuffers (theReadFbo, theDrawFbo))
732     {
733       return true;
734     }
735   }
736   else if (theDrawFbo == NULL)
737   {
738   #if !defined(GL_ES_VERSION_2_0)
739     aCtx->core11fwd->glGetBooleanv (GL_DOUBLEBUFFER, &toCopyBackToFront);
740   #endif
741     if (toCopyBackToFront)
742     {
743       if (!HasImmediateStructures()
744        && !theIsPartialUpdate)
745       {
746         // prefer Swap Buffers within Redraw in compatibility mode (without FBO)
747         return true;
748       }
749       copyBackToFront();
750     }
751     else
752     {
753       myBackBufferRestored = Standard_False;
754     }
755   }
756   else
757   {
758     myBackBufferRestored = Standard_False;
759   }
760   myIsImmediateDrawn = Standard_True;
761
762   myWorkspace->UseZBuffer()    = Standard_True;
763   myWorkspace->UseDepthWrite() = Standard_True;
764   glDepthFunc (GL_LEQUAL);
765   glDepthMask (GL_TRUE);
766   glEnable (GL_DEPTH_TEST);
767 #if !defined(GL_ES_VERSION_2_0)
768   glClearDepth (1.0);
769 #else
770   glClearDepthf (1.0f);
771 #endif
772
773   render (theProjection, theDrawFbo, theOitAccumFbo, Standard_True);
774
775   return !toCopyBackToFront;
776 }
777
778 //=======================================================================
779 //function : Render
780 //purpose  :
781 //=======================================================================
782 void OpenGl_View::render (Graphic3d_Camera::Projection theProjection,
783                           OpenGl_FrameBuffer*          theOutputFBO,
784                           OpenGl_FrameBuffer*          theOitAccumFbo,
785                           const Standard_Boolean       theToDrawImmediate)
786 {
787   // ==================================
788   //      Step 1: Prepare for render
789   // ==================================
790
791   const Handle(OpenGl_Context)& aContext = myWorkspace->GetGlContext();
792
793 #if !defined(GL_ES_VERSION_2_0)
794   // Disable current clipping planes
795   if (aContext->core11 != NULL)
796   {
797     const Standard_Integer aMaxPlanes = aContext->MaxClipPlanes();
798     for (Standard_Integer aClipPlaneId = GL_CLIP_PLANE0; aClipPlaneId < GL_CLIP_PLANE0 + aMaxPlanes; ++aClipPlaneId)
799     {
800       aContext->core11fwd->glDisable (aClipPlaneId);
801     }
802   }
803 #endif
804
805   // Update states of OpenGl_BVHTreeSelector (frustum culling algorithm).
806   myBVHSelector.SetViewVolume (myCamera);
807   myBVHSelector.SetViewportSize (myWindow->Width(), myWindow->Height());
808
809   const Handle(OpenGl_ShaderManager)& aManager   = aContext->ShaderManager();
810   if (StateInfo (myCurrLightSourceState, aManager->LightSourceState().Index()) != myLastLightSourceState)
811   {
812     aManager->UpdateLightSourceStateTo (myShadingModel == Graphic3d_TOSM_NONE ? &myNoShadingLight : &myLights);
813     myLastLightSourceState = StateInfo (myCurrLightSourceState, aManager->LightSourceState().Index());
814   }
815
816   // Update matrices if camera has changed.
817   Graphic3d_WorldViewProjState aWVPState = myCamera->WorldViewProjState();
818   if (myWorldViewProjState != aWVPState)
819   {
820     myAccumFrames = 0;
821     myWorldViewProjState = aWVPState;
822   }
823
824   myLocalOrigin.SetCoord (0.0, 0.0, 0.0);
825   aContext->ProjectionState.SetCurrent (myCamera->ProjectionMatrixF());
826   aContext->WorldViewState .SetCurrent (myCamera->OrientationMatrixF());
827   aContext->ApplyProjectionMatrix();
828   aContext->ApplyWorldViewMatrix();
829   if (aManager->ModelWorldState().Index() == 0)
830   {
831     aContext->ShaderManager()->UpdateModelWorldStateTo (OpenGl_Mat4());
832   }
833
834   // ====================================
835   //      Step 2: Redraw background
836   // ====================================
837
838   // Render background
839   if (!theToDrawImmediate)
840   {
841     drawBackground (myWorkspace);
842   }
843
844 #if !defined(GL_ES_VERSION_2_0)
845   // Switch off lighting by default
846   if (aContext->core11 != NULL)
847   {
848     glDisable(GL_LIGHTING);
849   }
850 #endif
851
852   // =================================
853   //      Step 3: Redraw main plane
854   // =================================
855
856   // Setup face culling
857   GLboolean isCullFace = GL_FALSE;
858   if (myBackfacing != Graphic3d_TOBM_AUTOMATIC)
859   {
860     isCullFace = glIsEnabled (GL_CULL_FACE);
861     if (myBackfacing == Graphic3d_TOBM_DISABLE)
862     {
863       glEnable (GL_CULL_FACE);
864       glCullFace (GL_BACK);
865     }
866     else
867       glDisable (GL_CULL_FACE);
868   }
869
870 #if !defined(GL_ES_VERSION_2_0)
871   // if the view is scaled normal vectors are scaled to unit
872   // length for correct displaying of shaded objects
873   const gp_Pnt anAxialScale = myCamera->AxialScale();
874   if (anAxialScale.X() != 1.F ||
875       anAxialScale.Y() != 1.F ||
876       anAxialScale.Z() != 1.F)
877   {
878     aContext->SetGlNormalizeEnabled (Standard_True);
879   }
880   else
881   {
882     aContext->SetGlNormalizeEnabled (Standard_False);
883   }
884
885   // Apply InteriorShadingMethod
886   if (aContext->core11 != NULL)
887   {
888     aContext->core11->glShadeModel (myShadingModel == Graphic3d_TOSM_FACET
889                                  || myShadingModel == Graphic3d_TOSM_NONE ? GL_FLAT : GL_SMOOTH);
890   }
891 #endif
892
893   aManager->SetShadingModel (myShadingModel);
894
895   // Redraw 3d scene
896   if (theProjection == Graphic3d_Camera::Projection_MonoLeftEye)
897   {
898     aContext->ProjectionState.SetCurrent (myCamera->ProjectionStereoLeftF());
899     aContext->ApplyProjectionMatrix();
900   }
901   else if (theProjection == Graphic3d_Camera::Projection_MonoRightEye)
902   {
903     aContext->ProjectionState.SetCurrent (myCamera->ProjectionStereoRightF());
904     aContext->ApplyProjectionMatrix();
905   }
906
907   myWorkspace->SetEnvironmentTexture (myTextureEnv);
908
909   renderScene (theProjection, theOutputFBO, theOitAccumFbo, theToDrawImmediate);
910
911   myWorkspace->SetEnvironmentTexture (Handle(OpenGl_Texture)());
912
913   // ===============================
914   //      Step 4: Trihedron
915   // ===============================
916
917   // Resetting GL parameters according to the default aspects
918   // in order to synchronize GL state with the graphic driver state
919   // before drawing auxiliary stuff (trihedrons, overlayer)
920   myWorkspace->ResetAppliedAspect();
921
922   // Render trihedron
923   if (!theToDrawImmediate)
924   {
925     renderTrihedron (myWorkspace);
926
927     // Restore face culling
928     if (myBackfacing != Graphic3d_TOBM_AUTOMATIC)
929     {
930       if (isCullFace)
931       {
932         glEnable (GL_CULL_FACE);
933         glCullFace (GL_BACK);
934       }
935       else
936         glDisable (GL_CULL_FACE);
937     }
938   }
939
940   // reset FFP state for safety
941   aContext->BindProgram (Handle(OpenGl_ShaderProgram)());
942   aContext->ShaderManager()->PushState (Handle(OpenGl_ShaderProgram)());
943
944   // ==============================================================
945   //      Step 6: Keep shader manager informed about last View
946   // ==============================================================
947
948   if (!aManager.IsNull())
949   {
950     aManager->SetLastView (this);
951   }
952 }
953
954 // =======================================================================
955 // function : InvalidateBVHData
956 // purpose  :
957 // =======================================================================
958 void OpenGl_View::InvalidateBVHData (const Graphic3d_ZLayerId theLayerId)
959 {
960   myZLayers.InvalidateBVHData (theLayerId);
961 }
962
963 //=======================================================================
964 //function : renderStructs
965 //purpose  :
966 //=======================================================================
967 void OpenGl_View::renderStructs (Graphic3d_Camera::Projection theProjection,
968                                  OpenGl_FrameBuffer*          theReadDrawFbo,
969                                  OpenGl_FrameBuffer*          theOitAccumFbo,
970                                  const Standard_Boolean       theToDrawImmediate)
971 {
972   if ( myZLayers.NbStructures() <= 0 )
973     return;
974
975   Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
976   Standard_Boolean toRenderGL = theToDrawImmediate ||
977     myRenderParams.Method != Graphic3d_RM_RAYTRACING ||
978     myRaytraceInitStatus == OpenGl_RT_FAIL ||
979     aCtx->IsFeedback();
980
981   if (!toRenderGL)
982   {
983     toRenderGL = !initRaytraceResources (aCtx) ||
984       !updateRaytraceGeometry (OpenGl_GUM_CHECK, myId, aCtx);
985
986     toRenderGL |= !myIsRaytraceDataValid; // if no ray-trace data use OpenGL
987
988     if (!toRenderGL)
989     {
990       const Standard_Integer aSizeX = theReadDrawFbo != NULL ? theReadDrawFbo->GetVPSizeX() : myWindow->Width();
991       const Standard_Integer aSizeY = theReadDrawFbo != NULL ? theReadDrawFbo->GetVPSizeY() : myWindow->Height();
992       myOpenGlFBO ->InitLazy (aCtx, aSizeX, aSizeY, myFboColorFormat, myFboDepthFormat, 0);
993
994       if (myRaytraceFilter.IsNull())
995         myRaytraceFilter = new OpenGl_RaytraceFilter;
996
997       myRaytraceFilter->SetPrevRenderFilter (myWorkspace->GetRenderFilter());
998
999       if (theReadDrawFbo != NULL)
1000         theReadDrawFbo->UnbindBuffer (aCtx);
1001
1002       // Prepare preliminary OpenGL output
1003       if (aCtx->arbFBOBlit != NULL)
1004       {
1005         // Render bottom OSD layer
1006         myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_Bottom, theReadDrawFbo, theOitAccumFbo);
1007
1008         myWorkspace->SetRenderFilter (myRaytraceFilter);
1009         {
1010           if (theReadDrawFbo != NULL)
1011           {
1012             theReadDrawFbo->BindDrawBuffer (aCtx);
1013           }
1014           else
1015           {
1016             aCtx->arbFBO->glBindFramebuffer (GL_DRAW_FRAMEBUFFER, 0);
1017           }
1018
1019           // Render non-polygonal elements in default layer
1020           myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_Default, theReadDrawFbo, theOitAccumFbo);
1021         }
1022         myWorkspace->SetRenderFilter (myRaytraceFilter->PrevRenderFilter());
1023       }
1024
1025       if (theReadDrawFbo != NULL)
1026       {
1027         theReadDrawFbo->BindBuffer (aCtx);
1028       }
1029       else
1030       {
1031         aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, 0);
1032       }
1033
1034       // Reset OpenGl aspects state to default to avoid enabling of
1035       // backface culling which is not supported in ray-tracing.
1036       myWorkspace->ResetAppliedAspect();
1037
1038       // Ray-tracing polygonal primitive arrays
1039       raytrace (aSizeX, aSizeY, theProjection, theReadDrawFbo, aCtx);
1040
1041       // Render upper (top and topmost) OpenGL layers
1042       myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_Upper, theReadDrawFbo, theOitAccumFbo);
1043     }
1044   }
1045
1046   // Redraw 3D scene using OpenGL in standard
1047   // mode or in case of ray-tracing failure
1048   if (toRenderGL)
1049   {
1050     myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_All, theReadDrawFbo, theOitAccumFbo);
1051
1052     // Set flag that scene was redrawn by standard pipeline
1053     myWasRedrawnGL = Standard_True;
1054   }
1055 }
1056
1057 //=======================================================================
1058 //function : renderTrihedron
1059 //purpose  :
1060 //=======================================================================
1061 void OpenGl_View::renderTrihedron (const Handle(OpenGl_Workspace) &theWorkspace)
1062 {
1063   if (myToShowGradTrihedron)
1064   {
1065     myGraduatedTrihedron.Render (theWorkspace);
1066   }
1067 }
1068
1069 // =======================================================================
1070 // function : Invalidate
1071 // purpose  :
1072 // =======================================================================
1073 void OpenGl_View::Invalidate()
1074 {
1075   myBackBufferRestored = Standard_False;
1076 }
1077
1078 //=======================================================================
1079 //function : renderScene
1080 //purpose  :
1081 //=======================================================================
1082 void OpenGl_View::renderScene (Graphic3d_Camera::Projection theProjection,
1083                                OpenGl_FrameBuffer*          theReadDrawFbo,
1084                                OpenGl_FrameBuffer*          theOitAccumFbo,
1085                                const Standard_Boolean       theToDrawImmediate)
1086 {
1087   const Handle(OpenGl_Context)& aContext = myWorkspace->GetGlContext();
1088
1089   // Specify clipping planes in view transformation space
1090   aContext->ChangeClipping().Reset (aContext, myClipPlanes);
1091   if (!myClipPlanes.IsNull()
1092    && !myClipPlanes->IsEmpty())
1093   {
1094     aContext->ShaderManager()->UpdateClippingState();
1095   }
1096
1097   renderStructs (theProjection, theReadDrawFbo, theOitAccumFbo, theToDrawImmediate);
1098   myWorkspace->DisableTexture();
1099
1100   // Apply restored view matrix.
1101   aContext->ApplyWorldViewMatrix();
1102
1103   aContext->ChangeClipping().Reset (aContext, Handle(Graphic3d_SequenceOfHClipPlane)());
1104   if (!myClipPlanes.IsNull()
1105    && !myClipPlanes->IsEmpty())
1106   {
1107     aContext->ShaderManager()->RevertClippingState();
1108   }
1109 }
1110
1111 // =======================================================================
1112 // function : bindDefaultFbo
1113 // purpose  :
1114 // =======================================================================
1115 void OpenGl_View::bindDefaultFbo (OpenGl_FrameBuffer* theCustomFbo)
1116 {
1117   Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
1118   OpenGl_FrameBuffer* anFbo = (theCustomFbo != NULL && theCustomFbo->IsValid())
1119                             ?  theCustomFbo
1120                             : (!aCtx->DefaultFrameBuffer().IsNull()
1121                              && aCtx->DefaultFrameBuffer()->IsValid()
1122                               ? aCtx->DefaultFrameBuffer().operator->()
1123                               : NULL);
1124   if (anFbo != NULL)
1125   {
1126     anFbo->BindBuffer (aCtx);
1127     anFbo->SetupViewport (aCtx);
1128   }
1129   else
1130   {
1131   #if !defined(GL_ES_VERSION_2_0)
1132     aCtx->SetReadDrawBuffer (GL_BACK);
1133   #else
1134     if (aCtx->arbFBO != NULL)
1135     {
1136       aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
1137     }
1138   #endif
1139     const Standard_Integer aViewport[4] = { 0, 0, myWindow->Width(), myWindow->Height() };
1140     aCtx->ResizeViewport (aViewport);
1141   }
1142 }
1143
1144 // =======================================================================
1145 // function : initBlitQuad
1146 // purpose  :
1147 // =======================================================================
1148 OpenGl_VertexBuffer* OpenGl_View::initBlitQuad (const Standard_Boolean theToFlip)
1149 {
1150   OpenGl_VertexBuffer* aVerts = NULL;
1151   if (!theToFlip)
1152   {
1153     aVerts = &myFullScreenQuad;
1154     if (!aVerts->IsValid())
1155     {
1156       OpenGl_Vec4 aQuad[4] =
1157       {
1158         OpenGl_Vec4( 1.0f, -1.0f, 1.0f, 0.0f),
1159         OpenGl_Vec4( 1.0f,  1.0f, 1.0f, 1.0f),
1160         OpenGl_Vec4(-1.0f, -1.0f, 0.0f, 0.0f),
1161         OpenGl_Vec4(-1.0f,  1.0f, 0.0f, 1.0f)
1162       };
1163       aVerts->Init (myWorkspace->GetGlContext(), 4, 4, aQuad[0].GetData());
1164     }
1165   }
1166   else
1167   {
1168     aVerts = &myFullScreenQuadFlip;
1169     if (!aVerts->IsValid())
1170     {
1171       OpenGl_Vec4 aQuad[4] =
1172       {
1173         OpenGl_Vec4( 1.0f, -1.0f, 1.0f, 1.0f),
1174         OpenGl_Vec4( 1.0f,  1.0f, 1.0f, 0.0f),
1175         OpenGl_Vec4(-1.0f, -1.0f, 0.0f, 1.0f),
1176         OpenGl_Vec4(-1.0f,  1.0f, 0.0f, 0.0f)
1177       };
1178       aVerts->Init (myWorkspace->GetGlContext(), 4, 4, aQuad[0].GetData());
1179     }
1180   }
1181   return aVerts;
1182 }
1183
1184 // =======================================================================
1185 // function : blitBuffers
1186 // purpose  :
1187 // =======================================================================
1188 bool OpenGl_View::blitBuffers (OpenGl_FrameBuffer*    theReadFbo,
1189                                OpenGl_FrameBuffer*    theDrawFbo,
1190                                const Standard_Boolean theToFlip)
1191 {
1192   Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
1193   const Standard_Integer aReadSizeX = theReadFbo != NULL ? theReadFbo->GetVPSizeX() : myWindow->Width();
1194   const Standard_Integer aReadSizeY = theReadFbo != NULL ? theReadFbo->GetVPSizeY() : myWindow->Height();
1195   const Standard_Integer aDrawSizeX = theDrawFbo != NULL ? theDrawFbo->GetVPSizeX() : myWindow->Width();
1196   const Standard_Integer aDrawSizeY = theDrawFbo != NULL ? theDrawFbo->GetVPSizeY() : myWindow->Height();
1197   if (theReadFbo == NULL || aCtx->IsFeedback())
1198   {
1199     return false;
1200   }
1201   else if (theReadFbo == theDrawFbo)
1202   {
1203     return true;
1204   }
1205
1206   // clear destination before blitting
1207   if (theDrawFbo != NULL
1208   &&  theDrawFbo->IsValid())
1209   {
1210     theDrawFbo->BindBuffer (aCtx);
1211   }
1212   else
1213   {
1214     aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
1215   }
1216   const Standard_Integer aViewport[4] = { 0, 0, aDrawSizeX, aDrawSizeY };
1217   aCtx->ResizeViewport (aViewport);
1218
1219 #if !defined(GL_ES_VERSION_2_0)
1220   aCtx->core20fwd->glClearDepth  (1.0);
1221 #else
1222   aCtx->core20fwd->glClearDepthf (1.0f);
1223 #endif
1224   aCtx->core20fwd->glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
1225
1226   if (aCtx->arbFBOBlit != NULL
1227    && theReadFbo->NbSamples() != 0)
1228   {
1229     GLbitfield aCopyMask = 0;
1230     theReadFbo->BindReadBuffer (aCtx);
1231     if (theDrawFbo != NULL
1232      && theDrawFbo->IsValid())
1233     {
1234       theDrawFbo->BindDrawBuffer (aCtx);
1235       if (theDrawFbo->HasColor()
1236        && theReadFbo->HasColor())
1237       {
1238         aCopyMask |= GL_COLOR_BUFFER_BIT;
1239       }
1240       if (theDrawFbo->HasDepth()
1241        && theReadFbo->HasDepth())
1242       {
1243         aCopyMask |= GL_DEPTH_BUFFER_BIT;
1244       }
1245     }
1246     else
1247     {
1248       if (theReadFbo->HasColor())
1249       {
1250         aCopyMask |= GL_COLOR_BUFFER_BIT;
1251       }
1252       if (theReadFbo->HasDepth())
1253       {
1254         aCopyMask |= GL_DEPTH_BUFFER_BIT;
1255       }
1256       aCtx->arbFBO->glBindFramebuffer (GL_DRAW_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
1257     }
1258
1259     // we don't copy stencil buffer here... does it matter for performance?
1260     aCtx->arbFBOBlit->glBlitFramebuffer (0, 0, aReadSizeX, aReadSizeY,
1261                                          0, 0, aDrawSizeX, aDrawSizeY,
1262                                          aCopyMask, GL_NEAREST);
1263     const int anErr = ::glGetError();
1264     if (anErr != GL_NO_ERROR)
1265     {
1266       // glBlitFramebuffer() might fail in several cases:
1267       // - Both FBOs have MSAA and they are samples number does not match.
1268       //   OCCT checks that this does not happen,
1269       //   however some graphics drivers provide an option for overriding MSAA.
1270       //   In this case window MSAA might be non-zero (and application can not check it)
1271       //   and might not match MSAA of our offscreen FBOs.
1272       // - Pixel formats of FBOs do not match.
1273       //   This also might happen with window has pixel format,
1274       //   e.g. Mesa fails blitting RGBA8 -> RGB8 while other drivers support this conversion.
1275       TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "FBO blitting has failed [Error #" + anErr + "]\n"
1276                                       + "  Please check your graphics driver settings or try updating driver.";
1277       if (theReadFbo->NbSamples() != 0)
1278       {
1279         myToDisableMSAA = true;
1280         aMsg += "\n  MSAA settings should not be overridden by driver!";
1281       }
1282       aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1283                          GL_DEBUG_TYPE_ERROR,
1284                          0,
1285                          GL_DEBUG_SEVERITY_HIGH,
1286                          aMsg);
1287     }
1288
1289     if (theDrawFbo != NULL
1290      && theDrawFbo->IsValid())
1291     {
1292       theDrawFbo->BindBuffer (aCtx);
1293     }
1294     else
1295     {
1296       aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
1297     }
1298   }
1299   else
1300   {
1301     aCtx->core20fwd->glDepthFunc (GL_ALWAYS);
1302     aCtx->core20fwd->glDepthMask (GL_TRUE);
1303     aCtx->core20fwd->glEnable (GL_DEPTH_TEST);
1304   #if defined(GL_ES_VERSION_2_0)
1305     if (!aCtx->IsGlGreaterEqual (3, 0)
1306      && !aCtx->extFragDepth)
1307     {
1308       aCtx->core20fwd->glDisable (GL_DEPTH_TEST);
1309     }
1310   #endif
1311
1312     myWorkspace->DisableTexture();
1313
1314     const Graphic3d_TypeOfTextureFilter aFilter = (aDrawSizeX == aReadSizeX && aDrawSizeY == aReadSizeY) ? Graphic3d_TOTF_NEAREST : Graphic3d_TOTF_BILINEAR;
1315     const GLint aFilterGl = aFilter == Graphic3d_TOTF_NEAREST ? GL_NEAREST : GL_LINEAR;
1316
1317     OpenGl_VertexBuffer* aVerts = initBlitQuad (theToFlip);
1318     const Handle(OpenGl_ShaderManager)& aManager = aCtx->ShaderManager();
1319     if (aVerts->IsValid()
1320      && aManager->BindFboBlitProgram())
1321     {
1322       theReadFbo->ColorTexture()->Bind (aCtx, GL_TEXTURE0 + 0);
1323       if (theReadFbo->ColorTexture()->GetParams()->Filter() != aFilter)
1324       {
1325         theReadFbo->ColorTexture()->GetParams()->SetFilter (aFilter);
1326         aCtx->core20fwd->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, aFilterGl);
1327         aCtx->core20fwd->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, aFilterGl);
1328       }
1329
1330       theReadFbo->DepthStencilTexture()->Bind (aCtx, GL_TEXTURE0 + 1);
1331       if (theReadFbo->DepthStencilTexture()->GetParams()->Filter() != aFilter)
1332       {
1333         theReadFbo->DepthStencilTexture()->GetParams()->SetFilter (aFilter);
1334         aCtx->core20fwd->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, aFilterGl);
1335         aCtx->core20fwd->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, aFilterGl);
1336       }
1337
1338       aVerts->BindVertexAttrib (aCtx, Graphic3d_TOA_POS);
1339
1340       aCtx->core20fwd->glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
1341
1342       aVerts->UnbindVertexAttrib (aCtx, Graphic3d_TOA_POS);
1343       theReadFbo->DepthStencilTexture()->Unbind (aCtx, GL_TEXTURE0 + 1);
1344       theReadFbo->ColorTexture()       ->Unbind (aCtx, GL_TEXTURE0 + 0);
1345       aCtx->BindProgram (NULL);
1346     }
1347     else
1348     {
1349       TCollection_ExtendedString aMsg = TCollection_ExtendedString()
1350         + "Error! FBO blitting has failed";
1351       aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1352                          GL_DEBUG_TYPE_ERROR,
1353                          0,
1354                          GL_DEBUG_SEVERITY_HIGH,
1355                          aMsg);
1356       myHasFboBlit = Standard_False;
1357       theReadFbo->Release (aCtx.operator->());
1358       return true;
1359     }
1360   }
1361   return true;
1362 }
1363
1364 // =======================================================================
1365 // function : drawStereoPair
1366 // purpose  :
1367 // =======================================================================
1368 void OpenGl_View::drawStereoPair (OpenGl_FrameBuffer* theDrawFbo)
1369 {
1370   const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext();
1371   bindDefaultFbo (theDrawFbo);
1372   OpenGl_FrameBuffer* aPair[2] =
1373   {
1374     myImmediateSceneFbos[0]->IsValid() ? myImmediateSceneFbos[0].operator->() : NULL,
1375     myImmediateSceneFbos[1]->IsValid() ? myImmediateSceneFbos[1].operator->() : NULL
1376   };
1377   if (aPair[0] == NULL
1378   ||  aPair[1] == NULL
1379   || !myTransientDrawToFront)
1380   {
1381     aPair[0] = myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL;
1382     aPair[1] = myMainSceneFbos[1]->IsValid() ? myMainSceneFbos[1].operator->() : NULL;
1383   }
1384
1385   if (aPair[0] == NULL
1386    || aPair[1] == NULL)
1387   {
1388     return;
1389   }
1390
1391   if (aPair[0]->NbSamples() != 0)
1392   {
1393     // resolve MSAA buffers before drawing
1394     if (!myOpenGlFBO ->InitLazy (aCtx, aPair[0]->GetVPSizeX(), aPair[0]->GetVPSizeY(), myFboColorFormat, myFboDepthFormat, 0)
1395      || !myOpenGlFBO2->InitLazy (aCtx, aPair[0]->GetVPSizeX(), aPair[0]->GetVPSizeY(), myFboColorFormat, 0, 0))
1396     {
1397       aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1398                          GL_DEBUG_TYPE_ERROR,
1399                          0,
1400                          GL_DEBUG_SEVERITY_HIGH,
1401                          "Error! Unable to allocate FBO for blitting stereo pair");
1402       bindDefaultFbo (theDrawFbo);
1403       return;
1404     }
1405
1406     if (!blitBuffers (aPair[0], myOpenGlFBO .operator->(), Standard_False)
1407      || !blitBuffers (aPair[1], myOpenGlFBO2.operator->(), Standard_False))
1408     {
1409       bindDefaultFbo (theDrawFbo);
1410       return;
1411     }
1412
1413     aPair[0] = myOpenGlFBO .operator->();
1414     aPair[1] = myOpenGlFBO2.operator->();
1415     bindDefaultFbo (theDrawFbo);
1416   }
1417
1418   struct
1419   {
1420     Standard_Integer left;
1421     Standard_Integer top;
1422     Standard_Integer right;
1423     Standard_Integer bottom;
1424     Standard_Integer dx() { return right  - left; }
1425     Standard_Integer dy() { return bottom - top; }
1426   } aGeom;
1427
1428   myWindow->PlatformWindow()->Position (aGeom.left, aGeom.top, aGeom.right, aGeom.bottom);
1429
1430   Standard_Boolean toReverse = myRenderParams.ToReverseStereo;
1431   const Standard_Boolean isOddY = (aGeom.top + aGeom.dy()) % 2 == 1;
1432   const Standard_Boolean isOddX =  aGeom.left % 2 == 1;
1433   if (isOddY
1434    && (myRenderParams.StereoMode == Graphic3d_StereoMode_RowInterlaced
1435     || myRenderParams.StereoMode == Graphic3d_StereoMode_ChessBoard))
1436   {
1437     toReverse = !toReverse;
1438   }
1439   if (isOddX
1440    && (myRenderParams.StereoMode == Graphic3d_StereoMode_ColumnInterlaced
1441     || myRenderParams.StereoMode == Graphic3d_StereoMode_ChessBoard))
1442   {
1443     toReverse = !toReverse;
1444   }
1445
1446   if (toReverse)
1447   {
1448     std::swap (aPair[0], aPair[1]);
1449   }
1450
1451   aCtx->core20fwd->glDepthFunc (GL_ALWAYS);
1452   aCtx->core20fwd->glDepthMask (GL_TRUE);
1453   aCtx->core20fwd->glEnable (GL_DEPTH_TEST);
1454
1455   myWorkspace->DisableTexture();
1456   OpenGl_VertexBuffer* aVerts = initBlitQuad (myToFlipOutput);
1457
1458   const Handle(OpenGl_ShaderManager)& aManager = aCtx->ShaderManager();
1459   if (aVerts->IsValid()
1460    && aManager->BindStereoProgram (myRenderParams.StereoMode))
1461   {
1462     if (myRenderParams.StereoMode == Graphic3d_StereoMode_Anaglyph)
1463     {
1464       OpenGl_Mat4 aFilterL, aFilterR;
1465       aFilterL.SetDiagonal (Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f));
1466       aFilterR.SetDiagonal (Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f));
1467       switch (myRenderParams.AnaglyphFilter)
1468       {
1469         case Graphic3d_RenderingParams::Anaglyph_RedCyan_Simple:
1470         {
1471           aFilterL.SetRow (0, Graphic3d_Vec4 (1.0f, 0.0f, 0.0f, 0.0f));
1472           aFilterR.SetRow (1, Graphic3d_Vec4 (0.0f, 1.0f, 0.0f, 0.0f));
1473           aFilterR.SetRow (2, Graphic3d_Vec4 (0.0f, 0.0f, 1.0f, 0.0f));
1474           break;
1475         }
1476         case Graphic3d_RenderingParams::Anaglyph_RedCyan_Optimized:
1477         {
1478           aFilterL.SetRow (0, Graphic3d_Vec4 ( 0.4154f,      0.4710f,      0.16666667f, 0.0f));
1479           aFilterL.SetRow (1, Graphic3d_Vec4 (-0.0458f,     -0.0484f,     -0.0257f,     0.0f));
1480           aFilterL.SetRow (2, Graphic3d_Vec4 (-0.0547f,     -0.0615f,      0.0128f,     0.0f));
1481           aFilterL.SetRow (3, Graphic3d_Vec4 ( 0.0f,         0.0f,         0.0f,        0.0f));
1482           aFilterR.SetRow (0, Graphic3d_Vec4 (-0.01090909f, -0.03636364f, -0.00606061f, 0.0f));
1483           aFilterR.SetRow (1, Graphic3d_Vec4 ( 0.37560000f,  0.73333333f,  0.01111111f, 0.0f));
1484           aFilterR.SetRow (2, Graphic3d_Vec4 (-0.06510000f, -0.12870000f,  1.29710000f, 0.0f));
1485           aFilterR.SetRow (3, Graphic3d_Vec4 ( 0.0f,                0.0f,  0.0f,        0.0f));
1486           break;
1487         }
1488         case Graphic3d_RenderingParams::Anaglyph_YellowBlue_Simple:
1489         {
1490           aFilterL.SetRow (0, Graphic3d_Vec4 (1.0f, 0.0f, 0.0f, 0.0f));
1491           aFilterL.SetRow (1, Graphic3d_Vec4 (0.0f, 1.0f, 0.0f, 0.0f));
1492           aFilterR.SetRow (2, Graphic3d_Vec4 (0.0f, 0.0f, 1.0f, 0.0f));
1493           break;
1494         }
1495         case Graphic3d_RenderingParams::Anaglyph_YellowBlue_Optimized:
1496         {
1497           aFilterL.SetRow (0, Graphic3d_Vec4 ( 1.062f, -0.205f,  0.299f, 0.0f));
1498           aFilterL.SetRow (1, Graphic3d_Vec4 (-0.026f,  0.908f,  0.068f, 0.0f));
1499           aFilterL.SetRow (2, Graphic3d_Vec4 (-0.038f, -0.173f,  0.022f, 0.0f));
1500           aFilterL.SetRow (3, Graphic3d_Vec4 ( 0.0f,    0.0f,    0.0f,   0.0f));
1501           aFilterR.SetRow (0, Graphic3d_Vec4 (-0.016f, -0.123f, -0.017f, 0.0f));
1502           aFilterR.SetRow (1, Graphic3d_Vec4 ( 0.006f,  0.062f, -0.017f, 0.0f));
1503           aFilterR.SetRow (2, Graphic3d_Vec4 ( 0.094f,  0.185f,  0.911f, 0.0f));
1504           aFilterR.SetRow (3, Graphic3d_Vec4 ( 0.0f,    0.0f,    0.0f,   0.0f));
1505           break;
1506         }
1507         case Graphic3d_RenderingParams::Anaglyph_GreenMagenta_Simple:
1508         {
1509           aFilterR.SetRow (0, Graphic3d_Vec4 (1.0f, 0.0f, 0.0f, 0.0f));
1510           aFilterL.SetRow (1, Graphic3d_Vec4 (0.0f, 1.0f, 0.0f, 0.0f));
1511           aFilterR.SetRow (2, Graphic3d_Vec4 (0.0f, 0.0f, 1.0f, 0.0f));
1512           break;
1513         }
1514         case Graphic3d_RenderingParams::Anaglyph_UserDefined:
1515         {
1516           aFilterL = myRenderParams.AnaglyphLeft;
1517           aFilterR = myRenderParams.AnaglyphRight;
1518           break;
1519         }
1520       }
1521       aCtx->ActiveProgram()->SetUniform (aCtx, "uMultL", aFilterL);
1522       aCtx->ActiveProgram()->SetUniform (aCtx, "uMultR", aFilterR);
1523     }
1524
1525     aPair[0]->ColorTexture()->Bind (aCtx, GL_TEXTURE0 + 0);
1526     aPair[1]->ColorTexture()->Bind (aCtx, GL_TEXTURE0 + 1);
1527     aVerts->BindVertexAttrib (aCtx, 0);
1528
1529     aCtx->core20fwd->glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
1530
1531     aVerts->UnbindVertexAttrib (aCtx, 0);
1532     aPair[1]->ColorTexture()->Unbind (aCtx, GL_TEXTURE0 + 1);
1533     aPair[0]->ColorTexture()->Unbind (aCtx, GL_TEXTURE0 + 0);
1534   }
1535   else
1536   {
1537     TCollection_ExtendedString aMsg = TCollection_ExtendedString()
1538       + "Error! Anaglyph has failed";
1539     aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1540                        GL_DEBUG_TYPE_ERROR,
1541                        0,
1542                        GL_DEBUG_SEVERITY_HIGH,
1543                        aMsg);
1544   }
1545 }
1546
1547 // =======================================================================
1548 // function : copyBackToFront
1549 // purpose  :
1550 // =======================================================================
1551 void OpenGl_View::copyBackToFront()
1552 {
1553 #if !defined(GL_ES_VERSION_2_0)
1554
1555   OpenGl_Mat4 aProjectMat;
1556   Graphic3d_TransformUtils::Ortho2D (aProjectMat,
1557                                      0.0f, static_cast<GLfloat> (myWindow->Width()),
1558                                      0.0f, static_cast<GLfloat> (myWindow->Height()));
1559
1560   const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext();
1561
1562   aCtx->WorldViewState.Push();
1563   aCtx->ProjectionState.Push();
1564
1565   aCtx->WorldViewState.SetIdentity();
1566   aCtx->ProjectionState.SetCurrent (aProjectMat);
1567
1568   aCtx->ApplyProjectionMatrix();
1569   aCtx->ApplyWorldViewMatrix();
1570
1571   // synchronize FFP state before copying pixels
1572   aCtx->BindProgram (Handle(OpenGl_ShaderProgram)());
1573   aCtx->ShaderManager()->PushState (Handle(OpenGl_ShaderProgram)());
1574   aCtx->DisableFeatures();
1575
1576   switch (aCtx->DrawBuffer())
1577   {
1578     case GL_BACK_LEFT:
1579     {
1580       aCtx->SetReadBuffer (GL_BACK_LEFT);
1581       aCtx->SetDrawBuffer (GL_FRONT_LEFT);
1582       break;
1583     }
1584     case GL_BACK_RIGHT:
1585     {
1586       aCtx->SetReadBuffer (GL_BACK_RIGHT);
1587       aCtx->SetDrawBuffer (GL_FRONT_RIGHT);
1588       break;
1589     }
1590     default:
1591     {
1592       aCtx->SetReadBuffer (GL_BACK);
1593       aCtx->SetDrawBuffer (GL_FRONT);
1594       break;
1595     }
1596   }
1597
1598   glRasterPos2i (0, 0);
1599   glCopyPixels  (0, 0, myWindow->Width() + 1, myWindow->Height() + 1, GL_COLOR);
1600   //glCopyPixels  (0, 0, myWidth + 1, myHeight + 1, GL_DEPTH);
1601
1602   aCtx->EnableFeatures();
1603
1604   aCtx->WorldViewState.Pop();
1605   aCtx->ProjectionState.Pop();
1606   aCtx->ApplyProjectionMatrix();
1607
1608   // read/write from front buffer now
1609   aCtx->SetReadBuffer (aCtx->DrawBuffer());
1610 #endif
1611   myIsImmediateDrawn = Standard_False;
1612 }
1613
1614 // =======================================================================
1615 // function : checkOitCompatibility
1616 // purpose  :
1617 // =======================================================================
1618 Standard_Boolean OpenGl_View::checkOitCompatibility (const Handle(OpenGl_Context)& theGlContext,
1619                                                      const Standard_Boolean theMSAA)
1620 {
1621   // determine if OIT is supported by current OpenGl context
1622   Standard_Boolean& aToDisableOIT = theMSAA ? myToDisableMSAA : myToDisableOIT;
1623   if (aToDisableOIT)
1624   {
1625     return Standard_False;
1626   }
1627
1628   TCollection_ExtendedString aCompatibilityMsg;
1629   if (!theGlContext->hasFloatBuffer
1630    && !theGlContext->hasHalfFloatBuffer)
1631   {
1632     aCompatibilityMsg += "OpenGL context does not support floating-point RGBA color buffer format.\n";
1633   }
1634   if (theMSAA && !theGlContext->hasSampleVariables)
1635   {
1636     aCompatibilityMsg += "Current version of GLSL does not support built-in sample variables.\n";
1637   }
1638   if (!theGlContext->hasDrawBuffers)
1639   {
1640     aCompatibilityMsg += "OpenGL context does not support multiple draw buffers.\n";
1641   }
1642   if (aCompatibilityMsg.IsEmpty())
1643   {
1644     return Standard_True;
1645   }
1646
1647   aCompatibilityMsg += "  Blended order-independent transparency will not be available.\n";
1648   theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1649                           GL_DEBUG_TYPE_ERROR,
1650                           0,
1651                           GL_DEBUG_SEVERITY_HIGH,
1652                           aCompatibilityMsg);
1653
1654   aToDisableOIT = Standard_True;
1655   return Standard_False;
1656 }
1657
1658 // =======================================================================
1659 // function : chooseOitColorConfiguration
1660 // purpose  :
1661 // =======================================================================
1662 bool OpenGl_View::chooseOitColorConfiguration (const Handle(OpenGl_Context)& theGlContext,
1663                                                const Standard_Integer theConfigIndex,
1664                                                OpenGl_ColorFormats& theFormats)
1665 {
1666   theFormats.Clear();
1667   switch (theConfigIndex)
1668   {
1669     case 0: // choose best applicable color format combination
1670     {
1671       theFormats.Append (theGlContext->hasHalfFloatBuffer ? GL_RGBA16F : GL_RGBA32F);
1672       theFormats.Append (theGlContext->hasHalfFloatBuffer ? GL_R16F    : GL_R32F);
1673       return true;
1674     }
1675     case 1: // choose non-optimal applicable color format combination
1676     {
1677       theFormats.Append (theGlContext->hasHalfFloatBuffer ? GL_RGBA16F : GL_RGBA32F);
1678       theFormats.Append (theGlContext->hasHalfFloatBuffer ? GL_RGBA16F : GL_RGBA32F);
1679       return true;
1680     }
1681   }
1682   return false; // color combination does not exist
1683 }