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.

The kernel's loops are bounded: wgpu's loop bounding on for it (#195)

Yujiadfd01ca9965e
diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md
index e45e91a..9712919 100644
--- a/docs/ROADMAP.md
+++ b/docs/ROADMAP.md
@@ -2776,6 +2776,20 @@ in-place tests' world, rows given back; two models in a shared pose posed again
(tests/render/data/arm.glb, a skinned strip of two materials); `geometry/emitter-model-live` (a model's emitting
section that is not its first, the model a unit under software traversal).
+**The kernel's loops are bounded (2026-11-23).** The kernel was the one shader compiled without wgpu's loop
+bounding (every loop given a count its compiler can see the end of); every other pass has it. Its loops end on
+sound data, which is no promise to a compiler that they end. Without the bound, the kernels Metal's compiler made
+lost the state of whole SIMD groups (8 × 4 pixels) on the home showcase, a scene with cutouts, one build and not the
+next. Nine builds of this kernel that differ by a statement that changes nothing (a counter, a
+branch never taken), under the loop guard: in six the guard stopped 14 to 43 invocations a run; with the bound, in
+none. Five builds of the next change's kernel (triangles as vertices and indices) drew 94,000 to 112,000 of the
+picture's 518,400 pixels wrong without the guard; with the bound each drew the right picture. Bounds checks and
+ray-query tracking, turned on alone, changed nothing and stay off. `THREERT_CHECKED=0` is the kernel as it was.
+What it costs: under hardware traversal a frame's GPU time in twelve scenes, alternated twice with the kernel before,
+is -1 % to +4 % (Zero Day +4.0, the neon street +3.8, the colour room +3.6, the crowd +2.9, the others under 2, Tokyo
+-1.0); under software traversal, whose tree walks are loops, the trace of a crowd of 3.3 M triangles is 85.3 -> 90.6 ms
+(+6 %). The pictures and the suites are the ones before.
+
**A triangle's texel density is the hit's to compute (2026-11-22).** Every triangle carried a constant for its mip
choice, half the log of its UV area over its area, made when the scene was built; an instance added an offset for
its scale, the skinning pass and the particle passes wrote it again each pose and frame. Now the kernel takes it
diff --git a/docs/RUNTIME.md b/docs/RUNTIME.md
index 1932368..5c8a296 100644
--- a/docs/RUNTIME.md
+++ b/docs/RUNTIME.md
@@ -1916,8 +1916,9 @@ on the device", and differs where it says so.
| `THREERT_LIGHT_PICK` | 0 | past the camera hit, the directional lights and one of the other kinds (local lights, emitters, the sky), weighted by their number: two shadow rays a vertex instead of four; unbiased, noisier | a handheld's host sets it with `THREERT_LAST_DIRECT` |
| `THREERT_LIGHT_TILES` | Android: 0 | a scene with more than `RIS_EXACT` shadow-casting local lights has no light tiles (`F_LISTS`, #87): its lights are resampled unguided, as before the tiles (not a flag of the source: the scene's feature is left off, gpu.rs `light_tiles_on`) | OpenStrikeRT's Foundry, 249 × 140, one bounce, the tables read whole: 48 ms with the tiles, 31 ms without (30 fps, the 0.4 clients' rate); `THREERT_LIGHT_PICK=1` instead: 38 ms |
- `THREERT_CHECKED=1` (or a mask: 2 bounds, 4 loop bounding, 8 ray-query initialization) compiles the kernel with
- naga's runtime checks, to tell a driver's fault from the kernel's.
+ The kernel is compiled with wgpu's loop bounding and no other runtime check (ROADMAP § 1a, "The kernel's loops
+ are bounded"). `THREERT_CHECKED` = 0 (none), 1 (every check) or a mask (2 bounds, 4 loop bounding, 8 ray-query
+ initialization) tells a driver's fault from the kernel's.
- **The pipeline cache** (`GpuOptions::pipeline_cache`, `THREERT_PIPELINE_CACHE=<directory>`; `pipeline_cache.rs`).
The driver compiles a kernel variant for about ten seconds. With a directory given, the device has a wgpu
`PipelineCache` (Vulkan only), every compute pipeline of the engine and the raster pass's are made with it, and it
diff --git a/renderer/src/gpu.rs b/renderer/src/gpu.rs
index 9ed13b5..f2ce09a 100644
--- a/renderer/src/gpu.rs
+++ b/renderer/src/gpu.rs
@@ -1419,11 +1419,17 @@ impl KernelSwitches {
}
}
-/// The kernel's runtime checks: none (all indices come from our own BVH / scene builder). THREERT_CHECKED turns naga's
-/// on to diagnose a driver: 1 = every check, else a mask (2 bounds, 4 loop bounding, 8 ray-query initialization
-/// tracking).
+/// The kernel's runtime checks: loop bounding alone, wgpu's default for it (every loop has a count its compiler can
+/// see the end of). The kernel's loops end on sound data, which does not promise a compiler that they end; without
+/// the bound, kernels Metal's compiler made lost the state of whole SIMD groups on a scene with cutouts, one build and
+/// not the next. No bounds checks: all indices come from our own BVH / scene builder. THREERT_CHECKED, to diagnose a
+/// driver: 0 = none, 1 = every check, else a mask (2 bounds, 4 loop bounding, 8 ray-query initialization tracking).
fn kernel_checks() -> wgpu::ShaderRuntimeChecks {
- let v: u32 = std::env::var("THREERT_CHECKED").ok().and_then(|v| v.parse().ok()).unwrap_or(0);
+ kernel_checks_of(std::env::var("THREERT_CHECKED").ok().and_then(|v| v.parse().ok()))
+}
+
+fn kernel_checks_of(asked: Option<u32>) -> wgpu::ShaderRuntimeChecks {
+ let v = asked.unwrap_or(4);
if v == 1 {
return wgpu::ShaderRuntimeChecks::checked();
}
@@ -4267,6 +4273,13 @@ mod tests {
wesl_wgsl(include_str!("../shaders/skin.wgsl"), &[], &[]);
}
+ /// The kernel is compiled with its loops bounded and nothing else checked, unless asked.
+ #[test]
+ fn the_kernels_loops_are_bounded() {
+ let of = |v| { let c = kernel_checks_of(v); (c.force_loop_bounding, c.bounds_checks, c.ray_query_initialization_tracking) };
+ assert_eq!([of(None), of(Some(0)), of(Some(1)), of(Some(2)), of(Some(12))], [(true, false, false), (false, false, false), (true, true, true), (false, true, false), (true, false, true)]);
+ }
+
/// The skinning pass is valid WGSL, and its block size and dispatch row are the ones the CPU lays its work out by.
#[test]
fn the_skinning_pass_is_valid_and_its_sizes_are_the_cpus() {