Skip to content

Welcome to FragmentColor!

FragmentColor is a universal GPU abstraction library where you can simply write some WGSL or GLSL code, forget about manual pipeline setup, set uniforms by key-value, and it will render on every device in existence:

let shader = Shader::new(wgsl)?;
shader.set("my_uniform", 0.5)?; // reflected from source
renderer.render(&shader, &target)?;

And that’s it.

It runs on Vulkan, Metal, DirectX 12, OpenGL, WebGL2, and WebGPU through wgpu. See Platform Support for details.

  • The shader is the source of truth. Uniforms, textures, samplers, storage buffers, and push constants come from the shader itself. shader.set("light.color", [1.0, 0.8, 0.6]) writes the field it names.
  • One API in five languages. Rust, JavaScript, Python, Swift, and Kotlin share the same calls.
  • A shader library built in. Over 200 WGSL functions for noise, distance fields, color, lighting, and post effects compose into your shader by name.
  • Passes and compute. Chain render and compute passes, render into textures, and read the results back.
  • Native surfaces. A window on desktop, a <canvas> on the web, a CAMetalLayer on iOS, and a Surface on Android.

In the playground, the canvas rebuilds as you type, the sliders come from the shader’s own uniforms, and every demo shows the same program in all five languages.

The API Reference covers every object and method, and the project lives on GitHub.

Terminal window
npm install fragmentcolor
import init, { Renderer, Shader, Pass } from "fragmentcolor";
async function start() {
// Initializes the WASM module
await init();
// Initializes a renderer and a target compatible with the given canvas
const canvas = document.getElementById("my-canvas");
const renderer = new Renderer();
const target = await renderer.createTarget(canvas);
// A source string builds at once. A file path or URL loads with Shader.fetch:
const circle = await Shader.fetch("./path/to/circle.wgsl");
const triangle = await Shader.fetch("https://fragmentcolor.org/shaders/triangle.wgsl");
const shader = new Shader(`
28 collapsed lines
struct VertexOutput {
@builtin(position) coords: vec4<f32>,
}
struct MyStruct {
my_field: vec3<f32>,
}
@group(0) @binding(0)
var<uniform> my_struct: MyStruct;
@group(0) @binding(1)
var<uniform> my_vec2: vec2<f32>;
@vertex
fn vs_main(@builtin(vertex_index) in_vertex_index: u32) -> VertexOutput {
const vertices = array(
vec2( -1., -1.),
vec2( 3., -1.),
vec2( -1., 3.)
);
return VertexOutput(vec4<f32>(vertices[in_vertex_index], 0.0, 1.0));
}
@fragment
fn fs_main() -> @location(0) vec4<f32> {
return vec4<f32>(my_struct.my_field, 1.0);
}
`);
// The library binds and updates the uniforms automatically
shader.set("my_struct.my_field", [0.1, 0.8, 0.9]);
shader.set("my_vec2", [1.0, 1.0]);
// One shader is all you need to render
renderer.render(circle, target);
// But you can also combine multiple shaders in a render Pass
const rpass = new Pass("single pass");
rpass.addShader(circle);
rpass.addShader(triangle);
rpass.addShader(shader);
renderer.render(rpass, target);
// Finally, multiple passes can run in one frame: pass an array, or use
// Pass.require(...) to declare dependencies and let the renderer order them.
const guiPass = new Pass("GUI pass");
renderer.render([rpass, guiPass], target);
// To animate, simply update the uniforms in a loop
function animate() {
circle.set("position", [0.0, 0.0]);
renderer.render([rpass, guiPass], target);
requestAnimationFrame(animate);
}
animate();
}
start();