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_TEXTURE_TRSF2D,
72 OpenGl_OCCT_POINT_SIZE,
74 // DON'T MODIFY THIS ITEM (insert new items before it)
75 OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES
78 class OpenGl_ShaderProgram;
80 //! Interface for generic setter of user-defined uniform variables.
81 struct OpenGl_SetterInterface
83 //! Sets user-defined uniform variable to specified program.
84 virtual void Set (const Handle(OpenGl_Context)& theCtx,
85 const Handle(Graphic3d_ShaderVariable)& theVariable,
86 OpenGl_ShaderProgram* theProgram) = 0;
89 virtual ~OpenGl_SetterInterface() {}
92 //! List of OpenGL shader objects.
93 typedef NCollection_Sequence<Handle(OpenGl_ShaderObject)> OpenGl_ShaderList;
95 //! List of shader variable setters.
96 typedef NCollection_DataMap<size_t, OpenGl_SetterInterface*> OpenGl_SetterList;
98 //! Support tool for setting user-defined uniform variables.
99 class OpenGl_VariableSetterSelector
103 //! Creates new setter selector.
104 OpenGl_VariableSetterSelector();
106 //! Releases memory resources of setter selector.
107 ~OpenGl_VariableSetterSelector();
109 //! Sets user-defined uniform variable to specified program.
110 void Set (const Handle(OpenGl_Context)& theCtx,
111 const Handle(Graphic3d_ShaderVariable)& theVariable,
112 OpenGl_ShaderProgram* theProgram) const;
116 //! List of variable setters.
117 OpenGl_SetterList mySetterList;
120 //! Defines types of uniform state variables.
121 enum OpenGl_UniformStateType
123 OpenGl_LIGHT_SOURCES_STATE,
124 OpenGl_CLIP_PLANES_STATE,
125 OpenGl_MODEL_WORLD_STATE,
126 OpenGl_WORLD_VIEW_STATE,
127 OpenGl_PROJECTION_STATE,
128 OpenGl_MATERIALS_STATE,
129 OpenGl_SURF_DETAIL_STATE
132 //! Total number of state types.
133 const int MaxStateTypes = 6;
135 //! Wrapper for OpenGL program object.
136 class OpenGl_ShaderProgram : public OpenGl_Resource
138 friend class OpenGl_View;
142 //! Non-valid shader name.
143 static const GLuint NO_PROGRAM = 0;
145 //! Invalid location of uniform/attribute variable.
146 static const GLint INVALID_LOCATION = -1;
148 //! List of pre-defined OCCT state uniform variables.
149 static Standard_CString PredefinedKeywords[OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES];
151 //! Creates uninitialized shader program.
153 //! WARNING! This constructor is not intended to be called anywhere but from OpenGl_ShaderManager::Create().
154 //! Manager has been designed to synchronize camera position, lights definition and other aspects of the program implicitly,
155 //! as well as sharing same program across rendering groups.
157 //! Program created outside the manager will be left detached from these routines,
158 //! and them should be performed manually by caller.
160 //! This constructor has been made public to provide more flexibility to re-use OCCT OpenGL classes without OCCT Viewer itself.
161 //! If this is not the case - create the program using shared OpenGl_ShaderManager instance instead.
162 Standard_EXPORT OpenGl_ShaderProgram (const Handle(Graphic3d_ShaderProgram)& theProxy = NULL);
166 static OpenGl_VariableSetterSelector mySetterSelector;
170 //! Releases resources of shader program.
171 Standard_EXPORT virtual ~OpenGl_ShaderProgram();
173 //! Creates new empty shader program of specified type.
174 Standard_EXPORT Standard_Boolean Create (const Handle(OpenGl_Context)& theCtx);
176 //! Destroys shader program.
177 Standard_EXPORT virtual void Release (OpenGl_Context* theCtx) Standard_OVERRIDE;
179 //! Attaches shader object to the program object.
180 Standard_EXPORT Standard_Boolean AttachShader (const Handle(OpenGl_Context)& theCtx,
181 const Handle(OpenGl_ShaderObject)& theShader);
183 //! Detaches shader object to the program object.
184 Standard_EXPORT Standard_Boolean DetachShader (const Handle(OpenGl_Context)& theCtx,
185 const Handle(OpenGl_ShaderObject)& theShader);
187 //! Initializes program object with the list of shader objects.
188 Standard_EXPORT Standard_Boolean Initialize (const Handle(OpenGl_Context)& theCtx,
189 const Graphic3d_ShaderObjectList& theShaders);
191 //! Links the program object.
192 Standard_EXPORT Standard_Boolean Link (const Handle(OpenGl_Context)& theCtx);
194 //! Fetches information log of the last link operation.
195 Standard_EXPORT Standard_Boolean FetchInfoLog (const Handle(OpenGl_Context)& theCtx,
196 TCollection_AsciiString& theLog);
198 //! Fetches uniform variables from proxy shader program.
199 Standard_EXPORT Standard_Boolean ApplyVariables (const Handle(OpenGl_Context)& theCtx);
201 //! @return true if current object was initialized
202 inline bool IsValid() const
204 return myProgramID != NO_PROGRAM;
207 //! @return program ID
208 inline GLuint ProgramId() const
215 //! Returns index of last modification of variables of specified state type.
216 Standard_EXPORT Standard_Size ActiveState (const OpenGl_UniformStateType theType) const;
218 //! Updates index of last modification of variables of specified state type.
219 Standard_EXPORT void UpdateState (const OpenGl_UniformStateType theType,
220 const Standard_Size theIndex);
224 //! Returns location of the specific uniform variable.
225 Standard_EXPORT GLint GetUniformLocation (const Handle(OpenGl_Context)& theCtx,
226 const GLchar* theName) const;
228 //! Returns index of the generic vertex attribute by variable name.
229 Standard_EXPORT GLint GetAttributeLocation (const Handle(OpenGl_Context)& theCtx,
230 const GLchar* theName) const;
232 //! Returns location of the OCCT state uniform variable.
233 Standard_EXPORT GLint GetStateLocation (const GLuint theVariable) const;
237 //! Returns the value of the integer uniform variable.
238 Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
239 const GLchar* theName,
240 OpenGl_Vec4i& theValue) const;
242 Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
244 OpenGl_Vec4i& theValue) const;
246 //! Returns the value of the float uniform variable.
247 Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
248 const GLchar* theName,
249 OpenGl_Vec4& theValue) const;
251 //! Returns the value of the float uniform variable.
252 Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
254 OpenGl_Vec4& theValue) const;
258 //! Returns the integer vertex attribute.
259 Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
260 const GLchar* theName,
261 OpenGl_Vec4i& theValue) const;
263 //! Returns the integer vertex attribute.
264 Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
266 OpenGl_Vec4i& theValue) const;
268 //! Returns the float vertex attribute.
269 Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
270 const GLchar* theName,
271 OpenGl_Vec4& theValue) const;
273 //! Returns the float vertex attribute.
274 Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
276 OpenGl_Vec4& theValue) const;
280 //! Wrapper for glBindAttribLocation()
281 Standard_EXPORT Standard_Boolean SetAttributeName (const Handle(OpenGl_Context)& theCtx,
283 const GLchar* theName);
285 //! Wrapper for glVertexAttrib1f()
286 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
287 const GLchar* theName,
290 //! Wrapper for glVertexAttrib1f()
291 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
295 //! Wrapper for glVertexAttrib2fv()
296 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
297 const GLchar* theName,
298 const OpenGl_Vec2& theValue);
300 //! Wrapper for glVertexAttrib2fv()
301 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
303 const OpenGl_Vec2& theValue);
305 //! Wrapper for glVertexAttrib3fv()
306 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
307 const GLchar* theName,
308 const OpenGl_Vec3& theValue);
310 //! Wrapper for glVertexAttrib3fv()
311 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
313 const OpenGl_Vec3& theValue);
315 //! Wrapper for glVertexAttrib4fv()
316 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
317 const GLchar* theName,
318 const OpenGl_Vec4& theValue);
320 //! Wrapper for glVertexAttrib4fv()
321 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
323 const OpenGl_Vec4& theValue);
327 //! Specifies the value of the integer uniform variable.
328 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
329 const GLchar* theName,
332 //! Specifies the value of the integer uniform variable.
333 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
337 //! Specifies the value of the integer uniform 2D vector.
338 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
339 const GLchar* theName,
340 const OpenGl_Vec2i& theValue);
342 //! Specifies the value of the integer uniform 2D vector.
343 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
345 const OpenGl_Vec2i& theValue);
347 //! Specifies the value of the integer uniform 3D vector.
348 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
349 const GLchar* theName,
350 const OpenGl_Vec3i& theValue);
352 //! Specifies the value of the integer uniform 3D vector.
353 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
355 const OpenGl_Vec3i& theValue);
357 //! Specifies the value of the integer uniform 4D vector.
358 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
359 const GLchar* theName,
360 const OpenGl_Vec4i& theValue);
362 //! Specifies the value of the integer uniform 4D vector.
363 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
365 const OpenGl_Vec4i& theValue);
369 //! Specifies the value of the unsigned integer uniform 2D vector (uvec2).
370 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
371 const GLchar* theName,
372 const OpenGl_Vec2u& theValue);
374 //! Specifies the value of the unsigned integer uniform 2D vector (uvec2).
375 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
377 const OpenGl_Vec2u& theValue);
379 //! Specifies the value of the uvec2 uniform array
380 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
381 const GLchar* theName,
382 const GLsizei theCount,
383 const OpenGl_Vec2u* theValue);
385 //! Specifies the value of the uvec2 uniform array
386 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
388 const GLsizei theCount,
389 const OpenGl_Vec2u* theValue);
393 //! Specifies the value of the float uniform variable.
394 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
395 const GLchar* theName,
398 //! Specifies the value of the float uniform variable.
399 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
403 //! Specifies the value of the float uniform 2D vector.
404 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
405 const GLchar* theName,
406 const OpenGl_Vec2& theValue);
408 //! Specifies the value of the float uniform 2D vector.
409 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
411 const OpenGl_Vec2& theValue);
413 //! Specifies the value of the float uniform 3D vector.
414 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
415 const GLchar* theName,
416 const OpenGl_Vec3& theValue);
418 //! Specifies the value of the float uniform 3D vector.
419 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
421 const OpenGl_Vec3& theValue);
423 //! Specifies the value of the float uniform 4D vector.
424 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
425 const GLchar* theName,
426 const OpenGl_Vec4& theValue);
428 //! Specifies the value of the float uniform 4D vector.
429 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
431 const OpenGl_Vec4& theValue);
435 //! Specifies the value of the float uniform 4x4 matrix.
436 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
437 const GLchar* theName,
438 const OpenGl_Mat4& theValue,
439 GLboolean theTranspose = GL_FALSE);
441 //! Specifies the value of the float uniform 4x4 matrix.
442 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
444 const OpenGl_Mat4& theValue,
445 GLboolean theTranspose = GL_FALSE);
447 //! Specifies the value of the float uniform 4x4 matrix.
448 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
449 const GLchar* theName,
450 const OpenGl_Matrix& theValue,
451 GLboolean theTranspose = GL_FALSE);
453 //! Specifies the value of the float uniform 4x4 matrix.
454 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
456 const OpenGl_Matrix& theValue,
457 GLboolean theTranspose = GL_FALSE);
459 //! Specifies the value of the float uniform array
460 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
463 const Standard_ShortReal* theData);
465 //! Specifies the value of the float2 uniform array
466 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
469 const OpenGl_Vec2* theData);
471 //! Specifies the value of the float3 uniform array
472 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
475 const OpenGl_Vec3* theData);
477 //! Specifies the value of the float4 uniform array
478 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
481 const OpenGl_Vec4* theData);
483 //! Specifies the value of the integer uniform array
484 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
487 const Standard_Integer* theData);
489 //! Specifies the value of the int2 uniform array
490 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
493 const OpenGl_Vec2i* theData);
495 //! Specifies the value of the int3 uniform array
496 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
499 const OpenGl_Vec3i* theData);
501 //! Specifies the value of the int4 uniform array
502 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
505 const OpenGl_Vec4i* theData);
509 //! Specifies the value of the sampler uniform variable.
510 Standard_EXPORT Standard_Boolean SetSampler (const Handle(OpenGl_Context)& theCtx,
511 const GLchar* theName,
512 const GLenum theTextureUnit);
514 //! Specifies the value of the sampler uniform variable.
515 Standard_EXPORT Standard_Boolean SetSampler (const Handle(OpenGl_Context)& theCtx,
517 const GLenum theTextureUnit);
521 //! Increments counter of users.
522 //! Used by OpenGl_ShaderManager.
523 //! @return true when resource has been restored from delayed release queue
526 return ++myShareCount == 1;
529 //! Decrements counter of users.
530 //! Used by OpenGl_ShaderManager.
531 //! @return true when there are no more users of this program has been left
534 return --myShareCount == 0;
539 GLuint myProgramID; //!< Handle of OpenGL shader program
540 OpenGl_ShaderList myShaderObjects; //!< List of attached shader objects
541 Handle(Graphic3d_ShaderProgram) myProxy; //!< Proxy shader program (from application layer)
542 Standard_Integer myShareCount; //!< program users count, initialized with 1 (already shared by one user)
546 Standard_Size myCurrentState[MaxStateTypes]; //!< defines last modification for variables of each state type
548 //! Stores locations of OCCT state uniform variables.
549 GLint myStateLocations[OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES];
553 DEFINE_STANDARD_RTTI (OpenGl_ShaderProgram, OpenGl_Resource)
554 friend class OpenGl_ShaderManager;
559 struct OpenGl_VariableSetter : public OpenGl_SetterInterface
561 virtual void Set (const Handle(OpenGl_Context)& theCtx,
562 const Handle(Graphic3d_ShaderVariable)& theVariable,
563 OpenGl_ShaderProgram* theProgram)
565 theProgram->SetUniform (theCtx,
566 theVariable->Name().ToCString(),
567 theVariable->Value()->As<T>());
571 namespace OpenGl_HashMapInitializer
573 template<class K, class V>
576 NCollection_DataMap<K, V> myDictionary;
578 MapListOfType (K theKey, V theValue)
580 myDictionary.Bind (theKey, theValue);
583 MapListOfType& operator() (K theKey, V theValue)
585 myDictionary.Bind (theKey, theValue);
589 operator const NCollection_DataMap<K, V>& () const
595 template<class K, class V>
596 MapListOfType<K, V> CreateListOf (K theKey, V theValue)
598 return MapListOfType<K, V> (theKey, theValue);
602 #endif // _OpenGl_ShaderProgram_Header