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 class OpenGl_ShaderProgram;
33 DEFINE_STANDARD_HANDLE(OpenGl_ShaderProgram, OpenGl_Resource)
35 //! The enumeration of OCCT-specific OpenGL/GLSL variables.
36 enum OpenGl_StateVariable
38 // OpenGL matrix state
39 OpenGl_OCC_MODEL_WORLD_MATRIX,
40 OpenGl_OCC_WORLD_VIEW_MATRIX,
41 OpenGl_OCC_PROJECTION_MATRIX,
42 OpenGl_OCC_MODEL_WORLD_MATRIX_INVERSE,
43 OpenGl_OCC_WORLD_VIEW_MATRIX_INVERSE,
44 OpenGl_OCC_PROJECTION_MATRIX_INVERSE,
45 OpenGl_OCC_MODEL_WORLD_MATRIX_TRANSPOSE,
46 OpenGl_OCC_WORLD_VIEW_MATRIX_TRANSPOSE,
47 OpenGl_OCC_PROJECTION_MATRIX_TRANSPOSE,
48 OpenGl_OCC_MODEL_WORLD_MATRIX_INVERSE_TRANSPOSE,
49 OpenGl_OCC_WORLD_VIEW_MATRIX_INVERSE_TRANSPOSE,
50 OpenGl_OCC_PROJECTION_MATRIX_INVERSE_TRANSPOSE,
52 // OpenGL clip planes state
53 OpenGl_OCC_CLIP_PLANE_EQUATIONS,
54 OpenGl_OCC_CLIP_PLANE_SPACES,
55 OpenGl_OCC_CLIP_PLANE_COUNT,
58 OpenGl_OCC_LIGHT_SOURCE_COUNT,
59 OpenGl_OCC_LIGHT_SOURCE_TYPES,
60 OpenGl_OCC_LIGHT_SOURCE_PARAMS,
61 OpenGl_OCC_LIGHT_AMBIENT,
64 OpenGl_OCCT_ACTIVE_SAMPLER,
65 OpenGl_OCCT_TEXTURE_ENABLE,
66 OpenGl_OCCT_DISTINGUISH_MODE,
67 OpenGl_OCCT_FRONT_MATERIAL,
68 OpenGl_OCCT_BACK_MATERIAL,
71 OpenGl_OCCT_POINT_SIZE,
73 // DON'T MODIFY THIS ITEM (insert new items before it)
74 OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES
77 class OpenGl_ShaderProgram;
79 //! Interface for generic setter of user-defined uniform variables.
80 struct OpenGl_SetterInterface
82 //! Sets user-defined uniform variable to specified program.
83 virtual void Set (const Handle(OpenGl_Context)& theCtx,
84 const Handle(Graphic3d_ShaderVariable)& theVariable,
85 OpenGl_ShaderProgram* theProgram) = 0;
88 virtual ~OpenGl_SetterInterface() {}
91 //! List of OpenGL shader objects.
92 typedef NCollection_Sequence<Handle(OpenGl_ShaderObject)> OpenGl_ShaderList;
94 //! List of shader variable setters.
95 typedef NCollection_DataMap<size_t, OpenGl_SetterInterface*> OpenGl_SetterList;
97 //! Support tool for setting user-defined uniform variables.
98 class OpenGl_VariableSetterSelector
102 //! Creates new setter selector.
103 OpenGl_VariableSetterSelector();
105 //! Releases memory resources of setter selector.
106 ~OpenGl_VariableSetterSelector();
108 //! Sets user-defined uniform variable to specified program.
109 void Set (const Handle(OpenGl_Context)& theCtx,
110 const Handle(Graphic3d_ShaderVariable)& theVariable,
111 OpenGl_ShaderProgram* theProgram) const;
115 //! List of variable setters.
116 OpenGl_SetterList mySetterList;
119 //! Defines types of uniform state variables.
120 enum OpenGl_UniformStateType
122 OpenGl_LIGHT_SOURCES_STATE,
123 OpenGl_CLIP_PLANES_STATE,
124 OpenGl_MODEL_WORLD_STATE,
125 OpenGl_WORLD_VIEW_STATE,
126 OpenGl_PROJECTION_STATE,
127 OpenGl_MATERIALS_STATE,
128 OpenGl_SURF_DETAIL_STATE
131 //! Total number of state types.
132 const int MaxStateTypes = 6;
134 //! Wrapper for OpenGL program object.
135 class OpenGl_ShaderProgram : public OpenGl_Resource
137 friend class OpenGl_View;
141 //! Non-valid shader name.
142 static const GLuint NO_PROGRAM = 0;
144 //! Invalid location of uniform/attribute variable.
145 static const GLint INVALID_LOCATION = -1;
147 //! List of pre-defined OCCT state uniform variables.
148 static Standard_CString PredefinedKeywords[OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES];
150 //! Creates uninitialized shader program.
152 //! WARNING! This constructor is not intended to be called anywhere but from OpenGl_ShaderManager::Create().
153 //! Manager has been designed to synchronize camera position, lights definition and other aspects of the program implicitly,
154 //! as well as sharing same program across rendering groups.
156 //! Program created outside the manager will be left detached from these routines,
157 //! and them should be performed manually by caller.
159 //! This constructor has been made public to provide more flexibility to re-use OCCT OpenGL classes without OCCT Viewer itself.
160 //! If this is not the case - create the program using shared OpenGl_ShaderManager instance instead.
161 Standard_EXPORT OpenGl_ShaderProgram (const Handle(Graphic3d_ShaderProgram)& theProxy = NULL);
165 static OpenGl_VariableSetterSelector mySetterSelector;
169 //! Releases resources of shader program.
170 Standard_EXPORT virtual ~OpenGl_ShaderProgram();
172 //! Creates new empty shader program of specified type.
173 Standard_EXPORT Standard_Boolean Create (const Handle(OpenGl_Context)& theCtx);
175 //! Destroys shader program.
176 Standard_EXPORT virtual void Release (OpenGl_Context* theCtx);
178 //! Attaches shader object to the program object.
179 Standard_EXPORT Standard_Boolean AttachShader (const Handle(OpenGl_Context)& theCtx,
180 const Handle(OpenGl_ShaderObject)& theShader);
182 //! Detaches shader object to the program object.
183 Standard_EXPORT Standard_Boolean DetachShader (const Handle(OpenGl_Context)& theCtx,
184 const Handle(OpenGl_ShaderObject)& theShader);
186 //! Initializes program object with the list of shader objects.
187 Standard_EXPORT Standard_Boolean Initialize (const Handle(OpenGl_Context)& theCtx,
188 const Graphic3d_ShaderObjectList& theShaders);
190 //! Links the program object.
191 Standard_EXPORT Standard_Boolean Link (const Handle(OpenGl_Context)& theCtx);
193 //! Fetches information log of the last link operation.
194 Standard_EXPORT Standard_Boolean FetchInfoLog (const Handle(OpenGl_Context)& theCtx,
195 TCollection_AsciiString& theLog);
197 //! Fetches uniform variables from proxy shader program.
198 Standard_EXPORT Standard_Boolean ApplyVariables (const Handle(OpenGl_Context)& theCtx);
200 //! @return true if current object was initialized
201 inline bool IsValid() const
203 return myProgramID != NO_PROGRAM;
206 //! @return program ID
207 inline GLuint ProgramId() const
214 //! Returns index of last modification of variables of specified state type.
215 Standard_EXPORT Standard_Size ActiveState (const OpenGl_UniformStateType theType) const;
217 //! Updates index of last modification of variables of specified state type.
218 Standard_EXPORT void UpdateState (const OpenGl_UniformStateType theType,
219 const Standard_Size theIndex);
223 //! Returns location of the specific uniform variable.
224 Standard_EXPORT GLint GetUniformLocation (const Handle(OpenGl_Context)& theCtx,
225 const GLchar* theName) const;
227 //! Returns index of the generic vertex attribute by variable name.
228 Standard_EXPORT GLint GetAttributeLocation (const Handle(OpenGl_Context)& theCtx,
229 const GLchar* theName) const;
231 //! Returns location of the OCCT state uniform variable.
232 Standard_EXPORT GLint GetStateLocation (const GLuint theVariable) const;
236 //! Returns the value of the integer uniform variable.
237 Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
238 const GLchar* theName,
239 OpenGl_Vec4i& theValue) const;
241 Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
243 OpenGl_Vec4i& theValue) const;
245 //! Returns the value of the float uniform variable.
246 Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
247 const GLchar* theName,
248 OpenGl_Vec4& theValue) const;
250 //! Returns the value of the float uniform variable.
251 Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
253 OpenGl_Vec4& theValue) const;
257 //! Returns the integer vertex attribute.
258 Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
259 const GLchar* theName,
260 OpenGl_Vec4i& theValue) const;
262 //! Returns the integer vertex attribute.
263 Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
265 OpenGl_Vec4i& theValue) const;
267 //! Returns the float vertex attribute.
268 Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
269 const GLchar* theName,
270 OpenGl_Vec4& theValue) const;
272 //! Returns the float vertex attribute.
273 Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
275 OpenGl_Vec4& theValue) const;
279 //! Wrapper for glBindAttribLocation()
280 Standard_EXPORT Standard_Boolean SetAttributeName (const Handle(OpenGl_Context)& theCtx,
282 const GLchar* theName);
284 //! Wrapper for glVertexAttrib1f()
285 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
286 const GLchar* theName,
289 //! Wrapper for glVertexAttrib1f()
290 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
294 //! Wrapper for glVertexAttrib2fv()
295 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
296 const GLchar* theName,
297 const OpenGl_Vec2& theValue);
299 //! Wrapper for glVertexAttrib2fv()
300 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
302 const OpenGl_Vec2& theValue);
304 //! Wrapper for glVertexAttrib3fv()
305 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
306 const GLchar* theName,
307 const OpenGl_Vec3& theValue);
309 //! Wrapper for glVertexAttrib3fv()
310 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
312 const OpenGl_Vec3& theValue);
314 //! Wrapper for glVertexAttrib4fv()
315 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
316 const GLchar* theName,
317 const OpenGl_Vec4& theValue);
319 //! Wrapper for glVertexAttrib4fv()
320 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
322 const OpenGl_Vec4& theValue);
326 //! Specifies the value of the integer uniform variable.
327 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
328 const GLchar* theName,
331 //! Specifies the value of the integer uniform variable.
332 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
336 //! Specifies the value of the integer uniform 2D vector.
337 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
338 const GLchar* theName,
339 const OpenGl_Vec2i& theValue);
341 //! Specifies the value of the integer uniform 2D vector.
342 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
344 const OpenGl_Vec2i& theValue);
346 //! Specifies the value of the integer uniform 3D vector.
347 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
348 const GLchar* theName,
349 const OpenGl_Vec3i& theValue);
351 //! Specifies the value of the integer uniform 3D vector.
352 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
354 const OpenGl_Vec3i& theValue);
356 //! Specifies the value of the integer uniform 4D vector.
357 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
358 const GLchar* theName,
359 const OpenGl_Vec4i& theValue);
361 //! Specifies the value of the integer uniform 4D vector.
362 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
364 const OpenGl_Vec4i& theValue);
368 //! Specifies the value of the 64-bit unsigned integer uniform variable.
369 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
370 const GLchar* theName,
373 //! Specifies the value of the 64-bit unsigned integer uniform variable.
374 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
378 //! Specifies the value of the 64-bit unsigned integer uniform array.
379 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
380 const GLchar* theName,
381 const GLsizei theCount,
382 const GLuint64* theValue);
384 //! Specifies the value of the 64-bit unsigned integer uniform array.
385 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
387 const GLsizei theCount,
388 const GLuint64* theValue);
392 //! Specifies the value of the float uniform variable.
393 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
394 const GLchar* theName,
397 //! Specifies the value of the float uniform variable.
398 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
402 //! Specifies the value of the float uniform 2D vector.
403 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
404 const GLchar* theName,
405 const OpenGl_Vec2& theValue);
407 //! Specifies the value of the float uniform 2D vector.
408 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
410 const OpenGl_Vec2& theValue);
412 //! Specifies the value of the float uniform 3D vector.
413 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
414 const GLchar* theName,
415 const OpenGl_Vec3& theValue);
417 //! Specifies the value of the float uniform 3D vector.
418 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
420 const OpenGl_Vec3& theValue);
422 //! Specifies the value of the float uniform 4D vector.
423 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
424 const GLchar* theName,
425 const OpenGl_Vec4& theValue);
427 //! Specifies the value of the float uniform 4D vector.
428 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
430 const OpenGl_Vec4& theValue);
434 //! Specifies the value of the float uniform 4x4 matrix.
435 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
436 const GLchar* theName,
437 const OpenGl_Mat4& theValue,
438 GLboolean theTranspose = GL_FALSE);
440 //! Specifies the value of the float uniform 4x4 matrix.
441 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
443 const OpenGl_Mat4& theValue,
444 GLboolean theTranspose = GL_FALSE);
446 //! Specifies the value of the float uniform 4x4 matrix.
447 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
448 const GLchar* theName,
449 const OpenGl_Matrix& theValue,
450 GLboolean theTranspose = GL_FALSE);
452 //! Specifies the value of the float uniform 4x4 matrix.
453 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
455 const OpenGl_Matrix& theValue,
456 GLboolean theTranspose = GL_FALSE);
458 //! Specifies the value of the float uniform array
459 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
462 const Standard_ShortReal* theData);
464 //! Specifies the value of the float2 uniform array
465 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
468 const OpenGl_Vec2* theData);
470 //! Specifies the value of the float3 uniform array
471 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
474 const OpenGl_Vec3* theData);
476 //! Specifies the value of the float4 uniform array
477 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
480 const OpenGl_Vec4* theData);
482 //! Specifies the value of the integer uniform array
483 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
486 const Standard_Integer* theData);
488 //! Specifies the value of the int2 uniform array
489 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
492 const OpenGl_Vec2i* theData);
494 //! Specifies the value of the int3 uniform array
495 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
498 const OpenGl_Vec3i* theData);
500 //! Specifies the value of the int4 uniform array
501 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
504 const OpenGl_Vec4i* theData);
508 //! Specifies the value of the sampler uniform variable.
509 Standard_EXPORT Standard_Boolean SetSampler (const Handle(OpenGl_Context)& theCtx,
510 const GLchar* theName,
511 const GLenum theTextureUnit);
513 //! Specifies the value of the sampler uniform variable.
514 Standard_EXPORT Standard_Boolean SetSampler (const Handle(OpenGl_Context)& theCtx,
516 const GLenum theTextureUnit);
520 //! Increments counter of users.
521 //! Used by OpenGl_ShaderManager.
522 //! @return true when resource has been restored from delayed release queue
525 return ++myShareCount == 1;
528 //! Decrements counter of users.
529 //! Used by OpenGl_ShaderManager.
530 //! @return true when there are no more users of this program has been left
533 return --myShareCount == 0;
538 GLuint myProgramID; //!< Handle of OpenGL shader program
539 OpenGl_ShaderList myShaderObjects; //!< List of attached shader objects
540 Handle(Graphic3d_ShaderProgram) myProxy; //!< Proxy shader program (from application layer)
541 Standard_Integer myShareCount; //!< program users count, initialized with 1 (already shared by one user)
545 Standard_Size myCurrentState[MaxStateTypes]; //!< defines last modification for variables of each state type
547 //! Stores locations of OCCT state uniform variables.
548 GLint myStateLocations[OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES];
552 DEFINE_STANDARD_RTTI (OpenGl_ShaderProgram, OpenGl_Resource)
553 friend class OpenGl_ShaderManager;
558 struct OpenGl_VariableSetter : public OpenGl_SetterInterface
560 virtual void Set (const Handle(OpenGl_Context)& theCtx,
561 const Handle(Graphic3d_ShaderVariable)& theVariable,
562 OpenGl_ShaderProgram* theProgram)
564 theProgram->SetUniform (theCtx,
565 theVariable->Name().ToCString(),
566 theVariable->Value()->As<T>());
570 namespace OpenGl_HashMapInitializer
572 template<class K, class V>
575 NCollection_DataMap<K, V> myDictionary;
577 MapListOfType (K theKey, V theValue)
579 myDictionary.Bind (theKey, theValue);
582 MapListOfType& operator() (K theKey, V theValue)
584 myDictionary.Bind (theKey, theValue);
588 operator const NCollection_DataMap<K, V>& () const
594 template<class K, class V>
595 MapListOfType<K, V> CreateListOf (K theKey, V theValue)
597 return MapListOfType<K, V> (theKey, theValue);
601 #endif // _OpenGl_ShaderProgram_Header