webgpu-computed 0.0.17 → 0.0.18
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 +35 -37
- package/src/utils/GpuComputed.d.ts +4 -4
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -40,8 +40,8 @@ function U(f) {
|
|
|
40
40
|
const e = Object.keys(f), r = e.map((c) => {
|
|
41
41
|
const l = f[c];
|
|
42
42
|
if (Array.isArray(l)) {
|
|
43
|
-
for (const
|
|
44
|
-
if ($[
|
|
43
|
+
for (const y of ["vec2", "vec3", "vec4", "mat3x3", "mat4x4"])
|
|
44
|
+
if ($[y] === l.length) return y;
|
|
45
45
|
throw new Error(`${c} 不支持的数组长度 ${l.length}`);
|
|
46
46
|
}
|
|
47
47
|
if (typeof l == "number") return "f32";
|
|
@@ -50,15 +50,15 @@ function U(f) {
|
|
|
50
50
|
if (e.length !== r.length) throw new Error("keys 与 types 长度不一致");
|
|
51
51
|
let s = 0;
|
|
52
52
|
const u = e.map((c, l) => {
|
|
53
|
-
const
|
|
54
|
-
s += _(s,
|
|
53
|
+
const y = r[l];
|
|
54
|
+
s += _(s, y);
|
|
55
55
|
const a = {
|
|
56
56
|
name: c,
|
|
57
|
-
type:
|
|
57
|
+
type: y,
|
|
58
58
|
offset: s,
|
|
59
|
-
size: $[
|
|
59
|
+
size: $[y]
|
|
60
60
|
};
|
|
61
|
-
return s += $[
|
|
61
|
+
return s += $[y], a;
|
|
62
62
|
}), t = Math.max(...r.map((c) => E[c]));
|
|
63
63
|
return {
|
|
64
64
|
stride: s + (t - s % t) % t,
|
|
@@ -111,7 +111,7 @@ class T {
|
|
|
111
111
|
r(t.layout);
|
|
112
112
|
const n = t.layout[t.layout.length - 1], c = n.offset + n.size;
|
|
113
113
|
let l = 1;
|
|
114
|
-
for (const
|
|
114
|
+
for (const y of t.layout) l = Math.max(l, E[y.type]);
|
|
115
115
|
t.stride = P(c, l);
|
|
116
116
|
};
|
|
117
117
|
Object.keys(e).forEach((t) => {
|
|
@@ -133,9 +133,9 @@ class T {
|
|
|
133
133
|
if (!this.template) throw new Error("初始化计算管线错误,未找到可用数据模版");
|
|
134
134
|
await T.init();
|
|
135
135
|
const e = this.template, { device: r } = await this.getDevice(), s = [], u = [], t = [];
|
|
136
|
-
this.device = r, Object.keys(e).forEach((i,
|
|
136
|
+
this.device = r, Object.keys(e).forEach((i, d) => {
|
|
137
137
|
if (t.push({
|
|
138
|
-
binding:
|
|
138
|
+
binding: d,
|
|
139
139
|
// 绑定到组里的0号位插槽
|
|
140
140
|
visibility: GPUShaderStage.COMPUTE,
|
|
141
141
|
// 数据在哪些阶段可以使用, 计算着色器、片元着色器、顶点着色器
|
|
@@ -144,20 +144,20 @@ class T {
|
|
|
144
144
|
}
|
|
145
145
|
}), m(e[i])) {
|
|
146
146
|
const g = e[i], h = g.map((b) => `${b.name}:${b.type === "f32" ? "f32" : b.type + "<f32>"}`).join(","), v = `${S(i)}Struct`;
|
|
147
|
-
s.push(`struct ${S(i)}Struct {${h}};`), u.push(`@group(0) @binding(${
|
|
147
|
+
s.push(`struct ${S(i)}Struct {${h}};`), u.push(`@group(0) @binding(${d}) var<storage, read_write> ${i}: ${v};`);
|
|
148
148
|
} else if (B(e[i])) {
|
|
149
149
|
const g = e[i], h = g.layout.map((b) => `${b.name}:${b.type === "f32" ? "f32" : b.type + "<f32>"}`).join(","), v = `${S(i)}Struct`;
|
|
150
|
-
s.push(`struct ${v} {${h}};`), u.push(`@group(0) @binding(${
|
|
150
|
+
s.push(`struct ${v} {${h}};`), u.push(`@group(0) @binding(${d}) var<storage, read_write> ${i}: array<${v}>;`);
|
|
151
151
|
} else if (ArrayBuffer.isView(e[i]) && !(e[i] instanceof DataView)) {
|
|
152
152
|
const g = e[i], h = q(g);
|
|
153
|
-
g instanceof D ? u.push(`@group(0) @binding(${
|
|
153
|
+
g instanceof D ? u.push(`@group(0) @binding(${d}) var<storage, read_write> ${i}: array<atomic<${h}>>;`) : u.push(`@group(0) @binding(${d}) var<storage, read_write> ${i}: array<${h}>;`);
|
|
154
154
|
}
|
|
155
155
|
});
|
|
156
156
|
const {
|
|
157
157
|
beforeCodes: n = [],
|
|
158
158
|
workgroupSize: c = [32, 1, 1],
|
|
159
159
|
globalInvocationIdName: l = "grid",
|
|
160
|
-
workgroupIndexName:
|
|
160
|
+
workgroupIndexName: y = "index",
|
|
161
161
|
code: a = ""
|
|
162
162
|
} = this.option ?? {}, o = (
|
|
163
163
|
/*wgsl*/
|
|
@@ -168,7 +168,7 @@ ${u.join("")}
|
|
|
168
168
|
|
|
169
169
|
@compute @workgroup_size(${c.join(",")})
|
|
170
170
|
fn main(@builtin(global_invocation_id) ${l}: vec3<u32>) {
|
|
171
|
-
var ${
|
|
171
|
+
var ${y} = ${l}.x;
|
|
172
172
|
${a}
|
|
173
173
|
}
|
|
174
174
|
`
|
|
@@ -200,23 +200,23 @@ ${u.join("")}
|
|
|
200
200
|
function c(a, o, p = 0, i) {
|
|
201
201
|
if (ArrayBuffer.isView(a) || Array.isArray(a)) return a;
|
|
202
202
|
if (!i) {
|
|
203
|
-
const
|
|
203
|
+
const d = o[o.length - 1], g = d.offset + d.size;
|
|
204
204
|
let h = 1;
|
|
205
205
|
for (const v of o) h = Math.max(h, E[v.type]);
|
|
206
206
|
i = i ?? new Float32Array(P(g, h)).fill(0);
|
|
207
207
|
}
|
|
208
|
-
return o.forEach((
|
|
209
|
-
let g = a[
|
|
208
|
+
return o.forEach((d) => {
|
|
209
|
+
let g = a[d.name];
|
|
210
210
|
Array.isArray(g) || (g = [g]);
|
|
211
|
-
for (let h = 0; h <
|
|
212
|
-
i[p +
|
|
211
|
+
for (let h = 0; h < d.size; h++)
|
|
212
|
+
i[p + d.offset + h] = Number(g[h] ?? 0);
|
|
213
213
|
}), i;
|
|
214
214
|
}
|
|
215
215
|
function l(a, o) {
|
|
216
216
|
if (ArrayBuffer.isView(a) || typeof a[0] == "number") return a;
|
|
217
217
|
const p = new Float32Array(o.stride * a.length).fill(0);
|
|
218
|
-
return a.forEach((i,
|
|
219
|
-
const g =
|
|
218
|
+
return a.forEach((i, d) => {
|
|
219
|
+
const g = d * o.stride;
|
|
220
220
|
c(i, o.layout, g, p);
|
|
221
221
|
}), p;
|
|
222
222
|
}
|
|
@@ -229,13 +229,13 @@ ${u.join("")}
|
|
|
229
229
|
const o = u[a], p = e[a];
|
|
230
230
|
let i = [];
|
|
231
231
|
m(o) ? i = c(p, o) : B(o) ? i = l(p, o) : (Array.isArray(p) || ArrayBuffer.isView(p)) && (i = p);
|
|
232
|
-
let
|
|
233
|
-
if (o instanceof Float32Array ?
|
|
232
|
+
let d = null;
|
|
233
|
+
if (o instanceof Float32Array ? d = new Float32Array(i) : o instanceof Uint32Array ? d = new Uint32Array(i) : o instanceof Int32Array ? d = new Int32Array(i) : d = new Float32Array(i), !d) throw new Error("不支持的数组类型" + o);
|
|
234
234
|
const g = s.createBuffer({
|
|
235
|
-
size:
|
|
235
|
+
size: d.byteLength,
|
|
236
236
|
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.COPY_SRC | GPUBufferUsage.STORAGE
|
|
237
237
|
});
|
|
238
|
-
s.queue.writeBuffer(g, 0,
|
|
238
|
+
s.queue.writeBuffer(g, 0, d), t.push({ name: a, buffer: g });
|
|
239
239
|
}), {
|
|
240
240
|
group: s.createBindGroup({
|
|
241
241
|
layout: this.groupLayout,
|
|
@@ -257,9 +257,9 @@ ${u.join("")}
|
|
|
257
257
|
const s = this.template[r], u = e.length / s.stride, t = [];
|
|
258
258
|
for (let n = 0; n < u; n++) {
|
|
259
259
|
const c = n * s.stride, l = {};
|
|
260
|
-
s.layout.forEach((
|
|
261
|
-
const a = e.slice(c +
|
|
262
|
-
l[
|
|
260
|
+
s.layout.forEach((y) => {
|
|
261
|
+
const a = e.slice(c + y.offset, c + y.offset + y.size);
|
|
262
|
+
l[y.name] = a.length === 1 ? a[0] : [...a];
|
|
263
263
|
}), t.push(l);
|
|
264
264
|
}
|
|
265
265
|
return t;
|
|
@@ -268,7 +268,7 @@ ${u.join("")}
|
|
|
268
268
|
const s = this.template[r], u = {};
|
|
269
269
|
return s.forEach((t) => {
|
|
270
270
|
const n = e.slice(t.offset, t.offset + t.size);
|
|
271
|
-
u[t.name] = n.length === 1 ? n[0] : n;
|
|
271
|
+
u[t.name] = n.length === 1 ? n[0] : [...n];
|
|
272
272
|
}), u;
|
|
273
273
|
}
|
|
274
274
|
return e;
|
|
@@ -293,17 +293,15 @@ ${u.join("")}
|
|
|
293
293
|
}
|
|
294
294
|
}).filter((o) => !!o);
|
|
295
295
|
u.queue.submit([n.finish()]), await u.queue.onSubmittedWorkDone();
|
|
296
|
-
const
|
|
296
|
+
const y = /* @__PURE__ */ new Map();
|
|
297
297
|
return await Promise.all(
|
|
298
298
|
l.map(async (o) => {
|
|
299
299
|
await o.buffer.mapAsync(GPUMapMode.READ);
|
|
300
300
|
const p = o.buffer.getMappedRange();
|
|
301
301
|
let i = null;
|
|
302
|
-
this.template[o.name] instanceof Float32Array ? i = new Float32Array(p) : this.template[o.name] instanceof Uint32Array ? i = new Uint32Array(p) : this.template[o.name] instanceof Int32Array ? i = new Int32Array(p) : i = new Float32Array(p);
|
|
303
|
-
const y = [...i];
|
|
304
|
-
d.set(o.name, y);
|
|
302
|
+
this.template[o.name] instanceof Float32Array ? i = new Float32Array(p) : this.template[o.name] instanceof Uint32Array ? i = new Uint32Array(p) : this.template[o.name] instanceof Int32Array ? i = new Int32Array(p) : i = new Float32Array(p), y.set(o.name, i);
|
|
305
303
|
})
|
|
306
|
-
), s.map((o) =>
|
|
304
|
+
), s.map((o) => y.get(o));
|
|
307
305
|
}
|
|
308
306
|
/** 初始化gpu设备
|
|
309
307
|
* @returns
|
|
@@ -359,8 +357,8 @@ ${u.join("")}
|
|
|
359
357
|
*/
|
|
360
358
|
static async computed(e) {
|
|
361
359
|
let { data: r, map: s = !1, workgroupCount: u, synchronize: t, onSuccess: n, ...c } = e;
|
|
362
|
-
const l = await this.fromByData({ data: r, ...c }),
|
|
363
|
-
return n && n({ gpuComputed: l, group:
|
|
360
|
+
const l = await this.fromByData({ data: r, ...c }), y = l.createBindGroup(r), a = await l.computed(y, u, t);
|
|
361
|
+
return n && n({ gpuComputed: l, group: y, results: a }), s ? a.map((o, p) => l.dataMap(o, t[p])) : a;
|
|
364
362
|
}
|
|
365
363
|
}
|
|
366
364
|
const M = (
|
|
@@ -74,14 +74,14 @@ export declare class GpuComputed {
|
|
|
74
74
|
* @param array
|
|
75
75
|
* @param key
|
|
76
76
|
*/
|
|
77
|
-
dataMap(array:
|
|
77
|
+
dataMap(array: Float32Array | Uint32Array | Int32Array, key: string): Float32Array<ArrayBufferLike> | Int32Array<ArrayBufferLike> | Uint32Array<ArrayBufferLike> | Record<string, number | number[]> | Record<string, number | number[]>[];
|
|
78
78
|
/** 开始计算
|
|
79
79
|
* @param group 数据组
|
|
80
80
|
* @param workgroupCount 工作组大小
|
|
81
81
|
* @param synchronize 需要同步的数据字段
|
|
82
82
|
* @returns
|
|
83
83
|
*/
|
|
84
|
-
computed(group: BufferGroup, workgroupCount: [number, number?, number?], synchronize?: string[]): Promise<
|
|
84
|
+
computed(group: BufferGroup, workgroupCount: [number, number?, number?], synchronize?: string[]): Promise<(Float32Array<ArrayBufferLike> | Int32Array<ArrayBufferLike> | Uint32Array<ArrayBufferLike>)[]>;
|
|
85
85
|
/** 初始化gpu设备
|
|
86
86
|
* @returns
|
|
87
87
|
*/
|
|
@@ -112,8 +112,8 @@ export declare class GpuComputed {
|
|
|
112
112
|
onSuccess?: (opt: {
|
|
113
113
|
gpuComputed: GpuComputed;
|
|
114
114
|
group: BufferGroup;
|
|
115
|
-
results:
|
|
115
|
+
results: (Float32Array | Uint32Array | Int32Array)[];
|
|
116
116
|
}) => void;
|
|
117
|
-
} & GpuComputedOption): Promise<(
|
|
117
|
+
} & GpuComputedOption): Promise<(Float32Array<ArrayBufferLike> | Int32Array<ArrayBufferLike> | Uint32Array<ArrayBufferLike> | Record<string, number | number[]> | Record<string, number | number[]>[])[]>;
|
|
118
118
|
}
|
|
119
119
|
export {};
|