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(gi): dynamic GI 8.9 -> 3.3 ms at 1080p - a card relit by a workgroup of 64 rays, cards relit in slices, probes by stride and a fixed count, the nearest instance recorded per field cell; frame timing tool

yhhcb83120408cd
diff --git a/docs/GI_LUMEN_DESIGN.md b/docs/GI_LUMEN_DESIGN.md
index 0ac70961d..9ec3efc32 100644
--- a/docs/GI_LUMEN_DESIGN.md
+++ b/docs/GI_LUMEN_DESIGN.md
@@ -105,6 +105,27 @@
- **手机成本**:现有体素聚集在手机(Adreno/Vulkan)上曾因聚集里的 atomic 读耗时 1 秒以上,后改为只读数组(见 `VoxelGI.cpp` 注释)。新方案所有读都用只读数组;探针数量和光线数按 `QualityConfig.power` 缩放;
目标:桌面 1080p ≤ 3 ms,手机 720p ≤ 6 ms(未验证,第 6 节每期实测后再改)。
+## 5.1 帧时间实测(2026-11-11)
+
+`node tools/gi/measure-gi-frame.mjs <web 目录>`:无头 Chromium 跑在本机 GPU(NVIDIA Ampere 架构)上,用设备的 `timestamp-query` 给每个 compute 通道打时间戳,取 200 帧的中位数。场景是 lab 的房间(10 个盒子,约 4 万个 card 像素)。
+
+| 阶段 | 起点(1280×720) | 现在 |
+|---|---|---|
+| 卡面光照 | 4.3 ms | 0.18 ms |
+| 探针 | 4.2 ms | 2.2–2.7 ms |
+| 应用 | 0.39 ms | 0.4 ms(720p)/ 0.9 ms(1080p) |
+| 合计 | 8.9 ms | 2.8–3.5 ms(1080p 3.3 ms) |
+
+起点到现在的改动,按收益排:
+- **卡面一个工作组一个像素、64 个线程各追一条射线**(共享内存规约),原来一个线程串行追 64 条,GPU 在等自己那条读取链:4.3→0.18 ms,是最大的一项。只要线程串行追射线,削减要更新的像素数几乎不省时间(16 倍分片仍是 3 ms)。
+- **卡面分片**:每帧只重新点亮 1/4,混合系数相应放大。
+- **距离场里每个格子记下最近的实例**(7 位 id,4 个一个字),命中点只查那一个实例的 card,不再遍历全部:每个通道省 0.9–1.5 ms,实例越多越值。
+- **探针 2 条里追 1 条,交替**,历史补另一半;**探针数固定约 160×90**,间距随画面高度走(720p 为 8 像素,1080p 为 12),探针成本不随分辨率涨。
+- 远离表面时用一次读取代替三线性的 8 次读取:收益很小。
+- 精度没变:`gi_oracle.py` 对 Cycles 的占比与改前一致。
+
+没测的:手机(Adreno/Mali),更大的场景(实例多、距离场更大),以及更高分辨率下应用通道的 0.9 ms 往上的增长。
+
## 6. 分期
每期独立可验收、可回退,期间旧 `voxelGI` 一直可用。
diff --git a/pipeline/tests/gpu-scene.test.ts b/pipeline/tests/gpu-scene.test.ts
index 11af959a4..2829fd915 100644
--- a/pipeline/tests/gpu-scene.test.ts
+++ b/pipeline/tests/gpu-scene.test.ts
@@ -86,7 +86,7 @@ describe('the packed scene', () => {
expect(scene.header.length).toBe(GI_HEADER_FLOATS);
const [dim, cell, , , , reach, count, pixels] = scene.header;
expect(dim).toBe(128);
- expect(scene.sdf.length).toBe(128 ** 3);
+ expect(scene.sdf.length).toBe(128 ** 3 * 1.25); // the field, then the nearest instance of each cell, four to a word
expect(reach).toBeCloseTo(cell! * 6, 4);
expect(count).toBe(2);
expect(scene.instances.length).toBe(2 * GI_INSTANCE_FLOATS);
diff --git a/sdk/src/gi/globalSdf.ts b/sdk/src/gi/globalSdf.ts
index 4d970e214..2b3d0853a 100644
--- a/sdk/src/gi/globalSdf.ts
+++ b/sdk/src/gi/globalSdf.ts
@@ -46,6 +46,8 @@ interface Level {
/** The window's first cell, in whole cells of the level. */
origin: [number, number, number];
data: Float32Array;
+ /** Per cell, 1 + the id of the instance nearest to it (127 for an id past 125), 0 where nothing is within reach. */
+ owner: Uint8Array;
}
export class GlobalSdf {
@@ -57,7 +59,7 @@ export class GlobalSdf {
constructor(levels: readonly GlobalSdfLevel[]) {
this.levels = levels.map((l) => {
if ((l.dim & (l.dim - 1)) !== 0 || l.dim < 2) throw new Error(`a clipmap level is a power of two cells across, got ${l.dim}`);
- const level: Level = { dim: l.dim, cell: l.cell, origin: [0, 0, 0], data: new Float32Array(l.dim ** 3) };
+ const level: Level = { dim: l.dim, cell: l.cell, origin: [0, 0, 0], data: new Float32Array(l.dim ** 3), owner: new Uint8Array(l.dim ** 3) };
level.data.fill(l.cell * GlobalSdf.REACH_CELLS);
return level;
});
@@ -207,9 +209,14 @@ export class GlobalSdf {
const mask = level.dim - 1;
for (let z = a[2]!; z < b[2]!; z++) for (let y = a[1]!; y < b[1]!; y++) for (let x = a[0]!; x < b[0]!; x++) {
const p: [number, number, number] = [(x + 0.5) * level.cell, (y + 0.5) * level.cell, (z + 0.5) * level.cell];
- let d = reach;
- for (const placed of near) d = Math.min(d, this.distanceTo_(placed, p));
- level.data[(x & mask) + (y & mask) * level.dim + (z & mask) * level.dim * level.dim] = Math.max(d, -reach);
+ let d = reach, owner = 0;
+ for (const placed of near) {
+ const v = this.distanceTo_(placed, p);
+ if (v < d) { d = v; owner = placed.instance.id < 126 ? placed.instance.id + 1 : 127; }
+ }
+ const at = (x & mask) + (y & mask) * level.dim + (z & mask) * level.dim * level.dim;
+ level.data[at] = Math.max(d, -reach);
+ level.owner[at] = owner;
}
}
diff --git a/sdk/src/gi/gpuScene.ts b/sdk/src/gi/gpuScene.ts
index 03ed20715..c68693c78 100644
--- a/sdk/src/gi/gpuScene.ts
+++ b/sdk/src/gi/gpuScene.ts
@@ -8,7 +8,7 @@
*
* header (floats) dim, cell, origin x y z (cells), reach, instances, card pixels, sun toLight x y z, sun on, sun radiance r g b,
* ray bias, ray range, card tolerance, sky r g b, hit fraction
- * sdf dim^3 floats, toroidal like GlobalSdf's level
+ * sdf dim^3 floats, toroidal like GlobalSdf's level, then dim^3 / 4 words holding the id (+ 1) of the instance nearest each cell
* instance (20 vec4 = 80 floats) 0-3 local to world (column major) | 4-6 world to local rows, w the translation, scale divided out |
* 7 world box min, w scale | 8 world box max, w card pixel size | 9 local box min, w emission scale | 10 local box max |
* 11-16 the six faces (width, height, first pixel) | 17-19 spare
@@ -142,5 +142,10 @@ export function packGPUScene(instances: readonly GIInstance[], light: GILight |
set(0, dim, cell, level.origin[0]!, level.origin[1]!, level.origin[2]!, cell * GlobalSdf.REACH_CELLS, instances.length, pixelTotal);
set(8, ...(light?.toLight ?? [0, 1, 0]), light ? 1 : 0, ...(light?.radiance ?? [0, 0, 0]), bias, extent * 2.5, cell * 3);
set(18, ...(options.sky ?? [0, 0, 0]), 0.25);
- return { header, sdf: level.data, instances: instanceData, pixels };
+ // The field, then which instance each cell is nearest to, four cells to a word of seven-bit ids (a word with a top bit set could read as a
+ // NaN, and a device is free to change those).
+ const cells = level.data.length, packed = new Float32Array(cells + cells / 4), words = new Uint32Array(packed.buffer, cells * 4, cells / 4);
+ packed.set(level.data);
+ for (let i = 0; i < cells; i++) words[i >> 2]! |= level.owner[i]! << ((i & 3) * 8);
+ return { header, sdf: packed, instances: instanceData, pixels };
}
diff --git a/sdk/src/postprocess/effects.ts b/sdk/src/postprocess/effects.ts
index b452c5cb9..16fab9a93 100644
--- a/sdk/src/postprocess/effects.ts
+++ b/sdk/src/postprocess/effects.ts
@@ -577,6 +577,10 @@ registerEffect({
{ name: 'u_intensity', label: 'Intensity', min: 0, max: 4, step: 0.01, defaultValue: 1, neutralValue: 0 },
// How much of each frame replaces what the cards and probes held; lower is smoother and slower to follow a change.
{ name: 'u_blend', label: 'Temporal Blend', min: 0.02, max: 1, step: 0.01, defaultValue: 0.15 },
+ // The cards (the surfaces the rays read light from) are relit a slice at a time over this many frames: their light changes slowly.
+ { name: 'u_cardSlices', label: 'Card Update Frames', min: 1, max: 16, step: 1, defaultValue: 4 },
+ // One in this many of a probe's 64 rays is traced each frame, alternating; the history fills the rest in.
+ { name: 'u_probeStride', label: 'Probe Ray Stride', min: 1, max: 8, step: 1, defaultValue: 2 },
],
});
diff --git a/src/esengine/renderer/frame/DynamicGI.cpp b/src/esengine/renderer/frame/DynamicGI.cpp
index f4ef64b69..72083a885 100644
--- a/src/esengine/renderer/frame/DynamicGI.cpp
+++ b/src/esengine/renderer/frame/DynamicGI.cpp
@@ -37,8 +37,9 @@ struct ParamsBlock {
f32 sunRad[4];
f32 trace[4];
f32 sky[4];
+ f32 misc[4];
};
-static_assert(sizeof(ParamsBlock) == 112, "the GI block is seven vec4s");
+static_assert(sizeof(ParamsBlock) == 128, "the GI block is eight vec4s");
#ifdef ES_ENABLE_WEBGPU
// The CPU twin of what is here is in sdk/src/gi (globalSdf.ts sphereTrace, surfaceCache.ts lookup, screenProbes.ts tracePoint);
@@ -57,6 +58,7 @@ struct GI {
sunRad : vec4f, // rgb: radiance; w: how far off a surface a ray starts
trace : vec4f, // x: how far a ray looks, y: card tolerance, z: blend, w: hit fraction of a cell
sky : vec4f, // rgb: what a ray that meets nothing brings
+ misc : vec4f, // x: how many frames the cards are spread over; y: 1 of every this many rays of a probe is traced a frame; z: pixels between probes
};
@group(0) @binding(0) var<uniform> frame : Frame;
@group(0) @binding(1) var<uniform> gi : GI;
@@ -98,6 +100,10 @@ fn sdfTrace(origin : vec3f, dir : vec3f, maxT : f32) -> f32 {
if (t >= maxT) { return -1.0; }
let p = origin + dir * t;
if (!inField(p)) { return -1.0; }
+ // Far from anything (the cell the point is in says so by more than three cells) one read is enough: the field there, less the
+ // farthest a point can be from its cell's centre, is a distance nothing is nearer than. Close to a surface the 8 reads of a trilinear.
+ let near = cellValue(vec3i(floor(p / cell)));
+ if (near > cell * 3.0) { t = t + near - cell * 0.87; continue; }
let d = sdfSample(p);
if (d < cell * gi.trace.w) { return t; }
t = t + max(d, cell * 0.25);
@@ -128,57 +134,80 @@ fn unpackColour(w : u32) -> vec3f {
return vec3f(f32(w & 255u), f32((w >> 8u) & 255u), f32((w >> 16u) & 255u)) / 255.0;
}
-// What a ray brings back from the surface it hit at point, travelling along dir: the cards that face it, blended by how squarely each
-// does, from the pixel whose surface is within the tolerance of the point; of the instances that hold the point, the nearest wins.
+// What the cards of one instance say a ray brings back from the surface it hit at point, travelling along dir: the cards that face it,
+// blended by how squarely each does, from the pixel whose surface is within the tolerance of the point. w is how far that pixel is, or
+// -1 when no card of the instance holds the point.
+fn lookupIn(i : u32, point : vec3f, dir : vec3f) -> vec4f {
+ let tol = gi.trace.y;
+ let b = instBase(i);
+ let wmin = insts[b + 7u];
+ let wmax = insts[b + 8u];
+ if (any(point < wmin.xyz - vec3f(tol)) || any(point > wmax.xyz + vec3f(tol))) { return vec4f(0.0, 0.0, 0.0, -1.0); }
+ let r0 = insts[b + 4u]; let r1 = insts[b + 5u]; let r2 = insts[b + 6u];
+ let local = vec3f(dot(r0.xyz, point) + r0.w, dot(r1.xyz, point) + r1.w, dot(r2.xyz, point) + r2.w);
+ var d = vec3f(dot(r0.xyz, dir), dot(r1.xyz, dir), dot(r2.xyz, dir));
+ d = d / max(length(d), 1e-9);
+ let lmin = insts[b + 9u].xyz;
+ let lmax = insts[b + 10u].xyz;
+ let scale = wmin.w;
+ let cell = wmax.w;
+ var sum = vec3f(0.0);
+ var total = 0.0;
+ var error = 0.0;
+ for (var axis = 0u; axis < 3u; axis = axis + 1u) {
+ let da = comp(d, axis);
+ let weight = abs(da);
+ if (weight < 0.05) { continue; }
+ let face = axis * 2u + select(0u, 1u, da > 0.0);
+ let uv = faceAxes(face);
+ let lu = comp(local, uv.x); let lv = comp(local, uv.y);
+ if (lu < comp(lmin, uv.x) - cell || lu > comp(lmax, uv.x) + cell || lv < comp(lmin, uv.y) - cell || lv > comp(lmax, uv.y) + cell) { continue; }
+ let f = insts[b + 11u + face];
+ let fu = clamp((lu - comp(lmin, uv.x)) / max(comp(lmax, uv.x) - comp(lmin, uv.x), 1e-9), 0.0, 0.99999);
+ let fv = clamp((lv - comp(lmin, uv.y)) / max(comp(lmax, uv.y) - comp(lmin, uv.y), 1e-9), 0.0, 0.99999);
+ let px = u32(floor(fu * f.x));
+ let py = u32(floor(fv * f.y));
+ let idx = u32(f.z) + px + py * u32(f.x);
+ let pix = cardPix[idx];
+ let depth16 = pix.x & 0xffffu;
+ if (depth16 == 65535u) { continue; }
+ let ext = comp(lmax, axis) - comp(lmin, axis);
+ let sgn = face % 2u == 0u;
+ let la = comp(local, axis);
+ let pointDepth = select(la - comp(lmin, axis), comp(lmax, axis) - la, sgn);
+ let cardDepth = f32(depth16) / 65534.0 * ext;
+ let off = abs(cardDepth - pointDepth) * scale;
+ if (off > tol) { continue; }
+ error = max(error, off);
+ sum = sum + weight * radIn[idx].xyz;
+ total = total + weight;
+ }
+ if (total <= 0.0) { return vec4f(0.0, 0.0, 0.0, -1.0); }
+ return vec4f(sum / total, error);
+}
+
+// Which instance a cell is nearest to (id + 1; 0 for none, 127 for an id the table cannot hold). The words after the field hold four each.
+fn ownerAt(c : vec3i) -> u32 {
+ let d = i32(gi.sdf.x);
+ let w = c & vec3i(d - 1);
+ let idx = u32(w.x + w.y * d + w.z * d * d);
+ let cells = u32(d * d * d);
+ return (bitcast<u32>(sdfData[cells + (idx >> 2u)]) >> ((idx & 3u) * 8u)) & 127u;
+}
+
+// What a ray brings back from the surface it hit at point. The field names the instance the point is by, so the cards of that one are
+// asked; only where it says none (or its cards do not hold the point) are all of them, and of those the nearest wins.
fn lookup(point : vec3f, dir : vec3f) -> vec3f {
+ let owner = ownerAt(vec3i(floor(point / gi.sdf.y)));
+ if (owner > 0u && owner < 127u && owner <= gi.counts.x) {
+ let r = lookupIn(owner - 1u, point, dir);
+ if (r.w >= 0.0) { return r.xyz; }
+ }
var best = vec3f(0.0);
var bestError = 1e30;
- let tol = gi.trace.y;
for (var i = 0u; i < gi.counts.x; i = i + 1u) {
- let b = instBase(i);
- let wmin = insts[b + 7u];
- let wmax = insts[b + 8u];
- if (any(point < wmin.xyz - vec3f(tol)) || any(point > wmax.xyz + vec3f(tol))) { continue; }
- let r0 = insts[b + 4u]; let r1 = insts[b + 5u]; let r2 = insts[b + 6u];
- let local = vec3f(dot(r0.xyz, point) + r0.w, dot(r1.xyz, point) + r1.w, dot(r2.xyz, point) + r2.w);
- var d = vec3f(dot(r0.xyz, dir), dot(r1.xyz, dir), dot(r2.xyz, dir));
- d = d / max(length(d), 1e-9);
- let lmin = insts[b + 9u].xyz;
- let lmax = insts[b + 10u].xyz;
- let scale = wmin.w;
- let cell = wmax.w;
- var sum = vec3f(0.0);
- var total = 0.0;
- var error = 0.0;
- for (var axis = 0u; axis < 3u; axis = axis + 1u) {
- let da = comp(d, axis);
- let weight = abs(da);
- if (weight < 0.05) { continue; }
- let face = axis * 2u + select(0u, 1u, da > 0.0);
- let uv = faceAxes(face);
- let lu = comp(local, uv.x); let lv = comp(local, uv.y);
- if (lu < comp(lmin, uv.x) - cell || lu > comp(lmax, uv.x) + cell || lv < comp(lmin, uv.y) - cell || lv > comp(lmax, uv.y) + cell) { continue; }
- let f = insts[b + 11u + face];
- let fu = clamp((lu - comp(lmin, uv.x)) / max(comp(lmax, uv.x) - comp(lmin, uv.x), 1e-9), 0.0, 0.99999);
- let fv = clamp((lv - comp(lmin, uv.y)) / max(comp(lmax, uv.y) - comp(lmin, uv.y), 1e-9), 0.0, 0.99999);
- let px = u32(floor(fu * f.x));
- let py = u32(floor(fv * f.y));
- let idx = u32(f.z) + px + py * u32(f.x);
- let pix = cardPix[idx];
- let depth16 = pix.x & 0xffffu;
- if (depth16 == 65535u) { continue; }
- let ext = comp(lmax, axis) - comp(lmin, axis);
- let sgn = face % 2u == 0u;
- let la = comp(local, axis);
- let pointDepth = select(la - comp(lmin, axis), comp(lmax, axis) - la, sgn);
- let cardDepth = f32(depth16) / 65534.0 * ext;
- let off = abs(cardDepth - pointDepth) * scale;
- if (off > tol) { continue; }
- error = max(error, off);
- sum = sum + weight * radIn[idx].xyz;
- total = total + weight;
- }
- if (total > 0.0 && error < bestError) { bestError = error; best = sum / total; }
+ let r = lookupIn(i, point, dir);
+ if (r.w >= 0.0 && r.w < bestError) { bestError = r.w; best = r.xyz; }
}
return best;
}
@@ -214,16 +243,25 @@ fn rayRadiance(origin : vec3f, d : vec3f) -> vec3f {
}
)WGSL";
+// One workgroup relights one card pixel: its 64 threads are the 64 rays (one a thread, so the GPU is kept busy where one thread tracing
+// 64 in turn left it waiting on its own chain of reads), reduced in shared memory. Only a slice of the cards is relit each frame, round
+// robin. The cards are updated in place (a ray that reads a card another workgroup is rewriting sees the old or the new light, either
+// of which is a frame of the right answer), so radIn is declared read-write here.
const char* kCardLight = R"WGSL(
-@group(0) @binding(6) var<storage, read_write> radOut : array<vec4f>;
+var<workgroup> cardSum : array<vec3f, 64>;
@compute @workgroup_size(64)
-fn main(@builtin(global_invocation_id) id : vec3u) {
- let p = id.x;
- if (p >= gi.counts.y) { return; }
+fn main(@builtin(workgroup_id) wg : vec3u, @builtin(local_invocation_index) lid : u32) {
+ let slices = max(u32(gi.misc.x), 1u);
+ let chunk = (gi.counts.y + slices - 1u) / slices;
+ let settled = gi.origin.w >= 2;
+ let start = select(0u, (u32(gi.origin.w) % slices) * chunk, settled);
+ let end = select(gi.counts.y, min(start + chunk, gi.counts.y), settled);
+ let p = start + wg.x + wg.y * 65535u;
+ if (p >= end) { return; }
let pix = cardPix[p];
let depth16 = pix.x & 0xffffu;
- if (depth16 == 65535u) { radOut[p] = vec4f(0.0); return; }
+ if (depth16 == 65535u) { if (lid == 0u) { radIn[p] = vec4f(0.0); } return; }
let instance = pix.w >> 3u;
let face = pix.w & 7u;
let b = instBase(instance);
@@ -246,30 +284,26 @@ fn main(@builtin(global_invocation_id) id : vec3u) {
let scale = insts[b + 7u].w;
let nl = unpackNormal(pix.x, pix.y);
let n = normalize((m0.xyz * nl.x + m1.xyz * nl.y + m2.xyz * nl.z) / scale);
+ let origin = pos + n * gi.sunRad.w;
+ let d = probeRay(lid & 7u, lid >> 3u, n, p, u32(gi.origin.w));
+ cardSum[lid] = rayRadiance(origin, d) * dot(d, n);
+ workgroupBarrier();
+ for (var s = 32u; s > 0u; s = s >> 1u) {
+ if (lid < s) { cardSum[lid] = cardSum[lid] + cardSum[lid + s]; }
+ workgroupBarrier();
+ }
+ if (lid != 0u) { return; }
let albedo = unpackColour(pix.y >> 8u);
let emissive = unpackColour(pix.z) * insts[b + 9u].w;
-
var direct = vec3f(0.0);
if (gi.sunDir.w > 0.5) {
let ndl = dot(n, gi.sunDir.xyz);
- if (ndl > 0.0) {
- let t = sdfTrace(pos + n * gi.sunRad.w, gi.sunDir.xyz, gi.trace.x);
- if (t < 0.0) { direct = gi.sunRad.xyz * ndl; }
- }
+ if (ndl > 0.0 && sdfTrace(origin, gi.sunDir.xyz, gi.trace.x) < 0.0) { direct = gi.sunRad.xyz * ndl; }
}
- let origin = pos + n * gi.sunRad.w;
- var sum = vec3f(0.0);
- let frameIndex = u32(gi.origin.w);
- for (var j = 0u; j < 8u; j = j + 1u) {
- for (var i = 0u; i < 8u; i = i + 1u) {
- let d = probeRay(i, j, n, p, frameIndex);
- sum = sum + rayRadiance(origin, d) * dot(d, n);
- }
- }
- let indirect = sum * (2.0 / 64.0);
- let now = albedo * (direct + indirect) + emissive;
- let blend = select(gi.trace.z, 1.0, gi.origin.w < 2);
- radOut[p] = vec4f(mix(radIn[p].xyz, now, blend), 1.0);
+ let now = albedo * (direct + cardSum[0] * (2.0 / 64.0)) + emissive;
+ // A card is relit once in `slices` frames, so each relight carries that much more of the change.
+ let blend = select(min(gi.trace.z * f32(slices), 1.0), 1.0, !settled);
+ radIn[p] = vec4f(mix(radIn[p].xyz, now, blend), 1.0);
}
)WGSL";
@@ -307,7 +341,8 @@ var<workgroup> shB : array<vec3f, 64>;
@compute @workgroup_size(8, 8)
fn main(@builtin(workgroup_id) wg : vec3u, @builtin(local_invocation_id) lid3 : vec3u, @builtin(local_invocation_index) lid : u32) {
let size = vec2i(textureDimensions(depthTex));
- let t = min(vec2i(wg.xy) * 8 + vec2i(4), size - vec2i(1));
+ let spacing = i32(gi.misc.z);
+ let t = min(vec2i(wg.xy) * spacing + vec2i(spacing / 2), size - vec2i(1));
let depth = textureLoad(depthTex, t, 0);
let g = textureLoad(gbuffer, t, 0);
let valid = depth < 1.0 && g.a > 0.7;
@@ -318,9 +353,13 @@ fn main(@builtin(workgroup_id) wg : vec3u, @builtin(local_invocation_id) lid3 :
let pos = worldFromDepth((vec2f(t) + 0.5) / vec2f(size), depth);
if (dot(n, viewDirection(pos)) < 0.0) { n = -n; }
eyeDistance = length(pos - frame.camera.xyz);
- let d = probeRay(lid3.x, lid3.y, n, wg.x + wg.y * gi.counts.z, u32(gi.origin.w));
- let L = rayRadiance(pos + n * gi.sunRad.w, d);
- contribution = array<vec3f, 3>(d * L.x, d * L.y, d * L.z);
+ // One ray in `stride` of the 64 is traced this frame, the cells alternating like a checkerboard from frame to frame.
+ let stride = max(u32(gi.misc.y), 1u);
+ if (((lid3.x + lid3.y * 3u + u32(gi.origin.w)) % stride) == 0u) {
+ let d = probeRay(lid3.x, lid3.y, n, wg.x + wg.y * gi.counts.z, u32(gi.origin.w));
+ let L = rayRadiance(pos + n * gi.sunRad.w, d);
+ contribution = array<vec3f, 3>(d * L.x * f32(stride), d * L.y * f32(stride), d * L.z * f32(stride));
+ }
}
shR[lid] = contribution[0]; shG[lid] = contribution[1]; shB[lid] = contribution[2];
workgroupBarrier();
@@ -369,9 +408,10 @@ fn main(@builtin(global_invocation_id) id : vec3u) {
let pos = worldFromDepth((vec2f(p) + 0.5) / vec2f(size), depth);
if (dot(n, viewDirection(pos)) < 0.0) { n = -n; }
let tiles = vec2i(gi.counts.zw);
- // The probes round the pixel: probe (tx, ty) stands at pixel (tx * 8 + 4, ty * 8 + 4). The nine nearest are looked at, since in the
+ // The probes round the pixel: probe (tx, ty) stands at pixel (tx * spacing + spacing / 2, ty * spacing + spacing / 2). The nine nearest are looked at, since in the
// corner of two walls the four nearest can all be on the other wall, and a probe on another plane has nothing to say about this one.
- let q = (vec2f(p) - vec2f(4.0)) / 8.0;
+ let spacing = i32(gi.misc.z);
+ let q = (vec2f(p) - vec2f(f32(spacing / 2))) / f32(spacing);
let q0 = vec2i(floor(q));
let eye = max(length(pos - frame.camera.xyz), 1.0);
var E = vec3f(0.0);
@@ -382,7 +422,7 @@ fn main(@builtin(global_invocation_id) id : vec3u) {
let o = vec2i(k & 3, k >> 2) - vec2i(1);
let tile = q0 + o;
if (any(tile < vec2i(0)) || any(tile >= tiles)) { continue; }
- let pp = min(tile * 8 + vec2i(4), size - vec2i(1));
+ let pp = min(tile * spacing + vec2i(spacing / 2), size - vec2i(1));
let pd = textureLoad(depthTex, pp, 0);
let pg = textureLoad(gbuffer, pp, 0);
if (pd >= 1.0 || pg.a <= 0.7) { continue; }
@@ -419,6 +459,14 @@ const char* kProbe = "";
const char* kApply = "";
#endif
+/// The card relight writes the cards it reads (one buffer, updated in place): the common block declares them read-only.
+std::string readWriteCards(std::string s) {
+ const std::string from = "var<storage, read> radIn";
+ const size_t at = s.find(from);
+ if (at != std::string::npos) s.replace(at, from.size(), "var<storage, read_write> radIn");
+ return s;
+}
+
std::string join(const char* a, const char* b, const char* c = "") {
return std::string(a) + b + c;
}
@@ -468,7 +516,7 @@ bool DynamicGI::ensure() {
}
return true;
};
- return make(cardLight_, join(kCommon, kCardLight), "card light") && make(probe_, join(kCommon, kSurface, kProbe), "probe")
+ return make(cardLight_, readWriteCards(join(kCommon, kCardLight)), "card light") && make(probe_, join(kCommon, kSurface, kProbe), "probe")
&& make(apply_, join(kCommon, kSurface, kApply), "apply");
}
@@ -495,18 +543,16 @@ bool DynamicGI::upload() {
return false;
}
pixelCount_ = static_cast<u32>(s.header[kPixels]);
- // The radiance the cards hold: 16 bytes a pixel, two of them (this frame is written from the last), zeroed again on a new scene.
+ // The radiance the cards hold: 16 bytes a pixel, zeroed again on a new scene.
const u32 radianceBytes = std::max(pixelCount_ * 16u, 16u);
if (radianceBytes != radianceBytes_ || radiance_[0] == BufferHandle::Invalid) {
- for (BufferHandle& r : radiance_) {
- if (r != BufferHandle::Invalid) device_.deleteBuffer(r);
- r = device_.createBuffer({GfxBufferUsage::Storage, radianceBytes, false}, GfxContent::transient(), nullptr);
- }
+ if (radiance_[0] != BufferHandle::Invalid) device_.deleteBuffer(radiance_[0]);
+ radiance_[0] = device_.createBuffer({GfxBufferUsage::Storage, radianceBytes, false}, GfxContent::transient(), nullptr);
radianceBytes_ = radianceBytes;
- if (radiance_[0] == BufferHandle::Invalid || radiance_[1] == BufferHandle::Invalid) { failed_ = true; return false; }
+ if (radiance_[0] == BufferHandle::Invalid) { failed_ = true; return false; }
}
std::vector<f32> zero(radianceBytes / 4, 0.0f);
- for (BufferHandle r : radiance_) device_.updateBuffer(r, 0, zero.data(), radianceBytes);
+ device_.updateBuffer(radiance_[0], 0, zero.data(), radianceBytes);
frame_ = 0;
uploadedVersion_ = s.version;
return true;
@@ -538,7 +584,9 @@ rg::ResourceId DynamicGI::declare(rg::RenderGraph& graph, rg::ResourceId input,
u32 width, u32 height) {
if (!ensure() || input == rg::kNoResource || scene == rg::kNoResource) return input;
if (!upload()) return input;
- const u32 tilesX = (width + kProbeSize - 1) / kProbeSize, tilesY = (height + kProbeSize - 1) / kProbeSize;
+ // About 160 x 90 probes whatever the size of the picture: 8 pixels apart at 720p, 12 at 1080p, 16 at 1440p. Their cost is in their number.
+ probeSpacing_ = std::clamp(((height + 89) / 90 + 1) / 2 * 2, 8u, 32u);
+ const u32 tilesX = (width + probeSpacing_ - 1) / probeSpacing_, tilesY = (height + probeSpacing_ - 1) / probeSpacing_;
if (!ensureProbes(tilesX, tilesY)) return input;
rg::TargetDesc token;
@@ -563,6 +611,8 @@ rg::ResourceId DynamicGI::declare(rg::RenderGraph& graph, rg::ResourceId input,
b.sunRad[0] = h[kSunR]; b.sunRad[1] = h[kSunG]; b.sunRad[2] = h[kSunB]; b.sunRad[3] = h[kBias];
b.trace[0] = h[kRange]; b.trace[1] = h[kTolerance]; b.trace[2] = std::clamp(p.blend, 0.01f, 1.0f); b.trace[3] = h[kHit];
b.sky[0] = h[kSkyR]; b.sky[1] = h[kSkyG]; b.sky[2] = h[kSkyB];
+ b.misc[0] = static_cast<f32>(std::clamp(p.cardSlices, 1u, 4096u)); b.misc[1] = static_cast<f32>(std::clamp(p.probeStride, 1u, 8u));
+ b.misc[2] = static_cast<f32>(probeSpacing_);
device_.updateBuffer(params_, 0, &b, sizeof(b));
};
@@ -573,20 +623,22 @@ rg::ResourceId DynamicGI::declare(rg::RenderGraph& graph, rg::ResourceId input,
pass.compute = true;
pass.dependencies = {scene};
pass.write = lit;
- pass.execute = [this, writeParams](const rg::PassContext&) {
+ pass.execute = [this, writeParams, p](const rg::PassContext&) {
writeParams();
- // This frame's lighting is written to one of the two buffers and read from the other.
- const BufferHandle in = radiance_[frame_ & 1u], out = radiance_[(frame_ & 1u) ^ 1u];
- const GfxComputeBinding bindings[7] = {
+ const GfxComputeBinding bindings[6] = {
GfxComputeBinding::uniform(0, context_.frameUbo()),
GfxComputeBinding::uniform(1, params_),
GfxComputeBinding::storageRead(2, sdf_),
GfxComputeBinding::storageRead(3, instances_),
GfxComputeBinding::storageRead(4, pixels_),
- GfxComputeBinding::storageRead(5, in),
- GfxComputeBinding::storage(6, out),
+ GfxComputeBinding::storage(5, radiance_[0]),
};
- device_.dispatchCompute(cardLight_, bindings, 7, (pixelCount_ + 63) / 64);
+ // One workgroup a card pixel to relight: all of them for the first frames, then one slice of them.
+ const u32 slices = std::clamp(p.cardSlices, 1u, 4096u);
+ const u32 chunk = (pixelCount_ + slices - 1) / slices;
+ const u32 active = frame_ < 2 ? pixelCount_ : chunk;
+ if (active == 0) return;
+ device_.dispatchCompute(cardLight_, bindings, 6, std::min(active, 65535u), (active + 65534u) / 65535u);
};
graph.addPass(std::move(pass));
}
@@ -603,7 +655,7 @@ rg::ResourceId DynamicGI::declare(rg::RenderGraph& graph, rg::ResourceId input,
const TextureHandle gbuffer = graph.gbufferTextureOf(scene);
if (depth == TextureHandle::Invalid || gbuffer == TextureHandle::Invalid) return;
// The cards were just written into the buffer the next frame reads: the probes see this frame's lighting.
- const BufferHandle cards = radiance_[(frame_ & 1u) ^ 1u];
+ const BufferHandle cards = radiance_[0];
const u32 cur = frame_ & 1u;
const GfxComputeBinding bindings[10] = {
GfxComputeBinding::uniform(0, context_.frameUbo()),
diff --git a/src/esengine/renderer/frame/DynamicGI.hpp b/src/esengine/renderer/frame/DynamicGI.hpp
index 408cd026d..8c940609e 100644
--- a/src/esengine/renderer/frame/DynamicGI.hpp
+++ b/src/esengine/renderer/frame/DynamicGI.hpp
@@ -37,6 +37,10 @@ struct DynamicGIParams {
f32 intensity = 1.0f;
/// How much of each frame's card lighting replaces what the card held (the rest is history, which is what smooths the noise).
f32 blend = 0.15f;
+ /// The cards are relit a slice at a time over this many frames (their light changes slowly; the cost is in proportion).
+ u32 cardSlices = 4;
+ /// One in this many of a probe rays is traced each frame, alternating, so the history fills the rest in.
+ u32 probeStride = 2;
};
/**
@@ -92,6 +96,8 @@ private:
bool failed_ = false;
u32 frame_ = 0;
u32 pixelCount_ = 0;
+ /// Pixels between probes, chosen from the height of the picture.
+ u32 probeSpacing_ = 8;
};
} // namespace esengine
diff --git a/src/esengine/renderer/frame/PostProcessPipeline.cpp b/src/esengine/renderer/frame/PostProcessPipeline.cpp
index eefb21d04..3245e82ee 100644
--- a/src/esengine/renderer/frame/PostProcessPipeline.cpp
+++ b/src/esengine/renderer/frame/PostProcessPipeline.cpp
@@ -1334,6 +1334,8 @@ void PostProcessPipeline::runChain(std::vector<PostProcessPass>& passes, rg::Res
DynamicGIParams gi;
gi.intensity = get("u_intensity", gi.intensity);
gi.blend = get("u_blend", gi.blend);
+ gi.cardSlices = static_cast<u32>(std::max(get("u_cardSlices", static_cast<f32>(gi.cardSlices)), 1.0f));
+ gi.probeStride = static_cast<u32>(std::max(get("u_probeStride", static_cast<f32>(gi.probeStride)), 1.0f));
auto& cache = dynamic_gis_[temporalKey_];
if (!cache) {
if (dynamic_gis_.size() > 4) {
diff --git a/tools/gi/measure-gi-frame.mjs b/tools/gi/measure-gi-frame.mjs
new file mode 100644
index 000000000..13914170c
--- /dev/null
+++ b/tools/gi/measure-gi-frame.mjs
@@ -0,0 +1,105 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * The GPU time of every compute pass of a frame, from the device's own timestamp queries, over a few hundred frames of an exported web game.
+ *
+ * node tools/gi/measure-gi-frame.mjs <webDir> [frames=240] env W, H: the viewport (default 1280 x 720)
+ *
+ * Headless Chromium on the machine's own GPU (no window). A shim installed before the page loads asks the device for `timestamp-query`
+ * and puts a begin/end timestamp on every compute pass the game records; the results are read back a frame later. Passes are told apart
+ * by their order in a frame, since the engine does not label them: the lab scenes run the GI's three (cards, probes, apply) and nothing else
+ * that computes, so the order is theirs. Prints the median and the 95th percentile of each pass, and of their sum, in milliseconds.
+ */
+import path from 'node:path';
+import { pathToFileURL } from 'node:url';
+
+const root = path.resolve(import.meta.dirname, '../..');
+const [webDir, framesArg] = process.argv.slice(2);
+const frames = Number(framesArg ?? 240);
+const { chromium } = await import(pathToFileURL(path.join(root, 'node_modules/playwright/index.mjs')).href);
+const { serveHost } = await import(pathToFileURL(path.join(root, 'tools/lib/staticServer.mjs')).href);
+
+const shim = () => {
+ const frames = [];
+ window.__gpuFrames = frames;
+ const wrapAdapter = (adapter) => {
+ if (!adapter || adapter.__wrapped) return adapter;
+ adapter.__wrapped = true;
+ const request = adapter.requestDevice.bind(adapter);
+ adapter.requestDevice = async (desc = {}) => {
+ const features = new Set(desc.requiredFeatures ?? []);
+ if (adapter.features.has('timestamp-query')) features.add('timestamp-query');
+ const device = await request({ ...desc, requiredFeatures: [...features] });
+ instrument(device);
+ return device;
+ };
+ return adapter;
+ };
+ const original = navigator.gpu.requestAdapter.bind(navigator.gpu);
+ navigator.gpu.requestAdapter = async (...a) => wrapAdapter(await original(...a));
+
+ function instrument(device) {
+ if (!device.features.has('timestamp-query')) return;
+ const state = new WeakMap();
+ const proto = GPUCommandEncoder.prototype;
+ const begin = proto.beginComputePass, finish = proto.finish;
+ proto.beginComputePass = function (desc = {}) {
+ let s = state.get(this);
+ if (!s) { s = { passes: 0, querySet: device.createQuerySet({ type: 'timestamp', count: 64 }) }; state.set(this, s); }
+ if (s.passes >= 32) return begin.call(this, desc);
+ const k = s.passes++;
+ return begin.call(this, { ...desc, timestampWrites: { querySet: s.querySet, beginningOfPassWriteIndex: 2 * k, endOfPassWriteIndex: 2 * k + 1 } });
+ };
+ proto.finish = function (desc) {
+ const s = state.get(this);
+ if (!s || s.passes === 0) return finish.call(this, desc);
+ const bytes = 16 * s.passes;
+ const resolve = device.createBuffer({ size: bytes, usage: GPUBufferUsage.QUERY_RESOLVE | GPUBufferUsage.COPY_SRC });
+ const read = device.createBuffer({ size: bytes, usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ });
+ this.resolveQuerySet(s.querySet, 0, 2 * s.passes, resolve, 0);
+ this.copyBufferToBuffer(resolve, 0, read, 0, bytes);
+ const buffer = finish.call(this, desc);
+ (buffer.__reads ??= []).push({ read, passes: s.passes });
+ return buffer;
+ };
+ const submit = device.queue.submit.bind(device.queue);
+ device.queue.submit = (buffers) => {
+ submit(buffers);
+ for (const b of buffers) for (const { read, passes } of b.__reads ?? []) {
+ read.mapAsync(GPUMapMode.READ).then(() => {
+ const t = new BigUint64Array(read.getMappedRange().slice(0));
+ const ms = [];
+ for (let k = 0; k < passes; k++) ms.push(Number(t[2 * k + 1] - t[2 * k]) / 1e6);
+ frames.push(ms);
+ read.destroy();
+ });
+ }
+ };
+ }
+};
+
+const server = await serveHost(path.resolve(webDir));
+const browser = await chromium.launch({ channel: 'chromium', headless: true, args: ['--enable-unsafe-webgpu', '--ignore-gpu-blocklist', '--mute-audio'] });
+try {
+ const page = await browser.newPage({ viewport: { width: Number(process.env.W ?? 1280), height: Number(process.env.H ?? 720) }, deviceScaleFactor: 1 });
+ await page.addInitScript(shim);
+ await page.goto(`http://127.0.0.1:${server.address().port}/`, { waitUntil: 'load' });
+ // Let it settle (the scene loads, the cards converge), then take `frames` frames.
+ await page.waitForFunction((n) => window.__gpuFrames.length > n, (frames + 120) * Number(process.env.PASSES ?? 3), { timeout: 180000 });
+ const all = (await page.evaluate(() => window.__gpuFrames)).flat();
+ // The engine records each compute pass in an encoder of its own, so they arrive one at a time, in frame order, a frame being PASSES of them.
+ const passes = Number(process.env.PASSES ?? 3);
+ const skip = 120 * passes, taken = [];
+ for (let i = skip; i + passes <= all.length && taken.length < frames; i += passes) taken.push(all.slice(i, i + passes));
+ const pct = (xs, p) => [...xs].sort((a, b) => a - b)[Math.min(xs.length - 1, Math.floor(xs.length * p))];
+ const sums = taken.map((f) => f.reduce((a, b) => a + b, 0));
+ console.log(`${taken.length} frames, ${passes} compute passes a frame, ${process.env.W ?? 1280}x${process.env.H ?? 720}`);
+ for (let k = 0; k < passes; k++) {
+ const r = taken.map((f) => f[k]);
+ console.log(` pass ${k}: median ${pct(r, 0.5).toFixed(3)} ms p95 ${pct(r, 0.95).toFixed(3)} ms`);
+ }
+ console.log(` all compute: median ${pct(sums, 0.5).toFixed(3)} ms p95 ${pct(sums, 0.95).toFixed(3)} ms`);
+} finally {
+ await browser.close();
+ server.close();
+}