This commit is contained in:
2026-09-13 23:06:00 +02:00
parent 59e3ed5f87
commit d1f76fe9f0
4 changed files with 253 additions and 29 deletions
+112 -12
View File
@@ -3,6 +3,12 @@ struct PushConstants
float4x4 view_proj;
float3 cam_pos;
uint32_t frame_timestamp;
uint32_t width;
uint32_t height;
uint32_t chunk_width;
uint32_t chunk_height;
uint32_t chunk_alt;
}
public struct VertexOutput
@@ -180,7 +186,14 @@ float3 floor_scale(float3 position, uint32_t scale_exp)
return asfloat(asuint(position) & mask);
}
float4 ray_march(float3 ray_direction, float3 ray_origin, uint32_t root_id, float dist_offset, out float3 hit_pos)
struct HitInformation
{
bool hit;
float3 hit_pos;
float4 color;
}
HitInformation ray_march(float3 ray_direction, float3 ray_origin, uint32_t root_id, float dist_offset)
{
float fov_deg = 100. / 1920.;
float fov_rad = (float.getPi() * fov_deg) / 180.;
@@ -189,14 +202,18 @@ float4 ray_march(float3 ray_direction, float3 ray_origin, uint32_t root_id, floa
let st_pointer = structure_table_pointer[root_id];
if(!st_pointer.subdivided())
{
discard;
var hit: HitInformation;
hit.hit = false;
return hit;
}
if(!st_pointer.pointer_valid())
{
// Record request
structure_table_request_buffer[root_id].add(1);
discard;
var hit: HitInformation;
hit.hit = false;
return hit;
}
ray_origin += float3(1.);
@@ -259,8 +276,13 @@ 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;
//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.);
hit.color = color_pool[current_node_index].colors[child_index].float_color;
return hit;
}
//uint64_t occupancy = get_child_mask(structure_pool[current_node_index].occupancy_low, structure_pool[current_node_index].occupancy_high);
@@ -301,7 +323,7 @@ float4 ray_march(float3 ray_direction, float3 ray_origin, uint32_t root_id, floa
int32_t common_depth = (1 + (22 - firstbithigh(diff)) / 2) * 2;
if(common_depth <= 0)
{
discard;
break;
}
scale_exp = 23 - common_depth;
@@ -309,8 +331,9 @@ 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.);
var hit: HitInformation;
hit.hit = false;
return hit;
}
struct FragmentOutput
@@ -327,19 +350,96 @@ FragmentOutput fragment(VertexOutput vertex_out)
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
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 hit = ray_march(ray_direction, local_ray_origin, vertex_out.structure_id, max(0., intersection_t.x));
let world_hit_pos = hit.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;
frag_out.color = hit.color;
return frag_out;
}
bool3 min_mask(float3 val)
{
let min_val = min(val.x, min(val.y, val.z));
return val == min_val;
}
[[vk::binding(0, 1)]]
[[format("rgba32f")]]
WTexture2D<float4> output_texture;
[shader("compute")]
[numthreads(16, 16, 1)]
void ray_march_compute(uint32_t3 location : SV_DispatchThreadID)
{
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);
var inter = box_intersect(constants.cam_pos, ray_direction, float3(0.), float3(constants.chunk_width, constants.chunk_alt, constants.chunk_height));
if(inter.y <= inter.x || inter.y <= 0.)
{
output_texture.Store(pixel_loc, float4(0.));
return;
}
inter.x = max(0., inter.x);
let start_position = inter.x * ray_direction + constants.cam_pos;
// FVT
int32_t3 current_voxel = clamp(
int32_t3(floor(start_position)),
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;
var color = float4(0.);
var hit_pos = float3(0.);
for(int32_t i = 0; i < 256; i++)
{
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;
}
t += select(min_mask, delta, float3(0.));
current_voxel += select(min_mask, offset, 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(pixel_loc, float4(color));
}
float2 box_intersect(float3 origin, float3 ray_direction, float3 box_min, float3 box_max)
{
let min_ts = (box_min - origin) / ray_direction;
BIN
View File
Binary file not shown.