Skip to content
0.2.0-dev Download ZIP
1064 lines ·33.8 KB Raw
1#[compute]
2
3#version 450
4
5#VERSION_DEFINES
6
7#ifdef MODE_JUMPFLOOD_OPTIMIZED
8#define GROUP_SIZE 8
9
10layout(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
14layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
15
16#else
17//grid layout
18layout(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)
23layout(r16ui, set = 0, binding = 1) uniform restrict readonly uimage3D src_color;
24layout(rgba8ui, set = 0, binding = 2) uniform restrict writeonly uimage3D dst_positions;
25#endif
26
27#ifdef MODE_UPSCALE_JUMP_FLOOD
28layout(r16ui, set = 0, binding = 1) uniform restrict readonly uimage3D src_color;
29layout(rgba8ui, set = 0, binding = 2) uniform restrict readonly uimage3D src_positions_half;
30layout(rgba8ui, set = 0, binding = 3) uniform restrict writeonly uimage3D dst_positions;
31#endif
32
33#if defined(MODE_JUMPFLOOD) || defined(MODE_JUMPFLOOD_OPTIMIZED)
34layout(rgba8ui, set = 0, binding = 1) uniform restrict readonly uimage3D src_positions;
35layout(rgba8ui, set = 0, binding = 2) uniform restrict writeonly uimage3D dst_positions;
36#endif
37
38#ifdef MODE_JUMPFLOOD_OPTIMIZED
39
40shared uvec4 group_positions[(GROUP_SIZE + 2) * (GROUP_SIZE + 2) * (GROUP_SIZE + 2)]; //4x4x4 with margins
41
42void 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
47uvec4 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
56layout(r16ui, set = 0, binding = 1) uniform restrict readonly uimage3D src_color;
57layout(r8, set = 0, binding = 2) uniform restrict image3D dst_occlusion[8];
58layout(r32ui, set = 0, binding = 3) uniform restrict readonly uimage3D src_facing;
59
60const 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));
61const 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
73shared uint occlusion_facing[((OCCLUSION_SIZE * 2) * (OCCLUSION_SIZE * 2) * (OCCLUSION_SIZE * 2)) / 4];
74
75uint 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
85layout(rgba8ui, set = 0, binding = 1) uniform restrict readonly uimage3D src_positions;
86layout(r16ui, set = 0, binding = 2) uniform restrict readonly uimage3D src_albedo;
87layout(r8, set = 0, binding = 3) uniform restrict readonly image3D src_occlusion[8];
88layout(r32ui, set = 0, binding = 4) uniform restrict readonly uimage3D src_light;
89layout(r32ui, set = 0, binding = 5) uniform restrict readonly uimage3D src_light_aniso;
90layout(r32ui, set = 0, binding = 6) uniform restrict readonly uimage3D src_facing;
91
92layout(r8, set = 0, binding = 7) uniform restrict writeonly image3D dst_sdf;
93layout(r16ui, set = 0, binding = 8) uniform restrict writeonly uimage3D dst_occlusion;
94
95layout(set = 0, binding = 10, std430) restrict buffer DispatchData {
96 uint x;
97 uint y;
98 uint z;
99 uint total_count;
100}
101dispatch_data;
102
103struct 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
111layout(set = 0, binding = 11, std430) restrict buffer writeonly ProcessVoxels {
112 ProcessVoxel data[];
113}
114dst_process_voxels;
115
116shared ProcessVoxel store_positions[4 * 4 * 4];
117shared uint store_position_count;
118shared uint store_from_index;
119#endif
120
121#ifdef MODE_SCROLL
122
123layout(r16ui, set = 0, binding = 1) uniform restrict writeonly uimage3D dst_albedo;
124layout(r32ui, set = 0, binding = 2) uniform restrict writeonly uimage3D dst_facing;
125layout(r32ui, set = 0, binding = 3) uniform restrict writeonly uimage3D dst_light;
126layout(r32ui, set = 0, binding = 4) uniform restrict writeonly uimage3D dst_light_aniso;
127
128layout(set = 0, binding = 5, std430) restrict buffer readonly DispatchData {
129 uint x;
130 uint y;
131 uint z;
132 uint total_count;
133}
134dispatch_data;
135
136struct 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
144layout(set = 0, binding = 6, std430) restrict buffer readonly ProcessVoxels {
145 ProcessVoxel data[];
146}
147src_process_voxels;
148
149#endif
150
151#ifdef MODE_SCROLL_OCCLUSION
152
153layout(r8, set = 0, binding = 1) uniform restrict image3D dst_occlusion[8];
154layout(r16ui, set = 0, binding = 2) uniform restrict readonly uimage3D src_occlusion;
155
156#endif
157
158layout(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}
171params;
172
173void 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 //sync
377 groupMemoryBarrier();
378 barrier();
379
380 ivec3 global_pos = group_pos + ivec3(gl_LocalInvocationID.xyz) * params.step_size;
381
382 if (any(lessThan(global_pos, ivec3(0))) || any(greaterThanEqual(global_pos, ivec3(params.grid_size)))) {
383 return; //do nothing else, end here because outside range
384 }
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 bool region_out_of_bounds = false;
494
495 if (params.scroll != ivec3(0)) {
496 //validate scroll region
497 ivec3 region_offset_to = region_offset + ivec3(OCCLUSION_SIZE * 2);
498 uvec3 scroll_mask = uvec3(notEqual(params.scroll, ivec3(0))); //save which axes acre scrolling
499 ivec3 scroll_from = mix(ivec3(0), ivec3(params.grid_size) + params.scroll, lessThan(params.scroll, ivec3(0)));
500 ivec3 scroll_to = mix(ivec3(params.grid_size), params.scroll, greaterThan(params.scroll, ivec3(0)));
501
502 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
503 region_out_of_bounds = true; //region outside scroll bounds, quit
504 }
505 }
506
507#define OCC_HALF_SIZE (OCCLUSION_SIZE / 2)
508
509 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;
510
511 if (!region_out_of_bounds) {
512 for (int i = 0; i < 16; i++) { //skip x, so it can be packed
513
514 ivec3 offset = local_ofs + ((ivec3(i * 4) >> ivec3(0, 2, 4)) & ivec3(3, 3, 3));
515
516 uint facing_pack = 0;
517 for (int j = 0; j < 4; j++) {
518 ivec3 foffset = region_offset + offset + ivec3(j, 0, 0);
519 if (all(greaterThanEqual(foffset, ivec3(0))) && all(lessThan(foffset, ivec3(params.grid_size)))) {
520 uint f = imageLoad(src_facing, foffset).r;
521 facing_pack |= f << (j * 8);
522 }
523 }
524
525 occlusion_facing[(offset.z * (OCCLUSION_SIZE * 2 * OCCLUSION_SIZE * 2) + offset.y * (OCCLUSION_SIZE * 2) + offset.x) / 4] = facing_pack;
526 }
527 }
528
529 //sync occlusion saved
530 groupMemoryBarrier();
531 barrier();
532
533 //process occlusion
534
535#define OCC_STEPS (OCCLUSION_SIZE * 3 - 2)
536#define OCC_HALF_STEPS (OCC_STEPS / 2)
537
538 for (int step = 0; step < OCC_STEPS; step++) {
539 if (!region_out_of_bounds) {
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
656 groupMemoryBarrier();
657 barrier();
658 }
659#if 1
660 //bias solid voxels away
661
662 if (!region_out_of_bounds) {
663 for (int i = 0; i < 64; i++) {
664 ivec3 local_offset = local_ofs + ((ivec3(i) >> ivec3(0, 2, 4)) & ivec3(3, 3, 3));
665 ivec3 offset = region_offset + local_offset;
666
667 if (all(greaterThanEqual(offset, ivec3(0))) && all(lessThan(offset, ivec3(params.grid_size)))) {
668 uint facing = get_facing(local_offset);
669
670 if (facing != 0) {
671 //only work on solids
672
673 ivec3 proc_pos = local_offset - ivec3(OCCLUSION_SIZE);
674 proc_pos += mix(ivec3(0), ivec3(1), greaterThanEqual(proc_pos, ivec3(0)));
675
676 float avg = 0.0;
677 float occ = 0.0;
678
679 ivec3 read_dir = -sign(proc_pos);
680 ivec3 read_dir_x = ivec3(read_dir.x, 0, 0);
681 ivec3 read_dir_y = ivec3(0, read_dir.y, 0);
682 ivec3 read_dir_z = ivec3(0, 0, read_dir.z);
683 //solid
684#if 0
685
686 uvec3 facing_pos_base = (uvec3(facing) >> uvec3(0,1,2)) & uvec3(1,1,1);
687 uvec3 facing_neg_base = (uvec3(facing) >> uvec3(3,4,5)) & uvec3(1,1,1);
688 uvec3 facing_pos= facing_pos_base &((~facing_neg_base)&uvec3(1,1,1));
689 uvec3 facing_neg= facing_neg_base &((~facing_pos_base)&uvec3(1,1,1));
690#else
691 uvec3 facing_pos = (uvec3(facing) >> uvec3(0, 1, 2)) & uvec3(1, 1, 1);
692 uvec3 facing_neg = (uvec3(facing) >> uvec3(3, 4, 5)) & uvec3(1, 1, 1);
693#endif
694 bvec3 read_valid = bvec3(mix(facing_neg, facing_pos, greaterThan(read_dir, ivec3(0))));
695
696 //sides
697 if (read_valid.x) {
698 ivec3 read_offset = local_offset + read_dir_x;
699 uint f = get_facing(read_offset);
700 if (f == 0) {
701 read_offset += region_offset;
702 if (all(greaterThanEqual(read_offset, ivec3(0))) && all(lessThan(read_offset, ivec3(params.grid_size)))) {
703 occ += imageLoad(dst_occlusion[params.occlusion_index], read_offset).r;
704 avg += 1.0;
705 }
706 }
707 }
708
709 if (read_valid.y) {
710 ivec3 read_offset = local_offset + read_dir_y;
711 uint f = get_facing(read_offset);
712 if (f == 0) {
713 read_offset += region_offset;
714 if (all(greaterThanEqual(read_offset, ivec3(0))) && all(lessThan(read_offset, ivec3(params.grid_size)))) {
715 occ += imageLoad(dst_occlusion[params.occlusion_index], read_offset).r;
716 avg += 1.0;
717 }
718 }
719 }
720
721 if (read_valid.z) {
722 ivec3 read_offset = local_offset + read_dir_z;
723 uint f = get_facing(read_offset);
724 if (f == 0) {
725 read_offset += region_offset;
726 if (all(greaterThanEqual(read_offset, ivec3(0))) && all(lessThan(read_offset, ivec3(params.grid_size)))) {
727 occ += imageLoad(dst_occlusion[params.occlusion_index], read_offset).r;
728 avg += 1.0;
729 }
730 }
731 }
732
733 //adjacents
734
735 if (all(read_valid.yz)) {
736 ivec3 read_offset = local_offset + read_dir_y + read_dir_z;
737 uint f = get_facing(read_offset);
738 if (f == 0) {
739 read_offset += region_offset;
740 if (all(greaterThanEqual(read_offset, ivec3(0))) && all(lessThan(read_offset, ivec3(params.grid_size)))) {
741 occ += imageLoad(dst_occlusion[params.occlusion_index], read_offset).r;
742 avg += 1.0;
743 }
744 }
745 }
746
747 if (all(read_valid.xz)) {
748 ivec3 read_offset = local_offset + read_dir_x + read_dir_z;
749 uint f = get_facing(read_offset);
750 if (f == 0) {
751 read_offset += region_offset;
752 if (all(greaterThanEqual(read_offset, ivec3(0))) && all(lessThan(read_offset, ivec3(params.grid_size)))) {
753 occ += imageLoad(dst_occlusion[params.occlusion_index], read_offset).r;
754 avg += 1.0;
755 }
756 }
757 }
758
759 if (all(read_valid.xy)) {
760 ivec3 read_offset = local_offset + read_dir_x + read_dir_y;
761 uint f = get_facing(read_offset);
762 if (f == 0) {
763 read_offset += region_offset;
764 if (all(greaterThanEqual(read_offset, ivec3(0))) && all(lessThan(read_offset, ivec3(params.grid_size)))) {
765 occ += imageLoad(dst_occlusion[params.occlusion_index], read_offset).r;
766 avg += 1.0;
767 }
768 }
769 }
770
771 //diagonal
772
773 if (all(read_valid)) {
774 ivec3 read_offset = local_offset + read_dir;
775 uint f = get_facing(read_offset);
776 if (f == 0) {
777 read_offset += region_offset;
778 if (all(greaterThanEqual(read_offset, ivec3(0))) && all(lessThan(read_offset, ivec3(params.grid_size)))) {
779 occ += imageLoad(dst_occlusion[params.occlusion_index], read_offset).r;
780 avg += 1.0;
781 }
782 }
783 }
784
785 if (avg > 0.0) {
786 occ /= avg;
787 }
788
789 imageStore(dst_occlusion[params.occlusion_index], offset, vec4(occ));
790 }
791 }
792 }
793 }
794
795#endif
796
797#if 1
798 groupMemoryBarrier();
799 barrier();
800
801 // There are no more barriers after this, so we can early return.
802 if (region_out_of_bounds) {
803 return;
804 }
805
806 for (int i = 0; i < 64; i++) {
807 ivec3 local_offset = local_ofs + ((ivec3(i) >> ivec3(0, 2, 4)) & ivec3(3, 3, 3));
808 ivec3 offset = region_offset + local_offset;
809
810 if (all(greaterThanEqual(offset, ivec3(0))) && all(lessThan(offset, ivec3(params.grid_size)))) {
811 uint facing = get_facing(local_offset);
812
813 if (facing == 0) {
814 ivec3 proc_pos = local_offset - ivec3(OCCLUSION_SIZE);
815 proc_pos += mix(ivec3(0), ivec3(1), greaterThanEqual(proc_pos, ivec3(0)));
816
817 ivec3 proc_abs = abs(proc_pos);
818
819 ivec3 read_dir = sign(proc_pos); //opposite direction
820 ivec3 read_dir_x = ivec3(read_dir.x, 0, 0);
821 ivec3 read_dir_y = ivec3(0, read_dir.y, 0);
822 ivec3 read_dir_z = ivec3(0, 0, read_dir.z);
823 //solid
824 uvec3 read_mask = mix(uvec3(1, 2, 4), uvec3(8, 16, 32), greaterThan(read_dir, ivec3(0))); //match positive with negative normals
825 uvec3 block_mask = mix(uvec3(1, 2, 4), uvec3(8, 16, 32), lessThan(read_dir, ivec3(0))); //match positive with negative normals
826
827 block_mask = uvec3(0);
828
829 float visible = 0.0;
830 float occlude_total = 0.0;
831
832 if (proc_abs.x < OCCLUSION_SIZE) {
833 ivec3 read_offset = local_offset + read_dir_x;
834 uint x_mask = get_facing(read_offset);
835 if (x_mask != 0) {
836 read_offset += region_offset;
837 if (all(greaterThanEqual(read_offset, ivec3(0))) && all(lessThan(read_offset, ivec3(params.grid_size)))) {
838 occlude_total += 1.0;
839 if (bool(x_mask & read_mask.x) && !bool(x_mask & block_mask.x)) {
840 visible += 1.0;
841 }
842 }
843 }
844 }
845
846 if (proc_abs.y < OCCLUSION_SIZE) {
847 ivec3 read_offset = local_offset + read_dir_y;
848 uint y_mask = get_facing(read_offset);
849 if (y_mask != 0) {
850 read_offset += region_offset;
851 if (all(greaterThanEqual(read_offset, ivec3(0))) && all(lessThan(read_offset, ivec3(params.grid_size)))) {
852 occlude_total += 1.0;
853 if (bool(y_mask & read_mask.y) && !bool(y_mask & block_mask.y)) {
854 visible += 1.0;
855 }
856 }
857 }
858 }
859
860 if (proc_abs.z < OCCLUSION_SIZE) {
861 ivec3 read_offset = local_offset + read_dir_z;
862 uint z_mask = get_facing(read_offset);
863 if (z_mask != 0) {
864 read_offset += region_offset;
865 if (all(greaterThanEqual(read_offset, ivec3(0))) && all(lessThan(read_offset, ivec3(params.grid_size)))) {
866 occlude_total += 1.0;
867 if (bool(z_mask & read_mask.z) && !bool(z_mask & block_mask.z)) {
868 visible += 1.0;
869 }
870 }
871 }
872 }
873
874 //if near the cartesian plane, test in opposite direction too
875
876 read_mask = mix(uvec3(1, 2, 4), uvec3(8, 16, 32), lessThan(read_dir, ivec3(0))); //match negative with positive normals
877 block_mask = mix(uvec3(1, 2, 4), uvec3(8, 16, 32), greaterThan(read_dir, ivec3(0))); //match negative with positive normals
878 block_mask = uvec3(0);
879
880 if (proc_abs.x == 1) {
881 ivec3 read_offset = local_offset - read_dir_x;
882 uint x_mask = get_facing(read_offset);
883 if (x_mask != 0) {
884 read_offset += region_offset;
885 if (all(greaterThanEqual(read_offset, ivec3(0))) && all(lessThan(read_offset, ivec3(params.grid_size)))) {
886 occlude_total += 1.0;
887 if (bool(x_mask & read_mask.x) && !bool(x_mask & block_mask.x)) {
888 visible += 1.0;
889 }
890 }
891 }
892 }
893
894 if (proc_abs.y == 1) {
895 ivec3 read_offset = local_offset - read_dir_y;
896 uint y_mask = get_facing(read_offset);
897 if (y_mask != 0) {
898 read_offset += region_offset;
899 if (all(greaterThanEqual(read_offset, ivec3(0))) && all(lessThan(read_offset, ivec3(params.grid_size)))) {
900 occlude_total += 1.0;
901 if (bool(y_mask & read_mask.y) && !bool(y_mask & block_mask.y)) {
902 visible += 1.0;
903 }
904 }
905 }
906 }
907
908 if (proc_abs.z == 1) {
909 ivec3 read_offset = local_offset - read_dir_z;
910 uint z_mask = get_facing(read_offset);
911 if (z_mask != 0) {
912 read_offset += region_offset;
913 if (all(greaterThanEqual(read_offset, ivec3(0))) && all(lessThan(read_offset, ivec3(params.grid_size)))) {
914 occlude_total += 1.0;
915 if (bool(z_mask & read_mask.z) && !bool(z_mask & block_mask.z)) {
916 visible += 1.0;
917 }
918 }
919 }
920 }
921
922 if (occlude_total > 0.0) {
923 float occ = imageLoad(dst_occlusion[params.occlusion_index], offset).r;
924 occ *= visible / occlude_total;
925 imageStore(dst_occlusion[params.occlusion_index], offset, vec4(occ));
926 }
927 }
928 }
929 }
930
931#endif
932
933 /*
934 for(int i=0;i<8;i++) {
935 ivec3 local_offset = local_pos + ((ivec3(i) >> ivec3(2,1,0)) & ivec3(1,1,1)) * OCCLUSION_SIZE;
936 ivec3 offset = local_offset - ivec3(OCCLUSION_SIZE); //looking around probe, so starts negative
937 offset += region * OCCLUSION_SIZE * 2; //offset by region
938 offset += params.probe_offset * OCCLUSION_SIZE; // offset by probe offset
939 if (all(greaterThanEqual(offset,ivec3(0))) && all(lessThan(offset,ivec3(params.grid_size)))) {
940 imageStore(dst_occlusion[params.occlusion_index],offset,vec4( occlusion_data[ to_linear(local_offset) ] ));
941 //imageStore(dst_occlusion[params.occlusion_index],offset,vec4( occlusion_solid[ to_linear(local_offset) ] ));
942 }
943 }
944*/
945
946#endif
947
948#ifdef MODE_STORE
949
950 ivec3 local = ivec3(gl_LocalInvocationID.xyz);
951 ivec3 pos = ivec3(gl_GlobalInvocationID.xyz);
952 // store SDF
953 uvec4 p = imageLoad(src_positions, pos);
954
955 bool solid = false;
956 float d;
957 if (ivec3(p.xyz) == pos) {
958 //solid block
959 d = 0;
960 solid = true;
961 } else {
962 //distance block
963 d = 1.0 + length(vec3(p.xyz) - vec3(pos));
964 }
965
966 d /= 255.0;
967
968 imageStore(dst_sdf, pos, vec4(d));
969
970 // STORE OCCLUSION
971
972 uint occlusion = 0;
973 const uint occlusion_shift[8] = uint[](12, 8, 4, 0, 28, 24, 20, 16);
974 for (int i = 0; i < 8; i++) {
975 float occ = imageLoad(src_occlusion[i], pos).r;
976 occlusion |= uint(clamp(occ * 15.0, 0.0, 15.0)) << occlusion_shift[i];
977 }
978 {
979 ivec3 occ_pos = pos;
980 occ_pos.z += params.cascade * params.grid_size;
981 imageStore(dst_occlusion, occ_pos, uvec4(occlusion & 0xFFFF));
982 occ_pos.x += params.grid_size;
983 imageStore(dst_occlusion, occ_pos, uvec4(occlusion >> 16));
984 }
985
986 // STORE POSITIONS
987
988 if (local == ivec3(0)) {
989 store_position_count = 0; //base one stores as zero, the others wait
990 }
991
992 groupMemoryBarrier();
993 barrier();
994
995 if (solid) {
996 uint index = atomicAdd(store_position_count, 1);
997 // At least do the conversion work in parallel
998 store_positions[index].position = uint(pos.x | (pos.y << 7) | (pos.z << 14));
999
1000 //see around which voxels point to this one, add them to the list
1001 uint bit_index = 0;
1002 uint neighbour_bits = 0;
1003 for (int i = -1; i <= 1; i++) {
1004 for (int j = -1; j <= 1; j++) {
1005 for (int k = -1; k <= 1; k++) {
1006 if (i == 0 && j == 0 && k == 0) {
1007 continue;
1008 }
1009 ivec3 npos = pos + ivec3(i, j, k);
1010 if (all(greaterThanEqual(npos, ivec3(0))) && all(lessThan(npos, ivec3(params.grid_size)))) {
1011 p = imageLoad(src_positions, npos);
1012 if (ivec3(p.xyz) == pos) {
1013 neighbour_bits |= (1 << bit_index);
1014 }
1015 }
1016 bit_index++;
1017 }
1018 }
1019 }
1020
1021 uint rgb = imageLoad(src_albedo, pos).r;
1022 uint facing = imageLoad(src_facing, pos).r;
1023
1024 store_positions[index].albedo = rgb >> 1; //store as it comes (555) to avoid precision loss (and move away the alpha bit)
1025 store_positions[index].albedo |= (facing & 0x3F) << 15; // store facing in bits 15-21
1026
1027 store_positions[index].albedo |= neighbour_bits << 21; //store lower 11 bits of neighbors with remaining albedo
1028 store_positions[index].position |= (neighbour_bits >> 11) << 21; //store 11 bits more of neighbors with position
1029
1030 store_positions[index].light = imageLoad(src_light, pos).r;
1031 store_positions[index].light_aniso = imageLoad(src_light_aniso, pos).r;
1032 //add neighbors
1033 store_positions[index].light |= (neighbour_bits >> 22) << 30; //store 2 bits more of neighbors with light
1034 store_positions[index].light_aniso |= (neighbour_bits >> 24) << 30; //store 2 bits more of neighbors with aniso
1035 }
1036
1037 groupMemoryBarrier();
1038 barrier();
1039
1040 // global increment only once per group, to reduce pressure
1041
1042 if (local == ivec3(0) && store_position_count > 0) {
1043 store_from_index = atomicAdd(dispatch_data.total_count, store_position_count);
1044 uint group_count = (store_from_index + store_position_count - 1) / 64 + 1;
1045 atomicMax(dispatch_data.x, group_count);
1046 }
1047
1048 groupMemoryBarrier();
1049 barrier();
1050
1051 uint read_index = uint(local.z * 4 * 4 + local.y * 4 + local.x);
1052 uint write_index = store_from_index + read_index;
1053
1054 if (read_index < store_position_count) {
1055 dst_process_voxels.data[write_index] = store_positions[read_index];
1056 }
1057
1058 if (pos == ivec3(0)) {
1059 //this thread clears y and z
1060 dispatch_data.y = 1;
1061 dispatch_data.z = 1;
1062 }
1063#endif
1064}