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>
22 #include <NCollection_DataMap.hxx>
23 #include <NCollection_Sequence.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>
36 //! List of shader programs.
37 typedef NCollection_Sequence<Handle(OpenGl_ShaderProgram)> OpenGl_ShaderProgramList;
39 //! Map to declare per-program states of OCCT materials.
40 typedef NCollection_DataMap<Handle(OpenGl_ShaderProgram), OpenGl_MaterialState> OpenGl_MaterialStates;
42 class OpenGl_ShaderManager;
43 DEFINE_STANDARD_HANDLE(OpenGl_ShaderManager, Standard_Transient)
45 //! This class is responsible for managing shader programs.
46 class OpenGl_ShaderManager : public Standard_Transient
48 friend class OpenGl_ShaderProgram;
52 //! Creates new empty shader manager.
53 Standard_EXPORT OpenGl_ShaderManager (OpenGl_Context* theContext);
55 //! Releases resources of shader manager.
56 Standard_EXPORT virtual ~OpenGl_ShaderManager();
58 //! Release all resources.
59 Standard_EXPORT void clear();
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);
70 //! Unregisters specified shader program.
71 Standard_EXPORT void Unregister (TCollection_AsciiString& theShareKey,
72 Handle(OpenGl_ShaderProgram)& theProgram);
74 //! Returns list of registered shader programs.
75 Standard_EXPORT const OpenGl_ShaderProgramList& ShaderPrograms() const;
77 //! Returns true if no program objects are registered in the manager.
78 Standard_EXPORT Standard_Boolean IsEmpty() const;
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)
87 if (!theCustomProgram.IsNull()
88 || myContext->caps->ffpEnable)
90 return bindProgramWithState (theCustomProgram, theAspect);
93 const Standard_Integer aBits = getProgramBits (theTexture, theHasVertColor, Standard_True);
94 Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits);
95 return bindProgramWithState (aProgram, theAspect);
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)
105 if (!theCustomProgram.IsNull()
106 || myContext->caps->ffpEnable)
108 return bindProgramWithState (theCustomProgram, theAspect);
111 Standard_Integer aBits = getProgramBits (theTexture, theHasVertColor);
112 if (theAspect->Type() != Aspect_TOL_SOLID)
114 aBits |= OpenGl_PO_StippleLine;
117 Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits);
118 return bindProgramWithState (aProgram, theAspect);
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)
128 if (!theCustomProgram.IsNull()
129 || myContext->caps->ffpEnable)
131 return bindProgramWithState (theCustomProgram, theAspect);
134 const Standard_Integer aBits = getProgramBits (theTexture, theHasVertColor) | OpenGl_PO_Point;
135 Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits);
136 return bindProgramWithState (aProgram, theAspect);
139 //! Bind program for rendering alpha-textured font.
140 Standard_Boolean BindProgram (const OpenGl_AspectText* theAspect,
141 const Handle(OpenGl_ShaderProgram)& theCustomProgram)
143 if (!theCustomProgram.IsNull()
144 || myContext->caps->ffpEnable)
146 return bindProgramWithState (theCustomProgram, theAspect);
149 if (myFontProgram.IsNull())
151 prepareStdProgramFont();
153 return bindProgramWithState (myFontProgram, theAspect);
156 //! Bind program for FBO blit operation.
157 Standard_Boolean BindFboBlitProgram()
159 if (myBlitProgram.IsNull())
161 prepareStdProgramFboBlit();
163 return !myBlitProgram.IsNull()
164 && myContext->BindProgram (myBlitProgram);
167 //! Bind program for rendering stereoscopic image.
168 Standard_Boolean BindStereoProgram (const Graphic3d_StereoMode theStereoMode)
170 if (theStereoMode < 0 || theStereoMode >= Graphic3d_StereoMode_NB)
172 return Standard_False;
175 if (myStereoPrograms[theStereoMode].IsNull())
177 prepareStdProgramStereo (myStereoPrograms[theStereoMode], theStereoMode);
179 const Handle(OpenGl_ShaderProgram)& aProgram = myStereoPrograms[theStereoMode];
180 return !aProgram.IsNull()
181 && myContext->BindProgram (aProgram);
186 //! Returns current state of OCCT light sources.
187 Standard_EXPORT const OpenGl_LightSourceState& LightSourceState() const;
189 //! Updates state of OCCT light sources.
190 Standard_EXPORT void UpdateLightSourceStateTo (const OpenGl_ListOfLight* theLights);
192 //! Pushes current state of OCCT light sources to specified program.
193 Standard_EXPORT void PushLightSourceState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
197 //! Returns current state of OCCT projection transform.
198 Standard_EXPORT const OpenGl_ProjectionState& ProjectionState() const;
200 //! Updates state of OCCT projection transform.
201 Standard_EXPORT void UpdateProjectionStateTo (const OpenGl_Mat4& theProjectionMatrix);
203 //! Pushes current state of OCCT projection transform to specified program.
204 Standard_EXPORT void PushProjectionState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
208 //! Returns current state of OCCT model-world transform.
209 Standard_EXPORT const OpenGl_ModelWorldState& ModelWorldState() const;
211 //! Updates state of OCCT model-world transform.
212 Standard_EXPORT void UpdateModelWorldStateTo (const OpenGl_Mat4& theModelWorldMatrix);
214 //! Pushes current state of OCCT model-world transform to specified program.
215 Standard_EXPORT void PushModelWorldState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
219 //! Returns current state of OCCT world-view transform.
220 Standard_EXPORT const OpenGl_WorldViewState& WorldViewState() const;
222 //! Updates state of OCCT world-view transform.
223 Standard_EXPORT void UpdateWorldViewStateTo (const OpenGl_Mat4& theWorldViewMatrix);
225 //! Pushes current state of OCCT world-view transform to specified program.
226 Standard_EXPORT void PushWorldViewState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
230 //! Updates state of OCCT clipping planes.
231 Standard_EXPORT void UpdateClippingState();
233 //! Reverts state of OCCT clipping planes.
234 Standard_EXPORT void RevertClippingState();
236 //! Pushes current state of OCCT clipping planes to specified program.
237 Standard_EXPORT void PushClippingState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
241 //! Resets state of OCCT material for all programs.
242 Standard_EXPORT void ResetMaterialStates();
244 //! Updates state of OCCT material for specified program.
245 Standard_EXPORT void UpdateMaterialStateTo (const Handle(OpenGl_ShaderProgram)& theProgram,
246 const OpenGl_Element* theAspect);
248 //! Pushes current state of OCCT material to specified program.
249 Standard_EXPORT void PushMaterialState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
251 //! Returns current state of OCCT material for specified program.
252 Standard_EXPORT const OpenGl_MaterialState* MaterialState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
256 //! Returns current state of OCCT surface detail.
257 Standard_EXPORT const OpenGl_SurfaceDetailState& SurfaceDetailState() const;
259 //! Updates state of OCCT surface detail.
260 Standard_EXPORT void UpdateSurfaceDetailStateTo (const Visual3d_TypeOfSurfaceDetail theDetail);
264 //! Pushes current state of OCCT graphics parameters to specified program.
265 Standard_EXPORT void PushState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
269 //! Overwrites context
270 void SetContext (OpenGl_Context* theCtx)
275 //! Sets shading model.
276 Standard_EXPORT void SetShadingModel(const Visual3d_TypeOfModel theModel);
278 //! Sets last view manger used with.
279 //! Helps to handle matrix states in multi-view configurations.
280 void SetLastView (const OpenGl_View* theLastView)
282 myLastView = theLastView;
285 //! Returns true when provided view is the same as cached one.
286 bool IsSameView (const OpenGl_View* theView) const
288 return myLastView == theView;
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)
299 Standard_Integer aBits = 0;
300 if (myContext->Clipping().IsClippingOrCappingOn())
302 aBits |= OpenGl_PO_ClipPlanes;
304 if (theEnableEnvMap && mySurfaceDetailState.Detail() == Visual3d_TOD_ENVIRONMENT)
306 // Environment map overwrites material texture
307 aBits |= OpenGl_PO_TextureEnv;
309 else if (!theTexture.IsNull())
311 aBits |= theTexture->IsAlpha() ? OpenGl_PO_TextureA : OpenGl_PO_TextureRGB;
315 aBits |= OpenGl_PO_VertColor;
320 //! Prepare standard GLSL program.
321 Handle(OpenGl_ShaderProgram)& getStdProgram (const Standard_Boolean theToLightOn,
322 const Standard_Integer theBits)
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)
328 Handle(OpenGl_ShaderProgram)& aProgram = myLightPrograms->ChangeValue (theBits);
329 if (aProgram.IsNull())
331 prepareStdProgramLight (aProgram, theBits);
336 Handle(OpenGl_ShaderProgram)& aProgram = myFlatPrograms.ChangeValue (theBits);
337 if (aProgram.IsNull())
339 prepareStdProgramFlat (aProgram, theBits);
344 //! Prepare standard GLSL program for textured font.
345 Standard_EXPORT Standard_Boolean prepareStdProgramFont();
347 //! Prepare standard GLSL program for FBO blit operation.
348 Standard_EXPORT Standard_Boolean prepareStdProgramFboBlit();
350 //! Prepare standard GLSL program without lighting.
351 Standard_EXPORT Standard_Boolean prepareStdProgramFlat (Handle(OpenGl_ShaderProgram)& theProgram,
352 const Standard_Integer theBits);
354 //! Prepare standard GLSL program with lighting.
355 Standard_Boolean prepareStdProgramLight (Handle(OpenGl_ShaderProgram)& theProgram,
356 const Standard_Integer theBits)
358 return myShadingModel == Visual3d_TOM_FRAGMENT
359 ? prepareStdProgramPhong (theProgram, theBits)
360 : prepareStdProgramGouraud (theProgram, theBits);
363 //! Prepare standard GLSL program with per-vertex lighting.
364 Standard_EXPORT Standard_Boolean prepareStdProgramGouraud (Handle(OpenGl_ShaderProgram)& theProgram,
365 const Standard_Integer theBits);
367 //! Prepare standard GLSL program with per-pixel lighting.
368 Standard_EXPORT Standard_Boolean prepareStdProgramPhong (Handle(OpenGl_ShaderProgram)& theProgram,
369 const Standard_Integer theBits);
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);
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);
379 //! Set pointer myLightPrograms to active lighting programs set from myMapOfLightPrograms
380 Standard_EXPORT void switchLightPrograms();
382 //! Prepare standard GLSL program for stereoscopic image.
383 Standard_EXPORT Standard_Boolean prepareStdProgramStereo (Handle(OpenGl_ShaderProgram)& theProgram,
384 const Graphic3d_StereoMode theStereoMode);
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
396 Handle(OpenGl_ShaderProgram) myStereoPrograms[Graphic3d_StereoMode_NB]; //!< standard stereo programs
398 OpenGl_Context* myContext; //!< OpenGL context
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
412 const OpenGl_View* myLastView; //!< Pointer to the last view shader manager used with
416 DEFINE_STANDARD_RTTI (OpenGl_ShaderManager, Standard_Transient)
420 #endif // _OpenGl_ShaderManager_HeaderFile