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