add early termination
This commit is contained in:
@@ -39,7 +39,7 @@ fn pathtrace(@builtin(global_invocation_id) global_id: vec3<u32>) {
|
|||||||
var throughput = vec3(1.0);
|
var throughput = vec3(1.0);
|
||||||
var p_bounce = 0.0;
|
var p_bounce = 0.0;
|
||||||
loop {
|
loop {
|
||||||
let ray = trace_ray(ray_origin, ray_direction, ray_t_min, RAY_T_MAX, RAY_FLAG_NONE);
|
let ray = trace_ray(ray_origin, ray_direction, ray_t_min, RAY_T_MAX, RAY_FLAG_NONE, p_bounce == 0.0);
|
||||||
if ray.kind != RAY_QUERY_INTERSECTION_NONE {
|
if ray.kind != RAY_QUERY_INTERSECTION_NONE {
|
||||||
let ray_hit = resolve_ray_hit_full(ray, ray_origin, ray_direction);
|
let ray_hit = resolve_ray_hit_full(ray, ray_origin, ray_direction);
|
||||||
//if ray.kind == RAY_QUERY_INTERSECTION_GENERATED {
|
//if ray.kind == RAY_QUERY_INTERSECTION_GENERATED {
|
||||||
|
|||||||
@@ -96,7 +96,7 @@ fn generate_initial_path(world_position: vec3<f32>, world_normal: vec3<f32>, mat
|
|||||||
// Sample the BRDF and trace the next ray
|
// Sample the BRDF and trace the next ray
|
||||||
let next_bounce = evaluate_and_sample_brdf(path.wo, path.normal, path.material, F_ab, rng);
|
let next_bounce = evaluate_and_sample_brdf(path.wo, path.normal, path.material, F_ab, rng);
|
||||||
if next_bounce.pdf == 0.0 { break; }
|
if next_bounce.pdf == 0.0 { break; }
|
||||||
let ray = trace_ray(path.ray_origin, next_bounce.wi, RAY_T_MIN, RAY_T_MAX, RAY_FLAG_NONE);
|
let ray = trace_ray(path.ray_origin, next_bounce.wi, RAY_T_MIN, RAY_T_MAX, RAY_FLAG_NONE, false);
|
||||||
|
|
||||||
if ray.kind == RAY_QUERY_INTERSECTION_NONE {
|
if ray.kind == RAY_QUERY_INTERSECTION_NONE {
|
||||||
@if(DLSS_RR_GUIDE_BUFFERS)
|
@if(DLSS_RR_GUIDE_BUFFERS)
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ fn sample_gi(@builtin(workgroup_id) workgroup_id: vec3<u32>, @builtin(global_inv
|
|||||||
if rand_f(&rng) >= f32(constants.world_cache_cell_updates_soft_target) / f32(world_cache.active_cells_count) { return; }
|
if rand_f(&rng) >= f32(constants.world_cache_cell_updates_soft_target) / f32(world_cache.active_cells_count) { return; }
|
||||||
|
|
||||||
let ray_direction = sample_cosine_hemisphere(geometry_data.world_normal, &rng);
|
let ray_direction = sample_cosine_hemisphere(geometry_data.world_normal, &rng);
|
||||||
let ray = trace_ray(geometry_data.world_position + (geometry_data.world_normal * RAY_T_MIN), ray_direction, RAY_T_MIN, constants.world_cache_max_gi_ray_distance, RAY_FLAG_NONE);
|
let ray = trace_ray(geometry_data.world_position + (geometry_data.world_normal * RAY_T_MIN), ray_direction, RAY_T_MIN, constants.world_cache_max_gi_ray_distance, RAY_FLAG_NONE, false);
|
||||||
if ray.kind != RAY_QUERY_INTERSECTION_NONE {
|
if ray.kind != RAY_QUERY_INTERSECTION_NONE {
|
||||||
let ray_hit = resolve_ray_hit_full(ray, geometry_data.world_position + (geometry_data.world_normal * RAY_T_MIN), ray_direction);
|
let ray_hit = resolve_ray_hit_full(ray, geometry_data.world_position + (geometry_data.world_normal * RAY_T_MIN), ray_direction);
|
||||||
let cell_life = atomicLoad(&world_cache.life[cell_index]);
|
let cell_life = atomicLoad(&world_cache.life[cell_index]);
|
||||||
|
|||||||
@@ -175,7 +175,7 @@ const RAY_T_MAX = 100000.0f;
|
|||||||
|
|
||||||
const RAY_NO_CULL = 0xFFu;
|
const RAY_NO_CULL = 0xFFu;
|
||||||
|
|
||||||
fn trace_ray(ray_origin: vec3<f32>, ray_direction: vec3<f32>, ray_t_min: f32, ray_t_max: f32, ray_flag: u32) -> RayIntersection {
|
fn trace_ray(ray_origin: vec3<f32>, ray_direction: vec3<f32>, ray_t_min: f32, ray_t_max: f32, ray_flag: u32, primary_ray: bool) -> RayIntersection {
|
||||||
let ray = RayDesc(ray_flag, RAY_NO_CULL, ray_t_min, ray_t_max, ray_origin, ray_direction);
|
let ray = RayDesc(ray_flag, RAY_NO_CULL, ray_t_min, ray_t_max, ray_origin, ray_direction);
|
||||||
var rq: ray_query;
|
var rq: ray_query;
|
||||||
rayQueryInitialize(&rq, tlas, ray);
|
rayQueryInitialize(&rq, tlas, ray);
|
||||||
@@ -187,16 +187,18 @@ fn trace_ray(ray_origin: vec3<f32>, ray_direction: vec3<f32>, ray_t_min: f32, ra
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if candidate.kind == RAY_QUERY_INTERSECTION_AABB {
|
if candidate.kind == RAY_QUERY_INTERSECTION_AABB {
|
||||||
let t = tape_confirm_intersection(candidate.instance_custom_data, candidate.primitive_index, ray_origin, ray_direction);
|
let t = tape_confirm_intersection(candidate.instance_custom_data, candidate.primitive_index, ray_origin, ray_direction, primary_ray);
|
||||||
if t > 0.0 {
|
if t > 0.0 {
|
||||||
rayQueryGenerateIntersection(&rq, t);
|
rayQueryGenerateIntersection(&rq, t);
|
||||||
|
} else if t < -1.0 {
|
||||||
|
rayQueryTerminate(&rq);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return rayQueryGetCommittedIntersection(&rq);
|
return rayQueryGetCommittedIntersection(&rq);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn trace_ray_previous_frame(ray_origin: vec3<f32>, ray_direction: vec3<f32>, ray_t_min: f32, ray_t_max: f32, ray_flag: u32) -> RayIntersection {
|
fn trace_ray_previous_frame(ray_origin: vec3<f32>, ray_direction: vec3<f32>, ray_t_min: f32, ray_t_max: f32, ray_flag: u32, primary_ray: bool) -> RayIntersection {
|
||||||
let ray = RayDesc(ray_flag, RAY_NO_CULL, ray_t_min, ray_t_max, ray_origin, ray_direction);
|
let ray = RayDesc(ray_flag, RAY_NO_CULL, ray_t_min, ray_t_max, ray_origin, ray_direction);
|
||||||
var rq: ray_query;
|
var rq: ray_query;
|
||||||
rayQueryInitialize(&rq, previous_frame_tlas, ray);
|
rayQueryInitialize(&rq, previous_frame_tlas, ray);
|
||||||
@@ -208,9 +210,11 @@ fn trace_ray_previous_frame(ray_origin: vec3<f32>, ray_direction: vec3<f32>, ray
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if candidate.kind == RAY_QUERY_INTERSECTION_AABB {
|
if candidate.kind == RAY_QUERY_INTERSECTION_AABB {
|
||||||
let t = tape_confirm_intersection(candidate.instance_custom_data, candidate.primitive_index, ray_origin, ray_direction);
|
let t = tape_confirm_intersection(candidate.instance_custom_data, candidate.primitive_index, ray_origin, ray_direction, primary_ray);
|
||||||
if t > 0.0 {
|
if t > 0.0 {
|
||||||
rayQueryGenerateIntersection(&rq, t);
|
rayQueryGenerateIntersection(&rq, t);
|
||||||
|
} else if t < -1.0 {
|
||||||
|
rayQueryTerminate(&rq);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -392,11 +396,11 @@ fn resolve_triangle_data_full(instance_id: u32, triangle_id: u32, barycentrics:
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn tape_confirm_intersection(instance_id: u32, primitive_index: u32, ray_position: vec3<f32>, ray_direction: vec3<f32>) -> f32 {
|
fn tape_confirm_intersection(instance_id: u32, primitive_index: u32, ray_position: vec3<f32>, ray_direction: vec3<f32>, primary_ray: bool) -> f32 {
|
||||||
let instance_tape_ids = tape_ids[instance_id];
|
let instance_tape_ids = tape_ids[instance_id];
|
||||||
let metadata = tape_metadata_buffers[instance_tape_ids.metadata_buffer_id].metadata[instance_tape_ids.metadata_buffer_offset];
|
let metadata = tape_metadata_buffers[instance_tape_ids.metadata_buffer_id].metadata[instance_tape_ids.metadata_buffer_offset];
|
||||||
|
|
||||||
return trace_tape(metadata, instance_tape_ids.spatial_texture_id, primitive_index, ray_position, ray_direction);
|
return trace_tape(metadata, instance_tape_ids.spatial_texture_id, primitive_index, ray_position, ray_direction, primary_ray);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn resolve_tape_data_full(instance_id: u32, position: vec3<f32>) -> ResolvedRayHitFull {
|
fn resolve_tape_data_full(instance_id: u32, position: vec3<f32>) -> ResolvedRayHitFull {
|
||||||
|
|||||||
@@ -329,7 +329,7 @@ fn trace_visibility(ray_origin: vec3<f32>, point: vec4<f32>) -> f32 {
|
|||||||
|
|
||||||
if ray_t_max < RAY_T_MIN { return 0.0; }
|
if ray_t_max < RAY_T_MIN { return 0.0; }
|
||||||
|
|
||||||
let ray_hit = trace_ray(ray_origin, ray_direction, RAY_T_MIN, ray_t_max, RAY_FLAG_TERMINATE_ON_FIRST_HIT);
|
let ray_hit = trace_ray(ray_origin, ray_direction, RAY_T_MIN, ray_t_max, RAY_FLAG_TERMINATE_ON_FIRST_HIT, true);
|
||||||
return f32(ray_hit.kind == RAY_QUERY_INTERSECTION_NONE);
|
return f32(ray_hit.kind == RAY_QUERY_INTERSECTION_NONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -346,7 +346,7 @@ fn trace_visibility_previous_frame(ray_origin: vec3<f32>, point: vec4<f32>) -> f
|
|||||||
|
|
||||||
if ray_t_max < RAY_T_MIN { return 0.0; }
|
if ray_t_max < RAY_T_MIN { return 0.0; }
|
||||||
|
|
||||||
let ray_hit = trace_ray_previous_frame(ray_origin, ray_direction, RAY_T_MIN, ray_t_max, RAY_FLAG_TERMINATE_ON_FIRST_HIT);
|
let ray_hit = trace_ray_previous_frame(ray_origin, ray_direction, RAY_T_MIN, ray_t_max, RAY_FLAG_TERMINATE_ON_FIRST_HIT, true);
|
||||||
return f32(ray_hit.kind == RAY_QUERY_INTERSECTION_NONE);
|
return f32(ray_hit.kind == RAY_QUERY_INTERSECTION_NONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,10 @@ const MAX_STEPS = 50;
|
|||||||
const SEB_EPSILON = EPSILON;
|
const SEB_EPSILON = EPSILON;
|
||||||
const SEB_MAX_STEPS = MAX_STEPS;
|
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"
|
// axis aligned, center at the origin, dimensions "box_size"
|
||||||
// https://iquilezles.org/articles/intersectors/
|
// https://iquilezles.org/articles/intersectors/
|
||||||
fn box_intersection(ro: vec3<f32>, rd: vec3<f32>, box_size: vec3<f32>) -> vec2<f32>
|
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);
|
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 total_dist = near_plane;
|
||||||
var p = fma(ray_dir, vec3(total_dist), ray_pos);
|
var p = fma(ray_dir, vec3(total_dist), ray_pos);
|
||||||
for (var i = 0; i < MAX_STEPS; i++) {
|
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));
|
let dist = spatial_scene(spatial_texture, vec4(p, metadata.time));
|
||||||
total_dist += dist;
|
total_dist += dist;
|
||||||
p = fma(ray_dir, vec3(total_dist), ray_pos);
|
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
|
// 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
|
// Do the first step without the outside check
|
||||||
// Rasterization does not guarantee perfect interpolation
|
// Rasterization does not guarantee perfect interpolation
|
||||||
let s = spatial_scene(spatial_texture, vec4(ray_pos, metadata.time));
|
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)
|
if (s > SEB_EPSILON)
|
||||||
{
|
{
|
||||||
for (var i=0; i<SEB_MAX_STEPS; i++) {
|
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);
|
let uvw = fma(ray_dir, vec3(d), ray_pos);
|
||||||
if (d > far_plane) {
|
if (d > far_plane) {
|
||||||
return -1.0;
|
return -1.0;
|
||||||
@@ -71,7 +85,7 @@ fn sebastian_trace(metadata: TapeMetadata, spatial_texture: u32, near_plane: f32
|
|||||||
return d;
|
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 uinternal_pos = decode_internal_pos(primitive_id, metadata.subdivision_log2);
|
||||||
let internal_pos = vec3f(uinternal_pos);
|
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_pos = spatial_scene_transform(metadata, ray_pos);
|
||||||
let spatial_ray_dir = (ray_dir / metadata.dimensions) * SPATIAL_SCENE_SCALE;
|
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 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);
|
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