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>
21 #include <NCollection_DataMap.hxx>
22 #include <NCollection_Sequence.hxx>
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>
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 //! This class is responsible for managing shader programs.
43 class OpenGl_ShaderManager : public Standard_Transient
45 friend class OpenGl_ShaderProgram;
49 //! Creates new empty shader manager.
50 Standard_EXPORT OpenGl_ShaderManager (OpenGl_Context* theContext);
52 //! Releases resources of shader manager.
53 Standard_EXPORT virtual ~OpenGl_ShaderManager();
55 //! Release all resources.
56 Standard_EXPORT void clear();
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);
67 //! Unregisters specified shader program.
68 Standard_EXPORT void Unregister (TCollection_AsciiString& theShareKey,
69 Handle(OpenGl_ShaderProgram)& theProgram);
71 //! Returns list of registered shader programs.
72 Standard_EXPORT const OpenGl_ShaderProgramList& ShaderPrograms() const;
74 //! Returns true if no program objects are registered in the manager.
75 Standard_EXPORT Standard_Boolean IsEmpty() const;
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)
84 if (!theCustomProgram.IsNull()
85 || myContext->caps->ffpEnable)
87 return bindProgramWithState (theCustomProgram, theAspect);
90 const Standard_Integer aBits = getProgramBits (theTexture, theHasVertColor, Standard_True);
91 Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits);
92 return bindProgramWithState (aProgram, theAspect);
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)
102 if (!theCustomProgram.IsNull()
103 || myContext->caps->ffpEnable)
105 return bindProgramWithState (theCustomProgram, theAspect);
108 const Standard_Integer aBits = getProgramBits (theTexture, theHasVertColor);
109 Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits);
110 return bindProgramWithState (aProgram, theAspect);
113 //! Bind program for point rendering
114 Standard_Boolean BindProgram (const OpenGl_AspectMarker* theAspect,
115 const Handle(OpenGl_Texture)& theTexture,
116 const Standard_Boolean theToLightOn,
117 const Standard_Boolean theHasVertColor,
118 const Handle(OpenGl_ShaderProgram)& theCustomProgram)
120 if (!theCustomProgram.IsNull()
121 || myContext->caps->ffpEnable)
123 return bindProgramWithState (theCustomProgram, theAspect);
126 const Standard_Integer aBits = getProgramBits (theTexture, theHasVertColor) | OpenGl_PO_Point;
127 Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits);
128 return bindProgramWithState (aProgram, theAspect);
131 //! Bind program for rendering alpha-textured font.
132 Standard_Boolean BindProgram (const OpenGl_AspectText* theAspect,
133 const Handle(OpenGl_ShaderProgram)& theCustomProgram)
135 if (!theCustomProgram.IsNull()
136 || myContext->caps->ffpEnable)
138 return bindProgramWithState (theCustomProgram, theAspect);
141 if (myFontProgram.IsNull())
143 prepareStdProgramFont();
145 return bindProgramWithState (myFontProgram, theAspect);
148 //! Bind program for FBO blit operation.
149 Standard_Boolean BindFboBlitProgram()
151 if (myBlitProgram.IsNull())
153 prepareStdProgramFboBlit();
155 return !myBlitProgram.IsNull()
156 && myContext->BindProgram (myBlitProgram);
161 //! Returns current state of OCCT light sources.
162 Standard_EXPORT const OpenGl_LightSourceState& LightSourceState() const;
164 //! Updates state of OCCT light sources.
165 Standard_EXPORT void UpdateLightSourceStateTo (const OpenGl_ListOfLight* theLights);
167 //! Pushes current state of OCCT light sources to specified program.
168 Standard_EXPORT void PushLightSourceState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
172 //! Returns current state of OCCT projection transform.
173 Standard_EXPORT const OpenGl_ProjectionState& ProjectionState() const;
175 //! Updates state of OCCT projection transform.
176 Standard_EXPORT void UpdateProjectionStateTo (const OpenGl_Mat4& theProjectionMatrix);
178 //! Pushes current state of OCCT projection transform to specified program.
179 Standard_EXPORT void PushProjectionState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
183 //! Returns current state of OCCT model-world transform.
184 Standard_EXPORT const OpenGl_ModelWorldState& ModelWorldState() const;
186 //! Updates state of OCCT model-world transform.
187 Standard_EXPORT void UpdateModelWorldStateTo (const OpenGl_Mat4& theModelWorldMatrix);
189 //! Pushes current state of OCCT model-world transform to specified program.
190 Standard_EXPORT void PushModelWorldState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
194 //! Returns current state of OCCT world-view transform.
195 Standard_EXPORT const OpenGl_WorldViewState& WorldViewState() const;
197 //! Updates state of OCCT world-view transform.
198 Standard_EXPORT void UpdateWorldViewStateTo (const OpenGl_Mat4& theWorldViewMatrix);
200 //! Pushes current state of OCCT world-view transform to specified program.
201 Standard_EXPORT void PushWorldViewState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
205 //! Updates state of OCCT clipping planes.
206 Standard_EXPORT void UpdateClippingState();
208 //! Reverts state of OCCT clipping planes.
209 Standard_EXPORT void RevertClippingState();
211 //! Pushes current state of OCCT clipping planes to specified program.
212 Standard_EXPORT void PushClippingState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
216 //! Resets state of OCCT material for all programs.
217 Standard_EXPORT void ResetMaterialStates();
219 //! Updates state of OCCT material for specified program.
220 Standard_EXPORT void UpdateMaterialStateTo (const Handle(OpenGl_ShaderProgram)& theProgram,
221 const OpenGl_Element* theAspect);
223 //! Pushes current state of OCCT material to specified program.
224 Standard_EXPORT void PushMaterialState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
226 //! Returns current state of OCCT material for specified program.
227 Standard_EXPORT const OpenGl_MaterialState* MaterialState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
231 //! Returns current state of OCCT surface detail.
232 Standard_EXPORT const OpenGl_SurfaceDetailState& SurfaceDetailState() const;
234 //! Updates state of OCCT surface detail.
235 Standard_EXPORT void UpdateSurfaceDetailStateTo (const Visual3d_TypeOfSurfaceDetail theDetail);
239 //! Pushes current state of OCCT graphics parameters to specified program.
240 Standard_EXPORT void PushState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
244 //! Overwrites context
245 void SetContext (OpenGl_Context* theCtx)
250 //! Sets shading model.
251 Standard_EXPORT void SetShadingModel(const Visual3d_TypeOfModel theModel);
253 //! Sets last view manger used with.
254 //! Helps to handle matrix states in multi-view configurations.
255 void SetLastView (const OpenGl_View* theLastView)
257 myLastView = theLastView;
260 //! Returns true when provided view is the same as cached one.
261 bool IsSameView (const OpenGl_View* theView) const
263 return myLastView == theView;
268 //! Define program bits.
269 Standard_Integer getProgramBits (const Handle(OpenGl_Texture)& theTexture,
270 const Standard_Boolean theHasVertColor,
271 const Standard_Boolean theEnableEnvMap = Standard_False)
274 Standard_Integer aBits = 0;
275 if (myContext->Clipping().IsClippingOrCappingOn())
277 aBits |= OpenGl_PO_ClipPlanes;
279 if (theEnableEnvMap && mySurfaceDetailState.Detail() == Visual3d_TOD_ENVIRONMENT)
281 // Environment map overwrites material texture
282 aBits |= OpenGl_PO_TextureEnv;
284 else if (!theTexture.IsNull())
286 aBits |= theTexture->IsAlpha() ? OpenGl_PO_TextureA : OpenGl_PO_TextureRGB;
290 aBits |= OpenGl_PO_VertColor;
295 //! Prepare standard GLSL program.
296 Handle(OpenGl_ShaderProgram)& getStdProgram (const Standard_Boolean theToLightOn,
297 const Standard_Integer theBits)
299 // If environment map is enabled lighting calculations are
300 // not needed (in accordance with default OCCT behaviour)
301 if (theToLightOn && (theBits & OpenGl_PO_TextureEnv) == 0)
303 Handle(OpenGl_ShaderProgram)& aProgram = myLightPrograms->ChangeValue (theBits);
304 if (aProgram.IsNull())
306 prepareStdProgramLight (aProgram, theBits);
311 Handle(OpenGl_ShaderProgram)& aProgram = myFlatPrograms.ChangeValue (theBits);
312 if (aProgram.IsNull())
314 prepareStdProgramFlat (aProgram, theBits);
319 //! Prepare standard GLSL program for textured font.
320 Standard_EXPORT Standard_Boolean prepareStdProgramFont();
322 //! Prepare standard GLSL program for FBO blit operation.
323 Standard_EXPORT Standard_Boolean prepareStdProgramFboBlit();
325 //! Prepare standard GLSL program without lighting.
326 Standard_EXPORT Standard_Boolean prepareStdProgramFlat (Handle(OpenGl_ShaderProgram)& theProgram,
327 const Standard_Integer theBits);
329 //! Prepare standard GLSL program with lighting.
330 Standard_Boolean prepareStdProgramLight (Handle(OpenGl_ShaderProgram)& theProgram,
331 const Standard_Integer theBits)
333 return myShadingModel == Visual3d_TOM_FRAGMENT
334 ? prepareStdProgramPhong (theProgram, theBits)
335 : prepareStdProgramGouraud (theProgram, theBits);
338 //! Prepare standard GLSL program with per-vertex lighting.
339 Standard_EXPORT Standard_Boolean prepareStdProgramGouraud (Handle(OpenGl_ShaderProgram)& theProgram,
340 const Standard_Integer theBits);
342 //! Prepare standard GLSL program with per-pixel lighting.
343 Standard_EXPORT Standard_Boolean prepareStdProgramPhong (Handle(OpenGl_ShaderProgram)& theProgram,
344 const Standard_Integer theBits);
346 //! Define computeLighting GLSL function depending on current lights configuration
347 //! @param theHasVertColor flag to use getVertColor() instead of Ambient and Diffuse components of active material
348 Standard_EXPORT TCollection_AsciiString stdComputeLighting (const Standard_Boolean theHasVertColor);
350 //! Bind specified program to current context and apply state.
351 Standard_EXPORT Standard_Boolean bindProgramWithState (const Handle(OpenGl_ShaderProgram)& theProgram,
352 const OpenGl_Element* theAspect);
354 //! Set pointer myLightPrograms to active lighting programs set from myMapOfLightPrograms
355 Standard_EXPORT void switchLightPrograms();
359 Visual3d_TypeOfModel myShadingModel; //!< lighting shading model
360 OpenGl_ShaderProgramList myProgramList; //!< The list of shader programs
361 Handle(OpenGl_SetOfShaderPrograms) myLightPrograms; //!< pointer to active lighting programs matrix
362 OpenGl_SetOfShaderPrograms myFlatPrograms; //!< programs matrix without lighting
363 Handle(OpenGl_ShaderProgram) myFontProgram; //!< standard program for textured text
364 Handle(OpenGl_ShaderProgram) myBlitProgram; //!< standard program for FBO blit emulation
365 OpenGl_MapOfShaderPrograms myMapOfLightPrograms; //!< map of lighting programs depending on shading model and lights configuration
367 OpenGl_Context* myContext; //!< OpenGL context
371 OpenGl_MaterialStates myMaterialStates; //!< Per-program state of OCCT material
372 OpenGl_ProjectionState myProjectionState; //!< State of OCCT projection transformation
373 OpenGl_ModelWorldState myModelWorldState; //!< State of OCCT model-world transformation
374 OpenGl_WorldViewState myWorldViewState; //!< State of OCCT world-view transformation
375 OpenGl_ClippingState myClippingState; //!< State of OCCT clipping planes
376 OpenGl_LightSourceState myLightSourceState; //!< State of OCCT light sources
377 OpenGl_SurfaceDetailState mySurfaceDetailState; //!< State of OCCT surface detail
381 const OpenGl_View* myLastView; //!< Pointer to the last view shader manager used with
385 DEFINE_STANDARD_RTTI (OpenGl_ShaderManager)
389 #endif // _OpenGl_ShaderManager_HeaderFile