Debian Codegraph @

shaders/src/brdf.fs

raw
1 //------------------------------------------------------------------------------
2 // BRDF configuration
3 //------------------------------------------------------------------------------
4
5 // Diffuse BRDFs
6 #define DIFFUSE_LAMBERT 0
7 #define DIFFUSE_BURLEY 1
8
9 // Specular BRDF
10 // Normal distribution functions
11 #define SPECULAR_D_GGX 0
12
13 // Anisotropic NDFs
14 #define SPECULAR_D_GGX_ANISOTROPIC 0
15
16 // Cloth NDFs
17 #define SPECULAR_D_CHARLIE 0
18
19 // Visibility functions
20 #define SPECULAR_V_SMITH_GGX 0
21 #define SPECULAR_V_SMITH_GGX_FAST 1
22 #define SPECULAR_V_GGX_ANISOTROPIC 2
23 #define SPECULAR_V_KELEMEN 3
24 #define SPECULAR_V_NEUBELT 4
25
26 // Fresnel functions
27 #define SPECULAR_F_SCHLICK 0
28
29 #define BRDF_DIFFUSE DIFFUSE_LAMBERT
30
31 #if FILAMENT_QUALITY < FILAMENT_QUALITY_HIGH
32 #define BRDF_SPECULAR_D SPECULAR_D_GGX
33 #define BRDF_SPECULAR_V SPECULAR_V_SMITH_GGX_FAST
34 #define BRDF_SPECULAR_F SPECULAR_F_SCHLICK
35 #else
36 #define BRDF_SPECULAR_D SPECULAR_D_GGX
37 #define BRDF_SPECULAR_V SPECULAR_V_SMITH_GGX
38 #define BRDF_SPECULAR_F SPECULAR_F_SCHLICK
39 #endif
40
41 #define BRDF_CLEAR_COAT_D SPECULAR_D_GGX
42 #define BRDF_CLEAR_COAT_V SPECULAR_V_KELEMEN
43
44 #define BRDF_ANISOTROPIC_D SPECULAR_D_GGX_ANISOTROPIC
45 #define BRDF_ANISOTROPIC_V SPECULAR_V_GGX_ANISOTROPIC
46
47 #define BRDF_CLOTH_D SPECULAR_D_CHARLIE
48 #define BRDF_CLOTH_V SPECULAR_V_NEUBELT
49
50 //------------------------------------------------------------------------------
51 // Specular BRDF implementations
52 //------------------------------------------------------------------------------
53
54 float D_GGX(float roughness, float NoH, const vec3 h) {
55 // Walter et al. 2007, "Microfacet Models for Refraction through Rough Surfaces"
56
57 // In mediump, there are two problems computing 1.0 - NoH^2
58 // 1) 1.0 - NoH^2 suffers floating point cancellation when NoH^2 is close to 1 (highlights)
59 // 2) NoH doesn't have enough precision around 1.0
60 // Both problem can be fixed by computing 1-NoH^2 in highp and providing NoH in highp as well
61
62 // However, we can do better using Lagrange's identity:
63 // ||a x b||^2 = ||a||^2 ||b||^2 - (a . b)^2
64 // since N and H are unit vectors: ||N x H||^2 = 1.0 - NoH^2
65 // This computes 1.0 - NoH^2 directly (which is close to zero in the highlights and has
66 // enough precision).
67 // Overall this yields better performance, keeping all computations in mediump
68 #if defined(TARGET_MOBILE)
69 vec3 NxH = cross(shading_normal, h);
70 float oneMinusNoHSquared = dot(NxH, NxH);
71 #else
72 float oneMinusNoHSquared = 1.0 - NoH * NoH;
73 #endif
74
75 float a = NoH * roughness;
76 float k = roughness / (oneMinusNoHSquared + a * a);
77 float d = k * k * (1.0 / PI);
78 return saturateMediump(d);
79 }
80
81 float D_GGX_Anisotropic(float at, float ab, float ToH, float BoH, float NoH) {
82 // Burley 2012, "Physically-Based Shading at Disney"
83
84 // The values at and ab are perceptualRoughness^2, a2 is therefore perceptualRoughness^4
85 // The dot product below computes perceptualRoughness^8. We cannot fit in fp16 without clamping
86 // the roughness to too high values so we perform the dot product and the division in fp32
87 float a2 = at * ab;
88 highp vec3 d = vec3(ab * ToH, at * BoH, a2 * NoH);
89 highp float d2 = dot(d, d);
90 float b2 = a2 / d2;
91 return a2 * b2 * b2 * (1.0 / PI);
92 }
93
94 float D_Charlie(float roughness, float NoH) {
95 // Estevez and Kulla 2017, "Production Friendly Microfacet Sheen BRDF"
96 float invAlpha = 1.0 / roughness;
97 float cos2h = NoH * NoH;
98 float sin2h = max(1.0 - cos2h, 0.0078125); // 2^(-14/2), so sin2h^2 > 0 in fp16
99 return (2.0 + invAlpha) * pow(sin2h, invAlpha * 0.5) / (2.0 * PI);
100 }
101
102 float V_SmithGGXCorrelated(float roughness, float NoV, float NoL) {
103 // Heitz 2014, "Understanding the Masking-Shadowing Function in Microfacet-Based BRDFs"
104 float a2 = roughness * roughness;
105 // TODO: lambdaV can be pre-computed for all the lights, it should be moved out of this function
106 float lambdaV = NoL * sqrt((NoV - a2 * NoV) * NoV + a2);
107 float lambdaL = NoV * sqrt((NoL - a2 * NoL) * NoL + a2);
108 float v = 0.5 / (lambdaV + lambdaL);
109 // a2=0 => v = 1 / 4*NoL*NoV => min=1/4, max=+inf
110 // a2=1 => v = 1 / 2*(NoL+NoV) => min=1/4, max=+inf
111 // clamp to the maximum value representable in mediump
112 return saturateMediump(v);
113 }
114
115 float V_SmithGGXCorrelated_Fast(float roughness, float NoV, float NoL) {
116 // Hammon 2017, "PBR Diffuse Lighting for GGX+Smith Microsurfaces"
117 float v = 0.5 / mix(2.0 * NoL * NoV, NoL + NoV, roughness);
118 return saturateMediump(v);
119 }
120
121 float V_SmithGGXCorrelated_Anisotropic(float at, float ab, float ToV, float BoV,
122 float ToL, float BoL, float NoV, float NoL) {
123 // Heitz 2014, "Understanding the Masking-Shadowing Function in Microfacet-Based BRDFs"
124 // TODO: lambdaV can be pre-computed for all the lights, it should be moved out of this function
125 float lambdaV = NoL * length(vec3(at * ToV, ab * BoV, NoV));
126 float lambdaL = NoV * length(vec3(at * ToL, ab * BoL, NoL));
127 float v = 0.5 / (lambdaV + lambdaL);
128 return saturateMediump(v);
129 }
130
131 float V_Kelemen(float LoH) {
132 // Kelemen 2001, "A Microfacet Based Coupled Specular-Matte BRDF Model with Importance Sampling"
133 return saturateMediump(0.25 / (LoH * LoH));
134 }
135
136 float V_Neubelt(float NoV, float NoL) {
137 // Neubelt and Pettineo 2013, "Crafting a Next-gen Material Pipeline for The Order: 1886"
138 return saturateMediump(1.0 / (4.0 * (NoL + NoV - NoL * NoV)));
139 }
140
141 vec3 F_Schlick(const vec3 f0, float f90, float VoH) {
142 // Schlick 1994, "An Inexpensive BRDF Model for Physically-Based Rendering"
143 return f0 + (f90 - f0) * pow5(1.0 - VoH);
144 }
145
146 vec3 F_Schlick(const vec3 f0, float VoH) {
147 float f = pow(1.0 - VoH, 5.0);
148 return f + f0 * (1.0 - f);
149 }
150
151 float F_Schlick(float f0, float f90, float VoH) {
152 return f0 + (f90 - f0) * pow5(1.0 - VoH);
153 }
154
155 //------------------------------------------------------------------------------
156 // Specular BRDF dispatch
157 //------------------------------------------------------------------------------
158
159 float distribution(float roughness, float NoH, const vec3 h) {
160 #if BRDF_SPECULAR_D == SPECULAR_D_GGX
161 return D_GGX(roughness, NoH, h);
162 #endif
163 }
164
165 float visibility(float roughness, float NoV, float NoL) {
166 #if BRDF_SPECULAR_V == SPECULAR_V_SMITH_GGX
167 return V_SmithGGXCorrelated(roughness, NoV, NoL);
168 #elif BRDF_SPECULAR_V == SPECULAR_V_SMITH_GGX_FAST
169 return V_SmithGGXCorrelated_Fast(roughness, NoV, NoL);
170 #endif
171 }
172
173 vec3 fresnel(const vec3 f0, float LoH) {
174 #if BRDF_SPECULAR_F == SPECULAR_F_SCHLICK
175 #if FILAMENT_QUALITY == FILAMENT_QUALITY_LOW
176 return F_Schlick(f0, LoH); // f90 = 1.0
177 #else
178 float f90 = saturate(dot(f0, vec3(50.0 * 0.33)));
179 return F_Schlick(f0, f90, LoH);
180 #endif
181 #endif
182 }
183
184 float distributionAnisotropic(float at, float ab, float ToH, float BoH, float NoH) {
185 #if BRDF_ANISOTROPIC_D == SPECULAR_D_GGX_ANISOTROPIC
186 return D_GGX_Anisotropic(at, ab, ToH, BoH, NoH);
187 #endif
188 }
189
190 float visibilityAnisotropic(float roughness, float at, float ab,
191 float ToV, float BoV, float ToL, float BoL, float NoV, float NoL) {
192 #if BRDF_ANISOTROPIC_V == SPECULAR_V_SMITH_GGX
193 return V_SmithGGXCorrelated(roughness, NoV, NoL);
194 #elif BRDF_ANISOTROPIC_V == SPECULAR_V_GGX_ANISOTROPIC
195 return V_SmithGGXCorrelated_Anisotropic(at, ab, ToV, BoV, ToL, BoL, NoV, NoL);
196 #endif
197 }
198
199 float distributionClearCoat(float roughness, float NoH, const vec3 h) {
200 #if BRDF_CLEAR_COAT_D == SPECULAR_D_GGX
201 return D_GGX(roughness, NoH, h);
202 #endif
203 }
204
205 float visibilityClearCoat(float LoH) {
206 #if BRDF_CLEAR_COAT_V == SPECULAR_V_KELEMEN
207 return V_Kelemen(LoH);
208 #endif
209 }
210
211 float distributionCloth(float roughness, float NoH) {
212 #if BRDF_CLOTH_D == SPECULAR_D_CHARLIE
213 return D_Charlie(roughness, NoH);
214 #endif
215 }
216
217 float visibilityCloth(float NoV, float NoL) {
218 #if BRDF_CLOTH_V == SPECULAR_V_NEUBELT
219 return V_Neubelt(NoV, NoL);
220 #endif
221 }
222
223 //------------------------------------------------------------------------------
224 // Diffuse BRDF implementations
225 //------------------------------------------------------------------------------
226
227 float Fd_Lambert() {
228 return 1.0 / PI;
229 }
230
231 float Fd_Burley(float roughness, float NoV, float NoL, float LoH) {
232 // Burley 2012, "Physically-Based Shading at Disney"
233 float f90 = 0.5 + 2.0 * roughness * LoH * LoH;
234 float lightScatter = F_Schlick(1.0, f90, NoL);
235 float viewScatter = F_Schlick(1.0, f90, NoV);
236 return lightScatter * viewScatter * (1.0 / PI);
237 }
238
239 // Energy conserving wrap diffuse term, does *not* include the divide by pi
240 float Fd_Wrap(float NoL, float w) {
241 return saturate((NoL + w) / sq(1.0 + w));
242 }
243
244 //------------------------------------------------------------------------------
245 // Diffuse BRDF dispatch
246 //------------------------------------------------------------------------------
247
248 float diffuse(float roughness, float NoV, float NoL, float LoH) {
249 #if BRDF_DIFFUSE == DIFFUSE_LAMBERT
250 return Fd_Lambert();
251 #elif BRDF_DIFFUSE == DIFFUSE_BURLEY
252 return Fd_Burley(roughness, NoV, NoL, LoH);
253 #endif
254 }