Debian Codegraph @

servers/rendering/renderer_rd/shaders/forward_clustered/integrate_dfg.glsl

raw
1 #[compute]
2 #version 450
3
4 // References:
5 // https://www.gamedevs.org/uploads/real-shading-in-unreal-engine-4.pdf
6 // https://google.github.io/filament/Filament.html
7 // https://learnopengl.com/PBR/IBL/Specular-IBL
8
9 layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
10
11 layout(rgba16f, set = 0, binding = 0) uniform restrict writeonly image2D current_image;
12
13 #define M_PI 3.14159265359
14 #define SAMPLE_COUNT 1024
15 #define SIZE 128
16
17 #define saturate(x) clamp(x, 0, 1)
18
19 // http://holger.dammertz.org/stuff/notes_HammersleyOnHemisphere.html
20 // efficient VanDerCorpus calculation
21 float radical_inverse_vdc(uint bits) {
22 bits = (bits << 16u) | (bits >> 16u);
23 bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u);
24 bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u);
25 bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u);
26 bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u);
27 return float(bits) * 2.3283064365386963e-10; // / 0x100000000
28 }
29
30 vec2 hammersley(uint i, float n) {
31 return vec2(float(i) / n, radical_inverse_vdc(i));
32 }
33
34 vec3 importance_sample_ggx(vec2 Xi, vec3 N, float roughness) {
35 float a = roughness * roughness;
36
37 float phi = 2.0 * M_PI * Xi.x;
38 float cosTheta = sqrt((1.0 - Xi.y) / (1.0 + (a * a - 1.0) * Xi.y));
39 float sinTheta = sqrt(1.0 - cosTheta * cosTheta);
40
41 // from spherical coordinates to cartesian coordinates - halfway vector
42 vec3 H;
43 H.x = cos(phi) * sinTheta;
44 H.y = sin(phi) * sinTheta;
45 H.z = cosTheta;
46
47 // from tangent-space H vector to world-space sample vector
48 vec3 up = abs(N.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(1.0, 0.0, 0.0);
49 vec3 tangent = normalize(cross(up, N));
50 vec3 bitangent = cross(N, tangent);
51
52 vec3 sampleVec = tangent * H.x + bitangent * H.y + N * H.z;
53 return normalize(sampleVec);
54 }
55
56 float geometry_schlick_ggx(float NdotV, float roughness) {
57 // note that we use a different k for IBL
58 float a = roughness;
59 float k = (a * a) / 2.0;
60
61 float nom = NdotV;
62 float denom = NdotV * (1.0 - k) + k;
63
64 return nom / denom;
65 }
66
67 float geometry_smith(vec3 N, vec3 V, vec3 L, float roughness) {
68 float NdotV = saturate(dot(N, V));
69 float NdotL = saturate(dot(N, L));
70 float ggx2 = geometry_schlick_ggx(NdotV, roughness);
71 float ggx1 = geometry_schlick_ggx(NdotL, roughness);
72
73 return ggx1 * ggx2;
74 }
75
76 vec3 importance_uniform_sample(vec2 u) {
77 float phi = 2.0f * M_PI * u.x;
78 float cosTheta = 1 - u.y;
79 float sinTheta = sqrt(1 - cosTheta * cosTheta);
80 return vec3(sinTheta * cos(phi), sinTheta * sin(phi), cosTheta);
81 }
82
83 float distribution_charlie(float NoH, float roughness) {
84 // Estevez and Kulla 2017, "Production Friendly Microfacet Sheen BRDF"
85 float a = roughness * roughness;
86 float invAlpha = 1 / a;
87 float cos2h = NoH * NoH;
88 float sin2h = 1 - cos2h;
89 return (2.0f + invAlpha) * pow(sin2h, invAlpha * 0.5f) / (2.0f * M_PI);
90 }
91
92 float visibility_ashikhmin(float NoV, float NoL) {
93 // Neubelt and Pettineo 2013, "Crafting a Next-gen Material Pipeline for The Order: 1886"
94 return 1 / (4 * (NoL + NoV - NoL * NoV));
95 }
96
97 void integrate_brdfs(float n_dot_v, float roughness, out vec2 brdf, out float cloth_brdf) {
98 vec3 v = vec3(sqrt(1.0 - n_dot_v * n_dot_v), 0, n_dot_v);
99 vec3 n = vec3(0.0f, 0.0f, 1.0f);
100 float A = 0.0f;
101 float B = 0.0f;
102 float C = 0.0f;
103
104 for (uint i = 0; i < SAMPLE_COUNT; ++i) {
105 vec2 Xi = hammersley(i, SAMPLE_COUNT);
106 vec3 h = importance_sample_ggx(Xi, n, roughness);
107 vec3 l = normalize(2.0 * dot(v, h) * h - v);
108
109 float n_dot_l = saturate(l.z);
110 float n_dot_h = saturate(h.z);
111 float v_dot_h = saturate(dot(v, h));
112
113 if (n_dot_l > 0.0) {
114 float G = geometry_smith(n, v, l, roughness);
115 float G_Vis = (G * v_dot_h) / (n_dot_h * n_dot_v);
116 float Fc = pow(1.0 - v_dot_h, 5.0);
117
118 // LDFG term for multiscattering
119 // https://google.github.io/filament/Filament.html#toc5.3.4.7
120 A += Fc * G_Vis;
121 B += G_Vis;
122 }
123
124 // Cloth BRDF calculations
125 // https://github.com/google/filament/blob/main/libs/ibl/src/CubemapIBL.cpp#L856-L874
126 vec3 h_cloth = importance_uniform_sample(Xi);
127 vec3 l_cloth = normalize(2.0 * dot(v, h_cloth) * h_cloth - v);
128 float n_dot_l_cloth = saturate(l_cloth.z);
129 float n_dot_h_cloth = saturate(h_cloth.z);
130 float v_dot_h_cloth = saturate(dot(v, h_cloth));
131
132 if (n_dot_l_cloth > 0.0) {
133 float v_cloth = visibility_ashikhmin(n_dot_v, n_dot_l_cloth);
134 float d_cloth = distribution_charlie(n_dot_h_cloth, roughness);
135 C += v_cloth * d_cloth * n_dot_l_cloth * v_dot_h_cloth;
136 }
137 }
138
139 A /= float(SAMPLE_COUNT);
140 B /= float(SAMPLE_COUNT);
141 C *= (4.0 * 2.0 * M_PI / SAMPLE_COUNT);
142
143 brdf = vec2(A, B);
144 cloth_brdf = C;
145 }
146
147 void main() {
148 ivec2 pos = ivec2(gl_GlobalInvocationID.xy);
149 float roughness = float(pos.y + 0.5f) / SIZE;
150 float NdotV = float(pos.x + 0.5f) / SIZE;
151 vec2 brdf;
152 float cloth_brdf;
153 integrate_brdfs(NdotV, roughness, brdf, cloth_brdf);
154 ivec2 out_pos = ivec2(pos.x, (SIZE - 1) - pos.y);
155 imageStore(current_image, out_pos, vec4(brdf, cloth_brdf, 1.0));
156 }