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