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