0026083: Coding Rules - Poor design of Graphic3d_GraduatedTrihedron
[occt.git] / src / OpenGl / OpenGl_View.cxx
CommitLineData
b311480e 1// Created on: 2011-09-20
2// Created by: Sergey ZERCHANINOV
973c2be1 3// Copyright (c) 2011-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 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
973c2be1 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
b311480e 15
c357e426 16#include <OpenGl_View.hxx>
5f8b738e 17
c357e426 18#include <Aspect_RenderingContext.hxx>
19#include <Aspect_Window.hxx>
cfb54c0c 20#include <Aspect_XRSession.hxx>
c357e426 21#include <Graphic3d_AspectFillArea3d.hxx>
22#include <Graphic3d_Texture2Dmanual.hxx>
23#include <Graphic3d_TextureEnv.hxx>
24#include <Graphic3d_Mat4d.hxx>
cfb54c0c 25#include <Image_AlienPixMap.hxx>
26#include <OpenGl_ArbFBO.hxx>
1220d98e 27#include <OpenGl_BackgroundArray.hxx>
bf75be98 28#include <OpenGl_Context.hxx>
78c4e836 29#include <OpenGl_DepthPeeling.hxx>
c357e426 30#include <OpenGl_FrameBuffer.hxx>
30f0ad28 31#include <OpenGl_GlCore11.hxx>
32#include <OpenGl_GraduatedTrihedron.hxx>
392ac980 33#include <OpenGl_GraphicDriver.hxx>
30f0ad28 34#include <OpenGl_ShaderManager.hxx>
d84e8669 35#include <OpenGl_ShadowMap.hxx>
bf75be98 36#include <OpenGl_Texture.hxx>
c357e426 37#include <OpenGl_Window.hxx>
30f0ad28 38#include <OpenGl_Workspace.hxx>
f5b72419 39#include <OSD_Parallel.hxx>
c357e426 40#include <Standard_CLocaleSentry.hxx>
2166f0fa 41
f5b72419 42#include "../Graphic3d/Graphic3d_Structure.pxx"
cfb54c0c 43#include "../Textures/Textures_EnvLUT.pxx"
44
45namespace
46{
47 //! Format Frame Buffer format for logging messages.
48 static TCollection_AsciiString printFboFormat (const Handle(OpenGl_FrameBuffer)& theFbo)
49 {
50 return TCollection_AsciiString() + theFbo->GetInitVPSizeX() + "x" + theFbo->GetInitVPSizeY() + "@" + theFbo->NbSamples();
51 }
52
53 //! Return TRUE if Frame Buffer initialized has failed with the same parameters.
54 static bool checkWasFailedFbo (const Handle(OpenGl_FrameBuffer)& theFboToCheck,
55 Standard_Integer theSizeX,
56 Standard_Integer theSizeY,
57 Standard_Integer theNbSamples)
58 {
59 return !theFboToCheck->IsValid()
60 && theFboToCheck->GetInitVPSizeX() == theSizeX
61 && theFboToCheck->GetInitVPSizeY() == theSizeY
62 && theFboToCheck->NbSamples() == theNbSamples;
63 }
64
65 //! Return TRUE if Frame Buffer initialized has failed with the same parameters.
66 static bool checkWasFailedFbo (const Handle(OpenGl_FrameBuffer)& theFboToCheck,
67 const Handle(OpenGl_FrameBuffer)& theFboRef)
68 {
69 return checkWasFailedFbo (theFboToCheck, theFboRef->GetVPSizeX(), theFboRef->GetVPSizeY(), theFboRef->NbSamples());
70 }
1220d98e 71
72 //! Chooses compatible internal color format for OIT frame buffer.
73 static bool chooseOitColorConfiguration (const Handle(OpenGl_Context)& theGlContext,
74 const Standard_Integer theConfigIndex,
75 OpenGl_ColorFormats& theFormats)
76 {
77 theFormats.Clear();
78 switch (theConfigIndex)
79 {
80 case 0: // choose best applicable color format combination
81 {
82 theFormats.Append (theGlContext->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable ? GL_RGBA16F : GL_RGBA32F);
83 theFormats.Append (theGlContext->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable ? GL_R16F : GL_R32F);
84 return true;
85 }
86 case 1: // choose non-optimal applicable color format combination
87 {
88 theFormats.Append (theGlContext->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable ? GL_RGBA16F : GL_RGBA32F);
89 theFormats.Append (theGlContext->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable ? GL_RGBA16F : GL_RGBA32F);
90 return true;
91 }
92 }
93 return false; // color combination does not exist
94 }
cfb54c0c 95}
f5b72419 96
92efcf78 97IMPLEMENT_STANDARD_RTTIEXT(OpenGl_View,Graphic3d_CView)
98
c357e426 99// =======================================================================
100// function : Constructor
101// purpose :
102// =======================================================================
103OpenGl_View::OpenGl_View (const Handle(Graphic3d_StructureManager)& theMgr,
104 const Handle(OpenGl_GraphicDriver)& theDriver,
105 const Handle(OpenGl_Caps)& theCaps,
c357e426 106 OpenGl_StateCounter* theCounter)
107: Graphic3d_CView (theMgr),
108 myDriver (theDriver.operator->()),
109 myCaps (theCaps),
c357e426 110 myWasRedrawnGL (Standard_False),
c357e426 111 myToShowGradTrihedron (false),
f5b72419 112 myZLayers (Structure_MAX_PRIORITY - Structure_MIN_PRIORITY + 1),
c357e426 113 myStateCounter (theCounter),
992ed6b3 114 myCurrLightSourceState (theCounter->Increment()),
115 myLightsRevision (0),
0b0320e7 116 myLastLightSourceState (0, 0),
ba00aab7 117 mySRgbState (-1),
118 myFboColorFormat (GL_SRGB8_ALPHA8), // note that GL_SRGB8 is not required to be renderable, unlike GL_RGB8, GL_RGBA8, GL_SRGB8_ALPHA8
3c4b62a4 119 myFboDepthFormat (GL_DEPTH24_STENCIL8),
120 myToFlipOutput (Standard_False),
b40cdc2b 121 //
c357e426 122 myFrameCounter (0),
123 myHasFboBlit (Standard_True),
a1073ae2 124 myToDisableOIT (Standard_False),
125 myToDisableOITMSAA (Standard_False),
7ccf8676 126 myToDisableMSAA (Standard_False),
c357e426 127 myTransientDrawToFront (Standard_True),
128 myBackBufferRestored (Standard_False),
129 myIsImmediateDrawn (Standard_False),
bf5f0ca2 130 myTextureParams (new OpenGl_Aspects()),
077a220c 131 myCubeMapParams (new OpenGl_Aspects()),
132 myBackgroundType (Graphic3d_TOB_NONE),
67312b79 133 myPBREnvState (OpenGl_PBREnvState_NONEXISTENT),
134 myPBREnvRequest (OpenGl_PBREnvRequest_NONE),
91c60b57 135 // ray-tracing fields initialization
c357e426 136 myRaytraceInitStatus (OpenGl_RT_NONE),
137 myIsRaytraceDataValid (Standard_False),
91c60b57 138 myIsRaytraceWarnTextures (Standard_False),
f5b72419 139 myRaytraceBVHBuilder (new BVH_BinnedBuilder<Standard_ShortReal, 3, BVH_Constants_NbBinsBest> (BVH_Constants_LeafNodeSizeAverage,
140 BVH_Constants_MaxTreeDepth,
141 Standard_False,
142 OSD_Parallel::NbLogicalProcessors() + 1)),
143 myRaytraceSceneRadius (0.0f),
144 myRaytraceSceneEpsilon (1.0e-6f),
91c60b57 145 myToUpdateEnvironmentMap (Standard_False),
b27ab03d 146 myRaytraceLayerListState (0),
147 myPrevCameraApertureRadius(0.f),
148 myPrevCameraFocalPlaneDist(0.f)
2166f0fa 149{
077a220c 150 for (int i = 0; i < Graphic3d_TypeOfBackground_NB; ++i)
151 {
152 myBackgrounds[i] = new OpenGl_BackgroundArray(Graphic3d_TypeOfBackground(i));
153 }
154
c357e426 155 myWorkspace = new OpenGl_Workspace (this, NULL);
2166f0fa 156
06d40093 157 Handle(Graphic3d_CLight) aLight = new Graphic3d_CLight (Graphic3d_TypeOfLightSource_Ambient);
992ed6b3 158 aLight->SetColor (Quantity_NOC_WHITE);
8445efca 159 myLights = new Graphic3d_LightSet();
992ed6b3 160 myNoShadingLight = new Graphic3d_LightSet();
161 myNoShadingLight->Add (aLight);
016e5959 162
a1073ae2 163 myMainSceneFbos[0] = new OpenGl_FrameBuffer();
164 myMainSceneFbos[1] = new OpenGl_FrameBuffer();
165 myMainSceneFbosOit[0] = new OpenGl_FrameBuffer();
166 myMainSceneFbosOit[1] = new OpenGl_FrameBuffer();
167 myImmediateSceneFbos[0] = new OpenGl_FrameBuffer();
168 myImmediateSceneFbos[1] = new OpenGl_FrameBuffer();
169 myImmediateSceneFbosOit[0] = new OpenGl_FrameBuffer();
170 myImmediateSceneFbosOit[1] = new OpenGl_FrameBuffer();
b40cdc2b 171 myXrSceneFbo = new OpenGl_FrameBuffer();
a1073ae2 172 myOpenGlFBO = new OpenGl_FrameBuffer();
173 myOpenGlFBO2 = new OpenGl_FrameBuffer();
174 myRaytraceFBO1[0] = new OpenGl_FrameBuffer();
175 myRaytraceFBO1[1] = new OpenGl_FrameBuffer();
176 myRaytraceFBO2[0] = new OpenGl_FrameBuffer();
177 myRaytraceFBO2[1] = new OpenGl_FrameBuffer();
78c4e836 178 myDepthPeelingFbos = new OpenGl_DepthPeeling();
d84e8669 179 myShadowMaps = new OpenGl_ShadowMapArray();
c357e426 180}
181
182// =======================================================================
183// function : Destructor
184// purpose :
185// =======================================================================
0b0320e7 186OpenGl_View::~OpenGl_View()
2166f0fa 187{
bf75be98 188 ReleaseGlResources (NULL); // ensure ReleaseGlResources() was called within valid context
077a220c 189 for (int i = 0; i < Graphic3d_TypeOfBackground_NB; ++i)
190 {
191 OpenGl_Element::Destroy(NULL, myBackgrounds[i]);
192 }
193
0b0320e7 194 OpenGl_Element::Destroy (NULL, myTextureParams);
077a220c 195 OpenGl_Element::Destroy (NULL, myCubeMapParams);
bf75be98 196}
197
c357e426 198// =======================================================================
ba00aab7 199// function : releaseSrgbResources
c357e426 200// purpose :
201// =======================================================================
ba00aab7 202void OpenGl_View::releaseSrgbResources (const Handle(OpenGl_Context)& theCtx)
bf75be98 203{
ba00aab7 204 myRenderParams.RebuildRayTracingShaders = true;
30f0ad28 205
bf75be98 206 if (!myTextureEnv.IsNull())
207 {
ad67e367 208 if (!theCtx.IsNull())
cc8cbabe 209 {
ad67e367 210 for (OpenGl_TextureSet::Iterator aTextureIter (myTextureEnv); aTextureIter.More(); aTextureIter.Next())
211 {
212 theCtx->DelayedRelease (aTextureIter.ChangeValue());
213 aTextureIter.ChangeValue().Nullify();
214 }
cc8cbabe 215 }
bf75be98 216 myTextureEnv.Nullify();
217 }
0b0320e7 218
219 if (myTextureParams != NULL)
220 {
ad67e367 221 myTextureParams->Release (theCtx.get());
0b0320e7 222 }
077a220c 223
224 if (myCubeMapParams != NULL)
0b0320e7 225 {
077a220c 226 myCubeMapParams->Release (theCtx.get());
0b0320e7 227 }
077a220c 228
229 for (int i = 0; i < Graphic3d_TypeOfBackground_NB; ++i)
bf75be98 230 {
077a220c 231 if (myBackgrounds[i] != NULL)
232 {
233 myBackgrounds[i]->Release (theCtx.get());
234 }
bf75be98 235 }
91c60b57 236
ad67e367 237 myMainSceneFbos[0] ->Release (theCtx.get());
238 myMainSceneFbos[1] ->Release (theCtx.get());
239 myMainSceneFbosOit[0] ->Release (theCtx.get());
240 myMainSceneFbosOit[1] ->Release (theCtx.get());
241 myImmediateSceneFbos[0] ->Release (theCtx.get());
242 myImmediateSceneFbos[1] ->Release (theCtx.get());
243 myImmediateSceneFbosOit[0]->Release (theCtx.get());
244 myImmediateSceneFbosOit[1]->Release (theCtx.get());
b40cdc2b 245 myXrSceneFbo ->Release (theCtx.get());
78c4e836 246 myDepthPeelingFbos ->Release (theCtx.get());
ad67e367 247 myOpenGlFBO ->Release (theCtx.get());
248 myOpenGlFBO2 ->Release (theCtx.get());
249 myFullScreenQuad .Release (theCtx.get());
250 myFullScreenQuadFlip .Release (theCtx.get());
d84e8669 251 myShadowMaps->Release (theCtx.get());
c357e426 252
ba00aab7 253 // Technically we should also re-initialize all sRGB/RGB8 color textures.
254 // But for now consider this sRGB disabling/enabling to be done at application start-up
255 // and re-create dynamically only frame buffers.
256}
257
258// =======================================================================
259// function : ReleaseGlResources
260// purpose :
261// =======================================================================
262void OpenGl_View::ReleaseGlResources (const Handle(OpenGl_Context)& theCtx)
263{
264 myGraduatedTrihedron.Release (theCtx.get());
265 myFrameStatsPrs.Release (theCtx.get());
266
267 releaseSrgbResources (theCtx);
268
91c60b57 269 releaseRaytraceResources (theCtx);
67312b79 270
271 if (!myPBREnvironment.IsNull())
272 {
273 myPBREnvironment->Release (theCtx.get());
274 }
b40cdc2b 275 ReleaseXR();
bf75be98 276}
2166f0fa 277
c357e426 278// =======================================================================
279// function : Remove
280// purpose :
281// =======================================================================
282void OpenGl_View::Remove()
bf75be98 283{
c357e426 284 if (IsRemoved())
bf75be98 285 {
c357e426 286 return;
bf75be98 287 }
288
c357e426 289 myDriver->RemoveView (this);
290 myWindow.Nullify();
291
292 Graphic3d_CView::Remove();
293}
294
7c3ef2f7 295// =======================================================================
296// function : SetLocalOrigin
297// purpose :
298// =======================================================================
299void OpenGl_View::SetLocalOrigin (const gp_XYZ& theOrigin)
300{
301 myLocalOrigin = theOrigin;
302 const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext();
303 if (!aCtx.IsNull())
304 {
305 aCtx->ShaderManager()->SetLocalOrigin (theOrigin);
306 }
307}
308
c357e426 309// =======================================================================
310// function : SetTextureEnv
311// purpose :
312// =======================================================================
313void OpenGl_View::SetTextureEnv (const Handle(Graphic3d_TextureEnv)& theTextureEnv)
314{
315 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
316 if (!aCtx.IsNull() && !myTextureEnv.IsNull())
317 {
cc8cbabe 318 for (OpenGl_TextureSet::Iterator aTextureIter (myTextureEnv); aTextureIter.More(); aTextureIter.Next())
319 {
320 aCtx->DelayedRelease (aTextureIter.ChangeValue());
321 aTextureIter.ChangeValue().Nullify();
322 }
c357e426 323 }
324
325 myToUpdateEnvironmentMap = Standard_True;
326 myTextureEnvData = theTextureEnv;
327 myTextureEnv.Nullify();
328 initTextureEnv (aCtx);
329}
330
331// =======================================================================
332// function : initTextureEnv
333// purpose :
334// =======================================================================
335void OpenGl_View::initTextureEnv (const Handle(OpenGl_Context)& theContext)
336{
337 if (myTextureEnvData.IsNull()
338 || theContext.IsNull()
339 || !theContext->MakeCurrent())
bf75be98 340 {
341 return;
342 }
343
72f6dc61 344 Handle(OpenGl_Texture) aTextureEnv = new OpenGl_Texture (myTextureEnvData->GetId(), myTextureEnvData->GetParams());
faff3767 345 if (Handle(Image_PixMap) anImage = myTextureEnvData->GetImage (theContext->SupportedTextureFormats()))
c357e426 346 {
ba00aab7 347 aTextureEnv->Init (theContext, *anImage, myTextureEnvData->Type(), true);
c357e426 348 }
72f6dc61 349 myTextureEnv = new OpenGl_TextureSet (aTextureEnv);
350 myTextureEnv->ChangeTextureSetBits() = Graphic3d_TextureSetBits_BaseColor;
c357e426 351}
e276548b 352
c357e426 353// =======================================================================
354// function : SetImmediateModeDrawToFront
355// purpose :
356// =======================================================================
357Standard_Boolean OpenGl_View::SetImmediateModeDrawToFront (const Standard_Boolean theDrawToFrontBuffer)
358{
359 const Standard_Boolean aPrevMode = myTransientDrawToFront;
360 myTransientDrawToFront = theDrawToFrontBuffer;
361 return aPrevMode;
e276548b 362}
363
1220d98e 364// =======================================================================
365// function : Window
366// purpose :
367// =======================================================================
368Handle(Aspect_Window) OpenGl_View::Window() const
369{
370 return myWindow->PlatformWindow();
371}
372
c357e426 373// =======================================================================
374// function : SetWindow
375// purpose :
376// =======================================================================
377void OpenGl_View::SetWindow (const Handle(Aspect_Window)& theWindow,
a521d90d 378 const Aspect_RenderingContext theContext)
e276548b 379{
c357e426 380 myWindow = myDriver->CreateRenderWindow (theWindow, theContext);
381 Standard_ASSERT_RAISE (!myWindow.IsNull(),
382 "OpenGl_View::SetWindow, "
383 "Failed to create OpenGl window.");
e276548b 384
c357e426 385 myWorkspace = new OpenGl_Workspace (this, myWindow);
386 myWorldViewProjState.Reset();
91c60b57 387 myToUpdateEnvironmentMap = Standard_True;
c357e426 388 myHasFboBlit = Standard_True;
389 Invalidate();
390
391 // Environment texture resource does not support lazy initialization.
392 initTextureEnv (myWorkspace->GetGlContext());
393}
394
395// =======================================================================
396// function : Resized
397// purpose :
398// =======================================================================
399void OpenGl_View::Resized()
400{
401 if (myWindow.IsNull())
402 return;
403
404 myWindow->Resize();
405}
406
c357e426 407// =======================================================================
408// function : SetMinMaxValuesCallback
409// purpose :
410// =======================================================================
411static void SetMinMaxValuesCallback (Graphic3d_CView* theView)
412{
413 OpenGl_View* aView = dynamic_cast<OpenGl_View*>(theView);
414 if (aView == NULL)
415 return;
416
417 Bnd_Box aBox = theView->MinMaxValues();
418 if (!aBox.IsVoid())
2166f0fa 419 {
c357e426 420 gp_Pnt aMin = aBox.CornerMin();
421 gp_Pnt aMax = aBox.CornerMax();
422
423 Graphic3d_Vec3 aMinVec ((Standard_ShortReal )aMin.X(), (Standard_ShortReal )aMin.Y(), (Standard_ShortReal )aMin.Z());
424 Graphic3d_Vec3 aMaxVec ((Standard_ShortReal )aMax.X(), (Standard_ShortReal )aMax.Y(), (Standard_ShortReal )aMax.Z());
425 aView->GraduatedTrihedronMinMaxValues (aMinVec, aMaxVec);
2166f0fa
SK
426 }
427}
428
c357e426 429// =======================================================================
430// function : GraduatedTrihedronDisplay
431// purpose :
432// =======================================================================
433void OpenGl_View::GraduatedTrihedronDisplay (const Graphic3d_GraduatedTrihedron& theTrihedronData)
434{
435 myGTrihedronData = theTrihedronData;
1b5eb2be 436 myGTrihedronData.SetCubicAxesCallback (SetMinMaxValuesCallback);
c357e426 437 myGraduatedTrihedron.SetValues (myGTrihedronData);
438 myToShowGradTrihedron = true;
439}
2166f0fa 440
c357e426 441// =======================================================================
442// function : GraduatedTrihedronErase
443// purpose :
444// =======================================================================
445void OpenGl_View::GraduatedTrihedronErase()
2166f0fa 446{
c357e426 447 myGraduatedTrihedron.Release (myWorkspace->GetGlContext().operator->());
448 myToShowGradTrihedron = false;
2166f0fa
SK
449}
450
c357e426 451// =======================================================================
452// function : GraduatedTrihedronMinMaxValues
453// purpose :
454// =======================================================================
455void OpenGl_View::GraduatedTrihedronMinMaxValues (const Graphic3d_Vec3 theMin, const Graphic3d_Vec3 theMax)
456{
457 myGraduatedTrihedron.SetMinMax (theMin, theMax);
458}
2166f0fa 459
c357e426 460// =======================================================================
461// function : BufferDump
462// purpose :
463// =======================================================================
464Standard_Boolean OpenGl_View::BufferDump (Image_PixMap& theImage, const Graphic3d_BufferType& theBufferType)
465{
38d90bb3 466 if (theBufferType != Graphic3d_BT_RGB_RayTraceHdrLeft)
467 {
468 return myWorkspace->BufferDump(myFBO, theImage, theBufferType);
469 }
470
471 if (!myRaytraceParameters.AdaptiveScreenSampling)
472 {
473 return myWorkspace->BufferDump(myAccumFrames % 2 ? myRaytraceFBO2[0] : myRaytraceFBO1[0], theImage, theBufferType);
474 }
475
476#if defined(GL_ES_VERSION_2_0)
477 return false;
478#else
479 if (theImage.Format() != Image_Format_RGBF)
480 {
481 return false;
482 }
483
484 const GLuint aW = myRaytraceOutputTexture[0]->SizeX();
485 const GLuint aH = myRaytraceOutputTexture[0]->SizeY();
486 if (aW / 3 != theImage.SizeX() || aH / 2 != theImage.SizeY())
487 {
488 return false;
489 }
490
491 std::vector<GLfloat> aValues;
492 try
493 {
494 aValues.resize (aW * aH);
495 }
496 catch (const std::bad_alloc&)
497 {
498 return false;
499 }
500
501 glBindTexture (GL_TEXTURE_RECTANGLE, myRaytraceOutputTexture[0]->TextureId());
502 glGetTexImage (GL_TEXTURE_RECTANGLE, 0, OpenGl_TextureFormat::Create<GLfloat, 1>().Format(), GL_FLOAT, &aValues[0]);
503 glBindTexture (GL_TEXTURE_RECTANGLE, 0);
504 for (unsigned int aRow = 0; aRow < aH; aRow += 2)
505 {
506 for (unsigned int aCol = 0; aCol < aW; aCol += 3)
507 {
508 float* anImageValue = theImage.ChangeValue<float[3]> ((aH - aRow) / 2 - 1, aCol / 3);
509 float aInvNbSamples = 1.f / aValues[aRow * aW + aCol + aW];
510 anImageValue[0] = aValues[aRow * aW + aCol] * aInvNbSamples;
511 anImageValue[1] = aValues[aRow * aW + aCol + 1] * aInvNbSamples;
512 anImageValue[2] = aValues[aRow * aW + aCol + 1 + aW] * aInvNbSamples;
513 }
514 }
515
516 return true;
517#endif
c357e426 518}
519
c357e426 520// =======================================================================
521// function : GradientBackground
522// purpose :
523// =======================================================================
524Aspect_GradientBackground OpenGl_View::GradientBackground() const
525{
526 Quantity_Color aColor1, aColor2;
077a220c 527 aColor1.SetValues (myBackgrounds[Graphic3d_TOB_GRADIENT]->GradientColor (0).r(),
528 myBackgrounds[Graphic3d_TOB_GRADIENT]->GradientColor (0).g(),
529 myBackgrounds[Graphic3d_TOB_GRADIENT]->GradientColor (0).b(), Quantity_TOC_RGB);
530 aColor2.SetValues (myBackgrounds[Graphic3d_TOB_GRADIENT]->GradientColor (1).r(),
531 myBackgrounds[Graphic3d_TOB_GRADIENT]->GradientColor (1).g(),
532 myBackgrounds[Graphic3d_TOB_GRADIENT]->GradientColor (1).b(), Quantity_TOC_RGB);
533 return Aspect_GradientBackground (aColor1, aColor2, myBackgrounds[Graphic3d_TOB_GRADIENT]->GradientFillMethod());
c357e426 534}
2166f0fa 535
c357e426 536// =======================================================================
537// function : SetGradientBackground
538// purpose :
539// =======================================================================
540void OpenGl_View::SetGradientBackground (const Aspect_GradientBackground& theBackground)
2166f0fa 541{
c357e426 542 Quantity_Color aColor1, aColor2;
543 theBackground.Colors (aColor1, aColor2);
077a220c 544 myBackgrounds[Graphic3d_TOB_GRADIENT]->SetGradientParameters (aColor1, aColor2, theBackground.BgGradientFillMethod());
545
546 myBackgroundType = Graphic3d_TOB_GRADIENT;
c357e426 547}
548
549// =======================================================================
550// function : SetBackgroundImage
551// purpose :
552// =======================================================================
99289bed 553void OpenGl_View::SetBackgroundImage (const Handle(Graphic3d_TextureMap)& theTextureMap,
554 Standard_Boolean theToUpdatePBREnv)
c357e426 555{
99289bed 556 if (theTextureMap.IsNull()
557 || !theTextureMap->IsDone())
558 {
559 if (myBackgroundType == Graphic3d_TOB_TEXTURE
560 || myBackgroundType == Graphic3d_TOB_CUBEMAP)
561 {
562 myBackgroundType = Graphic3d_TOB_NONE;
563 if (theToUpdatePBREnv)
564 {
565 myPBREnvRequest = OpenGl_PBREnvRequest_CLEAR;
566 }
567 }
568 return;
569 }
570
c357e426 571 Handle(Graphic3d_AspectFillArea3d) anAspect = new Graphic3d_AspectFillArea3d();
99289bed 572 Handle(Graphic3d_TextureSet) aTextureSet = new Graphic3d_TextureSet (theTextureMap);
c357e426 573 anAspect->SetInteriorStyle (Aspect_IS_SOLID);
7fd4958d 574 anAspect->SetFaceCulling (Graphic3d_TypeOfBackfacingModel_DoubleSided);
99289bed 575 anAspect->SetShadingModel (Graphic3d_TOSM_UNLIT);
576 anAspect->SetTextureSet (aTextureSet);
577 anAspect->SetTextureMapOn (true);
578
579 if (Handle(Graphic3d_Texture2D) aTextureMap = Handle(Graphic3d_Texture2D)::DownCast (theTextureMap))
2166f0fa 580 {
99289bed 581 if (theToUpdatePBREnv && myBackgroundType == Graphic3d_TOB_CUBEMAP)
582 {
583 myPBREnvRequest = OpenGl_PBREnvRequest_CLEAR;
584 }
585
586 myTextureParams->SetAspect (anAspect);
587 myBackgroundType = Graphic3d_TOB_TEXTURE;
588 myBackgroundImage = aTextureMap;
589 return;
2166f0fa 590 }
99289bed 591
592 if (Handle(Graphic3d_CubeMap) aCubeMap = Handle(Graphic3d_CubeMap)::DownCast (theTextureMap))
2166f0fa 593 {
99289bed 594 if (theToUpdatePBREnv)
595 {
596 myPBREnvRequest = OpenGl_PBREnvRequest_BAKE;
597 }
598
599 aCubeMap->SetMipmapsGeneration (Standard_True);
600 if (const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext())
601 {
602 anAspect->SetShaderProgram (aCtx->ShaderManager()->GetBgCubeMapProgram());
603 }
604
605 myCubeMapParams->SetAspect (anAspect);
606
607 const OpenGl_Aspects* anAspectsBackup = myWorkspace->SetAspects (myCubeMapParams);
608 myWorkspace->ApplyAspects();
609 myWorkspace->SetAspects (anAspectsBackup);
610 myWorkspace->ApplyAspects();
611
612 myBackgroundType = Graphic3d_TOB_CUBEMAP;
613 myBackgroundCubeMap = aCubeMap;
c357e426 614 return;
615 }
2166f0fa 616
99289bed 617 throw Standard_ProgramError ("OpenGl_View::SetBackgroundImage() - invalid texture map set for background");
c357e426 618}
2166f0fa 619
c357e426 620// =======================================================================
621// function : BackgroundImageStyle
622// purpose :
623// =======================================================================
624Aspect_FillMethod OpenGl_View::BackgroundImageStyle() const
625{
077a220c 626 return myBackgrounds[Graphic3d_TOB_TEXTURE]->TextureFillMethod();
2166f0fa
SK
627}
628
c357e426 629// =======================================================================
630// function : SetBackgroundImageStyle
631// purpose :
632// =======================================================================
633void OpenGl_View::SetBackgroundImageStyle (const Aspect_FillMethod theFillStyle)
634{
077a220c 635 myBackgrounds[Graphic3d_TOB_TEXTURE]->SetTextureFillMethod (theFillStyle);
636}
637
638// =======================================================================
639// function : BackgroundCubeMap
640// purpose :
641// =======================================================================
642Handle(Graphic3d_CubeMap) OpenGl_View::BackgroundCubeMap() const
643{
644 return myBackgroundCubeMap;
645}
67312b79 646
647// =======================================================================
648// function : SpecIBLMapLevels
649// purpose :
650// =======================================================================
651unsigned int OpenGl_View::SpecIBLMapLevels() const
652{
653 return myPBREnvironment.IsNull() ? 0 : myPBREnvironment->SpecMapLevelsNumber();
654}
2166f0fa 655
c357e426 656//=======================================================================
1c728f2d 657//function : InsertLayerBefore
c357e426 658//purpose :
659//=======================================================================
1c728f2d 660void OpenGl_View::InsertLayerBefore (const Graphic3d_ZLayerId theLayerId,
077a220c 661 const Graphic3d_ZLayerSettings& theSettings,
662 const Graphic3d_ZLayerId theLayerAfter)
2166f0fa 663{
1c728f2d 664 myZLayers.InsertLayerBefore (theLayerId, theSettings, theLayerAfter);
665}
666
667//=======================================================================
668//function : InsertLayerAfter
669//purpose :
670//=======================================================================
671void OpenGl_View::InsertLayerAfter (const Graphic3d_ZLayerId theLayerId,
077a220c 672 const Graphic3d_ZLayerSettings& theSettings,
673 const Graphic3d_ZLayerId theLayerBefore)
1c728f2d 674{
675 myZLayers.InsertLayerAfter (theLayerId, theSettings, theLayerBefore);
2166f0fa
SK
676}
677
c357e426 678//=======================================================================
679//function : RemoveZLayer
680//purpose :
681//=======================================================================
682void OpenGl_View::RemoveZLayer (const Graphic3d_ZLayerId theLayerId)
683{
684 myZLayers.RemoveLayer (theLayerId);
685}
2166f0fa 686
c357e426 687//=======================================================================
688//function : SetZLayerSettings
689//purpose :
690//=======================================================================
691void OpenGl_View::SetZLayerSettings (const Graphic3d_ZLayerId theLayerId,
692 const Graphic3d_ZLayerSettings& theSettings)
2166f0fa 693{
c357e426 694 myZLayers.SetLayerSettings (theLayerId, theSettings);
2166f0fa
SK
695}
696
50d06d8f 697//=======================================================================
698//function : ZLayerMax
699//purpose :
700//=======================================================================
701Standard_Integer OpenGl_View::ZLayerMax() const
702{
703 Standard_Integer aLayerMax = Graphic3d_ZLayerId_Default;
1c728f2d 704 for (NCollection_List<Handle(Graphic3d_Layer)>::Iterator aLayerIter (myZLayers.Layers()); aLayerIter.More(); aLayerIter.Next())
50d06d8f 705 {
1c728f2d 706 aLayerMax = Max (aLayerMax, aLayerIter.Value()->LayerId());
50d06d8f 707 }
50d06d8f 708 return aLayerMax;
709}
710
711//=======================================================================
1c728f2d 712//function : Layers
713//purpose :
714//=======================================================================
715const NCollection_List<Handle(Graphic3d_Layer)>& OpenGl_View::Layers() const
716{
717 return myZLayers.Layers();
718}
719
720//=======================================================================
721//function : Layer
50d06d8f 722//purpose :
723//=======================================================================
1c728f2d 724Handle(Graphic3d_Layer) OpenGl_View::Layer (const Graphic3d_ZLayerId theLayerId) const
50d06d8f 725{
1c728f2d 726 Handle(Graphic3d_Layer) aLayer;
727 if (theLayerId != Graphic3d_ZLayerId_UNKNOWN)
50d06d8f 728 {
1c728f2d 729 myZLayers.LayerIDs().Find (theLayerId, aLayer);
50d06d8f 730 }
1c728f2d 731 return aLayer;
50d06d8f 732}
733
734//=======================================================================
1c728f2d 735//function : MinMaxValues
50d06d8f 736//purpose :
737//=======================================================================
1c728f2d 738Bnd_Box OpenGl_View::MinMaxValues (const Standard_Boolean theToIncludeAuxiliary) const
50d06d8f 739{
1c728f2d 740 if (!IsDefined())
50d06d8f 741 {
1c728f2d 742 return Bnd_Box();
50d06d8f 743 }
744
1c728f2d 745 Bnd_Box aBox = base_type::MinMaxValues (theToIncludeAuxiliary);
746
71d8ccc7 747 return aBox;
50d06d8f 748}
749
750//=======================================================================
c357e426 751//function : FBO
752//purpose :
753//=======================================================================
b128c892 754Handle(Standard_Transient) OpenGl_View::FBO() const
c357e426 755{
b128c892 756 return Handle(Standard_Transient)(myFBO);
c357e426 757}
2166f0fa 758
c357e426 759//=======================================================================
760//function : SetFBO
761//purpose :
762//=======================================================================
b128c892 763void OpenGl_View::SetFBO (const Handle(Standard_Transient)& theFbo)
2166f0fa 764{
b128c892 765 myFBO = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
2166f0fa
SK
766}
767
c357e426 768//=======================================================================
769//function : FBOCreate
770//purpose :
771//=======================================================================
b128c892 772Handle(Standard_Transient) OpenGl_View::FBOCreate (const Standard_Integer theWidth,
773 const Standard_Integer theHeight)
c357e426 774{
775 return myWorkspace->FBOCreate (theWidth, theHeight);
776}
2166f0fa 777
c357e426 778//=======================================================================
779//function : FBORelease
780//purpose :
781//=======================================================================
b128c892 782void OpenGl_View::FBORelease (Handle(Standard_Transient)& theFbo)
2166f0fa 783{
b128c892 784 Handle(OpenGl_FrameBuffer) aFrameBuffer = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
785 if (aFrameBuffer.IsNull())
786 {
787 return;
788 }
789
790 myWorkspace->FBORelease (aFrameBuffer);
791 theFbo.Nullify();
c357e426 792}
793
794//=======================================================================
795//function : FBOGetDimensions
796//purpose :
797//=======================================================================
b128c892 798void OpenGl_View::FBOGetDimensions (const Handle(Standard_Transient)& theFbo,
c357e426 799 Standard_Integer& theWidth,
800 Standard_Integer& theHeight,
801 Standard_Integer& theWidthMax,
802 Standard_Integer& theHeightMax)
803{
b128c892 804 const Handle(OpenGl_FrameBuffer) aFrameBuffer = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
805 if (aFrameBuffer.IsNull())
806 {
807 return;
808 }
809
c357e426 810 theWidth = aFrameBuffer->GetVPSizeX(); // current viewport size
811 theHeight = aFrameBuffer->GetVPSizeY();
812 theWidthMax = aFrameBuffer->GetSizeX(); // texture size
813 theHeightMax = aFrameBuffer->GetSizeY();
814}
815
816//=======================================================================
817//function : FBOChangeViewport
818//purpose :
819//=======================================================================
b128c892 820void OpenGl_View::FBOChangeViewport (const Handle(Standard_Transient)& theFbo,
c357e426 821 const Standard_Integer theWidth,
822 const Standard_Integer theHeight)
823{
b128c892 824 const Handle(OpenGl_FrameBuffer) aFrameBuffer = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
825 if (aFrameBuffer.IsNull())
826 {
827 return;
828 }
829
c357e426 830 aFrameBuffer->ChangeViewport (theWidth, theHeight);
831}
832
c357e426 833//=======================================================================
834//function : displayStructure
835//purpose :
836//=======================================================================
837void OpenGl_View::displayStructure (const Handle(Graphic3d_CStructure)& theStructure,
838 const Standard_Integer thePriority)
839{
7d47ebdb 840 const OpenGl_Structure* aStruct = static_cast<const OpenGl_Structure*> (theStructure.get());
c357e426 841 const Graphic3d_ZLayerId aZLayer = aStruct->ZLayer();
842 myZLayers.AddStructure (aStruct, aZLayer, thePriority);
843}
844
845//=======================================================================
846//function : eraseStructure
847//purpose :
848//=======================================================================
849void OpenGl_View::eraseStructure (const Handle(Graphic3d_CStructure)& theStructure)
850{
7d47ebdb 851 const OpenGl_Structure* aStruct = static_cast<const OpenGl_Structure*> (theStructure.get());
c357e426 852 myZLayers.RemoveStructure (aStruct);
853}
854
855//=======================================================================
856//function : changeZLayer
857//purpose :
858//=======================================================================
859void OpenGl_View::changeZLayer (const Handle(Graphic3d_CStructure)& theStructure,
860 const Graphic3d_ZLayerId theNewLayerId)
861{
862 const Graphic3d_ZLayerId anOldLayer = theStructure->ZLayer();
7d47ebdb 863 const OpenGl_Structure* aStruct = static_cast<const OpenGl_Structure*> (theStructure.get());
c357e426 864 myZLayers.ChangeLayer (aStruct, anOldLayer, theNewLayerId);
cfece3ef 865 Update (anOldLayer);
866 Update (theNewLayerId);
c357e426 867}
868
869//=======================================================================
870//function : changePriority
871//purpose :
872//=======================================================================
873void OpenGl_View::changePriority (const Handle(Graphic3d_CStructure)& theStructure,
874 const Standard_Integer theNewPriority)
875{
876 const Graphic3d_ZLayerId aLayerId = theStructure->ZLayer();
7d47ebdb 877 const OpenGl_Structure* aStruct = static_cast<const OpenGl_Structure*> (theStructure.get());
c357e426 878 myZLayers.ChangePriority (aStruct, aLayerId, theNewPriority);
2166f0fa 879}
26d9c835 880
881//=======================================================================
882//function : DiagnosticInformation
883//purpose :
884//=======================================================================
885void OpenGl_View::DiagnosticInformation (TColStd_IndexedDataMapOfStringString& theDict,
886 Graphic3d_DiagnosticInfo theFlags) const
887{
b40cdc2b 888 base_type::DiagnosticInformation (theDict, theFlags);
26d9c835 889 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
890 if (!myWorkspace->Activate()
891 || aCtx.IsNull())
892 {
893 return;
894 }
895
896 aCtx->DiagnosticInformation (theDict, theFlags);
897 if ((theFlags & Graphic3d_DiagnosticInfo_FrameBuffer) != 0)
898 {
899 TCollection_AsciiString aResRatio (myRenderParams.ResolutionRatio());
900 theDict.ChangeFromIndex (theDict.Add ("ResolutionRatio", aResRatio)) = aResRatio;
901 }
902}
79b544e6 903
904//=======================================================================
905//function : StatisticInformation
906//purpose :
907//=======================================================================
908void OpenGl_View::StatisticInformation (TColStd_IndexedDataMapOfStringString& theDict) const
909{
910 if (const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext())
911 {
912 const Handle(OpenGl_FrameStats)& aStats = aCtx->FrameStats();
913 const Graphic3d_RenderingParams& aRendParams = myWorkspace->View()->RenderingParams();
914 aStats->FormatStats (theDict, aRendParams.CollectedStats);
915 }
916}
917
918//=======================================================================
919//function : StatisticInformation
920//purpose :
921//=======================================================================
922TCollection_AsciiString OpenGl_View::StatisticInformation() const
923{
924 if (const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext())
925 {
926 const Handle(OpenGl_FrameStats)& aStats = aCtx->FrameStats();
927 const Graphic3d_RenderingParams& aRendParams = myWorkspace->View()->RenderingParams();
928 return aStats->FormatStats (aRendParams.CollectedStats);
929 }
930 return TCollection_AsciiString();
931}
cfb54c0c 932
933//=======================================================================
934//function : drawBackground
935//purpose :
936//=======================================================================
937void OpenGl_View::drawBackground (const Handle(OpenGl_Workspace)& theWorkspace,
938 Graphic3d_Camera::Projection theProjection)
939{
940 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
4b52faa5 941 const bool wasUsedZBuffer = theWorkspace->SetUseZBuffer (false);
cfb54c0c 942 if (wasUsedZBuffer)
943 {
944 aCtx->core11fwd->glDisable (GL_DEPTH_TEST);
945 }
946
4b52faa5 947#ifdef GL_DEPTH_CLAMP
948 const bool wasDepthClamped = aCtx->arbDepthClamp && glIsEnabled (GL_DEPTH_CLAMP);
949 if (aCtx->arbDepthClamp && !wasDepthClamped)
950 {
951 // make sure background is always drawn (workaround skybox rendering on some hardware)
952 aCtx->core11fwd->glEnable (GL_DEPTH_CLAMP);
953 }
954#endif
955
cfb54c0c 956 if (myBackgroundType == Graphic3d_TOB_CUBEMAP)
957 {
958 myCubeMapParams->Aspect()->ShaderProgram()->PushVariableInt ("uZCoeff", myBackgroundCubeMap->ZIsInverted() ? -1 : 1);
959 myCubeMapParams->Aspect()->ShaderProgram()->PushVariableInt ("uYCoeff", myBackgroundCubeMap->IsTopDown() ? 1 : -1);
960 const OpenGl_Aspects* anOldAspectFace = theWorkspace->SetAspects (myCubeMapParams);
961
962 myBackgrounds[Graphic3d_TOB_CUBEMAP]->Render (theWorkspace, theProjection);
963
964 theWorkspace->SetAspects (anOldAspectFace);
965 }
966 else if (myBackgroundType == Graphic3d_TOB_GRADIENT
967 || myBackgroundType == Graphic3d_TOB_TEXTURE)
968 {
969 // Drawing background gradient if:
970 // - gradient fill type is not Aspect_GFM_NONE and
971 // - either background texture is no specified or it is drawn in Aspect_FM_CENTERED mode
972 if (myBackgrounds[Graphic3d_TOB_GRADIENT]->IsDefined()
973 && (!myTextureParams->Aspect()->ToMapTexture()
974 || myBackgrounds[Graphic3d_TOB_TEXTURE]->TextureFillMethod() == Aspect_FM_CENTERED
975 || myBackgrounds[Graphic3d_TOB_TEXTURE]->TextureFillMethod() == Aspect_FM_NONE))
976 {
977 myBackgrounds[Graphic3d_TOB_GRADIENT]->Render(theWorkspace, theProjection);
978 }
979
980 // Drawing background image if it is defined
981 // (texture is defined and fill type is not Aspect_FM_NONE)
982 if (myBackgrounds[Graphic3d_TOB_TEXTURE]->IsDefined()
983 && myTextureParams->Aspect()->ToMapTexture())
984 {
985 aCtx->core11fwd->glDisable (GL_BLEND);
986
987 const OpenGl_Aspects* anOldAspectFace = theWorkspace->SetAspects (myTextureParams);
988 myBackgrounds[Graphic3d_TOB_TEXTURE]->Render (theWorkspace, theProjection);
989 theWorkspace->SetAspects (anOldAspectFace);
990 }
991 }
992
993 if (wasUsedZBuffer)
994 {
995 theWorkspace->SetUseZBuffer (Standard_True);
996 aCtx->core11fwd->glEnable (GL_DEPTH_TEST);
997 }
4b52faa5 998#ifdef GL_DEPTH_CLAMP
999 if (aCtx->arbDepthClamp && !wasDepthClamped)
1000 {
1001 aCtx->core11fwd->glDisable (GL_DEPTH_CLAMP);
1002 }
1003#endif
cfb54c0c 1004}
1005
1006//=======================================================================
1007//function : prepareFrameBuffers
1008//purpose :
1009//=======================================================================
1010bool OpenGl_View::prepareFrameBuffers (Graphic3d_Camera::Projection& theProj)
1011{
1012 theProj = myCamera->ProjectionType();
1013 const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext();
1014
1015 Standard_Integer aSizeX = 0, aSizeY = 0;
1016 OpenGl_FrameBuffer* aFrameBuffer = myFBO.get();
1017 if (aFrameBuffer != NULL)
1018 {
1019 aSizeX = aFrameBuffer->GetVPSizeX();
1020 aSizeY = aFrameBuffer->GetVPSizeY();
1021 }
1022 else if (IsActiveXR())
1023 {
1024 aSizeX = myXRSession->RecommendedViewport().x();
1025 aSizeY = myXRSession->RecommendedViewport().y();
1026 }
1027 else
1028 {
1029 aSizeX = myWindow->Width();
1030 aSizeY = myWindow->Height();
1031 }
1032
1220d98e 1033 const Graphic3d_Vec2i aRendSize (Standard_Integer(myRenderParams.RenderResolutionScale * aSizeX + 0.5f),
1034 Standard_Integer(myRenderParams.RenderResolutionScale * aSizeY + 0.5f));
cfb54c0c 1035 if (aSizeX < 1
1036 || aSizeY < 1
1220d98e 1037 || aRendSize.x() < 1
1038 || aRendSize.y() < 1)
cfb54c0c 1039 {
1040 myBackBufferRestored = Standard_False;
1041 myIsImmediateDrawn = Standard_False;
1042 return false;
1043 }
1044
1045 // determine multisampling parameters
1220d98e 1046 Standard_Integer aNbSamples = !myToDisableMSAA && aSizeX == aRendSize.x()
cfb54c0c 1047 ? Max (Min (myRenderParams.NbMsaaSamples, aCtx->MaxMsaaSamples()), 0)
1048 : 0;
1049 if (aNbSamples != 0)
1050 {
1051 aNbSamples = OpenGl_Context::GetPowerOfTwo (aNbSamples, aCtx->MaxMsaaSamples());
1052 }
1053
78c4e836 1054 bool toUseOit = myRenderParams.TransparencyMethod != Graphic3d_RTM_BLEND_UNORDERED
cfb54c0c 1055 && checkOitCompatibility (aCtx, aNbSamples > 0);
1056
1057 const bool toInitImmediateFbo = myTransientDrawToFront
1058 && (!aCtx->caps->useSystemBuffer || (toUseOit && HasImmediateStructures()));
1059
1060 if ( aFrameBuffer == NULL
1061 && !aCtx->DefaultFrameBuffer().IsNull()
1062 && aCtx->DefaultFrameBuffer()->IsValid())
1063 {
1064 aFrameBuffer = aCtx->DefaultFrameBuffer().operator->();
1065 }
1066
1067 if (myHasFboBlit
1068 && (myTransientDrawToFront
1069 || theProj == Graphic3d_Camera::Projection_Stereo
1070 || aNbSamples != 0
1071 || toUseOit
1220d98e 1072 || aSizeX != aRendSize.x()))
cfb54c0c 1073 {
1220d98e 1074 if (myMainSceneFbos[0]->GetVPSize() != aRendSize
1075 || myMainSceneFbos[0]->NbSamples() != aNbSamples)
cfb54c0c 1076 {
1077 if (!myTransientDrawToFront)
1078 {
1079 myImmediateSceneFbos[0]->Release (aCtx.operator->());
1080 myImmediateSceneFbos[1]->Release (aCtx.operator->());
1081 myImmediateSceneFbos[0]->ChangeViewport (0, 0);
1082 myImmediateSceneFbos[1]->ChangeViewport (0, 0);
1083 }
1084
1085 // prepare FBOs containing main scene
1086 // for further blitting and rendering immediate presentations on top
1087 if (aCtx->core20fwd != NULL)
1088 {
1220d98e 1089 const bool wasFailedMain0 = checkWasFailedFbo (myMainSceneFbos[0], aRendSize.x(), aRendSize.y(), aNbSamples);
1090 if (!myMainSceneFbos[0]->Init (aCtx, aRendSize, myFboColorFormat, myFboDepthFormat, aNbSamples)
cfb54c0c 1091 && !wasFailedMain0)
1092 {
1093 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "Error! Main FBO "
1094 + printFboFormat (myMainSceneFbos[0]) + " initialization has failed";
1095 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
1096 }
1097 }
1098 }
78c4e836 1099
cfb54c0c 1100 if (myMainSceneFbos[0]->IsValid() && (toInitImmediateFbo || myImmediateSceneFbos[0]->IsValid()))
1101 {
1102 const bool wasFailedImm0 = checkWasFailedFbo (myImmediateSceneFbos[0], myMainSceneFbos[0]);
1103 if (!myImmediateSceneFbos[0]->InitLazy (aCtx, *myMainSceneFbos[0])
1104 && !wasFailedImm0)
1105 {
1106 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "Error! Immediate FBO "
1107 + printFboFormat (myImmediateSceneFbos[0]) + " initialization has failed";
1108 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
1109 }
1110 }
1111 }
1112 else
1113 {
1114 myMainSceneFbos [0]->Release (aCtx.operator->());
1115 myMainSceneFbos [1]->Release (aCtx.operator->());
1116 myImmediateSceneFbos[0]->Release (aCtx.operator->());
1117 myImmediateSceneFbos[1]->Release (aCtx.operator->());
1118 myXrSceneFbo ->Release (aCtx.operator->());
1119 myMainSceneFbos [0]->ChangeViewport (0, 0);
1120 myMainSceneFbos [1]->ChangeViewport (0, 0);
1121 myImmediateSceneFbos[0]->ChangeViewport (0, 0);
1122 myImmediateSceneFbos[1]->ChangeViewport (0, 0);
1123 myXrSceneFbo ->ChangeViewport (0, 0);
1124 }
1125
1126 bool hasXRBlitFbo = false;
1127 if (theProj == Graphic3d_Camera::Projection_Stereo
1128 && IsActiveXR()
1129 && myMainSceneFbos[0]->IsValid())
1130 {
1131 if (aNbSamples != 0
1220d98e 1132 || aSizeX != aRendSize.x())
cfb54c0c 1133 {
1220d98e 1134 hasXRBlitFbo = myXrSceneFbo->InitLazy (aCtx, Graphic3d_Vec2i (aSizeX, aSizeY), myFboColorFormat, myFboDepthFormat, 0);
cfb54c0c 1135 if (!hasXRBlitFbo)
1136 {
1137 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "Error! VR FBO "
1138 + printFboFormat (myXrSceneFbo) + " initialization has failed";
1139 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
1140 }
1141 }
1142 }
1143 else if (theProj == Graphic3d_Camera::Projection_Stereo
1144 && myMainSceneFbos[0]->IsValid())
1145 {
1146 const bool wasFailedMain1 = checkWasFailedFbo (myMainSceneFbos[1], myMainSceneFbos[0]);
1147 if (!myMainSceneFbos[1]->InitLazy (aCtx, *myMainSceneFbos[0])
1148 && !wasFailedMain1)
1149 {
1150 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "Error! Main FBO (second) "
1151 + printFboFormat (myMainSceneFbos[1]) + " initialization has failed";
1152 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
1153 }
1154 if (!myMainSceneFbos[1]->IsValid())
1155 {
1156 // no enough memory?
1157 theProj = Graphic3d_Camera::Projection_Perspective;
1158 }
1159 else if (!myTransientDrawToFront)
1160 {
1161 //
1162 }
1163 else if (!aCtx->HasStereoBuffers()
1164 || myRenderParams.StereoMode != Graphic3d_StereoMode_QuadBuffer)
1165 {
1166 const bool wasFailedImm0 = checkWasFailedFbo (myImmediateSceneFbos[0], myMainSceneFbos[0]);
1167 const bool wasFailedImm1 = checkWasFailedFbo (myImmediateSceneFbos[1], myMainSceneFbos[0]);
1168 if (!myImmediateSceneFbos[0]->InitLazy (aCtx, *myMainSceneFbos[0])
1169 && !wasFailedImm0)
1170 {
1171 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "Error! Immediate FBO (first) "
1172 + printFboFormat (myImmediateSceneFbos[0]) + " initialization has failed";
1173 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
1174 }
1175 if (!myImmediateSceneFbos[1]->InitLazy (aCtx, *myMainSceneFbos[0])
1176 && !wasFailedImm1)
1177 {
1178 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "Error! Immediate FBO (first) "
1179 + printFboFormat (myImmediateSceneFbos[1]) + " initialization has failed";
1180 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
1181 }
1182 if (!myImmediateSceneFbos[0]->IsValid()
1183 || !myImmediateSceneFbos[1]->IsValid())
1184 {
1185 theProj = Graphic3d_Camera::Projection_Perspective;
1186 }
1187 }
1188 }
1189 if (!hasXRBlitFbo)
1190 {
1191 myXrSceneFbo->Release (aCtx.get());
1192 myXrSceneFbo->ChangeViewport (0, 0);
1193 }
1194
1195 // process PBR environment
114db5a5 1196 if (myRenderParams.ShadingModel == Graphic3d_TOSM_PBR
1197 || myRenderParams.ShadingModel == Graphic3d_TOSM_PBR_FACET)
cfb54c0c 1198 {
1199 if (!myPBREnvironment.IsNull()
1200 && myPBREnvironment->SizesAreDifferent (myRenderParams.PbrEnvPow2Size,
1201 myRenderParams.PbrEnvSpecMapNbLevels))
1202 {
1203 myPBREnvironment->Release (aCtx.get());
1204 myPBREnvironment.Nullify();
1205 myPBREnvState = OpenGl_PBREnvState_NONEXISTENT;
1206 myPBREnvRequest = OpenGl_PBREnvRequest_BAKE;
1207 ++myLightsRevision;
1208 }
1209
1210 if (myPBREnvState == OpenGl_PBREnvState_NONEXISTENT
1211 && aCtx->HasPBR())
1212 {
1213 myPBREnvironment = OpenGl_PBREnvironment::Create (aCtx, myRenderParams.PbrEnvPow2Size, myRenderParams.PbrEnvSpecMapNbLevels);
1214 myPBREnvState = myPBREnvironment.IsNull() ? OpenGl_PBREnvState_UNAVAILABLE : OpenGl_PBREnvState_CREATED;
1215 if (myPBREnvState == OpenGl_PBREnvState_CREATED)
1216 {
1217 Handle(OpenGl_Texture) anEnvLUT;
1218 static const TCollection_AsciiString THE_SHARED_ENV_LUT_KEY("EnvLUT");
1219 if (!aCtx->GetResource (THE_SHARED_ENV_LUT_KEY, anEnvLUT))
1220 {
1ac837b2 1221 bool toConvertHalfFloat = false;
1222 #if defined(GL_ES_VERSION_2_0)
1223 // GL_RG32F is not texture-filterable format in OpenGL ES without OES_texture_float_linear extension.
1224 // GL_RG16F is texture-filterable since OpenGL ES 3.0 or OpenGL ES 2.0 + OES_texture_half_float_linear.
1225 // OpenGL ES 3.0 allows initialization of GL_RG16F from 32-bit float data, but OpenGL ES 2.0 + OES_texture_half_float does not.
1226 // Note that it is expected that GL_RG16F has enough precision for this table, so that it can be used also on desktop OpenGL.
1227 const bool hasHalfFloat = aCtx->IsGlGreaterEqual (3, 0) || aCtx->CheckExtension ("GL_OES_texture_half_float_linear");
1228 toConvertHalfFloat = !aCtx->IsGlGreaterEqual (3, 0) && hasHalfFloat;
1229 #endif
1230 Image_Format anImgFormat = Image_Format_UNKNOWN;
8f8fe4a9 1231 if (aCtx->arbTexRG)
1ac837b2 1232 {
1233 anImgFormat = toConvertHalfFloat ? Image_Format_RGF_half : Image_Format_RGF;
1234 }
1235 else
1236 {
1237 anImgFormat = toConvertHalfFloat ? Image_Format_RGBAF_half : Image_Format_RGBAF;
1238 }
1239
1240 Handle(Image_PixMap) aPixMap = new Image_PixMap();
1241 if (anImgFormat == Image_Format_RGF)
8f8fe4a9 1242 {
1243 aPixMap->InitWrapper (Image_Format_RGF, (Standard_Byte*)Textures_EnvLUT, Textures_EnvLUTSize, Textures_EnvLUTSize);
1244 }
1245 else
1246 {
1ac837b2 1247 aPixMap->InitZero (anImgFormat, Textures_EnvLUTSize, Textures_EnvLUTSize);
8f8fe4a9 1248 Image_PixMap aPixMapRG;
1249 aPixMapRG.InitWrapper (Image_Format_RGF, (Standard_Byte*)Textures_EnvLUT, Textures_EnvLUTSize, Textures_EnvLUTSize);
8f8fe4a9 1250 for (Standard_Size aRowIter = 0; aRowIter < aPixMapRG.SizeY(); ++aRowIter)
1251 {
1252 for (Standard_Size aColIter = 0; aColIter < aPixMapRG.SizeX(); ++aColIter)
1253 {
1254 const Image_ColorRGF& aPixelRG = aPixMapRG.Value<Image_ColorRGF> (aRowIter, aColIter);
1ac837b2 1255 if (toConvertHalfFloat)
1256 {
1257 NCollection_Vec2<uint16_t>& aPixelRGBA = aPixMap->ChangeValue<NCollection_Vec2<uint16_t>> (aRowIter, aColIter);
1258 aPixelRGBA.x() = Image_PixMap::ConvertToHalfFloat (aPixelRG.r());
1259 aPixelRGBA.y() = Image_PixMap::ConvertToHalfFloat (aPixelRG.g());
1260 }
1261 else
1262 {
1263 Image_ColorRGBAF& aPixelRGBA = aPixMap->ChangeValue<Image_ColorRGBAF> (aRowIter, aColIter);
1264 aPixelRGBA.r() = aPixelRG.r();
1265 aPixelRGBA.g() = aPixelRG.g();
1266 }
8f8fe4a9 1267 }
1268 }
1269 }
1270
cfb54c0c 1271 OpenGl_TextureFormat aTexFormat = OpenGl_TextureFormat::FindFormat (aCtx, aPixMap->Format(), false);
1272 #if defined(GL_ES_VERSION_2_0)
1ac837b2 1273 if (aTexFormat.IsValid()
1274 && hasHalfFloat)
1275 {
1276 aTexFormat.SetInternalFormat (aCtx->arbTexRG ? GL_RG16F : GL_RGBA16F);
1277 }
cfb54c0c 1278 #endif
1ac837b2 1279
1280 Handle(Graphic3d_TextureParams) aParams = new Graphic3d_TextureParams();
1281 aParams->SetFilter (Graphic3d_TOTF_BILINEAR);
1282 aParams->SetRepeat (Standard_False);
1283 aParams->SetTextureUnit (aCtx->PBREnvLUTTexUnit());
1284 anEnvLUT = new OpenGl_Texture(THE_SHARED_ENV_LUT_KEY, aParams);
cfb54c0c 1285 if (!aTexFormat.IsValid()
1286 || !anEnvLUT->Init (aCtx, aTexFormat, Graphic3d_Vec2i((Standard_Integer)Textures_EnvLUTSize), Graphic3d_TOT_2D, aPixMap.get()))
1287 {
1288 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, "Failed allocation of LUT for PBR");
1289 anEnvLUT.Nullify();
1290 }
1291 aCtx->ShareResource (THE_SHARED_ENV_LUT_KEY, anEnvLUT);
1292 }
1293 if (!anEnvLUT.IsNull())
1294 {
1295 anEnvLUT->Bind (aCtx);
1296 }
1297 myWorkspace->ApplyAspects();
1298 }
1299 }
1300 processPBREnvRequest (aCtx);
1301 }
1302
1303 // create color and coverage accumulation buffers required for OIT algorithm
78c4e836 1304 if (toUseOit
1305 && myRenderParams.TransparencyMethod == Graphic3d_RTM_DEPTH_PEELING_OIT)
1306 {
1220d98e 1307 if (myDepthPeelingFbos->BlendBackFboOit()->GetSize() != aRendSize)
78c4e836 1308 {
1220d98e 1309 if (myDepthPeelingFbos->BlendBackFboOit()->Init (aCtx, aRendSize, GL_RGBA16F, 0))
78c4e836 1310 {
1311 for (int aPairIter = 0; aPairIter < 2; ++aPairIter)
1312 {
1313 OpenGl_ColorFormats aColorFormats;
1314 aColorFormats.Append (GL_RG32F);
1315 aColorFormats.Append (GL_RGBA16F);
1316 aColorFormats.Append (GL_RGBA16F);
1220d98e 1317 myDepthPeelingFbos->DepthPeelFbosOit()[aPairIter]->Init (aCtx, aRendSize, aColorFormats, 0);
78c4e836 1318
1319 NCollection_Sequence<Handle(OpenGl_Texture)> anAttachments;
1320 anAttachments.Append (myDepthPeelingFbos->DepthPeelFbosOit()[aPairIter]->ColorTexture (1));
1321 anAttachments.Append (myDepthPeelingFbos->DepthPeelFbosOit()[aPairIter]->ColorTexture (2));
1322 myDepthPeelingFbos->FrontBackColorFbosOit()[aPairIter]->InitWrapper (aCtx, anAttachments);
1323 }
1324 }
1325 else
1326 {
1327 toUseOit = false;
1328 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH,
1329 "Initialization of float texture framebuffer for use with\n"
1330 " Depth-Peeling order-independent transparency rendering algorithm has failed.");
1331 }
1332 }
1333 }
1334 if (!toUseOit)
1335 {
1336 myDepthPeelingFbos->Release (aCtx.operator->());
1337 }
1338
1339 if (toUseOit
1340 && myRenderParams.TransparencyMethod == Graphic3d_RTM_BLEND_OIT)
cfb54c0c 1341 {
1342 Standard_Integer anFboIt = 0;
1343 for (; anFboIt < 2; ++anFboIt)
1344 {
1345 Handle(OpenGl_FrameBuffer)& aMainSceneFbo = myMainSceneFbos [anFboIt];
1346 Handle(OpenGl_FrameBuffer)& aMainSceneFboOit = myMainSceneFbosOit [anFboIt];
1347 Handle(OpenGl_FrameBuffer)& anImmediateSceneFbo = myImmediateSceneFbos [anFboIt];
1348 Handle(OpenGl_FrameBuffer)& anImmediateSceneFboOit = myImmediateSceneFbosOit[anFboIt];
1349 if (aMainSceneFbo->IsValid()
1220d98e 1350 && (aMainSceneFboOit->GetVPSize() != aRendSize
1351 || aMainSceneFboOit->NbSamples() != aNbSamples))
cfb54c0c 1352 {
1353 Standard_Integer aColorConfig = 0;
1354 for (;;) // seemly responding to driver limitation (GL_FRAMEBUFFER_UNSUPPORTED)
1355 {
1356 if (myFboOitColorConfig.IsEmpty())
1357 {
1358 if (!chooseOitColorConfiguration (aCtx, aColorConfig++, myFboOitColorConfig))
1359 {
1360 break;
1361 }
1362 }
1220d98e 1363 if (aMainSceneFboOit->Init (aCtx, aRendSize, myFboOitColorConfig, aMainSceneFbo->DepthStencilTexture(), aNbSamples))
cfb54c0c 1364 {
1365 break;
1366 }
1367 myFboOitColorConfig.Clear();
1368 }
1369 if (!aMainSceneFboOit->IsValid())
1370 {
1371 break;
1372 }
1373 }
1374 else if (!aMainSceneFbo->IsValid())
1375 {
1376 aMainSceneFboOit->Release (aCtx.operator->());
1377 aMainSceneFboOit->ChangeViewport (0, 0);
1378 }
1379
1380 if (anImmediateSceneFbo->IsValid()
1220d98e 1381 && (anImmediateSceneFboOit->GetVPSize() != aRendSize
1382 || anImmediateSceneFboOit->NbSamples() != aNbSamples))
cfb54c0c 1383 {
1220d98e 1384 if (!anImmediateSceneFboOit->Init (aCtx, aRendSize, myFboOitColorConfig,
cfb54c0c 1385 anImmediateSceneFbo->DepthStencilTexture(), aNbSamples))
1386 {
1387 break;
1388 }
1389 }
1390 else if (!anImmediateSceneFbo->IsValid())
1391 {
1392 anImmediateSceneFboOit->Release (aCtx.operator->());
1393 anImmediateSceneFboOit->ChangeViewport (0, 0);
1394 }
1395 }
1396 if (anFboIt == 0) // only the first OIT framebuffer is mandatory
1397 {
1398 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH,
1399 "Initialization of float texture framebuffer for use with\n"
1400 " blended order-independent transparency rendering algorithm has failed.\n"
1401 " Blended order-independent transparency will not be available.\n");
1402 if (aNbSamples > 0)
1403 {
1404 myToDisableOITMSAA = Standard_True;
1405 }
1406 else
1407 {
1408 myToDisableOIT = Standard_True;
1409 }
1410 toUseOit = false;
1411 }
1412 }
1413 if (!toUseOit && myMainSceneFbosOit[0]->IsValid())
1414 {
78c4e836 1415 myDepthPeelingFbos->Release (aCtx.operator->());
cfb54c0c 1416 myMainSceneFbosOit [0]->Release (aCtx.operator->());
1417 myMainSceneFbosOit [1]->Release (aCtx.operator->());
1418 myImmediateSceneFbosOit[0]->Release (aCtx.operator->());
1419 myImmediateSceneFbosOit[1]->Release (aCtx.operator->());
1420 myMainSceneFbosOit [0]->ChangeViewport (0, 0);
1421 myMainSceneFbosOit [1]->ChangeViewport (0, 0);
1422 myImmediateSceneFbosOit[0]->ChangeViewport (0, 0);
1423 myImmediateSceneFbosOit[1]->ChangeViewport (0, 0);
1424 }
1425
d84e8669 1426 // allocate shadow maps
114db5a5 1427 const Handle(Graphic3d_LightSet)& aLights = myRenderParams.ShadingModel == Graphic3d_TOSM_UNLIT ? myNoShadingLight : myLights;
d84e8669 1428 if (!aLights.IsNull())
1429 {
1430 aLights->UpdateRevision();
1431 }
1432 bool toUseShadowMap = myRenderParams.IsShadowEnabled
1433 && myRenderParams.ShadowMapResolution > 0
1434 && !myLights.IsNull()
1435 && myLights->NbCastShadows() > 0
1436 && myRenderParams.Method != Graphic3d_RM_RAYTRACING;
1437 if (toUseShadowMap)
1438 {
1439 if (myShadowMaps->Size() != myLights->NbCastShadows())
1440 {
1441 myShadowMaps->Release (aCtx.get());
1442 myShadowMaps->Resize (0, myLights->NbCastShadows() - 1, true);
1443 }
1444
1445 const GLint aSamplFrom = GLint(aCtx->ShadowMapTexUnit()) - myLights->NbCastShadows() + 1;
1446 for (Standard_Integer aShadowIter = 0; aShadowIter < myShadowMaps->Size(); ++aShadowIter)
1447 {
1448 Handle(OpenGl_ShadowMap)& aShadow = myShadowMaps->ChangeValue (aShadowIter);
1449 if (aShadow.IsNull())
1450 {
1451 aShadow = new OpenGl_ShadowMap();
1452 }
1453 aShadow->SetShadowMapBias (myRenderParams.ShadowMapBias);
1454 aShadow->Texture()->Sampler()->Parameters()->SetTextureUnit ((Graphic3d_TextureUnit )(aSamplFrom + aShadowIter));
1455
1456 const Handle(OpenGl_FrameBuffer)& aShadowFbo = aShadow->FrameBuffer();
1457 if (aShadowFbo->GetVPSizeX() != myRenderParams.ShadowMapResolution
1458 && toUseShadowMap)
1459 {
1460 OpenGl_ColorFormats aDummy;
1220d98e 1461 if (!aShadowFbo->Init (aCtx, Graphic3d_Vec2i (myRenderParams.ShadowMapResolution), aDummy, myFboDepthFormat, 0))
d84e8669 1462 {
1463 toUseShadowMap = false;
1464 }
1465 }
1466 }
1467 }
1468 if (!toUseShadowMap && myShadowMaps->IsValid())
1469 {
1470 myShadowMaps->Release (aCtx.get());
1471 }
1472
cfb54c0c 1473 return true;
1474}
1475
1476//=======================================================================
1477//function : Redraw
1478//purpose :
1479//=======================================================================
1480void OpenGl_View::Redraw()
1481{
1482 const Standard_Boolean wasDisabledMSAA = myToDisableMSAA;
1483 const Standard_Boolean hadFboBlit = myHasFboBlit;
1484 if (myRenderParams.Method == Graphic3d_RM_RAYTRACING
1485 && !myCaps->vboDisable
1486 && !myCaps->keepArrayData)
1487 {
1488 // caps are shared across all views, thus we need to invalidate all of them
1489 // if (myWasRedrawnGL) { myStructureManager->SetDeviceLost(); }
1490 myDriver->setDeviceLost();
1491 myCaps->keepArrayData = Standard_True;
1492 }
1493
1494 if (!myWorkspace->Activate())
1495 {
1496 return;
1497 }
1498
1499 // implicitly disable VSync when using HMD composer (can be mirrored in window for debugging)
1500 myWindow->SetSwapInterval (IsActiveXR());
1501
1502 ++myFrameCounter;
1503 const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext();
1504 aCtx->FrameStats()->FrameStart (myWorkspace->View(), false);
1505 aCtx->SetLineFeather (myRenderParams.LineFeather);
1506
1507 const Standard_Integer anSRgbState = aCtx->ToRenderSRGB() ? 1 : 0;
1508 if (mySRgbState != -1
1509 && mySRgbState != anSRgbState)
1510 {
1511 releaseSrgbResources (aCtx);
1512 initTextureEnv (aCtx);
1513 }
1514 mySRgbState = anSRgbState;
1515 aCtx->ShaderManager()->UpdateSRgbState();
1516
1517 // release pending GL resources
1518 aCtx->ReleaseDelayed();
1519
1520 // fetch OpenGl context state
1521 aCtx->FetchState();
1522
1523 const Graphic3d_StereoMode aStereoMode = myRenderParams.StereoMode;
1524 Graphic3d_Camera::Projection aProjectType = myCamera->ProjectionType();
1525 if (!prepareFrameBuffers (aProjectType))
1526 {
1527 myBackBufferRestored = Standard_False;
1528 myIsImmediateDrawn = Standard_False;
1529 return;
1530 }
1531
d84e8669 1532 // draw shadow maps
1533 if (myShadowMaps->IsValid())
1534 {
1535 Standard_Integer aShadowIndex = myShadowMaps->Lower();
1536 for (Graphic3d_LightSet::Iterator aLightIter (myLights, Graphic3d_LightSet::IterationFilter_ActiveShadowCasters);
1537 aLightIter.More(); aLightIter.Next())
1538 {
1539 const Handle(Graphic3d_CLight)& aLight = aLightIter.Value();
1540 if (aLight->ToCastShadows())
1541 {
1542 const Handle(OpenGl_ShadowMap)& aShadowMap = myShadowMaps->ChangeValue (aShadowIndex);
1543 aShadowMap->SetLightSource (aLight);
1544 renderShadowMap (aShadowMap);
1545 ++aShadowIndex;
1546 }
1547 }
1548 for (; aShadowIndex <= myShadowMaps->Upper(); ++aShadowIndex)
1549 {
1550 myShadowMaps->ChangeValue (aShadowIndex)->SetLightSource (Handle(Graphic3d_CLight)());
1551 }
1552 }
1553
cfb54c0c 1554 OpenGl_FrameBuffer* aFrameBuffer = myFBO.get();
1555 bool toSwap = aCtx->IsRender()
1556 && !aCtx->caps->buffersNoSwap
1557 && aFrameBuffer == NULL
1558 && (!IsActiveXR() || myRenderParams.ToMirrorComposer);
1559 if ( aFrameBuffer == NULL
1560 && !aCtx->DefaultFrameBuffer().IsNull()
1561 && aCtx->DefaultFrameBuffer()->IsValid())
1562 {
1563 aFrameBuffer = aCtx->DefaultFrameBuffer().operator->();
1564 }
1565
1566 if (aProjectType == Graphic3d_Camera::Projection_Stereo)
1567 {
1568 OpenGl_FrameBuffer* aMainFbos[2] =
1569 {
1570 myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL,
1571 myMainSceneFbos[1]->IsValid() ? myMainSceneFbos[1].operator->() : NULL
1572 };
1573 OpenGl_FrameBuffer* aMainFbosOit[2] =
1574 {
1575 myMainSceneFbosOit[0]->IsValid() ? myMainSceneFbosOit[0].operator->() : NULL,
1576 myMainSceneFbosOit[1]->IsValid() ? myMainSceneFbosOit[1].operator->() :
1577 myMainSceneFbosOit[0]->IsValid() ? myMainSceneFbosOit[0].operator->() : NULL
1578 };
1579
1580 OpenGl_FrameBuffer* anImmFbos[2] =
1581 {
1582 myImmediateSceneFbos[0]->IsValid() ? myImmediateSceneFbos[0].operator->() : NULL,
1583 myImmediateSceneFbos[1]->IsValid() ? myImmediateSceneFbos[1].operator->() : NULL
1584 };
1585 OpenGl_FrameBuffer* anImmFbosOit[2] =
1586 {
1587 myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL,
1588 myImmediateSceneFbosOit[1]->IsValid() ? myImmediateSceneFbosOit[1].operator->() :
1589 myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL
1590 };
1591
1592 if (IsActiveXR())
1593 {
1594 // use single frame for both views - caching main scene content makes no sense
1595 // when head position is expected to be updated each frame redraw with high accuracy
1596 aMainFbos[1] = aMainFbos[0];
1597 aMainFbosOit[1] = aMainFbosOit[0];
1598 anImmFbos[0] = aMainFbos[0];
1599 anImmFbos[1] = aMainFbos[1];
1600 anImmFbosOit[0] = aMainFbosOit[0];
1601 anImmFbosOit[1] = aMainFbosOit[1];
1602 }
1603 else if (!myTransientDrawToFront)
1604 {
1605 anImmFbos [0] = aMainFbos [0];
1606 anImmFbos [1] = aMainFbos [1];
1607 anImmFbosOit[0] = aMainFbosOit[0];
1608 anImmFbosOit[1] = aMainFbosOit[1];
1609 }
1610 else if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip
1611 || aStereoMode == Graphic3d_StereoMode_QuadBuffer)
1612 {
1613 anImmFbos [0] = NULL;
1614 anImmFbos [1] = NULL;
1615 anImmFbosOit[0] = NULL;
1616 anImmFbosOit[1] = NULL;
1617 }
1618
1619 #if !defined(GL_ES_VERSION_2_0)
1620 aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_LEFT : GL_BACK);
1621 #endif
1622 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
1623 aMainFbos[0] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
1624
1625 redraw (Graphic3d_Camera::Projection_MonoLeftEye, aMainFbos[0], aMainFbosOit[0]);
1626 myBackBufferRestored = Standard_True;
1627 myIsImmediateDrawn = Standard_False;
1628 #if !defined(GL_ES_VERSION_2_0)
1629 aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_LEFT : GL_BACK);
1630 #endif
1631 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
1632 anImmFbos[0] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
1633 if (!redrawImmediate (Graphic3d_Camera::Projection_MonoLeftEye, aMainFbos[0], anImmFbos[0], anImmFbosOit[0]))
1634 {
1635 toSwap = false;
1636 }
1637 else if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip && toSwap)
1638 {
1639 aCtx->SwapBuffers();
1640 }
1641
1642 if (IsActiveXR())
1643 {
1644 // push Left frame to HMD display composer
1645 OpenGl_FrameBuffer* anXRFbo = myXrSceneFbo->IsValid() ? myXrSceneFbo.get() : aMainFbos[0];
1646 if (anXRFbo != aMainFbos[0])
1647 {
1648 blitBuffers (aMainFbos[0], anXRFbo); // resize or resolve MSAA samples
1649 }
1650 #if !defined(GL_ES_VERSION_2_0)
1651 const Aspect_GraphicsLibrary aGraphicsLib = Aspect_GraphicsLibrary_OpenGL;
1652 #else
1653 const Aspect_GraphicsLibrary aGraphicsLib = Aspect_GraphicsLibrary_OpenGLES;
1654 #endif
1655 myXRSession->SubmitEye ((void* )(size_t )anXRFbo->ColorTexture()->TextureId(),
1656 aGraphicsLib, Aspect_ColorSpace_sRGB, Aspect_Eye_Left);
1657 }
1658
1659 #if !defined(GL_ES_VERSION_2_0)
1660 aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_RIGHT : GL_BACK);
1661 #endif
1662 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
1663 aMainFbos[1] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
1664
1665 redraw (Graphic3d_Camera::Projection_MonoRightEye, aMainFbos[1], aMainFbosOit[1]);
1666 myBackBufferRestored = Standard_True;
1667 myIsImmediateDrawn = Standard_False;
1668 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
1669 anImmFbos[1] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
1670 if (!redrawImmediate (Graphic3d_Camera::Projection_MonoRightEye, aMainFbos[1], anImmFbos[1], anImmFbosOit[1]))
1671 {
1672 toSwap = false;
1673 }
1674
1675 if (IsActiveXR())
1676 {
1677 // push Right frame to HMD display composer
1678 OpenGl_FrameBuffer* anXRFbo = myXrSceneFbo->IsValid() ? myXrSceneFbo.get() : aMainFbos[1];
1679 if (anXRFbo != aMainFbos[1])
1680 {
1681 blitBuffers (aMainFbos[1], anXRFbo); // resize or resolve MSAA samples
1682 }
1683 #if !defined(GL_ES_VERSION_2_0)
1684 const Aspect_GraphicsLibrary aGraphicsLib = Aspect_GraphicsLibrary_OpenGL;
1685 #else
1686 const Aspect_GraphicsLibrary aGraphicsLib = Aspect_GraphicsLibrary_OpenGLES;
1687 #endif
1688 myXRSession->SubmitEye ((void* )(size_t )anXRFbo->ColorTexture()->TextureId(),
1689 aGraphicsLib, Aspect_ColorSpace_sRGB, Aspect_Eye_Right);
43eddb47 1690 aCtx->core11fwd->glFinish();
cfb54c0c 1691
1692 if (myRenderParams.ToMirrorComposer)
1693 {
1694 blitBuffers (anXRFbo, aFrameBuffer, myToFlipOutput);
1695 }
1696 }
1697 else if (anImmFbos[0] != NULL)
1698 {
1699 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(), 1.0f);
1700 drawStereoPair (aFrameBuffer);
1701 }
1702 }
1703 else
1704 {
1705 OpenGl_FrameBuffer* aMainFbo = myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : aFrameBuffer;
1706 OpenGl_FrameBuffer* aMainFboOit = myMainSceneFbosOit[0]->IsValid() ? myMainSceneFbosOit[0].operator->() : NULL;
1707 OpenGl_FrameBuffer* anImmFbo = aFrameBuffer;
1708 OpenGl_FrameBuffer* anImmFboOit = NULL;
1709 if (!myTransientDrawToFront)
1710 {
1711 anImmFbo = aMainFbo;
1712 anImmFboOit = aMainFboOit;
1713 }
1714 else if (myImmediateSceneFbos[0]->IsValid())
1715 {
1716 anImmFbo = myImmediateSceneFbos[0].operator->();
1717 anImmFboOit = myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL;
1718 }
1719
1720 #if !defined(GL_ES_VERSION_2_0)
1721 if (aMainFbo == NULL)
1722 {
1723 aCtx->SetReadDrawBuffer (GL_BACK);
1724 }
1725 #endif
1726 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
1727 aMainFbo != aFrameBuffer ? myRenderParams.RenderResolutionScale : 1.0f);
1728
1729 redraw (aProjectType, aMainFbo, aMainFboOit);
1730 myBackBufferRestored = Standard_True;
1731 myIsImmediateDrawn = Standard_False;
1732 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
1733 anImmFbo != aFrameBuffer ? myRenderParams.RenderResolutionScale : 1.0f);
1734 if (!redrawImmediate (aProjectType, aMainFbo, anImmFbo, anImmFboOit))
1735 {
1736 toSwap = false;
1737 }
1738
1739 if (anImmFbo != NULL
1740 && anImmFbo != aFrameBuffer)
1741 {
1742 blitBuffers (anImmFbo, aFrameBuffer, myToFlipOutput);
1743 }
1744 }
1745
1746 if (myRenderParams.Method == Graphic3d_RM_RAYTRACING
1747 && myRenderParams.IsGlobalIlluminationEnabled)
1748 {
1749 myAccumFrames++;
1750 }
1751
1752 // bind default FBO
1753 bindDefaultFbo();
1754
1755 if (wasDisabledMSAA != myToDisableMSAA
1756 || hadFboBlit != myHasFboBlit)
1757 {
1758 // retry on error
1759 Redraw();
1760 }
1761
1762 // reset state for safety
1763 aCtx->BindProgram (Handle(OpenGl_ShaderProgram)());
1764 if (aCtx->caps->ffpEnable)
1765 {
1766 aCtx->ShaderManager()->PushState (Handle(OpenGl_ShaderProgram)());
1767 }
1768
1769 // Swap the buffers
1770 if (toSwap)
1771 {
1772 aCtx->SwapBuffers();
1773 if (!myMainSceneFbos[0]->IsValid())
1774 {
1775 myBackBufferRestored = Standard_False;
1776 }
1777 }
1778 else
1779 {
1780 aCtx->core11fwd->glFlush();
1781 }
1782
1783 // reset render mode state
1784 aCtx->FetchState();
1785 aCtx->FrameStats()->FrameEnd (myWorkspace->View(), false);
1786
1787 myWasRedrawnGL = Standard_True;
1788}
1789
1790// =======================================================================
1791// function : RedrawImmediate
1792// purpose :
1793// =======================================================================
1794void OpenGl_View::RedrawImmediate()
1795{
1796 if (!myWorkspace->Activate())
1797 return;
1798
1799 // no special handling of HMD display, since it will force full Redraw() due to no frame caching (myBackBufferRestored)
1800 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
1801 if (!myTransientDrawToFront
1802 || !myBackBufferRestored
1803 || (aCtx->caps->buffersNoSwap && !myMainSceneFbos[0]->IsValid()))
1804 {
1805 Redraw();
1806 return;
1807 }
1808
1809 const Graphic3d_StereoMode aStereoMode = myRenderParams.StereoMode;
1810 Graphic3d_Camera::Projection aProjectType = myCamera->ProjectionType();
1811 OpenGl_FrameBuffer* aFrameBuffer = myFBO.get();
1812 aCtx->FrameStats()->FrameStart (myWorkspace->View(), true);
1813
1814 if ( aFrameBuffer == NULL
1815 && !aCtx->DefaultFrameBuffer().IsNull()
1816 && aCtx->DefaultFrameBuffer()->IsValid())
1817 {
1818 aFrameBuffer = aCtx->DefaultFrameBuffer().operator->();
1819 }
1820
1821 if (aProjectType == Graphic3d_Camera::Projection_Stereo)
1822 {
1823 if (myMainSceneFbos[0]->IsValid()
1824 && !myMainSceneFbos[1]->IsValid())
1825 {
1826 aProjectType = Graphic3d_Camera::Projection_Perspective;
1827 }
1828 }
1829
1830 bool toSwap = false;
1831 if (aProjectType == Graphic3d_Camera::Projection_Stereo)
1832 {
1833 OpenGl_FrameBuffer* aMainFbos[2] =
1834 {
1835 myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL,
1836 myMainSceneFbos[1]->IsValid() ? myMainSceneFbos[1].operator->() : NULL
1837 };
1838 OpenGl_FrameBuffer* anImmFbos[2] =
1839 {
1840 myImmediateSceneFbos[0]->IsValid() ? myImmediateSceneFbos[0].operator->() : NULL,
1841 myImmediateSceneFbos[1]->IsValid() ? myImmediateSceneFbos[1].operator->() : NULL
1842 };
1843 OpenGl_FrameBuffer* anImmFbosOit[2] =
1844 {
1845 myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL,
1846 myImmediateSceneFbosOit[1]->IsValid() ? myImmediateSceneFbosOit[1].operator->() :
1847 myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL
1848 };
1849 if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip
1850 || aStereoMode == Graphic3d_StereoMode_QuadBuffer)
1851 {
1852 anImmFbos[0] = NULL;
1853 anImmFbos[1] = NULL;
1854 anImmFbosOit[0] = NULL;
1855 anImmFbosOit[1] = NULL;
1856 }
1857
1858 if (aCtx->arbFBO != NULL)
1859 {
1860 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
1861 }
1862 #if !defined(GL_ES_VERSION_2_0)
1863 if (anImmFbos[0] == NULL)
1864 {
1865 aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_LEFT : GL_BACK);
1866 }
1867 #endif
1868
1869 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
1870 anImmFbos[0] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
1871 toSwap = redrawImmediate (Graphic3d_Camera::Projection_MonoLeftEye,
1872 aMainFbos[0],
1873 anImmFbos[0],
1874 anImmFbosOit[0],
1875 Standard_True) || toSwap;
1876 if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip
1877 && toSwap
655e883c 1878 && myFBO.get() == NULL
cfb54c0c 1879 && !aCtx->caps->buffersNoSwap)
1880 {
1881 aCtx->SwapBuffers();
1882 }
1883
1884 if (aCtx->arbFBO != NULL)
1885 {
1886 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
1887 }
1888 #if !defined(GL_ES_VERSION_2_0)
1889 if (anImmFbos[1] == NULL)
1890 {
1891 aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_RIGHT : GL_BACK);
1892 }
1893 #endif
1894 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
1895 anImmFbos[1] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
1896 toSwap = redrawImmediate (Graphic3d_Camera::Projection_MonoRightEye,
1897 aMainFbos[1],
1898 anImmFbos[1],
1899 anImmFbosOit[1],
1900 Standard_True) || toSwap;
1901 if (anImmFbos[0] != NULL)
1902 {
1903 drawStereoPair (aFrameBuffer);
1904 }
1905 }
1906 else
1907 {
1908 OpenGl_FrameBuffer* aMainFbo = myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL;
1909 OpenGl_FrameBuffer* anImmFbo = aFrameBuffer;
1910 OpenGl_FrameBuffer* anImmFboOit = NULL;
1911 if (myImmediateSceneFbos[0]->IsValid())
1912 {
1913 anImmFbo = myImmediateSceneFbos[0].operator->();
1914 anImmFboOit = myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL;
1915 }
1916 #if !defined(GL_ES_VERSION_2_0)
1917 if (aMainFbo == NULL)
1918 {
1919 aCtx->SetReadDrawBuffer (GL_BACK);
1920 }
1921 #endif
1922 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
1923 anImmFbo != aFrameBuffer ? myRenderParams.RenderResolutionScale : 1.0f);
1924 toSwap = redrawImmediate (aProjectType,
1925 aMainFbo,
1926 anImmFbo,
1927 anImmFboOit,
1928 Standard_True) || toSwap;
1929 if (anImmFbo != NULL
1930 && anImmFbo != aFrameBuffer)
1931 {
1932 blitBuffers (anImmFbo, aFrameBuffer, myToFlipOutput);
1933 }
1934 }
1935
1936 // bind default FBO
1937 bindDefaultFbo();
1938
1939 // reset state for safety
1940 aCtx->BindProgram (Handle(OpenGl_ShaderProgram)());
1941 if (aCtx->caps->ffpEnable)
1942 {
1943 aCtx->ShaderManager()->PushState (Handle(OpenGl_ShaderProgram)());
1944 }
1945
655e883c 1946 if (toSwap
1947 && myFBO.get() == NULL
1948 && !aCtx->caps->buffersNoSwap)
cfb54c0c 1949 {
1950 aCtx->SwapBuffers();
1951 }
1952 else
1953 {
1954 aCtx->core11fwd->glFlush();
1955 }
1956 aCtx->FrameStats()->FrameEnd (myWorkspace->View(), true);
1957
1958 myWasRedrawnGL = Standard_True;
1959}
1960
1961// =======================================================================
1962// function : redraw
1963// purpose :
1964// =======================================================================
1965void OpenGl_View::redraw (const Graphic3d_Camera::Projection theProjection,
1966 OpenGl_FrameBuffer* theReadDrawFbo,
1967 OpenGl_FrameBuffer* theOitAccumFbo)
1968{
1969 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
1970 if (theReadDrawFbo != NULL)
1971 {
1972 theReadDrawFbo->BindBuffer (aCtx);
1973 theReadDrawFbo->SetupViewport (aCtx);
1974 }
1975 else
1976 {
1977 const Standard_Integer aViewport[4] = { 0, 0, myWindow->Width(), myWindow->Height() };
1978 aCtx->ResizeViewport (aViewport);
1979 }
1980
1981 // request reset of material
1982 aCtx->ShaderManager()->UpdateMaterialState();
1983
1984 myWorkspace->UseZBuffer() = Standard_True;
1985 myWorkspace->UseDepthWrite() = Standard_True;
1986 GLbitfield toClear = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT;
1987 aCtx->core11fwd->glDepthFunc (GL_LEQUAL);
1988 aCtx->core11fwd->glDepthMask (GL_TRUE);
1989 aCtx->core11fwd->glEnable (GL_DEPTH_TEST);
1990
1991 aCtx->core11fwd->glClearDepth (1.0);
1992
1993 const OpenGl_Vec4 aBgColor = aCtx->Vec4FromQuantityColor (myBgColor);
1994 aCtx->SetColorMaskRGBA (NCollection_Vec4<bool> (true)); // force writes into all components, including alpha
1995 aCtx->core11fwd->glClearColor (aBgColor.r(), aBgColor.g(), aBgColor.b(), aCtx->caps->buffersOpaqueAlpha ? 1.0f : 0.0f);
1996 aCtx->core11fwd->glClear (toClear);
1997 aCtx->SetColorMask (true); // restore default alpha component write state
1998
1999 render (theProjection, theReadDrawFbo, theOitAccumFbo, Standard_False);
2000}
2001
2002// =======================================================================
d84e8669 2003// function : redrawImmediate
cfb54c0c 2004// purpose :
2005// =======================================================================
2006bool OpenGl_View::redrawImmediate (const Graphic3d_Camera::Projection theProjection,
2007 OpenGl_FrameBuffer* theReadFbo,
2008 OpenGl_FrameBuffer* theDrawFbo,
2009 OpenGl_FrameBuffer* theOitAccumFbo,
2010 const Standard_Boolean theIsPartialUpdate)
2011{
2012 const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext();
2013 GLboolean toCopyBackToFront = GL_FALSE;
2014 if (theDrawFbo == theReadFbo
2015 && theDrawFbo != NULL
2016 && theDrawFbo->IsValid())
2017 {
2018 myBackBufferRestored = Standard_False;
2019 theDrawFbo->BindBuffer (aCtx);
2020 }
2021 else if (theReadFbo != NULL
2022 && theReadFbo->IsValid()
2023 && aCtx->IsRender())
2024 {
2025 if (!blitBuffers (theReadFbo, theDrawFbo))
2026 {
2027 return true;
2028 }
2029 }
2030 else if (theDrawFbo == NULL)
2031 {
2032 #if !defined(GL_ES_VERSION_2_0)
2033 aCtx->core11fwd->glGetBooleanv (GL_DOUBLEBUFFER, &toCopyBackToFront);
2034 #endif
2035 if (toCopyBackToFront
2036 && myTransientDrawToFront)
2037 {
2038 if (!HasImmediateStructures()
2039 && !theIsPartialUpdate)
2040 {
2041 // prefer Swap Buffers within Redraw in compatibility mode (without FBO)
2042 return true;
2043 }
2044 if (!copyBackToFront())
2045 {
2046 toCopyBackToFront = GL_FALSE;
2047 myBackBufferRestored = Standard_False;
2048 }
2049 }
2050 else
2051 {
2052 toCopyBackToFront = GL_FALSE;
2053 myBackBufferRestored = Standard_False;
2054 }
2055 }
2056 else
2057 {
2058 myBackBufferRestored = Standard_False;
2059 }
2060 myIsImmediateDrawn = Standard_True;
2061
2062 myWorkspace->UseZBuffer() = Standard_True;
2063 myWorkspace->UseDepthWrite() = Standard_True;
43eddb47 2064 aCtx->core11fwd->glDepthFunc (GL_LEQUAL);
2065 aCtx->core11fwd->glDepthMask (GL_TRUE);
2066 aCtx->core11fwd->glEnable (GL_DEPTH_TEST);
2067 aCtx->core11fwd->glClearDepth (1.0);
cfb54c0c 2068
2069 render (theProjection, theDrawFbo, theOitAccumFbo, Standard_True);
2070
2071 return !toCopyBackToFront;
2072}
2073
d84e8669 2074//=======================================================================
2075//function : renderShadowMap
2076//purpose :
2077//=======================================================================
2078void OpenGl_View::renderShadowMap (const Handle(OpenGl_ShadowMap)& theShadowMap)
2079{
2080 const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext();
2081 if (!theShadowMap->UpdateCamera (*this))
2082 {
2083 return;
2084 }
2085
2086 myBVHSelector.SetViewVolume (theShadowMap->Camera());
2087 myBVHSelector.SetViewportSize (myWindow->Width(), myWindow->Height(), myRenderParams.ResolutionRatio());
2088 myBVHSelector.CacheClipPtsProjections();
2089
2090 myLocalOrigin.SetCoord (0.0, 0.0, 0.0);
2091 aCtx->SetCamera (theShadowMap->Camera());
2092 aCtx->ProjectionState.SetCurrent (theShadowMap->Camera()->ProjectionMatrixF());
2093 aCtx->ApplyProjectionMatrix();
2094
2095 aCtx->ShaderManager()->UpdateMaterialState();
2096 aCtx->ShaderManager()->UpdateModelWorldStateTo (OpenGl_Mat4());
2097 aCtx->ShaderManager()->SetShadingModel (Graphic3d_TOSM_UNLIT);
2098
2099 const Handle(OpenGl_FrameBuffer)& aShadowBuffer = theShadowMap->FrameBuffer();
2100 aShadowBuffer->BindBuffer (aCtx);
2101 aShadowBuffer->SetupViewport (aCtx);
2102
2103 aCtx->SetColorMask (false);
2104 aCtx->SetAllowSampleAlphaToCoverage (false);
2105 aCtx->SetSampleAlphaToCoverage (false);
2106
2107 myWorkspace->UseZBuffer() = true;
2108 myWorkspace->UseDepthWrite() = true;
2109 aCtx->core11fwd->glDepthFunc (GL_LEQUAL);
2110 aCtx->core11fwd->glDepthMask (GL_TRUE);
2111 aCtx->core11fwd->glEnable (GL_DEPTH_TEST);
2112 aCtx->core11fwd->glClearDepth (1.0);
2113 aCtx->core11fwd->glClear (GL_DEPTH_BUFFER_BIT);
2114
2115 renderScene (Graphic3d_Camera::Projection_Orthographic, aShadowBuffer.get(), NULL, false);
2116
2117 aCtx->SetColorMask (true);
2118 myWorkspace->ResetAppliedAspect();
2119 aCtx->BindProgram (Handle(OpenGl_ShaderProgram)());
2120
2121//Image_AlienPixMap anImage; anImage.InitZero (Image_Format_Gray, aShadowBuffer->GetVPSizeX(), aShadowBuffer->GetVPSizeY());
2122//OpenGl_FrameBuffer::BufferDump (aCtx, aShadowBuffer, anImage, Graphic3d_BT_Depth);
2123//anImage.Save (TCollection_AsciiString ("shadow") + theShadowMap->Texture()->Sampler()->Parameters()->TextureUnit() + ".png");
2124
2125 bindDefaultFbo();
2126}
2127
cfb54c0c 2128//=======================================================================
2129//function : Render
2130//purpose :
2131//=======================================================================
2132void OpenGl_View::render (Graphic3d_Camera::Projection theProjection,
2133 OpenGl_FrameBuffer* theOutputFBO,
2134 OpenGl_FrameBuffer* theOitAccumFbo,
2135 const Standard_Boolean theToDrawImmediate)
2136{
2137 // ==================================
2138 // Step 1: Prepare for render
2139 // ==================================
2140
2141 const Handle(OpenGl_Context)& aContext = myWorkspace->GetGlContext();
2142 aContext->SetAllowSampleAlphaToCoverage (myRenderParams.ToEnableAlphaToCoverage
2143 && theOutputFBO != NULL
2144 && theOutputFBO->NbSamples() != 0);
2145
2146#if !defined(GL_ES_VERSION_2_0)
2147 // Disable current clipping planes
43eddb47 2148 if (aContext->core11ffp != NULL)
cfb54c0c 2149 {
2150 const Standard_Integer aMaxPlanes = aContext->MaxClipPlanes();
2151 for (Standard_Integer aClipPlaneId = GL_CLIP_PLANE0; aClipPlaneId < GL_CLIP_PLANE0 + aMaxPlanes; ++aClipPlaneId)
2152 {
2153 aContext->core11fwd->glDisable (aClipPlaneId);
2154 }
2155 }
2156#endif
2157
2158 // update states of OpenGl_BVHTreeSelector (frustum culling algorithm);
2159 // note that we pass here window dimensions ignoring Graphic3d_RenderingParams::RenderResolutionScale
2160 myBVHSelector.SetViewVolume (myCamera);
2161 myBVHSelector.SetViewportSize (myWindow->Width(), myWindow->Height(), myRenderParams.ResolutionRatio());
2162 myBVHSelector.CacheClipPtsProjections();
2163
2164 const Handle(OpenGl_ShaderManager)& aManager = aContext->ShaderManager();
114db5a5 2165 const Handle(Graphic3d_LightSet)& aLights = myRenderParams.ShadingModel == Graphic3d_TOSM_UNLIT ? myNoShadingLight : myLights;
cfb54c0c 2166 Standard_Size aLightsRevision = 0;
2167 if (!aLights.IsNull())
2168 {
2169 aLightsRevision = aLights->UpdateRevision();
2170 }
2171 if (StateInfo (myCurrLightSourceState, aManager->LightSourceState().Index()) != myLastLightSourceState
2172 || aLightsRevision != myLightsRevision)
2173 {
2174 myLightsRevision = aLightsRevision;
d84e8669 2175 aManager->UpdateLightSourceStateTo (aLights, SpecIBLMapLevels(), myShadowMaps->IsValid() ? myShadowMaps : Handle(OpenGl_ShadowMapArray)());
cfb54c0c 2176 myLastLightSourceState = StateInfo (myCurrLightSourceState, aManager->LightSourceState().Index());
2177 }
2178
2179 // Update matrices if camera has changed.
2180 Graphic3d_WorldViewProjState aWVPState = myCamera->WorldViewProjState();
2181 if (myWorldViewProjState != aWVPState)
2182 {
2183 myAccumFrames = 0;
2184 myWorldViewProjState = aWVPState;
2185 }
2186
2187 myLocalOrigin.SetCoord (0.0, 0.0, 0.0);
2188 aContext->SetCamera (myCamera);
2189 if (aManager->ModelWorldState().Index() == 0)
2190 {
2191 aContext->ShaderManager()->UpdateModelWorldStateTo (OpenGl_Mat4());
2192 }
2193
2194 // ====================================
2195 // Step 2: Redraw background
2196 // ====================================
2197
2198 // Render background
2199 if (!theToDrawImmediate)
2200 {
2201 drawBackground (myWorkspace, theProjection);
2202 }
2203
2204#if !defined(GL_ES_VERSION_2_0)
2205 // Switch off lighting by default
43eddb47 2206 if (aContext->core11ffp != NULL
cfb54c0c 2207 && aContext->caps->ffpEnable)
2208 {
43eddb47 2209 aContext->core11fwd->glDisable (GL_LIGHTING);
cfb54c0c 2210 }
2211#endif
2212
2213 // =================================
2214 // Step 3: Redraw main plane
2215 // =================================
2216
cfb54c0c 2217#if !defined(GL_ES_VERSION_2_0)
2218 // if the view is scaled normal vectors are scaled to unit
2219 // length for correct displaying of shaded objects
2220 const gp_Pnt anAxialScale = aContext->Camera()->AxialScale();
2221 if (anAxialScale.X() != 1.F ||
2222 anAxialScale.Y() != 1.F ||
2223 anAxialScale.Z() != 1.F)
2224 {
2225 aContext->SetGlNormalizeEnabled (Standard_True);
2226 }
2227 else
2228 {
2229 aContext->SetGlNormalizeEnabled (Standard_False);
2230 }
2231#endif
2232
114db5a5 2233 aManager->SetShadingModel (OpenGl_ShaderManager::PBRShadingModelFallback (myRenderParams.ShadingModel, checkPBRAvailability()));
cfb54c0c 2234
2235 // Redraw 3d scene
2236 if (theProjection == Graphic3d_Camera::Projection_MonoLeftEye)
2237 {
2238 aContext->ProjectionState.SetCurrent (aContext->Camera()->ProjectionStereoLeftF());
2239 aContext->ApplyProjectionMatrix();
2240 }
2241 else if (theProjection == Graphic3d_Camera::Projection_MonoRightEye)
2242 {
2243 aContext->ProjectionState.SetCurrent (aContext->Camera()->ProjectionStereoRightF());
2244 aContext->ApplyProjectionMatrix();
2245 }
2246
2247 myWorkspace->SetEnvironmentTexture (myTextureEnv);
2248
d84e8669 2249 const bool hasShadowMap = aContext->ShaderManager()->LightSourceState().HasShadowMaps();
2250 if (hasShadowMap)
2251 {
2252 for (Standard_Integer aShadowIter = myShadowMaps->Lower(); aShadowIter <= myShadowMaps->Upper(); ++aShadowIter)
2253 {
2254 const Handle(OpenGl_ShadowMap)& aShadow = myShadowMaps->Value (aShadowIter);
2255 aShadow->Texture()->Bind (aContext);
2256 }
2257 }
2258
cfb54c0c 2259 renderScene (theProjection, theOutputFBO, theOitAccumFbo, theToDrawImmediate);
2260
d84e8669 2261 if (hasShadowMap)
2262 {
2263 for (Standard_Integer aShadowIter = myShadowMaps->Lower(); aShadowIter <= myShadowMaps->Upper(); ++aShadowIter)
2264 {
2265 const Handle(OpenGl_ShadowMap)& aShadow = myShadowMaps->Value (aShadowIter);
2266 aShadow->Texture()->Unbind (aContext);
2267 }
2268 if (aContext->core15fwd != NULL)
2269 {
2270 aContext->core15fwd->glActiveTexture (GL_TEXTURE0);
2271 }
2272 }
2273
cfb54c0c 2274 myWorkspace->SetEnvironmentTexture (Handle(OpenGl_TextureSet)());
2275
2276 // ===============================
2277 // Step 4: Trihedron
2278 // ===============================
2279
2280 // Resetting GL parameters according to the default aspects
2281 // in order to synchronize GL state with the graphic driver state
2282 // before drawing auxiliary stuff (trihedrons, overlayer)
2283 myWorkspace->ResetAppliedAspect();
2284
2285 // Render trihedron
2286 if (!theToDrawImmediate)
2287 {
2288 renderTrihedron (myWorkspace);
cfb54c0c 2289 }
2290 else
2291 {
2292 renderFrameStats();
2293 }
2294
2295 myWorkspace->ResetAppliedAspect();
2296 aContext->SetAllowSampleAlphaToCoverage (false);
2297 aContext->SetSampleAlphaToCoverage (false);
2298
2299 // reset FFP state for safety
2300 aContext->BindProgram (Handle(OpenGl_ShaderProgram)());
2301 if (aContext->caps->ffpEnable)
2302 {
2303 aContext->ShaderManager()->PushState (Handle(OpenGl_ShaderProgram)());
2304 }
cfb54c0c 2305}
2306
2307// =======================================================================
2308// function : InvalidateBVHData
2309// purpose :
2310// =======================================================================
2311void OpenGl_View::InvalidateBVHData (const Graphic3d_ZLayerId theLayerId)
2312{
2313 myZLayers.InvalidateBVHData (theLayerId);
2314}
2315
2316//=======================================================================
2317//function : renderStructs
2318//purpose :
2319//=======================================================================
2320void OpenGl_View::renderStructs (Graphic3d_Camera::Projection theProjection,
2321 OpenGl_FrameBuffer* theReadDrawFbo,
2322 OpenGl_FrameBuffer* theOitAccumFbo,
2323 const Standard_Boolean theToDrawImmediate)
2324{
2325 myZLayers.UpdateCulling (myWorkspace, theToDrawImmediate);
2326 if ( myZLayers.NbStructures() <= 0 )
2327 return;
2328
2329 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
2330 Standard_Boolean toRenderGL = theToDrawImmediate ||
2331 myRenderParams.Method != Graphic3d_RM_RAYTRACING ||
2332 myRaytraceInitStatus == OpenGl_RT_FAIL ||
2333 aCtx->IsFeedback();
2334
2335 if (!toRenderGL)
2336 {
1220d98e 2337 const Graphic3d_Vec2i aSizeXY = theReadDrawFbo != NULL
2338 ? theReadDrawFbo->GetVPSize()
2339 : Graphic3d_Vec2i (myWindow->Width(), myWindow->Height());
cfb54c0c 2340
1220d98e 2341 toRenderGL = !initRaytraceResources (aSizeXY.x(), aSizeXY.y(), aCtx)
cfb54c0c 2342 || !updateRaytraceGeometry (OpenGl_GUM_CHECK, myId, aCtx);
2343
2344 toRenderGL |= !myIsRaytraceDataValid; // if no ray-trace data use OpenGL
2345
2346 if (!toRenderGL)
2347 {
1220d98e 2348 myOpenGlFBO ->InitLazy (aCtx, aSizeXY, myFboColorFormat, myFboDepthFormat, 0);
cfb54c0c 2349 if (theReadDrawFbo != NULL)
1220d98e 2350 {
cfb54c0c 2351 theReadDrawFbo->UnbindBuffer (aCtx);
1220d98e 2352 }
cfb54c0c 2353
2354 // Prepare preliminary OpenGL output
2355 if (aCtx->arbFBOBlit != NULL)
2356 {
2357 // Render bottom OSD layer
2358 myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_Bottom, theReadDrawFbo, theOitAccumFbo);
2359
2360 const Standard_Integer aPrevFilter = myWorkspace->RenderFilter() & ~(Standard_Integer )(OpenGl_RenderFilter_NonRaytraceableOnly);
2361 myWorkspace->SetRenderFilter (aPrevFilter | OpenGl_RenderFilter_NonRaytraceableOnly);
2362 {
2363 if (theReadDrawFbo != NULL)
2364 {
2365 theReadDrawFbo->BindDrawBuffer (aCtx);
2366 }
2367 else
2368 {
2369 aCtx->arbFBO->glBindFramebuffer (GL_DRAW_FRAMEBUFFER, 0);
2370 aCtx->SetFrameBufferSRGB (false);
2371 }
2372
2373 // Render non-polygonal elements in default layer
2374 myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_RayTracable, theReadDrawFbo, theOitAccumFbo);
2375 }
2376 myWorkspace->SetRenderFilter (aPrevFilter);
2377 }
2378
2379 if (theReadDrawFbo != NULL)
2380 {
2381 theReadDrawFbo->BindBuffer (aCtx);
2382 }
2383 else
2384 {
2385 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, 0);
2386 aCtx->SetFrameBufferSRGB (false);
2387 }
2388
2389 // Reset OpenGl aspects state to default to avoid enabling of
2390 // backface culling which is not supported in ray-tracing.
2391 myWorkspace->ResetAppliedAspect();
2392
2393 // Ray-tracing polygonal primitive arrays
1220d98e 2394 raytrace (aSizeXY.x(), aSizeXY.y(), theProjection, theReadDrawFbo, aCtx);
cfb54c0c 2395
2396 // Render upper (top and topmost) OpenGL layers
2397 myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_Upper, theReadDrawFbo, theOitAccumFbo);
2398 }
2399 }
2400
2401 // Redraw 3D scene using OpenGL in standard
2402 // mode or in case of ray-tracing failure
2403 if (toRenderGL)
2404 {
2405 myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_All, theReadDrawFbo, theOitAccumFbo);
2406
2407 // Set flag that scene was redrawn by standard pipeline
2408 myWasRedrawnGL = Standard_True;
2409 }
2410}
2411
2412//=======================================================================
2413//function : renderTrihedron
2414//purpose :
2415//=======================================================================
2416void OpenGl_View::renderTrihedron (const Handle(OpenGl_Workspace) &theWorkspace)
2417{
2418 if (myToShowGradTrihedron)
2419 {
2420 myGraduatedTrihedron.Render (theWorkspace);
2421 }
2422}
2423
2424//=======================================================================
2425//function : renderFrameStats
2426//purpose :
2427//=======================================================================
2428void OpenGl_View::renderFrameStats()
2429{
2430 if (myRenderParams.ToShowStats
2431 && myRenderParams.CollectedStats != Graphic3d_RenderingParams::PerfCounters_NONE)
2432 {
2433 myFrameStatsPrs.Update (myWorkspace);
2434 myFrameStatsPrs.Render (myWorkspace);
2435 }
2436}
2437
2438// =======================================================================
2439// function : Invalidate
2440// purpose :
2441// =======================================================================
2442void OpenGl_View::Invalidate()
2443{
2444 myBackBufferRestored = Standard_False;
2445}
2446
2447//=======================================================================
2448//function : renderScene
2449//purpose :
2450//=======================================================================
2451void OpenGl_View::renderScene (Graphic3d_Camera::Projection theProjection,
2452 OpenGl_FrameBuffer* theReadDrawFbo,
2453 OpenGl_FrameBuffer* theOitAccumFbo,
2454 const Standard_Boolean theToDrawImmediate)
2455{
2456 const Handle(OpenGl_Context)& aContext = myWorkspace->GetGlContext();
2457
2458 // Specify clipping planes in view transformation space
2459 aContext->ChangeClipping().Reset (myClipPlanes);
2460 if (!myClipPlanes.IsNull()
2461 && !myClipPlanes->IsEmpty())
2462 {
2463 aContext->ShaderManager()->UpdateClippingState();
2464 }
2465
2466 renderStructs (theProjection, theReadDrawFbo, theOitAccumFbo, theToDrawImmediate);
2467 aContext->BindTextures (Handle(OpenGl_TextureSet)(), Handle(OpenGl_ShaderProgram)());
2468
2469 // Apply restored view matrix.
2470 aContext->ApplyWorldViewMatrix();
2471
2472 aContext->ChangeClipping().Reset (Handle(Graphic3d_SequenceOfHClipPlane)());
2473 if (!myClipPlanes.IsNull()
2474 && !myClipPlanes->IsEmpty())
2475 {
2476 aContext->ShaderManager()->RevertClippingState();
2477 }
2478}
2479
2480// =======================================================================
2481// function : bindDefaultFbo
2482// purpose :
2483// =======================================================================
2484void OpenGl_View::bindDefaultFbo (OpenGl_FrameBuffer* theCustomFbo)
2485{
2486 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
2487 OpenGl_FrameBuffer* anFbo = (theCustomFbo != NULL && theCustomFbo->IsValid())
2488 ? theCustomFbo
2489 : (!aCtx->DefaultFrameBuffer().IsNull()
2490 && aCtx->DefaultFrameBuffer()->IsValid()
2491 ? aCtx->DefaultFrameBuffer().operator->()
2492 : NULL);
2493 if (anFbo != NULL)
2494 {
2495 anFbo->BindBuffer (aCtx);
2496 anFbo->SetupViewport (aCtx);
2497 }
2498 else
2499 {
2500 #if !defined(GL_ES_VERSION_2_0)
2501 aCtx->SetReadDrawBuffer (GL_BACK);
2502 #else
2503 if (aCtx->arbFBO != NULL)
2504 {
2505 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
2506 }
2507 #endif
2508 const Standard_Integer aViewport[4] = { 0, 0, myWindow->Width(), myWindow->Height() };
2509 aCtx->ResizeViewport (aViewport);
2510 }
2511}
2512
2513// =======================================================================
2514// function : initBlitQuad
2515// purpose :
2516// =======================================================================
2517OpenGl_VertexBuffer* OpenGl_View::initBlitQuad (const Standard_Boolean theToFlip)
2518{
2519 OpenGl_VertexBuffer* aVerts = NULL;
2520 if (!theToFlip)
2521 {
2522 aVerts = &myFullScreenQuad;
2523 if (!aVerts->IsValid())
2524 {
2525 OpenGl_Vec4 aQuad[4] =
2526 {
2527 OpenGl_Vec4( 1.0f, -1.0f, 1.0f, 0.0f),
2528 OpenGl_Vec4( 1.0f, 1.0f, 1.0f, 1.0f),
2529 OpenGl_Vec4(-1.0f, -1.0f, 0.0f, 0.0f),
2530 OpenGl_Vec4(-1.0f, 1.0f, 0.0f, 1.0f)
2531 };
2532 aVerts->Init (myWorkspace->GetGlContext(), 4, 4, aQuad[0].GetData());
2533 }
2534 }
2535 else
2536 {
2537 aVerts = &myFullScreenQuadFlip;
2538 if (!aVerts->IsValid())
2539 {
2540 OpenGl_Vec4 aQuad[4] =
2541 {
2542 OpenGl_Vec4( 1.0f, -1.0f, 1.0f, 1.0f),
2543 OpenGl_Vec4( 1.0f, 1.0f, 1.0f, 0.0f),
2544 OpenGl_Vec4(-1.0f, -1.0f, 0.0f, 1.0f),
2545 OpenGl_Vec4(-1.0f, 1.0f, 0.0f, 0.0f)
2546 };
2547 aVerts->Init (myWorkspace->GetGlContext(), 4, 4, aQuad[0].GetData());
2548 }
2549 }
2550 return aVerts;
2551}
2552
2553// =======================================================================
2554// function : blitBuffers
2555// purpose :
2556// =======================================================================
2557bool OpenGl_View::blitBuffers (OpenGl_FrameBuffer* theReadFbo,
2558 OpenGl_FrameBuffer* theDrawFbo,
2559 const Standard_Boolean theToFlip)
2560{
2561 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
2562 const Standard_Integer aReadSizeX = theReadFbo != NULL ? theReadFbo->GetVPSizeX() : myWindow->Width();
2563 const Standard_Integer aReadSizeY = theReadFbo != NULL ? theReadFbo->GetVPSizeY() : myWindow->Height();
2564 const Standard_Integer aDrawSizeX = theDrawFbo != NULL ? theDrawFbo->GetVPSizeX() : myWindow->Width();
2565 const Standard_Integer aDrawSizeY = theDrawFbo != NULL ? theDrawFbo->GetVPSizeY() : myWindow->Height();
2566 if (theReadFbo == NULL || aCtx->IsFeedback())
2567 {
2568 return false;
2569 }
2570 else if (theReadFbo == theDrawFbo)
2571 {
2572 return true;
2573 }
2574
2575 // clear destination before blitting
2576 if (theDrawFbo != NULL
2577 && theDrawFbo->IsValid())
2578 {
2579 theDrawFbo->BindBuffer (aCtx);
2580 }
2581 else
2582 {
2583 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
2584 aCtx->SetFrameBufferSRGB (false);
2585 }
2586 const Standard_Integer aViewport[4] = { 0, 0, aDrawSizeX, aDrawSizeY };
2587 aCtx->ResizeViewport (aViewport);
2588
2589 aCtx->SetColorMaskRGBA (NCollection_Vec4<bool> (true)); // force writes into all components, including alpha
2590 aCtx->core20fwd->glClearDepth (1.0);
57357010 2591 aCtx->core20fwd->glClearColor (0.0f, 0.0f, 0.0f, aCtx->caps->buffersOpaqueAlpha ? 1.0f : 0.0f);
cfb54c0c 2592 aCtx->core20fwd->glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
2593 aCtx->SetColorMask (true); // restore default alpha component write state
2594
2595 const bool toApplyGamma = aCtx->ToRenderSRGB() != aCtx->IsFrameBufferSRGB();
2596 if (aCtx->arbFBOBlit != NULL
2597 && !toApplyGamma
2598 && theReadFbo->NbSamples() != 0)
2599 {
2600 GLbitfield aCopyMask = 0;
2601 theReadFbo->BindReadBuffer (aCtx);
2602 if (theDrawFbo != NULL
2603 && theDrawFbo->IsValid())
2604 {
2605 theDrawFbo->BindDrawBuffer (aCtx);
2606 if (theDrawFbo->HasColor()
2607 && theReadFbo->HasColor())
2608 {
2609 aCopyMask |= GL_COLOR_BUFFER_BIT;
2610 }
2611 if (theDrawFbo->HasDepth()
2612 && theReadFbo->HasDepth())
2613 {
2614 aCopyMask |= GL_DEPTH_BUFFER_BIT;
2615 }
2616 }
2617 else
2618 {
2619 if (theReadFbo->HasColor())
2620 {
2621 aCopyMask |= GL_COLOR_BUFFER_BIT;
2622 }
2623 if (theReadFbo->HasDepth())
2624 {
2625 aCopyMask |= GL_DEPTH_BUFFER_BIT;
2626 }
2627 aCtx->arbFBO->glBindFramebuffer (GL_DRAW_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
2628 aCtx->SetFrameBufferSRGB (false);
2629 }
2630
2631 // we don't copy stencil buffer here... does it matter for performance?
2632 aCtx->arbFBOBlit->glBlitFramebuffer (0, 0, aReadSizeX, aReadSizeY,
2633 0, 0, aDrawSizeX, aDrawSizeY,
2634 aCopyMask, GL_NEAREST);
a46ab511 2635 const int anErr = aCtx->core11fwd->glGetError();
cfb54c0c 2636 if (anErr != GL_NO_ERROR)
2637 {
2638 // glBlitFramebuffer() might fail in several cases:
2639 // - Both FBOs have MSAA and they are samples number does not match.
2640 // OCCT checks that this does not happen,
2641 // however some graphics drivers provide an option for overriding MSAA.
2642 // In this case window MSAA might be non-zero (and application can not check it)
2643 // and might not match MSAA of our offscreen FBOs.
2644 // - Pixel formats of FBOs do not match.
2645 // This also might happen with window has pixel format,
2646 // e.g. Mesa fails blitting RGBA8 -> RGB8 while other drivers support this conversion.
a46ab511 2647 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "FBO blitting has failed [Error " + OpenGl_Context::FormatGlError (anErr) + "]\n"
cfb54c0c 2648 + " Please check your graphics driver settings or try updating driver.";
2649 if (theReadFbo->NbSamples() != 0)
2650 {
2651 myToDisableMSAA = true;
2652 aMsg += "\n MSAA settings should not be overridden by driver!";
2653 }
a46ab511 2654 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
cfb54c0c 2655 }
2656
2657 if (theDrawFbo != NULL
2658 && theDrawFbo->IsValid())
2659 {
2660 theDrawFbo->BindBuffer (aCtx);
2661 }
2662 else
2663 {
2664 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
2665 aCtx->SetFrameBufferSRGB (false);
2666 }
2667 }
2668 else
2669 {
2670 aCtx->core20fwd->glDepthFunc (GL_ALWAYS);
2671 aCtx->core20fwd->glDepthMask (GL_TRUE);
2672 aCtx->core20fwd->glEnable (GL_DEPTH_TEST);
2673 #if defined(GL_ES_VERSION_2_0)
2674 if (!aCtx->IsGlGreaterEqual (3, 0)
2675 && !aCtx->extFragDepth)
2676 {
2677 aCtx->core20fwd->glDisable (GL_DEPTH_TEST);
2678 }
2679 #endif
2680
2681 aCtx->BindTextures (Handle(OpenGl_TextureSet)(), Handle(OpenGl_ShaderProgram)());
2682
2683 const Graphic3d_TypeOfTextureFilter aFilter = (aDrawSizeX == aReadSizeX && aDrawSizeY == aReadSizeY) ? Graphic3d_TOTF_NEAREST : Graphic3d_TOTF_BILINEAR;
2684 const GLint aFilterGl = aFilter == Graphic3d_TOTF_NEAREST ? GL_NEAREST : GL_LINEAR;
2685
2686 OpenGl_VertexBuffer* aVerts = initBlitQuad (theToFlip);
2687 const Handle(OpenGl_ShaderManager)& aManager = aCtx->ShaderManager();
2688 if (aVerts->IsValid()
2689 && aManager->BindFboBlitProgram (theReadFbo != NULL ? theReadFbo->NbSamples() : 0, toApplyGamma))
2690 {
2691 aCtx->SetSampleAlphaToCoverage (false);
2692 theReadFbo->ColorTexture()->Bind (aCtx, Graphic3d_TextureUnit_0);
2693 if (theReadFbo->ColorTexture()->Sampler()->Parameters()->Filter() != aFilter)
2694 {
2695 theReadFbo->ColorTexture()->Sampler()->Parameters()->SetFilter (aFilter);
2696 aCtx->core20fwd->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, aFilterGl);
2697 aCtx->core20fwd->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, aFilterGl);
2698 }
2699
2700 theReadFbo->DepthStencilTexture()->Bind (aCtx, Graphic3d_TextureUnit_1);
2701 if (theReadFbo->DepthStencilTexture()->Sampler()->Parameters()->Filter() != aFilter)
2702 {
2703 theReadFbo->DepthStencilTexture()->Sampler()->Parameters()->SetFilter (aFilter);
2704 aCtx->core20fwd->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, aFilterGl);
2705 aCtx->core20fwd->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, aFilterGl);
2706 }
2707
2708 aVerts->BindVertexAttrib (aCtx, Graphic3d_TOA_POS);
2709
2710 aCtx->core20fwd->glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
2711
2712 aVerts->UnbindVertexAttrib (aCtx, Graphic3d_TOA_POS);
2713 theReadFbo->DepthStencilTexture()->Unbind (aCtx, Graphic3d_TextureUnit_1);
2714 theReadFbo->ColorTexture() ->Unbind (aCtx, Graphic3d_TextureUnit_0);
2715 aCtx->BindProgram (NULL);
2716 }
2717 else
2718 {
2719 TCollection_ExtendedString aMsg = TCollection_ExtendedString()
2720 + "Error! FBO blitting has failed";
2721 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
2722 GL_DEBUG_TYPE_ERROR,
2723 0,
2724 GL_DEBUG_SEVERITY_HIGH,
2725 aMsg);
2726 myHasFboBlit = Standard_False;
2727 theReadFbo->Release (aCtx.operator->());
2728 return true;
2729 }
2730 }
2731 return true;
2732}
2733
2734// =======================================================================
2735// function : drawStereoPair
2736// purpose :
2737// =======================================================================
2738void OpenGl_View::drawStereoPair (OpenGl_FrameBuffer* theDrawFbo)
2739{
2740 const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext();
2741 bindDefaultFbo (theDrawFbo);
2742 OpenGl_FrameBuffer* aPair[2] =
2743 {
2744 myImmediateSceneFbos[0]->IsValid() ? myImmediateSceneFbos[0].operator->() : NULL,
2745 myImmediateSceneFbos[1]->IsValid() ? myImmediateSceneFbos[1].operator->() : NULL
2746 };
2747 if (aPair[0] == NULL
2748 || aPair[1] == NULL
2749 || !myTransientDrawToFront)
2750 {
2751 aPair[0] = myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL;
2752 aPair[1] = myMainSceneFbos[1]->IsValid() ? myMainSceneFbos[1].operator->() : NULL;
2753 }
2754
2755 if (aPair[0] == NULL
2756 || aPair[1] == NULL)
2757 {
2758 return;
2759 }
2760
2761 if (aPair[0]->NbSamples() != 0)
2762 {
2763 // resolve MSAA buffers before drawing
1220d98e 2764 if (!myOpenGlFBO ->InitLazy (aCtx, aPair[0]->GetVPSize(), myFboColorFormat, myFboDepthFormat, 0)
2765 || !myOpenGlFBO2->InitLazy (aCtx, aPair[0]->GetVPSize(), myFboColorFormat, 0, 0))
cfb54c0c 2766 {
2767 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
2768 GL_DEBUG_TYPE_ERROR,
2769 0,
2770 GL_DEBUG_SEVERITY_HIGH,
2771 "Error! Unable to allocate FBO for blitting stereo pair");
2772 bindDefaultFbo (theDrawFbo);
2773 return;
2774 }
2775
2776 if (!blitBuffers (aPair[0], myOpenGlFBO .operator->(), Standard_False)
2777 || !blitBuffers (aPair[1], myOpenGlFBO2.operator->(), Standard_False))
2778 {
2779 bindDefaultFbo (theDrawFbo);
2780 return;
2781 }
2782
2783 aPair[0] = myOpenGlFBO .operator->();
2784 aPair[1] = myOpenGlFBO2.operator->();
2785 bindDefaultFbo (theDrawFbo);
2786 }
2787
2788 struct
2789 {
2790 Standard_Integer left;
2791 Standard_Integer top;
2792 Standard_Integer right;
2793 Standard_Integer bottom;
2794 Standard_Integer dx() { return right - left; }
2795 Standard_Integer dy() { return bottom - top; }
2796 } aGeom;
2797
2798 myWindow->PlatformWindow()->Position (aGeom.left, aGeom.top, aGeom.right, aGeom.bottom);
2799
2800 Standard_Boolean toReverse = myRenderParams.ToReverseStereo;
2801 const Standard_Boolean isOddY = (aGeom.top + aGeom.dy()) % 2 == 1;
2802 const Standard_Boolean isOddX = aGeom.left % 2 == 1;
2803 if (isOddY
2804 && (myRenderParams.StereoMode == Graphic3d_StereoMode_RowInterlaced
2805 || myRenderParams.StereoMode == Graphic3d_StereoMode_ChessBoard))
2806 {
2807 toReverse = !toReverse;
2808 }
2809 if (isOddX
2810 && (myRenderParams.StereoMode == Graphic3d_StereoMode_ColumnInterlaced
2811 || myRenderParams.StereoMode == Graphic3d_StereoMode_ChessBoard))
2812 {
2813 toReverse = !toReverse;
2814 }
2815
2816 if (toReverse)
2817 {
2818 std::swap (aPair[0], aPair[1]);
2819 }
2820
2821 aCtx->core20fwd->glDepthFunc (GL_ALWAYS);
2822 aCtx->core20fwd->glDepthMask (GL_TRUE);
2823 aCtx->core20fwd->glEnable (GL_DEPTH_TEST);
2824
2825 aCtx->BindTextures (Handle(OpenGl_TextureSet)(), Handle(OpenGl_ShaderProgram)());
2826 OpenGl_VertexBuffer* aVerts = initBlitQuad (myToFlipOutput);
2827
2828 const Handle(OpenGl_ShaderManager)& aManager = aCtx->ShaderManager();
2829 if (aVerts->IsValid()
2830 && aManager->BindStereoProgram (myRenderParams.StereoMode))
2831 {
2832 if (myRenderParams.StereoMode == Graphic3d_StereoMode_Anaglyph)
2833 {
2834 OpenGl_Mat4 aFilterL, aFilterR;
2835 aFilterL.SetDiagonal (Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f));
2836 aFilterR.SetDiagonal (Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f));
2837 switch (myRenderParams.AnaglyphFilter)
2838 {
2839 case Graphic3d_RenderingParams::Anaglyph_RedCyan_Simple:
2840 {
2841 aFilterL.SetRow (0, Graphic3d_Vec4 (1.0f, 0.0f, 0.0f, 0.0f));
2842 aFilterR.SetRow (1, Graphic3d_Vec4 (0.0f, 1.0f, 0.0f, 0.0f));
2843 aFilterR.SetRow (2, Graphic3d_Vec4 (0.0f, 0.0f, 1.0f, 0.0f));
2844 break;
2845 }
2846 case Graphic3d_RenderingParams::Anaglyph_RedCyan_Optimized:
2847 {
2848 aFilterL.SetRow (0, Graphic3d_Vec4 ( 0.4154f, 0.4710f, 0.16666667f, 0.0f));
2849 aFilterL.SetRow (1, Graphic3d_Vec4 (-0.0458f, -0.0484f, -0.0257f, 0.0f));
2850 aFilterL.SetRow (2, Graphic3d_Vec4 (-0.0547f, -0.0615f, 0.0128f, 0.0f));
2851 aFilterL.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f));
2852 aFilterR.SetRow (0, Graphic3d_Vec4 (-0.01090909f, -0.03636364f, -0.00606061f, 0.0f));
2853 aFilterR.SetRow (1, Graphic3d_Vec4 ( 0.37560000f, 0.73333333f, 0.01111111f, 0.0f));
2854 aFilterR.SetRow (2, Graphic3d_Vec4 (-0.06510000f, -0.12870000f, 1.29710000f, 0.0f));
2855 aFilterR.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f));
2856 break;
2857 }
2858 case Graphic3d_RenderingParams::Anaglyph_YellowBlue_Simple:
2859 {
2860 aFilterL.SetRow (0, Graphic3d_Vec4 (1.0f, 0.0f, 0.0f, 0.0f));
2861 aFilterL.SetRow (1, Graphic3d_Vec4 (0.0f, 1.0f, 0.0f, 0.0f));
2862 aFilterR.SetRow (2, Graphic3d_Vec4 (0.0f, 0.0f, 1.0f, 0.0f));
2863 break;
2864 }
2865 case Graphic3d_RenderingParams::Anaglyph_YellowBlue_Optimized:
2866 {
2867 aFilterL.SetRow (0, Graphic3d_Vec4 ( 1.062f, -0.205f, 0.299f, 0.0f));
2868 aFilterL.SetRow (1, Graphic3d_Vec4 (-0.026f, 0.908f, 0.068f, 0.0f));
2869 aFilterL.SetRow (2, Graphic3d_Vec4 (-0.038f, -0.173f, 0.022f, 0.0f));
2870 aFilterL.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f));
2871 aFilterR.SetRow (0, Graphic3d_Vec4 (-0.016f, -0.123f, -0.017f, 0.0f));
2872 aFilterR.SetRow (1, Graphic3d_Vec4 ( 0.006f, 0.062f, -0.017f, 0.0f));
2873 aFilterR.SetRow (2, Graphic3d_Vec4 ( 0.094f, 0.185f, 0.911f, 0.0f));
2874 aFilterR.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f));
2875 break;
2876 }
2877 case Graphic3d_RenderingParams::Anaglyph_GreenMagenta_Simple:
2878 {
2879 aFilterR.SetRow (0, Graphic3d_Vec4 (1.0f, 0.0f, 0.0f, 0.0f));
2880 aFilterL.SetRow (1, Graphic3d_Vec4 (0.0f, 1.0f, 0.0f, 0.0f));
2881 aFilterR.SetRow (2, Graphic3d_Vec4 (0.0f, 0.0f, 1.0f, 0.0f));
2882 break;
2883 }
2884 case Graphic3d_RenderingParams::Anaglyph_UserDefined:
2885 {
2886 aFilterL = myRenderParams.AnaglyphLeft;
2887 aFilterR = myRenderParams.AnaglyphRight;
2888 break;
2889 }
2890 }
2891 aCtx->ActiveProgram()->SetUniform (aCtx, "uMultL", aFilterL);
2892 aCtx->ActiveProgram()->SetUniform (aCtx, "uMultR", aFilterR);
2893 }
2894
2895 aPair[0]->ColorTexture()->Bind (aCtx, Graphic3d_TextureUnit_0);
2896 aPair[1]->ColorTexture()->Bind (aCtx, Graphic3d_TextureUnit_1);
2897 aVerts->BindVertexAttrib (aCtx, 0);
2898
2899 aCtx->core20fwd->glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
2900
2901 aVerts->UnbindVertexAttrib (aCtx, 0);
2902 aPair[1]->ColorTexture()->Unbind (aCtx, Graphic3d_TextureUnit_1);
2903 aPair[0]->ColorTexture()->Unbind (aCtx, Graphic3d_TextureUnit_0);
2904 }
2905 else
2906 {
2907 TCollection_ExtendedString aMsg = TCollection_ExtendedString()
2908 + "Error! Anaglyph has failed";
2909 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
2910 GL_DEBUG_TYPE_ERROR,
2911 0,
2912 GL_DEBUG_SEVERITY_HIGH,
2913 aMsg);
2914 }
2915}
2916
2917// =======================================================================
2918// function : copyBackToFront
2919// purpose :
2920// =======================================================================
2921bool OpenGl_View::copyBackToFront()
2922{
2923 myIsImmediateDrawn = Standard_False;
2924#if !defined(GL_ES_VERSION_2_0)
2925 const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext();
43eddb47 2926 if (aCtx->core11ffp == NULL)
cfb54c0c 2927 {
2928 return false;
2929 }
2930
2931 OpenGl_Mat4 aProjectMat;
2932 Graphic3d_TransformUtils::Ortho2D (aProjectMat,
2933 0.0f, static_cast<GLfloat> (myWindow->Width()),
2934 0.0f, static_cast<GLfloat> (myWindow->Height()));
2935
2936 aCtx->WorldViewState.Push();
2937 aCtx->ProjectionState.Push();
2938
2939 aCtx->WorldViewState.SetIdentity();
2940 aCtx->ProjectionState.SetCurrent (aProjectMat);
2941
2942 aCtx->ApplyProjectionMatrix();
2943 aCtx->ApplyWorldViewMatrix();
2944
2945 // synchronize FFP state before copying pixels
2946 aCtx->BindProgram (Handle(OpenGl_ShaderProgram)());
2947 aCtx->ShaderManager()->PushState (Handle(OpenGl_ShaderProgram)());
2948 aCtx->DisableFeatures();
2949
2950 switch (aCtx->DrawBuffer())
2951 {
2952 case GL_BACK_LEFT:
2953 {
2954 aCtx->SetReadBuffer (GL_BACK_LEFT);
2955 aCtx->SetDrawBuffer (GL_FRONT_LEFT);
2956 break;
2957 }
2958 case GL_BACK_RIGHT:
2959 {
2960 aCtx->SetReadBuffer (GL_BACK_RIGHT);
2961 aCtx->SetDrawBuffer (GL_FRONT_RIGHT);
2962 break;
2963 }
2964 default:
2965 {
2966 aCtx->SetReadBuffer (GL_BACK);
2967 aCtx->SetDrawBuffer (GL_FRONT);
2968 break;
2969 }
2970 }
2971
43eddb47 2972 aCtx->core11ffp->glRasterPos2i (0, 0);
2973 aCtx->core11ffp->glCopyPixels (0, 0, myWindow->Width() + 1, myWindow->Height() + 1, GL_COLOR);
2974 //aCtx->core11ffp->glCopyPixels (0, 0, myWidth + 1, myHeight + 1, GL_DEPTH);
cfb54c0c 2975
2976 aCtx->EnableFeatures();
2977
2978 aCtx->WorldViewState.Pop();
2979 aCtx->ProjectionState.Pop();
2980 aCtx->ApplyProjectionMatrix();
2981
2982 // read/write from front buffer now
2983 aCtx->SetReadBuffer (aCtx->DrawBuffer());
2984 return true;
2985#else
2986 return false;
2987#endif
2988}
2989
2990// =======================================================================
2991// function : checkOitCompatibility
2992// purpose :
2993// =======================================================================
2994Standard_Boolean OpenGl_View::checkOitCompatibility (const Handle(OpenGl_Context)& theGlContext,
2995 const Standard_Boolean theMSAA)
2996{
2997 // determine if OIT is supported by current OpenGl context
2998 Standard_Boolean& aToDisableOIT = theMSAA ? myToDisableMSAA : myToDisableOIT;
2999 if (aToDisableOIT)
3000 {
3001 return Standard_False;
3002 }
3003
3004 TCollection_ExtendedString aCompatibilityMsg;
3005 if (theGlContext->hasFloatBuffer == OpenGl_FeatureNotAvailable
3006 && theGlContext->hasHalfFloatBuffer == OpenGl_FeatureNotAvailable)
3007 {
3008 aCompatibilityMsg += "OpenGL context does not support floating-point RGBA color buffer format.\n";
3009 }
3010 if (theMSAA && theGlContext->hasSampleVariables == OpenGl_FeatureNotAvailable)
3011 {
3012 aCompatibilityMsg += "Current version of GLSL does not support built-in sample variables.\n";
3013 }
3014 if (theGlContext->hasDrawBuffers == OpenGl_FeatureNotAvailable)
3015 {
3016 aCompatibilityMsg += "OpenGL context does not support multiple draw buffers.\n";
3017 }
3018 if (aCompatibilityMsg.IsEmpty())
3019 {
3020 return Standard_True;
3021 }
3022
3023 aCompatibilityMsg += " Blended order-independent transparency will not be available.\n";
3024 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
3025 GL_DEBUG_TYPE_ERROR,
3026 0,
3027 GL_DEBUG_SEVERITY_HIGH,
3028 aCompatibilityMsg);
3029
3030 aToDisableOIT = Standard_True;
3031 return Standard_False;
3032}
3033
cfb54c0c 3034// =======================================================================
3035// function : checkPBRAvailability
3036// purpose :
3037// =======================================================================
3038Standard_Boolean OpenGl_View::checkPBRAvailability() const
3039{
3040 return myWorkspace->GetGlContext()->HasPBR()
3041 && !myPBREnvironment.IsNull();
3042}
3043
3044// =======================================================================
3045// function : bakePBREnvironment
3046// purpose :
3047// =======================================================================
3048void OpenGl_View::bakePBREnvironment (const Handle(OpenGl_Context)& theCtx)
3049{
3050 const Handle(OpenGl_TextureSet)& aTextureSet = myCubeMapParams->TextureSet (theCtx);
3051 if (!aTextureSet.IsNull()
3052 && !aTextureSet->IsEmpty())
3053 {
3054 myPBREnvironment->Bake (theCtx,
3055 aTextureSet->First(),
3056 myBackgroundCubeMap->ZIsInverted(),
3057 myBackgroundCubeMap->IsTopDown(),
3058 myRenderParams.PbrEnvBakingDiffNbSamples,
3059 myRenderParams.PbrEnvBakingSpecNbSamples,
3060 myRenderParams.PbrEnvBakingProbability);
3061 }
3062 else
3063 {
3064 myPBREnvironment->Clear (theCtx);
3065 }
3066}
3067
3068// =======================================================================
3069// function : clearPBREnvironment
3070// purpose :
3071// =======================================================================
3072void OpenGl_View::clearPBREnvironment (const Handle(OpenGl_Context)& theCtx)
3073{
3074 myPBREnvironment->Clear (theCtx);
3075}
3076
3077// =======================================================================
3078// function : clearPBREnvironment
3079// purpose :
3080// =======================================================================
3081void OpenGl_View::processPBREnvRequest (const Handle(OpenGl_Context)& theCtx)
3082{
3083 if (myPBREnvState == OpenGl_PBREnvState_CREATED)
3084 {
3085 switch (myPBREnvRequest)
3086 {
3087 case OpenGl_PBREnvRequest_NONE: return;
3088 case OpenGl_PBREnvRequest_BAKE: bakePBREnvironment (theCtx); break;
3089 case OpenGl_PBREnvRequest_CLEAR: clearPBREnvironment (theCtx); break;
3090 }
3091 }
3092 myPBREnvRequest = OpenGl_PBREnvRequest_NONE;
3093}