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; |
280 | if (myContext->Clipping().IsClippingOrCappingOn()) |
281 | { |
282 | aBits |= OpenGl_PO_ClipPlanes; |
283 | } |
83da37b1 |
284 | if (theEnableEnvMap) |
e135a155 |
285 | { |
286 | // Environment map overwrites material texture |
287 | aBits |= OpenGl_PO_TextureEnv; |
288 | } |
289 | else if (!theTexture.IsNull()) |
8625ef7e |
290 | { |
4e1523ef |
291 | aBits |= theTexture->IsAlpha() ? OpenGl_PO_TextureA : OpenGl_PO_TextureRGB; |
8625ef7e |
292 | } |
293 | if (theHasVertColor) |
294 | { |
295 | aBits |= OpenGl_PO_VertColor; |
296 | } |
297 | return aBits; |
298 | } |
299 | |
300 | //! Prepare standard GLSL program. |
301 | Handle(OpenGl_ShaderProgram)& getStdProgram (const Standard_Boolean theToLightOn, |
302 | const Standard_Integer theBits) |
303 | { |
e135a155 |
304 | // If environment map is enabled lighting calculations are |
305 | // not needed (in accordance with default OCCT behaviour) |
306 | if (theToLightOn && (theBits & OpenGl_PO_TextureEnv) == 0) |
8625ef7e |
307 | { |
256f9ac0 |
308 | Handle(OpenGl_ShaderProgram)& aProgram = myLightPrograms->ChangeValue (theBits); |
8625ef7e |
309 | if (aProgram.IsNull()) |
310 | { |
311 | prepareStdProgramLight (aProgram, theBits); |
312 | } |
313 | return aProgram; |
314 | } |
315 | |
256f9ac0 |
316 | Handle(OpenGl_ShaderProgram)& aProgram = myFlatPrograms.ChangeValue (theBits); |
8625ef7e |
317 | if (aProgram.IsNull()) |
318 | { |
319 | prepareStdProgramFlat (aProgram, theBits); |
320 | } |
321 | return aProgram; |
322 | } |
323 | |
299e0ab9 |
324 | //! Prepare standard GLSL program for accessing point sprite alpha. |
325 | Standard_EXPORT TCollection_AsciiString pointSpriteAlphaSrc(); |
326 | |
327 | //! Prepare standard GLSL program for computing point sprite shading. |
328 | Standard_EXPORT TCollection_AsciiString pointSpriteShadingSrc (const TCollection_AsciiString theBaseColorSrc, const Standard_Integer theBits); |
329 | |
8625ef7e |
330 | //! Prepare standard GLSL program for textured font. |
331 | Standard_EXPORT Standard_Boolean prepareStdProgramFont(); |
332 | |
b86bb3df |
333 | //! Prepare standard GLSL program for FBO blit operation. |
334 | Standard_EXPORT Standard_Boolean prepareStdProgramFboBlit(); |
335 | |
8625ef7e |
336 | //! Prepare standard GLSL program without lighting. |
337 | Standard_EXPORT Standard_Boolean prepareStdProgramFlat (Handle(OpenGl_ShaderProgram)& theProgram, |
338 | const Standard_Integer theBits); |
339 | |
340 | //! Prepare standard GLSL program with lighting. |
341 | Standard_Boolean prepareStdProgramLight (Handle(OpenGl_ShaderProgram)& theProgram, |
342 | const Standard_Integer theBits) |
343 | { |
c357e426 |
344 | return myShadingModel == Graphic3d_TOSM_FRAGMENT |
8625ef7e |
345 | ? prepareStdProgramPhong (theProgram, theBits) |
346 | : prepareStdProgramGouraud (theProgram, theBits); |
347 | } |
348 | |
349 | //! Prepare standard GLSL program with per-vertex lighting. |
350 | Standard_EXPORT Standard_Boolean prepareStdProgramGouraud (Handle(OpenGl_ShaderProgram)& theProgram, |
351 | const Standard_Integer theBits); |
352 | |
353 | //! Prepare standard GLSL program with per-pixel lighting. |
354 | Standard_EXPORT Standard_Boolean prepareStdProgramPhong (Handle(OpenGl_ShaderProgram)& theProgram, |
355 | const Standard_Integer theBits); |
356 | |
256f9ac0 |
357 | //! Define computeLighting GLSL function depending on current lights configuration |
abdf0b10 |
358 | //! @param theHasVertColor flag to use getVertColor() instead of Ambient and Diffuse components of active material |
359 | Standard_EXPORT TCollection_AsciiString stdComputeLighting (const Standard_Boolean theHasVertColor); |
256f9ac0 |
360 | |
8625ef7e |
361 | //! Bind specified program to current context and apply state. |
299e0ab9 |
362 | Standard_EXPORT Standard_Boolean bindProgramWithState (const Handle(OpenGl_ShaderProgram)& theProgram); |
8625ef7e |
363 | |
256f9ac0 |
364 | //! Set pointer myLightPrograms to active lighting programs set from myMapOfLightPrograms |
365 | Standard_EXPORT void switchLightPrograms(); |
366 | |
f978241f |
367 | //! Prepare standard GLSL program for stereoscopic image. |
368 | Standard_EXPORT Standard_Boolean prepareStdProgramStereo (Handle(OpenGl_ShaderProgram)& theProgram, |
369 | const Graphic3d_StereoMode theStereoMode); |
38a0206f |
370 | |
8625ef7e |
371 | protected: |
372 | |
c357e426 |
373 | Graphic3d_TypeOfShadingModel myShadingModel; //!< lighting shading model |
256f9ac0 |
374 | OpenGl_ShaderProgramList myProgramList; //!< The list of shader programs |
375 | Handle(OpenGl_SetOfShaderPrograms) myLightPrograms; //!< pointer to active lighting programs matrix |
376 | OpenGl_SetOfShaderPrograms myFlatPrograms; //!< programs matrix without lighting |
377 | Handle(OpenGl_ShaderProgram) myFontProgram; //!< standard program for textured text |
b86bb3df |
378 | Handle(OpenGl_ShaderProgram) myBlitProgram; //!< standard program for FBO blit emulation |
256f9ac0 |
379 | OpenGl_MapOfShaderPrograms myMapOfLightPrograms; //!< map of lighting programs depending on shading model and lights configuration |
8625ef7e |
380 | |
f978241f |
381 | Handle(OpenGl_ShaderProgram) myStereoPrograms[Graphic3d_StereoMode_NB]; //!< standard stereo programs |
38a0206f |
382 | |
256f9ac0 |
383 | OpenGl_Context* myContext; //!< OpenGL context |
8625ef7e |
384 | |
385 | protected: |
386 | |
256f9ac0 |
387 | OpenGl_ProjectionState myProjectionState; //!< State of OCCT projection transformation |
388 | OpenGl_ModelWorldState myModelWorldState; //!< State of OCCT model-world transformation |
389 | OpenGl_WorldViewState myWorldViewState; //!< State of OCCT world-view transformation |
8cf06aa2 |
390 | OpenGl_ClippingState myClippingState; //!< State of OCCT clipping planes |
256f9ac0 |
391 | OpenGl_LightSourceState myLightSourceState; //!< State of OCCT light sources |
30f0ad28 |
392 | |
393 | private: |
394 | |
256f9ac0 |
395 | const OpenGl_View* myLastView; //!< Pointer to the last view shader manager used with |
8625ef7e |
396 | |
397 | public: |
398 | |
92efcf78 |
399 | DEFINE_STANDARD_RTTIEXT(OpenGl_ShaderManager,Standard_Transient) |
30f0ad28 |
400 | }; |
401 | |
402 | #endif // _OpenGl_ShaderManager_HeaderFile |