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