TypeGPU Documentation
repository·main·Indexed 25 days ago
https://github.com/software-mansion/typegpuA 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.
What's inside TypeGPU
- tinyest (Tiny Embeddable Syntax Tree) is a compact, fast, and embeddable JavaScript Abstract Syntax Tree (AST) designed for transpilation tasks. It is used as a foundational component in projects like TypeGPU.
Overview of TypeGPU
mainTypeGPU is a modular and open-ended toolkit for WebGPU. It provides advanced type inference and allows developers to write shaders directly in TypeScript. This enables code that can be executed in JavaScript for logic/calculations and also used to generate WGSL for GPU execution.Overview of unplugin-typegpu features
mainThe
unplugin-typegpupackage provides bundler plugins that enhance TypeGPU with the following capabilities:- JavaScript/TypeScript shader support: Enables the use of the
'use gpu'directive. - Improved debugging: Provides automatic naming of resources to make debugging easier.
- JavaScript/TypeScript shader support: Enables the use of the
Overview of tinyest-for-wgsl
maintinyest-for-wgslis a utility that transforms JavaScript function bodies into a 'tinyest' embeddable syntax tree. This syntax tree is used to generate equivalent or near-equivalent WGSL (WebGPU Shading Language) code. It is a core component used by TypeGPU to enable writing shaders in JavaScript.Overview of TypeGPU packages
mainThe 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.
Determine when to use TypeGPU
mainTypeGPU 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
Use @typegpu/color for WebGPU/TypeGPU color helpers
main@typegpu/color provides a set of color helper functions specifically designed for use in WebGPU or TypeGPU applications.Use @typegpu/sdf for shader-side shape logic
mainThe
@typegpu/sdfpackage 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,
0is 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); });- SDF Output: A negative value indicates being inside the shape,
Understand how 'use gpu' works with TypeGPU
mainTypeGPU 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:- 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. - Function metadata assignment: It attaches an AST (Abstract Syntax Tree) and a set of
externals(captured variables from the outer scope) to the function viaglobalThis.__TYPEGPU_META__. - 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]));- Auto-naming: It uses
Understand the MediaPipe Selfie Segmentation Model asset
mainThe
selfie-segmentationdirectory 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.ssbinbinary 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
LICENSEandNOTICEfiles.Use tgpu-gen for automatic TypeGPU code generation
mainThetgpu-genpackage is a CLI tool designed to automate the generation of TypeGPU code. For complete usage instructions, command flags, and configuration details, refer to the official Generator CLI chapter in the TypeGPU documentation.Use @typegpu/geometry for WebGPU/TypeGPU applications
main@typegpu/geometry provides a collection of geometry helper functions designed specifically for use within WebGPU or TypeGPU applications.