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)
BIN
View File
Binary file not shown.
+22 -12
View File
@@ -62,6 +62,7 @@ use crate::voxel_cache::data::CacheResponse;
use crate::voxel_cache::data::ColorBytes;
use crate::voxel_cache::data::DestinationElement;
use crate::voxel_cache::data::LocationPoolElement;
use crate::voxel_cache::data::StructurePoolElement;
use crate::voxel_cache::producer_interface::CacheProducerInterface;
use crate::voxel_cache::producer_interface::CacheRequest;
@@ -142,7 +143,8 @@ impl State
.request_device(&wgpu::DeviceDescriptor {
required_features: Features::IMMEDIATES
| Features::SHADER_EARLY_DEPTH_TEST
| Features::TIMESTAMP_QUERY,
| Features::TIMESTAMP_QUERY
| Features::SHADER_INT64,
required_limits: wgpu::Limits {
max_immediate_size: 112,
max_storage_buffers_per_shader_stage: 16,
@@ -161,22 +163,22 @@ impl State
let egui_renderer = EguiRenderer::new(&device, surface_format, &window);
let mut voxel_cache = VoxelCache::<4>::new(200_000, device.clone(), queue.clone());
let mut voxel_cache = VoxelCache::<4>::new(100_000, device.clone(), queue.clone());
let cache_interface = CacheProducerInterface::new(1024, &device);
let terrain_generator = TerrainGenerator::<4>::new(5, "vxls_height.tif", 0.2, "img.jpg");
//let terrain_generator = TerrainGenerator::<4>::new(5, "vxls_height.tif", 0.2, "img.jpg");
// let terrain_generator = TerrainGenerator::<4>::new(
// 5,
// "./pointe_percee/height.tif",
// 0.2,
// "./pointe_percee/ortho.jpg",
// );
// let terrain_generator = TerrainGenerator::<4>::new(
// 5,
// "/home/albin/Documents/vxls_maps/lapiz/height.tif",
// 0.2,
// "/home/albin/Documents/vxls_maps/lapiz/ortho.jpg",
// );
let terrain_generator = TerrainGenerator::<4>::new(
5,
"/home/albin/Documents/vxls_maps/lapiz/height.tif",
0.2,
"/home/albin/Documents/vxls_maps/lapiz/ortho.jpg",
);
let mut chunk_pos_map = HashMap::new();
let chunk_instances = (0..terrain_generator.chunk_width)
@@ -270,7 +272,7 @@ impl State
depth_stencil: Some(wgpu::DepthStencilState {
format: wgpu::TextureFormat::Depth24PlusStencil8,
depth_write_enabled: Some(true),
depth_compare: Some(wgpu::CompareFunction::Less),
depth_compare: Some(wgpu::CompareFunction::LessEqual),
stencil: wgpu::StencilState::default(),
bias: wgpu::DepthBiasState::default(),
}),
@@ -645,7 +647,7 @@ impl State
let generator = cloned_generator;
let gen_test = SineGenerator::<4>::new(5);
let mut structure_nodes = vec![];
let mut structure_nodes: Vec<StructurePoolElement<4>> = vec![];
let mut color_nodes: Vec<[ColorBytes; 64]> = vec![];
let mut location_nodes = vec![];
@@ -704,7 +706,15 @@ impl State
};
}
(node.structure, node.colors, location)
(
StructurePoolElement {
occupancy_low: 0,
occupancy_high: 0,
pointers: node.structure,
},
node.colors,
location,
)
})
.collect::<Vec<_>>()
.into_iter()
+34 -1
View File
@@ -346,6 +346,8 @@ where
format!("
struct StructurePoolElement
{{
occupancy_low: atomic<u32>,
occupancy_high: atomic<u32>,
pointers: array<u32, {children_count}>
}}
@@ -565,15 +567,37 @@ where
{{
// Phase 1
// Copy into cache page
var occupancy_high = u32(0);
var occupancy_low = u32(0);
for(var i = 0; i < {children_count}; i++)
{{
structure_pool[overwritten_element].pointers[i] = select(u32(0), u32(1)<<31, structure_nodes[index].pointers[i] != 0);
let turn_on_bit =
select(
u32(0),
u32(u32(1) << u32(i % ({children_count} / 2))),
((color_nodes[index].colors[i] >> 24) != 0) ||
structure_nodes[index].pointers[i] != 0
);
if(i > {children_count} / 2)
{{
occupancy_high |= turn_on_bit;
}}else
{{
occupancy_low |= turn_on_bit;
}}
}}
atomicStore(&structure_pool[overwritten_element].occupancy_low, occupancy_low);
atomicStore(&structure_pool[overwritten_element].occupancy_high, occupancy_high);
color_pool[overwritten_element] = color_nodes[index];
location_pool[overwritten_element] = locations[index];
// Mark dirty/correct timestamp
usage_buffer[overwritten_element] = parameters.frame_timestamp + 1;
usage_buffer[overwritten_element] = parameters.frame_timestamp + 1;
}}
if(parameters.write_pointers != 0 && usage_buffer[overwritten_element] != parameters.frame_timestamp)
@@ -588,6 +612,15 @@ where
}}else if usage_buffer[requests_wb[index].node_index] != parameters.frame_timestamp + 1
{{
structure_pool[requests_wb[index].node_index].pointers[requests_wb[index].child_index] = new_pointer;
let turn_on_bit = u32(1 << (requests_wb[index].child_index % ({children_count} / 2)));
if(requests_wb[index].child_index > {children_count} / 2)
{{
//atomicOr(&structure_pool[requests_wb[index].node_index].occupancy_high, turn_on_bit);
}}else
{{
//atomicOr(&structure_pool[requests_wb[index].node_index].occupancy_low, turn_on_bit);
}}
}}
}}
+4 -1
View File
@@ -24,11 +24,14 @@ where
request_count: [u32; N * N * N],
}
#[repr(C)]
pub struct StructurePoolElement<const N: usize>
where
[(); N * N * N]:,
{
pointers: [StructurePointer; N * N * N],
pub occupancy_low: u32,
pub occupancy_high: u32,
pub pointers: [StructurePointer; N * N * N],
}
pub struct DestinationElement