0027787: Visualization, TKOpenGl - Optimize rendering by additional check whether...
[occt.git] / src / OpenGl / OpenGl_ShaderManager.hxx
index acb058a..b13d422 100644 (file)
 #ifndef _OpenGl_ShaderManager_HeaderFile
 #define _OpenGl_ShaderManager_HeaderFile
 
-#include <Graphic3d_ShaderProgram_Handle.hxx>
+#include <Graphic3d_ShaderProgram.hxx>
+#include <Graphic3d_StereoMode.hxx>
+#include <Graphic3d_TypeOfShadingModel.hxx>
 
 #include <NCollection_DataMap.hxx>
 #include <NCollection_Sequence.hxx>
 
-#include <Handle_OpenGl_ShaderManager.hxx>
 #include <OpenGl_SetOfShaderPrograms.hxx>
 #include <OpenGl_ShaderStates.hxx>
 #include <OpenGl_AspectFace.hxx>
 #include <OpenGl_AspectText.hxx>
 #include <OpenGl_AspectMarker.hxx>
 #include <OpenGl_Texture.hxx>
-#include <Visual3d_TypeOfModel.hxx>
+
 
 class OpenGl_View;
 
 //! List of shader programs.
 typedef NCollection_Sequence<Handle(OpenGl_ShaderProgram)> OpenGl_ShaderProgramList;
 
-//! Map to declare per-program states of OCCT materials.
-typedef NCollection_DataMap<Handle(OpenGl_ShaderProgram), OpenGl_MaterialState> OpenGl_MaterialStates;
+class OpenGl_ShaderManager;
+DEFINE_STANDARD_HANDLE(OpenGl_ShaderManager, Standard_Transient)
 
 //! This class is responsible for managing shader programs.
 class OpenGl_ShaderManager : public Standard_Transient
@@ -75,74 +76,106 @@ public:
   Standard_EXPORT Standard_Boolean IsEmpty() const;
 
   //! Bind program for filled primitives rendering
-  Standard_Boolean BindProgram (const OpenGl_AspectFace*            theAspect,
-                                const Handle(OpenGl_Texture)&       theTexture,
-                                const Standard_Boolean              theToLightOn,
-                                const Standard_Boolean              theHasVertColor,
-                                const Handle(OpenGl_ShaderProgram)& theCustomProgram)
+  Standard_Boolean BindFaceProgram (const Handle(OpenGl_Texture)&       theTexture,
+                                    const Standard_Boolean              theToLightOn,
+                                    const Standard_Boolean              theHasVertColor,
+                                    const Standard_Boolean              theEnableEnvMap,
+                                    const Handle(OpenGl_ShaderProgram)& theCustomProgram)
   {
     if (!theCustomProgram.IsNull()
      || myContext->caps->ffpEnable)
     {
-      return bindProgramWithState (theCustomProgram, theAspect);
+      return bindProgramWithState (theCustomProgram);
     }
 
-    const Standard_Integer        aBits    = getProgramBits (theTexture, theHasVertColor);
+    const Standard_Integer        aBits    = getProgramBits (theTexture, theHasVertColor, theEnableEnvMap);
     Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits);
-    return bindProgramWithState (aProgram, theAspect);
+    return bindProgramWithState (aProgram);
   }
 
   //! Bind program for line rendering
-  Standard_Boolean BindProgram (const OpenGl_AspectLine*            theAspect,
-                                const Handle(OpenGl_Texture)&       theTexture,
-                                const Standard_Boolean              theToLightOn,
-                                const Standard_Boolean              theHasVertColor,
-                                const Handle(OpenGl_ShaderProgram)& theCustomProgram)
+  Standard_Boolean BindLineProgram (const Handle(OpenGl_Texture)&       theTexture,
+                                    const Standard_Boolean              theStipple,
+                                    const Standard_Boolean              theToLightOn,
+                                    const Standard_Boolean              theHasVertColor,
+                                    const Handle(OpenGl_ShaderProgram)& theCustomProgram)
   {
     if (!theCustomProgram.IsNull()
      || myContext->caps->ffpEnable)
     {
-      return bindProgramWithState (theCustomProgram, theAspect);
+      return bindProgramWithState (theCustomProgram);
+    }
+
+    Standard_Integer aBits = getProgramBits (theTexture, theHasVertColor);
+    if (theStipple)
+    {
+      aBits |= OpenGl_PO_StippleLine;
     }
 
-    const Standard_Integer        aBits    = getProgramBits (theTexture, theHasVertColor);
     Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits);
-    return bindProgramWithState (aProgram, theAspect);
+    return bindProgramWithState (aProgram);
   }
 
   //! Bind program for point rendering
-  Standard_Boolean BindProgram (const OpenGl_AspectMarker*          theAspect,
-                                const Handle(OpenGl_Texture)&       theTexture,
-                                const Standard_Boolean              theToLightOn,
-                                const Standard_Boolean              theHasVertColor,
-                                const Handle(OpenGl_ShaderProgram)& theCustomProgram)
+  Standard_Boolean BindMarkerProgram (const Handle(OpenGl_Texture)&       theTexture,
+                                      const Standard_Boolean              theToLightOn,
+                                      const Standard_Boolean              theHasVertColor,
+                                      const Handle(OpenGl_ShaderProgram)& theCustomProgram)
   {
     if (!theCustomProgram.IsNull()
      || myContext->caps->ffpEnable)
     {
-      return bindProgramWithState (theCustomProgram, theAspect);
+      return bindProgramWithState (theCustomProgram);
     }
 
     const Standard_Integer        aBits    = getProgramBits (theTexture, theHasVertColor) | OpenGl_PO_Point;
     Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits);
-    return bindProgramWithState (aProgram, theAspect);
+    return bindProgramWithState (aProgram);
   }
 
   //! Bind program for rendering alpha-textured font.
-  Standard_Boolean BindProgram (const OpenGl_AspectText*            theAspect,
-                                const Handle(OpenGl_ShaderProgram)& theCustomProgram)
+  Standard_Boolean BindFontProgram (const Handle(OpenGl_ShaderProgram)& theCustomProgram)
   {
     if (!theCustomProgram.IsNull()
      || myContext->caps->ffpEnable)
     {
-      return bindProgramWithState (theCustomProgram, theAspect);
+      return bindProgramWithState (theCustomProgram);
     }
 
     if (myFontProgram.IsNull())
     {
       prepareStdProgramFont();
     }
-    return bindProgramWithState (myFontProgram, theAspect);
+
+    return bindProgramWithState (myFontProgram);
+  }
+
+  //! Bind program for FBO blit operation.
+  Standard_Boolean BindFboBlitProgram()
+  {
+    if (myBlitProgram.IsNull())
+    {
+      prepareStdProgramFboBlit();
+    }
+    return !myBlitProgram.IsNull()
+         && myContext->BindProgram (myBlitProgram);
+  }
+
+  //! Bind program for rendering stereoscopic image.
+  Standard_Boolean BindStereoProgram (const Graphic3d_StereoMode theStereoMode)
+  {
+    if (theStereoMode < 0 || theStereoMode >= Graphic3d_StereoMode_NB)
+    {
+      return Standard_False;
+    }
+
+    if (myStereoPrograms[theStereoMode].IsNull())
+    {
+      prepareStdProgramStereo (myStereoPrograms[theStereoMode], theStereoMode);
+    }
+    const Handle(OpenGl_ShaderProgram)& aProgram = myStereoPrograms[theStereoMode];
+    return !aProgram.IsNull()
+         && myContext->BindProgram (aProgram);
   }
 
 public:
@@ -164,9 +197,6 @@ public:
   //! Updates state of OCCT projection transform.
   Standard_EXPORT void UpdateProjectionStateTo (const OpenGl_Mat4& theProjectionMatrix);
 
-  //! Reverts state of OCCT projection transform.
-  Standard_EXPORT void RevertProjectionStateTo (const OpenGl_Mat4& theProjectionMatrix);
-
   //! Pushes current state of OCCT projection transform to specified program.
   Standard_EXPORT void PushProjectionState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
 
@@ -178,9 +208,6 @@ public:
   //! Updates state of OCCT model-world transform.
   Standard_EXPORT void UpdateModelWorldStateTo (const OpenGl_Mat4& theModelWorldMatrix);
 
-  //! Reverts state of OCCT model-world transform.
-  Standard_EXPORT void RevertModelWorldStateTo (const OpenGl_Mat4& theModelWorldMatrix);
-
   //! Pushes current state of OCCT model-world transform to specified program.
   Standard_EXPORT void PushModelWorldState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
 
@@ -192,9 +219,6 @@ public:
   //! Updates state of OCCT world-view transform.
   Standard_EXPORT void UpdateWorldViewStateTo (const OpenGl_Mat4& theWorldViewMatrix);
 
-  //! Reverts state of OCCT world-view transform.
-  Standard_EXPORT void RevertWorldViewStateTo (const OpenGl_Mat4& theWorldViewMatrix);
-
   //! Pushes current state of OCCT world-view transform to specified program.
   Standard_EXPORT void PushWorldViewState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
 
@@ -209,21 +233,6 @@ public:
   //! Pushes current state of OCCT clipping planes to specified program.
   Standard_EXPORT void PushClippingState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
 
-public:
-
-  //! Resets state of OCCT material for all programs.
-  Standard_EXPORT void ResetMaterialStates();
-
-  //! Updates state of OCCT material for specified program.
-  Standard_EXPORT void UpdateMaterialStateTo (const Handle(OpenGl_ShaderProgram)& theProgram,
-                                              const OpenGl_Element*               theAspect);
-
-  //! Pushes current state of OCCT material to specified program.
-  Standard_EXPORT void PushMaterialState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
-
-  //! Returns current state of OCCT material for specified program.
-  Standard_EXPORT const OpenGl_MaterialState* MaterialState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
-
 public:
 
   //! Pushes current state of OCCT graphics parameters to specified program.
@@ -237,8 +246,14 @@ public:
     myContext = theCtx;
   }
 
+  //! Returns true when provided context is the same as used one by shader manager.
+  bool IsSameContext (OpenGl_Context* theCtx) const
+  {
+    return myContext == theCtx;
+  }
+
   //! Sets shading model.
-  Standard_EXPORT void SetShadingModel(const Visual3d_TypeOfModel theModel);
+  Standard_EXPORT void SetShadingModel (const Graphic3d_TypeOfShadingModel theModel);
 
   //! Sets last view manger used with.
   //! Helps to handle matrix states in multi-view configurations.
@@ -257,17 +272,34 @@ protected:
 
   //! Define program bits.
   Standard_Integer getProgramBits (const Handle(OpenGl_Texture)& theTexture,
-                                   const Standard_Boolean        theHasVertColor)
+                                   const Standard_Boolean        theHasVertColor,
+                                   const Standard_Boolean        theEnableEnvMap = Standard_False)
+
   {
     Standard_Integer aBits = 0;
-    if (myContext->Clipping().IsClippingOrCappingOn())
+
+    const Standard_Integer aNbPlanes = myContext->Clipping().NbClippingOrCappingOn();
+    if (aNbPlanes > 0)
+    {
+      aBits |= OpenGl_PO_ClipPlanesN;
+      if (aNbPlanes == 1)
+      {
+        aBits |= OpenGl_PO_ClipPlanes1;
+      }
+      else if (aNbPlanes == 2)
+      {
+        aBits |= OpenGl_PO_ClipPlanes2;
+      }
+    }
+
+    if (theEnableEnvMap)
     {
-      aBits |= OpenGl_PO_ClipPlanes;
+      // Environment map overwrites material texture
+      aBits |= OpenGl_PO_TextureEnv;
     }
-    if (!theTexture.IsNull())
+    else if (!theTexture.IsNull())
     {
-      // GL_RED to be handled
-      aBits |= theTexture->GetFormat() == GL_ALPHA ? OpenGl_PO_TextureA : OpenGl_PO_TextureRGB;
+      aBits |= theTexture->IsAlpha() ? OpenGl_PO_TextureA : OpenGl_PO_TextureRGB;
     }
     if (theHasVertColor)
     {
@@ -280,7 +312,9 @@ protected:
   Handle(OpenGl_ShaderProgram)& getStdProgram (const Standard_Boolean theToLightOn,
                                                const Standard_Integer theBits)
   {
-    if (theToLightOn)
+    // If environment map is enabled lighting calculations are
+    // not needed (in accordance with default OCCT behaviour)
+    if (theToLightOn && (theBits & OpenGl_PO_TextureEnv) == 0)
     {
       Handle(OpenGl_ShaderProgram)& aProgram = myLightPrograms->ChangeValue (theBits);
       if (aProgram.IsNull())
@@ -298,9 +332,18 @@ protected:
     return aProgram;
   }
 
+  //! Prepare standard GLSL program for accessing point sprite alpha.
+  Standard_EXPORT TCollection_AsciiString pointSpriteAlphaSrc (const Standard_Integer theBits);
+
+  //! Prepare standard GLSL program for computing point sprite shading.
+  Standard_EXPORT TCollection_AsciiString pointSpriteShadingSrc (const TCollection_AsciiString theBaseColorSrc, const Standard_Integer theBits);
+
   //! Prepare standard GLSL program for textured font.
   Standard_EXPORT Standard_Boolean prepareStdProgramFont();
 
+  //! Prepare standard GLSL program for FBO blit operation.
+  Standard_EXPORT Standard_Boolean prepareStdProgramFboBlit();
+
   //! Prepare standard GLSL program without lighting.
   Standard_EXPORT Standard_Boolean prepareStdProgramFlat (Handle(OpenGl_ShaderProgram)& theProgram,
                                                           const Standard_Integer        theBits);
@@ -309,7 +352,7 @@ protected:
   Standard_Boolean prepareStdProgramLight (Handle(OpenGl_ShaderProgram)& theProgram,
                                            const Standard_Integer        theBits)
   {
-    return myShadingModel == Visual3d_TOM_FRAGMENT
+    return myShadingModel == Graphic3d_TOSM_FRAGMENT
          ? prepareStdProgramPhong   (theProgram, theBits)
          : prepareStdProgramGouraud (theProgram, theBits);
   }
@@ -323,33 +366,39 @@ protected:
                                                            const Standard_Integer        theBits);
 
   //! Define computeLighting GLSL function depending on current lights configuration
-  Standard_EXPORT TCollection_AsciiString stdComputeLighting();
+  //! @param theHasVertColor flag to use getVertColor() instead of Ambient and Diffuse components of active material
+  Standard_EXPORT TCollection_AsciiString stdComputeLighting (const Standard_Boolean theHasVertColor);
 
   //! Bind specified program to current context and apply state.
-  Standard_EXPORT Standard_Boolean bindProgramWithState (const Handle(OpenGl_ShaderProgram)& theProgram,
-                                                         const OpenGl_Element*               theAspect);
+  Standard_EXPORT Standard_Boolean bindProgramWithState (const Handle(OpenGl_ShaderProgram)& theProgram);
 
   //! Set pointer myLightPrograms to active lighting programs set from myMapOfLightPrograms
   Standard_EXPORT void switchLightPrograms();
 
+  //! Prepare standard GLSL program for stereoscopic image.
+  Standard_EXPORT Standard_Boolean prepareStdProgramStereo (Handle(OpenGl_ShaderProgram)& theProgram,
+                                                            const Graphic3d_StereoMode    theStereoMode);
+
 protected:
 
-  Visual3d_TypeOfModel               myShadingModel;       //!< lighting shading model
+  Graphic3d_TypeOfShadingModel       myShadingModel;       //!< lighting shading model
   OpenGl_ShaderProgramList           myProgramList;        //!< The list of shader programs
   Handle(OpenGl_SetOfShaderPrograms) myLightPrograms;      //!< pointer to active lighting programs matrix
   OpenGl_SetOfShaderPrograms         myFlatPrograms;       //!< programs matrix without  lighting
   Handle(OpenGl_ShaderProgram)       myFontProgram;        //!< standard program for textured text
+  Handle(OpenGl_ShaderProgram)       myBlitProgram;        //!< standard program for FBO blit emulation
   OpenGl_MapOfShaderPrograms         myMapOfLightPrograms; //!< map of lighting programs depending on shading model and lights configuration
 
+  Handle(OpenGl_ShaderProgram)       myStereoPrograms[Graphic3d_StereoMode_NB]; //!< standard stereo programs
+
   OpenGl_Context*                    myContext;            //!< OpenGL context
 
 protected:
 
-  OpenGl_MaterialStates              myMaterialStates;     //!< Per-program state of OCCT material
   OpenGl_ProjectionState             myProjectionState;    //!< State of OCCT projection  transformation
   OpenGl_ModelWorldState             myModelWorldState;    //!< State of OCCT model-world transformation
   OpenGl_WorldViewState              myWorldViewState;     //!< State of OCCT world-view  transformation
-  OpenGl_LightSourceState            myClippingState;      //!< State of OCCT clipping planes
+  OpenGl_ClippingState               myClippingState;      //!< State of OCCT clipping planes
   OpenGl_LightSourceState            myLightSourceState;   //!< State of OCCT light sources
 
 private:
@@ -358,8 +407,7 @@ private:
 
 public:
 
-  DEFINE_STANDARD_RTTI (OpenGl_ShaderManager)
-
+  DEFINE_STANDARD_RTTIEXT(OpenGl_ShaderManager,Standard_Transient)
 };
 
 #endif // _OpenGl_ShaderManager_HeaderFile