| 1 |
#[compute] |
| 2 |
|
| 3 |
#version 450 |
| 4 |
|
| 5 |
#VERSION_DEFINES |
| 6 |
|
| 7 |
#ifdef SAMPLE_VOXEL_GI_NEAREST |
| 8 |
#extension GL_EXT_samplerless_texture_functions : enable |
| 9 |
#endif |
| 10 |
|
| 11 |
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in; |
| 12 |
|
| 13 |
#define M_PI 3.141592 |
| 14 |
|
| 15 |
/* Specialization Constants (Toggles) */ |
| 16 |
|
| 17 |
layout(constant_id = 0) const bool sc_half_res = false; |
| 18 |
layout(constant_id = 1) const bool sc_use_full_projection_matrix = false; |
| 19 |
layout(constant_id = 2) const bool sc_use_vrs = false; |
| 20 |
|
| 21 |
#define SDFGI_MAX_CASCADES 8 |
| 22 |
|
| 23 |
//set 0 for SDFGI and render buffers |
| 24 |
|
| 25 |
layout(set = 0, binding = 1) uniform texture3D sdf_cascades[SDFGI_MAX_CASCADES]; |
| 26 |
layout(set = 0, binding = 2) uniform texture3D light_cascades[SDFGI_MAX_CASCADES]; |
| 27 |
layout(set = 0, binding = 3) uniform texture3D aniso0_cascades[SDFGI_MAX_CASCADES]; |
| 28 |
layout(set = 0, binding = 4) uniform texture3D aniso1_cascades[SDFGI_MAX_CASCADES]; |
| 29 |
layout(set = 0, binding = 5) uniform texture3D occlusion_texture; |
| 30 |
|
| 31 |
layout(set = 0, binding = 6) uniform sampler linear_sampler; |
| 32 |
layout(set = 0, binding = 7) uniform sampler linear_sampler_with_mipmaps; |
| 33 |
|
| 34 |
struct ProbeCascadeData { |
| 35 |
vec3 position; |
| 36 |
float to_probe; |
| 37 |
ivec3 probe_world_offset; |
| 38 |
float to_cell; // 1/bounds * grid_size |
| 39 |
vec3 pad; |
| 40 |
float exposure_normalization; |
| 41 |
}; |
| 42 |
|
| 43 |
layout(rgba16f, set = 0, binding = 9) uniform restrict writeonly image2D ambient_buffer; |
| 44 |
layout(rgba16f, set = 0, binding = 10) uniform restrict writeonly image2D reflection_buffer; |
| 45 |
|
| 46 |
layout(set = 0, binding = 11) uniform texture2DArray lightprobe_texture; |
| 47 |
|
| 48 |
layout(set = 0, binding = 12) uniform texture2D depth_buffer; |
| 49 |
layout(set = 0, binding = 13) uniform texture2D normal_roughness_buffer; |
| 50 |
layout(set = 0, binding = 14) uniform utexture2D voxel_gi_buffer; |
| 51 |
|
| 52 |
layout(set = 0, binding = 15, std140) uniform SDFGI { |
| 53 |
vec3 grid_size; |
| 54 |
uint max_cascades; |
| 55 |
|
| 56 |
bool use_occlusion; |
| 57 |
int probe_axis_size; |
| 58 |
float probe_to_uvw; |
| 59 |
float normal_bias; |
| 60 |
|
| 61 |
vec3 lightprobe_tex_pixel_size; |
| 62 |
float energy; |
| 63 |
|
| 64 |
vec3 lightprobe_uv_offset; |
| 65 |
float y_mult; |
| 66 |
|
| 67 |
vec3 occlusion_clamp; |
| 68 |
uint pad3; |
| 69 |
|
| 70 |
vec3 occlusion_renormalize; |
| 71 |
uint pad4; |
| 72 |
|
| 73 |
vec3 cascade_probe_size; |
| 74 |
uint pad5; |
| 75 |
|
| 76 |
ProbeCascadeData cascades[SDFGI_MAX_CASCADES]; |
| 77 |
} |
| 78 |
sdfgi; |
| 79 |
|
| 80 |
#define MAX_VOXEL_GI_INSTANCES 8 |
| 81 |
|
| 82 |
struct VoxelGIData { |
| 83 |
mat4 xform; // 64 - 64 |
| 84 |
|
| 85 |
vec3 bounds; // 12 - 76 |
| 86 |
float dynamic_range; // 4 - 80 |
| 87 |
|
| 88 |
float bias; // 4 - 84 |
| 89 |
float normal_bias; // 4 - 88 |
| 90 |
bool blend_ambient; // 4 - 92 |
| 91 |
uint mipmaps; // 4 - 96 |
| 92 |
|
| 93 |
vec3 pad; // 12 - 108 |
| 94 |
float exposure_normalization; // 4 - 112 |
| 95 |
}; |
| 96 |
|
| 97 |
layout(set = 0, binding = 16, std140) uniform VoxelGIs { |
| 98 |
VoxelGIData data[MAX_VOXEL_GI_INSTANCES]; |
| 99 |
} |
| 100 |
voxel_gi_instances; |
| 101 |
|
| 102 |
layout(set = 0, binding = 17) uniform texture3D voxel_gi_textures[MAX_VOXEL_GI_INSTANCES]; |
| 103 |
|
| 104 |
layout(set = 0, binding = 18, std140) uniform SceneData { |
| 105 |
mat4x4 inv_projection[2]; |
| 106 |
mat4x4 cam_transform; |
| 107 |
vec4 eye_offset[2]; |
| 108 |
|
| 109 |
ivec2 screen_size; |
| 110 |
float pad1; |
| 111 |
float pad2; |
| 112 |
} |
| 113 |
scene_data; |
| 114 |
|
| 115 |
#ifdef USE_VRS |
| 116 |
layout(r8ui, set = 0, binding = 19) uniform restrict readonly uimage2D vrs_buffer; |
| 117 |
#endif |
| 118 |
|
| 119 |
layout(push_constant, std430) uniform Params { |
| 120 |
uint max_voxel_gi_instances; |
| 121 |
bool high_quality_vct; |
| 122 |
bool orthogonal; |
| 123 |
uint view_index; |
| 124 |
|
| 125 |
vec4 proj_info; |
| 126 |
|
| 127 |
float z_near; |
| 128 |
float z_far; |
| 129 |
float pad2; |
| 130 |
float pad3; |
| 131 |
} |
| 132 |
params; |
| 133 |
|
| 134 |
vec2 octahedron_wrap(vec2 v) { |
| 135 |
vec2 signVal; |
| 136 |
signVal.x = v.x >= 0.0 ? 1.0 : -1.0; |
| 137 |
signVal.y = v.y >= 0.0 ? 1.0 : -1.0; |
| 138 |
return (1.0 - abs(v.yx)) * signVal; |
| 139 |
} |
| 140 |
|
| 141 |
vec2 octahedron_encode(vec3 n) { |
| 142 |
// https://twitter.com/Stubbesaurus/status/937994790553227264 |
| 143 |
n /= (abs(n.x) + abs(n.y) + abs(n.z)); |
| 144 |
n.xy = n.z >= 0.0 ? n.xy : octahedron_wrap(n.xy); |
| 145 |
n.xy = n.xy * 0.5 + 0.5; |
| 146 |
return n.xy; |
| 147 |
} |
| 148 |
|
| 149 |
vec4 blend_color(vec4 src, vec4 dst) { |
| 150 |
vec4 res; |
| 151 |
float sa = 1.0 - src.a; |
| 152 |
res.a = dst.a * sa + src.a; |
| 153 |
if (res.a == 0.0) { |
| 154 |
res.rgb = vec3(0); |
| 155 |
} else { |
| 156 |
res.rgb = (dst.rgb * dst.a * sa + src.rgb * src.a) / res.a; |
| 157 |
} |
| 158 |
return res; |
| 159 |
} |
| 160 |
|
| 161 |
vec3 reconstruct_position(ivec2 screen_pos) { |
| 162 |
if (sc_use_full_projection_matrix) { |
| 163 |
vec4 pos; |
| 164 |
pos.xy = (2.0 * vec2(screen_pos) / vec2(scene_data.screen_size)) - 1.0; |
| 165 |
pos.z = texelFetch(sampler2D(depth_buffer, linear_sampler), screen_pos, 0).r * 2.0 - 1.0; |
| 166 |
pos.w = 1.0; |
| 167 |
|
| 168 |
pos = scene_data.inv_projection[params.view_index] * pos; |
| 169 |
|
| 170 |
return pos.xyz / pos.w; |
| 171 |
} else { |
| 172 |
vec3 pos; |
| 173 |
pos.z = texelFetch(sampler2D(depth_buffer, linear_sampler), screen_pos, 0).r; |
| 174 |
|
| 175 |
pos.z = pos.z * 2.0 - 1.0; |
| 176 |
if (params.orthogonal) { |
| 177 |
pos.z = -(pos.z * (params.z_far - params.z_near) - (params.z_far + params.z_near)) / 2.0; |
| 178 |
} else { |
| 179 |
pos.z = 2.0 * params.z_near * params.z_far / (params.z_far + params.z_near + pos.z * (params.z_far - params.z_near)); |
| 180 |
} |
| 181 |
pos.z = -pos.z; |
| 182 |
|
| 183 |
pos.xy = vec2(screen_pos) * params.proj_info.xy + params.proj_info.zw; |
| 184 |
if (!params.orthogonal) { |
| 185 |
pos.xy *= pos.z; |
| 186 |
} |
| 187 |
|
| 188 |
return pos; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
void sdfvoxel_gi_process(uint cascade, vec3 cascade_pos, vec3 cam_pos, vec3 cam_normal, vec3 cam_specular_normal, float roughness, out vec3 diffuse_light, out vec3 specular_light) { |
| 193 |
cascade_pos += cam_normal * sdfgi.normal_bias; |
| 194 |
|
| 195 |
vec3 base_pos = floor(cascade_pos); |
| 196 |
//cascade_pos += mix(vec3(0.0),vec3(0.01),lessThan(abs(cascade_pos-base_pos),vec3(0.01))) * cam_normal; |
| 197 |
ivec3 probe_base_pos = ivec3(base_pos); |
| 198 |
|
| 199 |
vec4 diffuse_accum = vec4(0.0); |
| 200 |
vec3 specular_accum; |
| 201 |
|
| 202 |
ivec3 tex_pos = ivec3(probe_base_pos.xy, int(cascade)); |
| 203 |
tex_pos.x += probe_base_pos.z * sdfgi.probe_axis_size; |
| 204 |
tex_pos.xy = tex_pos.xy * (SDFGI_OCT_SIZE + 2) + ivec2(1); |
| 205 |
|
| 206 |
vec3 diffuse_posf = (vec3(tex_pos) + vec3(octahedron_encode(cam_normal) * float(SDFGI_OCT_SIZE), 0.0)) * sdfgi.lightprobe_tex_pixel_size; |
| 207 |
|
| 208 |
vec3 specular_posf = (vec3(tex_pos) + vec3(octahedron_encode(cam_specular_normal) * float(SDFGI_OCT_SIZE), 0.0)) * sdfgi.lightprobe_tex_pixel_size; |
| 209 |
|
| 210 |
specular_accum = vec3(0.0); |
| 211 |
|
| 212 |
vec4 light_accum = vec4(0.0); |
| 213 |
float weight_accum = 0.0; |
| 214 |
|
| 215 |
for (uint j = 0; j < 8; j++) { |
| 216 |
ivec3 offset = (ivec3(j) >> ivec3(0, 1, 2)) & ivec3(1, 1, 1); |
| 217 |
ivec3 probe_posi = probe_base_pos; |
| 218 |
probe_posi += offset; |
| 219 |
|
| 220 |
// Compute weight |
| 221 |
|
| 222 |
vec3 probe_pos = vec3(probe_posi); |
| 223 |
vec3 probe_to_pos = cascade_pos - probe_pos; |
| 224 |
vec3 probe_dir = normalize(-probe_to_pos); |
| 225 |
|
| 226 |
vec3 trilinear = vec3(1.0) - abs(probe_to_pos); |
| 227 |
float weight = trilinear.x * trilinear.y * trilinear.z * max(0.005, dot(cam_normal, probe_dir)); |
| 228 |
|
| 229 |
// Compute lightprobe occlusion |
| 230 |
|
| 231 |
if (sdfgi.use_occlusion) { |
| 232 |
ivec3 occ_indexv = abs((sdfgi.cascades[cascade].probe_world_offset + probe_posi) & ivec3(1, 1, 1)) * ivec3(1, 2, 4); |
| 233 |
vec4 occ_mask = mix(vec4(0.0), vec4(1.0), equal(ivec4(occ_indexv.x | occ_indexv.y), ivec4(0, 1, 2, 3))); |
| 234 |
|
| 235 |
vec3 occ_pos = clamp(cascade_pos, probe_pos - sdfgi.occlusion_clamp, probe_pos + sdfgi.occlusion_clamp) * sdfgi.probe_to_uvw; |
| 236 |
occ_pos.z += float(cascade); |
| 237 |
if (occ_indexv.z != 0) { //z bit is on, means index is >=4, so make it switch to the other half of textures |
| 238 |
occ_pos.x += 1.0; |
| 239 |
} |
| 240 |
|
| 241 |
occ_pos *= sdfgi.occlusion_renormalize; |
| 242 |
float occlusion = dot(textureLod(sampler3D(occlusion_texture, linear_sampler), occ_pos, 0.0), occ_mask); |
| 243 |
|
| 244 |
weight *= max(occlusion, 0.01); |
| 245 |
} |
| 246 |
|
| 247 |
// Compute lightprobe texture position |
| 248 |
|
| 249 |
vec3 diffuse; |
| 250 |
vec3 pos_uvw = diffuse_posf; |
| 251 |
pos_uvw.xy += vec2(offset.xy) * sdfgi.lightprobe_uv_offset.xy; |
| 252 |
pos_uvw.x += float(offset.z) * sdfgi.lightprobe_uv_offset.z; |
| 253 |
diffuse = textureLod(sampler2DArray(lightprobe_texture, linear_sampler), pos_uvw, 0.0).rgb; |
| 254 |
|
| 255 |
diffuse_accum += vec4(diffuse * weight * sdfgi.cascades[cascade].exposure_normalization, weight); |
| 256 |
|
| 257 |
{ |
| 258 |
vec3 specular = vec3(0.0); |
| 259 |
vec3 pos_uvw = specular_posf; |
| 260 |
pos_uvw.xy += vec2(offset.xy) * sdfgi.lightprobe_uv_offset.xy; |
| 261 |
pos_uvw.x += float(offset.z) * sdfgi.lightprobe_uv_offset.z; |
| 262 |
if (roughness < 0.99) { |
| 263 |
specular = textureLod(sampler2DArray(lightprobe_texture, linear_sampler), pos_uvw + vec3(0, 0, float(sdfgi.max_cascades)), 0.0).rgb; |
| 264 |
} |
| 265 |
if (roughness > 0.2) { |
| 266 |
specular = mix(specular, textureLod(sampler2DArray(lightprobe_texture, linear_sampler), pos_uvw, 0.0).rgb, (roughness - 0.2) * 1.25); |
| 267 |
} |
| 268 |
|
| 269 |
specular_accum += specular * weight * sdfgi.cascades[cascade].exposure_normalization; |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
if (diffuse_accum.a > 0.0) { |
| 274 |
diffuse_accum.rgb /= diffuse_accum.a; |
| 275 |
} |
| 276 |
|
| 277 |
diffuse_light = diffuse_accum.rgb; |
| 278 |
|
| 279 |
if (diffuse_accum.a > 0.0) { |
| 280 |
specular_accum /= diffuse_accum.a; |
| 281 |
} |
| 282 |
|
| 283 |
specular_light = specular_accum; |
| 284 |
} |
| 285 |
|
| 286 |
void sdfgi_process(vec3 vertex, vec3 normal, vec3 reflection, float roughness, out vec4 ambient_light, out vec4 reflection_light) { |
| 287 |
//make vertex orientation the world one, but still align to camera |
| 288 |
vertex.y *= sdfgi.y_mult; |
| 289 |
normal.y *= sdfgi.y_mult; |
| 290 |
reflection.y *= sdfgi.y_mult; |
| 291 |
|
| 292 |
//renormalize |
| 293 |
normal = normalize(normal); |
| 294 |
reflection = normalize(reflection); |
| 295 |
|
| 296 |
vec3 cam_pos = vertex; |
| 297 |
vec3 cam_normal = normal; |
| 298 |
|
| 299 |
vec4 light_accum = vec4(0.0); |
| 300 |
float weight_accum = 0.0; |
| 301 |
|
| 302 |
vec4 light_blend_accum = vec4(0.0); |
| 303 |
float weight_blend_accum = 0.0; |
| 304 |
|
| 305 |
float blend = -1.0; |
| 306 |
|
| 307 |
// helper constants, compute once |
| 308 |
|
| 309 |
uint cascade = 0xFFFFFFFF; |
| 310 |
vec3 cascade_pos; |
| 311 |
vec3 cascade_normal; |
| 312 |
|
| 313 |
for (uint i = 0; i < sdfgi.max_cascades; i++) { |
| 314 |
cascade_pos = (cam_pos - sdfgi.cascades[i].position) * sdfgi.cascades[i].to_probe; |
| 315 |
|
| 316 |
if (any(lessThan(cascade_pos, vec3(0.0))) || any(greaterThanEqual(cascade_pos, sdfgi.cascade_probe_size))) { |
| 317 |
continue; //skip cascade |
| 318 |
} |
| 319 |
|
| 320 |
cascade = i; |
| 321 |
break; |
| 322 |
} |
| 323 |
|
| 324 |
if (cascade < SDFGI_MAX_CASCADES) { |
| 325 |
ambient_light = vec4(0, 0, 0, 1); |
| 326 |
reflection_light = vec4(0, 0, 0, 1); |
| 327 |
|
| 328 |
float blend; |
| 329 |
vec3 diffuse, specular; |
| 330 |
sdfvoxel_gi_process(cascade, cascade_pos, cam_pos, cam_normal, reflection, roughness, diffuse, specular); |
| 331 |
|
| 332 |
{ |
| 333 |
//process blend |
| 334 |
float blend_from = (float(sdfgi.probe_axis_size - 1) / 2.0) - 2.5; |
| 335 |
float blend_to = blend_from + 2.0; |
| 336 |
|
| 337 |
vec3 inner_pos = cam_pos * sdfgi.cascades[cascade].to_probe; |
| 338 |
|
| 339 |
float len = length(inner_pos); |
| 340 |
|
| 341 |
inner_pos = abs(normalize(inner_pos)); |
| 342 |
len *= max(inner_pos.x, max(inner_pos.y, inner_pos.z)); |
| 343 |
|
| 344 |
if (len >= blend_from) { |
| 345 |
blend = smoothstep(blend_from, blend_to, len); |
| 346 |
} else { |
| 347 |
blend = 0.0; |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
if (blend > 0.0) { |
| 352 |
//blend |
| 353 |
if (cascade == sdfgi.max_cascades - 1) { |
| 354 |
ambient_light.a = 1.0 - blend; |
| 355 |
reflection_light.a = 1.0 - blend; |
| 356 |
|
| 357 |
} else { |
| 358 |
vec3 diffuse2, specular2; |
| 359 |
cascade_pos = (cam_pos - sdfgi.cascades[cascade + 1].position) * sdfgi.cascades[cascade + 1].to_probe; |
| 360 |
sdfvoxel_gi_process(cascade + 1, cascade_pos, cam_pos, cam_normal, reflection, roughness, diffuse2, specular2); |
| 361 |
diffuse = mix(diffuse, diffuse2, blend); |
| 362 |
specular = mix(specular, specular2, blend); |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
ambient_light.rgb = diffuse; |
| 367 |
|
| 368 |
if (roughness < 0.2) { |
| 369 |
vec3 pos_to_uvw = 1.0 / sdfgi.grid_size; |
| 370 |
vec4 light_accum = vec4(0.0); |
| 371 |
|
| 372 |
float blend_size = (sdfgi.grid_size.x / float(sdfgi.probe_axis_size - 1)) * 0.5; |
| 373 |
|
| 374 |
float radius_sizes[SDFGI_MAX_CASCADES]; |
| 375 |
cascade = 0xFFFF; |
| 376 |
|
| 377 |
float base_distance = length(cam_pos); |
| 378 |
for (uint i = 0; i < sdfgi.max_cascades; i++) { |
| 379 |
radius_sizes[i] = (1.0 / sdfgi.cascades[i].to_cell) * (sdfgi.grid_size.x * 0.5 - blend_size); |
| 380 |
if (cascade == 0xFFFF && base_distance < radius_sizes[i]) { |
| 381 |
cascade = i; |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
cascade = min(cascade, sdfgi.max_cascades - 1); |
| 386 |
|
| 387 |
float max_distance = radius_sizes[sdfgi.max_cascades - 1]; |
| 388 |
vec3 ray_pos = cam_pos; |
| 389 |
vec3 ray_dir = reflection; |
| 390 |
|
| 391 |
{ |
| 392 |
float prev_radius = cascade > 0 ? radius_sizes[cascade - 1] : 0.0; |
| 393 |
float base_blend = (base_distance - prev_radius) / (radius_sizes[cascade] - prev_radius); |
| 394 |
float bias = (1.0 + base_blend) * 1.1; |
| 395 |
vec3 abs_ray_dir = abs(ray_dir); |
| 396 |
//ray_pos += ray_dir * (bias / sdfgi.cascades[cascade].to_cell); //bias to avoid self occlusion |
| 397 |
ray_pos += (ray_dir * 1.0 / max(abs_ray_dir.x, max(abs_ray_dir.y, abs_ray_dir.z)) + cam_normal * 1.4) * bias / sdfgi.cascades[cascade].to_cell; |
| 398 |
} |
| 399 |
float softness = 0.2 + min(1.0, roughness * 5.0) * 4.0; //approximation to roughness so it does not seem like a hard fade |
| 400 |
uint i = 0; |
| 401 |
bool found = false; |
| 402 |
while (true) { |
| 403 |
if (length(ray_pos) >= max_distance || light_accum.a > 0.99) { |
| 404 |
break; |
| 405 |
} |
| 406 |
if (!found && i >= cascade && length(ray_pos) < radius_sizes[i]) { |
| 407 |
uint next_i = min(i + 1, sdfgi.max_cascades - 1); |
| 408 |
cascade = max(i, cascade); //never go down |
| 409 |
|
| 410 |
vec3 pos = ray_pos - sdfgi.cascades[i].position; |
| 411 |
pos *= sdfgi.cascades[i].to_cell * pos_to_uvw; |
| 412 |
|
| 413 |
float fdistance = textureLod(sampler3D(sdf_cascades[i], linear_sampler), pos, 0.0).r * 255.0 - 1.1; |
| 414 |
|
| 415 |
vec4 hit_light = vec4(0.0); |
| 416 |
if (fdistance < softness) { |
| 417 |
hit_light.rgb = textureLod(sampler3D(light_cascades[i], linear_sampler), pos, 0.0).rgb; |
| 418 |
hit_light.rgb *= 0.5; //approximation given value read is actually meant for anisotropy |
| 419 |
hit_light.a = clamp(1.0 - (fdistance / softness), 0.0, 1.0); |
| 420 |
hit_light.rgb *= hit_light.a; |
| 421 |
} |
| 422 |
|
| 423 |
fdistance /= sdfgi.cascades[i].to_cell; |
| 424 |
|
| 425 |
if (i < (sdfgi.max_cascades - 1)) { |
| 426 |
pos = ray_pos - sdfgi.cascades[next_i].position; |
| 427 |
pos *= sdfgi.cascades[next_i].to_cell * pos_to_uvw; |
| 428 |
|
| 429 |
float fdistance2 = textureLod(sampler3D(sdf_cascades[next_i], linear_sampler), pos, 0.0).r * 255.0 - 1.1; |
| 430 |
|
| 431 |
vec4 hit_light2 = vec4(0.0); |
| 432 |
if (fdistance2 < softness) { |
| 433 |
hit_light2.rgb = textureLod(sampler3D(light_cascades[next_i], linear_sampler), pos, 0.0).rgb; |
| 434 |
hit_light2.rgb *= 0.5; //approximation given value read is actually meant for anisotropy |
| 435 |
hit_light2.a = clamp(1.0 - (fdistance2 / softness), 0.0, 1.0); |
| 436 |
hit_light2.rgb *= hit_light2.a; |
| 437 |
} |
| 438 |
|
| 439 |
float prev_radius = i == 0 ? 0.0 : radius_sizes[max(0, i - 1)]; |
| 440 |
float blend = clamp((length(ray_pos) - prev_radius) / (radius_sizes[i] - prev_radius), 0.0, 1.0); |
| 441 |
|
| 442 |
fdistance2 /= sdfgi.cascades[next_i].to_cell; |
| 443 |
|
| 444 |
hit_light = mix(hit_light, hit_light2, blend); |
| 445 |
fdistance = mix(fdistance, fdistance2, blend); |
| 446 |
} |
| 447 |
|
| 448 |
light_accum += hit_light; |
| 449 |
ray_pos += ray_dir * fdistance; |
| 450 |
found = true; |
| 451 |
} |
| 452 |
i++; |
| 453 |
if (i == sdfgi.max_cascades) { |
| 454 |
i = 0; |
| 455 |
found = false; |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
vec3 light = light_accum.rgb / max(light_accum.a, 0.00001); |
| 460 |
float alpha = min(1.0, light_accum.a); |
| 461 |
|
| 462 |
float b = min(1.0, roughness * 5.0); |
| 463 |
|
| 464 |
float sa = 1.0 - b; |
| 465 |
|
| 466 |
reflection_light.a = alpha * sa + b; |
| 467 |
if (reflection_light.a == 0) { |
| 468 |
specular = vec3(0.0); |
| 469 |
} else { |
| 470 |
specular = (light * alpha * sa + specular * b) / reflection_light.a; |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
reflection_light.rgb = specular; |
| 475 |
|
| 476 |
ambient_light.rgb *= sdfgi.energy; |
| 477 |
reflection_light.rgb *= sdfgi.energy; |
| 478 |
} else { |
| 479 |
ambient_light = vec4(0); |
| 480 |
reflection_light = vec4(0); |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
//standard voxel cone trace |
| 485 |
vec4 voxel_cone_trace(texture3D probe, vec3 cell_size, vec3 pos, vec3 direction, float tan_half_angle, float max_distance, float p_bias) { |
| 486 |
float dist = p_bias; |
| 487 |
vec4 color = vec4(0.0); |
| 488 |
|
| 489 |
while (dist < max_distance && color.a < 1.0) { |
| 490 |
float diameter = max(1.0, 2.0 * tan_half_angle * dist); |
| 491 |
vec3 uvw_pos = (pos + dist * direction) * cell_size; |
| 492 |
float half_diameter = diameter * 0.5; |
| 493 |
//check if outside, then break |
| 494 |
if (any(greaterThan(abs(uvw_pos - 0.5), vec3(0.5f + half_diameter * cell_size)))) { |
| 495 |
break; |
| 496 |
} |
| 497 |
vec4 scolor = textureLod(sampler3D(probe, linear_sampler_with_mipmaps), uvw_pos, log2(diameter)); |
| 498 |
float a = (1.0 - color.a); |
| 499 |
color += a * scolor; |
| 500 |
dist += half_diameter; |
| 501 |
} |
| 502 |
|
| 503 |
return color; |
| 504 |
} |
| 505 |
|
| 506 |
vec4 voxel_cone_trace_45_degrees(texture3D probe, vec3 cell_size, vec3 pos, vec3 direction, float max_distance, float p_bias) { |
| 507 |
float dist = p_bias; |
| 508 |
vec4 color = vec4(0.0); |
| 509 |
float radius = max(0.5, dist); |
| 510 |
float lod_level = log2(radius * 2.0); |
| 511 |
|
| 512 |
while (dist < max_distance && color.a < 1.0) { |
| 513 |
vec3 uvw_pos = (pos + dist * direction) * cell_size; |
| 514 |
|
| 515 |
//check if outside, then break |
| 516 |
if (any(greaterThan(abs(uvw_pos - 0.5), vec3(0.5f + radius * cell_size)))) { |
| 517 |
break; |
| 518 |
} |
| 519 |
vec4 scolor = textureLod(sampler3D(probe, linear_sampler_with_mipmaps), uvw_pos, lod_level); |
| 520 |
lod_level += 1.0; |
| 521 |
|
| 522 |
float a = (1.0 - color.a); |
| 523 |
scolor *= a; |
| 524 |
color += scolor; |
| 525 |
dist += radius; |
| 526 |
radius = max(0.5, dist); |
| 527 |
} |
| 528 |
return color; |
| 529 |
} |
| 530 |
|
| 531 |
void voxel_gi_compute(uint index, vec3 position, vec3 normal, vec3 ref_vec, mat3 normal_xform, float roughness, inout vec4 out_spec, inout vec4 out_diff, inout float out_blend) { |
| 532 |
position = (voxel_gi_instances.data[index].xform * vec4(position, 1.0)).xyz; |
| 533 |
ref_vec = normalize((voxel_gi_instances.data[index].xform * vec4(ref_vec, 0.0)).xyz); |
| 534 |
normal = normalize((voxel_gi_instances.data[index].xform * vec4(normal, 0.0)).xyz); |
| 535 |
|
| 536 |
position += normal * voxel_gi_instances.data[index].normal_bias; |
| 537 |
|
| 538 |
//this causes corrupted pixels, i have no idea why.. |
| 539 |
if (any(bvec2(any(lessThan(position, vec3(0.0))), any(greaterThan(position, voxel_gi_instances.data[index].bounds))))) { |
| 540 |
return; |
| 541 |
} |
| 542 |
|
| 543 |
mat3 dir_xform = mat3(voxel_gi_instances.data[index].xform) * normal_xform; |
| 544 |
|
| 545 |
vec3 blendv = abs(position / voxel_gi_instances.data[index].bounds * 2.0 - 1.0); |
| 546 |
float blend = clamp(1.0 - max(blendv.x, max(blendv.y, blendv.z)), 0.0, 1.0); |
| 547 |
//float blend=1.0; |
| 548 |
|
| 549 |
float max_distance = length(voxel_gi_instances.data[index].bounds); |
| 550 |
vec3 cell_size = 1.0 / voxel_gi_instances.data[index].bounds; |
| 551 |
|
| 552 |
//irradiance |
| 553 |
|
| 554 |
vec4 light = vec4(0.0); |
| 555 |
|
| 556 |
if (params.high_quality_vct) { |
| 557 |
const uint cone_dir_count = 6; |
| 558 |
vec3 cone_dirs[cone_dir_count] = vec3[]( |
| 559 |
vec3(0.0, 0.0, 1.0), |
| 560 |
vec3(0.866025, 0.0, 0.5), |
| 561 |
vec3(0.267617, 0.823639, 0.5), |
| 562 |
vec3(-0.700629, 0.509037, 0.5), |
| 563 |
vec3(-0.700629, -0.509037, 0.5), |
| 564 |
vec3(0.267617, -0.823639, 0.5)); |
| 565 |
|
| 566 |
float cone_weights[cone_dir_count] = float[](0.25, 0.15, 0.15, 0.15, 0.15, 0.15); |
| 567 |
float cone_angle_tan = 0.577; |
| 568 |
|
| 569 |
for (uint i = 0; i < cone_dir_count; i++) { |
| 570 |
vec3 dir = normalize(dir_xform * cone_dirs[i]); |
| 571 |
light += cone_weights[i] * voxel_cone_trace(voxel_gi_textures[index], cell_size, position, dir, cone_angle_tan, max_distance, voxel_gi_instances.data[index].bias); |
| 572 |
} |
| 573 |
} else { |
| 574 |
const uint cone_dir_count = 4; |
| 575 |
vec3 cone_dirs[cone_dir_count] = vec3[]( |
| 576 |
vec3(0.707107, 0.0, 0.707107), |
| 577 |
vec3(0.0, 0.707107, 0.707107), |
| 578 |
vec3(-0.707107, 0.0, 0.707107), |
| 579 |
vec3(0.0, -0.707107, 0.707107)); |
| 580 |
|
| 581 |
float cone_weights[cone_dir_count] = float[](0.25, 0.25, 0.25, 0.25); |
| 582 |
for (int i = 0; i < cone_dir_count; i++) { |
| 583 |
vec3 dir = normalize(dir_xform * cone_dirs[i]); |
| 584 |
light += cone_weights[i] * voxel_cone_trace_45_degrees(voxel_gi_textures[index], cell_size, position, dir, max_distance, voxel_gi_instances.data[index].bias); |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
light.rgb *= voxel_gi_instances.data[index].dynamic_range * voxel_gi_instances.data[index].exposure_normalization; |
| 589 |
if (!voxel_gi_instances.data[index].blend_ambient) { |
| 590 |
light.a = 1.0; |
| 591 |
} |
| 592 |
|
| 593 |
out_diff += light * blend; |
| 594 |
|
| 595 |
//radiance |
| 596 |
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); |
| 597 |
irr_light.rgb *= voxel_gi_instances.data[index].dynamic_range * voxel_gi_instances.data[index].exposure_normalization; |
| 598 |
if (!voxel_gi_instances.data[index].blend_ambient) { |
| 599 |
irr_light.a = 1.0; |
| 600 |
} |
| 601 |
|
| 602 |
out_spec += irr_light * blend; |
| 603 |
|
| 604 |
out_blend += blend; |
| 605 |
} |
| 606 |
|
| 607 |
vec4 fetch_normal_and_roughness(ivec2 pos) { |
| 608 |
vec4 normal_roughness = texelFetch(sampler2D(normal_roughness_buffer, linear_sampler), pos, 0); |
| 609 |
normal_roughness.xyz = normalize(normal_roughness.xyz * 2.0 - 1.0); |
| 610 |
return normal_roughness; |
| 611 |
} |
| 612 |
|
| 613 |
void process_gi(ivec2 pos, vec3 vertex, inout vec4 ambient_light, inout vec4 reflection_light) { |
| 614 |
vec4 normal_roughness = fetch_normal_and_roughness(pos); |
| 615 |
|
| 616 |
vec3 normal = normal_roughness.xyz; |
| 617 |
|
| 618 |
if (normal.length() > 0.5) { |
| 619 |
//valid normal, can do GI |
| 620 |
float roughness = normal_roughness.w; |
| 621 |
bool dynamic_object = roughness > 0.5; |
| 622 |
if (dynamic_object) { |
| 623 |
roughness = 1.0 - roughness; |
| 624 |
} |
| 625 |
roughness /= (127.0 / 255.0); |
| 626 |
vec3 view = -normalize(mat3(scene_data.cam_transform) * (vertex - scene_data.eye_offset[gl_GlobalInvocationID.z].xyz)); |
| 627 |
vertex = mat3(scene_data.cam_transform) * vertex; |
| 628 |
normal = normalize(mat3(scene_data.cam_transform) * normal); |
| 629 |
vec3 reflection = normalize(reflect(-view, normal)); |
| 630 |
|
| 631 |
#ifdef USE_SDFGI |
| 632 |
sdfgi_process(vertex, normal, reflection, roughness, ambient_light, reflection_light); |
| 633 |
#endif |
| 634 |
|
| 635 |
#ifdef USE_VOXEL_GI_INSTANCES |
| 636 |
{ |
| 637 |
#ifdef SAMPLE_VOXEL_GI_NEAREST |
| 638 |
uvec2 voxel_gi_tex = texelFetch(voxel_gi_buffer, pos, 0).rg; |
| 639 |
#else |
| 640 |
uvec2 voxel_gi_tex = texelFetch(usampler2D(voxel_gi_buffer, linear_sampler), pos, 0).rg; |
| 641 |
#endif |
| 642 |
roughness *= roughness; |
| 643 |
//find arbitrary tangent and bitangent, then build a matrix |
| 644 |
vec3 v0 = abs(normal.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(0.0, 1.0, 0.0); |
| 645 |
vec3 tangent = normalize(cross(v0, normal)); |
| 646 |
vec3 bitangent = normalize(cross(tangent, normal)); |
| 647 |
mat3 normal_mat = mat3(tangent, bitangent, normal); |
| 648 |
|
| 649 |
vec4 amb_accum = vec4(0.0); |
| 650 |
vec4 spec_accum = vec4(0.0); |
| 651 |
float blend_accum = 0.0; |
| 652 |
|
| 653 |
for (uint i = 0; i < params.max_voxel_gi_instances; i++) { |
| 654 |
if (any(equal(uvec2(i), voxel_gi_tex))) { |
| 655 |
voxel_gi_compute(i, vertex, normal, reflection, normal_mat, roughness, spec_accum, amb_accum, blend_accum); |
| 656 |
} |
| 657 |
} |
| 658 |
if (blend_accum > 0.0) { |
| 659 |
amb_accum /= blend_accum; |
| 660 |
spec_accum /= blend_accum; |
| 661 |
} |
| 662 |
|
| 663 |
#ifdef USE_SDFGI |
| 664 |
reflection_light = blend_color(spec_accum, reflection_light); |
| 665 |
ambient_light = blend_color(amb_accum, ambient_light); |
| 666 |
#else |
| 667 |
reflection_light = spec_accum; |
| 668 |
ambient_light = amb_accum; |
| 669 |
#endif |
| 670 |
} |
| 671 |
#endif |
| 672 |
} |
| 673 |
} |
| 674 |
|
| 675 |
void main() { |
| 676 |
ivec2 pos = ivec2(gl_GlobalInvocationID.xy); |
| 677 |
|
| 678 |
uint vrs_x, vrs_y; |
| 679 |
#ifdef USE_VRS |
| 680 |
if (sc_use_vrs) { |
| 681 |
ivec2 vrs_pos; |
| 682 |
|
| 683 |
// Currently we use a 16x16 texel, possibly some day make this configurable. |
| 684 |
if (sc_half_res) { |
| 685 |
vrs_pos = pos >> 3; |
| 686 |
} else { |
| 687 |
vrs_pos = pos >> 4; |
| 688 |
} |
| 689 |
|
| 690 |
uint vrs_texel = imageLoad(vrs_buffer, vrs_pos).r; |
| 691 |
// note, valid values for vrs_x and vrs_y are 1, 2 and 4. |
| 692 |
vrs_x = 1 << ((vrs_texel >> 2) & 3); |
| 693 |
vrs_y = 1 << (vrs_texel & 3); |
| 694 |
|
| 695 |
if (mod(pos.x, vrs_x) != 0) { |
| 696 |
return; |
| 697 |
} |
| 698 |
|
| 699 |
if (mod(pos.y, vrs_y) != 0) { |
| 700 |
return; |
| 701 |
} |
| 702 |
} |
| 703 |
#endif |
| 704 |
|
| 705 |
if (sc_half_res) { |
| 706 |
pos <<= 1; |
| 707 |
} |
| 708 |
|
| 709 |
if (any(greaterThanEqual(pos, scene_data.screen_size))) { //too large, do nothing |
| 710 |
return; |
| 711 |
} |
| 712 |
|
| 713 |
vec4 ambient_light = vec4(0.0); |
| 714 |
vec4 reflection_light = vec4(0.0); |
| 715 |
|
| 716 |
vec3 vertex = reconstruct_position(pos); |
| 717 |
vertex.y = -vertex.y; |
| 718 |
|
| 719 |
process_gi(pos, vertex, ambient_light, reflection_light); |
| 720 |
|
| 721 |
if (sc_half_res) { |
| 722 |
pos >>= 1; |
| 723 |
} |
| 724 |
|
| 725 |
imageStore(ambient_buffer, pos, ambient_light); |
| 726 |
imageStore(reflection_buffer, pos, reflection_light); |
| 727 |
|
| 728 |
#ifdef USE_VRS |
| 729 |
if (sc_use_vrs) { |
| 730 |
if (vrs_x > 1) { |
| 731 |
imageStore(ambient_buffer, pos + ivec2(1, 0), ambient_light); |
| 732 |
imageStore(reflection_buffer, pos + ivec2(1, 0), reflection_light); |
| 733 |
} |
| 734 |
|
| 735 |
if (vrs_x > 2) { |
| 736 |
imageStore(ambient_buffer, pos + ivec2(2, 0), ambient_light); |
| 737 |
imageStore(reflection_buffer, pos + ivec2(2, 0), reflection_light); |
| 738 |
|
| 739 |
imageStore(ambient_buffer, pos + ivec2(3, 0), ambient_light); |
| 740 |
imageStore(reflection_buffer, pos + ivec2(3, 0), reflection_light); |
| 741 |
} |
| 742 |
|
| 743 |
if (vrs_y > 1) { |
| 744 |
imageStore(ambient_buffer, pos + ivec2(0, 1), ambient_light); |
| 745 |
imageStore(reflection_buffer, pos + ivec2(0, 1), reflection_light); |
| 746 |
} |
| 747 |
|
| 748 |
if (vrs_y > 1 && vrs_x > 1) { |
| 749 |
imageStore(ambient_buffer, pos + ivec2(1, 1), ambient_light); |
| 750 |
imageStore(reflection_buffer, pos + ivec2(1, 1), reflection_light); |
| 751 |
} |
| 752 |
|
| 753 |
if (vrs_y > 1 && vrs_x > 2) { |
| 754 |
imageStore(ambient_buffer, pos + ivec2(2, 1), ambient_light); |
| 755 |
imageStore(reflection_buffer, pos + ivec2(2, 1), reflection_light); |
| 756 |
|
| 757 |
imageStore(ambient_buffer, pos + ivec2(3, 1), ambient_light); |
| 758 |
imageStore(reflection_buffer, pos + ivec2(3, 1), reflection_light); |
| 759 |
} |
| 760 |
|
| 761 |
if (vrs_y > 2) { |
| 762 |
imageStore(ambient_buffer, pos + ivec2(0, 2), ambient_light); |
| 763 |
imageStore(reflection_buffer, pos + ivec2(0, 2), reflection_light); |
| 764 |
imageStore(ambient_buffer, pos + ivec2(0, 3), ambient_light); |
| 765 |
imageStore(reflection_buffer, pos + ivec2(0, 3), reflection_light); |
| 766 |
} |
| 767 |
|
| 768 |
if (vrs_y > 2 && vrs_x > 1) { |
| 769 |
imageStore(ambient_buffer, pos + ivec2(1, 2), ambient_light); |
| 770 |
imageStore(reflection_buffer, pos + ivec2(1, 2), reflection_light); |
| 771 |
imageStore(ambient_buffer, pos + ivec2(1, 3), ambient_light); |
| 772 |
imageStore(reflection_buffer, pos + ivec2(1, 3), reflection_light); |
| 773 |
} |
| 774 |
|
| 775 |
if (vrs_y > 2 && vrs_x > 2) { |
| 776 |
imageStore(ambient_buffer, pos + ivec2(2, 2), ambient_light); |
| 777 |
imageStore(reflection_buffer, pos + ivec2(2, 2), reflection_light); |
| 778 |
imageStore(ambient_buffer, pos + ivec2(2, 3), ambient_light); |
| 779 |
imageStore(reflection_buffer, pos + ivec2(2, 3), reflection_light); |
| 780 |
|
| 781 |
imageStore(ambient_buffer, pos + ivec2(3, 2), ambient_light); |
| 782 |
imageStore(reflection_buffer, pos + ivec2(3, 2), reflection_light); |
| 783 |
imageStore(ambient_buffer, pos + ivec2(3, 3), ambient_light); |
| 784 |
imageStore(reflection_buffer, pos + ivec2(3, 3), reflection_light); |
| 785 |
} |
| 786 |
} |
| 787 |
#endif |
| 788 |
} |