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 <OpenGl_Vec.hxx>
27 #include <OpenGl_Matrix.hxx>
28 #include <OpenGl_ShaderObject.hxx>
30 class OpenGl_ShaderProgram;
31 DEFINE_STANDARD_HANDLE(OpenGl_ShaderProgram, OpenGl_Resource)
33 //! The enumeration of OCCT-specific OpenGL/GLSL variables.
34 enum OpenGl_StateVariable
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,
50 // OpenGL clip planes state
51 OpenGl_OCC_CLIP_PLANE_EQUATIONS,
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_TEXTURE_TRSF2D,
69 OpenGl_OCCT_POINT_SIZE,
71 // DON'T MODIFY THIS ITEM (insert new items before it)
72 OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES
75 class OpenGl_ShaderProgram;
77 //! Interface for generic setter of user-defined uniform variables.
78 struct OpenGl_SetterInterface
80 //! Sets user-defined uniform variable to specified program.
81 virtual void Set (const Handle(OpenGl_Context)& theCtx,
82 const Handle(Graphic3d_ShaderVariable)& theVariable,
83 OpenGl_ShaderProgram* theProgram) = 0;
86 virtual ~OpenGl_SetterInterface() {}
89 //! List of OpenGL shader objects.
90 typedef NCollection_Sequence<Handle(OpenGl_ShaderObject)> OpenGl_ShaderList;
92 //! List of shader variable setters.
93 typedef NCollection_DataMap<size_t, OpenGl_SetterInterface*> OpenGl_SetterList;
95 //! Support tool for setting user-defined uniform variables.
96 class OpenGl_VariableSetterSelector
100 //! Creates new setter selector.
101 OpenGl_VariableSetterSelector();
103 //! Releases memory resources of setter selector.
104 ~OpenGl_VariableSetterSelector();
106 //! Sets user-defined uniform variable to specified program.
107 void Set (const Handle(OpenGl_Context)& theCtx,
108 const Handle(Graphic3d_ShaderVariable)& theVariable,
109 OpenGl_ShaderProgram* theProgram) const;
113 //! List of variable setters.
114 OpenGl_SetterList mySetterList;
117 //! Defines types of uniform state variables.
118 enum OpenGl_UniformStateType
120 OpenGl_LIGHT_SOURCES_STATE,
121 OpenGl_CLIP_PLANES_STATE,
122 OpenGl_MODEL_WORLD_STATE,
123 OpenGl_WORLD_VIEW_STATE,
124 OpenGl_PROJECTION_STATE,
125 OpenGl_MATERIALS_STATE,
126 OpenGl_SURF_DETAIL_STATE
129 //! Total number of state types.
130 const int MaxStateTypes = 6;
132 //! Wrapper for OpenGL program object.
133 class OpenGl_ShaderProgram : public OpenGl_Resource
135 friend class OpenGl_View;
139 //! Non-valid shader name.
140 static const GLuint NO_PROGRAM = 0;
142 //! Invalid location of uniform/attribute variable.
143 static const GLint INVALID_LOCATION = -1;
145 //! List of pre-defined OCCT state uniform variables.
146 static Standard_CString PredefinedKeywords[OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES];
148 //! Creates uninitialized shader program.
150 //! WARNING! This constructor is not intended to be called anywhere but from OpenGl_ShaderManager::Create().
151 //! Manager has been designed to synchronize camera position, lights definition and other aspects of the program implicitly,
152 //! as well as sharing same program across rendering groups.
154 //! Program created outside the manager will be left detached from these routines,
155 //! and them should be performed manually by caller.
157 //! This constructor has been made public to provide more flexibility to re-use OCCT OpenGL classes without OCCT Viewer itself.
158 //! If this is not the case - create the program using shared OpenGl_ShaderManager instance instead.
159 Standard_EXPORT OpenGl_ShaderProgram (const Handle(Graphic3d_ShaderProgram)& theProxy = NULL);
163 static OpenGl_VariableSetterSelector mySetterSelector;
167 //! Releases resources of shader program.
168 Standard_EXPORT virtual ~OpenGl_ShaderProgram();
170 //! Creates new empty shader program of specified type.
171 Standard_EXPORT Standard_Boolean Create (const Handle(OpenGl_Context)& theCtx);
173 //! Destroys shader program.
174 Standard_EXPORT virtual void Release (OpenGl_Context* theCtx) Standard_OVERRIDE;
176 //! Attaches shader object to the program object.
177 Standard_EXPORT Standard_Boolean AttachShader (const Handle(OpenGl_Context)& theCtx,
178 const Handle(OpenGl_ShaderObject)& theShader);
180 //! Detaches shader object to the program object.
181 Standard_EXPORT Standard_Boolean DetachShader (const Handle(OpenGl_Context)& theCtx,
182 const Handle(OpenGl_ShaderObject)& theShader);
184 //! Initializes program object with the list of shader objects.
185 Standard_EXPORT Standard_Boolean Initialize (const Handle(OpenGl_Context)& theCtx,
186 const Graphic3d_ShaderObjectList& theShaders);
188 //! Links the program object.
189 Standard_EXPORT Standard_Boolean Link (const Handle(OpenGl_Context)& theCtx);
191 //! Fetches information log of the last link operation.
192 Standard_EXPORT Standard_Boolean FetchInfoLog (const Handle(OpenGl_Context)& theCtx,
193 TCollection_AsciiString& theLog);
195 //! Fetches uniform variables from proxy shader program.
196 Standard_EXPORT Standard_Boolean ApplyVariables (const Handle(OpenGl_Context)& theCtx);
198 //! @return true if current object was initialized
199 inline bool IsValid() const
201 return myProgramID != NO_PROGRAM;
204 //! @return program ID
205 inline GLuint ProgramId() const
212 //! Returns index of last modification of variables of specified state type.
213 Standard_EXPORT Standard_Size ActiveState (const OpenGl_UniformStateType theType) const;
215 //! Updates index of last modification of variables of specified state type.
216 Standard_EXPORT void UpdateState (const OpenGl_UniformStateType theType,
217 const Standard_Size theIndex);
221 //! Returns location of the specific uniform variable.
222 Standard_EXPORT GLint GetUniformLocation (const Handle(OpenGl_Context)& theCtx,
223 const GLchar* theName) const;
225 //! Returns index of the generic vertex attribute by variable name.
226 Standard_EXPORT GLint GetAttributeLocation (const Handle(OpenGl_Context)& theCtx,
227 const GLchar* theName) const;
229 //! Returns location of the OCCT state uniform variable.
230 Standard_EXPORT GLint GetStateLocation (const GLuint theVariable) const;
234 //! Returns the value of the integer uniform variable.
235 Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
236 const GLchar* theName,
237 OpenGl_Vec4i& theValue) const;
239 Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
241 OpenGl_Vec4i& theValue) const;
243 //! Returns the value of the float uniform variable.
244 Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
245 const GLchar* theName,
246 OpenGl_Vec4& theValue) const;
248 //! Returns the value of the float uniform variable.
249 Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
251 OpenGl_Vec4& theValue) const;
255 //! Returns the integer vertex attribute.
256 Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
257 const GLchar* theName,
258 OpenGl_Vec4i& theValue) const;
260 //! Returns the integer vertex attribute.
261 Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
263 OpenGl_Vec4i& theValue) const;
265 //! Returns the float vertex attribute.
266 Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
267 const GLchar* theName,
268 OpenGl_Vec4& theValue) const;
270 //! Returns the float vertex attribute.
271 Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
273 OpenGl_Vec4& theValue) const;
277 //! Wrapper for glBindAttribLocation()
278 Standard_EXPORT Standard_Boolean SetAttributeName (const Handle(OpenGl_Context)& theCtx,
280 const GLchar* theName);
282 //! Wrapper for glVertexAttrib1f()
283 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
284 const GLchar* theName,
287 //! Wrapper for glVertexAttrib1f()
288 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
292 //! Wrapper for glVertexAttrib2fv()
293 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
294 const GLchar* theName,
295 const OpenGl_Vec2& theValue);
297 //! Wrapper for glVertexAttrib2fv()
298 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
300 const OpenGl_Vec2& theValue);
302 //! Wrapper for glVertexAttrib3fv()
303 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
304 const GLchar* theName,
305 const OpenGl_Vec3& theValue);
307 //! Wrapper for glVertexAttrib3fv()
308 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
310 const OpenGl_Vec3& theValue);
312 //! Wrapper for glVertexAttrib4fv()
313 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
314 const GLchar* theName,
315 const OpenGl_Vec4& theValue);
317 //! Wrapper for glVertexAttrib4fv()
318 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
320 const OpenGl_Vec4& theValue);
324 //! Specifies the value of the integer uniform variable.
325 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
326 const GLchar* theName,
329 //! Specifies the value of the integer uniform variable.
330 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
334 //! Specifies the value of the integer uniform 2D vector.
335 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
336 const GLchar* theName,
337 const OpenGl_Vec2i& theValue);
339 //! Specifies the value of the integer uniform 2D vector.
340 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
342 const OpenGl_Vec2i& theValue);
344 //! Specifies the value of the integer uniform 3D vector.
345 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
346 const GLchar* theName,
347 const OpenGl_Vec3i& theValue);
349 //! Specifies the value of the integer uniform 3D vector.
350 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
352 const OpenGl_Vec3i& theValue);
354 //! Specifies the value of the integer uniform 4D vector.
355 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
356 const GLchar* theName,
357 const OpenGl_Vec4i& theValue);
359 //! Specifies the value of the integer uniform 4D vector.
360 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
362 const OpenGl_Vec4i& theValue);
366 //! Specifies the value of the unsigned integer uniform 2D vector (uvec2).
367 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
368 const GLchar* theName,
369 const OpenGl_Vec2u& theValue);
371 //! Specifies the value of the unsigned integer uniform 2D vector (uvec2).
372 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
374 const OpenGl_Vec2u& theValue);
376 //! Specifies the value of the uvec2 uniform array
377 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
378 const GLchar* theName,
379 const GLsizei theCount,
380 const OpenGl_Vec2u* theValue);
382 //! Specifies the value of the uvec2 uniform array
383 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
385 const GLsizei theCount,
386 const OpenGl_Vec2u* theValue);
390 //! Specifies the value of the float uniform variable.
391 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
392 const GLchar* theName,
395 //! Specifies the value of the float uniform variable.
396 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
400 //! Specifies the value of the float uniform 2D vector.
401 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
402 const GLchar* theName,
403 const OpenGl_Vec2& theValue);
405 //! Specifies the value of the float uniform 2D vector.
406 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
408 const OpenGl_Vec2& theValue);
410 //! Specifies the value of the float uniform 3D vector.
411 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
412 const GLchar* theName,
413 const OpenGl_Vec3& theValue);
415 //! Specifies the value of the float uniform 3D vector.
416 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
418 const OpenGl_Vec3& theValue);
420 //! Specifies the value of the float uniform 4D vector.
421 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
422 const GLchar* theName,
423 const OpenGl_Vec4& theValue);
425 //! Specifies the value of the float uniform 4D vector.
426 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
428 const OpenGl_Vec4& theValue);
432 //! Specifies the value of the float uniform 4x4 matrix.
433 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
434 const GLchar* theName,
435 const OpenGl_Mat4& theValue,
436 GLboolean theTranspose = GL_FALSE);
438 //! Specifies the value of the float uniform 4x4 matrix.
439 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
441 const OpenGl_Mat4& theValue,
442 GLboolean theTranspose = GL_FALSE);
444 //! Specifies the value of the float uniform 4x4 matrix.
445 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
446 const GLchar* theName,
447 const OpenGl_Matrix& theValue,
448 GLboolean theTranspose = GL_FALSE);
450 //! Specifies the value of the float uniform 4x4 matrix.
451 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
453 const OpenGl_Matrix& theValue,
454 GLboolean theTranspose = GL_FALSE);
456 //! Specifies the value of the float uniform array
457 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
460 const Standard_ShortReal* theData);
462 //! Specifies the value of the float2 uniform array
463 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
466 const OpenGl_Vec2* theData);
468 //! Specifies the value of the float3 uniform array
469 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
472 const OpenGl_Vec3* theData);
474 //! Specifies the value of the float4 uniform array
475 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
478 const OpenGl_Vec4* theData);
480 //! Specifies the value of the integer uniform array
481 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
484 const Standard_Integer* theData);
486 //! Specifies the value of the int2 uniform array
487 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
490 const OpenGl_Vec2i* theData);
492 //! Specifies the value of the int3 uniform array
493 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
496 const OpenGl_Vec3i* theData);
498 //! Specifies the value of the int4 uniform array
499 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
502 const OpenGl_Vec4i* theData);
506 //! Specifies the value of the sampler uniform variable.
507 Standard_EXPORT Standard_Boolean SetSampler (const Handle(OpenGl_Context)& theCtx,
508 const GLchar* theName,
509 const GLenum theTextureUnit);
511 //! Specifies the value of the sampler uniform variable.
512 Standard_EXPORT Standard_Boolean SetSampler (const Handle(OpenGl_Context)& theCtx,
514 const GLenum theTextureUnit);
518 //! Increments counter of users.
519 //! Used by OpenGl_ShaderManager.
520 //! @return true when resource has been restored from delayed release queue
523 return ++myShareCount == 1;
526 //! Decrements counter of users.
527 //! Used by OpenGl_ShaderManager.
528 //! @return true when there are no more users of this program has been left
531 return --myShareCount == 0;
536 GLuint myProgramID; //!< Handle of OpenGL shader program
537 OpenGl_ShaderList myShaderObjects; //!< List of attached shader objects
538 Handle(Graphic3d_ShaderProgram) myProxy; //!< Proxy shader program (from application layer)
539 Standard_Integer myShareCount; //!< program users count, initialized with 1 (already shared by one user)
543 Standard_Size myCurrentState[MaxStateTypes]; //!< defines last modification for variables of each state type
545 //! Stores locations of OCCT state uniform variables.
546 GLint myStateLocations[OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES];
550 DEFINE_STANDARD_RTTIEXT(OpenGl_ShaderProgram,OpenGl_Resource)
551 friend class OpenGl_ShaderManager;
556 struct OpenGl_VariableSetter : public OpenGl_SetterInterface
558 virtual void Set (const Handle(OpenGl_Context)& theCtx,
559 const Handle(Graphic3d_ShaderVariable)& theVariable,
560 OpenGl_ShaderProgram* theProgram)
562 theProgram->SetUniform (theCtx,
563 theVariable->Name().ToCString(),
564 theVariable->Value()->As<T>());
568 namespace OpenGl_HashMapInitializer
570 template<class K, class V>
573 NCollection_DataMap<K, V> myDictionary;
575 MapListOfType (K theKey, V theValue)
577 myDictionary.Bind (theKey, theValue);
580 MapListOfType& operator() (K theKey, V theValue)
582 myDictionary.Bind (theKey, theValue);
586 operator const NCollection_DataMap<K, V>& () const
592 template<class K, class V>
593 MapListOfType<K, V> CreateListOf (K theKey, V theValue)
595 return MapListOfType<K, V> (theKey, theValue);
599 #endif // _OpenGl_ShaderProgram_Header