Skip to content

Commit 553d544

Browse files
authored
Merge pull request #34 from NewKrok/claude/webgpu-compute-support-3kCxi
docs: add WebGPU compute support implementation plan
2 parents a8e300f + eec1e1a commit 553d544

97 files changed

Lines changed: 31427 additions & 1007 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.claude/CLAUDE.md

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,24 @@ src/js/effects/three-particles/
2121
├── three-particles-utils.ts # Shape generators, value resolution, texture
2222
├── three-particles-enums.ts # SimulationSpace, Shape, EmitFrom, etc.
2323
├── three-particles-forces.ts # Force fields and attractors
24+
├── three-particles-collision.ts # CPU collision plane logic
25+
├── three-particles-renderer-detect.ts # WebGPU renderer detection
2426
├── three-particles-serialization.ts # Config save/load serialization
2527
├── types.ts # Complete TypeScript type definitions
26-
└── shaders/ # GLSL shaders (points, instanced, trail, mesh)
28+
├── shaders/ # GLSL shaders (points, instanced, trail, mesh)
29+
└── webgpu/ # WebGPU compute + TSL materials
30+
├── tsl-materials.ts # TSL material factory
31+
├── tsl-shared.ts # Shared TSL nodes (texture animation, soft particles)
32+
├── tsl-point-sprite-material.ts # POINTS renderer TSL material
33+
├── tsl-instanced-billboard-material.ts # INSTANCED renderer TSL material
34+
├── tsl-mesh-particle-material.ts # MESH renderer TSL material
35+
├── tsl-trail-ribbon-material.ts # TRAIL renderer TSL material
36+
├── compute-particle-update.ts # Core physics compute shader
37+
├── compute-modifiers.ts # All 7 modifiers compute shader
38+
├── compute-force-fields.ts # Force field GPU compute
39+
├── compute-collision-planes.ts # Collision plane GPU compute
40+
├── curve-bake.ts # Curve baking (bezier/easing → Float32Array)
41+
└── tsl-noise.ts # 3D simplex noise in TSL
2742
```
2843

2944
**Tests:** `src/__tests__/*.test.ts`
@@ -103,16 +118,42 @@ See [Development Workflow](doc/workflow.md) for the full step-by-step guide.
103118

104119
---
105120

121+
## WebGPU Compute — Development Guide
122+
123+
The library supports a **dual-path architecture**: WebGL (CPU simulation + GLSL) and WebGPU (GPU compute simulation + TSL materials). See [Architecture](doc/architecture.md) for the full technical deep-dive.
124+
125+
### Key Design Decisions
126+
127+
- **Additive, non-breaking:** All WebGPU code is in `webgpu/` and the separate `src/webgpu.ts` entry point. The WebGL path is never modified.
128+
- **Opt-in registration:** Users call `registerTSLMaterialFactory()` to enable WebGPU. If not called, only GLSL shaders are used.
129+
- **Duck-typed detection:** `isComputeCapableRenderer()` checks for `.compute()` and `.hasFeature()` methods — no hard dependency on `THREE.WebGPURenderer`.
130+
- **Single compute dispatch:** All modifiers run in one GPU compute pass (compile-time branching via TSL, not runtime branching).
131+
- **Collision planes on GPU:** Collision planes use the same dual-path pattern as force fields — CPU logic in `three-particles-collision.ts`, GPU compute in `webgpu/compute-collision-planes.ts`. Plane data is encoded into a packed Float32Array and uploaded as a uniform buffer.
132+
- **Curve baking:** Lifetime curves are pre-baked to 256-sample Float32Arrays at system creation, stored in the `curveData` buffer.
133+
- **Sub-emitters forced to CPU:** Sub-emitters always use `SimulationBackend.CPU` because they need CPU-side death detection callbacks.
134+
- **Trail always CPU:** `RendererType.TRAIL` uses CPU simulation regardless of backend setting.
135+
136+
### When Working on WebGPU Code
137+
138+
- **TSL materials** (`webgpu/tsl-*.ts`): Each renderer type has its own TSL material. They detect GPU compute via the `gpuCompute` flag — when true, data comes from packed vec4 storage buffers; when false, from individual float attributes.
139+
- **Compute shaders** (`webgpu/compute-*.ts`): Written in TSL (compiles to WGSL). Core physics, all 7 modifiers, and force fields run in a single dispatch.
140+
- **Storage buffers:** 8 vec4 bindings per system. Layout documented in [Architecture](doc/architecture.md#storage-buffer-layout).
141+
- **Testing:** WebGPU-specific tests are in `src/__tests__/three-particles-webgpu-integration.test.ts`. Use mock factories (no real WebGPU context in Jest).
142+
- **Imports:** `three/tsl` and `three/webgpu` are external — never imported from the main entry point. They're only used inside `webgpu/` files.
143+
144+
---
145+
106146
## Detailed Documentation
107147

108148
Detailed guides are in `.claude/doc/` — read these on-demand, not loaded into every conversation:
109149

110150
| Document | When to read |
111151
|----------|-------------|
112-
| [Architecture](doc/architecture.md) | Understanding internal data flow, shader pipeline, module responsibilities |
152+
| [Architecture](doc/architecture.md) | Understanding internal data flow, shader pipeline, WebGPU dual-path architecture, storage buffer layout |
113153
| [CI/CD Pipeline](doc/ci-cd.md) | Release process, workflow troubleshooting, version bump logic |
114154
| [Development Workflow](doc/workflow.md) | Step-by-step workflow, agent orchestration pattern, pre-commit checks |
115155
| [Testing Guide](doc/testing.md) | Mocking patterns, test helpers, coverage targets, writing effective tests |
156+
| [WebGPU Compute Plan](doc/webgpu-compute-plan.md) | Original implementation plan (all 6 phases completed) — useful as historical reference for design decisions |
116157

117158
---
118159

@@ -125,6 +166,7 @@ Detailed guides are in `.claude/doc/` — read these on-demand, not loaded into
125166
| Enums & constants | `src/js/effects/three-particles/three-particles-enums.ts` |
126167
| Modifiers | `src/js/effects/three-particles/three-particles-modifiers.ts` |
127168
| Force fields | `src/js/effects/three-particles/three-particles-forces.ts` |
169+
| Collision planes | `src/js/effects/three-particles/three-particles-collision.ts` |
128170
| Serialization | `src/js/effects/three-particles/three-particles-serialization.ts` |
129171
| Curves / Bezier | `three-particles-curves.ts`, `three-particles-bezier.ts` |
130172
| Utilities | `src/js/effects/three-particles/three-particles-utils.ts` |
@@ -138,8 +180,8 @@ Detailed guides are in `.claude/doc/` — read these on-demand, not loaded into
138180

139181
## Project Status
140182

141-
Completed: Core system, all shape emitters, lifetime modifiers, noise, burst/distance emission, texture sheet animation, world/local space, sub-emitters, force fields, GPU instancing, trail renderer (with smoothing, adaptive sampling, maxTime, twist prevention, connected ribbons), mesh renderer, soft particles, serialization, TypeDoc, visual editor, examples page, CI/CD, benchmark suite, React Three Fiber docs, llms.txt, real-time config updates (`updateConfig`).
183+
Completed: Core system, all shape emitters, lifetime modifiers, noise, burst/distance emission, texture sheet animation, world/local space, sub-emitters, force fields, collision planes (kill/clamp/bounce), GPU instancing, trail renderer (with smoothing, adaptive sampling, maxTime, twist prevention, connected ribbons), mesh renderer, soft particles, serialization, TypeDoc, visual editor, examples page, CI/CD, benchmark suite, React Three Fiber docs, llms.txt, real-time config updates (`updateConfig`), WebGPU compute support (TSL shaders, GPU physics, modifiers, force fields, collision planes, noise, curve baking).
142184

143-
**Planned:** Trail Phase 2 (UV texture modes, UV scrolling, width by speed), WebGPU compute, preset system.
185+
**Planned:** Trail Phase 2 (UV texture modes, UV scrolling, width by speed), preset system.
144186

145187
**Coverage target:** >=90% statement, >=80% branch.

0 commit comments

Comments
 (0)