0027670: Visualization - avoid duplication of structures defining primitive array...
[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 <NCollection_Sequence.hxx>
24
25 //! List of shader objects.
26 typedef NCollection_Sequence<Handle(Graphic3d_ShaderObject)> Graphic3d_ShaderObjectList;
27
28 //! List of custom uniform shader variables.
29 typedef NCollection_Sequence<Handle(Graphic3d_ShaderVariable)> Graphic3d_ShaderVariableList;
30
31 //! List of custom vertex shader attrubures
32 typedef NCollection_Sequence<Handle(Graphic3d_ShaderAttribute)> Graphic3d_ShaderAttributeList;
33
34 //! This class is responsible for managing shader programs.
35 class Graphic3d_ShaderProgram : public Standard_Transient
36 {
37
38 public:
39
40   //! The list of built-in GLSL programs
41   enum ShaderName
42   {
43     ShaderName_UNKNOWN, //!< undefined program
44     ShaderName_Phong    //!< per-pixel lighting (Phong shading)
45   };
46
47 public:
48
49   //! Creates new empty program object.
50   Standard_EXPORT Graphic3d_ShaderProgram();
51
52   //! Creates program object from pre-defined shaders.
53   //! Raises Standard_Failure exception if shader resources are unavailable.
54   Standard_EXPORT Graphic3d_ShaderProgram (const Graphic3d_ShaderProgram::ShaderName theName);
55
56   //! Releases resources of program object.
57   Standard_EXPORT virtual ~Graphic3d_ShaderProgram();
58
59   //! Releases resources of program object.
60   Standard_EXPORT void Destroy() const;
61
62   //! Checks if the program object is valid or not.
63   Standard_EXPORT virtual Standard_Boolean IsDone() const;
64
65   //! Returns unique ID used to manage resource in graphic driver.
66   const TCollection_AsciiString& GetId() const { return myID; }
67
68   //! Returns GLSL header (version code and extensions).
69   const TCollection_AsciiString& Header() const { return myHeader; }
70
71   //! Setup GLSL header containing language version code and used extensions.
72   //! Will be prepended to the very beginning of the source code.
73   //! Example:
74   //! @code
75   //!   #version 300 es
76   //!   #extension GL_ARB_bindless_texture : require
77   //! @endcode
78   void SetHeader (const TCollection_AsciiString& theHeader) { myHeader = theHeader; }
79
80   //! Attaches shader object to the program object.
81   Standard_EXPORT Standard_Boolean AttachShader (const Handle(Graphic3d_ShaderObject)& theShader);
82
83   //! Detaches shader object from the program object.
84   Standard_EXPORT Standard_Boolean DetachShader (const Handle(Graphic3d_ShaderObject)& theShader);
85
86   //! Returns list of attached shader objects.
87   const Graphic3d_ShaderObjectList& ShaderObjects() const { return myShaderObjects; }
88
89   //! The list of currently pushed but not applied custom uniform variables.
90   //! This list is automatically cleared after applying to GLSL program.
91   const Graphic3d_ShaderVariableList& Variables() const { return myVariables; }
92
93   //! Return the list of custom vertex attributes.
94   const Graphic3d_ShaderAttributeList& VertexAttributes() const { return myAttributes; }
95
96   //! Assign the list of custom vertex attributes.
97   //! Should be done before GLSL program initialization.
98   Standard_EXPORT void SetVertexAttributes (const Graphic3d_ShaderAttributeList& theAttributes);
99
100   //! Pushes custom uniform variable to the program.
101   //! The list of pushed variables is automatically cleared after applying to GLSL program.
102   //! Thus after program recreation even unchanged uniforms should be pushed anew.
103   template<class T>
104   Standard_Boolean PushVariable (const TCollection_AsciiString& theName,
105                                  const T&                       theValue);
106
107   //! Removes all custom uniform variables from the program.
108   Standard_EXPORT void ClearVariables();
109
110   //! Pushes float uniform.
111   Standard_Boolean PushVariableFloat (const TCollection_AsciiString& theName, const float theValue)            { return PushVariable (theName, theValue); }
112
113   //! Pushes vec2 uniform.
114   Standard_Boolean PushVariableVec2  (const TCollection_AsciiString& theName, const Graphic3d_Vec2& theValue)  { return PushVariable (theName, theValue); }
115
116   //! Pushes vec3 uniform.
117   Standard_Boolean PushVariableVec3  (const TCollection_AsciiString& theName, const Graphic3d_Vec3& theValue)  { return PushVariable (theName, theValue); }
118
119   //! Pushes vec4 uniform.
120   Standard_Boolean PushVariableVec4  (const TCollection_AsciiString& theName, const Graphic3d_Vec4& theValue)  { return PushVariable (theName, theValue); }
121
122   //! Pushes int uniform.
123   Standard_Boolean PushVariableInt   (const TCollection_AsciiString& theName, const int theValue)              { return PushVariable (theName, theValue); }
124
125   //! Pushes vec2i uniform.
126   Standard_Boolean PushVariableVec2i (const TCollection_AsciiString& theName, const Graphic3d_Vec2i& theValue) { return PushVariable (theName, theValue); }
127
128   //! Pushes vec3i uniform.
129   Standard_Boolean PushVariableVec3i (const TCollection_AsciiString& theName, const Graphic3d_Vec3i& theValue) { return PushVariable (theName, theValue); }
130
131   //! Pushes vec4i uniform.
132   Standard_Boolean PushVariableVec4i (const TCollection_AsciiString& theName, const Graphic3d_Vec4i& theValue) { return PushVariable (theName, theValue); }
133
134 public:
135
136   //! The path to GLSL programs determined from CSF_ShadersDirectory or CASROOT environment variables.
137   //! @return the root folder with default GLSL programs.
138   Standard_EXPORT static const TCollection_AsciiString& ShadersFolder();
139
140 public:
141
142   DEFINE_STANDARD_RTTIEXT(Graphic3d_ShaderProgram,Standard_Transient)
143
144 private:
145
146   TCollection_AsciiString       myID;            //!< the unique identifier of program object
147   Graphic3d_ShaderObjectList    myShaderObjects; //!< the list of attached shader objects
148   Graphic3d_ShaderVariableList  myVariables;     //!< the list of custom uniform variables
149   Graphic3d_ShaderAttributeList myAttributes;    //!< the list of custom vertex attributes
150   TCollection_AsciiString       myHeader;        //!< GLSL header with version code and used extensions
151
152 };
153
154 DEFINE_STANDARD_HANDLE (Graphic3d_ShaderProgram, Standard_Transient)
155
156 // =======================================================================
157 // function : PushVariable
158 // purpose  : Pushes custom uniform variable to the program
159 // =======================================================================
160 template<class T> inline
161 Standard_Boolean Graphic3d_ShaderProgram::PushVariable (const TCollection_AsciiString& theName,
162                                                         const T& theValue)
163 {
164   Handle(Graphic3d_ShaderVariable) aVariable = Graphic3d_ShaderVariable::Create (theName, theValue);
165   if (aVariable.IsNull() || !aVariable->IsDone())
166   {
167     return Standard_False;
168   }
169
170   myVariables.Append (aVariable);
171   return Standard_True;
172 }
173
174 #endif