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