0024888: Refactoring of OCCT CMake meta-project
[occt.git] / src / OpenGl / OpenGl_ShaderProgram.hxx
CommitLineData
30f0ad28 1// Created on: 2013-09-19
2// Created by: Denis BOGOLEPOV
d5f74e42 3// Copyright (c) 2013-2014 OPEN CASCADE SAS
30f0ad28 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
30f0ad28 6//
d5f74e42 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
973c2be1 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.
30f0ad28 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
30f0ad28 15
16#ifndef _OpenGl_ShaderProgram_Header
17#define _OpenGl_ShaderProgram_Header
18
19#include <NCollection_DataMap.hxx>
20#include <NCollection_Sequence.hxx>
21#include <TCollection_AsciiString.hxx>
22
23#include <Graphic3d_ShaderObject.hxx>
24#include <Graphic3d_ShaderProgram.hxx>
25
26#include <InterfaceGraphic_tgl_all.hxx>
27
28#include <OpenGl_Vec.hxx>
29#include <OpenGl_Matrix.hxx>
30#include <OpenGl_ShaderObject.hxx>
31#include <Handle_OpenGl_ShaderProgram.hxx>
32
33//! The enumeration of OCCT-specific OpenGL/GLSL variables.
34enum OpenGl_StateVariable
35{
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,
49
50 // OpenGL clip planes state
12381341 51 OpenGl_OCC_CLIP_PLANE_EQUATIONS,
52 OpenGl_OCC_CLIP_PLANE_SPACES,
5495fa7e 53 OpenGl_OCC_CLIP_PLANE_COUNT,
30f0ad28 54
55 // OpenGL light state
12381341 56 OpenGl_OCC_LIGHT_SOURCE_COUNT,
57 OpenGl_OCC_LIGHT_SOURCE_TYPES,
58 OpenGl_OCC_LIGHT_SOURCE_PARAMS,
59 OpenGl_OCC_LIGHT_AMBIENT,
30f0ad28 60
61 // Material state
62 OpenGl_OCCT_ACTIVE_SAMPLER,
63 OpenGl_OCCT_TEXTURE_ENABLE,
64 OpenGl_OCCT_DISTINGUISH_MODE,
12381341 65 OpenGl_OCCT_FRONT_MATERIAL,
66 OpenGl_OCCT_BACK_MATERIAL,
30f0ad28 67
68 // DON'T MODIFY THIS ITEM (insert new items before it)
69 OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES
70};
71
72class OpenGl_ShaderProgram;
73
74//! Interface for generic setter of user-defined uniform variables.
75struct OpenGl_SetterInterface
76{
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;
81
82 //! Destructor
83 virtual ~OpenGl_SetterInterface() {}
84};
85
86//! List of OpenGL shader objects.
87typedef NCollection_Sequence<Handle(OpenGl_ShaderObject)> OpenGl_ShaderList;
88
89//! List of shader variable setters.
90typedef NCollection_DataMap<size_t, OpenGl_SetterInterface*> OpenGl_SetterList;
91
92//! Support tool for setting user-defined uniform variables.
93class OpenGl_VariableSetterSelector
94{
95public:
96
97 //! Creates new setter selector.
98 OpenGl_VariableSetterSelector();
99
100 //! Releases memory resources of setter selector.
101 ~OpenGl_VariableSetterSelector();
102
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;
107
108private:
109
110 //! List of variable setters.
111 OpenGl_SetterList mySetterList;
112};
113
114//! Defines types of uniform state variables.
115enum OpenGl_UniformStateType
116{
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
123};
124
125//! Total number of state types.
126const int MaxStateTypes = 6;
127
128//! Wrapper for OpenGL program object.
129class OpenGl_ShaderProgram : public OpenGl_Resource
130{
fc73a202 131 friend class OpenGl_Workspace;
30f0ad28 132
133public:
134
135 //! Non-valid shader name.
136 static const GLuint NO_PROGRAM = 0;
137
138 //! Invalid location of uniform/attribute variable.
139 static const GLint INVALID_LOCATION = -1;
140
141 //! List of pre-defined OCCT state uniform variables.
142 static Standard_CString PredefinedKeywords[OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES];
143
144protected:
145
146 //! Creates uninitialized shader program.
147 Standard_EXPORT OpenGl_ShaderProgram (const Handle(Graphic3d_ShaderProgram)& theProxy = NULL);
148
149 static OpenGl_VariableSetterSelector mySetterSelector;
150
151public:
152
153 //! Releases resources of shader program.
154 Standard_EXPORT virtual ~OpenGl_ShaderProgram();
155
156 //! Creates new empty shader program of specified type.
157 Standard_EXPORT Standard_Boolean Create (const Handle(OpenGl_Context)& theCtx);
158
159 //! Destroys shader program.
10b9c7df 160 Standard_EXPORT virtual void Release (OpenGl_Context* theCtx);
30f0ad28 161
162 //! Attaches shader object to the program object.
163 Standard_EXPORT Standard_Boolean AttachShader (const Handle(OpenGl_Context)& theCtx,
164 const Handle(OpenGl_ShaderObject)& theShader);
165
166 //! Detaches shader object to the program object.
167 Standard_EXPORT Standard_Boolean DetachShader (const Handle(OpenGl_Context)& theCtx,
168 const Handle(OpenGl_ShaderObject)& theShader);
169
170 //! Initializes program object with the list of shader objects.
171 Standard_EXPORT Standard_Boolean Initialize (const Handle(OpenGl_Context)& theCtx,
172 const Graphic3d_ShaderObjectList& theShaders);
173
174 //! Links the program object.
175 Standard_EXPORT Standard_Boolean Link (const Handle(OpenGl_Context)& theCtx);
176
177 //! Fetches information log of the last link operation.
178 Standard_EXPORT Standard_Boolean FetchInfoLog (const Handle(OpenGl_Context)& theCtx,
179 TCollection_AsciiString& theLog);
180
181 //! Fetches uniform variables from proxy shader program.
182 Standard_EXPORT Standard_Boolean ApplyVariables (const Handle(OpenGl_Context)& theCtx);
183
184 //! Sets the program object as part of current rendering state.
185 Standard_EXPORT void Bind (const Handle(OpenGl_Context)& theCtx) const;
186
187 //! Binds the program object and applies variables from proxy shader program.
188 Standard_EXPORT Standard_Boolean BindWithVariables (const Handle(OpenGl_Context)& theCtx);
189
190 //! Reverts to fixed-function graphics pipeline (FFP).
191 Standard_EXPORT static void Unbind (const Handle(OpenGl_Context)& theCtx);
192
12381341 193 //! @return true if current object was initialized
194 inline bool IsValid() const
195 {
196 return myProgramID != NO_PROGRAM;
197 }
198
30f0ad28 199private:
200
201 //! Returns index of last modification of variables of specified state type.
202 Standard_EXPORT Standard_Size ActiveState (const OpenGl_UniformStateType theType) const;
203
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);
207
208public:
209
210 //! Returns location of the specific uniform variable.
211 Standard_EXPORT GLint GetUniformLocation (const Handle(OpenGl_Context)& theCtx,
212 const GLchar* theName) const;
213
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;
217
218 //! Returns location of the OCCT state uniform variable.
219 Standard_EXPORT GLint GetStateLocation (const GLuint theVariable) const;
220
221public:
222
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;
227
228 Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
229 GLint theLocation,
230 OpenGl_Vec4i& theValue) const;
231
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;
236
237 //! Returns the value of the float uniform variable.
238 Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
239 GLint theLocation,
240 OpenGl_Vec4& theValue) const;
241
242public:
243
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;
248
249 //! Returns the integer vertex attribute.
250 Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
251 GLint theIndex,
252 OpenGl_Vec4i& theValue) const;
253
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;
258
259 //! Returns the float vertex attribute.
260 Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
261 GLint theIndex,
262 OpenGl_Vec4& theValue) const;
263
264public:
265
fc73a202 266 //! Wrapper for glBindAttribLocation()
267 Standard_EXPORT Standard_Boolean SetAttributeName (const Handle(OpenGl_Context)& theCtx,
268 GLint theIndex,
269 const GLchar* theName);
270
271 //! Wrapper for glVertexAttrib1f()
272 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
273 const GLchar* theName,
274 GLfloat theValue);
275
276 //! Wrapper for glVertexAttrib1f()
277 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
278 GLint theIndex,
279 GLfloat theValue);
280
281 //! Wrapper for glVertexAttrib2fv()
282 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
283 const GLchar* theName,
284 const OpenGl_Vec2& theValue);
285
286 //! Wrapper for glVertexAttrib2fv()
287 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
288 GLint theIndex,
289 const OpenGl_Vec2& theValue);
290
291 //! Wrapper for glVertexAttrib3fv()
292 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
293 const GLchar* theName,
294 const OpenGl_Vec3& theValue);
295
296 //! Wrapper for glVertexAttrib3fv()
297 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
298 GLint theIndex,
299 const OpenGl_Vec3& theValue);
300
301 //! Wrapper for glVertexAttrib4fv()
302 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
303 const GLchar* theName,
304 const OpenGl_Vec4& theValue);
305
306 //! Wrapper for glVertexAttrib4fv()
307 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
308 GLint theIndex,
309 const OpenGl_Vec4& theValue);
310
311public:
312
30f0ad28 313 //! Specifies the value of the integer uniform variable.
314 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
315 const GLchar* theName,
316 GLint theValue);
317
318 //! Specifies the value of the integer uniform variable.
319 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
320 GLint theLocation,
321 GLint theValue);
322
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);
327
328 //! Specifies the value of the integer uniform 2D vector.
329 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
330 GLint theLocation,
331 const OpenGl_Vec2i& theValue);
332
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);
337
338 //! Specifies the value of the integer uniform 3D vector.
339 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
340 GLint theLocation,
341 const OpenGl_Vec3i& theValue);
342
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);
347
348 //! Specifies the value of the integer uniform 4D vector.
349 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
350 GLint theLocation,
351 const OpenGl_Vec4i& theValue);
352
353public:
354
355 //! Specifies the value of the float uniform variable.
356 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
357 const GLchar* theName,
358 GLfloat theValue);
359
360 //! Specifies the value of the float uniform variable.
361 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
362 GLint theLocation,
363 GLfloat theValue);
364
365 //! Specifies the value of the float uniform 2D vector.
366 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
367 const GLchar* theName,
368 const OpenGl_Vec2& theValue);
369
370 //! Specifies the value of the float uniform 2D vector.
371 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
372 GLint theLocation,
373 const OpenGl_Vec2& theValue);
374
375 //! Specifies the value of the float uniform 3D vector.
376 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
377 const GLchar* theName,
378 const OpenGl_Vec3& theValue);
379
380 //! Specifies the value of the float uniform 3D vector.
381 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
382 GLint theLocation,
383 const OpenGl_Vec3& theValue);
384
385 //! Specifies the value of the float uniform 4D vector.
386 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
387 const GLchar* theName,
388 const OpenGl_Vec4& theValue);
389
390 //! Specifies the value of the float uniform 4D vector.
391 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
392 GLint theLocation,
393 const OpenGl_Vec4& theValue);
394
395public:
396
397 //! Specifies the value of the float uniform 4x4 matrix.
398 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
399 const GLchar* theName,
400 const OpenGl_Matrix& theValue,
401 GLboolean theTranspose = GL_FALSE);
402
403 //! Specifies the value of the float uniform 4x4 matrix.
404 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
405 GLint theLocation,
406 const OpenGl_Matrix& theValue,
407 GLboolean theTranspose = GL_FALSE);
408
409 //! Specifies the value of the float uniform 4x4 matrix.
410 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
411 const GLchar* theName,
412 const Tmatrix3& theValue,
413 GLboolean theTranspose = GL_FALSE);
414
415 //! Specifies the value of the float uniform 4x4 matrix.
416 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
417 GLint theLocation,
418 const Tmatrix3& theValue,
419 GLboolean theTranspose = GL_FALSE);
420
4fe9ad57 421 //! Specifies the value of the float uniform array
422 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
423 GLint theLocation,
424 GLuint theCount,
425 const Standard_ShortReal* theData);
426
427 //! Specifies the value of the float2 uniform array
428 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
429 GLint theLocation,
430 GLuint theCount,
431 const OpenGl_Vec2* theData);
432
433 //! Specifies the value of the float3 uniform array
434 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
435 GLint theLocation,
436 GLuint theCount,
437 const OpenGl_Vec3* theData);
438
439 //! Specifies the value of the float4 uniform array
440 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
441 GLint theLocation,
442 GLuint theCount,
443 const OpenGl_Vec4* theData);
444
445 //! Specifies the value of the integer uniform array
446 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
447 GLint theLocation,
448 GLuint theCount,
449 const Standard_Integer* theData);
450
451 //! Specifies the value of the int2 uniform array
452 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
453 GLint theLocation,
454 GLuint theCount,
455 const OpenGl_Vec2i* theData);
456
457 //! Specifies the value of the int3 uniform array
458 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
459 GLint theLocation,
460 GLuint theCount,
461 const OpenGl_Vec3i* theData);
462
463 //! Specifies the value of the int4 uniform array
464 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
465 GLint theLocation,
466 GLuint theCount,
467 const OpenGl_Vec4i* theData);
468
30f0ad28 469public:
470
471 //! Specifies the value of the sampler uniform variable.
472 Standard_EXPORT Standard_Boolean SetSampler (const Handle(OpenGl_Context)& theCtx,
473 const GLchar* theName,
474 const GLenum theTextureUnit);
475
476 //! Specifies the value of the sampler uniform variable.
477 Standard_EXPORT Standard_Boolean SetSampler (const Handle(OpenGl_Context)& theCtx,
478 GLint theLocation,
479 const GLenum theTextureUnit);
480
481protected:
482
392ac980 483 //! Increments counter of users.
484 //! Used by OpenGl_ShaderManager.
05dd08ce 485 //! @return true when resource has been restored from delayed release queue
486 bool Share()
392ac980 487 {
05dd08ce 488 return ++myShareCount == 1;
392ac980 489 }
490
491 //! Decrements counter of users.
492 //! Used by OpenGl_ShaderManager.
493 //! @return true when there are no more users of this program has been left
494 bool UnShare()
495 {
496 return --myShareCount == 0;
497 }
498
499protected:
500
501 GLuint myProgramID; //!< Handle of OpenGL shader program
502 OpenGl_ShaderList myShaderObjects; //!< List of attached shader objects
503 Handle(Graphic3d_ShaderProgram) myProxy; //!< Proxy shader program (from application layer)
504 Standard_Integer myShareCount; //!< program users count, initialized with 1 (already shared by one user)
30f0ad28 505
506protected:
507
508 //! Defines last modification for variables of each state type.
509 Standard_Size myCurrentState[MaxStateTypes];
510
511 //! Stores locations of OCCT state uniform variables.
512 GLint myStateLocations[OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES];
513
514public:
515
516 DEFINE_STANDARD_RTTI (OpenGl_ShaderProgram)
517 friend class OpenGl_ShaderManager;
518
519};
520
521template<class T>
522struct OpenGl_VariableSetter : public OpenGl_SetterInterface
523{
524 virtual void Set (const Handle(OpenGl_Context)& theCtx,
525 const Handle(Graphic3d_ShaderVariable)& theVariable,
526 OpenGl_ShaderProgram* theProgram)
527 {
528 theProgram->SetUniform (theCtx,
529 theVariable->Name().ToCString(),
530 theVariable->Value()->As<T>());
531 }
532};
533
534namespace OpenGl_HashMapInitializer
535{
536 template<class K, class V>
537 struct MapListOfType
538 {
539 NCollection_DataMap<K, V> myDictionary;
540
541 MapListOfType (K theKey, V theValue)
542 {
543 myDictionary.Bind (theKey, theValue);
544 }
545
546 MapListOfType& operator() (K theKey, V theValue)
547 {
548 myDictionary.Bind (theKey, theValue);
549 return *this;
550 }
551
552 operator const NCollection_DataMap<K, V>& () const
553 {
554 return myDictionary;
555 }
556 };
557
558 template<class K, class V>
559 MapListOfType<K, V> CreateListOf (K theKey, V theValue)
560 {
561 return MapListOfType<K, V> (theKey, theValue);
562 }
563}
564
565#endif // _OpenGl_ShaderProgram_Header