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