30f0ad28 |
1 | // Created on: 2013-09-26 |
2 | // Created by: Denis BOGOLEPOV |
d5f74e42 |
3 | // Copyright (c) 2013-2014 OPEN CASCADE SAS |
30f0ad28 |
4 | // |
973c2be1 |
5 | // This file is part of Open CASCADE Technology software library. |
30f0ad28 |
6 | // |
d5f74e42 |
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 |
973c2be1 |
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. |
30f0ad28 |
12 | // |
973c2be1 |
13 | // Alternatively, this file may be used under the terms of Open CASCADE |
14 | // commercial license or contractual agreement. |
30f0ad28 |
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> |
256f9ac0 |
25 | #include <OpenGl_SetOfShaderPrograms.hxx> |
30f0ad28 |
26 | #include <OpenGl_ShaderStates.hxx> |
8625ef7e |
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> |
30f0ad28 |
33 | |
e6804ff7 |
34 | class OpenGl_View; |
35 | |
30f0ad28 |
36 | //! List of shader programs. |
37 | typedef NCollection_Sequence<Handle(OpenGl_ShaderProgram)> OpenGl_ShaderProgramList; |
38 | |
39 | //! Map to declare per-program states of OCCT materials. |
40 | typedef NCollection_DataMap<Handle(OpenGl_ShaderProgram), OpenGl_MaterialState> OpenGl_MaterialStates; |
41 | |
42 | //! This class is responsible for managing shader programs. |
43 | class OpenGl_ShaderManager : public Standard_Transient |
44 | { |
45 | friend class OpenGl_ShaderProgram; |
46 | |
47 | public: |
48 | |
49 | //! Creates new empty shader manager. |
50 | Standard_EXPORT OpenGl_ShaderManager (OpenGl_Context* theContext); |
51 | |
52 | //! Releases resources of shader manager. |
53 | Standard_EXPORT virtual ~OpenGl_ShaderManager(); |
54 | |
05e2200b |
55 | //! Release all resources. |
56 | Standard_EXPORT void clear(); |
57 | |
392ac980 |
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 |
8625ef7e |
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); |
30f0ad28 |
66 | |
67 | //! Unregisters specified shader program. |
392ac980 |
68 | Standard_EXPORT void Unregister (TCollection_AsciiString& theShareKey, |
69 | Handle(OpenGl_ShaderProgram)& theProgram); |
30f0ad28 |
70 | |
71 | //! Returns list of registered shader programs. |
72 | Standard_EXPORT const OpenGl_ShaderProgramList& ShaderPrograms() const; |
73 | |
74 | //! Returns true if no program objects are registered in the manager. |
75 | Standard_EXPORT Standard_Boolean IsEmpty() const; |
76 | |
8625ef7e |
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) |
83 | { |
84 | if (!theCustomProgram.IsNull() |
85 | || myContext->caps->ffpEnable) |
86 | { |
87 | return bindProgramWithState (theCustomProgram, theAspect); |
88 | } |
89 | |
90 | const Standard_Integer aBits = getProgramBits (theTexture, theHasVertColor); |
91 | Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits); |
92 | return bindProgramWithState (aProgram, theAspect); |
93 | } |
30f0ad28 |
94 | |
8625ef7e |
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) |
101 | { |
102 | if (!theCustomProgram.IsNull() |
103 | || myContext->caps->ffpEnable) |
104 | { |
105 | return bindProgramWithState (theCustomProgram, theAspect); |
106 | } |
107 | |
108 | const Standard_Integer aBits = getProgramBits (theTexture, theHasVertColor); |
109 | Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits); |
110 | return bindProgramWithState (aProgram, theAspect); |
111 | } |
30f0ad28 |
112 | |
8625ef7e |
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) |
119 | { |
120 | if (!theCustomProgram.IsNull() |
121 | || myContext->caps->ffpEnable) |
122 | { |
123 | return bindProgramWithState (theCustomProgram, theAspect); |
124 | } |
125 | |
126 | const Standard_Integer aBits = getProgramBits (theTexture, theHasVertColor) | OpenGl_PO_Point; |
127 | Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits); |
128 | return bindProgramWithState (aProgram, theAspect); |
129 | } |
130 | |
131 | //! Bind program for rendering alpha-textured font. |
132 | Standard_Boolean BindProgram (const OpenGl_AspectText* theAspect, |
133 | const Handle(OpenGl_ShaderProgram)& theCustomProgram) |
134 | { |
135 | if (!theCustomProgram.IsNull() |
136 | || myContext->caps->ffpEnable) |
137 | { |
138 | return bindProgramWithState (theCustomProgram, theAspect); |
139 | } |
140 | |
141 | if (myFontProgram.IsNull()) |
142 | { |
143 | prepareStdProgramFont(); |
144 | } |
145 | return bindProgramWithState (myFontProgram, theAspect); |
146 | } |
30f0ad28 |
147 | |
148 | public: |
149 | |
150 | //! Returns current state of OCCT light sources. |
151 | Standard_EXPORT const OpenGl_LightSourceState& LightSourceState() const; |
152 | |
153 | //! Updates state of OCCT light sources. |
154 | Standard_EXPORT void UpdateLightSourceStateTo (const OpenGl_ListOfLight* theLights); |
155 | |
156 | //! Pushes current state of OCCT light sources to specified program. |
157 | Standard_EXPORT void PushLightSourceState (const Handle(OpenGl_ShaderProgram)& theProgram) const; |
158 | |
159 | public: |
160 | |
161 | //! Returns current state of OCCT projection transform. |
162 | Standard_EXPORT const OpenGl_ProjectionState& ProjectionState() const; |
163 | |
164 | //! Updates state of OCCT projection transform. |
c827ea3a |
165 | Standard_EXPORT void UpdateProjectionStateTo (const OpenGl_Mat4& theProjectionMatrix); |
30f0ad28 |
166 | |
167 | //! Reverts state of OCCT projection transform. |
c827ea3a |
168 | Standard_EXPORT void RevertProjectionStateTo (const OpenGl_Mat4& theProjectionMatrix); |
30f0ad28 |
169 | |
170 | //! Pushes current state of OCCT projection transform to specified program. |
171 | Standard_EXPORT void PushProjectionState (const Handle(OpenGl_ShaderProgram)& theProgram) const; |
172 | |
173 | public: |
174 | |
175 | //! Returns current state of OCCT model-world transform. |
176 | Standard_EXPORT const OpenGl_ModelWorldState& ModelWorldState() const; |
177 | |
178 | //! Updates state of OCCT model-world transform. |
c827ea3a |
179 | Standard_EXPORT void UpdateModelWorldStateTo (const OpenGl_Mat4& theModelWorldMatrix); |
30f0ad28 |
180 | |
181 | //! Reverts state of OCCT model-world transform. |
c827ea3a |
182 | Standard_EXPORT void RevertModelWorldStateTo (const OpenGl_Mat4& theModelWorldMatrix); |
30f0ad28 |
183 | |
184 | //! Pushes current state of OCCT model-world transform to specified program. |
185 | Standard_EXPORT void PushModelWorldState (const Handle(OpenGl_ShaderProgram)& theProgram) const; |
186 | |
187 | public: |
188 | |
189 | //! Returns current state of OCCT world-view transform. |
190 | Standard_EXPORT const OpenGl_WorldViewState& WorldViewState() const; |
191 | |
192 | //! Updates state of OCCT world-view transform. |
c827ea3a |
193 | Standard_EXPORT void UpdateWorldViewStateTo (const OpenGl_Mat4& theWorldViewMatrix); |
30f0ad28 |
194 | |
195 | //! Reverts state of OCCT world-view transform. |
c827ea3a |
196 | Standard_EXPORT void RevertWorldViewStateTo (const OpenGl_Mat4& theWorldViewMatrix); |
30f0ad28 |
197 | |
198 | //! Pushes current state of OCCT world-view transform to specified program. |
199 | Standard_EXPORT void PushWorldViewState (const Handle(OpenGl_ShaderProgram)& theProgram) const; |
200 | |
201 | public: |
202 | |
203 | //! Updates state of OCCT clipping planes. |
204 | Standard_EXPORT void UpdateClippingState(); |
205 | |
206 | //! Reverts state of OCCT clipping planes. |
207 | Standard_EXPORT void RevertClippingState(); |
208 | |
209 | //! Pushes current state of OCCT clipping planes to specified program. |
210 | Standard_EXPORT void PushClippingState (const Handle(OpenGl_ShaderProgram)& theProgram) const; |
211 | |
212 | public: |
213 | |
214 | //! Resets state of OCCT material for all programs. |
215 | Standard_EXPORT void ResetMaterialStates(); |
216 | |
217 | //! Updates state of OCCT material for specified program. |
218 | Standard_EXPORT void UpdateMaterialStateTo (const Handle(OpenGl_ShaderProgram)& theProgram, |
219 | const OpenGl_Element* theAspect); |
220 | |
221 | //! Pushes current state of OCCT material to specified program. |
222 | Standard_EXPORT void PushMaterialState (const Handle(OpenGl_ShaderProgram)& theProgram) const; |
223 | |
224 | //! Returns current state of OCCT material for specified program. |
225 | Standard_EXPORT const OpenGl_MaterialState* MaterialState (const Handle(OpenGl_ShaderProgram)& theProgram) const; |
226 | |
227 | public: |
05e2200b |
228 | |
30f0ad28 |
229 | //! Pushes current state of OCCT graphics parameters to specified program. |
230 | Standard_EXPORT void PushState (const Handle(OpenGl_ShaderProgram)& theProgram) const; |
231 | |
392ac980 |
232 | public: |
233 | |
234 | //! Overwrites context |
235 | void SetContext (OpenGl_Context* theCtx) |
236 | { |
237 | myContext = theCtx; |
238 | } |
239 | |
8625ef7e |
240 | //! Sets shading model. |
256f9ac0 |
241 | Standard_EXPORT void SetShadingModel(const Visual3d_TypeOfModel theModel); |
8625ef7e |
242 | |
e6804ff7 |
243 | //! Sets last view manger used with. |
244 | //! Helps to handle matrix states in multi-view configurations. |
245 | void SetLastView (const OpenGl_View* theLastView) |
246 | { |
247 | myLastView = theLastView; |
248 | } |
249 | |
250 | //! Returns true when provided view is the same as cached one. |
251 | bool IsSameView (const OpenGl_View* theView) const |
252 | { |
253 | return myLastView == theView; |
254 | } |
255 | |
30f0ad28 |
256 | protected: |
257 | |
8625ef7e |
258 | //! Define program bits. |
259 | Standard_Integer getProgramBits (const Handle(OpenGl_Texture)& theTexture, |
260 | const Standard_Boolean theHasVertColor) |
261 | { |
262 | Standard_Integer aBits = 0; |
263 | if (myContext->Clipping().IsClippingOrCappingOn()) |
264 | { |
265 | aBits |= OpenGl_PO_ClipPlanes; |
266 | } |
267 | if (!theTexture.IsNull()) |
268 | { |
269 | // GL_RED to be handled |
270 | aBits |= theTexture->GetFormat() == GL_ALPHA ? OpenGl_PO_TextureA : OpenGl_PO_TextureRGB; |
271 | } |
272 | if (theHasVertColor) |
273 | { |
274 | aBits |= OpenGl_PO_VertColor; |
275 | } |
276 | return aBits; |
277 | } |
278 | |
279 | //! Prepare standard GLSL program. |
280 | Handle(OpenGl_ShaderProgram)& getStdProgram (const Standard_Boolean theToLightOn, |
281 | const Standard_Integer theBits) |
282 | { |
283 | if (theToLightOn) |
284 | { |
256f9ac0 |
285 | Handle(OpenGl_ShaderProgram)& aProgram = myLightPrograms->ChangeValue (theBits); |
8625ef7e |
286 | if (aProgram.IsNull()) |
287 | { |
288 | prepareStdProgramLight (aProgram, theBits); |
289 | } |
290 | return aProgram; |
291 | } |
292 | |
256f9ac0 |
293 | Handle(OpenGl_ShaderProgram)& aProgram = myFlatPrograms.ChangeValue (theBits); |
8625ef7e |
294 | if (aProgram.IsNull()) |
295 | { |
296 | prepareStdProgramFlat (aProgram, theBits); |
297 | } |
298 | return aProgram; |
299 | } |
300 | |
301 | //! Prepare standard GLSL program for textured font. |
302 | Standard_EXPORT Standard_Boolean prepareStdProgramFont(); |
303 | |
304 | //! Prepare standard GLSL program without lighting. |
305 | Standard_EXPORT Standard_Boolean prepareStdProgramFlat (Handle(OpenGl_ShaderProgram)& theProgram, |
306 | const Standard_Integer theBits); |
307 | |
308 | //! Prepare standard GLSL program with lighting. |
309 | Standard_Boolean prepareStdProgramLight (Handle(OpenGl_ShaderProgram)& theProgram, |
310 | const Standard_Integer theBits) |
311 | { |
312 | return myShadingModel == Visual3d_TOM_FRAGMENT |
313 | ? prepareStdProgramPhong (theProgram, theBits) |
314 | : prepareStdProgramGouraud (theProgram, theBits); |
315 | } |
316 | |
317 | //! Prepare standard GLSL program with per-vertex lighting. |
318 | Standard_EXPORT Standard_Boolean prepareStdProgramGouraud (Handle(OpenGl_ShaderProgram)& theProgram, |
319 | const Standard_Integer theBits); |
320 | |
321 | //! Prepare standard GLSL program with per-pixel lighting. |
322 | Standard_EXPORT Standard_Boolean prepareStdProgramPhong (Handle(OpenGl_ShaderProgram)& theProgram, |
323 | const Standard_Integer theBits); |
324 | |
256f9ac0 |
325 | //! Define computeLighting GLSL function depending on current lights configuration |
326 | Standard_EXPORT TCollection_AsciiString stdComputeLighting(); |
327 | |
8625ef7e |
328 | //! Bind specified program to current context and apply state. |
329 | Standard_EXPORT Standard_Boolean bindProgramWithState (const Handle(OpenGl_ShaderProgram)& theProgram, |
330 | const OpenGl_Element* theAspect); |
331 | |
256f9ac0 |
332 | //! Set pointer myLightPrograms to active lighting programs set from myMapOfLightPrograms |
333 | Standard_EXPORT void switchLightPrograms(); |
334 | |
8625ef7e |
335 | protected: |
336 | |
256f9ac0 |
337 | Visual3d_TypeOfModel myShadingModel; //!< lighting shading model |
338 | OpenGl_ShaderProgramList myProgramList; //!< The list of shader programs |
339 | Handle(OpenGl_SetOfShaderPrograms) myLightPrograms; //!< pointer to active lighting programs matrix |
340 | OpenGl_SetOfShaderPrograms myFlatPrograms; //!< programs matrix without lighting |
341 | Handle(OpenGl_ShaderProgram) myFontProgram; //!< standard program for textured text |
342 | OpenGl_MapOfShaderPrograms myMapOfLightPrograms; //!< map of lighting programs depending on shading model and lights configuration |
8625ef7e |
343 | |
256f9ac0 |
344 | OpenGl_Context* myContext; //!< OpenGL context |
8625ef7e |
345 | |
346 | protected: |
347 | |
256f9ac0 |
348 | OpenGl_MaterialStates myMaterialStates; //!< Per-program state of OCCT material |
349 | OpenGl_ProjectionState myProjectionState; //!< State of OCCT projection transformation |
350 | OpenGl_ModelWorldState myModelWorldState; //!< State of OCCT model-world transformation |
351 | OpenGl_WorldViewState myWorldViewState; //!< State of OCCT world-view transformation |
352 | OpenGl_LightSourceState myClippingState; //!< State of OCCT clipping planes |
353 | OpenGl_LightSourceState myLightSourceState; //!< State of OCCT light sources |
30f0ad28 |
354 | |
355 | private: |
356 | |
256f9ac0 |
357 | const OpenGl_View* myLastView; //!< Pointer to the last view shader manager used with |
8625ef7e |
358 | |
359 | public: |
360 | |
361 | DEFINE_STANDARD_RTTI (OpenGl_ShaderManager) |
362 | |
30f0ad28 |
363 | }; |
364 | |
365 | #endif // _OpenGl_ShaderManager_HeaderFile |