webgpu-computed 0.0.12 → 0.0.14
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 +30 -30
- package/src/index.js +175 -164
- package/src/utils/GpuComputed.d.ts +12 -3
package/package.json
CHANGED
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "webgpu-computed",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "对webgpu的封装,处理了繁琐的前置工作,只关注wgsl本身逻辑",
|
|
5
|
-
"main": "./src/index.js",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
-
},
|
|
9
|
-
"keywords": [
|
|
10
|
-
"webgpu",
|
|
11
|
-
"gpu",
|
|
12
|
-
"compute",
|
|
13
|
-
"wgsl",
|
|
14
|
-
"typescript",
|
|
15
|
-
"javascript"
|
|
16
|
-
],
|
|
17
|
-
"dependencies": {
|
|
18
|
-
"webgpu": "^0.3.8"
|
|
19
|
-
},
|
|
20
|
-
"repository": {
|
|
21
|
-
"type": "git",
|
|
22
|
-
"url": "https://github.com/xiaguochuqiu/webgpu-computed.git"
|
|
23
|
-
},
|
|
24
|
-
"bugs": {
|
|
25
|
-
"url": "https://github.com/xiaguochuqiu/webgpu-computed/issues"
|
|
26
|
-
},
|
|
27
|
-
"homepage": "https://github.com/xiaguochuqiu/webgpu-computed#readme",
|
|
28
|
-
"author": "夏过初秋",
|
|
29
|
-
"license": "ISC"
|
|
30
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "webgpu-computed",
|
|
3
|
+
"version": "0.0.14",
|
|
4
|
+
"description": "对webgpu的封装,处理了繁琐的前置工作,只关注wgsl本身逻辑",
|
|
5
|
+
"main": "./src/index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"keywords": [
|
|
10
|
+
"webgpu",
|
|
11
|
+
"gpu",
|
|
12
|
+
"compute",
|
|
13
|
+
"wgsl",
|
|
14
|
+
"typescript",
|
|
15
|
+
"javascript"
|
|
16
|
+
],
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"webgpu": "^0.3.8"
|
|
19
|
+
},
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "https://github.com/xiaguochuqiu/webgpu-computed.git"
|
|
23
|
+
},
|
|
24
|
+
"bugs": {
|
|
25
|
+
"url": "https://github.com/xiaguochuqiu/webgpu-computed/issues"
|
|
26
|
+
},
|
|
27
|
+
"homepage": "https://github.com/xiaguochuqiu/webgpu-computed#readme",
|
|
28
|
+
"author": "夏过初秋",
|
|
29
|
+
"license": "ISC"
|
|
30
|
+
}
|
package/src/index.js
CHANGED
|
@@ -1,68 +1,68 @@
|
|
|
1
|
-
async function
|
|
1
|
+
async function z(s, e = !0) {
|
|
2
2
|
if (typeof global < "u" && typeof require < "u")
|
|
3
|
-
return require(
|
|
3
|
+
return require(s);
|
|
4
4
|
{
|
|
5
5
|
let r = await import(
|
|
6
6
|
/* @vite-ignore */
|
|
7
|
-
|
|
7
|
+
s
|
|
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(s) {
|
|
13
|
+
if (s instanceof Float32Array) return "f32";
|
|
14
|
+
if (s instanceof Int32Array) return "i32";
|
|
15
|
+
if (s instanceof Uint32Array) return "u32";
|
|
16
16
|
throw new Error(
|
|
17
|
-
`Unsupported ArrayBufferView type: ${
|
|
17
|
+
`Unsupported ArrayBufferView type: ${s.constructor.name}`
|
|
18
18
|
);
|
|
19
19
|
}
|
|
20
|
-
function S(
|
|
21
|
-
return
|
|
20
|
+
function S(s) {
|
|
21
|
+
return s && s[0].toUpperCase() + s.slice(1);
|
|
22
22
|
}
|
|
23
|
-
function x(
|
|
24
|
-
return O.includes(
|
|
23
|
+
function x(s) {
|
|
24
|
+
return O.includes(s);
|
|
25
25
|
}
|
|
26
|
-
function
|
|
27
|
-
return
|
|
26
|
+
function m(s) {
|
|
27
|
+
return s && Array.isArray(s) && s.length ? s.every((e) => e && typeof e == "object" && "name" in e && "type" in e && x(e.type)) : !1;
|
|
28
28
|
}
|
|
29
|
-
function $(
|
|
30
|
-
return
|
|
29
|
+
function $(s) {
|
|
30
|
+
return s && "layout" in s && m(s.layout);
|
|
31
31
|
}
|
|
32
|
-
function
|
|
32
|
+
function _(s, e) {
|
|
33
33
|
const r = E[e];
|
|
34
|
-
return (r -
|
|
34
|
+
return (r - s % r) % r;
|
|
35
35
|
}
|
|
36
|
-
function
|
|
37
|
-
return
|
|
36
|
+
function P(s, e) {
|
|
37
|
+
return s + (e - s % e) % e;
|
|
38
38
|
}
|
|
39
|
-
function
|
|
40
|
-
const e = Object.keys(
|
|
41
|
-
const
|
|
42
|
-
if (Array.isArray(
|
|
43
|
-
for (const
|
|
44
|
-
if (B[
|
|
45
|
-
throw new Error(`${
|
|
39
|
+
function U(s) {
|
|
40
|
+
const e = Object.keys(s), r = e.map((c) => {
|
|
41
|
+
const l = s[c];
|
|
42
|
+
if (Array.isArray(l)) {
|
|
43
|
+
for (const y of ["vec2", "vec3", "vec4", "mat3x3", "mat4x4"])
|
|
44
|
+
if (B[y] === l.length) return y;
|
|
45
|
+
throw new Error(`${c} 不支持的数组长度 ${l.length}`);
|
|
46
46
|
}
|
|
47
|
-
if (typeof
|
|
48
|
-
throw new Error(`${
|
|
47
|
+
if (typeof l == "number") return "f32";
|
|
48
|
+
throw new Error(`${c} 不支持的类型`);
|
|
49
49
|
});
|
|
50
50
|
if (e.length !== r.length) throw new Error("keys 与 types 长度不一致");
|
|
51
|
-
let
|
|
52
|
-
const
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
const
|
|
56
|
-
name:
|
|
57
|
-
type:
|
|
58
|
-
offset:
|
|
59
|
-
size: B[
|
|
51
|
+
let i = 0;
|
|
52
|
+
const f = e.map((c, l) => {
|
|
53
|
+
const y = r[l];
|
|
54
|
+
i += _(i, y);
|
|
55
|
+
const u = {
|
|
56
|
+
name: c,
|
|
57
|
+
type: y,
|
|
58
|
+
offset: i,
|
|
59
|
+
size: B[y]
|
|
60
60
|
};
|
|
61
|
-
return
|
|
62
|
-
}), t = Math.max(...r.map((
|
|
61
|
+
return i += B[y], u;
|
|
62
|
+
}), t = Math.max(...r.map((c) => E[c]));
|
|
63
63
|
return {
|
|
64
|
-
stride:
|
|
65
|
-
layout:
|
|
64
|
+
stride: i + (t - i % t) % t,
|
|
65
|
+
layout: f
|
|
66
66
|
};
|
|
67
67
|
}
|
|
68
68
|
const B = {
|
|
@@ -87,7 +87,7 @@ const B = {
|
|
|
87
87
|
class D extends Uint32Array {
|
|
88
88
|
}
|
|
89
89
|
const O = ["f32", "u32", "vec2", "vec3", "vec4", "mat3x3", "mat4x4"];
|
|
90
|
-
let
|
|
90
|
+
let w = null, v = null;
|
|
91
91
|
class T {
|
|
92
92
|
template;
|
|
93
93
|
option;
|
|
@@ -104,27 +104,27 @@ class T {
|
|
|
104
104
|
improveTemplateOption(e) {
|
|
105
105
|
const r = (t) => {
|
|
106
106
|
let n = 0;
|
|
107
|
-
t.forEach((
|
|
108
|
-
n +=
|
|
107
|
+
t.forEach((c) => {
|
|
108
|
+
n += _(n, c.type), c.offset = n, c.size = B[c.type], n += c.size;
|
|
109
109
|
});
|
|
110
|
-
},
|
|
110
|
+
}, i = (t) => {
|
|
111
111
|
r(t.layout);
|
|
112
|
-
const n = t.layout[t.layout.length - 1],
|
|
113
|
-
let
|
|
114
|
-
for (const
|
|
115
|
-
t.stride =
|
|
112
|
+
const n = t.layout[t.layout.length - 1], c = n.offset + n.size;
|
|
113
|
+
let l = 1;
|
|
114
|
+
for (const y of t.layout) l = Math.max(l, E[y.type]);
|
|
115
|
+
t.stride = P(c, l);
|
|
116
116
|
};
|
|
117
117
|
Object.keys(e).forEach((t) => {
|
|
118
118
|
const n = e[t];
|
|
119
|
-
|
|
119
|
+
m(n) ? r(n) : $(n) ? i(n) : Array.isArray(n) && typeof n[0] == "number" && (e[t] = new Float32Array());
|
|
120
120
|
});
|
|
121
121
|
}
|
|
122
122
|
/** 获取Gpu设备
|
|
123
123
|
* @returns
|
|
124
124
|
*/
|
|
125
125
|
async getDevice() {
|
|
126
|
-
if (!
|
|
127
|
-
return { adapter:
|
|
126
|
+
if (!w || !v) throw new Error("webgpu未初始化或不可用");
|
|
127
|
+
return { adapter: w, device: v };
|
|
128
128
|
}
|
|
129
129
|
/**
|
|
130
130
|
* 初始化计算管线
|
|
@@ -132,110 +132,117 @@ 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(), i = [], f = [], t = [];
|
|
136
|
+
this.device = r, Object.keys(e).forEach((a, p) => {
|
|
137
137
|
if (t.push({
|
|
138
|
-
binding:
|
|
138
|
+
binding: p,
|
|
139
139
|
// 绑定到组里的0号位插槽
|
|
140
140
|
visibility: GPUShaderStage.COMPUTE,
|
|
141
141
|
// 数据在哪些阶段可以使用, 计算着色器、片元着色器、顶点着色器
|
|
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[a])) {
|
|
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};`);
|
|
148
|
+
} else if ($(e[a])) {
|
|
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}>;`);
|
|
151
|
+
} else if (ArrayBuffer.isView(e[a]) && !(e[a] instanceof DataView)) {
|
|
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}>;`);
|
|
154
154
|
}
|
|
155
155
|
});
|
|
156
156
|
const {
|
|
157
157
|
beforeCodes: n = [],
|
|
158
|
-
workgroupSize:
|
|
159
|
-
globalInvocationIdName:
|
|
160
|
-
workgroupIndexName:
|
|
161
|
-
code:
|
|
162
|
-
} = this.option ?? {},
|
|
158
|
+
workgroupSize: c = [32, 1, 1],
|
|
159
|
+
globalInvocationIdName: l = "grid",
|
|
160
|
+
workgroupIndexName: y = "index",
|
|
161
|
+
code: u = ""
|
|
162
|
+
} = this.option ?? {}, o = (
|
|
163
163
|
/*wgsl*/
|
|
164
164
|
`
|
|
165
|
-
${
|
|
166
|
-
${
|
|
165
|
+
${i.join("")}
|
|
166
|
+
${f.join("")}
|
|
167
167
|
${n.join(" ") ?? ""}
|
|
168
168
|
|
|
169
|
-
@compute @workgroup_size(${
|
|
170
|
-
fn main(@builtin(global_invocation_id) ${
|
|
171
|
-
var ${
|
|
172
|
-
${
|
|
169
|
+
@compute @workgroup_size(${c.join(",")})
|
|
170
|
+
fn main(@builtin(global_invocation_id) ${l}: vec3<u32>) {
|
|
171
|
+
var ${y} = ${l}.x;
|
|
172
|
+
${u}
|
|
173
173
|
}
|
|
174
174
|
`
|
|
175
175
|
);
|
|
176
|
-
this.code =
|
|
177
|
-
const
|
|
176
|
+
this.code = o;
|
|
177
|
+
const d = r.createBindGroupLayout({
|
|
178
178
|
entries: t
|
|
179
179
|
});
|
|
180
|
-
this.groupLayout =
|
|
180
|
+
this.groupLayout = d, this.pipeline = r.createComputePipeline({
|
|
181
181
|
layout: r.createPipelineLayout({
|
|
182
|
-
bindGroupLayouts: [
|
|
182
|
+
bindGroupLayouts: [d]
|
|
183
183
|
}),
|
|
184
184
|
compute: {
|
|
185
|
-
module: r.createShaderModule({ code:
|
|
185
|
+
module: r.createShaderModule({ code: o, label: "" }),
|
|
186
186
|
entryPoint: "main"
|
|
187
187
|
}
|
|
188
188
|
});
|
|
189
189
|
}
|
|
190
|
-
/**
|
|
191
|
-
*
|
|
190
|
+
/**
|
|
191
|
+
* 根据数据创建buffer组
|
|
192
|
+
* @param data 数据
|
|
193
|
+
* @param bindGroup 已创建的bindGroup, 会在没有传入对应数据时使用,用于Buffer重复利用,不需要重新构建,按名字判断,所以请确保名称和类型一致性
|
|
194
|
+
* @returns
|
|
192
195
|
*/
|
|
193
|
-
createBindGroup(e) {
|
|
196
|
+
createBindGroup(e, r) {
|
|
194
197
|
if (!this.template) throw new Error("创建buffer组错误,未找到可用数据模版");
|
|
195
198
|
if (!this.device) throw new Error("创建buffer组错误,未找到可用的gpu设备,请确保初始化完计算管线");
|
|
196
|
-
const
|
|
197
|
-
function
|
|
198
|
-
if (!
|
|
199
|
-
const p = o[o.length - 1],
|
|
200
|
-
let
|
|
201
|
-
for (const
|
|
202
|
-
|
|
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) {
|
|
201
|
+
if (!a) {
|
|
202
|
+
const p = o[o.length - 1], g = p.offset + p.size;
|
|
203
|
+
let h = 1;
|
|
204
|
+
for (const A of o) h = Math.max(h, E[A.type]);
|
|
205
|
+
a = a ?? new Array(P(g, h)).fill(0);
|
|
203
206
|
}
|
|
204
207
|
return o.forEach((p) => {
|
|
205
|
-
let
|
|
206
|
-
Array.isArray(
|
|
207
|
-
for (let
|
|
208
|
-
|
|
209
|
-
}),
|
|
208
|
+
let g = u[p.name];
|
|
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);
|
|
212
|
+
}), a;
|
|
210
213
|
}
|
|
211
|
-
function
|
|
212
|
-
const
|
|
213
|
-
return
|
|
214
|
-
const
|
|
215
|
-
|
|
216
|
-
}),
|
|
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;
|
|
217
220
|
}
|
|
218
|
-
return Object.keys(
|
|
219
|
-
if (!(
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
221
|
+
return Object.keys(f).forEach((u) => {
|
|
222
|
+
if (!(u in e)) {
|
|
223
|
+
if (n && n.has(u))
|
|
224
|
+
return t.push({ name: u, buffer: n.get(u) });
|
|
225
|
+
throw new Error(`传入的数据中,不存在${u}字段`);
|
|
226
|
+
}
|
|
227
|
+
const o = f[u], d = e[u];
|
|
228
|
+
let a = [];
|
|
229
|
+
m(o) ? a = c(d, o) : $(o) ? a = l(d, o) : Array.isArray(d) && (a = d);
|
|
223
230
|
let p = null;
|
|
224
|
-
if (o instanceof Float32Array ? p = new Float32Array(
|
|
225
|
-
const
|
|
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);
|
|
232
|
+
const g = i.createBuffer({
|
|
226
233
|
size: p.byteLength,
|
|
227
234
|
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.COPY_SRC | GPUBufferUsage.STORAGE
|
|
228
235
|
});
|
|
229
|
-
|
|
236
|
+
i.queue.writeBuffer(g, 0, p), t.push({ name: u, buffer: g });
|
|
230
237
|
}), {
|
|
231
|
-
group:
|
|
238
|
+
group: i.createBindGroup({
|
|
232
239
|
layout: this.groupLayout,
|
|
233
|
-
entries:
|
|
240
|
+
entries: t.map((u, o) => ({
|
|
234
241
|
binding: o,
|
|
235
|
-
resource: { buffer:
|
|
242
|
+
resource: { buffer: u.buffer }
|
|
236
243
|
}))
|
|
237
244
|
}),
|
|
238
|
-
buffers:
|
|
245
|
+
buffers: t
|
|
239
246
|
};
|
|
240
247
|
}
|
|
241
248
|
/** 数据映射回模版数据
|
|
@@ -245,22 +252,22 @@ ${s.join("")}
|
|
|
245
252
|
dataMap(e, r) {
|
|
246
253
|
if (!(r in this.template)) throw new Error("未找到数据字段:" + r);
|
|
247
254
|
if ($(this.template[r])) {
|
|
248
|
-
const
|
|
249
|
-
for (let n = 0; n <
|
|
250
|
-
const
|
|
251
|
-
|
|
252
|
-
const
|
|
253
|
-
|
|
254
|
-
}), t.push(
|
|
255
|
+
const i = this.template[r], f = e.length / i.stride, t = [];
|
|
256
|
+
for (let n = 0; n < f; n++) {
|
|
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;
|
|
261
|
+
}), t.push(l);
|
|
255
262
|
}
|
|
256
263
|
return t;
|
|
257
264
|
}
|
|
258
|
-
if (
|
|
259
|
-
const
|
|
260
|
-
return
|
|
265
|
+
if (m(this.template[r])) {
|
|
266
|
+
const i = this.template[r], f = {};
|
|
267
|
+
return i.forEach((t) => {
|
|
261
268
|
const n = e.slice(t.offset, t.offset + t.size);
|
|
262
|
-
|
|
263
|
-
}),
|
|
269
|
+
f[t.name] = n.length === 1 ? n[0] : n;
|
|
270
|
+
}), f;
|
|
264
271
|
}
|
|
265
272
|
return e;
|
|
266
273
|
}
|
|
@@ -270,65 +277,69 @@ ${s.join("")}
|
|
|
270
277
|
* @param synchronize 需要同步的数据字段
|
|
271
278
|
* @returns
|
|
272
279
|
*/
|
|
273
|
-
async computed(e, r,
|
|
280
|
+
async computed(e, r, i = []) {
|
|
274
281
|
if (!this.pipeline) throw new Error("未找到可用计算管线,请确保计算管线已经创建成功");
|
|
275
|
-
const
|
|
276
|
-
|
|
277
|
-
const
|
|
278
|
-
if (
|
|
279
|
-
const
|
|
280
|
-
size:
|
|
282
|
+
const f = this.device, t = this.pipeline, n = f.createCommandEncoder(), c = n.beginComputePass();
|
|
283
|
+
c.setPipeline(t), c.setBindGroup(0, e.group), c.dispatchWorkgroups(r[0], r[1], r[2]), c.end();
|
|
284
|
+
const l = e.buffers?.map((o) => {
|
|
285
|
+
if (i?.includes(o.name)) {
|
|
286
|
+
const d = f.createBuffer({
|
|
287
|
+
size: o.buffer.size,
|
|
281
288
|
usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST
|
|
282
289
|
});
|
|
283
|
-
return n.copyBufferToBuffer(
|
|
290
|
+
return n.copyBufferToBuffer(o.buffer, 0, d, 0, d.size), { buffer: d, name: o.name };
|
|
284
291
|
}
|
|
285
|
-
}).filter((
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
+
}).filter((o) => !!o);
|
|
293
|
+
f.queue.submit([n.finish()]), await f.queue.onSubmittedWorkDone();
|
|
294
|
+
const y = /* @__PURE__ */ new Map();
|
|
295
|
+
return await Promise.all(
|
|
296
|
+
l.map(async (o) => {
|
|
297
|
+
await o.buffer.mapAsync(GPUMapMode.READ);
|
|
298
|
+
const d = o.buffer.getMappedRange();
|
|
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);
|
|
292
303
|
})
|
|
293
|
-
);
|
|
304
|
+
), i.map((o) => y.get(o));
|
|
294
305
|
}
|
|
295
306
|
/** 初始化gpu设备
|
|
296
307
|
* @returns
|
|
297
308
|
*/
|
|
298
309
|
static async init() {
|
|
299
|
-
if (!(
|
|
310
|
+
if (!(w && v)) {
|
|
300
311
|
if (typeof globalThis < "u" && typeof window > "u") {
|
|
301
|
-
const { create: e, globals: r } = await
|
|
312
|
+
const { create: e, globals: r } = await z("webgpu", !1);
|
|
302
313
|
Object.assign(globalThis, r), globalThis.navigator || (globalThis.navigator = {}), Object.assign(globalThis.navigator, { gpu: e([]) });
|
|
303
314
|
}
|
|
304
315
|
if (!navigator.gpu) throw new Error("该环境不支持webgpu");
|
|
305
|
-
if (
|
|
306
|
-
if (
|
|
316
|
+
if (w || (w = await navigator.gpu.requestAdapter({})), !w) throw new Error("获取适配器失败");
|
|
317
|
+
if (v = await w.requestDevice(), !w) throw new Error("获取设备失败");
|
|
307
318
|
}
|
|
308
319
|
}
|
|
309
320
|
/** 注销gpu设备
|
|
310
321
|
*/
|
|
311
322
|
static destroy() {
|
|
312
|
-
|
|
323
|
+
v && v.destroy(), v = null;
|
|
313
324
|
}
|
|
314
325
|
/**
|
|
315
326
|
* @param data
|
|
316
327
|
*/
|
|
317
328
|
static buildBufferTypeByData(e) {
|
|
318
|
-
return Object.keys(e).reduce((
|
|
319
|
-
let t = e[
|
|
329
|
+
return Object.keys(e).reduce((i, f) => {
|
|
330
|
+
let t = e[f];
|
|
320
331
|
if (Array.isArray(t) && typeof t[0] == "number" && (t = new Float32Array()), Array.isArray(t))
|
|
321
332
|
if (typeof t[0] == "object" || t.length) {
|
|
322
|
-
const n =
|
|
323
|
-
|
|
324
|
-
} else console.log(`字段:${
|
|
333
|
+
const n = U(t[0]);
|
|
334
|
+
i[f] = n;
|
|
335
|
+
} else console.log(`字段:${f}, 不支持该值对应数据类型或数组为空`);
|
|
325
336
|
else if (ArrayBuffer.isView(t) && !(t instanceof DataView))
|
|
326
|
-
|
|
337
|
+
i[f] = t;
|
|
327
338
|
else if (typeof t == "object") {
|
|
328
|
-
const n =
|
|
329
|
-
|
|
330
|
-
} else console.log(`字段:${
|
|
331
|
-
return
|
|
339
|
+
const n = U(t);
|
|
340
|
+
i[f] = n.layout;
|
|
341
|
+
} else console.log(`字段:${f}, 不支持的数据类型`);
|
|
342
|
+
return i;
|
|
332
343
|
}, {});
|
|
333
344
|
}
|
|
334
345
|
/** 通过数据创建
|
|
@@ -336,8 +347,8 @@ ${s.join("")}
|
|
|
336
347
|
* @returns
|
|
337
348
|
*/
|
|
338
349
|
static async fromByData(e) {
|
|
339
|
-
let { data: r, ...
|
|
340
|
-
const
|
|
350
|
+
let { data: r, ...i } = e;
|
|
351
|
+
const f = this.buildBufferTypeByData(r), t = new T(f, i);
|
|
341
352
|
return await t.initPipeline(), t;
|
|
342
353
|
}
|
|
343
354
|
/** 快捷计算方法
|
|
@@ -345,9 +356,9 @@ ${s.join("")}
|
|
|
345
356
|
* @returns
|
|
346
357
|
*/
|
|
347
358
|
static async computed(e) {
|
|
348
|
-
let { data: r, map:
|
|
349
|
-
const
|
|
350
|
-
return n && n({ gpuComputed:
|
|
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;
|
|
351
362
|
}
|
|
352
363
|
}
|
|
353
364
|
const j = (
|
|
@@ -379,7 +390,7 @@ const j = (
|
|
|
379
390
|
return all(abs(pLocal) <= halfSize);
|
|
380
391
|
}
|
|
381
392
|
`
|
|
382
|
-
),
|
|
393
|
+
), M = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
383
394
|
__proto__: null,
|
|
384
395
|
point_in_obb: G,
|
|
385
396
|
quat_rotate: j
|
|
@@ -387,5 +398,5 @@ const j = (
|
|
|
387
398
|
export {
|
|
388
399
|
D as AtomicUint32Array,
|
|
389
400
|
T as GpuComputed,
|
|
390
|
-
|
|
401
|
+
M as WGSL_Fun
|
|
391
402
|
};
|
|
@@ -51,10 +51,19 @@ export declare class GpuComputed {
|
|
|
51
51
|
* 初始化计算管线
|
|
52
52
|
*/
|
|
53
53
|
initPipeline(): Promise<void>;
|
|
54
|
-
/**
|
|
55
|
-
*
|
|
54
|
+
/**
|
|
55
|
+
* 根据数据创建buffer组
|
|
56
|
+
* @param data 数据
|
|
57
|
+
* @param bindGroup 已创建的bindGroup, 会在没有传入对应数据时使用,用于Buffer重复利用,不需要重新构建,按名字判断,所以请确保名称和类型一致性
|
|
58
|
+
* @returns
|
|
56
59
|
*/
|
|
57
|
-
createBindGroup(data: Record<string, any
|
|
60
|
+
createBindGroup(data: Record<string, any>, bindGroup?: {
|
|
61
|
+
group: GPUBindGroup;
|
|
62
|
+
buffers: {
|
|
63
|
+
name: string;
|
|
64
|
+
buffer: GPUBuffer;
|
|
65
|
+
}[];
|
|
66
|
+
}): {
|
|
58
67
|
group: GPUBindGroup;
|
|
59
68
|
buffers: {
|
|
60
69
|
name: string;
|