@micjanic/recursive-grid 1.0.2

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