0027925: Visualization - implement order-independent transparency algorithm within...
[occt.git] / src / OpenGl / OpenGl_Workspace.cxx
index 3ee7a41..7ec727d 100644 (file)
@@ -15,8 +15,6 @@
 
 #include <OpenGl_Workspace.hxx>
 
-#include <InterfaceGraphic.hxx>
-
 #include <OpenGl_ArbFBO.hxx>
 #include <OpenGl_AspectLine.hxx>
 #include <OpenGl_AspectFace.hxx>
 #include <OpenGl_SceneGeometry.hxx>
 #include <OpenGl_Structure.hxx>
 #include <OpenGl_Sampler.hxx>
+#include <OpenGl_ShaderManager.hxx>
 #include <OpenGl_Texture.hxx>
 #include <OpenGl_View.hxx>
 #include <OpenGl_Window.hxx>
 
 #include <Graphic3d_TextureParams.hxx>
 #include <Graphic3d_TransformUtils.hxx>
+#include <NCollection_AlignedAllocator.hxx>
 
 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Workspace,Standard_Transient)
 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_RaytraceFilter,OpenGl_RenderFilter)
@@ -75,13 +75,13 @@ namespace
 void OpenGl_Material::Init (const Graphic3d_MaterialAspect& theMat,
                             const Quantity_Color&           theInteriorColor)
 {
-  const bool isPhysic = theMat.MaterialType (Graphic3d_MATERIAL_PHYSIC) == Standard_True;
+  const bool isPhysic = theMat.MaterialType (Graphic3d_MATERIAL_PHYSIC);
 
   // ambient component
   if (theMat.ReflectionMode (Graphic3d_TOR_AMBIENT))
   {
     const OpenGl_Vec3& aSrcAmb = isPhysic ? theMat.AmbientColor() : theInteriorColor;
-    Ambient = OpenGl_Vec4 (aSrcAmb * (float )theMat.Ambient(), 1.0f);
+    Ambient = OpenGl_Vec4 (aSrcAmb * theMat.Ambient(), 1.0f);
   }
   else
   {
@@ -92,7 +92,7 @@ void OpenGl_Material::Init (const Graphic3d_MaterialAspect& theMat,
   if (theMat.ReflectionMode (Graphic3d_TOR_DIFFUSE))
   {
     const OpenGl_Vec3& aSrcDif = isPhysic ? theMat.DiffuseColor() : theInteriorColor;
-    Diffuse = OpenGl_Vec4 (aSrcDif * (float )theMat.Diffuse(), 1.0f);
+    Diffuse = OpenGl_Vec4 (aSrcDif * theMat.Diffuse(), 1.0f);
   }
   else
   {
@@ -103,7 +103,7 @@ void OpenGl_Material::Init (const Graphic3d_MaterialAspect& theMat,
   if (theMat.ReflectionMode (Graphic3d_TOR_SPECULAR))
   {
     const OpenGl_Vec3& aSrcSpe = isPhysic ? (const OpenGl_Vec3& )theMat.SpecularColor() : THE_WHITE_COLOR.rgb();
-    Specular = OpenGl_Vec4 (aSrcSpe * (float )theMat.Specular(), 1.0f);
+    Specular = OpenGl_Vec4 (aSrcSpe * theMat.Specular(), 1.0f);
   }
   else
   {
@@ -114,15 +114,15 @@ void OpenGl_Material::Init (const Graphic3d_MaterialAspect& theMat,
   if (theMat.ReflectionMode (Graphic3d_TOR_EMISSION))
   {
     const OpenGl_Vec3& aSrcEms = isPhysic ? theMat.EmissiveColor() : theInteriorColor;
-    Emission = OpenGl_Vec4 (aSrcEms * (float )theMat.Emissive(), 1.0f);
+    Emission = OpenGl_Vec4 (aSrcEms * theMat.Emissive(), 1.0f);
   }
   else
   {
     Emission = THE_BLACK_COLOR;
   }
 
-  ChangeShine()        = 128.0f * float(theMat.Shininess());
-  ChangeTransparency() = 1.0f - (float )theMat.Transparency();
+  ChangeShine()        = 128.0f * theMat.Shininess();
+  ChangeTransparency() = 1.0f - theMat.Transparency();
 }
 
 // =======================================================================
@@ -130,41 +130,27 @@ void OpenGl_Material::Init (const Graphic3d_MaterialAspect& theMat,
 // purpose  :
 // =======================================================================
 OpenGl_Workspace::OpenGl_Workspace (OpenGl_View* theView, const Handle(OpenGl_Window)& theWindow)
-: NamedStatus (0),
-  HighlightColor (&THE_WHITE_COLOR),
-  myView (theView),
+: myView (theView),
   myWindow (theWindow),
   myGlContext (!theWindow.IsNull() ? theWindow->GetGlContext() : NULL),
   myUseZBuffer    (Standard_True),
   myUseDepthWrite (Standard_True),
-  myUseGLLight (Standard_True),
   //
   myAspectLineSet (&myDefaultAspectLine),
   myAspectFaceSet (&myDefaultAspectFace),
   myAspectMarkerSet (&myDefaultAspectMarker),
   myAspectTextSet (&myDefaultAspectText),
-  myAspectFaceAppliedWithHL (false),
   //
   ViewMatrix_applied (&myDefaultMatrix),
   StructureMatrix_applied (&myDefaultMatrix),
   myToAllowFaceCulling (false),
-  myToHighlight (false),
   myModelViewMatrix (myDefaultMatrix)
 {
   if (!myGlContext.IsNull() && myGlContext->MakeCurrent())
   {
     myGlContext->core11fwd->glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
 
-    if (!myGlContext->GetResource ("OpenGl_LineAttributes", myLineAttribs))
-    {
-      // share and register for release once the resource is no longer used
-      myLineAttribs = new OpenGl_LineAttributes();
-      myGlContext->ShareResource ("OpenGl_LineAttributes", myLineAttribs);
-      myLineAttribs->Init (myGlContext);
-    }
-
     // General initialization of the context
-
   #if !defined(GL_ES_VERSION_2_0)
     if (myGlContext->core11 != NULL)
     {
@@ -190,19 +176,6 @@ OpenGl_Workspace::OpenGl_Workspace (OpenGl_View* theView, const Handle(OpenGl_Wi
   myFrontCulling.Aspect()->SetDrawEdges (false);
 }
 
-// =======================================================================
-// function : ~OpenGl_Workspace
-// purpose  :
-// =======================================================================
-OpenGl_Workspace::~OpenGl_Workspace()
-{
-  if (!myLineAttribs.IsNull())
-  {
-    myLineAttribs.Nullify();
-    myGlContext->ReleaseResource ("OpenGl_LineAttributes", Standard_True);
-  }
-}
-
 // =======================================================================
 // function : Activate
 // purpose  :
@@ -219,6 +192,13 @@ Standard_Boolean OpenGl_Workspace::Activate()
 
   ResetAppliedAspect();
 
+  // reset state for safety
+  myGlContext->BindProgram (Handle(OpenGl_ShaderProgram)());
+  if (myGlContext->core20fwd != NULL)
+  {
+    myGlContext->core20fwd->glUseProgram (OpenGl_ShaderProgram::NO_PROGRAM);
+  }
+  myGlContext->ShaderManager()->PushState (Handle(OpenGl_ShaderProgram)());
   return Standard_True;
 }
 
@@ -230,8 +210,7 @@ void OpenGl_Workspace::ResetAppliedAspect()
 {
   myGlContext->BindDefaultVao();
 
-  NamedStatus           = !myTextureBound.IsNull() ? OPENGL_NS_TEXTURE : 0;
-  HighlightColor        = &THE_WHITE_COLOR;
+  myHighlightStyle.Nullify();
   myToAllowFaceCulling  = false;
   myAspectLineSet       = &myDefaultAspectLine;
   myAspectFaceSet       = &myDefaultAspectFace;
@@ -589,166 +568,6 @@ Handle(OpenGl_Texture) OpenGl_Workspace::EnableTexture (const Handle(OpenGl_Text
   return aPrevTexture;
 }
 
-// =======================================================================
-// function : updateMaterial
-// purpose  :
-// =======================================================================
-void OpenGl_Workspace::updateMaterial (const int theFlag)
-{
-  // Case of hidden line
-  if (myAspectFaceSet->Aspect()->InteriorStyle() == Aspect_IS_HIDDENLINE)
-  {
-    // copy all values including line edge aspect
-    *myAspectFaceHl.Aspect().operator->() = *myAspectFaceSet->Aspect();
-    myAspectFaceHl.SetAspectEdge (myAspectFaceSet->AspectEdge());
-    myAspectFaceHl.Aspect()->SetInteriorColor (myView->BackgroundColor().GetRGB());
-    myAspectFaceHl.SetNoLighting (true);
-    myAspectFaceSet = &myAspectFaceHl;
-    return;
-  }
-
-  const Graphic3d_MaterialAspect* aSrcMat      = &myAspectFaceSet->Aspect()->FrontMaterial();
-  const Quantity_Color*           aSrcIntColor = &myAspectFaceSet->Aspect()->InteriorColor();
-  GLenum aFace = GL_FRONT_AND_BACK;
-  if (theFlag == TEL_BACK_MATERIAL)
-  {
-    aFace        = GL_BACK;
-    aSrcMat      = &myAspectFaceSet->Aspect()->BackMaterial();
-    aSrcIntColor = &myAspectFaceSet->Aspect()->BackInteriorColor();
-  }
-  else if (myAspectFaceSet->Aspect()->Distinguish()
-        && !(NamedStatus & OPENGL_NS_RESMAT))
-  {
-    aFace = GL_FRONT;
-  }
-
-  myMatTmp.Init (*aSrcMat, *aSrcIntColor);
-  if (myToHighlight)
-  {
-    myMatTmp.SetColor (*HighlightColor);
-  }
-
-  // handling transparency
-  if (NamedStatus & OPENGL_NS_2NDPASSDO)
-  {
-    // second pass
-    myMatTmp.Diffuse.a() = aSrcMat->EnvReflexion();
-  }
-  else
-  {
-    if (aSrcMat->EnvReflexion() != 0.0f)
-    {
-      // if the material reflects the environment scene, the second pass is needed
-      NamedStatus |= OPENGL_NS_2NDPASSNEED;
-    }
-
-    const float aTransp = (float )aSrcMat->Transparency();
-    if (aTransp != 0.0f)
-    {
-      // render transparent
-      myMatTmp.Diffuse.a() = 1.0f - aTransp;
-      glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-      glEnable    (GL_BLEND);
-      if (myUseDepthWrite)
-      {
-        glDepthMask (GL_FALSE);
-      }
-    }
-    else
-    {
-      // render opaque
-      if ((NamedStatus & OPENGL_NS_ANTIALIASING) == 0)
-      {
-        glBlendFunc (GL_ONE, GL_ZERO);
-        glDisable   (GL_BLEND);
-      }
-      if (myUseDepthWrite)
-      {
-        glDepthMask (GL_TRUE);
-      }
-    }
-  }
-
-  // do not update material properties in case of zero reflection mode,
-  // because GL lighting will be disabled by OpenGl_PrimitiveArray::DrawArray() anyway.
-  if (myAspectFaceSet->IsNoLighting())
-  {
-    return;
-  }
-
-  // reset material
-  if (NamedStatus & OPENGL_NS_RESMAT)
-  {
-  #if !defined(GL_ES_VERSION_2_0)
-    if (myGlContext->core11 != NULL)
-    {
-      myGlContext->core11->glMaterialfv (aFace, GL_AMBIENT,   myMatTmp.Ambient.GetData());
-      myGlContext->core11->glMaterialfv (aFace, GL_DIFFUSE,   myMatTmp.Diffuse.GetData());
-      myGlContext->core11->glMaterialfv (aFace, GL_SPECULAR,  myMatTmp.Specular.GetData());
-      myGlContext->core11->glMaterialfv (aFace, GL_EMISSION,  myMatTmp.Emission.GetData());
-      myGlContext->core11->glMaterialf  (aFace, GL_SHININESS, myMatTmp.Shine());
-    }
-  #endif
-
-    if (theFlag == TEL_FRONT_MATERIAL)
-    {
-      myMatFront = myMatTmp;
-      myMatBack  = myMatTmp;
-    }
-    else
-    {
-      myMatBack = myMatTmp;
-    }
-
-    NamedStatus &= ~OPENGL_NS_RESMAT;
-    return;
-  }
-
-  // reduce updates
-  OpenGl_Material& anOld = (theFlag == TEL_FRONT_MATERIAL)
-                         ? myMatFront
-                         : myMatBack;
-#if !defined(GL_ES_VERSION_2_0)
-  if (myGlContext->core11 != NULL)
-  {
-    if (myMatTmp.Ambient.r() != anOld.Ambient.r()
-     || myMatTmp.Ambient.g() != anOld.Ambient.g()
-     || myMatTmp.Ambient.b() != anOld.Ambient.b())
-    {
-      myGlContext->core11->glMaterialfv (aFace, GL_AMBIENT, myMatTmp.Ambient.GetData());
-    }
-    if (myMatTmp.Diffuse.r() != anOld.Diffuse.r()
-     || myMatTmp.Diffuse.g() != anOld.Diffuse.g()
-     || myMatTmp.Diffuse.b() != anOld.Diffuse.b()
-     || fabs (myMatTmp.Diffuse.a() - anOld.Diffuse.a()) > 0.01f)
-    {
-      myGlContext->core11->glMaterialfv (aFace, GL_DIFFUSE, myMatTmp.Diffuse.GetData());
-    }
-    if (myMatTmp.Specular.r() != anOld.Specular.r()
-     || myMatTmp.Specular.g() != anOld.Specular.g()
-     || myMatTmp.Specular.b() != anOld.Specular.b())
-    {
-      myGlContext->core11->glMaterialfv (aFace, GL_SPECULAR, myMatTmp.Specular.GetData());
-    }
-    if (myMatTmp.Emission.r() != anOld.Emission.r()
-     || myMatTmp.Emission.g() != anOld.Emission.g()
-     || myMatTmp.Emission.b() != anOld.Emission.b())
-    {
-      myGlContext->core11->glMaterialfv (aFace, GL_EMISSION, myMatTmp.Emission.GetData());
-    }
-    if (myMatTmp.Shine() != anOld.Shine())
-    {
-      myGlContext->core11->glMaterialf (aFace, GL_SHININESS, myMatTmp.Shine());
-    }
-  }
-#endif
-  anOld = myMatTmp;
-  if (aFace == GL_FRONT_AND_BACK)
-  {
-    myMatBack = myMatTmp;
-  }
-}
-
 // =======================================================================
 // function : SetAspectLine
 // purpose  :
@@ -814,8 +633,7 @@ const OpenGl_AspectFace* OpenGl_Workspace::ApplyAspectFace()
     }
     if (toSuppressBackFaces)
     {
-      if (!(NamedStatus & OPENGL_NS_2NDPASSDO)
-       && (float )myAspectFaceSet->Aspect()->FrontMaterial().Transparency() != 0.0f)
+      if ((float )myAspectFaceSet->Aspect()->FrontMaterial().Transparency() != 0.0f)
       {
         // disable culling in case of translucent shading aspect
         toSuppressBackFaces = false;
@@ -825,11 +643,11 @@ const OpenGl_AspectFace* OpenGl_Workspace::ApplyAspectFace()
   }
 
   if (myAspectFaceSet->Aspect() == myAspectFaceApplied
-   && myToHighlight == myAspectFaceAppliedWithHL)
+   && myHighlightStyle == myAspectFaceAppliedWithHL)
   {
     return myAspectFaceSet;
   }
-  myAspectFaceAppliedWithHL = myToHighlight;
+  myAspectFaceAppliedWithHL = myHighlightStyle;
 
 #if !defined(GL_ES_VERSION_2_0)
   const Aspect_InteriorStyle anIntstyle = myAspectFaceSet->Aspect()->InteriorStyle();
@@ -841,28 +659,25 @@ const OpenGl_AspectFace* OpenGl_Workspace::ApplyAspectFace()
       case Aspect_IS_EMPTY:
       case Aspect_IS_HOLLOW:
       {
-        glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
+        myGlContext->SetPolygonMode (GL_LINE);
         break;
       }
       case Aspect_IS_HATCH:
       {
-        glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
-        myLineAttribs->SetTypeOfHatch (!myAspectFaceApplied.IsNull() ? myAspectFaceApplied->HatchStyle() : Aspect_HS_SOLID);
+        myGlContext->SetPolygonMode (GL_FILL);
+        myGlContext->SetPolygonHatchEnabled (true);
         break;
       }
       case Aspect_IS_SOLID:
       case Aspect_IS_HIDDENLINE:
       {
-        glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
-        if (myGlContext->core11 != NULL)
-        {
-          glDisable (GL_POLYGON_STIPPLE);
-        }
+        myGlContext->SetPolygonMode (GL_FILL);
+        myGlContext->SetPolygonHatchEnabled (false);
         break;
       }
       case Aspect_IS_POINT:
       {
-        glPolygonMode (GL_FRONT_AND_BACK, GL_POINT);
+        myGlContext->SetPolygonMode (GL_POINT);
         break;
       }
     }
@@ -870,12 +685,7 @@ const OpenGl_AspectFace* OpenGl_Workspace::ApplyAspectFace()
 
   if (anIntstyle == Aspect_IS_HATCH)
   {
-    const Aspect_HatchStyle hatchstyle = myAspectFaceSet->Aspect()->HatchStyle();
-    if (myAspectFaceApplied.IsNull()
-     || myAspectFaceApplied->HatchStyle() != hatchstyle)
-    {
-      myLineAttribs->SetTypeOfHatch (hatchstyle);
-    }
+    myGlContext->SetPolygonHatchStyle (myAspectFaceSet->Aspect()->HatchStyle());
   }
 #endif
 
@@ -890,10 +700,19 @@ const OpenGl_AspectFace* OpenGl_Workspace::ApplyAspectFace()
     }
   }
 
-  updateMaterial (TEL_FRONT_MATERIAL);
-  if (myAspectFaceSet->Aspect()->Distinguish())
+  // Case of hidden line
+  if (myAspectFaceSet->Aspect()->InteriorStyle() == Aspect_IS_HIDDENLINE)
+  {
+    // copy all values including line edge aspect
+    *myAspectFaceHl.Aspect().operator->() = *myAspectFaceSet->Aspect();
+    myAspectFaceHl.SetAspectEdge (myAspectFaceSet->AspectEdge());
+    myAspectFaceHl.Aspect()->SetInteriorColor (myView->BackgroundColor().GetRGB());
+    myAspectFaceHl.SetNoLighting (true);
+    myAspectFaceSet = &myAspectFaceHl;
+  }
+  else
   {
-    updateMaterial (TEL_BACK_MATERIAL);
+    myGlContext->SetShadingMaterial (myAspectFaceSet, myHighlightStyle);
   }
 
   if (myAspectFaceSet->Aspect()->ToMapTexture())
@@ -998,24 +817,6 @@ Standard_Integer OpenGl_Workspace::Height() const
   return !myView->GlWindow().IsNull() ? myView->GlWindow()->Height() : 0;
 }
 
-// =======================================================================
-// function : UseGLLight
-// purpose  :
-// =======================================================================
-Standard_Boolean OpenGl_Workspace::UseGLLight() const
-{
-  return myView->IsGLLightEnabled();
-}
-
-// =======================================================================
-// function : AntiAliasingMode
-// purpose  :
-// =======================================================================
-Standard_Integer OpenGl_Workspace::AntiAliasingMode() const
-{
-  return myView->IsAntialiasingEnabled();
-}
-
 // =======================================================================
 // function : IsCullingEnabled
 // purpose  :
@@ -1066,76 +867,6 @@ void OpenGl_Workspace::FBORelease (Handle(OpenGl_FrameBuffer)& theFbo)
   theFbo.Nullify();
 }
 
-inline bool getDataFormat (const Image_PixMap& theData,
-                           GLenum&             thePixelFormat,
-                           GLenum&             theDataType)
-{
-  thePixelFormat = GL_RGB;
-  theDataType    = GL_UNSIGNED_BYTE;
-  switch (theData.Format())
-  {
-  #if !defined(GL_ES_VERSION_2_0)
-    case Image_PixMap::ImgGray:
-      thePixelFormat = GL_DEPTH_COMPONENT;
-      theDataType    = GL_UNSIGNED_BYTE;
-      return true;
-    case Image_PixMap::ImgGrayF:
-      thePixelFormat = GL_DEPTH_COMPONENT;
-      theDataType    = GL_FLOAT;
-      return true;
-    case Image_PixMap::ImgBGR:
-      thePixelFormat = GL_BGR;
-      theDataType    = GL_UNSIGNED_BYTE;
-      return true;
-    case Image_PixMap::ImgBGRA:
-    case Image_PixMap::ImgBGR32:
-      thePixelFormat = GL_BGRA;
-      theDataType    = GL_UNSIGNED_BYTE;
-      return true;
-    case Image_PixMap::ImgBGRF:
-      thePixelFormat = GL_BGR;
-      theDataType    = GL_FLOAT;
-      return true;
-    case Image_PixMap::ImgBGRAF:
-      thePixelFormat = GL_BGRA;
-      theDataType    = GL_FLOAT;
-      return true;
-  #else
-    case Image_PixMap::ImgGray:
-    case Image_PixMap::ImgGrayF:
-    case Image_PixMap::ImgBGR:
-    case Image_PixMap::ImgBGRA:
-    case Image_PixMap::ImgBGR32:
-    case Image_PixMap::ImgBGRF:
-    case Image_PixMap::ImgBGRAF:
-      return false;
-  #endif
-    case Image_PixMap::ImgRGB:
-      thePixelFormat = GL_RGB;
-      theDataType    = GL_UNSIGNED_BYTE;
-      return true;
-    case Image_PixMap::ImgRGBA:
-    case Image_PixMap::ImgRGB32:
-      thePixelFormat = GL_RGBA;
-      theDataType    = GL_UNSIGNED_BYTE;
-      return true;
-    case Image_PixMap::ImgRGBF:
-      thePixelFormat = GL_RGB;
-      theDataType    = GL_FLOAT;
-      return true;
-    case Image_PixMap::ImgRGBAF:
-      thePixelFormat = GL_RGBA;
-      theDataType    = GL_FLOAT;
-      return true;
-    case Image_PixMap::ImgAlpha:
-    case Image_PixMap::ImgAlphaF:
-      return false; // GL_ALPHA is no more supported in core context
-    case Image_PixMap::ImgUNKNOWN:
-      return false;
-  }
-  return false;
-}
-
 // =======================================================================
 // function : getAligned
 // purpose  :
@@ -1146,6 +877,21 @@ inline Standard_Size getAligned (const Standard_Size theNumber,
   return theNumber + theAlignment - 1 - (theNumber - 1) % theAlignment;
 }
 
+template<typename T>
+inline void convertRowFromRgba (T* theRgbRow,
+                                const Image_ColorRGBA* theRgbaRow,
+                                const Standard_Size theWidth)
+{
+  for (Standard_Size aCol = 0; aCol < theWidth; ++aCol)
+  {
+    const Image_ColorRGBA& anRgba = theRgbaRow[aCol];
+    T& anRgb = theRgbRow[aCol];
+    anRgb.r() = anRgba.r();
+    anRgb.g() = anRgba.g();
+    anRgb.b() = anRgba.b();
+  }
+}
+
 // =======================================================================
 // function : BufferDump
 // purpose  :
@@ -1154,13 +900,92 @@ Standard_Boolean OpenGl_Workspace::BufferDump (const Handle(OpenGl_FrameBuffer)&
                                                Image_PixMap&                     theImage,
                                                const Graphic3d_BufferType&       theBufferType)
 {
-  GLenum aFormat, aType;
   if (theImage.IsEmpty()
-   || !getDataFormat (theImage, aFormat, aType)
-   || !Activate())
+  || !Activate())
   {
     return Standard_False;
   }
+
+  GLenum aFormat = 0;
+  GLenum aType   = 0;
+  bool toSwapRgbaBgra = false;
+  bool toConvRgba2Rgb = false;
+  switch (theImage.Format())
+  {
+  #if !defined(GL_ES_VERSION_2_0)
+    case Image_Format_Gray:
+      aFormat = GL_DEPTH_COMPONENT;
+      aType   = GL_UNSIGNED_BYTE;
+      break;
+    case Image_Format_GrayF:
+      aFormat = GL_DEPTH_COMPONENT;
+      aType   = GL_FLOAT;
+      break;
+    case Image_Format_RGB:
+      aFormat = GL_RGB;
+      aType   = GL_UNSIGNED_BYTE;
+      break;
+    case Image_Format_BGR:
+      aFormat = GL_BGR;
+      aType   = GL_UNSIGNED_BYTE;
+      break;
+    case Image_Format_BGRA:
+    case Image_Format_BGR32:
+      aFormat = GL_BGRA;
+      aType   = GL_UNSIGNED_BYTE;
+      break;
+    case Image_Format_BGRF:
+      aFormat = GL_BGR;
+      aType   = GL_FLOAT;
+      break;
+    case Image_Format_BGRAF:
+      aFormat = GL_BGRA;
+      aType   = GL_FLOAT;
+      break;
+  #else
+    case Image_Format_Gray:
+    case Image_Format_GrayF:
+    case Image_Format_BGRF:
+    case Image_Format_BGRAF:
+      return Standard_False;
+    case Image_Format_BGRA:
+    case Image_Format_BGR32:
+      aFormat = GL_RGBA;
+      aType   = GL_UNSIGNED_BYTE;
+      toSwapRgbaBgra = true;
+      break;
+    case Image_Format_BGR:
+    case Image_Format_RGB:
+      aFormat = GL_RGBA;
+      aType   = GL_UNSIGNED_BYTE;
+      toConvRgba2Rgb = true;
+      break;
+  #endif
+    case Image_Format_RGBA:
+    case Image_Format_RGB32:
+      aFormat = GL_RGBA;
+      aType   = GL_UNSIGNED_BYTE;
+      break;
+    case Image_Format_RGBF:
+      aFormat = GL_RGB;
+      aType   = GL_FLOAT;
+      break;
+    case Image_Format_RGBAF:
+      aFormat = GL_RGBA;
+      aType   = GL_FLOAT;
+      break;
+    case Image_Format_Alpha:
+    case Image_Format_AlphaF:
+      return Standard_False; // GL_ALPHA is no more supported in core context
+    case Image_Format_UNKNOWN:
+      return Standard_False;
+  }
+
+  if (aFormat == 0)
+  {
+    return Standard_False;
+  }
+
 #if !defined(GL_ES_VERSION_2_0)
   GLint aReadBufferPrev = GL_BACK;
   if (theBufferType == Graphic3d_BT_Depth
@@ -1212,8 +1037,33 @@ Standard_Boolean OpenGl_Workspace::BufferDump (const Handle(OpenGl_FrameBuffer)&
     isBatchCopy = false;
   }
 #endif
+  if (toConvRgba2Rgb)
+  {
+    Handle(NCollection_BaseAllocator) anAlloc = new NCollection_AlignedAllocator (16);
+    const Standard_Size aRowSize = theImage.SizeX() * 4;
+    NCollection_Buffer aRowBuffer (anAlloc);
+    if (!aRowBuffer.Allocate (aRowSize))
+    {
+      return Standard_False;
+    }
 
-  if (!isBatchCopy)
+    for (Standard_Size aRow = 0; aRow < theImage.SizeY(); ++aRow)
+    {
+      // Image_PixMap rows indexation always starts from the upper corner
+      // while order in memory depends on the flag and processed by ChangeRow() method
+      glReadPixels (0, GLint(theImage.SizeY() - aRow - 1), GLsizei (theImage.SizeX()), 1, aFormat, aType, aRowBuffer.ChangeData());
+      const Image_ColorRGBA* aRowDataRgba = (const Image_ColorRGBA* )aRowBuffer.Data();
+      if (theImage.Format() == Image_Format_BGR)
+      {
+        convertRowFromRgba ((Image_ColorBGR* )theImage.ChangeRow (aRow), aRowDataRgba, theImage.SizeX());
+      }
+      else
+      {
+        convertRowFromRgba ((Image_ColorRGB* )theImage.ChangeRow (aRow), aRowDataRgba, theImage.SizeX());
+      }
+    }
+  }
+  else if (!isBatchCopy)
   {
     // copy row by row
     for (Standard_Size aRow = 0; aRow < theImage.SizeY(); ++aRow)
@@ -1227,6 +1077,7 @@ Standard_Boolean OpenGl_Workspace::BufferDump (const Handle(OpenGl_FrameBuffer)&
   {
     glReadPixels (0, 0, GLsizei (theImage.SizeX()), GLsizei (theImage.SizeY()), aFormat, aType, theImage.ChangeData());
   }
+  const bool hasErrors = myGlContext->ResetErrors (true);
 
   glPixelStorei (GL_PACK_ALIGNMENT,  1);
 #if !defined(GL_ES_VERSION_2_0)
@@ -1243,19 +1094,26 @@ Standard_Boolean OpenGl_Workspace::BufferDump (const Handle(OpenGl_FrameBuffer)&
     glReadBuffer (aReadBufferPrev);
   #endif
   }
-  return Standard_True;
+
+  if (toSwapRgbaBgra)
+  {
+    Image_PixMap::SwapRgbaBgra (theImage);
+  }
+
+  return !hasErrors;
 }
 
 // =======================================================================
-// function : CanRender
+// function : ShouldRender
 // purpose  :
 // =======================================================================
-Standard_Boolean OpenGl_RaytraceFilter::CanRender (const OpenGl_Element* theElement)
+Standard_Boolean OpenGl_RaytraceFilter::ShouldRender (const Handle(OpenGl_Workspace)& theWorkspace,
+                                                      const OpenGl_Element*           theElement)
 {
   Standard_Boolean aPrevFilterResult = Standard_True;
   if (!myPrevRenderFilter.IsNull())
   {
-    aPrevFilterResult = myPrevRenderFilter->CanRender (theElement);
+    aPrevFilterResult = myPrevRenderFilter->ShouldRender (theWorkspace, theElement);
   }
   return aPrevFilterResult &&
     !OpenGl_Raytrace::IsRaytracedElement (theElement);