0033661: Data Exchange, Step Import - Tessellated GDTs are not imported
[occt.git] / src / Shaders / Declarations.glsl
1
2 //! @file Declarations.glsl includes definition of common uniform variables in OCCT GLSL programs
3 //! @def THE_MAX_LIGHTS
4 //! Specifies the length of array of lights, which is 8 by default. Defined by Shader Manager.
5 // #define THE_MAX_LIGHTS 8
6
7 //! @def THE_MAX_CLIP_PLANES
8 //! Specifies the length of array of clipping planes, which is 8 by default. Defined by Shader Manager.
9 // #define THE_MAX_CLIP_PLANES 8
10
11 //! @def THE_NB_FRAG_OUTPUTS
12 //! Specifies the length of array of Fragment Shader outputs, which is 1 by default. Defined by Shader Manager.
13 // #define THE_NB_FRAG_OUTPUTS 1
14
15 // compatibility macros
16 #if (__VERSION__ >= 130)
17   #define THE_ATTRIBUTE  in
18   #define THE_SHADER_IN  in
19   #define THE_SHADER_OUT out
20   #define THE_OUT        out
21   #define occTexture1D   texture
22   #define occTexture2D   texture
23   #define occTexture3D   texture
24   #define occTextureCube texture
25   #define occTextureCubeLod textureLod
26 #else
27   #define THE_ATTRIBUTE  attribute
28   #define THE_SHADER_IN  varying
29   #define THE_SHADER_OUT varying
30   #define THE_OUT
31   #define occTexture1D   texture1D
32   #define occTexture2D   texture2D
33   #define occTexture3D   texture3D
34   #define occTextureCube textureCube
35   #define occTextureCubeLod textureCubeLod
36 #endif
37
38 #ifdef GL_ES
39 #if (__VERSION__ >= 300)
40   #define THE_PREC_ENUM highp // lowp should be enough for enums but triggers driver bugs
41 #else
42   #define THE_PREC_ENUM lowp
43 #endif
44 #else
45   #define THE_PREC_ENUM
46 #endif
47
48 // Vertex attributes
49 #ifdef VERTEX_SHADER
50   THE_ATTRIBUTE vec4 occVertex;
51   THE_ATTRIBUTE vec3 occNormal;
52   THE_ATTRIBUTE vec4 occTexCoord;
53   THE_ATTRIBUTE vec4 occVertColor;
54 #elif defined(FRAGMENT_SHADER)
55   #if (__VERSION__ >= 130)
56     #ifdef OCC_ENABLE_draw_buffers
57       out vec4 occFragColorArray[THE_NB_FRAG_OUTPUTS];
58       #define occFragColorArrayAlias occFragColorArray
59       #define occFragColor0 occFragColorArray[0]
60     #else
61       out vec4 occFragColor0;
62     #endif
63   #else
64     #ifdef OCC_ENABLE_draw_buffers
65       #define occFragColorArrayAlias gl_FragData
66       #define occFragColor0 gl_FragData[0]
67     #else
68       #define occFragColor0 gl_FragColor
69     #endif
70   #endif
71
72   #if (THE_NB_FRAG_OUTPUTS >= 2)
73     #define occFragColor1 occFragColorArrayAlias[1]
74   #else
75     vec4 occFragColor1;
76   #endif
77   #if (THE_NB_FRAG_OUTPUTS >= 3)
78     #define occFragColor2 occFragColorArrayAlias[2]
79   #else
80     vec4 occFragColor2;
81   #endif
82   #if (THE_NB_FRAG_OUTPUTS >= 4)
83     #define occFragColor3 occFragColorArrayAlias[3]
84   #else
85     vec4 occFragColor3;
86   #endif
87
88   // Built-in outputs notation
89   #define occFragColor    occFragColor0
90   #define occFragCoverage occFragColor1
91
92   //! Define the main Fragment Shader output - color value.
93   void occSetFragColor (in vec4 theColor);
94 #endif
95
96 // Pi number definitions
97 #define PI       3.141592654
98 #define PI_2     6.283185307
99 #define PI_DIV_2 1.570796327
100 #define PI_DIV_3 1.047197551
101 #define PI_DIV_4 0.785398163
102 #define INV_PI   0.318309886
103 #define INV_PI_2 0.159154943
104
105 // Matrix state
106 uniform mat4 occWorldViewMatrix;  //!< World-view  matrix
107 uniform mat4 occProjectionMatrix; //!< Projection  matrix
108 uniform mat4 occModelWorldMatrix; //!< Model-world matrix
109
110 uniform mat4 occWorldViewMatrixInverse;    //!< Inverse of the world-view  matrix
111 uniform mat4 occProjectionMatrixInverse;   //!< Inverse of the projection  matrix
112 uniform mat4 occModelWorldMatrixInverse;   //!< Inverse of the model-world matrix
113
114 uniform mat4 occWorldViewMatrixTranspose;  //!< Transpose of the world-view  matrix
115 uniform mat4 occProjectionMatrixTranspose; //!< Transpose of the projection  matrix
116 uniform mat4 occModelWorldMatrixTranspose; //!< Transpose of the model-world matrix
117
118 uniform mat4 occWorldViewMatrixInverseTranspose;  //!< Transpose of the inverse of the world-view  matrix
119 uniform mat4 occProjectionMatrixInverseTranspose; //!< Transpose of the inverse of the projection  matrix
120 uniform mat4 occModelWorldMatrixInverseTranspose; //!< Transpose of the inverse of the model-world matrix
121
122 #if defined(THE_IS_PBR)
123 uniform sampler2D   occEnvLUT;             //!< Environment Lookup Table
124 uniform sampler2D   occDiffIBLMapSHCoeffs; //!< Packed diffuse (irradiance) IBL map's spherical harmonics coefficients
125 uniform samplerCube occSpecIBLMap;         //!< Specular IBL map
126 uniform int         occNbSpecIBLLevels;    //!< Number of mipmap levels used in occSpecIBLMap to store different roughness values maps
127
128 vec3 occDiffIBLMap (in vec3 theNormal); //!< Unpacks spherical harmonics coefficients to diffuse IBL map's values
129 #endif
130
131 // light type enumeration (same as Graphic3d_TypeOfLightSource)
132 const int OccLightType_Direct = 1; //!< directional     light source
133 const int OccLightType_Point  = 2; //!< isotropic point light source
134 const int OccLightType_Spot   = 3; //!< spot            light source
135
136 // Light sources
137 uniform               vec4 occLightAmbient;      //!< Cumulative ambient color
138 #if defined(THE_MAX_LIGHTS) && (THE_MAX_LIGHTS > 0)
139 uniform THE_PREC_ENUM int  occLightSourcesCount; //!< Total number of light sources
140
141 //! Type of light source, int (see OccLightType enum).
142 #define occLight_Type(theId)              occLightSourcesTypes[theId]
143
144 //! Specular intensity (equals to diffuse), vec3.
145 #define occLight_Specular(theId)          occLightSources[theId * 4 + 0].rgb
146
147 //! Intensity of light source (>= 0), float.
148 #define occLight_Intensity(theId)         occLightSources[theId * 4 + 0].a
149
150 //! Is light a headlight, bool?
151 #define occLight_IsHeadlight(theId)      (occLightSources[theId * 4 + 1].w > 0.5)
152
153 //! Position of specified light source, vec3.
154 #define occLight_Position(theId)          occLightSources[theId * 4 + 1].xyz
155
156 //! Direction of specified spot light source, vec3.
157 #define occLight_SpotDirection(theId)     occLightSources[theId * 4 + 2].xyz
158
159 //! Range on which point light source (positional or spot) can affect (>= 0), float.
160 #define occLight_Range(theId)             occLightSources[theId * 4 + 2].w
161
162 //! Maximum spread angle of the spot light (in radians), float.
163 #define occLight_SpotCutOff(theId)        occLightSources[theId * 4 + 3].z
164
165 //! Attenuation of the spot light intensity (from 0 to 1), float.
166 #define occLight_SpotExponent(theId)      occLightSources[theId * 4 + 3].w
167
168 #if !defined(THE_IS_PBR)
169 //! Diffuse intensity (equals to Specular), vec3.
170 #define occLight_Diffuse(theId)           occLightSources[theId * 4 + 0].rgb
171
172 //! Const attenuation factor of positional light source, float.
173 #define occLight_ConstAttenuation(theId)  occLightSources[theId * 4 + 3].x
174
175 //! Linear attenuation factor of positional light source, float.
176 #define occLight_LinearAttenuation(theId) occLightSources[theId * 4 + 3].y
177 #endif
178 #endif
179
180 #if defined(THE_IS_PBR)
181 //! Converts roughness value from range [0, 1] to real value for calculations
182 float occRoughness (in float theNormalizedRoughness);
183
184 // Front/back material properties accessors
185 vec4  occPBRMaterial_Color(in bool theIsFront);    //!< Base color of PBR material
186 float occPBRMaterial_Metallic(in bool theIsFront); //!< Metallic coefficient
187 float occPBRMaterial_NormalizedRoughness(in bool theIsFront); //!< Normalized roughness coefficient
188 vec3  occPBRMaterial_Emission(in bool theIsFront); //!< Light intensity emitted by material
189 float occPBRMaterial_IOR(in bool theIsFront);      //!< Index of refraction
190 #else
191 // Front material properties accessors
192 vec4  occFrontMaterial_Emission(void);           //!< Emission color
193 vec4  occFrontMaterial_Ambient(void);            //!< Ambient  reflection
194 vec4  occFrontMaterial_Diffuse(void);            //!< Diffuse  reflection
195 vec4  occFrontMaterial_Specular(void);           //!< Specular reflection
196 float occFrontMaterial_Shininess(void);          //!< Specular exponent
197 float occFrontMaterial_Transparency(void);       //!< Transparency coefficient
198
199 // Back material properties accessors
200 vec4  occBackMaterial_Emission(void);            //!< Emission color
201 vec4  occBackMaterial_Ambient(void);             //!< Ambient  reflection
202 vec4  occBackMaterial_Diffuse(void);             //!< Diffuse  reflection
203 vec4  occBackMaterial_Specular(void);            //!< Specular reflection
204 float occBackMaterial_Shininess(void);           //!< Specular exponent
205 float occBackMaterial_Transparency(void);        //!< Transparency coefficient
206 #endif
207
208 #ifdef THE_HAS_DEFAULT_SAMPLER
209 #define occActiveSampler    occSampler0  //!< alias for backward compatibility
210 #define occSamplerBaseColor occSampler0  //!< alias to a base color texture
211 uniform sampler2D           occSampler0; //!< current active sampler;
212 #endif                                   //!  occSampler1, occSampler2,... should be defined in GLSL program body for multitexturing
213
214 #if defined(THE_HAS_TEXTURE_COLOR)
215 #define occTextureColor(theMatColor, theTexCoord) (theMatColor * occTexture2D(occSamplerBaseColor, theTexCoord))
216 #else
217 #define occTextureColor(theMatColor, theTexCoord) theMatColor
218 #endif
219
220 #if defined(THE_HAS_TEXTURE_OCCLUSION) && defined(FRAGMENT_SHADER)
221 uniform sampler2D occSamplerOcclusion;   //!< R occlusion texture sampler
222 #define occTextureOcclusion(theColor, theTexCoord) theColor *= occTexture2D(occSamplerOcclusion, theTexCoord).r;
223 #else
224 #define occTextureOcclusion(theColor, theTexCoord)
225 #endif
226
227 #if defined(THE_HAS_TEXTURE_EMISSIVE) && defined(FRAGMENT_SHADER)
228 uniform sampler2D occSamplerEmissive;    //!< RGB emissive texture sampler
229 #define occTextureEmissive(theMatEmis, theTexCoord) (theMatEmis * occTexture2D(occSamplerEmissive, theTexCoord).rgb)
230 #else
231 #define occTextureEmissive(theMatEmis, theTexCoord) theMatEmis
232 #endif
233
234 #if defined(THE_HAS_TEXTURE_NORMAL) && defined(FRAGMENT_SHADER)
235 uniform sampler2D occSamplerNormal;      //!< XYZ normal texture sampler with W==0 indicating no texture
236 #define occTextureNormal(theTexCoord) occTexture2D(occSamplerNormal, theTexCoord)
237 #else
238 #define occTextureNormal(theTexCoord) vec4(0.0) // no normal map
239 #endif
240
241 #if defined(THE_HAS_TEXTURE_METALROUGHNESS) && defined(FRAGMENT_SHADER)
242 uniform sampler2D occSamplerMetallicRoughness; //!< BG metallic-roughness texture sampler
243 #define occTextureRoughness(theRoug, theTexCoord) (theRoug * occTexture2D(occSamplerMetallicRoughness, theTexCoord).g)
244 #define occTextureMetallic(theMet,   theTexCoord) (theMet  * occTexture2D(occSamplerMetallicRoughness, theTexCoord).b)
245 #else
246 #define occTextureRoughness(theRoug, theTexCoord) theRoug
247 #define occTextureMetallic(theMet,   theTexCoord) theMet
248 #endif
249
250 uniform               vec4      occColor;              //!< color value (in case of disabled lighting)
251 uniform THE_PREC_ENUM int       occDistinguishingMode; //!< Are front and back faces distinguished?
252 uniform THE_PREC_ENUM int       occTextureEnable;      //!< Is texture enabled?
253 uniform               vec4      occTexTrsf2d[2];       //!< 2D texture transformation parameters
254 uniform               float     occPointSize;          //!< point size
255
256 //! Parameters of blended order-independent transparency rendering algorithm
257 uniform               int       occOitOutput;      //!< Enable bit for writing output color buffers for OIT (occFragColor, occFragCoverage)
258 uniform               float     occOitDepthFactor; //!< Influence of the depth component to the coverage of the accumulated fragment
259 uniform               float     occAlphaCutoff;    //!< alpha test cutoff value
260
261 //! Parameters of clipping planes
262 #if defined(THE_MAX_CLIP_PLANES) && (THE_MAX_CLIP_PLANES > 0)
263 uniform               vec4 occClipPlaneEquations[THE_MAX_CLIP_PLANES];
264 uniform THE_PREC_ENUM int  occClipPlaneChains[THE_MAX_CLIP_PLANES]; //! Indicating the number of planes in the Chain
265 uniform THE_PREC_ENUM int  occClipPlaneCount;   //!< Total number of clip planes
266 #endif
267 //! @endfile Declarations.glsl