Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

perf(render): a Voxel GI ray asks a small table whether a cell holds anything and wraps it with a mask, so a default frame costs a third of what it did

yhha4bb20378a3d
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e3b33990e..31e1eb0db 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -23,6 +23,7 @@ published separately; it ships inside the editor.
### Fixed
+- **Voxel GI costs a third of what it did.** Every cell a ray crossed took two integer divisions, a hash, and a read in the 32 MB grid, which is a cache miss, for a cell that is nearly always empty. Wrapping is now a mask, the hash check is gone (the cache re-tags every slot each frame, so it could not fail), and the tags hold a mark saying whether a slot has anything, read before the grid is. On an RTX 3060 at 720p the default settings went from 17.3 to about 5.4 ms a frame, and the picture is the same: held against the old kernels, the difference is no larger than between two runs of one build.
- **Writing an entity list on the web replaces it.** A `SpringBone.colliders` or `MeshSkin.joints` written to a component that already had one was added to what was there, so a `[a, b]` written twice read `[a, b, a, b]`, a `Mut` row grew it on every write-back (72 entries in a few frames) and writing `[]` cleared nothing. Number lists and native builds were right.
- **A spring bone's colliders work in a native build.** Entity lists were left out of the native bindings unless written by hand, and only `MeshSkin` and `Children` were. `SpringBone.colliders` from a scene never reached the engine on a device, with no error: hair and tails passed through what they should have been kept out of, and a script reading the field got `undefined`. Every entity list now crosses as a generated pair, as other lists do.
- **`Mut()` on the web no longer turns a component's colours to NaN** (#107). Since 0.98.0 a `Mut` row is written back when the loop moves on and once more when the system returns, and the first write rewrote the row's colour into the engine's shape, so the second sent NaN. A loop over `Mut(UIVisual)` that wrote nothing made the element vanish. Components read through the wasm heap, and every native build, were not affected. Writing back no longer touches the row. The same write also handed a list field to the engine as a JS array, so `Mut` on a component with one threw every frame; it now takes the one marshalled write `world.set` and `insert` use.
diff --git a/src/esengine/renderer/frame/VoxelGI.cpp b/src/esengine/renderer/frame/VoxelGI.cpp
index 350ba9477..e1d80b5fa 100644
--- a/src/esengine/renderer/frame/VoxelGI.cpp
+++ b/src/esengine/renderer/frame/VoxelGI.cpp
@@ -55,16 +55,21 @@ fn addArea(base : u32, area : f32) {
// Set on a slot's tag by the inject when the picture showed its cell this frame, and cleared by the maintain each frame.
const SEEN : u32 = 2u;
+// Set on a slot's tag while it holds anything. What a ray asks of every cell it crosses, most of them empty: the tags are an eighth of the
+// grid, so the answer is mostly in cache, where the grid's own count is a miss.
+const HELD : u32 = 4u;
+const MARKS : u32 = SEEN | HELD;
fn hash3(c : vec3i) -> u32 {
var h = (u32(c.x) * 73856093u) ^ (u32(c.y) * 19349663u) ^ (u32(c.z) * 83492791u);
h = (h ^ (h >> 16u)) * 0x45d9f3bu;
h = h ^ (h >> 16u);
- // Bit 0 always set (no live cell hashes to 0, which an empty slot holds); bit 1 is not part of the hash, but the mark SEEN.
- return (h | 1u) & ~SEEN;
+ // Bit 0 always set (no live cell hashes to 0, which an empty slot holds); the marks are not part of the hash.
+ return (h | 1u) & ~MARKS;
}
fn wrapIndex(cell : vec3i) -> u32 {
- let d = vec3i(gi.dims.xyz);
- let w = ((cell % d) + d) % d;
+ // The dims are powers of two (VoxelGI.hpp), so a mask wraps a negative cell too, and costs nothing where a modulo is an integer
+ // division: a ray takes this every step.
+ let w = cell & (vec3i(gi.dims.xyz) - vec3i(1));
return u32(w.x) + u32(w.y) * gi.dims.x + u32(w.z) * gi.dims.x * gi.dims.y;
}
fn inWindow(cell : vec3i) -> bool {
@@ -101,22 +106,24 @@ fn main(@builtin(global_invocation_id) id : vec3u) {
let total = gi.dims.x * gi.dims.y * gi.dims.z;
let i = id.x;
if (i >= total) { return; }
- let g = vec3i(i32(i % gi.dims.x), i32((i / gi.dims.x) % gi.dims.y), i32(i / (gi.dims.x * gi.dims.y)));
+ let g = vec3i(i32(i & (gi.dims.x - 1u)), i32((i >> countTrailingZeros(gi.dims.x)) & (gi.dims.y - 1u)),
+ i32(i >> countTrailingZeros(gi.dims.x * gi.dims.y)));
let d = vec3i(gi.dims.xyz);
// The cell this slot stands for in the window as it is now: toroidal, so the window slides over the same memory.
- let rel = ((g - gi.origin.xyz) % d + d) % d;
+ let rel = (g - gi.origin.xyz) & (d - vec3i(1));
let cell = gi.origin.xyz + rel;
let tag = hash3(cell);
let base = i * WORDS;
// Each invocation owns its slot, so the words are read and written plainly (readWriteCache): ageing a million slots through atomics
// every frame was most of what the cache cost a phone. An empty slot (no samples, no area) has nothing to age and is left alone.
- if ((tags[i] & ~SEEN) != tag) {
+ if ((tags[i] & ~MARKS) != tag) {
tags[i] = tag;
for (var k = 0u; k < WORDS; k = k + 1u) { grid[base + k] = 0u; }
} else {
tags[i] = tag;
if ((grid[base] | grid[base + 1u] | grid[base + 2u] | grid[base + 3u] | grid[base + 7u]) == 0u) { return; }
for (var k = 0u; k < WORDS; k = k + 1u) { grid[base + k] = u32(f32(grid[base + k]) * gi.a.y); }
+ if (grid[base + 3u] != 0u) { tags[i] = tag | HELD; }
}
}
)WGSL";
@@ -160,7 +167,7 @@ fn main(@builtin(global_invocation_id) id : vec3u) {
}
addArea(base, footprint);
// Every writer writes the same value, so the race is harmless.
- tags[wrapIndex(cell)] = hash3(cell) | SEEN;
+ tags[wrapIndex(cell)] = hash3(cell) | MARKS;
var lit = textureLoad(scene, t, 0).rgb;
let albedo = textureLoad(albedoTex, t, 0);
if (gi.view.y > 0.5 && albedo.a > 0.5) {
@@ -238,6 +245,7 @@ fn main(@builtin(global_invocation_id) id : vec3u) {
// Where the picture has shown this voxel (the inject ran first this frame), its own colour is the better answer than an assumed
// albedo: the sun's estimate is what stands in only where the camera has not looked.
if ((tags[wrapIndex(cell)] & SEEN) != 0u) { return; }
+ tags[wrapIndex(cell)] = hash3(cell) | HELD;
addArea(base, sqrt(area));
let c = clamp(sun.light.rgb * (sun.light.w * max(dot(nrm, L), 0.0)), vec3f(0.0), vec3f(gi.a.z));
atomicAdd(&grid[base], u32(c.r * FIXED));
@@ -296,9 +304,10 @@ fn trace(origin : vec3f, dir : vec3f) -> vec3f {
cell.z = cell.z + stepv.z; t = tMax.z; tMax.z = tMax.z + delta.z;
}
if (!inWindow(cell)) { break; }
- let idx = wrapIndex(cell);
- if ((tags[idx] & ~SEEN) != hash3(cell)) { continue; }
- let base = idx * WORDS;
+ // A slot in the window is that cell's: the maintain re-tagged every slot this frame, and the window was just asked.
+ let slot = wrapIndex(cell);
+ if ((tags[slot] & HELD) == 0u) { continue; }
+ let base = slot * WORDS;
let count = atomicLoad(&grid[base + 3u]);
if (count < 2u * COUNT) { continue; }
let inv = f32(COUNT) / f32(count);
diff --git a/src/esengine/renderer/frame/VoxelGI.hpp b/src/esengine/renderer/frame/VoxelGI.hpp
index 941fe4832..11b594b8f 100644
--- a/src/esengine/renderer/frame/VoxelGI.hpp
+++ b/src/esengine/renderer/frame/VoxelGI.hpp
@@ -92,6 +92,8 @@ public:
static constexpr u32 kDimsX = 128;
static constexpr u32 kDimsY = 64;
static constexpr u32 kDimsZ = 128;
+ static_assert((kDimsX & (kDimsX - 1)) == 0 && (kDimsY & (kDimsY - 1)) == 0 && (kDimsZ & (kDimsZ - 1)) == 0,
+ "the kernels wrap a cell into the grid with a mask");
private:
struct Programs;
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index daa19561a..503e2b1db 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -3850,4 +3850,6 @@ export const NOTED = {
{"note":"Writing an entity list on the web replaces it"},
"fix(native): a component's entity list reaches the native core, so a spring bone's colliders work in a device build and a script can read them":
{"note":"A spring bone's colliders work in a native build"},
+ "perf(render): a Voxel GI ray asks a small table whether a cell holds anything and wraps it with a mask, so a default frame costs a third of what it did":
+ {"note":"Voxel GI costs a third of what it did."},
};
diff --git a/tools/render-lab/README.md b/tools/render-lab/README.md
index 2c25b6893..b848c1ea1 100644
--- a/tools/render-lab/README.md
+++ b/tools/render-lab/README.md
@@ -120,3 +120,17 @@ voxel:
The voxel pass went from 1.11 to 2.45 ms (frame 4.4 → 5.1 ms). There is no coverage term here (the atlas has no spare channel), so
coarse grids still overshoot regionally: the ceiling is 1.4 at 40 cm. One headless run of 12 cm read 0.51. A rerun twice read 0.80,
so take a single low reading as a capture timed before the grid filled.
+
+### Cost of a voxel step (2026-11-05, WebGPU)
+
+Windows host, hidden, RTX 3060, 720p, uncapped (`ESTELLA_BENCH_FRAMES`), scene `gi_room` (0.77 ms with GI off):
+
+| Change | Default frame | One ray per point |
+| --- | --- | --- |
+| Before | 17.3 ms | 3.5 ms |
+| Masks for the modulos, no per-step hash check | 11.5 ms | 2.7 ms |
+| A held mark in the tags, read before the grid | 5.2 ms | 2.1 ms |
+
+A two-level walk, skipping an empty 4x4x4 block in one step, was tried on top and measured nothing, in this room, with its walls removed, and at 12 cm: with the mark, a cell is already nearly free. It was taken out. About 1 ms of what is left does not depend on rays (the upkeep, the inject, the apply), and each ray costs about 0.4 ms.
+
+Held against the old kernels with this lab, the light share and the pixels match within the noise of two runs of one build (0.90 at the default size, 0.70 at 12 cm). Those are lower than the 0.95 and 0.74 recorded above, and that came with an earlier commit, not with this.