| 1 |
#[compute] |
| 2 |
|
| 3 |
#version 450 |
| 4 |
|
| 5 |
#VERSION_DEFINES |
| 6 |
|
| 7 |
#ifdef MODE_JUMPFLOOD_OPTIMIZED |
| 8 |
#define GROUP_SIZE 8 |
| 9 |
|
| 10 |
layout(local_size_x = GROUP_SIZE, local_size_y = GROUP_SIZE, local_size_z = GROUP_SIZE) in; |
| 11 |
|
| 12 |
#elif defined(MODE_OCCLUSION) || defined(MODE_SCROLL) |
| 13 |
//buffer layout |
| 14 |
layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in; |
| 15 |
|
| 16 |
#else |
| 17 |
//grid layout |
| 18 |
layout(local_size_x = 4, local_size_y = 4, local_size_z = 4) in; |
| 19 |
|
| 20 |
#endif |
| 21 |
|
| 22 |
#if defined(MODE_INITIALIZE_JUMP_FLOOD) || defined(MODE_INITIALIZE_JUMP_FLOOD_HALF) |
| 23 |
layout(r16ui, set = 0, binding = 1) uniform restrict readonly uimage3D src_color; |
| 24 |
layout(rgba8ui, set = 0, binding = 2) uniform restrict writeonly uimage3D dst_positions; |
| 25 |
#endif |
| 26 |
|
| 27 |
#ifdef MODE_UPSCALE_JUMP_FLOOD |
| 28 |
layout(r16ui, set = 0, binding = 1) uniform restrict readonly uimage3D src_color; |
| 29 |
layout(rgba8ui, set = 0, binding = 2) uniform restrict readonly uimage3D src_positions_half; |
| 30 |
layout(rgba8ui, set = 0, binding = 3) uniform restrict writeonly uimage3D dst_positions; |
| 31 |
#endif |
| 32 |
|
| 33 |
#if defined(MODE_JUMPFLOOD) || defined(MODE_JUMPFLOOD_OPTIMIZED) |
| 34 |
layout(rgba8ui, set = 0, binding = 1) uniform restrict readonly uimage3D src_positions; |
| 35 |
layout(rgba8ui, set = 0, binding = 2) uniform restrict writeonly uimage3D dst_positions; |
| 36 |
#endif |
| 37 |
|
| 38 |
#ifdef MODE_JUMPFLOOD_OPTIMIZED |
| 39 |
|
| 40 |
shared uvec4 group_positions[(GROUP_SIZE + 2) * (GROUP_SIZE + 2) * (GROUP_SIZE + 2)]; //4x4x4 with margins |
| 41 |
|
| 42 |
void group_store(ivec3 p_pos, uvec4 p_value) { |
| 43 |
uint offset = uint(p_pos.z * (GROUP_SIZE + 2) * (GROUP_SIZE + 2) + p_pos.y * (GROUP_SIZE + 2) + p_pos.x); |
| 44 |
group_positions[offset] = p_value; |
| 45 |
} |
| 46 |
|
| 47 |
uvec4 group_load(ivec3 p_pos) { |
| 48 |
uint offset = uint(p_pos.z * (GROUP_SIZE + 2) * (GROUP_SIZE + 2) + p_pos.y * (GROUP_SIZE + 2) + p_pos.x); |
| 49 |
return group_positions[offset]; |
| 50 |
} |
| 51 |
|
| 52 |
#endif |
| 53 |
|
| 54 |
#ifdef MODE_OCCLUSION |
| 55 |
|
| 56 |
layout(r16ui, set = 0, binding = 1) uniform restrict readonly uimage3D src_color; |
| 57 |
layout(r8, set = 0, binding = 2) uniform restrict image3D dst_occlusion[8]; |
| 58 |
layout(r32ui, set = 0, binding = 3) uniform restrict readonly uimage3D src_facing; |
| 59 |
|
| 60 |
const uvec2 group_size_offset[11] = uvec2[](uvec2(1, 0), uvec2(3, 1), uvec2(6, 4), uvec2(10, 10), uvec2(15, 20), uvec2(21, 35), uvec2(28, 56), uvec2(36, 84), uvec2(42, 120), uvec2(46, 162), uvec2(48, 208)); |
| 61 |
const uint group_pos[256] = uint[](0, |
| 62 |
65536, 256, 1, |
| 63 |
131072, 65792, 512, 65537, 257, 2, |
| 64 |
196608, 131328, 66048, 768, 131073, 65793, 513, 65538, 258, 3, |
| 65 |
262144, 196864, 131584, 66304, 1024, 196609, 131329, 66049, 769, 131074, 65794, 514, 65539, 259, 4, |
| 66 |
327680, 262400, 197120, 131840, 66560, 1280, 262145, 196865, 131585, 66305, 1025, 196610, 131330, 66050, 770, 131075, 65795, 515, 65540, 260, 5, |
| 67 |
393216, 327936, 262656, 197376, 132096, 66816, 1536, 327681, 262401, 197121, 131841, 66561, 1281, 262146, 196866, 131586, 66306, 1026, 196611, 131331, 66051, 771, 131076, 65796, 516, 65541, 261, 6, |
| 68 |
458752, 393472, 328192, 262912, 197632, 132352, 67072, 1792, 393217, 327937, 262657, 197377, 132097, 66817, 1537, 327682, 262402, 197122, 131842, 66562, 1282, 262147, 196867, 131587, 66307, 1027, 196612, 131332, 66052, 772, 131077, 65797, 517, 65542, 262, 7, |
| 69 |
459008, 393728, 328448, 263168, 197888, 132608, 67328, 458753, 393473, 328193, 262913, 197633, 132353, 67073, 1793, 393218, 327938, 262658, 197378, 132098, 66818, 1538, 327683, 262403, 197123, 131843, 66563, 1283, 262148, 196868, 131588, 66308, 1028, 196613, 131333, 66053, 773, 131078, 65798, 518, 65543, 263, |
| 70 |
459264, 393984, 328704, 263424, 198144, 132864, 459009, 393729, 328449, 263169, 197889, 132609, 67329, 458754, 393474, 328194, 262914, 197634, 132354, 67074, 1794, 393219, 327939, 262659, 197379, 132099, 66819, 1539, 327684, 262404, 197124, 131844, 66564, 1284, 262149, 196869, 131589, 66309, 1029, 196614, 131334, 66054, 774, 131079, 65799, 519, |
| 71 |
459520, 394240, 328960, 263680, 198400, 459265, 393985, 328705, 263425, 198145, 132865, 459010, 393730, 328450, 263170, 197890, 132610, 67330, 458755, 393475, 328195, 262915, 197635, 132355, 67075, 1795, 393220, 327940, 262660, 197380, 132100, 66820, 1540, 327685, 262405, 197125, 131845, 66565, 1285, 262150, 196870, 131590, 66310, 1030, 196615, 131335, 66055, 775); |
| 72 |
|
| 73 |
shared uint occlusion_facing[((OCCLUSION_SIZE * 2) * (OCCLUSION_SIZE * 2) * (OCCLUSION_SIZE * 2)) / 4]; |
| 74 |
|
| 75 |
uint get_facing(ivec3 p_pos) { |
| 76 |
uint ofs = uint(p_pos.z * OCCLUSION_SIZE * 2 * OCCLUSION_SIZE * 2 + p_pos.y * OCCLUSION_SIZE * 2 + p_pos.x); |
| 77 |
uint v = occlusion_facing[ofs / 4]; |
| 78 |
return (v >> ((ofs % 4) * 8)) & 0xFF; |
| 79 |
} |
| 80 |
|
| 81 |
#endif |
| 82 |
|
| 83 |
#ifdef MODE_STORE |
| 84 |
|
| 85 |
layout(rgba8ui, set = 0, binding = 1) uniform restrict readonly uimage3D src_positions; |
| 86 |
layout(r16ui, set = 0, binding = 2) uniform restrict readonly uimage3D src_albedo; |
| 87 |
layout(r8, set = 0, binding = 3) uniform restrict readonly image3D src_occlusion[8]; |
| 88 |
layout(r32ui, set = 0, binding = 4) uniform restrict readonly uimage3D src_light; |
| 89 |
layout(r32ui, set = 0, binding = 5) uniform restrict readonly uimage3D src_light_aniso; |
| 90 |
layout(r32ui, set = 0, binding = 6) uniform restrict readonly uimage3D src_facing; |
| 91 |
|
| 92 |
layout(r8, set = 0, binding = 7) uniform restrict writeonly image3D dst_sdf; |
| 93 |
layout(r16ui, set = 0, binding = 8) uniform restrict writeonly uimage3D dst_occlusion; |
| 94 |
|
| 95 |
layout(set = 0, binding = 10, std430) restrict buffer DispatchData { |
| 96 |
uint x; |
| 97 |
uint y; |
| 98 |
uint z; |
| 99 |
uint total_count; |
| 100 |
} |
| 101 |
dispatch_data; |
| 102 |
|
| 103 |
struct ProcessVoxel { |
| 104 |
uint position; // xyz 7 bit packed, extra 11 bits for neighbors. |
| 105 |
uint albedo; //rgb bits 0-15 albedo, bits 16-21 are normal bits (set if geometry exists toward that side), extra 11 bits for neighbors |
| 106 |
uint light; //rgbe8985 encoded total saved light, extra 2 bits for neighbors |
| 107 |
uint light_aniso; //55555 light anisotropy, extra 2 bits for neighbors |
| 108 |
//total neighbors: 26 |
| 109 |
}; |
| 110 |
|
| 111 |
layout(set = 0, binding = 11, std430) restrict buffer writeonly ProcessVoxels { |
| 112 |
ProcessVoxel data[]; |
| 113 |
} |
| 114 |
dst_process_voxels; |
| 115 |
|
| 116 |
shared ProcessVoxel store_positions[4 * 4 * 4]; |
| 117 |
shared uint store_position_count; |
| 118 |
shared uint store_from_index; |
| 119 |
#endif |
| 120 |
|
| 121 |
#ifdef MODE_SCROLL |
| 122 |
|
| 123 |
layout(r16ui, set = 0, binding = 1) uniform restrict writeonly uimage3D dst_albedo; |
| 124 |
layout(r32ui, set = 0, binding = 2) uniform restrict writeonly uimage3D dst_facing; |
| 125 |
layout(r32ui, set = 0, binding = 3) uniform restrict writeonly uimage3D dst_light; |
| 126 |
layout(r32ui, set = 0, binding = 4) uniform restrict writeonly uimage3D dst_light_aniso; |
| 127 |
|
| 128 |
layout(set = 0, binding = 5, std430) restrict buffer readonly DispatchData { |
| 129 |
uint x; |
| 130 |
uint y; |
| 131 |
uint z; |
| 132 |
uint total_count; |
| 133 |
} |
| 134 |
dispatch_data; |
| 135 |
|
| 136 |
struct ProcessVoxel { |
| 137 |
uint position; // xyz 7 bit packed, extra 11 bits for neighbors. |
| 138 |
uint albedo; //rgb bits 0-15 albedo, bits 16-21 are normal bits (set if geometry exists toward that side), extra 11 bits for neighbors |
| 139 |
uint light; //rgbe8985 encoded total saved light, extra 2 bits for neighbors |
| 140 |
uint light_aniso; //55555 light anisotropy, extra 2 bits for neighbors |
| 141 |
//total neighbors: 26 |
| 142 |
}; |
| 143 |
|
| 144 |
layout(set = 0, binding = 6, std430) restrict buffer readonly ProcessVoxels { |
| 145 |
ProcessVoxel data[]; |
| 146 |
} |
| 147 |
src_process_voxels; |
| 148 |
|
| 149 |
#endif |
| 150 |
|
| 151 |
#ifdef MODE_SCROLL_OCCLUSION |
| 152 |
|
| 153 |
layout(r8, set = 0, binding = 1) uniform restrict image3D dst_occlusion[8]; |
| 154 |
layout(r16ui, set = 0, binding = 2) uniform restrict readonly uimage3D src_occlusion; |
| 155 |
|
| 156 |
#endif |
| 157 |
|
| 158 |
layout(push_constant, std430) uniform Params { |
| 159 |
ivec3 scroll; |
| 160 |
|
| 161 |
int grid_size; |
| 162 |
|
| 163 |
ivec3 probe_offset; |
| 164 |
int step_size; |
| 165 |
|
| 166 |
bool half_size; |
| 167 |
uint occlusion_index; |
| 168 |
int cascade; |
| 169 |
uint pad; |
| 170 |
} |
| 171 |
params; |
| 172 |
|
| 173 |
void main() { |
| 174 |
#ifdef MODE_SCROLL |
| 175 |
|
| 176 |
// Pixel being shaded |
| 177 |
int index = int(gl_GlobalInvocationID.x); |
| 178 |
if (index >= dispatch_data.total_count) { //too big |
| 179 |
return; |
| 180 |
} |
| 181 |
|
| 182 |
ivec3 read_pos = (ivec3(src_process_voxels.data[index].position) >> ivec3(0, 7, 14)) & ivec3(0x7F); |
| 183 |
ivec3 write_pos = read_pos + params.scroll; |
| 184 |
|
| 185 |
if (any(lessThan(write_pos, ivec3(0))) || any(greaterThanEqual(write_pos, ivec3(params.grid_size)))) { |
| 186 |
return; // Fits outside the 3D texture, don't do anything. |
| 187 |
} |
| 188 |
|
| 189 |
uint albedo = ((src_process_voxels.data[index].albedo & 0x7FFF) << 1) | 1; //add solid bit |
| 190 |
imageStore(dst_albedo, write_pos, uvec4(albedo)); |
| 191 |
|
| 192 |
uint facing = (src_process_voxels.data[index].albedo >> 15) & 0x3F; //6 anisotropic facing bits |
| 193 |
imageStore(dst_facing, write_pos, uvec4(facing)); |
| 194 |
|
| 195 |
uint light = src_process_voxels.data[index].light & 0x3fffffff; //30 bits of RGBE8985 |
| 196 |
imageStore(dst_light, write_pos, uvec4(light)); |
| 197 |
|
| 198 |
uint light_aniso = src_process_voxels.data[index].light_aniso & 0x3fffffff; //30 bits of 6 anisotropic 5 bits values |
| 199 |
imageStore(dst_light_aniso, write_pos, uvec4(light_aniso)); |
| 200 |
|
| 201 |
#endif |
| 202 |
|
| 203 |
#ifdef MODE_SCROLL_OCCLUSION |
| 204 |
|
| 205 |
ivec3 pos = ivec3(gl_GlobalInvocationID.xyz); |
| 206 |
if (any(greaterThanEqual(pos, ivec3(params.grid_size) - abs(params.scroll)))) { //too large, do nothing |
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
ivec3 read_pos = pos + max(ivec3(0), -params.scroll); |
| 211 |
ivec3 write_pos = pos + max(ivec3(0), params.scroll); |
| 212 |
|
| 213 |
read_pos.z += params.cascade * params.grid_size; |
| 214 |
uint occlusion = imageLoad(src_occlusion, read_pos).r; |
| 215 |
read_pos.x += params.grid_size; |
| 216 |
occlusion |= imageLoad(src_occlusion, read_pos).r << 16; |
| 217 |
|
| 218 |
const uint occlusion_shift[8] = uint[](12, 8, 4, 0, 28, 24, 20, 16); |
| 219 |
|
| 220 |
for (uint i = 0; i < 8; i++) { |
| 221 |
float o = float((occlusion >> occlusion_shift[i]) & 0xF) / 15.0; |
| 222 |
imageStore(dst_occlusion[i], write_pos, vec4(o)); |
| 223 |
} |
| 224 |
|
| 225 |
#endif |
| 226 |
|
| 227 |
#ifdef MODE_INITIALIZE_JUMP_FLOOD |
| 228 |
|
| 229 |
ivec3 pos = ivec3(gl_GlobalInvocationID.xyz); |
| 230 |
|
| 231 |
uint c = imageLoad(src_color, pos).r; |
| 232 |
uvec4 v; |
| 233 |
if (bool(c & 0x1)) { |
| 234 |
//bit set means this is solid |
| 235 |
v.xyz = uvec3(pos); |
| 236 |
v.w = 255; //not zero means used |
| 237 |
} else { |
| 238 |
v.xyz = uvec3(0); |
| 239 |
v.w = 0; // zero means unused |
| 240 |
} |
| 241 |
|
| 242 |
imageStore(dst_positions, pos, v); |
| 243 |
#endif |
| 244 |
|
| 245 |
#ifdef MODE_INITIALIZE_JUMP_FLOOD_HALF |
| 246 |
|
| 247 |
ivec3 pos = ivec3(gl_GlobalInvocationID.xyz); |
| 248 |
ivec3 base_pos = pos * 2; |
| 249 |
|
| 250 |
//since we store in half size, lets kind of randomize what we store, so |
| 251 |
//the half size jump flood has a bit better chance to find something |
| 252 |
uvec4 closest[8]; |
| 253 |
int closest_count = 0; |
| 254 |
|
| 255 |
for (uint i = 0; i < 8; i++) { |
| 256 |
ivec3 src_pos = base_pos + ((ivec3(i) >> ivec3(0, 1, 2)) & ivec3(1, 1, 1)); |
| 257 |
uint c = imageLoad(src_color, src_pos).r; |
| 258 |
if (bool(c & 1)) { |
| 259 |
uvec4 v = uvec4(uvec3(src_pos), 255); |
| 260 |
closest[closest_count] = v; |
| 261 |
closest_count++; |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
if (closest_count == 0) { |
| 266 |
imageStore(dst_positions, pos, uvec4(0)); |
| 267 |
} else { |
| 268 |
ivec3 indexv = (pos & ivec3(1, 1, 1)) * ivec3(1, 2, 4); |
| 269 |
int index = (indexv.x | indexv.y | indexv.z) % closest_count; |
| 270 |
imageStore(dst_positions, pos, closest[index]); |
| 271 |
} |
| 272 |
|
| 273 |
#endif |
| 274 |
|
| 275 |
#ifdef MODE_JUMPFLOOD |
| 276 |
|
| 277 |
//regular jumpflood, efficient for large steps, inefficient for small steps |
| 278 |
ivec3 pos = ivec3(gl_GlobalInvocationID.xyz); |
| 279 |
|
| 280 |
vec3 posf = vec3(pos); |
| 281 |
|
| 282 |
if (params.half_size) { |
| 283 |
posf = posf * 2.0 + 0.5; |
| 284 |
} |
| 285 |
|
| 286 |
uvec4 p = imageLoad(src_positions, pos); |
| 287 |
|
| 288 |
if (!params.half_size && p == uvec4(uvec3(pos), 255)) { |
| 289 |
imageStore(dst_positions, pos, p); |
| 290 |
return; //points to itself and valid, nothing better can be done, just pass |
| 291 |
} |
| 292 |
|
| 293 |
float p_dist; |
| 294 |
|
| 295 |
if (p.w != 0) { |
| 296 |
p_dist = distance(posf, vec3(p.xyz)); |
| 297 |
} else { |
| 298 |
p_dist = 0.0; //should not matter |
| 299 |
} |
| 300 |
|
| 301 |
const uint offset_count = 26; |
| 302 |
const ivec3 offsets[offset_count] = ivec3[]( |
| 303 |
ivec3(-1, -1, -1), |
| 304 |
ivec3(-1, -1, 0), |
| 305 |
ivec3(-1, -1, 1), |
| 306 |
ivec3(-1, 0, -1), |
| 307 |
ivec3(-1, 0, 0), |
| 308 |
ivec3(-1, 0, 1), |
| 309 |
ivec3(-1, 1, -1), |
| 310 |
ivec3(-1, 1, 0), |
| 311 |
ivec3(-1, 1, 1), |
| 312 |
ivec3(0, -1, -1), |
| 313 |
ivec3(0, -1, 0), |
| 314 |
ivec3(0, -1, 1), |
| 315 |
ivec3(0, 0, -1), |
| 316 |
ivec3(0, 0, 1), |
| 317 |
ivec3(0, 1, -1), |
| 318 |
ivec3(0, 1, 0), |
| 319 |
ivec3(0, 1, 1), |
| 320 |
ivec3(1, -1, -1), |
| 321 |
ivec3(1, -1, 0), |
| 322 |
ivec3(1, -1, 1), |
| 323 |
ivec3(1, 0, -1), |
| 324 |
ivec3(1, 0, 0), |
| 325 |
ivec3(1, 0, 1), |
| 326 |
ivec3(1, 1, -1), |
| 327 |
ivec3(1, 1, 0), |
| 328 |
ivec3(1, 1, 1)); |
| 329 |
|
| 330 |
for (uint i = 0; i < offset_count; i++) { |
| 331 |
ivec3 ofs = pos + offsets[i] * params.step_size; |
| 332 |
if (any(lessThan(ofs, ivec3(0))) || any(greaterThanEqual(ofs, ivec3(params.grid_size)))) { |
| 333 |
continue; |
| 334 |
} |
| 335 |
uvec4 q = imageLoad(src_positions, ofs); |
| 336 |
|
| 337 |
if (q.w == 0) { |
| 338 |
continue; //was not initialized yet, ignore |
| 339 |
} |
| 340 |
|
| 341 |
float q_dist = distance(posf, vec3(q.xyz)); |
| 342 |
if (p.w == 0 || q_dist < p_dist) { |
| 343 |
p = q; //just replace because current is unused |
| 344 |
p_dist = q_dist; |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
imageStore(dst_positions, pos, p); |
| 349 |
#endif |
| 350 |
|
| 351 |
#ifdef MODE_JUMPFLOOD_OPTIMIZED |
| 352 |
//optimized version using shared compute memory |
| 353 |
|
| 354 |
ivec3 group_offset = ivec3(gl_WorkGroupID.xyz) % params.step_size; |
| 355 |
ivec3 group_pos = group_offset + (ivec3(gl_WorkGroupID.xyz) / params.step_size) * ivec3(GROUP_SIZE * params.step_size); |
| 356 |
|
| 357 |
//load data into local group memory |
| 358 |
|
| 359 |
if (all(lessThan(ivec3(gl_LocalInvocationID.xyz), ivec3((GROUP_SIZE + 2) / 2)))) { |
| 360 |
//use this thread for loading, this method uses less threads for this but its simpler and less divergent |
| 361 |
ivec3 base_pos = ivec3(gl_LocalInvocationID.xyz) * 2; |
| 362 |
for (uint i = 0; i < 8; i++) { |
| 363 |
ivec3 load_pos = base_pos + ((ivec3(i) >> ivec3(0, 1, 2)) & ivec3(1, 1, 1)); |
| 364 |
ivec3 load_global_pos = group_pos + (load_pos - ivec3(1)) * params.step_size; |
| 365 |
uvec4 q; |
| 366 |
if (all(greaterThanEqual(load_global_pos, ivec3(0))) && all(lessThan(load_global_pos, ivec3(params.grid_size)))) { |
| 367 |
q = imageLoad(src_positions, load_global_pos); |
| 368 |
} else { |
| 369 |
q = uvec4(0); //unused |
| 370 |
} |
| 371 |
|
| 372 |
group_store(load_pos, q); |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
ivec3 global_pos = group_pos + ivec3(gl_LocalInvocationID.xyz) * params.step_size; |
| 377 |
|
| 378 |
if (any(lessThan(global_pos, ivec3(0))) || any(greaterThanEqual(global_pos, ivec3(params.grid_size)))) { |
| 379 |
return; //do nothing else, end here because outside range |
| 380 |
} |
| 381 |
|
| 382 |
//sync |
| 383 |
groupMemoryBarrier(); |
| 384 |
barrier(); |
| 385 |
|
| 386 |
ivec3 local_pos = ivec3(gl_LocalInvocationID.xyz) + ivec3(1); |
| 387 |
|
| 388 |
const uint offset_count = 27; |
| 389 |
const ivec3 offsets[offset_count] = ivec3[]( |
| 390 |
ivec3(-1, -1, -1), |
| 391 |
ivec3(-1, -1, 0), |
| 392 |
ivec3(-1, -1, 1), |
| 393 |
ivec3(-1, 0, -1), |
| 394 |
ivec3(-1, 0, 0), |
| 395 |
ivec3(-1, 0, 1), |
| 396 |
ivec3(-1, 1, -1), |
| 397 |
ivec3(-1, 1, 0), |
| 398 |
ivec3(-1, 1, 1), |
| 399 |
ivec3(0, -1, -1), |
| 400 |
ivec3(0, -1, 0), |
| 401 |
ivec3(0, -1, 1), |
| 402 |
ivec3(0, 0, -1), |
| 403 |
ivec3(0, 0, 0), |
| 404 |
ivec3(0, 0, 1), |
| 405 |
ivec3(0, 1, -1), |
| 406 |
ivec3(0, 1, 0), |
| 407 |
ivec3(0, 1, 1), |
| 408 |
ivec3(1, -1, -1), |
| 409 |
ivec3(1, -1, 0), |
| 410 |
ivec3(1, -1, 1), |
| 411 |
ivec3(1, 0, -1), |
| 412 |
ivec3(1, 0, 0), |
| 413 |
ivec3(1, 0, 1), |
| 414 |
ivec3(1, 1, -1), |
| 415 |
ivec3(1, 1, 0), |
| 416 |
ivec3(1, 1, 1)); |
| 417 |
|
| 418 |
//only makes sense if point is inside screen |
| 419 |
uvec4 closest = uvec4(0); |
| 420 |
float closest_dist = 0.0; |
| 421 |
|
| 422 |
vec3 posf = vec3(global_pos); |
| 423 |
|
| 424 |
if (params.half_size) { |
| 425 |
posf = posf * 2.0 + 0.5; |
| 426 |
} |
| 427 |
|
| 428 |
for (uint i = 0; i < offset_count; i++) { |
| 429 |
uvec4 point = group_load(local_pos + offsets[i]); |
| 430 |
|
| 431 |
if (point.w == 0) { |
| 432 |
continue; //was not initialized yet, ignore |
| 433 |
} |
| 434 |
|
| 435 |
float dist = distance(posf, vec3(point.xyz)); |
| 436 |
if (closest.w == 0 || dist < closest_dist) { |
| 437 |
closest = point; |
| 438 |
closest_dist = dist; |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
imageStore(dst_positions, global_pos, closest); |
| 443 |
|
| 444 |
#endif |
| 445 |
|
| 446 |
#ifdef MODE_UPSCALE_JUMP_FLOOD |
| 447 |
|
| 448 |
ivec3 pos = ivec3(gl_GlobalInvocationID.xyz); |
| 449 |
|
| 450 |
uint c = imageLoad(src_color, pos).r; |
| 451 |
uvec4 v; |
| 452 |
if (bool(c & 1)) { |
| 453 |
//bit set means this is solid |
| 454 |
v.xyz = uvec3(pos); |
| 455 |
v.w = 255; //not zero means used |
| 456 |
} else { |
| 457 |
v = imageLoad(src_positions_half, pos >> 1); |
| 458 |
float d = length(vec3(ivec3(v.xyz) - pos)); |
| 459 |
|
| 460 |
ivec3 vbase = ivec3(v.xyz - (v.xyz & uvec3(1))); |
| 461 |
|
| 462 |
//search around if there is a better candidate from the same block |
| 463 |
for (int i = 0; i < 8; i++) { |
| 464 |
ivec3 bits = ((ivec3(i) >> ivec3(0, 1, 2)) & ivec3(1, 1, 1)); |
| 465 |
ivec3 p = vbase + bits; |
| 466 |
|
| 467 |
float d2 = length(vec3(p - pos)); |
| 468 |
if (d2 < d) { //check valid distance before test so we avoid a read |
| 469 |
uint c2 = imageLoad(src_color, p).r; |
| 470 |
if (bool(c2 & 1)) { |
| 471 |
v.xyz = uvec3(p); |
| 472 |
d = d2; |
| 473 |
} |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
//could validate better position.. |
| 478 |
} |
| 479 |
|
| 480 |
imageStore(dst_positions, pos, v); |
| 481 |
|
| 482 |
#endif |
| 483 |
|
| 484 |
#ifdef MODE_OCCLUSION |
| 485 |
|
| 486 |
uint invocation_idx = uint(gl_LocalInvocationID.x); |
| 487 |
ivec3 region = ivec3(gl_WorkGroupID); |
| 488 |
|
| 489 |
ivec3 region_offset = -ivec3(OCCLUSION_SIZE); |
| 490 |
region_offset += region * OCCLUSION_SIZE * 2; |
| 491 |
region_offset += params.probe_offset * OCCLUSION_SIZE; |
| 492 |
|
| 493 |
if (params.scroll != ivec3(0)) { |
| 494 |
//validate scroll region |
| 495 |
ivec3 region_offset_to = region_offset + ivec3(OCCLUSION_SIZE * 2); |
| 496 |
uvec3 scroll_mask = uvec3(notEqual(params.scroll, ivec3(0))); //save which axes acre scrolling |
| 497 |
ivec3 scroll_from = mix(ivec3(0), ivec3(params.grid_size) + params.scroll, lessThan(params.scroll, ivec3(0))); |
| 498 |
ivec3 scroll_to = mix(ivec3(params.grid_size), params.scroll, greaterThan(params.scroll, ivec3(0))); |
| 499 |
|
| 500 |
if ((uvec3(lessThanEqual(region_offset_to, scroll_from)) | uvec3(greaterThanEqual(region_offset, scroll_to))) * scroll_mask == scroll_mask) { //all axes that scroll are out, exit |
| 501 |
return; //region outside scroll bounds, quit |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
#define OCC_HALF_SIZE (OCCLUSION_SIZE / 2) |
| 506 |
|
| 507 |
ivec3 local_ofs = ivec3(uvec3(invocation_idx % OCC_HALF_SIZE, (invocation_idx % (OCC_HALF_SIZE * OCC_HALF_SIZE)) / OCC_HALF_SIZE, invocation_idx / (OCC_HALF_SIZE * OCC_HALF_SIZE))) * 4; |
| 508 |
|
| 509 |
/* for(int i=0;i<64;i++) { |
| 510 |
ivec3 offset = region_offset + local_ofs + ((ivec3(i) >> ivec3(0,2,4)) & ivec3(3,3,3)); |
| 511 |
uint facig = |
| 512 |
if (all(greaterThanEqual(offset,ivec3(0))) && all(lessThan(offset,ivec3(params.grid_size)))) {*/ |
| 513 |
|
| 514 |
for (int i = 0; i < 16; i++) { //skip x, so it can be packed |
| 515 |
|
| 516 |
ivec3 offset = local_ofs + ((ivec3(i * 4) >> ivec3(0, 2, 4)) & ivec3(3, 3, 3)); |
| 517 |
|
| 518 |
uint facing_pack = 0; |
| 519 |
for (int j = 0; j < 4; j++) { |
| 520 |
ivec3 foffset = region_offset + offset + ivec3(j, 0, 0); |
| 521 |
if (all(greaterThanEqual(foffset, ivec3(0))) && all(lessThan(foffset, ivec3(params.grid_size)))) { |
| 522 |
uint f = imageLoad(src_facing, foffset).r; |
| 523 |
facing_pack |= f << (j * 8); |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
occlusion_facing[(offset.z * (OCCLUSION_SIZE * 2 * OCCLUSION_SIZE * 2) + offset.y * (OCCLUSION_SIZE * 2) + offset.x) / 4] = facing_pack; |
| 528 |
} |
| 529 |
|
| 530 |
//sync occlusion saved |
| 531 |
groupMemoryBarrier(); |
| 532 |
barrier(); |
| 533 |
|
| 534 |
//process occlusion |
| 535 |
|
| 536 |
#define OCC_STEPS (OCCLUSION_SIZE * 3 - 2) |
| 537 |
#define OCC_HALF_STEPS (OCC_STEPS / 2) |
| 538 |
|
| 539 |
for (int step = 0; step < OCC_STEPS; step++) { |
| 540 |
bool shrink = step >= OCC_HALF_STEPS; |
| 541 |
int occ_step = shrink ? OCC_HALF_STEPS - (step - OCC_HALF_STEPS) - 1 : step; |
| 542 |
|
| 543 |
if (invocation_idx < group_size_offset[occ_step].x) { |
| 544 |
uint pv = group_pos[group_size_offset[occ_step].y + invocation_idx]; |
| 545 |
ivec3 proc_abs = (ivec3(int(pv)) >> ivec3(0, 8, 16)) & ivec3(0xFF); |
| 546 |
|
| 547 |
if (shrink) { |
| 548 |
proc_abs = ivec3(OCCLUSION_SIZE) - proc_abs - ivec3(1); |
| 549 |
} |
| 550 |
|
| 551 |
for (int i = 0; i < 8; i++) { |
| 552 |
ivec3 bits = ((ivec3(i) >> ivec3(0, 1, 2)) & ivec3(1, 1, 1)); |
| 553 |
ivec3 proc_sign = bits * 2 - 1; |
| 554 |
ivec3 local_offset = ivec3(OCCLUSION_SIZE) + proc_abs * proc_sign - (ivec3(1) - bits); |
| 555 |
ivec3 offset = local_offset + region_offset; |
| 556 |
if (all(greaterThanEqual(offset, ivec3(0))) && all(lessThan(offset, ivec3(params.grid_size)))) { |
| 557 |
float occ; |
| 558 |
|
| 559 |
uint facing = get_facing(local_offset); |
| 560 |
|
| 561 |
if (facing != 0) { //solid |
| 562 |
occ = 0.0; |
| 563 |
} else if (step == 0) { |
| 564 |
#if 0 |
| 565 |
occ = 0.0; |
| 566 |
if (get_facing(local_offset - ivec3(proc_sign.x,0,0))==0) { |
| 567 |
occ+=1.0; |
| 568 |
} |
| 569 |
if (get_facing(local_offset - ivec3(0,proc_sign.y,0))==0) { |
| 570 |
occ+=1.0; |
| 571 |
} |
| 572 |
if (get_facing(local_offset - ivec3(0,0,proc_sign.z))==0) { |
| 573 |
occ+=1.0; |
| 574 |
} |
| 575 |
/* |
| 576 |
if (get_facing(local_offset - proc_sign)==0) { |
| 577 |
occ+=1.0; |
| 578 |
}*/ |
| 579 |
|
| 580 |
occ/=3.0; |
| 581 |
#endif |
| 582 |
occ = 1.0; |
| 583 |
|
| 584 |
} else { |
| 585 |
ivec3 read_dir = -proc_sign; |
| 586 |
|
| 587 |
ivec3 major_axis; |
| 588 |
if (proc_abs.x < proc_abs.y) { |
| 589 |
if (proc_abs.z < proc_abs.y) { |
| 590 |
major_axis = ivec3(0, 1, 0); |
| 591 |
} else { |
| 592 |
major_axis = ivec3(0, 0, 1); |
| 593 |
} |
| 594 |
} else { |
| 595 |
if (proc_abs.z < proc_abs.x) { |
| 596 |
major_axis = ivec3(1, 0, 0); |
| 597 |
} else { |
| 598 |
major_axis = ivec3(0, 0, 1); |
| 599 |
} |
| 600 |
} |
| 601 |
|
| 602 |
float avg = 0.0; |
| 603 |
occ = 0.0; |
| 604 |
|
| 605 |
ivec3 read_x = offset + ivec3(read_dir.x, 0, 0) + (proc_abs.x == 0 ? major_axis * read_dir : ivec3(0)); |
| 606 |
ivec3 read_y = offset + ivec3(0, read_dir.y, 0) + (proc_abs.y == 0 ? major_axis * read_dir : ivec3(0)); |
| 607 |
ivec3 read_z = offset + ivec3(0, 0, read_dir.z) + (proc_abs.z == 0 ? major_axis * read_dir : ivec3(0)); |
| 608 |
|
| 609 |
uint facing_x = get_facing(read_x - region_offset); |
| 610 |
if (facing_x == 0) { |
| 611 |
if (all(greaterThanEqual(read_x, ivec3(0))) && all(lessThan(read_x, ivec3(params.grid_size)))) { |
| 612 |
occ += imageLoad(dst_occlusion[params.occlusion_index], read_x).r; |
| 613 |
avg += 1.0; |
| 614 |
} |
| 615 |
} else { |
| 616 |
if (proc_abs.x != 0) { //do not occlude from voxels in the opposite octant |
| 617 |
avg += 1.0; |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
uint facing_y = get_facing(read_y - region_offset); |
| 622 |
if (facing_y == 0) { |
| 623 |
if (all(greaterThanEqual(read_y, ivec3(0))) && all(lessThan(read_y, ivec3(params.grid_size)))) { |
| 624 |
occ += imageLoad(dst_occlusion[params.occlusion_index], read_y).r; |
| 625 |
avg += 1.0; |
| 626 |
} |
| 627 |
} else { |
| 628 |
if (proc_abs.y != 0) { |
| 629 |
avg += 1.0; |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
uint facing_z = get_facing(read_z - region_offset); |
| 634 |
if (facing_z == 0) { |
| 635 |
if (all(greaterThanEqual(read_z, ivec3(0))) && all(lessThan(read_z, ivec3(params.grid_size)))) { |
| 636 |
occ += imageLoad(dst_occlusion[params.occlusion_index], read_z).r; |
| 637 |
avg += 1.0; |
| 638 |
} |
| 639 |
} else { |
| 640 |
if (proc_abs.z != 0) { |
| 641 |
avg += 1.0; |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
if (avg > 0.0) { |
| 646 |
occ /= avg; |
| 647 |
} |
| 648 |
} |
| 649 |
|
| 650 |
imageStore(dst_occlusion[params.occlusion_index], offset, vec4(occ)); |
| 651 |
} |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
groupMemoryBarrier(); |
| 656 |
barrier(); |
| 657 |
} |
| 658 |
#if 1 |
| 659 |
//bias solid voxels away |
| 660 |
|
| 661 |
for (int i = 0; i < 64; i++) { |
| 662 |
ivec3 local_offset = local_ofs + ((ivec3(i) >> ivec3(0, 2, 4)) & ivec3(3, 3, 3)); |
| 663 |
ivec3 offset = region_offset + local_offset; |
| 664 |
|
| 665 |
if (all(greaterThanEqual(offset, ivec3(0))) && all(lessThan(offset, ivec3(params.grid_size)))) { |
| 666 |
uint facing = get_facing(local_offset); |
| 667 |
|
| 668 |
if (facing != 0) { |
| 669 |
//only work on solids |
| 670 |
|
| 671 |
ivec3 proc_pos = local_offset - ivec3(OCCLUSION_SIZE); |
| 672 |
proc_pos += mix(ivec3(0), ivec3(1), greaterThanEqual(proc_pos, ivec3(0))); |
| 673 |
|
| 674 |
float avg = 0.0; |
| 675 |
float occ = 0.0; |
| 676 |
|
| 677 |
ivec3 read_dir = -sign(proc_pos); |
| 678 |
ivec3 read_dir_x = ivec3(read_dir.x, 0, 0); |
| 679 |
ivec3 read_dir_y = ivec3(0, read_dir.y, 0); |
| 680 |
ivec3 read_dir_z = ivec3(0, 0, read_dir.z); |
| 681 |
//solid |
| 682 |
#if 0 |
| 683 |
|
| 684 |
uvec3 facing_pos_base = (uvec3(facing) >> uvec3(0,1,2)) & uvec3(1,1,1); |
| 685 |
uvec3 facing_neg_base = (uvec3(facing) >> uvec3(3,4,5)) & uvec3(1,1,1); |
| 686 |
uvec3 facing_pos= facing_pos_base &((~facing_neg_base)&uvec3(1,1,1)); |
| 687 |
uvec3 facing_neg= facing_neg_base &((~facing_pos_base)&uvec3(1,1,1)); |
| 688 |
#else |
| 689 |
uvec3 facing_pos = (uvec3(facing) >> uvec3(0, 1, 2)) & uvec3(1, 1, 1); |
| 690 |
uvec3 facing_neg = (uvec3(facing) >> uvec3(3, 4, 5)) & uvec3(1, 1, 1); |
| 691 |
#endif |
| 692 |
bvec3 read_valid = bvec3(mix(facing_neg, facing_pos, greaterThan(read_dir, ivec3(0)))); |
| 693 |
|
| 694 |
//sides |
| 695 |
if (read_valid.x) { |
| 696 |
ivec3 read_offset = local_offset + read_dir_x; |
| 697 |
uint f = get_facing(read_offset); |
| 698 |
if (f == 0) { |
| 699 |
read_offset += region_offset; |
| 700 |
if (all(greaterThanEqual(read_offset, ivec3(0))) && all(lessThan(read_offset, ivec3(params.grid_size)))) { |
| 701 |
occ += imageLoad(dst_occlusion[params.occlusion_index], read_offset).r; |
| 702 |
avg += 1.0; |
| 703 |
} |
| 704 |
} |
| 705 |
} |
| 706 |
|
| 707 |
if (read_valid.y) { |
| 708 |
ivec3 read_offset = local_offset + read_dir_y; |
| 709 |
uint f = get_facing(read_offset); |
| 710 |
if (f == 0) { |
| 711 |
read_offset += region_offset; |
| 712 |
if (all(greaterThanEqual(read_offset, ivec3(0))) && all(lessThan(read_offset, ivec3(params.grid_size)))) { |
| 713 |
occ += imageLoad(dst_occlusion[params.occlusion_index], read_offset).r; |
| 714 |
avg += 1.0; |
| 715 |
} |
| 716 |
} |
| 717 |
} |
| 718 |
|
| 719 |
if (read_valid.z) { |
| 720 |
ivec3 read_offset = local_offset + read_dir_z; |
| 721 |
uint f = get_facing(read_offset); |
| 722 |
if (f == 0) { |
| 723 |
read_offset += region_offset; |
| 724 |
if (all(greaterThanEqual(read_offset, ivec3(0))) && all(lessThan(read_offset, ivec3(params.grid_size)))) { |
| 725 |
occ += imageLoad(dst_occlusion[params.occlusion_index], read_offset).r; |
| 726 |
avg += 1.0; |
| 727 |
} |
| 728 |
} |
| 729 |
} |
| 730 |
|
| 731 |
//adjacents |
| 732 |
|
| 733 |
if (all(read_valid.yz)) { |
| 734 |
ivec3 read_offset = local_offset + read_dir_y + read_dir_z; |
| 735 |
uint f = get_facing(read_offset); |
| 736 |
if (f == 0) { |
| 737 |
read_offset += region_offset; |
| 738 |
if (all(greaterThanEqual(read_offset, ivec3(0))) && all(lessThan(read_offset, ivec3(params.grid_size)))) { |
| 739 |
occ += imageLoad(dst_occlusion[params.occlusion_index], read_offset).r; |
| 740 |
avg += 1.0; |
| 741 |
} |
| 742 |
} |
| 743 |
} |
| 744 |
|
| 745 |
if (all(read_valid.xz)) { |
| 746 |
ivec3 read_offset = local_offset + read_dir_x + read_dir_z; |
| 747 |
uint f = get_facing(read_offset); |
| 748 |
if (f == 0) { |
| 749 |
read_offset += region_offset; |
| 750 |
if (all(greaterThanEqual(read_offset, ivec3(0))) && all(lessThan(read_offset, ivec3(params.grid_size)))) { |
| 751 |
occ += imageLoad(dst_occlusion[params.occlusion_index], read_offset).r; |
| 752 |
avg += 1.0; |
| 753 |
} |
| 754 |
} |
| 755 |
} |
| 756 |
|
| 757 |
if (all(read_valid.xy)) { |
| 758 |
ivec3 read_offset = local_offset + read_dir_x + read_dir_y; |
| 759 |
uint f = get_facing(read_offset); |
| 760 |
if (f == 0) { |
| 761 |
read_offset += region_offset; |
| 762 |
if (all(greaterThanEqual(read_offset, ivec3(0))) && all(lessThan(read_offset, ivec3(params.grid_size)))) { |
| 763 |
occ += imageLoad(dst_occlusion[params.occlusion_index], read_offset).r; |
| 764 |
avg += 1.0; |
| 765 |
} |
| 766 |
} |
| 767 |
} |
| 768 |
|
| 769 |
//diagonal |
| 770 |
|
| 771 |
if (all(read_valid)) { |
| 772 |
ivec3 read_offset = local_offset + read_dir; |
| 773 |
uint f = get_facing(read_offset); |
| 774 |
if (f == 0) { |
| 775 |
read_offset += region_offset; |
| 776 |
if (all(greaterThanEqual(read_offset, ivec3(0))) && all(lessThan(read_offset, ivec3(params.grid_size)))) { |
| 777 |
occ += imageLoad(dst_occlusion[params.occlusion_index], read_offset).r; |
| 778 |
avg += 1.0; |
| 779 |
} |
| 780 |
} |
| 781 |
} |
| 782 |
|
| 783 |
if (avg > 0.0) { |
| 784 |
occ /= avg; |
| 785 |
} |
| 786 |
|
| 787 |
imageStore(dst_occlusion[params.occlusion_index], offset, vec4(occ)); |
| 788 |
} |
| 789 |
} |
| 790 |
} |
| 791 |
|
| 792 |
#endif |
| 793 |
|
| 794 |
#if 1 |
| 795 |
groupMemoryBarrier(); |
| 796 |
barrier(); |
| 797 |
|
| 798 |
for (int i = 0; i < 64; i++) { |
| 799 |
ivec3 local_offset = local_ofs + ((ivec3(i) >> ivec3(0, 2, 4)) & ivec3(3, 3, 3)); |
| 800 |
ivec3 offset = region_offset + local_offset; |
| 801 |
|
| 802 |
if (all(greaterThanEqual(offset, ivec3(0))) && all(lessThan(offset, ivec3(params.grid_size)))) { |
| 803 |
uint facing = get_facing(local_offset); |
| 804 |
|
| 805 |
if (facing == 0) { |
| 806 |
ivec3 proc_pos = local_offset - ivec3(OCCLUSION_SIZE); |
| 807 |
proc_pos += mix(ivec3(0), ivec3(1), greaterThanEqual(proc_pos, ivec3(0))); |
| 808 |
|
| 809 |
ivec3 proc_abs = abs(proc_pos); |
| 810 |
|
| 811 |
ivec3 read_dir = sign(proc_pos); //opposite direction |
| 812 |
ivec3 read_dir_x = ivec3(read_dir.x, 0, 0); |
| 813 |
ivec3 read_dir_y = ivec3(0, read_dir.y, 0); |
| 814 |
ivec3 read_dir_z = ivec3(0, 0, read_dir.z); |
| 815 |
//solid |
| 816 |
uvec3 read_mask = mix(uvec3(1, 2, 4), uvec3(8, 16, 32), greaterThan(read_dir, ivec3(0))); //match positive with negative normals |
| 817 |
uvec3 block_mask = mix(uvec3(1, 2, 4), uvec3(8, 16, 32), lessThan(read_dir, ivec3(0))); //match positive with negative normals |
| 818 |
|
| 819 |
block_mask = uvec3(0); |
| 820 |
|
| 821 |
float visible = 0.0; |
| 822 |
float occlude_total = 0.0; |
| 823 |
|
| 824 |
if (proc_abs.x < OCCLUSION_SIZE) { |
| 825 |
ivec3 read_offset = local_offset + read_dir_x; |
| 826 |
uint x_mask = get_facing(read_offset); |
| 827 |
if (x_mask != 0) { |
| 828 |
read_offset += region_offset; |
| 829 |
if (all(greaterThanEqual(read_offset, ivec3(0))) && all(lessThan(read_offset, ivec3(params.grid_size)))) { |
| 830 |
occlude_total += 1.0; |
| 831 |
if (bool(x_mask & read_mask.x) && !bool(x_mask & block_mask.x)) { |
| 832 |
visible += 1.0; |
| 833 |
} |
| 834 |
} |
| 835 |
} |
| 836 |
} |
| 837 |
|
| 838 |
if (proc_abs.y < OCCLUSION_SIZE) { |
| 839 |
ivec3 read_offset = local_offset + read_dir_y; |
| 840 |
uint y_mask = get_facing(read_offset); |
| 841 |
if (y_mask != 0) { |
| 842 |
read_offset += region_offset; |
| 843 |
if (all(greaterThanEqual(read_offset, ivec3(0))) && all(lessThan(read_offset, ivec3(params.grid_size)))) { |
| 844 |
occlude_total += 1.0; |
| 845 |
if (bool(y_mask & read_mask.y) && !bool(y_mask & block_mask.y)) { |
| 846 |
visible += 1.0; |
| 847 |
} |
| 848 |
} |
| 849 |
} |
| 850 |
} |
| 851 |
|
| 852 |
if (proc_abs.z < OCCLUSION_SIZE) { |
| 853 |
ivec3 read_offset = local_offset + read_dir_z; |
| 854 |
uint z_mask = get_facing(read_offset); |
| 855 |
if (z_mask != 0) { |
| 856 |
read_offset += region_offset; |
| 857 |
if (all(greaterThanEqual(read_offset, ivec3(0))) && all(lessThan(read_offset, ivec3(params.grid_size)))) { |
| 858 |
occlude_total += 1.0; |
| 859 |
if (bool(z_mask & read_mask.z) && !bool(z_mask & block_mask.z)) { |
| 860 |
visible += 1.0; |
| 861 |
} |
| 862 |
} |
| 863 |
} |
| 864 |
} |
| 865 |
|
| 866 |
//if near the cartesian plane, test in opposite direction too |
| 867 |
|
| 868 |
read_mask = mix(uvec3(1, 2, 4), uvec3(8, 16, 32), lessThan(read_dir, ivec3(0))); //match negative with positive normals |
| 869 |
block_mask = mix(uvec3(1, 2, 4), uvec3(8, 16, 32), greaterThan(read_dir, ivec3(0))); //match negative with positive normals |
| 870 |
block_mask = uvec3(0); |
| 871 |
|
| 872 |
if (proc_abs.x == 1) { |
| 873 |
ivec3 read_offset = local_offset - read_dir_x; |
| 874 |
uint x_mask = get_facing(read_offset); |
| 875 |
if (x_mask != 0) { |
| 876 |
read_offset += region_offset; |
| 877 |
if (all(greaterThanEqual(read_offset, ivec3(0))) && all(lessThan(read_offset, ivec3(params.grid_size)))) { |
| 878 |
occlude_total += 1.0; |
| 879 |
if (bool(x_mask & read_mask.x) && !bool(x_mask & block_mask.x)) { |
| 880 |
visible += 1.0; |
| 881 |
} |
| 882 |
} |
| 883 |
} |
| 884 |
} |
| 885 |
|
| 886 |
if (proc_abs.y == 1) { |
| 887 |
ivec3 read_offset = local_offset - read_dir_y; |
| 888 |
uint y_mask = get_facing(read_offset); |
| 889 |
if (y_mask != 0) { |
| 890 |
read_offset += region_offset; |
| 891 |
if (all(greaterThanEqual(read_offset, ivec3(0))) && all(lessThan(read_offset, ivec3(params.grid_size)))) { |
| 892 |
occlude_total += 1.0; |
| 893 |
if (bool(y_mask & read_mask.y) && !bool(y_mask & block_mask.y)) { |
| 894 |
visible += 1.0; |
| 895 |
} |
| 896 |
} |
| 897 |
} |
| 898 |
} |
| 899 |
|
| 900 |
if (proc_abs.z == 1) { |
| 901 |
ivec3 read_offset = local_offset - read_dir_z; |
| 902 |
uint z_mask = get_facing(read_offset); |
| 903 |
if (z_mask != 0) { |
| 904 |
read_offset += region_offset; |
| 905 |
if (all(greaterThanEqual(read_offset, ivec3(0))) && all(lessThan(read_offset, ivec3(params.grid_size)))) { |
| 906 |
occlude_total += 1.0; |
| 907 |
if (bool(z_mask & read_mask.z) && !bool(z_mask & block_mask.z)) { |
| 908 |
visible += 1.0; |
| 909 |
} |
| 910 |
} |
| 911 |
} |
| 912 |
} |
| 913 |
|
| 914 |
if (occlude_total > 0.0) { |
| 915 |
float occ = imageLoad(dst_occlusion[params.occlusion_index], offset).r; |
| 916 |
occ *= visible / occlude_total; |
| 917 |
imageStore(dst_occlusion[params.occlusion_index], offset, vec4(occ)); |
| 918 |
} |
| 919 |
} |
| 920 |
} |
| 921 |
} |
| 922 |
|
| 923 |
#endif |
| 924 |
|
| 925 |
/* |
| 926 |
for(int i=0;i<8;i++) { |
| 927 |
ivec3 local_offset = local_pos + ((ivec3(i) >> ivec3(2,1,0)) & ivec3(1,1,1)) * OCCLUSION_SIZE; |
| 928 |
ivec3 offset = local_offset - ivec3(OCCLUSION_SIZE); //looking around probe, so starts negative |
| 929 |
offset += region * OCCLUSION_SIZE * 2; //offset by region |
| 930 |
offset += params.probe_offset * OCCLUSION_SIZE; // offset by probe offset |
| 931 |
if (all(greaterThanEqual(offset,ivec3(0))) && all(lessThan(offset,ivec3(params.grid_size)))) { |
| 932 |
imageStore(dst_occlusion[params.occlusion_index],offset,vec4( occlusion_data[ to_linear(local_offset) ] )); |
| 933 |
//imageStore(dst_occlusion[params.occlusion_index],offset,vec4( occlusion_solid[ to_linear(local_offset) ] )); |
| 934 |
} |
| 935 |
} |
| 936 |
*/ |
| 937 |
|
| 938 |
#endif |
| 939 |
|
| 940 |
#ifdef MODE_STORE |
| 941 |
|
| 942 |
ivec3 local = ivec3(gl_LocalInvocationID.xyz); |
| 943 |
ivec3 pos = ivec3(gl_GlobalInvocationID.xyz); |
| 944 |
// store SDF |
| 945 |
uvec4 p = imageLoad(src_positions, pos); |
| 946 |
|
| 947 |
bool solid = false; |
| 948 |
float d; |
| 949 |
if (ivec3(p.xyz) == pos) { |
| 950 |
//solid block |
| 951 |
d = 0; |
| 952 |
solid = true; |
| 953 |
} else { |
| 954 |
//distance block |
| 955 |
d = 1.0 + length(vec3(p.xyz) - vec3(pos)); |
| 956 |
} |
| 957 |
|
| 958 |
d /= 255.0; |
| 959 |
|
| 960 |
imageStore(dst_sdf, pos, vec4(d)); |
| 961 |
|
| 962 |
// STORE OCCLUSION |
| 963 |
|
| 964 |
uint occlusion = 0; |
| 965 |
const uint occlusion_shift[8] = uint[](12, 8, 4, 0, 28, 24, 20, 16); |
| 966 |
for (int i = 0; i < 8; i++) { |
| 967 |
float occ = imageLoad(src_occlusion[i], pos).r; |
| 968 |
occlusion |= uint(clamp(occ * 15.0, 0.0, 15.0)) << occlusion_shift[i]; |
| 969 |
} |
| 970 |
{ |
| 971 |
ivec3 occ_pos = pos; |
| 972 |
occ_pos.z += params.cascade * params.grid_size; |
| 973 |
imageStore(dst_occlusion, occ_pos, uvec4(occlusion & 0xFFFF)); |
| 974 |
occ_pos.x += params.grid_size; |
| 975 |
imageStore(dst_occlusion, occ_pos, uvec4(occlusion >> 16)); |
| 976 |
} |
| 977 |
|
| 978 |
// STORE POSITIONS |
| 979 |
|
| 980 |
if (local == ivec3(0)) { |
| 981 |
store_position_count = 0; //base one stores as zero, the others wait |
| 982 |
} |
| 983 |
|
| 984 |
groupMemoryBarrier(); |
| 985 |
barrier(); |
| 986 |
|
| 987 |
if (solid) { |
| 988 |
uint index = atomicAdd(store_position_count, 1); |
| 989 |
// At least do the conversion work in parallel |
| 990 |
store_positions[index].position = uint(pos.x | (pos.y << 7) | (pos.z << 14)); |
| 991 |
|
| 992 |
//see around which voxels point to this one, add them to the list |
| 993 |
uint bit_index = 0; |
| 994 |
uint neighbour_bits = 0; |
| 995 |
for (int i = -1; i <= 1; i++) { |
| 996 |
for (int j = -1; j <= 1; j++) { |
| 997 |
for (int k = -1; k <= 1; k++) { |
| 998 |
if (i == 0 && j == 0 && k == 0) { |
| 999 |
continue; |
| 1000 |
} |
| 1001 |
ivec3 npos = pos + ivec3(i, j, k); |
| 1002 |
if (all(greaterThanEqual(npos, ivec3(0))) && all(lessThan(npos, ivec3(params.grid_size)))) { |
| 1003 |
p = imageLoad(src_positions, npos); |
| 1004 |
if (ivec3(p.xyz) == pos) { |
| 1005 |
neighbour_bits |= (1 << bit_index); |
| 1006 |
} |
| 1007 |
} |
| 1008 |
bit_index++; |
| 1009 |
} |
| 1010 |
} |
| 1011 |
} |
| 1012 |
|
| 1013 |
uint rgb = imageLoad(src_albedo, pos).r; |
| 1014 |
uint facing = imageLoad(src_facing, pos).r; |
| 1015 |
|
| 1016 |
store_positions[index].albedo = rgb >> 1; //store as it comes (555) to avoid precision loss (and move away the alpha bit) |
| 1017 |
store_positions[index].albedo |= (facing & 0x3F) << 15; // store facing in bits 15-21 |
| 1018 |
|
| 1019 |
store_positions[index].albedo |= neighbour_bits << 21; //store lower 11 bits of neighbors with remaining albedo |
| 1020 |
store_positions[index].position |= (neighbour_bits >> 11) << 21; //store 11 bits more of neighbors with position |
| 1021 |
|
| 1022 |
store_positions[index].light = imageLoad(src_light, pos).r; |
| 1023 |
store_positions[index].light_aniso = imageLoad(src_light_aniso, pos).r; |
| 1024 |
//add neighbors |
| 1025 |
store_positions[index].light |= (neighbour_bits >> 22) << 30; //store 2 bits more of neighbors with light |
| 1026 |
store_positions[index].light_aniso |= (neighbour_bits >> 24) << 30; //store 2 bits more of neighbors with aniso |
| 1027 |
} |
| 1028 |
|
| 1029 |
groupMemoryBarrier(); |
| 1030 |
barrier(); |
| 1031 |
|
| 1032 |
// global increment only once per group, to reduce pressure |
| 1033 |
|
| 1034 |
if (local == ivec3(0) && store_position_count > 0) { |
| 1035 |
store_from_index = atomicAdd(dispatch_data.total_count, store_position_count); |
| 1036 |
uint group_count = (store_from_index + store_position_count - 1) / 64 + 1; |
| 1037 |
atomicMax(dispatch_data.x, group_count); |
| 1038 |
} |
| 1039 |
|
| 1040 |
groupMemoryBarrier(); |
| 1041 |
barrier(); |
| 1042 |
|
| 1043 |
uint read_index = uint(local.z * 4 * 4 + local.y * 4 + local.x); |
| 1044 |
uint write_index = store_from_index + read_index; |
| 1045 |
|
| 1046 |
if (read_index < store_position_count) { |
| 1047 |
dst_process_voxels.data[write_index] = store_positions[read_index]; |
| 1048 |
} |
| 1049 |
|
| 1050 |
if (pos == ivec3(0)) { |
| 1051 |
//this thread clears y and z |
| 1052 |
dispatch_data.y = 1; |
| 1053 |
dispatch_data.z = 1; |
| 1054 |
} |
| 1055 |
#endif |
| 1056 |
} |