Update of unstable Boolean test cases
[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
f483f2ed 1014 // If OpenGL driver supports bindless textures and texturing
1015 // is actually used, activate texturing in ray-tracing mode
1016 if (myRaytraceParameters.UseBindlessTextures && theGlContext->arbTexBindless != NULL)
91c60b57 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
f483f2ed 1156 if (myRaytraceGeometry.HasTextures() != myRaytraceParameters.UseBindlessTextures)
1157 {
1158 myRaytraceParameters.UseBindlessTextures = myRaytraceGeometry.HasTextures();
1159 aToRebuildShaders = Standard_True;
1160 }
1161
bc8c79bb 1162 if (theCView.RenderParams.IsTransparentShadowEnabled != myRaytraceParameters.TransparentShadows)
1163 {
1164 myRaytraceParameters.TransparentShadows = theCView.RenderParams.IsTransparentShadowEnabled;
1165 aToRebuildShaders = Standard_True;
1166 }
1167
fc73a202 1168 if (aToRebuildShaders)
1169 {
e276548b 1170#ifdef RAY_TRACE_PRINT_INFO
bc8c79bb 1171 std::cout << "Info: Rebuild shaders with stack size: " << myRaytraceParameters.StackSize << std::endl;
e276548b 1172#endif
e276548b 1173
bc8c79bb 1174 // Change state to force update all uniforms
91c60b57 1175 myToUpdateEnvironmentMap = Standard_True;
84c71f29 1176
91c60b57 1177 TCollection_AsciiString aPrefixString = generateShaderPrefix (theGlContext);
bc8c79bb 1178
1179#ifdef RAY_TRACE_PRINT_INFO
1180 std::cout << "GLSL prefix string:" << std::endl << aPrefixString << std::endl;
1181#endif
265d4508 1182
bc8c79bb 1183 myRaytraceShaderSource.SetPrefix (aPrefixString);
1184 myPostFSAAShaderSource.SetPrefix (aPrefixString);
265d4508 1185
91c60b57 1186 if (!myRaytraceShader->LoadSource (theGlContext, myRaytraceShaderSource.Source())
1187 || !myPostFSAAShader->LoadSource (theGlContext, myPostFSAAShaderSource.Source()))
fc73a202 1188 {
91c60b57 1189 return safeFailBack ("Failed to load source into ray-tracing fragment shaders", theGlContext);
fc73a202 1190 }
265d4508 1191
91c60b57 1192 if (!myRaytraceShader->Compile (theGlContext)
1193 || !myPostFSAAShader->Compile (theGlContext))
fc73a202 1194 {
91c60b57 1195 return safeFailBack ("Failed to compile ray-tracing fragment shaders", theGlContext);
fc73a202 1196 }
1197
91c60b57 1198 myRaytraceProgram->SetAttributeName (theGlContext, Graphic3d_TOA_POS, "occVertex");
1199 myPostFSAAProgram->SetAttributeName (theGlContext, Graphic3d_TOA_POS, "occVertex");
1200 if (!myRaytraceProgram->Link (theGlContext)
1201 || !myPostFSAAProgram->Link (theGlContext))
fc73a202 1202 {
91c60b57 1203 return safeFailBack ("Failed to initialize vertex attributes for ray-tracing program", theGlContext);
fc73a202 1204 }
1205 }
e276548b 1206 }
1207
91c60b57 1208 if (myRaytraceInitStatus == OpenGl_RT_NONE)
fc73a202 1209 {
91c60b57 1210 if (!theGlContext->IsGlGreaterEqual (3, 1))
fc73a202 1211 {
91c60b57 1212 return safeFailBack ("Ray-tracing requires OpenGL 3.1 and higher", theGlContext);
25ef750e 1213 }
91c60b57 1214 else if (!theGlContext->arbTboRGB32)
25ef750e 1215 {
91c60b57 1216 return safeFailBack ("Ray-tracing requires OpenGL 4.0+ or GL_ARB_texture_buffer_object_rgb32 extension", theGlContext);
1217 }
1218 else if (!theGlContext->arbFBOBlit)
1219 {
1220 return safeFailBack ("Ray-tracing requires EXT_framebuffer_blit extension", theGlContext);
fc73a202 1221 }
5322131b 1222
25ef750e 1223 myRaytraceParameters.NbBounces = theCView.RenderParams.RaytracingDepth;
bc8c79bb 1224
fc73a202 1225 TCollection_AsciiString aFolder = Graphic3d_ShaderProgram::ShadersFolder();
265d4508 1226
fc73a202 1227 if (aFolder.IsEmpty())
1228 {
91c60b57 1229 return safeFailBack ("Failed to locate shaders directory", theGlContext);
fc73a202 1230 }
265d4508 1231
fc73a202 1232 if (myIsRaytraceDataValid)
1233 {
bc8c79bb 1234 myRaytraceParameters.StackSize = Max (THE_DEFAULT_STACK_SIZE,
fc73a202 1235 myRaytraceGeometry.HighLevelTreeDepth() + myRaytraceGeometry.BottomLevelTreeDepth());
1236 }
265d4508 1237
91c60b57 1238 TCollection_AsciiString aPrefixString = generateShaderPrefix (theGlContext);
bc8c79bb 1239
1240#ifdef RAY_TRACE_PRINT_INFO
1241 std::cout << "GLSL prefix string:" << std::endl << aPrefixString << std::endl;
1242#endif
1243
fc73a202 1244 {
91c60b57 1245 Handle(OpenGl_ShaderObject) aBasicVertShader = initShader (
1246 GL_VERTEX_SHADER, ShaderSource (aFolder + "/RaytraceBase.vs"), theGlContext);
265d4508 1247
fc73a202 1248 if (aBasicVertShader.IsNull())
1249 {
91c60b57 1250 return safeFailBack ("Failed to initialize ray-trace vertex shader", theGlContext);
fc73a202 1251 }
265d4508 1252
07039714 1253 TCollection_AsciiString aFiles[] = { aFolder + "/RaytraceBase.fs",
1254 aFolder + "/RaytraceRender.fs" };
265d4508 1255
fc73a202 1256 myRaytraceShaderSource.Load (aFiles, 2);
e276548b 1257
bc8c79bb 1258 myRaytraceShaderSource.SetPrefix (aPrefixString);
265d4508 1259
91c60b57 1260 myRaytraceShader = initShader (GL_FRAGMENT_SHADER, myRaytraceShaderSource, theGlContext);
265d4508 1261
fc73a202 1262 if (myRaytraceShader.IsNull())
1263 {
91c60b57 1264 aBasicVertShader->Release (theGlContext.operator->());
265d4508 1265
91c60b57 1266 return safeFailBack ("Failed to initialize ray-trace fragment shader", theGlContext);
fc73a202 1267 }
265d4508 1268
fc73a202 1269 myRaytraceProgram = new OpenGl_ShaderProgram;
5322131b 1270
91c60b57 1271 if (!myRaytraceProgram->Create (theGlContext))
fc73a202 1272 {
91c60b57 1273 aBasicVertShader->Release (theGlContext.operator->());
265d4508 1274
91c60b57 1275 return safeFailBack ("Failed to create ray-trace shader program", theGlContext);
fc73a202 1276 }
265d4508 1277
91c60b57 1278 if (!myRaytraceProgram->AttachShader (theGlContext, aBasicVertShader)
1279 || !myRaytraceProgram->AttachShader (theGlContext, myRaytraceShader))
fc73a202 1280 {
91c60b57 1281 aBasicVertShader->Release (theGlContext.operator->());
265d4508 1282
91c60b57 1283 return safeFailBack ("Failed to attach ray-trace shader objects", theGlContext);
fc73a202 1284 }
265d4508 1285
91c60b57 1286 myRaytraceProgram->SetAttributeName (theGlContext, Graphic3d_TOA_POS, "occVertex");
07039714 1287
1288 TCollection_AsciiString aLinkLog;
1289
91c60b57 1290 if (!myRaytraceProgram->Link (theGlContext))
fc73a202 1291 {
91c60b57 1292 myRaytraceProgram->FetchInfoLog (theGlContext, aLinkLog);
07039714 1293
91c60b57 1294 return safeFailBack (TCollection_ExtendedString (
1295 "Failed to link ray-trace shader program:\n") + aLinkLog, theGlContext);
07039714 1296 }
91c60b57 1297 else if (theGlContext->caps->glslWarnings)
07039714 1298 {
91c60b57 1299 myRaytraceProgram->FetchInfoLog (theGlContext, aLinkLog);
e276548b 1300
07039714 1301 if (!aLinkLog.IsEmpty() && !aLinkLog.IsEqual ("No errors.\n"))
fc73a202 1302 {
07039714 1303 const TCollection_ExtendedString aMessage = TCollection_ExtendedString (
1304 "Ray-trace shader program was linked with following warnings:\n") + aLinkLog;
265d4508 1305
91c60b57 1306 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
07039714 1307 GL_DEBUG_TYPE_PORTABILITY_ARB, 0, GL_DEBUG_SEVERITY_LOW_ARB, aMessage);
1308 }
fc73a202 1309 }
1310 }
265d4508 1311
fc73a202 1312 {
91c60b57 1313 Handle(OpenGl_ShaderObject) aBasicVertShader = initShader (
1314 GL_VERTEX_SHADER, ShaderSource (aFolder + "/RaytraceBase.vs"), theGlContext);
265d4508 1315
fc73a202 1316 if (aBasicVertShader.IsNull())
1317 {
91c60b57 1318 return safeFailBack ("Failed to initialize FSAA vertex shader", theGlContext);
fc73a202 1319 }
265d4508 1320
07039714 1321 TCollection_AsciiString aFiles[] = { aFolder + "/RaytraceBase.fs",
1322 aFolder + "/RaytraceSmooth.fs" };
265d4508 1323
fc73a202 1324 myPostFSAAShaderSource.Load (aFiles, 2);
265d4508 1325
bc8c79bb 1326 myPostFSAAShaderSource.SetPrefix (aPrefixString);
fc73a202 1327
91c60b57 1328 myPostFSAAShader = initShader (GL_FRAGMENT_SHADER, myPostFSAAShaderSource, theGlContext);
265d4508 1329
fc73a202 1330 if (myPostFSAAShader.IsNull())
1331 {
91c60b57 1332 aBasicVertShader->Release (theGlContext.operator->());
265d4508 1333
91c60b57 1334 return safeFailBack ("Failed to initialize FSAA fragment shader", theGlContext);
fc73a202 1335 }
265d4508 1336
fc73a202 1337 myPostFSAAProgram = new OpenGl_ShaderProgram;
265d4508 1338
91c60b57 1339 if (!myPostFSAAProgram->Create (theGlContext))
fc73a202 1340 {
91c60b57 1341 aBasicVertShader->Release (theGlContext.operator->());
265d4508 1342
91c60b57 1343 return safeFailBack ("Failed to create FSAA shader program", theGlContext);
fc73a202 1344 }
1345
91c60b57 1346 if (!myPostFSAAProgram->AttachShader (theGlContext, aBasicVertShader)
1347 || !myPostFSAAProgram->AttachShader (theGlContext, myPostFSAAShader))
fc73a202 1348 {
91c60b57 1349 aBasicVertShader->Release (theGlContext.operator->());
fc73a202 1350
91c60b57 1351 return safeFailBack ("Failed to attach FSAA shader objects", theGlContext);
fc73a202 1352 }
1353
91c60b57 1354 myPostFSAAProgram->SetAttributeName (theGlContext, Graphic3d_TOA_POS, "occVertex");
07039714 1355
1356 TCollection_AsciiString aLinkLog;
1357
91c60b57 1358 if (!myPostFSAAProgram->Link (theGlContext))
fc73a202 1359 {
91c60b57 1360 myPostFSAAProgram->FetchInfoLog (theGlContext, aLinkLog);
07039714 1361
91c60b57 1362 return safeFailBack (TCollection_ExtendedString (
1363 "Failed to link FSAA shader program:\n") + aLinkLog, theGlContext);
07039714 1364 }
91c60b57 1365 else if (theGlContext->caps->glslWarnings)
07039714 1366 {
91c60b57 1367 myPostFSAAProgram->FetchInfoLog (theGlContext, aLinkLog);
fc73a202 1368
07039714 1369 if (!aLinkLog.IsEmpty() && !aLinkLog.IsEqual ("No errors.\n"))
fc73a202 1370 {
07039714 1371 const TCollection_ExtendedString aMessage = TCollection_ExtendedString (
1372 "FSAA shader program was linked with following warnings:\n") + aLinkLog;
1373
91c60b57 1374 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
07039714 1375 GL_DEBUG_TYPE_PORTABILITY_ARB, 0, GL_DEBUG_SEVERITY_LOW_ARB, aMessage);
fc73a202 1376 }
fc73a202 1377 }
e276548b 1378 }
fc73a202 1379 }
e276548b 1380
91c60b57 1381 if (myRaytraceInitStatus == OpenGl_RT_NONE || aToRebuildShaders)
fc73a202 1382 {
1383 for (Standard_Integer anIndex = 0; anIndex < 2; ++anIndex)
e276548b 1384 {
fc73a202 1385 Handle(OpenGl_ShaderProgram)& aShaderProgram =
1386 (anIndex == 0) ? myRaytraceProgram : myPostFSAAProgram;
1387
91c60b57 1388 theGlContext->BindProgram (aShaderProgram);
fc73a202 1389
91c60b57 1390 aShaderProgram->SetSampler (theGlContext,
fc73a202 1391 "uSceneMinPointTexture", OpenGl_RT_SceneMinPointTexture);
91c60b57 1392 aShaderProgram->SetSampler (theGlContext,
fc73a202 1393 "uSceneMaxPointTexture", OpenGl_RT_SceneMaxPointTexture);
91c60b57 1394 aShaderProgram->SetSampler (theGlContext,
fc73a202 1395 "uSceneNodeInfoTexture", OpenGl_RT_SceneNodeInfoTexture);
91c60b57 1396 aShaderProgram->SetSampler (theGlContext,
fc73a202 1397 "uGeometryVertexTexture", OpenGl_RT_GeometryVertexTexture);
91c60b57 1398 aShaderProgram->SetSampler (theGlContext,
fc73a202 1399 "uGeometryNormalTexture", OpenGl_RT_GeometryNormalTexture);
91c60b57 1400 aShaderProgram->SetSampler (theGlContext,
25ef750e 1401 "uGeometryTexCrdTexture", OpenGl_RT_GeometryTexCrdTexture);
91c60b57 1402 aShaderProgram->SetSampler (theGlContext,
25ef750e 1403 "uGeometryTriangTexture", OpenGl_RT_GeometryTriangTexture);
91c60b57 1404 aShaderProgram->SetSampler (theGlContext,
84c71f29 1405 "uSceneTransformTexture", OpenGl_RT_SceneTransformTexture);
91c60b57 1406 aShaderProgram->SetSampler (theGlContext,
84c71f29 1407 "uEnvironmentMapTexture", OpenGl_RT_EnvironmentMapTexture);
91c60b57 1408 aShaderProgram->SetSampler (theGlContext,
25ef750e 1409 "uRaytraceMaterialTexture", OpenGl_RT_RaytraceMaterialTexture);
91c60b57 1410 aShaderProgram->SetSampler (theGlContext,
25ef750e 1411 "uRaytraceLightSrcTexture", OpenGl_RT_RaytraceLightSrcTexture);
fc73a202 1412
91c60b57 1413 aShaderProgram->SetSampler (theGlContext,
a89742cf 1414 "uOpenGlColorTexture", OpenGl_RT_OpenGlColorTexture);
91c60b57 1415 aShaderProgram->SetSampler (theGlContext,
a89742cf 1416 "uOpenGlDepthTexture", OpenGl_RT_OpenGlDepthTexture);
1417
fc73a202 1418 if (anIndex == 1)
1419 {
91c60b57 1420 aShaderProgram->SetSampler (theGlContext,
fc73a202 1421 "uFSAAInputTexture", OpenGl_RT_FSAAInputTexture);
1422 }
265d4508 1423
fc73a202 1424 myUniformLocations[anIndex][OpenGl_RT_aPosition] =
91c60b57 1425 aShaderProgram->GetAttributeLocation (theGlContext, "occVertex");
fc73a202 1426
1427 myUniformLocations[anIndex][OpenGl_RT_uOriginLB] =
91c60b57 1428 aShaderProgram->GetUniformLocation (theGlContext, "uOriginLB");
fc73a202 1429 myUniformLocations[anIndex][OpenGl_RT_uOriginRB] =
91c60b57 1430 aShaderProgram->GetUniformLocation (theGlContext, "uOriginRB");
fc73a202 1431 myUniformLocations[anIndex][OpenGl_RT_uOriginLT] =
91c60b57 1432 aShaderProgram->GetUniformLocation (theGlContext, "uOriginLT");
fc73a202 1433 myUniformLocations[anIndex][OpenGl_RT_uOriginRT] =
91c60b57 1434 aShaderProgram->GetUniformLocation (theGlContext, "uOriginRT");
fc73a202 1435 myUniformLocations[anIndex][OpenGl_RT_uDirectLB] =
91c60b57 1436 aShaderProgram->GetUniformLocation (theGlContext, "uDirectLB");
fc73a202 1437 myUniformLocations[anIndex][OpenGl_RT_uDirectRB] =
91c60b57 1438 aShaderProgram->GetUniformLocation (theGlContext, "uDirectRB");
fc73a202 1439 myUniformLocations[anIndex][OpenGl_RT_uDirectLT] =
91c60b57 1440 aShaderProgram->GetUniformLocation (theGlContext, "uDirectLT");
fc73a202 1441 myUniformLocations[anIndex][OpenGl_RT_uDirectRT] =
91c60b57 1442 aShaderProgram->GetUniformLocation (theGlContext, "uDirectRT");
25ef750e 1443 myUniformLocations[anIndex][OpenGl_RT_uUnviewMat] =
91c60b57 1444 aShaderProgram->GetUniformLocation (theGlContext, "uUnviewMat");
fc73a202 1445
1446 myUniformLocations[anIndex][OpenGl_RT_uSceneRad] =
91c60b57 1447 aShaderProgram->GetUniformLocation (theGlContext, "uSceneRadius");
fc73a202 1448 myUniformLocations[anIndex][OpenGl_RT_uSceneEps] =
91c60b57 1449 aShaderProgram->GetUniformLocation (theGlContext, "uSceneEpsilon");
25ef750e 1450 myUniformLocations[anIndex][OpenGl_RT_uLightCount] =
91c60b57 1451 aShaderProgram->GetUniformLocation (theGlContext, "uLightCount");
25ef750e 1452 myUniformLocations[anIndex][OpenGl_RT_uLightAmbnt] =
91c60b57 1453 aShaderProgram->GetUniformLocation (theGlContext, "uGlobalAmbient");
fc73a202 1454
1455 myUniformLocations[anIndex][OpenGl_RT_uOffsetX] =
91c60b57 1456 aShaderProgram->GetUniformLocation (theGlContext, "uOffsetX");
fc73a202 1457 myUniformLocations[anIndex][OpenGl_RT_uOffsetY] =
91c60b57 1458 aShaderProgram->GetUniformLocation (theGlContext, "uOffsetY");
fc73a202 1459 myUniformLocations[anIndex][OpenGl_RT_uSamples] =
91c60b57 1460 aShaderProgram->GetUniformLocation (theGlContext, "uSamples");
312a4043 1461 myUniformLocations[anIndex][OpenGl_RT_uWinSizeX] =
91c60b57 1462 aShaderProgram->GetUniformLocation (theGlContext, "uWinSizeX");
312a4043 1463 myUniformLocations[anIndex][OpenGl_RT_uWinSizeY] =
91c60b57 1464 aShaderProgram->GetUniformLocation (theGlContext, "uWinSizeY");
84c71f29 1465
25ef750e 1466 myUniformLocations[anIndex][OpenGl_RT_uTextures] =
91c60b57 1467 aShaderProgram->GetUniformLocation (theGlContext, "uTextureSamplers");
25ef750e 1468
1469 myUniformLocations[anIndex][OpenGl_RT_uShadEnabled] =
91c60b57 1470 aShaderProgram->GetUniformLocation (theGlContext, "uShadowsEnable");
25ef750e 1471 myUniformLocations[anIndex][OpenGl_RT_uReflEnabled] =
91c60b57 1472 aShaderProgram->GetUniformLocation (theGlContext, "uReflectionsEnable");
25ef750e 1473 myUniformLocations[anIndex][OpenGl_RT_uEnvMapEnable] =
91c60b57 1474 aShaderProgram->GetUniformLocation (theGlContext, "uEnvironmentEnable");
fc73a202 1475 }
265d4508 1476
91c60b57 1477 theGlContext->BindProgram (NULL);
fc73a202 1478 }
265d4508 1479
91c60b57 1480 if (myRaytraceInitStatus != OpenGl_RT_NONE)
fc73a202 1481 {
91c60b57 1482 return myRaytraceInitStatus == OpenGl_RT_INIT;
fc73a202 1483 }
265d4508 1484
fc73a202 1485 if (myRaytraceFBO1.IsNull())
1486 {
1487 myRaytraceFBO1 = new OpenGl_FrameBuffer;
1488 }
265d4508 1489
fc73a202 1490 if (myRaytraceFBO2.IsNull())
1491 {
1492 myRaytraceFBO2 = new OpenGl_FrameBuffer;
1493 }
265d4508 1494
fc73a202 1495 const GLfloat aVertices[] = { -1.f, -1.f, 0.f,
1496 -1.f, 1.f, 0.f,
1497 1.f, 1.f, 0.f,
1498 1.f, 1.f, 0.f,
1499 1.f, -1.f, 0.f,
1500 -1.f, -1.f, 0.f };
265d4508 1501
91c60b57 1502 myRaytraceScreenQuad.Init (theGlContext, 3, 6, aVertices);
265d4508 1503
91c60b57 1504 myRaytraceInitStatus = OpenGl_RT_INIT; // initialized in normal way
ca3c13d1 1505
fc73a202 1506 return Standard_True;
1507}
265d4508 1508
fc73a202 1509// =======================================================================
1510// function : NullifyResource
1511// purpose :
1512// =======================================================================
91c60b57 1513inline void NullifyResource (const Handle(OpenGl_Context)& theGlContext,
fc73a202 1514 Handle(OpenGl_Resource)& theResource)
1515{
1516 if (!theResource.IsNull())
1517 {
91c60b57 1518 theResource->Release (theGlContext.operator->());
fc73a202 1519 theResource.Nullify();
1520 }
1521}
265d4508 1522
fc73a202 1523// =======================================================================
1524// function : ReleaseRaytraceResources
1525// purpose : Releases OpenGL/GLSL shader programs
1526// =======================================================================
91c60b57 1527void OpenGl_View::releaseRaytraceResources (const Handle(OpenGl_Context)& theGlContext)
fc73a202 1528{
91c60b57 1529 NullifyResource (theGlContext, myOpenGlFBO);
1530 NullifyResource (theGlContext, myRaytraceFBO1);
1531 NullifyResource (theGlContext, myRaytraceFBO2);
265d4508 1532
91c60b57 1533 NullifyResource (theGlContext, myRaytraceShader);
1534 NullifyResource (theGlContext, myPostFSAAShader);
265d4508 1535
91c60b57 1536 NullifyResource (theGlContext, myRaytraceProgram);
1537 NullifyResource (theGlContext, myPostFSAAProgram);
265d4508 1538
91c60b57 1539 NullifyResource (theGlContext, mySceneNodeInfoTexture);
1540 NullifyResource (theGlContext, mySceneMinPointTexture);
1541 NullifyResource (theGlContext, mySceneMaxPointTexture);
265d4508 1542
91c60b57 1543 NullifyResource (theGlContext, myGeometryVertexTexture);
1544 NullifyResource (theGlContext, myGeometryNormalTexture);
1545 NullifyResource (theGlContext, myGeometryTexCrdTexture);
1546 NullifyResource (theGlContext, myGeometryTriangTexture);
1547 NullifyResource (theGlContext, mySceneTransformTexture);
265d4508 1548
91c60b57 1549 NullifyResource (theGlContext, myRaytraceLightSrcTexture);
1550 NullifyResource (theGlContext, myRaytraceMaterialTexture);
265d4508 1551
fc73a202 1552 if (myRaytraceScreenQuad.IsValid())
91c60b57 1553 myRaytraceScreenQuad.Release (theGlContext.operator->());
1554}
1555
1556// =======================================================================
1557// function : ResizeRaytraceBuffers
1558// purpose : Resizes OpenGL frame buffers
1559// =======================================================================
1560Standard_Boolean OpenGl_View::resizeRaytraceBuffers (const Standard_Integer theSizeX,
1561 const Standard_Integer theSizeY,
1562 const Handle(OpenGl_Context)& theGlContext)
1563{
1564 if (myRaytraceFBO1->GetVPSizeX() != theSizeX
1565 || myRaytraceFBO1->GetVPSizeY() != theSizeY)
1566 {
1567 myRaytraceFBO1->Init (theGlContext, theSizeX, theSizeY);
1568 myRaytraceFBO2->Init (theGlContext, theSizeX, theSizeY);
1569 }
1570
1571 return Standard_True;
1572}
1573
1574// =======================================================================
1575// function : UpdateCamera
1576// purpose : Generates viewing rays for corners of screen quad
1577// =======================================================================
1578void OpenGl_View::updateCamera (const OpenGl_Mat4& theOrientation,
1579 const OpenGl_Mat4& theViewMapping,
1580 OpenGl_Vec3* theOrigins,
1581 OpenGl_Vec3* theDirects,
1582 OpenGl_Mat4& theUnview)
1583{
1584 // compute inverse model-view-projection matrix
1585 (theViewMapping * theOrientation).Inverted (theUnview);
1586
1587 Standard_Integer aOriginIndex = 0;
1588 Standard_Integer aDirectIndex = 0;
1589
1590 for (Standard_Integer aY = -1; aY <= 1; aY += 2)
1591 {
1592 for (Standard_Integer aX = -1; aX <= 1; aX += 2)
1593 {
1594 OpenGl_Vec4 aOrigin (GLfloat(aX),
1595 GLfloat(aY),
1596 -1.0f,
1597 1.0f);
1598
1599 aOrigin = theUnview * aOrigin;
1600
1601 aOrigin.x() = aOrigin.x() / aOrigin.w();
1602 aOrigin.y() = aOrigin.y() / aOrigin.w();
1603 aOrigin.z() = aOrigin.z() / aOrigin.w();
1604
1605 OpenGl_Vec4 aDirect (GLfloat(aX),
1606 GLfloat(aY),
1607 1.0f,
1608 1.0f);
1609
1610 aDirect = theUnview * aDirect;
1611
1612 aDirect.x() = aDirect.x() / aDirect.w();
1613 aDirect.y() = aDirect.y() / aDirect.w();
1614 aDirect.z() = aDirect.z() / aDirect.w();
1615
1616 aDirect = aDirect - aOrigin;
1617
1618 GLdouble aInvLen = 1.0 / sqrt (aDirect.x() * aDirect.x() +
1619 aDirect.y() * aDirect.y() +
1620 aDirect.z() * aDirect.z());
1621
1622 theOrigins[aOriginIndex++] = OpenGl_Vec3 (static_cast<GLfloat> (aOrigin.x()),
1623 static_cast<GLfloat> (aOrigin.y()),
1624 static_cast<GLfloat> (aOrigin.z()));
1625
1626 theDirects[aDirectIndex++] = OpenGl_Vec3 (static_cast<GLfloat> (aDirect.x() * aInvLen),
1627 static_cast<GLfloat> (aDirect.y() * aInvLen),
1628 static_cast<GLfloat> (aDirect.z() * aInvLen));
1629 }
1630 }
fc73a202 1631}
265d4508 1632
fc73a202 1633// =======================================================================
1634// function : UploadRaytraceData
1635// purpose : Uploads ray-trace data to the GPU
1636// =======================================================================
91c60b57 1637Standard_Boolean OpenGl_View::uploadRaytraceData (const Handle(OpenGl_Context)& theGlContext)
fc73a202 1638{
91c60b57 1639 if (!theGlContext->IsGlGreaterEqual (3, 1))
fc73a202 1640 {
265d4508 1641#ifdef RAY_TRACE_PRINT_INFO
fc73a202 1642 std::cout << "Error: OpenGL version is less than 3.1" << std::endl;
265d4508 1643#endif
fc73a202 1644 return Standard_False;
e276548b 1645 }
1646
265d4508 1647 /////////////////////////////////////////////////////////////////////////////
25ef750e 1648 // Prepare OpenGL textures
1649
91c60b57 1650 if (theGlContext->arbTexBindless != NULL)
25ef750e 1651 {
1652 // If OpenGL driver supports bindless textures we need
1653 // to get unique 64- bit handles for using on the GPU
91c60b57 1654 if (!myRaytraceGeometry.UpdateTextureHandles (theGlContext))
25ef750e 1655 {
1656#ifdef RAY_TRACE_PRINT_INFO
1657 std::cout << "Error: Failed to get OpenGL texture handles" << std::endl;
1658#endif
1659 return Standard_False;
1660 }
1661 }
1662
1663 /////////////////////////////////////////////////////////////////////////////
e2da917a 1664 // Create OpenGL BVH buffers
265d4508 1665
e2da917a 1666 if (mySceneNodeInfoTexture.IsNull()) // create scene BVH buffers
e276548b 1667 {
e2da917a 1668 mySceneNodeInfoTexture = new OpenGl_TextureBufferArb;
1669 mySceneMinPointTexture = new OpenGl_TextureBufferArb;
1670 mySceneMaxPointTexture = new OpenGl_TextureBufferArb;
84c71f29 1671 mySceneTransformTexture = new OpenGl_TextureBufferArb;
265d4508 1672
91c60b57 1673 if (!mySceneNodeInfoTexture->Create (theGlContext)
1674 || !mySceneMinPointTexture->Create (theGlContext)
1675 || !mySceneMaxPointTexture->Create (theGlContext)
1676 || !mySceneTransformTexture->Create (theGlContext))
e276548b 1677 {
fc73a202 1678#ifdef RAY_TRACE_PRINT_INFO
e2da917a 1679 std::cout << "Error: Failed to create scene BVH buffers" << std::endl;
fc73a202 1680#endif
1681 return Standard_False;
1682 }
1683 }
5322131b 1684
e2da917a 1685 if (myGeometryVertexTexture.IsNull()) // create geometry buffers
265d4508 1686 {
fc73a202 1687 myGeometryVertexTexture = new OpenGl_TextureBufferArb;
1688 myGeometryNormalTexture = new OpenGl_TextureBufferArb;
25ef750e 1689 myGeometryTexCrdTexture = new OpenGl_TextureBufferArb;
fc73a202 1690 myGeometryTriangTexture = new OpenGl_TextureBufferArb;
e276548b 1691
91c60b57 1692 if (!myGeometryVertexTexture->Create (theGlContext)
1693 || !myGeometryNormalTexture->Create (theGlContext)
1694 || !myGeometryTexCrdTexture->Create (theGlContext)
1695 || !myGeometryTriangTexture->Create (theGlContext))
fc73a202 1696 {
1697#ifdef RAY_TRACE_PRINT_INFO
1698 std::cout << "Error: Failed to create buffers for triangulation data" << std::endl;
1699#endif
1700 return Standard_False;
1701 }
265d4508 1702 }
5322131b 1703
e2da917a 1704 if (myRaytraceMaterialTexture.IsNull()) // create material buffer
fc73a202 1705 {
1706 myRaytraceMaterialTexture = new OpenGl_TextureBufferArb;
e276548b 1707
91c60b57 1708 if (!myRaytraceMaterialTexture->Create (theGlContext))
fc73a202 1709 {
1710#ifdef RAY_TRACE_PRINT_INFO
1711 std::cout << "Error: Failed to create buffers for material data" << std::endl;
e276548b 1712#endif
fc73a202 1713 return Standard_False;
1714 }
1715 }
e2da917a 1716
84c71f29 1717 /////////////////////////////////////////////////////////////////////////////
1718 // Write transform buffer
1719
1720 BVH_Mat4f* aNodeTransforms = new BVH_Mat4f[myRaytraceGeometry.Size()];
84c71f29 1721
e2da917a 1722 bool aResult = true;
1723
84c71f29 1724 for (Standard_Integer anElemIndex = 0; anElemIndex < myRaytraceGeometry.Size(); ++anElemIndex)
1725 {
1726 OpenGl_TriangleSet* aTriangleSet = dynamic_cast<OpenGl_TriangleSet*> (
1727 myRaytraceGeometry.Objects().ChangeValue (anElemIndex).operator->());
1728
1729 const BVH_Transform<Standard_ShortReal, 4>* aTransform =
1730 dynamic_cast<const BVH_Transform<Standard_ShortReal, 4>* > (aTriangleSet->Properties().operator->());
1731
1732 Standard_ASSERT_RETURN (aTransform != NULL,
1733 "OpenGl_TriangleSet does not contain transform", Standard_False);
1734
1735 aNodeTransforms[anElemIndex] = aTransform->Inversed();
84c71f29 1736 }
1737
91c60b57 1738 aResult &= mySceneTransformTexture->Init (theGlContext, 4,
84c71f29 1739 myRaytraceGeometry.Size() * 4, reinterpret_cast<const GLfloat*> (aNodeTransforms));
1740
25ef750e 1741 delete [] aNodeTransforms;
84c71f29 1742
1743 /////////////////////////////////////////////////////////////////////////////
bc8c79bb 1744 // Write geometry and bottom-level BVH buffers
84c71f29 1745
fc73a202 1746 Standard_Size aTotalVerticesNb = 0;
1747 Standard_Size aTotalElementsNb = 0;
1748 Standard_Size aTotalBVHNodesNb = 0;
265d4508 1749
fc73a202 1750 for (Standard_Integer anElemIndex = 0; anElemIndex < myRaytraceGeometry.Size(); ++anElemIndex)
1751 {
1752 OpenGl_TriangleSet* aTriangleSet = dynamic_cast<OpenGl_TriangleSet*> (
1753 myRaytraceGeometry.Objects().ChangeValue (anElemIndex).operator->());
265d4508 1754
fc73a202 1755 Standard_ASSERT_RETURN (aTriangleSet != NULL,
1756 "Error: Failed to get triangulation of OpenGL element", Standard_False);
1757
1758 aTotalVerticesNb += aTriangleSet->Vertices.size();
1759 aTotalElementsNb += aTriangleSet->Elements.size();
e276548b 1760
fc73a202 1761 Standard_ASSERT_RETURN (!aTriangleSet->BVH().IsNull(),
1762 "Error: Failed to get bottom-level BVH of OpenGL element", Standard_False);
265d4508 1763
fc73a202 1764 aTotalBVHNodesNb += aTriangleSet->BVH()->NodeInfoBuffer().size();
1765 }
e276548b 1766
e2da917a 1767 aTotalBVHNodesNb += myRaytraceGeometry.BVH()->NodeInfoBuffer().size();
1768
fc73a202 1769 if (aTotalBVHNodesNb != 0)
e276548b 1770 {
e2da917a 1771 aResult &= mySceneNodeInfoTexture->Init (
91c60b57 1772 theGlContext, 4, GLsizei (aTotalBVHNodesNb), static_cast<const GLuint*> (NULL));
e2da917a 1773 aResult &= mySceneMinPointTexture->Init (
91c60b57 1774 theGlContext, 3, GLsizei (aTotalBVHNodesNb), static_cast<const GLfloat*> (NULL));
e2da917a 1775 aResult &= mySceneMaxPointTexture->Init (
91c60b57 1776 theGlContext, 3, GLsizei (aTotalBVHNodesNb), static_cast<const GLfloat*> (NULL));
fc73a202 1777 }
265d4508 1778
fc73a202 1779 if (!aResult)
1780 {
1781#ifdef RAY_TRACE_PRINT_INFO
1782 std::cout << "Error: Failed to upload buffers for bottom-level scene BVH" << std::endl;
1783#endif
1784 return Standard_False;
1785 }
e276548b 1786
fc73a202 1787 if (aTotalElementsNb != 0)
1788 {
25ef750e 1789 aResult &= myGeometryTriangTexture->Init (
91c60b57 1790 theGlContext, 4, GLsizei (aTotalElementsNb), static_cast<const GLuint*> (NULL));
fc73a202 1791 }
265d4508 1792
fc73a202 1793 if (aTotalVerticesNb != 0)
1794 {
25ef750e 1795 aResult &= myGeometryVertexTexture->Init (
91c60b57 1796 theGlContext, 3, GLsizei (aTotalVerticesNb), static_cast<const GLfloat*> (NULL));
25ef750e 1797 aResult &= myGeometryNormalTexture->Init (
91c60b57 1798 theGlContext, 3, GLsizei (aTotalVerticesNb), static_cast<const GLfloat*> (NULL));
25ef750e 1799 aResult &= myGeometryTexCrdTexture->Init (
91c60b57 1800 theGlContext, 2, GLsizei (aTotalVerticesNb), static_cast<const GLfloat*> (NULL));
fc73a202 1801 }
265d4508 1802
fc73a202 1803 if (!aResult)
1804 {
1805#ifdef RAY_TRACE_PRINT_INFO
1806 std::cout << "Error: Failed to upload buffers for scene geometry" << std::endl;
1807#endif
1808 return Standard_False;
1809 }
265d4508 1810
e2da917a 1811 const NCollection_Handle<BVH_Tree<Standard_ShortReal, 3> >& aBVH = myRaytraceGeometry.BVH();
91c60b57 1812
1813 if (aBVH->Length() > 0)
1e99558f 1814 {
91c60b57 1815 aResult &= mySceneNodeInfoTexture->SubData (theGlContext, 0, aBVH->Length(),
1e99558f 1816 reinterpret_cast<const GLuint*> (&aBVH->NodeInfoBuffer().front()));
91c60b57 1817 aResult &= mySceneMinPointTexture->SubData (theGlContext, 0, aBVH->Length(),
1e99558f 1818 reinterpret_cast<const GLfloat*> (&aBVH->MinPointBuffer().front()));
91c60b57 1819 aResult &= mySceneMaxPointTexture->SubData (theGlContext, 0, aBVH->Length(),
1e99558f 1820 reinterpret_cast<const GLfloat*> (&aBVH->MaxPointBuffer().front()));
1821 }
e2da917a 1822
fc73a202 1823 for (Standard_Integer aNodeIdx = 0; aNodeIdx < aBVH->Length(); ++aNodeIdx)
1824 {
1825 if (!aBVH->IsOuter (aNodeIdx))
1826 continue;
265d4508 1827
fc73a202 1828 OpenGl_TriangleSet* aTriangleSet = myRaytraceGeometry.TriangleSet (aNodeIdx);
e276548b 1829
fc73a202 1830 Standard_ASSERT_RETURN (aTriangleSet != NULL,
1831 "Error: Failed to get triangulation of OpenGL element", Standard_False);
e276548b 1832
e2da917a 1833 Standard_Integer aBVHOffset = myRaytraceGeometry.AccelerationOffset (aNodeIdx);
e276548b 1834
fc73a202 1835 Standard_ASSERT_RETURN (aBVHOffset != OpenGl_RaytraceGeometry::INVALID_OFFSET,
1836 "Error: Failed to get offset for bottom-level BVH", Standard_False);
e276548b 1837
e2da917a 1838 const Standard_Integer aBvhBuffersSize = aTriangleSet->BVH()->Length();
265d4508 1839
e2da917a 1840 if (aBvhBuffersSize != 0)
fc73a202 1841 {
91c60b57 1842 aResult &= mySceneNodeInfoTexture->SubData (theGlContext, aBVHOffset, aBvhBuffersSize,
1843 reinterpret_cast<const GLuint*> (&aTriangleSet->BVH()->NodeInfoBuffer().front()));
1844 aResult &= mySceneMinPointTexture->SubData (theGlContext, aBVHOffset, aBvhBuffersSize,
1845 reinterpret_cast<const GLfloat*> (&aTriangleSet->BVH()->MinPointBuffer().front()));
1846 aResult &= mySceneMaxPointTexture->SubData (theGlContext, aBVHOffset, aBvhBuffersSize,
1847 reinterpret_cast<const GLfloat*> (&aTriangleSet->BVH()->MaxPointBuffer().front()));
1848
fc73a202 1849 if (!aResult)
e276548b 1850 {
fc73a202 1851#ifdef RAY_TRACE_PRINT_INFO
1852 std::cout << "Error: Failed to upload buffers for bottom-level scene BVHs" << std::endl;
1853#endif
e276548b 1854 return Standard_False;
1855 }
1856 }
1857
fc73a202 1858 const Standard_Integer aVerticesOffset = myRaytraceGeometry.VerticesOffset (aNodeIdx);
265d4508 1859
fc73a202 1860 Standard_ASSERT_RETURN (aVerticesOffset != OpenGl_RaytraceGeometry::INVALID_OFFSET,
1861 "Error: Failed to get offset for triangulation vertices of OpenGL element", Standard_False);
265d4508 1862
fc73a202 1863 if (!aTriangleSet->Vertices.empty())
1864 {
91c60b57 1865 aResult &= myGeometryNormalTexture->SubData (theGlContext, aVerticesOffset,
1866 GLsizei (aTriangleSet->Normals.size()), reinterpret_cast<const GLfloat*> (&aTriangleSet->Normals.front()));
1867 aResult &= myGeometryTexCrdTexture->SubData (theGlContext, aVerticesOffset,
1868 GLsizei (aTriangleSet->TexCrds.size()), reinterpret_cast<const GLfloat*> (&aTriangleSet->TexCrds.front()));
1869 aResult &= myGeometryVertexTexture->SubData (theGlContext, aVerticesOffset,
1870 GLsizei (aTriangleSet->Vertices.size()), reinterpret_cast<const GLfloat*> (&aTriangleSet->Vertices.front()));
fc73a202 1871 }
e276548b 1872
fc73a202 1873 const Standard_Integer anElementsOffset = myRaytraceGeometry.ElementsOffset (aNodeIdx);
265d4508 1874
fc73a202 1875 Standard_ASSERT_RETURN (anElementsOffset != OpenGl_RaytraceGeometry::INVALID_OFFSET,
1876 "Error: Failed to get offset for triangulation elements of OpenGL element", Standard_False);
e276548b 1877
fc73a202 1878 if (!aTriangleSet->Elements.empty())
e276548b 1879 {
91c60b57 1880 aResult &= myGeometryTriangTexture->SubData (theGlContext, anElementsOffset, GLsizei (aTriangleSet->Elements.size()),
5cff985a 1881 reinterpret_cast<const GLuint*> (&aTriangleSet->Elements.front()));
e276548b 1882 }
265d4508 1883
fc73a202 1884 if (!aResult)
1885 {
1886#ifdef RAY_TRACE_PRINT_INFO
1887 std::cout << "Error: Failed to upload triangulation buffers for OpenGL element" << std::endl;
e276548b 1888#endif
fc73a202 1889 return Standard_False;
1890 }
e276548b 1891 }
e276548b 1892
25ef750e 1893 /////////////////////////////////////////////////////////////////////////////
1894 // Write material buffer
1895
fc73a202 1896 if (myRaytraceGeometry.Materials.size() != 0)
1897 {
91c60b57 1898 aResult &= myRaytraceMaterialTexture->Init (theGlContext, 4,
25ef750e 1899 GLsizei (myRaytraceGeometry.Materials.size() * 11), myRaytraceGeometry.Materials.front().Packed());
1900
fc73a202 1901 if (!aResult)
1902 {
1903#ifdef RAY_TRACE_PRINT_INFO
1904 std::cout << "Error: Failed to upload material buffer" << std::endl;
1905#endif
1906 return Standard_False;
1907 }
1908 }
e276548b 1909
fc73a202 1910 myIsRaytraceDataValid = myRaytraceGeometry.Objects().Size() != 0;
e276548b 1911
fc73a202 1912#ifdef RAY_TRACE_PRINT_INFO
e276548b 1913
fc73a202 1914 Standard_ShortReal aMemUsed = 0.f;
e276548b 1915
fc73a202 1916 for (Standard_Integer anElemIdx = 0; anElemIdx < myRaytraceGeometry.Size(); ++anElemIdx)
1917 {
1918 OpenGl_TriangleSet* aTriangleSet = dynamic_cast<OpenGl_TriangleSet*> (
1919 myRaytraceGeometry.Objects().ChangeValue (anElemIdx).operator->());
e276548b 1920
fc73a202 1921 aMemUsed += static_cast<Standard_ShortReal> (
25ef750e 1922 aTriangleSet->Vertices.size() * sizeof (BVH_Vec3f));
fc73a202 1923 aMemUsed += static_cast<Standard_ShortReal> (
25ef750e 1924 aTriangleSet->Normals.size() * sizeof (BVH_Vec3f));
1925 aMemUsed += static_cast<Standard_ShortReal> (
1926 aTriangleSet->TexCrds.size() * sizeof (BVH_Vec2f));
fc73a202 1927 aMemUsed += static_cast<Standard_ShortReal> (
1928 aTriangleSet->Elements.size() * sizeof (BVH_Vec4i));
e276548b 1929
fc73a202 1930 aMemUsed += static_cast<Standard_ShortReal> (
1931 aTriangleSet->BVH()->NodeInfoBuffer().size() * sizeof (BVH_Vec4i));
1932 aMemUsed += static_cast<Standard_ShortReal> (
25ef750e 1933 aTriangleSet->BVH()->MinPointBuffer().size() * sizeof (BVH_Vec3f));
fc73a202 1934 aMemUsed += static_cast<Standard_ShortReal> (
25ef750e 1935 aTriangleSet->BVH()->MaxPointBuffer().size() * sizeof (BVH_Vec3f));
fc73a202 1936 }
e276548b 1937
fc73a202 1938 aMemUsed += static_cast<Standard_ShortReal> (
1939 myRaytraceGeometry.BVH()->NodeInfoBuffer().size() * sizeof (BVH_Vec4i));
1940 aMemUsed += static_cast<Standard_ShortReal> (
25ef750e 1941 myRaytraceGeometry.BVH()->MinPointBuffer().size() * sizeof (BVH_Vec3f));
fc73a202 1942 aMemUsed += static_cast<Standard_ShortReal> (
25ef750e 1943 myRaytraceGeometry.BVH()->MaxPointBuffer().size() * sizeof (BVH_Vec3f));
e276548b 1944
fc73a202 1945 std::cout << "GPU Memory Used (MB): ~" << aMemUsed / 1048576 << std::endl;
e276548b 1946
fc73a202 1947#endif
e276548b 1948
fc73a202 1949 return aResult;
1950}
e276548b 1951
fc73a202 1952// =======================================================================
91c60b57 1953// function : UpdateRaytraceLightSources
1954// purpose : Updates 3D scene light sources for ray-tracing
fc73a202 1955// =======================================================================
91c60b57 1956Standard_Boolean OpenGl_View::updateRaytraceLightSources (const OpenGl_Mat4& theInvModelView, const Handle(OpenGl_Context)& theGlContext)
fc73a202 1957{
91c60b57 1958 myRaytraceGeometry.Sources.clear();
1959
1960 myRaytraceGeometry.Ambient = BVH_Vec4f (0.0f, 0.0f, 0.0f, 0.0f);
1961
1962 for (OpenGl_ListOfLight::Iterator anItl (LightList()); anItl.More(); anItl.Next())
1963 {
1964 const OpenGl_Light& aLight = anItl.Value();
1965
1966 if (aLight.Type == Visual3d_TOLS_AMBIENT)
1967 {
1968 myRaytraceGeometry.Ambient += BVH_Vec4f (aLight.Color.r(),
1969 aLight.Color.g(),
1970 aLight.Color.b(),
1971 0.0f);
1972 continue;
1973 }
1974
1975 BVH_Vec4f aDiffuse (aLight.Color.r(),
1976 aLight.Color.g(),
1977 aLight.Color.b(),
1978 1.0f);
1979
1980 BVH_Vec4f aPosition (-aLight.Direction.x(),
1981 -aLight.Direction.y(),
1982 -aLight.Direction.z(),
1983 0.0f);
1984
1985 if (aLight.Type != Visual3d_TOLS_DIRECTIONAL)
1986 {
1987 aPosition = BVH_Vec4f (aLight.Position.x(),
1988 aLight.Position.y(),
1989 aLight.Position.z(),
1990 1.0f);
1991 }
1992
1993 if (aLight.IsHeadlight)
1994 {
1995 aPosition = theInvModelView * aPosition;
1996 }
1997
1998 myRaytraceGeometry.Sources.push_back (OpenGl_RaytraceLight (aDiffuse, aPosition));
1999 }
2000
2001 if (myRaytraceLightSrcTexture.IsNull()) // create light source buffer
2002 {
2003 myRaytraceLightSrcTexture = new OpenGl_TextureBufferArb;
2004
2005 if (!myRaytraceLightSrcTexture->Create (theGlContext))
2006 {
2007#ifdef RAY_TRACE_PRINT_INFO
2008 std::cout << "Error: Failed to create light source buffer" << std::endl;
2009#endif
2010 return Standard_False;
2011 }
2012 }
2013
2014 if (myRaytraceGeometry.Sources.size() != 0)
fc73a202 2015 {
91c60b57 2016 const GLfloat* aDataPtr = myRaytraceGeometry.Sources.front().Packed();
2017 if (!myRaytraceLightSrcTexture->Init (theGlContext, 4, GLsizei (myRaytraceGeometry.Sources.size() * 2), aDataPtr))
2018 {
2019#ifdef RAY_TRACE_PRINT_INFO
2020 std::cout << "Error: Failed to upload light source buffer" << std::endl;
2021#endif
2022 return Standard_False;
2023 }
fc73a202 2024 }
e276548b 2025
fc73a202 2026 return Standard_True;
e276548b 2027}
2028
2029// =======================================================================
91c60b57 2030// function : UpdateRaytraceEnvironmentMap
2031// purpose : Updates environment map for ray-tracing
e276548b 2032// =======================================================================
91c60b57 2033Standard_Boolean OpenGl_View::updateRaytraceEnvironmentMap (const Handle(OpenGl_Context)& theGlContext)
e276548b 2034{
91c60b57 2035 Standard_Boolean aResult = Standard_True;
e276548b 2036
91c60b57 2037 if (!myToUpdateEnvironmentMap)
e276548b 2038 {
91c60b57 2039 return aResult;
2040 }
e276548b 2041
91c60b57 2042 for (Standard_Integer anIdx = 0; anIdx < 2; ++anIdx)
2043 {
2044 const Handle(OpenGl_ShaderProgram)& aProgram =
2045 anIdx == 0 ? myRaytraceProgram : myPostFSAAProgram;
fc73a202 2046
91c60b57 2047 if (!aProgram.IsNull())
2048 {
2049 aResult &= theGlContext->BindProgram (aProgram);
e276548b 2050
91c60b57 2051 if (!myTextureEnv.IsNull() && mySurfaceDetail != Visual3d_TOD_NONE)
2052 {
2053 myTextureEnv->Bind (theGlContext,
2054 GL_TEXTURE0 + OpenGl_RT_EnvironmentMapTexture);
b5ac8292 2055
91c60b57 2056 aResult &= aProgram->SetUniform (theGlContext,
2057 myUniformLocations[anIdx][OpenGl_RT_uEnvMapEnable], 1);
2058 }
2059 else
2060 {
2061 aResult &= aProgram->SetUniform (theGlContext,
2062 myUniformLocations[anIdx][OpenGl_RT_uEnvMapEnable], 0);
2063 }
2064 }
2065 }
e276548b 2066
91c60b57 2067 myToUpdateEnvironmentMap = Standard_False;
e276548b 2068
91c60b57 2069 theGlContext->BindProgram (NULL);
fc73a202 2070
91c60b57 2071 return aResult;
fc73a202 2072}
2073
2074// =======================================================================
25ef750e 2075// function : SetUniformState
2076// purpose : Sets uniform state for the given ray-tracing shader program
2077// =======================================================================
91c60b57 2078Standard_Boolean OpenGl_View::setUniformState (const Graphic3d_CView& theCView,
2079 const OpenGl_Vec3* theOrigins,
2080 const OpenGl_Vec3* theDirects,
2081 const OpenGl_Mat4& theUnviewMat,
2082 const Standard_Integer theProgramId,
2083 const Handle(OpenGl_Context)& theGlContext)
25ef750e 2084{
91c60b57 2085 Handle(OpenGl_ShaderProgram)& theProgram =
2086 theProgramId == 0 ? myRaytraceProgram : myPostFSAAProgram;
2087
2088 if (theProgram.IsNull())
25ef750e 2089 {
2090 return Standard_False;
2091 }
2092
25ef750e 2093 Standard_Boolean aResult = Standard_True;
2094
91c60b57 2095 const Standard_Integer aLightSourceBufferSize =
2096 static_cast<Standard_Integer> (myRaytraceGeometry.Sources.size());
2097
25ef750e 2098 // Set camera state
91c60b57 2099 aResult &= theProgram->SetUniform (theGlContext,
2100 myUniformLocations[theProgramId][OpenGl_RT_uOriginLB], theOrigins[0]);
2101 aResult &= theProgram->SetUniform (theGlContext,
2102 myUniformLocations[theProgramId][OpenGl_RT_uOriginRB], theOrigins[1]);
2103 aResult &= theProgram->SetUniform (theGlContext,
2104 myUniformLocations[theProgramId][OpenGl_RT_uOriginLT], theOrigins[2]);
2105 aResult &= theProgram->SetUniform (theGlContext,
2106 myUniformLocations[theProgramId][OpenGl_RT_uOriginRT], theOrigins[3]);
2107 aResult &= theProgram->SetUniform (theGlContext,
2108 myUniformLocations[theProgramId][OpenGl_RT_uDirectLB], theDirects[0]);
2109 aResult &= theProgram->SetUniform (theGlContext,
2110 myUniformLocations[theProgramId][OpenGl_RT_uDirectRB], theDirects[1]);
2111 aResult &= theProgram->SetUniform (theGlContext,
2112 myUniformLocations[theProgramId][OpenGl_RT_uDirectLT], theDirects[2]);
2113 aResult &= theProgram->SetUniform (theGlContext,
2114 myUniformLocations[theProgramId][OpenGl_RT_uDirectRT], theDirects[3]);
2115 aResult &= theProgram->SetUniform (theGlContext,
2116 myUniformLocations[theProgramId][OpenGl_RT_uUnviewMat], theUnviewMat);
312a4043 2117
25ef750e 2118 // Set scene parameters
91c60b57 2119 aResult &= theProgram->SetUniform (theGlContext,
2120 myUniformLocations[theProgramId][OpenGl_RT_uSceneRad], myRaytraceSceneRadius);
2121 aResult &= theProgram->SetUniform (theGlContext,
2122 myUniformLocations[theProgramId][OpenGl_RT_uSceneEps], myRaytraceSceneEpsilon);
2123 aResult &= theProgram->SetUniform (theGlContext,
2124 myUniformLocations[theProgramId][OpenGl_RT_uLightCount], aLightSourceBufferSize);
2125 aResult &= theProgram->SetUniform (theGlContext,
2126 myUniformLocations[theProgramId][OpenGl_RT_uLightAmbnt], myRaytraceGeometry.Ambient);
2127
2128 // Set run-time rendering options
2129 aResult &= theProgram->SetUniform (theGlContext,
2130 myUniformLocations[theProgramId][OpenGl_RT_uShadEnabled], theCView.RenderParams.IsShadowEnabled ? 1 : 0);
2131 aResult &= theProgram->SetUniform (theGlContext,
2132 myUniformLocations[theProgramId][OpenGl_RT_uReflEnabled], theCView.RenderParams.IsReflectionEnabled ? 1 : 0);
25ef750e 2133
2134 // Set array of 64-bit texture handles
91c60b57 2135 if (theGlContext->arbTexBindless != NULL && myRaytraceGeometry.HasTextures())
25ef750e 2136 {
91c60b57 2137 aResult &= theProgram->SetUniform (theGlContext, "uTextureSamplers", static_cast<GLsizei> (
2138 myRaytraceGeometry.TextureHandles().size()), &myRaytraceGeometry.TextureHandles()[0]);
25ef750e 2139 }
2140
2141 if (!aResult)
2142 {
2143#ifdef RAY_TRACE_PRINT_INFO
91c60b57 2144 std::cout << "Info: Not all uniforms were detected for program " << theProgramId << std::endl;
25ef750e 2145#endif
2146 }
2147
2148 return aResult;
2149}
2150
2151// =======================================================================
91c60b57 2152// function : BindRaytraceTextures
2153// purpose : Binds ray-trace textures to corresponding texture units
2154// =======================================================================
2155void OpenGl_View::bindRaytraceTextures (const Handle(OpenGl_Context)& theGlContext)
2156{
2157 mySceneMinPointTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneMinPointTexture);
2158 mySceneMaxPointTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneMaxPointTexture);
2159 mySceneNodeInfoTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneNodeInfoTexture);
2160 myGeometryVertexTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryVertexTexture);
2161 myGeometryNormalTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryNormalTexture);
2162 myGeometryTexCrdTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryTexCrdTexture);
2163 myGeometryTriangTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryTriangTexture);
2164 mySceneTransformTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneTransformTexture);
2165 myRaytraceMaterialTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_RaytraceMaterialTexture);
2166 myRaytraceLightSrcTexture->BindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_RaytraceLightSrcTexture);
2167
2168 if (!myOpenGlFBO.IsNull())
2169 {
2170 myOpenGlFBO->ColorTexture()->Bind (theGlContext, GL_TEXTURE0 + OpenGl_RT_OpenGlColorTexture);
2171 myOpenGlFBO->DepthStencilTexture()->Bind (theGlContext, GL_TEXTURE0 + OpenGl_RT_OpenGlDepthTexture);
2172 }
2173}
2174
2175// =======================================================================
2176// function : UnbindRaytraceTextures
2177// purpose : Unbinds ray-trace textures from corresponding texture units
2178// =======================================================================
2179void OpenGl_View::unbindRaytraceTextures (const Handle(OpenGl_Context)& theGlContext)
2180{
2181 mySceneMinPointTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneMinPointTexture);
2182 mySceneMaxPointTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneMaxPointTexture);
2183 mySceneNodeInfoTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneNodeInfoTexture);
2184 myGeometryVertexTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryVertexTexture);
2185 myGeometryNormalTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryNormalTexture);
2186 myGeometryTexCrdTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryTexCrdTexture);
2187 myGeometryTriangTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryTriangTexture);
2188 mySceneTransformTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_SceneTransformTexture);
2189 myRaytraceMaterialTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_RaytraceMaterialTexture);
2190 myRaytraceLightSrcTexture->UnbindTexture (theGlContext, GL_TEXTURE0 + OpenGl_RT_RaytraceLightSrcTexture);
2191
2192 if (!myOpenGlFBO.IsNull())
2193 {
2194 myOpenGlFBO->ColorTexture()->Unbind (theGlContext, GL_TEXTURE0 + OpenGl_RT_OpenGlColorTexture);
2195 myOpenGlFBO->DepthStencilTexture()->Unbind (theGlContext, GL_TEXTURE0 + OpenGl_RT_OpenGlDepthTexture);
2196 }
2197
2198 theGlContext->core15fwd->glActiveTexture (GL_TEXTURE0);
2199}
2200
2201// =======================================================================
fc73a202 2202// function : RunRaytraceShaders
2203// purpose : Runs ray-tracing shader programs
2204// =======================================================================
91c60b57 2205Standard_Boolean OpenGl_View::runRaytraceShaders (const Graphic3d_CView& theCView,
2206 const Standard_Integer theSizeX,
2207 const Standard_Integer theSizeY,
2208 const OpenGl_Vec3* theOrigins,
2209 const OpenGl_Vec3* theDirects,
2210 const OpenGl_Mat4& theUnviewMat,
38a0206f 2211 OpenGl_FrameBuffer* theReadDrawFbo,
91c60b57 2212 const Handle(OpenGl_Context)& theGlContext)
fc73a202 2213{
91c60b57 2214 bindRaytraceTextures (theGlContext);
fc73a202 2215
bc8c79bb 2216 if (theCView.RenderParams.IsAntialiasingEnabled) // render source image to FBO
fc73a202 2217 {
91c60b57 2218 myRaytraceFBO1->BindBuffer (theGlContext);
2219
fc73a202 2220 glDisable (GL_BLEND);
2221 }
2222
91c60b57 2223 Standard_Boolean aResult = theGlContext->BindProgram (myRaytraceProgram);
fc73a202 2224
91c60b57 2225 aResult &= setUniformState (theCView,
2226 theOrigins,
2227 theDirects,
2228 theUnviewMat,
2229 0, // ID of RT program
2230 theGlContext);
fc73a202 2231
91c60b57 2232 myRaytraceScreenQuad.BindVertexAttrib (theGlContext, Graphic3d_TOA_POS);
fc73a202 2233 {
91c60b57 2234 if (aResult)
2235 theGlContext->core20fwd->glDrawArrays (GL_TRIANGLES, 0, 6);
fc73a202 2236 }
91c60b57 2237 myRaytraceScreenQuad.UnbindVertexAttrib (theGlContext, Graphic3d_TOA_POS);
25ef750e 2238
91c60b57 2239 if (!theCView.RenderParams.IsAntialiasingEnabled || !aResult)
fc73a202 2240 {
91c60b57 2241 unbindRaytraceTextures (theGlContext);
fc73a202 2242
91c60b57 2243 theGlContext->BindProgram (NULL);
a89742cf 2244
91c60b57 2245 return aResult;
fc73a202 2246 }
2247
91c60b57 2248 myRaytraceFBO1->ColorTexture()->Bind (theGlContext, GL_TEXTURE0 + OpenGl_RT_FSAAInputTexture);
fc73a202 2249
91c60b57 2250 aResult &= theGlContext->BindProgram (myPostFSAAProgram);
fc73a202 2251
91c60b57 2252 aResult &= setUniformState (theCView,
2253 theOrigins,
2254 theDirects,
2255 theUnviewMat,
2256 1, // ID of FSAA program
2257 theGlContext);
fc73a202 2258
91c60b57 2259 myRaytraceScreenQuad.BindVertexAttrib (theGlContext, Graphic3d_TOA_POS);
fc73a202 2260 {
91c60b57 2261 // Perform multi-pass adaptive FSAA using ping-pong technique.
2262 // We use 'FLIPTRI' sampling pattern changing for every pixel
2263 // (3 additional samples per pixel, the 1st sample is already
2264 // available from initial ray-traced image).
2265 for (Standard_Integer anIt = 1; anIt < 4; ++anIt)
50d0e1ce 2266 {
91c60b57 2267 GLfloat aOffsetX = 1.f / theSizeX;
2268 GLfloat aOffsetY = 1.f / theSizeY;
50d0e1ce 2269
91c60b57 2270 if (anIt == 1)
2271 {
2272 aOffsetX *= -0.55f;
2273 aOffsetY *= 0.55f;
2274 }
2275 else if (anIt == 2)
2276 {
2277 aOffsetX *= 0.00f;
2278 aOffsetY *= -0.55f;
2279 }
2280 else if (anIt == 3)
2281 {
2282 aOffsetX *= 0.55f;
2283 aOffsetY *= 0.00f;
2284 }
fc73a202 2285
91c60b57 2286 aResult &= myPostFSAAProgram->SetUniform (theGlContext,
2287 myUniformLocations[1][OpenGl_RT_uSamples], anIt + 1);
2288 aResult &= myPostFSAAProgram->SetUniform (theGlContext,
2289 myUniformLocations[1][OpenGl_RT_uOffsetX], aOffsetX);
2290 aResult &= myPostFSAAProgram->SetUniform (theGlContext,
2291 myUniformLocations[1][OpenGl_RT_uOffsetY], aOffsetY);
fc73a202 2292
91c60b57 2293 Handle(OpenGl_FrameBuffer)& aFramebuffer = anIt % 2 ? myRaytraceFBO2 : myRaytraceFBO1;
fc73a202 2294
91c60b57 2295 if (anIt == 3) // disable FBO on last iteration
2296 {
2297 glEnable (GL_BLEND);
2298
38a0206f 2299 if (theReadDrawFbo != NULL)
2300 theReadDrawFbo->BindBuffer (theGlContext);
91c60b57 2301 }
2302 else
2303 {
2304 aFramebuffer->BindBuffer (theGlContext);
2305 }
50d0e1ce 2306
91c60b57 2307 theGlContext->core20fwd->glDrawArrays (GL_TRIANGLES, 0, 6);
e276548b 2308
91c60b57 2309 if (anIt != 3) // set input for the next pass
2310 {
2311 aFramebuffer->ColorTexture()->Bind (theGlContext, GL_TEXTURE0 + OpenGl_RT_FSAAInputTexture);
2312 }
e276548b 2313 }
2314 }
91c60b57 2315 myRaytraceScreenQuad.UnbindVertexAttrib (theGlContext, Graphic3d_TOA_POS);
fc73a202 2316
91c60b57 2317 unbindRaytraceTextures (theGlContext);
a89742cf 2318
91c60b57 2319 theGlContext->BindProgram (NULL);
fc73a202 2320
91c60b57 2321 return aResult;
e276548b 2322}
2323
2324// =======================================================================
2325// function : Raytrace
fc73a202 2326// purpose : Redraws the window using OpenGL/GLSL ray-tracing
e276548b 2327// =======================================================================
91c60b57 2328Standard_Boolean OpenGl_View::raytrace (const Graphic3d_CView& theCView,
2329 const Standard_Integer theSizeX,
2330 const Standard_Integer theSizeY,
38a0206f 2331 OpenGl_FrameBuffer* theReadDrawFbo,
91c60b57 2332 const Handle(OpenGl_Context)& theGlContext)
e276548b 2333{
91c60b57 2334 if (!initRaytraceResources (theCView, theGlContext))
2335 {
e276548b 2336 return Standard_False;
91c60b57 2337 }
2338
2339 if (!resizeRaytraceBuffers (theSizeX, theSizeY, theGlContext))
2340 {
2341 return Standard_False;
2342 }
e276548b 2343
91c60b57 2344 if (!updateRaytraceEnvironmentMap (theGlContext))
2345 {
e276548b 2346 return Standard_False;
91c60b57 2347 }
e276548b 2348
2349 // Get model-view and projection matrices
38a0206f 2350 OpenGl_Mat4 aOrientationMatrix = myCamera->OrientationMatrixF();
2351 OpenGl_Mat4 aViewMappingMatrix = theGlContext->ProjectionState.Current();
e276548b 2352
38a0206f 2353 OpenGl_Mat4 aInverOrientMatrix;
91c60b57 2354 aOrientationMatrix.Inverted (aInverOrientMatrix);
91c60b57 2355 if (!updateRaytraceLightSources (aInverOrientMatrix, theGlContext))
2356 {
e276548b 2357 return Standard_False;
91c60b57 2358 }
e276548b 2359
fc73a202 2360 OpenGl_Vec3 aOrigins[4];
2361 OpenGl_Vec3 aDirects[4];
25ef750e 2362 OpenGl_Mat4 anUnviewMat;
e276548b 2363
91c60b57 2364 updateCamera (aOrientationMatrix,
fc73a202 2365 aViewMappingMatrix,
2366 aOrigins,
a89742cf 2367 aDirects,
25ef750e 2368 anUnviewMat);
e276548b 2369
91c60b57 2370 glEnable (GL_BLEND);
e276548b 2371 glDisable (GL_DEPTH_TEST);
91c60b57 2372 glBlendFunc (GL_ONE, GL_SRC_ALPHA);
e276548b 2373
38a0206f 2374 if (theReadDrawFbo != NULL)
a89742cf 2375 {
38a0206f 2376 theReadDrawFbo->BindBuffer (theGlContext);
a89742cf 2377 }
2378
c827ea3a 2379 // Generate ray-traced image
e276548b 2380 if (myIsRaytraceDataValid)
2381 {
91c60b57 2382 myRaytraceScreenQuad.Bind (theGlContext);
fc73a202 2383
91c60b57 2384 if (!myRaytraceGeometry.AcquireTextures (theGlContext))
25ef750e 2385 {
91c60b57 2386 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB, GL_DEBUG_TYPE_ERROR_ARB,
2387 0, GL_DEBUG_SEVERITY_MEDIUM_ARB, "Error: Failed to acquire OpenGL image textures");
25ef750e 2388 }
2389
91c60b57 2390 Standard_Boolean aResult = runRaytraceShaders (theCView,
2391 theSizeX,
2392 theSizeY,
2393 aOrigins,
2394 aDirects,
2395 anUnviewMat,
38a0206f 2396 theReadDrawFbo,
91c60b57 2397 theGlContext);
fc73a202 2398
91c60b57 2399 if (!aResult)
25ef750e 2400 {
91c60b57 2401 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB, GL_DEBUG_TYPE_ERROR_ARB,
2402 0, GL_DEBUG_SEVERITY_MEDIUM_ARB, "Error: Failed to execute ray-tracing shaders");
2403 }
25ef750e 2404
91c60b57 2405 if (!myRaytraceGeometry.ReleaseTextures (theGlContext))
2406 {
2407 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB, GL_DEBUG_TYPE_ERROR_ARB,
2408 0, GL_DEBUG_SEVERITY_MEDIUM_ARB, "Error: Failed to release OpenGL image textures");
25ef750e 2409 }
2410
91c60b57 2411 myRaytraceScreenQuad.Unbind (theGlContext);
e276548b 2412 }
2413
91c60b57 2414 glDisable (GL_BLEND);
2415 glEnable (GL_DEPTH_TEST);
a89742cf 2416
e276548b 2417 return Standard_True;
91c60b57 2418}