0027696: Return max distance in xdistcs Draw command
[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 #include <Graphic3d_TypeOfShadingModel.hxx>
22
23 #include <NCollection_DataMap.hxx>
24 #include <NCollection_Sequence.hxx>
25
26 #include <OpenGl_SetOfShaderPrograms.hxx>
27 #include <OpenGl_ShaderStates.hxx>
28 #include <OpenGl_AspectFace.hxx>
29 #include <OpenGl_AspectLine.hxx>
30 #include <OpenGl_AspectText.hxx>
31 #include <OpenGl_AspectMarker.hxx>
32 #include <OpenGl_Texture.hxx>
33
34
35 class OpenGl_View;
36
37 //! List of shader programs.
38 typedef NCollection_Sequence<Handle(OpenGl_ShaderProgram)> OpenGl_ShaderProgramList;
39
40 class OpenGl_ShaderManager;
41 DEFINE_STANDARD_HANDLE(OpenGl_ShaderManager, Standard_Transient)
42
43 //! This class is responsible for managing shader programs.
44 class OpenGl_ShaderManager : public Standard_Transient
45 {
46   friend class OpenGl_ShaderProgram;
47
48 public:
49
50   //! Creates new empty shader manager.
51   Standard_EXPORT OpenGl_ShaderManager (OpenGl_Context* theContext);
52
53   //! Releases resources of shader manager.
54   Standard_EXPORT virtual ~OpenGl_ShaderManager();
55
56   //! Release all resources.
57   Standard_EXPORT void clear();
58
59   //! Creates new shader program or re-use shared instance.
60   //! @param theProxy    [IN]  program definition
61   //! @param theShareKey [OUT] sharing key
62   //! @param theProgram  [OUT] OpenGL program
63   //! @return true on success
64   Standard_EXPORT Standard_Boolean Create (const Handle(Graphic3d_ShaderProgram)& theProxy,
65                                            TCollection_AsciiString&               theShareKey,
66                                            Handle(OpenGl_ShaderProgram)&          theProgram);
67
68   //! Unregisters specified shader program.
69   Standard_EXPORT void Unregister (TCollection_AsciiString&      theShareKey,
70                                    Handle(OpenGl_ShaderProgram)& theProgram);
71
72   //! Returns list of registered shader programs.
73   Standard_EXPORT const OpenGl_ShaderProgramList& ShaderPrograms() const;
74
75   //! Returns true if no program objects are registered in the manager.
76   Standard_EXPORT Standard_Boolean IsEmpty() const;
77
78   //! Bind program for filled primitives rendering
79   Standard_Boolean BindFaceProgram (const Handle(OpenGl_Texture)&       theTexture,
80                                     const Standard_Boolean              theToLightOn,
81                                     const Standard_Boolean              theHasVertColor,
82                                     const Standard_Boolean              theEnableEnvMap,
83                                     const Handle(OpenGl_ShaderProgram)& theCustomProgram)
84   {
85     if (!theCustomProgram.IsNull()
86      || myContext->caps->ffpEnable)
87     {
88       return bindProgramWithState (theCustomProgram);
89     }
90
91     const Standard_Integer        aBits    = getProgramBits (theTexture, theHasVertColor, theEnableEnvMap);
92     Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits);
93     return bindProgramWithState (aProgram);
94   }
95
96   //! Bind program for line rendering
97   Standard_Boolean BindLineProgram (const Handle(OpenGl_Texture)&       theTexture,
98                                     const Standard_Boolean              theStipple,
99                                     const Standard_Boolean              theToLightOn,
100                                     const Standard_Boolean              theHasVertColor,
101                                     const Handle(OpenGl_ShaderProgram)& theCustomProgram)
102   {
103     if (!theCustomProgram.IsNull()
104      || myContext->caps->ffpEnable)
105     {
106       return bindProgramWithState (theCustomProgram);
107     }
108
109     Standard_Integer aBits = getProgramBits (theTexture, theHasVertColor);
110     if (theStipple)
111     {
112       aBits |= OpenGl_PO_StippleLine;
113     }
114
115     Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits);
116     return bindProgramWithState (aProgram);
117   }
118
119   //! Bind program for point rendering
120   Standard_Boolean BindMarkerProgram (const Handle(OpenGl_Texture)&       theTexture,
121                                       const Standard_Boolean              theToLightOn,
122                                       const Standard_Boolean              theHasVertColor,
123                                       const Handle(OpenGl_ShaderProgram)& theCustomProgram)
124   {
125     if (!theCustomProgram.IsNull()
126      || myContext->caps->ffpEnable)
127     {
128       return bindProgramWithState (theCustomProgram);
129     }
130
131     const Standard_Integer        aBits    = getProgramBits (theTexture, theHasVertColor) | OpenGl_PO_Point;
132     Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits);
133     return bindProgramWithState (aProgram);
134   }
135
136   //! Bind program for rendering alpha-textured font.
137   Standard_Boolean BindFontProgram (const Handle(OpenGl_ShaderProgram)& theCustomProgram)
138   {
139     if (!theCustomProgram.IsNull()
140      || myContext->caps->ffpEnable)
141     {
142       return bindProgramWithState (theCustomProgram);
143     }
144
145     if (myFontProgram.IsNull())
146     {
147       prepareStdProgramFont();
148     }
149
150     return bindProgramWithState (myFontProgram);
151   }
152
153   //! Bind program for FBO blit operation.
154   Standard_Boolean BindFboBlitProgram()
155   {
156     if (myBlitProgram.IsNull())
157     {
158       prepareStdProgramFboBlit();
159     }
160     return !myBlitProgram.IsNull()
161          && myContext->BindProgram (myBlitProgram);
162   }
163
164   //! Bind program for rendering stereoscopic image.
165   Standard_Boolean BindStereoProgram (const Graphic3d_StereoMode theStereoMode)
166   {
167     if (theStereoMode < 0 || theStereoMode >= Graphic3d_StereoMode_NB)
168     {
169       return Standard_False;
170     }
171
172     if (myStereoPrograms[theStereoMode].IsNull())
173     {
174       prepareStdProgramStereo (myStereoPrograms[theStereoMode], theStereoMode);
175     }
176     const Handle(OpenGl_ShaderProgram)& aProgram = myStereoPrograms[theStereoMode];
177     return !aProgram.IsNull()
178          && myContext->BindProgram (aProgram);
179   }
180
181 public:
182
183   //! Returns current state of OCCT light sources.
184   Standard_EXPORT const OpenGl_LightSourceState& LightSourceState() const;
185
186   //! Updates state of OCCT light sources.
187   Standard_EXPORT void UpdateLightSourceStateTo (const OpenGl_ListOfLight* theLights);
188
189   //! Pushes current state of OCCT light sources to specified program.
190   Standard_EXPORT void PushLightSourceState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
191
192 public:
193
194   //! Returns current state of OCCT projection transform.
195   Standard_EXPORT const OpenGl_ProjectionState& ProjectionState() const;
196
197   //! Updates state of OCCT projection transform.
198   Standard_EXPORT void UpdateProjectionStateTo (const OpenGl_Mat4& theProjectionMatrix);
199
200   //! Pushes current state of OCCT projection transform to specified program.
201   Standard_EXPORT void PushProjectionState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
202
203 public:
204
205   //! Returns current state of OCCT model-world transform.
206   Standard_EXPORT const OpenGl_ModelWorldState& ModelWorldState() const;
207
208   //! Updates state of OCCT model-world transform.
209   Standard_EXPORT void UpdateModelWorldStateTo (const OpenGl_Mat4& theModelWorldMatrix);
210
211   //! Pushes current state of OCCT model-world transform to specified program.
212   Standard_EXPORT void PushModelWorldState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
213
214 public:
215
216   //! Returns current state of OCCT world-view transform.
217   Standard_EXPORT const OpenGl_WorldViewState& WorldViewState() const;
218
219   //! Updates state of OCCT world-view transform.
220   Standard_EXPORT void UpdateWorldViewStateTo (const OpenGl_Mat4& theWorldViewMatrix);
221
222   //! Pushes current state of OCCT world-view transform to specified program.
223   Standard_EXPORT void PushWorldViewState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
224
225 public:
226
227   //! Updates state of OCCT clipping planes.
228   Standard_EXPORT void UpdateClippingState();
229
230   //! Reverts state of OCCT clipping planes.
231   Standard_EXPORT void RevertClippingState();
232
233   //! Pushes current state of OCCT clipping planes to specified program.
234   Standard_EXPORT void PushClippingState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
235
236 public:
237
238   //! Pushes current state of OCCT graphics parameters to specified program.
239   Standard_EXPORT void PushState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
240
241 public:
242
243   //! Overwrites context
244   void SetContext (OpenGl_Context* theCtx)
245   {
246     myContext = theCtx;
247   }
248
249   //! Returns true when provided context is the same as used one by shader manager.
250   bool IsSameContext (OpenGl_Context* theCtx) const
251   {
252     return myContext == theCtx;
253   }
254
255   //! Sets shading model.
256   Standard_EXPORT void SetShadingModel (const Graphic3d_TypeOfShadingModel theModel);
257
258   //! Sets last view manger used with.
259   //! Helps to handle matrix states in multi-view configurations.
260   void SetLastView (const OpenGl_View* theLastView)
261   {
262     myLastView = theLastView;
263   }
264
265   //! Returns true when provided view is the same as cached one.
266   bool IsSameView (const OpenGl_View* theView) const
267   {
268     return myLastView == theView;
269   }
270
271 protected:
272
273   //! Define program bits.
274   Standard_Integer getProgramBits (const Handle(OpenGl_Texture)& theTexture,
275                                    const Standard_Boolean        theHasVertColor,
276                                    const Standard_Boolean        theEnableEnvMap = Standard_False)
277
278   {
279     Standard_Integer aBits = 0;
280
281     const Standard_Integer aNbPlanes = myContext->Clipping().NbClippingOrCappingOn();
282     if (aNbPlanes > 0)
283     {
284       aBits |= OpenGl_PO_ClipPlanesN;
285       if (aNbPlanes == 1)
286       {
287         aBits |= OpenGl_PO_ClipPlanes1;
288       }
289       else if (aNbPlanes == 2)
290       {
291         aBits |= OpenGl_PO_ClipPlanes2;
292       }
293     }
294
295     if (theEnableEnvMap)
296     {
297       // Environment map overwrites material texture
298       aBits |= OpenGl_PO_TextureEnv;
299     }
300     else if (!theTexture.IsNull())
301     {
302       aBits |= theTexture->IsAlpha() ? OpenGl_PO_TextureA : OpenGl_PO_TextureRGB;
303     }
304     if (theHasVertColor)
305     {
306       aBits |= OpenGl_PO_VertColor;
307     }
308     return aBits;
309   }
310
311   //! Prepare standard GLSL program.
312   Handle(OpenGl_ShaderProgram)& getStdProgram (const Standard_Boolean theToLightOn,
313                                                const Standard_Integer theBits)
314   {
315     // If environment map is enabled lighting calculations are
316     // not needed (in accordance with default OCCT behaviour)
317     if (theToLightOn && (theBits & OpenGl_PO_TextureEnv) == 0)
318     {
319       Handle(OpenGl_ShaderProgram)& aProgram = myLightPrograms->ChangeValue (theBits);
320       if (aProgram.IsNull())
321       {
322         prepareStdProgramLight (aProgram, theBits);
323       }
324       return aProgram;
325     }
326
327     Handle(OpenGl_ShaderProgram)& aProgram = myFlatPrograms.ChangeValue (theBits);
328     if (aProgram.IsNull())
329     {
330       prepareStdProgramFlat (aProgram, theBits);
331     }
332     return aProgram;
333   }
334
335   //! Prepare standard GLSL program for accessing point sprite alpha.
336   Standard_EXPORT TCollection_AsciiString pointSpriteAlphaSrc (const Standard_Integer theBits);
337
338   //! Prepare standard GLSL program for computing point sprite shading.
339   Standard_EXPORT TCollection_AsciiString pointSpriteShadingSrc (const TCollection_AsciiString theBaseColorSrc, const Standard_Integer theBits);
340
341   //! Prepare standard GLSL program for textured font.
342   Standard_EXPORT Standard_Boolean prepareStdProgramFont();
343
344   //! Prepare standard GLSL program for FBO blit operation.
345   Standard_EXPORT Standard_Boolean prepareStdProgramFboBlit();
346
347   //! Prepare standard GLSL program without lighting.
348   Standard_EXPORT Standard_Boolean prepareStdProgramFlat (Handle(OpenGl_ShaderProgram)& theProgram,
349                                                           const Standard_Integer        theBits);
350
351   //! Prepare standard GLSL program with lighting.
352   Standard_Boolean prepareStdProgramLight (Handle(OpenGl_ShaderProgram)& theProgram,
353                                            const Standard_Integer        theBits)
354   {
355     return myShadingModel == Graphic3d_TOSM_FRAGMENT
356          ? prepareStdProgramPhong   (theProgram, theBits)
357          : prepareStdProgramGouraud (theProgram, theBits);
358   }
359
360   //! Prepare standard GLSL program with per-vertex lighting.
361   Standard_EXPORT Standard_Boolean prepareStdProgramGouraud (Handle(OpenGl_ShaderProgram)& theProgram,
362                                                              const Standard_Integer        theBits);
363
364   //! Prepare standard GLSL program with per-pixel lighting.
365   Standard_EXPORT Standard_Boolean prepareStdProgramPhong (Handle(OpenGl_ShaderProgram)& theProgram,
366                                                            const Standard_Integer        theBits);
367
368   //! Define computeLighting GLSL function depending on current lights configuration
369   //! @param theHasVertColor flag to use getVertColor() instead of Ambient and Diffuse components of active material
370   Standard_EXPORT TCollection_AsciiString stdComputeLighting (const Standard_Boolean theHasVertColor);
371
372   //! Bind specified program to current context and apply state.
373   Standard_EXPORT Standard_Boolean bindProgramWithState (const Handle(OpenGl_ShaderProgram)& theProgram);
374
375   //! Set pointer myLightPrograms to active lighting programs set from myMapOfLightPrograms
376   Standard_EXPORT void switchLightPrograms();
377
378   //! Prepare standard GLSL program for stereoscopic image.
379   Standard_EXPORT Standard_Boolean prepareStdProgramStereo (Handle(OpenGl_ShaderProgram)& theProgram,
380                                                             const Graphic3d_StereoMode    theStereoMode);
381
382 protected:
383
384   Graphic3d_TypeOfShadingModel       myShadingModel;       //!< lighting shading model
385   OpenGl_ShaderProgramList           myProgramList;        //!< The list of shader programs
386   Handle(OpenGl_SetOfShaderPrograms) myLightPrograms;      //!< pointer to active lighting programs matrix
387   OpenGl_SetOfShaderPrograms         myFlatPrograms;       //!< programs matrix without  lighting
388   Handle(OpenGl_ShaderProgram)       myFontProgram;        //!< standard program for textured text
389   Handle(OpenGl_ShaderProgram)       myBlitProgram;        //!< standard program for FBO blit emulation
390   OpenGl_MapOfShaderPrograms         myMapOfLightPrograms; //!< map of lighting programs depending on shading model and lights configuration
391
392   Handle(OpenGl_ShaderProgram)       myStereoPrograms[Graphic3d_StereoMode_NB]; //!< standard stereo programs
393
394   OpenGl_Context*                    myContext;            //!< OpenGL context
395
396 protected:
397
398   OpenGl_ProjectionState             myProjectionState;    //!< State of OCCT projection  transformation
399   OpenGl_ModelWorldState             myModelWorldState;    //!< State of OCCT model-world transformation
400   OpenGl_WorldViewState              myWorldViewState;     //!< State of OCCT world-view  transformation
401   OpenGl_ClippingState               myClippingState;      //!< State of OCCT clipping planes
402   OpenGl_LightSourceState            myLightSourceState;   //!< State of OCCT light sources
403
404 private:
405
406   const OpenGl_View*                 myLastView;           //!< Pointer to the last view shader manager used with
407
408 public:
409
410   DEFINE_STANDARD_RTTIEXT(OpenGl_ShaderManager,Standard_Transient)
411 };
412
413 #endif // _OpenGl_ShaderManager_HeaderFile