Skip to content
/ webgpu-template Public template

A lightweight WebGPU demo project that renders a simple triangle using the WebGPU API. This template provides a minimal setup for getting started with WebGPU development in TypeScript.

License

Notifications You must be signed in to change notification settings

vancura/webgpu-template

Repository files navigation

WebGPU Template

CI License: ISC pnpm TypeScript WebGPU

A minimal starter template for WebGPU projects using TypeScript. Get up and running with modern GPU programming on the web in minutes.

Features

  • WebGPU ready β€” Pre-configured with @webgpu/types for full TypeScript support
  • Modern tooling β€” Vite for fast development with HMR
  • Strict TypeScript β€” Comprehensive type checking enabled
  • Code quality β€” ESLint, Prettier, and Husky pre-configured
  • Release workflow β€” Changesets for versioning and publishing

Prerequisites

  • Node.js v20 or higher (LTS recommended)
  • pnpm v10.24.0 or higher
  • A WebGPU-compatible browser:
    • Chrome/Edge 113+ (Windows, macOS, Linux, Android)
    • Firefox Nightly (with dom.webgpu.enabled in about:config)
    • Safari 18+ (macOS/iOS)

Quick Start

# Clone the repository
git clone https://github.com/ambilab/webgpu-template.git
cd webgpu-template

# Install dependencies
pnpm install

# Start development server
pnpm dev

Open your browser at http://localhost:5173 to see a WebGPU-rendered triangle.

Scripts

Command Description
pnpm dev Start dev server with HMR
pnpm build Type-check and build for production
pnpm preview Preview the production build
pnpm lint Run ESLint
pnpm lint:fix Run ESLint with auto-fix
pnpm format Format code with Prettier
pnpm format:check Check formatting without changes
pnpm typecheck Run TypeScript type checking
pnpm clean Remove dist and cache directories
pnpm changeset Create a changeset for version bump
pnpm version:bump Bump version based on changesets
pnpm release Build library and publish to npm

Project Structure

webgpu-template/
β”œβ”€β”€ src/
β”‚   └── main.ts           # WebGPU demo entry point
β”œβ”€β”€ index.html            # HTML template
β”œβ”€β”€ package.json
β”œβ”€β”€ tsconfig.json         # TypeScript configuration
β”œβ”€β”€ vite.config.ts        # Vite configuration
β”œβ”€β”€ eslint.config.js      # ESLint flat config
β”œβ”€β”€ prettier.config.js    # Prettier configuration
└── commitlint.config.js  # Commit message linting

How It Works

The template includes a minimal WebGPU demo that:

  1. Checks WebGPU support β€” Displays a helpful error if unavailable
  2. Initializes the GPU β€” Requests adapter and device
  3. Configures the canvas β€” Sets up the WebGPU context
  4. Creates shaders β€” Vertex and fragment shaders in WGSL
  5. Builds a pipeline β€” Configures the render pipeline
  6. Renders a triangle β€” Clears to dark gray, draws a blue triangle
// Example: Creating a shader module
const vertexShaderModule = device.createShaderModule({
  label: 'Vertex Shader',
  code: `
        @vertex
        fn main(@builtin(vertex_index) vertexIndex: u32) -> @builtin(position) vec4f {
            var positions = array<vec2f, 3>(
                vec2f(0.0, 0.5),
                vec2f(-0.5, -0.5),
                vec2f(0.5, -0.5)
            );
            return vec4f(positions[vertexIndex], 0.0, 1.0);
        }
    `,
});

Browser Compatibility

Browser Version Status
Chrome/Edge 113+ Enabled by default
Firefox Nightly Enable dom.webgpu.enabled
Safari 18+ Enabled by default

The demo displays a user-friendly error message if WebGPU is not supported.

Technologies

  • WebGPU β€” Modern GPU API for the web
  • TypeScript β€” Type-safe JavaScript
  • Vite β€” Fast build tool with HMR
  • WGSL β€” WebGPU Shading Language

Resources

License

ISC

About

A lightweight WebGPU demo project that renders a simple triangle using the WebGPU API. This template provides a minimal setup for getting started with WebGPU development in TypeScript.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •