add early termination
This commit is contained in:
@@ -12,6 +12,10 @@ const MAX_STEPS = 50;
|
||||
const SEB_EPSILON = EPSILON;
|
||||
const SEB_MAX_STEPS = MAX_STEPS;
|
||||
|
||||
const EARLY_TERMINATION = true;
|
||||
const STEP_TERMINATION_THRESHOLD = 5;
|
||||
const SUBGROUP_TERMINATION_THRESHOLD = 3;
|
||||
|
||||
// axis aligned, center at the origin, dimensions "box_size"
|
||||
// https://iquilezles.org/articles/intersectors/
|
||||
fn box_intersection(ro: vec3<f32>, rd: vec3<f32>, box_size: vec3<f32>) -> vec2<f32>
|
||||
@@ -29,10 +33,17 @@ fn box_intersection(ro: vec3<f32>, rd: vec3<f32>, box_size: vec3<f32>) -> vec2<f
|
||||
return vec2(tN, tF);
|
||||
}
|
||||
|
||||
fn molive_trace(metadata: TapeMetadata, spatial_texture: u32, near_plane: f32, far_plane: f32, ray_pos: vec3<f32>, ray_dir: vec3<f32>) -> f32 {
|
||||
fn early_terminate(primary_ray: bool, i: u32) -> bool {
|
||||
return EARLY_TERMINATION && !primary_ray && i > STEP_TERMINATION_THRESHOLD && subgroupAdd(1) < SUBGROUP_TERMINATION_THRESHOLD;
|
||||
}
|
||||
|
||||
fn molive_trace(metadata: TapeMetadata, spatial_texture: u32, near_plane: f32, far_plane: f32, ray_pos: vec3<f32>, ray_dir: vec3<f32>, primary_ray: bool) -> f32 {
|
||||
var total_dist = near_plane;
|
||||
var p = fma(ray_dir, vec3(total_dist), ray_pos);
|
||||
for (var i = 0; i < MAX_STEPS; i++) {
|
||||
if (early_terminate(primary_ray, i)) {
|
||||
return -2.0;
|
||||
}
|
||||
let dist = spatial_scene(spatial_texture, vec4(p, metadata.time));
|
||||
total_dist += dist;
|
||||
p = fma(ray_dir, vec3(total_dist), ray_pos);
|
||||
@@ -46,7 +57,7 @@ fn molive_trace(metadata: TapeMetadata, spatial_texture: u32, near_plane: f32, f
|
||||
}
|
||||
|
||||
// https://github.com/sebbbi/rust_test/blob/e4356e133eeaf248c0d482523258dcad7154646e/shader/main.frag#L65
|
||||
fn sebastian_trace(metadata: TapeMetadata, spatial_texture: u32, near_plane: f32, far_plane: f32, ray_pos: vec3<f32>, ray_dir: vec3<f32>) -> f32 {
|
||||
fn sebastian_trace(metadata: TapeMetadata, spatial_texture: u32, near_plane: f32, far_plane: f32, ray_pos: vec3<f32>, ray_dir: vec3<f32>, primary_ray: bool) -> f32 {
|
||||
// Do the first step without the outside check
|
||||
// Rasterization does not guarantee perfect interpolation
|
||||
let s = spatial_scene(spatial_texture, vec4(ray_pos, metadata.time));
|
||||
@@ -56,6 +67,9 @@ fn sebastian_trace(metadata: TapeMetadata, spatial_texture: u32, near_plane: f32
|
||||
if (s > SEB_EPSILON)
|
||||
{
|
||||
for (var i=0; i<SEB_MAX_STEPS; i++) {
|
||||
if (early_terminate(primary_ray, i)) {
|
||||
return -2.0;
|
||||
}
|
||||
let uvw = fma(ray_dir, vec3(d), ray_pos);
|
||||
if (d > far_plane) {
|
||||
return -1.0;
|
||||
@@ -71,7 +85,7 @@ fn sebastian_trace(metadata: TapeMetadata, spatial_texture: u32, near_plane: f32
|
||||
return d;
|
||||
}
|
||||
|
||||
fn trace_tape(metadata: TapeMetadata, spatial_texture: u32, primitive_id: u32, ray_pos: vec3<f32>, ray_dir: vec3<f32>) -> f32 {
|
||||
fn trace_tape(metadata: TapeMetadata, spatial_texture: u32, primitive_id: u32, ray_pos: vec3<f32>, ray_dir: vec3<f32>, primary_ray: bool) -> f32 {
|
||||
let uinternal_pos = decode_internal_pos(primitive_id, metadata.subdivision_log2);
|
||||
let internal_pos = vec3f(uinternal_pos);
|
||||
|
||||
@@ -93,6 +107,6 @@ fn trace_tape(metadata: TapeMetadata, spatial_texture: u32, primitive_id: u32, r
|
||||
let spatial_ray_pos = spatial_scene_transform(metadata, ray_pos);
|
||||
let spatial_ray_dir = (ray_dir / metadata.dimensions) * SPATIAL_SCENE_SCALE;
|
||||
|
||||
//return molive_trace(metadata, spatial_texture, near_far.x, near_far.y, spatial_ray_pos, spatial_ray_dir);
|
||||
return sebastian_trace(metadata, spatial_texture, near_far.x, near_far.y, spatial_ray_pos, spatial_ray_dir);
|
||||
//return molive_trace(metadata, spatial_texture, near_far.x, near_far.y, spatial_ray_pos, spatial_ray_dir, primary_ray);
|
||||
return sebastian_trace(metadata, spatial_texture, near_far.x, near_far.y, spatial_ray_pos, spatial_ray_dir, primary_ray);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user