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>
31 #include <Handle_OpenGl_ShaderProgram.hxx>
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_SPACES,
53 OpenGl_OCC_CLIP_PLANE_COUNT,
56 OpenGl_OCC_LIGHT_SOURCE_COUNT,
57 OpenGl_OCC_LIGHT_SOURCE_TYPES,
58 OpenGl_OCC_LIGHT_SOURCE_PARAMS,
59 OpenGl_OCC_LIGHT_AMBIENT,
62 OpenGl_OCCT_ACTIVE_SAMPLER,
63 OpenGl_OCCT_TEXTURE_ENABLE,
64 OpenGl_OCCT_DISTINGUISH_MODE,
65 OpenGl_OCCT_FRONT_MATERIAL,
66 OpenGl_OCCT_BACK_MATERIAL,
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
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_Workspace;
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];
149 //! Creates uninitialized shader program.
150 Standard_EXPORT OpenGl_ShaderProgram (const Handle(Graphic3d_ShaderProgram)& theProxy = NULL);
152 static OpenGl_VariableSetterSelector mySetterSelector;
156 //! Releases resources of shader program.
157 Standard_EXPORT virtual ~OpenGl_ShaderProgram();
159 //! Creates new empty shader program of specified type.
160 Standard_EXPORT Standard_Boolean Create (const Handle(OpenGl_Context)& theCtx);
162 //! Destroys shader program.
163 Standard_EXPORT virtual void Release (OpenGl_Context* theCtx);
165 //! Attaches shader object to the program object.
166 Standard_EXPORT Standard_Boolean AttachShader (const Handle(OpenGl_Context)& theCtx,
167 const Handle(OpenGl_ShaderObject)& theShader);
169 //! Detaches shader object to the program object.
170 Standard_EXPORT Standard_Boolean DetachShader (const Handle(OpenGl_Context)& theCtx,
171 const Handle(OpenGl_ShaderObject)& theShader);
173 //! Initializes program object with the list of shader objects.
174 Standard_EXPORT Standard_Boolean Initialize (const Handle(OpenGl_Context)& theCtx,
175 const Graphic3d_ShaderObjectList& theShaders);
177 //! Links the program object.
178 Standard_EXPORT Standard_Boolean Link (const Handle(OpenGl_Context)& theCtx);
180 //! Fetches information log of the last link operation.
181 Standard_EXPORT Standard_Boolean FetchInfoLog (const Handle(OpenGl_Context)& theCtx,
182 TCollection_AsciiString& theLog);
184 //! Fetches uniform variables from proxy shader program.
185 Standard_EXPORT Standard_Boolean ApplyVariables (const Handle(OpenGl_Context)& theCtx);
187 //! @return true if current object was initialized
188 inline bool IsValid() const
190 return myProgramID != NO_PROGRAM;
193 //! @return program ID
194 inline GLuint ProgramId() const
201 //! Returns index of last modification of variables of specified state type.
202 Standard_EXPORT Standard_Size ActiveState (const OpenGl_UniformStateType theType) const;
204 //! Updates index of last modification of variables of specified state type.
205 Standard_EXPORT void UpdateState (const OpenGl_UniformStateType theType,
206 const Standard_Size theIndex);
210 //! Returns location of the specific uniform variable.
211 Standard_EXPORT GLint GetUniformLocation (const Handle(OpenGl_Context)& theCtx,
212 const GLchar* theName) const;
214 //! Returns index of the generic vertex attribute by variable name.
215 Standard_EXPORT GLint GetAttributeLocation (const Handle(OpenGl_Context)& theCtx,
216 const GLchar* theName) const;
218 //! Returns location of the OCCT state uniform variable.
219 Standard_EXPORT GLint GetStateLocation (const GLuint theVariable) const;
223 //! Returns the value of the integer uniform variable.
224 Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
225 const GLchar* theName,
226 OpenGl_Vec4i& theValue) const;
228 Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
230 OpenGl_Vec4i& theValue) const;
232 //! Returns the value of the float uniform variable.
233 Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
234 const GLchar* theName,
235 OpenGl_Vec4& theValue) const;
237 //! Returns the value of the float uniform variable.
238 Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
240 OpenGl_Vec4& theValue) const;
244 //! Returns the integer vertex attribute.
245 Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
246 const GLchar* theName,
247 OpenGl_Vec4i& theValue) const;
249 //! Returns the integer vertex attribute.
250 Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
252 OpenGl_Vec4i& theValue) const;
254 //! Returns the float vertex attribute.
255 Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
256 const GLchar* theName,
257 OpenGl_Vec4& theValue) const;
259 //! Returns the float vertex attribute.
260 Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
262 OpenGl_Vec4& theValue) const;
266 //! Wrapper for glBindAttribLocation()
267 Standard_EXPORT Standard_Boolean SetAttributeName (const Handle(OpenGl_Context)& theCtx,
269 const GLchar* theName);
271 //! Wrapper for glVertexAttrib1f()
272 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
273 const GLchar* theName,
276 //! Wrapper for glVertexAttrib1f()
277 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
281 //! Wrapper for glVertexAttrib2fv()
282 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
283 const GLchar* theName,
284 const OpenGl_Vec2& theValue);
286 //! Wrapper for glVertexAttrib2fv()
287 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
289 const OpenGl_Vec2& theValue);
291 //! Wrapper for glVertexAttrib3fv()
292 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
293 const GLchar* theName,
294 const OpenGl_Vec3& theValue);
296 //! Wrapper for glVertexAttrib3fv()
297 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
299 const OpenGl_Vec3& theValue);
301 //! Wrapper for glVertexAttrib4fv()
302 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
303 const GLchar* theName,
304 const OpenGl_Vec4& theValue);
306 //! Wrapper for glVertexAttrib4fv()
307 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
309 const OpenGl_Vec4& theValue);
313 //! Specifies the value of the integer uniform variable.
314 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
315 const GLchar* theName,
318 //! Specifies the value of the integer uniform variable.
319 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
323 //! Specifies the value of the integer uniform 2D vector.
324 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
325 const GLchar* theName,
326 const OpenGl_Vec2i& theValue);
328 //! Specifies the value of the integer uniform 2D vector.
329 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
331 const OpenGl_Vec2i& theValue);
333 //! Specifies the value of the integer uniform 3D vector.
334 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
335 const GLchar* theName,
336 const OpenGl_Vec3i& theValue);
338 //! Specifies the value of the integer uniform 3D vector.
339 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
341 const OpenGl_Vec3i& theValue);
343 //! Specifies the value of the integer uniform 4D vector.
344 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
345 const GLchar* theName,
346 const OpenGl_Vec4i& theValue);
348 //! Specifies the value of the integer uniform 4D vector.
349 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
351 const OpenGl_Vec4i& theValue);
355 //! Specifies the value of the 64-bit unsigned integer uniform variable.
356 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
357 const GLchar* theName,
360 //! Specifies the value of the 64-bit unsigned integer uniform variable.
361 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
365 //! Specifies the value of the 64-bit unsigned integer uniform array.
366 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
367 const GLchar* theName,
368 const GLsizei theCount,
369 const GLuint64* theValue);
371 //! Specifies the value of the 64-bit unsigned integer uniform array.
372 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
374 const GLsizei theCount,
375 const GLuint64* theValue);
379 //! Specifies the value of the float uniform variable.
380 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
381 const GLchar* theName,
384 //! Specifies the value of the float uniform variable.
385 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
389 //! Specifies the value of the float uniform 2D vector.
390 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
391 const GLchar* theName,
392 const OpenGl_Vec2& theValue);
394 //! Specifies the value of the float uniform 2D vector.
395 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
397 const OpenGl_Vec2& theValue);
399 //! Specifies the value of the float uniform 3D vector.
400 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
401 const GLchar* theName,
402 const OpenGl_Vec3& theValue);
404 //! Specifies the value of the float uniform 3D vector.
405 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
407 const OpenGl_Vec3& theValue);
409 //! Specifies the value of the float uniform 4D vector.
410 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
411 const GLchar* theName,
412 const OpenGl_Vec4& theValue);
414 //! Specifies the value of the float uniform 4D vector.
415 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
417 const OpenGl_Vec4& theValue);
421 //! Specifies the value of the float uniform 4x4 matrix.
422 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
423 const GLchar* theName,
424 const OpenGl_Mat4& theValue,
425 GLboolean theTranspose = GL_FALSE);
427 //! Specifies the value of the float uniform 4x4 matrix.
428 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
430 const OpenGl_Mat4& theValue,
431 GLboolean theTranspose = GL_FALSE);
433 //! Specifies the value of the float uniform 4x4 matrix.
434 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
435 const GLchar* theName,
436 const OpenGl_Matrix& theValue,
437 GLboolean theTranspose = GL_FALSE);
439 //! Specifies the value of the float uniform 4x4 matrix.
440 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
442 const OpenGl_Matrix& theValue,
443 GLboolean theTranspose = GL_FALSE);
445 //! Specifies the value of the float uniform 4x4 matrix.
446 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
447 const GLchar* theName,
448 const Tmatrix3& theValue,
449 GLboolean theTranspose = GL_FALSE);
451 //! Specifies the value of the float uniform 4x4 matrix.
452 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
454 const Tmatrix3& theValue,
455 GLboolean theTranspose = GL_FALSE);
457 //! Specifies the value of the float uniform array
458 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
461 const Standard_ShortReal* theData);
463 //! Specifies the value of the float2 uniform array
464 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
467 const OpenGl_Vec2* theData);
469 //! Specifies the value of the float3 uniform array
470 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
473 const OpenGl_Vec3* theData);
475 //! Specifies the value of the float4 uniform array
476 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
479 const OpenGl_Vec4* theData);
481 //! Specifies the value of the integer uniform array
482 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
485 const Standard_Integer* theData);
487 //! Specifies the value of the int2 uniform array
488 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
491 const OpenGl_Vec2i* theData);
493 //! Specifies the value of the int3 uniform array
494 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
497 const OpenGl_Vec3i* theData);
499 //! Specifies the value of the int4 uniform array
500 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
503 const OpenGl_Vec4i* theData);
507 //! Specifies the value of the sampler uniform variable.
508 Standard_EXPORT Standard_Boolean SetSampler (const Handle(OpenGl_Context)& theCtx,
509 const GLchar* theName,
510 const GLenum theTextureUnit);
512 //! Specifies the value of the sampler uniform variable.
513 Standard_EXPORT Standard_Boolean SetSampler (const Handle(OpenGl_Context)& theCtx,
515 const GLenum theTextureUnit);
519 //! Increments counter of users.
520 //! Used by OpenGl_ShaderManager.
521 //! @return true when resource has been restored from delayed release queue
524 return ++myShareCount == 1;
527 //! Decrements counter of users.
528 //! Used by OpenGl_ShaderManager.
529 //! @return true when there are no more users of this program has been left
532 return --myShareCount == 0;
537 GLuint myProgramID; //!< Handle of OpenGL shader program
538 OpenGl_ShaderList myShaderObjects; //!< List of attached shader objects
539 Handle(Graphic3d_ShaderProgram) myProxy; //!< Proxy shader program (from application layer)
540 Standard_Integer myShareCount; //!< program users count, initialized with 1 (already shared by one user)
544 Standard_Size myCurrentState[MaxStateTypes]; //!< defines last modification for variables of each state type
546 //! Stores locations of OCCT state uniform variables.
547 GLint myStateLocations[OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES];
551 DEFINE_STANDARD_RTTI (OpenGl_ShaderProgram)
552 friend class OpenGl_ShaderManager;
557 struct OpenGl_VariableSetter : public OpenGl_SetterInterface
559 virtual void Set (const Handle(OpenGl_Context)& theCtx,
560 const Handle(Graphic3d_ShaderVariable)& theVariable,
561 OpenGl_ShaderProgram* theProgram)
563 theProgram->SetUniform (theCtx,
564 theVariable->Name().ToCString(),
565 theVariable->Value()->As<T>());
569 namespace OpenGl_HashMapInitializer
571 template<class K, class V>
574 NCollection_DataMap<K, V> myDictionary;
576 MapListOfType (K theKey, V theValue)
578 myDictionary.Bind (theKey, theValue);
581 MapListOfType& operator() (K theKey, V theValue)
583 myDictionary.Bind (theKey, theValue);
587 operator const NCollection_DataMap<K, V>& () const
593 template<class K, class V>
594 MapListOfType<K, V> CreateListOf (K theKey, V theValue)
596 return MapListOfType<K, V> (theKey, theValue);
600 #endif // _OpenGl_ShaderProgram_Header