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