91c60b57 |
1 | // Created on: 2015-02-20 |
e276548b |
2 | // Created by: Denis BOGOLEPOV |
91c60b57 |
3 | // Copyright (c) 2015 OPEN CASCADE SAS |
e276548b |
4 | // |
973c2be1 |
5 | // This file is part of Open CASCADE Technology software library. |
e276548b |
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. |
e276548b |
12 | // |
973c2be1 |
13 | // Alternatively, this file may be used under the terms of Open CASCADE |
14 | // commercial license or contractual agreement. |
e276548b |
15 | |
c357e426 |
16 | #include <OpenGl_View.hxx> |
91c60b57 |
17 | |
c357e426 |
18 | #include <Graphic3d_TextureParams.hxx> |
b7cd4ba7 |
19 | #include <OpenGl_PrimitiveArray.hxx> |
fc73a202 |
20 | #include <OpenGl_VertexBuffer.hxx> |
3a9b5dc8 |
21 | #include <OpenGl_GlCore44.hxx> |
fc73a202 |
22 | #include <OSD_Protection.hxx> |
91c60b57 |
23 | #include <OSD_File.hxx> |
e276548b |
24 | |
ee5befae |
25 | #include "../Shaders/Shaders_RaytraceBase_vs.pxx" |
26 | #include "../Shaders/Shaders_RaytraceBase_fs.pxx" |
27 | #include "../Shaders/Shaders_PathtraceBase_fs.pxx" |
28 | #include "../Shaders/Shaders_RaytraceRender_fs.pxx" |
29 | #include "../Shaders/Shaders_RaytraceSmooth_fs.pxx" |
30 | #include "../Shaders/Shaders_Display_fs.pxx" |
78607702 |
31 | #include "../Shaders/Shaders_TangentSpaceNormal_glsl.pxx" |
ee5befae |
32 | |
e276548b |
33 | //! Use this macro to output ray-tracing debug info |
fc73a202 |
34 | // #define RAY_TRACE_PRINT_INFO |
e276548b |
35 | |
36 | #ifdef RAY_TRACE_PRINT_INFO |
37 | #include <OSD_Timer.hxx> |
38 | #endif |
39 | |
b6472664 |
40 | namespace |
41 | { |
42 | static const OpenGl_Vec4 THE_WHITE_COLOR (1.0f, 1.0f, 1.0f, 1.0f); |
43 | static const OpenGl_Vec4 THE_BLACK_COLOR (0.0f, 0.0f, 0.0f, 1.0f); |
44 | } |
45 | |
cc8cbabe |
46 | namespace |
47 | { |
48 | //! Defines OpenGL texture samplers. |
89d855ba |
49 | static const Graphic3d_TextureUnit OpenGl_RT_EnvMapTexture = Graphic3d_TextureUnit_0; |
cc8cbabe |
50 | |
51 | static const Graphic3d_TextureUnit OpenGl_RT_SceneNodeInfoTexture = Graphic3d_TextureUnit_1; |
52 | static const Graphic3d_TextureUnit OpenGl_RT_SceneMinPointTexture = Graphic3d_TextureUnit_2; |
53 | static const Graphic3d_TextureUnit OpenGl_RT_SceneMaxPointTexture = Graphic3d_TextureUnit_3; |
54 | static const Graphic3d_TextureUnit OpenGl_RT_SceneTransformTexture = Graphic3d_TextureUnit_4; |
55 | |
56 | static const Graphic3d_TextureUnit OpenGl_RT_GeometryVertexTexture = Graphic3d_TextureUnit_5; |
57 | static const Graphic3d_TextureUnit OpenGl_RT_GeometryNormalTexture = Graphic3d_TextureUnit_6; |
58 | static const Graphic3d_TextureUnit OpenGl_RT_GeometryTexCrdTexture = Graphic3d_TextureUnit_7; |
59 | static const Graphic3d_TextureUnit OpenGl_RT_GeometryTriangTexture = Graphic3d_TextureUnit_8; |
60 | |
61 | static const Graphic3d_TextureUnit OpenGl_RT_RaytraceMaterialTexture = Graphic3d_TextureUnit_9; |
62 | static const Graphic3d_TextureUnit OpenGl_RT_RaytraceLightSrcTexture = Graphic3d_TextureUnit_10; |
63 | |
64 | static const Graphic3d_TextureUnit OpenGl_RT_FsaaInputTexture = Graphic3d_TextureUnit_11; |
65 | static const Graphic3d_TextureUnit OpenGl_RT_PrevAccumTexture = Graphic3d_TextureUnit_12; |
66 | |
67 | static const Graphic3d_TextureUnit OpenGl_RT_RaytraceDepthTexture = Graphic3d_TextureUnit_13; |
68 | } |
69 | |
e276548b |
70 | // ======================================================================= |
189f85a3 |
71 | // function : updateRaytraceGeometry |
91c60b57 |
72 | // purpose : Updates 3D scene geometry for ray-tracing |
e276548b |
73 | // ======================================================================= |
91c60b57 |
74 | Standard_Boolean OpenGl_View::updateRaytraceGeometry (const RaytraceUpdateMode theMode, |
75 | const Standard_Integer theViewId, |
76 | const Handle(OpenGl_Context)& theGlContext) |
e276548b |
77 | { |
91c60b57 |
78 | // In 'check' mode (OpenGl_GUM_CHECK) the scene geometry is analyzed for |
79 | // modifications. This is light-weight procedure performed on each frame |
84c71f29 |
80 | if (theMode == OpenGl_GUM_CHECK) |
e276548b |
81 | { |
bd6a8454 |
82 | if (myRaytraceLayerListState != myZLayers.ModificationStateOfRaytracable()) |
91c60b57 |
83 | { |
84 | return updateRaytraceGeometry (OpenGl_GUM_PREPARE, theViewId, theGlContext); |
85 | } |
86 | } |
84c71f29 |
87 | else if (theMode == OpenGl_GUM_PREPARE) |
88 | { |
89 | myRaytraceGeometry.ClearMaterials(); |
91c60b57 |
90 | |
84c71f29 |
91 | myArrayToTrianglesMap.clear(); |
e276548b |
92 | |
93 | myIsRaytraceDataValid = Standard_False; |
94 | } |
e276548b |
95 | |
96 | // The set of processed structures (reflected to ray-tracing) |
97 | // This set is used to remove out-of-date records from the |
98 | // hash map of structures |
99 | std::set<const OpenGl_Structure*> anElements; |
100 | |
91c60b57 |
101 | // Set to store all currently visible OpenGL primitive arrays |
102 | // applicable for ray-tracing |
8d3f219f |
103 | std::set<Standard_Size> anArrayIDs; |
84c71f29 |
104 | |
189f85a3 |
105 | // Set to store all non-raytracable elements allowing tracking |
106 | // of changes in OpenGL scene (only for path tracing) |
107 | std::set<Standard_Integer> aNonRaytraceIDs; |
108 | |
1c728f2d |
109 | for (NCollection_List<Handle(Graphic3d_Layer)>::Iterator aLayerIter (myZLayers.Layers()); aLayerIter.More(); aLayerIter.Next()) |
e276548b |
110 | { |
1c728f2d |
111 | const Handle(OpenGl_Layer)& aLayer = aLayerIter.Value(); |
112 | if (aLayer->NbStructures() == 0 |
113 | || !aLayer->LayerSettings().IsRaytracable() |
114 | || aLayer->LayerSettings().IsImmediate()) |
115 | { |
116 | continue; |
117 | } |
91c60b57 |
118 | |
1c728f2d |
119 | const Graphic3d_ArrayOfIndexedMapOfStructure& aStructArray = aLayer->ArrayOfStructures(); |
68333c8f |
120 | for (Standard_Integer anIndex = 0; anIndex < aStructArray.Length(); ++anIndex) |
e276548b |
121 | { |
d325cb7f |
122 | for (OpenGl_Structure::StructIterator aStructIt (aStructArray.Value (anIndex)); aStructIt.More(); aStructIt.Next()) |
e276548b |
123 | { |
124 | const OpenGl_Structure* aStructure = aStructIt.Value(); |
125 | |
84c71f29 |
126 | if (theMode == OpenGl_GUM_CHECK) |
e276548b |
127 | { |
91c60b57 |
128 | if (toUpdateStructure (aStructure)) |
e276548b |
129 | { |
91c60b57 |
130 | return updateRaytraceGeometry (OpenGl_GUM_PREPARE, theViewId, theGlContext); |
e276548b |
131 | } |
189f85a3 |
132 | else if (aStructure->IsVisible() && myRaytraceParameters.GlobalIllumination) |
133 | { |
134 | aNonRaytraceIDs.insert (aStructure->highlight ? aStructure->Id : -aStructure->Id); |
135 | } |
e2da917a |
136 | } |
84c71f29 |
137 | else if (theMode == OpenGl_GUM_PREPARE) |
138 | { |
91c60b57 |
139 | if (!aStructure->IsRaytracable() || !aStructure->IsVisible()) |
a1954302 |
140 | { |
a272ed94 |
141 | continue; |
142 | } |
91c60b57 |
143 | else if (!aStructure->ViewAffinity.IsNull() && !aStructure->ViewAffinity->IsVisible (theViewId)) |
a272ed94 |
144 | { |
84c71f29 |
145 | continue; |
a1954302 |
146 | } |
84c71f29 |
147 | |
3e05329c |
148 | for (OpenGl_Structure::GroupIterator aGroupIter (aStructure->Groups()); aGroupIter.More(); aGroupIter.Next()) |
84c71f29 |
149 | { |
91c60b57 |
150 | // Extract OpenGL elements from the group (primitives arrays) |
84c71f29 |
151 | for (const OpenGl_ElementNode* aNode = aGroupIter.Value()->FirstNode(); aNode != NULL; aNode = aNode->next) |
152 | { |
153 | OpenGl_PrimitiveArray* aPrimArray = dynamic_cast<OpenGl_PrimitiveArray*> (aNode->elem); |
154 | |
155 | if (aPrimArray != NULL) |
156 | { |
8d3f219f |
157 | anArrayIDs.insert (aPrimArray->GetUID()); |
84c71f29 |
158 | } |
159 | } |
160 | } |
e2da917a |
161 | } |
91c60b57 |
162 | else if (theMode == OpenGl_GUM_REBUILD) |
e276548b |
163 | { |
164 | if (!aStructure->IsRaytracable()) |
91c60b57 |
165 | { |
e276548b |
166 | continue; |
91c60b57 |
167 | } |
168 | else if (addRaytraceStructure (aStructure, theGlContext)) |
169 | { |
170 | anElements.insert (aStructure); // structure was processed |
171 | } |
e276548b |
172 | } |
173 | } |
174 | } |
175 | } |
176 | |
84c71f29 |
177 | if (theMode == OpenGl_GUM_PREPARE) |
178 | { |
25ef750e |
179 | BVH_ObjectSet<Standard_ShortReal, 3>::BVH_ObjectList anUnchangedObjects; |
84c71f29 |
180 | |
91c60b57 |
181 | // Filter out unchanged objects so only their transformations and materials |
182 | // will be updated (and newly added objects will be processed from scratch) |
183 | for (Standard_Integer anObjIdx = 0; anObjIdx < myRaytraceGeometry.Size(); ++anObjIdx) |
84c71f29 |
184 | { |
185 | OpenGl_TriangleSet* aTriangleSet = dynamic_cast<OpenGl_TriangleSet*> ( |
91c60b57 |
186 | myRaytraceGeometry.Objects().ChangeValue (anObjIdx).operator->()); |
84c71f29 |
187 | |
91c60b57 |
188 | if (aTriangleSet == NULL) |
84c71f29 |
189 | { |
91c60b57 |
190 | continue; |
191 | } |
192 | |
193 | if (anArrayIDs.find (aTriangleSet->AssociatedPArrayID()) != anArrayIDs.end()) |
194 | { |
195 | anUnchangedObjects.Append (myRaytraceGeometry.Objects().Value (anObjIdx)); |
84c71f29 |
196 | |
8d3f219f |
197 | myArrayToTrianglesMap[aTriangleSet->AssociatedPArrayID()] = aTriangleSet; |
84c71f29 |
198 | } |
199 | } |
200 | |
201 | myRaytraceGeometry.Objects() = anUnchangedObjects; |
202 | |
91c60b57 |
203 | return updateRaytraceGeometry (OpenGl_GUM_REBUILD, theViewId, theGlContext); |
84c71f29 |
204 | } |
91c60b57 |
205 | else if (theMode == OpenGl_GUM_REBUILD) |
e276548b |
206 | { |
91c60b57 |
207 | // Actualize the hash map of structures - remove out-of-date records |
d4aaad5b |
208 | std::map<const OpenGl_Structure*, StructState>::iterator anIter = myStructureStates.begin(); |
e276548b |
209 | |
210 | while (anIter != myStructureStates.end()) |
211 | { |
212 | if (anElements.find (anIter->first) == anElements.end()) |
213 | { |
214 | myStructureStates.erase (anIter++); |
215 | } |
216 | else |
217 | { |
218 | ++anIter; |
219 | } |
220 | } |
221 | |
222 | // Actualize OpenGL layer list state |
bd6a8454 |
223 | myRaytraceLayerListState = myZLayers.ModificationStateOfRaytracable(); |
e276548b |
224 | |
91c60b57 |
225 | // Rebuild two-level acceleration structure |
265d4508 |
226 | myRaytraceGeometry.ProcessAcceleration(); |
e276548b |
227 | |
91c60b57 |
228 | myRaytraceSceneRadius = 2.f /* scale factor */ * std::max ( |
229 | myRaytraceGeometry.Box().CornerMin().cwiseAbs().maxComp(), |
230 | myRaytraceGeometry.Box().CornerMax().cwiseAbs().maxComp()); |
e276548b |
231 | |
25ef750e |
232 | const BVH_Vec3f aSize = myRaytraceGeometry.Box().Size(); |
e276548b |
233 | |
91c60b57 |
234 | myRaytraceSceneEpsilon = Max (1.0e-6f, 1.0e-4f * aSize.Modulus()); |
fc73a202 |
235 | |
91c60b57 |
236 | return uploadRaytraceData (theGlContext); |
e276548b |
237 | } |
238 | |
189f85a3 |
239 | if (myRaytraceParameters.GlobalIllumination) |
240 | { |
241 | Standard_Boolean toRestart = |
242 | aNonRaytraceIDs.size() != myNonRaytraceStructureIDs.size(); |
243 | |
244 | for (std::set<Standard_Integer>::iterator anID = aNonRaytraceIDs.begin(); anID != aNonRaytraceIDs.end() && !toRestart; ++anID) |
245 | { |
246 | if (myNonRaytraceStructureIDs.find (*anID) == myNonRaytraceStructureIDs.end()) |
247 | { |
248 | toRestart = Standard_True; |
249 | } |
250 | } |
251 | |
252 | if (toRestart) |
3a9b5dc8 |
253 | { |
189f85a3 |
254 | myAccumFrames = 0; |
3a9b5dc8 |
255 | } |
189f85a3 |
256 | |
257 | myNonRaytraceStructureIDs = aNonRaytraceIDs; |
258 | } |
259 | |
e276548b |
260 | return Standard_True; |
261 | } |
262 | |
263 | // ======================================================================= |
189f85a3 |
264 | // function : toUpdateStructure |
e2da917a |
265 | // purpose : Checks to see if the structure is modified |
e276548b |
266 | // ======================================================================= |
91c60b57 |
267 | Standard_Boolean OpenGl_View::toUpdateStructure (const OpenGl_Structure* theStructure) |
e276548b |
268 | { |
269 | if (!theStructure->IsRaytracable()) |
270 | { |
e276548b |
271 | if (theStructure->ModificationState() > 0) |
272 | { |
273 | theStructure->ResetModificationState(); |
91c60b57 |
274 | |
275 | return Standard_True; // ray-trace element was removed - need to rebuild |
e276548b |
276 | } |
277 | |
91c60b57 |
278 | return Standard_False; // did not contain ray-trace elements |
e276548b |
279 | } |
280 | |
d4aaad5b |
281 | std::map<const OpenGl_Structure*, StructState>::iterator aStructState = myStructureStates.find (theStructure); |
e276548b |
282 | |
d4aaad5b |
283 | if (aStructState == myStructureStates.end() || aStructState->second.StructureState != theStructure->ModificationState()) |
91c60b57 |
284 | { |
d4aaad5b |
285 | return Standard_True; |
286 | } |
287 | else if (theStructure->InstancedStructure() != NULL) |
288 | { |
289 | return aStructState->second.InstancedState != theStructure->InstancedStructure()->ModificationState(); |
91c60b57 |
290 | } |
e276548b |
291 | |
d4aaad5b |
292 | return Standard_False; |
e276548b |
293 | } |
294 | |
25ef750e |
295 | // ======================================================================= |
189f85a3 |
296 | // function : buildTextureTransform |
25ef750e |
297 | // purpose : Constructs texture transformation matrix |
298 | // ======================================================================= |
189f85a3 |
299 | void buildTextureTransform (const Handle(Graphic3d_TextureParams)& theParams, BVH_Mat4f& theMatrix) |
25ef750e |
300 | { |
301 | theMatrix.InitIdentity(); |
cc8cbabe |
302 | if (theParams.IsNull()) |
303 | { |
304 | return; |
305 | } |
25ef750e |
306 | |
307 | // Apply scaling |
308 | const Graphic3d_Vec2& aScale = theParams->Scale(); |
309 | |
310 | theMatrix.ChangeValue (0, 0) *= aScale.x(); |
311 | theMatrix.ChangeValue (1, 0) *= aScale.x(); |
312 | theMatrix.ChangeValue (2, 0) *= aScale.x(); |
313 | theMatrix.ChangeValue (3, 0) *= aScale.x(); |
314 | |
315 | theMatrix.ChangeValue (0, 1) *= aScale.y(); |
316 | theMatrix.ChangeValue (1, 1) *= aScale.y(); |
317 | theMatrix.ChangeValue (2, 1) *= aScale.y(); |
318 | theMatrix.ChangeValue (3, 1) *= aScale.y(); |
319 | |
320 | // Apply translation |
321 | const Graphic3d_Vec2 aTrans = -theParams->Translation(); |
322 | |
323 | theMatrix.ChangeValue (0, 3) = theMatrix.GetValue (0, 0) * aTrans.x() + |
324 | theMatrix.GetValue (0, 1) * aTrans.y(); |
325 | |
326 | theMatrix.ChangeValue (1, 3) = theMatrix.GetValue (1, 0) * aTrans.x() + |
327 | theMatrix.GetValue (1, 1) * aTrans.y(); |
328 | |
329 | theMatrix.ChangeValue (2, 3) = theMatrix.GetValue (2, 0) * aTrans.x() + |
330 | theMatrix.GetValue (2, 1) * aTrans.y(); |
331 | |
332 | // Apply rotation |
333 | const Standard_ShortReal aSin = std::sin ( |
334 | -theParams->Rotation() * static_cast<Standard_ShortReal> (M_PI / 180.0)); |
335 | const Standard_ShortReal aCos = std::cos ( |
336 | -theParams->Rotation() * static_cast<Standard_ShortReal> (M_PI / 180.0)); |
337 | |
338 | BVH_Mat4f aRotationMat; |
339 | aRotationMat.SetValue (0, 0, aCos); |
340 | aRotationMat.SetValue (1, 1, aCos); |
341 | aRotationMat.SetValue (0, 1, -aSin); |
342 | aRotationMat.SetValue (1, 0, aSin); |
343 | |
344 | theMatrix = theMatrix * aRotationMat; |
345 | } |
346 | |
e276548b |
347 | // ======================================================================= |
189f85a3 |
348 | // function : convertMaterial |
e276548b |
349 | // purpose : Creates ray-tracing material properties |
350 | // ======================================================================= |
bf5f0ca2 |
351 | OpenGl_RaytraceMaterial OpenGl_View::convertMaterial (const OpenGl_Aspects* theAspect, |
91c60b57 |
352 | const Handle(OpenGl_Context)& theGlContext) |
e276548b |
353 | { |
61168418 |
354 | OpenGl_RaytraceMaterial aResMat; |
25ef750e |
355 | |
b6472664 |
356 | const Graphic3d_MaterialAspect& aSrcMat = theAspect->Aspect()->FrontMaterial(); |
357 | const OpenGl_Vec3& aMatCol = theAspect->Aspect()->InteriorColor(); |
358 | const float aShine = 128.0f * float(aSrcMat.Shininess()); |
b6472664 |
359 | |
61168418 |
360 | const OpenGl_Vec3& aSrcAmb = aSrcMat.AmbientColor(); |
361 | const OpenGl_Vec3& aSrcDif = aSrcMat.DiffuseColor(); |
362 | const OpenGl_Vec3& aSrcSpe = aSrcMat.SpecularColor(); |
363 | const OpenGl_Vec3& aSrcEms = aSrcMat.EmissiveColor(); |
364 | switch (aSrcMat.MaterialType()) |
b6472664 |
365 | { |
61168418 |
366 | case Graphic3d_MATERIAL_ASPECT: |
367 | { |
368 | aResMat.Ambient .SetValues (aSrcAmb * aMatCol, 1.0f); |
369 | aResMat.Diffuse .SetValues (aSrcDif * aMatCol, -1.0f); // -1 is no texture |
370 | aResMat.Emission.SetValues (aSrcEms * aMatCol, 1.0f); |
371 | break; |
372 | } |
373 | case Graphic3d_MATERIAL_PHYSIC: |
374 | { |
375 | aResMat.Ambient .SetValues (aSrcAmb, 1.0f); |
376 | aResMat.Diffuse .SetValues (aSrcDif, -1.0f); // -1 is no texture |
377 | aResMat.Emission.SetValues (aSrcEms, 1.0f); |
378 | break; |
379 | } |
b6472664 |
380 | } |
381 | |
b6472664 |
382 | { |
61168418 |
383 | // interior color is always ignored for Specular |
384 | aResMat.Specular.SetValues (aSrcSpe, aShine); |
385 | const Standard_ShortReal aMaxRefl = Max (aResMat.Diffuse.x() + aResMat.Specular.x(), |
386 | Max (aResMat.Diffuse.y() + aResMat.Specular.y(), |
387 | aResMat.Diffuse.z() + aResMat.Specular.z())); |
b6472664 |
388 | const Standard_ShortReal aReflectionScale = 0.75f / aMaxRefl; |
61168418 |
389 | aResMat.Reflection.SetValues (aSrcSpe * aReflectionScale, 0.0f); |
b6472664 |
390 | } |
391 | |
392 | const float anIndex = (float )aSrcMat.RefractionIndex(); |
61168418 |
393 | aResMat.Transparency = BVH_Vec4f (aSrcMat.Alpha(), aSrcMat.Transparency(), |
394 | anIndex == 0 ? 1.0f : anIndex, |
395 | anIndex == 0 ? 1.0f : 1.0f / anIndex); |
25ef750e |
396 | |
ba00aab7 |
397 | aResMat.Ambient = theGlContext->Vec4FromQuantityColor (aResMat.Ambient); |
398 | aResMat.Diffuse = theGlContext->Vec4FromQuantityColor (aResMat.Diffuse); |
399 | aResMat.Specular = theGlContext->Vec4FromQuantityColor (aResMat.Specular); |
400 | aResMat.Emission = theGlContext->Vec4FromQuantityColor (aResMat.Emission); |
401 | |
189f85a3 |
402 | // Serialize physically-based material properties |
b6472664 |
403 | const Graphic3d_BSDF& aBSDF = aSrcMat.BSDF(); |
189f85a3 |
404 | |
61168418 |
405 | aResMat.BSDF.Kc = aBSDF.Kc; |
406 | aResMat.BSDF.Ks = aBSDF.Ks; |
72f6dc61 |
407 | aResMat.BSDF.Kd = BVH_Vec4f (aBSDF.Kd, -1.0f); // no base color texture |
408 | aResMat.BSDF.Kt = BVH_Vec4f (aBSDF.Kt, -1.0f); // no metallic-roughness texture |
409 | aResMat.BSDF.Le = BVH_Vec4f (aBSDF.Le, -1.0f); // no emissive texture |
189f85a3 |
410 | |
61168418 |
411 | aResMat.BSDF.Absorption = aBSDF.Absorption; |
189f85a3 |
412 | |
61168418 |
413 | aResMat.BSDF.FresnelCoat = aBSDF.FresnelCoat.Serialize (); |
414 | aResMat.BSDF.FresnelBase = aBSDF.FresnelBase.Serialize (); |
78607702 |
415 | aResMat.BSDF.FresnelBase.w() = -1.0; // no normal map texture |
189f85a3 |
416 | |
417 | // Handle material textures |
cc8cbabe |
418 | if (!theAspect->Aspect()->ToMapTexture()) |
25ef750e |
419 | { |
61168418 |
420 | return aResMat; |
cc8cbabe |
421 | } |
91c60b57 |
422 | |
cc8cbabe |
423 | const Handle(OpenGl_TextureSet)& aTextureSet = theAspect->TextureSet (theGlContext); |
424 | if (aTextureSet.IsNull() |
425 | || aTextureSet->IsEmpty() |
426 | || aTextureSet->First().IsNull()) |
427 | { |
61168418 |
428 | return aResMat; |
cc8cbabe |
429 | } |
91c60b57 |
430 | |
cc8cbabe |
431 | if (theGlContext->HasRayTracingTextures()) |
432 | { |
72f6dc61 |
433 | // write texture ID to diffuse w-components |
434 | for (OpenGl_TextureSet::Iterator aTexIter (aTextureSet); aTexIter.More(); aTexIter.Next()) |
435 | { |
436 | const Handle(OpenGl_Texture)& aTexture = aTexIter.Value(); |
437 | if (aTexIter.Unit() == Graphic3d_TextureUnit_BaseColor) |
438 | { |
439 | buildTextureTransform (aTexture->Sampler()->Parameters(), aResMat.TextureTransform); |
440 | aResMat.Diffuse.w() = aResMat.BSDF.Kd.w() = static_cast<Standard_ShortReal> (myRaytraceGeometry.AddTexture (aTexture)); |
441 | } |
442 | else if (aTexIter.Unit() == Graphic3d_TextureUnit_MetallicRoughness) |
443 | { |
444 | buildTextureTransform (aTexture->Sampler()->Parameters(), aResMat.TextureTransform); |
445 | aResMat.BSDF.Kt.w() = static_cast<Standard_ShortReal> (myRaytraceGeometry.AddTexture (aTexture)); |
446 | } |
447 | else if (aTexIter.Unit() == Graphic3d_TextureUnit_Emissive) |
448 | { |
449 | buildTextureTransform (aTexture->Sampler()->Parameters(), aResMat.TextureTransform); |
450 | aResMat.BSDF.Le.w() = static_cast<Standard_ShortReal> (myRaytraceGeometry.AddTexture (aTexture)); |
451 | } |
78607702 |
452 | else if (aTexIter.Unit() == Graphic3d_TextureUnit_Normal) |
453 | { |
454 | buildTextureTransform (aTexture->Sampler()->Parameters(), aResMat.TextureTransform); |
455 | aResMat.BSDF.FresnelBase.w() = static_cast<Standard_ShortReal> (myRaytraceGeometry.AddTexture (aTexture)); |
456 | } |
72f6dc61 |
457 | } |
cc8cbabe |
458 | } |
459 | else if (!myIsRaytraceWarnTextures) |
460 | { |
61168418 |
461 | theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_HIGH, |
462 | "Warning: texturing in Ray-Trace requires GL_ARB_bindless_texture extension which is missing. " |
463 | "Please try to update graphics card driver. At the moment textures will be ignored."); |
cc8cbabe |
464 | myIsRaytraceWarnTextures = Standard_True; |
25ef750e |
465 | } |
e276548b |
466 | |
61168418 |
467 | return aResMat; |
e276548b |
468 | } |
469 | |
470 | // ======================================================================= |
189f85a3 |
471 | // function : addRaytraceStructure |
e276548b |
472 | // purpose : Adds OpenGL structure to ray-traced scene geometry |
473 | // ======================================================================= |
91c60b57 |
474 | Standard_Boolean OpenGl_View::addRaytraceStructure (const OpenGl_Structure* theStructure, |
475 | const Handle(OpenGl_Context)& theGlContext) |
e276548b |
476 | { |
e276548b |
477 | if (!theStructure->IsVisible()) |
478 | { |
d4aaad5b |
479 | myStructureStates[theStructure] = StructState (theStructure); |
91c60b57 |
480 | |
e276548b |
481 | return Standard_True; |
482 | } |
483 | |
484 | // Get structure material |
2831708b |
485 | OpenGl_RaytraceMaterial aDefaultMaterial; |
1f7f5a90 |
486 | Standard_Boolean aResult = addRaytraceGroups (theStructure, aDefaultMaterial, theStructure->Transformation(), theGlContext); |
0717ddc1 |
487 | |
488 | // Process all connected OpenGL structures |
d4aaad5b |
489 | const OpenGl_Structure* anInstanced = theStructure->InstancedStructure(); |
490 | |
491 | if (anInstanced != NULL && anInstanced->IsRaytracable()) |
0717ddc1 |
492 | { |
1f7f5a90 |
493 | aResult &= addRaytraceGroups (anInstanced, aDefaultMaterial, theStructure->Transformation(), theGlContext); |
0717ddc1 |
494 | } |
495 | |
d4aaad5b |
496 | myStructureStates[theStructure] = StructState (theStructure); |
91c60b57 |
497 | |
498 | return aResult; |
0717ddc1 |
499 | } |
500 | |
501 | // ======================================================================= |
189f85a3 |
502 | // function : addRaytraceGroups |
0717ddc1 |
503 | // purpose : Adds OpenGL groups to ray-traced scene geometry |
504 | // ======================================================================= |
8c820969 |
505 | Standard_Boolean OpenGl_View::addRaytraceGroups (const OpenGl_Structure* theStructure, |
506 | const OpenGl_RaytraceMaterial& theStructMat, |
7f24b768 |
507 | const Handle(TopLoc_Datum3D)& theTrsf, |
8c820969 |
508 | const Handle(OpenGl_Context)& theGlContext) |
0717ddc1 |
509 | { |
1f7f5a90 |
510 | OpenGl_Mat4 aMat4; |
3e05329c |
511 | for (OpenGl_Structure::GroupIterator aGroupIter (theStructure->Groups()); aGroupIter.More(); aGroupIter.Next()) |
e276548b |
512 | { |
513 | // Get group material |
8c820969 |
514 | OpenGl_RaytraceMaterial aGroupMaterial; |
bf5f0ca2 |
515 | if (aGroupIter.Value()->GlAspects() != NULL) |
e276548b |
516 | { |
bf5f0ca2 |
517 | aGroupMaterial = convertMaterial (aGroupIter.Value()->GlAspects(), theGlContext); |
e276548b |
518 | } |
519 | |
8c820969 |
520 | Standard_Integer aMatID = static_cast<Standard_Integer> (myRaytraceGeometry.Materials.size()); |
e276548b |
521 | |
8c820969 |
522 | // Use group material if available, otherwise use structure material |
bf5f0ca2 |
523 | myRaytraceGeometry.Materials.push_back (aGroupIter.Value()->GlAspects() != NULL ? aGroupMaterial : theStructMat); |
e276548b |
524 | |
265d4508 |
525 | // Add OpenGL elements from group (extract primitives arrays and aspects) |
b64d84be |
526 | for (const OpenGl_ElementNode* aNode = aGroupIter.Value()->FirstNode(); aNode != NULL; aNode = aNode->next) |
e276548b |
527 | { |
bf5f0ca2 |
528 | OpenGl_Aspects* anAspect = dynamic_cast<OpenGl_Aspects*> (aNode->elem); |
91c60b57 |
529 | |
5322131b |
530 | if (anAspect != NULL) |
e276548b |
531 | { |
5322131b |
532 | aMatID = static_cast<Standard_Integer> (myRaytraceGeometry.Materials.size()); |
e276548b |
533 | |
91c60b57 |
534 | OpenGl_RaytraceMaterial aMaterial = convertMaterial (anAspect, theGlContext); |
e276548b |
535 | |
5322131b |
536 | myRaytraceGeometry.Materials.push_back (aMaterial); |
e276548b |
537 | } |
5322131b |
538 | else |
e276548b |
539 | { |
540 | OpenGl_PrimitiveArray* aPrimArray = dynamic_cast<OpenGl_PrimitiveArray*> (aNode->elem); |
84c71f29 |
541 | |
e276548b |
542 | if (aPrimArray != NULL) |
543 | { |
8d3f219f |
544 | std::map<Standard_Size, OpenGl_TriangleSet*>::iterator aSetIter = myArrayToTrianglesMap.find (aPrimArray->GetUID()); |
545 | |
84c71f29 |
546 | if (aSetIter != myArrayToTrianglesMap.end()) |
547 | { |
548 | OpenGl_TriangleSet* aSet = aSetIter->second; |
f5b72419 |
549 | opencascade::handle<BVH_Transform<Standard_ShortReal, 4> > aTransform = new BVH_Transform<Standard_ShortReal, 4>(); |
1f7f5a90 |
550 | if (!theTrsf.IsNull()) |
84c71f29 |
551 | { |
1f7f5a90 |
552 | theTrsf->Trsf().GetMat4 (aMat4); |
553 | aTransform->SetTransform (aMat4); |
84c71f29 |
554 | } |
91c60b57 |
555 | |
84c71f29 |
556 | aSet->SetProperties (aTransform); |
25ef750e |
557 | if (aSet->MaterialIndex() != OpenGl_TriangleSet::INVALID_MATERIAL && aSet->MaterialIndex() != aMatID) |
84c71f29 |
558 | { |
559 | aSet->SetMaterialIndex (aMatID); |
560 | } |
561 | } |
562 | else |
563 | { |
f5b72419 |
564 | if (Handle(OpenGl_TriangleSet) aSet = addRaytracePrimitiveArray (aPrimArray, aMatID, 0)) |
84c71f29 |
565 | { |
f5b72419 |
566 | opencascade::handle<BVH_Transform<Standard_ShortReal, 4> > aTransform = new BVH_Transform<Standard_ShortReal, 4>(); |
1f7f5a90 |
567 | if (!theTrsf.IsNull()) |
84c71f29 |
568 | { |
1f7f5a90 |
569 | theTrsf->Trsf().GetMat4 (aMat4); |
570 | aTransform->SetTransform (aMat4); |
84c71f29 |
571 | } |
572 | |
573 | aSet->SetProperties (aTransform); |
84c71f29 |
574 | myRaytraceGeometry.Objects().Append (aSet); |
575 | } |
576 | } |
e276548b |
577 | } |
578 | } |
579 | } |
e276548b |
580 | } |
581 | |
e276548b |
582 | return Standard_True; |
583 | } |
584 | |
585 | // ======================================================================= |
189f85a3 |
586 | // function : addRaytracePrimitiveArray |
e276548b |
587 | // purpose : Adds OpenGL primitive array to ray-traced scene geometry |
588 | // ======================================================================= |
f5b72419 |
589 | Handle(OpenGl_TriangleSet) OpenGl_View::addRaytracePrimitiveArray (const OpenGl_PrimitiveArray* theArray, |
590 | const Standard_Integer theMaterial, |
591 | const OpenGl_Mat4* theTransform) |
e276548b |
592 | { |
91c60b57 |
593 | const Handle(Graphic3d_BoundBuffer)& aBounds = theArray->Bounds(); |
871fa103 |
594 | const Handle(Graphic3d_IndexBuffer)& anIndices = theArray->Indices(); |
595 | const Handle(Graphic3d_Buffer)& anAttribs = theArray->Attributes(); |
91c60b57 |
596 | |
871fa103 |
597 | if (theArray->DrawMode() < GL_TRIANGLES |
91c60b57 |
598 | #ifndef GL_ES_VERSION_2_0 |
871fa103 |
599 | || theArray->DrawMode() > GL_POLYGON |
ca3c13d1 |
600 | #else |
601 | || theArray->DrawMode() > GL_TRIANGLE_FAN |
602 | #endif |
871fa103 |
603 | || anAttribs.IsNull()) |
e276548b |
604 | { |
f5b72419 |
605 | return Handle(OpenGl_TriangleSet)(); |
e276548b |
606 | } |
607 | |
25ef750e |
608 | OpenGl_Mat4 aNormalMatrix; |
25ef750e |
609 | if (theTransform != NULL) |
610 | { |
611 | Standard_ASSERT_RETURN (theTransform->Inverted (aNormalMatrix), |
612 | "Error: Failed to compute normal transformation matrix", NULL); |
613 | |
614 | aNormalMatrix.Transpose(); |
615 | } |
616 | |
f5b72419 |
617 | Handle(OpenGl_TriangleSet) aSet = new OpenGl_TriangleSet (theArray->GetUID(), myRaytraceBVHBuilder); |
e276548b |
618 | { |
871fa103 |
619 | aSet->Vertices.reserve (anAttribs->NbElements); |
91c60b57 |
620 | aSet->Normals.reserve (anAttribs->NbElements); |
621 | aSet->TexCrds.reserve (anAttribs->NbElements); |
25ef750e |
622 | |
871fa103 |
623 | const size_t aVertFrom = aSet->Vertices.size(); |
da87ddc3 |
624 | |
625 | Standard_Integer anAttribIndex = 0; |
626 | Standard_Size anAttribStride = 0; |
627 | if (const Standard_Byte* aPosData = anAttribs->AttributeData (Graphic3d_TOA_POS, anAttribIndex, anAttribStride)) |
265d4508 |
628 | { |
da87ddc3 |
629 | const Graphic3d_Attribute& anAttrib = anAttribs->Attribute (anAttribIndex); |
630 | if (anAttrib.DataType == Graphic3d_TOD_VEC2 |
631 | || anAttrib.DataType == Graphic3d_TOD_VEC3 |
632 | || anAttrib.DataType == Graphic3d_TOD_VEC4) |
871fa103 |
633 | { |
da87ddc3 |
634 | for (Standard_Integer aVertIter = 0; aVertIter < anAttribs->NbElements; ++aVertIter) |
871fa103 |
635 | { |
da87ddc3 |
636 | const float* aCoords = reinterpret_cast<const float*> (aPosData + anAttribStride * aVertIter); |
637 | aSet->Vertices.push_back (BVH_Vec3f (aCoords[0], aCoords[1], anAttrib.DataType != Graphic3d_TOD_VEC2 ? aCoords[2] : 0.0f)); |
871fa103 |
638 | } |
639 | } |
da87ddc3 |
640 | } |
641 | if (const Standard_Byte* aNormData = anAttribs->AttributeData (Graphic3d_TOA_NORM, anAttribIndex, anAttribStride)) |
642 | { |
643 | const Graphic3d_Attribute& anAttrib = anAttribs->Attribute (anAttribIndex); |
644 | if (anAttrib.DataType == Graphic3d_TOD_VEC3 |
645 | || anAttrib.DataType == Graphic3d_TOD_VEC4) |
871fa103 |
646 | { |
da87ddc3 |
647 | for (Standard_Integer aVertIter = 0; aVertIter < anAttribs->NbElements; ++aVertIter) |
871fa103 |
648 | { |
da87ddc3 |
649 | aSet->Normals.push_back (*reinterpret_cast<const Graphic3d_Vec3*> (aNormData + anAttribStride * aVertIter)); |
25ef750e |
650 | } |
651 | } |
da87ddc3 |
652 | } |
653 | if (const Standard_Byte* aTexData = anAttribs->AttributeData (Graphic3d_TOA_UV, anAttribIndex, anAttribStride)) |
654 | { |
655 | const Graphic3d_Attribute& anAttrib = anAttribs->Attribute (anAttribIndex); |
656 | if (anAttrib.DataType == Graphic3d_TOD_VEC2) |
25ef750e |
657 | { |
da87ddc3 |
658 | for (Standard_Integer aVertIter = 0; aVertIter < anAttribs->NbElements; ++aVertIter) |
25ef750e |
659 | { |
da87ddc3 |
660 | aSet->TexCrds.push_back (*reinterpret_cast<const Graphic3d_Vec2*> (aTexData + anAttribStride * aVertIter)); |
871fa103 |
661 | } |
662 | } |
265d4508 |
663 | } |
e276548b |
664 | |
871fa103 |
665 | if (aSet->Normals.size() != aSet->Vertices.size()) |
265d4508 |
666 | { |
871fa103 |
667 | for (Standard_Integer aVertIter = 0; aVertIter < anAttribs->NbElements; ++aVertIter) |
265d4508 |
668 | { |
25ef750e |
669 | aSet->Normals.push_back (BVH_Vec3f()); |
670 | } |
671 | } |
672 | |
673 | if (aSet->TexCrds.size() != aSet->Vertices.size()) |
674 | { |
675 | for (Standard_Integer aVertIter = 0; aVertIter < anAttribs->NbElements; ++aVertIter) |
676 | { |
677 | aSet->TexCrds.push_back (BVH_Vec2f()); |
265d4508 |
678 | } |
871fa103 |
679 | } |
e276548b |
680 | |
25ef750e |
681 | if (theTransform != NULL) |
871fa103 |
682 | { |
683 | for (size_t aVertIter = aVertFrom; aVertIter < aSet->Vertices.size(); ++aVertIter) |
684 | { |
25ef750e |
685 | BVH_Vec3f& aVertex = aSet->Vertices[aVertIter]; |
686 | |
91c60b57 |
687 | BVH_Vec4f aTransVertex = *theTransform * |
688 | BVH_Vec4f (aVertex.x(), aVertex.y(), aVertex.z(), 1.f); |
25ef750e |
689 | |
690 | aVertex = BVH_Vec3f (aTransVertex.x(), aTransVertex.y(), aTransVertex.z()); |
871fa103 |
691 | } |
692 | for (size_t aVertIter = aVertFrom; aVertIter < aSet->Normals.size(); ++aVertIter) |
693 | { |
25ef750e |
694 | BVH_Vec3f& aNormal = aSet->Normals[aVertIter]; |
695 | |
91c60b57 |
696 | BVH_Vec4f aTransNormal = aNormalMatrix * |
697 | BVH_Vec4f (aNormal.x(), aNormal.y(), aNormal.z(), 0.f); |
25ef750e |
698 | |
699 | aNormal = BVH_Vec3f (aTransNormal.x(), aTransNormal.y(), aTransNormal.z()); |
871fa103 |
700 | } |
e276548b |
701 | } |
702 | |
871fa103 |
703 | if (!aBounds.IsNull()) |
265d4508 |
704 | { |
91c60b57 |
705 | for (Standard_Integer aBound = 0, aBoundStart = 0; aBound < aBounds->NbBounds; ++aBound) |
265d4508 |
706 | { |
871fa103 |
707 | const Standard_Integer aVertNum = aBounds->Bounds[aBound]; |
e276548b |
708 | |
91c60b57 |
709 | if (!addRaytraceVertexIndices (*aSet, theMaterial, aVertNum, aBoundStart, *theArray)) |
265d4508 |
710 | { |
f5b72419 |
711 | aSet.Nullify(); |
712 | return Handle(OpenGl_TriangleSet)(); |
265d4508 |
713 | } |
714 | |
715 | aBoundStart += aVertNum; |
716 | } |
717 | } |
718 | else |
e276548b |
719 | { |
871fa103 |
720 | const Standard_Integer aVertNum = !anIndices.IsNull() ? anIndices->NbElements : anAttribs->NbElements; |
e276548b |
721 | |
91c60b57 |
722 | if (!addRaytraceVertexIndices (*aSet, theMaterial, aVertNum, 0, *theArray)) |
e276548b |
723 | { |
f5b72419 |
724 | aSet.Nullify(); |
725 | return Handle(OpenGl_TriangleSet)(); |
e276548b |
726 | } |
e276548b |
727 | } |
728 | } |
e276548b |
729 | |
265d4508 |
730 | if (aSet->Size() != 0) |
91c60b57 |
731 | { |
265d4508 |
732 | aSet->MarkDirty(); |
91c60b57 |
733 | } |
e276548b |
734 | |
265d4508 |
735 | return aSet; |
e276548b |
736 | } |
737 | |
738 | // ======================================================================= |
189f85a3 |
739 | // function : addRaytraceVertexIndices |
e276548b |
740 | // purpose : Adds vertex indices to ray-traced scene geometry |
741 | // ======================================================================= |
91c60b57 |
742 | Standard_Boolean OpenGl_View::addRaytraceVertexIndices (OpenGl_TriangleSet& theSet, |
743 | const Standard_Integer theMatID, |
744 | const Standard_Integer theCount, |
745 | const Standard_Integer theOffset, |
746 | const OpenGl_PrimitiveArray& theArray) |
e276548b |
747 | { |
871fa103 |
748 | switch (theArray.DrawMode()) |
e276548b |
749 | { |
91c60b57 |
750 | case GL_TRIANGLES: return addRaytraceTriangleArray (theSet, theMatID, theCount, theOffset, theArray.Indices()); |
751 | case GL_TRIANGLE_FAN: return addRaytraceTriangleFanArray (theSet, theMatID, theCount, theOffset, theArray.Indices()); |
752 | case GL_TRIANGLE_STRIP: return addRaytraceTriangleStripArray (theSet, theMatID, theCount, theOffset, theArray.Indices()); |
ca3c13d1 |
753 | #if !defined(GL_ES_VERSION_2_0) |
91c60b57 |
754 | case GL_QUAD_STRIP: return addRaytraceQuadrangleStripArray (theSet, theMatID, theCount, theOffset, theArray.Indices()); |
755 | case GL_QUADS: return addRaytraceQuadrangleArray (theSet, theMatID, theCount, theOffset, theArray.Indices()); |
756 | case GL_POLYGON: return addRaytracePolygonArray (theSet, theMatID, theCount, theOffset, theArray.Indices()); |
ca3c13d1 |
757 | #endif |
e276548b |
758 | } |
91c60b57 |
759 | |
871fa103 |
760 | return Standard_False; |
e276548b |
761 | } |
762 | |
763 | // ======================================================================= |
189f85a3 |
764 | // function : addRaytraceTriangleArray |
e276548b |
765 | // purpose : Adds OpenGL triangle array to ray-traced scene geometry |
766 | // ======================================================================= |
91c60b57 |
767 | Standard_Boolean OpenGl_View::addRaytraceTriangleArray (OpenGl_TriangleSet& theSet, |
768 | const Standard_Integer theMatID, |
769 | const Standard_Integer theCount, |
770 | const Standard_Integer theOffset, |
771 | const Handle(Graphic3d_IndexBuffer)& theIndices) |
e276548b |
772 | { |
265d4508 |
773 | if (theCount < 3) |
91c60b57 |
774 | { |
e276548b |
775 | return Standard_True; |
91c60b57 |
776 | } |
e276548b |
777 | |
871fa103 |
778 | theSet.Elements.reserve (theSet.Elements.size() + theCount / 3); |
265d4508 |
779 | |
871fa103 |
780 | if (!theIndices.IsNull()) |
e276548b |
781 | { |
265d4508 |
782 | for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; aVert += 3) |
e276548b |
783 | { |
871fa103 |
784 | theSet.Elements.push_back (BVH_Vec4i (theIndices->Index (aVert + 0), |
785 | theIndices->Index (aVert + 1), |
786 | theIndices->Index (aVert + 2), |
787 | theMatID)); |
e276548b |
788 | } |
789 | } |
790 | else |
791 | { |
265d4508 |
792 | for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; aVert += 3) |
e276548b |
793 | { |
91c60b57 |
794 | theSet.Elements.push_back (BVH_Vec4i (aVert + 0, aVert + 1, aVert + 2, theMatID)); |
e276548b |
795 | } |
796 | } |
797 | |
798 | return Standard_True; |
799 | } |
800 | |
801 | // ======================================================================= |
189f85a3 |
802 | // function : addRaytraceTriangleFanArray |
e276548b |
803 | // purpose : Adds OpenGL triangle fan array to ray-traced scene geometry |
804 | // ======================================================================= |
91c60b57 |
805 | Standard_Boolean OpenGl_View::addRaytraceTriangleFanArray (OpenGl_TriangleSet& theSet, |
806 | const Standard_Integer theMatID, |
807 | const Standard_Integer theCount, |
808 | const Standard_Integer theOffset, |
809 | const Handle(Graphic3d_IndexBuffer)& theIndices) |
e276548b |
810 | { |
265d4508 |
811 | if (theCount < 3) |
91c60b57 |
812 | { |
e276548b |
813 | return Standard_True; |
91c60b57 |
814 | } |
e276548b |
815 | |
871fa103 |
816 | theSet.Elements.reserve (theSet.Elements.size() + theCount - 2); |
265d4508 |
817 | |
871fa103 |
818 | if (!theIndices.IsNull()) |
e276548b |
819 | { |
265d4508 |
820 | for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; ++aVert) |
e276548b |
821 | { |
871fa103 |
822 | theSet.Elements.push_back (BVH_Vec4i (theIndices->Index (theOffset), |
823 | theIndices->Index (aVert + 1), |
824 | theIndices->Index (aVert + 2), |
825 | theMatID)); |
e276548b |
826 | } |
827 | } |
828 | else |
829 | { |
265d4508 |
830 | for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; ++aVert) |
e276548b |
831 | { |
871fa103 |
832 | theSet.Elements.push_back (BVH_Vec4i (theOffset, |
833 | aVert + 1, |
834 | aVert + 2, |
835 | theMatID)); |
e276548b |
836 | } |
837 | } |
838 | |
839 | return Standard_True; |
840 | } |
841 | |
842 | // ======================================================================= |
189f85a3 |
843 | // function : addRaytraceTriangleStripArray |
e276548b |
844 | // purpose : Adds OpenGL triangle strip array to ray-traced scene geometry |
845 | // ======================================================================= |
91c60b57 |
846 | Standard_Boolean OpenGl_View::addRaytraceTriangleStripArray (OpenGl_TriangleSet& theSet, |
847 | const Standard_Integer theMatID, |
848 | const Standard_Integer theCount, |
849 | const Standard_Integer theOffset, |
850 | const Handle(Graphic3d_IndexBuffer)& theIndices) |
e276548b |
851 | { |
265d4508 |
852 | if (theCount < 3) |
91c60b57 |
853 | { |
e276548b |
854 | return Standard_True; |
91c60b57 |
855 | } |
e276548b |
856 | |
871fa103 |
857 | theSet.Elements.reserve (theSet.Elements.size() + theCount - 2); |
265d4508 |
858 | |
871fa103 |
859 | if (!theIndices.IsNull()) |
e276548b |
860 | { |
265d4508 |
861 | for (Standard_Integer aVert = theOffset, aCW = 0; aVert < theOffset + theCount - 2; ++aVert, aCW = (aCW + 1) % 2) |
e276548b |
862 | { |
ad2a55b2 |
863 | theSet.Elements.push_back (BVH_Vec4i (theIndices->Index (aVert + (aCW ? 1 : 0)), |
864 | theIndices->Index (aVert + (aCW ? 0 : 1)), |
871fa103 |
865 | theIndices->Index (aVert + 2), |
866 | theMatID)); |
e276548b |
867 | } |
868 | } |
869 | else |
870 | { |
265d4508 |
871 | for (Standard_Integer aVert = theOffset, aCW = 0; aVert < theOffset + theCount - 2; ++aVert, aCW = (aCW + 1) % 2) |
e276548b |
872 | { |
ad2a55b2 |
873 | theSet.Elements.push_back (BVH_Vec4i (aVert + (aCW ? 1 : 0), |
874 | aVert + (aCW ? 0 : 1), |
871fa103 |
875 | aVert + 2, |
876 | theMatID)); |
e276548b |
877 | } |
878 | } |
879 | |
880 | return Standard_True; |
881 | } |
882 | |
883 | // ======================================================================= |
189f85a3 |
884 | // function : addRaytraceQuadrangleArray |
e276548b |
885 | // purpose : Adds OpenGL quad array to ray-traced scene geometry |
886 | // ======================================================================= |
91c60b57 |
887 | Standard_Boolean OpenGl_View::addRaytraceQuadrangleArray (OpenGl_TriangleSet& theSet, |
888 | const Standard_Integer theMatID, |
889 | const Standard_Integer theCount, |
890 | const Standard_Integer theOffset, |
891 | const Handle(Graphic3d_IndexBuffer)& theIndices) |
e276548b |
892 | { |
265d4508 |
893 | if (theCount < 4) |
91c60b57 |
894 | { |
e276548b |
895 | return Standard_True; |
91c60b57 |
896 | } |
e276548b |
897 | |
871fa103 |
898 | theSet.Elements.reserve (theSet.Elements.size() + theCount / 2); |
265d4508 |
899 | |
871fa103 |
900 | if (!theIndices.IsNull()) |
e276548b |
901 | { |
265d4508 |
902 | for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 3; aVert += 4) |
e276548b |
903 | { |
871fa103 |
904 | theSet.Elements.push_back (BVH_Vec4i (theIndices->Index (aVert + 0), |
905 | theIndices->Index (aVert + 1), |
906 | theIndices->Index (aVert + 2), |
907 | theMatID)); |
908 | theSet.Elements.push_back (BVH_Vec4i (theIndices->Index (aVert + 0), |
909 | theIndices->Index (aVert + 2), |
910 | theIndices->Index (aVert + 3), |
911 | theMatID)); |
e276548b |
912 | } |
913 | } |
914 | else |
915 | { |
265d4508 |
916 | for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 3; aVert += 4) |
e276548b |
917 | { |
871fa103 |
918 | theSet.Elements.push_back (BVH_Vec4i (aVert + 0, aVert + 1, aVert + 2, |
919 | theMatID)); |
920 | theSet.Elements.push_back (BVH_Vec4i (aVert + 0, aVert + 2, aVert + 3, |
921 | theMatID)); |
e276548b |
922 | } |
923 | } |
924 | |
925 | return Standard_True; |
926 | } |
927 | |
928 | // ======================================================================= |
189f85a3 |
929 | // function : addRaytraceQuadrangleStripArray |
e276548b |
930 | // purpose : Adds OpenGL quad strip array to ray-traced scene geometry |
931 | // ======================================================================= |
91c60b57 |
932 | Standard_Boolean OpenGl_View::addRaytraceQuadrangleStripArray (OpenGl_TriangleSet& theSet, |
933 | const Standard_Integer theMatID, |
934 | const Standard_Integer theCount, |
935 | const Standard_Integer theOffset, |
936 | const Handle(Graphic3d_IndexBuffer)& theIndices) |
e276548b |
937 | { |
265d4508 |
938 | if (theCount < 4) |
91c60b57 |
939 | { |
e276548b |
940 | return Standard_True; |
91c60b57 |
941 | } |
e276548b |
942 | |
871fa103 |
943 | theSet.Elements.reserve (theSet.Elements.size() + 2 * theCount - 6); |
265d4508 |
944 | |
871fa103 |
945 | if (!theIndices.IsNull()) |
e276548b |
946 | { |
265d4508 |
947 | for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 3; aVert += 2) |
e276548b |
948 | { |
871fa103 |
949 | theSet.Elements.push_back (BVH_Vec4i (theIndices->Index (aVert + 0), |
950 | theIndices->Index (aVert + 1), |
951 | theIndices->Index (aVert + 2), |
952 | theMatID)); |
953 | |
954 | theSet.Elements.push_back (BVH_Vec4i (theIndices->Index (aVert + 1), |
955 | theIndices->Index (aVert + 3), |
956 | theIndices->Index (aVert + 2), |
957 | theMatID)); |
e276548b |
958 | } |
959 | } |
960 | else |
961 | { |
265d4508 |
962 | for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 3; aVert += 2) |
e276548b |
963 | { |
871fa103 |
964 | theSet.Elements.push_back (BVH_Vec4i (aVert + 0, |
965 | aVert + 1, |
966 | aVert + 2, |
967 | theMatID)); |
968 | |
969 | theSet.Elements.push_back (BVH_Vec4i (aVert + 1, |
970 | aVert + 3, |
971 | aVert + 2, |
972 | theMatID)); |
e276548b |
973 | } |
974 | } |
975 | |
976 | return Standard_True; |
977 | } |
978 | |
979 | // ======================================================================= |
189f85a3 |
980 | // function : addRaytracePolygonArray |
e276548b |
981 | // purpose : Adds OpenGL polygon array to ray-traced scene geometry |
982 | // ======================================================================= |
91c60b57 |
983 | Standard_Boolean OpenGl_View::addRaytracePolygonArray (OpenGl_TriangleSet& theSet, |
984 | const Standard_Integer theMatID, |
985 | const Standard_Integer theCount, |
986 | const Standard_Integer theOffset, |
987 | const Handle(Graphic3d_IndexBuffer)& theIndices) |
e276548b |
988 | { |
265d4508 |
989 | if (theCount < 3) |
91c60b57 |
990 | { |
e276548b |
991 | return Standard_True; |
91c60b57 |
992 | } |
e276548b |
993 | |
871fa103 |
994 | theSet.Elements.reserve (theSet.Elements.size() + theCount - 2); |
265d4508 |
995 | |
871fa103 |
996 | if (!theIndices.IsNull()) |
e276548b |
997 | { |
265d4508 |
998 | for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; ++aVert) |
e276548b |
999 | { |
871fa103 |
1000 | theSet.Elements.push_back (BVH_Vec4i (theIndices->Index (theOffset), |
1001 | theIndices->Index (aVert + 1), |
1002 | theIndices->Index (aVert + 2), |
1003 | theMatID)); |
e276548b |
1004 | } |
1005 | } |
1006 | else |
1007 | { |
265d4508 |
1008 | for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; ++aVert) |
e276548b |
1009 | { |
871fa103 |
1010 | theSet.Elements.push_back (BVH_Vec4i (theOffset, |
1011 | aVert + 1, |
1012 | aVert + 2, |
1013 | theMatID)); |
e276548b |
1014 | } |
1015 | } |
1016 | |
1017 | return Standard_True; |
1018 | } |
1019 | |
189f85a3 |
1020 | const TCollection_AsciiString OpenGl_View::ShaderSource::EMPTY_PREFIX; |
1021 | |
e276548b |
1022 | // ======================================================================= |
fc73a202 |
1023 | // function : Source |
1024 | // purpose : Returns shader source combined with prefix |
e276548b |
1025 | // ======================================================================= |
93cdaa76 |
1026 | TCollection_AsciiString OpenGl_View::ShaderSource::Source (const Handle(OpenGl_Context)& theCtx, |
1027 | const GLenum theType) const |
e276548b |
1028 | { |
93cdaa76 |
1029 | TCollection_AsciiString aVersion = |
1030 | #if defined(GL_ES_VERSION_2_0) |
1031 | "#version 320 es\n"; |
1032 | #else |
1033 | "#version 140\n"; |
1034 | #endif |
1035 | TCollection_AsciiString aPrecisionHeader; |
1036 | if (theType == GL_FRAGMENT_SHADER) |
1037 | { |
1038 | #if defined(GL_ES_VERSION_2_0) |
1039 | aPrecisionHeader = theCtx->hasHighp |
1040 | ? "precision highp float;\n" |
1041 | "precision highp int;\n" |
1042 | "precision highp samplerBuffer;\n" |
1043 | "precision highp isamplerBuffer;\n" |
1044 | : "precision mediump float;\n" |
1045 | "precision mediump int;\n" |
1046 | "precision mediump samplerBuffer;\n" |
1047 | "precision mediump isamplerBuffer;\n"; |
1048 | #else |
1049 | (void )theCtx; |
1050 | #endif |
1051 | } |
fc73a202 |
1052 | if (myPrefix.IsEmpty()) |
e276548b |
1053 | { |
93cdaa76 |
1054 | return aVersion + aPrecisionHeader + mySource; |
e276548b |
1055 | } |
93cdaa76 |
1056 | return aVersion + aPrecisionHeader + myPrefix + "\n" + mySource; |
e276548b |
1057 | } |
1058 | |
1059 | // ======================================================================= |
ee5befae |
1060 | // function : LoadFromFiles |
fc73a202 |
1061 | // purpose : Loads shader source from specified files |
e276548b |
1062 | // ======================================================================= |
ee5befae |
1063 | Standard_Boolean OpenGl_View::ShaderSource::LoadFromFiles (const TCollection_AsciiString* theFileNames, |
1064 | const TCollection_AsciiString& thePrefix) |
e276548b |
1065 | { |
73722cc9 |
1066 | myError.Clear(); |
fc73a202 |
1067 | mySource.Clear(); |
ee5befae |
1068 | myPrefix = thePrefix; |
1069 | |
73722cc9 |
1070 | TCollection_AsciiString aMissingFiles; |
189f85a3 |
1071 | for (Standard_Integer anIndex = 0; !theFileNames[anIndex].IsEmpty(); ++anIndex) |
fc73a202 |
1072 | { |
1073 | OSD_File aFile (theFileNames[anIndex]); |
73722cc9 |
1074 | if (aFile.Exists()) |
1075 | { |
1076 | aFile.Open (OSD_ReadOnly, OSD_Protection()); |
1077 | } |
1078 | if (!aFile.IsOpen()) |
1079 | { |
1080 | if (!aMissingFiles.IsEmpty()) |
1081 | { |
1082 | aMissingFiles += ", "; |
1083 | } |
1084 | aMissingFiles += TCollection_AsciiString("'") + theFileNames[anIndex] + "'"; |
1085 | continue; |
1086 | } |
1087 | else if (!aMissingFiles.IsEmpty()) |
1088 | { |
1089 | aFile.Close(); |
1090 | continue; |
1091 | } |
265d4508 |
1092 | |
fc73a202 |
1093 | TCollection_AsciiString aSource; |
fc73a202 |
1094 | aFile.Read (aSource, (Standard_Integer) aFile.Size()); |
fc73a202 |
1095 | if (!aSource.IsEmpty()) |
1096 | { |
1097 | mySource += TCollection_AsciiString ("\n") + aSource; |
1098 | } |
fc73a202 |
1099 | aFile.Close(); |
68333c8f |
1100 | } |
189f85a3 |
1101 | |
73722cc9 |
1102 | if (!aMissingFiles.IsEmpty()) |
1103 | { |
1104 | myError = TCollection_AsciiString("Shader files ") + aMissingFiles + " are missing or inaccessible"; |
1105 | return Standard_False; |
1106 | } |
1107 | return Standard_True; |
e276548b |
1108 | } |
1109 | |
ee5befae |
1110 | // ======================================================================= |
1111 | // function : LoadFromStrings |
1112 | // purpose : |
1113 | // ======================================================================= |
1114 | Standard_Boolean OpenGl_View::ShaderSource::LoadFromStrings (const TCollection_AsciiString* theStrings, |
1115 | const TCollection_AsciiString& thePrefix) |
1116 | { |
1117 | myError.Clear(); |
1118 | mySource.Clear(); |
1119 | myPrefix = thePrefix; |
1120 | |
1121 | for (Standard_Integer anIndex = 0; !theStrings[anIndex].IsEmpty(); ++anIndex) |
1122 | { |
1123 | TCollection_AsciiString aSource = theStrings[anIndex]; |
1124 | if (!aSource.IsEmpty()) |
1125 | { |
1126 | mySource += TCollection_AsciiString ("\n") + aSource; |
1127 | } |
1128 | } |
1129 | return Standard_True; |
1130 | } |
1131 | |
e276548b |
1132 | // ======================================================================= |
189f85a3 |
1133 | // function : generateShaderPrefix |
91c60b57 |
1134 | // purpose : Generates shader prefix based on current ray-tracing options |
1135 | // ======================================================================= |
1136 | TCollection_AsciiString OpenGl_View::generateShaderPrefix (const Handle(OpenGl_Context)& theGlContext) const |
1137 | { |
1138 | TCollection_AsciiString aPrefixString = |
1139 | TCollection_AsciiString ("#define STACK_SIZE ") + TCollection_AsciiString (myRaytraceParameters.StackSize) + "\n" + |
1140 | TCollection_AsciiString ("#define NB_BOUNCES ") + TCollection_AsciiString (myRaytraceParameters.NbBounces); |
1141 | |
e70625d6 |
1142 | if (myRaytraceParameters.IsZeroToOneDepth) |
1143 | { |
1144 | aPrefixString += TCollection_AsciiString ("\n#define THE_ZERO_TO_ONE_DEPTH"); |
1145 | } |
1146 | |
91c60b57 |
1147 | if (myRaytraceParameters.TransparentShadows) |
1148 | { |
1149 | aPrefixString += TCollection_AsciiString ("\n#define TRANSPARENT_SHADOWS"); |
1150 | } |
ba00aab7 |
1151 | if (!theGlContext->ToRenderSRGB()) |
1152 | { |
1153 | aPrefixString += TCollection_AsciiString ("\n#define THE_SHIFT_sRGB"); |
1154 | } |
91c60b57 |
1155 | |
f483f2ed |
1156 | // If OpenGL driver supports bindless textures and texturing |
1157 | // is actually used, activate texturing in ray-tracing mode |
1158 | if (myRaytraceParameters.UseBindlessTextures && theGlContext->arbTexBindless != NULL) |
91c60b57 |
1159 | { |
1160 | aPrefixString += TCollection_AsciiString ("\n#define USE_TEXTURES") + |
1161 | TCollection_AsciiString ("\n#define MAX_TEX_NUMBER ") + TCollection_AsciiString (OpenGl_RaytraceGeometry::MAX_TEX_NUMBER); |
1162 | } |
1163 | |
3a9b5dc8 |
1164 | if (myRaytraceParameters.GlobalIllumination) // path tracing activated |
189f85a3 |
1165 | { |
1166 | aPrefixString += TCollection_AsciiString ("\n#define PATH_TRACING"); |
3a9b5dc8 |
1167 | |
1168 | if (myRaytraceParameters.AdaptiveScreenSampling) // adaptive screen sampling requested |
1169 | { |
e084dbbc |
1170 | if (theGlContext->IsGlGreaterEqual (4, 4)) |
3a9b5dc8 |
1171 | { |
66d1cdc6 |
1172 | aPrefixString += TCollection_AsciiString ("\n#define ADAPTIVE_SAMPLING"); |
e084dbbc |
1173 | if (myRaytraceParameters.AdaptiveScreenSamplingAtomic |
1174 | && theGlContext->CheckExtension ("GL_NV_shader_atomic_float")) |
1175 | { |
1176 | aPrefixString += TCollection_AsciiString ("\n#define ADAPTIVE_SAMPLING_ATOMIC"); |
1177 | } |
3a9b5dc8 |
1178 | } |
1179 | } |
b4327ba8 |
1180 | |
1181 | if (myRaytraceParameters.TwoSidedBsdfModels) // two-sided BSDFs requested |
1182 | { |
1183 | aPrefixString += TCollection_AsciiString ("\n#define TWO_SIDED_BXDF"); |
1184 | } |
eb85ed36 |
1185 | |
1186 | switch (myRaytraceParameters.ToneMappingMethod) |
1187 | { |
1188 | case Graphic3d_ToneMappingMethod_Disabled: |
1189 | break; |
1190 | case Graphic3d_ToneMappingMethod_Filmic: |
1191 | aPrefixString += TCollection_AsciiString ("\n#define TONE_MAPPING_FILMIC"); |
1192 | break; |
1193 | } |
189f85a3 |
1194 | } |
1195 | |
78607702 |
1196 | if (myRaytraceParameters.ToIgnoreNormalMap) |
1197 | { |
1198 | aPrefixString += TCollection_AsciiString("\n#define IGNORE_NORMAL_MAP"); |
1199 | } |
1200 | |
89d855ba |
1201 | if (myRaytraceParameters.CubemapForBack) |
1202 | { |
1203 | aPrefixString += TCollection_AsciiString("\n#define BACKGROUND_CUBEMAP"); |
1204 | } |
1205 | |
b27ab03d |
1206 | if (myRaytraceParameters.DepthOfField) |
1207 | { |
1208 | aPrefixString += TCollection_AsciiString("\n#define DEPTH_OF_FIELD"); |
1209 | } |
1210 | |
91c60b57 |
1211 | return aPrefixString; |
1212 | } |
1213 | |
1214 | // ======================================================================= |
189f85a3 |
1215 | // function : safeFailBack |
91c60b57 |
1216 | // purpose : Performs safe exit when shaders initialization fails |
1217 | // ======================================================================= |
1218 | Standard_Boolean OpenGl_View::safeFailBack (const TCollection_ExtendedString& theMessage, |
1219 | const Handle(OpenGl_Context)& theGlContext) |
1220 | { |
3b523c4c |
1221 | theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION, |
1222 | GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, theMessage); |
91c60b57 |
1223 | |
1224 | myRaytraceInitStatus = OpenGl_RT_FAIL; |
1225 | |
1226 | releaseRaytraceResources (theGlContext); |
1227 | |
1228 | return Standard_False; |
1229 | } |
1230 | |
1231 | // ======================================================================= |
189f85a3 |
1232 | // function : initShader |
fc73a202 |
1233 | // purpose : Creates new shader object with specified source |
e276548b |
1234 | // ======================================================================= |
91c60b57 |
1235 | Handle(OpenGl_ShaderObject) OpenGl_View::initShader (const GLenum theType, |
1236 | const ShaderSource& theSource, |
1237 | const Handle(OpenGl_Context)& theGlContext) |
e276548b |
1238 | { |
fc73a202 |
1239 | Handle(OpenGl_ShaderObject) aShader = new OpenGl_ShaderObject (theType); |
91c60b57 |
1240 | if (!aShader->Create (theGlContext)) |
e276548b |
1241 | { |
74413ca7 |
1242 | theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, |
1243 | TCollection_ExtendedString ("Error: Failed to create ") + |
1244 | (theType == GL_VERTEX_SHADER ? "vertex" : "fragment") + " shader object"); |
1245 | aShader->Release (theGlContext.get()); |
fc73a202 |
1246 | return Handle(OpenGl_ShaderObject)(); |
1247 | } |
e276548b |
1248 | |
93cdaa76 |
1249 | if (!aShader->LoadAndCompile (theGlContext, "", theSource.Source (theGlContext, theType))) |
e276548b |
1250 | { |
74413ca7 |
1251 | aShader->Release (theGlContext.get()); |
fc73a202 |
1252 | return Handle(OpenGl_ShaderObject)(); |
1253 | } |
fc73a202 |
1254 | return aShader; |
e276548b |
1255 | } |
1256 | |
1257 | // ======================================================================= |
189f85a3 |
1258 | // function : initProgram |
1259 | // purpose : Creates GLSL program from the given shader objects |
1260 | // ======================================================================= |
1261 | Handle(OpenGl_ShaderProgram) OpenGl_View::initProgram (const Handle(OpenGl_Context)& theGlContext, |
1262 | const Handle(OpenGl_ShaderObject)& theVertShader, |
d95f5ce1 |
1263 | const Handle(OpenGl_ShaderObject)& theFragShader, |
1264 | const TCollection_AsciiString& theName) |
189f85a3 |
1265 | { |
d95f5ce1 |
1266 | const TCollection_AsciiString anId = TCollection_AsciiString("occt_rt_") + theName; |
1267 | Handle(OpenGl_ShaderProgram) aProgram = new OpenGl_ShaderProgram(Handle(Graphic3d_ShaderProgram)(), anId); |
189f85a3 |
1268 | |
1269 | if (!aProgram->Create (theGlContext)) |
1270 | { |
1271 | theVertShader->Release (theGlContext.operator->()); |
1272 | |
3b523c4c |
1273 | theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION, |
1274 | GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, "Failed to create shader program"); |
189f85a3 |
1275 | |
1276 | return Handle(OpenGl_ShaderProgram)(); |
1277 | } |
1278 | |
1279 | if (!aProgram->AttachShader (theGlContext, theVertShader) |
1280 | || !aProgram->AttachShader (theGlContext, theFragShader)) |
1281 | { |
1282 | theVertShader->Release (theGlContext.operator->()); |
1283 | |
3b523c4c |
1284 | theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION, |
1285 | GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, "Failed to attach shader objects"); |
189f85a3 |
1286 | |
1287 | return Handle(OpenGl_ShaderProgram)(); |
1288 | } |
1289 | |
1290 | aProgram->SetAttributeName (theGlContext, Graphic3d_TOA_POS, "occVertex"); |
1291 | |
1292 | TCollection_AsciiString aLinkLog; |
1293 | |
1294 | if (!aProgram->Link (theGlContext)) |
1295 | { |
1296 | aProgram->FetchInfoLog (theGlContext, aLinkLog); |
1297 | |
1298 | const TCollection_ExtendedString aMessage = TCollection_ExtendedString ( |
1299 | "Failed to link shader program:\n") + aLinkLog; |
1300 | |
3b523c4c |
1301 | theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION, |
1302 | GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMessage); |
189f85a3 |
1303 | |
1304 | return Handle(OpenGl_ShaderProgram)(); |
1305 | } |
1306 | else if (theGlContext->caps->glslWarnings) |
1307 | { |
47e9c178 |
1308 | aProgram->FetchInfoLog (theGlContext, aLinkLog); |
189f85a3 |
1309 | if (!aLinkLog.IsEmpty() && !aLinkLog.IsEqual ("No errors.\n")) |
1310 | { |
1311 | const TCollection_ExtendedString aMessage = TCollection_ExtendedString ( |
1312 | "Shader program was linked with following warnings:\n") + aLinkLog; |
1313 | |
3b523c4c |
1314 | theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION, |
1315 | GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_LOW, aMessage); |
189f85a3 |
1316 | } |
1317 | } |
1318 | |
1319 | return aProgram; |
1320 | } |
1321 | |
1322 | // ======================================================================= |
1323 | // function : initRaytraceResources |
91c60b57 |
1324 | // purpose : Initializes OpenGL/GLSL shader programs |
e276548b |
1325 | // ======================================================================= |
66d1cdc6 |
1326 | Standard_Boolean OpenGl_View::initRaytraceResources (const Standard_Integer theSizeX, |
1327 | const Standard_Integer theSizeY, |
1328 | const Handle(OpenGl_Context)& theGlContext) |
e276548b |
1329 | { |
91c60b57 |
1330 | if (myRaytraceInitStatus == OpenGl_RT_FAIL) |
1331 | { |
1332 | return Standard_False; |
1333 | } |
265d4508 |
1334 | |
91c60b57 |
1335 | Standard_Boolean aToRebuildShaders = Standard_False; |
265d4508 |
1336 | |
d877e610 |
1337 | if (myRenderParams.RebuildRayTracingShaders) // requires complete re-initialization |
1338 | { |
1339 | myRaytraceInitStatus = OpenGl_RT_NONE; |
1340 | releaseRaytraceResources (theGlContext, Standard_True); |
1341 | myRenderParams.RebuildRayTracingShaders = Standard_False; // clear rebuilding flag |
1342 | } |
1343 | |
91c60b57 |
1344 | if (myRaytraceInitStatus == OpenGl_RT_INIT) |
1345 | { |
1346 | if (!myIsRaytraceDataValid) |
d877e610 |
1347 | { |
91c60b57 |
1348 | return Standard_True; |
d877e610 |
1349 | } |
07039714 |
1350 | |
91c60b57 |
1351 | const Standard_Integer aRequiredStackSize = |
f2474958 |
1352 | myRaytraceGeometry.TopLevelTreeDepth() + myRaytraceGeometry.BotLevelTreeDepth(); |
e276548b |
1353 | |
91c60b57 |
1354 | if (myRaytraceParameters.StackSize < aRequiredStackSize) |
1355 | { |
1356 | myRaytraceParameters.StackSize = Max (aRequiredStackSize, THE_DEFAULT_STACK_SIZE); |
e276548b |
1357 | |
fc73a202 |
1358 | aToRebuildShaders = Standard_True; |
1359 | } |
1360 | else |
1361 | { |
bc8c79bb |
1362 | if (aRequiredStackSize < myRaytraceParameters.StackSize) |
fc73a202 |
1363 | { |
bc8c79bb |
1364 | if (myRaytraceParameters.StackSize > THE_DEFAULT_STACK_SIZE) |
fc73a202 |
1365 | { |
bc8c79bb |
1366 | myRaytraceParameters.StackSize = Max (aRequiredStackSize, THE_DEFAULT_STACK_SIZE); |
fc73a202 |
1367 | aToRebuildShaders = Standard_True; |
1368 | } |
1369 | } |
1370 | } |
e276548b |
1371 | |
e70625d6 |
1372 | const bool isZeroToOneDepth = myCaps->useZeroToOneDepth |
1373 | && myWorkspace->GetGlContext()->arbClipControl; |
1374 | if (isZeroToOneDepth != myRaytraceParameters.IsZeroToOneDepth |
1375 | || myRenderParams.RaytracingDepth != myRaytraceParameters.NbBounces |
b4327ba8 |
1376 | || myRenderParams.IsTransparentShadowEnabled != myRaytraceParameters.TransparentShadows |
1377 | || myRenderParams.IsGlobalIlluminationEnabled != myRaytraceParameters.GlobalIllumination |
1378 | || myRenderParams.TwoSidedBsdfModels != myRaytraceParameters.TwoSidedBsdfModels |
78607702 |
1379 | || myRaytraceGeometry.HasTextures() != myRaytraceParameters.UseBindlessTextures |
1380 | || myRenderParams.ToIgnoreNormalMapInRayTracing != myRaytraceParameters.ToIgnoreNormalMap) |
f483f2ed |
1381 | { |
e70625d6 |
1382 | myRaytraceParameters.IsZeroToOneDepth = isZeroToOneDepth; |
b4327ba8 |
1383 | myRaytraceParameters.NbBounces = myRenderParams.RaytracingDepth; |
1384 | myRaytraceParameters.TransparentShadows = myRenderParams.IsTransparentShadowEnabled; |
1385 | myRaytraceParameters.GlobalIllumination = myRenderParams.IsGlobalIlluminationEnabled; |
1386 | myRaytraceParameters.TwoSidedBsdfModels = myRenderParams.TwoSidedBsdfModels; |
f483f2ed |
1387 | myRaytraceParameters.UseBindlessTextures = myRaytraceGeometry.HasTextures(); |
78607702 |
1388 | myRaytraceParameters.ToIgnoreNormalMap = myRenderParams.ToIgnoreNormalMapInRayTracing; |
189f85a3 |
1389 | aToRebuildShaders = Standard_True; |
1390 | } |
1391 | |
e084dbbc |
1392 | if (myRenderParams.AdaptiveScreenSampling != myRaytraceParameters.AdaptiveScreenSampling |
1393 | || myRenderParams.AdaptiveScreenSamplingAtomic != myRaytraceParameters.AdaptiveScreenSamplingAtomic) |
3a9b5dc8 |
1394 | { |
e084dbbc |
1395 | myRaytraceParameters.AdaptiveScreenSampling = myRenderParams.AdaptiveScreenSampling; |
1396 | myRaytraceParameters.AdaptiveScreenSamplingAtomic = myRenderParams.AdaptiveScreenSamplingAtomic; |
3a9b5dc8 |
1397 | if (myRenderParams.AdaptiveScreenSampling) // adaptive sampling was requested |
1398 | { |
1399 | if (!theGlContext->HasRayTracingAdaptiveSampling()) |
1400 | { |
1401 | // disable the feature if it is not supported |
1402 | myRaytraceParameters.AdaptiveScreenSampling = myRenderParams.AdaptiveScreenSampling = Standard_False; |
1403 | theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_LOW, |
e084dbbc |
1404 | "Adaptive sampling is not supported (OpenGL 4.4 is missing)"); |
1405 | } |
1406 | else if (myRaytraceParameters.AdaptiveScreenSamplingAtomic |
1407 | && !theGlContext->HasRayTracingAdaptiveSamplingAtomic()) |
1408 | { |
1409 | // disable the feature if it is not supported |
1410 | myRaytraceParameters.AdaptiveScreenSamplingAtomic = myRenderParams.AdaptiveScreenSamplingAtomic = Standard_False; |
1411 | theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_LOW, |
1412 | "Atomic adaptive sampling is not supported (GL_NV_shader_atomic_float is missing)"); |
3a9b5dc8 |
1413 | } |
1414 | } |
1415 | |
1416 | aToRebuildShaders = Standard_True; |
1417 | } |
66d1cdc6 |
1418 | myTileSampler.SetSize (myRenderParams, myRaytraceParameters.AdaptiveScreenSampling ? Graphic3d_Vec2i (theSizeX, theSizeY) : Graphic3d_Vec2i (0, 0)); |
3a9b5dc8 |
1419 | |
89d855ba |
1420 | const bool isCubemapForBack = !myBackgroundCubeMap.IsNull(); |
1421 | if (myRaytraceParameters.CubemapForBack != isCubemapForBack) |
1422 | { |
1423 | myRaytraceParameters.CubemapForBack = isCubemapForBack; |
1424 | aToRebuildShaders = Standard_True; |
1425 | } |
1426 | |
b27ab03d |
1427 | const bool toEnableDof = !myCamera->IsOrthographic() && myRaytraceParameters.GlobalIllumination; |
1428 | if (myRaytraceParameters.DepthOfField != toEnableDof) |
1429 | { |
1430 | myRaytraceParameters.DepthOfField = toEnableDof; |
1431 | aToRebuildShaders = Standard_True; |
1432 | } |
1433 | |
eb85ed36 |
1434 | if (myRenderParams.ToneMappingMethod != myRaytraceParameters.ToneMappingMethod) |
1435 | { |
1436 | myRaytraceParameters.ToneMappingMethod = myRenderParams.ToneMappingMethod; |
1437 | aToRebuildShaders = true; |
1438 | } |
1439 | |
fc73a202 |
1440 | if (aToRebuildShaders) |
1441 | { |
189f85a3 |
1442 | // Reject accumulated frames |
1443 | myAccumFrames = 0; |
e276548b |
1444 | |
3a9b5dc8 |
1445 | // Environment map should be updated |
91c60b57 |
1446 | myToUpdateEnvironmentMap = Standard_True; |
84c71f29 |
1447 | |
3a9b5dc8 |
1448 | const TCollection_AsciiString aPrefixString = generateShaderPrefix (theGlContext); |
bc8c79bb |
1449 | #ifdef RAY_TRACE_PRINT_INFO |
93cdaa76 |
1450 | Message::SendTrace() << "GLSL prefix string:" << std::endl << aPrefixString; |
bc8c79bb |
1451 | #endif |
bc8c79bb |
1452 | myRaytraceShaderSource.SetPrefix (aPrefixString); |
1453 | myPostFSAAShaderSource.SetPrefix (aPrefixString); |
3a9b5dc8 |
1454 | myOutImageShaderSource.SetPrefix (aPrefixString); |
93cdaa76 |
1455 | if (!myRaytraceShader->LoadAndCompile (theGlContext, myRaytraceProgram->ResourceId(), myRaytraceShaderSource.Source (theGlContext, GL_FRAGMENT_SHADER)) |
1456 | || !myPostFSAAShader->LoadAndCompile (theGlContext, myPostFSAAProgram->ResourceId(), myPostFSAAShaderSource.Source (theGlContext, GL_FRAGMENT_SHADER)) |
1457 | || !myOutImageShader->LoadAndCompile (theGlContext, myOutImageProgram->ResourceId(), myOutImageShaderSource.Source (theGlContext, GL_FRAGMENT_SHADER))) |
fc73a202 |
1458 | { |
91c60b57 |
1459 | return safeFailBack ("Failed to compile ray-tracing fragment shaders", theGlContext); |
fc73a202 |
1460 | } |
1461 | |
91c60b57 |
1462 | myRaytraceProgram->SetAttributeName (theGlContext, Graphic3d_TOA_POS, "occVertex"); |
1463 | myPostFSAAProgram->SetAttributeName (theGlContext, Graphic3d_TOA_POS, "occVertex"); |
3a9b5dc8 |
1464 | myOutImageProgram->SetAttributeName (theGlContext, Graphic3d_TOA_POS, "occVertex"); |
91c60b57 |
1465 | if (!myRaytraceProgram->Link (theGlContext) |
3a9b5dc8 |
1466 | || !myPostFSAAProgram->Link (theGlContext) |
1467 | || !myOutImageProgram->Link (theGlContext)) |
fc73a202 |
1468 | { |
91c60b57 |
1469 | return safeFailBack ("Failed to initialize vertex attributes for ray-tracing program", theGlContext); |
fc73a202 |
1470 | } |
1471 | } |
e276548b |
1472 | } |
1473 | |
91c60b57 |
1474 | if (myRaytraceInitStatus == OpenGl_RT_NONE) |
fc73a202 |
1475 | { |
6e728f3b |
1476 | myAccumFrames = 0; // accumulation should be restarted |
d877e610 |
1477 | |
93cdaa76 |
1478 | #if defined(GL_ES_VERSION_2_0) |
1479 | if (!theGlContext->IsGlGreaterEqual (3, 2)) |
1480 | { |
1481 | return safeFailBack ("Ray-tracing requires OpenGL ES 3.2 and higher", theGlContext); |
1482 | } |
1483 | #else |
91c60b57 |
1484 | if (!theGlContext->IsGlGreaterEqual (3, 1)) |
fc73a202 |
1485 | { |
91c60b57 |
1486 | return safeFailBack ("Ray-tracing requires OpenGL 3.1 and higher", theGlContext); |
25ef750e |
1487 | } |
91c60b57 |
1488 | else if (!theGlContext->arbTboRGB32) |
25ef750e |
1489 | { |
91c60b57 |
1490 | return safeFailBack ("Ray-tracing requires OpenGL 4.0+ or GL_ARB_texture_buffer_object_rgb32 extension", theGlContext); |
1491 | } |
1492 | else if (!theGlContext->arbFBOBlit) |
1493 | { |
1494 | return safeFailBack ("Ray-tracing requires EXT_framebuffer_blit extension", theGlContext); |
fc73a202 |
1495 | } |
93cdaa76 |
1496 | #endif |
5322131b |
1497 | |
c357e426 |
1498 | myRaytraceParameters.NbBounces = myRenderParams.RaytracingDepth; |
bc8c79bb |
1499 | |
ee5befae |
1500 | const TCollection_AsciiString aShaderFolder = Graphic3d_ShaderProgram::ShadersFolder(); |
fc73a202 |
1501 | if (myIsRaytraceDataValid) |
1502 | { |
bc8c79bb |
1503 | myRaytraceParameters.StackSize = Max (THE_DEFAULT_STACK_SIZE, |
f2474958 |
1504 | myRaytraceGeometry.TopLevelTreeDepth() + myRaytraceGeometry.BotLevelTreeDepth()); |
fc73a202 |
1505 | } |
265d4508 |
1506 | |
3a9b5dc8 |
1507 | const TCollection_AsciiString aPrefixString = generateShaderPrefix (theGlContext); |
bc8c79bb |
1508 | |
1509 | #ifdef RAY_TRACE_PRINT_INFO |
93cdaa76 |
1510 | Message::SendTrace() << "GLSL prefix string:" << std::endl << aPrefixString; |
bc8c79bb |
1511 | #endif |
1512 | |
73722cc9 |
1513 | ShaderSource aBasicVertShaderSrc; |
fc73a202 |
1514 | { |
ee5befae |
1515 | if (!aShaderFolder.IsEmpty()) |
fc73a202 |
1516 | { |
ee5befae |
1517 | const TCollection_AsciiString aFiles[] = { aShaderFolder + "/RaytraceBase.vs", "" }; |
1518 | if (!aBasicVertShaderSrc.LoadFromFiles (aFiles)) |
1519 | { |
1520 | return safeFailBack (aBasicVertShaderSrc.ErrorDescription(), theGlContext); |
1521 | } |
1522 | } |
1523 | else |
1524 | { |
1525 | const TCollection_AsciiString aSrcShaders[] = { Shaders_RaytraceBase_vs, "" }; |
1526 | aBasicVertShaderSrc.LoadFromStrings (aSrcShaders); |
fc73a202 |
1527 | } |
73722cc9 |
1528 | } |
265d4508 |
1529 | |
73722cc9 |
1530 | { |
ee5befae |
1531 | if (!aShaderFolder.IsEmpty()) |
1532 | { |
1533 | const TCollection_AsciiString aFiles[] = { aShaderFolder + "/RaytraceBase.fs", |
78607702 |
1534 | aShaderFolder + "/TangentSpaceNormal.glsl", |
ee5befae |
1535 | aShaderFolder + "/PathtraceBase.fs", |
1536 | aShaderFolder + "/RaytraceRender.fs", |
1537 | "" }; |
1538 | if (!myRaytraceShaderSource.LoadFromFiles (aFiles, aPrefixString)) |
1539 | { |
1540 | return safeFailBack (myRaytraceShaderSource.ErrorDescription(), theGlContext); |
1541 | } |
1542 | } |
1543 | else |
73722cc9 |
1544 | { |
ee5befae |
1545 | const TCollection_AsciiString aSrcShaders[] = { Shaders_RaytraceBase_fs, |
78607702 |
1546 | Shaders_TangentSpaceNormal_glsl, |
ee5befae |
1547 | Shaders_PathtraceBase_fs, |
1548 | Shaders_RaytraceRender_fs, |
1549 | "" }; |
1550 | myRaytraceShaderSource.LoadFromStrings (aSrcShaders, aPrefixString); |
73722cc9 |
1551 | } |
e276548b |
1552 | |
73722cc9 |
1553 | Handle(OpenGl_ShaderObject) aBasicVertShader = initShader (GL_VERTEX_SHADER, aBasicVertShaderSrc, theGlContext); |
1554 | if (aBasicVertShader.IsNull()) |
1555 | { |
1556 | return safeFailBack ("Failed to initialize ray-trace vertex shader", theGlContext); |
1557 | } |
265d4508 |
1558 | |
91c60b57 |
1559 | myRaytraceShader = initShader (GL_FRAGMENT_SHADER, myRaytraceShaderSource, theGlContext); |
fc73a202 |
1560 | if (myRaytraceShader.IsNull()) |
1561 | { |
91c60b57 |
1562 | aBasicVertShader->Release (theGlContext.operator->()); |
91c60b57 |
1563 | return safeFailBack ("Failed to initialize ray-trace fragment shader", theGlContext); |
fc73a202 |
1564 | } |
265d4508 |
1565 | |
d95f5ce1 |
1566 | myRaytraceProgram = initProgram (theGlContext, aBasicVertShader, myRaytraceShader, "main"); |
189f85a3 |
1567 | if (myRaytraceProgram.IsNull()) |
fc73a202 |
1568 | { |
189f85a3 |
1569 | return safeFailBack ("Failed to initialize ray-trace shader program", theGlContext); |
fc73a202 |
1570 | } |
1571 | } |
265d4508 |
1572 | |
fc73a202 |
1573 | { |
ee5befae |
1574 | if (!aShaderFolder.IsEmpty()) |
1575 | { |
1576 | const TCollection_AsciiString aFiles[] = { aShaderFolder + "/RaytraceBase.fs", aShaderFolder + "/RaytraceSmooth.fs", "" }; |
1577 | if (!myPostFSAAShaderSource.LoadFromFiles (aFiles, aPrefixString)) |
1578 | { |
1579 | return safeFailBack (myPostFSAAShaderSource.ErrorDescription(), theGlContext); |
1580 | } |
1581 | } |
1582 | else |
73722cc9 |
1583 | { |
ee5befae |
1584 | const TCollection_AsciiString aSrcShaders[] = { Shaders_RaytraceBase_fs, Shaders_RaytraceSmooth_fs, "" }; |
1585 | myPostFSAAShaderSource.LoadFromStrings (aSrcShaders, aPrefixString); |
73722cc9 |
1586 | } |
265d4508 |
1587 | |
73722cc9 |
1588 | Handle(OpenGl_ShaderObject) aBasicVertShader = initShader (GL_VERTEX_SHADER, aBasicVertShaderSrc, theGlContext); |
fc73a202 |
1589 | if (aBasicVertShader.IsNull()) |
1590 | { |
91c60b57 |
1591 | return safeFailBack ("Failed to initialize FSAA vertex shader", theGlContext); |
fc73a202 |
1592 | } |
265d4508 |
1593 | |
91c60b57 |
1594 | myPostFSAAShader = initShader (GL_FRAGMENT_SHADER, myPostFSAAShaderSource, theGlContext); |
fc73a202 |
1595 | if (myPostFSAAShader.IsNull()) |
1596 | { |
91c60b57 |
1597 | aBasicVertShader->Release (theGlContext.operator->()); |
91c60b57 |
1598 | return safeFailBack ("Failed to initialize FSAA fragment shader", theGlContext); |
fc73a202 |
1599 | } |
265d4508 |
1600 | |
d95f5ce1 |
1601 | myPostFSAAProgram = initProgram (theGlContext, aBasicVertShader, myPostFSAAShader, "fsaa"); |
189f85a3 |
1602 | if (myPostFSAAProgram.IsNull()) |
fc73a202 |
1603 | { |
189f85a3 |
1604 | return safeFailBack ("Failed to initialize FSAA shader program", theGlContext); |
fc73a202 |
1605 | } |
189f85a3 |
1606 | } |
fc73a202 |
1607 | |
189f85a3 |
1608 | { |
ee5befae |
1609 | if (!aShaderFolder.IsEmpty()) |
1610 | { |
1611 | const TCollection_AsciiString aFiles[] = { aShaderFolder + "/Display.fs", "" }; |
1612 | if (!myOutImageShaderSource.LoadFromFiles (aFiles, aPrefixString)) |
1613 | { |
1614 | return safeFailBack (myOutImageShaderSource.ErrorDescription(), theGlContext); |
1615 | } |
1616 | } |
1617 | else |
73722cc9 |
1618 | { |
ee5befae |
1619 | const TCollection_AsciiString aSrcShaders[] = { Shaders_Display_fs, "" }; |
1620 | myOutImageShaderSource.LoadFromStrings (aSrcShaders, aPrefixString); |
73722cc9 |
1621 | } |
fc73a202 |
1622 | |
73722cc9 |
1623 | Handle(OpenGl_ShaderObject) aBasicVertShader = initShader (GL_VERTEX_SHADER, aBasicVertShaderSrc, theGlContext); |
189f85a3 |
1624 | if (aBasicVertShader.IsNull()) |
1625 | { |
1626 | return safeFailBack ("Failed to set vertex shader source", theGlContext); |
fc73a202 |
1627 | } |
1628 | |
3a9b5dc8 |
1629 | myOutImageShader = initShader (GL_FRAGMENT_SHADER, myOutImageShaderSource, theGlContext); |
1630 | if (myOutImageShader.IsNull()) |
fc73a202 |
1631 | { |
189f85a3 |
1632 | aBasicVertShader->Release (theGlContext.operator->()); |
189f85a3 |
1633 | return safeFailBack ("Failed to set display fragment shader source", theGlContext); |
07039714 |
1634 | } |
fc73a202 |
1635 | |
d95f5ce1 |
1636 | myOutImageProgram = initProgram (theGlContext, aBasicVertShader, myOutImageShader, "out"); |
189f85a3 |
1637 | if (myOutImageProgram.IsNull()) |
1638 | { |
3a9b5dc8 |
1639 | return safeFailBack ("Failed to initialize display shader program", theGlContext); |
fc73a202 |
1640 | } |
e276548b |
1641 | } |
fc73a202 |
1642 | } |
e276548b |
1643 | |
91c60b57 |
1644 | if (myRaytraceInitStatus == OpenGl_RT_NONE || aToRebuildShaders) |
fc73a202 |
1645 | { |
1646 | for (Standard_Integer anIndex = 0; anIndex < 2; ++anIndex) |
e276548b |
1647 | { |
fc73a202 |
1648 | Handle(OpenGl_ShaderProgram)& aShaderProgram = |
1649 | (anIndex == 0) ? myRaytraceProgram : myPostFSAAProgram; |
1650 | |
91c60b57 |
1651 | theGlContext->BindProgram (aShaderProgram); |
fc73a202 |
1652 | |
91c60b57 |
1653 | aShaderProgram->SetSampler (theGlContext, |
fc73a202 |
1654 | "uSceneMinPointTexture", OpenGl_RT_SceneMinPointTexture); |
91c60b57 |
1655 | aShaderProgram->SetSampler (theGlContext, |
fc73a202 |
1656 | "uSceneMaxPointTexture", OpenGl_RT_SceneMaxPointTexture); |
91c60b57 |
1657 | aShaderProgram->SetSampler (theGlContext, |
fc73a202 |
1658 | "uSceneNodeInfoTexture", OpenGl_RT_SceneNodeInfoTexture); |
91c60b57 |
1659 | aShaderProgram->SetSampler (theGlContext, |
fc73a202 |
1660 | "uGeometryVertexTexture", OpenGl_RT_GeometryVertexTexture); |
91c60b57 |
1661 | aShaderProgram->SetSampler (theGlContext, |
fc73a202 |
1662 | "uGeometryNormalTexture", OpenGl_RT_GeometryNormalTexture); |
91c60b57 |
1663 | aShaderProgram->SetSampler (theGlContext, |
25ef750e |
1664 | "uGeometryTexCrdTexture", OpenGl_RT_GeometryTexCrdTexture); |
91c60b57 |
1665 | aShaderProgram->SetSampler (theGlContext, |
25ef750e |
1666 | "uGeometryTriangTexture", OpenGl_RT_GeometryTriangTexture); |
91c60b57 |
1667 | aShaderProgram->SetSampler (theGlContext, |
84c71f29 |
1668 | "uSceneTransformTexture", OpenGl_RT_SceneTransformTexture); |
91c60b57 |
1669 | aShaderProgram->SetSampler (theGlContext, |
89d855ba |
1670 | "uEnvMapTexture", OpenGl_RT_EnvMapTexture); |
91c60b57 |
1671 | aShaderProgram->SetSampler (theGlContext, |
25ef750e |
1672 | "uRaytraceMaterialTexture", OpenGl_RT_RaytraceMaterialTexture); |
91c60b57 |
1673 | aShaderProgram->SetSampler (theGlContext, |
25ef750e |
1674 | "uRaytraceLightSrcTexture", OpenGl_RT_RaytraceLightSrcTexture); |
fc73a202 |
1675 | |
1676 | if (anIndex == 1) |
1677 | { |
91c60b57 |
1678 | aShaderProgram->SetSampler (theGlContext, |
189f85a3 |
1679 | "uFSAAInputTexture", OpenGl_RT_FsaaInputTexture); |
1680 | } |
1681 | else |
1682 | { |
1683 | aShaderProgram->SetSampler (theGlContext, |
1684 | "uAccumTexture", OpenGl_RT_PrevAccumTexture); |
fc73a202 |
1685 | } |
265d4508 |
1686 | |
fc73a202 |
1687 | myUniformLocations[anIndex][OpenGl_RT_aPosition] = |
91c60b57 |
1688 | aShaderProgram->GetAttributeLocation (theGlContext, "occVertex"); |
fc73a202 |
1689 | |
1690 | myUniformLocations[anIndex][OpenGl_RT_uOriginLB] = |
91c60b57 |
1691 | aShaderProgram->GetUniformLocation (theGlContext, "uOriginLB"); |
fc73a202 |
1692 | myUniformLocations[anIndex][OpenGl_RT_uOriginRB] = |
91c60b57 |
1693 | aShaderProgram->GetUniformLocation (theGlContext, "uOriginRB"); |
fc73a202 |
1694 | myUniformLocations[anIndex][OpenGl_RT_uOriginLT] = |
91c60b57 |
1695 | aShaderProgram->GetUniformLocation (theGlContext, "uOriginLT"); |
fc73a202 |
1696 | myUniformLocations[anIndex][OpenGl_RT_uOriginRT] = |
91c60b57 |
1697 | aShaderProgram->GetUniformLocation (theGlContext, "uOriginRT"); |
fc73a202 |
1698 | myUniformLocations[anIndex][OpenGl_RT_uDirectLB] = |
91c60b57 |
1699 | aShaderProgram->GetUniformLocation (theGlContext, "uDirectLB"); |
fc73a202 |
1700 | myUniformLocations[anIndex][OpenGl_RT_uDirectRB] = |
91c60b57 |
1701 | aShaderProgram->GetUniformLocation (theGlContext, "uDirectRB"); |
fc73a202 |
1702 | myUniformLocations[anIndex][OpenGl_RT_uDirectLT] = |
91c60b57 |
1703 | aShaderProgram->GetUniformLocation (theGlContext, "uDirectLT"); |
fc73a202 |
1704 | myUniformLocations[anIndex][OpenGl_RT_uDirectRT] = |
91c60b57 |
1705 | aShaderProgram->GetUniformLocation (theGlContext, "uDirectRT"); |
3a9b5dc8 |
1706 | myUniformLocations[anIndex][OpenGl_RT_uViewPrMat] = |
1d865689 |
1707 | aShaderProgram->GetUniformLocation (theGlContext, "uViewMat"); |
25ef750e |
1708 | myUniformLocations[anIndex][OpenGl_RT_uUnviewMat] = |
91c60b57 |
1709 | aShaderProgram->GetUniformLocation (theGlContext, "uUnviewMat"); |
fc73a202 |
1710 | |
1711 | myUniformLocations[anIndex][OpenGl_RT_uSceneRad] = |
91c60b57 |
1712 | aShaderProgram->GetUniformLocation (theGlContext, "uSceneRadius"); |
fc73a202 |
1713 | myUniformLocations[anIndex][OpenGl_RT_uSceneEps] = |
91c60b57 |
1714 | aShaderProgram->GetUniformLocation (theGlContext, "uSceneEpsilon"); |
25ef750e |
1715 | myUniformLocations[anIndex][OpenGl_RT_uLightCount] = |
91c60b57 |
1716 | aShaderProgram->GetUniformLocation (theGlContext, "uLightCount"); |
25ef750e |
1717 | myUniformLocations[anIndex][OpenGl_RT_uLightAmbnt] = |
91c60b57 |
1718 | aShaderProgram->GetUniformLocation (theGlContext, "uGlobalAmbient"); |
fc73a202 |
1719 | |
93cdaa76 |
1720 | myUniformLocations[anIndex][OpenGl_RT_uFsaaOffset] = |
1721 | aShaderProgram->GetUniformLocation (theGlContext, "uFsaaOffset"); |
fc73a202 |
1722 | myUniformLocations[anIndex][OpenGl_RT_uSamples] = |
91c60b57 |
1723 | aShaderProgram->GetUniformLocation (theGlContext, "uSamples"); |
84c71f29 |
1724 | |
189f85a3 |
1725 | myUniformLocations[anIndex][OpenGl_RT_uTexSamplersArray] = |
91c60b57 |
1726 | aShaderProgram->GetUniformLocation (theGlContext, "uTextureSamplers"); |
25ef750e |
1727 | |
189f85a3 |
1728 | myUniformLocations[anIndex][OpenGl_RT_uShadowsEnabled] = |
1729 | aShaderProgram->GetUniformLocation (theGlContext, "uShadowsEnabled"); |
1730 | myUniformLocations[anIndex][OpenGl_RT_uReflectEnabled] = |
1731 | aShaderProgram->GetUniformLocation (theGlContext, "uReflectEnabled"); |
89d855ba |
1732 | myUniformLocations[anIndex][OpenGl_RT_uEnvMapEnabled] = |
1733 | aShaderProgram->GetUniformLocation (theGlContext, "uEnvMapEnabled"); |
1734 | myUniformLocations[anIndex][OpenGl_RT_uEnvMapForBack] = |
1735 | aShaderProgram->GetUniformLocation (theGlContext, "uEnvMapForBack"); |
8c820969 |
1736 | myUniformLocations[anIndex][OpenGl_RT_uBlockedRngEnabled] = |
1737 | aShaderProgram->GetUniformLocation (theGlContext, "uBlockedRngEnabled"); |
189f85a3 |
1738 | |
3a9b5dc8 |
1739 | myUniformLocations[anIndex][OpenGl_RT_uWinSizeX] = |
1740 | aShaderProgram->GetUniformLocation (theGlContext, "uWinSizeX"); |
1741 | myUniformLocations[anIndex][OpenGl_RT_uWinSizeY] = |
1742 | aShaderProgram->GetUniformLocation (theGlContext, "uWinSizeY"); |
1743 | |
383c6c9f |
1744 | myUniformLocations[anIndex][OpenGl_RT_uAccumSamples] = |
1745 | aShaderProgram->GetUniformLocation (theGlContext, "uAccumSamples"); |
189f85a3 |
1746 | myUniformLocations[anIndex][OpenGl_RT_uFrameRndSeed] = |
1747 | aShaderProgram->GetUniformLocation (theGlContext, "uFrameRndSeed"); |
1748 | |
3a9b5dc8 |
1749 | myUniformLocations[anIndex][OpenGl_RT_uRenderImage] = |
1750 | aShaderProgram->GetUniformLocation (theGlContext, "uRenderImage"); |
e084dbbc |
1751 | myUniformLocations[anIndex][OpenGl_RT_uTilesImage] = |
1752 | aShaderProgram->GetUniformLocation (theGlContext, "uTilesImage"); |
3a9b5dc8 |
1753 | myUniformLocations[anIndex][OpenGl_RT_uOffsetImage] = |
1754 | aShaderProgram->GetUniformLocation (theGlContext, "uOffsetImage"); |
66d1cdc6 |
1755 | myUniformLocations[anIndex][OpenGl_RT_uTileSize] = |
1756 | aShaderProgram->GetUniformLocation (theGlContext, "uTileSize"); |
1757 | myUniformLocations[anIndex][OpenGl_RT_uVarianceScaleFactor] = |
1758 | aShaderProgram->GetUniformLocation (theGlContext, "uVarianceScaleFactor"); |
3a9b5dc8 |
1759 | |
189f85a3 |
1760 | myUniformLocations[anIndex][OpenGl_RT_uBackColorTop] = |
1761 | aShaderProgram->GetUniformLocation (theGlContext, "uBackColorTop"); |
1762 | myUniformLocations[anIndex][OpenGl_RT_uBackColorBot] = |
1763 | aShaderProgram->GetUniformLocation (theGlContext, "uBackColorBot"); |
b09447ed |
1764 | |
1765 | myUniformLocations[anIndex][OpenGl_RT_uMaxRadiance] = |
1766 | aShaderProgram->GetUniformLocation (theGlContext, "uMaxRadiance"); |
fc73a202 |
1767 | } |
265d4508 |
1768 | |
189f85a3 |
1769 | theGlContext->BindProgram (myOutImageProgram); |
1770 | |
1771 | myOutImageProgram->SetSampler (theGlContext, |
1772 | "uInputTexture", OpenGl_RT_PrevAccumTexture); |
1773 | |
1d865689 |
1774 | myOutImageProgram->SetSampler (theGlContext, |
3a9b5dc8 |
1775 | "uDepthTexture", OpenGl_RT_RaytraceDepthTexture); |
1d865689 |
1776 | |
91c60b57 |
1777 | theGlContext->BindProgram (NULL); |
fc73a202 |
1778 | } |
265d4508 |
1779 | |
91c60b57 |
1780 | if (myRaytraceInitStatus != OpenGl_RT_NONE) |
fc73a202 |
1781 | { |
91c60b57 |
1782 | return myRaytraceInitStatus == OpenGl_RT_INIT; |
fc73a202 |
1783 | } |
265d4508 |
1784 | |
fc73a202 |
1785 | const GLfloat aVertices[] = { -1.f, -1.f, 0.f, |
1786 | -1.f, 1.f, 0.f, |
1787 | 1.f, 1.f, 0.f, |
1788 | 1.f, 1.f, 0.f, |
1789 | 1.f, -1.f, 0.f, |
1790 | -1.f, -1.f, 0.f }; |
265d4508 |
1791 | |
91c60b57 |
1792 | myRaytraceScreenQuad.Init (theGlContext, 3, 6, aVertices); |
265d4508 |
1793 | |
91c60b57 |
1794 | myRaytraceInitStatus = OpenGl_RT_INIT; // initialized in normal way |
ca3c13d1 |
1795 | |
fc73a202 |
1796 | return Standard_True; |
1797 | } |
265d4508 |
1798 | |
fc73a202 |
1799 | // ======================================================================= |
189f85a3 |
1800 | // function : nullifyResource |
3a9b5dc8 |
1801 | // purpose : Releases OpenGL resource |
fc73a202 |
1802 | // ======================================================================= |
aa00364d |
1803 | template <class T> |
3a9b5dc8 |
1804 | inline void nullifyResource (const Handle(OpenGl_Context)& theGlContext, Handle(T)& theResource) |
fc73a202 |
1805 | { |
1806 | if (!theResource.IsNull()) |
1807 | { |
ad67e367 |
1808 | theResource->Release (theGlContext.get()); |
fc73a202 |
1809 | theResource.Nullify(); |
1810 | } |
1811 | } |
265d4508 |
1812 | |
fc73a202 |
1813 | // ======================================================================= |
189f85a3 |
1814 | // function : releaseRaytraceResources |
fc73a202 |
1815 | // purpose : Releases OpenGL/GLSL shader programs |
1816 | // ======================================================================= |
d877e610 |
1817 | void OpenGl_View::releaseRaytraceResources (const Handle(OpenGl_Context)& theGlContext, const Standard_Boolean theToRebuild) |
fc73a202 |
1818 | { |
d877e610 |
1819 | // release shader resources |
189f85a3 |
1820 | nullifyResource (theGlContext, myRaytraceShader); |
1821 | nullifyResource (theGlContext, myPostFSAAShader); |
265d4508 |
1822 | |
189f85a3 |
1823 | nullifyResource (theGlContext, myRaytraceProgram); |
1824 | nullifyResource (theGlContext, myPostFSAAProgram); |
1825 | nullifyResource (theGlContext, myOutImageProgram); |
265d4508 |
1826 | |
d877e610 |
1827 | if (!theToRebuild) // complete release |
1828 | { |
ad67e367 |
1829 | myRaytraceFBO1[0]->Release (theGlContext.get()); |
1830 | myRaytraceFBO1[1]->Release (theGlContext.get()); |
1831 | myRaytraceFBO2[0]->Release (theGlContext.get()); |
1832 | myRaytraceFBO2[1]->Release (theGlContext.get()); |
d877e610 |
1833 | |
1834 | nullifyResource (theGlContext, myRaytraceOutputTexture[0]); |
1835 | nullifyResource (theGlContext, myRaytraceOutputTexture[1]); |
265d4508 |
1836 | |
b27ab03d |
1837 | nullifyResource (theGlContext, myRaytraceTileOffsetsTexture[0]); |
1838 | nullifyResource (theGlContext, myRaytraceTileOffsetsTexture[1]); |
1839 | nullifyResource (theGlContext, myRaytraceVisualErrorTexture[0]); |
1840 | nullifyResource (theGlContext, myRaytraceVisualErrorTexture[1]); |
e084dbbc |
1841 | nullifyResource (theGlContext, myRaytraceTileSamplesTexture[0]); |
1842 | nullifyResource (theGlContext, myRaytraceTileSamplesTexture[1]); |
265d4508 |
1843 | |
d877e610 |
1844 | nullifyResource (theGlContext, mySceneNodeInfoTexture); |
1845 | nullifyResource (theGlContext, mySceneMinPointTexture); |
1846 | nullifyResource (theGlContext, mySceneMaxPointTexture); |
265d4508 |
1847 | |
d877e610 |
1848 | nullifyResource (theGlContext, myGeometryVertexTexture); |
1849 | nullifyResource (theGlContext, myGeometryNormalTexture); |
1850 | nullifyResource (theGlContext, myGeometryTexCrdTexture); |
1851 | nullifyResource (theGlContext, myGeometryTriangTexture); |
1852 | nullifyResource (theGlContext, mySceneTransformTexture); |
47e9c178 |
1853 | |
d877e610 |
1854 | nullifyResource (theGlContext, myRaytraceLightSrcTexture); |
1855 | nullifyResource (theGlContext, myRaytraceMaterialTexture); |
1856 | |
1857 | myRaytraceGeometry.ReleaseResources (theGlContext); |
1858 | |
1859 | if (myRaytraceScreenQuad.IsValid ()) |
1860 | { |
ad67e367 |
1861 | myRaytraceScreenQuad.Release (theGlContext.get()); |
d877e610 |
1862 | } |
3a9b5dc8 |
1863 | } |
91c60b57 |
1864 | } |
1865 | |
1866 | // ======================================================================= |
bf02aa7d |
1867 | // function : updateRaytraceBuffers |
1868 | // purpose : Updates auxiliary OpenGL frame buffers. |
91c60b57 |
1869 | // ======================================================================= |
bf02aa7d |
1870 | Standard_Boolean OpenGl_View::updateRaytraceBuffers (const Standard_Integer theSizeX, |
91c60b57 |
1871 | const Standard_Integer theSizeY, |
1872 | const Handle(OpenGl_Context)& theGlContext) |
1873 | { |
6dd6e5c7 |
1874 | // Auxiliary buffers are not used |
bf02aa7d |
1875 | if (!myRaytraceParameters.GlobalIllumination && !myRenderParams.IsAntialiasingEnabled) |
91c60b57 |
1876 | { |
bf02aa7d |
1877 | myRaytraceFBO1[0]->Release (theGlContext.operator->()); |
1878 | myRaytraceFBO2[0]->Release (theGlContext.operator->()); |
1879 | myRaytraceFBO1[1]->Release (theGlContext.operator->()); |
1880 | myRaytraceFBO2[1]->Release (theGlContext.operator->()); |
3a9b5dc8 |
1881 | |
bf02aa7d |
1882 | return Standard_True; |
1883 | } |
1884 | |
4eaaf9d8 |
1885 | if (myRaytraceParameters.AdaptiveScreenSampling) |
6dd6e5c7 |
1886 | { |
66d1cdc6 |
1887 | Graphic3d_Vec2i aMaxViewport = myTileSampler.OffsetTilesViewportMax().cwiseMax (Graphic3d_Vec2i (theSizeX, theSizeY)); |
1888 | myRaytraceFBO1[0]->InitLazy (theGlContext, aMaxViewport.x(), aMaxViewport.y(), GL_RGBA32F, myFboDepthFormat); |
1889 | myRaytraceFBO2[0]->InitLazy (theGlContext, aMaxViewport.x(), aMaxViewport.y(), GL_RGBA32F, myFboDepthFormat); |
4eaaf9d8 |
1890 | if (myRaytraceFBO1[1]->IsValid()) // second FBO not needed |
1891 | { |
1892 | myRaytraceFBO1[1]->Release (theGlContext.operator->()); |
1893 | myRaytraceFBO2[1]->Release (theGlContext.operator->()); |
1894 | } |
bf02aa7d |
1895 | } |
66d1cdc6 |
1896 | |
1897 | for (int aViewIter = 0; aViewIter < 2; ++aViewIter) |
bf02aa7d |
1898 | { |
66d1cdc6 |
1899 | if (myRaytraceTileOffsetsTexture[aViewIter].IsNull()) |
4eaaf9d8 |
1900 | { |
66d1cdc6 |
1901 | myRaytraceOutputTexture[aViewIter] = new OpenGl_Texture(); |
1902 | myRaytraceVisualErrorTexture[aViewIter] = new OpenGl_Texture(); |
e084dbbc |
1903 | myRaytraceTileSamplesTexture[aViewIter] = new OpenGl_Texture(); |
66d1cdc6 |
1904 | myRaytraceTileOffsetsTexture[aViewIter] = new OpenGl_Texture(); |
4eaaf9d8 |
1905 | } |
1906 | |
66d1cdc6 |
1907 | if (aViewIter == 1 |
1908 | && myCamera->ProjectionType() != Graphic3d_Camera::Projection_Stereo) |
4eaaf9d8 |
1909 | { |
1910 | myRaytraceFBO1[1]->Release (theGlContext.operator->()); |
1911 | myRaytraceFBO2[1]->Release (theGlContext.operator->()); |
66d1cdc6 |
1912 | myRaytraceOutputTexture[1]->Release (theGlContext.operator->()); |
1913 | myRaytraceVisualErrorTexture[1]->Release (theGlContext.operator->()); |
1914 | myRaytraceTileOffsetsTexture[1]->Release (theGlContext.operator->()); |
1915 | continue; |
4eaaf9d8 |
1916 | } |
3a9b5dc8 |
1917 | |
66d1cdc6 |
1918 | if (myRaytraceParameters.AdaptiveScreenSampling) |
3a9b5dc8 |
1919 | { |
66d1cdc6 |
1920 | if (myRaytraceOutputTexture[aViewIter]->SizeX() / 3 == theSizeX |
1921 | && myRaytraceOutputTexture[aViewIter]->SizeY() / 2 == theSizeY |
1922 | && myRaytraceVisualErrorTexture[aViewIter]->SizeX() == myTileSampler.NbTilesX() |
1923 | && myRaytraceVisualErrorTexture[aViewIter]->SizeY() == myTileSampler.NbTilesY()) |
1924 | { |
e084dbbc |
1925 | if (myRaytraceParameters.AdaptiveScreenSamplingAtomic) |
1926 | { |
1927 | continue; // offsets texture is dynamically resized |
1928 | } |
1929 | else if (myRaytraceTileSamplesTexture[aViewIter]->SizeX() == myTileSampler.NbTilesX() |
1930 | && myRaytraceTileSamplesTexture[aViewIter]->SizeY() == myTileSampler.NbTilesY()) |
1931 | { |
1932 | continue; |
1933 | } |
66d1cdc6 |
1934 | } |
b27ab03d |
1935 | |
66d1cdc6 |
1936 | myAccumFrames = 0; |
b27ab03d |
1937 | |
66d1cdc6 |
1938 | // Due to limitations of OpenGL image load-store extension |
1939 | // atomic operations are supported only for single-channel |
1940 | // images, so we define GL_R32F image. It is used as array |
1941 | // of 6D floating point vectors: |
1942 | // 0 - R color channel |
1943 | // 1 - G color channel |
1944 | // 2 - B color channel |
1945 | // 3 - hit time transformed into OpenGL NDC space |
1946 | // 4 - luminance accumulated for odd samples only |
1947 | myRaytraceOutputTexture[aViewIter]->InitRectangle (theGlContext, theSizeX * 3, theSizeY * 2, OpenGl_TextureFormat::Create<GLfloat, 1>()); |
1948 | |
1949 | // workaround for some NVIDIA drivers |
1950 | myRaytraceVisualErrorTexture[aViewIter]->Release (theGlContext.operator->()); |
e084dbbc |
1951 | myRaytraceTileSamplesTexture[aViewIter]->Release (theGlContext.operator->()); |
ba00aab7 |
1952 | myRaytraceVisualErrorTexture[aViewIter]->Init (theGlContext, |
1953 | OpenGl_TextureFormat::FindSizedFormat (theGlContext, GL_R32I), |
1954 | Graphic3d_Vec2i (myTileSampler.NbTilesX(), myTileSampler.NbTilesY()), |
1955 | Graphic3d_TOT_2D); |
e084dbbc |
1956 | if (!myRaytraceParameters.AdaptiveScreenSamplingAtomic) |
1957 | { |
ba00aab7 |
1958 | myRaytraceTileSamplesTexture[aViewIter]->Init (theGlContext, |
1959 | OpenGl_TextureFormat::FindSizedFormat (theGlContext, GL_R32I), |
1960 | Graphic3d_Vec2i (myTileSampler.NbTilesX(), myTileSampler.NbTilesY()), |
1961 | Graphic3d_TOT_2D); |
e084dbbc |
1962 | } |
66d1cdc6 |
1963 | } |
1964 | else // non-adaptive mode |
1965 | { |
1966 | if (myRaytraceFBO1[aViewIter]->GetSizeX() != theSizeX |
1967 | || myRaytraceFBO1[aViewIter]->GetSizeY() != theSizeY) |
1968 | { |
1969 | myAccumFrames = 0; // accumulation should be restarted |
1970 | } |
b27ab03d |
1971 | |
66d1cdc6 |
1972 | myRaytraceFBO1[aViewIter]->InitLazy (theGlContext, theSizeX, theSizeY, GL_RGBA32F, myFboDepthFormat); |
1973 | myRaytraceFBO2[aViewIter]->InitLazy (theGlContext, theSizeX, theSizeY, GL_RGBA32F, myFboDepthFormat); |
3a9b5dc8 |
1974 | } |
1975 | } |
91c60b57 |
1976 | return Standard_True; |
1977 | } |
1978 | |
1979 | // ======================================================================= |
189f85a3 |
1980 | // function : updateCamera |
91c60b57 |
1981 | // purpose : Generates viewing rays for corners of screen quad |
1982 | // ======================================================================= |
1983 | void OpenGl_View::updateCamera (const OpenGl_Mat4& theOrientation, |
1984 | const OpenGl_Mat4& theViewMapping, |
1985 | OpenGl_Vec3* theOrigins, |
1986 | OpenGl_Vec3* theDirects, |
3a9b5dc8 |
1987 | OpenGl_Mat4& theViewPr, |
91c60b57 |
1988 | OpenGl_Mat4& theUnview) |
1989 | { |
1d865689 |
1990 | // compute view-projection matrix |
3a9b5dc8 |
1991 | theViewPr = theViewMapping * theOrientation; |
1d865689 |
1992 | |
1993 | // compute inverse view-projection matrix |
3a9b5dc8 |
1994 | theViewPr.Inverted (theUnview); |
91c60b57 |
1995 | |
1996 | Standard_Integer aOriginIndex = 0; |
1997 | Standard_Integer aDirectIndex = 0; |
1998 | |
1999 | for (Standard_Integer aY = -1; aY <= 1; aY += 2) |
2000 | { |
2001 | for (Standard_Integer aX = -1; aX <= 1; aX += 2) |
2002 | { |
2003 | OpenGl_Vec4 aOrigin (GLfloat(aX), |
2004 | GLfloat(aY), |
b27ab03d |
2005 | -1.0f, |
91c60b57 |
2006 | 1.0f); |
2007 | |
2008 | aOrigin = theUnview * aOrigin; |
2009 | |
2010 | aOrigin.x() = aOrigin.x() / aOrigin.w(); |
2011 | aOrigin.y() = aOrigin.y() / aOrigin.w(); |
2012 | aOrigin.z() = aOrigin.z() / aOrigin.w(); |
2013 | |
2014 | OpenGl_Vec4 aDirect (GLfloat(aX), |
2015 | GLfloat(aY), |
2016 | 1.0f, |
2017 | 1.0f); |
2018 | |
2019 | aDirect = theUnview * aDirect; |
2020 | |
2021 | aDirect.x() = aDirect.x() / aDirect.w(); |
2022 | aDirect.y() = aDirect.y() / aDirect.w(); |
2023 | aDirect.z() = aDirect.z() / aDirect.w(); |
2024 | |
2025 | aDirect = aDirect - aOrigin; |
2026 | |
91c60b57 |
2027 | theOrigins[aOriginIndex++] = OpenGl_Vec3 (static_cast<GLfloat> (aOrigin.x()), |
2028 | static_cast<GLfloat> (aOrigin.y()), |
2029 | static_cast<GLfloat> (aOrigin.z())); |
2030 | |
b0dc79bc |
2031 | theDirects[aDirectIndex++] = OpenGl_Vec3 (static_cast<GLfloat> (aDirect.x()), |
2032 | static_cast<GLfloat> (aDirect.y()), |
2033 | static_cast<GLfloat> (aDirect.z())); |
91c60b57 |
2034 | } |
2035 | } |
fc73a202 |
2036 | } |
265d4508 |
2037 | |
b27ab03d |
2038 | // ======================================================================= |
2039 | // function : updatePerspCameraPT |
2040 | // purpose : Generates viewing rays (path tracing, perspective camera) |
2041 | // ======================================================================= |
2042 | void OpenGl_View::updatePerspCameraPT (const OpenGl_Mat4& theOrientation, |
2043 | const OpenGl_Mat4& theViewMapping, |
2044 | Graphic3d_Camera::Projection theProjection, |
2045 | OpenGl_Mat4& theViewPr, |
2046 | OpenGl_Mat4& theUnview, |
2047 | const int theWinSizeX, |
2048 | const int theWinSizeY) |
2049 | { |
2050 | // compute view-projection matrix |
2051 | theViewPr = theViewMapping * theOrientation; |
2052 | |
2053 | // compute inverse view-projection matrix |
2054 | theViewPr.Inverted(theUnview); |
2055 | |
2056 | // get camera stereo params |
2057 | float anIOD = myCamera->GetIODType() == Graphic3d_Camera::IODType_Relative |
2058 | ? static_cast<float> (myCamera->IOD() * myCamera->Distance()) |
2059 | : static_cast<float> (myCamera->IOD()); |
2060 | |
2061 | float aZFocus = myCamera->ZFocusType() == Graphic3d_Camera::FocusType_Relative |
2062 | ? static_cast<float> (myCamera->ZFocus() * myCamera->Distance()) |
2063 | : static_cast<float> (myCamera->ZFocus()); |
2064 | |
2065 | // get camera view vectors |
2066 | const gp_Pnt anOrig = myCamera->Eye(); |
2067 | |
2068 | myEyeOrig = OpenGl_Vec3 (static_cast<float> (anOrig.X()), |
2069 | static_cast<float> (anOrig.Y()), |
2070 | static_cast<float> (anOrig.Z())); |
2071 | |
2072 | const gp_Dir aView = myCamera->Direction(); |
2073 | |
2074 | OpenGl_Vec3 anEyeViewMono = OpenGl_Vec3 (static_cast<float> (aView.X()), |
2075 | static_cast<float> (aView.Y()), |
2076 | static_cast<float> (aView.Z())); |
2077 | |
2078 | const gp_Dir anUp = myCamera->Up(); |
2079 | |
2080 | myEyeVert = OpenGl_Vec3 (static_cast<float> (anUp.X()), |
2081 | static_cast<float> (anUp.Y()), |
2082 | static_cast<float> (anUp.Z())); |
2083 | |
2084 | myEyeSide = OpenGl_Vec3::Cross (anEyeViewMono, myEyeVert); |
2085 | |
2086 | const double aScaleY = tan (myCamera->FOVy() / 360 * M_PI); |
2087 | const double aScaleX = theWinSizeX * aScaleY / theWinSizeY; |
2088 | |
2089 | myEyeSize = OpenGl_Vec2 (static_cast<float> (aScaleX), |
2090 | static_cast<float> (aScaleY)); |
2091 | |
2092 | if (theProjection == Graphic3d_Camera::Projection_Perspective) |
2093 | { |
2094 | myEyeView = anEyeViewMono; |
2095 | } |
2096 | else // stereo camera |
2097 | { |
2098 | // compute z-focus point |
2099 | OpenGl_Vec3 aZFocusPoint = myEyeOrig + anEyeViewMono * aZFocus; |
2100 | |
2101 | // compute stereo camera shift |
2102 | float aDx = theProjection == Graphic3d_Camera::Projection_MonoRightEye ? 0.5f * anIOD : -0.5f * anIOD; |
2103 | myEyeOrig += myEyeSide.Normalized() * aDx; |
2104 | |
2105 | // estimate new camera direction vector and correct its length |
2106 | myEyeView = (aZFocusPoint - myEyeOrig).Normalized(); |
2107 | myEyeView *= 1.f / anEyeViewMono.Dot (myEyeView); |
2108 | } |
2109 | } |
2110 | |
fc73a202 |
2111 | // ======================================================================= |
189f85a3 |
2112 | // function : uploadRaytraceData |
fc73a202 |
2113 | // purpose : Uploads ray-trace data to the GPU |
2114 | // ======================================================================= |
91c60b57 |
2115 | Standard_Boolean OpenGl_View::uploadRaytraceData (const Handle(OpenGl_Context)& theGlContext) |
fc73a202 |
2116 | { |
93cdaa76 |
2117 | #if defined(GL_ES_VERSION_2_0) |
2118 | if (!theGlContext->IsGlGreaterEqual (3, 2)) |
2119 | { |
2120 | Message::SendFail() << "Error: OpenGL ES version is less than 3.2"; |
2121 | return Standard_False; |
2122 | } |
2123 | #else |
91c60b57 |
2124 | if (!theGlContext->IsGlGreaterEqual (3, 1)) |
fc73a202 |
2125 | { |
93cdaa76 |
2126 | Message::SendFail() << "Error: OpenGL version is less than 3.1"; |
fc73a202 |
2127 | return Standard_False; |
e276548b |
2128 | } |
93cdaa76 |
2129 | #endif |
e276548b |
2130 | |
189f85a3 |
2131 | myAccumFrames = 0; // accumulation should be restarted |
2132 | |
25ef750e |
2133 | ///////////////////////////////////////////////////////////////////////////// |
2134 | // Prepare OpenGL textures |
2135 | |
91c60b57 |
2136 | if (theGlContext->arbTexBindless != NULL) |
25ef750e |
2137 | { |
2138 | // If OpenGL driver supports bindless textures we need |
2139 | // to get unique 64- bit handles for using on the GPU |
91c60b57 |
2140 | if (!myRaytraceGeometry.UpdateTextureHandles (theGlContext)) |
25ef750e |
2141 | { |
93cdaa76 |
2142 | Message::SendTrace() << "Error: Failed to get OpenGL texture handles"; |
25ef750e |
2143 | return Standard_False; |
2144 | } |
2145 | } |
2146 | |
265d4508 |
2147 | ///////////////////////////////////////////////////////////////////////////// |
e2da917a |
2148 | // Create OpenGL BVH buffers |
265d4508 |
2149 | |
3a9b5dc8 |
2150 | if (mySceneNodeInfoTexture.IsNull()) // create scene BVH buffers |
e276548b |
2151 | { |
e2da917a |
2152 | mySceneNodeInfoTexture = new OpenGl_TextureBufferArb; |
2153 | mySceneMinPointTexture = new OpenGl_TextureBufferArb; |
2154 | mySceneMaxPointTexture = new OpenGl_TextureBufferArb; |
84c71f29 |
2155 | mySceneTransformTexture = new OpenGl_TextureBufferArb; |
265d4508 |
2156 | |
91c60b57 |
2157 | if (!mySceneNodeInfoTexture->Create (theGlContext) |
2158 | || !mySceneMinPointTexture->Create (theGlContext) |
2159 | || !mySceneMaxPointTexture->Create (theGlContext) |
2160 | || !mySceneTransformTexture->Create (theGlContext)) |
e276548b |
2161 | { |
93cdaa76 |
2162 | Message::SendTrace() << "Error: Failed to create scene BVH buffers"; |
fc73a202 |
2163 | return Standard_False; |
2164 | } |
2165 | } |
5322131b |
2166 | |
3a9b5dc8 |
2167 | if (myGeometryVertexTexture.IsNull()) // create geometry buffers |
265d4508 |
2168 | { |
fc73a202 |
2169 | myGeometryVertexTexture = new OpenGl_TextureBufferArb; |
2170 | myGeometryNormalTexture = new OpenGl_TextureBufferArb; |
25ef750e |
2171 | myGeometryTexCrdTexture = new OpenGl_TextureBufferArb; |
fc73a202 |
2172 | myGeometryTriangTexture = new OpenGl_TextureBufferArb; |
e276548b |
2173 | |
91c60b57 |
2174 | if (!myGeometryVertexTexture->Create (theGlContext) |
2175 | || !myGeometryNormalTexture->Create (theGlContext) |
2176 | || !myGeometryTexCrdTexture->Create (theGlContext) |
2177 | || !myGeometryTriangTexture->Create (theGlContext)) |
fc73a202 |
2178 | { |
93cdaa76 |
2179 | Message::SendTrace() << "\nError: Failed to create buffers for triangulation data"; |
fc73a202 |
2180 | return Standard_False; |
2181 | } |
265d4508 |
2182 | } |
5322131b |
2183 | |
e2da917a |
2184 | if (myRaytraceMaterialTexture.IsNull()) // create material buffer |
fc73a202 |
2185 | { |
93cdaa76 |
2186 | myRaytraceMaterialTexture = new OpenGl_TextureBufferArb(); |
91c60b57 |
2187 | if (!myRaytraceMaterialTexture->Create (theGlContext)) |
fc73a202 |
2188 | { |
93cdaa76 |
2189 | Message::SendTrace() << "Error: Failed to create buffers for material data"; |
fc73a202 |
2190 | return Standard_False; |
2191 | } |
2192 | } |
e2da917a |
2193 | |
84c71f29 |
2194 | ///////////////////////////////////////////////////////////////////////////// |
2195 | // Write transform buffer |
2196 | |
2197 | BVH_Mat4f* aNodeTransforms = new BVH_Mat4f[myRaytraceGeometry.Size()]; |
84c71f29 |
2198 | |
e2da917a |
2199 | bool aResult = true; |
2200 | |
84c71f29 |
2201 | for (Standard_Integer anElemIndex = 0; anElemIndex < myRaytraceGeometry.Size(); ++anElemIndex) |
2202 | { |
2203 | OpenGl_TriangleSet* aTriangleSet = dynamic_cast<OpenGl_TriangleSet*> ( |
2204 | myRaytraceGeometry.Objects().ChangeValue (anElemIndex).operator->()); |
2205 | |
f5b72419 |
2206 | const BVH_Transform<Standard_ShortReal, 4>* aTransform = dynamic_cast<const BVH_Transform<Standard_ShortReal, 4>* > (aTriangleSet->Properties().get()); |
84c71f29 |
2207 | Standard_ASSERT_RETURN (aTransform != NULL, |
2208 | "OpenGl_TriangleSet does not contain transform", Standard_False); |
2209 | |
2210 | aNodeTransforms[anElemIndex] = aTransform->Inversed(); |
84c71f29 |
2211 | } |
2212 | |
91c60b57 |
2213 | aResult &= mySceneTransformTexture->Init (theGlContext, 4, |
84c71f29 |
2214 | myRaytraceGeometry.Size() * 4, reinterpret_cast<const GLfloat*> (aNodeTransforms)); |
2215 | |
25ef750e |
2216 | delete [] aNodeTransforms; |
84c71f29 |
2217 | |
2218 | ///////////////////////////////////////////////////////////////////////////// |
bc8c79bb |
2219 | // Write geometry and bottom-level BVH buffers |
84c71f29 |
2220 | |
fc73a202 |
2221 | Standard_Size aTotalVerticesNb = 0; |
2222 | Standard_Size aTotalElementsNb = 0; |
2223 | Standard_Size aTotalBVHNodesNb = 0; |
265d4508 |
2224 | |
fc73a202 |
2225 | for (Standard_Integer anElemIndex = 0; anElemIndex < myRaytraceGeometry.Size(); ++anElemIndex) |
2226 | { |
2227 | OpenGl_TriangleSet* aTriangleSet = dynamic_cast<OpenGl_TriangleSet*> ( |
2228 | myRaytraceGeometry.Objects().ChangeValue (anElemIndex).operator->()); |
265d4508 |
2229 | |
fc73a202 |
2230 | Standard_ASSERT_RETURN (aTriangleSet != NULL, |
2231 | "Error: Failed to get triangulation of OpenGL element", Standard_False); |
2232 | |
2233 | aTotalVerticesNb += aTriangleSet->Vertices.size(); |
2234 | aTotalElementsNb += aTriangleSet->Elements.size(); |
e276548b |
2235 | |
f2474958 |
2236 | Standard_ASSERT_RETURN (!aTriangleSet->QuadBVH().IsNull(), |
fc73a202 |
2237 | "Error: Failed to get bottom-level BVH of OpenGL element", Standard_False); |
265d4508 |
2238 | |
f2474958 |
2239 | aTotalBVHNodesNb += aTriangleSet->QuadBVH()->NodeInfoBuffer().size(); |
fc73a202 |
2240 | } |
e276548b |
2241 | |
f2474958 |
2242 | aTotalBVHNodesNb += myRaytraceGeometry.QuadBVH()->NodeInfoBuffer().size(); |
e2da917a |
2243 | |
fc73a202 |
2244 | if (aTotalBVHNodesNb != 0) |
e276548b |
2245 | { |
e2da917a |
2246 | aResult &= mySceneNodeInfoTexture->Init ( |
91c60b57 |
2247 | theGlContext, 4, GLsizei (aTotalBVHNodesNb), static_cast<const GLuint*> (NULL)); |
e2da917a |
2248 | aResult &= mySceneMinPointTexture->Init ( |
91c60b57 |
2249 | theGlContext, 3, GLsizei (aTotalBVHNodesNb), static_cast<const GLfloat*> (NULL)); |
e2da917a |
2250 | aResult &= mySceneMaxPointTexture->Init ( |
91c60b57 |
2251 | theGlContext, 3, GLsizei (aTotalBVHNodesNb), static_cast<const GLfloat*> (NULL)); |
fc73a202 |
2252 | } |
265d4508 |
2253 | |
fc73a202 |
2254 | if (!aResult) |
2255 | { |
93cdaa76 |
2256 | Message::SendTrace() << "Error: Failed to upload buffers for bottom-level scene BVH"; |
fc73a202 |
2257 | return Standard_False; |
2258 | } |
e276548b |
2259 | |
fc73a202 |
2260 | if (aTotalElementsNb != 0) |
2261 | { |
25ef750e |
2262 | aResult &= myGeometryTriangTexture->Init ( |
91c60b57 |
2263 | theGlContext, 4, GLsizei (aTotalElementsNb), static_cast<const GLuint*> (NULL)); |
fc73a202 |
2264 | } |
265d4508 |
2265 | |
fc73a202 |
2266 | if (aTotalVerticesNb != 0) |
2267 | { |
25ef750e |
2268 | aResult &= myGeometryVertexTexture->Init ( |
91c60b57 |
2269 | theGlContext, 3, GLsizei (aTotalVerticesNb), static_cast<const GLfloat*> (NULL)); |
25ef750e |
2270 | aResult &= myGeometryNormalTexture->Init ( |
91c60b57 |
2271 | theGlContext, 3, GLsizei (aTotalVerticesNb), static_cast<const GLfloat*> (NULL)); |
25ef750e |
2272 | aResult &= myGeometryTexCrdTexture->Init ( |
91c60b57 |
2273 | theGlContext, 2, GLsizei (aTotalVerticesNb), static_cast<const GLfloat*> (NULL)); |
fc73a202 |
2274 | } |
265d4508 |
2275 | |
fc73a202 |
2276 | if (!aResult) |
2277 | { |
93cdaa76 |
2278 | Message::SendTrace() << "Error: Failed to upload buffers for scene geometry"; |
fc73a202 |
2279 | return Standard_False; |
2280 | } |
265d4508 |
2281 | |
f2474958 |
2282 | const QuadBvhHandle& aBVH = myRaytraceGeometry.QuadBVH(); |
91c60b57 |
2283 | |
2284 | if (aBVH->Length() > 0) |
1e99558f |
2285 | { |
91c60b57 |
2286 | aResult &= mySceneNodeInfoTexture->SubData (theGlContext, 0, aBVH->Length(), |
1e99558f |
2287 | reinterpret_cast<const GLuint*> (&aBVH->NodeInfoBuffer().front())); |
91c60b57 |
2288 | aResult &= mySceneMinPointTexture->SubData (theGlContext, 0, aBVH->Length(), |
1e99558f |
2289 | reinterpret_cast<const GLfloat*> (&aBVH->MinPointBuffer().front())); |
91c60b57 |
2290 | aResult &= mySceneMaxPointTexture->SubData (theGlContext, 0, aBVH->Length(), |
1e99558f |
2291 | reinterpret_cast<const GLfloat*> (&aBVH->MaxPointBuffer().front())); |
2292 | } |
e2da917a |
2293 | |
fc73a202 |
2294 | for (Standard_Integer aNodeIdx = 0; aNodeIdx < aBVH->Length(); ++aNodeIdx) |
2295 | { |
2296 | if (!aBVH->IsOuter (aNodeIdx)) |
2297 | continue; |
265d4508 |
2298 | |
fc73a202 |
2299 | OpenGl_TriangleSet* aTriangleSet = myRaytraceGeometry.TriangleSet (aNodeIdx); |
e276548b |
2300 | |
fc73a202 |
2301 | Standard_ASSERT_RETURN (aTriangleSet != NULL, |
2302 | "Error: Failed to get triangulation of OpenGL element", Standard_False); |
e276548b |
2303 | |
e2da917a |
2304 | Standard_Integer aBVHOffset = myRaytraceGeometry.AccelerationOffset (aNodeIdx); |
e276548b |
2305 | |
fc73a202 |
2306 | Standard_ASSERT_RETURN (aBVHOffset != OpenGl_RaytraceGeometry::INVALID_OFFSET, |
2307 | "Error: Failed to get offset for bottom-level BVH", Standard_False); |
e276548b |
2308 | |
f2474958 |
2309 | const Standard_Integer aBvhBuffersSize = aTriangleSet->QuadBVH()->Length(); |
265d4508 |
2310 | |
e2da917a |
2311 | if (aBvhBuffersSize != 0) |
fc73a202 |
2312 | { |
91c60b57 |
2313 | aResult &= mySceneNodeInfoTexture->SubData (theGlContext, aBVHOffset, aBvhBuffersSize, |
f2474958 |
2314 | reinterpret_cast<const GLuint*> (&aTriangleSet->QuadBVH()->NodeInfoBuffer().front())); |
91c60b57 |
2315 | aResult &= mySceneMinPointTexture->SubData (theGlContext, aBVHOffset, aBvhBuffersSize, |
f2474958 |
2316 | reinterpret_cast<const GLfloat*> (&aTriangleSet->QuadBVH()->MinPointBuffer().front())); |
91c60b57 |
2317 | aResult &= mySceneMaxPointTexture->SubData (theGlContext, aBVHOffset, aBvhBuffersSize, |
f2474958 |
2318 | reinterpret_cast<const GLfloat*> (&aTriangleSet->QuadBVH()->MaxPointBuffer().front())); |
91c60b57 |
2319 | |
fc73a202 |
2320 | if (!aResult) |
e276548b |
2321 | { |
93cdaa76 |
2322 | Message::SendTrace() << "Error: Failed to upload buffers for bottom-level scene BVHs"; |
e276548b |
2323 | return Standard_False; |
2324 | } |
2325 | } |
2326 | |
fc73a202 |
2327 | const Standard_Integer aVerticesOffset = myRaytraceGeometry.VerticesOffset (aNodeIdx); |
265d4508 |
2328 | |
fc73a202 |
2329 | Standard_ASSERT_RETURN (aVerticesOffset != OpenGl_RaytraceGeometry::INVALID_OFFSET, |
2330 | "Error: Failed to get offset for triangulation vertices of OpenGL element", Standard_False); |
265d4508 |
2331 | |
fc73a202 |
2332 | if (!aTriangleSet->Vertices.empty()) |
2333 | { |
91c60b57 |
2334 | aResult &= myGeometryNormalTexture->SubData (theGlContext, aVerticesOffset, |
2335 | GLsizei (aTriangleSet->Normals.size()), reinterpret_cast<const GLfloat*> (&aTriangleSet->Normals.front())); |
2336 | aResult &= myGeometryTexCrdTexture->SubData (theGlContext, aVerticesOffset, |
2337 | GLsizei (aTriangleSet->TexCrds.size()), reinterpret_cast<const GLfloat*> (&aTriangleSet->TexCrds.front())); |
2338 | aResult &= myGeometryVertexTexture->SubData (theGlContext, aVerticesOffset, |
2339 | GLsizei (aTriangleSet->Vertices.size()), reinterpret_cast<const GLfloat*> (&aTriangleSet->Vertices.front())); |
fc73a202 |
2340 | } |
e276548b |
2341 | |
fc73a202 |
2342 | const Standard_Integer anElementsOffset = myRaytraceGeometry.ElementsOffset (aNodeIdx); |
265d4508 |
2343 | |
fc73a202 |
2344 | Standard_ASSERT_RETURN (anElementsOffset != OpenGl_RaytraceGeometry::INVALID_OFFSET, |
2345 | "Error: Failed to get offset for triangulation elements of OpenGL element", Standard_False); |
e276548b |
2346 | |
fc73a202 |
2347 | if (!aTriangleSet->Elements.empty()) |
e276548b |
2348 | { |
91c60b57 |
2349 | aResult &= myGeometryTriangTexture->SubData (theGlContext, anElementsOffset, GLsizei (aTriangleSet->Elements.size()), |
5cff985a |
2350 | reinterpret_cast<const GLuint*> (&aTriangleSet->Elements.front())); |
e276548b |
2351 | } |
265d4508 |
2352 | |
fc73a202 |
2353 | if (!aResult) |
2354 | { |
93cdaa76 |
2355 | Message::SendTrace() << "Error: Failed to upload triangulation buffers for OpenGL element"; |
fc73a202 |
2356 | return Standard_False; |
2357 | } |
e276548b |
2358 | } |
e276548b |
2359 | |
25ef750e |
2360 | ///////////////////////////////////////////////////////////////////////////// |
2361 | // Write material buffer |
2362 | |
fc73a202 |
2363 | if (myRaytraceGeometry.Materials.size() != 0) |
2364 | { |
91c60b57 |
2365 | aResult &= myRaytraceMaterialTexture->Init (theGlContext, 4, |
05aa616d |
2366 | GLsizei (myRaytraceGeometry.Materials.size() * 19), myRaytraceGeometry.Materials.front().Packed()); |
25ef750e |
2367 | |
fc73a202 |
2368 | if (!aResult) |
2369 | { |
93cdaa76 |
2370 | Message::SendTrace() << "Error: Failed to upload material buffer"; |
fc73a202 |
2371 | return Standard_False; |
2372 | } |
2373 | } |
e276548b |
2374 | |
fc73a202 |
2375 | myIsRaytraceDataValid = myRaytraceGeometry.Objects().Size() != 0; |
e276548b |
2376 | |
fc73a202 |
2377 | #ifdef RAY_TRACE_PRINT_INFO |
e276548b |
2378 | |
f2474958 |
2379 | Standard_ShortReal aMemTrgUsed = 0.f; |
2380 | Standard_ShortReal aMemBvhUsed = 0.f; |
e276548b |
2381 | |
fc73a202 |
2382 | for (Standard_Integer anElemIdx = 0; anElemIdx < myRaytraceGeometry.Size(); ++anElemIdx) |
2383 | { |
f2474958 |
2384 | OpenGl_TriangleSet* aTriangleSet = dynamic_cast<OpenGl_TriangleSet*> (myRaytraceGeometry.Objects()(anElemIdx).get()); |
e276548b |
2385 | |
f2474958 |
2386 | aMemTrgUsed += static_cast<Standard_ShortReal> ( |
25ef750e |
2387 | aTriangleSet->Vertices.size() * sizeof (BVH_Vec3f)); |
f2474958 |
2388 | aMemTrgUsed += static_cast<Standard_ShortReal> ( |
25ef750e |
2389 | aTriangleSet->Normals.size() * sizeof (BVH_Vec3f)); |
f2474958 |
2390 | aMemTrgUsed += static_cast<Standard_ShortReal> ( |
25ef750e |
2391 | aTriangleSet->TexCrds.size() * sizeof (BVH_Vec2f)); |
f2474958 |
2392 | aMemTrgUsed += static_cast<Standard_ShortReal> ( |
fc73a202 |
2393 | aTriangleSet->Elements.size() * sizeof (BVH_Vec4i)); |
e276548b |
2394 | |
f2474958 |
2395 | aMemBvhUsed += static_cast<Standard_ShortReal> ( |
2396 | aTriangleSet->QuadBVH()->NodeInfoBuffer().size() * sizeof (BVH_Vec4i)); |
2397 | aMemBvhUsed += static_cast<Standard_ShortReal> ( |
2398 | aTriangleSet->QuadBVH()->MinPointBuffer().size() * sizeof (BVH_Vec3f)); |
2399 | aMemBvhUsed += static_cast<Standard_ShortReal> ( |
2400 | aTriangleSet->QuadBVH()->MaxPointBuffer().size() * sizeof (BVH_Vec3f)); |
fc73a202 |
2401 | } |
e276548b |
2402 | |
f2474958 |
2403 | aMemBvhUsed += static_cast<Standard_ShortReal> ( |
2404 | myRaytraceGeometry.QuadBVH()->NodeInfoBuffer().size() * sizeof (BVH_Vec4i)); |
2405 | aMemBvhUsed += static_cast<Standard_ShortReal> ( |
2406 | myRaytraceGeometry.QuadBVH()->MinPointBuffer().size() * sizeof (BVH_Vec3f)); |
2407 | aMemBvhUsed += static_cast<Standard_ShortReal> ( |
2408 | myRaytraceGeometry.QuadBVH()->MaxPointBuffer().size() * sizeof (BVH_Vec3f)); |
e276548b |
2409 | |
f2474958 |
2410 | std::cout << "GPU Memory Used (Mb):\n" |
2411 | << "\tFor mesh: " << aMemTrgUsed / 1048576 << "\n" |
2412 | << "\tFor BVHs: " << aMemBvhUsed / 1048576 << "\n"; |
e276548b |
2413 | |
fc73a202 |
2414 | #endif |
e276548b |
2415 | |
fc73a202 |
2416 | return aResult; |
2417 | } |
e276548b |
2418 | |
fc73a202 |
2419 | // ======================================================================= |
189f85a3 |
2420 | // function : updateRaytraceLightSources |
91c60b57 |
2421 | // purpose : Updates 3D scene light sources for ray-tracing |
fc73a202 |
2422 | // ======================================================================= |
91c60b57 |
2423 | Standard_Boolean OpenGl_View::updateRaytraceLightSources (const OpenGl_Mat4& theInvModelView, const Handle(OpenGl_Context)& theGlContext) |
fc73a202 |
2424 | { |
992ed6b3 |
2425 | std::vector<Handle(Graphic3d_CLight)> aLightSources; |
89d855ba |
2426 | Graphic3d_Vec4 aNewAmbient (0.0f); |
114db5a5 |
2427 | if (myRenderParams.ShadingModel != Graphic3d_TOSM_UNLIT |
992ed6b3 |
2428 | && !myLights.IsNull()) |
6e728f3b |
2429 | { |
89d855ba |
2430 | aNewAmbient.SetValues (myLights->AmbientColor().rgb(), 0.0f); |
91c60b57 |
2431 | |
6e728f3b |
2432 | // move positional light sources at the front of the list |
992ed6b3 |
2433 | aLightSources.reserve (myLights->Extent()); |
2434 | for (Graphic3d_LightSet::Iterator aLightIter (myLights, Graphic3d_LightSet::IterationFilter_ExcludeDisabledAndAmbient); |
2435 | aLightIter.More(); aLightIter.Next()) |
2436 | { |
2437 | const Graphic3d_CLight& aLight = *aLightIter.Value(); |
2438 | if (aLight.Type() != Graphic3d_TOLS_DIRECTIONAL) |
2439 | { |
2440 | aLightSources.push_back (aLightIter.Value()); |
2441 | } |
2442 | } |
2443 | |
2444 | for (Graphic3d_LightSet::Iterator aLightIter (myLights, Graphic3d_LightSet::IterationFilter_ExcludeDisabledAndAmbient); |
2445 | aLightIter.More(); aLightIter.Next()) |
2446 | { |
2447 | if (aLightIter.Value()->Type() == Graphic3d_TOLS_DIRECTIONAL) |
2448 | { |
2449 | aLightSources.push_back (aLightIter.Value()); |
2450 | } |
2451 | } |
6e728f3b |
2452 | } |
2453 | |
89d855ba |
2454 | if (!myRaytraceGeometry.Ambient.IsEqual (aNewAmbient)) |
2455 | { |
2456 | myAccumFrames = 0; |
2457 | myRaytraceGeometry.Ambient = aNewAmbient; |
2458 | } |
2459 | |
6e728f3b |
2460 | // get number of 'real' (not ambient) light sources |
992ed6b3 |
2461 | const size_t aNbLights = aLightSources.size(); |
6e728f3b |
2462 | Standard_Boolean wasUpdated = myRaytraceGeometry.Sources.size () != aNbLights; |
6e728f3b |
2463 | if (wasUpdated) |
2464 | { |
2465 | myRaytraceGeometry.Sources.resize (aNbLights); |
2466 | } |
2467 | |
6e728f3b |
2468 | for (size_t aLightIdx = 0, aRealIdx = 0; aLightIdx < aLightSources.size(); ++aLightIdx) |
91c60b57 |
2469 | { |
992ed6b3 |
2470 | const Graphic3d_CLight& aLight = *aLightSources[aLightIdx]; |
2471 | const Graphic3d_Vec4& aLightColor = aLight.PackedColor(); |
2472 | BVH_Vec4f aEmission (aLightColor.r() * aLight.Intensity(), |
2473 | aLightColor.g() * aLight.Intensity(), |
2474 | aLightColor.b() * aLight.Intensity(), |
6e728f3b |
2475 | 1.0f); |
91c60b57 |
2476 | |
88b312d3 |
2477 | BVH_Vec4f aPosition (-aLight.PackedDirectionRange().x(), |
2478 | -aLight.PackedDirectionRange().y(), |
2479 | -aLight.PackedDirectionRange().z(), |
91c60b57 |
2480 | 0.0f); |
2481 | |
992ed6b3 |
2482 | if (aLight.Type() != Graphic3d_TOLS_DIRECTIONAL) |
91c60b57 |
2483 | { |
992ed6b3 |
2484 | aPosition = BVH_Vec4f (static_cast<float>(aLight.Position().X()), |
2485 | static_cast<float>(aLight.Position().Y()), |
2486 | static_cast<float>(aLight.Position().Z()), |
91c60b57 |
2487 | 1.0f); |
189f85a3 |
2488 | |
6e728f3b |
2489 | // store smoothing radius in W-component |
992ed6b3 |
2490 | aEmission.w() = Max (aLight.Smoothness(), 0.f); |
189f85a3 |
2491 | } |
2492 | else |
2493 | { |
6e728f3b |
2494 | // store cosine of smoothing angle in W-component |
992ed6b3 |
2495 | aEmission.w() = cosf (Min (Max (aLight.Smoothness(), 0.f), static_cast<Standard_ShortReal> (M_PI / 2.0))); |
91c60b57 |
2496 | } |
2497 | |
992ed6b3 |
2498 | if (aLight.IsHeadlight()) |
91c60b57 |
2499 | { |
2500 | aPosition = theInvModelView * aPosition; |
2501 | } |
2502 | |
6e728f3b |
2503 | for (int aK = 0; aK < 4; ++aK) |
2504 | { |
2505 | wasUpdated |= (aEmission[aK] != myRaytraceGeometry.Sources[aRealIdx].Emission[aK]) |
2506 | || (aPosition[aK] != myRaytraceGeometry.Sources[aRealIdx].Position[aK]); |
2507 | } |
2508 | |
2509 | if (wasUpdated) |
2510 | { |
2511 | myRaytraceGeometry.Sources[aRealIdx] = OpenGl_RaytraceLight (aEmission, aPosition); |
2512 | } |
2513 | |
2514 | ++aRealIdx; |
91c60b57 |
2515 | } |
2516 | |
6e728f3b |
2517 | if (myRaytraceLightSrcTexture.IsNull()) // create light source buffer |
91c60b57 |
2518 | { |
2519 | myRaytraceLightSrcTexture = new OpenGl_TextureBufferArb; |
91c60b57 |
2520 | } |
189f85a3 |
2521 | |
6e728f3b |
2522 | if (myRaytraceGeometry.Sources.size() != 0 && wasUpdated) |
fc73a202 |
2523 | { |
91c60b57 |
2524 | const GLfloat* aDataPtr = myRaytraceGeometry.Sources.front().Packed(); |
2525 | if (!myRaytraceLightSrcTexture->Init (theGlContext, 4, GLsizei (myRaytraceGeometry.Sources.size() * 2), aDataPtr)) |
2526 | { |
93cdaa76 |
2527 | Message::SendTrace() << "Error: Failed to upload light source buffer"; |
91c60b57 |
2528 | return Standard_False; |
2529 | } |
e276548b |
2530 | |
6e728f3b |
2531 | myAccumFrames = 0; // accumulation should be restarted |
91c60b57 |
2532 | } |
e276548b |
2533 | |
6e728f3b |
2534 | return Standard_True; |
fc73a202 |
2535 | } |
2536 | |
25ef750e |
2537 | // ======================================================================= |
189f85a3 |
2538 | // function : setUniformState |
25ef750e |
2539 | // purpose : Sets uniform state for the given ray-tracing shader program |
2540 | // ======================================================================= |
3a9b5dc8 |
2541 | Standard_Boolean OpenGl_View::setUniformState (const Standard_Integer theProgramId, |
2542 | const Standard_Integer theWinSizeX, |
2543 | const Standard_Integer theWinSizeY, |
b27ab03d |
2544 | Graphic3d_Camera::Projection theProjection, |
91c60b57 |
2545 | const Handle(OpenGl_Context)& theGlContext) |
25ef750e |
2546 | { |
3a9b5dc8 |
2547 | // Get projection state |
2548 | OpenGl_MatrixState<Standard_ShortReal>& aCntxProjectionState = theGlContext->ProjectionState; |
2549 | |
2550 | OpenGl_Mat4 aViewPrjMat; |
2551 | OpenGl_Mat4 anUnviewMat; |
2552 | OpenGl_Vec3 aOrigins[4]; |
2553 | OpenGl_Vec3 aDirects[4]; |
2554 | |
b27ab03d |
2555 | if (myCamera->IsOrthographic() |
2556 | || !myRenderParams.IsGlobalIlluminationEnabled) |
2557 | { |
2558 | updateCamera (myCamera->OrientationMatrixF(), |
2559 | aCntxProjectionState.Current(), |
2560 | aOrigins, |
2561 | aDirects, |
2562 | aViewPrjMat, |
2563 | anUnviewMat); |
832a6f44 |
2564 | |
2565 | if (myRenderParams.UseEnvironmentMapBackground |
2566 | || myRaytraceParameters.CubemapForBack) |
2567 | { |
2568 | OpenGl_Mat4 aTempMat; |
2569 | OpenGl_Mat4 aTempInvMat; |
2570 | updatePerspCameraPT (myCamera->OrientationMatrixF(), |
2571 | aCntxProjectionState.Current(), |
2572 | theProjection, |
2573 | aTempMat, |
2574 | aTempInvMat, |
2575 | theWinSizeX, |
2576 | theWinSizeY); |
2577 | } |
b27ab03d |
2578 | } |
2579 | else |
2580 | { |
2581 | updatePerspCameraPT (myCamera->OrientationMatrixF(), |
2582 | aCntxProjectionState.Current(), |
2583 | theProjection, |
2584 | aViewPrjMat, |
2585 | anUnviewMat, |
2586 | theWinSizeX, |
2587 | theWinSizeY); |
2588 | } |
3a9b5dc8 |
2589 | |
2590 | Handle(OpenGl_ShaderProgram)& theProgram = theProgramId == 0 |
2591 | ? myRaytraceProgram |
2592 | : myPostFSAAProgram; |
91c60b57 |
2593 | |
2594 | if (theProgram.IsNull()) |
25ef750e |
2595 | { |
2596 | return Standard_False; |
2597 | } |
b27ab03d |
2598 | |
2599 | theProgram->SetUniform(theGlContext, "uEyeOrig", myEyeOrig); |
2600 | theProgram->SetUniform(theGlContext, "uEyeView", myEyeView); |
2601 | theProgram->SetUniform(theGlContext, "uEyeVert", myEyeVert); |
2602 | theProgram->SetUniform(theGlContext, "uEyeSide", myEyeSide); |
2603 | theProgram->SetUniform(theGlContext, "uEyeSize", myEyeSize); |
2604 | |
2605 | theProgram->SetUniform(theGlContext, "uApertureRadius", myRenderParams.CameraApertureRadius); |
2606 | theProgram->SetUniform(theGlContext, "uFocalPlaneDist", myRenderParams.CameraFocalPlaneDist); |
89d855ba |
2607 | |
25ef750e |
2608 | // Set camera state |
189f85a3 |
2609 | theProgram->SetUniform (theGlContext, |
3a9b5dc8 |
2610 | myUniformLocations[theProgramId][OpenGl_RT_uOriginLB], aOrigins[0]); |
189f85a3 |
2611 | theProgram->SetUniform (theGlContext, |
3a9b5dc8 |
2612 | myUniformLocations[theProgramId][OpenGl_RT_uOriginRB], aOrigins[1]); |
189f85a3 |
2613 | theProgram->SetUniform (theGlContext, |
3a9b5dc8 |
2614 | myUniformLocations[theProgramId][OpenGl_RT_uOriginLT], aOrigins[2]); |
189f85a3 |
2615 | theProgram->SetUniform (theGlContext, |
3a9b5dc8 |
2616 | myUniformLocations[theProgramId][OpenGl_RT_uOriginRT], aOrigins[3]); |
189f85a3 |
2617 | theProgram->SetUniform (theGlContext, |
3a9b5dc8 |
2618 | myUniformLocations[theProgramId][OpenGl_RT_uDirectLB], aDirects[0]); |
189f85a3 |
2619 | theProgram->SetUniform (theGlContext, |
3a9b5dc8 |
2620 | myUniformLocations[theProgramId][OpenGl_RT_uDirectRB], aDirects[1]); |
189f85a3 |
2621 | theProgram->SetUniform (theGlContext, |
3a9b5dc8 |
2622 | myUniformLocations[theProgramId][OpenGl_RT_uDirectLT], aDirects[2]); |
189f85a3 |
2623 | theProgram->SetUniform (theGlContext, |
3a9b5dc8 |
2624 | myUniformLocations[theProgramId][OpenGl_RT_uDirectRT], aDirects[3]); |
1d865689 |
2625 | theProgram->SetUniform (theGlContext, |
3a9b5dc8 |
2626 | myUniformLocations[theProgramId][OpenGl_RT_uViewPrMat], aViewPrjMat); |
189f85a3 |
2627 | theProgram->SetUniform (theGlContext, |
3a9b5dc8 |
2628 | myUniformLocations[theProgramId][OpenGl_RT_uUnviewMat], anUnviewMat); |
312a4043 |
2629 | |
b09447ed |
2630 | // Set screen dimensions |
2631 | myRaytraceProgram->SetUniform (theGlContext, |
2632 | myUniformLocations[theProgramId][OpenGl_RT_uWinSizeX], theWinSizeX); |
2633 | myRaytraceProgram->SetUniform (theGlContext, |
2634 | myUniformLocations[theProgramId][OpenGl_RT_uWinSizeY], theWinSizeY); |
2635 | |
2636 | // Set 3D scene parameters |
189f85a3 |
2637 | theProgram->SetUniform (theGlContext, |
91c60b57 |
2638 | myUniformLocations[theProgramId][OpenGl_RT_uSceneRad], myRaytraceSceneRadius); |
189f85a3 |
2639 | theProgram->SetUniform (theGlContext, |
91c60b57 |
2640 | myUniformLocations[theProgramId][OpenGl_RT_uSceneEps], myRaytraceSceneEpsilon); |
3a9b5dc8 |
2641 | |
b09447ed |
2642 | // Set light source parameters |
3a9b5dc8 |
2643 | const Standard_Integer aLightSourceBufferSize = |
2644 | static_cast<Standard_Integer> (myRaytraceGeometry.Sources.size()); |
b09447ed |
2645 | |
189f85a3 |
2646 | theProgram->SetUniform (theGlContext, |
91c60b57 |
2647 | myUniformLocations[theProgramId][OpenGl_RT_uLightCount], aLightSourceBufferSize); |
8c820969 |
2648 | |
25ef750e |
2649 | // Set array of 64-bit texture handles |
91c60b57 |
2650 | if (theGlContext->arbTexBindless != NULL && myRaytraceGeometry.HasTextures()) |
25ef750e |
2651 | { |
47e9c178 |
2652 | const std::vector<GLuint64>& aTextures = myRaytraceGeometry.TextureHandles(); |
2653 | |
189f85a3 |
2654 | theProgram->SetUniform (theGlContext, myUniformLocations[theProgramId][OpenGl_RT_uTexSamplersArray], |
3a9b5dc8 |
2655 | static_cast<GLsizei> (aTextures.size()), reinterpret_cast<const OpenGl_Vec2u*> (&aTextures.front())); |
25ef750e |
2656 | } |
2657 | |
ba00aab7 |
2658 | // Set background colors (only vertical gradient background supported) |
2659 | OpenGl_Vec4 aBackColorTop = myBgColor, aBackColorBot = myBgColor; |
077a220c |
2660 | if (myBackgrounds[Graphic3d_TOB_GRADIENT] != NULL |
2661 | && myBackgrounds[Graphic3d_TOB_GRADIENT]->IsDefined()) |
25ef750e |
2662 | { |
ba00aab7 |
2663 | aBackColorTop = myBackgrounds[Graphic3d_TOB_GRADIENT]->GradientColor (0); |
2664 | aBackColorBot = myBackgrounds[Graphic3d_TOB_GRADIENT]->GradientColor (1); |
f051908e |
2665 | |
2666 | if (myCamera->Tile().IsValid()) |
2667 | { |
2668 | Standard_Integer aTileOffset = myCamera->Tile().OffsetLowerLeft().y(); |
2669 | Standard_Integer aTileSize = myCamera->Tile().TileSize.y(); |
2670 | Standard_Integer aViewSize = myCamera->Tile().TotalSize.y(); |
2671 | OpenGl_Vec4 aColorRange = aBackColorTop - aBackColorBot; |
2672 | aBackColorBot = aBackColorBot + aColorRange * ((float) aTileOffset / aViewSize); |
2673 | aBackColorTop = aBackColorBot + aColorRange * ((float) aTileSize / aViewSize); |
2674 | } |
67c12d80 |
2675 | } |
ba00aab7 |
2676 | aBackColorTop = theGlContext->Vec4FromQuantityColor (aBackColorTop); |
2677 | aBackColorBot = theGlContext->Vec4FromQuantityColor (aBackColorBot); |
2678 | theProgram->SetUniform (theGlContext, myUniformLocations[theProgramId][OpenGl_RT_uBackColorTop], aBackColorTop); |
2679 | theProgram->SetUniform (theGlContext, myUniformLocations[theProgramId][OpenGl_RT_uBackColorBot], aBackColorBot); |
25ef750e |
2680 | |
b09447ed |
2681 | // Set environment map parameters |
89d855ba |
2682 | const Handle(OpenGl_TextureSet)& anEnvTextureSet = myRaytraceParameters.CubemapForBack |
2683 | ? myCubeMapParams->TextureSet (theGlContext) |
2684 | : myTextureEnv; |
2685 | const bool toDisableEnvironmentMap = anEnvTextureSet.IsNull() |
2686 | || anEnvTextureSet->IsEmpty() |
2687 | || !anEnvTextureSet->First()->IsValid(); |
2688 | theProgram->SetUniform (theGlContext, myUniformLocations[theProgramId][OpenGl_RT_uEnvMapEnabled], |
2689 | toDisableEnvironmentMap ? 0 : 1); |
2690 | if (myRaytraceParameters.CubemapForBack) |
2691 | { |
2692 | theProgram->SetUniform (theGlContext, "uZCoeff", myBackgroundCubeMap->ZIsInverted() ? -1 : 1); |
2693 | theProgram->SetUniform (theGlContext, "uYCoeff", myBackgroundCubeMap->IsTopDown() ? 1 : -1); |
2694 | theProgram->SetUniform (theGlContext, myUniformLocations[theProgramId][OpenGl_RT_uEnvMapForBack], |
2695 | myBackgroundType == Graphic3d_TOB_CUBEMAP ? 1 : 0); |
2696 | } |
2697 | else |
2698 | { |
2699 | theProgram->SetUniform (theGlContext, myUniformLocations[theProgramId][OpenGl_RT_uEnvMapForBack], |
2700 | myRenderParams.UseEnvironmentMapBackground ? 1 : 0); |
2701 | } |
6e728f3b |
2702 | |
89d855ba |
2703 | // Set ambient light source |
189f85a3 |
2704 | theProgram->SetUniform (theGlContext, |
89d855ba |
2705 | myUniformLocations[theProgramId][OpenGl_RT_uLightAmbnt], myRaytraceGeometry.Ambient); |
b09447ed |
2706 | if (myRenderParams.IsGlobalIlluminationEnabled) // GI parameters |
2707 | { |
2708 | theProgram->SetUniform (theGlContext, |
2709 | myUniformLocations[theProgramId][OpenGl_RT_uMaxRadiance], myRenderParams.RadianceClampingValue); |
2710 | |
2711 | theProgram->SetUniform (theGlContext, |
2712 | myUniformLocations[theProgramId][OpenGl_RT_uBlockedRngEnabled], myRenderParams.CoherentPathTracingMode ? 1 : 0); |
2713 | |
2714 | // Check whether we should restart accumulation for run-time parameters |
2715 | if (myRenderParams.RadianceClampingValue != myRaytraceParameters.RadianceClampingValue |
2716 | || myRenderParams.UseEnvironmentMapBackground != myRaytraceParameters.UseEnvMapForBackground) |
2717 | { |
2718 | myAccumFrames = 0; // accumulation should be restarted |
2719 | |
2720 | myRaytraceParameters.RadianceClampingValue = myRenderParams.RadianceClampingValue; |
2721 | myRaytraceParameters.UseEnvMapForBackground = myRenderParams.UseEnvironmentMapBackground; |
2722 | } |
2723 | } |
2724 | else // RT parameters |
2725 | { |
b09447ed |
2726 | // Enable/disable run-time ray-tracing effects |
2727 | theProgram->SetUniform (theGlContext, |
2728 | myUniformLocations[theProgramId][OpenGl_RT_uShadowsEnabled], myRenderParams.IsShadowEnabled ? 1 : 0); |
2729 | theProgram->SetUniform (theGlContext, |
2730 | myUniformLocations[theProgramId][OpenGl_RT_uReflectEnabled], myRenderParams.IsReflectionEnabled ? 1 : 0); |
2731 | } |
2732 | |
189f85a3 |
2733 | return Standard_True; |
25ef750e |
2734 | } |
2735 | |
91c60b57 |
2736 | // ======================================================================= |
189f85a3 |
2737 | // function : bindRaytraceTextures |
91c60b57 |
2738 | // purpose : Binds ray-trace textures to corresponding texture units |
2739 | // ======================================================================= |
66d1cdc6 |
2740 | void OpenGl_View::bindRaytraceTextures (const Handle(OpenGl_Context)& theGlContext, |
2741 | int theStereoView) |
91c60b57 |
2742 | { |
66d1cdc6 |
2743 | if (myRaytraceParameters.AdaptiveScreenSampling |
2744 | && myRaytraceParameters.GlobalIllumination) |
3a9b5dc8 |
2745 | { |
2746 | #if !defined(GL_ES_VERSION_2_0) |
66d1cdc6 |
2747 | theGlContext->core42->glBindImageTexture (OpenGl_RT_OutputImage, |
2748 | myRaytraceOutputTexture[theStereoView]->TextureId(), 0, GL_TRUE, 0, GL_READ_WRITE, GL_R32F); |
2749 | theGlContext->core42->glBindImageTexture (OpenGl_RT_VisualErrorImage, |
2750 | myRaytraceVisualErrorTexture[theStereoView]->TextureId(), 0, GL_TRUE, 0, GL_READ_WRITE, GL_R32I); |
e084dbbc |
2751 | if (myRaytraceParameters.AdaptiveScreenSamplingAtomic) |
2752 | { |
2753 | theGlContext->core42->glBindImageTexture (OpenGl_RT_TileOffsetsImage, |
2754 | myRaytraceTileOffsetsTexture[theStereoView]->TextureId(), 0, GL_TRUE, 0, GL_READ_ONLY, GL_RG32I); |
2755 | } |
2756 | else |
2757 | { |
2758 | theGlContext->core42->glBindImageTexture (OpenGl_RT_TileSamplesImage, |
2759 | myRaytraceTileSamplesTexture[theStereoView]->TextureId(), 0, GL_TRUE, 0, GL_READ_WRITE, GL_R32I); |
2760 | } |
66d1cdc6 |
2761 | #else |
2762 | (void )theStereoView; |
2763 | #endif |
3a9b5dc8 |
2764 | } |
2765 | |
89d855ba |
2766 | const Handle(OpenGl_TextureSet)& anEnvTextureSet = myRaytraceParameters.CubemapForBack |
2767 | ? myCubeMapParams->TextureSet (theGlContext) |
2768 | : myTextureEnv; |
2769 | if (!anEnvTextureSet.IsNull() |
2770 | && !anEnvTextureSet->IsEmpty() |
2771 | && anEnvTextureSet->First()->IsValid()) |
6e728f3b |
2772 | { |
89d855ba |
2773 | anEnvTextureSet->First()->Bind (theGlContext, OpenGl_RT_EnvMapTexture); |
6e728f3b |
2774 | } |
2775 | |
cc8cbabe |
2776 | mySceneMinPointTexture ->BindTexture (theGlContext, OpenGl_RT_SceneMinPointTexture); |
2777 | mySceneMaxPointTexture ->BindTexture (theGlContext, OpenGl_RT_SceneMaxPointTexture); |
2778 | mySceneNodeInfoTexture ->BindTexture (theGlContext, OpenGl_RT_SceneNodeInfoTexture); |
2779 | myGeometryVertexTexture ->BindTexture (theGlContext, OpenGl_RT_GeometryVertexTexture); |
2780 | myGeometryNormalTexture ->BindTexture (theGlContext, OpenGl_RT_GeometryNormalTexture); |
2781 | myGeometryTexCrdTexture ->BindTexture (theGlContext, OpenGl_RT_GeometryTexCrdTexture); |
2782 | myGeometryTriangTexture ->BindTexture (theGlContext, OpenGl_RT_GeometryTriangTexture); |
2783 | mySceneTransformTexture ->BindTexture (theGlContext, OpenGl_RT_SceneTransformTexture); |
2784 | myRaytraceMaterialTexture->BindTexture (theGlContext, OpenGl_RT_RaytraceMaterialTexture); |
2785 | myRaytraceLightSrcTexture->BindTexture (theGlContext, OpenGl_RT_RaytraceLightSrcTexture); |
91c60b57 |
2786 | } |
2787 | |
2788 | // ======================================================================= |
189f85a3 |
2789 | // function : unbindRaytraceTextures |
91c60b57 |
2790 | // purpose : Unbinds ray-trace textures from corresponding texture units |
2791 | // ======================================================================= |
2792 | void OpenGl_View::unbindRaytraceTextures (const Handle(OpenGl_Context)& theGlContext) |
2793 | { |
cc8cbabe |
2794 | mySceneMinPointTexture ->UnbindTexture (theGlContext, OpenGl_RT_SceneMinPointTexture); |
2795 | mySceneMaxPointTexture ->UnbindTexture (theGlContext, OpenGl_RT_SceneMaxPointTexture); |
2796 | mySceneNodeInfoTexture ->UnbindTexture (theGlContext, OpenGl_RT_SceneNodeInfoTexture); |
2797 | myGeometryVertexTexture ->UnbindTexture (theGlContext, OpenGl_RT_GeometryVertexTexture); |
2798 | myGeometryNormalTexture ->UnbindTexture (theGlContext, OpenGl_RT_GeometryNormalTexture); |
2799 | myGeometryTexCrdTexture ->UnbindTexture (theGlContext, OpenGl_RT_GeometryTexCrdTexture); |
2800 | myGeometryTriangTexture ->UnbindTexture (theGlContext, OpenGl_RT_GeometryTriangTexture); |
2801 | mySceneTransformTexture ->UnbindTexture (theGlContext, OpenGl_RT_SceneTransformTexture); |
2802 | myRaytraceMaterialTexture->UnbindTexture (theGlContext, OpenGl_RT_RaytraceMaterialTexture); |
2803 | myRaytraceLightSrcTexture->UnbindTexture (theGlContext, OpenGl_RT_RaytraceLightSrcTexture); |
91c60b57 |
2804 | |
91c60b57 |
2805 | theGlContext->core15fwd->glActiveTexture (GL_TEXTURE0); |
2806 | } |
2807 | |
fc73a202 |
2808 | // ======================================================================= |
189f85a3 |
2809 | // function : runRaytraceShaders |
fc73a202 |
2810 | // purpose : Runs ray-tracing shader programs |
2811 | // ======================================================================= |
c357e426 |
2812 | Standard_Boolean OpenGl_View::runRaytraceShaders (const Standard_Integer theSizeX, |
91c60b57 |
2813 | const Standard_Integer theSizeY, |
bf02aa7d |
2814 | Graphic3d_Camera::Projection theProjection, |
38a0206f |
2815 | OpenGl_FrameBuffer* theReadDrawFbo, |
91c60b57 |
2816 | const Handle(OpenGl_Context)& theGlContext) |
fc73a202 |
2817 | { |
3a9b5dc8 |
2818 | Standard_Boolean aResult = theGlContext->BindProgram (myRaytraceProgram); |
189f85a3 |
2819 | |
3a9b5dc8 |
2820 | aResult &= setUniformState (0, |
2821 | theSizeX, |
2822 | theSizeY, |
b27ab03d |
2823 | theProjection, |
3a9b5dc8 |
2824 | theGlContext); |
bf02aa7d |
2825 | |
3a9b5dc8 |
2826 | if (myRaytraceParameters.GlobalIllumination) // path tracing |
189f85a3 |
2827 | { |
66d1cdc6 |
2828 | aResult &= runPathtrace (theSizeX, theSizeY, theProjection, theGlContext); |
2829 | aResult &= runPathtraceOut (theProjection, theReadDrawFbo, theGlContext); |
189f85a3 |
2830 | } |
3a9b5dc8 |
2831 | else // Whitted-style ray-tracing |
fc73a202 |
2832 | { |
3a9b5dc8 |
2833 | aResult &= runRaytrace (theSizeX, theSizeY, theProjection, theReadDrawFbo, theGlContext); |
fc73a202 |
2834 | } |
2835 | |
3a9b5dc8 |
2836 | return aResult; |
2837 | } |
fc73a202 |
2838 | |
3a9b5dc8 |
2839 | // ======================================================================= |
2840 | // function : runRaytrace |
2841 | // purpose : Runs Whitted-style ray-tracing |
2842 | // ======================================================================= |
2843 | Standard_Boolean OpenGl_View::runRaytrace (const Standard_Integer theSizeX, |
2844 | const Standard_Integer theSizeY, |
2845 | Graphic3d_Camera::Projection theProjection, |
2846 | OpenGl_FrameBuffer* theReadDrawFbo, |
2847 | const Handle(OpenGl_Context)& theGlContext) |
2848 | { |
2849 | Standard_Boolean aResult = Standard_True; |
fc73a202 |
2850 | |
3a9b5dc8 |
2851 | // Choose proper set of frame buffers for stereo rendering |
66d1cdc6 |
2852 | const Standard_Integer aFBOIdx = (theProjection == Graphic3d_Camera::Projection_MonoRightEye) ? 1 : 0; |
2853 | bindRaytraceTextures (theGlContext, aFBOIdx); |
3a9b5dc8 |
2854 | |
2855 | if (myRenderParams.IsAntialiasingEnabled) // if second FSAA pass is used |
2856 | { |
2857 | myRaytraceFBO1[aFBOIdx]->BindBuffer (theGlContext); |
2858 | |
2859 | glClear (GL_DEPTH_BUFFER_BIT); // render the image with depth |
fc73a202 |
2860 | } |
25ef750e |
2861 | |
189f85a3 |
2862 | theGlContext->core20fwd->glDrawArrays (GL_TRIANGLES, 0, 6); |
fc73a202 |
2863 | |
3a9b5dc8 |
2864 | if (myRenderParams.IsAntialiasingEnabled) |
189f85a3 |
2865 | { |
3a9b5dc8 |
2866 | glDisable (GL_DEPTH_TEST); // improve jagged edges without depth buffer |
1d865689 |
2867 | |
3a9b5dc8 |
2868 | // bind ray-tracing output image as input |
cc8cbabe |
2869 | myRaytraceFBO1[aFBOIdx]->ColorTexture()->Bind (theGlContext, OpenGl_RT_FsaaInputTexture); |
189f85a3 |
2870 | |
2871 | aResult &= theGlContext->BindProgram (myPostFSAAProgram); |
2872 | |
3a9b5dc8 |
2873 | aResult &= setUniformState (1 /* FSAA ID */, |
2874 | theSizeX, |
2875 | theSizeY, |
b27ab03d |
2876 | theProjection, |
189f85a3 |
2877 | theGlContext); |
2878 | |
91c60b57 |
2879 | // Perform multi-pass adaptive FSAA using ping-pong technique. |
2880 | // We use 'FLIPTRI' sampling pattern changing for every pixel |
2881 | // (3 additional samples per pixel, the 1st sample is already |
2882 | // available from initial ray-traced image). |
2883 | for (Standard_Integer anIt = 1; anIt < 4; ++anIt) |
50d0e1ce |
2884 | { |
93cdaa76 |
2885 | OpenGl_Vec2 aFsaaOffset (1.f / theSizeX, 1.f / theSizeY); |
91c60b57 |
2886 | if (anIt == 1) |
2887 | { |
93cdaa76 |
2888 | aFsaaOffset.x() *= -0.55f; |
2889 | aFsaaOffset.y() *= 0.55f; |
91c60b57 |
2890 | } |
2891 | else if (anIt == 2) |
2892 | { |
93cdaa76 |
2893 | aFsaaOffset.x() *= 0.00f; |
2894 | aFsaaOffset.y() *= -0.55f; |
91c60b57 |
2895 | } |
2896 | else if (anIt == 3) |
2897 | { |
93cdaa76 |
2898 | aFsaaOffset.x() *= 0.55f; |
2899 | aFsaaOffset.y() *= 0.00f; |
91c60b57 |
2900 | } |
fc73a202 |
2901 | |
91c60b57 |
2902 | aResult &= myPostFSAAProgram->SetUniform (theGlContext, |
2903 | myUniformLocations[1][OpenGl_RT_uSamples], anIt + 1); |
2904 | aResult &= myPostFSAAProgram->SetUniform (theGlContext, |
93cdaa76 |
2905 | myUniformLocations[1][OpenGl_RT_uFsaaOffset], aFsaaOffset); |
fc73a202 |
2906 | |
3a9b5dc8 |
2907 | Handle(OpenGl_FrameBuffer)& aFramebuffer = anIt % 2 |
2908 | ? myRaytraceFBO2[aFBOIdx] |
2909 | : myRaytraceFBO1[aFBOIdx]; |
fc73a202 |
2910 | |
d9e72440 |
2911 | aFramebuffer->BindBuffer (theGlContext); |
50d0e1ce |
2912 | |
3a9b5dc8 |
2913 | // perform adaptive FSAA pass |
91c60b57 |
2914 | theGlContext->core20fwd->glDrawArrays (GL_TRIANGLES, 0, 6); |
e276548b |
2915 | |
cc8cbabe |
2916 | aFramebuffer->ColorTexture()->Bind (theGlContext, OpenGl_RT_FsaaInputTexture); |
e276548b |
2917 | } |
d9e72440 |
2918 | |
66d1cdc6 |
2919 | const Handle(OpenGl_FrameBuffer)& aRenderImageFramebuffer = myRaytraceFBO2[aFBOIdx]; |
2920 | const Handle(OpenGl_FrameBuffer)& aDepthSourceFramebuffer = myRaytraceFBO1[aFBOIdx]; |
d9e72440 |
2921 | |
3a9b5dc8 |
2922 | glEnable (GL_DEPTH_TEST); |
d9e72440 |
2923 | |
3a9b5dc8 |
2924 | // Display filtered image |
d9e72440 |
2925 | theGlContext->BindProgram (myOutImageProgram); |
2926 | |
d9e72440 |
2927 | if (theReadDrawFbo != NULL) |
2928 | { |
2929 | theReadDrawFbo->BindBuffer (theGlContext); |
2930 | } |
2931 | else |
2932 | { |
3a9b5dc8 |
2933 | aRenderImageFramebuffer->UnbindBuffer (theGlContext); |
d9e72440 |
2934 | } |
2935 | |
cc8cbabe |
2936 | aRenderImageFramebuffer->ColorTexture() ->Bind (theGlContext, OpenGl_RT_PrevAccumTexture); |
2937 | aDepthSourceFramebuffer->DepthStencilTexture()->Bind (theGlContext, OpenGl_RT_RaytraceDepthTexture); |
d9e72440 |
2938 | |
3a9b5dc8 |
2939 | // copy the output image with depth values |
d9e72440 |
2940 | theGlContext->core20fwd->glDrawArrays (GL_TRIANGLES, 0, 6); |
2941 | |
cc8cbabe |
2942 | aDepthSourceFramebuffer->DepthStencilTexture()->Unbind (theGlContext, OpenGl_RT_RaytraceDepthTexture); |
2943 | aRenderImageFramebuffer->ColorTexture() ->Unbind (theGlContext, OpenGl_RT_PrevAccumTexture); |
e276548b |
2944 | } |
fc73a202 |
2945 | |
91c60b57 |
2946 | unbindRaytraceTextures (theGlContext); |
a89742cf |
2947 | |
91c60b57 |
2948 | theGlContext->BindProgram (NULL); |
fc73a202 |
2949 | |
91c60b57 |
2950 | return aResult; |
e276548b |
2951 | } |
2952 | |
3a9b5dc8 |
2953 | // ======================================================================= |
2954 | // function : runPathtrace |
2955 | // purpose : Runs path tracing shader |
2956 | // ======================================================================= |
4eaaf9d8 |
2957 | Standard_Boolean OpenGl_View::runPathtrace (const Standard_Integer theSizeX, |
2958 | const Standard_Integer theSizeY, |
2959 | const Graphic3d_Camera::Projection theProjection, |
3a9b5dc8 |
2960 | const Handle(OpenGl_Context)& theGlContext) |
2961 | { |
6e728f3b |
2962 | if (myToUpdateEnvironmentMap) // check whether the map was changed |
2963 | { |
2964 | myAccumFrames = myToUpdateEnvironmentMap = 0; |
2965 | } |
b27ab03d |
2966 | |
2967 | if (myRenderParams.CameraApertureRadius != myPrevCameraApertureRadius |
2968 | || myRenderParams.CameraFocalPlaneDist != myPrevCameraFocalPlaneDist) |
2969 | { |
b27ab03d |
2970 | myPrevCameraApertureRadius = myRenderParams.CameraApertureRadius; |
2971 | myPrevCameraFocalPlaneDist = myRenderParams.CameraFocalPlaneDist; |
b27ab03d |
2972 | myAccumFrames = 0; |
2973 | } |
2974 | |
2975 | // Choose proper set of frame buffers for stereo rendering |
66d1cdc6 |
2976 | const Standard_Integer aFBOIdx = (theProjection == Graphic3d_Camera::Projection_MonoRightEye) ? 1 : 0; |
6e728f3b |
2977 | |
3a9b5dc8 |
2978 | if (myRaytraceParameters.AdaptiveScreenSampling) |
2979 | { |
2980 | if (myAccumFrames == 0) |
2981 | { |
2982 | myTileSampler.Reset(); // reset tile sampler to its initial state |
3a9b5dc8 |
2983 | |
b27ab03d |
2984 | // Adaptive sampling is starting at the second frame |
e084dbbc |
2985 | if (myRaytraceParameters.AdaptiveScreenSamplingAtomic) |
2986 | { |
2987 | myTileSampler.UploadOffsets (theGlContext, myRaytraceTileOffsetsTexture[aFBOIdx], false); |
2988 | } |
2989 | else |
2990 | { |
2991 | myTileSampler.UploadSamples (theGlContext, myRaytraceTileSamplesTexture[aFBOIdx], false); |
2992 | } |
3a9b5dc8 |
2993 | |
66d1cdc6 |
2994 | #if !defined(GL_ES_VERSION_2_0) |
2995 | theGlContext->core44->glClearTexImage (myRaytraceOutputTexture[aFBOIdx]->TextureId(), 0, GL_RED, GL_FLOAT, NULL); |
2996 | #endif |
2997 | } |
b27ab03d |
2998 | |
66d1cdc6 |
2999 | // Clear adaptive screen sampling images |
3000 | #if !defined(GL_ES_VERSION_2_0) |
3001 | theGlContext->core44->glClearTexImage (myRaytraceVisualErrorTexture[aFBOIdx]->TextureId(), 0, GL_RED_INTEGER, GL_INT, NULL); |
3002 | #endif |
3003 | } |
3a9b5dc8 |
3004 | |
66d1cdc6 |
3005 | bindRaytraceTextures (theGlContext, aFBOIdx); |
3a9b5dc8 |
3006 | |
66d1cdc6 |
3007 | const Handle(OpenGl_FrameBuffer)& anAccumImageFramebuffer = myAccumFrames % 2 ? myRaytraceFBO2[aFBOIdx] : myRaytraceFBO1[aFBOIdx]; |
cc8cbabe |
3008 | anAccumImageFramebuffer->ColorTexture()->Bind (theGlContext, OpenGl_RT_PrevAccumTexture); |
3a9b5dc8 |
3009 | |
66d1cdc6 |
3010 | // Set frame accumulation weight |
3011 | myRaytraceProgram->SetUniform (theGlContext, myUniformLocations[0][OpenGl_RT_uAccumSamples], myAccumFrames); |
3a9b5dc8 |
3012 | |
66d1cdc6 |
3013 | // Set image uniforms for render program |
3a9b5dc8 |
3014 | if (myRaytraceParameters.AdaptiveScreenSampling) |
3015 | { |
66d1cdc6 |
3016 | myRaytraceProgram->SetUniform (theGlContext, myUniformLocations[0][OpenGl_RT_uRenderImage], OpenGl_RT_OutputImage); |
e084dbbc |
3017 | myRaytraceProgram->SetUniform (theGlContext, myUniformLocations[0][OpenGl_RT_uTilesImage], OpenGl_RT_TileSamplesImage); |
66d1cdc6 |
3018 | myRaytraceProgram->SetUniform (theGlContext, myUniformLocations[0][OpenGl_RT_uOffsetImage], OpenGl_RT_TileOffsetsImage); |
3019 | myRaytraceProgram->SetUniform (theGlContext, myUniformLocations[0][OpenGl_RT_uTileSize], myTileSampler.TileSize()); |
3a9b5dc8 |
3020 | } |
3021 | |
66d1cdc6 |
3022 | const Handle(OpenGl_FrameBuffer)& aRenderImageFramebuffer = myAccumFrames % 2 ? myRaytraceFBO1[aFBOIdx] : myRaytraceFBO2[aFBOIdx]; |
3023 | aRenderImageFramebuffer->BindBuffer (theGlContext); |
e084dbbc |
3024 | if (myRaytraceParameters.AdaptiveScreenSampling |
3025 | && myRaytraceParameters.AdaptiveScreenSamplingAtomic) |
4eaaf9d8 |
3026 | { |
66d1cdc6 |
3027 | // extend viewport here, so that tiles at boundaries (cut tile size by target rendering viewport) |
3028 | // redirected to inner tiles (full tile size) are drawn entirely |
3029 | const Graphic3d_Vec2i anOffsetViewport = myTileSampler.OffsetTilesViewport (myAccumFrames > 1); // shrunk offsets texture will be uploaded since 3rd frame |
3030 | glViewport (0, 0, anOffsetViewport.x(), anOffsetViewport.y()); |
4eaaf9d8 |
3031 | } |
3032 | |
3a9b5dc8 |
3033 | // Generate for the given RNG seed |
66d1cdc6 |
3034 | glDisable (GL_DEPTH_TEST); |
3a9b5dc8 |
3035 | |
e084dbbc |
3036 | // Adaptive Screen Sampling computes the same overall amount of samples per frame redraw as normal Path Tracing, |
3037 | // but distributes them unequally across pixels (grouped in tiles), so that some pixels do not receive new samples at all. |
3038 | // |
3039 | // Offsets map (redirecting currently rendered tile to another tile) allows performing Adaptive Screen Sampling in single pass, |
3040 | // but current implementation relies on atomic float operations (AdaptiveScreenSamplingAtomic) for this. |
3041 | // So that when atomic floats are not supported by GPU, multi-pass rendering is used instead. |
3042 | // |
3043 | // Single-pass rendering is more optimal due to smaller amount of draw calls, |
3044 | // memory synchronization barriers, discarding most of the fragments and bad parallelization in case of very small amount of tiles requiring more samples. |
3045 | // However, atomic operations on float values still produces different result (close, but not bit exact) making non-regression testing not robust. |
3046 | // It should be possible following single-pass rendering approach but using extra accumulation buffer and resolving pass as possible improvement. |
3047 | const int aNbPasses = myRaytraceParameters.AdaptiveScreenSampling |
3048 | && !myRaytraceParameters.AdaptiveScreenSamplingAtomic |
3049 | ? myTileSampler.MaxTileSamples() |
3050 | : 1; |
3051 | if (myAccumFrames == 0) |
3052 | { |
3053 | myRNG.SetSeed(); // start RNG from beginning |
3054 | } |
3055 | for (int aPassIter = 0; aPassIter < aNbPasses; ++aPassIter) |
3056 | { |
3057 | myRaytraceProgram->SetUniform (theGlContext, myUniformLocations[0][OpenGl_RT_uFrameRndSeed], static_cast<Standard_Integer> (myRNG.NextInt() >> 2)); |
3058 | theGlContext->core20fwd->glDrawArrays (GL_TRIANGLES, 0, 6); |
3059 | if (myRaytraceParameters.AdaptiveScreenSampling) |
3060 | { |
3061 | #if !defined(GL_ES_VERSION_2_0) |
3062 | theGlContext->core44->glMemoryBarrier (GL_SHADER_IMAGE_ACCESS_BARRIER_BIT); |
3063 | #endif |
3064 | } |
3065 | } |
66d1cdc6 |
3066 | aRenderImageFramebuffer->UnbindBuffer (theGlContext); |
3067 | |
e084dbbc |
3068 | if (myRaytraceParameters.AdaptiveScreenSampling |
3069 | && myRaytraceParameters.AdaptiveScreenSamplingAtomic) |
4eaaf9d8 |
3070 | { |
66d1cdc6 |
3071 | glViewport (0, 0, theSizeX, theSizeY); |
4eaaf9d8 |
3072 | } |
66d1cdc6 |
3073 | return true; |
3074 | } |
4eaaf9d8 |
3075 | |
66d1cdc6 |
3076 | // ======================================================================= |
3077 | // function : runPathtraceOut |
3078 | // purpose : |
3079 | // ======================================================================= |
3080 | Standard_Boolean OpenGl_View::runPathtraceOut (const Graphic3d_Camera::Projection theProjection, |
3081 | OpenGl_FrameBuffer* theReadDrawFbo, |
3082 | const Handle(OpenGl_Context)& theGlContext) |
3083 | { |
3a9b5dc8 |
3084 | // Output accumulated path traced image |
3085 | theGlContext->BindProgram (myOutImageProgram); |
3086 | |
66d1cdc6 |
3087 | // Choose proper set of frame buffers for stereo rendering |
3088 | const Standard_Integer aFBOIdx = (theProjection == Graphic3d_Camera::Projection_MonoRightEye) ? 1 : 0; |
3089 | |
3a9b5dc8 |
3090 | if (myRaytraceParameters.AdaptiveScreenSampling) |
3091 | { |
3092 | // Set uniforms for display program |
66d1cdc6 |
3093 | myOutImageProgram->SetUniform (theGlContext, "uRenderImage", OpenGl_RT_OutputImage); |
3a9b5dc8 |
3094 | myOutImageProgram->SetUniform (theGlContext, "uAccumFrames", myAccumFrames); |
66d1cdc6 |
3095 | myOutImageProgram->SetUniform (theGlContext, "uVarianceImage", OpenGl_RT_VisualErrorImage); |
3a9b5dc8 |
3096 | myOutImageProgram->SetUniform (theGlContext, "uDebugAdaptive", myRenderParams.ShowSamplingTiles ? 1 : 0); |
66d1cdc6 |
3097 | myOutImageProgram->SetUniform (theGlContext, "uTileSize", myTileSampler.TileSize()); |
3098 | myOutImageProgram->SetUniform (theGlContext, "uVarianceScaleFactor", myTileSampler.VarianceScaleFactor()); |
3a9b5dc8 |
3099 | } |
3100 | |
eb85ed36 |
3101 | if (myRaytraceParameters.GlobalIllumination) |
3102 | { |
3103 | myOutImageProgram->SetUniform(theGlContext, "uExposure", myRenderParams.Exposure); |
3104 | switch (myRaytraceParameters.ToneMappingMethod) |
3105 | { |
3106 | case Graphic3d_ToneMappingMethod_Disabled: |
3107 | break; |
3108 | case Graphic3d_ToneMappingMethod_Filmic: |
3109 | myOutImageProgram->SetUniform (theGlContext, "uWhitePoint", myRenderParams.WhitePoint); |
3110 | break; |
3111 | } |
3112 | } |
3113 | |
3a9b5dc8 |
3114 | if (theReadDrawFbo != NULL) |
3115 | { |
3116 | theReadDrawFbo->BindBuffer (theGlContext); |
3117 | } |
3a9b5dc8 |
3118 | |
66d1cdc6 |
3119 | const Handle(OpenGl_FrameBuffer)& aRenderImageFramebuffer = myAccumFrames % 2 ? myRaytraceFBO1[aFBOIdx] : myRaytraceFBO2[aFBOIdx]; |
cc8cbabe |
3120 | aRenderImageFramebuffer->ColorTexture()->Bind (theGlContext, OpenGl_RT_PrevAccumTexture); |
3a9b5dc8 |
3121 | |
3a9b5dc8 |
3122 | // Copy accumulated image with correct depth values |
66d1cdc6 |
3123 | glEnable (GL_DEPTH_TEST); |
3a9b5dc8 |
3124 | theGlContext->core20fwd->glDrawArrays (GL_TRIANGLES, 0, 6); |
3125 | |
cc8cbabe |
3126 | aRenderImageFramebuffer->ColorTexture()->Unbind (theGlContext, OpenGl_RT_PrevAccumTexture); |
3a9b5dc8 |
3127 | |
3128 | if (myRaytraceParameters.AdaptiveScreenSampling) |
3129 | { |
66d1cdc6 |
3130 | // Download visual error map from the GPU and build adjusted tile offsets for optimal image sampling |
3131 | myTileSampler.GrabVarianceMap (theGlContext, myRaytraceVisualErrorTexture[aFBOIdx]); |
e084dbbc |
3132 | if (myRaytraceParameters.AdaptiveScreenSamplingAtomic) |
3133 | { |
3134 | myTileSampler.UploadOffsets (theGlContext, myRaytraceTileOffsetsTexture[aFBOIdx], myAccumFrames != 0); |
3135 | } |
3136 | else |
3137 | { |
3138 | myTileSampler.UploadSamples (theGlContext, myRaytraceTileSamplesTexture[aFBOIdx], myAccumFrames != 0); |
3139 | } |
3a9b5dc8 |
3140 | } |
3141 | |
3142 | unbindRaytraceTextures (theGlContext); |
3a9b5dc8 |
3143 | theGlContext->BindProgram (NULL); |
66d1cdc6 |
3144 | return true; |
3a9b5dc8 |
3145 | } |
3146 | |
e276548b |
3147 | // ======================================================================= |
189f85a3 |
3148 | // function : raytrace |
fc73a202 |
3149 | // purpose : Redraws the window using OpenGL/GLSL ray-tracing |
e276548b |
3150 | // ======================================================================= |
c357e426 |
3151 | Standard_Boolean OpenGl_View::raytrace (const Standard_Integer theSizeX, |
91c60b57 |
3152 | const Standard_Integer theSizeY, |
bf02aa7d |
3153 | Graphic3d_Camera::Projection theProjection, |
38a0206f |
3154 | OpenGl_FrameBuffer* theReadDrawFbo, |
91c60b57 |
3155 | const Handle(OpenGl_Context)& theGlContext) |
e276548b |
3156 | { |
66d1cdc6 |
3157 | if (!initRaytraceResources (theSizeX, theSizeY, theGlContext)) |
91c60b57 |
3158 | { |
e276548b |
3159 | return Standard_False; |
91c60b57 |
3160 | } |
3161 | |
bf02aa7d |
3162 | if (!updateRaytraceBuffers (theSizeX, theSizeY, theGlContext)) |
91c60b57 |
3163 | { |
3164 | return Standard_False; |
3165 | } |
e276548b |
3166 | |
3a9b5dc8 |
3167 | OpenGl_Mat4 aLightSourceMatrix; |
e276548b |
3168 | |
3a9b5dc8 |
3169 | // Get inversed model-view matrix for transforming lights |
3170 | myCamera->OrientationMatrixF().Inverted (aLightSourceMatrix); |
e276548b |
3171 | |
3a9b5dc8 |
3172 | if (!updateRaytraceLightSources (aLightSourceMatrix, theGlContext)) |
a89742cf |
3173 | { |
3a9b5dc8 |
3174 | return Standard_False; |
a89742cf |
3175 | } |
3176 | |
3a9b5dc8 |
3177 | // Generate image using Whitted-style ray-tracing or path tracing |
e276548b |
3178 | if (myIsRaytraceDataValid) |
3179 | { |
189f85a3 |
3180 | myRaytraceScreenQuad.BindVertexAttrib (theGlContext, Graphic3d_TOA_POS); |
fc73a202 |
3181 | |
91c60b57 |
3182 | if (!myRaytraceGeometry.AcquireTextures (theGlContext)) |
25ef750e |
3183 | { |
3b523c4c |
3184 | theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, |
3185 | 0, GL_DEBUG_SEVERITY_MEDIUM, "Error: Failed to acquire OpenGL image textures"); |
25ef750e |
3186 | } |
3187 | |
1d865689 |
3188 | glDisable (GL_BLEND); |
3a9b5dc8 |
3189 | |
3190 | const Standard_Boolean aResult = runRaytraceShaders (theSizeX, |
3191 | theSizeY, |
3192 | theProjection, |
3193 | theReadDrawFbo, |
3194 | theGlContext); |
1d865689 |
3195 | |
91c60b57 |
3196 | if (!aResult) |
25ef750e |
3197 | { |
3b523c4c |
3198 | theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, |
3199 | 0, GL_DEBUG_SEVERITY_MEDIUM, "Error: Failed to execute ray-tracing shaders"); |
91c60b57 |
3200 | } |
25ef750e |
3201 | |
91c60b57 |
3202 | if (!myRaytraceGeometry.ReleaseTextures (theGlContext)) |
3203 | { |
3b523c4c |
3204 | theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, |
3205 | 0, GL_DEBUG_SEVERITY_MEDIUM, "Error: Failed to release OpenGL image textures"); |
25ef750e |
3206 | } |
3207 | |
189f85a3 |
3208 | myRaytraceScreenQuad.UnbindVertexAttrib (theGlContext, Graphic3d_TOA_POS); |
e276548b |
3209 | } |
3210 | |
e276548b |
3211 | return Standard_True; |
f8ae3605 |
3212 | } |