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