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> |
30f0ad28 |
22 | |
23 | #include <NCollection_DataMap.hxx> |
24 | #include <NCollection_Sequence.hxx> |
25 | |
256f9ac0 |
26 | #include <OpenGl_SetOfShaderPrograms.hxx> |
30f0ad28 |
27 | #include <OpenGl_ShaderStates.hxx> |
8625ef7e |
28 | #include <OpenGl_AspectFace.hxx> |
29 | #include <OpenGl_AspectLine.hxx> |
30 | #include <OpenGl_AspectText.hxx> |
31 | #include <OpenGl_AspectMarker.hxx> |
32 | #include <OpenGl_Texture.hxx> |
c357e426 |
33 | |
30f0ad28 |
34 | |
e6804ff7 |
35 | class OpenGl_View; |
36 | |
30f0ad28 |
37 | //! List of shader programs. |
38 | typedef NCollection_Sequence<Handle(OpenGl_ShaderProgram)> OpenGl_ShaderProgramList; |
39 | |
c04c30b3 |
40 | class OpenGl_ShaderManager; |
41 | DEFINE_STANDARD_HANDLE(OpenGl_ShaderManager, Standard_Transient) |
42 | |
30f0ad28 |
43 | //! This class is responsible for managing shader programs. |
44 | class OpenGl_ShaderManager : public Standard_Transient |
45 | { |
46 | friend class OpenGl_ShaderProgram; |
47 | |
48 | public: |
49 | |
50 | //! Creates new empty shader manager. |
51 | Standard_EXPORT OpenGl_ShaderManager (OpenGl_Context* theContext); |
52 | |
53 | //! Releases resources of shader manager. |
54 | Standard_EXPORT virtual ~OpenGl_ShaderManager(); |
55 | |
05e2200b |
56 | //! Release all resources. |
57 | Standard_EXPORT void clear(); |
58 | |
7c3ef2f7 |
59 | //! Return local camera transformation. |
60 | const gp_XYZ& LocalOrigin() const { return myLocalOrigin; } |
61 | |
62 | //! Setup local camera transformation for compensating float precision issues. |
63 | void SetLocalOrigin (const gp_XYZ& theOrigin) |
64 | { |
65 | myLocalOrigin = theOrigin; |
66 | myHasLocalOrigin = !theOrigin.IsEqual (gp_XYZ(0.0, 0.0, 0.0), gp::Resolution()); |
67 | } |
68 | |
392ac980 |
69 | //! Creates new shader program or re-use shared instance. |
70 | //! @param theProxy [IN] program definition |
71 | //! @param theShareKey [OUT] sharing key |
72 | //! @param theProgram [OUT] OpenGL program |
8625ef7e |
73 | //! @return true on success |
74 | Standard_EXPORT Standard_Boolean Create (const Handle(Graphic3d_ShaderProgram)& theProxy, |
75 | TCollection_AsciiString& theShareKey, |
76 | Handle(OpenGl_ShaderProgram)& theProgram); |
30f0ad28 |
77 | |
78 | //! Unregisters specified shader program. |
392ac980 |
79 | Standard_EXPORT void Unregister (TCollection_AsciiString& theShareKey, |
80 | Handle(OpenGl_ShaderProgram)& theProgram); |
30f0ad28 |
81 | |
82 | //! Returns list of registered shader programs. |
83 | Standard_EXPORT const OpenGl_ShaderProgramList& ShaderPrograms() const; |
84 | |
85 | //! Returns true if no program objects are registered in the manager. |
86 | Standard_EXPORT Standard_Boolean IsEmpty() const; |
87 | |
8625ef7e |
88 | //! Bind program for filled primitives rendering |
299e0ab9 |
89 | Standard_Boolean BindFaceProgram (const Handle(OpenGl_Texture)& theTexture, |
90 | const Standard_Boolean theToLightOn, |
91 | const Standard_Boolean theHasVertColor, |
92 | const Standard_Boolean theEnableEnvMap, |
93 | const Handle(OpenGl_ShaderProgram)& theCustomProgram) |
8625ef7e |
94 | { |
95 | if (!theCustomProgram.IsNull() |
96 | || myContext->caps->ffpEnable) |
97 | { |
299e0ab9 |
98 | return bindProgramWithState (theCustomProgram); |
8625ef7e |
99 | } |
100 | |
83da37b1 |
101 | const Standard_Integer aBits = getProgramBits (theTexture, theHasVertColor, theEnableEnvMap); |
8625ef7e |
102 | Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits); |
299e0ab9 |
103 | return bindProgramWithState (aProgram); |
8625ef7e |
104 | } |
30f0ad28 |
105 | |
8625ef7e |
106 | //! Bind program for line rendering |
299e0ab9 |
107 | Standard_Boolean BindLineProgram (const Handle(OpenGl_Texture)& theTexture, |
108 | const Standard_Boolean theStipple, |
109 | const Standard_Boolean theToLightOn, |
110 | const Standard_Boolean theHasVertColor, |
111 | const Handle(OpenGl_ShaderProgram)& theCustomProgram) |
8625ef7e |
112 | { |
113 | if (!theCustomProgram.IsNull() |
114 | || myContext->caps->ffpEnable) |
115 | { |
299e0ab9 |
116 | return bindProgramWithState (theCustomProgram); |
8625ef7e |
117 | } |
118 | |
ac116c22 |
119 | Standard_Integer aBits = getProgramBits (theTexture, theHasVertColor); |
299e0ab9 |
120 | if (theStipple) |
ac116c22 |
121 | { |
122 | aBits |= OpenGl_PO_StippleLine; |
123 | } |
124 | |
8625ef7e |
125 | Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits); |
299e0ab9 |
126 | return bindProgramWithState (aProgram); |
8625ef7e |
127 | } |
30f0ad28 |
128 | |
8625ef7e |
129 | //! Bind program for point rendering |
299e0ab9 |
130 | Standard_Boolean BindMarkerProgram (const Handle(OpenGl_Texture)& theTexture, |
131 | const Standard_Boolean theToLightOn, |
132 | const Standard_Boolean theHasVertColor, |
133 | const Handle(OpenGl_ShaderProgram)& theCustomProgram) |
8625ef7e |
134 | { |
135 | if (!theCustomProgram.IsNull() |
136 | || myContext->caps->ffpEnable) |
137 | { |
299e0ab9 |
138 | return bindProgramWithState (theCustomProgram); |
8625ef7e |
139 | } |
140 | |
141 | const Standard_Integer aBits = getProgramBits (theTexture, theHasVertColor) | OpenGl_PO_Point; |
142 | Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits); |
299e0ab9 |
143 | return bindProgramWithState (aProgram); |
8625ef7e |
144 | } |
145 | |
146 | //! Bind program for rendering alpha-textured font. |
299e0ab9 |
147 | Standard_Boolean BindFontProgram (const Handle(OpenGl_ShaderProgram)& theCustomProgram) |
8625ef7e |
148 | { |
149 | if (!theCustomProgram.IsNull() |
150 | || myContext->caps->ffpEnable) |
151 | { |
299e0ab9 |
152 | return bindProgramWithState (theCustomProgram); |
8625ef7e |
153 | } |
154 | |
155 | if (myFontProgram.IsNull()) |
156 | { |
157 | prepareStdProgramFont(); |
158 | } |
299e0ab9 |
159 | |
160 | return bindProgramWithState (myFontProgram); |
8625ef7e |
161 | } |
30f0ad28 |
162 | |
b86bb3df |
163 | //! Bind program for FBO blit operation. |
164 | Standard_Boolean BindFboBlitProgram() |
165 | { |
166 | if (myBlitProgram.IsNull()) |
167 | { |
168 | prepareStdProgramFboBlit(); |
169 | } |
170 | return !myBlitProgram.IsNull() |
171 | && myContext->BindProgram (myBlitProgram); |
172 | } |
173 | |
f978241f |
174 | //! Bind program for rendering stereoscopic image. |
175 | Standard_Boolean BindStereoProgram (const Graphic3d_StereoMode theStereoMode) |
38a0206f |
176 | { |
f978241f |
177 | if (theStereoMode < 0 || theStereoMode >= Graphic3d_StereoMode_NB) |
38a0206f |
178 | { |
f978241f |
179 | return Standard_False; |
38a0206f |
180 | } |
f978241f |
181 | |
182 | if (myStereoPrograms[theStereoMode].IsNull()) |
183 | { |
184 | prepareStdProgramStereo (myStereoPrograms[theStereoMode], theStereoMode); |
185 | } |
186 | const Handle(OpenGl_ShaderProgram)& aProgram = myStereoPrograms[theStereoMode]; |
187 | return !aProgram.IsNull() |
188 | && myContext->BindProgram (aProgram); |
38a0206f |
189 | } |
190 | |
30f0ad28 |
191 | public: |
192 | |
193 | //! Returns current state of OCCT light sources. |
194 | Standard_EXPORT const OpenGl_LightSourceState& LightSourceState() const; |
195 | |
196 | //! Updates state of OCCT light sources. |
197 | Standard_EXPORT void UpdateLightSourceStateTo (const OpenGl_ListOfLight* theLights); |
198 | |
7c3ef2f7 |
199 | //! Invalidate state of OCCT light sources. |
200 | Standard_EXPORT void UpdateLightSourceState(); |
201 | |
30f0ad28 |
202 | //! Pushes current state of OCCT light sources to specified program. |
203 | Standard_EXPORT void PushLightSourceState (const Handle(OpenGl_ShaderProgram)& theProgram) const; |
204 | |
205 | public: |
206 | |
207 | //! Returns current state of OCCT projection transform. |
208 | Standard_EXPORT const OpenGl_ProjectionState& ProjectionState() const; |
209 | |
210 | //! Updates state of OCCT projection transform. |
c827ea3a |
211 | Standard_EXPORT void UpdateProjectionStateTo (const OpenGl_Mat4& theProjectionMatrix); |
30f0ad28 |
212 | |
30f0ad28 |
213 | //! Pushes current state of OCCT projection transform to specified program. |
214 | Standard_EXPORT void PushProjectionState (const Handle(OpenGl_ShaderProgram)& theProgram) const; |
215 | |
216 | public: |
217 | |
218 | //! Returns current state of OCCT model-world transform. |
219 | Standard_EXPORT const OpenGl_ModelWorldState& ModelWorldState() const; |
220 | |
221 | //! Updates state of OCCT model-world transform. |
c827ea3a |
222 | Standard_EXPORT void UpdateModelWorldStateTo (const OpenGl_Mat4& theModelWorldMatrix); |
30f0ad28 |
223 | |
30f0ad28 |
224 | //! Pushes current state of OCCT model-world transform to specified program. |
225 | Standard_EXPORT void PushModelWorldState (const Handle(OpenGl_ShaderProgram)& theProgram) const; |
226 | |
227 | public: |
228 | |
229 | //! Returns current state of OCCT world-view transform. |
230 | Standard_EXPORT const OpenGl_WorldViewState& WorldViewState() const; |
231 | |
232 | //! Updates state of OCCT world-view transform. |
c827ea3a |
233 | Standard_EXPORT void UpdateWorldViewStateTo (const OpenGl_Mat4& theWorldViewMatrix); |
30f0ad28 |
234 | |
30f0ad28 |
235 | //! Pushes current state of OCCT world-view transform to specified program. |
236 | Standard_EXPORT void PushWorldViewState (const Handle(OpenGl_ShaderProgram)& theProgram) const; |
237 | |
238 | public: |
239 | |
240 | //! Updates state of OCCT clipping planes. |
241 | Standard_EXPORT void UpdateClippingState(); |
242 | |
243 | //! Reverts state of OCCT clipping planes. |
244 | Standard_EXPORT void RevertClippingState(); |
245 | |
246 | //! Pushes current state of OCCT clipping planes to specified program. |
247 | Standard_EXPORT void PushClippingState (const Handle(OpenGl_ShaderProgram)& theProgram) const; |
248 | |
30f0ad28 |
249 | public: |
05e2200b |
250 | |
30f0ad28 |
251 | //! Pushes current state of OCCT graphics parameters to specified program. |
252 | Standard_EXPORT void PushState (const Handle(OpenGl_ShaderProgram)& theProgram) const; |
253 | |
392ac980 |
254 | public: |
255 | |
256 | //! Overwrites context |
257 | void SetContext (OpenGl_Context* theCtx) |
258 | { |
259 | myContext = theCtx; |
260 | } |
261 | |
c357e426 |
262 | //! Returns true when provided context is the same as used one by shader manager. |
263 | bool IsSameContext (OpenGl_Context* theCtx) const |
264 | { |
265 | return myContext == theCtx; |
266 | } |
267 | |
8625ef7e |
268 | //! Sets shading model. |
c357e426 |
269 | Standard_EXPORT void SetShadingModel (const Graphic3d_TypeOfShadingModel theModel); |
8625ef7e |
270 | |
e6804ff7 |
271 | //! Sets last view manger used with. |
272 | //! Helps to handle matrix states in multi-view configurations. |
273 | void SetLastView (const OpenGl_View* theLastView) |
274 | { |
275 | myLastView = theLastView; |
276 | } |
277 | |
278 | //! Returns true when provided view is the same as cached one. |
279 | bool IsSameView (const OpenGl_View* theView) const |
280 | { |
281 | return myLastView == theView; |
282 | } |
283 | |
30f0ad28 |
284 | protected: |
285 | |
8625ef7e |
286 | //! Define program bits. |
287 | Standard_Integer getProgramBits (const Handle(OpenGl_Texture)& theTexture, |
e135a155 |
288 | const Standard_Boolean theHasVertColor, |
289 | const Standard_Boolean theEnableEnvMap = Standard_False) |
290 | |
8625ef7e |
291 | { |
292 | Standard_Integer aBits = 0; |
1a7ece8f |
293 | |
294 | const Standard_Integer aNbPlanes = myContext->Clipping().NbClippingOrCappingOn(); |
295 | if (aNbPlanes > 0) |
8625ef7e |
296 | { |
1a7ece8f |
297 | aBits |= OpenGl_PO_ClipPlanesN; |
298 | if (aNbPlanes == 1) |
299 | { |
300 | aBits |= OpenGl_PO_ClipPlanes1; |
301 | } |
302 | else if (aNbPlanes == 2) |
303 | { |
304 | aBits |= OpenGl_PO_ClipPlanes2; |
305 | } |
8625ef7e |
306 | } |
1a7ece8f |
307 | |
83da37b1 |
308 | if (theEnableEnvMap) |
e135a155 |
309 | { |
310 | // Environment map overwrites material texture |
311 | aBits |= OpenGl_PO_TextureEnv; |
312 | } |
313 | else if (!theTexture.IsNull()) |
8625ef7e |
314 | { |
4e1523ef |
315 | aBits |= theTexture->IsAlpha() ? OpenGl_PO_TextureA : OpenGl_PO_TextureRGB; |
8625ef7e |
316 | } |
317 | if (theHasVertColor) |
318 | { |
319 | aBits |= OpenGl_PO_VertColor; |
320 | } |
321 | return aBits; |
322 | } |
323 | |
324 | //! Prepare standard GLSL program. |
325 | Handle(OpenGl_ShaderProgram)& getStdProgram (const Standard_Boolean theToLightOn, |
326 | const Standard_Integer theBits) |
327 | { |
e135a155 |
328 | // If environment map is enabled lighting calculations are |
329 | // not needed (in accordance with default OCCT behaviour) |
330 | if (theToLightOn && (theBits & OpenGl_PO_TextureEnv) == 0) |
8625ef7e |
331 | { |
256f9ac0 |
332 | Handle(OpenGl_ShaderProgram)& aProgram = myLightPrograms->ChangeValue (theBits); |
8625ef7e |
333 | if (aProgram.IsNull()) |
334 | { |
335 | prepareStdProgramLight (aProgram, theBits); |
336 | } |
337 | return aProgram; |
338 | } |
339 | |
256f9ac0 |
340 | Handle(OpenGl_ShaderProgram)& aProgram = myFlatPrograms.ChangeValue (theBits); |
8625ef7e |
341 | if (aProgram.IsNull()) |
342 | { |
343 | prepareStdProgramFlat (aProgram, theBits); |
344 | } |
345 | return aProgram; |
346 | } |
347 | |
299e0ab9 |
348 | //! Prepare standard GLSL program for accessing point sprite alpha. |
fd59283a |
349 | Standard_EXPORT TCollection_AsciiString pointSpriteAlphaSrc (const Standard_Integer theBits); |
299e0ab9 |
350 | |
351 | //! Prepare standard GLSL program for computing point sprite shading. |
352 | Standard_EXPORT TCollection_AsciiString pointSpriteShadingSrc (const TCollection_AsciiString theBaseColorSrc, const Standard_Integer theBits); |
353 | |
8625ef7e |
354 | //! Prepare standard GLSL program for textured font. |
355 | Standard_EXPORT Standard_Boolean prepareStdProgramFont(); |
356 | |
b86bb3df |
357 | //! Prepare standard GLSL program for FBO blit operation. |
358 | Standard_EXPORT Standard_Boolean prepareStdProgramFboBlit(); |
359 | |
8625ef7e |
360 | //! Prepare standard GLSL program without lighting. |
361 | Standard_EXPORT Standard_Boolean prepareStdProgramFlat (Handle(OpenGl_ShaderProgram)& theProgram, |
362 | const Standard_Integer theBits); |
363 | |
364 | //! Prepare standard GLSL program with lighting. |
365 | Standard_Boolean prepareStdProgramLight (Handle(OpenGl_ShaderProgram)& theProgram, |
366 | const Standard_Integer theBits) |
367 | { |
c357e426 |
368 | return myShadingModel == Graphic3d_TOSM_FRAGMENT |
8625ef7e |
369 | ? prepareStdProgramPhong (theProgram, theBits) |
370 | : prepareStdProgramGouraud (theProgram, theBits); |
371 | } |
372 | |
373 | //! Prepare standard GLSL program with per-vertex lighting. |
374 | Standard_EXPORT Standard_Boolean prepareStdProgramGouraud (Handle(OpenGl_ShaderProgram)& theProgram, |
375 | const Standard_Integer theBits); |
376 | |
377 | //! Prepare standard GLSL program with per-pixel lighting. |
378 | Standard_EXPORT Standard_Boolean prepareStdProgramPhong (Handle(OpenGl_ShaderProgram)& theProgram, |
379 | const Standard_Integer theBits); |
380 | |
256f9ac0 |
381 | //! Define computeLighting GLSL function depending on current lights configuration |
abdf0b10 |
382 | //! @param theHasVertColor flag to use getVertColor() instead of Ambient and Diffuse components of active material |
383 | Standard_EXPORT TCollection_AsciiString stdComputeLighting (const Standard_Boolean theHasVertColor); |
256f9ac0 |
384 | |
8625ef7e |
385 | //! Bind specified program to current context and apply state. |
299e0ab9 |
386 | Standard_EXPORT Standard_Boolean bindProgramWithState (const Handle(OpenGl_ShaderProgram)& theProgram); |
8625ef7e |
387 | |
256f9ac0 |
388 | //! Set pointer myLightPrograms to active lighting programs set from myMapOfLightPrograms |
389 | Standard_EXPORT void switchLightPrograms(); |
390 | |
f978241f |
391 | //! Prepare standard GLSL program for stereoscopic image. |
392 | Standard_EXPORT Standard_Boolean prepareStdProgramStereo (Handle(OpenGl_ShaderProgram)& theProgram, |
393 | const Graphic3d_StereoMode theStereoMode); |
38a0206f |
394 | |
7c3ef2f7 |
395 | protected: |
396 | |
397 | //! Packed properties of light source |
398 | struct OpenGl_ShaderLightParameters |
399 | { |
400 | OpenGl_Vec4 Color; |
401 | OpenGl_Vec4 Position; |
402 | OpenGl_Vec4 Direction; |
403 | OpenGl_Vec4 Parameters; |
404 | |
405 | //! Returns packed (serialized) representation of light source properties |
406 | const OpenGl_Vec4* Packed() const { return reinterpret_cast<const OpenGl_Vec4*> (this); } |
407 | static Standard_Integer NbOfVec4() { return 4; } |
408 | }; |
409 | |
410 | //! Packed light source type information |
411 | struct OpenGl_ShaderLightType |
412 | { |
413 | Standard_Integer Type; |
414 | Standard_Integer IsHeadlight; |
415 | |
416 | //! Returns packed (serialized) representation of light source type |
417 | const OpenGl_Vec2i* Packed() const { return reinterpret_cast<const OpenGl_Vec2i*> (this); } |
418 | static Standard_Integer NbOfVec2i() { return 1; } |
419 | }; |
420 | |
8625ef7e |
421 | protected: |
422 | |
c357e426 |
423 | Graphic3d_TypeOfShadingModel myShadingModel; //!< lighting shading model |
256f9ac0 |
424 | OpenGl_ShaderProgramList myProgramList; //!< The list of shader programs |
425 | Handle(OpenGl_SetOfShaderPrograms) myLightPrograms; //!< pointer to active lighting programs matrix |
426 | OpenGl_SetOfShaderPrograms myFlatPrograms; //!< programs matrix without lighting |
427 | Handle(OpenGl_ShaderProgram) myFontProgram; //!< standard program for textured text |
b86bb3df |
428 | Handle(OpenGl_ShaderProgram) myBlitProgram; //!< standard program for FBO blit emulation |
256f9ac0 |
429 | OpenGl_MapOfShaderPrograms myMapOfLightPrograms; //!< map of lighting programs depending on shading model and lights configuration |
8625ef7e |
430 | |
f978241f |
431 | Handle(OpenGl_ShaderProgram) myStereoPrograms[Graphic3d_StereoMode_NB]; //!< standard stereo programs |
38a0206f |
432 | |
256f9ac0 |
433 | OpenGl_Context* myContext; //!< OpenGL context |
8625ef7e |
434 | |
435 | protected: |
436 | |
256f9ac0 |
437 | OpenGl_ProjectionState myProjectionState; //!< State of OCCT projection transformation |
438 | OpenGl_ModelWorldState myModelWorldState; //!< State of OCCT model-world transformation |
439 | OpenGl_WorldViewState myWorldViewState; //!< State of OCCT world-view transformation |
8cf06aa2 |
440 | OpenGl_ClippingState myClippingState; //!< State of OCCT clipping planes |
256f9ac0 |
441 | OpenGl_LightSourceState myLightSourceState; //!< State of OCCT light sources |
7c3ef2f7 |
442 | gp_XYZ myLocalOrigin; //!< local camera transformation |
443 | Standard_Boolean myHasLocalOrigin; //!< flag indicating that local camera transformation has been set |
444 | |
445 | mutable OpenGl_ShaderLightType myLightTypeArray [OpenGLMaxLights]; |
446 | mutable OpenGl_ShaderLightParameters myLightParamsArray[OpenGLMaxLights]; |
30f0ad28 |
447 | |
448 | private: |
449 | |
256f9ac0 |
450 | const OpenGl_View* myLastView; //!< Pointer to the last view shader manager used with |
8625ef7e |
451 | |
452 | public: |
453 | |
92efcf78 |
454 | DEFINE_STANDARD_RTTIEXT(OpenGl_ShaderManager,Standard_Transient) |
30f0ad28 |
455 | }; |
456 | |
457 | #endif // _OpenGl_ShaderManager_HeaderFile |