From e3414ada99bbed0eeffb83a11dac980a7f79dd4a Mon Sep 17 00:00:00 2001
From: kgv <kgv@opencascade.com>
Date: Thu, 29 Jan 2015 15:16:09 +0300
Subject: [PATCH] 0025758: Visualization, TKOpenGl - texture initialization
 fails on Intel HD 4600 in ray tracing test

Unbind global texture sampler right after its usage.
---
 src/OpenGl/OpenGl_Text.cxx      | 23 -----------------------
 src/OpenGl/OpenGl_Workspace.cxx |  6 ++++++
 2 files changed, 6 insertions(+), 23 deletions(-)

diff --git a/src/OpenGl/OpenGl_Text.cxx b/src/OpenGl/OpenGl_Text.cxx
index 7d7de531eb..da89880e4c 100755
--- a/src/OpenGl/OpenGl_Text.cxx
+++ b/src/OpenGl/OpenGl_Text.cxx
@@ -19,7 +19,6 @@
 #include <OpenGl_ShaderManager.hxx>
 #include <OpenGl_ShaderProgram.hxx>
 #include <OpenGl_ShaderStates.hxx>
-#include <OpenGl_Sampler.hxx>
 #include <OpenGl_Text.hxx>
 #include <OpenGl_Utils.hxx>
 #include <OpenGl_Workspace.hxx>
@@ -382,11 +381,6 @@ void OpenGl_Text::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
   const OpenGl_AspectText*      aTextAspect  = theWorkspace->AspectText (Standard_True);
   const Handle(OpenGl_Texture)  aPrevTexture = theWorkspace->DisableTexture();
   const Handle(OpenGl_Context)& aCtx         = theWorkspace->GetGlContext();
-  const Handle(OpenGl_Sampler)& aSampler     = aCtx->TextureSampler();
-  if (!aSampler.IsNull())
-  {
-    aSampler->Unbind (*aCtx);
-  }
 
   if (aCtx->IsGlGreaterEqual (2, 0))
   {
@@ -424,10 +418,6 @@ void OpenGl_Text::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
   }
 
   // restore aspects
-  if (!aSampler.IsNull())
-  {
-    aSampler->Bind (*aCtx);
-  }
   if (!aPrevTexture.IsNull())
   {
     theWorkspace->EnableTexture (aPrevTexture);
@@ -759,13 +749,6 @@ void OpenGl_Text::render (const Handle(OpenGl_PrinterContext)& thePrintCtx,
     theCtx->core15fwd->glActiveTexture (GL_TEXTURE0);
   }
 
-  // unbind current OpenGL sampler
-  const Handle(OpenGl_Sampler)& aSampler = theCtx->TextureSampler();
-  if (!aSampler.IsNull() && aSampler->IsValid())
-  {
-    aSampler->Unbind (*theCtx);
-  }
-
   // extra drawings
   switch (theTextAspect.DisplayType())
   {
@@ -855,11 +838,5 @@ void OpenGl_Text::render (const Handle(OpenGl_PrinterContext)& thePrintCtx,
   // model view matrix was modified
   theCtx->WorldViewState.Pop();
   theCtx->ApplyModelViewMatrix();
-
-  // revert custom OpenGL sampler
-  if (!aSampler.IsNull() && aSampler->IsValid())
-  {
-    aSampler->Bind (*theCtx);
-  }
 #endif
 }
diff --git a/src/OpenGl/OpenGl_Workspace.cxx b/src/OpenGl/OpenGl_Workspace.cxx
index fdeaeb52d1..c0e1d1a827 100644
--- a/src/OpenGl/OpenGl_Workspace.cxx
+++ b/src/OpenGl/OpenGl_Workspace.cxx
@@ -292,6 +292,12 @@ Handle(OpenGl_Texture) OpenGl_Workspace::DisableTexture()
     return myTextureBound;
   }
 
+  const Handle(OpenGl_Sampler)& aSampler = myGlContext->TextureSampler();
+  if (!aSampler.IsNull())
+  {
+    aSampler->Unbind (*myGlContext);
+  }
+
 #if !defined(GL_ES_VERSION_2_0)
   // reset texture matrix because some code may expect it is identity
   GLint aMatrixMode = GL_TEXTURE;