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