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> |
30f0ad28 |
21 | |
22 | #include <NCollection_DataMap.hxx> |
23 | #include <NCollection_Sequence.hxx> |
24 | |
256f9ac0 |
25 | #include <OpenGl_SetOfShaderPrograms.hxx> |
30f0ad28 |
26 | #include <OpenGl_ShaderStates.hxx> |
bf5f0ca2 |
27 | #include <OpenGl_Aspects.hxx> |
8613985b |
28 | #include <OpenGl_MaterialState.hxx> |
8625ef7e |
29 | #include <OpenGl_Texture.hxx> |
c357e426 |
30 | |
e6804ff7 |
31 | class OpenGl_View; |
98b15dbf |
32 | class OpenGl_VertexBuffer; |
e6804ff7 |
33 | |
30f0ad28 |
34 | //! List of shader programs. |
35 | typedef NCollection_Sequence<Handle(OpenGl_ShaderProgram)> OpenGl_ShaderProgramList; |
36 | |
30f0ad28 |
37 | //! This class is responsible for managing shader programs. |
38 | class OpenGl_ShaderManager : public Standard_Transient |
39 | { |
8613985b |
40 | DEFINE_STANDARD_RTTIEXT(OpenGl_ShaderManager, Standard_Transient) |
30f0ad28 |
41 | friend class OpenGl_ShaderProgram; |
30f0ad28 |
42 | public: |
43 | |
44 | //! Creates new empty shader manager. |
45 | Standard_EXPORT OpenGl_ShaderManager (OpenGl_Context* theContext); |
46 | |
47 | //! Releases resources of shader manager. |
48 | Standard_EXPORT virtual ~OpenGl_ShaderManager(); |
49 | |
05e2200b |
50 | //! Release all resources. |
51 | Standard_EXPORT void clear(); |
52 | |
7c3ef2f7 |
53 | //! Return local camera transformation. |
54 | const gp_XYZ& LocalOrigin() const { return myLocalOrigin; } |
55 | |
56 | //! Setup local camera transformation for compensating float precision issues. |
57 | void SetLocalOrigin (const gp_XYZ& theOrigin) |
58 | { |
59 | myLocalOrigin = theOrigin; |
60 | myHasLocalOrigin = !theOrigin.IsEqual (gp_XYZ(0.0, 0.0, 0.0), gp::Resolution()); |
61 | } |
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 |
cc8cbabe |
83 | Standard_Boolean BindFaceProgram (const Handle(OpenGl_TextureSet)& theTextures, |
2a332745 |
84 | Graphic3d_TypeOfShadingModel theShadingModel, |
85 | Graphic3d_AlphaMode theAlphaMode, |
86 | Standard_Boolean theHasVertColor, |
87 | Standard_Boolean theEnableEnvMap, |
88 | const Handle(OpenGl_ShaderProgram)& theCustomProgram) |
89 | { |
6ef0d6f1 |
90 | return BindFaceProgram (theTextures, theShadingModel, theAlphaMode, Aspect_IS_SOLID, |
91 | theHasVertColor, theEnableEnvMap, false, theCustomProgram); |
2a332745 |
92 | } |
93 | |
94 | //! Bind program for filled primitives rendering |
95 | Standard_Boolean BindFaceProgram (const Handle(OpenGl_TextureSet)& theTextures, |
96 | Graphic3d_TypeOfShadingModel theShadingModel, |
97 | Graphic3d_AlphaMode theAlphaMode, |
98 | Aspect_InteriorStyle theInteriorStyle, |
99 | Standard_Boolean theHasVertColor, |
100 | Standard_Boolean theEnableEnvMap, |
101 | Standard_Boolean theEnableMeshEdges, |
299e0ab9 |
102 | const Handle(OpenGl_ShaderProgram)& theCustomProgram) |
8625ef7e |
103 | { |
104 | if (!theCustomProgram.IsNull() |
105 | || myContext->caps->ffpEnable) |
106 | { |
299e0ab9 |
107 | return bindProgramWithState (theCustomProgram); |
8625ef7e |
108 | } |
109 | |
dc89236f |
110 | const Graphic3d_TypeOfShadingModel aShadeModelOnFace = theShadingModel != Graphic3d_TOSM_UNLIT |
111 | && (theTextures.IsNull() || theTextures->IsModulate()) |
112 | ? theShadingModel |
113 | : Graphic3d_TOSM_UNLIT; |
2a332745 |
114 | const Standard_Integer aBits = getProgramBits (theTextures, theAlphaMode, theInteriorStyle, theHasVertColor, theEnableEnvMap, theEnableMeshEdges); |
dc89236f |
115 | Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (aShadeModelOnFace, aBits); |
299e0ab9 |
116 | return bindProgramWithState (aProgram); |
8625ef7e |
117 | } |
30f0ad28 |
118 | |
8625ef7e |
119 | //! Bind program for line rendering |
cc8cbabe |
120 | Standard_Boolean BindLineProgram (const Handle(OpenGl_TextureSet)& theTextures, |
dc89236f |
121 | const Aspect_TypeOfLine theLineType, |
122 | const Graphic3d_TypeOfShadingModel theShadingModel, |
c40eb6b9 |
123 | const Graphic3d_AlphaMode theAlphaMode, |
299e0ab9 |
124 | const Standard_Boolean theHasVertColor, |
125 | const Handle(OpenGl_ShaderProgram)& theCustomProgram) |
8625ef7e |
126 | { |
127 | if (!theCustomProgram.IsNull() |
128 | || myContext->caps->ffpEnable) |
129 | { |
299e0ab9 |
130 | return bindProgramWithState (theCustomProgram); |
8625ef7e |
131 | } |
132 | |
2a332745 |
133 | Standard_Integer aBits = getProgramBits (theTextures, theAlphaMode, Aspect_IS_SOLID, theHasVertColor, false, false); |
dc89236f |
134 | if (theLineType != Aspect_TOL_SOLID) |
ac116c22 |
135 | { |
136 | aBits |= OpenGl_PO_StippleLine; |
137 | } |
138 | |
dc89236f |
139 | Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theShadingModel, aBits); |
299e0ab9 |
140 | return bindProgramWithState (aProgram); |
8625ef7e |
141 | } |
30f0ad28 |
142 | |
8625ef7e |
143 | //! Bind program for point rendering |
737e9a8d |
144 | Standard_EXPORT Standard_Boolean BindMarkerProgram (const Handle(OpenGl_TextureSet)& theTextures, |
145 | Graphic3d_TypeOfShadingModel theShadingModel, |
146 | Graphic3d_AlphaMode theAlphaMode, |
147 | Standard_Boolean theHasVertColor, |
148 | const Handle(OpenGl_ShaderProgram)& theCustomProgram); |
8625ef7e |
149 | |
150 | //! Bind program for rendering alpha-textured font. |
299e0ab9 |
151 | Standard_Boolean BindFontProgram (const Handle(OpenGl_ShaderProgram)& theCustomProgram) |
8625ef7e |
152 | { |
153 | if (!theCustomProgram.IsNull() |
154 | || myContext->caps->ffpEnable) |
155 | { |
299e0ab9 |
156 | return bindProgramWithState (theCustomProgram); |
8625ef7e |
157 | } |
158 | |
159 | if (myFontProgram.IsNull()) |
160 | { |
161 | prepareStdProgramFont(); |
162 | } |
299e0ab9 |
163 | |
164 | return bindProgramWithState (myFontProgram); |
8625ef7e |
165 | } |
30f0ad28 |
166 | |
6ef0d6f1 |
167 | //! Bind program for outline rendering |
168 | Standard_Boolean BindOutlineProgram() |
169 | { |
170 | if (myContext->caps->ffpEnable) |
171 | { |
172 | return false; |
173 | } |
174 | |
175 | const Standard_Integer aBits = getProgramBits (Handle(OpenGl_TextureSet)(), Graphic3d_AlphaMode_Opaque, Aspect_IS_SOLID, false, false, false); |
176 | if (myOutlinePrograms.IsNull()) |
177 | { |
178 | myOutlinePrograms = new OpenGl_SetOfPrograms(); |
179 | } |
180 | Handle(OpenGl_ShaderProgram)& aProgram = myOutlinePrograms->ChangeValue (aBits); |
181 | if (aProgram.IsNull()) |
182 | { |
183 | prepareStdProgramUnlit (aProgram, aBits, true); |
184 | } |
185 | return bindProgramWithState (aProgram); |
186 | } |
187 | |
b86bb3df |
188 | //! Bind program for FBO blit operation. |
189 | Standard_Boolean BindFboBlitProgram() |
190 | { |
191 | if (myBlitProgram.IsNull()) |
192 | { |
193 | prepareStdProgramFboBlit(); |
194 | } |
195 | return !myBlitProgram.IsNull() |
196 | && myContext->BindProgram (myBlitProgram); |
197 | } |
198 | |
a1073ae2 |
199 | //! Bind program for blended order-independent transparency buffers compositing. |
200 | Standard_Boolean BindOitCompositingProgram (const Standard_Boolean theIsMSAAEnabled) |
201 | { |
202 | const Standard_Integer aProgramIdx = theIsMSAAEnabled ? 1 : 0; |
203 | if (myOitCompositingProgram[aProgramIdx].IsNull()) |
204 | { |
205 | prepareStdProgramOitCompositing (theIsMSAAEnabled); |
206 | } |
207 | |
208 | const Handle(OpenGl_ShaderProgram)& aProgram = myOitCompositingProgram [aProgramIdx]; |
209 | return !aProgram.IsNull() && myContext->BindProgram (aProgram); |
210 | } |
211 | |
f978241f |
212 | //! Bind program for rendering stereoscopic image. |
213 | Standard_Boolean BindStereoProgram (const Graphic3d_StereoMode theStereoMode) |
38a0206f |
214 | { |
f978241f |
215 | if (theStereoMode < 0 || theStereoMode >= Graphic3d_StereoMode_NB) |
38a0206f |
216 | { |
f978241f |
217 | return Standard_False; |
38a0206f |
218 | } |
f978241f |
219 | |
220 | if (myStereoPrograms[theStereoMode].IsNull()) |
221 | { |
222 | prepareStdProgramStereo (myStereoPrograms[theStereoMode], theStereoMode); |
223 | } |
224 | const Handle(OpenGl_ShaderProgram)& aProgram = myStereoPrograms[theStereoMode]; |
225 | return !aProgram.IsNull() |
226 | && myContext->BindProgram (aProgram); |
38a0206f |
227 | } |
228 | |
98b15dbf |
229 | //! Bind program for rendering bounding box. |
230 | Standard_Boolean BindBoundBoxProgram() |
231 | { |
232 | if (myBoundBoxProgram.IsNull()) |
233 | { |
234 | prepareStdProgramBoundBox(); |
235 | } |
236 | return bindProgramWithState (myBoundBoxProgram); |
237 | } |
238 | |
239 | //! Returns bounding box vertex buffer. |
240 | const Handle(OpenGl_VertexBuffer)& BoundBoxVertBuffer() const { return myBoundBoxVertBuffer; } |
241 | |
30f0ad28 |
242 | public: |
243 | |
244 | //! Returns current state of OCCT light sources. |
8613985b |
245 | const OpenGl_LightSourceState& LightSourceState() const { return myLightSourceState; } |
30f0ad28 |
246 | |
247 | //! Updates state of OCCT light sources. |
992ed6b3 |
248 | Standard_EXPORT void UpdateLightSourceStateTo (const Handle(Graphic3d_LightSet)& theLights); |
30f0ad28 |
249 | |
7c3ef2f7 |
250 | //! Invalidate state of OCCT light sources. |
251 | Standard_EXPORT void UpdateLightSourceState(); |
252 | |
30f0ad28 |
253 | //! Pushes current state of OCCT light sources to specified program. |
254 | Standard_EXPORT void PushLightSourceState (const Handle(OpenGl_ShaderProgram)& theProgram) const; |
255 | |
256 | public: |
257 | |
258 | //! Returns current state of OCCT projection transform. |
8613985b |
259 | const OpenGl_ProjectionState& ProjectionState() const { return myProjectionState; } |
30f0ad28 |
260 | |
261 | //! Updates state of OCCT projection transform. |
c827ea3a |
262 | Standard_EXPORT void UpdateProjectionStateTo (const OpenGl_Mat4& theProjectionMatrix); |
30f0ad28 |
263 | |
30f0ad28 |
264 | //! Pushes current state of OCCT projection transform to specified program. |
265 | Standard_EXPORT void PushProjectionState (const Handle(OpenGl_ShaderProgram)& theProgram) const; |
266 | |
267 | public: |
268 | |
269 | //! Returns current state of OCCT model-world transform. |
8613985b |
270 | const OpenGl_ModelWorldState& ModelWorldState() const { return myModelWorldState; } |
30f0ad28 |
271 | |
272 | //! Updates state of OCCT model-world transform. |
c827ea3a |
273 | Standard_EXPORT void UpdateModelWorldStateTo (const OpenGl_Mat4& theModelWorldMatrix); |
30f0ad28 |
274 | |
30f0ad28 |
275 | //! Pushes current state of OCCT model-world transform to specified program. |
276 | Standard_EXPORT void PushModelWorldState (const Handle(OpenGl_ShaderProgram)& theProgram) const; |
277 | |
278 | public: |
279 | |
280 | //! Returns current state of OCCT world-view transform. |
8613985b |
281 | const OpenGl_WorldViewState& WorldViewState() const { return myWorldViewState; } |
30f0ad28 |
282 | |
283 | //! Updates state of OCCT world-view transform. |
c827ea3a |
284 | Standard_EXPORT void UpdateWorldViewStateTo (const OpenGl_Mat4& theWorldViewMatrix); |
30f0ad28 |
285 | |
30f0ad28 |
286 | //! Pushes current state of OCCT world-view transform to specified program. |
287 | Standard_EXPORT void PushWorldViewState (const Handle(OpenGl_ShaderProgram)& theProgram) const; |
288 | |
289 | public: |
290 | |
291 | //! Updates state of OCCT clipping planes. |
292 | Standard_EXPORT void UpdateClippingState(); |
293 | |
294 | //! Reverts state of OCCT clipping planes. |
295 | Standard_EXPORT void RevertClippingState(); |
296 | |
297 | //! Pushes current state of OCCT clipping planes to specified program. |
298 | Standard_EXPORT void PushClippingState (const Handle(OpenGl_ShaderProgram)& theProgram) const; |
299 | |
8613985b |
300 | public: |
301 | |
302 | //! Returns current state of material. |
303 | const OpenGl_MaterialState& MaterialState() const { return myMaterialState; } |
304 | |
305 | //! Updates state of material. |
306 | void UpdateMaterialStateTo (const OpenGl_Material& theFrontMat, |
307 | const OpenGl_Material& theBackMat, |
c40eb6b9 |
308 | const float theAlphaCutoff, |
8613985b |
309 | const bool theToDistinguish, |
310 | const bool theToMapTexture) |
311 | { |
c40eb6b9 |
312 | myMaterialState.Set (theFrontMat, theBackMat, theAlphaCutoff, theToDistinguish, theToMapTexture); |
8613985b |
313 | myMaterialState.Update(); |
314 | } |
315 | |
316 | //! Updates state of material. |
317 | void UpdateMaterialState() |
318 | { |
319 | myMaterialState.Update(); |
320 | } |
321 | |
322 | //! Pushes current state of material to specified program. |
323 | void PushMaterialState (const Handle(OpenGl_ShaderProgram)& theProgram) const; |
324 | |
2a332745 |
325 | public: |
326 | |
327 | //! Setup interior style line edges variables. |
328 | Standard_EXPORT void PushInteriorState (const Handle(OpenGl_ShaderProgram)& theProgram, |
bf5f0ca2 |
329 | const Handle(Graphic3d_Aspects)& theAspect) const; |
2a332745 |
330 | |
a1073ae2 |
331 | public: |
332 | |
726b5d9e |
333 | //! Returns state of OIT uniforms. |
334 | const OpenGl_OitState& OitState() const { return myOitState; } |
335 | |
a1073ae2 |
336 | //! Set the state of OIT rendering pass. |
337 | //! @param theToEnableOitWrite [in] flag indicating whether the special output should be written for OIT algorithm. |
338 | //! @param theDepthFactor [in] the scalar factor of depth influence to the fragment's coverage. |
339 | void SetOitState (const bool theToEnableOitWrite, const float theDepthFactor) |
340 | { |
341 | myOitState.Set (theToEnableOitWrite, theDepthFactor); |
342 | myOitState.Update(); |
343 | } |
344 | |
345 | //! Pushes state of OIT uniforms to the specified program. |
346 | Standard_EXPORT void PushOitState (const Handle(OpenGl_ShaderProgram)& theProgram) const; |
347 | |
30f0ad28 |
348 | public: |
05e2200b |
349 | |
30f0ad28 |
350 | //! Pushes current state of OCCT graphics parameters to specified program. |
351 | Standard_EXPORT void PushState (const Handle(OpenGl_ShaderProgram)& theProgram) const; |
352 | |
392ac980 |
353 | public: |
354 | |
355 | //! Overwrites context |
356 | void SetContext (OpenGl_Context* theCtx) |
357 | { |
358 | myContext = theCtx; |
359 | } |
360 | |
c357e426 |
361 | //! Returns true when provided context is the same as used one by shader manager. |
362 | bool IsSameContext (OpenGl_Context* theCtx) const |
363 | { |
364 | return myContext == theCtx; |
365 | } |
366 | |
db5d29de |
367 | //! Choose Shading Model for filled primitives. |
368 | //! Fallbacks to FACET model if there are no normal attributes. |
369 | Graphic3d_TypeOfShadingModel ChooseFaceShadingModel (Graphic3d_TypeOfShadingModel theCustomModel, |
370 | bool theHasNodalNormals) const |
dc89236f |
371 | { |
372 | if (!myContext->ColorMask()) |
373 | { |
374 | return Graphic3d_TOSM_UNLIT; |
375 | } |
376 | Graphic3d_TypeOfShadingModel aModel = theCustomModel != Graphic3d_TOSM_DEFAULT ? theCustomModel : myShadingModel; |
377 | switch (aModel) |
378 | { |
379 | case Graphic3d_TOSM_DEFAULT: |
380 | case Graphic3d_TOSM_UNLIT: |
381 | case Graphic3d_TOSM_FACET: |
db5d29de |
382 | return aModel; |
dc89236f |
383 | case Graphic3d_TOSM_VERTEX: |
384 | case Graphic3d_TOSM_FRAGMENT: |
db5d29de |
385 | return theHasNodalNormals ? aModel : Graphic3d_TOSM_FACET; |
dc89236f |
386 | } |
db5d29de |
387 | return Graphic3d_TOSM_UNLIT; |
388 | } |
389 | |
390 | //! Choose Shading Model for line primitives. |
391 | //! Fallbacks to UNLIT model if there are no normal attributes. |
392 | Graphic3d_TypeOfShadingModel ChooseLineShadingModel (Graphic3d_TypeOfShadingModel theCustomModel, |
393 | bool theHasNodalNormals) const |
394 | { |
395 | if (!myContext->ColorMask()) |
396 | { |
397 | return Graphic3d_TOSM_UNLIT; |
398 | } |
399 | Graphic3d_TypeOfShadingModel aModel = theCustomModel != Graphic3d_TOSM_DEFAULT ? theCustomModel : myShadingModel; |
400 | switch (aModel) |
401 | { |
402 | case Graphic3d_TOSM_DEFAULT: |
403 | case Graphic3d_TOSM_UNLIT: |
404 | case Graphic3d_TOSM_FACET: |
405 | return Graphic3d_TOSM_UNLIT; |
406 | case Graphic3d_TOSM_VERTEX: |
407 | case Graphic3d_TOSM_FRAGMENT: |
408 | return theHasNodalNormals ? aModel : Graphic3d_TOSM_UNLIT; |
409 | } |
410 | return Graphic3d_TOSM_UNLIT; |
411 | } |
412 | |
413 | //! Choose Shading Model for Marker primitives. |
414 | Graphic3d_TypeOfShadingModel ChooseMarkerShadingModel (Graphic3d_TypeOfShadingModel theCustomModel, |
415 | bool theHasNodalNormals) const |
416 | { |
417 | return ChooseLineShadingModel (theCustomModel, theHasNodalNormals); |
dc89236f |
418 | } |
419 | |
420 | //! Returns default Shading Model. |
421 | Graphic3d_TypeOfShadingModel ShadingModel() const { return myShadingModel; } |
422 | |
8625ef7e |
423 | //! Sets shading model. |
c357e426 |
424 | Standard_EXPORT void SetShadingModel (const Graphic3d_TypeOfShadingModel theModel); |
8625ef7e |
425 | |
e6804ff7 |
426 | //! Sets last view manger used with. |
427 | //! Helps to handle matrix states in multi-view configurations. |
428 | void SetLastView (const OpenGl_View* theLastView) |
429 | { |
430 | myLastView = theLastView; |
431 | } |
432 | |
433 | //! Returns true when provided view is the same as cached one. |
434 | bool IsSameView (const OpenGl_View* theView) const |
435 | { |
436 | return myLastView == theView; |
437 | } |
438 | |
30f0ad28 |
439 | protected: |
440 | |
2a332745 |
441 | //! Define clipping planes program bits. |
442 | Standard_Integer getClipPlaneBits() const |
443 | { |
444 | const Standard_Integer aNbPlanes = myContext->Clipping().NbClippingOrCappingOn(); |
445 | if (aNbPlanes <= 0) |
446 | { |
447 | return 0; |
448 | } |
449 | |
450 | Standard_Integer aBits = 0; |
451 | if (myContext->Clipping().HasClippingChains()) |
452 | { |
453 | aBits |= OpenGl_PO_ClipChains; |
454 | } |
455 | |
456 | if (aNbPlanes == 1) |
457 | { |
458 | aBits |= OpenGl_PO_ClipPlanes1; |
459 | } |
460 | else if (aNbPlanes == 2) |
461 | { |
462 | aBits |= OpenGl_PO_ClipPlanes2; |
463 | } |
464 | else |
465 | { |
466 | aBits |= OpenGl_PO_ClipPlanesN; |
467 | } |
468 | return aBits; |
469 | } |
470 | |
8625ef7e |
471 | //! Define program bits. |
cc8cbabe |
472 | Standard_Integer getProgramBits (const Handle(OpenGl_TextureSet)& theTextures, |
c40eb6b9 |
473 | Graphic3d_AlphaMode theAlphaMode, |
2a332745 |
474 | Aspect_InteriorStyle theInteriorStyle, |
c40eb6b9 |
475 | Standard_Boolean theHasVertColor, |
2a332745 |
476 | Standard_Boolean theEnableEnvMap, |
477 | Standard_Boolean theEnableMeshEdges) const |
8625ef7e |
478 | { |
479 | Standard_Integer aBits = 0; |
c40eb6b9 |
480 | if (theAlphaMode == Graphic3d_AlphaMode_Mask) |
481 | { |
482 | aBits |= OpenGl_PO_AlphaTest; |
483 | } |
1a7ece8f |
484 | |
2a332745 |
485 | aBits |= getClipPlaneBits(); |
486 | if (theEnableMeshEdges |
487 | && myContext->hasGeometryStage != OpenGl_FeatureNotAvailable) |
8625ef7e |
488 | { |
2a332745 |
489 | aBits |= OpenGl_PO_MeshEdges; |
490 | if (theInteriorStyle == Aspect_IS_HOLLOW) |
1a7ece8f |
491 | { |
2a332745 |
492 | aBits |= OpenGl_PO_AlphaTest; |
1a7ece8f |
493 | } |
8625ef7e |
494 | } |
1a7ece8f |
495 | |
83da37b1 |
496 | if (theEnableEnvMap) |
e135a155 |
497 | { |
498 | // Environment map overwrites material texture |
499 | aBits |= OpenGl_PO_TextureEnv; |
500 | } |
cc8cbabe |
501 | else if (!theTextures.IsNull() |
737e9a8d |
502 | && theTextures->HasNonPointSprite()) |
8625ef7e |
503 | { |
737e9a8d |
504 | aBits |= OpenGl_PO_TextureRGB; |
8625ef7e |
505 | } |
2a332745 |
506 | if (theHasVertColor |
507 | && theInteriorStyle != Aspect_IS_HIDDENLINE) |
8625ef7e |
508 | { |
509 | aBits |= OpenGl_PO_VertColor; |
510 | } |
a1073ae2 |
511 | |
512 | if (myOitState.ToEnableWrite()) |
513 | { |
514 | aBits |= OpenGl_PO_WriteOit; |
515 | } |
8625ef7e |
516 | return aBits; |
517 | } |
518 | |
519 | //! Prepare standard GLSL program. |
dc89236f |
520 | Handle(OpenGl_ShaderProgram)& getStdProgram (Graphic3d_TypeOfShadingModel theShadingModel, |
521 | Standard_Integer theBits) |
8625ef7e |
522 | { |
dc89236f |
523 | if (theShadingModel == Graphic3d_TOSM_UNLIT |
524 | || (theBits & OpenGl_PO_TextureEnv) != 0) |
8625ef7e |
525 | { |
dc89236f |
526 | // If environment map is enabled lighting calculations are |
527 | // not needed (in accordance with default OCCT behavior) |
6ef0d6f1 |
528 | Handle(OpenGl_ShaderProgram)& aProgram = myUnlitPrograms->ChangeValue (theBits); |
8625ef7e |
529 | if (aProgram.IsNull()) |
530 | { |
6ef0d6f1 |
531 | prepareStdProgramUnlit (aProgram, theBits, false); |
8625ef7e |
532 | } |
533 | return aProgram; |
534 | } |
535 | |
dc89236f |
536 | Handle(OpenGl_ShaderProgram)& aProgram = myLightPrograms->ChangeValue (theShadingModel, theBits); |
8625ef7e |
537 | if (aProgram.IsNull()) |
538 | { |
dc89236f |
539 | prepareStdProgramLight (aProgram, theShadingModel, theBits); |
8625ef7e |
540 | } |
541 | return aProgram; |
542 | } |
543 | |
299e0ab9 |
544 | //! Prepare standard GLSL program for accessing point sprite alpha. |
737e9a8d |
545 | Standard_EXPORT TCollection_AsciiString pointSpriteAlphaSrc (Standard_Integer theBits); |
299e0ab9 |
546 | |
547 | //! Prepare standard GLSL program for computing point sprite shading. |
737e9a8d |
548 | Standard_EXPORT TCollection_AsciiString pointSpriteShadingSrc (const TCollection_AsciiString& theBaseColorSrc, |
549 | Standard_Integer theBits); |
299e0ab9 |
550 | |
8625ef7e |
551 | //! Prepare standard GLSL program for textured font. |
552 | Standard_EXPORT Standard_Boolean prepareStdProgramFont(); |
553 | |
b86bb3df |
554 | //! Prepare standard GLSL program for FBO blit operation. |
555 | Standard_EXPORT Standard_Boolean prepareStdProgramFboBlit(); |
556 | |
a1073ae2 |
557 | //! Prepare standard GLSL programs for OIT compositing operation. |
558 | Standard_EXPORT Standard_Boolean prepareStdProgramOitCompositing (const Standard_Boolean theMsaa); |
559 | |
8625ef7e |
560 | //! Prepare standard GLSL program without lighting. |
dc89236f |
561 | Standard_EXPORT Standard_Boolean prepareStdProgramUnlit (Handle(OpenGl_ShaderProgram)& theProgram, |
6ef0d6f1 |
562 | Standard_Integer theBits, |
563 | Standard_Boolean theIsOutline = false); |
8625ef7e |
564 | |
565 | //! Prepare standard GLSL program with lighting. |
566 | Standard_Boolean prepareStdProgramLight (Handle(OpenGl_ShaderProgram)& theProgram, |
dc89236f |
567 | Graphic3d_TypeOfShadingModel theShadingModel, |
568 | Standard_Integer theBits) |
8625ef7e |
569 | { |
dc89236f |
570 | switch (theShadingModel) |
8c3237d4 |
571 | { |
6ef0d6f1 |
572 | case Graphic3d_TOSM_UNLIT: return prepareStdProgramUnlit (theProgram, theBits, false); |
8c3237d4 |
573 | case Graphic3d_TOSM_FACET: return prepareStdProgramPhong (theProgram, theBits, true); |
574 | case Graphic3d_TOSM_VERTEX: return prepareStdProgramGouraud(theProgram, theBits); |
dc89236f |
575 | case Graphic3d_TOSM_DEFAULT: |
8c3237d4 |
576 | case Graphic3d_TOSM_FRAGMENT: return prepareStdProgramPhong (theProgram, theBits, false); |
577 | } |
578 | return false; |
8625ef7e |
579 | } |
580 | |
581 | //! Prepare standard GLSL program with per-vertex lighting. |
582 | Standard_EXPORT Standard_Boolean prepareStdProgramGouraud (Handle(OpenGl_ShaderProgram)& theProgram, |
583 | const Standard_Integer theBits); |
584 | |
585 | //! Prepare standard GLSL program with per-pixel lighting. |
8c3237d4 |
586 | //! @param theIsFlatNormal when TRUE, the Vertex normals will be ignored and Face normal will be computed instead |
8625ef7e |
587 | Standard_EXPORT Standard_Boolean prepareStdProgramPhong (Handle(OpenGl_ShaderProgram)& theProgram, |
8c3237d4 |
588 | const Standard_Integer theBits, |
589 | const Standard_Boolean theIsFlatNormal = false); |
8625ef7e |
590 | |
256f9ac0 |
591 | //! Define computeLighting GLSL function depending on current lights configuration |
daf73ab7 |
592 | //! @param theNbLights [out] number of defined light sources |
593 | //! @param theHasVertColor [in] flag to use getVertColor() instead of Ambient and Diffuse components of active material |
594 | Standard_EXPORT TCollection_AsciiString stdComputeLighting (Standard_Integer& theNbLights, |
595 | Standard_Boolean theHasVertColor); |
256f9ac0 |
596 | |
8625ef7e |
597 | //! Bind specified program to current context and apply state. |
299e0ab9 |
598 | Standard_EXPORT Standard_Boolean bindProgramWithState (const Handle(OpenGl_ShaderProgram)& theProgram); |
8625ef7e |
599 | |
256f9ac0 |
600 | //! Set pointer myLightPrograms to active lighting programs set from myMapOfLightPrograms |
601 | Standard_EXPORT void switchLightPrograms(); |
602 | |
f978241f |
603 | //! Prepare standard GLSL program for stereoscopic image. |
604 | Standard_EXPORT Standard_Boolean prepareStdProgramStereo (Handle(OpenGl_ShaderProgram)& theProgram, |
605 | const Graphic3d_StereoMode theStereoMode); |
38a0206f |
606 | |
98b15dbf |
607 | //! Prepare standard GLSL program for bounding box. |
608 | Standard_EXPORT Standard_Boolean prepareStdProgramBoundBox(); |
609 | |
2a332745 |
610 | //! Prepare GLSL version header. |
611 | Standard_EXPORT Standard_Integer defaultGlslVersion (const Handle(Graphic3d_ShaderProgram)& theProgram, |
d95f5ce1 |
612 | const TCollection_AsciiString& theName, |
2a332745 |
613 | Standard_Integer theBits, |
614 | bool theUsesDerivates = false) const; |
615 | |
616 | //! Prepare GLSL source for geometry shader according to parameters. |
617 | Standard_EXPORT TCollection_AsciiString prepareGeomMainSrc (OpenGl_ShaderObject::ShaderVariableList& theUnifoms, |
618 | OpenGl_ShaderObject::ShaderVariableList& theStageInOuts, |
619 | Standard_Integer theBits); |
620 | |
7c3ef2f7 |
621 | protected: |
622 | |
623 | //! Packed properties of light source |
624 | struct OpenGl_ShaderLightParameters |
625 | { |
626 | OpenGl_Vec4 Color; |
627 | OpenGl_Vec4 Position; |
628 | OpenGl_Vec4 Direction; |
629 | OpenGl_Vec4 Parameters; |
630 | |
631 | //! Returns packed (serialized) representation of light source properties |
632 | const OpenGl_Vec4* Packed() const { return reinterpret_cast<const OpenGl_Vec4*> (this); } |
633 | static Standard_Integer NbOfVec4() { return 4; } |
634 | }; |
635 | |
636 | //! Packed light source type information |
637 | struct OpenGl_ShaderLightType |
638 | { |
639 | Standard_Integer Type; |
640 | Standard_Integer IsHeadlight; |
641 | |
642 | //! Returns packed (serialized) representation of light source type |
643 | const OpenGl_Vec2i* Packed() const { return reinterpret_cast<const OpenGl_Vec2i*> (this); } |
644 | static Standard_Integer NbOfVec2i() { return 1; } |
645 | }; |
646 | |
8613985b |
647 | //! Fake OpenGL program for tracking FFP state in the way consistent to programmable pipeline. |
648 | class OpenGl_ShaderProgramFFP : public OpenGl_ShaderProgram |
649 | { |
650 | DEFINE_STANDARD_RTTI_INLINE(OpenGl_ShaderProgramFFP, OpenGl_ShaderProgram) |
651 | friend class OpenGl_ShaderManager; |
652 | protected: |
653 | OpenGl_ShaderProgramFFP() {} |
654 | }; |
655 | |
25c35042 |
656 | protected: |
657 | |
658 | //! Append clipping plane definition to temporary buffers. |
659 | void addClippingPlane (Standard_Integer& thePlaneId, |
660 | const Graphic3d_ClipPlane& thePlane, |
661 | const Graphic3d_Vec4d& theEq, |
662 | const Standard_Integer theChainFwd) const |
663 | { |
664 | myClipChainArray.SetValue (thePlaneId, theChainFwd); |
665 | OpenGl_Vec4& aPlaneEq = myClipPlaneArray.ChangeValue (thePlaneId); |
666 | aPlaneEq.x() = float(theEq.x()); |
667 | aPlaneEq.y() = float(theEq.y()); |
668 | aPlaneEq.z() = float(theEq.z()); |
669 | aPlaneEq.w() = float(theEq.w()); |
670 | if (myHasLocalOrigin) |
671 | { |
672 | const gp_XYZ aPos = thePlane.ToPlane().Position().Location().XYZ() - myLocalOrigin; |
673 | const Standard_Real aD = -(theEq.x() * aPos.X() + theEq.y() * aPos.Y() + theEq.z() * aPos.Z()); |
674 | aPlaneEq.w() = float(aD); |
675 | } |
676 | ++thePlaneId; |
677 | } |
678 | |
8625ef7e |
679 | protected: |
680 | |
8613985b |
681 | Handle(OpenGl_ShaderProgramFFP) myFfpProgram; |
682 | |
c357e426 |
683 | Graphic3d_TypeOfShadingModel myShadingModel; //!< lighting shading model |
256f9ac0 |
684 | OpenGl_ShaderProgramList myProgramList; //!< The list of shader programs |
685 | Handle(OpenGl_SetOfShaderPrograms) myLightPrograms; //!< pointer to active lighting programs matrix |
6ef0d6f1 |
686 | Handle(OpenGl_SetOfPrograms) myUnlitPrograms; //!< programs matrix without lighting |
687 | Handle(OpenGl_SetOfPrograms) myOutlinePrograms; //!< programs matrix without lighting for outline presentation |
256f9ac0 |
688 | Handle(OpenGl_ShaderProgram) myFontProgram; //!< standard program for textured text |
b86bb3df |
689 | Handle(OpenGl_ShaderProgram) myBlitProgram; //!< standard program for FBO blit emulation |
98b15dbf |
690 | Handle(OpenGl_ShaderProgram) myBoundBoxProgram; //!< standard program for bounding box |
a1073ae2 |
691 | Handle(OpenGl_ShaderProgram) myOitCompositingProgram[2]; //!< standard program for OIT compositing (default and MSAA). |
dc89236f |
692 | OpenGl_MapOfShaderPrograms myMapOfLightPrograms; //!< map of lighting programs depending on lights configuration |
8625ef7e |
693 | |
f978241f |
694 | Handle(OpenGl_ShaderProgram) myStereoPrograms[Graphic3d_StereoMode_NB]; //!< standard stereo programs |
38a0206f |
695 | |
98b15dbf |
696 | Handle(OpenGl_VertexBuffer) myBoundBoxVertBuffer; //!< bounding box vertex buffer |
697 | |
256f9ac0 |
698 | OpenGl_Context* myContext; //!< OpenGL context |
8625ef7e |
699 | |
700 | protected: |
701 | |
256f9ac0 |
702 | OpenGl_ProjectionState myProjectionState; //!< State of OCCT projection transformation |
703 | OpenGl_ModelWorldState myModelWorldState; //!< State of OCCT model-world transformation |
704 | OpenGl_WorldViewState myWorldViewState; //!< State of OCCT world-view transformation |
8cf06aa2 |
705 | OpenGl_ClippingState myClippingState; //!< State of OCCT clipping planes |
256f9ac0 |
706 | OpenGl_LightSourceState myLightSourceState; //!< State of OCCT light sources |
8613985b |
707 | OpenGl_MaterialState myMaterialState; //!< State of Front and Back materials |
a1073ae2 |
708 | OpenGl_OitState myOitState; //!< State of OIT uniforms |
8613985b |
709 | |
7c3ef2f7 |
710 | gp_XYZ myLocalOrigin; //!< local camera transformation |
711 | Standard_Boolean myHasLocalOrigin; //!< flag indicating that local camera transformation has been set |
712 | |
daf73ab7 |
713 | mutable NCollection_Array1<OpenGl_ShaderLightType> myLightTypeArray; |
714 | mutable NCollection_Array1<OpenGl_ShaderLightParameters> myLightParamsArray; |
715 | mutable NCollection_Array1<OpenGl_Vec4> myClipPlaneArray; |
716 | mutable NCollection_Array1<OpenGl_Vec4d> myClipPlaneArrayFfp; |
25c35042 |
717 | mutable NCollection_Array1<Standard_Integer> myClipChainArray; |
30f0ad28 |
718 | |
719 | private: |
720 | |
256f9ac0 |
721 | const OpenGl_View* myLastView; //!< Pointer to the last view shader manager used with |
8625ef7e |
722 | |
30f0ad28 |
723 | }; |
724 | |
8613985b |
725 | DEFINE_STANDARD_HANDLE(OpenGl_ShaderManager, Standard_Transient) |
726 | |
30f0ad28 |
727 | #endif // _OpenGl_ShaderManager_HeaderFile |