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