4bf072e4 |
1 | // Copyright (c) 2013-2021 OPEN CASCADE SAS |
2 | // |
3 | // This file is part of Open CASCADE Technology software library. |
4 | // |
5 | // This library is free software; you can redistribute it and/or modify it under |
6 | // the terms of the GNU Lesser General Public License version 2.1 as published |
7 | // by the Free Software Foundation, with special exception defined in the file |
8 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
9 | // distribution for complete text of the license and disclaimer of any warranty. |
10 | // |
11 | // Alternatively, this file may be used under the terms of Open CASCADE |
12 | // commercial license or contractual agreement. |
13 | |
14 | #ifndef _Graphic3d_ShaderManager_HeaderFile |
15 | #define _Graphic3d_ShaderManager_HeaderFile |
16 | |
17 | #include <Aspect_GraphicsLibrary.hxx> |
18 | #include <Graphic3d_ShaderFlags.hxx> |
19 | #include <Graphic3d_StereoMode.hxx> |
20 | #include <Graphic3d_Vec2.hxx> |
21 | #include <Standard_Transient.hxx> |
22 | #include <TCollection_AsciiString.hxx> |
23 | |
24 | class Graphic3d_LightSet; |
25 | class Graphic3d_ShaderProgram; |
26 | |
27 | //! GLSL syntax extensions. |
28 | enum Graphic3d_GlslExtension |
29 | { |
30 | Graphic3d_GlslExtension_GL_OES_standard_derivatives, //!< OpenGL ES 2.0 extension GL_OES_standard_derivatives |
31 | Graphic3d_GlslExtension_GL_EXT_shader_texture_lod, //!< OpenGL ES 2.0 extension GL_EXT_shader_texture_lod |
32 | Graphic3d_GlslExtension_GL_EXT_frag_depth, //!< OpenGL ES 2.0 extension GL_EXT_frag_depth |
33 | Graphic3d_GlslExtension_GL_EXT_gpu_shader4, //!< OpenGL 2.0 extension GL_EXT_gpu_shader4 |
34 | }; |
35 | enum { Graphic3d_GlslExtension_NB = Graphic3d_GlslExtension_GL_EXT_gpu_shader4 + 1 }; |
36 | |
37 | //! This class is responsible for generation of shader programs. |
38 | class Graphic3d_ShaderManager : public Standard_Transient |
39 | { |
40 | DEFINE_STANDARD_RTTIEXT(Graphic3d_ShaderManager, Standard_Transient) |
41 | public: |
42 | |
43 | //! Creates new empty shader manager. |
44 | Standard_EXPORT Graphic3d_ShaderManager (Aspect_GraphicsLibrary theGapi); |
45 | |
46 | //! Releases resources of shader manager. |
47 | Standard_EXPORT virtual ~Graphic3d_ShaderManager(); |
48 | |
49 | //! @return true if detected GL version is greater or equal to requested one. |
50 | bool IsGapiGreaterEqual (Standard_Integer theVerMajor, |
51 | Standard_Integer theVerMinor) const |
52 | { |
53 | return (myGapiVersion[0] > theVerMajor) |
54 | || (myGapiVersion[0] == theVerMajor && myGapiVersion[1] >= theVerMinor); |
55 | } |
56 | |
57 | //! Return GAPI version major number. |
58 | Standard_Integer GapiVersionMajor() const { return myGapiVersion[0]; } |
59 | |
60 | //! Return GAPI version minor number. |
61 | Standard_Integer GapiVersionMinor() const { return myGapiVersion[1]; } |
62 | |
63 | //! Return GAPI version major number. |
64 | void SetGapiVersion (Standard_Integer theVerMajor, |
65 | Standard_Integer theVerMinor) |
66 | { |
67 | myGapiVersion.SetValues (theVerMajor, theVerMinor); |
68 | } |
69 | |
70 | //! Return TRUE if RED channel should be used instead of ALPHA for single-channel textures |
71 | //! (e.g. GAPI supports only GL_RED textures and not GL_ALPHA). |
72 | bool UseRedAlpha() const { return myUseRedAlpha; } |
73 | |
74 | //! Set if RED channel should be used instead of ALPHA for single-channel textures. |
75 | void SetUseRedAlpha (bool theUseRedAlpha) { myUseRedAlpha = theUseRedAlpha; } |
76 | |
77 | //! Return flag indicating flat shading usage; TRUE by default. |
78 | bool HasFlatShading() const { return myHasFlatShading; } |
79 | |
80 | //! Return flag indicating flat shading should reverse normal flag; FALSE by default. |
81 | bool ToReverseDFdxSign() const { return myToReverseDFdxSign; } |
82 | |
83 | //! Set flag indicating flat shading usage. |
84 | void SetFlatShading (bool theToUse, |
85 | bool theToReverseSign) |
86 | { |
87 | myHasFlatShading = theToUse; |
88 | myToReverseDFdxSign = theToReverseSign; |
89 | } |
90 | |
91 | //! Return TRUE if depth clamping should be emulated by GLSL program; TRUE by default. |
92 | bool ToEmulateDepthClamp() const { return myToEmulateDepthClamp; } |
93 | |
94 | //! Set if depth clamping should be emulated by GLSL program. |
95 | void SetEmulateDepthClamp (bool theToEmulate) { myToEmulateDepthClamp = theToEmulate; } |
96 | |
97 | //! Return TRUE if specified extension is available. |
98 | bool HasGlslExtension (Graphic3d_GlslExtension theExt) const { return myGlslExtensions[theExt]; } |
99 | |
100 | //! Set if specified extension is available or not. |
101 | void EnableGlslExtension (Graphic3d_GlslExtension theExt, |
102 | bool theToEnable = true) { myGlslExtensions[theExt] = theToEnable; } |
103 | |
104 | protected: |
105 | |
106 | //! Generate map key for light sources configuration. |
107 | //! @param theLights [in] list of light sources |
108 | //! @param theHasShadowMap [in] flag indicating shadow maps usage |
109 | Standard_EXPORT TCollection_AsciiString genLightKey (const Handle(Graphic3d_LightSet)& theLights, |
110 | const bool theHasShadowMap) const; |
111 | |
112 | //! Prepare standard GLSL program for textured font. |
113 | Standard_EXPORT Handle(Graphic3d_ShaderProgram) getStdProgramFont() const; |
114 | |
115 | //! Prepare standard GLSL program without lighting. |
116 | //! @param theBits [in] program bits |
117 | //! @param theIsOutline [in] draw silhouette |
118 | Standard_EXPORT Handle(Graphic3d_ShaderProgram) getStdProgramUnlit (Standard_Integer theBits, |
119 | Standard_Boolean theIsOutline = false) const; |
120 | |
121 | //! Prepare standard GLSL program with per-vertex lighting. |
122 | //! @param theLights [in] list of light sources |
123 | //! @param theBits [in] program bits |
124 | Standard_EXPORT Handle(Graphic3d_ShaderProgram) getStdProgramGouraud (const Handle(Graphic3d_LightSet)& theLights, |
125 | Standard_Integer theBits) const; |
126 | |
127 | //! Prepare standard GLSL program with per-pixel lighting. |
128 | //! @param theLights [in] list of light sources |
129 | //! @param theBits [in] program bits |
130 | //! @param theIsFlatNormal [in] when TRUE, the Vertex normals will be ignored and Face normal will be computed instead |
131 | //! @param theIsPBR [in] when TRUE, the PBR pipeline will be activated |
132 | //! @param theNbShadowMaps [in] number of shadow maps |
133 | Standard_EXPORT Handle(Graphic3d_ShaderProgram) getStdProgramPhong (const Handle(Graphic3d_LightSet)& theLights, |
134 | const Standard_Integer theBits, |
135 | const Standard_Boolean theIsFlatNormal, |
136 | const Standard_Boolean theIsPBR, |
137 | const Standard_Integer theNbShadowMaps) const; |
138 | |
139 | //! Prepare standard GLSL program for bounding box. |
140 | Standard_EXPORT Handle(Graphic3d_ShaderProgram) getStdProgramBoundBox() const; |
141 | |
142 | //! Generates shader program to render environment cubemap as background. |
143 | Standard_EXPORT Handle(Graphic3d_ShaderProgram) getBgCubeMapProgram() const; |
144 | |
145 | //! Prepare GLSL source for IBL generation used in PBR pipeline. |
146 | Standard_EXPORT Handle(Graphic3d_ShaderProgram) getPBREnvBakingProgram (Standard_Integer theIndex) const; |
147 | |
148 | //! Prepare standard GLSL program for FBO blit operation. |
149 | Standard_EXPORT Handle(Graphic3d_ShaderProgram) getStdProgramFboBlit (Standard_Integer theNbSamples, |
150 | Standard_Boolean theIsFallback_sRGB) const; |
151 | |
152 | //! Prepare standard GLSL program for stereoscopic image. |
153 | Standard_EXPORT Handle(Graphic3d_ShaderProgram) getStdProgramStereo (Graphic3d_StereoMode theStereoMode) const; |
154 | |
155 | //! Prepare standard GLSL programs for OIT compositing operation. |
156 | Standard_EXPORT Handle(Graphic3d_ShaderProgram) getStdProgramOitCompositing (Standard_Boolean theMsaa) const; |
157 | |
158 | //! Prepare standard GLSL programs for OIT Depth Peeling blend operation. |
159 | Standard_EXPORT Handle(Graphic3d_ShaderProgram) getStdProgramOitDepthPeelingBlend (Standard_Boolean theMsaa) const; |
160 | |
161 | //! Prepare standard GLSL programs for OIT Depth Peeling flush operation. |
162 | Standard_EXPORT Handle(Graphic3d_ShaderProgram) getStdProgramOitDepthPeelingFlush (Standard_Boolean theMsaa) const; |
163 | |
164 | protected: |
165 | |
166 | //! Return TRUE if bitwise operations can be used in GLSL program. |
167 | Standard_EXPORT bool hasGlslBitwiseOps() const; |
168 | |
169 | //! Prepare GLSL version header. |
170 | //! @param theProgram [in] [out] program to set version header |
171 | //! @param theName [in] program id suffix |
172 | //! @param theBits [in] program bits |
173 | //! @param theUsesDerivates [in] program uses standard derivatives functions or not |
174 | //! @return filtered program bits with unsupported features disabled |
175 | Standard_EXPORT Standard_Integer defaultGlslVersion (const Handle(Graphic3d_ShaderProgram)& theProgram, |
176 | const TCollection_AsciiString& theName, |
177 | Standard_Integer theBits, |
178 | bool theUsesDerivates = false) const; |
179 | |
180 | //! Prepare GLSL version header for OIT composition programs. |
181 | //! @param theProgram [in] [out] program to set version header |
182 | //! @param theName [in] program id suffix |
183 | //! @param theMsaa [in] multisampling flag |
184 | Standard_EXPORT void defaultOitGlslVersion (const Handle(Graphic3d_ShaderProgram)& theProgram, |
185 | const TCollection_AsciiString& theName, |
186 | bool theMsaa) const; |
187 | |
188 | //! Prepare standard GLSL program for accessing point sprite alpha. |
189 | Standard_EXPORT TCollection_AsciiString pointSpriteAlphaSrc (Standard_Integer theBits) const; |
190 | |
191 | //! Prepare standard GLSL program for computing point sprite shading. |
192 | Standard_EXPORT TCollection_AsciiString pointSpriteShadingSrc (const TCollection_AsciiString& theBaseColorSrc, |
193 | Standard_Integer theBits) const; |
194 | |
195 | //! Define computeLighting GLSL function depending on current lights configuration |
196 | //! @param theNbLights [out] number of defined light sources |
197 | //! @param theLights [in] light sources list |
198 | //! @param theHasVertColor [in] flag to use getVertColor() instead of Ambient and Diffuse components of active material |
199 | //! @param theIsPBR [in] flag to activate PBR pipeline |
941f6cae |
200 | //! @param theHasTexColor [in] flag to include color texturing |
4bf072e4 |
201 | //! @param theNbShadowMaps [in] flag to include shadow map |
202 | Standard_EXPORT TCollection_AsciiString stdComputeLighting (Standard_Integer& theNbLights, |
203 | const Handle(Graphic3d_LightSet)& theLights, |
204 | Standard_Boolean theHasVertColor, |
205 | Standard_Boolean theIsPBR, |
941f6cae |
206 | Standard_Boolean theHasTexColor, |
4bf072e4 |
207 | Standard_Integer theNbShadowMaps) const; |
208 | |
209 | protected: |
210 | |
211 | Aspect_GraphicsLibrary myGapi; //!< GAPI name |
212 | Graphic3d_Vec2i myGapiVersion; //!< GAPI version major/minor number pair |
213 | Standard_Boolean myGlslExtensions[Graphic3d_GlslExtension_NB]; |
214 | Standard_Boolean myHasFlatShading; //!< flag indicating flat shading usage |
215 | Standard_Boolean myToReverseDFdxSign; //!< flag to reverse flat shading normal (workaround) |
216 | Standard_Boolean mySetPointSize; //!< always set gl_PointSize variable |
217 | Standard_Boolean myUseRedAlpha; //!< use RED channel instead of ALPHA (e.g. GAPI supports only GL_RED textures and not GL_ALPHA) |
218 | Standard_Boolean myToEmulateDepthClamp; //!< emulate depth clamping in GLSL program |
219 | Standard_Boolean mySRgbState; //!< track sRGB state |
220 | |
221 | }; |
222 | |
223 | #endif // _Graphic3d_ShaderManager_HeaderFile |