Compare commits
2
Commits
e7fbeb7887
...
dd32c772e5
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dd32c772e5 | ||
|
|
4581538c66 |
+273
-212
@@ -3,27 +3,11 @@ import voxel_cache;
|
||||
|
||||
public struct Aabb
|
||||
{
|
||||
public float min_x;
|
||||
public float min_y;
|
||||
public float min_z;
|
||||
public float3 min;
|
||||
uint32_t _pad0; // std430 aligns float3 to 16 bytes
|
||||
|
||||
uint32_t _pad0;
|
||||
|
||||
public float max_x;
|
||||
public float max_y;
|
||||
public float max_z;
|
||||
|
||||
uint32_t _pad1;
|
||||
|
||||
public float3 min()
|
||||
{
|
||||
return float3(this.min_x, this.min_y, this.min_z);
|
||||
}
|
||||
|
||||
public float3 max()
|
||||
{
|
||||
return float3(this.max_x, this.max_y, this.max_z);
|
||||
}
|
||||
public float3 max;
|
||||
uint32_t _pad1;
|
||||
}
|
||||
|
||||
public struct BvhNode
|
||||
@@ -53,20 +37,38 @@ public struct BvhObject
|
||||
uint32_t _pad2;
|
||||
}
|
||||
|
||||
/// Traversal state for a single ray.
|
||||
///
|
||||
/// The BVH is traversed with a restart trail instead of a stack: one bit per
|
||||
/// tree level records what has already been processed, and every restart
|
||||
/// re-walks from the root steered by that trail. [`BvhCursor::trail`] is the
|
||||
/// only traversal state that survives between calls to [`Bvh::next`].
|
||||
///
|
||||
/// Trail invariant: bits `0 .. leaf_depth-1` encode the path to the current
|
||||
/// leaf (`0` = near child, `1` = far child); all deeper bits are clear.
|
||||
/// A 64 bit trail supports trees up to 64 levels deep.
|
||||
public struct BvhCursor
|
||||
{
|
||||
Ray ray;
|
||||
// One bit per level, bit 0 means left child unexplored, bit one, child explored
|
||||
uint64_t bit_stack;
|
||||
public Ray ray;
|
||||
|
||||
// Where to write the node when descending
|
||||
uint32_t stack_head;
|
||||
uint32_t stack_count;
|
||||
/// Reciprocal of [`Ray::direction`]; computed once per ray and reused for
|
||||
/// every node test.
|
||||
public float3 inv_direction;
|
||||
|
||||
uint32_t current_object;
|
||||
uint32_t current_node;
|
||||
/// Restart trail, see the struct documentation.
|
||||
public uint64_t trail;
|
||||
|
||||
uint32_t depth;
|
||||
/// Objects of the leaf currently being drained.
|
||||
public uint32_t leaf_first;
|
||||
public uint32_t leaf_count;
|
||||
public uint32_t leaf_next;
|
||||
public uint32_t leaf_depth;
|
||||
|
||||
/// True while [`Self::leaf_next`] still points into a valid leaf.
|
||||
public bool have_leaf;
|
||||
|
||||
/// Set once the whole tree has been processed.
|
||||
public bool done;
|
||||
}
|
||||
|
||||
public struct BvhParameters
|
||||
@@ -77,229 +79,288 @@ public struct BvhParameters
|
||||
RWStructuredBuffer<Atomic<uint32_t>> requests;
|
||||
}
|
||||
|
||||
static const uint32_t STACK_SIZE = 4;
|
||||
static const uint32_t STACK_MASK = 0b11;
|
||||
groupshared uint32_t stack[STACK_SIZE * 256];
|
||||
groupshared uint32_t stack_depth[STACK_SIZE * 256];
|
||||
|
||||
public struct Bvh : IStructure
|
||||
{
|
||||
typedef BvhCursor Cursor;
|
||||
public BvhParameters parameters;
|
||||
public uint32_t thread_index;
|
||||
|
||||
/// Kept for compatibility with the existing call sites. The restart
|
||||
/// traversal does not need per-thread storage, so this is unused.
|
||||
public uint32_t thread_index;
|
||||
|
||||
public BvhCursor begin(Ray world_ray)
|
||||
{
|
||||
BvhCursor c;
|
||||
c.ray = world_ray;
|
||||
c.bit_stack = 0;
|
||||
c.stack_head = 0;
|
||||
c.stack_count = 0;
|
||||
c.current_object = 0;
|
||||
c.current_node = 0;
|
||||
c.depth = 0;
|
||||
c.inv_direction = safe_reciprocal(world_ray.direction);
|
||||
c.trail = uint64_t(0);
|
||||
c.leaf_first = 0;
|
||||
c.leaf_count = 0;
|
||||
c.leaf_next = 0;
|
||||
c.leaf_depth = 0;
|
||||
c.have_leaf = false;
|
||||
|
||||
// Early out when the ray misses the whole scene.
|
||||
let root = this.parameters.nodes[0];
|
||||
let root_t = intersect_aabb(
|
||||
world_ray.origin,
|
||||
c.inv_direction,
|
||||
root.aabb.min,
|
||||
root.aabb.max);
|
||||
c.done = !(root_t.y >= max(root_t.x, 0.));
|
||||
|
||||
this.push(0, c);
|
||||
return c;
|
||||
}
|
||||
|
||||
public bool next(inout BvhCursor c, out Cell cell)
|
||||
{
|
||||
uint32_t node_index = 0;
|
||||
uint64_t out_b = 0;
|
||||
while (true && out_b < 80)
|
||||
// The out parameter is only meaningful on a `true` return, but
|
||||
// initialising it keeps the compiler's definite-assignment analysis
|
||||
// happy on all early-outs.
|
||||
cell.cache_pointer = 0;
|
||||
cell.local_ray.origin = float3(0.);
|
||||
cell.local_ray.direction = float3(0.);
|
||||
cell.t = 0.;
|
||||
|
||||
if (c.done)
|
||||
{
|
||||
out_b += 1;
|
||||
uint32_t b = 0;
|
||||
while (parameters.nodes[node_index].is_internal() && b < 80)
|
||||
{
|
||||
b++;
|
||||
uint32_t left = parameters.nodes[node_index].first_or_left;
|
||||
float2 inter1 = box_intersect(c.ray.origin, c.ray.direction, parameters.nodes[left].aabb.min(), parameters.nodes[left].aabb.max());
|
||||
float2 inter2 = box_intersect(c.ray.origin, c.ray.direction, parameters.nodes[left + 1].aabb.min(), parameters.nodes[left + 1].aabb.max());
|
||||
|
||||
bool hit1 = inter1.x <= inter1.y && inter1.y >= 0;
|
||||
bool hit2 = inter2.x <= inter2.y && inter2.y >= 0;
|
||||
|
||||
if (hit1 && hit2)
|
||||
{
|
||||
let near = select(inter1.x < inter2.x, left, left + 1);
|
||||
let far = select(inter1.x < inter2.x, left + 1, left);
|
||||
|
||||
// Push far
|
||||
this.push(far, c);
|
||||
|
||||
// Go to near
|
||||
c.depth += 1;
|
||||
node_index = near;
|
||||
}
|
||||
else if (hit1 || hit2)
|
||||
{
|
||||
// Go to hit
|
||||
c.depth += 1;
|
||||
node_index = select(hit1, left, left + 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
// none hit, go to next
|
||||
if (!this.pop(node_index, c))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (parameters.nodes[node_index].is_leaf())
|
||||
{
|
||||
let first = parameters.nodes[node_index].first_or_left;
|
||||
for (uint32_t i = first; i < first + parameters.nodes[node_index].count; i++)
|
||||
{
|
||||
if (this.objectHit(i, c.ray, cell))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool objectHit(uint32_t object_index, Ray r, out Cell c)
|
||||
{
|
||||
// Project direction and view in local space
|
||||
let direction = mul(parameters.objects[object_index].world_to_local, float4(r.direction, 0));
|
||||
let origin = mul(parameters.objects[object_index].world_to_local, float4(r.origin, 1));
|
||||
|
||||
float2 inter = box_intersect(origin.xyz, direction.xyz, float3(0.), float3(1.));
|
||||
|
||||
return inter.x <= inter.y && inter.y >= 0.;
|
||||
}
|
||||
|
||||
void push(uint32_t node, inout BvhCursor c)
|
||||
{
|
||||
stack[(c.stack_head & STACK_MASK) * 256 + this.thread_index] = node;
|
||||
stack_depth[(c.stack_head & STACK_MASK) * 256 + this.thread_index] = c.depth;
|
||||
c.stack_head += 1;
|
||||
c.stack_count = max(c.stack_count + 1, STACK_SIZE);
|
||||
}
|
||||
|
||||
bool pop(out uint32_t node, inout BvhCursor c)
|
||||
{
|
||||
if (c.stack_count > 0)
|
||||
{
|
||||
c.stack_head -= 1;
|
||||
c.stack_count -= 1;
|
||||
node = stack[(c.stack_head & STACK_MASK) * 256 + this.thread_index];
|
||||
c.depth = stack_depth[(c.stack_head & STACK_MASK) * 256 + this.thread_index];
|
||||
|
||||
// This node is done, mark it ok, remove bits of deeper levels, they're done
|
||||
c.bit_stack = (c.bit_stack | 1 << c.depth) & ((2 << c.depth) - 1);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
// Stack is underflowed, we need to restart
|
||||
uint64_t above = (~c.bit_stack) & ((1 << c.depth) - 1);
|
||||
if (above == 0)
|
||||
{
|
||||
// Then all upper levels are one, meaning they've been popped once
|
||||
// Meaning we were in a far->far->far... ->far path.
|
||||
// so we have explored everything
|
||||
return false;
|
||||
}
|
||||
|
||||
// Select the depth at which we want to go back down, which is the first 0
|
||||
// bit
|
||||
uint32_t return_depth = firstbithigh(above);
|
||||
|
||||
// Since we visited that level, we mark it visited
|
||||
c.bit_stack = (c.bit_stack | 1 << return_depth) & ((2 << return_depth) - 1);
|
||||
|
||||
// Reset stack
|
||||
c.stack_head = 0;
|
||||
c.stack_count = 0;
|
||||
|
||||
// Travel back down to level using the bit stack
|
||||
node = 0;
|
||||
for (uint32_t l = 0; l < return_depth; l++)
|
||||
[loop]
|
||||
for (;;)
|
||||
{
|
||||
bool go_far = ((c.bit_stack >> l) & 1) == 1;
|
||||
|
||||
uint32_t left = parameters.nodes[node].first_or_left;
|
||||
float2 inter1 = box_intersect(c.ray.origin, c.ray.direction, parameters.nodes[left].aabb.min(), parameters.nodes[left].aabb.max());
|
||||
float2 inter2 = box_intersect(c.ray.origin, c.ray.direction, parameters.nodes[left + 1].aabb.min(), parameters.nodes[left + 1].aabb.max());
|
||||
|
||||
bool hit1 = inter1.x <= inter1.y && inter1.y >= 0;
|
||||
bool hit2 = inter2.x <= inter2.y && inter2.y >= 0;
|
||||
|
||||
if (hit1 && hit2)
|
||||
// 1. Drain the objects of the leaf we are currently in.
|
||||
[loop]
|
||||
while (c.leaf_next < c.leaf_count)
|
||||
{
|
||||
let near = select(inter1.x < inter2.x, left, left + 1);
|
||||
let far = select(inter1.x < inter2.x, left + 1, left);
|
||||
node = select(go_far, far, near);
|
||||
if (!go_far)
|
||||
let object_index = c.leaf_first + c.leaf_next;
|
||||
c.leaf_next += 1;
|
||||
|
||||
Cell candidate;
|
||||
if (this.object_hit(object_index, c.ray, candidate))
|
||||
{
|
||||
this.push(far, c);
|
||||
cell = candidate;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else if (hit1 || hit2)
|
||||
{
|
||||
// At this level, we must have chosen the one that hit
|
||||
node = select(hit1, left, left + 1);
|
||||
}
|
||||
}
|
||||
|
||||
// Hit the far node
|
||||
{
|
||||
uint32_t left = parameters.nodes[node].first_or_left;
|
||||
float2 inter1 = box_intersect(c.ray.origin, c.ray.direction, parameters.nodes[left].aabb.min(), parameters.nodes[left].aabb.max());
|
||||
float2 inter2 = box_intersect(c.ray.origin, c.ray.direction, parameters.nodes[left + 1].aabb.min(), parameters.nodes[left + 1].aabb.max());
|
||||
|
||||
bool hit1 = inter1.x <= inter1.y && inter1.y >= 0;
|
||||
bool hit2 = inter2.x <= inter2.y && inter2.y >= 0;
|
||||
|
||||
if (hit1 && hit2)
|
||||
// 2. The leaf is exhausted: advance the trail past it.
|
||||
if (c.have_leaf)
|
||||
{
|
||||
node = select(inter1.x < inter2.x, left + 1, left);
|
||||
}
|
||||
else if (hit1 || hit2)
|
||||
{
|
||||
// At this level, we must have chosen the one that hit
|
||||
node = select(hit1, left, left + 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Not hit, we can pop
|
||||
if (c.stack_count == 0)
|
||||
c.have_leaf = false;
|
||||
if (!advance_trail(c, c.leaf_depth))
|
||||
{
|
||||
c.done = true;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Restart: descend from the root, steered by the trail.
|
||||
uint32_t node = 0;
|
||||
uint32_t level = 0;
|
||||
bool dead = false;
|
||||
|
||||
[loop]
|
||||
for (;;)
|
||||
{
|
||||
let n = this.parameters.nodes[node];
|
||||
if (n.is_leaf())
|
||||
{
|
||||
c.leaf_first = n.first_or_left;
|
||||
c.leaf_count = n.count;
|
||||
c.leaf_next = 0;
|
||||
c.leaf_depth = level;
|
||||
c.have_leaf = true;
|
||||
break;
|
||||
}
|
||||
|
||||
let left = n.first_or_left;
|
||||
let right = left + 1;
|
||||
|
||||
let t_left = intersect_aabb(
|
||||
c.ray.origin,
|
||||
c.inv_direction,
|
||||
this.parameters.nodes[left].aabb.min,
|
||||
this.parameters.nodes[left].aabb.max);
|
||||
let t_right = intersect_aabb(
|
||||
c.ray.origin,
|
||||
c.inv_direction,
|
||||
this.parameters.nodes[right].aabb.min,
|
||||
this.parameters.nodes[right].aabb.max);
|
||||
|
||||
let hit_left = t_left.y >= max(t_left.x, 0.);
|
||||
let hit_right = t_right.y >= max(t_right.x, 0.);
|
||||
|
||||
if (hit_left && hit_right)
|
||||
{
|
||||
// Trail bit 0 means "near child first", 1 means "far".
|
||||
// The near/far decision is deterministic for a fixed ray,
|
||||
// so it stays consistent across restarts.
|
||||
let left_is_near = t_left.x <= t_right.x;
|
||||
let go_far = ((c.trail >> level) & uint64_t(1)) != uint64_t(0);
|
||||
|
||||
if (left_is_near)
|
||||
{
|
||||
node = go_far ? right : left;
|
||||
}
|
||||
else
|
||||
{
|
||||
node = go_far ? left : right;
|
||||
}
|
||||
level += 1;
|
||||
}
|
||||
else if (hit_left)
|
||||
{
|
||||
// The far child has no work; mark this level processed so
|
||||
// a later restart does not try to enter it.
|
||||
c.trail |= (uint64_t(1) << level);
|
||||
node = left;
|
||||
level += 1;
|
||||
}
|
||||
else if (hit_right)
|
||||
{
|
||||
c.trail |= (uint64_t(1) << level);
|
||||
node = right;
|
||||
level += 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
c.stack_head -= 1;
|
||||
c.stack_count -= 1;
|
||||
node = stack[(c.stack_head & STACK_MASK) * 256 + this.thread_index];
|
||||
c.depth = stack_depth[(c.stack_head & STACK_MASK) * 256 + this.thread_index];
|
||||
dead = true;
|
||||
break;
|
||||
}
|
||||
|
||||
// This node is done, mark it ok, remove bits of deeper levels, they're done
|
||||
c.bit_stack = (c.bit_stack | 1 << c.depth) & ((2 << c.depth) - 1);
|
||||
if (level >= 64)
|
||||
{
|
||||
// The trail cannot represent anything deeper.
|
||||
c.done = true;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (dead)
|
||||
{
|
||||
// No child of this node is hit: move on to the next
|
||||
// unprocessed part of the tree.
|
||||
if (!advance_trail(c, level))
|
||||
{
|
||||
c.done = true;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Tests a leaf object and, if it can be traced, fills its cache cell.
|
||||
bool object_hit(uint32_t object_index, Ray world_ray, out Cell cell)
|
||||
{
|
||||
cell.cache_pointer = 0;
|
||||
cell.local_ray.origin = float3(0.);
|
||||
cell.local_ray.direction = float3(0.);
|
||||
cell.t = 0.;
|
||||
|
||||
let object = this.parameters.objects[object_index];
|
||||
|
||||
// Transform the ray to the object's local space, where the voxel
|
||||
// chunk is the unit cube.
|
||||
let local_origin = mul(object.world_to_local, float4(world_ray.origin, 1.)).xyz;
|
||||
let local_direction = mul(object.world_to_local, float4(world_ray.direction, 0.)).xyz;
|
||||
|
||||
let t = intersect_aabb(
|
||||
local_origin,
|
||||
safe_reciprocal(local_direction),
|
||||
float3(0.),
|
||||
float3(1.));
|
||||
|
||||
if (t.y < max(t.x, 0.))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
let pointer = this.parameters.slots[object.table_slot];
|
||||
if (pointer.subdivided_valid())
|
||||
{
|
||||
let entry_t = max(t.x, 0.);
|
||||
cell.cache_pointer = pointer.pointer();
|
||||
cell.local_ray.origin = local_origin + local_direction * entry_t;
|
||||
cell.local_ray.direction = local_direction;
|
||||
cell.t = entry_t;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (pointer.subdivided())
|
||||
{
|
||||
// The root of this object is requested but not available yet.
|
||||
this.parameters.requests[object.table_slot].add(1);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
float2 box_intersect(float3 origin, float3 ray_direction, float3 box_min, float3 box_max)
|
||||
/// Moves the trail to the next unprocessed part of the tree.
|
||||
///
|
||||
/// Finds the nearest ancestor with a pending far child, marks it processed
|
||||
/// and clears the path below it. Returns false when the whole tree has been
|
||||
/// visited.
|
||||
bool advance_trail(inout BvhCursor c, uint32_t depth)
|
||||
{
|
||||
let min_ts = (box_min - origin) / ray_direction;
|
||||
let max_ts = (box_max - origin) / ray_direction;
|
||||
uint32_t level = depth;
|
||||
uint64_t trail = (~c.trail) & ((1 << uint64_t(level)) - 1);
|
||||
int32_t fbh = firstbithigh(trail);
|
||||
if (trail == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
let far_ts = max(min_ts, max_ts);
|
||||
let near_ts = min(min_ts, max_ts);
|
||||
level = fbh;
|
||||
c.trail |= (uint64_t(1) << level);
|
||||
// Keep the path bits up to and including this level, drop the
|
||||
// stale path below it.
|
||||
c.trail &= (uint64_t(2) << level) - uint64_t(1);
|
||||
|
||||
let far_t = min(far_ts.x, min(far_ts.y, far_ts.z));
|
||||
let near_t = max(near_ts.x, max(near_ts.y, near_ts.z));
|
||||
return float2(near_t, far_t);
|
||||
// [loop]
|
||||
// while (level > 0)
|
||||
// {
|
||||
// level -= 1;
|
||||
// if (((c.trail >> level) & uint64_t(1)) == uint64_t(0))
|
||||
// {
|
||||
// c.trail |= (uint64_t(1) << level);
|
||||
// // Keep the path bits up to and including this level, drop the
|
||||
// // stale path below it.
|
||||
// c.trail &= (uint64_t(2) << level) - uint64_t(1);
|
||||
// return true;
|
||||
// }
|
||||
// }
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Slab test against an axis aligned box, using a precomputed reciprocal.
|
||||
///
|
||||
/// Returns `(t_near, t_far)`. A hit is `t_far >= max(t_near, 0)`.
|
||||
float2 intersect_aabb(float3 origin, float3 inv_direction, float3 box_min, float3 box_max)
|
||||
{
|
||||
let t0 = (box_min - origin) * inv_direction;
|
||||
let t1 = (box_max - origin) * inv_direction;
|
||||
|
||||
let t_near = min(t0, t1);
|
||||
let t_far = max(t0, t1);
|
||||
|
||||
return float2(
|
||||
max(t_near.x, max(t_near.y, t_near.z)),
|
||||
min(t_far.x, min(t_far.y, t_far.z)));
|
||||
}
|
||||
|
||||
/// Reciprocal that avoids `0 * inf = NaN` for rays parallel to a slab.
|
||||
///
|
||||
/// A component that is (near) zero is replaced by a tiny positive value. The
|
||||
/// slab test stays correct: the resulting huge `t` values either do not
|
||||
/// constrain the interval or reject the box, depending on which side of the
|
||||
/// slab the origin lies.
|
||||
float3 safe_reciprocal(float3 direction)
|
||||
{
|
||||
let tiny = abs(direction) < float3(1e-8);
|
||||
return 1. / select(tiny, float3(1e-8), direction);
|
||||
}
|
||||
|
||||
|
||||
@@ -75,6 +75,10 @@ void gi(uint32_t3 location: SV_DispatchThreadID, uint32_t3 local_location: SV_Gr
|
||||
let pos = gbuffer_position.Load(location.xy);
|
||||
let normal = gbuffer_normal.Load(location.xy);
|
||||
let albedo = gbuffer_albedo.Load(location.xy);
|
||||
|
||||
// let floored_pos = floor(pos);
|
||||
// let c = (floored_pos.x + floored_pos.y + floored_pos.z) % 2;
|
||||
// target_texture.Store(location.xy, albedo * select(c == 0, 0.5, 1.));
|
||||
target_texture.Store(location.xy, albedo);
|
||||
return;
|
||||
}
|
||||
|
||||
Binary file not shown.
+51
-54
@@ -146,64 +146,61 @@ void ray_march_compute(uint32_t3 location: SV_DispatchThreadID, uint32_t3 local_
|
||||
|
||||
Bvh bvh;
|
||||
bvh.parameters = table_parameters;
|
||||
bvh.thread_index = local_location.x;
|
||||
BvhCursor c = bvh.begin(r);
|
||||
Cell cell;
|
||||
bvh.thread_index = stack_index;
|
||||
// BvhCursor c = bvh.begin(r);
|
||||
Cell cell;
|
||||
|
||||
// for (uint32_t i = 0; i < 100; i++)
|
||||
// Setup for simple bvh traversal check
|
||||
// if (bvh.next(c, cell))
|
||||
// {
|
||||
if (bvh.next(c, cell))
|
||||
// gbuffer_albedo.Store(location.xy, float4(0., 1., 0., 1.));
|
||||
// return;
|
||||
// }
|
||||
// gbuffer_albedo.Store(location.xy, float4(0.));
|
||||
|
||||
// ToroidalStructureTableTracer table_tracer;
|
||||
// table_tracer.structure_table = table_parameters;
|
||||
// table_tracer.extent =
|
||||
// uint32_t3(constants.chunk_width, constants.chunk_alt, constants.chunk_height);
|
||||
// table_tracer.base_slot = uint32_t3(
|
||||
// constants.table_base_slot_x,
|
||||
// constants.table_base_slot_y,
|
||||
// constants.table_base_slot_z);
|
||||
|
||||
PixelFootprintLod lod = PixelFootprintLod.create((100.f * 3.14f) / 180.f, 1080.f, 2.f);
|
||||
|
||||
Tracer<Bvh, VoxelCacheTracer<2, ColorHit<2>>, PixelFootprintLod> tracer;
|
||||
tracer.structure = bvh;
|
||||
tracer.cache = cache_tracer;
|
||||
tracer.lod = lod;
|
||||
|
||||
Hit h;
|
||||
// Closest hit at or past tMin. Re-trace past a hit with:
|
||||
// t_min = h.t + 1e-4;
|
||||
// hit = tracer.trace(stack_index, t_min, h, r);
|
||||
bool hit = tracer.trace(stack_index, 0., h, r);
|
||||
|
||||
// ToroidalStructureTableTracer.Cursor cursor = table_tracer.begin(r);
|
||||
// Cell cell;
|
||||
|
||||
if (hit)
|
||||
{
|
||||
gbuffer_albedo.Store(location.xy, float4(0., 1., 0., 1.));
|
||||
return;
|
||||
// Gather
|
||||
let hit_pos = h.hit_location;
|
||||
|
||||
let hit_normal = fetch_normal(h.node_index, h.child_index);
|
||||
var hit_albedo = float4(0.);
|
||||
hit_albedo = fetch_color(h.node_index, h.child_index).float_color;
|
||||
// let hit_albedo = hit.color;
|
||||
|
||||
gbuffer_position.Store(location.xy, float4(hit_pos, 1.));
|
||||
gbuffer_normal.Store(location.xy, float4(hit_normal, 1.));
|
||||
gbuffer_albedo.Store(location.xy, hit_albedo);
|
||||
}
|
||||
else
|
||||
{
|
||||
gbuffer_albedo.Store(location.xy, float4(0.));
|
||||
}
|
||||
//}
|
||||
gbuffer_albedo.Store(location.xy, float4(0.));
|
||||
|
||||
/*
|
||||
ToroidalStructureTableTracer table_tracer;
|
||||
table_tracer.structure_table = table_parameters;
|
||||
table_tracer.extent =
|
||||
uint32_t3(constants.chunk_width, constants.chunk_alt, constants.chunk_height);
|
||||
table_tracer.base_slot = uint32_t3(
|
||||
constants.table_base_slot_x,
|
||||
constants.table_base_slot_y,
|
||||
constants.table_base_slot_z);
|
||||
|
||||
|
||||
|
||||
PixelFootprintLod lod = PixelFootprintLod.create((100.f * 3.14f) / 180.f, 1080.f, 2.f);
|
||||
|
||||
Tracer<ToroidalStructureTableTracer, VoxelCacheTracer<2, ColorHit<2>>, PixelFootprintLod> tracer;
|
||||
tracer.structure = table_tracer;
|
||||
tracer.cache = cache_tracer;
|
||||
tracer.lod = lod;
|
||||
|
||||
Hit h;
|
||||
bool hit = tracer.trace(stack_index, h, r);
|
||||
|
||||
ToroidalStructureTableTracer.Cursor cursor = table_tracer.begin(r);
|
||||
Cell cell;
|
||||
|
||||
if (hit)
|
||||
{
|
||||
// Gather
|
||||
let hit_pos = h.hit_location;
|
||||
|
||||
let hit_normal = fetch_normal(h.node_index, h.child_index);
|
||||
var hit_albedo = float4(0.);
|
||||
hit_albedo = fetch_color(h.node_index, h.child_index).float_color;
|
||||
// let hit_albedo = hit.color;
|
||||
|
||||
gbuffer_position.Store(location.xy, float4(hit_pos, 1.));
|
||||
gbuffer_normal.Store(location.xy, float4(hit_normal, 1.));
|
||||
gbuffer_albedo.Store(location.xy, hit_albedo);
|
||||
}
|
||||
else
|
||||
{
|
||||
gbuffer_albedo.Store(location.xy, float4(0.));
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
float2 box_intersect(float3 origin, float3 ray_direction, float3 box_min, float3 box_max)
|
||||
|
||||
@@ -142,6 +142,8 @@ public struct VoxelCacheTracer<let N : int, H : IVoxelHit<N>> : IVoxelCache
|
||||
|
||||
public bool trace<L : ILodPolicy>(uint32_t root, uint32_t thread_id, Ray r, L lod, float offset_t, out Hit v)
|
||||
{
|
||||
v.t = 0.;
|
||||
|
||||
// Ray mirroring
|
||||
r.origin += float3(1.);
|
||||
r.origin = clamp(r.origin, float(1.), asfloat(0x3fffffff));
|
||||
@@ -216,6 +218,7 @@ public struct VoxelCacheTracer<let N : int, H : IVoxelHit<N>> : IVoxelCache
|
||||
asfloat(asuint(pos) ^ 0x007fffff),
|
||||
pos) -
|
||||
float3(1.);
|
||||
v.t = offset_t + t;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -44,6 +44,10 @@ public struct Hit
|
||||
public uint32_t node_index;
|
||||
public uint32_t child_index;
|
||||
public float3 hit_location;
|
||||
|
||||
/// Distance of the hit from the ray origin, in the units of the ray
|
||||
/// direction. Used to order hits and to restart the traversal past a hit.
|
||||
public float t;
|
||||
}
|
||||
|
||||
public interface IVoxelCache
|
||||
@@ -60,23 +64,66 @@ public struct Tracer<S : IStructure, C : IVoxelCache, L : ILodPolicy>
|
||||
public C cache;
|
||||
public L lod;
|
||||
|
||||
public bool trace(uint32_t thread_id, out Hit h, Ray r)
|
||||
/// Traces the closest hit at or past `t_min`.
|
||||
///
|
||||
/// The structure may return cells in any order (the BVH restart trail is
|
||||
/// not distance ordered). Instead of returning on the first cache hit, we
|
||||
/// keep the closest hit found so far and skip cells that start beyond it:
|
||||
/// every hit inside a cell is at least `cell.t` away, so a cell with
|
||||
/// `entry >= best_t` cannot improve the result.
|
||||
public bool trace(uint32_t thread_id, float t_min, out Hit h, Ray r)
|
||||
{
|
||||
/* loop: next cell -> cache.trace -> early out */
|
||||
S.Cursor cursor = this.structure.begin(r);
|
||||
Cell cell;
|
||||
|
||||
h.hit = false;
|
||||
h.hit_location = float3(0.f);
|
||||
h.child_index = 0;
|
||||
h.node_index = 0;
|
||||
h.t = 0.f;
|
||||
|
||||
// Start just past the previous hit.
|
||||
Ray adjusted = r;
|
||||
adjusted.origin += r.direction * t_min;
|
||||
|
||||
float best_t = asfloat(0x7f7fffff); // FLT_MAX
|
||||
Hit best_hit = h;
|
||||
bool found = false;
|
||||
|
||||
S.Cursor cursor = this.structure.begin(adjusted);
|
||||
Cell cell;
|
||||
|
||||
[loop]
|
||||
while (this.structure.next(cursor, cell))
|
||||
{
|
||||
if (cache.trace(cell.cache_pointer, thread_id, cell.local_ray, lod, cell.t, h))
|
||||
// `cell.t` is relative to the advanced origin, `best_t` is not.
|
||||
float cell_t = t_min + cell.t;
|
||||
|
||||
// No hit inside this cell can be closer than the current best.
|
||||
if (cell_t >= best_t)
|
||||
{
|
||||
return true;
|
||||
continue;
|
||||
}
|
||||
|
||||
Hit candidate = h;
|
||||
if (cache.trace(
|
||||
cell.cache_pointer,
|
||||
thread_id,
|
||||
cell.local_ray,
|
||||
lod,
|
||||
cell_t,
|
||||
candidate))
|
||||
{
|
||||
if (candidate.t < best_t)
|
||||
{
|
||||
best_t = candidate.t;
|
||||
best_hit = candidate;
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (found)
|
||||
{
|
||||
h = best_hit;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
+68
@@ -12,8 +12,13 @@ use crate::bvh::build::BvhPrimitive;
|
||||
use crate::bvh::build::build_bvh;
|
||||
use crate::bvh::gpu::GpuBvhNode;
|
||||
use crate::bvh::gpu::GpuObject;
|
||||
use crate::gpu_profiler::ScopeEncoder;
|
||||
use crate::production::ProducerId;
|
||||
use crate::production::StructureAnchor;
|
||||
use crate::voxel_cache::CacheInvalidable;
|
||||
use crate::voxel_cache::eviction::CacheEvictor;
|
||||
use crate::voxel_cache::insertion::CacheInsertionData;
|
||||
use crate::voxel_cache::requests::CacheRequestBuffer;
|
||||
use crate::voxel_cache::tables::DynamicStructureTable;
|
||||
|
||||
pub mod aabb;
|
||||
@@ -305,6 +310,69 @@ impl BvhStructureTable
|
||||
{
|
||||
&self.raytrace_bind_group
|
||||
}
|
||||
|
||||
/// Gathers the root requests emitted by the inner structure table into
|
||||
/// `request_buffer`.
|
||||
pub fn gather_requests(
|
||||
&self,
|
||||
device: &wgpu::Device,
|
||||
encoder: &mut ScopeEncoder<wgpu::CommandEncoder>,
|
||||
request_buffer: &CacheRequestBuffer,
|
||||
)
|
||||
{
|
||||
self.structure_table
|
||||
.gather_requests(device, encoder, request_buffer);
|
||||
}
|
||||
|
||||
/// Writes the cache page pointers produced by an external insertion into
|
||||
/// the inner structure table.
|
||||
pub fn insert<const N: usize>(
|
||||
&self,
|
||||
device: &wgpu::Device,
|
||||
encoder: &mut ScopeEncoder<wgpu::CommandEncoder>,
|
||||
insertion: &CacheInsertionData<N>,
|
||||
) where
|
||||
[(); N * N * N]:,
|
||||
{
|
||||
self.structure_table.insert(device, encoder, insertion);
|
||||
}
|
||||
|
||||
pub fn get(&self, handle: &BvhHandle) -> Option<&BvhObject>
|
||||
{
|
||||
self.objects.get(handle)
|
||||
}
|
||||
|
||||
pub fn get_mut(&mut self, handle: &BvhHandle) -> Option<&mut BvhObject>
|
||||
{
|
||||
self.objects.get_mut(handle)
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize> CacheInvalidable<N> for BvhStructureTable
|
||||
where
|
||||
[(); N * N * N]:,
|
||||
{
|
||||
fn validate_insertion(
|
||||
&mut self,
|
||||
device: &wgpu::Device,
|
||||
encoder: &mut ScopeEncoder<wgpu::CommandEncoder>,
|
||||
insertion: &CacheInsertionData<N>,
|
||||
)
|
||||
{
|
||||
self.structure_table
|
||||
.validate_insertion(device, encoder, insertion);
|
||||
}
|
||||
|
||||
fn invalidate(
|
||||
&mut self,
|
||||
encoder: &mut ScopeEncoder<wgpu::CommandEncoder>,
|
||||
cache_evictor: &CacheEvictor,
|
||||
invalid_timestamp: u32,
|
||||
)
|
||||
{
|
||||
self.structure_table
|
||||
.invalidate(encoder, cache_evictor, invalid_timestamp);
|
||||
}
|
||||
}
|
||||
|
||||
fn create_bvh_buffer<T>(device: &wgpu::Device, capacity: usize, label: &str) -> wgpu::Buffer
|
||||
|
||||
@@ -63,6 +63,7 @@ impl From<BvhNode> for GpuBvhNode
|
||||
aabb: value.aabb.into(),
|
||||
count: value.count,
|
||||
first_or_left: value.first_or_left,
|
||||
_pad2: [0; 2],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+3
-2
@@ -4,9 +4,10 @@ use glam::Mat4;
|
||||
use wgpu::BlendState;
|
||||
|
||||
// debug.wgsl mirrors the GPU structs manually. Keep the array strides in sync:
|
||||
// WGSL would otherwise round them up to 16-byte alignment.
|
||||
// WGSL rounds structs containing vec3 up to a 16-byte alignment, so a node's
|
||||
// 40 bytes of data become a 48-byte array stride.
|
||||
const _: () = {
|
||||
assert!(size_of::<crate::bvh::gpu::GpuBvhNode>() == 40);
|
||||
assert!(size_of::<crate::bvh::gpu::GpuBvhNode>() == 48);
|
||||
assert!(size_of::<crate::bvh::gpu::GpuObject>() == 144);
|
||||
};
|
||||
|
||||
|
||||
+8
-12
@@ -5,19 +5,15 @@
|
||||
|
||||
struct GpuBvhNode
|
||||
{
|
||||
// Kept as scalar fields on purpose. WGSL rounds structs containing vec3/vec4
|
||||
// up to a 16-byte alignment (48-byte array stride), while the Rust side is
|
||||
// a 40-byte `#[repr(C)]` struct.
|
||||
min_x: f32,
|
||||
min_y: f32,
|
||||
min_z: f32,
|
||||
_pad0: f32,
|
||||
max_x: f32,
|
||||
max_y: f32,
|
||||
max_z: f32,
|
||||
// vec3 members align to 16 bytes, so this struct has a 48-byte array
|
||||
// stride. The Rust `GpuBvhNode` is padded to the same size.
|
||||
min: vec3<f32>,
|
||||
_pad0: u32,
|
||||
max: vec3<f32>,
|
||||
_pad1: u32,
|
||||
count: u32,
|
||||
first_or_left: u32,
|
||||
_pad2: vec2<u32>,
|
||||
};
|
||||
|
||||
struct GpuObject
|
||||
@@ -111,8 +107,8 @@ fn vs_lines(
|
||||
}
|
||||
|
||||
let node = nodes[instance_index];
|
||||
let min_corner = vec3<f32>(node.min_x, node.min_y, node.min_z);
|
||||
let max_corner = vec3<f32>(node.max_x, node.max_y, node.max_z);
|
||||
let min_corner = node.min;
|
||||
let max_corner = node.max;
|
||||
let world_position = corner_position(
|
||||
min_corner,
|
||||
max_corner,
|
||||
|
||||
@@ -16,6 +16,10 @@ pub struct GpuAabb
|
||||
}
|
||||
|
||||
/// Follow the same documentation as [`BvhNode`]
|
||||
///
|
||||
/// The trailing padding keeps the array stride at 48 bytes. On the GPU a struct
|
||||
/// containing `vec3` has a 16-byte alignment, so the shader rounds the 40 bytes
|
||||
/// of data up to a 48-byte stride.
|
||||
#[derive(Debug, Clone, Copy, bytemuck::Zeroable, bytemuck::Pod)]
|
||||
#[repr(C)]
|
||||
pub struct GpuBvhNode
|
||||
@@ -23,6 +27,7 @@ pub struct GpuBvhNode
|
||||
pub aabb: GpuAabb,
|
||||
pub count: u32,
|
||||
pub first_or_left: u32,
|
||||
pub(crate) _pad2: [u32; 2],
|
||||
}
|
||||
|
||||
/// To be a leaf of a GPU BVH
|
||||
|
||||
+120
-102
@@ -54,9 +54,11 @@ use winit::event_loop::OwnedDisplayHandle;
|
||||
use winit::window::Window;
|
||||
use winit::window::WindowId;
|
||||
|
||||
use crate::bvh::BvhHandle;
|
||||
use crate::bvh::BvhObject;
|
||||
use crate::bvh::BvhStructureTable;
|
||||
use crate::bvh::aabb::Aabb;
|
||||
use crate::bvh::build::Bvh;
|
||||
use crate::bvh::debug::BvhDebugPipeline;
|
||||
use crate::camera::Camera;
|
||||
use crate::egui_renderer::EguiRenderer;
|
||||
@@ -148,8 +150,9 @@ struct State
|
||||
request_buffer: Arc<CacheRequestBuffer>,
|
||||
cache_insertion: Arc<CacheInsertionData<4>>,
|
||||
|
||||
structure_table: Arc<Mutex<BvhStructureTable>>,
|
||||
//structure_table: Arc<Mutex<FixedStructureTable>>,
|
||||
structure_table: Arc<Mutex<ToroidalStructureTable>>,
|
||||
//structure_table: Arc<Mutex<ToroidalStructureTable>>,
|
||||
structure_table_request_buffer: Arc<CacheRequestBuffer>,
|
||||
structure_table_cache_insertion: Arc<CacheInsertionData<4>>,
|
||||
usage_vec: Arc<Mutex<Vec<usize>>>,
|
||||
@@ -170,8 +173,8 @@ struct State
|
||||
// Producers workers
|
||||
wakeup: Sender<()>,
|
||||
|
||||
bvh: BvhStructureTable,
|
||||
bvh_debug: BvhDebugPipeline,
|
||||
bvh_objs: Vec<(BvhHandle, Vec3)>,
|
||||
}
|
||||
|
||||
pub unsafe fn as_raw_bytes<T: Sized>(slice: &[T]) -> &[u8]
|
||||
@@ -445,19 +448,74 @@ impl State
|
||||
let total_chunk_count = terrain_height * terrain_width * terrain_alt;
|
||||
|
||||
let structure_table_request_buffer = CacheRequestBuffer::new(&device, 128);
|
||||
|
||||
let mut bvh = BvhStructureTable::new(&device, 128, 128);
|
||||
let extent = Aabb::new(-10. * Vec3::ONE, 10. * Vec3::ONE);
|
||||
let mut bvh_objs = Vec::new();
|
||||
for _ in 0..1000
|
||||
{
|
||||
let transform = Mat4::from_rotation_translation(
|
||||
Quat::from_euler(
|
||||
glam::EulerRot::XYZ,
|
||||
random_range(-3.1..3.1),
|
||||
random_range(-3.1..3.1),
|
||||
random_range(-3.1..3.1),
|
||||
),
|
||||
Vec3::new(
|
||||
random_range(extent.min().x..extent.max().x),
|
||||
random_range(extent.min().y..extent.max().y),
|
||||
random_range(extent.min().z..extent.max().z),
|
||||
),
|
||||
);
|
||||
|
||||
let handle = bvh.add(
|
||||
&device,
|
||||
&queue,
|
||||
BvhObject {
|
||||
anchor: StructureAnchor::zeroed(),
|
||||
producer_id: ProducerId(0),
|
||||
transform,
|
||||
bounding_box: Aabb::CHUNK,
|
||||
},
|
||||
);
|
||||
|
||||
bvh_objs.push((
|
||||
handle,
|
||||
Vec3::new(
|
||||
random_range(-1.0..1.0),
|
||||
random_range(-1.0..1.0),
|
||||
random_range(-1.0..1.0),
|
||||
)
|
||||
.normalize(),
|
||||
));
|
||||
}
|
||||
|
||||
let now = Instant::now();
|
||||
bvh.rebuild(&device, &queue);
|
||||
let time = Instant::now() - now;
|
||||
|
||||
println!("Bvh build time : {}ms", time.as_millis());
|
||||
|
||||
let bvh_debug = BvhDebugPipeline::new(
|
||||
&device,
|
||||
surface_format.add_srgb_suffix(),
|
||||
wgpu::TextureFormat::Depth24PlusStencil8,
|
||||
);
|
||||
|
||||
// let structure_table = FixedStructureTable::new(
|
||||
// &device,
|
||||
// total_chunk_count,
|
||||
// structure_table_request_buffer.max_requests,
|
||||
// 0,
|
||||
// );
|
||||
let structure_table = ToroidalStructureTable::new(
|
||||
&device,
|
||||
(terrain_width, terrain_alt, terrain_height),
|
||||
Vec3::ZERO,
|
||||
structure_table_request_buffer.max_requests,
|
||||
0,
|
||||
);
|
||||
// let structure_table = ToroidalStructureTable::new(
|
||||
// &device,
|
||||
// (terrain_width, terrain_alt, terrain_height),
|
||||
// Vec3::ZERO,
|
||||
// structure_table_request_buffer.max_requests,
|
||||
// 0,
|
||||
// );
|
||||
let structure_table = bvh;
|
||||
let structure_table_insertion_data = CacheInsertionData::new(&device, 128, &layout);
|
||||
let chunk_instances = (0..terrain_alt)
|
||||
.cartesian_product(0..terrain_height)
|
||||
@@ -523,48 +581,6 @@ impl State
|
||||
// )
|
||||
// };
|
||||
|
||||
let mut bvh = BvhStructureTable::new(&device, 128, 128);
|
||||
let extent = Aabb::new(-10. * Vec3::ONE, 10. * Vec3::ONE);
|
||||
for _ in 0..100
|
||||
{
|
||||
let transform = Mat4::from_rotation_translation(
|
||||
Quat::from_euler(
|
||||
glam::EulerRot::XYZ,
|
||||
random_range(-3.1..3.1),
|
||||
random_range(-3.1..3.1),
|
||||
random_range(-3.1..3.1),
|
||||
),
|
||||
Vec3::new(
|
||||
random_range(extent.min().x..extent.max().x),
|
||||
random_range(extent.min().y..extent.max().y),
|
||||
random_range(extent.min().z..extent.max().z),
|
||||
),
|
||||
);
|
||||
|
||||
bvh.add(
|
||||
&device,
|
||||
&queue,
|
||||
BvhObject {
|
||||
anchor: StructureAnchor::zeroed(),
|
||||
producer_id: ProducerId(0),
|
||||
transform,
|
||||
bounding_box: Aabb::CHUNK,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
let now = Instant::now();
|
||||
bvh.rebuild(&device, &queue);
|
||||
let time = Instant::now() - now;
|
||||
|
||||
println!("Bvh build time : {}ms", time.as_millis());
|
||||
|
||||
let bvh_debug = BvhDebugPipeline::new(
|
||||
&device,
|
||||
surface_format.add_srgb_suffix(),
|
||||
wgpu::TextureFormat::Depth24PlusStencil8,
|
||||
);
|
||||
|
||||
let shader_module = unsafe {
|
||||
device.create_shader_module_passthrough({
|
||||
wgpu::ShaderModuleDescriptorPassthrough {
|
||||
@@ -706,8 +722,7 @@ impl State
|
||||
|
||||
bind_group_layouts: &[
|
||||
Some(voxel_cache.raytrace_bind_group_layout()),
|
||||
//Some(structure_table.raytrace_bind_group_layout()),
|
||||
Some(bvh.raytrace_bind_group_layout()),
|
||||
Some(structure_table.raytrace_bind_group_layout()),
|
||||
Some(&ray_march_bind_group_layout),
|
||||
],
|
||||
immediate_size: size_of::<Immediates>() as u32,
|
||||
@@ -867,8 +882,8 @@ impl State
|
||||
insert_profile: Arc::new(Mutex::new(Profile::default())),
|
||||
cursor_grabed: true,
|
||||
wakeup: wakeup_tx,
|
||||
bvh,
|
||||
bvh_debug,
|
||||
bvh_objs,
|
||||
};
|
||||
|
||||
// Configure surface for the first time
|
||||
@@ -1070,6 +1085,28 @@ impl State
|
||||
{
|
||||
self.camera.update();
|
||||
|
||||
let mut locked_table = self.structure_table.lock();
|
||||
for (hndl, dir) in self.bvh_objs.iter_mut()
|
||||
{
|
||||
let obj = locked_table.get_mut(hndl);
|
||||
if let Some(obj) = obj
|
||||
{
|
||||
let (scale, mut rot, mut trans) = obj.transform.to_scale_rotation_translation();
|
||||
let speed = 0.01;
|
||||
rot = rot.mul_quat(Quat::from_axis_angle(*dir, speed));
|
||||
trans += *dir * speed;
|
||||
*dir += Vec3::new(
|
||||
random_range(-speed..speed),
|
||||
random_range(-speed..speed),
|
||||
random_range(-speed..speed),
|
||||
);
|
||||
obj.transform = Mat4::from_scale_rotation_translation(scale, rot, trans);
|
||||
}
|
||||
}
|
||||
locked_table.mark_dirty();
|
||||
locked_table.rebuild_if_dirty(&self.device, &self.queue);
|
||||
drop(locked_table);
|
||||
|
||||
// Create texture view.
|
||||
// NOTE: We must handle Timeout because the surface may be unavailable
|
||||
// (e.g., when the window is occluded on macOS).
|
||||
@@ -1170,47 +1207,23 @@ impl State
|
||||
let mut command_encoder = self.device.create_command_encoder(&Default::default());
|
||||
let mut prof_command_encoder = ProfilingEncoder::new(&self.device, &mut command_encoder);
|
||||
|
||||
{
|
||||
let retired = self.structure_table.lock().move_position(
|
||||
&self.queue,
|
||||
&mut prof_command_encoder,
|
||||
self.camera.position,
|
||||
);
|
||||
|
||||
// Cancellation is only an optimisation: stale insertions are
|
||||
// rejected by the GPU because they no longer match the slot.
|
||||
let mut request_queue = self.request_queue.lock();
|
||||
for anchor in retired
|
||||
{
|
||||
request_queue.invalidate_anchor(anchor);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
let mut encoder = prof_command_encoder.begin_frame(Some("Main frame"));
|
||||
|
||||
encoder.write_timestamp(×tamp_query, 0);
|
||||
{
|
||||
gpu_profiler::begin_scope!(encoder, "Compute render pass");
|
||||
let mut renderpass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
|
||||
label: Some("compute_render_pass"),
|
||||
timestamp_writes: Some(wgpu::ComputePassTimestampWrites {
|
||||
query_set: ×tamp_query,
|
||||
beginning_of_pass_write_index: Some(2),
|
||||
end_of_pass_write_index: Some(3),
|
||||
}),
|
||||
});
|
||||
let mut renderpass = encoder.begin_profiled_compute_pass("compute_render_pass");
|
||||
|
||||
renderpass.set_pipeline(&self.pipeline);
|
||||
renderpass.set_bind_group(0, self.voxel_cache.lock().raytrace_bind_group(), &[]);
|
||||
renderpass.set_bind_group(1, self.bvh.raytrace_bind_group(), &[]);
|
||||
// renderpass.set_bind_group(
|
||||
// 1,
|
||||
// self.structure_table.lock().raytrace_bind_group(),
|
||||
// &[],
|
||||
// );
|
||||
renderpass.set_bind_group(
|
||||
1,
|
||||
self.structure_table.lock().raytrace_bind_group(),
|
||||
&[],
|
||||
);
|
||||
renderpass.set_bind_group(2, Some(&surface_bg), &[]);
|
||||
let table_base_slot = self.structure_table.lock().base_slot();
|
||||
let table_base_slot = [0; 3];
|
||||
let imm = [Immediates {
|
||||
view_proj: self.camera.view_proj().inverse(),
|
||||
cam_pos: self.camera.position,
|
||||
@@ -1229,11 +1242,14 @@ impl State
|
||||
table_base_slot,
|
||||
}];
|
||||
renderpass.set_immediates(0, unsafe { as_raw_bytes(&imm) });
|
||||
renderpass.dispatch_workgroups(
|
||||
self.size.width.div_ceil(16),
|
||||
self.size.height.div_ceil(16),
|
||||
1,
|
||||
);
|
||||
{
|
||||
gpu_profiler::begin_scope!(renderpass, "RT");
|
||||
renderpass.dispatch_workgroups(
|
||||
self.size.width.div_ceil(16),
|
||||
self.size.height.div_ceil(16),
|
||||
1,
|
||||
);
|
||||
}
|
||||
renderpass.set_pipeline(&self.gi_pipeline);
|
||||
renderpass.set_immediates(0, unsafe { as_raw_bytes(&imm) });
|
||||
renderpass.set_bind_group(2, Some(&gi_surface_bg), &[]);
|
||||
@@ -1261,18 +1277,20 @@ impl State
|
||||
self.target_blitter
|
||||
.copy(&self.device, &mut encoder, target_view, &texture_view);
|
||||
|
||||
self.bvh_debug.render(
|
||||
&self.device,
|
||||
&self.queue,
|
||||
&mut encoder,
|
||||
&texture_view,
|
||||
&self.depth_buffer.1,
|
||||
self.camera.view_proj(),
|
||||
self.bvh.bvh_nodes(),
|
||||
self.bvh.bvh_objects(),
|
||||
self.bvh.node_count(),
|
||||
2,
|
||||
);
|
||||
let structure_table = self.structure_table.lock();
|
||||
// self.bvh_debug.render(
|
||||
// &self.device,
|
||||
// &self.queue,
|
||||
// &mut encoder,
|
||||
// &texture_view,
|
||||
// &self.depth_buffer.1,
|
||||
// self.camera.view_proj(),
|
||||
// structure_table.bvh_nodes(),
|
||||
// structure_table.bvh_objects(),
|
||||
// structure_table.node_count(),
|
||||
// 2,
|
||||
// );
|
||||
drop(structure_table);
|
||||
|
||||
// ~~ EGUI Render pass ~~
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user