1 // Created on: 2015-02-20
2 // Created by: Denis BOGOLEPOV
3 // Copyright (c) 2015 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <OpenGl_View.hxx>
18 #include <Graphic3d_TextureParams.hxx>
19 #include <OpenGl_PrimitiveArray.hxx>
20 #include <OpenGl_VertexBuffer.hxx>
21 #include <OSD_Protection.hxx>
22 #include <OSD_File.hxx>
24 using namespace OpenGl_Raytrace;
26 //! Use this macro to output ray-tracing debug info
27 // #define RAY_TRACE_PRINT_INFO
29 #ifdef RAY_TRACE_PRINT_INFO
30 #include <OSD_Timer.hxx>
33 // =======================================================================
34 // function : updateRaytraceGeometry
35 // purpose : Updates 3D scene geometry for ray-tracing
36 // =======================================================================
37 Standard_Boolean OpenGl_View::updateRaytraceGeometry (const RaytraceUpdateMode theMode,
38 const Standard_Integer theViewId,
39 const Handle(OpenGl_Context)& theGlContext)
41 // In 'check' mode (OpenGl_GUM_CHECK) the scene geometry is analyzed for
42 // modifications. This is light-weight procedure performed on each frame
43 if (theMode == OpenGl_GUM_CHECK)
45 if (myLayerListState != myZLayers.ModificationState())
47 return updateRaytraceGeometry (OpenGl_GUM_PREPARE, theViewId, theGlContext);
50 else if (theMode == OpenGl_GUM_PREPARE)
52 myRaytraceGeometry.ClearMaterials();
54 myArrayToTrianglesMap.clear();
56 myIsRaytraceDataValid = Standard_False;
59 // The set of processed structures (reflected to ray-tracing)
60 // This set is used to remove out-of-date records from the
61 // hash map of structures
62 std::set<const OpenGl_Structure*> anElements;
64 // Set to store all currently visible OpenGL primitive arrays
65 // applicable for ray-tracing
66 std::set<Standard_Size> anArrayIDs;
68 // Set to store all non-raytracable elements allowing tracking
69 // of changes in OpenGL scene (only for path tracing)
70 std::set<Standard_Integer> aNonRaytraceIDs;
72 const OpenGl_Layer& aLayer = myZLayers.Layer (Graphic3d_ZLayerId_Default);
74 if (aLayer.NbStructures() != 0)
76 const OpenGl_ArrayOfIndexedMapOfStructure& aStructArray = aLayer.ArrayOfStructures();
78 for (Standard_Integer anIndex = 0; anIndex < aStructArray.Length(); ++anIndex)
80 for (OpenGl_IndexedMapOfStructure::Iterator aStructIt (aStructArray (anIndex)); aStructIt.More(); aStructIt.Next())
82 const OpenGl_Structure* aStructure = aStructIt.Value();
84 if (theMode == OpenGl_GUM_CHECK)
86 if (toUpdateStructure (aStructure))
88 return updateRaytraceGeometry (OpenGl_GUM_PREPARE, theViewId, theGlContext);
90 else if (aStructure->IsVisible() && myRaytraceParameters.GlobalIllumination)
92 aNonRaytraceIDs.insert (aStructure->highlight ? aStructure->Id : -aStructure->Id);
95 else if (theMode == OpenGl_GUM_PREPARE)
97 if (!aStructure->IsRaytracable() || !aStructure->IsVisible())
101 else if (!aStructure->ViewAffinity.IsNull() && !aStructure->ViewAffinity->IsVisible (theViewId))
106 for (OpenGl_Structure::GroupIterator aGroupIter (aStructure->DrawGroups()); aGroupIter.More(); aGroupIter.Next())
108 // Extract OpenGL elements from the group (primitives arrays)
109 for (const OpenGl_ElementNode* aNode = aGroupIter.Value()->FirstNode(); aNode != NULL; aNode = aNode->next)
111 OpenGl_PrimitiveArray* aPrimArray = dynamic_cast<OpenGl_PrimitiveArray*> (aNode->elem);
113 if (aPrimArray != NULL)
115 anArrayIDs.insert (aPrimArray->GetUID());
120 else if (theMode == OpenGl_GUM_REBUILD)
122 if (!aStructure->IsRaytracable())
126 else if (addRaytraceStructure (aStructure, theGlContext))
128 anElements.insert (aStructure); // structure was processed
135 if (theMode == OpenGl_GUM_PREPARE)
137 BVH_ObjectSet<Standard_ShortReal, 3>::BVH_ObjectList anUnchangedObjects;
139 // Filter out unchanged objects so only their transformations and materials
140 // will be updated (and newly added objects will be processed from scratch)
141 for (Standard_Integer anObjIdx = 0; anObjIdx < myRaytraceGeometry.Size(); ++anObjIdx)
143 OpenGl_TriangleSet* aTriangleSet = dynamic_cast<OpenGl_TriangleSet*> (
144 myRaytraceGeometry.Objects().ChangeValue (anObjIdx).operator->());
146 if (aTriangleSet == NULL)
151 if (anArrayIDs.find (aTriangleSet->AssociatedPArrayID()) != anArrayIDs.end())
153 anUnchangedObjects.Append (myRaytraceGeometry.Objects().Value (anObjIdx));
155 myArrayToTrianglesMap[aTriangleSet->AssociatedPArrayID()] = aTriangleSet;
159 myRaytraceGeometry.Objects() = anUnchangedObjects;
161 return updateRaytraceGeometry (OpenGl_GUM_REBUILD, theViewId, theGlContext);
163 else if (theMode == OpenGl_GUM_REBUILD)
165 // Actualize the hash map of structures - remove out-of-date records
166 std::map<const OpenGl_Structure*, StructState>::iterator anIter = myStructureStates.begin();
168 while (anIter != myStructureStates.end())
170 if (anElements.find (anIter->first) == anElements.end())
172 myStructureStates.erase (anIter++);
180 // Actualize OpenGL layer list state
181 myLayerListState = myZLayers.ModificationState();
183 // Rebuild two-level acceleration structure
184 myRaytraceGeometry.ProcessAcceleration();
186 myRaytraceSceneRadius = 2.f /* scale factor */ * std::max (
187 myRaytraceGeometry.Box().CornerMin().cwiseAbs().maxComp(),
188 myRaytraceGeometry.Box().CornerMax().cwiseAbs().maxComp());
190 const BVH_Vec3f aSize = myRaytraceGeometry.Box().Size();
192 myRaytraceSceneEpsilon = Max (1.0e-6f, 1.0e-4f * aSize.Modulus());
194 return uploadRaytraceData (theGlContext);
197 if (myRaytraceParameters.GlobalIllumination)
199 Standard_Boolean toRestart =
200 aNonRaytraceIDs.size() != myNonRaytraceStructureIDs.size();
202 for (std::set<Standard_Integer>::iterator anID = aNonRaytraceIDs.begin(); anID != aNonRaytraceIDs.end() && !toRestart; ++anID)
204 if (myNonRaytraceStructureIDs.find (*anID) == myNonRaytraceStructureIDs.end())
206 toRestart = Standard_True;
213 myNonRaytraceStructureIDs = aNonRaytraceIDs;
216 return Standard_True;
219 // =======================================================================
220 // function : toUpdateStructure
221 // purpose : Checks to see if the structure is modified
222 // =======================================================================
223 Standard_Boolean OpenGl_View::toUpdateStructure (const OpenGl_Structure* theStructure)
225 if (!theStructure->IsRaytracable())
227 if (theStructure->ModificationState() > 0)
229 theStructure->ResetModificationState();
231 return Standard_True; // ray-trace element was removed - need to rebuild
234 return Standard_False; // did not contain ray-trace elements
237 std::map<const OpenGl_Structure*, StructState>::iterator aStructState = myStructureStates.find (theStructure);
239 if (aStructState == myStructureStates.end() || aStructState->second.StructureState != theStructure->ModificationState())
241 return Standard_True;
243 else if (theStructure->InstancedStructure() != NULL)
245 return aStructState->second.InstancedState != theStructure->InstancedStructure()->ModificationState();
248 return Standard_False;
251 // =======================================================================
252 // function : buildTextureTransform
253 // purpose : Constructs texture transformation matrix
254 // =======================================================================
255 void buildTextureTransform (const Handle(Graphic3d_TextureParams)& theParams, BVH_Mat4f& theMatrix)
257 theMatrix.InitIdentity();
260 const Graphic3d_Vec2& aScale = theParams->Scale();
262 theMatrix.ChangeValue (0, 0) *= aScale.x();
263 theMatrix.ChangeValue (1, 0) *= aScale.x();
264 theMatrix.ChangeValue (2, 0) *= aScale.x();
265 theMatrix.ChangeValue (3, 0) *= aScale.x();
267 theMatrix.ChangeValue (0, 1) *= aScale.y();
268 theMatrix.ChangeValue (1, 1) *= aScale.y();
269 theMatrix.ChangeValue (2, 1) *= aScale.y();
270 theMatrix.ChangeValue (3, 1) *= aScale.y();
273 const Graphic3d_Vec2 aTrans = -theParams->Translation();
275 theMatrix.ChangeValue (0, 3) = theMatrix.GetValue (0, 0) * aTrans.x() +
276 theMatrix.GetValue (0, 1) * aTrans.y();
278 theMatrix.ChangeValue (1, 3) = theMatrix.GetValue (1, 0) * aTrans.x() +
279 theMatrix.GetValue (1, 1) * aTrans.y();
281 theMatrix.ChangeValue (2, 3) = theMatrix.GetValue (2, 0) * aTrans.x() +
282 theMatrix.GetValue (2, 1) * aTrans.y();
285 const Standard_ShortReal aSin = std::sin (
286 -theParams->Rotation() * static_cast<Standard_ShortReal> (M_PI / 180.0));
287 const Standard_ShortReal aCos = std::cos (
288 -theParams->Rotation() * static_cast<Standard_ShortReal> (M_PI / 180.0));
290 BVH_Mat4f aRotationMat;
291 aRotationMat.SetValue (0, 0, aCos);
292 aRotationMat.SetValue (1, 1, aCos);
293 aRotationMat.SetValue (0, 1, -aSin);
294 aRotationMat.SetValue (1, 0, aSin);
296 theMatrix = theMatrix * aRotationMat;
299 // =======================================================================
300 // function : convertMaterial
301 // purpose : Creates ray-tracing material properties
302 // =======================================================================
303 OpenGl_RaytraceMaterial OpenGl_View::convertMaterial (const OpenGl_AspectFace* theAspect,
304 const Handle(OpenGl_Context)& theGlContext)
306 OpenGl_RaytraceMaterial theMaterial;
308 const OPENGL_SURF_PROP& aProperties = theAspect->IntFront();
310 theMaterial.Ambient = BVH_Vec4f (
311 (aProperties.isphysic ? aProperties.ambcol.rgb[0] : aProperties.matcol.rgb[0]) * aProperties.amb,
312 (aProperties.isphysic ? aProperties.ambcol.rgb[1] : aProperties.matcol.rgb[1]) * aProperties.amb,
313 (aProperties.isphysic ? aProperties.ambcol.rgb[2] : aProperties.matcol.rgb[2]) * aProperties.amb,
316 theMaterial.Diffuse = BVH_Vec4f (
317 (aProperties.isphysic ? aProperties.difcol.rgb[0] : aProperties.matcol.rgb[0]) * aProperties.diff,
318 (aProperties.isphysic ? aProperties.difcol.rgb[1] : aProperties.matcol.rgb[1]) * aProperties.diff,
319 (aProperties.isphysic ? aProperties.difcol.rgb[2] : aProperties.matcol.rgb[2]) * aProperties.diff,
320 -1.f /* no texture */);
322 theMaterial.Specular = BVH_Vec4f (
323 (aProperties.isphysic ? aProperties.speccol.rgb[0] : 1.f) * aProperties.spec,
324 (aProperties.isphysic ? aProperties.speccol.rgb[1] : 1.f) * aProperties.spec,
325 (aProperties.isphysic ? aProperties.speccol.rgb[2] : 1.f) * aProperties.spec,
328 theMaterial.Emission = BVH_Vec4f (
329 (aProperties.isphysic ? aProperties.emscol.rgb[0] : aProperties.matcol.rgb[0]) * aProperties.emsv,
330 (aProperties.isphysic ? aProperties.emscol.rgb[1] : aProperties.matcol.rgb[1]) * aProperties.emsv,
331 (aProperties.isphysic ? aProperties.emscol.rgb[2] : aProperties.matcol.rgb[2]) * aProperties.emsv,
334 theMaterial.Transparency = BVH_Vec4f (aProperties.trans,
335 1.f - aProperties.trans,
336 aProperties.index == 0 ? 1.f : aProperties.index,
337 aProperties.index == 0 ? 1.f : 1.f / aProperties.index);
339 const Standard_ShortReal aMaxRefl = Max (theMaterial.Diffuse.x() + theMaterial.Specular.x(),
340 Max (theMaterial.Diffuse.y() + theMaterial.Specular.y(),
341 theMaterial.Diffuse.z() + theMaterial.Specular.z()));
343 const Standard_ShortReal aReflectionScale = 0.75f / aMaxRefl;
345 theMaterial.Reflection = BVH_Vec4f (
346 aProperties.speccol.rgb[0] * aProperties.spec * aReflectionScale,
347 aProperties.speccol.rgb[1] * aProperties.spec * aReflectionScale,
348 aProperties.speccol.rgb[2] * aProperties.spec * aReflectionScale,
351 // Serialize physically-based material properties
352 const Graphic3d_BSDF& aBSDF = aProperties.BSDF;
354 theMaterial.BSDF.Le = BVH_Vec4f (aBSDF.Le, 0.f);
355 theMaterial.BSDF.Kd = BVH_Vec4f (aBSDF.Kd, -1.f /* no tex */);
356 theMaterial.BSDF.Kr = BVH_Vec4f (aBSDF.Kr, 0.f);
357 theMaterial.BSDF.Kt = BVH_Vec4f (aBSDF.Kt, 0.f);
358 theMaterial.BSDF.Ks = BVH_Vec4f (aBSDF.Ks, aBSDF.Roughness);
360 theMaterial.BSDF.Fresnel = aBSDF.Fresnel.Serialize();
362 theMaterial.BSDF.Absorption = BVH_Vec4f (aBSDF.AbsorptionColor,
363 aBSDF.AbsorptionCoeff);
365 // Handle material textures
366 if (theAspect->DoTextureMap())
368 if (theGlContext->arbTexBindless != NULL)
370 buildTextureTransform (theAspect->TextureParams(), theMaterial.TextureTransform);
372 // write texture ID to diffuse w-component
373 theMaterial.Diffuse.w() = theMaterial.BSDF.Kd.w() =
374 static_cast<Standard_ShortReal> (myRaytraceGeometry.AddTexture (theAspect->TextureRes (theGlContext)));
376 else if (!myIsRaytraceWarnTextures)
378 const TCollection_ExtendedString aWarnMessage =
379 "Warning: texturing in Ray-Trace requires GL_ARB_bindless_texture extension which is missing. "
380 "Please try to update graphics card driver. At the moment textures will be ignored.";
382 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
383 GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_HIGH, aWarnMessage);
385 myIsRaytraceWarnTextures = Standard_True;
392 // =======================================================================
393 // function : addRaytraceStructure
394 // purpose : Adds OpenGL structure to ray-traced scene geometry
395 // =======================================================================
396 Standard_Boolean OpenGl_View::addRaytraceStructure (const OpenGl_Structure* theStructure,
397 const Handle(OpenGl_Context)& theGlContext)
399 if (!theStructure->IsVisible())
401 myStructureStates[theStructure] = StructState (theStructure);
403 return Standard_True;
406 // Get structure material
407 OpenGl_RaytraceMaterial aDefaultMaterial;
408 Standard_Boolean aResult = addRaytraceGroups (theStructure, aDefaultMaterial, &theStructure->Transformation, theGlContext);
410 // Process all connected OpenGL structures
411 const OpenGl_Structure* anInstanced = theStructure->InstancedStructure();
413 if (anInstanced != NULL && anInstanced->IsRaytracable())
415 aResult &= addRaytraceGroups (anInstanced, aDefaultMaterial, &theStructure->Transformation, theGlContext);
418 myStructureStates[theStructure] = StructState (theStructure);
423 // =======================================================================
424 // function : addRaytraceGroups
425 // purpose : Adds OpenGL groups to ray-traced scene geometry
426 // =======================================================================
427 Standard_Boolean OpenGl_View::addRaytraceGroups (const OpenGl_Structure* theStructure,
428 const OpenGl_RaytraceMaterial& theStructMat,
429 const Graphic3d_Mat4* theTransform,
430 const Handle(OpenGl_Context)& theGlContext)
432 for (OpenGl_Structure::GroupIterator aGroupIter (theStructure->DrawGroups()); aGroupIter.More(); aGroupIter.Next())
434 // Get group material
435 OpenGl_RaytraceMaterial aGroupMaterial;
436 if (aGroupIter.Value()->AspectFace() != NULL)
438 aGroupMaterial = convertMaterial (
439 aGroupIter.Value()->AspectFace(), theGlContext);
442 Standard_Integer aMatID = static_cast<Standard_Integer> (myRaytraceGeometry.Materials.size());
444 // Use group material if available, otherwise use structure material
445 myRaytraceGeometry.Materials.push_back (
446 aGroupIter.Value()->AspectFace() != NULL ? aGroupMaterial : theStructMat);
448 // Add OpenGL elements from group (extract primitives arrays and aspects)
449 for (const OpenGl_ElementNode* aNode = aGroupIter.Value()->FirstNode(); aNode != NULL; aNode = aNode->next)
451 OpenGl_AspectFace* anAspect = dynamic_cast<OpenGl_AspectFace*> (aNode->elem);
453 if (anAspect != NULL)
455 aMatID = static_cast<Standard_Integer> (myRaytraceGeometry.Materials.size());
457 OpenGl_RaytraceMaterial aMaterial = convertMaterial (anAspect, theGlContext);
459 myRaytraceGeometry.Materials.push_back (aMaterial);
463 OpenGl_PrimitiveArray* aPrimArray = dynamic_cast<OpenGl_PrimitiveArray*> (aNode->elem);
465 if (aPrimArray != NULL)
467 std::map<Standard_Size, OpenGl_TriangleSet*>::iterator aSetIter = myArrayToTrianglesMap.find (aPrimArray->GetUID());
469 if (aSetIter != myArrayToTrianglesMap.end())
471 OpenGl_TriangleSet* aSet = aSetIter->second;
473 BVH_Transform<Standard_ShortReal, 4>* aTransform = new BVH_Transform<Standard_ShortReal, 4>();
475 if (theTransform != NULL)
477 aTransform->SetTransform (*theTransform);
480 aSet->SetProperties (aTransform);
482 if (aSet->MaterialIndex() != OpenGl_TriangleSet::INVALID_MATERIAL && aSet->MaterialIndex() != aMatID)
484 aSet->SetMaterialIndex (aMatID);
489 NCollection_Handle<BVH_Object<Standard_ShortReal, 3> > aSet =
490 addRaytracePrimitiveArray (aPrimArray, aMatID, 0);
494 BVH_Transform<Standard_ShortReal, 4>* aTransform = new BVH_Transform<Standard_ShortReal, 4>;
496 if (theTransform != NULL)
498 aTransform->SetTransform (*theTransform);
501 aSet->SetProperties (aTransform);
503 myRaytraceGeometry.Objects().Append (aSet);
511 return Standard_True;
514 // =======================================================================
515 // function : addRaytracePrimitiveArray
516 // purpose : Adds OpenGL primitive array to ray-traced scene geometry
517 // =======================================================================
518 OpenGl_TriangleSet* OpenGl_View::addRaytracePrimitiveArray (const OpenGl_PrimitiveArray* theArray,
519 const Standard_Integer theMaterial,
520 const OpenGl_Mat4* theTransform)
522 const Handle(Graphic3d_BoundBuffer)& aBounds = theArray->Bounds();
523 const Handle(Graphic3d_IndexBuffer)& anIndices = theArray->Indices();
524 const Handle(Graphic3d_Buffer)& anAttribs = theArray->Attributes();
526 if (theArray->DrawMode() < GL_TRIANGLES
527 #ifndef GL_ES_VERSION_2_0
528 || theArray->DrawMode() > GL_POLYGON
530 || theArray->DrawMode() > GL_TRIANGLE_FAN
532 || anAttribs.IsNull())
537 OpenGl_Mat4 aNormalMatrix;
539 if (theTransform != NULL)
541 Standard_ASSERT_RETURN (theTransform->Inverted (aNormalMatrix),
542 "Error: Failed to compute normal transformation matrix", NULL);
544 aNormalMatrix.Transpose();
547 OpenGl_TriangleSet* aSet = new OpenGl_TriangleSet (theArray->GetUID());
549 aSet->Vertices.reserve (anAttribs->NbElements);
550 aSet->Normals.reserve (anAttribs->NbElements);
551 aSet->TexCrds.reserve (anAttribs->NbElements);
553 const size_t aVertFrom = aSet->Vertices.size();
555 for (Standard_Integer anAttribIter = 0; anAttribIter < anAttribs->NbAttributes; ++anAttribIter)
557 const Graphic3d_Attribute& anAttrib = anAttribs->Attribute (anAttribIter);
558 const size_t anOffset = anAttribs->AttributeOffset (anAttribIter);
559 if (anAttrib.Id == Graphic3d_TOA_POS)
561 if (anAttrib.DataType == Graphic3d_TOD_VEC3
562 || anAttrib.DataType == Graphic3d_TOD_VEC4)
564 for (Standard_Integer aVertIter = 0; aVertIter < anAttribs->NbElements; ++aVertIter)
566 aSet->Vertices.push_back (
567 *reinterpret_cast<const Graphic3d_Vec3*> (anAttribs->value (aVertIter) + anOffset));
570 else if (anAttrib.DataType == Graphic3d_TOD_VEC2)
572 for (Standard_Integer aVertIter = 0; aVertIter < anAttribs->NbElements; ++aVertIter)
574 const Standard_ShortReal* aCoords =
575 reinterpret_cast<const Standard_ShortReal*> (anAttribs->value (aVertIter) + anOffset);
577 aSet->Vertices.push_back (BVH_Vec3f (aCoords[0], aCoords[1], 0.0f));
581 else if (anAttrib.Id == Graphic3d_TOA_NORM)
583 if (anAttrib.DataType == Graphic3d_TOD_VEC3
584 || anAttrib.DataType == Graphic3d_TOD_VEC4)
586 for (Standard_Integer aVertIter = 0; aVertIter < anAttribs->NbElements; ++aVertIter)
588 aSet->Normals.push_back (
589 *reinterpret_cast<const Graphic3d_Vec3*> (anAttribs->value (aVertIter) + anOffset));
593 else if (anAttrib.Id == Graphic3d_TOA_UV)
595 if (anAttrib.DataType == Graphic3d_TOD_VEC2)
597 for (Standard_Integer aVertIter = 0; aVertIter < anAttribs->NbElements; ++aVertIter)
599 aSet->TexCrds.push_back (
600 *reinterpret_cast<const Graphic3d_Vec2*> (anAttribs->value (aVertIter) + anOffset));
606 if (aSet->Normals.size() != aSet->Vertices.size())
608 for (Standard_Integer aVertIter = 0; aVertIter < anAttribs->NbElements; ++aVertIter)
610 aSet->Normals.push_back (BVH_Vec3f());
614 if (aSet->TexCrds.size() != aSet->Vertices.size())
616 for (Standard_Integer aVertIter = 0; aVertIter < anAttribs->NbElements; ++aVertIter)
618 aSet->TexCrds.push_back (BVH_Vec2f());
622 if (theTransform != NULL)
624 for (size_t aVertIter = aVertFrom; aVertIter < aSet->Vertices.size(); ++aVertIter)
626 BVH_Vec3f& aVertex = aSet->Vertices[aVertIter];
628 BVH_Vec4f aTransVertex = *theTransform *
629 BVH_Vec4f (aVertex.x(), aVertex.y(), aVertex.z(), 1.f);
631 aVertex = BVH_Vec3f (aTransVertex.x(), aTransVertex.y(), aTransVertex.z());
633 for (size_t aVertIter = aVertFrom; aVertIter < aSet->Normals.size(); ++aVertIter)
635 BVH_Vec3f& aNormal = aSet->Normals[aVertIter];
637 BVH_Vec4f aTransNormal = aNormalMatrix *
638 BVH_Vec4f (aNormal.x(), aNormal.y(), aNormal.z(), 0.f);
640 aNormal = BVH_Vec3f (aTransNormal.x(), aTransNormal.y(), aTransNormal.z());
644 if (!aBounds.IsNull())
646 for (Standard_Integer aBound = 0, aBoundStart = 0; aBound < aBounds->NbBounds; ++aBound)
648 const Standard_Integer aVertNum = aBounds->Bounds[aBound];
650 if (!addRaytraceVertexIndices (*aSet, theMaterial, aVertNum, aBoundStart, *theArray))
656 aBoundStart += aVertNum;
661 const Standard_Integer aVertNum = !anIndices.IsNull() ? anIndices->NbElements : anAttribs->NbElements;
663 if (!addRaytraceVertexIndices (*aSet, theMaterial, aVertNum, 0, *theArray))
671 if (aSet->Size() != 0)
679 // =======================================================================
680 // function : addRaytraceVertexIndices
681 // purpose : Adds vertex indices to ray-traced scene geometry
682 // =======================================================================
683 Standard_Boolean OpenGl_View::addRaytraceVertexIndices (OpenGl_TriangleSet& theSet,
684 const Standard_Integer theMatID,
685 const Standard_Integer theCount,
686 const Standard_Integer theOffset,
687 const OpenGl_PrimitiveArray& theArray)
689 switch (theArray.DrawMode())
691 case GL_TRIANGLES: return addRaytraceTriangleArray (theSet, theMatID, theCount, theOffset, theArray.Indices());
692 case GL_TRIANGLE_FAN: return addRaytraceTriangleFanArray (theSet, theMatID, theCount, theOffset, theArray.Indices());
693 case GL_TRIANGLE_STRIP: return addRaytraceTriangleStripArray (theSet, theMatID, theCount, theOffset, theArray.Indices());
694 #if !defined(GL_ES_VERSION_2_0)
695 case GL_QUAD_STRIP: return addRaytraceQuadrangleStripArray (theSet, theMatID, theCount, theOffset, theArray.Indices());
696 case GL_QUADS: return addRaytraceQuadrangleArray (theSet, theMatID, theCount, theOffset, theArray.Indices());
697 case GL_POLYGON: return addRaytracePolygonArray (theSet, theMatID, theCount, theOffset, theArray.Indices());
701 return Standard_False;
704 // =======================================================================
705 // function : addRaytraceTriangleArray
706 // purpose : Adds OpenGL triangle array to ray-traced scene geometry
707 // =======================================================================
708 Standard_Boolean OpenGl_View::addRaytraceTriangleArray (OpenGl_TriangleSet& theSet,
709 const Standard_Integer theMatID,
710 const Standard_Integer theCount,
711 const Standard_Integer theOffset,
712 const Handle(Graphic3d_IndexBuffer)& theIndices)
716 return Standard_True;
719 theSet.Elements.reserve (theSet.Elements.size() + theCount / 3);
721 if (!theIndices.IsNull())
723 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; aVert += 3)
725 theSet.Elements.push_back (BVH_Vec4i (theIndices->Index (aVert + 0),
726 theIndices->Index (aVert + 1),
727 theIndices->Index (aVert + 2),
733 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; aVert += 3)
735 theSet.Elements.push_back (BVH_Vec4i (aVert + 0, aVert + 1, aVert + 2, theMatID));
739 return Standard_True;
742 // =======================================================================
743 // function : addRaytraceTriangleFanArray
744 // purpose : Adds OpenGL triangle fan array to ray-traced scene geometry
745 // =======================================================================
746 Standard_Boolean OpenGl_View::addRaytraceTriangleFanArray (OpenGl_TriangleSet& theSet,
747 const Standard_Integer theMatID,
748 const Standard_Integer theCount,
749 const Standard_Integer theOffset,
750 const Handle(Graphic3d_IndexBuffer)& theIndices)
754 return Standard_True;
757 theSet.Elements.reserve (theSet.Elements.size() + theCount - 2);
759 if (!theIndices.IsNull())
761 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; ++aVert)
763 theSet.Elements.push_back (BVH_Vec4i (theIndices->Index (theOffset),
764 theIndices->Index (aVert + 1),
765 theIndices->Index (aVert + 2),
771 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; ++aVert)
773 theSet.Elements.push_back (BVH_Vec4i (theOffset,
780 return Standard_True;
783 // =======================================================================
784 // function : addRaytraceTriangleStripArray
785 // purpose : Adds OpenGL triangle strip array to ray-traced scene geometry
786 // =======================================================================
787 Standard_Boolean OpenGl_View::addRaytraceTriangleStripArray (OpenGl_TriangleSet& theSet,
788 const Standard_Integer theMatID,
789 const Standard_Integer theCount,
790 const Standard_Integer theOffset,
791 const Handle(Graphic3d_IndexBuffer)& theIndices)
795 return Standard_True;
798 theSet.Elements.reserve (theSet.Elements.size() + theCount - 2);
800 if (!theIndices.IsNull())
802 for (Standard_Integer aVert = theOffset, aCW = 0; aVert < theOffset + theCount - 2; ++aVert, aCW = (aCW + 1) % 2)
804 theSet.Elements.push_back (BVH_Vec4i (theIndices->Index (aVert + (aCW ? 1 : 0)),
805 theIndices->Index (aVert + (aCW ? 0 : 1)),
806 theIndices->Index (aVert + 2),
812 for (Standard_Integer aVert = theOffset, aCW = 0; aVert < theOffset + theCount - 2; ++aVert, aCW = (aCW + 1) % 2)
814 theSet.Elements.push_back (BVH_Vec4i (aVert + (aCW ? 1 : 0),
815 aVert + (aCW ? 0 : 1),
821 return Standard_True;
824 // =======================================================================
825 // function : addRaytraceQuadrangleArray
826 // purpose : Adds OpenGL quad array to ray-traced scene geometry
827 // =======================================================================
828 Standard_Boolean OpenGl_View::addRaytraceQuadrangleArray (OpenGl_TriangleSet& theSet,
829 const Standard_Integer theMatID,
830 const Standard_Integer theCount,
831 const Standard_Integer theOffset,
832 const Handle(Graphic3d_IndexBuffer)& theIndices)
836 return Standard_True;
839 theSet.Elements.reserve (theSet.Elements.size() + theCount / 2);
841 if (!theIndices.IsNull())
843 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 3; aVert += 4)
845 theSet.Elements.push_back (BVH_Vec4i (theIndices->Index (aVert + 0),
846 theIndices->Index (aVert + 1),
847 theIndices->Index (aVert + 2),
849 theSet.Elements.push_back (BVH_Vec4i (theIndices->Index (aVert + 0),
850 theIndices->Index (aVert + 2),
851 theIndices->Index (aVert + 3),
857 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 3; aVert += 4)
859 theSet.Elements.push_back (BVH_Vec4i (aVert + 0, aVert + 1, aVert + 2,
861 theSet.Elements.push_back (BVH_Vec4i (aVert + 0, aVert + 2, aVert + 3,
866 return Standard_True;
869 // =======================================================================
870 // function : addRaytraceQuadrangleStripArray
871 // purpose : Adds OpenGL quad strip array to ray-traced scene geometry
872 // =======================================================================
873 Standard_Boolean OpenGl_View::addRaytraceQuadrangleStripArray (OpenGl_TriangleSet& theSet,
874 const Standard_Integer theMatID,
875 const Standard_Integer theCount,
876 const Standard_Integer theOffset,
877 const Handle(Graphic3d_IndexBuffer)& theIndices)
881 return Standard_True;
884 theSet.Elements.reserve (theSet.Elements.size() + 2 * theCount - 6);
886 if (!theIndices.IsNull())
888 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 3; aVert += 2)
890 theSet.Elements.push_back (BVH_Vec4i (theIndices->Index (aVert + 0),
891 theIndices->Index (aVert + 1),
892 theIndices->Index (aVert + 2),
895 theSet.Elements.push_back (BVH_Vec4i (theIndices->Index (aVert + 1),
896 theIndices->Index (aVert + 3),
897 theIndices->Index (aVert + 2),
903 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 3; aVert += 2)
905 theSet.Elements.push_back (BVH_Vec4i (aVert + 0,
910 theSet.Elements.push_back (BVH_Vec4i (aVert + 1,
917 return Standard_True;
920 // =======================================================================
921 // function : addRaytracePolygonArray
922 // purpose : Adds OpenGL polygon array to ray-traced scene geometry
923 // =======================================================================
924 Standard_Boolean OpenGl_View::addRaytracePolygonArray (OpenGl_TriangleSet& theSet,
925 const Standard_Integer theMatID,
926 const Standard_Integer theCount,
927 const Standard_Integer theOffset,
928 const Handle(Graphic3d_IndexBuffer)& theIndices)
932 return Standard_True;
935 theSet.Elements.reserve (theSet.Elements.size() + theCount - 2);
937 if (!theIndices.IsNull())
939 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; ++aVert)
941 theSet.Elements.push_back (BVH_Vec4i (theIndices->Index (theOffset),
942 theIndices->Index (aVert + 1),
943 theIndices->Index (aVert + 2),
949 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; ++aVert)
951 theSet.Elements.push_back (BVH_Vec4i (theOffset,
958 return Standard_True;
961 const TCollection_AsciiString OpenGl_View::ShaderSource::EMPTY_PREFIX;
963 // =======================================================================
965 // purpose : Returns shader source combined with prefix
966 // =======================================================================
967 TCollection_AsciiString OpenGl_View::ShaderSource::Source() const
969 const TCollection_AsciiString aVersion = "#version 140";
971 if (myPrefix.IsEmpty())
973 return aVersion + "\n" + mySource;
976 return aVersion + "\n" + myPrefix + "\n" + mySource;
979 // =======================================================================
981 // purpose : Loads shader source from specified files
982 // =======================================================================
983 Standard_Boolean OpenGl_View::ShaderSource::Load (const TCollection_AsciiString* theFileNames,
984 const TCollection_AsciiString& thePrefix)
988 TCollection_AsciiString aMissingFiles;
989 for (Standard_Integer anIndex = 0; !theFileNames[anIndex].IsEmpty(); ++anIndex)
991 OSD_File aFile (theFileNames[anIndex]);
994 aFile.Open (OSD_ReadOnly, OSD_Protection());
998 if (!aMissingFiles.IsEmpty())
1000 aMissingFiles += ", ";
1002 aMissingFiles += TCollection_AsciiString("'") + theFileNames[anIndex] + "'";
1005 else if (!aMissingFiles.IsEmpty())
1011 TCollection_AsciiString aSource;
1012 aFile.Read (aSource, (Standard_Integer) aFile.Size());
1013 if (!aSource.IsEmpty())
1015 mySource += TCollection_AsciiString ("\n") + aSource;
1020 myPrefix = thePrefix;
1021 if (!aMissingFiles.IsEmpty())
1023 myError = TCollection_AsciiString("Shader files ") + aMissingFiles + " are missing or inaccessible";
1024 return Standard_False;
1026 return Standard_True;
1029 // =======================================================================
1030 // function : generateShaderPrefix
1031 // purpose : Generates shader prefix based on current ray-tracing options
1032 // =======================================================================
1033 TCollection_AsciiString OpenGl_View::generateShaderPrefix (const Handle(OpenGl_Context)& theGlContext) const
1035 TCollection_AsciiString aPrefixString =
1036 TCollection_AsciiString ("#define STACK_SIZE ") + TCollection_AsciiString (myRaytraceParameters.StackSize) + "\n" +
1037 TCollection_AsciiString ("#define NB_BOUNCES ") + TCollection_AsciiString (myRaytraceParameters.NbBounces);
1039 if (myRaytraceParameters.TransparentShadows)
1041 aPrefixString += TCollection_AsciiString ("\n#define TRANSPARENT_SHADOWS");
1044 // If OpenGL driver supports bindless textures and texturing
1045 // is actually used, activate texturing in ray-tracing mode
1046 if (myRaytraceParameters.UseBindlessTextures && theGlContext->arbTexBindless != NULL)
1048 aPrefixString += TCollection_AsciiString ("\n#define USE_TEXTURES") +
1049 TCollection_AsciiString ("\n#define MAX_TEX_NUMBER ") + TCollection_AsciiString (OpenGl_RaytraceGeometry::MAX_TEX_NUMBER);
1052 if (myRaytraceParameters.GlobalIllumination)
1054 aPrefixString += TCollection_AsciiString ("\n#define PATH_TRACING");
1057 return aPrefixString;
1060 // =======================================================================
1061 // function : safeFailBack
1062 // purpose : Performs safe exit when shaders initialization fails
1063 // =======================================================================
1064 Standard_Boolean OpenGl_View::safeFailBack (const TCollection_ExtendedString& theMessage,
1065 const Handle(OpenGl_Context)& theGlContext)
1067 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1068 GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, theMessage);
1070 myRaytraceInitStatus = OpenGl_RT_FAIL;
1072 releaseRaytraceResources (theGlContext);
1074 return Standard_False;
1077 // =======================================================================
1078 // function : initShader
1079 // purpose : Creates new shader object with specified source
1080 // =======================================================================
1081 Handle(OpenGl_ShaderObject) OpenGl_View::initShader (const GLenum theType,
1082 const ShaderSource& theSource,
1083 const Handle(OpenGl_Context)& theGlContext)
1085 Handle(OpenGl_ShaderObject) aShader = new OpenGl_ShaderObject (theType);
1087 if (!aShader->Create (theGlContext))
1089 const TCollection_ExtendedString aMessage = TCollection_ExtendedString ("Error: Failed to create ") +
1090 (theType == GL_VERTEX_SHADER ? "vertex" : "fragment") + " shader object";
1092 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1093 GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMessage);
1095 aShader->Release (theGlContext.operator->());
1097 return Handle(OpenGl_ShaderObject)();
1100 if (!aShader->LoadSource (theGlContext, theSource.Source()))
1102 const TCollection_ExtendedString aMessage = TCollection_ExtendedString ("Error: Failed to set ") +
1103 (theType == GL_VERTEX_SHADER ? "vertex" : "fragment") + " shader source";
1105 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1106 GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMessage);
1108 aShader->Release (theGlContext.operator->());
1110 return Handle(OpenGl_ShaderObject)();
1113 TCollection_AsciiString aBuildLog;
1115 if (!aShader->Compile (theGlContext))
1117 aShader->FetchInfoLog (theGlContext, aBuildLog);
1119 const TCollection_ExtendedString aMessage = TCollection_ExtendedString ("Error: Failed to compile ") +
1120 (theType == GL_VERTEX_SHADER ? "vertex" : "fragment") + " shader object:\n" + aBuildLog;
1122 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1123 GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMessage);
1125 aShader->Release (theGlContext.operator->());
1127 return Handle(OpenGl_ShaderObject)();
1129 else if (theGlContext->caps->glslWarnings)
1131 aShader->FetchInfoLog (theGlContext, aBuildLog);
1133 if (!aBuildLog.IsEmpty() && !aBuildLog.IsEqual ("No errors.\n"))
1135 const TCollection_ExtendedString aMessage = TCollection_ExtendedString (theType == GL_VERTEX_SHADER ?
1136 "Vertex" : "Fragment") + " shader was compiled with following warnings:\n" + aBuildLog;
1138 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1139 GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_LOW, aMessage);
1146 // =======================================================================
1147 // function : initProgram
1148 // purpose : Creates GLSL program from the given shader objects
1149 // =======================================================================
1150 Handle(OpenGl_ShaderProgram) OpenGl_View::initProgram (const Handle(OpenGl_Context)& theGlContext,
1151 const Handle(OpenGl_ShaderObject)& theVertShader,
1152 const Handle(OpenGl_ShaderObject)& theFragShader)
1154 Handle(OpenGl_ShaderProgram) aProgram = new OpenGl_ShaderProgram;
1156 if (!aProgram->Create (theGlContext))
1158 theVertShader->Release (theGlContext.operator->());
1160 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1161 GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, "Failed to create shader program");
1163 return Handle(OpenGl_ShaderProgram)();
1166 if (!aProgram->AttachShader (theGlContext, theVertShader)
1167 || !aProgram->AttachShader (theGlContext, theFragShader))
1169 theVertShader->Release (theGlContext.operator->());
1171 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1172 GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, "Failed to attach shader objects");
1174 return Handle(OpenGl_ShaderProgram)();
1177 aProgram->SetAttributeName (theGlContext, Graphic3d_TOA_POS, "occVertex");
1179 TCollection_AsciiString aLinkLog;
1181 if (!aProgram->Link (theGlContext))
1183 aProgram->FetchInfoLog (theGlContext, aLinkLog);
1185 const TCollection_ExtendedString aMessage = TCollection_ExtendedString (
1186 "Failed to link shader program:\n") + aLinkLog;
1188 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1189 GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMessage);
1191 return Handle(OpenGl_ShaderProgram)();
1193 else if (theGlContext->caps->glslWarnings)
1195 aProgram->FetchInfoLog (theGlContext, aLinkLog);
1196 if (!aLinkLog.IsEmpty() && !aLinkLog.IsEqual ("No errors.\n"))
1198 const TCollection_ExtendedString aMessage = TCollection_ExtendedString (
1199 "Shader program was linked with following warnings:\n") + aLinkLog;
1201 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1202 GL_DEBUG_TYPE_PORTABILITY, 0, GL_DEBUG_SEVERITY_LOW, aMessage);
1209 // =======================================================================
1210 // function : initRaytraceResources
1211 // purpose : Initializes OpenGL/GLSL shader programs
1212 // =======================================================================
1213 Standard_Boolean OpenGl_View::initRaytraceResources (const Handle(OpenGl_Context)& theGlContext)
1215 if (myRaytraceInitStatus == OpenGl_RT_FAIL)
1217 return Standard_False;
1220 Standard_Boolean aToRebuildShaders = Standard_False;
1222 if (myRaytraceInitStatus == OpenGl_RT_INIT)
1224 if (!myIsRaytraceDataValid)
1225 return Standard_True;
1227 const Standard_Integer aRequiredStackSize =
1228 myRaytraceGeometry.TopLevelTreeDepth() + myRaytraceGeometry.BotLevelTreeDepth();
1230 if (myRaytraceParameters.StackSize < aRequiredStackSize)
1232 myRaytraceParameters.StackSize = Max (aRequiredStackSize, THE_DEFAULT_STACK_SIZE);
1234 aToRebuildShaders = Standard_True;
1238 if (aRequiredStackSize < myRaytraceParameters.StackSize)
1240 if (myRaytraceParameters.StackSize > THE_DEFAULT_STACK_SIZE)
1242 myRaytraceParameters.StackSize = Max (aRequiredStackSize, THE_DEFAULT_STACK_SIZE);
1243 aToRebuildShaders = Standard_True;
1248 if (myRenderParams.RaytracingDepth != myRaytraceParameters.NbBounces)
1250 myRaytraceParameters.NbBounces = myRenderParams.RaytracingDepth;
1251 aToRebuildShaders = Standard_True;
1254 if (myRaytraceGeometry.HasTextures() != myRaytraceParameters.UseBindlessTextures)
1256 myRaytraceParameters.UseBindlessTextures = myRaytraceGeometry.HasTextures();
1257 aToRebuildShaders = Standard_True;
1260 if (myRenderParams.IsTransparentShadowEnabled != myRaytraceParameters.TransparentShadows)
1262 myRaytraceParameters.TransparentShadows = myRenderParams.IsTransparentShadowEnabled;
1263 aToRebuildShaders = Standard_True;
1266 if (myRenderParams.IsGlobalIlluminationEnabled != myRaytraceParameters.GlobalIllumination)
1268 myRaytraceParameters.GlobalIllumination = myRenderParams.IsGlobalIlluminationEnabled;
1269 aToRebuildShaders = Standard_True;
1272 if (aToRebuildShaders)
1274 // Reject accumulated frames
1277 // We need to update environment texture
1278 myToUpdateEnvironmentMap = Standard_True;
1280 TCollection_AsciiString aPrefixString = generateShaderPrefix (theGlContext);
1282 #ifdef RAY_TRACE_PRINT_INFO
1283 std::cout << "GLSL prefix string:" << std::endl << aPrefixString << std::endl;
1286 myRaytraceShaderSource.SetPrefix (aPrefixString);
1287 myPostFSAAShaderSource.SetPrefix (aPrefixString);
1289 if (!myRaytraceShader->LoadSource (theGlContext, myRaytraceShaderSource.Source())
1290 || !myPostFSAAShader->LoadSource (theGlContext, myPostFSAAShaderSource.Source()))
1292 return safeFailBack ("Failed to load source into ray-tracing fragment shaders", theGlContext);
1295 if (!myRaytraceShader->Compile (theGlContext)
1296 || !myPostFSAAShader->Compile (theGlContext))
1298 return safeFailBack ("Failed to compile ray-tracing fragment shaders", theGlContext);
1301 myRaytraceProgram->SetAttributeName (theGlContext, Graphic3d_TOA_POS, "occVertex");
1302 myPostFSAAProgram->SetAttributeName (theGlContext, Graphic3d_TOA_POS, "occVertex");
1303 if (!myRaytraceProgram->Link (theGlContext)
1304 || !myPostFSAAProgram->Link (theGlContext))
1306 return safeFailBack ("Failed to initialize vertex attributes for ray-tracing program", theGlContext);
1311 if (myRaytraceInitStatus == OpenGl_RT_NONE)
1313 if (!theGlContext->IsGlGreaterEqual (3, 1))
1315 return safeFailBack ("Ray-tracing requires OpenGL 3.1 and higher", theGlContext);
1317 else if (!theGlContext->arbTboRGB32)
1319 return safeFailBack ("Ray-tracing requires OpenGL 4.0+ or GL_ARB_texture_buffer_object_rgb32 extension", theGlContext);
1321 else if (!theGlContext->arbFBOBlit)
1323 return safeFailBack ("Ray-tracing requires EXT_framebuffer_blit extension", theGlContext);
1326 myRaytraceParameters.NbBounces = myRenderParams.RaytracingDepth;
1328 TCollection_AsciiString aFolder = Graphic3d_ShaderProgram::ShadersFolder();
1330 if (aFolder.IsEmpty())
1332 return safeFailBack ("Failed to locate shaders directory", theGlContext);
1335 if (myIsRaytraceDataValid)
1337 myRaytraceParameters.StackSize = Max (THE_DEFAULT_STACK_SIZE,
1338 myRaytraceGeometry.TopLevelTreeDepth() + myRaytraceGeometry.BotLevelTreeDepth());
1341 TCollection_AsciiString aPrefixString = generateShaderPrefix (theGlContext);
1343 #ifdef RAY_TRACE_PRINT_INFO
1344 std::cout << "GLSL prefix string:" << std::endl << aPrefixString << std::endl;
1347 ShaderSource aBasicVertShaderSrc;
1349 TCollection_AsciiString aFiles[] = { aFolder + "/RaytraceBase.vs", "" };
1350 if (!aBasicVertShaderSrc.Load (aFiles))
1352 return safeFailBack (aBasicVertShaderSrc.ErrorDescription(), theGlContext);
1357 TCollection_AsciiString aFiles[] = { aFolder + "/RaytraceBase.fs",
1358 aFolder + "/PathtraceBase.fs",
1359 aFolder + "/RaytraceRender.fs",
1361 if (!myRaytraceShaderSource.Load (aFiles, aPrefixString))
1363 return safeFailBack (myRaytraceShaderSource.ErrorDescription(), theGlContext);
1366 Handle(OpenGl_ShaderObject) aBasicVertShader = initShader (GL_VERTEX_SHADER, aBasicVertShaderSrc, theGlContext);
1367 if (aBasicVertShader.IsNull())
1369 return safeFailBack ("Failed to initialize ray-trace vertex shader", theGlContext);
1372 myRaytraceShader = initShader (GL_FRAGMENT_SHADER, myRaytraceShaderSource, theGlContext);
1373 if (myRaytraceShader.IsNull())
1375 aBasicVertShader->Release (theGlContext.operator->());
1376 return safeFailBack ("Failed to initialize ray-trace fragment shader", theGlContext);
1379 myRaytraceProgram = initProgram (theGlContext, aBasicVertShader, myRaytraceShader);
1380 if (myRaytraceProgram.IsNull())
1382 return safeFailBack ("Failed to initialize ray-trace shader program", theGlContext);
1387 TCollection_AsciiString aFiles[] = { aFolder + "/RaytraceBase.fs",
1388 aFolder + "/RaytraceSmooth.fs",
1390 if (!myPostFSAAShaderSource.Load (aFiles, aPrefixString))
1392 return safeFailBack (myPostFSAAShaderSource.ErrorDescription(), theGlContext);
1395 Handle(OpenGl_ShaderObject) aBasicVertShader = initShader (GL_VERTEX_SHADER, aBasicVertShaderSrc, theGlContext);
1396 if (aBasicVertShader.IsNull())
1398 return safeFailBack ("Failed to initialize FSAA vertex shader", theGlContext);
1401 myPostFSAAShader = initShader (GL_FRAGMENT_SHADER, myPostFSAAShaderSource, theGlContext);
1402 if (myPostFSAAShader.IsNull())
1404 aBasicVertShader->Release (theGlContext.operator->());
1405 return safeFailBack ("Failed to initialize FSAA fragment shader", theGlContext);
1408 myPostFSAAProgram = initProgram (theGlContext, aBasicVertShader, myPostFSAAShader);
1409 if (myPostFSAAProgram.IsNull())
1411 return safeFailBack ("Failed to initialize FSAA shader program", theGlContext);
1416 ShaderSource aDispShaderSrc;
1417 TCollection_AsciiString aFiles[] = { aFolder + "/Display.fs", "" };
1418 if (!aDispShaderSrc.Load (aFiles, aPrefixString))
1420 return safeFailBack (aDispShaderSrc.ErrorDescription(), theGlContext);
1423 Handle(OpenGl_ShaderObject) aBasicVertShader = initShader (GL_VERTEX_SHADER, aBasicVertShaderSrc, theGlContext);
1424 if (aBasicVertShader.IsNull())
1426 return safeFailBack ("Failed to set vertex shader source", theGlContext);
1429 Handle(OpenGl_ShaderObject) aDisplayShader = initShader (GL_FRAGMENT_SHADER, aDispShaderSrc, theGlContext);
1430 if (aDisplayShader.IsNull())
1432 aBasicVertShader->Release (theGlContext.operator->());
1433 return safeFailBack ("Failed to set display fragment shader source", theGlContext);
1436 myOutImageProgram = initProgram (theGlContext, aBasicVertShader, aDisplayShader);
1437 if (myOutImageProgram.IsNull())
1439 return safeFailBack ("Failed to initialize output shader program", theGlContext);
1444 if (myRaytraceInitStatus == OpenGl_RT_NONE || aToRebuildShaders)
1446 for (Standard_Integer anIndex = 0; anIndex < 2; ++anIndex)
1448 Handle(OpenGl_ShaderProgram)& aShaderProgram =
1449 (anIndex == 0) ? myRaytraceProgram : myPostFSAAProgram;
1451 theGlContext->BindProgram (aShaderProgram);
1453 aShaderProgram->SetSampler (theGlContext,
1454 "uSceneMinPointTexture", OpenGl_RT_SceneMinPointTexture);
1455 aShaderProgram->SetSampler (theGlContext,
1456 "uSceneMaxPointTexture", OpenGl_RT_SceneMaxPointTexture);
1457 aShaderProgram->SetSampler (theGlContext,
1458 "uSceneNodeInfoTexture", OpenGl_RT_SceneNodeInfoTexture);
1459 aShaderProgram->SetSampler (theGlContext,
1460 "uGeometryVertexTexture", OpenGl_RT_GeometryVertexTexture);
1461 aShaderProgram->SetSampler (theGlContext,
1462 "uGeometryNormalTexture", OpenGl_RT_GeometryNormalTexture);
1463 aShaderProgram->SetSampler (theGlContext,
1464 "uGeometryTexCrdTexture", OpenGl_RT_GeometryTexCrdTexture);
1465 aShaderProgram->SetSampler (theGlContext,
1466 "uGeometryTriangTexture", OpenGl_RT_GeometryTriangTexture);
1467 aShaderProgram->SetSampler (theGlContext,
1468 "uSceneTransformTexture", OpenGl_RT_SceneTransformTexture);
1469 aShaderProgram->SetSampler (theGlContext,
1470 "uEnvironmentMapTexture", OpenGl_RT_EnvironmentMapTexture);
1471 aShaderProgram->SetSampler (theGlContext,
1472 "uRaytraceMaterialTexture", OpenGl_RT_RaytraceMaterialTexture);
1473 aShaderProgram->SetSampler (theGlContext,
1474 "uRaytraceLightSrcTexture", OpenGl_RT_RaytraceLightSrcTexture);
1476 aShaderProgram->SetSampler (theGlContext,
1477 "uOpenGlColorTexture", OpenGl_RT_OpenGlColorTexture);
1478 aShaderProgram->SetSampler (theGlContext,
1479 "uOpenGlDepthTexture", OpenGl_RT_OpenGlDepthTexture);
1483 aShaderProgram->SetSampler (theGlContext,
1484 "uFSAAInputTexture", OpenGl_RT_FsaaInputTexture);
1488 aShaderProgram->SetSampler (theGlContext,
1489 "uAccumTexture", OpenGl_RT_PrevAccumTexture);
1492 myUniformLocations[anIndex][OpenGl_RT_aPosition] =
1493 aShaderProgram->GetAttributeLocation (theGlContext, "occVertex");
1495 myUniformLocations[anIndex][OpenGl_RT_uOriginLB] =
1496 aShaderProgram->GetUniformLocation (theGlContext, "uOriginLB");
1497 myUniformLocations[anIndex][OpenGl_RT_uOriginRB] =
1498 aShaderProgram->GetUniformLocation (theGlContext, "uOriginRB");
1499 myUniformLocations[anIndex][OpenGl_RT_uOriginLT] =
1500 aShaderProgram->GetUniformLocation (theGlContext, "uOriginLT");
1501 myUniformLocations[anIndex][OpenGl_RT_uOriginRT] =
1502 aShaderProgram->GetUniformLocation (theGlContext, "uOriginRT");
1503 myUniformLocations[anIndex][OpenGl_RT_uDirectLB] =
1504 aShaderProgram->GetUniformLocation (theGlContext, "uDirectLB");
1505 myUniformLocations[anIndex][OpenGl_RT_uDirectRB] =
1506 aShaderProgram->GetUniformLocation (theGlContext, "uDirectRB");
1507 myUniformLocations[anIndex][OpenGl_RT_uDirectLT] =
1508 aShaderProgram->GetUniformLocation (theGlContext, "uDirectLT");
1509 myUniformLocations[anIndex][OpenGl_RT_uDirectRT] =
1510 aShaderProgram->GetUniformLocation (theGlContext, "uDirectRT");
1511 myUniformLocations[anIndex][OpenGl_RT_uViewMat] =
1512 aShaderProgram->GetUniformLocation (theGlContext, "uViewMat");
1513 myUniformLocations[anIndex][OpenGl_RT_uUnviewMat] =
1514 aShaderProgram->GetUniformLocation (theGlContext, "uUnviewMat");
1516 myUniformLocations[anIndex][OpenGl_RT_uSceneRad] =
1517 aShaderProgram->GetUniformLocation (theGlContext, "uSceneRadius");
1518 myUniformLocations[anIndex][OpenGl_RT_uSceneEps] =
1519 aShaderProgram->GetUniformLocation (theGlContext, "uSceneEpsilon");
1520 myUniformLocations[anIndex][OpenGl_RT_uLightCount] =
1521 aShaderProgram->GetUniformLocation (theGlContext, "uLightCount");
1522 myUniformLocations[anIndex][OpenGl_RT_uLightAmbnt] =
1523 aShaderProgram->GetUniformLocation (theGlContext, "uGlobalAmbient");
1525 myUniformLocations[anIndex][OpenGl_RT_uOffsetX] =
1526 aShaderProgram->GetUniformLocation (theGlContext, "uOffsetX");
1527 myUniformLocations[anIndex][OpenGl_RT_uOffsetY] =
1528 aShaderProgram->GetUniformLocation (theGlContext, "uOffsetY");
1529 myUniformLocations[anIndex][OpenGl_RT_uSamples] =
1530 aShaderProgram->GetUniformLocation (theGlContext, "uSamples");
1532 myUniformLocations[anIndex][OpenGl_RT_uTexSamplersArray] =
1533 aShaderProgram->GetUniformLocation (theGlContext, "uTextureSamplers");
1535 myUniformLocations[anIndex][OpenGl_RT_uShadowsEnabled] =
1536 aShaderProgram->GetUniformLocation (theGlContext, "uShadowsEnabled");
1537 myUniformLocations[anIndex][OpenGl_RT_uReflectEnabled] =
1538 aShaderProgram->GetUniformLocation (theGlContext, "uReflectEnabled");
1539 myUniformLocations[anIndex][OpenGl_RT_uSphereMapEnabled] =
1540 aShaderProgram->GetUniformLocation (theGlContext, "uSphereMapEnabled");
1541 myUniformLocations[anIndex][OpenGl_RT_uSphereMapForBack] =
1542 aShaderProgram->GetUniformLocation (theGlContext, "uSphereMapForBack");
1543 myUniformLocations[anIndex][OpenGl_RT_uBlockedRngEnabled] =
1544 aShaderProgram->GetUniformLocation (theGlContext, "uBlockedRngEnabled");
1546 myUniformLocations[anIndex][OpenGl_RT_uSampleWeight] =
1547 aShaderProgram->GetUniformLocation (theGlContext, "uSampleWeight");
1548 myUniformLocations[anIndex][OpenGl_RT_uFrameRndSeed] =
1549 aShaderProgram->GetUniformLocation (theGlContext, "uFrameRndSeed");
1551 myUniformLocations[anIndex][OpenGl_RT_uBackColorTop] =
1552 aShaderProgram->GetUniformLocation (theGlContext, "uBackColorTop");
1553 myUniformLocations[anIndex][OpenGl_RT_uBackColorBot] =
1554 aShaderProgram->GetUniformLocation (theGlContext, "uBackColorBot");
1557 theGlContext->BindProgram (myOutImageProgram);
1559 myOutImageProgram->SetSampler (theGlContext,
1560 "uInputTexture", OpenGl_RT_PrevAccumTexture);
1562 myOutImageProgram->SetSampler (theGlContext,
1563 "uDepthTexture", OpenGl_RT_DepthTexture);
1565 theGlContext->BindProgram (NULL);
1568 if (myRaytraceInitStatus != OpenGl_RT_NONE)
1570 return myRaytraceInitStatus == OpenGl_RT_INIT;
1573 const GLfloat aVertices[] = { -1.f, -1.f, 0.f,
1580 myRaytraceScreenQuad.Init (theGlContext, 3, 6, aVertices);
1582 myRaytraceInitStatus = OpenGl_RT_INIT; // initialized in normal way
1584 return Standard_True;
1587 // =======================================================================
1588 // function : nullifyResource
1590 // =======================================================================
1592 inline void nullifyResource (const Handle(OpenGl_Context)& theGlContext,
1593 Handle(T)& theResource)
1595 if (!theResource.IsNull())
1597 theResource->Release (theGlContext.operator->());
1598 theResource.Nullify();
1602 // =======================================================================
1603 // function : releaseRaytraceResources
1604 // purpose : Releases OpenGL/GLSL shader programs
1605 // =======================================================================
1606 void OpenGl_View::releaseRaytraceResources (const Handle(OpenGl_Context)& theGlContext)
1608 myRaytraceFBO1[0]->Release (theGlContext.operator->());
1609 myRaytraceFBO1[1]->Release (theGlContext.operator->());
1610 myRaytraceFBO2[0]->Release (theGlContext.operator->());
1611 myRaytraceFBO2[1]->Release (theGlContext.operator->());
1613 nullifyResource (theGlContext, myRaytraceShader);
1614 nullifyResource (theGlContext, myPostFSAAShader);
1616 nullifyResource (theGlContext, myRaytraceProgram);
1617 nullifyResource (theGlContext, myPostFSAAProgram);
1618 nullifyResource (theGlContext, myOutImageProgram);
1620 nullifyResource (theGlContext, mySceneNodeInfoTexture);
1621 nullifyResource (theGlContext, mySceneMinPointTexture);
1622 nullifyResource (theGlContext, mySceneMaxPointTexture);
1624 nullifyResource (theGlContext, myGeometryVertexTexture);
1625 nullifyResource (theGlContext, myGeometryNormalTexture);
1626 nullifyResource (theGlContext, myGeometryTexCrdTexture);
1627 nullifyResource (theGlContext, myGeometryTriangTexture);
1628 nullifyResource (theGlContext, mySceneTransformTexture);
1630 nullifyResource (theGlContext, myRaytraceLightSrcTexture);
1631 nullifyResource (theGlContext, myRaytraceMaterialTexture);
1633 myRaytraceGeometry.ReleaseResources (theGlContext);
1635 if (myRaytraceScreenQuad.IsValid())
1636 myRaytraceScreenQuad.Release (theGlContext.operator->());
1639 // =======================================================================
1640 // function : updateRaytraceBuffers
1641 // purpose : Updates auxiliary OpenGL frame buffers.
1642 // =======================================================================
1643 Standard_Boolean OpenGl_View::updateRaytraceBuffers (const Standard_Integer theSizeX,
1644 const Standard_Integer theSizeY,
1645 const Handle(OpenGl_Context)& theGlContext)
1647 // Auxiliary buffers are not used.
1648 if (!myRaytraceParameters.GlobalIllumination && !myRenderParams.IsAntialiasingEnabled)
1650 myRaytraceFBO1[0]->Release (theGlContext.operator->());
1651 myRaytraceFBO2[0]->Release (theGlContext.operator->());
1652 myRaytraceFBO1[1]->Release (theGlContext.operator->());
1653 myRaytraceFBO2[1]->Release (theGlContext.operator->());
1654 return Standard_True;
1657 myRaytraceFBO1[0]->InitLazy (theGlContext, theSizeX, theSizeY, GL_RGBA32F, myFboDepthFormat);
1658 myRaytraceFBO2[0]->InitLazy (theGlContext, theSizeX, theSizeY, GL_RGBA32F, myFboDepthFormat);
1660 // Init second set of buffers for stereographic rendering.
1661 if (myCamera->ProjectionType() == Graphic3d_Camera::Projection_Stereo)
1663 myRaytraceFBO1[1]->InitLazy (theGlContext, theSizeX, theSizeY, GL_RGBA32F, myFboDepthFormat);
1664 myRaytraceFBO2[1]->InitLazy (theGlContext, theSizeX, theSizeY, GL_RGBA32F, myFboDepthFormat);
1668 myRaytraceFBO1[1]->Release (theGlContext.operator->());
1669 myRaytraceFBO2[1]->Release (theGlContext.operator->());
1672 return Standard_True;
1675 // =======================================================================
1676 // function : updateCamera
1677 // purpose : Generates viewing rays for corners of screen quad
1678 // =======================================================================
1679 void OpenGl_View::updateCamera (const OpenGl_Mat4& theOrientation,
1680 const OpenGl_Mat4& theViewMapping,
1681 OpenGl_Vec3* theOrigins,
1682 OpenGl_Vec3* theDirects,
1683 OpenGl_Mat4& theView,
1684 OpenGl_Mat4& theUnview)
1686 // compute view-projection matrix
1687 theView = theViewMapping * theOrientation;
1689 // compute inverse view-projection matrix
1690 theView.Inverted (theUnview);
1692 Standard_Integer aOriginIndex = 0;
1693 Standard_Integer aDirectIndex = 0;
1695 for (Standard_Integer aY = -1; aY <= 1; aY += 2)
1697 for (Standard_Integer aX = -1; aX <= 1; aX += 2)
1699 OpenGl_Vec4 aOrigin (GLfloat(aX),
1704 aOrigin = theUnview * aOrigin;
1706 aOrigin.x() = aOrigin.x() / aOrigin.w();
1707 aOrigin.y() = aOrigin.y() / aOrigin.w();
1708 aOrigin.z() = aOrigin.z() / aOrigin.w();
1710 OpenGl_Vec4 aDirect (GLfloat(aX),
1715 aDirect = theUnview * aDirect;
1717 aDirect.x() = aDirect.x() / aDirect.w();
1718 aDirect.y() = aDirect.y() / aDirect.w();
1719 aDirect.z() = aDirect.z() / aDirect.w();
1721 aDirect = aDirect - aOrigin;
1723 theOrigins[aOriginIndex++] = OpenGl_Vec3 (static_cast<GLfloat> (aOrigin.x()),
1724 static_cast<GLfloat> (aOrigin.y()),
1725 static_cast<GLfloat> (aOrigin.z()));
1727 theDirects[aDirectIndex++] = OpenGl_Vec3 (static_cast<GLfloat> (aDirect.x()),
1728 static_cast<GLfloat> (aDirect.y()),
1729 static_cast<GLfloat> (aDirect.z()));
1734 // =======================================================================
1735 // function : uploadRaytraceData
1736 // purpose : Uploads ray-trace data to the GPU
1737 // =======================================================================
1738 Standard_Boolean OpenGl_View::uploadRaytraceData (const Handle(OpenGl_Context)& theGlContext)
1740 if (!theGlContext->IsGlGreaterEqual (3, 1))
1742 #ifdef RAY_TRACE_PRINT_INFO
1743 std::cout << "Error: OpenGL version is less than 3.1" << std::endl;
1745 return Standard_False;
1748 myAccumFrames = 0; // accumulation should be restarted
1750 /////////////////////////////////////////////////////////////////////////////
1751 // Prepare OpenGL textures
1753 if (theGlContext->arbTexBindless != NULL)
1755 // If OpenGL driver supports bindless textures we need
1756 // to get unique 64- bit handles for using on the GPU
1757 if (!myRaytraceGeometry.UpdateTextureHandles (theGlContext))
1759 #ifdef RAY_TRACE_PRINT_INFO
1760 std::cout << "Error: Failed to get OpenGL texture handles" << std::endl;
1762 return Standard_False;
1766 /////////////////////////////////////////////////////////////////////////////
1767 // Create OpenGL BVH buffers
1769 if (mySceneNodeInfoTexture.IsNull()) // create scene BVH buffers
1771 mySceneNodeInfoTexture = new OpenGl_TextureBufferArb;
1772 mySceneMinPointTexture = new OpenGl_TextureBufferArb;
1773 mySceneMaxPointTexture = new OpenGl_TextureBufferArb;
1774 mySceneTransformTexture = new OpenGl_TextureBufferArb;
1776 if (!mySceneNodeInfoTexture->Create (theGlContext)
1777 || !mySceneMinPointTexture->Create (theGlContext)
1778 || !mySceneMaxPointTexture->Create (theGlContext)
1779 || !mySceneTransformTexture->Create (theGlContext))
1781 #ifdef RAY_TRACE_PRINT_INFO
1782 std::cout << "Error: Failed to create scene BVH buffers" << std::endl;
1784 return Standard_False;
1788 if (myGeometryVertexTexture.IsNull()) // create geometry buffers
1790 myGeometryVertexTexture = new OpenGl_TextureBufferArb;
1791 myGeometryNormalTexture = new OpenGl_TextureBufferArb;
1792 myGeometryTexCrdTexture = new OpenGl_TextureBufferArb;
1793 myGeometryTriangTexture = new OpenGl_TextureBufferArb;
1795 if (!myGeometryVertexTexture->Create (theGlContext)
1796 || !myGeometryNormalTexture->Create (theGlContext)
1797 || !myGeometryTexCrdTexture->Create (theGlContext)
1798 || !myGeometryTriangTexture->Create (theGlContext))
1800 #ifdef RAY_TRACE_PRINT_INFO
1801 std::cout << "Error: Failed to create buffers for triangulation data" << std::endl;
1803 return Standard_False;
1807 if (myRaytraceMaterialTexture.IsNull()) // create material buffer
1809 myRaytraceMaterialTexture = new OpenGl_TextureBufferArb;
1811 if (!myRaytraceMaterialTexture->Create (theGlContext))
1813 #ifdef RAY_TRACE_PRINT_INFO
1814 std::cout << "Error: Failed to create buffers for material data" << std::endl;
1816 return Standard_False;
1820 /////////////////////////////////////////////////////////////////////////////
1821 // Write transform buffer
1823 BVH_Mat4f* aNodeTransforms = new BVH_Mat4f[myRaytraceGeometry.Size()];
1825 bool aResult = true;
1827 for (Standard_Integer anElemIndex = 0; anElemIndex < myRaytraceGeometry.Size(); ++anElemIndex)
1829 OpenGl_TriangleSet* aTriangleSet = dynamic_cast<OpenGl_TriangleSet*> (
1830 myRaytraceGeometry.Objects().ChangeValue (anElemIndex).operator->());
1832 const BVH_Transform<Standard_ShortReal, 4>* aTransform =
1833 dynamic_cast<const BVH_Transform<Standard_ShortReal, 4>* > (aTriangleSet->Properties().operator->());
1835 Standard_ASSERT_RETURN (aTransform != NULL,
1836 "OpenGl_TriangleSet does not contain transform", Standard_False);
1838 aNodeTransforms[anElemIndex] = aTransform->Inversed();
1841 aResult &= mySceneTransformTexture->Init (theGlContext, 4,
1842 myRaytraceGeometry.Size() * 4, reinterpret_cast<const GLfloat*> (aNodeTransforms));
1844 delete [] aNodeTransforms;
1846 /////////////////////////////////////////////////////////////////////////////
1847 // Write geometry and bottom-level BVH buffers
1849 Standard_Size aTotalVerticesNb = 0;
1850 Standard_Size aTotalElementsNb = 0;
1851 Standard_Size aTotalBVHNodesNb = 0;
1853 for (Standard_Integer anElemIndex = 0; anElemIndex < myRaytraceGeometry.Size(); ++anElemIndex)
1855 OpenGl_TriangleSet* aTriangleSet = dynamic_cast<OpenGl_TriangleSet*> (
1856 myRaytraceGeometry.Objects().ChangeValue (anElemIndex).operator->());
1858 Standard_ASSERT_RETURN (aTriangleSet != NULL,
1859 "Error: Failed to get triangulation of OpenGL element", Standard_False);
1861 aTotalVerticesNb += aTriangleSet->Vertices.size();
1862 aTotalElementsNb += aTriangleSet->Elements.size();
1864 Standard_ASSERT_RETURN (!aTriangleSet->QuadBVH().IsNull(),
1865 "Error: Failed to get bottom-level BVH of OpenGL element", Standard_False);
1867 aTotalBVHNodesNb += aTriangleSet->QuadBVH()->NodeInfoBuffer().size();
1870 aTotalBVHNodesNb += myRaytraceGeometry.QuadBVH()->NodeInfoBuffer().size();
1872 if (aTotalBVHNodesNb != 0)
1874 aResult &= mySceneNodeInfoTexture->Init (
1875 theGlContext, 4, GLsizei (aTotalBVHNodesNb), static_cast<const GLuint*> (NULL));
1876 aResult &= mySceneMinPointTexture->Init (
1877 theGlContext, 3, GLsizei (aTotalBVHNodesNb), static_cast<const GLfloat*> (NULL));
1878 aResult &= mySceneMaxPointTexture->Init (
1879 theGlContext, 3, GLsizei (aTotalBVHNodesNb), static_cast<const GLfloat*> (NULL));
1884 #ifdef RAY_TRACE_PRINT_INFO
1885 std::cout << "Error: Failed to upload buffers for bottom-level scene BVH" << std::endl;
1887 return Standard_False;
1890 if (aTotalElementsNb != 0)
1892 aResult &= myGeometryTriangTexture->Init (
1893 theGlContext, 4, GLsizei (aTotalElementsNb), static_cast<const GLuint*> (NULL));
1896 if (aTotalVerticesNb != 0)
1898 aResult &= myGeometryVertexTexture->Init (
1899 theGlContext, 3, GLsizei (aTotalVerticesNb), static_cast<const GLfloat*> (NULL));
1900 aResult &= myGeometryNormalTexture->Init (
1901 theGlContext, 3, GLsizei (aTotalVerticesNb), static_cast<const GLfloat*> (NULL));
1902 aResult &= myGeometryTexCrdTexture->Init (
1903 theGlContext, 2, GLsizei (aTotalVerticesNb), static_cast<const GLfloat*> (NULL));
1908 #ifdef RAY_TRACE_PRINT_INFO
1909 std::cout << "Error: Failed to upload buffers for scene geometry" << std::endl;
1911 return Standard_False;
1914 const QuadBvhHandle& aBVH = myRaytraceGeometry.QuadBVH();
1916 if (aBVH->Length() > 0)
1918 aResult &= mySceneNodeInfoTexture->SubData (theGlContext, 0, aBVH->Length(),
1919 reinterpret_cast<const GLuint*> (&aBVH->NodeInfoBuffer().front()));
1920 aResult &= mySceneMinPointTexture->SubData (theGlContext, 0, aBVH->Length(),
1921 reinterpret_cast<const GLfloat*> (&aBVH->MinPointBuffer().front()));
1922 aResult &= mySceneMaxPointTexture->SubData (theGlContext, 0, aBVH->Length(),
1923 reinterpret_cast<const GLfloat*> (&aBVH->MaxPointBuffer().front()));
1926 for (Standard_Integer aNodeIdx = 0; aNodeIdx < aBVH->Length(); ++aNodeIdx)
1928 if (!aBVH->IsOuter (aNodeIdx))
1931 OpenGl_TriangleSet* aTriangleSet = myRaytraceGeometry.TriangleSet (aNodeIdx);
1933 Standard_ASSERT_RETURN (aTriangleSet != NULL,
1934 "Error: Failed to get triangulation of OpenGL element", Standard_False);
1936 Standard_Integer aBVHOffset = myRaytraceGeometry.AccelerationOffset (aNodeIdx);
1938 Standard_ASSERT_RETURN (aBVHOffset != OpenGl_RaytraceGeometry::INVALID_OFFSET,
1939 "Error: Failed to get offset for bottom-level BVH", Standard_False);
1941 const Standard_Integer aBvhBuffersSize = aTriangleSet->QuadBVH()->Length();
1943 if (aBvhBuffersSize != 0)
1945 aResult &= mySceneNodeInfoTexture->SubData (theGlContext, aBVHOffset, aBvhBuffersSize,
1946 reinterpret_cast<const GLuint*> (&aTriangleSet->QuadBVH()->NodeInfoBuffer().front()));
1947 aResult &= mySceneMinPointTexture->SubData (theGlContext, aBVHOffset, aBvhBuffersSize,
1948 reinterpret_cast<const GLfloat*> (&aTriangleSet->QuadBVH()->MinPointBuffer().front()));
1949 aResult &= mySceneMaxPointTexture->SubData (theGlContext, aBVHOffset, aBvhBuffersSize,
1950 reinterpret_cast<const GLfloat*> (&aTriangleSet->QuadBVH()->MaxPointBuffer().front()));
1954 #ifdef RAY_TRACE_PRINT_INFO
1955 std::cout << "Error: Failed to upload buffers for bottom-level scene BVHs" << std::endl;
1957 return Standard_False;
1961 const Standard_Integer aVerticesOffset = myRaytraceGeometry.VerticesOffset (aNodeIdx);
1963 Standard_ASSERT_RETURN (aVerticesOffset != OpenGl_RaytraceGeometry::INVALID_OFFSET,
1964 "Error: Failed to get offset for triangulation vertices of OpenGL element", Standard_False);
1966 if (!aTriangleSet->Vertices.empty())
1968 aResult &= myGeometryNormalTexture->SubData (theGlContext, aVerticesOffset,
1969 GLsizei (aTriangleSet->Normals.size()), reinterpret_cast<const GLfloat*> (&aTriangleSet->Normals.front()));
1970 aResult &= myGeometryTexCrdTexture->SubData (theGlContext, aVerticesOffset,
1971 GLsizei (aTriangleSet->TexCrds.size()), reinterpret_cast<const GLfloat*> (&aTriangleSet->TexCrds.front()));
1972 aResult &= myGeometryVertexTexture->SubData (theGlContext, aVerticesOffset,
1973 GLsizei (aTriangleSet->Vertices.size()), reinterpret_cast<const GLfloat*> (&aTriangleSet->Vertices.front()));
1976 const Standard_Integer anElementsOffset = myRaytraceGeometry.ElementsOffset (aNodeIdx);
1978 Standard_ASSERT_RETURN (anElementsOffset != OpenGl_RaytraceGeometry::INVALID_OFFSET,
1979 "Error: Failed to get offset for triangulation elements of OpenGL element", Standard_False);
1981 if (!aTriangleSet->Elements.empty())
1983 aResult &= myGeometryTriangTexture->SubData (theGlContext, anElementsOffset, GLsizei (aTriangleSet->Elements.size()),
1984 reinterpret_cast<const GLuint*> (&aTriangleSet->Elements.front()));
1989 #ifdef RAY_TRACE_PRINT_INFO
1990 std::cout << "Error: Failed to upload triangulation buffers for OpenGL element" << std::endl;
1992 return Standard_False;
1996 /////////////////////////////////////////////////////////////////////////////
1997 // Write material buffer
1999 if (myRaytraceGeometry.Materials.size() != 0)
2001 aResult &= myRaytraceMaterialTexture->Init (theGlContext, 4,
2002 GLsizei (myRaytraceGeometry.Materials.size() * 18), myRaytraceGeometry.Materials.front().Packed());
2006 #ifdef RAY_TRACE_PRINT_INFO
2007 std::cout << "Error: Failed to upload material buffer" << std::endl;
2009 return Standard_False;
2013 myIsRaytraceDataValid = myRaytraceGeometry.Objects().Size() != 0;
2015 #ifdef RAY_TRACE_PRINT_INFO
2017 Standard_ShortReal aMemTrgUsed = 0.f;
2018 Standard_ShortReal aMemBvhUsed = 0.f;
2020 for (Standard_Integer anElemIdx = 0; anElemIdx < myRaytraceGeometry.Size(); ++anElemIdx)
2022 OpenGl_TriangleSet* aTriangleSet = dynamic_cast<OpenGl_TriangleSet*> (myRaytraceGeometry.Objects()(anElemIdx).get());
2024 aMemTrgUsed += static_cast<Standard_ShortReal> (
2025 aTriangleSet->Vertices.size() * sizeof (BVH_Vec3f));
2026 aMemTrgUsed += static_cast<Standard_ShortReal> (
2027 aTriangleSet->Normals.size() * sizeof (BVH_Vec3f));
2028 aMemTrgUsed += static_cast<Standard_ShortReal> (
2029 aTriangleSet->TexCrds.size() * sizeof (BVH_Vec2f));
2030 aMemTrgUsed += static_cast<Standard_ShortReal> (
2031 aTriangleSet->Elements.size() * sizeof (BVH_Vec4i));
2033 aMemBvhUsed += static_cast<Standard_ShortReal> (
2034 aTriangleSet->QuadBVH()->NodeInfoBuffer().size() * sizeof (BVH_Vec4i));
2035 aMemBvhUsed += static_cast<Standard_ShortReal> (
2036 aTriangleSet->QuadBVH()->MinPointBuffer().size() * sizeof (BVH_Vec3f));
2037 aMemBvhUsed += static_cast<Standard_ShortReal> (
2038 aTriangleSet->QuadBVH()->MaxPointBuffer().size() * sizeof (BVH_Vec3f));
2041 aMemBvhUsed += static_cast<Standard_ShortReal> (
2042 myRaytraceGeometry.QuadBVH()->NodeInfoBuffer().size() * sizeof (BVH_Vec4i));
2043 aMemBvhUsed += static_cast<Standard_ShortReal> (
2044 myRaytraceGeometry.QuadBVH()->MinPointBuffer().size() * sizeof (BVH_Vec3f));
2045 aMemBvhUsed += static_cast<Standard_ShortReal> (
2046 myRaytraceGeometry.QuadBVH()->MaxPointBuffer().size() * sizeof (BVH_Vec3f));
2048 std::cout << "GPU Memory Used (Mb):\n"
2049 << "\tFor mesh: " << aMemTrgUsed / 1048576 << "\n"
2050 << "\tFor BVHs: " << aMemBvhUsed / 1048576 << "\n";
2057 // =======================================================================
2058 // function : updateRaytraceLightSources
2059 // purpose : Updates 3D scene light sources for ray-tracing
2060 // =======================================================================
2061 Standard_Boolean OpenGl_View::updateRaytraceLightSources (const OpenGl_Mat4& theInvModelView, const Handle(OpenGl_Context)& theGlContext)
2063 myRaytraceGeometry.Sources.clear();
2065 myRaytraceGeometry.Ambient = BVH_Vec4f (0.0f, 0.0f, 0.0f, 0.0f);
2067 OpenGl_ListOfLight::Iterator aLightIter (myShadingModel == Graphic3d_TOSM_NONE ? myNoShadingLight : myLights);
2068 for (; aLightIter.More(); aLightIter.Next())
2070 const OpenGl_Light& aLight = aLightIter.Value();
2072 if (aLight.Type == Graphic3d_TOLS_AMBIENT)
2074 myRaytraceGeometry.Ambient += BVH_Vec4f (aLight.Color.r() * aLight.Intensity,
2075 aLight.Color.g() * aLight.Intensity,
2076 aLight.Color.b() * aLight.Intensity,
2081 BVH_Vec4f aDiffuse (aLight.Color.r() * aLight.Intensity,
2082 aLight.Color.g() * aLight.Intensity,
2083 aLight.Color.b() * aLight.Intensity,
2086 BVH_Vec4f aPosition (-aLight.Direction.x(),
2087 -aLight.Direction.y(),
2088 -aLight.Direction.z(),
2091 if (aLight.Type != Graphic3d_TOLS_DIRECTIONAL)
2093 aPosition = BVH_Vec4f (aLight.Position.x(),
2094 aLight.Position.y(),
2095 aLight.Position.z(),
2098 // store smoothing radius in w-component
2099 aDiffuse.w() = Max (aLight.Smoothness, 0.f);
2103 // store cosine of smoothing angle in w-component
2104 aDiffuse.w() = cosf (Min (Max (aLight.Smoothness, 0.f), static_cast<Standard_ShortReal> (M_PI / 2.0)));
2107 if (aLight.IsHeadlight)
2109 aPosition = theInvModelView * aPosition;
2112 myRaytraceGeometry.Sources.push_back (OpenGl_RaytraceLight (aDiffuse, aPosition));
2115 if (myRaytraceLightSrcTexture.IsNull()) // create light source buffer
2117 myRaytraceLightSrcTexture = new OpenGl_TextureBufferArb;
2119 if (!myRaytraceLightSrcTexture->Create (theGlContext))
2121 #ifdef RAY_TRACE_PRINT_INFO
2122 std::cout << "Error: Failed to create light source buffer" << std::endl;
2124 return Standard_False;
2128 if (myRaytraceGeometry.Sources.size() != 0)
2130 const GLfloat* aDataPtr = myRaytraceGeometry.Sources.front().Packed();
2131 if (!myRaytraceLightSrcTexture->Init (theGlContext, 4, GLsizei (myRaytraceGeometry.Sources.size() * 2), aDataPtr))
2133 #ifdef RAY_TRACE_PRINT_INFO
2134 std::cout << "Error: Failed to upload light source buffer" << std::endl;
2136 return Standard_False;
2140 return Standard_True;
2143 // =======================================================================
2144 // function : updateRaytraceEnvironmentMap
2145 // purpose : Updates environment map for ray-tracing
2146 // =======================================================================
2147 Standard_Boolean OpenGl_View::updateRaytraceEnvironmentMap (const Handle(OpenGl_Context)& theGlContext)
2149 Standard_Boolean aResult = Standard_True;
2151 if (!myToUpdateEnvironmentMap)
2156 for (Standard_Integer anIdx = 0; anIdx < 2; ++anIdx)
2158 const Handle(OpenGl_ShaderProgram)& aProgram =
2159 anIdx == 0 ? myRaytraceProgram : myPostFSAAProgram;
2161 if (!aProgram.IsNull())
2163 aResult &= theGlContext->BindProgram (aProgram);
2165 if (!myTextureEnv.IsNull())
2167 myTextureEnv->Bind (theGlContext,
2168 GL_TEXTURE0 + OpenGl_RT_EnvironmentMapTexture);
2170 aResult &= aProgram->SetUniform (theGlContext,
2171 myUniformLocations[anIdx][OpenGl_RT_uSphereMapEnabled], 1);
2175 aResult &= aProgram->SetUniform (theGlContext,
2176 myUniformLocations[anIdx][OpenGl_RT_uSphereMapEnabled], 0);
2181 myToUpdateEnvironmentMap = Standard_False;
2183 theGlContext->BindProgram (NULL);
2188 // =======================================================================
2189 // function : setUniformState
2190 // purpose : Sets uniform state for the given ray-tracing shader program
2191 // =======================================================================
2192 Standard_Boolean OpenGl_View::setUniformState (const OpenGl_Vec3* theOrigins,
2193 const OpenGl_Vec3* theDirects,
2194 const OpenGl_Mat4& theViewMat,
2195 const OpenGl_Mat4& theUnviewMat,
2196 const Standard_Integer theProgramId,
2197 const Handle(OpenGl_Context)& theGlContext)
2199 Handle(OpenGl_ShaderProgram)& theProgram =
2200 theProgramId == 0 ? myRaytraceProgram : myPostFSAAProgram;
2202 if (theProgram.IsNull())
2204 return Standard_False;
2207 const Standard_Integer aLightSourceBufferSize =
2208 static_cast<Standard_Integer> (myRaytraceGeometry.Sources.size());
2211 theProgram->SetUniform (theGlContext,
2212 myUniformLocations[theProgramId][OpenGl_RT_uOriginLB], theOrigins[0]);
2213 theProgram->SetUniform (theGlContext,
2214 myUniformLocations[theProgramId][OpenGl_RT_uOriginRB], theOrigins[1]);
2215 theProgram->SetUniform (theGlContext,
2216 myUniformLocations[theProgramId][OpenGl_RT_uOriginLT], theOrigins[2]);
2217 theProgram->SetUniform (theGlContext,
2218 myUniformLocations[theProgramId][OpenGl_RT_uOriginRT], theOrigins[3]);
2219 theProgram->SetUniform (theGlContext,
2220 myUniformLocations[theProgramId][OpenGl_RT_uDirectLB], theDirects[0]);
2221 theProgram->SetUniform (theGlContext,
2222 myUniformLocations[theProgramId][OpenGl_RT_uDirectRB], theDirects[1]);
2223 theProgram->SetUniform (theGlContext,
2224 myUniformLocations[theProgramId][OpenGl_RT_uDirectLT], theDirects[2]);
2225 theProgram->SetUniform (theGlContext,
2226 myUniformLocations[theProgramId][OpenGl_RT_uDirectRT], theDirects[3]);
2227 theProgram->SetUniform (theGlContext,
2228 myUniformLocations[theProgramId][OpenGl_RT_uViewMat], theViewMat);
2229 theProgram->SetUniform (theGlContext,
2230 myUniformLocations[theProgramId][OpenGl_RT_uUnviewMat], theUnviewMat);
2232 // Set scene parameters
2233 theProgram->SetUniform (theGlContext,
2234 myUniformLocations[theProgramId][OpenGl_RT_uSceneRad], myRaytraceSceneRadius);
2235 theProgram->SetUniform (theGlContext,
2236 myUniformLocations[theProgramId][OpenGl_RT_uSceneEps], myRaytraceSceneEpsilon);
2237 theProgram->SetUniform (theGlContext,
2238 myUniformLocations[theProgramId][OpenGl_RT_uLightCount], aLightSourceBufferSize);
2239 theProgram->SetUniform (theGlContext,
2240 myUniformLocations[theProgramId][OpenGl_RT_uLightAmbnt], myRaytraceGeometry.Ambient);
2242 // Set run-time rendering options
2243 theProgram->SetUniform (theGlContext,
2244 myUniformLocations[theProgramId][OpenGl_RT_uShadowsEnabled], myRenderParams.IsShadowEnabled ? 1 : 0);
2245 theProgram->SetUniform (theGlContext,
2246 myUniformLocations[theProgramId][OpenGl_RT_uReflectEnabled], myRenderParams.IsReflectionEnabled ? 1 : 0);
2248 if (myRenderParams.IsGlobalIlluminationEnabled)
2250 theProgram->SetUniform (theGlContext,
2251 myUniformLocations[theProgramId][OpenGl_RT_uBlockedRngEnabled], myRenderParams.CoherentPathTracingMode ? 1 : 0);
2254 // Set array of 64-bit texture handles
2255 if (theGlContext->arbTexBindless != NULL && myRaytraceGeometry.HasTextures())
2257 const std::vector<GLuint64>& aTextures = myRaytraceGeometry.TextureHandles();
2259 theProgram->SetUniform (theGlContext, myUniformLocations[theProgramId][OpenGl_RT_uTexSamplersArray],
2260 static_cast<GLsizei> (aTextures.size()), (OpenGl_Vec2u* )&aTextures.front());
2263 // Set background colors (only gradient background supported)
2264 if (myBgGradientArray != NULL
2265 && myBgGradientArray->IsDefined())
2267 theProgram->SetUniform (theGlContext,
2268 myUniformLocations[theProgramId][OpenGl_RT_uBackColorTop], myBgGradientArray->GradientColor (0));
2269 theProgram->SetUniform (theGlContext,
2270 myUniformLocations[theProgramId][OpenGl_RT_uBackColorBot], myBgGradientArray->GradientColor (1));
2274 const OpenGl_Vec4 aBackColor (myBgColor.rgb[0],
2278 theProgram->SetUniform (theGlContext,
2279 myUniformLocations[theProgramId][OpenGl_RT_uBackColorTop], aBackColor);
2280 theProgram->SetUniform (theGlContext,
2281 myUniformLocations[theProgramId][OpenGl_RT_uBackColorBot], aBackColor);
2284 theProgram->SetUniform (theGlContext,
2285 myUniformLocations[theProgramId][OpenGl_RT_uSphereMapForBack], myRenderParams.UseEnvironmentMapBackground ? 1 : 0);
2287 return Standard_True;
2290 // =======================================================================
2291 // function : bindRaytraceTextures
2292 // purpose : Binds ray-trace textures to corresponding texture units
2293 // =======================================================================
2294 void OpenGl_View::bindRaytraceTextures (const Handle(OpenGl_Context)& theGlContext)
2296 mySceneMinPointTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneMinPointTexture);
2297 mySceneMaxPointTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneMaxPointTexture);
2298 mySceneNodeInfoTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneNodeInfoTexture);
2299 myGeometryVertexTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryVertexTexture);
2300 myGeometryNormalTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryNormalTexture);
2301 myGeometryTexCrdTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryTexCrdTexture);
2302 myGeometryTriangTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryTriangTexture);
2303 mySceneTransformTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneTransformTexture);
2304 myRaytraceMaterialTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_RaytraceMaterialTexture);
2305 myRaytraceLightSrcTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_RaytraceLightSrcTexture);
2307 if (!myOpenGlFBO.IsNull())
2309 myOpenGlFBO->ColorTexture()->Bind (theGlContext, GL_TEXTURE0 + OpenGl_RT_OpenGlColorTexture);
2310 myOpenGlFBO->DepthStencilTexture()->Bind (theGlContext, GL_TEXTURE0 + OpenGl_RT_OpenGlDepthTexture);
2314 // =======================================================================
2315 // function : unbindRaytraceTextures
2316 // purpose : Unbinds ray-trace textures from corresponding texture units
2317 // =======================================================================
2318 void OpenGl_View::unbindRaytraceTextures (const Handle(OpenGl_Context)& theGlContext)
2320 mySceneMinPointTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneMinPointTexture);
2321 mySceneMaxPointTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneMaxPointTexture);
2322 mySceneNodeInfoTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneNodeInfoTexture);
2323 myGeometryVertexTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryVertexTexture);
2324 myGeometryNormalTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryNormalTexture);
2325 myGeometryTexCrdTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryTexCrdTexture);
2326 myGeometryTriangTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryTriangTexture);
2327 mySceneTransformTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneTransformTexture);
2328 myRaytraceMaterialTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_RaytraceMaterialTexture);
2329 myRaytraceLightSrcTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_RaytraceLightSrcTexture);
2331 if (!myOpenGlFBO.IsNull())
2333 myOpenGlFBO->ColorTexture()->Unbind (theGlContext, GL_TEXTURE0 + OpenGl_RT_OpenGlColorTexture);
2334 myOpenGlFBO->DepthStencilTexture()->Unbind (theGlContext, GL_TEXTURE0 + OpenGl_RT_OpenGlDepthTexture);
2337 theGlContext->core15fwd->glActiveTexture (GL_TEXTURE0);
2340 // =======================================================================
2341 // function : runRaytraceShaders
2342 // purpose : Runs ray-tracing shader programs
2343 // =======================================================================
2344 Standard_Boolean OpenGl_View::runRaytraceShaders (const Standard_Integer theSizeX,
2345 const Standard_Integer theSizeY,
2346 const OpenGl_Vec3* theOrigins,
2347 const OpenGl_Vec3* theDirects,
2348 const OpenGl_Mat4& theViewMat,
2349 const OpenGl_Mat4& theUnviewMat,
2350 Graphic3d_Camera::Projection theProjection,
2351 OpenGl_FrameBuffer* theReadDrawFbo,
2352 const Handle(OpenGl_Context)& theGlContext)
2354 bindRaytraceTextures (theGlContext);
2356 Handle(OpenGl_FrameBuffer) aRenderFramebuffer;
2357 Handle(OpenGl_FrameBuffer) aDepthSourceFramebuffer;
2358 Handle(OpenGl_FrameBuffer) anAccumFramebuffer;
2360 // Choose proper set of framebuffers for stereo rendering
2361 Standard_Boolean isStereo = myCamera->ProjectionType() == Graphic3d_Camera::Projection_Stereo;
2362 Standard_Boolean isRightEye = theProjection == Graphic3d_Camera::Projection_MonoRightEye;
2363 Standard_Integer aFBOIdx = (isStereo && isRightEye) ? 1 : 0;
2365 if (myRaytraceParameters.GlobalIllumination) // if path-tracing is used
2367 aRenderFramebuffer = myAccumFrames % 2 ? myRaytraceFBO1[aFBOIdx] : myRaytraceFBO2[aFBOIdx];
2368 anAccumFramebuffer = myAccumFrames % 2 ? myRaytraceFBO2[aFBOIdx] : myRaytraceFBO1[aFBOIdx];
2369 aDepthSourceFramebuffer = aRenderFramebuffer;
2371 anAccumFramebuffer->ColorTexture()->Bind (
2372 theGlContext, GL_TEXTURE0 + OpenGl_RT_PrevAccumTexture);
2374 aRenderFramebuffer->BindBuffer (theGlContext);
2376 else if (myRenderParams.IsAntialiasingEnabled) // if 2-pass ray-tracing is used
2378 myRaytraceFBO1[aFBOIdx]->BindBuffer (theGlContext);
2381 Standard_Boolean aResult = theGlContext->BindProgram (myRaytraceProgram);
2383 aResult &= setUniformState (theOrigins,
2387 0, // ID of RT program
2390 if (myRaytraceParameters.GlobalIllumination)
2392 if (myAccumFrames == 0)
2397 // Set frame accumulation weight
2398 myRaytraceProgram->SetUniform (theGlContext,
2399 myUniformLocations[0][OpenGl_RT_uSampleWeight], 1.f / (myAccumFrames + 1));
2401 // Set random number generator seed
2402 myRaytraceProgram->SetUniform (theGlContext,
2403 myUniformLocations[0][OpenGl_RT_uFrameRndSeed], static_cast<Standard_Integer> (myRNG.NextInt() >> 2));
2406 theGlContext->core20fwd->glDrawArrays (GL_TRIANGLES, 0, 6);
2408 if (myRenderParams.IsAntialiasingEnabled && !myRenderParams.IsGlobalIlluminationEnabled)
2410 glDepthMask (GL_FALSE);
2412 myRaytraceFBO1[aFBOIdx]->ColorTexture()->Bind (theGlContext, GL_TEXTURE0 + OpenGl_RT_FsaaInputTexture);
2414 aResult &= theGlContext->BindProgram (myPostFSAAProgram);
2416 aResult &= setUniformState (theOrigins,
2420 1, // ID of FSAA program
2423 // Perform multi-pass adaptive FSAA using ping-pong technique.
2424 // We use 'FLIPTRI' sampling pattern changing for every pixel
2425 // (3 additional samples per pixel, the 1st sample is already
2426 // available from initial ray-traced image).
2427 for (Standard_Integer anIt = 1; anIt < 4; ++anIt)
2429 GLfloat aOffsetX = 1.f / theSizeX;
2430 GLfloat aOffsetY = 1.f / theSizeY;
2448 aResult &= myPostFSAAProgram->SetUniform (theGlContext,
2449 myUniformLocations[1][OpenGl_RT_uSamples], anIt + 1);
2450 aResult &= myPostFSAAProgram->SetUniform (theGlContext,
2451 myUniformLocations[1][OpenGl_RT_uOffsetX], aOffsetX);
2452 aResult &= myPostFSAAProgram->SetUniform (theGlContext,
2453 myUniformLocations[1][OpenGl_RT_uOffsetY], aOffsetY);
2455 Handle(OpenGl_FrameBuffer)& aFramebuffer = anIt % 2 ? myRaytraceFBO2[aFBOIdx] : myRaytraceFBO1[aFBOIdx];
2457 aFramebuffer->BindBuffer (theGlContext);
2459 theGlContext->core20fwd->glDrawArrays (GL_TRIANGLES, 0, 6);
2461 aFramebuffer->ColorTexture()->Bind (theGlContext, GL_TEXTURE0 + OpenGl_RT_FsaaInputTexture);
2464 aRenderFramebuffer = myRaytraceFBO2[aFBOIdx];
2465 aDepthSourceFramebuffer = myRaytraceFBO1[aFBOIdx];
2468 if (myRaytraceParameters.GlobalIllumination || myRenderParams.IsAntialiasingEnabled)
2470 // Output accumulated image
2471 glDepthMask (GL_TRUE);
2473 theGlContext->BindProgram (myOutImageProgram);
2475 myOutImageProgram->SetUniform (theGlContext, "uApplyGamma", static_cast<Standard_Integer> (myRaytraceParameters.GlobalIllumination));
2477 if (theReadDrawFbo != NULL)
2479 theReadDrawFbo->BindBuffer (theGlContext);
2483 aRenderFramebuffer->UnbindBuffer (theGlContext);
2486 aRenderFramebuffer->ColorTexture()->Bind (
2487 theGlContext, GL_TEXTURE0 + OpenGl_RT_PrevAccumTexture);
2489 aDepthSourceFramebuffer->DepthStencilTexture()->Bind (
2490 theGlContext, GL_TEXTURE0 + OpenGl_RT_DepthTexture);
2492 theGlContext->core20fwd->glDrawArrays (GL_TRIANGLES, 0, 6);
2494 aDepthSourceFramebuffer->DepthStencilTexture()->Unbind (
2495 theGlContext, GL_TEXTURE0 + OpenGl_RT_DepthTexture);
2497 aRenderFramebuffer->ColorTexture()->Unbind (
2498 theGlContext, GL_TEXTURE0 + OpenGl_RT_PrevAccumTexture);
2501 unbindRaytraceTextures (theGlContext);
2503 theGlContext->BindProgram (NULL);
2508 // =======================================================================
2509 // function : raytrace
2510 // purpose : Redraws the window using OpenGL/GLSL ray-tracing
2511 // =======================================================================
2512 Standard_Boolean OpenGl_View::raytrace (const Standard_Integer theSizeX,
2513 const Standard_Integer theSizeY,
2514 Graphic3d_Camera::Projection theProjection,
2515 OpenGl_FrameBuffer* theReadDrawFbo,
2516 const Handle(OpenGl_Context)& theGlContext)
2518 if (!initRaytraceResources (theGlContext))
2520 return Standard_False;
2523 if (!updateRaytraceBuffers (theSizeX, theSizeY, theGlContext))
2525 return Standard_False;
2528 if (!updateRaytraceEnvironmentMap (theGlContext))
2530 return Standard_False;
2533 // Get model-view and projection matrices
2534 OpenGl_Mat4 aOrientationMatrix = myCamera->OrientationMatrixF();
2535 OpenGl_Mat4 aViewMappingMatrix = theGlContext->ProjectionState.Current();
2537 OpenGl_Mat4 aInverOrientMatrix;
2538 aOrientationMatrix.Inverted (aInverOrientMatrix);
2539 if (!updateRaytraceLightSources (aInverOrientMatrix, theGlContext))
2541 return Standard_False;
2544 OpenGl_Vec3 aOrigins[4];
2545 OpenGl_Vec3 aDirects[4];
2546 OpenGl_Mat4 aViewMat;
2547 OpenGl_Mat4 anUnviewMat;
2549 updateCamera (aOrientationMatrix,
2556 if (theReadDrawFbo != NULL)
2558 theReadDrawFbo->BindBuffer (theGlContext);
2561 // Generate ray-traced image
2562 if (myIsRaytraceDataValid)
2564 myRaytraceScreenQuad.BindVertexAttrib (theGlContext, Graphic3d_TOA_POS);
2566 if (!myRaytraceGeometry.AcquireTextures (theGlContext))
2568 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR,
2569 0, GL_DEBUG_SEVERITY_MEDIUM, "Error: Failed to acquire OpenGL image textures");
2572 // Remember the old depth function and mask
2574 theGlContext->core11fwd->glGetIntegerv (GL_DEPTH_FUNC, &aDepthFunc);
2576 GLboolean aDepthMask;
2577 theGlContext->core11fwd->glGetBooleanv (GL_DEPTH_WRITEMASK, &aDepthMask);
2579 glDisable (GL_BLEND);
2580 glDepthFunc (GL_ALWAYS);
2582 Standard_Boolean aResult = runRaytraceShaders (theSizeX,
2592 // Restore depth function and mask
2593 glDepthFunc (aDepthFunc);
2594 glDepthMask (aDepthMask);
2598 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR,
2599 0, GL_DEBUG_SEVERITY_MEDIUM, "Error: Failed to execute ray-tracing shaders");
2602 if (!myRaytraceGeometry.ReleaseTextures (theGlContext))
2604 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR,
2605 0, GL_DEBUG_SEVERITY_MEDIUM, "Error: Failed to release OpenGL image textures");
2608 myRaytraceScreenQuad.UnbindVertexAttrib (theGlContext, Graphic3d_TOA_POS);
2611 return Standard_True;