0029076: Visualization - implement element shrinking Shader
[occt.git] / src / OpenGl / OpenGl_ShaderManager.hxx
1 // Created on: 2013-09-26
2 // Created by: Denis BOGOLEPOV
3 // Copyright (c) 2013-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #ifndef _OpenGl_ShaderManager_HeaderFile
17 #define _OpenGl_ShaderManager_HeaderFile
18
19 #include <Graphic3d_ShaderProgram.hxx>
20 #include <Graphic3d_StereoMode.hxx>
21
22 #include <NCollection_DataMap.hxx>
23 #include <NCollection_Sequence.hxx>
24
25 #include <OpenGl_SetOfShaderPrograms.hxx>
26 #include <OpenGl_ShaderStates.hxx>
27 #include <OpenGl_AspectFace.hxx>
28 #include <OpenGl_AspectLine.hxx>
29 #include <OpenGl_AspectText.hxx>
30 #include <OpenGl_AspectMarker.hxx>
31 #include <OpenGl_MaterialState.hxx>
32 #include <OpenGl_Texture.hxx>
33
34 class OpenGl_View;
35 class OpenGl_VertexBuffer;
36
37 //! List of shader programs.
38 typedef NCollection_Sequence<Handle(OpenGl_ShaderProgram)> OpenGl_ShaderProgramList;
39
40 //! This class is responsible for managing shader programs.
41 class OpenGl_ShaderManager : public Standard_Transient
42 {
43   DEFINE_STANDARD_RTTIEXT(OpenGl_ShaderManager, Standard_Transient)
44   friend class OpenGl_ShaderProgram;
45 public:
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
53   //! Release all resources.
54   Standard_EXPORT void clear();
55
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
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
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);
74
75   //! Unregisters specified shader program.
76   Standard_EXPORT void Unregister (TCollection_AsciiString&      theShareKey,
77                                    Handle(OpenGl_ShaderProgram)& theProgram);
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
85   //! Bind program for filled primitives rendering
86   Standard_Boolean BindFaceProgram (const Handle(OpenGl_TextureSet)& theTextures,
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,
104                                     const Handle(OpenGl_ShaderProgram)& theCustomProgram)
105   {
106     if (!theCustomProgram.IsNull()
107      || myContext->caps->ffpEnable)
108     {
109       return bindProgramWithState (theCustomProgram);
110     }
111
112     const Graphic3d_TypeOfShadingModel aShadeModelOnFace = theShadingModel != Graphic3d_TOSM_UNLIT
113                                                         && (theTextures.IsNull() || theTextures->IsModulate())
114                                                         ? theShadingModel
115                                                         : Graphic3d_TOSM_UNLIT;
116     const Standard_Integer aBits = getProgramBits (theTextures, theAlphaMode, theInteriorStyle, theHasVertColor, theEnableEnvMap, theEnableMeshEdges);
117     Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (aShadeModelOnFace, aBits);
118     return bindProgramWithState (aProgram);
119   }
120
121   //! Bind program for line rendering
122   Standard_Boolean BindLineProgram (const Handle(OpenGl_TextureSet)&    theTextures,
123                                     const Aspect_TypeOfLine             theLineType,
124                                     const Graphic3d_TypeOfShadingModel  theShadingModel,
125                                     const Graphic3d_AlphaMode           theAlphaMode,
126                                     const Standard_Boolean              theHasVertColor,
127                                     const Handle(OpenGl_ShaderProgram)& theCustomProgram)
128   {
129     if (!theCustomProgram.IsNull()
130      || myContext->caps->ffpEnable)
131     {
132       return bindProgramWithState (theCustomProgram);
133     }
134
135     Standard_Integer aBits = getProgramBits (theTextures, theAlphaMode, Aspect_IS_SOLID, theHasVertColor, false, false);
136     if (theLineType != Aspect_TOL_SOLID)
137     {
138       aBits |= OpenGl_PO_StippleLine;
139     }
140
141     Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theShadingModel, aBits);
142     return bindProgramWithState (aProgram);
143   }
144
145   //! Bind program for point rendering
146   Standard_Boolean BindMarkerProgram (const Handle(OpenGl_TextureSet)&    theTextures,
147                                       const Graphic3d_TypeOfShadingModel  theShadingModel,
148                                       const Graphic3d_AlphaMode           theAlphaMode,
149                                       const Standard_Boolean              theHasVertColor,
150                                       const Handle(OpenGl_ShaderProgram)& theCustomProgram)
151   {
152     if (!theCustomProgram.IsNull()
153      || myContext->caps->ffpEnable)
154     {
155       return bindProgramWithState (theCustomProgram);
156     }
157
158     const Standard_Integer        aBits    = getProgramBits (theTextures, theAlphaMode, Aspect_IS_SOLID, theHasVertColor, false, false) | OpenGl_PO_Point;
159     Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theShadingModel, aBits);
160     return bindProgramWithState (aProgram);
161   }
162
163   //! Bind program for rendering alpha-textured font.
164   Standard_Boolean BindFontProgram (const Handle(OpenGl_ShaderProgram)& theCustomProgram)
165   {
166     if (!theCustomProgram.IsNull()
167      || myContext->caps->ffpEnable)
168     {
169       return bindProgramWithState (theCustomProgram);
170     }
171
172     if (myFontProgram.IsNull())
173     {
174       prepareStdProgramFont();
175     }
176
177     return bindProgramWithState (myFontProgram);
178   }
179
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
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
204   //! Bind program for rendering stereoscopic image.
205   Standard_Boolean BindStereoProgram (const Graphic3d_StereoMode theStereoMode)
206   {
207     if (theStereoMode < 0 || theStereoMode >= Graphic3d_StereoMode_NB)
208     {
209       return Standard_False;
210     }
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);
219   }
220
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
234 public:
235
236   //! Returns current state of OCCT light sources.
237   const OpenGl_LightSourceState& LightSourceState() const { return myLightSourceState; }
238
239   //! Updates state of OCCT light sources.
240   Standard_EXPORT void UpdateLightSourceStateTo (const Handle(Graphic3d_LightSet)& theLights);
241
242   //! Invalidate state of OCCT light sources.
243   Standard_EXPORT void UpdateLightSourceState();
244
245   //! Pushes current state of OCCT light sources to specified program.
246   Standard_EXPORT void PushLightSourceState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
247
248 public:
249
250   //! Returns current state of OCCT projection transform.
251   const OpenGl_ProjectionState& ProjectionState() const { return myProjectionState; }
252
253   //! Updates state of OCCT projection transform.
254   Standard_EXPORT void UpdateProjectionStateTo (const OpenGl_Mat4& theProjectionMatrix);
255
256   //! Pushes current state of OCCT projection transform to specified program.
257   Standard_EXPORT void PushProjectionState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
258
259 public:
260
261   //! Returns current state of OCCT model-world transform.
262   const OpenGl_ModelWorldState& ModelWorldState() const { return myModelWorldState; }
263
264   //! Updates state of OCCT model-world transform.
265   Standard_EXPORT void UpdateModelWorldStateTo (const OpenGl_Mat4& theModelWorldMatrix);
266
267   //! Pushes current state of OCCT model-world transform to specified program.
268   Standard_EXPORT void PushModelWorldState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
269
270 public:
271
272   //! Returns current state of OCCT world-view transform.
273   const OpenGl_WorldViewState& WorldViewState() const { return myWorldViewState; }
274
275   //! Updates state of OCCT world-view transform.
276   Standard_EXPORT void UpdateWorldViewStateTo (const OpenGl_Mat4& theWorldViewMatrix);
277
278   //! Pushes current state of OCCT world-view transform to specified program.
279   Standard_EXPORT void PushWorldViewState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
280
281 public:
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
292 public:
293
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,
300                               const float theAlphaCutoff,
301                               const bool theToDistinguish,
302                               const bool theToMapTexture)
303   {
304     myMaterialState.Set (theFrontMat, theBackMat, theAlphaCutoff, theToDistinguish, theToMapTexture);
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
317 public:
318
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
323 public:
324
325   //! Returns state of OIT uniforms.
326   const OpenGl_OitState& OitState() const { return myOitState; }
327
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
340 public:
341
342   //! Pushes current state of OCCT graphics parameters to specified program.
343   Standard_EXPORT void PushState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
344
345 public:
346
347   //! Overwrites context
348   void SetContext (OpenGl_Context* theCtx)
349   {
350     myContext = theCtx;
351   }
352
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
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
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:
374         return aModel;
375       case Graphic3d_TOSM_VERTEX:
376       case Graphic3d_TOSM_FRAGMENT:
377         return theHasNodalNormals ? aModel : Graphic3d_TOSM_FACET;
378     }
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);
410   }
411
412   //! Returns default Shading Model.
413   Graphic3d_TypeOfShadingModel ShadingModel() const { return myShadingModel; }
414
415   //! Sets shading model.
416   Standard_EXPORT void SetShadingModel (const Graphic3d_TypeOfShadingModel theModel);
417
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
431 protected:
432
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
463   //! Define program bits.
464   Standard_Integer getProgramBits (const Handle(OpenGl_TextureSet)& theTextures,
465                                    Graphic3d_AlphaMode theAlphaMode,
466                                    Aspect_InteriorStyle theInteriorStyle,
467                                    Standard_Boolean theHasVertColor,
468                                    Standard_Boolean theEnableEnvMap,
469                                    Standard_Boolean theEnableMeshEdges) const
470   {
471     Standard_Integer aBits = 0;
472     if (theAlphaMode == Graphic3d_AlphaMode_Mask)
473     {
474       aBits |= OpenGl_PO_AlphaTest;
475     }
476
477     aBits |= getClipPlaneBits();
478     if (theEnableMeshEdges
479      && myContext->hasGeometryStage != OpenGl_FeatureNotAvailable)
480     {
481       aBits |= OpenGl_PO_MeshEdges;
482       if (theInteriorStyle == Aspect_IS_HOLLOW)
483       {
484         aBits |= OpenGl_PO_AlphaTest;
485       }
486     }
487
488     if (theEnableEnvMap)
489     {
490       // Environment map overwrites material texture
491       aBits |= OpenGl_PO_TextureEnv;
492     }
493     else if (!theTextures.IsNull()
494           && !theTextures->IsEmpty()
495           && !theTextures->First().IsNull())
496     {
497       aBits |= theTextures->First()->IsAlpha() ? OpenGl_PO_TextureA : OpenGl_PO_TextureRGB;
498     }
499     if (theHasVertColor
500      && theInteriorStyle != Aspect_IS_HIDDENLINE)
501     {
502       aBits |= OpenGl_PO_VertColor;
503     }
504
505     if (myOitState.ToEnableWrite())
506     {
507       aBits |= OpenGl_PO_WriteOit;
508     }
509     return aBits;
510   }
511
512   //! Prepare standard GLSL program.
513   Handle(OpenGl_ShaderProgram)& getStdProgram (Graphic3d_TypeOfShadingModel theShadingModel,
514                                                Standard_Integer theBits)
515   {
516     if (theShadingModel == Graphic3d_TOSM_UNLIT
517      || (theBits & OpenGl_PO_TextureEnv) != 0)
518     {
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);
522       if (aProgram.IsNull())
523       {
524         prepareStdProgramUnlit (aProgram, theBits);
525       }
526       return aProgram;
527     }
528
529     Handle(OpenGl_ShaderProgram)& aProgram = myLightPrograms->ChangeValue (theShadingModel, theBits);
530     if (aProgram.IsNull())
531     {
532       prepareStdProgramLight (aProgram, theShadingModel, theBits);
533     }
534     return aProgram;
535   }
536
537   //! Prepare standard GLSL program for accessing point sprite alpha.
538   Standard_EXPORT TCollection_AsciiString pointSpriteAlphaSrc (const Standard_Integer theBits);
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
543   //! Prepare standard GLSL program for textured font.
544   Standard_EXPORT Standard_Boolean prepareStdProgramFont();
545
546   //! Prepare standard GLSL program for FBO blit operation.
547   Standard_EXPORT Standard_Boolean prepareStdProgramFboBlit();
548
549   //! Prepare standard GLSL programs for OIT compositing operation.
550   Standard_EXPORT Standard_Boolean prepareStdProgramOitCompositing (const Standard_Boolean theMsaa);
551
552   //! Prepare standard GLSL program without lighting.
553   Standard_EXPORT Standard_Boolean prepareStdProgramUnlit (Handle(OpenGl_ShaderProgram)& theProgram,
554                                                            const Standard_Integer        theBits);
555
556   //! Prepare standard GLSL program with lighting.
557   Standard_Boolean prepareStdProgramLight (Handle(OpenGl_ShaderProgram)& theProgram,
558                                            Graphic3d_TypeOfShadingModel theShadingModel,
559                                            Standard_Integer theBits)
560   {
561     switch (theShadingModel)
562     {
563       case Graphic3d_TOSM_UNLIT:    return prepareStdProgramUnlit  (theProgram, theBits);
564       case Graphic3d_TOSM_FACET:    return prepareStdProgramPhong  (theProgram, theBits, true);
565       case Graphic3d_TOSM_VERTEX:   return prepareStdProgramGouraud(theProgram, theBits);
566       case Graphic3d_TOSM_DEFAULT:
567       case Graphic3d_TOSM_FRAGMENT: return prepareStdProgramPhong  (theProgram, theBits, false);
568     }
569     return false;
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.
577   //! @param theIsFlatNormal when TRUE, the Vertex normals will be ignored and Face normal will be computed instead
578   Standard_EXPORT Standard_Boolean prepareStdProgramPhong (Handle(OpenGl_ShaderProgram)& theProgram,
579                                                            const Standard_Integer        theBits,
580                                                            const Standard_Boolean        theIsFlatNormal = false);
581
582   //! Define computeLighting GLSL function depending on current lights configuration
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);
587
588   //! Bind specified program to current context and apply state.
589   Standard_EXPORT Standard_Boolean bindProgramWithState (const Handle(OpenGl_ShaderProgram)& theProgram);
590
591   //! Set pointer myLightPrograms to active lighting programs set from myMapOfLightPrograms
592   Standard_EXPORT void switchLightPrograms();
593
594   //! Prepare standard GLSL program for stereoscopic image.
595   Standard_EXPORT Standard_Boolean prepareStdProgramStereo (Handle(OpenGl_ShaderProgram)& theProgram,
596                                                             const Graphic3d_StereoMode    theStereoMode);
597
598   //! Prepare standard GLSL program for bounding box.
599   Standard_EXPORT Standard_Boolean prepareStdProgramBoundBox();
600
601   //! Prepare GLSL version header.
602   Standard_EXPORT Standard_Integer defaultGlslVersion (const Handle(Graphic3d_ShaderProgram)& theProgram,
603                                                        Standard_Integer theBits,
604                                                        bool theUsesDerivates = false) const;
605
606   //! Prepare GLSL source for geometry shader according to parameters.
607   Standard_EXPORT TCollection_AsciiString prepareGeomMainSrc (OpenGl_ShaderObject::ShaderVariableList& theUnifoms,
608                                                               OpenGl_ShaderObject::ShaderVariableList& theStageInOuts,
609                                                               Standard_Integer theBits);
610
611 protected:
612
613   //! Packed properties of light source
614   struct OpenGl_ShaderLightParameters
615   {
616     OpenGl_Vec4 Color;
617     OpenGl_Vec4 Position;
618     OpenGl_Vec4 Direction;
619     OpenGl_Vec4 Parameters;
620
621     //! Returns packed (serialized) representation of light source properties
622     const OpenGl_Vec4* Packed() const { return reinterpret_cast<const OpenGl_Vec4*> (this); }
623     static Standard_Integer NbOfVec4() { return 4; }
624   };
625
626   //! Packed light source type information
627   struct OpenGl_ShaderLightType
628   {
629     Standard_Integer Type;
630     Standard_Integer IsHeadlight;
631
632     //! Returns packed (serialized) representation of light source type
633     const OpenGl_Vec2i* Packed() const { return reinterpret_cast<const OpenGl_Vec2i*> (this); }
634     static Standard_Integer NbOfVec2i() { return 1; }
635   };
636
637   //! Fake OpenGL program for tracking FFP state in the way consistent to programmable pipeline.
638   class OpenGl_ShaderProgramFFP : public OpenGl_ShaderProgram
639   {
640     DEFINE_STANDARD_RTTI_INLINE(OpenGl_ShaderProgramFFP, OpenGl_ShaderProgram)
641     friend class OpenGl_ShaderManager;
642   protected:
643     OpenGl_ShaderProgramFFP() {}
644   };
645
646 protected:
647
648   //! Append clipping plane definition to temporary buffers.
649   void addClippingPlane (Standard_Integer& thePlaneId,
650                          const Graphic3d_ClipPlane& thePlane,
651                          const Graphic3d_Vec4d& theEq,
652                          const Standard_Integer theChainFwd) const
653   {
654     myClipChainArray.SetValue (thePlaneId, theChainFwd);
655     OpenGl_Vec4& aPlaneEq = myClipPlaneArray.ChangeValue (thePlaneId);
656     aPlaneEq.x() = float(theEq.x());
657     aPlaneEq.y() = float(theEq.y());
658     aPlaneEq.z() = float(theEq.z());
659     aPlaneEq.w() = float(theEq.w());
660     if (myHasLocalOrigin)
661     {
662       const gp_XYZ        aPos = thePlane.ToPlane().Position().Location().XYZ() - myLocalOrigin;
663       const Standard_Real aD   = -(theEq.x() * aPos.X() + theEq.y() * aPos.Y() + theEq.z() * aPos.Z());
664       aPlaneEq.w() = float(aD);
665     }
666     ++thePlaneId;
667   }
668
669 protected:
670
671   Handle(OpenGl_ShaderProgramFFP)    myFfpProgram;
672
673   Graphic3d_TypeOfShadingModel       myShadingModel;       //!< lighting shading model
674   OpenGl_ShaderProgramList           myProgramList;        //!< The list of shader programs
675   Handle(OpenGl_SetOfShaderPrograms) myLightPrograms;      //!< pointer to active lighting programs matrix
676   Handle(OpenGl_SetOfShaderPrograms) myUnlitPrograms;      //!< programs matrix without  lighting
677   Handle(OpenGl_ShaderProgram)       myFontProgram;        //!< standard program for textured text
678   Handle(OpenGl_ShaderProgram)       myBlitProgram;        //!< standard program for FBO blit emulation
679   Handle(OpenGl_ShaderProgram)       myBoundBoxProgram;    //!< standard program for bounding box
680   Handle(OpenGl_ShaderProgram)       myOitCompositingProgram[2]; //!< standard program for OIT compositing (default and MSAA).
681   OpenGl_MapOfShaderPrograms         myMapOfLightPrograms; //!< map of lighting programs depending on lights configuration
682
683   Handle(OpenGl_ShaderProgram)       myStereoPrograms[Graphic3d_StereoMode_NB]; //!< standard stereo programs
684
685   Handle(OpenGl_VertexBuffer)        myBoundBoxVertBuffer; //!< bounding box vertex buffer
686
687   OpenGl_Context*                    myContext;            //!< OpenGL context
688
689 protected:
690
691   OpenGl_ProjectionState             myProjectionState;    //!< State of OCCT projection  transformation
692   OpenGl_ModelWorldState             myModelWorldState;    //!< State of OCCT model-world transformation
693   OpenGl_WorldViewState              myWorldViewState;     //!< State of OCCT world-view  transformation
694   OpenGl_ClippingState               myClippingState;      //!< State of OCCT clipping planes
695   OpenGl_LightSourceState            myLightSourceState;   //!< State of OCCT light sources
696   OpenGl_MaterialState               myMaterialState;      //!< State of Front and Back materials
697   OpenGl_OitState                    myOitState;           //!< State of OIT uniforms
698
699   gp_XYZ                             myLocalOrigin;        //!< local camera transformation
700   Standard_Boolean                   myHasLocalOrigin;     //!< flag indicating that local camera transformation has been set
701
702   mutable NCollection_Array1<OpenGl_ShaderLightType>       myLightTypeArray;
703   mutable NCollection_Array1<OpenGl_ShaderLightParameters> myLightParamsArray;
704   mutable NCollection_Array1<OpenGl_Vec4>                  myClipPlaneArray;
705   mutable NCollection_Array1<OpenGl_Vec4d>                 myClipPlaneArrayFfp;
706   mutable NCollection_Array1<Standard_Integer>             myClipChainArray;
707
708 private:
709
710   const OpenGl_View*                 myLastView;           //!< Pointer to the last view shader manager used with
711
712 };
713
714 DEFINE_STANDARD_HANDLE(OpenGl_ShaderManager, Standard_Transient)
715
716 #endif // _OpenGl_ShaderManager_HeaderFile