Skip to content

Free open-source image editor for the browser

Show the GPU's textures, buffers, memory, and frame rate with ?stats (#69)

Rodrigo Belfiore01bc3e8677d1
diff --git a/README.md b/README.md
index 2c50d94..7853421 100644
--- a/README.md
+++ b/README.md
@@ -64,6 +64,8 @@ bun dev
The shared controls, such as buttons, sliders, menus, and panels, come from [@roprgm/ui](https://ui.roprgm.com). See [AGENTS.md](AGENTS.md) for browser setup and checks, and [ARCHITECTURE.md](ARCHITECTURE.md) for how the code is organized. `window.openlight` exposes a scripting [API](API.md).
+Add `?stats` to the URL to see frames per second and every texture and buffer the GPU makes, frees, and keeps, with their memory; Reset counts a workflow from zero. It wraps WebGPU's own methods, so nothing else in the code reports to it.
+
## License
[MIT](LICENSE)
diff --git a/src/components/gpu-stats.tsx b/src/components/gpu-stats.tsx
new file mode 100644
index 0000000..7779231
--- /dev/null
+++ b/src/components/gpu-stats.tsx
@@ -0,0 +1,90 @@
+import { Button } from "@roprgm/ui/button";
+import { useEffect, useState } from "react";
+import { readGpuStats, resetGpuStats } from "@/lib/gpu-stats";
+
+type Shown = ReturnType<typeof readGpuStats> & {
+ fps: number;
+ texturesPerSecond: number;
+ heap?: number;
+};
+
+/** Chromium alone reports the JavaScript heap. */
+const memory = (performance as { memory?: { usedJSHeapSize: number } }).memory;
+
+const megabytes = (bytes: number) => `${(bytes / 2 ** 20).toFixed(1)} MB`;
+
+function Row({ label, value }: { label: string; value: string }) {
+ return (
+ <>
+ <dt className="text-muted">{label}</dt>
+ <dd>{value}</dd>
+ </>
+ );
+}
+
+/**
+ * Frames per second and the GPU's textures and buffers, made, freed, and alive, refreshed every
+ * second over the page. Reset starts counting a workflow.
+ */
+export function GpuStats() {
+ const [shown, setShown] = useState<Shown>();
+ useEffect(() => {
+ let frames = 0;
+ let frame = requestAnimationFrame(function count() {
+ frames++;
+ frame = requestAnimationFrame(count);
+ });
+ let last = readGpuStats();
+ const timer = setInterval(() => {
+ const now = readGpuStats();
+ setShown({
+ ...now,
+ fps: frames,
+ texturesPerSecond: Math.max(0, now.textures.made - last.textures.made),
+ heap: memory?.usedJSHeapSize,
+ });
+ frames = 0;
+ last = now;
+ }, 1000);
+ return () => {
+ cancelAnimationFrame(frame);
+ clearInterval(timer);
+ };
+ }, []);
+ if (!shown) {
+ return null;
+ }
+ const { textures, buffers } = shown;
+ return (
+ <section
+ aria-label="GPU stats"
+ className="pointer-events-none fixed top-14 right-2 z-50 md:top-auto md:right-auto md:bottom-14 md:left-14 flex flex-col gap-2 rounded-md bg-level-4/80 px-3 py-2 tabular-nums backdrop-blur-sm"
+ >
+ <dl className="grid grid-cols-[auto_auto] gap-x-3">
+ <Row label="FPS" value={`${shown.fps}`} />
+ <Row
+ label="Textures"
+ value={`${textures.live} live · ${textures.made} made · ${textures.freed} freed · ${shown.texturesPerSecond}/s`}
+ />
+ <Row
+ label="Buffers"
+ value={`${buffers.live} live · ${buffers.made} made · ${buffers.freed} freed`}
+ />
+ <Row
+ label="GPU"
+ value={`${megabytes(textures.bytes + buffers.bytes)} · peak ${megabytes(shown.peakBytes)}`}
+ />
+ {shown.heap !== undefined && (
+ <Row label="Heap" value={megabytes(shown.heap)} />
+ )}
+ </dl>
+ <Button
+ size="sm"
+ className="pointer-events-auto self-start"
+ onClick={resetGpuStats}
+ >
+ Reset
+ </Button>
+ </section>
+ );
+}
diff --git a/src/lib/gpu-stats.ts b/src/lib/gpu-stats.ts
new file mode 100644
index 0000000..d6a0d31
--- /dev/null
+++ b/src/lib/gpu-stats.ts
@@ -0,0 +1,104 @@
+/** What the GPU allocated and freed of one kind of resource since the last reset, and what lives now. */
+type Count = { made: number; freed: number; live: number; bytes: number };
+
+/** Opt in with `?stats`, so the counting never runs for anyone who didn't ask for it. */
+export const statsEnabled = new URLSearchParams(location.search).has("stats");
+
+const count = (): Count => ({ made: 0, freed: 0, live: 0, bytes: 0 });
+const stats = { textures: count(), buffers: count(), peakBytes: 0 };
+
+const texelBytes: Record<string, number> = {
+ r8unorm: 1,
+ r16float: 2,
+ rgba8unorm: 4,
+ "rgba8unorm-srgb": 4,
+ bgra8unorm: 4,
+ r32float: 4,
+ rgba16float: 8,
+ rgba32float: 16,
+};
+
+function textureBytes({ size, format }: GPUTextureDescriptor) {
+ const [width, height = 1, layers = 1] =
+ Symbol.iterator in Object(size)
+ ? [...(size as Iterable<number>)]
+ : [
+ (size as GPUExtent3DDict).width,
+ (size as GPUExtent3DDict).height,
+ (size as GPUExtent3DDict).depthOrArrayLayers,
+ ];
+ return width * height * layers * (texelBytes[format] ?? 4);
+}
+
+/**
+ * Counts every texture and buffer the page creates and frees, wherever in the code, by wrapping
+ * WebGPU's own methods once. A resource the garbage collector takes without `destroy` counts as freed.
+ */
+export function trackGpu() {
+ const sizes = new WeakMap<object, [Count, number]>();
+ const collected = new FinalizationRegistry<[Count, number]>(
+ ([counter, bytes]) => release(counter, bytes),
+ );
+ function track(counter: Count, resource: object, bytes: number) {
+ counter.made++;
+ counter.live++;
+ counter.bytes += bytes;
+ sizes.set(resource, [counter, bytes]);
+ collected.register(resource, [counter, bytes], resource);
+ stats.peakBytes = Math.max(
+ stats.peakBytes,
+ stats.textures.bytes + stats.buffers.bytes,
+ );
+ }
+ function release(counter: Count, bytes: number) {
+ counter.freed++;
+ counter.live--;
+ counter.bytes -= bytes;
+ }
+ function destroyed(resource: object) {
+ const tracked = sizes.get(resource);
+ if (tracked) {
+ sizes.delete(resource);
+ collected.unregister(resource);
+ release(...tracked);
+ }
+ }
+ const { createTexture, createBuffer } = GPUDevice.prototype;
+ const destroyTexture = GPUTexture.prototype.destroy;
+ const destroyBuffer = GPUBuffer.prototype.destroy;
+ GPUDevice.prototype.createTexture = function (descriptor) {
+ const texture = createTexture.call(this, descriptor);
+ track(stats.textures, texture, textureBytes(descriptor));
+ return texture;
+ };
+ GPUDevice.prototype.createBuffer = function (descriptor) {
+ const buffer = createBuffer.call(this, descriptor);
+ track(stats.buffers, buffer, descriptor.size);
+ return buffer;
+ };
+ GPUTexture.prototype.destroy = function () {
+ destroyed(this);
+ destroyTexture.call(this);
+ };
+ GPUBuffer.prototype.destroy = function () {
+ destroyed(this);
+ destroyBuffer.call(this);
+ };
+}
+
+export function readGpuStats() {
+ return {
+ textures: { ...stats.textures },
+ buffers: { ...stats.buffers },
+ peakBytes: stats.peakBytes,
+ };
+}
+
+/** Starts counting a workflow: made and freed from zero, and the peak from what lives now. */
+export function resetGpuStats() {
+ for (const counter of [stats.textures, stats.buffers]) {
+ counter.made = 0;
+ counter.freed = 0;
+ }
+ stats.peakBytes = stats.textures.bytes + stats.buffers.bytes;
+}
diff --git a/src/main.tsx b/src/main.tsx
index eff02d5..594f481 100644
--- a/src/main.tsx
+++ b/src/main.tsx
@@ -5,9 +5,15 @@ import { Component, type ReactNode } from "react";
import { createRoot } from "react-dom/client";
import { GpuProvider } from "vgpu-react";
import { App } from "@/app";
+import { GpuStats } from "@/components/gpu-stats";
import { TextLink } from "@/components/ui/text-link";
+import { statsEnabled, trackGpu } from "@/lib/gpu-stats";
import "./index.css";
+if (statsEnabled) {
+ trackGpu();
+}
+
/** GPU initialization throws during render; without WebGPU the page would otherwise stay blank. */
class GpuBoundary extends Component<
{ children: ReactNode },
@@ -51,6 +57,7 @@ if (root) {
</TooltipProvider>
</GpuProvider>
</GpuBoundary>
+ {statsEnabled && <GpuStats />}
{/* Their scripts load from Vercel's CDN; development and tests stay offline. */}
{import.meta.env.PROD && (
<>
diff --git a/tests/gpu-stats.e2e.ts b/tests/gpu-stats.e2e.ts
new file mode 100644
index 0000000..2c109cd
--- /dev/null
+++ b/tests/gpu-stats.e2e.ts
@@ -0,0 +1,24 @@
+import { expect, test } from "./fixtures";
+
+test("?stats counts the GPU's textures and buffers, and Reset starts a workflow from zero", async ({
+ page,
+}) => {
+ await page.setViewportSize({ width: 1440, height: 1000 });
+ await page.goto("/?stats");
+ await page
+ .locator('input[type="file"]')
+ .setInputFiles("tests/fixtures/photo.svg");
+ const exposure = page.getByRole("textbox", { name: "Exposure", exact: true });
+ await expect(exposure).toHaveValue("0.00");
+ const stats = page.getByRole("region", { name: "GPU stats" });
+ const value = (label: string) =>
+ stats.locator("dt", { hasText: label }).locator("+ dd");
+ await expect(value("Textures")).toHaveText(/^[1-9]\d* live · [1-9]\d* made/);
+ await expect(value("GPU")).toHaveText(/^\d+\.\d MB · peak/);
+ await stats.getByRole("button", { name: "Reset" }).click();
+ await expect(value("Textures")).toHaveText(/ · 0 made · 0 freed/);
+ // A new adjustment pass renders into a texture of its own.
+ await exposure.fill("1");
+ await exposure.press("Enter");
+ await expect(value("Textures")).toHaveText(/ · [1-9]\d* made/);
+});