1 // Created on: 2013-09-19
2 // Created by: Denis BOGOLEPOV
3 // Copyright (c) 2013-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #ifndef _OpenGl_ShaderProgram_Header
17 #define _OpenGl_ShaderProgram_Header
19 #include <NCollection_DataMap.hxx>
20 #include <NCollection_Sequence.hxx>
21 #include <TCollection_AsciiString.hxx>
23 #include <Graphic3d_ShaderObject.hxx>
24 #include <Graphic3d_ShaderProgram.hxx>
26 #include <InterfaceGraphic_tgl_all.hxx>
28 #include <OpenGl_Vec.hxx>
29 #include <OpenGl_Matrix.hxx>
30 #include <OpenGl_ShaderObject.hxx>
32 //! The enumeration of OCCT-specific OpenGL/GLSL variables.
33 enum OpenGl_StateVariable
35 // OpenGL matrix state
36 OpenGl_OCC_MODEL_WORLD_MATRIX,
37 OpenGl_OCC_WORLD_VIEW_MATRIX,
38 OpenGl_OCC_PROJECTION_MATRIX,
39 OpenGl_OCC_MODEL_WORLD_MATRIX_INVERSE,
40 OpenGl_OCC_WORLD_VIEW_MATRIX_INVERSE,
41 OpenGl_OCC_PROJECTION_MATRIX_INVERSE,
42 OpenGl_OCC_MODEL_WORLD_MATRIX_TRANSPOSE,
43 OpenGl_OCC_WORLD_VIEW_MATRIX_TRANSPOSE,
44 OpenGl_OCC_PROJECTION_MATRIX_TRANSPOSE,
45 OpenGl_OCC_MODEL_WORLD_MATRIX_INVERSE_TRANSPOSE,
46 OpenGl_OCC_WORLD_VIEW_MATRIX_INVERSE_TRANSPOSE,
47 OpenGl_OCC_PROJECTION_MATRIX_INVERSE_TRANSPOSE,
49 // OpenGL clip planes state
50 OpenGl_OCC_CLIP_PLANE_EQUATIONS,
51 OpenGl_OCC_CLIP_PLANE_SPACES,
52 OpenGl_OCC_CLIP_PLANE_COUNT,
55 OpenGl_OCC_LIGHT_SOURCE_COUNT,
56 OpenGl_OCC_LIGHT_SOURCE_TYPES,
57 OpenGl_OCC_LIGHT_SOURCE_PARAMS,
58 OpenGl_OCC_LIGHT_AMBIENT,
61 OpenGl_OCCT_ACTIVE_SAMPLER,
62 OpenGl_OCCT_TEXTURE_ENABLE,
63 OpenGl_OCCT_DISTINGUISH_MODE,
64 OpenGl_OCCT_FRONT_MATERIAL,
65 OpenGl_OCCT_BACK_MATERIAL,
68 OpenGl_OCCT_POINT_SIZE,
70 // DON'T MODIFY THIS ITEM (insert new items before it)
71 OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES
74 class OpenGl_ShaderProgram;
76 //! Interface for generic setter of user-defined uniform variables.
77 struct OpenGl_SetterInterface
79 //! Sets user-defined uniform variable to specified program.
80 virtual void Set (const Handle(OpenGl_Context)& theCtx,
81 const Handle(Graphic3d_ShaderVariable)& theVariable,
82 OpenGl_ShaderProgram* theProgram) = 0;
85 virtual ~OpenGl_SetterInterface() {}
88 //! List of OpenGL shader objects.
89 typedef NCollection_Sequence<Handle(OpenGl_ShaderObject)> OpenGl_ShaderList;
91 //! List of shader variable setters.
92 typedef NCollection_DataMap<size_t, OpenGl_SetterInterface*> OpenGl_SetterList;
94 //! Support tool for setting user-defined uniform variables.
95 class OpenGl_VariableSetterSelector
99 //! Creates new setter selector.
100 OpenGl_VariableSetterSelector();
102 //! Releases memory resources of setter selector.
103 ~OpenGl_VariableSetterSelector();
105 //! Sets user-defined uniform variable to specified program.
106 void Set (const Handle(OpenGl_Context)& theCtx,
107 const Handle(Graphic3d_ShaderVariable)& theVariable,
108 OpenGl_ShaderProgram* theProgram) const;
112 //! List of variable setters.
113 OpenGl_SetterList mySetterList;
116 //! Defines types of uniform state variables.
117 enum OpenGl_UniformStateType
119 OpenGl_LIGHT_SOURCES_STATE,
120 OpenGl_CLIP_PLANES_STATE,
121 OpenGl_MODEL_WORLD_STATE,
122 OpenGl_WORLD_VIEW_STATE,
123 OpenGl_PROJECTION_STATE,
124 OpenGl_MATERIALS_STATE,
125 OpenGl_SURF_DETAIL_STATE
128 //! Total number of state types.
129 const int MaxStateTypes = 6;
131 //! Wrapper for OpenGL program object.
132 class OpenGl_ShaderProgram : public OpenGl_Resource
134 friend class OpenGl_View;
138 //! Non-valid shader name.
139 static const GLuint NO_PROGRAM = 0;
141 //! Invalid location of uniform/attribute variable.
142 static const GLint INVALID_LOCATION = -1;
144 //! List of pre-defined OCCT state uniform variables.
145 static Standard_CString PredefinedKeywords[OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES];
147 //! Creates uninitialized shader program.
149 //! WARNING! This constructor is not intended to be called anywhere but from OpenGl_ShaderManager::Create().
150 //! Manager has been designed to synchronize camera position, lights definition and other aspects of the program implicitly,
151 //! as well as sharing same program across rendering groups.
153 //! Program created outside the manager will be left detached from these routines,
154 //! and them should be performed manually by caller.
156 //! This constructor has been made public to provide more flexibility to re-use OCCT OpenGL classes without OCCT Viewer itself.
157 //! If this is not the case - create the program using shared OpenGl_ShaderManager instance instead.
158 Standard_EXPORT OpenGl_ShaderProgram (const Handle(Graphic3d_ShaderProgram)& theProxy = NULL);
162 static OpenGl_VariableSetterSelector mySetterSelector;
166 //! Releases resources of shader program.
167 Standard_EXPORT virtual ~OpenGl_ShaderProgram();
169 //! Creates new empty shader program of specified type.
170 Standard_EXPORT Standard_Boolean Create (const Handle(OpenGl_Context)& theCtx);
172 //! Destroys shader program.
173 Standard_EXPORT virtual void Release (OpenGl_Context* theCtx);
175 //! Attaches shader object to the program object.
176 Standard_EXPORT Standard_Boolean AttachShader (const Handle(OpenGl_Context)& theCtx,
177 const Handle(OpenGl_ShaderObject)& theShader);
179 //! Detaches shader object to the program object.
180 Standard_EXPORT Standard_Boolean DetachShader (const Handle(OpenGl_Context)& theCtx,
181 const Handle(OpenGl_ShaderObject)& theShader);
183 //! Initializes program object with the list of shader objects.
184 Standard_EXPORT Standard_Boolean Initialize (const Handle(OpenGl_Context)& theCtx,
185 const Graphic3d_ShaderObjectList& theShaders);
187 //! Links the program object.
188 Standard_EXPORT Standard_Boolean Link (const Handle(OpenGl_Context)& theCtx);
190 //! Fetches information log of the last link operation.
191 Standard_EXPORT Standard_Boolean FetchInfoLog (const Handle(OpenGl_Context)& theCtx,
192 TCollection_AsciiString& theLog);
194 //! Fetches uniform variables from proxy shader program.
195 Standard_EXPORT Standard_Boolean ApplyVariables (const Handle(OpenGl_Context)& theCtx);
197 //! @return true if current object was initialized
198 inline bool IsValid() const
200 return myProgramID != NO_PROGRAM;
203 //! @return program ID
204 inline GLuint ProgramId() const
211 //! Returns index of last modification of variables of specified state type.
212 Standard_EXPORT Standard_Size ActiveState (const OpenGl_UniformStateType theType) const;
214 //! Updates index of last modification of variables of specified state type.
215 Standard_EXPORT void UpdateState (const OpenGl_UniformStateType theType,
216 const Standard_Size theIndex);
220 //! Returns location of the specific uniform variable.
221 Standard_EXPORT GLint GetUniformLocation (const Handle(OpenGl_Context)& theCtx,
222 const GLchar* theName) const;
224 //! Returns index of the generic vertex attribute by variable name.
225 Standard_EXPORT GLint GetAttributeLocation (const Handle(OpenGl_Context)& theCtx,
226 const GLchar* theName) const;
228 //! Returns location of the OCCT state uniform variable.
229 Standard_EXPORT GLint GetStateLocation (const GLuint theVariable) const;
233 //! Returns the value of the integer uniform variable.
234 Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
235 const GLchar* theName,
236 OpenGl_Vec4i& theValue) const;
238 Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
240 OpenGl_Vec4i& theValue) const;
242 //! Returns the value of the float uniform variable.
243 Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
244 const GLchar* theName,
245 OpenGl_Vec4& theValue) const;
247 //! Returns the value of the float uniform variable.
248 Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
250 OpenGl_Vec4& theValue) const;
254 //! Returns the integer vertex attribute.
255 Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
256 const GLchar* theName,
257 OpenGl_Vec4i& theValue) const;
259 //! Returns the integer vertex attribute.
260 Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
262 OpenGl_Vec4i& theValue) const;
264 //! Returns the float vertex attribute.
265 Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
266 const GLchar* theName,
267 OpenGl_Vec4& theValue) const;
269 //! Returns the float vertex attribute.
270 Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
272 OpenGl_Vec4& theValue) const;
276 //! Wrapper for glBindAttribLocation()
277 Standard_EXPORT Standard_Boolean SetAttributeName (const Handle(OpenGl_Context)& theCtx,
279 const GLchar* theName);
281 //! Wrapper for glVertexAttrib1f()
282 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
283 const GLchar* theName,
286 //! Wrapper for glVertexAttrib1f()
287 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
291 //! Wrapper for glVertexAttrib2fv()
292 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
293 const GLchar* theName,
294 const OpenGl_Vec2& theValue);
296 //! Wrapper for glVertexAttrib2fv()
297 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
299 const OpenGl_Vec2& theValue);
301 //! Wrapper for glVertexAttrib3fv()
302 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
303 const GLchar* theName,
304 const OpenGl_Vec3& theValue);
306 //! Wrapper for glVertexAttrib3fv()
307 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
309 const OpenGl_Vec3& theValue);
311 //! Wrapper for glVertexAttrib4fv()
312 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
313 const GLchar* theName,
314 const OpenGl_Vec4& theValue);
316 //! Wrapper for glVertexAttrib4fv()
317 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
319 const OpenGl_Vec4& theValue);
323 //! Specifies the value of the integer uniform variable.
324 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
325 const GLchar* theName,
328 //! Specifies the value of the integer uniform variable.
329 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
333 //! Specifies the value of the integer uniform 2D vector.
334 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
335 const GLchar* theName,
336 const OpenGl_Vec2i& theValue);
338 //! Specifies the value of the integer uniform 2D vector.
339 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
341 const OpenGl_Vec2i& theValue);
343 //! Specifies the value of the integer uniform 3D vector.
344 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
345 const GLchar* theName,
346 const OpenGl_Vec3i& theValue);
348 //! Specifies the value of the integer uniform 3D vector.
349 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
351 const OpenGl_Vec3i& theValue);
353 //! Specifies the value of the integer uniform 4D vector.
354 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
355 const GLchar* theName,
356 const OpenGl_Vec4i& theValue);
358 //! Specifies the value of the integer uniform 4D vector.
359 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
361 const OpenGl_Vec4i& theValue);
365 //! Specifies the value of the 64-bit unsigned integer uniform variable.
366 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
367 const GLchar* theName,
370 //! Specifies the value of the 64-bit unsigned integer uniform variable.
371 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
375 //! Specifies the value of the 64-bit unsigned integer uniform array.
376 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
377 const GLchar* theName,
378 const GLsizei theCount,
379 const GLuint64* theValue);
381 //! Specifies the value of the 64-bit unsigned integer uniform array.
382 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
384 const GLsizei theCount,
385 const GLuint64* theValue);
389 //! Specifies the value of the float uniform variable.
390 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
391 const GLchar* theName,
394 //! Specifies the value of the float uniform variable.
395 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
399 //! Specifies the value of the float uniform 2D vector.
400 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
401 const GLchar* theName,
402 const OpenGl_Vec2& theValue);
404 //! Specifies the value of the float uniform 2D vector.
405 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
407 const OpenGl_Vec2& theValue);
409 //! Specifies the value of the float uniform 3D vector.
410 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
411 const GLchar* theName,
412 const OpenGl_Vec3& theValue);
414 //! Specifies the value of the float uniform 3D vector.
415 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
417 const OpenGl_Vec3& theValue);
419 //! Specifies the value of the float uniform 4D vector.
420 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
421 const GLchar* theName,
422 const OpenGl_Vec4& theValue);
424 //! Specifies the value of the float uniform 4D vector.
425 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
427 const OpenGl_Vec4& theValue);
431 //! Specifies the value of the float uniform 4x4 matrix.
432 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
433 const GLchar* theName,
434 const OpenGl_Mat4& theValue,
435 GLboolean theTranspose = GL_FALSE);
437 //! Specifies the value of the float uniform 4x4 matrix.
438 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
440 const OpenGl_Mat4& theValue,
441 GLboolean theTranspose = GL_FALSE);
443 //! Specifies the value of the float uniform 4x4 matrix.
444 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
445 const GLchar* theName,
446 const OpenGl_Matrix& theValue,
447 GLboolean theTranspose = GL_FALSE);
449 //! Specifies the value of the float uniform 4x4 matrix.
450 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
452 const OpenGl_Matrix& theValue,
453 GLboolean theTranspose = GL_FALSE);
455 //! Specifies the value of the float uniform array
456 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
459 const Standard_ShortReal* theData);
461 //! Specifies the value of the float2 uniform array
462 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
465 const OpenGl_Vec2* theData);
467 //! Specifies the value of the float3 uniform array
468 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
471 const OpenGl_Vec3* theData);
473 //! Specifies the value of the float4 uniform array
474 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
477 const OpenGl_Vec4* theData);
479 //! Specifies the value of the integer uniform array
480 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
483 const Standard_Integer* theData);
485 //! Specifies the value of the int2 uniform array
486 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
489 const OpenGl_Vec2i* theData);
491 //! Specifies the value of the int3 uniform array
492 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
495 const OpenGl_Vec3i* theData);
497 //! Specifies the value of the int4 uniform array
498 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
501 const OpenGl_Vec4i* theData);
505 //! Specifies the value of the sampler uniform variable.
506 Standard_EXPORT Standard_Boolean SetSampler (const Handle(OpenGl_Context)& theCtx,
507 const GLchar* theName,
508 const GLenum theTextureUnit);
510 //! Specifies the value of the sampler uniform variable.
511 Standard_EXPORT Standard_Boolean SetSampler (const Handle(OpenGl_Context)& theCtx,
513 const GLenum theTextureUnit);
517 //! Increments counter of users.
518 //! Used by OpenGl_ShaderManager.
519 //! @return true when resource has been restored from delayed release queue
522 return ++myShareCount == 1;
525 //! Decrements counter of users.
526 //! Used by OpenGl_ShaderManager.
527 //! @return true when there are no more users of this program has been left
530 return --myShareCount == 0;
535 GLuint myProgramID; //!< Handle of OpenGL shader program
536 OpenGl_ShaderList myShaderObjects; //!< List of attached shader objects
537 Handle(Graphic3d_ShaderProgram) myProxy; //!< Proxy shader program (from application layer)
538 Standard_Integer myShareCount; //!< program users count, initialized with 1 (already shared by one user)
542 Standard_Size myCurrentState[MaxStateTypes]; //!< defines last modification for variables of each state type
544 //! Stores locations of OCCT state uniform variables.
545 GLint myStateLocations[OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES];
549 DEFINE_STANDARD_RTTI (OpenGl_ShaderProgram, OpenGl_Resource)
550 friend class OpenGl_ShaderManager;
555 struct OpenGl_VariableSetter : public OpenGl_SetterInterface
557 virtual void Set (const Handle(OpenGl_Context)& theCtx,
558 const Handle(Graphic3d_ShaderVariable)& theVariable,
559 OpenGl_ShaderProgram* theProgram)
561 theProgram->SetUniform (theCtx,
562 theVariable->Name().ToCString(),
563 theVariable->Value()->As<T>());
567 namespace OpenGl_HashMapInitializer
569 template<class K, class V>
572 NCollection_DataMap<K, V> myDictionary;
574 MapListOfType (K theKey, V theValue)
576 myDictionary.Bind (theKey, theValue);
579 MapListOfType& operator() (K theKey, V theValue)
581 myDictionary.Bind (theKey, theValue);
585 operator const NCollection_DataMap<K, V>& () const
591 template<class K, class V>
592 MapListOfType<K, V> CreateListOf (K theKey, V theValue)
594 return MapListOfType<K, V> (theKey, theValue);
598 #endif // _OpenGl_ShaderProgram_Header