@vivi2d/web 0.1.0-alpha.0

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 (37) hide show
  1. package/LICENSE +160 -0
  2. package/README.md +201 -0
  3. package/dist/CanvasPool-CASccukD.js +48 -0
  4. package/dist/CanvasPool-CASccukD.js.map +1 -0
  5. package/dist/CanvasRenderer-BWDoXN0L.js +5750 -0
  6. package/dist/CanvasRenderer-BWDoXN0L.js.map +1 -0
  7. package/dist/GpuStencilModesToPixi-c2lsBIrd.js +221 -0
  8. package/dist/GpuStencilModesToPixi-c2lsBIrd.js.map +1 -0
  9. package/dist/RenderTargetSystem-BCowAg_f.js +3493 -0
  10. package/dist/RenderTargetSystem-BCowAg_f.js.map +1 -0
  11. package/dist/WebGLRenderer-KKZauisz.js +2236 -0
  12. package/dist/WebGLRenderer-KKZauisz.js.map +1 -0
  13. package/dist/WebGPURenderer-xmqgPtcF.js +1632 -0
  14. package/dist/WebGPURenderer-xmqgPtcF.js.map +1 -0
  15. package/dist/auto-register.d.ts +1 -0
  16. package/dist/auto-register.js +3 -0
  17. package/dist/auto-register.js.map +1 -0
  18. package/dist/browserAll-DQ0eWekj.js +1872 -0
  19. package/dist/browserAll-DQ0eWekj.js.map +1 -0
  20. package/dist/canvasUtils-BSaR9Fot.js +210 -0
  21. package/dist/canvasUtils-BSaR9Fot.js.map +1 -0
  22. package/dist/errors.d.ts +12 -0
  23. package/dist/getTextureBatchBindGroup-BYhXfZM4.js +22 -0
  24. package/dist/getTextureBatchBindGroup-BYhXfZM4.js.map +1 -0
  25. package/dist/index-CW8mx7yF.js +18805 -0
  26. package/dist/index-CW8mx7yF.js.map +1 -0
  27. package/dist/index.d.ts +7 -0
  28. package/dist/model-loader.d.ts +42 -0
  29. package/dist/player.d.ts +85 -0
  30. package/dist/vivi-model-element.d.ts +65 -0
  31. package/dist/vivi2d.es.js +11 -0
  32. package/dist/vivi2d.es.js.map +1 -0
  33. package/dist/vivi2d.umd.js +1020 -0
  34. package/dist/vivi2d.umd.js.map +1 -0
  35. package/dist/webworkerAll-oTmmI2Qm.js +694 -0
  36. package/dist/webworkerAll-oTmmI2Qm.js.map +1 -0
  37. package/package.json +56 -0
@@ -0,0 +1,1632 @@
1
+ import { x as E, i as l, al as ae, D as P, S as T, e as S, c as ue, d as ce, a as L, a1 as U, ao as C, C as y, f as de, J as he, V as A, M as D, v as k, T as w, A as pe, u as le, a6 as B, q as fe } from "./index-CW8mx7yF.js";
2
+ import { g as F } from "./getTextureBatchBindGroup-BYhXfZM4.js";
3
+ import { G as O, j as ge, b as me, i as I, f as _e, r as H, k as be, c as ye, S as xe } from "./RenderTargetSystem-BCowAg_f.js";
4
+ import { G as Ge, e as Pe, b as Be, l as Se, t as Te } from "./GpuStencilModesToPixi-c2lsBIrd.js";
5
+ import { C as M } from "./CanvasPool-CASccukD.js";
6
+ const G = E.for2d();
7
+ class W {
8
+ start(e, t, r) {
9
+ const s = e.renderer, i = s.encoder, n = r.gpuProgram;
10
+ this._shader = r, this._geometry = t, i.setGeometry(t, n), G.blendMode = "normal", s.pipeline.getPipeline(
11
+ t,
12
+ n,
13
+ G
14
+ );
15
+ const o = s.globalUniforms.bindGroup;
16
+ i.resetBindGroup(1), i.setBindGroup(0, o, n);
17
+ }
18
+ execute(e, t) {
19
+ const r = this._shader.gpuProgram, s = e.renderer, i = s.encoder;
20
+ if (!t.bindGroup) {
21
+ const u = t.textures;
22
+ t.bindGroup = F(
23
+ u.textures,
24
+ u.count,
25
+ s.limits.maxBatchableTextures
26
+ );
27
+ }
28
+ G.blendMode = t.blendMode;
29
+ const n = s.bindGroup.getBindGroup(
30
+ t.bindGroup,
31
+ r,
32
+ 1
33
+ ), o = s.pipeline.getPipeline(
34
+ this._geometry,
35
+ r,
36
+ G,
37
+ t.topology
38
+ );
39
+ t.bindGroup._touch(s.gc.now, s.tick), i.setPipeline(o), i.renderPassEncoder.setBindGroup(1, n), i.renderPassEncoder.drawIndexed(t.size, 1, t.start);
40
+ }
41
+ }
42
+ W.extension = {
43
+ type: [
44
+ l.WebGPUPipesAdaptor
45
+ ],
46
+ name: "batch"
47
+ };
48
+ class V {
49
+ constructor(e) {
50
+ this._hash = /* @__PURE__ */ Object.create(null), this._renderer = e;
51
+ }
52
+ contextChange(e) {
53
+ this._gpu = e;
54
+ }
55
+ getBindGroup(e, t, r) {
56
+ return e._updateKey(), this._hash[e._key] || this._createBindGroup(e, t, r);
57
+ }
58
+ _createBindGroup(e, t, r) {
59
+ const s = this._gpu.device, i = t.layout[r], n = [], o = this._renderer;
60
+ for (const f in i) {
61
+ const h = e.resources[f] ?? e.resources[i[f]];
62
+ let p;
63
+ if (h._resourceType === "uniformGroup") {
64
+ const d = h;
65
+ o.ubo.updateUniformGroup(d);
66
+ const g = d.buffer;
67
+ p = {
68
+ buffer: o.buffer.getGPUBuffer(g),
69
+ offset: 0,
70
+ size: g.descriptor.size
71
+ };
72
+ } else if (h._resourceType === "buffer") {
73
+ const d = h;
74
+ p = {
75
+ buffer: o.buffer.getGPUBuffer(d),
76
+ offset: 0,
77
+ size: d.descriptor.size
78
+ };
79
+ } else if (h._resourceType === "bufferResource") {
80
+ const d = h;
81
+ p = {
82
+ buffer: o.buffer.getGPUBuffer(d.buffer),
83
+ offset: d.offset,
84
+ size: d.size
85
+ };
86
+ } else if (h._resourceType === "textureSampler") {
87
+ const d = h;
88
+ p = o.texture.getGpuSampler(d);
89
+ } else if (h._resourceType === "textureSource") {
90
+ const d = h;
91
+ p = o.texture.getTextureView(d);
92
+ }
93
+ n.push({
94
+ binding: i[f],
95
+ resource: p
96
+ });
97
+ }
98
+ const u = o.shader.getProgramData(t).bindGroups[r], c = s.createBindGroup({
99
+ layout: u,
100
+ entries: n
101
+ });
102
+ return this._hash[e._key] = c, c;
103
+ }
104
+ destroy() {
105
+ this._hash = null, this._renderer = null;
106
+ }
107
+ }
108
+ V.extension = {
109
+ type: [
110
+ l.WebGPUSystem
111
+ ],
112
+ name: "bindGroup"
113
+ };
114
+ class Ce {
115
+ constructor(e) {
116
+ this.gpuBuffer = e;
117
+ }
118
+ destroy() {
119
+ this.gpuBuffer.destroy(), this.gpuBuffer = null;
120
+ }
121
+ }
122
+ class N {
123
+ constructor(e) {
124
+ this._renderer = e, this._managedBuffers = new O({
125
+ renderer: e,
126
+ type: "resource",
127
+ onUnload: this.onBufferUnload.bind(this),
128
+ name: "gpuBuffer"
129
+ });
130
+ }
131
+ contextChange(e) {
132
+ this._gpu = e;
133
+ }
134
+ getGPUBuffer(e) {
135
+ var t;
136
+ return e._gcLastUsed = this._renderer.gc.now, ((t = e._gpuData[this._renderer.uid]) == null ? void 0 : t.gpuBuffer) || this.createGPUBuffer(e);
137
+ }
138
+ updateBuffer(e) {
139
+ const t = this.getGPUBuffer(e), r = e.data;
140
+ return e._updateID && r && (e._updateID = 0, this._gpu.device.queue.writeBuffer(
141
+ t,
142
+ 0,
143
+ r.buffer,
144
+ 0,
145
+ // round to the nearest 4 bytes
146
+ (e._updateSize || r.byteLength) + 3 & -4
147
+ )), t;
148
+ }
149
+ /** dispose all WebGL resources of all managed buffers */
150
+ destroyAll() {
151
+ this._managedBuffers.removeAll();
152
+ }
153
+ onBufferUnload(e) {
154
+ e.off("update", this.updateBuffer, this), e.off("change", this.onBufferChange, this);
155
+ }
156
+ createGPUBuffer(e) {
157
+ const t = this._gpu.device.createBuffer(e.descriptor);
158
+ return e._updateID = 0, e._resourceId = ae("resource"), e.data && (ge(
159
+ e.data.buffer,
160
+ t.getMappedRange(),
161
+ e.data.byteOffset,
162
+ e.data.byteLength
163
+ ), t.unmap()), e._gpuData[this._renderer.uid] = new Ce(t), this._managedBuffers.add(e) && (e.on("update", this.updateBuffer, this), e.on("change", this.onBufferChange, this)), t;
164
+ }
165
+ onBufferChange(e) {
166
+ this._managedBuffers.remove(e), e._updateID = 0, this.createGPUBuffer(e);
167
+ }
168
+ destroy() {
169
+ this._managedBuffers.destroy(), this._renderer = null, this._gpu = null;
170
+ }
171
+ }
172
+ N.extension = {
173
+ type: [
174
+ l.WebGPUSystem
175
+ ],
176
+ name: "buffer"
177
+ };
178
+ class ve {
179
+ constructor({ minUniformOffsetAlignment: e }) {
180
+ this._minUniformOffsetAlignment = 256, this.byteIndex = 0, this._minUniformOffsetAlignment = e, this.data = new Float32Array(65535);
181
+ }
182
+ clear() {
183
+ this.byteIndex = 0;
184
+ }
185
+ addEmptyGroup(e) {
186
+ if (e > this._minUniformOffsetAlignment / 4)
187
+ throw new Error(`UniformBufferBatch: array is too large: ${e * 4}`);
188
+ const t = this.byteIndex;
189
+ let r = t + e * 4;
190
+ if (r = Math.ceil(r / this._minUniformOffsetAlignment) * this._minUniformOffsetAlignment, r > this.data.length * 4)
191
+ throw new Error("UniformBufferBatch: ubo batch got too big");
192
+ return this.byteIndex = r, t;
193
+ }
194
+ addGroup(e) {
195
+ const t = this.addEmptyGroup(e.length);
196
+ for (let r = 0; r < e.length; r++)
197
+ this.data[t / 4 + r] = e[r];
198
+ return t;
199
+ }
200
+ destroy() {
201
+ this.data = null;
202
+ }
203
+ }
204
+ class z {
205
+ constructor(e) {
206
+ this._colorMaskCache = 15, this._renderer = e;
207
+ }
208
+ setMask(e) {
209
+ this._colorMaskCache !== e && (this._colorMaskCache = e, this._renderer.pipeline.setColorMask(e));
210
+ }
211
+ destroy() {
212
+ this._renderer = null, this._colorMaskCache = null;
213
+ }
214
+ }
215
+ z.extension = {
216
+ type: [
217
+ l.WebGPUSystem
218
+ ],
219
+ name: "colorMask"
220
+ };
221
+ class v {
222
+ /**
223
+ * @param {WebGPURenderer} renderer - The renderer this System works for.
224
+ */
225
+ constructor(e) {
226
+ this._renderer = e;
227
+ }
228
+ async init(e) {
229
+ return this._initPromise ? this._initPromise : (this._initPromise = (e.gpu ? Promise.resolve(e.gpu) : this._createDeviceAndAdaptor(e)).then((t) => {
230
+ this.gpu = t, this._renderer.runners.contextChange.emit(this.gpu);
231
+ }), this._initPromise);
232
+ }
233
+ /**
234
+ * Handle the context change event
235
+ * @param gpu
236
+ */
237
+ contextChange(e) {
238
+ this._renderer.gpu = e;
239
+ }
240
+ /**
241
+ * Helper class to create a WebGL Context
242
+ * @param {object} options - An options object that gets passed in to the canvas element containing the
243
+ * context attributes
244
+ * @see https://developer.mozilla.org/en/docs/Web/API/HTMLCanvasElement/getContext
245
+ * @returns {WebGLRenderingContext} the WebGL context
246
+ */
247
+ async _createDeviceAndAdaptor(e) {
248
+ const t = await P.get().getNavigator().gpu.requestAdapter({
249
+ powerPreference: e.powerPreference,
250
+ forceFallbackAdapter: e.forceFallbackAdapter
251
+ }), r = [
252
+ "texture-compression-bc",
253
+ "texture-compression-astc",
254
+ "texture-compression-etc2"
255
+ ].filter((i) => t.features.has(i)), s = await t.requestDevice({
256
+ requiredFeatures: r
257
+ });
258
+ return { adapter: t, device: s };
259
+ }
260
+ destroy() {
261
+ this.gpu = null, this._renderer = null;
262
+ }
263
+ }
264
+ v.extension = {
265
+ type: [
266
+ l.WebGPUSystem
267
+ ],
268
+ name: "device"
269
+ };
270
+ v.defaultOptions = {
271
+ /**
272
+ * {@link WebGPUOptions.powerPreference}
273
+ * @default default
274
+ */
275
+ powerPreference: void 0,
276
+ /**
277
+ * Force the use of the fallback adapter
278
+ * @default false
279
+ */
280
+ forceFallbackAdapter: !1
281
+ };
282
+ class j {
283
+ constructor(e) {
284
+ this._boundBindGroup = /* @__PURE__ */ Object.create(null), this._boundVertexBuffer = /* @__PURE__ */ Object.create(null), this._renderer = e;
285
+ }
286
+ renderStart() {
287
+ this.commandFinished = new Promise((e) => {
288
+ this._resolveCommandFinished = e;
289
+ }), this.commandEncoder = this._renderer.gpu.device.createCommandEncoder();
290
+ }
291
+ beginRenderPass(e) {
292
+ this.endRenderPass(), this._clearCache(), this.renderPassEncoder = this.commandEncoder.beginRenderPass(e.descriptor);
293
+ }
294
+ endRenderPass() {
295
+ this.renderPassEncoder && this.renderPassEncoder.end(), this.renderPassEncoder = null;
296
+ }
297
+ setViewport(e) {
298
+ this.renderPassEncoder.setViewport(e.x, e.y, e.width, e.height, 0, 1);
299
+ }
300
+ setPipelineFromGeometryProgramAndState(e, t, r, s) {
301
+ const i = this._renderer.pipeline.getPipeline(e, t, r, s);
302
+ this.setPipeline(i);
303
+ }
304
+ setPipeline(e) {
305
+ this._boundPipeline !== e && (this._boundPipeline = e, this.renderPassEncoder.setPipeline(e));
306
+ }
307
+ _setVertexBuffer(e, t) {
308
+ this._boundVertexBuffer[e] !== t && (this._boundVertexBuffer[e] = t, this.renderPassEncoder.setVertexBuffer(e, this._renderer.buffer.updateBuffer(t)));
309
+ }
310
+ _setIndexBuffer(e) {
311
+ if (this._boundIndexBuffer === e) return;
312
+ this._boundIndexBuffer = e;
313
+ const t = e.data.BYTES_PER_ELEMENT === 2 ? "uint16" : "uint32";
314
+ this.renderPassEncoder.setIndexBuffer(this._renderer.buffer.updateBuffer(e), t);
315
+ }
316
+ resetBindGroup(e) {
317
+ this._boundBindGroup[e] = null;
318
+ }
319
+ setBindGroup(e, t, r) {
320
+ if (this._boundBindGroup[e] === t) return;
321
+ this._boundBindGroup[e] = t, t._touch(this._renderer.gc.now, this._renderer.tick);
322
+ const s = this._renderer.bindGroup.getBindGroup(t, r, e);
323
+ this.renderPassEncoder.setBindGroup(e, s);
324
+ }
325
+ setGeometry(e, t) {
326
+ const r = this._renderer.pipeline.getBufferNamesToBind(e, t);
327
+ for (const s in r)
328
+ this._setVertexBuffer(parseInt(s, 10), e.attributes[r[s]].buffer);
329
+ e.indexBuffer && this._setIndexBuffer(e.indexBuffer);
330
+ }
331
+ _setShaderBindGroups(e, t) {
332
+ for (const r in e.groups) {
333
+ const s = e.groups[r];
334
+ t || this._syncBindGroup(s), this.setBindGroup(r, s, e.gpuProgram);
335
+ }
336
+ }
337
+ _syncBindGroup(e) {
338
+ for (const t in e.resources) {
339
+ const r = e.resources[t];
340
+ r.isUniformGroup && this._renderer.ubo.updateUniformGroup(r);
341
+ }
342
+ }
343
+ draw(e) {
344
+ const { geometry: t, shader: r, state: s, topology: i, size: n, start: o, instanceCount: u, skipSync: c } = e;
345
+ this.setPipelineFromGeometryProgramAndState(t, r.gpuProgram, s, i), this.setGeometry(t, r.gpuProgram), this._setShaderBindGroups(r, c), t.indexBuffer ? this.renderPassEncoder.drawIndexed(
346
+ n || t.indexBuffer.data.length,
347
+ u ?? t.instanceCount,
348
+ o || 0
349
+ ) : this.renderPassEncoder.draw(n || t.getSize(), u ?? t.instanceCount, o || 0);
350
+ }
351
+ finishRenderPass() {
352
+ this.renderPassEncoder && (this.renderPassEncoder.end(), this.renderPassEncoder = null);
353
+ }
354
+ postrender() {
355
+ this.finishRenderPass(), this._gpu.device.queue.submit([this.commandEncoder.finish()]), this._resolveCommandFinished(), this.commandEncoder = null;
356
+ }
357
+ // restores a render pass if finishRenderPass was called
358
+ // not optimised as really used for debugging!
359
+ // used when we want to stop drawing and log a texture..
360
+ restoreRenderPass() {
361
+ const e = this._renderer.renderTarget.adaptor.getDescriptor(
362
+ this._renderer.renderTarget.renderTarget,
363
+ !1,
364
+ [0, 0, 0, 1],
365
+ this._renderer.renderTarget.mipLevel,
366
+ this._renderer.renderTarget.layer
367
+ );
368
+ this.renderPassEncoder = this.commandEncoder.beginRenderPass(e);
369
+ const t = this._boundPipeline, r = { ...this._boundVertexBuffer }, s = this._boundIndexBuffer, i = { ...this._boundBindGroup };
370
+ this._clearCache();
371
+ const n = this._renderer.renderTarget.viewport;
372
+ this.renderPassEncoder.setViewport(n.x, n.y, n.width, n.height, 0, 1), this.setPipeline(t);
373
+ for (const o in r)
374
+ this._setVertexBuffer(o, r[o]);
375
+ for (const o in i)
376
+ this.setBindGroup(o, i[o], null);
377
+ this._setIndexBuffer(s);
378
+ }
379
+ _clearCache() {
380
+ for (let e = 0; e < 16; e++)
381
+ this._boundBindGroup[e] = null, this._boundVertexBuffer[e] = null;
382
+ this._boundIndexBuffer = null, this._boundPipeline = null;
383
+ }
384
+ destroy() {
385
+ this._renderer = null, this._gpu = null, this._boundBindGroup = null, this._boundVertexBuffer = null, this._boundIndexBuffer = null, this._boundPipeline = null;
386
+ }
387
+ contextChange(e) {
388
+ this._gpu = e;
389
+ }
390
+ }
391
+ j.extension = {
392
+ type: [l.WebGPUSystem],
393
+ name: "encoder",
394
+ priority: 1
395
+ };
396
+ class K {
397
+ constructor(e) {
398
+ this._renderer = e;
399
+ }
400
+ contextChange() {
401
+ this.maxTextures = this._renderer.device.gpu.device.limits.maxSampledTexturesPerShaderStage, this.maxBatchableTextures = this.maxTextures;
402
+ }
403
+ destroy() {
404
+ }
405
+ }
406
+ K.extension = {
407
+ type: [
408
+ l.WebGPUSystem
409
+ ],
410
+ name: "limits"
411
+ };
412
+ class q {
413
+ constructor(e) {
414
+ this._renderTargetStencilState = /* @__PURE__ */ Object.create(null), this._renderer = e, e.renderTarget.onRenderTargetChange.add(this);
415
+ }
416
+ onRenderTargetChange(e) {
417
+ let t = this._renderTargetStencilState[e.uid];
418
+ t || (t = this._renderTargetStencilState[e.uid] = {
419
+ stencilMode: T.DISABLED,
420
+ stencilReference: 0
421
+ }), this._activeRenderTarget = e, this.setStencilMode(t.stencilMode, t.stencilReference);
422
+ }
423
+ setStencilMode(e, t) {
424
+ const r = this._renderTargetStencilState[this._activeRenderTarget.uid];
425
+ r.stencilMode = e, r.stencilReference = t;
426
+ const s = this._renderer;
427
+ s.pipeline.setStencilMode(e), s.encoder.renderPassEncoder.setStencilReference(t);
428
+ }
429
+ destroy() {
430
+ this._renderer.renderTarget.onRenderTargetChange.remove(this), this._renderer = null, this._activeRenderTarget = null, this._renderTargetStencilState = null;
431
+ }
432
+ }
433
+ q.extension = {
434
+ type: [
435
+ l.WebGPUSystem
436
+ ],
437
+ name: "stencil"
438
+ };
439
+ const b = 128;
440
+ class Y {
441
+ constructor(e) {
442
+ this._bindGroupHash = /* @__PURE__ */ Object.create(null), this._buffers = [], this._bindGroups = [], this._bufferResources = [], this._renderer = e, this._batchBuffer = new ve({ minUniformOffsetAlignment: b });
443
+ const t = 256 / b;
444
+ for (let r = 0; r < t; r++) {
445
+ let s = S.UNIFORM | S.COPY_DST;
446
+ r === 0 && (s |= S.COPY_SRC), this._buffers.push(new ue({
447
+ data: this._batchBuffer.data,
448
+ usage: s
449
+ }));
450
+ }
451
+ }
452
+ renderEnd() {
453
+ this._uploadBindGroups(), this._resetBindGroups();
454
+ }
455
+ _resetBindGroups() {
456
+ this._bindGroupHash = /* @__PURE__ */ Object.create(null), this._batchBuffer.clear();
457
+ }
458
+ // just works for single bind groups for now
459
+ getUniformBindGroup(e, t) {
460
+ if (!t && this._bindGroupHash[e.uid])
461
+ return this._bindGroupHash[e.uid];
462
+ this._renderer.ubo.ensureUniformGroup(e);
463
+ const r = e.buffer.data, s = this._batchBuffer.addEmptyGroup(r.length);
464
+ return this._renderer.ubo.syncUniformGroup(e, this._batchBuffer.data, s / 4), this._bindGroupHash[e.uid] = this._getBindGroup(s / b), this._bindGroupHash[e.uid];
465
+ }
466
+ getUboResource(e) {
467
+ this._renderer.ubo.updateUniformGroup(e);
468
+ const t = e.buffer.data, r = this._batchBuffer.addGroup(t);
469
+ return this._getBufferResource(r / b);
470
+ }
471
+ getArrayBindGroup(e) {
472
+ const t = this._batchBuffer.addGroup(e);
473
+ return this._getBindGroup(t / b);
474
+ }
475
+ getArrayBufferResource(e) {
476
+ const r = this._batchBuffer.addGroup(e) / b;
477
+ return this._getBufferResource(r);
478
+ }
479
+ _getBufferResource(e) {
480
+ if (!this._bufferResources[e]) {
481
+ const t = this._buffers[e % 2];
482
+ this._bufferResources[e] = new ce({
483
+ buffer: t,
484
+ offset: (e / 2 | 0) * 256,
485
+ size: b
486
+ });
487
+ }
488
+ return this._bufferResources[e];
489
+ }
490
+ _getBindGroup(e) {
491
+ if (!this._bindGroups[e]) {
492
+ const t = new L({
493
+ 0: this._getBufferResource(e)
494
+ });
495
+ this._bindGroups[e] = t;
496
+ }
497
+ return this._bindGroups[e];
498
+ }
499
+ _uploadBindGroups() {
500
+ const e = this._renderer.buffer, t = this._buffers[0];
501
+ t.update(this._batchBuffer.byteIndex), e.updateBuffer(t);
502
+ const r = this._renderer.gpu.device.createCommandEncoder();
503
+ for (let s = 1; s < this._buffers.length; s++) {
504
+ const i = this._buffers[s];
505
+ r.copyBufferToBuffer(
506
+ e.getGPUBuffer(t),
507
+ b,
508
+ e.getGPUBuffer(i),
509
+ 0,
510
+ this._batchBuffer.byteIndex
511
+ );
512
+ }
513
+ this._renderer.gpu.device.queue.submit([r.finish()]);
514
+ }
515
+ destroy() {
516
+ var e;
517
+ for (let t = 0; t < this._bindGroups.length; t++)
518
+ (e = this._bindGroups[t]) == null || e.destroy();
519
+ this._bindGroups = null, this._bindGroupHash = null;
520
+ for (let t = 0; t < this._buffers.length; t++)
521
+ this._buffers[t].destroy();
522
+ this._buffers = null;
523
+ for (let t = 0; t < this._bufferResources.length; t++)
524
+ this._bufferResources[t].destroy();
525
+ this._bufferResources = null, this._batchBuffer.destroy(), this._renderer = null;
526
+ }
527
+ }
528
+ Y.extension = {
529
+ type: [
530
+ l.WebGPUPipes
531
+ ],
532
+ name: "uniformBatch"
533
+ };
534
+ const Ue = {
535
+ "point-list": 0,
536
+ "line-list": 1,
537
+ "line-strip": 2,
538
+ "triangle-list": 3,
539
+ "triangle-strip": 4
540
+ };
541
+ function we(a, e, t, r, s) {
542
+ return a << 24 | e << 16 | t << 10 | r << 5 | s;
543
+ }
544
+ function Me(a, e, t, r, s) {
545
+ return t << 8 | a << 5 | r << 3 | s << 1 | e;
546
+ }
547
+ class X {
548
+ constructor(e) {
549
+ this._moduleCache = /* @__PURE__ */ Object.create(null), this._bufferLayoutsCache = /* @__PURE__ */ Object.create(null), this._bindingNamesCache = /* @__PURE__ */ Object.create(null), this._pipeCache = /* @__PURE__ */ Object.create(null), this._pipeStateCaches = /* @__PURE__ */ Object.create(null), this._colorMask = 15, this._multisampleCount = 1, this._colorTargetCount = 1, this._renderer = e;
550
+ }
551
+ contextChange(e) {
552
+ this._gpu = e, this.setStencilMode(T.DISABLED), this._updatePipeHash();
553
+ }
554
+ setMultisampleCount(e) {
555
+ this._multisampleCount !== e && (this._multisampleCount = e, this._updatePipeHash());
556
+ }
557
+ setRenderTarget(e) {
558
+ this._multisampleCount = e.msaaSamples, this._depthStencilAttachment = e.descriptor.depthStencilAttachment ? 1 : 0, this._colorTargetCount = e.colorTargetCount, this._updatePipeHash();
559
+ }
560
+ setColorMask(e) {
561
+ this._colorMask !== e && (this._colorMask = e, this._updatePipeHash());
562
+ }
563
+ setStencilMode(e) {
564
+ this._stencilMode !== e && (this._stencilMode = e, this._stencilState = Ge[e], this._updatePipeHash());
565
+ }
566
+ setPipeline(e, t, r, s) {
567
+ const i = this.getPipeline(e, t, r);
568
+ s.setPipeline(i);
569
+ }
570
+ getPipeline(e, t, r, s) {
571
+ e._layoutKey || (Pe(e, t.attributeData), this._generateBufferKey(e)), s || (s = e.topology);
572
+ const i = we(
573
+ e._layoutKey,
574
+ t._layoutKey,
575
+ r.data,
576
+ r._blendModeId,
577
+ Ue[s]
578
+ );
579
+ return this._pipeCache[i] ? this._pipeCache[i] : (this._pipeCache[i] = this._createPipeline(e, t, r, s), this._pipeCache[i]);
580
+ }
581
+ _createPipeline(e, t, r, s) {
582
+ const i = this._gpu.device, n = this._createVertexBufferLayouts(e, t), o = this._renderer.state.getColorTargets(r, this._colorTargetCount), u = this._stencilMode === T.RENDERING_MASK_ADD ? 0 : this._colorMask;
583
+ for (let p = 0; p < o.length; p++)
584
+ o[p].writeMask = u;
585
+ const c = this._renderer.shader.getProgramData(t).pipeline, f = {
586
+ // TODO later check if its helpful to create..
587
+ // layout,
588
+ vertex: {
589
+ module: this._getModule(t.vertex.source),
590
+ entryPoint: t.vertex.entryPoint,
591
+ // geometry..
592
+ buffers: n
593
+ },
594
+ fragment: {
595
+ module: this._getModule(t.fragment.source),
596
+ entryPoint: t.fragment.entryPoint,
597
+ targets: o
598
+ },
599
+ primitive: {
600
+ topology: s,
601
+ cullMode: r.cullMode
602
+ },
603
+ layout: c,
604
+ multisample: {
605
+ count: this._multisampleCount
606
+ },
607
+ // depthStencil,
608
+ label: "PIXI Pipeline"
609
+ };
610
+ return this._depthStencilAttachment && (f.depthStencil = {
611
+ ...this._stencilState,
612
+ format: "depth24plus-stencil8",
613
+ depthWriteEnabled: r.depthTest,
614
+ depthCompare: r.depthTest ? "less" : "always"
615
+ }), i.createRenderPipeline(f);
616
+ }
617
+ _getModule(e) {
618
+ return this._moduleCache[e] || this._createModule(e);
619
+ }
620
+ _createModule(e) {
621
+ const t = this._gpu.device;
622
+ return this._moduleCache[e] = t.createShaderModule({
623
+ code: e
624
+ }), this._moduleCache[e];
625
+ }
626
+ _generateBufferKey(e) {
627
+ const t = [];
628
+ let r = 0;
629
+ const s = Object.keys(e.attributes).sort();
630
+ for (let n = 0; n < s.length; n++) {
631
+ const o = e.attributes[s[n]];
632
+ t[r++] = o.offset, t[r++] = o.format, t[r++] = o.stride, t[r++] = o.instance;
633
+ }
634
+ const i = t.join("|");
635
+ return e._layoutKey = U(i, "geometry"), e._layoutKey;
636
+ }
637
+ _generateAttributeLocationsKey(e) {
638
+ const t = [];
639
+ let r = 0;
640
+ const s = Object.keys(e.attributeData).sort();
641
+ for (let n = 0; n < s.length; n++) {
642
+ const o = e.attributeData[s[n]];
643
+ t[r++] = o.location;
644
+ }
645
+ const i = t.join("|");
646
+ return e._attributeLocationsKey = U(i, "programAttributes"), e._attributeLocationsKey;
647
+ }
648
+ /**
649
+ * Returns a hash of buffer names mapped to bind locations.
650
+ * This is used to bind the correct buffer to the correct location in the shader.
651
+ * @param geometry - The geometry where to get the buffer names
652
+ * @param program - The program where to get the buffer names
653
+ * @returns An object of buffer names mapped to the bind location.
654
+ */
655
+ getBufferNamesToBind(e, t) {
656
+ const r = e._layoutKey << 16 | t._attributeLocationsKey;
657
+ if (this._bindingNamesCache[r]) return this._bindingNamesCache[r];
658
+ const s = this._createVertexBufferLayouts(e, t), i = /* @__PURE__ */ Object.create(null), n = t.attributeData;
659
+ for (let o = 0; o < s.length; o++) {
660
+ const c = Object.values(s[o].attributes)[0].shaderLocation;
661
+ for (const f in n)
662
+ if (n[f].location === c) {
663
+ i[o] = f;
664
+ break;
665
+ }
666
+ }
667
+ return this._bindingNamesCache[r] = i, i;
668
+ }
669
+ _createVertexBufferLayouts(e, t) {
670
+ t._attributeLocationsKey || this._generateAttributeLocationsKey(t);
671
+ const r = e._layoutKey << 16 | t._attributeLocationsKey;
672
+ if (this._bufferLayoutsCache[r])
673
+ return this._bufferLayoutsCache[r];
674
+ const s = [];
675
+ return e.buffers.forEach((i) => {
676
+ const n = {
677
+ arrayStride: 0,
678
+ stepMode: "vertex",
679
+ attributes: []
680
+ }, o = n.attributes;
681
+ for (const u in t.attributeData) {
682
+ const c = e.attributes[u];
683
+ (c.divisor ?? 1) !== 1 && C(`Attribute ${u} has an invalid divisor value of '${c.divisor}'. WebGPU only supports a divisor value of 1`), c.buffer === i && (n.arrayStride = c.stride, n.stepMode = c.instance ? "instance" : "vertex", o.push({
684
+ shaderLocation: t.attributeData[u].location,
685
+ offset: c.offset,
686
+ format: c.format
687
+ }));
688
+ }
689
+ o.length && s.push(n);
690
+ }), this._bufferLayoutsCache[r] = s, s;
691
+ }
692
+ _updatePipeHash() {
693
+ const e = Me(
694
+ this._stencilMode,
695
+ this._multisampleCount,
696
+ this._colorMask,
697
+ this._depthStencilAttachment,
698
+ this._colorTargetCount
699
+ );
700
+ this._pipeStateCaches[e] || (this._pipeStateCaches[e] = /* @__PURE__ */ Object.create(null)), this._pipeCache = this._pipeStateCaches[e];
701
+ }
702
+ destroy() {
703
+ this._renderer = null, this._bufferLayoutsCache = null;
704
+ }
705
+ }
706
+ X.extension = {
707
+ type: [l.WebGPUSystem],
708
+ name: "pipeline"
709
+ };
710
+ class Re {
711
+ constructor() {
712
+ this.contexts = [], this.msaaTextures = [], this.msaaSamples = 1;
713
+ }
714
+ }
715
+ class Ee {
716
+ init(e, t) {
717
+ this._renderer = e, this._renderTargetSystem = t;
718
+ }
719
+ copyToTexture(e, t, r, s, i) {
720
+ const n = this._renderer, o = this._getGpuColorTexture(
721
+ e
722
+ ), u = n.texture.getGpuSource(
723
+ t.source
724
+ );
725
+ return n.encoder.commandEncoder.copyTextureToTexture(
726
+ {
727
+ texture: o,
728
+ origin: r
729
+ },
730
+ {
731
+ texture: u,
732
+ origin: i
733
+ },
734
+ s
735
+ ), t;
736
+ }
737
+ startRenderPass(e, t = !0, r, s, i = 0, n = 0) {
738
+ var f, h;
739
+ const u = this._renderTargetSystem.getGpuRenderTarget(e);
740
+ if (n !== 0 && ((f = u.msaaTextures) != null && f.length))
741
+ throw new Error("[RenderTargetSystem] Rendering to array layers is not supported with MSAA render targets.");
742
+ if (i > 0 && ((h = u.msaaTextures) != null && h.length))
743
+ throw new Error("[RenderTargetSystem] Rendering to mip levels is not supported with MSAA render targets.");
744
+ const c = this.getDescriptor(e, t, r, i, n);
745
+ u.descriptor = c, this._renderer.pipeline.setRenderTarget(u), this._renderer.encoder.beginRenderPass(u), this._renderer.encoder.setViewport(s);
746
+ }
747
+ finishRenderPass() {
748
+ this._renderer.encoder.endRenderPass();
749
+ }
750
+ /**
751
+ * returns the gpu texture for the first color texture in the render target
752
+ * mainly used by the filter manager to get copy the texture for blending
753
+ * @param renderTarget
754
+ * @returns a gpu texture
755
+ */
756
+ _getGpuColorTexture(e) {
757
+ const t = this._renderTargetSystem.getGpuRenderTarget(e);
758
+ return t.contexts[0] ? t.contexts[0].getCurrentTexture() : this._renderer.texture.getGpuSource(
759
+ e.colorTextures[0].source
760
+ );
761
+ }
762
+ getDescriptor(e, t, r, s = 0, i = 0) {
763
+ typeof t == "boolean" && (t = t ? y.ALL : y.NONE);
764
+ const n = this._renderTargetSystem, o = n.getGpuRenderTarget(e), u = e.colorTextures.map(
765
+ (h, p) => {
766
+ const d = o.contexts[p];
767
+ let g, m;
768
+ if (d) {
769
+ if (i !== 0)
770
+ throw new Error("[RenderTargetSystem] Rendering to array layers is not supported for canvas targets.");
771
+ g = d.getCurrentTexture().createView();
772
+ } else
773
+ g = this._renderer.texture.getGpuSource(h).createView({
774
+ // Render attachments must be 2d views; for array/cube textures we select a single layer.
775
+ dimension: "2d",
776
+ baseMipLevel: s,
777
+ mipLevelCount: 1,
778
+ baseArrayLayer: i,
779
+ arrayLayerCount: 1
780
+ });
781
+ o.msaaTextures[p] && (m = g, g = this._renderer.texture.getTextureView(
782
+ o.msaaTextures[p]
783
+ ));
784
+ const x = t & y.COLOR ? "clear" : "load";
785
+ return r ?? (r = n.defaultClearColor), {
786
+ view: g,
787
+ resolveTarget: m,
788
+ clearValue: r,
789
+ storeOp: "store",
790
+ loadOp: x
791
+ };
792
+ }
793
+ );
794
+ let c;
795
+ if ((e.stencil || e.depth) && !e.depthStencilTexture && (e.ensureDepthStencilTexture(), e.depthStencilTexture.source.sampleCount = o.msaa ? 4 : 1), e.depthStencilTexture) {
796
+ const h = t & y.STENCIL ? "clear" : "load", p = t & y.DEPTH ? "clear" : "load";
797
+ c = {
798
+ view: this._renderer.texture.getGpuSource(e.depthStencilTexture.source).createView({
799
+ dimension: "2d",
800
+ baseMipLevel: s,
801
+ mipLevelCount: 1,
802
+ baseArrayLayer: i,
803
+ arrayLayerCount: 1
804
+ }),
805
+ stencilStoreOp: "store",
806
+ stencilLoadOp: h,
807
+ depthClearValue: 1,
808
+ depthLoadOp: p,
809
+ depthStoreOp: "store"
810
+ };
811
+ }
812
+ return {
813
+ colorAttachments: u,
814
+ depthStencilAttachment: c
815
+ };
816
+ }
817
+ clear(e, t = !0, r, s, i = 0, n = 0) {
818
+ if (!t) return;
819
+ const { gpu: o, encoder: u } = this._renderer, c = o.device;
820
+ if (u.commandEncoder === null) {
821
+ const h = c.createCommandEncoder(), p = this.getDescriptor(e, t, r, i, n), d = h.beginRenderPass(p);
822
+ d.setViewport(s.x, s.y, s.width, s.height, 0, 1), d.end();
823
+ const g = h.finish();
824
+ c.queue.submit([g]);
825
+ } else
826
+ this.startRenderPass(e, t, r, s, i, n);
827
+ }
828
+ initGpuRenderTarget(e) {
829
+ e.isRoot = !0;
830
+ const t = new Re();
831
+ return t.colorTargetCount = e.colorTextures.length, e.colorTextures.forEach((r, s) => {
832
+ if (r instanceof de) {
833
+ const i = r.resource.getContext(
834
+ "webgpu"
835
+ ), n = r.transparent ? "premultiplied" : "opaque";
836
+ try {
837
+ i.configure({
838
+ device: this._renderer.gpu.device,
839
+ usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST | GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_SRC,
840
+ format: "bgra8unorm",
841
+ alphaMode: n
842
+ });
843
+ } catch (o) {
844
+ console.error(o);
845
+ }
846
+ t.contexts[s] = i;
847
+ }
848
+ if (t.msaa = r.source.antialias, r.source.antialias) {
849
+ const i = new he({
850
+ width: 0,
851
+ height: 0,
852
+ sampleCount: 4,
853
+ arrayLayerCount: r.source.arrayLayerCount
854
+ });
855
+ t.msaaTextures[s] = i;
856
+ }
857
+ }), t.msaa && (t.msaaSamples = 4, e.depthStencilTexture && (e.depthStencilTexture.source.sampleCount = 4)), t;
858
+ }
859
+ destroyGpuRenderTarget(e) {
860
+ e.contexts.forEach((t) => {
861
+ t.unconfigure();
862
+ }), e.msaaTextures.forEach((t) => {
863
+ t.destroy();
864
+ }), e.msaaTextures.length = 0, e.contexts.length = 0;
865
+ }
866
+ ensureDepthStencilTexture(e) {
867
+ const t = this._renderTargetSystem.getGpuRenderTarget(e);
868
+ e.depthStencilTexture && t.msaa && (e.depthStencilTexture.source.sampleCount = 4);
869
+ }
870
+ resizeGpuRenderTarget(e) {
871
+ const t = this._renderTargetSystem.getGpuRenderTarget(e);
872
+ t.width = e.width, t.height = e.height, t.msaa && e.colorTextures.forEach((r, s) => {
873
+ const i = t.msaaTextures[s];
874
+ i == null || i.resize(
875
+ r.source.width,
876
+ r.source.height,
877
+ r.source._resolution
878
+ );
879
+ });
880
+ }
881
+ }
882
+ class $ extends me {
883
+ constructor(e) {
884
+ super(e), this.adaptor = new Ee(), this.adaptor.init(e, this);
885
+ }
886
+ }
887
+ $.extension = {
888
+ type: [l.WebGPUSystem],
889
+ name: "renderTarget"
890
+ };
891
+ class J {
892
+ constructor() {
893
+ this._gpuProgramData = /* @__PURE__ */ Object.create(null);
894
+ }
895
+ contextChange(e) {
896
+ this._gpu = e;
897
+ }
898
+ getProgramData(e) {
899
+ return this._gpuProgramData[e._layoutKey] || this._createGPUProgramData(e);
900
+ }
901
+ _createGPUProgramData(e) {
902
+ const t = this._gpu.device, r = e.gpuLayout.map((i) => t.createBindGroupLayout({ entries: i })), s = { bindGroupLayouts: r };
903
+ return this._gpuProgramData[e._layoutKey] = {
904
+ bindGroups: r,
905
+ pipeline: t.createPipelineLayout(s)
906
+ }, this._gpuProgramData[e._layoutKey];
907
+ }
908
+ destroy() {
909
+ this._gpu = null, this._gpuProgramData = null;
910
+ }
911
+ }
912
+ J.extension = {
913
+ type: [
914
+ l.WebGPUSystem
915
+ ],
916
+ name: "shader"
917
+ };
918
+ const _ = {};
919
+ _.normal = {
920
+ alpha: {
921
+ srcFactor: "one",
922
+ dstFactor: "one-minus-src-alpha",
923
+ operation: "add"
924
+ },
925
+ color: {
926
+ srcFactor: "one",
927
+ dstFactor: "one-minus-src-alpha",
928
+ operation: "add"
929
+ }
930
+ };
931
+ _.add = {
932
+ alpha: {
933
+ srcFactor: "src-alpha",
934
+ dstFactor: "one-minus-src-alpha",
935
+ operation: "add"
936
+ },
937
+ color: {
938
+ srcFactor: "one",
939
+ dstFactor: "one",
940
+ operation: "add"
941
+ }
942
+ };
943
+ _.multiply = {
944
+ alpha: {
945
+ srcFactor: "one",
946
+ dstFactor: "one-minus-src-alpha",
947
+ operation: "add"
948
+ },
949
+ color: {
950
+ srcFactor: "dst",
951
+ dstFactor: "one-minus-src-alpha",
952
+ operation: "add"
953
+ }
954
+ };
955
+ _.screen = {
956
+ alpha: {
957
+ srcFactor: "one",
958
+ dstFactor: "one-minus-src-alpha",
959
+ operation: "add"
960
+ },
961
+ color: {
962
+ srcFactor: "one",
963
+ dstFactor: "one-minus-src",
964
+ operation: "add"
965
+ }
966
+ };
967
+ _.overlay = {
968
+ alpha: {
969
+ srcFactor: "one",
970
+ dstFactor: "one-minus-src-alpha",
971
+ operation: "add"
972
+ },
973
+ color: {
974
+ srcFactor: "one",
975
+ dstFactor: "one-minus-src",
976
+ operation: "add"
977
+ }
978
+ };
979
+ _.none = {
980
+ alpha: {
981
+ srcFactor: "one",
982
+ dstFactor: "one-minus-src-alpha",
983
+ operation: "add"
984
+ },
985
+ color: {
986
+ srcFactor: "zero",
987
+ dstFactor: "zero",
988
+ operation: "add"
989
+ }
990
+ };
991
+ _["normal-npm"] = {
992
+ alpha: {
993
+ srcFactor: "one",
994
+ dstFactor: "one-minus-src-alpha",
995
+ operation: "add"
996
+ },
997
+ color: {
998
+ srcFactor: "src-alpha",
999
+ dstFactor: "one-minus-src-alpha",
1000
+ operation: "add"
1001
+ }
1002
+ };
1003
+ _["add-npm"] = {
1004
+ alpha: {
1005
+ srcFactor: "one",
1006
+ dstFactor: "one",
1007
+ operation: "add"
1008
+ },
1009
+ color: {
1010
+ srcFactor: "src-alpha",
1011
+ dstFactor: "one",
1012
+ operation: "add"
1013
+ }
1014
+ };
1015
+ _["screen-npm"] = {
1016
+ alpha: {
1017
+ srcFactor: "one",
1018
+ dstFactor: "one-minus-src-alpha",
1019
+ operation: "add"
1020
+ },
1021
+ color: {
1022
+ srcFactor: "src-alpha",
1023
+ dstFactor: "one-minus-src",
1024
+ operation: "add"
1025
+ }
1026
+ };
1027
+ _.erase = {
1028
+ alpha: {
1029
+ srcFactor: "zero",
1030
+ dstFactor: "one-minus-src-alpha",
1031
+ operation: "add"
1032
+ },
1033
+ color: {
1034
+ srcFactor: "zero",
1035
+ dstFactor: "one-minus-src",
1036
+ operation: "add"
1037
+ }
1038
+ };
1039
+ _.min = {
1040
+ alpha: {
1041
+ srcFactor: "one",
1042
+ dstFactor: "one",
1043
+ operation: "min"
1044
+ },
1045
+ color: {
1046
+ srcFactor: "one",
1047
+ dstFactor: "one",
1048
+ operation: "min"
1049
+ }
1050
+ };
1051
+ _.max = {
1052
+ alpha: {
1053
+ srcFactor: "one",
1054
+ dstFactor: "one",
1055
+ operation: "max"
1056
+ },
1057
+ color: {
1058
+ srcFactor: "one",
1059
+ dstFactor: "one",
1060
+ operation: "max"
1061
+ }
1062
+ };
1063
+ class Q {
1064
+ constructor() {
1065
+ this.defaultState = new E(), this.defaultState.blend = !0;
1066
+ }
1067
+ contextChange(e) {
1068
+ this.gpu = e;
1069
+ }
1070
+ /**
1071
+ * Gets the blend mode data for the current state
1072
+ * @param state - The state to get the blend mode from
1073
+ * @param count - The number of color targets to create
1074
+ */
1075
+ getColorTargets(e, t) {
1076
+ const r = _[e.blendMode] || _.normal, s = [], i = {
1077
+ format: "bgra8unorm",
1078
+ writeMask: 0,
1079
+ blend: r
1080
+ };
1081
+ for (let n = 0; n < t; n++)
1082
+ s[n] = i;
1083
+ return s;
1084
+ }
1085
+ destroy() {
1086
+ this.gpu = null;
1087
+ }
1088
+ }
1089
+ Q.extension = {
1090
+ type: [
1091
+ l.WebGPUSystem
1092
+ ],
1093
+ name: "state"
1094
+ };
1095
+ const Le = {
1096
+ type: "image",
1097
+ upload(a, e, t, r = 0) {
1098
+ const s = a.resource, i = (a.pixelWidth | 0) * (a.pixelHeight | 0), n = s.byteLength / i;
1099
+ t.device.queue.writeTexture(
1100
+ { texture: e, origin: { x: 0, y: 0, z: r } },
1101
+ s,
1102
+ {
1103
+ offset: 0,
1104
+ rowsPerImage: a.pixelHeight,
1105
+ bytesPerRow: a.pixelWidth * n
1106
+ },
1107
+ {
1108
+ width: a.pixelWidth,
1109
+ height: a.pixelHeight,
1110
+ depthOrArrayLayers: 1
1111
+ }
1112
+ );
1113
+ }
1114
+ }, Z = {
1115
+ "bc1-rgba-unorm": { blockBytes: 8, blockWidth: 4, blockHeight: 4 },
1116
+ "bc2-rgba-unorm": { blockBytes: 16, blockWidth: 4, blockHeight: 4 },
1117
+ "bc3-rgba-unorm": { blockBytes: 16, blockWidth: 4, blockHeight: 4 },
1118
+ "bc7-rgba-unorm": { blockBytes: 16, blockWidth: 4, blockHeight: 4 },
1119
+ "etc1-rgb-unorm": { blockBytes: 8, blockWidth: 4, blockHeight: 4 },
1120
+ "etc2-rgba8unorm": { blockBytes: 16, blockWidth: 4, blockHeight: 4 },
1121
+ "astc-4x4-unorm": { blockBytes: 16, blockWidth: 4, blockHeight: 4 }
1122
+ }, Ae = { blockBytes: 4, blockWidth: 1, blockHeight: 1 }, De = {
1123
+ type: "compressed",
1124
+ upload(a, e, t, r = 0) {
1125
+ let s = a.pixelWidth, i = a.pixelHeight;
1126
+ const n = Z[a.format] || Ae;
1127
+ for (let o = 0; o < a.resource.length; o++) {
1128
+ const u = a.resource[o], c = Math.ceil(s / n.blockWidth) * n.blockBytes;
1129
+ t.device.queue.writeTexture(
1130
+ {
1131
+ texture: e,
1132
+ mipLevel: o,
1133
+ origin: { x: 0, y: 0, z: r }
1134
+ },
1135
+ u,
1136
+ {
1137
+ offset: 0,
1138
+ bytesPerRow: c
1139
+ },
1140
+ {
1141
+ width: Math.ceil(s / n.blockWidth) * n.blockWidth,
1142
+ height: Math.ceil(i / n.blockHeight) * n.blockHeight,
1143
+ depthOrArrayLayers: 1
1144
+ }
1145
+ ), s = Math.max(s >> 1, 1), i = Math.max(i >> 1, 1);
1146
+ }
1147
+ }
1148
+ }, R = ["right", "left", "top", "bottom", "front", "back"];
1149
+ function ke(a) {
1150
+ return {
1151
+ type: "cube",
1152
+ upload(e, t, r) {
1153
+ const s = e.faces;
1154
+ for (let i = 0; i < R.length; i++) {
1155
+ const n = R[i], o = s[n];
1156
+ (a[o.uploadMethodId] || a.image).upload(o, t, r, i);
1157
+ }
1158
+ }
1159
+ };
1160
+ }
1161
+ const ee = {
1162
+ type: "image",
1163
+ upload(a, e, t, r = 0) {
1164
+ const s = a.resource;
1165
+ if (!s) return;
1166
+ if (globalThis.HTMLImageElement && s instanceof HTMLImageElement) {
1167
+ const u = P.get().createCanvas(s.width, s.height);
1168
+ u.getContext("2d").drawImage(s, 0, 0, s.width, s.height), a.resource = u, C("ImageSource: Image element passed, converting to canvas and replacing resource.");
1169
+ }
1170
+ const i = Math.min(e.width, a.resourceWidth || a.pixelWidth), n = Math.min(e.height, a.resourceHeight || a.pixelHeight), o = a.alphaMode === "premultiply-alpha-on-upload";
1171
+ t.device.queue.copyExternalImageToTexture(
1172
+ { source: s },
1173
+ { texture: e, origin: { x: 0, y: 0, z: r }, premultipliedAlpha: o },
1174
+ {
1175
+ width: i,
1176
+ height: n
1177
+ }
1178
+ );
1179
+ }
1180
+ }, Fe = {
1181
+ type: "video",
1182
+ upload(a, e, t, r) {
1183
+ ee.upload(a, e, t, r);
1184
+ }
1185
+ };
1186
+ class Oe {
1187
+ constructor(e) {
1188
+ this.device = e, this.sampler = e.createSampler({ minFilter: "linear" }), this.pipelines = {};
1189
+ }
1190
+ _getMipmapPipeline(e) {
1191
+ let t = this.pipelines[e];
1192
+ return t || (this.mipmapShaderModule || (this.mipmapShaderModule = this.device.createShaderModule({
1193
+ code: (
1194
+ /* wgsl */
1195
+ `
1196
+ var<private> pos : array<vec2<f32>, 3> = array<vec2<f32>, 3>(
1197
+ vec2<f32>(-1.0, -1.0), vec2<f32>(-1.0, 3.0), vec2<f32>(3.0, -1.0));
1198
+
1199
+ struct VertexOutput {
1200
+ @builtin(position) position : vec4<f32>,
1201
+ @location(0) texCoord : vec2<f32>,
1202
+ };
1203
+
1204
+ @vertex
1205
+ fn vertexMain(@builtin(vertex_index) vertexIndex : u32) -> VertexOutput {
1206
+ var output : VertexOutput;
1207
+ output.texCoord = pos[vertexIndex] * vec2<f32>(0.5, -0.5) + vec2<f32>(0.5);
1208
+ output.position = vec4<f32>(pos[vertexIndex], 0.0, 1.0);
1209
+ return output;
1210
+ }
1211
+
1212
+ @group(0) @binding(0) var imgSampler : sampler;
1213
+ @group(0) @binding(1) var img : texture_2d<f32>;
1214
+
1215
+ @fragment
1216
+ fn fragmentMain(@location(0) texCoord : vec2<f32>) -> @location(0) vec4<f32> {
1217
+ return textureSample(img, imgSampler, texCoord);
1218
+ }
1219
+ `
1220
+ )
1221
+ })), t = this.device.createRenderPipeline({
1222
+ layout: "auto",
1223
+ vertex: {
1224
+ module: this.mipmapShaderModule,
1225
+ entryPoint: "vertexMain"
1226
+ },
1227
+ fragment: {
1228
+ module: this.mipmapShaderModule,
1229
+ entryPoint: "fragmentMain",
1230
+ targets: [{ format: e }]
1231
+ }
1232
+ }), this.pipelines[e] = t), t;
1233
+ }
1234
+ /**
1235
+ * Generates mipmaps for the given GPUTexture from the data in level 0.
1236
+ * @param {module:External.GPUTexture} texture - Texture to generate mipmaps for.
1237
+ * @returns {module:External.GPUTexture} - The originally passed texture
1238
+ */
1239
+ generateMipmap(e) {
1240
+ const t = this._getMipmapPipeline(e.format);
1241
+ if (e.dimension === "3d" || e.dimension === "1d")
1242
+ throw new Error("Generating mipmaps for non-2d textures is currently unsupported!");
1243
+ let r = e;
1244
+ const s = e.depthOrArrayLayers || 1, i = e.usage & GPUTextureUsage.RENDER_ATTACHMENT;
1245
+ if (!i) {
1246
+ const u = {
1247
+ size: {
1248
+ width: Math.ceil(e.width / 2),
1249
+ height: Math.ceil(e.height / 2),
1250
+ depthOrArrayLayers: s
1251
+ },
1252
+ format: e.format,
1253
+ usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_SRC | GPUTextureUsage.RENDER_ATTACHMENT,
1254
+ mipLevelCount: e.mipLevelCount - 1
1255
+ };
1256
+ r = this.device.createTexture(u);
1257
+ }
1258
+ const n = this.device.createCommandEncoder({}), o = t.getBindGroupLayout(0);
1259
+ for (let u = 0; u < s; ++u) {
1260
+ let c = e.createView({
1261
+ baseMipLevel: 0,
1262
+ mipLevelCount: 1,
1263
+ dimension: "2d",
1264
+ baseArrayLayer: u,
1265
+ arrayLayerCount: 1
1266
+ }), f = i ? 1 : 0;
1267
+ for (let h = 1; h < e.mipLevelCount; ++h) {
1268
+ const p = r.createView({
1269
+ baseMipLevel: f++,
1270
+ mipLevelCount: 1,
1271
+ dimension: "2d",
1272
+ baseArrayLayer: u,
1273
+ arrayLayerCount: 1
1274
+ }), d = n.beginRenderPass({
1275
+ colorAttachments: [{
1276
+ view: p,
1277
+ storeOp: "store",
1278
+ loadOp: "clear",
1279
+ clearValue: { r: 0, g: 0, b: 0, a: 0 }
1280
+ }]
1281
+ }), g = this.device.createBindGroup({
1282
+ layout: o,
1283
+ entries: [{
1284
+ binding: 0,
1285
+ resource: this.sampler
1286
+ }, {
1287
+ binding: 1,
1288
+ resource: c
1289
+ }]
1290
+ });
1291
+ d.setPipeline(t), d.setBindGroup(0, g), d.draw(3, 1, 0, 0), d.end(), c = p;
1292
+ }
1293
+ }
1294
+ if (!i) {
1295
+ const u = {
1296
+ width: Math.ceil(e.width / 2),
1297
+ height: Math.ceil(e.height / 2),
1298
+ depthOrArrayLayers: s
1299
+ };
1300
+ for (let c = 1; c < e.mipLevelCount; ++c)
1301
+ n.copyTextureToTexture({
1302
+ texture: r,
1303
+ mipLevel: c - 1
1304
+ }, {
1305
+ texture: e,
1306
+ mipLevel: c
1307
+ }, u), u.width = Math.ceil(u.width / 2), u.height = Math.ceil(u.height / 2);
1308
+ }
1309
+ return this.device.queue.submit([n.finish()]), i || r.destroy(), e;
1310
+ }
1311
+ }
1312
+ class Ie {
1313
+ constructor(e) {
1314
+ this.textureView = null, this.gpuTexture = e;
1315
+ }
1316
+ /** Destroys this GPU data instance. */
1317
+ destroy() {
1318
+ this.gpuTexture.destroy(), this.textureView = null, this.gpuTexture = null;
1319
+ }
1320
+ }
1321
+ class te {
1322
+ constructor(e) {
1323
+ this._gpuSamplers = /* @__PURE__ */ Object.create(null), this._bindGroupHash = /* @__PURE__ */ Object.create(null), this._renderer = e, e.gc.addCollection(this, "_bindGroupHash", "hash"), this._managedTextures = new O({
1324
+ renderer: e,
1325
+ type: "resource",
1326
+ onUnload: this.onSourceUnload.bind(this),
1327
+ name: "gpuTextureSource"
1328
+ });
1329
+ const t = {
1330
+ image: ee,
1331
+ buffer: Le,
1332
+ video: Fe,
1333
+ compressed: De
1334
+ };
1335
+ this._uploads = {
1336
+ ...t,
1337
+ cube: ke(t)
1338
+ };
1339
+ }
1340
+ /**
1341
+ * @deprecated since 8.15.0
1342
+ */
1343
+ get managedTextures() {
1344
+ return Object.values(this._managedTextures.items);
1345
+ }
1346
+ contextChange(e) {
1347
+ this._gpu = e;
1348
+ }
1349
+ /**
1350
+ * Initializes a texture source, if it has already been initialized nothing will happen.
1351
+ * @param source - The texture source to initialize.
1352
+ * @returns The initialized texture source.
1353
+ */
1354
+ initSource(e) {
1355
+ var t;
1356
+ return ((t = e._gpuData[this._renderer.uid]) == null ? void 0 : t.gpuTexture) || this._initSource(e);
1357
+ }
1358
+ _initSource(e) {
1359
+ if (e.autoGenerateMipmaps) {
1360
+ const c = Math.max(e.pixelWidth, e.pixelHeight);
1361
+ e.mipLevelCount = Math.floor(Math.log2(c)) + 1;
1362
+ }
1363
+ let t = GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST;
1364
+ e.uploadMethodId !== "compressed" && (t |= GPUTextureUsage.RENDER_ATTACHMENT, t |= GPUTextureUsage.COPY_SRC);
1365
+ const r = Z[e.format] || { blockWidth: 1, blockHeight: 1 }, s = Math.ceil(e.pixelWidth / r.blockWidth) * r.blockWidth, i = Math.ceil(e.pixelHeight / r.blockHeight) * r.blockHeight, n = {
1366
+ label: e.label,
1367
+ size: { width: s, height: i, depthOrArrayLayers: e.arrayLayerCount },
1368
+ format: e.format,
1369
+ sampleCount: e.sampleCount,
1370
+ mipLevelCount: e.mipLevelCount,
1371
+ dimension: e.dimension,
1372
+ usage: t
1373
+ }, o = this._gpu.device.createTexture(n);
1374
+ return e._gpuData[this._renderer.uid] = new Ie(o), this._managedTextures.add(e) && (e.on("update", this.onSourceUpdate, this), e.on("resize", this.onSourceResize, this), e.on("updateMipmaps", this.onUpdateMipmaps, this)), this.onSourceUpdate(e), o;
1375
+ }
1376
+ onSourceUpdate(e) {
1377
+ const t = this.getGpuSource(e);
1378
+ t && (this._uploads[e.uploadMethodId] && this._uploads[e.uploadMethodId].upload(e, t, this._gpu), e.autoGenerateMipmaps && e.mipLevelCount > 1 && this.onUpdateMipmaps(e));
1379
+ }
1380
+ onUpdateMipmaps(e) {
1381
+ this._mipmapGenerator || (this._mipmapGenerator = new Oe(this._gpu.device));
1382
+ const t = this.getGpuSource(e);
1383
+ this._mipmapGenerator.generateMipmap(t);
1384
+ }
1385
+ onSourceUnload(e) {
1386
+ e.off("update", this.onSourceUpdate, this), e.off("resize", this.onSourceResize, this), e.off("updateMipmaps", this.onUpdateMipmaps, this);
1387
+ }
1388
+ onSourceResize(e) {
1389
+ e._gcLastUsed = this._renderer.gc.now;
1390
+ const t = e._gpuData[this._renderer.uid], r = t == null ? void 0 : t.gpuTexture;
1391
+ r ? (r.width !== e.pixelWidth || r.height !== e.pixelHeight) && (t.destroy(), this._bindGroupHash[e.uid] = null, e._gpuData[this._renderer.uid] = null, this.initSource(e)) : this.initSource(e);
1392
+ }
1393
+ _initSampler(e) {
1394
+ return this._gpuSamplers[e._resourceId] = this._gpu.device.createSampler(e), this._gpuSamplers[e._resourceId];
1395
+ }
1396
+ getGpuSampler(e) {
1397
+ return this._gpuSamplers[e._resourceId] || this._initSampler(e);
1398
+ }
1399
+ getGpuSource(e) {
1400
+ var t;
1401
+ return e._gcLastUsed = this._renderer.gc.now, ((t = e._gpuData[this._renderer.uid]) == null ? void 0 : t.gpuTexture) || this.initSource(e);
1402
+ }
1403
+ /**
1404
+ * this returns s bind group for a specific texture, the bind group contains
1405
+ * - the texture source
1406
+ * - the texture style
1407
+ * - the texture matrix
1408
+ * This is cached so the bind group should only be created once per texture
1409
+ * @param texture - the texture you want the bindgroup for
1410
+ * @returns the bind group for the texture
1411
+ */
1412
+ getTextureBindGroup(e) {
1413
+ return this._bindGroupHash[e.uid] || this._createTextureBindGroup(e);
1414
+ }
1415
+ _createTextureBindGroup(e) {
1416
+ const t = e.source;
1417
+ return this._bindGroupHash[e.uid] = new L({
1418
+ 0: t,
1419
+ 1: t.style,
1420
+ 2: new A({
1421
+ uTextureMatrix: { type: "mat3x3<f32>", value: e.textureMatrix.mapCoord }
1422
+ })
1423
+ }), this._bindGroupHash[e.uid];
1424
+ }
1425
+ getTextureView(e) {
1426
+ const t = e.source;
1427
+ t._gcLastUsed = this._renderer.gc.now;
1428
+ let r = t._gpuData[this._renderer.uid];
1429
+ return r || (this.initSource(t), r = t._gpuData[this._renderer.uid]), r.textureView || (r.textureView = r.gpuTexture.createView({ dimension: t.viewDimension })), r.textureView;
1430
+ }
1431
+ generateCanvas(e) {
1432
+ const t = this._renderer, r = t.gpu.device.createCommandEncoder(), s = P.get().createCanvas();
1433
+ s.width = e.source.pixelWidth, s.height = e.source.pixelHeight;
1434
+ const i = s.getContext("webgpu");
1435
+ return i.configure({
1436
+ device: t.gpu.device,
1437
+ usage: GPUTextureUsage.COPY_DST | GPUTextureUsage.COPY_SRC,
1438
+ format: P.get().getNavigator().gpu.getPreferredCanvasFormat(),
1439
+ alphaMode: "premultiplied"
1440
+ }), r.copyTextureToTexture({
1441
+ texture: t.texture.getGpuSource(e.source),
1442
+ origin: {
1443
+ x: 0,
1444
+ y: 0
1445
+ }
1446
+ }, {
1447
+ texture: i.getCurrentTexture()
1448
+ }, {
1449
+ width: s.width,
1450
+ height: s.height
1451
+ }), t.gpu.device.queue.submit([r.finish()]), s;
1452
+ }
1453
+ getPixels(e) {
1454
+ const t = this.generateCanvas(e), r = M.getOptimalCanvasAndContext(t.width, t.height), s = r.context;
1455
+ s.drawImage(t, 0, 0);
1456
+ const { width: i, height: n } = t, o = s.getImageData(0, 0, i, n), u = new Uint8ClampedArray(o.data.buffer);
1457
+ return M.returnCanvasAndContext(r), { pixels: u, width: i, height: n };
1458
+ }
1459
+ destroy() {
1460
+ this._managedTextures.destroy();
1461
+ for (const e of Object.keys(this._bindGroupHash)) {
1462
+ const t = Number(e), r = this._bindGroupHash[t];
1463
+ r == null || r.destroy();
1464
+ }
1465
+ this._renderer = null, this._gpu = null, this._mipmapGenerator = null, this._gpuSamplers = null, this._bindGroupHash = null;
1466
+ }
1467
+ }
1468
+ te.extension = {
1469
+ type: [
1470
+ l.WebGPUSystem
1471
+ ],
1472
+ name: "texture"
1473
+ };
1474
+ class re {
1475
+ constructor() {
1476
+ this._maxTextures = 0;
1477
+ }
1478
+ contextChange(e) {
1479
+ const t = new A({
1480
+ uTransformMatrix: { value: new D(), type: "mat3x3<f32>" },
1481
+ uColor: { value: new Float32Array([1, 1, 1, 1]), type: "vec4<f32>" },
1482
+ uRound: { value: 0, type: "f32" }
1483
+ });
1484
+ this._maxTextures = e.limits.maxBatchableTextures;
1485
+ const r = I({
1486
+ name: "graphics",
1487
+ bits: [
1488
+ _e,
1489
+ be(this._maxTextures),
1490
+ Be,
1491
+ H
1492
+ ]
1493
+ });
1494
+ this.shader = new k({
1495
+ gpuProgram: r,
1496
+ resources: {
1497
+ // added on the fly!
1498
+ localUniforms: t
1499
+ }
1500
+ });
1501
+ }
1502
+ execute(e, t) {
1503
+ const r = t.context, s = r.customShader || this.shader, i = e.renderer, n = i.graphicsContext, {
1504
+ batcher: o,
1505
+ instructions: u
1506
+ } = n.getContextRenderData(r), c = i.encoder;
1507
+ c.setGeometry(o.geometry, s.gpuProgram);
1508
+ const f = i.globalUniforms.bindGroup;
1509
+ c.setBindGroup(0, f, s.gpuProgram);
1510
+ const h = i.renderPipes.uniformBatch.getUniformBindGroup(s.resources.localUniforms, !0);
1511
+ c.setBindGroup(2, h, s.gpuProgram);
1512
+ const p = u.instructions;
1513
+ let d = null;
1514
+ for (let g = 0; g < u.instructionSize; g++) {
1515
+ const m = p[g];
1516
+ if (m.topology !== d && (d = m.topology, c.setPipelineFromGeometryProgramAndState(
1517
+ o.geometry,
1518
+ s.gpuProgram,
1519
+ e.state,
1520
+ m.topology
1521
+ )), s.groups[1] = m.bindGroup, !m.gpuBindGroup) {
1522
+ const x = m.textures;
1523
+ m.bindGroup = F(
1524
+ x.textures,
1525
+ x.count,
1526
+ this._maxTextures
1527
+ ), m.gpuBindGroup = i.bindGroup.getBindGroup(
1528
+ m.bindGroup,
1529
+ s.gpuProgram,
1530
+ 1
1531
+ );
1532
+ }
1533
+ c.setBindGroup(1, m.bindGroup, s.gpuProgram), c.renderPassEncoder.drawIndexed(m.size, 1, m.start);
1534
+ }
1535
+ }
1536
+ destroy() {
1537
+ this.shader.destroy(!0), this.shader = null;
1538
+ }
1539
+ }
1540
+ re.extension = {
1541
+ type: [
1542
+ l.WebGPUPipesAdaptor
1543
+ ],
1544
+ name: "graphics"
1545
+ };
1546
+ class se {
1547
+ init() {
1548
+ const e = I({
1549
+ name: "mesh",
1550
+ bits: [
1551
+ Se,
1552
+ Te,
1553
+ H
1554
+ ]
1555
+ });
1556
+ this._shader = new k({
1557
+ gpuProgram: e,
1558
+ resources: {
1559
+ uTexture: w.EMPTY._source,
1560
+ uSampler: w.EMPTY._source.style,
1561
+ textureUniforms: {
1562
+ uTextureMatrix: { type: "mat3x3<f32>", value: new D() }
1563
+ }
1564
+ }
1565
+ });
1566
+ }
1567
+ execute(e, t) {
1568
+ const r = e.renderer;
1569
+ let s = t._shader;
1570
+ if (!s)
1571
+ s = this._shader, s.groups[2] = r.texture.getTextureBindGroup(t.texture);
1572
+ else if (!s.gpuProgram) {
1573
+ C("Mesh shader has no gpuProgram", t.shader);
1574
+ return;
1575
+ }
1576
+ const i = s.gpuProgram;
1577
+ if (i.autoAssignGlobalUniforms && (s.groups[0] = r.globalUniforms.bindGroup), i.autoAssignLocalUniforms) {
1578
+ const n = e.localUniforms;
1579
+ s.groups[1] = r.renderPipes.uniformBatch.getUniformBindGroup(n, !0);
1580
+ }
1581
+ r.encoder.draw({
1582
+ geometry: t._geometry,
1583
+ shader: s,
1584
+ state: t.state
1585
+ });
1586
+ }
1587
+ destroy() {
1588
+ this._shader.destroy(!0), this._shader = null;
1589
+ }
1590
+ }
1591
+ se.extension = {
1592
+ type: [
1593
+ l.WebGPUPipesAdaptor
1594
+ ],
1595
+ name: "mesh"
1596
+ };
1597
+ const He = [
1598
+ ...ye,
1599
+ fe,
1600
+ j,
1601
+ v,
1602
+ K,
1603
+ N,
1604
+ te,
1605
+ $,
1606
+ J,
1607
+ Q,
1608
+ X,
1609
+ z,
1610
+ q,
1611
+ V
1612
+ ], We = [...xe, Y], Ve = [W, se, re], ie = [], ne = [], oe = [];
1613
+ B.handleByNamedList(l.WebGPUSystem, ie);
1614
+ B.handleByNamedList(l.WebGPUPipes, ne);
1615
+ B.handleByNamedList(l.WebGPUPipesAdaptor, oe);
1616
+ B.add(...He, ...We, ...Ve);
1617
+ class $e extends pe {
1618
+ constructor() {
1619
+ const e = {
1620
+ name: "webgpu",
1621
+ type: le.WEBGPU,
1622
+ systems: ie,
1623
+ renderPipes: ne,
1624
+ renderPipeAdaptors: oe
1625
+ };
1626
+ super(e);
1627
+ }
1628
+ }
1629
+ export {
1630
+ $e as WebGPURenderer
1631
+ };
1632
+ //# sourceMappingURL=WebGPURenderer-xmqgPtcF.js.map