dcgrid 0.0.0-stage → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +674 -0
- package/README.md +55 -2
- package/THIRD_PARTY_NOTICES.md +140 -0
- package/dist/dcgrid.d.mts +3 -0
- package/dist/dcgrid.mjs +2 -0
- package/dist/dcgrid.wasm +0 -0
- package/dist/index.d.ts +259 -0
- package/dist/index.js +333 -0
- package/package.json +50 -4
package/dist/index.js
ADDED
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DCGrid for JavaScript: the WebAssembly build of libdcgrid on a GPUDevice you own.
|
|
3
|
+
*
|
|
4
|
+
* const dcgrid = await DCGrid.load();
|
|
5
|
+
* const sim = dcgrid.create(device, { size: [128, 128, 128], cellSize: 78 });
|
|
6
|
+
* await sim.whenReady();
|
|
7
|
+
* sim.step();
|
|
8
|
+
*
|
|
9
|
+
* Nothing blocks: creating and resetting a simulation return at once and `whenReady()` resolves once
|
|
10
|
+
* the pipelines are compiled and the grid is initialized. Steps are submitted to the device queue
|
|
11
|
+
* without waiting; the adaptive grid reads its refinement scores back asynchronously.
|
|
12
|
+
*/
|
|
13
|
+
import createDCGridModule from "./dcgrid.mjs";
|
|
14
|
+
export const FACE_NAMES = ["-x", "+x", "-y", "+y", "-z", "+z"];
|
|
15
|
+
const FIELDS = ["density", "velocity", "fluidity", "cell_size"];
|
|
16
|
+
const CODES = { 1: "invalid_argument", 2: "unsupported", 3: "not_ready", 4: "device" };
|
|
17
|
+
export class DCGridError extends Error {
|
|
18
|
+
code;
|
|
19
|
+
constructor(message, code) {
|
|
20
|
+
super(message);
|
|
21
|
+
this.code = code;
|
|
22
|
+
this.name = "DCGridError";
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/** Resolves on the next animation frame (or macrotask where there are no frames). */
|
|
26
|
+
const nextTick = () => new Promise((resolve) => typeof requestAnimationFrame === "function" ? requestAnimationFrame(() => resolve()) : setTimeout(resolve, 1));
|
|
27
|
+
export class DCGrid {
|
|
28
|
+
m;
|
|
29
|
+
devices = new WeakMap();
|
|
30
|
+
/** Names of the preset scenes (sphere obstacle, open plume, wind over hills, wildfire, clouds). */
|
|
31
|
+
scenes;
|
|
32
|
+
constructor(m) {
|
|
33
|
+
this.m = m;
|
|
34
|
+
const count = m._dcgridSceneCount();
|
|
35
|
+
this.scenes = Array.from({ length: count }, (_, i) => m.UTF8ToString(m._dcgridSceneName(i)));
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Loads the WebAssembly module. `wasm` overrides where dcgrid.wasm is fetched from (by default
|
|
39
|
+
* next to dcgrid.mjs) or passes its bytes.
|
|
40
|
+
*/
|
|
41
|
+
static async load(options = {}) {
|
|
42
|
+
const settings = {};
|
|
43
|
+
if (options.wasm instanceof ArrayBuffer)
|
|
44
|
+
settings.wasmBinary = options.wasm;
|
|
45
|
+
else if (options.wasm)
|
|
46
|
+
settings.locateFile = (file) => (file.endsWith(".wasm") ? String(options.wasm) : file);
|
|
47
|
+
return new DCGrid((await createDCGridModule(settings)));
|
|
48
|
+
}
|
|
49
|
+
/** Default description and render settings: 128³ cells of 78 m, adaptive, 40000 blocks, no-slip walls. */
|
|
50
|
+
defaults() {
|
|
51
|
+
return this.json(this.m._dcgridJsDefaults());
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* A preset scene at `resolution` cells per 10 km. `base` keeps grid, maxBlocks, criterion and
|
|
55
|
+
* densityScale (other settings come from the scene).
|
|
56
|
+
*/
|
|
57
|
+
describeScene(scene, resolution, base = {}) {
|
|
58
|
+
const ptr = this.withString(JSON.stringify(base), (s) => this.m._dcgridJsDescribeScene(scene, resolution, s));
|
|
59
|
+
if (!ptr)
|
|
60
|
+
throw this.error(1);
|
|
61
|
+
return this.json(ptr);
|
|
62
|
+
}
|
|
63
|
+
/** Creates a simulation on `device`; it builds in the background (see Simulation.whenReady). */
|
|
64
|
+
create(device, desc) {
|
|
65
|
+
const devicePtr = this.importDevice(device);
|
|
66
|
+
const ptr = this.withString(JSON.stringify(desc), (s) => this.m._dcgridJsCreate(devicePtr, s));
|
|
67
|
+
if (!ptr)
|
|
68
|
+
throw this.error(1);
|
|
69
|
+
return new Simulation(this, ptr, devicePtr);
|
|
70
|
+
}
|
|
71
|
+
// ---------------------------------------------------------------- internals for Simulation/Renderer
|
|
72
|
+
/** @internal */
|
|
73
|
+
get module() {
|
|
74
|
+
return this.m;
|
|
75
|
+
}
|
|
76
|
+
/** @internal */
|
|
77
|
+
importDevice(device) {
|
|
78
|
+
let ptr = this.devices.get(device);
|
|
79
|
+
if (!ptr) {
|
|
80
|
+
ptr = this.m.importDevice(device, this.m._dcgridJsInstance());
|
|
81
|
+
this.devices.set(device, ptr);
|
|
82
|
+
}
|
|
83
|
+
return ptr;
|
|
84
|
+
}
|
|
85
|
+
/** @internal Runs f with an imported texture handle. */
|
|
86
|
+
withTexture(texture, device, f) {
|
|
87
|
+
const ptr = this.m.importTexture(texture, device);
|
|
88
|
+
try {
|
|
89
|
+
return f(ptr);
|
|
90
|
+
}
|
|
91
|
+
finally {
|
|
92
|
+
this.m._wgpuTextureRelease(ptr);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/** @internal */
|
|
96
|
+
withString(s, f) {
|
|
97
|
+
const ptr = this.m.stringToNewUTF8(s);
|
|
98
|
+
try {
|
|
99
|
+
return f(ptr);
|
|
100
|
+
}
|
|
101
|
+
finally {
|
|
102
|
+
this.m._free(ptr);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/** @internal Copies floats into wasm memory for the duration of f (null passes a null pointer). */
|
|
106
|
+
withFloats(data, f) {
|
|
107
|
+
if (!data)
|
|
108
|
+
return f(0);
|
|
109
|
+
const ptr = this.m._malloc(data.byteLength);
|
|
110
|
+
try {
|
|
111
|
+
this.m.HEAPF32.set(data, ptr >> 2);
|
|
112
|
+
return f(ptr);
|
|
113
|
+
}
|
|
114
|
+
finally {
|
|
115
|
+
this.m._free(ptr);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
/** @internal Parses and frees a JSON string the shim returned. */
|
|
119
|
+
json(ptr) {
|
|
120
|
+
try {
|
|
121
|
+
return JSON.parse(this.m.UTF8ToString(ptr));
|
|
122
|
+
}
|
|
123
|
+
finally {
|
|
124
|
+
this.m._free(ptr);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
/** @internal */
|
|
128
|
+
error(code) {
|
|
129
|
+
return new DCGridError(this.m.UTF8ToString(this.m._dcgridJsLastError()), CODES[code] ?? "device");
|
|
130
|
+
}
|
|
131
|
+
/** @internal Throws unless a call succeeded. */
|
|
132
|
+
check(code) {
|
|
133
|
+
if (code !== 0)
|
|
134
|
+
throw this.error(code);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
export class Simulation {
|
|
138
|
+
dcgrid;
|
|
139
|
+
ptr;
|
|
140
|
+
device;
|
|
141
|
+
sources = { density: null, velocity: null };
|
|
142
|
+
destroyed = false;
|
|
143
|
+
/** @internal */
|
|
144
|
+
constructor(dcgrid,
|
|
145
|
+
/** @internal */ ptr,
|
|
146
|
+
/** @internal */ device) {
|
|
147
|
+
this.dcgrid = dcgrid;
|
|
148
|
+
this.ptr = ptr;
|
|
149
|
+
this.device = device;
|
|
150
|
+
}
|
|
151
|
+
get m() {
|
|
152
|
+
return this.dcgrid.module;
|
|
153
|
+
}
|
|
154
|
+
/** Processes finished GPU work: advances building, resetting and the adaptation. Call once per frame. */
|
|
155
|
+
poll() {
|
|
156
|
+
this.m._dcgridPoll(this.ptr);
|
|
157
|
+
}
|
|
158
|
+
/** Built and reset: steps can be recorded. */
|
|
159
|
+
get ready() {
|
|
160
|
+
return this.m._dcgridIsReady(this.ptr) !== 0;
|
|
161
|
+
}
|
|
162
|
+
/** The error that stopped the simulation (shader compilation, device loss), or null. */
|
|
163
|
+
get error() {
|
|
164
|
+
const status = this.m._dcgridGetStatus(this.ptr);
|
|
165
|
+
return status === 0 || status === 3 ? null : this.dcgrid.error(status);
|
|
166
|
+
}
|
|
167
|
+
/** Polls until ready; rejects if the simulation fails. */
|
|
168
|
+
async whenReady() {
|
|
169
|
+
for (;;) {
|
|
170
|
+
this.poll();
|
|
171
|
+
const error = this.error;
|
|
172
|
+
if (error)
|
|
173
|
+
throw error;
|
|
174
|
+
if (this.ready)
|
|
175
|
+
return;
|
|
176
|
+
await nextTick();
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
/** Back to the initial state (zero fields, coarse grid). */
|
|
180
|
+
async reset() {
|
|
181
|
+
this.dcgrid.check(this.m._dcgridReset(this.ptr));
|
|
182
|
+
await this.whenReady();
|
|
183
|
+
}
|
|
184
|
+
/** Records and submits one step; throws a "not_ready" DCGridError while building or resetting. */
|
|
185
|
+
step() {
|
|
186
|
+
this.dcgrid.check(this.m._dcgridStep(this.ptr));
|
|
187
|
+
}
|
|
188
|
+
/** Submitted steps the GPU has not finished. */
|
|
189
|
+
get stepsInFlight() {
|
|
190
|
+
return this.m._dcgridStepsInFlight(this.ptr);
|
|
191
|
+
}
|
|
192
|
+
/** Resolves once the GPU has finished every submitted step. */
|
|
193
|
+
async idle() {
|
|
194
|
+
for (;;) {
|
|
195
|
+
this.poll();
|
|
196
|
+
if (this.stepsInFlight === 0)
|
|
197
|
+
return;
|
|
198
|
+
await nextTick();
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
getStepParams() {
|
|
202
|
+
return this.dcgrid.json(this.m._dcgridJsDesc(this.ptr));
|
|
203
|
+
}
|
|
204
|
+
/** Changes settings between steps; omitted ones keep their values. */
|
|
205
|
+
setStepParams(params) {
|
|
206
|
+
this.dcgrid.check(this.dcgrid.withString(JSON.stringify(params), (s) => this.m._dcgridJsSetStepParams(this.ptr, s)));
|
|
207
|
+
}
|
|
208
|
+
/** Solid signed distance in meters (negative inside); null removes solids. */
|
|
209
|
+
setSolids(sdf) {
|
|
210
|
+
this.setVolume(0, sdf, null);
|
|
211
|
+
}
|
|
212
|
+
/** Density per second (scalar) and velocity in m/s² (vector) added every step; null removes one. */
|
|
213
|
+
setSources(density, velocity) {
|
|
214
|
+
this.sources = { density, velocity };
|
|
215
|
+
this.setVolume(1, density, velocity);
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Per-cell inflow for an inflow face: 4 floats (velocity xyz in m/s, density) per finest cell of
|
|
219
|
+
* the face along its two other axes (x faces: y then z; y faces: x then z; z faces: x then y),
|
|
220
|
+
* first axis fastest. null returns to the face's uniform values.
|
|
221
|
+
*/
|
|
222
|
+
setInflowMap(face, values) {
|
|
223
|
+
const f = FACE_NAMES.indexOf(face);
|
|
224
|
+
this.dcgrid.check(this.dcgrid.withFloats(values, (p) => this.m._dcgridSetInflowMap(this.ptr, f, p)));
|
|
225
|
+
}
|
|
226
|
+
/** Uploads a preset scene's solids, sources and inflow maps (for a simulation described by it). */
|
|
227
|
+
applyScene(scene) {
|
|
228
|
+
this.dcgrid.check(this.m._dcgridSceneApply(this.ptr, scene));
|
|
229
|
+
this.sources = { density: null, velocity: null };
|
|
230
|
+
}
|
|
231
|
+
stats() {
|
|
232
|
+
return this.dcgrid.json(this.m._dcgridJsStats(this.ptr));
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Resamples a field into a 3D texture (r32float, rgba16float or rgba32float with STORAGE_BINDING),
|
|
236
|
+
* texel centers spanning the domain, and submits it. The first call waits for the field
|
|
237
|
+
* pipelines to compile.
|
|
238
|
+
*/
|
|
239
|
+
async sampleField(field, target) {
|
|
240
|
+
const f = FIELDS.indexOf(field);
|
|
241
|
+
for (;;) {
|
|
242
|
+
this.poll();
|
|
243
|
+
const code = this.dcgrid.withTexture(target, this.device, (t) => this.m._dcgridSampleFieldWebGPU(this.ptr, f, t));
|
|
244
|
+
if (code === 0)
|
|
245
|
+
break;
|
|
246
|
+
if (code !== 3)
|
|
247
|
+
throw this.dcgrid.error(code);
|
|
248
|
+
await nextTick();
|
|
249
|
+
}
|
|
250
|
+
this.m._dcgridFlush(this.ptr);
|
|
251
|
+
}
|
|
252
|
+
/** The optional render module; its pipelines compile in the background. */
|
|
253
|
+
createRenderer() {
|
|
254
|
+
const ptr = this.m._dcgridJsCreateRenderer(this.ptr);
|
|
255
|
+
if (!ptr)
|
|
256
|
+
throw this.dcgrid.error(4);
|
|
257
|
+
return new Renderer(this.dcgrid, this, ptr);
|
|
258
|
+
}
|
|
259
|
+
/** Releases the simulation (destroy its renderers first). GPU work in flight finishes on its own. */
|
|
260
|
+
destroy() {
|
|
261
|
+
if (this.destroyed)
|
|
262
|
+
return;
|
|
263
|
+
this.destroyed = true;
|
|
264
|
+
this.m._dcgridDestroy(this.ptr);
|
|
265
|
+
}
|
|
266
|
+
setVolume(kind, volume, other) {
|
|
267
|
+
const shape = (v) => {
|
|
268
|
+
if (!v)
|
|
269
|
+
return [0, 0, 0, 0];
|
|
270
|
+
const voxels = v.size[0] * v.size[1] * v.size[2];
|
|
271
|
+
const channels = v.channels ?? v.data.length / voxels;
|
|
272
|
+
if (!Number.isInteger(channels) || v.data.length !== voxels * channels)
|
|
273
|
+
throw new DCGridError("Volume data does not match its size", "invalid_argument");
|
|
274
|
+
return [...v.size, channels];
|
|
275
|
+
};
|
|
276
|
+
const [nx, ny, nz, channels] = shape(volume);
|
|
277
|
+
const [ox, oy, oz] = shape(other);
|
|
278
|
+
const code = this.dcgrid.withFloats(volume?.data ?? null, (p) => this.dcgrid.withFloats(other?.data ?? null, (q) => this.m._dcgridJsSetVolume(this.ptr, kind, nx, ny, nz, channels, p, q, ox, oy, oz)));
|
|
279
|
+
this.dcgrid.check(code);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
export class Renderer {
|
|
283
|
+
dcgrid;
|
|
284
|
+
sim;
|
|
285
|
+
ptr;
|
|
286
|
+
destroyed = false;
|
|
287
|
+
/** @internal */
|
|
288
|
+
constructor(dcgrid, sim, ptr) {
|
|
289
|
+
this.dcgrid = dcgrid;
|
|
290
|
+
this.sim = sim;
|
|
291
|
+
this.ptr = ptr;
|
|
292
|
+
}
|
|
293
|
+
get m() {
|
|
294
|
+
return this.dcgrid.module;
|
|
295
|
+
}
|
|
296
|
+
get ready() {
|
|
297
|
+
return this.m._dcgridRendererIsReady(this.ptr) !== 0;
|
|
298
|
+
}
|
|
299
|
+
async whenReady() {
|
|
300
|
+
await this.sim.whenReady();
|
|
301
|
+
while (!this.ready) {
|
|
302
|
+
this.sim.poll();
|
|
303
|
+
const error = this.sim.error;
|
|
304
|
+
if (error)
|
|
305
|
+
throw error;
|
|
306
|
+
await nextTick();
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Records and submits a render into `target` (rgba8unorm with STORAGE_BINDING). Settings not given
|
|
311
|
+
* take their defaults. Throws a "not_ready" DCGridError until the simulation and the render
|
|
312
|
+
* pipelines are ready.
|
|
313
|
+
*/
|
|
314
|
+
render(camera, settings, target) {
|
|
315
|
+
const c = new Float32Array([...camera.eye, ...camera.right, ...camera.up, ...camera.forward, camera.fovScale, ...camera.sunDirection]);
|
|
316
|
+
const code = this.dcgrid.withFloats(c, (cam) => this.dcgrid.withString(JSON.stringify(settings), (s) => this.dcgrid.withTexture(target, this.sim.device, (t) => this.m._dcgridJsRender(this.ptr, cam, s, t))));
|
|
317
|
+
this.dcgrid.check(code);
|
|
318
|
+
}
|
|
319
|
+
/** The last submitted render is still running on the GPU. */
|
|
320
|
+
get busy() {
|
|
321
|
+
return this.m._dcgridRendererBusy(this.ptr) !== 0;
|
|
322
|
+
}
|
|
323
|
+
/** Recomputes the sun transmittance volume at the next render. */
|
|
324
|
+
invalidate() {
|
|
325
|
+
this.m._dcgridRendererInvalidate(this.ptr);
|
|
326
|
+
}
|
|
327
|
+
destroy() {
|
|
328
|
+
if (this.destroyed)
|
|
329
|
+
return;
|
|
330
|
+
this.destroyed = true;
|
|
331
|
+
this.m._dcgridRendererDestroy(this.ptr);
|
|
332
|
+
}
|
|
333
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dcgrid",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"description": "Adaptive-grid smoke simulation (DCGrid) on WebGPU, compiled to WebAssembly",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"webgpu",
|
|
7
|
+
"fluid",
|
|
8
|
+
"smoke",
|
|
9
|
+
"simulation",
|
|
10
|
+
"adaptive-grid",
|
|
11
|
+
"wasm",
|
|
12
|
+
"dcgrid"
|
|
13
|
+
],
|
|
14
|
+
"homepage": "https://dcgrid.vercel.app",
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/wouterraateland/dcgrid/issues"
|
|
17
|
+
},
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/wouterraateland/dcgrid.git",
|
|
21
|
+
"directory": "js"
|
|
22
|
+
},
|
|
23
|
+
"license": "GPL-3.0-only",
|
|
24
|
+
"type": "module",
|
|
25
|
+
"main": "./dist/index.js",
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"default": "./dist/index.js"
|
|
31
|
+
},
|
|
32
|
+
"./dcgrid.wasm": "./dist/dcgrid.wasm"
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"dist",
|
|
36
|
+
"THIRD_PARTY_NOTICES.md",
|
|
37
|
+
"LICENSE.md"
|
|
38
|
+
],
|
|
39
|
+
"sideEffects": false,
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build:wasm": "sh scripts/build-wasm.sh",
|
|
42
|
+
"build:ts": "tsc -p tsconfig.json && cp build/dcgrid.mjs build/dcgrid.wasm src/dcgrid.d.mts dist/ && cp ../LICENCE.md LICENSE.md",
|
|
43
|
+
"build": "npm run build:wasm && npm run build:ts",
|
|
44
|
+
"test": "node --test test/",
|
|
45
|
+
"prepublishOnly": "npm run build"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@webgpu/types": "0.1.64",
|
|
49
|
+
"typescript": "5.9.3",
|
|
50
|
+
"webgpu": "0.6.2"
|
|
51
|
+
}
|
|
52
|
+
}
|