Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-11-15 09:47:01

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