working z prepass shit
This commit is contained in:
+70
-26
@@ -2,12 +2,12 @@ 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>,
|
||||
@location(6) ndc: vec4<f32>
|
||||
@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>,
|
||||
}
|
||||
|
||||
struct ChunkImmediate
|
||||
@@ -15,6 +15,8 @@ struct ChunkImmediate
|
||||
view_proj: mat4x4<f32>,
|
||||
cam_pos: vec3<f32>,
|
||||
frame_timestamp: u32,
|
||||
width: u32,
|
||||
downsampling_factor: u32,
|
||||
}
|
||||
|
||||
var<immediate> constants: ChunkImmediate;
|
||||
@@ -25,7 +27,7 @@ struct CacheChunkObject
|
||||
transform: mat4x4<f32>,
|
||||
color: vec4<f32>,
|
||||
id: u32,
|
||||
pointer: u32
|
||||
pointer: u32,
|
||||
}
|
||||
|
||||
|
||||
@@ -74,8 +76,7 @@ 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_sampler: sampler;
|
||||
@group(1) @binding(1) var prepass_depth: texture_2d<f32>;
|
||||
@group(1) @binding(0) var prepass_depth: texture_2d<f32>;
|
||||
|
||||
@vertex
|
||||
fn chunk(@builtin(vertex_index) index: u32, @location(0) position: vec3<f32>, @location(1) id: u32) -> VertexOutput
|
||||
@@ -118,13 +119,13 @@ 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;
|
||||
output.world_pos = vertex + position;
|
||||
output.structure_id = id;
|
||||
output.chunk_position = position;
|
||||
output.ndc = output_vertex;
|
||||
|
||||
//let output = vec4<f32>(vertex, 1.0f);
|
||||
return output;
|
||||
@@ -216,9 +217,9 @@ 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. / 1920.;
|
||||
let fovy_deg = 100. / f32(constants.width);
|
||||
let fovy_rad = (fovy_deg * 3.14) / 180.;
|
||||
let cone_factor = tan(fovy_rad / 2.) * 2.;
|
||||
let cone_factor = 1.414 * f32(constants.downsampling_factor) * (tan(fovy_rad / 2.) * 2.); // How many pixels per distance a voxel takes
|
||||
|
||||
let st_pointer = structure_table_pointer[root_id];
|
||||
|
||||
@@ -263,6 +264,7 @@ 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 ++)
|
||||
{
|
||||
@@ -275,9 +277,9 @@ fn new_traverse(ray_dir: vec3<f32>, ray_origin: vec3<f32>, root_id: u32, dist_of
|
||||
var current_node = dfs_stack[current_depth];
|
||||
var pointer = structure_pool[current_node].pointers[child_index];
|
||||
|
||||
let min_child_size = (length(vec3<f32>(voxel) - pos_origin) + dist_offset_voxel) * cone_factor;
|
||||
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))
|
||||
@@ -311,7 +313,8 @@ 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 / 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));
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -363,16 +366,19 @@ struct FragmentOutput {
|
||||
|
||||
struct FragmentPrepassOutput {
|
||||
@location(0) depth_prepass: f32, // Equivalent to gl_FragDepth
|
||||
@builtin(frag_depth) depth: f32, // Equivalent to gl_FragDepth
|
||||
//@builtin(frag_depth) depth: f32, // Equivalent to gl_FragDepth
|
||||
}
|
||||
|
||||
@early_depth_test(less_equal)
|
||||
|
||||
//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 - 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 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.);
|
||||
@@ -380,10 +386,14 @@ fn fragment_prepass(in: VertexOutput) -> FragmentPrepassOutput
|
||||
var frag_out: FragmentPrepassOutput;
|
||||
|
||||
//frag_out.color = result.color;
|
||||
//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;
|
||||
//frag_out.depth = depth;
|
||||
//frag_out.depth_prepass = depth;
|
||||
return frag_out;
|
||||
}
|
||||
|
||||
@@ -391,20 +401,54 @@ fn fragment_prepass(in: VertexOutput) -> FragmentPrepassOutput
|
||||
@fragment
|
||||
fn fragment(in: VertexOutput) -> FragmentOutput
|
||||
{
|
||||
let surface_depth = in.ndc.z;
|
||||
let prepass_depth = textureSample(prepass_depth, prepass_depth_sampler, in.ndc.xy).x;
|
||||
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 surface_depth <= prepass_depth
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user