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