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.
Rust electron-alternativegame-engineglobal-illuminationgraphicsjavascript-runtimemetalpath-tracingray-tracingreal-time-renderingrenderingrustthreejstypescriptvoxelvulkanwebgpuwgpu
//! Real-time renderer (LiveCore), modelled on UE5's real-time path with hardware RT: every frame traces 1+ spp
//! (primary visibility, GI, shadows, reflections), reprojects and accumulates history (converging to the offline
//! reference while the camera holds still), denoises and applies the UE post chain. Drawn by the native window viewer
//! (`view`) and the native clients; `live` runs it headless at a fixed frame rate for benchmarks:
//!
//! threert-render live <game bundle.js> [--assets DIR] [--room id] [--width 1280 --height 720 --fps 60 --bounces 4
//! --scale 1|0.6|auto --display-scale 1|2|3] [--frame-log frames.jsonl]
//!
//! `--scale` is the render scale (UE screen percentage): the frame is traced, denoised and composed at that fraction
//! of the output resolution and the temporal upscaler takes it to the output; `auto` = dynamic resolution.
//! `--display-scale`: the output is a display's physical pixels, that many a point (a Retina window: 2), as the
//! window host's is; dynamic resolution then starts from the points' resolution and goes down to half of it.
//!
//! stdin (JSON lines): {"cmd":"camera","pos":[x,y,z],"target":[x,y,z]} | {"cmd":"event","type":t,"payload":{..}}
//! | {"cmd":"click","u":u,"v":v} | {"cmd":"reset"} | {"cmd":"time","t":s} (set the scene time)
//! | {"cmd":"snapshot","path":p,"after":k (history length ≥ k),"frames":n (the n-th frame from now, default 1)}
//! (a path ending in .raw: the linear radiance, RGB f32 at output size; ending in .signals:
//! its diffuse / specular / emission apart, <stem>-{diffuse,specular,emission}.raw, as
//! `render --signals` writes them)
//! | {"cmd":"sync","id":s,"frames":n} (stderr "live: sync s" after the n-th frame from now)
//! | {"cmd":"lockstep","dt":s | "on":false} (render only the frames steps ask for, at frame time s)
//! | {"cmd":"camera",...,"frames":n,"sync":id | "snapshot":p} | {"cmd":"frames","n":n,"sync":id | "snapshot":p}
//! (a step: camera, frames, and the sync answer / snapshot at its last frame, in one command)
//! | {"cmd":"scale","scale":s (0 = auto)} | {"cmd":"quit"}
//! | {"cmd":"resize","width":w,"height":h} (the view at another size; stderr "live: resized to wxh in t ms")
//!
//! The files: this one holds `LiveCore` itself (its settings, camera, resolution governor and readbacks) and what the
//! others share; `targets` the live passes' pipelines, screen-size buffers and bind group; `load` a scene loaded and
//! the workers of its sky and emitters; `edits` the world's edits applied in place; `frame` one frame (the trace, the
//! live passes, the caches' training, the GPU timing); `session` the headless runner (native only).
use crate::gpu::Buf;
use crate::heap::{Heap, Section, Stream, Table};
use crate::world::World;
use crate::{gpu, post, scene, tables};
// (The headless runner, `session`, native only, is what uses all of it.)
#[cfg_attr(target_arch = "wasm32", allow(unused_imports))]
use anyhow::{anyhow, Result};
use glam::Vec3;
#[cfg_attr(target_arch = "wasm32", allow(unused_imports))]
use std::io::BufRead;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::mpsc;
use std::sync::Arc;
#[cfg_attr(target_arch = "wasm32", allow(unused_imports))]
use std::time::Duration;
use web_time::Instant;
mod edits;
mod frame;
#[cfg(dlss)]
mod dlss;
mod fsr;
mod layout;
#[cfg(metalfx)]
mod metalfx;
mod load;
mod reconstruction;
mod rendering;
#[cfg(not(target_arch = "wasm32"))]
mod session;
mod state;
mod targets;
#[cfg(test)]
pub(crate) use targets::live_source;
pub(crate) use targets::Targets;
#[cfg(not(target_arch = "wasm32"))]
pub use session::{parse_cmd, parse_launch, parse_opts, run, save_bgra};
pub use reconstruction::{JointGuides, JointProvider, Method, Prepared, Reconstruction, ReconstructionFrame, ReconstructionSize, ReconstructionStatus, Skip, UpscaleProvider};
pub use rendering::{next_method, FrameGeneration, Latency, Quality, RenderingControl, RenderingRequest, RenderingStatus};
use reconstruction::Reconstructor;
use edits::*;
use frame::*;
use load::*;
use state::*;
use targets::*;
pub struct LiveOpts {
/// `--quality`, `--reconstruction`, `--frame-generation`, `--latency` (live/rendering.rs); `--quality` over `scale`.
pub rendering: RenderingRequest,
pub width: u32,
pub height: u32,
pub fps: u32,
pub bounces: u32,
/// Render scale (traced / output resolution); None = dynamic resolution.
pub scale: Option<f32>,
/// Game bundle (game code + runtime) that builds and drives the scene in-process.
pub bundle: PathBuf,
/// Asset root its model / texture paths resolve against.
pub assets: PathBuf,
pub room: String,
/// JSON line per frame (wall interval, CPU work, GPU time, scale, history): hitch statistics (tools/bench-hitches.ts).
/// `interval` runs from the end of one frame's work to the next's, so it carries the variation of the work itself;
/// `start` runs from one frame's start to the next's, which the pacer sets: the frame rate a player sees.
pub frame_log: Option<PathBuf>,
/// The output's pixels a point of its display (1: the output is in points).
pub display_scale: f32,
/// `--launch`: the `params` of the host channel's `launch` message (docs/RUNTIME.md "The host channel").
pub launch: Option<serde_json::Value>,
/// `--caps files,store`: the families of requests the driver answers, listed in the `launch` message.
pub caps: Vec<String>,
/// `--store <dir>`: the game's store, where `user:` assets resolve (docs/RUNTIME.md "Files"). The driver answers
/// `store.*` itself; this is only where the engine reads what it wrote.
pub store: Option<PathBuf>,
}
/// Lowest dynamic-resolution scale (UE r.DynamicRes.MinScreenPercentage default) of a view whose pixels are the
/// display's points; a host whose view is in the display's physical pixels divides it by the display's scale factor
/// (`LiveCore::min_scale`), so the fewest rays a window traces do not depend on the display's density.
pub const MIN_SCALE: f32 = 0.5;
/// The lowest render scale any view takes (a view of 3 pixels a point at `MIN_SCALE`, rounded down).
pub const SCALE_FLOOR: f32 = 0.125;
#[repr(C)]
#[derive(Clone, Copy, Default, bytemuck::Pod, bytemuck::Zeroable)]
struct CamU {
pos: [f32; 4],
fwd: [f32; 4],
right: [f32; 4],
up: [f32; 4],
}
#[repr(C)]
#[derive(Clone, Copy, Default, bytemuck::Pod, bytemuck::Zeroable)]
struct LiveParams {
cur: CamU,
prev: CamU,
size: [u32; 4],
misc: [f32; 4],
misc2: [f32; 4],
mode: [u32; 4],
post: [f32; 4],
/// Auto exposure's metering: low / high fraction of the histogram, its log2 luminance min / max.
ae: [f32; 4],
/// Compensation (2^bias), min / max average luminance, mode (0 manual, 1 auto).
ae2: [f32; 4],
/// Speed toward brighter / darker scenes (EV/s), frame time (s), snap to target (1).
ae3: [f32; 4],
/// Local exposure: highlight contrast, shadow contrast, blurred-luminance blend, detail strength.
lx: [f32; 4],
/// Traced (internal) width, height.
isz: [u32; 4],
/// Bloom (`post::BloomPlan`): the weight of pyramid level 0..3; of 4, 5, the wide pass's reach (texels of the
/// coarsest level; 0 = nothing to sum), the weight of the five-pixel kernel of the output's own pixels.
bl: [f32; 4],
bl2: [f32; 4],
/// The wide Gaussians 0..5, a (weight, 1 / (2 sigma^2)) pair each.
bw: [[f32; 4]; 3],
/// Local exposure's wide blur of log luminance: the weight of level 0..3; of 4, 5, its wide Gaussian's pair.
lb: [f32; 4],
lb2: [f32; 4],
/// Depth of field: CoC radius of infinity (output px; 0 = off), focus distance (m), max radius (px), -.
dof: [f32; 4],
/// This frame's sub-pixel jitter (every traced sample's: x, y in 0 .. 1 of a traced pixel); the output grid's
/// guides' (z, w in 0 .. 1 of an output pixel: frame.rs `decoupled`).
jit: [f32; 4],
/// Which half of the lasting buffers is this frame's (live.wgsl `this_half`; the other the previous frame's); the
/// output is composed from guides of its own (1); the scene has media the camera's rays are marched through
/// (1); the previous frame's output was composed so too (1: its thin things' records are there to carry).
state: [u32; 4],
/// The light tiles (live.wgsl `light_tiles`): this frame's grid offset x, y, -, tiles a row.
lt: [u32; 4],
}
/// Resampling candidates a vertex the camera sees takes from its pixel's light tile, beside the kernel's RIS_M
/// uniform ones (`Params::ltile.w`; half past the camera hit). 2 read as 4, 8 and 16 on the lamp-rooms shots.
pub(super) const LIST_CANDIDATES: u32 = 2;
/// The kernel's group-1 bind groups over a view's targets: the trace's, and the translucency pass's where the device
/// has one (`gpu::Gpu::tl_live_layout`: without `finish`'s records).
fn kernel_bind_groups(g: &gpu::Gpu, targets: &Targets) -> (wgpu::BindGroup, Option<wgpu::BindGroup>) {
(gpu::live_bind_group(&g.device, &g.live_layout, &targets.kernel, false), g.tl_live_layout.as_ref().map(|l| gpu::live_bind_group(&g.device, l, &targets.kernel, true)))
}
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct Camera {
pub pos: Vec3,
pub fwd: Vec3,
pub up: Vec3,
pub right: Vec3,
pub tan_half_fov: f32,
}
impl Camera {
pub fn look_at(pos: Vec3, target: Vec3, tan_half_fov: f32) -> Camera {
let fwd = (target - pos).normalize();
let right = fwd.cross(Vec3::Y).normalize();
Camera { pos, fwd, up: right.cross(fwd), right, tan_half_fov }
}
fn uniform(&self, aspect: f32) -> CamU {
CamU {
pos: [self.pos.x, self.pos.y, self.pos.z, self.tan_half_fov],
fwd: [self.fwd.x, self.fwd.y, self.fwd.z, aspect],
right: [self.right.x, self.right.y, self.right.z, 0.0],
up: [self.up.x, self.up.y, self.up.z, 0.0],
}
}
}
pub enum Cmd {
Camera(Camera),
/// An event for the in-process game (type, payload).
Event(String, serde_json::Value),
/// A click at (u, v) for the in-process game (picks the node under it).
Click(f32, f32),
/// Messages of the host channel for the in-process game (`{"cmd":"host","messages":[{"t":...}, ...]}`: what an
/// app would send, its input among them; docs/RUNTIME.md "The host channel").
Host(Vec<serde_json::Value>),
/// Snapshot (path) of the first frame whose history length reaches `after` (None = any) and that is at least the
/// `frames`-th frame from now (None = the next).
Snapshot(PathBuf, Option<u32>, Option<u32>),
/// Answer `live: sync <id>` on stderr once the n-th frame from now is rendered (benchmarks step the camera frame by
/// frame, so what they measure does not depend on how many frames a busy machine renders per second).
Sync(String, u32),
/// Lockstep (benchmarks): with Some(frame time, s) the runner renders only the frames `Step`s ask for, at that frame
/// time (motion blur, exposure, frame script); None = real time again.
Lockstep(Option<f32>),
/// A step: an optional camera, `frames` frames to render (lockstep), and a sync answer / snapshot at the last of them,
/// registered with the camera in one command so neither can land a frame late.
Step { cam: Option<Camera>, frames: u32, sync: Option<String>, snapshot: Option<PathBuf> },
/// Drop all accumulated history (benchmarks measure convergence from here).
Reset,
/// Set the scene time, s (benchmarks: with lockstep, frame k from here renders at t + k x its frame time; frame
/// time 0 holds it there).
Time(f32),
/// View mode (see LiveCore::view_mode).
Mode(u32),
Bounces(u32),
/// Max samples per frame (adaptive below it).
SppCap(u32),
/// Viewer status text (e.g. the game score), shown in the window title.
Status(String),
/// Render scale (0 = dynamic resolution).
Scale(f32),
/// Rendering settings (live/rendering.rs): the fields given change.
Rendering(RenderingRequest),
/// The view at another size, pixels (`LiveCore::resize`; the window host: its window's).
Resize(u32, u32),
Quit,
}
/// The real-time renderer: scene + screen targets + camera/history state. Records one frame at a time; the output
/// (sRGB BGRA8 per pixel) lands in `targets.out`. Used by the headless runner, the native window viewer and the native clients.
pub struct LiveCore {
reconstructor: Reconstructor,
/// The requested render resolution (`render_scale` and `dynamic_res` are what it resolves to).
quality: Quality,
frame_generation: FrameGeneration,
latency: Latency,
rendering_control: Option<RenderingControl>,
/// The game's particle spawns since the last frame (`World::particle_spawns`), for the GPU particle pass.
particle_spawns: std::collections::BTreeMap<u32, Vec<f32>>,
/// The sources the game gave its density grids since the last frame (`World::grid_sources`), for the grid pass.
grid_sources: std::collections::BTreeMap<u32, Vec<f32>>,
pub g: gpu::Gpu,
passes: Passes,
pub targets: Targets,
sc: LoadedScene,
/// How a frame is taken and shown, and the environment: beside the scene's data (state.rs).
view: ViewState,
env: Environment,
presets: PathBuf,
/// Asset root (models, file textures).
assets: PathBuf,
pub w: u32,
pub h: u32,
/// Committed state sequence number the image shows (set by the game session).
pub seq: i64,
aspect: f32,
pub cam: Camera,
prev_cam: Camera,
pub frame: u32,
pub history_len: f32,
reset: bool,
/// What the bloom passes do for the preset at the view's size.
bloom: post::BloomPlan,
vignette: f32,
/// Chromatic aberration, film grain, bloom threshold, longest motion blur (output pixels).
post: [f32; 4],
/// Motion blur: the shutter as a fraction of the frame time (0 = off), the target frame rate (0 = the real one).
blur: (f32, f32),
/// Last submission of each frame still on the GPU (at most two frames in flight).
inflight: std::collections::VecDeque<wgpu::SubmissionIndex>,
pub gpu_ms: f32,
/// The frame `gpu_ms` timed (it lags by the frames in flight).
gpu_frame: u32,
/// That frame's trace pass alone (the path-tracing kernel: intersection and shading), ms; 0 without timestamps.
/// Frame log `trace`: the per-frame cost a geometry representation changes, without THREERT_PROFILE's stalls.
pub gpu_trace_ms: f32,
/// This frame's geometry edits applied in place (`edit_geometry`: CPU ms), and scene rebuilds (ms) and, when
/// edits fell back to one, why: summed over the frame's commits (a game event's and the frame script's), reset
/// when the frame is logged. Fallbacks since the viewport opened.
/// Frame log `edit`, `rebuild`, `fallback`, `fallbacks`.
pub edit_ms: f32,
/// CPU time of this frame's in-place instance updates (`update_instanced`), ms.
pub instanced_ms: f32,
/// CPU time of this frame's builds above the triangles (`build_top`: one a commit, for whatever its edits
/// changed; none of their own times holds it), ms: summed and reset as `edit_ms`. Frame log `build`.
pub build_ms: f32,
pub edit_split: EditSplit,
pub rebuild_ms: f32,
pub fallback: Option<String>,
pub fallbacks: u32,
/// The commits that built above the triangles (`build_top`): one build each, whatever moved.
pub(super) top_builds: 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,
/// The lights a camera hit resamples (emitters, the local lights past those summed exactly) in a pass of their
/// own, after the paths (true; pathtrace.wgsl `direct`). THREERT_DIRECT_PASS=0, for A/B: the paths light the
/// hit themselves, as they do in a scene without such lights.
direct_pass: 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,
/// Lockstep frame time (s; benchmarks): motion blur and exposure adaptation use it instead of the wall clock.
pub fixed_dt: Option<f32>,
/// The kernel the frames run (`choose_kernel`): the scene's (`up.kernel`), or the one before it while the scene's
/// is being made on another thread. None: none chosen yet (a view's first frame, the first after a rebuild).
pub(super) kernel: Option<gpu::Kernel>,
/// Kernel features an edit of this commit asks for that the scene's structure had not (`F_MASKED`: its first
/// cutout on an instance; `F_IGLASS`: its first shadow caster with glass among its sections; `F_IFUNC`: its
/// first material function on an instance). They switch code on, over data every scene has: the commit adds
/// them to the scene's (`instance_features`).
pub(super) more_features: std::cell::Cell<u32>,
pub triangles: usize,
pub scene_name: String,
/// Scene time, s: what `advance_time` added up, the dt each frame script gets, so it is the game's clock (`f.time`)
/// and a burst fired at a game time starts then; lockstep runs repeat exactly. Animated shading, the particle pass
/// and power edits' time stamps use it (`shader_time`).
time: f32,
/// Time between rendered frames (auto exposure adaptation), s.
frame_dt: f32,
last_frame: Instant,
/// The post preset the LUT / bloom / grain settings were built from (scenes may override it).
post_key: String,
/// Traced / output resolution (UE screen percentage); the temporal pass upsamples below 1.
pub render_scale: f32,
/// Adjust `render_scale` to the frame budget (`govern_resolution`).
pub dynamic_res: bool,
/// Dynamic resolution's lowest scale: `MIN_SCALE` over the display's scale factor.
pub min_scale: f32,
/// The view's pixels a point of its display (`set_display_scale`): dynamic resolution's steps are a point's.
display_scale: f32,
/// Dynamic resolution: consecutive measured frames well over the frame time, and the first frame at the current
/// scale.
over_budget: (u32, u32),
/// When this view was made, and when its render scale was last checked (`govern_resolution` by the clock).
born: Instant,
governed: Instant,
/// THREERT_PROFILE=1: GPU time per pass (timestamp queries), printed every 2 s.
profile: Option<Profile>,
/// GPU time per frame without stalls (timestamp queries; None: measured by draining the queue twice a second).
timer: Option<FrameTimer>,
/// World-space radiance cache (pathtrace.wgsl; None: THREERT_CACHE=0, paths trace every bounce).
cache: Option<RadianceCache>,
/// Kernel group 1: primary visibility + radiance cache + what the live passes read of a trace.
kernel_bg: wgpu::BindGroup,
/// The same for the translucency pass (`gpu::Gpu::tl_live_layout`; hardware traversal).
tl_kernel_bg: Option<wgpu::BindGroup>,
/// Dynamic triangles moved in this commit: the light tree's moving subtree is refit once at its end.
emitters_moved: bool,
/// An instanced object's emitting copies changed size or were shown or hidden in this commit: the emitters are
/// weighed again once at its end.
emitters_reweigh: bool,
/// Emitters made in place this commit, for `write_emitters` (`start_moving`: a still mesh's, moving from now on).
pending_emitters: Vec<(u32, crate::GpuEmitterTri)>,
/// Something the emitters' weights are made of changed (a material, a power, a record, the table or the tree as
/// the CPU wrote them), or the scene is new (its first frame makes them, and the passes' pipelines with them, not
/// its first edit): the next frame makes them again on the GPU (`gpu::Gpu::weigh_emitters`), with what its
/// passes keep.
weights_dirty: bool,
weights: Option<gpu::EmitterWeights>,
/// Emitters have been added in place since the scene was built: an emitter edit grows the tree's slot again
/// (`write_emitters`).
slot_used: bool,
/// Objects whose instance a move hid or brought back this commit (a placement without size): their rows' power.
flipped: Vec<u32>,
/// Likewise for posed skinned models' meshes (`repose_skinned`): the spans the last new poses took (each mesh's
/// first triangle, the first of its previous vertices, how many) and whether this frame re-skinned any.
pose_spans: Vec<(usize, usize, usize)>,
pose_reposed: bool,
/// Movable instances moved by the last update (their records' previous-frame rows differ), and whether it was this
/// frame's.
inst_moving: Vec<u32>,
inst_moved: bool,
/// The previous frame's traced size (its traced-grid diffuse history; 0 before the first frame).
prev_isz: (u32, u32),
/// The previous frame composed its output from guides of its own (frame.rs `decoupled`): its thin things' records
/// are this frame's to carry (live.wgsl `Thin`).
prev_decoupled: bool,
/// The light tiles' lists as the last frame built them (pathtrace.wgsl `Params::ltile`'s y, z, `ltile2`'s size
/// and `lt_cam`, that frame's camera): what this frame's paths take candidates from. None: no lists (the first
/// frame, after a reset, a scene whose local lights are summed exactly).
light_tiles: Option<(u32, u32, (u32, u32), [[f32; 4]; 4])>,
/// Frame of the last lighting edit (power, light / material parameters, environment): the temporal pass looks
/// for changed lighting (anti-lag) only shortly after one.
lit_changed: u32,
/// The scene has light-sampled particles (emitter table entries with a nominal area).
spark_lights: bool,
/// Samples per pixel of the last frame rendered (the kernel's sample buffer holds their sums).
shown_spp: u32,
/// CPU time of the last commit (ms): skinning and re-transforming moving objects (`reskin`, rigid instance poses), and
/// uploading them (frame log `skin`, `upload`; the build they leave is `build_ms`).
pub cpu_skin_ms: f32,
pub cpu_upload_ms: f32,
/// THREERT_PROFILE_BLAS=1: GPU time of the last commit's build above the triangles (ms; frame log `blas`), measured by draining
/// the queue before it and waiting for it after (it stalls the frame: measurement runs only). The pass profile has
/// no timestamps for it (Metal cannot write them between acceleration-structure builds).
pub gpu_blas_ms: f32,
/// CPU time the last `render` spent waiting for the oldest frame in flight (ms; frame log `wait`): GPU-bound frames.
pub cpu_wait_ms: f32,
/// The loop guard (gpu.rs `guard_loops`), if a kernel of this session may have it: the invocations it has stopped at
/// its limit since the view was made, as last read back (a frame or two behind, without waiting). Above zero:
/// frames with wrong pixels, a traversal that did not end on the scene's data. None without the guard.
pub guard_trips: Option<u32>,
/// Its readback: the buffer and its state (0 free, 1 mapping, 2 mapped), as the frame timer's.
guard_read: Option<(wgpu::Buffer, Arc<AtomicU32>)>,
}
/// Ceiling for one live lighting sample, in display units at the current exposure (live.wgsl `sample_clamp`): fireflies
/// far above white. 64 until 2026-11-09, when it cut the light of small strong lights that the denoisers now keep: the
/// Bistro crowd in motion (lockstep at 10 s) diffuse 0.87 -> 0.90 of our offline render, specular 0.83 -> 0.89, the
/// pavement under the headlights 0.71 -> 0.77. 1024 adds nothing more.
const SAMPLE_CEILING: f32 = 256.0;
/// THREERT_GOVERN_CLOCK=1|0 (default 1 on Android): dynamic resolution also checks by the clock, so a view whose
/// frames are far slower than 60 a second comes down to its frame rate in seconds (`LiveCore::govern_resolution`).
fn govern_by_clock() -> bool {
static F: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*F.get_or_init(|| std::env::var("THREERT_GOVERN_CLOCK").map(|v| v == "1").unwrap_or(cfg!(target_os = "android")))
}
/// THREERT_FREEZE_TIME=1: animation holds still (shader time 0, the frame script gets dt 0: signs, screens, flicker groups),
/// so frames taken at different times compare (tools/bench-ghosting.ts).
fn freeze_time() -> bool {
static F: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*F.get_or_init(|| std::env::var("THREERT_FREEZE_TIME").is_ok_and(|v| v == "1"))
}
/// Seconds of animated shading (sign flicker, screens, ripples) and particles: the scene time; 0 under THREERT_FREEZE_TIME.
fn shader_time(time: f32) -> f32 {
if freeze_time() { 0.0 } else { time }
}
/// THREERT_EXPOSURE_SNAP=1: auto exposure jumps to its target every frame (no adaptation lag), so a frame taken while the
/// view moves has the exposure a settled view of that pose would have (tools/bench-ghosting.ts).
fn exposure_snap() -> bool {
static F: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*F.get_or_init(|| std::env::var("THREERT_EXPOSURE_SNAP").is_ok_and(|v| v == "1"))
}
/// The lens for live.wgsl's `dof_coc`, from the camera's depth-of-field settings (`desc::Dof`: focus distance in
/// metres, f-stop, sensor width in mm) and the view (vertical FOV, output size): `LiveParams::dof` = the circle of
/// confusion's radius for a point at infinity (output pixels; 0 = off), the focus distance (m), the largest radius
/// (output pixels: 2.5 % of the width).
///
/// A thin lens of focal length f and aperture diameter f / N focused at distance s images a point at distance z as
/// a disc of diameter c = (f / N) f / (s - f) |z - s| / z on the sensor (the thin-lens equation 1/f = 1/s + 1/s' and
/// similar triangles at the aperture; Potmesil & Chakravarty 1981, "A lens and aperture camera model for synthetic
/// image generation", and any optics text): c_inf (1 - s / z) with c_inf = f^2 / (N (s - f)), negative in front of
/// the focus plane. The focal length is the one that gives the view's
/// vertical FOV on a sensor of the given width and the output's aspect; a sensor width is `w` output pixels.
fn dof_params(dof: Option<&crate::desc::Dof>, tan_half_fov: f32, w: u32, h: u32) -> [f32; 4] {
let Some(d) = dof else { return [0.0; 4] };
if !(d.f_stop > 0.0 && d.sensor_width > 0.0 && d.focus_distance > 0.0 && tan_half_fov > 0.0) {
return [0.0; 4];
}
let sensor_height = d.sensor_width * h as f32 / w as f32;
let f = 0.5 * sensor_height / tan_half_fov; // mm
let s = (d.focus_distance * 1000.0).max(f * 1.001); // mm
let c_inf = f * f / (d.f_stop * (s - f)); // mm on the sensor
let largest = 0.025 * w as f32;
[(0.5 * c_inf / d.sensor_width * w as f32).max(1e-6), d.focus_distance, largest, 0.0]
}
/// Light LOD (`emitter_lod`): emitters within this distance of the camera keep their sampling weight, farther ones
/// lose it as (d0 / d)^2.
const LIGHT_LOD_D0: f32 = 10.0;
/// Emitters farther than this from the light LOD's reference are not proposed (`emitter_lod`); BSDF rays bring their
/// light.
const EMITTER_CULL: f32 = 60.0;
/// The cull's reach inside the view cone (`EmitterCull`), in cull radii: emitters ahead of the camera are kept out to
/// this.
const EMITTER_CULL_VIEW: f32 = 2.0;
/// The view cone: the frustum's half-diagonal plus this margin (degrees); the CDF is rebuilt when the camera turns by
/// LIGHT_LOD_TURN, so the frustum stays inside the cone between rebuilds.
const EMITTER_CULL_MARGIN: f32 = 20.0;
const LIGHT_LOD_TURN: f32 = 15.0;
/// Halton(base) of index i (sub-pixel jitter sequence).
fn halton(mut i: u32, base: u32) -> f32 {
let (mut f, mut r) = (1.0f32, 0.0f32);
while i > 0 {
f /= base as f32;
r += f * (i % base) as f32;
i /= base;
}
r
}
/// History caps of the cache: samples (noise), frames (lag; shorter for a while after a lighting edit).
const CACHE_SAMPLES: f32 = 256.0;
const CACHE_FRAMES: f32 = 32.0;
const CACHE_FRAMES_LIT: f32 = 4.0;
/// The most frames a pixel's history holds while the scene's lighting changes (`live_lighting`). What live.wgsl's
/// change chart follows (a light switched, a shadow that passes) does not wait for this bound, and what its drift
/// follows (a light that falls or rises steadily) is taken out of the history without shortening it ("The response
/// of the histories" there); the bound is for what both leave alone: a change too small or too noisy to tell, and
/// any change on pixels lit by rare bright samples. Those trail the light by this many frames, and every pixel's
/// noise while anything in the scene moves is that of a mean over this many.
/// Measured on the integrated tree, 2026-11-24, with the chart and the drift as they are, for 48 / 32 / 24 frames:
/// bench-window's tokyo-dusk (the sun setting over ten seconds, the frame against the same moment held; dE at 6, 7
/// and 8 s) 1.23 / 0.69 / 0.55, 1.15 / 0.64 / 0.53, 1.11 / 0.61 / 0.51 (the previous release, whose bound was 32:
/// 1.12 / 0.60 / 0.50; without the drift 2.61 / 1.44 / 0.87 at 48 and 1.73 / 0.83 / 0.58 at 32); bench-temporal's
/// lighting edit and bench-object-ghosting moved by 0.05 dE or less between 32 and 24, mostly down (the lamp's
/// bounce 32 frames after it went out 0.79 and 0.76, the runner 0.3 s after its run 0.31 and 0.29). In the tree
/// before it had specular light, the noise a shorter bound costs where nothing changes while something else moves
/// (the top fifth of the frame in the dragged device's shots and in the runner's), for 60 / 48 / 32: dE 0.24 / 0.24 /
/// 0.26 and 0.76 / 0.77 / 0.80.
const HISTORY_LIVE: f32 = 24.0;
/// The most once the scene has settled: holding still, the frame converges on the offline render.
const HISTORY_FRAMES: f32 = 4096.0;
impl LiveCore {
/// Advances the scene time by a frame's dt (the frame script's: at most 0.1 s).
pub fn advance_time(&mut self, dt: f32) {
self.time += dt.clamp(0.0, 0.1);
}
/// Sets the scene time: a game session's frame script clock (`GameSession::frame`).
pub fn set_time(&mut self, t: f32) {
self.time = t;
}
/// The scene time animation sees (0 under THREERT_FREEZE_TIME).
pub fn scene_time(&self) -> f32 {
shader_time(self.time)
}
/// A live view of the engine's world; asset files resolve against `assets`.
pub fn new(g: gpu::Gpu, world: &World, assets: &Path, presets: &Path, w: u32, h: u32) -> Result<LiveCore> {
let passes = Passes::new(&g);
// The scene's post preset replaces this LUT below (apply_post).
let cache_on = !std::env::var("THREERT_CACHE").is_ok_and(|v| v == "0");
// (Without the cache the kernel binds one of one entry.)
let targets = Targets::new(&g, &passes, w, h, &vec![0u16; (LUT * LUT * LUT * 4) as usize], if cache_on { gpu::CACHE_SIZE } else { 1 });
let (sc, view, env) = load_scene(&g, world, assets, presets, w, h, &targets)?;
let cam = view.camera;
let (triangles, scene_name) = (sc.triangles, sc.name.clone());
let profile = (g.timestamps && std::env::var("THREERT_PROFILE").is_ok()).then(|| {
// trace, the post passes, cache resolve, then the particle pass, the BLAS rebuild, the translucency pass and
// the volume training.
let n = (passes.pipes.len() as u64 + 6) * 2;
Profile {
queries: g.device.create_query_set(&wgpu::QuerySetDescriptor { label: Some("profile"), ty: wgpu::QueryType::Timestamp, count: n as u32 }),
resolve: g.device.create_buffer(&wgpu::BufferDescriptor { label: None, size: n * 8, usage: wgpu::BufferUsages::QUERY_RESOLVE | wgpu::BufferUsages::COPY_SRC, mapped_at_creation: false }),
read: g.device.create_buffer(&wgpu::BufferDescriptor { label: None, size: n * 8, usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST, mapped_at_creation: false }),
sums: vec![0.0; passes.pipes.len() + 6],
frames: 0,
grid: (
g.device.create_query_set(&wgpu::QuerySetDescriptor { label: Some("profile grid"), ty: wgpu::QueryType::Timestamp, count: 6 }),
g.device.create_buffer(&wgpu::BufferDescriptor { label: None, size: 2 * wgpu::QUERY_RESOLVE_BUFFER_ALIGNMENT, usage: wgpu::BufferUsages::QUERY_RESOLVE | wgpu::BufferUsages::COPY_SRC, mapped_at_creation: false }),
g.device.create_buffer(&wgpu::BufferDescriptor { label: None, size: 48, usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST, mapped_at_creation: false }),
),
grid_sum: 0.0,
media_sum: 0.0,
kernel: {
let n = frame::KERNEL_STAGES.len() as u64 * 2;
(
g.device.create_query_set(&wgpu::QuerySetDescriptor { label: Some("profile kernel"), ty: wgpu::QueryType::Timestamp, count: n as u32 }),
g.device.create_buffer(&wgpu::BufferDescriptor { label: None, size: n * 8, usage: wgpu::BufferUsages::QUERY_RESOLVE | wgpu::BufferUsages::COPY_SRC, mapped_at_creation: false }),
g.device.create_buffer(&wgpu::BufferDescriptor { label: None, size: n * 8, usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST, mapped_at_creation: false }),
)
},
kernel_sums: [0.0; frame::KERNEL_STAGES.len()],
}
});
let timer = (g.timestamps && profile.is_none()).then(|| FrameTimer::new(&g.device));
let cache = cache_on.then(|| RadianceCache { bufs: targets.kernel.cache.clone(), resolve: g.cache_resolve_pipeline() });
let (kernel_bg, tl_kernel_bg) = kernel_bind_groups(&g, &targets);
// (Read whenever a kernel of this session may have the guard: under software traversal a scene's first unit
// 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 reconstructor = Reconstructor::default();
#[cfg(not(target_arch = "wasm32"))]
let reconstructor = {
let mut reconstructor = reconstructor;
if let Ok(mode) = std::env::var("THREERT_RECONSTRUCTION") {
reconstructor.set(Reconstruction::parse(&mode)?);
}
reconstructor
};
let mut core = LiveCore {
kernel: None,
more_features: Default::default(),
reconstructor, quality: Quality::Native, frame_generation: FrameGeneration::Off, latency: Latency::Auto, rendering_control: None, pending_emitters: vec![], weights_dirty: true, weights: None, slot_used: false, flipped: vec![], top_builds: 0, particle_spawns: Default::default(), grid_sources: Default::default(), g, passes, targets, sc, view, env, 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, bloom: Default::default(), vignette: 0.0, post: [0.0; 4], blur: (0.0, 0.0), inflight: Default::default(), gpu_ms: 0.0, gpu_frame: 0, gpu_trace_ms: 0.0, edit_ms: 0.0, instanced_ms: 0.0, build_ms: 0.0, edit_split: EditSplit::default(), rebuild_ms: 0.0, fallback: None, fallbacks: 0, bounces: 4, rays: true, direct_pass: std::env::var("THREERT_DIRECT_PASS").map_or(true, |v| v != "0"), view_mode: std::env::var("THREERT_VIEW_MODE").ok().and_then(|v| v.parse().ok()).unwrap_or(0), fixed_dt: None, 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, min_scale: MIN_SCALE, display_scale: 1.0, over_budget: (0, 0), born: Instant::now(), governed: Instant::now(), profile, timer, cache, kernel_bg, tl_kernel_bg, emitters_moved: false, emitters_reweigh: false, pose_spans: vec![], pose_reposed: false, inst_moving: vec![], inst_moved: false, prev_isz: (0, 0), prev_decoupled: false, light_tiles: None, lit_changed: 0, spark_lights: false, shown_spp: 1, cpu_skin_ms: 0.0, cpu_upload_ms: 0.0, gpu_blas_ms: 0.0, cpu_wait_ms: 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.apply_post();
Ok(core)
}
/// Change the rendering settings given (live/rendering.rs); from the next frame.
pub fn set_rendering(&mut self, r: RenderingRequest) {
if let Some(q) = r.quality {
self.set_scale(q.scale().unwrap_or(0.0));
self.quality = q;
}
if let Some(m) = r.reconstruction { self.set_reconstruction(m); }
if let Some(f) = r.frame_generation { self.frame_generation = f; }
if let Some(l) = r.latency { self.latency = l; }
}
/// A reconstruction request: a method, a preference list, or `Reconstruction::Auto`.
pub fn set_reconstruction(&mut self, r: impl Into<Reconstruction>) {
if self.reconstructor.set(r.into()) {
self.reset_history();
}
}
/// The method the last frame ran.
pub fn reconstruction(&self) -> Method { self.reconstructor.method() }
pub fn requested_reconstruction(&self) -> &Reconstruction { &self.reconstructor.requested }
pub fn reconstruction_status(&self) -> ReconstructionStatus { self.reconstructor.status() }
pub fn rendering_status(&self) -> RenderingStatus {
RenderingStatus {
quality: self.quality, scale: self.render_scale, dynamic: self.dynamic_res,
reconstruction: self.reconstruction_status(), frame_generation: self.frame_generation, latency: self.latency,
frames_in_flight: self.frames_in_flight() as u32,
}
}
/// Frames the CPU may record ahead of the GPU: one with low latency on (or frames generated, which need it).
pub(crate) fn frames_in_flight(&self) -> usize {
match self.latency { Latency::On => 1, Latency::Off | Latency::Auto => 2 }
}
/// Install an upscaler (live/reconstruction.rs), selected as `custom:<id>` like a built-in method.
pub fn register_upscaler(&mut self, id: &str, provider: impl UpscaleProvider + 'static) -> Result<()> {
self.reconstructor.register_upscaler(id, Box::new(provider))
}
/// Install a joint denoiser (live/reconstruction.rs), selected as `custom:<id>` like a built-in method.
pub fn register_joint_denoiser(&mut self, id: &str, provider: impl JointProvider + 'static) -> Result<()> {
self.reconstructor.register_joint(id, Box::new(provider))
}
/// Share the settings with a game runtime or a page: its requests apply from the next frame, and it reads each
/// frame's status.
pub fn attach_rendering_control(&mut self, control: RenderingControl) {
if let Some(r) = control.take() { self.set_rendering(r); }
control.publish(self.rendering_status());
self.rendering_control = Some(control);
}
pub fn scene_camera(&self) -> Camera {
self.view.camera
}
/// `guard_trips` for the frame log: None while the scene's kernel has no guard. Natively with nothing set that is
/// a scene without units (`gpu::loop_guard_of`): under software traversal the first unit brings the guard.
pub fn guard_count(&self) -> Option<u32> {
self.guard_trips.filter(|_| gpu::loop_guard_of(self.sc.up.kernel.features & gpu::F_UNITS != 0).is_some())
}
/// The edit room as the frame log reports it (`room`): triangles, records and object entries in use and held (GPU
/// order counts, holes not in use), its growths, and the GPU bytes of the buffers edits write and of the triangle
/// BLASes the live meshes need (estimated from the build's bytes per triangle), MB.
pub fn room_stats(&self) -> serde_json::Value {
let (e, up) = (&self.sc.edit, &self.sc.up);
// Software traversal's (it does not grow): the same counts, and of the node buffer the mesh trees' range (in
// use, held) and the units the top tree has against the most its range holds.
if let Some(u) = up.units.as_ref() {
return serde_json::json!({
"tris": [e.tris.used(), e.tris.cap], "records": [e.records.used(), e.records.cap], "objects": [e.objects.used(), e.objects.cap],
"holes": e.tris.free.len(), "grows": e.grows, "nodes": [u.layout.mesh_cap as usize - u.layout.free_nodes(), u.layout.mesh_cap], "units": [u.count, u.layout.max_units()],
});
}
let Some(a) = up.accel.as_ref() else { return serde_json::Value::Null };
let buffers = [&up.vertices, &up.prev_vertices, &up.heap.buffer, &up.words.buffer].iter().map(|b| b.size()).sum::<u64>();
let live: u64 = self.sc.meshes.iter().enumerate().filter(|(k, _)| e.users.get(&(*k as u32)).copied().unwrap_or(0) > 0).map(|(_, m)| m.count as u64).sum::<u64>();
let mb = |b: f64| (b / (1 << 20) as f64 * 10.0).round() / 10.0;
serde_json::json!({
"tris": [e.tris.used(), e.tris.cap], "records": [e.records.used(), e.records.cap], "objects": [e.objects.used(), e.objects.cap],
"holes": e.tris.free.len(), "grows": e.grows, "buffers_mb": mb(buffers as f64), "blas_mb": mb(live as f64 * a.blas_per_tri),
})
}
/// Software traversal's top tree so far (the frame log's `top`): how many times it was built, kept with leaves
/// taken out and put in, and kept with its boxes brought up to date, and the ms all of that took. Null without
/// units.
pub fn top_stats(&self) -> serde_json::Value {
self.sc.up.units.as_ref().map_or(serde_json::Value::Null, |u| serde_json::json!([u.kept.builds, u.kept.changes, u.kept.refits, (u.kept.ms * 1e3).round() / 1e3]))
}
pub fn rebuild(&mut self, world: &World) -> Result<()> {
let (s, view, env) = load_scene(&self.g, world, &self.assets, &self.presets, self.w, self.h, &self.targets)?;
self.triangles = s.triangles;
self.scene_name = s.name.clone();
// The scene's camera changed (a new lens rebuilds, and a move in the same commit comes with it): the view takes it.
// Otherwise the view stays where it is.
if view.camera != self.view.camera {
self.cam = view.camera;
}
(self.view, self.env) = (view, env);
self.apply_post();
// What the grid programs had simulated goes with the grids that are still the same.
self.g.carry_grids(&self.sc.up, &s.up);
self.sc = s;
// (What the weight passes kept was the old scene's; the first frame makes the weights, as after a load.)
(self.weights, self.weights_dirty, self.slot_used) = (None, true, false);
// (Its data is its own kernel's: the load made that one.)
self.kernel = None;
self.spark_lights = self.sc.tables.emitter_tris.iter().any(|e| e.nominal);
self.inst_moving.clear();
self.pose_spans.clear();
self.emitters_moved = false;
self.emitters_reweigh = false;
self.lit_changed = self.frame.max(1);
Ok(())
}
/// The post passes' settings from the view's preset (`self.view`: assigned before this): the tone-map table,
/// written when the preset's post settings are others, and bloom / vignette / fringe / grain / motion blur, of
/// which the bloom's stages and the blur's reach follow the view's size.
fn apply_post(&mut self) {
let preset = self.view.preset.clone();
let pp = &preset.post;
let key = format!("{pp:?}");
if key != self.post_key {
let lut = tonemap_lut(pp);
self.g.queue.write_texture(
wgpu::TexelCopyTextureInfo { texture: &self.targets.lut_tex, mip_level: 0, origin: wgpu::Origin3d::ZERO, aspect: wgpu::TextureAspect::All },
bytemuck::cast_slice(&lut),
wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(LUT * 8), rows_per_image: Some(LUT) },
wgpu::Extent3d { width: LUT, height: LUT, depth_or_array_layers: LUT },
);
self.post_key = key;
}
self.bloom = post::BloomPlan::new(pp, self.w, self.h);
self.vignette = pp.vignette;
// THREERT_NO_GRAIN=1: no film grain (a fresh random pattern every frame; comparisons between frames see it as error).
let grain = if std::env::var("THREERT_NO_GRAIN").is_ok_and(|v| v == "1") { 0.0 } else { pp.film_grain };
// THREERT_MOTION_BLUR=1: the default motion blur (`desc::MotionBlur`) whatever the preset says; =amount,max,targetFps: those.
let mb = match std::env::var("THREERT_MOTION_BLUR").ok().as_deref() {
Some("1") => crate::desc::MotionBlur::default(),
Some(v) => match v.split(',').map(|x| x.trim().parse::<f32>()).collect::<Result<Vec<_>, _>>().as_deref() {
Ok([amount, max, target_fps]) => crate::desc::MotionBlur { amount: *amount, max: *max, target_fps: *target_fps },
_ => pp.motion_blur.clone(),
},
None => pp.motion_blur.clone(),
};
self.post = [pp.chromatic_aberration, grain, pp.bloom.threshold, mb.max / 100.0 * self.w as f32];
self.blur = (if mb.max > 0.0 { mb.amount.max(0.0) } else { 0.0 }, mb.target_fps);
}
/// Switches to a different world: camera from it, history cleared.
pub fn open(&mut self, world: &World, assets: &Path) -> Result<()> {
self.assets = assets.to_path_buf();
self.rebuild(world)?;
if let Some(c) = &self.cache {
let mut e = self.g.device.create_command_encoder(&Default::default());
c.bufs.clear(&mut e);
self.g.queue.submit([e.finish()]);
}
self.cam = self.view.camera;
self.prev_cam = self.cam;
self.reset = true;
Ok(())
}
/// The view is a display's physical pixels, `k` a point: dynamic resolution runs from the points' resolution (the
/// rays a view in points starts with) down to `MIN_SCALE` of it, and starts there again; a fixed scale stays.
pub fn set_display_scale(&mut self, k: f32) {
let k = k.max(1.0);
self.display_scale = k;
self.min_scale = (MIN_SCALE / k).max(SCALE_FLOOR);
if self.dynamic_res {
self.render_scale = 1.0 / k;
}
}
/// The scene's lighting is changing: something moved or was edited in the last 45 frames, or the scene has spark
/// lights (light-sampled particles: they move every frame, and no edit says so).
fn live_lighting(&self) -> bool {
self.spark_lights || (self.lit_changed > 0 && self.frame.wrapping_sub(self.lit_changed) < 45)
}
/// Fixed render scale (`s` in SCALE_FLOOR ..= 1), or dynamic resolution (`s` = 0).
pub fn set_scale(&mut self, s: f32) {
self.quality = if s <= 0.0 { Quality::Auto } else { Quality::Scale(s) };
self.dynamic_res = s <= 0.0;
if s > 0.0 {
self.render_scale = s.clamp(SCALE_FLOOR, 1.0);
}
}
pub fn reset_history(&mut self) {
self.reset = true;
}
/// The next picture of a stream texture (a material's `StreamTexture`, by its name): a screen in the scene shows
/// what the app draws. The pixels go into the texture's place; the scene and the history stay. True when the
/// scene has the stream.
pub fn write_stream(&mut self, name: &str, width: u32, height: u32, rgba: &[u8]) -> Result<bool> {
self.g.write_stream(name, width, height, rgba)
}
/// 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.view.camera.tan_half_fov;
}
self.cam = c;
}
/// Room in `finish`'s records for a sample of `pixels` traced pixels, any of which may leave one. The buffer follows
/// the traced size, not the output's (96 bytes a pixel: 354 MB for a 2560 x 1440 output, 50 traced at 0.375), and
/// is made a quarter larger than asked, so dynamic resolution's steps seldom make it again.
pub(super) fn ensure_deferred(&mut self, pixels: u64) {
let Some(d) = &self.targets.kernel.deferred else { return };
if self.cache.is_none() || d.size >= 16 + pixels * gpu::DEFERRED_BYTES {
return;
}
self.targets.kernel.deferred = Some(gpu::deferred_buffer(&self.g.device, (pixels + pixels / 4).min(self.w as u64 * self.h as u64)));
(self.kernel_bg, self.tl_kernel_bg) = kernel_bind_groups(&self.g, &self.targets);
}
/// Room after the traced pixels' direct light for the output grid's guides (`media`: and a medium's record a
/// traced pixel), where a frame's output is composed from them (frame.rs `decoupled`). The kernel's buffers are
/// made again for it once, as for `ensure_first`.
pub(super) fn ensure_guides(&mut self, media: bool) {
let direct = if media { 4 } else { 3 };
if self.targets.direct >= direct {
return;
}
self.targets.grow_direct(&self.g, &self.passes, direct);
// (The merged layout's cache is in the buffer made again, as in `ensure_first`.)
if self.g.merged {
if let Some(c) = &mut self.cache {
c.bufs = self.targets.kernel.cache.clone();
}
(self.reset, self.prev_isz) = (true, (0, 0));
}
(self.kernel_bg, self.tl_kernel_bg) = kernel_bind_groups(&self.g, &self.targets);
}
/// Room for a frame's camera hits at `pixels` traced pixels: 32 bytes each, 96 more for a mirror chain in a
/// scene that has a mirror (`gpu::F_MIRROR`; pathtrace.wgsl `Chain`: without the room the first pass follows no
/// mirror), and the surface the paths find there (`gpu::SURFACE_BYTES`: without the room the paths light it
/// themselves). The buffer follows the traced size, a quarter larger than asked, as `finish`'s records do.
pub(super) fn ensure_first(&mut self, pixels: u64) {
let each = if self.sc.up.kernel.features & gpu::F_MIRROR != 0 { 128 } else { 32 } + if self.direct_pass { gpu::SURFACE_BYTES } else { 0 };
if self.targets.kernel.first.size >= pixels * each {
return;
}
self.targets.grow_first(&self.g, &self.passes, (pixels + pixels / 4).min(self.w as u64 * self.h as u64) * each);
// (The merged layout's cache is in the buffer made again: the view's cache is that one from here, empty, and
// nothing of the frames before stands.)
if self.g.merged {
if let Some(c) = &mut self.cache {
c.bufs = self.targets.kernel.cache.clone();
}
(self.reset, self.prev_isz) = (true, (0, 0));
}
(self.kernel_bg, self.tl_kernel_bg) = kernel_bind_groups(&self.g, &self.targets);
}
/// The view at another size (its host's window, in pixels): the targets that follow the size are made again
/// (`Targets::resize`) and bound, the scene with the new samples' buffer; the frames' histories start again. The
/// scene's data, the radiance cache (the world's), the view's camera, state and render scale stay. A size this
/// device does not hold is refused, the view as it was.
pub fn resize(&mut self, w: u32, h: u32) -> Result<()> {
let (w, h) = (w.max(1), h.max(1));
if (w, h) == (self.w, self.h) {
return Ok(());
}
if !self.g.view_fits(w, h) {
anyhow::bail!("a view of {w} x {h}: more samples than this device holds in one buffer");
}
(self.w, self.h, self.aspect) = (w, h, w as f32 / h as f32);
self.reconstructor.invalidate();
self.targets.resize(&self.g, &self.passes, w, h);
if let Some(c) = &mut self.cache {
c.bufs = self.targets.kernel.cache.clone();
}
(self.kernel_bg, self.tl_kernel_bg) = kernel_bind_groups(&self.g, &self.targets);
self.g.set_accum(&mut self.sc.up, &self.targets.accum);
self.apply_post();
// As a new view: no history, no frame before this one.
(self.reset, self.prev_isz) = (true, (0, 0));
Ok(())
}
/// Renders one frame with `spp` samples per pixel; returns the command buffers to submit after the samples.
/// Path-trace dispatches are submitted inside (each needs its own uniform contents).
/// Traced resolution at the current render scale: 8 pixels a side at least, or the view's where it has fewer.
pub fn internal_size(&self) -> (u32, u32) {
let s = self.render_scale.clamp(SCALE_FLOOR, 1.0);
let side = |n: u32| ((n as f32 * s).round() as u32).clamp(n.min(8), n);
(side(self.w), side(self.h))
}
/// Dynamic resolution (UE r.DynamicRes): twice a second, move the render scale toward the frame budget (traced
/// pixels ~ GPU time), within [`min_scale`, 1], in damped steps with hysteresis so it does not oscillate.
/// Two frames in a row well over the frame time (x 1.25) act at once (UE
/// r.DynamicRes.MaxConsecutiveOverbudgetGPUFrameCount = 2): turning from a light view at a raised scale toward a
/// heavy one otherwise cost up to 30 frames at 35-48 ms (the 3 x 3 district's drive, its far end). Just over the
/// frame time waits for the regular check, as before. After a change, only frames rendered at the new scale count:
/// GPU time lags the scale by the frames in flight, and acting on the old scale's time overshot to the floor.
///
/// "Twice a second" and "the first 2 s" are counted in frames (every 30th, the first 120), which is the same thing
/// at 60 frames a second. Where frames are much slower (`govern_by_clock`: a phone's GPU, on which a scene opens
/// at its full scale at 6 frames a second) they are also read off the clock: counted in frames alone the first
/// check that may act at once came after 20 s and the regular one every 5 s, and a game opened as a slide show
/// for half a minute (Adreno 740, OpenStrikeRT: 25 s to its frame rate; 3 s with the clock).
pub fn govern_resolution(&mut self, fps: u32) {
if !self.dynamic_res || self.gpu_ms <= 0.0 {
return;
}
if (self.gpu_frame.wrapping_sub(self.over_budget.1) as i32) < 0 {
return;
}
let clock = govern_by_clock();
// Not in the first 2 s: those frames run while caches fill (Kabukichō's first frames at 0.7 take ~47 ms), and
// the regular check settles the start as before.
let warm = self.frame >= 120 || (clock && self.born.elapsed().as_secs_f32() >= 2.0);
// (The floor: the budget's, or higher where the reconstruction asked for takes no lower scale: MetalFX scales
// outputs up to 3x its input, so a third.)
let floor = self.min_scale.max(self.reconstructor.scale_floor().unwrap_or(0.0)).min(1.0);
self.over_budget.0 = if warm && self.gpu_ms > 1250.0 / fps as f32 && self.render_scale > floor { self.over_budget.0 + 1 } else { 0 };
let urgent = self.over_budget.0 >= 2;
// (By the clock only when 30 frames take longer than 0.75 s: at 40 frames a second and above the frame count
// comes first, as it always did.)
let due = self.frame % 30 == 1 || (clock && self.governed.elapsed().as_secs_f32() >= 0.75);
if !due && !urgent {
return;
}
self.governed = Instant::now();
self.over_budget.0 = 0;
let before = self.render_scale;
let budget = 1000.0 / fps as f32 * 0.9;
let target = (self.render_scale * (budget / self.gpu_ms).sqrt()).clamp(floor, 1.0);
// The hysteresis (0.03) does not hold the scale just short of either end: 0.525 with the floor as the target
// kept the district's return drive at 21-22 ms.
let to_end = (target == 1.0 && self.render_scale < 1.0 && self.gpu_ms < budget * 0.8) || (target == floor && self.render_scale > floor) || self.render_scale < floor;
// (The hysteresis, the steps and the grid of scales are a view in points'; on a display of k pixels a point the
// same share of the rays is 1 / k of the scale. Left absolute, a Retina view held 0.325 at 17.3 ms: its target,
// 0.30, was inside the hysteresis.)
let k = self.display_scale;
if (target - self.render_scale).abs() > 0.03 / k || to_end {
// Down fast when far over budget (a heavy scene just opened), up slowly; straight to the target when urgent.
let step = if urgent { target - self.render_scale } else { (target - self.render_scale).clamp(if self.gpu_ms > budget * 1.5 { -0.3 } else { -0.15 } / k, 0.1 / k) };
self.render_scale = ((self.render_scale + step) * 40.0 * k).round() / (40.0 * k);
// (On the grid, not below the floor: a step rounded down under a provider's range would lose it again.)
if self.render_scale < floor { self.render_scale = floor; }
}
if self.render_scale != before {
// `frame` is already the next frame's index (render advanced it): the first one at the new scale.
self.over_budget.1 = self.frame;
}
}
#[cfg(not(target_arch = "wasm32"))]
/// The last frame's linear HDR radiance (nits, before exposure and post), RGB per output pixel. Waits for the GPU.
pub fn read_hdr(&self) -> Result<Vec<f32>> {
let d = &self.g.device;
let size = self.w as u64 * self.h as u64 * 16;
let read = d.create_buffer(&wgpu::BufferDescriptor { label: Some("hdr read"), size, usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST, mapped_at_creation: false });
let mut e = d.create_command_encoder(&Default::default());
let fa = self.targets.buf("fa");
e.copy_buffer_to_buffer(&fa.buffer, fa.offset, &read, 0, size);
self.g.queue.submit([e.finish()]);
read.slice(..).map_async(wgpu::MapMode::Read, |_| {});
d.poll(wgpu::PollType::wait_indefinitely())?;
let px = bytemuck::cast_slice::<u8, [f32; 4]>(&read.slice(..).get_mapped_range().unwrap()).iter().flat_map(|p| [p[0], p[1], p[2]]).collect();
Ok(px)
}
#[cfg(not(target_arch = "wasm32"))]
/// The last frame's signals apart, linear RGB f32 per output pixel: diffuse (the composed colour less the rest, so
/// with the sun's direct light, which view mode 6 leaves out), specular (the specular denoiser's output: mode 7) and
/// emission (what the camera ray saw directly). They are the render resolution's (the frame as `compose` hands it to
/// the upscaler), each output pixel taking the traced sample under it; not with depth of field and motion blur both
/// on (the composed colour's buffer is theirs then). Waits for the GPU.
pub fn read_signals(&self) -> Result<[Vec<f32>; 3]> {
let d = &self.g.device;
let (w, h) = (self.w as usize, self.h as usize);
let (iw, ih) = (self.prev_isz.0 as usize, self.prev_isz.1 as usize);
let (n, tn) = ((w * h) as u64, (iw * ih) as u64);
let t = &self.targets;
let read = |buffer: &wgpu::Buffer, offset: u64, size: u64| -> Result<Vec<u8>> {
let r = d.create_buffer(&wgpu::BufferDescriptor { label: Some("signals read"), size, usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST, mapped_at_creation: false });
let mut e = d.create_command_encoder(&Default::default());
e.copy_buffer_to_buffer(buffer, offset, &r, 0, size);
self.g.queue.submit([e.finish()]);
r.slice(..).map_async(wgpu::MapMode::Read, |_| {});
d.poll(wgpu::PollType::wait_indefinitely())?;
let v = r.slice(..).get_mapped_range()?.to_vec();
Ok(v)
};
let half = |b: u32, hi: bool| half::f16::from_bits(if hi { (b >> 16) as u16 } else { b as u16 }).to_f32();
let fb = t.buf("fb");
let composed = read(&fb.buffer, fb.offset, tn * 16)?;
let spec_pp = t.buf("spec_pp");
let spec = read(&spec_pp.buffer, spec_pp.offset + n * 8, tn * 8)?; // spec_pp slot 1: (rgb, history) as two pairs of f16
let samples = read(&self.targets.accum, 0, tn * gpu::SAMPLE_BYTES)?; // the kernel's samples (live.wgsl `Pixel`): emit at 48
let (composed, spec, samples): (&[[f32; 4]], &[[u32; 2]], &[[f32; 20]]) = (bytemuck::cast_slice(&composed), bytemuck::cast_slice(&spec), bytemuck::cast_slice(&samples));
let (mut dif, mut spe, mut emi) = (Vec::with_capacity(w * h * 3), Vec::with_capacity(w * h * 3), Vec::with_capacity(w * h * 3));
let finite = |v: f32| if v.is_finite() { v } else { 0.0 };
for y in 0..h {
for x in 0..w {
// live.wgsl `sample_under`.
let q = (((y as f32 + 0.5) * ih as f32 / h as f32) as usize).min(ih - 1) * iw + (((x as f32 + 0.5) * iw as f32 / w as f32) as usize).min(iw - 1);
// The media before the surface (live.wgsl `medium_tr`): the specular as the frame shows it.
let tr = (samples[q][11].fract() * 2.0).clamp(0.0, 1.0);
let s = [half(spec[q][0], false), half(spec[q][0], true), half(spec[q][1], false)].map(|v| v * tr);
for c in 0..3 {
let e = finite(samples[q][12 + c] / self.shown_spp as f32);
spe.push(s[c]);
emi.push(e);
dif.push((composed[q][c] - s[c] - e).max(0.0));
}
}
}
Ok([dif, spe, emi])
}
#[cfg(not(target_arch = "wasm32"))]
/// The exposure the last frame was shown at, EV100 (auto exposure: its adapted value).
pub fn read_ev100(&self) -> Result<f32> {
if self.view.exposure_settings.ae2[3] <= 0.5 {
return Ok(-self.view.exposure.log2());
}
let d = &self.g.device;
let read = d.create_buffer(&wgpu::BufferDescriptor { label: Some("ae read"), size: 16, usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST, mapped_at_creation: false });
let mut e = d.create_command_encoder(&Default::default());
let ae = self.targets.buf("ae_state");
e.copy_buffer_to_buffer(&ae.buffer, ae.offset, &read, 0, 16);
self.g.queue.submit([e.finish()]);
read.slice(..).map_async(wgpu::MapMode::Read, |_| {});
d.poll(wgpu::PollType::wait_indefinitely())?;
let s = bytemuck::cast_slice::<u8, f32>(&read.slice(..).get_mapped_range().unwrap())[0];
Ok(-s.max(1e-30).log2())
}
/// Phases of the last scene (re)load, ms.
pub fn load_timings(&self) -> &[(String, f64)] {
&self.sc.timings
}
pub fn aspect(&self) -> f32 {
self.aspect
}
/// Primary ray through (u, v) of the image (0..1, v down), as the path tracer generates it.
pub fn ray(&self, u: f32, v: f32) -> (Vec3, Vec3) {
let c = &self.cam;
let d = c.fwd + c.right * ((u * 2.0 - 1.0) * c.tan_half_fov * self.aspect) + c.up * ((1.0 - v * 2.0) * c.tan_half_fov);
(c.pos, d.normalize())
}
/// Closest surface under (u, v): (node id, world-space hit point). CPU ray cast over the scene triangles, and over
/// each instance's model-space mesh with the ray in its space (same ray parameter t).
/// A mesh's triangles in its own space, as they are now. One that deforms is skinned by the GPU, and the CPU keeps
/// none of its pose: it is skinned here when asked for (a click, a query), once a call (`posed`, by mesh).
fn mesh_tris<'a>(&'a self, mesh: u32, posed: &'a mut std::collections::HashMap<u32, Vec<[glam::Vec3A; 3]>>) -> &'a [[glam::Vec3A; 3]] {
let m = self.sc.meshes[mesh as usize];
let pool = &self.sc.pool_tris[m.first as usize..(m.first + m.count) as usize];
if !m.deforms || self.sc.up.skin.is_none() {
return pool;
}
let Some(pg) = self.sc.tables.retained.skinned.iter().find(|g| g.mesh == mesh) else { return pool };
if !posed.contains_key(&mesh) {
let tris = scene::model_gltf(&pg.uri, &self.assets).and_then(|model| model.triangles(glam::Mat4::IDENTITY, pg.nodes.as_deref(), Some(&pg.animation)));
match tris {
// (The mesh's triangles are the model's, less what the build left out of it.)
Ok(t) if pg.order.iter().all(|&(k, _, _)| (k as usize) < t.len()) => posed.insert(mesh, pg.order.iter().map(|&(k, _, _)| t[k as usize].p.map(glam::Vec3A::from)).collect()),
_ => return pool,
};
}
&posed[&mesh]
}
/// The object under view point (u, v): its name, the hit point, and for an instanced object the instance's index.
pub fn pick(&self, u: f32, v: f32) -> Option<(String, Vec3, Option<u32>)> {
let (o, d) = self.ray(u, v);
let (o, d) = (glam::Vec3A::from(o), glam::Vec3A::from(d));
// Moller-Trumbore, two-sided: the ray parameter of the closest hit in `tris` beyond `near`.
let closest = |tris: &[[glam::Vec3A; 3]], o: glam::Vec3A, d: glam::Vec3A| {
let mut best = (f32::INFINITY, usize::MAX);
for (i, [a, b, c]) in tris.iter().enumerate() {
let (e1, e2) = (*b - *a, *c - *a);
let p = d.cross(e2);
let det = e1.dot(p);
if det.abs() < 1e-12 {
continue;
}
let inv = 1.0 / det;
let s = o - *a;
let bu = s.dot(p) * inv;
if !(0.0..=1.0).contains(&bu) {
continue;
}
let q = s.cross(e1);
let bv = d.dot(q) * inv;
if bv < 0.0 || bu + bv > 1.0 {
continue;
}
let t = e2.dot(q) * inv;
if t > 1e-4 && t < best.0 {
best = (t, i);
}
}
best
};
let (mut t, mut node, mut sub) = (f32::INFINITY, None, None);
let mut posed = Default::default();
for inst in self.sc.instances.iter().filter(|i| !i.hidden) {
let m = self.sc.meshes[inst.mesh as usize];
let inv = inst.world.inverse();
let (lo, ld) = (inv.transform_point3a(o), inv.transform_vector3a(d));
// Slab test against the mesh bounds before its triangles.
let (t0, t1) = ((m.lo - lo) / ld, (m.hi - lo) / ld);
let (near, far) = (t0.min(t1).max_element(), t0.max(t1).min_element());
if far < near.max(0.0) || near > t {
continue;
}
let (ti, _) = closest(&self.mesh_tris(inst.mesh, &mut posed), lo, ld);
if ti < t {
t = ti;
node = Some(inst.name.as_str());
sub = inst.sub;
}
}
// Procedural geometry: its boxes (the geometry function runs on the GPU only).
for p in self.sc.procs.iter().filter(|p| !p.hidden) {
let inv = p.world.inverse();
let (lo, ld) = (inv.transform_point3a(o), inv.transform_vector3a(d));
let slab = |a: glam::Vec3A, b: glam::Vec3A| {
let (t0, t1) = ((a - lo) / ld, (b - lo) / ld);
let (near, far) = (t0.min(t1).max_element().max(1e-4), t0.max(t1).min_element());
(near <= far).then_some(near)
};
if slab(p.lo.into(), p.hi.into()).is_none_or(|near| near > t) {
continue;
}
for b in p.blob.boxes.chunks(6) {
if let Some(near) = slab(glam::Vec3A::new(b[0], b[1], b[2]), glam::Vec3A::new(b[3], b[4], b[5])).filter(|&near| near < t) {
t = near;
node = Some(p.name.as_str());
sub = p.sub;
}
}
}
Some((node?.to_string(), Vec3::from(o + d * t), sub))
}
/// World-space triangles of a node (empty for lights without geometry, cameras, hidden nodes).
pub fn node_tris(&self, id: &str) -> Vec<[glam::Vec3A; 3]> {
let mut out = vec![];
// A model's first instance; every drawn instance of an instanced object.
let mut of_node = self.sc.instances.iter().filter(|i| i.name == id && !i.hidden);
let first = of_node.next();
let mut posed = Default::default();
for inst in first.into_iter().chain(of_node.filter(|i| i.sub.is_some())) {
out.extend(self.mesh_tris(inst.mesh, &mut posed).iter().map(|t| inst.tri(t)));
}
out
}
/// Frame metadata (camera pose for picking, convergence, timings).
pub fn meta(&self) -> serde_json::Value {
let c = &self.cam;
serde_json::json!({
"seq": self.seq, "frame": self.frame, "history": self.history_len, "gpu_ms": self.gpu_ms,
"reconstruction": self.reconstruction().name(), "requested_reconstruction": self.requested_reconstruction().name(),
"reconstruction_fallback": self.reconstructor.status().fallback.map(Skip::name), "rendering": self.rendering_status().json(),
"scene": self.scene_name, "triangles": self.triangles, "bounces": self.bounces, "mode": self.view_mode, "width": self.w, "height": self.h, "scale": self.render_scale,
"cam": { "pos": c.pos.to_array(), "fwd": c.fwd.to_array(), "up": c.up.to_array(), "right": c.right.to_array(), "tan": c.tan_half_fov, "aspect": self.aspect },
})
}
}
/// Adaptive samples per frame: fill ~75% of the frame budget with path tracing.
pub fn next_spp(spp: u32, gpu_ms: f32, fps: u32) -> u32 {
if gpu_ms <= 0.0 {
return spp;
}
let budget = 750.0 / fps as f32;
let per_sample = gpu_ms / spp as f32;
let target = (budget / per_sample).floor() as u32;
target.clamp(1, 16).min(spp + 1).max(spp.saturating_sub(2).max(1))
}
#[cfg(not(target_arch = "wasm32"))]
/// `--scale`: a fraction (0.25 .. 1) or `auto` (dynamic resolution).
pub fn parse_scale(v: Option<&str>, default: Option<f32>) -> Result<Option<f32>> {
match v {
None => Ok(default),
Some("auto") => Ok(None),
Some(v) => v.parse::<f32>().map(|s| Some(s.clamp(SCALE_FLOOR, 1.0))).map_err(|_| anyhow!("--scale must be a number or auto")),
}
}
#[cfg(test)]
mod timer_tests {
use super::*;
#[test]
fn a_view_in_physical_pixels_starts_with_dynamic_resolution() {
let opts = |extra: &[&str]| parse_opts(&["threert-render", "live", "game.js"].iter().chain(extra).map(|a| a.to_string()).collect::<Vec<_>>()).unwrap();
assert_eq!(opts(&[]).scale, Some(1.0));
assert_eq!(opts(&["--display-scale", "2"]).scale, None);
assert_eq!(opts(&["--display-scale", "2", "--scale", "0.2"]).scale, Some(0.2));
assert_eq!(opts(&["--scale", "auto"]).scale, None);
}
#[test]
fn a_post_pass_from_another_frame_counts_by_its_own_length() {
// Two reads of the crowd scene (ns, period 1), from when a slot's stamps were resolved in their own command
// buffers: one frame's, and a mix of two uses of the slot (the post pair 74.5 ms after the trace's).
let t453 = [35929577531333, 35929585495833, 35929585700583, 35929591610875, 0, 0, 0, 0, 0, 0];
let t454 = [35929593765500, 35929601614666, 35929668312875, 35929673973041, 0, 0, 0, 0, 0, 0];
let ms = |t: [u64; 10]| frame_gpu_ms(&t, 1, 1.0).unwrap();
assert_eq!(ms(t453), (t453[3] - t453[0]) as f32 * 1e-6);
assert!((ms(t454) - (7.849166 + 5.660166)).abs() < 1e-3, "{}", ms(t454));
// The first pass is the frame's start; nothing timed, no measurement.
assert_eq!(frame_gpu_ms(&[0; 10], 1, 1.0), None);
let particles_first = [2_000_000, 9_000_000, 9_200_000, 14_000_000, 0, 0, 1_000_000, 1_900_000, 0, 0];
assert_eq!(frame_gpu_ms(&particles_first, 1, 1.0), Some(13.0));
let weights_first = [2_000_000, 9_000_000, 9_200_000, 14_000_000, 1_500_000, 1_900_000, 0, 0, 0, 0];
assert_eq!(frame_gpu_ms(&weights_first, 1, 1.0), Some(12.5));
// 3 samples: two more traces (untimed) between the first and the post pass, a 16.2 ms gap: the span. The post
// pair of frame 458 under 3 samples: still out of place (trace 7.85 ms, gap 66.7 ms), counted as the passes.
let three = [1_000_000, 9_000_000, 25_200_000, 31_000_000, 0, 0, 0, 0, 0, 0];
assert_eq!(frame_gpu_ms(&three, 3, 1.0), Some(30.0));
assert!((frame_gpu_ms(&t454, 3, 1.0).unwrap() - (3.0 * 7.849166 + 5.660166)).abs() < 1e-3);
}
#[test]
fn a_frame_with_a_reconstruction_provider_ends_with_the_passes_after_it() {
let near = |t: [u64; 10], want: f32| { let ms = frame_gpu_ms(&t, 1, 1.0).unwrap(); assert!((ms - want).abs() < 1e-4, "{ms} for {want}"); };
// The trace, the passes before the provider (the second pair), the provider, the passes after it (the fifth).
near([2_000_000, 9_000_000, 9_200_000, 10_500_000, 0, 0, 0, 0, 11_300_000, 12_000_000], 10.0);
// A late post pass: the trace, then from the second pair's begin to the fifth's end.
near([2_000_000, 9_000_000, 29_200_000, 30_500_000, 0, 0, 0, 0, 31_300_000, 32_000_000], 7.0 + 2.8);
// Without a provider the fifth pair is zeros: the post pass ends the frame.
near([2_000_000, 9_000_000, 9_200_000, 10_500_000, 0, 0, 0, 0, 0, 0], 8.5);
}
}
#[cfg(test)]
mod inplace_tests;
#[cfg(test)]
mod instanced_tests {
use super::*;
use crate::world::{Geometry, Kind};
use glam::Mat4;
/// Clicks pick what is drawn (ROADMAP § 6 step 5): an object added in place is picked, one removed in place no
/// longer is, and a rebuild agrees. (Skipped without a GPU with hardware RT.)
#[test]
fn picks_follow_in_place_edits() {
let g = match gpu::Gpu::new() {
Ok(g) if g.hwrt => g,
other => return eprintln!("skipped: {}", other.map_or_else(|e| e.to_string(), |_| "no hardware RT".into())),
};
let mut w = World::default();
w.set_material(1, serde_json::from_value(serde_json::json!({ "name": "m", "color": [0.5, 0.5, 0.5], "opacity": 1, "metalness": 0, "roughness": 1, "emissive": [0, 0, 0], "emissiveIntensity": 0, "doubleSided": true })).unwrap());
let quad = |s: f32| Geometry { positions: vec![-s, -s, 0.0, s, -s, 0.0, s, s, 0.0, -s, s, 0.0], normals: vec![0.0, 0.0, 1.0].repeat(4), uvs: vec![0.0; 8], indices: vec![0, 1, 2, 0, 2, 3], groups: vec![], colors: vec![] };
w.set_geometry(2, quad(1.0)).unwrap();
w.set_geometry(3, quad(0.5)).unwrap();
let mesh = |geometry| Kind::Mesh { geometry, material: 1, own_blas: true, materials: vec![] };
w.set_object(10, "a".into(), None, mesh(2), true, false).unwrap();
let cam = serde_json::from_value(serde_json::json!({ "fov": 50, "near": 0.1, "far": 100, "exposure": { "mode": "manual", "ev100": 0 } })).unwrap();
w.set_object(4, "camera".into(), None, Kind::Camera(cam), true, false).unwrap();
w.set_transform(4, Vec3::new(0.0, 0.0, 5.0), glam::Quat::IDENTITY, Vec3::ONE).unwrap();
w.set_settings(crate::world::Settings { camera: Some(4), ..Default::default() });
let mut core = LiveCore::new(g, &w, Path::new("."), &crate::default_presets_dir(), 64, 36).unwrap();
w.take_changes();
let picked = |core: &LiveCore| core.pick(0.5, 0.5).map(|p| p.0);
assert_eq!(picked(&core).as_deref(), Some("a"));
// Added in place, in front of it: picked.
w.set_object(11, "b".into(), None, mesh(3), true, false).unwrap();
w.set_transform(11, Vec3::new(0.0, 0.0, 1.0), glam::Quat::IDENTITY, Vec3::ONE).unwrap();
assert!(!core.commit(&mut w).unwrap(), "added in place");
assert_eq!(picked(&core).as_deref(), Some("b"));
assert_eq!(core.node_tris("b").len(), 2, "its triangles under its name");
// Removed in place: the one behind it is picked again; both removed: nothing.
w.remove(11);
assert!(!core.commit(&mut w).unwrap(), "removed in place");
assert_eq!(picked(&core).as_deref(), Some("a"));
assert!(core.node_tris("b").is_empty(), "removed: none");
w.remove(10);
assert!(!core.commit(&mut w).unwrap(), "removed in place");
assert_eq!(picked(&core), None);
core.rebuild(&w).unwrap();
assert_eq!(picked(&core), None, "a rebuild agrees");
}
/// A mesh a rigid body drives, added while the game runs with a material the scene did not have (ROADMAP § 6 step
/// 7): placed in place, and its body's moves are its instance's transform, with no rebuild; a rebuild places it the
/// same and it goes on moving in place. (Skipped without a GPU with hardware RT.)
#[test]
fn driven_meshes_added_in_place_move_as_instances() {
let g = match gpu::Gpu::new() {
Ok(g) if g.hwrt => g,
other => return eprintln!("skipped: {}", other.map_or_else(|e| e.to_string(), |_| "no hardware RT".into())),
};
let mut w = World::default();
let mat = |r: f32| serde_json::from_value(serde_json::json!({ "name": "m", "color": [r, r, r], "opacity": 1, "metalness": 0, "roughness": 1, "emissive": [0, 0, 0], "emissiveIntensity": 0, "doubleSided": true })).unwrap();
w.set_material(1, mat(0.5));
let quad = |s: f32| Geometry { positions: vec![-s, -s, 0.0, s, -s, 0.0, s, s, 0.0, -s, s, 0.0], normals: vec![0.0, 0.0, 1.0].repeat(4), uvs: vec![0.0; 8], indices: vec![0, 1, 2, 0, 2, 3], groups: vec![], colors: vec![] };
w.set_geometry(2, quad(1.0)).unwrap();
w.set_geometry(3, quad(0.5)).unwrap();
let mesh = |geometry, material| Kind::Mesh { geometry, material, own_blas: true, materials: vec![] };
w.set_object(10, "a".into(), None, mesh(2, 1), true, false).unwrap();
let cam = serde_json::from_value(serde_json::json!({ "fov": 50, "near": 0.1, "far": 100, "exposure": { "mode": "manual", "ev100": 0 } })).unwrap();
w.set_object(4, "camera".into(), None, Kind::Camera(cam), true, false).unwrap();
w.set_transform(4, Vec3::new(0.0, 0.0, 5.0), glam::Quat::IDENTITY, Vec3::ONE).unwrap();
w.set_settings(crate::world::Settings { camera: Some(4), ..Default::default() });
let mut core = LiveCore::new(g, &w, Path::new("."), &crate::default_presets_dir(), 64, 36).unwrap();
w.take_changes();
let picked = |core: &LiveCore, u: f32| core.pick(u, 0.5).map(|p| p.0);
// The box and its material arrive together, as scene code adds them; its body is told of it right after.
let materials = core.sc.tables.materials.len();
w.set_material(5, mat(0.9));
w.set_object(11, "b".into(), None, mesh(3, 5), true, false).unwrap();
w.set_driven(11).unwrap();
w.set_transform(11, Vec3::new(0.0, 0.0, 1.0), glam::Quat::IDENTITY, Vec3::ONE).unwrap();
assert!(!core.sc.moving_instances, "nothing moves as an instance yet");
assert!(!core.commit(&mut w).unwrap(), "added in place, its material too");
assert_eq!(core.sc.tables.materials.len(), materials + 1);
assert!(core.sc.moving_instances, "a driven mesh: the frames write motion vectors from here");
assert_eq!(picked(&core, 0.5).as_deref(), Some("b"));
// Its body moves it 3 m to the side (u 0.95 of the image, 4 m from the camera): no rebuild, its record moved
// with the rows it had the frame before.
let record = core.sc.tables.retained.instance(11).unwrap().instance as usize;
assert!(core.sc.edit.in_room(record), "placed by an edit: a record of the room");
assert!(w.place_driven(11, Vec3::new(3.0, 0.0, 1.0), glam::Quat::IDENTITY));
assert!(!core.commit(&mut w).unwrap(), "moved as an instance");
assert_eq!(core.fallbacks, 0);
assert_eq!((picked(&core, 0.5).as_deref(), picked(&core, 0.95).as_deref()), (Some("a"), Some("b")));
let r = core.sc.gpu_instances[record];
assert_eq!((r.m[0][3], r.p[0][3]), (3.0, 0.0), "moved: last frame's rows kept");
// A rebuild places it as an instance too, and it moves on in place.
core.rebuild(&w).unwrap();
assert!(core.sc.tables.retained.instance(11).is_some_and(|i| i.moves && !core.sc.edit.in_room(i.instance as usize)), "placed by the build");
assert_eq!((picked(&core, 0.5).as_deref(), picked(&core, 0.95).as_deref()), (Some("a"), Some("b")));
assert!(w.place_driven(11, Vec3::new(0.0, 0.0, 1.0), glam::Quat::IDENTITY));
assert!(!core.commit(&mut w).unwrap(), "moved as an instance");
assert_eq!((picked(&core, 0.5).as_deref(), picked(&core, 0.95)), (Some("b"), None));
// Removed in place.
w.remove(11);
assert!(!core.commit(&mut w).unwrap(), "removed in place");
assert_eq!(picked(&core, 0.5).as_deref(), Some("a"));
assert!(!core.sc.moving_instances, "removed: nothing moves as an instance");
}
/// An instanced mesh's moved instances keep their previous-frame rows (motion vectors) through the commit that moves
/// them, and catch up at the next commit that does not move them. (Skipped without a GPU with hardware RT.)
#[test]
fn moved_instances_keep_their_motion() {
let g = match gpu::Gpu::new() {
Ok(g) if g.hwrt => g,
other => return eprintln!("skipped: {}", other.map_or_else(|e| e.to_string(), |_| "no hardware RT".into())),
};
let mut w = World::default();
let mat = |r: f32| serde_json::from_value(serde_json::json!({ "name": "m", "color": [r, r, r], "opacity": 1, "metalness": 0, "roughness": 1, "emissive": [0, 0, 0], "emissiveIntensity": 0, "doubleSided": false })).unwrap();
w.set_material(1, mat(0.5));
w.set_geometry(2, Geometry { positions: vec![0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0], normals: vec![0.0, 0.0, 1.0].repeat(3), uvs: vec![0.0; 6], indices: vec![0, 1, 2], groups: vec![], colors: vec![] }).unwrap();
w.set_object(3, "copies".into(), None, Kind::Instanced { geometry: 2, procedural: false, materials: vec![1], capacity: 2 }, true, false).unwrap();
let at = |x: f32| [Mat4::IDENTITY.to_cols_array(), Mat4::from_translation(Vec3::new(x, 0.0, 0.0)).to_cols_array()].concat();
w.set_instances(3, 2, Some(at(1.0)), None, None).unwrap();
let cam = serde_json::from_value(serde_json::json!({ "fov": 50, "near": 0.1, "far": 100, "exposure": { "mode": "manual", "ev100": 0 } })).unwrap();
w.set_object(4, "camera".into(), None, Kind::Camera(cam), true, false).unwrap();
w.set_transform(4, Vec3::new(0.0, 0.0, 5.0), glam::Quat::IDENTITY, Vec3::ONE).unwrap();
w.set_settings(crate::world::Settings { camera: Some(4), ..Default::default() });
let mut core = LiveCore::new(g, &w, Path::new("."), &crate::default_presets_dir(), 64, 36).unwrap();
w.take_changes();
let first = core.sc.tables.retained.instanced(3).unwrap().first as usize;
w.set_instances(3, 2, Some(at(2.0)), None, None).unwrap();
assert!(!core.commit(&mut w).unwrap(), "in place");
let r = core.sc.gpu_instances[first + 1];
assert_eq!((r.m[0][3], r.p[0][3]), (2.0, 1.0), "moved: last frame's rows kept");
w.set_material(1, mat(0.6));
core.commit(&mut w).unwrap();
let r = core.sc.gpu_instances[first + 1];
assert_eq!(r.p, r.m, "not moved since: caught up");
// Movable, moved and given new instances in one commit: updated once (twice would lose the motion).
w.set_object(3, "copies".into(), None, Kind::Instanced { geometry: 2, procedural: false, materials: vec![1], capacity: 2 }, true, true).unwrap();
assert!(core.commit(&mut w).unwrap(), "made movable: rebuilt");
let first = core.sc.tables.retained.instanced(3).unwrap().first as usize;
w.set_transform(3, Vec3::new(0.0, 1.0, 0.0), glam::Quat::IDENTITY, Vec3::ONE).unwrap();
w.set_instances(3, 2, Some(at(3.0)), None, None).unwrap();
let c = w.changes.clone();
assert!(c.moved.contains(&3) && c.instanced.contains(&3), "{c:?}");
assert!(!core.commit(&mut w).unwrap(), "in place");
let r = core.sc.gpu_instances[first + 1];
assert_eq!((r.m[0][3], r.m[1][3], r.p[0][3], r.p[1][3]), (3.0, 1.0, 2.0, 0.0), "moved: last frame's rows kept");
}
/// A wall, a small movable panel in front of it, and an emissive quad that lights both from the side; a camera.
fn lit_world() -> World {
let mut w = World::default();
let mat = |color: f32, emissive: f32| serde_json::from_value(serde_json::json!({ "name": "m", "color": [color, color * 0.5, color], "opacity": 1, "metalness": 0, "roughness": 1, "emissive": [1, 1, 1], "emissiveIntensity": emissive, "doubleSided": true })).unwrap();
w.set_material(1, mat(0.8, 0.0));
w.set_material(5, mat(0.0, 40.0));
let quad = |s: f32| Geometry { positions: vec![-s, -s, 0.0, s, -s, 0.0, s, s, 0.0, -s, s, 0.0], normals: [0.0, 0.0, 1.0].repeat(4), uvs: vec![0.0; 8], indices: vec![0, 1, 2, 0, 2, 3], groups: vec![], colors: vec![] };
w.set_geometry(2, quad(1.0)).unwrap();
w.set_geometry(3, quad(0.25)).unwrap();
let mesh = |geometry, material, own_blas| Kind::Mesh { geometry, material, own_blas, materials: vec![] };
// A wall, a small movable panel in front of it, and an emissive quad that lights both from the side.
w.set_object(10, "wall".into(), None, mesh(2, 1, false), true, false).unwrap();
w.set_object(11, "panel".into(), None, mesh(3, 1, true), true, true).unwrap();
w.set_transform(11, Vec3::new(-0.4, 0.0, 0.5), glam::Quat::IDENTITY, Vec3::ONE).unwrap();
w.set_object(12, "lamp".into(), None, mesh(3, 5, false), true, false).unwrap();
w.set_transform(12, Vec3::new(0.6, 0.3, 1.5), glam::Quat::from_rotation_y(2.6), Vec3::ONE).unwrap();
let cam = serde_json::from_value(serde_json::json!({ "fov": 50, "near": 0.1, "far": 100, "exposure": { "mode": "manual", "ev100": 0 } })).unwrap();
w.set_object(4, "camera".into(), None, Kind::Camera(cam), true, false).unwrap();
w.set_transform(4, Vec3::new(0.0, 0.0, 5.0), glam::Quat::IDENTITY, Vec3::ONE).unwrap();
w.set_settings(crate::world::Settings { camera: Some(4), ..Default::default() });
w
}
/// The tables read whole (`gpu::GpuOptions::table_views` off: what a device is opened with on Android), on this
/// machine's GPU: the scene renders offline to the views' image bit for bit, and so does a live frame after an
/// emissive object's power changed in place. (Skipped without a GPU, or where the layout is the merged one.)
#[test]
fn the_tables_read_whole_render_what_the_views_do() {
let open = |views: bool| gpu::Gpu::new_with(gpu::GpuOptions { table_views: Some(views), ..Default::default() });
let (views, whole) = match (open(true), open(false)) {
(Ok(a), Ok(b)) => (a, b),
(Err(e), _) | (_, Err(e)) => return eprintln!("skipped: {e}"),
};
if views.merged {
return eprintln!("skipped: the merged layout reads the tables whole already");
}
assert!(whole.whole_tables && !views.whole_tables);
let w = lit_world();
let presets = crate::default_presets_dir();
let offline = |g: &gpu::Gpu| {
let mut sc = crate::scene::build(&w, Path::new("."), &presets, None).unwrap();
(sc.width, sc.height) = (64, 36);
let prep = crate::prepare(g, &mut sc, &mut vec![]);
let frame = g.render(&sc, bytemuck::cast_slice(&prep.nodes), &prep.emitters, &prep.gpu_emitter_tris, &prep.emitter_runs, crate::ltree::nodes_of(prep.light_tree.as_ref()), &prep.env, &prep.env_cdf, &prep.energy, prep.params, 16, &mut vec![]).unwrap();
frame.color.iter().flatten().map(|v| v.to_bits()).collect::<Vec<u32>>()
};
let (a, b) = (offline(&views), offline(&whole));
let lit = a.iter().filter(|v| f32::from_bits(**v) > 0.0).count();
assert!(lit > a.len() / 16, "the scene is lit ({lit} of {} numbers)", a.len());
let differ = a.iter().zip(&b).filter(|(x, y)| x != y).count();
assert_eq!(differ, 0, "{differ} of {} numbers differ between the views and the tables read whole", a.len());
// Live, lockstep: two frames, the lamp dimmed in place between them.
let live = |g: gpu::Gpu, w: &mut World| {
let mut core = LiveCore::new(g, w, Path::new("."), &presets, 64, 36).unwrap();
core.fixed_dt = Some(1.0 / 30.0);
w.take_changes();
core.render(1, |_, _| {});
w.set_power(12, 0.25).unwrap();
assert!(!core.commit(w).unwrap(), "a power edit in place");
core.render(1, |_, _| {});
let _ = core.g.device.poll(wgpu::PollType::wait_indefinitely());
core.read_hdr().unwrap().iter().map(|v| v.to_bits()).collect::<Vec<u32>>()
};
let (mut w2, mut w3) = (lit_world(), lit_world());
let (x, y) = (live(views, &mut w2), live(whole, &mut w3));
assert!(x.iter().any(|v| *v != 0), "the live frame has a picture");
assert_eq!(x, y, "the live frames differ between the views and the tables read whole");
}
}