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