1 // Created on: 2013-09-19
2 // Created by: Denis BOGOLEPOV
3 // Copyright (c) 2013 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
20 #ifndef _OpenGl_ShaderProgram_Header
21 #define _OpenGl_ShaderProgram_Header
23 #include <NCollection_DataMap.hxx>
24 #include <NCollection_Sequence.hxx>
25 #include <TCollection_AsciiString.hxx>
27 #include <Graphic3d_ShaderObject.hxx>
28 #include <Graphic3d_ShaderProgram.hxx>
30 #include <InterfaceGraphic_tgl_all.hxx>
32 #include <OpenGl_Vec.hxx>
33 #include <OpenGl_Matrix.hxx>
34 #include <OpenGl_ShaderObject.hxx>
35 #include <Handle_OpenGl_ShaderProgram.hxx>
37 //! The enumeration of OCCT-specific OpenGL/GLSL variables.
38 enum OpenGl_StateVariable
40 // OpenGL matrix state
41 OpenGl_OCC_MODEL_WORLD_MATRIX,
42 OpenGl_OCC_WORLD_VIEW_MATRIX,
43 OpenGl_OCC_PROJECTION_MATRIX,
44 OpenGl_OCC_MODEL_WORLD_MATRIX_INVERSE,
45 OpenGl_OCC_WORLD_VIEW_MATRIX_INVERSE,
46 OpenGl_OCC_PROJECTION_MATRIX_INVERSE,
47 OpenGl_OCC_MODEL_WORLD_MATRIX_TRANSPOSE,
48 OpenGl_OCC_WORLD_VIEW_MATRIX_TRANSPOSE,
49 OpenGl_OCC_PROJECTION_MATRIX_TRANSPOSE,
50 OpenGl_OCC_MODEL_WORLD_MATRIX_INVERSE_TRANSPOSE,
51 OpenGl_OCC_WORLD_VIEW_MATRIX_INVERSE_TRANSPOSE,
52 OpenGl_OCC_PROJECTION_MATRIX_INVERSE_TRANSPOSE,
54 // OpenGL clip planes state
55 OpenGl_OCC_CLIP_PLANE_EQUATIONS,
56 OpenGl_OCC_CLIP_PLANE_SPACES,
59 OpenGl_OCC_LIGHT_SOURCE_COUNT,
60 OpenGl_OCC_LIGHT_SOURCE_TYPES,
61 OpenGl_OCC_LIGHT_SOURCE_PARAMS,
62 OpenGl_OCC_LIGHT_AMBIENT,
65 OpenGl_OCCT_ACTIVE_SAMPLER,
66 OpenGl_OCCT_TEXTURE_ENABLE,
67 OpenGl_OCCT_DISTINGUISH_MODE,
68 OpenGl_OCCT_FRONT_MATERIAL,
69 OpenGl_OCCT_BACK_MATERIAL,
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
137 //! Non-valid shader name.
138 static const GLuint NO_PROGRAM = 0;
140 //! Invalid location of uniform/attribute variable.
141 static const GLint INVALID_LOCATION = -1;
143 //! List of pre-defined OCCT state uniform variables.
144 static Standard_CString PredefinedKeywords[OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES];
148 //! Creates uninitialized shader program.
149 Standard_EXPORT OpenGl_ShaderProgram (const Handle(Graphic3d_ShaderProgram)& theProxy = NULL);
151 static OpenGl_VariableSetterSelector mySetterSelector;
155 //! Releases resources of shader program.
156 Standard_EXPORT virtual ~OpenGl_ShaderProgram();
158 //! Creates new empty shader program of specified type.
159 Standard_EXPORT Standard_Boolean Create (const Handle(OpenGl_Context)& theCtx);
161 //! Destroys shader program.
162 Standard_EXPORT virtual void Release (const OpenGl_Context* theCtx);
164 //! Attaches shader object to the program object.
165 Standard_EXPORT Standard_Boolean AttachShader (const Handle(OpenGl_Context)& theCtx,
166 const Handle(OpenGl_ShaderObject)& theShader);
168 //! Detaches shader object to the program object.
169 Standard_EXPORT Standard_Boolean DetachShader (const Handle(OpenGl_Context)& theCtx,
170 const Handle(OpenGl_ShaderObject)& theShader);
172 //! Initializes program object with the list of shader objects.
173 Standard_EXPORT Standard_Boolean Initialize (const Handle(OpenGl_Context)& theCtx,
174 const Graphic3d_ShaderObjectList& theShaders);
176 //! Links the program object.
177 Standard_EXPORT Standard_Boolean Link (const Handle(OpenGl_Context)& theCtx);
179 //! Fetches information log of the last link operation.
180 Standard_EXPORT Standard_Boolean FetchInfoLog (const Handle(OpenGl_Context)& theCtx,
181 TCollection_AsciiString& theLog);
183 //! Fetches uniform variables from proxy shader program.
184 Standard_EXPORT Standard_Boolean ApplyVariables (const Handle(OpenGl_Context)& theCtx);
186 //! Sets the program object as part of current rendering state.
187 Standard_EXPORT void Bind (const Handle(OpenGl_Context)& theCtx) const;
189 //! Binds the program object and applies variables from proxy shader program.
190 Standard_EXPORT Standard_Boolean BindWithVariables (const Handle(OpenGl_Context)& theCtx);
192 //! Reverts to fixed-function graphics pipeline (FFP).
193 Standard_EXPORT static void Unbind (const Handle(OpenGl_Context)& theCtx);
195 //! @return true if current object was initialized
196 inline bool IsValid() const
198 return myProgramID != NO_PROGRAM;
203 //! Returns index of last modification of variables of specified state type.
204 Standard_EXPORT Standard_Size ActiveState (const OpenGl_UniformStateType theType) const;
206 //! Updates index of last modification of variables of specified state type.
207 Standard_EXPORT void UpdateState (const OpenGl_UniformStateType theType,
208 const Standard_Size theIndex);
212 //! Returns location of the specific uniform variable.
213 Standard_EXPORT GLint GetUniformLocation (const Handle(OpenGl_Context)& theCtx,
214 const GLchar* theName) const;
216 //! Returns index of the generic vertex attribute by variable name.
217 Standard_EXPORT GLint GetAttributeLocation (const Handle(OpenGl_Context)& theCtx,
218 const GLchar* theName) const;
220 //! Returns location of the OCCT state uniform variable.
221 Standard_EXPORT GLint GetStateLocation (const GLuint theVariable) const;
225 //! Returns the value of the integer uniform variable.
226 Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
227 const GLchar* theName,
228 OpenGl_Vec4i& theValue) const;
230 Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
232 OpenGl_Vec4i& theValue) const;
234 //! Returns the value of the float uniform variable.
235 Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
236 const GLchar* theName,
237 OpenGl_Vec4& theValue) const;
239 //! Returns the value of the float uniform variable.
240 Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
242 OpenGl_Vec4& theValue) const;
246 //! Returns the integer vertex attribute.
247 Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
248 const GLchar* theName,
249 OpenGl_Vec4i& theValue) const;
251 //! Returns the integer vertex attribute.
252 Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
254 OpenGl_Vec4i& theValue) const;
256 //! Returns the float vertex attribute.
257 Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
258 const GLchar* theName,
259 OpenGl_Vec4& theValue) const;
261 //! Returns the float vertex attribute.
262 Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
264 OpenGl_Vec4& theValue) const;
268 //! Specifies the value of the integer uniform variable.
269 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
270 const GLchar* theName,
273 //! Specifies the value of the integer uniform variable.
274 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
278 //! Specifies the value of the integer uniform 2D vector.
279 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
280 const GLchar* theName,
281 const OpenGl_Vec2i& theValue);
283 //! Specifies the value of the integer uniform 2D vector.
284 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
286 const OpenGl_Vec2i& theValue);
288 //! Specifies the value of the integer uniform 3D vector.
289 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
290 const GLchar* theName,
291 const OpenGl_Vec3i& theValue);
293 //! Specifies the value of the integer uniform 3D vector.
294 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
296 const OpenGl_Vec3i& theValue);
298 //! Specifies the value of the integer uniform 4D vector.
299 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
300 const GLchar* theName,
301 const OpenGl_Vec4i& theValue);
303 //! Specifies the value of the integer uniform 4D vector.
304 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
306 const OpenGl_Vec4i& theValue);
310 //! Specifies the value of the float uniform variable.
311 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
312 const GLchar* theName,
315 //! Specifies the value of the float uniform variable.
316 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
320 //! Specifies the value of the float uniform 2D vector.
321 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
322 const GLchar* theName,
323 const OpenGl_Vec2& theValue);
325 //! Specifies the value of the float uniform 2D vector.
326 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
328 const OpenGl_Vec2& theValue);
330 //! Specifies the value of the float uniform 3D vector.
331 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
332 const GLchar* theName,
333 const OpenGl_Vec3& theValue);
335 //! Specifies the value of the float uniform 3D vector.
336 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
338 const OpenGl_Vec3& theValue);
340 //! Specifies the value of the float uniform 4D vector.
341 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
342 const GLchar* theName,
343 const OpenGl_Vec4& theValue);
345 //! Specifies the value of the float uniform 4D vector.
346 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
348 const OpenGl_Vec4& theValue);
352 //! Specifies the value of the float uniform 4x4 matrix.
353 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
354 const GLchar* theName,
355 const OpenGl_Matrix& theValue,
356 GLboolean theTranspose = GL_FALSE);
358 //! Specifies the value of the float uniform 4x4 matrix.
359 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
361 const OpenGl_Matrix& theValue,
362 GLboolean theTranspose = GL_FALSE);
364 //! Specifies the value of the float uniform 4x4 matrix.
365 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
366 const GLchar* theName,
367 const Tmatrix3& theValue,
368 GLboolean theTranspose = GL_FALSE);
370 //! Specifies the value of the float uniform 4x4 matrix.
371 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
373 const Tmatrix3& theValue,
374 GLboolean theTranspose = GL_FALSE);
376 //! Specifies the value of the float uniform array
377 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
380 const Standard_ShortReal* theData);
382 //! Specifies the value of the float2 uniform array
383 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
386 const OpenGl_Vec2* theData);
388 //! Specifies the value of the float3 uniform array
389 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
392 const OpenGl_Vec3* theData);
394 //! Specifies the value of the float4 uniform array
395 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
398 const OpenGl_Vec4* theData);
400 //! Specifies the value of the integer uniform array
401 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
404 const Standard_Integer* theData);
406 //! Specifies the value of the int2 uniform array
407 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
410 const OpenGl_Vec2i* theData);
412 //! Specifies the value of the int3 uniform array
413 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
416 const OpenGl_Vec3i* theData);
418 //! Specifies the value of the int4 uniform array
419 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
422 const OpenGl_Vec4i* theData);
426 //! Specifies the value of the sampler uniform variable.
427 Standard_EXPORT Standard_Boolean SetSampler (const Handle(OpenGl_Context)& theCtx,
428 const GLchar* theName,
429 const GLenum theTextureUnit);
431 //! Specifies the value of the sampler uniform variable.
432 Standard_EXPORT Standard_Boolean SetSampler (const Handle(OpenGl_Context)& theCtx,
434 const GLenum theTextureUnit);
438 //! Increments counter of users.
439 //! Used by OpenGl_ShaderManager.
440 //! @return true when resource has been restored from delayed release queue
443 return ++myShareCount == 1;
446 //! Decrements counter of users.
447 //! Used by OpenGl_ShaderManager.
448 //! @return true when there are no more users of this program has been left
451 return --myShareCount == 0;
456 GLuint myProgramID; //!< Handle of OpenGL shader program
457 OpenGl_ShaderList myShaderObjects; //!< List of attached shader objects
458 Handle(Graphic3d_ShaderProgram) myProxy; //!< Proxy shader program (from application layer)
459 Standard_Integer myShareCount; //!< program users count, initialized with 1 (already shared by one user)
463 //! Defines last modification for variables of each state type.
464 Standard_Size myCurrentState[MaxStateTypes];
466 //! Stores locations of OCCT state uniform variables.
467 GLint myStateLocations[OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES];
471 DEFINE_STANDARD_RTTI (OpenGl_ShaderProgram)
472 friend class OpenGl_ShaderManager;
477 struct OpenGl_VariableSetter : public OpenGl_SetterInterface
479 virtual void Set (const Handle(OpenGl_Context)& theCtx,
480 const Handle(Graphic3d_ShaderVariable)& theVariable,
481 OpenGl_ShaderProgram* theProgram)
483 theProgram->SetUniform (theCtx,
484 theVariable->Name().ToCString(),
485 theVariable->Value()->As<T>());
489 namespace OpenGl_HashMapInitializer
491 template<class K, class V>
494 NCollection_DataMap<K, V> myDictionary;
496 MapListOfType (K theKey, V theValue)
498 myDictionary.Bind (theKey, theValue);
501 MapListOfType& operator() (K theKey, V theValue)
503 myDictionary.Bind (theKey, theValue);
507 operator const NCollection_DataMap<K, V>& () const
513 template<class K, class V>
514 MapListOfType<K, V> CreateListOf (K theKey, V theValue)
516 return MapListOfType<K, V> (theKey, theValue);
520 #endif // _OpenGl_ShaderProgram_Header