Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-01 09:17:52

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 typedef NCollection_Sequence<Handle(Graphic3d_ShaderObject)> Graphic3d_ShaderObjectList;
0028 
0029 //! List of custom uniform shader variables.
0030 typedef NCollection_Sequence<Handle(Graphic3d_ShaderVariable)> Graphic3d_ShaderVariableList;
0031 
0032 //! List of custom vertex shader attributes
0033 typedef NCollection_Sequence<Handle(Graphic3d_ShaderAttribute)> Graphic3d_ShaderAttributeList;
0034 
0035 //! This class is responsible for managing shader programs.
0036 class Graphic3d_ShaderProgram : public Standard_Transient
0037 {
0038   DEFINE_STANDARD_RTTIEXT(Graphic3d_ShaderProgram, Standard_Transient)
0039 public:
0040   //! Default value of THE_MAX_LIGHTS macros within GLSL program (see Declarations.glsl).
0041   static const Standard_Integer THE_MAX_LIGHTS_DEFAULT = 8;
0042 
0043   //! Default value of THE_MAX_CLIP_PLANES macros within GLSL program (see Declarations.glsl).
0044   static const Standard_Integer THE_MAX_CLIP_PLANES_DEFAULT = 8;
0045 
0046   //! Default value of THE_NB_FRAG_OUTPUTS macros within GLSL program (see Declarations.glsl).
0047   static const Standard_Integer THE_NB_FRAG_OUTPUTS = 1;
0048 
0049 public:
0050   //! Creates new empty program object.
0051   Standard_EXPORT Graphic3d_ShaderProgram();
0052 
0053   //! Releases resources of program object.
0054   Standard_EXPORT virtual ~Graphic3d_ShaderProgram();
0055 
0056   //! Checks if the program object is valid or not.
0057   Standard_EXPORT virtual Standard_Boolean IsDone() const;
0058 
0059   //! Returns unique ID used to manage resource in graphic driver.
0060   const TCollection_AsciiString& GetId() const { return myID; }
0061 
0062   //! Sets unique ID used to manage resource in graphic driver.
0063   //! WARNING! Graphic3d_ShaderProgram constructor generates a unique id for proper resource
0064   //! management; however if application overrides it, it is responsibility of application to avoid
0065   //! name collisions.
0066   void SetId(const TCollection_AsciiString& theId) { myID = theId; }
0067 
0068   //! Returns GLSL header (version code and extensions).
0069   const TCollection_AsciiString& Header() const { return myHeader; }
0070 
0071   //! Setup GLSL header containing language version code and used extensions.
0072   //! Will be prepended to the very beginning of the source code.
0073   //! Example:
0074   //! @code
0075   //!   #version 300 es
0076   //!   #extension GL_ARB_bindless_texture : require
0077   //! @endcode
0078   void SetHeader(const TCollection_AsciiString& theHeader) { myHeader = theHeader; }
0079 
0080   //! Append line to GLSL header.
0081   void AppendToHeader(const TCollection_AsciiString& theHeaderLine)
0082   {
0083     if (!myHeader.IsEmpty())
0084     {
0085       myHeader += "\n";
0086     }
0087     myHeader += theHeaderLine;
0088   }
0089 
0090   //! Return the length of array of light sources (THE_MAX_LIGHTS),
0091   //! to be used for initialization occLightSources.
0092   //! Default value is THE_MAX_LIGHTS_DEFAULT.
0093   Standard_Integer NbLightsMax() const { return myNbLightsMax; }
0094 
0095   //! Specify the length of array of light sources (THE_MAX_LIGHTS).
0096   void SetNbLightsMax(Standard_Integer theNbLights) { myNbLightsMax = theNbLights; }
0097 
0098   //! Return the length of array of shadow maps (THE_NB_SHADOWMAPS); 0 by default.
0099   Standard_Integer NbShadowMaps() const { return myNbShadowMaps; }
0100 
0101   //! Specify the length of array of shadow maps (THE_NB_SHADOWMAPS).
0102   void SetNbShadowMaps(Standard_Integer theNbMaps) { myNbShadowMaps = theNbMaps; }
0103 
0104   //! Return the length of array of clipping planes (THE_MAX_CLIP_PLANES),
0105   //! to be used for initialization occClipPlaneEquations.
0106   //! Default value is THE_MAX_CLIP_PLANES_DEFAULT.
0107   Standard_Integer NbClipPlanesMax() const { return myNbClipPlanesMax; }
0108 
0109   //! Specify the length of array of clipping planes (THE_MAX_CLIP_PLANES).
0110   void SetNbClipPlanesMax(Standard_Integer theNbPlanes) { myNbClipPlanesMax = theNbPlanes; }
0111 
0112   //! Attaches shader object to the program object.
0113   Standard_EXPORT Standard_Boolean AttachShader(const Handle(Graphic3d_ShaderObject)& theShader);
0114 
0115   //! Detaches shader object from the program object.
0116   Standard_EXPORT Standard_Boolean DetachShader(const Handle(Graphic3d_ShaderObject)& theShader);
0117 
0118   //! Returns list of attached shader objects.
0119   const Graphic3d_ShaderObjectList& ShaderObjects() const { return myShaderObjects; }
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 Graphic3d_ShaderVariableList& Variables() const { return myVariables; }
0124 
0125   //! Return the list of custom vertex attributes.
0126   const Graphic3d_ShaderAttributeList& VertexAttributes() const { return myAttributes; }
0127 
0128   //! Assign the list of custom vertex attributes.
0129   //! Should be done before GLSL program initialization.
0130   Standard_EXPORT void SetVertexAttributes(const Graphic3d_ShaderAttributeList& theAttributes);
0131 
0132   //! Returns the number (1+) of Fragment Shader outputs to be written to
0133   //! (more than 1 can be in case of multiple draw buffers); 1 by default.
0134   Standard_Integer NbFragmentOutputs() const { return myNbFragOutputs; }
0135 
0136   //! Sets the number of Fragment Shader outputs to be written to.
0137   //! Should be done before GLSL program initialization.
0138   void SetNbFragmentOutputs(const Standard_Integer theNbOutputs) { myNbFragOutputs = theNbOutputs; }
0139 
0140   //! Return true if Fragment Shader should perform alpha test; FALSE by default.
0141   Standard_Boolean HasAlphaTest() const { return myHasAlphaTest; }
0142 
0143   //! Set if Fragment Shader should perform alpha test.
0144   //! Note that this flag is designed for usage with - custom shader program may discard fragment
0145   //! regardless this flag.
0146   void SetAlphaTest(Standard_Boolean theAlphaTest) { myHasAlphaTest = theAlphaTest; }
0147 
0148   //! Return TRUE if standard program header should define default texture sampler occSampler0; TRUE
0149   //! by default for compatibility.
0150   Standard_Boolean HasDefaultSampler() const { return myHasDefSampler; }
0151 
0152   //! Set if standard program header should define default texture sampler occSampler0.
0153   void SetDefaultSampler(Standard_Boolean theHasDefSampler) { myHasDefSampler = theHasDefSampler; }
0154 
0155   //! Return if Fragment Shader color should output to OIT buffers; OFF by default.
0156   Graphic3d_RenderTransparentMethod OitOutput() const { return myOitOutput; }
0157 
0158   //! Set if Fragment Shader color should output to OIT buffers.
0159   //! Note that weighted OIT also requires at least 2 Fragment Outputs (color + coverage),
0160   //! and Depth Peeling requires at least 3 Fragment Outputs (depth + front color + back color),
0161   void SetOitOutput(Graphic3d_RenderTransparentMethod theOutput) { myOitOutput = theOutput; }
0162 
0163   //! Return TRUE if standard program header should define functions and variables used in PBR
0164   //! pipeline. FALSE by default.
0165   Standard_Boolean IsPBR() const { return myIsPBR; }
0166 
0167   //! Sets whether standard program header should define functions and variables used in PBR
0168   //! pipeline.
0169   void SetPBR(Standard_Boolean theIsPBR) { myIsPBR = theIsPBR; }
0170 
0171   //! Return texture units declared within the program, @sa Graphic3d_TextureSetBits.
0172   Standard_Integer TextureSetBits() const { return myTextureSetBits; }
0173 
0174   //! Set texture units declared within the program.
0175   void SetTextureSetBits(Standard_Integer theBits) { myTextureSetBits = theBits; }
0176 
0177   //! Pushes custom uniform variable to the program.
0178   //! The list of pushed variables is automatically cleared after applying to GLSL program.
0179   //! Thus after program recreation even unchanged uniforms should be pushed anew.
0180   template <class T>
0181   Standard_Boolean PushVariable(const TCollection_AsciiString& theName, const T& theValue);
0182 
0183   //! Removes all custom uniform variables from the program.
0184   Standard_EXPORT void ClearVariables();
0185 
0186   //! Pushes float uniform.
0187   Standard_Boolean PushVariableFloat(const TCollection_AsciiString& theName, const float theValue)
0188   {
0189     return PushVariable(theName, theValue);
0190   }
0191 
0192   //! Pushes vec2 uniform.
0193   Standard_Boolean PushVariableVec2(const TCollection_AsciiString& theName,
0194                                     const Graphic3d_Vec2&          theValue)
0195   {
0196     return PushVariable(theName, theValue);
0197   }
0198 
0199   //! Pushes vec3 uniform.
0200   Standard_Boolean PushVariableVec3(const TCollection_AsciiString& theName,
0201                                     const Graphic3d_Vec3&          theValue)
0202   {
0203     return PushVariable(theName, theValue);
0204   }
0205 
0206   //! Pushes vec4 uniform.
0207   Standard_Boolean PushVariableVec4(const TCollection_AsciiString& theName,
0208                                     const Graphic3d_Vec4&          theValue)
0209   {
0210     return PushVariable(theName, theValue);
0211   }
0212 
0213   //! Pushes int uniform.
0214   Standard_Boolean PushVariableInt(const TCollection_AsciiString& theName, const int theValue)
0215   {
0216     return PushVariable(theName, theValue);
0217   }
0218 
0219   //! Pushes vec2i uniform.
0220   Standard_Boolean PushVariableVec2i(const TCollection_AsciiString& theName,
0221                                      const Graphic3d_Vec2i&         theValue)
0222   {
0223     return PushVariable(theName, theValue);
0224   }
0225 
0226   //! Pushes vec3i uniform.
0227   Standard_Boolean PushVariableVec3i(const TCollection_AsciiString& theName,
0228                                      const Graphic3d_Vec3i&         theValue)
0229   {
0230     return PushVariable(theName, theValue);
0231   }
0232 
0233   //! Pushes vec4i uniform.
0234   Standard_Boolean PushVariableVec4i(const TCollection_AsciiString& theName,
0235                                      const Graphic3d_Vec4i&         theValue)
0236   {
0237     return PushVariable(theName, theValue);
0238   }
0239 
0240 public:
0241   //! The path to GLSL programs determined from CSF_ShadersDirectory or CASROOT environment
0242   //! variables.
0243   //! @return the root folder with default GLSL programs.
0244   Standard_EXPORT static const TCollection_AsciiString& ShadersFolder();
0245 
0246 private:
0247   TCollection_AsciiString       myID;            //!< the unique identifier of program object
0248   Graphic3d_ShaderObjectList    myShaderObjects; //!< the list of attached shader objects
0249   Graphic3d_ShaderVariableList  myVariables;     //!< the list of custom uniform variables
0250   Graphic3d_ShaderAttributeList myAttributes;    //!< the list of custom vertex attributes
0251   // clang-format off
0252   TCollection_AsciiString       myHeader;        //!< GLSL header with version code and used extensions
0253   Standard_Integer              myNbLightsMax;   //!< length of array of light sources (THE_MAX_LIGHTS)
0254   Standard_Integer              myNbShadowMaps;  //!< length of array of shadow maps (THE_NB_SHADOWMAPS)
0255   Standard_Integer              myNbClipPlanesMax; //!< length of array of clipping planes (THE_MAX_CLIP_PLANES)
0256   Standard_Integer              myNbFragOutputs; //!< length of array of Fragment Shader outputs (THE_NB_FRAG_OUTPUTS)
0257   Standard_Integer              myTextureSetBits;//!< texture units declared within the program, @sa Graphic3d_TextureSetBits
0258   Graphic3d_RenderTransparentMethod myOitOutput; //!< flag indicating that Fragment Shader includes OIT outputs
0259   Standard_Boolean              myHasDefSampler; //!< flag indicating that program defines default texture sampler occSampler0
0260   Standard_Boolean              myHasAlphaTest;       //!< flag indicating that Fragment Shader performs alpha test
0261   Standard_Boolean              myIsPBR;         //!< flag indicating that program defines functions and variables used in PBR pipeline
0262   // clang-format on
0263 };
0264 
0265 DEFINE_STANDARD_HANDLE(Graphic3d_ShaderProgram, Standard_Transient)
0266 
0267 // =======================================================================
0268 // function : PushVariable
0269 // purpose  : Pushes custom uniform variable to the program
0270 // =======================================================================
0271 template <class T>
0272 inline Standard_Boolean Graphic3d_ShaderProgram::PushVariable(
0273   const TCollection_AsciiString& theName,
0274   const T&                       theValue)
0275 {
0276   Handle(Graphic3d_ShaderVariable) aVariable = Graphic3d_ShaderVariable::Create(theName, theValue);
0277   if (aVariable.IsNull() || !aVariable->IsDone())
0278   {
0279     return Standard_False;
0280   }
0281 
0282   myVariables.Append(aVariable);
0283   return Standard_True;
0284 }
0285 
0286 #endif