Skip to content

Free open-source image editor for the browser

Paint colors with the Brush (#61)

Rodrigo Belfiore71cfb077012c
diff --git a/AGENTS.md b/AGENTS.md
index ad1a429..6a9cce5 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -2,7 +2,7 @@
A professional photo editor with little, readable code: Vite, React, [vgpu](https://vgpu.sh) on WebGPU, Bun, and Biome.
-[ARCHITECTURE.md](ARCHITECTURE.md) maps the code and its layer rules; start there to find what a request touches. [CONTEXT.md](CONTEXT.md) names domain terms, [DESIGN.md](DESIGN.md) covers UI patterns, [REVIEW.md](REVIEW.md) covers reviewing and verifying a change, and [API.md](API.md) documents `window.openlight`.
+Start with [ARCHITECTURE.md](ARCHITECTURE.md) to find the affected code and its layer rules. [CONTEXT.md](CONTEXT.md) names domain terms. For UI changes, read [DESIGN.md](DESIGN.md) for the library and editor components. [REVIEW.md](REVIEW.md) covers verification, and [API.md](API.md) documents `window.openlight`.
## Setup
@@ -31,12 +31,12 @@ Run the first three on code changes; CI runs them too. Run browser tests for wha
- File names use kebab-case. Prefer named exports; use `@/` across folders and relative imports within one.
- Files read from small to large: define a function above the functions that use it.
- Comment only what the code cannot say.
-- Keep Tailwind classes inline and use `cva` for variants. Text has one size; express hierarchy with color and weight.
+- Keep Tailwind classes inline; use `cva` for local variants when needed.
## Quality
- Code quality comes first and technical debt is not accepted: no workarounds, dead code, or TODOs in place of a fix. When the right fix is larger than the request, say so instead of patching around it.
-- Find where the behavior lives and change it there, with the fewest lines that solve it well. Add no tests, helpers, abstractions, or docs the request does not need.
+- Change behavior at its owner and keep the diff within the request. Preserve unrelated UI and behavior. Add only the tests, helpers, abstractions, or docs the change needs.
- Give each module, component, and function one responsibility. Start with direct functions and library calls; prefer a few repeated lines over coupling unrelated behavior.
- Drive behavior from the state that causes it, such as props or document state, never from incidental DOM structure, selectors, or timing.
- Keep control flow linear with guard clauses and `const`. Avoid nested ternaries and dense logic in JSX.
diff --git a/API.md b/API.md
index a247316..43d3ce7 100644
--- a/API.md
+++ b/API.md
@@ -64,6 +64,8 @@ Create an empty Heal effect with `addLayer("heal")`.
`addHealPatch(id, stroke, offset)` appends a Healing patch and returns its ID. `stroke` is a painted `BrushStroke`, and `offset` is `[sourceX - targetX, sourceY - targetY]` in source pixels. `setHealSource(id, patchId, offset)` changes a donor. Patches replay in order from the accumulated composite below; the tool supplies automatic donors.
+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.
+
For RAW sources, `setWhiteBalance({ temperature, tint })` sets absolute Kelvin and DNG tint, preserving unspecified values. Temperature accepts 2000–25000 K and tint accepts -150–150, extending either range to include the file's As Shot value. `setWhiteBalance()` restores that value (the decoder's daylight fallback if camera multipliers are unavailable). Non-RAW sources and invalid values throw. Incremental temperature/tint remain separate RGB adjustments.
`autoWhiteBalance()` neutralizes the photo's color cast as one edit and resolves once it is made. The GPU measures the cast of the source before any edit, as its edges' average color, so calling it again gives the same balance. A RAW photo's white balance moves from As Shot; any other photo's incremental temperature and tint are replaced. When another photo opens first, it resolves without an edit.
@@ -82,7 +84,7 @@ Direct mask children of another mask modify coverage instead of processing image
| Method | Behavior |
| --- | --- |
-| `addLayer(kind, placement?)` | Adds `"exposure"`, `"color-mixer"`, `"details"`, `"vignette"`, `"grain"`, `"fill"`, `"heal"`, or `"mask"`; selects and returns its ID. A `"lut"` layer comes from a `.cube` file instead. `{ inside: id }` appends a child to a processing layer; `{ above: id }` inserts directly above that layer among its siblings; without a placement, the layer goes on top of the root stack. Exposure starts at +1 EV, Vignette at intensity 50, Grain at amount 25; Color Mixer, Details, Healing, and masks start neutral. |
+| `addLayer(kind, placement?)` | Adds `"exposure"`, `"color-mixer"`, `"details"`, `"vignette"`, `"grain"`, `"fill"`, `"heal"`, `"paint"`, or `"mask"`; selects and returns its ID. A `"lut"` layer comes from a `.cube` file instead. `{ inside: id }` appends a child to a processing layer; `{ above: id }` inserts directly above that layer among its siblings; without a placement, the layer goes on top of the root stack. Exposure starts at +1 EV, Vignette at intensity 50, Grain at amount 25; Color Mixer, Details, Healing, and masks start neutral. |
| `selectLayer(id)` | Selects any layer. |
| `setLayer(id, change)` | Updates processing-layer `name`, `visible`, or `opacity` (0–1). |
| `setExposure(id, value)` | Sets an Exposure layer to -5…5 EV. |
@@ -94,7 +96,7 @@ Direct mask children of another mask modify coverage instead of processing image
A linear gradient has full coverage at `start`, zero at `end`; its points must be finite and distinct. A radial gradient covers the ellipse inside `radius`, with positive radii and a feathered falloff toward its edge. Crop, rotation, and viewport navigation do not move it within the document.
-A brush mask is a list of strokes. Each stroke has `mode` (`"paint"` or `"erase"`), `size` (diameter in source pixels), `feather` and `flow` (0–1), and `points`, each `[x, y, pressure]` in source pixels with pressure 0–1. Dabs land every quarter diameter along the points; a paint stroke adds `flow × pressure` of the remaining coverage under each dab, and an erase stroke removes that share of the existing coverage. The renderer rasterizes strokes into a cached coverage texture at source resolution and only stamps new points, so appending to the last stroke is cheap and undo replays the rest. A brush inside another mask keeps its own coverage, paint and erase strokes alike, which the mask adds or subtracts scaled by the brush layer's opacity. A mask with no painted coverage and nothing added to it is bypassed.
+A brush mask is a list of strokes, over the pixels earlier strokes settled into, as a Paint layer's are: in the editor they settle every 100 strokes, and `getState()` shows the mask's `raster` ID. `setLayerMask` keeps a `raster` only if it names the document's own settled pixels. Each stroke has `mode` (`"paint"` or `"erase"`), `size` (diameter in source pixels), `feather` and `flow` (0–1), and `points`, each `[x, y, pressure]` in source pixels with pressure 0–1. A paint stroke adds `flow × pressure` of the remaining coverage every quarter diameter along the points, and an erase stroke removes that share of the existing coverage; dabs land sixteen times per diameter, each laying a fraction of that, so soft edges show no ripples. The renderer rasterizes strokes into a cached coverage texture at half the photo's resolution each way and only stamps new points, so appending to the last stroke is cheap and undo replays at most 99. A brush inside another mask keeps its own coverage, paint and erase strokes alike, which the mask adds or subtracts scaled by the brush layer's opacity. A mask with no painted coverage and nothing added to it is bypassed.
## LUTs
diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md
index 2f075a6..002321c 100644
--- a/ARCHITECTURE.md
+++ b/ARCHITECTURE.md
@@ -21,6 +21,7 @@ Where the code lives and the rules that keep it in place. Keep this file true: w
| `app/editor/empty.tsx` | The editor before a document opens: welcome or loading status, and the placeholder sidebar. |
| `app/editor/sidebar.tsx` | Sidebar sections in order: `EditorSidebar` with a document, `PlaceholderSidebar` without one. |
| `app/editor/tools.tsx`, `tool-rail.tsx` | The tools and the desktop rail. A tool brings a `Canvas` overlay, `Options` for the bar over the image or the dock, or its own `View`. |
+| `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. |
| `app/editor/mask-overlay.tsx` | The only writer of the mask overlay, derived from the selection. |
@@ -74,9 +75,11 @@ Dependencies point downward. Features do not import each other; `app/` connects
- 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.
- A LUT layer converts explicitly: its table expects display-referred sRGB, so it receives the working color as the display shows it, headroom above white clipped, and its output returns to linear Rec.2020.
- A node's storage arrays upload when the array changes; passing the same array, such as a LUT's table, keeps its buffer.
-- Layers process in stack order; the image layer's adjustments and tone curve run last, on the composite.
-- Preserve HDR headroom through exposure, curves, and vibrance. Exposure clips negatives and applies one luminance gain to all channels.
+- The image layer's adjustments and tone curve develop the photo first; its children and the layers above process that result in stack order. Paint colors therefore follow the photo's adjustments. Working textures stay floating-point through composition; display and export encode the final result.
+- Preserve HDR headroom through exposure, curves, and vibrance. Exposure multiplies linear RGB by `2^EV`, without clipping, so +3 EV followed by -3 EV recovers the original light even when intermediate values exceed 1.
- The adjustment shader's parameters use UI units; its fitted constants are calibration data.
-- Strokes are scene content; the renderer caches their rasterized coverage and stamps only appended dabs.
+- 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 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 patch's hard stroke rounds the same either way, so it stamps straight into a raster over only the 256 px tiles it reaches.
+- 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. The mask module combines that coverage with child masks. Gradient masks need no texture; the mix pass computes them. 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.
- 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.
- TIFF and camera RAW decode through `raw-webgpu`; OpenLight adapts its resources to document ownership.
diff --git a/CONTEXT.md b/CONTEXT.md
index 149c1e3..38bb911 100644
--- a/CONTEXT.md
+++ b/CONTEXT.md
@@ -6,15 +6,15 @@ OpenLight edits documents containing image content and adjustments.
| --- | --- |
| **Document** | An independent editing session with its own canvas, history, and resources. Opening an image creates a document containing that image. |
| **Scene** | The document's serializable content: canvas geometry, image-source references, and adjustments. Files and GPU resources live outside it. |
-| **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, and brush child masks add or subtract coverage. Layers compose render nodes without owning textures. |
-| **Stroke** | One brush drag: paint or erase, size, feather, flow, and points with pressure in source pixels. Strokes are content; the renderer rasterizes them into a cached coverage texture. A Healing patch stores a hard stroke and feathers its accumulated shape separately. |
+| **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, and brush child masks add or subtract 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 a hard stroke, rasterized over only the tiles it reaches, and feathers its accumulated shape separately. |
| **Healing patch** | One painted, non-destructive repair in a Healing layer. It copies a nearby donor, found automatically or set by hand, with a bounded boundary color correction. Patches replay in order and each sees the result of the preceding patches. Size belongs to the recorded hard stroke; 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 and the original source files. 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 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, 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. |
| **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, 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. |
+| **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. |
Use these names consistently in code, UI text, and documentation.
diff --git a/DESIGN.md b/DESIGN.md
index 71eb197..e082e3a 100644
--- a/DESIGN.md
+++ b/DESIGN.md
@@ -1,53 +1,22 @@
-# Interface design system
-
-OpenLight uses a small set of shared UI primitives so the editor reads as one application across tools and panels. They come from [`@roprgm/ui`](https://ui.roprgm.com), imported per component as `@roprgm/ui/<name>`, with its theme imported in `src/index.css`; floating parts are built on [Base UI](https://base-ui.com), which owns positioning, focus, dismissal, keyboard navigation, and ARIA. `src/components/ui` keeps only what the library does not provide yet: `TextLink`, `TreeDrag`, and `Dial`.
-
-| Component | Use |
-| --- | --- |
-| `Button`, `IconButton` | Actions. `IconButton` is a ghost icon button whose `label` names it and shows as its tooltip, with an optional `shortcut`; use `size="icon-sm"` in headers, rows, and bars. |
-| `Chip` | Pill buttons in the bar over the canvas and the dock; `aria-pressed` shows a toggle on. A segmented group is a `fieldset` of chips; `DockChips` is the dock's, and scrolls to the chip chosen. |
-| `Tooltip` | A hint on hover or focus for any control: a `TooltipTrigger` renders the control and `TooltipContent` holds the hint, with an optional shortcut written as `Mod Z` (⌘ or Ctrl). |
-| `Menu`, `MenuItem`, `MenuSeparator`, `Submenu` | Commands such as Duplicate, Move, or Delete, in a `MenuContent` opened by a `MenuTrigger` that renders a control such as an `IconButton`; a `Submenu` holds a `SubmenuTrigger` and a `SubmenuContent`. |
-| `Select` | A choice that replaces a current value, with `field` or `pill` trigger. |
-| `Popover` | Settings that open beside a `PopoverTrigger`, such as bar controls that no longer fit; its `PopoverContent` holds `CardSection`s. |
-| `Slider`, `ScrubInput` | Numbers. A `Slider` is `panel`, `toolbar`, or `compact`, or stands upright with `orientation="vertical"`; `format` writes the value and whatever follows its digits reads as the unit. |
-| `Dial`, `DialRow` | Numbers in the mobile dock: a drag anywhere on the dial sets it, right or up to add, a tap picks it for typing, and a double tap resets. A touch moves nothing until it drags past a few pixels. A `DialRow` never scrolls under a finger: its dials center with a capped gap, and those that don't fit page behind an arrow. |
-
-## Selection and menus
-
-- Use `Menu` for commands and `Popover` for settings; a menu's arrow keys and typeahead would fight sliders and fields inside it. Menu items close the menu when chosen, and nested choices use a `Submenu` rather than a control inside the menu.
-- Use `Select` for a choice that replaces a current value. Its options check the selected one.
-- Do not render a native HTML `select` in product UI. Specialized browser pickers such as file and color inputs are allowed when the browser owns behavior OpenLight does not reproduce.
-- While a surface is open it owns the keyboard: editor shortcuts pause until it closes.
-
-## Tooltips
-
-- Every icon-only control has a tooltip; use `IconButton` or give the control a `Tooltip`. Do not use the native `title` attribute.
-- A tooltip names the control in a few words and adds the shortcut beside it; longer guidance belongs in the panel. The canvas shows only brief status, such as an error or a stroke finishing.
-- A tooltip is not the accessible name. Keep `aria-label` on icon-only controls; `IconButton` sets it from `label`.
-- Text that may be cut short, such as a layer name, shows its tooltip only when truncated.
-
-## Panels
-
-- `EditorPanel` stacks its sections in the order written with a divider between each; `PanelBody` is the section that takes the remaining height, with its `header` fixed above the part that scrolls. `PanelHeader` titles a section and rules it off. The layers section keeps room for three rows and shows, empty, before a document opens. The editing sidebar's order is the JSX in `app/editor/sidebar.tsx`.
-- `EditorViewport` is the canvas region: a `ViewportStage` inside it pans and zooms with the image, and other children float over it without panning.
-- A feature describes its numbers once as `Parameter`s, which the sidebar draws as `Slider`s and the dock as `Dial`s. Controls that aren't numbers, such as a curve or a color, are the same component in both.
-- In the dock, `DockControls` puts a centered header, usually `DockChips` choosing a group or mode, over a `DialRow`; while a dial drags, its name and value replace the header, and a tapped dial keeps them there as a field to type into until Done. Until someone first moves a dial, a hint floats above the dock, once per browser. The dock takes its view's height: dials keep it low, and a graph such as the curve raises it with a short transition. A view whose sidebar panel doesn't suit a phone, such as Crop, passes its own `dock` to `EditorLayout`.
-
-## Panel collections
-
-- Use `ListItem` for selectable rows in editor panels. It owns row height, divider, hover, muted, and selected states.
-- A row may compose its own thumbnail, label, compact fields, and action menu. Keep those feature-specific parts with the feature.
-- Keep lists flush with the panel edges. Put empty-state copy inside the panel padding rather than padding every populated row.
-
-## Surfaces and controls
-
-- Use shared components for repeated interaction and surface behavior. Keep feature-specific composition in the feature.
-- Use Tailwind tokens already present in the shared primitive for color, borders, shadows, focus, and pointer-coarse sizing. A caller chooses a documented variant instead of recreating the surface.
-- On a coarse pointer, the header's controls grow to 40 px while their icons keep their size. The dock's tab bar keeps only its icons below 23rem, where the names no longer fit.
-- Anchor viewport-corner controls and messages 12 px from both edges. Tool overlays cover the full viewport; the camera preserves a 24 px margin around fitted content without shrinking that coordinate space.
-- Use `Notice` for a message floating over the viewport that must not block editing, such as a storage failure or an offer to recover a draft. Choose its `tone` (status or alert, which sets the ARIA role) and position it with classes, `fixed` over the window or `absolute` inside the editor viewport. Its message and buttons are `CardSection`s; when the user may close it, a `NoticeClose` goes in a `CardAction` beside the message.
-- Use `TextLink` for an underlined link inside running text. It renders an anchor with `href` and a button otherwise; the `muted` variant stays in the sentence's color for secondary actions such as Forget.
-- Use `ScrubInput` for numbers that read as inline text, drag sideways, and accept typing. It hugs its digits and unit and shows a subtly rounded background only while text editing is active.
-- Give toolbar slider values an explicit minimum character width (`valueWidth`) when their expected range changes digit count. It counts digits, with the unit after them: percentages reserve three tabular characters, while brush size uses the actual maximum's digit count. Do not guess a global maximum.
-- Text uses the global body size. Express hierarchy with weight and color. The About page is the exception: it is read, not operated, so its text is a step larger.
+# Interface design
+
+## UI library
+
+Use [`@roprgm/ui`](https://ui.roprgm.com) for standard controls, imported as `@roprgm/ui/<name>`. Its theme is loaded in `src/index.css`. The library owns their appearance and interaction; use its defaults and supported variants. Consult the installed component API when choosing props.
+
+Choose `Button` or `IconButton` for actions, `Menu` for commands, `Popover` for settings, and `Select` for choices. Use the library's other primitives wherever they cover the interaction.
+
+Keep styling overrides specific to an editor requirement demonstrated by the task. An existing override is not a rule to copy onto other controls.
+
+## Editor composition
+
+The photo editor has layouts and interactions the library does not provide. Reuse the existing editor components and compose library primitives inside them:
+
+- `EditorPanel`, `PanelBody`, and `PanelHeader` organize the desktop sidebar.
+- `EditorViewport` and `ViewportStage` separate the image from overlays that stay fixed in the viewport.
+- Features describe numeric controls as `Parameter`s, rendered as library sliders on desktop and local dials in the mobile dock. Reuse `DockControls`, `DockChips`, and `DialRow` for that composition.
+- Layer rows use `ListItem` with their own thumbnails, controls, and drag behavior.
+
+Keep editor-specific controls with their feature. `src/components/ui` holds shared interactions missing from the library, such as `Dial`, `TreeDrag`, and `TextLink`. Add a local component only when existing primitives cannot provide the required behavior.
+
+See [ARCHITECTURE.md](ARCHITECTURE.md#layouts) for layout ownership and [REVIEW.md](REVIEW.md#verify) for visual verification of UI changes.
diff --git a/README.md b/README.md
index 7853421..279034c 100644
--- a/README.md
+++ b/README.md
@@ -20,7 +20,7 @@ Image processing runs on your own GPU with WebGPU. Photos never leave your devic
**Layers and masks**
-- Draggable effect layers: Details, Exposure, Color Mixer, Vignette, Grain, Color, which paints one color with Photoshop blend modes, and LUT, which grades the photo with a 3D LUT from a `.cube` file.
+- Draggable effect layers: Details, Exposure, Color Mixer, Vignette, Grain, Color, which paints one color with Photoshop blend modes, Paint, which holds brush strokes in any colors with the same modes, and LUT, which grades the photo with a 3D LUT from a `.cube` file.
- Linear and radial masks with their own adjustments and child effects.
- Add and Subtract submasks, from a mask's actions menu, to shape its coverage.
@@ -28,7 +28,9 @@ Image processing runs on your own GPU with WebGPU. Photos never leave your devic
- Healing paints patches from an automatic or manually positioned donor. Repairs remain editable and each patch builds on the previous result.
- Brush, linear, and radial gradient tools on the rail draw masks on top of the stack; their options sit in the bar over the image.
-- Brush masks with size, feather, flow, pen pressure, and Alt to erase.
+- The Brush paints colors on a Paint layer or coverage on a brush mask, with size, feather, flow, pen pressure, and Alt to erase. Its primary and secondary colors sit at the foot of the tool rail, as in Photoshop.
+- Photoshop's keys: B on the Brush switches Color and Mask; X swaps the colors, or paint and erase on a mask; D resets them to black and white; I picks one from the screen where the browser offers it; `[` and `]` size the brush and Shift with them its feather; digits set the flow and Shift with them the layer's opacity. A right click opens size and feather where you paint.
+- A photo holds up to 4 Paint layers and 10 brush masks, since each keeps a raster at half the photo's resolution. A Paint layer's or brush mask's strokes settle into pixels every 100 strokes, so undo stays quick however much you paint, and still reaches back through them.
- The mask overlay shows until a mask changes the image, then the image shows the mask; the overlay button beside the mask's adjustments, or O, shows or hides it for every mask.
- Edits preview at a reduced resolution while you drag, then render in full.
diff --git a/src/app/controls.ts b/src/app/controls.ts
index 398c2d8..dfab81b 100644
--- a/src/app/controls.ts
+++ b/src/app/controls.ts
@@ -15,11 +15,13 @@ import { createLoaderRegistry } from "@/app/loaders/registry";
import { createSceneLoader, writeSceneFile } from "@/app/loaders/scene";
import type {
Adjustments,
+ Blend,
BrushStroke,
Details,
Fill,
Grain,
Mask,
+ PaintStroke,
Preview,
ToneCurve,
Vignette,
@@ -53,6 +55,7 @@ import {
setMaskOperation,
} from "@/features/layers/edits";
import { defaultGradient } from "@/features/layers/gradient";
+import { addPaintStroke, setPaintBlend } from "@/features/paint/edits";
import { defaultCurve } from "@/features/tone-curves/curve";
import { setToneCurve } from "@/features/tone-curves/edits";
import { setVignette } from "@/features/vignette/edits";
@@ -147,6 +150,10 @@ export function createControls(
addHealPatch(workspace.getDocument(), id, stroke, offset),
setHealSource: (id: string, patchId: string, offset: Point) =>
setHealSource(workspace.getDocument(), id, patchId, offset),
+ addPaintStroke: (id: string, stroke: PaintStroke) =>
+ addPaintStroke(workspace.getDocument(), id, stroke),
+ setPaintBlend: (id: string, blend: Blend) =>
+ setPaintBlend(workspace.getDocument(), id, blend),
addLayer(kind: DefaultEffect | "mask", placement?: LayerPlacement) {
const document = workspace.getDocument();
if (kind !== "mask") {
diff --git a/src/app/draft/store.ts b/src/app/draft/store.ts
index 8d62067..f4b2555 100644
--- a/src/app/draft/store.ts
+++ b/src/app/draft/store.ts
@@ -8,7 +8,7 @@ import { parse } from "@/lib/parse";
/** Raised only when older drafts can no longer open as written; the scene inside keeps its own version. */
const version = 1;
-/** The latest document: the scene JSON a scene file holds, while its source files live in their own store by ID. */
+/** The latest document: the scene JSON a scene file holds, while the files it names live in their own store by ID. */
const recordSchema = z.object(
{
version: z.int().check(z.minimum(1)),
@@ -22,8 +22,11 @@ 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, files } = snapshotScene(document);
- return { record: { version, name, scene: json }, files };
+ const { json, sources, paint } = snapshotScene(document);
+ return {
+ record: { version, name, scene: json },
+ files: new Map<string, Blob>([...sources, ...paint]),
+ };
}
/** Storage returns whatever an earlier version wrote, so the record is parsed where it is read; the scene opens later. */
@@ -49,7 +52,8 @@ interface DraftDatabase extends DBSchema {
}
/**
- * One draft in IndexedDB: the record under a single key, and source files keyed by source ID.
+ * 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.
* Operations run one at a time in call order, so a discard never races a save.
*/
export function createDraftStore(name = "openlight") {
@@ -82,7 +86,7 @@ export function createDraftStore(name = "openlight") {
}
return {
- /** Keeps source files already stored under their ID and deletes the ones the draft no longer uses. */
+ /** Keeps files already stored under their ID and deletes the ones the draft no longer uses. */
save({ record, files }: Draft) {
return run(async (database) => {
const transaction = database.transaction(
@@ -103,14 +107,14 @@ export function createDraftStore(name = "openlight") {
]);
});
},
- /** The draft's display name, without reading its source files. */
+ /** The draft's display name, without reading its files. */
peek() {
return run(async (database) => {
const record = await database.get("draft", "latest");
return record && { name: readRecord(record).name };
});
},
- /** The record with every stored source file; a save leaves only the files the draft uses. */
+ /** The record with every stored file; a save leaves only the files the draft uses. */
read() {
return run(async (database): Promise<Draft | undefined> => {
const transaction = database.transaction(["draft", "sources"]);
diff --git a/src/app/editor/adjust/index.tsx b/src/app/editor/adjust/index.tsx
index 2fea820..f812074 100644
--- a/src/app/editor/adjust/index.tsx
+++ b/src/app/editor/adjust/index.tsx
@@ -40,6 +40,7 @@ import { Histogram } from "@/features/histogram";
import { setLayer } from "@/features/layers/edits";
import { OverlayToggle } from "@/features/layers/overlay-toggle";
import { LutCurves } from "@/features/lut/curves";
+import { PaintControls } from "@/features/paint/controls";
import { defaultCurve } from "@/features/tone-curves/curve";
import { setToneCurve } from "@/features/tone-curves/edits";
import { ToneCurves } from "@/features/tone-curves/tone-curves";
@@ -171,6 +172,8 @@ function SelectedControls({ layer }: { layer: Layer }) {
}
case "heal":
return <HealControls id={layer.id} patches={layer.patches} />;
+ case "paint":
+ return <PaintControls layer={layer} />;
case "exposure": {
const { id, ...parameter } = exposureParameter(document, layer);
return (
@@ -403,6 +406,12 @@ function SelectedDials({
</div>
</DockControls>
);
+ case "paint":
+ return (
+ <DockControls header={<DockTitle layer={layer} />}>
+ <PaintControls layer={layer} />
+ </DockControls>
+ );
}
}
diff --git a/src/app/editor/brush-keys.tsx b/src/app/editor/brush-keys.tsx
new file mode 100644
index 0000000..917e451
--- /dev/null
+++ b/src/app/editor/brush-keys.tsx
@@ -0,0 +1,54 @@
+import { flowAt, useBrushTool } from "@/components/editor/brush-tool";
+import { useDocument } from "@/components/editor/session";
+import { findLayer } from "@/core/document";
+import { setLayer } from "@/features/layers/edits";
+import { usePaintColors } from "@/features/paint/colors";
+import { useShortcuts } from "@/hooks/use-shortcuts";
+import { useTool } from "./tools";
+
+/** Digits as Photoshop reads them: 1 is 10%, 9 is 90%, and 0 is 100%. */
+const digits = Array.from({ length: 10 }, (_, digit) => ({
+ key: `${digit}`,
+ share: (digit || 10) / 10,
+}));
+
+/**
+ * Photoshop's keys for the brush and its colors. X swaps the colors, or paint and erase while the
+ * Brush paints a mask; D resets them; I picks one. On the Brush, a digit sets the flow, and Shift with
+ * it the selected layer's opacity.
+ */
+export function BrushKeys() {
+ const document = useDocument();
+ const { tool } = useTool();
+ const brush = useBrushTool();
+ const colors = usePaintColors();
+ const brushing = tool.id === "brush";
+ function swap() {
+ if (brushing && brush.settings.mode === "mask") {
+ brush.update({ erase: !brush.settings.erase });
+ return;
+ }
+ colors.swap();
+ }
+ function setOpacity(opacity: number) {
+ const { layerId } = document.selection.getState();
+ if (
+ findLayer(document.scene.getState().layers, layerId)?.kind !== "image"
+ ) {
+ setLayer(document, layerId, { opacity });
+ }
+ }
+ const levels = brushing
+ ? digits.flatMap(({ key, share }) => [
+ [key, () => brush.update({ flow: flowAt(share) })] as const,
+ [`shift+${key}`, () => setOpacity(share)] as const,
+ ])
+ : [];
+ useShortcuts({
+ x: swap,
+ d: colors.reset,
+ ...(colors.pick && { i: colors.pick }),
+ ...Object.fromEntries(levels),
+ });
+ return null;
+}
diff --git a/src/app/editor/brush.tsx b/src/app/editor/brush.tsx
new file mode 100644
index 0000000..1c8de87
--- /dev/null
+++ b/src/app/editor/brush.tsx
@@ -0,0 +1,109 @@
+import { useRef } from "react";
+import { useStore } from "zustand";
+import { BrushCanvas } from "@/components/editor/brush-canvas";
+import {
+ type BrushMode,
+ brushMode,
+ useBrushTool,
+} from "@/components/editor/brush-tool";
+import { CanvasHint } from "@/components/editor/canvas-hint";
+import { useDesktopLayout } from "@/components/editor/layout";
+import { useDocument, useScene } from "@/components/editor/session";
+import { useToolLayer } from "@/components/editor/tool-layer";
+import { findLayer, type Layer } from "@/core/document";
+import { BrushOptions } from "@/features/layers/brush-options";
+import {
+ addLayer,
+ brushLayerCounts,
+ brushLimit,
+ brushLimits,
+ extendStroke,
+ paintStroke,
+} from "@/features/layers/edits";
+import { useMaskTool } from "@/features/layers/mask-tool";
+import { PaintColors } from "@/features/paint/colors";
+import { addPaintStroke, extendPaintStroke } from "@/features/paint/edits";
+import { createLayer } from "./layers";
+
+/** Paints color or mask coverage on the selected layer, dropping an untouched new one on leaving. */
+function BrushOverlay({ mode, full }: { mode: BrushMode; full: boolean }) {
+ const document = useDocument();
+ const tool = useMaskTool();
+ const brush = useBrushTool();
+ const painting = useRef<string | undefined>(undefined);
+ const selected = useToolLayer({
+ accepts: (layer: Layer): layer is Layer => brushMode(layer) === mode,
+ create: full
+ ? undefined
+ : () => {
+ if (mode === "mask") {
+ tool.create({ kind: "brush", strokes: [] });
+ } else {
+ addLayer(document, createLayer("paint"));
+ }
+ },
+ leave: () => tool.edit(),
+ // A chosen nesting starts a new brush even over a selected one.
+ fresh: mode === "mask" && tool.pending?.shape === "brush",
+ });
+ return (
+ <BrushCanvas
+ label={mode === "mask" ? "Brush canvas" : "Paint canvas"}
+ erase={brush.erase}
+ onStart={(stroke) => {
+ const id = selected()?.id;
+ if (!id) return false;
+ painting.current = id;
+ if (mode === "mask") {
+ paintStroke(document, id, stroke);
+ } else {
+ addPaintStroke(document, id, {
+ ...stroke,
+ color: brush.settings.colors[0],
+ });
+ }
+ return true;
+ }}
+ onExtend={(points) => {
+ const id = painting.current;
+ if (!id) return;
+ const extend = mode === "mask" ? extendStroke : extendPaintStroke;
+ extend(document, id, points);
+ }}
+ onDone={() => tool.edit()}
+ />
+ );
+}
+
+/**
+ * The Brush tool paints color on a paint layer or coverage on a brush mask, whichever its mode says.
+ * The mode follows the selection onto either kind, and choosing the other mode starts a layer of it.
+ * A photo at its limit of layers of the mode adds none, and the canvas says why.
+ */
+export function BrushToolCanvas() {
+ const document = useDocument();
+ const { mode } = useBrushTool().settings;
+ const full = useScene(
+ (scene) => brushLayerCounts(scene.layers)[mode] >= brushLimits[mode],
+ );
+ const selectedId = useStore(document.selection, (state) => state.layerId);
+ const selected = useScene((scene) =>
+ brushMode(findLayer(scene.layers, selectedId)),
+ );
+ return (
+ <>
+ <BrushOverlay key={mode} mode={mode} full={full} />
+ {full && selected !== mode && (
+ <CanvasHint>
+ {brushLimit(mode)}: select one to paint on, or delete one.
+ </CanvasHint>
+ )}
+ </>
+ );
+}
+
+export function BrushToolOptions() {
+ // On desktop the colors sit in the tool rail; the dock has no rail, so they follow the Color chip.
+ const colors = !useDesktopLayout() && <PaintColors />;
+ return <BrushOptions colors={colors} />;
+}
diff --git a/src/app/editor/export/export-image.ts b/src/app/editor/export/export-image.ts
index 6e0955f..0e61a06 100644
--- a/src/app/editor/export/export-image.ts
+++ b/src/app/editor/export/export-image.ts
@@ -1,6 +1,6 @@
import type { Gpu, Target } from "vgpu";
import { createEditorRenderer } from "@/app/editor/renderer";
-import type { EditorDocument } from "@/core/document";
+import { type EditorDocument, settledPixels } from "@/core/document";
import type { Point } from "@/core/image/frame";
import { renderBitmap } from "@/core/renderer";
@@ -82,7 +82,17 @@ export async function exportImage(
) {
const scene = document.scene.getState();
const source = document.resources.get(scene.layers[0].source);
- const renderer = createEditorRenderer(gpu, source);
+ // Taken with the scene: another photo may close the document before the renderer loads them.
+ const pixels = settledPixels(document, scene);
+ const renderer = createEditorRenderer(gpu, source, {
+ paintPixels: (id) => {
+ const blob = pixels.get(id);
+ if (!blob) {
+ throw Error("Settled paint is unavailable.");
+ }
+ return blob;
+ },
+ });
try {
await renderer.update(scene);
// The renderer retains the source until encoding finishes.
diff --git a/src/app/editor/histogram.tsx b/src/app/editor/histogram.tsx
index 8630233..67ae270 100644
--- a/src/app/editor/histogram.tsx
+++ b/src/app/editor/histogram.tsx
@@ -9,9 +9,9 @@ const placement = cva("group shrink-0", {
variants: {
placement: {
panel: "relative bg-level-2",
- // Anchored like the zoom control, at the canvas's other bottom corner.
+ // In the canvas's top right corner, beside the layer's options, leaving the bottom to the zoom control and hints.
canvas:
- "absolute bottom-3 left-3 z-10 h-14 w-44 overflow-hidden rounded-md bg-level-4/80 backdrop-blur-sm",
+ "absolute top-3 right-3 z-10 h-14 w-44 overflow-hidden rounded-md bg-level-4/80 backdrop-blur-sm",
},
},
});
diff --git a/src/app/editor/index.tsx b/src/app/editor/index.tsx
index 9afc650..8d5049d 100644
--- a/src/app/editor/index.tsx
+++ b/src/app/editor/index.tsx
@@ -2,7 +2,7 @@ import { Button } from "@roprgm/ui/button";
import { Tooltip, TooltipContent, TooltipTrigger } from "@roprgm/ui/tooltip";
import type { ReactNode } from "react";
import type { Workspace } from "@/app/workspace";
-import { BrushProvider } from "@/components/editor/brush-tool";
+import { BrushProvider, useBrushTool } from "@/components/editor/brush-tool";
import { Image } from "@/components/editor/image";
import {
EditorFrame,
@@ -22,8 +22,10 @@ import { HealingProvider } from "@/features/heal/mode";
import { addLayer } from "@/features/layers/edits";
import { MaskToolProvider, type Nesting } from "@/features/layers/mask-tool";
import { CanvasToolbar } from "@/features/layers/toolbar";
+import { PaintSettling } from "@/features/paint/settle";
import { useShortcuts } from "@/hooks/use-shortcuts";
import { blurActive } from "@/lib/dom";
+import { BrushKeys } from "./brush-keys";
import { ComparisonControl } from "./comparison-control";
import { ComparisonDivider } from "./comparison-divider";
import { DockPanel, DockProvider, DockTabs } from "./dock";
@@ -97,8 +99,9 @@ function ExportButton() {
<TooltipTrigger
render={
<Button
+ size="sm"
aria-pressed={exporting}
- className="ml-1 px-4 aria-pressed:bg-raised-hover pointer-coarse:h-10"
+ className="ml-1 aria-pressed:bg-raised-hover pointer-coarse:h-8"
onClick={() => setTool(exporting ? tools[0] : exportTool)}
>
Export
@@ -116,6 +119,7 @@ function ExportButton() {
function MaskTools({ children }: { children: ReactNode }) {
const document = useDocument();
const { setTool } = useTool();
+ const brush = useBrushTool();
// A mask from the rail goes on top of the stack; one chosen from a mask's Add or Subtract menu goes inside it.
function addMask(mask: Mask, nesting: Nesting) {
const layer = createMask(mask, nesting.operation);
@@ -129,6 +133,9 @@ function MaskTools({ children }: { children: ReactNode }) {
<MaskToolProvider
onCreate={addMask}
onTool={(shape) => {
+ if (shape === "brush") {
+ brush.update({ mode: "mask" });
+ }
const tool = tools.find((entry) => entry.id === shape);
if (tool) {
setTool(tool);
@@ -171,10 +178,10 @@ function DocumentEditor({
onDismissFailure: () => void;
}) {
return (
- <ToolProvider>
- <MaskTools>
- <HealingTools>
- <BrushProvider>
+ <BrushProvider>
+ <ToolProvider>
+ <MaskTools>
+ <HealingTools>
<EditorHeader
file={state.file}
open={<OpenButton onOpen={onOpen} />}
@@ -199,10 +206,12 @@ function DocumentEditor({
failure={state.failure}
onDismiss={onDismissFailure}
/>
- </BrushProvider>
- </HealingTools>
- </MaskTools>
- </ToolProvider>
+ <PaintSettling />
+ <BrushKeys />
+ </HealingTools>
+ </MaskTools>
+ </ToolProvider>
+ </BrushProvider>
);
}
diff --git a/src/app/editor/layers.ts b/src/app/editor/layers.ts
index 1ba759a..339126b 100644
--- a/src/app/editor/layers.ts
+++ b/src/app/editor/layers.ts
@@ -1,3 +1,4 @@
+import { BrushIcon } from "@/components/icons/brush";
import { ColorMixerIcon } from "@/components/icons/color-mixer";
import { DetailsIcon } from "@/components/icons/details";
import { ExposureIcon } from "@/components/icons/exposure";
@@ -9,6 +10,7 @@ import {
type EditorDocument,
type EffectLayer,
findLayer,
+ hasPaint,
type ImageLayer,
type Layer,
type LookupTable,
@@ -63,6 +65,7 @@ export const effectKinds = [
{ kind: "fill", label: "Color", addable: true },
{ kind: "lut", label: "LUT", Icon: LutIcon, addable: true },
{ kind: "heal", label: "Healing", Icon: HealIcon, addable: false },
+ { kind: "paint", label: "Paint", Icon: BrushIcon, addable: false },
] as const satisfies readonly EffectKind[];
/** Effects that start from defaults; a LUT layer needs its table first. */
@@ -89,6 +92,8 @@ export function createLayer(kind: DefaultEffect): EffectLayer {
return { ...base, kind, fill: { ...defaultFill } };
case "heal":
return { ...base, kind, patches: [] };
+ case "paint":
+ return { ...base, kind, blend: "normal", strokes: [] };
}
}
@@ -150,6 +155,8 @@ function effectNeutral(layer: ProcessingLayer): boolean {
return false;
case "heal":
return layer.patches.length === 0;
+ case "paint":
+ return !hasPaint(layer);
case "mask":
return true;
}
diff --git a/src/app/editor/renderer.ts b/src/app/editor/renderer.ts
index fcb6640..5a0ab9a 100644
--- a/src/app/editor/renderer.ts
+++ b/src/app/editor/renderer.ts
@@ -19,6 +19,7 @@ import { fill } from "@/features/fill/pass";
import { grain } from "@/features/grain/pass";
import { heal } from "@/features/heal/pass";
import { lut } from "@/features/lut/pass";
+import { paint } from "@/features/paint/pass";
import { toneCurves } from "@/features/tone-curves/pass";
import { vignette } from "@/features/vignette/pass";
@@ -89,6 +90,14 @@ function composeLayer(
case "lut":
edited = pipeline(below, [lut(layer.lut, `${name}/lut`)]);
break;
+ case "paint":
+ edited = paint(
+ below,
+ composition.paint(layer),
+ layer.blend,
+ `${name}/paint`,
+ );
+ break;
case "heal": {
const result = heal(below, layer.patches, name, composition);
edited = result.image;
@@ -131,13 +140,16 @@ function composeLayers(
/**
* Pure layer composition shares the same graph for preview, crop, and export.
- * Layers process the source; the image layer's adjustments and curve then tone the composite,
- * so a local exposure still sees the light a global exposure would push past white.
+ * The image layer develops the photo first; layers above it process that floating-point result.
+ * Display and export encode only after composition, so later layers can recover HDR headroom.
*/
export function createEditorRenderer(
gpu: Gpu,
source: ImageSource,
- timer?: Timer,
+ {
+ timer,
+ paintPixels,
+ }: { timer?: Timer; paintPixels?: (id: string) => Blob } = {},
) {
return createRenderer(
gpu,
@@ -146,14 +158,19 @@ export function createEditorRenderer(
const [sourceLayer, ...layers] = scene.layers;
const name = `layer/${sourceLayer.id}`;
composition.retain(name);
- const children = composeLayers(image, sourceLayer.children, composition);
- const composite = composeLayers(children.image, layers, composition);
- const adjusted = pipeline(composite.image, [
+ const adjusted = pipeline(image, [
adjustments(sourceLayer.adjustments, name),
]);
- const full = pipeline(adjusted, [
+ const developed = pipeline(adjusted, [
toneCurves(sourceLayer.toneCurve, `${name}/curves`),
]);
+ const children = composeLayers(
+ developed,
+ sourceLayer.children,
+ composition,
+ );
+ const composite = composeLayers(children.image, layers, composition);
+ const full = composite.image;
const [original, output] = transformImages(
[input(source.image), full],
scene.frame,
@@ -169,5 +186,6 @@ export function createEditorRenderer(
};
},
timer,
+ paintPixels,
);
}
diff --git a/src/app/editor/tool-rail.tsx b/src/app/editor/tool-rail.tsx
index 7039676..6bcb887 100644
--- a/src/app/editor/tool-rail.tsx
+++ b/src/app/editor/tool-rail.tsx
@@ -3,6 +3,7 @@ import { Tooltip, TooltipContent, TooltipTrigger } from "@roprgm/ui/tooltip";
import type { ReactNode } from "react";
import { GithubIcon } from "@/components/icons/github";
import { InfoIcon } from "@/components/icons/info";
+import { PaintColors } from "@/features/paint/colors";
import { type Tool, tools, useTool } from "./tools";
/** Opens in a new tab, leaving the photo open. */
@@ -35,13 +36,18 @@ function RailLink({
);
}
-/** Icon tabs in a column at the window's left edge; tooltips carry the label and shortcut. Without a handler the tabs are inert. */
+/**
+ * Icon tabs in a column at the window's left edge, then `children`; tooltips carry the label and
+ * shortcut. Without a handler the tabs are inert.
+ */
export function ToolTabList({
selected,
onSelect,
+ children,
}: {
selected: Tool;
onSelect?: (tool: Tool) => void;
+ children?: ReactNode;
}) {
const editing = tools.filter((entry) => entry.group === "edit");
return (
@@ -71,6 +77,7 @@ export function ToolTabList({
</Tooltip>
))}
</TabList>
+ {children}
<div className="mt-auto flex flex-col gap-1">
<RailLink href="/about" label="About OpenLight">
<InfoIcon className="size-5" />
@@ -86,7 +93,12 @@ export function ToolTabList({
);
}
+/** The tools, then the paint colors, as Photoshop keeps them. */
export function ToolRail() {
const { tool, setTool } = useTool();
- return <ToolTabList selected={tool} onSelect={setTool} />;
+ return (
+ <ToolTabList selected={tool} onSelect={setTool}>
+ <PaintColors vertical />
+ </ToolTabList>
+ );
}
diff --git a/src/app/editor/tools.tsx b/src/app/editor/tools.tsx
index bfa22f3..5cd1c88 100644
--- a/src/app/editor/tools.tsx
+++ b/src/app/editor/tools.tsx
@@ -6,6 +6,7 @@ import {
useContext,
useState,
} from "react";
+import { useBrushTool } from "@/components/editor/brush-tool";
import { useDocument } from "@/components/editor/session";
import { AdjustIcon } from "@/components/icons/adjust";
import { BrushIcon } from "@/components/icons/brush";
@@ -17,11 +18,10 @@ import { RadialGradientIcon } from "@/components/icons/radial-gradient";
import { CropEditor } from "@/features/crop/view";
import { HealOptions } from "@/features/heal/options";
import { HealOverlay } from "@/features/heal/overlay";
-import { BrushOptions } from "@/features/layers/brush-options";
-import { BrushOverlay } from "@/features/layers/brush-overlay";
import { addLayer } from "@/features/layers/edits";
import { GradientOverlay } from "@/features/layers/gradient-overlay";
import { useShortcuts } from "@/hooks/use-shortcuts";
+import { BrushToolCanvas, BrushToolOptions } from "./brush";
import { ExportMode } from "./export";
import { createLayer } from "./layers";
@@ -92,8 +92,8 @@ export const tools = [
key: "b",
Icon: BrushIcon,
group: "edit",
- Canvas: BrushOverlay,
- Options: BrushOptions,
+ Canvas: BrushToolCanvas,
+ Options: BrushToolOptions,
},
{
id: "linear",
@@ -137,12 +137,25 @@ export const tools = [
export type Tool = (typeof tools)[number];
-/** Holds the active tool, and each tool's key selects it in either layout. */
+/**
+ * Holds the active tool, and each tool's key selects it in either layout. The Brush's key, pressed on
+ * the Brush, switches it between color and mask.
+ */
export function ToolProvider({ children }: { children: ReactNode }) {
const [tool, setTool] = useState<Tool>(adjust);
+ const brush = useBrushTool();
+ function select(entry: Tool) {
+ if (entry === tool && entry.id === "brush") {
+ brush.update({
+ mode: brush.settings.mode === "color" ? "mask" : "color",
+ });
+ return;
+ }
+ setTool(entry);
+ }
useShortcuts(
Object.fromEntries(
- tools.map((entry) => [entry.key, () => setTool(entry)] as const),
+ tools.map((entry) => [entry.key, () => select(entry)] as const),
),
);
return <ToolContext value={{ tool, setTool }}>{children}</ToolContext>;
diff --git a/src/app/loaders/scene.ts b/src/app/loaders/scene.ts
index d35e280..c1b0975 100644
--- a/src/app/loaders/scene.ts
+++ b/src/app/loaders/scene.ts
@@ -15,9 +15,12 @@ export function isSceneFile(file: File) {
return file.name.toLowerCase().endsWith(sceneExtension);
}
-/** The document as a ZIP archive: a deflated `scene.json` and each source's bytes at `sources/<id>`. */
+/**
+ * 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>`.
+ */
export async function writeSceneFile(document: EditorDocument) {
- const { json, files } = snapshotScene(document);
+ const { json, sources, paint } = snapshotScene(document);
const { writeZip } = await import("@/lib/zip");
const archive = await writeZip([
{
@@ -25,9 +28,10 @@ export async function writeSceneFile(document: EditorDocument) {
data: new Blob([JSON.stringify(json)]),
deflate: true,
},
- ...[...files].map(([id, file]) => ({ name: `sources/${id}`, data: file })),
+ ...[...sources].map(([id, data]) => ({ name: `sources/${id}`, data })),
+ ...[...paint].map(([id, data]) => ({ name: `paint/${id}`, data })),
]);
- const [file] = files.values();
+ const [file] = sources.values();
const name = file.name.replace(/\.[^.]*$/, "") || "scene";
return new File([archive], `${name}${sceneExtension}`);
}
@@ -43,9 +47,12 @@ export async function openSceneFile(
throw Error("This file doesn't contain an OpenLight scene.");
}
const files = new Map(
- [...entries].flatMap(([name, data]) =>
- name.startsWith("sources/") ? [[name.slice(8), data] as const] : [],
- ),
+ [...entries].flatMap(([name, data]) => {
+ const [folder, id] = name.split("/");
+ return (folder === "sources" || folder === "paint") && id
+ ? [[id, data] as const]
+ : [];
+ }),
);
return openScene(JSON.parse(await json.text()), files, decode);
}
diff --git a/src/app/scene-file.ts b/src/app/scene-file.ts
index d0484fd..80f5e4a 100644
--- a/src/app/scene-file.ts
+++ b/src/app/scene-file.ts
@@ -4,7 +4,9 @@ import {
createResources,
type EditorDocument,
type ProcessingLayer,
+ paintingOf,
type Scene,
+ settledPixels,
walkLayers,
} from "@/core/document";
import type { ImageSource } from "@/core/image";
@@ -19,12 +21,14 @@ import { defaultDetails, detailsSchema } from "@/features/details/model";
import { defaultFill, fillSchema } from "@/features/fill/model";
import { defaultGrain, grainSchema } from "@/features/grain/model";
import { healPatchSchema } from "@/features/heal/model";
+import { validateBrushLayers } from "@/features/layers/edits";
import {
layerSettings,
maskOperation,
maskSchema,
} from "@/features/layers/model";
import { lutSchema } from "@/features/lut/model";
+import { paintShape } from "@/features/paint/model";
import { curveSchema } from "@/features/tone-curves/curve";
import { defaultVignette, vignetteSchema } from "@/features/vignette/model";
import { whiteBalanceSchema } from "@/features/white-balance/edits";
@@ -41,7 +45,7 @@ export type SceneJson = {
scene: Scene;
};
-/** The document's scene and the source files it references, read without rendering. */
+/** The document's scene, the source file it references, and the pixels its paint settled into, read without rendering. */
export function snapshotScene(document: EditorDocument) {
const scene = document.scene.getState();
const { source } = scene.layers[0];
@@ -52,7 +56,11 @@ export function snapshotScene(document: EditorDocument) {
sources: { [source]: { name: file.name, type: file.type } },
scene,
};
- return { json, files: new Map([[source, file]]) };
+ return {
+ json,
+ sources: new Map([[source, file]]),
+ paint: settledPixels(document, scene),
+ };
}
const id = z.string().check(z.minLength(1));
@@ -100,6 +108,7 @@ function processingLayer(children: z.ZodMiniType<readonly ProcessingLayer[]>) {
kind: z.literal("heal"),
patches: z.array(healPatchSchema),
}),
+ z.object({ ...base, kind: z.literal("paint"), ...paintShape }),
z.object({
...base,
kind: z.literal("mask"),
@@ -177,7 +186,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 by ID.
+ * Scene files and drafts both open through here; `files` holds each source's bytes, and the pixels
+ * each paint layer settled into, by ID.
*/
export async function openScene(
saved: unknown,
@@ -188,6 +198,7 @@ export async function openScene(
throw Error("This scene needs a newer version of OpenLight.");
}
const { sources, scene } = parse(savedSchema, saved, "Invalid scene");
+ validateBrushLayers(scene.layers);
const [image, ...layers] = scene.layers;
const source = sources[image.source];
const data = files.get(image.source);
@@ -209,6 +220,16 @@ export async function openScene(
const resources = createResources();
try {
resources.add(sourceFile, decoded, image.source);
+ for (const { layer } of walkLayers(layers)) {
+ const raster = paintingOf(layer)?.raster;
+ if (raster) {
+ const pixels = files.get(raster);
+ if (!pixels) {
+ throw Error("The scene's paint is missing.");
+ }
+ resources.addPaint(pixels, raster);
+ }
+ }
return createDocument(
{
frame: scene.frame,
diff --git a/src/components/editor/brush-canvas.tsx b/src/components/editor/brush-canvas.tsx
index 3c82fb2..16589cf 100644
--- a/src/components/editor/brush-canvas.tsx
+++ b/src/components/editor/brush-canvas.tsx
@@ -1,3 +1,6 @@
+import { Popover, PopoverContent, PopoverTrigger } from "@roprgm/ui/popover";
+import { Section } from "@roprgm/ui/section";
+import { Slider } from "@roprgm/ui/slider";
import {
type PointerEvent,
type ReactNode,
@@ -10,7 +13,7 @@ import type { BrushStroke, StrokePoint } from "@/core/document";
import type { Point } from "@/core/image/frame";
import { useShortcuts } from "@/hooks/use-shortcuts";
import { blurActive } from "@/lib/dom";
-import { useBrushTool } from "./brush-tool";
+import { useBrushParameters, useBrushTool } from "./brush-tool";
import { CanvasHint } from "./canvas-hint";
import { useDocumentMapping } from "./mapping";
import { useDocument } from "./session";
@@ -68,6 +71,8 @@ export function BrushCanvas({
const stroke = useRef<Stroke | null>(null);
const completing = useRef<AbortController | null>(null);
const [pointer, setPointer] = useState<Point | null>(null);
+ /** Where a right click opened the brush menu. */
+ const [menu, setMenu] = useState<Point | null>(null);
const [pointerVisible, setPointerVisible] = useState(false);
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string>();
@@ -168,7 +173,13 @@ export function BrushCanvas({
},
"[": () => brush.update({ size: Math.round(brush.settings.size / 1.25) }),
"]": () => brush.update({ size: Math.round(brush.settings.size * 1.25) }),
+ "shift+[": () => featherBy(-0.1),
+ "shift+]": () => featherBy(0.1),
});
+ function featherBy(step: number) {
+ const feather = Math.round((brush.settings.feather + step) * 10) / 10;
+ brush.update({ feather: Math.min(1, Math.max(0, feather)) });
+ }
function start(event: PointerEvent<HTMLDivElement>) {
const current = stroke.current;
if (current?.touch && event.pointerType === "touch" && !event.isPrimary) {
@@ -260,6 +271,11 @@ export function BrushCanvas({
className="absolute inset-0 cursor-none touch-none data-[pan=true]:pointer-events-none"
data-pan={camera.panMode}
onDoubleClick={(event) => event.stopPropagation()}
+ onContextMenu={(event) => {
+ event.preventDefault();
+ const bounds = event.currentTarget.getBoundingClientRect();
+ setMenu([event.clientX - bounds.left, event.clientY - bounds.top]);
+ }}
onPointerDown={start}
onPointerMove={move}
onPointerUp={end}
@@ -314,6 +330,33 @@ export function BrushCanvas({
</svg>
)}
{status && <CanvasHint>{status}</CanvasHint>}
+ {menu && <BrushMenu at={menu} onClose={() => setMenu(null)} />}
</div>
);
}
+
+/** The brush's size and feather where a right click opened them, as Photoshop's brush menu. */
+function BrushMenu({ at, onClose }: { at: Point; onClose: () => void }) {
+ const [size, feather] = useBrushParameters();
+ return (
+ <Popover open onOpenChange={(open) => !open && onClose()}>
+ <PopoverTrigger
+ nativeButton={false}
+ render={
+ <span
+ aria-hidden
+ className="pointer-events-none absolute size-0"
+ style={{ left: at[0], top: at[1] }}
+ />
+ }
+ />
+ <PopoverContent align="start" className="w-60">
+ <Section>
+ {[size, feather].map(({ id, ...parameter }) => (
+ <Slider key={id} {...parameter} variant="panel" />
+ ))}
+ </Section>
+ </PopoverContent>
+ </Popover>
+ );
+}
diff --git a/src/components/editor/brush-tool.tsx b/src/components/editor/brush-tool.tsx
index 5791fd4..49cb228 100644
--- a/src/components/editor/brush-tool.tsx
+++ b/src/components/editor/brush-tool.tsx
@@ -5,9 +5,14 @@ import {
useEffect,
useState,
} from "react";
+import { findLayer, type Layer } from "@/core/document";
import { clamp } from "@/lib/math";
+import type { Parameter } from "./parameter";
import { useDocument, useScene } from "./session";
+/** What the brush paints: color on a paint layer, or coverage on a brush mask. */
+export type BrushMode = "color" | "mask";
+
export type BrushSettings = {
/** Diameter in source pixels. */
size: number;
@@ -15,8 +20,25 @@ export type BrushSettings = {
feather: number;
flow: number;
erase: boolean;
+ mode: BrushMode;
+ /** The primary color, which strokes paint, and the secondary, as `#rrggbb`. */
+ colors: readonly [string, string];
};
+/** Black over white, as Photoshop starts. */
+export const defaultColors = ["#000000", "#ffffff"] as const;
+
+/** The mode that paints on `layer`, if the brush can paint on it. */
+export function brushMode(layer: Layer | undefined): BrushMode | undefined {
+ if (layer?.kind === "paint") {
+ return "color";
+ }
+ if (layer?.kind === "mask" && layer.mask.kind === "brush") {
+ return "mask";
+ }
+ return undefined;
+}
+
const BrushTool = createContext<{
settings: BrushSettings;
/** The mode the next stroke uses: the setting, inverted while Alt is held. */
@@ -37,7 +59,10 @@ export function useBrushTool() {
return tool;
}
-/** Brush settings outlive strokes and tool switches; they start relative to the image size. */
+/**
+ * Brush settings outlive strokes and tool switches; they start relative to the image size. The mode
+ * follows the selection onto a paint layer or brush mask, so the brush paints what is selected.
+ */
export function BrushProvider({ children }: { children: ReactNode }) {
const document = useDocument();
const sourceId = useScene((scene) => scene.layers[0].source);
@@ -54,9 +79,23 @@ export function BrushProvider({ children }: { children: ReactNode }) {
feather: 0.5,
flow: 1,
erase: false,
+ mode: "mask",
+ colors: defaultColors,
});
const [alt, setAlt] = useState(false);
const [preview, setPreview] = useState(false);
+ useEffect(
+ () =>
+ document.selection.subscribe(({ layerId }) => {
+ const mode = brushMode(
+ findLayer(document.scene.getState().layers, layerId),
+ );
+ if (mode) {
+ setSettings((settings) => ({ ...settings, mode }));
+ }
+ }),
+ [document],
+ );
useEffect(() => {
const controller = new AbortController();
const { signal } = controller;
@@ -88,3 +127,53 @@ export function BrushProvider({ children }: { children: ReactNode }) {
</BrushTool>
);
}
+
+/** How the Flow slider curves: flow is its share to this power, so 50% lays a quarter and low values stay light. */
+const flowCurve = 2;
+
+export function flowAt(share: number) {
+ return share ** flowCurve;
+}
+
+/** The next stroke's size, feather, and flow, for sliders and dials wherever they show. */
+export function useBrushParameters(): [Parameter, Parameter, Parameter] {
+ const { settings, maxSize, setPreview, update } = useBrushTool();
+ return [
+ {
+ id: "size",
+ label: "Size",
+ value: settings.size,
+ min: 1,
+ max: maxSize,
+ format: (value) => `${value}px`,
+ valueWidth: `${maxSize}`.length,
+ onEditingChange: setPreview,
+ onChange: (size) => update({ size: Math.round(size) }),
+ },
+ {
+ id: "feather",
+ label: "Feather",
+ value: Math.round(settings.feather * 100),
+ min: 0,
+ max: 100,
+ defaultValue: 50,
+ origin: 0,
+ format: (value) => `${value}%`,
+ valueWidth: 3,
+ onEditingChange: setPreview,
+ onChange: (value) => update({ feather: value / 100 }),
+ },
+ {
+ id: "flow",
+ label: "Flow",
+ value: Math.round(settings.flow ** (1 / flowCurve) * 100),
+ min: 1,
+ max: 100,
+ defaultValue: 100,
+ origin: 0,
+ format: (value) => `${value}%`,
+ valueWidth: 3,
+ onChange: (value) => update({ flow: flowAt(value / 100) }),
+ },
+ ];
+}
diff --git a/src/components/editor/canvas-hint.tsx b/src/components/editor/canvas-hint.tsx
index e6bfbe4..ddc78db 100644
--- a/src/components/editor/canvas-hint.tsx
+++ b/src/components/editor/canvas-hint.tsx
@@ -1,9 +1,9 @@
import type { ReactNode } from "react";
-/** A short instruction in the canvas corner that never takes the pointer. */
+/** A short instruction in the canvas corner that never takes the pointer, wrapping short of the zoom control. */
export function CanvasHint({ children }: { children: ReactNode }) {
return (
- <p className="pointer-events-none absolute bottom-3 left-3 rounded-full bg-level-4/80 px-3 py-1.5 text-foreground backdrop-blur-sm">
+ <p className="pointer-events-none absolute bottom-3 left-3 max-w-[calc(100%-9rem)] rounded-2xl bg-level-4/80 px-3 py-1.5 text-balance text-foreground backdrop-blur-sm">
{children}
</p>
);
diff --git a/src/components/editor/coverage-thumbnail.tsx b/src/components/editor/coverage-thumbnail.tsx
index 57ce0d9..50d88dd 100644
--- a/src/components/editor/coverage-thumbnail.tsx
+++ b/src/components/editor/coverage-thumbnail.tsx
@@ -6,8 +6,9 @@ import { useDocument } from "./session";
/**
* The renderer's coverage raster for one id, drawn into a small canvas after each committed change.
- * `version` is the scene content the raster follows; a change redraws once its gesture ends.
- * Without a raster, such as a mask that paints nothing yet, the fallback shows instead.
+ * `version` is the scene content the raster follows; a change redraws once its gesture ends. `region`
+ * is in source pixels, whatever part of the photo the raster covers. Without a raster, such as a mask
+ * that paints nothing yet, the fallback shows instead.
*/
export function CoverageThumbnail({
id,
@@ -47,8 +48,17 @@ export function CoverageThumbnail({
useEffect(() => {
const coverage = ready !== undefined && renderer.coverage(id);
if (!coverage) return;
+ const { target, origin } = coverage;
let active = true;
- renderCoverage(gpu, coverage, [64, 64], region)
+ renderCoverage(
+ gpu,
+ target,
+ [64, 64],
+ region && {
+ origin: [region.origin[0] - origin[0], region.origin[1] - origin[1]],
+ extent: region.extent,
+ },
+ )
.then((bitmap) => {
const context = canvas.current?.getContext("2d");
if (active && context) context.drawImage(bitmap, 0, 0);
diff --git a/src/components/editor/image.tsx b/src/components/editor/image.tsx
index 107d897..913d8dd 100644
--- a/src/components/editor/image.tsx
+++ b/src/components/editor/image.tsx
@@ -66,7 +66,7 @@ export function Image({
frame: sceneFrame,
sourceSize,
coverage: overlay.layerId
- ? renderer.coverage(overlay.layerId)
+ ? renderer.coverage(overlay.layerId)?.target
: undefined,
},
});
diff --git a/src/components/editor/pipeline.tsx b/src/components/editor/pipeline.tsx
index fe69fe0..06da406 100644
--- a/src/components/editor/pipeline.tsx
+++ b/src/components/editor/pipeline.tsx
@@ -39,6 +39,7 @@ type RendererProviderProps = {
createRenderer: (
gpu: Gpu,
source: ImageSource,
+ options: { paintPixels: (id: string) => Blob },
) => ReturnType<typeof createRenderer>;
};
@@ -51,8 +52,11 @@ export function RendererProvider({
const sourceId = useScene((scene) => scene.layers[0].source);
const source = document.resources.get(sourceId);
const renderer = useDisposable(
- () => createRenderer(gpu, source),
- [gpu, source, createRenderer],
+ () =>
+ createRenderer(gpu, source, {
+ paintPixels: (id) => document.resources.paint(id),
+ }),
+ [gpu, document, source, createRenderer],
);
const [error, setError] = useState<string>();
diff --git a/src/components/editor/tool-layer.ts b/src/components/editor/tool-layer.ts
index 444ac26..5fa3676 100644
--- a/src/components/editor/tool-layer.ts
+++ b/src/components/editor/tool-layer.ts
@@ -1,11 +1,12 @@
-import { useEffect } from "react";
-import { findLayer, type Layer } from "@/core/document";
+import { useEffect, useRef } from "react";
+import { findLayer, type Layer, type Scene } from "@/core/document";
import { useDocument } from "./session";
/**
- * Keeps a tool on a layer it accepts. Opening without one, or with `fresh`, creates a layer that leaves
- * with the tool unless anything happened after its creation; selecting any other layer leaves the tool.
- * Returns a lookup of the selected layer at call time, for pointer handlers that run before a render.
+ * Keeps a tool on a layer it accepts. Opening without one, or with `fresh`, creates a layer, when
+ * `create` is given, that leaves with the tool unless anything happened after its creation; selecting
+ * any other layer leaves the tool. Returns a lookup of the selected layer at call time, for pointer
+ * handlers that run before a render.
*/
export function useToolLayer<L extends Layer>({
accepts,
@@ -14,11 +15,13 @@ export function useToolLayer<L extends Layer>({
fresh = false,
}: {
accepts: (layer: Layer) => layer is L;
- create: () => void;
+ create?: () => void;
leave: () => void;
fresh?: boolean;
}) {
const document = useDocument();
+ const before = useRef<Scene | undefined>(undefined);
+ const canCreate = create !== undefined;
function selected() {
const layer = findLayer(
document.scene.getState().layers,
@@ -27,10 +30,12 @@ export function useToolLayer<L extends Layer>({
return layer && accepts(layer) ? layer : undefined;
}
useEffect(() => {
- const before = fresh || !selected() ? document.scene.getState() : undefined;
- if (before) {
+ if (create && (fresh || !selected())) {
+ before.current = document.scene.getState();
create();
}
+ }, [canCreate, fresh]);
+ useEffect(() => {
const unsubscribe = document.selection.subscribe(() => {
if (!selected()) {
leave();
@@ -38,8 +43,8 @@ export function useToolLayer<L extends Layer>({
});
return () => {
unsubscribe();
- if (before) {
- document.history.drop(before);
+ if (before.current) {
+ document.history.drop(before.current);
}
};
}, []);
diff --git a/src/components/icons/eyedropper.tsx b/src/components/icons/eyedropper.tsx
new file mode 100644
index 0000000..1b8b4a8
--- /dev/null
+++ b/src/components/icons/eyedropper.tsx
@@ -0,0 +1,9 @@
+import { Icon, type IconProps } from "./icon";
+
+export function EyedropperIcon(props: IconProps) {
+ return (
+ <Icon viewBox="0 0 20 20" {...props}>
+ <path d="m11 6 3 3M9.5 7.5l-5 5a2 2 0 0 0-.5 1.5v2h2a2 2 0 0 0 1.5-.5l5-5M10 5l1.5-1.5a2.1 2.1 0 0 1 3 0l2 2a2.1 2.1 0 0 1 0 3L15 10" />
+ </Icon>
+ );
+}
diff --git a/src/components/icons/swap.tsx b/src/components/icons/swap.tsx
new file mode 100644
index 0000000..cc5377c
--- /dev/null
+++ b/src/components/icons/swap.tsx
@@ -0,0 +1,9 @@
+import { Icon, type IconProps } from "./icon";
+
+export function SwapIcon(props: IconProps) {
+ return (
+ <Icon viewBox="0 0 20 20" {...props}>
+ <path d="M5 11V9a3 3 0 0 1 3-3h7m-2-2 2 2-2 2M15 9v2a3 3 0 0 1-3 3H5m2 2-2-2 2-2" />
+ </Icon>
+ );
+}
diff --git a/src/core/document/brush.ts b/src/core/document/brush.ts
index af82563..3fedc87 100644
--- a/src/core/document/brush.ts
+++ b/src/core/document/brush.ts
@@ -1,5 +1,5 @@
import { z } from "zod/mini";
-import { unit } from "@/lib/parse";
+import { hexColor, unit } from "@/lib/parse";
/** Finite source-pixel coordinates and a pressure from 0 to 1. */
export const strokePoints = z
@@ -13,3 +13,5 @@ export const strokeSchema = z.object({
flow: unit,
points: strokePoints,
});
+
+export const paintStrokeSchema = z.extend(strokeSchema, { color: hexColor });
diff --git a/src/core/document/history.ts b/src/core/document/history.ts
index 39d2a61..f33885c 100644
--- a/src/core/document/history.ts
+++ b/src/core/document/history.ts
@@ -82,6 +82,11 @@ export function createHistory<T extends object>(
}
publish();
},
+ /** Changes the current state in place, as a change that shows nothing new; undo and redo stay as they were. */
+ replace(next: T) {
+ state.setState(next, true);
+ publish();
+ },
/** Returns whether this call opened the group; nested callers leave it to the opener. */
begin() {
const opened = group === undefined;
diff --git a/src/core/document/index.ts b/src/core/document/index.ts
index 9c8075e..84e7599 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 } from "./tree";
+import { findLayer, paintingOf, walkLayers } from "./tree";
export type {
Adjustments,
@@ -25,6 +25,9 @@ export type {
Mask,
MaskLayer,
MaskModifier,
+ Painting,
+ PaintLayer,
+ PaintStroke,
ProcessingLayer,
RadialGradient,
Scene,
@@ -36,8 +39,10 @@ export {
adjustmentTarget,
editLayer,
findLayer,
+ hasPaint,
locateLayer,
maskModifiers,
+ paintingOf,
updateLayer,
walkLayers,
} from "./tree";
@@ -83,18 +88,43 @@ function equal(a: unknown, b: unknown): boolean {
);
}
+/** The resources a scene names: its image source and the pixels its paintings settled into. */
+function resourceIds(scene: Scene) {
+ const ids = [scene.layers[0].source];
+ for (const { layer } of walkLayers(scene.layers)) {
+ const raster = paintingOf(layer)?.raster;
+ if (raster) {
+ ids.push(raster);
+ }
+ }
+ return ids;
+}
+
+/** The settled pixels `scene` names, taken now, so work that may outlive the document still has them. */
+export function settledPixels(
+ document: EditorDocument,
+ scene = document.scene.getState(),
+) {
+ const pixels = new Map<string, Blob>();
+ for (const { layer } of walkLayers(scene.layers)) {
+ const raster = paintingOf(layer)?.raster;
+ if (raster) {
+ pixels.set(raster, document.resources.paint(raster));
+ }
+ }
+ return pixels;
+}
+
/** One independent editing session. No React, decoders, or file workflows. */
export function createDocument(initial: Scene, resources = createResources()) {
const scene = createStore(() => initial);
const selection = createStore(() => ({ layerId: initial.layers[0].id }));
- const { update, ...history } = createHistory(
+ const { update, replace, ...history } = createHistory(
scene,
equal,
100,
(retained) => {
- resources.retain(
- new Set(retained.map((state) => state.layers[0].source)),
- );
+ resources.retain(new Set(retained.flatMap(resourceIds)));
},
);
const unsubscribe = scene.subscribe((state) => {
@@ -137,6 +167,16 @@ export function createDocument(initial: Scene, resources = createResources()) {
validateFrame(next.frame);
update(next);
},
+ /**
+ * Replaces the current scene in place, for a change that shows nothing new, such as paint strokes
+ * settling into pixels; history stays as it was.
+ */
+ replace(next: Scene) {
+ if (closed) {
+ throw new Error("Document is closed.");
+ }
+ replace(next);
+ },
/** 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.ts
index 1dcb505..2b72dfd 100644
--- a/src/core/document/resources.ts
+++ b/src/core/document/resources.ts
@@ -1,8 +1,12 @@
import type { ImageSource } from "@/core/image";
-/** Owns the document's image files and GPU targets, outside scene history. */
+/**
+ * Owns the document's image files and GPU targets, and the deflated pixels paint layers settled into,
+ * outside scene history.
+ */
export function createResources() {
const images = new Map<string, { file: File } & ImageSource>();
+ const paints = new Map<string, Blob>();
let disposed = false;
return {
/** A saved document restores each source under its original ID. */
@@ -23,6 +27,22 @@ export function createResources() {
}
return resource;
},
+ /** 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.");
+ }
+ paints.set(id, pixels);
+ return id;
+ },
+ paint(id: string) {
+ const pixels = paints.get(id);
+ if (!pixels) {
+ throw new Error("Settled paint is unavailable.");
+ }
+ return pixels;
+ },
+ /** Frees what no retained scene names. */
retain(ids: ReadonlySet<string>) {
for (const [id, source] of images) {
if (!ids.has(id)) {
@@ -30,6 +50,11 @@ export function createResources() {
images.delete(id);
}
}
+ for (const id of paints.keys()) {
+ if (!ids.has(id)) {
+ paints.delete(id);
+ }
+ }
},
dispose() {
disposed = true;
@@ -37,6 +62,7 @@ export function createResources() {
source.dispose();
}
images.clear();
+ paints.clear();
},
};
}
diff --git a/src/core/document/scene.ts b/src/core/document/scene.ts
index 7ed0273..bdd1694 100644
--- a/src/core/document/scene.ts
+++ b/src/core/document/scene.ts
@@ -103,11 +103,17 @@ export type BrushStroke = {
readonly flow: number;
readonly points: readonly StrokePoint[];
};
-/** Coverage painted with strokes; the renderer rasterizes them into a cached texture. */
-export type BrushMask = {
- readonly kind: "brush";
- readonly strokes: readonly BrushStroke[];
+/** A stroke that paints a color, `#rrggbb` sRGB; erasing removes paint whatever its color. */
+export type PaintStroke = BrushStroke & { readonly color: string };
+/** What strokes painted, a paint layer's color or a brush mask's coverage. */
+export type Painting<S extends BrushStroke = BrushStroke> = {
+ /** The document resource of pixels earlier strokes settled into, which `strokes` draw over. */
+ readonly raster?: string;
+ /** The latest strokes, which undo takes back by drawing the rest over `raster` again. */
+ readonly strokes: readonly S[];
};
+/** Coverage painted with strokes; the renderer rasterizes them into a cached texture. */
+export type BrushMask = { readonly kind: "brush" } & Painting;
/** One non-destructive repair: a painted shape filled from a donor at `offset` source pixels away. */
export type HealPatch = {
readonly id: string;
@@ -133,6 +139,7 @@ export type ProcessingLayer = {
| { readonly kind: "fill"; readonly fill: Fill }
| { readonly kind: "lut"; readonly lut: LookupTable }
| { readonly kind: "heal"; readonly patches: readonly HealPatch[] }
+ | ({ readonly kind: "paint"; readonly blend: Blend } & Painting<PaintStroke>)
| {
readonly kind: "mask";
readonly operation: "add" | "subtract";
@@ -142,6 +149,7 @@ export type ProcessingLayer = {
}
);
export type MaskLayer = Extract<ProcessingLayer, { kind: "mask" }>;
+export type PaintLayer = Extract<ProcessingLayer, { kind: "paint" }>;
/** A child mask that adds to or subtracts from its parent's coverage. */
export type MaskModifier = Pick<
MaskLayer,
diff --git a/src/core/document/tree.ts b/src/core/document/tree.ts
index 90c69d5..3576367 100644
--- a/src/core/document/tree.ts
+++ b/src/core/document/tree.ts
@@ -1,5 +1,11 @@
import type { EditorDocument } from "./index";
-import type { Layer, MaskLayer, ProcessingLayer, Scene } from "./scene";
+import type {
+ Layer,
+ MaskLayer,
+ Painting,
+ ProcessingLayer,
+ Scene,
+} from "./scene";
type LayerLocation = {
layer: Layer;
@@ -49,6 +55,22 @@ export function maskModifiers(layer: MaskLayer) {
);
}
+/** Whether a painting holds anything: settled pixels or strokes over them. */
+export function hasPaint(painting: Painting) {
+ return painting.raster !== undefined || painting.strokes.length > 0;
+}
+
+/** What a paint layer or brush mask painted; other layers paint nothing. */
+export function paintingOf(layer: Layer): Painting | undefined {
+ if (layer.kind === "paint") {
+ return layer;
+ }
+ if (layer.kind === "mask" && layer.mask.kind === "brush") {
+ return layer.mask;
+ }
+ return undefined;
+}
+
function withChildren<L extends Layer>(
layer: L,
children: readonly ProcessingLayer[],
diff --git a/src/core/image/blend.ts b/src/core/image/blend.ts
new file mode 100644
index 0000000..f0bc42c
--- /dev/null
+++ b/src/core/image/blend.ts
@@ -0,0 +1,26 @@
+import { z } from "zod/mini";
+import type { Blend } from "@/core/document";
+
+/** Photoshop's blend modes in menu order; `blend.wgsl` numbers them by this index. */
+export const blends: readonly (readonly [Blend, string])[] = [
+ ["normal", "Normal"],
+ ["multiply", "Multiply"],
+ ["screen", "Screen"],
+ ["overlay", "Overlay"],
+ ["soft-light", "Soft Light"],
+ ["color", "Color"],
+ ["luminosity", "Luminosity"],
+];
+
+export const blendSchema = z.enum(blends.map(([blend]) => blend));
+
+export function blendIndex(blend: Blend) {
+ return blends.findIndex(([value]) => value === blend);
+}
+
+/** The sRGB channels of a `#rrggbb` color, 0 to 1. */
+export function parseColor(color: string): [number, number, number] {
+ const channel = (offset: number) =>
+ Number.parseInt(color.slice(offset, offset + 2), 16) / 255;
+ return [channel(1), channel(3), channel(5)];
+}
diff --git a/src/core/image/blend.wgsl b/src/core/image/blend.wgsl
new file mode 100644
index 0000000..92d4ad2
--- /dev/null
+++ b/src/core/image/blend.wgsl
@@ -0,0 +1,47 @@
+// Photoshop's blend formulas on encoded values, 0 to 1: callers take the sRGB transfer of both colors first,
+// so Multiply, Overlay and the rest look as they do in Photoshop. Modes follow `blends` in blend.ts.
+
+fn lum(color: vec3f) -> f32 {
+ return dot(color, vec3f(0.3, 0.59, 0.11));
+}
+
+fn clipColor(color: vec3f) -> vec3f {
+ let l = lum(color);
+ let low = min(color.r, min(color.g, color.b));
+ let high = max(color.r, max(color.g, color.b));
+ var result = color;
+ if (low < 0.0) {
+ result = l + (result - l) * l / (l - low);
+ }
+ if (high > 1.0) {
+ result = l + (result - l) * (1.0 - l) / (high - l);
+ }
+ return result;
+}
+
+fn setLum(color: vec3f, l: f32) -> vec3f {
+ return clipColor(color + (l - lum(color)));
+}
+
+fn softLight(base: vec3f, blend: vec3f) -> vec3f {
+ let d = select(sqrt(base), ((16.0 * base - 12.0) * base + 4.0) * base, base <= vec3f(0.25));
+ return select(
+ base + (2.0 * blend - 1.0) * (d - base),
+ base - (1.0 - 2.0 * blend) * base * (1.0 - base),
+ blend <= vec3f(0.5),
+ );
+}
+
+export fn blendColors(base: vec3f, blend: vec3f, mode: u32) -> vec3f {
+ switch (mode) {
+ case 1u: { return base * blend; }
+ case 2u: { return base + blend - base * blend; }
+ case 3u: {
+ return select(1.0 - 2.0 * (1.0 - base) * (1.0 - blend), 2.0 * base * blend, base <= vec3f(0.5));
+ }
+ case 4u: { return softLight(base, blend); }
+ case 5u: { return setLum(blend, lum(base)); }
+ case 6u: { return setLum(base, lum(blend)); }
+ default: { return blend; }
+ }
+}
diff --git a/src/core/renderer/index.ts b/src/core/renderer/index.ts
index 1be9b49..e4294f3 100644
--- a/src/core/renderer/index.ts
+++ b/src/core/renderer/index.ts
@@ -2,14 +2,24 @@ import type { Gpu, Target, Timer } from "vgpu";
import {
type BrushStroke,
type MaskLayer,
+ type PaintLayer,
+ paintingOf,
type Scene,
walkLayers,
} from "@/core/document";
import type { ImageSource, WhiteBalance } from "@/core/image";
+import type { Point } from "@/core/image/frame";
import { createRenderGraph } from "./graph";
import { createMaskRaster } from "./mask";
+import { createPatchRaster, type PatchInput } from "./mask/patches";
import { input, type RenderImage, type RenderInput } from "./node";
+import {
+ type AcceptPainting,
+ createPaintRaster,
+ type PaintInput,
+} from "./paint";
import { createProxy } from "./proxy";
+import { createStrokes } from "./strokes";
export { maskInput, mixAdjustment } from "./blend";
export {
@@ -21,6 +31,7 @@ export {
renderCoverage,
type View,
} from "./display";
+export type { PatchInput } from "./mask/patches";
export {
input,
merge,
@@ -35,6 +46,7 @@ export {
sourceSize,
split,
} from "./node";
+export type { PaintInput } from "./paint";
export { transformImages } from "./transform";
export { createRenderGraph };
@@ -44,7 +56,10 @@ export type Composition = {
retain: (id: string) => void;
/** Rasterized coverage of a mask that paints with brushes, prepared before composition. */
coverage: (layer: MaskLayer) => RenderInput | undefined;
- brush: (id: string, strokes: readonly BrushStroke[]) => RenderInput;
+ /** Rasterized paint of a paint layer, prepared before composition. */
+ paint: (layer: PaintLayer) => PaintInput | undefined;
+ /** Rasterized coverage of an effect's own stroke, such as a Healing patch. */
+ patch: (id: string, stroke: BrushStroke) => PatchInput;
};
/** App composition describes requested outputs; the engine owns their storage. */
@@ -70,16 +85,26 @@ function sameBalance(a: WhiteBalance | undefined, b: WhiteBalance | undefined) {
return a?.temperature === b?.temperature && a?.tint === b?.tint;
}
-/** Owns scene passes, mask rasters, the proxy, and intermediate textures for one decoded source. */
+/**
+ * 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.
+ */
export function createRenderer(
gpu: Gpu,
resource: ImageSource,
compose: SceneProcessing,
timer?: Timer,
+ paintPixels: (id: string) => Blob = () => {
+ throw Error("This renderer has no settled paint.");
+ },
) {
const source = resource.image;
const graph = createRenderGraph(gpu, timer);
- const raster = createMaskRaster(gpu);
+ const strokes = createStrokes(gpu);
+ const brushes = createPaintRaster(gpu, strokes, "r8unorm");
+ const masks = createMaskRaster(gpu, brushes);
+ const patches = createPatchRaster(gpu, strokes);
+ const paints = createPaintRaster(gpu, strokes, "rgba8unorm");
const proxy = createProxy(gpu);
const release = resource.retain();
const raw = resource.raw?.createPass();
@@ -96,6 +121,8 @@ 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. */
+ let settling: Promise<unknown> | undefined;
let disposed = false;
let instances = new Set<string>();
function render(request: RenderRequest) {
@@ -110,11 +137,13 @@ export function createRenderer(
}
const active = new Set<string>();
const developed = raw?.render() ?? source;
- // Every mask updates once, bypassed or not, so a hidden mask keeps its cache; child masks only shape their parent's coverage.
- const coverage = new Map<string, RenderInput | undefined>();
+ // Every mask and paint layer updates once, bypassed or not, so a hidden one keeps its raster; child masks only shape their parent's coverage.
for (const { layer, parent } of walkLayers(scene.layers)) {
if (layer.kind === "mask" && parent?.kind !== "mask") {
- coverage.set(layer.id, raster.update(layer, developed.size));
+ masks.update(layer, developed.size);
+ }
+ if (layer.kind === "paint") {
+ paints.draw(layer.id, layer, developed.size);
}
}
const image =
@@ -122,14 +151,19 @@ export function createRenderer(
const images = compose(image, scene, {
inputId,
retain: (id) => active.add(id),
- coverage: (layer) => coverage.get(layer.id),
- brush: (id, strokes) => raster.brush(id, strokes, developed.size),
+ coverage: (layer) => masks.coverage(layer.id),
+ paint: (layer) => paints.input(layer),
+ patch: (id, stroke) => patches.patch(id, stroke, developed.size),
});
for (const id of instances) {
if (!active.has(id)) graph.release(`${id}/`);
}
instances = active;
- raster.sweep();
+ // The stroke buffer goes last, once the others let go of the strokes they left open.
+ masks.sweep();
+ patches.sweep();
+ paints.sweep();
+ strokes.sweep();
const targets = graph.render([
images.original,
images.full,
@@ -145,12 +179,36 @@ export function createRenderer(
listener();
}
}
- /** Only calibration crosses the worker; each renderer owns its GPU RAW pass. */
+ /** The raster of a paint layer's color or of a brush mask's coverage. */
+ function paintRaster(id: string) {
+ return paints.get(id) ? paints : brushes;
+ }
+ /** Paintings whose rasters must load settled pixels before they draw. */
+ function stalePaint(scene: Scene) {
+ const stale = [];
+ for (const { layer } of walkLayers(scene.layers)) {
+ const painting = paintingOf(layer);
+ const rasters = layer.kind === "paint" ? paints : brushes;
+ if (painting && rasters.needsBase(layer.id, painting)) {
+ stale.push({ id: layer.id, raster: painting.raster, rasters });
+ }
+ }
+ return stale;
+ }
+ /**
+ * 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.
+ */
async function develop() {
while (next && !disposed) {
const request = next;
next = undefined;
const { scene } = request;
+ if (settling) {
+ await settling;
+ }
+ if (disposed) return;
+ if (next) continue;
const selected = scene.layers[0].whiteBalance ?? resource.raw?.asShot;
if (raw && selected && !sameBalance(balance, selected)) {
await raw.prepare(selected);
@@ -160,6 +218,12 @@ export function createRenderer(
balance = selected;
version++;
}
+ for (const { id, raster, rasters } of stalePaint(scene)) {
+ await rasters.loadBase(id, raster, paintPixels(raster), source.size);
+ if (disposed) {
+ return;
+ }
+ }
if (!next) {
render(request);
}
@@ -175,7 +239,8 @@ export function createRenderer(
throw Error("Renderer is closed.");
}
const factor = interactive ? Math.max(1, Math.floor(1 / displayScale)) : 1;
- if (!resource.raw) {
+ // Renders wait, in order, for a settle, a RAW development, or settled paint to load.
+ if (!raw && !pending && !settling && !stalePaint(scene).length) {
render({ scene, inputId, factor });
return;
}
@@ -195,21 +260,47 @@ export function createRenderer(
return pending;
}
+ function releaseResources() {
+ graph.dispose();
+ masks.dispose();
+ patches.dispose();
+ paints.dispose();
+ strokes.dispose();
+ proxy.dispose();
+ raw?.dispose();
+ release();
+ }
+
return {
originalImage: () => original,
fullImage: () => full,
outputImage: () => output,
inputImage: (id: string) =>
inspected?.id === id ? inspected.image : undefined,
- /** The rasterized coverage of a mask layer, for the display overlay. */
- coverage: (id: string) => raster.get(id)?.target,
+ /**
+ * A raster by ID and where it sits in the photo: a mask's coverage, for the display overlay and
+ * thumbnails, a Healing patch's, or a paint layer's.
+ */
+ coverage(id: string): { target: Target; origin: Point } | undefined {
+ const target = masks.coverage(id)?.target ?? paints.get(id);
+ return target ? { target, origin: [0, 0] } : patches.raster(id);
+ },
/** Device pixels shown per source pixel; interactive renders reduce the source to about this density. */
setDisplayScale(scale: number) {
if (Number.isFinite(scale) && scale > 0) {
displayScale = scale;
}
},
- inspect: () => ({ ...graph.inspect(), ...raster.inspect() }),
+ inspect: () => ({
+ ...graph.inspect(),
+ stamped: strokes.stamped(),
+ rasters: [
+ ...masks.inspect(),
+ ...patches.inspect(),
+ ...paints.inspect(),
+ ...strokes.inspect(),
+ ],
+ }),
subscribe(listener: () => void) {
listeners.add(listener);
if (rendered) {
@@ -220,17 +311,38 @@ export function createRenderer(
};
},
update,
+ /**
+ * 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),
+ );
+ settling = read;
+ try {
+ return await read;
+ } finally {
+ settling = undefined;
+ }
+ },
dispose() {
if (disposed) {
return;
}
disposed = true;
listeners.clear();
- graph.dispose();
- raster.dispose();
- proxy.dispose();
- raw?.dispose();
- release();
+ // Banded transfers keep their targets until their last GPU operation finishes.
+ const finishing = pending ?? settling;
+ if (finishing) {
+ void finishing.then(releaseResources, releaseResources);
+ } else {
+ releaseResources();
+ }
},
};
}
diff --git a/src/core/renderer/mask/dabs.ts b/src/core/renderer/mask/dabs.ts
deleted file mode 100644
index 3c5c6ee..0000000
--- a/src/core/renderer/mask/dabs.ts
+++ /dev/null
@@ -1,38 +0,0 @@
-import type { BrushStroke } from "@/core/document";
-
-/** Dab center with its radius and alpha: x, y, radius, alpha. */
-export type Dab = readonly [number, number, number, number];
-
-/** Dabs every quarter diameter along the stroke, starting at its first point; a fixed walk keeps replays identical. */
-export function strokeDabs(stroke: BrushStroke): Dab[] {
- const radius = stroke.size / 2;
- const spacing = Math.max(1, stroke.size / 4);
- const dabs: Dab[] = [];
- const [first] = stroke.points;
- if (!first) {
- return dabs;
- }
- const dab = (x: number, y: number, pressure: number): Dab => [
- x,
- y,
- radius,
- stroke.flow * pressure,
- ];
- dabs.push(dab(first[0], first[1], first[2]));
- let travelled = 0;
- let next = spacing;
- for (let i = 1; i < stroke.points.length; i++) {
- const [ax, ay, ap] = stroke.points[i - 1];
- const [bx, by, bp] = stroke.points[i];
- const length = Math.hypot(bx - ax, by - ay);
- while (length > 0 && next <= travelled + length) {
- const t = (next - travelled) / length;
- dabs.push(
- dab(ax + (bx - ax) * t, ay + (by - ay) * t, ap + (bp - ap) * t),
- );
- next += spacing;
- }
- travelled += length;
- }
- return dabs;
-}
diff --git a/src/core/renderer/mask/gradient.wgsl b/src/core/renderer/mask/gradient.wgsl
index b300dff..a342d4c 100644
--- a/src/core/renderer/mask/gradient.wgsl
+++ b/src/core/renderer/mask/gradient.wgsl
@@ -1,6 +1,6 @@
import { gradientCoverage } from "../blend/coverage.wgsl";
-// One gradient's coverage at source resolution, scaled by its strength; the pass blend adds or subtracts it.
+// One gradient's coverage in a mask's group, scaled by its strength; the pass blend adds or subtracts it.
struct Params {
kind: u32,
first: vec2f,
@@ -8,10 +8,12 @@ struct Params {
feather: f32,
angle: f32,
opacity: f32,
+ // Photo pixels per group texel, so the gradient lands where it does on the photo.
+ scale: vec2f,
}
@group(0) @binding(0) var<uniform> params: Params;
@fragment fn fs_main(@builtin(position) position: vec4f) -> @location(0) vec4f {
- let coverage = gradientCoverage(position.xy, params.first, params.second, params.kind, params.feather, params.angle);
+ let coverage = gradientCoverage(position.xy * params.scale, params.first, params.second, params.kind, params.feather, params.angle);
return vec4f(coverage * params.opacity, 0.0, 0.0, 1.0);
}
diff --git a/src/core/renderer/mask/index.ts b/src/core/renderer/mask/index.ts
index 8e9f725..3853f4b 100644
--- a/src/core/renderer/mask/index.ts
+++ b/src/core/renderer/mask/index.ts
@@ -1,36 +1,18 @@
+import { type BlendOptions, effect, frame, type Gpu, type Target } from "vgpu";
import {
- type BlendOptions,
- type Buffer,
- effect,
- frame,
- type Gpu,
- type Target,
- target,
-} from "vgpu";
-import {
- type BrushStroke,
+ hasPaint,
type MaskLayer,
type MaskModifier,
maskModifiers,
} from "@/core/document";
import { gradientParams } from "@/core/renderer/blend";
import { input, type RenderInput } from "@/core/renderer/node";
+import { type PaintRaster, paintingSize } from "@/core/renderer/paint";
+import { createRasterCache } from "@/core/renderer/raster-cache";
import brushShader from "./brush.wgsl";
-import { type Dab, strokeDabs } from "./dabs";
import gradientShader from "./gradient.wgsl";
-import strokeShader from "./strokes.wgsl";
-
-/** Dabs per pass: every fragment in the chunk's bounds loops over them. */
-const chunk = 64;
type Size = readonly [number, number];
-/** One brush's own coverage, stamped as its strokes grow. */
-type Brush = {
- target: Target;
- strokes: readonly BrushStroke[];
- /** Dabs already stamped from the last stroke. */
- dabs: number;
-};
/** A mask's own coverage combined with the children that shape it. */
type Group = {
target: Target;
@@ -45,43 +27,9 @@ function maskOps(layer: MaskLayer): readonly MaskModifier[] {
];
}
-/** Whether an op can cover pixels: a gradient always does, a brush once it has strokes. */
+/** Whether an op can cover pixels: a gradient always does, a brush once it has paint. */
function covers(op: MaskModifier) {
- return op.mask.kind !== "brush" || op.mask.strokes.length > 0;
-}
-
-function sameStroke(a: BrushStroke, b: BrushStroke) {
- return (
- a.mode === b.mode &&
- a.size === b.size &&
- a.feather === b.feather &&
- a.flow === b.flow
- );
-}
-
-/** Whether `next` only appends points to `previous`, sharing every earlier point. */
-function extendsStroke(previous: BrushStroke, next: BrushStroke) {
- return (
- previous === next ||
- (sameStroke(previous, next) &&
- next.points.length >= previous.points.length &&
- previous.points.every((point, i) => point === next.points[i]))
- );
-}
-
-/** Whether `after` only appends strokes or points to `before`. */
-function extendsStrokes(
- before: readonly BrushStroke[],
- after: readonly BrushStroke[],
-) {
- return (
- after.length >= before.length &&
- before.every((stroke, i) =>
- i === before.length - 1
- ? extendsStroke(stroke, after[i])
- : stroke === after[i],
- )
- );
+ return op.mask.kind !== "brush" || hasPaint(op.mask);
}
function sameOps(a: readonly MaskModifier[], b: readonly MaskModifier[]) {
@@ -96,164 +44,39 @@ function sameOps(a: readonly MaskModifier[], b: readonly MaskModifier[]) {
);
}
-const over: BlendOptions = { color: { src: "one", dst: "one-minus-src" } };
-const out: BlendOptions = { color: { src: "zero", dst: "one-minus-src" } };
const add: BlendOptions = { color: { src: "one", dst: "one" } };
const subtract: BlendOptions = {
color: { src: "one", dst: "one", op: "reverse-subtract" },
};
/**
- * Rasterizes brush coverage into r8unorm textures at source resolution. Each brush owns the
- * coverage of its strokes, stamped incrementally as they grow and replayed when earlier content
- * changes; a mask shaped by children combines its own coverage with theirs in a second texture,
- * rebuilt whenever any of them changes. Strokes stay the document's truth; textures are caches.
+ * Brings masks' coverage together at half the photo's resolution, as paintings keep theirs. A brush paints its coverage as a paint layer
+ * paints color, into an r8unorm raster of `brushes`; a mask shaped by children combines its own
+ * coverage with theirs in a second texture, rebuilt whenever any of them changes. Gradients alone
+ * need no texture: the mix pass computes them.
*/
-export function createMaskRaster(gpu: Gpu) {
- const brushes = new Map<string, Brush>();
- const groups = new Map<string, Group>();
- const used = new Set<Brush | Group>();
- const dabs: Buffer = gpu.device.createBuffer({
- size: chunk * 16,
- usage: ["storage", "copy_dst"],
- });
- const paint = effect(gpu, strokeShader, { blend: over, set: { dabs } });
- const erase = effect(gpu, strokeShader, { blend: out, set: { dabs } });
+export function createMaskRaster(gpu: Gpu, brushes: PaintRaster) {
+ const groups = createRasterCache<Group>(gpu, "r8unorm", (target) => ({
+ target,
+ ops: [],
+ }));
+ /** Each mask layer's coverage as its last update left it: its brush's, its group's, or none. */
+ const shown = new Map<string, Target | Group | undefined>();
+ const updated = new Set<string>();
const gradientAdd = effect(gpu, gradientShader, { blend: add });
const gradientSubtract = effect(gpu, gradientShader, { blend: subtract });
const brushAdd = effect(gpu, brushShader, { blend: add });
const brushSubtract = effect(gpu, brushShader, { blend: subtract });
- let stamped = 0;
-
- function clear(target: Target) {
- frame(gpu, (frame) => frame.pass({ target, clear: true }, () => {}));
- }
-
- function stampDabs(
- target: Target,
- dabList: readonly Dab[],
- feather: number,
- mode: BrushStroke["mode"],
- ) {
- const [width, height] = target.size;
- for (let start = 0; start < dabList.length; start += chunk) {
- const batch = dabList.slice(start, start + chunk);
- let left = width;
- let top = height;
- let right = 0;
- let bottom = 0;
- const data = new Float32Array(batch.length * 4);
- batch.forEach((dab, i) => {
- data.set(dab, i * 4);
- left = Math.min(left, dab[0] - dab[2]);
- top = Math.min(top, dab[1] - dab[2]);
- right = Math.max(right, dab[0] + dab[2]);
- bottom = Math.max(bottom, dab[1] + dab[2]);
- });
- const x = Math.max(0, Math.floor(left) - 1);
- const y = Math.max(0, Math.floor(top) - 1);
- const w = Math.min(width, Math.ceil(right) + 1) - x;
- const h = Math.min(height, Math.ceil(bottom) + 1) - y;
- if (w <= 0 || h <= 0) {
- continue;
- }
- dabs.write(data);
- const pass = mode === "paint" ? paint : erase;
- pass.set({ params: { count: batch.length, feather } });
- // One frame per chunk: buffer and uniform writes land in queue order, before the pass that reads them.
- frame(gpu, (frame) =>
- frame.pass({ target, clear: false, scissor: [x, y, w, h] }, pass),
- );
- stamped += batch.length;
- }
- }
-
- /** Stamps strokes from `fromStroke` on, skipping `skipDabs` of that first one; returns the last stroke's dab count. */
- function stampStrokes(
- target: Target,
- strokes: readonly BrushStroke[],
- fromStroke = 0,
- skipDabs = 0,
- ) {
- let count = 0;
- strokes.forEach((stroke, i) => {
- if (i < fromStroke) {
- return;
- }
- const all = strokeDabs(stroke);
- count = all.length;
- stampDabs(
- target,
- i === fromStroke ? all.slice(skipDabs) : all,
- stroke.feather,
- stroke.mode,
- );
- });
- return count;
- }
-
- /** The entry for `id` at `size`, created or resized as needed, and marked as in use. */
- function reserve<E extends Brush | Group>(
- map: Map<string, E>,
- id: string,
- size: Size,
- create: (target: Target) => E,
- ) {
- let entry = map.get(id);
- if (
- entry &&
- (entry.target.size[0] !== size[0] || entry.target.size[1] !== size[1])
- ) {
- entry.target.color.dispose();
- entry = undefined;
- }
- if (!entry) {
- entry = create(target(gpu, { size: [...size], format: "r8unorm" }));
- map.set(id, entry);
- }
- used.add(entry);
- return entry;
- }
-
- /** Brings one brush's coverage up to date: appended points stamp, any other change replays every stroke. */
- function updateBrush(
- id: string,
- strokes: readonly BrushStroke[],
- size: Size,
- ) {
- const brush = reserve(brushes, id, size, (target) => ({
- target,
- strokes: [],
- dabs: 0,
- }));
- const applied = brush.strokes;
- if (applied === strokes) {
- return brush;
- }
- if (applied.length && extendsStrokes(applied, strokes)) {
- brush.dabs = stampStrokes(
- brush.target,
- strokes,
- applied.length - 1,
- brush.dabs,
- );
- } else {
- clear(brush.target);
- brush.dabs = stampStrokes(brush.target, strokes);
- }
- brush.strokes = strokes;
- return brush;
- }
/** The pass that adds or subtracts one op's coverage, or nothing for a brush without strokes. */
- function opPass(op: MaskModifier) {
+ function opPass(op: MaskModifier, scale: Size) {
if (op.mask.kind !== "brush") {
const pass = op.operation === "add" ? gradientAdd : gradientSubtract;
return pass.set({
- params: { ...gradientParams(op.mask), opacity: op.opacity },
+ params: { ...gradientParams(op.mask), opacity: op.opacity, scale },
});
}
- const brush = covers(op) ? brushes.get(op.id) : undefined;
+ const brush = covers(op) ? brushes.coverage(op.id) : undefined;
if (!brush) {
return undefined;
}
@@ -265,14 +88,19 @@ export function createMaskRaster(gpu: Gpu) {
}
/** Combines a mask's own coverage with its children's in stored order, once any of them changed. */
- function updateGroup(id: string, ops: readonly MaskModifier[], size: Size) {
- const group = reserve(groups, id, size, (target) => ({ target, ops: [] }));
+ function updateGroup(id: string, ops: readonly MaskModifier[], source: Size) {
+ const size = paintingSize(source);
+ const group = groups.reserve(id, size);
+ // Gradients evaluate at photo pixels, and the group keeps the brushes' resolution.
+ const scale = [source[0] / size[0], source[1] / size[1]] as const;
if (sameOps(group.ops, ops)) {
return group;
}
- clear(group.target);
+ frame(gpu, (frame) =>
+ frame.pass({ target: group.target, clear: true }, () => {}),
+ );
for (const op of ops) {
- const pass = opPass(op);
+ const pass = opPass(op, scale);
if (pass) {
// One frame per pass: uniform writes land in queue order, before the pass that reads them.
frame(gpu, (frame) =>
@@ -284,79 +112,65 @@ export function createMaskRaster(gpu: Gpu) {
return group;
}
- function release<E extends Brush | Group>(map: Map<string, E>) {
- for (const [id, entry] of map) {
- if (!used.has(entry)) {
- entry.target.color.dispose();
- map.delete(id);
+ /** Brings a mask layer's rasters up to date and returns the one that holds its coverage, if any. */
+ function updateLayer(layer: MaskLayer, size: Size) {
+ const [own, ...children] = maskOps(layer);
+ const active = children.filter(covers);
+ const ops = [own, ...active];
+ for (const op of ops) {
+ if (op.mask.kind === "brush" && covers(op)) {
+ brushes.draw(op.id, op.mask, size);
}
}
+ if (own.mask.kind !== "brush") {
+ // Gradient children combine in the mix pass; a painted brush child needs a raster.
+ return active.some((op) => op.mask.kind === "brush")
+ ? updateGroup(layer.id, ops, size)
+ : undefined;
+ }
+ if (active.length === 0) {
+ // Nothing shapes the brush, so its own coverage is the mask's; without strokes the layer is bypassed.
+ return covers(own) ? brushes.get(own.id) : undefined;
+ }
+ if (!covers(own) && !active.some((op) => op.operation === "add")) {
+ return undefined;
+ }
+ return updateGroup(layer.id, ops, size);
}
return {
- /** Shares the incremental brush cache with effects that own individual strokes. */
- brush(id: string, strokes: readonly BrushStroke[], size: Size) {
- return input(updateBrush(id, strokes, size).target);
+ /** Brings a mask layer's rasters up to date; `coverage` reads them once every layer has. */
+ update(layer: MaskLayer, size: Size) {
+ shown.set(layer.id, updateLayer(layer, size));
+ updated.add(layer.id);
},
- /** Brings the layer's rasters up to date and returns the coverage its composition samples, if any. */
- update(layer: MaskLayer, size: Size): RenderInput | undefined {
- const [own, ...children] = maskOps(layer);
- const active = children.filter(covers);
- const ops = [own, ...active];
- for (const op of ops) {
- if (op.mask.kind === "brush" && covers(op)) {
- updateBrush(op.id, op.mask.strokes, size);
- }
- }
- if (own.mask.kind !== "brush") {
- // Gradient children combine in the mix pass; a painted brush child needs a raster.
- return active.some((op) => op.mask.kind === "brush")
- ? input(updateGroup(layer.id, ops, size).target)
- : undefined;
- }
- if (active.length === 0) {
- // Nothing shapes the brush, so its own coverage is the mask's; without strokes the layer is bypassed.
- const brush = covers(own) ? brushes.get(own.id) : undefined;
- return brush && input(brush.target);
- }
- if (!covers(own) && !active.some((op) => op.operation === "add")) {
+ /**
+ * A mask layer's coverage, or a brush's own, if any. Read it once every layer updated: drawing one
+ * brush can close another's stroke.
+ */
+ coverage(id: string): RenderInput | undefined {
+ const raster = shown.has(id) ? shown.get(id) : brushes.get(id);
+ if (!raster) {
return undefined;
}
- return input(updateGroup(layer.id, ops, size).target);
- },
- /** A mask's combined coverage, or a brush's own. */
- get(id: string): RenderInput | undefined {
- const raster = groups.get(id) ?? brushes.get(id);
- return raster && input(raster.target);
+ return "ops" in raster ? input(raster.target) : brushes.coverage(id);
},
- /** Releases the rasters that no update used since the previous sweep. */
+ /** Releases the rasters that no update used since the previous sweep, including their painting views. */
sweep() {
- release(brushes);
- release(groups);
- used.clear();
- },
- inspect() {
- return {
- stamped,
- rasters: [
- ...[...brushes].map(([id, { target }]) => ({
- id,
- size: [...target.size],
- })),
- ...[...groups].map(([id, { target }]) => ({
- id: `${id}/group`,
- size: [...target.size],
- })),
- ],
- };
+ for (const id of shown.keys()) {
+ if (!updated.has(id)) {
+ shown.delete(id);
+ }
+ }
+ updated.clear();
+ brushes.sweep();
+ groups.sweep();
},
+ inspect: () => [...brushes.inspect(), ...groups.inspect("/group")],
dispose() {
- for (const { target } of [...brushes.values(), ...groups.values()]) {
- target.color.dispose();
- }
- brushes.clear();
- groups.clear();
- dabs.dispose();
+ brushes.dispose();
+ groups.dispose();
+ shown.clear();
},
};
}
diff --git a/src/core/renderer/mask/patches.ts b/src/core/renderer/mask/patches.ts
new file mode 100644
index 0000000..5a687fa
--- /dev/null
+++ b/src/core/renderer/mask/patches.ts
@@ -0,0 +1,83 @@
+import type { Gpu, Target } from "vgpu";
+import type { BrushStroke } from "@/core/document";
+import type { Point } from "@/core/image/frame";
+import { input, type RenderInput } from "@/core/renderer/node";
+import { createRasterCache } from "@/core/renderer/raster-cache";
+import { extendsStroke, type Strokes } from "@/core/renderer/strokes";
+import type { DabWalk } from "@/core/renderer/strokes/dabs";
+
+type Size = readonly [number, number];
+/** Rasters cover whole tiles, so one follows a growing stroke without reallocating at every move. */
+const tile = 256;
+
+type Patch = {
+ target: Target;
+ origin: Point;
+ stroke?: BrushStroke;
+ /** Where the stroke's dabs stopped, to go on from there as it grows. */
+ walk?: DabWalk;
+};
+
+/** A patch's coverage and where it sits in the photo, in source pixels. */
+export type PatchInput = { coverage: RenderInput; origin: Point };
+
+/** The tiles a stroke's dabs reach, within the photo. */
+function strokeTiles(stroke: BrushStroke, [width, height]: Size) {
+ const radius = stroke.size / 2 + 1;
+ const xs = stroke.points.map(([x]) => x);
+ const ys = stroke.points.map(([, y]) => y);
+ const snap = (value: number, round: (value: number) => number, end: number) =>
+ Math.min(end, Math.max(0, round(value / tile) * tile));
+ const left = snap(Math.min(...xs) - radius, Math.floor, width);
+ const top = snap(Math.min(...ys) - radius, Math.floor, height);
+ const right = snap(Math.max(...xs) + radius, Math.ceil, width);
+ const bottom = snap(Math.max(...ys) + radius, Math.ceil, height);
+ return {
+ origin: [left, top] as Point,
+ size: [Math.max(1, right - left), Math.max(1, bottom - top)] as const,
+ };
+}
+
+/**
+ * Rasterizes the hard strokes of Healing patches into r8unorm textures over only the tiles they reach,
+ * since a layer holds any number of patches. A stroke at full flow rounds the same either way, so it
+ * stamps straight into its raster, and a patch still growing stamps only its new dabs.
+ */
+export function createPatchRaster(gpu: Gpu, strokes: Strokes) {
+ const patches = createRasterCache<Patch>(gpu, "r8unorm", (target) => ({
+ target,
+ origin: [0, 0],
+ }));
+ return {
+ patch(id: string, stroke: BrushStroke, size: Size): PatchInput {
+ const tiles = strokeTiles(stroke, size);
+ const patch = patches.reserve(id, tiles.size);
+ const moved =
+ patch.origin[0] !== tiles.origin[0] ||
+ patch.origin[1] !== tiles.origin[1];
+ if (moved || !patch.stroke || !extendsStroke(patch.stroke, stroke)) {
+ strokes.clear(patch.target);
+ Object.assign(patch, {
+ origin: tiles.origin,
+ stroke: undefined,
+ walk: undefined,
+ });
+ }
+ if (patch.stroke !== stroke) {
+ patch.walk = strokes.stamp(
+ patch.target,
+ stroke,
+ patch.walk,
+ patch.origin,
+ );
+ patch.stroke = stroke;
+ }
+ return { coverage: input(patch.target), origin: patch.origin };
+ },
+ /** A patch's raster and where it sits in the photo, for its thumbnail. */
+ raster: (id: string) => patches.get(id),
+ sweep: patches.sweep,
+ inspect: () => patches.inspect(),
+ dispose: patches.dispose,
+ };
+}
diff --git a/src/core/renderer/paint/index.ts b/src/core/renderer/paint/index.ts
new file mode 100644
index 0000000..9e010c3
--- /dev/null
+++ b/src/core/renderer/paint/index.ts
@@ -0,0 +1,225 @@
+import { type Gpu, type Target, target } from "vgpu";
+import {
+ hasPaint,
+ type Painting,
+ type PaintLayer,
+ type PaintStroke,
+} from "@/core/document";
+import { input, type RenderInput } from "@/core/renderer/node";
+import { createRasterCache } from "@/core/renderer/raster-cache";
+import {
+ extendsStrokes,
+ type Rect,
+ type Stroke,
+ type Strokes,
+} from "@/core/renderer/strokes";
+import type { DabWalk } from "@/core/renderer/strokes/dabs";
+import { readRaster, writeRaster } from "./transfer";
+
+type Size = readonly [number, number];
+
+/**
+ * Paintings keep half the photo's resolution each way, a quarter of its pixels, which the passes that
+ * read them sample back to full size by position. A stroke can't set a single photo pixel, which
+ * photos rarely want of paint or masks, and each raster takes a quarter of the memory.
+ */
+export function paintingSize([width, height]: Size): Size {
+ return [Math.ceil(width / 2), Math.ceil(height / 2)];
+}
+
+/** Raster pixels per photo pixel on each axis. */
+function paintingScale(source: Size): Size {
+ const [width, height] = paintingSize(source);
+ return [width / source[0], height / source[1]];
+}
+
+/** One painting's raster: its settled pixels, if any, and strokes over them. */
+type Raster = {
+ target: Target;
+ /** The settled pixels the raster starts from. */
+ base?: string;
+ strokes: readonly Stroke[];
+ /** Where the last stroke's dabs stopped, to go on from there as it grows. */
+ walk?: DabWalk;
+};
+const none: readonly Stroke[] = [];
+
+/** A paint layer's raster, and its open stroke, which the stroke buffer holds, to lay over it. */
+export type PaintInput = {
+ raster: RenderInput;
+ /** The stroke buffer, or the raster again while no stroke of the layer is open. */
+ buffer: RenderInput;
+ stroke?: PaintStroke;
+};
+
+/** Whether a raster holds `base` and strokes that only grow into `strokes`. */
+function holds(
+ raster: Raster | undefined,
+ { raster: base, strokes }: Painting,
+) {
+ return (
+ raster !== undefined &&
+ raster.base === base &&
+ (raster.strokes === strokes || extendsStrokes(raster.strokes, strokes))
+ );
+}
+
+/** Pixels and the stroke prefix they replace, accepted only while the painting still has that prefix. */
+type SettledPainting = {
+ base?: string;
+ strokes: readonly Stroke[];
+ pixels: Blob;
+};
+export type AcceptPainting = (settled: SettledPainting) => string | undefined;
+export type PaintRaster = ReturnType<typeof createPaintRaster>;
+
+/**
+ * Rasterizes paintings at half the photo's resolution, premultiplied color for paint layers and coverage for
+ * brush masks: settled pixels, loaded first, and strokes drawn over them through the stroke buffer. A
+ * raster comes with its painting's first stroke or pixels and stays, cleared if need be, while the
+ * painting lasts, so painting and undoing never reallocate it.
+ */
+export function createPaintRaster(
+ gpu: Gpu,
+ strokes: Strokes,
+ format: "rgba8unorm" | "r8unorm",
+) {
+ // Coverage readers need the open stroke laid over its raster; paint composes the two separately.
+ let view: Target | undefined;
+ function show(brush: Target, changed?: Rect) {
+ const [width, height] = brush.size;
+ if (view && (view.size[0] !== width || view.size[1] !== height)) {
+ view.color.dispose();
+ view = undefined;
+ }
+ view ??= target(gpu, {
+ size: [width, height],
+ format: "r8unorm",
+ clearColor: [0, 0, 0, 0],
+ });
+ strokes.resolve(view, brush, changed ?? [0, 0, width, height]);
+ }
+ const rasters = createRasterCache<Raster>(
+ gpu,
+ format,
+ (target) => ({ target, strokes: none }),
+ (raster) => strokes.discard(raster.target),
+ );
+ return {
+ get: (id: string) => rasters.get(id)?.target,
+ /** Coverage with the latest stroke laid over it, read after every painting drew. */
+ coverage(id: string) {
+ const raster = rasters.get(id)?.target;
+ return raster && input(view && strokes.isOpen(raster) ? view : raster);
+ },
+ /** Whether a painting's raster must load its settled pixels before it can draw the strokes. */
+ needsBase(
+ id: string,
+ painting: Painting,
+ ): painting is Painting & { raster: string } {
+ return painting.raster !== undefined && !holds(rasters.get(id), painting);
+ },
+ /** Loads a painting's settled pixels; the next draw lays its strokes over them. `source` is the photo's size. */
+ async loadBase(id: string, base: string, pixels: Blob, source: Size) {
+ const raster = rasters.reserve(id, paintingSize(source));
+ strokes.discard(raster.target);
+ await writeRaster(gpu, raster.target, pixels);
+ Object.assign(raster, { base, strokes: none, walk: undefined });
+ },
+ /**
+ * Brings a painting's raster and coverage view up to date once its settled pixels loaded.
+ * `source` is the photo's size.
+ */
+ draw(id: string, painting: Painting, source: Size) {
+ if (!hasPaint(painting) && !rasters.get(id)) {
+ return undefined;
+ }
+ const raster = rasters.reserve(id, paintingSize(source));
+ const { target } = raster;
+ if (!holds(raster, painting)) {
+ if (painting.raster !== undefined) {
+ throw Error("Load the painting's settled pixels first.");
+ }
+ strokes.discard(target);
+ strokes.clear(target);
+ Object.assign(raster, {
+ base: undefined,
+ strokes: none,
+ walk: undefined,
+ });
+ }
+ if (raster.strokes === painting.strokes) {
+ return;
+ }
+ const drawn = raster.strokes.length;
+ const { walk, opened, changed } = strokes.draw(
+ target,
+ painting.strokes,
+ Math.max(drawn - 1, 0),
+ drawn ? raster.walk : undefined,
+ paintingScale(source),
+ );
+ Object.assign(raster, { strokes: painting.strokes, walk });
+ if (
+ format === "r8unorm" &&
+ strokes.isOpen(target) &&
+ (opened || changed)
+ ) {
+ show(target, opened ? undefined : changed);
+ }
+ },
+ /** What a paint layer composes. Read it once every layer drew: drawing one can close another's stroke. */
+ input(layer: PaintLayer): PaintInput | undefined {
+ const target = rasters.get(layer.id)?.target;
+ if (!target || !hasPaint(layer)) {
+ return undefined;
+ }
+ const open = strokes.isOpen(target);
+ return {
+ raster: input(target),
+ buffer: input((open && strokes.buffer()) || target),
+ stroke: open ? layer.strokes.at(-1) : undefined,
+ };
+ },
+ /**
+ * Reads a painting's raster, its settled pixels with every stroke laid in, so they settle into new
+ * pixels. Accepting them replaces the scene's stroke prefix and records its new raster base together.
+ * Nothing may draw until both finish.
+ */
+ async settle(id: string, accept: AcceptPainting) {
+ const raster = rasters.get(id);
+ if (!raster?.strokes.length) {
+ return false;
+ }
+ if (strokes.isOpen(raster.target)) {
+ strokes.close();
+ }
+ const { base, strokes: settled } = raster;
+ const pixels = await readRaster(gpu, raster.target);
+ const accepted = accept({ base, strokes: settled, pixels });
+ if (accepted === undefined) {
+ return false;
+ }
+ Object.assign(raster, { base: accepted, strokes: none, walk: undefined });
+ return true;
+ },
+ sweep() {
+ rasters.sweep();
+ if (!rasters.size) {
+ view?.color.dispose();
+ view = undefined;
+ }
+ },
+ inspect: () => [
+ ...rasters.inspect(),
+ ...(view
+ ? [{ id: "stroke view", size: [...view.size], format: view.format }]
+ : []),
+ ],
+ dispose() {
+ rasters.dispose();
+ view?.color.dispose();
+ view = undefined;
+ },
+ };
+}
diff --git a/src/core/renderer/paint/transfer.ts b/src/core/renderer/paint/transfer.ts
new file mode 100644
index 0000000..d8b97ed
--- /dev/null
+++ b/src/core/renderer/paint/transfer.ts
@@ -0,0 +1,137 @@
+import { effect, frame, type Gpu, type Target } from "vgpu";
+import copyShader from "@/core/renderer/strokes/copy.wgsl";
+import { weakMemo } from "@/lib/weak-memo";
+
+/** Rows moved at a time, so neither direction holds a copy of the whole raster. */
+const bandRows = 256;
+
+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. */
+function rowBytes({ size, format }: Target) {
+ return size[0] * (format === "r8unorm" ? 1 : 4);
+}
+
+/**
+ * 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.
+ */
+export async function readRaster(gpu: Gpu, raster: Target) {
+ const device = gpu.gpu;
+ const [width, height] = raster.size;
+ const packed = rowBytes(raster);
+ // Buffer copies take rows aligned to 256 bytes.
+ const stride = Math.ceil(packed / 256) * 256;
+ const buffer = gpu.device.createBuffer({
+ size: stride * Math.min(bandRows, height),
+ usage: ["copy_dst", "map_read"],
+ });
+ const compression = new CompressionStream("deflate-raw");
+ const writer = compression.writable.getWriter();
+ const pixels = new Response(compression.readable).blob();
+ try {
+ for (let y = 0; y < height; y += bandRows) {
+ const rows = Math.min(bandRows, height - y);
+ const encoder = device.createCommandEncoder();
+ encoder.copyTextureToBuffer(
+ { texture: raster.color.gpu, origin: [0, y] },
+ { buffer: buffer.gpu, bytesPerRow: stride },
+ [width, rows],
+ );
+ device.queue.submit([encoder.finish()]);
+ await buffer.gpu.mapAsync(mapRead, 0, stride * rows);
+ const read = new Uint8Array(buffer.gpu.getMappedRange(0, stride * rows));
+ const band = new Uint8Array(packed * rows);
+ for (let row = 0; row < rows; row++) {
+ const start = row * stride;
+ band.set(read.subarray(start, start + packed), row * packed);
+ }
+ buffer.gpu.unmap();
+ await writer.write(band);
+ }
+ await writer.close();
+ } catch (error) {
+ await writer.abort(error);
+ throw error;
+ } finally {
+ buffer.destroy();
+ }
+ return pixels;
+}
+
+/**
+ * Inflates bytes `readRaster` wrote into a raster of the same size and format, band by band. Render
+ * targets take no copies, so each band lands in a staging texture that draws into the raster.
+ */
+export async function writeRaster(gpu: Gpu, raster: Target, pixels: Blob) {
+ const [width, height] = raster.size;
+ const packed = rowBytes(raster);
+ const band = new Uint8Array(packed * Math.min(bandRows, height));
+ const staging = gpu.device.createTexture({
+ size: [width, band.length / packed],
+ format: raster.format,
+ usage: ["texture_binding", "copy_dst"],
+ });
+ let filled = 0;
+ let y = 0;
+ function draw(rows: number) {
+ if (y + rows > height) {
+ throw Error("The paint doesn't match the photo'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(
+ { texture: staging.gpu },
+ band,
+ { bytesPerRow: packed },
+ [width, rows],
+ );
+ const pass = copy(gpu).set({
+ source: staging,
+ params: { offset: [0, -y] },
+ });
+ frame(gpu, (frame) =>
+ frame.pass(
+ { target: raster, clear: false, scissor: [0, y, width, rows] },
+ pass,
+ ),
+ );
+ y += rows;
+ }
+ const reader = pixels
+ .stream()
+ .pipeThrough(new DecompressionStream("deflate-raw"))
+ .getReader();
+ try {
+ for (;;) {
+ const { done, value } = await reader.read();
+ if (done) {
+ break;
+ }
+ for (let offset = 0; offset < value.length; ) {
+ const taken = Math.min(band.length - filled, value.length - offset);
+ band.set(value.subarray(offset, offset + taken), filled);
+ filled += taken;
+ offset += taken;
+ if (filled === band.length) {
+ draw(band.length / packed);
+ filled = 0;
+ }
+ }
+ }
+ if (filled % packed !== 0) {
+ throw Error("The paint doesn't match the photo's size.");
+ }
+ if (filled) {
+ draw(filled / packed);
+ }
+ if (y !== height) {
+ throw Error("The paint doesn't match the photo's size.");
+ }
+ } finally {
+ staging.dispose();
+ }
+}
diff --git a/src/core/renderer/raster-cache.ts b/src/core/renderer/raster-cache.ts
new file mode 100644
index 0000000..08b7819
--- /dev/null
+++ b/src/core/renderer/raster-cache.ts
@@ -0,0 +1,70 @@
+import { type Gpu, type Target, target } from "vgpu";
+
+type Size = readonly [number, number];
+
+/**
+ * Rasters by ID, each at the size it needs and transparent at first, kept while renders use them:
+ * `reserve` marks one used, and `sweep` frees those no render reserved since the last sweep. `onFree`
+ * lets go of anything that points into a raster before it goes.
+ */
+export function createRasterCache<E extends { target: Target }>(
+ gpu: Gpu,
+ format: GPUTextureFormat,
+ create: (target: Target) => E,
+ onFree: (entry: E) => void = () => {},
+) {
+ const entries = new Map<string, E>();
+ const used = new Set<E>();
+ function free(id: string, entry: E) {
+ onFree(entry);
+ entry.target.color.dispose();
+ entries.delete(id);
+ }
+ return {
+ get: (id: string) => entries.get(id),
+ get size() {
+ return entries.size;
+ },
+ /** The raster for `id` at `size`, created, or created again at a new size, and marked used. */
+ reserve(id: string, size: Size) {
+ let entry = entries.get(id);
+ if (
+ entry &&
+ (entry.target.size[0] !== size[0] || entry.target.size[1] !== size[1])
+ ) {
+ free(id, entry);
+ entry = undefined;
+ }
+ if (!entry) {
+ entry = create(
+ target(gpu, { size: [...size], format, clearColor: [0, 0, 0, 0] }),
+ );
+ entries.set(id, entry);
+ }
+ used.add(entry);
+ return entry;
+ },
+ sweep() {
+ for (const [id, entry] of entries) {
+ if (!used.has(entry)) {
+ free(id, entry);
+ }
+ }
+ used.clear();
+ },
+ inspect(suffix = "") {
+ return [...entries].map(([id, { target }]) => ({
+ id: `${id}${suffix}`,
+ size: [...target.size],
+ format: target.format,
+ }));
+ },
+ dispose() {
+ for (const { target } of entries.values()) {
+ target.color.dispose();
+ }
+ entries.clear();
+ used.clear();
+ },
+ };
+}
diff --git a/src/core/renderer/strokes/copy.wgsl b/src/core/renderer/strokes/copy.wgsl
new file mode 100644
index 0000000..d9c23fa
--- /dev/null
+++ b/src/core/renderer/strokes/copy.wgsl
@@ -0,0 +1,10 @@
+// Copies a texture by position, shifted by `offset`; the pass's scissor keeps it to the part it copies.
+struct Params {
+ offset: vec2f,
+}
+@group(0) @binding(0) var source: texture_2d<f32>;
+@group(0) @binding(1) var<uniform> params: Params;
+
+@fragment fn fs_main(@builtin(position) position: vec4f) -> @location(0) vec4f {
+ return textureLoad(source, vec2i(position.xy + params.offset), 0);
+}
diff --git a/src/core/renderer/strokes/dabs.ts b/src/core/renderer/strokes/dabs.ts
new file mode 100644
index 0000000..3c9ff2a
--- /dev/null
+++ b/src/core/renderer/strokes/dabs.ts
@@ -0,0 +1,54 @@
+import type { BrushStroke } from "@/core/document";
+
+/** Dab center with its radius and alpha: x, y, radius, alpha. */
+export type Dab = readonly [number, number, number, number];
+
+/** Where a walk along a stroke stopped: the points it passed, the distance it covered, and where the next dab lands. */
+export type DabWalk = {
+ readonly points: number;
+ readonly travelled: number;
+ readonly next: number;
+};
+
+/**
+ * Dabs sixteen times per diameter along the stroke, starting at its first point, close enough that soft
+ * edges add up without ripples; a fixed walk keeps replays identical. Each dab lays less, so a stroke
+ * builds up as it does with four, Photoshop's spacing. A walk goes on `from` where an earlier one
+ * stopped on the same first points, so a growing stroke walks each point once.
+ */
+export function walkDabs(
+ stroke: BrushStroke,
+ from?: DabWalk,
+): { dabs: Dab[]; walk: DabWalk } {
+ const radius = stroke.size / 2;
+ const spacing = Math.max(1, stroke.size / 16);
+ const share = spacing / Math.max(1, stroke.size / 4);
+ const dabs: Dab[] = [];
+ const dab = (x: number, y: number, pressure: number): Dab => [
+ x,
+ y,
+ radius,
+ 1 - (1 - stroke.flow * pressure) ** share,
+ ];
+ const [first] = stroke.points;
+ const resumed = from?.points ? from : undefined;
+ if (first && !resumed) {
+ dabs.push(dab(first[0], first[1], first[2]));
+ }
+ let travelled = resumed?.travelled ?? 0;
+ let next = resumed?.next ?? spacing;
+ for (let i = resumed?.points ?? 1; i < stroke.points.length; i++) {
+ const [ax, ay, ap] = stroke.points[i - 1];
+ const [bx, by, bp] = stroke.points[i];
+ const length = Math.hypot(bx - ax, by - ay);
+ while (length > 0 && next <= travelled + length) {
+ const t = (next - travelled) / length;
+ dabs.push(
+ dab(ax + (bx - ax) * t, ay + (by - ay) * t, ap + (bp - ap) * t),
+ );
+ next += spacing;
+ }
+ travelled += length;
+ }
+ return { dabs, walk: { points: stroke.points.length, travelled, next } };
+}
diff --git a/src/core/renderer/strokes/empty.wgsl b/src/core/renderer/strokes/empty.wgsl
new file mode 100644
index 0000000..76bbceb
--- /dev/null
+++ b/src/core/renderer/strokes/empty.wgsl
@@ -0,0 +1,4 @@
+// Empties the stroke buffer where a stroke reached: clearing all of it for every stroke would write the whole photo.
+@fragment fn fs_main() -> @location(0) vec4f {
+ return vec4f(0.0);
+}
diff --git a/src/core/renderer/strokes/index.ts b/src/core/renderer/strokes/index.ts
new file mode 100644
index 0000000..9e6b46e
--- /dev/null
+++ b/src/core/renderer/strokes/index.ts
@@ -0,0 +1,326 @@
+import {
+ type BlendOptions,
+ type Buffer,
+ effect,
+ frame,
+ type Gpu,
+ type Target,
+ target,
+} from "vgpu";
+import type { BrushStroke, PaintStroke } from "@/core/document";
+import { parseColor } from "@/core/image/blend";
+import type { Point } from "@/core/image/frame";
+import copyShader from "./copy.wgsl";
+import { type Dab, type DabWalk, walkDabs } from "./dabs";
+import emptyShader from "./empty.wgsl";
+import layShader from "./lay.wgsl";
+import stampShader from "./stamp.wgsl";
+
+/** Dabs per pass: every fragment in the chunk's bounds loops over them. */
+const chunk = 32;
+
+type Size = readonly [number, number];
+/** x, y, width, height, in pixels. */
+export type Rect = readonly [number, number, number, number];
+/** Every stroke stamps coverage; a paint stroke also has the color it lays. */
+export type Stroke = BrushStroke | PaintStroke;
+
+const white = [1, 1, 1] as const;
+
+function union(a: Rect | undefined, b: Rect | undefined): Rect | undefined {
+ if (!a || !b) {
+ return a ?? b;
+ }
+ const left = Math.min(a[0], b[0]);
+ const top = Math.min(a[1], b[1]);
+ return [
+ left,
+ top,
+ Math.max(a[0] + a[2], b[0] + b[2]) - left,
+ Math.max(a[1] + a[3], b[1] + b[3]) - top,
+ ];
+}
+
+function strokeColor(stroke: Stroke) {
+ return "color" in stroke ? stroke.color : undefined;
+}
+
+function sameStroke(a: Stroke, b: Stroke) {
+ return (
+ a.mode === b.mode &&
+ a.size === b.size &&
+ a.feather === b.feather &&
+ a.flow === b.flow &&
+ strokeColor(a) === strokeColor(b)
+ );
+}
+
+/** Whether `next` only appends points to `previous`, sharing every earlier point. */
+export function extendsStroke(previous: Stroke, next: Stroke) {
+ return (
+ previous === next ||
+ (sameStroke(previous, next) &&
+ next.points.length >= previous.points.length &&
+ previous.points.every((point, i) => point === next.points[i]))
+ );
+}
+
+/** Whether `after` only appends strokes or points to `before`. */
+export function extendsStrokes(
+ before: readonly Stroke[],
+ after: readonly Stroke[],
+) {
+ return (
+ after.length >= before.length &&
+ before.every((stroke, i) =>
+ i === before.length - 1
+ ? extendsStroke(stroke, after[i])
+ : stroke === after[i],
+ )
+ );
+}
+
+// Premultiplied: paint lays its share over what is there; erase removes that share of it.
+const over: BlendOptions = {
+ color: { src: "one", dst: "one-minus-src-alpha" },
+ alpha: { src: "one", dst: "one-minus-src-alpha" },
+};
+const out: BlendOptions = {
+ color: { src: "zero", dst: "one-minus-src-alpha" },
+ alpha: { src: "zero", dst: "one-minus-src-alpha" },
+};
+
+export type Strokes = ReturnType<typeof createStrokes>;
+
+/**
+ * Stamps strokes into the rasters that cache them. A mask's or paint layer's stroke builds up in the
+ * stroke buffer, half floats as large as the rasters, one stroke at a time: its raster takes it in one
+ * pass once another stroke starts, so a stroke drawn a few dabs at a time rounds to 8 bits once, as it
+ * does drawn whole. Until then, whoever shows the raster lays the open stroke over it.
+ */
+export function createStrokes(gpu: Gpu) {
+ const dabs: Buffer = gpu.device.createBuffer({
+ size: chunk * 16,
+ usage: ["storage", "copy_dst"],
+ });
+ const cover = effect(gpu, stampShader, { blend: over, set: { dabs } });
+ const uncover = effect(gpu, stampShader, { blend: out, set: { dabs } });
+ const layOver = effect(gpu, layShader, { blend: over });
+ const layOut = effect(gpu, layShader, { blend: out });
+ const empty = effect(gpu, emptyShader);
+ const copy = effect(gpu, copyShader);
+ let buffer: Target | undefined;
+ /** The last stroke drawn through the buffer, stroke `index` of `raster`, and where its dabs reached. */
+ let open:
+ | { raster: Target; index: number; stroke: Stroke; bounds?: Rect }
+ | undefined;
+ let reserved = false;
+ let stamped = 0;
+
+ /**
+ * Stamps dabs' coverage into `target`, a chunk at a time. The raster sits at `origin` in the photo and
+ * has `scale` of its pixels for each of the photo's.
+ */
+ function stampDabs(
+ target: Target,
+ list: readonly Dab[],
+ feather: number,
+ pass: typeof cover,
+ origin: Point = [0, 0],
+ scale: Point = [1, 1],
+ ) {
+ const [width, height] = target.size;
+ let reached: Rect | undefined;
+ for (let start = 0; start < list.length; start += chunk) {
+ const batch = list.slice(start, start + chunk);
+ let left = width;
+ let top = height;
+ let right = 0;
+ let bottom = 0;
+ const data = new Float32Array(batch.length * 4);
+ batch.forEach(([x, y, size, alpha], i) => {
+ const center = [(x - origin[0]) * scale[0], (y - origin[1]) * scale[1]];
+ const radius = size * scale[0];
+ data.set([center[0], center[1], radius, alpha], i * 4);
+ left = Math.min(left, center[0] - radius);
+ top = Math.min(top, center[1] - radius);
+ right = Math.max(right, center[0] + radius);
+ bottom = Math.max(bottom, center[1] + radius);
+ });
+ const x = Math.max(0, Math.floor(left) - 1);
+ const y = Math.max(0, Math.floor(top) - 1);
+ const w = Math.min(width, Math.ceil(right) + 1) - x;
+ const h = Math.min(height, Math.ceil(bottom) + 1) - y;
+ if (w <= 0 || h <= 0) {
+ continue;
+ }
+ dabs.write(data);
+ pass.set({ params: { count: batch.length, feather } });
+ // One frame per chunk: buffer and uniform writes land in queue order, before the pass that reads them.
+ frame(gpu, (frame) =>
+ frame.pass({ target, clear: false, scissor: [x, y, w, h] }, pass),
+ );
+ stamped += batch.length;
+ reached = union(reached, [x, y, w, h]);
+ }
+ return reached;
+ }
+
+ function reserveBuffer(size: Size) {
+ reserved = true;
+ if (buffer && (buffer.size[0] !== size[0] || buffer.size[1] !== size[1])) {
+ buffer.color.dispose();
+ buffer = undefined;
+ open = undefined;
+ }
+ buffer ??= target(gpu, {
+ size: [...size],
+ format: "r16float",
+ clearColor: [0, 0, 0, 0],
+ });
+ return buffer;
+ }
+
+ /** Lays the open stroke's coverage over `target` within `rect`, in its color or erasing. */
+ function lay(target: Target, rect: Rect) {
+ if (!open || !buffer) {
+ return;
+ }
+ const { stroke } = open;
+ const pass = (stroke.mode === "paint" ? layOver : layOut).set({
+ coverage: buffer.color,
+ params: { color: "color" in stroke ? parseColor(stroke.color) : white },
+ });
+ frame(gpu, (frame) =>
+ frame.pass({ target, clear: false, scissor: rect }, pass),
+ );
+ }
+
+ /** Forgets the open stroke, emptying the buffer where it reached; clearing it all would write the whole photo. */
+ function forget() {
+ const bounds = open?.bounds;
+ const emptied = buffer;
+ open = undefined;
+ if (bounds && emptied) {
+ frame(gpu, (frame) =>
+ frame.pass({ target: emptied, clear: false, scissor: bounds }, empty),
+ );
+ }
+ }
+
+ function close() {
+ if (open?.bounds) {
+ lay(open.raster, open.bounds);
+ }
+ forget();
+ }
+
+ return {
+ buffer: () => buffer,
+ clear(target: Target) {
+ frame(gpu, (frame) => frame.pass({ target, clear: true }, () => {}));
+ },
+ /**
+ * Draws `raster`'s strokes from `from` on through the buffer, at `scale` raster pixels per photo
+ * pixel, going on with that first one where `walk` stopped. The last stays open, so it can grow.
+ * Returns where its walk stopped, whether it opened now, and where the buffer changed.
+ */
+ draw(
+ raster: Target,
+ strokes: readonly Stroke[],
+ from: number,
+ walk: DabWalk | undefined,
+ scale: Point,
+ ) {
+ const target = reserveBuffer(raster.size);
+ let last = walk;
+ let opened = false;
+ let changed: Rect | undefined;
+ for (let i = from; i < strokes.length; i++) {
+ const stroke = strokes[i];
+ if (open?.raster !== raster || open.index !== i) {
+ close();
+ opened = true;
+ }
+ const current = open ?? { raster, index: i, stroke };
+ const { dabs, walk } = walkDabs(stroke, i === from ? last : undefined);
+ last = walk;
+ const reached = stampDabs(
+ target,
+ dabs,
+ stroke.feather,
+ cover,
+ [0, 0],
+ scale,
+ );
+ changed = union(changed, reached);
+ open = { ...current, stroke, bounds: union(current.bounds, reached) };
+ }
+ return { walk: last, opened, changed };
+ },
+ /**
+ * Stamps a stroke straight into `raster`, which sits at `origin` in the photo, going on where `walk`
+ * stopped; returns where this walk stops. For hard strokes at full flow, which round the same either way.
+ */
+ stamp(
+ raster: Target,
+ stroke: Stroke,
+ walk: DabWalk | undefined,
+ origin: Point,
+ ) {
+ const walked = walkDabs(stroke, walk);
+ const pass = stroke.mode === "paint" ? cover : uncover;
+ stampDabs(raster, walked.dabs, stroke.feather, pass, origin);
+ return walked.walk;
+ },
+ /** Whether `raster`'s last stroke is open, its coverage in the buffer rather than the raster. */
+ isOpen: (raster: Target) => open?.raster === raster,
+ /** Lays the open stroke into its raster and empties the buffer. */
+ close,
+ /** Drops `raster`'s open stroke, for a raster about to be drawn again or freed. */
+ discard(raster: Target) {
+ if (open?.raster === raster) {
+ forget();
+ }
+ },
+ /** Copies `raster` into `view` within `rect`, then lays the raster's open stroke over the copy there. */
+ resolve(view: Target, raster: Target, rect: Rect) {
+ const pass = copy.set({
+ source: raster.color,
+ params: { offset: [0, 0] },
+ });
+ frame(gpu, (frame) =>
+ frame.pass({ target: view, clear: false, scissor: rect }, pass),
+ );
+ if (open?.raster === raster) {
+ lay(view, rect);
+ }
+ },
+ /** Frees the buffer once no render reserved it since the last sweep and no stroke waits in it. */
+ sweep() {
+ if (!reserved && !open) {
+ buffer?.color.dispose();
+ buffer = undefined;
+ }
+ reserved = false;
+ },
+ stamped: () => stamped,
+ inspect() {
+ return buffer
+ ? [
+ {
+ id: "stroke buffer",
+ size: [...buffer.size],
+ format: buffer.format,
+ },
+ ]
+ : [];
+ },
+ dispose() {
+ buffer?.color.dispose();
+ buffer = undefined;
+ open = undefined;
+ dabs.dispose();
+ },
+ };
+}
diff --git a/src/core/renderer/strokes/lay.wgsl b/src/core/renderer/strokes/lay.wgsl
new file mode 100644
index 0000000..371f3df
--- /dev/null
+++ b/src/core/renderer/strokes/lay.wgsl
@@ -0,0 +1,12 @@
+// Lays a paint stroke's coverage over its raster in the stroke's color, premultiplied, or erases by it,
+// in one pass, so the stroke rounds to the raster's 8 bits once rather than at every dab.
+struct Params {
+ color: vec3f,
+}
+@group(0) @binding(0) var coverage: texture_2d<f32>;
+@group(0) @binding(1) var<uniform> params: Params;
+
+@fragment fn fs_main(@builtin(position) position: vec4f) -> @location(0) vec4f {
+ let value = textureLoad(coverage, vec2i(position.xy), 0).r;
+ return vec4f(params.color * value, value);
+}
diff --git a/src/core/renderer/mask/strokes.wgsl b/src/core/renderer/strokes/stamp.wgsl
similarity index 84%
rename from src/core/renderer/mask/strokes.wgsl
rename to src/core/renderer/strokes/stamp.wgsl
index f329939..c20060b 100644
--- a/src/core/renderer/mask/strokes.wgsl
+++ b/src/core/renderer/strokes/stamp.wgsl
@@ -1,5 +1,5 @@
// Accumulates a chunk of dabs like successive layers of paint: each dab covers its share of what is left.
-// The pass blends the result into the mask, so chunks and strokes compose exactly.
+// The pass blends the coverage into its target, so chunks and strokes compose exactly.
struct Params {
count: u32,
feather: f32,
@@ -17,5 +17,5 @@ struct Params {
let weight = 1.0 - smoothstep(inner, radius, length(position.xy - dab.xy));
remaining *= 1.0 - dab.w * weight;
}
- return vec4f(1.0 - remaining, 0.0, 0.0, 1.0);
+ return vec4f(1.0 - remaining);
}
diff --git a/src/env.d.ts b/src/env.d.ts
index 01df2c6..33b6bca 100644
--- a/src/env.d.ts
+++ b/src/env.d.ts
@@ -1,2 +1,7 @@
/// <reference types="vite/client" />
/// <reference types="@vgpu/wgsl/wgsl-types" />
+
+/** Chromium's screen color picker; other browsers lack it. */
+interface Window {
+ EyeDropper?: new () => { open(): Promise<{ sRGBHex: string }> };
+}
diff --git a/src/features/adjustments/prepare.wgsl b/src/features/adjustments/prepare.wgsl
index 42df163..de93cb2 100644
--- a/src/features/adjustments/prepare.wgsl
+++ b/src/features/adjustments/prepare.wgsl
@@ -1,5 +1,3 @@
-import { luminance } from "../../core/image/color.wgsl";
-
export struct Adjustments {
exposure: f32,
incrementalTemperature: f32,
@@ -13,26 +11,9 @@ export struct Adjustments {
saturation: f32,
}
-// Source preparation in linear Rec.2020; retain the calibrated constants.
-
-// Fitted exposure of a gray level. Light above 1.0 is headroom and scales linearly with the stops.
-fn exposeGray(light: f32, stops: f32) -> f32 {
- let bounded = min(light, 1.0);
- let headroom = (light - bounded) * exp2(stops);
- if stops < 0.0 {
- return headroom + exp2(stops * 1.09) * pow(bounded, exp2(-stops * 0.14));
- }
- let gain = mix(vec2f(1.11, -0.11) * min(stops, 1.0), vec2f(4.05, -0.63), max(stops - 1.0, 0.0) / 4.0);
- return headroom + 1.0 - pow(1.0 - pow(bounded, exp2(gain.y)), exp2(gain.x));
-}
-
-// Every channel scales by the gain of the pixel's luminance, so hue holds at any exposure.
-// Negatives, from wide-gamut sources or noise below black, clip here.
+// Exposure scales linear light, including values outside the display range, so opposite stops cancel.
export fn adjustExposure(color: vec3f, stops: f32) -> vec3f {
- let clipped = max(color, vec3f(0.0));
- let light = luminance(clipped);
- if light <= 0.0 { return clipped; }
- return clipped * (exposeGray(light, stops) / light);
+ return color * exp2(stops);
}
fn adjustWhiteBalance(color: vec3f, temperature: f32, tint: f32) -> vec3f {
diff --git a/src/features/fill/controls.tsx b/src/features/fill/controls.tsx
index 1620e70..0fb2c00 100644
--- a/src/features/fill/controls.tsx
+++ b/src/features/fill/controls.tsx
@@ -2,8 +2,8 @@ import { Select } from "@roprgm/ui/select";
import { useEffect, useRef } from "react";
import { useDocument } from "@/components/editor/session";
import type { Fill } from "@/core/document";
+import { blends } from "@/core/image/blend";
import { setFill } from "./edits";
-import { blends } from "./model";
export function FillControls({ id, fill }: { id: string; fill: Fill }) {
const document = useDocument();
diff --git a/src/features/fill/fill.wgsl b/src/features/fill/fill.wgsl
index 9c4df34..8e2015c 100644
--- a/src/features/fill/fill.wgsl
+++ b/src/features/fill/fill.wgsl
@@ -1,9 +1,9 @@
import { linearToSrgb3, srgbToLinear3 } from "@vgpu/wgsl-std/color";
+import { blendColors } from "../../core/image/blend.wgsl";
import { srgbToRec2020 } from "../../core/image/color.wgsl";
-// Photoshop's blend formulas on encoded values: the working RGB and the sRGB color both take the
-// sRGB transfer first, so Multiply, Overlay and the rest look as they do in Photoshop. HDR input
-// is clamped where the fill applies.
+// The working RGB and the sRGB color both take the sRGB transfer before blending, as in Photoshop.
+// HDR input is clamped where the fill applies.
struct Params {
color: vec3f,
blend: u32,
@@ -11,54 +11,9 @@ struct Params {
@group(0) @binding(0) var source: texture_2d<f32>;
@group(0) @binding(1) var<uniform> params: Params;
-fn lum(color: vec3f) -> f32 {
- return dot(color, vec3f(0.3, 0.59, 0.11));
-}
-
-fn clipColor(color: vec3f) -> vec3f {
- let l = lum(color);
- let low = min(color.r, min(color.g, color.b));
- let high = max(color.r, max(color.g, color.b));
- var result = color;
- if (low < 0.0) {
- result = l + (result - l) * l / (l - low);
- }
- if (high > 1.0) {
- result = l + (result - l) * (1.0 - l) / (high - l);
- }
- return result;
-}
-
-fn setLum(color: vec3f, l: f32) -> vec3f {
- return clipColor(color + (l - lum(color)));
-}
-
-fn softLight(base: vec3f, blend: vec3f) -> vec3f {
- let d = select(sqrt(base), ((16.0 * base - 12.0) * base + 4.0) * base, base <= vec3f(0.25));
- return select(
- base + (2.0 * blend - 1.0) * (d - base),
- base - (1.0 - 2.0 * blend) * base * (1.0 - base),
- blend <= vec3f(0.5),
- );
-}
-
-fn blend(base: vec3f, blend: vec3f) -> vec3f {
- switch (params.blend) {
- case 1u: { return base * blend; }
- case 2u: { return base + blend - base * blend; }
- case 3u: {
- return select(1.0 - 2.0 * (1.0 - base) * (1.0 - blend), 2.0 * base * blend, base <= vec3f(0.5));
- }
- case 4u: { return softLight(base, blend); }
- case 5u: { return setLum(blend, lum(base)); }
- case 6u: { return setLum(base, lum(blend)); }
- default: { return blend; }
- }
-}
-
@fragment fn fs_main(@builtin(position) position: vec4f) -> @location(0) vec4f {
let input = textureLoad(source, vec2i(position.xy), 0);
let base = linearToSrgb3(clamp(input.rgb, vec3f(0.0), vec3f(1.0)));
let paint = linearToSrgb3(clamp(srgbToRec2020 * srgbToLinear3(params.color), vec3f(0.0), vec3f(1.0)));
- return vec4f(srgbToLinear3(clamp(blend(base, paint), vec3f(0.0), vec3f(1.0))), input.a);
+ return vec4f(srgbToLinear3(clamp(blendColors(base, paint, params.blend), vec3f(0.0), vec3f(1.0))), input.a);
}
diff --git a/src/features/fill/model.ts b/src/features/fill/model.ts
index 3eb69a1..8202e20 100644
--- a/src/features/fill/model.ts
+++ b/src/features/fill/model.ts
@@ -1,15 +1,7 @@
import { z } from "zod/mini";
-import type { Blend, Fill } from "@/core/document";
-
-export const blends: readonly (readonly [Blend, string])[] = [
- ["normal", "Normal"],
- ["multiply", "Multiply"],
- ["screen", "Screen"],
- ["overlay", "Overlay"],
- ["soft-light", "Soft Light"],
- ["color", "Color"],
- ["luminosity", "Luminosity"],
-];
+import type { Fill } from "@/core/document";
+import { blendSchema } from "@/core/image/blend";
+import { hexColor } from "@/lib/parse";
export const defaultFill: Readonly<Fill> = {
color: "#f0763c",
@@ -17,13 +9,6 @@ export const defaultFill: Readonly<Fill> = {
};
export const fillSchema = z.object({
- color: z.string().check(z.regex(/^#[0-9a-f]{6}$/i), z.toLowerCase()),
- blend: z.enum(blends.map(([blend]) => blend)),
+ color: hexColor,
+ blend: blendSchema,
}) satisfies z.ZodMiniType<Fill>;
-
-/** The sRGB channels of a `#rrggbb` color, 0 to 1. */
-export function parseColor(color: string): [number, number, number] {
- const channel = (offset: number) =>
- Number.parseInt(color.slice(offset, offset + 2), 16) / 255;
- return [channel(1), channel(3), channel(5)];
-}
diff --git a/src/features/fill/pass.ts b/src/features/fill/pass.ts
index 9af2568..f161807 100644
--- a/src/features/fill/pass.ts
+++ b/src/features/fill/pass.ts
@@ -1,14 +1,14 @@
import type { Fill } from "@/core/document";
+import { blendIndex, parseColor } from "@/core/image/blend";
import { node } from "@/core/renderer";
import shader from "./fill.wgsl";
-import { blends, parseColor } from "./model";
export function fill(settings: Fill, name = "fill") {
return node(name, shader, {
set: {
params: {
color: parseColor(settings.color),
- blend: blends.findIndex(([blend]) => blend === settings.blend),
+ blend: blendIndex(settings.blend),
},
},
});
diff --git a/src/features/heal/heal.wgsl b/src/features/heal/heal.wgsl
index 6837b32..b21a700 100644
--- a/src/features/heal/heal.wgsl
+++ b/src/features/heal/heal.wgsl
@@ -7,6 +7,9 @@ struct Params {
mode: u32,
feather: f32,
opacity: f32,
+ // The patch's coverage covers only the tiles its stroke reaches, from this corner.
+ coverageOrigin: vec2f,
+ coverageSize: vec2f,
}
@group(0) @binding(0) var source: texture_2d<f32>;
@group(0) @binding(1) var coverage: texture_2d<f32>;
@@ -18,7 +21,7 @@ fn color(p: vec2f) -> vec4f {
return textureSampleLevel(source, linearSampler, p / params.dimensions, 0.0);
}
fn mask(p: vec2f) -> f32 {
- return textureSampleLevel(coverage, linearSampler, p / params.dimensions, 0.0).r;
+ return textureSampleLevel(coverage, linearSampler, (p - params.coverageOrigin) / params.coverageSize, 0.0).r;
}
fn difference(p: vec2f) -> vec3f {
// A smooth log gain matches color without subtracting bright-boundary differences
@@ -67,7 +70,7 @@ fn patchCoverage(
if (any(p < params.origin) || any(p > params.origin + params.extent)) {
return original;
}
- let amount = patchCoverage(coverage, linearSampler, p, params.dimensions, params.feather) * params.opacity;
+ let amount = patchCoverage(coverage, linearSampler, p - params.coverageOrigin, params.coverageSize, params.feather) * params.opacity;
let donor = p + params.offset;
if (amount == 0.0 || any(donor < vec2f(0.5)) || any(donor > params.dimensions - 0.5)) {
return original;
diff --git a/src/features/heal/pass.ts b/src/features/heal/pass.ts
index ed2f628..2bacdd1 100644
--- a/src/features/heal/pass.ts
+++ b/src/features/heal/pass.ts
@@ -3,18 +3,18 @@ import {
type Composition,
merge,
node,
+ type PatchInput,
type RenderImage,
- type RenderInput,
sourceSize,
} from "@/core/renderer";
import shader from "./heal.wgsl";
import { patchBounds } from "./model";
-type HealComposition = Pick<Composition, "brush" | "inputId" | "retain">;
+type HealComposition = Pick<Composition, "patch" | "inputId" | "retain">;
function healPatch(
source: RenderImage,
- coverage: RenderInput,
+ { coverage, origin: coverageOrigin }: PatchInput,
patch: HealPatch,
name: string,
) {
@@ -28,6 +28,8 @@ function healPatch(
extent,
dimensions,
offset: patch.offset,
+ coverageOrigin,
+ coverageSize: coverage.size,
};
let correction = source;
// Coarse-to-fine harmonic extension of the destination/donor log color ratio.
@@ -92,7 +94,7 @@ export function heal(
if (patch.id === composition.inputId) inspected = image;
const id = `${name}/${patch.id}`;
if (patch.offset[0] === 0 && patch.offset[1] === 0) continue;
- const coverage = composition.brush(id, [patch.stroke]);
+ const coverage = composition.patch(id, patch.stroke);
composition.retain(id);
image = healPatch(image, coverage, patch, id);
}
diff --git a/src/features/layers/brush-options.tsx b/src/features/layers/brush-options.tsx
index 091f2b2..fcfca0b 100644
--- a/src/features/layers/brush-options.tsx
+++ b/src/features/layers/brush-options.tsx
@@ -2,69 +2,95 @@ import { Chip } from "@roprgm/ui/chip";
import { Slider } from "@roprgm/ui/slider";
import { Tooltip, TooltipContent, TooltipTrigger } from "@roprgm/ui/tooltip";
import { cn } from "cn";
-import { useBrushTool } from "@/components/editor/brush-tool";
+import type { ReactNode } from "react";
+import {
+ type BrushMode,
+ useBrushParameters,
+ useBrushTool,
+} from "@/components/editor/brush-tool";
import { DockChips, DockControls } from "@/components/editor/dock";
-import type { Parameter } from "@/components/editor/parameter";
import { barSlider, useBarDensity } from "@/components/editor/toolbar-density";
+const targets: readonly (readonly [BrushMode, string, string])[] = [
+ ["color", "Color", "Paint colors on a paint layer"],
+ ["mask", "Mask", "Paint where a mask adjusts"],
+];
+
const modes = [
- ["paint", "Paint", "Add coverage", undefined],
- ["erase", "Erase", "Remove coverage", "Hold Alt"],
+ ["paint", "Paint", "Add paint", undefined],
+ ["erase", "Erase", "Remove paint", "Hold Alt"],
] as const;
-/** The next stroke's mode, size, edge, and flow, in the bar over the canvas or the dock. Alt shows on the Erase chip. */
-export function BrushOptions() {
- const { settings, erase, maxSize, setPreview, update } = useBrushTool();
+function Chips<T extends string>({
+ label,
+ menu,
+ items,
+ value,
+ onChange,
+}: {
+ label: string;
+ menu: boolean;
+ items: readonly (readonly [T, string, string, string?])[];
+ value: T;
+ onChange: (value: T) => void;
+}) {
+ return (
+ <fieldset
+ aria-label={label}
+ className="flex gap-0.5 rounded-full bg-hover p-0.5"
+ >
+ {items.map(([id, label, hint, shortcut]) => (
+ <Tooltip key={id}>
+ <TooltipTrigger
+ render={
+ <Chip
+ aria-pressed={id === value}
+ className={cn("h-6", menu && "flex-1 justify-center")}
+ onClick={() => onChange(id)}
+ >
+ {label}
+ </Chip>
+ }
+ />
+ <TooltipContent shortcut={shortcut}>{hint}</TooltipContent>
+ </Tooltip>
+ ))}
+ </fieldset>
+ );
+}
+
+/**
+ * What the brush paints, color or a mask, then the next stroke's mode, size, edge, and flow, in the bar
+ * over the canvas or the dock. `colors` follows the Color chip while it is chosen. Alt shows on the Erase chip.
+ */
+export function BrushOptions({ colors }: { colors?: ReactNode }) {
+ const { settings, erase, update } = useBrushTool();
+ const shownColors = settings.mode === "color" && colors;
+ const stroke = erase ? "erase" : "paint";
const density = useBarDensity();
const variant = barSlider(density);
// In the overflow menu the group spans the column, so the two modes share it evenly.
const menu = density === "menu";
- const parameters: Parameter[] = [
- {
- id: "size",
- label: "Size",
- value: settings.size,
- min: 1,
- max: maxSize,
- format: (value) => `${value}px`,
- valueWidth: `${maxSize}`.length,
- onEditingChange: setPreview,
- onChange: (size) => update({ size: Math.round(size) }),
- },
- {
- id: "feather",
- label: "Feather",
- value: Math.round(settings.feather * 100),
- min: 0,
- max: 100,
- defaultValue: 50,
- format: (value) => `${value}%`,
- valueWidth: 3,
- onEditingChange: setPreview,
- onChange: (value) => update({ feather: value / 100 }),
- },
- {
- id: "flow",
- label: "Flow",
- value: Math.round(settings.flow * 100),
- min: 1,
- max: 100,
- defaultValue: 100,
- format: (value) => `${value}%`,
- valueWidth: 3,
- onChange: (value) => update({ flow: value / 100 }),
- },
- ];
+ const parameters = useBrushParameters();
if (density === "dock") {
return (
<DockControls
header={
- <DockChips
- label="Brush mode"
- items={modes.map(([mode, label]) => [mode, label] as const)}
- value={erase ? "erase" : "paint"}
- onChange={(mode) => update({ erase: mode === "erase" })}
- />
+ <div className="flex min-w-0 items-center gap-2">
+ <DockChips
+ label="Brush paints"
+ items={targets.map(([mode, label]) => [mode, label] as const)}
+ value={settings.mode}
+ onChange={(mode) => update({ mode })}
+ />
+ {shownColors}
+ <DockChips
+ label="Brush mode"
+ items={modes.map(([mode, label]) => [mode, label] as const)}
+ value={stroke}
+ onChange={(mode) => update({ erase: mode === "erase" })}
+ />
+ </div>
}
parameters={parameters}
/>
@@ -72,27 +98,21 @@ export function BrushOptions() {
}
return (
<>
- <fieldset
- aria-label="Brush mode"
- className="flex gap-0.5 rounded-full bg-hover p-0.5"
- >
- {modes.map(([mode, label, hint, shortcut]) => (
- <Tooltip key={mode}>
- <TooltipTrigger
- render={
- <Chip
- aria-pressed={erase === (mode === "erase")}
- className={cn("h-6", menu && "flex-1 justify-center")}
- onClick={() => update({ erase: mode === "erase" })}
- >
- {label}
- </Chip>
- }
- />
- <TooltipContent shortcut={shortcut}>{hint}</TooltipContent>
- </Tooltip>
- ))}
- </fieldset>
+ <Chips
+ label="Brush paints"
+ menu={menu}
+ items={targets}
+ value={settings.mode}
+ onChange={(mode) => update({ mode })}
+ />
+ {shownColors}
+ <Chips
+ label="Brush mode"
+ menu={menu}
+ items={modes}
+ value={stroke}
+ onChange={(mode) => update({ erase: mode === "erase" })}
+ />
{parameters.map(({ id, ...parameter }) => (
<Slider key={id} {...parameter} variant={variant} />
))}
diff --git a/src/features/layers/brush-overlay.tsx b/src/features/layers/brush-overlay.tsx
deleted file mode 100644
index be4266a..0000000
--- a/src/features/layers/brush-overlay.tsx
+++ /dev/null
@@ -1,47 +0,0 @@
-import { useRef } from "react";
-import { BrushCanvas } from "@/components/editor/brush-canvas";
-import { useBrushTool } from "@/components/editor/brush-tool";
-import { useDocument } from "@/components/editor/session";
-import { useToolLayer } from "@/components/editor/tool-layer";
-import type { Layer, MaskLayer } from "@/core/document";
-import { extendStroke, paintStroke } from "./edits";
-import { useMaskTool } from "./mask-tool";
-
-function isBrushMask(layer: Layer): layer is MaskLayer {
- return layer.kind === "mask" && layer.mask.kind === "brush";
-}
-
-/** Keeps a brush mask selected; an untouched new mask disappears on leaving. */
-export function BrushOverlay() {
- const document = useDocument();
- const tool = useMaskTool();
- const brush = useBrushTool();
- const painting = useRef<string | undefined>(undefined);
- const selectedBrush = useToolLayer({
- accepts: isBrushMask,
- create: () => tool.create({ kind: "brush", strokes: [] }),
- leave: () => tool.edit(),
- // A chosen nesting starts a new brush even over a selected one.
- fresh: tool.pending?.shape === "brush",
- });
- return (
- <BrushCanvas
- label="Brush canvas"
- erase={brush.erase}
- onStart={(stroke) => {
- const id = selectedBrush()?.id;
- if (!id) return false;
- painting.current = id;
- paintStroke(document, id, stroke);
- return true;
- }}
- onExtend={(points) => {
- const id = painting.current;
- if (id) {
- extendStroke(document, id, points);
- }
- }}
- onDone={() => tool.edit()}
- />
- );
-}
diff --git a/src/features/layers/edits.ts b/src/features/layers/edits.ts
index 4852efd..60f8c87 100644
--- a/src/features/layers/edits.ts
+++ b/src/features/layers/edits.ts
@@ -53,6 +53,47 @@ export function validateDepth(layers: readonly Layer[]) {
}
}
+/**
+ * Each paint layer keeps an rgba8 raster at half the photo's resolution and each brush mask an r8
+ * one, so a photo holds a few of each.
+ */
+export const brushLimits = { color: 4, mask: 10 } as const;
+const brushLimitNames = { color: "paint layers", mask: "brush masks" };
+
+/** The most a photo holds of paint layers or of brush masks, as a sentence without its period. */
+export function brushLimit(mode: keyof typeof brushLimits) {
+ return `A photo holds up to ${brushLimits[mode]} ${brushLimitNames[mode]}`;
+}
+
+/** The paint layers and brush masks, submasks included, among `layers` and their children. */
+export function brushLayerCounts(layers: readonly Layer[]) {
+ const counts = { color: 0, mask: 0 };
+ for (const { layer } of walkLayers(layers)) {
+ if (layer.kind === "paint") {
+ counts.color++;
+ } else if (layer.kind === "mask" && layer.mask.kind === "brush") {
+ counts.mask++;
+ }
+ }
+ return counts;
+}
+
+/** Why `layers` are more than a photo holds, if they are. */
+export function brushExcess(layers: readonly Layer[]) {
+ const counts = brushLayerCounts(layers);
+ const over = (["color", "mask"] as const).find(
+ (mode) => counts[mode] > brushLimits[mode],
+ );
+ return over && `${brushLimit(over)}.`;
+}
+
+export function validateBrushLayers(layers: readonly Layer[]) {
+ const excess = brushExcess(layers);
+ if (excess) {
+ throw Error(excess);
+ }
+}
+
/** Above a sibling, inside a processing layer, or on top of the root stack. */
export type LayerPlacement = { above: string } | { inside: string };
@@ -100,6 +141,7 @@ export function addLayer(
layers.toSpliced(index ?? layers.length, 0, layer),
);
validateDepth(next.layers);
+ validateBrushLayers(next.layers);
document.history.commit();
document.edit(next);
document.selectLayer(layer.id);
@@ -120,19 +162,25 @@ export function setLayer(
export function setLayerMask(document: EditorDocument, id: string, mask: Mask) {
const next = parse(maskSchema, mask, "Invalid mask");
- editLayer(document, id, (layer) => {
+ if (next.kind === "brush" && next.raster !== undefined) {
+ // Settled pixels come from the document's own settling, never from outside.
+ document.resources.paint(next.raster);
+ }
+ const scene = updateLayer(document.scene.getState(), id, (layer) => {
if (layer.kind !== "mask") {
throw Error("Select a mask layer.");
}
return { ...layer, mask: next };
});
+ validateBrushLayers(scene.layers);
+ document.edit(scene);
}
function brushLayer(layer: Layer) {
if (layer.kind !== "mask" || layer.mask.kind !== "brush") {
throw Error("Select a brush mask.");
}
- return { layer, strokes: layer.mask.strokes };
+ return { layer, mask: layer.mask, strokes: layer.mask.strokes };
}
/** Starts a stroke on a brush mask; group it with the points that follow. */
@@ -143,11 +191,8 @@ export function paintStroke(
) {
const painted = parse(strokeSchema, stroke, "Invalid stroke");
editLayer(document, id, (item) => {
- const { layer, strokes } = brushLayer(item);
- return {
- ...layer,
- mask: { kind: "brush", strokes: [...strokes, painted] },
- };
+ const { layer, mask, strokes } = brushLayer(item);
+ return { ...layer, mask: { ...mask, strokes: [...strokes, painted] } };
});
}
@@ -159,7 +204,7 @@ export function extendStroke(
) {
const added = parse(strokePoints, points, "Invalid stroke points");
editLayer(document, id, (item) => {
- const { layer, strokes } = brushLayer(item);
+ const { layer, mask, strokes } = brushLayer(item);
const last = strokes.at(-1);
if (!last) {
throw Error("Start a stroke before extending it.");
@@ -167,7 +212,7 @@ export function extendStroke(
const stroke = { ...last, points: [...last.points, ...added] };
return {
...layer,
- mask: { kind: "brush", strokes: [...strokes.slice(0, -1), stroke] },
+ mask: { ...mask, strokes: [...strokes.slice(0, -1), stroke] },
};
});
}
@@ -202,6 +247,7 @@ export function duplicateLayer(document: EditorDocument, id: string) {
layers.toSpliced(layers.findIndex((item) => item.id === id) + 1, 0, layer),
);
validateDepth(next.layers);
+ validateBrushLayers(next.layers);
document.history.commit();
document.edit(next);
document.selectLayer(layer.id);
diff --git a/src/features/layers/menu.tsx b/src/features/layers/menu.tsx
index 5c50bf2..32a2bdf 100644
--- a/src/features/layers/menu.tsx
+++ b/src/features/layers/menu.tsx
@@ -17,7 +17,14 @@ import {
type MaskLayer,
type ProcessingLayer,
} from "@/core/document";
-import { deleteLayer, duplicateLayer, moveLayer } from "./edits";
+import {
+ brushExcess,
+ brushLayerCounts,
+ brushLimits,
+ deleteLayer,
+ duplicateLayer,
+ moveLayer,
+} from "./edits";
import { useMaskTool } from "./mask-tool";
/** Mounted only while the menu is open, so rows do not track siblings and containers. */
@@ -40,6 +47,7 @@ function LayerActionItems({
!findLayer([layer], item.id),
);
const bottom = !parent ? 1 : 0;
+ const duplicable = !brushExcess([...layers, layer]);
function moveOut() {
if (!parent) {
return;
@@ -91,7 +99,10 @@ function LayerActionItems({
</Submenu>
)}
<MenuSeparator />
- <MenuItem onClick={() => onSelect(duplicateLayer(document, layer.id))}>
+ <MenuItem
+ disabled={!duplicable}
+ onClick={() => onSelect(duplicateLayer(document, layer.id))}
+ >
Duplicate
</MenuItem>
<MenuItem onClick={() => deleteLayer(document, layer.id)}>
@@ -115,6 +126,9 @@ const nestings = [
function NestingItems({ layer }: { layer: MaskLayer }) {
const document = useDocument();
const tool = useMaskTool();
+ const full = useScene(
+ (scene) => brushLayerCounts(scene.layers).mask >= brushLimits.mask,
+ );
return nestings.map(([operation, label]) => (
<Submenu key={operation}>
<SubmenuTrigger>{label}</SubmenuTrigger>
@@ -122,6 +136,7 @@ function NestingItems({ layer }: { layer: MaskLayer }) {
{shapes.map(([shape, name]) => (
<MenuItem
key={shape}
+ disabled={shape === "brush" && full}
onClick={() => {
document.selectLayer(layer.id);
tool.add(layer.id, operation, shape);
diff --git a/src/features/layers/model.ts b/src/features/layers/model.ts
index 589b1be..784720b 100644
--- a/src/features/layers/model.ts
+++ b/src/features/layers/model.ts
@@ -21,7 +21,11 @@ export const maskSchema = z.discriminatedUnion("kind", [
angle: z.number(),
feather: unit,
}),
- z.object({ kind: z.literal("brush"), strokes: z.array(strokeSchema) }),
+ z.object({
+ kind: z.literal("brush"),
+ raster: z.optional(z.string().check(z.minLength(1))),
+ strokes: z.array(strokeSchema),
+ }),
]) satisfies z.ZodMiniType<Mask>;
export const maskOperation = z.enum([
diff --git a/src/features/layers/toolbar.tsx b/src/features/layers/toolbar.tsx
index f41f3b1..5e41bfb 100644
--- a/src/features/layers/toolbar.tsx
+++ b/src/features/layers/toolbar.tsx
@@ -27,6 +27,7 @@ function LayerOptions({ layer }: { layer: ProcessingLayer }) {
min={0}
max={100}
defaultValue={100}
+ origin={0}
format={(value) => `${value}%`}
valueWidth={3}
variant={barSlider(density)}
@@ -98,8 +99,9 @@ export function CanvasToolbar({ children }: { children?: ReactNode }) {
{...gesture}
// No wrapping, so running out of room overflows, which is what the steps above measure. A button or
// 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.
- className="absolute top-3 left-3 flex min-w-0 max-w-[calc(100%-1.5rem)] items-center gap-x-2.5 overflow-hidden whitespace-nowrap rounded-full surface-panel bg-level-4/80 p-1 backdrop-blur-sm [&>:first-child:not(button,fieldset)]:ml-2 [&>:last-child:not(button,fieldset)]:mr-1.5"
+ // 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"
>
<Density value={step === 0 ? "full" : "compact"}>
{inlineTool}
diff --git a/src/features/paint/colors.tsx b/src/features/paint/colors.tsx
new file mode 100644
index 0000000..b7d20f7
--- /dev/null
+++ b/src/features/paint/colors.tsx
@@ -0,0 +1,124 @@
+import { IconButton } from "@roprgm/ui/icon-button";
+import { Tooltip, TooltipContent, TooltipTrigger } from "@roprgm/ui/tooltip";
+import { cn } from "cn";
+import { defaultColors, useBrushTool } from "@/components/editor/brush-tool";
+import { EyedropperIcon } from "@/components/icons/eyedropper";
+import { SwapIcon } from "@/components/icons/swap";
+import { hexColor } from "@/lib/parse";
+
+/** The brush's primary and secondary colors, and the actions Photoshop keys to X, D, and I. */
+export function usePaintColors() {
+ const { settings, update } = useBrushTool();
+ const [primary, secondary] = settings.colors;
+ const EyeDropper = window.EyeDropper;
+ return {
+ primary,
+ secondary,
+ setPrimary: (color: string) => update({ colors: [color, secondary] }),
+ setSecondary: (color: string) => update({ colors: [primary, color] }),
+ swap: () => update({ colors: [secondary, primary] }),
+ reset: () => update({ colors: defaultColors }),
+ /** Picks the primary color from anywhere on screen, where the browser offers it. */
+ pick:
+ EyeDropper &&
+ (async () => {
+ const picked = await new EyeDropper().open().catch(() => undefined);
+ const color = hexColor.safeParse(picked?.sRGBHex);
+ if (color.success) {
+ update({ colors: [color.data, secondary] });
+ }
+ }),
+ };
+}
+
+type Side = "top" | "right";
+
+function Swatch({
+ label,
+ value,
+ side,
+ className,
+ onChange,
+}: {
+ label: string;
+ value: string;
+ side: Side;
+ className: string;
+ onChange: (color: string) => void;
+}) {
+ return (
+ <Tooltip>
+ <TooltipTrigger
+ render={
+ <label
+ className={cn(
+ "absolute size-4.5 cursor-pointer rounded-sm surface-raised focus-ring",
+ className,
+ )}
+ style={{ backgroundColor: value }}
+ >
+ <input
+ type="color"
+ aria-label={label}
+ value={value}
+ className="sr-only"
+ onChange={(event) => onChange(event.currentTarget.value)}
+ />
+ </label>
+ }
+ />
+ <TooltipContent side={side}>{label}</TooltipContent>
+ </Tooltip>
+ );
+}
+
+/**
+ * Two raised squares, as in Photoshop: the primary over the secondary, with swap and pick after them,
+ * in a row or, in the tool rail, a column.
+ */
+export function PaintColors({ vertical = false }: { vertical?: boolean }) {
+ const colors = usePaintColors();
+ const side = vertical ? "right" : "top";
+ return (
+ <div className={cn("flex items-center gap-0.5", vertical && "flex-col")}>
+ <div className="relative m-1 size-7 shrink-0">
+ <Swatch
+ label="Secondary color"
+ value={colors.secondary}
+ side={side}
+ className="right-0 bottom-0"
+ onChange={colors.setSecondary}
+ />
+ <Swatch
+ label="Primary color"
+ value={colors.primary}
+ side={side}
+ className="top-0 left-0"
+ onChange={colors.setPrimary}
+ />
+ </div>
+ <IconButton
+ label="Swap colors"
+ shortcut="X"
+ side={side}
+ size="icon-sm"
+ className="rounded-full"
+ onClick={colors.swap}
+ >
+ <SwapIcon className="size-4" />
+ </IconButton>
+ {colors.pick && (
+ <IconButton
+ label="Pick a color"
+ shortcut="I"
+ side={side}
+ size="icon-sm"
+ className="rounded-full"
+ onClick={colors.pick}
+ >
+ <EyedropperIcon className="size-4" />
+ </IconButton>
+ )}
+ </div>
+ );
+}
diff --git a/src/features/paint/controls.tsx b/src/features/paint/controls.tsx
new file mode 100644
index 0000000..780e39b
--- /dev/null
+++ b/src/features/paint/controls.tsx
@@ -0,0 +1,24 @@
+import { Select } from "@roprgm/ui/select";
+import { useDocument } from "@/components/editor/session";
+import type { PaintLayer } from "@/core/document";
+import { blends } from "@/core/image/blend";
+import { setPaintBlend } from "./edits";
+
+export function PaintControls({ layer }: { layer: PaintLayer }) {
+ const document = useDocument();
+ return (
+ <section className="flex items-center justify-between p-3.5 text-muted">
+ Blend
+ <Select
+ raised
+ aria-label="Blend"
+ value={layer.blend}
+ items={blends.map(([value, label]) => ({ value, label }))}
+ className="w-28"
+ onValueChange={(blend) =>
+ blend && setPaintBlend(document, layer.id, blend)
+ }
+ />
+ </section>
+ );
+}
diff --git a/src/features/paint/edits.ts b/src/features/paint/edits.ts
new file mode 100644
index 0000000..6e7e7c1
--- /dev/null
+++ b/src/features/paint/edits.ts
@@ -0,0 +1,58 @@
+import {
+ type Blend,
+ type EditorDocument,
+ editLayer,
+ type Layer,
+ type PaintStroke,
+ type StrokePoint,
+} from "@/core/document";
+import { paintStrokeSchema, strokePoints } from "@/core/document/brush";
+import { parse } from "@/lib/parse";
+import { paintShape } from "./model";
+
+function paintLayer(layer: Layer) {
+ if (layer.kind !== "paint") {
+ throw Error("Select a paint layer.");
+ }
+ return layer;
+}
+
+/** Starts a stroke on a paint layer; group it with the points that follow. */
+export function addPaintStroke(
+ document: EditorDocument,
+ id: string,
+ stroke: PaintStroke,
+) {
+ const painted = parse(paintStrokeSchema, stroke, "Invalid stroke");
+ editLayer(document, id, (item) => {
+ const layer = paintLayer(item);
+ return { ...layer, strokes: [...layer.strokes, painted] };
+ });
+}
+
+/** Appends points to the layer's last stroke, keeping earlier points so rendering only stamps the new ones. */
+export function extendPaintStroke(
+ document: EditorDocument,
+ id: string,
+ points: readonly StrokePoint[],
+) {
+ const added = parse(strokePoints, points, "Invalid stroke points");
+ editLayer(document, id, (item) => {
+ const layer = paintLayer(item);
+ const last = layer.strokes.at(-1);
+ if (!last) {
+ throw Error("Start a stroke before extending it.");
+ }
+ const stroke = { ...last, points: [...last.points, ...added] };
+ return { ...layer, strokes: [...layer.strokes.slice(0, -1), stroke] };
+ });
+}
+
+export function setPaintBlend(
+ document: EditorDocument,
+ id: string,
+ blend: Blend,
+) {
+ const value = parse(paintShape.blend, blend, "Invalid blend");
+ editLayer(document, id, (layer) => ({ ...paintLayer(layer), blend: value }));
+}
diff --git a/src/features/paint/model.ts b/src/features/paint/model.ts
new file mode 100644
index 0000000..b93205f
--- /dev/null
+++ b/src/features/paint/model.ts
@@ -0,0 +1,10 @@
+import { z } from "zod/mini";
+import { paintStrokeSchema } from "@/core/document/brush";
+import { blendSchema } from "@/core/image/blend";
+
+/** A paint layer's own fields, as edits and scene files check them. */
+export const paintShape = {
+ blend: blendSchema,
+ raster: z.optional(z.string().check(z.minLength(1))),
+ strokes: z.array(paintStrokeSchema),
+};
diff --git a/src/features/paint/paint.wgsl b/src/features/paint/paint.wgsl
new file mode 100644
index 0000000..fbe762a
--- /dev/null
+++ b/src/features/paint/paint.wgsl
@@ -0,0 +1,37 @@
+import { linearToSrgb3, srgbToLinear3 } from "@vgpu/wgsl-std/color";
+import { blendColors } from "../../core/image/blend.wgsl";
+import { srgbToRec2020 } from "../../core/image/color.wgsl";
+
+// Lays premultiplied sRGB paint over the image with a Photoshop blend, mixing by the paint's alpha on
+// encoded values as Photoshop does. Unpainted pixels pass through, and so does HDR headroom under thin
+// paint. The raster covers the source, sampled by position so any proxy resolution reads the same paint.
+// The layer's open stroke, not in the raster yet, lays over it from the stroke buffer's coverage.
+struct Params {
+ blend: u32,
+ // 0 when the stroke buffer holds no stroke of this layer, 1 when it paints `strokeColor`, 2 when it erases.
+ stroke: u32,
+ strokeColor: vec3f,
+}
+@group(0) @binding(0) var source: texture_2d<f32>;
+@group(0) @binding(1) var paint: texture_2d<f32>;
+@group(0) @binding(2) var stroke: texture_2d<f32>;
+@group(0) @binding(3) var paintSampler: sampler;
+@group(0) @binding(4) var<uniform> params: Params;
+
+@fragment fn fs_main(@builtin(position) position: vec4f) -> @location(0) vec4f {
+ let input = textureLoad(source, vec2i(position.xy), 0);
+ let uv = position.xy / vec2f(textureDimensions(source));
+ var painted = textureSampleLevel(paint, paintSampler, uv, 0.0);
+ if (params.stroke != 0u) {
+ let coverage = textureSampleLevel(stroke, paintSampler, uv, 0.0).r;
+ let laid = select(vec4f(0.0), vec4f(params.strokeColor, 1.0), params.stroke == 1u);
+ painted = mix(painted, laid, coverage);
+ }
+ if (painted.a <= 0.0) {
+ return input;
+ }
+ let color = linearToSrgb3(clamp(srgbToRec2020 * srgbToLinear3(painted.rgb / painted.a), vec3f(0.0), vec3f(1.0)));
+ let base = linearToSrgb3(max(input.rgb, vec3f(0.0)));
+ let blended = clamp(blendColors(min(base, vec3f(1.0)), color, params.blend), vec3f(0.0), vec3f(1.0));
+ return vec4f(srgbToLinear3(mix(base, blended, painted.a)), input.a);
+}
diff --git a/src/features/paint/pass.ts b/src/features/paint/pass.ts
new file mode 100644
index 0000000..230e1b9
--- /dev/null
+++ b/src/features/paint/pass.ts
@@ -0,0 +1,37 @@
+import type { Blend } from "@/core/document";
+import { blendIndex, parseColor } from "@/core/image/blend";
+import {
+ merge,
+ node,
+ type PaintInput,
+ type RenderImage,
+} from "@/core/renderer";
+import shader from "./paint.wgsl";
+
+const strokeModes = { paint: 1, erase: 2 };
+
+/** The image under a paint layer's raster and open stroke; a layer that paints nothing leaves it as it is. */
+export function paint(
+ below: RenderImage,
+ painted: PaintInput | undefined,
+ blend: Blend,
+ name: string,
+) {
+ if (!painted) {
+ return below;
+ }
+ const { raster, buffer, stroke } = painted;
+ return merge(
+ { source: below, paint: raster, stroke: buffer },
+ node(name, shader, {
+ samplers: { paintSampler: { minFilter: "linear", magFilter: "linear" } },
+ set: {
+ params: {
+ blend: blendIndex(blend),
+ stroke: stroke ? strokeModes[stroke.mode] : 0,
+ strokeColor: parseColor(stroke?.color ?? "#000000"),
+ },
+ },
+ }),
+ );
+}
diff --git a/src/features/paint/settle.tsx b/src/features/paint/settle.tsx
new file mode 100644
index 0000000..1e7798b
--- /dev/null
+++ b/src/features/paint/settle.tsx
@@ -0,0 +1,112 @@
+import { useEffect } from "react";
+import { useRenderer } from "@/components/editor/pipeline";
+import { useDocument } from "@/components/editor/session";
+import {
+ type EditorDocument,
+ findLayer,
+ paintingOf,
+ type Scene,
+ updateLayer,
+ walkLayers,
+} from "@/core/document";
+
+/** Strokes a painting keeps before they settle into pixels, which bounds what undo draws again. */
+export const settleAt = 100;
+
+type Renderer = ReturnType<typeof useRenderer>;
+
+/**
+ * Settles a paint layer's or brush mask's strokes into the pixels its raster reads back, unless undo
+ * changed the layer meanwhile, and returns whether it did. The scene changes in place: what it shows is
+ * the same, and undo still reaches every stroke through the history's earlier scenes and the pixels they
+ * name.
+ */
+export function settlePaint(
+ document: EditorDocument,
+ renderer: Pick<Renderer, "settle">,
+ id: string,
+) {
+ return renderer.settle(id, (settled) => {
+ if (document.closed) {
+ return undefined;
+ }
+ const scene = document.scene.getState();
+ const layer = findLayer(scene.layers, id);
+ const painting = layer && paintingOf(layer);
+ if (
+ painting?.raster !== settled.base ||
+ settled.strokes.some((stroke, i) => stroke !== painting?.strokes[i])
+ ) {
+ return undefined;
+ }
+ const raster = document.resources.addPaint(settled.pixels);
+ const count = settled.strokes.length;
+ document.replace(
+ updateLayer(scene, id, (item) => {
+ if (item.kind === "paint") {
+ return { ...item, raster, strokes: item.strokes.slice(count) };
+ }
+ if (item.kind === "mask" && item.mask.kind === "brush") {
+ const strokes = item.mask.strokes.slice(count);
+ return { ...item, mask: { ...item.mask, raster, strokes } };
+ }
+ return item;
+ }),
+ );
+ return raster;
+ });
+}
+
+function crowded(scene: Scene) {
+ for (const { layer } of walkLayers(scene.layers)) {
+ if ((paintingOf(layer)?.strokes.length ?? 0) >= settleAt) {
+ return layer.id;
+ }
+ }
+ return undefined;
+}
+
+/**
+ * Settles a painting, one at a time, once it has `settleAt` strokes and no gesture is open. It checks
+ * after each render, when the painting's raster holds the strokes the scene has, and at once after a
+ * settle, for another crowded painting. A settle that finds nothing to do waits for the next render
+ * instead, and nothing checks after the stop, since the renderer may be gone. Returns the stop.
+ */
+export function watchSettling(
+ document: EditorDocument,
+ renderer: Pick<Renderer, "settle" | "subscribe">,
+) {
+ let active = true;
+ let running = false;
+ async function settle(id: string) {
+ running = true;
+ try {
+ return await settlePaint(document, renderer, id);
+ } finally {
+ running = false;
+ }
+ }
+ function check() {
+ if (!active || running || document.history.status.getState().editing) {
+ return;
+ }
+ const id = crowded(document.scene.getState());
+ if (id) {
+ void settle(id).then((settled) => settled && check());
+ }
+ }
+ const unsubscribe = renderer.subscribe(check);
+ const detach = document.history.status.subscribe(check);
+ return () => {
+ active = false;
+ unsubscribe();
+ detach();
+ };
+}
+
+export function PaintSettling() {
+ const document = useDocument();
+ const renderer = useRenderer();
+ useEffect(() => watchSettling(document, renderer), [document, renderer]);
+ return null;
+}
diff --git a/src/hooks/use-shortcuts.ts b/src/hooks/use-shortcuts.ts
index ddad013..57c5582 100644
--- a/src/hooks/use-shortcuts.ts
+++ b/src/hooks/use-shortcuts.ts
@@ -1,6 +1,15 @@
import { useEffect, useEffectEvent } from "react";
import { isTyping, typingFields } from "@/lib/dom";
+/** Keys whose shifted character depends on the layout, named by the key itself so Shift combines with them. */
+const shiftable: Record<string, string> = {
+ BracketLeft: "[",
+ BracketRight: "]",
+ ...Object.fromEntries(
+ Array.from({ length: 10 }, (_, digit) => [`Digit${digit}`, `${digit}`]),
+ ),
+};
+
/** View-scoped keyboard actions. Text fields opt in explicitly. */
export function useShortcuts(
actions: Record<string, () => void>,
@@ -22,7 +31,11 @@ export function useShortcuts(
if (!inputs && isTyping(event.target, owned)) {
return;
}
- const action = actions[`${modifier}${shift}${event.key.toLowerCase()}`];
+ const shifted = event.shiftKey && !modifier && shiftable[event.code];
+ const name = shifted
+ ? `shift+${shifted}`
+ : `${modifier}${shift}${event.key.toLowerCase()}`;
+ const action = actions[name];
if (action) {
event.preventDefault();
action();
diff --git a/src/lib/dom.ts b/src/lib/dom.ts
index 7d299cf..200fdbc 100644
--- a/src/lib/dom.ts
+++ b/src/lib/dom.ts
@@ -1,6 +1,6 @@
-/** Fields that own their keys, so page shortcuts and held keys leave them alone. */
+/** Fields that own their keys, so page shortcuts and held keys leave them alone; range and color inputs only pick. */
export const typingFields =
- 'input:not([type="range"]), textarea, select, dialog, [role="dialog"]';
+ 'input:not([type="range"], [type="color"]), textarea, select, dialog, [role="dialog"]';
export function isTyping(target: EventTarget | null, fields = typingFields) {
return (
diff --git a/src/lib/parse.ts b/src/lib/parse.ts
index 97e1beb..b595dc0 100644
--- a/src/lib/parse.ts
+++ b/src/lib/parse.ts
@@ -45,3 +45,7 @@ export const range = (min: number, max: number) =>
z.number().check(z.minimum(min), z.maximum(max));
export const unit = range(0, 1);
export const point = z.tuple([z.number(), z.number()]);
+/** A `#rrggbb` color, stored lowercase. */
+export const hexColor = z
+ .string()
+ .check(z.regex(/^#[0-9a-f]{6}$/i), z.toLowerCase());
diff --git a/tests/brush.test.ts b/tests/brush.test.ts
index 5e35888..8f84689 100644
--- a/tests/brush.test.ts
+++ b/tests/brush.test.ts
@@ -2,11 +2,11 @@ import { expect, mock, test } from "bun:test";
import { init, target } from "vgpu/mock";
import { createImageLayer, createMask } from "@/app/editor/layers";
import { createEditorRenderer } from "@/app/editor/renderer";
-import type { BrushStroke } from "@/core/document";
+import type { BrushStroke, StrokePoint } from "@/core/document";
import { createDocument, createResources, findLayer } from "@/core/document";
import { createImageSource } from "@/core/image";
import { imageFrame } from "@/core/image/frame";
-import { strokeDabs } from "@/core/renderer/mask/dabs";
+import { type Dab, type DabWalk, walkDabs } from "@/core/renderer/strokes/dabs";
import { setAdjustments } from "@/features/adjustments/edits";
import {
addLayer,
@@ -26,22 +26,57 @@ const stroke: BrushStroke = {
points: [[10, 10, 1]],
};
-test("dabs follow the stroke at a quarter diameter with interpolated pressure", () => {
- expect(strokeDabs(stroke)).toEqual([[10, 10, 4, 0.5]]);
- const line = strokeDabs({
- ...stroke,
+test("dabs follow the stroke sixteen times per diameter with interpolated pressure, laying what one every quarter would", () => {
+ const wide = { ...stroke, size: 32 };
+ // Four dabs stand for one a quarter diameter apart, together leaving as much uncovered.
+ const laid = (flow: number) => 1 - (1 - flow) ** (1 / 4);
+ expect(walkDabs(wide).dabs).toEqual([[10, 10, 16, laid(0.5)]]);
+ const line = walkDabs({
+ ...wide,
points: [
[10, 10, 1],
[20, 10, 0.5],
[30, 10, 0],
],
- });
+ }).dabs;
expect(line).toHaveLength(11);
- expect(line[0]).toEqual([10, 10, 4, 0.5]);
- expect(line[5]).toEqual([20, 10, 4, 0.25]);
+ expect(line[0]).toEqual([10, 10, 16, laid(0.5)]);
+ expect(line[5]).toEqual([20, 10, 16, laid(0.25)]);
expect(line[10][0]).toBeCloseTo(30);
expect(line[10][3]).toBeCloseTo(0);
- expect(strokeDabs({ ...stroke, points: [] })).toEqual([]);
+ // Small brushes keep a dab per pixel, each laying a share to match.
+ const small = walkDabs({
+ ...stroke,
+ points: [
+ [10, 10, 1],
+ [20, 10, 1],
+ ],
+ }).dabs;
+ expect(small).toHaveLength(11);
+ expect(small[0][3]).toBeCloseTo(1 - 0.5 ** 0.5);
+ expect(walkDabs({ ...stroke, points: [] }).dabs).toEqual([]);
+});
+
+test("a walk goes on where it stopped as a stroke grows, landing the dabs a whole walk would", () => {
+ const points: StrokePoint[] = [
+ [10, 10, 1],
+ [23, 14, 0.8],
+ [31, 30, 0.4],
+ [50, 31, 0.9],
+ [52, 60, 0.2],
+ ];
+ const whole = walkDabs({ ...stroke, size: 20, points }).dabs;
+ const walked: Dab[] = [];
+ let walk: DabWalk | undefined;
+ for (let end = 1; end <= points.length; end++) {
+ const step = walkDabs(
+ { ...stroke, size: 20, points: points.slice(0, end) },
+ walk,
+ );
+ walked.push(...step.dabs);
+ walk = step.walk;
+ }
+ expect(walked).toEqual(whole);
});
test("brush strokes stamp incrementally, replay after undo, and render a proxy during gestures", async () => {
@@ -81,19 +116,24 @@ test("brush strokes stamp incrementally, replay after undo, and render a proxy d
expect(renderer.inspect()).toMatchObject({
passes: [`layer/${mask}/exposure`, `layer/${mask}/raster`],
stamped: 1,
- rasters: [{ id: mask, size: [64, 32] }],
+ // The open stroke waits in the stroke buffer, and the mask reads it laid over its raster in the view.
+ rasters: [
+ { id: mask, size: [32, 16], format: "r8unorm" },
+ { id: "stroke view", size: [32, 16], format: "r8unorm" },
+ { id: "stroke buffer", size: [32, 16], format: "r16float" },
+ ],
});
- expect(renderer.coverage(mask)?.size).toEqual([64, 32]);
- // A 20 px extension adds ten dabs; earlier ones are not stamped again.
+ expect(renderer.coverage(mask)?.target.size).toEqual([32, 16]);
+ // A 20 px extension adds twenty dabs; earlier ones are not stamped again.
extendStroke(document, mask, [[30, 10, 1]]);
await render(true);
- expect(renderer.inspect().stamped).toBe(11);
+ expect(renderer.inspect().stamped).toBe(21);
expect(renderer.outputImage().size).toEqual([64, 32]);
// The display scale sets the proxy: a quarter of a device pixel per source pixel means a quarter-size render.
renderer.setDisplayScale(0.25);
await render(true);
expect(renderer.outputImage().size).toEqual([16, 8]);
- expect(renderer.inspect().stamped).toBe(11);
+ expect(renderer.inspect().stamped).toBe(21);
document.history.commit();
expect(document.history.status.getState().editing).toBe(false);
await render();
@@ -103,11 +143,11 @@ test("brush strokes stamp incrementally, replay after undo, and render a proxy d
expect(notify).toHaveBeenCalledTimes(rendered);
paintStroke(document, mask, { ...stroke, mode: "erase" });
await render();
- expect(renderer.inspect().stamped).toBe(12);
+ expect(renderer.inspect().stamped).toBe(22);
// Undo removes the last stroke, which rebuilds the raster from the remaining one.
document.history.undo();
await render();
- expect(renderer.inspect().stamped).toBe(23);
+ expect(renderer.inspect().stamped).toBe(43);
const layer = findLayer(document.scene.getState().layers, mask);
expect(layer?.kind === "mask" && layer.mask.kind === "brush").toBe(true);
if (layer?.kind === "mask" && layer.mask.kind === "brush") {
@@ -149,9 +189,11 @@ test("brush strokes stamp incrementally, replay after undo, and render a proxy d
expect(renderer.inspect().rasters.map((raster) => raster.id)).toEqual([
mask,
child,
+ "stroke view",
`${gradient}/group`,
+ "stroke buffer",
]);
- expect(renderer.coverage(child)?.size).toEqual([64, 32]);
+ expect(renderer.coverage(child)?.target.size).toEqual([32, 16]);
// Erasing inside the child stamps its own raster only, and the group recombines.
const stampedBefore = renderer.inspect().stamped;
paintStroke(document, child, { ...stroke, mode: "erase" });
diff --git a/tests/editing.e2e.ts b/tests/editing.e2e.ts
index f19a9d9..0c22a82 100644
--- a/tests/editing.e2e.ts
+++ b/tests/editing.e2e.ts
@@ -1052,18 +1052,11 @@ test("edit a photo, inspect the preview and histograms, undo changes, and export
await mobile
.locator('input[type="file"]')
.setInputFiles("tests/fixtures/photo.svg");
- const region = mobile.getByRole("region", { name: "Image canvas" });
- const canvas = await box(region);
const histogram = await box(
mobile.getByRole("region", { name: "Image histogram" }),
);
expect(histogram.width).toBe(176);
expect(histogram.height).toBe(56);
- expect(histogram.x).toBeCloseTo(canvas.x + 12, 0);
- const bottomGap =
- canvas.y + canvas.height - histogram.y - histogram.height;
- expect(bottomGap).toBeGreaterThanOrEqual(11);
- expect(bottomGap).toBeLessThanOrEqual(12);
const exposure = mobile.getByRole("slider", {
name: "Exposure",
exact: true,
diff --git a/tests/exposure-gpu.ts b/tests/exposure-gpu.ts
new file mode 100644
index 0000000..ae3058d
--- /dev/null
+++ b/tests/exposure-gpu.ts
@@ -0,0 +1,55 @@
+import { frame, init, target } from "vgpu";
+import { createImageLayer, createMask } from "@/app/editor/layers";
+import { createEditorRenderer } from "@/app/editor/renderer";
+import { createDocument, createResources } from "@/core/document";
+import { createImageSource } from "@/core/image";
+import { imageFrame } from "@/core/image/frame";
+import { setAdjustments } from "@/features/adjustments/edits";
+import { addLayer } from "@/features/layers/edits";
+
+/** Reads working pixels before display clipping, including those a later mask can recover. */
+export async function recoverExposure() {
+ const gpu = await init();
+ const image = target(gpu, {
+ size: [16, 16],
+ format: "rgba16float",
+ clearColor: [0.25, 0.5, 0.75, 0.75],
+ });
+ frame(gpu, (frame) => frame.pass({ target: image, clear: true }, () => {}));
+ const source = createImageSource(image);
+ const resources = createResources();
+ const sourceId = resources.add(new File([], "photo.png"), source);
+ const document = createDocument(
+ {
+ frame: imageFrame(image.size),
+ layers: [createImageLayer(sourceId, "Photo")],
+ },
+ resources,
+ );
+ const renderer = createEditorRenderer(gpu, source);
+ const errors: string[] = [];
+ gpu.onError((error) => errors.push(error.message));
+ async function samples() {
+ await renderer.update(document.scene.getState());
+ const image = renderer.fullImage();
+ const pixels = await image.readFloats();
+ return [1, 8, 14].map((y) => [
+ ...pixels.slice((y * 16 + 8) * 4, (y * 16 + 8) * 4 + 4),
+ ]);
+ }
+ try {
+ setAdjustments(document, { exposure: 3 });
+ const exposed = await samples();
+ const mask = addLayer(
+ document,
+ createMask({ kind: "linear", start: [0, 4], end: [0, 12] }),
+ );
+ setAdjustments(document, { exposure: -3 }, mask);
+ const recovered = await samples();
+ return { exposed, recovered, errors };
+ } finally {
+ renderer.dispose();
+ document.dispose();
+ gpu.dispose();
+ }
+}
diff --git a/tests/fill.test.ts b/tests/fill.test.ts
index 024a6bd..5dc47cd 100644
--- a/tests/fill.test.ts
+++ b/tests/fill.test.ts
@@ -9,10 +9,10 @@ import {
import { createEditorRenderer } from "@/app/editor/renderer";
import { createDocument, createResources, findLayer } from "@/core/document";
import { createImageSource } from "@/core/image";
+import { parseColor } from "@/core/image/blend";
import { imageFrame } from "@/core/image/frame";
import { setAdjustments } from "@/features/adjustments/edits";
import { setFill } from "@/features/fill/edits";
-import { parseColor } from "@/features/fill/model";
import { addLayer, setLayer } from "@/features/layers/edits";
import { defaultGradient } from "@/features/layers/gradient";
diff --git a/tests/heal.test.ts b/tests/heal.test.ts
index ddcb7ff..24a36ec 100644
--- a/tests/heal.test.ts
+++ b/tests/heal.test.ts
@@ -85,7 +85,7 @@ test("heal patches reuse brush rasters, scale with the proxy, undo, and release
await renderer.update(document.scene.getState(), id, true);
expect(renderer.fullImage().size).toEqual([64, 48]);
expect(renderer.inspect().rasters).toEqual([
- { id: `layer/${id}/${patch}`, size: [256, 192] },
+ { id: `layer/${id}/${patch}`, size: [256, 192], format: "r8unorm" },
]);
const stamped = renderer.inspect().stamped;
extendHealPatch(document, id, [[120, 80, 1]]);
@@ -135,6 +135,45 @@ test("heal patches reuse brush rasters, scale with the proxy, undo, and release
}
});
+test("a heal patch's raster covers only the tiles its stroke reaches, growing with it", async () => {
+ const gpu = await init();
+ const source = createImageSource(
+ target(gpu, { size: [2048, 1536], format: "rgba16float" }),
+ );
+ const resources = createResources();
+ const sourceId = resources.add(new File([], "photo.png"), source);
+ const document = createDocument(
+ {
+ frame: imageFrame(source.image.size),
+ layers: [createImageLayer(sourceId, "Photo")],
+ },
+ resources,
+ );
+ const renderer = createEditorRenderer(gpu, source);
+ const raster = () => renderer.inspect().rasters.map(({ size }) => size);
+ try {
+ const id = addLayer(document, createLayer("heal"));
+ document.history.begin();
+ addHealPatch(
+ document,
+ id,
+ { ...dab, size: 30, points: [[1000, 700, 1]] },
+ [80, 0],
+ );
+ await renderer.update(document.scene.getState(), undefined, true);
+ // One 256 px tile rather than the whole photo.
+ expect(raster()).toEqual([[256, 256]]);
+ extendHealPatch(document, id, [[1100, 700, 1]]);
+ await renderer.update(document.scene.getState(), undefined, true);
+ expect(raster()).toEqual([[512, 256]]);
+ document.history.commit();
+ } finally {
+ renderer.dispose();
+ document.dispose();
+ gpu.dispose();
+ }
+});
+
test("one Healing layer composes its patches in order through render nodes", async () => {
const gpu = await init();
const resources = createResources();
diff --git a/tests/paint.e2e.ts b/tests/paint.e2e.ts
new file mode 100644
index 0000000..3c3ad61
--- /dev/null
+++ b/tests/paint.e2e.ts
@@ -0,0 +1,489 @@
+import type { Page } from "@playwright/test";
+import { expect, test } from "./fixtures";
+import { readImage } from "./images";
+import { box, choose, drag } from "./pointer";
+
+const gray = [128, 128, 128, 255];
+
+/** Opens the gray test photo, 1200 × 800, in a wide window. */
+async function openPhoto(page: Page) {
+ await page.setViewportSize({ width: 1440, height: 1000 });
+ await page.goto("/");
+ await page
+ .locator('input[type="file"]')
+ .setInputFiles("tests/fixtures/photo.svg");
+ await expect(
+ page.getByRole("textbox", { name: "Exposure", exact: true }),
+ ).toHaveValue("0.00");
+}
+
+const brushBar = (page: Page) =>
+ page.getByRole("group", { name: "Layer options" });
+const brushField = (page: Page, name: string) =>
+ brushBar(page).getByRole("textbox", { name, exact: true });
+const brushChip = (page: Page, name: string) =>
+ brushBar(page).getByRole("button", { name, exact: true });
+
+async function setBrush(page: Page, fields: Record<string, string>) {
+ for (const [name, value] of Object.entries(fields)) {
+ await brushField(page, name).fill(value);
+ await brushField(page, name).press("Enter");
+ }
+}
+
+/** Maps photo pixels, counted from the photo's center, to the page. */
+async function photoToPage(page: Page) {
+ const bounds = await box(page.getByRole("region", { name: "Image canvas" }));
+ const scale = Math.min(bounds.width / 1200, bounds.height / 800, 2);
+ return (x: number, y: number) => [
+ bounds.x + bounds.width / 2 + x * scale,
+ bounds.y + bounds.height / 2 + y * scale,
+ ];
+}
+
+/** Export pixels inside the strokes and far above them. */
+async function samples(page: Page) {
+ const { samples } = await readImage(page, undefined, [
+ [600, 400],
+ [600, 100],
+ ]);
+ return samples ?? [];
+}
+
+test("paint colors on a layer, swap them, erase, blend, and switch to a mask", async ({
+ page,
+}) => {
+ test.setTimeout(90_000);
+ const kinds = async () =>
+ (await page.evaluate(() => window.openlight.getState())).scene?.layers.map(
+ (layer) => layer.kind,
+ );
+ await openPhoto(page);
+ await page.getByRole("tab", { name: "Brush", exact: true }).click();
+ const color = brushChip(page, "Color");
+ const mask = brushChip(page, "Mask");
+ await expect(mask).toHaveAttribute("aria-pressed", "true");
+ await color.click();
+ const canvas = page.getByLabel("Paint canvas", { exact: true });
+ await expect(canvas).toBeVisible();
+ // The untouched brush mask gives way to a paint layer.
+ expect(await kinds()).toEqual(["image", "paint"]);
+ await setBrush(page, { Size: "200" });
+ const at = await photoToPage(page);
+ const [from, to] = [at(-150, 0), at(150, 0)];
+ await test.step("a stroke paints the primary color", async () => {
+ await page.getByLabel("Primary color").fill("#ff0000");
+ await drag(page, from, to, 16);
+ expect(await samples(page)).toEqual([[255, 0, 0, 255], gray]);
+ });
+ await test.step("X swaps in the secondary color, white", async () => {
+ await page.keyboard.press("x");
+ await expect(page.getByLabel("Secondary color")).toHaveValue("#ff0000");
+ await drag(page, from, to, 16);
+ expect(await samples(page)).toEqual([[255, 255, 255, 255], gray]);
+ });
+ await test.step("Multiply by white leaves the image as it was", async () => {
+ await choose(
+ page,
+ page.getByRole("combobox", { name: "Blend" }),
+ "Multiply",
+ );
+ expect(await samples(page)).toEqual([gray, gray]);
+ await page.keyboard.press("ControlOrMeta+z");
+ });
+ await test.step("Alt erases the paint, and undo brings it back", async () => {
+ await page.keyboard.down("Alt");
+ await drag(page, from, to, 16);
+ await page.keyboard.up("Alt");
+ expect(await samples(page)).toEqual([gray, gray]);
+ await page.keyboard.press("ControlOrMeta+z");
+ expect(await samples(page)).toEqual([[255, 255, 255, 255], gray]);
+ });
+ await test.step("the mode follows the selected layer", async () => {
+ await mask.click();
+ await expect(
+ page.getByLabel("Brush canvas", { exact: true }),
+ ).toBeVisible();
+ expect(await kinds()).toEqual(["image", "paint", "mask"]);
+ // Choosing the paint layer leaves the tool, and the untouched mask with it; B paints on the layer again.
+ await page
+ .getByRole("button", { name: "Select Paint", exact: true })
+ .click();
+ expect(await kinds()).toEqual(["image", "paint"]);
+ await page.keyboard.press("b");
+ await expect(color).toHaveAttribute("aria-pressed", "true");
+ await expect(canvas).toBeVisible();
+ expect(await kinds()).toEqual(["image", "paint"]);
+ });
+});
+
+/** The red channel's least and most along each displayed row, `rows` in page pixels. */
+async function redRange(
+ page: Page,
+ rows: readonly number[],
+ left: number,
+ right: number,
+) {
+ const canvas = page
+ .getByRole("region", { name: "Image canvas" })
+ .locator("canvas");
+ const origin = await box(canvas);
+ const bytes = await canvas.screenshot();
+ return page.evaluate(
+ async ([bytes, rows, left, right]) => {
+ const image = await createImageBitmap(new Blob([new Uint8Array(bytes)]));
+ const canvas = new OffscreenCanvas(image.width, image.height);
+ const context = canvas.getContext("2d");
+ if (!context) throw new Error("Cannot read preview pixels.");
+ context.drawImage(image, 0, 0);
+ return rows.map((y) => {
+ const { data } = context.getImageData(left, y, right - left, 1);
+ const red = data.filter((_, i) => i % 4 === 0);
+ return [Math.min(...red), Math.max(...red)];
+ });
+ },
+ [
+ [...bytes],
+ rows.map((y) => Math.round(y - origin.y)),
+ Math.round(left - origin.x),
+ Math.round(right - origin.x),
+ ] as const,
+ );
+}
+
+/** Draws a light stroke across the photo in small moves; each row along it must show and stay within a level. */
+async function expectSmoothStroke(page: Page, y: number) {
+ await setBrush(page, { Size: "200", Flow: "40" });
+ const at = await photoToPage(page);
+ // Each small move stamps a dab or two, which used to round to 8 bits apart and leave rings.
+ await drag(page, at(-300, y), at(300, y), 120);
+ const rows = [0, 25, 50, 70].map((offset) => at(0, y + offset)[1]);
+ const ranges = await redRange(page, rows, at(-100, 0)[0], at(100, 0)[0]);
+ expect(ranges[0][0]).toBeGreaterThan(gray[0] + 20);
+ for (const [least, most] of ranges) {
+ expect(most - least).toBeLessThanOrEqual(1);
+ }
+}
+
+test("light strokes drawn a little at a time come out smooth, on a mask and in color", async ({
+ page,
+}) => {
+ test.setTimeout(120_000);
+ await openPhoto(page);
+ await page.getByRole("tab", { name: "Brush", exact: true }).click();
+ await test.step("a mask brightens through its stroke", async () => {
+ const exposure = page.getByRole("textbox", {
+ name: "Exposure",
+ exact: true,
+ });
+ await exposure.fill("2");
+ await exposure.press("Enter");
+ await expectSmoothStroke(page, -150);
+ });
+ await test.step("a paint layer lays red", async () => {
+ await brushChip(page, "Color").click();
+ await page.getByLabel("Primary color").fill("#ff0000");
+ await expectSmoothStroke(page, 150);
+ });
+});
+
+test("strokes settle into pixels, undo takes them back, and scenes keep them", async ({
+ page,
+}) => {
+ test.setTimeout(120_000);
+ await openPhoto(page);
+ const layer = () =>
+ page.evaluate(() => {
+ const [, paint] = window.openlight.getState().scene?.layers ?? [];
+ return paint?.kind === "paint"
+ ? { raster: paint.raster, strokes: paint.strokes.length }
+ : undefined;
+ });
+ // Short strokes down the image, red then blue, the last across the center.
+ const paint = (from: number, to: number) =>
+ page.evaluate(
+ ([from, to]) => {
+ const [, layer] = window.openlight.getState().scene?.layers ?? [];
+ const id = layer?.id ?? window.openlight.addLayer("paint");
+ for (let i = from; i < to; i++) {
+ const x = i === 99 ? 580 : 100 + (i % 10) * 100;
+ const y = i === 99 ? 400 : 80 + Math.floor(i / 10) * 60;
+ window.openlight.addPaintStroke(id, {
+ mode: "paint",
+ size: 30,
+ feather: 0.5,
+ flow: 1,
+ color: i === 99 ? "#0000ff" : "#ff0000",
+ points: [
+ [x, y, 1],
+ [x + 40, y, 1],
+ ],
+ });
+ }
+ },
+ [from, to] as const,
+ );
+ await paint(0, 99);
+ expect(await layer()).toEqual({ raster: undefined, strokes: 99 });
+ const before = await samples(page);
+ expect(before[0]).toEqual(gray);
+
+ await test.step("the hundredth stroke settles every stroke into pixels", async () => {
+ await paint(99, 100);
+ await expect.poll(layer).toMatchObject({ strokes: 0 });
+ expect((await layer())?.raster).toEqual(expect.any(String));
+ const settled = await samples(page);
+ expect(settled[0][2]).toBeGreaterThan(200);
+ expect(settled[1]).toEqual(before[1]);
+ });
+
+ await test.step("undo goes back to the strokes, and redo to the pixels", async () => {
+ const settled = await samples(page);
+ await page.evaluate(() => window.openlight.undo());
+ expect(await layer()).toEqual({ raster: undefined, strokes: 99 });
+ expect(await samples(page)).toEqual(before);
+ await page.evaluate(() => window.openlight.redo());
+ expect(await samples(page)).toEqual(settled);
+ });
+
+ await test.step("a stroke over the pixels undoes by loading them again", async () => {
+ const settled = await samples(page);
+ await paint(100, 101);
+ expect((await layer())?.strokes).toBe(1);
+ await page.evaluate(() => window.openlight.undo());
+ expect(await samples(page)).toEqual(settled);
+ });
+
+ await test.step("a scene file keeps the pixels", async () => {
+ const settled = await samples(page);
+ const raster = (await layer())?.raster;
+ await page.evaluate(async () => {
+ const scene = await window.openlight.exportScene();
+ await window.openlight.openFile(scene);
+ });
+ await expect.poll(layer).toEqual({ raster, strokes: 0 });
+ expect(await samples(page)).toEqual(settled);
+ });
+});
+
+test("a brush mask settles into pixels like paint, and undo and scene files keep them", async ({
+ page,
+}) => {
+ test.setTimeout(120_000);
+ await openPhoto(page);
+ const mask = () =>
+ page.evaluate(() => {
+ const [, layer] = window.openlight.getState().scene?.layers ?? [];
+ return layer?.kind === "mask" && layer.mask.kind === "brush"
+ ? { raster: layer.mask.raster, strokes: layer.mask.strokes.length }
+ : undefined;
+ });
+ // Short strokes down the image, through a mask that brightens by a stop.
+ const paint = (count: number) =>
+ page.evaluate((count) => {
+ const api = window.openlight;
+ const [, layer] = api.getState().scene?.layers ?? [];
+ const id = layer?.id ?? api.addLayer("mask");
+ api.setLayerMask(id, {
+ kind: "brush",
+ strokes: Array.from({ length: count }, (_, i) => {
+ const [x, y] = [100 + (i % 10) * 100, 80 + Math.floor(i / 10) * 60];
+ return {
+ mode: "paint" as const,
+ size: 30,
+ feather: 0.5,
+ flow: 1,
+ points: [
+ [x, y, 1],
+ [x + 40, y, 1],
+ ] as [number, number, number][],
+ };
+ }),
+ });
+ api.setAdjustments({ exposure: 1 }, id);
+ }, count);
+ const inside = async () =>
+ (await readImage(page, undefined, [[620, 80]])).samples?.[0];
+ await paint(99);
+ expect(await mask()).toEqual({ raster: undefined, strokes: 99 });
+ const before = await inside();
+ expect(before?.[0]).toBeGreaterThan(gray[0] + 20);
+ await paint(100);
+ await expect.poll(mask).toMatchObject({ strokes: 0 });
+ const raster = (await mask())?.raster;
+ expect(raster).toEqual(expect.any(String));
+ expect(await inside()).toEqual(before);
+ await page.evaluate(() => window.openlight.undo());
+ expect(await mask()).toEqual({ raster: undefined, strokes: 99 });
+ await page.evaluate(() => window.openlight.redo());
+ expect(await mask()).toEqual({ raster, strokes: 0 });
+ expect(await inside()).toEqual(before);
+ await page.evaluate(async () => {
+ const scene = await window.openlight.exportScene();
+ await window.openlight.openFile(scene);
+ });
+ await expect.poll(mask).toEqual({ raster, strokes: 0 });
+ expect(await inside()).toEqual(before);
+});
+
+test("Photoshop's keys switch the brush, its colors, feather, flow, and opacity, and a right click sizes it", async ({
+ page,
+}) => {
+ await openPhoto(page);
+ const field = (name: string) => brushField(page, name);
+ const chip = (name: string) => brushChip(page, name);
+ await page.keyboard.press("b");
+ await expect(chip("Mask")).toHaveAttribute("aria-pressed", "true");
+ await page.keyboard.press("b");
+ await expect(chip("Color")).toHaveAttribute("aria-pressed", "true");
+
+ const primary = page.getByLabel("Primary color");
+ await primary.fill("#ff0000");
+ await page.keyboard.press("d");
+ await expect(primary).toHaveValue("#000000");
+
+ await page.keyboard.press("Shift+BracketRight");
+ await expect(field("Feather")).toHaveValue("60");
+ await page.keyboard.press("5");
+ await expect(field("Flow")).toHaveValue("50");
+ await page.keyboard.press("Shift+Digit3");
+ await expect(field("Opacity")).toHaveValue("30");
+
+ // On a mask, X paints or erases, as black and white do in Photoshop.
+ await page.keyboard.press("b");
+ await expect(chip("Mask")).toHaveAttribute("aria-pressed", "true");
+ await page.keyboard.press("x");
+ await expect(chip("Erase")).toHaveAttribute("aria-pressed", "true");
+ await page.keyboard.press("x");
+ await expect(chip("Paint")).toHaveAttribute("aria-pressed", "true");
+
+ await page
+ .getByLabel("Brush canvas", { exact: true })
+ .click({ button: "right", position: { x: 300, y: 300 } });
+ const size = page
+ .getByRole("dialog")
+ .getByRole("textbox", { name: "Size", exact: true });
+ await size.fill("120");
+ await size.press("Enter");
+ await expect(field("Size")).toHaveValue("120");
+ await page.keyboard.press("Escape");
+ await expect(page.getByRole("dialog")).toHaveCount(0);
+});
+
+for (const [mode, action] of [
+ ["paint", "append"],
+ ["mask", "append"],
+ ["paint", "undo"],
+ ["paint", "close"],
+] as const) {
+ test(`${mode} settling remains atomic when ${action} happens during readback`, async ({
+ page,
+ }) => {
+ await page.goto("/tests/gpu.html");
+ const result = await page.evaluate(
+ async ({ mode, action }) => {
+ const path = "/tests/settle-gpu.ts";
+ const { editDuringSettle } = (await import(
+ path
+ )) as typeof import("./settle-gpu");
+ return editDuringSettle(mode, action);
+ },
+ { mode, action },
+ );
+ expect(result.errors).toEqual([]);
+ expect(result.historyUnchanged).toBe(true);
+ expect(result.accepted).toBe(action === "append");
+ if (action !== "close") {
+ expect(result.error).toBeLessThan(0.002);
+ }
+ if (action === "append") {
+ expect(result.raster).toEqual(expect.any(String));
+ expect(result.strokes).toBe(1);
+ expect(result.stamped).toBe(101);
+ } else {
+ expect(result.unchanged).toBe(true);
+ expect(result.raster).toBeUndefined();
+ expect(result.strokes).toBe(action === "undo" ? 99 : 100);
+ }
+ });
+}
+
+test("photo exposure, contrast, and curves leave the selected paint color unchanged", async ({
+ page,
+}) => {
+ await openPhoto(page);
+ await page.evaluate(() => {
+ const api = window.openlight;
+ api.setAdjustments({ exposure: 3, contrast: 97 });
+ api.setToneCurve([
+ { x: 0, y: 0 },
+ { x: 0.5, y: 0.8 },
+ { x: 1, y: 1 },
+ ]);
+ const id = api.addLayer("paint");
+ api.addPaintStroke(id, {
+ color: "#b95050",
+ mode: "paint",
+ size: 791,
+ feather: 0.5,
+ flow: 1,
+ points: [[600, 400, 1]],
+ });
+ });
+ expect((await samples(page))[0]).toEqual([185, 80, 80, 255]);
+});
+
+test("deleting a paint layer at the limit lets the brush paint again", async ({
+ page,
+}) => {
+ await openPhoto(page);
+ await page.evaluate(() => {
+ const api = window.openlight;
+ for (let i = 0; i < 4; i++) {
+ const id = api.addLayer("paint");
+ api.setLayer(id, { name: `Paint ${i + 1}` });
+ }
+ const scene = api.getState().scene;
+ if (!scene) throw Error("No photo loaded.");
+ api.selectLayer(scene.layers[0].id);
+ });
+ await page.getByRole("tab", { name: "Brush", exact: true }).click();
+ await brushChip(page, "Color").click();
+ await expect(
+ page.getByText(/A photo holds up to 4 paint layers/),
+ ).toBeVisible();
+ await page
+ .getByRole("button", { name: "Paint 1 actions", exact: true })
+ .click();
+ await page.getByRole("menuitem", { name: "Delete", exact: true }).click();
+ await page.getByLabel("Primary color").fill("#ff0000");
+ const at = await photoToPage(page);
+ await drag(page, at(-20, 0), at(20, 0));
+ expect((await samples(page))[0]).toEqual([255, 0, 0, 255]);
+});
+
+test("at the mask limit, nesting another brush is unavailable", async ({
+ page,
+}) => {
+ await openPhoto(page);
+ await page.evaluate(() => {
+ const api = window.openlight;
+ for (let i = 0; i < 10; i++) {
+ const id = api.addLayer("mask");
+ api.setLayerMask(id, { kind: "brush", strokes: [] });
+ api.setLayer(id, { name: `Mask ${i + 1}` });
+ }
+ });
+ await page
+ .getByRole("button", { name: "Mask 1 actions", exact: true })
+ .click();
+ await page
+ .getByRole("menuitem", { name: "Add to mask", exact: true })
+ .click();
+ await expect(
+ page.getByRole("menuitem", { name: "Brush", exact: true }),
+ ).toBeDisabled();
+ await expect(
+ page.getByRole("menuitem", { name: "Linear gradient", exact: true }),
+ ).toBeEnabled();
+});
diff --git a/tests/paint.test.ts b/tests/paint.test.ts
new file mode 100644
index 0000000..adef488
--- /dev/null
+++ b/tests/paint.test.ts
@@ -0,0 +1,272 @@
+import { expect, test } from "bun:test";
+import { init, target } from "vgpu/mock";
+import {
+ createImageLayer,
+ createLayer,
+ createMask,
+ maskNeutral,
+} from "@/app/editor/layers";
+import { createEditorRenderer } from "@/app/editor/renderer";
+import {
+ type Blend,
+ createDocument,
+ createResources,
+ findLayer,
+ type PaintStroke,
+ settledPixels,
+ updateLayer,
+} from "@/core/document";
+import { createImageSource } from "@/core/image";
+import { imageFrame } from "@/core/image/frame";
+import {
+ addLayer,
+ brushLimits,
+ deleteLayer,
+ duplicateLayer,
+ paintStroke,
+ setLayer,
+ setLayerMask,
+} from "@/features/layers/edits";
+import { defaultGradient } from "@/features/layers/gradient";
+import {
+ addPaintStroke,
+ extendPaintStroke,
+ setPaintBlend,
+} from "@/features/paint/edits";
+
+const stroke: PaintStroke = {
+ mode: "paint",
+ size: 8,
+ feather: 0.5,
+ flow: 1,
+ color: "#FF8000",
+ points: [[600, 500, 1]],
+};
+
+async function open() {
+ const gpu = await init();
+ const source = createImageSource(
+ target(gpu, { size: [1024, 768], format: "rgba16float" }),
+ );
+ const resources = createResources();
+ const sourceId = resources.add(new File([], "photo.png"), source);
+ const document = createDocument(
+ {
+ frame: imageFrame(source.image.size),
+ layers: [{ ...createImageLayer(sourceId, "Photo"), id: "base" }],
+ },
+ resources,
+ );
+ return { gpu, source, document };
+}
+
+test("a paint layer stamps colored strokes into one raster at half the photo's resolution, kept until the layer goes, through one stroke buffer", async () => {
+ const { gpu, source, document } = await open();
+ const renderer = createEditorRenderer(gpu, source);
+ const render = () => renderer.update(document.scene.getState());
+ try {
+ const paint = addLayer(document, createLayer("paint"));
+ await render();
+ // A layer without paint is bypassed and holds no raster.
+ expect(renderer.inspect()).toMatchObject({ passes: [], rasters: [] });
+ document.history.begin();
+ addPaintStroke(document, paint, stroke);
+ extendPaintStroke(document, paint, [[640, 500, 1]]);
+ document.history.commit();
+ await render();
+ expect(renderer.inspect()).toMatchObject({
+ passes: [`layer/${paint}/paint`],
+ stamped: 1 + 40,
+ rasters: [
+ { id: paint, size: [512, 384], format: "rgba8unorm" },
+ { id: "stroke buffer", size: [512, 384], format: "r16float" },
+ ],
+ });
+ const scene = document.scene.getState();
+ const layer = scene.layers[1];
+ expect(layer.kind === "paint" && layer.strokes[0].color).toBe("#ff8000");
+ setPaintBlend(document, paint, "multiply");
+ expect(() => setPaintBlend(document, paint, "dissolve" as Blend)).toThrow(
+ "Invalid blend",
+ );
+ expect(() =>
+ addPaintStroke(document, paint, { ...stroke, color: "orange" }),
+ ).toThrow("Invalid stroke");
+ // Hiding keeps the raster, so showing the layer again stamps nothing.
+ setLayer(document, paint, { visible: false });
+ await render();
+ expect(renderer.inspect()).toMatchObject({ passes: [], stamped: 41 });
+ expect(renderer.inspect().rasters).toHaveLength(2);
+ // Undoing every stroke clears the raster rather than freeing it, and painting again reuses it.
+ const raster = renderer.coverage(paint)?.target;
+ document.history.undo();
+ document.history.undo();
+ document.history.undo();
+ await render();
+ expect(renderer.inspect()).toMatchObject({ passes: [] });
+ expect(renderer.coverage(paint)?.target).toBe(raster);
+ addPaintStroke(document, paint, stroke);
+ await render();
+ expect(renderer.inspect().passes).toEqual([`layer/${paint}/paint`]);
+ expect(renderer.coverage(paint)?.target).toBe(raster);
+ // Deleting the last paint layer frees its raster and the stroke buffer.
+ deleteLayer(document, paint);
+ await render();
+ expect(renderer.inspect().rasters).toEqual([]);
+ } finally {
+ renderer.dispose();
+ document.dispose();
+ gpu.dispose();
+ }
+});
+
+test("a photo holds up to 4 paint layers and, apart, 10 brush masks", async () => {
+ const { gpu, document } = await open();
+ try {
+ const paint = addLayer(document, createLayer("paint"));
+ for (let i = 1; i < brushLimits.color; i++) {
+ addLayer(document, createLayer("paint"));
+ }
+ const full = document.scene.getState();
+ expect(() => addLayer(document, createLayer("paint"))).toThrow(
+ "up to 4 paint layers",
+ );
+ expect(() => duplicateLayer(document, paint)).toThrow("paint layers");
+ expect(document.scene.getState()).toBe(full);
+ for (let i = 0; i < brushLimits.mask; i++) {
+ addLayer(document, createMask({ kind: "brush", strokes: [] }));
+ }
+ expect(() =>
+ addLayer(document, createMask({ kind: "brush", strokes: [] })),
+ ).toThrow("up to 10 brush masks");
+ const gradient = addLayer(
+ document,
+ createMask(defaultGradient([1024, 768])),
+ );
+ const before = document.scene.getState();
+ expect(() =>
+ setLayerMask(document, gradient, { kind: "brush", strokes: [] }),
+ ).toThrow("up to 10 brush masks");
+ expect(document.scene.getState()).toBe(before);
+ // Other layers still fit.
+ addLayer(document, createLayer("exposure"));
+ } finally {
+ document.dispose();
+ gpu.dispose();
+ }
+});
+
+test("settled paint stays while the history names it and goes after", async () => {
+ const { gpu, document } = await open();
+ try {
+ const paint = addLayer(document, createLayer("paint"));
+ addPaintStroke(document, paint, stroke);
+ const settle = (pixels: string) => {
+ const raster = document.resources.addPaint(new Blob([pixels]));
+ document.replace(
+ updateLayer(document.scene.getState(), paint, (layer) => ({
+ ...layer,
+ raster,
+ strokes: [],
+ })),
+ );
+ return raster;
+ };
+ const first = settle("first");
+ const undoCount = document.history.status.getState().undoCount;
+ // Settling shows nothing new, so it records no step.
+ expect(undoCount).toBe(2);
+ addPaintStroke(document, paint, stroke);
+ const second = settle("second");
+ // The stroke's step names the first pixels, so they stay; nothing names them once it goes.
+ expect(await document.resources.paint(first).text()).toBe("first");
+ document.history.undo();
+ expect(document.scene.getState().layers[1]).toMatchObject({
+ raster: first,
+ strokes: [],
+ });
+ document.history.clear();
+ setLayer(document, paint, { name: "Glow" });
+ expect(() => document.resources.paint(second)).toThrow("unavailable");
+ expect(await document.resources.paint(first).text()).toBe("first");
+ // Deleting the layer keeps them for undo, until that step goes too.
+ deleteLayer(document, paint);
+ expect(await document.resources.paint(first).text()).toBe("first");
+ document.history.clear();
+ expect(() => document.resources.paint(first)).toThrow("unavailable");
+ } finally {
+ document.dispose();
+ gpu.dispose();
+ }
+});
+
+test("a brush mask settles like paint: its pixels stay while history names them, and none come from outside", async () => {
+ const { gpu, document } = await open();
+ const { color: _, ...coverage } = stroke;
+ try {
+ const mask = addLayer(document, createMask({ kind: "brush", strokes: [] }));
+ paintStroke(document, mask, coverage);
+ const raster = document.resources.addPaint(new Blob(["coverage"]));
+ document.replace(
+ updateLayer(document.scene.getState(), mask, (layer) =>
+ layer.kind === "mask" && layer.mask.kind === "brush"
+ ? { ...layer, mask: { ...layer.mask, raster, strokes: [] } }
+ : layer,
+ ),
+ );
+ // A stroke keeps the settled pixels under it, and undo still reaches them.
+ paintStroke(document, mask, coverage);
+ const layer = () => findLayer(document.scene.getState().layers, mask);
+ expect(layer()).toMatchObject({ mask: { raster, strokes: [coverage] } });
+ document.history.undo();
+ expect(layer()).toMatchObject({ mask: { raster, strokes: [] } });
+ expect(await document.resources.paint(raster).text()).toBe("coverage");
+ expect(() =>
+ setLayerMask(document, mask, {
+ kind: "brush",
+ raster: "made-up",
+ strokes: [],
+ }),
+ ).toThrow("unavailable");
+ deleteLayer(document, mask);
+ document.history.clear();
+ expect(() => document.resources.paint(raster)).toThrow("unavailable");
+ } finally {
+ document.dispose();
+ gpu.dispose();
+ }
+});
+
+test("settled pixels taken with a scene outlive the document, as an export's must", async () => {
+ const { gpu, document } = await open();
+ try {
+ const paint = addLayer(document, createLayer("paint"));
+ const raster = document.resources.addPaint(new Blob(["settled"]));
+ document.replace(
+ updateLayer(document.scene.getState(), paint, (layer) => ({
+ ...layer,
+ raster,
+ strokes: [],
+ })),
+ );
+ const pixels = settledPixels(document);
+ // Opening another photo closes this document and frees what it holds.
+ document.dispose();
+ expect(() => document.resources.paint(raster)).toThrow("unavailable");
+ expect(await pixels.get(raster)?.text()).toBe("settled");
+ } finally {
+ gpu.dispose();
+ }
+});
+
+test("a paint layer changes a mask once it holds paint, settled or not", () => {
+ const mask = createMask({ kind: "brush", strokes: [] });
+ const paint = createLayer("paint");
+ expect(maskNeutral({ ...mask, children: [paint] })).toBe(true);
+ expect(
+ maskNeutral({ ...mask, children: [{ ...paint, strokes: [stroke] }] }),
+ ).toBe(false);
+ expect(
+ maskNeutral({ ...mask, children: [{ ...paint, raster: "settled" }] }),
+ ).toBe(false);
+});
diff --git a/tests/renderer.test.ts b/tests/renderer.test.ts
index 12ba7f5..c1a067f 100644
--- a/tests/renderer.test.ts
+++ b/tests/renderer.test.ts
@@ -281,9 +281,9 @@ test("rendering follows grouped edits and undo, reuses pipelines, and releases o
expect(renderer.inputImage("base")).not.toBe(renderer.outputImage());
expect(renderer.inputImage("exposure")).toBeUndefined();
expect(renderer.inspect().passes).toEqual([
- "layer/exposure/exposure",
"layer/base/exposure",
"layer/base/curves",
+ "layer/exposure/exposure",
]);
document.edit(beforeInput);
expect(renderer.inputImage("base")).toBeUndefined();
diff --git a/tests/rendering-benchmark.ts b/tests/rendering-benchmark.ts
index 00f119e..965eac5 100644
--- a/tests/rendering-benchmark.ts
+++ b/tests/rendering-benchmark.ts
@@ -30,6 +30,7 @@ export type Workload =
| "layer-stack"
| "fill"
| "lut"
+ | "paint"
| "heal"
| "heal-empty"
| "heal-proxy"
@@ -47,7 +48,13 @@ function summarize(values: number[]) {
};
}
-/** Wavy strokes across the image, three painted and one erased, dabbed every quarter diameter. */
+const rasterPixelBytes: Record<string, number> = {
+ r8unorm: 1,
+ r16float: 2,
+ rgba8unorm: 4,
+};
+
+/** Wavy strokes across the image, three painted and one erased. */
function brushStrokes(size: [number, number]): BrushStroke[] {
return [0.25, 0.45, 0.65, 0.45].map((row, index) => ({
mode: index === 3 ? "erase" : "paint",
@@ -223,6 +230,19 @@ export async function benchmarkRendering(
lut: benchmarkLut(),
});
}
+ if (workload === "paint") {
+ effects.push({
+ ...common,
+ id: "benchmark-paint",
+ name: "Paint",
+ kind: "paint",
+ blend: "soft-light",
+ strokes: brushStrokes(size).map((stroke, index) => ({
+ ...stroke,
+ color: ["#f0763c", "#3c8ef0", "#f0d23c", "#000000"][index],
+ })),
+ });
+ }
if (
workload === "heal" ||
workload === "heal-empty" ||
@@ -276,7 +296,7 @@ export async function benchmarkRendering(
],
};
function create(clock?: Timer) {
- const renderer = createEditorRenderer(gpu, source, clock);
+ const renderer = createEditorRenderer(gpu, source, { timer: clock });
// Half a device pixel per source pixel, as a fitted view of a large photo, renders at a factor of 2.
renderer.setDisplayScale(proxy ? 0.5 : 1);
return renderer;
@@ -391,9 +411,10 @@ export async function benchmarkRendering(
sum + size[0] * size[1] * (format === "rgba32float" ? 16 : 8),
0,
),
- // Brush rasters are r8unorm at source resolution, outside the graph.
+ // Brush rasters, paint, and the stroke buffer, at source resolution, outside the graph.
rasterBytes: storage.rasters.reduce(
- (sum, { size }) => sum + size[0] * size[1],
+ (sum, { size, format }) =>
+ sum + size[0] * size[1] * rasterPixelBytes[format],
0,
),
image: [...new Uint8Array(await blob.arrayBuffer())],
diff --git a/tests/rendering.bench.ts b/tests/rendering.bench.ts
index 03ca910..5e37f80 100644
--- a/tests/rendering.bench.ts
+++ b/tests/rendering.bench.ts
@@ -18,6 +18,7 @@ for (const workload of [
"layer-stack",
"fill",
"lut",
+ "paint",
"heal",
"heal-empty",
"heal-proxy",
diff --git a/tests/rendering.e2e.ts b/tests/rendering.e2e.ts
index 7552b10..e12871b 100644
--- a/tests/rendering.e2e.ts
+++ b/tests/rendering.e2e.ts
@@ -180,3 +180,24 @@ test("tone adjustments preserve ramps, colors and alpha, compose, and reset", as
expect(result.reloadMatches).toBe(true);
expect(result.resetMatches).toBe(true);
});
+
+test("a gradient recovers +3 EV photo exposure without clipping between layers", async ({
+ page,
+}) => {
+ await page.goto("/tests/gpu.html");
+ const result = await page.evaluate(async () => {
+ const path = "/tests/exposure-gpu.ts";
+ const { recoverExposure } = (await import(
+ path
+ )) as typeof import("./exposure-gpu");
+ return recoverExposure();
+ });
+ expect(result.errors).toEqual([]);
+ for (const exposed of result.exposed) {
+ expect(exposed).toEqual([2, 4, 6, 0.75]);
+ }
+ expect(result.recovered[0]).toEqual([0.25, 0.5, 0.75, 0.75]);
+ expect(result.recovered[2]).toEqual([2, 4, 6, 0.75]);
+ expect(result.recovered[1][2]).toBeGreaterThan(1);
+ expect(result.recovered[1][3]).toBe(0.75);
+});
diff --git a/tests/scene-file.test.ts b/tests/scene-file.test.ts
index e2dbebe..a0ebb90 100644
--- a/tests/scene-file.test.ts
+++ b/tests/scene-file.test.ts
@@ -1,7 +1,7 @@
import { expect, test } from "bun:test";
import { init, target } from "vgpu/mock";
import { createControls } from "@/app/controls";
-import { createImageLayer } from "@/app/editor/layers";
+import { createImageLayer, createLayer, createMask } from "@/app/editor/layers";
import { openSceneFile } from "@/app/loaders/scene";
import { createWorkspace } from "@/app/workspace";
import { createDocument, createResources } from "@/core/document";
@@ -98,6 +98,19 @@ test("scene files reopen the photo with every layer for further editing", async
{ mode: "paint", size: 4, feather: 0.2, flow: 1, points: [[40, 20, 1]] },
[-6, 2],
);
+ const paint = api.addLayer("paint");
+ api.addPaintStroke(paint, {
+ mode: "paint",
+ size: 5,
+ feather: 0.3,
+ flow: 1,
+ color: "#ff8800",
+ points: [
+ [12, 6, 1],
+ [30, 9, 0.7],
+ ],
+ });
+ api.setPaintBlend(paint, "overlay");
api.setFill({ color: "#123456", blend: "soft-light" });
const fill = api.getState().scene?.layers.at(-1)?.id ?? "";
api.setLayer(fill, { name: "Warm", opacity: 0.5, visible: false });
@@ -197,10 +210,30 @@ test("scene files reopen the photo with every layer for further editing", async
"The table needs three values per entry",
],
];
+ for (const [kind, count, message] of [
+ ["paint", 5, "up to 4 paint layers"],
+ ["mask", 11, "up to 10 brush masks"],
+ ] as const) {
+ const layers = Array.from({ length: count }, () =>
+ kind === "paint"
+ ? createLayer("paint")
+ : createMask({ kind: "brush", strokes: [] }),
+ );
+ invalid.push([
+ await archive({
+ ...json,
+ scene: { ...json.scene, layers: [json.scene.layers[0], ...layers] },
+ }),
+ message,
+ ]);
+ }
+ const decodedBefore = raw.decoded.length;
for (const [value, message] of invalid) {
await expect(openSceneFile(value, raw.decode)).rejects.toThrow(message);
}
+ expect(raw.decoded).toHaveLength(decodedBefore);
+
// A parameter missing from a group takes its default, as in a file older than that parameter.
const vignette = json.scene.layers.find(
(layer: { kind: string }) => layer.kind === "vignette",
diff --git a/tests/settle-gpu.ts b/tests/settle-gpu.ts
new file mode 100644
index 0000000..e7da1cf
--- /dev/null
+++ b/tests/settle-gpu.ts
@@ -0,0 +1,145 @@
+import { frame, init, target } from "vgpu";
+import { createImageLayer, createLayer, createMask } from "@/app/editor/layers";
+import { createEditorRenderer } from "@/app/editor/renderer";
+import { createDocument, createResources, paintingOf } from "@/core/document";
+import { createImageSource } from "@/core/image";
+import { imageFrame } from "@/core/image/frame";
+import { setAdjustments } from "@/features/adjustments/edits";
+import { addLayer, paintStroke } from "@/features/layers/edits";
+import { addPaintStroke } from "@/features/paint/edits";
+import { settlePaint } from "@/features/paint/settle";
+
+/** Holds actual GPU readback while the document changes, as another stroke or undo can do. */
+export async function editDuringSettle(
+ mode: "paint" | "mask",
+ action: "append" | "undo" | "close",
+) {
+ const gpu = await init();
+ const errors: string[] = [];
+ gpu.onError((error) => errors.push(error.message));
+ gpu.gpu.addEventListener("uncapturederror", (event) =>
+ errors.push(event.error.message),
+ );
+ const height = action === "close" ? 1200 : 64;
+ const source = createImageSource(
+ target(gpu, {
+ size: [64, height],
+ format: "rgba16float",
+ clearColor: [0.2, 0.2, 0.2, 1],
+ }),
+ );
+ frame(gpu, (frame) =>
+ frame.pass({ target: source.image, clear: true }, () => {}),
+ );
+ const resources = createResources();
+ const sourceId = resources.add(new File([], "photo.png"), source);
+ const document = createDocument(
+ {
+ frame: imageFrame(source.image.size),
+ layers: [createImageLayer(sourceId, "Photo")],
+ },
+ resources,
+ );
+ const renderer = createEditorRenderer(gpu, source, {
+ paintPixels: resources.paint,
+ });
+ const reference = createEditorRenderer(gpu, source, {
+ paintPixels: resources.paint,
+ });
+ let pending = Promise.resolve();
+ const detach = document.scene.subscribe((scene) => {
+ pending = renderer.update(scene);
+ });
+ let release = () => {};
+ let started = () => {};
+ const gate = new Promise<void>((resolve) => {
+ release = resolve;
+ });
+ const reading = new Promise<void>((resolve) => {
+ started = resolve;
+ });
+ const createBuffer = gpu.device.createBuffer.bind(gpu.device);
+ try {
+ const id = addLayer(
+ document,
+ mode === "paint"
+ ? createLayer("paint")
+ : createMask({ kind: "brush", strokes: [] }),
+ );
+ if (mode === "mask") setAdjustments(document, { exposure: 1 }, id);
+ const stroke = {
+ mode: "paint" as const,
+ size: 8,
+ feather: 0.5,
+ flow: 0.01,
+ points: [[32, height / 2, 1] as const],
+ };
+ const paint = () =>
+ mode === "paint"
+ ? addPaintStroke(document, id, { ...stroke, color: "#ff0000" })
+ : paintStroke(document, id, stroke);
+ for (let i = 0; i < 100; i++) paint();
+ await pending;
+ // The next buffer is readRaster's mapped buffer. Keep it mapped until the edit queued its render.
+ gpu.device.createBuffer = (descriptor) => {
+ const buffer = createBuffer(descriptor);
+ const map = buffer.gpu.mapAsync.bind(buffer.gpu);
+ buffer.gpu.mapAsync = async (...args) => {
+ await map(...args);
+ started();
+ await gate;
+ };
+ return buffer;
+ };
+ const settling = settlePaint(document, renderer, id);
+ await reading;
+ gpu.device.createBuffer = createBuffer;
+ if (action === "append") paint();
+ if (action === "undo") document.history.undo();
+ if (action === "close") {
+ pending = renderer.update(document.scene.getState());
+ document.dispose();
+ renderer.dispose();
+ }
+ const before = document.scene.getState();
+ const undoCount = document.history.status.getState().undoCount;
+ release();
+ const accepted = await settling;
+ await pending;
+ await gpu.gpu.queue.onSubmittedWorkDone();
+ await gpu.settled();
+ const scene = document.scene.getState();
+ let error: number | undefined;
+ if (action !== "close") {
+ await renderer.update(scene);
+ await reference.update(before);
+ const actual = await renderer.fullImage().readFloats();
+ const expected = await reference.fullImage().readFloats();
+ error = actual.reduce(
+ (max, value, index) => Math.max(max, Math.abs(value - expected[index])),
+ 0,
+ );
+ }
+ const layer = scene.layers.find((layer) => layer.id === id);
+ const painting = layer && paintingOf(layer);
+ return {
+ accepted,
+ strokes: painting?.strokes.length,
+ raster: painting?.raster,
+ stamped: renderer.inspect().stamped,
+ unchanged: scene === before,
+ historyUnchanged:
+ document.history.status.getState().undoCount === undoCount,
+ error,
+ errors,
+ };
+ } finally {
+ release();
+ gpu.device.createBuffer = createBuffer;
+ detach();
+ reference.dispose();
+ renderer.dispose();
+ document.dispose();
+ gpu.dispose();
+ }
+}
diff --git a/tests/settle.test.ts b/tests/settle.test.ts
new file mode 100644
index 0000000..b6ad962
--- /dev/null
+++ b/tests/settle.test.ts
@@ -0,0 +1,61 @@
+import { expect, test } from "bun:test";
+import { init, target } from "vgpu/mock";
+import { createImageLayer, createLayer } from "@/app/editor/layers";
+import { createDocument, createResources } from "@/core/document";
+import { createImageSource } from "@/core/image";
+import { imageFrame } from "@/core/image/frame";
+import { addLayer } from "@/features/layers/edits";
+import { addPaintStroke } from "@/features/paint/edits";
+import { settleAt, watchSettling } from "@/features/paint/settle";
+
+test("an unsuccessful settle does not retry on its own", async () => {
+ const gpu = await init();
+ const source = createImageSource(
+ target(gpu, { size: [64, 64], format: "rgba16float" }),
+ );
+ const resources = createResources();
+ const sourceId = resources.add(new File([], "photo.png"), source);
+ const document = createDocument(
+ {
+ frame: imageFrame(source.image.size),
+ layers: [createImageLayer(sourceId, "Photo")],
+ },
+ resources,
+ );
+ // A renderer that cannot accept the settle, as after another photo opens.
+ let settles = 0;
+ const renderer = {
+ settle: () => {
+ settles++;
+ // Stops a runaway loop, so the test fails rather than hangs.
+ return settles < 50
+ ? Promise.resolve(false)
+ : new Promise<boolean>(() => {});
+ },
+ subscribe: () => () => {},
+ };
+ const stop = watchSettling(document, renderer);
+ try {
+ const paint = addLayer(document, createLayer("paint"));
+ document.history.begin();
+ for (let i = 0; i < settleAt; i++) {
+ addPaintStroke(document, paint, {
+ mode: "paint",
+ size: 8,
+ feather: 0.5,
+ flow: 1,
+ color: "#ff0000",
+ points: [[10, 10, 1]],
+ });
+ }
+ expect(settles).toBe(0);
+ // Ending the gesture checks the crowded layer once.
+ document.history.commit();
+ await new Promise((resolve) => setTimeout(resolve, 0));
+ expect(settles).toBe(1);
+ } finally {
+ stop();
+ document.dispose();
+ gpu.dispose();
+ }
+});