Revamped shader arch
This commit is contained in:
@@ -0,0 +1,264 @@
|
||||
import voxel_tracing;
|
||||
|
||||
public struct ByteColor
|
||||
{
|
||||
public uint32_t byte_color;
|
||||
|
||||
public property uint32_t byte_r
|
||||
{
|
||||
get {
|
||||
return byte_color & 0xFF;
|
||||
}
|
||||
}
|
||||
|
||||
public property uint32_t byte_g
|
||||
{
|
||||
get {
|
||||
return (byte_color >> 8) & 0xFF;
|
||||
}
|
||||
}
|
||||
|
||||
public property uint32_t byte_b
|
||||
{
|
||||
get {
|
||||
return (byte_color >> 16) & 0xFF;
|
||||
}
|
||||
}
|
||||
|
||||
public property uint32_t byte_a
|
||||
{
|
||||
get {
|
||||
return byte_color >> 24;
|
||||
}
|
||||
}
|
||||
|
||||
public property float4 float_color
|
||||
{
|
||||
get {
|
||||
return float4(
|
||||
float(byte_r) / 255.,
|
||||
float(byte_g) / 255.,
|
||||
float(byte_b) / 255.,
|
||||
float(byte_a) / 255.);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public struct StructurePointer
|
||||
{
|
||||
uint32_t value;
|
||||
|
||||
public bool subdivided()
|
||||
{
|
||||
return (this.value & 0x80000000) != 0;
|
||||
}
|
||||
|
||||
public bool pointer_valid()
|
||||
{
|
||||
return (this.value & 0x40000000) != 0;
|
||||
}
|
||||
|
||||
public bool subdivided_valid()
|
||||
{
|
||||
return (this.value & 0xC0000000) == 0xC0000000;
|
||||
}
|
||||
|
||||
public uint32_t pointer()
|
||||
{
|
||||
return this.value & 0x3FFFFFFF;
|
||||
}
|
||||
}
|
||||
|
||||
public struct StructureElement
|
||||
{
|
||||
public uint32_t occupancy_low;
|
||||
public uint32_t occupancy_high;
|
||||
public StructurePointer pointers[64];
|
||||
}
|
||||
|
||||
public struct RequestBufferElement
|
||||
{
|
||||
public Atomic<uint32_t> requests[64];
|
||||
}
|
||||
|
||||
/// Everything needed to trace within a voxelCache, to be used with a parameter block
|
||||
public struct VoxelCacheParameters<let N : uint>
|
||||
{
|
||||
public RWStructuredBuffer<StructureElement> structure_pool;
|
||||
public RWStructuredBuffer<RequestBufferElement> request_buffer;
|
||||
public RWStructuredBuffer<uint32_t> usage_buffer;
|
||||
public RWStructuredBuffer<uint32_t> attributes_pool[N];
|
||||
}
|
||||
|
||||
public struct VoxelTraceStack
|
||||
{
|
||||
public uint32_t stack[5];
|
||||
}
|
||||
|
||||
uint32_t3 get_children_pos(float3 position, uint32_t scale_exp)
|
||||
{
|
||||
return (asuint(position) >> scale_exp) & 3;
|
||||
}
|
||||
|
||||
uint32_t get_children_index(float3 position, uint32_t scale_exp)
|
||||
{
|
||||
// Get mantissa bits for this scale exp an retain bits for the specific children
|
||||
uint32_t3 cell_position = (asuint(position) >> scale_exp) & 3;
|
||||
return cell_position.x + cell_position.y * 4 + cell_position.z * 4 * 4;
|
||||
}
|
||||
|
||||
uint64_t get_child_mask(uint32_t low, uint32_t high)
|
||||
{
|
||||
return ((uint64_t)high << 32) | (uint64_t)low;
|
||||
}
|
||||
|
||||
float3 floor_scale(float3 position, uint32_t scale_exp)
|
||||
{
|
||||
uint32_t mask = ~0u << scale_exp;
|
||||
return asfloat(asuint(position) & mask);
|
||||
}
|
||||
|
||||
float node_size(uint32_t scale_exp)
|
||||
{
|
||||
let mantissa_mask = ~(~0u << 23);
|
||||
return ((1 << scale_exp) & mantissa_mask | (127 << 23)) - 1.f;
|
||||
}
|
||||
|
||||
ByteColor fetch_color<let N : int>(VoxelCacheParameters<N> p, uint32_t index, uint32_t child_index)
|
||||
{
|
||||
return ByteColor(p.attributes_pool[0][index * 64 + child_index]);
|
||||
}
|
||||
|
||||
public interface IVoxelHit<let N : uint32_t>
|
||||
{
|
||||
public bool shouldHit(VoxelCacheParameters<N> parameters, uint32_t node_index, uint32_t child_index);
|
||||
}
|
||||
|
||||
public struct VoxelCacheTracer<let N : int, H : IVoxelHit<N>> : IVoxelCache
|
||||
{
|
||||
typedef VoxelTraceStack GroupSharedData;
|
||||
|
||||
public bool report_usage;
|
||||
public bool report_requests;
|
||||
public uint32_t frame_timestamp;
|
||||
public H voxel_hit;
|
||||
public VoxelCacheParameters<N> parameters;
|
||||
|
||||
public bool trace<L : ILodPolicy>(uint32_t root, inout VoxelTraceStack stack, Ray r, L lod, float offset_t, out Hit v)
|
||||
{
|
||||
// Ray mirroring
|
||||
r.origin += float3(1.);
|
||||
r.origin = clamp(r.origin, float(1.), asfloat(0x3fffffff));
|
||||
r.origin = select(r.direction > 0., asfloat(asuint(r.origin) ^ 0x007fffff), r.origin);
|
||||
uint32_t child_mirror = 0;
|
||||
if (r.direction.x > 0.)
|
||||
{
|
||||
child_mirror |= 3;
|
||||
}
|
||||
if (r.direction.y > 0.)
|
||||
{
|
||||
child_mirror |= 3 << 2;
|
||||
}
|
||||
if (r.direction.z > 0.)
|
||||
{
|
||||
child_mirror |= 3 << 4;
|
||||
}
|
||||
|
||||
let ray_direction = -abs(r.direction);
|
||||
|
||||
float t = 0.;
|
||||
float3 pos = r.origin;
|
||||
uint32_t scale_exp = 23 - 2;
|
||||
uint32_t current_node_index = root;
|
||||
if (this.report_usage)
|
||||
{
|
||||
this.parameters.usage_buffer[current_node_index] = this.frame_timestamp;
|
||||
}
|
||||
stack.stack[10 - scale_exp / 2] = current_node_index;
|
||||
|
||||
[loop]
|
||||
for (uint32_t iter = 0; iter < 500; iter++)
|
||||
{
|
||||
uint32_t child_index = get_children_index(pos, scale_exp) ^ child_mirror;
|
||||
StructurePointer current_node = this.parameters.structure_pool[current_node_index].pointers[child_index];
|
||||
|
||||
while (current_node.subdivided_valid() && !lod.shouldStop(offset_t + t, node_size(scale_exp)))
|
||||
{
|
||||
scale_exp -= 2;
|
||||
current_node_index = current_node.pointer();
|
||||
stack.stack[10 - scale_exp / 2] = current_node_index;
|
||||
child_index = get_children_index(pos, scale_exp) ^ child_mirror;
|
||||
current_node = this.parameters.structure_pool[current_node_index].pointers[child_index];
|
||||
// Write usage
|
||||
if (this.report_usage)
|
||||
{
|
||||
this.parameters.usage_buffer[current_node_index] = frame_timestamp;
|
||||
}
|
||||
}
|
||||
|
||||
// Request subdiv
|
||||
if (this.report_requests && !lod.shouldStop(t + offset_t, scale_exp) && current_node.subdivided() && !current_node.pointer_valid())
|
||||
{
|
||||
this.parameters.request_buffer[current_node_index].requests[child_index].add(1);
|
||||
}
|
||||
|
||||
uint64_t occupancy = get_child_mask(
|
||||
this.parameters.structure_pool[current_node_index].occupancy_low,
|
||||
this.parameters.structure_pool[current_node_index].occupancy_high);
|
||||
|
||||
let occupied = ((occupancy >> child_index) & 1) != 0;
|
||||
// if (fetch_color(this.parameters, current_node_index, child_index).byte_a != 0)
|
||||
// if (fetch_color(cache, current_node_index, child_index).byte_a != 0)
|
||||
if (occupied && voxel_hit.shouldHit(this.parameters, current_node_index, child_index))
|
||||
{
|
||||
v.hit = true;
|
||||
v.node_index = current_node_index;
|
||||
v.child_index = child_index;
|
||||
v.hit_location =
|
||||
select(
|
||||
r.direction > 0.,
|
||||
asfloat(asuint(pos) ^ 0x007fffff),
|
||||
pos) -
|
||||
float3(1.);
|
||||
return true;
|
||||
}
|
||||
|
||||
uint32_t adv_scale_exp = scale_exp;
|
||||
if (((occupancy >> (child_index & 0b101010)) & 0x00330033) == 0)
|
||||
{
|
||||
adv_scale_exp++;
|
||||
}
|
||||
|
||||
// Perform dda
|
||||
// Compute correct exponent, and shift it into the exponent part of floatt
|
||||
let child_pos : float3 = floor_scale(pos, adv_scale_exp);
|
||||
// Intersection t
|
||||
let inter_ts : float3 = (child_pos - r.origin) / ray_direction;
|
||||
float inter_t = min(inter_ts.x, min(inter_ts.y, inter_ts.z));
|
||||
t = inter_t;
|
||||
// return float4(inter_t);
|
||||
|
||||
// Perform dda step
|
||||
// let neighbor_max = asint(child_pos) + select(inter_t == inter_ts, -1, (1 << adv_scale_exp) - 1);
|
||||
let neighbor_max = asint(child_pos) + select(inter_t == inter_ts, -1, (1 << adv_scale_exp) - 1);
|
||||
pos = min(r.origin + ray_direction * inter_t, asfloat(neighbor_max));
|
||||
|
||||
// Find most common ancestor
|
||||
uint32_t3 diffs = asuint(child_pos) ^ asuint(pos);
|
||||
uint32_t diff = (diffs.x | diffs.y | diffs.z);
|
||||
|
||||
int32_t common_depth = (1 + (22 - firstbithigh(diff)) / 2) * 2;
|
||||
if (common_depth <= 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
scale_exp = 23 - common_depth;
|
||||
current_node_index = stack.stack[10 - scale_exp / 2];
|
||||
}
|
||||
|
||||
v.hit = false;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user