Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:03:49

0001 // Copyright (c) 2013-2021 OPEN CASCADE SAS
0002 //
0003 // This file is part of Open CASCADE Technology software library.
0004 //
0005 // This library is free software; you can redistribute it and/or modify it under
0006 // the terms of the GNU Lesser General Public License version 2.1 as published
0007 // by the Free Software Foundation, with special exception defined in the file
0008 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0009 // distribution for complete text of the license and disclaimer of any warranty.
0010 //
0011 // Alternatively, this file may be used under the terms of Open CASCADE
0012 // commercial license or contractual agreement.
0013 
0014 #ifndef _Graphic3d_ShaderManager_HeaderFile
0015 #define _Graphic3d_ShaderManager_HeaderFile
0016 
0017 #include <Aspect_GraphicsLibrary.hxx>
0018 #include <Graphic3d_ShaderFlags.hxx>
0019 #include <Graphic3d_StereoMode.hxx>
0020 #include <Graphic3d_Vec2.hxx>
0021 #include <Standard_Transient.hxx>
0022 #include <TCollection_AsciiString.hxx>
0023 
0024 class Graphic3d_LightSet;
0025 class Graphic3d_ShaderProgram;
0026 
0027 //! GLSL syntax extensions.
0028 enum Graphic3d_GlslExtension
0029 {
0030   Graphic3d_GlslExtension_GL_OES_standard_derivatives, //!< OpenGL ES 2.0 extension GL_OES_standard_derivatives
0031   Graphic3d_GlslExtension_GL_EXT_shader_texture_lod,   //!< OpenGL ES 2.0 extension GL_EXT_shader_texture_lod
0032   Graphic3d_GlslExtension_GL_EXT_frag_depth,           //!< OpenGL ES 2.0 extension GL_EXT_frag_depth
0033   Graphic3d_GlslExtension_GL_EXT_gpu_shader4,          //!< OpenGL 2.0 extension GL_EXT_gpu_shader4
0034 };
0035 enum { Graphic3d_GlslExtension_NB = Graphic3d_GlslExtension_GL_EXT_gpu_shader4 + 1 };
0036 
0037 //! This class is responsible for generation of shader programs.
0038 class Graphic3d_ShaderManager : public Standard_Transient
0039 {
0040   DEFINE_STANDARD_RTTIEXT(Graphic3d_ShaderManager, Standard_Transient)
0041 public:
0042 
0043   //! Creates new empty shader manager.
0044   Standard_EXPORT Graphic3d_ShaderManager (Aspect_GraphicsLibrary theGapi);
0045 
0046   //! Releases resources of shader manager.
0047   Standard_EXPORT virtual ~Graphic3d_ShaderManager();
0048 
0049   //! @return true if detected GL version is greater or equal to requested one.
0050   bool IsGapiGreaterEqual (Standard_Integer theVerMajor,
0051                            Standard_Integer theVerMinor) const
0052   {
0053     return (myGapiVersion[0] >  theVerMajor)
0054         || (myGapiVersion[0] == theVerMajor && myGapiVersion[1] >= theVerMinor);
0055   }
0056 
0057   //! Return GAPI version major number.
0058   Standard_Integer GapiVersionMajor() const { return myGapiVersion[0]; }
0059 
0060   //! Return GAPI version minor number.
0061   Standard_Integer GapiVersionMinor() const { return myGapiVersion[1]; }
0062 
0063   //! Return GAPI version major number.
0064   void SetGapiVersion (Standard_Integer theVerMajor,
0065                        Standard_Integer theVerMinor)
0066   {
0067     myGapiVersion.SetValues (theVerMajor, theVerMinor);
0068   }
0069 
0070   //! Return TRUE if RED channel should be used instead of ALPHA for single-channel textures
0071   //! (e.g. GAPI supports only GL_RED textures and not GL_ALPHA).
0072   bool UseRedAlpha() const { return myUseRedAlpha; }
0073 
0074   //! Set if RED channel should be used instead of ALPHA for single-channel textures.
0075   void SetUseRedAlpha (bool theUseRedAlpha) { myUseRedAlpha = theUseRedAlpha; }
0076 
0077   //! Return flag indicating flat shading usage; TRUE by default.
0078   bool HasFlatShading() const { return myHasFlatShading; }
0079 
0080   //! Return flag indicating flat shading should reverse normal flag; FALSE by default.
0081   bool ToReverseDFdxSign() const { return myToReverseDFdxSign; }
0082 
0083   //! Set flag indicating flat shading usage.
0084   void SetFlatShading (bool theToUse,
0085                        bool theToReverseSign)
0086   {
0087     myHasFlatShading = theToUse;
0088     myToReverseDFdxSign = theToReverseSign;
0089   }
0090 
0091   //! Return TRUE if depth clamping should be emulated by GLSL program; TRUE by default.
0092   bool ToEmulateDepthClamp() const { return myToEmulateDepthClamp; }
0093 
0094   //! Set if depth clamping should be emulated by GLSL program.
0095   void SetEmulateDepthClamp (bool theToEmulate) { myToEmulateDepthClamp = theToEmulate; }
0096 
0097   //! Return TRUE if specified extension is available.
0098   bool HasGlslExtension (Graphic3d_GlslExtension theExt) const { return myGlslExtensions[theExt]; }
0099 
0100   //! Set if specified extension is available or not.
0101   void EnableGlslExtension (Graphic3d_GlslExtension theExt,
0102                             bool theToEnable = true) { myGlslExtensions[theExt] = theToEnable; }
0103 
0104 protected:
0105 
0106   //! Generate map key for light sources configuration.
0107   //! @param theLights [in] list of light sources
0108   //! @param theHasShadowMap [in] flag indicating shadow maps usage
0109   Standard_EXPORT TCollection_AsciiString genLightKey (const Handle(Graphic3d_LightSet)& theLights,
0110                                                        const bool theHasShadowMap) const;
0111 
0112   //! Prepare standard GLSL program for textured font.
0113   Standard_EXPORT Handle(Graphic3d_ShaderProgram) getStdProgramFont() const;
0114 
0115   //! Prepare standard GLSL program without lighting.
0116   //! @param theBits      [in] program bits
0117   //! @param theIsOutline [in] draw silhouette
0118   Standard_EXPORT Handle(Graphic3d_ShaderProgram) getStdProgramUnlit (Standard_Integer theBits,
0119                                                                       Standard_Boolean theIsOutline = false) const;
0120 
0121   //! Prepare standard GLSL program with per-vertex lighting.
0122   //! @param theLights [in] list of light sources
0123   //! @param theBits   [in] program bits
0124   Standard_EXPORT Handle(Graphic3d_ShaderProgram) getStdProgramGouraud (const Handle(Graphic3d_LightSet)& theLights,
0125                                                                         Standard_Integer theBits) const;
0126 
0127   //! Prepare standard GLSL program with per-pixel lighting.
0128   //! @param theLights [in] list of light sources
0129   //! @param theBits   [in] program bits
0130   //! @param theIsFlatNormal [in] when TRUE, the Vertex normals will be ignored and Face normal will be computed instead
0131   //! @param theIsPBR  [in] when TRUE, the PBR pipeline will be activated
0132   //! @param theNbShadowMaps [in] number of shadow maps
0133   Standard_EXPORT Handle(Graphic3d_ShaderProgram) getStdProgramPhong (const Handle(Graphic3d_LightSet)& theLights,
0134                                                                       const Standard_Integer theBits,
0135                                                                       const Standard_Boolean theIsFlatNormal,
0136                                                                       const Standard_Boolean theIsPBR,
0137                                                                       const Standard_Integer theNbShadowMaps) const;
0138 
0139   //! Prepare standard GLSL program for bounding box.
0140   Standard_EXPORT Handle(Graphic3d_ShaderProgram) getStdProgramBoundBox() const;
0141 
0142   //! Generates shader program to render environment cubemap as background.
0143   Standard_EXPORT Handle(Graphic3d_ShaderProgram) getBgCubeMapProgram() const;
0144 
0145   //! Generates shader program to render skydome background.
0146   Standard_EXPORT Handle(Graphic3d_ShaderProgram) getBgSkydomeProgram() const;
0147 
0148   //! Generates shader program to render correctly colored quad.
0149   Standard_EXPORT Handle(Graphic3d_ShaderProgram) getColoredQuadProgram() const;
0150 
0151   //! Prepare GLSL source for IBL generation used in PBR pipeline.
0152   Standard_EXPORT Handle(Graphic3d_ShaderProgram) getPBREnvBakingProgram (Standard_Integer theIndex) const;
0153 
0154   //! Prepare standard GLSL program for FBO blit operation.
0155   Standard_EXPORT Handle(Graphic3d_ShaderProgram) getStdProgramFboBlit (Standard_Integer theNbSamples,
0156                                                                         Standard_Boolean theIsFallback_sRGB) const;
0157 
0158   //! Prepare standard GLSL program for stereoscopic image.
0159   Standard_EXPORT Handle(Graphic3d_ShaderProgram) getStdProgramStereo (Graphic3d_StereoMode theStereoMode) const;
0160 
0161   //! Prepare standard GLSL programs for OIT compositing operation.
0162   Standard_EXPORT Handle(Graphic3d_ShaderProgram) getStdProgramOitCompositing (Standard_Boolean theMsaa) const;
0163 
0164   //! Prepare standard GLSL programs for OIT Depth Peeling blend operation.
0165   Standard_EXPORT Handle(Graphic3d_ShaderProgram) getStdProgramOitDepthPeelingBlend (Standard_Boolean theMsaa) const;
0166 
0167   //! Prepare standard GLSL programs for OIT Depth Peeling flush operation.
0168   Standard_EXPORT Handle(Graphic3d_ShaderProgram) getStdProgramOitDepthPeelingFlush (Standard_Boolean theMsaa) const;
0169 
0170 protected:
0171 
0172   //! Return TRUE if bitwise operations can be used in GLSL program.
0173   Standard_EXPORT bool hasGlslBitwiseOps() const;
0174 
0175   //! Prepare GLSL version header.
0176   //! @param theProgram [in] [out] program to set version header
0177   //! @param theName [in] program id suffix
0178   //! @param theBits [in] program bits
0179   //! @param theUsesDerivates [in] program uses standard derivatives functions or not
0180   //! @return filtered program bits with unsupported features disabled
0181   Standard_EXPORT Standard_Integer defaultGlslVersion (const Handle(Graphic3d_ShaderProgram)& theProgram,
0182                                                        const TCollection_AsciiString& theName,
0183                                                        Standard_Integer theBits,
0184                                                        bool theUsesDerivates = false) const;
0185 
0186   //! Prepare GLSL version header for OIT composition programs.
0187   //! @param theProgram [in] [out] program to set version header
0188   //! @param theName [in] program id suffix
0189   //! @param theMsaa [in] multisampling flag
0190   Standard_EXPORT void defaultOitGlslVersion (const Handle(Graphic3d_ShaderProgram)& theProgram,
0191                                               const TCollection_AsciiString& theName,
0192                                               bool theMsaa) const;
0193 
0194   //! Prepare standard GLSL program for accessing point sprite alpha.
0195   Standard_EXPORT TCollection_AsciiString pointSpriteAlphaSrc (Standard_Integer theBits) const;
0196 
0197   //! Prepare standard GLSL program for computing point sprite shading.
0198   Standard_EXPORT TCollection_AsciiString pointSpriteShadingSrc (const TCollection_AsciiString& theBaseColorSrc,
0199                                                                  Standard_Integer theBits) const;
0200 
0201   //! Define computeLighting GLSL function depending on current lights configuration
0202   //! @param theNbLights     [out] number of defined light sources
0203   //! @param theLights       [in]  light sources list
0204   //! @param theHasVertColor [in]  flag to use getVertColor() instead of Ambient and Diffuse components of active material
0205   //! @param theIsPBR        [in]  flag to activate PBR pipeline
0206   //! @param theHasTexColor  [in]  flag to include color texturing
0207   //! @param theNbShadowMaps [in]  flag to include shadow map
0208   Standard_EXPORT TCollection_AsciiString stdComputeLighting (Standard_Integer& theNbLights,
0209                                                               const Handle(Graphic3d_LightSet)& theLights,
0210                                                               Standard_Boolean  theHasVertColor,
0211                                                               Standard_Boolean  theIsPBR,
0212                                                               Standard_Boolean  theHasTexColor,
0213                                                               Standard_Integer  theNbShadowMaps) const;
0214 
0215 protected:
0216 
0217   Aspect_GraphicsLibrary myGapi;          //!< GAPI name
0218   Graphic3d_Vec2i  myGapiVersion;         //!< GAPI version major/minor number pair
0219   Standard_Boolean myGlslExtensions[Graphic3d_GlslExtension_NB];
0220   Standard_Boolean myHasFlatShading;      //!< flag indicating flat shading usage
0221   Standard_Boolean myToReverseDFdxSign;   //!< flag to reverse flat shading normal (workaround)
0222   Standard_Boolean mySetPointSize;        //!< always set gl_PointSize variable
0223   Standard_Boolean myUseRedAlpha;         //!< use RED channel instead of ALPHA (e.g. GAPI supports only GL_RED textures and not GL_ALPHA)
0224   Standard_Boolean myToEmulateDepthClamp; //!< emulate depth clamping in GLSL program
0225   Standard_Boolean mySRgbState;           //!< track sRGB state
0226 
0227 };
0228 
0229 #endif // _Graphic3d_ShaderManager_HeaderFile