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_RenderTransparentMethod.hxx>
20 #include <Graphic3d_ShaderAttribute.hxx>
21 #include <Graphic3d_ShaderObject.hxx>
22 #include <Graphic3d_ShaderVariable.hxx>
23 #include <Graphic3d_TextureParams.hxx>
24 #include <Graphic3d_TextureSetBits.hxx>
25 #include <NCollection_Sequence.hxx>
26
27 //! List of shader objects.
28 typedef NCollection_Sequence<Handle(Graphic3d_ShaderObject)> Graphic3d_ShaderObjectList;
29
30 //! List of custom uniform shader variables.
31 typedef NCollection_Sequence<Handle(Graphic3d_ShaderVariable)> Graphic3d_ShaderVariableList;
32
33 //! List of custom vertex shader attributes
34 typedef NCollection_Sequence<Handle(Graphic3d_ShaderAttribute)> Graphic3d_ShaderAttributeList;
35
36 //! This class is responsible for managing shader programs.
37 class Graphic3d_ShaderProgram : public Standard_Transient
38 {
39 DEFINE_STANDARD_RTTIEXT(Graphic3d_ShaderProgram, Standard_Transient)
40 public:
41
42 //! Default value of THE_MAX_LIGHTS macros within GLSL program (see Declarations.glsl).
43 static const Standard_Integer THE_MAX_LIGHTS_DEFAULT = 8;
44
45 //! Default value of THE_MAX_CLIP_PLANES macros within GLSL program (see Declarations.glsl).
46 static const Standard_Integer THE_MAX_CLIP_PLANES_DEFAULT = 8;
47
48 //! Default value of THE_NB_FRAG_OUTPUTS macros within GLSL program (see Declarations.glsl).
49 static const Standard_Integer THE_NB_FRAG_OUTPUTS = 1;
50
51 public:
52
53 //! Creates new empty program object.
54 Standard_EXPORT Graphic3d_ShaderProgram();
55
56 //! Releases resources of program object.
57 Standard_EXPORT virtual ~Graphic3d_ShaderProgram();
58
59 //! Checks if the program object is valid or not.
60 Standard_EXPORT virtual Standard_Boolean IsDone() const;
61
62 //! Returns unique ID used to manage resource in graphic driver.
GetId() const63 const TCollection_AsciiString& GetId() const { return myID; }
64
65 //! Sets unique ID used to manage resource in graphic driver.
66 //! WARNING! Graphic3d_ShaderProgram constructor generates a unique id for proper resource management;
67 //! however if application overrides it, it is responsibility of application to avoid name collisions.
SetId(const TCollection_AsciiString & theId)68 void SetId (const TCollection_AsciiString& theId) { myID = theId; }
69
70 //! Returns GLSL header (version code and extensions).
Header() const71 const TCollection_AsciiString& Header() const { return myHeader; }
72
73 //! Setup GLSL header containing language version code and used extensions.
74 //! Will be prepended to the very beginning of the source code.
75 //! Example:
76 //! @code
77 //! #version 300 es
78 //! #extension GL_ARB_bindless_texture : require
79 //! @endcode
SetHeader(const TCollection_AsciiString & theHeader)80 void SetHeader (const TCollection_AsciiString& theHeader) { myHeader = theHeader; }
81
82 //! Append line to GLSL header.
AppendToHeader(const TCollection_AsciiString & theHeaderLine)83 void AppendToHeader (const TCollection_AsciiString& theHeaderLine)
84 {
85 if (!myHeader.IsEmpty())
86 {
87 myHeader += "\n";
88 }
89 myHeader += theHeaderLine;
90 }
91
92 //! Return the length of array of light sources (THE_MAX_LIGHTS),
93 //! to be used for initialization occLightSources.
94 //! Default value is THE_MAX_LIGHTS_DEFAULT.
NbLightsMax() const95 Standard_Integer NbLightsMax() const { return myNbLightsMax; }
96
97 //! Specify the length of array of light sources (THE_MAX_LIGHTS).
SetNbLightsMax(Standard_Integer theNbLights)98 void SetNbLightsMax (Standard_Integer theNbLights) { myNbLightsMax = theNbLights; }
99
100 //! Return the length of array of shadow maps (THE_NB_SHADOWMAPS); 0 by default.
NbShadowMaps() const101 Standard_Integer NbShadowMaps() const { return myNbShadowMaps; }
102
103 //! Specify the length of array of shadow maps (THE_NB_SHADOWMAPS).
SetNbShadowMaps(Standard_Integer theNbMaps)104 void SetNbShadowMaps (Standard_Integer theNbMaps) { myNbShadowMaps = theNbMaps; }
105
106 //! Return the length of array of clipping planes (THE_MAX_CLIP_PLANES),
107 //! to be used for initialization occClipPlaneEquations.
108 //! Default value is THE_MAX_CLIP_PLANES_DEFAULT.
NbClipPlanesMax() const109 Standard_Integer NbClipPlanesMax() const { return myNbClipPlanesMax; }
110
111 //! Specify the length of array of clipping planes (THE_MAX_CLIP_PLANES).
SetNbClipPlanesMax(Standard_Integer theNbPlanes)112 void SetNbClipPlanesMax (Standard_Integer theNbPlanes) { myNbClipPlanesMax = theNbPlanes; }
113
114 //! Attaches shader object to the program object.
115 Standard_EXPORT Standard_Boolean AttachShader (const Handle(Graphic3d_ShaderObject)& theShader);
116
117 //! Detaches shader object from the program object.
118 Standard_EXPORT Standard_Boolean DetachShader (const Handle(Graphic3d_ShaderObject)& theShader);
119
120 //! Returns list of attached shader objects.
ShaderObjects() const121 const Graphic3d_ShaderObjectList& ShaderObjects() const { return myShaderObjects; }
122
123 //! The list of currently pushed but not applied custom uniform variables.
124 //! This list is automatically cleared after applying to GLSL program.
Variables() const125 const Graphic3d_ShaderVariableList& Variables() const { return myVariables; }
126
127 //! Return the list of custom vertex attributes.
VertexAttributes() const128 const Graphic3d_ShaderAttributeList& VertexAttributes() const { return myAttributes; }
129
130 //! Assign the list of custom vertex attributes.
131 //! Should be done before GLSL program initialization.
132 Standard_EXPORT void SetVertexAttributes (const Graphic3d_ShaderAttributeList& theAttributes);
133
134 //! Returns the number (1+) of Fragment Shader outputs to be written to
135 //! (more than 1 can be in case of multiple draw buffers); 1 by default.
NbFragmentOutputs() const136 Standard_Integer NbFragmentOutputs() const { return myNbFragOutputs; }
137
138 //! Sets the number of Fragment Shader outputs to be written to.
139 //! Should be done before GLSL program initialization.
SetNbFragmentOutputs(const Standard_Integer theNbOutputs)140 void SetNbFragmentOutputs (const Standard_Integer theNbOutputs) { myNbFragOutputs = theNbOutputs; }
141
142 //! Return true if Fragment Shader should perform alpha test; FALSE by default.
HasAlphaTest() const143 Standard_Boolean HasAlphaTest() const { return myHasAlphaTest; }
144
145 //! Set if Fragment Shader should perform alpha test.
146 //! Note that this flag is designed for usage with - custom shader program may discard fragment regardless this flag.
SetAlphaTest(Standard_Boolean theAlphaTest)147 void SetAlphaTest (Standard_Boolean theAlphaTest) { myHasAlphaTest = theAlphaTest; }
148
149 //! Return TRUE if standard program header should define default texture sampler occSampler0; TRUE by default for compatibility.
HasDefaultSampler() const150 Standard_Boolean HasDefaultSampler() const { return myHasDefSampler; }
151
152 //! Set if standard program header should define default texture sampler occSampler0.
SetDefaultSampler(Standard_Boolean theHasDefSampler)153 void SetDefaultSampler (Standard_Boolean theHasDefSampler) { myHasDefSampler = theHasDefSampler; }
154
155 //! Return if Fragment Shader color should output to OIT buffers; OFF by default.
OitOutput() const156 Graphic3d_RenderTransparentMethod OitOutput() const { return myOitOutput; }
157
158 //! Set if Fragment Shader color should output to OIT buffers.
159 //! Note that weighted OIT also requires at least 2 Fragment Outputs (color + coverage),
160 //! and Depth Peeling requires at least 3 Fragment Outputs (depth + front color + back color),
SetOitOutput(Graphic3d_RenderTransparentMethod theOutput)161 void SetOitOutput (Graphic3d_RenderTransparentMethod theOutput) { myOitOutput = theOutput; }
162
163 //! Return TRUE if standard program header should define functions and variables used in PBR pipeline.
164 //! FALSE by default.
IsPBR() const165 Standard_Boolean IsPBR() const { return myIsPBR; }
166
167 //! Sets whether standard program header should define functions and variables used in PBR pipeline.
SetPBR(Standard_Boolean theIsPBR)168 void SetPBR (Standard_Boolean theIsPBR) { myIsPBR = theIsPBR; }
169
170 //! Return texture units declared within the program, @sa Graphic3d_TextureSetBits.
TextureSetBits() const171 Standard_Integer TextureSetBits() const { return myTextureSetBits; }
172
173 //! Set texture units declared within the program.
SetTextureSetBits(Standard_Integer theBits)174 void SetTextureSetBits (Standard_Integer theBits) { myTextureSetBits = theBits; }
175
176 //! Pushes custom uniform variable to the program.
177 //! The list of pushed variables is automatically cleared after applying to GLSL program.
178 //! Thus after program recreation even unchanged uniforms should be pushed anew.
179 template<class T>
180 Standard_Boolean PushVariable (const TCollection_AsciiString& theName,
181 const T& theValue);
182
183 //! Removes all custom uniform variables from the program.
184 Standard_EXPORT void ClearVariables();
185
186 //! Pushes float uniform.
PushVariableFloat(const TCollection_AsciiString & theName,const float theValue)187 Standard_Boolean PushVariableFloat (const TCollection_AsciiString& theName, const float theValue) { return PushVariable (theName, theValue); }
188
189 //! Pushes vec2 uniform.
PushVariableVec2(const TCollection_AsciiString & theName,const Graphic3d_Vec2 & theValue)190 Standard_Boolean PushVariableVec2 (const TCollection_AsciiString& theName, const Graphic3d_Vec2& theValue) { return PushVariable (theName, theValue); }
191
192 //! Pushes vec3 uniform.
PushVariableVec3(const TCollection_AsciiString & theName,const Graphic3d_Vec3 & theValue)193 Standard_Boolean PushVariableVec3 (const TCollection_AsciiString& theName, const Graphic3d_Vec3& theValue) { return PushVariable (theName, theValue); }
194
195 //! Pushes vec4 uniform.
PushVariableVec4(const TCollection_AsciiString & theName,const Graphic3d_Vec4 & theValue)196 Standard_Boolean PushVariableVec4 (const TCollection_AsciiString& theName, const Graphic3d_Vec4& theValue) { return PushVariable (theName, theValue); }
197
198 //! Pushes int uniform.
PushVariableInt(const TCollection_AsciiString & theName,const int theValue)199 Standard_Boolean PushVariableInt (const TCollection_AsciiString& theName, const int theValue) { return PushVariable (theName, theValue); }
200
201 //! Pushes vec2i uniform.
PushVariableVec2i(const TCollection_AsciiString & theName,const Graphic3d_Vec2i & theValue)202 Standard_Boolean PushVariableVec2i (const TCollection_AsciiString& theName, const Graphic3d_Vec2i& theValue) { return PushVariable (theName, theValue); }
203
204 //! Pushes vec3i uniform.
PushVariableVec3i(const TCollection_AsciiString & theName,const Graphic3d_Vec3i & theValue)205 Standard_Boolean PushVariableVec3i (const TCollection_AsciiString& theName, const Graphic3d_Vec3i& theValue) { return PushVariable (theName, theValue); }
206
207 //! Pushes vec4i uniform.
PushVariableVec4i(const TCollection_AsciiString & theName,const Graphic3d_Vec4i & theValue)208 Standard_Boolean PushVariableVec4i (const TCollection_AsciiString& theName, const Graphic3d_Vec4i& theValue) { return PushVariable (theName, theValue); }
209
210 public:
211
212 //! The path to GLSL programs determined from CSF_ShadersDirectory or CASROOT environment variables.
213 //! @return the root folder with default GLSL programs.
214 Standard_EXPORT static const TCollection_AsciiString& ShadersFolder();
215
216 private:
217
218 TCollection_AsciiString myID; //!< the unique identifier of program object
219 Graphic3d_ShaderObjectList myShaderObjects; //!< the list of attached shader objects
220 Graphic3d_ShaderVariableList myVariables; //!< the list of custom uniform variables
221 Graphic3d_ShaderAttributeList myAttributes; //!< the list of custom vertex attributes
222 TCollection_AsciiString myHeader; //!< GLSL header with version code and used extensions
223 Standard_Integer myNbLightsMax; //!< length of array of light sources (THE_MAX_LIGHTS)
224 Standard_Integer myNbShadowMaps; //!< length of array of shadow maps (THE_NB_SHADOWMAPS)
225 Standard_Integer myNbClipPlanesMax; //!< length of array of clipping planes (THE_MAX_CLIP_PLANES)
226 Standard_Integer myNbFragOutputs; //!< length of array of Fragment Shader outputs (THE_NB_FRAG_OUTPUTS)
227 Standard_Integer myTextureSetBits;//!< texture units declared within the program, @sa Graphic3d_TextureSetBits
228 Graphic3d_RenderTransparentMethod myOitOutput; //!< flag indicating that Fragment Shader includes OIT outputs
229 Standard_Boolean myHasDefSampler; //!< flag indicating that program defines default texture sampler occSampler0
230 Standard_Boolean myHasAlphaTest; //!< flag indicating that Fragment Shader performs alpha test
231 Standard_Boolean myIsPBR; //!< flag indicating that program defines functions and variables used in PBR pipeline
232
233 };
234
DEFINE_STANDARD_HANDLE(Graphic3d_ShaderProgram,Standard_Transient)235 DEFINE_STANDARD_HANDLE (Graphic3d_ShaderProgram, Standard_Transient)
236
237 // =======================================================================
238 // function : PushVariable
239 // purpose : Pushes custom uniform variable to the program
240 // =======================================================================
241 template<class T> inline
242 Standard_Boolean Graphic3d_ShaderProgram::PushVariable (const TCollection_AsciiString& theName,
243 const T& theValue)
244 {
245 Handle(Graphic3d_ShaderVariable) aVariable = Graphic3d_ShaderVariable::Create (theName, theValue);
246 if (aVariable.IsNull() || !aVariable->IsDone())
247 {
248 return Standard_False;
249 }
250
251 myVariables.Append (aVariable);
252 return Standard_True;
253 }
254
255 #endif
256