Compare commits

Author SHA1 Message Date
octagonal 7c78085b61 starting compaction stuff 2026-09-16 20:49:44 +02:00
octagonal 5ed2f67324 adding sort that actually stops when finshed 2026-09-15 22:27:26 +02:00
octagonal 532580b9bd more occup 2026-09-15 13:39:56 +02:00
octagonal d1f76fe9f0 WTF 2026-09-13 23:06:00 +02:00
octagonal 59e3ed5f87 Faster coalescing 2026-09-13 15:48:27 +02:00
octagonal 9015ed85d6 slang fast 2026-09-12 16:16:21 +02:00
octagonal 3835a6fa78 lfs WITH files out of gitignore 2026-09-11 17:23:31 +02:00
octagonal a342643ab7 Test lfs 2026-09-11 17:17:22 +02:00
octagonal 54c5e91a7f Fix RT timer 2026-09-11 17:12:38 +02:00
19 changed files with 1314 additions and 722 deletions
+3
View File
@@ -0,0 +1,3 @@
img.jpg filter=lfs diff=lfs merge=lfs -text
img_low.jpg filter=lfs diff=lfs merge=lfs -text
vxls_height.tif filter=lfs diff=lfs merge=lfs -text
-4
View File
@@ -1,11 +1,7 @@
/target
Cargo.lock
img.jpg
img.png
img_low.jpg
imgs.tar.gz
vxls_height.tif
# Added by cargo
#
+1 -2
View File
@@ -16,10 +16,9 @@ glam = "0.33.5"
image = "0.25.10"
indicatif = "0.18.6"
itertools = "0.15.0"
ordered-float = "5.5.0"
pollster = "1.0.1"
rand = "0.10.2"
rayon = "1.12.0"
tiff = "0.11.3"
wgpu = "30"
wgpu = {version = "30", features = ["spirv"]}
winit = "0.30.13"
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
+4
View File
@@ -0,0 +1,4 @@
all: voxel.spv
%.spv: %.slang
slangc $< -O3 -fvk-use-entrypoint-name -target spirv -o $@
+124
View File
@@ -0,0 +1,124 @@
[[vk::binding(0, 0)]]
RWStructuredBuffer<uint32_t> count_buffer;
[[vk::binding(0, 1)]]
RWStructuredBuffer<uint32_t> reduced_buffer;
[[vk::binding(1, 1)]]
RWStructuredBuffer<uint32_t> sum_buffer;
[[vk::binding(2, 1)]]
RWStructuredBuffer<uint32_t> compaction_buffer;
groupshared uint32_t local_data[256 * 2];
static uint32_t THREAD_WIDTH = 256;
static uint32_t DATA_WIDTH = THREAD_WIDTH * 2;
[numthreads(256, 1, 1)]
[shader("compute")]
void block_sum(
uint32_t3 workgroup_id: SV_GroupID,
uint32_t3 local_thread_id: SV_GroupThreadID,
uint32_t3 global_thread_id: SV_DispatchThreadID)
{
// Perform sum in current block
// Copy local_datainto LDS with predicate
let thread_index = global_thread_id.x;
let local_thread_index = local_thread_id.x;
let total = count_buffer.getCount();
if (thread_index * 2 < total)
{
local_data[local_thread_index * 2] = select(count_buffer[thread_index * 2] != 0, 1, 0);
}
else
{
local_data[local_thread_index * 2] = 0;
}
if (thread_index * 2 + 1 < total)
{
local_data[local_thread_index * 2 + 1] = select(count_buffer[thread_index * 2 + 1] != 0, 1, 0);
}
else
{
local_data[local_thread_index * 2 + 1] = 0;
}
GroupMemoryBarrierWithGroupSync();
var width : uint32_t = 2;
while (width <= DATA_WIDTH)
{
let dest_index = width * (thread_index + 1) - 1;
let get_index = dest_index - (width / 2);
// println!("{}, {}", get_index, dest_index);
if (dest_index < DATA_WIDTH)
{
local_data[dest_index] += local_data[get_index];
}
width *= 2;
GroupMemoryBarrierWithGroupSync();
}
local_data[DATA_WIDTH - 1] = 0;
while (width >= 2)
{
let dest_index = width * (thread_index + 1) - 1;
let get_index = dest_index - (width / 2);
// println!("{}, {}", get_index, dest_index);
if (dest_index < DATA_WIDTH)
{
let self_data = local_data[dest_index];
local_data[dest_index] += local_data[get_index];
local_data[get_index] = self_data;
}
width /= 2;
GroupMemoryBarrierWithGroupSync();
}
// Block now contains running local sum
// Dump back to sum buffer
sum_buffer[2 * thread_index] = local_data[2 * local_thread_index];
sum_buffer[2 * thread_index + 1] = local_data[2 * local_thread_index + 1];
// Write to reduced buffer
reduced_buffer[workgroup_id.x] = local_data[DATA_WIDTH - 1];
}
[numthreads(1, 1, 1)]
[shader("compute")]
void linear_reduced_sum(
uint32_t3 workgroup_id: SV_GroupID,
uint32_t3 local_thread_id: SV_GroupThreadID,
uint32_t3 global_thread_id: SV_DispatchThreadID)
{
let size = reduced_buffer.getCount();
// Perform exclusive sum
var running_sum : uint32_t = 0;
for (uint32_t i = 0; i < size; i++)
{
let value = reduced_buffer[i];
reduced_buffer[i] = running_sum;
running_sum += value;
}
}
[numthreads(256, 1, 1)]
[shader("compute")]
void uniform_add(
uint32_t3 workgroup_id: SV_GroupID,
uint32_t3 local_thread_id: SV_GroupThreadID,
uint32_t3 global_thread_id: SV_DispatchThreadID)
{
let thread_index = global_thread_id.x;
let local_thread_index = local_thread_id.x;
// Gather
let reduced_value = reduced_buffer[global_thread_id.x];
// Apply
sum_buffer[thread_index * 2] += reduced_value;
sum_buffer[thread_index * 2 + 1] += reduced_value;
}
+471
View File
@@ -0,0 +1,471 @@
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
{
public float4 position : SV_Position;
[vk::location(0)]
public float3 world_position;
[vk::location(1)]
public nointerpolation uint32_t structure_id;
[vk::location(2)]
public float3 cam_position;
[vk::location(3)]
public float3 chunk_position;
}
[[vk::push_constant]]
uniform PushConstants constants;
[shader("vertex")]
VertexOutput chunk(
uint index: SV_VulkanVertexID,
[vk::location(0)] float3 chunk_position,
[vk::location(1)] uint id)
{
let cube_vertices : float3[8] =
float3[](
float3(0., 0., 0.),
float3(0., 0., 1.),
float3(1., 0., 1.),
float3(1., 0., 0.),
float3(0., 1., 0.),
float3(0., 1., 1.),
float3(1., 1., 1.),
float3(1., 1., 0.), );
// clang-format off
let cube_faces: int[24] = int[](
// Bottom face
1, 0, 2, 3,
// Top face
4, 5, 7, 6,
// Side faces
0, 1, 4, 5,
1, 2, 5, 6,
2, 3, 6, 7,
3, 0, 7, 4,
);
let quad_index = index / (3 * 2);
let triangle_index = index % (3 * 2);
let triangle_map: int[6] = int[](
0, 1, 2, 1, 3, 2
);
let vertex = cube_vertices[cube_faces[quad_index * 4 + triangle_map[triangle_index]]];
let output_vertex = mul(constants.view_proj, float4(vertex + chunk_position, 1.0f));
VertexOutput vertex_output;
vertex_output.position = output_vertex;
vertex_output.world_position = vertex + chunk_position;
vertex_output.structure_id = id;
vertex_output.cam_position = constants.cam_pos;
vertex_output.chunk_position = chunk_position;
return vertex_output;
}
struct StructurePointer
{
uint32_t value;
bool subdivided()
{
return (this.value & 0x80000000) != 0;
}
bool pointer_valid()
{
return (this.value & 0x40000000) != 0;
}
bool subdivided_valid()
{
return (this.value & 0xC0000000) == 0xC0000000;
}
uint32_t pointer()
{
return this.value & 0x3FFFFFFF;
}
}
struct ByteColor
{
uint32_t byte_color;
property uint32_t byte_r {
get {return byte_color & 0xFF;}
}
property uint32_t byte_g {
get {return (byte_color >> 8) & 0xFF;}
}
property uint32_t byte_b {
get {return (byte_color >> 16) & 0xFF;}
}
property uint32_t byte_a {
get {return byte_color >> 24;}
}
property float4 float_color {
get {return float4(
float(byte_r) / 255.,
float(byte_g) / 255.,
float(byte_b) / 255.,
float(byte_a) / 255.
); }
}
}
struct StructurePoolElement
{
uint32_t occupancy_low;
uint32_t occupancy_high;
StructurePointer pointers[64];
}
struct RequestBufferElement
{
Atomic<uint32_t> requests[64];
}
struct ColorPoolElement
{
ByteColor colors[64];
}
struct LocationPoolElement
{
uint32_t structure_id;
uint32_t structure_locator;
}
[[vk::binding(0, 0)]] RWStructuredBuffer<StructurePoolElement> structure_pool;
[[vk::binding(1, 0)]] RWStructuredBuffer<ColorPoolElement> color_pool;
[[vk::binding(2, 0)]] RWStructuredBuffer<LocationPoolElement> location_pool;
[[vk::binding(3, 0)]] RWStructuredBuffer<RequestBufferElement> request_buffer;
[[vk::binding(4, 0)]] RWStructuredBuffer<uint32_t> usage_buffer;
[[vk::binding(5, 0)]] RWStructuredBuffer<StructurePointer> structure_table_pointer;
[[vk::binding(6, 0)]] RWStructuredBuffer<Atomic<uint32_t>> structure_table_request_buffer;
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);
}
struct HitInformation
{
bool hit;
float3 hit_pos;
float4 color;
}
// 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.;
float cone_factor = tan(fov_rad / 2.) * 2; // Horizontal size of pixel
let st_pointer = structure_table_pointer[root_id];
if(!st_pointer.subdivided())
{
var hit: HitInformation;
hit.hit = false;
return hit;
}
if(!st_pointer.pointer_valid())
{
// Record request
structure_table_request_buffer[root_id].add(1);
var hit: HitInformation;
hit.hit = false;
return hit;
}
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;
uint32_t scale_exp = 23 - 2;
uint32_t node_stack[5] =
{
0
};
uint32_t current_node_index = structure_table_pointer[root_id].pointer();
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 ++)
{
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
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_valid() &&
scale_exp - 2 > exponent
)
{
scale_exp -= 2;
current_node_index = current_node.pointer();
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;
}
// Request subdiv
if(current_node.subdivided() && !current_node.pointer_valid())
{
request_buffer[current_node_index].requests[child_index].add(1);
}
if(color_pool[current_node_index].colors[child_index].byte_a != 0)
{
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);
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 - 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_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);
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_index * 5 + 10 - scale_exp / 2];
//current_node_index = node_stack[10 - scale_exp / 2];
}
var hit: HitInformation;
hit.hit = false;
return hit;
}
struct FragmentOutput
{
float depth : SV_Depth;
float4 color : SV_Target<0>;
}
/*
//[earlydepthstencil]
[shader("fragment")]
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
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 = 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;
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, uint32_t3 local_location: SV_GroupThreadID)
{
if(location.x >= constants.width || location.y >= constants.height)
{
return;
}
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(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;
// 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 t = select(ray_direction > 0., current_voxel + int32_t3(1) - start_position, start_position - current_voxel) / abs(ray_direction);
t += inter.x;
[loop]
while(true)
{
{
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;
}
}
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)))
{
output_texture.Store(location.xy, float4(0.));
return;
}
}
}
float2 box_intersect(float3 origin, float3 ray_direction, float3 box_min, float3 box_max)
{
let min_ts = (box_min - origin) / ray_direction;
let max_ts = (box_max - origin) / ray_direction;
let far_ts = max(min_ts, max_ts);
let near_ts = min(min_ts, max_ts);
let far_t = min(far_ts.x, min(far_ts.y, far_ts.z));
let near_t = max(near_ts.x, max(near_ts.y, near_ts.z));
return float2(near_t, far_t);
}
BIN
View File
Binary file not shown.
+399
View File
@@ -0,0 +1,399 @@
struct VertexOutput
{
@builtin(position) postion: vec4<f32>,
@location(0) @interpolate(flat) chunk_index: u32,
@location(1) color: vec4<f32>,
@location(2) cam_pos: vec3<f32>,
@location(3) world_pos: vec3<f32>,
@location(4) @interpolate(flat) structure_id: u32,
@location(5) chunk_position: vec3<f32>
}
struct ChunkImmediate
{
view_proj: mat4x4<f32>,
cam_pos: vec3<f32>,
frame_timestamp: u32,
}
var<immediate> constants: ChunkImmediate;
//var<push_constant> constants: ChunkInfo;
struct CacheChunkObject
{
transform: mat4x4<f32>,
color: vec4<f32>,
id: u32,
pointer: u32
}
struct StructurePoolElement
{
pointers: array<u32, 64>
}
struct RequestBufferElement
{
requests: array<atomic<u32>, 64>
}
struct ColorPoolElement
{
colors: array<u32, 64>
}
struct LocationPoolElement
{
structure_id: u32,
structure_locator: u32
}
struct SortedRequestsElement
{
node: u32,
child: u32
}
fn unpack_color(color: u32) -> vec4<f32>
{
return vec4<f32>(
f32(color & 0xFF) / 255.,
f32((color >> 8) & 0xFF) / 255.,
f32((color >> 16) & 0xFF) / 255.,
f32((color >> 24) & 0xFF) / 255.
);
}
@group(0) @binding(0) var<storage, read_write> structure_pool: array<StructurePoolElement>;
@group(0) @binding(1) var<storage, read_write> color_pool: array<ColorPoolElement>;
@group(0) @binding(2) var<storage, read_write> location_pool: array<LocationPoolElement>;
@group(0) @binding(3) var<storage, read_write> request_buffer: array<RequestBufferElement>;
@group(0) @binding(4) var<storage, read_write> usage_buffer: array<atomic<u32>>;
@group(0) @binding(5) var<storage, read_write> structure_table_pointer: array<u32>;
@group(0) @binding(6) var<storage, read_write> structure_table_request_buffer: array<atomic<u32>>;
struct FragmentOutput {
@location(0) color: vec4<f32>,
@builtin(frag_depth) depth: f32, // Equivalent to gl_FragDepth
}
@vertex
fn chunk(@builtin(vertex_index) index: u32, @location(0) position: vec3<f32>, @location(1) id: u32) -> @builtin(position) vec4<f32>
{
let cube_vertices = array<vec3<f32>, 8>(
vec3<f32>(0., 0., 0.),
vec3<f32>(0., 0., 1.),
vec3<f32>(1., 0., 1.),
vec3<f32>(1., 0., 0.),
vec3<f32>(0., 1., 0.),
vec3<f32>(0., 1., 1.),
vec3<f32>(1., 1., 1.),
vec3<f32>(1., 1., 0.),
);
let cube_faces = array<u32, 24>(
// Bottom face
1, 0, 2, 3,
// Top face
4, 5, 7, 6,
// Side faces
0, 1, 4, 5,
1, 2, 5, 6,
2, 3, 6, 7,
3, 0, 7, 4,
);
let quad_index = index / (3 * 2);
let triangle_index = index % (3 * 2);
let triangle_map = array<u32, 6>(
0, 1, 2, 1, 3, 2
);
let vertex = cube_vertices[cube_faces[quad_index * 4 + triangle_map[triangle_index]]];
let output_vertex = constants.view_proj * vec4<f32>(vertex + position, 1.0f);
return output_vertex;
}
struct StructureElement
{
children: array<u32, 64>
}
struct ColorElement
{
children: array<vec4<f32>, 64>
}
struct LocationElement
{
children: array<vec4<f32>, 64>
}
struct RequestElement
{
children: array<atomic<u32>, 64>
}
fn box_inter(pos: vec3<f32>, ray_dir: vec3<f32>, box_min: vec3<f32>, box_max: vec3<f32>) -> vec2<f32>
{
let box_min_t = (box_min - pos) / ray_dir;
let box_max_t = (box_max - pos) / ray_dir;
let near_ts = min(box_min_t, box_max_t);
let far_ts = max(box_min_t, box_max_t);
let far_t = min(min(far_ts.x, far_ts.y), far_ts.z);
let near_t = max(max(near_ts.x, near_ts.y), near_ts.z);
return vec2(near_t, far_t);
}
fn sdf(voxel: vec3<i32>) -> bool
{
let len = length(vec3<f32>(voxel) - vec3(128)) / 128.;
return len <= 1.;
}
fn min_vec(x: vec3<f32>) -> f32
{
return min(x.x, min(x.y, x.z));
}
fn min_mask(x: vec3<f32>) -> vec3<bool>
{
let min = min(x.x, min(x.y, x.z));
return vec3<bool>(min == x.x, min == x.y, min == x.z);
}
fn node_subdivided(node: u32) -> bool
{
return ((node >> 31) & 1) != 0;
}
fn node_pointer_valid(node: u32) -> bool
{
return ((node >> 30) & 1) != 0;
}
fn node_pointer(node: u32) -> u32
{
return node & 0x3FFFFFFF;
}
fn voxel_from_wall(position: vec3<f32>, ray_dir: vec3<f32>) -> vec3<i32>
{
let integers = round(position);
let wall_mask = min_mask(abs(position - vec3<f32>(integers)));
let offsets = select(vec3<f32>(-0.5), vec3<f32>(0.5), ray_dir > vec3(0.));
return vec3<i32>(floor(position + select(vec3<f32>(0.), offsets, wall_mask)));
}
struct HitResult
{
color: vec4<f32>,
hit_pos: vec3<f32>
}
fn new_traverse(ray_dir: vec3<f32>, ray_origin: vec3<f32>, root_id: u32, dist_offset: f32) -> HitResult
{
let max_depth = 5;
let dist_offset_voxel = dist_offset * f32(1 << u32(max_depth * 2));
let fovy_deg = 100. / 1920.;
let fovy_rad = (fovy_deg * 3.14) / 180.;
let cone_factor = tan(fovy_rad / 2.) * 2.;
let st_pointer = structure_table_pointer[root_id];
if (!node_subdivided(st_pointer))
{
discard;
var result: HitResult;
result.color = vec4(0., 1., 0., 1.);
result.hit_pos = ray_origin;
return result;
}
if(!node_pointer_valid(st_pointer))
{
// Node is subdivided, but not valid
// Send request on structure table
atomicAdd(&structure_table_request_buffer[root_id], 1);
discard;
var result: HitResult;
result.color = vec4(0., 1., 0., 1.);
result.hit_pos = ray_origin;
return result;
}
//var current_node = node_pointer(st_pointer);
var dfs_stack = array<u32, 6>(node_pointer(st_pointer), 0, 0, 0, 0, 0);
var current_depth = 0;
var current_node = dfs_stack[current_depth];
usage_buffer[current_node] = constants.frame_timestamp;
// Start location
//let voxel_dir = select(vec3(-1), vec3(1), ray_dir >= vec3(0.));
var node_shift = (max_depth - current_depth) * 2;
var child_size = 1 << u32(node_shift - 2);
var node_size = 1 << u32(node_shift);
var pos_origin = clamp(ray_origin * f32(1 << u32(max_depth * 2)), vec3(0.), vec3(f32(node_size) - 1.));
var voxel = vec3<i32>(pos_origin);
var far_t = 0.;
var inv_ray_dir = 1. / ray_dir;
var ray_positive = ray_dir > vec3(0.);
var step_dir = select(vec3(-1), vec3(1), ray_positive);
for(var iter = 0; iter < 400; iter ++)
{
// Shift into voxel position
node_shift = (max_depth - current_depth) * 2;
child_size = 1 << u32(node_shift - 2);
// Compute child position position from voxel position
var child_pos = (voxel >> vec3(u32(node_shift - 2))) & vec3(3);
// Compute child index in pointers
var child_index = child_pos.x + child_pos.y * 4 + child_pos.z * 4 * 4;
// Candidate child pointer
var pointer = structure_pool[current_node].pointers[child_index];
// Descent loop
let min_child_size = (length(vec3<f32>(voxel) - pos_origin) + dist_offset_voxel) * cone_factor;
while(node_subdivided(pointer) && node_pointer_valid(pointer) &&
f32(child_size / 4) >= min_child_size
)
{
// Descend
current_depth += 1;
// Try to descend again
node_shift = (max_depth - current_depth) * 2;
child_size = 1 << u32(node_shift - 2);
child_pos = (voxel >> vec3(u32(node_shift - 2))) & vec3(3);
current_node = node_pointer(pointer);
dfs_stack[current_depth] = current_node;
child_index = child_pos.x + child_pos.y * 4 + child_pos.z * 4 * 4;
pointer = structure_pool[current_node].pointers[child_index];
// Record usage in usage buffer
usage_buffer[current_node] = constants.frame_timestamp;
}
// If we could not descencd, request the child
if(node_subdivided(pointer) && !node_pointer_valid(pointer) &&
f32(child_size / 4) >= min_child_size)
{
// Record request
atomicAdd(&request_buffer[dfs_stack[current_depth]].requests[child_index], 1);
}
// Check color
let color = color_pool[current_node].colors[child_index];
if(((color >> 24) & 0xFF) != 0)
{
var result: HitResult;
result.color = unpack_color(color);
result.hit_pos = (far_t / f32(1 << u32(max_depth * 2))) * ray_dir + ray_origin;
return result;
}
// Advance
child_pos = voxel & vec3(i32(0xFFFFFFFF << u32(node_shift - 2)));
let far_wall = child_pos + select(vec3(0), vec3(child_size), ray_positive);
let far_wall_inter = (vec3<f32>(far_wall) - pos_origin) * inv_ray_dir;
far_t = min(min(far_wall_inter.x, far_wall_inter.y), far_wall_inter.z);
// Perform dda step on the children scale
let next_child = select(child_pos, child_pos + step_dir * vec3(child_size), vec3(far_t) == far_wall_inter);
let previous_voxel = voxel;
voxel = clamp(vec3<i32>(pos_origin + far_t * ray_dir), next_child, next_child + vec3(child_size) - vec3(1));
if any(voxel < vec3(0)) || any(voxel >= vec3(1 << u32((max_depth * 2))))
{
discard;
}
// We touched a voxel as if we explored blocks sized by the child size of the current node.
// But we might have exited the current node.
// If this is the case we have to walk back up the tree
// And then back down to the next node over
// As such we find the lowest ancestor that can contain both the privous voxel (in node) and the new voxel (out of node)
let bit_diffs = voxel ^ previous_voxel;
let bit_diffs_lowest = bit_diffs.x | bit_diffs.y | bit_diffs.z;
let common_depth = ((countLeadingZeros(bit_diffs_lowest) - i32(32 - max_depth * 2)) / 2);
current_depth = common_depth;
current_node = dfs_stack[current_depth];
}
// Iter max color
var result: HitResult;
result.color = vec4(1., 0., 1., 1.);
result.hit_pos = (far_t / f32(1 << u32(max_depth * 2))) * ray_dir + ray_origin;
return result;
}
@fragment
fn fragment() -> @location(0) vec4<f32>
{
return vec4(1., 0., 0., 1.) ;
}
@early_depth_test(less_equal)
@fragment
fn _fragment(in: VertexOutput) -> FragmentOutput
{
//frag_out.color = vec4<f32>(2 * 0.01 / (100. + 0.01 - depth * (100. - 0.01)));
let ray_dir = normalize(in.world_pos - in.cam_pos);
let interp = box_inter(in.cam_pos - in.chunk_position, ray_dir, vec3(0.), vec3(1));
let ray_origin = (in.cam_pos - in.chunk_position) + ray_dir * (max(0., interp.x));
let result = new_traverse(ray_dir, ray_origin, in.structure_id, length(in.cam_pos - (ray_origin + in.chunk_position)));
let clip_pos = constants.view_proj * vec4(result.hit_pos + in.chunk_position, 1.);
let depth = clip_pos.z / clip_pos.w;
var frag_out: FragmentOutput;
//frag_out.color = result.color;
frag_out.color = result.color;
frag_out.depth = depth;
return frag_out;
//return vec4<f32>(ray_origin, 1.);
//return frag_out;
//return vec4(interp.y / 10.);
}
/*
@fragment
fn fragment(in: VertexOutput) -> @location(0) vec4<f32>
{
let st = structure_table_pointer[0];
let subdivided = ((st >> 31) & 1) != 0;
let pointer_valid = ((st >> 30) & 1) != 0;
// Request stuff
atomicAdd(&structure_table_request_buffer[0], 1);
if(subdivided && !pointer_valid)
{
return vec4(0., 1., 0., 1.);
}
return vec4(1., 0., 0., 1.);
}
*/
+23 -310
View File
@@ -2,12 +2,11 @@ struct VertexOutput
{
@builtin(position) postion: vec4<f32>,
@location(0) @interpolate(flat) chunk_index: u32,
@location(1) ndc: vec4<f32>,
@location(2) color: vec4<f32>,
@location(3) cam_pos: vec3<f32>,
@location(4) world_pos: vec3<f32>,
@location(5) @interpolate(flat) structure_id: u32,
@location(6) chunk_position: vec3<f32>,
@location(1) color: vec4<f32>,
@location(2) cam_pos: vec3<f32>,
@location(3) world_pos: vec3<f32>,
@location(4) @interpolate(flat) structure_id: u32,
@location(5) chunk_position: vec3<f32>
}
struct ChunkImmediate
@@ -15,8 +14,6 @@ struct ChunkImmediate
view_proj: mat4x4<f32>,
cam_pos: vec3<f32>,
frame_timestamp: u32,
width: u32,
downsampling_factor: u32,
}
var<immediate> constants: ChunkImmediate;
@@ -27,7 +24,7 @@ struct CacheChunkObject
transform: mat4x4<f32>,
color: vec4<f32>,
id: u32,
pointer: u32,
pointer: u32
}
@@ -76,7 +73,10 @@ fn unpack_color(color: u32) -> vec4<f32>
@group(0) @binding(5) var<storage, read_write> structure_table_pointer: array<u32>;
@group(0) @binding(6) var<storage, read_write> structure_table_request_buffer: array<atomic<u32>>;
@group(1) @binding(0) var prepass_depth: texture_2d<f32>;
struct FragmentOutput {
@location(0) color: vec4<f32>,
@builtin(frag_depth) depth: f32, // Equivalent to gl_FragDepth
}
@vertex
fn chunk(@builtin(vertex_index) index: u32, @location(0) position: vec3<f32>, @location(1) id: u32) -> VertexOutput
@@ -119,7 +119,6 @@ fn chunk(@builtin(vertex_index) index: u32, @location(0) position: vec3<f32>, @l
var output: VertexOutput;
output.postion = output_vertex;
output.ndc = output_vertex / output_vertex.w;
output.color = vec4(1.);
output.chunk_index = 0;
output.cam_pos = constants.cam_pos;
@@ -217,11 +216,12 @@ fn new_traverse(ray_dir: vec3<f32>, ray_origin: vec3<f32>, root_id: u32, dist_of
{
let max_depth = 5;
let dist_offset_voxel = dist_offset * f32(1 << u32(max_depth * 2));
let fovy_deg = 100. / f32(constants.width);
let fovy_deg = 100. / 1920.;
let fovy_rad = (fovy_deg * 3.14) / 180.;
let cone_factor = 1.414 * f32(constants.downsampling_factor) * (tan(fovy_rad / 2.) * 2.); // How many pixels per distance a voxel takes
let cone_factor = tan(fovy_rad / 2.) * 2.;
let st_pointer = structure_table_pointer[root_id];
if (!node_subdivided(st_pointer))
{
@@ -245,11 +245,11 @@ fn new_traverse(ray_dir: vec3<f32>, ray_origin: vec3<f32>, root_id: u32, dist_of
}
//var current_node = node_pointer(st_pointer);
// Record usage
var dfs_stack = array<u32, 6>(node_pointer(st_pointer), 0, 0, 0, 0, 0);
var current_depth = 0;
var current_node = dfs_stack[current_depth];
usage_buffer[dfs_stack[current_depth]] = constants.frame_timestamp;
usage_buffer[current_node] = constants.frame_timestamp;
// Start location
//let voxel_dir = select(vec3(-1), vec3(1), ray_dir >= vec3(0.));
@@ -264,7 +264,6 @@ fn new_traverse(ray_dir: vec3<f32>, ray_origin: vec3<f32>, root_id: u32, dist_of
var inv_ray_dir = 1. / ray_dir;
var ray_positive = ray_dir > vec3(0.);
var step_dir = select(vec3(-1), vec3(1), ray_positive);
var min_child_size = cone_factor * dist_offset_voxel;
for(var iter = 0; iter < 400; iter ++)
{
@@ -274,18 +273,17 @@ fn new_traverse(ray_dir: vec3<f32>, ray_origin: vec3<f32>, root_id: u32, dist_of
var child_pos = (voxel >> vec3(u32(node_shift - 2))) & vec3(3);
var child_index = child_pos.x + child_pos.y * 4 + child_pos.z * 4 * 4;
var current_node = dfs_stack[current_depth];
var pointer = structure_pool[current_node].pointers[child_index];
min_child_size = (length(vec3<f32>(voxel) - pos_origin) + dist_offset_voxel) * cone_factor;
let min_child_size = (length(vec3<f32>(voxel) - pos_origin) + dist_offset_voxel) * cone_factor;
while(node_subdivided(pointer) &&
f32(child_size) / 4 > min_child_size
f32(child_size / 4) >= min_child_size
)
{
if(!node_pointer_valid(pointer) && node_subdivided(pointer))
{
// Record request
atomicAdd(&request_buffer[current_node].requests[child_pos.x + child_pos.y * 4 + child_pos.z * 4 * 4], 1);
atomicAdd(&request_buffer[dfs_stack[current_depth]].requests[child_index], 1);
break;
}
@@ -295,9 +293,9 @@ fn new_traverse(ray_dir: vec3<f32>, ray_origin: vec3<f32>, root_id: u32, dist_of
node_shift = (max_depth - current_depth) * 2;
child_size = 1 << u32(node_shift - 2);
child_pos = (voxel >> vec3(u32(node_shift - 2))) & vec3(3);
child_index = child_pos.x + child_pos.y * 4 + child_pos.z * 4 * 4;
current_node = node_pointer(pointer);
dfs_stack[current_depth] = current_node;
child_index = child_pos.x + child_pos.y * 4 + child_pos.z * 4 * 4;
pointer = structure_pool[current_node].pointers[child_index];
@@ -312,9 +310,7 @@ fn new_traverse(ray_dir: vec3<f32>, ray_origin: vec3<f32>, root_id: u32, dist_of
{
var result: HitResult;
result.color = unpack_color(color);
//result.color = vec4<f32>(f32(iter) / 400.);
//result.hit_pos = (far_t / f32(1 << u32(max_depth * 2))) * ray_dir + ray_origin;
result.hit_pos = (far_t * ray_dir + pos_origin) / f32(1 << u32(max_depth * 2));
result.hit_pos = (far_t / f32(1 << u32(max_depth * 2))) * ray_dir + ray_origin;
return result;
}
@@ -349,7 +345,7 @@ fn new_traverse(ray_dir: vec3<f32>, ray_origin: vec3<f32>, root_id: u32, dist_of
let common_depth = ((countLeadingZeros(bit_diffs_lowest) - i32(32 - max_depth * 2)) / 2);
current_depth = common_depth;
//current_node = dfs_stack[current_depth];
current_node = dfs_stack[current_depth];
}
// Iter max color
@@ -359,105 +355,23 @@ fn new_traverse(ray_dir: vec3<f32>, ray_origin: vec3<f32>, root_id: u32, dist_of
return result;
}
struct FragmentOutput {
@location(0) color: vec4<f32>,
@builtin(frag_depth) depth: f32, // Equivalent to gl_FragDepth
}
struct FragmentPrepassOutput {
@location(0) depth_prepass: f32, // Equivalent to gl_FragDepth
//@builtin(frag_depth) depth: f32, // Equivalent to gl_FragDepth
}
//fn fragment_prepass(in: VertexOutput) -> @location(0) vec4<f32>
//@early_depth_test(less_equal)
@fragment
fn fragment_prepass(in: VertexOutput) -> FragmentPrepassOutput
{
let ray_dir = normalize(in.world_pos - in.cam_pos);
let interp = box_inter(in.cam_pos, ray_dir, in.chunk_position + vec3(0.), in.chunk_position + vec3(1));
let ray_origin = in.cam_pos + ray_dir * max(interp.x, 0.) - in.chunk_position;
let result = new_traverse(ray_dir, ray_origin, in.structure_id, length(in.cam_pos - (ray_origin + in.chunk_position)));
let clip_pos = constants.view_proj * vec4(result.hit_pos + in.chunk_position, 1.);
let depth = clip_pos.z / clip_pos.w;
var frag_out: FragmentPrepassOutput;
//frag_out.color = result.color;
//frag_out.depth_prepass = in.postion.z;
frag_out.depth_prepass = length(ray_origin + in.chunk_position - in.cam_pos);
//frag_out.depth_prepass = interp.x;
//frag_out.depth_prepass = 100.;
//frag_out.depth = depth;
//frag_out.depth = depth;
//frag_out.depth_prepass = depth;
return frag_out;
}
@early_depth_test(less_equal)
@fragment
fn fragment(in: VertexOutput) -> FragmentOutput
{
let surface_depth = in.postion.z;
let prepass_depth_sample = textureLoad(prepass_depth, vec2<i32>(in.postion.xy), 0).x;
let lin_depth = (100. * 0.01) / (100. - surface_depth * (100. - 0.01));
if lin_depth < prepass_depth_sample || prepass_depth_sample == -1.
{
discard;
}
//frag_out.color = vec4<f32>(2 * 0.01 / (100. + 0.01 - depth * (100. - 0.01)));
let ray_dir = normalize(in.world_pos - in.cam_pos);
let prepass_origin = in.cam_pos + ray_dir * max(prepass_depth_sample - 0.01, 0.);
let interp = box_inter(prepass_origin, ray_dir, in.chunk_position + vec3(0.), in.chunk_position + vec3(1));
let ray_origin = prepass_origin + ray_dir * max(interp.x, 0.) - in.chunk_position;
//let ray_origin = in.cam_pos + ray_dir * (max(0., lin_depth - 0.1)) - in.chunk_position;
//let ray_origin = in.cam_pos + ray_dir * max(interp.x, 0.) - in.chunk_position;
//let ray_origin = in.cam_pos + ray_dir * max(prepass_depth, 0.) - in.chunk_position;
//let space_ro = in.cam_pos + ray_dir * lin_depth;
//let ray_origin = space_ro - in.chunk_position;
let result = new_traverse(ray_dir, ray_origin, in.structure_id, length(in.cam_pos - (ray_origin + in.chunk_position)));
let clip_pos = constants.view_proj * vec4(result.hit_pos + in.chunk_position, 1.);
let depth = clip_pos.z / clip_pos.w;
var frag_out: FragmentOutput;
frag_out.color = result.color;
//frag_out.color = vec4<f32>(vec3<f32>(prepass_depth_sample) / 100., 1.);
//frag_out.color = vec4<f32>(smpl);
//frag_out.color = vec4<f32>(ray_origin, 1.);
//frag_out.color = result.color;
//frag_out.color = vec4<f32>(ray_origin, 1.);
//frag_out.color = vec4<f32>(vec3<f32>(lin_depth) / 100., 1.);
frag_out.depth = depth;
return frag_out;
//return vec4<f32>(ray_origin, 1.);
//return frag_out;
//return vec4(interp.y / 10.);
}
@fragment
fn _fragment(in: VertexOutput) -> FragmentOutput
{
//frag_out.color = vec4<f32>(2 * 0.01 / (100. + 0.01 - depth * (100. - 0.01)));
let ray_dir = normalize(in.world_pos - in.cam_pos);
let interp = box_inter(in.cam_pos - in.chunk_position, ray_dir, vec3(0.), vec3(1));
let ray_origin = (in.cam_pos - in.chunk_position) + ray_dir * (max(0., interp.x));
let result = new_traverse(ray_dir, ray_origin, in.structure_id, length(in.cam_pos - (ray_origin + in.chunk_position)));
let clip_pos = constants.view_proj * vec4(result.hit_pos + in.chunk_position, 1.);
let depth = clip_pos.z / clip_pos.w;
var frag_out: FragmentOutput;
//frag_out.color = result.color;
frag_out.color = result.color;
//frag_out.color = vec4<f32>(smpl);
//frag_out.color = vec4<f32>((2. * 0.01 * 100.) / (0.01 + 100. - prepass_depth * (100. - 0.01)));
//frag_out.depth = depth;
frag_out.depth = depth;
return frag_out;
//return vec4<f32>(ray_origin, 1.);
@@ -482,204 +396,3 @@ fn fragment(in: VertexOutput) -> @location(0) vec4<f32>
}
*/
/*
fn traverse(ray_dir: vec3<f32>, ray_origin: vec3<f32>, root_id: u32, dist_offset: f32) -> vec4<f32>
{
let st_pointer = structure_table_pointer[root_id];
if (!node_subdivided(st_pointer))
{
discard;
return vec4(0., 1., 0., 1.);
}
if(!node_pointer_valid(st_pointer))
{
atomicAdd(&structure_table_request_buffer[root_id], 1);
discard;
return vec4(0., 1., 0., 1.);
}
let fovy_deg = 100.;
let fovy = 3.14159 * (fovy_deg / 180.);
let definition = 1920.;
let cone_fovy = fovy / definition;
let factor = 1.;
let cone_size_factor = 2. * tan(cone_fovy) * factor;
// Current depth of the node we are exploring
var current_depth = 0;
// Index of the current node's data
var current_node = u32(st_pointer & 0x3FFFFFFF);
usage_buffer[current_node] = constants.frame_timestamp;
var dfs_stack = array<u32, 6>(current_node, 0, 0, 0, 0, 0);
// Lut of the node_size per depth
var node_size_lut = array<i32, 6>(
4 * 4 * 4 * 4 * 4,
4 * 4 * 4 * 4,
4 * 4 * 4,
4 * 4,
4,
1,
);
let local_dist_offset = dist_offset * f32(node_size_lut[0]);
// Current node size
var node_size = node_size_lut[0]; // 128
// Size of a child of this node
var child_size = node_size / 4;
// Simple FVT
let t_off = abs(1. / ray_dir);
// Start location
let voxel_dir = select(vec3(-1), vec3(1), ray_dir >= vec3(0.));
var pos_origin = clamp(ray_origin * f32(node_size), vec3(0.), vec3(f32(node_size) - 1.));
var voxel = vec3<i32>(pos_origin);
var last_voxel = voxel;
let wall_offset = select(vec3(0), vec3(1), ray_dir > vec3(0.));
let max_depth = u32(5);
var adaptive_depth = i32(max_depth);
var far_t = 0.;
let ray_dir_inv = 1. / ray_dir;
let fma_offset = - pos_origin * ray_dir_inv;
//let depth_limit = 3;
for(var iter = 0; iter < 400; iter ++)
{
// Our ray is currently touching a voxel.
// Descend to the lowest node that contains this voxel
// Position of the child we are in
var child_pos = (vec3<u32>(voxel) >> vec3<u32>((max_depth - u32(current_depth + 1)) * 2)) & vec3<u32>(3); // Hardcode for 4-tree
var child_index = child_pos.x + child_pos.y * 4 + child_pos.z * 4 * 4;
// Current node has been used. report
usage_buffer[current_node] = constants.frame_timestamp;
while(
node_subdivided(structure_pool[current_node].pointers[child_index]) &&
(local_dist_offset + far_t) * cone_size_factor < f32(node_size_lut[current_depth])
)
{
if(!node_pointer_valid(structure_pool[current_node].pointers[child_index]))
{
atomicAdd(&request_buffer[current_node].requests[child_index], 1);
break;
}
// Child node is subdivided, we go in, save position in stack
current_node = node_pointer(structure_pool[current_node].pointers[child_index]);
usage_buffer[current_node] = constants.frame_timestamp;
current_depth += 1;
dfs_stack[current_depth] = current_node;
node_size = node_size_lut[current_depth];
child_pos = (vec3<u32>(voxel) >> vec3<u32>((max_depth - u32(current_depth + 1)) * 2)) & vec3<u32>(3); // Hardcode for 4-tree
child_index = child_pos.x + child_pos.y * 4 + child_pos.z * 4 * 4;
child_size = node_size / 4;
}
usage_buffer[current_node] = constants.frame_timestamp;
// At this point current_depth is the depth of the node that contains the voxel
// child_pos and child_index relate to the specific child of the node that contains this voxel
// It is guaranteed that the child is leave
// Check current leave's color
let color = unpack_color(color_pool[current_node].colors[child_index]);
if(color.w != 0.) // Not transparent
{
/*
let k = child_pos.x + child_pos.y + child_pos.z;
let w = voxel.x + voxel.y + voxel.z;
let x = select(0.5, 1., k % 2 == 0) * select(0.8, 1., w % 2 == 0);
var div = 1;
var overlay = 1.;
for(var i = 1; i <= 5; i++)
{
let x = (voxel.x / div + voxel.y / div + voxel.z / div) % 2 == 0;
overlay -= select(0., 1. / (f32(i) * 2.5), x);
div *= 4;
}
*/
return color;
//return overlay * color;
}
// Voxel and whole child containing it is empty
// Perform a step through the children of the node
let child_position = (voxel / child_size) * child_size;
let far_corner = child_position + wall_offset * child_size;
//let far_ts = (vec3<f32>(far_corner) - pos_origin) / ray_dir; // TODO: Turn into fma
let far_ts = fma(vec3<f32>(far_corner), ray_dir_inv, fma_offset);
far_t = min(min(far_ts.x, far_ts.y), far_ts.z);
let next_child_min = select(child_position, child_position + voxel_dir * child_size, vec3(far_t) == far_ts);
let next_child_max = next_child_min + vec3(child_size) - vec3(1);
// The ray (far_t) is now touching the new child to explore
// Find out which actual voxel we are touching
let previous_voxel = voxel;
let float_voxel = clamp(vec3<i32>(pos_origin + far_t * ray_dir), next_child_min, next_child_max);
/*
voxel = vec3<i32>(
floor(
select(
float_voxel - vec3(0.5),
float_voxel + vec3(0.5),
ray_dir > vec3(0.)
))
);
*/
//voxel = vec3<i32>(round(float_voxel));
//voxel = voxel_from_wall(float_voxel, ray_dir);
//voxel = voxel_from_wall(float_voxel, ray_dir);
voxel = float_voxel;
if(any(voxel < vec3(0)) || any(voxel >= vec3(node_size_lut[0])))
{
//return vec4(f32(iter) / 100.);
discard;
}
// We touched a voxel as if we explored blocks sized by the child size of the current node.
// But we might have exited the current node.
// If this is the case we have to walk back up the tree
// And then back down to the next node over
// As such we find the lowest ancestor that can contain both the privous voxel (in node) and the new voxel (out of node)
let bit_diffs = voxel ^ previous_voxel;
let bit_diffs_lowest = bit_diffs.x | bit_diffs.y | bit_diffs.z;
let flb = ((countLeadingZeros(bit_diffs_lowest) - i32(32 - max_depth * 2)) / 2);
let common_depth = flb;
current_depth = common_depth;
node_size = node_size_lut[current_depth];
child_size = node_size / 4;
current_node = dfs_stack[current_depth];
// Figure out current voxel position
//voxel = vec3<i32>(ray_origin + ray_dir * t);
}
return vec4<f32>(1., 0., 1., 1.);
}
*/
+185 -388
View File
@@ -2,6 +2,7 @@
#![feature(float_algebraic)]
use std::collections::HashMap;
use std::ops::Div;
use std::sync::Arc;
use std::sync::mpsc::sync_channel;
@@ -16,15 +17,12 @@ use glam::Mat4;
use glam::Vec3;
use glam::Vec4;
use itertools::Itertools;
use ordered_float::OrderedFloat;
use rayon::iter::IndexedParallelIterator;
use rayon::iter::IntoParallelRefIterator;
use rayon::iter::ParallelIterator;
use wgpu::BindGroupLayout;
use wgpu::BlendState;
use wgpu::Buffer;
use wgpu::BufferUsages;
use wgpu::Color;
use wgpu::ComputePipeline;
use wgpu::Device;
use wgpu::Extent3d;
@@ -34,13 +32,13 @@ use wgpu::InstanceFlags;
use wgpu::MemoryBudgetThresholds;
use wgpu::Operations;
use wgpu::Origin3d;
use wgpu::PipelineCompilationOptions;
use wgpu::RenderPipeline;
use wgpu::ShaderStages;
use wgpu::Texture;
use wgpu::TextureFormat;
use wgpu::TextureUsages;
use wgpu::TextureView;
use wgpu::include_spirv;
use wgpu::include_wgsl;
use wgpu::util::BufferInitDescriptor;
use wgpu::util::DeviceExt;
use wgpu::util::DownloadBuffer;
@@ -69,12 +67,12 @@ 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;
mod camera;
mod egui_renderer;
mod host_production;
mod producers;
mod sparse_tree;
mod voxel_cache;
@@ -89,18 +87,14 @@ struct State
size: winit::dpi::PhysicalSize<u32>,
surface: wgpu::Surface<'static>,
depth_buffer: (wgpu::Texture, wgpu::TextureView),
prepass_depth_buffer: (wgpu::Texture, wgpu::TextureView),
prepass_depth: (wgpu::Texture, wgpu::TextureView),
upsampled_prepass_depth: (wgpu::Texture, wgpu::TextureView),
prepass_downsampling: u32,
upsample_pipeline: ComputePipeline,
target_texture: (wgpu::Texture, wgpu::TextureView),
surface_format: wgpu::TextureFormat,
target_blitter: wgpu::util::TextureBlitter,
egui_renderer: EguiRenderer,
pipeline: RenderPipeline,
prepass_pipeline: RenderPipeline,
prepass_upsample_bg_layout: BindGroupLayout,
prepass_depth_bind_group_layout: BindGroupLayout,
//pipeline: RenderPipeline,
pipeline: ComputePipeline,
surface_bg_layout: BindGroupLayout,
voxel_cache: Arc<Mutex<VoxelCache<4>>>,
cache_interface: Arc<CacheProducerInterface<4>>,
terrain_generator: Arc<TerrainGenerator<4>>,
@@ -127,7 +121,11 @@ struct Immediates
cam_pos: Vec3,
frame_timestamp: u32,
width: u32,
downsample_factor: u32,
height: u32,
chunk_width: u32,
chunk_height: u32,
chunk_alt: u32,
}
#[derive(Debug, Clone, Copy, Zeroable, Pod)]
@@ -148,6 +146,7 @@ impl State
backends: wgpu::Backends::VULKAN,
display: Some(Box::new(display)),
//flags: InstanceFlags::default() | InstanceFlags::debugging(),
flags: InstanceFlags::default(),
memory_budget_thresholds: MemoryBudgetThresholds::default(),
backend_options: Default::default(),
@@ -160,7 +159,10 @@ impl State
.request_device(&wgpu::DeviceDescriptor {
required_features: Features::IMMEDIATES
| Features::SHADER_EARLY_DEPTH_TEST
| Features::TIMESTAMP_QUERY,
| Features::TIMESTAMP_QUERY
| Features::SHADER_I16
| Features::SHADER_F16
| Features::SHADER_INT64,
required_limits: wgpu::Limits {
max_immediate_size: 112,
max_storage_buffers_per_shader_stage: 16,
@@ -180,10 +182,9 @@ impl State
let egui_renderer = EguiRenderer::new(&device, surface_format, &window);
let mut voxel_cache = VoxelCache::<4>::new(100_000, device.clone(), queue.clone());
let cache_interface = CacheProducerInterface::new(1024, &device);
let cache_interface = CacheProducerInterface::new(256, &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",
@@ -192,9 +193,9 @@ impl State
// );
// let terrain_generator = TerrainGenerator::<4>::new(
// 5,
// "/home/albin/Documents/vxls_maps/orgere/height.tif",
// "/home/albin/Documents/vxls_maps/lapiz/height.tif",
// 0.2,
// "/home/albin/Documents/vxls_maps/orgere/color.jpg",
// "/home/albin/Documents/vxls_maps/lapiz/ortho.jpg",
// );
let mut chunk_pos_map = HashMap::new();
@@ -219,110 +220,88 @@ impl State
let instance_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Instance buffer"),
contents: bytemuck::cast_slice(chunk_instances.as_slice()),
usage: BufferUsages::COPY_DST | BufferUsages::VERTEX,
usage: BufferUsages::COPY_DST | BufferUsages::VERTEX | BufferUsages::STORAGE,
});
let shader_module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("Main shader module"),
source: wgpu::ShaderSource::Wgsl(
std::fs::read_to_string("shaders/voxel.wgsl")
.unwrap()
.into(),
),
});
// let shader_module = unsafe {
// device.create_shader_module_trusted(
// wgpu::ShaderModuleDescriptor {
// label: Some("Main shader module"),
// source: wgpu::ShaderSource::Wgsl(
// std::fs::read_to_string("shaders/voxel.wgsl")
// .unwrap()
// .into(),
// ),
// },
// 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 prepass_pipeline_layout =
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("Voxel pipeline layout"),
//let shader_module = device.create_shader_module(include_wgsl!("../shaders/voxel.wgsl"));
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,
},
)
};
bind_group_layouts: &[Some(&voxel_cache.bind_group_layout())],
immediate_size: size_of::<Immediates>() as u32,
});
let prepass_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("Render pipeline"),
layout: Some(&prepass_pipeline_layout),
vertex: wgpu::VertexState {
module: &shader_module,
entry_point: Some("chunk"),
compilation_options: Default::default(),
buffers: &[Some(wgpu::VertexBufferLayout {
array_stride: (size_of::<f32>() * 3 + size_of::<u32>()) as u64,
step_mode: wgpu::VertexStepMode::Instance,
attributes: &[
wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32x3,
offset: 0,
shader_location: 0,
},
wgpu::VertexAttribute {
format: wgpu::VertexFormat::Uint32,
offset: (size_of::<f32>() * 3) as u64,
shader_location: 1,
},
],
})],
},
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
strip_index_format: None,
front_face: wgpu::FrontFace::Ccw,
cull_mode: Some(wgpu::Face::Front),
unclipped_depth: false,
polygon_mode: wgpu::PolygonMode::Fill,
conservative: false,
},
depth_stencil: Some(wgpu::DepthStencilState {
format: wgpu::TextureFormat::Depth24PlusStencil8,
depth_write_enabled: Some(true),
depth_compare: Some(wgpu::CompareFunction::Less),
stencil: wgpu::StencilState::default(),
bias: wgpu::DepthBiasState::default(),
}),
multisample: wgpu::MultisampleState::default(),
fragment: Some(wgpu::FragmentState {
module: &shader_module,
entry_point: Some("fragment_prepass"),
compilation_options: wgpu::PipelineCompilationOptions::default(),
targets: &[Some(wgpu::ColorTargetState {
format: TextureFormat::R32Float,
blend: None,
write_mask: wgpu::ColorWrites::all(),
})],
}),
multiview_mask: None,
cache: None,
});
let prepass_depth_bind_group_layout =
let surface_bind_group_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("prepass_upsample_bind_group_layout "),
label: Some("surface_bg_layout"),
entries: &[wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: ShaderStages::FRAGMENT,
ty: wgpu::BindingType::Texture {
sample_type: wgpu::TextureSampleType::Float { filterable: false },
visibility: ShaderStages::COMPUTE,
ty: wgpu::BindingType::StorageTexture {
access: wgpu::StorageTextureAccess::WriteOnly,
format: wgpu::TextureFormat::Rgba32Float,
view_dimension: wgpu::TextureViewDimension::D2,
multisampled: false,
},
count: None,
}],
});
let chunk_pipeline_layout =
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("Voxel pipeline layout"),
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("Voxel pipeline layout"),
bind_group_layouts: &[
Some(&voxel_cache.bind_group_layout()),
Some(&prepass_depth_bind_group_layout),
],
immediate_size: size_of::<Immediates>() as u32,
});
bind_group_layouts: &[
Some(&voxel_cache.bind_group_layout()),
Some(&surface_bind_group_layout),
],
immediate_size: size_of::<Immediates>() as u32,
});
let chunk_pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
label: Some("Compute render"),
layout: Some(&pipeline_layout),
module: &shader_module,
entry_point: Some("ray_march_compute"),
compilation_options: wgpu::PipelineCompilationOptions::default(),
cache: None,
});
/*
let chunk_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("Render pipeline"),
layout: Some(&chunk_pipeline_layout),
layout: Some(&pipeline_layout),
vertex: wgpu::VertexState {
module: &shader_module,
entry_point: Some("chunk"),
@@ -356,7 +335,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(),
}),
@@ -374,85 +353,7 @@ impl State
multiview_mask: None,
cache: None,
});
let prepass_upsample_bg_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("prepass_upsample_bg"),
entries: &[
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: ShaderStages::COMPUTE,
ty: wgpu::BindingType::StorageTexture {
access: wgpu::StorageTextureAccess::ReadOnly,
format: wgpu::TextureFormat::R32Float,
view_dimension: wgpu::TextureViewDimension::D2,
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 1,
visibility: ShaderStages::COMPUTE,
ty: wgpu::BindingType::StorageTexture {
access: wgpu::StorageTextureAccess::WriteOnly,
format: wgpu::TextureFormat::R32Float,
view_dimension: wgpu::TextureViewDimension::D2,
},
count: None,
},
],
});
let prepass_upsample_layout =
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("prepass_upsample_layout"),
bind_group_layouts: &[Some(&prepass_upsample_bg_layout)],
immediate_size: 0,
});
let prepass_downsampling = 4;
let prepass_upsample = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
label: Some("prepass_upsample"),
layout: Some(&prepass_upsample_layout),
module: &device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("prepass_upsample_module"),
source: wgpu::ShaderSource::Wgsl(
format!(
"
@group(0) @binding(0) var input_tex: texture_storage_2d<r32float, read>;
@group(0) @binding(1) var output_tex: texture_storage_2d<r32float, write>;
@compute @workgroup_size(8, 8)
fn main(@builtin(global_invocation_id) id: vec3<u32>)
{{
let source_pos = vec2<i32>(id.xy);
var depth = textureLoad(input_tex, source_pos + vec2<i32>(0, 0)).x;
depth = min(depth, textureLoad(input_tex, source_pos + vec2<i32>(1, 0)).x);
depth = min(depth, textureLoad(input_tex, source_pos + vec2<i32>(0, 1)).x);
depth = min(depth, textureLoad(input_tex, source_pos + vec2<i32>(1, 1)).x);
let dest_pos = source_pos * {prepass_downsampling};
for(var ox = 0; ox < {prepass_downsampling}; ox ++)
{{
for(var oy = 0; oy < {prepass_downsampling}; oy ++)
{{
textureStore(
output_tex,
dest_pos + vec2<i32>(ox, oy),
vec4<f32>(depth)
);
}}
}}
}}
"
)
.into(),
),
}),
entry_point: Some("main"),
compilation_options: PipelineCompilationOptions::default(),
cache: None,
});
*/
let state = State {
instance,
@@ -462,33 +363,18 @@ impl State
surface_format,
egui_renderer,
depth_buffer: Self::create_depth_buffer(&device, size.width, size.height),
prepass_depth_buffer: Self::create_depth_buffer(
&device,
size.width / prepass_downsampling,
size.height / prepass_downsampling,
),
prepass_depth: Self::create_prepass_depth_buffer(
&device,
size.width / prepass_downsampling,
size.height / prepass_downsampling,
),
upsampled_prepass_depth: Self::create_prepass_depth_buffer(
&device,
size.width,
size.height,
),
prepass_downsampling,
upsample_pipeline: prepass_upsample,
target_texture: Self::create_target_texture(&device, size.width, size.height),
target_blitter: wgpu::util::TextureBlitterBuilder::new(&device, surface_format)
.sample_type(wgpu::FilterMode::Nearest)
.build(),
queue,
device,
surface_bg_layout: surface_bind_group_layout,
usage_vec: Arc::new(Mutex::new(vec![])),
pipeline: chunk_pipeline,
prepass_pipeline,
prepass_upsample_bg_layout,
prepass_depth_bind_group_layout,
voxel_cache: Arc::new(Mutex::new(voxel_cache)),
cache_interface: cache_interface.into(),
insertion_debounce: true,
insertion_debounce: false,
camera: Default::default(),
instance_buffer,
instance_count,
@@ -503,6 +389,27 @@ impl State
state
}
fn create_target_texture(device: &Device, width: u32, height: u32) -> (Texture, TextureView)
{
let texture = device.create_texture(&wgpu::TextureDescriptor {
label: Some("Target texture"),
size: Extent3d {
width,
height,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba32Float,
usage: TextureUsages::STORAGE_BINDING | TextureUsages::TEXTURE_BINDING,
view_formats: &[],
});
let texture_view = texture.create_view(&wgpu::wgt::TextureViewDescriptor::default());
(texture, texture_view)
}
fn get_window(&self) -> &Window
{
&self.window
@@ -510,19 +417,6 @@ impl State
fn handle_event(&mut self, event: &WindowEvent)
{
if let WindowEvent::KeyboardInput { event, .. } = event
{
match (event.state, event.physical_key)
{
(
winit::event::ElementState::Pressed,
winit::keyboard::PhysicalKey::Code(winit::keyboard::KeyCode::KeyF),
) => self.insertion_debounce = !self.insertion_debounce,
_ =>
{}
}
}
self.egui_renderer.handle_input(&self.window, event);
self.camera.handle_input(event);
}
@@ -542,35 +436,6 @@ impl State
}
}
fn create_prepass_depth_buffer(
device: &Device,
width: u32,
height: u32,
) -> (Texture, TextureView)
{
let texture = device.create_texture(&wgpu::wgt::TextureDescriptor {
label: Some("Prepass Depth buffer"),
size: Extent3d {
width,
height,
depth_or_array_layers: 1,
},
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::R32Float,
usage: TextureUsages::RENDER_ATTACHMENT
| TextureUsages::TEXTURE_BINDING
| TextureUsages::STORAGE_BINDING,
view_formats: &[wgpu::TextureFormat::R32Float],
});
let texture_view = texture.create_view(&wgpu::wgt::TextureViewDescriptor {
label: Some("prepass depth view"),
..Default::default()
});
(texture, texture_view)
}
fn create_depth_buffer(device: &Device, width: u32, height: u32) -> (Texture, TextureView)
{
let texture = device.create_texture(&wgpu::wgt::TextureDescriptor {
@@ -620,18 +485,8 @@ impl State
self.configure_surface();
self.depth_buffer =
Self::create_depth_buffer(&self.device, new_size.width, new_size.height);
self.prepass_depth_buffer = Self::create_depth_buffer(
&self.device,
new_size.width / self.prepass_downsampling,
new_size.height / self.prepass_downsampling,
);
self.prepass_depth = Self::create_prepass_depth_buffer(
&self.device,
new_size.width / self.prepass_downsampling,
new_size.height / self.prepass_downsampling,
);
self.upsampled_prepass_depth =
Self::create_prepass_depth_buffer(&self.device, new_size.width, new_size.height);
self.target_texture =
Self::create_target_texture(&self.device, new_size.width, new_size.height);
}
fn render(&mut self)
@@ -639,43 +494,6 @@ impl State
self.camera.update();
self.voxel_cache.lock().next_frame();
// Build sorted buffer
let mut chunks = self.chunk_pos_map.iter().collect::<Vec<_>>();
chunks.sort_by_key(|(_, (x, y, z))| {
OrderedFloat(
(Vec3::new(*x as f32, *y as f32, *z as f32) - self.camera.position).length(),
)
});
let instance_buffer = self
.device
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Instance buffer"),
contents: bytemuck::cast_slice(
chunks
.iter()
.map(|(structure_id, (x, y, z))| InstanceAttribute {
x: *x as f32,
y: *y as f32,
z: *z as f32,
id: **structure_id,
})
.collect::<Vec<_>>()
.as_slice(),
),
usage: BufferUsages::COPY_DST | BufferUsages::VERTEX,
});
let prepass_depth_bind_group = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("prepass_depth_bind_group"),
layout: &self.prepass_depth_bind_group_layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(&self.upsampled_prepass_depth.1),
}],
});
// Create texture view.
// NOTE: We must handle Timeout because the surface may be unavailable
// (e.g., when the window is occluded on macOS).
@@ -716,6 +534,15 @@ impl State
..Default::default()
});
let surface_bg = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("surface_bg"),
layout: &self.surface_bg_layout,
entries: &[wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(&self.target_texture.1),
}],
});
// ~~ Ray-marching timestamp query setup ~~
let timestamp_query = self.device.create_query_set(&wgpu::QuerySetDescriptor {
label: Some("timestamp_query_set"),
@@ -730,88 +557,50 @@ impl State
mapped_at_creation: false,
});
// ~~ Prepass upsample bind group ~~
let prepass_bg = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("prepass_bind_group"),
layout: &self.prepass_upsample_bg_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(&self.prepass_depth.1),
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::TextureView(&self.upsampled_prepass_depth.1),
},
],
});
// ~~ Main render pass ~~
let mut encoder = self.device.create_command_encoder(&Default::default());
// ~~ Prepass ~~
{
let mut renderpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: None,
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: &self.prepass_depth.1,
depth_slice: None,
resolve_target: None,
ops: Operations {
load: wgpu::LoadOp::Clear(Color {
r: -1.,
g: 0.,
b: 0.,
a: 0.,
}),
store: wgpu::StoreOp::Store,
},
})],
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
view: &self.prepass_depth_buffer.1,
depth_ops: Some(Operations {
load: wgpu::LoadOp::Clear(1.),
store: wgpu::StoreOp::Discard,
}),
stencil_ops: None,
let mut renderpass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some("compute_render_pass"),
timestamp_writes: Some(wgpu::ComputePassTimestampWrites {
query_set: &timestamp_query,
beginning_of_pass_write_index: Some(0),
end_of_pass_write_index: Some(1),
}),
timestamp_writes: None,
occlusion_query_set: None,
multiview_mask: None,
});
renderpass.set_vertex_buffer(0, instance_buffer.slice(..));
renderpass.set_pipeline(&self.pipeline);
renderpass.set_bind_group(0, Some(&self.voxel_cache.lock().bind_group()), &[]);
renderpass.set_bind_group(1, Some(&surface_bg), &[]);
let imm = [Immediates {
view_proj: self.camera.view_proj(),
view_proj: self.camera.view_proj().inverse(),
cam_pos: self.camera.position,
frame_timestamp: self.voxel_cache.lock().current_timestamp(),
width: self.size.width / self.prepass_downsampling,
downsample_factor: self.prepass_downsampling,
width: self.size.width,
height: self.size.height,
chunk_width: self.terrain_generator.chunk_width as u32,
chunk_height: self.terrain_generator.chunk_height as u32,
chunk_alt: self.terrain_generator.chunk_alt as u32,
}];
renderpass.set_pipeline(&self.prepass_pipeline);
renderpass.set_immediates(0, unsafe { as_raw_bytes(&imm) });
renderpass.draw(0..36, 0..(self.instance_count as u32));
// End the renderpass.
drop(renderpass);
}
// ~~ Upsample pass ~~
{
let mut compute_pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some("upsample_compute_pass"),
timestamp_writes: None,
});
compute_pass.set_bind_group(0, Some(&prepass_bg), &[]);
compute_pass.set_pipeline(&self.upsample_pipeline);
compute_pass.dispatch_workgroups(
(self.size.width / self.prepass_downsampling).div_ceil(8),
(self.size.height / self.prepass_downsampling).div_ceil(8),
renderpass.dispatch_workgroups(
self.size.width.div_ceil(16),
self.size.height.div_ceil(16),
1,
);
drop(renderpass);
encoder.resolve_query_set(&timestamp_query, 0..2, &timestamp_buffer, 0);
}
// ~~ Main render pass ~~
self.target_blitter.copy(
&self.device,
&mut encoder,
&self.target_texture.1,
&texture_view,
);
/*
{
let mut renderpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: None,
@@ -845,18 +634,15 @@ impl State
occlusion_query_set: None,
multiview_mask: None,
});
renderpass.set_vertex_buffer(0, instance_buffer.slice(..));
renderpass.set_pipeline(&self.pipeline);
renderpass.set_vertex_buffer(0, self.instance_buffer.slice(..));
renderpass.set_bind_group(0, Some(&self.voxel_cache.lock().bind_group()), &[]);
renderpass.set_bind_group(1, Some(&prepass_depth_bind_group), &[]);
let imm = [Immediates {
view_proj: self.camera.view_proj(),
cam_pos: self.camera.position,
frame_timestamp: self.voxel_cache.lock().current_timestamp(),
width: self.size.width,
downsample_factor: 1,
}];
renderpass.set_pipeline(&self.pipeline);
renderpass.set_immediates(0, unsafe { as_raw_bytes(&imm) });
renderpass.draw(0..36, 0..(self.instance_count as u32));
@@ -865,14 +651,18 @@ impl State
encoder.resolve_query_set(&timestamp_query, 0..2, &timestamp_buffer, 0);
}
*/
// ~~ EGUI Render pass ~~
{
self.egui_renderer.begin_frame(&self.window);
egui::Window::new("Window ! ").resizable(false).show(
egui::Window::new("Window ! ").resizable(true).show(
self.egui_renderer.context(),
|ui| {
if !self.insertion_debounce
if self
.camera
.pressed_keyset
.contains(&winit::keyboard::KeyCode::KeyF)
{
ui.label(
egui::RichText::new("Cache paused")
@@ -936,11 +726,10 @@ impl State
);
// ~~ Do cache managment
// if !self
// .camera
// .pressed_keyset
// .contains(&winit::keyboard::KeyCode::KeyF)
if self.insertion_debounce
if !self
.camera
.pressed_keyset
.contains(&winit::keyboard::KeyCode::KeyF)
{
self.voxel_cache
.lock()
@@ -968,12 +757,12 @@ impl State
);
// ~~ Do cache managment
// if !self
// .camera
// .pressed_keyset
// .contains(&winit::keyboard::KeyCode::KeyF)
if self.insertion_debounce
if !self
.camera
.pressed_keyset
.contains(&winit::keyboard::KeyCode::KeyF)
{
self.insertion_debounce = true;
let request_count = self
.cache_interface
.total_request_count(&self.device, &self.queue);
@@ -1002,7 +791,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![];
@@ -1061,7 +850,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()
+1 -3
View File
@@ -3,8 +3,6 @@ use std::path::Path;
use glam::Vec3;
use itertools::Itertools;
use rayon::iter::IntoParallelRefMutIterator;
use rayon::iter::ParallelIterator;
use crate::sparse_tree::Color;
use crate::voxel_cache::data::ExplicitNTreeNode;
@@ -312,7 +310,7 @@ where
let heightmap_max = heightmap.iter().copied().reduce(f32::max).unwrap();
heightmap
.par_iter_mut()
.iter_mut()
.filter(|x| **x == -9999.)
.for_each(|x| *x = heightmap_min);
+42 -8
View File
@@ -8,6 +8,7 @@ pub mod request_buffer;
pub mod structure_table;
pub mod usage_buffer;
pub mod producer_interface;
pub mod indirect_buffer;
pub mod data;
@@ -95,7 +96,7 @@ where
// Structure pool
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: ShaderStages::FRAGMENT,
visibility: ShaderStages::COMPUTE,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Storage { read_only: false },
has_dynamic_offset: false,
@@ -106,7 +107,7 @@ where
// Color pool
wgpu::BindGroupLayoutEntry {
binding: 1,
visibility: ShaderStages::FRAGMENT,
visibility: ShaderStages::COMPUTE,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Storage { read_only: false },
has_dynamic_offset: false,
@@ -117,7 +118,7 @@ where
// Location pool
wgpu::BindGroupLayoutEntry {
binding: 2,
visibility: ShaderStages::FRAGMENT,
visibility: ShaderStages::COMPUTE,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Storage { read_only: false },
has_dynamic_offset: false,
@@ -128,7 +129,7 @@ where
// Request buffer
wgpu::BindGroupLayoutEntry {
binding: 3,
visibility: ShaderStages::FRAGMENT,
visibility: ShaderStages::COMPUTE,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Storage { read_only: false },
has_dynamic_offset: false,
@@ -139,7 +140,7 @@ where
// Usage buffer
wgpu::BindGroupLayoutEntry {
binding: 4,
visibility: ShaderStages::FRAGMENT,
visibility: ShaderStages::COMPUTE,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Storage { read_only: false },
has_dynamic_offset: false,
@@ -152,7 +153,7 @@ where
// Structure table pointers
wgpu::BindGroupLayoutEntry {
binding: 5,
visibility: ShaderStages::FRAGMENT,
visibility: ShaderStages::COMPUTE,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Storage { read_only: false },
has_dynamic_offset: false,
@@ -164,7 +165,7 @@ where
// Structure table request buffer
wgpu::BindGroupLayoutEntry {
binding: 6,
visibility: ShaderStages::FRAGMENT,
visibility: ShaderStages::COMPUTE,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Storage { read_only: false },
has_dynamic_offset: false,
@@ -346,6 +347,8 @@ where
format!("
struct StructurePoolElement
{{
occupancy_low: atomic<u32>,
occupancy_high: atomic<u32>,
pointers: array<u32, {children_count}>
}}
@@ -565,15 +568,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 +613,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
+44
View File
@@ -0,0 +1,44 @@
use wgpu::{Buffer, BufferUsages, Device, util::DeviceExt};
pub struct BufferCompactor
{
size: usize,
block_count: usize,
reduced_buffer: Buffer,
sum_buffer: Buffer,
compaction_buffer: Buffer,
}
impl BufferCompactor
{
const THREAD_COUNT: usize = 256;
pub fn new(device: &Device, size: usize) -> Self
{
let block_count = size.div_ceil(size);
let reduced_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("indirect_buffer_reduced"),
contents: bytemuck::cast_slice(vec![0; block_count].as_slice()),
usage: BufferUsages::STORAGE,
});
let sum_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("indirect_buffer_sum"),
contents: bytemuck::cast_slice(vec![0; size].as_slice()),
usage: BufferUsages::STORAGE,
});
let compaction_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("indirect_buffer_compaction"),
contents: bytemuck::cast_slice(vec![0; size].as_slice()),
usage: BufferUsages::STORAGE,
});
BufferCompactor {
size,
block_count,
reduced_buffer,
sum_buffer,
compaction_buffer,
}
}
}
+1 -4
View File
@@ -249,17 +249,14 @@ where
// Check if phase is last
/*
if(phase == sub_phase)
{{
let next_phase_width = phase_total_width * 2;
if(next_phase_width >= element_sort_count && phase_total_width >= element_sort_count)
if(next_phase_width >= (element_sort_count * 2) && phase_total_width >= (element_sort_count * 2))
{{
// This was the final phase, stop
indirect_count = vec3<u32>(0);
}}
}}
*/
if(phase == 0)
{{
+3 -2
View File
@@ -80,7 +80,7 @@ impl StructureTable
&mut self.request_buffer
}
pub fn free_structure(&mut self, structure_id: u32)
pub fn remove_structure(&mut self, structure_id: u32)
{
self.available_slots += 1;
self.allocation_table[structure_id as usize] = false;
@@ -104,7 +104,8 @@ impl StructureTable
.allocation_table
.iter()
.enumerate()
.find(|(_, allocated)| !**allocated)
.filter(|(_, allocated)| !**allocated)
.next()
.unwrap();
self.allocation_table[first_id] = true;
self.available_slots -= 1;
BIN
View File
Binary file not shown.