Adjusting test cases at current state of OCCT master
[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>
8625ef7e 27#include <OpenGl_AspectFace.hxx>
28#include <OpenGl_AspectLine.hxx>
29#include <OpenGl_AspectText.hxx>
30#include <OpenGl_AspectMarker.hxx>
31#include <OpenGl_Texture.hxx>
32#include <Visual3d_TypeOfModel.hxx>
30f0ad28 33
e6804ff7 34class OpenGl_View;
35
30f0ad28 36//! List of shader programs.
37typedef NCollection_Sequence<Handle(OpenGl_ShaderProgram)> OpenGl_ShaderProgramList;
38
39//! Map to declare per-program states of OCCT materials.
40typedef NCollection_DataMap<Handle(OpenGl_ShaderProgram), OpenGl_MaterialState> OpenGl_MaterialStates;
41
c04c30b3 42class OpenGl_ShaderManager;
43DEFINE_STANDARD_HANDLE(OpenGl_ShaderManager, Standard_Transient)
44
30f0ad28 45//! This class is responsible for managing shader programs.
46class OpenGl_ShaderManager : public Standard_Transient
47{
48 friend class OpenGl_ShaderProgram;
49
50public:
51
52 //! Creates new empty shader manager.
53 Standard_EXPORT OpenGl_ShaderManager (OpenGl_Context* theContext);
54
55 //! Releases resources of shader manager.
56 Standard_EXPORT virtual ~OpenGl_ShaderManager();
57
05e2200b 58 //! Release all resources.
59 Standard_EXPORT void clear();
60
392ac980 61 //! Creates new shader program or re-use shared instance.
62 //! @param theProxy [IN] program definition
63 //! @param theShareKey [OUT] sharing key
64 //! @param theProgram [OUT] OpenGL program
8625ef7e 65 //! @return true on success
66 Standard_EXPORT Standard_Boolean Create (const Handle(Graphic3d_ShaderProgram)& theProxy,
67 TCollection_AsciiString& theShareKey,
68 Handle(OpenGl_ShaderProgram)& theProgram);
30f0ad28 69
70 //! Unregisters specified shader program.
392ac980 71 Standard_EXPORT void Unregister (TCollection_AsciiString& theShareKey,
72 Handle(OpenGl_ShaderProgram)& theProgram);
30f0ad28 73
74 //! Returns list of registered shader programs.
75 Standard_EXPORT const OpenGl_ShaderProgramList& ShaderPrograms() const;
76
77 //! Returns true if no program objects are registered in the manager.
78 Standard_EXPORT Standard_Boolean IsEmpty() const;
79
8625ef7e 80 //! Bind program for filled primitives rendering
81 Standard_Boolean BindProgram (const OpenGl_AspectFace* theAspect,
82 const Handle(OpenGl_Texture)& theTexture,
83 const Standard_Boolean theToLightOn,
84 const Standard_Boolean theHasVertColor,
85 const Handle(OpenGl_ShaderProgram)& theCustomProgram)
86 {
87 if (!theCustomProgram.IsNull()
88 || myContext->caps->ffpEnable)
89 {
90 return bindProgramWithState (theCustomProgram, theAspect);
91 }
92
e135a155 93 const Standard_Integer aBits = getProgramBits (theTexture, theHasVertColor, Standard_True);
8625ef7e 94 Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits);
95 return bindProgramWithState (aProgram, theAspect);
96 }
30f0ad28 97
8625ef7e 98 //! Bind program for line rendering
99 Standard_Boolean BindProgram (const OpenGl_AspectLine* theAspect,
100 const Handle(OpenGl_Texture)& theTexture,
101 const Standard_Boolean theToLightOn,
102 const Standard_Boolean theHasVertColor,
103 const Handle(OpenGl_ShaderProgram)& theCustomProgram)
104 {
105 if (!theCustomProgram.IsNull()
106 || myContext->caps->ffpEnable)
107 {
108 return bindProgramWithState (theCustomProgram, theAspect);
109 }
110
ac116c22 111 Standard_Integer aBits = getProgramBits (theTexture, theHasVertColor);
112 if (theAspect->Type() != Aspect_TOL_SOLID)
113 {
114 aBits |= OpenGl_PO_StippleLine;
115 }
116
8625ef7e 117 Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits);
118 return bindProgramWithState (aProgram, theAspect);
119 }
30f0ad28 120
8625ef7e 121 //! Bind program for point rendering
122 Standard_Boolean BindProgram (const OpenGl_AspectMarker* theAspect,
123 const Handle(OpenGl_Texture)& theTexture,
124 const Standard_Boolean theToLightOn,
125 const Standard_Boolean theHasVertColor,
126 const Handle(OpenGl_ShaderProgram)& theCustomProgram)
127 {
128 if (!theCustomProgram.IsNull()
129 || myContext->caps->ffpEnable)
130 {
131 return bindProgramWithState (theCustomProgram, theAspect);
132 }
133
134 const Standard_Integer aBits = getProgramBits (theTexture, theHasVertColor) | OpenGl_PO_Point;
135 Handle(OpenGl_ShaderProgram)& aProgram = getStdProgram (theToLightOn, aBits);
136 return bindProgramWithState (aProgram, theAspect);
137 }
138
139 //! Bind program for rendering alpha-textured font.
140 Standard_Boolean BindProgram (const OpenGl_AspectText* theAspect,
141 const Handle(OpenGl_ShaderProgram)& theCustomProgram)
142 {
143 if (!theCustomProgram.IsNull()
144 || myContext->caps->ffpEnable)
145 {
146 return bindProgramWithState (theCustomProgram, theAspect);
147 }
148
149 if (myFontProgram.IsNull())
150 {
151 prepareStdProgramFont();
152 }
153 return bindProgramWithState (myFontProgram, theAspect);
154 }
30f0ad28 155
b86bb3df 156 //! Bind program for FBO blit operation.
157 Standard_Boolean BindFboBlitProgram()
158 {
159 if (myBlitProgram.IsNull())
160 {
161 prepareStdProgramFboBlit();
162 }
163 return !myBlitProgram.IsNull()
164 && myContext->BindProgram (myBlitProgram);
165 }
166
f978241f 167 //! Bind program for rendering stereoscopic image.
168 Standard_Boolean BindStereoProgram (const Graphic3d_StereoMode theStereoMode)
38a0206f 169 {
f978241f 170 if (theStereoMode < 0 || theStereoMode >= Graphic3d_StereoMode_NB)
38a0206f 171 {
f978241f 172 return Standard_False;
38a0206f 173 }
f978241f 174
175 if (myStereoPrograms[theStereoMode].IsNull())
176 {
177 prepareStdProgramStereo (myStereoPrograms[theStereoMode], theStereoMode);
178 }
179 const Handle(OpenGl_ShaderProgram)& aProgram = myStereoPrograms[theStereoMode];
180 return !aProgram.IsNull()
181 && myContext->BindProgram (aProgram);
38a0206f 182 }
183
30f0ad28 184public:
185
186 //! Returns current state of OCCT light sources.
187 Standard_EXPORT const OpenGl_LightSourceState& LightSourceState() const;
188
189 //! Updates state of OCCT light sources.
190 Standard_EXPORT void UpdateLightSourceStateTo (const OpenGl_ListOfLight* theLights);
191
192 //! Pushes current state of OCCT light sources to specified program.
193 Standard_EXPORT void PushLightSourceState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
194
195public:
196
197 //! Returns current state of OCCT projection transform.
198 Standard_EXPORT const OpenGl_ProjectionState& ProjectionState() const;
199
200 //! Updates state of OCCT projection transform.
c827ea3a 201 Standard_EXPORT void UpdateProjectionStateTo (const OpenGl_Mat4& theProjectionMatrix);
30f0ad28 202
30f0ad28 203 //! Pushes current state of OCCT projection transform to specified program.
204 Standard_EXPORT void PushProjectionState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
205
206public:
207
208 //! Returns current state of OCCT model-world transform.
209 Standard_EXPORT const OpenGl_ModelWorldState& ModelWorldState() const;
210
211 //! Updates state of OCCT model-world transform.
c827ea3a 212 Standard_EXPORT void UpdateModelWorldStateTo (const OpenGl_Mat4& theModelWorldMatrix);
30f0ad28 213
30f0ad28 214 //! Pushes current state of OCCT model-world transform to specified program.
215 Standard_EXPORT void PushModelWorldState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
216
217public:
218
219 //! Returns current state of OCCT world-view transform.
220 Standard_EXPORT const OpenGl_WorldViewState& WorldViewState() const;
221
222 //! Updates state of OCCT world-view transform.
c827ea3a 223 Standard_EXPORT void UpdateWorldViewStateTo (const OpenGl_Mat4& theWorldViewMatrix);
30f0ad28 224
30f0ad28 225 //! Pushes current state of OCCT world-view transform to specified program.
226 Standard_EXPORT void PushWorldViewState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
227
228public:
229
230 //! Updates state of OCCT clipping planes.
231 Standard_EXPORT void UpdateClippingState();
232
233 //! Reverts state of OCCT clipping planes.
234 Standard_EXPORT void RevertClippingState();
235
236 //! Pushes current state of OCCT clipping planes to specified program.
237 Standard_EXPORT void PushClippingState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
238
239public:
240
241 //! Resets state of OCCT material for all programs.
242 Standard_EXPORT void ResetMaterialStates();
243
244 //! Updates state of OCCT material for specified program.
245 Standard_EXPORT void UpdateMaterialStateTo (const Handle(OpenGl_ShaderProgram)& theProgram,
246 const OpenGl_Element* theAspect);
247
248 //! Pushes current state of OCCT material to specified program.
249 Standard_EXPORT void PushMaterialState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
250
251 //! Returns current state of OCCT material for specified program.
252 Standard_EXPORT const OpenGl_MaterialState* MaterialState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
253
e135a155 254public:
255
256 //! Returns current state of OCCT surface detail.
257 Standard_EXPORT const OpenGl_SurfaceDetailState& SurfaceDetailState() const;
258
259 //! Updates state of OCCT surface detail.
260 Standard_EXPORT void UpdateSurfaceDetailStateTo (const Visual3d_TypeOfSurfaceDetail theDetail);
261
30f0ad28 262public:
05e2200b 263
30f0ad28 264 //! Pushes current state of OCCT graphics parameters to specified program.
265 Standard_EXPORT void PushState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
266
392ac980 267public:
268
269 //! Overwrites context
270 void SetContext (OpenGl_Context* theCtx)
271 {
272 myContext = theCtx;
273 }
274
8625ef7e 275 //! Sets shading model.
256f9ac0 276 Standard_EXPORT void SetShadingModel(const Visual3d_TypeOfModel 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 }
e135a155 304 if (theEnableEnvMap && mySurfaceDetailState.Detail() == Visual3d_TOD_ENVIRONMENT)
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 {
358 return myShadingModel == Visual3d_TOM_FRAGMENT
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
256f9ac0 388 Visual3d_TypeOfModel myShadingModel; //!< lighting shading model
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
e135a155 408 OpenGl_SurfaceDetailState mySurfaceDetailState; //!< State of OCCT surface detail
30f0ad28 409
410private:
411
256f9ac0 412 const OpenGl_View* myLastView; //!< Pointer to the last view shader manager used with
8625ef7e 413
414public:
415
ec357c5c 416 DEFINE_STANDARD_RTTI (OpenGl_ShaderManager, Standard_Transient)
8625ef7e 417
30f0ad28 418};
419
420#endif // _OpenGl_ShaderManager_HeaderFile