webgpu-computed 0.0.14 → 0.0.15

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +48 -48
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webgpu-computed",
3
- "version": "0.0.14",
3
+ "version": "0.0.15",
4
4
  "description": "对webgpu的封装,处理了繁琐的前置工作,只关注wgsl本身逻辑",
5
5
  "main": "./src/index.js",
6
6
  "scripts": {
package/src/index.js CHANGED
@@ -40,8 +40,8 @@ function U(s) {
40
40
  const e = Object.keys(s), r = e.map((c) => {
41
41
  const l = s[c];
42
42
  if (Array.isArray(l)) {
43
- for (const y of ["vec2", "vec3", "vec4", "mat3x3", "mat4x4"])
44
- if (B[y] === l.length) return y;
43
+ for (const d of ["vec2", "vec3", "vec4", "mat3x3", "mat4x4"])
44
+ if (B[d] === l.length) return d;
45
45
  throw new Error(`${c} 不支持的数组长度 ${l.length}`);
46
46
  }
47
47
  if (typeof l == "number") return "f32";
@@ -50,15 +50,15 @@ function U(s) {
50
50
  if (e.length !== r.length) throw new Error("keys 与 types 长度不一致");
51
51
  let i = 0;
52
52
  const f = e.map((c, l) => {
53
- const y = r[l];
54
- i += _(i, y);
53
+ const d = r[l];
54
+ i += _(i, d);
55
55
  const u = {
56
56
  name: c,
57
- type: y,
57
+ type: d,
58
58
  offset: i,
59
- size: B[y]
59
+ size: B[d]
60
60
  };
61
- return i += B[y], u;
61
+ return i += B[d], u;
62
62
  }), t = Math.max(...r.map((c) => E[c]));
63
63
  return {
64
64
  stride: i + (t - i % 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 y of t.layout) l = Math.max(l, E[y.type]);
114
+ for (const d of t.layout) l = Math.max(l, E[d.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(), i = [], f = [], t = [];
136
- this.device = r, Object.keys(e).forEach((a, p) => {
136
+ this.device = r, Object.keys(e).forEach((a, y) => {
137
137
  if (t.push({
138
- binding: p,
138
+ binding: y,
139
139
  // 绑定到组里的0号位插槽
140
140
  visibility: GPUShaderStage.COMPUTE,
141
141
  // 数据在哪些阶段可以使用, 计算着色器、片元着色器、顶点着色器
@@ -144,20 +144,20 @@ class T {
144
144
  }
145
145
  }), m(e[a])) {
146
146
  const g = e[a], h = g.map((b) => `${b.name}:${b.type === "f32" ? "f32" : b.type + "<f32>"}`).join(","), A = `${S(a)}Struct`;
147
- i.push(`struct ${S(a)}Struct {${h}};`), f.push(`@group(0) @binding(${p}) var<storage, read_write> ${a}: ${A};`);
147
+ i.push(`struct ${S(a)}Struct {${h}};`), f.push(`@group(0) @binding(${y}) var<storage, read_write> ${a}: ${A};`);
148
148
  } else if ($(e[a])) {
149
149
  const g = e[a], h = g.layout.map((b) => `${b.name}:${b.type === "f32" ? "f32" : b.type + "<f32>"}`).join(","), A = `${S(a)}Struct`;
150
- i.push(`struct ${A} {${h}};`), f.push(`@group(0) @binding(${p}) var<storage, read_write> ${a}: array<${A}>;`);
150
+ i.push(`struct ${A} {${h}};`), f.push(`@group(0) @binding(${y}) var<storage, read_write> ${a}: array<${A}>;`);
151
151
  } else if (ArrayBuffer.isView(e[a]) && !(e[a] instanceof DataView)) {
152
152
  const g = e[a], h = q(g);
153
- g instanceof D ? f.push(`@group(0) @binding(${p}) var<storage, read_write> ${a}: array<atomic<${h}>>;`) : f.push(`@group(0) @binding(${p}) var<storage, read_write> ${a}: array<${h}>;`);
153
+ g instanceof D ? f.push(`@group(0) @binding(${y}) var<storage, read_write> ${a}: array<atomic<${h}>>;`) : f.push(`@group(0) @binding(${y}) var<storage, read_write> ${a}: 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: y = "index",
160
+ workgroupIndexName: d = "index",
161
161
  code: u = ""
162
162
  } = this.option ?? {}, o = (
163
163
  /*wgsl*/
@@ -168,18 +168,18 @@ ${f.join("")}
168
168
 
169
169
  @compute @workgroup_size(${c.join(",")})
170
170
  fn main(@builtin(global_invocation_id) ${l}: vec3<u32>) {
171
- var ${y} = ${l}.x;
171
+ var ${d} = ${l}.x;
172
172
  ${u}
173
173
  }
174
174
  `
175
175
  );
176
176
  this.code = o;
177
- const d = r.createBindGroupLayout({
177
+ const p = r.createBindGroupLayout({
178
178
  entries: t
179
179
  });
180
- this.groupLayout = d, this.pipeline = r.createComputePipeline({
180
+ this.groupLayout = p, this.pipeline = r.createComputePipeline({
181
181
  layout: r.createPipelineLayout({
182
- bindGroupLayouts: [d]
182
+ bindGroupLayouts: [p]
183
183
  }),
184
184
  compute: {
185
185
  module: r.createShaderModule({ code: o, label: "" }),
@@ -197,26 +197,26 @@ ${f.join("")}
197
197
  if (!this.template) throw new Error("创建buffer组错误,未找到可用数据模版");
198
198
  if (!this.device) throw new Error("创建buffer组错误,未找到可用的gpu设备,请确保初始化完计算管线");
199
199
  const i = this.device, f = this.template, t = [], n = r?.buffers?.reduce((u, o) => (u.set(o.name, o.buffer), u), /* @__PURE__ */ new Map());
200
- function c(u, o, d = 0, a) {
200
+ function c(u, o, p = 0, a) {
201
201
  if (!a) {
202
- const p = o[o.length - 1], g = p.offset + p.size;
202
+ const y = o[o.length - 1], g = y.offset + y.size;
203
203
  let h = 1;
204
204
  for (const A of o) h = Math.max(h, E[A.type]);
205
205
  a = a ?? new Array(P(g, h)).fill(0);
206
206
  }
207
- return o.forEach((p) => {
208
- let g = u[p.name];
207
+ return o.forEach((y) => {
208
+ let g = u[y.name];
209
209
  Array.isArray(g) || (g = [g]);
210
- for (let h = 0; h < p.size; h++)
211
- a[d + p.offset + h] = Number(g[h] ?? 0);
210
+ for (let h = 0; h < y.size; h++)
211
+ a[p + y.offset + h] = Number(g[h] ?? 0);
212
212
  }), a;
213
213
  }
214
214
  function l(u, o) {
215
- const d = new Array(o.stride * u.length).fill(0);
216
- return u.forEach((a, p) => {
217
- const g = p * o.stride;
218
- c(a, o.layout, g, d);
219
- }), d;
215
+ const p = new Array(o.stride * u.length).fill(0);
216
+ return u.forEach((a, y) => {
217
+ const g = y * o.stride;
218
+ c(a, o.layout, g, p);
219
+ }), p;
220
220
  }
221
221
  return Object.keys(f).forEach((u) => {
222
222
  if (!(u in e)) {
@@ -224,16 +224,16 @@ ${f.join("")}
224
224
  return t.push({ name: u, buffer: n.get(u) });
225
225
  throw new Error(`传入的数据中,不存在${u}字段`);
226
226
  }
227
- const o = f[u], d = e[u];
227
+ const o = f[u], p = e[u];
228
228
  let a = [];
229
- m(o) ? a = c(d, o) : $(o) ? a = l(d, o) : Array.isArray(d) && (a = d);
230
- let p = null;
231
- if (o instanceof Float32Array ? p = new Float32Array(a) : o instanceof Uint32Array ? p = new Uint32Array(a) : o instanceof Int32Array ? p = new Int32Array(a) : p = new Float32Array(a), !p) throw new Error("不支持的数组类型" + o);
229
+ m(o) ? a = c(p, o) : $(o) ? a = l(p, o) : (Array.isArray(p) || ArrayBuffer.isView(p)) && (a = p);
230
+ let y = null;
231
+ if (o instanceof Float32Array ? y = new Float32Array(a) : o instanceof Uint32Array ? y = new Uint32Array(a) : o instanceof Int32Array ? y = new Int32Array(a) : y = new Float32Array(a), !y) throw new Error("不支持的数组类型" + o);
232
232
  const g = i.createBuffer({
233
- size: p.byteLength,
233
+ size: y.byteLength,
234
234
  usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.COPY_SRC | GPUBufferUsage.STORAGE
235
235
  });
236
- i.queue.writeBuffer(g, 0, p), t.push({ name: u, buffer: g });
236
+ i.queue.writeBuffer(g, 0, y), t.push({ name: u, buffer: g });
237
237
  }), {
238
238
  group: i.createBindGroup({
239
239
  layout: this.groupLayout,
@@ -255,9 +255,9 @@ ${f.join("")}
255
255
  const i = this.template[r], f = e.length / i.stride, t = [];
256
256
  for (let n = 0; n < f; n++) {
257
257
  const c = n * i.stride, l = {};
258
- i.layout.forEach((y) => {
259
- const u = e.slice(c + y.offset, c + y.offset + y.size);
260
- l[y.name] = u.length === 1 ? u[0] : u;
258
+ i.layout.forEach((d) => {
259
+ const u = e.slice(c + d.offset, c + d.offset + d.size);
260
+ l[d.name] = u.length === 1 ? u[0] : u;
261
261
  }), t.push(l);
262
262
  }
263
263
  return t;
@@ -283,25 +283,25 @@ ${f.join("")}
283
283
  c.setPipeline(t), c.setBindGroup(0, e.group), c.dispatchWorkgroups(r[0], r[1], r[2]), c.end();
284
284
  const l = e.buffers?.map((o) => {
285
285
  if (i?.includes(o.name)) {
286
- const d = f.createBuffer({
286
+ const p = f.createBuffer({
287
287
  size: o.buffer.size,
288
288
  usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST
289
289
  });
290
- return n.copyBufferToBuffer(o.buffer, 0, d, 0, d.size), { buffer: d, name: o.name };
290
+ return n.copyBufferToBuffer(o.buffer, 0, p, 0, p.size), { buffer: p, name: o.name };
291
291
  }
292
292
  }).filter((o) => !!o);
293
293
  f.queue.submit([n.finish()]), await f.queue.onSubmittedWorkDone();
294
- const y = /* @__PURE__ */ new Map();
294
+ const d = /* @__PURE__ */ new Map();
295
295
  return await Promise.all(
296
296
  l.map(async (o) => {
297
297
  await o.buffer.mapAsync(GPUMapMode.READ);
298
- const d = o.buffer.getMappedRange();
298
+ const p = o.buffer.getMappedRange();
299
299
  let a = null;
300
- this.template[o.name] instanceof Float32Array ? a = new Float32Array(d) : this.template[o.name] instanceof Uint32Array ? a = new Uint32Array(d) : this.template[o.name] instanceof Int32Array ? a = new Int32Array(d) : a = new Float32Array(d);
301
- const p = [...a];
302
- y.set(o.name, p);
300
+ this.template[o.name] instanceof Float32Array ? a = new Float32Array(p) : this.template[o.name] instanceof Uint32Array ? a = new Uint32Array(p) : this.template[o.name] instanceof Int32Array ? a = new Int32Array(p) : a = new Float32Array(p);
301
+ const y = [...a];
302
+ d.set(o.name, y);
303
303
  })
304
- ), i.map((o) => y.get(o));
304
+ ), i.map((o) => d.get(o));
305
305
  }
306
306
  /** 初始化gpu设备
307
307
  * @returns
@@ -357,8 +357,8 @@ ${f.join("")}
357
357
  */
358
358
  static async computed(e) {
359
359
  let { data: r, map: i = !1, workgroupCount: f, synchronize: t, onSuccess: n, ...c } = e;
360
- const l = await this.fromByData({ data: r, ...c }), y = l.createBindGroup(r), u = await l.computed(y, f, t);
361
- return n && n({ gpuComputed: l, group: y, results: u }), i ? u.map((o, d) => l.dataMap(o, t[d])) : u;
360
+ const l = await this.fromByData({ data: r, ...c }), d = l.createBindGroup(r), u = await l.computed(d, f, t);
361
+ return n && n({ gpuComputed: l, group: d, results: u }), i ? u.map((o, p) => l.dataMap(o, t[p])) : u;
362
362
  }
363
363
  }
364
364
  const j = (