0025133: TKOpenGl - Crash on closing a view containing presentations with capping
[occt.git] / src / OpenGl / OpenGl_ShaderManager.hxx
1 // Created on: 2013-09-26
2 // Created by: Denis BOGOLEPOV
3 // Copyright (c) 2013-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #ifndef _OpenGl_ShaderManager_HeaderFile
17 #define _OpenGl_ShaderManager_HeaderFile
18
19 #include <Graphic3d_ShaderProgram_Handle.hxx>
20
21 #include <NCollection_DataMap.hxx>
22 #include <NCollection_Sequence.hxx>
23
24 #include <Handle_OpenGl_ShaderManager.hxx>
25 #include <OpenGl_ShaderProgram.hxx>
26 #include <OpenGl_ShaderStates.hxx>
27
28 class OpenGl_View;
29
30 //! List of shader programs.
31 typedef NCollection_Sequence<Handle(OpenGl_ShaderProgram)> OpenGl_ShaderProgramList;
32
33 //! Map to declare per-program states of OCCT materials.
34 typedef NCollection_DataMap<Handle(OpenGl_ShaderProgram), OpenGl_MaterialState> OpenGl_MaterialStates;
35
36 //! This class is responsible for managing shader programs.
37 class OpenGl_ShaderManager : public Standard_Transient
38 {
39   friend class OpenGl_ShaderProgram;
40
41 public:
42
43   //! Creates new empty shader manager.
44   Standard_EXPORT OpenGl_ShaderManager (OpenGl_Context* theContext);
45
46   //! Releases resources of shader manager.
47   Standard_EXPORT virtual ~OpenGl_ShaderManager();
48
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);
56
57   //! Unregisters specified shader program.
58   Standard_EXPORT void Unregister (TCollection_AsciiString&      theShareKey,
59                                    Handle(OpenGl_ShaderProgram)& theProgram);
60
61   //! Returns list of registered shader programs.
62   Standard_EXPORT const OpenGl_ShaderProgramList& ShaderPrograms() const;
63
64   //! Returns true if no program objects are registered in the manager.
65   Standard_EXPORT Standard_Boolean IsEmpty() const;
66
67   DEFINE_STANDARD_RTTI (OpenGl_ShaderManager)
68
69 protected:
70
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
77
78 public:
79
80   //! Returns current state of OCCT light sources.
81   Standard_EXPORT const OpenGl_LightSourceState& LightSourceState() const;
82
83   //! Updates state of OCCT light sources.
84   Standard_EXPORT void UpdateLightSourceStateTo (const OpenGl_ListOfLight* theLights);
85
86   //! Pushes current state of OCCT light sources to specified program.
87   Standard_EXPORT void PushLightSourceState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
88
89 public:
90
91   //! Returns current state of OCCT projection transform.
92   Standard_EXPORT const OpenGl_ProjectionState& ProjectionState() const;
93
94   //! Updates state of OCCT projection transform.
95   Standard_EXPORT void UpdateProjectionStateTo (const Tmatrix3* theProjectionMatrix);
96
97   //! Reverts state of OCCT projection transform.
98   Standard_EXPORT void RevertProjectionStateTo (const Tmatrix3* theProjectionMatrix);
99
100   //! Pushes current state of OCCT projection transform to specified program.
101   Standard_EXPORT void PushProjectionState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
102
103 public:
104
105   //! Returns current state of OCCT model-world transform.
106   Standard_EXPORT const OpenGl_ModelWorldState& ModelWorldState() const;
107
108   //! Updates state of OCCT model-world transform.
109   Standard_EXPORT void UpdateModelWorldStateTo (const Tmatrix3* theModelWorldMatrix);
110
111   //! Reverts state of OCCT model-world transform.
112   Standard_EXPORT void RevertModelWorldStateTo (const Tmatrix3* theModelWorldMatrix);
113
114   //! Pushes current state of OCCT model-world transform to specified program.
115   Standard_EXPORT void PushModelWorldState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
116
117 public:
118
119   //! Returns current state of OCCT world-view transform.
120   Standard_EXPORT const OpenGl_WorldViewState& WorldViewState() const;
121
122   //! Updates state of OCCT world-view transform.
123   Standard_EXPORT void UpdateWorldViewStateTo (const Tmatrix3* theWorldViewMatrix);
124
125   //! Reverts state of OCCT world-view transform.
126   Standard_EXPORT void RevertWorldViewStateTo (const Tmatrix3* theWorldViewMatrix);
127
128   //! Pushes current state of OCCT world-view transform to specified program.
129   Standard_EXPORT void PushWorldViewState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
130
131 public:
132
133   //! Updates state of OCCT clipping planes.
134   Standard_EXPORT void UpdateClippingState();
135
136   //! Reverts state of OCCT clipping planes.
137   Standard_EXPORT void RevertClippingState();
138
139   //! Pushes current state of OCCT clipping planes to specified program.
140   Standard_EXPORT void PushClippingState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
141
142 public:
143
144   //! Resets state of OCCT material for all programs.
145   Standard_EXPORT void ResetMaterialStates();
146
147   //! Updates state of OCCT material for specified program.
148   Standard_EXPORT void UpdateMaterialStateTo (const Handle(OpenGl_ShaderProgram)& theProgram,
149                                               const OpenGl_Element*               theAspect);
150
151   //! Pushes current state of OCCT material to specified program.
152   Standard_EXPORT void PushMaterialState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
153
154   //! Returns current state of OCCT material for specified program.
155   Standard_EXPORT const OpenGl_MaterialState* MaterialState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
156
157 public:
158   
159   //! Pushes current state of OCCT graphics parameters to specified program.
160   Standard_EXPORT void PushState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
161
162 public:
163
164   //! Overwrites context
165   void SetContext (OpenGl_Context* theCtx)
166   {
167     myContext = theCtx;
168   }
169
170   //! Sets last view manger used with.
171   //! Helps to handle matrix states in multi-view configurations.
172   void SetLastView (const OpenGl_View* theLastView)
173   {
174     myLastView = theLastView;
175   }
176
177   //! Returns true when provided view is the same as cached one.
178   bool IsSameView (const OpenGl_View* theView) const
179   {
180     return myLastView == theView;
181   }
182
183 protected:
184
185   OpenGl_ShaderProgramList myProgramList;  //!< The list of shader programs
186   OpenGl_Context*          myContext;      //!< The OpenGL context
187
188 private:
189
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.
192 };
193
194 #endif // _OpenGl_ShaderManager_HeaderFile