Eevee: Hair: Disable SSR for hairs.
[blender.git] / source / blender / draw / engines / eevee / shaders / lit_surface_frag.glsl
1
2 #ifndef LIT_SURFACE_UNIFORM
3 #define LIT_SURFACE_UNIFORM
4
5 uniform float refractionDepth;
6
7 #ifndef UTIL_TEX
8 #define UTIL_TEX
9 uniform sampler2DArray utilTex;
10 #define texelfetch_noise_tex(coord) texelFetch(utilTex, ivec3(ivec2(coord) % LUT_SIZE, 2.0), 0)
11 #endif /* UTIL_TEX */
12
13 in vec3 worldPosition;
14 in vec3 viewPosition;
15
16 #ifdef USE_FLAT_NORMAL
17 flat in vec3 worldNormal;
18 flat in vec3 viewNormal;
19 #else
20 in vec3 worldNormal;
21 in vec3 viewNormal;
22 #endif
23
24 #endif /* LIT_SURFACE_UNIFORM */
25
26 /** AUTO CONFIG
27  * We include the file multiple times each time with a different configuration.
28  * This leads to a lot of deadcode. Better idea would be to only generate the one needed.
29  */
30 #if !defined(SURFACE_DEFAULT)
31         #define SURFACE_DEFAULT
32         #define CLOSURE_NAME eevee_closure_default
33         #define CLOSURE_DIFFUSE
34         #define CLOSURE_GLOSSY
35 #endif /* SURFACE_DEFAULT */
36
37 #if !defined(SURFACE_PRINCIPLED) && !defined(CLOSURE_NAME)
38         #define SURFACE_PRINCIPLED
39         #define CLOSURE_NAME eevee_closure_principled
40         #define CLOSURE_DIFFUSE
41         #define CLOSURE_GLOSSY
42         #define CLOSURE_CLEARCOAT
43         #define CLOSURE_REFRACTION
44         #define CLOSURE_SUBSURFACE
45 #endif /* SURFACE_PRINCIPLED */
46
47 #if !defined(SURFACE_DIFFUSE) && !defined(CLOSURE_NAME)
48         #define SURFACE_DIFFUSE
49         #define CLOSURE_NAME eevee_closure_diffuse
50         #define CLOSURE_DIFFUSE
51 #endif /* SURFACE_DIFFUSE */
52
53 #if !defined(SURFACE_SUBSURFACE) && !defined(CLOSURE_NAME)
54         #define SURFACE_SUBSURFACE
55         #define CLOSURE_NAME eevee_closure_subsurface
56         #define CLOSURE_DIFFUSE
57         #define CLOSURE_SUBSURFACE
58 #endif /* SURFACE_SUBSURFACE */
59
60 #if !defined(SURFACE_GLOSSY) && !defined(CLOSURE_NAME)
61         #define SURFACE_GLOSSY
62         #define CLOSURE_NAME eevee_closure_glossy
63         #define CLOSURE_GLOSSY
64 #endif /* SURFACE_GLOSSY */
65
66 #if !defined(SURFACE_REFRACT) && !defined(CLOSURE_NAME)
67         #define SURFACE_REFRACT
68         #define CLOSURE_NAME eevee_closure_refraction
69         #define CLOSURE_REFRACTION
70 #endif /* SURFACE_REFRACT */
71
72 #if !defined(SURFACE_GLASS) && !defined(CLOSURE_NAME)
73         #define SURFACE_GLASS
74         #define CLOSURE_NAME eevee_closure_glass
75         #define CLOSURE_GLOSSY
76         #define CLOSURE_REFRACTION
77 #endif /* SURFACE_GLASS */
78
79 /* Safety : CLOSURE_CLEARCOAT implies CLOSURE_GLOSSY */
80 #ifdef CLOSURE_CLEARCOAT
81         #ifndef CLOSURE_GLOSSY
82                 #define CLOSURE_GLOSSY
83         #endif
84 #endif /* CLOSURE_CLEARCOAT */
85
86 void CLOSURE_NAME(
87         vec3 N
88 #ifdef CLOSURE_DIFFUSE
89         , vec3 albedo
90 #endif
91 #ifdef CLOSURE_GLOSSY
92         , vec3 f0, int ssr_id
93 #endif
94 #if defined(CLOSURE_GLOSSY) || defined(CLOSURE_REFRACTION)
95         , float roughness
96 #endif
97 #ifdef CLOSURE_CLEARCOAT
98         , vec3 C_N, float C_intensity, float C_roughness
99 #endif
100 #if defined(CLOSURE_GLOSSY) || defined(CLOSURE_DIFFUSE)
101         , float ao
102 #endif
103 #ifdef CLOSURE_SUBSURFACE
104         , float sss_scale
105 #endif
106 #ifdef CLOSURE_REFRACTION
107         , float ior
108 #endif
109 #ifdef CLOSURE_DIFFUSE
110         , out vec3 out_diff
111 #endif
112 #ifdef CLOSURE_SUBSURFACE
113         , out vec3 out_trans
114 #endif
115 #ifdef CLOSURE_GLOSSY
116         , out vec3 out_spec
117 #endif
118 #ifdef CLOSURE_REFRACTION
119         , out vec3 out_refr
120 #endif
121 #ifdef CLOSURE_GLOSSY
122         , out vec3 ssr_spec
123 #endif
124         )
125 {
126 #ifdef CLOSURE_DIFFUSE
127         out_diff = vec3(0.0);
128 #endif
129
130 #ifdef CLOSURE_SUBSURFACE
131         out_trans = vec3(0.0);
132 #endif
133
134 #ifdef CLOSURE_GLOSSY
135         out_spec = vec3(0.0);
136 #endif
137
138 #ifdef CLOSURE_REFRACTION
139         out_refr = vec3(0.0);
140 #endif
141
142         /* Zero length vectors cause issues, see: T51979. */
143         float len = length(N);
144         if (isnan(len)) {
145                 return;
146         }
147         N /= len;
148
149 #ifdef CLOSURE_CLEARCOAT
150         len = length(C_N);
151         if (isnan(len)) {
152                 return;
153         }
154         C_N /= len;
155 #endif
156
157 #if defined(CLOSURE_GLOSSY) || defined(CLOSURE_REFRACTION)
158         roughness = clamp(roughness, 1e-8, 0.9999);
159         float roughnessSquared = roughness * roughness;
160 #endif
161
162 #ifdef CLOSURE_CLEARCOAT
163         C_roughness = clamp(C_roughness, 1e-8, 0.9999);
164         float C_roughnessSquared = C_roughness * C_roughness;
165 #endif
166
167         vec3 V = cameraVec;
168
169         vec4 rand = texelFetch(utilTex, ivec3(ivec2(gl_FragCoord.xy) % LUT_SIZE, 2.0), 0);
170
171 #ifdef HAIR_SHADER
172         /* Random normal distribution on the hair surface. */
173         vec3 T = normalize(worldNormal); /* meh, TODO fix worldNormal misnaming. */
174         vec3 B = normalize(cross(V, T));
175         N = cross(T, B); /* Normal facing view */
176         /* We want a cosine distribution. */
177         float cos_theta = rand.x * 2.0 - 1.0;
178         float sin_theta = sqrt(max(0.0, 1.0f - cos_theta*cos_theta));;
179         N = N * sin_theta + B * cos_theta;
180
181 #  ifdef CLOSURE_GLOSSY
182         /* Hair random normal does not work with SSR :(.
183          * It just create self reflection feedback (which is beautifful btw)
184          * but not correct. */
185         ssr_id = NO_SSR; /* Force bypass */
186 #  endif
187 #endif
188
189         /* ---------------------------------------------------------------- */
190         /* -------------------- SCENE LAMPS LIGHTING ---------------------- */
191         /* ---------------------------------------------------------------- */
192
193         for (int i = 0; i < MAX_LIGHT && i < laNumLight; ++i) {
194                 LightData ld = lights_data[i];
195
196                 vec4 l_vector; /* Non-Normalized Light Vector with length in last component. */
197                 l_vector.xyz = ld.l_position - worldPosition;
198                 l_vector.w = length(l_vector.xyz);
199
200                 vec3 l_color_vis = ld.l_color * light_visibility(ld, worldPosition, viewPosition, viewNormal, l_vector);
201
202         #ifdef CLOSURE_DIFFUSE
203                 out_diff += l_color_vis * light_diffuse(ld, N, V, l_vector);
204         #endif
205
206         #ifdef CLOSURE_SUBSURFACE
207                 out_trans += ld.l_color * light_translucent(ld, worldPosition, -N, l_vector, sss_scale);
208         #endif
209
210         #ifdef CLOSURE_GLOSSY
211                 out_spec += l_color_vis * light_specular(ld, N, V, l_vector, roughnessSquared, f0) * ld.l_spec;
212         #endif
213
214         #ifdef CLOSURE_CLEARCOAT
215                 out_spec += l_color_vis * light_specular(ld, C_N, V, l_vector, C_roughnessSquared, f0) * C_intensity * ld.l_spec;
216         #endif
217         }
218
219
220         /* ---------------------------------------------------------------- */
221         /* ---------------- SPECULAR ENVIRONMENT LIGHTING ----------------- */
222         /* ---------------------------------------------------------------- */
223
224         /* Accumulate incomming light from all sources until accumulator is full. Then apply Occlusion and BRDF. */
225 #ifdef CLOSURE_GLOSSY
226         vec4 spec_accum = vec4(0.0);
227 #endif
228
229 #ifdef CLOSURE_CLEARCOAT
230         vec4 C_spec_accum = vec4(0.0);
231 #endif
232
233 #ifdef CLOSURE_REFRACTION
234         vec4 refr_accum = vec4(0.0);
235 #endif
236
237 #ifdef CLOSURE_GLOSSY
238         /* ---------------------------- */
239         /*      Planar Reflections      */
240         /* ---------------------------- */
241
242         for (int i = 0; i < MAX_PLANAR && i < prbNumPlanar && spec_accum.a < 0.999; ++i) {
243                 PlanarData pd = planars_data[i];
244
245                 /* Fade on geometric normal. */
246                 float fade = probe_attenuation_planar(pd, worldPosition, worldNormal, roughness);
247
248                 if (fade > 0.0) {
249                         if (!(ssrToggle && ssr_id == outputSsrId)) {
250                                 vec3 spec = probe_evaluate_planar(float(i), pd, worldPosition, N, V, roughness, fade);
251                                 accumulate_light(spec, fade, spec_accum);
252                         }
253
254         #ifdef CLOSURE_CLEARCOAT
255                         vec3 C_spec = probe_evaluate_planar(float(i), pd, worldPosition, C_N, V, C_roughness, fade);
256                         accumulate_light(C_spec, fade, C_spec_accum);
257         #endif
258
259                 }
260         }
261 #endif
262
263
264 #ifdef CLOSURE_GLOSSY
265         vec3 spec_dir = get_specular_reflection_dominant_dir(N, V, roughnessSquared);
266 #endif
267
268 #ifdef CLOSURE_CLEARCOAT
269         vec3 C_spec_dir = get_specular_reflection_dominant_dir(C_N, V, C_roughnessSquared);
270 #endif
271
272 #ifdef CLOSURE_REFRACTION
273         /* Refract the view vector using the depth heuristic.
274          * Then later Refract a second time the already refracted
275          * ray using the inverse ior. */
276         float final_ior = (refractionDepth > 0.0) ? 1.0 / ior : ior;
277         vec3 refr_V = (refractionDepth > 0.0) ? -refract(-V, N, final_ior) : V;
278         vec3 refr_pos = (refractionDepth > 0.0) ? line_plane_intersect(worldPosition, refr_V, worldPosition - N * refractionDepth, N) : worldPosition;
279         vec3 refr_dir = get_specular_refraction_dominant_dir(N, refr_V, roughness, final_ior);
280 #endif
281
282
283 #ifdef CLOSURE_REFRACTION
284         /* ---------------------------- */
285         /*   Screen Space Refraction    */
286         /* ---------------------------- */
287         #ifdef USE_REFRACTION
288         if (ssrToggle && roughness < ssrMaxRoughness + 0.2) {
289                 /* Find approximated position of the 2nd refraction event. */
290                 vec3 refr_vpos = (refractionDepth > 0.0) ? transform_point(ViewMatrix, refr_pos) : viewPosition;
291                 vec4 trans = screen_space_refraction(refr_vpos, N, refr_V, final_ior, roughnessSquared, rand);
292                 trans.a *= smoothstep(ssrMaxRoughness + 0.2, ssrMaxRoughness, roughness);
293                 accumulate_light(trans.rgb, trans.a, refr_accum);
294         }
295         #endif
296
297 #endif
298
299
300         /* ---------------------------- */
301         /*       Specular probes        */
302         /* ---------------------------- */
303 #if defined(CLOSURE_GLOSSY) || defined(CLOSURE_REFRACTION)
304
305         #ifdef CLOSURE_REFRACTION
306                 #define ACCUM refr_accum
307         #else
308                 #define ACCUM spec_accum
309         #endif
310
311         /* Starts at 1 because 0 is world probe */
312         for (int i = 1; ACCUM.a < 0.999 && i < prbNumRenderCube && i < MAX_PROBE; ++i) {
313                 CubeData cd = probes_data[i];
314
315                 float fade = probe_attenuation_cube(cd, worldPosition);
316
317                 if (fade > 0.0) {
318
319         #ifdef CLOSURE_GLOSSY
320                         if (!(ssrToggle && ssr_id == outputSsrId)) {
321                                 vec3 spec = probe_evaluate_cube(float(i), cd, worldPosition, spec_dir, roughness);
322                                 accumulate_light(spec, fade, spec_accum);
323                         }
324         #endif
325
326         #ifdef CLOSURE_CLEARCOAT
327                         vec3 C_spec = probe_evaluate_cube(float(i), cd, worldPosition, C_spec_dir, C_roughness);
328                         accumulate_light(C_spec, fade, C_spec_accum);
329         #endif
330
331         #ifdef CLOSURE_REFRACTION
332                         vec3 trans = probe_evaluate_cube(float(i), cd, refr_pos, refr_dir, roughnessSquared);
333                         accumulate_light(trans, fade, refr_accum);
334         #endif
335                 }
336         }
337
338         #undef ACCUM
339
340         /* ---------------------------- */
341         /*          World Probe         */
342         /* ---------------------------- */
343         #ifdef CLOSURE_GLOSSY
344         if (spec_accum.a < 0.999) {
345                 if (!(ssrToggle && ssr_id == outputSsrId)) {
346                         vec3 spec = probe_evaluate_world_spec(spec_dir, roughness);
347                         accumulate_light(spec, 1.0, spec_accum);
348                 }
349
350                 #ifdef CLOSURE_CLEARCOAT
351                 vec3 C_spec = probe_evaluate_world_spec(C_spec_dir, C_roughness);
352                 accumulate_light(C_spec, 1.0, C_spec_accum);
353                 #endif
354
355         }
356         #endif
357
358         #ifdef CLOSURE_REFRACTION
359         if (refr_accum.a < 0.999) {
360                 vec3 trans = probe_evaluate_world_spec(refr_dir, roughnessSquared);
361                 accumulate_light(trans, 1.0, refr_accum);
362         }
363         #endif
364 #endif /* Specular probes */ 
365
366
367         /* ---------------------------- */
368         /*       Ambient Occlusion      */
369         /* ---------------------------- */
370 #if defined(CLOSURE_GLOSSY) || defined(CLOSURE_DIFFUSE)
371         vec3 bent_normal;
372         float final_ao = occlusion_compute(N, viewPosition, ao, rand, bent_normal);
373 #endif
374
375
376         /* ---------------------------- */
377         /*        Specular Output       */
378         /* ---------------------------- */
379         float NV = dot(N, V);
380 #ifdef CLOSURE_GLOSSY
381         vec2 uv = lut_coords(NV, roughness);
382         vec2 brdf_lut = texture(utilTex, vec3(uv, 1.0)).rg;
383
384         /* This factor is outputed to be used by SSR in order
385          * to match the intensity of the regular reflections. */
386         ssr_spec = F_ibl(f0, brdf_lut);
387         float spec_occlu = specular_occlusion(NV, final_ao, roughness);
388
389         /* The SSR pass recompute the occlusion to not apply it to the SSR */
390         if (ssrToggle && ssr_id == outputSsrId) {
391                 spec_occlu = 1.0;
392         }
393
394         out_spec += spec_accum.rgb * ssr_spec * spec_occlu * float(specToggle);
395
396 #  ifdef HAIR_SHADER
397         /* Hack: Overide spec color so that ssr will not be computed
398          * even if ssr_id match the active ssr. */
399         ssr_spec = vec3(0.0);
400 #  endif
401 #endif
402
403 #ifdef CLOSURE_REFRACTION
404         float btdf = get_btdf_lut(utilTex, NV, roughness, ior);
405
406         out_refr += refr_accum.rgb * btdf;
407 #endif
408
409 #ifdef CLOSURE_CLEARCOAT
410         NV = dot(C_N, V);
411         vec2 C_uv = lut_coords(NV, C_roughness);
412         vec2 C_brdf_lut = texture(utilTex, vec3(C_uv, 1.0)).rg;
413         vec3 C_fresnel = F_ibl(vec3(0.04), brdf_lut) * specular_occlusion(NV, final_ao, C_roughness);
414
415         out_spec += C_spec_accum.rgb * C_fresnel * float(specToggle) * C_intensity;
416 #endif
417
418         /* ---------------------------------------------------------------- */
419         /* ---------------- DIFFUSE ENVIRONMENT LIGHTING ------------------ */
420         /* ---------------------------------------------------------------- */
421
422         /* Accumulate light from all sources until accumulator is full. Then apply Occlusion and BRDF. */
423 #ifdef CLOSURE_DIFFUSE
424         vec4 diff_accum = vec4(0.0);
425
426         /* ---------------------------- */
427         /*       Irradiance Grids       */
428         /* ---------------------------- */
429         /* Start at 1 because 0 is world irradiance */
430         for (int i = 1; i < MAX_GRID && i < prbNumRenderGrid && diff_accum.a < 0.999; ++i) {
431                 GridData gd = grids_data[i];
432
433                 vec3 localpos;
434                 float fade = probe_attenuation_grid(gd, worldPosition, localpos);
435
436                 if (fade > 0.0) {
437                         vec3 diff = probe_evaluate_grid(gd, worldPosition, bent_normal, localpos);
438                         accumulate_light(diff, fade, diff_accum);
439                 }
440         }
441
442         /* ---------------------------- */
443         /*        World Diffuse         */
444         /* ---------------------------- */
445         if (diff_accum.a < 0.999 && prbNumRenderGrid > 0) {
446                 vec3 diff = probe_evaluate_world_diff(bent_normal);
447                 accumulate_light(diff, 1.0, diff_accum);
448         }
449
450         out_diff += diff_accum.rgb * gtao_multibounce(final_ao, albedo);
451 #endif
452 }
453
454 /* Cleanup for next configuration */
455 #undef CLOSURE_NAME
456
457 #ifdef CLOSURE_DIFFUSE
458         #undef CLOSURE_DIFFUSE
459 #endif
460
461 #ifdef CLOSURE_GLOSSY
462         #undef CLOSURE_GLOSSY
463 #endif
464
465 #ifdef CLOSURE_CLEARCOAT
466         #undef CLOSURE_CLEARCOAT
467 #endif
468
469 #ifdef CLOSURE_REFRACTION
470         #undef CLOSURE_REFRACTION
471 #endif
472
473 #ifdef CLOSURE_SUBSURFACE
474         #undef CLOSURE_SUBSURFACE
475 #endif