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
MetalFX's joint denoiser takes its input at 1/32 of display white, as its upscaler: given lighting near white it bent bright emitters and averaged highlights down (Zero Day 0.71 -> 0.91 of the offline reference's luminance, its orange LEDs yellow -> orange) (#193)
Evan (Yifeng) Wangdbc938e68970
diff --git a/renderer/shaders/live.wgsl b/renderer/shaders/live.wgslindex 0e60c86..d985ad8 100644--- a/renderer/shaders/live.wgsl+++ b/renderer/shaders/live.wgsl@@ -18,7 +18,7 @@ struct LiveParams {size: vec4<u32>, // width, height, frame, reset history (1)misc: vec4<f32>, // exposure scale, max history, vignette intensity, bloom on (1) / off (0)misc2: vec4<f32>, // denoise strength, sample clamp (display units at the current exposure), -, samples this frame- mode: vec4<u32>, // view mode (0 lit, 1 base color, 2 world normal, 3 lit no denoise, 4 raw sample, 5 history), -, the scene's lighting is changing (live/mod.rs `live_lighting`), -+ mode: vec4<u32>, // view mode (0 lit, 1 base color, 2 world normal, 3 lit no denoise, 4 raw sample, 5 history), display whites a unit of a reconstruction provider's input is (reconstruction.wgsl `rc_radiance_unit`), the scene's lighting is changing (live/mod.rs `live_lighting`), -post: vec4<f32>, // chromatic aberration (UE scene fringe), film grain, bloom threshold (<= 0 off), -ae: vec4<f32>, // auto exposure: low / high fraction, histogram log2 min / maxae2: vec4<f32>, // compensation (2^bias; manual: the exposure), min / max average luminance, mode (1 = auto)diff --git a/renderer/shaders/reconstruction.wgsl b/renderer/shaders/reconstruction.wgslindex a347e5e..d16d163 100644--- a/renderer/shaders/reconstruction.wgsl+++ b/renderer/shaders/reconstruction.wgsl@@ -12,11 +12,11 @@ struct RcInputs {@location(2) motion: vec2<f32>,@location(3) reactive: f32,};-// A fixed radiometric unit between exposure changes. The post chain receives scene radiance again.+// A fixed radiometric unit between exposure changes: the display whites a unit is, as the active provider takes it+// (`L.mode.y`: live/reconstruction.rs `white_units`, which `encode` passes as `radiance_unit`), over the exposure.+// The post chain receives scene radiance again.fn rc_radiance_unit() -> f32 {- // A joint denoiser takes lighting near display white (its overlay keeps much brighter emitters); an upscaler- // the whole lit frame in a wider linear range. (live/reconstruction.rs `encode` passes the same unit.)- return select(32.0, 1.0, L.mode.w == 1u) / clamp(L.misc.x, 0.00001, 64.0);+ return f32(max(L.mode.y, 1u)) / clamp(L.misc.x, 0.00001, 64.0);}fn pack_rc_inputs(p: vec4<f32>) -> RcInputs {let q = u32(p.y) * L.isz.x + u32(p.x);diff --git a/renderer/src/live/frame.rs b/renderer/src/live/frame.rsindex bdb7597..f9dc9fa 100644--- a/renderer/src/live/frame.rs+++ b/renderer/src/live/frame.rs@@ -541,7 +541,7 @@ impl LiveCore {// UpdateMotionBlurTimeScale) blurs over +- amount / 2 x the motion x (1 / target FPS) / frame time, the// actual frame time without a target. 0 while nothing moves (the pass then costs nothing).misc2: [1.0, SAMPLE_CEILING, if !blur { 0.0 } else if self.blur.1 > 0.0 { self.blur.0 / (self.blur.1 * self.frame_dt.clamp(1.0 / 240.0, 0.25)) } else { self.blur.0 }, spp.max(1) as f32],- mode: [self.view_mode, 0, self.live_lighting() as u32, reconstruction.raw_signal() as u32],+ mode: [self.view_mode, self.reconstructor.white_units() as u32, self.live_lighting() as u32, reconstruction.raw_signal() as u32],post: self.post,ae: self.view.exposure_settings.ae,ae2: self.view.exposure_settings.ae2,diff --git a/renderer/src/live/inplace_tests.rs b/renderer/src/live/inplace_tests.rsindex f6bba82..d50b9be 100644--- a/renderer/src/live/inplace_tests.rs+++ b/renderer/src/live/inplace_tests.rs@@ -3393,6 +3393,50 @@ fn reconstruction_keeps_hdr_emitters_and_resets_on_a_cut() {}}+/// A bright emitter keeps its level and hue through MetalFX, as through the built-in path, from 1800 to 120 000 nits+/// (at EV 6: 28 to 1900 times display white). The denoised scaler given lighting near display white kept them only to+/// ~30x: Zero Day's orange LEDs (~125x) came out yellow and its frame at 0.68 of the offline reference's luminance; at+/// 20 000 nits this read 0.82 of the level and green / red 0.29, at 120 000, 0.22 and 0.71 (metalfx.rs `white_units`).+#[test]+fn metalfx_keeps_bright_emitters_level_and_hue() {+ #[cfg(metalfx)]+ let methods = [Method::MetalFx, Method::MetalFxDenoised];+ #[cfg(not(metalfx))]+ let methods: [Method; 0] = [];+ for nits in [1800.0f32, 20000.0, 120000.0] {+ let Ok(gpu) = gpu::Gpu::new() else { return };+ let mut w = world();+ w.set_material(5, serde_json::from_value(serde_json::json!({+ "name": "HDR panel", "color": [0, 0, 0], "opacity": 1, "metalness": 0, "roughness": 1,+ "emissive": [1, 0.25, 0.5], "emissiveIntensity": nits, "doubleSided": true,+ })).unwrap());+ w.set_transform(12, Vec3::new(0.0, 0.0, 1.0), glam::Quat::IDENTITY, Vec3::new(5.0, 0.5, 1.0)).unwrap();+ w.set_camera_desc(4, camera(50.0, None, manual(6.0))).unwrap();+ let mut core = LiveCore::new(gpu, &w, Path::new("."), &crate::default_presets_dir(), 320, 180).unwrap();+ for method in methods.clone() {+ core.set_scale(0.5);+ core.set_reconstruction(method.clone());+ for _ in 0..48 { core.render(1, |_, _| {}); }+ if core.reconstruction() == Method::Builtin {+ eprintln!("{} unsupported on this device: {:?}", method.name(), core.reconstruction_status().skipped);+ continue;+ }+ // The panel's middle (a block: one pixel's upscaling does not decide it).+ let hdr = core.read_hdr().unwrap();+ let mut c = [0.0f32; 3];+ for y in core.h / 2 - 5..core.h / 2 + 5 {+ for x in core.w / 2 - 10..core.w / 2 + 10 {+ let p = ((y * core.w + x) * 3) as usize;+ for k in 0..3 { c[k] += hdr[p + k] / 200.0; }+ }+ }+ let what = format!("{} at {nits} nits: {c:?}", method.name());+ assert!((c[0] / nits - 1.0).abs() < 0.05, "{what}: level");+ assert!((c[1] / c[0] / 0.25 - 1.0).abs() < 0.03 && (c[2] / c[0] / 0.5 - 1.0).abs() < 0.03, "{what}: hue");+ }+ }+}+/// A request is a preference list: the first method this device runs is used, and the status says why each one/// before it did not. A typed quality preset is the vendors' exact ratio.#[test]diff --git a/renderer/src/live/metalfx.rs b/renderer/src/live/metalfx.rsindex dec04ac..e3fa35a 100644--- a/renderer/src/live/metalfx.rs+++ b/renderer/src/live/metalfx.rs@@ -141,4 +141,10 @@ impl JointProvider for MetalFx {fn untimed_gpu_ms(&self) -> f32 { if self.scaler.is_some() { unsafe { threert_fx_gpu_ms() } } else { 0.0 } }fn scale_range(&self) -> Option<[f32; 2]> { self.scales }fn invalidate(&mut self) { self.scaler = None; self.key = None; }+ /// The denoised scaler is not linear in its input's level. Given lighting near display white it averaged the+ /// highlights down and bent bright emitters (M3 Max, macOS 26.6: Zero Day at 0.68 of the offline reference's+ /// luminance, its orange LEDs yellow and dull; an emitter at 300x white lost a fifth of its red, the hue drifting+ /// to yellow, and at 1900x came out clipped flat). Given it at 1/32 of white, as the upscaler: 0.999, the LEDs+ /// orange, the emitter's level and hue within 1 % to 1900x.+ fn white_units(&self) -> f32 { 32.0 }}diff --git a/renderer/src/live/reconstruction.rs b/renderer/src/live/reconstruction.rsindex dcc897c..5bbacf8 100644--- a/renderer/src/live/reconstruction.rs+++ b/renderer/src/live/reconstruction.rs@@ -256,6 +256,9 @@ pub trait JointProvider: Send {/// As [`UpscaleProvider::scale_range`].fn scale_range(&self) -> Option<[f32; 2]> { None }fn invalidate(&mut self) {}+ /// Display whites a unit of `color` and of the overlay is (`radiance_unit` = this / exposure, in nits per unit):+ /// 1 = lighting near display white, the scale a denoiser is commonly trained on. Upscalers take 32.+ fn white_units(&self) -> f32 { 1.0 }}enum Provider {@@ -271,6 +274,9 @@ impl Provider {fn untimed_gpu_ms(&self) -> f32 {match self { Self::Upscale(p) => p.untimed_gpu_ms(), Self::Joint(p) => p.untimed_gpu_ms() }}+ fn white_units(&self) -> f32 {+ match self { Self::Upscale(_) => 32.0, Self::Joint(p) => p.white_units() }+ }fn invalidate(&mut self) {match self { Self::Upscale(p) => p.invalidate(), Self::Joint(p) => p.invalidate() }}@@ -547,6 +553,15 @@ impl Reconstructor {}}+ /// Display whites a unit of the active provider's input is (its `radiance_unit` = this / exposure); the shader's+ /// `rc_radiance_unit` takes the same (`L.mode.y`).+ pub(super) fn white_units(&self) -> f32 {+ match self.active {+ Active::Upscaler(i) | Active::Joint(i) => self.providers[i].1.white_units(),+ _ => 32.0,+ }+ }+/// GPU time (ms) the active provider spends outside the frame timer's passes.pub(super) fn untimed_gpu_ms(&self) -> f32 {match self.active {@@ -576,7 +591,7 @@ impl Reconstructor {let textures = self.textures.as_mut().expect("prepared reconstruction textures");let pipes = self.pipes.as_ref().unwrap();textures.pack(e, pipes, size.input);- let unit = if textures.key.0 { 1.0 } else { 32.0 } / exposure.clamp(0.00001, 64.0);+ let unit = self.providers[i].1.white_units() / exposure.clamp(0.00001, 64.0);let world_to_view = glam::Mat4::look_to_rh(cam.pos, cam.fwd, cam.up).to_cols_array();let view_to_clip = glam::Mat4::perspective_infinite_reverse_rh(2.0 * cam.tan_half_fov.atan(), aspect, 0.01).to_cols_array();let [color, depth, motion, reactive, ..] = &textures.inputs[..] else { unreachable!() };