|
|
|
@@ -25,21 +25,30 @@ var<storage, read> aabb_buffer: AabbBuffer;
|
|
|
|
|
var spatial_texture: texture_3d<f32>;
|
|
|
|
|
@group(0) @binding(3)
|
|
|
|
|
var tiletree: texture_storage_3d<rgb10a2unorm, write>;
|
|
|
|
|
//@group(0) @binding(4)
|
|
|
|
|
//var tiletree_pointer: atomic<u32>;
|
|
|
|
|
|
|
|
|
|
const SOLVE_ARRAY_LEN: u32 = 16*16*16;
|
|
|
|
|
|
|
|
|
|
const INFINITE_LOOP_LIMIT: i32 = 512;
|
|
|
|
|
const SUBDIVIDE_LOOPS: i32 = 2;
|
|
|
|
|
|
|
|
|
|
var<workgroup> to_solve: array<u32, SOLVE_ARRAY_LEN/2>;
|
|
|
|
|
var<workgroup> low_head: atomic<u32>;
|
|
|
|
|
var<workgroup> high_head: atomic<u32>;
|
|
|
|
|
const SOLVE_ARRAY_LEN: u32 = 16 * 16 * 16;
|
|
|
|
|
const OCTREE_LEN: u32 = SOLVE_ARRAY_LEN * 6;
|
|
|
|
|
|
|
|
|
|
struct Octree {
|
|
|
|
|
bitmask: array<u32, OCTREE_LEN / 32>,
|
|
|
|
|
summary_0: array<u32, OCTREE_LEN / 32 / 2>,
|
|
|
|
|
summary_1: array<u32, OCTREE_LEN / 32 / 32>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var<workgroup> octree: Octree;
|
|
|
|
|
|
|
|
|
|
var<immediate> push_constants: PushConstants;
|
|
|
|
|
|
|
|
|
|
struct ComputeInput {
|
|
|
|
|
@builtin(workgroup_id) workgroup_id: vec3u,
|
|
|
|
|
@builtin(local_invocation_id) local_invocation_id: vec3u,
|
|
|
|
|
@builtin(num_subgroups) num_subgroups: u32,
|
|
|
|
|
@builtin(subgroup_id) subgroup_id: u32,
|
|
|
|
|
@builtin(subgroup_invocation_id) subgroup_invocation_id: u32,
|
|
|
|
|
@builtin(subgroup_size) subgroup_size: u32,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn in_aabb(metadata: TapeMetadata, internal_pos: vec3f) -> bool {
|
|
|
|
@@ -54,72 +63,47 @@ fn in_aabb(metadata: TapeMetadata, internal_pos: vec3f) -> bool {
|
|
|
|
|
return (interval.x < 0) && (interval.y > 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn get_solve_array(index: u32) -> u32 {
|
|
|
|
|
if (index % 2 == 0) {
|
|
|
|
|
return to_solve[index / 2] & 0x0000FFFFu;
|
|
|
|
|
} else {
|
|
|
|
|
return to_solve[index / 2] >> 16u;
|
|
|
|
|
fn init_octree() {
|
|
|
|
|
let stride = get_stride(3);
|
|
|
|
|
octree.bitmask[(stride * 0) >> 5] = 1;
|
|
|
|
|
octree.bitmask[(stride * 1) >> 5] = 1;
|
|
|
|
|
octree.bitmask[(stride * 2) >> 5] = 1;
|
|
|
|
|
octree.bitmask[(stride * 3) >> 5] = 1;
|
|
|
|
|
octree.bitmask[(stride * 4) >> 5] = 1;
|
|
|
|
|
octree.bitmask[(stride * 5) >> 5] = 1;
|
|
|
|
|
octree.bitmask[(stride * 6) >> 5] = 1;
|
|
|
|
|
octree.bitmask[(stride * 7) >> 5] = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn get_stride(level: u32) -> u32 {
|
|
|
|
|
return encode_internal_pos(vec3u(1u << level, 0, 0));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn write_work_item_level_0(subgroup_width: u32, cursor: u32, en: bool) {
|
|
|
|
|
let ballot = subgroupBallot(en);
|
|
|
|
|
if subgroup_elect() {
|
|
|
|
|
octree.bitmask[cursor >> 5] = ballot.x;
|
|
|
|
|
if subgroup_width > 32 {
|
|
|
|
|
octree.bitmask[(cursor >> 5) + 1] = ballot.y;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn set_solve_array_pair(index: u32, value_low: u32, value_high: u32) {
|
|
|
|
|
to_solve[index / 2] = (value_low & 0x0000FFFFu) | (value_high << 16u);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn create_work_item_pair(increasing: bool, subdivision_log2: u32, uinternal_pos: vec3u) {
|
|
|
|
|
var index: u32;
|
|
|
|
|
if (increasing) {
|
|
|
|
|
index = atomicAdd(&low_head, 2);
|
|
|
|
|
} else {
|
|
|
|
|
index = atomicSub(&high_head, 2);
|
|
|
|
|
fn write_work_item(subgroup_width: u32, stride: u32, cursor: u32, en: u32) {
|
|
|
|
|
if stride == 1 {
|
|
|
|
|
write_work_item_level_0(subgroup_width, cursor, en != 0);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var pos = encode_internal_pos(uinternal_pos, subdivision_log2);
|
|
|
|
|
set_solve_array_pair(index, pos, pos + 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn create_work_items_batch(increasing: bool, subdivision_log2: u32, uinternal_pos_local: vec3u) {
|
|
|
|
|
var index: u32;
|
|
|
|
|
var direction: i32;
|
|
|
|
|
if (increasing) {
|
|
|
|
|
index = atomicAdd(&low_head, 8);
|
|
|
|
|
direction = 1;
|
|
|
|
|
} else {
|
|
|
|
|
index = atomicSub(&high_head, 8);
|
|
|
|
|
direction = -1;
|
|
|
|
|
}
|
|
|
|
|
set_solve_array_pair(u32(i32(index) + (0 * direction)),
|
|
|
|
|
encode_internal_pos(uinternal_pos_local + vec3(0,0,0), subdivision_log2),
|
|
|
|
|
encode_internal_pos(uinternal_pos_local + vec3(1,0,0), subdivision_log2)
|
|
|
|
|
);
|
|
|
|
|
set_solve_array_pair(u32(i32(index) + (2 * direction)),
|
|
|
|
|
encode_internal_pos(uinternal_pos_local + vec3(0,1,0), subdivision_log2),
|
|
|
|
|
encode_internal_pos(uinternal_pos_local + vec3(1,1,0), subdivision_log2)
|
|
|
|
|
);
|
|
|
|
|
set_solve_array_pair(u32(i32(index) + (4 * direction)),
|
|
|
|
|
encode_internal_pos(uinternal_pos_local + vec3(0,0,1), subdivision_log2),
|
|
|
|
|
encode_internal_pos(uinternal_pos_local + vec3(1,0,1), subdivision_log2)
|
|
|
|
|
);
|
|
|
|
|
set_solve_array_pair(u32(i32(index) + (6 * direction)),
|
|
|
|
|
encode_internal_pos(uinternal_pos_local + vec3(0,1,1), subdivision_log2),
|
|
|
|
|
encode_internal_pos(uinternal_pos_local + vec3(1,1,1), subdivision_log2)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn get_work_item(increasing: bool) -> u32 {
|
|
|
|
|
var index: u32;
|
|
|
|
|
if (increasing) {
|
|
|
|
|
index = atomicAdd(&high_head, 1) + 1;
|
|
|
|
|
} else {
|
|
|
|
|
index = atomicSub(&low_head, 1) - 1;
|
|
|
|
|
}
|
|
|
|
|
return index;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn reset_stack(increasing: bool) {
|
|
|
|
|
if (increasing) {
|
|
|
|
|
atomicStore(&low_head, 0);
|
|
|
|
|
} else {
|
|
|
|
|
atomicStore(&high_head, SOLVE_ARRAY_LEN - 1);
|
|
|
|
|
var working_write = 0u;
|
|
|
|
|
var cursor_head = cursor;
|
|
|
|
|
for (var i = 0; i < 8; i++) {
|
|
|
|
|
let bit_pos = (cursor_head & 31);
|
|
|
|
|
working_write |= (en << bit_pos);
|
|
|
|
|
if bit_pos + stride > 31 {
|
|
|
|
|
octree.bitmask[cursor_head >> 5] = working_write;
|
|
|
|
|
working_write = 0;
|
|
|
|
|
}
|
|
|
|
|
cursor_head += stride;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -129,13 +113,18 @@ fn subgroup_elect() -> bool {
|
|
|
|
|
return subgroupBroadcastFirst(index) == index;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@compute @workgroup_size(4, 4, 4)
|
|
|
|
|
fn workgroup_elect(subgroup_id: u32) -> bool {
|
|
|
|
|
return subgroup_id == 0 && subgroup_elect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const WORKGROUP_SIZE = vec3u(4, 4, 4);
|
|
|
|
|
@compute @workgroup_size(4 * 4 * 4)
|
|
|
|
|
fn main(compute_input: ComputeInput) {
|
|
|
|
|
var metadata = tape_metadata_buffer.metadata[push_constants.metadata_buffer_offset];
|
|
|
|
|
|
|
|
|
|
var exit = 0;
|
|
|
|
|
if subgroup_elect() {
|
|
|
|
|
let base_index = encode_internal_pos(compute_input.workgroup_id, metadata.subdivision_log2);
|
|
|
|
|
let base_index = encode_internal_pos(compute_input.workgroup_id);
|
|
|
|
|
let aabb = &aabb_buffer.aabbs[push_constants.aabb_buffer_offset + base_index];
|
|
|
|
|
if bitcast<u32>(aabb.min_x) == 0xFFC00000u {
|
|
|
|
|
exit = 1;
|
|
|
|
@@ -146,73 +135,69 @@ fn main(compute_input: ComputeInput) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (workgroup_elect(compute_input.subgroup_id)) {
|
|
|
|
|
init_octree();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let global_offset = compute_input.workgroup_id * 16;
|
|
|
|
|
metadata.subdivision_log2 += 2;
|
|
|
|
|
|
|
|
|
|
var rolling_subdivision = 2u;
|
|
|
|
|
var global_offset = compute_input.workgroup_id * vec3(4, 4, 4);
|
|
|
|
|
let subgroup_stride = SOLVE_ARRAY_LEN / compute_input.num_subgroups;
|
|
|
|
|
|
|
|
|
|
var increasing = false;
|
|
|
|
|
|
|
|
|
|
if (subgroup_elect()) {
|
|
|
|
|
reset_stack(increasing);
|
|
|
|
|
}
|
|
|
|
|
workgroupBarrier();
|
|
|
|
|
|
|
|
|
|
if (compute_input.local_invocation_id.x % 2 == 0) {
|
|
|
|
|
create_work_item_pair(increasing, rolling_subdivision, compute_input.local_invocation_id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
increasing = !increasing;
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < SUBDIVIDE_LOOPS; i++) {
|
|
|
|
|
workgroupBarrier();
|
|
|
|
|
if (subgroup_elect()) {
|
|
|
|
|
reset_stack(increasing);
|
|
|
|
|
}
|
|
|
|
|
for (var level = 3; level >= 0; level--) {
|
|
|
|
|
workgroupBarrier();
|
|
|
|
|
let level_stride = get_stride(u32(level));
|
|
|
|
|
|
|
|
|
|
for (var infini = 0; infini < INFINITE_LOOP_LIMIT; infini++) {
|
|
|
|
|
let item = get_work_item(increasing);
|
|
|
|
|
if (item >= SOLVE_ARRAY_LEN) {
|
|
|
|
|
break;
|
|
|
|
|
let cursor_start = (subgroup_stride * compute_input.subgroup_id) + (level_stride * compute_input.subgroup_invocation_id);
|
|
|
|
|
let cursor_end = (subgroup_stride * (compute_input.subgroup_id + 1));
|
|
|
|
|
let cursor_stride = (level_stride * compute_input.subgroup_size);
|
|
|
|
|
|
|
|
|
|
if level > 0 {
|
|
|
|
|
for (var cursor = cursor_start ; cursor < cursor_end; cursor += cursor_stride) {
|
|
|
|
|
var solve_item = 0u;
|
|
|
|
|
if cursor < SOLVE_ARRAY_LEN {
|
|
|
|
|
solve_item = (octree.bitmask[cursor >> 5] >> (cursor & 31)) & 1;
|
|
|
|
|
if solve_item != 0 {
|
|
|
|
|
let uinternal_pos_local = decode_internal_pos(cursor);
|
|
|
|
|
let uinternal_pos_global = ((uinternal_pos_local + global_offset) / (1u << u32(level + 1))) ;
|
|
|
|
|
solve_item = u32(in_aabb(metadata, vec3f(uinternal_pos_global)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
write_work_item(compute_input.subgroup_size, level_stride / 8, cursor, solve_item);
|
|
|
|
|
}
|
|
|
|
|
let solve_item = get_solve_array(item);
|
|
|
|
|
let uinternal_pos_local = decode_internal_pos(solve_item, rolling_subdivision);
|
|
|
|
|
let uinternal_pos_global = uinternal_pos_local + global_offset;
|
|
|
|
|
let aabb = in_aabb(metadata, vec3f(uinternal_pos_global));
|
|
|
|
|
if (aabb) {
|
|
|
|
|
create_work_items_batch(increasing, rolling_subdivision + 1, uinternal_pos_local * 2);
|
|
|
|
|
if level > 1 {
|
|
|
|
|
metadata.subdivision_log2 += 1;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
for (var cursor = cursor_start ; cursor < cursor_end; cursor += cursor_stride) {
|
|
|
|
|
if cursor < SOLVE_ARRAY_LEN {
|
|
|
|
|
let solve_item = (octree.bitmask[cursor >> 5] >> (cursor & 31)) & 1;
|
|
|
|
|
let uinternal_pos_local = decode_internal_pos(cursor);
|
|
|
|
|
let uinternal_pos_global = uinternal_pos_local + global_offset;
|
|
|
|
|
let position = internal_to_external_pos(vec3f(uinternal_pos_global), metadata);
|
|
|
|
|
|
|
|
|
|
let leaf_face_mask = solve_item * 0xb111111u;
|
|
|
|
|
|
|
|
|
|
for (var i = 0u; i < 6; i++) {
|
|
|
|
|
write_work_item_level_0(compute_input.subgroup_size, cursor + (SOLVE_ARRAY_LEN * i), (leaf_face_mask & (1u << i)) > 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if solve_item != 0 {
|
|
|
|
|
//let grad = gradient_scene(vec4(position, metadata.time));
|
|
|
|
|
|
|
|
|
|
const EPSILON = 0.001;
|
|
|
|
|
|
|
|
|
|
let n = vec3(
|
|
|
|
|
scene(vec4(position.x + EPSILON, position.y, position.z, metadata.time)),
|
|
|
|
|
scene(vec4(position.x, position.y + EPSILON, position.z, metadata.time)),
|
|
|
|
|
scene(vec4(position.x, position.y, position.z + EPSILON, metadata.time)),
|
|
|
|
|
);
|
|
|
|
|
let grad = normalize(n - (scene(vec4(position, metadata.time))));
|
|
|
|
|
|
|
|
|
|
textureStore(tiletree, uinternal_pos_global, vec4(grad * 0.5 + 0.5, 1.0));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
metadata.subdivision_log2 += 1;
|
|
|
|
|
rolling_subdivision += 1;
|
|
|
|
|
global_offset *= 2;
|
|
|
|
|
increasing = !increasing;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
workgroupBarrier();
|
|
|
|
|
|
|
|
|
|
for (var infini = 0; infini < INFINITE_LOOP_LIMIT; infini++) {
|
|
|
|
|
let item = get_work_item(increasing);
|
|
|
|
|
if (item >= SOLVE_ARRAY_LEN) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
let solve_item = get_solve_array(item);
|
|
|
|
|
let uinternal_pos_local = decode_internal_pos(solve_item, rolling_subdivision);
|
|
|
|
|
let uinternal_pos_global = uinternal_pos_local + global_offset;
|
|
|
|
|
let position = internal_to_external_pos(vec3f(uinternal_pos_global), metadata);
|
|
|
|
|
//let grad = gradient_scene(vec4(position, metadata.time));
|
|
|
|
|
|
|
|
|
|
const EPSILON = 0.001;
|
|
|
|
|
|
|
|
|
|
let n = vec3(
|
|
|
|
|
scene(vec4(position.x + EPSILON, position.y, position.z, metadata.time)),
|
|
|
|
|
scene(vec4(position.x, position.y + EPSILON, position.z, metadata.time)),
|
|
|
|
|
scene(vec4(position.x, position.y, position.z + EPSILON, metadata.time)),
|
|
|
|
|
);
|
|
|
|
|
let grad = normalize(n - (scene(vec4(position, metadata.time))));
|
|
|
|
|
|
|
|
|
|
textureStore(tiletree, uinternal_pos_global, vec4(grad * 0.5 + 0.5, 0.0));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|