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.

Material function dispatch per call site: the alpha test and emitter sampling inline only the functions that change what they read

Evan (Yifeng) Wang09be03a764de
diff --git a/renderer/shaders/pathtrace.wgsl b/renderer/shaders/pathtrace.wgsl
index 639d2db..ddd8e9f 100644
--- a/renderer/shaders/pathtrace.wgsl
+++ b/renderer/shaders/pathtrace.wgsl
@@ -927,6 +927,8 @@ fn from_ue_vec(v: vec3<f32>) -> vec3<f32> { return vec3<f32>(v.x, v.z, v.y); }
// @material-functions-begin
fn material_function(f: i32, i: MatIn, m: Material, base: MatOut) -> MatOut { return base; }
fn translucent_material_function(f: i32, i: MatIn, m: Material, base: MatOut) -> MatOut { return base; }
+fn alpha_material_function(f: i32, i: MatIn, m: Material, base: MatOut) -> MatOut { return base; }
+fn emissive_material_function(f: i32, i: MatIn, m: Material, base: MatOut) -> MatOut { return base; }
// @material-functions-end
fn object_of(a: TriAttr) -> u32 { return bitcast<u32>(a.x.y); }
@@ -980,7 +982,7 @@ fn alpha_pass(ti: u32, inst: u32, u: f32, v: f32, t: f32) -> bool {
let i = mat_in_at(ti, u, v, vec3<f32>(0.0, 1.0, 0.0), log2(max(t * 2.0 * P.cam_pos.w / lod_height(), 1e-7)));
var b = mat_base(m);
b.opacity = alpha;
- alpha = material_function(m.tex1.w - 1, i, m, b).opacity;
+ alpha = alpha_material_function(m.tex1.w - 1, i, m, b).opacity;
}
return alpha >= m.uv2.w;
}
@@ -1641,7 +1643,7 @@ fn emitter_radiance(ti: u32, b1: f32, b2: f32, l: vec3<f32>) -> vec3<f32> {
if (has(F_FUNC) && m.tex1.w > 0) {
var mb = mat_base(m);
mb.emissive *= pw;
- le = material_function(m.tex1.w - 1, mat_in_at(ti, b1, b2, -l, log2(0.05)), m, mb).emissive;
+ le = emissive_material_function(m.tex1.w - 1, mat_in_at(ti, b1, b2, -l, log2(0.05)), m, mb).emissive;
}
return le;
}
@@ -1761,7 +1763,7 @@ fn sample_emitter(x: vec3<f32>, n: vec3<f32>) -> ES {
if (has(F_FUNC) && m.tex1.w > 0) {
var mb = mat_base(m);
mb.emissive *= pw;
- r.le = material_function(m.tex1.w - 1, mat_in_at(ti, b1, b2, -l, log2(0.05)), m, mb).emissive;
+ r.le = emissive_material_function(m.tex1.w - 1, mat_in_at(ti, b1, b2, -l, log2(0.05)), m, mb).emissive;
}
r.l = l;
r.dist = dist;
diff --git a/renderer/src/gpu.rs b/renderer/src/gpu.rs
index 5b632af..35034c5 100644
--- a/renderer/src/gpu.rs
+++ b/renderer/src/gpu.rs
@@ -637,6 +637,19 @@ pub fn vis_bind_group(d: &wgpu::Device, layout: &wgpu::BindGroupLayout, view: &w
})
}
+/// Whether a material function's WGSL (`fn mf_<name>(...) -> MatOut { var o = base; ... return o; }`) can change its
+/// result's `field`: it names `o.<field>`, or uses `o` otherwise than as `o.<another field>` (a whole assignment, a
+/// pointer passed on) beyond its declaration and its return.
+fn writes_field(code: &str, field: &str) -> bool {
+ if code.contains(&format!("o.{field}")) {
+ return true;
+ }
+ let body = code.replacen("var o = base;", "", 1).replace("return o;", "");
+ let b = body.as_bytes();
+ let ident = |c: u8| c.is_ascii_alphanumeric() || c == b'_';
+ (0..b.len()).any(|k| b[k] == b'o' && (k == 0 || !ident(b[k - 1])) && b.get(k + 1).is_none_or(|&c| !ident(c) && c != b'.'))
+}
+
/// What the path-tracing kernel is specialised on: the scene's feature mask and its material functions (WGSL).
#[derive(Clone)]
pub struct Kernel {
@@ -679,6 +692,19 @@ impl Kernel {
}
}
functions.push_str(" default { return base; }\n }\n}\n");
+ // The sites that read one field of the result (alpha tests: `opacity`; emitter sampling: `emissive`): only
+ // the functions that can change it; for the others the result's field is `base`'s. Each site inlines its
+ // dispatch, and a function's code costs the kernel there even where it never runs (wetLayer's puddles:
+ // ~0.8 ms on Tokyo, dry).
+ for (name, field) in [("alpha_material_function", "opacity"), ("emissive_material_function", "emissive")] {
+ functions.push_str(&format!("fn {name}(f: i32, i: MatIn, m: Material, base: MatOut) -> MatOut {{\n switch f {{\n"));
+ for (k, (id, code, _)) in scene.shaders.iter().enumerate() {
+ if writes_field(code, field) {
+ functions.push_str(&format!(" case {k} {{ return mf_{id}(i, m, base); }}\n"));
+ }
+ }
+ functions.push_str(" default { return base; }\n }\n}\n");
+ }
}
let mut volume_functions = String::new();
if !scene.volume_fns.is_empty() {
@@ -2410,6 +2436,19 @@ impl Gpu {
#[cfg(test)]
mod tests {
+ #[test]
+ fn material_functions_by_the_fields_they_write() {
+ let f = |body: &str| format!("fn mf_x(i: MatIn, m: Material, base: MatOut) -> MatOut {{\n var o = base;\n{body}\n return o;\n}}\n");
+ let wet = f(" o.base = base.base * 0.5;\n o.roughness = 0.1;\n o.normal = i.ng;");
+ assert!(!writes_field(&wet, "emissive") && !writes_field(&wet, "opacity"));
+ assert!(writes_field(&f(" o.emissive = vec3<f32>(1.0);"), "emissive"));
+ assert!(writes_field(&f(" o.opacity *= 0.5;"), "opacity"));
+ // A whole assignment or a pointer passed on may change any field.
+ assert!(writes_field(&f(" o = base;"), "emissive"));
+ assert!(writes_field(&f(" helper(&o);"), "opacity"));
+ assert!(!writes_field(&f(" let no = 1.0;\n o.base = vec3<f32>(no);"), "opacity"));
+ }
+
use super::*;
/// The kernel features are one bit each, no two the same (`override FEAT` gets them all, the source-patching ones