diff --git a/crates/bevy_tape_drive_solari/src/scene/tapes/blas.rs b/crates/bevy_tape_drive_solari/src/scene/tapes/blas.rs index 92c0a51..fcf98de 100644 --- a/crates/bevy_tape_drive_solari/src/scene/tapes/blas.rs +++ b/crates/bevy_tape_drive_solari/src/scene/tapes/blas.rs @@ -350,9 +350,9 @@ pub fn compile_raytracing_blas( compute_pass.set_pipeline(tiletree_pipeline); compute_pass.set_bind_group(0, &tiletree_bind_group, &[]); compute_pass.dispatch_workgroups( - (tape.subdivision / 4) * 128, - (tape.subdivision / 4) * 128, - (tape.subdivision / 4) * 6, + tape.subdivision, + tape.subdivision, + tape.subdivision, ); updated_blases.push(key.tape); diff --git a/crates/bevy_tape_load/src/render/allocator.rs b/crates/bevy_tape_load/src/render/allocator.rs index 47acf1a..ed7965f 100644 --- a/crates/bevy_tape_load/src/render/allocator.rs +++ b/crates/bevy_tape_load/src/render/allocator.rs @@ -417,9 +417,9 @@ impl TapeAllocator { sample_count: 1, view_formats: &[TextureFormat::Rgb10a2Unorm], size: Extent3d { - width: tape.subdivision * 128, - height: tape.subdivision * 128, - depth_or_array_layers: tape.subdivision * 6, + width: tape.subdivision * 4 * 2 * 2, + height: tape.subdivision * 4 * 2 * 2, + depth_or_array_layers: tape.subdivision * 4 * 2 * 2, }, }); let texture_view = texture.create_view(&TextureViewDescriptor { diff --git a/crates/bevy_tape_load/src/render/tiletree.wesl b/crates/bevy_tape_load/src/render/tiletree.wesl index 20b2e30..1904b0c 100644 --- a/crates/bevy_tape_load/src/render/tiletree.wesl +++ b/crates/bevy_tape_load/src/render/tiletree.wesl @@ -1,7 +1,7 @@ import package::render::aabb::PackedAabb; import package::render::metadata_types::TapeMetadata; import package::render::implicit::{interval_scene, scene, gradient_scene}; -import package::render::util::{NAN, encode_internal_pos, internal_to_external_pos}; +import package::render::util::{NAN, decode_internal_pos, encode_internal_pos, internal_to_external_pos}; import package::render::spatial_implicit::{SPATIAL_SCENE_OFFSET, INVERSE_SPATIAL_SCENE_SCALE}; enable wgpu_binding_array; @@ -26,49 +26,193 @@ var spatial_texture: texture_3d; @group(0) @binding(3) var tiletree: texture_storage_3d; +const SOLVE_ARRAY_LEN: u32 = 16*16*16; + +const INFINITE_LOOP_LIMIT: i32 = 512; +const SUBDIVIDE_LOOPS: i32 = 2; + +var to_solve: array; +var low_head: atomic; +var high_head: atomic; + var push_constants: PushConstants; struct ComputeInput { @builtin(workgroup_id) workgroup_id: vec3u, - @builtin(local_invocation_id) local_invocaion_id: vec3u, - @builtin(global_invocation_id) global_invocation_id: vec3u + @builtin(local_invocation_id) local_invocation_id: vec3u, +} + +fn in_aabb(metadata: TapeMetadata, internal_pos: vec3f) -> bool { + let low_corner = internal_to_external_pos(internal_pos + vec3(0.0), metadata); + let high_corner = internal_to_external_pos(internal_pos + vec3(1.0), metadata); + + let interval = interval_scene( + vec4(low_corner, metadata.time), + vec4(high_corner, metadata.time), + ); + + 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 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); + } + 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); + } +} + +// subgroupElect is not implemented in naga +fn subgroup_elect() -> bool { + let index = subgroupExclusiveAdd(1); + return subgroupBroadcastFirst(index) == index; } @compute @workgroup_size(4, 4, 4) fn main(compute_input: ComputeInput) { var metadata = tape_metadata_buffer.metadata[push_constants.metadata_buffer_offset]; - let uinternal_pos = compute_input.global_invocation_id; - let internal_pos = vec3f(uinternal_pos); - - // subgroupElect is not implemented in naga - //let index = subgroupExclusiveAdd(1); - //let elect = subgroupBroadcastFirst(index) == index; - var exit = 0; - //if elect { - let base_index = encode_internal_pos(uinternal_pos / vec3u(128, 128, 6), metadata.subdivision_log2); + if subgroup_elect() { + let base_index = encode_internal_pos(compute_input.workgroup_id, metadata.subdivision_log2); let aabb = &aabb_buffer.aabbs[push_constants.aabb_buffer_offset + base_index]; if bitcast(aabb.min_x) == 0xFFC00000u { exit = 1; } - //} - //exit = subgroupBroadcastFirst(exit); + } + exit = subgroupBroadcastFirst(exit); if exit == 1 { return; } - let position = internal_to_external_pos(internal_pos / vec3(128.0, 128.0, 6.0), metadata); - //let grad = gradient_scene(vec4(position, metadata.time)); + metadata.subdivision_log2 += 2; - const EPSILON = 0.001; + var rolling_subdivision = 2u; + var global_offset = compute_input.workgroup_id * vec3(4, 4, 4); - 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)))); + var increasing = false; - textureStore(tiletree, uinternal_pos, vec4(grad * 0.5 + 0.5, 0.0)); + 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); + } + 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 aabb = in_aabb(metadata, vec3f(uinternal_pos_global)); + if (aabb) { + create_work_items_batch(increasing, rolling_subdivision + 1, uinternal_pos_local * 2); + } + } + + 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)); + } }