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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
perf(streaming): a layer of chunks with nothing left to make is not walked - every cell in reach was keyed, measured and asked each frame, 9 ms for six fields of plants with the camera standing; the walk is kept while its sources stand and skipped once every chunk is made
yhh65cbc2d7b651
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex deeb3a619..21fe85720 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -16,6 +16,7 @@ published separately; it ships inside the editor.### Fixed+- **A meadow at rest no longer costs the frame 9 ms.** What is made round the camera in chunks (a `Foliage` field's cells, a generated world's squares) was worked out afresh every frame: every cell within reach given a key and a distance, then asked whether it was made. Six fields of 8 m cells reaching 260 m cost 9.1 ms a frame that way with the camera standing still and nothing left to make. The walk is now kept while no source has left its cell or moved a sixteenth of one, and a layer whose every chunk is made is not asked again until a source moves, its generation changes or a part of it is invalidated: 0.13 ms for the same six fields. A camera in flight still walks as it crosses cells.- **Baking a water region no longer holds the frame.** A `WaterZone` is baked over several frames, a step at a time inside the frame's spare milliseconds, but a step was 16 or 32 rows whatever the rows' length and then whole sweeps of the grid: at a `resolution` of 2048 each step took 70 ms and the last ones 190 to 400, so the editor and a game ran at 10 frames a second for as long as the bake took, with frames of two seconds in it. A step is a count of cells now (the flood, the two distance sweeps and the blurs step too): the longest measured is 20 ms, and the same bake takes 7,600 steps where it took 300. The baked field is the same. It still runs on the main thread.- **A ground with no layer files is painted again.** In 0.104.0 a `Terrain` drawn by the stock shader (four layer colours or textures and a splatmap, no `.esterrainlayer` files) was one colour from edge to edge on every platform, and logged `shader Terrain: texture param "u_paintPages" finds no free texture unit`. The baked occlusion map made the paint that shader's eighth texture, one past the seven units a material has; the eighth takes an engine unit the shader does not read, and the shader was counted as reading four of them because it keeps three colours in variables named `s0` to `s3`, which is how WGSL names a sampler unit. A name a source declares for itself (`let s2`, a `var` in a function, a parameter) is not a unit now. A criterion, `terrain-paint`, draws a ground painted in two colours on both backends: nothing in the pull-request tier drew a terrain before.- **A loop is seamless in a packaged game.** A `.wav` is re-encoded to MP3 when a game is exported, and a decoder gives an MP3 back with 1105 samples of silence before its sound and several hundred after it: measured in a browser, a one-second loop that was seamless as a `.wav` had 21 ms of silence every time round once packaged, and every sound, looping or not, began a fortieth of a second late. No tag in the file tells a player to take the silence off, and none does. The exporter now records, for each clip it re-encodes, how long it really is and what leads it, and the runtime plays the clip from past the lead to its real end and loops it between the two: exact to the sample where the clip is decoded (the web's decoded clips and the native host), and started past the lead where it is played from its file (a streamed clip on the web, a mini-game). A decoder that takes the silence off itself is seen by the length of what it returns and is not asked to twice. A project played from the editor reads its own `.wav` files and was never affected.diff --git a/sdk/src/residency/chunks.ts b/sdk/src/residency/chunks.tsindex b754e231c..916b2b0ed 100644--- a/sdk/src/residency/chunks.ts+++ b/sdk/src/residency/chunks.ts@@ -100,8 +100,18 @@ interface Layer<T> {chunks: Map<string, Chunk<T>>;/** The chunks within the load radius of a source, as of the last frame. */wanted: Set<string>;+ /** The last walk of the cells round the sources, kept while they stand where they stood: see {@link WALK_SLACK}. */+ walk?: { from: string; at: readonly ChunkSource[]; near: Map<string, number>; keep: Set<string>; settledFor?: number | string };}+/**+ * How far a source may move, as a share of a cell, before the cells round it are walked again. The walk is every cell within the+ * larger radius, each with a key and a distance, and then every one of them asked whether it is made: 9 ms a frame for six fields+ * of 8 m cells reaching 260 m, with the camera standing still and nothing left to make. A source that has not left its cell and+ * has moved less than this is where it was; and a layer with every chunk it wants made, for the generation it is at, is not asked.+ */+const WALK_SLACK = 1 / 16;+/** At most this many chunks of a layer are answered later at once. */const MAX_IN_FLIGHT = 16;@@ -176,6 +186,8 @@ export class ProceduralChunks {const d = layer.def;const cx0 = Math.floor((x0 - d.originX) / d.sizeX), cx1 = Math.floor((x1 - d.originX) / d.sizeX);const cz0 = Math.floor((z0 - d.originZ) / d.sizeZ), cz1 = Math.floor((z1 - d.originZ) / d.sizeZ);+ // What was settled is not any more: the layer is asked again from the next step.+ if (layer.walk) layer.walk.settledFor = undefined;for (const c of layer.chunks.values()) {if (c.cx < cx0 || c.cx > cx1 || c.cz < cz0 || c.cz > cz1) continue;if (c.value !== null) c.publishedFor = OUTDATED;@@ -200,9 +212,21 @@ export class ProceduralChunks {for (const layer of this.layers_.values()) {const d = layer.def;const sources = d.sources ? [...d.sources()] : d.plane === 'xy' ? world.map((s) => ({ x: s.x, z: s.y ?? 0 })) : world;- const near = new Map<string, number>();- const keep = new Set<string>();- for (const s of sources) {+ // What decides the walk beside the sources: a layer that changes its reach or its cells is walked again.+ const from = `${d.loadRadius},${d.unloadRadius},${d.sizeX},${d.sizeZ},${d.originX},${d.originZ},${d.extent ? d.extent.join(',') : ''}`;+ const slack = Math.min(d.sizeX, d.sizeZ) * WALK_SLACK;+ const was = layer.walk;+ const stood = was !== undefined && was.from === from && was.at.length === sources.length && sources.every((s, i) => {+ const o = was.at[i]!;+ return Math.abs(s.x - o.x) <= slack && Math.abs(s.z - o.z) <= slack+ && Math.floor((s.x - d.originX) / d.sizeX) === Math.floor((o.x - d.originX) / d.sizeX)+ && Math.floor((s.z - d.originZ) / d.sizeZ) === Math.floor((o.z - d.originZ) / d.sizeZ);+ });+ const near = stood ? was.near : new Map<string, number>();+ const keep = stood ? was.keep : new Set<string>();+ if (stood && was.settledFor === d.generation) continue;+ if (!stood) layer.walk = { from, at: sources.map((s) => ({ x: s.x, z: s.z })), near, keep };+ for (const s of stood ? [] : sources) {const reach = Math.max(d.loadRadius, d.unloadRadius);const x0 = Math.floor((s.x - reach - d.originX) / d.sizeX), x1 = Math.floor((s.x + reach - d.originX) / d.sizeX);const z0 = Math.floor((s.z - reach - d.originZ) / d.sizeZ), z1 = Math.floor((s.z + reach - d.originZ) / d.sizeZ);@@ -223,14 +247,19 @@ export class ProceduralChunks {this.drop(layer, c);layer.chunks.delete(key);}- layer.wanted = new Set(near.keys());+ if (!stood) layer.wanted = new Set(near.keys());+ let unsettled = false;for (const [key, dist] of near) {const c = layer.chunks.get(key);const state = c ? stateOf(c, d.generation) : null;+ if (c?.making) unsettled = true;if (state === 'ready' || state === 'failed' || c?.making) continue;const [cx, cz] = key.split(',').map(Number) as [number, number];wanted.push({ layer, cx, cz, dist });+ unsettled = true;}+ // Nothing to make and nothing on its way: until its sources move or its generation does, this layer has nothing to say.+ layer.walk!.settledFor = unsettled ? undefined : d.generation;}wanted.sort((a, b) => a.dist - b.dist);const inFlight = new Map<Layer<unknown>, number>();diff --git a/sdk/tests/procedural-chunks.test.ts b/sdk/tests/procedural-chunks.test.tsindex 0f4ec87d4..ccb4b496e 100644--- a/sdk/tests/procedural-chunks.test.ts+++ b/sdk/tests/procedural-chunks.test.ts@@ -202,3 +202,59 @@ describe('the frame\'s one budget', () => {expect(bakes.hasTime()).toBe(true);});});++describe('a layer whose sources stand still', () => {+ // The walk of every cell in reach was made again each frame: 19 ms a frame for a meadow with the camera standing.+ it('is asked for nothing more, follows a source out of its cell at once, and lets go what it leaves behind', () => {+ const { layer, live, log } = recording();+ const at: ChunkSource = { x: 5, z: 5 };+ const chunks = new ProceduralChunks();+ chunks.sources_ = () => [at];+ chunks.set('l', layer);+ chunks.step(always);+ const first = log.made.length;+ expect(first).toBeGreaterThan(20);+ // A step smaller than a sixteenth of a cell, a hundred frames of it: nothing is made and nothing is let go.+ for (let i = 0; i < 100; i++) { at.x = 5 + 0.3 * Math.sin(i); chunks.step(always); }+ expect([log.made.length, log.released.length]).toEqual([first, 0]);+ // Into the next cell: the cells that came into reach are made in that very step.+ at.x = 25;+ chunks.step(always);+ expect(log.made.length).toBeGreaterThan(first);+ expect(live.has('5,0')).toBe(true);+ // And far away: what was round the first place is gone.+ at.x = 500;+ chunks.step(always);+ expect(live.has('0,0')).toBe(false);+ expect(live.has('50,0')).toBe(true);+ });++ it('makes again what is made out of date by hand, and what a new generation asks for, without a source moving', () => {+ const { layer, log } = recording();+ const chunks = new ProceduralChunks();+ chunks.sources_ = () => [{ x: 5, z: 5 }];+ chunks.set('l', layer);+ chunks.step(always);+ chunks.step(always);+ const first = log.made.length;+ chunks.invalidate('l', 0, 0, 9, 9);+ chunks.step(always);+ expect(log.made.length).toBe(first + 1);+ layer.generation = 2;+ chunks.step(always);+ expect(log.made.length).toBe(first + 1 + first);+ });++ it('is walked again when the layer changes how far it reaches', () => {+ const { layer, live } = recording();+ const chunks = new ProceduralChunks();+ chunks.sources_ = () => [{ x: 5, z: 5 }];+ chunks.set('l', layer);+ chunks.step(always);+ expect(live.has('6,0')).toBe(false);+ layer.loadRadius = 65;+ layer.unloadRadius = 80;+ chunks.step(always);+ expect(live.has('6,0')).toBe(true);+ });+});diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex 4bf3e7c4b..0a6f19b53 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -4714,6 +4714,8 @@ export const NOTED = {{"note": "A ground with no layer files is painted again."},"fix(water): baking a water region no longer holds the frame - its steps were a fixed count of rows and whole sweeps of the grid, 70 to 400 ms each at 2048 texels a side; a step is a count of cells now, 20 ms at the most":{"note": "Baking a water region no longer holds the frame."},+ "perf(streaming): a layer of chunks with nothing left to make is not walked - every cell in reach was keyed, measured and asked each frame, 9 ms for six fields of plants with the camera standing; the walk is kept while its sources stand and skipped once every chunk is made":+ {"note": "A meadow at rest no longer costs the frame 9 ms."},"chore(gates): what the checks asked of this week's work - a disposition for esengine/image in a native script and its namespace there, Waterfall's width and its place in the corpus, the recipe worker's bundle as not a contract, shadeLight2D's inout against its return, and a test that named App as a type":{"internal": "tables the gates read, brought level; a natively packaged script can now import esengine/image, which no native game has run yet"},"feat(image): a picture's source goes where a texture goes, and its maps are recipes too - a texture field takes an .esimage, the image tools write a picture's normal and surface maps as recipes beside it and answer their refs, text is set by the drawer a game sets it with, and the viewport draws a recipe as a game does":