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_Handle.hxx>
21 #include <NCollection_DataMap.hxx>
22 #include <NCollection_Sequence.hxx>
24 #include <Handle_OpenGl_ShaderManager.hxx>
25 #include <OpenGl_ShaderProgram.hxx>
26 #include <OpenGl_ShaderStates.hxx>
30 //! List of shader programs.
31 typedef NCollection_Sequence<Handle(OpenGl_ShaderProgram)> OpenGl_ShaderProgramList;
33 //! Map to declare per-program states of OCCT materials.
34 typedef NCollection_DataMap<Handle(OpenGl_ShaderProgram), OpenGl_MaterialState> OpenGl_MaterialStates;
36 //! This class is responsible for managing shader programs.
37 class OpenGl_ShaderManager : public Standard_Transient
39 friend class OpenGl_ShaderProgram;
43 //! Creates new empty shader manager.
44 Standard_EXPORT OpenGl_ShaderManager (OpenGl_Context* theContext);
46 //! Releases resources of shader manager.
47 Standard_EXPORT virtual ~OpenGl_ShaderManager();
49 //! Creates new shader program or re-use shared instance.
50 //! @param theProxy [IN] program definition
51 //! @param theShareKey [OUT] sharing key
52 //! @param theProgram [OUT] OpenGL program
53 Standard_EXPORT void Create (const Handle(Graphic3d_ShaderProgram)& theProxy,
54 TCollection_AsciiString& theShareKey,
55 Handle(OpenGl_ShaderProgram)& theProgram);
57 //! Unregisters specified shader program.
58 Standard_EXPORT void Unregister (TCollection_AsciiString& theShareKey,
59 Handle(OpenGl_ShaderProgram)& theProgram);
61 //! Returns list of registered shader programs.
62 Standard_EXPORT const OpenGl_ShaderProgramList& ShaderPrograms() const;
64 //! Returns true if no program objects are registered in the manager.
65 Standard_EXPORT Standard_Boolean IsEmpty() const;
67 DEFINE_STANDARD_RTTI (OpenGl_ShaderManager)
71 OpenGl_MaterialStates myMaterialStates; //!< Per-program state of OCCT material
72 OpenGl_ProjectionState myProjectionState; //!< State of OCCT projection transformation
73 OpenGl_ModelWorldState myModelWorldState; //!< State of OCCT model-world transformation
74 OpenGl_WorldViewState myWorldViewState; //!< State of OCCT world-view transformation
75 OpenGl_LightSourceState myClippingState; //!< State of OCCT clipping planes
76 OpenGl_LightSourceState myLightSourceState; //!< State of OCCT light sources
80 //! Returns current state of OCCT light sources.
81 Standard_EXPORT const OpenGl_LightSourceState& LightSourceState() const;
83 //! Updates state of OCCT light sources.
84 Standard_EXPORT void UpdateLightSourceStateTo (const OpenGl_ListOfLight* theLights);
86 //! Pushes current state of OCCT light sources to specified program.
87 Standard_EXPORT void PushLightSourceState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
91 //! Returns current state of OCCT projection transform.
92 Standard_EXPORT const OpenGl_ProjectionState& ProjectionState() const;
94 //! Updates state of OCCT projection transform.
95 Standard_EXPORT void UpdateProjectionStateTo (const Tmatrix3* theProjectionMatrix);
97 //! Reverts state of OCCT projection transform.
98 Standard_EXPORT void RevertProjectionStateTo (const Tmatrix3* theProjectionMatrix);
100 //! Pushes current state of OCCT projection transform to specified program.
101 Standard_EXPORT void PushProjectionState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
105 //! Returns current state of OCCT model-world transform.
106 Standard_EXPORT const OpenGl_ModelWorldState& ModelWorldState() const;
108 //! Updates state of OCCT model-world transform.
109 Standard_EXPORT void UpdateModelWorldStateTo (const Tmatrix3* theModelWorldMatrix);
111 //! Reverts state of OCCT model-world transform.
112 Standard_EXPORT void RevertModelWorldStateTo (const Tmatrix3* theModelWorldMatrix);
114 //! Pushes current state of OCCT model-world transform to specified program.
115 Standard_EXPORT void PushModelWorldState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
119 //! Returns current state of OCCT world-view transform.
120 Standard_EXPORT const OpenGl_WorldViewState& WorldViewState() const;
122 //! Updates state of OCCT world-view transform.
123 Standard_EXPORT void UpdateWorldViewStateTo (const Tmatrix3* theWorldViewMatrix);
125 //! Reverts state of OCCT world-view transform.
126 Standard_EXPORT void RevertWorldViewStateTo (const Tmatrix3* theWorldViewMatrix);
128 //! Pushes current state of OCCT world-view transform to specified program.
129 Standard_EXPORT void PushWorldViewState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
133 //! Updates state of OCCT clipping planes.
134 Standard_EXPORT void UpdateClippingState();
136 //! Reverts state of OCCT clipping planes.
137 Standard_EXPORT void RevertClippingState();
139 //! Pushes current state of OCCT clipping planes to specified program.
140 Standard_EXPORT void PushClippingState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
144 //! Resets state of OCCT material for all programs.
145 Standard_EXPORT void ResetMaterialStates();
147 //! Updates state of OCCT material for specified program.
148 Standard_EXPORT void UpdateMaterialStateTo (const Handle(OpenGl_ShaderProgram)& theProgram,
149 const OpenGl_Element* theAspect);
151 //! Pushes current state of OCCT material to specified program.
152 Standard_EXPORT void PushMaterialState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
154 //! Returns current state of OCCT material for specified program.
155 Standard_EXPORT const OpenGl_MaterialState* MaterialState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
159 //! Pushes current state of OCCT graphics parameters to specified program.
160 Standard_EXPORT void PushState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
164 //! Overwrites context
165 void SetContext (OpenGl_Context* theCtx)
170 //! Sets last view manger used with.
171 //! Helps to handle matrix states in multi-view configurations.
172 void SetLastView (const OpenGl_View* theLastView)
174 myLastView = theLastView;
177 //! Returns true when provided view is the same as cached one.
178 bool IsSameView (const OpenGl_View* theView) const
180 return myLastView == theView;
185 OpenGl_ShaderProgramList myProgramList; //!< The list of shader programs
186 OpenGl_Context* myContext; //!< The OpenGL context
190 Standard_Boolean myIsPP; //!< Is any program object bound (programmable pipeline)?
191 const OpenGl_View* myLastView; //!< Pointer to the last view shader manager used with.
194 #endif // _OpenGl_ShaderManager_HeaderFile