Skip to content

Runtime Reference

This page documents the runtime-facing modules that power the demo. Test-only verification helpers are intentionally excluded; see the compute-shader notes for the CPU mirrors used to validate WGSL behavior.

Configuration

Runtime constants and shader preambles live in src/config/sim.ts.

ConstantTypeDefaultDescription
PARTICLE_COUNTnumber10000Default number of particles
PARTICLE_SIZEnumber16Bytes per particle (4 floats)
WORKGROUP_SIZEnumber64Compute shader workgroup size
GRAVITYVec2{x: 0, y: 600}Gravity acceleration (px/s²)
DAMPINGnumber0.9Velocity damping on bounce
REPULSION_RADIUSnumber200Mouse influence radius (px)
REPULSION_STRENGTHnumber3000Mouse repulsion force
MAX_SPEEDnumber800Maximum velocity (px/s)
TRAIL_FADE_ALPHAnumber0.05Trail fade per frame
typescript
function buildComputeShaderPreamble(): string;
function buildRenderShaderPreamble(): string;
function buildTrailShaderPreamble(): string;

These helpers inject TypeScript-owned constants into WGSL at pipeline creation time.

Shared Types

src/types.ts holds the shared contracts between the runtime, shader setup, and tests.

typescript
interface Particle {
  x: number;
  y: number;
  vx: number;
  vy: number;
}

interface Vec2 {
  x: number;
  y: number;
}

interface Uniforms {
  width: number;
  height: number;
  mouseX: number;
  mouseY: number;
  deltaTime: number;
  _pad1: number;
  _pad2: number;
  _pad3: number;
}

interface WebGPUContext {
  adapter: GPUAdapter;
  device: GPUDevice;
  context: GPUCanvasContext;
  format: GPUTextureFormat;
  canvas: HTMLCanvasElement;
}

Runtime Modules

Application bootstrap (src/main.ts)

The bootstrap path is intentionally thin:

  1. size the canvas,
  2. initialize WebGPU,
  3. resolve adaptive quality,
  4. create GPU resources,
  5. attach input and overlays,
  6. start the renderer.

Canvas sizing (src/core/canvas.ts)

typescript
function setupCanvas(canvas: HTMLCanvasElement): void;

Keeps the canvas synced to the viewport and device pixel ratio.

WebGPU initialization (src/core/webgpu.ts)

typescript
async function initWebGPU(canvas: HTMLCanvasElement): Promise<WebGPUContext>;
function reconfigureContext(ctx: WebGPUContext): void;

Owns adapter/device acquisition, canvas context configuration, and resize reconfiguration.

Runtime quality (src/core/quality.ts)

typescript
type SimulationQualityTier = 'low' | 'medium' | 'high';

interface RuntimeSimulationSettings {
  particleCount: number;
  qualityTier: SimulationQualityTier;
  scale: number;
}

function resolveSimulationSettings(
  input: SimulationHeuristicsInput,
  preferredParticleCount?: number
): RuntimeSimulationSettings;
function readRuntimeHeuristics(adapter: GPUAdapter, device: GPUDevice): SimulationHeuristicsInput;

Derives a safe particle budget from the current device and viewport.

GPU resource assembly (src/core/simulation-resources.ts)

typescript
interface SimulationResources {
  buffers: ParticleBuffers;
  pipelines: Pipelines;
}

function createSimulationResources(
  device: GPUDevice,
  format: GPUTextureFormat,
  canvasSize: Vec2,
  particleCount?: number
): SimulationResources;
function destroySimulationResources(resources: SimulationResources): void;

Creates the particle buffers, uniform buffer, pipelines, and bind groups as one coherent unit.

Frame orchestration (src/core/renderer.ts)

typescript
class Renderer {
  start(): void;
  stop(): void;
  destroy(): void;
}

function createRenderer(
  ctx: WebGPUContext,
  pipelines: Pipelines,
  buffers: ParticleBuffers,
  getMousePosition: () => Vec2,
  onFrame?: () => void
): Renderer;

Owns the render loop, offscreen trail texture lifecycle, and GPU submission.

Input and overlays (src/core/input.ts, src/core/app-shell.ts)

These modules stay intentionally simple:

  • createMouseHandler() tracks pointer/touch position in canvas space.
  • createFPSCounter(), createInfoOverlay(), createLoadingIndicator(), and showError() own the minimal DOM shell around the demo.

Data Layouts

Particle Buffer

OffsetSizeFieldType
04xf32
44yf32
84vxf32
124vyf32

Uniform Buffer

OffsetSizeFieldType
04widthf32
44heightf32
84mouseXf32
124mouseYf32
164deltaTimef32
20-2812_padf32

Built with VitePress