| 1 |
#[compute] |
| 2 |
|
| 3 |
#version 450 |
| 4 |
|
| 5 |
#VERSION_DEFINES |
| 6 |
|
| 7 |
layout(local_size_x = 4, local_size_y = 4, local_size_z = 4) in; |
| 8 |
|
| 9 |
#define MAX_DISTANCE 100000.0 |
| 10 |
|
| 11 |
#define NO_CHILDREN 0xFFFFFFFF |
| 12 |
|
| 13 |
struct CellChildren { |
| 14 |
uint children[8]; |
| 15 |
}; |
| 16 |
|
| 17 |
layout(set = 0, binding = 1, std430) buffer CellChildrenBuffer { |
| 18 |
CellChildren data[]; |
| 19 |
} |
| 20 |
cell_children; |
| 21 |
|
| 22 |
struct CellData { |
| 23 |
uint position; // xyz 10 bits |
| 24 |
uint albedo; //rgb albedo |
| 25 |
uint emission; //rgb normalized with e as multiplier |
| 26 |
uint normal; //RGB normal encoded |
| 27 |
}; |
| 28 |
|
| 29 |
layout(set = 0, binding = 2, std430) buffer CellDataBuffer { |
| 30 |
CellData data[]; |
| 31 |
} |
| 32 |
cell_data; |
| 33 |
|
| 34 |
layout(r8ui, set = 0, binding = 3) uniform restrict writeonly uimage3D sdf_tex; |
| 35 |
|
| 36 |
layout(push_constant, std430) uniform Params { |
| 37 |
uint offset; |
| 38 |
uint end; |
| 39 |
uint pad0; |
| 40 |
uint pad1; |
| 41 |
} |
| 42 |
params; |
| 43 |
|
| 44 |
void main() { |
| 45 |
vec3 pos = vec3(gl_GlobalInvocationID); |
| 46 |
float closest_dist = MAX_DISTANCE; |
| 47 |
|
| 48 |
for (uint i = params.offset; i < params.end; i++) { |
| 49 |
vec3 posu = vec3(uvec3(cell_data.data[i].position & 0x7FF, (cell_data.data[i].position >> 11) & 0x3FF, cell_data.data[i].position >> 21)); |
| 50 |
float dist = length(pos - posu); |
| 51 |
if (dist < closest_dist) { |
| 52 |
closest_dist = dist; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
uint dist_8; |
| 57 |
|
| 58 |
if (closest_dist < 0.0001) { // same cell |
| 59 |
dist_8 = 0; //equals to -1 |
| 60 |
} else { |
| 61 |
dist_8 = clamp(uint(closest_dist), 0, 254) + 1; //conservative, 0 is 1, so <1 is considered solid |
| 62 |
} |
| 63 |
|
| 64 |
imageStore(sdf_tex, ivec3(gl_GlobalInvocationID), uvec4(dist_8)); |
| 65 |
//imageStore(sdf_tex,pos,uvec4(pos*2,0)); |
| 66 |
} |
| 67 |
|
| 68 |
#if 0 |
| 69 |
layout(push_constant, std430) uniform Params { |
| 70 |
ivec3 limits; |
| 71 |
uint stack_size; |
| 72 |
} |
| 73 |
params; |
| 74 |
|
| 75 |
float distance_to_aabb(ivec3 pos, ivec3 aabb_pos, ivec3 aabb_size) { |
| 76 |
vec3 delta = vec3(max(ivec3(0), max(aabb_pos - pos, pos - (aabb_pos + aabb_size - ivec3(1))))); |
| 77 |
return length(delta); |
| 78 |
} |
| 79 |
|
| 80 |
void main() { |
| 81 |
ivec3 pos = ivec3(gl_GlobalInvocationID); |
| 82 |
|
| 83 |
uint stack[10] = uint[](0, 0, 0, 0, 0, 0, 0, 0, 0, 0); |
| 84 |
uint stack_indices[10] = uint[](0, 0, 0, 0, 0, 0, 0, 0, 0, 0); |
| 85 |
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)); |
| 86 |
|
| 87 |
const uint cell_orders[8] = uint[]( |
| 88 |
0x11f58d1, |
| 89 |
0xe2e70a, |
| 90 |
0xd47463, |
| 91 |
0xbb829c, |
| 92 |
0x8d11f5, |
| 93 |
0x70ae2e, |
| 94 |
0x463d47, |
| 95 |
0x29cbb8); |
| 96 |
|
| 97 |
bool cell_found = false; |
| 98 |
bool cell_found_exact = false; |
| 99 |
ivec3 closest_cell_pos; |
| 100 |
float closest_distance = MAX_DISTANCE; |
| 101 |
int stack_pos = 0; |
| 102 |
|
| 103 |
while (true) { |
| 104 |
uint index = stack_indices[stack_pos] >> 24; |
| 105 |
|
| 106 |
if (index == 8) { |
| 107 |
//go up |
| 108 |
if (stack_pos == 0) { |
| 109 |
break; //done going through octree |
| 110 |
} |
| 111 |
stack_pos--; |
| 112 |
continue; |
| 113 |
} |
| 114 |
|
| 115 |
stack_indices[stack_pos] = (stack_indices[stack_pos] & ((1 << 24) - 1)) | ((index + 1) << 24); |
| 116 |
|
| 117 |
uint cell_index = (stack_indices[stack_pos] >> (index * 3)) & 0x7; |
| 118 |
uint child_cell = cell_children.data[stack[stack_pos]].children[cell_index]; |
| 119 |
|
| 120 |
if (child_cell == NO_CHILDREN) { |
| 121 |
continue; |
| 122 |
} |
| 123 |
|
| 124 |
ivec3 child_cell_size = params.limits >> (stack_pos + 1); |
| 125 |
ivec3 child_cell_pos = stack_positions[stack_pos]; |
| 126 |
|
| 127 |
child_cell_pos += mix(ivec3(0), child_cell_size, bvec3(uvec3(index & 1, index & 2, index & 4) != uvec3(0))); |
| 128 |
|
| 129 |
bool is_leaf = stack_pos == (params.stack_size - 2); |
| 130 |
|
| 131 |
if (child_cell_pos == pos && is_leaf) { |
| 132 |
//we may actually end up in the exact cell. |
| 133 |
//if this happens, just abort |
| 134 |
cell_found_exact = true; |
| 135 |
break; |
| 136 |
} |
| 137 |
|
| 138 |
if (cell_found) { |
| 139 |
//discard by distance |
| 140 |
float distance = distance_to_aabb(pos, child_cell_pos, child_cell_size); |
| 141 |
if (distance >= closest_distance) { |
| 142 |
continue; //pointless, just test next child |
| 143 |
} else if (is_leaf) { |
| 144 |
//closer than what we have AND end of stack, save and continue |
| 145 |
closest_cell_pos = child_cell_pos; |
| 146 |
closest_distance = distance; |
| 147 |
continue; |
| 148 |
} |
| 149 |
} else if (is_leaf) { |
| 150 |
//first solid cell we find, save and continue |
| 151 |
closest_distance = distance_to_aabb(pos, child_cell_pos, child_cell_size); |
| 152 |
closest_cell_pos = child_cell_pos; |
| 153 |
cell_found = true; |
| 154 |
continue; |
| 155 |
} |
| 156 |
|
| 157 |
bvec3 direction = greaterThan((pos - (child_cell_pos + (child_cell_size >> 1))), ivec3(0)); |
| 158 |
uint cell_order = 0; |
| 159 |
cell_order |= mix(0, 1, direction.x); |
| 160 |
cell_order |= mix(0, 2, direction.y); |
| 161 |
cell_order |= mix(0, 4, direction.z); |
| 162 |
|
| 163 |
stack[stack_pos + 1] = child_cell; |
| 164 |
stack_indices[stack_pos + 1] = cell_orders[cell_order]; //start counting |
| 165 |
stack_positions[stack_pos + 1] = child_cell_pos; |
| 166 |
stack_pos++; //go up stack |
| 167 |
} |
| 168 |
|
| 169 |
uint dist_8; |
| 170 |
|
| 171 |
if (cell_found_exact) { |
| 172 |
dist_8 = 0; //equals to -1 |
| 173 |
} else { |
| 174 |
float closest_distance = length(vec3(pos - closest_cell_pos)); |
| 175 |
dist_8 = clamp(uint(closest_distance), 0, 254) + 1; //conservative, 0 is 1, so <1 is considered solid |
| 176 |
} |
| 177 |
|
| 178 |
imageStore(sdf_tex, pos, uvec4(dist_8)); |
| 179 |
} |
| 180 |
#endif |