-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmodule.ts
More file actions
423 lines (395 loc) · 11.6 KB
/
Copy pathmodule.ts
File metadata and controls
423 lines (395 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
import { Particle } from "./particle";
import { View } from "./view";
/**
* Module descriptors and base Module class
*
* Defines the type-level contract for modules and the DSL surface used by the
* WGSL builders. A `Module` instance provides a `descriptor()` which declares:
* - role: `system`, `force`, or `render`
* - bindings: uniform fields exposed to CPU and populated into GPU uniform buffers
* - for system/force modules: optional global/state/apply/constrain/correct hooks
* - for render modules: one or more passes (fullscreen or compute) with their bindings
*
* The base `Module` offers uniform writer/reader plumbing and enabled toggling,
* and module authors extend it to implement their descriptor and any runtime API.
*/
export enum ModuleRole {
Force = "force",
Render = "render",
}
export enum DataType {
NUMBER = "number",
ARRAY = "array",
}
export abstract class Module<
Name extends string = string,
Inputs extends Record<string, number | number[]> = Record<
string,
number | number[]
>,
StateKeys extends string = any
> {
abstract readonly name: Name;
abstract readonly role: ModuleRole;
abstract readonly inputs: { [K in keyof Inputs]: DataType };
private _state: Partial<Inputs> = {};
private _writer:
| ((values: Partial<Inputs & { enabled: number }>) => void)
| null = (values: Partial<Inputs>) => {
for (const key of Object.keys(values)) {
const val = values[key as keyof Inputs];
if (typeof val === "number") {
this._state[key as keyof Inputs] = val as Inputs[keyof Inputs];
} else if (Array.isArray(val)) {
this._state[key as keyof Inputs] = [...val] as Inputs[keyof Inputs];
}
}
};
private _reader: (() => Partial<Inputs>) | null = () => {
return { ...this._state };
};
private _enabled: boolean = true;
attachUniformWriter(
writer: (values: Partial<Record<string, number | number[]>>) => void
): void {
const values = this.read();
this._writer = writer;
writer({ ...values, enabled: this._enabled ? 1 : 0 });
}
attachUniformReader(reader: () => Partial<Inputs>): void {
this._reader = reader;
}
public write(values: Partial<Inputs>): void {
// Binding keys are narrowed by the generic; cast to the writer's accepted shape
this._writer?.(values as unknown as Partial<Inputs & { enabled: number }>);
}
public read(): Partial<Inputs> {
const vals = this._reader?.() as unknown as Partial<Inputs>;
return vals || {};
}
public readValue(key: keyof Inputs | "enabled"): number {
const vals = this._reader?.() as unknown as Partial<
Record<keyof Inputs | "enabled", number | number[]>
>;
const val = vals[key];
return typeof val === "number" ? val : 0;
}
public readArray(key: keyof Inputs | "enabled"): number[] {
const vals = this._reader?.() as unknown as Partial<
Record<keyof Inputs | "enabled", number | number[]>
>;
const val = vals[key];
return Array.isArray(val) ? val : [];
}
isEnabled(): boolean {
return this._enabled;
}
setEnabled(enabled: boolean): void {
this._enabled = !!enabled;
// Propagate to GPU uniform if available
if (this._writer) {
this._writer({ enabled: this._enabled ? 1 : 0 } as unknown as Partial<
Inputs & { enabled: number }
>);
}
}
abstract webgpu(): WebGPUDescriptor<Inputs, StateKeys>;
abstract cpu(): CPUDescriptor<Inputs, StateKeys>;
}
export interface WebGPUForceDescriptor<
Inputs extends Record<string, number | number[]> = Record<
string,
number | number[]
>,
StateKeys extends string | number | symbol = string
> {
states?: readonly StateKeys[];
global?: (args: {
getUniform: (id: keyof Inputs, index?: number | string) => string;
getLength: (id: keyof Inputs) => string;
}) => string;
state?: (args: {
particleVar: string;
dtVar: string;
maxSizeVar: string;
getUniform: (id: keyof Inputs, index?: number | string) => string;
getLength: (id: keyof Inputs) => string;
setState: (name: StateKeys, valueExpr: string) => string;
}) => string;
apply?: (args: {
particleVar: string;
dtVar: string;
maxSizeVar: string;
getUniform: (id: keyof Inputs, index?: number | string) => string;
getLength: (id: keyof Inputs) => string;
getState: (name: StateKeys, pidVar?: string) => string;
}) => string;
constrain?: (args: {
particleVar: string;
dtVar: string;
maxSizeVar: string;
getUniform: (id: keyof Inputs, index?: number | string) => string;
getLength: (id: keyof Inputs) => string;
getState: (name: StateKeys, pidVar?: string) => string;
}) => string;
correct?: (args: {
particleVar: string;
dtVar: string;
maxSizeVar: string;
prevPosVar: string;
postPosVar: string;
getUniform: (id: keyof Inputs, index?: number | string) => string;
getLength: (id: keyof Inputs) => string;
getState: (name: StateKeys, pidVar?: string) => string;
}) => string;
}
export type FullscreenRenderPass<
Inputs extends Record<string, number | number[]> = Record<
string,
number | number[]
>
> = {
kind: RenderPassKind.Fullscreen;
vertex?: (args: {
getUniform: (
id: keyof Inputs | "canvasWidth" | "canvasHeight",
index?: number | string
) => string;
getLength: (id: keyof Inputs) => string;
}) => string;
globals?: (args: {
getUniform: (id: keyof Inputs, index?: number | string) => string;
getLength: (id: keyof Inputs) => string;
}) => string;
fragment: (args: {
getUniform: (
id:
| keyof Inputs
| "canvasWidth"
| "canvasHeight"
| "clearColorR"
| "clearColorG"
| "clearColorB",
index?: number | string
) => string;
getLength: (id: keyof Inputs) => string;
sampleScene: (uvExpr: string) => string;
}) => string;
bindings: (keyof Inputs)[];
readsScene?: boolean;
writesScene?: true;
instanced?: boolean;
// Optional: override instance count by the length of this array input
instanceFrom?: keyof Inputs;
};
export enum RenderPassKind {
Fullscreen = "fullscreen",
Compute = "compute",
}
export type ComputeRenderPass<
Inputs extends Record<string, number | number[]> = Record<
string,
number | number[]
>
> = {
kind: RenderPassKind.Compute;
kernel: (args: {
getUniform: (
id:
| keyof Inputs
| "canvasWidth"
| "canvasHeight"
| "clearColorR"
| "clearColorG"
| "clearColorB",
index?: number | string
) => string;
getLength: (id: keyof Inputs) => string;
readScene: (coordsExpr: string) => string;
writeScene: (coordsExpr: string, colorExpr: string) => string;
}) => string;
bindings: (keyof Inputs)[];
readsScene?: boolean;
writesScene?: true;
workgroupSize?: [number, number, number];
globals?: (args: {
getUniform: (id: keyof Inputs, index?: number | string) => string;
getLength: (id: keyof Inputs) => string;
}) => string;
};
export type RenderPass<
Inputs extends Record<string, number | number[]> = Record<
string,
number | number[]
>
> = FullscreenRenderPass<Inputs> | ComputeRenderPass<Inputs>;
export interface WebGPURenderDescriptor<
Inputs extends Record<string, number | number[]> = Record<
string,
number | number[]
>
> {
passes: Array<RenderPass<Inputs>>;
}
export type WebGPUDescriptor<
Inputs extends Record<string, number | number[]> = Record<
string,
number | number[]
>,
StateKeys extends string | number | symbol = string
> = WebGPUForceDescriptor<Inputs, StateKeys> | WebGPURenderDescriptor<Inputs>;
export interface CPUForceDescriptor<
Inputs extends Record<string, number | number[]> = Record<
string,
number | number[]
>,
StateKeys extends string | number | symbol = never
> {
states?: readonly StateKeys[];
state?: (args: {
particle: Particle;
getNeighbors: (
position: { x: number; y: number },
radius: number
) => Particle[];
dt: number;
input: Inputs;
setState: (name: StateKeys, value: number) => void;
view: View;
index: number;
particles: Particle[];
getImageData: (
x: number,
y: number,
width: number,
height: number
) => ImageData | null;
}) => void;
apply?: (args: {
particle: Particle;
getNeighbors: (
position: { x: number; y: number },
radius: number
) => Particle[];
dt: number;
maxSize: number;
input: Inputs;
getState: (name: StateKeys, pid?: number) => number;
view: View;
index: number;
particles: Particle[];
getImageData: (
x: number,
y: number,
width: number,
height: number
) => ImageData | null;
}) => void;
constrain?: (args: {
particle: Particle;
getNeighbors: (
position: { x: number; y: number },
radius: number
) => Particle[];
dt: number;
maxSize: number;
input: Inputs;
getState: (name: StateKeys, pid?: number) => number;
view: View;
index: number;
particles: Particle[];
getImageData: (
x: number,
y: number,
width: number,
height: number
) => ImageData | null;
}) => void;
correct?: (args: {
particle: Particle;
getNeighbors: (
position: { x: number; y: number },
radius: number
) => Particle[];
dt: number;
maxSize: number;
prevPos: { x: number; y: number };
postPos: { x: number; y: number };
input: Inputs;
getState: (name: StateKeys, pid?: number) => number;
view: View;
index: number;
particles: Particle[];
getImageData: (
x: number,
y: number,
width: number,
height: number
) => ImageData | null;
}) => void;
}
export interface CPURenderUtils {
formatColor(color: { r: number; g: number; b: number; a: number }): string;
drawCircle(
x: number,
y: number,
radius: number,
color: { r: number; g: number; b: number; a: number }
): void;
drawRect(
x: number,
y: number,
width: number,
height: number,
color: { r: number; g: number; b: number; a: number }
): void;
}
export enum CanvasComposition {
// Module needs a clear canvas to work properly (default for most render modules)
RequiresClear = "requiresClear",
// Module handles its own background/clearing (e.g. trails with fade)
HandlesBackground = "handlesBackground",
// Module renders on top of whatever is there (additive effects)
Additive = "additive",
}
export interface CPURenderDescriptor<
Inputs extends Record<string, number | number[]> = Record<
string,
number | number[]
>
> {
// How this module composes with the canvas background
composition?: CanvasComposition;
// Optional setup phase (called once per frame before particles)
setup?: (args: {
context: CanvasRenderingContext2D;
input: Inputs;
view: View;
clearColor: { r: number; g: number; b: number; a: number };
utils: CPURenderUtils;
particles: Particle[];
}) => void;
// Optional per-particle rendering (called for each visible particle with transformed coordinates)
render?: (args: {
context: CanvasRenderingContext2D;
particle: Particle;
screenX: number;
screenY: number;
screenSize: number;
input: Inputs;
utils: CPURenderUtils;
}) => void;
// Optional teardown phase (called once per frame after all particles)
teardown?: (args: {
context: CanvasRenderingContext2D;
input: Inputs;
utils: CPURenderUtils;
}) => void;
}
export type CPUDescriptor<
Inputs extends Record<string, number | number[]> = Record<
string,
number | number[]
>,
StateKeys extends string | number | symbol = string
> = CPUForceDescriptor<Inputs, StateKeys> | CPURenderDescriptor<Inputs>;