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