evotars 0.0.11 → 0.0.12

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