0030520: VIS - IVtkTools_ShapePicker::GetPickPosition() returns incorrect point
[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
30f0ad28 26#include <OpenGl_Vec.hxx>
27#include <OpenGl_Matrix.hxx>
cc8cbabe 28#include <OpenGl_NamedResource.hxx>
30f0ad28 29#include <OpenGl_ShaderObject.hxx>
30f0ad28 30
c04c30b3 31class OpenGl_ShaderProgram;
cc8cbabe 32DEFINE_STANDARD_HANDLE(OpenGl_ShaderProgram, OpenGl_NamedResource)
c04c30b3 33
30f0ad28 34//! The enumeration of OCCT-specific OpenGL/GLSL variables.
35enum OpenGl_StateVariable
36{
37 // OpenGL matrix state
38 OpenGl_OCC_MODEL_WORLD_MATRIX,
39 OpenGl_OCC_WORLD_VIEW_MATRIX,
40 OpenGl_OCC_PROJECTION_MATRIX,
41 OpenGl_OCC_MODEL_WORLD_MATRIX_INVERSE,
42 OpenGl_OCC_WORLD_VIEW_MATRIX_INVERSE,
43 OpenGl_OCC_PROJECTION_MATRIX_INVERSE,
44 OpenGl_OCC_MODEL_WORLD_MATRIX_TRANSPOSE,
45 OpenGl_OCC_WORLD_VIEW_MATRIX_TRANSPOSE,
46 OpenGl_OCC_PROJECTION_MATRIX_TRANSPOSE,
47 OpenGl_OCC_MODEL_WORLD_MATRIX_INVERSE_TRANSPOSE,
48 OpenGl_OCC_WORLD_VIEW_MATRIX_INVERSE_TRANSPOSE,
49 OpenGl_OCC_PROJECTION_MATRIX_INVERSE_TRANSPOSE,
50
51 // OpenGL clip planes state
12381341 52 OpenGl_OCC_CLIP_PLANE_EQUATIONS,
25c35042 53 OpenGl_OCC_CLIP_PLANE_CHAINS,
5495fa7e 54 OpenGl_OCC_CLIP_PLANE_COUNT,
30f0ad28 55
56 // OpenGL light state
12381341 57 OpenGl_OCC_LIGHT_SOURCE_COUNT,
58 OpenGl_OCC_LIGHT_SOURCE_TYPES,
59 OpenGl_OCC_LIGHT_SOURCE_PARAMS,
60 OpenGl_OCC_LIGHT_AMBIENT,
30f0ad28 61
62 // Material state
30f0ad28 63 OpenGl_OCCT_TEXTURE_ENABLE,
64 OpenGl_OCCT_DISTINGUISH_MODE,
12381341 65 OpenGl_OCCT_FRONT_MATERIAL,
66 OpenGl_OCCT_BACK_MATERIAL,
c40eb6b9 67 OpenGl_OCCT_ALPHA_CUTOFF,
8625ef7e 68 OpenGl_OCCT_COLOR,
69
a1073ae2 70 // Weighted, Blended Order-Independent Transparency rendering state
71 OpenGl_OCCT_OIT_OUTPUT,
72 OpenGl_OCCT_OIT_DEPTH_FACTOR,
73
74 // Context-dependent state
79f4f036 75 OpenGl_OCCT_TEXTURE_TRSF2D,
8625ef7e 76 OpenGl_OCCT_POINT_SIZE,
30f0ad28 77
2a332745 78 // Wireframe state
79 OpenGl_OCCT_VIEWPORT,
80 OpenGl_OCCT_LINE_WIDTH,
81 OpenGl_OCCT_LINE_FEATHER,
82 OpenGl_OCCT_WIREFRAME_COLOR,
83 OpenGl_OCCT_QUAD_MODE_STATE,
84
30f0ad28 85 // DON'T MODIFY THIS ITEM (insert new items before it)
86 OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES
87};
88
30f0ad28 89//! Interface for generic setter of user-defined uniform variables.
90struct OpenGl_SetterInterface
91{
92 //! Sets user-defined uniform variable to specified program.
93 virtual void Set (const Handle(OpenGl_Context)& theCtx,
94 const Handle(Graphic3d_ShaderVariable)& theVariable,
95 OpenGl_ShaderProgram* theProgram) = 0;
96
97 //! Destructor
98 virtual ~OpenGl_SetterInterface() {}
99};
100
101//! List of OpenGL shader objects.
102typedef NCollection_Sequence<Handle(OpenGl_ShaderObject)> OpenGl_ShaderList;
103
104//! List of shader variable setters.
105typedef NCollection_DataMap<size_t, OpenGl_SetterInterface*> OpenGl_SetterList;
106
107//! Support tool for setting user-defined uniform variables.
108class OpenGl_VariableSetterSelector
109{
110public:
111
112 //! Creates new setter selector.
113 OpenGl_VariableSetterSelector();
114
115 //! Releases memory resources of setter selector.
116 ~OpenGl_VariableSetterSelector();
117
118 //! Sets user-defined uniform variable to specified program.
119 void Set (const Handle(OpenGl_Context)& theCtx,
120 const Handle(Graphic3d_ShaderVariable)& theVariable,
121 OpenGl_ShaderProgram* theProgram) const;
122
123private:
124
125 //! List of variable setters.
126 OpenGl_SetterList mySetterList;
127};
128
129//! Defines types of uniform state variables.
130enum OpenGl_UniformStateType
131{
132 OpenGl_LIGHT_SOURCES_STATE,
133 OpenGl_CLIP_PLANES_STATE,
134 OpenGl_MODEL_WORLD_STATE,
135 OpenGl_WORLD_VIEW_STATE,
136 OpenGl_PROJECTION_STATE,
8613985b 137 OpenGl_MATERIAL_STATE,
138 OpenGl_SURF_DETAIL_STATE,
a1073ae2 139 OpenGL_OIT_STATE,
8613985b 140 OpenGl_UniformStateType_NB
30f0ad28 141};
142
2a332745 143//! Simple class represents GLSL program variable location.
144class OpenGl_ShaderUniformLocation
145{
146public:
147 //! Invalid location of uniform/attribute variable.
148 static const GLint INVALID_LOCATION = -1;
149public:
150
151 //! Construct an invalid location.
152 OpenGl_ShaderUniformLocation() : myLocation (INVALID_LOCATION) {}
153
154 //! Constructor with initialization.
155 explicit OpenGl_ShaderUniformLocation (GLint theLocation) : myLocation (theLocation) {}
156
157 //! Note you may safely put invalid location in functions like glUniform* - the data passed in will be silently ignored.
158 //! @return true if location is not equal to -1.
159 bool IsValid() const { return myLocation != INVALID_LOCATION; }
160
161 //! Return TRUE for non-invalid location.
162 operator bool() const { return myLocation != INVALID_LOCATION; }
163
164 //! Convert operators help silently put object to GL functions like glUniform*.
165 operator GLint() const { return myLocation; }
166
167private:
168 GLint myLocation;
169};
170
30f0ad28 171//! Wrapper for OpenGL program object.
cc8cbabe 172class OpenGl_ShaderProgram : public OpenGl_NamedResource
30f0ad28 173{
91c60b57 174 friend class OpenGl_View;
cc8cbabe 175 friend class OpenGl_ShaderManager;
176 DEFINE_STANDARD_RTTIEXT(OpenGl_ShaderProgram, OpenGl_NamedResource)
30f0ad28 177public:
178
179 //! Non-valid shader name.
180 static const GLuint NO_PROGRAM = 0;
181
182 //! Invalid location of uniform/attribute variable.
183 static const GLint INVALID_LOCATION = -1;
184
185 //! List of pre-defined OCCT state uniform variables.
186 static Standard_CString PredefinedKeywords[OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES];
187
2bda8346 188 //! Wrapper for compiling shader object with verbose printing on error.
189 Standard_EXPORT static bool compileShaderVerbose (const Handle(OpenGl_Context)& theCtx,
190 const Handle(OpenGl_ShaderObject)& theShader,
191 const TCollection_AsciiString& theSource,
192 bool theToPrintSource = true);
193
30f0ad28 194 //! Creates uninitialized shader program.
5440a790 195 //!
196 //! WARNING! This constructor is not intended to be called anywhere but from OpenGl_ShaderManager::Create().
197 //! Manager has been designed to synchronize camera position, lights definition and other aspects of the program implicitly,
198 //! as well as sharing same program across rendering groups.
199 //!
200 //! Program created outside the manager will be left detached from these routines,
201 //! and them should be performed manually by caller.
202 //!
203 //! This constructor has been made public to provide more flexibility to re-use OCCT OpenGL classes without OCCT Viewer itself.
204 //! If this is not the case - create the program using shared OpenGl_ShaderManager instance instead.
30f0ad28 205 Standard_EXPORT OpenGl_ShaderProgram (const Handle(Graphic3d_ShaderProgram)& theProxy = NULL);
206
5440a790 207protected:
208
30f0ad28 209 static OpenGl_VariableSetterSelector mySetterSelector;
210
211public:
212
213 //! Releases resources of shader program.
214 Standard_EXPORT virtual ~OpenGl_ShaderProgram();
215
216 //! Creates new empty shader program of specified type.
217 Standard_EXPORT Standard_Boolean Create (const Handle(OpenGl_Context)& theCtx);
218
219 //! Destroys shader program.
79104795 220 Standard_EXPORT virtual void Release (OpenGl_Context* theCtx) Standard_OVERRIDE;
30f0ad28 221
15669413 222 //! Returns estimated GPU memory usage - cannot be easily estimated.
223 virtual Standard_Size EstimatedDataSize() const Standard_OVERRIDE { return 0; }
224
30f0ad28 225 //! Attaches shader object to the program object.
226 Standard_EXPORT Standard_Boolean AttachShader (const Handle(OpenGl_Context)& theCtx,
227 const Handle(OpenGl_ShaderObject)& theShader);
228
229 //! Detaches shader object to the program object.
230 Standard_EXPORT Standard_Boolean DetachShader (const Handle(OpenGl_Context)& theCtx,
231 const Handle(OpenGl_ShaderObject)& theShader);
232
233 //! Initializes program object with the list of shader objects.
234 Standard_EXPORT Standard_Boolean Initialize (const Handle(OpenGl_Context)& theCtx,
235 const Graphic3d_ShaderObjectList& theShaders);
236
237 //! Links the program object.
2bda8346 238 //! @param theCtx bound OpenGL context
239 //! @param theIsVerbose flag to print log on error
240 Standard_EXPORT Standard_Boolean Link (const Handle(OpenGl_Context)& theCtx,
241 bool theIsVerbose = true);
30f0ad28 242
243 //! Fetches information log of the last link operation.
244 Standard_EXPORT Standard_Boolean FetchInfoLog (const Handle(OpenGl_Context)& theCtx,
245 TCollection_AsciiString& theLog);
246
247 //! Fetches uniform variables from proxy shader program.
248 Standard_EXPORT Standard_Boolean ApplyVariables (const Handle(OpenGl_Context)& theCtx);
249
12381341 250 //! @return true if current object was initialized
251 inline bool IsValid() const
252 {
253 return myProgramID != NO_PROGRAM;
254 }
255
7d3e64ef 256 //! @return program ID
257 inline GLuint ProgramId() const
258 {
259 return myProgramID;
260 }
261
daf73ab7 262public:
263
8e0a2b19 264 //! Return TRUE if program defines tessellation stage.
265 Standard_Boolean HasTessellationStage() const { return myHasTessShader; }
266
daf73ab7 267 //! Return the length of array of light sources (THE_MAX_LIGHTS),
268 //! to be used for initialization occLightSources (OpenGl_OCC_LIGHT_SOURCE_PARAMS).
269 Standard_Integer NbLightsMax() const { return myNbLightsMax; }
270
271 //! Return the length of array of clipping planes (THE_MAX_CLIP_PLANES),
25c35042 272 //! to be used for initialization occClipPlaneEquations (OpenGl_OCC_CLIP_PLANE_EQUATIONS) and occClipPlaneChains (OpenGl_OCC_CLIP_PLANE_CHAINS).
daf73ab7 273 Standard_Integer NbClipPlanesMax() const { return myNbClipPlanesMax; }
274
b17e5bae 275 //! Return the length of array of Fragment Shader outputs (THE_NB_FRAG_OUTPUTS),
276 //! to be used for initialization occFragColorArray/occFragColorN.
277 Standard_Integer NbFragmentOutputs() const { return myNbFragOutputs; }
278
c40eb6b9 279 //! Return true if Fragment Shader should perform alpha test; FALSE by default.
280 Standard_Boolean HasAlphaTest() const { return myHasAlphaTest; }
281
b17e5bae 282 //! Return true if Fragment Shader color should output the weighted OIT coverage; FALSE by default.
283 Standard_Boolean HasWeightOitOutput() const { return myHasWeightOitOutput; }
284
30f0ad28 285private:
286
287 //! Returns index of last modification of variables of specified state type.
8613985b 288 Standard_Size ActiveState (const OpenGl_UniformStateType theType) const
289 {
290 return theType < OpenGl_UniformStateType_NB
291 ? myCurrentState[theType]
292 : 0;
293 }
30f0ad28 294
295 //! Updates index of last modification of variables of specified state type.
8613985b 296 void UpdateState (const OpenGl_UniformStateType theType,
297 const Standard_Size theIndex)
298 {
299 if (theType < OpenGl_UniformStateType_NB)
300 {
301 myCurrentState[theType] = theIndex;
302 }
303 }
30f0ad28 304
305public:
306
307 //! Returns location of the specific uniform variable.
2a332745 308 Standard_EXPORT OpenGl_ShaderUniformLocation GetUniformLocation (const Handle(OpenGl_Context)& theCtx,
309 const GLchar* theName) const;
30f0ad28 310
311 //! Returns index of the generic vertex attribute by variable name.
312 Standard_EXPORT GLint GetAttributeLocation (const Handle(OpenGl_Context)& theCtx,
313 const GLchar* theName) const;
314
315 //! Returns location of the OCCT state uniform variable.
2a332745 316 const OpenGl_ShaderUniformLocation& GetStateLocation (OpenGl_StateVariable theVariable) const { return myStateLocations[theVariable]; }
30f0ad28 317
318public:
319
320 //! Returns the value of the integer uniform variable.
321 Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
322 const GLchar* theName,
323 OpenGl_Vec4i& theValue) const;
324
325 Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
326 GLint theLocation,
327 OpenGl_Vec4i& theValue) const;
328
329 //! Returns the value of the float uniform variable.
330 Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
331 const GLchar* theName,
332 OpenGl_Vec4& theValue) const;
333
334 //! Returns the value of the float uniform variable.
335 Standard_EXPORT Standard_Boolean GetUniform (const Handle(OpenGl_Context)& theCtx,
336 GLint theLocation,
337 OpenGl_Vec4& theValue) const;
338
339public:
340
341 //! Returns the integer vertex attribute.
342 Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
343 const GLchar* theName,
344 OpenGl_Vec4i& theValue) const;
345
346 //! Returns the integer vertex attribute.
347 Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
348 GLint theIndex,
349 OpenGl_Vec4i& theValue) const;
350
351 //! Returns the float vertex attribute.
352 Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
353 const GLchar* theName,
354 OpenGl_Vec4& theValue) const;
355
356 //! Returns the float vertex attribute.
357 Standard_EXPORT Standard_Boolean GetAttribute (const Handle(OpenGl_Context)& theCtx,
358 GLint theIndex,
359 OpenGl_Vec4& theValue) const;
360
361public:
362
fc73a202 363 //! Wrapper for glBindAttribLocation()
364 Standard_EXPORT Standard_Boolean SetAttributeName (const Handle(OpenGl_Context)& theCtx,
365 GLint theIndex,
366 const GLchar* theName);
367
368 //! Wrapper for glVertexAttrib1f()
369 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
370 const GLchar* theName,
371 GLfloat theValue);
372
373 //! Wrapper for glVertexAttrib1f()
374 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
375 GLint theIndex,
376 GLfloat theValue);
377
378 //! Wrapper for glVertexAttrib2fv()
379 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
380 const GLchar* theName,
381 const OpenGl_Vec2& theValue);
382
383 //! Wrapper for glVertexAttrib2fv()
384 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
385 GLint theIndex,
386 const OpenGl_Vec2& theValue);
387
388 //! Wrapper for glVertexAttrib3fv()
389 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
390 const GLchar* theName,
391 const OpenGl_Vec3& theValue);
392
393 //! Wrapper for glVertexAttrib3fv()
394 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
395 GLint theIndex,
396 const OpenGl_Vec3& theValue);
397
398 //! Wrapper for glVertexAttrib4fv()
399 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
400 const GLchar* theName,
401 const OpenGl_Vec4& theValue);
402
403 //! Wrapper for glVertexAttrib4fv()
404 Standard_EXPORT Standard_Boolean SetAttribute (const Handle(OpenGl_Context)& theCtx,
405 GLint theIndex,
406 const OpenGl_Vec4& theValue);
407
408public:
409
30f0ad28 410 //! Specifies the value of the integer uniform variable.
411 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
412 const GLchar* theName,
413 GLint theValue);
414
415 //! Specifies the value of the integer uniform variable.
416 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
417 GLint theLocation,
418 GLint theValue);
419
420 //! Specifies the value of the integer uniform 2D vector.
421 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
422 const GLchar* theName,
423 const OpenGl_Vec2i& theValue);
424
425 //! Specifies the value of the integer uniform 2D vector.
426 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
427 GLint theLocation,
428 const OpenGl_Vec2i& theValue);
429
430 //! Specifies the value of the integer uniform 3D vector.
431 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
432 const GLchar* theName,
433 const OpenGl_Vec3i& theValue);
434
435 //! Specifies the value of the integer uniform 3D vector.
436 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
437 GLint theLocation,
438 const OpenGl_Vec3i& theValue);
439
440 //! Specifies the value of the integer uniform 4D vector.
441 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
442 const GLchar* theName,
443 const OpenGl_Vec4i& theValue);
444
445 //! Specifies the value of the integer uniform 4D vector.
446 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
447 GLint theLocation,
448 const OpenGl_Vec4i& theValue);
449
450public:
451
47e9c178 452 //! Specifies the value of the unsigned integer uniform 2D vector (uvec2).
25ef750e 453 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
454 const GLchar* theName,
47e9c178 455 const OpenGl_Vec2u& theValue);
25ef750e 456
47e9c178 457 //! Specifies the value of the unsigned integer uniform 2D vector (uvec2).
25ef750e 458 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
459 GLint theLocation,
47e9c178 460 const OpenGl_Vec2u& theValue);
25ef750e 461
47e9c178 462 //! Specifies the value of the uvec2 uniform array
25ef750e 463 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
464 const GLchar* theName,
465 const GLsizei theCount,
47e9c178 466 const OpenGl_Vec2u* theValue);
25ef750e 467
47e9c178 468 //! Specifies the value of the uvec2 uniform array
25ef750e 469 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
470 GLint theLocation,
471 const GLsizei theCount,
47e9c178 472 const OpenGl_Vec2u* theValue);
25ef750e 473
474public:
475
30f0ad28 476 //! Specifies the value of the float uniform variable.
477 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
478 const GLchar* theName,
479 GLfloat theValue);
480
481 //! Specifies the value of the float uniform variable.
482 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
483 GLint theLocation,
484 GLfloat theValue);
485
486 //! Specifies the value of the float uniform 2D vector.
487 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
488 const GLchar* theName,
489 const OpenGl_Vec2& theValue);
490
491 //! Specifies the value of the float uniform 2D vector.
492 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
493 GLint theLocation,
494 const OpenGl_Vec2& theValue);
495
496 //! Specifies the value of the float uniform 3D vector.
497 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
498 const GLchar* theName,
499 const OpenGl_Vec3& theValue);
500
501 //! Specifies the value of the float uniform 3D vector.
502 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
503 GLint theLocation,
504 const OpenGl_Vec3& theValue);
505
506 //! Specifies the value of the float uniform 4D vector.
507 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
508 const GLchar* theName,
509 const OpenGl_Vec4& theValue);
510
511 //! Specifies the value of the float uniform 4D vector.
512 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
513 GLint theLocation,
514 const OpenGl_Vec4& theValue);
515
516public:
517
518 //! Specifies the value of the float uniform 4x4 matrix.
519 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
520 const GLchar* theName,
25ef750e 521 const OpenGl_Mat4& theValue,
522 GLboolean theTranspose = GL_FALSE);
523
524 //! Specifies the value of the float uniform 4x4 matrix.
525 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
526 GLint theLocation,
527 const OpenGl_Mat4& theValue,
528 GLboolean theTranspose = GL_FALSE);
529
530 //! Specifies the value of the float uniform 4x4 matrix.
531 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
532 const GLchar* theName,
30f0ad28 533 const OpenGl_Matrix& theValue,
534 GLboolean theTranspose = GL_FALSE);
535
536 //! Specifies the value of the float uniform 4x4 matrix.
537 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
538 GLint theLocation,
539 const OpenGl_Matrix& theValue,
540 GLboolean theTranspose = GL_FALSE);
541
4fe9ad57 542 //! Specifies the value of the float uniform array
543 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
544 GLint theLocation,
545 GLuint theCount,
546 const Standard_ShortReal* theData);
547
548 //! Specifies the value of the float2 uniform array
549 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
550 GLint theLocation,
551 GLuint theCount,
552 const OpenGl_Vec2* theData);
553
554 //! Specifies the value of the float3 uniform array
555 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
556 GLint theLocation,
557 GLuint theCount,
558 const OpenGl_Vec3* theData);
559
560 //! Specifies the value of the float4 uniform array
561 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
562 GLint theLocation,
563 GLuint theCount,
564 const OpenGl_Vec4* theData);
565
566 //! Specifies the value of the integer uniform array
567 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
568 GLint theLocation,
569 GLuint theCount,
570 const Standard_Integer* theData);
571
572 //! Specifies the value of the int2 uniform array
573 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
574 GLint theLocation,
575 GLuint theCount,
576 const OpenGl_Vec2i* theData);
577
578 //! Specifies the value of the int3 uniform array
579 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
580 GLint theLocation,
581 GLuint theCount,
582 const OpenGl_Vec3i* theData);
583
584 //! Specifies the value of the int4 uniform array
585 Standard_EXPORT Standard_Boolean SetUniform (const Handle(OpenGl_Context)& theCtx,
586 GLint theLocation,
587 GLuint theCount,
588 const OpenGl_Vec4i* theData);
589
30f0ad28 590public:
591
592 //! Specifies the value of the sampler uniform variable.
593 Standard_EXPORT Standard_Boolean SetSampler (const Handle(OpenGl_Context)& theCtx,
594 const GLchar* theName,
cc8cbabe 595 const Graphic3d_TextureUnit theTextureUnit);
30f0ad28 596
597 //! Specifies the value of the sampler uniform variable.
598 Standard_EXPORT Standard_Boolean SetSampler (const Handle(OpenGl_Context)& theCtx,
599 GLint theLocation,
cc8cbabe 600 const Graphic3d_TextureUnit theTextureUnit);
30f0ad28 601
602protected:
603
392ac980 604 //! Increments counter of users.
605 //! Used by OpenGl_ShaderManager.
05dd08ce 606 //! @return true when resource has been restored from delayed release queue
607 bool Share()
392ac980 608 {
05dd08ce 609 return ++myShareCount == 1;
392ac980 610 }
611
612 //! Decrements counter of users.
613 //! Used by OpenGl_ShaderManager.
614 //! @return true when there are no more users of this program has been left
615 bool UnShare()
616 {
617 return --myShareCount == 0;
618 }
619
2bda8346 620 //! Links the program object.
621 Standard_EXPORT Standard_Boolean link (const Handle(OpenGl_Context)& theCtx);
622
392ac980 623protected:
624
625 GLuint myProgramID; //!< Handle of OpenGL shader program
626 OpenGl_ShaderList myShaderObjects; //!< List of attached shader objects
627 Handle(Graphic3d_ShaderProgram) myProxy; //!< Proxy shader program (from application layer)
628 Standard_Integer myShareCount; //!< program users count, initialized with 1 (already shared by one user)
daf73ab7 629 Standard_Integer myNbLightsMax; //!< length of array of light sources (THE_MAX_LIGHTS)
630 Standard_Integer myNbClipPlanesMax; //!< length of array of clipping planes (THE_MAX_CLIP_PLANES)
b17e5bae 631 Standard_Integer myNbFragOutputs; //!< length of array of Fragment Shader outputs (THE_NB_FRAG_OUTPUTS)
c40eb6b9 632 Standard_Boolean myHasAlphaTest; //!< flag indicating that Fragment Shader should perform alpha-test
b17e5bae 633 Standard_Boolean myHasWeightOitOutput; //!< flag indicating that Fragment Shader includes weighted OIT coverage
8e0a2b19 634 Standard_Boolean myHasTessShader; //!< flag indicating that program defines tessellation stage
30f0ad28 635
636protected:
637
8613985b 638 Standard_Size myCurrentState[OpenGl_UniformStateType_NB]; //!< defines last modification for variables of each state type
30f0ad28 639
640 //! Stores locations of OCCT state uniform variables.
2a332745 641 OpenGl_ShaderUniformLocation myStateLocations[OpenGl_OCCT_NUMBER_OF_STATE_VARIABLES];
30f0ad28 642
30f0ad28 643};
644
645template<class T>
646struct OpenGl_VariableSetter : public OpenGl_SetterInterface
647{
648 virtual void Set (const Handle(OpenGl_Context)& theCtx,
649 const Handle(Graphic3d_ShaderVariable)& theVariable,
650 OpenGl_ShaderProgram* theProgram)
651 {
652 theProgram->SetUniform (theCtx,
653 theVariable->Name().ToCString(),
654 theVariable->Value()->As<T>());
655 }
656};
657
658namespace OpenGl_HashMapInitializer
659{
660 template<class K, class V>
661 struct MapListOfType
662 {
663 NCollection_DataMap<K, V> myDictionary;
664
665 MapListOfType (K theKey, V theValue)
666 {
667 myDictionary.Bind (theKey, theValue);
668 }
669
670 MapListOfType& operator() (K theKey, V theValue)
671 {
672 myDictionary.Bind (theKey, theValue);
673 return *this;
674 }
675
676 operator const NCollection_DataMap<K, V>& () const
677 {
678 return myDictionary;
679 }
680 };
681
682 template<class K, class V>
683 MapListOfType<K, V> CreateListOf (K theKey, V theValue)
684 {
685 return MapListOfType<K, V> (theKey, theValue);
686 }
687}
688
689#endif // _OpenGl_ShaderProgram_Header