0027302: Invalid curves number in intersection result
[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>
32#include <OpenGl_Texture.hxx>
c357e426 33
30f0ad28 34
e6804ff7 35class OpenGl_View;
36
30f0ad28 37//! List of shader programs.
38typedef NCollection_Sequence<Handle(OpenGl_ShaderProgram)> OpenGl_ShaderProgramList;
39
40//! Map to declare per-program states of OCCT materials.
41typedef NCollection_DataMap<Handle(OpenGl_ShaderProgram), OpenGl_MaterialState> OpenGl_MaterialStates;
42
c04c30b3 43class OpenGl_ShaderManager;
44DEFINE_STANDARD_HANDLE(OpenGl_ShaderManager, Standard_Transient)
45
30f0ad28 46//! This class is responsible for managing shader programs.
47class OpenGl_ShaderManager : public Standard_Transient
48{
49 friend class OpenGl_ShaderProgram;
50
51public:
52
53 //! Creates new empty shader manager.
54 Standard_EXPORT OpenGl_ShaderManager (OpenGl_Context* theContext);
55
56 //! Releases resources of shader manager.
57 Standard_EXPORT virtual ~OpenGl_ShaderManager();
58
05e2200b 59 //! Release all resources.
60 Standard_EXPORT void clear();
61
392ac980 62 //! Creates new shader program or re-use shared instance.
63 //! @param theProxy [IN] program definition
64 //! @param theShareKey [OUT] sharing key
65 //! @param theProgram [OUT] OpenGL program
8625ef7e 66 //! @return true on success
67 Standard_EXPORT Standard_Boolean Create (const Handle(Graphic3d_ShaderProgram)& theProxy,
68 TCollection_AsciiString& theShareKey,
69 Handle(OpenGl_ShaderProgram)& theProgram);
30f0ad28 70
71 //! Unregisters specified shader program.
392ac980 72 Standard_EXPORT void Unregister (TCollection_AsciiString& theShareKey,
73 Handle(OpenGl_ShaderProgram)& theProgram);
30f0ad28 74
75 //! Returns list of registered shader programs.
76 Standard_EXPORT const OpenGl_ShaderProgramList& ShaderPrograms() const;
77
78 //! Returns true if no program objects are registered in the manager.
79 Standard_EXPORT Standard_Boolean IsEmpty() const;
80
8625ef7e 81 //! Bind program for filled primitives rendering
82 Standard_Boolean BindProgram (const OpenGl_AspectFace* theAspect,
83 const Handle(OpenGl_Texture)& theTexture,
84 const Standard_Boolean theToLightOn,
85 const Standard_Boolean theHasVertColor,
83da37b1 86 const Standard_Boolean theEnableEnvMap,
8625ef7e 87 const Handle(OpenGl_ShaderProgram)& theCustomProgram)
88 {
89 if (!theCustomProgram.IsNull()
90 || myContext->caps->ffpEnable)
91 {
92 return bindProgramWithState (theCustomProgram, theAspect);
93 }
94
83da37b1 95 const Standard_Integer aBits = getProgramBits (theTexture, theHasVertColor, theEnableEnvMap);
8625ef7e 96 Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits);
97 return bindProgramWithState (aProgram, theAspect);
98 }
30f0ad28 99
8625ef7e 100 //! Bind program for line rendering
101 Standard_Boolean BindProgram (const OpenGl_AspectLine* theAspect,
102 const Handle(OpenGl_Texture)& theTexture,
103 const Standard_Boolean theToLightOn,
104 const Standard_Boolean theHasVertColor,
105 const Handle(OpenGl_ShaderProgram)& theCustomProgram)
106 {
107 if (!theCustomProgram.IsNull()
108 || myContext->caps->ffpEnable)
109 {
110 return bindProgramWithState (theCustomProgram, theAspect);
111 }
112
ac116c22 113 Standard_Integer aBits = getProgramBits (theTexture, theHasVertColor);
114 if (theAspect->Type() != Aspect_TOL_SOLID)
115 {
116 aBits |= OpenGl_PO_StippleLine;
117 }
118
8625ef7e 119 Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits);
120 return bindProgramWithState (aProgram, theAspect);
121 }
30f0ad28 122
8625ef7e 123 //! Bind program for point rendering
124 Standard_Boolean BindProgram (const OpenGl_AspectMarker* theAspect,
125 const Handle(OpenGl_Texture)& theTexture,
126 const Standard_Boolean theToLightOn,
127 const Standard_Boolean theHasVertColor,
128 const Handle(OpenGl_ShaderProgram)& theCustomProgram)
129 {
130 if (!theCustomProgram.IsNull()
131 || myContext->caps->ffpEnable)
132 {
133 return bindProgramWithState (theCustomProgram, theAspect);
134 }
135
136 const Standard_Integer aBits = getProgramBits (theTexture, theHasVertColor) | OpenGl_PO_Point;
137 Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits);
138 return bindProgramWithState (aProgram, theAspect);
139 }
140
141 //! Bind program for rendering alpha-textured font.
142 Standard_Boolean BindProgram (const OpenGl_AspectText* theAspect,
143 const Handle(OpenGl_ShaderProgram)& theCustomProgram)
144 {
145 if (!theCustomProgram.IsNull()
146 || myContext->caps->ffpEnable)
147 {
148 return bindProgramWithState (theCustomProgram, theAspect);
149 }
150
151 if (myFontProgram.IsNull())
152 {
153 prepareStdProgramFont();
154 }
155 return bindProgramWithState (myFontProgram, theAspect);
156 }
30f0ad28 157
b86bb3df 158 //! Bind program for FBO blit operation.
159 Standard_Boolean BindFboBlitProgram()
160 {
161 if (myBlitProgram.IsNull())
162 {
163 prepareStdProgramFboBlit();
164 }
165 return !myBlitProgram.IsNull()
166 && myContext->BindProgram (myBlitProgram);
167 }
168
f978241f 169 //! Bind program for rendering stereoscopic image.
170 Standard_Boolean BindStereoProgram (const Graphic3d_StereoMode theStereoMode)
38a0206f 171 {
f978241f 172 if (theStereoMode < 0 || theStereoMode >= Graphic3d_StereoMode_NB)
38a0206f 173 {
f978241f 174 return Standard_False;
38a0206f 175 }
f978241f 176
177 if (myStereoPrograms[theStereoMode].IsNull())
178 {
179 prepareStdProgramStereo (myStereoPrograms[theStereoMode], theStereoMode);
180 }
181 const Handle(OpenGl_ShaderProgram)& aProgram = myStereoPrograms[theStereoMode];
182 return !aProgram.IsNull()
183 && myContext->BindProgram (aProgram);
38a0206f 184 }
185
30f0ad28 186public:
187
188 //! Returns current state of OCCT light sources.
189 Standard_EXPORT const OpenGl_LightSourceState& LightSourceState() const;
190
191 //! Updates state of OCCT light sources.
192 Standard_EXPORT void UpdateLightSourceStateTo (const OpenGl_ListOfLight* theLights);
193
194 //! Pushes current state of OCCT light sources to specified program.
195 Standard_EXPORT void PushLightSourceState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
196
197public:
198
199 //! Returns current state of OCCT projection transform.
200 Standard_EXPORT const OpenGl_ProjectionState& ProjectionState() const;
201
202 //! Updates state of OCCT projection transform.
c827ea3a 203 Standard_EXPORT void UpdateProjectionStateTo (const OpenGl_Mat4& theProjectionMatrix);
30f0ad28 204
30f0ad28 205 //! Pushes current state of OCCT projection transform to specified program.
206 Standard_EXPORT void PushProjectionState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
207
208public:
209
210 //! Returns current state of OCCT model-world transform.
211 Standard_EXPORT const OpenGl_ModelWorldState& ModelWorldState() const;
212
213 //! Updates state of OCCT model-world transform.
c827ea3a 214 Standard_EXPORT void UpdateModelWorldStateTo (const OpenGl_Mat4& theModelWorldMatrix);
30f0ad28 215
30f0ad28 216 //! Pushes current state of OCCT model-world transform to specified program.
217 Standard_EXPORT void PushModelWorldState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
218
219public:
220
221 //! Returns current state of OCCT world-view transform.
222 Standard_EXPORT const OpenGl_WorldViewState& WorldViewState() const;
223
224 //! Updates state of OCCT world-view transform.
c827ea3a 225 Standard_EXPORT void UpdateWorldViewStateTo (const OpenGl_Mat4& theWorldViewMatrix);
30f0ad28 226
30f0ad28 227 //! Pushes current state of OCCT world-view transform to specified program.
228 Standard_EXPORT void PushWorldViewState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
229
230public:
231
232 //! Updates state of OCCT clipping planes.
233 Standard_EXPORT void UpdateClippingState();
234
235 //! Reverts state of OCCT clipping planes.
236 Standard_EXPORT void RevertClippingState();
237
238 //! Pushes current state of OCCT clipping planes to specified program.
239 Standard_EXPORT void PushClippingState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
240
241public:
242
243 //! Resets state of OCCT material for all programs.
244 Standard_EXPORT void ResetMaterialStates();
245
246 //! Updates state of OCCT material for specified program.
247 Standard_EXPORT void UpdateMaterialStateTo (const Handle(OpenGl_ShaderProgram)& theProgram,
248 const OpenGl_Element* theAspect);
249
250 //! Pushes current state of OCCT material to specified program.
251 Standard_EXPORT void PushMaterialState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
252
253 //! Returns current state of OCCT material for specified program.
254 Standard_EXPORT const OpenGl_MaterialState* MaterialState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
255
256public:
05e2200b 257
30f0ad28 258 //! Pushes current state of OCCT graphics parameters to specified program.
259 Standard_EXPORT void PushState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
260
392ac980 261public:
262
263 //! Overwrites context
264 void SetContext (OpenGl_Context* theCtx)
265 {
266 myContext = theCtx;
267 }
268
c357e426 269 //! Returns true when provided context is the same as used one by shader manager.
270 bool IsSameContext (OpenGl_Context* theCtx) const
271 {
272 return myContext == theCtx;
273 }
274
8625ef7e 275 //! Sets shading model.
c357e426 276 Standard_EXPORT void SetShadingModel (const Graphic3d_TypeOfShadingModel theModel);
8625ef7e 277
e6804ff7 278 //! Sets last view manger used with.
279 //! Helps to handle matrix states in multi-view configurations.
280 void SetLastView (const OpenGl_View* theLastView)
281 {
282 myLastView = theLastView;
283 }
284
285 //! Returns true when provided view is the same as cached one.
286 bool IsSameView (const OpenGl_View* theView) const
287 {
288 return myLastView == theView;
289 }
290
30f0ad28 291protected:
292
8625ef7e 293 //! Define program bits.
294 Standard_Integer getProgramBits (const Handle(OpenGl_Texture)& theTexture,
e135a155 295 const Standard_Boolean theHasVertColor,
296 const Standard_Boolean theEnableEnvMap = Standard_False)
297
8625ef7e 298 {
299 Standard_Integer aBits = 0;
300 if (myContext->Clipping().IsClippingOrCappingOn())
301 {
302 aBits |= OpenGl_PO_ClipPlanes;
303 }
83da37b1 304 if (theEnableEnvMap)
e135a155 305 {
306 // Environment map overwrites material texture
307 aBits |= OpenGl_PO_TextureEnv;
308 }
309 else if (!theTexture.IsNull())
8625ef7e 310 {
4e1523ef 311 aBits |= theTexture->IsAlpha() ? OpenGl_PO_TextureA : OpenGl_PO_TextureRGB;
8625ef7e 312 }
313 if (theHasVertColor)
314 {
315 aBits |= OpenGl_PO_VertColor;
316 }
317 return aBits;
318 }
319
320 //! Prepare standard GLSL program.
321 Handle(OpenGl_ShaderProgram)& getStdProgram (const Standard_Boolean theToLightOn,
322 const Standard_Integer theBits)
323 {
e135a155 324 // If environment map is enabled lighting calculations are
325 // not needed (in accordance with default OCCT behaviour)
326 if (theToLightOn && (theBits & OpenGl_PO_TextureEnv) == 0)
8625ef7e 327 {
256f9ac0 328 Handle(OpenGl_ShaderProgram)& aProgram = myLightPrograms->ChangeValue (theBits);
8625ef7e 329 if (aProgram.IsNull())
330 {
331 prepareStdProgramLight (aProgram, theBits);
332 }
333 return aProgram;
334 }
335
256f9ac0 336 Handle(OpenGl_ShaderProgram)& aProgram = myFlatPrograms.ChangeValue (theBits);
8625ef7e 337 if (aProgram.IsNull())
338 {
339 prepareStdProgramFlat (aProgram, theBits);
340 }
341 return aProgram;
342 }
343
344 //! Prepare standard GLSL program for textured font.
345 Standard_EXPORT Standard_Boolean prepareStdProgramFont();
346
b86bb3df 347 //! Prepare standard GLSL program for FBO blit operation.
348 Standard_EXPORT Standard_Boolean prepareStdProgramFboBlit();
349
8625ef7e 350 //! Prepare standard GLSL program without lighting.
351 Standard_EXPORT Standard_Boolean prepareStdProgramFlat (Handle(OpenGl_ShaderProgram)& theProgram,
352 const Standard_Integer theBits);
353
354 //! Prepare standard GLSL program with lighting.
355 Standard_Boolean prepareStdProgramLight (Handle(OpenGl_ShaderProgram)& theProgram,
356 const Standard_Integer theBits)
357 {
c357e426 358 return myShadingModel == Graphic3d_TOSM_FRAGMENT
8625ef7e 359 ? prepareStdProgramPhong (theProgram, theBits)
360 : prepareStdProgramGouraud (theProgram, theBits);
361 }
362
363 //! Prepare standard GLSL program with per-vertex lighting.
364 Standard_EXPORT Standard_Boolean prepareStdProgramGouraud (Handle(OpenGl_ShaderProgram)& theProgram,
365 const Standard_Integer theBits);
366
367 //! Prepare standard GLSL program with per-pixel lighting.
368 Standard_EXPORT Standard_Boolean prepareStdProgramPhong (Handle(OpenGl_ShaderProgram)& theProgram,
369 const Standard_Integer theBits);
370
256f9ac0 371 //! Define computeLighting GLSL function depending on current lights configuration
abdf0b10 372 //! @param theHasVertColor flag to use getVertColor() instead of Ambient and Diffuse components of active material
373 Standard_EXPORT TCollection_AsciiString stdComputeLighting (const Standard_Boolean theHasVertColor);
256f9ac0 374
8625ef7e 375 //! Bind specified program to current context and apply state.
376 Standard_EXPORT Standard_Boolean bindProgramWithState (const Handle(OpenGl_ShaderProgram)& theProgram,
377 const OpenGl_Element* theAspect);
378
256f9ac0 379 //! Set pointer myLightPrograms to active lighting programs set from myMapOfLightPrograms
380 Standard_EXPORT void switchLightPrograms();
381
f978241f 382 //! Prepare standard GLSL program for stereoscopic image.
383 Standard_EXPORT Standard_Boolean prepareStdProgramStereo (Handle(OpenGl_ShaderProgram)& theProgram,
384 const Graphic3d_StereoMode theStereoMode);
38a0206f 385
8625ef7e 386protected:
387
c357e426 388 Graphic3d_TypeOfShadingModel myShadingModel; //!< lighting shading model
256f9ac0 389 OpenGl_ShaderProgramList myProgramList; //!< The list of shader programs
390 Handle(OpenGl_SetOfShaderPrograms) myLightPrograms; //!< pointer to active lighting programs matrix
391 OpenGl_SetOfShaderPrograms myFlatPrograms; //!< programs matrix without lighting
392 Handle(OpenGl_ShaderProgram) myFontProgram; //!< standard program for textured text
b86bb3df 393 Handle(OpenGl_ShaderProgram) myBlitProgram; //!< standard program for FBO blit emulation
256f9ac0 394 OpenGl_MapOfShaderPrograms myMapOfLightPrograms; //!< map of lighting programs depending on shading model and lights configuration
8625ef7e 395
f978241f 396 Handle(OpenGl_ShaderProgram) myStereoPrograms[Graphic3d_StereoMode_NB]; //!< standard stereo programs
38a0206f 397
256f9ac0 398 OpenGl_Context* myContext; //!< OpenGL context
8625ef7e 399
400protected:
401
256f9ac0 402 OpenGl_MaterialStates myMaterialStates; //!< Per-program state of OCCT material
403 OpenGl_ProjectionState myProjectionState; //!< State of OCCT projection transformation
404 OpenGl_ModelWorldState myModelWorldState; //!< State of OCCT model-world transformation
405 OpenGl_WorldViewState myWorldViewState; //!< State of OCCT world-view transformation
8cf06aa2 406 OpenGl_ClippingState myClippingState; //!< State of OCCT clipping planes
256f9ac0 407 OpenGl_LightSourceState myLightSourceState; //!< State of OCCT light sources
30f0ad28 408
409private:
410
256f9ac0 411 const OpenGl_View* myLastView; //!< Pointer to the last view shader manager used with
8625ef7e 412
413public:
414
92efcf78 415 DEFINE_STANDARD_RTTIEXT(OpenGl_ShaderManager,Standard_Transient)
8625ef7e 416
30f0ad28 417};
418
419#endif // _OpenGl_ShaderManager_HeaderFile