0027925: Visualization - implement order-independent transparency algorithm within...
[occt.git] / src / OpenGl / OpenGl_ShaderProgram.cxx
index a3c0a46..7e3b8ad 100755 (executable)
@@ -28,6 +28,9 @@
 
 #include <OpenGl_GlCore32.hxx>
 
+#include "../Shaders/Shaders_DeclarationsImpl_glsl.pxx"
+#include "../Shaders/Shaders_Declarations_glsl.pxx"
+
 #ifdef _WIN32
   #include <malloc.h> // for alloca()
 #endif
@@ -53,7 +56,6 @@ Standard_CString OpenGl_ShaderProgram::PredefinedKeywords[] =
   "occProjectionMatrixInverseTranspose", // OpenGl_OCC_PROJECTION_MATRIX_INVERSE_TRANSPOSE
 
   "occClipPlaneEquations", // OpenGl_OCC_CLIP_PLANE_EQUATIONS
-  "occClipPlaneSpaces",    // OpenGl_OCC_CLIP_PLANE_SPACES
   "occClipPlaneCount",     // OpenGl_OCC_CLIP_PLANE_COUNT
 
   "occLightSourcesCount",  // OpenGl_OCC_LIGHT_SOURCE_COUNT
@@ -68,9 +70,11 @@ Standard_CString OpenGl_ShaderProgram::PredefinedKeywords[] =
   "occBackMaterial",       // OpenGl_OCCT_BACK_MATERIAL
   "occColor",              // OpenGl_OCCT_COLOR
 
+  "occOitOutput",          // OpenGl_OCCT_OIT_OUTPUT
+  "occOitDepthFactor",     // OpenGl_OCCT_OIT_DEPTH_FACTOR
+
   "occTexTrsf2d",          // OpenGl_OCCT_TEXTURE_TRSF2D
   "occPointSize"           // OpenGl_OCCT_POINT_SIZE
-
 };
 
 // =======================================================================
@@ -147,33 +151,13 @@ Standard_Boolean OpenGl_ShaderProgram::Initialize (const Handle(OpenGl_Context)&
     return Standard_False;
   }
 
-  OSD_File aDeclFile     (Graphic3d_ShaderProgram::ShadersFolder() + "/Declarations.glsl");
-  OSD_File aDeclImplFile (Graphic3d_ShaderProgram::ShadersFolder() + "/DeclarationsImpl.glsl");
-  if (!aDeclFile.Exists()
-   || !aDeclImplFile.Exists())
-  {
-    const TCollection_ExtendedString aMsg = "Error! Failed to load OCCT shader declarations file";
-    theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
-                         GL_DEBUG_TYPE_ERROR,
-                         0,
-                         GL_DEBUG_SEVERITY_HIGH,
-                         aMsg);
-    return Standard_False;
-  }
-
   TCollection_AsciiString aHeader = !myProxy.IsNull() && !myProxy->Header().IsEmpty()
                                   ? (myProxy->Header() + "\n")
                                   : TCollection_AsciiString();
 
-  TCollection_AsciiString aDeclarations;
-  aDeclFile.Open (OSD_ReadOnly, OSD_Protection());
-  aDeclFile.Read (aDeclarations, (int)aDeclFile.Size());
-  aDeclFile.Close();
+  TCollection_AsciiString aDeclarations = Shaders_Declarations_glsl;
+  TCollection_AsciiString aDeclImpl = Shaders_DeclarationsImpl_glsl;
 
-  TCollection_AsciiString aDeclImpl;
-  aDeclImplFile.Open (OSD_ReadOnly, OSD_Protection());
-  aDeclImplFile.Read (aDeclImpl, (int)aDeclImplFile.Size());
-  aDeclImplFile.Close();
   aDeclarations += aDeclImpl;
 
   for (Graphic3d_ShaderObjectList::Iterator anIter (theShaders);
@@ -222,11 +206,43 @@ Standard_Boolean OpenGl_ShaderProgram::Initialize (const Handle(OpenGl_Context)&
     }
 
     TCollection_AsciiString aSource = aDeclarations + anIter.Value()->Source();
+    TCollection_AsciiString anExtensions = "// This section enables extensions used in OCCT GLSL programs\n";
+    if (theCtx->hasDrawBuffers)
+    {
+      anExtensions += "#define OCC_ENABLE_draw_buffers\n";
+    }
+    if (theCtx->hasDrawBuffers == OpenGl_FeatureInExtensions)
+    {
+      if (theCtx->arbDrawBuffers)
+      {
+        anExtensions += "#extension GL_ARB_draw_buffers : enable\n";
+      }
+      else if (theCtx->extDrawBuffers)
+      {
+        anExtensions += "#extension GL_EXT_draw_buffers : enable\n";
+      }
+    }
+
+    if (theCtx->hasSampleVariables == OpenGl_FeatureInExtensions)
+    {
+#if defined(GL_ES_VERSION_2_0)
+      if (theCtx->oesSampleVariables)
+      {
+        anExtensions += "#extension GL_OES_sample_variables : enable\n";
+      }
+#else
+      if (theCtx->arbSampleShading)
+      {
+        anExtensions += "#extension GL_ARB_sample_shading : enable\n";
+      }
+#endif
+    }
+
     switch (anIter.Value()->Type())
     {
       case Graphic3d_TOS_VERTEX:
       {
-        aSource = aHeader + TCollection_AsciiString ("#define VERTEX_SHADER\n") + aSource;
+        aSource = aHeader + TCollection_AsciiString ("#define VERTEX_SHADER\n") + anExtensions + aSource;
         break;
       }
       case Graphic3d_TOS_FRAGMENT:
@@ -237,9 +253,9 @@ Standard_Boolean OpenGl_ShaderProgram::Initialize (const Handle(OpenGl_Context)&
                                          "precision highp int;\n"
                                        : "precision mediump float;\n"
                                          "precision mediump int;\n");
-        aSource = aHeader + aPrefix + aSource;
+        aSource = aHeader + aPrefix + anExtensions + aSource;
       #else
-        aSource = aHeader + aSource;
+        aSource = aHeader + anExtensions + aSource;
       #endif
         break;
       }
@@ -247,6 +263,7 @@ Standard_Boolean OpenGl_ShaderProgram::Initialize (const Handle(OpenGl_Context)&
 
     if (!aShader->LoadSource (theCtx, aSource))
     {
+      theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aSource);
       const TCollection_ExtendedString aMsg = "Error! Failed to set shader source";
       theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
                            GL_DEBUG_TYPE_ERROR,
@@ -259,6 +276,7 @@ Standard_Boolean OpenGl_ShaderProgram::Initialize (const Handle(OpenGl_Context)&
 
     if (!aShader->Compile (theCtx))
     {
+      theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aSource);
       TCollection_AsciiString aLog;
       aShader->FetchInfoLog (theCtx, aLog);
       if (aLog.IsEmpty())
@@ -495,32 +513,6 @@ Standard_Boolean OpenGl_ShaderProgram::ApplyVariables(const Handle(OpenGl_Contex
   return Standard_True;
 }
 
-// =======================================================================
-// function : ActiveState
-// purpose  : Returns index of last modification for specified state type
-// =======================================================================
-Standard_Size OpenGl_ShaderProgram::ActiveState (const OpenGl_UniformStateType theType) const
-{
-  if (theType < MaxStateTypes)
-  {
-    return myCurrentState[theType];
-  }
-  return 0;
-}
-
-// =======================================================================
-// function : UpdateState
-// purpose  : Updates index of last modification for specified state type
-// =======================================================================
-void OpenGl_ShaderProgram::UpdateState (const OpenGl_UniformStateType theType,
-                                        const Standard_Size           theIndex)
-{
-  if (theType < MaxStateTypes)
-  {
-    myCurrentState[theType] = theIndex;
-  }
-}
-
 // =======================================================================
 // function : GetUniformLocation
 // purpose  : Returns location (index) of the specific uniform variable