Faster coalescing

This commit is contained in:
2026-09-13 15:48:27 +02:00
parent 9015ed85d6
commit 59e3ed5f87
5 changed files with 119 additions and 22 deletions
+59 -8
View File
@@ -80,7 +80,6 @@ VertexOutput chunk(
struct StructurePointer
{
uint32_t value;
bool subdivided()
{
return (this.value & 0x80000000) != 0;
@@ -129,6 +128,8 @@ struct ByteColor
struct StructurePoolElement
{
uint32_t occupancy_low;
uint32_t occupancy_high;
StructurePointer pointers[64];
}
@@ -168,14 +169,23 @@ uint32_t get_children_index(float3 position, uint32_t scale_exp)
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);
}
float4 ray_march(float3 ray_direction, float3 ray_origin, uint32_t root_id, float dist_offset)
float4 ray_march(float3 ray_direction, float3 ray_origin, uint32_t root_id, float dist_offset, out float3 hit_pos)
{
float fov_deg = 100. / 1920.;
float fov_rad = (float.getPi() * fov_deg) / 180.;
float cone_factor = tan(fov_rad / 2.) * 2; // Horizontal size of pixel
let st_pointer = structure_table_pointer[root_id];
if(!st_pointer.subdivided())
{
@@ -216,7 +226,21 @@ float4 ray_march(float3 ray_direction, float3 ray_origin, uint32_t root_id, floa
child_index = get_children_index(pos, scale_exp);
current_node = structure_pool[current_node_index].pointers[child_index];
while(current_node.subdivided() && current_node.pointer_valid())
// Scale computations
//return float4(cone_size * 1000.);
let cone_size = (length(ray_origin - pos) + dist_offset) * cone_factor;
let exponent =
select(
cone_size == 0.,
0,
23 - (127 - (asuint(cone_size) >> 23))
);
while(
current_node.subdivided() &&
current_node.pointer_valid() &&
scale_exp - 2 > exponent
)
{
scale_exp -= 2;
current_node_index = current_node.pointer();
@@ -235,13 +259,22 @@ float4 ray_march(float3 ray_direction, float3 ray_origin, uint32_t root_id, floa
if(color_pool[current_node_index].colors[child_index].byte_a != 0)
{
hit_pos = pos - float3(1.);
return color_pool[current_node_index].colors[child_index].float_color;
}
//uint64_t occupancy = get_child_mask(structure_pool[current_node_index].occupancy_low, structure_pool[current_node_index].occupancy_high);
uint64_t occupancy = get_child_mask(structure_pool[current_node_index].occupancy_low, structure_pool[current_node_index].occupancy_high);
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_scale : float = asfloat((scale_exp - 23 + 127) << 23);
let child_pos : float3 = floor_scale(pos, scale_exp);
let child_scale : float = asfloat((adv_scale_exp - 23 + 127) << 23);
let child_pos : float3 = floor_scale(pos, adv_scale_exp);
let child_far : float3 = child_pos + select(ray_direction > 0., float3(child_scale), float3(0.));
// Intersection t
let inter_ts : float3 = (child_far - ray_origin) / ray_direction;
@@ -250,7 +283,7 @@ float4 ray_march(float3 ray_direction, float3 ray_origin, uint32_t root_id, floa
// Perform dda step
let neighbor_min : float3 = select(float3(inter_t) == inter_ts, child_pos + copysign(child_scale, ray_direction), child_pos);
let neighbor_max : float3 = asfloat(asint(neighbor_min) + ((1 << scale_exp) - 1));
let neighbor_max : float3 = asfloat(asint(neighbor_min) + ((1 << adv_scale_exp) - 1));
let previous_pos : float3 = pos;
pos = clamp(ray_origin + ray_direction * inter_t, neighbor_min, neighbor_max);
@@ -276,17 +309,35 @@ float4 ray_march(float3 ray_direction, float3 ray_origin, uint32_t root_id, floa
}
hit_pos = pos - float3(1.);
return float4(1., 0., 1., 1.);
}
struct FragmentOutput
{
float depth : SV_Depth;
float4 color : SV_Target<0>;
}
//[earlydepthstencil]
[shader("fragment")]
float4 fragment(VertexOutput vertex_out) : SV_Target<0>
FragmentOutput fragment(VertexOutput vertex_out)
{
let ray_direction = normalize(vertex_out.world_position - vertex_out.cam_position);
let intersection_t = box_intersect(vertex_out.cam_position, ray_direction, vertex_out.chunk_position, vertex_out.chunk_position + float3(1.));
let local_ray_origin = max(intersection_t.x, 0.) * ray_direction + vertex_out.cam_position - vertex_out.chunk_position;
// Figure out intersection
return ray_march(ray_direction, local_ray_origin, vertex_out.structure_id, 0.);
var hit_pos : float3;
let color = ray_march(ray_direction, local_ray_origin, vertex_out.structure_id, max(0., intersection_t.x), hit_pos);
let world_hit_pos = hit_pos + vertex_out.chunk_position;
let clip = mul(constants.view_proj, float4(world_hit_pos, 1.));
let depth = clip.z / clip.w;
var frag_out : FragmentOutput;
frag_out.depth = depth;
frag_out.color = color;
return frag_out;
}
float2 box_intersect(float3 origin, float3 ray_direction, float3 box_min, float3 box_max)