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.

perf(assets): UASTC textures ship under zstd supercompression

yhh7b9042e1ab7c
diff --git a/pipeline/src/assets/textureCook.ts b/pipeline/src/assets/textureCook.ts
index 116f59d73..bc769d930 100644
--- a/pipeline/src/assets/textureCook.ts
+++ b/pipeline/src/assets/textureCook.ts
@@ -50,7 +50,7 @@ async function encodeCoverageChain(enc: BasisEncoderModule, img: { rgba: Uint8Ar
let level = img;
for (;;) {
parts.push(await enc.encodeToKtx2({ type: enc.ImageType.RGBA, data: level.rgba, width: level.width, height: level.height },
- { mode: 'uastc', srgb, mipmaps: false }));
+ { mode: 'uastc', srgb, mipmaps: false, supercompress: true }));
if (level.width === 1 && level.height === 1) break;
const half = halveRgba(level.rgba, level.width, level.height, srgb);
preserveAlphaCoverage(half.rgba, cutoff, coverage);
@@ -176,15 +176,19 @@ export async function cookTexture(input: TextureCookInput): Promise<TextureCookO
const mode = cook.selected;
const source = data;
const coverage = mode === 'uastc' ? tex.mipCoverage : 0;
+ // UASTC levels ship under zstd — the bytes a project's textures cost to move and to store.
+ // ETC1S already carries its own supercompression. Part of the key, so an entry cooked
+ // before this existed is re-encoded rather than reused.
+ const supercompress = mode === 'uastc';
const key = [source, JSON.stringify({ mode, srgb: tex.srgb, maxSize: tex.maxSize, scaled: scaled ? [size!.width, size!.height] : null,
- ...(coverage ? { mipCoverage: coverage } : {}) }), input.encoder!.id];
+ ...(coverage ? { mipCoverage: coverage } : {}), ...(supercompress ? { supercompress } : {}) }), input.encoder!.id];
const produce = (): Promise<Uint8Array> => {
const image = scaled ? scaledImage() : null;
return coverage
? encodeCoverageChain(enc, image ?? decodePngImage(path, source), tex.srgb, coverage)
: image
- ? enc.encodeToKtx2({ type: enc.ImageType.RGBA, data: image.rgba, width: image.width, height: image.height }, { mode, srgb: tex.srgb })
- : enc.encodeToKtx2({ type: enc.ImageType.PNG, data: source }, { mode, srgb: tex.srgb });
+ ? enc.encodeToKtx2({ type: enc.ImageType.RGBA, data: image.rgba, width: image.width, height: image.height }, { mode, srgb: tex.srgb, supercompress })
+ : enc.encodeToKtx2({ type: enc.ImageType.PNG, data: source }, { mode, srgb: tex.srgb, supercompress });
};
const file = cookCacheFile(root, key);
const encoded = input.cachedOnly ? await cookCacheHitAt(file) : (await cookCachedAt(file, produce)).bytes;
diff --git a/pipeline/tests/texture-cook.test.ts b/pipeline/tests/texture-cook.test.ts
index 42a7ef4da..a050150a9 100644
--- a/pipeline/tests/texture-cook.test.ts
+++ b/pipeline/tests/texture-cook.test.ts
@@ -11,6 +11,7 @@ import { tmpdir } from 'node:os';
import path from 'node:path';
import { cookTexture, loadTextureEncoder } from '../src/assets/textureCook';
import { cookAssets } from '../src/assets/cookAssets';
+import { readKtx2Layout } from '../src/assets/ktx2Mips';
import { noisePng, solidPng } from './fixtures/solidPng.mjs';
describe('a texture compressed for the editor', () => {
@@ -123,3 +124,27 @@ describe('a texture compressed for the editor', () => {
}
}, 60_000);
});
+
+describe('a UASTC texture the cook ships', () => {
+ it('is zstd-supercompressed, smaller than plain UASTC, and decodes to the same pixels', async () => {
+ const r = mkdtempSync(path.join(tmpdir(), 'estella-texcook-zstd-'));
+ try {
+ const png = solidPng(128, 128, [200, 90, 40, 255]);
+ const encoder = await loadTextureEncoder();
+ const cooked = await cookTexture({
+ root: r, path: 't/flat.png', data: new Uint8Array(png), ext: '.png', importer: { compress: 'on' },
+ encoder, compressTextures: true, atlasTextures: false, drawnIn3D: false,
+ });
+ expect(cooked.ext).toBe('.ktx2');
+ const layout = readKtx2Layout(cooked.data)!;
+ const plain = await encoder.module.encodeToKtx2({ type: encoder.module.ImageType.PNG, data: new Uint8Array(png) },
+ { mode: 'uastc', srgb: layout.srgb });
+ expect(layout.scheme).toBe(2);
+ expect(cooked.data.length).toBeLessThan(plain.length);
+ const [a, b] = await Promise.all([encoder.module.transcodeKtx2ToRgba(cooked.data), encoder.module.transcodeKtx2ToRgba(plain)]);
+ expect(Buffer.from(a.pixels).equals(Buffer.from(b.pixels))).toBe(true);
+ } finally {
+ rmSync(r, { recursive: true, force: true, maxRetries: 10, retryDelay: 50 });
+ }
+ });
+});