@ghostty-webgpu/shaderc
v0.0.4
Published
Runtime ShaderToy and Ghostty GLSL compiler for WebGPU and WebGL2
Maintainers
Readme
ghostty-web-shaderc
Compile Ghostty and ShaderToy GLSL strings to WebGPU WGSL and WebGL2 GLSL, at runtime, in the browser or in Node.
import { compileGhosttyShader } from '@ghostty-webgpu/shaderc'
const shader = await compileGhosttyShader(`
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 uv = fragCoord / iResolution.xy;
fragColor = texture(iChannel0, uv) * vec4(1.0, 0.8, 0.6, 1.0);
}
`)
console.log(shader.wgsl.match(/fn mainImage[\s\S]*?\n}/)?.[0])
console.log(shader.webgl)fn mainImage(fragColor: ptr<function, vec4<f32>>, fragCoord: vec2<f32>) {
var fragCoord_1: vec2<f32>;
var uv: vec2<f32>;
fragCoord_1 = fragCoord;
let _e62 = fragCoord_1;
let _e63 = global.iResolution;
uv = (_e62 / _e63.xy);
let _e67 = uv;
let _e68 = textureSample(_ghosttyChannel0Texture, _ghosttyChannel0Sampler, _e67);
(*fragColor) = (_e68 * vec4<f32>(1f, 0.8f, 0.6f, 1f));
return;
}
{
uniformBlock: 'GhosttyUniforms_block_0Fragment',
channel0: '_group_0_binding_1_fs'
}The example prints two things: the mainImage function from shader.wgsl, and shader.webgl, the two names you bind in WebGL2. shader.wgsl is the full program for a WebGPU pipeline. shader.glsl is the full program for a WebGL2 program, and the example does not print it. Ghostty shaders are plain GLSL files, and browsers cannot run them directly. This package is the missing compile step.
- Same source, both APIs. One
mainImageshader becomes WGSL and GLSL ES 3.00 with the same uniform layout and the same coordinates. - Errors point at your lines. A diagnostic reports the line and column in your source, not in the generated wrapper.
- Strict-CSP friendly. Naga compiles the shader inside a WebAssembly module. The page needs
wasm-unsafe-eval, notunsafe-eval. - Compiler only. You keep control of render targets, animation, and GPU resources. ghostty-webgpu is a separate project, and installing this package adds no shader support to it.
The showcase compiles the shader in your browser and shows the input and both outputs. The Matrix shader is by its listed authors, with its license in SHADER_LICENSES.md.
One shader goes in and two programs come out. The renderer on the right is yours to write.
Quick start
The package is not on npm yet. Install the tarball from the v0.0.2 release. You need Node 24.12 or later. That is the engines floor in package.json, CI runs Node 24, and Node 24 runs the .ts examples below without a build step.
Install it.
npm install @ghostty-webgpu/shadercThe same URL installs with
pnpm add,yarn add, andbun add. See Package managers.Save the example above as
demo.mtsand run it.node demo.mtsYou see the output above.
Compile a file from the command line.
cat > shader.glsl <<'EOF' void mainImage(out vec4 fragColor, in vec2 fragCoord) { fragColor = texture(iChannel0, fragCoord / iResolution.xy); } EOF npx --no-install ghostty-web-shaderc shader.glsl --out shader.jsonshader.jsonholdswgsl,glsl, and the WebGL binding names.
Getting started takes this to a working draw call.
Package managers
The tarball install was run end to end, with the demo script and the CLI, on Node 24.22.1 with these tools:
- npm 11.19.1:
npm install <tarball-url> - pnpm 8.11.0:
pnpm add <tarball-url> - Yarn 1.22.22:
yarn add <tarball-url> - Bun 1.4.4:
bun add <tarball-url>
All four worked. Bun also runs the demo (bun demo.mts) and the CLI (bunx --no-install ghostty-web-shaderc). Yarn 2 and later were not tried.
In a browser
Install the same tarball into your web project and import the package from your bundler entry. A Vite 8.3.5 production build of this page emitted the 2.0 MB naga_bg-*.wasm file as an asset, and the compiled shader came back in Chromium:
// main.ts
import { compileGhosttyShader } from '@ghostty-webgpu/shaderc'
const shader = await compileGhosttyShader(`
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
fragColor = texture(iChannel0, fragCoord / iResolution.xy);
}
`)
document.body.textContent = shader.wgslVite is the only bundler tested. Run in the browser covers Web Workers, serving the WASM file yourself, and the CSP.
What you can do
- Draw a compiled shader with WebGPU
- Draw a compiled shader with WebGL2
- Bundle the compiler for a page or a Web Worker
- Compile ahead of time and ship JSON
- See five shaders on a Ghostty terminal
- Find out why a shader fails
How it works
compileGhosttyShader wraps your mainImage in a generated GLSL prefix and suffix. The prefix declares the Ghostty uniform block and the iChannel0 texture. Naga parses the result, validates it, and writes WGSL and GLSL ES 3.00. The package then checks that the output matches the fixed binding contract and maps every diagnostic back to your source lines. See How it works.
Docs
- Tutorial: Getting started
- Guides: docs/guides/
- Concepts: How it works
- Reference: API · CLI · Shader contract · Uniform layout
- Compatibility · Troubleshooting · Development · Decisions
Status
Pre-1.0 (0.0.x), experimental. The API and the ghostty-v1 uniform layout can change between patch releases.
- Not on npm. The installable artifact is the GitHub release tarball.
- Supports one ShaderToy image pass with
iChannel0. No buffer passes, audio, video, or extra channels. - Compatibility covers the tested fixtures and pixel tests, not every GLSL extension. A shader that runs in desktop Ghostty can fail here. See Compatibility.
- Tested in CI on Chromium (software GPU), Node, and Bun. iOS Safari 26.5 was checked by hand on the WebGL2 path. Firefox and desktop Safari are untested.
License
MIT. Bundled example shaders keep their own licenses, listed in examples/showcase/SHADER_LICENSES.md. See also THIRD_PARTY_NOTICES.md.
