0024739: TKOpenGl - port ray-tracing from OpenCL to GLSL for better integration and...
[occt.git] / src / OpenGl / OpenGl_Workspace_Raytrace.cxx
index 2090cef..5074174 100755 (executable)
 // Alternatively, this file may be used under the terms of Open CASCADE
 // commercial license or contractual agreement.
 
-#ifdef HAVE_CONFIG_H
-  #include <config.h>
-#endif
-
-#ifdef HAVE_OPENCL
-
-#include <OpenGl_Cl.hxx>
-
-#if defined(_WIN32)
-
-  #include <windows.h>
-  #include <wingdi.h>
-
-  #pragma comment (lib, "DelayImp.lib")
-  #pragma comment (lib, "OpenCL.lib")
-
-#elif defined(__APPLE__) && !defined(MACOSX_USE_GLX)
-  #include <OpenGL/CGLCurrent.h>
-#else
-  #include <GL/glx.h>
-#endif
-
-#include <OpenGl_Context.hxx>
+#include <NCollection_Mat4.hxx>
+#include <OpenGl_ArbFBO.hxx>
+#include <OpenGl_FrameBuffer.hxx>
 #include <OpenGl_Texture.hxx>
+#include <OpenGl_VertexBuffer.hxx>
 #include <OpenGl_View.hxx>
 #include <OpenGl_Workspace.hxx>
+#include <OSD_File.hxx>
+#include <OSD_Protection.hxx>
 #include <Standard_Assert.hxx>
 
 using namespace OpenGl_Raytrace;
 
 //! Use this macro to output ray-tracing debug info
-//#define RAY_TRACE_PRINT_INFO
+// #define RAY_TRACE_PRINT_INFO
 
 #ifdef RAY_TRACE_PRINT_INFO
   #include <OSD_Timer.hxx>
 #endif
 
-//! OpenCL source of ray-tracing kernels.
-extern const char THE_RAY_TRACE_OPENCL_SOURCE[];
-
 // =======================================================================
 // function : MatVecMult
 // purpose  : Multiples 4x4 matrix by 4D vector
@@ -71,86 +51,6 @@ BVH_Vec4f MatVecMult (const T m[16], const BVH_Vec4f& v)
                         m[11] * v.z() + m[15] * v.w()));
 }
 
-// =======================================================================
-// function : UpdateRaytraceEnvironmentMap
-// purpose  : Updates environment map for ray-tracing
-// =======================================================================
-Standard_Boolean OpenGl_Workspace::UpdateRaytraceEnvironmentMap()
-{
-  if (myView.IsNull())
-    return Standard_False;
-
-  if (myViewModificationStatus == myView->ModificationState())
-    return Standard_True;
-
-  cl_int anError = CL_SUCCESS;
-
-  if (myRaytraceEnvironment != NULL)
-    clReleaseMemObject (myRaytraceEnvironment);
-
-  Standard_Integer aSizeX = 1;
-  Standard_Integer aSizeY = 1;
-
-  if (!myView->TextureEnv().IsNull() && myView->SurfaceDetail() != Visual3d_TOD_NONE)
-  {
-    aSizeX = (myView->TextureEnv()->SizeX() <= 0) ? 1 : myView->TextureEnv()->SizeX();
-    aSizeY = (myView->TextureEnv()->SizeY() <= 0) ? 1 : myView->TextureEnv()->SizeY();
-  }
-
-  cl_image_format anImageFormat;
-
-  anImageFormat.image_channel_order = CL_RGBA;
-  anImageFormat.image_channel_data_type = CL_FLOAT;
-
-  myRaytraceEnvironment = clCreateImage2D (myComputeContext,
-    CL_MEM_READ_ONLY, &anImageFormat, aSizeX, aSizeY, 0, NULL, &anError);
-
-  cl_float* aPixelData = new cl_float[aSizeX * aSizeY * 4];
-
-  // Note: texture format is not compatible with OpenCL image
-  // (it's not possible to create image directly from texture)
-
-  if (!myView->TextureEnv().IsNull() && myView->SurfaceDetail() != Visual3d_TOD_NONE)
-  {
-    myView->TextureEnv()->Bind (GetGlContext());
-
-    glGetTexImage (GL_TEXTURE_2D,
-                   0,
-                   GL_RGBA,
-                   GL_FLOAT,
-                   aPixelData);
-
-    myView->TextureEnv()->Unbind (GetGlContext());
-  }
-  else
-  {
-    for (Standard_Integer aPixel = 0; aPixel < aSizeX * aSizeY * 4; ++aPixel)
-      aPixelData[aPixel] = 0.f;
-  }
-
-  size_t anImageOffset[] = { 0,
-                             0,
-                             0 };
-
-  size_t anImageRegion[] = { aSizeX,
-                             aSizeY,
-                             1 };
-
-  anError |= clEnqueueWriteImage (myComputeQueue, myRaytraceEnvironment,
-    CL_TRUE, anImageOffset, anImageRegion, 0, 0, aPixelData, 0, NULL, NULL);
-
-#ifdef RAY_TRACE_PRINT_INFO
-  if (anError != CL_SUCCESS)
-    std::cout << "Error! Failed to write environment map image!" << std::endl;
-#endif
-
-  delete[] aPixelData;
-
-  myViewModificationStatus = myView->ModificationState();
-
-  return (anError == CL_SUCCESS);
-}
-
 // =======================================================================
 // function : UpdateRaytraceGeometry
 // purpose  : Updates 3D scene geometry for ray tracing
@@ -263,10 +163,12 @@ Standard_Boolean OpenGl_Workspace::UpdateRaytraceGeometry (Standard_Boolean theC
 
     myRaytraceSceneRadius = 2.f /* scale factor */ * Max (aMinRadius, aMaxRadius);
 
-    myRaytraceSceneEpsilon = Max (1e-4f,
-      myRaytraceGeometry.Box().Size().Length() * 1e-4f);
+    const BVH_Vec4f aSize = myRaytraceGeometry.Box().Size();
 
-    return WriteRaytraceSceneToDevice();
+    myRaytraceSceneEpsilon = Max (1e-4f, 1e-4f * sqrtf (
+      aSize.x() * aSize.x() + aSize.y() * aSize.y() + aSize.z() * aSize.z()));
+
+    return UploadRaytraceData();
   }
 
   delete [] aTransform;
@@ -345,10 +247,10 @@ void CreateMaterial (const OPENGL_SURF_PROP& theProp, OpenGl_RaytraceMaterial& t
 
   const float aReflectionScale = 0.75f / aMaxRefl;
 
-  theMaterial.Reflection = BVH_Vec4f (theProp.speccol.rgb[0] * theProp.spec,
-                                      theProp.speccol.rgb[1] * theProp.spec,
-                                      theProp.speccol.rgb[2] * theProp.spec,
-                                      0.f) * aReflectionScale;
+  theMaterial.Reflection = BVH_Vec4f (theProp.speccol.rgb[0] * theProp.spec * aReflectionScale,
+                                      theProp.speccol.rgb[1] * theProp.spec * aReflectionScale,
+                                      theProp.speccol.rgb[2] * theProp.spec * aReflectionScale,
+                                      0.f);
 }
 
 // =======================================================================
@@ -857,7 +759,7 @@ Standard_Boolean OpenGl_Workspace::UpdateRaytraceLightSources (const GLdouble th
 {
   myRaytraceGeometry.Sources.clear();
 
-  myRaytraceGeometry.GlobalAmbient = BVH_Vec4f (0.0f, 0.0f, 0.0f, 0.0f);
+  myRaytraceGeometry.Ambient = BVH_Vec4f (0.0f, 0.0f, 0.0f, 0.0f);
 
   for (OpenGl_ListOfLight::Iterator anItl (myView->LightList()); anItl.More(); anItl.Next())
   {
@@ -865,10 +767,10 @@ Standard_Boolean OpenGl_Workspace::UpdateRaytraceLightSources (const GLdouble th
 
     if (aLight.Type == Visual3d_TOLS_AMBIENT)
     {
-      myRaytraceGeometry.GlobalAmbient += BVH_Vec4f (aLight.Color.r(),
-                                                     aLight.Color.g(),
-                                                     aLight.Color.b(),
-                                                     0.0f);
+      myRaytraceGeometry.Ambient += BVH_Vec4f (aLight.Color.r(),
+                                               aLight.Color.g(),
+                                               aLight.Color.b(),
+                                               0.0f);
       continue;
     }
 
@@ -893,1216 +795,1179 @@ Standard_Boolean OpenGl_Workspace::UpdateRaytraceLightSources (const GLdouble th
     if (aLight.IsHeadlight)
       aPosition = MatVecMult (theInvModelView, aPosition);
 
+    
     myRaytraceGeometry.Sources.push_back (OpenGl_RaytraceLight (aDiffuse, aPosition));
   }
 
-  cl_int anError = CL_SUCCESS;
-
-  if (myRaytraceLightSourceBuffer != NULL)
-    clReleaseMemObject (myRaytraceLightSourceBuffer);
-
-  Standard_Integer aLightBufferSize = myRaytraceGeometry.Sources.size() != 0 ?
-    static_cast<Standard_Integer> (myRaytraceGeometry.Sources.size()) : 1;
-
-  myRaytraceLightSourceBuffer = clCreateBuffer (myComputeContext, CL_MEM_READ_ONLY,
-    aLightBufferSize * sizeof(OpenGl_RaytraceLight), NULL, &anError);
-
-  if (myRaytraceGeometry.Sources.size() != 0)
+  if (myRaytraceLightSrcTexture.IsNull())  // create light source buffer
   {
-    const void* aDataPtr = myRaytraceGeometry.Sources.front().Packed();
-
-    anError |= clEnqueueWriteBuffer (myComputeQueue, myRaytraceLightSourceBuffer, CL_TRUE, 0,
-      aLightBufferSize * sizeof(OpenGl_RaytraceLight), aDataPtr, 0, NULL, NULL);
-  }
+    myRaytraceLightSrcTexture = new OpenGl_TextureBufferArb;
 
+    if (!myRaytraceLightSrcTexture->Create (myGlContext))
+    {
 #ifdef RAY_TRACE_PRINT_INFO
-  if (anError != CL_SUCCESS)
-  {
-    std::cout << "Error! Failed to set light sources";
-
-    return Standard_False;
-  }
+      std::cout << "Error: Failed to create light source buffer" << std::endl;
 #endif
-
-  return Standard_True;
-}
-
-// =======================================================================
-// function : CheckOpenCL
-// purpose  : Checks OpenCL dynamic library availability
-// =======================================================================
-Standard_Boolean CheckOpenCL()
-{
-#if defined ( _WIN32 )
-
-  __try
-  {
-    cl_uint aNbPlatforms;
-    clGetPlatformIDs (0, NULL, &aNbPlatforms);
+      return Standard_False;
+    }
   }
-  __except (EXCEPTION_EXECUTE_HANDLER)
+  
+  if (myRaytraceGeometry.Sources.size() != 0)
   {
-    return Standard_False;
-  }
+    const GLfloat* aDataPtr = myRaytraceGeometry.Sources.front().Packed();
+
+    bool aResult = myRaytraceLightSrcTexture->Init (
+      myGlContext, 4, myRaytraceGeometry.Sources.size() * 2, aDataPtr);
 
+    if (!aResult)
+    {
+#ifdef RAY_TRACE_PRINT_INFO
+      std::cout << "Error: Failed to upload light source buffer" << std::endl;
 #endif
+      return Standard_False;
+    }
+  }
 
   return Standard_True;
 }
 
 // =======================================================================
-// function : InitOpenCL
-// purpose  : Initializes OpenCL objects
+// function : UpdateRaytraceEnvironmentMap
+// purpose  : Updates environment map for ray-tracing
 // =======================================================================
-Standard_Boolean OpenGl_Workspace::InitOpenCL()
+Standard_Boolean OpenGl_Workspace::UpdateRaytraceEnvironmentMap()
 {
-  if (myComputeInitStatus != OpenGl_CLIS_NONE)
-  {
-    return myComputeInitStatus == OpenGl_CLIS_INIT;
-  }
-
-  if (!CheckOpenCL())
-  {
-    myComputeInitStatus = OpenGl_CLIS_FAIL; // fail to load OpenCL library
-    myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
-                              GL_DEBUG_TYPE_ERROR_ARB,
-                              0,
-                              GL_DEBUG_SEVERITY_HIGH_ARB,
-                              "Failed to load OpenCL dynamic library!");
+  if (myView.IsNull())
     return Standard_False;
-  }
 
-  // Obtain the list of platforms available
-  cl_uint aNbPlatforms = 0;
-  cl_int  anError = clGetPlatformIDs (0, NULL, &aNbPlatforms);
-  cl_platform_id* aPlatforms = (cl_platform_id* )alloca (aNbPlatforms * sizeof(cl_platform_id));
-  anError |= clGetPlatformIDs (aNbPlatforms, aPlatforms, NULL);
-  if (anError != CL_SUCCESS
-   || aNbPlatforms == 0)
-  {
-    myComputeInitStatus = OpenGl_CLIS_FAIL;
-    myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
-                              GL_DEBUG_TYPE_ERROR_ARB,
-                              0,
-                              GL_DEBUG_SEVERITY_HIGH_ARB,
-                              "No any OpenCL platform installed!");
-    return Standard_False;
-  }
+  if (myViewModificationStatus == myView->ModificationState())
+    return Standard_True;
 
-  // Note: We try to find NVIDIA or AMD platforms with GPU devices!
-  cl_platform_id aPrefPlatform = NULL;
-  for (cl_uint aPlatIter = 0; aPlatIter < aNbPlatforms; ++aPlatIter)
+  for (Standard_Integer anIdx = 0; anIdx < 2; ++anIdx)
   {
-    char aName[256];
-    anError = clGetPlatformInfo (aPlatforms[aPlatIter], CL_PLATFORM_NAME,
-                                 sizeof(aName), aName, NULL);
-    if (anError != CL_SUCCESS)
-    {
-      continue;
-    }
+    const Handle(OpenGl_ShaderProgram)& aProgram =
+      anIdx == 0 ? myRaytraceProgram : myPostFSAAProgram;
 
-    if (strncmp (aName, "NVIDIA", strlen ("NVIDIA")) == 0)
+    if (!aProgram.IsNull())
     {
-      aPrefPlatform = aPlatforms[aPlatIter];
-
-      // Use optimizations for NVIDIA GPUs
-      myIsAmdComputePlatform = Standard_False;
-    }
-    else if (strncmp (aName, "AMD", strlen ("AMD")) == 0)
-    {
-      aPrefPlatform = (aPrefPlatform == NULL)
-                    ? aPlatforms[aPlatIter]
-                    : aPrefPlatform;
-
-      // Use optimizations for ATI/AMD platform
-      myIsAmdComputePlatform = Standard_True;
-    }
-  }
-
-  if (aPrefPlatform == NULL)
-  {
-    aPrefPlatform = aPlatforms[0];
-  }
-
-  // Obtain the list of devices available in the selected platform
-  cl_uint aNbDevices = 0;
-  anError = clGetDeviceIDs (aPrefPlatform, CL_DEVICE_TYPE_GPU,
-                            0, NULL, &aNbDevices);
-
-  cl_device_id* aDevices = (cl_device_id* )alloca (aNbDevices * sizeof(cl_device_id));
-  anError |= clGetDeviceIDs (aPrefPlatform, CL_DEVICE_TYPE_GPU,
-                             aNbDevices, aDevices, NULL);
-  if (anError != CL_SUCCESS)
-  {
-    myComputeInitStatus = OpenGl_CLIS_FAIL;
-    myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
-                              GL_DEBUG_TYPE_ERROR_ARB,
-                              0,
-                              GL_DEBUG_SEVERITY_HIGH_ARB,
-                              "Failed to get OpenCL GPU device!");
-    return Standard_False;
-  }
-
-  // Note: Simply get first available GPU
-  cl_device_id aDevice = aDevices[0];
-
-  // detect old contexts
-  char aVerClStr[256];
-  clGetDeviceInfo (aDevice, CL_DEVICE_VERSION,
-                   sizeof(aVerClStr), aVerClStr, NULL);
-  aVerClStr[strlen ("OpenCL 1.0")] = '\0';
-  const bool isVer10 = strncmp (aVerClStr, "OpenCL 1.0", strlen ("OpenCL 1.0")) == 0;
-
-  // Create OpenCL context
-  cl_context_properties aCtxProp[] =
-  {
-  #if defined(__APPLE__) && !defined(MACOSX_USE_GLX)
-    CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE,
-    (cl_context_properties )CGLGetShareGroup (CGLGetCurrentContext()),
-  #elif defined(_WIN32)
-    CL_CONTEXT_PLATFORM, (cl_context_properties )aPrefPlatform,
-    CL_GL_CONTEXT_KHR,   (cl_context_properties )wglGetCurrentContext(),
-    CL_WGL_HDC_KHR,      (cl_context_properties )wglGetCurrentDC(),
-  #else
-    CL_GL_CONTEXT_KHR,   (cl_context_properties )glXGetCurrentContext(),
-    CL_GLX_DISPLAY_KHR,  (cl_context_properties )glXGetCurrentDisplay(),
-    CL_CONTEXT_PLATFORM, (cl_context_properties )aPrefPlatform,
-  #endif
-    0
-  };
-
-  myComputeContext = clCreateContext (aCtxProp,
-                                    #if defined(__APPLE__) && !defined(MACOSX_USE_GLX)
-                                      0, NULL, // device will be taken from GL context
-                                    #else
-                                      1, &aDevice,
-                                    #endif
-                                      NULL, NULL, &anError);
-  if (anError != CL_SUCCESS)
-  {
-    myComputeInitStatus = OpenGl_CLIS_FAIL;
-    myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
-                              GL_DEBUG_TYPE_ERROR_ARB,
-                              0,
-                              GL_DEBUG_SEVERITY_HIGH_ARB,
-                              "Failed to initialize OpenCL context!");
-    return Standard_False;
-  }
-
-  // Create OpenCL program
-  const char* aSources[] =
-  {
-    isVer10 ? "#define M_PI_F ( float )( 3.14159265359f )\n" : "",
-    THE_RAY_TRACE_OPENCL_SOURCE
-  };
-  myRaytraceProgram = clCreateProgramWithSource (myComputeContext, 2,
-                                                 aSources, NULL, &anError);
-  if (anError != CL_SUCCESS)
-  {
-    myComputeInitStatus = OpenGl_CLIS_FAIL;
-    myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
-                              GL_DEBUG_TYPE_ERROR_ARB,
-                              0,
-                              GL_DEBUG_SEVERITY_HIGH_ARB,
-                              "Failed to create OpenCL ray-tracing program!");
-    return Standard_False;
-  }
-
-  anError = clBuildProgram (myRaytraceProgram, 0,
-                            NULL, NULL, NULL, NULL);
-  {
-    // Fetch build log
-    size_t aLogLen = 0;
-    cl_int aResult = clGetProgramBuildInfo (myRaytraceProgram, aDevice,
-                                            CL_PROGRAM_BUILD_LOG, 0, NULL, &aLogLen);
+      aProgram->Bind (myGlContext);
 
-    char* aBuildLog = (char* )alloca (aLogLen);
-    aResult |= clGetProgramBuildInfo (myRaytraceProgram, aDevice,
-                                      CL_PROGRAM_BUILD_LOG, aLogLen, aBuildLog, NULL);
-    if (aResult == CL_SUCCESS)
-    {
-      if (anError != CL_SUCCESS)
+      if (!myView->TextureEnv().IsNull() && myView->SurfaceDetail() != Visual3d_TOD_NONE)
       {
-        myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
-                                  GL_DEBUG_TYPE_ERROR_ARB,
-                                  0,
-                                  GL_DEBUG_SEVERITY_HIGH_ARB,
-                                  aBuildLog);
+        myView->TextureEnv()->Bind (
+          myGlContext, GL_TEXTURE0 + OpenGl_RT_EnvironmentMapTexture);
+
+        aProgram->SetUniform (myGlContext, "uEnvironmentEnable", 1);
       }
       else
       {
-      #ifdef RAY_TRACE_PRINT_INFO
-        std::cout << aBuildLog << std::endl;
-      #endif
+        aProgram->SetUniform (myGlContext, "uEnvironmentEnable", 0);
       }
-    }
-  }
-
-  if (anError != CL_SUCCESS)
-  {
-    return Standard_False;
-  }
-
-  // Create OpenCL ray tracing kernels
-  myRaytraceRenderKernel = clCreateKernel (myRaytraceProgram, "RaytraceRender", &anError);
-  if (anError != CL_SUCCESS)
-  {
-    myComputeInitStatus = OpenGl_CLIS_FAIL;
-    myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
-                              GL_DEBUG_TYPE_ERROR_ARB,
-                              0,
-                              GL_DEBUG_SEVERITY_HIGH_ARB,
-                              "Failed to create OpenCL ray-tracing kernel!");
-    return Standard_False;
-  }
 
-  myRaytraceSmoothKernel = clCreateKernel (myRaytraceProgram, "RaytraceSmooth", &anError);
-  if (anError != CL_SUCCESS)
-  {
-    myComputeInitStatus = OpenGl_CLIS_FAIL;
-    myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
-                              GL_DEBUG_TYPE_ERROR_ARB,
-                              0,
-                              GL_DEBUG_SEVERITY_HIGH_ARB,
-                              "Failed to create OpenCL ray-tracing kernel!");
-    return Standard_False;
+      aProgram->SetSampler (myGlContext,
+        "uEnvironmentMapTexture", OpenGl_RT_EnvironmentMapTexture);
+    }
   }
 
-  // Create OpenCL command queue
-  // Note: For profiling set CL_QUEUE_PROFILING_ENABLE
-  cl_command_queue_properties aProps = CL_QUEUE_PROFILING_ENABLE;
-
-  myComputeQueue = clCreateCommandQueue (myComputeContext, aDevice, aProps, &anError);
-  if (anError != CL_SUCCESS)
-  {
-    myComputeInitStatus = OpenGl_CLIS_FAIL;
-    myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
-                              GL_DEBUG_TYPE_ERROR_ARB,
-                              0,
-                              GL_DEBUG_SEVERITY_HIGH_ARB,
-                              "Failed to create OpenCL command queue!");
+  OpenGl_ShaderProgram::Unbind (myGlContext);
 
-    return Standard_False;
-  }
+  myViewModificationStatus = myView->ModificationState();
 
-  myComputeInitStatus = OpenGl_CLIS_INIT; // initialized in normal way
   return Standard_True;
 }
 
 // =======================================================================
-// function : GetOpenClDeviceInfo
-// purpose  : Returns information about device used for computations
+// function : Source
+// purpose  : Returns shader source combined with prefix
 // =======================================================================
-Standard_Boolean OpenGl_Workspace::GetOpenClDeviceInfo (NCollection_DataMap<TCollection_AsciiString,
-                                                                            TCollection_AsciiString>& theInfo) const
+TCollection_AsciiString OpenGl_Workspace::ShaderSource::Source() const
 {
-  theInfo.Clear();
-  if (myComputeContext == NULL)
-  {
-    return Standard_False;
-  }
+  static const TCollection_AsciiString aVersion = "#version 140";
 
-  size_t aDevicesSize = 0;
-  cl_int anError = clGetContextInfo (myComputeContext, CL_CONTEXT_DEVICES, 0, NULL, &aDevicesSize);
-  cl_device_id* aDevices = (cl_device_id* )alloca (aDevicesSize);
-  anError |= clGetContextInfo (myComputeContext, CL_CONTEXT_DEVICES, aDevicesSize, aDevices, NULL);
-  if (anError != CL_SUCCESS)
+  if (myPrefix.IsEmpty())
   {
-    return Standard_False;
+    return aVersion + "\n" + mySource;
   }
 
-  char aDeviceName[256];
-  anError |= clGetDeviceInfo (aDevices[0], CL_DEVICE_NAME, sizeof(aDeviceName), aDeviceName, NULL);
-  theInfo.Bind ("Name", aDeviceName);
-
-  char aDeviceVendor[256];
-  anError |= clGetDeviceInfo (aDevices[0], CL_DEVICE_VENDOR, sizeof(aDeviceVendor), aDeviceVendor, NULL);
-  theInfo.Bind ("Vendor", aDeviceVendor);
-
-  cl_device_type aDeviceType;
-  anError |= clGetDeviceInfo (aDevices[0], CL_DEVICE_TYPE, sizeof(aDeviceType), &aDeviceType, NULL);
-  theInfo.Bind ("Type", aDeviceType == CL_DEVICE_TYPE_GPU ? "GPU" : "CPU");
-  return Standard_True;
+  return aVersion + "\n" + myPrefix + "\n" + mySource;
 }
 
 // =======================================================================
-// function : ReleaseOpenCL
-// purpose  : Releases resources of OpenCL objects
+// function : Load
+// purpose  : Loads shader source from specified files
 // =======================================================================
-void OpenGl_Workspace::ReleaseOpenCL()
+void OpenGl_Workspace::ShaderSource::Load (
+  const TCollection_AsciiString* theFileNames, const Standard_Integer theCount)
 {
-  clReleaseKernel (myRaytraceRenderKernel);
-  clReleaseKernel (myRaytraceSmoothKernel);
+  mySource.Clear();
 
-  clReleaseProgram (myRaytraceProgram);
-  clReleaseCommandQueue (myComputeQueue);
-
-  clReleaseMemObject (myRaytraceOutputImage);
-  clReleaseMemObject (myRaytraceEnvironment);
-  clReleaseMemObject (myRaytraceOutputImageAA);
+  for (Standard_Integer anIndex = 0; anIndex < theCount; ++anIndex)
+  {
+    OSD_File aFile (theFileNames[anIndex]);
 
-  clReleaseMemObject (myRaytraceMaterialBuffer);
-  clReleaseMemObject (myRaytraceLightSourceBuffer);
+    Standard_ASSERT_RETURN (aFile.Exists(),
+      "Error: Failed to find shader source file", /* none */);
 
-  clReleaseMemObject (mySceneNodeInfoBuffer);
-  clReleaseMemObject (mySceneMinPointBuffer);
-  clReleaseMemObject (mySceneMaxPointBuffer);
+    aFile.Open (OSD_ReadOnly, OSD_Protection());
 
-  clReleaseMemObject (myObjectNodeInfoBuffer);
-  clReleaseMemObject (myObjectMinPointBuffer);
-  clReleaseMemObject (myObjectMaxPointBuffer);
+    TCollection_AsciiString aSource;
 
-  clReleaseMemObject (myGeometryVertexBuffer);
-  clReleaseMemObject (myGeometryNormalBuffer);
-  clReleaseMemObject (myGeometryTriangBuffer);
+    Standard_ASSERT_RETURN (aFile.IsOpen(),
+      "Error: Failed to open shader source file", /* none */);
 
-  clReleaseContext (myComputeContext);
+    aFile.Read (aSource, (Standard_Integer) aFile.Size());
 
-  if (!myGlContext.IsNull())
-  {
-    if (!myRaytraceOutputTexture.IsNull())
-      myGlContext->DelayedRelease (myRaytraceOutputTexture);
-    myRaytraceOutputTexture.Nullify();
+    if (!aSource.IsEmpty())
+    {
+      mySource += TCollection_AsciiString ("\n") + aSource;
+    }
 
-    if (!myRaytraceOutputTextureAA.IsNull())
-      myGlContext->DelayedRelease (myRaytraceOutputTextureAA);
-    myRaytraceOutputTextureAA.Nullify();
+    aFile.Close();
   }
 }
 
 // =======================================================================
-// function : ResizeRaytraceOutputBuffer
-// purpose  : Resizes OpenCL output image
+// function : LoadShader
+// purpose  : Creates new shader object with specified source
 // =======================================================================
-Standard_Boolean OpenGl_Workspace::ResizeRaytraceOutputBuffer (const cl_int theSizeX,
-                                                               const cl_int theSizeY)
+Handle(OpenGl_ShaderObject) OpenGl_Workspace::LoadShader (const ShaderSource& theSource, GLenum theType)
 {
-  if (myComputeContext == NULL)
-  {
-    return Standard_False;
-  }
+  Handle(OpenGl_ShaderObject) aShader = new OpenGl_ShaderObject (theType);
 
-  if (!myRaytraceOutputTexture.IsNull())
+  if (!aShader->Create (myGlContext))
   {
-    Standard_Boolean toResize = myRaytraceOutputTexture->SizeX() != theSizeX ||
-                                myRaytraceOutputTexture->SizeY() != theSizeY;
+    const TCollection_ExtendedString aMessage = "Error: Failed to create shader object";
+      
+    myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
+      GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMessage);
 
-    if (!toResize)
-      return Standard_True;
+    aShader->Release (myGlContext.operator->());
 
-    if (!myGlContext.IsNull())
-    {
-      if (!myRaytraceOutputTexture.IsNull())
-        myGlContext->DelayedRelease (myRaytraceOutputTexture);
-      if (!myRaytraceOutputTextureAA.IsNull())
-        myGlContext->DelayedRelease (myRaytraceOutputTextureAA);
-    }
+    return Handle(OpenGl_ShaderObject)();
   }
 
-  myRaytraceOutputTexture = new OpenGl_Texture();
-
-  myRaytraceOutputTexture->Create (myGlContext);
-  myRaytraceOutputTexture->InitRectangle (myGlContext,
-    theSizeX, theSizeY, OpenGl_TextureFormat::Create<GLfloat, 4>());
-
-  myRaytraceOutputTextureAA = new OpenGl_Texture();
-
-  myRaytraceOutputTextureAA->Create (myGlContext);
-  myRaytraceOutputTextureAA->InitRectangle (myGlContext,
-    theSizeX, theSizeY, OpenGl_TextureFormat::Create<GLfloat, 4>());
-
-  if (myRaytraceOutputImage != NULL)
-    clReleaseMemObject (myRaytraceOutputImage);
+  if (!aShader->LoadSource (myGlContext, theSource.Source()))
+  {
+    const TCollection_ExtendedString aMessage = "Error: Failed to set shader source";
+      
+    myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
+      GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMessage);
 
-  if (myRaytraceOutputImageAA != NULL)
-    clReleaseMemObject (myRaytraceOutputImageAA);
+    aShader->Release (myGlContext.operator->());
 
-  cl_int anError = CL_SUCCESS;
+    return Handle(OpenGl_ShaderObject)();
+  }
 
-  myRaytraceOutputImage = clCreateFromGLTexture2D (myComputeContext,
-    CL_MEM_READ_WRITE, GL_TEXTURE_RECTANGLE, 0, myRaytraceOutputTexture->TextureId(), &anError);
+  TCollection_AsciiString aBuildLog;
 
-  if (anError != CL_SUCCESS)
+  if (!aShader->Compile (myGlContext))
   {
+    if (aShader->FetchInfoLog (myGlContext, aBuildLog))
+    {
+      const TCollection_ExtendedString aMessage =
+        TCollection_ExtendedString ("Error: Failed to compile shader object:\n") + aBuildLog;
+
 #ifdef RAY_TRACE_PRINT_INFO
-    std::cout << "Error! Failed to create output image!" << std::endl;
+      std::cout << aBuildLog << std::endl;
 #endif
-    return Standard_False;
-  }
 
-  myRaytraceOutputImageAA = clCreateFromGLTexture2D (myComputeContext,
-    CL_MEM_READ_WRITE, GL_TEXTURE_RECTANGLE, 0, myRaytraceOutputTextureAA->TextureId(), &anError);
+      myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
+        GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMessage);
+    }
+    
+    aShader->Release (myGlContext.operator->());
+
+    return Handle(OpenGl_ShaderObject)();
+  }
 
-  if (anError != CL_SUCCESS)
-  {
 #ifdef RAY_TRACE_PRINT_INFO
-    std::cout << "Error! Failed to create anti-aliased output image!" << std::endl;
+  if (aShader->FetchInfoLog (myGlContext, aBuildLog))
+  {
+    if (!aBuildLog.IsEmpty())
+    {
+      std::cout << aBuildLog << std::endl;
+    }
+    else
+    {
+      std::cout << "Info: shader build log is empty" << std::endl;
+    }
+  }  
 #endif
-    return Standard_False;
-  }
 
-  return Standard_True;
+  return aShader;
 }
 
 // =======================================================================
-// function : WriteRaytraceSceneToDevice
-// purpose  : Writes scene geometry to OpenCL device
+// function : SafeFailBack
+// purpose  : Performs safe exit when shaders initialization fails
 // =======================================================================
-Standard_Boolean OpenGl_Workspace::WriteRaytraceSceneToDevice()
+Standard_Boolean OpenGl_Workspace::SafeFailBack (const TCollection_ExtendedString& theMessage)
 {
-  if (myComputeContext == NULL)
-    return Standard_False;
-
-  cl_int anErrorRes = CL_SUCCESS;
+  myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
+    GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, theMessage);
 
-  if (mySceneNodeInfoBuffer != NULL)
-    anErrorRes |= clReleaseMemObject (mySceneNodeInfoBuffer);
+  myComputeInitStatus = OpenGl_RT_FAIL;
 
-  if (mySceneMinPointBuffer != NULL)
-    anErrorRes |= clReleaseMemObject (mySceneMinPointBuffer);
-
-  if (mySceneMaxPointBuffer != NULL)
-    anErrorRes |= clReleaseMemObject (mySceneMaxPointBuffer);
-
-  if (myObjectNodeInfoBuffer != NULL)
-    anErrorRes |= clReleaseMemObject (myObjectNodeInfoBuffer);
+  ReleaseRaytraceResources();
+  
+  return Standard_False;
+}
 
-  if (myObjectMinPointBuffer != NULL)
-    anErrorRes |= clReleaseMemObject (myObjectMinPointBuffer);
+// =======================================================================
+// function : InitRaytraceResources
+// purpose  : Initializes OpenGL/GLSL shader programs
+// =======================================================================
+Standard_Boolean OpenGl_Workspace::InitRaytraceResources()
+{
+  Standard_Boolean aToRebuildShaders = Standard_False;
 
-  if (myObjectMaxPointBuffer != NULL)
-    anErrorRes |= clReleaseMemObject (myObjectMaxPointBuffer);
+  if (myComputeInitStatus == OpenGl_RT_INIT)
+  {
+    if (!myIsRaytraceDataValid)
+      return Standard_True;
 
-  if (myGeometryVertexBuffer != NULL)
-    anErrorRes |= clReleaseMemObject (myGeometryVertexBuffer);
+    const Standard_Integer aRequiredStackSize =
+      myRaytraceGeometry.HighLevelTreeDepth() + myRaytraceGeometry.BottomLevelTreeDepth();
 
-  if (myGeometryNormalBuffer != NULL)
-    anErrorRes |= clReleaseMemObject (myGeometryNormalBuffer);
+    if (myTraversalStackSize < aRequiredStackSize)
+    {
+      myTraversalStackSize = Max (aRequiredStackSize, THE_DEFAULT_STACK_SIZE);
 
-  if (myGeometryTriangBuffer != NULL)
-    anErrorRes |= clReleaseMemObject (myGeometryTriangBuffer);
+      aToRebuildShaders = Standard_True;
+    }
+    else
+    {
+      if (aRequiredStackSize < myTraversalStackSize)
+      {
+        if (myTraversalStackSize > THE_DEFAULT_STACK_SIZE)
+        {
+          myTraversalStackSize = Max (aRequiredStackSize, THE_DEFAULT_STACK_SIZE);
 
-  if (myRaytraceMaterialBuffer != NULL)
-    anErrorRes |= clReleaseMemObject (myRaytraceMaterialBuffer);
+          aToRebuildShaders = Standard_True;
+        }
+      }
+    }
 
-  if (anErrorRes != CL_SUCCESS)
-  {
+    if (aToRebuildShaders)
+    {
 #ifdef RAY_TRACE_PRINT_INFO
-    std::cout << "Error! Failed to release OpenCL buffers" << std::endl;
+      std::cout << "Info: Rebuild shaders with stack size: " << myTraversalStackSize << std::endl;
 #endif
-    return Standard_False;
-  }
 
-  /////////////////////////////////////////////////////////////////////////////
-  // Create material buffer
+      TCollection_AsciiString aStackSizeStr =
+        TCollection_AsciiString ("#define STACK_SIZE ") + TCollection_AsciiString (myTraversalStackSize);
 
-  const size_t aMaterialBufferSize =
-    myRaytraceGeometry.Materials.size() != 0 ? myRaytraceGeometry.Materials.size() : 1;
+      myRaytraceShaderSource.SetPrefix (aStackSizeStr);
+      myPostFSAAShaderSource.SetPrefix (aStackSizeStr);
 
-  myRaytraceMaterialBuffer = clCreateBuffer (myComputeContext,
-    CL_MEM_READ_ONLY, aMaterialBufferSize * sizeof(OpenGl_RaytraceMaterial), NULL, &anErrorRes);
+      if (!myRaytraceShader->LoadSource (myGlContext, myRaytraceShaderSource.Source())
+       || !myPostFSAAShader->LoadSource (myGlContext, myPostFSAAShaderSource.Source()))
+      {
+        return Standard_False;
+      }
 
-  if (anErrorRes != CL_SUCCESS)
-  {
-#ifdef RAY_TRACE_PRINT_INFO
-    std::cout << "Error! Failed to create OpenCL material buffer" << std::endl;
-#endif
-    return Standard_False;
+      if (!myRaytraceShader->Compile (myGlContext)
+       || !myPostFSAAShader->Compile (myGlContext))
+      {
+        return Standard_False;
+      }
+
+      if (!myRaytraceProgram->Link (myGlContext)
+       || !myPostFSAAProgram->Link (myGlContext))
+      {
+        return Standard_False;
+      }
+    }
   }
 
-  /////////////////////////////////////////////////////////////////////////////
-  // Create BVHs buffers
+  if (myComputeInitStatus == OpenGl_RT_NONE)
+  {
+    if (!myGlContext->IsGlGreaterEqual (3, 1))
+    {
+      const TCollection_ExtendedString aMessage = "Ray-tracing requires OpenGL 3.1 and higher";
 
-  cl_int anErrorTmp = CL_SUCCESS;
+      myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
+        GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMessage);
 
-  const NCollection_Handle<BVH_Tree<Standard_ShortReal, 4> >& aBVH = myRaytraceGeometry.BVH();
+      return Standard_False;
+    }
 
-  const size_t aSceneMinPointBufferSize =
-    aBVH->MinPointBuffer().size() != 0 ? aBVH->MinPointBuffer().size() : 1;
+    TCollection_AsciiString aFolder = Graphic3d_ShaderProgram::ShadersFolder();
 
-  mySceneMinPointBuffer = clCreateBuffer (myComputeContext,
-    CL_MEM_READ_ONLY, aSceneMinPointBufferSize * sizeof(cl_float4), NULL, &anErrorTmp);
-  anErrorRes |= anErrorTmp;
+    if (aFolder.IsEmpty())
+    {
+      const TCollection_ExtendedString aMessage = "Failed to locate shaders directory";
+      
+      myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
+        GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMessage);
+      
+      return Standard_False;
+    }
 
-  const size_t aSceneMaxPointBufferSize =
-    aBVH->MaxPointBuffer().size() != 0 ? aBVH->MaxPointBuffer().size() : 1;
+    if (myIsRaytraceDataValid)
+    {
+      myTraversalStackSize = Max (THE_DEFAULT_STACK_SIZE,
+        myRaytraceGeometry.HighLevelTreeDepth() + myRaytraceGeometry.BottomLevelTreeDepth());
+    }
 
-  mySceneMaxPointBuffer = clCreateBuffer (myComputeContext,
-    CL_MEM_READ_ONLY, aSceneMaxPointBufferSize * sizeof(cl_float4), NULL, &anErrorTmp);
-  anErrorRes |= anErrorTmp;
+    {
+      Handle(OpenGl_ShaderObject) aBasicVertShader = LoadShader (
+        ShaderSource (aFolder + "/RaytraceBase.vs"), GL_VERTEX_SHADER);
 
-  const size_t aSceneNodeInfoBufferSize =
-    aBVH->NodeInfoBuffer().size() != 0 ? aBVH->NodeInfoBuffer().size() : 1;
+      if (aBasicVertShader.IsNull())
+      {
+        return SafeFailBack ("Failed to set vertex shader source");
+      }
 
-  mySceneNodeInfoBuffer = clCreateBuffer (myComputeContext,
-    CL_MEM_READ_ONLY, aSceneNodeInfoBufferSize * sizeof(cl_int4), NULL, &anErrorTmp);
-  anErrorRes |= anErrorTmp;
+      TCollection_AsciiString aFiles[] = { aFolder + "/RaytraceBase.fs", aFolder + "/RaytraceRender.fs" };
 
-  if (anErrorRes != CL_SUCCESS)
-  {
-#ifdef RAY_TRACE_PRINT_INFO
-    std::cout << "Error! Failed to create OpenCL buffers for high-level scene BVH" << std::endl;
-#endif
-    return Standard_False;
-  }
+      myRaytraceShaderSource.Load (aFiles, 2);
 
-  Standard_Integer aTotalVerticesNb = 0;
-  Standard_Integer aTotalElementsNb = 0;
-  Standard_Integer aTotalBVHNodesNb = 0;
+      TCollection_AsciiString aStackSizeStr =
+        TCollection_AsciiString ("#define STACK_SIZE ") + TCollection_AsciiString (myTraversalStackSize);
 
-  for (Standard_Integer anElemIndex = 0; anElemIndex < myRaytraceGeometry.Size(); ++anElemIndex)
-  {
-    OpenGl_TriangleSet* aTriangleSet = dynamic_cast<OpenGl_TriangleSet*> (
-      myRaytraceGeometry.Objects().ChangeValue (anElemIndex).operator->());
+      myRaytraceShaderSource.SetPrefix (aStackSizeStr);
 
-    Standard_ASSERT_RETURN (aTriangleSet != NULL,
-      "Error! Failed to get triangulation of OpenGL element", Standard_False);
+      myRaytraceShader = LoadShader (myRaytraceShaderSource, GL_FRAGMENT_SHADER);
 
-    aTotalVerticesNb += (int)aTriangleSet->Vertices.size();
-    aTotalElementsNb += (int)aTriangleSet->Elements.size();
+      if (myRaytraceShader.IsNull())
+      {
+        aBasicVertShader->Release (myGlContext.operator->());
 
-    Standard_ASSERT_RETURN (!aTriangleSet->BVH().IsNull(),
-      "Error! Failed to get bottom-level BVH of OpenGL element", Standard_False);
+        return SafeFailBack ("Failed to set ray-trace fragment shader source");
+      }
 
-    aTotalBVHNodesNb += (int)aTriangleSet->BVH()->NodeInfoBuffer().size();
-  }
+      myRaytraceProgram = new OpenGl_ShaderProgram;
 
-  aTotalBVHNodesNb = aTotalBVHNodesNb > 0 ? aTotalBVHNodesNb : 1;
+      if (!myRaytraceProgram->Create (myGlContext))
+      {
+        aBasicVertShader->Release (myGlContext.operator->());
 
-  myObjectNodeInfoBuffer = clCreateBuffer (myComputeContext,
-    CL_MEM_READ_ONLY, aTotalBVHNodesNb * sizeof(cl_int4), NULL, &anErrorTmp);
-  anErrorRes |= anErrorTmp;
+        return SafeFailBack ("Failed to create ray-trace shader program");
+      }
 
-  myObjectMinPointBuffer = clCreateBuffer (myComputeContext,
-    CL_MEM_READ_ONLY, aTotalBVHNodesNb * sizeof(cl_float4), NULL, &anErrorTmp);
-  anErrorRes |= anErrorTmp;
+      if (!myRaytraceProgram->AttachShader (myGlContext, aBasicVertShader)
+       || !myRaytraceProgram->AttachShader (myGlContext, myRaytraceShader))
+      {
+        aBasicVertShader->Release (myGlContext.operator->());
 
-  myObjectMaxPointBuffer = clCreateBuffer (myComputeContext,
-    CL_MEM_READ_ONLY, aTotalBVHNodesNb * sizeof(cl_float4), NULL, &anErrorTmp);
-  anErrorRes |= anErrorTmp;
+        return SafeFailBack ("Failed to attach ray-trace shader objects");
+      }
 
-  if (anErrorRes != CL_SUCCESS)
-  {
-#ifdef RAY_TRACE_PRINT_INFO
-    std::cout << "Error! Failed to create OpenCL buffers for bottom-level scene BVHs" << std::endl;
-#endif
-    return Standard_False;
-  }
+      if (!myRaytraceProgram->Link (myGlContext))
+      {
+        TCollection_AsciiString aLinkLog;
 
-  /////////////////////////////////////////////////////////////////////////////
-  // Create geometry buffers
+        if (myRaytraceProgram->FetchInfoLog (myGlContext, aLinkLog))
+        {
+  #ifdef RAY_TRACE_PRINT_INFO
+          std::cout << aLinkLog << std::endl;
+  #endif
+        }
 
-  aTotalVerticesNb = aTotalVerticesNb > 0 ? aTotalVerticesNb : 1;
+        return SafeFailBack ("Failed to link ray-trace shader program");
+      }
+    }
 
-  myGeometryVertexBuffer = clCreateBuffer (myComputeContext,
-    CL_MEM_READ_ONLY, aTotalVerticesNb * sizeof(cl_float4), NULL, &anErrorTmp);
-  anErrorRes |= anErrorTmp;
+    {
+      Handle(OpenGl_ShaderObject) aBasicVertShader = LoadShader (
+        ShaderSource (aFolder + "/RaytraceBase.vs"), GL_VERTEX_SHADER);
 
-  myGeometryNormalBuffer = clCreateBuffer (myComputeContext,
-    CL_MEM_READ_ONLY, aTotalVerticesNb * sizeof(cl_float4), NULL, &anErrorTmp);
-  anErrorRes |= anErrorTmp;
+      if (aBasicVertShader.IsNull())
+      {
+        return SafeFailBack ("Failed to set vertex shader source");
+      }
 
-  aTotalElementsNb = aTotalElementsNb > 0 ? aTotalElementsNb : 1;
+      TCollection_AsciiString aFiles[] = { aFolder + "/RaytraceBase.fs", aFolder + "/RaytraceSmooth.fs" };
 
-  myGeometryTriangBuffer = clCreateBuffer (myComputeContext,
-    CL_MEM_READ_ONLY, aTotalElementsNb * sizeof(cl_int4), NULL, &anErrorTmp);
-  anErrorRes |= anErrorTmp;
+      myPostFSAAShaderSource.Load (aFiles, 2);
 
-  if (anErrorRes != CL_SUCCESS)
-  {
-#ifdef RAY_TRACE_PRINT_INFO
-    std::cout << "Error! Failed to create OpenCL geometry buffers" << std::endl;
-#endif
-    return Standard_False;
-  }
+      TCollection_AsciiString aStackSizeStr =
+        TCollection_AsciiString ("#define STACK_SIZE ") + TCollection_AsciiString (myTraversalStackSize);
 
-  /////////////////////////////////////////////////////////////////////////////
-  // Write BVH and geometry buffers
+      myPostFSAAShaderSource.SetPrefix (aStackSizeStr);
+    
+      myPostFSAAShader = LoadShader (myPostFSAAShaderSource, GL_FRAGMENT_SHADER);
 
-  if (aBVH->NodeInfoBuffer().size() != 0)
-  {
-    anErrorRes |= clEnqueueWriteBuffer (myComputeQueue, mySceneNodeInfoBuffer, CL_FALSE, 0,
-      aSceneNodeInfoBufferSize * sizeof(cl_int4), &aBVH->NodeInfoBuffer().front(), 0, NULL, NULL);
+      if (myPostFSAAShader.IsNull())
+      {
+        aBasicVertShader->Release (myGlContext.operator->());
 
-    anErrorRes |= clEnqueueWriteBuffer (myComputeQueue, mySceneMinPointBuffer, CL_FALSE, 0,
-      aSceneMinPointBufferSize * sizeof(cl_float4), &aBVH->MinPointBuffer().front(), 0, NULL, NULL);
+        return SafeFailBack ("Failed to set FSAA fragment shader source");
+      }
 
-    anErrorRes |= clEnqueueWriteBuffer (myComputeQueue, mySceneMaxPointBuffer, CL_FALSE, 0,
-      aSceneMaxPointBufferSize * sizeof(cl_float4), &aBVH->MaxPointBuffer().front(), 0, NULL, NULL);
+      myPostFSAAProgram = new OpenGl_ShaderProgram;
 
-    anErrorRes |= clFinish (myComputeQueue);
+      if (!myPostFSAAProgram->Create (myGlContext))
+      {
+        aBasicVertShader->Release (myGlContext.operator->());
 
-    if (anErrorRes != CL_SUCCESS)
-    {
-#ifdef RAY_TRACE_PRINT_INFO
-      std::cout << "Error! Failed to write OpenCL buffers for high-level scene BVH" << std::endl;
-#endif
-      return Standard_False;
+        return SafeFailBack ("Failed to create FSAA shader program");
+      }
+
+      if (!myPostFSAAProgram->AttachShader (myGlContext, aBasicVertShader)
+       || !myPostFSAAProgram->AttachShader (myGlContext, myPostFSAAShader))
+      {
+        aBasicVertShader->Release (myGlContext.operator->());
+
+        return SafeFailBack ("Failed to attach FSAA shader objects");
+      }
+
+      if (!myPostFSAAProgram->Link (myGlContext))
+      {
+        TCollection_AsciiString aLinkLog;
+
+        if (myPostFSAAProgram->FetchInfoLog (myGlContext, aLinkLog))
+        {
+  #ifdef RAY_TRACE_PRINT_INFO
+          std::cout << aLinkLog << std::endl;
+  #endif
+        }
+      
+        return SafeFailBack ("Failed to link FSAA shader program");
+      }
     }
+  }
 
-    for (Standard_Integer aNodeIdx = 0; aNodeIdx < aBVH->Length(); ++aNodeIdx)
+  if (myComputeInitStatus == OpenGl_RT_NONE || aToRebuildShaders)
+  {
+    for (Standard_Integer anIndex = 0; anIndex < 2; ++anIndex)
     {
-      if (!aBVH->IsOuter (aNodeIdx))
-        continue;
-
-      OpenGl_TriangleSet* aTriangleSet = myRaytraceGeometry.TriangleSet (aNodeIdx);
+      Handle(OpenGl_ShaderProgram)& aShaderProgram =
+        (anIndex == 0) ? myRaytraceProgram : myPostFSAAProgram;
+
+      aShaderProgram->Bind (myGlContext);
+
+      aShaderProgram->SetSampler (myGlContext,
+        "uSceneMinPointTexture", OpenGl_RT_SceneMinPointTexture);
+      aShaderProgram->SetSampler (myGlContext,
+        "uSceneMaxPointTexture", OpenGl_RT_SceneMaxPointTexture);
+      aShaderProgram->SetSampler (myGlContext,
+        "uSceneNodeInfoTexture", OpenGl_RT_SceneNodeInfoTexture);
+      aShaderProgram->SetSampler (myGlContext,
+        "uObjectMinPointTexture", OpenGl_RT_ObjectMinPointTexture);
+      aShaderProgram->SetSampler (myGlContext,
+        "uObjectMaxPointTexture", OpenGl_RT_ObjectMaxPointTexture);
+      aShaderProgram->SetSampler (myGlContext,
+        "uObjectNodeInfoTexture", OpenGl_RT_ObjectNodeInfoTexture);
+      aShaderProgram->SetSampler (myGlContext,
+        "uGeometryVertexTexture", OpenGl_RT_GeometryVertexTexture);
+      aShaderProgram->SetSampler (myGlContext,
+        "uGeometryNormalTexture", OpenGl_RT_GeometryNormalTexture);
+      aShaderProgram->SetSampler (myGlContext,
+        "uGeometryTriangTexture", OpenGl_RT_GeometryTriangTexture);
+      aShaderProgram->SetSampler (myGlContext,
+        "uRaytraceMaterialTexture", OpenGl_RT_RaytraceMaterialTexture);
+      aShaderProgram->SetSampler (myGlContext,
+        "uRaytraceLightSrcTexture", OpenGl_RT_RaytraceLightSrcTexture);
+
+      if (anIndex == 1)
+      {
+        aShaderProgram->SetSampler (myGlContext,
+          "uFSAAInputTexture", OpenGl_RT_FSAAInputTexture);
+      }
 
-      Standard_ASSERT_RETURN (aTriangleSet != NULL,
-        "Error! Failed to get triangulation of OpenGL element", Standard_False);
+      myUniformLocations[anIndex][OpenGl_RT_aPosition] =
+        aShaderProgram->GetAttributeLocation (myGlContext, "aPosition");
+
+      myUniformLocations[anIndex][OpenGl_RT_uOriginLB] =
+        aShaderProgram->GetUniformLocation (myGlContext, "uOriginLB");
+      myUniformLocations[anIndex][OpenGl_RT_uOriginRB] =
+        aShaderProgram->GetUniformLocation (myGlContext, "uOriginRB");
+      myUniformLocations[anIndex][OpenGl_RT_uOriginLT] =
+        aShaderProgram->GetUniformLocation (myGlContext, "uOriginLT");
+      myUniformLocations[anIndex][OpenGl_RT_uOriginRT] =
+        aShaderProgram->GetUniformLocation (myGlContext, "uOriginRT");
+      myUniformLocations[anIndex][OpenGl_RT_uDirectLB] =
+        aShaderProgram->GetUniformLocation (myGlContext, "uDirectLB");
+      myUniformLocations[anIndex][OpenGl_RT_uDirectRB] =
+        aShaderProgram->GetUniformLocation (myGlContext, "uDirectRB");
+      myUniformLocations[anIndex][OpenGl_RT_uDirectLT] =
+        aShaderProgram->GetUniformLocation (myGlContext, "uDirectLT");
+      myUniformLocations[anIndex][OpenGl_RT_uDirectRT] =
+        aShaderProgram->GetUniformLocation (myGlContext, "uDirectRT");
+
+      myUniformLocations[anIndex][OpenGl_RT_uLightCount] =
+        aShaderProgram->GetUniformLocation (myGlContext, "uLightCount");
+      myUniformLocations[anIndex][OpenGl_RT_uLightAmbnt] =
+        aShaderProgram->GetUniformLocation (myGlContext, "uGlobalAmbient");
+
+      myUniformLocations[anIndex][OpenGl_RT_uSceneRad] =
+        aShaderProgram->GetUniformLocation (myGlContext, "uSceneRadius");
+      myUniformLocations[anIndex][OpenGl_RT_uSceneEps] =
+        aShaderProgram->GetUniformLocation (myGlContext, "uSceneEpsilon");
+
+      myUniformLocations[anIndex][OpenGl_RT_uShadEnabled] =
+        aShaderProgram->GetUniformLocation (myGlContext, "uShadowsEnable");
+      myUniformLocations[anIndex][OpenGl_RT_uReflEnabled] =
+        aShaderProgram->GetUniformLocation (myGlContext, "uReflectionsEnable");
+
+      myUniformLocations[anIndex][OpenGl_RT_uOffsetX] =
+        aShaderProgram->GetUniformLocation (myGlContext, "uOffsetX");
+      myUniformLocations[anIndex][OpenGl_RT_uOffsetY] =
+        aShaderProgram->GetUniformLocation (myGlContext, "uOffsetY");
+      myUniformLocations[anIndex][OpenGl_RT_uSamples] =
+        aShaderProgram->GetUniformLocation (myGlContext, "uSamples");
+    }
 
-      const size_t aBVHBuffserSize =
-        aTriangleSet->BVH()->NodeInfoBuffer().size() != 0 ? aTriangleSet->BVH()->NodeInfoBuffer().size() : 1;
+    OpenGl_ShaderProgram::Unbind (myGlContext);
+  }
 
-      const Standard_Integer aBVHOffset = myRaytraceGeometry.AccelerationOffset (aNodeIdx);
+  if (myComputeInitStatus != OpenGl_RT_NONE)
+  {
+    return myComputeInitStatus == OpenGl_RT_INIT;
+  }
 
-      Standard_ASSERT_RETURN (aBVHOffset != OpenGl_RaytraceGeometry::INVALID_OFFSET,
-        "Error! Failed to get offset for bottom-level BVH", Standard_False);
+  if (myRaytraceFBO1.IsNull())
+  {
+    myRaytraceFBO1 = new OpenGl_FrameBuffer;
+  }
 
-      anErrorRes |= clEnqueueWriteBuffer (myComputeQueue, myObjectNodeInfoBuffer, CL_FALSE, aBVHOffset * sizeof(cl_int4),
-        aBVHBuffserSize * sizeof(cl_int4), &aTriangleSet->BVH()->NodeInfoBuffer().front(), 0, NULL, NULL);
+  if (myRaytraceFBO2.IsNull())
+  {
+    myRaytraceFBO2 = new OpenGl_FrameBuffer;
+  }
 
-      anErrorRes |= clEnqueueWriteBuffer (myComputeQueue, myObjectMinPointBuffer, CL_FALSE, aBVHOffset * sizeof(cl_float4),
-        aBVHBuffserSize * sizeof(cl_float4), &aTriangleSet->BVH()->MinPointBuffer().front(), 0, NULL, NULL);
+  const GLfloat aVertices[] = { -1.f, -1.f,  0.f,
+                                -1.f,  1.f,  0.f,
+                                 1.f,  1.f,  0.f,
+                                 1.f,  1.f,  0.f,
+                                 1.f, -1.f,  0.f,
+                                -1.f, -1.f,  0.f };
 
-      anErrorRes |= clEnqueueWriteBuffer (myComputeQueue, myObjectMaxPointBuffer, CL_FALSE, aBVHOffset * sizeof(cl_float4),
-        aBVHBuffserSize * sizeof(cl_float4), &aTriangleSet->BVH()->MaxPointBuffer().front(), 0, NULL, NULL);
+  myRaytraceScreenQuad.Init (myGlContext, 3, 6, aVertices);
 
-      anErrorRes |= clFinish (myComputeQueue);
+  myComputeInitStatus = OpenGl_RT_INIT; // initialized in normal way
+  
+  return Standard_True;
+}
 
-      if (anErrorRes != CL_SUCCESS)
-      {
-#ifdef RAY_TRACE_PRINT_INFO
-        std::cout << "Error! Failed to write OpenCL buffers for bottom-level scene BVHs" << std::endl;
-#endif
-        return Standard_False;
-      }
+// =======================================================================
+// function : NullifyResource
+// purpose  :
+// =======================================================================
+inline void NullifyResource (const Handle(OpenGl_Context)& theContext,
+                             Handle(OpenGl_Resource)&      theResource)
+{
+  if (!theResource.IsNull())
+  {
+    theResource->Release (theContext.operator->());
+    theResource.Nullify();
+  }
+}
 
-      const Standard_Integer aVerticesOffset = myRaytraceGeometry.VerticesOffset (aNodeIdx);
+// =======================================================================
+// function : ReleaseRaytraceResources
+// purpose  : Releases OpenGL/GLSL shader programs
+// =======================================================================
+void OpenGl_Workspace::ReleaseRaytraceResources()
+{
+  NullifyResource (myGlContext, myRaytraceFBO1);
+  NullifyResource (myGlContext, myRaytraceFBO2);
 
-      Standard_ASSERT_RETURN (aVerticesOffset != OpenGl_RaytraceGeometry::INVALID_OFFSET,
-        "Error! Failed to get offset for triangulation vertices of OpenGL element", Standard_False);
+  NullifyResource (myGlContext, myRaytraceShader);
+  NullifyResource (myGlContext, myPostFSAAShader);
 
-      anErrorRes |= clEnqueueWriteBuffer (myComputeQueue, myGeometryVertexBuffer, CL_FALSE, aVerticesOffset * sizeof(cl_float4),
-        aTriangleSet->Vertices.size() * sizeof(cl_float4), &aTriangleSet->Vertices.front(), 0, NULL, NULL);
+  NullifyResource (myGlContext, myRaytraceProgram);
+  NullifyResource (myGlContext, myPostFSAAProgram);
 
-      anErrorRes |= clEnqueueWriteBuffer (myComputeQueue, myGeometryNormalBuffer, CL_FALSE, aVerticesOffset * sizeof(cl_float4),
-        aTriangleSet->Normals.size() * sizeof(cl_float4), &aTriangleSet->Normals.front(), 0, NULL, NULL);
+  NullifyResource (myGlContext, mySceneNodeInfoTexture);
+  NullifyResource (myGlContext, mySceneMinPointTexture);
+  NullifyResource (myGlContext, mySceneMaxPointTexture);
 
-      const Standard_Integer anElementsOffset = myRaytraceGeometry.ElementsOffset (aNodeIdx);
+  NullifyResource (myGlContext, myObjectNodeInfoTexture);
+  NullifyResource (myGlContext, myObjectMinPointTexture);
+  NullifyResource (myGlContext, myObjectMaxPointTexture);
 
-      Standard_ASSERT_RETURN (anElementsOffset != OpenGl_RaytraceGeometry::INVALID_OFFSET,
-        "Error! Failed to get offset for triangulation elements of OpenGL element", Standard_False);
+  NullifyResource (myGlContext, myGeometryVertexTexture);
+  NullifyResource (myGlContext, myGeometryNormalTexture);
+  NullifyResource (myGlContext, myGeometryTriangTexture);
 
-      anErrorRes |= clEnqueueWriteBuffer (myComputeQueue, myGeometryTriangBuffer, CL_FALSE, anElementsOffset * sizeof(cl_int4),
-        aTriangleSet->Elements.size() * sizeof(cl_int4), &aTriangleSet->Elements.front(), 0, NULL, NULL);
+  NullifyResource (myGlContext, myRaytraceLightSrcTexture);
+  NullifyResource (myGlContext, myRaytraceMaterialTexture);
 
-      anErrorRes |= clFinish (myComputeQueue);
+  if (myRaytraceScreenQuad.IsValid())
+    myRaytraceScreenQuad.Release (myGlContext.operator->());
+}
 
-      if (anErrorRes != CL_SUCCESS)
-      {
+// =======================================================================
+// function : UploadRaytraceData
+// purpose  : Uploads ray-trace data to the GPU
+// =======================================================================
+Standard_Boolean OpenGl_Workspace::UploadRaytraceData()
+{
+  if (!myGlContext->IsGlGreaterEqual (3, 1))
+  {
 #ifdef RAY_TRACE_PRINT_INFO
-        std::cout << "Error! Failed to write OpenCL triangulation buffers for OpenGL element" << std::endl;
+    std::cout << "Error: OpenGL version is less than 3.1" << std::endl;
 #endif
-        return Standard_False;
-      }
-    }
+    return Standard_False;
   }
 
   /////////////////////////////////////////////////////////////////////////////
-  // Write material buffer
+  // Create OpenGL texture buffers
 
-  if (myRaytraceGeometry.Materials.size() != 0)
+  if (mySceneNodeInfoTexture.IsNull())  // create hight-level BVH buffers
   {
-    const void* aDataPtr = myRaytraceGeometry.Materials.front().Packed();
-
-    anErrorRes |= clEnqueueWriteBuffer (myComputeQueue, myRaytraceMaterialBuffer,
-      CL_FALSE, 0, aMaterialBufferSize * sizeof(OpenGl_RaytraceMaterial), aDataPtr, 0, NULL, NULL);
+    mySceneNodeInfoTexture = new OpenGl_TextureBufferArb;
+    mySceneMinPointTexture = new OpenGl_TextureBufferArb;
+    mySceneMaxPointTexture = new OpenGl_TextureBufferArb;
 
-    if (anErrorRes != CL_SUCCESS)
+    if (!mySceneNodeInfoTexture->Create (myGlContext)
+      || !mySceneMinPointTexture->Create (myGlContext)
+      || !mySceneMaxPointTexture->Create (myGlContext))
     {
-  #ifdef RAY_TRACE_PRINT_INFO
-      std::cout << "Error! Failed to write OpenCL material buffer" << std::endl;
-  #endif
+#ifdef RAY_TRACE_PRINT_INFO
+      std::cout << "Error: Failed to create buffers for high-level scene BVH" << std::endl;
+#endif
       return Standard_False;
     }
   }
 
-  anErrorRes |= clFinish (myComputeQueue);
-
-  if (anErrorRes == CL_SUCCESS)
+  if (myObjectNodeInfoTexture.IsNull())  // create bottom-level BVH buffers
   {
-    myIsRaytraceDataValid = myRaytraceGeometry.Objects().Size() != 0;
-  }
-#ifdef RAY_TRACE_PRINT_INFO
-  else
-  {
-    std::cout << "Error! Failed to set scene data buffers" << std::endl;
-  }
-#endif
+    myObjectNodeInfoTexture = new OpenGl_TextureBufferArb;
+    myObjectMinPointTexture = new OpenGl_TextureBufferArb;
+    myObjectMaxPointTexture = new OpenGl_TextureBufferArb;
 
+    if (!myObjectNodeInfoTexture->Create (myGlContext)
+      || !myObjectMinPointTexture->Create (myGlContext)
+      || !myObjectMaxPointTexture->Create (myGlContext))
+    {
 #ifdef RAY_TRACE_PRINT_INFO
+      std::cout << "Error: Failed to create buffers for bottom-level scene BVH" << std::endl;
+#endif
+      return Standard_False;
+    }
+  }
 
-  Standard_ShortReal aMemUsed = 0.f;
-
-  for (Standard_Integer anElemIdx = 0; anElemIdx < myRaytraceGeometry.Size(); ++anElemIdx)
+  if (myGeometryVertexTexture.IsNull())  // create geometry buffers
   {
-    OpenGl_TriangleSet* aTriangleSet = dynamic_cast<OpenGl_TriangleSet*> (
-      myRaytraceGeometry.Objects().ChangeValue (anElemIdx).operator->());
+    myGeometryVertexTexture = new OpenGl_TextureBufferArb;
+    myGeometryNormalTexture = new OpenGl_TextureBufferArb;
+    myGeometryTriangTexture = new OpenGl_TextureBufferArb;
 
-    aMemUsed += static_cast<Standard_ShortReal> (
-      aTriangleSet->Vertices.size() * sizeof (BVH_Vec4f));
-    aMemUsed += static_cast<Standard_ShortReal> (
-      aTriangleSet->Normals.size() * sizeof (BVH_Vec4f));
-    aMemUsed += static_cast<Standard_ShortReal> (
-      aTriangleSet->Elements.size() * sizeof (BVH_Vec4i));
-
-    aMemUsed += static_cast<Standard_ShortReal> (
-      aTriangleSet->BVH()->NodeInfoBuffer().size() * sizeof (BVH_Vec4i));
-    aMemUsed += static_cast<Standard_ShortReal> (
-      aTriangleSet->BVH()->MinPointBuffer().size() * sizeof (BVH_Vec4f));
-    aMemUsed += static_cast<Standard_ShortReal> (
-      aTriangleSet->BVH()->MaxPointBuffer().size() * sizeof (BVH_Vec4f));
+    if (!myGeometryVertexTexture->Create (myGlContext)
+      || !myGeometryNormalTexture->Create (myGlContext)
+      || !myGeometryTriangTexture->Create (myGlContext))
+    {
+#ifdef RAY_TRACE_PRINT_INFO
+      std::cout << "Error: Failed to create buffers for triangulation data" << std::endl;
+#endif
+      return Standard_False;
+    }
   }
 
-  aMemUsed += static_cast<Standard_ShortReal> (
-    myRaytraceGeometry.BVH()->NodeInfoBuffer().size() * sizeof (BVH_Vec4i));
-  aMemUsed += static_cast<Standard_ShortReal> (
-    myRaytraceGeometry.BVH()->MinPointBuffer().size() * sizeof (BVH_Vec4f));
-  aMemUsed += static_cast<Standard_ShortReal> (
-    myRaytraceGeometry.BVH()->MaxPointBuffer().size() * sizeof (BVH_Vec4f));
-
-  std::cout << "GPU Memory Used (MB): ~" << aMemUsed / 1048576 << std::endl;
+  if (myRaytraceMaterialTexture.IsNull())  // create material buffer
+  {
+    myRaytraceMaterialTexture = new OpenGl_TextureBufferArb;
 
+    if (!myRaytraceMaterialTexture->Create (myGlContext))
+    {
+#ifdef RAY_TRACE_PRINT_INFO
+      std::cout << "Error: Failed to create buffers for material data" << std::endl;
 #endif
+      return Standard_False;
+    }
+  }
 
-  return (CL_SUCCESS == anErrorRes);
-}
+  /////////////////////////////////////////////////////////////////////////////
+  // Write OpenGL texture buffers
 
-// Use it to estimate the optimal size of OpenCL work group
-// #define OPENCL_GROUP_SIZE_TEST
+  const NCollection_Handle<BVH_Tree<Standard_ShortReal, 4> >& aBVH = myRaytraceGeometry.BVH();
 
-// =======================================================================
-// function : RunRaytraceOpenCLKernels
-// purpose  : Runs OpenCL ray-tracing kernels
-// =======================================================================
-Standard_Boolean OpenGl_Workspace::RunRaytraceOpenCLKernels (const Graphic3d_CView&   theCView,
-                                                             const Standard_ShortReal theOrigins[16],
-                                                             const Standard_ShortReal theDirects[16],
-                                                             const Standard_Integer   theSizeX,
-                                                             const Standard_Integer   theSizeY)
-{
-  if (myRaytraceRenderKernel == NULL || myComputeQueue == NULL)
-    return Standard_False;
+  bool aResult = true;
 
-  ////////////////////////////////////////////////////////////////////////
-  // Set kernel arguments
-
-  cl_uint anIndex = 0;
-  cl_int  anError = 0;
-
-  cl_int aLightSourceBufferSize = (cl_int)myRaytraceGeometry.Sources.size();
-
-  anError |= clSetKernelArg (
-    myRaytraceRenderKernel, anIndex++, sizeof(cl_int), &theSizeX);
-  anError |= clSetKernelArg (
-    myRaytraceRenderKernel, anIndex++, sizeof(cl_int), &theSizeY);
-  anError |= clSetKernelArg (
-    myRaytraceRenderKernel, anIndex++, sizeof(cl_float16), theOrigins);
-  anError |= clSetKernelArg (
-    myRaytraceRenderKernel, anIndex++, sizeof(cl_float16), theDirects);
-  anError |= clSetKernelArg (
-    myRaytraceRenderKernel, anIndex++, sizeof(cl_mem), &myRaytraceEnvironment);
-  anError |= clSetKernelArg (
-    myRaytraceRenderKernel, anIndex++, sizeof(cl_mem), &myRaytraceOutputImage);
-  anError |= clSetKernelArg (
-    myRaytraceRenderKernel, anIndex++, sizeof(cl_mem), &mySceneNodeInfoBuffer);
-  anError |= clSetKernelArg (
-    myRaytraceRenderKernel, anIndex++, sizeof(cl_mem), &mySceneMinPointBuffer);
-  anError |= clSetKernelArg (
-    myRaytraceRenderKernel, anIndex++, sizeof(cl_mem), &mySceneMaxPointBuffer);
-  anError |= clSetKernelArg (
-    myRaytraceRenderKernel, anIndex++, sizeof(cl_mem), &myObjectNodeInfoBuffer);
-  anError |= clSetKernelArg (
-    myRaytraceRenderKernel, anIndex++, sizeof(cl_mem), &myObjectMinPointBuffer);
-  anError |= clSetKernelArg (
-    myRaytraceRenderKernel, anIndex++, sizeof(cl_mem), &myObjectMaxPointBuffer);
-  anError |= clSetKernelArg (
-    myRaytraceRenderKernel, anIndex++, sizeof(cl_mem), &myGeometryTriangBuffer);
-  anError |= clSetKernelArg (
-    myRaytraceRenderKernel, anIndex++, sizeof(cl_mem), &myGeometryVertexBuffer);
-  anError |= clSetKernelArg (
-    myRaytraceRenderKernel, anIndex++, sizeof(cl_mem), &myGeometryNormalBuffer);
-  anError |= clSetKernelArg (
-    myRaytraceRenderKernel, anIndex++, sizeof(cl_mem), &myRaytraceLightSourceBuffer);
-  anError |= clSetKernelArg (
-    myRaytraceRenderKernel, anIndex++, sizeof(cl_mem), &myRaytraceMaterialBuffer);
-  anError |= clSetKernelArg (
-    myRaytraceRenderKernel, anIndex++, sizeof(cl_float4), &myRaytraceGeometry.GlobalAmbient);
-  anError |= clSetKernelArg (
-    myRaytraceRenderKernel, anIndex++, sizeof(cl_int), &aLightSourceBufferSize);
-  anError |= clSetKernelArg (
-    myRaytraceRenderKernel, anIndex++, sizeof(cl_int), &theCView.IsShadowsEnabled);
-  anError |= clSetKernelArg (
-    myRaytraceRenderKernel, anIndex++, sizeof(cl_int), &theCView.IsReflectionsEnabled);
-  anError |= clSetKernelArg (
-    myRaytraceRenderKernel, anIndex++, sizeof(cl_float), &myRaytraceSceneEpsilon);
-  anError |= clSetKernelArg (
-    myRaytraceRenderKernel, anIndex++, sizeof(cl_float), &myRaytraceSceneRadius);
-
-  if (anError != CL_SUCCESS)
-  {
-    const TCollection_ExtendedString aMessage = "Error! Failed to set arguments of ray-tracing kernel!";
+  if (!aBVH->NodeInfoBuffer().empty())
+  {
+    aResult &= mySceneNodeInfoTexture->Init (myGlContext, 4,
+      aBVH->NodeInfoBuffer().size(), reinterpret_cast<const GLuint*> (&aBVH->NodeInfoBuffer().front()));
 
-    myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
-      GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMessage);
+    aResult &= mySceneMinPointTexture->Init (myGlContext, 4,
+      aBVH->MinPointBuffer().size(), reinterpret_cast<const GLfloat*> (&aBVH->MinPointBuffer().front()));
+
+    aResult &= mySceneMaxPointTexture->Init (myGlContext, 4,
+      aBVH->MaxPointBuffer().size(), reinterpret_cast<const GLfloat*> (&aBVH->MaxPointBuffer().front()));
+  }
 
+  if (!aResult)
+  {
+#ifdef RAY_TRACE_PRINT_INFO
+    std::cout << "Error: Failed to upload buffers for high-level scene BVH" << std::endl;
+#endif
     return Standard_False;
   }
 
-  // Second-pass 'smoothing' kernel runs only if anti-aliasing is enabled
-  if (theCView.IsAntialiasingEnabled)
-  {
-    anIndex = 0;
-
-    anError |= clSetKernelArg (
-      myRaytraceSmoothKernel, anIndex++, sizeof(cl_int), &theSizeX);
-    anError |= clSetKernelArg (
-      myRaytraceSmoothKernel, anIndex++, sizeof(cl_int), &theSizeY);
-    anError |= clSetKernelArg (
-      myRaytraceSmoothKernel, anIndex++, sizeof(cl_float16), theOrigins);
-    anError |= clSetKernelArg (
-      myRaytraceSmoothKernel, anIndex++, sizeof(cl_float16), theDirects);
-    anError |= clSetKernelArg (
-      myRaytraceSmoothKernel, anIndex++, sizeof(cl_mem), &myRaytraceOutputImage);
-    anError |= clSetKernelArg (
-      myRaytraceSmoothKernel, anIndex++, sizeof(cl_mem), &myRaytraceEnvironment);
-    anError |= clSetKernelArg (
-      myRaytraceSmoothKernel, anIndex++, sizeof(cl_mem), &myRaytraceOutputImageAA);
-    anError |= clSetKernelArg (
-      myRaytraceSmoothKernel, anIndex++, sizeof(cl_mem), &mySceneNodeInfoBuffer);
-    anError |= clSetKernelArg (
-      myRaytraceSmoothKernel, anIndex++, sizeof(cl_mem), &mySceneMinPointBuffer);
-    anError |= clSetKernelArg (
-      myRaytraceSmoothKernel, anIndex++, sizeof(cl_mem), &mySceneMaxPointBuffer);
-    anError |= clSetKernelArg (
-      myRaytraceSmoothKernel, anIndex++, sizeof(cl_mem), &myObjectNodeInfoBuffer);
-    anError |= clSetKernelArg (
-      myRaytraceSmoothKernel, anIndex++, sizeof(cl_mem), &myObjectMinPointBuffer);
-    anError |= clSetKernelArg (
-      myRaytraceSmoothKernel, anIndex++, sizeof(cl_mem), &myObjectMaxPointBuffer);
-    anError |= clSetKernelArg (
-      myRaytraceSmoothKernel, anIndex++, sizeof(cl_mem), &myGeometryTriangBuffer);
-    anError |= clSetKernelArg (
-      myRaytraceSmoothKernel, anIndex++, sizeof(cl_mem), &myGeometryVertexBuffer);
-    anError |= clSetKernelArg (
-      myRaytraceSmoothKernel, anIndex++, sizeof(cl_mem), &myGeometryNormalBuffer);
-    anError |= clSetKernelArg (
-      myRaytraceSmoothKernel, anIndex++, sizeof(cl_mem), &myRaytraceLightSourceBuffer);
-    anError |= clSetKernelArg (
-      myRaytraceSmoothKernel, anIndex++, sizeof(cl_mem), &myRaytraceMaterialBuffer);
-    anError |= clSetKernelArg (
-      myRaytraceSmoothKernel, anIndex++, sizeof(cl_float4), &myRaytraceGeometry.GlobalAmbient);
-    anError |= clSetKernelArg (
-      myRaytraceSmoothKernel, anIndex++, sizeof(cl_int), &aLightSourceBufferSize);
-    anError |= clSetKernelArg (
-      myRaytraceSmoothKernel, anIndex++, sizeof(cl_int), &theCView.IsShadowsEnabled);
-    anError |= clSetKernelArg (
-      myRaytraceSmoothKernel, anIndex++, sizeof(cl_int), &theCView.IsReflectionsEnabled);
-    anError |= clSetKernelArg (
-      myRaytraceSmoothKernel, anIndex++, sizeof(cl_float), &myRaytraceSceneEpsilon);
-    anError |= clSetKernelArg (
-      myRaytraceSmoothKernel, anIndex++, sizeof(cl_float), &myRaytraceSceneRadius);
-
-    if (anError != CL_SUCCESS)
-    {
-      const TCollection_ExtendedString aMessage = "Error! Failed to set arguments of smoothing kernel!";
+  Standard_Size aTotalVerticesNb = 0;
+  Standard_Size aTotalElementsNb = 0;
+  Standard_Size aTotalBVHNodesNb = 0;
 
-      myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
-        GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMessage);
+  for (Standard_Integer anElemIndex = 0; anElemIndex < myRaytraceGeometry.Size(); ++anElemIndex)
+  {
+    OpenGl_TriangleSet* aTriangleSet = dynamic_cast<OpenGl_TriangleSet*> (
+      myRaytraceGeometry.Objects().ChangeValue (anElemIndex).operator->());
 
-      return Standard_False;
-    }
-  }
+    Standard_ASSERT_RETURN (aTriangleSet != NULL,
+      "Error: Failed to get triangulation of OpenGL element", Standard_False);
+
+    aTotalVerticesNb += aTriangleSet->Vertices.size();
+    aTotalElementsNb += aTriangleSet->Elements.size();
 
-  ////////////////////////////////////////////////////////////////////////
-  // Set work size
+    Standard_ASSERT_RETURN (!aTriangleSet->BVH().IsNull(),
+      "Error: Failed to get bottom-level BVH of OpenGL element", Standard_False);
 
-  size_t aLocWorkSize[] = { myIsAmdComputePlatform ? 2 : 4, 32 };
+    aTotalBVHNodesNb += aTriangleSet->BVH()->NodeInfoBuffer().size();
+  }
 
-#ifdef OPENCL_GROUP_SIZE_TEST
-  for (size_t aLocX = 2; aLocX <= 32; aLocX <<= 1)
-  for (size_t aLocY = 2; aLocY <= 32; aLocY <<= 1)
+  if (aTotalBVHNodesNb != 0)
   {
-    aLocWorkSize[0] = aLocX;
-    aLocWorkSize[1] = aLocY;
-#endif
+    aResult &= myObjectNodeInfoTexture->Init (
+      myGlContext, 4, aTotalBVHNodesNb, static_cast<const GLuint*> (NULL));
 
-    size_t aWorkSizeX = theSizeX;
-    if (aWorkSizeX % aLocWorkSize[0] != 0)
-      aWorkSizeX += aLocWorkSize[0] - aWorkSizeX % aLocWorkSize[0];
+    aResult &= myObjectMinPointTexture->Init (
+      myGlContext, 4, aTotalBVHNodesNb, static_cast<const GLfloat*> (NULL));
 
-    size_t aWokrSizeY = theSizeY;
-    if (aWokrSizeY % aLocWorkSize[1] != 0 )
-      aWokrSizeY += aLocWorkSize[1] - aWokrSizeY % aLocWorkSize[1];
+    aResult &= myObjectMaxPointTexture->Init (
+      myGlContext, 4, aTotalBVHNodesNb, static_cast<const GLfloat*> (NULL));
+  }
 
-    size_t aTotWorkSize[] = { aWorkSizeX, aWokrSizeY };
+  if (!aResult)
+  {
+#ifdef RAY_TRACE_PRINT_INFO
+    std::cout << "Error: Failed to upload buffers for bottom-level scene BVH" << std::endl;
+#endif
+    return Standard_False;
+  }
 
-    cl_event anEvent = NULL, anEventSmooth = NULL;
+  if (aTotalElementsNb != 0)
+  {
+    aResult &= myGeometryTriangTexture->Init (
+      myGlContext, 4, aTotalElementsNb, static_cast<const GLuint*> (NULL));
+  }
 
-    anError = clEnqueueNDRangeKernel (myComputeQueue,
-      myRaytraceRenderKernel, 2, NULL, aTotWorkSize, aLocWorkSize, 0, NULL, &anEvent);
+  if (aTotalVerticesNb != 0)
+  {
+    aResult &= myGeometryVertexTexture->Init (
+      myGlContext, 4, aTotalVerticesNb, static_cast<const GLfloat*> (NULL));
 
-    if (anError != CL_SUCCESS)
-    {
-      const TCollection_ExtendedString aMessage = "Error! Failed to execute the ray-tracing kernel!";
+    aResult &= myGeometryNormalTexture->Init (
+      myGlContext, 4, aTotalVerticesNb, static_cast<const GLfloat*> (NULL));
+  }
 
-      myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
-        GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMessage);
+  if (!aResult)
+  {
+#ifdef RAY_TRACE_PRINT_INFO
+    std::cout << "Error: Failed to upload buffers for scene geometry" << std::endl;
+#endif
+    return Standard_False;
+  }
 
-      return Standard_False;
-    }
+  for (Standard_Integer aNodeIdx = 0; aNodeIdx < aBVH->Length(); ++aNodeIdx)
+  {
+    if (!aBVH->IsOuter (aNodeIdx))
+      continue;
 
-    clWaitForEvents (1, &anEvent);
+    OpenGl_TriangleSet* aTriangleSet = myRaytraceGeometry.TriangleSet (aNodeIdx);
 
-    if (theCView.IsAntialiasingEnabled)
-    {
-      size_t aLocWorkSizeSmooth[] = { myIsAmdComputePlatform ? 8 : 4,
-                                      myIsAmdComputePlatform ? 8 : 32 };
+    Standard_ASSERT_RETURN (aTriangleSet != NULL,
+      "Error: Failed to get triangulation of OpenGL element", Standard_False);
 
-#ifdef OPENCL_GROUP_SIZE_TEST
-      aLocWorkSizeSmooth[0] = aLocX;
-      aLocWorkSizeSmooth[1] = aLocY;
-#endif
+    const Standard_Integer aBVHOffset = myRaytraceGeometry.AccelerationOffset (aNodeIdx);
 
-      aWorkSizeX = theSizeX;
-      if (aWorkSizeX % aLocWorkSizeSmooth[0] != 0)
-        aWorkSizeX += aLocWorkSizeSmooth[0] - aWorkSizeX % aLocWorkSizeSmooth[0];
+    Standard_ASSERT_RETURN (aBVHOffset != OpenGl_RaytraceGeometry::INVALID_OFFSET,
+      "Error: Failed to get offset for bottom-level BVH", Standard_False);
 
-      size_t aWokrSizeY = theSizeY;
-      if (aWokrSizeY % aLocWorkSizeSmooth[1] != 0 )
-        aWokrSizeY += aLocWorkSizeSmooth[1] - aWokrSizeY % aLocWorkSizeSmooth[1];
+    const size_t aBVHBuffserSize = aTriangleSet->BVH()->NodeInfoBuffer().size();
 
-      size_t aTotWorkSizeSmooth [] = { aWorkSizeX, aWokrSizeY };
+    if (aBVHBuffserSize != 0)
+    {
+      aResult &= myObjectNodeInfoTexture->SubData (myGlContext, aBVHOffset,
+        aBVHBuffserSize, reinterpret_cast<const GLuint*> (&aTriangleSet->BVH()->NodeInfoBuffer().front()));
 
-      anError = clEnqueueNDRangeKernel (myComputeQueue, myRaytraceSmoothKernel,
-        2, NULL, aTotWorkSizeSmooth, aLocWorkSizeSmooth, 0, NULL, &anEventSmooth);
+      aResult &= myObjectMinPointTexture->SubData (myGlContext, aBVHOffset,
+        aBVHBuffserSize, reinterpret_cast<const GLfloat*> (&aTriangleSet->BVH()->MinPointBuffer().front()));
 
-      clWaitForEvents (1, &anEventSmooth);
+      aResult &= myObjectMaxPointTexture->SubData (myGlContext, aBVHOffset,
+        aBVHBuffserSize, reinterpret_cast<const GLfloat*> (&aTriangleSet->BVH()->MaxPointBuffer().front()));
 
-      if (anError != CL_SUCCESS)
+      if (!aResult)
       {
-        const TCollection_ExtendedString aMessage = "Error! Failed to execute the smoothing kernel";
-
-        myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
-          GL_DEBUG_TYPE_ERROR_ARB, 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMessage);
-
+#ifdef RAY_TRACE_PRINT_INFO
+        std::cout << "Error: Failed to upload buffers for bottom-level scene BVHs" << std::endl;
+#endif
         return Standard_False;
       }
     }
 
-#if defined (RAY_TRACE_PRINT_INFO) || defined (OPENCL_GROUP_SIZE_TEST)
+    const Standard_Integer aVerticesOffset = myRaytraceGeometry.VerticesOffset (aNodeIdx);
 
-    static cl_ulong ttt1 = 10000000000;
-    static cl_ulong ttt2 = 10000000000;
+    Standard_ASSERT_RETURN (aVerticesOffset != OpenGl_RaytraceGeometry::INVALID_OFFSET,
+      "Error: Failed to get offset for triangulation vertices of OpenGL element", Standard_False);
 
-    cl_ulong aBegTime = 0;
-    cl_ulong aEndTime = 0;
+    if (!aTriangleSet->Vertices.empty())
+    {
+      aResult &= myGeometryNormalTexture->SubData (myGlContext, aVerticesOffset,
+        aTriangleSet->Normals.size(), reinterpret_cast<const GLfloat*> (&aTriangleSet->Normals.front()));
 
-    clGetEventProfilingInfo (anEvent,
-      CL_PROFILING_COMMAND_START, sizeof(aBegTime), &aBegTime, NULL);
-    clGetEventProfilingInfo (anEvent,
-      CL_PROFILING_COMMAND_END, sizeof(aEndTime), &aEndTime, NULL);
+      aResult &= myGeometryVertexTexture->SubData (myGlContext, aVerticesOffset,
+        aTriangleSet->Vertices.size(), reinterpret_cast<const GLfloat*> (&aTriangleSet->Vertices.front()));
+    }
 
-    ttt1 = aEndTime - aBegTime < ttt1 ? aEndTime - aBegTime : ttt1;
+    const Standard_Integer anElementsOffset = myRaytraceGeometry.ElementsOffset (aNodeIdx);
 
-    std::cout << "\tRender time (ms): " << ttt1 / 1e6f << std::endl;
+    Standard_ASSERT_RETURN (anElementsOffset != OpenGl_RaytraceGeometry::INVALID_OFFSET,
+      "Error: Failed to get offset for triangulation elements of OpenGL element", Standard_False);
 
-    if (theCView.IsAntialiasingEnabled)
+    if (!aTriangleSet->Elements.empty())
     {
-      clGetEventProfilingInfo (anEventSmooth,
-        CL_PROFILING_COMMAND_START, sizeof(aBegTime), &aBegTime, NULL);
-      clGetEventProfilingInfo (anEventSmooth,
-        CL_PROFILING_COMMAND_END, sizeof(aEndTime), &aEndTime, NULL);
-
-      ttt2 = aEndTime - aBegTime < ttt2 ? aEndTime - aBegTime : ttt2;
-
-      std::cout << "\tSmooth time (ms): " << ttt2 / 1e6f << std::endl;
+      aResult &= myGeometryTriangTexture->SubData (myGlContext, anElementsOffset,
+        aTriangleSet->Elements.size(), reinterpret_cast<const GLuint*> (&aTriangleSet->Elements.front()));
     }
 
+    if (!aResult)
+    {
+#ifdef RAY_TRACE_PRINT_INFO
+      std::cout << "Error: Failed to upload triangulation buffers for OpenGL element" << std::endl;
 #endif
-
-    if (anEvent != NULL)
-      clReleaseEvent (anEvent);
-
-    if (anEventSmooth != NULL)
-      clReleaseEvent (anEventSmooth);
-
-#ifdef OPENCL_GROUP_SIZE_TEST
+      return Standard_False;
+    }
   }
-#endif
-
-  return Standard_True;
-}
-
-// =======================================================================
-// function : ComputeInverseMatrix
-// purpose  : Computes inversion of 4x4 floating-point matrix
-// =======================================================================
-template <typename T>
-void ComputeInverseMatrix (const T m[16], T inv[16])
-{
-  inv[ 0] = m[ 5] * (m[10] * m[15] - m[11] * m[14]) -
-            m[ 9] * (m[ 6] * m[15] - m[ 7] * m[14]) -
-            m[13] * (m[ 7] * m[10] - m[ 6] * m[11]);
 
-  inv[ 1] = m[ 1] * (m[11] * m[14] - m[10] * m[15]) -
-            m[ 9] * (m[ 3] * m[14] - m[ 2] * m[15]) -
-            m[13] * (m[ 2] * m[11] - m[ 3] * m[10]);
-
-  inv[ 2] = m[ 1] * (m[ 6] * m[15] - m[ 7] * m[14]) -
-            m[ 5] * (m[ 2] * m[15] - m[ 3] * m[14]) -
-            m[13] * (m[ 3] * m[ 6] - m[ 2] * m[ 7]);
-
-  inv[ 3] = m[ 1] * (m[ 7] * m[10] - m[ 6] * m[11]) -
-            m[ 5] * (m[ 3] * m[10] - m[ 2] * m[11]) -
-            m[ 9] * (m[ 2] * m[ 7] - m[ 3] * m[ 6]);
-
-  inv[ 4] = m[ 4] * (m[11] * m[14] - m[10] * m[15]) -
-            m[ 8] * (m[ 7] * m[14] - m[ 6] * m[15]) -
-            m[12] * (m[ 6] * m[11] - m[ 7] * m[10]);
-
-  inv[ 5] = m[ 0] * (m[10] * m[15] - m[11] * m[14]) -
-            m[ 8] * (m[ 2] * m[15] - m[ 3] * m[14]) -
-            m[12] * (m[ 3] * m[10] - m[ 2] * m[11]);
+  if (myRaytraceGeometry.Materials.size() != 0)
+  {
+    const GLfloat* aDataPtr = myRaytraceGeometry.Materials.front().Packed();
 
-  inv[ 6] = m[ 0] * (m[ 7] * m[14] - m[ 6] * m[15]) -
-            m[ 4] * (m[ 3] * m[14] - m[ 2] * m[15]) -
-            m[12] * (m[ 2] * m[ 7] - m[ 3] * m[ 6]);
+    aResult &= myRaytraceMaterialTexture->Init (
+      myGlContext, 4, myRaytraceGeometry.Materials.size() * 7, aDataPtr);
 
-  inv[ 7] = m[ 0] * (m[ 6] * m[11] - m[ 7] * m[10]) -
-            m[ 4] * (m[ 2] * m[11] - m[ 3] * m[10]) -
-            m[ 8] * (m[ 3] * m[ 6] - m[ 2] * m[ 7]);
+    if (!aResult)
+    {
+#ifdef RAY_TRACE_PRINT_INFO
+      std::cout << "Error: Failed to upload material buffer" << std::endl;
+#endif
+      return Standard_False;
+    }
+  }
 
-  inv[ 8] = m[ 4] * (m[ 9] * m[15] - m[11] * m[13]) -
-            m[ 8] * (m[ 5] * m[15] - m[ 7] * m[13]) -
-            m[12] * (m[ 7] * m[ 9] - m[ 5] * m[11]);
+  myIsRaytraceDataValid = myRaytraceGeometry.Objects().Size() != 0;
 
-  inv[ 9] = m[ 0] * (m[11] * m[13] - m[ 9] * m[15]) -
-            m[ 8] * (m[ 3] * m[13] - m[ 1] * m[15]) -
-            m[12] * (m[ 1] * m[11] - m[ 3] * m[ 9]);
+#ifdef RAY_TRACE_PRINT_INFO
 
-  inv[10] = m[ 0] * (m[ 5] * m[15] - m[ 7] * m[13]) -
-            m[ 4] * (m[ 1] * m[15] - m[ 3] * m[13]) -
-            m[12] * (m[ 3] * m[ 5] - m[ 1] * m[ 7]);
+  Standard_ShortReal aMemUsed = 0.f;
 
-  inv[11] = m[ 0] * (m[ 7] * m[ 9] - m[ 5] * m[11]) -
-            m[ 4] * (m[ 3] * m[ 9] - m[ 1] * m[11]) -
-            m[ 8] * (m[ 1] * m[ 7] - m[ 3] * m[ 5]);
+  for (Standard_Integer anElemIdx = 0; anElemIdx < myRaytraceGeometry.Size(); ++anElemIdx)
+  {
+    OpenGl_TriangleSet* aTriangleSet = dynamic_cast<OpenGl_TriangleSet*> (
+      myRaytraceGeometry.Objects().ChangeValue (anElemIdx).operator->());
 
-  inv[12] = m[ 4] * (m[10] * m[13] - m[ 9] * m[14]) -
-            m[ 8] * (m[ 6] * m[13] - m[ 5] * m[14]) -
-            m[12] * (m[ 5] * m[10] - m[ 6] * m[ 9]);
+    aMemUsed += static_cast<Standard_ShortReal> (
+      aTriangleSet->Vertices.size() * sizeof (BVH_Vec4f));
+    aMemUsed += static_cast<Standard_ShortReal> (
+      aTriangleSet->Normals.size() * sizeof (BVH_Vec4f));
+    aMemUsed += static_cast<Standard_ShortReal> (
+      aTriangleSet->Elements.size() * sizeof (BVH_Vec4i));
 
-  inv[13] = m[ 0] * (m[ 9] * m[14] - m[10] * m[13]) -
-            m[ 8] * (m[ 1] * m[14] - m[ 2] * m[13]) -
-            m[12] * (m[ 2] * m[ 9] - m[ 1] * m[10]);
+    aMemUsed += static_cast<Standard_ShortReal> (
+      aTriangleSet->BVH()->NodeInfoBuffer().size() * sizeof (BVH_Vec4i));
+    aMemUsed += static_cast<Standard_ShortReal> (
+      aTriangleSet->BVH()->MinPointBuffer().size() * sizeof (BVH_Vec4f));
+    aMemUsed += static_cast<Standard_ShortReal> (
+      aTriangleSet->BVH()->MaxPointBuffer().size() * sizeof (BVH_Vec4f));
+  }
 
-  inv[14] = m[ 0] * (m[ 6] * m[13] - m[ 5] * m[14]) -
-            m[ 4] * (m[ 2] * m[13] - m[ 1] * m[14]) -
-            m[12] * (m[ 1] * m[ 6] - m[ 2] * m[ 5]);
+  aMemUsed += static_cast<Standard_ShortReal> (
+    myRaytraceGeometry.BVH()->NodeInfoBuffer().size() * sizeof (BVH_Vec4i));
+  aMemUsed += static_cast<Standard_ShortReal> (
+    myRaytraceGeometry.BVH()->MinPointBuffer().size() * sizeof (BVH_Vec4f));
+  aMemUsed += static_cast<Standard_ShortReal> (
+    myRaytraceGeometry.BVH()->MaxPointBuffer().size() * sizeof (BVH_Vec4f));
 
-  inv[15] = m[ 0] * (m[ 5] * m[10] - m[ 6] * m[ 9]) -
-            m[ 4] * (m[ 1] * m[10] - m[ 2] * m[ 9]) -
-            m[ 8] * (m[ 2] * m[ 5] - m[ 1] * m[ 6]);
+  std::cout << "GPU Memory Used (MB): ~" << aMemUsed / 1048576 << std::endl;
 
-  T det = m[0] * inv[ 0] +
-          m[1] * inv[ 4] +
-          m[2] * inv[ 8] +
-          m[3] * inv[12];
+#endif
 
-  if (det == T (0.0)) return;
+  return aResult;
+}
 
-  det = T (1.0) / det;
+// =======================================================================
+// function : ResizeRaytraceBuffers
+// purpose  : Resizes OpenGL frame buffers
+// =======================================================================
+Standard_Boolean OpenGl_Workspace::ResizeRaytraceBuffers (const Standard_Integer theSizeX,
+                                                          const Standard_Integer theSizeY)
+{
+  if (myRaytraceFBO1->GetVPSizeX() != theSizeX
+   || myRaytraceFBO1->GetVPSizeY() != theSizeY)
+  {
+    myRaytraceFBO1->Init (myGlContext, theSizeX, theSizeY);
+    myRaytraceFBO2->Init (myGlContext, theSizeX, theSizeY);
+  }
 
-  for (Standard_Integer i = 0; i < 16; ++i)
-    inv[i] *= det;
+  return Standard_True;
 }
 
 // =======================================================================
-// function : GenerateCornerRays
-// purpose  : Generates primary rays for corners of screen quad
+// function : UpdateCamera
+// purpose  : Generates viewing rays for corners of screen quad
 // =======================================================================
-void GenerateCornerRays (const GLdouble theInvModelProj[16],
-                         cl_float       theOrigins[16],
-                         cl_float       theDirects[16])
+void OpenGl_Workspace::UpdateCamera (const NCollection_Mat4<GLdouble>& theOrientation,
+                                     const NCollection_Mat4<GLdouble>& theViewMapping,
+                                     OpenGl_Vec3                       theOrigins[4],
+                                     OpenGl_Vec3                       theDirects[4])
 {
+  NCollection_Mat4<GLdouble> aInvModelProj;
+
+  // compute invserse model-view-projection matrix
+  (theViewMapping * theOrientation).Inverted (aInvModelProj);
+
   Standard_Integer aOriginIndex = 0;
   Standard_Integer aDirectIndex = 0;
 
-  for (Standard_Integer y = -1; y <= 1; y += 2)
+  for (Standard_Integer aY = -1; aY <= 1; aY += 2)
   {
-    for (Standard_Integer x = -1; x <= 1; x += 2)
+    for (Standard_Integer aX = -1; aX <= 1; aX += 2)
     {
-      BVH_Vec4f aOrigin (float(x),
-                         float(y),
-                         -1.f,
-                         1.f);
+      OpenGl_Vec4d aOrigin (GLdouble(aX),
+                            GLdouble(aY),
+                           -1.0,
+                            1.0);
+
+      aOrigin = aInvModelProj * aOrigin;
 
-      aOrigin = MatVecMult (theInvModelProj, aOrigin);
       aOrigin.x() = aOrigin.x() / aOrigin.w();
       aOrigin.y() = aOrigin.y() / aOrigin.w();
       aOrigin.z() = aOrigin.z() / aOrigin.w();
-      aOrigin.w() = 1.f;
 
-      BVH_Vec4f aDirect (float(x),
-                         float(y),
-                         1.f,
-                         1.f);
+      OpenGl_Vec4d aDirect (GLdouble(aX),
+                            GLdouble(aY),
+                            1.0,
+                            1.0);
+
+      aDirect = aInvModelProj * aDirect;
 
-      aDirect = MatVecMult (theInvModelProj, aDirect);
       aDirect.x() = aDirect.x() / aDirect.w();
       aDirect.y() = aDirect.y() / aDirect.w();
       aDirect.z() = aDirect.z() / aDirect.w();
-      aDirect.w() = 1.f;
 
       aDirect = aDirect - aOrigin;
 
-      GLdouble aInvLen = 1.f / sqrt (aDirect.x() * aDirect.x() +
+      GLdouble aInvLen = 1.0 / sqrt (aDirect.x() * aDirect.x() +
                                      aDirect.y() * aDirect.y() +
                                      aDirect.z() * aDirect.z());
 
-      theOrigins [aOriginIndex++] = static_cast<GLfloat> (aOrigin.x());
-      theOrigins [aOriginIndex++] = static_cast<GLfloat> (aOrigin.y());
-      theOrigins [aOriginIndex++] = static_cast<GLfloat> (aOrigin.z());
-      theOrigins [aOriginIndex++] = 1.f;
+      theOrigins[aOriginIndex++] = OpenGl_Vec3 (static_cast<GLfloat> (aOrigin.x()),
+                                                static_cast<GLfloat> (aOrigin.y()),
+                                                static_cast<GLfloat> (aOrigin.z()));
+
+      theDirects[aDirectIndex++] = OpenGl_Vec3 (static_cast<GLfloat> (aDirect.x() * aInvLen),
+                                                static_cast<GLfloat> (aDirect.y() * aInvLen),
+                                                static_cast<GLfloat> (aDirect.z() * aInvLen));
+    }
+  }
+}
+
+// =======================================================================
+// function : RunRaytraceShaders
+// purpose  : Runs ray-tracing shader programs
+// =======================================================================
+Standard_Boolean OpenGl_Workspace::RunRaytraceShaders (const Graphic3d_CView& theCView,
+                                                       const Standard_Integer theSizeX,
+                                                       const Standard_Integer theSizeY,
+                                                       const OpenGl_Vec3      theOrigins[4],
+                                                       const OpenGl_Vec3      theDirects[4],
+                                                       OpenGl_FrameBuffer*    theFrameBuffer)
+{
+  mySceneMinPointTexture->BindTexture (myGlContext, GL_TEXTURE0 + OpenGl_RT_SceneMinPointTexture);
+  mySceneMaxPointTexture->BindTexture (myGlContext, GL_TEXTURE0 + OpenGl_RT_SceneMaxPointTexture);
+  mySceneNodeInfoTexture->BindTexture (myGlContext, GL_TEXTURE0 + OpenGl_RT_SceneNodeInfoTexture);
+  myObjectMinPointTexture->BindTexture (myGlContext, GL_TEXTURE0 + OpenGl_RT_ObjectMinPointTexture);
+  myObjectMaxPointTexture->BindTexture (myGlContext, GL_TEXTURE0 + OpenGl_RT_ObjectMaxPointTexture);
+  myObjectNodeInfoTexture->BindTexture (myGlContext, GL_TEXTURE0 + OpenGl_RT_ObjectNodeInfoTexture);
+  myGeometryVertexTexture->BindTexture (myGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryVertexTexture);
+  myGeometryNormalTexture->BindTexture (myGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryNormalTexture);
+  myGeometryTriangTexture->BindTexture (myGlContext, GL_TEXTURE0 + OpenGl_RT_GeometryTriangTexture);
+  myRaytraceMaterialTexture->BindTexture (myGlContext, GL_TEXTURE0 + OpenGl_RT_RaytraceMaterialTexture);
+  myRaytraceLightSrcTexture->BindTexture (myGlContext, GL_TEXTURE0 + OpenGl_RT_RaytraceLightSrcTexture);
+
+  if (theCView.IsAntialiasingEnabled) // render source image to FBO
+  {
+    myRaytraceFBO1->BindBuffer (myGlContext);
+    
+    glDisable (GL_BLEND);
+  }
+
+  myRaytraceProgram->Bind (myGlContext);
+
+  Standard_Integer aLightSourceBufferSize =
+    static_cast<Standard_Integer> (myRaytraceGeometry.Sources.size());
+
+  myRaytraceProgram->SetUniform (myGlContext,
+    myUniformLocations[0][OpenGl_RT_uOriginLB], theOrigins[0]);
+  myRaytraceProgram->SetUniform (myGlContext,
+    myUniformLocations[0][OpenGl_RT_uOriginRB], theOrigins[1]);
+  myRaytraceProgram->SetUniform (myGlContext,
+    myUniformLocations[0][OpenGl_RT_uOriginLT], theOrigins[2]);
+  myRaytraceProgram->SetUniform (myGlContext,
+    myUniformLocations[0][OpenGl_RT_uOriginRT], theOrigins[3]);
+  myRaytraceProgram->SetUniform (myGlContext,
+    myUniformLocations[0][OpenGl_RT_uDirectLB], theDirects[0]);
+  myRaytraceProgram->SetUniform (myGlContext,
+    myUniformLocations[0][OpenGl_RT_uDirectRB], theDirects[1]);
+  myRaytraceProgram->SetUniform (myGlContext,
+    myUniformLocations[0][OpenGl_RT_uDirectLT], theDirects[2]);
+  myRaytraceProgram->SetUniform (myGlContext,
+    myUniformLocations[0][OpenGl_RT_uDirectRT], theDirects[3]);
+  myRaytraceProgram->SetUniform (myGlContext,
+    myUniformLocations[0][OpenGl_RT_uSceneRad], myRaytraceSceneRadius);
+  myRaytraceProgram->SetUniform (myGlContext,
+    myUniformLocations[0][OpenGl_RT_uSceneEps], myRaytraceSceneEpsilon);
+  myRaytraceProgram->SetUniform (myGlContext,
+    myUniformLocations[0][OpenGl_RT_uLightCount], aLightSourceBufferSize);
+  myRaytraceProgram->SetUniform (myGlContext,
+    myUniformLocations[0][OpenGl_RT_uLightAmbnt], myRaytraceGeometry.Ambient);
+  myRaytraceProgram->SetUniform (myGlContext,
+    myUniformLocations[0][OpenGl_RT_uShadEnabled], theCView.IsShadowsEnabled);
+  myRaytraceProgram->SetUniform (myGlContext,
+    myUniformLocations[0][OpenGl_RT_uReflEnabled], theCView.IsReflectionsEnabled);
+
+  myGlContext->core20fwd->glEnableVertexAttribArray (
+    myUniformLocations[0][OpenGl_RT_aPosition]);
+  {
+    myGlContext->core20fwd->glVertexAttribPointer (
+      myUniformLocations[0][OpenGl_RT_aPosition], 3, GL_FLOAT, GL_FALSE, 0, NULL);
+
+    myGlContext->core15fwd->glDrawArrays (GL_TRIANGLES, 0, 6);
+  }
+  myGlContext->core20fwd->glDisableVertexAttribArray (
+    myUniformLocations[0][OpenGl_RT_aPosition]);
+  
+  if (!theCView.IsAntialiasingEnabled)
+  {
+    myRaytraceProgram->Unbind (myGlContext);
+
+    return Standard_True;
+  }
+
+  myGlContext->core20fwd->glActiveTexture (
+    GL_TEXTURE0 + OpenGl_RT_FSAAInputTexture); // texture unit for FBO texture
+
+  myRaytraceFBO1->BindTexture (myGlContext);
+
+  myPostFSAAProgram->Bind (myGlContext);
+
+  myPostFSAAProgram->SetUniform (myGlContext,
+    myUniformLocations[1][OpenGl_RT_uOriginLB], theOrigins[0]);
+  myPostFSAAProgram->SetUniform (myGlContext,
+    myUniformLocations[1][OpenGl_RT_uOriginRB], theOrigins[1]);
+  myPostFSAAProgram->SetUniform (myGlContext,
+    myUniformLocations[1][OpenGl_RT_uOriginLT], theOrigins[2]);
+  myPostFSAAProgram->SetUniform (myGlContext,
+    myUniformLocations[1][OpenGl_RT_uOriginRT], theOrigins[3]);
+  myPostFSAAProgram->SetUniform (myGlContext,
+    myUniformLocations[1][OpenGl_RT_uDirectLB], theDirects[0]);
+  myPostFSAAProgram->SetUniform (myGlContext,
+    myUniformLocations[1][OpenGl_RT_uDirectRB], theDirects[1]);
+  myPostFSAAProgram->SetUniform (myGlContext,
+    myUniformLocations[1][OpenGl_RT_uDirectLT], theDirects[2]);
+  myPostFSAAProgram->SetUniform (myGlContext,
+    myUniformLocations[1][OpenGl_RT_uDirectRT], theDirects[3]);
+  myPostFSAAProgram->SetUniform (myGlContext,
+    myUniformLocations[1][OpenGl_RT_uSceneRad], myRaytraceSceneRadius);
+  myPostFSAAProgram->SetUniform (myGlContext,
+    myUniformLocations[1][OpenGl_RT_uSceneEps], myRaytraceSceneEpsilon);
+  myPostFSAAProgram->SetUniform (myGlContext,
+    myUniformLocations[1][OpenGl_RT_uLightCount], aLightSourceBufferSize);
+  myPostFSAAProgram->SetUniform (myGlContext,
+    myUniformLocations[1][OpenGl_RT_uLightAmbnt], myRaytraceGeometry.Ambient);
+  myPostFSAAProgram->SetUniform (myGlContext,
+    myUniformLocations[1][OpenGl_RT_uShadEnabled], theCView.IsShadowsEnabled);
+  myPostFSAAProgram->SetUniform (myGlContext,
+    myUniformLocations[1][OpenGl_RT_uReflEnabled], theCView.IsReflectionsEnabled);
+
+  const Standard_ShortReal aMaxOffset = 0.559017f;
+  const Standard_ShortReal aMinOffset = 0.186339f;
+
+  myGlContext->core20fwd->glEnableVertexAttribArray (
+    myUniformLocations[1][OpenGl_RT_aPosition]);
+  
+  myGlContext->core20fwd->glVertexAttribPointer (
+    myUniformLocations[1][OpenGl_RT_aPosition], 3, GL_FLOAT, GL_FALSE, 0, NULL);
+
+  // Perform multi-pass adaptive FSAA using ping-pong technique
+  for (Standard_Integer anIt = 0; anIt < 4; ++anIt)
+  {
+    GLfloat aOffsetX = 1.f / theSizeX;
+    GLfloat aOffsetY = 1.f / theSizeY;
+
+    if (anIt < 2)
+    {
+      aOffsetX *= anIt < 1 ? aMinOffset : -aMaxOffset;
+      aOffsetY *= anIt < 1 ? aMaxOffset :  aMinOffset;
+    }
+    else
+    {
+      aOffsetX *= anIt > 2 ?  aMaxOffset : -aMinOffset;
+      aOffsetY *= anIt > 2 ? -aMinOffset : -aMaxOffset;
+    }
+    
+    myPostFSAAProgram->SetUniform (myGlContext,
+      myUniformLocations[1][OpenGl_RT_uSamples], anIt + 2);
+    myPostFSAAProgram->SetUniform (myGlContext,
+      myUniformLocations[1][OpenGl_RT_uOffsetX], aOffsetX);
+    myPostFSAAProgram->SetUniform (myGlContext,
+      myUniformLocations[1][OpenGl_RT_uOffsetY], aOffsetY);
+
+    Handle(OpenGl_FrameBuffer)& aFramebuffer = anIt % 2 ? myRaytraceFBO1 : myRaytraceFBO2;
+
+    if (anIt == 3) // disable FBO on last iteration
+    {
+      glEnable (GL_BLEND);
+
+      if (theFrameBuffer != NULL)
+        theFrameBuffer->BindBuffer (myGlContext);
+    }
+    else
+    {
+      aFramebuffer->BindBuffer (myGlContext);
+    }
+    
+    myGlContext->core15fwd->glDrawArrays (GL_TRIANGLES, 0, 6);
 
-      theDirects [aDirectIndex++] = static_cast<GLfloat> (aDirect.x() * aInvLen);
-      theDirects [aDirectIndex++] = static_cast<GLfloat> (aDirect.y() * aInvLen);
-      theDirects [aDirectIndex++] = static_cast<GLfloat> (aDirect.z() * aInvLen);
-      theDirects [aDirectIndex++] = 0.f;
+    if (anIt != 3) // set input for the next pass
+    {
+      aFramebuffer->BindTexture  (myGlContext);
+      aFramebuffer->UnbindBuffer (myGlContext);
     }
   }
+
+  myGlContext->core20fwd->glDisableVertexAttribArray (
+    myUniformLocations[1][OpenGl_RT_aPosition]);
+
+  myPostFSAAProgram->Unbind (myGlContext);
+
+  return Standard_True;
 }
 
 // =======================================================================
 // function : Raytrace
-// purpose  : Redraws the window using OpenCL ray tracing
+// purpose  : Redraws the window using OpenGL/GLSL ray-tracing
 // =======================================================================
 Standard_Boolean OpenGl_Workspace::Raytrace (const Graphic3d_CView& theCView,
                                              const Standard_Integer theSizeX,
                                              const Standard_Integer theSizeY,
-                                             const Tint             theToSwap)
+                                             const Standard_Boolean theToSwap,
+                                             OpenGl_FrameBuffer*    theFrameBuffer)
 {
-  if (!InitOpenCL())
+  if (!UpdateRaytraceGeometry (Standard_True))
     return Standard_False;
 
-  if (!ResizeRaytraceOutputBuffer (theSizeX, theSizeY))
+  if (!InitRaytraceResources())
     return Standard_False;
 
-  if (!UpdateRaytraceEnvironmentMap())
+  if (!ResizeRaytraceBuffers (theSizeX, theSizeY))
     return Standard_False;
 
-  if (!UpdateRaytraceGeometry (Standard_True))
+  if (!UpdateRaytraceEnvironmentMap())
     return Standard_False;
 
   // Get model-view and projection matrices
@@ -2111,62 +1976,31 @@ Standard_Boolean OpenGl_Workspace::Raytrace (const Graphic3d_CView& theCView,
 
   myView->GetMatrices (theOrientation, theViewMapping);
 
-  GLdouble aOrientationMatrix[16];
-  GLdouble aViewMappingMatrix[16];
-  GLdouble aOrientationInvers[16];
+  NCollection_Mat4<GLdouble> aOrientationMatrix;
+  NCollection_Mat4<GLdouble> aViewMappingMatrix;
 
   for (Standard_Integer j = 0; j < 4; ++j)
+  {
     for (Standard_Integer i = 0; i < 4; ++i)
     {
       aOrientationMatrix [4 * j + i] = theOrientation (i, j);
       aViewMappingMatrix [4 * j + i] = theViewMapping (i, j);
     }
+  }
+  
+  NCollection_Mat4<GLdouble> aInvOrientationMatrix;
+  aOrientationMatrix.Inverted (aInvOrientationMatrix);
 
-  ComputeInverseMatrix (aOrientationMatrix, aOrientationInvers);
-
-  if (!UpdateRaytraceLightSources (aOrientationInvers))
+  if (!UpdateRaytraceLightSources (aInvOrientationMatrix))
     return Standard_False;
 
-  // Generate primary rays for corners of the screen quad
-  glMatrixMode (GL_MODELVIEW);
-
-  glLoadMatrixd (aViewMappingMatrix);
-  glMultMatrixd (aOrientationMatrix);
-
-  GLdouble aModelProject[16];
-  GLdouble aInvModelProj[16];
-
-  glGetDoublev (GL_MODELVIEW_MATRIX, aModelProject);
-
-  ComputeInverseMatrix (aModelProject, aInvModelProj);
+  OpenGl_Vec3 aOrigins[4];
+  OpenGl_Vec3 aDirects[4];
 
-  GLfloat aOrigins[16];
-  GLfloat aDirects[16];
-
-  GenerateCornerRays (aInvModelProj,
-                      aOrigins,
-                      aDirects);
-
-  // Compute ray-traced image using OpenCL kernel
-  cl_mem anImages[] = { myRaytraceOutputImage, myRaytraceOutputImageAA };
-  cl_int anError = clEnqueueAcquireGLObjects (myComputeQueue,
-                                              2, anImages,
-                                              0, NULL, NULL);
-  clFinish (myComputeQueue);
-
-  if (myIsRaytraceDataValid)
-  {
-    RunRaytraceOpenCLKernels (theCView,
-                              aOrigins,
-                              aDirects,
-                              theSizeX,
-                              theSizeY);
-  }
-
-  anError |= clEnqueueReleaseGLObjects (myComputeQueue,
-                                        2, anImages,
-                                        0, NULL, NULL);
-  clFinish (myComputeQueue);
+  UpdateCamera (aOrientationMatrix,
+                aViewMappingMatrix,
+                aOrigins,
+                aDirects);
 
   // Draw background
   glPushAttrib (GL_ENABLE_BIT |
@@ -2190,44 +2024,38 @@ Standard_Boolean OpenGl_Workspace::Raytrace (const Graphic3d_CView& theCView,
 
   glClear (GL_COLOR_BUFFER_BIT);
 
-  Handle(OpenGl_Workspace) aWorkspace (this);
-  myView->DrawBackground (aWorkspace);
-
-  // Draw dummy quad to show result image
-  glEnable (GL_COLOR_MATERIAL);
-  glEnable (GL_BLEND);
+  if (theFrameBuffer != NULL)
+    theFrameBuffer->BindBuffer (myGlContext);
 
-  glDisable (GL_DEPTH_TEST);
-
-  glBlendFunc (GL_ONE, GL_SRC_ALPHA);
-
-  glEnable (GL_TEXTURE_RECTANGLE);
+  myView->DrawBackground (*this);
 
+  // Generate ray-traced image
   glMatrixMode (GL_PROJECTION);
   glLoadIdentity();
 
   glMatrixMode (GL_MODELVIEW);
   glLoadIdentity();
 
-  glColor3f (1.0f, 1.0f, 1.0f);
-
-  if (!theCView.IsAntialiasingEnabled)
-    myRaytraceOutputTexture->Bind (myGlContext);
-  else
-    myRaytraceOutputTextureAA->Bind (myGlContext);
+  glEnable (GL_BLEND);
+  glBlendFunc (GL_ONE, GL_SRC_ALPHA);
 
   if (myIsRaytraceDataValid)
   {
-    glBegin (GL_QUADS);
-    {
-      glTexCoord2i (       0,        0);   glVertex2f (-1.f, -1.f);
-      glTexCoord2i (       0, theSizeY);   glVertex2f (-1.f,  1.f);
-      glTexCoord2i (theSizeX, theSizeY);   glVertex2f ( 1.f,  1.f);
-      glTexCoord2i (theSizeX,        0);   glVertex2f ( 1.f, -1.f);
-    }
-    glEnd();
+    myRaytraceScreenQuad.Bind (myGlContext);
+
+    RunRaytraceShaders (theCView,
+                        theSizeX,
+                        theSizeY,
+                        aOrigins,
+                        aDirects,
+                        theFrameBuffer);
+
+    myRaytraceScreenQuad.Unbind (myGlContext);
   }
 
+  if (theFrameBuffer != NULL)
+    theFrameBuffer->UnbindBuffer (myGlContext);
+
   glPopAttrib();
 
   // Swap the buffers
@@ -2237,9 +2065,9 @@ Standard_Boolean OpenGl_Workspace::Raytrace (const Graphic3d_CView& theCView,
     myBackBufferRestored = Standard_False;
   }
   else
+  {
     glFlush();
+  }
 
   return Standard_True;
 }
-
-#endif