1 // Created on: 2013-09-19
2 // Created by: Denis BOGOLEPOV
3 // Copyright (c) 2013 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
8 // under the terms of the GNU Lesser General Public 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,
68 // DON'T MODIFY THIS ITEM (insert new items before it)
69 OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES
72 class OpenGl_ShaderProgram;
74 //! Interface for generic setter of user-defined uniform variables.
75 struct OpenGl_SetterInterface
77 //! Sets user-defined uniform variable to specified program.
78 virtual void Set (const Handle(OpenGl_Context)& theCtx,
79 const Handle(Graphic3d_ShaderVariable)& theVariable,
80 OpenGl_ShaderProgram* theProgram) = 0;
83 virtual ~OpenGl_SetterInterface() {}
86 //! List of OpenGL shader objects.
87 typedef NCollection_Sequence<Handle(OpenGl_ShaderObject)> OpenGl_ShaderList;
89 //! List of shader variable setters.
90 typedef NCollection_DataMap<size_t, OpenGl_SetterInterface*> OpenGl_SetterList;
92 //! Support tool for setting user-defined uniform variables.
93 class OpenGl_VariableSetterSelector
97 //! Creates new setter selector.
98 OpenGl_VariableSetterSelector();
100 //! Releases memory resources of setter selector.
101 ~OpenGl_VariableSetterSelector();
103 //! Sets user-defined uniform variable to specified program.
104 void Set (const Handle(OpenGl_Context)& theCtx,
105 const Handle(Graphic3d_ShaderVariable)& theVariable,
106 OpenGl_ShaderProgram* theProgram) const;
110 //! List of variable setters.
111 OpenGl_SetterList mySetterList;
114 //! Defines types of uniform state variables.
115 enum OpenGl_UniformStateType
117 OpenGl_LIGHT_SOURCES_STATE,
118 OpenGl_CLIP_PLANES_STATE,
119 OpenGl_MODEL_WORLD_STATE,
120 OpenGl_WORLD_VIEW_STATE,
121 OpenGl_PROJECTION_STATE,
122 OpenGl_MATERIALS_STATE
125 //! Total number of state types.
126 const int MaxStateTypes = 6;
128 //! Wrapper for OpenGL program object.
129 class OpenGl_ShaderProgram : public OpenGl_Resource
134 //! Non-valid shader name.
135 static const GLuint NO_PROGRAM = 0;
137 //! Invalid location of uniform/attribute variable.
138 static const GLint INVALID_LOCATION = -1;
140 //! List of pre-defined OCCT state uniform variables.
141 static Standard_CString PredefinedKeywords[OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES];
145 //! Creates uninitialized shader program.
146 Standard_EXPORT OpenGl_ShaderProgram (const Handle(Graphic3d_ShaderProgram)& theProxy = NULL);
148 static OpenGl_VariableSetterSelector mySetterSelector;
152 //! Releases resources of shader program.
153 Standard_EXPORT virtual ~OpenGl_ShaderProgram();
155 //! Creates new empty shader program of specified type.
156 Standard_EXPORT Standard_Boolean Create (const Handle(OpenGl_Context)& theCtx);
158 //! Destroys shader program.
159 Standard_EXPORT virtual void Release (const OpenGl_Context* theCtx);
161 //! Attaches shader object to the program object.
162 Standard_EXPORT Standard_Boolean AttachShader (const Handle(OpenGl_Context)& theCtx,
163 const Handle(OpenGl_ShaderObject)& theShader);
165 //! Detaches shader object to the program object.
166 Standard_EXPORT Standard_Boolean DetachShader (const Handle(OpenGl_Context)& theCtx,
167 const Handle(OpenGl_ShaderObject)& theShader);
169 //! Initializes program object with the list of shader objects.
170 Standard_EXPORT Standard_Boolean Initialize (const Handle(OpenGl_Context)& theCtx,
171 const Graphic3d_ShaderObjectList& theShaders);
173 //! Links the program object.
174 Standard_EXPORT Standard_Boolean Link (const Handle(OpenGl_Context)& theCtx);
176 //! Fetches information log of the last link operation.
177 Standard_EXPORT Standard_Boolean FetchInfoLog (const Handle(OpenGl_Context)& theCtx,
178 TCollection_AsciiString& theLog);
180 //! Fetches uniform variables from proxy shader program.
181 Standard_EXPORT Standard_Boolean ApplyVariables (const Handle(OpenGl_Context)& theCtx);
183 //! Sets the program object as part of current rendering state.
184 Standard_EXPORT void Bind (const Handle(OpenGl_Context)& theCtx) const;
186 //! Binds the program object and applies variables from proxy shader program.
187 Standard_EXPORT Standard_Boolean BindWithVariables (const Handle(OpenGl_Context)& theCtx);
189 //! Reverts to fixed-function graphics pipeline (FFP).
190 Standard_EXPORT static void Unbind (const Handle(OpenGl_Context)& theCtx);
192 //! @return true if current object was initialized
193 inline bool IsValid() const
195 return myProgramID != NO_PROGRAM;
200 //! Returns index of last modification of variables of specified state type.
201 Standard_EXPORT Standard_Size ActiveState (const OpenGl_UniformStateType theType) const;
203 //! Updates index of last modification of variables of specified state type.
204 Standard_EXPORT void UpdateState (const OpenGl_UniformStateType theType,
205 const Standard_Size theIndex);
209 //! Returns location of the specific uniform variable.
210 Standard_EXPORT GLint GetUniformLocation (const Handle(OpenGl_Context)& theCtx,
211 const GLchar* theName) const;
213 //! Returns index of the generic vertex attribute by variable name.
214 Standard_EXPORT GLint GetAttributeLocation (const Handle(OpenGl_Context)& theCtx,
215 const GLchar* theName) const;
217 //! Returns location of the OCCT state uniform variable.
218 Standard_EXPORT GLint GetStateLocation (const GLuint theVariable) const;
222 //! Returns the value of the integer uniform variable.
223 Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
224 const GLchar* theName,
225 OpenGl_Vec4i& theValue) const;
227 Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
229 OpenGl_Vec4i& theValue) const;
231 //! Returns the value of the float uniform variable.
232 Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
233 const GLchar* theName,
234 OpenGl_Vec4& theValue) const;
236 //! Returns the value of the float uniform variable.
237 Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
239 OpenGl_Vec4& theValue) const;
243 //! Returns the integer vertex attribute.
244 Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
245 const GLchar* theName,
246 OpenGl_Vec4i& theValue) const;
248 //! Returns the integer vertex attribute.
249 Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
251 OpenGl_Vec4i& theValue) const;
253 //! Returns the float vertex attribute.
254 Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
255 const GLchar* theName,
256 OpenGl_Vec4& theValue) const;
258 //! Returns the float vertex attribute.
259 Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
261 OpenGl_Vec4& theValue) const;
265 //! Specifies the value of the integer uniform variable.
266 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
267 const GLchar* theName,
270 //! Specifies the value of the integer uniform variable.
271 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
275 //! Specifies the value of the integer uniform 2D vector.
276 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
277 const GLchar* theName,
278 const OpenGl_Vec2i& theValue);
280 //! Specifies the value of the integer uniform 2D vector.
281 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
283 const OpenGl_Vec2i& theValue);
285 //! Specifies the value of the integer uniform 3D vector.
286 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
287 const GLchar* theName,
288 const OpenGl_Vec3i& theValue);
290 //! Specifies the value of the integer uniform 3D vector.
291 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
293 const OpenGl_Vec3i& theValue);
295 //! Specifies the value of the integer uniform 4D vector.
296 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
297 const GLchar* theName,
298 const OpenGl_Vec4i& theValue);
300 //! Specifies the value of the integer uniform 4D vector.
301 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
303 const OpenGl_Vec4i& theValue);
307 //! Specifies the value of the float uniform variable.
308 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
309 const GLchar* theName,
312 //! Specifies the value of the float uniform variable.
313 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
317 //! Specifies the value of the float uniform 2D vector.
318 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
319 const GLchar* theName,
320 const OpenGl_Vec2& theValue);
322 //! Specifies the value of the float uniform 2D vector.
323 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
325 const OpenGl_Vec2& theValue);
327 //! Specifies the value of the float uniform 3D vector.
328 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
329 const GLchar* theName,
330 const OpenGl_Vec3& theValue);
332 //! Specifies the value of the float uniform 3D vector.
333 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
335 const OpenGl_Vec3& theValue);
337 //! Specifies the value of the float uniform 4D vector.
338 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
339 const GLchar* theName,
340 const OpenGl_Vec4& theValue);
342 //! Specifies the value of the float uniform 4D vector.
343 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
345 const OpenGl_Vec4& theValue);
349 //! Specifies the value of the float uniform 4x4 matrix.
350 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
351 const GLchar* theName,
352 const OpenGl_Matrix& theValue,
353 GLboolean theTranspose = GL_FALSE);
355 //! Specifies the value of the float uniform 4x4 matrix.
356 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
358 const OpenGl_Matrix& theValue,
359 GLboolean theTranspose = GL_FALSE);
361 //! Specifies the value of the float uniform 4x4 matrix.
362 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
363 const GLchar* theName,
364 const Tmatrix3& theValue,
365 GLboolean theTranspose = GL_FALSE);
367 //! Specifies the value of the float uniform 4x4 matrix.
368 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
370 const Tmatrix3& theValue,
371 GLboolean theTranspose = GL_FALSE);
373 //! Specifies the value of the float uniform array
374 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
377 const Standard_ShortReal* theData);
379 //! Specifies the value of the float2 uniform array
380 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
383 const OpenGl_Vec2* theData);
385 //! Specifies the value of the float3 uniform array
386 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
389 const OpenGl_Vec3* theData);
391 //! Specifies the value of the float4 uniform array
392 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
395 const OpenGl_Vec4* theData);
397 //! Specifies the value of the integer uniform array
398 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
401 const Standard_Integer* theData);
403 //! Specifies the value of the int2 uniform array
404 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
407 const OpenGl_Vec2i* theData);
409 //! Specifies the value of the int3 uniform array
410 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
413 const OpenGl_Vec3i* theData);
415 //! Specifies the value of the int4 uniform array
416 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
419 const OpenGl_Vec4i* theData);
423 //! Specifies the value of the sampler uniform variable.
424 Standard_EXPORT Standard_Boolean SetSampler (const Handle(OpenGl_Context)& theCtx,
425 const GLchar* theName,
426 const GLenum theTextureUnit);
428 //! Specifies the value of the sampler uniform variable.
429 Standard_EXPORT Standard_Boolean SetSampler (const Handle(OpenGl_Context)& theCtx,
431 const GLenum theTextureUnit);
435 //! Increments counter of users.
436 //! Used by OpenGl_ShaderManager.
437 //! @return true when resource has been restored from delayed release queue
440 return ++myShareCount == 1;
443 //! Decrements counter of users.
444 //! Used by OpenGl_ShaderManager.
445 //! @return true when there are no more users of this program has been left
448 return --myShareCount == 0;
453 GLuint myProgramID; //!< Handle of OpenGL shader program
454 OpenGl_ShaderList myShaderObjects; //!< List of attached shader objects
455 Handle(Graphic3d_ShaderProgram) myProxy; //!< Proxy shader program (from application layer)
456 Standard_Integer myShareCount; //!< program users count, initialized with 1 (already shared by one user)
460 //! Defines last modification for variables of each state type.
461 Standard_Size myCurrentState[MaxStateTypes];
463 //! Stores locations of OCCT state uniform variables.
464 GLint myStateLocations[OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES];
468 DEFINE_STANDARD_RTTI (OpenGl_ShaderProgram)
469 friend class OpenGl_ShaderManager;
474 struct OpenGl_VariableSetter : public OpenGl_SetterInterface
476 virtual void Set (const Handle(OpenGl_Context)& theCtx,
477 const Handle(Graphic3d_ShaderVariable)& theVariable,
478 OpenGl_ShaderProgram* theProgram)
480 theProgram->SetUniform (theCtx,
481 theVariable->Name().ToCString(),
482 theVariable->Value()->As<T>());
486 namespace OpenGl_HashMapInitializer
488 template<class K, class V>
491 NCollection_DataMap<K, V> myDictionary;
493 MapListOfType (K theKey, V theValue)
495 myDictionary.Bind (theKey, theValue);
498 MapListOfType& operator() (K theKey, V theValue)
500 myDictionary.Bind (theKey, theValue);
504 operator const NCollection_DataMap<K, V>& () const
510 template<class K, class V>
511 MapListOfType<K, V> CreateListOf (K theKey, V theValue)
513 return MapListOfType<K, V> (theKey, theValue);
517 #endif // _OpenGl_ShaderProgram_Header