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

gpu_shader_material_bsdf_glossy.glsl « shaders « gpu « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 33c683268fbc9099f41bab4ebbe88348753e4bf5 (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
/* -------- Utils Functions --------- */

vec3 sample_ggx(float nsample, float a2, vec3 N, vec3 T, vec3 B)
{
	vec3 Xi = hammersley_3d(nsample);

	float z = sqrt( (1.0 - Xi.x) / ( 1.0 + a2 * Xi.x - Xi.x ) ); /* cos theta */
	float r = sqrt( 1.0 - z * z ); /* sin theta */
	float x = r * Xi.y;
	float y = r * Xi.z;

	/* Global variable */
	Ht = vec3(x, y, z);

	return from_tangent_to_world(Ht, N, T, B);
}

vec3 sample_beckmann(float nsample, float a2, vec3 N, vec3 T, vec3 B)
{
	vec3 Xi = hammersley_3d(nsample);

	float z = sqrt( 1.0 / ( 1.0 - a2 * log(1.0 - Xi.x) ) ); /* cos theta */
	float r = sqrt( 1.0 - z * z ); /* sin theta */
	float x = r * Xi.y;
	float y = r * Xi.z;

	/* Global variable */
	Ht = vec3(x, y, z);

	return from_tangent_to_world(Ht, N, T, B);
}

vec3 sample_ashikhmin_shirley(float nsample, float n_x, vec3 N, vec3 T, vec3 B)
{
	vec3 Xi = hammersley_3d(nsample);

	float z = pow( Xi.x, 1.0 / (n_x + 1.0) ); /* cos theta */
	float r = sqrt( 1.0 - z * z ); /* sin theta */
	float x = r * Xi.y;
	float y = r * Xi.z;

	/* Global variable */
	Ht = vec3(x, y, z);

	return from_tangent_to_world(Ht, N, T, B);
}

float D_ggx_opti(float NH, float a2)
{
	float tmp = (NH * a2 - NH) * NH + 1.0;
	return M_PI * tmp*tmp; /* Doing RCP and mul a2 at the end */
}

float D_beckman(float NH, float a2)
{
	float NH2 = NH * NH;
	return exp((NH2 - 1) / (NH2 * a2)) / (M_PI * a2 * NH2 * NH2);
}

float pdf_ggx_reflect(float NH, float a2)
{
	return NH * a2 / D_ggx_opti(NH, a2);
}

float pdf_beckmann_reflect(float NH, float a2)
{
	return NH * D_beckman(NH, a2);
}

float pdf_ashikhmin_shirley_reflect(float NH, float VH, float n_x)
{
	float lobe = pow(NH, n_x);
	float norm = (n_x + 1.0) * 0.125 * M_1_PI;

	return norm * lobe / VH;
}

void prepare_glossy(float roughness, out float a, out float a2)
{
	/* Artifacts appear with roughness below this threshold */
	/* XXX TODO : find why flooring is necessary */
	a  = clamp(roughness, 2e-4, 0.9999999);
	a2 = max(1e-8, a*a);
}

float G1_Smith_GGX(float NX, float a2)
{
	/* Using Brian Karis approach and refactoring by NX/NX
	 * this way the (2*NL)*(2*NV) in G = G1(V) * G1(L) gets canceled by the brdf denominator 4*NL*NV
	 * Rcp is done on the whole G later
	 * Note that this is not convenient for the transmition formula */
	return NX + sqrt( NX * (NX - NX * a2) + a2 );
	/* return 2 / (1 + sqrt(1 + a2 * (1 - NX*NX) / (NX*NX) ) ); /* Reference function */
}

float G1_Smith_beckmann(float NX, float a2)
{
	float tmp = 1 / (sqrt(a2 * (1 - NX * NX) / (NX * NX)));
	return (tmp < 1.6) ? (3.535 * tmp + 2.181 * tmp * tmp) / (1 + 2.276 * tmp + 2.577 * tmp * tmp) : 1.0;
}

/* -------- BSDF --------- */

float bsdf_ggx(vec3 N, vec3 L, vec3 V, float roughness)
{
	float a, a2; prepare_glossy(roughness, a, a2);

	vec3 H = normalize(L + V);
	float NH = max(dot(N, H), 1e-8);
	float NL = max(dot(N, L), 1e-8);
	float NV = max(dot(N, V), 1e-8);

	float G = G1_Smith_GGX(NV, a2) * G1_Smith_GGX(NL, a2); /* Doing RCP at the end */
	float D = D_ggx_opti(NH, a2);

	/* Denominator is canceled by G1_Smith */
	/* bsdf = D * G / (4.0 * NL * NV); /* Reference function */
	return NL * a2 / (D * G); /* NL to Fit cycles Equation : line. 345 in bsdf_microfacet.h */
}

/* This one returns the brdf already divided by the pdf */
float bsdf_ggx_pdf(float a2, float NH, float NL, float VH, float G1_V)
{
	float G = G1_V * G1_Smith_GGX(NL, a2); /* Doing RCP at the end */

	/* Denominator is canceled by G1_Smith
	 * brdf = D * G / (4.0 * NL * NV) [denominator canceled by G]
	 * pdf = D * NH / (4 * VH) [D canceled later by D in brdf] */
	return 4.0 * VH / (NH * G); /* brdf / pdf */
}

float bsdf_beckmann(vec3 N, vec3 L, vec3 V, float roughness)
{
	float a, a2; prepare_glossy(roughness, a, a2);

	vec3 H = normalize(L + V);
	float NH = max(dot(N, H), 1e-8);
	float NL = max(dot(N, L), 1e-8);
	float NV = max(dot(N, V), 1e-8);

	float G = G1_Smith_beckmann(NV, a2) * G1_Smith_beckmann(NL, a2);
	float D = D_beckman(NH, a2);

	return NL * D * G * 0.25 / (NL * NV);
}

/* This one returns the brdf already divided by the pdf */
float bsdf_beckmann_pdf(float a2, float NH, float NV, float NL, float VH, float G1_V)
{
	float G = G1_V * G1_Smith_beckmann(NL, a2);

	/* brdf = D * G / (4.0 * NL * NV)
	 * pdf = D * NH / (4 * VH) [D canceled later by D in brdf] */
	return G * VH / (NH * NV * NL); /* brdf / pdf */
}

float bsdf_ashikhmin_shirley(vec3 N, vec3 L, vec3 V, float roughness)
{
	float a, a2; prepare_glossy(roughness, a, a2);

	vec3 H = normalize(L + V);
	float NL = max(dot(N, L), 1e-6);
	float NV = max(dot(N, V), 1e-6);
	float NH = max(dot(N, H), 1e-6);
	float VH = max(abs(dot(V, H)), 1e-6);

	float pump = 1.0 / max(1e-6, VH * max(NL, NV));
	float n_x = 2.0 / a2 - 2.0;
	float lobe = pow(NH, n_x);
	float norm = (n_x + 1.0f) * 0.125 * M_1_PI;

	return NL * norm * lobe * pump;
}

/* This one returns the brdf already divided by the pdf */
float bsdf_ashikhmin_shirley_pdf(float NV, float NL, float VH)
{
	float pump = 1.0 / max(1e-6, VH * max(NL, NV));

	return VH * pump;
}

/* -------- Preview Lights --------- */

void node_bsdf_glossy_lights(vec4 color, float roughness, vec3 N, vec3 V, vec4 ambient_light, out vec4 result)
{
	vec3 accumulator = ambient_light.rgb;

	if (roughness <= 1e-4) {
		result = vec4(accumulator * color.rgb, 1.0);
		return;
	}

	shade_view(V, V); V = -V;
	N = normalize(N);

	/* directional lights */
	for(int i = 0; i < NUM_LIGHTS; i++) {
		vec3 L = gl_LightSource[i].position.xyz;
		vec3 light_color = gl_LightSource[i].specular.rgb;

		accumulator += light_color * bsdf_ggx(N, L, V, roughness);
	}

	result = vec4(accumulator * color.rgb, 1.0);
}

/* -------- Physical Lights --------- */

/* GLOSSY SHARP */

void bsdf_glossy_sharp_sphere_light(
	vec3 N, vec3 T, vec3 L, vec3 V,
	vec3 l_coords, float l_distance, float l_areasizex, float l_areasizey, vec2 l_areascale, mat4 l_mat,
	float roughness, float ior, float sigma, float toon_size, float toon_smooth, float anisotropy, float aniso_rotation,
	out float bsdf)
{
	float l_radius = l_areasizex;
	L = l_distance * L;
	vec3 R = -reflect(V, N);

	vec3 P = line_aligned_plane_intersect(vec3(0.0), R, L);
	bsdf = (distance_squared(P, L) < l_radius * l_radius) ? 1.0 : 0.0;

	/* Energy conservation + cycle matching */
	bsdf *= sphere_energy(l_radius);
}

void bsdf_glossy_sharp_area_light(
	vec3 N, vec3 T, vec3 L, vec3 V,
	vec3 l_coords, float l_distance, float l_areasizex, float l_areasizey, vec2 l_areascale, mat4 l_mat,
	float roughness, float ior, float sigma, float toon_size, float toon_smooth, float anisotropy, float aniso_rotation,
	out float bsdf)
{
	if (max(l_areasizex, l_areasizey) < 1e-6) {
		bsdf = 0.0;
		return;
	}

	L = l_distance * L;

	vec3 lampx, lampy, lampz;
	vec2 halfsize = area_light_prepass(l_mat, l_areasizex, l_areasizey, l_areascale, lampx, lampy, lampz);

	/* Find the intersection point E between the reflection vector and the light plane */
	vec3 R = reflect(V, N);
	vec3 E = line_plane_intersect(vec3(0.0), R, L, lampz);

	/* Project it onto the light plane */
	vec3 projection = E - L;
	float A = dot(lampx, projection);
	float B = dot(lampy, projection);

	bsdf = (abs(A) < halfsize.x && abs(B) < halfsize.y) ? 1.0 : 0.0;

	/* Masking */
	bsdf *= (dot(-L, lampz) > 0.0) ? 1.0 : 0.0;
	bsdf *= (dot(R, lampz) > 0.0) ? 1.0 : 0.0;

	/* Energy conservation + cycle matching */
	bsdf *= rectangle_energy(l_areasizex, l_areasizey);
}

void bsdf_glossy_sharp_sun_light(
	vec3 N, vec3 T, vec3 L, vec3 V,
	vec3 l_coords, float l_distance, float l_areasizex, float l_areasizey, vec2 l_areascale, mat4 l_mat,
	float roughness, float ior, float sigma, float toon_size, float toon_smooth, float anisotropy, float aniso_rotation,
	out float bsdf)
{
	vec3 R = reflect(V, N);

	float l_radius = l_areasizex;
	float angle = atan(l_radius);

	float costheta = dot(-L, R);
	float cosangle = cos(angle);

	bsdf = (costheta > cosangle) ? 1.0 : 0.0;

	/* Energy conservation + cycle matching */
	bsdf *= disk_energy(l_radius);
	bsdf /= costheta * costheta * costheta;
	bsdf *= M_PI;
}

/* GLOSSY GGX */

void bsdf_glossy_ggx_sphere_light(
	vec3 N, vec3 T, vec3 L, vec3 V,
	vec3 l_coords, float l_distance, float l_areasizex, float l_areasizey, vec2 l_areascale, mat4 l_mat,
	float roughness, float ior, float sigma, float toon_size, float toon_smooth, float anisotropy, float aniso_rotation,
	out float bsdf)
{
	if (roughness < 1e-4 && l_areasizex == 0) {
		bsdf = 0.0;
		return;
	}

#if 1
	/* MRP is twice as fast as LTC, does not exhibit crucial artifacts and is better looking */
	float l_radius = l_areasizex;

	shade_view(V, V);
	vec3 R = reflect(V, N);

	float energy_conservation = 1.0;
	most_representative_point(l_radius, 0.0, vec3(0.0), l_distance, R, L, roughness, energy_conservation);
	bsdf = bsdf_ggx(N, L, V, roughness);

	bsdf *= energy_conservation / (l_distance * l_distance);
	bsdf *= sphere_energy(l_radius) * max(l_radius * l_radius, 1e-16); /* l_radius is already inside energy_conservation */
	bsdf *= M_PI;
#else
	/* LTC */
	float l_radius = max(0.007, l_areasizex);

	vec3 pos = V;
	shade_view(V, V);
	vec3 R = -reflect(V, N);
	L = l_distance * L;
	V = -V;
	N = -N;

	vec3 P = line_aligned_plane_intersect(vec3(0.0), R, L);
	vec3 Px = normalize(P - L) * l_radius;
	vec3 Py = axis_angle_rotation(Px, R, M_PI_2);

	vec3 points[4];
	points[0] = l_coords + Px;
	points[1] = l_coords - Py;
	points[2] = l_coords - Px;
	points[3] = l_coords + Py;

	float NV = max(dot(N, V), 1e-8);
	vec2 uv = ltc_coords(NV, sqrt(roughness));
	mat3 ltcmat = ltc_matrix(uv);

	bsdf = ltc_evaluate(N, V, pos, ltcmat, points);
	bsdf *= texture2D(unfltcmag, uv).r; /* Bsdf matching */

	bsdf *= M_1_2PI;
	bsdf *= sphere_energy(l_radius);
#endif
}

void bsdf_glossy_ggx_area_light(
	vec3 N, vec3 T, vec3 L, vec3 V,
	vec3 l_coords, float l_distance, float l_areasizex, float l_areasizey, vec2 l_areascale, mat4 l_mat,
	float roughness, float ior, float sigma, float toon_size, float toon_smooth, float anisotropy, float aniso_rotation,
	out float bsdf)
{
	if (min(l_areasizex, l_areasizey) < 1e-6) {
		bsdf = 0.0;
		return;
	}

	vec3 pos = V;
	shade_view(V, V);
	V = -V;
	N = -N;

	vec3 lampx, lampy, lampz;
	vec2 halfsize = area_light_prepass(l_mat, l_areasizex, l_areasizey, l_areascale, lampx, lampy, lampz);

	vec3 points[4];
	area_light_points(l_coords, halfsize, lampx, lampy, points);

	float NV = max(dot(N, V), 1e-8);
	vec2 uv = ltc_coords(NV, sqrt(roughness));
	mat3 ltcmat = ltc_matrix(uv);

	bsdf = ltc_evaluate(N, V, pos, ltcmat, points);
	bsdf *= texture2D(unfltcmag, uv).r; /* Bsdf matching */

	bsdf *= step(0.0, -dot(L, lampz));
	bsdf *= M_1_2PI;
	bsdf *= rectangle_energy(l_areasizex, l_areasizey);
}

void bsdf_glossy_ggx_sun_light(
	vec3 N, vec3 T, vec3 L, vec3 V,
	vec3 l_coords, float l_distance, float l_areasizex, float l_areasizey, vec2 l_areascale, mat4 l_mat,
	float roughness, float ior, float sigma, float toon_size, float toon_smooth, float anisotropy, float aniso_rotation,
	out float bsdf)
{
	/* Correct ligth shape but uniform intensity
	 * Does not take into account the division by costheta^3 */
	if (roughness < 1e-4 && l_areasizex == 0) {
		bsdf = 0.0;
		return;
	}

	vec3 R = reflect(V, N);

	float l_radius = l_areasizex;
	float angle = atan(l_radius);

	float costheta = dot(-L, R);
	float cosangle = cos(angle);

	float energy_conservation = 1.0;
	most_representative_point_disk(l_radius, 1.0, R, L, roughness, energy_conservation);

	bsdf = bsdf_ggx(N, L, V, roughness);
	bsdf *= energy_conservation;
}

/* GLOSSY BECKMANN */

void bsdf_glossy_beckmann_sphere_light(
	vec3 N, vec3 T, vec3 L, vec3 V,
	vec3 l_coords, float l_distance, float l_areasizex, float l_areasizey, vec2 l_areascale, mat4 l_mat,
	float roughness, float ior, float sigma, float toon_size, float toon_smooth, float anisotropy, float aniso_rotation,
	out float bsdf)
{
	if (roughness < 1e-4 && l_areasizex == 0) {
		bsdf = 0.0;
		return;
	}

	/* MRP is twice as fast as LTC, does not exhibit crucial artifacts and is better looking */
	float l_radius = l_areasizex;

	shade_view(V, V);
	vec3 R = reflect(V, N);

	float energy_conservation = 1.0; /* XXX TODO : Energy conservation is done for GGX */
	most_representative_point(l_radius, 0.0, vec3(0.0), l_distance, R, L, roughness, energy_conservation);
	bsdf = bsdf_beckmann(N, L, V, roughness);

	bsdf *= energy_conservation / (l_distance * l_distance);
	bsdf *= sphere_energy(l_radius) * max(l_radius * l_radius, 1e-16); /* l_radius is already inside energy_conservation */
	bsdf *= M_PI;
}

void bsdf_glossy_beckmann_area_light(
	vec3 N, vec3 T, vec3 L, vec3 V,
	vec3 l_coords, float l_distance, float l_areasizex, float l_areasizey, vec2 l_areascale, mat4 l_mat,
	float roughness, float ior, float sigma, float toon_size, float toon_smooth, float anisotropy, float aniso_rotation,
	out float bsdf)
{
	/* TODO Make the other ltc luts */
	bsdf_glossy_ggx_area_light( N, T, L, V, l_coords, l_distance, l_areasizex, l_areasizey, l_areascale, 
		l_mat, roughness, ior, sigma, toon_size, toon_smooth, anisotropy, aniso_rotation, bsdf);
}

void bsdf_glossy_beckmann_sun_light(
	vec3 N, vec3 T, vec3 L, vec3 V,
	vec3 l_coords, float l_distance, float l_areasizex, float l_areasizey, vec2 l_areascale, mat4 l_mat,
	float roughness, float ior, float sigma, float toon_size, float toon_smooth, float anisotropy, float aniso_rotation,
	out float bsdf)
{
	/* Correct ligth shape but uniform intensity
	 * Does not take into account the division by costheta^3 */
	if (roughness < 1e-4 && l_areasizex == 0) {
		bsdf = 0.0;
		return;
	}

	vec3 R = reflect(V, N);

	float l_radius = l_areasizex;
	float angle = atan(l_radius);

	float costheta = dot(-L, R);
	float cosangle = cos(angle);

	float energy_conservation = 1.0;
	most_representative_point_disk(l_radius, 1.0, R, L, roughness, energy_conservation);

	bsdf = bsdf_beckmann(N, L, V, roughness);
	bsdf *= energy_conservation;
}

/* GLOSSY ASHIKhMIN SHIRLEY */

void bsdf_glossy_ashikhmin_shirley_sphere_light(
	vec3 N, vec3 T, vec3 L, vec3 V,
	vec3 l_coords, float l_distance, float l_areasizex, float l_areasizey, vec2 l_areascale, mat4 l_mat,
	float roughness, float ior, float sigma, float toon_size, float toon_smooth, float anisotropy, float aniso_rotation,
	out float bsdf)
{
	if (roughness < 1e-4 && l_areasizex == 0) {
		bsdf = 0.0;
		return;
	}

	/* MRP is twice as fast as LTC, does not exhibit crucial artifacts and is better looking */
	float l_radius = l_areasizex;

	shade_view(V, V);
	vec3 R = reflect(V, N);

	float energy_conservation = 1.0; /* XXX TODO : Energy conservation is done for GGX */
	most_representative_point(l_radius, 0.0, vec3(0.0), l_distance, R, L, roughness, energy_conservation);
	bsdf = bsdf_ashikhmin_shirley(N, L, V, roughness);

	bsdf *= energy_conservation / (l_distance * l_distance);
	bsdf *= sphere_energy(l_radius) * max(l_radius * l_radius, 1e-16); /* l_radius is already inside energy_conservation */
	bsdf *= M_PI;
}

void bsdf_glossy_ashikhmin_shirley_area_light(
	vec3 N, vec3 T, vec3 L, vec3 V,
	vec3 l_coords, float l_distance, float l_areasizex, float l_areasizey, vec2 l_areascale, mat4 l_mat,
	float roughness, float ior, float sigma, float toon_size, float toon_smooth, float anisotropy, float aniso_rotation,
	out float bsdf)
{
	/* TODO Make the other ltc luts */
	bsdf_glossy_ggx_area_light( N, T, L, V, l_coords, l_distance, l_areasizex, l_areasizey, l_areascale, 
		l_mat, roughness, ior, sigma, toon_size, toon_smooth, anisotropy, aniso_rotation, bsdf);
}

void bsdf_glossy_ashikhmin_shirley_sun_light(
	vec3 N, vec3 T, vec3 L, vec3 V,
	vec3 l_coords, float l_distance, float l_areasizex, float l_areasizey, vec2 l_areascale, mat4 l_mat,
	float roughness, float ior, float sigma, float toon_size, float toon_smooth, float anisotropy, float aniso_rotation,
	out float bsdf)
{
	/* Correct ligth shape but uniform intensity
	 * Does not take into account the division by costheta^3 */
	if (roughness < 1e-4 && l_areasizex == 0) {
		bsdf = 0.0;
		return;
	}

	vec3 R = reflect(V, N);

	float l_radius = l_areasizex;
	float angle = atan(l_radius);

	float costheta = dot(-L, R);
	float cosangle = cos(angle);

	float energy_conservation = 1.0;
	most_representative_point_disk(l_radius, 1.0, R, L, roughness, energy_conservation);

	bsdf = bsdf_ashikhmin_shirley(N, L, V, roughness);
	bsdf *= energy_conservation;
}

/* -------- Image Based Lighting --------- */

void env_sampling_glossy_sharp(
	float pbr, vec3 viewpos, mat4 invviewmat, mat4 viewmat,
	vec3 N, vec3 T, float roughness, float ior, float sigma,
	float toon_size, float toon_smooth, float anisotropy, float aniso_rotation,
	float ao_factor, out vec3 result)
{
	/* Setup */
	vector_prepass(viewpos, N, invviewmat, viewmat);

	/* Precomputation */
	vec3 L = reflect(I, N);
	vec3 vL = (viewmat * vec4(L, 0.0)).xyz;

	/* Probe */
	vec4 sample_probe = sample_reflect(L) * specular_occlusion(dot(N, -I), ao_factor, 0.0);

#ifdef USE_SSR
	/* SSR */
	vec2 hitpixel; vec3 hitco; float hitstep;

	bool hit = raycast(viewpos, vL, hitstep, hitpixel, hitco);
	float contrib = ssr_contribution(viewpos, hitstep, hit, hitco);

	vec4 sample_ssr = bufferFetch(unfscenebuf, ivec2(hitpixel.xy), 0);
	srgb_to_linearrgb(sample_ssr, sample_ssr);

	result = mix(sample_probe.rgb, sample_ssr.rgb, contrib);
#else
	result = sample_probe.rgb;
#endif
}

void env_sampling_glossy_ggx(
	float pbr, vec3 viewpos, mat4 invviewmat, mat4 viewmat,
	vec3 N, vec3 T, float roughness, float ior, float sigma,
	float toon_size, float toon_smooth, float anisotropy, float aniso_rotation,
	float ao_factor, out vec3 result)
{
	/* Setup */
	vector_prepass(viewpos, N, invviewmat, viewmat);
	make_orthonormals(N, T, B); /* Generate tangent space */
	setup_noise(gl_FragCoord.xy); /* Noise to dither the samples */
	float a, a2; prepare_glossy(roughness, a, a2);

	/* Precomputation */
	float NV = max(1e-8, abs(dot(I, N)));
	float G1_V = G1_Smith_GGX(NV, a2);

	/* Integrating Envmap */
	vec4 out_radiance = vec4(0.0);
	for (float i = 0; i < unfbsdfsamples.x; i++) {
		vec3 H = sample_ggx(i, a2, N, T, B); /* Microfacet normal */
		vec3 L = reflect(I, H);
		float NL = dot(N, L);

		if (NL > 0.0) {
			/* Step 1 : Sampling Environment */
			float NH = max(1e-8, dot(N, H)); /* cosTheta */
			float VH = max(1e-8, -dot(I, H));

			float pdf = pdf_ggx_reflect(NH, a2);

			vec4 sample = sample_reflect_pdf(L, roughness, pdf);

			/* Step 2 : Integrating BRDF */
			float brdf_pdf = bsdf_ggx_pdf(a2, NH, NL, VH, G1_V);

			out_radiance += NL * sample * brdf_pdf;
		}
	}

	result = out_radiance.rgb * unfbsdfsamples.y * specular_occlusion(NV, ao_factor, a2);
}

void env_sampling_glossy_beckmann(
	float pbr, vec3 viewpos, mat4 invviewmat, mat4 viewmat,
	vec3 N, vec3 T, float roughness, float ior, float sigma,
	float toon_size, float toon_smooth, float anisotropy, float aniso_rotation,
	float ao_factor, out vec3 result)
{
	/* Setup */
	vector_prepass(viewpos, N, invviewmat, viewmat);
	make_orthonormals(N, T, B); /* Generate tangent space */
	setup_noise(gl_FragCoord.xy); /* Noise to dither the samples */
	float a, a2; prepare_glossy(roughness, a, a2);

	/* Precomputation */
	float NV = max(1e-8, abs(dot(I, N)));
	float G1_V = G1_Smith_beckmann(NV, a2);

	/* Integrating Envmap */
	vec4 out_radiance = vec4(0.0);
	for (float i = 0; i < unfbsdfsamples.x; i++) {
		vec3 H = sample_beckmann(i, a2, N, T, B); /* Microfacet normal */
		vec3 L = reflect(I, H);
		float NL = dot(N, L);

		if (NL > 0.0) {
			/* Step 1 : Sampling Environment */
			float NH = max(1e-8, dot(N, H)); /* cosTheta */
			float VH = max(1e-8, -dot(I, H));

			float pdf = pdf_beckmann_reflect(NH, a2);

			vec4 sample = sample_reflect_pdf(L, roughness, pdf);

			/* Step 2 : Integrating BRDF */
			float brdf_pdf = bsdf_beckmann_pdf(a2, NH, NV, NL, VH, G1_V);

			out_radiance += NL * sample * brdf_pdf;
		}
	}

	result = out_radiance.rgb * unfbsdfsamples.y * specular_occlusion(NV, ao_factor, a2);
}

void env_sampling_glossy_ashikhmin_shirley(
	float pbr, vec3 viewpos, mat4 invviewmat, mat4 viewmat,
	vec3 N, vec3 T, float roughness, float ior, float sigma,
	float toon_size, float toon_smooth, float anisotropy, float aniso_rotation,
	float ao_factor, out vec3 result)
{
	/* Setup */
	vector_prepass(viewpos, N, invviewmat, viewmat);
	make_orthonormals(N, T, B); /* Generate tangent space */
	setup_noise(gl_FragCoord.xy); /* Noise to dither the samples */
	float a, a2; prepare_glossy(roughness, a, a2);

	/* Precomputation */
	float NV = max(1e-8, abs(dot(I, N)));
	float n_x = 2.0 / a2 - 2.0;

	/* Integrating Envmap */
	vec4 out_radiance = vec4(0.0);
	for (float i = 0; i < unfbsdfsamples.x; i++) {
		vec3 H = sample_ashikhmin_shirley(i, n_x, N, T, B); /* Microfacet normal */
		float VH = dot(H, -I);
		if (VH < 0.0) H = -H;
		/* reflect I on H to get omega_in */
		vec3 L = I + (2.0 * VH) * H;
		float NL = dot(N, L);

		if (NL > 0.0) {
			/* Step 1 : Sampling Environment */
			float NH = max(1e-8, dot(N, H)); /* cosTheta */
			VH = max(1e-8, abs(VH));
			NL = max(1e-8, NL);

			float pdf = pdf_ashikhmin_shirley_reflect(NH, VH, n_x);

			vec4 sample = sample_reflect_pdf(L, roughness, pdf);

			/* Step 2 : Integrating BRDF */
			float brdf_pdf = bsdf_ashikhmin_shirley_pdf(NV, NL, VH);

			out_radiance += NL * sample * brdf_pdf;
		}
	}

	result = out_radiance.rgb * unfbsdfsamples.y * specular_occlusion(NV, ao_factor, a2);
}