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