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.

test(render): a KTX2 fixture that a flipped upload cannot pass

yhh2bc887acaebe
diff --git a/desktop/public/scenes/ktx2-orientation.esscene b/desktop/public/scenes/ktx2-orientation.esscene
new file mode 100644
index 000000000..bbf6d9887
--- /dev/null
+++ b/desktop/public/scenes/ktx2-orientation.esscene
@@ -0,0 +1,39 @@
+{
+ "version": "1.0",
+ "name": "KTX2 Orientation",
+ "entities": [
+ {
+ "id": 0,
+ "name": "Camera",
+ "parent": null,
+ "children": [],
+ "components": [
+ { "type": "Transform", "data": { "position": { "x": 0, "y": 0, "z": 10 } } },
+ { "type": "Camera", "data": { "projectionType": 1, "orthoSize": 150, "isActive": true, "priority": 0 } }
+ ],
+ "visible": true
+ },
+ {
+ "id": 1,
+ "name": "Png",
+ "parent": null,
+ "children": [],
+ "components": [
+ { "type": "Transform", "data": { "position": { "x": -80, "y": 0, "z": 0 } } },
+ { "type": "Sprite", "data": { "size": { "x": 120, "y": 120 }, "color": { "r": 1, "g": 1, "b": 1, "a": 1 }, "texture": "@uuid:11111111-1111-4111-8111-111111111111" } }
+ ],
+ "visible": true
+ },
+ {
+ "id": 2,
+ "name": "Ktx2",
+ "parent": null,
+ "children": [],
+ "components": [
+ { "type": "Transform", "data": { "position": { "x": 80, "y": 0, "z": 0 } } },
+ { "type": "Sprite", "data": { "size": { "x": 120, "y": 120 }, "color": { "r": 1, "g": 1, "b": 1, "a": 1 }, "texture": "@uuid:22222222-2222-4222-8222-222222222222" } }
+ ],
+ "visible": true
+ }
+ ]
+}
diff --git a/desktop/public/scenes/ktx2-orientation.textures.json b/desktop/public/scenes/ktx2-orientation.textures.json
new file mode 100644
index 000000000..e486bbc44
--- /dev/null
+++ b/desktop/public/scenes/ktx2-orientation.textures.json
@@ -0,0 +1,4 @@
+{
+ "11111111-1111-4111-8111-111111111111": "/scenes/ktx2-test/updown.png",
+ "22222222-2222-4222-8222-222222222222": "/scenes/ktx2-test/updown.ktx2"
+}
diff --git a/desktop/public/scenes/ktx2-test/updown.ktx2 b/desktop/public/scenes/ktx2-test/updown.ktx2
new file mode 100644
index 000000000..54260df0d
Binary files /dev/null and b/desktop/public/scenes/ktx2-test/updown.ktx2 differ
diff --git a/desktop/public/scenes/ktx2-test/updown.png b/desktop/public/scenes/ktx2-test/updown.png
new file mode 100644
index 000000000..f4a7e4d7d
Binary files /dev/null and b/desktop/public/scenes/ktx2-test/updown.png differ
diff --git a/tools/make-orientation-fixture.mjs b/tools/make-orientation-fixture.mjs
new file mode 100644
index 000000000..ba2379b6f
--- /dev/null
+++ b/tools/make-orientation-fixture.mjs
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * Regenerate the KTX2 orientation fixture: one asymmetric image, written twice —
+ * once as PNG and once as KTX2.
+ *
+ * Asymmetric on purpose. Compressed textures shipped upside down for several
+ * releases and nothing caught it, because the fixture guarding that path was a
+ * solid green square: it looks the same either way up. A top-red/bottom-blue
+ * image cannot pass a flipped upload, and the PNG beside it is the control —
+ * the claim is not "KTX2 looks right", it is "KTX2 and PNG agree".
+ *
+ * node tools/make-orientation-fixture.mjs desktop/public/scenes/ktx2-test
+ */
+import { writeFileSync } from 'node:fs';
+import { deflateSync } from 'node:zlib';
+import { encodeToKtx2, ImageType } from '../build-tools/basis/encoder.mjs';
+
+const W = 64, H = 64;
+// Vertically ASYMMETRIC on purpose: the whole reason the flip bug survived is
+// that the fixture guarding it looked the same either way up.
+const px = Buffer.alloc(W * H * 4);
+for (let y = 0; y < H; y++) {
+ for (let x = 0; x < W; x++) {
+ const i = (y * W + x) * 4;
+ const top = y < H / 2;
+ px[i] = top ? 220 : 20; // R: top red
+ px[i + 1] = 20;
+ px[i + 2] = top ? 20 : 220; // B: bottom blue
+ px[i + 3] = 255;
+ }
+}
+
+// Minimal PNG (no filter, one IDAT).
+const crcTable = [...Array(256)].map((_, n) => { let c = n; for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1; return c >>> 0; });
+const crc = (b) => { let c = 0xffffffff; for (const x of b) c = crcTable[(c ^ x) & 0xff] ^ (c >>> 8); return (c ^ 0xffffffff) >>> 0; };
+const chunk = (type, data) => { const len = Buffer.alloc(4); len.writeUInt32BE(data.length); const td = Buffer.concat([Buffer.from(type), data]); const cc = Buffer.alloc(4); cc.writeUInt32BE(crc(td)); return Buffer.concat([len, td, cc]); };
+const ihdr = Buffer.alloc(13); ihdr.writeUInt32BE(W, 0); ihdr.writeUInt32BE(H, 4); ihdr[8] = 8; ihdr[9] = 6;
+const raw = Buffer.alloc((W * 4 + 1) * H);
+for (let y = 0; y < H; y++) { raw[y * (W * 4 + 1)] = 0; px.copy(raw, y * (W * 4 + 1) + 1, y * W * 4, (y + 1) * W * 4); }
+const png = Buffer.concat([Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]), chunk('IHDR', ihdr), chunk('IDAT', deflateSync(raw)), chunk('IEND', Buffer.alloc(0))]);
+writeFileSync(process.argv[2] + '/updown.png', png);
+
+const ktx2 = await encodeToKtx2({ type: ImageType.RGBA, data: new Uint8Array(px), width: W, height: H }, { mode: 'uastc', mipmaps: false });
+writeFileSync(process.argv[2] + '/updown.ktx2', Buffer.from(ktx2));
+console.log('wrote updown.png', png.length, 'and updown.ktx2', ktx2.length);