Skip to content

Free open-source image editor for the browser

Read histograms through one mapped buffer each, so painting and editing make no buffers (#70)

Rodrigo Belfiore71c9513d85c8
diff --git a/src/features/histogram/histogram.ts b/src/features/histogram/histogram.ts
index 91fddae..f764c5c 100644
--- a/src/features/histogram/histogram.ts
+++ b/src/features/histogram/histogram.ts
@@ -6,6 +6,9 @@ type Passes = {
finish: ReturnType<typeof compute>;
};
+/** `GPUMapMode.READ`, which the page's WebGPU types leave out. */
+const mapRead = 1;
+
/** The counting passes, compiled once per GPU and shared, since a histogram mounts with each curve shown. */
const shared = new WeakMap<Gpu, Passes>();
@@ -21,7 +24,11 @@ function histogramPasses(gpu: Gpu) {
return passes;
}
-/** Counts an image into 256 bins per channel on the GPU and reads back normalized heights. */
+/**
+ * Counts an image into 256 bins per channel on the GPU and reads back normalized heights. Reads take
+ * turns, so one mapped buffer serves them all, rather than one made and destroyed per frame, a churn
+ * Safari tolerates poorly.
+ */
export function createHistogram(gpu: Gpu) {
const { count, finish } = histogramPasses(gpu);
const bins = gpu.device.createBuffer({
@@ -32,18 +39,51 @@ export function createHistogram(gpu: Gpu) {
size: 3072,
usage: ["storage", "copy_src"],
});
+ const staging = gpu.device.createBuffer({
+ size: 3072,
+ usage: ["copy_dst", "map_read"],
+ });
const empty = new Uint32Array(768);
+ let turn: Promise<unknown> = Promise.resolve();
+ async function readHeights(
+ image: () => Target | undefined,
+ working: boolean,
+ channels: 1 | 3,
+ ) {
+ const source = image();
+ if (!source) {
+ return undefined;
+ }
+ const params = { working: Number(working), channels };
+ const bytes = channels * 1024;
+ bins.write(empty);
+ count.set({ source: source.color, bins, params }).dispatch(32, 20);
+ finish.set({ bins, heights, params }).dispatch(1);
+ const encoder = gpu.gpu.createCommandEncoder();
+ encoder.copyBufferToBuffer(heights.gpu, 0, staging.gpu, 0, bytes);
+ gpu.gpu.queue.submit([encoder.finish()]);
+ await staging.gpu.mapAsync(mapRead, 0, bytes);
+ try {
+ return new Float32Array(staging.gpu.getMappedRange(0, bytes).slice(0));
+ } finally {
+ staging.gpu.unmap();
+ }
+ }
return {
- async read(image: Target, working = false, channels: 1 | 3 = 3) {
- const params = { working: Number(working), channels };
- bins.write(empty);
- count.set({ source: image.color, bins, params }).dispatch(32, 20);
- finish.set({ bins, heights, params }).dispatch(1);
- return new Float32Array(await heights.read(channels * 1024));
+ /** Heights of the image `image` returns when this read's turn comes, since an earlier one may have replaced it. */
+ read(
+ image: () => Target | undefined,
+ working = false,
+ channels: 1 | 3 = 3,
+ ) {
+ const read = turn.then(() => readHeights(image, working, channels));
+ turn = read.catch(() => {});
+ return read;
},
dispose() {
bins.dispose();
heights.dispose();
+ staging.dispose();
},
};
}
diff --git a/src/features/histogram/index.tsx b/src/features/histogram/index.tsx
index 2f2c0df..f3503b8 100644
--- a/src/features/histogram/index.tsx
+++ b/src/features/histogram/index.tsx
@@ -9,7 +9,7 @@ type Colors = readonly [string] | readonly [string, string, string];
/** Draws each channel as a filled polygon with an outline; one read runs at a time and the latest request wins. */
function plot(
svg: SVGSVGElement,
- read: (image: Target) => Promise<Float32Array>,
+ read: (image: () => Target | undefined) => Promise<Float32Array | undefined>,
image: () => Target | undefined,
colors: Colors,
) {
@@ -38,21 +38,20 @@ function plot(
try {
do {
requested = false;
- const source = image();
- if (!source) {
- for (const { polygon, polyline } of curves) {
- polygon.setAttribute("points", "");
- polyline.setAttribute("points", "");
- }
- return;
- }
- const values = await read(source);
+ const values = await read(image);
if (!group.isConnected) {
return;
}
if (requested) {
continue;
}
+ if (!values) {
+ for (const { polygon, polyline } of curves) {
+ polygon.setAttribute("points", "");
+ polyline.setAttribute("points", "");
+ }
+ return;
+ }
curves.forEach(({ polygon, polyline }, channel) => {
const points = Array.from(
values.subarray(channel * 256, (channel + 1) * 256),
@@ -94,7 +93,7 @@ export function Histogram({
if (svg) {
const chart = plot(
svg,
- (source) => histogram.read(source, working, colors.length),
+ (image) => histogram.read(image, working, colors.length),
image,
colors,
);
diff --git a/tests/gpu-stats.e2e.ts b/tests/gpu-stats.e2e.ts
index 2c109cd..8a136de 100644
--- a/tests/gpu-stats.e2e.ts
+++ b/tests/gpu-stats.e2e.ts
@@ -21,4 +21,13 @@ test("?stats counts the GPU's textures and buffers, and Reset starts a workflow
await exposure.fill("1");
await exposure.press("Enter");
await expect(value("Textures")).toHaveText(/ · [1-9]\d* made/);
+ // Changing values only rewrites uniforms: every render, histograms included, reuses what it has.
+ await stats.getByRole("button", { name: "Reset" }).click();
+ for (const next of ["2", "0.5", "1.5"]) {
+ await exposure.fill(next);
+ await exposure.press("Enter");
+ }
+ await page.waitForTimeout(1500);
+ await expect(value("Buffers")).toHaveText(/ · 0 made · 0 freed/);
+ await expect(value("Textures")).toHaveText(/ · 0 made · 0 freed/);
});
diff --git a/tests/rendering-benchmark.ts b/tests/rendering-benchmark.ts
index 91c6ca5..00f119e 100644
--- a/tests/rendering-benchmark.ts
+++ b/tests/rendering-benchmark.ts
@@ -297,7 +297,7 @@ export async function benchmarkRendering(
if (inputId && !inspected) {
throw Error("Missing benchmark curve input.");
}
- const reading = inspected && histogram?.read(inspected, true, 1);
+ const reading = inspected && histogram?.read(() => inspected, true, 1);
const encoded = performance.now();
await gpu.gpu.queue.onSubmittedWorkDone();
const end = performance.now();