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>
22 #include <Graphic3d_TypeOfSurfaceDetail.hxx>
24 #include <NCollection_DataMap.hxx>
25 #include <NCollection_Sequence.hxx>
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>
38 //! List of shader programs.
39 typedef NCollection_Sequence<Handle(OpenGl_ShaderProgram)> OpenGl_ShaderProgramList;
41 //! Map to declare per-program states of OCCT materials.
42 typedef NCollection_DataMap<Handle(OpenGl_ShaderProgram), OpenGl_MaterialState> OpenGl_MaterialStates;
44 class OpenGl_ShaderManager;
45 DEFINE_STANDARD_HANDLE(OpenGl_ShaderManager, Standard_Transient)
47 //! This class is responsible for managing shader programs.
48 class OpenGl_ShaderManager : public Standard_Transient
50 friend class OpenGl_ShaderProgram;
54 //! Creates new empty shader manager.
55 Standard_EXPORT OpenGl_ShaderManager (OpenGl_Context* theContext);
57 //! Releases resources of shader manager.
58 Standard_EXPORT virtual ~OpenGl_ShaderManager();
60 //! Release all resources.
61 Standard_EXPORT void clear();
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);
72 //! Unregisters specified shader program.
73 Standard_EXPORT void Unregister (TCollection_AsciiString& theShareKey,
74 Handle(OpenGl_ShaderProgram)& theProgram);
76 //! Returns list of registered shader programs.
77 Standard_EXPORT const OpenGl_ShaderProgramList& ShaderPrograms() const;
79 //! Returns true if no program objects are registered in the manager.
80 Standard_EXPORT Standard_Boolean IsEmpty() const;
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)
89 if (!theCustomProgram.IsNull()
90 || myContext->caps->ffpEnable)
92 return bindProgramWithState (theCustomProgram, theAspect);
95 const Standard_Integer aBits = getProgramBits (theTexture, theHasVertColor, Standard_True);
96 Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits);
97 return bindProgramWithState (aProgram, theAspect);
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)
107 if (!theCustomProgram.IsNull()
108 || myContext->caps->ffpEnable)
110 return bindProgramWithState (theCustomProgram, theAspect);
113 Standard_Integer aBits = getProgramBits (theTexture, theHasVertColor);
114 if (theAspect->Type() != Aspect_TOL_SOLID)
116 aBits |= OpenGl_PO_StippleLine;
119 Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits);
120 return bindProgramWithState (aProgram, theAspect);
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)
130 if (!theCustomProgram.IsNull()
131 || myContext->caps->ffpEnable)
133 return bindProgramWithState (theCustomProgram, theAspect);
136 const Standard_Integer aBits = getProgramBits (theTexture, theHasVertColor) | OpenGl_PO_Point;
137 Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits);
138 return bindProgramWithState (aProgram, theAspect);
141 //! Bind program for rendering alpha-textured font.
142 Standard_Boolean BindProgram (const OpenGl_AspectText* theAspect,
143 const Handle(OpenGl_ShaderProgram)& theCustomProgram)
145 if (!theCustomProgram.IsNull()
146 || myContext->caps->ffpEnable)
148 return bindProgramWithState (theCustomProgram, theAspect);
151 if (myFontProgram.IsNull())
153 prepareStdProgramFont();
155 return bindProgramWithState (myFontProgram, theAspect);
158 //! Bind program for FBO blit operation.
159 Standard_Boolean BindFboBlitProgram()
161 if (myBlitProgram.IsNull())
163 prepareStdProgramFboBlit();
165 return !myBlitProgram.IsNull()
166 && myContext->BindProgram (myBlitProgram);
169 //! Bind program for rendering stereoscopic image.
170 Standard_Boolean BindStereoProgram (const Graphic3d_StereoMode theStereoMode)
172 if (theStereoMode < 0 || theStereoMode >= Graphic3d_StereoMode_NB)
174 return Standard_False;
177 if (myStereoPrograms[theStereoMode].IsNull())
179 prepareStdProgramStereo (myStereoPrograms[theStereoMode], theStereoMode);
181 const Handle(OpenGl_ShaderProgram)& aProgram = myStereoPrograms[theStereoMode];
182 return !aProgram.IsNull()
183 && myContext->BindProgram (aProgram);
188 //! Returns current state of OCCT light sources.
189 Standard_EXPORT const OpenGl_LightSourceState& LightSourceState() const;
191 //! Updates state of OCCT light sources.
192 Standard_EXPORT void UpdateLightSourceStateTo (const OpenGl_ListOfLight* theLights);
194 //! Pushes current state of OCCT light sources to specified program.
195 Standard_EXPORT void PushLightSourceState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
199 //! Returns current state of OCCT projection transform.
200 Standard_EXPORT const OpenGl_ProjectionState& ProjectionState() const;
202 //! Updates state of OCCT projection transform.
203 Standard_EXPORT void UpdateProjectionStateTo (const OpenGl_Mat4& theProjectionMatrix);
205 //! Pushes current state of OCCT projection transform to specified program.
206 Standard_EXPORT void PushProjectionState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
210 //! Returns current state of OCCT model-world transform.
211 Standard_EXPORT const OpenGl_ModelWorldState& ModelWorldState() const;
213 //! Updates state of OCCT model-world transform.
214 Standard_EXPORT void UpdateModelWorldStateTo (const OpenGl_Mat4& theModelWorldMatrix);
216 //! Pushes current state of OCCT model-world transform to specified program.
217 Standard_EXPORT void PushModelWorldState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
221 //! Returns current state of OCCT world-view transform.
222 Standard_EXPORT const OpenGl_WorldViewState& WorldViewState() const;
224 //! Updates state of OCCT world-view transform.
225 Standard_EXPORT void UpdateWorldViewStateTo (const OpenGl_Mat4& theWorldViewMatrix);
227 //! Pushes current state of OCCT world-view transform to specified program.
228 Standard_EXPORT void PushWorldViewState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
232 //! Updates state of OCCT clipping planes.
233 Standard_EXPORT void UpdateClippingState();
235 //! Reverts state of OCCT clipping planes.
236 Standard_EXPORT void RevertClippingState();
238 //! Pushes current state of OCCT clipping planes to specified program.
239 Standard_EXPORT void PushClippingState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
243 //! Resets state of OCCT material for all programs.
244 Standard_EXPORT void ResetMaterialStates();
246 //! Updates state of OCCT material for specified program.
247 Standard_EXPORT void UpdateMaterialStateTo (const Handle(OpenGl_ShaderProgram)& theProgram,
248 const OpenGl_Element* theAspect);
250 //! Pushes current state of OCCT material to specified program.
251 Standard_EXPORT void PushMaterialState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
253 //! Returns current state of OCCT material for specified program.
254 Standard_EXPORT const OpenGl_MaterialState* MaterialState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
258 //! Returns current state of OCCT surface detail.
259 Standard_EXPORT const OpenGl_SurfaceDetailState& SurfaceDetailState() const;
261 //! Updates state of OCCT surface detail.
262 Standard_EXPORT void UpdateSurfaceDetailStateTo (const Graphic3d_TypeOfSurfaceDetail theDetail);
266 //! Pushes current state of OCCT graphics parameters to specified program.
267 Standard_EXPORT void PushState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
271 //! Overwrites context
272 void SetContext (OpenGl_Context* theCtx)
277 //! Returns true when provided context is the same as used one by shader manager.
278 bool IsSameContext (OpenGl_Context* theCtx) const
280 return myContext == theCtx;
283 //! Sets shading model.
284 Standard_EXPORT void SetShadingModel (const Graphic3d_TypeOfShadingModel theModel);
286 //! Sets last view manger used with.
287 //! Helps to handle matrix states in multi-view configurations.
288 void SetLastView (const OpenGl_View* theLastView)
290 myLastView = theLastView;
293 //! Returns true when provided view is the same as cached one.
294 bool IsSameView (const OpenGl_View* theView) const
296 return myLastView == theView;
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)
307 Standard_Integer aBits = 0;
308 if (myContext->Clipping().IsClippingOrCappingOn())
310 aBits |= OpenGl_PO_ClipPlanes;
312 if (theEnableEnvMap && mySurfaceDetailState.Detail() == Graphic3d_TOD_ENVIRONMENT)
314 // Environment map overwrites material texture
315 aBits |= OpenGl_PO_TextureEnv;
317 else if (!theTexture.IsNull())
319 aBits |= theTexture->IsAlpha() ? OpenGl_PO_TextureA : OpenGl_PO_TextureRGB;
323 aBits |= OpenGl_PO_VertColor;
328 //! Prepare standard GLSL program.
329 Handle(OpenGl_ShaderProgram)& getStdProgram (const Standard_Boolean theToLightOn,
330 const Standard_Integer theBits)
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)
336 Handle(OpenGl_ShaderProgram)& aProgram = myLightPrograms->ChangeValue (theBits);
337 if (aProgram.IsNull())
339 prepareStdProgramLight (aProgram, theBits);
344 Handle(OpenGl_ShaderProgram)& aProgram = myFlatPrograms.ChangeValue (theBits);
345 if (aProgram.IsNull())
347 prepareStdProgramFlat (aProgram, theBits);
352 //! Prepare standard GLSL program for textured font.
353 Standard_EXPORT Standard_Boolean prepareStdProgramFont();
355 //! Prepare standard GLSL program for FBO blit operation.
356 Standard_EXPORT Standard_Boolean prepareStdProgramFboBlit();
358 //! Prepare standard GLSL program without lighting.
359 Standard_EXPORT Standard_Boolean prepareStdProgramFlat (Handle(OpenGl_ShaderProgram)& theProgram,
360 const Standard_Integer theBits);
362 //! Prepare standard GLSL program with lighting.
363 Standard_Boolean prepareStdProgramLight (Handle(OpenGl_ShaderProgram)& theProgram,
364 const Standard_Integer theBits)
366 return myShadingModel == Graphic3d_TOSM_FRAGMENT
367 ? prepareStdProgramPhong (theProgram, theBits)
368 : prepareStdProgramGouraud (theProgram, theBits);
371 //! Prepare standard GLSL program with per-vertex lighting.
372 Standard_EXPORT Standard_Boolean prepareStdProgramGouraud (Handle(OpenGl_ShaderProgram)& theProgram,
373 const Standard_Integer theBits);
375 //! Prepare standard GLSL program with per-pixel lighting.
376 Standard_EXPORT Standard_Boolean prepareStdProgramPhong (Handle(OpenGl_ShaderProgram)& theProgram,
377 const Standard_Integer theBits);
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);
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);
387 //! Set pointer myLightPrograms to active lighting programs set from myMapOfLightPrograms
388 Standard_EXPORT void switchLightPrograms();
390 //! Prepare standard GLSL program for stereoscopic image.
391 Standard_EXPORT Standard_Boolean prepareStdProgramStereo (Handle(OpenGl_ShaderProgram)& theProgram,
392 const Graphic3d_StereoMode theStereoMode);
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
404 Handle(OpenGl_ShaderProgram) myStereoPrograms[Graphic3d_StereoMode_NB]; //!< standard stereo programs
406 OpenGl_Context* myContext; //!< OpenGL context
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
420 const OpenGl_View* myLastView; //!< Pointer to the last view shader manager used with
424 DEFINE_STANDARD_RTTIEXT(OpenGl_ShaderManager,Standard_Transient)
428 #endif // _OpenGl_ShaderManager_HeaderFile