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.

feat(assets): re-upload textures behind the handles they already have

esengine1c8f82bf9ce9
diff --git a/desktop/scripts/headless-verify.mjs b/desktop/scripts/headless-verify.mjs
index 18234b2e4..f99b2cd60 100644
--- a/desktop/scripts/headless-verify.mjs
+++ b/desktop/scripts/headless-verify.mjs
@@ -104,11 +104,9 @@ function finish(result, server) {
const dl = result.deviceLoss;
const deviceLossOk = !dl || (dl.supported && dl.statusAfterLoss === 1 &&
(dl.reportAfterLoss?.length ?? 0) > 0 && dl.glLostAfterRestore === false &&
- dl.recovered === true && dl.statusAfterRecover === 2);
- // After a recovery the textures are placeholders until the asset layer
- // re-uploads, so "did it draw content" is not the question a loss run asks —
- // the cycle assertions above are. Restore this once re-upload lands.
- const renderedOk = dl ? true : (result.capture?.rendered ?? false);
+ dl.recovered === true && dl.statusAfterRecover === 2 &&
+ dl.fullRecovered === true && dl.statusAfterFull === 0);
+ const renderedOk = result.capture?.rendered ?? false;
const ok = result.ok && renderedOk && (result.expect?.ok ?? true) &&
(result.resize?.ok ?? true) && (result.preview?.ok ?? true) && (result.grid?.ok ?? true) &&
deviceLossOk;
@@ -257,6 +255,10 @@ app.whenReady().then(async () => {
if (!out.recovered) await new Promise((r) => setTimeout(r, 100));
}
out.statusAfterRecover = d.status();
+
+ // The full cycle: rebuild, re-upload the textures, and declare it whole.
+ out.fullRecovered = await d.recoverFull();
+ out.statusAfterFull = d.status();
api.step(${STEPS}, 1 / 60);
out.drawCallsAfterRecover = api.getStats ? api.getStats().drawCalls : -1;
return out;
diff --git a/desktop/src/headless.ts b/desktop/src/headless.ts
index 630da4756..40ac21346 100644
--- a/desktop/src/headless.ts
+++ b/desktop/src/headless.ts
@@ -13,7 +13,7 @@
import { EngineHost } from './engine/EngineHost';
import {
DeviceStatus, getDeviceStatus, getDeviceLostReport, recoverDevice, finishDeviceRecovery,
- getContextLossGuardInfo,
+ getContextLossGuardInfo, Assets,
} from 'esengine';
// From EditorSession: importing it constructs the default session (which wires the
// Reconciler + SceneStore to the model) before the engine boots below.
@@ -32,6 +32,7 @@ declare global {
report(): string;
recover(): boolean;
finishRecovery(): void;
+ recoverFull(): Promise<boolean>;
lose(): boolean;
contextLost(): boolean | null;
guard(): { target: string; lostEventsSeen: number };
@@ -79,6 +80,11 @@ window.__estellaHeadless = {
report: () => getDeviceLostReport(),
recover: () => recoverDevice(),
finishRecovery: () => finishDeviceRecovery(),
+ recoverFull: async () => {
+ const assets = EngineHost.getResource(Assets);
+ if (!assets) return false;
+ return assets.recoverFromDeviceLoss();
+ },
guard: () => getContextLossGuardInfo(),
contextLost: () => {
const gl = EngineHost.canvas?.getContext('webgl2') as WebGL2RenderingContext | null;
diff --git a/sdk/etc/index.api.md b/sdk/etc/index.api.md
index 3262de941..6ba5c853d 100644
--- a/sdk/etc/index.api.md
+++ b/sdk/etc/index.api.md
@@ -675,6 +675,7 @@ get: (key: string) => T | undefined
getOrLoad: (key: string, loader: () => Promise<T>, timeout?: number) => Promise<T>
has: (key: string) => boolean
invalidate: (key: string) => boolean
+set: (key: string, value: T) => void
sizes: () => { cached: number; pending: number; failed: number; }
values: () => IterableIterator<T>
static new <T>(dispose_?: ((value: T) => void) | undefined): AsyncCache<T>
diff --git a/sdk/etc/index.native.api.md b/sdk/etc/index.native.api.md
index 2250447c1..cb3500cc9 100644
--- a/sdk/etc/index.native.api.md
+++ b/sdk/etc/index.native.api.md
@@ -675,6 +675,7 @@ get: (key: string) => T | undefined
getOrLoad: (key: string, loader: () => Promise<T>, timeout?: number) => Promise<T>
has: (key: string) => boolean
invalidate: (key: string) => boolean
+set: (key: string, value: T) => void
sizes: () => { cached: number; pending: number; failed: number; }
values: () => IterableIterator<T>
static new <T>(dispose_?: ((value: T) => void) | undefined): AsyncCache<T>
diff --git a/sdk/etc/index.node.api.md b/sdk/etc/index.node.api.md
index e0444a9bb..152fc272d 100644
--- a/sdk/etc/index.node.api.md
+++ b/sdk/etc/index.node.api.md
@@ -670,6 +670,7 @@ get: (key: string) => T | undefined
getOrLoad: (key: string, loader: () => Promise<T>, timeout?: number) => Promise<T>
has: (key: string) => boolean
invalidate: (key: string) => boolean
+set: (key: string, value: T) => void
sizes: () => { cached: number; pending: number; failed: number; }
values: () => IterableIterator<T>
static new <T>(dispose_?: ((value: T) => void) | undefined): AsyncCache<T>
diff --git a/sdk/etc/minigame.api.md b/sdk/etc/minigame.api.md
index 7616df120..c15eb18c4 100644
--- a/sdk/etc/minigame.api.md
+++ b/sdk/etc/minigame.api.md
@@ -670,6 +670,7 @@ get: (key: string) => T | undefined
getOrLoad: (key: string, loader: () => Promise<T>, timeout?: number) => Promise<T>
has: (key: string) => boolean
invalidate: (key: string) => boolean
+set: (key: string, value: T) => void
sizes: () => { cached: number; pending: number; failed: number; }
values: () => IterableIterator<T>
static new <T>(dispose_?: ((value: T) => void) | undefined): AsyncCache<T>
diff --git a/sdk/etc/wechat.api.md b/sdk/etc/wechat.api.md
index 2bd4fd86d..66efe1122 100644
--- a/sdk/etc/wechat.api.md
+++ b/sdk/etc/wechat.api.md
@@ -670,6 +670,7 @@ get: (key: string) => T | undefined
getOrLoad: (key: string, loader: () => Promise<T>, timeout?: number) => Promise<T>
has: (key: string) => boolean
invalidate: (key: string) => boolean
+set: (key: string, value: T) => void
sizes: () => { cached: number; pending: number; failed: number; }
values: () => IterableIterator<T>
static new <T>(dispose_?: ((value: T) => void) | undefined): AsyncCache<T>
diff --git a/sdk/src/asset/Assets.ts b/sdk/src/asset/Assets.ts
index cb2444404..6258d814d 100644
--- a/sdk/src/asset/Assets.ts
+++ b/sdk/src/asset/Assets.ts
@@ -44,6 +44,7 @@ import type { AssetRegistry } from './AssetRegistry';
import { UUID_REF_PREFIX } from './AssetRegistry';
import type { AssetRefCounter } from './AssetRefCounter';
import { log } from '../util/logger';
+import { recoverDevice, finishDeviceRecovery } from '../render/renderer';
/** Callback fired when `Assets.invalidate(ref)` actually dropped cache entries.
* `oldTextureHandle` is the texture handle that was bound before the drop (0 when
@@ -365,6 +366,59 @@ export class Assets {
return this.loadTextureVariant_(ref, false);
}
+ /**
+ * Re-uploads every cached texture behind the handle it already has.
+ *
+ * The GPU objects died with the device; the handles did not. The load path
+ * runs again for its BYTES and the result is retargeted onto the original
+ * handle — reusing it keeps this one sweep, not one per upload path.
+ */
+ async reuploadTexturesAfterDeviceLoss(): Promise<number> {
+ const rm = requireResourceManager();
+ if (!rm.retargetExternalTexture) return 0;
+
+ let restored = 0;
+ for (const [key, previous] of [...this.textureCache_.entries()]) {
+ const cut = key.lastIndexOf(':');
+ if (cut < 0) continue;
+ const path = key.slice(0, cut);
+ const flip = key.slice(cut + 1) === 'f';
+
+ // Removed so the load is a real one: the cached entry names a GPU
+ // object that no longer exists.
+ this.textureCache_.delete(key);
+ try {
+ const fresh = await this.loadTextureVariant_(path, flip);
+ const glId = rm.getTextureGLId(fresh.handle);
+ if (glId && rm.retargetExternalTexture(previous.handle, glId, fresh.width, fresh.height)) {
+ restored++;
+ }
+ // The fresh POOL entry is scaffolding; its GPU object now belongs
+ // to the original handle, and external textures are not owned, so
+ // releasing the record does not take it away.
+ if (fresh.handle !== previous.handle) rm.releaseTexture(fresh.handle);
+ } catch (e) {
+ log.warn('assets', `Device recovery: re-upload failed for ${path}`, e);
+ }
+ this.textureCache_.set(key, previous);
+ }
+ log.info('assets', `Device recovery: ${restored} texture(s) re-uploaded`);
+ return restored;
+ }
+
+ /**
+ * The whole recovery, in the order it has to happen: rebuild what the engine
+ * can, put the content back, then declare the device whole. Returns false
+ * while the context is not available yet — a browser restores when it is
+ * ready, so a caller retries rather than giving up.
+ */
+ async recoverFromDeviceLoss(): Promise<boolean> {
+ if (!recoverDevice()) return false;
+ await this.reuploadTexturesAfterDeviceLoss();
+ finishDeviceRecovery();
+ return true;
+ }
+
private async loadTextureVariant_(ref: string, flip: boolean): Promise<TextureResult> {
const path = this.resolveLoadPath_(ref);
const cacheKey = this.textureCacheKey_(path, flip);
diff --git a/sdk/src/asset/AsyncCache.ts b/sdk/src/asset/AsyncCache.ts
index dbe71af79..d1860544f 100644
--- a/sdk/src/asset/AsyncCache.ts
+++ b/sdk/src/asset/AsyncCache.ts
@@ -172,6 +172,11 @@ export class AsyncCache<T> {
this.pending_.clear();
}
+ /** Re-file a value under `key`, for a sweep that removed it to force a reload. */
+ set(key: string, value: T): void {
+ this.cache_.set(key, value);
+ }
+
/**
* Key/value pairs of what is resolved. `values()` is not enough for a sweep
* that has to RELOAD each entry: the key is what names the asset.