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