Compare commits

5 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
10 changed files with 604 additions and 63 deletions
+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;
}
+2
View File
@@ -0,0 +1,2 @@
module example;
+208 -40
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
@@ -80,7 +86,6 @@ VertexOutput chunk(
struct StructurePointer
{
uint32_t value;
bool subdivided()
{
return (this.value & 0x80000000) != 0;
@@ -91,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;
@@ -129,6 +139,8 @@ struct ByteColor
struct StructurePoolElement
{
uint32_t occupancy_low;
uint32_t occupancy_high;
StructurePointer pointers[64];
}
@@ -168,60 +180,101 @@ 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)
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())
{
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.);
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();
uint32_t child_index = get_children_index(pos, scale_exp) ^ child_mirror;
StructurePointer current_node = structure_pool[current_node_index].pointers[child_index];
child_index = get_children_index(pos, scale_exp);
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() && current_node.pointer_valid())
while(
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;
@@ -235,31 +288,32 @@ 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)
{
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);
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_far : float3 = child_pos + select(ray_direction > 0., float3(child_scale), float3(0.));
let child_pos : float3 = floor_scale(pos, adv_scale_exp);
// 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 << 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);
@@ -268,25 +322,139 @@ 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;
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];
}
return float4(1., 0., 1., 1.);
var hit: HitInformation;
hit.hit = false;
return hit;
}
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.);
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)
BIN
View File
Binary file not shown.
+15
View File
@@ -0,0 +1,15 @@
// Contains useful facilities to render data streamed in from the host
// Can produce a voxel given
// - Its depth
// - Its position within the chunk
// - The chunks position
pub trait ChunkVoxelProducer
{
fn produce_voxel(
&mut self,
depth: usize,
chunk_position: (usize, usize, usize),
voxel_position: (usize, usize, usize),
);
}
+164 -10
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;
@@ -19,8 +20,10 @@ use itertools::Itertools;
use rayon::iter::IndexedParallelIterator;
use rayon::iter::IntoParallelRefIterator;
use rayon::iter::ParallelIterator;
use wgpu::BindGroupLayout;
use wgpu::Buffer;
use wgpu::BufferUsages;
use wgpu::ComputePipeline;
use wgpu::Device;
use wgpu::Extent3d;
use wgpu::Features;
@@ -28,7 +31,9 @@ use wgpu::InstanceDescriptor;
use wgpu::InstanceFlags;
use wgpu::MemoryBudgetThresholds;
use wgpu::Operations;
use wgpu::Origin3d;
use wgpu::RenderPipeline;
use wgpu::ShaderStages;
use wgpu::Texture;
use wgpu::TextureUsages;
use wgpu::TextureView;
@@ -62,6 +67,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;
@@ -81,10 +87,14 @@ struct State
size: winit::dpi::PhysicalSize<u32>,
surface: wgpu::Surface<'static>,
depth_buffer: (wgpu::Texture, wgpu::TextureView),
target_texture: (wgpu::Texture, wgpu::TextureView),
surface_format: wgpu::TextureFormat,
target_blitter: wgpu::util::TextureBlitter,
egui_renderer: EguiRenderer,
pipeline: RenderPipeline,
//pipeline: RenderPipeline,
pipeline: ComputePipeline,
surface_bg_layout: BindGroupLayout,
voxel_cache: Arc<Mutex<VoxelCache<4>>>,
cache_interface: Arc<CacheProducerInterface<4>>,
terrain_generator: Arc<TerrainGenerator<4>>,
@@ -110,6 +120,12 @@ struct Immediates
view_proj: Mat4,
cam_pos: Vec3,
frame_timestamp: u32,
width: u32,
height: u32,
chunk_width: u32,
chunk_height: u32,
chunk_alt: u32,
}
#[derive(Debug, Clone, Copy, Zeroable, Pod)]
@@ -130,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(),
@@ -142,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,
@@ -161,8 +181,8 @@ 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 cache_interface = CacheProducerInterface::new(1024, &device);
let mut voxel_cache = VoxelCache::<4>::new(100_000, device.clone(), queue.clone());
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(
@@ -200,7 +220,7 @@ 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 = unsafe {
@@ -225,15 +245,60 @@ 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 {
label: Some("surface_bg_layout"),
entries: &[wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: ShaderStages::COMPUTE,
ty: wgpu::BindingType::StorageTexture {
access: wgpu::StorageTextureAccess::WriteOnly,
format: wgpu::TextureFormat::Rgba32Float,
view_dimension: wgpu::TextureViewDimension::D2,
},
count: None,
}],
});
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("Voxel pipeline layout"),
bind_group_layouts: &[Some(&voxel_cache.bind_group_layout())],
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(&pipeline_layout),
@@ -270,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(),
}),
@@ -288,6 +353,7 @@ impl State
multiview_mask: None,
cache: None,
});
*/
let state = State {
instance,
@@ -297,8 +363,13 @@ impl State
surface_format,
egui_renderer,
depth_buffer: Self::create_depth_buffer(&device, size.width, size.height),
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,
voxel_cache: Arc::new(Mutex::new(voxel_cache)),
@@ -318,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
@@ -393,6 +485,8 @@ impl State
self.configure_surface();
self.depth_buffer =
Self::create_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)
@@ -440,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"),
@@ -456,6 +559,48 @@ impl State
// ~~ Main render pass ~~
let mut encoder = self.device.create_command_encoder(&Default::default());
{
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),
}),
});
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().inverse(),
cam_pos: self.camera.position,
frame_timestamp: self.voxel_cache.lock().current_timestamp(),
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_immediates(0, unsafe { as_raw_bytes(&imm) });
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);
}
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,
@@ -506,6 +651,7 @@ impl State
encoder.resolve_query_set(&timestamp_query, 0..2, &timestamp_buffer, 0);
}
*/
// ~~ EGUI Render pass ~~
{
@@ -645,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![];
@@ -704,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()
+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)
{{