more occup

This commit is contained in:
2026-09-15 13:39:56 +02:00
parent d1f76fe9f0
commit 532580b9bd
3 changed files with 108 additions and 72 deletions
+88 -71
View File
@@ -96,6 +96,11 @@ struct StructurePointer
return (this.value & 0x40000000) != 0;
}
bool subdivided_valid()
{
return (this.value & 0xC0000000) == 0xC0000000;
}
uint32_t pointer()
{
return this.value & 0x3FFFFFFF;
@@ -193,7 +198,14 @@ struct HitInformation
float4 color;
}
HitInformation ray_march(float3 ray_direction, float3 ray_origin, uint32_t root_id, float dist_offset)
// struct NodeStack
// {
// uint32_t node_stack[5];
// }
groupshared uint32_t stack[256 * 5];
HitInformation ray_march(float3 ray_direction, float3 ray_origin, uint32_t root_id, float dist_offset, uint32_t stack_index)
{
float fov_deg = 100. / 1920.;
float fov_rad = (float.getPi() * fov_deg) / 180.;
@@ -217,34 +229,34 @@ HitInformation ray_march(float3 ray_direction, float3 ray_origin, uint32_t root_
}
ray_origin += float3(1.);
ray_origin = clamp(ray_origin , float(1.), asfloat(0x3fffffff));
ray_origin = select(ray_direction > 0., asfloat(asuint(ray_origin) ^ 0x007fffff), ray_origin);
uint32_t child_mirror = 0;
if(ray_direction.x > 0.) {child_mirror |= 3;}
if(ray_direction.y > 0.) {child_mirror |= 3 << 2;}
if(ray_direction.z > 0.) {child_mirror |= 3 << 4;}
ray_direction = -abs(ray_direction);
float3 pos = ray_origin;
pos = clamp(pos, float(1.), asfloat(0x3fffffff));
uint32_t scale_exp = 23 - 2;
uint32_t node_stack[5] =
{
0
};
uint32_t current_node_index = structure_table_pointer[root_id].pointer();
node_stack[10 - scale_exp / 2] = current_node_index;
uint32_t child_index = get_children_index(pos, scale_exp);
StructurePointer current_node = structure_pool[current_node_index].pointers[child_index];
usage_buffer[current_node_index] = constants.frame_timestamp;
stack[stack_index * 5 + 10 - scale_exp / 2] = current_node_index;
//node_stack[10 - scale_exp / 2] = current_node_index;
[loop]
for(uint32_t iter = 0; iter < 500; iter ++)
{
//scale_exp = 23 - 2;
//current_node_index = structure_table_pointer[root_id].pointer();
child_index = get_children_index(pos, scale_exp);
current_node = structure_pool[current_node_index].pointers[child_index];
uint32_t child_index = get_children_index(pos, scale_exp) ^ child_mirror;
StructurePointer current_node = structure_pool[current_node_index].pointers[child_index];
// Scale computations
//return float4(cone_size * 1000.);
let cone_size = (length(ray_origin - pos) + dist_offset) * cone_factor;
let exponent =
select(
@@ -254,15 +266,15 @@ HitInformation ray_march(float3 ray_direction, float3 ray_origin, uint32_t root_
);
while(
current_node.subdivided() &&
current_node.pointer_valid() &&
current_node.subdivided_valid() &&
scale_exp - 2 > exponent
)
{
scale_exp -= 2;
current_node_index = current_node.pointer();
node_stack[10 - scale_exp / 2] = current_node_index;
child_index = get_children_index(pos, scale_exp);
stack[stack_index * 5 + 10 - scale_exp / 2] = current_node_index;
//node_stack[10 - scale_exp / 2] = current_node_index;
child_index = get_children_index(pos, scale_exp) ^ child_mirror;
current_node = structure_pool[current_node_index].pointers[child_index];
// Write usage
usage_buffer[current_node_index] = constants.frame_timestamp;
@@ -276,8 +288,6 @@ HitInformation ray_march(float3 ray_direction, float3 ray_origin, uint32_t root_
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;
var hit: HitInformation;
hit.hit = true;
hit.hit_pos = pos - float3(1.);
@@ -285,7 +295,6 @@ HitInformation ray_march(float3 ray_direction, float3 ray_origin, uint32_t root_
return hit;
}
//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)
@@ -295,26 +304,16 @@ HitInformation ray_march(float3 ray_direction, float3 ray_origin, uint32_t root_
// Perform dda
// Compute correct exponent, and shift it into the exponent part of floatt
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;
let inter_ts : float3 = (child_pos - ray_origin) / ray_direction;
float inter_t = min(inter_ts.x, min(inter_ts.y, inter_ts.z));
//return float4(inter_t);
// 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 << adv_scale_exp) - 1));
let previous_pos : float3 = pos;
pos = clamp(ray_origin + ray_direction * inter_t, neighbor_min, neighbor_max);
/*
if(any(pos >= 2.) || any(pos < 1.))
{
discard;
}
*/
//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(ray_origin + ray_direction * inter_t, asfloat(neighbor_max));
// Find most common ancestor
uint32_t3 diffs = asuint(child_pos) ^ asuint(pos);
@@ -327,7 +326,8 @@ HitInformation ray_march(float3 ray_direction, float3 ray_origin, uint32_t root_
}
scale_exp = 23 - common_depth;
current_node_index = node_stack[10 - scale_exp / 2];
current_node_index = stack[stack_index * 5 + 10 - scale_exp / 2];
//current_node_index = node_stack[10 - scale_exp / 2];
}
@@ -342,6 +342,7 @@ struct FragmentOutput
float4 color : SV_Target<0>;
}
/*
//[earlydepthstencil]
[shader("fragment")]
FragmentOutput fragment(VertexOutput vertex_out)
@@ -361,6 +362,7 @@ FragmentOutput fragment(VertexOutput vertex_out)
return frag_out;
}
*/
bool3 min_mask(float3 val)
{
@@ -372,31 +374,47 @@ bool3 min_mask(float3 val)
[[format("rgba32f")]]
WTexture2D<float4> output_texture;
float3 get_ray_direction(uint32_t2 pixel_loc)
{
let ndc_loc_x = (float)pixel_loc.x / (float)constants.width * 2. - 1.;
let ndc_loc_y = 1. - (float)pixel_loc.y / (float)constants.height * 2.;
var world_loc = mul(constants.view_proj, float4(ndc_loc_x, ndc_loc_y, 1., 1.));
world_loc /= world_loc.w;
return normalize(world_loc.xyz - constants.cam_pos);
}
[shader("compute")]
[numthreads(16, 16, 1)]
void ray_march_compute(uint32_t3 location : SV_DispatchThreadID)
void ray_march_compute(uint32_t3 location : SV_DispatchThreadID, uint32_t3 local_location: SV_GroupThreadID)
{
if(location.x >= constants.width || location.y >= constants.height)
{
return;
}
uint32_t2 pixel_loc = uint32_t2(location.x, location.y);
let ndc_loc_x = (float)location.x / (float)constants.width * 2. - 1.;
let ndc_loc_y = 1. - (float)location.y / (float)constants.height * 2.;
var world_loc = mul(constants.view_proj, float4(ndc_loc_x, ndc_loc_y, 1., 1.));
world_loc /= world_loc.w;
let ray_direction = normalize(world_loc.xyz - constants.cam_pos);
let stack_index = local_location.x + local_location.y * 16;
let ray_direction = get_ray_direction(location.xy);
var inter = box_intersect(constants.cam_pos, ray_direction, float3(0.), float3(constants.chunk_width, constants.chunk_alt, constants.chunk_height));
// Clear if out of box
if(inter.y <= inter.x || inter.y <= 0.)
{
output_texture.Store(pixel_loc, float4(0.));
output_texture.Store(location.xy, float4(0.));
return;
}
inter.x = max(0., inter.x);
// float3 position = constants.cam_pos + ray_direction * inter.x;
// int32_t3 current_voxel = clamp(
// int32_t3(floor(position)),
// int32_t3(0),
// int32_t3(constants.chunk_width - 1, constants.chunk_alt - 1, constants.chunk_height - 1)
// );
// let chunk_index = current_voxel.y + current_voxel.z * constants.chunk_alt + current_voxel.x * constants.chunk_alt * constants.chunk_height;
// let hit = ray_march(ray_direction, position - float3(current_voxel), chunk_index, length(position - constants.cam_pos), stack_index);
// if(hit.hit)
// {
// output_texture.Store(location.xy, float4(hit.color));
// }
let start_position = inter.x * ray_direction + constants.cam_pos;
@@ -406,38 +424,37 @@ void ray_march_compute(uint32_t3 location : SV_DispatchThreadID)
int32_t3(0),
int32_t3(constants.chunk_width - 1, constants.chunk_alt - 1, constants.chunk_height - 1)
);
int32_t3 offset = int32_t3(sign(ray_direction));
float3 delta = abs(1. / ray_direction);
float3 position = start_position;
float3 t = select(ray_direction > 0., current_voxel + int32_t3(1) - start_position, start_position - current_voxel) * delta;
//int32_t3 offset = int32_t3(sign(ray_direction));
//float3 delta = abs(1. / ray_direction);
float3 t = select(ray_direction > 0., current_voxel + int32_t3(1) - start_position, start_position - current_voxel) / abs(ray_direction);
t += inter.x;
var color = float4(0.);
var hit_pos = float3(0.);
for(int32_t i = 0; i < 256; i++)
[loop]
while(true)
{
let min_mask = min_mask(t);
let t_adv = select(min_mask, t, float3(0.));
let t_ray = t_adv.x + t_adv.y + t_adv.z;
let chunk_index = current_voxel.y + current_voxel.z * constants.chunk_alt + current_voxel.x * constants.chunk_alt * constants.chunk_height;
let hit = ray_march(ray_direction, position - float3(current_voxel), chunk_index, length(position - constants.cam_pos));
position = start_position + ray_direction * t_ray;
if(hit.hit)
{
color = hit.color;
break;
let off = t - abs(1. / ray_direction);
let t_adv = max(max(off.x, max(off.y, off.z)), 0.);
let chunk_index = current_voxel.y + current_voxel.z * constants.chunk_alt + current_voxel.x * constants.chunk_alt * constants.chunk_height;
let position = constants.cam_pos + ray_direction * t_adv;
let hit = ray_march(ray_direction, position - float3(current_voxel), chunk_index, t_adv, stack_index);
if(hit.hit)
{
output_texture.Store(location.xy, hit.color);
return;
}
}
t += select(min_mask, delta, float3(0.));
current_voxel += select(min_mask, offset, int32_t3(0));
let min_mask = min_mask(t);
t += select(min_mask, abs(1. / ray_direction), float3(0.));
current_voxel += select(min_mask, int32_t3(sign(ray_direction)), int32_t3(0));
if(any(current_voxel < 0) || any(current_voxel >= int32_t3(constants.chunk_width, constants.chunk_alt, constants.chunk_height)))
{
color = float4(0.);
break;
output_texture.Store(location.xy, float4(0.));
return;
}
}
output_texture.Store(pixel_loc, float4(color));
}
float2 box_intersect(float3 origin, float3 ray_direction, float3 box_min, float3 box_max)
BIN
View File
Binary file not shown.
+20 -1
View File
@@ -159,6 +159,8 @@ impl State
required_features: Features::IMMEDIATES
| Features::SHADER_EARLY_DEPTH_TEST
| Features::TIMESTAMP_QUERY
| Features::SHADER_I16
| Features::SHADER_F16
| Features::SHADER_INT64,
required_limits: wgpu::Limits {
max_immediate_size: 112,
@@ -242,7 +244,24 @@ impl State
// };
//let shader_module = device.create_shader_module(include_wgsl!("../shaders/voxel.wgsl"));
let shader_module = device.create_shader_module(include_spirv!("../shaders/voxel.spv"));
let shader_module = unsafe {
device.create_shader_module_trusted(
wgpu::ShaderModuleDescriptor {
label: Some("../shaders/voxel.spv"),
source: wgpu::ShaderSource::SpirV(wgpu::__macro_helpers::Cow::Borrowed(
wgpu::include_spirv_source!("../shaders/voxel.spv"),
)),
},
wgpu::ShaderRuntimeChecks {
bounds_checks: false,
force_loop_bounding: false,
ray_query_initialization_tracking: false,
task_shader_dispatch_tracking: false,
mesh_shader_primitive_indices_clamp: false,
int_div_checks: false,
},
)
};
let surface_bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {