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 | |
494782f6 |
19 | #include <Graphic3d_ShaderProgram.hxx> |
f978241f |
20 | #include <Graphic3d_StereoMode.hxx> |
c357e426 |
21 | #include <Graphic3d_TypeOfShadingModel.hxx> |
22 | #include <Graphic3d_TypeOfSurfaceDetail.hxx> |
30f0ad28 |
23 | |
24 | #include <NCollection_DataMap.hxx> |
25 | #include <NCollection_Sequence.hxx> |
26 | |
256f9ac0 |
27 | #include <OpenGl_SetOfShaderPrograms.hxx> |
30f0ad28 |
28 | #include <OpenGl_ShaderStates.hxx> |
8625ef7e |
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> |
c357e426 |
34 | |
30f0ad28 |
35 | |
e6804ff7 |
36 | class OpenGl_View; |
37 | |
30f0ad28 |
38 | //! List of shader programs. |
39 | typedef NCollection_Sequence<Handle(OpenGl_ShaderProgram)> OpenGl_ShaderProgramList; |
40 | |
41 | //! Map to declare per-program states of OCCT materials. |
42 | typedef NCollection_DataMap<Handle(OpenGl_ShaderProgram), OpenGl_MaterialState> OpenGl_MaterialStates; |
43 | |
c04c30b3 |
44 | class OpenGl_ShaderManager; |
45 | DEFINE_STANDARD_HANDLE(OpenGl_ShaderManager, Standard_Transient) |
46 | |
30f0ad28 |
47 | //! This class is responsible for managing shader programs. |
48 | class OpenGl_ShaderManager : public Standard_Transient |
49 | { |
50 | friend class OpenGl_ShaderProgram; |
51 | |
52 | public: |
53 | |
54 | //! Creates new empty shader manager. |
55 | Standard_EXPORT OpenGl_ShaderManager (OpenGl_Context* theContext); |
56 | |
57 | //! Releases resources of shader manager. |
58 | Standard_EXPORT virtual ~OpenGl_ShaderManager(); |
59 | |
05e2200b |
60 | //! Release all resources. |
61 | Standard_EXPORT void clear(); |
62 | |
392ac980 |
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 |
8625ef7e |
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); |
30f0ad28 |
71 | |
72 | //! Unregisters specified shader program. |
392ac980 |
73 | Standard_EXPORT void Unregister (TCollection_AsciiString& theShareKey, |
74 | Handle(OpenGl_ShaderProgram)& theProgram); |
30f0ad28 |
75 | |
76 | //! Returns list of registered shader programs. |
77 | Standard_EXPORT const OpenGl_ShaderProgramList& ShaderPrograms() const; |
78 | |
79 | //! Returns true if no program objects are registered in the manager. |
80 | Standard_EXPORT Standard_Boolean IsEmpty() const; |
81 | |
8625ef7e |
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) |
88 | { |
89 | if (!theCustomProgram.IsNull() |
90 | || myContext->caps->ffpEnable) |
91 | { |
92 | return bindProgramWithState (theCustomProgram, theAspect); |
93 | } |
94 | |
e135a155 |
95 | const Standard_Integer aBits = getProgramBits (theTexture, theHasVertColor, Standard_True); |
8625ef7e |
96 | Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits); |
97 | return bindProgramWithState (aProgram, theAspect); |
98 | } |
30f0ad28 |
99 | |
8625ef7e |
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) |
106 | { |
107 | if (!theCustomProgram.IsNull() |
108 | || myContext->caps->ffpEnable) |
109 | { |
110 | return bindProgramWithState (theCustomProgram, theAspect); |
111 | } |
112 | |
ac116c22 |
113 | Standard_Integer aBits = getProgramBits (theTexture, theHasVertColor); |
114 | if (theAspect->Type() != Aspect_TOL_SOLID) |
115 | { |
116 | aBits |= OpenGl_PO_StippleLine; |
117 | } |
118 | |
8625ef7e |
119 | Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits); |
120 | return bindProgramWithState (aProgram, theAspect); |
121 | } |
30f0ad28 |
122 | |
8625ef7e |
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) |
129 | { |
130 | if (!theCustomProgram.IsNull() |
131 | || myContext->caps->ffpEnable) |
132 | { |
133 | return bindProgramWithState (theCustomProgram, theAspect); |
134 | } |
135 | |
136 | const Standard_Integer aBits = getProgramBits (theTexture, theHasVertColor) | OpenGl_PO_Point; |
137 | Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits); |
138 | return bindProgramWithState (aProgram, theAspect); |
139 | } |
140 | |
141 | //! Bind program for rendering alpha-textured font. |
142 | Standard_Boolean BindProgram (const OpenGl_AspectText* theAspect, |
143 | const Handle(OpenGl_ShaderProgram)& theCustomProgram) |
144 | { |
145 | if (!theCustomProgram.IsNull() |
146 | || myContext->caps->ffpEnable) |
147 | { |
148 | return bindProgramWithState (theCustomProgram, theAspect); |
149 | } |
150 | |
151 | if (myFontProgram.IsNull()) |
152 | { |
153 | prepareStdProgramFont(); |
154 | } |
155 | return bindProgramWithState (myFontProgram, theAspect); |
156 | } |
30f0ad28 |
157 | |
b86bb3df |
158 | //! Bind program for FBO blit operation. |
159 | Standard_Boolean BindFboBlitProgram() |
160 | { |
161 | if (myBlitProgram.IsNull()) |
162 | { |
163 | prepareStdProgramFboBlit(); |
164 | } |
165 | return !myBlitProgram.IsNull() |
166 | && myContext->BindProgram (myBlitProgram); |
167 | } |
168 | |
f978241f |
169 | //! Bind program for rendering stereoscopic image. |
170 | Standard_Boolean BindStereoProgram (const Graphic3d_StereoMode theStereoMode) |
38a0206f |
171 | { |
f978241f |
172 | if (theStereoMode < 0 || theStereoMode >= Graphic3d_StereoMode_NB) |
38a0206f |
173 | { |
f978241f |
174 | return Standard_False; |
38a0206f |
175 | } |
f978241f |
176 | |
177 | if (myStereoPrograms[theStereoMode].IsNull()) |
178 | { |
179 | prepareStdProgramStereo (myStereoPrograms[theStereoMode], theStereoMode); |
180 | } |
181 | const Handle(OpenGl_ShaderProgram)& aProgram = myStereoPrograms[theStereoMode]; |
182 | return !aProgram.IsNull() |
183 | && myContext->BindProgram (aProgram); |
38a0206f |
184 | } |
185 | |
30f0ad28 |
186 | public: |
187 | |
188 | //! Returns current state of OCCT light sources. |
189 | Standard_EXPORT const OpenGl_LightSourceState& LightSourceState() const; |
190 | |
191 | //! Updates state of OCCT light sources. |
192 | Standard_EXPORT void UpdateLightSourceStateTo (const OpenGl_ListOfLight* theLights); |
193 | |
194 | //! Pushes current state of OCCT light sources to specified program. |
195 | Standard_EXPORT void PushLightSourceState (const Handle(OpenGl_ShaderProgram)& theProgram) const; |
196 | |
197 | public: |
198 | |
199 | //! Returns current state of OCCT projection transform. |
200 | Standard_EXPORT const OpenGl_ProjectionState& ProjectionState() const; |
201 | |
202 | //! Updates state of OCCT projection transform. |
c827ea3a |
203 | Standard_EXPORT void UpdateProjectionStateTo (const OpenGl_Mat4& theProjectionMatrix); |
30f0ad28 |
204 | |
30f0ad28 |
205 | //! Pushes current state of OCCT projection transform to specified program. |
206 | Standard_EXPORT void PushProjectionState (const Handle(OpenGl_ShaderProgram)& theProgram) const; |
207 | |
208 | public: |
209 | |
210 | //! Returns current state of OCCT model-world transform. |
211 | Standard_EXPORT const OpenGl_ModelWorldState& ModelWorldState() const; |
212 | |
213 | //! Updates state of OCCT model-world transform. |
c827ea3a |
214 | Standard_EXPORT void UpdateModelWorldStateTo (const OpenGl_Mat4& theModelWorldMatrix); |
30f0ad28 |
215 | |
30f0ad28 |
216 | //! Pushes current state of OCCT model-world transform to specified program. |
217 | Standard_EXPORT void PushModelWorldState (const Handle(OpenGl_ShaderProgram)& theProgram) const; |
218 | |
219 | public: |
220 | |
221 | //! Returns current state of OCCT world-view transform. |
222 | Standard_EXPORT const OpenGl_WorldViewState& WorldViewState() const; |
223 | |
224 | //! Updates state of OCCT world-view transform. |
c827ea3a |
225 | Standard_EXPORT void UpdateWorldViewStateTo (const OpenGl_Mat4& theWorldViewMatrix); |
30f0ad28 |
226 | |
30f0ad28 |
227 | //! Pushes current state of OCCT world-view transform to specified program. |
228 | Standard_EXPORT void PushWorldViewState (const Handle(OpenGl_ShaderProgram)& theProgram) const; |
229 | |
230 | public: |
231 | |
232 | //! Updates state of OCCT clipping planes. |
233 | Standard_EXPORT void UpdateClippingState(); |
234 | |
235 | //! Reverts state of OCCT clipping planes. |
236 | Standard_EXPORT void RevertClippingState(); |
237 | |
238 | //! Pushes current state of OCCT clipping planes to specified program. |
239 | Standard_EXPORT void PushClippingState (const Handle(OpenGl_ShaderProgram)& theProgram) const; |
240 | |
241 | public: |
242 | |
243 | //! Resets state of OCCT material for all programs. |
244 | Standard_EXPORT void ResetMaterialStates(); |
245 | |
246 | //! Updates state of OCCT material for specified program. |
247 | Standard_EXPORT void UpdateMaterialStateTo (const Handle(OpenGl_ShaderProgram)& theProgram, |
248 | const OpenGl_Element* theAspect); |
249 | |
250 | //! Pushes current state of OCCT material to specified program. |
251 | Standard_EXPORT void PushMaterialState (const Handle(OpenGl_ShaderProgram)& theProgram) const; |
252 | |
253 | //! Returns current state of OCCT material for specified program. |
254 | Standard_EXPORT const OpenGl_MaterialState* MaterialState (const Handle(OpenGl_ShaderProgram)& theProgram) const; |
255 | |
e135a155 |
256 | public: |
257 | |
258 | //! Returns current state of OCCT surface detail. |
259 | Standard_EXPORT const OpenGl_SurfaceDetailState& SurfaceDetailState() const; |
260 | |
261 | //! Updates state of OCCT surface detail. |
c357e426 |
262 | Standard_EXPORT void UpdateSurfaceDetailStateTo (const Graphic3d_TypeOfSurfaceDetail theDetail); |
e135a155 |
263 | |
30f0ad28 |
264 | public: |
05e2200b |
265 | |
30f0ad28 |
266 | //! Pushes current state of OCCT graphics parameters to specified program. |
267 | Standard_EXPORT void PushState (const Handle(OpenGl_ShaderProgram)& theProgram) const; |
268 | |
392ac980 |
269 | public: |
270 | |
271 | //! Overwrites context |
272 | void SetContext (OpenGl_Context* theCtx) |
273 | { |
274 | myContext = theCtx; |
275 | } |
276 | |
c357e426 |
277 | //! Returns true when provided context is the same as used one by shader manager. |
278 | bool IsSameContext (OpenGl_Context* theCtx) const |
279 | { |
280 | return myContext == theCtx; |
281 | } |
282 | |
8625ef7e |
283 | //! Sets shading model. |
c357e426 |
284 | Standard_EXPORT void SetShadingModel (const Graphic3d_TypeOfShadingModel theModel); |
8625ef7e |
285 | |
e6804ff7 |
286 | //! Sets last view manger used with. |
287 | //! Helps to handle matrix states in multi-view configurations. |
288 | void SetLastView (const OpenGl_View* theLastView) |
289 | { |
290 | myLastView = theLastView; |
291 | } |
292 | |
293 | //! Returns true when provided view is the same as cached one. |
294 | bool IsSameView (const OpenGl_View* theView) const |
295 | { |
296 | return myLastView == theView; |
297 | } |
298 | |
30f0ad28 |
299 | protected: |
300 | |
8625ef7e |
301 | //! Define program bits. |
302 | Standard_Integer getProgramBits (const Handle(OpenGl_Texture)& theTexture, |
e135a155 |
303 | const Standard_Boolean theHasVertColor, |
304 | const Standard_Boolean theEnableEnvMap = Standard_False) |
305 | |
8625ef7e |
306 | { |
307 | Standard_Integer aBits = 0; |
308 | if (myContext->Clipping().IsClippingOrCappingOn()) |
309 | { |
310 | aBits |= OpenGl_PO_ClipPlanes; |
311 | } |
c357e426 |
312 | if (theEnableEnvMap && mySurfaceDetailState.Detail() == Graphic3d_TOD_ENVIRONMENT) |
e135a155 |
313 | { |
314 | // Environment map overwrites material texture |
315 | aBits |= OpenGl_PO_TextureEnv; |
316 | } |
317 | else if (!theTexture.IsNull()) |
8625ef7e |
318 | { |
4e1523ef |
319 | aBits |= theTexture->IsAlpha() ? OpenGl_PO_TextureA : OpenGl_PO_TextureRGB; |
8625ef7e |
320 | } |
321 | if (theHasVertColor) |
322 | { |
323 | aBits |= OpenGl_PO_VertColor; |
324 | } |
325 | return aBits; |
326 | } |
327 | |
328 | //! Prepare standard GLSL program. |
329 | Handle(OpenGl_ShaderProgram)& getStdProgram (const Standard_Boolean theToLightOn, |
330 | const Standard_Integer theBits) |
331 | { |
e135a155 |
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) |
8625ef7e |
335 | { |
256f9ac0 |
336 | Handle(OpenGl_ShaderProgram)& aProgram = myLightPrograms->ChangeValue (theBits); |
8625ef7e |
337 | if (aProgram.IsNull()) |
338 | { |
339 | prepareStdProgramLight (aProgram, theBits); |
340 | } |
341 | return aProgram; |
342 | } |
343 | |
256f9ac0 |
344 | Handle(OpenGl_ShaderProgram)& aProgram = myFlatPrograms.ChangeValue (theBits); |
8625ef7e |
345 | if (aProgram.IsNull()) |
346 | { |
347 | prepareStdProgramFlat (aProgram, theBits); |
348 | } |
349 | return aProgram; |
350 | } |
351 | |
352 | //! Prepare standard GLSL program for textured font. |
353 | Standard_EXPORT Standard_Boolean prepareStdProgramFont(); |
354 | |
b86bb3df |
355 | //! Prepare standard GLSL program for FBO blit operation. |
356 | Standard_EXPORT Standard_Boolean prepareStdProgramFboBlit(); |
357 | |
8625ef7e |
358 | //! Prepare standard GLSL program without lighting. |
359 | Standard_EXPORT Standard_Boolean prepareStdProgramFlat (Handle(OpenGl_ShaderProgram)& theProgram, |
360 | const Standard_Integer theBits); |
361 | |
362 | //! Prepare standard GLSL program with lighting. |
363 | Standard_Boolean prepareStdProgramLight (Handle(OpenGl_ShaderProgram)& theProgram, |
364 | const Standard_Integer theBits) |
365 | { |
c357e426 |
366 | return myShadingModel == Graphic3d_TOSM_FRAGMENT |
8625ef7e |
367 | ? prepareStdProgramPhong (theProgram, theBits) |
368 | : prepareStdProgramGouraud (theProgram, theBits); |
369 | } |
370 | |
371 | //! Prepare standard GLSL program with per-vertex lighting. |
372 | Standard_EXPORT Standard_Boolean prepareStdProgramGouraud (Handle(OpenGl_ShaderProgram)& theProgram, |
373 | const Standard_Integer theBits); |
374 | |
375 | //! Prepare standard GLSL program with per-pixel lighting. |
376 | Standard_EXPORT Standard_Boolean prepareStdProgramPhong (Handle(OpenGl_ShaderProgram)& theProgram, |
377 | const Standard_Integer theBits); |
378 | |
256f9ac0 |
379 | //! Define computeLighting GLSL function depending on current lights configuration |
abdf0b10 |
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); |
256f9ac0 |
382 | |
8625ef7e |
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); |
386 | |
256f9ac0 |
387 | //! Set pointer myLightPrograms to active lighting programs set from myMapOfLightPrograms |
388 | Standard_EXPORT void switchLightPrograms(); |
389 | |
f978241f |
390 | //! Prepare standard GLSL program for stereoscopic image. |
391 | Standard_EXPORT Standard_Boolean prepareStdProgramStereo (Handle(OpenGl_ShaderProgram)& theProgram, |
392 | const Graphic3d_StereoMode theStereoMode); |
38a0206f |
393 | |
8625ef7e |
394 | protected: |
395 | |
c357e426 |
396 | Graphic3d_TypeOfShadingModel myShadingModel; //!< lighting shading model |
256f9ac0 |
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 |
b86bb3df |
401 | Handle(OpenGl_ShaderProgram) myBlitProgram; //!< standard program for FBO blit emulation |
256f9ac0 |
402 | OpenGl_MapOfShaderPrograms myMapOfLightPrograms; //!< map of lighting programs depending on shading model and lights configuration |
8625ef7e |
403 | |
f978241f |
404 | Handle(OpenGl_ShaderProgram) myStereoPrograms[Graphic3d_StereoMode_NB]; //!< standard stereo programs |
38a0206f |
405 | |
256f9ac0 |
406 | OpenGl_Context* myContext; //!< OpenGL context |
8625ef7e |
407 | |
408 | protected: |
409 | |
256f9ac0 |
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 |
8cf06aa2 |
414 | OpenGl_ClippingState myClippingState; //!< State of OCCT clipping planes |
256f9ac0 |
415 | OpenGl_LightSourceState myLightSourceState; //!< State of OCCT light sources |
e135a155 |
416 | OpenGl_SurfaceDetailState mySurfaceDetailState; //!< State of OCCT surface detail |
30f0ad28 |
417 | |
418 | private: |
419 | |
256f9ac0 |
420 | const OpenGl_View* myLastView; //!< Pointer to the last view shader manager used with |
8625ef7e |
421 | |
422 | public: |
423 | |
ec357c5c |
424 | DEFINE_STANDARD_RTTI (OpenGl_ShaderManager, Standard_Transient) |
8625ef7e |
425 | |
30f0ad28 |
426 | }; |
427 | |
428 | #endif // _OpenGl_ShaderManager_HeaderFile |