0028698: Visualization, Graphic3d_CView - mark methods MinMaxValues() and NumberOfDis...
[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>
c357e426 21#include <Graphic3d_TypeOfShadingModel.hxx>
30f0ad28 22
23#include <NCollection_DataMap.hxx>
24#include <NCollection_Sequence.hxx>
25
256f9ac0 26#include <OpenGl_SetOfShaderPrograms.hxx>
30f0ad28 27#include <OpenGl_ShaderStates.hxx>
8625ef7e 28#include <OpenGl_AspectFace.hxx>
29#include <OpenGl_AspectLine.hxx>
30#include <OpenGl_AspectText.hxx>
31#include <OpenGl_AspectMarker.hxx>
8613985b 32#include <OpenGl_MaterialState.hxx>
8625ef7e 33#include <OpenGl_Texture.hxx>
c357e426 34
e6804ff7 35class OpenGl_View;
36
30f0ad28 37//! List of shader programs.
38typedef NCollection_Sequence<Handle(OpenGl_ShaderProgram)> OpenGl_ShaderProgramList;
39
30f0ad28 40//! This class is responsible for managing shader programs.
41class OpenGl_ShaderManager : public Standard_Transient
42{
8613985b 43 DEFINE_STANDARD_RTTIEXT(OpenGl_ShaderManager, Standard_Transient)
30f0ad28 44 friend class OpenGl_ShaderProgram;
30f0ad28 45public:
46
47 //! Creates new empty shader manager.
48 Standard_EXPORT OpenGl_ShaderManager (OpenGl_Context* theContext);
49
50 //! Releases resources of shader manager.
51 Standard_EXPORT virtual ~OpenGl_ShaderManager();
52
05e2200b 53 //! Release all resources.
54 Standard_EXPORT void clear();
55
7c3ef2f7 56 //! Return local camera transformation.
57 const gp_XYZ& LocalOrigin() const { return myLocalOrigin; }
58
59 //! Setup local camera transformation for compensating float precision issues.
60 void SetLocalOrigin (const gp_XYZ& theOrigin)
61 {
62 myLocalOrigin = theOrigin;
63 myHasLocalOrigin = !theOrigin.IsEqual (gp_XYZ(0.0, 0.0, 0.0), gp::Resolution());
64 }
65
392ac980 66 //! Creates new shader program or re-use shared instance.
67 //! @param theProxy [IN] program definition
68 //! @param theShareKey [OUT] sharing key
69 //! @param theProgram [OUT] OpenGL program
8625ef7e 70 //! @return true on success
71 Standard_EXPORT Standard_Boolean Create (const Handle(Graphic3d_ShaderProgram)& theProxy,
72 TCollection_AsciiString& theShareKey,
73 Handle(OpenGl_ShaderProgram)& theProgram);
30f0ad28 74
75 //! Unregisters specified shader program.
392ac980 76 Standard_EXPORT void Unregister (TCollection_AsciiString& theShareKey,
77 Handle(OpenGl_ShaderProgram)& theProgram);
30f0ad28 78
79 //! Returns list of registered shader programs.
80 Standard_EXPORT const OpenGl_ShaderProgramList& ShaderPrograms() const;
81
82 //! Returns true if no program objects are registered in the manager.
83 Standard_EXPORT Standard_Boolean IsEmpty() const;
84
8625ef7e 85 //! Bind program for filled primitives rendering
299e0ab9 86 Standard_Boolean BindFaceProgram (const Handle(OpenGl_Texture)& theTexture,
87 const Standard_Boolean theToLightOn,
88 const Standard_Boolean theHasVertColor,
89 const Standard_Boolean theEnableEnvMap,
90 const Handle(OpenGl_ShaderProgram)& theCustomProgram)
8625ef7e 91 {
92 if (!theCustomProgram.IsNull()
93 || myContext->caps->ffpEnable)
94 {
299e0ab9 95 return bindProgramWithState (theCustomProgram);
8625ef7e 96 }
97
83da37b1 98 const Standard_Integer aBits = getProgramBits (theTexture, theHasVertColor, theEnableEnvMap);
8625ef7e 99 Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits);
299e0ab9 100 return bindProgramWithState (aProgram);
8625ef7e 101 }
30f0ad28 102
8625ef7e 103 //! Bind program for line rendering
299e0ab9 104 Standard_Boolean BindLineProgram (const Handle(OpenGl_Texture)& theTexture,
105 const Standard_Boolean theStipple,
106 const Standard_Boolean theToLightOn,
107 const Standard_Boolean theHasVertColor,
108 const Handle(OpenGl_ShaderProgram)& theCustomProgram)
8625ef7e 109 {
110 if (!theCustomProgram.IsNull()
111 || myContext->caps->ffpEnable)
112 {
299e0ab9 113 return bindProgramWithState (theCustomProgram);
8625ef7e 114 }
115
ac116c22 116 Standard_Integer aBits = getProgramBits (theTexture, theHasVertColor);
299e0ab9 117 if (theStipple)
ac116c22 118 {
119 aBits |= OpenGl_PO_StippleLine;
120 }
121
8625ef7e 122 Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits);
299e0ab9 123 return bindProgramWithState (aProgram);
8625ef7e 124 }
30f0ad28 125
8625ef7e 126 //! Bind program for point rendering
299e0ab9 127 Standard_Boolean BindMarkerProgram (const Handle(OpenGl_Texture)& theTexture,
128 const Standard_Boolean theToLightOn,
129 const Standard_Boolean theHasVertColor,
130 const Handle(OpenGl_ShaderProgram)& theCustomProgram)
8625ef7e 131 {
132 if (!theCustomProgram.IsNull()
133 || myContext->caps->ffpEnable)
134 {
299e0ab9 135 return bindProgramWithState (theCustomProgram);
8625ef7e 136 }
137
138 const Standard_Integer aBits = getProgramBits (theTexture, theHasVertColor) | OpenGl_PO_Point;
139 Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits);
299e0ab9 140 return bindProgramWithState (aProgram);
8625ef7e 141 }
142
143 //! Bind program for rendering alpha-textured font.
299e0ab9 144 Standard_Boolean BindFontProgram (const Handle(OpenGl_ShaderProgram)& theCustomProgram)
8625ef7e 145 {
146 if (!theCustomProgram.IsNull()
147 || myContext->caps->ffpEnable)
148 {
299e0ab9 149 return bindProgramWithState (theCustomProgram);
8625ef7e 150 }
151
152 if (myFontProgram.IsNull())
153 {
154 prepareStdProgramFont();
155 }
299e0ab9 156
157 return bindProgramWithState (myFontProgram);
8625ef7e 158 }
30f0ad28 159
b86bb3df 160 //! Bind program for FBO blit operation.
161 Standard_Boolean BindFboBlitProgram()
162 {
163 if (myBlitProgram.IsNull())
164 {
165 prepareStdProgramFboBlit();
166 }
167 return !myBlitProgram.IsNull()
168 && myContext->BindProgram (myBlitProgram);
169 }
170
f978241f 171 //! Bind program for rendering stereoscopic image.
172 Standard_Boolean BindStereoProgram (const Graphic3d_StereoMode theStereoMode)
38a0206f 173 {
f978241f 174 if (theStereoMode < 0 || theStereoMode >= Graphic3d_StereoMode_NB)
38a0206f 175 {
f978241f 176 return Standard_False;
38a0206f 177 }
f978241f 178
179 if (myStereoPrograms[theStereoMode].IsNull())
180 {
181 prepareStdProgramStereo (myStereoPrograms[theStereoMode], theStereoMode);
182 }
183 const Handle(OpenGl_ShaderProgram)& aProgram = myStereoPrograms[theStereoMode];
184 return !aProgram.IsNull()
185 && myContext->BindProgram (aProgram);
38a0206f 186 }
187
30f0ad28 188public:
189
190 //! Returns current state of OCCT light sources.
8613985b 191 const OpenGl_LightSourceState& LightSourceState() const { return myLightSourceState; }
30f0ad28 192
193 //! Updates state of OCCT light sources.
194 Standard_EXPORT void UpdateLightSourceStateTo (const OpenGl_ListOfLight* theLights);
195
7c3ef2f7 196 //! Invalidate state of OCCT light sources.
197 Standard_EXPORT void UpdateLightSourceState();
198
30f0ad28 199 //! Pushes current state of OCCT light sources to specified program.
200 Standard_EXPORT void PushLightSourceState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
201
202public:
203
204 //! Returns current state of OCCT projection transform.
8613985b 205 const OpenGl_ProjectionState& ProjectionState() const { return myProjectionState; }
30f0ad28 206
207 //! Updates state of OCCT projection transform.
c827ea3a 208 Standard_EXPORT void UpdateProjectionStateTo (const OpenGl_Mat4& theProjectionMatrix);
30f0ad28 209
30f0ad28 210 //! Pushes current state of OCCT projection transform to specified program.
211 Standard_EXPORT void PushProjectionState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
212
213public:
214
215 //! Returns current state of OCCT model-world transform.
8613985b 216 const OpenGl_ModelWorldState& ModelWorldState() const { return myModelWorldState; }
30f0ad28 217
218 //! Updates state of OCCT model-world transform.
c827ea3a 219 Standard_EXPORT void UpdateModelWorldStateTo (const OpenGl_Mat4& theModelWorldMatrix);
30f0ad28 220
30f0ad28 221 //! Pushes current state of OCCT model-world transform to specified program.
222 Standard_EXPORT void PushModelWorldState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
223
224public:
225
226 //! Returns current state of OCCT world-view transform.
8613985b 227 const OpenGl_WorldViewState& WorldViewState() const { return myWorldViewState; }
30f0ad28 228
229 //! Updates state of OCCT world-view transform.
c827ea3a 230 Standard_EXPORT void UpdateWorldViewStateTo (const OpenGl_Mat4& theWorldViewMatrix);
30f0ad28 231
30f0ad28 232 //! Pushes current state of OCCT world-view transform to specified program.
233 Standard_EXPORT void PushWorldViewState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
234
235public:
236
237 //! Updates state of OCCT clipping planes.
238 Standard_EXPORT void UpdateClippingState();
239
240 //! Reverts state of OCCT clipping planes.
241 Standard_EXPORT void RevertClippingState();
242
243 //! Pushes current state of OCCT clipping planes to specified program.
244 Standard_EXPORT void PushClippingState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
245
246public:
247
8613985b 248 //! Returns current state of material.
249 const OpenGl_MaterialState& MaterialState() const { return myMaterialState; }
250
251 //! Updates state of material.
252 void UpdateMaterialStateTo (const OpenGl_Material& theFrontMat,
253 const OpenGl_Material& theBackMat,
254 const bool theToDistinguish,
255 const bool theToMapTexture)
256 {
257 myMaterialState.Set (theFrontMat, theBackMat, theToDistinguish, theToMapTexture);
258 myMaterialState.Update();
259 }
260
261 //! Updates state of material.
262 void UpdateMaterialState()
263 {
264 myMaterialState.Update();
265 }
266
267 //! Pushes current state of material to specified program.
268 void PushMaterialState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
269
270public:
271
30f0ad28 272 //! Pushes current state of OCCT graphics parameters to specified program.
273 Standard_EXPORT void PushState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
274
392ac980 275public:
276
277 //! Overwrites context
278 void SetContext (OpenGl_Context* theCtx)
279 {
280 myContext = theCtx;
281 }
282
c357e426 283 //! Returns true when provided context is the same as used one by shader manager.
284 bool IsSameContext (OpenGl_Context* theCtx) const
285 {
286 return myContext == theCtx;
287 }
288
8625ef7e 289 //! Sets shading model.
c357e426 290 Standard_EXPORT void SetShadingModel (const Graphic3d_TypeOfShadingModel theModel);
8625ef7e 291
e6804ff7 292 //! Sets last view manger used with.
293 //! Helps to handle matrix states in multi-view configurations.
294 void SetLastView (const OpenGl_View* theLastView)
295 {
296 myLastView = theLastView;
297 }
298
299 //! Returns true when provided view is the same as cached one.
300 bool IsSameView (const OpenGl_View* theView) const
301 {
302 return myLastView == theView;
303 }
304
30f0ad28 305protected:
306
8625ef7e 307 //! Define program bits.
308 Standard_Integer getProgramBits (const Handle(OpenGl_Texture)& theTexture,
e135a155 309 const Standard_Boolean theHasVertColor,
310 const Standard_Boolean theEnableEnvMap = Standard_False)
311
8625ef7e 312 {
313 Standard_Integer aBits = 0;
1a7ece8f 314
315 const Standard_Integer aNbPlanes = myContext->Clipping().NbClippingOrCappingOn();
316 if (aNbPlanes > 0)
8625ef7e 317 {
1a7ece8f 318 aBits |= OpenGl_PO_ClipPlanesN;
319 if (aNbPlanes == 1)
320 {
321 aBits |= OpenGl_PO_ClipPlanes1;
322 }
323 else if (aNbPlanes == 2)
324 {
325 aBits |= OpenGl_PO_ClipPlanes2;
326 }
8625ef7e 327 }
1a7ece8f 328
83da37b1 329 if (theEnableEnvMap)
e135a155 330 {
331 // Environment map overwrites material texture
332 aBits |= OpenGl_PO_TextureEnv;
333 }
334 else if (!theTexture.IsNull())
8625ef7e 335 {
4e1523ef 336 aBits |= theTexture->IsAlpha() ? OpenGl_PO_TextureA : OpenGl_PO_TextureRGB;
8625ef7e 337 }
338 if (theHasVertColor)
339 {
340 aBits |= OpenGl_PO_VertColor;
341 }
342 return aBits;
343 }
344
345 //! Prepare standard GLSL program.
346 Handle(OpenGl_ShaderProgram)& getStdProgram (const Standard_Boolean theToLightOn,
347 const Standard_Integer theBits)
348 {
e135a155 349 // If environment map is enabled lighting calculations are
350 // not needed (in accordance with default OCCT behaviour)
351 if (theToLightOn && (theBits & OpenGl_PO_TextureEnv) == 0)
8625ef7e 352 {
256f9ac0 353 Handle(OpenGl_ShaderProgram)& aProgram = myLightPrograms->ChangeValue (theBits);
8625ef7e 354 if (aProgram.IsNull())
355 {
356 prepareStdProgramLight (aProgram, theBits);
357 }
358 return aProgram;
359 }
360
256f9ac0 361 Handle(OpenGl_ShaderProgram)& aProgram = myFlatPrograms.ChangeValue (theBits);
8625ef7e 362 if (aProgram.IsNull())
363 {
364 prepareStdProgramFlat (aProgram, theBits);
365 }
366 return aProgram;
367 }
368
299e0ab9 369 //! Prepare standard GLSL program for accessing point sprite alpha.
fd59283a 370 Standard_EXPORT TCollection_AsciiString pointSpriteAlphaSrc (const Standard_Integer theBits);
299e0ab9 371
372 //! Prepare standard GLSL program for computing point sprite shading.
373 Standard_EXPORT TCollection_AsciiString pointSpriteShadingSrc (const TCollection_AsciiString theBaseColorSrc, const Standard_Integer theBits);
374
8625ef7e 375 //! Prepare standard GLSL program for textured font.
376 Standard_EXPORT Standard_Boolean prepareStdProgramFont();
377
b86bb3df 378 //! Prepare standard GLSL program for FBO blit operation.
379 Standard_EXPORT Standard_Boolean prepareStdProgramFboBlit();
380
8625ef7e 381 //! Prepare standard GLSL program without lighting.
382 Standard_EXPORT Standard_Boolean prepareStdProgramFlat (Handle(OpenGl_ShaderProgram)& theProgram,
383 const Standard_Integer theBits);
384
385 //! Prepare standard GLSL program with lighting.
386 Standard_Boolean prepareStdProgramLight (Handle(OpenGl_ShaderProgram)& theProgram,
387 const Standard_Integer theBits)
388 {
c357e426 389 return myShadingModel == Graphic3d_TOSM_FRAGMENT
8625ef7e 390 ? prepareStdProgramPhong (theProgram, theBits)
391 : prepareStdProgramGouraud (theProgram, theBits);
392 }
393
394 //! Prepare standard GLSL program with per-vertex lighting.
395 Standard_EXPORT Standard_Boolean prepareStdProgramGouraud (Handle(OpenGl_ShaderProgram)& theProgram,
396 const Standard_Integer theBits);
397
398 //! Prepare standard GLSL program with per-pixel lighting.
399 Standard_EXPORT Standard_Boolean prepareStdProgramPhong (Handle(OpenGl_ShaderProgram)& theProgram,
400 const Standard_Integer theBits);
401
256f9ac0 402 //! Define computeLighting GLSL function depending on current lights configuration
abdf0b10 403 //! @param theHasVertColor flag to use getVertColor() instead of Ambient and Diffuse components of active material
404 Standard_EXPORT TCollection_AsciiString stdComputeLighting (const Standard_Boolean theHasVertColor);
256f9ac0 405
8625ef7e 406 //! Bind specified program to current context and apply state.
299e0ab9 407 Standard_EXPORT Standard_Boolean bindProgramWithState (const Handle(OpenGl_ShaderProgram)& theProgram);
8625ef7e 408
256f9ac0 409 //! Set pointer myLightPrograms to active lighting programs set from myMapOfLightPrograms
410 Standard_EXPORT void switchLightPrograms();
411
f978241f 412 //! Prepare standard GLSL program for stereoscopic image.
413 Standard_EXPORT Standard_Boolean prepareStdProgramStereo (Handle(OpenGl_ShaderProgram)& theProgram,
414 const Graphic3d_StereoMode theStereoMode);
38a0206f 415
8625ef7e 416protected:
417
7c3ef2f7 418 //! Packed properties of light source
419 struct OpenGl_ShaderLightParameters
420 {
421 OpenGl_Vec4 Color;
422 OpenGl_Vec4 Position;
423 OpenGl_Vec4 Direction;
424 OpenGl_Vec4 Parameters;
425
426 //! Returns packed (serialized) representation of light source properties
427 const OpenGl_Vec4* Packed() const { return reinterpret_cast<const OpenGl_Vec4*> (this); }
428 static Standard_Integer NbOfVec4() { return 4; }
429 };
430
431 //! Packed light source type information
432 struct OpenGl_ShaderLightType
433 {
434 Standard_Integer Type;
435 Standard_Integer IsHeadlight;
436
437 //! Returns packed (serialized) representation of light source type
438 const OpenGl_Vec2i* Packed() const { return reinterpret_cast<const OpenGl_Vec2i*> (this); }
439 static Standard_Integer NbOfVec2i() { return 1; }
440 };
441
8613985b 442 //! Fake OpenGL program for tracking FFP state in the way consistent to programmable pipeline.
443 class OpenGl_ShaderProgramFFP : public OpenGl_ShaderProgram
444 {
445 DEFINE_STANDARD_RTTI_INLINE(OpenGl_ShaderProgramFFP, OpenGl_ShaderProgram)
446 friend class OpenGl_ShaderManager;
447 protected:
448 OpenGl_ShaderProgramFFP() {}
449 };
450
7c3ef2f7 451protected:
452
8613985b 453 Handle(OpenGl_ShaderProgramFFP) myFfpProgram;
454
c357e426 455 Graphic3d_TypeOfShadingModel myShadingModel; //!< lighting shading model
256f9ac0 456 OpenGl_ShaderProgramList myProgramList; //!< The list of shader programs
457 Handle(OpenGl_SetOfShaderPrograms) myLightPrograms; //!< pointer to active lighting programs matrix
458 OpenGl_SetOfShaderPrograms myFlatPrograms; //!< programs matrix without lighting
459 Handle(OpenGl_ShaderProgram) myFontProgram; //!< standard program for textured text
b86bb3df 460 Handle(OpenGl_ShaderProgram) myBlitProgram; //!< standard program for FBO blit emulation
256f9ac0 461 OpenGl_MapOfShaderPrograms myMapOfLightPrograms; //!< map of lighting programs depending on shading model and lights configuration
8625ef7e 462
f978241f 463 Handle(OpenGl_ShaderProgram) myStereoPrograms[Graphic3d_StereoMode_NB]; //!< standard stereo programs
38a0206f 464
256f9ac0 465 OpenGl_Context* myContext; //!< OpenGL context
8625ef7e 466
467protected:
468
256f9ac0 469 OpenGl_ProjectionState myProjectionState; //!< State of OCCT projection transformation
470 OpenGl_ModelWorldState myModelWorldState; //!< State of OCCT model-world transformation
471 OpenGl_WorldViewState myWorldViewState; //!< State of OCCT world-view transformation
8cf06aa2 472 OpenGl_ClippingState myClippingState; //!< State of OCCT clipping planes
256f9ac0 473 OpenGl_LightSourceState myLightSourceState; //!< State of OCCT light sources
8613985b 474 OpenGl_MaterialState myMaterialState; //!< State of Front and Back materials
475
7c3ef2f7 476 gp_XYZ myLocalOrigin; //!< local camera transformation
477 Standard_Boolean myHasLocalOrigin; //!< flag indicating that local camera transformation has been set
478
479 mutable OpenGl_ShaderLightType myLightTypeArray [OpenGLMaxLights];
480 mutable OpenGl_ShaderLightParameters myLightParamsArray[OpenGLMaxLights];
30f0ad28 481
482private:
483
256f9ac0 484 const OpenGl_View* myLastView; //!< Pointer to the last view shader manager used with
8625ef7e 485
30f0ad28 486};
487
8613985b 488DEFINE_STANDARD_HANDLE(OpenGl_ShaderManager, Standard_Transient)
489
30f0ad28 490#endif // _OpenGl_ShaderManager_HeaderFile