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(asset): a texture made or rewritten from JS leaves the device's unit bound as it was, so a scene view no longer goes black for good after a heightmap loads

yhh9c204fec6350
diff --git a/desktop b/desktop
index bd6a301cc..32d960f97 160000
--- a/desktop
+++ b/desktop
@@ -1 +1 @@
-Subproject commit bd6a301cc8c3cba2b308264d2725043b0c7591b0
+Subproject commit 32d960f97efffabd87446110aa06453936659a4f
diff --git a/sdk/src/asset/glTextureUpload.ts b/sdk/src/asset/glTextureUpload.ts
index 9a09c1a66..4cf007e0b 100644
--- a/sdk/src/asset/glTextureUpload.ts
+++ b/sdk/src/asset/glTextureUpload.ts
@@ -115,6 +115,26 @@ export function findWebGL2Context(glObj: ESEngineModule['GL'] | undefined): WebG
return null;
}
+/**
+ * Run `fn` with `texture` bound, and leave the unit bound to what it had.
+ *
+ * The device's context is not ours to leave changed: the device remembers what
+ * each unit holds and skips a bind it thinks is already made. Binding here on the
+ * unit it last made active (often 15, the per-instance records) made the device's
+ * memory a lie: it kept skipping its own bind, every instanced mesh read this
+ * texture as its transforms, and once the texture was freed they read nothing and
+ * the whole scene drew black, for good.
+ */
+function withBoundTexture<R>(gl: WebGL2RenderingContext, texture: WebGLTexture, fn: () => R): R {
+ const previous = gl.getParameter(gl.TEXTURE_BINDING_2D) as WebGLTexture | null;
+ gl.bindTexture(gl.TEXTURE_2D, texture);
+ try {
+ return fn();
+ } finally {
+ gl.bindTexture(gl.TEXTURE_2D, previous);
+ }
+}
+
/** What a texture handed to the device is, beyond its pixels. */
export interface HandOverDesc {
width: number;
@@ -140,8 +160,7 @@ export function handOverNewTexture(
const texture = gl.createTexture();
if (!texture) throw new Error('the GL context gave no texture (is it lost?)');
try {
- gl.bindTexture(gl.TEXTURE_2D, texture);
- write(gl);
+ withBoundTexture(gl, texture, () => write(gl));
} catch (err) {
gl.deleteTexture(texture);
throw err;
@@ -174,8 +193,7 @@ export function writeDeviceTexture(
const id = requireResourceManager().getTextureNativeId?.(handle) ?? 0;
const texture = id ? module.GL.textures[id] : null;
if (!texture) return false;
- gl.bindTexture(gl.TEXTURE_2D, texture);
- write(gl);
+ withBoundTexture(gl, texture, () => write(gl));
return true;
}
diff --git a/sdk/tests/canvas-texture.test.ts b/sdk/tests/canvas-texture.test.ts
index 7abdad52b..c72a50893 100644
--- a/sdk/tests/canvas-texture.test.ts
+++ b/sdk/tests/canvas-texture.test.ts
@@ -23,8 +23,13 @@ const LINEAR = 0x2601;
const LINEAR_MIPMAP_LINEAR = 0x2703;
function makeGl() {
+ // What the unit holds, so the bridge's restore of it can be checked: the device
+ // remembers its bindings, and one left changed is one it never makes again.
+ const ENGINE_BOUND = { tag: 'the device\'s own' } as unknown as WebGLTexture;
+ let bound: WebGLTexture | null = ENGINE_BOUND;
return {
- TEXTURE_2D: 0x0de1, RGBA, UNSIGNED_BYTE: 0x1401, SRGB8_ALPHA8,
+ ENGINE_BOUND,
+ TEXTURE_2D: 0x0de1, TEXTURE_BINDING_2D: 0x8069, RGBA, UNSIGNED_BYTE: 0x1401, SRGB8_ALPHA8,
NEAREST: 0x2600, LINEAR, LINEAR_MIPMAP_LINEAR, NEAREST_MIPMAP_NEAREST: 0x2700,
CLAMP_TO_EDGE, MIRRORED_REPEAT: 0x8370, REPEAT: 0x2901,
TEXTURE_MIN_FILTER, TEXTURE_MAG_FILTER: 0x2800,
@@ -34,7 +39,8 @@ function makeGl() {
texStorage2D: vi.fn(),
createTexture: vi.fn(() => ({ tag: 'first' }) as unknown as WebGLTexture),
deleteTexture: vi.fn(),
- bindTexture: vi.fn(),
+ bindTexture: vi.fn((_target: number, texture: WebGLTexture | null) => { bound = texture; }),
+ getParameter: vi.fn((name: number) => (name === 0x8069 ? bound : null)),
texImage2D: vi.fn(),
texParameteri: vi.fn(),
pixelStorei: vi.fn(),
@@ -174,6 +180,17 @@ describe('createCanvasTexture', () => {
expect(releaseTexture).toHaveBeenCalledTimes(1);
});
+ it('leaves the unit bound to what the device bound there, on the first take and every re-take', () => {
+ // The device skips a bind it believes is made. A texture left on its unit (15,
+ // the per-instance records, when that was the last one it used) was read as
+ // every instanced mesh's transforms, and once freed the whole scene drew black.
+ const gl = makeGl();
+ const tex = createCanvasTexture(makeApp(gl), makeSource(8, 8))!;
+ expect(gl.getParameter(gl.TEXTURE_BINDING_2D)).toBe(gl.ENGINE_BOUND);
+ tex.update();
+ expect(gl.getParameter(gl.TEXTURE_BINDING_2D)).toBe(gl.ENGINE_BOUND);
+ });
+
it('takes the canvas again into the texture the device rebuilt, and says the debt is paid', () => {
const gl = makeGl();
const app = makeApp(gl);
@@ -186,8 +203,11 @@ describe('createCanvasTexture', () => {
pool[7] = rebuilt;
expect(textureRefill(tex.handle)!()).toBe(true);
- expect(gl.bindTexture).toHaveBeenLastCalledWith(gl.TEXTURE_2D, rebuilt);
- expect(gl.bindTexture).not.toHaveBeenLastCalledWith(gl.TEXTURE_2D, beforeLoss);
+ const writes = gl.bindTexture.mock.calls.map((c) => c[1]).slice(-2);
+ expect(writes[0]).toBe(rebuilt);
+ expect(writes[0]).not.toBe(beforeLoss);
+ // ...and the unit is left holding what the device bound there.
+ expect(writes[1]).toBe(gl.ENGINE_BOUND);
expect(gl.texImage2D).toHaveBeenCalledTimes(2);
expect(restoreTextureContent).toHaveBeenCalledWith(tex.handle);
});
diff --git a/sdk/tests/compressed-texture.test.ts b/sdk/tests/compressed-texture.test.ts
index e1165ab60..9a47698c5 100644
--- a/sdk/tests/compressed-texture.test.ts
+++ b/sdk/tests/compressed-texture.test.ts
@@ -40,7 +40,9 @@ function makeGl(support: { astc?: boolean; etc?: boolean; s3tc?: boolean; s3tcSr
generateMipmap: vi.fn(),
getExtension: vi.fn((n: string) => exts[n] ?? null),
createTexture: vi.fn(() => ({}) as WebGLTexture),
+ TEXTURE_BINDING_2D: 0x8069,
bindTexture: vi.fn(),
+ getParameter: vi.fn(() => null),
compressedTexImage2D: vi.fn(),
texImage2D: vi.fn(),
texParameteri: vi.fn(),
diff --git a/sdk/tests/texture-upload-leak.test.ts b/sdk/tests/texture-upload-leak.test.ts
index 060663c08..811f1d8be 100644
--- a/sdk/tests/texture-upload-leak.test.ts
+++ b/sdk/tests/texture-upload-leak.test.ts
@@ -19,8 +19,10 @@ function mockGl(opts?: { createReturnsNull?: boolean; throwOnUpload?: boolean })
LINEAR: 0x2601, NEAREST: 0x2600, LINEAR_MIPMAP_LINEAR: 0, NEAREST_MIPMAP_NEAREST: 0,
REPEAT: 0x2901, CLAMP_TO_EDGE: 0x812f, MIRRORED_REPEAT: 0x8370,
UNPACK_FLIP_Y_WEBGL: 0, UNPACK_PREMULTIPLY_ALPHA_WEBGL: 0,
+ TEXTURE_BINDING_2D: 0x8069,
createTexture: vi.fn(() => (opts?.createReturnsNull ? null : tex)),
bindTexture: vi.fn(),
+ getParameter: vi.fn(() => null),
pixelStorei: vi.fn(),
texImage2D: vi.fn(() => { if (opts?.throwOnUpload) throw new Error('GL upload failed'); }),
compressedTexImage2D: vi.fn(() => { if (opts?.throwOnUpload) throw new Error('GL upload failed'); }),