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