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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
refactor(assets): the texture cook reports the cache file it used
esengine755bcef1812b
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 5092e49a6..d0e8eb1f7 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -30,7 +30,7 @@ published separately; it ships inside the editor.- **physics-3d's lightmap is read as the light it is.** Its `.meta` predated the bake owning an atlas's import settings, so it was sampled as sRGB and repeated at its edges. `bake-scene --check` now holds an atlas's import settings to the bake's too, which is how it was found.-- **A large project opens in minutes, not half an hour.** The editor uploaded every texture as decoded pixels. ORCA Bistro's 224 2K textures came to 4.7 GiB of video memory, past what an 8 GB card had free, so Windows paged them in and out while the GPU sat idle. One frame stalled for up to 25 minutes, and Chromium's GPU watchdog then restarted the GPU process and the whole load began again. The editor, and the play realm beside it, now draw textures GPU-compressed, as a build does: the same KTX2, from the same cook cache, so the editor pays nothing for art an export already compressed and an export after an editing session encodes nothing. A texture the cache does not hold yet is drawn from a preview no larger than 1024 while it compresses in the background, then reloaded, and the editor does not report idle until it is. Textures with Compress off stay as they are. The editor draws each texture as the build ships it, by the same Compression rule. Bistro now opens with a cold GPU cache in about two minutes, where it took 20 to 36.+- **A large project opens in minutes, not half an hour.** The editor uploaded every texture as decoded pixels. ORCA Bistro's 224 2K textures came to 4.7 GiB of video memory, past what an 8 GB card had free, so Windows paged them in and out while the GPU sat idle. One frame stalled for up to 25 minutes, and Chromium's GPU watchdog then restarted the GPU process and the whole load began again. The editor, and the play realm beside it, now draw textures GPU-compressed, as a build does: the same KTX2, from the same cook cache, so the editor pays nothing for art an export already compressed and an export after an editing session encodes nothing. A texture the cache does not hold yet is drawn from a preview no larger than 1024 while it compresses in the background, then reloaded, and the editor does not report idle until it is. Textures with Compress off stay as they are. The editor draws each texture as the build ships it, by the same Compression rule. Bistro now opens with a cold GPU cache in about two minutes, where it took 20 to 36. Opening a project again reads and hashes no texture source: the editor remembers each texture's answer against the source's size and time, its import settings, whether a mesh draws it and the encoder, and streams the KTX2 from the cache file instead of copying it through its main process — Bistro's 1.3 GB of them had kept that process busy for most of the load.- **A scene of thousands of meshes draws its first frame in seconds.** Every mesh draw wrote its probe and instance blocks into a uniform buffer of its own, so a frame's first appearance created one per draw: ORCA Bistro's 8,000 draws made 16,000 buffers, 128 MB of them, and on Windows the GPU process spent 30 s of a 43 s project open creating them. A draw's blocks are now ranges of a few shared pages — a range starts where the last draw's bytes end, not a whole block later — and a pass stages every draw's blocks before it uploads each page once, where each draw had written its own. Bistro's frame now holds 18 pages and makes 56 uploads, where it made 15,924; it opens to idle in about 16 s.diff --git a/desktop b/desktopindex 1c86428f2..f750973a8 160000--- a/desktop+++ b/desktop@@ -1 +1 @@-Subproject commit 1c86428f26676111e8a3f76dd57c8710543a4dc1+Subproject commit f750973a84317c57627a7c5b851b496239b3d9b9diff --git a/pipeline/src/assets/cookCache.ts b/pipeline/src/assets/cookCache.tsindex 9bc395fef..0be66cb5c 100644--- a/pipeline/src/assets/cookCache.ts+++ b/pipeline/src/assets/cookCache.ts@@ -25,14 +25,11 @@ export function encoderIdentity(encoderFile: string): string {}}-/**- * The bytes `produce` makes for these inputs, from the cache when an earlier- * cook made them. A cache that cannot be written is only slower, never wrong.- *//** Concurrent cooks in one process can produce the same key; each writes its own temp file. */let tmpSerial = 0;-function cacheFile(root: string, inputs: ReadonlyArray<Uint8Array | string>): string {+/** Where the cache keeps what these inputs cook to — whether or not it holds it yet. */+export function cookCacheFile(root: string, inputs: ReadonlyArray<Uint8Array | string>): string {const h = createHash('sha256');for (const part of inputs) {h.update(typeof part === 'string' ? `s${part.length}:${part}` : `b${part.byteLength}:`);@@ -41,17 +38,20 @@ function cacheFile(root: string, inputs: ReadonlyArray<Uint8Array | string>): streturn path.join(root, CACHE_DIR, `${h.digest('hex')}.bin`);}-/** What an earlier cook made for these inputs, or null — never produces. */-export async function cookCacheHit(root: string, inputs: ReadonlyArray<Uint8Array | string>): Promise<Uint8Array | null> {- const file = cacheFile(root, inputs);- return existsSync(file) ? new Uint8Array(await readFile(file)) : null;+/** What an earlier cook left at @p file, or null — never produces. */+export async function cookCacheHitAt(file: string): Promise<Uint8Array | null> {+ return existsSync(file) ? await readFile(file) : null;}-export async function cookCached(- root: string, inputs: ReadonlyArray<Uint8Array | string>, produce: () => Promise<Uint8Array>,+/**+ * The bytes `produce` makes for the inputs @p file was named by, from the cache+ * when an earlier cook made them. A cache that cannot be written is only slower,+ * never wrong.+ */+export async function cookCachedAt(+ file: string, produce: () => Promise<Uint8Array>,): Promise<{ bytes: Uint8Array; hit: boolean }> {- const file = cacheFile(root, inputs);- if (existsSync(file)) return { bytes: new Uint8Array(await readFile(file)), hit: true };+ if (existsSync(file)) return { bytes: await readFile(file), hit: true };const bytes = await produce();try {await mkdir(path.dirname(file), { recursive: true });@@ -61,3 +61,9 @@ export async function cookCached(} catch { /* a read-only project cooks every time */ }return { bytes, hit: false };}++export function cookCached(+ root: string, inputs: ReadonlyArray<Uint8Array | string>, produce: () => Promise<Uint8Array>,+): Promise<{ bytes: Uint8Array; hit: boolean }> {+ return cookCachedAt(cookCacheFile(root, inputs), produce);+}diff --git a/pipeline/src/assets/textureCook.ts b/pipeline/src/assets/textureCook.tsindex 344adc2ea..fdcb81728 100644--- a/pipeline/src/assets/textureCook.ts+++ b/pipeline/src/assets/textureCook.ts@@ -15,7 +15,7 @@ import {type TextureCookDecision,} from './textureCookDecision';import { halveRgba, joinMipLevels, alphaCoverage, preserveAlphaCoverage } from './ktx2Mips';-import { cookCached, cookCacheHit, encoderIdentity } from './cookCache';+import { cookCachedAt, cookCacheFile, cookCacheHitAt, encoderIdentity } from './cookCache';/** Targets the KTX2 the cook emits can transcode to at runtime (UASTC + ETC1S). */export const COMPRESSED_TARGETS = ['astc-4x4', 'etc2-rgba8', 's3tc-dxt5'];@@ -125,6 +125,8 @@ export interface TextureCookOutput {defeatedByFormat: boolean;/** `cachedOnly`, and the encode this texture asks for has not been run. */pending?: boolean;+ /** The cook-cache file the compressed output is kept in. */+ cacheFile?: string;}/**@@ -168,7 +170,6 @@ export async function cookTexture(input: TextureCookInput): Promise<TextureCookOif (cook.reason === 'not-raster') defeatedByFormat = true;else warnings.push(`${path}: ${explainTextureCook(cook, size)}`);}- let compressedFormats: string[] | undefined;// A texture that ships as an image ships the shrunk one.if (cook.selected === 'raw' && rgba) data = encodeRgbaPng(tw, th, rgba);if (cook.selected !== 'raw') {@@ -187,11 +188,11 @@ export async function cookTexture(input: TextureCookInput): Promise<TextureCookO: rgba? enc.encodeToKtx2({ type: enc.ImageType.RGBA, data: rgba, width: tw, height: th }, { mode, srgb: tex.srgb }): enc.encodeToKtx2({ type: enc.ImageType.PNG, data: source }, { mode, srgb: tex.srgb }));- const encoded = input.cachedOnly ? await cookCacheHit(root, key) : (await cookCached(root, key, produce)).bytes;+ const file = cookCacheFile(root, key);+ const encoded = input.cachedOnly ? await cookCacheHitAt(file) : (await cookCachedAt(file, produce)).bytes;if (!encoded) return { data, ext, cook, warnings, defeatedByFormat, pending: true };- data = encoded;- ext = '.ktx2';- compressedFormats = COMPRESSED_TARGETS;+ return { data: encoded, ext: '.ktx2', cook, compressedFormats: COMPRESSED_TARGETS, warnings, defeatedByFormat,+ cacheFile: file };}- return { data, ext, cook, compressedFormats, warnings, defeatedByFormat };+ return { data, ext, cook, warnings, defeatedByFormat };}diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex d9a87f999..cb0db23f2 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -1326,6 +1326,10 @@ export const NOTED = {{ note: 'A model whose second UV set cannot be baked into' },'perf(renderer): a draw\'s uniform blocks are ranges of shared pages, uploaded once per pass':{ note: 'A scene of thousands of meshes draws its first frame in seconds' },+ 'perf(editor): a texture unchanged since the last open is answered from its cache file':+ { note: 'A large project opens in minutes, not half an hour' },+ 'refactor(assets): the texture cook reports the cache file it used':+ { note: 'A large project opens in minutes, not half an hour' },'fix(eht): the generator writes LF on every platform':{ internal: 'The engine header tool is a repository generator; on Windows it rewrote every generated file with CRLF, which only a contributor there ever saw.' },};