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