Skip to content

Run the ASCII art playground in real time with WebGPU - #123

Merged
adrianmg merged 4 commits into
masterfrom
adrianmg-webgpu-realtime-ascii-sandbox
Nov 2, 2026
Merged

adrianmg merged 4 commits into
masterfrom
adrianmg-webgpu-realtime-ascii-sandbox

Conversation

@adrianmg

@adrianmg adrianmg commented Nov 2, 2026

Copy link
Copy Markdown
Owner

The ASCII art playground now fits the picture with a WebGPU compute shader instead of once per change on the CPU. It's fast enough to redo the whole picture every frame, so it can also take a live camera or a video.

What changed

  • ascii-lab-gpu.js (new): compute-only WebGPU pipeline with three passes:
    • A brightness histogram and its 2%/98% levels for the contrast stretch.
    • One 64-lane workgroup per cell: blur, try all 48 marks in parallel, reduce to the lowest error, then fit RGB and brightness for the winner.
    • The cells are read back to the page.
  • ascii-lab.js: fitted cells are drawn by the same 2D canvas code for both GPU and CPU, so text looks identical. Browsers without WebGPU (or after a GPU failure or device loss) keep the original CPU fit, for still pictures only.
  • Live sources: a camera tile (mirrored, shown only when WebGPU is available) and video uploads (accepted once WebGPU is ready). Rendering pauses when the playground is off screen.
  • Tenth sample: Friedrich's Wanderer above the Sea of Fog (public domain), so the camera and upload tiles get their own row.
  • Post: two sentences about running on the GPU in real time.

Verification

  • npm run build passes.
  • Headless Chrome with WebGPU:
    • GPU and CPU output match closely (mean pixel difference 0.58/255; a couple of cells differ from the histogram-based stretch).
    • A fake camera holds 60 fps at 120 columns.
    • Switching from the camera back to a sample works.
    • A simulated GPU failure falls back to the CPU fit with no page errors.
  • A rubber-duck review found no blocking issues. Its four non-blocking findings are fixed in the last commit: stale fits, swallowed GPU errors, a video picked during WebGPU startup, and device-loss handling.

Adrian Mato and others added 4 commits November 2, 2026 10:42
Fit every cell in a WebGPU compute shader each frame, draw the result in a
fragment pass, and add camera and video sources. The CPU fit stays as the
fallback where WebGPU isn't available.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Draw the glyph atlas both light-on-dark and dark-on-light and blend by the
mark's brightness, since browsers draw the two at different weights. Add
Wanderer above the Sea of Fog so the camera and upload tiles get their own row.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Keep WebGPU for the fit, read the cells back each frame, and draw them with
the same 2D canvas code as the CPU path so text renders identically.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Ignore fits started before the picture or settings changed, fall back to the
CPU fit when a GPU fit fails or the device is lost (stopping any live
source first), check texture limits, and accept videos only once WebGPU is
ready.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@adrianmg
adrianmg merged commit fd2c47c into master Nov 2, 2026
6 checks passed
@adrianmg
adrianmg deleted the adrianmg-webgpu-realtime-ascii-sandbox branch November 2, 2026 21:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant