0030745: Visualization, TKOpenGl - fix initialization of GLES3 context
[occt.git] / src / OpenGl / OpenGl_ShaderManager.hxx
CommitLineData
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 31class OpenGl_View;
98b15dbf 32class OpenGl_VertexBuffer;
e6804ff7 33
30f0ad28 34//! List of shader programs.
35typedef NCollection_Sequence<Handle(OpenGl_ShaderProgram)> OpenGl_ShaderProgramList;
36
30f0ad28 37//! This class is responsible for managing shader programs.
38class OpenGl_ShaderManager : public Standard_Transient
39{
8613985b 40 DEFINE_STANDARD_RTTIEXT(OpenGl_ShaderManager, Standard_Transient)
30f0ad28 41 friend class OpenGl_ShaderProgram;
30f0ad28 42public:
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 242public:
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
8cf3bf84 253 //! Pushes current state of OCCT light sources to specified program (only on state change).
254 void PushLightSourceState (const Handle(OpenGl_ShaderProgram)& theProgram) const
255 {
256 if (myLightSourceState.Index() != theProgram->ActiveState (OpenGl_LIGHT_SOURCES_STATE))
257 {
258 pushLightSourceState (theProgram);
259 }
260 }
261
30f0ad28 262 //! Pushes current state of OCCT light sources to specified program.
8cf3bf84 263 Standard_EXPORT void pushLightSourceState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
30f0ad28 264
265public:
266
267 //! Returns current state of OCCT projection transform.
8613985b 268 const OpenGl_ProjectionState& ProjectionState() const { return myProjectionState; }
30f0ad28 269
270 //! Updates state of OCCT projection transform.
c827ea3a 271 Standard_EXPORT void UpdateProjectionStateTo (const OpenGl_Mat4& theProjectionMatrix);
30f0ad28 272
8cf3bf84 273 //! Pushes current state of OCCT projection transform to specified program (only on state change).
274 void PushProjectionState (const Handle(OpenGl_ShaderProgram)& theProgram) const
275 {
276 if (myProjectionState.Index() != theProgram->ActiveState (OpenGl_PROJECTION_STATE))
277 {
278 pushProjectionState (theProgram);
279 }
280 }
281
30f0ad28 282 //! Pushes current state of OCCT projection transform to specified program.
8cf3bf84 283 Standard_EXPORT void pushProjectionState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
30f0ad28 284
285public:
286
287 //! Returns current state of OCCT model-world transform.
8613985b 288 const OpenGl_ModelWorldState& ModelWorldState() const { return myModelWorldState; }
30f0ad28 289
290 //! Updates state of OCCT model-world transform.
c827ea3a 291 Standard_EXPORT void UpdateModelWorldStateTo (const OpenGl_Mat4& theModelWorldMatrix);
30f0ad28 292
8cf3bf84 293 //! Pushes current state of OCCT model-world transform to specified program (only on state change).
294 void PushModelWorldState (const Handle(OpenGl_ShaderProgram)& theProgram) const
295 {
296 if (myModelWorldState.Index() != theProgram->ActiveState (OpenGl_MODEL_WORLD_STATE))
297 {
298 pushModelWorldState (theProgram);
299 }
300 }
301
30f0ad28 302 //! Pushes current state of OCCT model-world transform to specified program.
8cf3bf84 303 Standard_EXPORT void pushModelWorldState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
30f0ad28 304
305public:
306
307 //! Returns current state of OCCT world-view transform.
8613985b 308 const OpenGl_WorldViewState& WorldViewState() const { return myWorldViewState; }
30f0ad28 309
310 //! Updates state of OCCT world-view transform.
c827ea3a 311 Standard_EXPORT void UpdateWorldViewStateTo (const OpenGl_Mat4& theWorldViewMatrix);
30f0ad28 312
8cf3bf84 313 //! Pushes current state of OCCT world-view transform to specified program (only on state change).
314 void PushWorldViewState (const Handle(OpenGl_ShaderProgram)& theProgram) const
315 {
316 if (myWorldViewState.Index() != theProgram->ActiveState (OpenGl_WORLD_VIEW_STATE))
317 {
318 pushWorldViewState (theProgram);
319 }
320 }
321
30f0ad28 322 //! Pushes current state of OCCT world-view transform to specified program.
8cf3bf84 323 Standard_EXPORT void pushWorldViewState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
30f0ad28 324
325public:
326
327 //! Updates state of OCCT clipping planes.
328 Standard_EXPORT void UpdateClippingState();
329
330 //! Reverts state of OCCT clipping planes.
331 Standard_EXPORT void RevertClippingState();
332
8cf3bf84 333 //! Pushes current state of OCCT clipping planes to specified program (only on state change).
334 void PushClippingState (const Handle(OpenGl_ShaderProgram)& theProgram) const
335 {
336 if (myClippingState.Index() != theProgram->ActiveState (OpenGl_CLIP_PLANES_STATE))
337 {
338 pushClippingState (theProgram);
339 }
340 }
341
30f0ad28 342 //! Pushes current state of OCCT clipping planes to specified program.
8cf3bf84 343 Standard_EXPORT void pushClippingState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
30f0ad28 344
345public:
346
8613985b 347 //! Returns current state of material.
348 const OpenGl_MaterialState& MaterialState() const { return myMaterialState; }
349
350 //! Updates state of material.
351 void UpdateMaterialStateTo (const OpenGl_Material& theFrontMat,
352 const OpenGl_Material& theBackMat,
c40eb6b9 353 const float theAlphaCutoff,
8613985b 354 const bool theToDistinguish,
355 const bool theToMapTexture)
356 {
c40eb6b9 357 myMaterialState.Set (theFrontMat, theBackMat, theAlphaCutoff, theToDistinguish, theToMapTexture);
8613985b 358 myMaterialState.Update();
359 }
360
361 //! Updates state of material.
362 void UpdateMaterialState()
363 {
364 myMaterialState.Update();
365 }
366
8cf3bf84 367 //! Pushes current state of material to specified program (only on state change).
368 void PushMaterialState (const Handle(OpenGl_ShaderProgram)& theProgram) const
369 {
370 if (myMaterialState.Index() != theProgram->ActiveState (OpenGl_MATERIAL_STATE))
371 {
372 pushMaterialState (theProgram);
373 }
374 }
375
8613985b 376 //! Pushes current state of material to specified program.
8cf3bf84 377 Standard_EXPORT void pushMaterialState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
8613985b 378
379public:
380
2a332745 381 //! Setup interior style line edges variables.
382 Standard_EXPORT void PushInteriorState (const Handle(OpenGl_ShaderProgram)& theProgram,
bf5f0ca2 383 const Handle(Graphic3d_Aspects)& theAspect) const;
2a332745 384
385public:
386
726b5d9e 387 //! Returns state of OIT uniforms.
388 const OpenGl_OitState& OitState() const { return myOitState; }
389
8cf3bf84 390 //! Set the state of OIT rendering pass (only on state change).
a1073ae2 391 //! @param theToEnableOitWrite [in] flag indicating whether the special output should be written for OIT algorithm.
392 //! @param theDepthFactor [in] the scalar factor of depth influence to the fragment's coverage.
393 void SetOitState (const bool theToEnableOitWrite, const float theDepthFactor)
394 {
395 myOitState.Set (theToEnableOitWrite, theDepthFactor);
396 myOitState.Update();
397 }
398
399 //! Pushes state of OIT uniforms to the specified program.
8cf3bf84 400 void PushOitState (const Handle(OpenGl_ShaderProgram)& theProgram) const
401 {
402 if (theProgram->IsValid()
403 && myOitState.Index() != theProgram->ActiveState (OpenGL_OIT_STATE))
404 {
405 pushOitState (theProgram);
406 }
407 }
408
409 //! Pushes state of OIT uniforms to the specified program.
410 Standard_EXPORT void pushOitState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
a1073ae2 411
412public:
413
30f0ad28 414 //! Pushes current state of OCCT graphics parameters to specified program.
415 Standard_EXPORT void PushState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
416
392ac980 417public:
418
419 //! Overwrites context
420 void SetContext (OpenGl_Context* theCtx)
421 {
422 myContext = theCtx;
423 }
424
c357e426 425 //! Returns true when provided context is the same as used one by shader manager.
426 bool IsSameContext (OpenGl_Context* theCtx) const
427 {
428 return myContext == theCtx;
429 }
430
db5d29de 431 //! Choose Shading Model for filled primitives.
432 //! Fallbacks to FACET model if there are no normal attributes.
433 Graphic3d_TypeOfShadingModel ChooseFaceShadingModel (Graphic3d_TypeOfShadingModel theCustomModel,
434 bool theHasNodalNormals) const
dc89236f 435 {
436 if (!myContext->ColorMask())
437 {
438 return Graphic3d_TOSM_UNLIT;
439 }
440 Graphic3d_TypeOfShadingModel aModel = theCustomModel != Graphic3d_TOSM_DEFAULT ? theCustomModel : myShadingModel;
441 switch (aModel)
442 {
443 case Graphic3d_TOSM_DEFAULT:
444 case Graphic3d_TOSM_UNLIT:
445 case Graphic3d_TOSM_FACET:
db5d29de 446 return aModel;
dc89236f 447 case Graphic3d_TOSM_VERTEX:
448 case Graphic3d_TOSM_FRAGMENT:
db5d29de 449 return theHasNodalNormals ? aModel : Graphic3d_TOSM_FACET;
dc89236f 450 }
db5d29de 451 return Graphic3d_TOSM_UNLIT;
452 }
453
454 //! Choose Shading Model for line primitives.
455 //! Fallbacks to UNLIT model if there are no normal attributes.
456 Graphic3d_TypeOfShadingModel ChooseLineShadingModel (Graphic3d_TypeOfShadingModel theCustomModel,
457 bool theHasNodalNormals) const
458 {
459 if (!myContext->ColorMask())
460 {
461 return Graphic3d_TOSM_UNLIT;
462 }
463 Graphic3d_TypeOfShadingModel aModel = theCustomModel != Graphic3d_TOSM_DEFAULT ? theCustomModel : myShadingModel;
464 switch (aModel)
465 {
466 case Graphic3d_TOSM_DEFAULT:
467 case Graphic3d_TOSM_UNLIT:
468 case Graphic3d_TOSM_FACET:
469 return Graphic3d_TOSM_UNLIT;
470 case Graphic3d_TOSM_VERTEX:
471 case Graphic3d_TOSM_FRAGMENT:
472 return theHasNodalNormals ? aModel : Graphic3d_TOSM_UNLIT;
473 }
474 return Graphic3d_TOSM_UNLIT;
475 }
476
477 //! Choose Shading Model for Marker primitives.
478 Graphic3d_TypeOfShadingModel ChooseMarkerShadingModel (Graphic3d_TypeOfShadingModel theCustomModel,
479 bool theHasNodalNormals) const
480 {
481 return ChooseLineShadingModel (theCustomModel, theHasNodalNormals);
dc89236f 482 }
483
484 //! Returns default Shading Model.
485 Graphic3d_TypeOfShadingModel ShadingModel() const { return myShadingModel; }
486
8625ef7e 487 //! Sets shading model.
c357e426 488 Standard_EXPORT void SetShadingModel (const Graphic3d_TypeOfShadingModel theModel);
8625ef7e 489
e6804ff7 490 //! Sets last view manger used with.
491 //! Helps to handle matrix states in multi-view configurations.
492 void SetLastView (const OpenGl_View* theLastView)
493 {
494 myLastView = theLastView;
495 }
496
497 //! Returns true when provided view is the same as cached one.
498 bool IsSameView (const OpenGl_View* theView) const
499 {
500 return myLastView == theView;
501 }
502
30f0ad28 503protected:
504
2a332745 505 //! Define clipping planes program bits.
506 Standard_Integer getClipPlaneBits() const
507 {
508 const Standard_Integer aNbPlanes = myContext->Clipping().NbClippingOrCappingOn();
509 if (aNbPlanes <= 0)
510 {
511 return 0;
512 }
513
514 Standard_Integer aBits = 0;
515 if (myContext->Clipping().HasClippingChains())
516 {
517 aBits |= OpenGl_PO_ClipChains;
518 }
519
520 if (aNbPlanes == 1)
521 {
522 aBits |= OpenGl_PO_ClipPlanes1;
523 }
524 else if (aNbPlanes == 2)
525 {
526 aBits |= OpenGl_PO_ClipPlanes2;
527 }
528 else
529 {
530 aBits |= OpenGl_PO_ClipPlanesN;
531 }
532 return aBits;
533 }
534
8625ef7e 535 //! Define program bits.
cc8cbabe 536 Standard_Integer getProgramBits (const Handle(OpenGl_TextureSet)& theTextures,
c40eb6b9 537 Graphic3d_AlphaMode theAlphaMode,
2a332745 538 Aspect_InteriorStyle theInteriorStyle,
c40eb6b9 539 Standard_Boolean theHasVertColor,
2a332745 540 Standard_Boolean theEnableEnvMap,
541 Standard_Boolean theEnableMeshEdges) const
8625ef7e 542 {
543 Standard_Integer aBits = 0;
c40eb6b9 544 if (theAlphaMode == Graphic3d_AlphaMode_Mask)
545 {
546 aBits |= OpenGl_PO_AlphaTest;
547 }
1a7ece8f 548
2a332745 549 aBits |= getClipPlaneBits();
550 if (theEnableMeshEdges
551 && myContext->hasGeometryStage != OpenGl_FeatureNotAvailable)
8625ef7e 552 {
2a332745 553 aBits |= OpenGl_PO_MeshEdges;
554 if (theInteriorStyle == Aspect_IS_HOLLOW)
1a7ece8f 555 {
2a332745 556 aBits |= OpenGl_PO_AlphaTest;
1a7ece8f 557 }
8625ef7e 558 }
1a7ece8f 559
83da37b1 560 if (theEnableEnvMap)
e135a155 561 {
562 // Environment map overwrites material texture
563 aBits |= OpenGl_PO_TextureEnv;
564 }
cc8cbabe 565 else if (!theTextures.IsNull()
737e9a8d 566 && theTextures->HasNonPointSprite())
8625ef7e 567 {
737e9a8d 568 aBits |= OpenGl_PO_TextureRGB;
8625ef7e 569 }
2a332745 570 if (theHasVertColor
571 && theInteriorStyle != Aspect_IS_HIDDENLINE)
8625ef7e 572 {
573 aBits |= OpenGl_PO_VertColor;
574 }
a1073ae2 575
576 if (myOitState.ToEnableWrite())
577 {
578 aBits |= OpenGl_PO_WriteOit;
579 }
8625ef7e 580 return aBits;
581 }
582
583 //! Prepare standard GLSL program.
dc89236f 584 Handle(OpenGl_ShaderProgram)& getStdProgram (Graphic3d_TypeOfShadingModel theShadingModel,
585 Standard_Integer theBits)
8625ef7e 586 {
dc89236f 587 if (theShadingModel == Graphic3d_TOSM_UNLIT
588 || (theBits & OpenGl_PO_TextureEnv) != 0)
8625ef7e 589 {
dc89236f 590 // If environment map is enabled lighting calculations are
591 // not needed (in accordance with default OCCT behavior)
6ef0d6f1 592 Handle(OpenGl_ShaderProgram)& aProgram = myUnlitPrograms->ChangeValue (theBits);
8625ef7e 593 if (aProgram.IsNull())
594 {
6ef0d6f1 595 prepareStdProgramUnlit (aProgram, theBits, false);
8625ef7e 596 }
597 return aProgram;
598 }
599
dc89236f 600 Handle(OpenGl_ShaderProgram)& aProgram = myLightPrograms->ChangeValue (theShadingModel, theBits);
8625ef7e 601 if (aProgram.IsNull())
602 {
dc89236f 603 prepareStdProgramLight (aProgram, theShadingModel, theBits);
8625ef7e 604 }
605 return aProgram;
606 }
607
299e0ab9 608 //! Prepare standard GLSL program for accessing point sprite alpha.
737e9a8d 609 Standard_EXPORT TCollection_AsciiString pointSpriteAlphaSrc (Standard_Integer theBits);
299e0ab9 610
611 //! Prepare standard GLSL program for computing point sprite shading.
737e9a8d 612 Standard_EXPORT TCollection_AsciiString pointSpriteShadingSrc (const TCollection_AsciiString& theBaseColorSrc,
613 Standard_Integer theBits);
299e0ab9 614
8625ef7e 615 //! Prepare standard GLSL program for textured font.
616 Standard_EXPORT Standard_Boolean prepareStdProgramFont();
617
b86bb3df 618 //! Prepare standard GLSL program for FBO blit operation.
619 Standard_EXPORT Standard_Boolean prepareStdProgramFboBlit();
620
a1073ae2 621 //! Prepare standard GLSL programs for OIT compositing operation.
622 Standard_EXPORT Standard_Boolean prepareStdProgramOitCompositing (const Standard_Boolean theMsaa);
623
8625ef7e 624 //! Prepare standard GLSL program without lighting.
dc89236f 625 Standard_EXPORT Standard_Boolean prepareStdProgramUnlit (Handle(OpenGl_ShaderProgram)& theProgram,
6ef0d6f1 626 Standard_Integer theBits,
627 Standard_Boolean theIsOutline = false);
8625ef7e 628
629 //! Prepare standard GLSL program with lighting.
630 Standard_Boolean prepareStdProgramLight (Handle(OpenGl_ShaderProgram)& theProgram,
dc89236f 631 Graphic3d_TypeOfShadingModel theShadingModel,
632 Standard_Integer theBits)
8625ef7e 633 {
dc89236f 634 switch (theShadingModel)
8c3237d4 635 {
6ef0d6f1 636 case Graphic3d_TOSM_UNLIT: return prepareStdProgramUnlit (theProgram, theBits, false);
8c3237d4 637 case Graphic3d_TOSM_FACET: return prepareStdProgramPhong (theProgram, theBits, true);
638 case Graphic3d_TOSM_VERTEX: return prepareStdProgramGouraud(theProgram, theBits);
dc89236f 639 case Graphic3d_TOSM_DEFAULT:
8c3237d4 640 case Graphic3d_TOSM_FRAGMENT: return prepareStdProgramPhong (theProgram, theBits, false);
641 }
642 return false;
8625ef7e 643 }
644
645 //! Prepare standard GLSL program with per-vertex lighting.
646 Standard_EXPORT Standard_Boolean prepareStdProgramGouraud (Handle(OpenGl_ShaderProgram)& theProgram,
647 const Standard_Integer theBits);
648
649 //! Prepare standard GLSL program with per-pixel lighting.
8c3237d4 650 //! @param theIsFlatNormal when TRUE, the Vertex normals will be ignored and Face normal will be computed instead
8625ef7e 651 Standard_EXPORT Standard_Boolean prepareStdProgramPhong (Handle(OpenGl_ShaderProgram)& theProgram,
8c3237d4 652 const Standard_Integer theBits,
653 const Standard_Boolean theIsFlatNormal = false);
8625ef7e 654
256f9ac0 655 //! Define computeLighting GLSL function depending on current lights configuration
daf73ab7 656 //! @param theNbLights [out] number of defined light sources
657 //! @param theHasVertColor [in] flag to use getVertColor() instead of Ambient and Diffuse components of active material
658 Standard_EXPORT TCollection_AsciiString stdComputeLighting (Standard_Integer& theNbLights,
659 Standard_Boolean theHasVertColor);
256f9ac0 660
8625ef7e 661 //! Bind specified program to current context and apply state.
299e0ab9 662 Standard_EXPORT Standard_Boolean bindProgramWithState (const Handle(OpenGl_ShaderProgram)& theProgram);
8625ef7e 663
256f9ac0 664 //! Set pointer myLightPrograms to active lighting programs set from myMapOfLightPrograms
665 Standard_EXPORT void switchLightPrograms();
666
f978241f 667 //! Prepare standard GLSL program for stereoscopic image.
668 Standard_EXPORT Standard_Boolean prepareStdProgramStereo (Handle(OpenGl_ShaderProgram)& theProgram,
669 const Graphic3d_StereoMode theStereoMode);
38a0206f 670
98b15dbf 671 //! Prepare standard GLSL program for bounding box.
672 Standard_EXPORT Standard_Boolean prepareStdProgramBoundBox();
673
2a332745 674 //! Prepare GLSL version header.
675 Standard_EXPORT Standard_Integer defaultGlslVersion (const Handle(Graphic3d_ShaderProgram)& theProgram,
d95f5ce1 676 const TCollection_AsciiString& theName,
2a332745 677 Standard_Integer theBits,
678 bool theUsesDerivates = false) const;
679
680 //! Prepare GLSL source for geometry shader according to parameters.
681 Standard_EXPORT TCollection_AsciiString prepareGeomMainSrc (OpenGl_ShaderObject::ShaderVariableList& theUnifoms,
682 OpenGl_ShaderObject::ShaderVariableList& theStageInOuts,
683 Standard_Integer theBits);
684
8625ef7e 685protected:
686
7c3ef2f7 687 //! Packed properties of light source
688 struct OpenGl_ShaderLightParameters
689 {
690 OpenGl_Vec4 Color;
691 OpenGl_Vec4 Position;
692 OpenGl_Vec4 Direction;
693 OpenGl_Vec4 Parameters;
694
695 //! Returns packed (serialized) representation of light source properties
696 const OpenGl_Vec4* Packed() const { return reinterpret_cast<const OpenGl_Vec4*> (this); }
697 static Standard_Integer NbOfVec4() { return 4; }
698 };
699
700 //! Packed light source type information
701 struct OpenGl_ShaderLightType
702 {
703 Standard_Integer Type;
704 Standard_Integer IsHeadlight;
705
706 //! Returns packed (serialized) representation of light source type
707 const OpenGl_Vec2i* Packed() const { return reinterpret_cast<const OpenGl_Vec2i*> (this); }
708 static Standard_Integer NbOfVec2i() { return 1; }
709 };
710
8613985b 711 //! Fake OpenGL program for tracking FFP state in the way consistent to programmable pipeline.
712 class OpenGl_ShaderProgramFFP : public OpenGl_ShaderProgram
713 {
714 DEFINE_STANDARD_RTTI_INLINE(OpenGl_ShaderProgramFFP, OpenGl_ShaderProgram)
715 friend class OpenGl_ShaderManager;
716 protected:
717 OpenGl_ShaderProgramFFP() {}
718 };
719
7c3ef2f7 720protected:
721
25c35042 722 //! Append clipping plane definition to temporary buffers.
723 void addClippingPlane (Standard_Integer& thePlaneId,
724 const Graphic3d_ClipPlane& thePlane,
725 const Graphic3d_Vec4d& theEq,
726 const Standard_Integer theChainFwd) const
727 {
728 myClipChainArray.SetValue (thePlaneId, theChainFwd);
729 OpenGl_Vec4& aPlaneEq = myClipPlaneArray.ChangeValue (thePlaneId);
730 aPlaneEq.x() = float(theEq.x());
731 aPlaneEq.y() = float(theEq.y());
732 aPlaneEq.z() = float(theEq.z());
733 aPlaneEq.w() = float(theEq.w());
734 if (myHasLocalOrigin)
735 {
736 const gp_XYZ aPos = thePlane.ToPlane().Position().Location().XYZ() - myLocalOrigin;
737 const Standard_Real aD = -(theEq.x() * aPos.X() + theEq.y() * aPos.Y() + theEq.z() * aPos.Z());
738 aPlaneEq.w() = float(aD);
739 }
740 ++thePlaneId;
741 }
742
743protected:
744
8613985b 745 Handle(OpenGl_ShaderProgramFFP) myFfpProgram;
746
c357e426 747 Graphic3d_TypeOfShadingModel myShadingModel; //!< lighting shading model
256f9ac0 748 OpenGl_ShaderProgramList myProgramList; //!< The list of shader programs
749 Handle(OpenGl_SetOfShaderPrograms) myLightPrograms; //!< pointer to active lighting programs matrix
6ef0d6f1 750 Handle(OpenGl_SetOfPrograms) myUnlitPrograms; //!< programs matrix without lighting
751 Handle(OpenGl_SetOfPrograms) myOutlinePrograms; //!< programs matrix without lighting for outline presentation
256f9ac0 752 Handle(OpenGl_ShaderProgram) myFontProgram; //!< standard program for textured text
b86bb3df 753 Handle(OpenGl_ShaderProgram) myBlitProgram; //!< standard program for FBO blit emulation
98b15dbf 754 Handle(OpenGl_ShaderProgram) myBoundBoxProgram; //!< standard program for bounding box
a1073ae2 755 Handle(OpenGl_ShaderProgram) myOitCompositingProgram[2]; //!< standard program for OIT compositing (default and MSAA).
dc89236f 756 OpenGl_MapOfShaderPrograms myMapOfLightPrograms; //!< map of lighting programs depending on lights configuration
8625ef7e 757
f978241f 758 Handle(OpenGl_ShaderProgram) myStereoPrograms[Graphic3d_StereoMode_NB]; //!< standard stereo programs
38a0206f 759
98b15dbf 760 Handle(OpenGl_VertexBuffer) myBoundBoxVertBuffer; //!< bounding box vertex buffer
761
256f9ac0 762 OpenGl_Context* myContext; //!< OpenGL context
8625ef7e 763
764protected:
765
256f9ac0 766 OpenGl_ProjectionState myProjectionState; //!< State of OCCT projection transformation
767 OpenGl_ModelWorldState myModelWorldState; //!< State of OCCT model-world transformation
768 OpenGl_WorldViewState myWorldViewState; //!< State of OCCT world-view transformation
8cf06aa2 769 OpenGl_ClippingState myClippingState; //!< State of OCCT clipping planes
256f9ac0 770 OpenGl_LightSourceState myLightSourceState; //!< State of OCCT light sources
8613985b 771 OpenGl_MaterialState myMaterialState; //!< State of Front and Back materials
a1073ae2 772 OpenGl_OitState myOitState; //!< State of OIT uniforms
8613985b 773
7c3ef2f7 774 gp_XYZ myLocalOrigin; //!< local camera transformation
775 Standard_Boolean myHasLocalOrigin; //!< flag indicating that local camera transformation has been set
776
daf73ab7 777 mutable NCollection_Array1<OpenGl_ShaderLightType> myLightTypeArray;
778 mutable NCollection_Array1<OpenGl_ShaderLightParameters> myLightParamsArray;
779 mutable NCollection_Array1<OpenGl_Vec4> myClipPlaneArray;
780 mutable NCollection_Array1<OpenGl_Vec4d> myClipPlaneArrayFfp;
25c35042 781 mutable NCollection_Array1<Standard_Integer> myClipChainArray;
30f0ad28 782
783private:
784
256f9ac0 785 const OpenGl_View* myLastView; //!< Pointer to the last view shader manager used with
8625ef7e 786
30f0ad28 787};
788
8613985b 789DEFINE_STANDARD_HANDLE(OpenGl_ShaderManager, Standard_Transient)
790
30f0ad28 791#endif // _OpenGl_ShaderManager_HeaderFile