0029517: Visualization - introduce AlphaMode property defining alpha value handling...
[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 <OpenGl_Vec.hxx>
27 #include <OpenGl_Matrix.hxx>
28 #include <OpenGl_NamedResource.hxx>
29 #include <OpenGl_ShaderObject.hxx>
30
31 class OpenGl_ShaderProgram;
32 DEFINE_STANDARD_HANDLE(OpenGl_ShaderProgram, OpenGl_NamedResource)
33
34 //! The enumeration of OCCT-specific OpenGL/GLSL variables.
35 enum OpenGl_StateVariable
36 {
37   // OpenGL matrix state
38   OpenGl_OCC_MODEL_WORLD_MATRIX,
39   OpenGl_OCC_WORLD_VIEW_MATRIX,
40   OpenGl_OCC_PROJECTION_MATRIX,
41   OpenGl_OCC_MODEL_WORLD_MATRIX_INVERSE,
42   OpenGl_OCC_WORLD_VIEW_MATRIX_INVERSE,
43   OpenGl_OCC_PROJECTION_MATRIX_INVERSE,
44   OpenGl_OCC_MODEL_WORLD_MATRIX_TRANSPOSE,
45   OpenGl_OCC_WORLD_VIEW_MATRIX_TRANSPOSE,
46   OpenGl_OCC_PROJECTION_MATRIX_TRANSPOSE,
47   OpenGl_OCC_MODEL_WORLD_MATRIX_INVERSE_TRANSPOSE,
48   OpenGl_OCC_WORLD_VIEW_MATRIX_INVERSE_TRANSPOSE,
49   OpenGl_OCC_PROJECTION_MATRIX_INVERSE_TRANSPOSE,
50
51   // OpenGL clip planes state
52   OpenGl_OCC_CLIP_PLANE_EQUATIONS,
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_TEXTURE_ENABLE,
63   OpenGl_OCCT_DISTINGUISH_MODE,
64   OpenGl_OCCT_FRONT_MATERIAL,
65   OpenGl_OCCT_BACK_MATERIAL,
66   OpenGl_OCCT_ALPHA_CUTOFF,
67   OpenGl_OCCT_COLOR,
68
69   // Weighted, Blended Order-Independent Transparency rendering state
70   OpenGl_OCCT_OIT_OUTPUT,
71   OpenGl_OCCT_OIT_DEPTH_FACTOR,
72
73   // Context-dependent state
74   OpenGl_OCCT_TEXTURE_TRSF2D,
75   OpenGl_OCCT_POINT_SIZE,
76
77   // DON'T MODIFY THIS ITEM (insert new items before it)
78   OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES
79 };
80
81 //! Interface for generic setter of user-defined uniform variables.
82 struct OpenGl_SetterInterface
83 {
84   //! Sets user-defined uniform variable to specified program.
85   virtual void Set (const Handle(OpenGl_Context)&           theCtx,
86                     const Handle(Graphic3d_ShaderVariable)& theVariable,
87                     OpenGl_ShaderProgram*                   theProgram) = 0;
88
89   //! Destructor
90   virtual ~OpenGl_SetterInterface() {}
91 };
92
93 //! List of OpenGL shader objects.
94 typedef NCollection_Sequence<Handle(OpenGl_ShaderObject)>    OpenGl_ShaderList;
95
96 //! List of shader variable setters.
97 typedef NCollection_DataMap<size_t, OpenGl_SetterInterface*> OpenGl_SetterList;
98
99 //! Support tool for setting user-defined uniform variables.
100 class OpenGl_VariableSetterSelector
101 {
102 public:
103
104   //! Creates new setter selector.
105   OpenGl_VariableSetterSelector();
106
107   //! Releases memory resources of setter selector.
108   ~OpenGl_VariableSetterSelector();
109
110   //! Sets user-defined uniform variable to specified program.
111   void Set (const Handle(OpenGl_Context)&           theCtx,
112             const Handle(Graphic3d_ShaderVariable)& theVariable,
113             OpenGl_ShaderProgram*                   theProgram) const;
114
115 private:
116
117   //! List of variable setters.
118   OpenGl_SetterList mySetterList;
119 };
120
121 //! Defines types of uniform state variables.
122 enum OpenGl_UniformStateType
123 {
124   OpenGl_LIGHT_SOURCES_STATE,
125   OpenGl_CLIP_PLANES_STATE,
126   OpenGl_MODEL_WORLD_STATE,
127   OpenGl_WORLD_VIEW_STATE,
128   OpenGl_PROJECTION_STATE,
129   OpenGl_MATERIAL_STATE,
130   OpenGl_SURF_DETAIL_STATE,
131   OpenGL_OIT_STATE,
132   OpenGl_UniformStateType_NB
133 };
134
135 //! Wrapper for OpenGL program object.
136 class OpenGl_ShaderProgram : public OpenGl_NamedResource
137 {
138   friend class OpenGl_View;
139   friend class OpenGl_ShaderManager;
140   DEFINE_STANDARD_RTTIEXT(OpenGl_ShaderProgram, OpenGl_NamedResource)
141 public:
142
143   //! Non-valid shader name.
144   static const GLuint NO_PROGRAM = 0;
145
146   //! Invalid location of uniform/attribute variable.
147   static const GLint INVALID_LOCATION = -1;
148
149   //! List of pre-defined OCCT state uniform variables.
150   static Standard_CString PredefinedKeywords[OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES];
151
152   //! Creates uninitialized shader program.
153   //!
154   //! WARNING! This constructor is not intended to be called anywhere but from OpenGl_ShaderManager::Create().
155   //! Manager has been designed to synchronize camera position, lights definition and other aspects of the program implicitly,
156   //! as well as sharing same program across rendering groups.
157   //!
158   //! Program created outside the manager will be left detached from these routines,
159   //! and them should be performed manually by caller.
160   //!
161   //! This constructor has been made public to provide more flexibility to re-use OCCT OpenGL classes without OCCT Viewer itself.
162   //! If this is not the case - create the program using shared OpenGl_ShaderManager instance instead.
163   Standard_EXPORT OpenGl_ShaderProgram (const Handle(Graphic3d_ShaderProgram)& theProxy = NULL);
164
165 protected:
166
167   static OpenGl_VariableSetterSelector mySetterSelector;
168
169 public:
170
171   //! Releases resources of shader program.
172   Standard_EXPORT virtual ~OpenGl_ShaderProgram();
173
174   //! Creates new empty shader program of specified type.
175   Standard_EXPORT Standard_Boolean Create (const Handle(OpenGl_Context)& theCtx);
176
177   //! Destroys shader program.
178   Standard_EXPORT virtual void Release (OpenGl_Context* theCtx) Standard_OVERRIDE;
179
180   //! Returns estimated GPU memory usage - cannot be easily estimated.
181   virtual Standard_Size EstimatedDataSize() const Standard_OVERRIDE { return 0; }
182
183   //! Attaches shader object to the program object.
184   Standard_EXPORT Standard_Boolean AttachShader (const Handle(OpenGl_Context)&      theCtx,
185                                                  const Handle(OpenGl_ShaderObject)& theShader);
186
187   //! Detaches shader object to the program object.
188   Standard_EXPORT Standard_Boolean DetachShader (const Handle(OpenGl_Context)&      theCtx,
189                                                  const Handle(OpenGl_ShaderObject)& theShader);
190
191   //! Initializes program object with the list of shader objects.
192   Standard_EXPORT Standard_Boolean Initialize (const Handle(OpenGl_Context)&     theCtx,
193                                                const Graphic3d_ShaderObjectList& theShaders);
194
195   //! Links the program object.
196   Standard_EXPORT Standard_Boolean Link (const Handle(OpenGl_Context)& theCtx);
197
198   //! Fetches information log of the last link operation.
199   Standard_EXPORT Standard_Boolean FetchInfoLog (const Handle(OpenGl_Context)& theCtx,
200                                                  TCollection_AsciiString&      theLog);
201
202   //! Fetches uniform variables from proxy shader program.
203   Standard_EXPORT Standard_Boolean ApplyVariables (const Handle(OpenGl_Context)& theCtx);
204
205   //! @return true if current object was initialized
206   inline bool IsValid() const
207   {
208     return myProgramID != NO_PROGRAM;
209   }
210
211   //! @return program ID
212   inline GLuint ProgramId() const
213   {
214     return myProgramID;
215   }
216
217 public:
218
219   //! Return TRUE if program defines tessellation stage.
220   Standard_Boolean HasTessellationStage() const { return myHasTessShader; }
221
222   //! Return the length of array of light sources (THE_MAX_LIGHTS),
223   //! to be used for initialization occLightSources (OpenGl_OCC_LIGHT_SOURCE_PARAMS).
224   Standard_Integer NbLightsMax() const { return myNbLightsMax; }
225
226   //! Return the length of array of clipping planes (THE_MAX_CLIP_PLANES),
227   //! to be used for initialization occClipPlaneEquations (OpenGl_OCC_CLIP_PLANE_EQUATIONS).
228   Standard_Integer NbClipPlanesMax() const { return myNbClipPlanesMax; }
229
230   //! Return the length of array of Fragment Shader outputs (THE_NB_FRAG_OUTPUTS),
231   //! to be used for initialization occFragColorArray/occFragColorN.
232   Standard_Integer NbFragmentOutputs() const { return myNbFragOutputs; }
233
234   //! Return true if Fragment Shader should perform alpha test; FALSE by default.
235   Standard_Boolean HasAlphaTest() const { return myHasAlphaTest; }
236
237   //! Return true if Fragment Shader color should output the weighted OIT coverage; FALSE by default.
238   Standard_Boolean HasWeightOitOutput() const { return myHasWeightOitOutput; }
239
240 private:
241
242   //! Returns index of last modification of variables of specified state type.
243   Standard_Size ActiveState (const OpenGl_UniformStateType theType) const
244   {
245     return theType < OpenGl_UniformStateType_NB
246          ? myCurrentState[theType]
247          : 0;
248   }
249
250   //! Updates index of last modification of variables of specified state type.
251   void UpdateState (const OpenGl_UniformStateType theType,
252                     const Standard_Size           theIndex)
253   {
254     if (theType < OpenGl_UniformStateType_NB)
255     {
256       myCurrentState[theType] = theIndex;
257     }
258   }
259
260 public:
261
262   //! Returns location of the specific uniform variable.
263   Standard_EXPORT GLint GetUniformLocation (const Handle(OpenGl_Context)& theCtx,
264                                             const GLchar*                 theName) const;
265
266   //! Returns index of the generic vertex attribute by variable name.
267   Standard_EXPORT GLint GetAttributeLocation (const Handle(OpenGl_Context)& theCtx,
268                                               const GLchar*                 theName) const;
269
270   //! Returns location of the OCCT state uniform variable.
271   Standard_EXPORT GLint GetStateLocation (const GLuint theVariable) const;
272
273 public:
274
275   //! Returns the value of the integer uniform variable.
276   Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
277                                                const GLchar*                 theName,
278                                                OpenGl_Vec4i&                 theValue) const;
279
280   Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
281                                                GLint                         theLocation,
282                                                OpenGl_Vec4i&                 theValue) const;
283
284   //! Returns the value of the float uniform variable.
285   Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
286                                                const GLchar*                 theName,
287                                                OpenGl_Vec4&                  theValue) const;
288
289   //! Returns the value of the float uniform variable.
290   Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
291                                                GLint                         theLocation,
292                                                OpenGl_Vec4&                  theValue) const;
293
294 public:
295
296   //! Returns the integer vertex attribute.
297   Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
298                                                  const GLchar*                 theName,
299                                                  OpenGl_Vec4i&                 theValue) const;
300
301   //! Returns the integer vertex attribute.
302   Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
303                                                  GLint                         theIndex,
304                                                  OpenGl_Vec4i&                 theValue) const;
305
306   //! Returns the float vertex attribute.
307   Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
308                                                  const GLchar*                 theName,
309                                                  OpenGl_Vec4&                  theValue) const;
310
311   //! Returns the float vertex attribute.
312   Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
313                                                  GLint                         theIndex,
314                                                  OpenGl_Vec4&                  theValue) const;
315
316 public:
317
318   //! Wrapper for glBindAttribLocation()
319   Standard_EXPORT Standard_Boolean SetAttributeName (const Handle(OpenGl_Context)& theCtx,
320                                                      GLint                         theIndex,
321                                                      const GLchar*                 theName);
322
323   //! Wrapper for glVertexAttrib1f()
324   Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
325                                                  const GLchar*                 theName,
326                                                  GLfloat                       theValue);
327
328   //! Wrapper for glVertexAttrib1f()
329   Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
330                                                  GLint                         theIndex,
331                                                  GLfloat                       theValue);
332
333   //! Wrapper for glVertexAttrib2fv()
334   Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
335                                                  const GLchar*                 theName,
336                                                  const OpenGl_Vec2&            theValue);
337
338   //! Wrapper for glVertexAttrib2fv()
339   Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
340                                                  GLint                         theIndex,
341                                                  const OpenGl_Vec2&            theValue);
342
343   //! Wrapper for glVertexAttrib3fv()
344   Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
345                                                  const GLchar*                 theName,
346                                                  const OpenGl_Vec3&            theValue);
347
348   //! Wrapper for glVertexAttrib3fv()
349   Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
350                                                  GLint                         theIndex,
351                                                  const OpenGl_Vec3&            theValue);
352
353   //! Wrapper for glVertexAttrib4fv()
354   Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
355                                                  const GLchar*                 theName,
356                                                  const OpenGl_Vec4&            theValue);
357
358   //! Wrapper for glVertexAttrib4fv()
359   Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
360                                                  GLint                         theIndex,
361                                                  const OpenGl_Vec4&            theValue);
362
363 public:
364
365   //! Specifies the value of the integer uniform variable.
366   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
367                                                const GLchar*                 theName,
368                                                GLint                         theValue);
369
370   //! Specifies the value of the integer uniform variable.
371   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
372                                                GLint                         theLocation,
373                                                GLint                         theValue);
374
375   //! Specifies the value of the integer uniform 2D vector.
376   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
377                                                const GLchar*                 theName,
378                                                const OpenGl_Vec2i&           theValue);
379
380   //! Specifies the value of the integer uniform 2D vector.
381   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
382                                                GLint                         theLocation,
383                                                const OpenGl_Vec2i&           theValue);
384
385   //! Specifies the value of the integer uniform 3D vector.
386   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
387                                                const GLchar*                 theName,
388                                                const OpenGl_Vec3i&           theValue);
389
390   //! Specifies the value of the integer uniform 3D vector.
391   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
392                                                GLint                         theLocation,
393                                                const OpenGl_Vec3i&           theValue);
394
395   //! Specifies the value of the integer uniform 4D vector.
396   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
397                                                const GLchar*                 theName,
398                                                const OpenGl_Vec4i&           theValue);
399
400   //! Specifies the value of the integer uniform 4D vector.
401   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
402                                                GLint                         theLocation,
403                                                const OpenGl_Vec4i&           theValue);
404
405 public:
406
407   //! Specifies the value of the unsigned integer uniform 2D vector (uvec2).
408   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
409                                                const GLchar*                 theName,
410                                                const OpenGl_Vec2u&           theValue);
411
412   //! Specifies the value of the unsigned integer uniform 2D vector (uvec2).
413   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
414                                                GLint                         theLocation,
415                                                const OpenGl_Vec2u&           theValue);
416
417   //! Specifies the value of the uvec2 uniform array
418   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
419                                                const GLchar*                 theName,
420                                                const GLsizei                 theCount,
421                                                const OpenGl_Vec2u*           theValue);
422
423   //! Specifies the value of the uvec2 uniform array
424   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
425                                                GLint                         theLocation,
426                                                const GLsizei                 theCount,
427                                                const OpenGl_Vec2u*           theValue);
428
429 public:
430
431   //! Specifies the value of the float uniform variable.
432   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
433                                                const GLchar*                 theName,
434                                                GLfloat                       theValue);
435
436   //! Specifies the value of the float uniform variable.
437   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
438                                                GLint                         theLocation,
439                                                GLfloat                       theValue);
440
441   //! Specifies the value of the float uniform 2D vector.
442   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
443                                                const GLchar*                 theName,
444                                                const OpenGl_Vec2&            theValue);
445
446   //! Specifies the value of the float uniform 2D vector.
447   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
448                                                GLint                         theLocation,
449                                                const OpenGl_Vec2&            theValue);
450
451   //! Specifies the value of the float uniform 3D vector.
452   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
453                                                const GLchar*                 theName,
454                                                const OpenGl_Vec3&            theValue);
455
456   //! Specifies the value of the float uniform 3D vector.
457   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
458                                                GLint                         theLocation,
459                                                const OpenGl_Vec3&            theValue);
460
461   //! Specifies the value of the float uniform 4D vector.
462   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
463                                                const GLchar*                 theName,
464                                                const OpenGl_Vec4&            theValue);
465
466   //! Specifies the value of the float uniform 4D vector.
467   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
468                                                GLint                         theLocation,
469                                                const OpenGl_Vec4&            theValue);
470
471 public:
472
473   //! Specifies the value of the float uniform 4x4 matrix.
474   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
475                                                const GLchar*                 theName,
476                                                const OpenGl_Mat4&            theValue,
477                                                GLboolean                     theTranspose = GL_FALSE);
478
479   //! Specifies the value of the float uniform 4x4 matrix.
480   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
481                                                GLint                         theLocation,
482                                                const OpenGl_Mat4&            theValue,
483                                                GLboolean                     theTranspose = GL_FALSE);
484
485   //! Specifies the value of the float uniform 4x4 matrix.
486   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
487                                                const GLchar*                 theName,
488                                                const OpenGl_Matrix&          theValue,
489                                                GLboolean                     theTranspose = GL_FALSE);
490
491   //! Specifies the value of the float uniform 4x4 matrix.
492   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
493                                                GLint                         theLocation,
494                                                const OpenGl_Matrix&          theValue,
495                                                GLboolean                     theTranspose = GL_FALSE);
496
497   //! Specifies the value of the float uniform array
498   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
499                                                GLint                         theLocation,
500                                                GLuint                        theCount,
501                                                const Standard_ShortReal*     theData);
502
503   //! Specifies the value of the float2 uniform array
504   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
505                                                GLint                         theLocation,
506                                                GLuint                        theCount,
507                                                const OpenGl_Vec2*            theData);
508
509   //! Specifies the value of the float3 uniform array
510   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
511                                                GLint                         theLocation,
512                                                GLuint                        theCount,
513                                                const OpenGl_Vec3*            theData);
514
515   //! Specifies the value of the float4 uniform array
516   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
517                                                GLint                         theLocation,
518                                                GLuint                        theCount,
519                                                const OpenGl_Vec4*            theData);
520
521   //! Specifies the value of the integer uniform array
522   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
523                                                GLint                         theLocation,
524                                                GLuint                        theCount,
525                                                const Standard_Integer*       theData);
526
527   //! Specifies the value of the int2 uniform array
528   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
529                                                GLint                         theLocation,
530                                                GLuint                        theCount,
531                                                const OpenGl_Vec2i*           theData);
532
533   //! Specifies the value of the int3 uniform array
534   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
535                                                GLint                         theLocation,
536                                                GLuint                        theCount,
537                                                const OpenGl_Vec3i*           theData);
538
539   //! Specifies the value of the int4 uniform array
540   Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
541                                                GLint                         theLocation,
542                                                GLuint                        theCount,
543                                                const OpenGl_Vec4i*           theData);
544
545 public:
546
547   //! Specifies the value of the sampler uniform variable.
548   Standard_EXPORT Standard_Boolean SetSampler (const Handle(OpenGl_Context)& theCtx,
549                                                const GLchar*                 theName,
550                                                const Graphic3d_TextureUnit   theTextureUnit);
551
552   //! Specifies the value of the sampler uniform variable.
553   Standard_EXPORT Standard_Boolean SetSampler (const Handle(OpenGl_Context)& theCtx,
554                                                GLint                         theLocation,
555                                                const Graphic3d_TextureUnit   theTextureUnit);
556
557 protected:
558
559   //! Increments counter of users.
560   //! Used by OpenGl_ShaderManager.
561   //! @return true when resource has been restored from delayed release queue
562   bool Share()
563   {
564     return ++myShareCount == 1;
565   }
566
567   //! Decrements counter of users.
568   //! Used by OpenGl_ShaderManager.
569   //! @return true when there are no more users of this program has been left
570   bool UnShare()
571   {
572     return --myShareCount == 0;
573   }
574
575 protected:
576
577   GLuint                          myProgramID;     //!< Handle of OpenGL shader program
578   OpenGl_ShaderList               myShaderObjects; //!< List of attached shader objects
579   Handle(Graphic3d_ShaderProgram) myProxy;         //!< Proxy shader program (from application layer)
580   Standard_Integer                myShareCount;    //!< program users count, initialized with 1 (already shared by one user)
581   Standard_Integer                myNbLightsMax;   //!< length of array of light sources (THE_MAX_LIGHTS)
582   Standard_Integer                myNbClipPlanesMax; //!< length of array of clipping planes (THE_MAX_CLIP_PLANES)
583   Standard_Integer                myNbFragOutputs; //!< length of array of Fragment Shader outputs (THE_NB_FRAG_OUTPUTS)
584   Standard_Boolean                myHasAlphaTest;  //!< flag indicating that Fragment Shader should perform alpha-test
585   Standard_Boolean                myHasWeightOitOutput; //!< flag indicating that Fragment Shader includes weighted OIT coverage
586   Standard_Boolean                myHasTessShader; //!< flag indicating that program defines tessellation stage
587
588 protected:
589
590   Standard_Size myCurrentState[OpenGl_UniformStateType_NB]; //!< defines last modification for variables of each state type
591
592   //! Stores locations of OCCT state uniform variables.
593   GLint myStateLocations[OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES];
594
595 };
596
597 template<class T>
598 struct OpenGl_VariableSetter : public OpenGl_SetterInterface
599 {
600   virtual void Set (const Handle(OpenGl_Context)&           theCtx,
601                     const Handle(Graphic3d_ShaderVariable)& theVariable,
602                     OpenGl_ShaderProgram*                   theProgram)
603   {
604     theProgram->SetUniform (theCtx,
605                             theVariable->Name().ToCString(),
606                             theVariable->Value()->As<T>());
607   }
608 };
609
610 namespace OpenGl_HashMapInitializer
611 {
612   template<class K, class V>
613   struct MapListOfType
614   {
615     NCollection_DataMap<K, V> myDictionary;
616
617     MapListOfType (K theKey, V theValue)
618     {
619       myDictionary.Bind (theKey, theValue);
620     }
621
622     MapListOfType& operator() (K theKey, V theValue)
623     {
624       myDictionary.Bind (theKey, theValue);
625       return *this;
626     }
627
628     operator const NCollection_DataMap<K, V>& () const
629     {
630       return myDictionary;
631     }
632   };
633
634   template<class K, class V>
635   MapListOfType<K, V> CreateListOf (K theKey, V theValue)
636   {
637     return MapListOfType<K, V> (theKey, theValue);
638   }
639 }
640
641 #endif // _OpenGl_ShaderProgram_Header