0efb326fd3826e76010f02554a0a35db5fddac74
[occt.git] / src / OpenGl / OpenGl_ShaderProgram.hxx
1 // Created on: 2013-09-19
2 // Created by: Denis BOGOLEPOV
3 // Copyright (c) 2013 OPEN CASCADE SAS
4 //
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
9 //
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 //
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
19
20 #ifndef _OpenGl_ShaderProgram_Header
21 #define _OpenGl_ShaderProgram_Header
22
23 #include <NCollection_DataMap.hxx>
24 #include <NCollection_Sequence.hxx>
25 #include <TCollection_AsciiString.hxx>
26
27 #include <Graphic3d_ShaderObject.hxx>
28 #include <Graphic3d_ShaderProgram.hxx>
29
30 #include <InterfaceGraphic_tgl_all.hxx>
31
32 #include <OpenGl_Vec.hxx>
33 #include <OpenGl_Matrix.hxx>
34 #include <OpenGl_ShaderObject.hxx>
35 #include <Handle_OpenGl_ShaderProgram.hxx>
36
37 //! The enumeration of OCCT-specific OpenGL/GLSL variables.
38 enum OpenGl_StateVariable
39 {
40   // OpenGL matrix state
41   OpenGl_OCC_MODEL_WORLD_MATRIX,
42   OpenGl_OCC_WORLD_VIEW_MATRIX,
43   OpenGl_OCC_PROJECTION_MATRIX,
44   OpenGl_OCC_MODEL_WORLD_MATRIX_INVERSE,
45   OpenGl_OCC_WORLD_VIEW_MATRIX_INVERSE,
46   OpenGl_OCC_PROJECTION_MATRIX_INVERSE,
47   OpenGl_OCC_MODEL_WORLD_MATRIX_TRANSPOSE,
48   OpenGl_OCC_WORLD_VIEW_MATRIX_TRANSPOSE,
49   OpenGl_OCC_PROJECTION_MATRIX_TRANSPOSE,
50   OpenGl_OCC_MODEL_WORLD_MATRIX_INVERSE_TRANSPOSE,
51   OpenGl_OCC_WORLD_VIEW_MATRIX_INVERSE_TRANSPOSE,
52   OpenGl_OCC_PROJECTION_MATRIX_INVERSE_TRANSPOSE,
53
54   // OpenGL clip planes state
55   OpenGl_OCC_CLIP_PLANE_EQUATIONS,
56   OpenGl_OCC_CLIP_PLANE_SPACES,
57
58   // OpenGL light state
59   OpenGl_OCC_LIGHT_SOURCE_COUNT,
60   OpenGl_OCC_LIGHT_SOURCE_TYPES,
61   OpenGl_OCC_LIGHT_SOURCE_PARAMS,
62   OpenGl_OCC_LIGHT_AMBIENT,
63
64   // Material state
65   OpenGl_OCCT_ACTIVE_SAMPLER,
66   OpenGl_OCCT_TEXTURE_ENABLE,
67   OpenGl_OCCT_DISTINGUISH_MODE,
68   OpenGl_OCCT_FRONT_MATERIAL,
69   OpenGl_OCCT_BACK_MATERIAL,
70
71   // DON'T MODIFY THIS ITEM (insert new items before it)
72   OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES
73 };
74
75 class OpenGl_ShaderProgram;
76
77 //! Interface for generic setter of user-defined uniform variables.
78 struct OpenGl_SetterInterface
79 {
80   //! Sets user-defined uniform variable to specified program.
81   virtual void Set (const Handle(OpenGl_Context)&           theCtx,
82                     const Handle(Graphic3d_ShaderVariable)& theVariable,
83                     OpenGl_ShaderProgram*                   theProgram) = 0;
84
85   //! Destructor
86   virtual ~OpenGl_SetterInterface() {}
87 };
88
89 //! List of OpenGL shader objects.
90 typedef NCollection_Sequence<Handle(OpenGl_ShaderObject)>    OpenGl_ShaderList;
91
92 //! List of shader variable setters.
93 typedef NCollection_DataMap<size_t, OpenGl_SetterInterface*> OpenGl_SetterList;
94
95 //! Support tool for setting user-defined uniform variables.
96 class OpenGl_VariableSetterSelector
97 {
98 public:
99
100   //! Creates new setter selector.
101   OpenGl_VariableSetterSelector();
102
103   //! Releases memory resources of setter selector.
104   ~OpenGl_VariableSetterSelector();
105
106   //! Sets user-defined uniform variable to specified program.
107   void Set (const Handle(OpenGl_Context)&           theCtx,
108             const Handle(Graphic3d_ShaderVariable)& theVariable,
109             OpenGl_ShaderProgram*                   theProgram) const;
110
111 private:
112
113   //! List of variable setters.
114   OpenGl_SetterList mySetterList;
115 };
116
117 //! Defines types of uniform state variables.
118 enum OpenGl_UniformStateType
119 {
120   OpenGl_LIGHT_SOURCES_STATE,
121   OpenGl_CLIP_PLANES_STATE,
122   OpenGl_MODEL_WORLD_STATE,
123   OpenGl_WORLD_VIEW_STATE,
124   OpenGl_PROJECTION_STATE,
125   OpenGl_MATERIALS_STATE
126 };
127
128 //! Total number of state types.
129 const int MaxStateTypes = 6;
130
131 //! Wrapper for OpenGL program object.
132 class OpenGl_ShaderProgram : public OpenGl_Resource
133 {
134
135 public:
136
137   //! Non-valid shader name.
138   static const GLuint NO_PROGRAM = 0;
139
140   //! Invalid location of uniform/attribute variable.
141   static const GLint INVALID_LOCATION = -1;
142
143   //! List of pre-defined OCCT state uniform variables.
144   static Standard_CString PredefinedKeywords[OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES];
145
146 protected:
147
148   //! Creates uninitialized shader program.
149   Standard_EXPORT OpenGl_ShaderProgram (const Handle(Graphic3d_ShaderProgram)& theProxy = NULL);
150
151   static OpenGl_VariableSetterSelector mySetterSelector;
152
153 public:
154
155   //! Releases resources of shader program.
156   Standard_EXPORT virtual ~OpenGl_ShaderProgram();
157
158   //! Creates new empty shader program of specified type.
159   Standard_EXPORT Standard_Boolean Create (const Handle(OpenGl_Context)& theCtx);
160
161   //! Destroys shader program.
162   Standard_EXPORT virtual void Release (const OpenGl_Context* theCtx);
163
164   //! Attaches shader object to the program object.
165   Standard_EXPORT Standard_Boolean AttachShader (const Handle(OpenGl_Context)&      theCtx,
166                                                  const Handle(OpenGl_ShaderObject)& theShader);
167
168   //! Detaches shader object to the program object.
169   Standard_EXPORT Standard_Boolean DetachShader (const Handle(OpenGl_Context)&      theCtx,
170                                                  const Handle(OpenGl_ShaderObject)& theShader);
171
172   //! Initializes program object with the list of shader objects.
173   Standard_EXPORT Standard_Boolean Initialize (const Handle(OpenGl_Context)&     theCtx,
174                                                const Graphic3d_ShaderObjectList& theShaders);
175
176   //! Links the program object.
177   Standard_EXPORT Standard_Boolean Link (const Handle(OpenGl_Context)& theCtx);
178
179   //! Fetches information log of the last link operation.
180   Standard_EXPORT Standard_Boolean FetchInfoLog (const Handle(OpenGl_Context)& theCtx,
181                                                  TCollection_AsciiString&      theLog);
182
183   //! Fetches uniform variables from proxy shader program.
184   Standard_EXPORT Standard_Boolean ApplyVariables (const Handle(OpenGl_Context)& theCtx);
185
186   //! Sets the program object as part of current rendering state.
187   Standard_EXPORT void Bind (const Handle(OpenGl_Context)& theCtx) const;
188
189   //! Binds the program object and applies variables from proxy shader program.
190   Standard_EXPORT Standard_Boolean BindWithVariables (const Handle(OpenGl_Context)& theCtx);
191
192   //! Reverts to fixed-function graphics pipeline (FFP).
193   Standard_EXPORT static void Unbind (const Handle(OpenGl_Context)& theCtx);
194
195   //! @return true if current object was initialized
196   inline bool IsValid() const
197   {
198     return myProgramID != NO_PROGRAM;
199   }
200
201 private:
202
203   //! Returns index of last modification of variables of specified state type.
204   Standard_EXPORT Standard_Size ActiveState (const OpenGl_UniformStateType theType) const;
205
206   //! Updates index of last modification of variables of specified state type.
207   Standard_EXPORT void UpdateState (const OpenGl_UniformStateType theType,
208                                     const Standard_Size           theIndex);
209
210 public:
211
212   //! Returns location of the specific uniform variable.
213   Standard_EXPORT GLint GetUniformLocation (const Handle(OpenGl_Context)& theCtx,
214                                             const GLchar*                 theName) const;
215
216   //! Returns index of the generic vertex attribute by variable name.
217   Standard_EXPORT GLint GetAttributeLocation (const Handle(OpenGl_Context)& theCtx,
218                                               const GLchar*                 theName) const;
219
220   //! Returns location of the OCCT state uniform variable.
221   Standard_EXPORT GLint GetStateLocation (const GLuint theVariable) const;
222
223 public:
224
225   //! Returns the value of the integer uniform variable.
226   Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
227                                                const GLchar*                 theName,
228                                                OpenGl_Vec4i&                 theValue) const;
229
230   Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
231                                                GLint                         theLocation,
232                                                OpenGl_Vec4i&                 theValue) const;
233
234   //! Returns the value of the float uniform variable.
235   Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
236                                                const GLchar*                 theName,
237                                                OpenGl_Vec4&                  theValue) const;
238
239   //! Returns the value of the float uniform variable.
240   Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
241                                                GLint                         theLocation,
242                                                OpenGl_Vec4&                  theValue) const;
243
244 public:
245
246   //! Returns the integer vertex attribute.
247   Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
248                                                  const GLchar*                 theName,
249                                                  OpenGl_Vec4i&                 theValue) const;
250
251   //! Returns the integer vertex attribute.
252   Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
253                                                  GLint                         theIndex,
254                                                  OpenGl_Vec4i&                 theValue) const;
255
256   //! Returns the float vertex attribute.
257   Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
258                                                  const GLchar*                 theName,
259                                                  OpenGl_Vec4&                  theValue) const;
260
261   //! Returns the float vertex attribute.
262   Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
263                                                  GLint                         theIndex,
264                                                  OpenGl_Vec4&                  theValue) const;
265
266 public:
267
268   //! Specifies the value of the integer uniform variable.
269   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
270                                                const GLchar*                 theName,
271                                                GLint                         theValue);
272
273   //! Specifies the value of the integer uniform variable.
274   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
275                                                GLint                         theLocation,
276                                                GLint                         theValue);
277
278   //! Specifies the value of the integer uniform 2D vector.
279   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
280                                                const GLchar*                 theName,
281                                                const OpenGl_Vec2i&           theValue);
282
283   //! Specifies the value of the integer uniform 2D vector.
284   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
285                                                GLint                         theLocation,
286                                                const OpenGl_Vec2i&           theValue);
287
288   //! Specifies the value of the integer uniform 3D vector.
289   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
290                                                const GLchar*                 theName,
291                                                const OpenGl_Vec3i&           theValue);
292
293   //! Specifies the value of the integer uniform 3D vector.
294   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
295                                                GLint                         theLocation,
296                                                const OpenGl_Vec3i&           theValue);
297
298   //! Specifies the value of the integer uniform 4D vector.
299   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
300                                                const GLchar*                 theName,
301                                                const OpenGl_Vec4i&           theValue);
302
303   //! Specifies the value of the integer uniform 4D vector.
304   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
305                                                GLint                         theLocation,
306                                                const OpenGl_Vec4i&           theValue);
307
308 public:
309
310   //! Specifies the value of the float uniform variable.
311   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
312                                                const GLchar*                 theName,
313                                                GLfloat                       theValue);
314
315   //! Specifies the value of the float uniform variable.
316   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
317                                                GLint                         theLocation,
318                                                GLfloat                       theValue);
319
320   //! Specifies the value of the float uniform 2D vector.
321   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
322                                                const GLchar*                 theName,
323                                                const OpenGl_Vec2&            theValue);
324
325   //! Specifies the value of the float uniform 2D vector.
326   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
327                                                GLint                         theLocation,
328                                                const OpenGl_Vec2&            theValue);
329
330   //! Specifies the value of the float uniform 3D vector.
331   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
332                                                const GLchar*                 theName,
333                                                const OpenGl_Vec3&            theValue);
334
335   //! Specifies the value of the float uniform 3D vector.
336   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
337                                                GLint                         theLocation,
338                                                const OpenGl_Vec3&            theValue);
339
340   //! Specifies the value of the float uniform 4D vector.
341   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
342                                                const GLchar*                 theName,
343                                                const OpenGl_Vec4&            theValue);
344
345   //! Specifies the value of the float uniform 4D vector.
346   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
347                                                GLint                         theLocation,
348                                                const OpenGl_Vec4&            theValue);
349
350 public:
351
352   //! Specifies the value of the float uniform 4x4 matrix.
353   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
354                                                const GLchar*                 theName,
355                                                const OpenGl_Matrix&          theValue,
356                                                GLboolean                     theTranspose = GL_FALSE);
357
358   //! Specifies the value of the float uniform 4x4 matrix.
359   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
360                                                GLint                         theLocation,
361                                                const OpenGl_Matrix&          theValue,
362                                                GLboolean                     theTranspose = GL_FALSE);
363
364   //! Specifies the value of the float uniform 4x4 matrix.
365   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
366                                                const GLchar*                 theName,
367                                                const Tmatrix3&               theValue,
368                                                GLboolean                     theTranspose = GL_FALSE);
369
370   //! Specifies the value of the float uniform 4x4 matrix.
371   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
372                                                GLint                         theLocation,
373                                                const Tmatrix3&               theValue,
374                                                GLboolean                     theTranspose = GL_FALSE);
375
376 public:
377
378   //! Specifies the value of the sampler uniform variable.
379   Standard_EXPORT Standard_Boolean SetSampler (const Handle(OpenGl_Context)& theCtx,
380                                                const GLchar*                 theName,
381                                                const GLenum                  theTextureUnit);
382
383   //! Specifies the value of the sampler uniform variable.
384   Standard_EXPORT Standard_Boolean SetSampler (const Handle(OpenGl_Context)& theCtx,
385                                                GLint                         theLocation,
386                                                const GLenum                  theTextureUnit);
387
388 protected:
389
390   //! Increments counter of users.
391   //! Used by OpenGl_ShaderManager.
392   //! @return true when resource has been restored from delayed release queue
393   bool Share()
394   {
395     return ++myShareCount == 1;
396   }
397
398   //! Decrements counter of users.
399   //! Used by OpenGl_ShaderManager.
400   //! @return true when there are no more users of this program has been left
401   bool UnShare()
402   {
403     return --myShareCount == 0;
404   }
405
406 protected:
407
408   GLuint                          myProgramID;     //!< Handle of OpenGL shader program
409   OpenGl_ShaderList               myShaderObjects; //!< List of attached shader objects
410   Handle(Graphic3d_ShaderProgram) myProxy;         //!< Proxy shader program (from application layer)
411   Standard_Integer                myShareCount;    //!< program users count, initialized with 1 (already shared by one user)
412
413 protected:
414
415   //! Defines last modification for variables of each state type.
416   Standard_Size myCurrentState[MaxStateTypes];
417
418   //! Stores locations of OCCT state uniform variables.
419   GLint myStateLocations[OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES];
420
421 public:
422
423   DEFINE_STANDARD_RTTI (OpenGl_ShaderProgram)
424   friend class OpenGl_ShaderManager;
425
426 };
427
428 template<class T>
429 struct OpenGl_VariableSetter : public OpenGl_SetterInterface
430 {
431   virtual void Set (const Handle(OpenGl_Context)&           theCtx,
432                     const Handle(Graphic3d_ShaderVariable)& theVariable,
433                     OpenGl_ShaderProgram*                   theProgram)
434   {
435     theProgram->SetUniform (theCtx,
436                             theVariable->Name().ToCString(),
437                             theVariable->Value()->As<T>());
438   }
439 };
440
441 namespace OpenGl_HashMapInitializer
442 {
443   template<class K, class V>
444   struct MapListOfType
445   {
446     NCollection_DataMap<K, V> myDictionary;
447
448     MapListOfType (K theKey, V theValue)
449     {
450       myDictionary.Bind (theKey, theValue);
451     }
452
453     MapListOfType& operator() (K theKey, V theValue)
454     {
455       myDictionary.Bind (theKey, theValue);
456       return *this;
457     }
458
459     operator const NCollection_DataMap<K, V>& () const
460     {
461       return myDictionary;
462     }
463   };
464
465   template<class K, class V>
466   MapListOfType<K, V> CreateListOf (K theKey, V theValue)
467   {
468     return MapListOfType<K, V> (theKey, theValue);
469   }
470 }
471
472 #endif // _OpenGl_ShaderProgram_Header