2020-05-18 08:56:22 +00:00
|
|
|
#[compute]
|
2019-10-11 02:14:56 +00:00
|
|
|
|
|
|
|
#version 450
|
|
|
|
|
2021-04-13 20:01:43 +00:00
|
|
|
#VERSION_DEFINES
|
2019-10-11 02:14:56 +00:00
|
|
|
|
|
|
|
layout(local_size_x = 4, local_size_y = 4, local_size_z = 4) in;
|
|
|
|
|
2021-11-28 21:51:59 +00:00
|
|
|
#define MAX_DISTANCE 100000.0
|
2019-10-11 02:14:56 +00:00
|
|
|
|
|
|
|
#define NO_CHILDREN 0xFFFFFFFF
|
|
|
|
|
|
|
|
struct CellChildren {
|
|
|
|
uint children[8];
|
|
|
|
};
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
layout(set = 0, binding = 1, std430) buffer CellChildrenBuffer {
|
|
|
|
CellChildren data[];
|
2020-02-11 13:01:43 +00:00
|
|
|
}
|
|
|
|
cell_children;
|
2019-10-11 02:14:56 +00:00
|
|
|
|
|
|
|
struct CellData {
|
|
|
|
uint position; // xyz 10 bits
|
|
|
|
uint albedo; //rgb albedo
|
|
|
|
uint emission; //rgb normalized with e as multiplier
|
|
|
|
uint normal; //RGB normal encoded
|
|
|
|
};
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
layout(set = 0, binding = 2, std430) buffer CellDataBuffer {
|
|
|
|
CellData data[];
|
2020-02-11 13:01:43 +00:00
|
|
|
}
|
|
|
|
cell_data;
|
2019-10-11 02:14:56 +00:00
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
layout(r8ui, set = 0, binding = 3) uniform restrict writeonly uimage3D sdf_tex;
|
2019-10-11 02:14:56 +00:00
|
|
|
|
2022-02-11 17:40:24 +00:00
|
|
|
layout(push_constant, std430) uniform Params {
|
2019-10-11 02:14:56 +00:00
|
|
|
uint offset;
|
|
|
|
uint end;
|
|
|
|
uint pad0;
|
|
|
|
uint pad1;
|
2020-02-11 13:01:43 +00:00
|
|
|
}
|
|
|
|
params;
|
2019-10-11 02:14:56 +00:00
|
|
|
|
|
|
|
void main() {
|
|
|
|
vec3 pos = vec3(gl_GlobalInvocationID);
|
2021-11-28 21:51:59 +00:00
|
|
|
float closest_dist = MAX_DISTANCE;
|
2019-10-11 02:14:56 +00:00
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
for (uint i = params.offset; i < params.end; i++) {
|
|
|
|
vec3 posu = vec3(uvec3(cell_data.data[i].position & 0x7FF, (cell_data.data[i].position >> 11) & 0x3FF, cell_data.data[i].position >> 21));
|
|
|
|
float dist = length(pos - posu);
|
2019-10-11 02:14:56 +00:00
|
|
|
if (dist < closest_dist) {
|
|
|
|
closest_dist = dist;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint dist_8;
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
if (closest_dist < 0.0001) { // same cell
|
|
|
|
dist_8 = 0; //equals to -1
|
2019-10-11 02:14:56 +00:00
|
|
|
} else {
|
2019-11-05 11:01:00 +00:00
|
|
|
dist_8 = clamp(uint(closest_dist), 0, 254) + 1; //conservative, 0 is 1, so <1 is considered solid
|
2019-10-11 02:14:56 +00:00
|
|
|
}
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
imageStore(sdf_tex, ivec3(gl_GlobalInvocationID), uvec4(dist_8));
|
2019-10-11 02:14:56 +00:00
|
|
|
//imageStore(sdf_tex,pos,uvec4(pos*2,0));
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
2022-02-11 17:40:24 +00:00
|
|
|
layout(push_constant, std430) uniform Params {
|
2019-10-11 02:14:56 +00:00
|
|
|
ivec3 limits;
|
|
|
|
uint stack_size;
|
2020-05-18 08:56:22 +00:00
|
|
|
}
|
|
|
|
params;
|
2019-10-11 02:14:56 +00:00
|
|
|
|
|
|
|
float distance_to_aabb(ivec3 pos, ivec3 aabb_pos, ivec3 aabb_size) {
|
2019-11-05 11:01:00 +00:00
|
|
|
vec3 delta = vec3(max(ivec3(0), max(aabb_pos - pos, pos - (aabb_pos + aabb_size - ivec3(1)))));
|
2019-10-11 02:14:56 +00:00
|
|
|
return length(delta);
|
|
|
|
}
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
ivec3 pos = ivec3(gl_GlobalInvocationID);
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
uint stack[10] = uint[](0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
|
|
|
uint stack_indices[10] = uint[](0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
|
|
|
ivec3 stack_positions[10] = ivec3[](ivec3(0), ivec3(0), ivec3(0), ivec3(0), ivec3(0), ivec3(0), ivec3(0), ivec3(0), ivec3(0), ivec3(0));
|
|
|
|
|
|
|
|
const uint cell_orders[8] = uint[](
|
|
|
|
0x11f58d1,
|
|
|
|
0xe2e70a,
|
|
|
|
0xd47463,
|
|
|
|
0xbb829c,
|
|
|
|
0x8d11f5,
|
|
|
|
0x70ae2e,
|
|
|
|
0x463d47,
|
|
|
|
0x29cbb8);
|
2019-10-11 02:14:56 +00:00
|
|
|
|
|
|
|
bool cell_found = false;
|
|
|
|
bool cell_found_exact = false;
|
|
|
|
ivec3 closest_cell_pos;
|
|
|
|
float closest_distance = MAX_DISTANCE;
|
|
|
|
int stack_pos = 0;
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
while (true) {
|
|
|
|
uint index = stack_indices[stack_pos] >> 24;
|
2019-10-11 02:14:56 +00:00
|
|
|
|
|
|
|
if (index == 8) {
|
|
|
|
//go up
|
2019-11-05 11:01:00 +00:00
|
|
|
if (stack_pos == 0) {
|
2019-10-11 02:14:56 +00:00
|
|
|
break; //done going through octree
|
|
|
|
}
|
|
|
|
stack_pos--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
stack_indices[stack_pos] = (stack_indices[stack_pos] & ((1 << 24) - 1)) | ((index + 1) << 24);
|
2019-10-11 02:14:56 +00:00
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
uint cell_index = (stack_indices[stack_pos] >> (index * 3)) & 0x7;
|
2019-10-11 02:14:56 +00:00
|
|
|
uint child_cell = cell_children.data[stack[stack_pos]].children[cell_index];
|
|
|
|
|
|
|
|
if (child_cell == NO_CHILDREN) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
ivec3 child_cell_size = params.limits >> (stack_pos + 1);
|
2019-10-11 02:14:56 +00:00
|
|
|
ivec3 child_cell_pos = stack_positions[stack_pos];
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
child_cell_pos += mix(ivec3(0), child_cell_size, bvec3(uvec3(index & 1, index & 2, index & 4) != uvec3(0)));
|
2019-10-11 02:14:56 +00:00
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
bool is_leaf = stack_pos == (params.stack_size - 2);
|
2019-10-11 02:14:56 +00:00
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
if (child_cell_pos == pos && is_leaf) {
|
2019-10-11 02:14:56 +00:00
|
|
|
//we may actually end up in the exact cell.
|
|
|
|
//if this happens, just abort
|
2019-11-05 11:01:00 +00:00
|
|
|
cell_found_exact = true;
|
2019-10-11 02:14:56 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cell_found) {
|
|
|
|
//discard by distance
|
2019-11-05 11:01:00 +00:00
|
|
|
float distance = distance_to_aabb(pos, child_cell_pos, child_cell_size);
|
2019-10-11 02:14:56 +00:00
|
|
|
if (distance >= closest_distance) {
|
|
|
|
continue; //pointless, just test next child
|
|
|
|
} else if (is_leaf) {
|
|
|
|
//closer than what we have AND end of stack, save and continue
|
|
|
|
closest_cell_pos = child_cell_pos;
|
|
|
|
closest_distance = distance;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else if (is_leaf) {
|
|
|
|
//first solid cell we find, save and continue
|
2019-11-05 11:01:00 +00:00
|
|
|
closest_distance = distance_to_aabb(pos, child_cell_pos, child_cell_size);
|
2019-10-11 02:14:56 +00:00
|
|
|
closest_cell_pos = child_cell_pos;
|
2019-11-05 11:01:00 +00:00
|
|
|
cell_found = true;
|
2019-10-11 02:14:56 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
bvec3 direction = greaterThan((pos - (child_cell_pos + (child_cell_size >> 1))), ivec3(0));
|
2019-10-11 02:14:56 +00:00
|
|
|
uint cell_order = 0;
|
2019-11-05 11:01:00 +00:00
|
|
|
cell_order |= mix(0, 1, direction.x);
|
|
|
|
cell_order |= mix(0, 2, direction.y);
|
|
|
|
cell_order |= mix(0, 4, direction.z);
|
2019-10-11 02:14:56 +00:00
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
stack[stack_pos + 1] = child_cell;
|
|
|
|
stack_indices[stack_pos + 1] = cell_orders[cell_order]; //start counting
|
|
|
|
stack_positions[stack_pos + 1] = child_cell_pos;
|
2019-10-11 02:14:56 +00:00
|
|
|
stack_pos++; //go up stack
|
|
|
|
}
|
|
|
|
|
|
|
|
uint dist_8;
|
|
|
|
|
|
|
|
if (cell_found_exact) {
|
2019-11-05 11:01:00 +00:00
|
|
|
dist_8 = 0; //equals to -1
|
2019-10-11 02:14:56 +00:00
|
|
|
} else {
|
2019-11-05 11:01:00 +00:00
|
|
|
float closest_distance = length(vec3(pos - closest_cell_pos));
|
|
|
|
dist_8 = clamp(uint(closest_distance), 0, 254) + 1; //conservative, 0 is 1, so <1 is considered solid
|
2019-10-11 02:14:56 +00:00
|
|
|
}
|
|
|
|
|
2019-11-05 11:01:00 +00:00
|
|
|
imageStore(sdf_tex, pos, uvec4(dist_8));
|
2019-10-11 02:14:56 +00:00
|
|
|
}
|
|
|
|
#endif
|