1 // Created on: 2013-09-26
2 // Created by: Denis BOGOLEPOV
3 // Copyright (c) 2013-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #ifndef _OpenGl_ShaderManager_HeaderFile
17 #define _OpenGl_ShaderManager_HeaderFile
19 #include <Graphic3d_ShaderProgram.hxx>
20 #include <Graphic3d_StereoMode.hxx>
21 #include <Graphic3d_TypeOfShadingModel.hxx>
23 #include <NCollection_DataMap.hxx>
24 #include <NCollection_Sequence.hxx>
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>
37 //! List of shader programs.
38 typedef NCollection_Sequence<Handle(OpenGl_ShaderProgram)> OpenGl_ShaderProgramList;
40 class OpenGl_ShaderManager;
41 DEFINE_STANDARD_HANDLE(OpenGl_ShaderManager, Standard_Transient)
43 //! This class is responsible for managing shader programs.
44 class OpenGl_ShaderManager : public Standard_Transient
46 friend class OpenGl_ShaderProgram;
50 //! Creates new empty shader manager.
51 Standard_EXPORT OpenGl_ShaderManager (OpenGl_Context* theContext);
53 //! Releases resources of shader manager.
54 Standard_EXPORT virtual ~OpenGl_ShaderManager();
56 //! Release all resources.
57 Standard_EXPORT void clear();
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);
68 //! Unregisters specified shader program.
69 Standard_EXPORT void Unregister (TCollection_AsciiString& theShareKey,
70 Handle(OpenGl_ShaderProgram)& theProgram);
72 //! Returns list of registered shader programs.
73 Standard_EXPORT const OpenGl_ShaderProgramList& ShaderPrograms() const;
75 //! Returns true if no program objects are registered in the manager.
76 Standard_EXPORT Standard_Boolean IsEmpty() const;
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)
85 if (!theCustomProgram.IsNull()
86 || myContext->caps->ffpEnable)
88 return bindProgramWithState (theCustomProgram);
91 const Standard_Integer aBits = getProgramBits (theTexture, theHasVertColor, theEnableEnvMap);
92 Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits);
93 return bindProgramWithState (aProgram);
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)
103 if (!theCustomProgram.IsNull()
104 || myContext->caps->ffpEnable)
106 return bindProgramWithState (theCustomProgram);
109 Standard_Integer aBits = getProgramBits (theTexture, theHasVertColor);
112 aBits |= OpenGl_PO_StippleLine;
115 Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits);
116 return bindProgramWithState (aProgram);
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)
125 if (!theCustomProgram.IsNull()
126 || myContext->caps->ffpEnable)
128 return bindProgramWithState (theCustomProgram);
131 const Standard_Integer aBits = getProgramBits (theTexture, theHasVertColor) | OpenGl_PO_Point;
132 Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits);
133 return bindProgramWithState (aProgram);
136 //! Bind program for rendering alpha-textured font.
137 Standard_Boolean BindFontProgram (const Handle(OpenGl_ShaderProgram)& theCustomProgram)
139 if (!theCustomProgram.IsNull()
140 || myContext->caps->ffpEnable)
142 return bindProgramWithState (theCustomProgram);
145 if (myFontProgram.IsNull())
147 prepareStdProgramFont();
150 return bindProgramWithState (myFontProgram);
153 //! Bind program for FBO blit operation.
154 Standard_Boolean BindFboBlitProgram()
156 if (myBlitProgram.IsNull())
158 prepareStdProgramFboBlit();
160 return !myBlitProgram.IsNull()
161 && myContext->BindProgram (myBlitProgram);
164 //! Bind program for rendering stereoscopic image.
165 Standard_Boolean BindStereoProgram (const Graphic3d_StereoMode theStereoMode)
167 if (theStereoMode < 0 || theStereoMode >= Graphic3d_StereoMode_NB)
169 return Standard_False;
172 if (myStereoPrograms[theStereoMode].IsNull())
174 prepareStdProgramStereo (myStereoPrograms[theStereoMode], theStereoMode);
176 const Handle(OpenGl_ShaderProgram)& aProgram = myStereoPrograms[theStereoMode];
177 return !aProgram.IsNull()
178 && myContext->BindProgram (aProgram);
183 //! Returns current state of OCCT light sources.
184 Standard_EXPORT const OpenGl_LightSourceState& LightSourceState() const;
186 //! Updates state of OCCT light sources.
187 Standard_EXPORT void UpdateLightSourceStateTo (const OpenGl_ListOfLight* theLights);
189 //! Pushes current state of OCCT light sources to specified program.
190 Standard_EXPORT void PushLightSourceState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
194 //! Returns current state of OCCT projection transform.
195 Standard_EXPORT const OpenGl_ProjectionState& ProjectionState() const;
197 //! Updates state of OCCT projection transform.
198 Standard_EXPORT void UpdateProjectionStateTo (const OpenGl_Mat4& theProjectionMatrix);
200 //! Pushes current state of OCCT projection transform to specified program.
201 Standard_EXPORT void PushProjectionState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
205 //! Returns current state of OCCT model-world transform.
206 Standard_EXPORT const OpenGl_ModelWorldState& ModelWorldState() const;
208 //! Updates state of OCCT model-world transform.
209 Standard_EXPORT void UpdateModelWorldStateTo (const OpenGl_Mat4& theModelWorldMatrix);
211 //! Pushes current state of OCCT model-world transform to specified program.
212 Standard_EXPORT void PushModelWorldState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
216 //! Returns current state of OCCT world-view transform.
217 Standard_EXPORT const OpenGl_WorldViewState& WorldViewState() const;
219 //! Updates state of OCCT world-view transform.
220 Standard_EXPORT void UpdateWorldViewStateTo (const OpenGl_Mat4& theWorldViewMatrix);
222 //! Pushes current state of OCCT world-view transform to specified program.
223 Standard_EXPORT void PushWorldViewState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
227 //! Updates state of OCCT clipping planes.
228 Standard_EXPORT void UpdateClippingState();
230 //! Reverts state of OCCT clipping planes.
231 Standard_EXPORT void RevertClippingState();
233 //! Pushes current state of OCCT clipping planes to specified program.
234 Standard_EXPORT void PushClippingState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
238 //! Pushes current state of OCCT graphics parameters to specified program.
239 Standard_EXPORT void PushState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
243 //! Overwrites context
244 void SetContext (OpenGl_Context* theCtx)
249 //! Returns true when provided context is the same as used one by shader manager.
250 bool IsSameContext (OpenGl_Context* theCtx) const
252 return myContext == theCtx;
255 //! Sets shading model.
256 Standard_EXPORT void SetShadingModel (const Graphic3d_TypeOfShadingModel theModel);
258 //! Sets last view manger used with.
259 //! Helps to handle matrix states in multi-view configurations.
260 void SetLastView (const OpenGl_View* theLastView)
262 myLastView = theLastView;
265 //! Returns true when provided view is the same as cached one.
266 bool IsSameView (const OpenGl_View* theView) const
268 return myLastView == theView;
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)
279 Standard_Integer aBits = 0;
281 const Standard_Integer aNbPlanes = myContext->Clipping().NbClippingOrCappingOn();
284 aBits |= OpenGl_PO_ClipPlanesN;
287 aBits |= OpenGl_PO_ClipPlanes1;
289 else if (aNbPlanes == 2)
291 aBits |= OpenGl_PO_ClipPlanes2;
297 // Environment map overwrites material texture
298 aBits |= OpenGl_PO_TextureEnv;
300 else if (!theTexture.IsNull())
302 aBits |= theTexture->IsAlpha() ? OpenGl_PO_TextureA : OpenGl_PO_TextureRGB;
306 aBits |= OpenGl_PO_VertColor;
311 //! Prepare standard GLSL program.
312 Handle(OpenGl_ShaderProgram)& getStdProgram (const Standard_Boolean theToLightOn,
313 const Standard_Integer theBits)
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)
319 Handle(OpenGl_ShaderProgram)& aProgram = myLightPrograms->ChangeValue (theBits);
320 if (aProgram.IsNull())
322 prepareStdProgramLight (aProgram, theBits);
327 Handle(OpenGl_ShaderProgram)& aProgram = myFlatPrograms.ChangeValue (theBits);
328 if (aProgram.IsNull())
330 prepareStdProgramFlat (aProgram, theBits);
335 //! Prepare standard GLSL program for accessing point sprite alpha.
336 Standard_EXPORT TCollection_AsciiString pointSpriteAlphaSrc (const Standard_Integer theBits);
338 //! Prepare standard GLSL program for computing point sprite shading.
339 Standard_EXPORT TCollection_AsciiString pointSpriteShadingSrc (const TCollection_AsciiString theBaseColorSrc, const Standard_Integer theBits);
341 //! Prepare standard GLSL program for textured font.
342 Standard_EXPORT Standard_Boolean prepareStdProgramFont();
344 //! Prepare standard GLSL program for FBO blit operation.
345 Standard_EXPORT Standard_Boolean prepareStdProgramFboBlit();
347 //! Prepare standard GLSL program without lighting.
348 Standard_EXPORT Standard_Boolean prepareStdProgramFlat (Handle(OpenGl_ShaderProgram)& theProgram,
349 const Standard_Integer theBits);
351 //! Prepare standard GLSL program with lighting.
352 Standard_Boolean prepareStdProgramLight (Handle(OpenGl_ShaderProgram)& theProgram,
353 const Standard_Integer theBits)
355 return myShadingModel == Graphic3d_TOSM_FRAGMENT
356 ? prepareStdProgramPhong (theProgram, theBits)
357 : prepareStdProgramGouraud (theProgram, theBits);
360 //! Prepare standard GLSL program with per-vertex lighting.
361 Standard_EXPORT Standard_Boolean prepareStdProgramGouraud (Handle(OpenGl_ShaderProgram)& theProgram,
362 const Standard_Integer theBits);
364 //! Prepare standard GLSL program with per-pixel lighting.
365 Standard_EXPORT Standard_Boolean prepareStdProgramPhong (Handle(OpenGl_ShaderProgram)& theProgram,
366 const Standard_Integer theBits);
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);
372 //! Bind specified program to current context and apply state.
373 Standard_EXPORT Standard_Boolean bindProgramWithState (const Handle(OpenGl_ShaderProgram)& theProgram);
375 //! Set pointer myLightPrograms to active lighting programs set from myMapOfLightPrograms
376 Standard_EXPORT void switchLightPrograms();
378 //! Prepare standard GLSL program for stereoscopic image.
379 Standard_EXPORT Standard_Boolean prepareStdProgramStereo (Handle(OpenGl_ShaderProgram)& theProgram,
380 const Graphic3d_StereoMode theStereoMode);
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
392 Handle(OpenGl_ShaderProgram) myStereoPrograms[Graphic3d_StereoMode_NB]; //!< standard stereo programs
394 OpenGl_Context* myContext; //!< OpenGL context
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
406 const OpenGl_View* myLastView; //!< Pointer to the last view shader manager used with
410 DEFINE_STANDARD_RTTIEXT(OpenGl_ShaderManager,Standard_Transient)
413 #endif // _OpenGl_ShaderManager_HeaderFile