TypeGPU Documentation

repository·main·Indexed 25 days ago

https://github.com/software-mansion/typegpu

A modular toolkit and thin layer between JavaScript and WebGPU/WGSL that provides advanced type inference, allowing developers to write shaders directly in TypeScript. The ecosystem includes a CLI for project scaffolding, an ESLint plugin, and specialized libraries such as @typegpu/react for React integration, @typegpu/noise for pseudo-random functions, and @typegpu/radiance-cascades for 2D radiance cascades.

Tokens
76.6K
Snippets
257
Records
390
Agent score
84%

What's inside TypeGPU

  1. Overview of TypeGPU packages

    main

    The TypeGPU monorepo consists of several specialized packages:

    Core & Helpers

    • packages/typegpu: The core library.
    • packages/typegpu-color: Color helper functions for WebGPU/TypeGPU.
    • packages/typegpu-noise: Noise and pseudo-random functions.

    Tooling

    • packages/unplugin-typegpu: A bundler plugin to enable writing shader functions in JS.
    • packages/tgpu-gen: A CLI tool for automatic TypeGPU code generation.
  2. Determine when to use TypeGPU

    main

    TypeGPU is a collection of low-level, type-safe primitives designed for building custom GPU-accelerated solutions.

    Use TypeGPU if you are building:

    • GPU-accelerated simulations
    • Custom 3D renderers
    • AI inference for proprietary models
    • Custom frameworks or building blocks for existing solutions

    Use a rendering framework (like Three.js) if you are building:

    • Standard interactive 3D models for websites
  3. Use @typegpu/sdf for shader-side shape logic

    main

    The @typegpu/sdf package provides signed distance functions (SDFs) and helpers for TypeGPU and WebGPU projects. It is designed for shader-side logic such as UI masks, ray-marched scenes, collision checks, and texture-based SDF generation.

    Key Concepts:

    • SDF Output: A negative value indicates being inside the shape, 0 is the boundary, and a positive value is outside.
    • Transformations: Shape primitives are centered at the origin. To move, scale, or rotate a shape, apply the inverse transform to the point passed into the SDF.
    • Integration: SDF helpers can be called from TypeGPU functions (tgpu.fn) just like any other TypeGPU resource; resolving the function automatically includes the SDF code and its dependencies.
    import { tgpu, d, std } from 'typegpu';
    import * as sdf from '@typegpu/sdf';
    
    const renderRoundedBox = tgpu.fn([d.vec2f], d.vec4f)((uv) => {
      'use gpu';
      const p = uv - 0.5;
      const dist = sdf.sdRoundedBox2d(p, d.vec2f(0.26, 0.12), 0.04);
      const edge = std.max(std.fwidth(dist), 0.001);
      const alpha = 1 - std.smoothstep(-edge, edge, dist);
      const glow = std.exp(-std.abs(dist) * 28) * 0.2;
    
      const bg = std.mix(d.vec3f(0.93, 0.95, 0.97), d.vec3f(0.74, 0.82, 0.95), uv.y);
      const fill = d.vec3f(0.08, 0.08, 0.1);
      const color = std.mix(bg, fill, alpha) + d.vec3f(1, 0.55, 0.28) * glow;
    
      return d.vec4f(std.min(color, d.vec3f(1)), 1);
    });
  4. Understand how 'use gpu' works with TypeGPU

    main

    TypeGPU uses a build-time plugin (unplugin-typegpu) to transform JavaScript/TypeScript code into WebGPU-compatible WGSL. When you mark a function with the 'use gpu' directive, the plugin performs three main transformations:

    1. Auto-naming: It uses globalThis.__TYPEGPU_AUTONAME__ to assign names to resources (like buffers or constants) based on their variable identifiers, which helps in generating readable WGSL.
    2. Function metadata assignment: It attaches an AST (Abstract Syntax Tree) and a set of externals (captured variables from the outer scope) to the function via globalThis.__TYPEGPU_META__.
    3. Operator overloading: It replaces standard operators with specialized functions (e.g., __tsover_add) to handle GPU-specific math operations.

    You can preview the generated WGSL code using tgpu.resolve([pipeline]) or by inspecting the transformed code during development.

    import { tgpu, d } from 'typegpu';
    
    const root = await tgpu.init();
    const counter = root.createMutable(d.vec2u);
    
    const increment = tgpu.computeFn({ workgroupSize: [1] })(() => {
      'use gpu';
      counter.$ += 1;
    });
    
    const incrementPipeline = root.createComputePipeline({ compute: increment });
    
    // Preview the generated WGSL
    console.log(tgpu.resolve([incrementPipeline]));
  5. Understand the MediaPipe Selfie Segmentation Model asset

    main

    The selfie-segmentation directory contains a third-party machine learning model used by the TypeGPU selfie segmentation example. This model is the MediaPipe Selfie Segmentation (general, 256×256) authored by Google LLC.

    Key Files:

    • selfie_segmenter.ssbin: The model weights repackaged into the .ssbin binary format required by TypeGPU. The weights and architecture are identical to the original MediaPipe model.
    • LICENSE: Apache License, Version 2.0.
    • NOTICE: Attribution and modification notices.

    Licensing Note: This model is not part of the TypeGPU core library. While TypeGPU is licensed under the MIT License, this specific asset is licensed under Apache License, Version 2.0. Any redistribution of this model must preserve the LICENSE and NOTICE files.