diff --git a/src/OpenGl/OpenGl_View_Raytrace.cxx b/src/OpenGl/OpenGl_View_Raytrace.cxx
index ecc26ac7be..0c7dc0067f 100644
--- a/src/OpenGl/OpenGl_View_Raytrace.cxx
+++ b/src/OpenGl/OpenGl_View_Raytrace.cxx
@@ -2358,6 +2358,7 @@ Standard_Boolean OpenGl_View::runRaytraceShaders (const Standard_Integer
   bindRaytraceTextures (theGlContext);
 
   Handle(OpenGl_FrameBuffer) aRenderFramebuffer;
+  Handle(OpenGl_FrameBuffer) aDepthSourceFramebuffer;
   Handle(OpenGl_FrameBuffer) anAccumFramebuffer;
 
   // Choose proper set of framebuffers for stereo rendering
@@ -2369,6 +2370,7 @@ Standard_Boolean OpenGl_View::runRaytraceShaders (const Standard_Integer
   {
     aRenderFramebuffer = myAccumFrames % 2 ? myRaytraceFBO1[aFBOIdx] : myRaytraceFBO2[aFBOIdx];
     anAccumFramebuffer = myAccumFrames % 2 ? myRaytraceFBO2[aFBOIdx] : myRaytraceFBO1[aFBOIdx];
+    aDepthSourceFramebuffer = aRenderFramebuffer;
 
     anAccumFramebuffer->ColorTexture()->Bind (
       theGlContext, GL_TEXTURE0 + OpenGl_RT_PrevAccumTexture);
@@ -2407,36 +2409,10 @@ Standard_Boolean OpenGl_View::runRaytraceShaders (const Standard_Integer
 
   theGlContext->core20fwd->glDrawArrays (GL_TRIANGLES, 0, 6);
 
-  if (myRaytraceParameters.GlobalIllumination)
+  if (myRenderParams.IsAntialiasingEnabled)
   {
-    // Output accumulated image
-    theGlContext->BindProgram (myOutImageProgram);
+    glDepthMask (GL_FALSE);
 
-    if (theReadDrawFbo != NULL)
-    {
-      theReadDrawFbo->BindBuffer (theGlContext);
-    }
-    else
-    {
-      aRenderFramebuffer->UnbindBuffer (theGlContext);
-    }
-
-    aRenderFramebuffer->ColorTexture()->Bind (
-      theGlContext, GL_TEXTURE0 + OpenGl_RT_PrevAccumTexture);
-
-    aRenderFramebuffer->DepthStencilTexture()->Bind (
-      theGlContext, GL_TEXTURE0 + OpenGl_RT_DepthTexture);
-
-    theGlContext->core20fwd->glDrawArrays (GL_TRIANGLES, 0, 6);
-
-    aRenderFramebuffer->DepthStencilTexture()->Unbind (
-      theGlContext, GL_TEXTURE0 + OpenGl_RT_DepthTexture);
-
-    aRenderFramebuffer->ColorTexture()->Unbind (
-      theGlContext, GL_TEXTURE0 + OpenGl_RT_PrevAccumTexture);
-  }
-  else if (myRenderParams.IsAntialiasingEnabled)
-  {
     myRaytraceFBO1[aFBOIdx]->ColorTexture()->Bind (theGlContext, GL_TEXTURE0 + OpenGl_RT_FsaaInputTexture);
 
     aResult &= theGlContext->BindProgram (myPostFSAAProgram);
@@ -2482,29 +2458,48 @@ Standard_Boolean OpenGl_View::runRaytraceShaders (const Standard_Integer
 
       Handle(OpenGl_FrameBuffer)& aFramebuffer = anIt % 2 ? myRaytraceFBO2[aFBOIdx] : myRaytraceFBO1[aFBOIdx];
 
-      if (anIt == 3) // disable FBO on last iteration
-      {
-        if (theReadDrawFbo != NULL)
-        {
-          theReadDrawFbo->BindBuffer (theGlContext);
-        }
-        else
-        {
-          aFramebuffer->UnbindBuffer (theGlContext);
-        }
-      }
-      else
-      {
-        aFramebuffer->BindBuffer (theGlContext);
-      }
+      aFramebuffer->BindBuffer (theGlContext);
 
       theGlContext->core20fwd->glDrawArrays (GL_TRIANGLES, 0, 6);
 
-      if (anIt != 3) // set input for the next pass
-      {
-        aFramebuffer->ColorTexture()->Bind (theGlContext, GL_TEXTURE0 + OpenGl_RT_FsaaInputTexture);
-      }
+      aFramebuffer->ColorTexture()->Bind (theGlContext, GL_TEXTURE0 + OpenGl_RT_FsaaInputTexture);
     }
+
+    aRenderFramebuffer = myRaytraceFBO2[aFBOIdx];
+    aDepthSourceFramebuffer = myRaytraceFBO1[aFBOIdx];
+  }
+
+  if (myRaytraceParameters.GlobalIllumination || myRenderParams.IsAntialiasingEnabled)
+  {
+    // Output accumulated image
+    glDepthMask (GL_TRUE);
+
+    theGlContext->BindProgram (myOutImageProgram);
+
+    myOutImageProgram->SetUniform (theGlContext, "uApplyGamma", static_cast<Standard_Integer> (myRaytraceParameters.GlobalIllumination));
+
+    if (theReadDrawFbo != NULL)
+    {
+      theReadDrawFbo->BindBuffer (theGlContext);
+    }
+    else
+    {
+      aRenderFramebuffer->UnbindBuffer (theGlContext);
+    }
+
+    aRenderFramebuffer->ColorTexture()->Bind (
+      theGlContext, GL_TEXTURE0 + OpenGl_RT_PrevAccumTexture);
+
+    aDepthSourceFramebuffer->DepthStencilTexture()->Bind (
+      theGlContext, GL_TEXTURE0 + OpenGl_RT_DepthTexture);
+
+    theGlContext->core20fwd->glDrawArrays (GL_TRIANGLES, 0, 6);
+
+    aDepthSourceFramebuffer->DepthStencilTexture()->Unbind (
+      theGlContext, GL_TEXTURE0 + OpenGl_RT_DepthTexture);
+
+    aRenderFramebuffer->ColorTexture()->Unbind (
+      theGlContext, GL_TEXTURE0 + OpenGl_RT_PrevAccumTexture);
   }
 
   unbindRaytraceTextures (theGlContext);
@@ -2578,10 +2573,13 @@ Standard_Boolean OpenGl_View::raytrace (const Standard_Integer        theSizeX,
         0, GL_DEBUG_SEVERITY_MEDIUM, "Error: Failed to acquire OpenGL image textures");
     }
 
-    // Remember the old depth function
+    // Remember the old depth function and mask
     GLint aDepthFunc;
     theGlContext->core11fwd->glGetIntegerv (GL_DEPTH_FUNC, &aDepthFunc);
 
+    GLboolean aDepthMask;
+    theGlContext->core11fwd->glGetBooleanv (GL_DEPTH_WRITEMASK, &aDepthMask);
+
     glDisable (GL_BLEND);
     glDepthFunc (GL_ALWAYS);
 
@@ -2595,8 +2593,9 @@ Standard_Boolean OpenGl_View::raytrace (const Standard_Integer        theSizeX,
                                                    theReadDrawFbo,
                                                    theGlContext);
 
-    // Restore depth function
+    // Restore depth function and mask
     glDepthFunc (aDepthFunc);
+    glDepthMask (aDepthMask);
 
     if (!aResult)
     {
diff --git a/src/Shaders/Display.fs b/src/Shaders/Display.fs
index 85ebec20e0..d367df8e4b 100644
--- a/src/Shaders/Display.fs
+++ b/src/Shaders/Display.fs
@@ -4,6 +4,9 @@ uniform sampler2D uInputTexture;
 //! Ray tracing depth image.
 uniform sampler2D uDepthTexture;
 
+//! Gamma correction flag.
+uniform int uApplyGamma;
+
 //! Output pixel color.
 out vec4 OutColor;
 
@@ -14,6 +17,13 @@ void main (void)
   float aDepth = texelFetch (uDepthTexture, ivec2 (gl_FragCoord.xy), 0).r;
   gl_FragDepth = aDepth;
 
-  // apply gamma correction (we use gamma = 2)
-  OutColor = vec4 (sqrt (aColor.rgb), aColor.a);
+  if (uApplyGamma == 1)
+  {
+    // apply gamma correction (we use gamma = 2)
+    OutColor = vec4 (sqrt (aColor.rgb), aColor.a);
+  }
+  else
+  {
+    OutColor = aColor;
+  }
 }
diff --git a/tests/bugs/vis/bug27337 b/tests/bugs/vis/bug27337
new file mode 100644
index 0000000000..32eda94d03
--- /dev/null
+++ b/tests/bugs/vis/bug27337
@@ -0,0 +1,18 @@
+puts "================"
+puts "0027337: \[Regression vs. 6.9.1\] Selection highlight is poor in ray traced mode with FSAA"
+puts "================"
+
+pload MODELING VISUALIZATION
+box b 10 10 10
+vdisplay b
+vsetdispmode 1
+vfit
+vrenderparams -rayTrace -fsaa
+vmoveto 200 200
+
+puts "Check result by color in the middle of the top face:"
+puts "should be color of shading and not of highlight (blue)"
+checkcolor 205 100 0.64 0.56 0.08
+
+puts "Check result by image: selection highlight should show only visible edges"
+checkview -screenshot -3d -path ${imagedir}/${test_image}.png