0030497: [REGRESSION] Mesh - wrong Poly_Polygon3D within local selection of located...
[occt.git] / src / OpenGl / OpenGl_ShaderManager.hxx
CommitLineData
30f0ad28 1// Created on: 2013-09-26
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_ShaderManager_HeaderFile
17#define _OpenGl_ShaderManager_HeaderFile
18
494782f6 19#include <Graphic3d_ShaderProgram.hxx>
f978241f 20#include <Graphic3d_StereoMode.hxx>
30f0ad28 21
22#include <NCollection_DataMap.hxx>
23#include <NCollection_Sequence.hxx>
24
256f9ac0 25#include <OpenGl_SetOfShaderPrograms.hxx>
30f0ad28 26#include <OpenGl_ShaderStates.hxx>
8625ef7e 27#include <OpenGl_AspectFace.hxx>
28#include <OpenGl_AspectLine.hxx>
29#include <OpenGl_AspectText.hxx>
30#include <OpenGl_AspectMarker.hxx>
8613985b 31#include <OpenGl_MaterialState.hxx>
8625ef7e 32#include <OpenGl_Texture.hxx>
c357e426 33
e6804ff7 34class OpenGl_View;
98b15dbf 35class OpenGl_VertexBuffer;
e6804ff7 36
30f0ad28 37//! List of shader programs.
38typedef NCollection_Sequence<Handle(OpenGl_ShaderProgram)> OpenGl_ShaderProgramList;
39
30f0ad28 40//! This class is responsible for managing shader programs.
41class OpenGl_ShaderManager : public Standard_Transient
42{
8613985b 43 DEFINE_STANDARD_RTTIEXT(OpenGl_ShaderManager, Standard_Transient)
30f0ad28 44 friend class OpenGl_ShaderProgram;
30f0ad28 45public:
46
47 //! Creates new empty shader manager.
48 Standard_EXPORT OpenGl_ShaderManager (OpenGl_Context* theContext);
49
50 //! Releases resources of shader manager.
51 Standard_EXPORT virtual ~OpenGl_ShaderManager();
52
05e2200b 53 //! Release all resources.
54 Standard_EXPORT void clear();
55
7c3ef2f7 56 //! Return local camera transformation.
57 const gp_XYZ& LocalOrigin() const { return myLocalOrigin; }
58
59 //! Setup local camera transformation for compensating float precision issues.
60 void SetLocalOrigin (const gp_XYZ& theOrigin)
61 {
62 myLocalOrigin = theOrigin;
63 myHasLocalOrigin = !theOrigin.IsEqual (gp_XYZ(0.0, 0.0, 0.0), gp::Resolution());
64 }
65
392ac980 66 //! Creates new shader program or re-use shared instance.
67 //! @param theProxy [IN] program definition
68 //! @param theShareKey [OUT] sharing key
69 //! @param theProgram [OUT] OpenGL program
8625ef7e 70 //! @return true on success
71 Standard_EXPORT Standard_Boolean Create (const Handle(Graphic3d_ShaderProgram)& theProxy,
72 TCollection_AsciiString& theShareKey,
73 Handle(OpenGl_ShaderProgram)& theProgram);
30f0ad28 74
75 //! Unregisters specified shader program.
392ac980 76 Standard_EXPORT void Unregister (TCollection_AsciiString& theShareKey,
77 Handle(OpenGl_ShaderProgram)& theProgram);
30f0ad28 78
79 //! Returns list of registered shader programs.
80 Standard_EXPORT const OpenGl_ShaderProgramList& ShaderPrograms() const;
81
82 //! Returns true if no program objects are registered in the manager.
83 Standard_EXPORT Standard_Boolean IsEmpty() const;
84
8625ef7e 85 //! Bind program for filled primitives rendering
cc8cbabe 86 Standard_Boolean BindFaceProgram (const Handle(OpenGl_TextureSet)& theTextures,
2a332745 87 Graphic3d_TypeOfShadingModel theShadingModel,
88 Graphic3d_AlphaMode theAlphaMode,
89 Standard_Boolean theHasVertColor,
90 Standard_Boolean theEnableEnvMap,
91 const Handle(OpenGl_ShaderProgram)& theCustomProgram)
92 {
93 return BindFaceProgram (theTextures, theShadingModel, theAlphaMode, Aspect_IS_SOLID, theHasVertColor, theEnableEnvMap, false, theCustomProgram);
94 }
95
96 //! Bind program for filled primitives rendering
97 Standard_Boolean BindFaceProgram (const Handle(OpenGl_TextureSet)& theTextures,
98 Graphic3d_TypeOfShadingModel theShadingModel,
99 Graphic3d_AlphaMode theAlphaMode,
100 Aspect_InteriorStyle theInteriorStyle,
101 Standard_Boolean theHasVertColor,
102 Standard_Boolean theEnableEnvMap,
103 Standard_Boolean theEnableMeshEdges,
299e0ab9 104 const Handle(OpenGl_ShaderProgram)& theCustomProgram)
8625ef7e 105 {
106 if (!theCustomProgram.IsNull()
107 || myContext->caps->ffpEnable)
108 {
299e0ab9 109 return bindProgramWithState (theCustomProgram);
8625ef7e 110 }
111
dc89236f 112 const Graphic3d_TypeOfShadingModel aShadeModelOnFace = theShadingModel != Graphic3d_TOSM_UNLIT
113 && (theTextures.IsNull() || theTextures->IsModulate())
114 ? theShadingModel
115 : Graphic3d_TOSM_UNLIT;
2a332745 116 const Standard_Integer aBits = getProgramBits (theTextures, theAlphaMode, theInteriorStyle, theHasVertColor, theEnableEnvMap, theEnableMeshEdges);
dc89236f 117 Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (aShadeModelOnFace, aBits);
299e0ab9 118 return bindProgramWithState (aProgram);
8625ef7e 119 }
30f0ad28 120
8625ef7e 121 //! Bind program for line rendering
cc8cbabe 122 Standard_Boolean BindLineProgram (const Handle(OpenGl_TextureSet)& theTextures,
dc89236f 123 const Aspect_TypeOfLine theLineType,
124 const Graphic3d_TypeOfShadingModel theShadingModel,
c40eb6b9 125 const Graphic3d_AlphaMode theAlphaMode,
299e0ab9 126 const Standard_Boolean theHasVertColor,
127 const Handle(OpenGl_ShaderProgram)& theCustomProgram)
8625ef7e 128 {
129 if (!theCustomProgram.IsNull()
130 || myContext->caps->ffpEnable)
131 {
299e0ab9 132 return bindProgramWithState (theCustomProgram);
8625ef7e 133 }
134
2a332745 135 Standard_Integer aBits = getProgramBits (theTextures, theAlphaMode, Aspect_IS_SOLID, theHasVertColor, false, false);
dc89236f 136 if (theLineType != Aspect_TOL_SOLID)
ac116c22 137 {
138 aBits |= OpenGl_PO_StippleLine;
139 }
140
dc89236f 141 Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theShadingModel, aBits);
299e0ab9 142 return bindProgramWithState (aProgram);
8625ef7e 143 }
30f0ad28 144
8625ef7e 145 //! Bind program for point rendering
cc8cbabe 146 Standard_Boolean BindMarkerProgram (const Handle(OpenGl_TextureSet)& theTextures,
dc89236f 147 const Graphic3d_TypeOfShadingModel theShadingModel,
c40eb6b9 148 const Graphic3d_AlphaMode theAlphaMode,
299e0ab9 149 const Standard_Boolean theHasVertColor,
150 const Handle(OpenGl_ShaderProgram)& theCustomProgram)
8625ef7e 151 {
152 if (!theCustomProgram.IsNull()
153 || myContext->caps->ffpEnable)
154 {
299e0ab9 155 return bindProgramWithState (theCustomProgram);
8625ef7e 156 }
157
2a332745 158 const Standard_Integer aBits = getProgramBits (theTextures, theAlphaMode, Aspect_IS_SOLID, theHasVertColor, false, false) | OpenGl_PO_Point;
dc89236f 159 Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theShadingModel, aBits);
299e0ab9 160 return bindProgramWithState (aProgram);
8625ef7e 161 }
162
163 //! Bind program for rendering alpha-textured font.
299e0ab9 164 Standard_Boolean BindFontProgram (const Handle(OpenGl_ShaderProgram)& theCustomProgram)
8625ef7e 165 {
166 if (!theCustomProgram.IsNull()
167 || myContext->caps->ffpEnable)
168 {
299e0ab9 169 return bindProgramWithState (theCustomProgram);
8625ef7e 170 }
171
172 if (myFontProgram.IsNull())
173 {
174 prepareStdProgramFont();
175 }
299e0ab9 176
177 return bindProgramWithState (myFontProgram);
8625ef7e 178 }
30f0ad28 179
b86bb3df 180 //! Bind program for FBO blit operation.
181 Standard_Boolean BindFboBlitProgram()
182 {
183 if (myBlitProgram.IsNull())
184 {
185 prepareStdProgramFboBlit();
186 }
187 return !myBlitProgram.IsNull()
188 && myContext->BindProgram (myBlitProgram);
189 }
190
a1073ae2 191 //! Bind program for blended order-independent transparency buffers compositing.
192 Standard_Boolean BindOitCompositingProgram (const Standard_Boolean theIsMSAAEnabled)
193 {
194 const Standard_Integer aProgramIdx = theIsMSAAEnabled ? 1 : 0;
195 if (myOitCompositingProgram[aProgramIdx].IsNull())
196 {
197 prepareStdProgramOitCompositing (theIsMSAAEnabled);
198 }
199
200 const Handle(OpenGl_ShaderProgram)& aProgram = myOitCompositingProgram [aProgramIdx];
201 return !aProgram.IsNull() && myContext->BindProgram (aProgram);
202 }
203
f978241f 204 //! Bind program for rendering stereoscopic image.
205 Standard_Boolean BindStereoProgram (const Graphic3d_StereoMode theStereoMode)
38a0206f 206 {
f978241f 207 if (theStereoMode < 0 || theStereoMode >= Graphic3d_StereoMode_NB)
38a0206f 208 {
f978241f 209 return Standard_False;
38a0206f 210 }
f978241f 211
212 if (myStereoPrograms[theStereoMode].IsNull())
213 {
214 prepareStdProgramStereo (myStereoPrograms[theStereoMode], theStereoMode);
215 }
216 const Handle(OpenGl_ShaderProgram)& aProgram = myStereoPrograms[theStereoMode];
217 return !aProgram.IsNull()
218 && myContext->BindProgram (aProgram);
38a0206f 219 }
220
98b15dbf 221 //! Bind program for rendering bounding box.
222 Standard_Boolean BindBoundBoxProgram()
223 {
224 if (myBoundBoxProgram.IsNull())
225 {
226 prepareStdProgramBoundBox();
227 }
228 return bindProgramWithState (myBoundBoxProgram);
229 }
230
231 //! Returns bounding box vertex buffer.
232 const Handle(OpenGl_VertexBuffer)& BoundBoxVertBuffer() const { return myBoundBoxVertBuffer; }
233
30f0ad28 234public:
235
236 //! Returns current state of OCCT light sources.
8613985b 237 const OpenGl_LightSourceState& LightSourceState() const { return myLightSourceState; }
30f0ad28 238
239 //! Updates state of OCCT light sources.
992ed6b3 240 Standard_EXPORT void UpdateLightSourceStateTo (const Handle(Graphic3d_LightSet)& theLights);
30f0ad28 241
7c3ef2f7 242 //! Invalidate state of OCCT light sources.
243 Standard_EXPORT void UpdateLightSourceState();
244
30f0ad28 245 //! Pushes current state of OCCT light sources to specified program.
246 Standard_EXPORT void PushLightSourceState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
247
248public:
249
250 //! Returns current state of OCCT projection transform.
8613985b 251 const OpenGl_ProjectionState& ProjectionState() const { return myProjectionState; }
30f0ad28 252
253 //! Updates state of OCCT projection transform.
c827ea3a 254 Standard_EXPORT void UpdateProjectionStateTo (const OpenGl_Mat4& theProjectionMatrix);
30f0ad28 255
30f0ad28 256 //! Pushes current state of OCCT projection transform to specified program.
257 Standard_EXPORT void PushProjectionState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
258
259public:
260
261 //! Returns current state of OCCT model-world transform.
8613985b 262 const OpenGl_ModelWorldState& ModelWorldState() const { return myModelWorldState; }
30f0ad28 263
264 //! Updates state of OCCT model-world transform.
c827ea3a 265 Standard_EXPORT void UpdateModelWorldStateTo (const OpenGl_Mat4& theModelWorldMatrix);
30f0ad28 266
30f0ad28 267 //! Pushes current state of OCCT model-world transform to specified program.
268 Standard_EXPORT void PushModelWorldState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
269
270public:
271
272 //! Returns current state of OCCT world-view transform.
8613985b 273 const OpenGl_WorldViewState& WorldViewState() const { return myWorldViewState; }
30f0ad28 274
275 //! Updates state of OCCT world-view transform.
c827ea3a 276 Standard_EXPORT void UpdateWorldViewStateTo (const OpenGl_Mat4& theWorldViewMatrix);
30f0ad28 277
30f0ad28 278 //! Pushes current state of OCCT world-view transform to specified program.
279 Standard_EXPORT void PushWorldViewState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
280
281public:
282
283 //! Updates state of OCCT clipping planes.
284 Standard_EXPORT void UpdateClippingState();
285
286 //! Reverts state of OCCT clipping planes.
287 Standard_EXPORT void RevertClippingState();
288
289 //! Pushes current state of OCCT clipping planes to specified program.
290 Standard_EXPORT void PushClippingState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
291
292public:
293
8613985b 294 //! Returns current state of material.
295 const OpenGl_MaterialState& MaterialState() const { return myMaterialState; }
296
297 //! Updates state of material.
298 void UpdateMaterialStateTo (const OpenGl_Material& theFrontMat,
299 const OpenGl_Material& theBackMat,
c40eb6b9 300 const float theAlphaCutoff,
8613985b 301 const bool theToDistinguish,
302 const bool theToMapTexture)
303 {
c40eb6b9 304 myMaterialState.Set (theFrontMat, theBackMat, theAlphaCutoff, theToDistinguish, theToMapTexture);
8613985b 305 myMaterialState.Update();
306 }
307
308 //! Updates state of material.
309 void UpdateMaterialState()
310 {
311 myMaterialState.Update();
312 }
313
314 //! Pushes current state of material to specified program.
315 void PushMaterialState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
316
317public:
318
2a332745 319 //! Setup interior style line edges variables.
320 Standard_EXPORT void PushInteriorState (const Handle(OpenGl_ShaderProgram)& theProgram,
321 const Handle(Graphic3d_AspectFillArea3d)& theAspect) const;
322
323public:
324
726b5d9e 325 //! Returns state of OIT uniforms.
326 const OpenGl_OitState& OitState() const { return myOitState; }
327
a1073ae2 328 //! Set the state of OIT rendering pass.
329 //! @param theToEnableOitWrite [in] flag indicating whether the special output should be written for OIT algorithm.
330 //! @param theDepthFactor [in] the scalar factor of depth influence to the fragment's coverage.
331 void SetOitState (const bool theToEnableOitWrite, const float theDepthFactor)
332 {
333 myOitState.Set (theToEnableOitWrite, theDepthFactor);
334 myOitState.Update();
335 }
336
337 //! Pushes state of OIT uniforms to the specified program.
338 Standard_EXPORT void PushOitState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
339
340public:
341
30f0ad28 342 //! Pushes current state of OCCT graphics parameters to specified program.
343 Standard_EXPORT void PushState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
344
392ac980 345public:
346
347 //! Overwrites context
348 void SetContext (OpenGl_Context* theCtx)
349 {
350 myContext = theCtx;
351 }
352
c357e426 353 //! Returns true when provided context is the same as used one by shader manager.
354 bool IsSameContext (OpenGl_Context* theCtx) const
355 {
356 return myContext == theCtx;
357 }
358
db5d29de 359 //! Choose Shading Model for filled primitives.
360 //! Fallbacks to FACET model if there are no normal attributes.
361 Graphic3d_TypeOfShadingModel ChooseFaceShadingModel (Graphic3d_TypeOfShadingModel theCustomModel,
362 bool theHasNodalNormals) const
dc89236f 363 {
364 if (!myContext->ColorMask())
365 {
366 return Graphic3d_TOSM_UNLIT;
367 }
368 Graphic3d_TypeOfShadingModel aModel = theCustomModel != Graphic3d_TOSM_DEFAULT ? theCustomModel : myShadingModel;
369 switch (aModel)
370 {
371 case Graphic3d_TOSM_DEFAULT:
372 case Graphic3d_TOSM_UNLIT:
373 case Graphic3d_TOSM_FACET:
db5d29de 374 return aModel;
dc89236f 375 case Graphic3d_TOSM_VERTEX:
376 case Graphic3d_TOSM_FRAGMENT:
db5d29de 377 return theHasNodalNormals ? aModel : Graphic3d_TOSM_FACET;
dc89236f 378 }
db5d29de 379 return Graphic3d_TOSM_UNLIT;
380 }
381
382 //! Choose Shading Model for line primitives.
383 //! Fallbacks to UNLIT model if there are no normal attributes.
384 Graphic3d_TypeOfShadingModel ChooseLineShadingModel (Graphic3d_TypeOfShadingModel theCustomModel,
385 bool theHasNodalNormals) const
386 {
387 if (!myContext->ColorMask())
388 {
389 return Graphic3d_TOSM_UNLIT;
390 }
391 Graphic3d_TypeOfShadingModel aModel = theCustomModel != Graphic3d_TOSM_DEFAULT ? theCustomModel : myShadingModel;
392 switch (aModel)
393 {
394 case Graphic3d_TOSM_DEFAULT:
395 case Graphic3d_TOSM_UNLIT:
396 case Graphic3d_TOSM_FACET:
397 return Graphic3d_TOSM_UNLIT;
398 case Graphic3d_TOSM_VERTEX:
399 case Graphic3d_TOSM_FRAGMENT:
400 return theHasNodalNormals ? aModel : Graphic3d_TOSM_UNLIT;
401 }
402 return Graphic3d_TOSM_UNLIT;
403 }
404
405 //! Choose Shading Model for Marker primitives.
406 Graphic3d_TypeOfShadingModel ChooseMarkerShadingModel (Graphic3d_TypeOfShadingModel theCustomModel,
407 bool theHasNodalNormals) const
408 {
409 return ChooseLineShadingModel (theCustomModel, theHasNodalNormals);
dc89236f 410 }
411
412 //! Returns default Shading Model.
413 Graphic3d_TypeOfShadingModel ShadingModel() const { return myShadingModel; }
414
8625ef7e 415 //! Sets shading model.
c357e426 416 Standard_EXPORT void SetShadingModel (const Graphic3d_TypeOfShadingModel theModel);
8625ef7e 417
e6804ff7 418 //! Sets last view manger used with.
419 //! Helps to handle matrix states in multi-view configurations.
420 void SetLastView (const OpenGl_View* theLastView)
421 {
422 myLastView = theLastView;
423 }
424
425 //! Returns true when provided view is the same as cached one.
426 bool IsSameView (const OpenGl_View* theView) const
427 {
428 return myLastView == theView;
429 }
430
30f0ad28 431protected:
432
2a332745 433 //! Define clipping planes program bits.
434 Standard_Integer getClipPlaneBits() const
435 {
436 const Standard_Integer aNbPlanes = myContext->Clipping().NbClippingOrCappingOn();
437 if (aNbPlanes <= 0)
438 {
439 return 0;
440 }
441
442 Standard_Integer aBits = 0;
443 if (myContext->Clipping().HasClippingChains())
444 {
445 aBits |= OpenGl_PO_ClipChains;
446 }
447
448 if (aNbPlanes == 1)
449 {
450 aBits |= OpenGl_PO_ClipPlanes1;
451 }
452 else if (aNbPlanes == 2)
453 {
454 aBits |= OpenGl_PO_ClipPlanes2;
455 }
456 else
457 {
458 aBits |= OpenGl_PO_ClipPlanesN;
459 }
460 return aBits;
461 }
462
8625ef7e 463 //! Define program bits.
cc8cbabe 464 Standard_Integer getProgramBits (const Handle(OpenGl_TextureSet)& theTextures,
c40eb6b9 465 Graphic3d_AlphaMode theAlphaMode,
2a332745 466 Aspect_InteriorStyle theInteriorStyle,
c40eb6b9 467 Standard_Boolean theHasVertColor,
2a332745 468 Standard_Boolean theEnableEnvMap,
469 Standard_Boolean theEnableMeshEdges) const
8625ef7e 470 {
471 Standard_Integer aBits = 0;
c40eb6b9 472 if (theAlphaMode == Graphic3d_AlphaMode_Mask)
473 {
474 aBits |= OpenGl_PO_AlphaTest;
475 }
1a7ece8f 476
2a332745 477 aBits |= getClipPlaneBits();
478 if (theEnableMeshEdges
479 && myContext->hasGeometryStage != OpenGl_FeatureNotAvailable)
8625ef7e 480 {
2a332745 481 aBits |= OpenGl_PO_MeshEdges;
482 if (theInteriorStyle == Aspect_IS_HOLLOW)
1a7ece8f 483 {
2a332745 484 aBits |= OpenGl_PO_AlphaTest;
1a7ece8f 485 }
8625ef7e 486 }
1a7ece8f 487
83da37b1 488 if (theEnableEnvMap)
e135a155 489 {
490 // Environment map overwrites material texture
491 aBits |= OpenGl_PO_TextureEnv;
492 }
cc8cbabe 493 else if (!theTextures.IsNull()
494 && !theTextures->IsEmpty()
495 && !theTextures->First().IsNull())
8625ef7e 496 {
cc8cbabe 497 aBits |= theTextures->First()->IsAlpha() ? OpenGl_PO_TextureA : OpenGl_PO_TextureRGB;
8625ef7e 498 }
2a332745 499 if (theHasVertColor
500 && theInteriorStyle != Aspect_IS_HIDDENLINE)
8625ef7e 501 {
502 aBits |= OpenGl_PO_VertColor;
503 }
a1073ae2 504
505 if (myOitState.ToEnableWrite())
506 {
507 aBits |= OpenGl_PO_WriteOit;
508 }
8625ef7e 509 return aBits;
510 }
511
512 //! Prepare standard GLSL program.
dc89236f 513 Handle(OpenGl_ShaderProgram)& getStdProgram (Graphic3d_TypeOfShadingModel theShadingModel,
514 Standard_Integer theBits)
8625ef7e 515 {
dc89236f 516 if (theShadingModel == Graphic3d_TOSM_UNLIT
517 || (theBits & OpenGl_PO_TextureEnv) != 0)
8625ef7e 518 {
dc89236f 519 // If environment map is enabled lighting calculations are
520 // not needed (in accordance with default OCCT behavior)
521 Handle(OpenGl_ShaderProgram)& aProgram = myUnlitPrograms->ChangeValue (Graphic3d_TOSM_UNLIT, theBits);
8625ef7e 522 if (aProgram.IsNull())
523 {
dc89236f 524 prepareStdProgramUnlit (aProgram, theBits);
8625ef7e 525 }
526 return aProgram;
527 }
528
dc89236f 529 Handle(OpenGl_ShaderProgram)& aProgram = myLightPrograms->ChangeValue (theShadingModel, theBits);
8625ef7e 530 if (aProgram.IsNull())
531 {
dc89236f 532 prepareStdProgramLight (aProgram, theShadingModel, theBits);
8625ef7e 533 }
534 return aProgram;
535 }
536
299e0ab9 537 //! Prepare standard GLSL program for accessing point sprite alpha.
fd59283a 538 Standard_EXPORT TCollection_AsciiString pointSpriteAlphaSrc (const Standard_Integer theBits);
299e0ab9 539
540 //! Prepare standard GLSL program for computing point sprite shading.
541 Standard_EXPORT TCollection_AsciiString pointSpriteShadingSrc (const TCollection_AsciiString theBaseColorSrc, const Standard_Integer theBits);
542
8625ef7e 543 //! Prepare standard GLSL program for textured font.
544 Standard_EXPORT Standard_Boolean prepareStdProgramFont();
545
b86bb3df 546 //! Prepare standard GLSL program for FBO blit operation.
547 Standard_EXPORT Standard_Boolean prepareStdProgramFboBlit();
548
a1073ae2 549 //! Prepare standard GLSL programs for OIT compositing operation.
550 Standard_EXPORT Standard_Boolean prepareStdProgramOitCompositing (const Standard_Boolean theMsaa);
551
8625ef7e 552 //! Prepare standard GLSL program without lighting.
dc89236f 553 Standard_EXPORT Standard_Boolean prepareStdProgramUnlit (Handle(OpenGl_ShaderProgram)& theProgram,
554 const Standard_Integer theBits);
8625ef7e 555
556 //! Prepare standard GLSL program with lighting.
557 Standard_Boolean prepareStdProgramLight (Handle(OpenGl_ShaderProgram)& theProgram,
dc89236f 558 Graphic3d_TypeOfShadingModel theShadingModel,
559 Standard_Integer theBits)
8625ef7e 560 {
dc89236f 561 switch (theShadingModel)
8c3237d4 562 {
dc89236f 563 case Graphic3d_TOSM_UNLIT: return prepareStdProgramUnlit (theProgram, theBits);
8c3237d4 564 case Graphic3d_TOSM_FACET: return prepareStdProgramPhong (theProgram, theBits, true);
565 case Graphic3d_TOSM_VERTEX: return prepareStdProgramGouraud(theProgram, theBits);
dc89236f 566 case Graphic3d_TOSM_DEFAULT:
8c3237d4 567 case Graphic3d_TOSM_FRAGMENT: return prepareStdProgramPhong (theProgram, theBits, false);
568 }
569 return false;
8625ef7e 570 }
571
572 //! Prepare standard GLSL program with per-vertex lighting.
573 Standard_EXPORT Standard_Boolean prepareStdProgramGouraud (Handle(OpenGl_ShaderProgram)& theProgram,
574 const Standard_Integer theBits);
575
576 //! Prepare standard GLSL program with per-pixel lighting.
8c3237d4 577 //! @param theIsFlatNormal when TRUE, the Vertex normals will be ignored and Face normal will be computed instead
8625ef7e 578 Standard_EXPORT Standard_Boolean prepareStdProgramPhong (Handle(OpenGl_ShaderProgram)& theProgram,
8c3237d4 579 const Standard_Integer theBits,
580 const Standard_Boolean theIsFlatNormal = false);
8625ef7e 581
256f9ac0 582 //! Define computeLighting GLSL function depending on current lights configuration
daf73ab7 583 //! @param theNbLights [out] number of defined light sources
584 //! @param theHasVertColor [in] flag to use getVertColor() instead of Ambient and Diffuse components of active material
585 Standard_EXPORT TCollection_AsciiString stdComputeLighting (Standard_Integer& theNbLights,
586 Standard_Boolean theHasVertColor);
256f9ac0 587
8625ef7e 588 //! Bind specified program to current context and apply state.
299e0ab9 589 Standard_EXPORT Standard_Boolean bindProgramWithState (const Handle(OpenGl_ShaderProgram)& theProgram);
8625ef7e 590
256f9ac0 591 //! Set pointer myLightPrograms to active lighting programs set from myMapOfLightPrograms
592 Standard_EXPORT void switchLightPrograms();
593
f978241f 594 //! Prepare standard GLSL program for stereoscopic image.
595 Standard_EXPORT Standard_Boolean prepareStdProgramStereo (Handle(OpenGl_ShaderProgram)& theProgram,
596 const Graphic3d_StereoMode theStereoMode);
38a0206f 597
98b15dbf 598 //! Prepare standard GLSL program for bounding box.
599 Standard_EXPORT Standard_Boolean prepareStdProgramBoundBox();
600
2a332745 601 //! Prepare GLSL version header.
602 Standard_EXPORT Standard_Integer defaultGlslVersion (const Handle(Graphic3d_ShaderProgram)& theProgram,
d95f5ce1 603 const TCollection_AsciiString& theName,
2a332745 604 Standard_Integer theBits,
605 bool theUsesDerivates = false) const;
606
607 //! Prepare GLSL source for geometry shader according to parameters.
608 Standard_EXPORT TCollection_AsciiString prepareGeomMainSrc (OpenGl_ShaderObject::ShaderVariableList& theUnifoms,
609 OpenGl_ShaderObject::ShaderVariableList& theStageInOuts,
610 Standard_Integer theBits);
611
8625ef7e 612protected:
613
7c3ef2f7 614 //! Packed properties of light source
615 struct OpenGl_ShaderLightParameters
616 {
617 OpenGl_Vec4 Color;
618 OpenGl_Vec4 Position;
619 OpenGl_Vec4 Direction;
620 OpenGl_Vec4 Parameters;
621
622 //! Returns packed (serialized) representation of light source properties
623 const OpenGl_Vec4* Packed() const { return reinterpret_cast<const OpenGl_Vec4*> (this); }
624 static Standard_Integer NbOfVec4() { return 4; }
625 };
626
627 //! Packed light source type information
628 struct OpenGl_ShaderLightType
629 {
630 Standard_Integer Type;
631 Standard_Integer IsHeadlight;
632
633 //! Returns packed (serialized) representation of light source type
634 const OpenGl_Vec2i* Packed() const { return reinterpret_cast<const OpenGl_Vec2i*> (this); }
635 static Standard_Integer NbOfVec2i() { return 1; }
636 };
637
8613985b 638 //! Fake OpenGL program for tracking FFP state in the way consistent to programmable pipeline.
639 class OpenGl_ShaderProgramFFP : public OpenGl_ShaderProgram
640 {
641 DEFINE_STANDARD_RTTI_INLINE(OpenGl_ShaderProgramFFP, OpenGl_ShaderProgram)
642 friend class OpenGl_ShaderManager;
643 protected:
644 OpenGl_ShaderProgramFFP() {}
645 };
646
7c3ef2f7 647protected:
648
25c35042 649 //! Append clipping plane definition to temporary buffers.
650 void addClippingPlane (Standard_Integer& thePlaneId,
651 const Graphic3d_ClipPlane& thePlane,
652 const Graphic3d_Vec4d& theEq,
653 const Standard_Integer theChainFwd) const
654 {
655 myClipChainArray.SetValue (thePlaneId, theChainFwd);
656 OpenGl_Vec4& aPlaneEq = myClipPlaneArray.ChangeValue (thePlaneId);
657 aPlaneEq.x() = float(theEq.x());
658 aPlaneEq.y() = float(theEq.y());
659 aPlaneEq.z() = float(theEq.z());
660 aPlaneEq.w() = float(theEq.w());
661 if (myHasLocalOrigin)
662 {
663 const gp_XYZ aPos = thePlane.ToPlane().Position().Location().XYZ() - myLocalOrigin;
664 const Standard_Real aD = -(theEq.x() * aPos.X() + theEq.y() * aPos.Y() + theEq.z() * aPos.Z());
665 aPlaneEq.w() = float(aD);
666 }
667 ++thePlaneId;
668 }
669
670protected:
671
8613985b 672 Handle(OpenGl_ShaderProgramFFP) myFfpProgram;
673
c357e426 674 Graphic3d_TypeOfShadingModel myShadingModel; //!< lighting shading model
256f9ac0 675 OpenGl_ShaderProgramList myProgramList; //!< The list of shader programs
676 Handle(OpenGl_SetOfShaderPrograms) myLightPrograms; //!< pointer to active lighting programs matrix
dc89236f 677 Handle(OpenGl_SetOfShaderPrograms) myUnlitPrograms; //!< programs matrix without lighting
256f9ac0 678 Handle(OpenGl_ShaderProgram) myFontProgram; //!< standard program for textured text
b86bb3df 679 Handle(OpenGl_ShaderProgram) myBlitProgram; //!< standard program for FBO blit emulation
98b15dbf 680 Handle(OpenGl_ShaderProgram) myBoundBoxProgram; //!< standard program for bounding box
a1073ae2 681 Handle(OpenGl_ShaderProgram) myOitCompositingProgram[2]; //!< standard program for OIT compositing (default and MSAA).
dc89236f 682 OpenGl_MapOfShaderPrograms myMapOfLightPrograms; //!< map of lighting programs depending on lights configuration
8625ef7e 683
f978241f 684 Handle(OpenGl_ShaderProgram) myStereoPrograms[Graphic3d_StereoMode_NB]; //!< standard stereo programs
38a0206f 685
98b15dbf 686 Handle(OpenGl_VertexBuffer) myBoundBoxVertBuffer; //!< bounding box vertex buffer
687
256f9ac0 688 OpenGl_Context* myContext; //!< OpenGL context
8625ef7e 689
690protected:
691
256f9ac0 692 OpenGl_ProjectionState myProjectionState; //!< State of OCCT projection transformation
693 OpenGl_ModelWorldState myModelWorldState; //!< State of OCCT model-world transformation
694 OpenGl_WorldViewState myWorldViewState; //!< State of OCCT world-view transformation
8cf06aa2 695 OpenGl_ClippingState myClippingState; //!< State of OCCT clipping planes
256f9ac0 696 OpenGl_LightSourceState myLightSourceState; //!< State of OCCT light sources
8613985b 697 OpenGl_MaterialState myMaterialState; //!< State of Front and Back materials
a1073ae2 698 OpenGl_OitState myOitState; //!< State of OIT uniforms
8613985b 699
7c3ef2f7 700 gp_XYZ myLocalOrigin; //!< local camera transformation
701 Standard_Boolean myHasLocalOrigin; //!< flag indicating that local camera transformation has been set
702
daf73ab7 703 mutable NCollection_Array1<OpenGl_ShaderLightType> myLightTypeArray;
704 mutable NCollection_Array1<OpenGl_ShaderLightParameters> myLightParamsArray;
705 mutable NCollection_Array1<OpenGl_Vec4> myClipPlaneArray;
706 mutable NCollection_Array1<OpenGl_Vec4d> myClipPlaneArrayFfp;
25c35042 707 mutable NCollection_Array1<Standard_Integer> myClipChainArray;
30f0ad28 708
709private:
710
256f9ac0 711 const OpenGl_View* myLastView; //!< Pointer to the last view shader manager used with
8625ef7e 712
30f0ad28 713};
714
8613985b 715DEFINE_STANDARD_HANDLE(OpenGl_ShaderManager, Standard_Transient)
716
30f0ad28 717#endif // _OpenGl_ShaderManager_HeaderFile