webgpu-computed 0.0.12 → 0.0.13
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/package.json +1 -1
- package/src/index.js +133 -129
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -1,68 +1,68 @@
|
|
|
1
|
-
async function
|
|
1
|
+
async function z(u, e = !0) {
|
|
2
2
|
if (typeof global < "u" && typeof require < "u")
|
|
3
|
-
return require(
|
|
3
|
+
return require(u);
|
|
4
4
|
{
|
|
5
5
|
let r = await import(
|
|
6
6
|
/* @vite-ignore */
|
|
7
|
-
|
|
7
|
+
u
|
|
8
8
|
);
|
|
9
9
|
return e && (r = r.default), r;
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
|
-
function q(
|
|
13
|
-
if (
|
|
14
|
-
if (
|
|
15
|
-
if (
|
|
12
|
+
function q(u) {
|
|
13
|
+
if (u instanceof Float32Array) return "f32";
|
|
14
|
+
if (u instanceof Int32Array) return "i32";
|
|
15
|
+
if (u instanceof Uint32Array) return "u32";
|
|
16
16
|
throw new Error(
|
|
17
|
-
`Unsupported ArrayBufferView type: ${
|
|
17
|
+
`Unsupported ArrayBufferView type: ${u.constructor.name}`
|
|
18
18
|
);
|
|
19
19
|
}
|
|
20
|
-
function S(
|
|
21
|
-
return
|
|
20
|
+
function S(u) {
|
|
21
|
+
return u && u[0].toUpperCase() + u.slice(1);
|
|
22
22
|
}
|
|
23
|
-
function x(
|
|
24
|
-
return O.includes(
|
|
23
|
+
function x(u) {
|
|
24
|
+
return O.includes(u);
|
|
25
25
|
}
|
|
26
|
-
function
|
|
27
|
-
return
|
|
26
|
+
function m(u) {
|
|
27
|
+
return u && Array.isArray(u) && u.length ? u.every((e) => e && typeof e == "object" && "name" in e && "type" in e && x(e.type)) : !1;
|
|
28
28
|
}
|
|
29
|
-
function $(
|
|
30
|
-
return
|
|
29
|
+
function $(u) {
|
|
30
|
+
return u && "layout" in u && m(u.layout);
|
|
31
31
|
}
|
|
32
|
-
function
|
|
32
|
+
function _(u, e) {
|
|
33
33
|
const r = E[e];
|
|
34
|
-
return (r -
|
|
34
|
+
return (r - u % r) % r;
|
|
35
35
|
}
|
|
36
|
-
function
|
|
37
|
-
return
|
|
36
|
+
function P(u, e) {
|
|
37
|
+
return u + (e - u % e) % e;
|
|
38
38
|
}
|
|
39
|
-
function
|
|
40
|
-
const e = Object.keys(
|
|
41
|
-
const a =
|
|
39
|
+
function U(u) {
|
|
40
|
+
const e = Object.keys(u), r = e.map((p) => {
|
|
41
|
+
const a = u[p];
|
|
42
42
|
if (Array.isArray(a)) {
|
|
43
43
|
for (const o of ["vec2", "vec3", "vec4", "mat3x3", "mat4x4"])
|
|
44
44
|
if (B[o] === a.length) return o;
|
|
45
|
-
throw new Error(`${
|
|
45
|
+
throw new Error(`${p} 不支持的数组长度 ${a.length}`);
|
|
46
46
|
}
|
|
47
47
|
if (typeof a == "number") return "f32";
|
|
48
|
-
throw new Error(`${
|
|
48
|
+
throw new Error(`${p} 不支持的类型`);
|
|
49
49
|
});
|
|
50
50
|
if (e.length !== r.length) throw new Error("keys 与 types 长度不一致");
|
|
51
|
-
let
|
|
52
|
-
const
|
|
51
|
+
let s = 0;
|
|
52
|
+
const f = e.map((p, a) => {
|
|
53
53
|
const o = r[a];
|
|
54
|
-
|
|
55
|
-
const
|
|
56
|
-
name:
|
|
54
|
+
s += _(s, o);
|
|
55
|
+
const y = {
|
|
56
|
+
name: p,
|
|
57
57
|
type: o,
|
|
58
|
-
offset:
|
|
58
|
+
offset: s,
|
|
59
59
|
size: B[o]
|
|
60
60
|
};
|
|
61
|
-
return
|
|
62
|
-
}), t = Math.max(...r.map((
|
|
61
|
+
return s += B[o], y;
|
|
62
|
+
}), t = Math.max(...r.map((p) => E[p]));
|
|
63
63
|
return {
|
|
64
|
-
stride:
|
|
65
|
-
layout:
|
|
64
|
+
stride: s + (t - s % t) % t,
|
|
65
|
+
layout: f
|
|
66
66
|
};
|
|
67
67
|
}
|
|
68
68
|
const B = {
|
|
@@ -104,19 +104,19 @@ class T {
|
|
|
104
104
|
improveTemplateOption(e) {
|
|
105
105
|
const r = (t) => {
|
|
106
106
|
let n = 0;
|
|
107
|
-
t.forEach((
|
|
108
|
-
n +=
|
|
107
|
+
t.forEach((p) => {
|
|
108
|
+
n += _(n, p.type), p.offset = n, p.size = B[p.type], n += p.size;
|
|
109
109
|
});
|
|
110
|
-
},
|
|
110
|
+
}, s = (t) => {
|
|
111
111
|
r(t.layout);
|
|
112
|
-
const n = t.layout[t.layout.length - 1],
|
|
112
|
+
const n = t.layout[t.layout.length - 1], p = n.offset + n.size;
|
|
113
113
|
let a = 1;
|
|
114
114
|
for (const o of t.layout) a = Math.max(a, E[o.type]);
|
|
115
|
-
t.stride =
|
|
115
|
+
t.stride = P(p, a);
|
|
116
116
|
};
|
|
117
117
|
Object.keys(e).forEach((t) => {
|
|
118
118
|
const n = e[t];
|
|
119
|
-
|
|
119
|
+
m(n) ? r(n) : $(n) ? s(n) : Array.isArray(n) && typeof n[0] == "number" && (e[t] = new Float32Array());
|
|
120
120
|
});
|
|
121
121
|
}
|
|
122
122
|
/** 获取Gpu设备
|
|
@@ -132,8 +132,8 @@ class T {
|
|
|
132
132
|
async initPipeline() {
|
|
133
133
|
if (!this.template) throw new Error("初始化计算管线错误,未找到可用数据模版");
|
|
134
134
|
await T.init();
|
|
135
|
-
const e = this.template, { device: r } = await this.getDevice(),
|
|
136
|
-
this.device = r, Object.keys(e).forEach((
|
|
135
|
+
const e = this.template, { device: r } = await this.getDevice(), s = [], f = [], t = [];
|
|
136
|
+
this.device = r, Object.keys(e).forEach((c, d) => {
|
|
137
137
|
if (t.push({
|
|
138
138
|
binding: d,
|
|
139
139
|
// 绑定到组里的0号位插槽
|
|
@@ -142,47 +142,47 @@ class T {
|
|
|
142
142
|
buffer: {
|
|
143
143
|
type: "storage"
|
|
144
144
|
}
|
|
145
|
-
}),
|
|
146
|
-
const g = e[
|
|
147
|
-
|
|
148
|
-
} else if ($(e[
|
|
149
|
-
const g = e[
|
|
150
|
-
|
|
151
|
-
} else if (ArrayBuffer.isView(e[
|
|
152
|
-
const g = e[
|
|
153
|
-
g instanceof D ?
|
|
145
|
+
}), m(e[c])) {
|
|
146
|
+
const g = e[c], v = g.map((w) => `${w.name}:${w.type === "f32" ? "f32" : w.type + "<f32>"}`).join(","), A = `${S(c)}Struct`;
|
|
147
|
+
s.push(`struct ${S(c)}Struct {${v}};`), f.push(`@group(0) @binding(${d}) var<storage, read_write> ${c}: ${A};`);
|
|
148
|
+
} else if ($(e[c])) {
|
|
149
|
+
const g = e[c], v = g.layout.map((w) => `${w.name}:${w.type === "f32" ? "f32" : w.type + "<f32>"}`).join(","), A = `${S(c)}Struct`;
|
|
150
|
+
s.push(`struct ${A} {${v}};`), f.push(`@group(0) @binding(${d}) var<storage, read_write> ${c}: array<${A}>;`);
|
|
151
|
+
} else if (ArrayBuffer.isView(e[c]) && !(e[c] instanceof DataView)) {
|
|
152
|
+
const g = e[c], v = q(g);
|
|
153
|
+
g instanceof D ? f.push(`@group(0) @binding(${d}) var<storage, read_write> ${c}: array<atomic<${v}>>;`) : f.push(`@group(0) @binding(${d}) var<storage, read_write> ${c}: array<${v}>;`);
|
|
154
154
|
}
|
|
155
155
|
});
|
|
156
156
|
const {
|
|
157
157
|
beforeCodes: n = [],
|
|
158
|
-
workgroupSize:
|
|
158
|
+
workgroupSize: p = [32, 1, 1],
|
|
159
159
|
globalInvocationIdName: a = "grid",
|
|
160
160
|
workgroupIndexName: o = "index",
|
|
161
|
-
code:
|
|
162
|
-
} = this.option ?? {},
|
|
161
|
+
code: y = ""
|
|
162
|
+
} = this.option ?? {}, i = (
|
|
163
163
|
/*wgsl*/
|
|
164
164
|
`
|
|
165
|
-
${
|
|
166
|
-
${
|
|
165
|
+
${s.join("")}
|
|
166
|
+
${f.join("")}
|
|
167
167
|
${n.join(" ") ?? ""}
|
|
168
168
|
|
|
169
|
-
@compute @workgroup_size(${
|
|
169
|
+
@compute @workgroup_size(${p.join(",")})
|
|
170
170
|
fn main(@builtin(global_invocation_id) ${a}: vec3<u32>) {
|
|
171
171
|
var ${o} = ${a}.x;
|
|
172
|
-
${
|
|
172
|
+
${y}
|
|
173
173
|
}
|
|
174
174
|
`
|
|
175
175
|
);
|
|
176
|
-
this.code =
|
|
177
|
-
const
|
|
176
|
+
this.code = i;
|
|
177
|
+
const l = r.createBindGroupLayout({
|
|
178
178
|
entries: t
|
|
179
179
|
});
|
|
180
|
-
this.groupLayout =
|
|
180
|
+
this.groupLayout = l, this.pipeline = r.createComputePipeline({
|
|
181
181
|
layout: r.createPipelineLayout({
|
|
182
|
-
bindGroupLayouts: [
|
|
182
|
+
bindGroupLayouts: [l]
|
|
183
183
|
}),
|
|
184
184
|
compute: {
|
|
185
|
-
module: r.createShaderModule({ code:
|
|
185
|
+
module: r.createShaderModule({ code: i, label: "" }),
|
|
186
186
|
entryPoint: "main"
|
|
187
187
|
}
|
|
188
188
|
});
|
|
@@ -193,49 +193,49 @@ ${s.join("")}
|
|
|
193
193
|
createBindGroup(e) {
|
|
194
194
|
if (!this.template) throw new Error("创建buffer组错误,未找到可用数据模版");
|
|
195
195
|
if (!this.device) throw new Error("创建buffer组错误,未找到可用的gpu设备,请确保初始化完计算管线");
|
|
196
|
-
const r = this.device,
|
|
197
|
-
function t(a, o,
|
|
198
|
-
if (!
|
|
199
|
-
const
|
|
196
|
+
const r = this.device, s = this.template, f = [];
|
|
197
|
+
function t(a, o, y = 0, i) {
|
|
198
|
+
if (!i) {
|
|
199
|
+
const l = o[o.length - 1], c = l.offset + l.size;
|
|
200
200
|
let d = 1;
|
|
201
201
|
for (const g of o) d = Math.max(d, E[g.type]);
|
|
202
|
-
|
|
202
|
+
i = i ?? new Array(P(c, d)).fill(0);
|
|
203
203
|
}
|
|
204
|
-
return o.forEach((
|
|
205
|
-
let
|
|
206
|
-
Array.isArray(
|
|
207
|
-
for (let d = 0; d <
|
|
208
|
-
|
|
209
|
-
}),
|
|
204
|
+
return o.forEach((l) => {
|
|
205
|
+
let c = a[l.name];
|
|
206
|
+
Array.isArray(c) || (c = [c]);
|
|
207
|
+
for (let d = 0; d < l.size; d++)
|
|
208
|
+
i[y + l.offset + d] = Number(c[d] ?? 0);
|
|
209
|
+
}), i;
|
|
210
210
|
}
|
|
211
211
|
function n(a, o) {
|
|
212
|
-
const
|
|
213
|
-
return a.forEach((
|
|
214
|
-
const
|
|
215
|
-
t(
|
|
216
|
-
}),
|
|
212
|
+
const y = new Array(o.stride * a.length).fill(0);
|
|
213
|
+
return a.forEach((i, l) => {
|
|
214
|
+
const c = l * o.stride;
|
|
215
|
+
t(i, o.layout, c, y);
|
|
216
|
+
}), y;
|
|
217
217
|
}
|
|
218
|
-
return Object.keys(
|
|
218
|
+
return Object.keys(s).forEach((a) => {
|
|
219
219
|
if (!(a in e)) throw new Error(`传入的数据中,不存在${a}字段`);
|
|
220
|
-
const o =
|
|
221
|
-
let
|
|
222
|
-
|
|
223
|
-
let
|
|
224
|
-
if (o instanceof Float32Array ?
|
|
225
|
-
const
|
|
226
|
-
size:
|
|
220
|
+
const o = s[a], y = e[a];
|
|
221
|
+
let i = [];
|
|
222
|
+
m(o) ? i = t(y, o) : $(o) ? i = n(y, o) : Array.isArray(y) && (i = y);
|
|
223
|
+
let l = null;
|
|
224
|
+
if (o instanceof Float32Array ? l = new Float32Array(i) : o instanceof Uint32Array ? l = new Uint32Array(i) : o instanceof Int32Array ? l = new Int32Array(i) : l = new Float32Array(i), !l) throw new Error("不支持的数组类型" + o);
|
|
225
|
+
const c = r.createBuffer({
|
|
226
|
+
size: l.byteLength,
|
|
227
227
|
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.COPY_SRC | GPUBufferUsage.STORAGE
|
|
228
228
|
});
|
|
229
|
-
r.queue.writeBuffer(
|
|
229
|
+
r.queue.writeBuffer(c, 0, l), f.push({ name: a, buffer: c });
|
|
230
230
|
}), {
|
|
231
231
|
group: r.createBindGroup({
|
|
232
232
|
layout: this.groupLayout,
|
|
233
|
-
entries:
|
|
233
|
+
entries: f.map((a, o) => ({
|
|
234
234
|
binding: o,
|
|
235
235
|
resource: { buffer: a.buffer }
|
|
236
236
|
}))
|
|
237
237
|
}),
|
|
238
|
-
buffers:
|
|
238
|
+
buffers: f
|
|
239
239
|
};
|
|
240
240
|
}
|
|
241
241
|
/** 数据映射回模版数据
|
|
@@ -245,22 +245,22 @@ ${s.join("")}
|
|
|
245
245
|
dataMap(e, r) {
|
|
246
246
|
if (!(r in this.template)) throw new Error("未找到数据字段:" + r);
|
|
247
247
|
if ($(this.template[r])) {
|
|
248
|
-
const
|
|
249
|
-
for (let n = 0; n <
|
|
250
|
-
const
|
|
251
|
-
|
|
252
|
-
const
|
|
253
|
-
a[o.name] =
|
|
248
|
+
const s = this.template[r], f = e.length / s.stride, t = [];
|
|
249
|
+
for (let n = 0; n < f; n++) {
|
|
250
|
+
const p = n * s.stride, a = {};
|
|
251
|
+
s.layout.forEach((o) => {
|
|
252
|
+
const y = e.slice(p + o.offset, p + o.offset + o.size);
|
|
253
|
+
a[o.name] = y.length === 1 ? y[0] : y;
|
|
254
254
|
}), t.push(a);
|
|
255
255
|
}
|
|
256
256
|
return t;
|
|
257
257
|
}
|
|
258
|
-
if (
|
|
259
|
-
const
|
|
260
|
-
return
|
|
258
|
+
if (m(this.template[r])) {
|
|
259
|
+
const s = this.template[r], f = {};
|
|
260
|
+
return s.forEach((t) => {
|
|
261
261
|
const n = e.slice(t.offset, t.offset + t.size);
|
|
262
|
-
|
|
263
|
-
}),
|
|
262
|
+
f[t.name] = n.length === 1 ? n[0] : n;
|
|
263
|
+
}), f;
|
|
264
264
|
}
|
|
265
265
|
return e;
|
|
266
266
|
}
|
|
@@ -270,27 +270,31 @@ ${s.join("")}
|
|
|
270
270
|
* @param synchronize 需要同步的数据字段
|
|
271
271
|
* @returns
|
|
272
272
|
*/
|
|
273
|
-
async computed(e, r,
|
|
273
|
+
async computed(e, r, s = []) {
|
|
274
274
|
if (!this.pipeline) throw new Error("未找到可用计算管线,请确保计算管线已经创建成功");
|
|
275
|
-
const
|
|
276
|
-
|
|
275
|
+
const f = this.device, t = this.pipeline, n = f.createCommandEncoder(), p = n.beginComputePass();
|
|
276
|
+
p.setPipeline(t), p.setBindGroup(0, e.group), p.dispatchWorkgroups(r[0], r[1], r[2]), p.end();
|
|
277
277
|
const a = e.buffers?.map((i) => {
|
|
278
|
-
if (
|
|
279
|
-
const
|
|
278
|
+
if (s?.includes(i.name)) {
|
|
279
|
+
const l = f.createBuffer({
|
|
280
280
|
size: i.buffer.size,
|
|
281
281
|
usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST
|
|
282
282
|
});
|
|
283
|
-
return n.copyBufferToBuffer(i.buffer, 0,
|
|
283
|
+
return n.copyBufferToBuffer(i.buffer, 0, l, 0, l.size), { buffer: l, name: i.name };
|
|
284
284
|
}
|
|
285
285
|
}).filter((i) => !!i);
|
|
286
|
-
|
|
286
|
+
f.queue.submit([n.finish()]), await f.queue.onSubmittedWorkDone();
|
|
287
|
+
const o = /* @__PURE__ */ new Map();
|
|
288
|
+
return await Promise.all(
|
|
287
289
|
a.map(async (i) => {
|
|
288
290
|
await i.buffer.mapAsync(GPUMapMode.READ);
|
|
289
|
-
const
|
|
290
|
-
let
|
|
291
|
-
|
|
291
|
+
const l = i.buffer.getMappedRange();
|
|
292
|
+
let c = null;
|
|
293
|
+
this.template[i.name] instanceof Float32Array ? c = new Float32Array(l) : this.template[i.name] instanceof Uint32Array ? c = new Uint32Array(l) : this.template[i.name] instanceof Int32Array ? c = new Int32Array(l) : c = new Float32Array(l);
|
|
294
|
+
const d = [...c];
|
|
295
|
+
o.set(i.name, d);
|
|
292
296
|
})
|
|
293
|
-
);
|
|
297
|
+
), s.map((i) => o.get(i));
|
|
294
298
|
}
|
|
295
299
|
/** 初始化gpu设备
|
|
296
300
|
* @returns
|
|
@@ -298,7 +302,7 @@ ${s.join("")}
|
|
|
298
302
|
static async init() {
|
|
299
303
|
if (!(h && b)) {
|
|
300
304
|
if (typeof globalThis < "u" && typeof window > "u") {
|
|
301
|
-
const { create: e, globals: r } = await
|
|
305
|
+
const { create: e, globals: r } = await z("webgpu", !1);
|
|
302
306
|
Object.assign(globalThis, r), globalThis.navigator || (globalThis.navigator = {}), Object.assign(globalThis.navigator, { gpu: e([]) });
|
|
303
307
|
}
|
|
304
308
|
if (!navigator.gpu) throw new Error("该环境不支持webgpu");
|
|
@@ -315,20 +319,20 @@ ${s.join("")}
|
|
|
315
319
|
* @param data
|
|
316
320
|
*/
|
|
317
321
|
static buildBufferTypeByData(e) {
|
|
318
|
-
return Object.keys(e).reduce((
|
|
319
|
-
let t = e[
|
|
322
|
+
return Object.keys(e).reduce((s, f) => {
|
|
323
|
+
let t = e[f];
|
|
320
324
|
if (Array.isArray(t) && typeof t[0] == "number" && (t = new Float32Array()), Array.isArray(t))
|
|
321
325
|
if (typeof t[0] == "object" || t.length) {
|
|
322
|
-
const n =
|
|
323
|
-
|
|
324
|
-
} else console.log(`字段:${
|
|
326
|
+
const n = U(t[0]);
|
|
327
|
+
s[f] = n;
|
|
328
|
+
} else console.log(`字段:${f}, 不支持该值对应数据类型或数组为空`);
|
|
325
329
|
else if (ArrayBuffer.isView(t) && !(t instanceof DataView))
|
|
326
|
-
|
|
330
|
+
s[f] = t;
|
|
327
331
|
else if (typeof t == "object") {
|
|
328
|
-
const n =
|
|
329
|
-
|
|
330
|
-
} else console.log(`字段:${
|
|
331
|
-
return
|
|
332
|
+
const n = U(t);
|
|
333
|
+
s[f] = n.layout;
|
|
334
|
+
} else console.log(`字段:${f}, 不支持的数据类型`);
|
|
335
|
+
return s;
|
|
332
336
|
}, {});
|
|
333
337
|
}
|
|
334
338
|
/** 通过数据创建
|
|
@@ -336,8 +340,8 @@ ${s.join("")}
|
|
|
336
340
|
* @returns
|
|
337
341
|
*/
|
|
338
342
|
static async fromByData(e) {
|
|
339
|
-
let { data: r, ...
|
|
340
|
-
const
|
|
343
|
+
let { data: r, ...s } = e;
|
|
344
|
+
const f = this.buildBufferTypeByData(r), t = new T(f, s);
|
|
341
345
|
return await t.initPipeline(), t;
|
|
342
346
|
}
|
|
343
347
|
/** 快捷计算方法
|
|
@@ -345,9 +349,9 @@ ${s.join("")}
|
|
|
345
349
|
* @returns
|
|
346
350
|
*/
|
|
347
351
|
static async computed(e) {
|
|
348
|
-
let { data: r, map:
|
|
349
|
-
const a = await this.fromByData({ data: r, ...
|
|
350
|
-
return n && n({ gpuComputed: a, group: o, results:
|
|
352
|
+
let { data: r, map: s = !1, workgroupCount: f, synchronize: t, onSuccess: n, ...p } = e;
|
|
353
|
+
const a = await this.fromByData({ data: r, ...p }), o = a.createBindGroup(r), y = await a.computed(o, f, t);
|
|
354
|
+
return n && n({ gpuComputed: a, group: o, results: y }), s ? y.map((i, l) => a.dataMap(i, t[l])) : y;
|
|
351
355
|
}
|
|
352
356
|
}
|
|
353
357
|
const j = (
|