0024309: TKOpenGl - Delay release of GLSL resources
[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_0_EQUATION,
56   OpenGl_OCC_CLIP_PLANE_1_EQUATION,
57   OpenGl_OCC_CLIP_PLANE_2_EQUATION,
58   OpenGl_OCC_CLIP_PLANE_3_EQUATION,
59   OpenGl_OCC_CLIP_PLANE_4_EQUATION,
60   OpenGl_OCC_CLIP_PLANE_5_EQUATION,
61   OpenGl_OCC_CLIP_PLANE_6_EQUATION,
62   OpenGl_OCC_CLIP_PLANE_7_EQUATION,
63   OpenGl_OCC_CLIP_PLANE_0_SPACE,
64   OpenGl_OCC_CLIP_PLANE_1_SPACE,
65   OpenGl_OCC_CLIP_PLANE_2_SPACE,
66   OpenGl_OCC_CLIP_PLANE_3_SPACE,
67   OpenGl_OCC_CLIP_PLANE_4_SPACE,
68   OpenGl_OCC_CLIP_PLANE_5_SPACE,
69   OpenGl_OCC_CLIP_PLANE_6_SPACE,
70   OpenGl_OCC_CLIP_PLANE_7_SPACE,
71
72   OpenGl_OCC_LIGHT_SOURCE_COUNT,
73
74   // OpenGL light state
75   OpenGl_OCC_LIGHT_SOURCE_0_TYPE,
76   OpenGl_OCC_LIGHT_SOURCE_1_TYPE,
77   OpenGl_OCC_LIGHT_SOURCE_2_TYPE,
78   OpenGl_OCC_LIGHT_SOURCE_3_TYPE,
79   OpenGl_OCC_LIGHT_SOURCE_4_TYPE,
80   OpenGl_OCC_LIGHT_SOURCE_5_TYPE,
81   OpenGl_OCC_LIGHT_SOURCE_6_TYPE,
82   OpenGl_OCC_LIGHT_SOURCE_7_TYPE,
83   OpenGl_OCC_LIGHT_SOURCE_0_HEAD,
84   OpenGl_OCC_LIGHT_SOURCE_1_HEAD,
85   OpenGl_OCC_LIGHT_SOURCE_2_HEAD,
86   OpenGl_OCC_LIGHT_SOURCE_3_HEAD,
87   OpenGl_OCC_LIGHT_SOURCE_4_HEAD,
88   OpenGl_OCC_LIGHT_SOURCE_5_HEAD,
89   OpenGl_OCC_LIGHT_SOURCE_6_HEAD,
90   OpenGl_OCC_LIGHT_SOURCE_7_HEAD,
91   OpenGl_OCC_LIGHT_SOURCE_0_AMBIENT,
92   OpenGl_OCC_LIGHT_SOURCE_1_AMBIENT,
93   OpenGl_OCC_LIGHT_SOURCE_2_AMBIENT,
94   OpenGl_OCC_LIGHT_SOURCE_3_AMBIENT,
95   OpenGl_OCC_LIGHT_SOURCE_4_AMBIENT,
96   OpenGl_OCC_LIGHT_SOURCE_5_AMBIENT,
97   OpenGl_OCC_LIGHT_SOURCE_6_AMBIENT,
98   OpenGl_OCC_LIGHT_SOURCE_7_AMBIENT,
99   OpenGl_OCC_LIGHT_SOURCE_0_DIFFUSE,
100   OpenGl_OCC_LIGHT_SOURCE_1_DIFFUSE,
101   OpenGl_OCC_LIGHT_SOURCE_2_DIFFUSE,
102   OpenGl_OCC_LIGHT_SOURCE_3_DIFFUSE,
103   OpenGl_OCC_LIGHT_SOURCE_4_DIFFUSE,
104   OpenGl_OCC_LIGHT_SOURCE_5_DIFFUSE,
105   OpenGl_OCC_LIGHT_SOURCE_6_DIFFUSE,
106   OpenGl_OCC_LIGHT_SOURCE_7_DIFFUSE,
107   OpenGl_OCC_LIGHT_SOURCE_0_SPECULAR,
108   OpenGl_OCC_LIGHT_SOURCE_1_SPECULAR,
109   OpenGl_OCC_LIGHT_SOURCE_2_SPECULAR,
110   OpenGl_OCC_LIGHT_SOURCE_3_SPECULAR,
111   OpenGl_OCC_LIGHT_SOURCE_4_SPECULAR,
112   OpenGl_OCC_LIGHT_SOURCE_5_SPECULAR,
113   OpenGl_OCC_LIGHT_SOURCE_6_SPECULAR,
114   OpenGl_OCC_LIGHT_SOURCE_7_SPECULAR,
115   OpenGl_OCC_LIGHT_SOURCE_0_POSITION,
116   OpenGl_OCC_LIGHT_SOURCE_1_POSITION,
117   OpenGl_OCC_LIGHT_SOURCE_2_POSITION,
118   OpenGl_OCC_LIGHT_SOURCE_3_POSITION,
119   OpenGl_OCC_LIGHT_SOURCE_4_POSITION,
120   OpenGl_OCC_LIGHT_SOURCE_5_POSITION,
121   OpenGl_OCC_LIGHT_SOURCE_6_POSITION,
122   OpenGl_OCC_LIGHT_SOURCE_7_POSITION,
123   OpenGl_OCC_LIGHT_SOURCE_0_SPOT_CUTOFF,
124   OpenGl_OCC_LIGHT_SOURCE_1_SPOT_CUTOFF,
125   OpenGl_OCC_LIGHT_SOURCE_2_SPOT_CUTOFF,
126   OpenGl_OCC_LIGHT_SOURCE_3_SPOT_CUTOFF,
127   OpenGl_OCC_LIGHT_SOURCE_4_SPOT_CUTOFF,
128   OpenGl_OCC_LIGHT_SOURCE_5_SPOT_CUTOFF,
129   OpenGl_OCC_LIGHT_SOURCE_6_SPOT_CUTOFF,
130   OpenGl_OCC_LIGHT_SOURCE_7_SPOT_CUTOFF,
131   OpenGl_OCC_LIGHT_SOURCE_0_SPOT_EXPONENT,
132   OpenGl_OCC_LIGHT_SOURCE_1_SPOT_EXPONENT,
133   OpenGl_OCC_LIGHT_SOURCE_2_SPOT_EXPONENT,
134   OpenGl_OCC_LIGHT_SOURCE_3_SPOT_EXPONENT,
135   OpenGl_OCC_LIGHT_SOURCE_4_SPOT_EXPONENT,
136   OpenGl_OCC_LIGHT_SOURCE_5_SPOT_EXPONENT,
137   OpenGl_OCC_LIGHT_SOURCE_6_SPOT_EXPONENT,
138   OpenGl_OCC_LIGHT_SOURCE_7_SPOT_EXPONENT,
139   OpenGl_OCC_LIGHT_SOURCE_0_SPOT_DIRECTION,
140   OpenGl_OCC_LIGHT_SOURCE_1_SPOT_DIRECTION,
141   OpenGl_OCC_LIGHT_SOURCE_2_SPOT_DIRECTION,
142   OpenGl_OCC_LIGHT_SOURCE_3_SPOT_DIRECTION,
143   OpenGl_OCC_LIGHT_SOURCE_4_SPOT_DIRECTION,
144   OpenGl_OCC_LIGHT_SOURCE_5_SPOT_DIRECTION,
145   OpenGl_OCC_LIGHT_SOURCE_6_SPOT_DIRECTION,
146   OpenGl_OCC_LIGHT_SOURCE_7_SPOT_DIRECTION,
147   OpenGl_OCC_LIGHT_SOURCE_0_CONST_ATTENUATION,
148   OpenGl_OCC_LIGHT_SOURCE_1_CONST_ATTENUATION,
149   OpenGl_OCC_LIGHT_SOURCE_2_CONST_ATTENUATION,
150   OpenGl_OCC_LIGHT_SOURCE_3_CONST_ATTENUATION,
151   OpenGl_OCC_LIGHT_SOURCE_4_CONST_ATTENUATION,
152   OpenGl_OCC_LIGHT_SOURCE_5_CONST_ATTENUATION,
153   OpenGl_OCC_LIGHT_SOURCE_6_CONST_ATTENUATION,
154   OpenGl_OCC_LIGHT_SOURCE_7_CONST_ATTENUATION,
155   OpenGl_OCC_LIGHT_SOURCE_0_LINEAR_ATTENUATION,
156   OpenGl_OCC_LIGHT_SOURCE_1_LINEAR_ATTENUATION,
157   OpenGl_OCC_LIGHT_SOURCE_2_LINEAR_ATTENUATION,
158   OpenGl_OCC_LIGHT_SOURCE_3_LINEAR_ATTENUATION,
159   OpenGl_OCC_LIGHT_SOURCE_4_LINEAR_ATTENUATION,
160   OpenGl_OCC_LIGHT_SOURCE_5_LINEAR_ATTENUATION,
161   OpenGl_OCC_LIGHT_SOURCE_6_LINEAR_ATTENUATION,
162   OpenGl_OCC_LIGHT_SOURCE_7_LINEAR_ATTENUATION,
163
164   // Material state
165   OpenGl_OCCT_ACTIVE_SAMPLER,
166   OpenGl_OCCT_TEXTURE_ENABLE,
167   OpenGl_OCCT_DISTINGUISH_MODE,
168   OpenGl_OCCT_FRONT_MATERIAL_AMBIENT,
169   OpenGl_OCCT_BACK_MATERIAL_AMBIENT,
170   OpenGl_OCCT_FRONT_MATERIAL_DIFFUSE,
171   OpenGl_OCCT_BACK_MATERIAL_DIFFUSE,
172   OpenGl_OCCT_FRONT_MATERIAL_SPECULAR,
173   OpenGl_OCCT_BACK_MATERIAL_SPECULAR,
174   OpenGl_OCCT_FRONT_MATERIAL_EMISSION,
175   OpenGl_OCCT_BACK_MATERIAL_EMISSION,
176   OpenGl_OCCT_FRONT_MATERIAL_SHININESS,
177   OpenGl_OCCT_BACK_MATERIAL_SHININESS,
178   OpenGl_OCCT_FRONT_MATERIAL_TRANSPARENCY,
179   OpenGl_OCCT_BACK_MATERIAL_TRANSPARENCY,
180
181   // DON'T MODIFY THIS ITEM (insert new items before it)
182   OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES
183 };
184
185 class OpenGl_ShaderProgram;
186
187 //! Interface for generic setter of user-defined uniform variables.
188 struct OpenGl_SetterInterface
189 {
190   //! Sets user-defined uniform variable to specified program.
191   virtual void Set (const Handle(OpenGl_Context)&           theCtx,
192                     const Handle(Graphic3d_ShaderVariable)& theVariable,
193                     OpenGl_ShaderProgram*                   theProgram) = 0;
194
195   //! Destructor
196   virtual ~OpenGl_SetterInterface() {}
197 };
198
199 //! List of OpenGL shader objects.
200 typedef NCollection_Sequence<Handle(OpenGl_ShaderObject)>    OpenGl_ShaderList;
201
202 //! List of shader variable setters.
203 typedef NCollection_DataMap<size_t, OpenGl_SetterInterface*> OpenGl_SetterList;
204
205 //! Support tool for setting user-defined uniform variables.
206 class OpenGl_VariableSetterSelector
207 {
208 public:
209
210   //! Creates new setter selector.
211   OpenGl_VariableSetterSelector();
212
213   //! Releases memory resources of setter selector.
214   ~OpenGl_VariableSetterSelector();
215
216   //! Sets user-defined uniform variable to specified program.
217   void Set (const Handle(OpenGl_Context)&           theCtx,
218             const Handle(Graphic3d_ShaderVariable)& theVariable,
219             OpenGl_ShaderProgram*                   theProgram) const;
220
221 private:
222
223   //! List of variable setters.
224   OpenGl_SetterList mySetterList;
225 };
226
227 //! Defines types of uniform state variables.
228 enum OpenGl_UniformStateType
229 {
230   OpenGl_LIGHT_SOURCES_STATE,
231   OpenGl_CLIP_PLANES_STATE,
232   OpenGl_MODEL_WORLD_STATE,
233   OpenGl_WORLD_VIEW_STATE,
234   OpenGl_PROJECTION_STATE,
235   OpenGl_MATERIALS_STATE
236 };
237
238 //! Total number of state types.
239 const int MaxStateTypes = 6;
240
241 //! Wrapper for OpenGL program object.
242 class OpenGl_ShaderProgram : public OpenGl_Resource
243 {
244
245 public:
246
247   //! Non-valid shader name.
248   static const GLuint NO_PROGRAM = 0;
249
250   //! Invalid location of uniform/attribute variable.
251   static const GLint INVALID_LOCATION = -1;
252
253   //! List of pre-defined OCCT state uniform variables.
254   static Standard_CString PredefinedKeywords[OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES];
255
256 protected:
257
258   //! Creates uninitialized shader program.
259   Standard_EXPORT OpenGl_ShaderProgram (const Handle(Graphic3d_ShaderProgram)& theProxy = NULL);
260
261   static OpenGl_VariableSetterSelector mySetterSelector;
262
263 public:
264
265   //! Releases resources of shader program.
266   Standard_EXPORT virtual ~OpenGl_ShaderProgram();
267
268   //! Creates new empty shader program of specified type.
269   Standard_EXPORT Standard_Boolean Create (const Handle(OpenGl_Context)& theCtx);
270
271   //! Destroys shader program.
272   Standard_EXPORT virtual void Release (const OpenGl_Context* theCtx);
273
274   //! Attaches shader object to the program object.
275   Standard_EXPORT Standard_Boolean AttachShader (const Handle(OpenGl_Context)&      theCtx,
276                                                  const Handle(OpenGl_ShaderObject)& theShader);
277
278   //! Detaches shader object to the program object.
279   Standard_EXPORT Standard_Boolean DetachShader (const Handle(OpenGl_Context)&      theCtx,
280                                                  const Handle(OpenGl_ShaderObject)& theShader);
281
282   //! Initializes program object with the list of shader objects.
283   Standard_EXPORT Standard_Boolean Initialize (const Handle(OpenGl_Context)&     theCtx,
284                                                const Graphic3d_ShaderObjectList& theShaders);
285
286   //! Links the program object.
287   Standard_EXPORT Standard_Boolean Link (const Handle(OpenGl_Context)& theCtx);
288
289   //! Fetches information log of the last link operation.
290   Standard_EXPORT Standard_Boolean FetchInfoLog (const Handle(OpenGl_Context)& theCtx,
291                                                  TCollection_AsciiString&      theLog);
292
293   //! Fetches uniform variables from proxy shader program.
294   Standard_EXPORT Standard_Boolean ApplyVariables (const Handle(OpenGl_Context)& theCtx);
295
296   //! Sets the program object as part of current rendering state.
297   Standard_EXPORT void Bind (const Handle(OpenGl_Context)& theCtx) const;
298
299   //! Binds the program object and applies variables from proxy shader program.
300   Standard_EXPORT Standard_Boolean BindWithVariables (const Handle(OpenGl_Context)& theCtx);
301
302   //! Reverts to fixed-function graphics pipeline (FFP).
303   Standard_EXPORT static void Unbind (const Handle(OpenGl_Context)& theCtx);
304
305 private:
306
307   //! Returns index of last modification of variables of specified state type.
308   Standard_EXPORT Standard_Size ActiveState (const OpenGl_UniformStateType theType) const;
309
310   //! Updates index of last modification of variables of specified state type.
311   Standard_EXPORT void UpdateState (const OpenGl_UniformStateType theType,
312                                     const Standard_Size           theIndex);
313
314 public:
315
316   //! Returns location of the specific uniform variable.
317   Standard_EXPORT GLint GetUniformLocation (const Handle(OpenGl_Context)& theCtx,
318                                             const GLchar*                 theName) const;
319
320   //! Returns index of the generic vertex attribute by variable name.
321   Standard_EXPORT GLint GetAttributeLocation (const Handle(OpenGl_Context)& theCtx,
322                                               const GLchar*                 theName) const;
323
324   //! Returns location of the OCCT state uniform variable.
325   Standard_EXPORT GLint GetStateLocation (const GLuint theVariable) const;
326
327 public:
328
329   //! Returns the value of the integer uniform variable.
330   Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
331                                                const GLchar*                 theName,
332                                                OpenGl_Vec4i&                 theValue) const;
333
334   Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
335                                                GLint                         theLocation,
336                                                OpenGl_Vec4i&                 theValue) const;
337
338   //! Returns the value of the float uniform variable.
339   Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
340                                                const GLchar*                 theName,
341                                                OpenGl_Vec4&                  theValue) const;
342
343   //! Returns the value of the float uniform variable.
344   Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
345                                                GLint                         theLocation,
346                                                OpenGl_Vec4&                  theValue) const;
347
348 public:
349
350   //! Returns the integer vertex attribute.
351   Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
352                                                  const GLchar*                 theName,
353                                                  OpenGl_Vec4i&                 theValue) const;
354
355   //! Returns the integer vertex attribute.
356   Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
357                                                  GLint                         theIndex,
358                                                  OpenGl_Vec4i&                 theValue) const;
359
360   //! Returns the float vertex attribute.
361   Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
362                                                  const GLchar*                 theName,
363                                                  OpenGl_Vec4&                  theValue) const;
364
365   //! Returns the float vertex attribute.
366   Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
367                                                  GLint                         theIndex,
368                                                  OpenGl_Vec4&                  theValue) const;
369
370 public:
371
372   //! Specifies the value of the integer uniform variable.
373   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
374                                                const GLchar*                 theName,
375                                                GLint                         theValue);
376
377   //! Specifies the value of the integer uniform variable.
378   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
379                                                GLint                         theLocation,
380                                                GLint                         theValue);
381
382   //! Specifies the value of the integer uniform 2D vector.
383   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
384                                                const GLchar*                 theName,
385                                                const OpenGl_Vec2i&           theValue);
386
387   //! Specifies the value of the integer uniform 2D vector.
388   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
389                                                GLint                         theLocation,
390                                                const OpenGl_Vec2i&           theValue);
391
392   //! Specifies the value of the integer uniform 3D vector.
393   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
394                                                const GLchar*                 theName,
395                                                const OpenGl_Vec3i&           theValue);
396
397   //! Specifies the value of the integer uniform 3D vector.
398   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
399                                                GLint                         theLocation,
400                                                const OpenGl_Vec3i&           theValue);
401
402   //! Specifies the value of the integer uniform 4D vector.
403   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
404                                                const GLchar*                 theName,
405                                                const OpenGl_Vec4i&           theValue);
406
407   //! Specifies the value of the integer uniform 4D vector.
408   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
409                                                GLint                         theLocation,
410                                                const OpenGl_Vec4i&           theValue);
411
412 public:
413
414   //! Specifies the value of the float uniform variable.
415   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
416                                                const GLchar*                 theName,
417                                                GLfloat                       theValue);
418
419   //! Specifies the value of the float uniform variable.
420   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
421                                                GLint                         theLocation,
422                                                GLfloat                       theValue);
423
424   //! Specifies the value of the float uniform 2D vector.
425   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
426                                                const GLchar*                 theName,
427                                                const OpenGl_Vec2&            theValue);
428
429   //! Specifies the value of the float uniform 2D vector.
430   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
431                                                GLint                         theLocation,
432                                                const OpenGl_Vec2&            theValue);
433
434   //! Specifies the value of the float uniform 3D vector.
435   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
436                                                const GLchar*                 theName,
437                                                const OpenGl_Vec3&            theValue);
438
439   //! Specifies the value of the float uniform 3D vector.
440   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
441                                                GLint                         theLocation,
442                                                const OpenGl_Vec3&            theValue);
443
444   //! Specifies the value of the float uniform 4D vector.
445   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
446                                                const GLchar*                 theName,
447                                                const OpenGl_Vec4&            theValue);
448
449   //! Specifies the value of the float uniform 4D vector.
450   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
451                                                GLint                         theLocation,
452                                                const OpenGl_Vec4&            theValue);
453
454 public:
455
456   //! Specifies the value of the float uniform 4x4 matrix.
457   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
458                                                const GLchar*                 theName,
459                                                const OpenGl_Matrix&          theValue,
460                                                GLboolean                     theTranspose = GL_FALSE);
461
462   //! Specifies the value of the float uniform 4x4 matrix.
463   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
464                                                GLint                         theLocation,
465                                                const OpenGl_Matrix&          theValue,
466                                                GLboolean                     theTranspose = GL_FALSE);
467
468   //! Specifies the value of the float uniform 4x4 matrix.
469   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
470                                                const GLchar*                 theName,
471                                                const Tmatrix3&               theValue,
472                                                GLboolean                     theTranspose = GL_FALSE);
473
474   //! Specifies the value of the float uniform 4x4 matrix.
475   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
476                                                GLint                         theLocation,
477                                                const Tmatrix3&               theValue,
478                                                GLboolean                     theTranspose = GL_FALSE);
479
480 public:
481
482   //! Specifies the value of the sampler uniform variable.
483   Standard_EXPORT Standard_Boolean SetSampler (const Handle(OpenGl_Context)& theCtx,
484                                                const GLchar*                 theName,
485                                                const GLenum                  theTextureUnit);
486
487   //! Specifies the value of the sampler uniform variable.
488   Standard_EXPORT Standard_Boolean SetSampler (const Handle(OpenGl_Context)& theCtx,
489                                                GLint                         theLocation,
490                                                const GLenum                  theTextureUnit);
491
492 protected:
493
494   //! Increments counter of users.
495   //! Used by OpenGl_ShaderManager.
496   //! @return true when resource has been restored from delayed release queue
497   bool Share()
498   {
499     return ++myShareCount == 1;
500   }
501
502   //! Decrements counter of users.
503   //! Used by OpenGl_ShaderManager.
504   //! @return true when there are no more users of this program has been left
505   bool UnShare()
506   {
507     return --myShareCount == 0;
508   }
509
510 protected:
511
512   GLuint                          myProgramID;     //!< Handle of OpenGL shader program
513   OpenGl_ShaderList               myShaderObjects; //!< List of attached shader objects
514   Handle(Graphic3d_ShaderProgram) myProxy;         //!< Proxy shader program (from application layer)
515   Standard_Integer                myShareCount;    //!< program users count, initialized with 1 (already shared by one user)
516
517 protected:
518
519   //! Defines last modification for variables of each state type.
520   Standard_Size myCurrentState[MaxStateTypes];
521
522   //! Stores locations of OCCT state uniform variables.
523   GLint myStateLocations[OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES];
524
525 public:
526
527   DEFINE_STANDARD_RTTI (OpenGl_ShaderProgram)
528   friend class OpenGl_ShaderManager;
529
530 };
531
532 template<class T>
533 struct OpenGl_VariableSetter : public OpenGl_SetterInterface
534 {
535   virtual void Set (const Handle(OpenGl_Context)&           theCtx,
536                     const Handle(Graphic3d_ShaderVariable)& theVariable,
537                     OpenGl_ShaderProgram*                   theProgram)
538   {
539     theProgram->SetUniform (theCtx,
540                             theVariable->Name().ToCString(),
541                             theVariable->Value()->As<T>());
542   }
543 };
544
545 namespace OpenGl_HashMapInitializer
546 {
547   template<class K, class V>
548   struct MapListOfType
549   {
550     NCollection_DataMap<K, V> myDictionary;
551
552     MapListOfType (K theKey, V theValue)
553     {
554       myDictionary.Bind (theKey, theValue);
555     }
556
557     MapListOfType& operator() (K theKey, V theValue)
558     {
559       myDictionary.Bind (theKey, theValue);
560       return *this;
561     }
562
563     operator const NCollection_DataMap<K, V>& () const
564     {
565       return myDictionary;
566     }
567   };
568
569   template<class K, class V>
570   MapListOfType<K, V> CreateListOf (K theKey, V theValue)
571   {
572     return MapListOfType<K, V> (theKey, theValue);
573   }
574 }
575
576 #endif // _OpenGl_ShaderProgram_Header