oria-webgpu 0.1.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 +21 -0
- package/README.ko.md +175 -0
- package/README.md +179 -0
- package/dist/binding/bind-group-layout.d.ts +6 -0
- package/dist/binding/bind-group-layout.d.ts.map +1 -0
- package/dist/binding/bind-group.d.ts +26 -0
- package/dist/binding/bind-group.d.ts.map +1 -0
- package/dist/commands/command-encoder.d.ts +3 -0
- package/dist/commands/command-encoder.d.ts.map +1 -0
- package/dist/core/context.d.ts +8 -0
- package/dist/core/context.d.ts.map +1 -0
- package/dist/core/device.d.ts +7 -0
- package/dist/core/device.d.ts.map +1 -0
- package/dist/core/gpu.d.ts +3 -0
- package/dist/core/gpu.d.ts.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +623 -0
- package/dist/index.js.map +1 -0
- package/dist/pass/compute-pass.d.ts +3 -0
- package/dist/pass/compute-pass.d.ts.map +1 -0
- package/dist/pass/render-pass.d.ts +3 -0
- package/dist/pass/render-pass.d.ts.map +1 -0
- package/dist/pipeline/compute-pipeline.d.ts +3 -0
- package/dist/pipeline/compute-pipeline.d.ts.map +1 -0
- package/dist/pipeline/pipeline-cache.d.ts +18 -0
- package/dist/pipeline/pipeline-cache.d.ts.map +1 -0
- package/dist/pipeline/render-pipeline.d.ts +3 -0
- package/dist/pipeline/render-pipeline.d.ts.map +1 -0
- package/dist/resources/buffer.d.ts +5 -0
- package/dist/resources/buffer.d.ts.map +1 -0
- package/dist/resources/readback.d.ts +10 -0
- package/dist/resources/readback.d.ts.map +1 -0
- package/dist/resources/sampler.d.ts +3 -0
- package/dist/resources/sampler.d.ts.map +1 -0
- package/dist/resources/texture.d.ts +4 -0
- package/dist/resources/texture.d.ts.map +1 -0
- package/dist/shader/reflection.d.ts +26 -0
- package/dist/shader/reflection.d.ts.map +1 -0
- package/dist/shader/shader.d.ts +10 -0
- package/dist/shader/shader.d.ts.map +1 -0
- package/dist/types/public.d.ts +211 -0
- package/dist/types/public.d.ts.map +1 -0
- package/dist/utils/alignment.d.ts +6 -0
- package/dist/utils/alignment.d.ts.map +1 -0
- package/dist/utils/hash.d.ts +11 -0
- package/dist/utils/hash.d.ts.map +1 -0
- package/dist/utils/validation.d.ts +6 -0
- package/dist/utils/validation.d.ts.map +1 -0
- package/package.json +63 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,623 @@
|
|
|
1
|
+
class c extends Error {
|
|
2
|
+
constructor(e) {
|
|
3
|
+
super(`[oria] ${e}`), this.name = "OriaError";
|
|
4
|
+
}
|
|
5
|
+
}
|
|
6
|
+
function h(t, e) {
|
|
7
|
+
if (!t) throw new c(e);
|
|
8
|
+
}
|
|
9
|
+
async function R(t) {
|
|
10
|
+
if (!("gpu" in navigator) || !navigator.gpu)
|
|
11
|
+
throw new c(
|
|
12
|
+
"WebGPU is not supported in this browser. `navigator.gpu` is undefined. oria-webgpu requires a WebGPU-capable browser (Chrome/Edge 113+, or another browser with WebGPU enabled)."
|
|
13
|
+
);
|
|
14
|
+
const e = await navigator.gpu.requestAdapter({
|
|
15
|
+
powerPreference: t.powerPreference,
|
|
16
|
+
forceFallbackAdapter: t.forceFallbackAdapter
|
|
17
|
+
});
|
|
18
|
+
if (!e)
|
|
19
|
+
throw new c(
|
|
20
|
+
"No suitable GPUAdapter was found. navigator.gpu.requestAdapter() returned null (no compatible GPU, or the browser denied access)."
|
|
21
|
+
);
|
|
22
|
+
let n;
|
|
23
|
+
try {
|
|
24
|
+
n = await e.requestDevice({
|
|
25
|
+
label: t.deviceLabel,
|
|
26
|
+
requiredFeatures: t.requiredFeatures,
|
|
27
|
+
requiredLimits: t.requiredLimits
|
|
28
|
+
});
|
|
29
|
+
} catch (r) {
|
|
30
|
+
throw new c(
|
|
31
|
+
`adapter.requestDevice() failed: ${r instanceof Error ? r.message : String(r)}. Check that requiredFeatures/requiredLimits are actually supported by this adapter (see adapter.features/adapter.limits).`
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
return { adapter: e, device: n };
|
|
35
|
+
}
|
|
36
|
+
function $(t, e, n) {
|
|
37
|
+
const r = t.getContext("webgpu");
|
|
38
|
+
if (!r)
|
|
39
|
+
throw new c(
|
|
40
|
+
"canvas.getContext('webgpu') returned null. Ensure the canvas hasn't already acquired a different context type (e.g. '2d' or 'webgl')."
|
|
41
|
+
);
|
|
42
|
+
const i = n.format ?? navigator.gpu.getPreferredCanvasFormat();
|
|
43
|
+
return r.configure({
|
|
44
|
+
device: e,
|
|
45
|
+
format: i,
|
|
46
|
+
alphaMode: n.alphaMode ?? "opaque"
|
|
47
|
+
}), { context: r, format: i };
|
|
48
|
+
}
|
|
49
|
+
function T(t, e, n, r = typeof devicePixelRatio == "number" ? devicePixelRatio : 1) {
|
|
50
|
+
const i = e ?? t.clientWidth, a = n ?? t.clientHeight;
|
|
51
|
+
t.width = Math.max(1, Math.floor(i * r)), t.height = Math.max(1, Math.floor(a * r));
|
|
52
|
+
}
|
|
53
|
+
function O(t, e) {
|
|
54
|
+
return Math.ceil(t / e) * e;
|
|
55
|
+
}
|
|
56
|
+
const z = 4;
|
|
57
|
+
function k(t) {
|
|
58
|
+
return O(t, z);
|
|
59
|
+
}
|
|
60
|
+
function q(t) {
|
|
61
|
+
return t instanceof ArrayBuffer, t.byteLength;
|
|
62
|
+
}
|
|
63
|
+
function A(t, e) {
|
|
64
|
+
const { data: n, usage: r, label: i } = e;
|
|
65
|
+
h(
|
|
66
|
+
n !== void 0 || e.size !== void 0,
|
|
67
|
+
"buffer descriptor must provide either `data` or `size`."
|
|
68
|
+
), h(
|
|
69
|
+
e.size === void 0 || e.size > 0,
|
|
70
|
+
`invalid buffer size ${e.size} — must be greater than 0.`
|
|
71
|
+
);
|
|
72
|
+
const a = e.size ?? q(n), o = k(a), s = e.mappedAtCreation ?? n !== void 0, u = t.createBuffer({ label: i, size: o, usage: r, mappedAtCreation: s });
|
|
73
|
+
if (n !== void 0 && s) {
|
|
74
|
+
const f = new Uint8Array(u.getMappedRange()), d = n instanceof ArrayBuffer ? new Uint8Array(n) : new Uint8Array(n.buffer, n.byteOffset, n.byteLength);
|
|
75
|
+
f.set(d), u.unmap();
|
|
76
|
+
}
|
|
77
|
+
const l = {
|
|
78
|
+
raw: u,
|
|
79
|
+
size: o,
|
|
80
|
+
usage: r,
|
|
81
|
+
label: i,
|
|
82
|
+
write(f, d = 0) {
|
|
83
|
+
t.queue.writeBuffer(u, d, f instanceof ArrayBuffer ? f : f.buffer, f instanceof ArrayBuffer ? 0 : f.byteOffset, (f instanceof ArrayBuffer, f.byteLength));
|
|
84
|
+
},
|
|
85
|
+
destroy() {
|
|
86
|
+
u.destroy();
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
return n !== void 0 && !s && l.write(n), l;
|
|
90
|
+
}
|
|
91
|
+
const N = GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST | GPUBufferUsage.COPY_SRC;
|
|
92
|
+
function _(t, e) {
|
|
93
|
+
return A(t, {
|
|
94
|
+
label: e.label,
|
|
95
|
+
data: e.data,
|
|
96
|
+
size: e.size,
|
|
97
|
+
mappedAtCreation: e.mappedAtCreation,
|
|
98
|
+
usage: e.usage ?? N
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
function U(t, e, n, r) {
|
|
102
|
+
return {
|
|
103
|
+
raw: t,
|
|
104
|
+
width: e,
|
|
105
|
+
height: n,
|
|
106
|
+
format: r,
|
|
107
|
+
createView(i) {
|
|
108
|
+
return t.createView(i);
|
|
109
|
+
},
|
|
110
|
+
destroy() {
|
|
111
|
+
t.destroy();
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
function w(t, e) {
|
|
116
|
+
const n = t.createTexture({
|
|
117
|
+
label: e.label,
|
|
118
|
+
size: {
|
|
119
|
+
width: e.width,
|
|
120
|
+
height: e.height,
|
|
121
|
+
depthOrArrayLayers: e.depthOrArrayLayers ?? 1
|
|
122
|
+
},
|
|
123
|
+
format: e.format,
|
|
124
|
+
usage: e.usage,
|
|
125
|
+
mipLevelCount: e.mipLevelCount,
|
|
126
|
+
sampleCount: e.sampleCount,
|
|
127
|
+
dimension: e.dimension
|
|
128
|
+
});
|
|
129
|
+
return U(n, e.width, e.height, e.format);
|
|
130
|
+
}
|
|
131
|
+
function W(t) {
|
|
132
|
+
if (typeof HTMLCanvasElement < "u" && t instanceof HTMLCanvasElement)
|
|
133
|
+
return { width: t.width, height: t.height };
|
|
134
|
+
if (typeof OffscreenCanvas < "u" && t instanceof OffscreenCanvas)
|
|
135
|
+
return { width: t.width, height: t.height };
|
|
136
|
+
const e = t;
|
|
137
|
+
return { width: e.width, height: e.height };
|
|
138
|
+
}
|
|
139
|
+
function I(t, e) {
|
|
140
|
+
const { width: n, height: r } = W(e.source), i = e.format ?? "rgba8unorm", a = e.usage ?? GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST | GPUTextureUsage.RENDER_ATTACHMENT, o = t.createTexture({
|
|
141
|
+
label: e.label,
|
|
142
|
+
size: { width: n, height: r },
|
|
143
|
+
format: i,
|
|
144
|
+
usage: a
|
|
145
|
+
});
|
|
146
|
+
return t.queue.copyExternalImageToTexture(
|
|
147
|
+
{ source: e.source, flipY: e.flipY },
|
|
148
|
+
{ texture: o, premultipliedAlpha: e.premultipliedAlpha },
|
|
149
|
+
{ width: n, height: r }
|
|
150
|
+
), U(o, n, r, i);
|
|
151
|
+
}
|
|
152
|
+
function j(t, e = {}) {
|
|
153
|
+
return t.createSampler(e);
|
|
154
|
+
}
|
|
155
|
+
async function x(t, e, n) {
|
|
156
|
+
const r = t.createBuffer({
|
|
157
|
+
label: e.label ? `${e.label}:readback-staging` : "readback-staging",
|
|
158
|
+
size: e.size,
|
|
159
|
+
usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST
|
|
160
|
+
}), i = t.createCommandEncoder();
|
|
161
|
+
i.copyBufferToBuffer(e.raw, 0, r, 0, e.size), t.queue.submit([i.finish()]), await r.mapAsync(GPUMapMode.READ);
|
|
162
|
+
const a = r.getMappedRange().slice(0);
|
|
163
|
+
return r.unmap(), r.destroy(), n ? new n(a) : a;
|
|
164
|
+
}
|
|
165
|
+
function m(t, e, n) {
|
|
166
|
+
const r = t.createShaderModule({ code: e, label: n });
|
|
167
|
+
return F(r, n), r;
|
|
168
|
+
}
|
|
169
|
+
async function F(t, e) {
|
|
170
|
+
if (!t.getCompilationInfo) return;
|
|
171
|
+
const r = (await t.getCompilationInfo()).messages.filter((o) => o.type === "error");
|
|
172
|
+
if (r.length === 0) return;
|
|
173
|
+
const i = r.map((o) => ` line ${o.lineNum}:${o.linePos} — ${o.message}`).join(`
|
|
174
|
+
`), a = new c(`WGSL compilation failed${e ? ` in "${e}"` : ""}:
|
|
175
|
+
${i}`);
|
|
176
|
+
console.error(a.message);
|
|
177
|
+
}
|
|
178
|
+
const V = /((?:@\w+\([^)]*\)\s*)+)var(<[^>]*>)?\s+(\w+)\s*:\s*([^;=]+)[;=]/g, D = /@(\w+)\(([^)]*)\)/g;
|
|
179
|
+
function H(t) {
|
|
180
|
+
return t.replace(/\/\*[\s\S]*?\*\//g, " ").replace(/\/\/.*$/gm, "");
|
|
181
|
+
}
|
|
182
|
+
function Y(t, e) {
|
|
183
|
+
if (t) {
|
|
184
|
+
const [r, i] = t.slice(1, -1).split(",").map((a) => a.trim());
|
|
185
|
+
if (r === "uniform") return "uniform-buffer";
|
|
186
|
+
if (r === "storage")
|
|
187
|
+
return i === "read_write" ? "storage-buffer" : "read-only-storage-buffer";
|
|
188
|
+
}
|
|
189
|
+
const n = e.trim();
|
|
190
|
+
return n === "sampler" ? "sampler" : n === "sampler_comparison" ? "comparison-sampler" : n.startsWith("texture_storage_") ? "storage-texture" : n === "texture_external" ? "external-texture" : n.startsWith("texture_") ? "texture" : "uniform-buffer";
|
|
191
|
+
}
|
|
192
|
+
function b(t) {
|
|
193
|
+
const e = H(t), n = [];
|
|
194
|
+
for (const r of e.matchAll(V)) {
|
|
195
|
+
const [, i, a, o, s] = r;
|
|
196
|
+
let u, l;
|
|
197
|
+
for (const f of i.matchAll(D)) {
|
|
198
|
+
const [, d, y] = f;
|
|
199
|
+
d === "group" && (u = Number(y)), d === "binding" && (l = Number(y));
|
|
200
|
+
}
|
|
201
|
+
u === void 0 || l === void 0 || n.push({
|
|
202
|
+
name: o,
|
|
203
|
+
group: u,
|
|
204
|
+
binding: l,
|
|
205
|
+
resourceType: Y(a, s),
|
|
206
|
+
wgslType: s.trim()
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
return n;
|
|
210
|
+
}
|
|
211
|
+
function J(t) {
|
|
212
|
+
const e = /* @__PURE__ */ new Map();
|
|
213
|
+
for (const n of t) {
|
|
214
|
+
const r = e.get(n.group);
|
|
215
|
+
r ? r.push(n) : e.set(n.group, [n]);
|
|
216
|
+
}
|
|
217
|
+
return e;
|
|
218
|
+
}
|
|
219
|
+
function K(...t) {
|
|
220
|
+
const e = /* @__PURE__ */ new Map();
|
|
221
|
+
for (const n of t)
|
|
222
|
+
for (const r of n)
|
|
223
|
+
e.set(r.name, r);
|
|
224
|
+
return [...e.values()];
|
|
225
|
+
}
|
|
226
|
+
function E(t) {
|
|
227
|
+
return typeof t == "object" && t !== null && "raw" in t && typeof t.write == "function";
|
|
228
|
+
}
|
|
229
|
+
function X(t) {
|
|
230
|
+
return typeof t == "object" && t !== null && !("raw" in t) && typeof t.destroy == "function" && typeof t.size == "number";
|
|
231
|
+
}
|
|
232
|
+
function v(t) {
|
|
233
|
+
return E(t) ? t.raw : t;
|
|
234
|
+
}
|
|
235
|
+
function Z(t) {
|
|
236
|
+
return typeof t == "object" && t !== null && "raw" in t && typeof t.createView == "function";
|
|
237
|
+
}
|
|
238
|
+
function Q(t) {
|
|
239
|
+
return typeof t == "object" && t !== null && !("raw" in t) && typeof t.createView == "function" && typeof t.destroy == "function";
|
|
240
|
+
}
|
|
241
|
+
function ee(t, e) {
|
|
242
|
+
const n = e.map((r) => `"${r.name}"`).join(", ");
|
|
243
|
+
return new c(
|
|
244
|
+
`Unknown shader binding "${t}" — no @group/@binding declaration for this name was found in the pipeline's WGSL.
|
|
245
|
+
|
|
246
|
+
Declared bindings: ${n || "(none)"}`
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
function te(t, e) {
|
|
250
|
+
return new c(
|
|
251
|
+
`Binding "${t.name}" expected ${e}, based on its WGSL declaration:
|
|
252
|
+
@group(${t.group}) @binding(${t.binding}) var ... ${t.name}: ${t.wgslType};`
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
function ne(t, e) {
|
|
256
|
+
switch (t.resourceType) {
|
|
257
|
+
case "uniform-buffer":
|
|
258
|
+
case "storage-buffer":
|
|
259
|
+
case "read-only-storage-buffer": {
|
|
260
|
+
if (E(e) || X(e))
|
|
261
|
+
return { buffer: v(e) };
|
|
262
|
+
if (typeof e == "object" && e !== null && "buffer" in e) {
|
|
263
|
+
const { buffer: r, offset: i, size: a } = e;
|
|
264
|
+
return { buffer: v(r), offset: i, size: a };
|
|
265
|
+
}
|
|
266
|
+
throw te(t, "a GPUBuffer (or Oria Buffer)");
|
|
267
|
+
}
|
|
268
|
+
case "sampler":
|
|
269
|
+
case "comparison-sampler":
|
|
270
|
+
return e;
|
|
271
|
+
case "texture":
|
|
272
|
+
case "storage-texture":
|
|
273
|
+
return Z(e) || Q(e) ? e.createView() : e;
|
|
274
|
+
case "external-texture":
|
|
275
|
+
return e;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
class S {
|
|
279
|
+
constructor(e, n, r) {
|
|
280
|
+
this.device = e, this.getBindGroupLayout = n, this.byName = new Map(r.map((i) => [i.name, i])), this.byGroup = J(r);
|
|
281
|
+
}
|
|
282
|
+
byName;
|
|
283
|
+
byGroup;
|
|
284
|
+
resourceCache = /* @__PURE__ */ new Map();
|
|
285
|
+
groupCache = /* @__PURE__ */ new Map();
|
|
286
|
+
get boundGroups() {
|
|
287
|
+
return this.groupCache;
|
|
288
|
+
}
|
|
289
|
+
apply(e) {
|
|
290
|
+
const n = /* @__PURE__ */ new Set();
|
|
291
|
+
for (const [r, i] of Object.entries(e)) {
|
|
292
|
+
const a = this.byName.get(r);
|
|
293
|
+
if (!a) throw ee(r, [...this.byName.values()]);
|
|
294
|
+
this.resourceCache.set(r, i), n.add(a.group);
|
|
295
|
+
}
|
|
296
|
+
for (const r of n) {
|
|
297
|
+
const i = this.buildGroup(r);
|
|
298
|
+
i && this.groupCache.set(r, i);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
buildGroup(e) {
|
|
302
|
+
const n = this.byGroup.get(e) ?? [];
|
|
303
|
+
if (n.some((i) => !this.resourceCache.has(i.name))) return;
|
|
304
|
+
const r = n.map((i) => ({
|
|
305
|
+
binding: i.binding,
|
|
306
|
+
resource: ne(i, this.resourceCache.get(i.name))
|
|
307
|
+
}));
|
|
308
|
+
return this.device.createBindGroup({
|
|
309
|
+
layout: this.getBindGroupLayout(e),
|
|
310
|
+
entries: r
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
function re(t) {
|
|
315
|
+
return t === null ? null : typeof t == "string" ? { format: t } : t;
|
|
316
|
+
}
|
|
317
|
+
function ie(t, e) {
|
|
318
|
+
const n = m(
|
|
319
|
+
t,
|
|
320
|
+
e.vertex.code,
|
|
321
|
+
e.label ? `${e.label}:vertex` : void 0
|
|
322
|
+
), r = e.fragment ? m(
|
|
323
|
+
t,
|
|
324
|
+
e.fragment.code,
|
|
325
|
+
e.label ? `${e.label}:fragment` : void 0
|
|
326
|
+
) : void 0, i = t.createRenderPipeline({
|
|
327
|
+
label: e.label,
|
|
328
|
+
layout: e.layout ?? "auto",
|
|
329
|
+
vertex: {
|
|
330
|
+
module: n,
|
|
331
|
+
entryPoint: e.vertex.entryPoint,
|
|
332
|
+
buffers: e.vertex.buffers,
|
|
333
|
+
constants: e.vertex.constants
|
|
334
|
+
},
|
|
335
|
+
fragment: e.fragment ? {
|
|
336
|
+
module: r,
|
|
337
|
+
entryPoint: e.fragment.entryPoint,
|
|
338
|
+
targets: e.fragment.targets.map(re),
|
|
339
|
+
constants: e.fragment.constants
|
|
340
|
+
} : void 0,
|
|
341
|
+
primitive: e.primitive,
|
|
342
|
+
depthStencil: e.depthStencil,
|
|
343
|
+
multisample: e.multisample
|
|
344
|
+
}), a = K(
|
|
345
|
+
b(e.vertex.code),
|
|
346
|
+
e.fragment ? b(e.fragment.code) : []
|
|
347
|
+
), o = new S(t, (u) => i.getBindGroupLayout(u), a), s = {
|
|
348
|
+
raw: i,
|
|
349
|
+
label: e.label,
|
|
350
|
+
bindings(u) {
|
|
351
|
+
return o.apply(u), s;
|
|
352
|
+
},
|
|
353
|
+
get boundGroups() {
|
|
354
|
+
return o.boundGroups;
|
|
355
|
+
}
|
|
356
|
+
};
|
|
357
|
+
return s;
|
|
358
|
+
}
|
|
359
|
+
function ae(t, e) {
|
|
360
|
+
const n = m(t, e.code, e.label), r = t.createComputePipeline({
|
|
361
|
+
label: e.label,
|
|
362
|
+
layout: e.layout ?? "auto",
|
|
363
|
+
compute: {
|
|
364
|
+
module: n,
|
|
365
|
+
entryPoint: e.entryPoint,
|
|
366
|
+
constants: e.constants
|
|
367
|
+
}
|
|
368
|
+
}), i = b(e.code), a = new S(t, (s) => r.getBindGroupLayout(s), i), o = {
|
|
369
|
+
raw: r,
|
|
370
|
+
label: e.label,
|
|
371
|
+
bindings(s) {
|
|
372
|
+
return a.apply(s), o;
|
|
373
|
+
},
|
|
374
|
+
get boundGroups() {
|
|
375
|
+
return a.boundGroups;
|
|
376
|
+
}
|
|
377
|
+
};
|
|
378
|
+
return o;
|
|
379
|
+
}
|
|
380
|
+
function g(t) {
|
|
381
|
+
if (t === void 0) return "undefined";
|
|
382
|
+
if (t === null || typeof t != "object") return JSON.stringify(t);
|
|
383
|
+
if (Array.isArray(t))
|
|
384
|
+
return `[${t.map(g).join(",")}]`;
|
|
385
|
+
const e = t;
|
|
386
|
+
return `{${Object.keys(e).sort().map((i) => `${JSON.stringify(i)}:${g(e[i])}`).join(",")}}`;
|
|
387
|
+
}
|
|
388
|
+
function L(t) {
|
|
389
|
+
let e = 2166136261;
|
|
390
|
+
for (let n = 0; n < t.length; n++)
|
|
391
|
+
e ^= t.charCodeAt(n), e = Math.imul(e, 16777619);
|
|
392
|
+
return (e >>> 0).toString(16);
|
|
393
|
+
}
|
|
394
|
+
const P = /* @__PURE__ */ new WeakMap();
|
|
395
|
+
let oe = 0;
|
|
396
|
+
function se(t) {
|
|
397
|
+
let e = P.get(t);
|
|
398
|
+
return e === void 0 && (e = `id${oe++}`, P.set(t, e)), e;
|
|
399
|
+
}
|
|
400
|
+
function M(t) {
|
|
401
|
+
return t === void 0 || t === "auto" ? "auto" : se(t);
|
|
402
|
+
}
|
|
403
|
+
function ue(t) {
|
|
404
|
+
const e = {
|
|
405
|
+
vertex: {
|
|
406
|
+
code: t.vertex.code,
|
|
407
|
+
entryPoint: t.vertex.entryPoint,
|
|
408
|
+
buffers: t.vertex.buffers,
|
|
409
|
+
constants: t.vertex.constants
|
|
410
|
+
},
|
|
411
|
+
fragment: t.fragment ? {
|
|
412
|
+
code: t.fragment.code,
|
|
413
|
+
entryPoint: t.fragment.entryPoint,
|
|
414
|
+
targets: t.fragment.targets,
|
|
415
|
+
constants: t.fragment.constants
|
|
416
|
+
} : void 0,
|
|
417
|
+
primitive: t.primitive,
|
|
418
|
+
depthStencil: t.depthStencil,
|
|
419
|
+
multisample: t.multisample,
|
|
420
|
+
layout: M(t.layout)
|
|
421
|
+
};
|
|
422
|
+
return L(g(e));
|
|
423
|
+
}
|
|
424
|
+
function fe(t) {
|
|
425
|
+
const e = {
|
|
426
|
+
code: t.code,
|
|
427
|
+
entryPoint: t.entryPoint,
|
|
428
|
+
constants: t.constants,
|
|
429
|
+
layout: M(t.layout)
|
|
430
|
+
};
|
|
431
|
+
return L(g(e));
|
|
432
|
+
}
|
|
433
|
+
class C {
|
|
434
|
+
constructor(e, n) {
|
|
435
|
+
this.keyOf = e, this.create = n;
|
|
436
|
+
}
|
|
437
|
+
cache = /* @__PURE__ */ new Map();
|
|
438
|
+
get(e) {
|
|
439
|
+
const n = this.keyOf(e);
|
|
440
|
+
let r = this.cache.get(n);
|
|
441
|
+
return r || (r = this.create(e), this.cache.set(n, r)), r;
|
|
442
|
+
}
|
|
443
|
+
clear() {
|
|
444
|
+
this.cache.clear();
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
function G(t, e) {
|
|
448
|
+
for (const [n, r] of e.boundGroups)
|
|
449
|
+
t.setBindGroup(n, r);
|
|
450
|
+
}
|
|
451
|
+
function ce(t) {
|
|
452
|
+
let e;
|
|
453
|
+
const n = {
|
|
454
|
+
raw: t,
|
|
455
|
+
pipeline(r) {
|
|
456
|
+
return t.setPipeline(r.raw), e = r, G(t, r), n;
|
|
457
|
+
},
|
|
458
|
+
bindings(r) {
|
|
459
|
+
return h(e !== void 0, "pass.bindings() requires pass.pipeline(...) to be called first."), e.bindings(r), G(t, e), n;
|
|
460
|
+
},
|
|
461
|
+
vertexBuffer(r, i, a, o) {
|
|
462
|
+
return t.setVertexBuffer(r, i.raw, a, o), n;
|
|
463
|
+
},
|
|
464
|
+
indexBuffer(r, i, a, o) {
|
|
465
|
+
return t.setIndexBuffer(r.raw, i, a, o), n;
|
|
466
|
+
},
|
|
467
|
+
draw(r, i, a, o) {
|
|
468
|
+
return t.draw(r, i, a, o), n;
|
|
469
|
+
},
|
|
470
|
+
drawIndexed(r, i, a, o, s) {
|
|
471
|
+
return t.drawIndexed(r, i, a, o, s), n;
|
|
472
|
+
}
|
|
473
|
+
};
|
|
474
|
+
return n;
|
|
475
|
+
}
|
|
476
|
+
function B(t, e) {
|
|
477
|
+
for (const [n, r] of e.boundGroups)
|
|
478
|
+
t.setBindGroup(n, r);
|
|
479
|
+
}
|
|
480
|
+
function le(t) {
|
|
481
|
+
let e;
|
|
482
|
+
const n = {
|
|
483
|
+
raw: t,
|
|
484
|
+
pipeline(r) {
|
|
485
|
+
return t.setPipeline(r.raw), e = r, B(t, r), n;
|
|
486
|
+
},
|
|
487
|
+
bindings(r) {
|
|
488
|
+
return h(e !== void 0, "pass.bindings() requires pass.pipeline(...) to be called first."), e.bindings(r), B(t, e), n;
|
|
489
|
+
},
|
|
490
|
+
dispatch(r, i, a) {
|
|
491
|
+
return t.dispatchWorkgroups(r, i, a), n;
|
|
492
|
+
}
|
|
493
|
+
};
|
|
494
|
+
return n;
|
|
495
|
+
}
|
|
496
|
+
function p(t, e, n) {
|
|
497
|
+
const r = t.createCommandEncoder({ label: e });
|
|
498
|
+
n(r), t.queue.submit([r.finish()]);
|
|
499
|
+
}
|
|
500
|
+
class de {
|
|
501
|
+
adapter;
|
|
502
|
+
device;
|
|
503
|
+
context;
|
|
504
|
+
format;
|
|
505
|
+
canvas;
|
|
506
|
+
options;
|
|
507
|
+
ownedBuffers = /* @__PURE__ */ new Set();
|
|
508
|
+
ownedTextures = /* @__PURE__ */ new Set();
|
|
509
|
+
deviceLostCallback;
|
|
510
|
+
renderPipelineCache;
|
|
511
|
+
computePipelineCache;
|
|
512
|
+
constructor(e, n, r, i, a, o) {
|
|
513
|
+
this.canvas = e, this.adapter = n, this.device = r, this.context = i, this.format = a, this.options = o, this.renderPipelineCache = new C(
|
|
514
|
+
ue,
|
|
515
|
+
(s) => ie(this.device, s)
|
|
516
|
+
), this.computePipelineCache = new C(
|
|
517
|
+
fe,
|
|
518
|
+
(s) => ae(this.device, s)
|
|
519
|
+
), r.lost.then((s) => {
|
|
520
|
+
this.deviceLostCallback?.(s);
|
|
521
|
+
});
|
|
522
|
+
}
|
|
523
|
+
buffer(e) {
|
|
524
|
+
const n = A(this.device, e);
|
|
525
|
+
return this.ownedBuffers.add(n), n;
|
|
526
|
+
}
|
|
527
|
+
storageBuffer(e) {
|
|
528
|
+
const n = _(this.device, e);
|
|
529
|
+
return this.ownedBuffers.add(n), n;
|
|
530
|
+
}
|
|
531
|
+
texture(e) {
|
|
532
|
+
const n = w(this.device, e);
|
|
533
|
+
return this.ownedTextures.add(n), n;
|
|
534
|
+
}
|
|
535
|
+
textureFromSource(e) {
|
|
536
|
+
const n = I(this.device, e);
|
|
537
|
+
return this.ownedTextures.add(n), n;
|
|
538
|
+
}
|
|
539
|
+
depthTexture(e = {}) {
|
|
540
|
+
const n = w(this.device, {
|
|
541
|
+
label: e.label,
|
|
542
|
+
width: e.width ?? this.canvas.width,
|
|
543
|
+
height: e.height ?? this.canvas.height,
|
|
544
|
+
format: e.format ?? "depth24plus",
|
|
545
|
+
sampleCount: e.sampleCount,
|
|
546
|
+
usage: GPUTextureUsage.RENDER_ATTACHMENT
|
|
547
|
+
});
|
|
548
|
+
return this.ownedTextures.add(n), n;
|
|
549
|
+
}
|
|
550
|
+
sampler(e) {
|
|
551
|
+
return j(this.device, e);
|
|
552
|
+
}
|
|
553
|
+
readBuffer(e, n) {
|
|
554
|
+
return n ? x(this.device, e, n) : x(this.device, e);
|
|
555
|
+
}
|
|
556
|
+
renderPipeline(e) {
|
|
557
|
+
const n = this.renderPipelineCache.get(e);
|
|
558
|
+
return e.bindings && n.bindings(e.bindings), n;
|
|
559
|
+
}
|
|
560
|
+
computePipeline(e) {
|
|
561
|
+
const n = this.computePipelineCache.get(e);
|
|
562
|
+
return e.bindings && n.bindings(e.bindings), n;
|
|
563
|
+
}
|
|
564
|
+
clearPipelineCache() {
|
|
565
|
+
this.renderPipelineCache.clear(), this.computePipelineCache.clear();
|
|
566
|
+
}
|
|
567
|
+
render(e, n) {
|
|
568
|
+
const r = typeof e == "function" ? {} : e, i = typeof e == "function" ? e : n;
|
|
569
|
+
h(i !== void 0, "render() requires a callback.");
|
|
570
|
+
const a = r.target ?? this.context.getCurrentTexture();
|
|
571
|
+
p(this.device, void 0, (o) => {
|
|
572
|
+
const s = o.beginRenderPass({
|
|
573
|
+
colorAttachments: [
|
|
574
|
+
{
|
|
575
|
+
view: a.createView(),
|
|
576
|
+
clearValue: r.clearColor ?? { r: 0, g: 0, b: 0, a: 1 },
|
|
577
|
+
loadOp: r.loadOp ?? "clear",
|
|
578
|
+
storeOp: r.storeOp ?? "store"
|
|
579
|
+
}
|
|
580
|
+
]
|
|
581
|
+
});
|
|
582
|
+
i(ce(s)), s.end();
|
|
583
|
+
});
|
|
584
|
+
}
|
|
585
|
+
compute(e) {
|
|
586
|
+
p(this.device, void 0, (n) => {
|
|
587
|
+
const r = n.beginComputePass();
|
|
588
|
+
e(le(r)), r.end();
|
|
589
|
+
});
|
|
590
|
+
}
|
|
591
|
+
command(e) {
|
|
592
|
+
p(this.device, void 0, e);
|
|
593
|
+
}
|
|
594
|
+
resize(e = {}) {
|
|
595
|
+
T(
|
|
596
|
+
this.canvas,
|
|
597
|
+
e.width,
|
|
598
|
+
e.height,
|
|
599
|
+
e.pixelRatio ?? this.options.pixelRatio
|
|
600
|
+
);
|
|
601
|
+
}
|
|
602
|
+
onDeviceLost(e) {
|
|
603
|
+
this.deviceLostCallback = e;
|
|
604
|
+
}
|
|
605
|
+
dispose() {
|
|
606
|
+
for (const e of this.ownedBuffers) e.destroy();
|
|
607
|
+
this.ownedBuffers.clear();
|
|
608
|
+
for (const e of this.ownedTextures) e.destroy();
|
|
609
|
+
this.ownedTextures.clear(), this.device.destroy();
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
async function he(t, e = {}) {
|
|
613
|
+
const { adapter: n, device: r } = await R(e), { context: i, format: a } = $(t, r, e);
|
|
614
|
+
return T(t, void 0, void 0, e.pixelRatio), new de(t, n, r, i, a, e);
|
|
615
|
+
}
|
|
616
|
+
export {
|
|
617
|
+
c as OriaError,
|
|
618
|
+
k as alignBufferSize,
|
|
619
|
+
O as alignTo,
|
|
620
|
+
he as createGPU,
|
|
621
|
+
b as reflectWGSLBindings
|
|
622
|
+
};
|
|
623
|
+
//# sourceMappingURL=index.js.map
|