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.

fix(terrain): the page atlases are owed after a device loss, so their refill runs and the ground comes back

yhh9679b8ac1f18
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6f43e63f9..9cac9c79f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -93,6 +93,7 @@ published separately; it ships inside the editor.
- **No "uniform not found" warnings for a texture a program does not read.** A material sets each of its textures on every program it is drawn with, and the depth program of a terrain or a lit material reads few of them, so opening a scene filled the log with warnings that read like typos. A texture a program does not use is now skipped quietly.
- **A terrain with texture layers no longer keeps a second copy of them in memory.** Its layer arrays were made as content the graphics device keeps a CPU copy of, to restore after a GPU loss: 12 MiB on Terrain & Water, which took the copies past their 16 MiB budget and logged a warning. The terrain now says how to make them again: after a loss it reads and packs its layer files once more and puts them back behind the same textures.
- **No "NaN is an invalid value for height" error when a project opens.** The content browser measured its grid before the editor had placed the panel, read the panel's padding as a number it was not, and gave the grid a height of NaN for that first frame.
+- **A web game comes back after the GPU goes away.** A browser can take the GPU from a page (a driver reset, a laptop waking, too many tabs). Three things stood in the way of the way back, all seen on Terrain & Water: while the engine waited for its textures, the frames it refused still gathered geometry and never emptied it, until an allocation of gigabytes stopped the module; the restored context had none of the WebGL extensions the engine relies on, so an HDR scene came back black; and the terrain's page atlases were never refilled, so the ground came back flat and black. The game now recovers to the picture it had.
- **A subpath of an official package resolves.** Importing `estella-plugin-…/something` from a project script no longer hangs the script build.
- **Background shader compilation, the warm-up and on-demand drawing reach a build whose quality level is Off.** A build dropped the whole `quality` block when the level was Off, though these three choices hold whatever the level: a project that switched `asyncShaderCompile`, `prewarmShaders` or `renderOnDemand` on read them in the editor and shipped a game that ignored them. The block now ships when any of them is on.
- **A terrain and a streamed world run on the Android and iOS templates.** The terrain's layer textures were refused on a device (it has no wasm heap to copy them through), and a chunked world's maker threw on every tick because QuickJS has no `AbortController`. The layers now go over as bytes, as every other texture does, and the world uses a handle of its own where the host has none.
diff --git a/sdk/src/terrain/atlases.ts b/sdk/src/terrain/atlases.ts
new file mode 100644
index 000000000..6c0eaba46
--- /dev/null
+++ b/sdk/src/terrain/atlases.ts
@@ -0,0 +1,27 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file atlases.ts
+ * @brief The atlases a ground's pages are drawn from (pages.ts): made blank, and refilled by forgetting the pages.
+ */
+import type { App } from '../app/app';
+import { createTextureFromPixels } from '../runtime/runtimeAssets';
+import { getResourceManager, provideTextureContent } from '../wasm/resourceManager';
+import { TextureContent } from '../wasm';
+
+/**
+ * `count` blank `size`-square atlases (heights, paint, then paint2 for a ground of more than four layers); 0 for one the engine refused.
+ * The device keeps no copy: a page is made from the source whenever it is wanted, so after a loss `forget` drops them all and they are
+ * made again. That leaves the atlases the blank storage they come back with, which is their content, and pays the device for them.
+ */
+export function createPageAtlases(app: App, count: number, size: number, forget: () => void): number[] {
+ const blank = new Uint8Array(size * size * 4);
+ const params = { filterMode: 'linear', wrapMode: 'clamp', srgb: false } as const;
+ const atlases: number[] = [];
+ for (let i = 0; i < count; i++) {
+ const handle = createTextureFromPixels(app.wasmModule, { width: size, height: size, pixels: blank }, TextureContent.Composed, false, params);
+ if (handle) provideTextureContent(handle, () => { forget(); getResourceManager()?.restoreTextureContent?.(handle); return true; });
+ atlases.push(handle);
+ }
+ return atlases;
+}
diff --git a/sdk/src/terrain/planetPlugin.ts b/sdk/src/terrain/planetPlugin.ts
index d3cde6649..2af07590b 100644
--- a/sdk/src/terrain/planetPlugin.ts
+++ b/sdk/src/terrain/planetPlugin.ts
@@ -22,9 +22,8 @@ import { Assets } from '../asset/AssetPlugin';
import type { MaterialShaderSource } from '../asset/loaders/MaterialShaderAssetLoader';
import { CullMode, Material, MATERIAL_FORMAT_VERSION } from '../render/material';
import { BlendMode } from '../render/blend';
-import { createTextureFromPixels, updateTextureSubregion } from '../runtime/runtimeAssets';
-import { getResourceManager, provideTextureContent, withdrawTextureContent } from '../wasm/resourceManager';
-import { TextureContent } from '../wasm';
+import { updateTextureSubregion } from '../runtime/runtimeAssets';
+import { getResourceManager, withdrawTextureContent } from '../wasm/resourceManager';
import { EditorView, editorViewEye } from '../camera/EditorView';
import { log } from '../util/logger';
import { renderOrigin } from '../render/renderOrigin';
@@ -33,6 +32,7 @@ import type { Color, Entity } from '../types';
import { PageTable, PAGE_SIDE, PAGE_TEXELS, PATCH_CELLS, type PageImage } from './pages';
import { buildPatchMesh } from './patch';
import { loadLayerArrays, releaseLayerArrays } from './layerArrays';
+import { createPageAtlases } from './atlases';
import { PlanetRegistry, PlanetRegistryRes } from './planetQuery';
import { buildSphereMesh } from './planetSea';
import { removePlanetCollision, updatePlanetCollision } from './planetCollision';
@@ -293,12 +293,7 @@ export class PlanetPlugin implements Plugin {
if (built.epoch !== epoch) return;
const assets = app.getResource(Assets);
built.mesh = assets.createMesh(buildPatchMesh(PATCH_CELLS, true));
- const blank = new Uint8Array(ATLAS_SIZE * ATLAS_SIZE * 4);
- const params = { filterMode: 'linear', wrapMode: 'clamp', srgb: false } as const;
- built.heights = createTextureFromPixels(app.wasmModule, { width: ATLAS_SIZE, height: ATLAS_SIZE, pixels: blank }, TextureContent.Transient, false, params);
- built.paint = createTextureFromPixels(app.wasmModule, { width: ATLAS_SIZE, height: ATLAS_SIZE, pixels: blank }, TextureContent.Transient, false, params);
- // A device that lost the atlases has lost every page in them: they are made again as they are wanted.
- for (const handle of [built.heights, built.paint]) provideTextureContent(handle, () => { built.table.clear(); return true; });
+ [built.heights = 0, built.paint = 0] = createPageAtlases(app, 2, ATLAS_SIZE, () => built.table.clear());
built.instance = Material.createInstance(material);
if (!built.mesh || !built.heights || !built.paint || !built.instance) {
built.failure = 'the engine refused the planet\'s mesh, atlases or material';
diff --git a/sdk/src/terrain/terrainPlugin.ts b/sdk/src/terrain/terrainPlugin.ts
index 480685e04..ec1e31544 100644
--- a/sdk/src/terrain/terrainPlugin.ts
+++ b/sdk/src/terrain/terrainPlugin.ts
@@ -29,8 +29,8 @@ import { decodeImagePixels } from '../asset/imageDecode';
import { decodePngData } from '../asset/pngData';
import { Material, MATERIAL_FORMAT_VERSION } from '../render/material';
import { BlendMode } from '../render/blend';
-import { createTextureArrayFromPixels, createTextureFromPixels, updateTextureSubregion } from '../runtime/runtimeAssets';
-import { getResourceManager, provideTextureContent, withdrawTextureContent } from '../wasm/resourceManager';
+import { createTextureArrayFromPixels, updateTextureSubregion } from '../runtime/runtimeAssets';
+import { getResourceManager, withdrawTextureContent } from '../wasm/resourceManager';
import { TextureContent } from '../wasm';
import { EditorView, editorViewEye } from '../camera/EditorView';
import { log } from '../util/logger';
@@ -52,6 +52,7 @@ import { collisionGeometryOf } from '../runtime/collisionGeometry';
import { chunksOf, ProceduralChunksRes } from '../residency/chunks';
import { movers } from './movers';
import { createLayerArrays, imagePixelsAt, readAndPackLayers, releaseLayerArrays } from './layerArrays';
+import { createPageAtlases } from './atlases';
/** The engine's own provider of ground without end: seeded noise of the world's position. */
export const NOISE_PROVIDER = 'estella:noise';
@@ -769,15 +770,7 @@ export class TerrainPlugin implements Plugin {
if (built.epoch !== epoch) return;
const assets = app.getResource(Assets);
built.mesh = assets.createMesh(buildPatchMesh(PATCH_CELLS));
- const blank = new Uint8Array(ATLAS_SIZE * ATLAS_SIZE * 4);
- const params = { filterMode: 'linear', wrapMode: 'clamp', srgb: false } as const;
- built.heights = createTextureFromPixels(app.wasmModule, { width: ATLAS_SIZE, height: ATLAS_SIZE, pixels: blank }, TextureContent.Transient, false, params);
- built.paint = createTextureFromPixels(app.wasmModule, { width: ATLAS_SIZE, height: ATLAS_SIZE, pixels: blank }, TextureContent.Transient, false, params);
- if (layered) built.paint2 = createTextureFromPixels(app.wasmModule, { width: ATLAS_SIZE, height: ATLAS_SIZE, pixels: blank }, TextureContent.Transient, false, params);
- // A device that lost the atlases has lost every page in them: they are made again as they are wanted.
- for (const handle of [built.heights, built.paint, built.paint2].filter(Boolean)) {
- provideTextureContent(handle, () => { built.table.clear(); return true; });
- }
+ [built.heights = 0, built.paint = 0, built.paint2 = 0] = createPageAtlases(app, layered ? 3 : 2, ATLAS_SIZE, () => built.table.clear());
built.instance = Material.createInstance(material);
if (!built.mesh || !built.heights || !built.paint || !built.instance) {
log.warn('terrain', 'the engine refused the terrain\'s mesh, atlases or material');
diff --git a/sdk/tests/terrain-page-atlases-refill.test.ts b/sdk/tests/terrain-page-atlases-refill.test.ts
new file mode 100644
index 000000000..bd465ce78
--- /dev/null
+++ b/sdk/tests/terrain-page-atlases-refill.test.ts
@@ -0,0 +1,45 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file A ground's page atlases are owed after a device loss, so their refill runs: it forgets the pages, which are made again as they
+ * are wanted, and pays the device for the blank storage the atlases come back with.
+ */
+import { afterEach, describe, expect, it, vi } from 'vitest';
+import type { App } from '../src/app/app';
+import { TextureContent, type CppResourceManager } from '../src/wasm';
+import { initResourceManager, shutdownResourceManager, textureRefill } from '../src/wasm/resourceManager';
+import { createPageAtlases } from '../src/terrain/atlases';
+
+function engine() {
+ let next = 20;
+ const content = new Map<number, number>();
+ const rm = {
+ createTextureFromBytes: vi.fn((_w: number, _h: number, _p: Uint8Array, _f: number, _flip: boolean, c: number) => {
+ const handle = next++;
+ content.set(handle, c);
+ return handle;
+ }),
+ restoreTextureContent: vi.fn((_handle: number) => {}),
+ };
+ initResourceManager(rm as unknown as CppResourceManager);
+ return { rm, content, app: { wasmModule: null } as unknown as App };
+}
+
+afterEach(() => shutdownResourceManager());
+
+describe('terrain page atlases after a device loss', () => {
+ it('are content their creator composes, so a loss leaves them owed and their refill is asked', () => {
+ const { content, app } = engine();
+ const atlases = createPageAtlases(app, 3, 4, () => {});
+ expect(atlases.map((h) => content.get(h))).toEqual([TextureContent.Composed, TextureContent.Composed, TextureContent.Composed]);
+ });
+
+ it('forget the pages and pay for the blank storage when refilled', async () => {
+ const { rm, app } = engine();
+ const forget = vi.fn();
+ const atlases = createPageAtlases(app, 2, 4, forget);
+ for (const h of atlases) expect(await textureRefill(h)!()).toBe(true);
+ expect(forget).toHaveBeenCalledTimes(2);
+ expect(rm.restoreTextureContent.mock.calls.map(([h]) => h)).toEqual(atlases);
+ });
+});
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index eb73902f9..145db7cc0 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -3436,4 +3436,10 @@ export const NOTED = {
{"internal":"the build command's debug switch; no engine change"},
"fix(editor): the content browser grid reads a scroller the dock has not attached yet as having no padding, not a NaN width":
{"note":"No \"NaN is an invalid value for height\" error when a project opens."},
+ "fix(render): a frame refused for a lost device starts empty and collects nothing, so its staging no longer grows until an allocation stops the module":
+ {"note":"A web game comes back after the GPU goes away."},
+ "fix(render): a restored WebGL context is given its extensions again before the device probes what it can do":
+ {"note":"A web game comes back after the GPU goes away."},
+ "fix(terrain): the page atlases are owed after a device loss, so their refill runs and the ground comes back":
+ {"note":"A web game comes back after the GPU goes away."},
};