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