Free open-source image editor for the browser
Show each layer's opacity in its row, and keep what Remove filled (#78)
Rodrigo Belfiore38cb82874d8a
diff --git a/API.md b/API.mdindex 5fabd13..e429c94 100644--- a/API.md+++ b/API.md@@ -66,7 +66,7 @@ Create an empty Heal effect with `addLayer("heal")`.`addHealStroke(id, patchId, stroke)` appends a painted or erased `BrushStroke` to an existing patch as one undoable edit. It preserves the mode, donor, feather, and opacity. Feather is a patch-wide blend based on the first stroke's diameter, shared by later additions. All patches expose an ordered `strokes` array; each path stays separate, coverage is combined on the GPU, and moving the patch moves paint and erase together. In the editor, Shift adds and Alt/Option subtracts on the selected patch, committing on release. Alt-click chooses a donor for Heal/Clone when no patch is selected. Older saved scenes with a single `stroke` load as one-element arrays.-`addRemovePatch(id, stroke)` appends a Remove patch and returns its ID. It synthesizes the covered region from unmasked surrounding context using multiscale GPU PatchMatch; no offset or manual source is needed. `setHealSource` rejects Remove patches. The same feather, opacity, destination, duplicate, and delete edits apply. The solver and compositor run through `createEditorRenderer` without React; [the solver documentation](src/features/heal/inpaint/algorithm.md) describes limits and reproducible benchmarks.+`addRemovePatch(id, stroke)` appends a Remove patch and returns its ID. It synthesizes the covered region from unmasked surrounding context using multiscale GPU PatchMatch; no offset or manual source is needed. `setHealSource` rejects Remove patches. The same feather, opacity, destination, duplicate, and delete edits apply. In the editor, the first full-resolution render synthesizes where each pixel copies from, and `getState()` then shows it as the patch's `field`, naming its stored texels: changes to the layers below, undo, export, and scene files keep it, while the copied colors follow those layers. `exportImage` and `exportScene` wait for a field still being read back. A stroke `addHealStroke` adds keeps what the field filled and synthesizes only the rest; moving the patch synthesizes it anew. The solver and compositor run through `createEditorRenderer` without React; [the solver documentation](src/features/heal/inpaint/algorithm.md) describes limits and reproducible benchmarks.Create an empty Paint layer with `addLayer("paint")`. `addPaintStroke(id, stroke)` appends a `BrushStroke` with a `color`, `#rrggbb` sRGB: a painting stroke lays that color over the layer's earlier paint, and an erasing one removes paint whatever its color. `setPaintBlend(id, blend)` blends the layer's paint over the image below with the modes `setFill` takes, on encoded values as Photoshop does. A paint stroke builds up as a mask's does. In the editor, a layer's strokes settle into pixels once it has 100 and no gesture is open: `getState()` then shows a `raster` ID and fewer strokes, undo still reaches every stroke, and scene files keep the pixels. A photo holds up to 4 Paint layers and 10 brush masks; adding more throws.diff --git a/ARCHITECTURE.md b/ARCHITECTURE.mdindex 136f71a..ed8fb48 100644--- a/ARCHITECTURE.md+++ b/ARCHITECTURE.md@@ -27,7 +27,7 @@ Where the code lives and the rules that keep it in place. Keep this file true: w| `features/heal/modes.ts` | Retouch names, descriptions, and order: Remove, Heal, Clone. The initial mode, desktop buttons, mobile tabs, and shortcut cycle share this order; choosing a mode is remembered across tool visits. || `app/editor/brush.tsx` | The Brush tool: one canvas that sends color or mask strokes to the feature edits its mode picks, and its options. || `app/editor/dock.tsx` | The mobile dock's tabs and what it shows: the layer stack, a tool's options, or the selected layer's dials. |-| `app/editor/renderer.ts` | Composes feature passes into the preview and export pipelines and tracks immutable upstream content for derived-image caches. |+| `app/editor/renderer.ts` | Composes feature passes into the preview and export pipelines. || `app/editor/mask-overlay.tsx` | The only writer of the mask overlay, derived from the selection. || `app/editor/layers.ts` | The effect kinds the app offers and their layer factories. || `app/editor/export` | The export view. |@@ -35,7 +35,7 @@ Where the code lives and the rules that keep it in place. Keep this file true: w| `components/ui` | What [`@roprgm/ui`](https://ui.roprgm.com) lacks; see [DESIGN.md](DESIGN.md). || `core/document` | Scene contract, layer tree, history, resources. || `core/image` | Image sources, decoding, geometry, color. |-| `core/renderer` | Render nodes, graph execution, masks, proxy, transform, display. |+| `core/renderer` | Render nodes, graph execution, masks, Remove fields, proxy, transform, display. || `features/<name>` | One capability: usually `model.ts` (parameters, defaults), `edits.ts`, `pass.ts` with its `.wgsl`, and `controls.tsx`. || `lib` | Utilities independent of OpenLight. || `tests` | `*.test.ts` run in Bun with `vgpu/mock`; `*.e2e.ts` run in Chromium; `fixtures/` holds test images. |@@ -74,7 +74,7 @@ Dependencies point downward. Features do not import each other; `app/` connects## Rendering- `core/renderer` defines passes as nodes (`node`, `pipeline`, `split`, `merge`); the graph owns intermediate textures and timing. Features declare passes; `app/editor/renderer.ts` connects them.-- Expensive derived nodes can cache their result by explicit immutable content dependencies. A cache hit prunes the upstream branch; the graph owns at most two revisions per node, releases them with the composition instance, and reports them separately in `inspect()`. Hidden layers retain their instances, including descendant layers and Healing patches. The renderer adds RAW development revision and proxy factor to the keys; app composition supplies upstream content, and the feature supplies its own geometry. Feather and opacity do not invalidate a Remove patch's synthesis. Remove caches only its bounded correspondence field, at most 4 MiB per revision, and samples donor color during composition.+- Hidden layers retain their composition instances, including descendant layers and Healing patches.- The graph keeps intermediate textures for the last two sets of sizes it rendered, so interactive proxy renders and full renders alternate without reallocating full-size textures, which a phone's GPU memory cannot absorb gesture after gesture.- Use `vgpu` for GPU work and `vgpu-react` for bindings. Create pipelines once per engine and reuse them. Keep `.wgsl` beside its owner.- The working space is linear Rec.2020 in `rgba16float`. Decoders convert into it and display converts out; passes preserve format and primaries unless they explicitly convert.@@ -85,7 +85,8 @@ Dependencies point downward. Features do not import each other; `app/` connects- The adjustment shader's parameters use UI units; its fitted constants are calibration data.- Strokes are scene content; the renderer caches their rasterized coverage, or a Paint layer's premultiplied 8-bit color, at half the source's resolution each way, a quarter of its pixels, which the passes that read them sample back to full size, and stamps only appended dabs. A Paint layer's or brush mask's raster comes with its first paint and stays, cleared if need be, until the layer goes, so painting and undoing allocate nothing. A photo holds up to 4 Paint layers and 10 brush masks.- A mask's or Paint layer's latest stroke builds up in a half-float stroke buffer at the same resolution, one stroke at a time; its raster takes it in one pass when the next stroke starts, so it rounds to 8 bits once, however many moves drew it. Until then a Paint layer composes the buffer over its raster, and a mask's readers get its raster with the stroke laid over, in a view redrawn where the stroke grows. A Healing or Clone patch records its mode, reuses the same coverage, and keeps editable feather and opacity. Clone copies donor color directly, skipping the correction passes. A Healing patch records ordered hard paint/erase strokes, stamped through the shared GPU brush engine into one raster over only the 256 px tiles its paint reaches. Appending a gesture stamps only new dabs; undo or moved geometry replays the sequence. Shift adds and Alt/Option subtracts on the selected patch, preserving its donor and blend; separate strokes stay separate. The local SVG preview composes the same ordered operations and shows union boundaries and erased holes. Scene loading migrates earlier single-stroke patches once at the boundary.-- Remove patches store hard paint/erase strokes without a donor offset. `features/heal/inpaint` builds a local, multiscale PatchMatch graph, entirely on the GPU: exclusion mask, preserved texture descriptors, nearest valid donor initialization, parallel propagation/random search, overlap voting, and final full-source sampling. Matching is bounded to a 512 px long edge; the output still samples the original source. The Heal compositor applies the patch's coverage, feather, and opacity. No processing depends on React or reads pixels to the CPU. See [the solver](src/features/heal/inpaint/algorithm.md) for its constraints and standalone benchmarks.+- Remove patches store hard paint/erase strokes without a donor offset. `features/heal/inpaint` synthesizes a field of each hole texel's offset to its donor with a local, multiscale PatchMatch graph, entirely on the GPU: exclusion mask, preserved texture descriptors, nearest valid donor initialization, parallel propagation/random search, and overlap voting. Matching is bounded to 512 texels along the long side; the output samples donors at full source resolution. The Heal compositor applies the patch's coverage, feather, and opacity.+- A Remove field is synthesized once per set of strokes, from the image below the patch at full resolution, and kept: the renderer holds it, and once no gesture is open `features/heal` reads it back and the patch names it as a document resource, as settled paint is, without a history step; a stroke added during the readback takes it as its base. The layers below, a proxy, hiding, undo, export, and a reopened scene all use a saved field and never solve again. The document keeps each readback in flight, so export and scene files wait for it, and a draft saves again once the field changes the scene. Composition samples donor color from the current image below, so the patch follows changes under it, such as exposure. A stroke added to or erased from the patch keeps the texels the earlier field filled, on the same lattice while it fits, as long as they stay in the hole and their donors stay clean, and synthesizes only the rest; moving the patch synthesizes it anew. Synthesis needs no React and no readback. See [the solver](src/features/heal/inpaint/algorithm.md) for its constraints and standalone benchmarks.- A brush mask paints coverage as a Paint layer paints color: both are a painting, the latest strokes over settled pixels, in one raster module (r8 for coverage, rgba8 for color), which also owns the open stroke coverage view. At 100 strokes, once no gesture is open, the renderer reads a painting's raster band by band through one mapped buffer, and the pixels, deflated, become a document resource the layer names in place of its strokes, without a history step. Rendering waits until both the scene and cached raster accept the settled pixels. History scenes name the pixels they drew over, so undo reaches past a settle: the renderer loads those pixels, inflating them band by band through a staging texture, and draws the strokes again. Resources go once no retained scene names them.- A mask that only gradients shape needs no texture: the mix pass computes it, children included. A mask with a painted brush or a range combines in the render graph: each child's coverage, a brush's cached raster, a gradient drawn by a node, or a range read from the image below the mask, folds into the group's in stored order through one combine pass, and the mix pass shares its add, subtract, and intersect. The group takes the image's resolution when a range takes part, since a range follows the photo's edges, and the brushes' otherwise. The renderer gives it out, with each range's own coverage, for the overlay and thumbnails. While a color is picked, renders also keep the image below the mask group, which the picker reads through one GPU pass and 16 bytes of readback per pick.- Open history groups render a reduced proxy. Every render image carries `scale`, its source pixels per texel; shaders that take document coordinates or radii apply it.diff --git a/CONTEXT.md b/CONTEXT.mdindex bc952d8..8d1c493 100644--- a/CONTEXT.md+++ b/CONTEXT.md@@ -9,12 +9,12 @@ OpenLight edits documents containing image content and adjustments.| **Layer** | A source, effect, or mask in the composition. Image layers own basic adjustments; effects process the image below; masks scope their basic adjustments and child effects. Linear, radial, brush, and range child masks add, subtract, or intersect coverage. A Paint layer is an effect that lays its strokes' colors over the image with a blend mode. Layers compose render nodes without owning textures. || **Stroke** | One brush drag: paint or erase, size, feather, flow, and points with pressure in source pixels; a Paint layer's strokes also carry a color. Strokes are content; the renderer rasterizes them into a cached texture at half the source's resolution, coverage for a mask and premultiplied color for a Paint layer, and the latest stroke into a shared stroke buffer until the next one starts. A Paint layer's or brush mask's strokes settle into pixels every 100 strokes, which it then paints over; undo still reaches every stroke. A Healing patch stores ordered hard paint/erase strokes, rasterized over only the tiles its paint reaches, and feathers its accumulated shape separately. || **Range** | A mask that selects pixels of the image below its mask group by lightness, a luminance range, or by color, a color range, rather than by position. A new color range has no coverage until it takes a color from a click on the photo. |-| **Healing patch** | One painted, non-destructive repair in a Healing layer. Its Heal mode copies a nearby donor, found automatically or set by hand, with a bounded boundary color correction. Clone mode copies the donor without color correction. Remove mode synthesizes many local donor patches from surrounding texture and has no donor offset. Patches replay in order and each sees the result of the preceding patches. Each gesture keeps its recorded size and separate path; Shift adds and Alt/Option subtracts on the selected patch. Feather and opacity remain editable on its accumulated shape. |+| **Healing patch** | One painted, non-destructive repair in a Healing layer. Its Heal mode copies a nearby donor, found automatically or set by hand, with a bounded boundary color correction. Clone mode copies the donor without color correction. Remove mode synthesizes many local donor patches from surrounding texture and has no donor offset; it keeps where each pixel copies from as its field, so the layers below change only the copied colors, and a stroke added or erased keeps what it already filled. Patches replay in order and each sees the result of the preceding patches. Each gesture keeps its recorded size and separate path; Shift adds and Alt/Option subtracts on the selected patch. Feather and opacity remain editable on its accumulated shape. || **Image source** | An image used as input to the document's composition. It is content within a document. || **LUT** | A 3D color lookup table, such as a creative grade, read from a `.cube` file. A LUT layer holds one and applies it to the image below, at the layer's opacity. || **Edit** | One reversible content change. A continuous slider or curve drag is one edit, as is importing a set of adjustments. |-| **Scene file** | A document saved as an `.openlight` archive: its scene, the original source files, and the pixels its paint settled into. Opening it creates a document that continues editing; history and preview settings are not saved. |-| **Draft** | The latest edited document, kept in the browser as a scene with its source files and settled paint so it survives closing the tab. The start screen offers to recover or forget it; recovering opens a new document with empty history. |+| **Scene file** | A document saved as an `.openlight` archive: its scene, the original source files, the pixels its paint settled into, and its Remove fields. Opening it creates a document that continues editing; history and preview settings are not saved. |+| **Draft** | The latest edited document, kept in the browser as a scene with its source files, settled paint, and Remove fields so it survives closing the tab. The start screen offers to recover or forget it; recovering opens a new document with empty history. || **Workspace** | The open-document session. It currently holds at most one document, replaced when another image or scene file is opened. || **Tool** | What a drag on the canvas does: Brush paints colors on a Paint layer or coverage on a brush mask, as its Color or Mask mode says, Healing repairs, and Linear and Radial gradient draw masks and edit the selected gradient's guides. Adjust leaves the canvas to pan and zoom. Crop and Export bring their own view. Switching tools never changes the sidebar; Brush and Healing create their layer on entry and remove it again if it leaves untouched. A selected mask is *active*; it is *edited* only while a shape tool is on. |diff --git a/PERFORMANCE.md b/PERFORMANCE.mdindex 88065ca..6fc573f 100644--- a/PERFORMANCE.md+++ b/PERFORMANCE.md@@ -27,6 +27,6 @@ Run `bun run test:browser --config playwright.bench.config.ts` apart from other## Object removal-`bun run test:browser --config playwright.bench.config.ts --grep 'inpainting|rendering remove'` runs three headless workloads. `inpainting solver` measures a fresh local synthesis and mask raster without app composition; `inpainting editor` measures the same moving, 240 px patch in a 2400×1600 scene through `createEditorRenderer`, including the final blend. Both move the stroke one pixel each sample to require a new solve. `rendering remove` retains an unchanged stroke, measuring composition after the synthesis cache is populated. All use 8 warmups and 40 samples, separate first render and CPU encoding, and profile timestamps in a second run. None mounts React. Images are already resident on the GPU; decoding, display, and export are excluded.+`bun run test:browser --config playwright.bench.config.ts --grep 'inpainting|rendering remove'` runs three headless workloads. `inpainting solver` measures a fresh local synthesis and mask raster without app composition; `inpainting editor` measures the same moving, 240 px patch in a 2400×1600 scene through `createEditorRenderer`, including the final blend. Both move the stroke one pixel each sample to require a new solve. `rendering remove` retains an unchanged stroke, measuring composition once the field is synthesized. All use 8 warmups and 40 samples, separate first render and CPU encoding, and profile timestamps in a second run. None mounts React. Images are already resident on the GPU; decoding, display, and export are excluded.-Cached results appear as `cachedTextures` in `inspect()`; include them alongside `textures` when calculating memory. Remove caches a correspondence field rather than a full-resolution RGB crop (at most 4 MiB per revision). A full/proxy pair or the last two content revisions is bounded per patch; hiding the layer retains them, and removing the patch releases them. Matching uses a local grid capped at 512 px rather than scanning the whole photograph.+Remove keeps one field per patch, offsets rather than a full-resolution RGB crop (at most 1 MiB), among the `rasters` of `inspect()`; include them alongside `textures` when calculating memory. Hiding the layer retains it, and removing the patch releases it. Matching uses a local grid capped at 512 texels rather than scanning the whole photograph.diff --git a/bun.lock b/bun.lockindex 08cc8c0..4fddd08 100644--- a/bun.lock+++ b/bun.lock@@ -8,7 +8,7 @@"@base-ui/react": "^1.8.0","@dnd-kit/core": "^6.3.1","@fontsource-variable/geist": "^5.3.0",- "@roprgm/ui": "^0.8.0",+ "@roprgm/ui": "^0.8.2","@tailwindcss/vite": "^4.3.3","@vercel/analytics": "^2.0.1","@vercel/speed-insights": "^2.0.0",@@ -136,7 +136,7 @@"@rolldown/pluginutils": ["@rolldown/[email protected]", "", {}, "sha512-2j9bGt5Jh8hj+vPtgzPtl72j0yRxHAyumoo6TNfAjsLB04UtpSvPbPcDcBMxz7n+9CYB0c1GxQFxYRg2jimqGw=="],- "@roprgm/ui": ["@roprgm/[email protected]", "", { "dependencies": { "@base-ui/react": "^1.8.0", "class-variance-authority": "^0.7.1", "cn": "^0.4.0" }, "peerDependencies": { "react": ">=19" } }, "sha512-jSeDGGJaJH8qZ6bKSqLZW6I+jxgC95Vpjd4kRLf8HToYfWHAY8GYiAMChxXdgiZDjW84e7BRo4YYHPmYYlMTqw=="],+ "@roprgm/ui": ["@roprgm/[email protected]", "", { "dependencies": { "@base-ui/react": "^1.8.0", "class-variance-authority": "^0.7.1", "cn": "^0.4.0" }, "peerDependencies": { "react": ">=19" } }, "sha512-+Pup7MTh3n8dcciORDGluVeXLHT09h1pdC3MDD/xwTzx3javT5MH59Jet22Z2gKEkDexzTumi0hxufaBmDwmzg=="],"@standard-schema/spec": ["@standard-schema/[email protected]", "", {}, "sha512-l2aFy5jALhniG5HgqrD6jXLi/rUWrKvqN/qJx6yoJsgKhblVd+iqqU4RCXavm/jPityDo5TCvKMnpjKnOriy0w=="],diff --git a/package.json b/package.jsonindex 4df5917..4702b45 100644--- a/package.json+++ b/package.json@@ -15,7 +15,7 @@"@base-ui/react": "^1.8.0","@dnd-kit/core": "^6.3.1","@fontsource-variable/geist": "^5.3.0",- "@roprgm/ui": "^0.8.0",+ "@roprgm/ui": "^0.8.2","@tailwindcss/vite": "^4.3.3","@vercel/analytics": "^2.0.1","@vercel/speed-insights": "^2.0.0",diff --git a/src/app/assistant/index.tsx b/src/app/assistant/index.tsxindex 3c2d9c4..89ea0a1 100644--- a/src/app/assistant/index.tsx+++ b/src/app/assistant/index.tsx@@ -159,7 +159,7 @@ export function Assistant({<divstyle={open ? { height } : undefined}className={cn(- "fixed bottom-3 left-14 z-50 overflow-clip surface-panel bg-level-4/95 backdrop-blur-sm transition-[width,height,border-radius] duration-300 ease-[cubic-bezier(0.2,0,0,1)] motion-reduce:transition-none max-md:hidden",+ "fixed bottom-3 left-14 z-50 overflow-clip material-panel bg-level-4/95 backdrop-blur-sm transition-[width,height,border-radius] duration-300 ease-[cubic-bezier(0.2,0,0,1)] motion-reduce:transition-none max-md:hidden",open ? "w-80 rounded-xl" : "size-10 rounded-[1.25rem]",)}>diff --git a/src/app/draft/store.ts b/src/app/draft/store.tsindex f4b2555..0b01239 100644--- a/src/app/draft/store.ts+++ b/src/app/draft/store.ts@@ -22,10 +22,10 @@ export type Draft = { record: DraftRecord; files: ReadonlyMap<string, Blob> };/** A draft of the document shown as `name`, captured synchronously so the document may change or close meanwhile. */export function snapshotDraft(document: EditorDocument, name: string) {- const { json, sources, paint } = snapshotScene(document);+ const { json, sources, paint, fields } = snapshotScene(document);return {record: { version, name, scene: json },- files: new Map<string, Blob>([...sources, ...paint]),+ files: new Map<string, Blob>([...sources, ...paint, ...fields]),};}@@ -52,8 +52,8 @@ interface DraftDatabase extends DBSchema {}/**- * One draft in IndexedDB: the record under a single key, and the files its scene names, its source- * and the pixels paint settled into, keyed by ID.+ * One draft in IndexedDB: the record under a single key, and the files its scene names, its source,+ * the pixels paint settled into, and Remove fields, keyed by ID.* Operations run one at a time in call order, so a discard never races a save.*/export function createDraftStore(name = "openlight") {diff --git a/src/app/editor/comparison-divider.tsx b/src/app/editor/comparison-divider.tsxindex 3a03c04..7c03a82 100644--- a/src/app/editor/comparison-divider.tsx+++ b/src/app/editor/comparison-divider.tsx@@ -64,13 +64,13 @@ export function ComparisonDivider() {}}><div className="absolute inset-y-0 w-px bg-foreground/90 shadow-[0_0_3px_var(--color-level-1)]" />- <span className="absolute right-6 bottom-4 rounded bg-backdrop px-1.5 py-0.5 text-foreground">+ <span className="absolute right-6 bottom-4 rounded backdrop px-1.5 py-0.5 text-foreground">Before</span>- <span className="absolute bottom-4 left-6 rounded bg-backdrop px-1.5 py-0.5 text-foreground">+ <span className="absolute bottom-4 left-6 rounded backdrop px-1.5 py-0.5 text-foreground">After</span>- <span className="relative flex size-7 items-center justify-center rounded-full surface-float text-foreground group-focus-visible:ring-2 group-focus-visible:ring-foreground">+ <span className="relative flex size-7 items-center justify-center rounded-full material-float text-foreground group-focus-visible:ring-2 group-focus-visible:ring-foreground"><svgaria-hidden="true"viewBox="0 0 20 20"diff --git a/src/app/editor/dock.tsx b/src/app/editor/dock.tsxindex 2639549..885b307 100644--- a/src/app/editor/dock.tsx+++ b/src/app/editor/dock.tsx@@ -49,7 +49,7 @@ export function DockTabList({<Tabsvalue={selected}onValueChange={onSelect}- className="@container surface-panel"+ className="@container material-panel"><TabListaria-label="Tools"diff --git a/src/app/editor/export/export-image.ts b/src/app/editor/export/export-image.tsindex 0e61a06..48c9250 100644--- a/src/app/editor/export/export-image.ts+++ b/src/app/editor/export/export-image.ts@@ -1,6 +1,10 @@import type { Gpu, Target } from "vgpu";import { createEditorRenderer } from "@/app/editor/renderer";-import { type EditorDocument, settledPixels } from "@/core/document";+import {+ type EditorDocument,+ fieldTexels,+ settledPixels,+} from "@/core/document";import type { Point } from "@/core/image/frame";import { renderBitmap } from "@/core/renderer";@@ -80,10 +84,12 @@ export async function exportImage(document: EditorDocument,options: ExportOptions = {},) {+ await document.replaced();const scene = document.scene.getState();const source = document.resources.get(scene.layers[0].source);// Taken with the scene: another photo may close the document before the renderer loads them.const pixels = settledPixels(document, scene);+ const fields = fieldTexels(document, scene);const renderer = createEditorRenderer(gpu, source, {paintPixels: (id) => {const blob = pixels.get(id);@@ -92,6 +98,13 @@ export async function exportImage(}return blob;},+ fieldTexels: (id) => {+ const blob = fields.get(id);+ if (!blob) {+ throw Error("Remove field is unavailable.");+ }+ return blob;+ },});try {await renderer.update(scene);diff --git a/src/app/editor/header.tsx b/src/app/editor/header.tsxindex d4f7d32..ed75b3e 100644--- a/src/app/editor/header.tsx+++ b/src/app/editor/header.tsx@@ -15,7 +15,7 @@ export function EditorHeader({children?: ReactNode;}) {return (- <header className="flex h-11 shrink-0 items-center gap-3 border-edge border-b surface-panel px-3">+ <header className="flex h-11 shrink-0 items-center gap-3 border-edge border-b material-panel px-3"><span className="flex items-center gap-2"><img src="/logo.svg" alt="" className="size-5 shrink-0" />{/* On a phone the logo alone names the app, leaving room for the file and its actions. */}diff --git a/src/app/editor/index.tsx b/src/app/editor/index.tsxindex d78ad72..5c87cb4 100644--- a/src/app/editor/index.tsx+++ b/src/app/editor/index.tsx@@ -21,6 +21,7 @@ import { EditorViewport, ViewportStage } from "@/components/editor/viewport";import type { Mask } from "@/core/document";import { locateLayer } from "@/core/document";import { HealingProvider, useHealBrush } from "@/features/heal/mode";+import { RemoveFieldSaving } from "@/features/heal/save-fields";import { addLayer } from "@/features/layers/edits";import { MaskToolProvider, type Nesting } from "@/features/layers/mask-tool";import { CanvasToolbar } from "@/features/layers/toolbar";@@ -220,6 +221,7 @@ function DocumentEditor({onDismiss={onDismissFailure}/><PaintSettling />+ <RemoveFieldSaving /><BrushKeys /><ToolShortcuts /></HealingTools>diff --git a/src/app/editor/renderer.ts b/src/app/editor/renderer.tsindex 67cdb7a..bb8e831 100644--- a/src/app/editor/renderer.ts+++ b/src/app/editor/renderer.ts@@ -1,4 +1,4 @@-import type { Gpu, Timer } from "vgpu";+import type { Gpu } from "vgpu";import {maskModifiers,type ProcessingLayer,@@ -6,13 +6,13 @@ import {} from "@/core/document";import type { ImageSource } from "@/core/image";import {- type CacheKey,type Composition,createRenderer,input,maskInput,mixAdjustment,pipeline,+ type RendererOptions,type RenderImage,transformImages,} from "@/core/renderer";@@ -31,7 +31,6 @@ import { vignette } from "@/features/vignette/pass";type Branch = {image: RenderImage;input?: RenderImage;- dependencies: readonly CacheKey[];rangeSource?: RenderImage;};@@ -39,7 +38,6 @@ function composeLayer(below: RenderImage,layer: ProcessingLayer,composition: Composition,- dependencies: readonly CacheKey[],): Branch {const name = `layer/${layer.id}`;composition.retain(name);@@ -67,14 +65,12 @@ function composeLayer(retainHealPatches(hidden.patches, instance, composition);}}- return { image: below, rangeSource, dependencies };+ return { image: below, rangeSource };}let input: RenderImage | undefined;let edited = below;- let content = dependencies;switch (layer.kind) {case "details":- content = [...dependencies, layer.details];edited = pipeline(below, [unsharpMask(`${name}/clarity`, layer.details.clarity / 200, 64, 16),unsharpMask(@@ -85,7 +81,6 @@ function composeLayer(]);break;case "mask": {- content = [...dependencies, layer.adjustments, layer.toneCurve];const adjusted = pipeline(below, [adjustments(layer.adjustments, name)]);if (layer.id === composition.inputId) {input = maskInput(name, adjusted, layer.mask, masks, coverage);@@ -96,33 +91,26 @@ function composeLayer(break;}case "exposure":- content = [...dependencies, layer.exposure];edited = pipeline(below, [exposure(`${name}/exposure`, layer.exposure)]);break;case "vignette":- content = [...dependencies, layer.vignette];edited = pipeline(below, [vignette(layer.vignette, `${name}/vignette`)]);break;case "grain":- content = [...dependencies, layer.grain];edited = pipeline(below, [grain(layer.grain, `${name}/grain`)]);break;case "color-mixer":- content = [...dependencies, layer.colorMixer];edited = pipeline(below, [colorMixer(layer.colorMixer, `${name}/color-mixer`),]);break;case "fill":- content = [...dependencies, layer.fill];edited = pipeline(below, [fill(layer.fill, `${name}/fill`)]);break;case "lut":- content = [...dependencies, layer.lut];edited = pipeline(below, [lut(layer.lut, `${name}/lut`)]);break;case "paint":- content = [...dependencies, layer.blend, layer.strokes, layer.raster];edited = paint(below,composition.paint(layer),@@ -131,14 +119,7 @@ function composeLayer();break;case "heal": {- content = [...dependencies, ...layer.patches];- const result = heal(- below,- layer.patches,- name,- composition,- dependencies,- );+ const result = heal(below, layer.id, layer.patches, name, composition);edited = result.image;input = result.input;break;@@ -149,7 +130,7 @@ function composeLayer(layer.kind === "mask"? layer.children.filter((child) => child.kind !== "mask"): layer.children;- const children = composeLayers(edited, effects, composition, content);+ const children = composeLayers(edited, effects, composition);const image = mixAdjustment(name,below,@@ -162,7 +143,6 @@ function composeLayer(return {image,input: input ?? children.input,- dependencies: [...children.dependencies, layer],rangeSource: rangeSource ?? children.rangeSource,};}@@ -171,20 +151,17 @@ function composeLayers(below: RenderImage,layers: readonly ProcessingLayer[],composition: Composition,- dependencies: readonly CacheKey[],): Branch {let image = below;let input: RenderImage | undefined;- let content = dependencies;let rangeSource: RenderImage | undefined;for (const layer of layers) {- const branch = composeLayer(image, layer, composition, content);+ const branch = composeLayer(image, layer, composition);image = branch.image;input ??= branch.input;- content = branch.dependencies;rangeSource ??= branch.rangeSource;}- return { image, input, rangeSource, dependencies: content };+ return { image, input, rangeSource };}/**@@ -195,10 +172,7 @@ function composeLayers(export function createEditorRenderer(gpu: Gpu,source: ImageSource,- {- timer,- paintPixels,- }: { timer?: Timer; paintPixels?: (id: string) => Blob } = {},+ options: RendererOptions = {},) {return createRenderer(gpu,@@ -217,14 +191,8 @@ export function createEditorRenderer(developed,sourceLayer.children,composition,- [sourceLayer.adjustments, sourceLayer.toneCurve],- );- const composite = composeLayers(- children.image,- layers,- composition,- children.dependencies,);+ const composite = composeLayers(children.image, layers, composition);const full = composite.image;const [original, output] = transformImages([input(source.image), full],@@ -241,7 +209,6 @@ export function createEditorRenderer(rangeSource: children.rangeSource ?? composite.rangeSource,};},- timer,- paintPixels,+ options,);}diff --git a/src/app/editor/tool-rail.tsx b/src/app/editor/tool-rail.tsxindex afc61c8..ccfc7b9 100644--- a/src/app/editor/tool-rail.tsx+++ b/src/app/editor/tool-rail.tsx@@ -54,7 +54,7 @@ export function ToolTabList({<Tabsvalue={selected}onValueChange={onSelect}- className="flex w-11 shrink-0 flex-col gap-1 overflow-auto border-edge border-r surface-panel p-1.5"+ className="flex w-11 shrink-0 flex-col gap-1 overflow-auto border-edge border-r material-panel p-1.5"><TabList aria-label="Tools" className="flex-col">{editing.map((entry) => (diff --git a/src/app/loaders/scene.ts b/src/app/loaders/scene.tsindex b3d47ad..83442e1 100644--- a/src/app/loaders/scene.ts+++ b/src/app/loaders/scene.ts@@ -16,11 +16,12 @@ function isSceneFile(file: File) {}/**- * The document as a ZIP archive: a deflated `scene.json`, each source's bytes at `sources/<id>`, and- * the already deflated pixels paint settled into at `paint/<id>`.+ * The document as a ZIP archive: a deflated `scene.json`, each source's bytes at `sources/<id>`, the+ * already deflated pixels paint settled into at `paint/<id>`, and Remove fields at `fields/<id>`.*/export async function writeSceneFile(document: EditorDocument) {- const { json, sources, paint } = snapshotScene(document);+ await document.replaced();+ const { json, sources, paint, fields } = snapshotScene(document);const { writeZip } = await import("@/lib/zip");const archive = await writeZip([{@@ -30,6 +31,7 @@ export async function writeSceneFile(document: EditorDocument) {},...[...sources].map(([id, data]) => ({ name: `sources/${id}`, data })),...[...paint].map(([id, data]) => ({ name: `paint/${id}`, data })),+ ...[...fields].map(([id, data]) => ({ name: `fields/${id}`, data })),]);const [file] = sources.values();const name = file.name.replace(/\.[^.]*$/, "") || "scene";@@ -49,7 +51,7 @@ export async function openSceneFile(const files = new Map([...entries].flatMap(([name, data]) => {const [folder, id] = name.split("/");- return (folder === "sources" || folder === "paint") && id+ return ["sources", "paint", "fields"].includes(folder) && id? [[id, data] as const]: [];}),diff --git a/src/app/scene-file.ts b/src/app/scene-file.tsindex a2c5337..4b762f8 100644--- a/src/app/scene-file.ts+++ b/src/app/scene-file.ts@@ -3,8 +3,10 @@ import {createDocument,createResources,type EditorDocument,+ fieldTexels,type ProcessingLayer,paintingOf,+ removePatches,type Scene,settledPixels,walkLayers,@@ -61,7 +63,10 @@ export type SceneJson = {scene: Scene;};-/** The document's scene, the source file it references, and the pixels its paint settled into, read without rendering. */+/**+ * The document's scene, the source file it references, the pixels its paint settled into, and its+ * Remove fields, read without rendering.+ */export function snapshotScene(document: EditorDocument) {const scene = document.scene.getState();const { source } = scene.layers[0];@@ -76,6 +81,7 @@ export function snapshotScene(document: EditorDocument) {json,sources: new Map([[source, file]]),paint: settledPixels(document, scene),+ fields: fieldTexels(document, scene),};}@@ -202,8 +208,8 @@ const savedSchema = z.extend(header, {/*** Opens a saved scene as a new document, validating every value as the edit that made it.- * Scene files and drafts both open through here; `files` holds each source's bytes, and the pixels- * each paint layer settled into, by ID.+ * Scene files and drafts both open through here; `files` holds each source's bytes, the pixels each+ * paint layer settled into, and the texels of each Remove field, by ID.*/export async function openScene(saved: unknown,@@ -246,6 +252,15 @@ export async function openScene(resources.addPaint(pixels, raster);}}+ for (const { patch } of removePatches(layers)) {+ if (patch.field) {+ const texels = files.get(patch.field.texels);+ if (!texels) {+ throw Error("The scene's Remove fields are missing.");+ }+ resources.addField(texels, patch.field.texels);+ }+ }return createDocument({frame: scene.frame,diff --git a/src/components/editor/dock.tsx b/src/components/editor/dock.tsxindex b0cdb24..bc6bf5a 100644--- a/src/components/editor/dock.tsx+++ b/src/components/editor/dock.tsx@@ -32,7 +32,7 @@ export function EditorDock({inert={inert}aria-hidden={inert}data-inert={inert}- className="flex max-h-1/2 shrink-0 flex-col border-edge border-t surface-panel *:not-last:shadow-[inset_0_-1px_0_var(--color-edge)] data-[inert=true]:*:opacity-50"+ className="flex max-h-1/2 shrink-0 flex-col border-edge border-t material-panel *:not-last:shadow-[inset_0_-1px_0_var(--color-edge)] data-[inert=true]:*:opacity-50"><Inert value={Boolean(inert)}>{children}</Inert></div>diff --git a/src/components/editor/panel.tsx b/src/components/editor/panel.tsxindex ddb625b..626f6ed 100644--- a/src/components/editor/panel.tsx+++ b/src/components/editor/panel.tsx@@ -35,7 +35,7 @@ export function EditorPanel({aria-hidden={inert}data-inert={inert}className={cn(- "relative flex min-h-0 w-(--width) shrink-0 flex-col border-edge border-l surface-panel data-[inert=true]:*:opacity-50",+ "relative flex min-h-0 w-(--width) shrink-0 flex-col border-edge border-l material-panel data-[inert=true]:*:opacity-50",className,)}style={{ "--width": `${width}px` } as CSSProperties}diff --git a/src/components/editor/pipeline.tsx b/src/components/editor/pipeline.tsxindex d9ff7dd..f3a4728 100644--- a/src/components/editor/pipeline.tsx+++ b/src/components/editor/pipeline.tsx@@ -10,7 +10,7 @@ import type { Gpu } from "vgpu";import { useGpu } from "vgpu-react";import { adjustmentTarget } from "@/core/document";import type { ImageSource } from "@/core/image";-import type { createRenderer } from "@/core/renderer";+import type { createRenderer, RendererOptions } from "@/core/renderer";import { useDisposable } from "@/hooks/use-disposable";import { useDocument, useScene } from "./session";@@ -39,7 +39,7 @@ type RendererProviderProps = {createRenderer: (gpu: Gpu,source: ImageSource,- options: { paintPixels: (id: string) => Blob },+ options: RendererOptions,) => ReturnType<typeof createRenderer>;};@@ -55,6 +55,7 @@ export function RendererProvider({() =>createRenderer(gpu, source, {paintPixels: (id) => document.resources.paint(id),+ fieldTexels: (id) => document.resources.field(id),}),[gpu, document, source, createRenderer],);diff --git a/src/components/editor/viewport.tsx b/src/components/editor/viewport.tsxindex fcf67e4..aa54b8d 100644--- a/src/components/editor/viewport.tsx+++ b/src/components/editor/viewport.tsx@@ -25,7 +25,7 @@ function ZoomControl() {const fitted = view.zoom === 1;return (<div className="absolute right-3 bottom-3">- <div className="flex items-center rounded-full surface-panel bg-level-4/80 p-0.5 backdrop-blur-sm">+ <div className="flex items-center rounded-full material-panel bg-level-4/80 p-0.5 backdrop-blur-sm"><IconButtonlabel="Zoom out"side="top"diff --git a/src/components/ui/color-swatch.tsx b/src/components/ui/color-swatch.tsxindex 27968b8..8033b40 100644--- a/src/components/ui/color-swatch.tsx+++ b/src/components/ui/color-swatch.tsx@@ -31,7 +31,7 @@ export function ColorSwatch({<span{...props}className={cn(- "relative block size-5 shrink-0 rounded-sm surface-control focus-ring",+ "relative block size-5 shrink-0 rounded-sm material-control focus-ring",className,)}style={{ backgroundColor: value }}diff --git a/src/components/ui/dial.tsx b/src/components/ui/dial.tsxindex cfd5088..85b0b4b 100644--- a/src/components/ui/dial.tsx+++ b/src/components/ui/dial.tsx@@ -233,7 +233,7 @@ export function Dial({><spanclassName={cn(- "relative grid place-items-center rounded-full surface-field tabular-nums transition group-data-[scrubbing=true]:scale-108",+ "relative grid place-items-center rounded-full material-field tabular-nums transition group-data-[scrubbing=true]:scale-108",color ? "size-9" : "size-11.5",edited ? "text-foreground" : "text-muted",)}diff --git a/src/components/ui/tree-drag.tsx b/src/components/ui/tree-drag.tsxindex 9d8cb55..fd2747e 100644--- a/src/components/ui/tree-drag.tsx+++ b/src/components/ui/tree-drag.tsx@@ -122,7 +122,7 @@ export function TreeDrag({ children, canDrop, onDrop, label }: TreeDragProps) {<Context value={context}>{children}</Context><DragOverlay dropAnimation={null}>{active && (- <div className="max-w-64 rounded-md surface-float px-3 py-2">+ <div className="max-w-64 rounded-md material-float px-3 py-2">{label(active)}</div>)}diff --git a/src/core/document/index.ts b/src/core/document/index.tsindex cba4a32..ca9195f 100644--- a/src/core/document/index.ts+++ b/src/core/document/index.ts@@ -3,7 +3,7 @@ import { validateFrame } from "@/core/image/frame";import { createHistory } from "./history";import { createResources } from "./resources";import type { Mask, MaskModifier, Scene } from "./scene";-import { findLayer, paintingOf, walkLayers } from "./tree";+import { findLayer, paintingOf, removePatches, walkLayers } from "./tree";export type {Adjustments,@@ -34,6 +34,8 @@ export type {ProcessingLayer,RadialGradient,RangeMask,+ RemoveField,+ RemovePatch,Scene,StrokePoint,ToneCurve,@@ -48,6 +50,7 @@ export {locateLayer,maskModifiers,paintingOf,+ removePatches,updateLayer,walkLayers,} from "./tree";@@ -95,7 +98,10 @@ function equal(a: unknown, b: unknown): boolean {);}-/** The resources a scene names: its image source and the pixels its paintings settled into. */+/**+ * The resources a scene names: its image source, the pixels its paintings settled into, and its+ * Remove fields.+ */function resourceIds(scene: Scene) {const ids = [scene.layers[0].source];for (const { layer } of walkLayers(scene.layers)) {@@ -104,6 +110,11 @@ function resourceIds(scene: Scene) {ids.push(raster);}}+ for (const { patch } of removePatches(scene.layers)) {+ if (patch.field) {+ ids.push(patch.field.texels);+ }+ }return ids;}@@ -122,6 +133,23 @@ export function settledPixels(return pixels;}+/** The Remove fields `scene` names, taken now, like `settledPixels`. */+export function fieldTexels(+ document: EditorDocument,+ scene = document.scene.getState(),+) {+ const texels = new Map<string, Blob>();+ for (const { patch } of removePatches(scene.layers)) {+ if (patch.field) {+ texels.set(+ patch.field.texels,+ document.resources.field(patch.field.texels),+ );+ }+ }+ return texels;+}+/** One independent editing session. No React, decoders, or file workflows. */export function createDocument(initial: Scene, resources = createResources()) {const scene = createStore(() => initial);@@ -140,6 +168,7 @@ export function createDocument(initial: Scene, resources = createResources()) {selection.setState({ layerId: state.layers[0].id });}});+ const inFlight = new Set<Promise<unknown>>();let closed = false;return {id: crypto.randomUUID(),@@ -184,6 +213,18 @@ export function createDocument(initial: Scene, resources = createResources()) {}replace(next);},+ /** Keeps `work` that will replace the scene in place, such as a Remove field read back, for `replaced`. */+ replacing(work: Promise<unknown>) {+ inFlight.add(work);+ const done = () => inFlight.delete(work);+ void work.then(done, done);+ },+ /** Resolves once in-place replacements in flight finish, so a snapshot holds what the editor shows. */+ async replaced() {+ while (inFlight.size) {+ await Promise.allSettled(inFlight);+ }+ },/** Whether the document was disposed, so work that outlived it can drop its result. */get closed() {return closed;diff --git a/src/core/document/resources.ts b/src/core/document/resources.tsindex 2b72dfd..058f0c5 100644--- a/src/core/document/resources.ts+++ b/src/core/document/resources.ts@@ -1,19 +1,23 @@import type { ImageSource } from "@/core/image";/**- * Owns the document's image files and GPU targets, and the deflated pixels paint layers settled into,- * outside scene history.+ * Owns the document's image files and GPU targets, the deflated pixels paint layers settled into, and+ * the deflated texels of Remove fields, outside scene history.*/export function createResources() {const images = new Map<string, { file: File } & ImageSource>();const paints = new Map<string, Blob>();+ const fields = new Map<string, Blob>();let disposed = false;+ function open() {+ if (disposed) {+ throw new Error("Document is closed.");+ }+ }return {/** A saved document restores each source under its original ID. */add(file: File, source: ImageSource, id: string = crypto.randomUUID()) {- if (disposed) {- throw new Error("Document is closed.");- }+ open();if (images.has(id)) {throw new Error("Image resource already exists.");}@@ -29,9 +33,7 @@ export function createResources() {},/** A saved document restores settled paint under its original ID too. */addPaint(pixels: Blob, id: string = crypto.randomUUID()) {- if (disposed) {- throw new Error("Document is closed.");- }+ open();paints.set(id, pixels);return id;},@@ -42,6 +44,19 @@ export function createResources() {}return pixels;},+ /** A saved document restores Remove fields under their original IDs too. */+ addField(texels: Blob, id: string = crypto.randomUUID()) {+ open();+ fields.set(id, texels);+ return id;+ },+ field(id: string) {+ const texels = fields.get(id);+ if (!texels) {+ throw new Error("Remove field is unavailable.");+ }+ return texels;+ },/** Frees what no retained scene names. */retain(ids: ReadonlySet<string>) {for (const [id, source] of images) {@@ -50,9 +65,11 @@ export function createResources() {images.delete(id);}}- for (const id of paints.keys()) {- if (!ids.has(id)) {- paints.delete(id);+ for (const stored of [paints, fields]) {+ for (const id of stored.keys()) {+ if (!ids.has(id)) {+ stored.delete(id);+ }}}},@@ -63,6 +80,7 @@ export function createResources() {}images.clear();paints.clear();+ fields.clear();},};}diff --git a/src/core/document/scene.ts b/src/core/document/scene.tsindex d684bca..43a32b5 100644--- a/src/core/document/scene.ts+++ b/src/core/document/scene.ts@@ -115,6 +115,22 @@ export type Painting<S extends BrushStroke = BrushStroke> = {/** Coverage painted with strokes; the renderer rasterizes them into a cached texture. */export type BrushMask = { readonly kind: "brush" } & Painting;export type HealMode = "heal" | "clone" | "remove";+/**+ * Where each pixel of a Remove patch copies from, synthesized once and kept, so the layers below and+ * the renderer can't change what the patch shows; the copied colors still follow the layers below.+ */+export type RemoveField = {+ /** The document resource of each texel's offset to its donor, in texels; zero keeps the pixel. */+ readonly texels: string;+ /** How many of the patch's strokes, from the first, it was synthesized for; later strokes extend it. */+ readonly strokes: number;+ /** Where its first texel's corner sits, in source pixels. */+ readonly origin: Point;+ /** Source pixels per texel. */+ readonly scale: number;+ /** Texels across and down. */+ readonly size: Point;+};/** A painted repair: Heal/Clone use a donor offset; Remove synthesizes surrounding texture. */export type HealPatch = {readonly id: string;@@ -125,8 +141,9 @@ export type HealPatch = {readonly opacity: number;} & (| { readonly mode: "heal" | "clone"; readonly offset: Point }- | { readonly mode: "remove" }+ | { readonly mode: "remove"; readonly field?: RemoveField });+export type RemovePatch = Extract<HealPatch, { mode: "remove" }>;/*** The pixels of the image below the mask group whose lightness, in 0..100 UI units from black to* white, lies within `low` to `high`, fading out over `smoothness` past either end.diff --git a/src/core/document/tree.ts b/src/core/document/tree.tsindex f91795f..651aa56 100644--- a/src/core/document/tree.ts+++ b/src/core/document/tree.ts@@ -6,6 +6,7 @@ import type {Painting,ProcessingLayer,RangeMask,+ RemovePatch,Scene,} from "./scene";@@ -26,6 +27,18 @@ export function* walkLayers(}}+/** Every Remove patch with the Healing layer that holds it. */+export function* removePatches(+ layers: readonly Layer[],+): Generator<{ layer: Layer; patch: RemovePatch }> {+ for (const { layer } of walkLayers(layers)) {+ if (layer.kind !== "heal") continue;+ for (const patch of layer.patches) {+ if (patch.mode === "remove") yield { layer, patch };+ }+ }+}+export function locateLayer(layers: readonly Layer[], id: string) {for (const location of walkLayers(layers)) {if (location.layer.id === id) {diff --git a/src/core/renderer/fields.ts b/src/core/renderer/fields.tsnew file mode 100644index 0000000..493c133--- /dev/null+++ b/src/core/renderer/fields.ts@@ -0,0 +1,137 @@+import type { Gpu, Target } from "vgpu";+import type { BrushStroke, RemoveField } from "@/core/document";+import { input, type RenderInput } from "./node";+import { createRasterCache } from "./raster-cache";+import { copyRaster, readRaster, writeRaster } from "./transfer";++/** Where a field's texels sit in the photo. */+export type FieldLattice = Pick<RemoveField, "origin" | "scale" | "size">;++/** A Remove field a renderer holds and the strokes it was synthesized for. */+export type HeldField = {+ texels: RenderInput;+ lattice: FieldLattice;+ strokes: readonly BrushStroke[];+};++type Entry = {+ target: Target;+ lattice: FieldLattice;+ strokes: readonly BrushStroke[];+};++/** Whether `strokes` start with every stroke of `first`, the same objects. */+function startsWith(+ strokes: readonly BrushStroke[],+ first: readonly BrushStroke[],+) {+ return (+ first.length <= strokes.length &&+ first.every((stroke, i) => stroke === strokes[i])+ );+}++/**+ * Holds one Remove field per patch, synthesized by a render or loaded from the document, with the+ * strokes it was synthesized for. A field for the patch's first strokes is the base that later+ * strokes extend.+ */+export function createFieldStore(gpu: Gpu) {+ const fields = createRasterCache<Entry>(gpu, "rg16float", (target) => ({+ target,+ lattice: { origin: [0, 0], scale: 1, size: target.size },+ strokes: [],+ }));+ function usable(+ key: string,+ strokes: readonly BrushStroke[],+ saved?: RemoveField,+ ) {+ const entry = fields.get(key);+ if (!entry?.strokes.length || !startsWith(strokes, entry.strokes)) {+ return undefined;+ }+ // A saved field for every stroke is what the patch shows, not an extension of fewer.+ if (+ saved?.strokes === strokes.length &&+ entry.strokes.length < strokes.length+ ) {+ return undefined;+ }+ return entry;+ }+ return {+ /** Whether a patch's saved field must load first, since nothing held serves its strokes. */+ needs(+ key: string,+ strokes: readonly BrushStroke[],+ saved?: RemoveField,+ ): saved is RemoveField {+ return saved !== undefined && !usable(key, strokes, saved);+ },+ async load(+ key: string,+ strokes: readonly BrushStroke[],+ saved: RemoveField,+ texels: Blob,+ ) {+ const entry = fields.reserve(key, saved.size);+ entry.strokes = [];+ await writeRaster(gpu, entry.target, texels);+ const { origin, scale, size } = saved;+ entry.lattice = { origin, scale, size };+ entry.strokes = strokes.slice(0, saved.strokes);+ },+ /** The field held for a patch's strokes, or for strokes they extend. */+ get(+ key: string,+ strokes: readonly BrushStroke[],+ saved?: RemoveField,+ ): HeldField | undefined {+ const entry = usable(key, strokes, saved);+ if (!entry) {+ return undefined;+ }+ return {+ texels: input(entry.target),+ lattice: entry.lattice,+ strokes: entry.strokes,+ };+ },+ /** Keeps a patch's field past the next sweep. */+ retain(key: string) {+ const entry = fields.get(key);+ if (entry) {+ fields.reserve(key, entry.target.size);+ }+ },+ /** Holds a field a render synthesized for a patch's strokes, in place of what it held. */+ keep(+ key: string,+ strokes: readonly BrushStroke[],+ lattice: FieldLattice,+ texels: Target,+ ) {+ const entry = fields.reserve(key, lattice.size);+ copyRaster(gpu, texels, entry.target);+ Object.assign(entry, { lattice, strokes });+ },+ /** Reads the field held for exactly a patch's strokes, deflated, to save it. */+ async read(key: string, strokes: readonly BrushStroke[]) {+ const entry = fields.get(key);+ if (+ entry?.strokes.length !== strokes.length ||+ !startsWith(strokes, entry.strokes)+ ) {+ return undefined;+ }+ return {+ texels: await readRaster(gpu, entry.target),+ lattice: entry.lattice,+ };+ },+ sweep: fields.sweep,+ inspect: () => fields.inspect("/field"),+ dispose: fields.dispose,+ };+}diff --git a/src/core/renderer/graph.ts b/src/core/renderer/graph.tsindex 23ca3bd..f4559df 100644--- a/src/core/renderer/graph.ts+++ b/src/core/renderer/graph.ts@@ -9,7 +9,7 @@ import {type Timer,target,} from "vgpu";-import type { CacheKey, RenderImage, RenderNode } from "./node";+import type { RenderImage, RenderNode } from "./node";type Pass = {shader: RenderNode["shader"];@@ -18,21 +18,11 @@ type Pass = {buffers: Map<string, { buffer: Buffer; data: Float32Array }>;};-type Cached = { keys: readonly CacheKey[]; target: Target };--function sameKeys(a: readonly CacheKey[], b: readonly CacheKey[]) {- return a.length === b.length && a.every((key, index) => key === b[index]);-}--function plan(- outputs: readonly RenderImage[],- caches: ReadonlyMap<string, Cached[]>,-) {+function plan(outputs: readonly RenderImage[]) {const uses = new Map<RenderImage, number>();const order: RenderNode[] = [];const live = new Set<Target>();const names = new Set<string>();- const results = new Map<RenderNode, Target>();function visit(image: RenderImage) {const count = uses.get(image) ?? 0;uses.set(image, count + 1);@@ -47,35 +37,18 @@ function plan(throw Error(`Duplicate render node: ${image.name}.`);}names.add(image.name);- const keys = image.cacheKeys;- const cached =- keys &&- caches- .get(image.name)- ?.find(- (cached) =>- sameKeys(cached.keys, keys) &&- cached.target.format === image.format &&- cached.target.size[0] === image.size[0] &&- cached.target.size[1] === image.size[1],- );- if (cached) {- live.add(cached.target);- results.set(image, cached.target);- return;- }Object.values(image.inputs).forEach(visit);order.push(image);}// Requested outputs count as consumers, keeping their targets live.outputs.forEach(visit);- return { order, uses, live, results };+ return { order, uses, live };}const sizeKey = (image: { size: readonly number[]; format: string }) =>`${image.size[0]}x${image.size[1]} ${image.format}`;-/** Owns effects, storage buffers, transient targets, and cached results for one renderer. */+/** Owns effects, storage buffers, and transient targets for one renderer. */export function createRenderGraph(gpu: Gpu, timer?: Timer) {const pool: Target[] = [];/**@@ -85,7 +58,6 @@ export function createRenderGraph(gpu: Gpu, timer?: Timer) {*/let recent: Map<string, number>[] = [];const effects = new Map<string, Pass>();- const caches = new Map<string, Cached[]>();let passes: string[] = [];let disposed = false;function prepare(node: RenderNode) {@@ -145,29 +117,9 @@ export function createRenderGraph(gpu: Gpu, timer?: Timer) {pool.push(image);return image;}- function acquireCached(node: RenderNode, live: ReadonlySet<Target>) {- const entries = caches.get(node.name);- const oldest = entries?.length === 2 ? entries.pop() : undefined;- if (oldest) {- if (- !live.has(oldest.target) &&- sizeKey(oldest.target) === sizeKey(node)- ) {- return oldest.target;- }- // An evicted result can still be an input to this frame; the pool respects its lifetime.- pool.push(oldest.target);- }- return target(gpu, { size: node.size, format: node.format });- }return {/** Retire removed composition instances; bypassed instances remain reusable. */release(prefix: string) {- for (const [name, entries] of caches) {- if (!name.startsWith(prefix)) continue;- for (const { target } of entries) target.color.dispose();- caches.delete(name);- }for (const [name, pass] of effects) {if (name.startsWith(prefix)) {for (const { buffer } of pass.buffers.values()) {@@ -181,7 +133,8 @@ export function createRenderGraph(gpu: Gpu, timer?: Timer) {if (disposed) {throw Error("Render graph is closed.");}- const { order, uses, live, results } = plan(outputs, caches);+ const { order, uses, live } = plan(outputs);+ const results = new Map<RenderNode, Target>();function resolve(image: RenderImage): Target {if (!("inputs" in image)) {return image.target;@@ -205,17 +158,10 @@ export function createRenderGraph(gpu: Gpu, timer?: Timer) {]),),});- const output = node.cacheKeys- ? acquireCached(node, live)- : acquire(node, live);+ const output = acquire(node, live);live.add(output);- if (!node.cacheKeys) written.add(output);+ written.add(output);frame.pass({ target: output, timer: timer?.span(node.name) }, pass);- if (node.cacheKeys) {- const entries = caches.get(node.name) ?? [];- entries.unshift({ keys: node.cacheKeys, target: output });- caches.set(node.name, entries);- }results.set(node, output);for (const input of Object.values(node.inputs)) {const remaining = (uses.get(input) ?? 0) - 1;@@ -275,12 +221,6 @@ export function createRenderGraph(gpu: Gpu, timer?: Timer) {size: [...size],format,})),- cachedTextures: [...caches.values()].flatMap((entries) =>- entries.map(({ target }) => ({- size: [...target.size],- format: target.format,- })),- ),};},dispose() {@@ -291,10 +231,6 @@ export function createRenderGraph(gpu: Gpu, timer?: Timer) {}}effects.clear();- for (const entries of caches.values()) {- for (const { target } of entries) target.color.dispose();- }- caches.clear();for (const image of pool) {image.color.dispose();}diff --git a/src/core/renderer/index.ts b/src/core/renderer/index.tsindex 01537ce..0f9d286 100644--- a/src/core/renderer/index.ts+++ b/src/core/renderer/index.ts@@ -5,20 +5,18 @@ import {type MaskLayer,type PaintLayer,paintingOf,+ type RemovePatch,+ removePatches,type Scene,walkLayers,} from "@/core/document";import type { ImageSource, WhiteBalance } from "@/core/image";import type { Point } from "@/core/image/frame";+import { createFieldStore, type FieldLattice, type HeldField } from "./fields";import { createRenderGraph } from "./graph";import { createMaskCoverage } from "./mask";import { createPatchRaster, type PatchInput } from "./mask/patches";-import {- type CacheKey,- input,- type RenderImage,- type RenderNode,-} from "./node";+import { input, type RenderImage } from "./node";import {type AcceptPainting,createPaintRaster,@@ -37,9 +35,9 @@ export {renderCoverage,type View,} from "./display";+export type { FieldLattice, HeldField } from "./fields";export type { PatchInput } from "./mask/patches";export {- type CacheKey,input,merge,type NodeDefinition,@@ -58,8 +56,6 @@ export { transformImages } from "./transform";export { createRenderGraph };export type Composition = {- /** Cache a derived image by its immutable content, RAW revision, and proxy factor. */- cache: (image: RenderNode, dependencies: readonly CacheKey[]) => RenderNode;inputId?: string;/** The mask whose ranges' image, the one below it, to keep for picking colors from it. */rangeSourceId?: string;@@ -74,6 +70,15 @@ export type Composition = {paint: (layer: PaintLayer) => PaintInput | undefined;/** Rasterized coverage of an effect's own stroke, such as a Healing patch. */patch: (id: string, strokes: readonly BrushStroke[]) => PatchInput;+ /** The Remove field the renderer holds for a patch's strokes, or for strokes they extend. */+ field: (layerId: string, patch: RemovePatch) => HeldField | undefined;+ /** Keeps a Remove field this render synthesizes for a patch's strokes, for the renders after it. */+ keepField: (+ layerId: string,+ patch: RemovePatch,+ lattice: FieldLattice,+ texels: RenderImage,+ ) => void;};/** App composition describes requested outputs; the engine owns their storage. */@@ -101,18 +106,37 @@ function sameBalance(a: WhiteBalance | undefined, b: WhiteBalance | undefined) {return a?.temperature === b?.temperature && a?.tint === b?.tint;}+/** A Remove patch's field, held by Healing layer and patch, since duplicated layers repeat patch IDs. */+function fieldKey(layerId: string, patch: RemovePatch) {+ return `${layerId}/${patch.id}`;+}++/** A renderer's timer, and where it reads what the document stores outside the scene, by the IDs the scene names. */+export type RendererOptions = {+ timer?: Timer;+ /** The settled pixels a paint layer's or brush mask's `raster` names. */+ paintPixels?: (id: string) => Blob;+ /** The texels a Remove patch's field names. */+ fieldTexels?: (id: string) => Blob;+};+/**- * Owns scene passes, mask and paint rasters, the proxy, and intermediate textures for one decoded- * source. `paintPixels` gives the settled pixels a paint layer's or brush mask's `raster` names.+ * Owns scene passes, mask and paint rasters, Remove fields, the proxy, and intermediate textures for+ * one decoded source.*/export function createRenderer(gpu: Gpu,resource: ImageSource,compose: SceneProcessing,- timer?: Timer,- paintPixels: (id: string) => Blob = () => {- throw Error("This renderer has no settled paint.");- },+ {+ timer,+ paintPixels = () => {+ throw Error("This renderer has no settled paint.");+ },+ fieldTexels = () => {+ throw Error("This renderer has no Remove fields.");+ },+ }: RendererOptions = {},) {const source = resource.image;const graph = createRenderGraph(gpu, timer);@@ -121,6 +145,7 @@ export function createRenderer(const masks = createMaskCoverage(brushes);const patches = createPatchRaster(gpu, strokes);const paints = createPaintRaster(gpu, strokes, "rgba8unorm");+ const fields = createFieldStore(gpu);const proxy = createProxy(gpu);const release = resource.retain();const raw = resource.raw?.createPass();@@ -140,7 +165,7 @@ export function createRenderer(let last: RenderRequest | undefined;let next: RenderRequest | undefined;let pending: Promise<void> | undefined;- /** A paint raster being read to settle, which nothing may draw into meanwhile. */+ /** A raster being read back, a painting to settle or a Remove field to save; nothing renders meanwhile. */let settling: Promise<unknown> | undefined;let disposed = false;let instances = new Set<string>();@@ -166,6 +191,10 @@ export function createRenderer(paints.draw(layer.id, layer, developed.size);}}+ // Remove fields stay while their patches do, shown or not.+ for (const { layer, patch } of removePatches(scene.layers)) {+ fields.retain(fieldKey(layer.id, patch));+ }const image =factor > 1 ? proxy.render(developed, factor, version) : input(developed);const covered = new Map<string, RenderImage>();@@ -180,11 +209,13 @@ export function createRenderer(if (coverage) covered.set(layer.id, coverage);}}+ const syntheses: {+ key: string;+ patch: RemovePatch;+ lattice: FieldLattice;+ texels: RenderImage;+ }[] = [];const images = compose(image, scene, {- cache: (image, dependencies) => ({- ...image,- cacheKeys: [version, factor, ...dependencies],- }),inputId,rangeSourceId,retain: (id) => active.add(id),@@ -195,6 +226,15 @@ export function createRenderer(}),paint: (layer) => paints.input(layer),patch: (id, strokes) => patches.patch(id, strokes, developed.size),+ field: (layerId, patch) =>+ fields.get(fieldKey(layerId, patch), patch.strokes, patch.field),+ keepField: (layerId, patch, lattice, texels) =>+ syntheses.push({+ key: fieldKey(layerId, patch),+ patch,+ lattice,+ texels,+ }),});for (const id of instances) {if (!active.has(id)) graph.release(`${id}/`);@@ -215,11 +255,20 @@ export function createRenderer(...inputs,...sources,...covered.values(),+ ...syntheses.map(({ texels }) => texels),]);[original, full, output] = targets;const [inputTarget] = targets.slice(3, 3 + inputs.length);const [sourceTarget] = targets.slice(3 + inputs.length);- const coverages = targets.slice(3 + inputs.length + sources.length);+ const coverages = targets.slice(+ 3 + inputs.length + sources.length,+ 3 + inputs.length + sources.length + covered.size,+ );+ const synthesized = targets.slice(targets.length - syntheses.length);+ for (const [i, { key, patch, lattice }] of syntheses.entries()) {+ fields.keep(key, patch.strokes, lattice, synthesized[i]);+ }+ fields.sweep();inspected =inputId && images.input ? { id: inputId, image: inputTarget } : undefined;kept =@@ -237,6 +286,17 @@ export function createRenderer(function paintRaster(id: string) {return paints.get(id) ? paints : brushes;}+ /** Remove fields to load before rendering: saved ones the renderer holds nothing for. */+ function staleFields(scene: Scene) {+ const stale = [];+ for (const { layer, patch } of removePatches(scene.layers)) {+ const key = fieldKey(layer.id, patch);+ if (fields.needs(key, patch.strokes, patch.field)) {+ stale.push({ key, strokes: patch.strokes, field: patch.field });+ }+ }+ return stale;+ }/** Paintings whose rasters must load settled pixels before they draw. */function stalePaint(scene: Scene) {const stale = [];@@ -251,7 +311,7 @@ export function createRenderer(}/*** Renders the latest request once what it waits for is ready: a settle, a RAW development, whose- * calibration alone crosses the worker, or settled paint to load.+ * calibration alone crosses the worker, settled paint, or Remove fields to load.*/async function develop() {while (next && !disposed) {@@ -278,6 +338,12 @@ export function createRenderer(return;}}+ for (const { key, strokes, field } of staleFields(scene)) {+ await fields.load(key, strokes, field, fieldTexels(field.texels));+ if (disposed) {+ return;+ }+ }if (!next) {render(request);}@@ -298,8 +364,14 @@ export function createRenderer(}const factor = interactive ? Math.max(1, Math.floor(1 / displayScale)) : 1;const request = { scene, inputId, rangeSourceId, factor };- // Renders wait, in order, for a settle, a RAW development, or settled paint to load.- if (!raw && !pending && !settling && !stalePaint(scene).length) {+ // Renders wait, in order, for a settle, a RAW development, or settled paint and fields to load.+ if (+ !raw &&+ !pending &&+ !settling &&+ !stalePaint(scene).length &&+ !staleFields(scene).length+ ) {render(request);return;}@@ -320,11 +392,27 @@ export function createRenderer(return pending;}+ /** Runs a readback once renders in flight finish, and renders nothing until it does. */+ async function hold<T>(read: () => Promise<T>) {+ while (pending || settling) {+ await (pending ?? settling);+ }+ if (disposed) return undefined;+ const reading = read();+ settling = reading;+ try {+ return await reading;+ } finally {+ settling = undefined;+ }+ }+function releaseResources() {graph.dispose();masks.dispose();patches.dispose();paints.dispose();+ fields.dispose();strokes.dispose();proxy.dispose();raw?.dispose();@@ -360,6 +448,7 @@ export function createRenderer(...masks.inspect(),...patches.inspect(),...paints.inspect(),+ ...fields.inspect(),...strokes.inspect(),],}),@@ -377,20 +466,19 @@ export function createRenderer(* Reads a paint layer's raster once renders in flight finish, holding back the next ones, so its* strokes, scene, and cached raster settle together before rendering resumes.*/- async settle(id: string, accept: AcceptPainting) {- while (pending || settling) {- await (pending ?? settling);- }- if (disposed) return false;- const read = paintRaster(id).settle(id, (painting) =>- disposed ? undefined : accept(painting),+ settle(id: string, accept: AcceptPainting) {+ return hold(() =>+ paintRaster(id).settle(id, (painting) =>+ disposed ? undefined : accept(painting),+ ),);- settling = read;- try {- return await read;- } finally {- settling = undefined;- }+ },+ /**+ * Reads the Remove field held for exactly a patch's strokes, deflated, once renders in flight+ * finish, holding back the next ones meanwhile; undefined when none is held.+ */+ readField(layerId: string, patch: RemovePatch) {+ return hold(() => fields.read(fieldKey(layerId, patch), patch.strokes));},dispose() {if (disposed) {diff --git a/src/core/renderer/node.ts b/src/core/renderer/node.tsindex 1e84855..fc25571 100644--- a/src/core/renderer/node.ts+++ b/src/core/renderer/node.ts@@ -1,10 +1,6 @@import type { EffectOptions, ShaderSource, Target } from "vgpu";-export type CacheKey = object | number | string | boolean | undefined;-type NodeOptions = {- /** Immutable content dependencies for an expensive, retained result. */- readonly cacheKeys?: readonly CacheKey[];/** Shares state across passes whose shader and non-input bindings are identical. */readonly instance?: string;readonly set?: EffectOptions["set"];diff --git a/src/core/renderer/paint/index.ts b/src/core/renderer/paint/index.tsindex 9e010c3..e339912 100644--- a/src/core/renderer/paint/index.ts+++ b/src/core/renderer/paint/index.ts@@ -14,7 +14,7 @@ import {type Strokes,} from "@/core/renderer/strokes";import type { DabWalk } from "@/core/renderer/strokes/dabs";-import { readRaster, writeRaster } from "./transfer";+import { readRaster, writeRaster } from "@/core/renderer/transfer";type Size = readonly [number, number];diff --git a/src/core/renderer/paint/transfer.ts b/src/core/renderer/transfer.tssimilarity index 78%rename from src/core/renderer/paint/transfer.tsrename to src/core/renderer/transfer.tsindex d8b97ed..eb32722 100644--- a/src/core/renderer/paint/transfer.ts+++ b/src/core/renderer/transfer.ts@@ -10,15 +10,32 @@ const copy = weakMemo((gpu: Gpu) => effect(gpu, copyShader));/** `GPUMapMode.READ`, which the page's WebGPU types leave out. */const mapRead = 1;-/** Bytes a row of an rgba8 or r8 raster takes. */+/** Bytes a texel takes in the formats rasters use. */+const texelBytes: Partial<Record<GPUTextureFormat, number>> = {+ r8unorm: 1,+ rgba8unorm: 4,+ rg16float: 4,+};++/** Bytes a row of a raster takes. */function rowBytes({ size, format }: Target) {- return size[0] * (format === "r8unorm" ? 1 : 4);+ const bytes = texelBytes[format];+ if (!bytes) {+ throw Error(`Rasters can't transfer ${format}.`);+ }+ return size[0] * bytes;+}++/** Copies a raster into another of the same size and format, which render targets take by drawing. */+export function copyRaster(gpu: Gpu, source: Target, raster: Target) {+ const pass = copy(gpu).set({ source, params: { offset: [0, 0] } });+ frame(gpu, (frame) => frame.pass({ target: raster, clear: false }, pass));}/**- * Reads an rgba8 or r8 raster band by band and deflates its bytes, rows tightly packed. One mapped- * buffer takes every band in turn. Nothing may draw into the raster until it resolves, since each band- * is read after the last.+ * Reads a raster band by band and deflates its bytes, rows tightly packed. One mapped buffer takes+ * every band in turn. Nothing may draw into the raster until it resolves, since each band is read+ * after the last.*/export async function readRaster(gpu: Gpu, raster: Target) {const device = gpu.gpu;@@ -80,7 +97,7 @@ export async function writeRaster(gpu: Gpu, raster: Target, pixels: Blob) {let y = 0;function draw(rows: number) {if (y + rows > height) {- throw Error("The paint doesn't match the photo's size.");+ throw Error("The stored pixels don't match their raster's size.");}// The queue copies the bytes at once, so the band is free to fill again, and draws in order.gpu.gpu.queue.writeTexture(@@ -123,13 +140,13 @@ export async function writeRaster(gpu: Gpu, raster: Target, pixels: Blob) {}}if (filled % packed !== 0) {- throw Error("The paint doesn't match the photo's size.");+ throw Error("The stored pixels don't match their raster's size.");}if (filled) {draw(filled / packed);}if (y !== height) {- throw Error("The paint doesn't match the photo's size.");+ throw Error("The stored pixels don't match their raster's size.");}} finally {staging.dispose();diff --git a/src/features/crop/overlay.tsx b/src/features/crop/overlay.tsxindex 9fb68ef..b53063d 100644--- a/src/features/crop/overlay.tsx+++ b/src/features/crop/overlay.tsx@@ -153,7 +153,7 @@ export function CropOverlay({className="group absolute flex size-11 min-h-11 min-w-11 -translate-1/2 items-center justify-center outline-none data-[handle=n]:w-full data-[handle=s]:w-full data-[handle=e]:h-full data-[handle=w]:h-full"style={{ left: `${left}%`, top: `${top}%`, cursor }}>- <span className="pointer-events-none size-2.5 surface-accent group-focus-visible:ring-2 group-focus-visible:ring-focus" />+ <span className="pointer-events-none size-2.5 bg-accent shadow-[0_0_2px_1px_oklch(10%_0_0/0.3),inset_0_1px_0_oklch(95%_0_0/0.2),0_1px_2px_oklch(10%_0_0/0.5)] group-focus-visible:ring-2 group-focus-visible:ring-focus" /></button>))}</div>diff --git a/src/features/heal/edits.ts b/src/features/heal/edits.tsindex 842df37..efd453e 100644--- a/src/features/heal/edits.ts+++ b/src/features/heal/edits.ts@@ -75,7 +75,10 @@ function editPatches(});}-/** Adds or subtracts a separate stroke without changing the patch's donor or blend. */+/**+ * Adds or subtracts a separate stroke without changing the patch's donor or blend. A Remove patch keeps+ * its field, which the next synthesis extends.+ */export function addHealStroke(document: EditorDocument,id: string,@@ -143,23 +146,21 @@ export function extendHealPatch(if (layer.kind !== "heal" || !layer.patches.length) {throw Error("Start a heal patch before extending it.");}- return {- ...layer,- patches: layer.patches.map((patch, index) =>- index === layer.patches.length - 1- ? {- ...patch,- strokes: patch.strokes.with(-1, {- ...patch.strokes[patch.strokes.length - 1],- points: [- ...patch.strokes[patch.strokes.length - 1].points,- ...added,- ],- }),- }- : patch,- ),- };+ const patch = layer.patches[layer.patches.length - 1];+ const last = patch.strokes[patch.strokes.length - 1];+ const strokes = patch.strokes.with(-1, {+ ...last,+ points: [...last.points, ...added],+ });+ // A Remove field counts whole strokes, so a stroke that grows takes a new one.+ if (patch.mode === "remove") {+ const { field, ...unfilled } = patch;+ return {+ ...layer,+ patches: layer.patches.with(-1, { ...unfilled, strokes }),+ };+ }+ return { ...layer, patches: layer.patches.with(-1, { ...patch, strokes }) };});}@@ -177,7 +178,7 @@ export function setHealSource(});}-/** Moves a patch; a Heal/Clone donor stays fixed. */+/** Moves a patch; a Heal/Clone donor stays fixed, and a Remove patch synthesizes its new place. */export function setHealDestination(document: EditorDocument,id: string,@@ -189,19 +190,19 @@ export function setHealDestination(const patch = patches[index];const [x, y] = patch.strokes[0].points[0];const delta: Point = [targetX - x, targetY - y];- const moved = {- ...patch,- strokes: patch.strokes.map((stroke) => ({- ...stroke,- points: stroke.points.map(- ([x, y, pressure]) => [x + delta[0], y + delta[1], pressure] as const,- ),- })),- };- if (patch.mode === "remove") return patches.with(index, moved);+ const strokes = patch.strokes.map((stroke) => ({+ ...stroke,+ points: stroke.points.map(+ ([x, y, pressure]) => [x + delta[0], y + delta[1], pressure] as const,+ ),+ }));+ if (patch.mode === "remove") {+ const { field, ...unfilled } = patch;+ return patches.with(index, { ...unfilled, strokes });+ }return patches.with(index, {- ...moved,- mode: patch.mode,+ ...patch,+ strokes,offset: [patch.offset[0] - delta[0], patch.offset[1] - delta[1]],});});diff --git a/src/features/heal/inpaint/algorithm.md b/src/features/heal/inpaint/algorithm.mdindex ae41d69..e8c0008 100644--- a/src/features/heal/inpaint/algorithm.md+++ b/src/features/heal/inpaint/algorithm.md@@ -1,16 +1,18 @@# GPU object removal-`inpaint(source, coverage, region, name)` declares a pure render graph; it requires no React, document store, model weights, workers, or pixel readback. `pass.ts` caches only `inpaintField`'s bounded donor correspondence field (at most 4 MiB per revision), then calls `sampleInpaint` and blends the result into the scene. All image processing runs in WGSL; TypeScript only describes regions, levels, and passes. The working image remains linear Rec.2020, with HDR values and the destination alpha preserved by the final blend. The field stores `[dx, dy, cost, valid]` in `rgba32float`; offsets are in matching-grid texels. `sampleInpaint` returns donor RGB with validity in alpha for the compositor.+`inpaint(source, coverage, region, name)` declares a pure render graph; it requires no React, document store, model weights, workers, or pixel readback. `inpaintField` returns each texel's offset to its donor, in texels, as `rg16float`, zero where nothing replaces the pixel: at most 1 MiB. The editor keeps that field, so `pass.ts` synthesizes once per set of strokes and otherwise only calls `sampleInpaint` and blends the result into the scene. All image processing runs in WGSL; TypeScript only describes lattices, levels, and passes. The working image remains linear Rec.2020, with HDR values and the destination alpha preserved by the final blend. The solver works on `[dx, dy, cost, valid]` in `rgba32float`. `sampleInpaint` returns donor RGB with validity in alpha for the compositor.The algorithm follows non-local, multiscale texture synthesis with PatchMatch, adapted for parallel GPU execution. It is not a reproduction of every step of the IPOL reference and does not infer hidden semantic content.1. Crop the accumulated patch with surrounding context: at least 32 source pixels, otherwise the largest painted brush diameter on every side, clipped to the image. Combine ordered paint/erase strokes in the shared GPU coverage raster; subtraction does not enlarge the region. Exclude the remaining painted object conservatively before creating a color pyramid.-2. Work on at most a 512 px long edge. Precompute logarithmic color once. Keep horizontal and vertical texture energy through the pyramid, so coarse colors do not erase fine texture differences.+2. Work on at most 512 texels along the long side. Precompute logarithmic color once. Keep horizontal and vertical texture energy through the pyramid, so coarse colors do not erase fine texture differences.3. Initialize from nearby wholly unmasked donors using jump flooding and grow the fill inward at the coarsest level; upsample and preserve the previous level's offsets. A donor's entire 7×7 footprint must be clean.4. Alternate parallel PatchMatch propagation at jumps 8, 4, 2, 1 and eight shrinking-radius random proposals with weighted overlapping reconstruction. Use six iterations at the coarsest level, four at finer levels. Compare nine samples across each 7×7 footprint plus texture descriptors; increase reconstructed-region confidence as refinement proceeds.-5. Sample donors at whole source texels from the original source for the final image rather than exporting the averaged matching pyramid. Different pixels can use different offsets. Apply the existing inward feather, patch opacity, and original alpha in a separate blend.+5. Keep each hole texel's offset. Sample donors at whole source texels from the image below for the final image rather than exporting the averaged matching pyramid, so the patch follows later changes to that image without solving again. Different pixels can use different offsets. Apply the existing inward feather, patch opacity, and original alpha in a separate blend.-Known, wholly clean patches skip search. Candidate comparison stops once its nonnegative partial error cannot beat the current score. Each search pass owns its uniforms; vgpu shares shader modules and pipelines, and the graph reuses temporary textures across iterations. Reconstruction passes share an effect because they have no uniforms. The graph retains only a bounded correspondence field for opacity/feather edits and separates RAW/proxy revisions. Random proposals are deterministic for the same inputs.+A stroke added to or erased from a patch extends its field: the new field keeps the earlier one's texels while they still fit 512 along the long side, and at every level pins each texel the earlier field filled to its donor, as long as the texel stays in the hole and the donor stays out of it. Pinned texels count as known context, so the new texels match them, and only the rest are searched. Past that size the lattice coarsens, and the earlier offsets land on the nearest texels.++Known, wholly clean patches skip search. Candidate comparison stops once its nonnegative partial error cannot beat the current score. Each search pass owns its uniforms; vgpu shares shader modules and pipelines, and the graph reuses temporary textures across iterations. Reconstruction passes share an effect because they have no uniforms. Random proposals are deterministic for the same inputs; since the inputs include the image below, a change there would find other donors, which is why the editor keeps the field instead of solving again.## Limits@@ -20,7 +22,7 @@ The user should paint over the entire object, including its halo or shadow. Pixe## Verification and measurement-`tests/inpaint-gpu.ts` creates known flat, gradient, periodic, edge, and brick backgrounds, allowing error measurement against a clean reference. Browser tests check actual removal, HDR/alpha, untouched pixels, proxy rendering, cache invalidation/release, unavailable donors, and the H/paint/export/undo workflow. Scene files round-trip Remove patches without offsets. `tests/inpaint-photo.ts` runs decoding, removal, and export on the demo photograph without mounting React.+`tests/inpaint-gpu.ts` creates known flat, gradient, periodic, edge, and brick backgrounds, allowing error measurement against a clean reference. Browser tests check actual removal, HDR/alpha, untouched pixels, proxy rendering, a kept field that follows the exposure below without solving again, extensions that keep what was filled, release, unavailable donors, and the H/paint/export/undo workflow. Scene files round-trip Remove patches with their fields and without offsets. `tests/inpaint-photo.ts` runs decoding, removal, and export on the demo photograph without mounting React.See [PERFORMANCE.md](../../../../PERFORMANCE.md#object-removal) for standalone and integrated benchmarks, including an unchanged-patch cache workload. Report the actual adapter; software-backend timings do not predict physical-GPU latency.diff --git a/src/features/heal/inpaint/finish.wgsl b/src/features/heal/inpaint/finish.wgslindex 8021228..c5efdd7 100644--- a/src/features/heal/inpaint/finish.wgsl+++ b/src/features/heal/inpaint/finish.wgsl@@ -1,15 +1,15 @@-struct Params { origin: vec2f, extent: vec2f, dimensions: vec2f, grid: vec2f, texel: vec2f }+struct Params { origin: vec2f, extent: vec2f, dimensions: vec2f, grid: vec2f, texel: vec2f, fieldOrigin: vec2f, fieldScale: f32 }@group(0) @binding(0) var source: texture_2d<f32>;@group(0) @binding(1) var field: texture_2d<f32>;@group(0) @binding(2) var linearSampler: sampler;@group(0) @binding(3) var<uniform> params: Params;@fragment fn fs_main(@builtin(position) position: vec4f) -> @location(0) vec4f {- let uv = position.xy / params.grid;- let p = params.origin + uv * params.extent;- let cell = clamp(vec2i(uv * vec2f(textureDimensions(field))), vec2i(0), vec2i(textureDimensions(field)) - 1);- let correspondence = textureLoad(field, cell, 0);- let offset = round(correspondence.xy / vec2f(textureDimensions(field)) * params.extent / params.texel) * params.texel;- let donor = p + offset;+ let p = params.origin + position.xy / params.grid * params.extent;+ let cell = vec2i(floor((p - params.fieldOrigin) / params.fieldScale));+ if (any(cell < vec2i(0)) || any(cell >= vec2i(textureDimensions(field)))) { return vec4f(0.0); }+ let offset = textureLoad(field, cell, 0).xy;+ if (all(offset == vec2f(0.0))) { return vec4f(0.0); }+ let donor = p + round(offset * params.fieldScale / params.texel) * params.texel;let color = textureSampleLevel(source, linearSampler, donor / params.dimensions, 0.0);- return vec4f(color.rgb, correspondence.w);+ return vec4f(color.rgb, 1.0);}diff --git a/src/features/heal/inpaint/index.ts b/src/features/heal/inpaint/index.tsindex 015f94a..12c9a87 100644--- a/src/features/heal/inpaint/index.ts+++ b/src/features/heal/inpaint/index.ts@@ -1,5 +1,6 @@import type { Point } from "@/core/image/frame";import {+ type FieldLattice,merge,node,type PatchInput,@@ -11,11 +12,17 @@ import featuresShader from "./features.wgsl";import finishShader from "./finish.wgsl";import matchShader from "./match.wgsl";import nearestShader from "./nearest.wgsl";+import offsetsShader from "./offsets.wgsl";+import pinShader from "./pin.wgsl";import prepareShader from "./prepare.wgsl";import pyramidShader from "./pyramid.wgsl";import reconstructShader from "./reconstruct.wgsl";export type InpaintRegion = { origin: Point; extent: Point };+/** An earlier field whose filled texels a new synthesis keeps. */+export type InpaintBase = { texels: RenderImage; lattice: FieldLattice };+/** Texels along a field's longer side at most; a larger region takes larger texels. */+export const fieldLongSide = 512;const samplers = {linearSampler: { minFilter: "linear", magFilter: "linear" },} as const;@@ -58,11 +65,38 @@ function reconstruct();}+/** A base field to pin in one level, whose texels span `scale` source pixels from `origin`. */+type Pinning = { base: InpaintBase; origin: Point; scale: number };++/** Pins the texels the base filled to their donors. */+function pin(+ source: RenderImage,+ field: RenderImage,+ { base, origin, scale }: Pinning,+ name: string,+) {+ return merge(+ { source, field, base: base.texels },+ node(name, pinShader, {+ format: "rgba32float",+ set: {+ params: {+ origin,+ scale,+ baseOrigin: base.lattice.origin,+ baseScale: base.lattice.scale,+ },+ },+ }),+ );+}+function solve(source: RenderImage,features: RenderImage,coarse: RenderImage | undefined,name: string,+ pinning?: Pinning,) {const closest = nearest(features, name);const params = {@@ -74,7 +108,7 @@ function solve(radius: coarse ? 1e10 : 4,};let iteration = 0;- let field = merge(+ let field: RenderImage = merge({source,features,@@ -87,6 +121,7 @@ function solve(set: { params },}),);+ if (pinning) field = pin(source, field, pinning, `${name}/pin`);let current = reconstruct(source,field,@@ -149,29 +184,69 @@ function solve(return field;}-/** Multiscale, non-local synthesis. Every pixel operation stays on the GPU; no React or readback. */+/**+ * The texels a field over `bounds` takes. An extension keeps its base's texels while they fit, so the+ * texels the base filled keep their donors exactly.+ */+export function fieldLattice(+ bounds: InpaintRegion,+ base?: FieldLattice,+): FieldLattice {+ const end = [+ bounds.origin[0] + bounds.extent[0],+ bounds.origin[1] + bounds.extent[1],+ ] as const;+ if (base) {+ const { scale } = base;+ const origin: Point = [+ base.origin[0] ++ Math.floor((bounds.origin[0] - base.origin[0]) / scale) * scale,+ base.origin[1] ++ Math.floor((bounds.origin[1] - base.origin[1]) / scale) * scale,+ ];+ const size: Point = [+ Math.ceil((end[0] - origin[0]) / scale),+ Math.ceil((end[1] - origin[1]) / scale),+ ];+ if (Math.max(...size) <= fieldLongSide) return { origin, scale, size };+ }+ const scale = Math.max(1, Math.max(...bounds.extent) / fieldLongSide);+ return {+ origin: bounds.origin,+ scale,+ size: [+ Math.max(1, Math.ceil(bounds.extent[0] / scale)),+ Math.max(1, Math.ceil(bounds.extent[1] / scale)),+ ],+ };+}++/**+ * Multiscale, non-local synthesis of each hole texel's offset to its donor, in texels, at full+ * resolution; texels a `base` filled keep their donors. Every pixel operation stays on the GPU; no+ * React or readback.+ */export function inpaintField(source: RenderImage,patch: PatchInput,- region: InpaintRegion,+ lattice: FieldLattice,name: string,+ base?: InpaintBase,): RenderNode {- const dimensions = sourceSize(source);- const factor = Math.max(...source.scale, Math.max(...region.extent) / 512);- const size: Point = [- Math.max(1, Math.ceil(region.extent[0] / factor)),- Math.max(1, Math.ceil(region.extent[1] / factor)),- ];const params = {- ...region,- dimensions,+ origin: lattice.origin,+ scale: lattice.scale,+ dimensions: sourceSize(source),coverageOrigin: patch.origin,coverageSize: patch.coverage.size,- grid: size,};const prepared = merge({ source, coverage: patch.coverage },- node(`${name}/prepare`, prepareShader, { size, samplers, set: { params } }),+ node(`${name}/prepare`, prepareShader, {+ size: lattice.size,+ samplers,+ set: { params },+ }),);const pyramid: RenderImage[] = [prepared];while (Math.max(...pyramid[pyramid.length - 1].size) > 32) {@@ -198,23 +273,30 @@ export function inpaintField(),);}- let field: RenderNode | undefined;+ let field: RenderImage | undefined;for (let level = pyramid.length - 1; level >= 0; level--) {+ // Each level halves the one below, so its texels span twice the source pixels.+ const scale = lattice.scale * 2 ** level;field = solve(pyramid[level],descriptors[level],field,`${name}/level/${level}`,+ base && { base, origin: lattice.origin, scale },);}if (!field) throw Error("An inpainting pyramid is required.");- return { ...field, name: `${name}/field` };+ return merge(+ { source: prepared, field },+ node(`${name}/offsets`, offsetsShader, { format: "rg16float" }),+ );}-/** Sample whole source texels from a correspondence field, preserving original donor detail. */+/** Sample whole source texels at a field's offsets, preserving original donor detail. */export function sampleInpaint(source: RenderImage,field: RenderImage,+ lattice: FieldLattice,region: InpaintRegion,name: string,): RenderNode {@@ -234,22 +316,26 @@ export function sampleInpaint(dimensions,grid: outputSize,texel: source.scale,+ fieldOrigin: lattice.origin,+ fieldScale: lattice.scale,},},}),);}-/** Standalone synthesis and reconstruction; app composition can cache just the bounded field. */+/** Standalone synthesis and sampling; app composition keeps the field to sample it again. */export function inpaint(source: RenderImage,patch: PatchInput,region: InpaintRegion,name: string,): RenderNode {+ const lattice = fieldLattice(region);return sampleInpaint(source,- inpaintField(source, patch, region, name),+ inpaintField(source, patch, lattice, name),+ lattice,region,`${name}/result`,);diff --git a/src/features/heal/inpaint/match.wgsl b/src/features/heal/inpaint/match.wgslindex ee8ddc3..230441f 100644--- a/src/features/heal/inpaint/match.wgsl+++ b/src/features/heal/inpaint/match.wgsl@@ -36,7 +36,9 @@ fn distance(p: vec2i, q: vec2i, limit: f32) -> f32 {let a = clamp(p + vec2i(x, y), vec2i(0), end);let b = q + vec2i(x, y);let masked = textureLoad(source, a, 0).a;- let reconstructed = select(0.0, params.confidence, textureLoad(field, a, 0).w > 0.5);+ let correspondence = textureLoad(field, a, 0);+ let pinned = params.initialize == 0u && correspondence.z < 0.0;+ let reconstructed = select(0.0, select(params.confidence, 1.0, pinned), correspondence.w > 0.5);let confidence = select(1.0, reconstructed, masked > 0.5);let destination = textureLoad(current, a, 0).rgb;let donor = textureLoad(source, b, 0).rgb;@@ -60,6 +62,10 @@ fn consider(p: vec2i, offset: vec2i, best: vec4f) -> vec4f {let p = vec2i(position.xy);let size = vec2i(textureDimensions(source));if (textureLoad(features, p, 0).a > 0.5) { return vec4f(0.0, 0.0, 0.0, 1.0); }+ if (params.initialize == 0u) {+ let pinned = textureLoad(field, p, 0);+ if (pinned.z < 0.0) { return pinned; }+ }if (textureLoad(source, p, 0).a > 0.5 && sqrt(textureLoad(nearest, p, 0).z) > params.radius) { return vec4f(0.0, 0.0, 1e10, 0.0); }var best = vec4f(0.0, 0.0, 1e10, 0.0);if (params.initialize != 0u) {diff --git a/src/features/heal/inpaint/offsets.wgsl b/src/features/heal/inpaint/offsets.wgslnew file mode 100644index 0000000..c8fd64b--- /dev/null+++ b/src/features/heal/inpaint/offsets.wgsl@@ -0,0 +1,9 @@+// What a field keeps: each hole texel's offset to its donor, zero where nothing replaces the pixel.+@group(0) @binding(0) var source: texture_2d<f32>;+@group(0) @binding(1) var field: texture_2d<f32>;+@fragment fn fs_main(@builtin(position) position: vec4f) -> @location(0) vec4f {+ let p = vec2i(position.xy);+ let correspondence = textureLoad(field, p, 0);+ let filled = textureLoad(source, p, 0).a > 0.5 && correspondence.w > 0.5;+ return vec4f(select(vec2f(0.0), correspondence.xy, filled), 0.0, 1.0);+}diff --git a/src/features/heal/inpaint/pass.ts b/src/features/heal/inpaint/pass.tsindex 4fb9a2d..cfd6bba 100644--- a/src/features/heal/inpaint/pass.ts+++ b/src/features/heal/inpaint/pass.ts@@ -1,7 +1,7 @@-import type { HealPatch } from "@/core/document";+import type { RemovePatch } from "@/core/document";import {- type CacheKey,type Composition,+ type FieldLattice,merge,node,type PatchInput,@@ -9,16 +9,52 @@ import {sourceSize,} from "@/core/renderer";import { patchBounds } from "@/features/heal/model";-import { inpaintField, sampleInpaint } from ".";+import {+ fieldLattice,+ type InpaintRegion,+ inpaintField,+ sampleInpaint,+} from ".";import shader from "./blend.wgsl";+type FieldComposition = Pick<Composition, "field" | "keepField">;++/**+ * The field a patch shows: the one held for its strokes, or else one synthesized from the image below+ * at full resolution, which keeps what the field held for its first strokes filled. A proxy shows the+ * held field meanwhile, since its reduced image would synthesize another.+ */+function patchField(+ source: RenderImage,+ coverage: PatchInput,+ region: InpaintRegion,+ layerId: string,+ patch: RemovePatch,+ name: string,+ composition: FieldComposition,+): { texels: RenderImage; lattice: FieldLattice } | undefined {+ const held = composition.field(layerId, patch);+ if (held?.strokes.length === patch.strokes.length) return held;+ if (source.scale.some((scale) => scale > 1)) return held;+ const lattice = fieldLattice(region, held?.lattice);+ const texels = inpaintField(+ source,+ coverage,+ lattice,+ `${name}/inpaint`,+ held,+ );+ composition.keepField(layerId, patch, lattice, texels);+ return { texels, lattice };+}+export function removePatch(source: RenderImage,coverage: PatchInput,- patch: Extract<HealPatch, { mode: "remove" }>,+ layerId: string,+ patch: RemovePatch,name: string,- cache: Composition["cache"],- dependencies: readonly CacheKey[],+ composition: FieldComposition,) {const dimensions = sourceSize(source);const margin = patch.strokes.reduce((margin, stroke) => {@@ -26,11 +62,23 @@ export function removePatch(return Math.max(margin, stroke.size);}, 32);const region = patchBounds(patch.strokes, dimensions, margin);- const field = cache(- inpaintField(source, coverage, region, `${name}/inpaint`),- [...dependencies, patch.strokes],+ const field = patchField(+ source,+ coverage,+ region,+ layerId,+ patch,+ name,+ composition,+ );+ if (!field) return source;+ const filled = sampleInpaint(+ source,+ field.texels,+ field.lattice,+ region,+ `${name}/sample`,);- const filled = sampleInpaint(source, field, region, `${name}/sample`);return merge({ source, coverage: coverage.coverage, filled },node(`${name}/blend`, shader, {diff --git a/src/features/heal/inpaint/pin.wgsl b/src/features/heal/inpaint/pin.wgslnew file mode 100644index 0000000..9a11f05--- /dev/null+++ b/src/features/heal/inpaint/pin.wgsl@@ -0,0 +1,20 @@+// A texel an earlier field filled keeps its donor while it stays in the hole and its donor stays out of it.+struct Params { origin: vec2f, baseOrigin: vec2f, scale: f32, baseScale: f32 }+@group(0) @binding(0) var source: texture_2d<f32>;+@group(0) @binding(1) var field: texture_2d<f32>;+@group(0) @binding(2) var base: texture_2d<f32>;+@group(0) @binding(3) var<uniform> params: Params;+@fragment fn fs_main(@builtin(position) position: vec4f) -> @location(0) vec4f {+ let p = vec2i(position.xy);+ let current = textureLoad(field, p, 0);+ if (textureLoad(source, p, 0).a < 0.5) { return current; }+ let cell = vec2i(floor((params.origin + position.xy * params.scale - params.baseOrigin) / params.baseScale));+ if (any(cell < vec2i(0)) || any(cell >= vec2i(textureDimensions(base)))) { return current; }+ let offset = textureLoad(base, cell, 0).xy;+ if (all(offset == vec2f(0.0))) { return current; }+ let donor = p + vec2i(round(offset * params.baseScale / params.scale));+ let end = vec2i(textureDimensions(source)) - 1;+ if (any(donor < vec2i(0)) || any(donor > end) || textureLoad(source, donor, 0).a > 0.5) { return current; }+ // A negative cost pins the texel for the passes after this one.+ return vec4f(vec2f(donor - p), -1.0, 1.0);+}diff --git a/src/features/heal/inpaint/prepare.wgsl b/src/features/heal/inpaint/prepare.wgslindex ae77543..bcea662 100644--- a/src/features/heal/inpaint/prepare.wgsl+++ b/src/features/heal/inpaint/prepare.wgsl@@ -1,10 +1,9 @@struct Params {origin: vec2f,- extent: vec2f,dimensions: vec2f,coverageOrigin: vec2f,coverageSize: vec2f,- grid: vec2f,+ scale: f32,}@group(0) @binding(0) var source: texture_2d<f32>;@group(0) @binding(1) var coverage: texture_2d<f32>;@@ -12,8 +11,8 @@ struct Params {@group(0) @binding(3) var<uniform> params: Params;@fragment fn fs_main(@builtin(position) position: vec4f) -> @location(0) vec4f {- let p = params.origin + position.xy / params.grid * params.extent;- let footprint = max(vec2f(1.0), params.extent / params.grid * 0.5);+ let p = params.origin + position.xy * params.scale;+ let footprint = max(1.0, params.scale * 0.5);var masked = 0.0;for (var y = -1; y <= 1; y++) {for (var x = -1; x <= 1; x++) {diff --git a/src/features/heal/inpaint/reconstruct.wgsl b/src/features/heal/inpaint/reconstruct.wgslindex 7486edf..68b19fb 100644--- a/src/features/heal/inpaint/reconstruct.wgsl+++ b/src/features/heal/inpaint/reconstruct.wgsl@@ -15,7 +15,8 @@let donor = clamp(p + vec2i(correspondence.xy), vec2i(0), end);let sample = textureLoad(source, donor, 0);if (sample.a > 0.5) { continue; }- let confidence = exp(-min(correspondence.z * 80.0, 20.0));+ // A pinned texel's negative cost counts as a perfect match.+ let confidence = exp(-clamp(correspondence.z * 80.0, 0.0, 20.0));color += sample.rgb * confidence;weight += confidence;}diff --git a/src/features/heal/model.ts b/src/features/heal/model.tsindex 57bbaa9..74d3757 100644--- a/src/features/heal/model.ts+++ b/src/features/heal/model.ts@@ -8,6 +8,7 @@ import {import { strokeSchema } from "@/core/document/brush";import type { Point } from "@/core/image/frame";import { change, point, unit } from "@/lib/parse";+import { fieldLongSide } from "./inpaint";export function findHealPatch(scene: Scene, layerId: string, patchId: string) {const layer = findLayer(scene.layers, layerId);@@ -130,11 +131,32 @@ const patchShape = {opacity: unit,};+const fieldSide = z.int().check(z.minimum(1), z.maximum(fieldLongSide));++const removeFieldSchema = z.object({+ texels: z.string().check(z.minLength(1)),+ strokes: z.int().check(z.minimum(1)),+ origin: point,+ scale: z.number().check(z.minimum(1)),+ size: z.tuple([fieldSide, fieldSide]),+});+export const healPatchSchema = z.union([z.object({...patchShape,mode: z._default(donorModeSchema, "heal"),offset: point,}),- z.object({ ...patchShape, mode: z.literal("remove") }),+ z+ .object({+ ...patchShape,+ mode: z.literal("remove"),+ field: z.optional(removeFieldSchema),+ })+ .check(+ z.refine(+ (patch) => (patch.field?.strokes ?? 0) <= patch.strokes.length,+ "A Remove field covers at most its patch's strokes",+ ),+ ),]) satisfies z.ZodMiniType<HealPatch>;diff --git a/src/features/heal/pass.ts b/src/features/heal/pass.tsindex 482054b..3f97f23 100644--- a/src/features/heal/pass.ts+++ b/src/features/heal/pass.ts@@ -1,6 +1,5 @@import type { HealPatch } from "@/core/document";import {- type CacheKey,type Composition,merge,node,@@ -14,7 +13,7 @@ import { patchBounds } from "./model";type HealComposition = Pick<Composition,- "patch" | "inputId" | "retain" | "cache"+ "patch" | "inputId" | "retain" | "field" | "keepField">;function healPatch(@@ -107,33 +106,24 @@ export function retainHealPatches(export function heal(source: RenderImage,+ layerId: string,patches: readonly HealPatch[],name: string,composition: HealComposition,- dependencies: readonly CacheKey[],) {retainHealPatches(patches, name, composition);let image = source;let inspected: RenderImage | undefined;- let content = dependencies;for (const patch of patches) {if (patch.id === composition.inputId) inspected = image;const id = `${name}/${patch.id}`;if (!repairsPixels(patch)) continue;const coverage = composition.patch(id, patch.strokes);if (patch.mode === "remove") {- image = removePatch(- image,- coverage,- patch,- id,- composition.cache,- content,- );+ image = removePatch(image, coverage, layerId, patch, id, composition);} else {image = healPatch(image, coverage, patch, id);}- content = [...content, patch];}return { image, input: inspected };}diff --git a/src/features/heal/save-fields.tsx b/src/features/heal/save-fields.tsxnew file mode 100644index 0000000..012ef8d--- /dev/null+++ b/src/features/heal/save-fields.tsx@@ -0,0 +1,144 @@+import { useEffect } from "react";+import { useRenderer } from "@/components/editor/pipeline";+import { useDocument } from "@/components/editor/session";+import {+ type BrushStroke,+ type EditorDocument,+ findLayer,+ type RemovePatch,+ removePatches,+ updateLayer,+} from "@/core/document";++type Renderer = ReturnType<typeof useRenderer>;++/** Whether a patch's saved field covers every stroke it has. */+function saved(patch: RemovePatch) {+ return patch.field?.strokes === patch.strokes.length;+}++/** Whether `strokes` start with every stroke of `first`, the same objects. */+function startsWith(+ strokes: readonly BrushStroke[],+ first: readonly BrushStroke[],+) {+ return (+ first.length <= strokes.length &&+ first.every((stroke, i) => stroke === strokes[i])+ );+}++/**+ * Saves the field the renderer synthesized for a Remove patch's strokes into the document. The scene+ * changes in place: the patch shows the same, and from then on undo, scene files, and other renderers+ * show it so too. A stroke added meanwhile keeps the field as the base its synthesis extends; any+ * other change to the patch, or a gesture opened meanwhile, drops it.+ */+async function saveRemoveField(+ document: EditorDocument,+ renderer: Pick<Renderer, "readField">,+ layerId: string,+ patch: RemovePatch,+) {+ const read = await renderer.readField(layerId, patch);+ if (!read || document.closed || document.history.status.getState().editing) {+ return;+ }+ const scene = document.scene.getState();+ const healing = findLayer(scene.layers, layerId);+ const current =+ healing?.kind === "heal"+ ? healing.patches.find(({ id }) => id === patch.id)+ : undefined;+ if (+ current?.mode !== "remove" ||+ !startsWith(current.strokes, patch.strokes) ||+ (current.field?.strokes ?? 0) >= patch.strokes.length+ ) {+ return;+ }+ const field = {+ texels: document.resources.addField(read.texels),+ strokes: patch.strokes.length,+ ...read.lattice,+ };+ document.replace(+ updateLayer(scene, layerId, (layer) =>+ layer.kind === "heal"+ ? {+ ...layer,+ patches: layer.patches.map((item) =>+ item === current ? { ...current, field } : item,+ ),+ }+ : layer,+ ),+ );+}++/**+ * Saves each Remove field once the renderer synthesizes it and no gesture is open, checking after+ * every render and when a gesture ends. A render during a save checks again after it; a patch the+ * renderer holds no field for, such as one on a hidden layer, waits for the next render. The document+ * keeps each save, so a snapshot waits for it. Returns the stop.+ */+export function watchRemoveFields(+ document: EditorDocument,+ renderer: Pick<Renderer, "readField" | "subscribe">,+) {+ let active = true;+ let running = false;+ let rendered = false;+ async function save() {+ running = true;+ rendered = false;+ try {+ const patches = [...removePatches(document.scene.getState().layers)];+ for (const { layer, patch } of patches) {+ // A gesture stops the pass, since a readback holds renders; its end checks again.+ if (!active || document.history.status.getState().editing) {+ return;+ }+ if (!saved(patch)) {+ await saveRemoveField(document, renderer, layer.id, patch);+ }+ }+ } finally {+ running = false;+ }+ }+ function check() {+ if (!active || document.history.status.getState().editing) {+ return;+ }+ if (running) {+ rendered = true;+ return;+ }+ const scene = document.scene.getState();+ if ([...removePatches(scene.layers)].every(({ patch }) => saved(patch))) {+ return;+ }+ document.replacing(+ save()+ .catch((error) =>+ console.error("A Remove field couldn't be saved.", error),+ )+ .then(() => rendered && check()),+ );+ }+ const unsubscribe = renderer.subscribe(check);+ const detach = document.history.status.subscribe(check);+ return () => {+ active = false;+ unsubscribe();+ detach();+ };+}++export function RemoveFieldSaving() {+ const document = useDocument();+ const renderer = useRenderer();+ useEffect(() => watchRemoveFields(document, renderer), [document, renderer]);+ return null;+}diff --git a/src/features/layers/controls.tsx b/src/features/layers/controls.tsxindex 36ae0a3..c55cf2e 100644--- a/src/features/layers/controls.tsx+++ b/src/features/layers/controls.tsx@@ -39,6 +39,7 @@ import { useShortcuts } from "@/hooks/use-shortcuts";import { layerDrop } from "./drop";import { deleteLayer, moveLayer, setLayer } from "./edits";import { LayerName } from "./layer-name";+import { LayerOpacity } from "./layer-opacity";import { useMaskTool } from "./mask-tool";import { LayerActions } from "./menu";import { ImageThumbnail, MaskThumbnail } from "./thumbnails";@@ -226,7 +227,10 @@ const LayerRow = memo(function LayerRow({</Tooltip>)}{layer.kind !== "image" && (- <LayerActions layer={layer} onSelect={onSelect} />+ <>+ <LayerOpacity layer={layer} />+ <LayerActions layer={layer} onSelect={onSelect} />+ </>)}</ListItemAction></ListItem>diff --git a/src/features/layers/layer-name.tsx b/src/features/layers/layer-name.tsxindex 8965f7c..aeafae2 100644--- a/src/features/layers/layer-name.tsx+++ b/src/features/layers/layer-name.tsx@@ -23,7 +23,7 @@ export function LayerName({aria-label="Layer name"ref={(input) => input?.select()}defaultValue={layer.name}- className="min-w-0 flex-1 rounded surface-field px-1 text-foreground outline-none"+ className="min-w-0 flex-1 rounded material-field px-1 text-foreground outline-none"onKeyDown={(event) => {if (event.key === "Enter") {event.currentTarget.blur();diff --git a/src/features/layers/layer-opacity.tsx b/src/features/layers/layer-opacity.tsxnew file mode 100644index 0000000..db7bc72--- /dev/null+++ b/src/features/layers/layer-opacity.tsx@@ -0,0 +1,27 @@+import { ScrubInput } from "@roprgm/ui/scrub-input";+import { useDocument } from "@/components/editor/session";+import type { ProcessingLayer } from "@/core/document";+import { useEditGesture } from "@/hooks/use-edit-gesture";+import { setLayer } from "./edits";++/** The row's opacity, which drags sideways as one edit and types on click. */+export function LayerOpacity({ layer }: { layer: ProcessingLayer }) {+ const document = useDocument();+ const gesture = useEditGesture(document.history);+ return (+ <span {...gesture} className="flex">+ <ScrubInput+ aria-label={`${layer.name} opacity`}+ value={layer.opacity * 100}+ min={0}+ max={100}+ defaultValue={100}+ format={(value) => `${value}%`}+ minChars={3}+ onChange={(value) =>+ setLayer(document, layer.id, { opacity: value / 100 })+ }+ />+ </span>+ );+}diff --git a/src/features/layers/toolbar.tsx b/src/features/layers/toolbar.tsxindex 5e41bfb..663bd81 100644--- a/src/features/layers/toolbar.tsx+++ b/src/features/layers/toolbar.tsx@@ -101,7 +101,7 @@ export function CanvasToolbar({ children }: { children?: ReactNode }) {// group at either end nests its corners 4px in; a text label first, such as a slider's, needs 8px// more, and a slider last 6px more, since a compact slider's value pulls 4px past its cell. On phones// the histogram floats at the top right, 11rem wide, so the bar stops 0.5rem short of it.- className="absolute top-3 left-3 flex min-w-0 max-w-[calc(100%-13rem)] items-center gap-x-2.5 overflow-hidden whitespace-nowrap rounded-full surface-panel bg-level-4/80 p-1 backdrop-blur-sm md:max-w-[calc(100%-1.5rem)] [&>:first-child:not(button,fieldset)]:ml-2 [&>:last-child:not(button,fieldset)]:mr-1.5"+ className="absolute top-3 left-3 flex min-w-0 max-w-[calc(100%-13rem)] items-center gap-x-2.5 overflow-hidden whitespace-nowrap rounded-full material-panel bg-level-4/80 p-1 backdrop-blur-sm md:max-w-[calc(100%-1.5rem)] [&>:first-child:not(button,fieldset)]:ml-2 [&>:last-child:not(button,fieldset)]:mr-1.5"><Density value={step === 0 ? "full" : "compact"}>{inlineTool}diff --git a/tests/heal-edit.e2e.ts b/tests/heal-edit.e2e.tsindex 3a6b016..4e88886 100644--- a/tests/heal-edit.e2e.ts+++ b/tests/heal-edit.e2e.ts@@ -64,7 +64,20 @@ for (const mode of ["Remove", "Heal"]) {.scene?.layers.find((layer) => layer.kind === "heal");return layer?.kind === "heal" ? layer.patches : [];});+ // Remove saves the field it synthesized into its patch once the render reads it back.+ const saved = () =>+ expect+ .poll(async () =>+ (await patches()).every(+ (patch) =>+ patch.mode !== "remove" ||+ patch.field?.strokes === patch.strokes.length,+ ),+ )+ .toBe(true);+ await saved();const original = (await patches())[0];+ const removed = await readImage(page, undefined, samples);const layerId = await page.evaluate(() => window.openlight.getState().selectedLayerId,);@@ -90,6 +103,7 @@ for (const mode of ["Remove", "Heal"]) {await page.mouse.up();await page.keyboard.up("Shift");await expect(preview).toHaveCount(0);+ await saved();const added = (await patches())[0];expect(await patches()).toHaveLength(1);expect(added).toMatchObject({@@ -109,6 +123,9 @@ for (const mode of ["Remove", "Heal"]) {}// Separate strokes never draw a connecting segment through the uncovered gap.expect(repaired.samples?.[2]).toEqual(before.samples?.[2]);+ // Remove keeps what it filled and synthesizes only the new stroke.+ if (original.mode === "remove")+ expect(repaired.samples?.[0]).toEqual(removed.samples?.[0]);await size.fill("20");await size.press("Enter");await page.keyboard.down("Alt");diff --git a/tests/heal.test.ts b/tests/heal.test.tsindex c5310cf..5ac2980 100644--- a/tests/heal.test.ts+++ b/tests/heal.test.ts@@ -7,10 +7,11 @@ import {createDocument,createResources,findLayer,+ removePatches,} from "@/core/document";import { createImageSource } from "@/core/image";import { imageFrame } from "@/core/image/frame";-import { createRenderGraph, input } from "@/core/renderer";+import { createRenderGraph, type FieldLattice, input } from "@/core/renderer";import { setAdjustments } from "@/features/adjustments/edits";import {addHealPatch,@@ -24,6 +25,7 @@ import {setHealSource,} from "@/features/heal/edits";import { inpaintField } from "@/features/heal/inpaint";+import { watchRemoveFields } from "@/features/heal/save-fields";import { addLayer, deleteLayer, setLayer } from "@/features/layers/edits";function healFixture(size: readonly [number, number] = [64, 64]) {@@ -51,15 +53,18 @@ test("Remove passes never overwrite distinct uniforms before their frame submitsconst gpu = await init();const source = target(gpu, { size: [64, 64], format: "rgba16float" });const coverage = target(gpu, { size: [64, 64], format: "r8unorm" });+ const base = target(gpu, { size: [64, 64], format: "rg16float" });const graph = createRenderGraph(gpu);const uploads = spyOn(gpu.gpu.queue, "writeBuffer");+ const lattice = { origin: [0, 0], scale: 1, size: source.size } as const;try {graph.render([inpaintField(input(source),{ coverage: input(coverage), origin: [0, 0] },- { origin: [0, 0], extent: source.size },+ lattice,"remove",+ { texels: input(base), lattice },),]);const uniforms = new Map<GPUBuffer, Uint8Array>();@@ -75,6 +80,7 @@ test("Remove passes never overwrite distinct uniforms before their frame submits} finally {uploads.mockRestore();graph.dispose();+ base.color.dispose();coverage.color.dispose();source.color.dispose();gpu.dispose();@@ -179,7 +185,7 @@ function patchesOf(document: ReturnType<typeof createDocument>, layer: string) {return healing.patches;}-test("a nested Remove retains synthesis when bypassed and invalidates upstream content and proxy changes", async () => {+test("a nested Remove synthesizes once for its strokes, whatever changes below or hides it, and extends on new strokes", async () => {const gpu = await init();const source = createImageSource(target(gpu, { size: [128, 96], format: "rgba16float" }),@@ -195,28 +201,36 @@ test("a nested Remove retains synthesis when bypassed and invalidates upstream c...dab,points: [[64, 48, 1]],});+ const render = (interactive = false) =>+ renderer.update(document.scene.getState(), undefined, interactive);const solves = () =>renderer.inspect().passes.some((name) => name.includes("/inpaint/"));+ const fields = () =>+ renderer.inspect().rasters.filter(({ id }) => id.endsWith("/field"));try {- await renderer.update(document.scene.getState());+ await render();expect(solves()).toBe(true);+ expect(fields()).toMatchObject([{ id: `${layer}/${patch}/field` }]);setHealPatch(document, layer, patch, { opacity: 0.5, feather: 0.4 });- await renderer.update(document.scene.getState());+ await render();expect(solves()).toBe(false);for (const id of [layer, mask]) {- setLayer(document, id, { visible: false });- await renderer.update(document.scene.getState());- expect(renderer.inspect().cachedTextures).toHaveLength(1);- setLayer(document, id, { visible: true });- await renderer.update(document.scene.getState());- expect(solves()).toBe(false);- setLayer(document, id, { opacity: 0 });- await renderer.update(document.scene.getState());- expect(renderer.inspect().cachedTextures).toHaveLength(1);- setLayer(document, id, { opacity: 1 });- await renderer.update(document.scene.getState());- expect(solves()).toBe(false);+ for (const change of [{ visible: false }, { opacity: 0 }]) {+ setLayer(document, id, change);+ await render();+ expect(fields()).toHaveLength(1);+ setLayer(document, id, { visible: true, opacity: 1 });+ await render();+ expect(solves()).toBe(false);+ }}+ setLayer(document, mask, { opacity: 0.5 });+ setAdjustments(document, { exposure: 1 }, mask);+ await render();+ expect(solves()).toBe(false);+ renderer.setDisplayScale(0.5);+ await render(true);+ expect(solves()).toBe(false);for (const mode of ["paint", "erase"] as const) {addHealStroke(document, layer, patch, {...dab,@@ -224,35 +238,23 @@ test("a nested Remove retains synthesis when bypassed and invalidates upstream csize: 2,points: [[64, 48, 1]],});- await renderer.update(document.scene.getState());+ // A proxy keeps the field it holds; only full resolution synthesizes the new stroke.+ await render(true);+ expect(solves()).toBe(false);+ await render();expect(solves()).toBe(true);setHealPatch(document, layer, patch, {opacity: mode === "paint" ? 0.6 : 0.5,});- await renderer.update(document.scene.getState());+ await render();expect(solves()).toBe(false);}- setLayer(document, mask, { opacity: 0.5 });- await renderer.update(document.scene.getState());- expect(solves()).toBe(false);- setLayer(document, mask, { visible: false });- await renderer.update(document.scene.getState());- setAdjustments(document, { exposure: 1 }, mask);- await renderer.update(document.scene.getState());- expect(solves()).toBe(false);- setLayer(document, mask, { visible: true });- await renderer.update(document.scene.getState());+ setHealDestination(document, layer, patch, [40, 40]);+ await render();expect(solves()).toBe(true);- renderer.setDisplayScale(0.5);- await renderer.update(document.scene.getState(), undefined, true);- expect(solves()).toBe(true);- await renderer.update(document.scene.getState());- expect(solves()).toBe(false);- setLayer(document, mask, { visible: false });- await renderer.update(document.scene.getState());deleteLayer(document, mask);- await renderer.update(document.scene.getState());- expect(renderer.inspect().cachedTextures).toEqual([]);+ await render();+ expect(fields()).toEqual([]);} finally {renderer.dispose();document.dispose();@@ -261,6 +263,69 @@ test("a nested Remove retains synthesis when bypassed and invalidates upstream c}});+test("Remove fields save once no gesture is open, a stroke added during a readback takes its field as a base, and snapshots wait for a readback", async () => {+ const { document, layer } = healFixture();+ // A renderer whose renders the test runs, and whose readbacks finish, oldest first, when it says.+ let render = () => {};+ const reads: (() => void)[] = [];+ const renderer = {+ subscribe(listener: () => void) {+ render = listener;+ return () => {};+ },+ readField: () =>+ new Promise<{ texels: Blob; lattice: FieldLattice }>((resolve) =>+ reads.push(() =>+ resolve({+ texels: new Blob(["field"]),+ lattice: { origin: [0, 0], scale: 1, size: [8, 8] },+ }),+ ),+ ),+ };+ async function finish() {+ reads.shift()?.();+ await new Promise((resolve) => setTimeout(resolve, 0));+ }+ const patches = () =>+ [...removePatches(document.scene.getState().layers)].map(+ ({ patch }) => patch,+ );+ const first = addRemovePatch(document, layer, dab);+ addRemovePatch(document, layer, dab);+ const { undoCount } = document.history.status.getState();+ const stop = watchRemoveFields(document, renderer);+ try {+ // A gesture drops the readback that finishes inside it and reads nothing more until it ends.+ render();+ document.history.begin();+ await finish();+ expect(reads).toHaveLength(0);+ expect(patches().map(({ field }) => field)).toEqual([undefined, undefined]);+ // Its end saves both fields, with no undo step.+ document.history.commit();+ await finish();+ await finish();+ expect(patches().map(({ field }) => field?.strokes)).toEqual([1, 1]);+ expect(document.history.status.getState().undoCount).toBe(undoCount);++ addHealStroke(document, layer, first, dab);+ addHealStroke(document, layer, first, dab);+ await finish();+ expect(patches()[0].strokes).toHaveLength(3);+ expect(patches()[0].field?.strokes).toBe(2);+ // The field for every stroke is being read back, and a snapshot waits for it.+ const snapshot = document+ .replaced()+ .then(() => patches()[0].field?.strokes);+ await finish();+ expect(await snapshot).toBe(3);+ } finally {+ stop();+ document.dispose();+ }+});+test("heal patches reuse brush rasters, scale with the proxy, undo, and release with the layer", async () => {const gpu = await init();const source = createImageSource(diff --git a/tests/inpaint-gpu.ts b/tests/inpaint-gpu.tsindex 777517b..8a9b4cb 100644--- a/tests/inpaint-gpu.ts+++ b/tests/inpaint-gpu.ts@@ -78,10 +78,12 @@ export async function renderInpaintReference(await gpu.gpu.queue.onSubmittedWorkDone();renderer.setDisplayScale(0.5);const start = performance.now();- await renderer.update(scene, undefined, proxy);+ // Only full resolution synthesizes; a proxy then shows the field it keeps.+ await renderer.update(scene);await gpu.gpu.queue.onSubmittedWorkDone();const completedMs = performance.now() - start;const solved = renderer.inspect();+ await renderer.update(scene, undefined, proxy);const result = renderer.fullImage();const actual = await result.readFloats();const before = await image.readFloats();@@ -128,7 +130,10 @@ export async function renderInpaintReference(undefined,proxy,);- const hiddenCaches = renderer.inspect().cachedTextures.length;+ const fields = () =>+ renderer.inspect().rasters.filter(({ id }) => id.endsWith("/field"))+ .length;+ const hiddenFields = fields();await renderer.update(scene, undefined, proxy);const shown = renderer.inspect();const visible = await renderer.fullImage().readFloats();@@ -163,7 +168,23 @@ export async function renderInpaintReference(undefined,proxy,);- const changed = renderer.inspect();+ const exposed = renderer.inspect();+ // The patch keeps its donors, whose colors follow the exposure below it.+ const brighter = await renderer.fullImage().readFloats();+ let exposedError = 0;+ for (let index = 0; index < brighter.length; index += 4) {+ const x = (index / 4) % result.size[0];+ const y = Math.floor(index / 4 / result.size[0]);+ const px = Math.floor((x + 0.5) * scale);+ const py = Math.floor((y + 0.5) * scale);+ if (Math.hypot(px - 96, py - 72) >= diameter / 2 - 2) continue;+ const reference = (py * size[0] + px) * 4;+ for (let channel = 0; channel < 3; channel++) {+ exposedError += Math.abs(+ brighter[index + channel] - expected[reference + channel] * 2 ** 0.5,+ );+ }+ }await renderer.update({...scene,@@ -186,7 +207,7 @@ export async function renderInpaintReference(0,);await renderer.update({ ...scene, layers: [scene.layers[0]] });- const released = renderer.inspect();+ const releasedFields = fields();await gpu.settled();return {fixture,@@ -199,7 +220,8 @@ export async function renderInpaintReference(alphaError,restoredError,visibilityError,- hiddenCaches,+ exposedError: exposedError / count,+ hiddenFields,shownSolverPasses: shown.passes.filter((name) =>name.includes("/inpaint/"),).length,@@ -211,10 +233,10 @@ export async function renderInpaintReference(cachedSolverPasses: cached.passes.filter((name) =>name.includes("/inpaint/"),).length,- changedSolverPasses: changed.passes.filter((name) =>+ exposedSolverPasses: exposed.passes.filter((name) =>name.includes("/inpaint/"),).length,- releasedCaches: released.cachedTextures.length,+ releasedFields,};} finally {renderer.dispose();@@ -223,3 +245,131 @@ export async function renderInpaintReference(gpu.dispose();}}++/**+ * A Remove patch over two spots on noise, where any other donor shows: a stroke added to it, or one+ * erasing part of it, leaves what it filled as it was and synthesizes only the rest.+ */+export async function renderInpaintExtension() {+ const gpu = await init();+ const errors: string[] = [];+ gpu.onError((error) => errors.push(error.message));+ const size: [number, number] = [192, 144];+ const image = target(gpu, { size, format: "rgba16float" });+ const source = createImageSource(image);+ const renderer = createEditorRenderer(gpu, source);+ const first = [56, 72] as const;+ const second = [140, 72] as const;+ const erased = [48, 72] as const;+ const stroke = (+ mode: "paint" | "erase",+ [x, y]: readonly [number, number],+ diameter: number,+ ) =>+ ({+ mode,+ size: diameter,+ feather: 0,+ flow: 1,+ points: [[x, y, 1]],+ }) as const;+ const strokes = [+ stroke("paint", first, 32),+ stroke("paint", second, 32),+ stroke("erase", erased, 12),+ ];+ const healing = createLayer("heal");+ const scene = (count: number): Scene => ({+ frame: imageFrame(size),+ layers: [+ createImageLayer("source", "Spots"),+ {+ ...healing,+ patches: [+ {+ id: "spots",+ mode: "remove",+ feather: 0,+ opacity: 1,+ strokes: strokes.slice(0, count),+ },+ ],+ },+ ],+ });+ try {+ frame(gpu, (f) =>+ f.pass(+ image,+ effect(+ gpu,+ `+ @fragment fn fs_main(@builtin(position) p: vec4f) -> @location(0) vec4f {+ let noise = fract(sin(dot(floor(p.xy), vec2f(12.9898, 78.233))) * 43758.5453);+ let spot = distance(p.xy, vec2f(${first.join(", ")})) < 12.0 || distance(p.xy, vec2f(${second.join(", ")})) < 12.0;+ return vec4f(select(vec3f(0.3 + 0.2 * noise, 0.4, 0.5), vec3f(0.015), spot), 1.0);+ }+ `,+ ),+ ),+ );+ const renders = [];+ for (const count of [1, 2, 3]) {+ await renderer.update(scene(count));+ renders.push({+ pixels: await renderer.fullImage().readFloats(),+ solved: renderer+ .inspect()+ .passes.some((name) => name.includes("/inpaint/")),+ });+ }+ const original = await image.readFloats();+ // The largest change in red between two renders over the pixels `within` selects.+ function change(+ a: Float32Array,+ b: Float32Array,+ within: (x: number, y: number) => boolean,+ ) {+ let largest = 0;+ for (let index = 0; index < a.length; index += 4) {+ const x = ((index / 4) % size[0]) + 0.5;+ const y = Math.floor(index / 4 / size[0]) + 0.5;+ if (within(x, y))+ largest = Math.max(largest, Math.abs(a[index] - b[index]));+ }+ return largest;+ }+ const near =+ ([cx, cy]: readonly [number, number], radius: number) =>+ (x: number, y: number) =>+ Math.hypot(x - cx, y - cy) < radius;+ const [filled, extended, subtracted] = renders.map(({ pixels }) => pixels);+ return {+ errors,+ solved: renders.map(({ solved }) => solved),+ // The first spot, as the first stroke filled it.+ keptByAdding: change(filled, extended, near(first, 14)),+ keptByErasing: change(+ filled,+ subtracted,+ (x, y) => near(first, 14)(x, y) && !near(erased, 8)(x, y),+ ),+ // The second spot was dark before the stroke over it, and the erased part is again.+ secondFilled: Math.min(+ ...[...extended].filter(+ (_, index) =>+ index % 4 === 0 &&+ near(second, 10)(+ ((index / 4) % size[0]) + 0.5,+ Math.floor(index / 4 / size[0]) + 0.5,+ ),+ ),+ ),+ erasedRestored: change(original, subtracted, near(erased, 4)),+ };+ } finally {+ renderer.dispose();+ source.dispose();+ gpu.dispose();+ }+}diff --git a/tests/inpaint.e2e.ts b/tests/inpaint.e2e.tsindex 2bdf384..13dea4a 100644--- a/tests/inpaint.e2e.ts+++ b/tests/inpaint.e2e.ts@@ -233,7 +233,7 @@ test("Remove moves on drop and cancels a pending move on undo, pointer cancellat}});-test("Remove synthesizes a spot without donor selection and reuses only unchanged content", async ({+test("Remove synthesizes a spot once, without donor selection, and follows the layers below", async ({page,}) => {await page.goto("/tests/gpu.html");@@ -251,12 +251,32 @@ test("Remove synthesizes a spot without donor selection and reuses only unchangeexpect(result.alphaError).toBe(0);expect(result.restoredError).toBe(0);expect(result.visibilityError).toBe(0);- expect(result.hiddenCaches).toBeGreaterThan(0);+ expect(result.exposedError).toBeLessThan(0.003);+ expect(result.hiddenFields).toBe(1);expect(result.shownSolverPasses).toBe(0);expect(result.solverPasses).toBeGreaterThan(0);expect(result.cachedSolverPasses).toBe(0);- expect(result.changedSolverPasses).toBeGreaterThan(0);- expect(result.releasedCaches).toBe(0);+ expect(result.exposedSolverPasses).toBe(0);+ expect(result.releasedFields).toBe(0);+});++test("Remove keeps what it filled when a stroke adds to or erases part of the patch", async ({+ page,+}) => {+ await page.goto("/tests/gpu.html");+ const result = await page.evaluate(async () => {+ const path = "/tests/inpaint-gpu.ts";+ const { renderInpaintExtension } = (await import(+ path+ )) as typeof import("./inpaint-gpu");+ return renderInpaintExtension();+ });+ expect(result.errors).toEqual([]);+ expect(result.solved).toEqual([true, true, true]);+ expect(result.keptByAdding).toBeLessThan(0.002);+ expect(result.keptByErasing).toBeLessThan(0.002);+ expect(result.secondFilled).toBeGreaterThan(0.25);+ expect(result.erasedRestored).toBe(0);});for (const fixture of ["gradient", "texture", "edge"] as const) {diff --git a/tests/layers.e2e.ts b/tests/layers.e2e.tsindex 49ee5f7..5d011f0 100644--- a/tests/layers.e2e.ts+++ b/tests/layers.e2e.ts@@ -176,6 +176,15 @@ test("draw masks, edit their child effects, reorder layers and undo", async ({await setField("Opacity", "0");expect(await samples()).toEqual(original);await undo();+ // The row's opacity drags to zero and undoes as one edit.+ const opacity = await box(+ page.getByRole("textbox", { name: "Sky opacity", exact: true }),+ );+ const y = opacity.y + opacity.height / 2;+ await drag(page, [opacity.x + opacity.width / 2, y], [opacity.x - 300, y]);+ expect(await samples()).toEqual(original);+ await undo();+ expect(await samples()).toEqual(masked);const show = page.getByRole("button", { name: "Show Sky", exact: true });await show.click();expect(await samples()).toEqual(original);diff --git a/tests/render-graph.test.ts b/tests/render-graph.test.tsindex 9c68336..780e33e 100644--- a/tests/render-graph.test.ts+++ b/tests/render-graph.test.ts@@ -25,49 +25,6 @@ function mixed(image: RenderImage, name: string, extra = {}) {node(name, shader, { ...options, ...extra }),);}-test("cached branches skip their dependencies, retain two revisions, and release owned results", async () => {- const gpu = await init();- const image = target(gpu, { size: [8, 8], format: "rgba16float" });- const source = input(image);- const graph = createRenderGraph(gpu);- const expensive = merge(- { source, base: source },- node("repair/search", shader, options),- );- const cached = (key: object) =>- merge(- { source: expensive, base: source },- node("repair/result", shader, { ...options, cacheKeys: [key] }),- );- try {- const firstKey = {};- const secondKey = {};- const [first] = graph.render([cached(firstKey)]);- expect(graph.inspect().passes).toEqual(["repair/search", "repair/result"]);- expect(graph.render([cached(firstKey)])[0]).toBe(first);- expect(graph.inspect().passes).toEqual([]);- const [second] = graph.render([cached(secondKey)]);- expect(second).not.toBe(first);- expect(graph.render([cached(firstKey)])[0]).toBe(first);- expect(graph.render([cached({})])[0]).toBe(first);- expect(graph.inspect().cachedTextures).toHaveLength(2);- // Eviction must keep a result that the caller is still using in this frame.- const latest = cached({});- expect(graph.render([input(second), latest])[0]).toBe(second);- expect(() => second.color.view).not.toThrow();- graph.render([latest]);- expect(() => second.color.view).toThrow("destroyed");- graph.release("repair/");- expect(graph.inspect().cachedTextures).toHaveLength(0);- expect(() => second.color.view).toThrow("destroyed");- expect(() => image.color.view).not.toThrow();- } finally {- graph.dispose();- image.color.dispose();- gpu.dispose();- }-});-test("pipeline and split preserve order, bypasses, and named merge inputs", async () => {const gpu = await init();const image = target(gpu, { size: [8, 4], format: "rgba16float" });diff --git a/tests/rendering-benchmark.ts b/tests/rendering-benchmark.tsindex 7ff7c75..3dadb81 100644--- a/tests/rendering-benchmark.ts+++ b/tests/rendering-benchmark.ts@@ -57,6 +57,7 @@ function summarize(values: number[]) {const pixelBytes: Record<string, number> = {r8unorm: 1,r16float: 2,+ rg16float: 4,rgba8unorm: 4,rgba16float: 8,rgba32float: 16,@@ -445,14 +446,11 @@ export async function benchmarkRendering(.map((value) => value.toString(16).padStart(2, "0")).join(""),// Source and driver memory are excluded.- intermediateBytes: [- ...storage.textures,- ...storage.cachedTextures,- ].reduce(+ intermediateBytes: storage.textures.reduce((sum, { size, format }) => sum + size[0] * size[1] * pixelBytes[format],0,),- // Brush rasters, paint, and the stroke buffer, at source resolution, outside the graph.+ // Brush rasters, paint, Remove fields, and the stroke buffer, outside the graph.rasterBytes: storage.rasters.reduce((sum, { size, format }) => sum + size[0] * size[1] * pixelBytes[format],0,diff --git a/tests/scene-file.e2e.ts b/tests/scene-file.e2e.tsindex 8c8976b..cba1a87 100644--- a/tests/scene-file.e2e.ts+++ b/tests/scene-file.e2e.ts@@ -6,11 +6,14 @@ test("a saved scene reopens the photo with its edits and keeps editing", async (page,}) => {await openPhoto(page);- // Inside the radial mask, and on the light strip outside it.+ // Inside the radial mask, on the light strip outside it, and the removed corner of the blue square.const samples = [[600, 400],[1100, 400],+ [445, 295],] as const;+ await page.getByRole("button", { name: "Export", exact: true }).click();+ const pending = page.waitForEvent("download");await page.evaluate(() => {const api = window.openlight;api.setAdjustments({ exposure: -1 });@@ -24,14 +27,34 @@ test("a saved scene reopens the photo with its edits and keeps editing", async (});api.setAdjustments({ exposure: 1.5 }, mask);api.setFill({ color: "#3060c0", blend: "soft-light" });+ api.addRemovePatch(api.addLayer("heal"), {+ mode: "paint",+ size: 40,+ feather: 0,+ flow: 1,+ points: [[445, 295, 1]],+ });+ // Saved in the same task as the stroke, before its field is read back, the scene still keeps the+ // field, so the reopened patch fills the same way.+ const save = [...document.querySelectorAll("button")].find(+ (button) => button.textContent === "Save scene",+ );+ if (!save) throw Error("Save scene is missing.");+ save.click();});- const edited = await readImage(page, undefined, samples);-- await page.getByRole("button", { name: "Export", exact: true }).click();- const pending = page.waitForEvent("download");- await page.getByRole("button", { name: "Save scene" }).click();const download = await pending;expect(download.suggestedFilename()).toBe("photo.openlight");+ const field = () =>+ page.evaluate(() => {+ const layer = window.openlight+ .getState()+ .scene?.layers.find((layer) => layer.kind === "heal");+ const patch = layer?.kind === "heal" ? layer.patches[0] : undefined;+ return patch?.mode === "remove" ? patch.field : undefined;+ });+ await expect.poll(field).toMatchObject({ strokes: 1 });+ const saved = await field();+ const edited = await readImage(page, undefined, samples);await page.reload();const picker = page.locator('input[type="file"]');@@ -43,6 +66,7 @@ test("a saved scene reopens the photo with its edits and keeps editing", async (});const exposure = page.getByRole("textbox", { name: "Exposure", exact: true });await expect(exposure).toHaveValue("-1.00");+ expect(await field()).toEqual(saved);expect(await readImage(page, undefined, samples)).toEqual(edited);await page.evaluate(() => window.openlight.setAdjustments({ exposure: 0 }));