Free a redrawn picture only once no shader names it (#293)
Editing a vector drawing made Pixi warn "destroyed while still bound
to a shader", twice with every change, once it had begun. It began
where an object wears the drawing through a shader asset. Each
change draws the picture anew and destroyed the one before it, but
the mesh of that object keeps its picture bound until it syncs. The
issue also asks for the button "Add shape" to go: a shape without
points cannot be given any, the Pen draws a new one.
- GeneratedPictures takes two more callbacks. inUse says that a
picture cannot be freed yet, soon runs a retry once whoever asked
is done.
- A replaced picture that is still in use waits in a list of its
own. It is freed right after the redraw that asked, as a rule. On
a later ask where its wearer was not part of that redraw.
- TextureLibrary tells a bound picture by the listeners for change
on its source. Under WebGL the bind groups are the only ones that
listen there. The retry is a microtask.
- The rule is asked for every drawn picture alike, whoever wears it.
- The inspector of a drawing and of a shape has no "Add shape" any
more. "Delete shapes" for several selected shapes stays.
Decided here:
- Under the list of shapes a second note says "The Pen draws a new
shape." while only the asset is selected.
Found on the way:
- Reproduced on a throwaway copy of the project the report came
from: 6 warnings for 3 changes with the shader override on the
object, none without it. After the change none, and no replaced
picture is left waiting.
- The fixture project of the tests never showed the warning. No
object there wore a drawing through a shader.
Tests:
- Gate on melektaus-atx: 211 files, 3219 tests (3209 + 10), exit 0
- e2e 47,48 on melektaus-atx: 113 checks, all pass
- e2e 28,30,31,39,40,47,48: 350 checks, all pass, run before the
wording of the new note changed
- The first gate was red: an existing test read the wording of the
note that had changed. Only three test files had been run after
the change.
- No full run and no smokes. The sum of 1668 (1667 + 1) is reckoned,
1667 is what CI counted for ec2fd90.
- Ten unit tests, five of them against the real bind group of Pixi
in tests/viewport/bound-pictures.test.ts
- One e2e check in block 47b-ii of module 47: a drawing worn through
a shader, three changes, no warning. The check on "Add shape" now
reads that there is no such button.
- Nine unit counter-probes and two browser counter-probes all fail
their tests. Without the change the new check counts 6 warnings.
- Not checked: a tilemap or a particle emitter holding a picture
bound the same way. Nothing was looked at after the build, the
new note included.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ec2fd9084f
commit
9e869df063
11 files changed
+364
-46
No files matched your search
@@ -130,6 +130,8 @@ export function renderHooks({ app, state, editor }: HookContext) {
|
||||
kept: texture !== null,
|
||||
};
|
||||
},
|
||||
/** Drawn pictures whose revision moved on while a shader still named them (#293); freed on a later ask. */
|
||||
picturesWaiting: () => textureLibrary.waitingPictures(),
|
||||
fps: () => editor.fps,
|
||||
/**
|
||||
* What the render loop did: frames actually drawn, and whether it is currently awake. The
|
||||
|
||||
@@ -10,7 +10,6 @@
|
||||
*/
|
||||
import {
|
||||
AddVectorLayerCommand,
|
||||
AddVectorShapeCommand,
|
||||
CombineVectorShapesCommand,
|
||||
DuplicateVectorShapeCommand,
|
||||
MoveVectorLayerCommand,
|
||||
@@ -66,7 +65,7 @@ import { removeShapes } from '../actions';
|
||||
import { FramePreview } from '../framePreview';
|
||||
import { listFold } from '../sectionFolds';
|
||||
import { type EditorState, shapeSelection } from '../state';
|
||||
import { activeLayerOf, activeVectorLayer, pickVectorLayer } from '../vectorart/activeLayer';
|
||||
import { activeVectorLayer, pickVectorLayer } from '../vectorart/activeLayer';
|
||||
import { groupBlocker, groupShapes, groupsToDissolve, ungroupShapes } from '../vectorart/grouping';
|
||||
import { GradientBar } from '../vectorart/GradientBar';
|
||||
import { ShapeList } from '../vectorart/ShapeList';
|
||||
@@ -276,8 +275,8 @@ export function buildShapeListSection(host: InspectorHost, asset: VectorArtAsset
|
||||
addLayerList(host, asset, '');
|
||||
addShapeList(host, asset, '', []);
|
||||
addNote(host, 'The top row is drawn over the ones below it.', 'small');
|
||||
addGap(host);
|
||||
host.body.add(addShapeButton(host, asset.id));
|
||||
// No button adds a shape (#293): one without points could not be given any.
|
||||
addNote(host, 'The Pen draws a new shape.', 'small');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -358,16 +357,17 @@ export function buildShapeSection(
|
||||
);
|
||||
} else addNote(host, 'Without an outline only the fill is drawn.', 'small');
|
||||
|
||||
addGap(host);
|
||||
// One shape goes by the cross on its row; several at once only from here.
|
||||
if (shapeIds.length > 1)
|
||||
// One shape goes by the cross on its row; several at once only from here. There is no button
|
||||
// that adds one (#293): a shape without points could not be given any, the Pen draws a new one.
|
||||
if (shapeIds.length > 1) {
|
||||
addGap(host);
|
||||
host.body.add(
|
||||
new Button('Delete shapes', {
|
||||
variant: 'danger',
|
||||
onClick: () => removeShapes(host.state, assetId, shapeIds),
|
||||
}),
|
||||
);
|
||||
host.body.add(addShapeButton(host, assetId));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -596,17 +596,6 @@ function removeShape(host: InspectorHost, assetId: string, itemId: string): void
|
||||
host.state.select(rest.length > 0 ? shapeSelection(assetId, rest) : { kind: 'asset', assetId });
|
||||
}
|
||||
|
||||
function addShapeButton(host: InspectorHost, assetId: string): Button {
|
||||
return new Button('Add shape', {
|
||||
onClick: () => {
|
||||
const asset = findAssetOf(host.state.project, assetId, 'vectorart');
|
||||
const layer = asset && activeLayerOf(host.state, asset);
|
||||
const command = new AddVectorShapeCommand(assetId, [], undefined, layer?.id);
|
||||
if (host.state.execute(command)) host.state.select(shapeSelection(assetId, [command.shapeId]));
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* The name row. Not `addNameField`: the shape's name also stands in the list right above it, and a
|
||||
* blank one is refused by the command, which cannot put the old text back into the field.
|
||||
|
||||
@@ -42,7 +42,7 @@ export class ShapeList extends SortableList {
|
||||
* the row pressed and whether it was pressed with Ctrl or Shift, which adds the shape to the
|
||||
* selection or takes it out (#187). `onDrop` is told where a dragged row landed. `onDuplicate` and
|
||||
* `onRemove` are the marks at the end of a row, `onRename` a name edited in its row. There is no
|
||||
* plus: a shape is drawn with the Pen, and "Add shape" stays below.
|
||||
* plus: a shape is drawn with the Pen.
|
||||
*/
|
||||
constructor(
|
||||
private readonly state: EditorState,
|
||||
|
||||
@@ -29,10 +29,15 @@ export class TextureLibrary extends FileLibrary<Texture> {
|
||||
/** Frames cut from their sheets, keyed by file and rectangle; kept like the files. */
|
||||
private readonly crops = new Map<string, Texture>();
|
||||
/** Pictures drawn instead of uploaded (#185); a new revision replaces the one before it. */
|
||||
private readonly generated = new GeneratedPictures<Texture>((texture, key) => {
|
||||
this.dropCropsOf(key);
|
||||
texture.destroy(true);
|
||||
});
|
||||
private readonly generated = new GeneratedPictures<Texture>(
|
||||
(texture, key) => {
|
||||
this.dropCropsOf(key);
|
||||
texture.destroy(true);
|
||||
},
|
||||
boundToShader,
|
||||
// After the redraw that asked: every mesh it synced wears the new picture by then.
|
||||
(retry) => queueMicrotask(retry),
|
||||
);
|
||||
|
||||
constructor() {
|
||||
super('/game-maker/data/textures', 'textures');
|
||||
@@ -97,6 +102,11 @@ export class TextureLibrary extends FileLibrary<Texture> {
|
||||
return this.loaded.get(file) ?? this.generated.peek(file);
|
||||
}
|
||||
|
||||
/** The keys of drawn pictures that are replaced but still bound to a shader (#293, test hook). */
|
||||
waitingPictures(): string[] {
|
||||
return this.generated.waitingKeys();
|
||||
}
|
||||
|
||||
/**
|
||||
* A frame as a texture of its own, cut the first time it is asked for. Drawn from the sheet
|
||||
* through a fill matrix it showed a row of the neighbouring cell along any edge that fell
|
||||
@@ -160,6 +170,17 @@ export class TextureLibrary extends FileLibrary<Texture> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a shader still names the picture (#293). An object with a shader asset keeps its picture
|
||||
* bound until its next sync, and so does Pixi's own shader for whatever mesh it drew last without
|
||||
* the batcher; destroying the source under either makes Pixi warn "destroyed while still bound to
|
||||
* a shader", for the source and for its sampler. Under WebGL the bind groups are the only ones
|
||||
* listening for `change` on a source, which is how one is told from here.
|
||||
*/
|
||||
function boundToShader(texture: Texture): boolean {
|
||||
return texture.source.listenerCount('change') > 0;
|
||||
}
|
||||
|
||||
function cropKey(file: string, rect: FrameRect): string {
|
||||
return `${file}|${rect.x},${rect.y},${rect.w},${rect.h}`;
|
||||
}
|
||||
|
||||
@@ -5,6 +5,11 @@
|
||||
* One picture per asset, keyed by `gen:<assetId>@<revision>`. A new revision is a new key, and
|
||||
* making it frees the one the asset had before – that is the whole invalidation rule. The library
|
||||
* owns what a generator hands it: it destroys the picture when the revision moves on.
|
||||
*
|
||||
* One that is still drawn with at that moment waits (#293): whoever wears a picture learns of the
|
||||
* new revision by asking for it, so the old one is always still in its hands when the new one is
|
||||
* made. It is freed once it has been let go: right after the ask as a rule, on a later ask where
|
||||
* the wearer was not part of that redraw.
|
||||
*/
|
||||
import type { Texture } from 'pixi.js';
|
||||
import type { Asset } from '../core/model/Asset';
|
||||
@@ -17,9 +22,21 @@ export type PictureFactory<T> = (assetId: string) => T | null;
|
||||
export class GeneratedPictures<T> {
|
||||
private readonly kept = new Map<string, { assetId: string; picture: T }>();
|
||||
private factory: PictureFactory<T> | null = null;
|
||||
/** Pictures whose revision has moved on while something still drew with them. */
|
||||
private waiting: { key: string; picture: T }[] = [];
|
||||
private asked = false;
|
||||
|
||||
/** `free` gives a picture back, by key, once its asset has moved on to a newer revision. */
|
||||
constructor(private readonly free: (picture: T, key: string) => void) {}
|
||||
/**
|
||||
* `free` gives a picture back, by key, once its asset has moved on to a newer revision. `inUse`
|
||||
* says that it cannot go yet; it is asked again on every later `get` until it can. `soon` runs
|
||||
* a retry once whoever asked is done – the wearer that made the picture wait has taken the new
|
||||
* one by then, so the old one does not lie about until the next redraw.
|
||||
*/
|
||||
constructor(
|
||||
private readonly free: (picture: T, key: string) => void,
|
||||
private readonly inUse: (picture: T) => boolean = () => false,
|
||||
private readonly soon: (retry: () => void) => void = () => {},
|
||||
) {}
|
||||
|
||||
/** Installs who draws; a session change swaps it, and everything drawn for the old one goes. */
|
||||
setFactory(factory: PictureFactory<T> | null): void {
|
||||
@@ -29,6 +46,7 @@ export class GeneratedPictures<T> {
|
||||
|
||||
/** The picture for this key, drawn on the first ask. Null for anything that is not a key. */
|
||||
get(key: string): T | null {
|
||||
if (this.waiting.length > 0) this.freeWaiting();
|
||||
const kept = this.kept.get(key);
|
||||
if (kept) return kept.picture;
|
||||
const parsed = parseGeneratedKey(key);
|
||||
@@ -51,10 +69,23 @@ export class GeneratedPictures<T> {
|
||||
return [...this.kept.keys()];
|
||||
}
|
||||
|
||||
/** The keys of the pictures that wait to be freed, oldest first – what a test reads. */
|
||||
waitingKeys(): string[] {
|
||||
return this.waiting.map((held) => held.key);
|
||||
}
|
||||
|
||||
clear(): void {
|
||||
for (const key of this.keys()) this.drop(key);
|
||||
}
|
||||
|
||||
private freeWaiting(): void {
|
||||
this.waiting = this.waiting.filter((held) => {
|
||||
if (this.inUse(held.picture)) return true;
|
||||
this.free(held.picture, held.key);
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
/** Every revision of one asset, however old. */
|
||||
private release(assetId: string): void {
|
||||
for (const [key, held] of this.kept) if (held.assetId === assetId) this.drop(key);
|
||||
@@ -64,7 +95,14 @@ export class GeneratedPictures<T> {
|
||||
const held = this.kept.get(key);
|
||||
if (!held) return;
|
||||
this.kept.delete(key);
|
||||
this.free(held.picture, key);
|
||||
if (!this.inUse(held.picture)) return this.free(held.picture, key);
|
||||
this.waiting.push({ key, picture: held.picture });
|
||||
if (this.asked) return;
|
||||
this.asked = true;
|
||||
this.soon(() => {
|
||||
this.asked = false;
|
||||
this.freeWaiting();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user