| 1 |
#[compute] |
| 2 |
|
| 3 |
#version 450 |
| 4 |
|
| 5 |
#VERSION_DEFINES |
| 6 |
|
| 7 |
#include "../oct_inc.glsl" |
| 8 |
|
| 9 |
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in; |
| 10 |
|
| 11 |
#define MAX_CASCADES 8 |
| 12 |
|
| 13 |
layout(set = 0, binding = 1) uniform texture3D sdf_cascades[MAX_CASCADES]; |
| 14 |
layout(set = 0, binding = 2) uniform texture3D light_cascades[MAX_CASCADES]; |
| 15 |
layout(set = 0, binding = 3) uniform texture3D aniso0_cascades[MAX_CASCADES]; |
| 16 |
layout(set = 0, binding = 4) uniform texture3D aniso1_cascades[MAX_CASCADES]; |
| 17 |
|
| 18 |
layout(set = 0, binding = 6) uniform sampler linear_sampler; |
| 19 |
|
| 20 |
struct CascadeData { |
| 21 |
vec3 offset; //offset of (0,0,0) in world coordinates |
| 22 |
float to_cell; // 1/bounds * grid_size |
| 23 |
ivec3 probe_world_offset; |
| 24 |
uint pad; |
| 25 |
vec4 pad2; |
| 26 |
}; |
| 27 |
|
| 28 |
layout(set = 0, binding = 7, std140) uniform Cascades { |
| 29 |
CascadeData data[MAX_CASCADES]; |
| 30 |
} |
| 31 |
cascades; |
| 32 |
|
| 33 |
layout(r32ui, set = 0, binding = 8) uniform restrict uimage2DArray lightprobe_texture_data; |
| 34 |
layout(rgba16i, set = 0, binding = 9) uniform restrict iimage2DArray lightprobe_history_texture; |
| 35 |
layout(rgba32i, set = 0, binding = 10) uniform restrict iimage2D lightprobe_average_texture; |
| 36 |
|
| 37 |
//used for scrolling |
| 38 |
layout(rgba16i, set = 0, binding = 11) uniform restrict iimage2DArray lightprobe_history_scroll_texture; |
| 39 |
layout(rgba32i, set = 0, binding = 12) uniform restrict iimage2D lightprobe_average_scroll_texture; |
| 40 |
|
| 41 |
layout(rgba32i, set = 0, binding = 13) uniform restrict iimage2D lightprobe_average_parent_texture; |
| 42 |
|
| 43 |
layout(rgba16f, set = 0, binding = 14) uniform restrict writeonly image2DArray lightprobe_ambient_texture; |
| 44 |
|
| 45 |
#ifdef USE_RADIANCE_OCTMAP_ARRAY |
| 46 |
layout(set = 1, binding = 0) uniform texture2DArray sky_irradiance; |
| 47 |
#else |
| 48 |
layout(set = 1, binding = 0) uniform texture2D sky_irradiance; |
| 49 |
#endif |
| 50 |
layout(set = 1, binding = 1) uniform sampler linear_sampler_mipmaps; |
| 51 |
|
| 52 |
#define HISTORY_BITS 10 |
| 53 |
|
| 54 |
#define SKY_FLAGS_MODE_COLOR 0x01 |
| 55 |
#define SKY_FLAGS_MODE_SKY 0x02 |
| 56 |
#define SKY_FLAGS_ORIENTATION_SIGN 0x04 |
| 57 |
|
| 58 |
layout(push_constant, std430) uniform Params { |
| 59 |
vec3 grid_size; |
| 60 |
uint max_cascades; |
| 61 |
|
| 62 |
uint probe_axis_size; |
| 63 |
uint cascade; |
| 64 |
uint history_index; |
| 65 |
uint history_size; |
| 66 |
|
| 67 |
uint ray_count; |
| 68 |
float ray_bias; |
| 69 |
ivec2 image_size; |
| 70 |
|
| 71 |
ivec3 world_offset; |
| 72 |
uint sky_flags; |
| 73 |
|
| 74 |
ivec3 scroll; |
| 75 |
float sky_energy; |
| 76 |
|
| 77 |
vec3 sky_color_or_orientation; |
| 78 |
float y_mult; |
| 79 |
|
| 80 |
vec2 sky_irradiance_border_size; |
| 81 |
bool store_ambient_texture; |
| 82 |
uint pad; |
| 83 |
} |
| 84 |
params; |
| 85 |
|
| 86 |
const float PI = 3.14159265f; |
| 87 |
const float GOLDEN_ANGLE = PI * (3.0 - sqrt(5.0)); |
| 88 |
|
| 89 |
vec3 vogel_hemisphere(uint p_index, uint p_count, float p_offset) { |
| 90 |
float r = sqrt(float(p_index) + 0.5f) / sqrt(float(p_count)); |
| 91 |
float theta = float(p_index) * GOLDEN_ANGLE + p_offset; |
| 92 |
float y = cos(r * PI * 0.5); |
| 93 |
float l = sin(r * PI * 0.5); |
| 94 |
return vec3(l * cos(theta), l * sin(theta), y * (float(p_index & 1) * 2.0 - 1.0)); |
| 95 |
} |
| 96 |
|
| 97 |
uvec3 hash3(uvec3 x) { |
| 98 |
x = ((x >> 16) ^ x) * 0x45d9f3b; |
| 99 |
x = ((x >> 16) ^ x) * 0x45d9f3b; |
| 100 |
x = (x >> 16) ^ x; |
| 101 |
return x; |
| 102 |
} |
| 103 |
|
| 104 |
float hashf3(vec3 co) { |
| 105 |
return fract(sin(dot(co, vec3(12.9898, 78.233, 137.13451))) * 43758.5453); |
| 106 |
} |
| 107 |
|
| 108 |
vec3 octahedron_encode(vec2 f) { |
| 109 |
// https://twitter.com/Stubbesaurus/status/937994790553227264 |
| 110 |
f = f * 2.0 - 1.0; |
| 111 |
vec3 n = vec3(f.x, f.y, 1.0f - abs(f.x) - abs(f.y)); |
| 112 |
float t = clamp(-n.z, 0.0, 1.0); |
| 113 |
n.x += n.x >= 0 ? -t : t; |
| 114 |
n.y += n.y >= 0 ? -t : t; |
| 115 |
return normalize(n); |
| 116 |
} |
| 117 |
|
| 118 |
uint rgbe_encode(vec3 color) { |
| 119 |
const float pow2to9 = 512.0f; |
| 120 |
const float B = 15.0f; |
| 121 |
const float N = 9.0f; |
| 122 |
const float LN2 = 0.6931471805599453094172321215; |
| 123 |
|
| 124 |
float cRed = clamp(color.r, 0.0, 65408.0); |
| 125 |
float cGreen = clamp(color.g, 0.0, 65408.0); |
| 126 |
float cBlue = clamp(color.b, 0.0, 65408.0); |
| 127 |
|
| 128 |
float cMax = max(cRed, max(cGreen, cBlue)); |
| 129 |
|
| 130 |
float expp = max(-B - 1.0f, floor(log(cMax) / LN2)) + 1.0f + B; |
| 131 |
|
| 132 |
float sMax = floor((cMax / pow(2.0f, expp - B - N)) + 0.5f); |
| 133 |
|
| 134 |
float exps = expp + 1.0f; |
| 135 |
|
| 136 |
if (0.0 <= sMax && sMax < pow2to9) { |
| 137 |
exps = expp; |
| 138 |
} |
| 139 |
|
| 140 |
float sRed = floor((cRed / pow(2.0f, exps - B - N)) + 0.5f); |
| 141 |
float sGreen = floor((cGreen / pow(2.0f, exps - B - N)) + 0.5f); |
| 142 |
float sBlue = floor((cBlue / pow(2.0f, exps - B - N)) + 0.5f); |
| 143 |
return (uint(sRed) & 0x1FF) | ((uint(sGreen) & 0x1FF) << 9) | ((uint(sBlue) & 0x1FF) << 18) | ((uint(exps) & 0x1F) << 27); |
| 144 |
} |
| 145 |
|
| 146 |
struct SH { |
| 147 |
#if (SH_SIZE == 16) |
| 148 |
float c[48]; |
| 149 |
#else |
| 150 |
float c[28]; |
| 151 |
#endif |
| 152 |
}; |
| 153 |
|
| 154 |
shared SH sh_accum[64]; //8x8 |
| 155 |
|
| 156 |
void main() { |
| 157 |
ivec2 pos = ivec2(gl_GlobalInvocationID.xy); |
| 158 |
if (any(greaterThanEqual(pos, params.image_size))) { //too large, do nothing |
| 159 |
return; |
| 160 |
} |
| 161 |
|
| 162 |
uint probe_index = gl_LocalInvocationID.x + gl_LocalInvocationID.y * 8; |
| 163 |
|
| 164 |
#ifdef MODE_PROCESS |
| 165 |
|
| 166 |
float probe_cell_size = float(params.grid_size.x / float(params.probe_axis_size - 1)) / cascades.data[params.cascade].to_cell; |
| 167 |
|
| 168 |
ivec3 probe_cell; |
| 169 |
probe_cell.x = pos.x % int(params.probe_axis_size); |
| 170 |
probe_cell.y = pos.y; |
| 171 |
probe_cell.z = pos.x / int(params.probe_axis_size); |
| 172 |
|
| 173 |
vec3 probe_pos = cascades.data[params.cascade].offset + vec3(probe_cell) * probe_cell_size; |
| 174 |
vec3 pos_to_uvw = 1.0 / params.grid_size; |
| 175 |
|
| 176 |
for (uint i = 0; i < SH_SIZE * 3; i++) { |
| 177 |
sh_accum[probe_index].c[i] = 0.0; |
| 178 |
} |
| 179 |
|
| 180 |
// quickly ensure each probe has a different "offset" for the vogel function, based on integer world position |
| 181 |
uvec3 h3 = hash3(uvec3(params.world_offset + probe_cell)); |
| 182 |
float offset = hashf3(vec3(h3 & uvec3(0xFFFFF))); |
| 183 |
|
| 184 |
//for a more homogeneous hemisphere, alternate based on history frames |
| 185 |
uint ray_offset = params.history_index; |
| 186 |
uint ray_mult = params.history_size; |
| 187 |
uint ray_total = ray_mult * params.ray_count; |
| 188 |
|
| 189 |
for (uint i = 0; i < params.ray_count; i++) { |
| 190 |
vec3 ray_dir = vogel_hemisphere(ray_offset + i * ray_mult, ray_total, offset); |
| 191 |
ray_dir.y *= params.y_mult; |
| 192 |
ray_dir = normalize(ray_dir); |
| 193 |
|
| 194 |
//needs to be visible |
| 195 |
vec3 ray_pos = probe_pos; |
| 196 |
vec3 inv_dir = 1.0 / ray_dir; |
| 197 |
|
| 198 |
bool hit = false; |
| 199 |
uint hit_cascade; |
| 200 |
|
| 201 |
float bias = params.ray_bias; |
| 202 |
vec3 abs_ray_dir = abs(ray_dir); |
| 203 |
ray_pos += ray_dir * 1.0 / max(abs_ray_dir.x, max(abs_ray_dir.y, abs_ray_dir.z)) * bias / cascades.data[params.cascade].to_cell; |
| 204 |
vec3 uvw; |
| 205 |
|
| 206 |
for (uint j = params.cascade; j < params.max_cascades; j++) { |
| 207 |
//convert to local bounds |
| 208 |
vec3 pos = ray_pos - cascades.data[j].offset; |
| 209 |
pos *= cascades.data[j].to_cell; |
| 210 |
|
| 211 |
if (any(lessThan(pos, vec3(0.0))) || any(greaterThanEqual(pos, params.grid_size))) { |
| 212 |
continue; //already past bounds for this cascade, goto next |
| 213 |
} |
| 214 |
|
| 215 |
//find maximum advance distance (until reaching bounds) |
| 216 |
vec3 t0 = -pos * inv_dir; |
| 217 |
vec3 t1 = (params.grid_size - pos) * inv_dir; |
| 218 |
vec3 tmax = max(t0, t1); |
| 219 |
float max_advance = min(tmax.x, min(tmax.y, tmax.z)); |
| 220 |
|
| 221 |
float advance = 0.0; |
| 222 |
|
| 223 |
while (advance < max_advance) { |
| 224 |
//read how much to advance from SDF |
| 225 |
uvw = (pos + ray_dir * advance) * pos_to_uvw; |
| 226 |
|
| 227 |
float distance = texture(sampler3D(sdf_cascades[j], linear_sampler), uvw).r * 255.0 - 1.0; |
| 228 |
if (distance < 0.05) { |
| 229 |
//consider hit |
| 230 |
hit = true; |
| 231 |
break; |
| 232 |
} |
| 233 |
|
| 234 |
advance += distance; |
| 235 |
} |
| 236 |
|
| 237 |
if (hit) { |
| 238 |
hit_cascade = j; |
| 239 |
break; |
| 240 |
} |
| 241 |
|
| 242 |
//change ray origin to collision with bounds |
| 243 |
pos += ray_dir * max_advance; |
| 244 |
pos /= cascades.data[j].to_cell; |
| 245 |
pos += cascades.data[j].offset; |
| 246 |
ray_pos = pos; |
| 247 |
} |
| 248 |
|
| 249 |
vec4 light; |
| 250 |
if (hit) { |
| 251 |
//avoid reading different texture from different threads |
| 252 |
for (uint j = params.cascade; j < params.max_cascades; j++) { |
| 253 |
if (j == hit_cascade) { |
| 254 |
const float EPSILON = 0.001; |
| 255 |
vec3 hit_normal = normalize(vec3( |
| 256 |
texture(sampler3D(sdf_cascades[hit_cascade], linear_sampler), uvw + vec3(EPSILON, 0.0, 0.0)).r - texture(sampler3D(sdf_cascades[hit_cascade], linear_sampler), uvw - vec3(EPSILON, 0.0, 0.0)).r, |
| 257 |
texture(sampler3D(sdf_cascades[hit_cascade], linear_sampler), uvw + vec3(0.0, EPSILON, 0.0)).r - texture(sampler3D(sdf_cascades[hit_cascade], linear_sampler), uvw - vec3(0.0, EPSILON, 0.0)).r, |
| 258 |
texture(sampler3D(sdf_cascades[hit_cascade], linear_sampler), uvw + vec3(0.0, 0.0, EPSILON)).r - texture(sampler3D(sdf_cascades[hit_cascade], linear_sampler), uvw - vec3(0.0, 0.0, EPSILON)).r)); |
| 259 |
|
| 260 |
vec3 hit_light = texture(sampler3D(light_cascades[hit_cascade], linear_sampler), uvw).rgb; |
| 261 |
vec4 aniso0 = texture(sampler3D(aniso0_cascades[hit_cascade], linear_sampler), uvw); |
| 262 |
vec3 hit_aniso0 = aniso0.rgb; |
| 263 |
vec3 hit_aniso1 = vec3(aniso0.a, texture(sampler3D(aniso1_cascades[hit_cascade], linear_sampler), uvw).rg); |
| 264 |
|
| 265 |
//one liner magic |
| 266 |
light.rgb = hit_light * (dot(max(vec3(0.0), (hit_normal * hit_aniso0)), vec3(1.0)) + dot(max(vec3(0.0), (-hit_normal * hit_aniso1)), vec3(1.0))); |
| 267 |
light.a = 1.0; |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
} else if (bool(params.sky_flags & SKY_FLAGS_MODE_SKY)) { |
| 272 |
// Reconstruct sky orientation as quaternion and rotate ray_dir before sampling. |
| 273 |
float sky_sign = bool(params.sky_flags & SKY_FLAGS_ORIENTATION_SIGN) ? 1.0 : -1.0; |
| 274 |
vec4 sky_quat = vec4(params.sky_color_or_orientation, sky_sign * sqrt(1.0 - dot(params.sky_color_or_orientation, params.sky_color_or_orientation))); |
| 275 |
vec3 sky_dir = cross(sky_quat.xyz, ray_dir); |
| 276 |
sky_dir = ray_dir + ((sky_dir * sky_quat.w) + cross(sky_quat.xyz, sky_dir)) * 2.0; |
| 277 |
#ifdef USE_RADIANCE_OCTMAP_ARRAY |
| 278 |
light.rgb = textureLod(sampler2DArray(sky_irradiance, linear_sampler_mipmaps), vec3(vec3_to_oct_with_border(sky_dir, params.sky_irradiance_border_size), 0.0), 2.0).rgb; // Use second mipmap because we don't usually throw a lot of rays, so this compensates. |
| 279 |
#else |
| 280 |
light.rgb = textureLod(sampler2D(sky_irradiance, linear_sampler_mipmaps), vec3_to_oct_with_border(sky_dir, params.sky_irradiance_border_size), 2.0).rgb; // Use second mipmap because we don't usually throw a lot of rays, so this compensates. |
| 281 |
#endif |
| 282 |
light.rgb *= params.sky_energy; |
| 283 |
light.a = 0.0; |
| 284 |
|
| 285 |
} else if (bool(params.sky_flags & SKY_FLAGS_MODE_COLOR)) { |
| 286 |
light.rgb = params.sky_color_or_orientation; |
| 287 |
light.rgb *= params.sky_energy; |
| 288 |
light.a = 0.0; |
| 289 |
} else { |
| 290 |
light = vec4(0, 0, 0, 0); |
| 291 |
} |
| 292 |
|
| 293 |
vec3 ray_dir2 = ray_dir * ray_dir; |
| 294 |
|
| 295 |
#define SH_ACCUM(m_idx, m_value) \ |
| 296 |
{ \ |
| 297 |
vec3 l = light.rgb * (m_value); \ |
| 298 |
sh_accum[probe_index].c[m_idx * 3 + 0] += l.r; \ |
| 299 |
sh_accum[probe_index].c[m_idx * 3 + 1] += l.g; \ |
| 300 |
sh_accum[probe_index].c[m_idx * 3 + 2] += l.b; \ |
| 301 |
} |
| 302 |
SH_ACCUM(0, 0.282095); //l0 |
| 303 |
SH_ACCUM(1, 0.488603 * ray_dir.y); //l1n1 |
| 304 |
SH_ACCUM(2, 0.488603 * ray_dir.z); //l1n0 |
| 305 |
SH_ACCUM(3, 0.488603 * ray_dir.x); //l1p1 |
| 306 |
SH_ACCUM(4, 1.092548 * ray_dir.x * ray_dir.y); //l2n2 |
| 307 |
SH_ACCUM(5, 1.092548 * ray_dir.y * ray_dir.z); //l2n1 |
| 308 |
SH_ACCUM(6, 0.315392 * (3.0 * ray_dir2.z - 1.0)); //l20 |
| 309 |
SH_ACCUM(7, 1.092548 * ray_dir.x * ray_dir.z); //l2p1 |
| 310 |
SH_ACCUM(8, 0.546274 * (ray_dir2.x - ray_dir2.y)); //l2p2 |
| 311 |
#if (SH_SIZE == 16) |
| 312 |
SH_ACCUM(9, 0.590043 * ray_dir.y * (3.0f * ray_dir2.x - ray_dir2.y)); |
| 313 |
SH_ACCUM(10, 2.890611 * ray_dir.y * ray_dir.x * ray_dir.z); |
| 314 |
SH_ACCUM(11, 0.646360 * ray_dir.y * (-1.0f + 5.0f * ray_dir2.z)); |
| 315 |
SH_ACCUM(12, 0.373176 * (5.0f * ray_dir2.z * ray_dir.z - 3.0f * ray_dir.z)); |
| 316 |
SH_ACCUM(13, 0.457045 * ray_dir.x * (-1.0f + 5.0f * ray_dir2.z)); |
| 317 |
SH_ACCUM(14, 1.445305 * (ray_dir2.x - ray_dir2.y) * ray_dir.z); |
| 318 |
SH_ACCUM(15, 0.590043 * ray_dir.x * (ray_dir2.x - 3.0f * ray_dir2.y)); |
| 319 |
|
| 320 |
#endif |
| 321 |
} |
| 322 |
|
| 323 |
for (uint i = 0; i < SH_SIZE; i++) { |
| 324 |
// store in history texture |
| 325 |
ivec3 prev_pos = ivec3(pos.x, pos.y * SH_SIZE + i, int(params.history_index)); |
| 326 |
ivec2 average_pos = prev_pos.xy; |
| 327 |
|
| 328 |
vec4 value = vec4(sh_accum[probe_index].c[i * 3 + 0], sh_accum[probe_index].c[i * 3 + 1], sh_accum[probe_index].c[i * 3 + 2], 1.0) * 4.0 / float(params.ray_count); |
| 329 |
|
| 330 |
ivec4 ivalue = clamp(ivec4(value * float(1 << HISTORY_BITS)), -32768, 32767); //clamp to 16 bits, so higher values don't break average |
| 331 |
|
| 332 |
ivec4 prev_value = imageLoad(lightprobe_history_texture, prev_pos); |
| 333 |
ivec4 average = imageLoad(lightprobe_average_texture, average_pos); |
| 334 |
|
| 335 |
average -= prev_value; |
| 336 |
average += ivalue; |
| 337 |
|
| 338 |
imageStore(lightprobe_history_texture, prev_pos, ivalue); |
| 339 |
imageStore(lightprobe_average_texture, average_pos, average); |
| 340 |
|
| 341 |
if (params.store_ambient_texture && i == 0) { |
| 342 |
ivec3 ambient_pos = ivec3(pos, int(params.cascade)); |
| 343 |
vec4 ambient_light = (vec4(average) / float(params.history_size)) / float(1 << HISTORY_BITS); |
| 344 |
ambient_light *= 0.88622; // SHL0 |
| 345 |
imageStore(lightprobe_ambient_texture, ambient_pos, ambient_light); |
| 346 |
} |
| 347 |
} |
| 348 |
#endif // MODE PROCESS |
| 349 |
|
| 350 |
#ifdef MODE_STORE |
| 351 |
|
| 352 |
// converting to octahedral in this step is required because |
| 353 |
// octahedral is much faster to read from the screen than spherical harmonics, |
| 354 |
// despite the very slight quality loss |
| 355 |
|
| 356 |
ivec2 sh_pos = (pos / OCT_SIZE) * ivec2(1, SH_SIZE); |
| 357 |
ivec2 oct_pos = (pos / OCT_SIZE) * (OCT_SIZE + 2) + ivec2(1); |
| 358 |
ivec2 local_pos = pos % OCT_SIZE; |
| 359 |
|
| 360 |
//compute the octahedral normal for this texel |
| 361 |
vec3 normal = octahedron_encode(vec2(local_pos) / float(OCT_SIZE)); |
| 362 |
|
| 363 |
// read the spherical harmonic |
| 364 |
|
| 365 |
vec3 normal2 = normal * normal; |
| 366 |
float c[SH_SIZE] = float[]( |
| 367 |
|
| 368 |
0.282095, //l0 |
| 369 |
0.488603 * normal.y, //l1n1 |
| 370 |
0.488603 * normal.z, //l1n0 |
| 371 |
0.488603 * normal.x, //l1p1 |
| 372 |
1.092548 * normal.x * normal.y, //l2n2 |
| 373 |
1.092548 * normal.y * normal.z, //l2n1 |
| 374 |
0.315392 * (3.0 * normal2.z - 1.0), //l20 |
| 375 |
1.092548 * normal.x * normal.z, //l2p1 |
| 376 |
0.546274 * (normal2.x - normal2.y) //l2p2 |
| 377 |
#if (SH_SIZE == 16) |
| 378 |
, |
| 379 |
0.590043 * normal.y * (3.0f * normal2.x - normal2.y), |
| 380 |
2.890611 * normal.y * normal.x * normal.z, |
| 381 |
0.646360 * normal.y * (-1.0f + 5.0f * normal2.z), |
| 382 |
0.373176 * (5.0f * normal2.z * normal.z - 3.0f * normal.z), |
| 383 |
0.457045 * normal.x * (-1.0f + 5.0f * normal2.z), |
| 384 |
1.445305 * (normal2.x - normal2.y) * normal.z, |
| 385 |
0.590043 * normal.x * (normal2.x - 3.0f * normal2.y) |
| 386 |
|
| 387 |
#endif |
| 388 |
); |
| 389 |
|
| 390 |
const float l_mult[SH_SIZE] = float[]( |
| 391 |
1.0, |
| 392 |
2.0 / 3.0, |
| 393 |
2.0 / 3.0, |
| 394 |
2.0 / 3.0, |
| 395 |
1.0 / 4.0, |
| 396 |
1.0 / 4.0, |
| 397 |
1.0 / 4.0, |
| 398 |
1.0 / 4.0, |
| 399 |
1.0 / 4.0 |
| 400 |
#if (SH_SIZE == 16) |
| 401 |
, // l4 does not contribute to irradiance |
| 402 |
0.0, |
| 403 |
0.0, |
| 404 |
0.0, |
| 405 |
0.0, |
| 406 |
0.0, |
| 407 |
0.0, |
| 408 |
0.0 |
| 409 |
#endif |
| 410 |
); |
| 411 |
|
| 412 |
vec3 irradiance = vec3(0.0); |
| 413 |
vec3 radiance = vec3(0.0); |
| 414 |
|
| 415 |
for (uint i = 0; i < SH_SIZE; i++) { |
| 416 |
// store in history texture |
| 417 |
ivec2 average_pos = sh_pos + ivec2(0, i); |
| 418 |
ivec4 average = imageLoad(lightprobe_average_texture, average_pos); |
| 419 |
|
| 420 |
vec4 sh = (vec4(average) / float(params.history_size)) / float(1 << HISTORY_BITS); |
| 421 |
|
| 422 |
vec3 m = sh.rgb * c[i] * 4.0; |
| 423 |
|
| 424 |
irradiance += m * l_mult[i]; |
| 425 |
radiance += m; |
| 426 |
} |
| 427 |
|
| 428 |
//encode RGBE9995 for the final texture |
| 429 |
|
| 430 |
uint irradiance_rgbe = rgbe_encode(irradiance); |
| 431 |
uint radiance_rgbe = rgbe_encode(radiance); |
| 432 |
|
| 433 |
//store in octahedral map |
| 434 |
|
| 435 |
ivec3 texture_pos = ivec3(oct_pos, int(params.cascade)); |
| 436 |
ivec3 copy_to[4] = ivec3[](ivec3(-2, -2, -2), ivec3(-2, -2, -2), ivec3(-2, -2, -2), ivec3(-2, -2, -2)); |
| 437 |
copy_to[0] = texture_pos + ivec3(local_pos, 0); |
| 438 |
|
| 439 |
if (local_pos == ivec2(0, 0)) { |
| 440 |
copy_to[1] = texture_pos + ivec3(OCT_SIZE - 1, -1, 0); |
| 441 |
copy_to[2] = texture_pos + ivec3(-1, OCT_SIZE - 1, 0); |
| 442 |
copy_to[3] = texture_pos + ivec3(OCT_SIZE, OCT_SIZE, 0); |
| 443 |
} else if (local_pos == ivec2(OCT_SIZE - 1, 0)) { |
| 444 |
copy_to[1] = texture_pos + ivec3(0, -1, 0); |
| 445 |
copy_to[2] = texture_pos + ivec3(OCT_SIZE, OCT_SIZE - 1, 0); |
| 446 |
copy_to[3] = texture_pos + ivec3(-1, OCT_SIZE, 0); |
| 447 |
} else if (local_pos == ivec2(0, OCT_SIZE - 1)) { |
| 448 |
copy_to[1] = texture_pos + ivec3(-1, 0, 0); |
| 449 |
copy_to[2] = texture_pos + ivec3(OCT_SIZE - 1, OCT_SIZE, 0); |
| 450 |
copy_to[3] = texture_pos + ivec3(OCT_SIZE, -1, 0); |
| 451 |
} else if (local_pos == ivec2(OCT_SIZE - 1, OCT_SIZE - 1)) { |
| 452 |
copy_to[1] = texture_pos + ivec3(0, OCT_SIZE, 0); |
| 453 |
copy_to[2] = texture_pos + ivec3(OCT_SIZE, 0, 0); |
| 454 |
copy_to[3] = texture_pos + ivec3(-1, -1, 0); |
| 455 |
} else if (local_pos.y == 0) { |
| 456 |
copy_to[1] = texture_pos + ivec3(OCT_SIZE - local_pos.x - 1, local_pos.y - 1, 0); |
| 457 |
} else if (local_pos.x == 0) { |
| 458 |
copy_to[1] = texture_pos + ivec3(local_pos.x - 1, OCT_SIZE - local_pos.y - 1, 0); |
| 459 |
} else if (local_pos.y == OCT_SIZE - 1) { |
| 460 |
copy_to[1] = texture_pos + ivec3(OCT_SIZE - local_pos.x - 1, local_pos.y + 1, 0); |
| 461 |
} else if (local_pos.x == OCT_SIZE - 1) { |
| 462 |
copy_to[1] = texture_pos + ivec3(local_pos.x + 1, OCT_SIZE - local_pos.y - 1, 0); |
| 463 |
} |
| 464 |
|
| 465 |
for (int i = 0; i < 4; i++) { |
| 466 |
if (copy_to[i] == ivec3(-2, -2, -2)) { |
| 467 |
continue; |
| 468 |
} |
| 469 |
imageStore(lightprobe_texture_data, copy_to[i], uvec4(irradiance_rgbe)); |
| 470 |
imageStore(lightprobe_texture_data, copy_to[i] + ivec3(0, 0, int(params.max_cascades)), uvec4(radiance_rgbe)); |
| 471 |
} |
| 472 |
|
| 473 |
#endif |
| 474 |
|
| 475 |
#ifdef MODE_SCROLL |
| 476 |
|
| 477 |
ivec3 probe_cell; |
| 478 |
probe_cell.x = pos.x % int(params.probe_axis_size); |
| 479 |
probe_cell.y = pos.y; |
| 480 |
probe_cell.z = pos.x / int(params.probe_axis_size); |
| 481 |
|
| 482 |
ivec3 read_probe = probe_cell - params.scroll; |
| 483 |
|
| 484 |
if (all(greaterThanEqual(read_probe, ivec3(0))) && all(lessThan(read_probe, ivec3(params.probe_axis_size)))) { |
| 485 |
// can scroll |
| 486 |
ivec2 tex_pos; |
| 487 |
tex_pos = read_probe.xy; |
| 488 |
tex_pos.x += read_probe.z * int(params.probe_axis_size); |
| 489 |
|
| 490 |
//scroll |
| 491 |
for (uint j = 0; j < params.history_size; j++) { |
| 492 |
for (int i = 0; i < SH_SIZE; i++) { |
| 493 |
// copy from history texture |
| 494 |
ivec3 src_pos = ivec3(tex_pos.x, tex_pos.y * SH_SIZE + i, int(j)); |
| 495 |
ivec3 dst_pos = ivec3(pos.x, pos.y * SH_SIZE + i, int(j)); |
| 496 |
ivec4 value = imageLoad(lightprobe_history_texture, src_pos); |
| 497 |
imageStore(lightprobe_history_scroll_texture, dst_pos, value); |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
for (int i = 0; i < SH_SIZE; i++) { |
| 502 |
// copy from average texture |
| 503 |
ivec2 src_pos = ivec2(tex_pos.x, tex_pos.y * SH_SIZE + i); |
| 504 |
ivec2 dst_pos = ivec2(pos.x, pos.y * SH_SIZE + i); |
| 505 |
ivec4 value = imageLoad(lightprobe_average_texture, src_pos); |
| 506 |
imageStore(lightprobe_average_scroll_texture, dst_pos, value); |
| 507 |
} |
| 508 |
} else if (params.cascade < params.max_cascades - 1) { |
| 509 |
//can't scroll, must look for position in parent cascade |
| 510 |
|
| 511 |
//to global coords |
| 512 |
float cell_to_probe = float(params.grid_size.x / float(params.probe_axis_size - 1)); |
| 513 |
|
| 514 |
float probe_cell_size = cell_to_probe / cascades.data[params.cascade].to_cell; |
| 515 |
vec3 probe_pos = cascades.data[params.cascade].offset + vec3(probe_cell) * probe_cell_size; |
| 516 |
|
| 517 |
//to parent local coords |
| 518 |
float probe_cell_size_next = cell_to_probe / cascades.data[params.cascade + 1].to_cell; |
| 519 |
probe_pos -= cascades.data[params.cascade + 1].offset; |
| 520 |
probe_pos /= probe_cell_size_next; |
| 521 |
|
| 522 |
ivec3 probe_posi = ivec3(probe_pos); |
| 523 |
//add up all light, no need to use occlusion here, since occlusion will do its work afterwards |
| 524 |
|
| 525 |
vec4 average_light[SH_SIZE] = vec4[](vec4(0), vec4(0), vec4(0), vec4(0), vec4(0), vec4(0), vec4(0), vec4(0), vec4(0) |
| 526 |
#if (SH_SIZE == 16) |
| 527 |
, |
| 528 |
vec4(0), vec4(0), vec4(0), vec4(0), vec4(0), vec4(0), vec4(0) |
| 529 |
#endif |
| 530 |
); |
| 531 |
float total_weight = 0.0; |
| 532 |
|
| 533 |
for (int i = 0; i < 8; i++) { |
| 534 |
ivec3 offset = probe_posi + ((ivec3(i) >> ivec3(0, 1, 2)) & ivec3(1, 1, 1)); |
| 535 |
|
| 536 |
vec3 trilinear = vec3(1.0) - abs(probe_pos - vec3(offset)); |
| 537 |
float weight = trilinear.x * trilinear.y * trilinear.z; |
| 538 |
|
| 539 |
ivec2 tex_pos; |
| 540 |
tex_pos = offset.xy; |
| 541 |
tex_pos.x += offset.z * int(params.probe_axis_size); |
| 542 |
|
| 543 |
for (int j = 0; j < SH_SIZE; j++) { |
| 544 |
// copy from history texture |
| 545 |
ivec2 src_pos = ivec2(tex_pos.x, tex_pos.y * SH_SIZE + j); |
| 546 |
ivec4 average = imageLoad(lightprobe_average_parent_texture, src_pos); |
| 547 |
vec4 value = (vec4(average) / float(params.history_size)) / float(1 << HISTORY_BITS); |
| 548 |
average_light[j] += value * weight; |
| 549 |
} |
| 550 |
|
| 551 |
total_weight += weight; |
| 552 |
} |
| 553 |
|
| 554 |
if (total_weight > 0.0) { |
| 555 |
total_weight = 1.0 / total_weight; |
| 556 |
} |
| 557 |
//store the averaged values everywhere |
| 558 |
|
| 559 |
for (int i = 0; i < SH_SIZE; i++) { |
| 560 |
ivec4 ivalue = clamp(ivec4(average_light[i] * total_weight * float(1 << HISTORY_BITS)), ivec4(-32768), ivec4(32767)); //clamp to 16 bits, so higher values don't break average |
| 561 |
// copy from history texture |
| 562 |
ivec3 dst_pos = ivec3(pos.x, pos.y * SH_SIZE + i, 0); |
| 563 |
for (uint j = 0; j < params.history_size; j++) { |
| 564 |
dst_pos.z = int(j); |
| 565 |
imageStore(lightprobe_history_scroll_texture, dst_pos, ivalue); |
| 566 |
} |
| 567 |
|
| 568 |
ivalue *= int(params.history_size); //average needs to have all history added up |
| 569 |
imageStore(lightprobe_average_scroll_texture, dst_pos.xy, ivalue); |
| 570 |
} |
| 571 |
|
| 572 |
} else { |
| 573 |
//scroll at the edge of the highest cascade, just copy what is there, |
| 574 |
//since its the closest we have anyway |
| 575 |
|
| 576 |
for (uint j = 0; j < params.history_size; j++) { |
| 577 |
ivec2 tex_pos; |
| 578 |
tex_pos = probe_cell.xy; |
| 579 |
tex_pos.x += probe_cell.z * int(params.probe_axis_size); |
| 580 |
|
| 581 |
for (int i = 0; i < SH_SIZE; i++) { |
| 582 |
// copy from history texture |
| 583 |
ivec3 src_pos = ivec3(tex_pos.x, tex_pos.y * SH_SIZE + i, int(j)); |
| 584 |
ivec3 dst_pos = ivec3(pos.x, pos.y * SH_SIZE + i, int(j)); |
| 585 |
ivec4 value = imageLoad(lightprobe_history_texture, dst_pos); |
| 586 |
imageStore(lightprobe_history_scroll_texture, dst_pos, value); |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
for (int i = 0; i < SH_SIZE; i++) { |
| 591 |
// copy from average texture |
| 592 |
ivec2 spos = ivec2(pos.x, pos.y * SH_SIZE + i); |
| 593 |
ivec4 average = imageLoad(lightprobe_average_texture, spos); |
| 594 |
imageStore(lightprobe_average_scroll_texture, spos, average); |
| 595 |
} |
| 596 |
} |
| 597 |
|
| 598 |
#endif |
| 599 |
|
| 600 |
#ifdef MODE_SCROLL_STORE |
| 601 |
|
| 602 |
//do not update probe texture, as these will be updated later |
| 603 |
|
| 604 |
for (uint j = 0; j < params.history_size; j++) { |
| 605 |
for (int i = 0; i < SH_SIZE; i++) { |
| 606 |
// copy from history texture |
| 607 |
ivec3 spos = ivec3(pos.x, pos.y * SH_SIZE + i, int(j)); |
| 608 |
ivec4 value = imageLoad(lightprobe_history_scroll_texture, spos); |
| 609 |
imageStore(lightprobe_history_texture, spos, value); |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
for (int i = 0; i < SH_SIZE; i++) { |
| 614 |
// copy from average texture |
| 615 |
ivec2 spos = ivec2(pos.x, pos.y * SH_SIZE + i); |
| 616 |
ivec4 average = imageLoad(lightprobe_average_scroll_texture, spos); |
| 617 |
imageStore(lightprobe_average_texture, spos, average); |
| 618 |
} |
| 619 |
|
| 620 |
#endif |
| 621 |
} |