Welcome to mirror list, hosted at ThFree Co, Russian Federation.

lit_surface_frag.glsl « shaders « eevee « engines « draw « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f63a9665810430f5ece7fadfe12ef5f536d2658b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812

uniform int light_count;
uniform int probe_count;
uniform int grid_count;
uniform int planar_count;

uniform bool specToggle;
uniform bool ssrToggle;

uniform float refractionDepth;

#ifndef UTIL_TEX
#define UTIL_TEX
uniform sampler2DArray utilTex;
#endif /* UTIL_TEX */

in vec3 worldPosition;
in vec3 viewPosition;

#ifdef USE_FLAT_NORMAL
flat in vec3 worldNormal;
flat in vec3 viewNormal;
#else
in vec3 worldNormal;
in vec3 viewNormal;
#endif

uniform float maxRoughness;
uniform int rayCount;

/* ----------- default -----------  */

vec3 eevee_surface_lit(vec3 N, vec3 albedo, vec3 f0, float roughness, float ao, int ssr_id, out vec3 ssr_spec)
{
	/* Zero length vectors cause issues, see: T51979. */
#if 0
	N = normalize(N);
#else
	{
		float len = length(N);
		if (isnan(len)) {
			return vec3(0.0);
		}
		N /= len;
	}
#endif

	roughness = clamp(roughness, 1e-8, 0.9999);
	float roughnessSquared = roughness * roughness;

	vec3 V = cameraVec;

	/* ---------------- SCENE LAMPS LIGHTING ----------------- */

#ifdef HAIR_SHADER
	vec3 norm_view = cross(V, N);
	norm_view = normalize(cross(norm_view, N)); /* Normal facing view */
#endif

	vec3 diff = vec3(0.0);
	vec3 spec = vec3(0.0);
	for (int i = 0; i < MAX_LIGHT && i < light_count; ++i) {
		LightData ld = lights_data[i];

		vec4 l_vector; /* Non-Normalized Light Vector with length in last component. */
		l_vector.xyz = ld.l_position - worldPosition;
		l_vector.w = length(l_vector.xyz);

		vec3 l_color_vis = ld.l_color * light_visibility(ld, worldPosition, viewPosition, viewNormal, l_vector);

#ifdef HAIR_SHADER
		vec3 norm_lamp, view_vec;
		float occlu_trans, occlu;
		light_hair_common(ld, N, V, l_vector, norm_view, occlu_trans, occlu, norm_lamp, view_vec);

		diff += l_color_vis * light_diffuse(ld, -norm_lamp, V, l_vector) * occlu_trans;
		spec += l_color_vis * light_specular(ld, N, view_vec, l_vector, roughnessSquared, f0) * occlu;
#else
		diff += l_color_vis * light_diffuse(ld, N, V, l_vector);
		spec += l_color_vis * light_specular(ld, N, V, l_vector, roughnessSquared, f0);
#endif
	}

	/* Accumulate outgoing radiance */
	vec3 out_light = diff * albedo + spec * float(specToggle);

#ifdef HAIR_SHADER
	N = -norm_view;
#endif

	/* ---------------- SPECULAR ENVIRONMENT LIGHTING ----------------- */

	/* Accumulate light from all sources until accumulator is full. Then apply Occlusion and BRDF. */
	vec4 spec_accum = vec4(0.0);

	/* SSR lobe is applied later in a defered style */
	if (!(ssrToggle && ssr_id == outputSsrId)) {
		/* Planar Reflections */
		for (int i = 0; i < MAX_PLANAR && i < planar_count && spec_accum.a < 0.999; ++i) {
			PlanarData pd = planars_data[i];

			float fade = probe_attenuation_planar(pd, worldPosition, N, roughness);

			if (fade > 0.0) {
				vec3 spec = probe_evaluate_planar(float(i), pd, worldPosition, N, V, roughness, fade);
				accumulate_light(spec, fade, spec_accum);
			}
		}

		/* Specular probes */
		vec3 spec_dir = get_specular_reflection_dominant_dir(N, V, roughnessSquared);

		/* Starts at 1 because 0 is world probe */
		for (int i = 1; i < MAX_PROBE && i < probe_count && spec_accum.a < 0.999; ++i) {
			CubeData cd = probes_data[i];

			float fade = probe_attenuation_cube(cd, worldPosition);

			if (fade > 0.0) {
				vec3 spec = probe_evaluate_cube(float(i), cd, worldPosition, spec_dir, roughness);
				accumulate_light(spec, fade, spec_accum);
			}
		}

		/* World Specular */
		if (spec_accum.a < 0.999) {
			vec3 spec = probe_evaluate_world_spec(spec_dir, roughness);
			accumulate_light(spec, 1.0, spec_accum);
		}
	}

	vec4 rand = texture(utilTex, vec3(gl_FragCoord.xy / LUT_SIZE, 2.0));

	/* Ambient Occlusion */
	vec3 bent_normal;
	float final_ao = occlusion_compute(N, viewPosition, ao, rand.rg, bent_normal);

	/* Get Brdf intensity */
	vec2 uv = lut_coords(dot(N, V), roughness);
	vec2 brdf_lut = texture(utilTex, vec3(uv, 1.0)).rg;

	ssr_spec = F_ibl(f0, brdf_lut);
	if (!(ssrToggle && ssr_id == outputSsrId)) {
		ssr_spec *= specular_occlusion(dot(N, V), final_ao, roughness);
	}
	out_light += spec_accum.rgb * ssr_spec * float(specToggle);

	/* ---------------- DIFFUSE ENVIRONMENT LIGHTING ----------------- */

	/* Accumulate light from all sources until accumulator is full. Then apply Occlusion and BRDF. */
	vec4 diff_accum = vec4(0.0);

	/* Start at 1 because 0 is world irradiance */
	for (int i = 1; i < MAX_GRID && i < grid_count && diff_accum.a < 0.999; ++i) {
		GridData gd = grids_data[i];

		vec3 localpos;
		float fade = probe_attenuation_grid(gd, worldPosition, localpos);

		if (fade > 0.0) {
			vec3 diff = probe_evaluate_grid(gd, worldPosition, bent_normal, localpos);
			accumulate_light(diff, fade, diff_accum);
		}
	}

	/* World Diffuse */
	if (diff_accum.a < 0.999 && grid_count > 0) {
		vec3 diff = probe_evaluate_world_diff(bent_normal);
		accumulate_light(diff, 1.0, diff_accum);
	}

	out_light += diff_accum.rgb * albedo * gtao_multibounce(final_ao, albedo);

	return out_light;
}

/* ----------- CLEAR COAT -----------  */

vec3 eevee_surface_clearcoat_lit(
        vec3 N, vec3 albedo, vec3 f0, float roughness,
        vec3 C_N, float C_intensity, float C_roughness, /* Clearcoat params */
        float ao, int ssr_id, out vec3 ssr_spec)
{
	roughness = clamp(roughness, 1e-8, 0.9999);
	float roughnessSquared = roughness * roughness;
	C_roughness = clamp(C_roughness, 1e-8, 0.9999);
	float C_roughnessSquared = C_roughness * C_roughness;

	/* Zero length vectors cause issues, see: T51979. */
#if 0
	N = normalize(N);
	C_N = normalize(C_N);
#else
	{
		float len = length(N);
		if (isnan(len)) {
			return vec3(0.0);
		}
		N /= len;

		len = length(C_N);
		if (isnan(len)) {
			return vec3(0.0);
		}
		C_N /= len;
	}
#endif

	vec3 V = cameraVec;

	/* ---------------- SCENE LAMPS LIGHTING ----------------- */

#ifdef HAIR_SHADER
	vec3 norm_view = cross(V, N);
	norm_view = normalize(cross(norm_view, N)); /* Normal facing view */
#endif

	vec3 diff = vec3(0.0);
	vec3 spec = vec3(0.0);
	for (int i = 0; i < MAX_LIGHT && i < light_count; ++i) {
		LightData ld = lights_data[i];

		vec4 l_vector; /* Non-Normalized Light Vector with length in last component. */
		l_vector.xyz = ld.l_position - worldPosition;
		l_vector.w = length(l_vector.xyz);

		vec3 l_color_vis = ld.l_color * light_visibility(ld, worldPosition, viewPosition, viewNormal, l_vector);

#ifdef HAIR_SHADER
		vec3 norm_lamp, view_vec;
		float occlu_trans, occlu;
		light_hair_common(ld, N, V, l_vector, norm_view, occlu_trans, occlu, norm_lamp, view_vec);

		diff += l_color_vis * light_diffuse(ld, -norm_lamp, V, l_vector) * occlu_trans;
		spec += l_color_vis * light_specular(ld, N, view_vec, l_vector, roughnessSquared, f0) * occlu;
		spec += l_color_vis * light_specular(ld, C_N, view_vec, l_vector, C_roughnessSquared, f0) * C_intensity * occlu;
#else
		diff += l_color_vis * light_diffuse(ld, N, V, l_vector);
		spec += l_color_vis * light_specular(ld, N, V, l_vector, roughnessSquared, f0);
		spec += l_color_vis * light_specular(ld, C_N, V, l_vector, C_roughnessSquared, f0) * C_intensity;
#endif
	}

	/* Accumulate outgoing radiance */
	vec3 out_light = diff * albedo + spec * float(specToggle);

#ifdef HAIR_SHADER
	N = -norm_view;
#endif

	/* ---------------- SPECULAR ENVIRONMENT LIGHTING ----------------- */

	/* Accumulate light from all sources until accumulator is full. Then apply Occlusion and BRDF. */
	vec4 spec_accum = vec4(0.0);
	vec4 C_spec_accum = vec4(0.0);

	/* Planar Reflections */
	for (int i = 0; i < MAX_PLANAR && i < planar_count && spec_accum.a < 0.999; ++i) {
		PlanarData pd = planars_data[i];

		/* Fade on geometric normal. */
		float fade = probe_attenuation_planar(pd, worldPosition, worldNormal, roughness);

		if (fade > 0.0) {
			if (!(ssrToggle && ssr_id == outputSsrId)) {
				vec3 spec = probe_evaluate_planar(float(i), pd, worldPosition, N, V, roughness, fade);
				accumulate_light(spec, fade, spec_accum);
			}

			vec3 C_spec = probe_evaluate_planar(float(i), pd, worldPosition, C_N, V, C_roughness, fade);
			accumulate_light(C_spec, fade, C_spec_accum);
		}
	}

	/* Specular probes */
	vec3 spec_dir = get_specular_reflection_dominant_dir(N, V, roughnessSquared);
	vec3 C_spec_dir = get_specular_reflection_dominant_dir(C_N, V, C_roughnessSquared);

	/* Starts at 1 because 0 is world probe */
	for (int i = 1; i < MAX_PROBE && i < probe_count && spec_accum.a < 0.999; ++i) {
		CubeData cd = probes_data[i];

		float fade = probe_attenuation_cube(cd, worldPosition);

		if (fade > 0.0) {
			if (!(ssrToggle && ssr_id == outputSsrId)) {
				vec3 spec = probe_evaluate_cube(float(i), cd, worldPosition, spec_dir, roughness);
				accumulate_light(spec, fade, spec_accum);
			}

			vec3 C_spec = probe_evaluate_cube(float(i), cd, worldPosition, C_spec_dir, C_roughness);
			accumulate_light(C_spec, fade, C_spec_accum);
		}
	}

	/* World Specular */
	if (spec_accum.a < 0.999) {
		if (!(ssrToggle && ssr_id == outputSsrId)) {
			vec3 spec = probe_evaluate_world_spec(spec_dir, roughness);
			accumulate_light(spec, 1.0, spec_accum);
		}

		vec3 C_spec = probe_evaluate_world_spec(C_spec_dir, C_roughness);
		accumulate_light(C_spec, 1.0, C_spec_accum);
	}

	vec4 rand = texture(utilTex, vec3(gl_FragCoord.xy / LUT_SIZE, 2.0));

	/* Ambient Occlusion */
	vec3 bent_normal;
	float final_ao = occlusion_compute(N, viewPosition, ao, rand.rg, bent_normal);

	/* Get Brdf intensity */
	vec2 uv = lut_coords(dot(N, V), roughness);
	vec2 brdf_lut = texture(utilTex, vec3(uv, 1.0)).rg;

	ssr_spec = F_ibl(f0, brdf_lut);
	if (!(ssrToggle && ssr_id == outputSsrId)) {
		ssr_spec *= specular_occlusion(dot(N, V), final_ao, roughness);
	}
	out_light += spec_accum.rgb * ssr_spec * float(specToggle);

	uv = lut_coords(dot(C_N, V), C_roughness);
	brdf_lut = texture(utilTex, vec3(uv, 1.0)).rg;

	out_light += C_spec_accum.rgb * F_ibl(vec3(0.04), brdf_lut) * specular_occlusion(dot(C_N, V), final_ao, C_roughness) * float(specToggle) * C_intensity;

	/* ---------------- DIFFUSE ENVIRONMENT LIGHTING ----------------- */

	/* Accumulate light from all sources until accumulator is full. Then apply Occlusion and BRDF. */
	vec4 diff_accum = vec4(0.0);

	/* Start at 1 because 0 is world irradiance */
	for (int i = 1; i < MAX_GRID && i < grid_count && diff_accum.a < 0.999; ++i) {
		GridData gd = grids_data[i];

		vec3 localpos;
		float fade = probe_attenuation_grid(gd, worldPosition, localpos);

		if (fade > 0.0) {
			vec3 diff = probe_evaluate_grid(gd, worldPosition, bent_normal, localpos);
			accumulate_light(diff, fade, diff_accum);
		}
	}

	/* World Diffuse */
	if (diff_accum.a < 0.999 && grid_count > 0) {
		vec3 diff = probe_evaluate_world_diff(bent_normal);
		accumulate_light(diff, 1.0, diff_accum);
	}

	out_light += diff_accum.rgb * albedo * gtao_multibounce(final_ao, albedo);

	return out_light;
}

/* ----------- Diffuse -----------  */

vec3 eevee_surface_diffuse_lit(vec3 N, vec3 albedo, float ao)
{
	vec3 V = cameraVec;

	/* Zero length vectors cause issues, see: T51979. */
#if 0
	N = normalize(N);
#else
	{
		float len = length(N);
		if (isnan(len)) {
			return vec3(0.0);
		}
		N /= len;
	}
#endif

	/* ---------------- SCENE LAMPS LIGHTING ----------------- */

#ifdef HAIR_SHADER
	vec3 norm_view = cross(V, N);
	norm_view = normalize(cross(norm_view, N)); /* Normal facing view */
#endif

	vec3 diff = vec3(0.0);
	for (int i = 0; i < MAX_LIGHT && i < light_count; ++i) {
		LightData ld = lights_data[i];

		vec4 l_vector; /* Non-Normalized Light Vector with length in last component. */
		l_vector.xyz = ld.l_position - worldPosition;
		l_vector.w = length(l_vector.xyz);

		vec3 l_color_vis = ld.l_color * light_visibility(ld, worldPosition, viewPosition, viewNormal, l_vector);

#ifdef HAIR_SHADER
		vec3 norm_lamp, view_vec;
		float occlu_trans, occlu;
		light_hair_common(ld, N, V, l_vector, norm_view, occlu_trans, occlu, norm_lamp, view_vec);

		diff += l_color_vis * light_diffuse(ld, -norm_lamp, V, l_vector) * occlu_trans;
#else
		diff += l_color_vis * light_diffuse(ld, N, V, l_vector);
#endif
	}

	/* Accumulate outgoing radiance */
	vec3 out_light = diff * albedo;

#ifdef HAIR_SHADER
	N = -norm_view;
#endif

	/* ---------------- DIFFUSE ENVIRONMENT LIGHTING ----------------- */

	vec4 rand = texture(utilTex, vec3(gl_FragCoord.xy / LUT_SIZE, 2.0));

	/* Ambient Occlusion */
	vec3 bent_normal;
	float final_ao = occlusion_compute(N, viewPosition, ao, rand.rg, bent_normal);

	/* Accumulate light from all sources until accumulator is full. Then apply Occlusion and BRDF. */
	vec4 diff_accum = vec4(0.0);

	/* Start at 1 because 0 is world irradiance */
	for (int i = 1; i < MAX_GRID && i < grid_count && diff_accum.a < 0.999; ++i) {
		GridData gd = grids_data[i];

		vec3 localpos;
		float fade = probe_attenuation_grid(gd, worldPosition, localpos);

		if (fade > 0.0) {
			vec3 diff = probe_evaluate_grid(gd, worldPosition, bent_normal, localpos);
			accumulate_light(diff, fade, diff_accum);
		}
	}

	/* World Diffuse */
	if (diff_accum.a < 0.999 && grid_count > 0) {
		vec3 diff = probe_evaluate_world_diff(bent_normal);
		accumulate_light(diff, 1.0, diff_accum);
	}

	out_light += diff_accum.rgb * albedo * gtao_multibounce(final_ao, albedo);

	return out_light;
}

/* ----------- Glossy -----------  */

vec3 eevee_surface_glossy_lit(vec3 N, vec3 f0, float roughness, float ao, int ssr_id, out vec3 ssr_spec)
{
	roughness = clamp(roughness, 1e-8, 0.9999);
	float roughnessSquared = roughness * roughness;

	vec3 V = cameraVec;

	/* Zero length vectors cause issues, see: T51979. */
#if 0
	N = normalize(N);
#else
	{
		float len = length(N);
		if (isnan(len)) {
			return vec3(0.0);
		}
		N /= len;
	}
#endif

	/* ---------------- SCENE LAMPS LIGHTING ----------------- */

#ifdef HAIR_SHADER
	vec3 norm_view = cross(V, N);
	norm_view = normalize(cross(norm_view, N)); /* Normal facing view */
#endif

	vec3 spec = vec3(0.0);
	for (int i = 0; i < MAX_LIGHT && i < light_count; ++i) {
		LightData ld = lights_data[i];

		vec4 l_vector; /* Non-Normalized Light Vector with length in last component. */
		l_vector.xyz = ld.l_position - worldPosition;
		l_vector.w = length(l_vector.xyz);

		vec3 l_color_vis = ld.l_color * light_visibility(ld, worldPosition, viewPosition, viewNormal, l_vector);

#ifdef HAIR_SHADER
		vec3 norm_lamp, view_vec;
		float occlu_trans, occlu;
		light_hair_common(ld, N, V, l_vector, norm_view, occlu_trans, occlu, norm_lamp, view_vec);

		spec += l_color_vis * light_specular(ld, N, view_vec, l_vector, roughnessSquared, f0) * occlu;
#else
		spec += l_color_vis * light_specular(ld, N, V, l_vector, roughnessSquared, f0);
#endif
	}

	/* Accumulate outgoing radiance */
	vec3 out_light = spec * float(specToggle);

#ifdef HAIR_SHADER
	N = -norm_view;
#endif

	/* ---------------- SPECULAR ENVIRONMENT LIGHTING ----------------- */

	/* Accumulate light from all sources until accumulator is full. Then apply Occlusion and BRDF. */
	vec4 spec_accum = vec4(0.0);

	if (!(ssrToggle && ssr_id == outputSsrId)) {
		/* Planar Reflections */
		for (int i = 0; i < MAX_PLANAR && i < planar_count && spec_accum.a < 0.999; ++i) {
			PlanarData pd = planars_data[i];

			float fade = probe_attenuation_planar(pd, worldPosition, N, roughness);

			if (fade > 0.0) {
				vec3 spec = probe_evaluate_planar(float(i), pd, worldPosition, N, V, roughness, fade);
				accumulate_light(spec, fade, spec_accum);
			}
		}

		/* Specular probes */
		vec3 spec_dir = get_specular_reflection_dominant_dir(N, V, roughnessSquared);

		/* Starts at 1 because 0 is world probe */
		for (int i = 1; i < MAX_PROBE && i < probe_count && spec_accum.a < 0.999; ++i) {
			CubeData cd = probes_data[i];

			float fade = probe_attenuation_cube(cd, worldPosition);

			if (fade > 0.0) {
				vec3 spec = probe_evaluate_cube(float(i), cd, worldPosition, spec_dir, roughness);
				accumulate_light(spec, fade, spec_accum);
			}
		}

		/* World Specular */
		if (spec_accum.a < 0.999) {
			vec3 spec = probe_evaluate_world_spec(spec_dir, roughness);
			accumulate_light(spec, 1.0, spec_accum);
		}
	}

	vec4 rand = texture(utilTex, vec3(gl_FragCoord.xy / LUT_SIZE, 2.0));

	/* Get Brdf intensity */
	vec2 uv = lut_coords(dot(N, V), roughness);
	vec2 brdf_lut = texture(utilTex, vec3(uv, 1.0)).rg;

	ssr_spec = F_ibl(f0, brdf_lut);
	if (!(ssrToggle && ssr_id == outputSsrId)) {
		/* Ambient Occlusion */
		vec3 bent_normal;
		float final_ao = occlusion_compute(N, viewPosition, ao, rand.rg, bent_normal);

		ssr_spec *= specular_occlusion(dot(N, V), final_ao, roughness);
	}
	out_light += spec_accum.rgb * ssr_spec * float(specToggle);

	return out_light;
}

/* ----------- Transmission -----------  */

vec3 eevee_surface_refraction(vec3 N, vec3 f0, float roughness, float ior)
{
	/* Zero length vectors cause issues, see: T51979. */
#if 0
	N = normalize(N);
#else
	{
		float len = length(N);
		if (isnan(len)) {
			return vec3(0.0);
		}
		N /= len;
	}
#endif
	vec3 V = cameraVec;
	ior = (gl_FrontFacing) ? ior : 1.0 / ior;

	roughness = clamp(roughness, 1e-8, 0.9999);
	float roughnessSquared = roughness * roughness;

	/* ---------------- SCENE LAMPS LIGHTING ----------------- */

	/* No support for now. Supporting LTCs mean having a 3D LUT.
	 * We could support point lights easily though. */

	/* ---------------- SPECULAR ENVIRONMENT LIGHTING ----------------- */

	/* Accumulate light from all sources until accumulator is full. Then apply Occlusion and BRDF. */
	vec4 trans_accum = vec4(0.0);

	/* Refract the view vector using the depth heuristic.
	 * Then later Refract a second time the already refracted
	 * ray using the inverse ior. */
	float final_ior = (refractionDepth > 0.0) ? 1.0 / ior : ior;
	vec3 refr_V = (refractionDepth > 0.0) ? -refract(-V, N, final_ior) : V;
	vec3 refr_pos = (refractionDepth > 0.0) ? line_plane_intersect(worldPosition, refr_V, worldPosition - N * refractionDepth, N) : worldPosition;

#ifdef USE_REFRACTION
	/* Screen Space Refraction */
	if (ssrToggle && roughness < maxRoughness + 0.2) {
		vec3 rand = texture(utilTex, vec3(gl_FragCoord.xy / LUT_SIZE, 2.0)).xzw;

		/* Find approximated position of the 2nd refraction event. */
		vec3 refr_vpos = (refractionDepth > 0.0) ? transform_point(ViewMatrix, refr_pos) : viewPosition;

		float ray_ofs = 1.0 / float(rayCount);
		vec4 spec = screen_space_refraction(refr_vpos, N, refr_V, final_ior, roughnessSquared, rand, 0.0);
		if (rayCount > 1) spec += screen_space_refraction(refr_vpos, N, refr_V, final_ior, roughnessSquared, rand.xyz * vec3(1.0, -1.0, -1.0), 1.0 * ray_ofs);
		if (rayCount > 2) spec += screen_space_refraction(refr_vpos, N, refr_V, final_ior, roughnessSquared, rand.xzy * vec3(1.0,  1.0, -1.0), 2.0 * ray_ofs);
		if (rayCount > 3) spec += screen_space_refraction(refr_vpos, N, refr_V, final_ior, roughnessSquared, rand.xzy * vec3(1.0, -1.0,  1.0), 3.0 * ray_ofs);
		spec /= float(rayCount);
		spec.a *= smoothstep(maxRoughness + 0.2, maxRoughness, roughness);
		accumulate_light(spec.rgb, spec.a, trans_accum);
	}
#endif

	/* Specular probes */
	/* NOTE: This bias the IOR */
	vec3 refr_dir = get_specular_refraction_dominant_dir(N, refr_V, roughness, final_ior);

	/* Starts at 1 because 0 is world probe */
	for (int i = 1; i < MAX_PROBE && i < probe_count && trans_accum.a < 0.999; ++i) {
		CubeData cd = probes_data[i];

		float fade = probe_attenuation_cube(cd, worldPosition);

		if (fade > 0.0) {
			vec3 spec = probe_evaluate_cube(float(i), cd, refr_pos, refr_dir, roughnessSquared);
			accumulate_light(spec, fade, trans_accum);
		}
	}

	/* World Specular */
	if (trans_accum.a < 0.999) {
		vec3 spec = probe_evaluate_world_spec(refr_dir, roughnessSquared);
		accumulate_light(spec, 1.0, trans_accum);
	}

	float btdf = get_btdf_lut(utilTex, dot(N, V), roughness, ior);

	return trans_accum.rgb * btdf;
}

vec3 eevee_surface_glass(vec3 N, vec3 transmission_col, float roughness, float ior, int ssr_id, out vec3 ssr_spec)
{
	/* Zero length vectors cause issues, see: T51979. */
#if 0
	N = normalize(N);
#else
	{
		float len = length(N);
		if (isnan(len)) {
			return vec3(0.0);
		}
		N /= len;
	}
#endif
	vec3 V = cameraVec;
	ior = (gl_FrontFacing) ? ior : 1.0 / ior;

	if (!specToggle) return vec3(0.0);

	roughness = clamp(roughness, 1e-8, 0.9999);
	float roughnessSquared = roughness * roughness;

	/* ---------------- SCENE LAMPS LIGHTING ----------------- */

#ifdef HAIR_SHADER
	vec3 norm_view = cross(V, N);
	norm_view = normalize(cross(norm_view, N)); /* Normal facing view */
#endif

	vec3 spec = vec3(0.0);
	for (int i = 0; i < MAX_LIGHT && i < light_count; ++i) {
		LightData ld = lights_data[i];

		vec4 l_vector; /* Non-Normalized Light Vector with length in last component. */
		l_vector.xyz = ld.l_position - worldPosition;
		l_vector.w = length(l_vector.xyz);

		vec3 l_color_vis = ld.l_color * light_visibility(ld, worldPosition, viewPosition, viewNormal, l_vector);

#ifdef HAIR_SHADER
		vec3 norm_lamp, view_vec;
		float occlu_trans, occlu;
		light_hair_common(ld, N, V, l_vector, norm_view, occlu_trans, occlu, norm_lamp, view_vec);

		spec += l_color_vis * light_specular(ld, N, view_vec, l_vector, roughnessSquared, vec3(1.0)) * occlu;
#else
		spec += l_color_vis * light_specular(ld, N, V, l_vector, roughnessSquared, vec3(1.0));
#endif
	}

	/* Accumulate outgoing radiance */
	vec3 out_light = spec;

#ifdef HAIR_SHADER
	N = -norm_view;
#endif


	/* ---------------- SPECULAR ENVIRONMENT LIGHTING ----------------- */

	/* Accumulate light from all sources until accumulator is full. Then apply Occlusion and BRDF. */
	vec4 spec_accum = vec4(0.0);

	/* Planar Reflections */
	if (!(ssrToggle && ssr_id == outputSsrId)) {
		for (int i = 0; i < MAX_PLANAR && i < planar_count && spec_accum.a < 0.999 && roughness < 0.1; ++i) {
			PlanarData pd = planars_data[i];

			float fade = probe_attenuation_planar(pd, worldPosition, N, roughness);

			if (fade > 0.0) {
				vec3 spec = probe_evaluate_planar(float(i), pd, worldPosition, N, V, roughness, fade);
				accumulate_light(spec, fade, spec_accum);
			}
		}
	}

	/* Refract the view vector using the depth heuristic.
	 * Then later Refract a second time the already refracted
	 * ray using the inverse ior. */
	float final_ior = (refractionDepth > 0.0) ? 1.0 / ior : ior;
	vec3 refr_V = (refractionDepth > 0.0) ? -refract(-V, N, final_ior) : V;
	vec3 refr_pos = (refractionDepth > 0.0) ? line_plane_intersect(worldPosition, refr_V, worldPosition - N * refractionDepth, N) : worldPosition;

	vec4 trans_accum = vec4(0.0);

#ifdef USE_REFRACTION
	/* Screen Space Refraction */
	if (ssrToggle && roughness < maxRoughness + 0.2) {
		vec3 rand = texture(utilTex, vec3(gl_FragCoord.xy / LUT_SIZE, 2.0)).xzw;

		/* Find approximated position of the 2nd refraction event. */
		vec3 refr_vpos = (refractionDepth > 0.0) ? transform_point(ViewMatrix, refr_pos) : viewPosition;

		float ray_ofs = 1.0 / float(rayCount);
		vec4 spec = screen_space_refraction(refr_vpos, N, refr_V, final_ior, roughnessSquared, rand, 0.0);
		if (rayCount > 1) spec += screen_space_refraction(refr_vpos, N, refr_V, final_ior, roughnessSquared, rand.xyz * vec3(1.0, -1.0, -1.0), 1.0 * ray_ofs);
		if (rayCount > 2) spec += screen_space_refraction(refr_vpos, N, refr_V, final_ior, roughnessSquared, rand.xzy * vec3(1.0,  1.0, -1.0), 2.0 * ray_ofs);
		if (rayCount > 3) spec += screen_space_refraction(refr_vpos, N, refr_V, final_ior, roughnessSquared, rand.xzy * vec3(1.0, -1.0,  1.0), 3.0 * ray_ofs);
		spec /= float(rayCount);
		spec.a *= smoothstep(maxRoughness + 0.2, maxRoughness, roughness);
		accumulate_light(spec.rgb, spec.a, trans_accum);
	}
#endif

	/* Specular probes */
	vec3 refr_dir = get_specular_refraction_dominant_dir(N, refr_V, roughness, final_ior);
	vec3 spec_dir = get_specular_reflection_dominant_dir(N, V, roughnessSquared);

	/* Starts at 1 because 0 is world probe */
	for (int i = 1; i < MAX_PROBE && i < probe_count && (spec_accum.a < 0.999 || trans_accum.a < 0.999); ++i) {
		CubeData cd = probes_data[i];

		float fade = probe_attenuation_cube(cd, worldPosition);

		if (fade > 0.0) {
			if (!(ssrToggle && ssr_id == outputSsrId)) {
				vec3 spec = probe_evaluate_cube(float(i), cd, worldPosition, spec_dir, roughness);
				accumulate_light(spec, fade, spec_accum);
			}

			spec = probe_evaluate_cube(float(i), cd, refr_pos, refr_dir, roughnessSquared);
			accumulate_light(spec, fade, trans_accum);
		}
	}

	/* World Specular */
	if (spec_accum.a < 0.999) {
		if (!(ssrToggle && ssr_id == outputSsrId)) {
			vec3 spec = probe_evaluate_world_spec(spec_dir, roughness);
			accumulate_light(spec, 1.0, spec_accum);
		}
	}

	if (trans_accum.a < 0.999) {
		spec = probe_evaluate_world_spec(refr_dir, roughnessSquared);
		accumulate_light(spec, 1.0, trans_accum);
	}

	/* Ambient Occlusion */
	/* TODO : when AO will be cheaper */
	float final_ao = 1.0;

	float NV = dot(N, V);
	/* Get Brdf intensity */
	vec2 uv = lut_coords(NV, roughness);
	vec2 brdf_lut = texture(utilTex, vec3(uv, 1.0)).rg;

	float fresnel = F_eta(ior, NV);

	/* Apply fresnel on lamps. */
	out_light *= vec3(fresnel);

	ssr_spec = vec3(fresnel) * F_ibl(vec3(1.0), brdf_lut);
	if (!(ssrToggle && ssr_id == outputSsrId)) {
		ssr_spec *= specular_occlusion(dot(N, V), final_ao, roughness);
	}
	out_light += spec_accum.rgb * ssr_spec;


	float btdf = get_btdf_lut(utilTex, NV, roughness, ior);

	out_light += vec3(1.0 - fresnel) * transmission_col * trans_accum.rgb * btdf;

	return out_light;
}