0025885: Visualization, ray tracing - Improve layer processing
[occt.git] / src / OpenGl / OpenGl_View_Raytrace.cxx
CommitLineData
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
25ef750e 16#include <Graphic3d_TextureParams.hxx>
91c60b57 17
fc73a202 18#include <OpenGl_FrameBuffer.hxx>
b7cd4ba7 19#include <OpenGl_PrimitiveArray.hxx>
fc73a202 20#include <OpenGl_VertexBuffer.hxx>
e276548b 21#include <OpenGl_View.hxx>
91c60b57 22
fc73a202 23#include <OSD_Protection.hxx>
91c60b57 24#include <OSD_File.hxx>
e276548b 25
26using namespace OpenGl_Raytrace;
27
28//! Use this macro to output ray-tracing debug info
fc73a202 29// #define RAY_TRACE_PRINT_INFO
e276548b 30
31#ifdef RAY_TRACE_PRINT_INFO
32 #include <OSD_Timer.hxx>
33#endif
34
e276548b 35// =======================================================================
e276548b 36// function : UpdateRaytraceGeometry
91c60b57 37// purpose : Updates 3D scene geometry for ray-tracing
e276548b 38// =======================================================================
91c60b57 39Standard_Boolean OpenGl_View::updateRaytraceGeometry (const RaytraceUpdateMode theMode,
40 const Standard_Integer theViewId,
41 const Handle(OpenGl_Context)& theGlContext)
e276548b 42{
91c60b57 43 // In 'check' mode (OpenGl_GUM_CHECK) the scene geometry is analyzed for
44 // modifications. This is light-weight procedure performed on each frame
84c71f29 45 if (theMode == OpenGl_GUM_CHECK)
e276548b 46 {
91c60b57 47 if (myLayersModificationStatus != myZLayers.ModificationState())
48 {
49 return updateRaytraceGeometry (OpenGl_GUM_PREPARE, theViewId, theGlContext);
50 }
51 }
84c71f29 52 else if (theMode == OpenGl_GUM_PREPARE)
53 {
54 myRaytraceGeometry.ClearMaterials();
91c60b57 55
84c71f29 56 myArrayToTrianglesMap.clear();
e276548b 57
58 myIsRaytraceDataValid = Standard_False;
59 }
e276548b 60
61 // The set of processed structures (reflected to ray-tracing)
62 // This set is used to remove out-of-date records from the
63 // hash map of structures
64 std::set<const OpenGl_Structure*> anElements;
65
91c60b57 66 // Set to store all currently visible OpenGL primitive arrays
67 // applicable for ray-tracing
8d3f219f 68 std::set<Standard_Size> anArrayIDs;
84c71f29 69
91c60b57 70 const OpenGl_Layer& aLayer = myZLayers.Layer (Graphic3d_ZLayerId_Default);
e276548b 71
91c60b57 72 if (aLayer.NbStructures() != 0)
e276548b 73 {
a1954302 74 const OpenGl_ArrayOfStructure& aStructArray = aLayer.ArrayOfStructures();
91c60b57 75
68333c8f 76 for (Standard_Integer anIndex = 0; anIndex < aStructArray.Length(); ++anIndex)
e276548b 77 {
a1954302 78 for (OpenGl_SequenceOfStructure::Iterator aStructIt (aStructArray (anIndex)); aStructIt.More(); aStructIt.Next())
e276548b 79 {
80 const OpenGl_Structure* aStructure = aStructIt.Value();
81
84c71f29 82 if (theMode == OpenGl_GUM_CHECK)
e276548b 83 {
91c60b57 84 if (toUpdateStructure (aStructure))
e276548b 85 {
91c60b57 86 return updateRaytraceGeometry (OpenGl_GUM_PREPARE, theViewId, theGlContext);
e276548b 87 }
e2da917a 88 }
84c71f29 89 else if (theMode == OpenGl_GUM_PREPARE)
90 {
91c60b57 91 if (!aStructure->IsRaytracable() || !aStructure->IsVisible())
a1954302 92 {
a272ed94 93 continue;
94 }
91c60b57 95 else if (!aStructure->ViewAffinity.IsNull() && !aStructure->ViewAffinity->IsVisible (theViewId))
a272ed94 96 {
84c71f29 97 continue;
a1954302 98 }
84c71f29 99
100 for (OpenGl_Structure::GroupIterator aGroupIter (aStructure->DrawGroups()); aGroupIter.More(); aGroupIter.Next())
101 {
91c60b57 102 // Extract OpenGL elements from the group (primitives arrays)
84c71f29 103 for (const OpenGl_ElementNode* aNode = aGroupIter.Value()->FirstNode(); aNode != NULL; aNode = aNode->next)
104 {
105 OpenGl_PrimitiveArray* aPrimArray = dynamic_cast<OpenGl_PrimitiveArray*> (aNode->elem);
106
107 if (aPrimArray != NULL)
108 {
8d3f219f 109 anArrayIDs.insert (aPrimArray->GetUID());
84c71f29 110 }
111 }
112 }
e2da917a 113 }
91c60b57 114 else if (theMode == OpenGl_GUM_REBUILD)
e276548b 115 {
116 if (!aStructure->IsRaytracable())
91c60b57 117 {
e276548b 118 continue;
91c60b57 119 }
120 else if (addRaytraceStructure (aStructure, theGlContext))
121 {
122 anElements.insert (aStructure); // structure was processed
123 }
e276548b 124 }
125 }
126 }
127 }
128
84c71f29 129 if (theMode == OpenGl_GUM_PREPARE)
130 {
25ef750e 131 BVH_ObjectSet<Standard_ShortReal, 3>::BVH_ObjectList anUnchangedObjects;
84c71f29 132
91c60b57 133 // Filter out unchanged objects so only their transformations and materials
134 // will be updated (and newly added objects will be processed from scratch)
135 for (Standard_Integer anObjIdx = 0; anObjIdx < myRaytraceGeometry.Size(); ++anObjIdx)
84c71f29 136 {
137 OpenGl_TriangleSet* aTriangleSet = dynamic_cast<OpenGl_TriangleSet*> (
91c60b57 138 myRaytraceGeometry.Objects().ChangeValue (anObjIdx).operator->());
84c71f29 139
91c60b57 140 if (aTriangleSet == NULL)
84c71f29 141 {
91c60b57 142 continue;
143 }
144
145 if (anArrayIDs.find (aTriangleSet->AssociatedPArrayID()) != anArrayIDs.end())
146 {
147 anUnchangedObjects.Append (myRaytraceGeometry.Objects().Value (anObjIdx));
84c71f29 148
8d3f219f 149 myArrayToTrianglesMap[aTriangleSet->AssociatedPArrayID()] = aTriangleSet;
84c71f29 150 }
151 }
152
153 myRaytraceGeometry.Objects() = anUnchangedObjects;
154
91c60b57 155 return updateRaytraceGeometry (OpenGl_GUM_REBUILD, theViewId, theGlContext);
84c71f29 156 }
91c60b57 157 else if (theMode == OpenGl_GUM_REBUILD)
e276548b 158 {
91c60b57 159 // Actualize the hash map of structures - remove out-of-date records
e276548b 160 std::map<const OpenGl_Structure*, Standard_Size>::iterator anIter = myStructureStates.begin();
161
162 while (anIter != myStructureStates.end())
163 {
164 if (anElements.find (anIter->first) == anElements.end())
165 {
166 myStructureStates.erase (anIter++);
167 }
168 else
169 {
170 ++anIter;
171 }
172 }
173
174 // Actualize OpenGL layer list state
91c60b57 175 myLayersModificationStatus = myZLayers.ModificationState();
e276548b 176
91c60b57 177 // Rebuild two-level acceleration structure
265d4508 178 myRaytraceGeometry.ProcessAcceleration();
e276548b 179
91c60b57 180 myRaytraceSceneRadius = 2.f /* scale factor */ * std::max (
181 myRaytraceGeometry.Box().CornerMin().cwiseAbs().maxComp(),
182 myRaytraceGeometry.Box().CornerMax().cwiseAbs().maxComp());
e276548b 183
25ef750e 184 const BVH_Vec3f aSize = myRaytraceGeometry.Box().Size();
e276548b 185
91c60b57 186 myRaytraceSceneEpsilon = Max (1.0e-6f, 1.0e-4f * aSize.Modulus());
fc73a202 187
91c60b57 188 return uploadRaytraceData (theGlContext);
e276548b 189 }
190
e276548b 191 return Standard_True;
192}
193
194// =======================================================================
91c60b57 195// function : ToUpdateStructure
e2da917a 196// purpose : Checks to see if the structure is modified
e276548b 197// =======================================================================
91c60b57 198Standard_Boolean OpenGl_View::toUpdateStructure (const OpenGl_Structure* theStructure)
e276548b 199{
200 if (!theStructure->IsRaytracable())
201 {
e276548b 202 if (theStructure->ModificationState() > 0)
203 {
204 theStructure->ResetModificationState();
91c60b57 205
206 return Standard_True; // ray-trace element was removed - need to rebuild
e276548b 207 }
208
91c60b57 209 return Standard_False; // did not contain ray-trace elements
e276548b 210 }
211
212 std::map<const OpenGl_Structure*, Standard_Size>::iterator aStructState = myStructureStates.find (theStructure);
213
214 if (aStructState != myStructureStates.end())
91c60b57 215 {
e276548b 216 return aStructState->second != theStructure->ModificationState();
91c60b57 217 }
e276548b 218
219 return Standard_True;
220}
221
222// =======================================================================
91c60b57 223// function : BuildTextureTransform
25ef750e 224// purpose : Constructs texture transformation matrix
225// =======================================================================
91c60b57 226void BuildTextureTransform (const Handle(Graphic3d_TextureParams)& theParams, BVH_Mat4f& theMatrix)
25ef750e 227{
228 theMatrix.InitIdentity();
229
230 // Apply scaling
231 const Graphic3d_Vec2& aScale = theParams->Scale();
232
233 theMatrix.ChangeValue (0, 0) *= aScale.x();
234 theMatrix.ChangeValue (1, 0) *= aScale.x();
235 theMatrix.ChangeValue (2, 0) *= aScale.x();
236 theMatrix.ChangeValue (3, 0) *= aScale.x();
237
238 theMatrix.ChangeValue (0, 1) *= aScale.y();
239 theMatrix.ChangeValue (1, 1) *= aScale.y();
240 theMatrix.ChangeValue (2, 1) *= aScale.y();
241 theMatrix.ChangeValue (3, 1) *= aScale.y();
242
243 // Apply translation
244 const Graphic3d_Vec2 aTrans = -theParams->Translation();
245
246 theMatrix.ChangeValue (0, 3) = theMatrix.GetValue (0, 0) * aTrans.x() +
247 theMatrix.GetValue (0, 1) * aTrans.y();
248
249 theMatrix.ChangeValue (1, 3) = theMatrix.GetValue (1, 0) * aTrans.x() +
250 theMatrix.GetValue (1, 1) * aTrans.y();
251
252 theMatrix.ChangeValue (2, 3) = theMatrix.GetValue (2, 0) * aTrans.x() +
253 theMatrix.GetValue (2, 1) * aTrans.y();
254
255 // Apply rotation
256 const Standard_ShortReal aSin = std::sin (
257 -theParams->Rotation() * static_cast<Standard_ShortReal> (M_PI / 180.0));
258 const Standard_ShortReal aCos = std::cos (
259 -theParams->Rotation() * static_cast<Standard_ShortReal> (M_PI / 180.0));
260
261 BVH_Mat4f aRotationMat;
262 aRotationMat.SetValue (0, 0, aCos);
263 aRotationMat.SetValue (1, 1, aCos);
264 aRotationMat.SetValue (0, 1, -aSin);
265 aRotationMat.SetValue (1, 0, aSin);
266
267 theMatrix = theMatrix * aRotationMat;
268}
269
270// =======================================================================
91c60b57 271// function : ConvertMaterial
e276548b 272// purpose : Creates ray-tracing material properties
273// =======================================================================
91c60b57 274OpenGl_RaytraceMaterial OpenGl_View::convertMaterial (const OpenGl_AspectFace* theAspect,
275 const Handle(OpenGl_Context)& theGlContext)
e276548b 276{
91c60b57 277 OpenGl_RaytraceMaterial theMaterial;
25ef750e 278
91c60b57 279 const OPENGL_SURF_PROP& aProperties = theAspect->IntFront();
25ef750e 280
91c60b57 281 theMaterial.Ambient = BVH_Vec4f (
282 (aProperties.isphysic ? aProperties.ambcol.rgb[0] : aProperties.matcol.rgb[0]) * aProperties.amb,
283 (aProperties.isphysic ? aProperties.ambcol.rgb[1] : aProperties.matcol.rgb[1]) * aProperties.amb,
284 (aProperties.isphysic ? aProperties.ambcol.rgb[2] : aProperties.matcol.rgb[2]) * aProperties.amb,
285 1.f);
25ef750e 286
91c60b57 287 theMaterial.Diffuse = BVH_Vec4f (
288 (aProperties.isphysic ? aProperties.difcol.rgb[0] : aProperties.matcol.rgb[0]) * aProperties.diff,
289 (aProperties.isphysic ? aProperties.difcol.rgb[1] : aProperties.matcol.rgb[1]) * aProperties.diff,
290 (aProperties.isphysic ? aProperties.difcol.rgb[2] : aProperties.matcol.rgb[2]) * aProperties.diff,
291 -1.f /* no texture */);
25ef750e 292
293 theMaterial.Specular = BVH_Vec4f (
294 (aProperties.isphysic ? aProperties.speccol.rgb[0] : 1.f) * aProperties.spec,
295 (aProperties.isphysic ? aProperties.speccol.rgb[1] : 1.f) * aProperties.spec,
296 (aProperties.isphysic ? aProperties.speccol.rgb[2] : 1.f) * aProperties.spec,
297 aProperties.shine);
298
91c60b57 299 theMaterial.Emission = BVH_Vec4f (
300 (aProperties.isphysic ? aProperties.emscol.rgb[0] : aProperties.matcol.rgb[0]) * aProperties.emsv,
301 (aProperties.isphysic ? aProperties.emscol.rgb[1] : aProperties.matcol.rgb[1]) * aProperties.emsv,
302 (aProperties.isphysic ? aProperties.emscol.rgb[2] : aProperties.matcol.rgb[2]) * aProperties.emsv,
303 1.f);
e276548b 304
91c60b57 305 theMaterial.Transparency = BVH_Vec4f (aProperties.trans,
25ef750e 306 1.f - aProperties.trans,
307 aProperties.index == 0 ? 1.f : aProperties.index,
308 aProperties.index == 0 ? 1.f : 1.f / aProperties.index);
e276548b 309
25ef750e 310 const Standard_ShortReal aMaxRefl = Max (theMaterial.Diffuse.x() + theMaterial.Specular.x(),
311 Max (theMaterial.Diffuse.y() + theMaterial.Specular.y(),
312 theMaterial.Diffuse.z() + theMaterial.Specular.z()));
e276548b 313
25ef750e 314 const Standard_ShortReal aReflectionScale = 0.75f / aMaxRefl;
315
316 theMaterial.Reflection = BVH_Vec4f (
317 aProperties.speccol.rgb[0] * aProperties.spec * aReflectionScale,
318 aProperties.speccol.rgb[1] * aProperties.spec * aReflectionScale,
319 aProperties.speccol.rgb[2] * aProperties.spec * aReflectionScale,
320 0.f);
321
322 if (theAspect->DoTextureMap())
323 {
91c60b57 324 if (theGlContext->arbTexBindless != NULL)
25ef750e 325 {
91c60b57 326 BuildTextureTransform (theAspect->TextureParams(), theMaterial.TextureTransform);
327
328 // write texture ID in the w-component
25ef750e 329 theMaterial.Diffuse.w() = static_cast<Standard_ShortReal> (
91c60b57 330 myRaytraceGeometry.AddTexture (theAspect->TextureRes (theGlContext)));
25ef750e 331 }
332 else if (!myIsRaytraceWarnTextures)
333 {
91c60b57 334 const TCollection_ExtendedString aWarnMessage =
335 "Warning: texturing in Ray-Trace requires GL_ARB_bindless_texture extension which is missing. "
336 "Please try to update graphics card driver. At the moment textures will be ignored.";
337
338 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
339 GL_DEBUG_TYPE_PORTABILITY_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aWarnMessage);
340
25ef750e 341 myIsRaytraceWarnTextures = Standard_True;
342 }
343 }
e276548b 344
91c60b57 345 return theMaterial;
e276548b 346}
347
348// =======================================================================
349// function : AddRaytraceStructure
350// purpose : Adds OpenGL structure to ray-traced scene geometry
351// =======================================================================
91c60b57 352Standard_Boolean OpenGl_View::addRaytraceStructure (const OpenGl_Structure* theStructure,
353 const Handle(OpenGl_Context)& theGlContext)
e276548b 354{
e276548b 355 if (!theStructure->IsVisible())
356 {
357 myStructureStates[theStructure] = theStructure->ModificationState();
91c60b57 358
e276548b 359 return Standard_True;
360 }
361
362 // Get structure material
68333c8f 363 Standard_Integer aStructMatID = -1;
e276548b 364
365 if (theStructure->AspectFace() != NULL)
366 {
265d4508 367 aStructMatID = static_cast<Standard_Integer> (myRaytraceGeometry.Materials.size());
e276548b 368
91c60b57 369 OpenGl_RaytraceMaterial aStructMaterial = convertMaterial (theStructure->AspectFace(), theGlContext);
e276548b 370
265d4508 371 myRaytraceGeometry.Materials.push_back (aStructMaterial);
e276548b 372 }
373
91c60b57 374 Standard_ShortReal aStructTransform[16];
375
0717ddc1 376 if (theStructure->Transformation()->mat != NULL)
377 {
0717ddc1 378 for (Standard_Integer i = 0; i < 4; ++i)
379 {
380 for (Standard_Integer j = 0; j < 4; ++j)
381 {
382 aStructTransform[j * 4 + i] = theStructure->Transformation()->mat[i][j];
383 }
384 }
385 }
386
91c60b57 387 Standard_Boolean aResult = addRaytraceGroups (theStructure, aStructMatID,
388 theStructure->Transformation()->mat ? aStructTransform : NULL, theGlContext);
0717ddc1 389
390 // Process all connected OpenGL structures
391 for (OpenGl_ListOfStructure::Iterator anIts (theStructure->ConnectedStructures()); anIts.More(); anIts.Next())
392 {
393 if (anIts.Value()->IsRaytracable())
91c60b57 394 {
395 aResult &= addRaytraceGroups (anIts.Value(), aStructMatID,
396 theStructure->Transformation()->mat ? aStructTransform : NULL, theGlContext);
397 }
0717ddc1 398 }
399
400 myStructureStates[theStructure] = theStructure->ModificationState();
91c60b57 401
402 return aResult;
0717ddc1 403}
404
405// =======================================================================
406// function : AddRaytraceGroups
407// purpose : Adds OpenGL groups to ray-traced scene geometry
408// =======================================================================
91c60b57 409Standard_Boolean OpenGl_View::addRaytraceGroups (const OpenGl_Structure* theStructure,
410 const Standard_Integer theStructMat,
411 const Standard_ShortReal* theTransform,
412 const Handle(OpenGl_Context)& theGlContext)
0717ddc1 413{
b64d84be 414 for (OpenGl_Structure::GroupIterator aGroupIter (theStructure->DrawGroups()); aGroupIter.More(); aGroupIter.Next())
e276548b 415 {
416 // Get group material
68333c8f 417 Standard_Integer aGroupMatID = -1;
b64d84be 418 if (aGroupIter.Value()->AspectFace() != NULL)
e276548b 419 {
265d4508 420 aGroupMatID = static_cast<Standard_Integer> (myRaytraceGeometry.Materials.size());
e276548b 421
91c60b57 422 OpenGl_RaytraceMaterial aGroupMaterial = convertMaterial (
423 aGroupIter.Value()->AspectFace(), theGlContext);
e276548b 424
265d4508 425 myRaytraceGeometry.Materials.push_back (aGroupMaterial);
e276548b 426 }
427
91c60b57 428 Standard_Integer aMatID = aGroupMatID < 0 ? theStructMat : aGroupMatID;
265d4508 429 if (aMatID < 0)
e276548b 430 {
265d4508 431 aMatID = static_cast<Standard_Integer> (myRaytraceGeometry.Materials.size());
e276548b 432
265d4508 433 myRaytraceGeometry.Materials.push_back (OpenGl_RaytraceMaterial());
e276548b 434 }
435
265d4508 436 // Add OpenGL elements from group (extract primitives arrays and aspects)
b64d84be 437 for (const OpenGl_ElementNode* aNode = aGroupIter.Value()->FirstNode(); aNode != NULL; aNode = aNode->next)
e276548b 438 {
5322131b 439 OpenGl_AspectFace* anAspect = dynamic_cast<OpenGl_AspectFace*> (aNode->elem);
91c60b57 440
5322131b 441 if (anAspect != NULL)
e276548b 442 {
5322131b 443 aMatID = static_cast<Standard_Integer> (myRaytraceGeometry.Materials.size());
e276548b 444
91c60b57 445 OpenGl_RaytraceMaterial aMaterial = convertMaterial (anAspect, theGlContext);
e276548b 446
5322131b 447 myRaytraceGeometry.Materials.push_back (aMaterial);
e276548b 448 }
5322131b 449 else
e276548b 450 {
451 OpenGl_PrimitiveArray* aPrimArray = dynamic_cast<OpenGl_PrimitiveArray*> (aNode->elem);
84c71f29 452
e276548b 453 if (aPrimArray != NULL)
454 {
8d3f219f 455 std::map<Standard_Size, OpenGl_TriangleSet*>::iterator aSetIter = myArrayToTrianglesMap.find (aPrimArray->GetUID());
456
84c71f29 457 if (aSetIter != myArrayToTrianglesMap.end())
458 {
459 OpenGl_TriangleSet* aSet = aSetIter->second;
25ef750e 460
84c71f29 461 BVH_Transform<Standard_ShortReal, 4>* aTransform = new BVH_Transform<Standard_ShortReal, 4>();
462
463 if (theTransform != NULL)
464 {
465 aTransform->SetTransform (*(reinterpret_cast<const BVH_Mat4f*> (theTransform)));
466 }
91c60b57 467
84c71f29 468 aSet->SetProperties (aTransform);
469
25ef750e 470 if (aSet->MaterialIndex() != OpenGl_TriangleSet::INVALID_MATERIAL && aSet->MaterialIndex() != aMatID)
84c71f29 471 {
472 aSet->SetMaterialIndex (aMatID);
473 }
474 }
475 else
476 {
25ef750e 477 NCollection_Handle<BVH_Object<Standard_ShortReal, 3> > aSet =
91c60b57 478 addRaytracePrimitiveArray (aPrimArray, aMatID, 0);
265d4508 479
84c71f29 480 if (!aSet.IsNull())
481 {
482 BVH_Transform<Standard_ShortReal, 4>* aTransform = new BVH_Transform<Standard_ShortReal, 4>;
483
484 if (theTransform != NULL)
485 {
486 aTransform->SetTransform (*(reinterpret_cast<const BVH_Mat4f*> (theTransform)));
487 }
488
489 aSet->SetProperties (aTransform);
490
491 myRaytraceGeometry.Objects().Append (aSet);
492 }
493 }
e276548b 494 }
495 }
496 }
e276548b 497 }
498
e276548b 499 return Standard_True;
500}
501
502// =======================================================================
503// function : AddRaytracePrimitiveArray
504// purpose : Adds OpenGL primitive array to ray-traced scene geometry
505// =======================================================================
91c60b57 506OpenGl_TriangleSet* OpenGl_View::addRaytracePrimitiveArray (const OpenGl_PrimitiveArray* theArray,
507 const Standard_Integer theMaterial,
508 const OpenGl_Mat4* theTransform)
e276548b 509{
91c60b57 510 const Handle(Graphic3d_BoundBuffer)& aBounds = theArray->Bounds();
871fa103 511 const Handle(Graphic3d_IndexBuffer)& anIndices = theArray->Indices();
512 const Handle(Graphic3d_Buffer)& anAttribs = theArray->Attributes();
91c60b57 513
871fa103 514 if (theArray->DrawMode() < GL_TRIANGLES
91c60b57 515 #ifndef GL_ES_VERSION_2_0
871fa103 516 || theArray->DrawMode() > GL_POLYGON
ca3c13d1 517 #else
518 || theArray->DrawMode() > GL_TRIANGLE_FAN
519 #endif
871fa103 520 || anAttribs.IsNull())
e276548b 521 {
265d4508 522 return NULL;
e276548b 523 }
524
25ef750e 525 OpenGl_Mat4 aNormalMatrix;
526
527 if (theTransform != NULL)
528 {
529 Standard_ASSERT_RETURN (theTransform->Inverted (aNormalMatrix),
530 "Error: Failed to compute normal transformation matrix", NULL);
531
532 aNormalMatrix.Transpose();
533 }
534
8d3f219f 535 OpenGl_TriangleSet* aSet = new OpenGl_TriangleSet (theArray->GetUID());
e276548b 536 {
871fa103 537 aSet->Vertices.reserve (anAttribs->NbElements);
91c60b57 538 aSet->Normals.reserve (anAttribs->NbElements);
539 aSet->TexCrds.reserve (anAttribs->NbElements);
25ef750e 540
871fa103 541 const size_t aVertFrom = aSet->Vertices.size();
91c60b57 542
871fa103 543 for (Standard_Integer anAttribIter = 0; anAttribIter < anAttribs->NbAttributes; ++anAttribIter)
265d4508 544 {
871fa103 545 const Graphic3d_Attribute& anAttrib = anAttribs->Attribute (anAttribIter);
546 const size_t anOffset = anAttribs->AttributeOffset (anAttribIter);
547 if (anAttrib.Id == Graphic3d_TOA_POS)
548 {
549 if (anAttrib.DataType == Graphic3d_TOD_VEC3
550 || anAttrib.DataType == Graphic3d_TOD_VEC4)
551 {
552 for (Standard_Integer aVertIter = 0; aVertIter < anAttribs->NbElements; ++aVertIter)
553 {
25ef750e 554 aSet->Vertices.push_back (
91c60b57 555 *reinterpret_cast<const Graphic3d_Vec3*> (anAttribs->value (aVertIter) + anOffset));
871fa103 556 }
557 }
558 else if (anAttrib.DataType == Graphic3d_TOD_VEC2)
559 {
560 for (Standard_Integer aVertIter = 0; aVertIter < anAttribs->NbElements; ++aVertIter)
561 {
91c60b57 562 const Standard_ShortReal* aCoords =
563 reinterpret_cast<const Standard_ShortReal*> (anAttribs->value (aVertIter) + anOffset);
564
565 aSet->Vertices.push_back (BVH_Vec3f (aCoords[0], aCoords[1], 0.0f));
871fa103 566 }
567 }
568 }
569 else if (anAttrib.Id == Graphic3d_TOA_NORM)
570 {
571 if (anAttrib.DataType == Graphic3d_TOD_VEC3
572 || anAttrib.DataType == Graphic3d_TOD_VEC4)
573 {
574 for (Standard_Integer aVertIter = 0; aVertIter < anAttribs->NbElements; ++aVertIter)
575 {
25ef750e 576 aSet->Normals.push_back (
91c60b57 577 *reinterpret_cast<const Graphic3d_Vec3*> (anAttribs->value (aVertIter) + anOffset));
25ef750e 578 }
579 }
580 }
581 else if (anAttrib.Id == Graphic3d_TOA_UV)
582 {
583 if (anAttrib.DataType == Graphic3d_TOD_VEC2)
584 {
585 for (Standard_Integer aVertIter = 0; aVertIter < anAttribs->NbElements; ++aVertIter)
586 {
587 aSet->TexCrds.push_back (
91c60b57 588 *reinterpret_cast<const Graphic3d_Vec2*> (anAttribs->value (aVertIter) + anOffset));
871fa103 589 }
590 }
591 }
265d4508 592 }
e276548b 593
871fa103 594 if (aSet->Normals.size() != aSet->Vertices.size())
265d4508 595 {
871fa103 596 for (Standard_Integer aVertIter = 0; aVertIter < anAttribs->NbElements; ++aVertIter)
265d4508 597 {
25ef750e 598 aSet->Normals.push_back (BVH_Vec3f());
599 }
600 }
601
602 if (aSet->TexCrds.size() != aSet->Vertices.size())
603 {
604 for (Standard_Integer aVertIter = 0; aVertIter < anAttribs->NbElements; ++aVertIter)
605 {
606 aSet->TexCrds.push_back (BVH_Vec2f());
265d4508 607 }
871fa103 608 }
e276548b 609
25ef750e 610 if (theTransform != NULL)
871fa103 611 {
612 for (size_t aVertIter = aVertFrom; aVertIter < aSet->Vertices.size(); ++aVertIter)
613 {
25ef750e 614 BVH_Vec3f& aVertex = aSet->Vertices[aVertIter];
615
91c60b57 616 BVH_Vec4f aTransVertex = *theTransform *
617 BVH_Vec4f (aVertex.x(), aVertex.y(), aVertex.z(), 1.f);
25ef750e 618
619 aVertex = BVH_Vec3f (aTransVertex.x(), aTransVertex.y(), aTransVertex.z());
871fa103 620 }
621 for (size_t aVertIter = aVertFrom; aVertIter < aSet->Normals.size(); ++aVertIter)
622 {
25ef750e 623 BVH_Vec3f& aNormal = aSet->Normals[aVertIter];
624
91c60b57 625 BVH_Vec4f aTransNormal = aNormalMatrix *
626 BVH_Vec4f (aNormal.x(), aNormal.y(), aNormal.z(), 0.f);
25ef750e 627
628 aNormal = BVH_Vec3f (aTransNormal.x(), aTransNormal.y(), aTransNormal.z());
871fa103 629 }
e276548b 630 }
631
871fa103 632 if (!aBounds.IsNull())
265d4508 633 {
91c60b57 634 for (Standard_Integer aBound = 0, aBoundStart = 0; aBound < aBounds->NbBounds; ++aBound)
265d4508 635 {
871fa103 636 const Standard_Integer aVertNum = aBounds->Bounds[aBound];
e276548b 637
91c60b57 638 if (!addRaytraceVertexIndices (*aSet, theMaterial, aVertNum, aBoundStart, *theArray))
265d4508 639 {
640 delete aSet;
641 return NULL;
642 }
643
644 aBoundStart += aVertNum;
645 }
646 }
647 else
e276548b 648 {
871fa103 649 const Standard_Integer aVertNum = !anIndices.IsNull() ? anIndices->NbElements : anAttribs->NbElements;
e276548b 650
91c60b57 651 if (!addRaytraceVertexIndices (*aSet, theMaterial, aVertNum, 0, *theArray))
e276548b 652 {
265d4508 653 delete aSet;
654 return NULL;
e276548b 655 }
e276548b 656 }
657 }
e276548b 658
265d4508 659 if (aSet->Size() != 0)
91c60b57 660 {
265d4508 661 aSet->MarkDirty();
91c60b57 662 }
e276548b 663
265d4508 664 return aSet;
e276548b 665}
666
667// =======================================================================
668// function : AddRaytraceVertexIndices
669// purpose : Adds vertex indices to ray-traced scene geometry
670// =======================================================================
91c60b57 671Standard_Boolean OpenGl_View::addRaytraceVertexIndices (OpenGl_TriangleSet& theSet,
672 const Standard_Integer theMatID,
673 const Standard_Integer theCount,
674 const Standard_Integer theOffset,
675 const OpenGl_PrimitiveArray& theArray)
e276548b 676{
871fa103 677 switch (theArray.DrawMode())
e276548b 678 {
91c60b57 679 case GL_TRIANGLES: return addRaytraceTriangleArray (theSet, theMatID, theCount, theOffset, theArray.Indices());
680 case GL_TRIANGLE_FAN: return addRaytraceTriangleFanArray (theSet, theMatID, theCount, theOffset, theArray.Indices());
681 case GL_TRIANGLE_STRIP: return addRaytraceTriangleStripArray (theSet, theMatID, theCount, theOffset, theArray.Indices());
ca3c13d1 682 #if !defined(GL_ES_VERSION_2_0)
91c60b57 683 case GL_QUAD_STRIP: return addRaytraceQuadrangleStripArray (theSet, theMatID, theCount, theOffset, theArray.Indices());
684 case GL_QUADS: return addRaytraceQuadrangleArray (theSet, theMatID, theCount, theOffset, theArray.Indices());
685 case GL_POLYGON: return addRaytracePolygonArray (theSet, theMatID, theCount, theOffset, theArray.Indices());
ca3c13d1 686 #endif
e276548b 687 }
91c60b57 688
871fa103 689 return Standard_False;
e276548b 690}
691
692// =======================================================================
693// function : AddRaytraceTriangleArray
694// purpose : Adds OpenGL triangle array to ray-traced scene geometry
695// =======================================================================
91c60b57 696Standard_Boolean OpenGl_View::addRaytraceTriangleArray (OpenGl_TriangleSet& theSet,
697 const Standard_Integer theMatID,
698 const Standard_Integer theCount,
699 const Standard_Integer theOffset,
700 const Handle(Graphic3d_IndexBuffer)& theIndices)
e276548b 701{
265d4508 702 if (theCount < 3)
91c60b57 703 {
e276548b 704 return Standard_True;
91c60b57 705 }
e276548b 706
871fa103 707 theSet.Elements.reserve (theSet.Elements.size() + theCount / 3);
265d4508 708
871fa103 709 if (!theIndices.IsNull())
e276548b 710 {
265d4508 711 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; aVert += 3)
e276548b 712 {
871fa103 713 theSet.Elements.push_back (BVH_Vec4i (theIndices->Index (aVert + 0),
714 theIndices->Index (aVert + 1),
715 theIndices->Index (aVert + 2),
716 theMatID));
e276548b 717 }
718 }
719 else
720 {
265d4508 721 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; aVert += 3)
e276548b 722 {
91c60b57 723 theSet.Elements.push_back (BVH_Vec4i (aVert + 0, aVert + 1, aVert + 2, theMatID));
e276548b 724 }
725 }
726
727 return Standard_True;
728}
729
730// =======================================================================
731// function : AddRaytraceTriangleFanArray
732// purpose : Adds OpenGL triangle fan array to ray-traced scene geometry
733// =======================================================================
91c60b57 734Standard_Boolean OpenGl_View::addRaytraceTriangleFanArray (OpenGl_TriangleSet& theSet,
735 const Standard_Integer theMatID,
736 const Standard_Integer theCount,
737 const Standard_Integer theOffset,
738 const Handle(Graphic3d_IndexBuffer)& theIndices)
e276548b 739{
265d4508 740 if (theCount < 3)
91c60b57 741 {
e276548b 742 return Standard_True;
91c60b57 743 }
e276548b 744
871fa103 745 theSet.Elements.reserve (theSet.Elements.size() + theCount - 2);
265d4508 746
871fa103 747 if (!theIndices.IsNull())
e276548b 748 {
265d4508 749 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; ++aVert)
e276548b 750 {
871fa103 751 theSet.Elements.push_back (BVH_Vec4i (theIndices->Index (theOffset),
752 theIndices->Index (aVert + 1),
753 theIndices->Index (aVert + 2),
754 theMatID));
e276548b 755 }
756 }
757 else
758 {
265d4508 759 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; ++aVert)
e276548b 760 {
871fa103 761 theSet.Elements.push_back (BVH_Vec4i (theOffset,
762 aVert + 1,
763 aVert + 2,
764 theMatID));
e276548b 765 }
766 }
767
768 return Standard_True;
769}
770
771// =======================================================================
772// function : AddRaytraceTriangleStripArray
773// purpose : Adds OpenGL triangle strip array to ray-traced scene geometry
774// =======================================================================
91c60b57 775Standard_Boolean OpenGl_View::addRaytraceTriangleStripArray (OpenGl_TriangleSet& theSet,
776 const Standard_Integer theMatID,
777 const Standard_Integer theCount,
778 const Standard_Integer theOffset,
779 const Handle(Graphic3d_IndexBuffer)& theIndices)
e276548b 780{
265d4508 781 if (theCount < 3)
91c60b57 782 {
e276548b 783 return Standard_True;
91c60b57 784 }
e276548b 785
871fa103 786 theSet.Elements.reserve (theSet.Elements.size() + theCount - 2);
265d4508 787
871fa103 788 if (!theIndices.IsNull())
e276548b 789 {
265d4508 790 for (Standard_Integer aVert = theOffset, aCW = 0; aVert < theOffset + theCount - 2; ++aVert, aCW = (aCW + 1) % 2)
e276548b 791 {
871fa103 792 theSet.Elements.push_back (BVH_Vec4i (theIndices->Index (aVert + aCW ? 1 : 0),
793 theIndices->Index (aVert + aCW ? 0 : 1),
794 theIndices->Index (aVert + 2),
795 theMatID));
e276548b 796 }
797 }
798 else
799 {
265d4508 800 for (Standard_Integer aVert = theOffset, aCW = 0; aVert < theOffset + theCount - 2; ++aVert, aCW = (aCW + 1) % 2)
e276548b 801 {
871fa103 802 theSet.Elements.push_back (BVH_Vec4i (aVert + aCW ? 1 : 0,
803 aVert + aCW ? 0 : 1,
804 aVert + 2,
805 theMatID));
e276548b 806 }
807 }
808
809 return Standard_True;
810}
811
812// =======================================================================
813// function : AddRaytraceQuadrangleArray
814// purpose : Adds OpenGL quad array to ray-traced scene geometry
815// =======================================================================
91c60b57 816Standard_Boolean OpenGl_View::addRaytraceQuadrangleArray (OpenGl_TriangleSet& theSet,
817 const Standard_Integer theMatID,
818 const Standard_Integer theCount,
819 const Standard_Integer theOffset,
820 const Handle(Graphic3d_IndexBuffer)& theIndices)
e276548b 821{
265d4508 822 if (theCount < 4)
91c60b57 823 {
e276548b 824 return Standard_True;
91c60b57 825 }
e276548b 826
871fa103 827 theSet.Elements.reserve (theSet.Elements.size() + theCount / 2);
265d4508 828
871fa103 829 if (!theIndices.IsNull())
e276548b 830 {
265d4508 831 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 3; aVert += 4)
e276548b 832 {
871fa103 833 theSet.Elements.push_back (BVH_Vec4i (theIndices->Index (aVert + 0),
834 theIndices->Index (aVert + 1),
835 theIndices->Index (aVert + 2),
836 theMatID));
837 theSet.Elements.push_back (BVH_Vec4i (theIndices->Index (aVert + 0),
838 theIndices->Index (aVert + 2),
839 theIndices->Index (aVert + 3),
840 theMatID));
e276548b 841 }
842 }
843 else
844 {
265d4508 845 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 3; aVert += 4)
e276548b 846 {
871fa103 847 theSet.Elements.push_back (BVH_Vec4i (aVert + 0, aVert + 1, aVert + 2,
848 theMatID));
849 theSet.Elements.push_back (BVH_Vec4i (aVert + 0, aVert + 2, aVert + 3,
850 theMatID));
e276548b 851 }
852 }
853
854 return Standard_True;
855}
856
857// =======================================================================
858// function : AddRaytraceQuadrangleStripArray
859// purpose : Adds OpenGL quad strip array to ray-traced scene geometry
860// =======================================================================
91c60b57 861Standard_Boolean OpenGl_View::addRaytraceQuadrangleStripArray (OpenGl_TriangleSet& theSet,
862 const Standard_Integer theMatID,
863 const Standard_Integer theCount,
864 const Standard_Integer theOffset,
865 const Handle(Graphic3d_IndexBuffer)& theIndices)
e276548b 866{
265d4508 867 if (theCount < 4)
91c60b57 868 {
e276548b 869 return Standard_True;
91c60b57 870 }
e276548b 871
871fa103 872 theSet.Elements.reserve (theSet.Elements.size() + 2 * theCount - 6);
265d4508 873
871fa103 874 if (!theIndices.IsNull())
e276548b 875 {
265d4508 876 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 3; aVert += 2)
e276548b 877 {
871fa103 878 theSet.Elements.push_back (BVH_Vec4i (theIndices->Index (aVert + 0),
879 theIndices->Index (aVert + 1),
880 theIndices->Index (aVert + 2),
881 theMatID));
882
883 theSet.Elements.push_back (BVH_Vec4i (theIndices->Index (aVert + 1),
884 theIndices->Index (aVert + 3),
885 theIndices->Index (aVert + 2),
886 theMatID));
e276548b 887 }
888 }
889 else
890 {
265d4508 891 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 3; aVert += 2)
e276548b 892 {
871fa103 893 theSet.Elements.push_back (BVH_Vec4i (aVert + 0,
894 aVert + 1,
895 aVert + 2,
896 theMatID));
897
898 theSet.Elements.push_back (BVH_Vec4i (aVert + 1,
899 aVert + 3,
900 aVert + 2,
901 theMatID));
e276548b 902 }
903 }
904
905 return Standard_True;
906}
907
908// =======================================================================
909// function : AddRaytracePolygonArray
910// purpose : Adds OpenGL polygon array to ray-traced scene geometry
911// =======================================================================
91c60b57 912Standard_Boolean OpenGl_View::addRaytracePolygonArray (OpenGl_TriangleSet& theSet,
913 const Standard_Integer theMatID,
914 const Standard_Integer theCount,
915 const Standard_Integer theOffset,
916 const Handle(Graphic3d_IndexBuffer)& theIndices)
e276548b 917{
265d4508 918 if (theCount < 3)
91c60b57 919 {
e276548b 920 return Standard_True;
91c60b57 921 }
e276548b 922
871fa103 923 theSet.Elements.reserve (theSet.Elements.size() + theCount - 2);
265d4508 924
871fa103 925 if (!theIndices.IsNull())
e276548b 926 {
265d4508 927 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; ++aVert)
e276548b 928 {
871fa103 929 theSet.Elements.push_back (BVH_Vec4i (theIndices->Index (theOffset),
930 theIndices->Index (aVert + 1),
931 theIndices->Index (aVert + 2),
932 theMatID));
e276548b 933 }
934 }
935 else
936 {
265d4508 937 for (Standard_Integer aVert = theOffset; aVert < theOffset + theCount - 2; ++aVert)
e276548b 938 {
871fa103 939 theSet.Elements.push_back (BVH_Vec4i (theOffset,
940 aVert + 1,
941 aVert + 2,
942 theMatID));
e276548b 943 }
944 }
945
946 return Standard_True;
947}
948
949// =======================================================================
fc73a202 950// function : Source
951// purpose : Returns shader source combined with prefix
e276548b 952// =======================================================================
91c60b57 953TCollection_AsciiString OpenGl_View::ShaderSource::Source() const
e276548b 954{
fc73a202 955 static const TCollection_AsciiString aVersion = "#version 140";
e276548b 956
fc73a202 957 if (myPrefix.IsEmpty())
e276548b 958 {
fc73a202 959 return aVersion + "\n" + mySource;
e276548b 960 }
961
fc73a202 962 return aVersion + "\n" + myPrefix + "\n" + mySource;
e276548b 963}
964
965// =======================================================================
fc73a202 966// function : Load
967// purpose : Loads shader source from specified files
e276548b 968// =======================================================================
91c60b57 969void OpenGl_View::ShaderSource::Load (const TCollection_AsciiString* theFileNames,
970 const Standard_Integer theCount)
e276548b 971{
fc73a202 972 mySource.Clear();
e276548b 973
fc73a202 974 for (Standard_Integer anIndex = 0; anIndex < theCount; ++anIndex)
975 {
976 OSD_File aFile (theFileNames[anIndex]);
e276548b 977
fc73a202 978 Standard_ASSERT_RETURN (aFile.Exists(),
979 "Error: Failed to find shader source file", /* none */);
e276548b 980
fc73a202 981 aFile.Open (OSD_ReadOnly, OSD_Protection());
265d4508 982
fc73a202 983 TCollection_AsciiString aSource;
265d4508 984
fc73a202 985 Standard_ASSERT_RETURN (aFile.IsOpen(),
986 "Error: Failed to open shader source file", /* none */);
e276548b 987
fc73a202 988 aFile.Read (aSource, (Standard_Integer) aFile.Size());
e276548b 989
fc73a202 990 if (!aSource.IsEmpty())
991 {
992 mySource += TCollection_AsciiString ("\n") + aSource;
993 }
68333c8f 994
fc73a202 995 aFile.Close();
68333c8f 996 }
e276548b 997}
998
999// =======================================================================
91c60b57 1000// function : GenerateShaderPrefix
1001// purpose : Generates shader prefix based on current ray-tracing options
1002// =======================================================================
1003TCollection_AsciiString OpenGl_View::generateShaderPrefix (const Handle(OpenGl_Context)& theGlContext) const
1004{
1005 TCollection_AsciiString aPrefixString =
1006 TCollection_AsciiString ("#define STACK_SIZE ") + TCollection_AsciiString (myRaytraceParameters.StackSize) + "\n" +
1007 TCollection_AsciiString ("#define NB_BOUNCES ") + TCollection_AsciiString (myRaytraceParameters.NbBounces);
1008
1009 if (myRaytraceParameters.TransparentShadows)
1010 {
1011 aPrefixString += TCollection_AsciiString ("\n#define TRANSPARENT_SHADOWS");
1012 }
1013
1014 // If OpenGL driver supports bindless textures,
1015 // activate texturing in ray-tracing mode
1016 if (theGlContext->arbTexBindless != NULL)
1017 {
1018 aPrefixString += TCollection_AsciiString ("\n#define USE_TEXTURES") +
1019 TCollection_AsciiString ("\n#define MAX_TEX_NUMBER ") + TCollection_AsciiString (OpenGl_RaytraceGeometry::MAX_TEX_NUMBER);
1020 }
1021
1022 return aPrefixString;
1023}
1024
1025// =======================================================================
1026// function : SafeFailBack
1027// purpose : Performs safe exit when shaders initialization fails
1028// =======================================================================
1029Standard_Boolean OpenGl_View::safeFailBack (const TCollection_ExtendedString& theMessage,
1030 const Handle(OpenGl_Context)& theGlContext)
1031{
1032 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
1033 GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, theMessage);
1034
1035 myRaytraceInitStatus = OpenGl_RT_FAIL;
1036
1037 releaseRaytraceResources (theGlContext);
1038
1039 return Standard_False;
1040}
1041
1042// =======================================================================
1043// function : InitShader
fc73a202 1044// purpose : Creates new shader object with specified source
e276548b 1045// =======================================================================
91c60b57 1046Handle(OpenGl_ShaderObject) OpenGl_View::initShader (const GLenum theType,
1047 const ShaderSource& theSource,
1048 const Handle(OpenGl_Context)& theGlContext)
e276548b 1049{
fc73a202 1050 Handle(OpenGl_ShaderObject) aShader = new OpenGl_ShaderObject (theType);
e276548b 1051
91c60b57 1052 if (!aShader->Create (theGlContext))
e276548b 1053 {
07039714 1054 const TCollection_ExtendedString aMessage = TCollection_ExtendedString ("Error: Failed to create ") +
1055 (theType == GL_VERTEX_SHADER ? "vertex" : "fragment") + " shader object";
1056
91c60b57 1057 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
fc73a202 1058 GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMessage);
e276548b 1059
91c60b57 1060 aShader->Release (theGlContext.operator->());
e276548b 1061
fc73a202 1062 return Handle(OpenGl_ShaderObject)();
e276548b 1063 }
5322131b 1064
91c60b57 1065 if (!aShader->LoadSource (theGlContext, theSource.Source()))
fc73a202 1066 {
07039714 1067 const TCollection_ExtendedString aMessage = TCollection_ExtendedString ("Error: Failed to set ") +
1068 (theType == GL_VERTEX_SHADER ? "vertex" : "fragment") + " shader source";
1069
91c60b57 1070 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
fc73a202 1071 GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMessage);
e276548b 1072
91c60b57 1073 aShader->Release (theGlContext.operator->());
e276548b 1074
fc73a202 1075 return Handle(OpenGl_ShaderObject)();
1076 }
e276548b 1077
fc73a202 1078 TCollection_AsciiString aBuildLog;
e276548b 1079
91c60b57 1080 if (!aShader->Compile (theGlContext))
e276548b 1081 {
91c60b57 1082 aShader->FetchInfoLog (theGlContext, aBuildLog);
fc73a202 1083
07039714 1084 const TCollection_ExtendedString aMessage = TCollection_ExtendedString ("Error: Failed to compile ") +
1085 (theType == GL_VERTEX_SHADER ? "vertex" : "fragment") + " shader object:\n" + aBuildLog;
1086
91c60b57 1087 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
07039714 1088 GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMessage);
e276548b 1089
91c60b57 1090 aShader->Release (theGlContext.operator->());
fc73a202 1091
1092 return Handle(OpenGl_ShaderObject)();
1093 }
91c60b57 1094 else if (theGlContext->caps->glslWarnings)
fc73a202 1095 {
91c60b57 1096 aShader->FetchInfoLog (theGlContext, aBuildLog);
07039714 1097
1098 if (!aBuildLog.IsEmpty() && !aBuildLog.IsEqual ("No errors.\n"))
fc73a202 1099 {
07039714 1100 const TCollection_ExtendedString aMessage = TCollection_ExtendedString (theType == GL_VERTEX_SHADER ?
1101 "Vertex" : "Fragment") + " shader was compiled with following warnings:\n" + aBuildLog;
1102
91c60b57 1103 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
07039714 1104 GL_DEBUG_TYPE_PORTABILITY_ARB, 0, GL_DEBUG_SEVERITY_LOW_ARB, aMessage);
fc73a202 1105 }
07039714 1106 }
e276548b 1107
fc73a202 1108 return aShader;
e276548b 1109}
1110
1111// =======================================================================
91c60b57 1112// function : InitRaytraceResources
1113// purpose : Initializes OpenGL/GLSL shader programs
e276548b 1114// =======================================================================
91c60b57 1115Standard_Boolean OpenGl_View::initRaytraceResources (const Graphic3d_CView& theCView, const Handle(OpenGl_Context)& theGlContext)
e276548b 1116{
91c60b57 1117 if (myRaytraceInitStatus == OpenGl_RT_FAIL)
1118 {
1119 return Standard_False;
1120 }
265d4508 1121
91c60b57 1122 Standard_Boolean aToRebuildShaders = Standard_False;
265d4508 1123
91c60b57 1124 if (myRaytraceInitStatus == OpenGl_RT_INIT)
1125 {
1126 if (!myIsRaytraceDataValid)
1127 return Standard_True;
07039714 1128
91c60b57 1129 const Standard_Integer aRequiredStackSize =
1130 myRaytraceGeometry.HighLevelTreeDepth() + myRaytraceGeometry.BottomLevelTreeDepth();
e276548b 1131
91c60b57 1132 if (myRaytraceParameters.StackSize < aRequiredStackSize)
1133 {
1134 myRaytraceParameters.StackSize = Max (aRequiredStackSize, THE_DEFAULT_STACK_SIZE);
e276548b 1135
fc73a202 1136 aToRebuildShaders = Standard_True;
1137 }
1138 else
1139 {
bc8c79bb 1140 if (aRequiredStackSize < myRaytraceParameters.StackSize)
fc73a202 1141 {
bc8c79bb 1142 if (myRaytraceParameters.StackSize > THE_DEFAULT_STACK_SIZE)
fc73a202 1143 {
bc8c79bb 1144 myRaytraceParameters.StackSize = Max (aRequiredStackSize, THE_DEFAULT_STACK_SIZE);
fc73a202 1145 aToRebuildShaders = Standard_True;
1146 }
1147 }
1148 }
e276548b 1149
25ef750e 1150 if (theCView.RenderParams.RaytracingDepth != myRaytraceParameters.NbBounces)
bc8c79bb 1151 {
25ef750e 1152 myRaytraceParameters.NbBounces = theCView.RenderParams.RaytracingDepth;
bc8c79bb 1153 aToRebuildShaders = Standard_True;
1154 }
1155
1156 if (theCView.RenderParams.IsTransparentShadowEnabled != myRaytraceParameters.TransparentShadows)
1157 {
1158 myRaytraceParameters.TransparentShadows = theCView.RenderParams.IsTransparentShadowEnabled;
1159 aToRebuildShaders = Standard_True;
1160 }
1161
fc73a202 1162 if (aToRebuildShaders)
1163 {
e276548b 1164#ifdef RAY_TRACE_PRINT_INFO
bc8c79bb 1165 std::cout << "Info: Rebuild shaders with stack size: " << myRaytraceParameters.StackSize << std::endl;
e276548b 1166#endif
e276548b 1167
bc8c79bb 1168 // Change state to force update all uniforms
91c60b57 1169 myToUpdateEnvironmentMap = Standard_True;
84c71f29 1170
91c60b57 1171 TCollection_AsciiString aPrefixString = generateShaderPrefix (theGlContext);
bc8c79bb 1172
1173#ifdef RAY_TRACE_PRINT_INFO
1174 std::cout << "GLSL prefix string:" << std::endl << aPrefixString << std::endl;
1175#endif
265d4508 1176
bc8c79bb 1177 myRaytraceShaderSource.SetPrefix (aPrefixString);
1178 myPostFSAAShaderSource.SetPrefix (aPrefixString);
265d4508 1179
91c60b57 1180 if (!myRaytraceShader->LoadSource (theGlContext, myRaytraceShaderSource.Source())
1181 || !myPostFSAAShader->LoadSource (theGlContext, myPostFSAAShaderSource.Source()))
fc73a202 1182 {
91c60b57 1183 return safeFailBack ("Failed to load source into ray-tracing fragment shaders", theGlContext);
fc73a202 1184 }
265d4508 1185
91c60b57 1186 if (!myRaytraceShader->Compile (theGlContext)
1187 || !myPostFSAAShader->Compile (theGlContext))
fc73a202 1188 {
91c60b57 1189 return safeFailBack ("Failed to compile ray-tracing fragment shaders", theGlContext);
fc73a202 1190 }
1191
91c60b57 1192 myRaytraceProgram->SetAttributeName (theGlContext, Graphic3d_TOA_POS, "occVertex");
1193 myPostFSAAProgram->SetAttributeName (theGlContext, Graphic3d_TOA_POS, "occVertex");
1194 if (!myRaytraceProgram->Link (theGlContext)
1195 || !myPostFSAAProgram->Link (theGlContext))
fc73a202 1196 {
91c60b57 1197 return safeFailBack ("Failed to initialize vertex attributes for ray-tracing program", theGlContext);
fc73a202 1198 }
1199 }
e276548b 1200 }
1201
91c60b57 1202 if (myRaytraceInitStatus == OpenGl_RT_NONE)
fc73a202 1203 {
91c60b57 1204 if (!theGlContext->IsGlGreaterEqual (3, 1))
fc73a202 1205 {
91c60b57 1206 return safeFailBack ("Ray-tracing requires OpenGL 3.1 and higher", theGlContext);
25ef750e 1207 }
91c60b57 1208 else if (!theGlContext->arbTboRGB32)
25ef750e 1209 {
91c60b57 1210 return safeFailBack ("Ray-tracing requires OpenGL 4.0+ or GL_ARB_texture_buffer_object_rgb32 extension", theGlContext);
1211 }
1212 else if (!theGlContext->arbFBOBlit)
1213 {
1214 return safeFailBack ("Ray-tracing requires EXT_framebuffer_blit extension", theGlContext);
fc73a202 1215 }
5322131b 1216
25ef750e 1217 myRaytraceParameters.NbBounces = theCView.RenderParams.RaytracingDepth;
bc8c79bb 1218
fc73a202 1219 TCollection_AsciiString aFolder = Graphic3d_ShaderProgram::ShadersFolder();
265d4508 1220
fc73a202 1221 if (aFolder.IsEmpty())
1222 {
91c60b57 1223 return safeFailBack ("Failed to locate shaders directory", theGlContext);
fc73a202 1224 }
265d4508 1225
fc73a202 1226 if (myIsRaytraceDataValid)
1227 {
bc8c79bb 1228 myRaytraceParameters.StackSize = Max (THE_DEFAULT_STACK_SIZE,
fc73a202 1229 myRaytraceGeometry.HighLevelTreeDepth() + myRaytraceGeometry.BottomLevelTreeDepth());
1230 }
265d4508 1231
91c60b57 1232 TCollection_AsciiString aPrefixString = generateShaderPrefix (theGlContext);
bc8c79bb 1233
1234#ifdef RAY_TRACE_PRINT_INFO
1235 std::cout << "GLSL prefix string:" << std::endl << aPrefixString << std::endl;
1236#endif
1237
fc73a202 1238 {
91c60b57 1239 Handle(OpenGl_ShaderObject) aBasicVertShader = initShader (
1240 GL_VERTEX_SHADER, ShaderSource (aFolder + "/RaytraceBase.vs"), theGlContext);
265d4508 1241
fc73a202 1242 if (aBasicVertShader.IsNull())
1243 {
91c60b57 1244 return safeFailBack ("Failed to initialize ray-trace vertex shader", theGlContext);
fc73a202 1245 }
265d4508 1246
07039714 1247 TCollection_AsciiString aFiles[] = { aFolder + "/RaytraceBase.fs",
1248 aFolder + "/RaytraceRender.fs" };
265d4508 1249
fc73a202 1250 myRaytraceShaderSource.Load (aFiles, 2);
e276548b 1251
bc8c79bb 1252 myRaytraceShaderSource.SetPrefix (aPrefixString);
265d4508 1253
91c60b57 1254 myRaytraceShader = initShader (GL_FRAGMENT_SHADER, myRaytraceShaderSource, theGlContext);
265d4508 1255
fc73a202 1256 if (myRaytraceShader.IsNull())
1257 {
91c60b57 1258 aBasicVertShader->Release (theGlContext.operator->());
265d4508 1259
91c60b57 1260 return safeFailBack ("Failed to initialize ray-trace fragment shader", theGlContext);
fc73a202 1261 }
265d4508 1262
fc73a202 1263 myRaytraceProgram = new OpenGl_ShaderProgram;
5322131b 1264
91c60b57 1265 if (!myRaytraceProgram->Create (theGlContext))
fc73a202 1266 {
91c60b57 1267 aBasicVertShader->Release (theGlContext.operator->());
265d4508 1268
91c60b57 1269 return safeFailBack ("Failed to create ray-trace shader program", theGlContext);
fc73a202 1270 }
265d4508 1271
91c60b57 1272 if (!myRaytraceProgram->AttachShader (theGlContext, aBasicVertShader)
1273 || !myRaytraceProgram->AttachShader (theGlContext, myRaytraceShader))
fc73a202 1274 {
91c60b57 1275 aBasicVertShader->Release (theGlContext.operator->());
265d4508 1276
91c60b57 1277 return safeFailBack ("Failed to attach ray-trace shader objects", theGlContext);
fc73a202 1278 }
265d4508 1279
91c60b57 1280 myRaytraceProgram->SetAttributeName (theGlContext, Graphic3d_TOA_POS, "occVertex");
07039714 1281
1282 TCollection_AsciiString aLinkLog;
1283
91c60b57 1284 if (!myRaytraceProgram->Link (theGlContext))
fc73a202 1285 {
91c60b57 1286 myRaytraceProgram->FetchInfoLog (theGlContext, aLinkLog);
07039714 1287
91c60b57 1288 return safeFailBack (TCollection_ExtendedString (
1289 "Failed to link ray-trace shader program:\n") + aLinkLog, theGlContext);
07039714 1290 }
91c60b57 1291 else if (theGlContext->caps->glslWarnings)
07039714 1292 {
91c60b57 1293 myRaytraceProgram->FetchInfoLog (theGlContext, aLinkLog);
e276548b 1294
07039714 1295 if (!aLinkLog.IsEmpty() && !aLinkLog.IsEqual ("No errors.\n"))
fc73a202 1296 {
07039714 1297 const TCollection_ExtendedString aMessage = TCollection_ExtendedString (
1298 "Ray-trace shader program was linked with following warnings:\n") + aLinkLog;
265d4508 1299
91c60b57 1300 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
07039714 1301 GL_DEBUG_TYPE_PORTABILITY_ARB, 0, GL_DEBUG_SEVERITY_LOW_ARB, aMessage);
1302 }
fc73a202 1303 }
1304 }
265d4508 1305
fc73a202 1306 {
91c60b57 1307 Handle(OpenGl_ShaderObject) aBasicVertShader = initShader (
1308 GL_VERTEX_SHADER, ShaderSource (aFolder + "/RaytraceBase.vs"), theGlContext);
265d4508 1309
fc73a202 1310 if (aBasicVertShader.IsNull())
1311 {
91c60b57 1312 return safeFailBack ("Failed to initialize FSAA vertex shader", theGlContext);
fc73a202 1313 }
265d4508 1314
07039714 1315 TCollection_AsciiString aFiles[] = { aFolder + "/RaytraceBase.fs",
1316 aFolder + "/RaytraceSmooth.fs" };
265d4508 1317
fc73a202 1318 myPostFSAAShaderSource.Load (aFiles, 2);
265d4508 1319
bc8c79bb 1320 myPostFSAAShaderSource.SetPrefix (aPrefixString);
fc73a202 1321
91c60b57 1322 myPostFSAAShader = initShader (GL_FRAGMENT_SHADER, myPostFSAAShaderSource, theGlContext);
265d4508 1323
fc73a202 1324 if (myPostFSAAShader.IsNull())
1325 {
91c60b57 1326 aBasicVertShader->Release (theGlContext.operator->());
265d4508 1327
91c60b57 1328 return safeFailBack ("Failed to initialize FSAA fragment shader", theGlContext);
fc73a202 1329 }
265d4508 1330
fc73a202 1331 myPostFSAAProgram = new OpenGl_ShaderProgram;
265d4508 1332
91c60b57 1333 if (!myPostFSAAProgram->Create (theGlContext))
fc73a202 1334 {
91c60b57 1335 aBasicVertShader->Release (theGlContext.operator->());
265d4508 1336
91c60b57 1337 return safeFailBack ("Failed to create FSAA shader program", theGlContext);
fc73a202 1338 }
1339
91c60b57 1340 if (!myPostFSAAProgram->AttachShader (theGlContext, aBasicVertShader)
1341 || !myPostFSAAProgram->AttachShader (theGlContext, myPostFSAAShader))
fc73a202 1342 {
91c60b57 1343 aBasicVertShader->Release (theGlContext.operator->());
fc73a202 1344
91c60b57 1345 return safeFailBack ("Failed to attach FSAA shader objects", theGlContext);
fc73a202 1346 }
1347
91c60b57 1348 myPostFSAAProgram->SetAttributeName (theGlContext, Graphic3d_TOA_POS, "occVertex");
07039714 1349
1350 TCollection_AsciiString aLinkLog;
1351
91c60b57 1352 if (!myPostFSAAProgram->Link (theGlContext))
fc73a202 1353 {
91c60b57 1354 myPostFSAAProgram->FetchInfoLog (theGlContext, aLinkLog);
07039714 1355
91c60b57 1356 return safeFailBack (TCollection_ExtendedString (
1357 "Failed to link FSAA shader program:\n") + aLinkLog, theGlContext);
07039714 1358 }
91c60b57 1359 else if (theGlContext->caps->glslWarnings)
07039714 1360 {
91c60b57 1361 myPostFSAAProgram->FetchInfoLog (theGlContext, aLinkLog);
fc73a202 1362
07039714 1363 if (!aLinkLog.IsEmpty() && !aLinkLog.IsEqual ("No errors.\n"))
fc73a202 1364 {
07039714 1365 const TCollection_ExtendedString aMessage = TCollection_ExtendedString (
1366 "FSAA shader program was linked with following warnings:\n") + aLinkLog;
1367
91c60b57 1368 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
07039714 1369 GL_DEBUG_TYPE_PORTABILITY_ARB, 0, GL_DEBUG_SEVERITY_LOW_ARB, aMessage);
fc73a202 1370 }
fc73a202 1371 }
e276548b 1372 }
fc73a202 1373 }
e276548b 1374
91c60b57 1375 if (myRaytraceInitStatus == OpenGl_RT_NONE || aToRebuildShaders)
fc73a202 1376 {
1377 for (Standard_Integer anIndex = 0; anIndex < 2; ++anIndex)
e276548b 1378 {
fc73a202 1379 Handle(OpenGl_ShaderProgram)& aShaderProgram =
1380 (anIndex == 0) ? myRaytraceProgram : myPostFSAAProgram;
1381
91c60b57 1382 theGlContext->BindProgram (aShaderProgram);
fc73a202 1383
91c60b57 1384 aShaderProgram->SetSampler (theGlContext,
fc73a202 1385 "uSceneMinPointTexture", OpenGl_RT_SceneMinPointTexture);
91c60b57 1386 aShaderProgram->SetSampler (theGlContext,
fc73a202 1387 "uSceneMaxPointTexture", OpenGl_RT_SceneMaxPointTexture);
91c60b57 1388 aShaderProgram->SetSampler (theGlContext,
fc73a202 1389 "uSceneNodeInfoTexture", OpenGl_RT_SceneNodeInfoTexture);
91c60b57 1390 aShaderProgram->SetSampler (theGlContext,
fc73a202 1391 "uGeometryVertexTexture", OpenGl_RT_GeometryVertexTexture);
91c60b57 1392 aShaderProgram->SetSampler (theGlContext,
fc73a202 1393 "uGeometryNormalTexture", OpenGl_RT_GeometryNormalTexture);
91c60b57 1394 aShaderProgram->SetSampler (theGlContext,
25ef750e 1395 "uGeometryTexCrdTexture", OpenGl_RT_GeometryTexCrdTexture);
91c60b57 1396 aShaderProgram->SetSampler (theGlContext,
25ef750e 1397 "uGeometryTriangTexture", OpenGl_RT_GeometryTriangTexture);
91c60b57 1398 aShaderProgram->SetSampler (theGlContext,
84c71f29 1399 "uSceneTransformTexture", OpenGl_RT_SceneTransformTexture);
91c60b57 1400 aShaderProgram->SetSampler (theGlContext,
84c71f29 1401 "uEnvironmentMapTexture", OpenGl_RT_EnvironmentMapTexture);
91c60b57 1402 aShaderProgram->SetSampler (theGlContext,
25ef750e 1403 "uRaytraceMaterialTexture", OpenGl_RT_RaytraceMaterialTexture);
91c60b57 1404 aShaderProgram->SetSampler (theGlContext,
25ef750e 1405 "uRaytraceLightSrcTexture", OpenGl_RT_RaytraceLightSrcTexture);
fc73a202 1406
91c60b57 1407 aShaderProgram->SetSampler (theGlContext,
a89742cf 1408 "uOpenGlColorTexture", OpenGl_RT_OpenGlColorTexture);
91c60b57 1409 aShaderProgram->SetSampler (theGlContext,
a89742cf 1410 "uOpenGlDepthTexture", OpenGl_RT_OpenGlDepthTexture);
1411
fc73a202 1412 if (anIndex == 1)
1413 {
91c60b57 1414 aShaderProgram->SetSampler (theGlContext,
fc73a202 1415 "uFSAAInputTexture", OpenGl_RT_FSAAInputTexture);
1416 }
265d4508 1417
fc73a202 1418 myUniformLocations[anIndex][OpenGl_RT_aPosition] =
91c60b57 1419 aShaderProgram->GetAttributeLocation (theGlContext, "occVertex");
fc73a202 1420
1421 myUniformLocations[anIndex][OpenGl_RT_uOriginLB] =
91c60b57 1422 aShaderProgram->GetUniformLocation (theGlContext, "uOriginLB");
fc73a202 1423 myUniformLocations[anIndex][OpenGl_RT_uOriginRB] =
91c60b57 1424 aShaderProgram->GetUniformLocation (theGlContext, "uOriginRB");
fc73a202 1425 myUniformLocations[anIndex][OpenGl_RT_uOriginLT] =
91c60b57 1426 aShaderProgram->GetUniformLocation (theGlContext, "uOriginLT");
fc73a202 1427 myUniformLocations[anIndex][OpenGl_RT_uOriginRT] =
91c60b57 1428 aShaderProgram->GetUniformLocation (theGlContext, "uOriginRT");
fc73a202 1429 myUniformLocations[anIndex][OpenGl_RT_uDirectLB] =
91c60b57 1430 aShaderProgram->GetUniformLocation (theGlContext, "uDirectLB");
fc73a202 1431 myUniformLocations[anIndex][OpenGl_RT_uDirectRB] =
91c60b57 1432 aShaderProgram->GetUniformLocation (theGlContext, "uDirectRB");
fc73a202 1433 myUniformLocations[anIndex][OpenGl_RT_uDirectLT] =
91c60b57 1434 aShaderProgram->GetUniformLocation (theGlContext, "uDirectLT");
fc73a202 1435 myUniformLocations[anIndex][OpenGl_RT_uDirectRT] =
91c60b57 1436 aShaderProgram->GetUniformLocation (theGlContext, "uDirectRT");
25ef750e 1437 myUniformLocations[anIndex][OpenGl_RT_uUnviewMat] =
91c60b57 1438 aShaderProgram->GetUniformLocation (theGlContext, "uUnviewMat");
fc73a202 1439
1440 myUniformLocations[anIndex][OpenGl_RT_uSceneRad] =
91c60b57 1441 aShaderProgram->GetUniformLocation (theGlContext, "uSceneRadius");
fc73a202 1442 myUniformLocations[anIndex][OpenGl_RT_uSceneEps] =
91c60b57 1443 aShaderProgram->GetUniformLocation (theGlContext, "uSceneEpsilon");
25ef750e 1444 myUniformLocations[anIndex][OpenGl_RT_uLightCount] =
91c60b57 1445 aShaderProgram->GetUniformLocation (theGlContext, "uLightCount");
25ef750e 1446 myUniformLocations[anIndex][OpenGl_RT_uLightAmbnt] =
91c60b57 1447 aShaderProgram->GetUniformLocation (theGlContext, "uGlobalAmbient");
fc73a202 1448
1449 myUniformLocations[anIndex][OpenGl_RT_uOffsetX] =
91c60b57 1450 aShaderProgram->GetUniformLocation (theGlContext, "uOffsetX");
fc73a202 1451 myUniformLocations[anIndex][OpenGl_RT_uOffsetY] =
91c60b57 1452 aShaderProgram->GetUniformLocation (theGlContext, "uOffsetY");
fc73a202 1453 myUniformLocations[anIndex][OpenGl_RT_uSamples] =
91c60b57 1454 aShaderProgram->GetUniformLocation (theGlContext, "uSamples");
312a4043 1455 myUniformLocations[anIndex][OpenGl_RT_uWinSizeX] =
91c60b57 1456 aShaderProgram->GetUniformLocation (theGlContext, "uWinSizeX");
312a4043 1457 myUniformLocations[anIndex][OpenGl_RT_uWinSizeY] =
91c60b57 1458 aShaderProgram->GetUniformLocation (theGlContext, "uWinSizeY");
84c71f29 1459
25ef750e 1460 myUniformLocations[anIndex][OpenGl_RT_uTextures] =
91c60b57 1461 aShaderProgram->GetUniformLocation (theGlContext, "uTextureSamplers");
25ef750e 1462
1463 myUniformLocations[anIndex][OpenGl_RT_uShadEnabled] =
91c60b57 1464 aShaderProgram->GetUniformLocation (theGlContext, "uShadowsEnable");
25ef750e 1465 myUniformLocations[anIndex][OpenGl_RT_uReflEnabled] =
91c60b57 1466 aShaderProgram->GetUniformLocation (theGlContext, "uReflectionsEnable");
25ef750e 1467 myUniformLocations[anIndex][OpenGl_RT_uEnvMapEnable] =
91c60b57 1468 aShaderProgram->GetUniformLocation (theGlContext, "uEnvironmentEnable");
fc73a202 1469 }
265d4508 1470
91c60b57 1471 theGlContext->BindProgram (NULL);
fc73a202 1472 }
265d4508 1473
91c60b57 1474 if (myRaytraceInitStatus != OpenGl_RT_NONE)
fc73a202 1475 {
91c60b57 1476 return myRaytraceInitStatus == OpenGl_RT_INIT;
fc73a202 1477 }
265d4508 1478
fc73a202 1479 if (myRaytraceFBO1.IsNull())
1480 {
1481 myRaytraceFBO1 = new OpenGl_FrameBuffer;
1482 }
265d4508 1483
fc73a202 1484 if (myRaytraceFBO2.IsNull())
1485 {
1486 myRaytraceFBO2 = new OpenGl_FrameBuffer;
1487 }
265d4508 1488
fc73a202 1489 const GLfloat aVertices[] = { -1.f, -1.f, 0.f,
1490 -1.f, 1.f, 0.f,
1491 1.f, 1.f, 0.f,
1492 1.f, 1.f, 0.f,
1493 1.f, -1.f, 0.f,
1494 -1.f, -1.f, 0.f };
265d4508 1495
91c60b57 1496 myRaytraceScreenQuad.Init (theGlContext, 3, 6, aVertices);
265d4508 1497
91c60b57 1498 myRaytraceInitStatus = OpenGl_RT_INIT; // initialized in normal way
ca3c13d1 1499
fc73a202 1500 return Standard_True;
1501}
265d4508 1502
fc73a202 1503// =======================================================================
1504// function : NullifyResource
1505// purpose :
1506// =======================================================================
91c60b57 1507inline void NullifyResource (const Handle(OpenGl_Context)& theGlContext,
fc73a202 1508 Handle(OpenGl_Resource)& theResource)
1509{
1510 if (!theResource.IsNull())
1511 {
91c60b57 1512 theResource->Release (theGlContext.operator->());
fc73a202 1513 theResource.Nullify();
1514 }
1515}
265d4508 1516
fc73a202 1517// =======================================================================
1518// function : ReleaseRaytraceResources
1519// purpose : Releases OpenGL/GLSL shader programs
1520// =======================================================================
91c60b57 1521void OpenGl_View::releaseRaytraceResources (const Handle(OpenGl_Context)& theGlContext)
fc73a202 1522{
91c60b57 1523 NullifyResource (theGlContext, myOpenGlFBO);
1524 NullifyResource (theGlContext, myRaytraceFBO1);
1525 NullifyResource (theGlContext, myRaytraceFBO2);
265d4508 1526
91c60b57 1527 NullifyResource (theGlContext, myRaytraceShader);
1528 NullifyResource (theGlContext, myPostFSAAShader);
265d4508 1529
91c60b57 1530 NullifyResource (theGlContext, myRaytraceProgram);
1531 NullifyResource (theGlContext, myPostFSAAProgram);
265d4508 1532
91c60b57 1533 NullifyResource (theGlContext, mySceneNodeInfoTexture);
1534 NullifyResource (theGlContext, mySceneMinPointTexture);
1535 NullifyResource (theGlContext, mySceneMaxPointTexture);
265d4508 1536
91c60b57 1537 NullifyResource (theGlContext, myGeometryVertexTexture);
1538 NullifyResource (theGlContext, myGeometryNormalTexture);
1539 NullifyResource (theGlContext, myGeometryTexCrdTexture);
1540 NullifyResource (theGlContext, myGeometryTriangTexture);
1541 NullifyResource (theGlContext, mySceneTransformTexture);
265d4508 1542
91c60b57 1543 NullifyResource (theGlContext, myRaytraceLightSrcTexture);
1544 NullifyResource (theGlContext, myRaytraceMaterialTexture);
265d4508 1545
fc73a202 1546 if (myRaytraceScreenQuad.IsValid())
91c60b57 1547 myRaytraceScreenQuad.Release (theGlContext.operator->());
1548}
1549
1550// =======================================================================
1551// function : ResizeRaytraceBuffers
1552// purpose : Resizes OpenGL frame buffers
1553// =======================================================================
1554Standard_Boolean OpenGl_View::resizeRaytraceBuffers (const Standard_Integer theSizeX,
1555 const Standard_Integer theSizeY,
1556 const Handle(OpenGl_Context)& theGlContext)
1557{
1558 if (myRaytraceFBO1->GetVPSizeX() != theSizeX
1559 || myRaytraceFBO1->GetVPSizeY() != theSizeY)
1560 {
1561 myRaytraceFBO1->Init (theGlContext, theSizeX, theSizeY);
1562 myRaytraceFBO2->Init (theGlContext, theSizeX, theSizeY);
1563 }
1564
1565 return Standard_True;
1566}
1567
1568// =======================================================================
1569// function : UpdateCamera
1570// purpose : Generates viewing rays for corners of screen quad
1571// =======================================================================
1572void OpenGl_View::updateCamera (const OpenGl_Mat4& theOrientation,
1573 const OpenGl_Mat4& theViewMapping,
1574 OpenGl_Vec3* theOrigins,
1575 OpenGl_Vec3* theDirects,
1576 OpenGl_Mat4& theUnview)
1577{
1578 // compute inverse model-view-projection matrix
1579 (theViewMapping * theOrientation).Inverted (theUnview);
1580
1581 Standard_Integer aOriginIndex = 0;
1582 Standard_Integer aDirectIndex = 0;
1583
1584 for (Standard_Integer aY = -1; aY <= 1; aY += 2)
1585 {
1586 for (Standard_Integer aX = -1; aX <= 1; aX += 2)
1587 {
1588 OpenGl_Vec4 aOrigin (GLfloat(aX),
1589 GLfloat(aY),
1590 -1.0f,
1591 1.0f);
1592
1593 aOrigin = theUnview * aOrigin;
1594
1595 aOrigin.x() = aOrigin.x() / aOrigin.w();
1596 aOrigin.y() = aOrigin.y() / aOrigin.w();
1597 aOrigin.z() = aOrigin.z() / aOrigin.w();
1598
1599 OpenGl_Vec4 aDirect (GLfloat(aX),
1600 GLfloat(aY),
1601 1.0f,
1602 1.0f);
1603
1604 aDirect = theUnview * aDirect;
1605
1606 aDirect.x() = aDirect.x() / aDirect.w();
1607 aDirect.y() = aDirect.y() / aDirect.w();
1608 aDirect.z() = aDirect.z() / aDirect.w();
1609
1610 aDirect = aDirect - aOrigin;
1611
1612 GLdouble aInvLen = 1.0 / sqrt (aDirect.x() * aDirect.x() +
1613 aDirect.y() * aDirect.y() +
1614 aDirect.z() * aDirect.z());
1615
1616 theOrigins[aOriginIndex++] = OpenGl_Vec3 (static_cast<GLfloat> (aOrigin.x()),
1617 static_cast<GLfloat> (aOrigin.y()),
1618 static_cast<GLfloat> (aOrigin.z()));
1619
1620 theDirects[aDirectIndex++] = OpenGl_Vec3 (static_cast<GLfloat> (aDirect.x() * aInvLen),
1621 static_cast<GLfloat> (aDirect.y() * aInvLen),
1622 static_cast<GLfloat> (aDirect.z() * aInvLen));
1623 }
1624 }
fc73a202 1625}
265d4508 1626
fc73a202 1627// =======================================================================
1628// function : UploadRaytraceData
1629// purpose : Uploads ray-trace data to the GPU
1630// =======================================================================
91c60b57 1631Standard_Boolean OpenGl_View::uploadRaytraceData (const Handle(OpenGl_Context)& theGlContext)
fc73a202 1632{
91c60b57 1633 if (!theGlContext->IsGlGreaterEqual (3, 1))
fc73a202 1634 {
265d4508 1635#ifdef RAY_TRACE_PRINT_INFO
fc73a202 1636 std::cout << "Error: OpenGL version is less than 3.1" << std::endl;
265d4508 1637#endif
fc73a202 1638 return Standard_False;
e276548b 1639 }
1640
265d4508 1641 /////////////////////////////////////////////////////////////////////////////
25ef750e 1642 // Prepare OpenGL textures
1643
91c60b57 1644 if (theGlContext->arbTexBindless != NULL)
25ef750e 1645 {
1646 // If OpenGL driver supports bindless textures we need
1647 // to get unique 64- bit handles for using on the GPU
91c60b57 1648 if (!myRaytraceGeometry.UpdateTextureHandles (theGlContext))
25ef750e 1649 {
1650#ifdef RAY_TRACE_PRINT_INFO
1651 std::cout << "Error: Failed to get OpenGL texture handles" << std::endl;
1652#endif
1653 return Standard_False;
1654 }
1655 }
1656
1657 /////////////////////////////////////////////////////////////////////////////
e2da917a 1658 // Create OpenGL BVH buffers
265d4508 1659
e2da917a 1660 if (mySceneNodeInfoTexture.IsNull()) // create scene BVH buffers
e276548b 1661 {
e2da917a 1662 mySceneNodeInfoTexture = new OpenGl_TextureBufferArb;
1663 mySceneMinPointTexture = new OpenGl_TextureBufferArb;
1664 mySceneMaxPointTexture = new OpenGl_TextureBufferArb;
84c71f29 1665 mySceneTransformTexture = new OpenGl_TextureBufferArb;
265d4508 1666
91c60b57 1667 if (!mySceneNodeInfoTexture->Create (theGlContext)
1668 || !mySceneMinPointTexture->Create (theGlContext)
1669 || !mySceneMaxPointTexture->Create (theGlContext)
1670 || !mySceneTransformTexture->Create (theGlContext))
e276548b 1671 {
fc73a202 1672#ifdef RAY_TRACE_PRINT_INFO
e2da917a 1673 std::cout << "Error: Failed to create scene BVH buffers" << std::endl;
fc73a202 1674#endif
1675 return Standard_False;
1676 }
1677 }
5322131b 1678
e2da917a 1679 if (myGeometryVertexTexture.IsNull()) // create geometry buffers
265d4508 1680 {
fc73a202 1681 myGeometryVertexTexture = new OpenGl_TextureBufferArb;
1682 myGeometryNormalTexture = new OpenGl_TextureBufferArb;
25ef750e 1683 myGeometryTexCrdTexture = new OpenGl_TextureBufferArb;
fc73a202 1684 myGeometryTriangTexture = new OpenGl_TextureBufferArb;
e276548b 1685
91c60b57 1686 if (!myGeometryVertexTexture->Create (theGlContext)
1687 || !myGeometryNormalTexture->Create (theGlContext)
1688 || !myGeometryTexCrdTexture->Create (theGlContext)
1689 || !myGeometryTriangTexture->Create (theGlContext))
fc73a202 1690 {
1691#ifdef RAY_TRACE_PRINT_INFO
1692 std::cout << "Error: Failed to create buffers for triangulation data" << std::endl;
1693#endif
1694 return Standard_False;
1695 }
265d4508 1696 }
5322131b 1697
e2da917a 1698 if (myRaytraceMaterialTexture.IsNull()) // create material buffer
fc73a202 1699 {
1700 myRaytraceMaterialTexture = new OpenGl_TextureBufferArb;
e276548b 1701
91c60b57 1702 if (!myRaytraceMaterialTexture->Create (theGlContext))
fc73a202 1703 {
1704#ifdef RAY_TRACE_PRINT_INFO
1705 std::cout << "Error: Failed to create buffers for material data" << std::endl;
e276548b 1706#endif
fc73a202 1707 return Standard_False;
1708 }
1709 }
e2da917a 1710
84c71f29 1711 /////////////////////////////////////////////////////////////////////////////
1712 // Write transform buffer
1713
1714 BVH_Mat4f* aNodeTransforms = new BVH_Mat4f[myRaytraceGeometry.Size()];
84c71f29 1715
e2da917a 1716 bool aResult = true;
1717
84c71f29 1718 for (Standard_Integer anElemIndex = 0; anElemIndex < myRaytraceGeometry.Size(); ++anElemIndex)
1719 {
1720 OpenGl_TriangleSet* aTriangleSet = dynamic_cast<OpenGl_TriangleSet*> (
1721 myRaytraceGeometry.Objects().ChangeValue (anElemIndex).operator->());
1722
1723 const BVH_Transform<Standard_ShortReal, 4>* aTransform =
1724 dynamic_cast<const BVH_Transform<Standard_ShortReal, 4>* > (aTriangleSet->Properties().operator->());
1725
1726 Standard_ASSERT_RETURN (aTransform != NULL,
1727 "OpenGl_TriangleSet does not contain transform", Standard_False);
1728
1729 aNodeTransforms[anElemIndex] = aTransform->Inversed();
84c71f29 1730 }
1731
91c60b57 1732 aResult &= mySceneTransformTexture->Init (theGlContext, 4,
84c71f29 1733 myRaytraceGeometry.Size() * 4, reinterpret_cast<const GLfloat*> (aNodeTransforms));
1734
25ef750e 1735 delete [] aNodeTransforms;
84c71f29 1736
1737 /////////////////////////////////////////////////////////////////////////////
bc8c79bb 1738 // Write geometry and bottom-level BVH buffers
84c71f29 1739
fc73a202 1740 Standard_Size aTotalVerticesNb = 0;
1741 Standard_Size aTotalElementsNb = 0;
1742 Standard_Size aTotalBVHNodesNb = 0;
265d4508 1743
fc73a202 1744 for (Standard_Integer anElemIndex = 0; anElemIndex < myRaytraceGeometry.Size(); ++anElemIndex)
1745 {
1746 OpenGl_TriangleSet* aTriangleSet = dynamic_cast<OpenGl_TriangleSet*> (
1747 myRaytraceGeometry.Objects().ChangeValue (anElemIndex).operator->());
265d4508 1748
fc73a202 1749 Standard_ASSERT_RETURN (aTriangleSet != NULL,
1750 "Error: Failed to get triangulation of OpenGL element", Standard_False);
1751
1752 aTotalVerticesNb += aTriangleSet->Vertices.size();
1753 aTotalElementsNb += aTriangleSet->Elements.size();
e276548b 1754
fc73a202 1755 Standard_ASSERT_RETURN (!aTriangleSet->BVH().IsNull(),
1756 "Error: Failed to get bottom-level BVH of OpenGL element", Standard_False);
265d4508 1757
fc73a202 1758 aTotalBVHNodesNb += aTriangleSet->BVH()->NodeInfoBuffer().size();
1759 }
e276548b 1760
e2da917a 1761 aTotalBVHNodesNb += myRaytraceGeometry.BVH()->NodeInfoBuffer().size();
1762
fc73a202 1763 if (aTotalBVHNodesNb != 0)
e276548b 1764 {
e2da917a 1765 aResult &= mySceneNodeInfoTexture->Init (
91c60b57 1766 theGlContext, 4, GLsizei (aTotalBVHNodesNb), static_cast<const GLuint*> (NULL));
e2da917a 1767 aResult &= mySceneMinPointTexture->Init (
91c60b57 1768 theGlContext, 3, GLsizei (aTotalBVHNodesNb), static_cast<const GLfloat*> (NULL));
e2da917a 1769 aResult &= mySceneMaxPointTexture->Init (
91c60b57 1770 theGlContext, 3, GLsizei (aTotalBVHNodesNb), static_cast<const GLfloat*> (NULL));
fc73a202 1771 }
265d4508 1772
fc73a202 1773 if (!aResult)
1774 {
1775#ifdef RAY_TRACE_PRINT_INFO
1776 std::cout << "Error: Failed to upload buffers for bottom-level scene BVH" << std::endl;
1777#endif
1778 return Standard_False;
1779 }
e276548b 1780
fc73a202 1781 if (aTotalElementsNb != 0)
1782 {
25ef750e 1783 aResult &= myGeometryTriangTexture->Init (
91c60b57 1784 theGlContext, 4, GLsizei (aTotalElementsNb), static_cast<const GLuint*> (NULL));
fc73a202 1785 }
265d4508 1786
fc73a202 1787 if (aTotalVerticesNb != 0)
1788 {
25ef750e 1789 aResult &= myGeometryVertexTexture->Init (
91c60b57 1790 theGlContext, 3, GLsizei (aTotalVerticesNb), static_cast<const GLfloat*> (NULL));
25ef750e 1791 aResult &= myGeometryNormalTexture->Init (
91c60b57 1792 theGlContext, 3, GLsizei (aTotalVerticesNb), static_cast<const GLfloat*> (NULL));
25ef750e 1793 aResult &= myGeometryTexCrdTexture->Init (
91c60b57 1794 theGlContext, 2, GLsizei (aTotalVerticesNb), static_cast<const GLfloat*> (NULL));
fc73a202 1795 }
265d4508 1796
fc73a202 1797 if (!aResult)
1798 {
1799#ifdef RAY_TRACE_PRINT_INFO
1800 std::cout << "Error: Failed to upload buffers for scene geometry" << std::endl;
1801#endif
1802 return Standard_False;
1803 }
265d4508 1804
e2da917a 1805 const NCollection_Handle<BVH_Tree<Standard_ShortReal, 3> >& aBVH = myRaytraceGeometry.BVH();
91c60b57 1806
1807 if (aBVH->Length() > 0)
1e99558f 1808 {
91c60b57 1809 aResult &= mySceneNodeInfoTexture->SubData (theGlContext, 0, aBVH->Length(),
1e99558f 1810 reinterpret_cast<const GLuint*> (&aBVH->NodeInfoBuffer().front()));
91c60b57 1811 aResult &= mySceneMinPointTexture->SubData (theGlContext, 0, aBVH->Length(),
1e99558f 1812 reinterpret_cast<const GLfloat*> (&aBVH->MinPointBuffer().front()));
91c60b57 1813 aResult &= mySceneMaxPointTexture->SubData (theGlContext, 0, aBVH->Length(),
1e99558f 1814 reinterpret_cast<const GLfloat*> (&aBVH->MaxPointBuffer().front()));
1815 }
e2da917a 1816
fc73a202 1817 for (Standard_Integer aNodeIdx = 0; aNodeIdx < aBVH->Length(); ++aNodeIdx)
1818 {
1819 if (!aBVH->IsOuter (aNodeIdx))
1820 continue;
265d4508 1821
fc73a202 1822 OpenGl_TriangleSet* aTriangleSet = myRaytraceGeometry.TriangleSet (aNodeIdx);
e276548b 1823
fc73a202 1824 Standard_ASSERT_RETURN (aTriangleSet != NULL,
1825 "Error: Failed to get triangulation of OpenGL element", Standard_False);
e276548b 1826
e2da917a 1827 Standard_Integer aBVHOffset = myRaytraceGeometry.AccelerationOffset (aNodeIdx);
e276548b 1828
fc73a202 1829 Standard_ASSERT_RETURN (aBVHOffset != OpenGl_RaytraceGeometry::INVALID_OFFSET,
1830 "Error: Failed to get offset for bottom-level BVH", Standard_False);
e276548b 1831
e2da917a 1832 const Standard_Integer aBvhBuffersSize = aTriangleSet->BVH()->Length();
265d4508 1833
e2da917a 1834 if (aBvhBuffersSize != 0)
fc73a202 1835 {
91c60b57 1836 aResult &= mySceneNodeInfoTexture->SubData (theGlContext, aBVHOffset, aBvhBuffersSize,
1837 reinterpret_cast<const GLuint*> (&aTriangleSet->BVH()->NodeInfoBuffer().front()));
1838 aResult &= mySceneMinPointTexture->SubData (theGlContext, aBVHOffset, aBvhBuffersSize,
1839 reinterpret_cast<const GLfloat*> (&aTriangleSet->BVH()->MinPointBuffer().front()));
1840 aResult &= mySceneMaxPointTexture->SubData (theGlContext, aBVHOffset, aBvhBuffersSize,
1841 reinterpret_cast<const GLfloat*> (&aTriangleSet->BVH()->MaxPointBuffer().front()));
1842
fc73a202 1843 if (!aResult)
e276548b 1844 {
fc73a202 1845#ifdef RAY_TRACE_PRINT_INFO
1846 std::cout << "Error: Failed to upload buffers for bottom-level scene BVHs" << std::endl;
1847#endif
e276548b 1848 return Standard_False;
1849 }
1850 }
1851
fc73a202 1852 const Standard_Integer aVerticesOffset = myRaytraceGeometry.VerticesOffset (aNodeIdx);
265d4508 1853
fc73a202 1854 Standard_ASSERT_RETURN (aVerticesOffset != OpenGl_RaytraceGeometry::INVALID_OFFSET,
1855 "Error: Failed to get offset for triangulation vertices of OpenGL element", Standard_False);
265d4508 1856
fc73a202 1857 if (!aTriangleSet->Vertices.empty())
1858 {
91c60b57 1859 aResult &= myGeometryNormalTexture->SubData (theGlContext, aVerticesOffset,
1860 GLsizei (aTriangleSet->Normals.size()), reinterpret_cast<const GLfloat*> (&aTriangleSet->Normals.front()));
1861 aResult &= myGeometryTexCrdTexture->SubData (theGlContext, aVerticesOffset,
1862 GLsizei (aTriangleSet->TexCrds.size()), reinterpret_cast<const GLfloat*> (&aTriangleSet->TexCrds.front()));
1863 aResult &= myGeometryVertexTexture->SubData (theGlContext, aVerticesOffset,
1864 GLsizei (aTriangleSet->Vertices.size()), reinterpret_cast<const GLfloat*> (&aTriangleSet->Vertices.front()));
fc73a202 1865 }
e276548b 1866
fc73a202 1867 const Standard_Integer anElementsOffset = myRaytraceGeometry.ElementsOffset (aNodeIdx);
265d4508 1868
fc73a202 1869 Standard_ASSERT_RETURN (anElementsOffset != OpenGl_RaytraceGeometry::INVALID_OFFSET,
1870 "Error: Failed to get offset for triangulation elements of OpenGL element", Standard_False);
e276548b 1871
fc73a202 1872 if (!aTriangleSet->Elements.empty())
e276548b 1873 {
91c60b57 1874 aResult &= myGeometryTriangTexture->SubData (theGlContext, anElementsOffset, GLsizei (aTriangleSet->Elements.size()),
5cff985a 1875 reinterpret_cast<const GLuint*> (&aTriangleSet->Elements.front()));
e276548b 1876 }
265d4508 1877
fc73a202 1878 if (!aResult)
1879 {
1880#ifdef RAY_TRACE_PRINT_INFO
1881 std::cout << "Error: Failed to upload triangulation buffers for OpenGL element" << std::endl;
e276548b 1882#endif
fc73a202 1883 return Standard_False;
1884 }
e276548b 1885 }
e276548b 1886
25ef750e 1887 /////////////////////////////////////////////////////////////////////////////
1888 // Write material buffer
1889
fc73a202 1890 if (myRaytraceGeometry.Materials.size() != 0)
1891 {
91c60b57 1892 aResult &= myRaytraceMaterialTexture->Init (theGlContext, 4,
25ef750e 1893 GLsizei (myRaytraceGeometry.Materials.size() * 11), myRaytraceGeometry.Materials.front().Packed());
1894
fc73a202 1895 if (!aResult)
1896 {
1897#ifdef RAY_TRACE_PRINT_INFO
1898 std::cout << "Error: Failed to upload material buffer" << std::endl;
1899#endif
1900 return Standard_False;
1901 }
1902 }
e276548b 1903
fc73a202 1904 myIsRaytraceDataValid = myRaytraceGeometry.Objects().Size() != 0;
e276548b 1905
fc73a202 1906#ifdef RAY_TRACE_PRINT_INFO
e276548b 1907
fc73a202 1908 Standard_ShortReal aMemUsed = 0.f;
e276548b 1909
fc73a202 1910 for (Standard_Integer anElemIdx = 0; anElemIdx < myRaytraceGeometry.Size(); ++anElemIdx)
1911 {
1912 OpenGl_TriangleSet* aTriangleSet = dynamic_cast<OpenGl_TriangleSet*> (
1913 myRaytraceGeometry.Objects().ChangeValue (anElemIdx).operator->());
e276548b 1914
fc73a202 1915 aMemUsed += static_cast<Standard_ShortReal> (
25ef750e 1916 aTriangleSet->Vertices.size() * sizeof (BVH_Vec3f));
fc73a202 1917 aMemUsed += static_cast<Standard_ShortReal> (
25ef750e 1918 aTriangleSet->Normals.size() * sizeof (BVH_Vec3f));
1919 aMemUsed += static_cast<Standard_ShortReal> (
1920 aTriangleSet->TexCrds.size() * sizeof (BVH_Vec2f));
fc73a202 1921 aMemUsed += static_cast<Standard_ShortReal> (
1922 aTriangleSet->Elements.size() * sizeof (BVH_Vec4i));
e276548b 1923
fc73a202 1924 aMemUsed += static_cast<Standard_ShortReal> (
1925 aTriangleSet->BVH()->NodeInfoBuffer().size() * sizeof (BVH_Vec4i));
1926 aMemUsed += static_cast<Standard_ShortReal> (
25ef750e 1927 aTriangleSet->BVH()->MinPointBuffer().size() * sizeof (BVH_Vec3f));
fc73a202 1928 aMemUsed += static_cast<Standard_ShortReal> (
25ef750e 1929 aTriangleSet->BVH()->MaxPointBuffer().size() * sizeof (BVH_Vec3f));
fc73a202 1930 }
e276548b 1931
fc73a202 1932 aMemUsed += static_cast<Standard_ShortReal> (
1933 myRaytraceGeometry.BVH()->NodeInfoBuffer().size() * sizeof (BVH_Vec4i));
1934 aMemUsed += static_cast<Standard_ShortReal> (
25ef750e 1935 myRaytraceGeometry.BVH()->MinPointBuffer().size() * sizeof (BVH_Vec3f));
fc73a202 1936 aMemUsed += static_cast<Standard_ShortReal> (
25ef750e 1937 myRaytraceGeometry.BVH()->MaxPointBuffer().size() * sizeof (BVH_Vec3f));
e276548b 1938
fc73a202 1939 std::cout << "GPU Memory Used (MB): ~" << aMemUsed / 1048576 << std::endl;
e276548b 1940
fc73a202 1941#endif
e276548b 1942
fc73a202 1943 return aResult;
1944}
e276548b 1945
fc73a202 1946// =======================================================================
91c60b57 1947// function : UpdateRaytraceLightSources
1948// purpose : Updates 3D scene light sources for ray-tracing
fc73a202 1949// =======================================================================
91c60b57 1950Standard_Boolean OpenGl_View::updateRaytraceLightSources (const OpenGl_Mat4& theInvModelView, const Handle(OpenGl_Context)& theGlContext)
fc73a202 1951{
91c60b57 1952 myRaytraceGeometry.Sources.clear();
1953
1954 myRaytraceGeometry.Ambient = BVH_Vec4f (0.0f, 0.0f, 0.0f, 0.0f);
1955
1956 for (OpenGl_ListOfLight::Iterator anItl (LightList()); anItl.More(); anItl.Next())
1957 {
1958 const OpenGl_Light& aLight = anItl.Value();
1959
1960 if (aLight.Type == Visual3d_TOLS_AMBIENT)
1961 {
1962 myRaytraceGeometry.Ambient += BVH_Vec4f (aLight.Color.r(),
1963 aLight.Color.g(),
1964 aLight.Color.b(),
1965 0.0f);
1966 continue;
1967 }
1968
1969 BVH_Vec4f aDiffuse (aLight.Color.r(),
1970 aLight.Color.g(),
1971 aLight.Color.b(),
1972 1.0f);
1973
1974 BVH_Vec4f aPosition (-aLight.Direction.x(),
1975 -aLight.Direction.y(),
1976 -aLight.Direction.z(),
1977 0.0f);
1978
1979 if (aLight.Type != Visual3d_TOLS_DIRECTIONAL)
1980 {
1981 aPosition = BVH_Vec4f (aLight.Position.x(),
1982 aLight.Position.y(),
1983 aLight.Position.z(),
1984 1.0f);
1985 }
1986
1987 if (aLight.IsHeadlight)
1988 {
1989 aPosition = theInvModelView * aPosition;
1990 }
1991
1992 myRaytraceGeometry.Sources.push_back (OpenGl_RaytraceLight (aDiffuse, aPosition));
1993 }
1994
1995 if (myRaytraceLightSrcTexture.IsNull()) // create light source buffer
1996 {
1997 myRaytraceLightSrcTexture = new OpenGl_TextureBufferArb;
1998
1999 if (!myRaytraceLightSrcTexture->Create (theGlContext))
2000 {
2001#ifdef RAY_TRACE_PRINT_INFO
2002 std::cout << "Error: Failed to create light source buffer" << std::endl;
2003#endif
2004 return Standard_False;
2005 }
2006 }
2007
2008 if (myRaytraceGeometry.Sources.size() != 0)
fc73a202 2009 {
91c60b57 2010 const GLfloat* aDataPtr = myRaytraceGeometry.Sources.front().Packed();
2011 if (!myRaytraceLightSrcTexture->Init (theGlContext, 4, GLsizei (myRaytraceGeometry.Sources.size() * 2), aDataPtr))
2012 {
2013#ifdef RAY_TRACE_PRINT_INFO
2014 std::cout << "Error: Failed to upload light source buffer" << std::endl;
2015#endif
2016 return Standard_False;
2017 }
fc73a202 2018 }
e276548b 2019
fc73a202 2020 return Standard_True;
e276548b 2021}
2022
2023// =======================================================================
91c60b57 2024// function : UpdateRaytraceEnvironmentMap
2025// purpose : Updates environment map for ray-tracing
e276548b 2026// =======================================================================
91c60b57 2027Standard_Boolean OpenGl_View::updateRaytraceEnvironmentMap (const Handle(OpenGl_Context)& theGlContext)
e276548b 2028{
91c60b57 2029 Standard_Boolean aResult = Standard_True;
e276548b 2030
91c60b57 2031 if (!myToUpdateEnvironmentMap)
e276548b 2032 {
91c60b57 2033 return aResult;
2034 }
e276548b 2035
91c60b57 2036 for (Standard_Integer anIdx = 0; anIdx < 2; ++anIdx)
2037 {
2038 const Handle(OpenGl_ShaderProgram)& aProgram =
2039 anIdx == 0 ? myRaytraceProgram : myPostFSAAProgram;
fc73a202 2040
91c60b57 2041 if (!aProgram.IsNull())
2042 {
2043 aResult &= theGlContext->BindProgram (aProgram);
e276548b 2044
91c60b57 2045 if (!myTextureEnv.IsNull() && mySurfaceDetail != Visual3d_TOD_NONE)
2046 {
2047 myTextureEnv->Bind (theGlContext,
2048 GL_TEXTURE0 + OpenGl_RT_EnvironmentMapTexture);
b5ac8292 2049
91c60b57 2050 aResult &= aProgram->SetUniform (theGlContext,
2051 myUniformLocations[anIdx][OpenGl_RT_uEnvMapEnable], 1);
2052 }
2053 else
2054 {
2055 aResult &= aProgram->SetUniform (theGlContext,
2056 myUniformLocations[anIdx][OpenGl_RT_uEnvMapEnable], 0);
2057 }
2058 }
2059 }
e276548b 2060
91c60b57 2061 myToUpdateEnvironmentMap = Standard_False;
e276548b 2062
91c60b57 2063 theGlContext->BindProgram (NULL);
fc73a202 2064
91c60b57 2065 return aResult;
fc73a202 2066}
2067
2068// =======================================================================
25ef750e 2069// function : SetUniformState
2070// purpose : Sets uniform state for the given ray-tracing shader program
2071// =======================================================================
91c60b57 2072Standard_Boolean OpenGl_View::setUniformState (const Graphic3d_CView& theCView,
2073 const OpenGl_Vec3* theOrigins,
2074 const OpenGl_Vec3* theDirects,
2075 const OpenGl_Mat4& theUnviewMat,
2076 const Standard_Integer theProgramId,
2077 const Handle(OpenGl_Context)& theGlContext)
25ef750e 2078{
91c60b57 2079 Handle(OpenGl_ShaderProgram)& theProgram =
2080 theProgramId == 0 ? myRaytraceProgram : myPostFSAAProgram;
2081
2082 if (theProgram.IsNull())
25ef750e 2083 {
2084 return Standard_False;
2085 }
2086
25ef750e 2087 Standard_Boolean aResult = Standard_True;
2088
91c60b57 2089 const Standard_Integer aLightSourceBufferSize =
2090 static_cast<Standard_Integer> (myRaytraceGeometry.Sources.size());
2091
25ef750e 2092 // Set camera state
91c60b57 2093 aResult &= theProgram->SetUniform (theGlContext,
2094 myUniformLocations[theProgramId][OpenGl_RT_uOriginLB], theOrigins[0]);
2095 aResult &= theProgram->SetUniform (theGlContext,
2096 myUniformLocations[theProgramId][OpenGl_RT_uOriginRB], theOrigins[1]);
2097 aResult &= theProgram->SetUniform (theGlContext,
2098 myUniformLocations[theProgramId][OpenGl_RT_uOriginLT], theOrigins[2]);
2099 aResult &= theProgram->SetUniform (theGlContext,
2100 myUniformLocations[theProgramId][OpenGl_RT_uOriginRT], theOrigins[3]);
2101 aResult &= theProgram->SetUniform (theGlContext,
2102 myUniformLocations[theProgramId][OpenGl_RT_uDirectLB], theDirects[0]);
2103 aResult &= theProgram->SetUniform (theGlContext,
2104 myUniformLocations[theProgramId][OpenGl_RT_uDirectRB], theDirects[1]);
2105 aResult &= theProgram->SetUniform (theGlContext,
2106 myUniformLocations[theProgramId][OpenGl_RT_uDirectLT], theDirects[2]);
2107 aResult &= theProgram->SetUniform (theGlContext,
2108 myUniformLocations[theProgramId][OpenGl_RT_uDirectRT], theDirects[3]);
2109 aResult &= theProgram->SetUniform (theGlContext,
2110 myUniformLocations[theProgramId][OpenGl_RT_uUnviewMat], theUnviewMat);
312a4043 2111
25ef750e 2112 // Set scene parameters
91c60b57 2113 aResult &= theProgram->SetUniform (theGlContext,
2114 myUniformLocations[theProgramId][OpenGl_RT_uSceneRad], myRaytraceSceneRadius);
2115 aResult &= theProgram->SetUniform (theGlContext,
2116 myUniformLocations[theProgramId][OpenGl_RT_uSceneEps], myRaytraceSceneEpsilon);
2117 aResult &= theProgram->SetUniform (theGlContext,
2118 myUniformLocations[theProgramId][OpenGl_RT_uLightCount], aLightSourceBufferSize);
2119 aResult &= theProgram->SetUniform (theGlContext,
2120 myUniformLocations[theProgramId][OpenGl_RT_uLightAmbnt], myRaytraceGeometry.Ambient);
2121
2122 // Set run-time rendering options
2123 aResult &= theProgram->SetUniform (theGlContext,
2124 myUniformLocations[theProgramId][OpenGl_RT_uShadEnabled], theCView.RenderParams.IsShadowEnabled ? 1 : 0);
2125 aResult &= theProgram->SetUniform (theGlContext,
2126 myUniformLocations[theProgramId][OpenGl_RT_uReflEnabled], theCView.RenderParams.IsReflectionEnabled ? 1 : 0);
25ef750e 2127
2128 // Set array of 64-bit texture handles
91c60b57 2129 if (theGlContext->arbTexBindless != NULL && myRaytraceGeometry.HasTextures())
25ef750e 2130 {
91c60b57 2131 aResult &= theProgram->SetUniform (theGlContext, "uTextureSamplers", static_cast<GLsizei> (
2132 myRaytraceGeometry.TextureHandles().size()), &myRaytraceGeometry.TextureHandles()[0]);
25ef750e 2133 }
2134
2135 if (!aResult)
2136 {
2137#ifdef RAY_TRACE_PRINT_INFO
91c60b57 2138 std::cout << "Info: Not all uniforms were detected for program " << theProgramId << std::endl;
25ef750e 2139#endif
2140 }
2141
2142 return aResult;
2143}
2144
2145// =======================================================================
91c60b57 2146// function : BindRaytraceTextures
2147// purpose : Binds ray-trace textures to corresponding texture units
2148// =======================================================================
2149void OpenGl_View::bindRaytraceTextures (const Handle(OpenGl_Context)& theGlContext)
2150{
2151 mySceneMinPointTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneMinPointTexture);
2152 mySceneMaxPointTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneMaxPointTexture);
2153 mySceneNodeInfoTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneNodeInfoTexture);
2154 myGeometryVertexTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryVertexTexture);
2155 myGeometryNormalTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryNormalTexture);
2156 myGeometryTexCrdTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryTexCrdTexture);
2157 myGeometryTriangTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryTriangTexture);
2158 mySceneTransformTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneTransformTexture);
2159 myRaytraceMaterialTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_RaytraceMaterialTexture);
2160 myRaytraceLightSrcTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_RaytraceLightSrcTexture);
2161
2162 if (!myOpenGlFBO.IsNull())
2163 {
2164 myOpenGlFBO->ColorTexture()->Bind (theGlContext, GL_TEXTURE0 + OpenGl_RT_OpenGlColorTexture);
2165 myOpenGlFBO->DepthStencilTexture()->Bind (theGlContext, GL_TEXTURE0 + OpenGl_RT_OpenGlDepthTexture);
2166 }
2167}
2168
2169// =======================================================================
2170// function : UnbindRaytraceTextures
2171// purpose : Unbinds ray-trace textures from corresponding texture units
2172// =======================================================================
2173void OpenGl_View::unbindRaytraceTextures (const Handle(OpenGl_Context)& theGlContext)
2174{
2175 mySceneMinPointTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneMinPointTexture);
2176 mySceneMaxPointTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneMaxPointTexture);
2177 mySceneNodeInfoTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneNodeInfoTexture);
2178 myGeometryVertexTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryVertexTexture);
2179 myGeometryNormalTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryNormalTexture);
2180 myGeometryTexCrdTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryTexCrdTexture);
2181 myGeometryTriangTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryTriangTexture);
2182 mySceneTransformTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneTransformTexture);
2183 myRaytraceMaterialTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_RaytraceMaterialTexture);
2184 myRaytraceLightSrcTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_RaytraceLightSrcTexture);
2185
2186 if (!myOpenGlFBO.IsNull())
2187 {
2188 myOpenGlFBO->ColorTexture()->Unbind (theGlContext, GL_TEXTURE0 + OpenGl_RT_OpenGlColorTexture);
2189 myOpenGlFBO->DepthStencilTexture()->Unbind (theGlContext, GL_TEXTURE0 + OpenGl_RT_OpenGlDepthTexture);
2190 }
2191
2192 theGlContext->core15fwd->glActiveTexture (GL_TEXTURE0);
2193}
2194
2195// =======================================================================
fc73a202 2196// function : RunRaytraceShaders
2197// purpose : Runs ray-tracing shader programs
2198// =======================================================================
91c60b57 2199Standard_Boolean OpenGl_View::runRaytraceShaders (const Graphic3d_CView& theCView,
2200 const Standard_Integer theSizeX,
2201 const Standard_Integer theSizeY,
2202 const OpenGl_Vec3* theOrigins,
2203 const OpenGl_Vec3* theDirects,
2204 const OpenGl_Mat4& theUnviewMat,
2205 OpenGl_FrameBuffer* theOutputFBO,
2206 const Handle(OpenGl_Context)& theGlContext)
fc73a202 2207{
91c60b57 2208 bindRaytraceTextures (theGlContext);
fc73a202 2209
bc8c79bb 2210 if (theCView.RenderParams.IsAntialiasingEnabled) // render source image to FBO
fc73a202 2211 {
91c60b57 2212 myRaytraceFBO1->BindBuffer (theGlContext);
2213
fc73a202 2214 glDisable (GL_BLEND);
2215 }
2216
91c60b57 2217 Standard_Boolean aResult = theGlContext->BindProgram (myRaytraceProgram);
fc73a202 2218
91c60b57 2219 aResult &= setUniformState (theCView,
2220 theOrigins,
2221 theDirects,
2222 theUnviewMat,
2223 0, // ID of RT program
2224 theGlContext);
fc73a202 2225
91c60b57 2226 myRaytraceScreenQuad.BindVertexAttrib (theGlContext, Graphic3d_TOA_POS);
fc73a202 2227 {
91c60b57 2228 if (aResult)
2229 theGlContext->core20fwd->glDrawArrays (GL_TRIANGLES, 0, 6);
fc73a202 2230 }
91c60b57 2231 myRaytraceScreenQuad.UnbindVertexAttrib (theGlContext, Graphic3d_TOA_POS);
25ef750e 2232
91c60b57 2233 if (!theCView.RenderParams.IsAntialiasingEnabled || !aResult)
fc73a202 2234 {
91c60b57 2235 unbindRaytraceTextures (theGlContext);
fc73a202 2236
91c60b57 2237 theGlContext->BindProgram (NULL);
a89742cf 2238
91c60b57 2239 return aResult;
fc73a202 2240 }
2241
91c60b57 2242 myRaytraceFBO1->ColorTexture()->Bind (theGlContext, GL_TEXTURE0 + OpenGl_RT_FSAAInputTexture);
fc73a202 2243
91c60b57 2244 aResult &= theGlContext->BindProgram (myPostFSAAProgram);
fc73a202 2245
91c60b57 2246 aResult &= setUniformState (theCView,
2247 theOrigins,
2248 theDirects,
2249 theUnviewMat,
2250 1, // ID of FSAA program
2251 theGlContext);
fc73a202 2252
91c60b57 2253 myRaytraceScreenQuad.BindVertexAttrib (theGlContext, Graphic3d_TOA_POS);
fc73a202 2254 {
91c60b57 2255 // Perform multi-pass adaptive FSAA using ping-pong technique.
2256 // We use 'FLIPTRI' sampling pattern changing for every pixel
2257 // (3 additional samples per pixel, the 1st sample is already
2258 // available from initial ray-traced image).
2259 for (Standard_Integer anIt = 1; anIt < 4; ++anIt)
50d0e1ce 2260 {
91c60b57 2261 GLfloat aOffsetX = 1.f / theSizeX;
2262 GLfloat aOffsetY = 1.f / theSizeY;
50d0e1ce 2263
91c60b57 2264 if (anIt == 1)
2265 {
2266 aOffsetX *= -0.55f;
2267 aOffsetY *= 0.55f;
2268 }
2269 else if (anIt == 2)
2270 {
2271 aOffsetX *= 0.00f;
2272 aOffsetY *= -0.55f;
2273 }
2274 else if (anIt == 3)
2275 {
2276 aOffsetX *= 0.55f;
2277 aOffsetY *= 0.00f;
2278 }
fc73a202 2279
91c60b57 2280 aResult &= myPostFSAAProgram->SetUniform (theGlContext,
2281 myUniformLocations[1][OpenGl_RT_uSamples], anIt + 1);
2282 aResult &= myPostFSAAProgram->SetUniform (theGlContext,
2283 myUniformLocations[1][OpenGl_RT_uOffsetX], aOffsetX);
2284 aResult &= myPostFSAAProgram->SetUniform (theGlContext,
2285 myUniformLocations[1][OpenGl_RT_uOffsetY], aOffsetY);
fc73a202 2286
91c60b57 2287 Handle(OpenGl_FrameBuffer)& aFramebuffer = anIt % 2 ? myRaytraceFBO2 : myRaytraceFBO1;
fc73a202 2288
91c60b57 2289 if (anIt == 3) // disable FBO on last iteration
2290 {
2291 glEnable (GL_BLEND);
2292
2293 if (theOutputFBO != NULL)
2294 theOutputFBO->BindBuffer (theGlContext);
2295 }
2296 else
2297 {
2298 aFramebuffer->BindBuffer (theGlContext);
2299 }
50d0e1ce 2300
91c60b57 2301 theGlContext->core20fwd->glDrawArrays (GL_TRIANGLES, 0, 6);
e276548b 2302
91c60b57 2303 if (anIt != 3) // set input for the next pass
2304 {
2305 aFramebuffer->ColorTexture()->Bind (theGlContext, GL_TEXTURE0 + OpenGl_RT_FSAAInputTexture);
2306 }
e276548b 2307 }
2308 }
91c60b57 2309 myRaytraceScreenQuad.UnbindVertexAttrib (theGlContext, Graphic3d_TOA_POS);
fc73a202 2310
91c60b57 2311 unbindRaytraceTextures (theGlContext);
a89742cf 2312
91c60b57 2313 theGlContext->BindProgram (NULL);
fc73a202 2314
91c60b57 2315 return aResult;
e276548b 2316}
2317
2318// =======================================================================
2319// function : Raytrace
fc73a202 2320// purpose : Redraws the window using OpenGL/GLSL ray-tracing
e276548b 2321// =======================================================================
91c60b57 2322Standard_Boolean OpenGl_View::raytrace (const Graphic3d_CView& theCView,
2323 const Standard_Integer theSizeX,
2324 const Standard_Integer theSizeY,
2325 OpenGl_FrameBuffer* theOutputFBO,
2326 const Handle(OpenGl_Context)& theGlContext)
e276548b 2327{
91c60b57 2328 if (!initRaytraceResources (theCView, theGlContext))
2329 {
e276548b 2330 return Standard_False;
91c60b57 2331 }
2332
2333 if (!resizeRaytraceBuffers (theSizeX, theSizeY, theGlContext))
2334 {
2335 return Standard_False;
2336 }
e276548b 2337
91c60b57 2338 if (!updateRaytraceEnvironmentMap (theGlContext))
2339 {
e276548b 2340 return Standard_False;
91c60b57 2341 }
e276548b 2342
2343 // Get model-view and projection matrices
25ef750e 2344 OpenGl_Mat4 aOrientationMatrix;
2345 OpenGl_Mat4 aViewMappingMatrix;
91c60b57 2346 OpenGl_Mat4 aInverOrientMatrix;
e276548b 2347
91c60b57 2348 GetMatrices (aOrientationMatrix,
2349 aViewMappingMatrix);
e276548b 2350
91c60b57 2351 aOrientationMatrix.Inverted (aInverOrientMatrix);
e276548b 2352
91c60b57 2353 if (!updateRaytraceLightSources (aInverOrientMatrix, theGlContext))
2354 {
e276548b 2355 return Standard_False;
91c60b57 2356 }
e276548b 2357
fc73a202 2358 OpenGl_Vec3 aOrigins[4];
2359 OpenGl_Vec3 aDirects[4];
25ef750e 2360 OpenGl_Mat4 anUnviewMat;
e276548b 2361
91c60b57 2362 updateCamera (aOrientationMatrix,
fc73a202 2363 aViewMappingMatrix,
2364 aOrigins,
a89742cf 2365 aDirects,
25ef750e 2366 anUnviewMat);
e276548b 2367
91c60b57 2368 glEnable (GL_BLEND);
e276548b 2369 glDisable (GL_DEPTH_TEST);
91c60b57 2370 glBlendFunc (GL_ONE, GL_SRC_ALPHA);
e276548b 2371
91c60b57 2372 if (theOutputFBO != NULL)
a89742cf 2373 {
91c60b57 2374 theOutputFBO->BindBuffer (theGlContext);
a89742cf 2375 }
2376
c827ea3a 2377 // Generate ray-traced image
e276548b 2378 if (myIsRaytraceDataValid)
2379 {
91c60b57 2380 myRaytraceScreenQuad.Bind (theGlContext);
fc73a202 2381
91c60b57 2382 if (!myRaytraceGeometry.AcquireTextures (theGlContext))
25ef750e 2383 {
91c60b57 2384 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB, GL_DEBUG_TYPE_ERROR_ARB,
2385 0, GL_DEBUG_SEVERITY_MEDIUM_ARB, "Error: Failed to acquire OpenGL image textures");
25ef750e 2386 }
2387
91c60b57 2388 Standard_Boolean aResult = runRaytraceShaders (theCView,
2389 theSizeX,
2390 theSizeY,
2391 aOrigins,
2392 aDirects,
2393 anUnviewMat,
2394 theOutputFBO,
2395 theGlContext);
fc73a202 2396
91c60b57 2397 if (!aResult)
25ef750e 2398 {
91c60b57 2399 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB, GL_DEBUG_TYPE_ERROR_ARB,
2400 0, GL_DEBUG_SEVERITY_MEDIUM_ARB, "Error: Failed to execute ray-tracing shaders");
2401 }
25ef750e 2402
91c60b57 2403 if (!myRaytraceGeometry.ReleaseTextures (theGlContext))
2404 {
2405 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB, GL_DEBUG_TYPE_ERROR_ARB,
2406 0, GL_DEBUG_SEVERITY_MEDIUM_ARB, "Error: Failed to release OpenGL image textures");
25ef750e 2407 }
2408
91c60b57 2409 myRaytraceScreenQuad.Unbind (theGlContext);
e276548b 2410 }
2411
91c60b57 2412 glDisable (GL_BLEND);
2413 glEnable (GL_DEPTH_TEST);
a89742cf 2414
e276548b 2415 return Standard_True;
91c60b57 2416}