0033661: Data Exchange, Step Import - Tessellated GDTs are not imported
[occt.git] / src / Graphic3d / Graphic3d_ShaderProgram.hxx
1 // Created on: 2013-09-20
2 // Created by: Denis BOGOLEPOV
3 // Copyright (c) 2013-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #ifndef _Graphic3d_ShaderProgram_HeaderFile
17 #define _Graphic3d_ShaderProgram_HeaderFile
18
19 #include <Graphic3d_ShaderAttribute.hxx>
20 #include <Graphic3d_ShaderObject.hxx>
21 #include <Graphic3d_ShaderVariable.hxx>
22 #include <Graphic3d_TextureParams.hxx>
23 #include <Graphic3d_TextureSetBits.hxx>
24 #include <NCollection_Sequence.hxx>
25
26 //! List of shader objects.
27 typedef NCollection_Sequence<Handle(Graphic3d_ShaderObject)> Graphic3d_ShaderObjectList;
28
29 //! List of custom uniform shader variables.
30 typedef NCollection_Sequence<Handle(Graphic3d_ShaderVariable)> Graphic3d_ShaderVariableList;
31
32 //! List of custom vertex shader attrubures
33 typedef NCollection_Sequence<Handle(Graphic3d_ShaderAttribute)> Graphic3d_ShaderAttributeList;
34
35 //! This class is responsible for managing shader programs.
36 class Graphic3d_ShaderProgram : public Standard_Transient
37 {
38   DEFINE_STANDARD_RTTIEXT(Graphic3d_ShaderProgram, Standard_Transient)
39 public:
40
41   //! Default value of THE_MAX_LIGHTS macros within GLSL program (see Declarations.glsl).
42   static const Standard_Integer THE_MAX_LIGHTS_DEFAULT = 8;
43
44   //! Default value of THE_MAX_CLIP_PLANES macros within GLSL program (see Declarations.glsl).
45   static const Standard_Integer THE_MAX_CLIP_PLANES_DEFAULT = 8;
46
47   //! Default value of THE_NB_FRAG_OUTPUTS macros within GLSL program (see Declarations.glsl).
48   static const Standard_Integer THE_NB_FRAG_OUTPUTS = 1;
49
50 public:
51
52   //! Creates new empty program object.
53   Standard_EXPORT Graphic3d_ShaderProgram();
54
55   //! Releases resources of program object.
56   Standard_EXPORT virtual ~Graphic3d_ShaderProgram();
57
58   //! Checks if the program object is valid or not.
59   Standard_EXPORT virtual Standard_Boolean IsDone() const;
60
61   //! Returns unique ID used to manage resource in graphic driver.
62   const TCollection_AsciiString& GetId() const { return myID; }
63
64   //! Sets unique ID used to manage resource in graphic driver.
65   //! WARNING! Graphic3d_ShaderProgram constructor generates a unique id for proper resource management;
66   //! however if application overrides it, it is responsibility of application to avoid name collisions.
67   void SetId (const TCollection_AsciiString& theId) { myID = theId; }
68
69   //! Returns GLSL header (version code and extensions).
70   const TCollection_AsciiString& Header() const { return myHeader; }
71
72   //! Setup GLSL header containing language version code and used extensions.
73   //! Will be prepended to the very beginning of the source code.
74   //! Example:
75   //! @code
76   //!   #version 300 es
77   //!   #extension GL_ARB_bindless_texture : require
78   //! @endcode
79   void SetHeader (const TCollection_AsciiString& theHeader) { myHeader = theHeader; }
80
81   //! Append line to GLSL header.
82   void AppendToHeader (const TCollection_AsciiString& theHeaderLine)
83   {
84     if (!myHeader.IsEmpty())
85     {
86       myHeader += "\n";
87     }
88     myHeader += theHeaderLine;
89   }
90
91   //! Return the length of array of light sources (THE_MAX_LIGHTS),
92   //! to be used for initialization occLightSources.
93   //! Default value is THE_MAX_LIGHTS_DEFAULT.
94   Standard_Integer NbLightsMax() const { return myNbLightsMax; }
95
96   //! Specify the length of array of light sources (THE_MAX_LIGHTS).
97   void SetNbLightsMax (Standard_Integer theNbLights) { myNbLightsMax = theNbLights; }
98
99   //! Return the length of array of clipping planes (THE_MAX_CLIP_PLANES),
100   //! to be used for initialization occClipPlaneEquations.
101   //! Default value is THE_MAX_CLIP_PLANES_DEFAULT.
102   Standard_Integer NbClipPlanesMax() const { return myNbClipPlanesMax; }
103
104   //! Specify the length of array of clipping planes (THE_MAX_CLIP_PLANES).
105   void SetNbClipPlanesMax (Standard_Integer theNbPlanes) { myNbClipPlanesMax = theNbPlanes; }
106
107   //! Attaches shader object to the program object.
108   Standard_EXPORT Standard_Boolean AttachShader (const Handle(Graphic3d_ShaderObject)& theShader);
109
110   //! Detaches shader object from the program object.
111   Standard_EXPORT Standard_Boolean DetachShader (const Handle(Graphic3d_ShaderObject)& theShader);
112
113   //! Returns list of attached shader objects.
114   const Graphic3d_ShaderObjectList& ShaderObjects() const { return myShaderObjects; }
115
116   //! The list of currently pushed but not applied custom uniform variables.
117   //! This list is automatically cleared after applying to GLSL program.
118   const Graphic3d_ShaderVariableList& Variables() const { return myVariables; }
119
120   //! Return the list of custom vertex attributes.
121   const Graphic3d_ShaderAttributeList& VertexAttributes() const { return myAttributes; }
122
123   //! Assign the list of custom vertex attributes.
124   //! Should be done before GLSL program initialization.
125   Standard_EXPORT void SetVertexAttributes (const Graphic3d_ShaderAttributeList& theAttributes);
126
127   //! Returns the number (1+) of Fragment Shader outputs to be written to
128   //! (more than 1 can be in case of multiple draw buffers); 1 by default.
129   Standard_Integer NbFragmentOutputs() const { return myNbFragOutputs; }
130
131   //! Sets the number of Fragment Shader outputs to be written to.
132   //! Should be done before GLSL program initialization.
133   void SetNbFragmentOutputs (const Standard_Integer theNbOutputs) { myNbFragOutputs = theNbOutputs; }
134
135   //! Return true if Fragment Shader should perform alpha test; FALSE by default.
136   Standard_Boolean HasAlphaTest() const { return myHasAlphaTest; }
137
138   //! Set if Fragment Shader should perform alpha test.
139   //! Note that this flag is designed for usage with - custom shader program may discard fragment regardless this flag.
140   void SetAlphaTest (Standard_Boolean theAlphaTest) { myHasAlphaTest = theAlphaTest; }
141
142   //! Return TRUE if standard program header should define default texture sampler occSampler0; TRUE by default for compatibility.
143   Standard_Boolean HasDefaultSampler() const { return myHasDefSampler; }
144
145   //! Set if standard program header should define default texture sampler occSampler0.
146   void SetDefaultSampler (Standard_Boolean theHasDefSampler) { myHasDefSampler = theHasDefSampler; }
147
148   //! Return true if Fragment Shader color should output the weighted OIT coverage; FALSE by default.
149   Standard_Boolean HasWeightOitOutput() const { return myHasWeightOitOutput; }
150
151   //! Set if Fragment Shader color should output the weighted OIT coverage.
152   //! Note that weighted OIT also requires at least 2 Fragment Outputs (color + coverage).
153   void SetWeightOitOutput (Standard_Boolean theOutput) { myHasWeightOitOutput = theOutput; }
154
155   //! Return TRUE if standard program header should define functions and variables used in PBR pipeline.
156   //! FALSE by default.
157   Standard_Boolean IsPBR() const { return myIsPBR; }
158
159   //! Sets whether standard program header should define functions and variables used in PBR pipeline.
160   void SetPBR (Standard_Boolean theIsPBR) { myIsPBR = theIsPBR; }
161
162   //! Return texture units declared within the program, @sa Graphic3d_TextureSetBits.
163   Standard_Integer TextureSetBits() const { return myTextureSetBits; }
164
165   //! Set texture units declared within the program.
166   void SetTextureSetBits (Standard_Integer theBits) { myTextureSetBits = theBits; }
167
168   //! Pushes custom uniform variable to the program.
169   //! The list of pushed variables is automatically cleared after applying to GLSL program.
170   //! Thus after program recreation even unchanged uniforms should be pushed anew.
171   template<class T>
172   Standard_Boolean PushVariable (const TCollection_AsciiString& theName,
173                                  const T&                       theValue);
174
175   //! Removes all custom uniform variables from the program.
176   Standard_EXPORT void ClearVariables();
177
178   //! Pushes float uniform.
179   Standard_Boolean PushVariableFloat (const TCollection_AsciiString& theName, const float theValue)            { return PushVariable (theName, theValue); }
180
181   //! Pushes vec2 uniform.
182   Standard_Boolean PushVariableVec2  (const TCollection_AsciiString& theName, const Graphic3d_Vec2& theValue)  { return PushVariable (theName, theValue); }
183
184   //! Pushes vec3 uniform.
185   Standard_Boolean PushVariableVec3  (const TCollection_AsciiString& theName, const Graphic3d_Vec3& theValue)  { return PushVariable (theName, theValue); }
186
187   //! Pushes vec4 uniform.
188   Standard_Boolean PushVariableVec4  (const TCollection_AsciiString& theName, const Graphic3d_Vec4& theValue)  { return PushVariable (theName, theValue); }
189
190   //! Pushes int uniform.
191   Standard_Boolean PushVariableInt   (const TCollection_AsciiString& theName, const int theValue)              { return PushVariable (theName, theValue); }
192
193   //! Pushes vec2i uniform.
194   Standard_Boolean PushVariableVec2i (const TCollection_AsciiString& theName, const Graphic3d_Vec2i& theValue) { return PushVariable (theName, theValue); }
195
196   //! Pushes vec3i uniform.
197   Standard_Boolean PushVariableVec3i (const TCollection_AsciiString& theName, const Graphic3d_Vec3i& theValue) { return PushVariable (theName, theValue); }
198
199   //! Pushes vec4i uniform.
200   Standard_Boolean PushVariableVec4i (const TCollection_AsciiString& theName, const Graphic3d_Vec4i& theValue) { return PushVariable (theName, theValue); }
201
202 public:
203
204   //! The path to GLSL programs determined from CSF_ShadersDirectory or CASROOT environment variables.
205   //! @return the root folder with default GLSL programs.
206   Standard_EXPORT static const TCollection_AsciiString& ShadersFolder();
207
208 private:
209
210   TCollection_AsciiString       myID;            //!< the unique identifier of program object
211   Graphic3d_ShaderObjectList    myShaderObjects; //!< the list of attached shader objects
212   Graphic3d_ShaderVariableList  myVariables;     //!< the list of custom uniform variables
213   Graphic3d_ShaderAttributeList myAttributes;    //!< the list of custom vertex attributes
214   TCollection_AsciiString       myHeader;        //!< GLSL header with version code and used extensions
215   Standard_Integer              myNbLightsMax;   //!< length of array of light sources (THE_MAX_LIGHTS)
216   Standard_Integer              myNbClipPlanesMax; //!< length of array of clipping planes (THE_MAX_CLIP_PLANES)
217   Standard_Integer              myNbFragOutputs; //!< length of array of Fragment Shader outputs (THE_NB_FRAG_OUTPUTS)
218   Standard_Integer              myTextureSetBits;//!< texture units declared within the program, @sa Graphic3d_TextureSetBits
219   Standard_Boolean              myHasDefSampler; //!< flag indicating that program defines default texture sampler occSampler0
220   Standard_Boolean              myHasAlphaTest;       //!< flag indicating that Fragment Shader performs alpha test
221   Standard_Boolean              myHasWeightOitOutput; //!< flag indicating that Fragment Shader includes weighted OIT coverage
222   Standard_Boolean              myIsPBR;         //!< flag indicating that program defines functions and variables used in PBR pipeline
223
224 };
225
226 DEFINE_STANDARD_HANDLE (Graphic3d_ShaderProgram, Standard_Transient)
227
228 // =======================================================================
229 // function : PushVariable
230 // purpose  : Pushes custom uniform variable to the program
231 // =======================================================================
232 template<class T> inline
233 Standard_Boolean Graphic3d_ShaderProgram::PushVariable (const TCollection_AsciiString& theName,
234                                                         const T& theValue)
235 {
236   Handle(Graphic3d_ShaderVariable) aVariable = Graphic3d_ShaderVariable::Create (theName, theValue);
237   if (aVariable.IsNull() || !aVariable->IsDone())
238   {
239     return Standard_False;
240   }
241
242   myVariables.Append (aVariable);
243   return Standard_True;
244 }
245
246 #endif