wip
This commit is contained in:
@@ -5,6 +5,6 @@ struct PackedAabb {
|
||||
max_x: f32,
|
||||
max_y: f32,
|
||||
max_z: f32,
|
||||
pad_a: u32,
|
||||
pad_b: u32,
|
||||
tiletree_index: u32,
|
||||
padding: u32,
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ fn main(compute_input: ComputeInput) {
|
||||
vec4(high_corner, metadata.time),
|
||||
);
|
||||
|
||||
let base_index = encode_internal_pos(uinternal_pos, metadata.subdivision_log2);
|
||||
let base_index = encode_internal_pos(uinternal_pos);
|
||||
let aabb = &aabb_buffer.aabbs[push_constants.aabb_buffer_offset + base_index];
|
||||
|
||||
let en = (interval.x < 0) && (interval.y > 0);
|
||||
|
||||
@@ -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;
|
||||
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;
|
||||
}
|
||||
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);
|
||||
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,62 +135,54 @@ 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);
|
||||
}
|
||||
for (var level = 3; level >= 0; level--) {
|
||||
workgroupBarrier();
|
||||
let level_stride = get_stride(u32(level));
|
||||
|
||||
if (compute_input.local_invocation_id.x % 2 == 0) {
|
||||
create_work_item_pair(increasing, rolling_subdivision, compute_input.local_invocation_id);
|
||||
}
|
||||
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);
|
||||
|
||||
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);
|
||||
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);
|
||||
}
|
||||
if level > 1 {
|
||||
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);
|
||||
} 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;
|
||||
@@ -213,6 +194,10 @@ fn main(compute_input: ComputeInput) {
|
||||
);
|
||||
let grad = normalize(n - (scene(vec4(position, metadata.time))));
|
||||
|
||||
textureStore(tiletree, uinternal_pos_global, vec4(grad * 0.5 + 0.5, 0.0));
|
||||
textureStore(tiletree, uinternal_pos_global, vec4(grad * 0.5 + 0.5, 1.0));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,7 +86,7 @@ fn sebastian_trace(metadata: TapeMetadata, spatial_texture: u32, near_plane: f32
|
||||
}
|
||||
|
||||
fn trace_tape(metadata: TapeMetadata, spatial_texture: u32, primitive_id: u32, ray_pos: vec3<f32>, ray_dir: vec3<f32>, primary_ray: bool) -> f32 {
|
||||
let uinternal_pos = decode_internal_pos(primitive_id, metadata.subdivision_log2);
|
||||
let uinternal_pos = decode_internal_pos(primitive_id);
|
||||
let internal_pos = vec3f(uinternal_pos);
|
||||
|
||||
let dimensions = internal_to_external_pos(vec3(1.0), metadata) - metadata.lowest_corner;
|
||||
|
||||
@@ -4,16 +4,48 @@ fn NAN() -> f32 {
|
||||
return bitcast<f32>(0xFFC00000u);
|
||||
}
|
||||
|
||||
fn decode_internal_pos(base_index: u32, subdivision_log2: u32) -> vec3<u32> {
|
||||
return vec3u(
|
||||
(base_index >> (subdivision_log2 * 0)),
|
||||
(base_index >> (subdivision_log2 * 1)),
|
||||
(base_index >> (subdivision_log2 * 2)),
|
||||
) & vec3u(u32((1 << subdivision_log2) - 1));
|
||||
// https://github.com/Forceflow/libmorton/blob/main/include/libmorton/morton3D.h
|
||||
|
||||
fn morton_split_by_3(a: u32) -> u32 {
|
||||
var x = a & 0x000003ff;
|
||||
x = (x | (x << 16)) & 0x30000ff;
|
||||
x = (x | (x << 8)) & 0x0300f00f;
|
||||
x = (x | (x << 4)) & 0x30c30c3;
|
||||
x = (x | (x << 2)) & 0x9249249;
|
||||
return x;
|
||||
}
|
||||
|
||||
fn encode_internal_pos(internal_pos: vec3<u32>, subdivision_log2: u32) -> u32 {
|
||||
return (((internal_pos.z << subdivision_log2) + internal_pos.y) << subdivision_log2) + internal_pos.x;
|
||||
// ENCODE 3D Morton code : Magic bits method
|
||||
// This method uses certain bit patterns (magic bits) to split bits in the coordinates
|
||||
fn morton_encode(pos: vec3<u32>) -> u32 {
|
||||
return morton_split_by_3(pos.x) | (morton_split_by_3(pos.y) << 1) | (morton_split_by_3(pos.z) << 2);
|
||||
}
|
||||
|
||||
fn morton_get_third_bits(m: u32) -> u32 {
|
||||
var x = m & 0x9249249;
|
||||
x = (x ^ (x >> 2)) & 0x30c30c3;
|
||||
x = (x ^ (x >> 4)) & 0x0300f00f;
|
||||
x = (x ^ (x >> 8)) & 0x30000ff;
|
||||
x = (x ^ (x >> 16)) & 0x000003ff;
|
||||
return x;
|
||||
}
|
||||
|
||||
// DECODE 3D Morton code : Magic bits
|
||||
// This method splits the morton codes bits by using certain patterns (magic bits)
|
||||
fn morton_decode(m: u32) -> vec3<u32> {
|
||||
return vec3u(
|
||||
morton_get_third_bits(m),
|
||||
morton_get_third_bits(m >> 1),
|
||||
morton_get_third_bits(m >> 2),
|
||||
);
|
||||
}
|
||||
|
||||
fn decode_internal_pos(base_index: u32) -> vec3<u32> {
|
||||
return morton_decode(base_index);
|
||||
}
|
||||
|
||||
fn encode_internal_pos(internal_pos: vec3<u32>) -> u32 {
|
||||
return morton_encode(internal_pos);
|
||||
}
|
||||
|
||||
fn internal_to_external_pos(internal_pos: vec3<f32>, metadata: TapeMetadata) -> vec3<f32> {
|
||||
|
||||
Reference in New Issue
Block a user