1 // Created on: 2013-08-27
2 // Created by: Denis BOGOLEPOV
3 // Copyright (c) 2013 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and / or modify it
8 // under the terms of the GNU Lesser General Public version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
22 #include <OpenGl_Cl.hxx>
29 #pragma comment (lib, "DelayImp.lib")
30 #pragma comment (lib, "OpenCL.lib")
32 #elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
33 #include <OpenGL/CGLCurrent.h>
38 #include <OpenGl_Context.hxx>
39 #include <OpenGl_Texture.hxx>
40 #include <OpenGl_View.hxx>
41 #include <OpenGl_Workspace.hxx>
43 using namespace OpenGl_Raytrace;
45 //! Use this macro to output ray-tracing debug info
46 //#define RAY_TRACE_PRINT_INFO
48 #ifdef RAY_TRACE_PRINT_INFO
49 #include <OSD_Timer.hxx>
52 //! OpenCL source of ray-tracing kernels.
53 extern const char THE_RAY_TRACE_OPENCL_SOURCE[];
55 // =======================================================================
56 // function : MatVecMult
57 // purpose : Multiples 4x4 matrix by 4D vector
58 // =======================================================================
59 template< typename T >
60 OpenGl_RTVec4f MatVecMult (const T m[16], const OpenGl_RTVec4f& v)
62 return OpenGl_RTVec4f (
63 static_cast<float> (m[ 0] * v.x() + m[ 4] * v.y() +
64 m[ 8] * v.z() + m[12] * v.w()),
65 static_cast<float> (m[ 1] * v.x() + m[ 5] * v.y() +
66 m[ 9] * v.z() + m[13] * v.w()),
67 static_cast<float> (m[ 2] * v.x() + m[ 6] * v.y() +
68 m[10] * v.z() + m[14] * v.w()),
69 static_cast<float> (m[ 3] * v.x() + m[ 7] * v.y() +
70 m[11] * v.z() + m[15] * v.w()));
73 // =======================================================================
74 // function : UpdateRaytraceEnvironmentMap
75 // purpose : Updates environment map for ray-tracing
76 // =======================================================================
77 Standard_Boolean OpenGl_Workspace::UpdateRaytraceEnvironmentMap()
80 return Standard_False;
82 if (myViewModificationStatus == myView->ModificationState())
85 cl_int anError = CL_SUCCESS;
87 if (myRaytraceEnvironment != NULL)
88 clReleaseMemObject (myRaytraceEnvironment);
90 Standard_Integer aSizeX = 1;
91 Standard_Integer aSizeY = 1;
93 if (!myView->TextureEnv().IsNull() && myView->SurfaceDetail() != Visual3d_TOD_NONE)
95 aSizeX = (myView->TextureEnv()->SizeX() <= 0) ? 1 : myView->TextureEnv()->SizeX();
96 aSizeY = (myView->TextureEnv()->SizeY() <= 0) ? 1 : myView->TextureEnv()->SizeY();
99 cl_image_format aImageFormat;
101 aImageFormat.image_channel_order = CL_RGBA;
102 aImageFormat.image_channel_data_type = CL_FLOAT;
104 myRaytraceEnvironment = clCreateImage2D (myComputeContext, CL_MEM_READ_ONLY,
105 &aImageFormat, aSizeX, aSizeY, 0,
108 cl_float* aPixelData = new cl_float[aSizeX * aSizeY * 4];
110 // Note: texture format is not compatible with OpenCL image
111 // (it's not possible to create image directly from texture)
113 if (!myView->TextureEnv().IsNull() && myView->SurfaceDetail() != Visual3d_TOD_NONE)
115 myView->TextureEnv()->Bind (GetGlContext());
117 glGetTexImage (GL_TEXTURE_2D,
123 myView->TextureEnv()->Unbind (GetGlContext());
127 for (Standard_Integer aPixel = 0; aPixel < aSizeX * aSizeY * 4; ++aPixel)
128 aPixelData[aPixel] = 0.f;
131 size_t anImageOffset[] = { 0,
135 size_t anImageRegion[] = { aSizeX,
139 anError |= clEnqueueWriteImage (myRaytraceQueue, myRaytraceEnvironment, CL_TRUE,
140 anImageOffset, anImageRegion, 0, 0, aPixelData,
142 #ifdef RAY_TRACE_PRINT_INFO
143 if (anError != CL_SUCCESS)
144 std::cout << "Error! Failed to write environment map image!" << std::endl;
149 myViewModificationStatus = myView->ModificationState();
151 return (anError == CL_SUCCESS);
154 // =======================================================================
155 // function : UpdateRaytraceGeometry
156 // purpose : Updates 3D scene geometry for ray tracing
157 // =======================================================================
158 Standard_Boolean OpenGl_Workspace::UpdateRaytraceGeometry (Standard_Boolean theCheck)
161 return Standard_False;
163 // Note: In 'check' mode the scene geometry is analyzed for modifications
164 // This is light-weight procedure performed for each frame
168 myRaytraceSceneData.Clear();
170 myIsRaytraceDataValid = Standard_False;
174 if (myLayersModificationStatus != myView->LayerList().ModificationState())
176 return UpdateRaytraceGeometry (Standard_False);
180 float* aTransform (NULL);
182 // The set of processed structures (reflected to ray-tracing)
183 // This set is used to remove out-of-date records from the
184 // hash map of structures
185 std::set<const OpenGl_Structure*> anElements;
187 const OpenGl_LayerList& aList = myView->LayerList();
189 for (OpenGl_SequenceOfLayers::Iterator anLayerIt (aList.Layers()); anLayerIt.More(); anLayerIt.Next())
191 const OpenGl_PriorityList& aPriorityList = anLayerIt.Value();
193 if (aPriorityList.NbStructures() == 0)
196 const OpenGl_ArrayOfStructure& aStructArray = aPriorityList.ArrayOfStructures();
198 for (Standard_Integer anIndex = 0; anIndex < aStructArray.Length(); ++anIndex)
200 OpenGl_SequenceOfStructure::Iterator aStructIt;
202 for (aStructIt.Init (aStructArray (anIndex)); aStructIt.More(); aStructIt.Next())
204 const OpenGl_Structure* aStructure = aStructIt.Value();
208 if (CheckRaytraceStructure (aStructure))
210 return UpdateRaytraceGeometry (Standard_False);
215 if (!aStructure->IsRaytracable())
218 if (aStructure->Transformation()->mat != NULL)
220 if (aTransform == NULL)
221 aTransform = new float[16];
223 for (Standard_Integer i = 0; i < 4; ++i)
224 for (Standard_Integer j = 0; j < 4; ++j)
226 aTransform[j * 4 + i] = aStructure->Transformation()->mat[i][j];
230 AddRaytraceStructure (aStructure, aTransform, anElements);
238 // Actualize the hash map of structures -- remove out-of-date records
239 std::map<const OpenGl_Structure*, Standard_Size>::iterator anIter = myStructureStates.begin();
241 while (anIter != myStructureStates.end())
243 if (anElements.find (anIter->first) == anElements.end())
245 myStructureStates.erase (anIter++);
253 // Actualize OpenGL layer list state
254 myLayersModificationStatus = myView->LayerList().ModificationState();
257 #ifdef RAY_TRACE_PRINT_INFO
262 myBVHBuilder.Build (myRaytraceSceneData);
264 #ifdef RAY_TRACE_PRINT_INFO
265 std::cout << " Build time: " << aTimer.ElapsedTime() << " for "
266 << myRaytraceSceneData.Triangles.size() / 1000 << "K triangles" << std::endl;
269 const float aScaleFactor = 1.5f;
271 myRaytraceSceneRadius = aScaleFactor *
272 Max ( Max (fabsf (myRaytraceSceneData.AABB.CornerMin().x()),
273 Max (fabsf (myRaytraceSceneData.AABB.CornerMin().y()),
274 fabsf (myRaytraceSceneData.AABB.CornerMin().z()))),
275 Max (fabsf (myRaytraceSceneData.AABB.CornerMax().x()),
276 Max (fabsf (myRaytraceSceneData.AABB.CornerMax().y()),
277 fabsf (myRaytraceSceneData.AABB.CornerMax().z()))) );
279 myRaytraceSceneEpsilon = Max (1e-4f, myRaytraceSceneRadius * 1e-4f);
281 return WriteRaytraceSceneToDevice();
284 delete [] aTransform;
286 return Standard_True;
289 // =======================================================================
290 // function : CheckRaytraceStructure
291 // purpose : Adds OpenGL structure to ray-traced scene geometry
292 // =======================================================================
293 Standard_Boolean OpenGl_Workspace::CheckRaytraceStructure (const OpenGl_Structure* theStructure)
295 if (!theStructure->IsRaytracable())
297 // Checks to see if all ray-tracable elements were
298 // removed from the structure
299 if (theStructure->ModificationState() > 0)
301 theStructure->ResetModificationState();
302 return Standard_True;
305 return Standard_False;
308 std::map<const OpenGl_Structure*, Standard_Size>::iterator aStructState = myStructureStates.find (theStructure);
310 if (aStructState != myStructureStates.end())
311 return aStructState->second != theStructure->ModificationState();
313 return Standard_True;
316 // =======================================================================
317 // function : CreateMaterial
318 // purpose : Creates ray-tracing material properties
319 // =======================================================================
320 void CreateMaterial (const OPENGL_SURF_PROP& theProp,
321 OpenGl_RaytraceMaterial& theMaterial)
323 const float* aSrcAmb = theProp.isphysic ? theProp.ambcol.rgb : theProp.matcol.rgb;
324 theMaterial.Ambient = OpenGl_RTVec4f (aSrcAmb[0] * theProp.amb,
325 aSrcAmb[1] * theProp.amb,
326 aSrcAmb[2] * theProp.amb,
329 const float* aSrcDif = theProp.isphysic ? theProp.difcol.rgb : theProp.matcol.rgb;
330 theMaterial.Diffuse = OpenGl_RTVec4f (aSrcDif[0] * theProp.diff,
331 aSrcDif[1] * theProp.diff,
332 aSrcDif[2] * theProp.diff,
335 const float aDefSpecCol[4] = {1.0f, 1.0f, 1.0f, 1.0f};
336 const float* aSrcSpe = theProp.isphysic ? theProp.speccol.rgb : aDefSpecCol;
337 theMaterial.Specular = OpenGl_RTVec4f (aSrcSpe[0] * theProp.spec,
338 aSrcSpe[1] * theProp.spec,
339 aSrcSpe[2] * theProp.spec,
342 const float* aSrcEms = theProp.isphysic ? theProp.emscol.rgb : theProp.matcol.rgb;
343 theMaterial.Emission = OpenGl_RTVec4f (aSrcEms[0] * theProp.emsv,
344 aSrcEms[1] * theProp.emsv,
345 aSrcEms[2] * theProp.emsv,
348 // Note: Here we use sub-linear transparency function
349 // to produce realistic-looking transparency effect
350 theMaterial.Transparency = OpenGl_RTVec4f (powf (theProp.trans, 0.75f),
355 const float aMaxRefl = Max (theMaterial.Diffuse.x() + theMaterial.Specular.x(),
356 Max (theMaterial.Diffuse.y() + theMaterial.Specular.y(),
357 theMaterial.Diffuse.z() + theMaterial.Specular.z()));
359 const float aReflectionScale = 0.75f / aMaxRefl;
361 theMaterial.Reflection = OpenGl_RTVec4f (theProp.speccol.rgb[0] * theProp.spec,
362 theProp.speccol.rgb[1] * theProp.spec,
363 theProp.speccol.rgb[2] * theProp.spec,
364 0.f) * aReflectionScale;
367 // =======================================================================
368 // function : AddRaytraceStructure
369 // purpose : Adds OpenGL structure to ray-traced scene geometry
370 // =======================================================================
371 Standard_Boolean OpenGl_Workspace::AddRaytraceStructure (const OpenGl_Structure* theStructure,
372 const float* theTransform,
373 std::set<const OpenGl_Structure*>& theElements)
375 #ifdef RAY_TRACE_PRINT_INFO
376 std::cout << "Add Structure" << std::endl;
379 theElements.insert (theStructure);
381 if (!theStructure->IsVisible())
383 myStructureStates[theStructure] = theStructure->ModificationState();
384 return Standard_True;
387 // Get structure material
388 Standard_Integer aStructMatID = -1;
390 if (theStructure->AspectFace() != NULL)
392 aStructMatID = static_cast<Standard_Integer> (myRaytraceSceneData.Materials.size());
394 OpenGl_RaytraceMaterial aStructMaterial;
395 CreateMaterial (theStructure->AspectFace()->IntFront(), aStructMaterial);
397 myRaytraceSceneData.Materials.push_back (aStructMaterial);
400 OpenGl_ListOfGroup::Iterator anItg (theStructure->Groups());
404 // Get group material
405 Standard_Integer aGroupMatID = -1;
407 if (anItg.Value()->AspectFace() != NULL)
409 aGroupMatID = static_cast<Standard_Integer> (myRaytraceSceneData.Materials.size());
411 OpenGl_RaytraceMaterial aGroupMaterial;
412 CreateMaterial (anItg.Value()->AspectFace()->IntFront(), aGroupMaterial);
414 myRaytraceSceneData.Materials.push_back (aGroupMaterial);
417 Standard_Integer aMatID = aGroupMatID < 0 ? aStructMatID : aGroupMatID;
419 if (aStructMatID < 0 && aGroupMatID < 0)
421 aMatID = static_cast<Standard_Integer> (myRaytraceSceneData.Materials.size());
423 myRaytraceSceneData.Materials.push_back (OpenGl_RaytraceMaterial());
426 // Add OpenGL elements from group (only arrays of primitives)
427 for (const OpenGl_ElementNode* aNode = anItg.Value()->FirstNode(); aNode != NULL; aNode = aNode->next)
429 if (TelNil == aNode->type)
431 OpenGl_AspectFace* anAspect = dynamic_cast<OpenGl_AspectFace*> (aNode->elem);
433 if (anAspect != NULL)
435 aMatID = static_cast<Standard_Integer> (myRaytraceSceneData.Materials.size());
437 OpenGl_RaytraceMaterial aMaterial;
438 CreateMaterial (anAspect->IntFront(), aMaterial);
440 myRaytraceSceneData.Materials.push_back (aMaterial);
443 else if (TelParray == aNode->type)
445 OpenGl_PrimitiveArray* aPrimArray = dynamic_cast<OpenGl_PrimitiveArray*> (aNode->elem);
447 if (aPrimArray != NULL)
449 AddRaytracePrimitiveArray (aPrimArray->PArray(), aMatID, theTransform);
457 float* aTransform (NULL);
459 // Process all connected OpenGL structures
460 OpenGl_ListOfStructure::Iterator anIts (theStructure->ConnectedStructures());
464 if (anIts.Value()->Transformation()->mat != NULL)
466 float* aTransform = new float[16];
468 for (Standard_Integer i = 0; i < 4; ++i)
469 for (Standard_Integer j = 0; j < 4; ++j)
471 aTransform[j * 4 + i] =
472 anIts.Value()->Transformation()->mat[i][j];
476 if (anIts.Value()->IsRaytracable())
477 AddRaytraceStructure (anIts.Value(), aTransform != NULL ? aTransform : theTransform, theElements);
484 myStructureStates[theStructure] = theStructure->ModificationState();
486 return Standard_True;
489 // =======================================================================
490 // function : AddRaytracePrimitiveArray
491 // purpose : Adds OpenGL primitive array to ray-traced scene geometry
492 // =======================================================================
493 Standard_Boolean OpenGl_Workspace::AddRaytracePrimitiveArray (const CALL_DEF_PARRAY* theArray,
494 Standard_Integer theMatID,
495 const float* theTransform)
497 if (theArray->type != TelPolygonsArrayType &&
498 theArray->type != TelTrianglesArrayType &&
499 theArray->type != TelQuadranglesArrayType &&
500 theArray->type != TelTriangleFansArrayType &&
501 theArray->type != TelTriangleStripsArrayType &&
502 theArray->type != TelQuadrangleStripsArrayType)
504 return Standard_True;
507 if (theArray->vertices == NULL)
508 return Standard_False;
510 #ifdef RAY_TRACE_PRINT_INFO
511 switch (theArray->type)
513 case TelPolygonsArrayType:
514 std::cout << "\tTelPolygonsArrayType" << std::endl; break;
515 case TelTrianglesArrayType:
516 std::cout << "\tTelTrianglesArrayType" << std::endl; break;
517 case TelQuadranglesArrayType:
518 std::cout << "\tTelQuadranglesArrayType" << std::endl; break;
519 case TelTriangleFansArrayType:
520 std::cout << "\tTelTriangleFansArrayType" << std::endl; break;
521 case TelTriangleStripsArrayType:
522 std::cout << "\tTelTriangleStripsArrayType" << std::endl; break;
523 case TelQuadrangleStripsArrayType:
524 std::cout << "\tTelQuadrangleStripsArrayType" << std::endl; break;
528 // Simple optimization to eliminate possible memory allocations
529 // during processing of the primitive array vertices
530 myRaytraceSceneData.Vertices.reserve (
531 myRaytraceSceneData.Vertices.size() + theArray->num_vertexs);
533 const Standard_Integer aFirstVert = static_cast<Standard_Integer> (myRaytraceSceneData.Vertices.size());
535 for (Standard_Integer aVert = 0; aVert < theArray->num_vertexs; ++aVert)
537 OpenGl_RTVec4f aVertex (theArray->vertices[aVert].xyz[0],
538 theArray->vertices[aVert].xyz[1],
539 theArray->vertices[aVert].xyz[2],
543 aVertex = MatVecMult (theTransform, aVertex);
545 myRaytraceSceneData.Vertices.push_back (aVertex);
547 myRaytraceSceneData.AABB.Add (aVertex);
550 myRaytraceSceneData.Normals.reserve (
551 myRaytraceSceneData.Normals.size() + theArray->num_vertexs);
553 for (Standard_Integer aNorm = 0; aNorm < theArray->num_vertexs; ++aNorm)
555 OpenGl_RTVec4f aNormal;
557 // Note: In case of absence of normals, the visualizer
558 // will use generated geometric normals
560 if (theArray->vnormals != NULL)
562 aNormal = OpenGl_RTVec4f (theArray->vnormals[aNorm].xyz[0],
563 theArray->vnormals[aNorm].xyz[1],
564 theArray->vnormals[aNorm].xyz[2],
568 aNormal = MatVecMult (theTransform, aNormal);
571 myRaytraceSceneData.Normals.push_back (aNormal);
574 if (theArray->num_bounds > 0)
576 #ifdef RAY_TRACE_PRINT_INFO
577 std::cout << "\tNumber of bounds = " << theArray->num_bounds << std::endl;
580 Standard_Integer aVertOffset = 0;
582 for (Standard_Integer aBound = 0; aBound < theArray->num_bounds; ++aBound)
584 const Standard_Integer aVertNum = theArray->bounds[aBound];
586 #ifdef RAY_TRACE_PRINT_INFO
587 std::cout << "\tAdd indices from bound " << aBound << ": " <<
588 aVertOffset << ", " << aVertNum << std::endl;
591 if (!AddRaytraceVertexIndices (theArray, aFirstVert, aVertOffset, aVertNum, theMatID))
593 return Standard_False;
596 aVertOffset += aVertNum;
601 const Standard_Integer aVertNum = theArray->num_edges > 0 ? theArray->num_edges : theArray->num_vertexs;
603 #ifdef RAY_TRACE_PRINT_INFO
604 std::cout << "\tAdd indices: " << aVertNum << std::endl;
607 return AddRaytraceVertexIndices (theArray, aFirstVert, 0, aVertNum, theMatID);
610 return Standard_True;
613 // =======================================================================
614 // function : AddRaytraceVertexIndices
615 // purpose : Adds vertex indices to ray-traced scene geometry
616 // =======================================================================
617 Standard_Boolean OpenGl_Workspace::AddRaytraceVertexIndices (const CALL_DEF_PARRAY* theArray,
618 Standard_Integer theFirstVert,
619 Standard_Integer theVertOffset,
620 Standard_Integer theVertNum,
621 Standard_Integer theMatID)
623 myRaytraceSceneData.Triangles.reserve (myRaytraceSceneData.Triangles.size() + theVertNum);
624 switch (theArray->type)
626 case TelTrianglesArrayType: return AddRaytraceTriangleArray (theArray, theFirstVert, theVertOffset, theVertNum, theMatID);
627 case TelQuadranglesArrayType: return AddRaytraceQuadrangleArray (theArray, theFirstVert, theVertOffset, theVertNum, theMatID);
628 case TelTriangleFansArrayType: return AddRaytraceTriangleFanArray (theArray, theFirstVert, theVertOffset, theVertNum, theMatID);
629 case TelTriangleStripsArrayType: return AddRaytraceTriangleStripArray (theArray, theFirstVert, theVertOffset, theVertNum, theMatID);
630 case TelQuadrangleStripsArrayType: return AddRaytraceQuadrangleStripArray (theArray, theFirstVert, theVertOffset, theVertNum, theMatID);
631 case TelPolygonsArrayType: return AddRaytracePolygonArray (theArray, theFirstVert, theVertOffset, theVertNum, theMatID);
632 default: return Standard_False;
636 // =======================================================================
637 // function : AddRaytraceTriangleArray
638 // purpose : Adds OpenGL triangle array to ray-traced scene geometry
639 // =======================================================================
640 Standard_Boolean OpenGl_Workspace::AddRaytraceTriangleArray (const CALL_DEF_PARRAY* theArray,
641 Standard_Integer theFirstVert,
642 Standard_Integer theVertOffset,
643 Standard_Integer theVertNum,
644 Standard_Integer theMatID)
647 return Standard_True;
649 if (theArray->num_edges > 0)
651 for (Standard_Integer aVert = theVertOffset; aVert < theVertOffset + theVertNum - 2; aVert += 3)
653 myRaytraceSceneData.Triangles.push_back (OpenGl_RTVec4i (theFirstVert + theArray->edges[aVert + 0],
654 theFirstVert + theArray->edges[aVert + 1],
655 theFirstVert + theArray->edges[aVert + 2],
661 for (Standard_Integer aVert = theVertOffset; aVert < theVertOffset + theVertNum - 2; aVert += 3)
663 myRaytraceSceneData.Triangles.push_back (OpenGl_RTVec4i (theFirstVert + aVert + 0,
664 theFirstVert + aVert + 1,
665 theFirstVert + aVert + 2,
670 return Standard_True;
673 // =======================================================================
674 // function : AddRaytraceTriangleFanArray
675 // purpose : Adds OpenGL triangle fan array to ray-traced scene geometry
676 // =======================================================================
677 Standard_Boolean OpenGl_Workspace::AddRaytraceTriangleFanArray (const CALL_DEF_PARRAY* theArray,
678 Standard_Integer theFirstVert,
679 Standard_Integer theVertOffset,
680 Standard_Integer theVertNum,
681 Standard_Integer theMatID)
684 return Standard_True;
686 if (theArray->num_edges > 0)
688 for (Standard_Integer aVert = theVertOffset; aVert < theVertOffset + theVertNum - 2; ++aVert)
690 myRaytraceSceneData.Triangles.push_back (OpenGl_RTVec4i (theFirstVert + theArray->edges[theVertOffset],
691 theFirstVert + theArray->edges[aVert + 1],
692 theFirstVert + theArray->edges[aVert + 2],
698 for (Standard_Integer aVert = theVertOffset; aVert < theVertOffset + theVertNum - 2; ++aVert)
700 myRaytraceSceneData.Triangles.push_back (OpenGl_RTVec4i (theFirstVert + theVertOffset,
701 theFirstVert + aVert + 1,
702 theFirstVert + aVert + 2,
707 return Standard_True;
710 // =======================================================================
711 // function : AddRaytraceTriangleStripArray
712 // purpose : Adds OpenGL triangle strip array to ray-traced scene geometry
713 // =======================================================================
714 Standard_Boolean OpenGl_Workspace::AddRaytraceTriangleStripArray (const CALL_DEF_PARRAY* theArray,
715 Standard_Integer theFirstVert,
716 Standard_Integer theVertOffset,
717 Standard_Integer theVertNum,
718 Standard_Integer theMatID)
721 return Standard_True;
723 if (theArray->num_edges > 0)
725 myRaytraceSceneData.Triangles.push_back (OpenGl_RTVec4i (
726 theFirstVert + theArray->edges[theVertOffset + 0],
727 theFirstVert + theArray->edges[theVertOffset + 1],
728 theFirstVert + theArray->edges[theVertOffset + 2],
731 for (Standard_Integer aVert = theVertOffset + 1, aTriNum = 1; aVert < theVertOffset + theVertNum - 2; ++aVert, ++aTriNum)
733 myRaytraceSceneData.Triangles.push_back (OpenGl_RTVec4i (
734 theFirstVert + theArray->edges[aVert + (aTriNum % 2) ? 1 : 0],
735 theFirstVert + theArray->edges[aVert + (aTriNum % 2) ? 0 : 1],
736 theFirstVert + theArray->edges[aVert + 2],
742 myRaytraceSceneData.Triangles.push_back (OpenGl_RTVec4i (theFirstVert + theVertOffset + 0,
743 theFirstVert + theVertOffset + 1,
744 theFirstVert + theVertOffset + 2,
747 for (Standard_Integer aVert = theVertOffset + 1, aTriNum = 1; aVert < theVertOffset + theVertNum - 2; ++aVert, ++aTriNum)
749 myRaytraceSceneData.Triangles.push_back (OpenGl_RTVec4i (theFirstVert + aVert + ( aTriNum % 2 ) ? 1 : 0,
750 theFirstVert + aVert + ( aTriNum % 2 ) ? 0 : 1,
751 theFirstVert + aVert + 2,
756 return Standard_True;
759 // =======================================================================
760 // function : AddRaytraceQuadrangleArray
761 // purpose : Adds OpenGL quad array to ray-traced scene geometry
762 // =======================================================================
763 Standard_Boolean OpenGl_Workspace::AddRaytraceQuadrangleArray (const CALL_DEF_PARRAY* theArray,
764 Standard_Integer theFirstVert,
765 Standard_Integer theVertOffset,
766 Standard_Integer theVertNum,
767 Standard_Integer theMatID)
770 return Standard_True;
772 if (theArray->num_edges > 0)
774 for (Standard_Integer aVert = theVertOffset; aVert < theVertOffset + theVertNum - 3; aVert += 4)
776 myRaytraceSceneData.Triangles.push_back (OpenGl_RTVec4i (theFirstVert + theArray->edges[aVert + 0],
777 theFirstVert + theArray->edges[aVert + 1],
778 theFirstVert + theArray->edges[aVert + 2],
781 myRaytraceSceneData.Triangles.push_back (OpenGl_RTVec4i (theFirstVert + theArray->edges[aVert + 0],
782 theFirstVert + theArray->edges[aVert + 2],
783 theFirstVert + theArray->edges[aVert + 3],
789 for (Standard_Integer aVert = theVertOffset; aVert < theVertOffset + theVertNum - 3; aVert += 4)
791 myRaytraceSceneData.Triangles.push_back (OpenGl_RTVec4i (theFirstVert + aVert + 0,
792 theFirstVert + aVert + 1,
793 theFirstVert + aVert + 2,
796 myRaytraceSceneData.Triangles.push_back (OpenGl_RTVec4i (theFirstVert + aVert + 0,
797 theFirstVert + aVert + 2,
798 theFirstVert + aVert + 3,
803 return Standard_True;
806 // =======================================================================
807 // function : AddRaytraceQuadrangleStripArray
808 // purpose : Adds OpenGL quad strip array to ray-traced scene geometry
809 // =======================================================================
810 Standard_Boolean OpenGl_Workspace::AddRaytraceQuadrangleStripArray (const CALL_DEF_PARRAY* theArray,
811 Standard_Integer theFirstVert,
812 Standard_Integer theVertOffset,
813 Standard_Integer theVertNum,
814 Standard_Integer theMatID)
817 return Standard_True;
819 if (theArray->num_edges > 0)
821 myRaytraceSceneData.Triangles.push_back (OpenGl_RTVec4i (
822 theFirstVert + theArray->edges[theVertOffset + 0],
823 theFirstVert + theArray->edges[theVertOffset + 1],
824 theFirstVert + theArray->edges[theVertOffset + 2],
827 myRaytraceSceneData.Triangles.push_back (OpenGl_RTVec4i (
828 theFirstVert + theArray->edges[theVertOffset + 1],
829 theFirstVert + theArray->edges[theVertOffset + 3],
830 theFirstVert + theArray->edges[theVertOffset + 2],
833 for (Standard_Integer aVert = theVertOffset + 2; aVert < theVertOffset + theVertNum - 3; aVert += 2)
835 myRaytraceSceneData.Triangles.push_back (OpenGl_RTVec4i (
836 theFirstVert + theArray->edges[aVert + 0],
837 theFirstVert + theArray->edges[aVert + 1],
838 theFirstVert + theArray->edges[aVert + 2],
841 myRaytraceSceneData.Triangles.push_back (OpenGl_RTVec4i (
842 theFirstVert + theArray->edges[aVert + 1],
843 theFirstVert + theArray->edges[aVert + 3],
844 theFirstVert + theArray->edges[aVert + 2],
850 myRaytraceSceneData.Triangles.push_back (OpenGl_RTVec4i (theFirstVert + 0,
855 myRaytraceSceneData.Triangles.push_back (OpenGl_RTVec4i (theFirstVert + 1,
860 for (Standard_Integer aVert = theVertOffset + 2; aVert < theVertOffset + theVertNum - 3; aVert += 2)
862 myRaytraceSceneData.Triangles.push_back (OpenGl_RTVec4i (theFirstVert + aVert + 0,
863 theFirstVert + aVert + 1,
864 theFirstVert + aVert + 2,
867 myRaytraceSceneData.Triangles.push_back (OpenGl_RTVec4i (theFirstVert + aVert + 1,
868 theFirstVert + aVert + 3,
869 theFirstVert + aVert + 2,
874 return Standard_True;
877 // =======================================================================
878 // function : AddRaytracePolygonArray
879 // purpose : Adds OpenGL polygon array to ray-traced scene geometry
880 // =======================================================================
881 Standard_Boolean OpenGl_Workspace::AddRaytracePolygonArray (const CALL_DEF_PARRAY* theArray,
882 Standard_Integer theFirstVert,
883 Standard_Integer theVertOffset,
884 Standard_Integer theVertNum,
885 Standard_Integer theMatID)
887 if (theArray->num_vertexs < 3)
888 return Standard_True;
890 if (theArray->edges != NULL)
892 for (Standard_Integer aVert = theVertOffset; aVert < theVertOffset + theVertNum - 2; ++aVert)
894 myRaytraceSceneData.Triangles.push_back (OpenGl_RTVec4i (theFirstVert + theArray->edges[theVertOffset],
895 theFirstVert + theArray->edges[aVert + 1],
896 theFirstVert + theArray->edges[aVert + 2],
902 for (Standard_Integer aVert = theVertOffset; aVert < theVertOffset + theVertNum - 2; ++aVert)
904 myRaytraceSceneData.Triangles.push_back (OpenGl_RTVec4i (theFirstVert + theVertOffset,
905 theFirstVert + aVert + 1,
906 theFirstVert + aVert + 2,
911 return Standard_True;
914 // =======================================================================
915 // function : UpdateRaytraceLightSources
916 // purpose : Updates 3D scene light sources for ray-tracing
917 // =======================================================================
918 Standard_Boolean OpenGl_Workspace::UpdateRaytraceLightSources (const GLdouble theInvModelView[16])
920 myRaytraceSceneData.LightSources.clear();
922 OpenGl_RTVec4f anAmbient (0.0f, 0.0f, 0.0f, 0.0f);
923 for (OpenGl_ListOfLight::Iterator anItl (myView->LightList());
924 anItl.More(); anItl.Next())
926 const OpenGl_Light& aLight = anItl.Value();
927 if (aLight.Type == Visual3d_TOLS_AMBIENT)
929 anAmbient += OpenGl_RTVec4f (aLight.Color.r(), aLight.Color.g(), aLight.Color.b(), 0.0f);
933 OpenGl_RTVec4f aDiffuse (aLight.Color.r(), aLight.Color.g(), aLight.Color.b(), 1.0f);
934 OpenGl_RTVec4f aPosition (-aLight.Direction.x(), -aLight.Direction.y(), -aLight.Direction.z(), 0.0f);
935 if (aLight.Type != Visual3d_TOLS_DIRECTIONAL)
937 aPosition = OpenGl_RTVec4f (aLight.Position.x(), aLight.Position.y(), aLight.Position.z(), 1.0f);
939 if (aLight.IsHeadlight)
941 aPosition = MatVecMult (theInvModelView, aPosition);
944 myRaytraceSceneData.LightSources.push_back (OpenGl_RaytraceLight (aDiffuse, aPosition));
947 if (myRaytraceSceneData.LightSources.size() > 0)
949 myRaytraceSceneData.LightSources.front().Ambient += anAmbient;
953 myRaytraceSceneData.LightSources.push_back (OpenGl_RaytraceLight (OpenGl_RTVec4f (anAmbient.rgb(), -1.0f)));
956 cl_int anError = CL_SUCCESS;
958 if (myRaytraceLightSourceBuffer != NULL)
959 clReleaseMemObject (myRaytraceLightSourceBuffer);
961 const size_t myLightBufferSize = myRaytraceSceneData.LightSources.size() > 0
962 ? myRaytraceSceneData.LightSources.size()
965 myRaytraceLightSourceBuffer = clCreateBuffer (myComputeContext, CL_MEM_READ_ONLY,
966 myLightBufferSize * sizeof(OpenGl_RaytraceLight),
969 if (myRaytraceSceneData.LightSources.size() > 0)
971 const void* aDataPtr = myRaytraceSceneData.LightSources.front().Packed();
972 anError |= clEnqueueWriteBuffer (myRaytraceQueue, myRaytraceLightSourceBuffer, CL_TRUE, 0,
973 myLightBufferSize * sizeof(OpenGl_RaytraceLight), aDataPtr,
977 #ifdef RAY_TRACE_PRINT_INFO
978 if (anError != CL_SUCCESS)
980 std::cout << "Error! Failed to set light sources!";
982 return Standard_False;
986 return Standard_True;
989 // =======================================================================
990 // function : CheckOpenCL
991 // purpose : Checks OpenCL dynamic library availability
992 // =======================================================================
993 Standard_Boolean CheckOpenCL()
995 #if defined ( _WIN32 )
999 cl_uint aNbPlatforms;
1000 clGetPlatformIDs (0, NULL, &aNbPlatforms);
1002 __except (EXCEPTION_EXECUTE_HANDLER)
1004 return Standard_False;
1009 return Standard_True;
1012 // =======================================================================
1013 // function : InitOpenCL
1014 // purpose : Initializes OpenCL objects
1015 // =======================================================================
1016 Standard_Boolean OpenGl_Workspace::InitOpenCL()
1018 if (myComputeInitStatus != OpenGl_CLIS_NONE)
1020 return myComputeInitStatus == OpenGl_CLIS_INIT;
1025 myComputeInitStatus = OpenGl_CLIS_FAIL; // fail to load OpenCL library
1026 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
1027 GL_DEBUG_TYPE_ERROR_ARB,
1029 GL_DEBUG_SEVERITY_HIGH_ARB,
1030 "Failed to load OpenCL dynamic library!");
1031 return Standard_False;
1034 // Obtain the list of platforms available
1035 cl_uint aNbPlatforms = 0;
1036 cl_int anError = clGetPlatformIDs (0, NULL, &aNbPlatforms);
1037 cl_platform_id* aPlatforms = (cl_platform_id* )alloca (aNbPlatforms * sizeof(cl_platform_id));
1038 anError |= clGetPlatformIDs (aNbPlatforms, aPlatforms, NULL);
1039 if (anError != CL_SUCCESS
1040 || aNbPlatforms == 0)
1042 myComputeInitStatus = OpenGl_CLIS_FAIL;
1043 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
1044 GL_DEBUG_TYPE_ERROR_ARB,
1046 GL_DEBUG_SEVERITY_HIGH_ARB,
1047 "No any OpenCL platform installed!");
1048 return Standard_False;
1051 // Note: We try to find NVIDIA or AMD platforms with GPU devices!
1052 cl_platform_id aPrefPlatform = NULL;
1053 for (cl_uint aPlatIter = 0; aPlatIter < aNbPlatforms; ++aPlatIter)
1056 anError = clGetPlatformInfo (aPlatforms[aPlatIter], CL_PLATFORM_NAME,
1057 sizeof(aName), aName, NULL);
1058 if (anError != CL_SUCCESS)
1063 if (strncmp (aName, "NVIDIA", strlen ("NVIDIA")) == 0)
1065 aPrefPlatform = aPlatforms[aPlatIter];
1067 // Use optimizations for NVIDIA GPUs
1068 myIsAmdComputePlatform = Standard_False;
1070 else if (strncmp (aName, "AMD", strlen ("AMD")) == 0)
1072 aPrefPlatform = (aPrefPlatform == NULL)
1073 ? aPlatforms[aPlatIter]
1076 // Use optimizations for ATI/AMD platform
1077 myIsAmdComputePlatform = Standard_True;
1081 if (aPrefPlatform == NULL)
1083 aPrefPlatform = aPlatforms[0];
1086 // Obtain the list of devices available in the selected platform
1087 cl_uint aNbDevices = 0;
1088 anError = clGetDeviceIDs (aPrefPlatform, CL_DEVICE_TYPE_GPU,
1089 0, NULL, &aNbDevices);
1091 cl_device_id* aDevices = (cl_device_id* )alloca (aNbDevices * sizeof(cl_device_id));
1092 anError |= clGetDeviceIDs (aPrefPlatform, CL_DEVICE_TYPE_GPU,
1093 aNbDevices, aDevices, NULL);
1094 if (anError != CL_SUCCESS)
1096 myComputeInitStatus = OpenGl_CLIS_FAIL;
1097 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
1098 GL_DEBUG_TYPE_ERROR_ARB,
1100 GL_DEBUG_SEVERITY_HIGH_ARB,
1101 "Failed to get OpenCL GPU device!");
1102 return Standard_False;
1105 // Note: Simply get first available GPU
1106 cl_device_id aDevice = aDevices[0];
1108 // detect old contexts
1109 char aVerClStr[256];
1110 clGetDeviceInfo (aDevice, CL_DEVICE_VERSION,
1111 sizeof(aVerClStr), aVerClStr, NULL);
1112 aVerClStr[strlen ("OpenCL 1.0")] = '\0';
1113 const bool isVer10 = strncmp (aVerClStr, "OpenCL 1.0", strlen ("OpenCL 1.0")) == 0;
1115 // Create OpenCL context
1116 cl_context_properties aCtxProp[] =
1118 #if defined(__APPLE__) && !defined(MACOSX_USE_GLX)
1119 CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE,
1120 (cl_context_properties )CGLGetShareGroup (CGLGetCurrentContext()),
1121 #elif defined(_WIN32)
1122 CL_CONTEXT_PLATFORM, (cl_context_properties )aPrefPlatform,
1123 CL_GL_CONTEXT_KHR, (cl_context_properties )wglGetCurrentContext(),
1124 CL_WGL_HDC_KHR, (cl_context_properties )wglGetCurrentDC(),
1126 CL_GL_CONTEXT_KHR, (cl_context_properties )glXGetCurrentContext(),
1127 CL_GLX_DISPLAY_KHR, (cl_context_properties )glXGetCurrentDisplay(),
1128 CL_CONTEXT_PLATFORM, (cl_context_properties )aPrefPlatform,
1133 myComputeContext = clCreateContext (aCtxProp,
1134 #if defined(__APPLE__) && !defined(MACOSX_USE_GLX)
1135 0, NULL, // device will be taken from GL context
1139 NULL, NULL, &anError);
1140 if (anError != CL_SUCCESS)
1142 myComputeInitStatus = OpenGl_CLIS_FAIL;
1143 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
1144 GL_DEBUG_TYPE_ERROR_ARB,
1146 GL_DEBUG_SEVERITY_HIGH_ARB,
1147 "Failed to initialize OpenCL context!");
1148 return Standard_False;
1151 // Create OpenCL program
1152 const char* aSources[] =
1154 isVer10 ? "#define M_PI_F ( float )( 3.14159265359f )\n" : "",
1155 THE_RAY_TRACE_OPENCL_SOURCE
1157 myRaytraceProgram = clCreateProgramWithSource (myComputeContext, 2,
1158 aSources, NULL, &anError);
1159 if (anError != CL_SUCCESS)
1161 myComputeInitStatus = OpenGl_CLIS_FAIL;
1162 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
1163 GL_DEBUG_TYPE_ERROR_ARB,
1165 GL_DEBUG_SEVERITY_HIGH_ARB,
1166 "Failed to create OpenCL ray-tracing program!");
1167 return Standard_False;
1170 anError = clBuildProgram (myRaytraceProgram, 0,
1171 NULL, NULL, NULL, NULL);
1175 cl_int aResult = clGetProgramBuildInfo (myRaytraceProgram, aDevice,
1176 CL_PROGRAM_BUILD_LOG, 0, NULL, &aLogLen);
1178 char* aBuildLog = (char* )alloca (aLogLen);
1179 aResult |= clGetProgramBuildInfo (myRaytraceProgram, aDevice,
1180 CL_PROGRAM_BUILD_LOG, aLogLen, aBuildLog, NULL);
1181 if (aResult == CL_SUCCESS)
1183 if (anError != CL_SUCCESS)
1185 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
1186 GL_DEBUG_TYPE_ERROR_ARB,
1188 GL_DEBUG_SEVERITY_HIGH_ARB,
1193 #ifdef RAY_TRACE_PRINT_INFO
1194 std::cout << aBuildLog << std::endl;
1200 if (anError != CL_SUCCESS)
1202 return Standard_False;
1205 // Create OpenCL ray tracing kernels
1206 myRaytraceRenderKernel = clCreateKernel (myRaytraceProgram, "Main", &anError);
1207 if (anError != CL_SUCCESS)
1209 myComputeInitStatus = OpenGl_CLIS_FAIL;
1210 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
1211 GL_DEBUG_TYPE_ERROR_ARB,
1213 GL_DEBUG_SEVERITY_HIGH_ARB,
1214 "Failed to create OpenCL ray-tracing kernel!");
1215 return Standard_False;
1218 myRaytraceSmoothKernel = clCreateKernel (myRaytraceProgram, "MainAntialiased", &anError);
1219 if (anError != CL_SUCCESS)
1221 myComputeInitStatus = OpenGl_CLIS_FAIL;
1222 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
1223 GL_DEBUG_TYPE_ERROR_ARB,
1225 GL_DEBUG_SEVERITY_HIGH_ARB,
1226 "Failed to create OpenCL ray-tracing kernel!");
1227 return Standard_False;
1230 // Create OpenCL command queue
1231 // Note: For profiling set CL_QUEUE_PROFILING_ENABLE
1232 cl_command_queue_properties aProps = CL_QUEUE_PROFILING_ENABLE;
1234 myRaytraceQueue = clCreateCommandQueue (myComputeContext, aDevice, aProps, &anError);
1235 if (anError != CL_SUCCESS)
1237 myComputeInitStatus = OpenGl_CLIS_FAIL;
1238 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
1239 GL_DEBUG_TYPE_ERROR_ARB,
1241 GL_DEBUG_SEVERITY_HIGH_ARB,
1242 "Failed to create OpenCL command queue!");
1244 return Standard_False;
1247 myComputeInitStatus = OpenGl_CLIS_INIT; // initialized in normal way
1248 return Standard_True;
1251 // =======================================================================
1252 // function : GetOpenClDeviceInfo
1253 // purpose : Returns information about device used for computations
1254 // =======================================================================
1255 Standard_Boolean OpenGl_Workspace::GetOpenClDeviceInfo (NCollection_DataMap<TCollection_AsciiString,
1256 TCollection_AsciiString>& theInfo) const
1259 if (myComputeContext == NULL)
1261 return Standard_False;
1264 size_t aDevicesSize = 0;
1265 cl_int anError = clGetContextInfo (myComputeContext, CL_CONTEXT_DEVICES, 0, NULL, &aDevicesSize);
1266 cl_device_id* aDevices = (cl_device_id* )alloca (aDevicesSize);
1267 anError |= clGetContextInfo (myComputeContext, CL_CONTEXT_DEVICES, aDevicesSize, aDevices, NULL);
1268 if (anError != CL_SUCCESS)
1270 return Standard_False;
1273 char aDeviceName[256];
1274 anError |= clGetDeviceInfo (aDevices[0], CL_DEVICE_NAME, sizeof(aDeviceName), aDeviceName, NULL);
1275 theInfo.Bind ("Name", aDeviceName);
1277 char aDeviceVendor[256];
1278 anError |= clGetDeviceInfo (aDevices[0], CL_DEVICE_VENDOR, sizeof(aDeviceVendor), aDeviceVendor, NULL);
1279 theInfo.Bind ("Vendor", aDeviceVendor);
1281 cl_device_type aDeviceType;
1282 anError |= clGetDeviceInfo (aDevices[0], CL_DEVICE_TYPE, sizeof(aDeviceType), &aDeviceType, NULL);
1283 theInfo.Bind ("Type", aDeviceType == CL_DEVICE_TYPE_GPU ? "GPU" : "CPU");
1284 return Standard_True;
1287 // =======================================================================
1288 // function : ReleaseOpenCL
1289 // purpose : Releases resources of OpenCL objects
1290 // =======================================================================
1291 void OpenGl_Workspace::ReleaseOpenCL()
1293 clReleaseKernel (myRaytraceRenderKernel);
1294 clReleaseKernel (myRaytraceSmoothKernel);
1296 clReleaseProgram (myRaytraceProgram);
1297 clReleaseCommandQueue (myRaytraceQueue);
1299 clReleaseMemObject (myRaytraceOutputImage);
1300 clReleaseMemObject (myRaytraceEnvironment);
1301 clReleaseMemObject (myRaytraceOutputImageAA);
1303 clReleaseMemObject (myRaytraceVertexBuffer);
1304 clReleaseMemObject (myRaytraceNormalBuffer);
1305 clReleaseMemObject (myRaytraceTriangleBuffer);
1307 clReleaseMemObject (myRaytraceMaterialBuffer);
1308 clReleaseMemObject (myRaytraceLightSourceBuffer);
1310 clReleaseMemObject (myRaytraceNodeMinPointBuffer);
1311 clReleaseMemObject (myRaytraceNodeMaxPointBuffer);
1312 clReleaseMemObject (myRaytraceNodeDataRcrdBuffer);
1314 clReleaseContext (myComputeContext);
1316 if (!myGlContext.IsNull())
1318 if (!myRaytraceOutputTexture.IsNull())
1319 myGlContext->DelayedRelease (myRaytraceOutputTexture);
1320 myRaytraceOutputTexture.Nullify();
1322 if (!myRaytraceOutputTextureAA.IsNull())
1323 myGlContext->DelayedRelease (myRaytraceOutputTextureAA);
1324 myRaytraceOutputTextureAA.Nullify();
1328 // =======================================================================
1329 // function : ResizeRaytraceOutputBuffer
1330 // purpose : Resizes OpenCL output image
1331 // =======================================================================
1332 Standard_Boolean OpenGl_Workspace::ResizeRaytraceOutputBuffer (const cl_int theSizeX,
1333 const cl_int theSizeY)
1335 if (myComputeContext == NULL)
1337 return Standard_False;
1340 if (!myRaytraceOutputTexture.IsNull())
1342 Standard_Boolean toResize = myRaytraceOutputTexture->SizeX() != theSizeX ||
1343 myRaytraceOutputTexture->SizeY() != theSizeY;
1346 return Standard_True;
1348 if (!myGlContext.IsNull())
1350 if (!myRaytraceOutputTexture.IsNull())
1351 myGlContext->DelayedRelease (myRaytraceOutputTexture);
1352 if (!myRaytraceOutputTextureAA.IsNull())
1353 myGlContext->DelayedRelease (myRaytraceOutputTextureAA);
1357 myRaytraceOutputTexture = new OpenGl_Texture();
1359 myRaytraceOutputTexture->Create (myGlContext);
1360 myRaytraceOutputTexture->InitRectangle (myGlContext,
1361 theSizeX, theSizeY, OpenGl_TextureFormat::Create<GLfloat, 4>());
1363 myRaytraceOutputTextureAA = new OpenGl_Texture();
1365 myRaytraceOutputTextureAA->Create (myGlContext);
1366 myRaytraceOutputTextureAA->InitRectangle (myGlContext,
1367 theSizeX, theSizeY, OpenGl_TextureFormat::Create<GLfloat, 4>());
1369 if (myRaytraceOutputImage != NULL)
1370 clReleaseMemObject (myRaytraceOutputImage);
1372 if (myRaytraceOutputImageAA != NULL)
1373 clReleaseMemObject (myRaytraceOutputImageAA);
1375 cl_int anError = CL_SUCCESS;
1377 myRaytraceOutputImage = clCreateFromGLTexture2D (myComputeContext,
1378 CL_MEM_READ_WRITE, GL_TEXTURE_RECTANGLE, 0, myRaytraceOutputTexture->TextureId(), &anError);
1380 if (anError != CL_SUCCESS)
1382 #ifdef RAY_TRACE_PRINT_INFO
1383 std::cout << "Error! Failed to create output image!" << std::endl;
1385 return Standard_False;
1388 myRaytraceOutputImageAA = clCreateFromGLTexture2D (myComputeContext,
1389 CL_MEM_READ_WRITE, GL_TEXTURE_RECTANGLE, 0, myRaytraceOutputTextureAA->TextureId(), &anError);
1391 if (anError != CL_SUCCESS)
1393 #ifdef RAY_TRACE_PRINT_INFO
1394 std::cout << "Error! Failed to create anti-aliased output image!" << std::endl;
1396 return Standard_False;
1399 return Standard_True;
1402 // =======================================================================
1403 // function : WriteRaytraceSceneToDevice
1404 // purpose : Writes scene geometry to OpenCl device
1405 // =======================================================================
1406 Standard_Boolean OpenGl_Workspace::WriteRaytraceSceneToDevice()
1408 if (myComputeContext == NULL)
1409 return Standard_False;
1411 cl_int anError = CL_SUCCESS;
1413 if (myRaytraceNormalBuffer != NULL)
1414 anError |= clReleaseMemObject (myRaytraceNormalBuffer);
1416 if (myRaytraceVertexBuffer != NULL)
1417 anError |= clReleaseMemObject (myRaytraceVertexBuffer);
1419 if (myRaytraceTriangleBuffer != NULL)
1420 anError |= clReleaseMemObject (myRaytraceTriangleBuffer);
1422 if (myRaytraceNodeMinPointBuffer != NULL)
1423 anError |= clReleaseMemObject (myRaytraceNodeMinPointBuffer);
1425 if (myRaytraceNodeMaxPointBuffer != NULL)
1426 anError |= clReleaseMemObject (myRaytraceNodeMaxPointBuffer);
1428 if (myRaytraceNodeDataRcrdBuffer != NULL)
1429 anError |= clReleaseMemObject (myRaytraceNodeDataRcrdBuffer);
1431 if (myRaytraceMaterialBuffer != NULL)
1432 anError |= clReleaseMemObject (myRaytraceMaterialBuffer);
1434 if (anError != CL_SUCCESS)
1436 #ifdef RAY_TRACE_PRINT_INFO
1437 std::cout << "Error! Failed to release OpenCL scene buffers!" << std::endl;
1439 return Standard_False;
1442 // Create geometry buffers
1443 cl_int anErrorTemp = CL_SUCCESS;
1444 const size_t myVertexBufferSize = myRaytraceSceneData.Vertices.size() > 0
1445 ? myRaytraceSceneData.Vertices.size() : 1;
1447 myRaytraceVertexBuffer = clCreateBuffer (myComputeContext, CL_MEM_READ_ONLY,
1448 myVertexBufferSize * sizeof(cl_float4), NULL, &anErrorTemp);
1449 anError |= anErrorTemp;
1451 const size_t myNormalBufferSize = myRaytraceSceneData.Normals.size() > 0
1452 ? myRaytraceSceneData.Normals.size() : 1;
1453 myRaytraceNormalBuffer = clCreateBuffer (myComputeContext, CL_MEM_READ_ONLY,
1454 myNormalBufferSize * sizeof(cl_float4), NULL, &anErrorTemp);
1455 anError |= anErrorTemp;
1457 const size_t myTriangleBufferSize = myRaytraceSceneData.Triangles.size() > 0
1458 ? myRaytraceSceneData.Triangles.size() : 1;
1459 myRaytraceTriangleBuffer = clCreateBuffer (myComputeContext, CL_MEM_READ_ONLY,
1460 myTriangleBufferSize * sizeof(cl_int4), NULL, &anErrorTemp);
1461 anError |= anErrorTemp;
1462 if (anError != CL_SUCCESS)
1464 #ifdef RAY_TRACE_PRINT_INFO
1465 std::cout << "Error! Failed to create OpenCL geometry buffers!" << std::endl;
1467 return Standard_False;
1470 // Create material buffer
1471 const size_t myMaterialBufferSize = myRaytraceSceneData.Materials.size() > 0
1472 ? myRaytraceSceneData.Materials.size() : 1;
1473 myRaytraceMaterialBuffer = clCreateBuffer (myComputeContext, CL_MEM_READ_ONLY,
1474 myMaterialBufferSize * sizeof(OpenGl_RaytraceMaterial), NULL,
1476 if (anErrorTemp != CL_SUCCESS)
1478 #ifdef RAY_TRACE_PRINT_INFO
1479 std::cout << "Error! Failed to create OpenCL material buffer!" << std::endl;
1481 return Standard_False;
1484 // Create BVH buffers
1485 OpenGl_BVH aTree = myBVHBuilder.Tree();
1486 const size_t myNodeMinPointBufferSize = aTree.MinPointBuffer().size() > 0
1487 ? aTree.MinPointBuffer().size() : 1;
1488 myRaytraceNodeMinPointBuffer = clCreateBuffer (myComputeContext, CL_MEM_READ_ONLY,
1489 myNodeMinPointBufferSize * sizeof(cl_float4), NULL,
1491 anError |= anErrorTemp;
1493 const size_t myNodeMaxPointBufferSize = aTree.MaxPointBuffer().size() > 0
1494 ? aTree.MaxPointBuffer().size() : 1;
1495 myRaytraceNodeMaxPointBuffer = clCreateBuffer (myComputeContext, CL_MEM_READ_ONLY,
1496 myNodeMaxPointBufferSize * sizeof(cl_float4), NULL,
1498 anError |= anErrorTemp;
1500 const size_t myNodeDataRecordBufferSize = aTree.DataRcrdBuffer().size() > 0
1501 ? aTree.DataRcrdBuffer().size() : 1;
1502 myRaytraceNodeDataRcrdBuffer = clCreateBuffer (myComputeContext, CL_MEM_READ_ONLY,
1503 myNodeDataRecordBufferSize * sizeof(cl_int4), NULL,
1505 anError |= anErrorTemp;
1506 if (anError != CL_SUCCESS)
1508 #ifdef RAY_TRACE_PRINT_INFO
1509 std::cout << "Error! Failed to create OpenCL BVH buffers!" << std::endl;
1511 return Standard_False;
1514 // Write scene geometry buffers
1515 if (myRaytraceSceneData.Triangles.size() > 0)
1517 anError |= clEnqueueWriteBuffer (myRaytraceQueue, myRaytraceVertexBuffer, CL_FALSE,
1518 0, myRaytraceSceneData.Vertices.size() * sizeof(cl_float4),
1519 &myRaytraceSceneData.Vertices.front(),
1521 anError |= clEnqueueWriteBuffer (myRaytraceQueue, myRaytraceNormalBuffer, CL_FALSE,
1522 0, myRaytraceSceneData.Normals.size() * sizeof(cl_float4),
1523 &myRaytraceSceneData.Normals.front(),
1525 anError |= clEnqueueWriteBuffer (myRaytraceQueue, myRaytraceTriangleBuffer, CL_FALSE,
1526 0, myRaytraceSceneData.Triangles.size() * sizeof(cl_int4),
1527 &myRaytraceSceneData.Triangles.front(),
1529 if (anError != CL_SUCCESS)
1531 #ifdef RAY_TRACE_PRINT_INFO
1532 std::cout << "Error! Failed to write OpenCL geometry buffers!" << std::endl;
1534 return Standard_False;
1538 // Write BVH buffers
1539 if (aTree.DataRcrdBuffer().size() > 0)
1541 anError |= clEnqueueWriteBuffer (myRaytraceQueue, myRaytraceNodeMinPointBuffer, CL_FALSE,
1542 0, aTree.MinPointBuffer().size() * sizeof(cl_float4),
1543 &aTree.MinPointBuffer().front(),
1545 anError |= clEnqueueWriteBuffer (myRaytraceQueue, myRaytraceNodeMaxPointBuffer, CL_FALSE,
1546 0, aTree.MaxPointBuffer().size() * sizeof(cl_float4),
1547 &aTree.MaxPointBuffer().front(),
1549 anError |= clEnqueueWriteBuffer (myRaytraceQueue, myRaytraceNodeDataRcrdBuffer, CL_FALSE,
1550 0, aTree.DataRcrdBuffer().size() * sizeof(cl_int4),
1551 &aTree.DataRcrdBuffer().front(),
1553 if (anError != CL_SUCCESS)
1555 #ifdef RAY_TRACE_PRINT_INFO
1556 std::cout << "Error! Failed to write OpenCL BVH buffers!" << std::endl;
1558 return Standard_False;
1562 // Write material buffers
1563 if (myRaytraceSceneData.Materials.size() > 0)
1565 const size_t aSize = myRaytraceSceneData.Materials.size();
1566 const void* aDataPtr = myRaytraceSceneData.Materials.front().Packed();
1568 anError |= clEnqueueWriteBuffer (myRaytraceQueue, myRaytraceMaterialBuffer, CL_FALSE,
1569 0, aSize * sizeof(OpenGl_RaytraceMaterial), aDataPtr,
1571 if (anError != CL_SUCCESS)
1573 #ifdef RAY_TRACE_PRINT_INFO
1574 std::cout << "Error! Failed to write OpenCL material buffer!" << std::endl;
1576 return Standard_False;
1580 anError |= clFinish (myRaytraceQueue);
1581 #ifdef RAY_TRACE_PRINT_INFO
1582 if (anError != CL_SUCCESS)
1583 std::cout << "Error! Failed to set scene data buffers!" << std::endl;
1586 if (anError == CL_SUCCESS)
1587 myIsRaytraceDataValid = myRaytraceSceneData.Triangles.size() > 0;
1589 #ifdef RAY_TRACE_PRINT_INFO
1591 float aMemUsed = static_cast<float> (
1592 myRaytraceSceneData.Materials.size()) * sizeof (OpenGl_RaytraceMaterial);
1594 aMemUsed += static_cast<float> (
1595 myRaytraceSceneData.Triangles.size() * sizeof (OpenGl_RTVec4i) +
1596 myRaytraceSceneData.Vertices.size() * sizeof (OpenGl_RTVec4f) +
1597 myRaytraceSceneData.Normals.size() * sizeof (OpenGl_RTVec4f));
1599 aMemUsed += static_cast<float> (
1600 aTree.MinPointBuffer().size() * sizeof (OpenGl_RTVec4f) +
1601 aTree.MaxPointBuffer().size() * sizeof (OpenGl_RTVec4f) +
1602 aTree.DataRcrdBuffer().size() * sizeof (OpenGl_RTVec4i));
1604 std::cout << "GPU memory used (Mb): " << aMemUsed / 1e6f << std::endl;
1608 myRaytraceSceneData.Clear();
1610 myBVHBuilder.CleanUp();
1612 return (CL_SUCCESS == anError);
1615 #define OPENCL_GROUP_SIZE_TEST_
1617 // =======================================================================
1618 // function : RunRaytraceOpenCLKernels
1619 // purpose : Runs OpenCL ray-tracing kernels
1620 // =======================================================================
1621 Standard_Boolean OpenGl_Workspace::RunRaytraceOpenCLKernels (const Graphic3d_CView& theCView,
1622 const GLfloat theOrigins[16],
1623 const GLfloat theDirects[16],
1624 const Standard_Integer theSizeX,
1625 const Standard_Integer theSizeY)
1627 if (myRaytraceRenderKernel == NULL || myRaytraceQueue == NULL)
1628 return Standard_False;
1630 ////////////////////////////////////////////////////////////
1631 // Set kernel arguments
1633 cl_uint anIndex = 0;
1636 anError = clSetKernelArg (myRaytraceRenderKernel, anIndex++,
1637 sizeof(cl_mem), &myRaytraceOutputImage);
1638 anError |= clSetKernelArg (myRaytraceRenderKernel, anIndex++,
1639 sizeof(cl_mem), &myRaytraceEnvironment);
1640 anError |= clSetKernelArg (myRaytraceRenderKernel, anIndex++,
1641 sizeof(cl_mem), &myRaytraceNodeMinPointBuffer);
1642 anError |= clSetKernelArg (myRaytraceRenderKernel, anIndex++,
1643 sizeof(cl_mem), &myRaytraceNodeMaxPointBuffer);
1644 anError |= clSetKernelArg (myRaytraceRenderKernel, anIndex++,
1645 sizeof(cl_mem), &myRaytraceNodeDataRcrdBuffer);
1646 anError |= clSetKernelArg (myRaytraceRenderKernel, anIndex++,
1647 sizeof(cl_mem), &myRaytraceLightSourceBuffer);
1648 anError |= clSetKernelArg (myRaytraceRenderKernel, anIndex++,
1649 sizeof(cl_mem), &myRaytraceMaterialBuffer);
1650 anError |= clSetKernelArg (myRaytraceRenderKernel, anIndex++,
1651 sizeof(cl_mem), &myRaytraceVertexBuffer);
1652 anError |= clSetKernelArg (myRaytraceRenderKernel, anIndex++,
1653 sizeof(cl_mem), &myRaytraceNormalBuffer);
1654 anError |= clSetKernelArg (myRaytraceRenderKernel, anIndex++,
1655 sizeof(cl_mem), &myRaytraceTriangleBuffer);
1657 anError |= clSetKernelArg (myRaytraceRenderKernel, anIndex++,
1658 sizeof(cl_float16), theOrigins);
1659 anError |= clSetKernelArg (myRaytraceRenderKernel, anIndex++,
1660 sizeof(cl_float16), theDirects);
1662 cl_int aLightCount = static_cast<cl_int> (myRaytraceSceneData.LightSources.size());
1664 anError |= clSetKernelArg (myRaytraceRenderKernel, anIndex++,
1665 sizeof(cl_int), &aLightCount);
1666 anError |= clSetKernelArg (myRaytraceRenderKernel, anIndex++,
1667 sizeof(cl_float), &myRaytraceSceneEpsilon);
1668 anError |= clSetKernelArg (myRaytraceRenderKernel, anIndex++,
1669 sizeof(cl_float), &myRaytraceSceneRadius);
1670 anError |= clSetKernelArg (myRaytraceRenderKernel, anIndex++,
1671 sizeof(cl_int), &theCView.IsShadowsEnabled);
1672 anError |= clSetKernelArg (myRaytraceRenderKernel, anIndex++,
1673 sizeof(cl_int), &theCView.IsReflectionsEnabled);
1674 anError |= clSetKernelArg (myRaytraceRenderKernel, anIndex++,
1675 sizeof(cl_int), &theSizeX);
1676 anError |= clSetKernelArg (myRaytraceRenderKernel, anIndex++,
1677 sizeof(cl_int), &theSizeY);
1678 if (anError != CL_SUCCESS)
1680 const TCollection_ExtendedString aMsg = "Error! Failed to set arguments of ray-tracing kernel!";
1681 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
1682 GL_DEBUG_TYPE_ERROR_ARB,
1684 GL_DEBUG_SEVERITY_HIGH_ARB,
1686 return Standard_False;
1689 // Note: second-pass 'smoothing' kernel runs only if anti-aliasing is enabled
1690 if (theCView.IsAntialiasingEnabled)
1693 anError = clSetKernelArg (myRaytraceSmoothKernel, anIndex++,
1694 sizeof(cl_mem), &myRaytraceOutputImage);
1695 anError |= clSetKernelArg (myRaytraceSmoothKernel, anIndex++,
1696 sizeof(cl_mem), &myRaytraceOutputImageAA);
1697 anError |= clSetKernelArg (myRaytraceSmoothKernel, anIndex++,
1698 sizeof(cl_mem), &myRaytraceEnvironment);
1699 anError |= clSetKernelArg (myRaytraceSmoothKernel, anIndex++,
1700 sizeof(cl_mem), &myRaytraceNodeMinPointBuffer);
1701 anError |= clSetKernelArg (myRaytraceSmoothKernel, anIndex++,
1702 sizeof(cl_mem), &myRaytraceNodeMaxPointBuffer);
1703 anError |= clSetKernelArg (myRaytraceSmoothKernel, anIndex++,
1704 sizeof(cl_mem), &myRaytraceNodeDataRcrdBuffer);
1705 anError |= clSetKernelArg (myRaytraceSmoothKernel, anIndex++,
1706 sizeof(cl_mem), &myRaytraceLightSourceBuffer);
1707 anError |= clSetKernelArg (myRaytraceSmoothKernel, anIndex++,
1708 sizeof(cl_mem), &myRaytraceMaterialBuffer);
1709 anError |= clSetKernelArg (myRaytraceSmoothKernel, anIndex++,
1710 sizeof(cl_mem), &myRaytraceVertexBuffer);
1711 anError |= clSetKernelArg (myRaytraceSmoothKernel, anIndex++,
1712 sizeof(cl_mem), &myRaytraceNormalBuffer);
1713 anError |= clSetKernelArg (myRaytraceSmoothKernel, anIndex++,
1714 sizeof(cl_mem), &myRaytraceTriangleBuffer);
1716 anError |= clSetKernelArg (myRaytraceSmoothKernel, anIndex++,
1717 sizeof(cl_float16), theOrigins);
1718 anError |= clSetKernelArg (myRaytraceSmoothKernel, anIndex++,
1719 sizeof(cl_float16), theDirects);
1721 anError |= clSetKernelArg (myRaytraceSmoothKernel, anIndex++,
1722 sizeof(cl_int), &aLightCount);
1723 anError |= clSetKernelArg (myRaytraceSmoothKernel, anIndex++,
1724 sizeof(cl_float), &myRaytraceSceneEpsilon);
1725 anError |= clSetKernelArg (myRaytraceSmoothKernel, anIndex++,
1726 sizeof(cl_float), &myRaytraceSceneRadius);
1727 anError |= clSetKernelArg (myRaytraceSmoothKernel, anIndex++,
1728 sizeof(cl_int), &theCView.IsShadowsEnabled);
1729 anError |= clSetKernelArg (myRaytraceSmoothKernel, anIndex++,
1730 sizeof(cl_int), &theCView.IsReflectionsEnabled);
1731 anError |= clSetKernelArg (myRaytraceSmoothKernel, anIndex++,
1732 sizeof(cl_int), &theSizeX);
1733 anError |= clSetKernelArg (myRaytraceSmoothKernel, anIndex++,
1734 sizeof(cl_int), &theSizeY);
1735 if (anError != CL_SUCCESS)
1737 const TCollection_ExtendedString aMsg = "Error! Failed to set arguments of 'smoothing' kernel!";
1738 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
1739 GL_DEBUG_TYPE_ERROR_ARB,
1741 GL_DEBUG_SEVERITY_HIGH_ARB,
1743 return Standard_False;
1748 size_t aLocSizeRender[] = { myIsAmdComputePlatform ? 2 : 4, 32 };
1750 #ifdef OPENCL_GROUP_SIZE_TEST
1751 for (size_t aLocX = 2; aLocX <= 32; aLocX <<= 1 )
1752 for (size_t aLocY = 2; aLocY <= 32; aLocY <<= 1 )
1755 #ifdef OPENCL_GROUP_SIZE_TEST
1756 aLocSizeRender[0] = aLocX;
1757 aLocSizeRender[1] = aLocY;
1760 size_t aWorkSizeX = theSizeX;
1761 if (aWorkSizeX % aLocSizeRender[0] != 0)
1762 aWorkSizeX += aLocSizeRender[0] - aWorkSizeX % aLocSizeRender[0];
1764 size_t aWokrSizeY = theSizeY;
1765 if (aWokrSizeY % aLocSizeRender[1] != 0 )
1766 aWokrSizeY += aLocSizeRender[1] - aWokrSizeY % aLocSizeRender[1];
1768 size_t aGlbSizeRender[] = { aWorkSizeX, aWokrSizeY };
1771 cl_event anEvent (NULL), anEventSmooth (NULL);
1772 anError = clEnqueueNDRangeKernel (myRaytraceQueue, myRaytraceRenderKernel,
1773 2, NULL, aGlbSizeRender, aLocSizeRender,
1775 if (anError != CL_SUCCESS)
1777 const TCollection_ExtendedString aMsg = "Error! Failed to execute the ray-tracing kernel!";
1778 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
1779 GL_DEBUG_TYPE_ERROR_ARB,
1781 GL_DEBUG_SEVERITY_HIGH_ARB,
1783 return Standard_False;
1785 clWaitForEvents (1, &anEvent);
1787 if (theCView.IsAntialiasingEnabled)
1789 size_t aLocSizeSmooth[] = { myIsAmdComputePlatform ? 8 : 4,
1790 myIsAmdComputePlatform ? 8 : 32 };
1792 #ifdef OPENCL_GROUP_SIZE_TEST
1793 aLocSizeSmooth[0] = aLocX;
1794 aLocSizeSmooth[1] = aLocY;
1797 aWorkSizeX = theSizeX;
1798 if (aWorkSizeX % aLocSizeSmooth[0] != 0)
1799 aWorkSizeX += aLocSizeSmooth[0] - aWorkSizeX % aLocSizeSmooth[0];
1801 size_t aWokrSizeY = theSizeY;
1802 if (aWokrSizeY % aLocSizeSmooth[1] != 0 )
1803 aWokrSizeY += aLocSizeSmooth[1] - aWokrSizeY % aLocSizeSmooth[1];
1805 size_t aGlbSizeSmooth [] = { aWorkSizeX, aWokrSizeY };
1806 anError = clEnqueueNDRangeKernel (myRaytraceQueue, myRaytraceSmoothKernel,
1807 2, NULL, aGlbSizeSmooth, aLocSizeSmooth,
1808 0, NULL, &anEventSmooth);
1809 clWaitForEvents (1, &anEventSmooth);
1811 if (anError != CL_SUCCESS)
1813 const TCollection_ExtendedString aMsg = "Error! Failed to execute the 'smoothing' kernel!";
1814 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
1815 GL_DEBUG_TYPE_ERROR_ARB,
1817 GL_DEBUG_SEVERITY_HIGH_ARB,
1819 return Standard_False;
1823 // Get the profiling data
1824 #if defined (RAY_TRACE_PRINT_INFO) || defined(OPENCL_GROUP_SIZE_TEST)
1826 cl_ulong aTimeStart,
1829 clGetEventProfilingInfo (anEvent, CL_PROFILING_COMMAND_START,
1830 sizeof(aTimeStart), &aTimeStart, NULL);
1831 clGetEventProfilingInfo (anEvent, CL_PROFILING_COMMAND_END,
1832 sizeof(aTimeFinal), &aTimeFinal, NULL);
1833 std::cout << "\tRender time (ms): " << ( aTimeFinal - aTimeStart ) / 1e6f << std::endl;
1835 if (theCView.IsAntialiasingEnabled)
1837 clGetEventProfilingInfo (anEventSmooth, CL_PROFILING_COMMAND_START,
1838 sizeof(aTimeStart), &aTimeStart, NULL);
1839 clGetEventProfilingInfo (anEventSmooth, CL_PROFILING_COMMAND_END,
1840 sizeof(aTimeFinal), &aTimeFinal, NULL);
1841 std::cout << "\tSmoothing time (ms): " << ( aTimeFinal - aTimeStart ) / 1e6f << std::endl;
1845 if (anEvent != NULL)
1846 clReleaseEvent (anEvent);
1848 if (anEventSmooth != NULL)
1849 clReleaseEvent (anEventSmooth);
1852 return Standard_True;
1855 // =======================================================================
1856 // function : ComputeInverseMatrix
1857 // purpose : Computes inversion of 4x4 floating-point matrix
1858 // =======================================================================
1859 template <typename T>
1860 void ComputeInverseMatrix (const T m[16], T inv[16])
1862 inv[ 0] = m[ 5] * (m[10] * m[15] - m[11] * m[14]) -
1863 m[ 9] * (m[ 6] * m[15] - m[ 7] * m[14]) -
1864 m[13] * (m[ 7] * m[10] - m[ 6] * m[11]);
1866 inv[ 1] = m[ 1] * (m[11] * m[14] - m[10] * m[15]) -
1867 m[ 9] * (m[ 3] * m[14] - m[ 2] * m[15]) -
1868 m[13] * (m[ 2] * m[11] - m[ 3] * m[10]);
1870 inv[ 2] = m[ 1] * (m[ 6] * m[15] - m[ 7] * m[14]) -
1871 m[ 5] * (m[ 2] * m[15] - m[ 3] * m[14]) -
1872 m[13] * (m[ 3] * m[ 6] - m[ 2] * m[ 7]);
1874 inv[ 3] = m[ 1] * (m[ 7] * m[10] - m[ 6] * m[11]) -
1875 m[ 5] * (m[ 3] * m[10] - m[ 2] * m[11]) -
1876 m[ 9] * (m[ 2] * m[ 7] - m[ 3] * m[ 6]);
1878 inv[ 4] = m[ 4] * (m[11] * m[14] - m[10] * m[15]) -
1879 m[ 8] * (m[ 7] * m[14] - m[ 6] * m[15]) -
1880 m[12] * (m[ 6] * m[11] - m[ 7] * m[10]);
1882 inv[ 5] = m[ 0] * (m[10] * m[15] - m[11] * m[14]) -
1883 m[ 8] * (m[ 2] * m[15] - m[ 3] * m[14]) -
1884 m[12] * (m[ 3] * m[10] - m[ 2] * m[11]);
1886 inv[ 6] = m[ 0] * (m[ 7] * m[14] - m[ 6] * m[15]) -
1887 m[ 4] * (m[ 3] * m[14] - m[ 2] * m[15]) -
1888 m[12] * (m[ 2] * m[ 7] - m[ 3] * m[ 6]);
1890 inv[ 7] = m[ 0] * (m[ 6] * m[11] - m[ 7] * m[10]) -
1891 m[ 4] * (m[ 2] * m[11] - m[ 3] * m[10]) -
1892 m[ 8] * (m[ 3] * m[ 6] - m[ 2] * m[ 7]);
1894 inv[ 8] = m[ 4] * (m[ 9] * m[15] - m[11] * m[13]) -
1895 m[ 8] * (m[ 5] * m[15] - m[ 7] * m[13]) -
1896 m[12] * (m[ 7] * m[ 9] - m[ 5] * m[11]);
1898 inv[ 9] = m[ 0] * (m[11] * m[13] - m[ 9] * m[15]) -
1899 m[ 8] * (m[ 3] * m[13] - m[ 1] * m[15]) -
1900 m[12] * (m[ 1] * m[11] - m[ 3] * m[ 9]);
1902 inv[10] = m[ 0] * (m[ 5] * m[15] - m[ 7] * m[13]) -
1903 m[ 4] * (m[ 1] * m[15] - m[ 3] * m[13]) -
1904 m[12] * (m[ 3] * m[ 5] - m[ 1] * m[ 7]);
1906 inv[11] = m[ 0] * (m[ 7] * m[ 9] - m[ 5] * m[11]) -
1907 m[ 4] * (m[ 3] * m[ 9] - m[ 1] * m[11]) -
1908 m[ 8] * (m[ 1] * m[ 7] - m[ 3] * m[ 5]);
1910 inv[12] = m[ 4] * (m[10] * m[13] - m[ 9] * m[14]) -
1911 m[ 8] * (m[ 6] * m[13] - m[ 5] * m[14]) -
1912 m[12] * (m[ 5] * m[10] - m[ 6] * m[ 9]);
1914 inv[13] = m[ 0] * (m[ 9] * m[14] - m[10] * m[13]) -
1915 m[ 8] * (m[ 1] * m[14] - m[ 2] * m[13]) -
1916 m[12] * (m[ 2] * m[ 9] - m[ 1] * m[10]);
1918 inv[14] = m[ 0] * (m[ 6] * m[13] - m[ 5] * m[14]) -
1919 m[ 4] * (m[ 2] * m[13] - m[ 1] * m[14]) -
1920 m[12] * (m[ 1] * m[ 6] - m[ 2] * m[ 5]);
1922 inv[15] = m[ 0] * (m[ 5] * m[10] - m[ 6] * m[ 9]) -
1923 m[ 4] * (m[ 1] * m[10] - m[ 2] * m[ 9]) -
1924 m[ 8] * (m[ 2] * m[ 5] - m[ 1] * m[ 6]);
1926 T det = m[0] * inv[ 0] +
1931 if (det == T (0.0)) return;
1933 det = T (1.0) / det;
1935 for (Standard_Integer i = 0; i < 16; ++i)
1939 // =======================================================================
1940 // function : GenerateCornerRays
1941 // purpose : Generates primary rays for corners of screen quad
1942 // =======================================================================
1943 void GenerateCornerRays (const GLdouble theInvModelProj[16],
1944 cl_float theOrigins[16],
1945 cl_float theDirects[16])
1947 Standard_Integer aOriginIndex = 0;
1948 Standard_Integer aDirectIndex = 0;
1950 for (Standard_Integer y = -1; y <= 1; y += 2)
1952 for (Standard_Integer x = -1; x <= 1; x += 2)
1954 OpenGl_RTVec4f aOrigin (float(x),
1959 aOrigin = MatVecMult (theInvModelProj, aOrigin);
1961 OpenGl_RTVec4f aDirect (float(x),
1966 aDirect = MatVecMult (theInvModelProj, aDirect) - aOrigin;
1968 GLdouble aInvLen = 1.f / sqrt (aDirect.x() * aDirect.x() +
1969 aDirect.y() * aDirect.y() +
1970 aDirect.z() * aDirect.z());
1972 theOrigins [aOriginIndex++] = static_cast<GLfloat> (aOrigin.x());
1973 theOrigins [aOriginIndex++] = static_cast<GLfloat> (aOrigin.y());
1974 theOrigins [aOriginIndex++] = static_cast<GLfloat> (aOrigin.z());
1975 theOrigins [aOriginIndex++] = 1.f;
1977 theDirects [aDirectIndex++] = static_cast<GLfloat> (aDirect.x() * aInvLen);
1978 theDirects [aDirectIndex++] = static_cast<GLfloat> (aDirect.y() * aInvLen);
1979 theDirects [aDirectIndex++] = static_cast<GLfloat> (aDirect.z() * aInvLen);
1980 theDirects [aDirectIndex++] = 0.f;
1985 // =======================================================================
1986 // function : Raytrace
1987 // purpose : Redraws the window using OpenCL ray tracing
1988 // =======================================================================
1989 Standard_Boolean OpenGl_Workspace::Raytrace (const Graphic3d_CView& theCView,
1990 const Standard_Integer theSizeX,
1991 const Standard_Integer theSizeY,
1992 const Tint theToSwap)
1995 return Standard_False;
1997 if (!ResizeRaytraceOutputBuffer (theSizeX, theSizeY))
1998 return Standard_False;
2000 if (!UpdateRaytraceEnvironmentMap())
2001 return Standard_False;
2003 if (!UpdateRaytraceGeometry (Standard_True))
2004 return Standard_False;
2006 // Get model-view and projection matrices
2007 TColStd_Array2OfReal theOrientation (0, 3, 0, 3);
2008 TColStd_Array2OfReal theViewMapping (0, 3, 0, 3);
2010 myView->GetMatrices (theOrientation, theViewMapping, Standard_True);
2012 GLdouble aOrientationMatrix[16];
2013 GLdouble aViewMappingMatrix[16];
2014 GLdouble aOrientationInvers[16];
2016 for (Standard_Integer j = 0; j < 4; ++j)
2017 for (Standard_Integer i = 0; i < 4; ++i)
2019 aOrientationMatrix [4 * j + i] = theOrientation (i, j);
2020 aViewMappingMatrix [4 * j + i] = theViewMapping (i, j);
2023 ComputeInverseMatrix (aOrientationMatrix, aOrientationInvers);
2025 if (!UpdateRaytraceLightSources (aOrientationInvers))
2026 return Standard_False;
2028 // Generate primary rays for corners of the screen quad
2029 glMatrixMode (GL_MODELVIEW);
2031 glLoadMatrixd (aViewMappingMatrix);
2032 glMultMatrixd (aOrientationMatrix);
2034 GLdouble aModelProject[16];
2035 GLdouble aInvModelProj[16];
2037 glGetDoublev (GL_MODELVIEW_MATRIX, aModelProject);
2039 ComputeInverseMatrix (aModelProject, aInvModelProj);
2041 GLfloat aOrigins[16];
2042 GLfloat aDirects[16];
2044 GenerateCornerRays (aInvModelProj,
2048 // Compute ray-traced image using OpenCL kernel
2049 cl_mem anImages[] = { myRaytraceOutputImage, myRaytraceOutputImageAA };
2050 cl_int anError = clEnqueueAcquireGLObjects (myRaytraceQueue,
2053 clFinish (myRaytraceQueue);
2055 if (myIsRaytraceDataValid)
2057 RunRaytraceOpenCLKernels (theCView,
2064 anError |= clEnqueueReleaseGLObjects (myRaytraceQueue,
2067 clFinish (myRaytraceQueue);
2070 glPushAttrib (GL_ENABLE_BIT |
2072 GL_COLOR_BUFFER_BIT |
2073 GL_DEPTH_BUFFER_BIT);
2075 glDisable (GL_DEPTH_TEST);
2077 if (NamedStatus & OPENGL_NS_WHITEBACK)
2079 glClearColor (1.0f, 1.0f, 1.0f, 1.0f);
2083 glClearColor (myBgColor.rgb[0],
2089 glClear (GL_COLOR_BUFFER_BIT);
2091 Handle(OpenGl_Workspace) aWorkspace (this);
2092 myView->DrawBackground (aWorkspace);
2094 // Draw dummy quad to show result image
2095 glEnable (GL_COLOR_MATERIAL);
2096 glEnable (GL_BLEND);
2098 glDisable (GL_DEPTH_TEST);
2100 glBlendFunc (GL_ONE, GL_SRC_ALPHA);
2102 glEnable (GL_TEXTURE_RECTANGLE);
2104 glMatrixMode (GL_PROJECTION);
2107 glMatrixMode (GL_MODELVIEW);
2110 glColor3f (1.0f, 1.0f, 1.0f);
2112 if (!theCView.IsAntialiasingEnabled)
2113 myRaytraceOutputTexture->Bind (myGlContext);
2115 myRaytraceOutputTextureAA->Bind (myGlContext);
2117 if (myIsRaytraceDataValid)
2121 glTexCoord2i ( 0, 0); glVertex2f (-1.f, -1.f);
2122 glTexCoord2i ( 0, theSizeY); glVertex2f (-1.f, 1.f);
2123 glTexCoord2i (theSizeX, theSizeY); glVertex2f ( 1.f, 1.f);
2124 glTexCoord2i (theSizeX, 0); glVertex2f ( 1.f, -1.f);
2134 GetGlContext()->SwapBuffers();
2135 myBackBufferRestored = Standard_False;
2140 return Standard_True;