0025305: Visualization, TKOpenGl - support stipple line aspects within built-in GLSL...
[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
21 #include <NCollection_DataMap.hxx>
22 #include <NCollection_Sequence.hxx>
23
24 #include <Handle_OpenGl_ShaderManager.hxx>
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_Texture.hxx>
32 #include <Visual3d_TypeOfModel.hxx>
33
34 class OpenGl_View;
35
36 //! List of shader programs.
37 typedef NCollection_Sequence<Handle(OpenGl_ShaderProgram)> OpenGl_ShaderProgramList;
38
39 //! Map to declare per-program states of OCCT materials.
40 typedef NCollection_DataMap<Handle(OpenGl_ShaderProgram), OpenGl_MaterialState> OpenGl_MaterialStates;
41
42 //! This class is responsible for managing shader programs.
43 class OpenGl_ShaderManager : public Standard_Transient
44 {
45   friend class OpenGl_ShaderProgram;
46
47 public:
48
49   //! Creates new empty shader manager.
50   Standard_EXPORT OpenGl_ShaderManager (OpenGl_Context* theContext);
51
52   //! Releases resources of shader manager.
53   Standard_EXPORT virtual ~OpenGl_ShaderManager();
54
55   //! Release all resources.
56   Standard_EXPORT void clear();
57
58   //! Creates new shader program or re-use shared instance.
59   //! @param theProxy    [IN]  program definition
60   //! @param theShareKey [OUT] sharing key
61   //! @param theProgram  [OUT] OpenGL program
62   //! @return true on success
63   Standard_EXPORT Standard_Boolean Create (const Handle(Graphic3d_ShaderProgram)& theProxy,
64                                            TCollection_AsciiString&               theShareKey,
65                                            Handle(OpenGl_ShaderProgram)&          theProgram);
66
67   //! Unregisters specified shader program.
68   Standard_EXPORT void Unregister (TCollection_AsciiString&      theShareKey,
69                                    Handle(OpenGl_ShaderProgram)& theProgram);
70
71   //! Returns list of registered shader programs.
72   Standard_EXPORT const OpenGl_ShaderProgramList& ShaderPrograms() const;
73
74   //! Returns true if no program objects are registered in the manager.
75   Standard_EXPORT Standard_Boolean IsEmpty() const;
76
77   //! Bind program for filled primitives rendering
78   Standard_Boolean BindProgram (const OpenGl_AspectFace*            theAspect,
79                                 const Handle(OpenGl_Texture)&       theTexture,
80                                 const Standard_Boolean              theToLightOn,
81                                 const Standard_Boolean              theHasVertColor,
82                                 const Handle(OpenGl_ShaderProgram)& theCustomProgram)
83   {
84     if (!theCustomProgram.IsNull()
85      || myContext->caps->ffpEnable)
86     {
87       return bindProgramWithState (theCustomProgram, theAspect);
88     }
89
90     const Standard_Integer        aBits    = getProgramBits (theTexture, theHasVertColor, Standard_True);
91     Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits);
92     return bindProgramWithState (aProgram, theAspect);
93   }
94
95   //! Bind program for line rendering
96   Standard_Boolean BindProgram (const OpenGl_AspectLine*            theAspect,
97                                 const Handle(OpenGl_Texture)&       theTexture,
98                                 const Standard_Boolean              theToLightOn,
99                                 const Standard_Boolean              theHasVertColor,
100                                 const Handle(OpenGl_ShaderProgram)& theCustomProgram)
101   {
102     if (!theCustomProgram.IsNull()
103      || myContext->caps->ffpEnable)
104     {
105       return bindProgramWithState (theCustomProgram, theAspect);
106     }
107
108     Standard_Integer aBits = getProgramBits (theTexture, theHasVertColor);
109     if (theAspect->Type() != Aspect_TOL_SOLID)
110     {
111       aBits |= OpenGl_PO_StippleLine;
112     }
113
114     Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits);
115     return bindProgramWithState (aProgram, theAspect);
116   }
117
118   //! Bind program for point rendering
119   Standard_Boolean BindProgram (const OpenGl_AspectMarker*          theAspect,
120                                 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, theAspect);
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, theAspect);
134   }
135
136   //! Bind program for rendering alpha-textured font.
137   Standard_Boolean BindProgram (const OpenGl_AspectText*            theAspect,
138                                 const Handle(OpenGl_ShaderProgram)& theCustomProgram)
139   {
140     if (!theCustomProgram.IsNull()
141      || myContext->caps->ffpEnable)
142     {
143       return bindProgramWithState (theCustomProgram, theAspect);
144     }
145
146     if (myFontProgram.IsNull())
147     {
148       prepareStdProgramFont();
149     }
150     return bindProgramWithState (myFontProgram, theAspect);
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 Anaglyph image.
165   Standard_Boolean BindAnaglyphProgram()
166   {
167     if (myAnaglyphProgram.IsNull())
168     {
169       prepareStdProgramAnaglyph();
170     }
171     return !myAnaglyphProgram.IsNull()
172          && myContext->BindProgram (myAnaglyphProgram);
173   }
174
175 public:
176
177   //! Returns current state of OCCT light sources.
178   Standard_EXPORT const OpenGl_LightSourceState& LightSourceState() const;
179
180   //! Updates state of OCCT light sources.
181   Standard_EXPORT void UpdateLightSourceStateTo (const OpenGl_ListOfLight* theLights);
182
183   //! Pushes current state of OCCT light sources to specified program.
184   Standard_EXPORT void PushLightSourceState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
185
186 public:
187
188   //! Returns current state of OCCT projection transform.
189   Standard_EXPORT const OpenGl_ProjectionState& ProjectionState() const;
190
191   //! Updates state of OCCT projection transform.
192   Standard_EXPORT void UpdateProjectionStateTo (const OpenGl_Mat4& theProjectionMatrix);
193
194   //! Pushes current state of OCCT projection transform to specified program.
195   Standard_EXPORT void PushProjectionState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
196
197 public:
198
199   //! Returns current state of OCCT model-world transform.
200   Standard_EXPORT const OpenGl_ModelWorldState& ModelWorldState() const;
201
202   //! Updates state of OCCT model-world transform.
203   Standard_EXPORT void UpdateModelWorldStateTo (const OpenGl_Mat4& theModelWorldMatrix);
204
205   //! Pushes current state of OCCT model-world transform to specified program.
206   Standard_EXPORT void PushModelWorldState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
207
208 public:
209
210   //! Returns current state of OCCT world-view transform.
211   Standard_EXPORT const OpenGl_WorldViewState& WorldViewState() const;
212
213   //! Updates state of OCCT world-view transform.
214   Standard_EXPORT void UpdateWorldViewStateTo (const OpenGl_Mat4& theWorldViewMatrix);
215
216   //! Pushes current state of OCCT world-view transform to specified program.
217   Standard_EXPORT void PushWorldViewState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
218
219 public:
220
221   //! Updates state of OCCT clipping planes.
222   Standard_EXPORT void UpdateClippingState();
223
224   //! Reverts state of OCCT clipping planes.
225   Standard_EXPORT void RevertClippingState();
226
227   //! Pushes current state of OCCT clipping planes to specified program.
228   Standard_EXPORT void PushClippingState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
229
230 public:
231
232   //! Resets state of OCCT material for all programs.
233   Standard_EXPORT void ResetMaterialStates();
234
235   //! Updates state of OCCT material for specified program.
236   Standard_EXPORT void UpdateMaterialStateTo (const Handle(OpenGl_ShaderProgram)& theProgram,
237                                               const OpenGl_Element*               theAspect);
238
239   //! Pushes current state of OCCT material to specified program.
240   Standard_EXPORT void PushMaterialState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
241
242   //! Returns current state of OCCT material for specified program.
243   Standard_EXPORT const OpenGl_MaterialState* MaterialState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
244
245 public:
246
247   //! Returns current state of OCCT surface detail.
248   Standard_EXPORT const OpenGl_SurfaceDetailState& SurfaceDetailState() const;
249
250   //! Updates state of OCCT surface detail.
251   Standard_EXPORT void UpdateSurfaceDetailStateTo (const Visual3d_TypeOfSurfaceDetail theDetail);
252
253 public:
254
255   //! Pushes current state of OCCT graphics parameters to specified program.
256   Standard_EXPORT void PushState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
257
258 public:
259
260   //! Overwrites context
261   void SetContext (OpenGl_Context* theCtx)
262   {
263     myContext = theCtx;
264   }
265
266   //! Sets shading model.
267   Standard_EXPORT void SetShadingModel(const Visual3d_TypeOfModel theModel);
268
269   //! Sets last view manger used with.
270   //! Helps to handle matrix states in multi-view configurations.
271   void SetLastView (const OpenGl_View* theLastView)
272   {
273     myLastView = theLastView;
274   }
275
276   //! Returns true when provided view is the same as cached one.
277   bool IsSameView (const OpenGl_View* theView) const
278   {
279     return myLastView == theView;
280   }
281
282 protected:
283
284   //! Define program bits.
285   Standard_Integer getProgramBits (const Handle(OpenGl_Texture)& theTexture,
286                                    const Standard_Boolean        theHasVertColor,
287                                    const Standard_Boolean        theEnableEnvMap = Standard_False)
288
289   {
290     Standard_Integer aBits = 0;
291     if (myContext->Clipping().IsClippingOrCappingOn())
292     {
293       aBits |= OpenGl_PO_ClipPlanes;
294     }
295     if (theEnableEnvMap && mySurfaceDetailState.Detail() == Visual3d_TOD_ENVIRONMENT)
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 textured font.
336   Standard_EXPORT Standard_Boolean prepareStdProgramFont();
337
338   //! Prepare standard GLSL program for FBO blit operation.
339   Standard_EXPORT Standard_Boolean prepareStdProgramFboBlit();
340
341   //! Prepare standard GLSL program without lighting.
342   Standard_EXPORT Standard_Boolean prepareStdProgramFlat (Handle(OpenGl_ShaderProgram)& theProgram,
343                                                           const Standard_Integer        theBits);
344
345   //! Prepare standard GLSL program with lighting.
346   Standard_Boolean prepareStdProgramLight (Handle(OpenGl_ShaderProgram)& theProgram,
347                                            const Standard_Integer        theBits)
348   {
349     return myShadingModel == Visual3d_TOM_FRAGMENT
350          ? prepareStdProgramPhong   (theProgram, theBits)
351          : prepareStdProgramGouraud (theProgram, theBits);
352   }
353
354   //! Prepare standard GLSL program with per-vertex lighting.
355   Standard_EXPORT Standard_Boolean prepareStdProgramGouraud (Handle(OpenGl_ShaderProgram)& theProgram,
356                                                              const Standard_Integer        theBits);
357
358   //! Prepare standard GLSL program with per-pixel lighting.
359   Standard_EXPORT Standard_Boolean prepareStdProgramPhong (Handle(OpenGl_ShaderProgram)& theProgram,
360                                                            const Standard_Integer        theBits);
361
362   //! Define computeLighting GLSL function depending on current lights configuration
363   //! @param theHasVertColor flag to use getVertColor() instead of Ambient and Diffuse components of active material
364   Standard_EXPORT TCollection_AsciiString stdComputeLighting (const Standard_Boolean theHasVertColor);
365
366   //! Bind specified program to current context and apply state.
367   Standard_EXPORT Standard_Boolean bindProgramWithState (const Handle(OpenGl_ShaderProgram)& theProgram,
368                                                          const OpenGl_Element*               theAspect);
369
370   //! Set pointer myLightPrograms to active lighting programs set from myMapOfLightPrograms
371   Standard_EXPORT void switchLightPrograms();
372
373   //! Prepare standard GLSL program for Anaglyph image.
374   Standard_EXPORT Standard_Boolean prepareStdProgramAnaglyph();
375
376 protected:
377
378   Visual3d_TypeOfModel               myShadingModel;       //!< lighting shading model
379   OpenGl_ShaderProgramList           myProgramList;        //!< The list of shader programs
380   Handle(OpenGl_SetOfShaderPrograms) myLightPrograms;      //!< pointer to active lighting programs matrix
381   OpenGl_SetOfShaderPrograms         myFlatPrograms;       //!< programs matrix without  lighting
382   Handle(OpenGl_ShaderProgram)       myFontProgram;        //!< standard program for textured text
383   Handle(OpenGl_ShaderProgram)       myBlitProgram;        //!< standard program for FBO blit emulation
384   OpenGl_MapOfShaderPrograms         myMapOfLightPrograms; //!< map of lighting programs depending on shading model and lights configuration
385
386   Handle(OpenGl_ShaderProgram)       myAnaglyphProgram;    //!< standard program for Anaglyph image
387
388   OpenGl_Context*                    myContext;            //!< OpenGL context
389
390 protected:
391
392   OpenGl_MaterialStates              myMaterialStates;     //!< Per-program state of OCCT material
393   OpenGl_ProjectionState             myProjectionState;    //!< State of OCCT projection  transformation
394   OpenGl_ModelWorldState             myModelWorldState;    //!< State of OCCT model-world transformation
395   OpenGl_WorldViewState              myWorldViewState;     //!< State of OCCT world-view  transformation
396   OpenGl_ClippingState               myClippingState;      //!< State of OCCT clipping planes
397   OpenGl_LightSourceState            myLightSourceState;   //!< State of OCCT light sources
398   OpenGl_SurfaceDetailState          mySurfaceDetailState; //!< State of OCCT surface detail
399
400 private:
401
402   const OpenGl_View*                 myLastView;           //!< Pointer to the last view shader manager used with
403
404 public:
405
406   DEFINE_STANDARD_RTTI (OpenGl_ShaderManager)
407
408 };
409
410 #endif // _OpenGl_ShaderManager_HeaderFile