Debian Codegraph @

servers/rendering/renderer_rd/shaders/scene_forward_gi_inc.glsl

raw
1 // Functions related to gi/sdfgi for our forward renderer
2
3 //standard voxel cone trace
4 vec4 voxel_cone_trace(texture3D probe, vec3 cell_size, vec3 pos, vec3 direction, float tan_half_angle, float max_distance, float p_bias) {
5 float dist = p_bias;
6 vec4 color = vec4(0.0);
7
8 while (dist < max_distance && color.a < 0.95) {
9 float diameter = max(1.0, 2.0 * tan_half_angle * dist);
10 vec3 uvw_pos = (pos + dist * direction) * cell_size;
11 float half_diameter = diameter * 0.5;
12 //check if outside, then break
13 if (any(greaterThan(abs(uvw_pos - 0.5), vec3(0.5f + half_diameter * cell_size)))) {
14 break;
15 }
16 vec4 scolor = textureLod(sampler3D(probe, DEFAULT_SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP), uvw_pos, log2(diameter));
17 float a = (1.0 - color.a);
18 color += a * scolor;
19 dist += half_diameter;
20 }
21
22 return color;
23 }
24
25 vec4 voxel_cone_trace_45_degrees(texture3D probe, vec3 cell_size, vec3 pos, vec3 direction, float tan_half_angle, float max_distance, float p_bias) {
26 float dist = p_bias;
27 vec4 color = vec4(0.0);
28 float radius = max(0.5, tan_half_angle * dist);
29 float lod_level = log2(radius * 2.0);
30
31 while (dist < max_distance && color.a < 0.95) {
32 vec3 uvw_pos = (pos + dist * direction) * cell_size;
33
34 //check if outside, then break
35 if (any(greaterThan(abs(uvw_pos - 0.5), vec3(0.5f + radius * cell_size)))) {
36 break;
37 }
38 vec4 scolor = textureLod(sampler3D(probe, DEFAULT_SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP), uvw_pos, lod_level);
39 lod_level += 1.0;
40
41 float a = (1.0 - color.a);
42 scolor *= a;
43 color += scolor;
44 dist += radius;
45 radius = max(0.5, tan_half_angle * dist);
46 }
47
48 return color;
49 }
50
51 void voxel_gi_compute(uint index, vec3 position, vec3 normal, vec3 ref_vec, mat3 normal_xform, float roughness, vec3 ambient, vec3 environment, inout vec4 out_spec, inout vec4 out_diff) {
52 position = (voxel_gi_instances.data[index].xform * vec4(position, 1.0)).xyz;
53 ref_vec = normalize((voxel_gi_instances.data[index].xform * vec4(ref_vec, 0.0)).xyz);
54 normal = normalize((voxel_gi_instances.data[index].xform * vec4(normal, 0.0)).xyz);
55
56 position += normal * voxel_gi_instances.data[index].normal_bias;
57
58 //this causes corrupted pixels, i have no idea why..
59 if (any(bvec2(any(lessThan(position, vec3(0.0))), any(greaterThan(position, voxel_gi_instances.data[index].bounds))))) {
60 return;
61 }
62
63 vec3 blendv = abs(position / voxel_gi_instances.data[index].bounds * 2.0 - 1.0);
64 float blend = clamp(1.0 - max(blendv.x, max(blendv.y, blendv.z)), 0.0, 1.0);
65 //float blend=1.0;
66
67 float max_distance = length(voxel_gi_instances.data[index].bounds);
68 vec3 cell_size = 1.0 / voxel_gi_instances.data[index].bounds;
69
70 //radiance
71
72 #define MAX_CONE_DIRS 4
73
74 vec3 cone_dirs[MAX_CONE_DIRS] = vec3[](
75 vec3(0.707107, 0.0, 0.707107),
76 vec3(0.0, 0.707107, 0.707107),
77 vec3(-0.707107, 0.0, 0.707107),
78 vec3(0.0, -0.707107, 0.707107));
79
80 float cone_weights[MAX_CONE_DIRS] = float[](0.25, 0.25, 0.25, 0.25);
81 float cone_angle_tan = 0.98269;
82
83 vec3 light = vec3(0.0);
84
85 for (int i = 0; i < MAX_CONE_DIRS; i++) {
86 vec3 dir = normalize((voxel_gi_instances.data[index].xform * vec4(normal_xform * cone_dirs[i], 0.0)).xyz);
87
88 vec4 cone_light = voxel_cone_trace_45_degrees(voxel_gi_textures[index], cell_size, position, dir, cone_angle_tan, max_distance, voxel_gi_instances.data[index].bias);
89
90 if (voxel_gi_instances.data[index].blend_ambient) {
91 cone_light.rgb = mix(ambient, cone_light.rgb, min(1.0, cone_light.a / 0.95));
92 }
93
94 light += cone_weights[i] * cone_light.rgb;
95 }
96
97 light *= voxel_gi_instances.data[index].dynamic_range * voxel_gi_instances.data[index].exposure_normalization;
98 out_diff += vec4(light * blend, blend);
99
100 //irradiance
101 vec4 irr_light = voxel_cone_trace(voxel_gi_textures[index], cell_size, position, ref_vec, tan(roughness * 0.5 * M_PI * 0.99), max_distance, voxel_gi_instances.data[index].bias);
102 if (voxel_gi_instances.data[index].blend_ambient) {
103 irr_light.rgb = mix(environment, irr_light.rgb, min(1.0, irr_light.a / 0.95));
104 }
105 irr_light.rgb *= voxel_gi_instances.data[index].dynamic_range * voxel_gi_instances.data[index].exposure_normalization;
106 //irr_light=vec3(0.0);
107
108 out_spec += vec4(irr_light.rgb * blend, blend);
109 }
110
111 vec2 octahedron_wrap(vec2 v) {
112 vec2 signVal;
113 signVal.x = v.x >= 0.0 ? 1.0 : -1.0;
114 signVal.y = v.y >= 0.0 ? 1.0 : -1.0;
115 return (1.0 - abs(v.yx)) * signVal;
116 }
117
118 vec2 octahedron_encode(vec3 n) {
119 // https://twitter.com/Stubbesaurus/status/937994790553227264
120 n /= (abs(n.x) + abs(n.y) + abs(n.z));
121 n.xy = n.z >= 0.0 ? n.xy : octahedron_wrap(n.xy);
122 n.xy = n.xy * 0.5 + 0.5;
123 return n.xy;
124 }
125
126 void sdfgi_process(uint cascade, vec3 cascade_pos, vec3 cam_pos, vec3 cam_normal, vec3 cam_specular_normal, bool use_specular, float roughness, out vec3 diffuse_light, out vec3 specular_light, out float blend) {
127 cascade_pos += cam_normal * sdfgi.normal_bias;
128
129 vec3 base_pos = floor(cascade_pos);
130 //cascade_pos += mix(vec3(0.0),vec3(0.01),lessThan(abs(cascade_pos-base_pos),vec3(0.01))) * cam_normal;
131 ivec3 probe_base_pos = ivec3(base_pos);
132
133 vec4 diffuse_accum = vec4(0.0);
134 vec3 specular_accum;
135
136 ivec3 tex_pos = ivec3(probe_base_pos.xy, int(cascade));
137 tex_pos.x += probe_base_pos.z * sdfgi.probe_axis_size;
138 tex_pos.xy = tex_pos.xy * (SDFGI_OCT_SIZE + 2) + ivec2(1);
139
140 vec3 diffuse_posf = (vec3(tex_pos) + vec3(octahedron_encode(cam_normal) * float(SDFGI_OCT_SIZE), 0.0)) * sdfgi.lightprobe_tex_pixel_size;
141
142 vec3 specular_posf;
143
144 if (use_specular) {
145 specular_accum = vec3(0.0);
146 specular_posf = (vec3(tex_pos) + vec3(octahedron_encode(cam_specular_normal) * float(SDFGI_OCT_SIZE), 0.0)) * sdfgi.lightprobe_tex_pixel_size;
147 }
148
149 vec4 light_accum = vec4(0.0);
150 float weight_accum = 0.0;
151
152 for (uint j = 0; j < 8; j++) {
153 ivec3 offset = (ivec3(j) >> ivec3(0, 1, 2)) & ivec3(1, 1, 1);
154 ivec3 probe_posi = probe_base_pos;
155 probe_posi += offset;
156
157 // Compute weight
158
159 vec3 probe_pos = vec3(probe_posi);
160 vec3 probe_to_pos = cascade_pos - probe_pos;
161 vec3 probe_dir = normalize(-probe_to_pos);
162
163 vec3 trilinear = vec3(1.0) - abs(probe_to_pos);
164 float weight = trilinear.x * trilinear.y * trilinear.z * max(0.005, dot(cam_normal, probe_dir));
165
166 // Compute lightprobe occlusion
167
168 if (sdfgi.use_occlusion) {
169 ivec3 occ_indexv = abs((sdfgi.cascades[cascade].probe_world_offset + probe_posi) & ivec3(1, 1, 1)) * ivec3(1, 2, 4);
170 vec4 occ_mask = mix(vec4(0.0), vec4(1.0), equal(ivec4(occ_indexv.x | occ_indexv.y), ivec4(0, 1, 2, 3)));
171
172 vec3 occ_pos = clamp(cascade_pos, probe_pos - sdfgi.occlusion_clamp, probe_pos + sdfgi.occlusion_clamp) * sdfgi.probe_to_uvw;
173 occ_pos.z += float(cascade);
174 if (occ_indexv.z != 0) { //z bit is on, means index is >=4, so make it switch to the other half of textures
175 occ_pos.x += 1.0;
176 }
177
178 occ_pos *= sdfgi.occlusion_renormalize;
179 float occlusion = dot(textureLod(sampler3D(sdfgi_occlusion_cascades, SAMPLER_LINEAR_CLAMP), occ_pos, 0.0), occ_mask);
180
181 weight *= max(occlusion, 0.01);
182 }
183
184 // Compute lightprobe texture position
185
186 vec3 diffuse;
187 vec3 pos_uvw = diffuse_posf;
188 pos_uvw.xy += vec2(offset.xy) * sdfgi.lightprobe_uv_offset.xy;
189 pos_uvw.x += float(offset.z) * sdfgi.lightprobe_uv_offset.z;
190 diffuse = textureLod(sampler2DArray(sdfgi_lightprobe_texture, SAMPLER_LINEAR_CLAMP), pos_uvw, 0.0).rgb;
191
192 diffuse_accum += vec4(diffuse * weight * sdfgi.cascades[cascade].exposure_normalization, weight);
193
194 if (use_specular) {
195 vec3 specular = vec3(0.0);
196 vec3 pos_uvw = specular_posf;
197 pos_uvw.xy += vec2(offset.xy) * sdfgi.lightprobe_uv_offset.xy;
198 pos_uvw.x += float(offset.z) * sdfgi.lightprobe_uv_offset.z;
199 if (roughness < 0.99) {
200 specular = textureLod(sampler2DArray(sdfgi_lightprobe_texture, SAMPLER_LINEAR_CLAMP), pos_uvw + vec3(0, 0, float(sdfgi.max_cascades)), 0.0).rgb;
201 }
202 if (roughness > 0.5) {
203 specular = mix(specular, textureLod(sampler2DArray(sdfgi_lightprobe_texture, SAMPLER_LINEAR_CLAMP), pos_uvw, 0.0).rgb, (roughness - 0.5) * 2.0);
204 }
205
206 specular_accum += specular * weight * sdfgi.cascades[cascade].exposure_normalization;
207 }
208 }
209
210 if (diffuse_accum.a > 0.0) {
211 diffuse_accum.rgb /= diffuse_accum.a;
212 }
213
214 diffuse_light = diffuse_accum.rgb;
215
216 if (use_specular) {
217 if (diffuse_accum.a > 0.0) {
218 specular_accum /= diffuse_accum.a;
219 }
220
221 specular_light = specular_accum;
222 }
223
224 {
225 //process blend
226 float blend_from = (float(sdfgi.probe_axis_size - 1) / 2.0) - 2.5;
227 float blend_to = blend_from + 2.0;
228
229 vec3 inner_pos = cam_pos * sdfgi.cascades[cascade].to_probe;
230
231 float len = length(inner_pos);
232
233 inner_pos = abs(normalize(inner_pos));
234 len *= max(inner_pos.x, max(inner_pos.y, inner_pos.z));
235
236 if (len >= blend_from) {
237 blend = smoothstep(blend_from, blend_to, len);
238 } else {
239 blend = 0.0;
240 }
241 }
242 }