Skip to content

System Architecture

This page describes the architecture and design decisions for a high-performance particle fluid simulation using WebGPU.

Overview

The system leverages GPU parallel computing via Compute Shaders to achieve real-time physics simulation for thousands of particles. The architecture follows a heterogeneous computing model where CPU handles orchestration and GPU handles parallel computation.

CPU-GPU Architecture

Four-Stage Render Pipeline

The simulation runs through four distinct GPU passes each frame:

1. Compute Pass

Parallel physics simulation for all particles:

Workgroup Configuration:

ParameterValuePurpose
workgroup_size64Optimal for most GPU architectures
Dispatchceil(particleCount / 64)One thread per particle

2. Trail Pass

Fades the persistent offscreen texture:

  • Draws a fullscreen quad
  • Alpha blending with TRAIL_FADE_ALPHA = 0.05
  • Creates the motion trail effect

3. Render Pass

Draws particles to offscreen texture:

  • Point primitives (one per particle)
  • Velocity-to-color mapping
  • HiDPI-aware scaling

4. Present Pass

Composites to the screen:

  • Samples offscreen texture
  • Bilinear filtering for smoothness
  • Outputs to swapchain

Data Layouts

Particle Buffer

Each particle: 16 bytes (4 × float32)

FieldTypeDescription
xf32Position X (pixels)
yf32Position Y (pixels)
vxf32Velocity X (px/s)
vyf32Velocity Y (px/s)

Uniform Buffer

Total: 32 bytes (8 × float32)

OffsetFieldTypePurpose
0widthf32Canvas width
4heightf32Canvas height
8mouseXf32Mouse X position
12mouseYf32Mouse Y position
16deltaTimef32Frame time
20-28_padf32Alignment padding

Key Design Decisions

DecisionRationale
Offscreen trail textureMore portable than swapchain persistence
Shared constants via preambleSingle source of truth for TypeScript/WGSL
CPU reference implementationsEnables property testing of GPU logic
Adaptive particle countGraceful degradation on low-end devices
Frame-rate independent physicsConsistent simulation across refresh rates

Frame Budget

MetricTargetNotes
Frame time< 16ms60 FPS target
Compute pass~2-4msPhysics for 10K particles
Render pass~1-2msPoint rendering
CPU overhead< 1msUniform updates, frame orchestration

Source Files

ModulePathPurpose
WebGPU Initsrc/core/webgpu.tsGPU initialization
Bufferssrc/core/buffers.tsMemory management
Physicssrc/core/physics.tsCPU reference
Pipelinessrc/core/pipelines.tsGPU pipeline creation
Renderersrc/core/renderer.tsFrame orchestration
Qualitysrc/core/quality.tsAdaptive scaling

Next Steps

Built with VitePress