Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-10-02 09:12:30

0001 // Created on: 2013-09-20
0002 // Created by: Denis BOGOLEPOV
0003 // Copyright (c) 2013-2014 OPEN CASCADE SAS
0004 //
0005 // This file is part of Open CASCADE Technology software library.
0006 //
0007 // This library is free software; you can redistribute it and/or modify it under
0008 // the terms of the GNU Lesser General Public License version 2.1 as published
0009 // by the Free Software Foundation, with special exception defined in the file
0010 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0011 // distribution for complete text of the license and disclaimer of any warranty.
0012 //
0013 // Alternatively, this file may be used under the terms of Open CASCADE
0014 // commercial license or contractual agreement.
0015 
0016 #ifndef _Graphic3d_ShaderProgram_HeaderFile
0017 #define _Graphic3d_ShaderProgram_HeaderFile
0018 
0019 #include <Graphic3d_RenderTransparentMethod.hxx>
0020 #include <Graphic3d_ShaderAttribute.hxx>
0021 #include <Graphic3d_ShaderObject.hxx>
0022 #include <Graphic3d_ShaderVariable.hxx>
0023 #include <Graphic3d_TextureParams.hxx>
0024 #include <NCollection_Sequence.hxx>
0025 
0026 //! List of shader objects.
0027 
0028 //! List of custom uniform shader variables.
0029 
0030 //! List of custom vertex shader attributes
0031 
0032 //! This class is responsible for managing shader programs.
0033 class Graphic3d_ShaderProgram : public Standard_Transient
0034 {
0035   DEFINE_STANDARD_RTTIEXT(Graphic3d_ShaderProgram, Standard_Transient)
0036 public:
0037   //! Default value of THE_MAX_LIGHTS macros within GLSL program (see Declarations.glsl).
0038   static const int THE_MAX_LIGHTS_DEFAULT = 8;
0039 
0040   //! Default value of THE_MAX_CLIP_PLANES macros within GLSL program (see Declarations.glsl).
0041   static const int THE_MAX_CLIP_PLANES_DEFAULT = 8;
0042 
0043   //! Default value of THE_NB_FRAG_OUTPUTS macros within GLSL program (see Declarations.glsl).
0044   static const int THE_NB_FRAG_OUTPUTS = 1;
0045 
0046 public:
0047   //! Creates new empty program object.
0048   Standard_EXPORT Graphic3d_ShaderProgram();
0049 
0050   //! Releases resources of program object.
0051   Standard_EXPORT ~Graphic3d_ShaderProgram() override;
0052 
0053   //! Checks if the program object is valid or not.
0054   Standard_EXPORT virtual bool IsDone() const;
0055 
0056   //! Returns unique ID used to manage resource in graphic driver.
0057   const TCollection_AsciiString& GetId() const { return myID; }
0058 
0059   //! Sets unique ID used to manage resource in graphic driver.
0060   //! WARNING! Graphic3d_ShaderProgram constructor generates a unique id for proper resource
0061   //! management; however if application overrides it, it is responsibility of application to avoid
0062   //! name collisions.
0063   void SetId(const TCollection_AsciiString& theId) { myID = theId; }
0064 
0065   //! Returns GLSL header (version code and extensions).
0066   const TCollection_AsciiString& Header() const { return myHeader; }
0067 
0068   //! Setup GLSL header containing language version code and used extensions.
0069   //! Will be prepended to the very beginning of the source code.
0070   //! Example:
0071   //! @code
0072   //!   #version 300 es
0073   //!   #extension GL_ARB_bindless_texture : require
0074   //! @endcode
0075   void SetHeader(const TCollection_AsciiString& theHeader) { myHeader = theHeader; }
0076 
0077   //! Append line to GLSL header.
0078   void AppendToHeader(const TCollection_AsciiString& theHeaderLine)
0079   {
0080     if (!myHeader.IsEmpty())
0081     {
0082       myHeader += "\n";
0083     }
0084     myHeader += theHeaderLine;
0085   }
0086 
0087   //! Return the length of array of light sources (THE_MAX_LIGHTS),
0088   //! to be used for initialization occLightSources.
0089   //! Default value is THE_MAX_LIGHTS_DEFAULT.
0090   int NbLightsMax() const { return myNbLightsMax; }
0091 
0092   //! Specify the length of array of light sources (THE_MAX_LIGHTS).
0093   void SetNbLightsMax(int theNbLights) { myNbLightsMax = theNbLights; }
0094 
0095   //! Return the length of array of shadow maps (THE_NB_SHADOWMAPS); 0 by default.
0096   int NbShadowMaps() const { return myNbShadowMaps; }
0097 
0098   //! Specify the length of array of shadow maps (THE_NB_SHADOWMAPS).
0099   void SetNbShadowMaps(int theNbMaps) { myNbShadowMaps = theNbMaps; }
0100 
0101   //! Return the length of array of clipping planes (THE_MAX_CLIP_PLANES),
0102   //! to be used for initialization occClipPlaneEquations.
0103   //! Default value is THE_MAX_CLIP_PLANES_DEFAULT.
0104   int NbClipPlanesMax() const { return myNbClipPlanesMax; }
0105 
0106   //! Specify the length of array of clipping planes (THE_MAX_CLIP_PLANES).
0107   void SetNbClipPlanesMax(int theNbPlanes) { myNbClipPlanesMax = theNbPlanes; }
0108 
0109   //! Attaches shader object to the program object.
0110   Standard_EXPORT bool AttachShader(const occ::handle<Graphic3d_ShaderObject>& theShader);
0111 
0112   //! Detaches shader object from the program object.
0113   Standard_EXPORT bool DetachShader(const occ::handle<Graphic3d_ShaderObject>& theShader);
0114 
0115   //! Returns list of attached shader objects.
0116   const NCollection_Sequence<occ::handle<Graphic3d_ShaderObject>>& ShaderObjects() const
0117   {
0118     return myShaderObjects;
0119   }
0120 
0121   //! The list of currently pushed but not applied custom uniform variables.
0122   //! This list is automatically cleared after applying to GLSL program.
0123   const NCollection_Sequence<occ::handle<Graphic3d_ShaderVariable>>& Variables() const
0124   {
0125     return myVariables;
0126   }
0127 
0128   //! Return the list of custom vertex attributes.
0129   const NCollection_Sequence<occ::handle<Graphic3d_ShaderAttribute>>& VertexAttributes() const
0130   {
0131     return myAttributes;
0132   }
0133 
0134   //! Assign the list of custom vertex attributes.
0135   //! Should be done before GLSL program initialization.
0136   Standard_EXPORT void SetVertexAttributes(
0137     const NCollection_Sequence<occ::handle<Graphic3d_ShaderAttribute>>& theAttributes);
0138 
0139   //! Returns the number (1+) of Fragment Shader outputs to be written to
0140   //! (more than 1 can be in case of multiple draw buffers); 1 by default.
0141   int NbFragmentOutputs() const { return myNbFragOutputs; }
0142 
0143   //! Sets the number of Fragment Shader outputs to be written to.
0144   //! Should be done before GLSL program initialization.
0145   void SetNbFragmentOutputs(const int theNbOutputs) { myNbFragOutputs = theNbOutputs; }
0146 
0147   //! Return true if Fragment Shader should perform alpha test; FALSE by default.
0148   bool HasAlphaTest() const { return myHasAlphaTest; }
0149 
0150   //! Set if Fragment Shader should perform alpha test.
0151   //! Note that this flag is designed for usage with - custom shader program may discard fragment
0152   //! regardless this flag.
0153   void SetAlphaTest(bool theAlphaTest) { myHasAlphaTest = theAlphaTest; }
0154 
0155   //! Return TRUE if standard program header should define default texture sampler occSampler0; TRUE
0156   //! by default for compatibility.
0157   bool HasDefaultSampler() const { return myHasDefSampler; }
0158 
0159   //! Set if standard program header should define default texture sampler occSampler0.
0160   void SetDefaultSampler(bool theHasDefSampler) { myHasDefSampler = theHasDefSampler; }
0161 
0162   //! Return if Fragment Shader color should output to OIT buffers; OFF by default.
0163   Graphic3d_RenderTransparentMethod OitOutput() const { return myOitOutput; }
0164 
0165   //! Set if Fragment Shader color should output to OIT buffers.
0166   //! Note that weighted OIT also requires at least 2 Fragment Outputs (color + coverage),
0167   //! and Depth Peeling requires at least 3 Fragment Outputs (depth + front color + back color),
0168   void SetOitOutput(Graphic3d_RenderTransparentMethod theOutput) { myOitOutput = theOutput; }
0169 
0170   //! Return TRUE if standard program header should define functions and variables used in PBR
0171   //! pipeline. FALSE by default.
0172   bool IsPBR() const { return myIsPBR; }
0173 
0174   //! Sets whether standard program header should define functions and variables used in PBR
0175   //! pipeline.
0176   void SetPBR(bool theIsPBR) { myIsPBR = theIsPBR; }
0177 
0178   //! Return texture units declared within the program, @sa Graphic3d_TextureSetBits.
0179   int TextureSetBits() const { return myTextureSetBits; }
0180 
0181   //! Set texture units declared within the program.
0182   void SetTextureSetBits(int theBits) { myTextureSetBits = theBits; }
0183 
0184   //! Pushes custom uniform variable to the program.
0185   //! The list of pushed variables is automatically cleared after applying to GLSL program.
0186   //! Thus after program recreation even unchanged uniforms should be pushed anew.
0187   template <class T>
0188   bool PushVariable(const TCollection_AsciiString& theName, const T& theValue);
0189 
0190   //! Removes all custom uniform variables from the program.
0191   Standard_EXPORT void ClearVariables();
0192 
0193   //! Pushes float uniform.
0194   bool PushVariableFloat(const TCollection_AsciiString& theName, const float theValue)
0195   {
0196     return PushVariable(theName, theValue);
0197   }
0198 
0199   //! Pushes vec2 uniform.
0200   bool PushVariableVec2(const TCollection_AsciiString& theName,
0201                         const NCollection_Vec2<float>& theValue)
0202   {
0203     return PushVariable(theName, theValue);
0204   }
0205 
0206   //! Pushes vec3 uniform.
0207   bool PushVariableVec3(const TCollection_AsciiString& theName,
0208                         const NCollection_Vec3<float>& theValue)
0209   {
0210     return PushVariable(theName, theValue);
0211   }
0212 
0213   //! Pushes vec4 uniform.
0214   bool PushVariableVec4(const TCollection_AsciiString& theName,
0215                         const NCollection_Vec4<float>& theValue)
0216   {
0217     return PushVariable(theName, theValue);
0218   }
0219 
0220   //! Pushes int uniform.
0221   bool PushVariableInt(const TCollection_AsciiString& theName, const int theValue)
0222   {
0223     return PushVariable(theName, theValue);
0224   }
0225 
0226   //! Pushes vec2i uniform.
0227   bool PushVariableVec2i(const TCollection_AsciiString& theName,
0228                          const NCollection_Vec2<int>&   theValue)
0229   {
0230     return PushVariable(theName, theValue);
0231   }
0232 
0233   //! Pushes vec3i uniform.
0234   bool PushVariableVec3i(const TCollection_AsciiString& theName,
0235                          const NCollection_Vec3<int>&   theValue)
0236   {
0237     return PushVariable(theName, theValue);
0238   }
0239 
0240   //! Pushes vec4i uniform.
0241   bool PushVariableVec4i(const TCollection_AsciiString& theName,
0242                          const NCollection_Vec4<int>&   theValue)
0243   {
0244     return PushVariable(theName, theValue);
0245   }
0246 
0247   //! Pushes mat3 uniform.
0248   bool PushVariableMat3(const TCollection_AsciiString& theName,
0249                         const NCollection_Mat3<float>& theValue)
0250   {
0251     return PushVariable(theName, theValue);
0252   }
0253 
0254   //! Pushes mat4 uniform.
0255   bool PushVariableMat4(const TCollection_AsciiString& theName,
0256                         const NCollection_Mat4<float>& theValue)
0257   {
0258     return PushVariable(theName, theValue);
0259   }
0260 
0261 public:
0262   //! The path to GLSL programs determined from CSF_ShadersDirectory or CASROOT environment
0263   //! variables.
0264   //! @return the root folder with default GLSL programs.
0265   Standard_EXPORT static const TCollection_AsciiString& ShadersFolder();
0266 
0267 private:
0268   TCollection_AsciiString myID; //!< the unique identifier of program object
0269   NCollection_Sequence<occ::handle<Graphic3d_ShaderObject>>
0270     myShaderObjects; //!< the list of attached shader objects
0271   NCollection_Sequence<occ::handle<Graphic3d_ShaderVariable>>
0272     myVariables; //!< the list of custom uniform variables
0273   NCollection_Sequence<occ::handle<Graphic3d_ShaderAttribute>>
0274     myAttributes; //!< the list of custom vertex attributes
0275   // clang-format off
0276   TCollection_AsciiString       myHeader;        //!< GLSL header with version code and used extensions
0277   int              myNbLightsMax;   //!< length of array of light sources (THE_MAX_LIGHTS)
0278   int              myNbShadowMaps;  //!< length of array of shadow maps (THE_NB_SHADOWMAPS)
0279   int              myNbClipPlanesMax; //!< length of array of clipping planes (THE_MAX_CLIP_PLANES)
0280   int              myNbFragOutputs; //!< length of array of Fragment Shader outputs (THE_NB_FRAG_OUTPUTS)
0281   int              myTextureSetBits;//!< texture units declared within the program, @sa Graphic3d_TextureSetBits
0282   Graphic3d_RenderTransparentMethod myOitOutput; //!< flag indicating that Fragment Shader includes OIT outputs
0283   bool              myHasDefSampler; //!< flag indicating that program defines default texture sampler occSampler0
0284   bool              myHasAlphaTest;       //!< flag indicating that Fragment Shader performs alpha test
0285   bool              myIsPBR;         //!< flag indicating that program defines functions and variables used in PBR pipeline
0286   // clang-format on
0287 };
0288 
0289 // =======================================================================
0290 // function : PushVariable
0291 // purpose  : Pushes custom uniform variable to the program
0292 // =======================================================================
0293 template <class T>
0294 inline bool Graphic3d_ShaderProgram::PushVariable(const TCollection_AsciiString& theName,
0295                                                   const T&                       theValue)
0296 {
0297   occ::handle<Graphic3d_ShaderVariable> aVariable =
0298     Graphic3d_ShaderVariable::Create(theName, theValue);
0299   if (aVariable.IsNull() || !aVariable->IsDone())
0300   {
0301     return false;
0302   }
0303 
0304   myVariables.Append(aVariable);
0305   return true;
0306 }
0307 
0308 #endif