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
fix(foliage): release retired generation assets after the last chunk
yhh4e3e1896a037
diff --git a/sdk/src/asset/AssetLease.ts b/sdk/src/asset/AssetLease.tsindex e9fc962fe..8a7e2ac90 100644--- a/sdk/src/asset/AssetLease.ts+++ b/sdk/src/asset/AssetLease.ts@@ -34,6 +34,25 @@ export interface AssetLease<T = unknown> {retain(): AssetLease<T> | null;}+/** A composed resource has no cache generation to re-acquire. Its last receipt disposes it. */+export function ownedAssetLease<T>(key: string, generation: number, value: T, dispose: (value: T) => void): AssetLease<T> {+ let holders = 0;+ const acquire = (): AssetLease<T> => {+ holders++;+ let spent = false;+ return {+ key, generation, value,+ release: () => {+ if (spent) return;+ spent = true;+ if (--holders === 0) dispose(value);+ },+ retain: () => spent ? null : acquire(),+ };+ };+ return acquire();+}+/*** @brief Everything one owner acquired, released together.*diff --git a/sdk/src/asset/Assets.ts b/sdk/src/asset/Assets.tsindex db344c65d..37d77145e 100644--- a/sdk/src/asset/Assets.ts+++ b/sdk/src/asset/Assets.ts@@ -25,7 +25,7 @@ import type { TextureFormatReport } from './textureFormatReport';import { TextureLoader, textureResidencyKey } from './loaders/TextureLoader';import { AssetRefLedger, type AssetRefLease } from './AssetRefLedger';import { optionalAssetLoaders } from './optionalLoaders';-import { AssetScope, type AssetLease } from './AssetLease';+import { AssetScope, ownedAssetLease, type AssetLease } from './AssetLease';import { EntityAssetScopes } from './entityAssetScopes';import { RegistryAssetSlots, type RegistryAssetKind } from './registryAssets';import { DependencyRecorder, PreparationFailure, preparationFailureReason, isInvalidatable, rebuildPlan, type AssetIdentity,@@ -2005,23 +2005,10 @@ export class Assets {* destroys it rather than handing it back to a pool that could revive it.*/private ownedTextureLease_(value: TextureResult): AssetLease<TextureResult> {- let holders = 0;- const lease = (): AssetLease<TextureResult> => {- holders++;- let spent = false;- return {- key: `composed:${value.handle}`, generation: value.handle, value,- release: () => {- if (spent) return;- spent = true;- if (--holders > 0) return;- requireResourceManager().releaseTexture(value.handle);- evictTextureDimensions(value.handle);- },- retain: () => lease(),- };- };- return lease();+ return ownedAssetLease(`composed:${value.handle}`, value.handle, value, texture => {+ requireResourceManager().releaseTexture(texture.handle);+ evictTextureDimensions(texture.handle);+ });}/** {@link textureLease_} for everything that goes through a typed loader. */diff --git a/sdk/src/foliage/placedFoliage.ts b/sdk/src/foliage/placedFoliage.tsindex 31c162aa3..f1974e50b 100644--- a/sdk/src/foliage/placedFoliage.ts+++ b/sdk/src/foliage/placedFoliage.ts@@ -8,10 +8,10 @@* Transform, a MeshRenderer and a draw for each, which no frame holds. Here they are DATA: `kinds` says what each is made of* (its parts: a mesh, its cheaper mesh, a material, a texture, a tint, and how far it is worth drawing), `records` says where* each stands, six numbers a plant. The ground is cut into cells; the cells round the streaming sources (or the eye) are grown,- * each as ONE mesh per part and kind with all the plants of that kind in it, and let go with their meshes past the distance. The- * scene holds one entity; a frame holds the few hundred draws near the eye. Nothing is baked into the scene.+ * each as one MeshInstances set per part and kind. Geometry is shared across cells, each plant is culled and selects its own+ * LOD, and matching parts batch across cells. Cells retain their generation's assets until replacement or unloading.*- * A mesh keeps every channel the artist made (see buildPlacedMesh), so a normal map, a second uv or a vertex colour survives;+ * The shared mesh keeps every channel the artist made, so a normal map, a second uv or a vertex colour survives;* nothing is painted over it. A plant stands upright, turned about the vertical, and uniformly scaled.*/import type { App, Plugin } from '../app/app';@@ -24,7 +24,7 @@ import { chunksOf, ProceduralChunksRes } from '../residency/chunks';import type { Entity } from '../types';import type { Plant } from './builder';import { PLACED_STRIDE, type PlacedKind, type PlacedPart } from './plantKind';-import { plantGrowthOf, prepareKinds, type GrownCell, type ReadyKinds } from './plantGrowth';+import { plantGrowthOf, prepareKinds, releaseKinds, type GrownCell, type ReadyKinds } from './plantGrowth';export { PLACED_STRIDE, type PlacedKind, type PlacedPart };@@ -82,6 +82,7 @@ interface BuiltPlaced {/** The records sorted into cells (`cz * 1000 + cx`), by kind, the first time a cell asks. */cells: Map<number, Map<number, Plant[]>> | null;epoch: number;+ retired: boolean;}const layerOf = (entity: Entity): string => `placed:${entity}`;@@ -129,12 +130,13 @@ export class PlacedFoliagePlugin implements Plugin {app.addSystemToSchedule(Schedule.Update, defineSystem([], () => {if (!app.hasResource(Assets)) return;- for (const entity of field.built_.keys()) {+ for (const [entity, built] of field.built_) {if (app.world.valid(entity) && app.world.has(entity, PlacedFoliage)) continue;if (app.hasResource(ProceduralChunksRes)) {app.getResource(ProceduralChunksRes).remove(layerOf(entity));app.getResource(ProceduralChunksRes).remove(`${layerOf(entity)}:far`);}+ this.retire(built);field.built_.delete(entity);}for (const entity of app.world.getEntitiesWithComponents([PlacedFoliage, Transform])) {@@ -142,7 +144,8 @@ export class PlacedFoliagePlugin implements Plugin {const signature = signatureOf(data);let built = field.built_.get(entity);if (!built || built.signature !== signature) {- built = { signature, data: { ...data }, ready: null, cells: null, epoch: (built?.epoch ?? 0) + 1 };+ if (built) this.retire(built);+ built = { signature, data: { ...data }, ready: null, cells: null, epoch: (built?.epoch ?? 0) + 1, retired: false };field.built_.set(entity, built);void this.start(app, built);}@@ -154,21 +157,28 @@ export class PlacedFoliagePlugin implements Plugin {cleanup(app?: App): void {if (!app?.hasResource(PlacedFoliageRes)) return;const field = app.getResource(PlacedFoliageRes);- for (const entity of field.built_.keys()) {+ for (const [entity, built] of field.built_) {if (app.hasResource(ProceduralChunksRes)) {app.getResource(ProceduralChunksRes).remove(layerOf(entity));app.getResource(ProceduralChunksRes).remove(`${layerOf(entity)}:far`);}+ this.retire(built);}field.built_.clear();}+ private retire(built: BuiltPlaced): void {+ built.retired = true;+ if (built.ready) releaseKinds(built.ready);+ }+/** Reads the geometry and makes the handles of every part of every kind; the cells wait for it. */private async start(app: App, built: BuiltPlaced): Promise<void> {const epoch = built.epoch;try {const ready = await prepareKinds(app.getResource(Assets), built.data.kinds);- if (built.epoch === epoch) built.ready = ready;+ if (!built.retired && built.epoch === epoch) built.ready = ready;+ else releaseKinds(ready);} catch (e) {log.error('foliage', 'preparing the placed plants failed', e);}diff --git a/sdk/src/foliage/plantGrowth.ts b/sdk/src/foliage/plantGrowth.tsindex ab56fb7fb..5f33ec97e 100644--- a/sdk/src/foliage/plantGrowth.ts+++ b/sdk/src/foliage/plantGrowth.ts@@ -21,6 +21,7 @@ import { engineApi } from '../ecs/bridge/engineApi';import type { CppRegistry } from '../wasm';import { withScratch } from '../wasm/wasmScratch';import { Assets } from '../asset/AssetPlugin';+import { AssetScope, ownedAssetLease, type AssetLease } from '../asset/AssetLease';import { Quality } from '../render/qualityRuntime';import type { Assets as AssetsApi } from '../asset/Assets';import { log } from '../util/logger';@@ -33,10 +34,10 @@ import type { PlacedKind, PlacedPart } from './plantKind';export interface ReadyPart { part: PlacedPart; mesh: number; lod1: number; lod2?: number; material: number; texture: number }/** Every kind's parts in hand, and the parts that stand for each far off; `owned` are the meshes made here, which `releaseKinds` gives back. */-export interface ReadyKinds { kinds: ReadyPart[][]; far: ReadyPart[][]; owned: number[] }+export interface ReadyKinds { kinds: ReadyPart[][]; far: ReadyPart[][]; owned: number[]; lease: AssetLease<AssetScope> }/** One grown cell: its entity (none where nothing stands), what it owns so far, and whether it has been let go. */-export interface GrownCell { entity: Entity | null; handles: number[]; dead: boolean }+export interface GrownCell { entity: Entity | null; handles: number[]; dead: boolean; assets?: AssetLease<AssetScope> }/*** Loads the meshes, materials and textures of every part of every kind, once. A part whose normals are turned toward up (`soft`) is a mesh of its own,@@ -44,13 +45,29 @@ export interface GrownCell { entity: Entity | null; handles: number[]; dead: boo*/export async function prepareKinds(assets: AssetsApi, kinds: readonly PlacedKind[]): Promise<ReadyKinds> {const owned: number[] = [];+ const scope = new AssetScope();+ const handles = new Map<string, number>();+ const load = async (type: 'mesh' | 'material' | 'texture', path: string): Promise<number> => {+ const key = `${type}:${path}`;+ const known = handles.get(key);+ if (known !== undefined) return known;+ const receipt = type === 'texture' ? await assets.acquireTexture(path) : await assets.acquireTyped<{ handle: number }>(type, path);+ scope.add(receipt);+ handles.set(key, receipt.value.handle);+ return receipt.value.handle;+ };const handleOf = async (path: string, soft: number): Promise<number> => {- const loaded = (await assets.load<{ handle: number }>('mesh', path)).handle;+ const loaded = await load('mesh', path);if (!soft) return loaded;+ const key = `soft:${soft}:${path}`;+ const known = handles.get(key);+ if (known !== undefined) return known;const source = await assets.readMeshData(loaded);const made = source ? assets.createMesh(softenNormals(source, soft)) : 0;if (made === 0) return loaded;owned.push(made);+ scope.add(ownedAssetLease(key, made, made, handle => assets.releaseBuiltMesh(handle)));+ handles.set(key, made);return made;};const ready = async (part: PlacedPart, withLod: boolean): Promise<ReadyPart | null> => {@@ -63,25 +80,32 @@ export async function prepareKinds(assets: AssetsApi, kinds: readonly PlacedKindif (withLod && part.lod2Mesh) { try { lod2 = await handleOf(part.lod2Mesh, part.soft ?? 0); } catch { lod2 = 0; } }return {part, mesh, lod1, ...(lod2 ? { lod2 } : {}),- material: part.material ? (await assets.loadMaterial(part.material)).handle : 0,- texture: part.texture ? (await assets.loadTexture(part.texture)).handle : 0,+ material: part.material ? await load('material', part.material) : 0,+ texture: part.texture ? await load('texture', part.texture) : 0,};};- const out: ReadyKinds = { kinds: [], far: [], owned };- for (const kind of kinds) {- const parts: ReadyPart[] = [], far: ReadyPart[] = [];- for (const part of kind.parts) { const r = await ready(part, true); if (r) parts.push(r); }- for (const part of kind.far ?? []) { const r = await ready(part, false); if (r) far.push(r); }- out.kinds.push(parts);- out.far.push(far);+ const lease = ownedAssetLease('foliage-parts', 0, scope, held => {+ try { held.releaseAll(); } finally { owned.length = 0; }+ });+ const out: ReadyKinds = { kinds: [], far: [], owned, lease };+ try {+ for (const kind of kinds) {+ const parts: ReadyPart[] = [], far: ReadyPart[] = [];+ for (const part of kind.parts) { const r = await ready(part, true); if (r) parts.push(r); }+ for (const part of kind.far ?? []) { const r = await ready(part, false); if (r) far.push(r); }+ out.kinds.push(parts);+ out.far.push(far);+ }+ } catch (error) {+ lease.release();+ throw error;}return out;}-/** Gives back the meshes `prepareKinds` made; the ones it only loaded stay with the asset system. */-export function releaseKinds(assets: AssetsApi, ready: ReadyKinds): void {- for (const handle of ready.owned) assets.releaseBuiltMesh(handle);- ready.owned.length = 0;+/** Retires the owner's receipt. Published cells keep this generation alive until they are replaced. */+export function releaseKinds(ready: ReadyKinds): void {+ ready.lease.release();}/** The growth of every plant in an app: the pieces waiting. */@@ -140,6 +164,8 @@ export class PlantGrowth {growCell(app: App, cell: GrownCell, kinds: readonly PlacedKind[], ready: ReadyKinds, byKind: ReadonlyMap<number, readonly Plant[]>): void {const world = app.world, entity = cell.entity;if (entity === null) return;+ cell.assets ??= ready.lease.retain() ?? undefined;+ if (!cell.assets) return;for (const [kindIndex, plants] of byKind) {const kind = kinds[kindIndex], parts = ready.kinds[kindIndex];if (!kind || !parts) continue;@@ -168,6 +194,8 @@ export class PlantGrowth {growFar(app: App, cell: GrownCell, kinds: readonly PlacedKind[], ready: ReadyKinds, byKind: ReadonlyMap<number, readonly Plant[]>, _at?: { x: number; z: number; half?: number }, thinnedBy = 1): void {const entity = cell.entity;if (entity === null) return;+ cell.assets ??= ready.lease.retain() ?? undefined;+ if (!cell.assets) return;for (const [kindIndex, plants] of byKind) {const parts = ready.far[kindIndex], kind = kinds[kindIndex];if (!kind || !parts || parts.length === 0) continue;@@ -179,8 +207,13 @@ export class PlantGrowth {/** Lets a cell go: what it made is despawned, and what it had not yet made is left unmade. */release(app: App, cell: GrownCell): void {cell.dead = true;- if (cell.entity !== null && app.world.valid(cell.entity)) app.world.despawn(cell.entity);- cell.handles.length = 0;+ try {+ if (cell.entity !== null && app.world.valid(cell.entity)) app.world.despawn(cell.entity);+ } finally {+ cell.assets?.release();+ cell.assets = undefined;+ cell.handles.length = 0;+ }}/** One frame of it: the far versions shown where they should be, and the queue worked through while the frame's budget lasts, one piece at least. */diff --git a/sdk/src/worldgen/worldgenPlugin.ts b/sdk/src/worldgen/worldgenPlugin.tsindex e57761099..8ca805d15 100644--- a/sdk/src/worldgen/worldgenPlugin.ts+++ b/sdk/src/worldgen/worldgenPlugin.ts@@ -10,7 +10,7 @@* it is made nearest first within the frame's budget, let go past its `drawDistance`, and made again with its old chunk kept* until then when the ground or the water changes; a zone or a change touches only the chunks it reaches.*- * - Decoration is drawn merged: a mesh a chunk for each kind of plant or stone in it.+ * - Plant parts use shared geometry and per-instance LOD; other decoration is merged per mesh and material in a chunk.* - What a game interacts with is an entity of its own, carrying a `PlacedObject` with its placement id and kind, so the* game can find it, act on it and record what it did (`diffs.remove(id)` when it is felled).* - The ground is painted by the biomes, when the WorldGen says so.@@ -25,6 +25,7 @@ import {type ComponentDef, type MeshRendererData, type TransformData,} from '../ecs/component';import { Assets } from '../asset/AssetPlugin';+import { AssetScope, ownedAssetLease, type AssetLease } from '../asset/AssetLease';import type { MeshData } from '../asset/meshFormat';import { log } from '../util/logger';import type { Entity } from '../types';@@ -156,6 +157,9 @@ export interface WorldGenStatus {}interface BuiltWorld {+ assets: AssetLease<AssetScope>;+ retired: boolean;+ meshData: Map<number, MeshData | null | Promise<MeshData | null>>;signature: string;data: WorldGenData;rules: WorldRules | null;@@ -198,6 +202,7 @@ interface Unit {/** A chunk of a layer: what it places (its decoration merged by mesh, its interactive placements), and what it put in the world. */interface PlacedChunk {+ assets?: AssetLease<AssetScope>;/** A kind of decoration: its near mesh and, when it has one, its far cards; drawn near out to `lodDistance`, out to `drawDistance`. */decor: { mesh: number; material: number; data: MeshData; far: MeshData | null; origin: [number, number, number]; radius: number; lodDistance: number; drawDistance: number }[];objects: Placement[];@@ -314,9 +319,6 @@ export class WorldGenPlugin implements Plugin {/** Stops listening for edited rules files. */private watching_: (() => void) | null = null;- /** Each mesh's geometry, read once, for drawing it merged. */- private readonly meshData_ = new Map<number, MeshData | null | Promise<MeshData | null>>();-build(app: App): void {const gens = new WorldGens();gens.app_ = app;@@ -351,7 +353,12 @@ export class WorldGenPlugin implements Plugin {/** A WorldGen's rules and plan, or why it has none. */private make(app: App, gens: WorldGens, entity: Entity, data: WorldGenData, had: BuiltWorld | undefined): BuiltWorld {- const built: BuiltWorld = { signature: `${JSON.stringify(data)}#${worldRulesRevision()}`, data: { ...data }, rules: null, plan: null, farPlan: null, failure: null, ground: '', authored: had?.authored ?? new Set(), levelling: new Set(), plants: null, plantsReady: null, faces: null, waitingForPlanet: false };+ if (had) this.retire(had);+ const meshData: BuiltWorld['meshData'] = new Map();+ const assets = ownedAssetLease(`worldgen:${entity}`, (had?.assets.generation ?? 0) + 1, new AssetScope(), scope => {+ try { scope.releaseAll(); } finally { meshData.clear(); }+ });+ const built: BuiltWorld = { assets, retired: false, meshData, signature: `${JSON.stringify(data)}#${worldRulesRevision()}`, data: { ...data }, rules: null, plan: null, farPlan: null, failure: null, ground: '', authored: had?.authored ?? new Set(), levelling: new Set(), plants: null, plantsReady: null, faces: null, waitingForPlanet: false };// Rules in a project file are read first, then made like rules registered by code.if (isRulesFile(data.rules)) {if (!app.hasResource(Assets)) {@@ -367,9 +374,10 @@ export class WorldGenPlugin implements Plugin {for (const b of gens.built_.values()) if (isRulesFile(b.data.rules) && assets.resolveLoadPath(b.data.rules) === changed) b.signature = '';});}- void assets.load<WorldRules>('world-rules', data.rules).then((rules) => {- if (gens.built_.get(entity) !== built) return;- this.adopt(app, gens, entity, built, rules, had);+ void assets.acquireTyped<WorldRules>('world-rules', data.rules).then((receipt) => {+ if (built.retired || gens.built_.get(entity) !== built) { receipt.release(); return; }+ built.assets.value.add(receipt);+ this.adopt(app, gens, entity, built, receipt.value, had);}, (e: unknown) => {if (gens.built_.get(entity) !== built) return;built.failure = `the world rules file ${data.rules} could not be read: ${e instanceof Error ? e.message : String(e)}`;@@ -406,8 +414,11 @@ export class WorldGenPlugin implements Plugin {const set = expandPlants(rules.plants);built.plants = set;void retried(() => prepareKinds(app.getResource(Assets), set.kinds)).then((ready) => {- if (gens.built_.get(entity) === built) built.plantsReady = ready;+ if (built.retired || gens.built_.get(entity) !== built) { ready.lease.release(); return; }+ built.assets.value.add(ready.lease);+ built.plantsReady = ready;}, (e: unknown) => {+ if (built.retired) return;built.failure = `the plants' assets could not be loaded: ${e instanceof Error ? e.message : String(e)}`;log.error('worldgen', built.failure);});@@ -416,10 +427,12 @@ export class WorldGenPlugin implements Plugin {const refs = assetRefsOf(rules);if (refs.length > 0 && app.hasResource(Assets)) {void retried(() => this.resolve(app, rules, refs)).then((resolved) => {- if (gens.built_.get(entity) !== built) return;- built.rules = resolved;- this.plan(app, gens, entity, built, resolved);+ if (built.retired || gens.built_.get(entity) !== built) { resolved.scope.releaseAll(); return; }+ built.assets.value.absorb(resolved.scope);+ built.rules = resolved.rules;+ this.plan(app, gens, entity, built, resolved.rules);}, (e: unknown) => {+ if (built.retired) return;built.failure = `the recipes' assets could not be loaded: ${e instanceof Error ? e.message : String(e)}`;log.error('worldgen', built.failure);});@@ -429,19 +442,22 @@ export class WorldGenPlugin implements Plugin {}/** Loads the meshes and materials `refs` name, and gives back the rules with each path replaced by its handle. */- private async resolve(app: App, rules: WorldRules, refs: { kind: 'mesh' | 'material'; ref: string }[]): Promise<WorldRules> {+ private async resolve(app: App, rules: WorldRules, refs: { kind: 'mesh' | 'material'; ref: string }[]): Promise<{ rules: WorldRules; scope: AssetScope }> {const assets = app.getResource(Assets);+ const scope = new AssetScope();const handles = new Map<string, number>();- await Promise.all(refs.map(async ({ kind, ref }) => {- const handle = kind === 'mesh' ? (await assets.load<{ handle: number }>('mesh', ref)).handle : (await assets.loadMaterial(ref)).handle;- handles.set(`${kind}:${ref}`, handle);+ const results = await Promise.allSettled(refs.map(async ({ kind, ref }) => {+ const receipt = scope.add(await assets.acquireTyped<{ handle: number }>(kind, ref));+ handles.set(`${kind}:${ref}`, receipt.value.handle);}));+ const failed = results.find((result): result is PromiseRejectedResult => result.status === 'rejected');+ if (failed) { scope.releaseAll(); throw failed.reason; }const member = (m: RecipeMember): RecipeMember => ({...m,mesh: typeof m.mesh === 'string' ? handles.get(`mesh:${m.mesh}`) ?? 0 : m.mesh,material: typeof m.material === 'string' ? handles.get(`material:${m.material}`) ?? 0 : m.material,});- return { ...rules, recipes: rules.recipes.map((r) => ({ ...r, anchor: member(r.anchor), members: r.members?.map(member) })) };+ return { scope, rules: { ...rules, recipes: rules.recipes.map((r) => ({ ...r, anchor: member(r.anchor), members: r.members?.map(member) })) } };}/** Makes a WorldGen's plan from its rules, and what keeps it current. */@@ -714,9 +730,9 @@ export class WorldGenPlugin implements Plugin {// The plants' parts are read before any chunk: a chunk made without them would leave the plants out until it is made again.if (rendering && built.plants && !built.plantsReady) return null;const away = unit.away?.(layer.id, false, x0, z0, x0 + size, z0 + size);- if (away) return away.then((placed) => this.chunk(app, null, diffs, layer, x0, z0, size, rendering, built.plants, face, false, 1, placed));+ if (away) return this.ownChunk(built, () => away.then((placed) => this.chunk(app, built.meshData, null, diffs, layer, x0, z0, size, rendering, built.plants, face, false, 1, placed)));if (waiting || !known(x0, z0, size)) return null;- return this.chunk(app, plan, diffs, layer, x0, z0, size, rendering, built.plants, face);+ return this.ownChunk(built, () => this.chunk(app, built.meshData, plan, diffs, layer, x0, z0, size, rendering, built.plants, face));},publish: (_cx, _cz, value) => this.place(app, gens, entity, value),release: (_cx, _cz, value) => this.unplace(app, gens, value),@@ -743,9 +759,9 @@ export class WorldGenPlugin implements Plugin {if (!built.plantsReady) return null;const sparse = (layer.farSpacing ?? 1) > 1 && unit.farPlan;const away = unit.away?.(layer.id, true, x0, z0, x0 + fsize, z0 + fsize);- if (away) return away.then((placed) => this.chunk(app, null, diffs, layer, x0, z0, fsize, rendering, built.plants, face, true, sparse ? layer.farSpacing! ** 2 : 1, placed));+ if (away) return this.ownChunk(built, () => away.then((placed) => this.chunk(app, built.meshData, null, diffs, layer, x0, z0, fsize, rendering, built.plants, face, true, sparse ? layer.farSpacing! ** 2 : 1, placed)));if (waiting || !known(x0, z0, fsize)) return null;- return this.chunk(app, sparse ? unit.farPlan! : plan, diffs, layer, x0, z0, fsize, rendering, built.plants, face, true, sparse ? layer.farSpacing! ** 2 : 1);+ return this.ownChunk(built, () => this.chunk(app, built.meshData, sparse ? unit.farPlan! : plan, diffs, layer, x0, z0, fsize, rendering, built.plants, face, true, sparse ? layer.farSpacing! ** 2 : 1));},publish: (_cx, _cz, value) => this.place(app, gens, entity, value),release: (_cx, _cz, value) => this.unplace(app, gens, value),@@ -753,8 +769,19 @@ export class WorldGenPlugin implements Plugin {}}+ /** Pending and published chunks both borrow the generation they were built from. */+ private ownChunk(built: BuiltWorld, make: () => PlacedChunk | Promise<PlacedChunk>): PlacedChunk | Promise<PlacedChunk> | null {+ const assets = built.assets.retain();+ if (!assets) return null;+ const hold = (chunk: PlacedChunk): PlacedChunk => { chunk.assets = assets; return chunk; };+ try {+ const value = make();+ return value instanceof Promise ? value.then(hold, error => { assets.release(); throw error; }) : hold(value);+ } catch (error) { assets.release(); throw error; }+ }+/** What one chunk of a layer places: its decoration merged by mesh, and its interactive placements, with the changes laid over. */- private chunk(app: App, plan: WorldPlan | null, diffs: PlacementDiffs, layer: PlacementLayer, x0: number, z0: number, size: number,+ private chunk(app: App, meshData: BuiltWorld['meshData'], plan: WorldPlan | null, diffs: PlacementDiffs, layer: PlacementLayer, x0: number, z0: number, size: number,rendering: boolean, plants: PlantSet | null, face: PlanetFace | null, far = false, thinnedBy = 1, given?: readonly Placement[]): PlacedChunk | Promise<PlacedChunk> {const placements: Placement[] = [];// What is placed in the square: asked of the plan here, or already worked out (by a worker).@@ -795,7 +822,7 @@ export class WorldGenPlugin implements Plugin {}const origin = out.at;for (const list of byKind.values()) {- const source = this.meshData_.get(list[0]!.mesh);+ const source = meshData.get(list[0]!.mesh);if (!source || source instanceof Promise) continue;const plants: Plant[] = list.map((p, i) => ({x: p.x - origin[0], y: p.y - origin[1], z: p.z - origin[2], yaw: p.yaw, scale: p.scale, phase: (i * 0.618) % 1, shade: 1,@@ -811,18 +838,18 @@ export class WorldGenPlugin implements Plugin {}return out;};- const missing = meshes.filter((m) => !this.meshData_.has(m) || this.meshData_.get(m) instanceof Promise);+ const missing = meshes.filter((m) => !meshData.has(m) || meshData.get(m) instanceof Promise);if (missing.length === 0) return finish();const assets = app.getResource(Assets);return Promise.all(missing.map((m) => {- let got = this.meshData_.get(m);+ let got = meshData.get(m);if (got === undefined) {const read = assets.readMeshData(m).catch(() => null).then((d) => {if (!d) log.warn('worldgen', `the geometry of mesh ${m} cannot be read, so what it draws is left out`);- this.meshData_.set(m, d);+ meshData.set(m, d);return d;});- this.meshData_.set(m, read);+ meshData.set(m, read);got = read;}return got;@@ -902,6 +929,13 @@ export class WorldGenPlugin implements Plugin {for (const h of chunk.meshes) assets?.releaseBuiltMesh(h);chunk.entities.length = 0;chunk.meshes.length = 0;+ chunk.assets?.release();+ chunk.assets = undefined;+ }++ private retire(built: BuiltWorld): void {+ built.retired = true;+ built.assets.release();}private drop(app: App, gens: WorldGens, entity: Entity): void {@@ -911,6 +945,7 @@ export class WorldGenPlugin implements Plugin {if (built?.faces && app.hasResource(PlanetRegistryRes)) app.getResource(PlanetRegistryRes).setPaint(entity, null);}if (built && app.hasResource(TerrainsRes)) app.getResource(TerrainsRes).setPaint(null);+ if (built) this.retire(built);gens.built_.delete(entity);}diff --git a/sdk/tests/foliage-lifetime.test.ts b/sdk/tests/foliage-lifetime.test.tsnew file mode 100644index 000000000..32122dd57--- /dev/null+++ b/sdk/tests/foliage-lifetime.test.ts@@ -0,0 +1,148 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+import { describe, expect, it, vi } from 'vitest';+import { App } from '../src/app/app';+import { AppContext, setDefaultContext } from '../src/ecs/context';+import { setEditorMode, setPlayMode } from '../src/ecs/env';+import { Transform } from '../src/ecs/component';+import { Assets } from '../src/asset/AssetPlugin';+import type { Assets as AssetsApi } from '../src/asset/Assets';+import { ownedAssetLease } from '../src/asset/AssetLease';+import { MeshChannel, MeshChannelType, packChannels, type MeshData } from '../src/asset/meshFormat';+import { PlacedFoliage, PlacedFoliagePlugin } from '../src/foliage/placedFoliage';+import { prepareKinds, releaseKinds, PlantGrowth, type GrownCell } from '../src/foliage/plantGrowth';+import type { PlacedKind } from '../src/foliage/plantKind';+import { WorldGen, WorldGenPlugin, WorldGenRes } from '../src/worldgen/worldgenPlugin';+import type { WorldRules } from '../src/worldgen/rules';+import { WorldStreamingSource } from '../src/residency/components';+import { chunksOf } from '../src/residency/chunks';+import { createMockModule } from './mocks/wasm';++const kind: PlacedKind = {+ parts: [{ mesh: 'leaf.mesh', lod1Mesh: '', material: 'leaf.material', texture: 'leaf.png', soft: .5, tint: [1,1,1,1], opaque: true }],+ lodDistance: 20, drawDistance: 30, far: [], farDistance: 0,+};+const rules: WorldRules = {+ id: 'test:lifetime', versions: [1], plants: { leaf: kind },+ biomes: [{ id: 'plain', conditions: [], recipes: [{ recipe: 'leaf', layer: 'plants', density: 1 }] }],+ recipes: [{ id: 'leaf', version: 1, anchor: { id: 'leaf', plant: 'leaf', footprint: .1 } }],+ layers: [{ id: 'plants', spacing: 10, drawDistance: 30, chunkSize: 16 }],+};+function triangle(): MeshData {+ const { channels, vertexStride } = packChannels([+ { semantic: MeshChannel.Position, components: 3, type: MeshChannelType.Float32 },+ { semantic: MeshChannel.Normal, components: 3, type: MeshChannelType.Float32 },+ ]);+ return { channels, vertexStride, vertexCount: 3,+ vertices: new Uint8Array(new Float32Array([0,0,0,0,0,1, 1,0,0,0,0,1, 0,1,0,0,0,1]).buffer),+ indices: new Uint32Array([0,1,2]), aabbMin: [0,0,0], aabbMax: [1,1,0] };+}+function assetFixture() {+ let next = 1;+ const receipts = new Set<number>(), generated = new Set<number>(), handles = new Map<string, number>();+ const calls: string[] = [];+ let barrier: Promise<void> | undefined;+ let heldType = '';+ let fail = '';+ const acquire = async (type: string, path: string) => {+ calls.push(`${type}:${path}`);+ if (barrier && (!heldType || heldType === type)) await barrier;+ if (type === fail) throw new Error('test failed ' + type);+ const id = next++; receipts.add(id);+ const key = `${type}:${path}`;+ if (!handles.has(key)) handles.set(key, next++);+ const value = type === 'world-rules' ? rules : { handle: handles.get(key)! };+ return ownedAssetLease(key, 1, value, () => { receipts.delete(id); });+ };+ const api = {+ acquireTyped: acquire, acquireTexture: (path: string) => acquire('texture', path),+ readMeshData: async () => triangle(),+ createMesh: () => { const h = next++; generated.add(h); return h; },+ releaseBuiltMesh: (h: number) => { generated.delete(h); },+ onInvalidate: () => () => {}, resolveLoadPath: (path: string) => path,+ } as unknown as AssetsApi;+ return { api, receipts, generated, calls, hold: (p: Promise<void> | undefined, type = '') => { barrier = p; heldType = type; }, fail: (type: string) => { fail = type; } };+}+function boot(assets: AssetsApi) {+ setDefaultContext(new AppContext()); setEditorMode(false); setPlayMode(false);+ const app = App.new(), module = createMockModule(); app.connectCpp(module.getRegistry(), module);+ app.insertResource(Assets, assets);+ const source = app.world.spawn(); app.world.insert(source, Transform, {}); app.world.insert(source, WorldStreamingSource, {});+ return app;+}+const tick = async (app: App, count = 20) => { for (let n=0;n<count;n++) { await app.tick(1/60); await Promise.resolve(); } };++describe('owned asset receipts', () => {+ it('disposes once after the last holder and never resurrects a spent receipt', () => {+ const dispose = vi.fn(), owner = ownedAssetLease('mesh', 7, {}, dispose), child = owner.retain()!;+ owner.release(); owner.release(); expect(owner.retain()).toBeNull(); expect(dispose).not.toHaveBeenCalled();+ const last = child.retain()!; child.release(); expect(dispose).not.toHaveBeenCalled();+ last.release(); last.release(); expect(last.retain()).toBeNull(); expect(dispose).toHaveBeenCalledTimes(1);+ });+});+describe('foliage resource lifetime', () => {+ it('shares assets and softened geometry across tint variants, then returns every acquisition', async () => {+ const f=assetFixture(), ready=await prepareKinds(f.api,[kind,kind]);+ expect(f.calls).toEqual(['mesh:leaf.mesh','material:leaf.material','texture:leaf.png']);+ expect(ready.kinds[0]![0]!.mesh).toBe(ready.kinds[1]![0]!.mesh);+ expect(f.generated.size).toBe(1); releaseKinds(ready);+ expect(f.receipts.size).toBe(0); expect(f.generated.size).toBe(0);+ });+ it('rolls back partial preparation, including composed meshes, if a material fails', async () => {+ const f=assetFixture(); f.fail('material');+ await expect(prepareKinds(f.api,[kind])).rejects.toThrow('test failed');+ expect(f.receipts.size).toBe(0); expect(f.generated.size).toBe(0);+ });+ it('keeps a retiring generation alive for a published cell and cancels its queued growth on release', async () => {+ const f=assetFixture(),app=boot(f.api),ready=await prepareKinds(f.api,[kind]),growth=new PlantGrowth();+ const e=app.world.spawn();app.world.insert(e,Transform,{});+ const cell:GrownCell={entity:e,handles:[],dead:false};+ growth.growCell(app,cell,[kind],ready,new Map([[0,[{x:0,y:0,z:0,yaw:0,scale:1,phase:0,shade:1}]]]));+ releaseKinds(ready);expect(f.generated.size).toBe(1);expect(f.receipts.size).toBe(3);+ growth.release(app,cell);growth.step(app);expect(growth.made).toBe(0);+ expect(f.generated.size).toBe(0);expect(f.receipts.size).toBe(0);+ });+ it('releases late preparation after a placed field has disappeared', async () => {+ const f=assetFixture();let resume!:()=>void;f.hold(new Promise<void>(r=>{resume=r;}));+ const app=boot(f.api);app.addPlugin(new PlacedFoliagePlugin());const e=app.world.spawn();+ app.world.insert(e,Transform,{});app.world.insert(e,PlacedFoliage,{kinds:[kind],records:[0,0,0,0,0,1]});+ await tick(app,1);app.world.despawn(e);await tick(app,1);f.hold(undefined);resume();await tick(app);+ expect(f.receipts.size).toBe(0);expect(f.generated.size).toBe(0);+ });+ it('bounds resources across generated-world seed changes and releases them after removal', async () => {+ const f=assetFixture(),app=boot(f.api);app.addPlugin(new WorldGenPlugin());const e=app.world.spawn();+ app.world.insert(e,Transform,{});app.world.insert(e,WorldGen,{rules:'forest.esworld',world:'lifetime',seed:1});+ await tick(app,60);expect(app.getResource(WorldGenRes).status(e)?.state).toBe('ready');+ const baseline=f.receipts.size;expect(baseline).toBe(4);expect(f.generated.size).toBe(1);+ for(let seed=2;seed<6;seed++){+ app.world.insert(e,WorldGen,{rules:'forest.esworld',world:'lifetime',seed});await tick(app,60);+ expect(f.receipts.size).toBe(baseline);expect(f.generated.size).toBe(1);+ }+ app.world.despawn(e);await tick(app);expect(f.receipts.size).toBe(0);expect(f.generated.size).toBe(0);+ expect(chunksOf(app).pending).toBe(0);+ });+ it('releases a late world-rules acquisition after the generator was removed', async () => {+ const f=assetFixture();let resume!:()=>void;f.hold(new Promise<void>(r=>{resume=r;}));+ const app=boot(f.api);app.addPlugin(new WorldGenPlugin());const e=app.world.spawn();+ app.world.insert(e,Transform,{});app.world.insert(e,WorldGen,{rules:'forest.esworld',world:'late'});+ await tick(app,1);app.world.despawn(e);await tick(app,1);f.hold(undefined);resume();await tick(app);+ expect(f.receipts.size).toBe(0);expect(f.generated.size).toBe(0);+ });+ it('releases late plant preparation after a generated world was removed', async () => {+ const f=assetFixture();let resume!:()=>void;f.hold(new Promise<void>(r=>{resume=r;}),'mesh');+ const app=boot(f.api);app.addPlugin(new WorldGenPlugin());const e=app.world.spawn();+ app.world.insert(e,Transform,{});app.world.insert(e,WorldGen,{rules:'forest.esworld',world:'late-plants'});+ await tick(app,4);expect(f.calls).toContain('mesh:leaf.mesh');app.world.despawn(e);await tick(app,1);+ f.hold(undefined);resume();await tick(app);+ expect(f.receipts.size).toBe(0);expect(f.generated.size).toBe(0);+ });+ it('releases replaced placed-foliage generations after their cells have changed', async () => {+ const f=assetFixture(),app=boot(f.api);app.addPlugin(new PlacedFoliagePlugin());const e=app.world.spawn();+ app.world.insert(e,Transform,{});+ for(let x=0;x<4;x++) {+ app.world.insert(e,PlacedFoliage,{sizeX:32,sizeZ:32,cellSize:16,kinds:[kind],records:[0,x,0,0,0,1]});+ await tick(app,30);expect(f.receipts.size).toBe(3);expect(f.generated.size).toBe(1);+ }+ app.world.despawn(e);await tick(app);expect(f.receipts.size).toBe(0);expect(f.generated.size).toBe(0);+ });+});