Skip to content

The native runtime for three.js: three.js, down to the metal. 45× smaller than Electron, on a Sony PS3, and path-traced at 60 fps on ray-tracing GPUs.

Ray tracing off: the live kernel traces the camera ray and no other (LiveCore::set_rays)

Yifeng "Evan" Wangf27b27d22a4c
diff --git a/renderer/shaders/pathtrace.wgsl b/renderer/shaders/pathtrace.wgsl
index ddd8e9f..a9dd880 100644
--- a/renderer/shaders/pathtrace.wgsl
+++ b/renderer/shaders/pathtrace.wgsl
@@ -70,7 +70,7 @@ struct Params {
fog_color: vec4<f32>, // ambient inscattering radiance, max opacity
vol: vec4<f32>, // volumetric scattering albedo (0 = off), HG anisotropy, extinction scale in range, -
vol2: vec4<f32>, // volumetric albedo tint rgb, range (m, 0 = unlimited)
- raster: vec4<f32>, // live primary visibility from the raster pass (1 = on), this frame's sub-pixel jitter x, y, -
+ raster: vec4<f32>, // live primary visibility from the raster pass (1 = on), this frame's sub-pixel jitter x, y; ray tracing off (1)
cache: vec4<f32>, // live radiance cache (1 = on), history cap in samples, history cap in frames, -
motion: vec4<f32>, // live motion vectors: 1 = on, -, -, - (first dynamic triangle: group_first[1].x)
prev_pos: vec4<f32>, // live: the previous frame's camera (emitter reservoir reprojection), as cam_*
@@ -154,6 +154,11 @@ fn inst_attr(r: Instance, a: TriAttr) -> TriAttr {
var<private> rng_state: u32;
// Path vertex index of the current shading point (cheaper procedural detail / fewer candidates past the first hit).
var<private> cur_depth: u32;
+// Live "ray tracing off" (P.raster.w, LiveCore::rays): set once the camera's own ray has found its surface. From then
+// on `trace` reports a miss, so nothing is shadowed, a bounce sees the sky and a mirror reflects the sky: what a
+// rasterizer without shadow maps draws, on any GPU, from the same scene and the same shading. The switch is a uniform:
+// no kernel is rebuilt to flip it.
+var<private> rays_off: bool;
// The path so far is camera ray + mirror-like bounces (puddles, glass): surface detail is still visible.
var<private> mirror_path: bool;
@@ -503,6 +508,7 @@ fn intersect_tri(o: vec3<f32>, d: vec3<f32>, i: u32, tmax: f32) -> vec3<f32> {
// mode: TRACE_CLOSEST, TRACE_SHADOW (any hit among shadow casters), TRACE_ANY (any hit on any surface).
fn trace(o: vec3<f32>, d: vec3<f32>, tmax_in: f32, mode: u32) -> Hit {
var hit = Hit(tmax_in, 0xffffffffu, 0.0, 0.0, NO_INST);
+ if (rays_off) { return hit; }
let inv_d = 1.0 / select(d, vec3<f32>(1e-20), abs(d) < vec3<f32>(1e-20));
var stack: array<u32, 48>;
var sp = 0u;
@@ -2993,6 +2999,8 @@ fn main(@builtin(global_invocation_id) gid: vec3<u32>) {
} else {
hit = trace(o, d, INF, TRACE_CLOSEST);
}
+ // Ray tracing off: the camera ray above was the last one traced for this pixel.
+ if (depth == 0u) { rays_off = LIVE && P.raster.w > 0.5; }
if (has(F_FOG) && P.fog.x > 0.0) {
// Height fog on every segment (attenuation + ambient inscattering); lit volumetric scattering on camera rays.
let t0 = select(0.0, P.fog.w, depth == 0u);
diff --git a/renderer/shaders/trace_hw.wgsl b/renderer/shaders/trace_hw.wgsl
index 7b2fd5b..5f5c9c0 100644
--- a/renderer/shaders/trace_hw.wgsl
+++ b/renderer/shaders/trace_hw.wgsl
@@ -5,6 +5,8 @@
@group(0) @binding(13) var acc: acceleration_structure;
fn trace(o: vec3<f32>, d: vec3<f32>, tmax_in: f32, mode: u32) -> Hit {
+ // Ray tracing off (pathtrace.wgsl `rays_off`): past the camera ray, every ray misses.
+ if (rays_off) { return Hit(tmax_in, 0xffffffffu, 0.0, 0.0, NO_INST); }
let mask = select(0xffu, 1u, mode == TRACE_SHADOW);
if (!has(F_MASKED)) {
let h = trace_opaque(o, d, tmax_in, mask, mode != TRACE_CLOSEST);
diff --git a/renderer/src/gpu.rs b/renderer/src/gpu.rs
index 5e42df1..c0b99ae 100644
--- a/renderer/src/gpu.rs
+++ b/renderer/src/gpu.rs
@@ -85,7 +85,7 @@ pub struct Params {
pub fog_color: [f32; 4], // ambient inscattering radiance rgb, max opacity
pub vol: [f32; 4], // volumetric scattering albedo (0 = off), phase anisotropy, extinction scale in range, -
pub vol2: [f32; 4], // volumetric albedo tint rgb, range (m, 0 = unlimited)
- pub raster: [f32; 4], // live primary visibility from the raster pass (1 = on), sub-pixel jitter x, y, -
+ pub raster: [f32; 4], // live primary visibility from the raster pass (1 = on), sub-pixel jitter x, y; ray tracing off (1)
pub cache: [f32; 4], // live radiance cache (1 = on), history cap in samples, history cap in frames, -
pub motion: [f32; 4], // live motion vectors: 1 = on, -, -, - (first dynamic triangle: group_first[1][0])
pub prev_pos: [f32; 4], // live: the previous frame's camera (emitter reservoir reprojection), as cam_*
diff --git a/renderer/src/live.rs b/renderer/src/live.rs
index e46f518..78a4b2f 100644
--- a/renderer/src/live.rs
+++ b/renderer/src/live.rs
@@ -1138,6 +1138,11 @@ pub struct LiveCore {
pub fallbacks: u32,
/// Path-tracing bounces for real-time frames (the offline preset uses more).
pub bounces: u32,
+ /// Ray tracing (true). Off (`set_rays`), the kernel traces the camera ray of each pixel and no other: no shadows,
+ /// no bounce light, reflections of the sky only; analytic lights and emissive surfaces light unoccluded. The
+ /// fallback for GPUs that trace too slowly (no hardware ray queries), and the "what tracing adds" comparison on
+ /// those that do: the same scene, materials, exposure and post either way.
+ rays: bool,
/// 0 lit, 1 base color, 2 world normal, 3 lit without denoiser, 4 raw sample, 5 history length, 6 diffuse,
/// 7 specular, 8 deterministic direct light (THREERT_VIEW_MODE at start: e.g. bench-temporal per signal).
pub view_mode: u32,
@@ -1601,7 +1606,7 @@ impl LiveCore {
// brings it, `gpu::loop_guard_of`.)
let guard_read = (gpu::loop_guard_of(!g.hwrt).is_some() && targets.kernel.guard.is_some())
.then(|| (g.device.create_buffer(&wgpu::BufferDescriptor { label: Some("loop guard read"), size: 4, usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST, mapped_at_creation: false }), Arc::new(AtomicU32::new(0))));
- let mut core = LiveCore { particle_spawns: Default::default(), grid_sources: Default::default(), g, passes, targets, sc, presets: presets.to_path_buf(), assets: assets.to_path_buf(), w, h, seq: 0, aspect: w as f32 / h as f32, cam, prev_cam: cam, frame: 0, history_len: 0.0, reset: true, cap_next: None, bloom: ([0.0; 6], [0.0; 6]), vignette: 0.0, post: [0.0; 4], blur: (0.0, 0.0), still: 0, inflight: Default::default(), gpu_ms: 0.0, gpu_frame: 0, gpu_trace_ms: 0.0, edit_ms: 0.0, instanced_ms: 0.0, edit_split: EditSplit::default(), rebuild_ms: 0.0, fallback: None, fallbacks: 0, bounces: 4, view_mode: std::env::var("THREERT_VIEW_MODE").ok().and_then(|v| v.parse().ok()).unwrap_or(0), fixed_dt: None, motion_blur_off: false, ghost_fix_off: false, triangles, scene_name, time: 0.0, frame_dt: 1.0 / 60.0, last_frame: Instant::now(), post_key: String::new(), render_scale: 1.0, dynamic_res: false, over_budget: (0, 0), profile, timer, raster, cache, restir, restir_prev: None, kernel_bg, prev_span: None, reposed: false, emitters_moved: false, cold_frames: 0, pose_span: None, pose_reposed: false, inst_moving: vec![], inst_moved: false, prev_isz: (0, 0), lit_changed: 0, spark_lights: false, cpu_skin_ms: 0.0, cpu_upload_ms: 0.0, gpu_blas_ms: 0.0, cpu_wait_ms: 0.0, sky_cover: 0.0, guard_trips: guard_read.as_ref().map(|_| 0), guard_read };
+ let mut core = LiveCore { particle_spawns: Default::default(), grid_sources: Default::default(), g, passes, targets, sc, presets: presets.to_path_buf(), assets: assets.to_path_buf(), w, h, seq: 0, aspect: w as f32 / h as f32, cam, prev_cam: cam, frame: 0, history_len: 0.0, reset: true, cap_next: None, bloom: ([0.0; 6], [0.0; 6]), vignette: 0.0, post: [0.0; 4], blur: (0.0, 0.0), still: 0, inflight: Default::default(), gpu_ms: 0.0, gpu_frame: 0, gpu_trace_ms: 0.0, edit_ms: 0.0, instanced_ms: 0.0, edit_split: EditSplit::default(), rebuild_ms: 0.0, fallback: None, fallbacks: 0, bounces: 4, rays: true, view_mode: std::env::var("THREERT_VIEW_MODE").ok().and_then(|v| v.parse().ok()).unwrap_or(0), fixed_dt: None, motion_blur_off: false, ghost_fix_off: false, triangles, scene_name, time: 0.0, frame_dt: 1.0 / 60.0, last_frame: Instant::now(), post_key: String::new(), render_scale: 1.0, dynamic_res: false, over_budget: (0, 0), profile, timer, raster, cache, restir, restir_prev: None, kernel_bg, prev_span: None, reposed: false, emitters_moved: false, cold_frames: 0, pose_span: None, pose_reposed: false, inst_moving: vec![], inst_moved: false, prev_isz: (0, 0), lit_changed: 0, spark_lights: false, cpu_skin_ms: 0.0, cpu_upload_ms: 0.0, gpu_blas_ms: 0.0, cpu_wait_ms: 0.0, sky_cover: 0.0, guard_trips: guard_read.as_ref().map(|_| 0), guard_read };
core.spark_lights = core.sc.tables.emitter_tris.iter().any(|e| e.nominal);
core.sky_cover = core.sc.tables.cloud_cover;
core.apply_post(&pp);
@@ -3546,6 +3551,18 @@ impl LiveCore {
self.reset = true;
}
+ /// Ray tracing on or off (see `rays`). The history starts over: the two modes light a pixel differently.
+ pub fn set_rays(&mut self, on: bool) {
+ if self.rays != on {
+ self.rays = on;
+ self.reset = true;
+ }
+ }
+
+ pub fn rays(&self) -> bool {
+ self.rays
+ }
+
pub fn set_camera(&mut self, mut c: Camera) {
if c.tan_half_fov <= 0.0 {
c.tan_half_fov = self.sc.tan_half_fov;
@@ -3744,6 +3761,7 @@ impl LiveCore {
if self.raster.is_some() && !self.sc.instanced {
p.raster = [1.0, jitter.0, jitter.1, 0.0];
}
+ p.raster[3] = if self.rays { 0.0 } else { 1.0 };
if self.restir {
let pc = self.prev_cam;
p.prev_pos = [pc.pos.x, pc.pos.y, pc.pos.z, pc.tan_half_fov];