@micjanic/recursive-grid 1.0.4 → 1.0.6

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.
@@ -1,1657 +0,0 @@
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-BHmY2ZMg.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-D2AiAlOX.js";
4
- import { C as R } from "./CanvasPool-D2Uot4VE.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(parseInt(s, 10), 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
- var e;
604
- for (let t = 0; t < this._bindGroups.length; t++)
605
- (e = this._bindGroups[t]) == null || e.destroy();
606
- this._bindGroups = null, this._bindGroupHash = null;
607
- for (let t = 0; t < this._buffers.length; t++)
608
- this._buffers[t].destroy();
609
- this._buffers = null;
610
- for (let t = 0; t < this._bufferResources.length; t++)
611
- this._bufferResources[t].destroy();
612
- this._bufferResources = null, this._batchBuffer.destroy(), this._bindGroupHash = null, this._renderer = null;
613
- }
614
- }
615
- Y.extension = {
616
- type: [
617
- p.WebGPUPipes
618
- ],
619
- name: "uniformBatch"
620
- };
621
- const Re = {
622
- "point-list": 0,
623
- "line-list": 1,
624
- "line-strip": 2,
625
- "triangle-list": 3,
626
- "triangle-strip": 4
627
- };
628
- function Ee(a, e, t, r, s) {
629
- return a << 24 | e << 16 | t << 10 | r << 5 | s;
630
- }
631
- function Le(a, e, t, r) {
632
- return t << 6 | a << 3 | r << 1 | e;
633
- }
634
- class $ {
635
- constructor(e) {
636
- 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;
637
- }
638
- contextChange(e) {
639
- this._gpu = e, this.setStencilMode(C.DISABLED), this._updatePipeHash();
640
- }
641
- setMultisampleCount(e) {
642
- this._multisampleCount !== e && (this._multisampleCount = e, this._updatePipeHash());
643
- }
644
- setRenderTarget(e) {
645
- this._multisampleCount = e.msaaSamples, this._depthStencilAttachment = e.descriptor.depthStencilAttachment ? 1 : 0, this._updatePipeHash();
646
- }
647
- setColorMask(e) {
648
- this._colorMask !== e && (this._colorMask = e, this._updatePipeHash());
649
- }
650
- setStencilMode(e) {
651
- this._stencilMode !== e && (this._stencilMode = e, this._stencilState = Ge[e], this._updatePipeHash());
652
- }
653
- setPipeline(e, t, r, s) {
654
- const i = this.getPipeline(e, t, r);
655
- s.setPipeline(i);
656
- }
657
- getPipeline(e, t, r, s) {
658
- e._layoutKey || (Be(e, t.attributeData), this._generateBufferKey(e)), s || (s = e.topology);
659
- const i = Ee(
660
- e._layoutKey,
661
- t._layoutKey,
662
- r.data,
663
- r._blendModeId,
664
- Re[s]
665
- );
666
- return this._pipeCache[i] ? this._pipeCache[i] : (this._pipeCache[i] = this._createPipeline(e, t, r, s), this._pipeCache[i]);
667
- }
668
- _createPipeline(e, t, r, s) {
669
- const i = this._gpu.device, n = this._createVertexBufferLayouts(e, t), o = this._renderer.state.getColorTargets(r);
670
- o[0].writeMask = this._stencilMode === C.RENDERING_MASK_ADD ? 0 : this._colorMask;
671
- const u = this._renderer.shader.getProgramData(t).pipeline, c = {
672
- // TODO later check if its helpful to create..
673
- // layout,
674
- vertex: {
675
- module: this._getModule(t.vertex.source),
676
- entryPoint: t.vertex.entryPoint,
677
- // geometry..
678
- buffers: n
679
- },
680
- fragment: {
681
- module: this._getModule(t.fragment.source),
682
- entryPoint: t.fragment.entryPoint,
683
- targets: o
684
- },
685
- primitive: {
686
- topology: s,
687
- cullMode: r.cullMode
688
- },
689
- layout: u,
690
- multisample: {
691
- count: this._multisampleCount
692
- },
693
- // depthStencil,
694
- label: "PIXI Pipeline"
695
- };
696
- return this._depthStencilAttachment && (c.depthStencil = {
697
- ...this._stencilState,
698
- format: "depth24plus-stencil8",
699
- depthWriteEnabled: r.depthTest,
700
- depthCompare: r.depthTest ? "less" : "always"
701
- }), i.createRenderPipeline(c);
702
- }
703
- _getModule(e) {
704
- return this._moduleCache[e] || this._createModule(e);
705
- }
706
- _createModule(e) {
707
- const t = this._gpu.device;
708
- return this._moduleCache[e] = t.createShaderModule({
709
- code: e
710
- }), this._moduleCache[e];
711
- }
712
- _generateBufferKey(e) {
713
- const t = [];
714
- let r = 0;
715
- const s = Object.keys(e.attributes).sort();
716
- for (let n = 0; n < s.length; n++) {
717
- const o = e.attributes[s[n]];
718
- t[r++] = o.offset, t[r++] = o.format, t[r++] = o.stride, t[r++] = o.instance;
719
- }
720
- const i = t.join("|");
721
- return e._layoutKey = M(i, "geometry"), e._layoutKey;
722
- }
723
- _generateAttributeLocationsKey(e) {
724
- const t = [];
725
- let r = 0;
726
- const s = Object.keys(e.attributeData).sort();
727
- for (let n = 0; n < s.length; n++) {
728
- const o = e.attributeData[s[n]];
729
- t[r++] = o.location;
730
- }
731
- const i = t.join("|");
732
- return e._attributeLocationsKey = M(i, "programAttributes"), e._attributeLocationsKey;
733
- }
734
- /**
735
- * Returns a hash of buffer names mapped to bind locations.
736
- * This is used to bind the correct buffer to the correct location in the shader.
737
- * @param geometry - The geometry where to get the buffer names
738
- * @param program - The program where to get the buffer names
739
- * @returns An object of buffer names mapped to the bind location.
740
- */
741
- getBufferNamesToBind(e, t) {
742
- const r = e._layoutKey << 16 | t._attributeLocationsKey;
743
- if (this._bindingNamesCache[r])
744
- return this._bindingNamesCache[r];
745
- const s = this._createVertexBufferLayouts(e, t), i = /* @__PURE__ */ Object.create(null), n = t.attributeData;
746
- for (let o = 0; o < s.length; o++) {
747
- const c = Object.values(s[o].attributes)[0].shaderLocation;
748
- for (const l in n)
749
- if (n[l].location === c) {
750
- i[o] = l;
751
- break;
752
- }
753
- }
754
- return this._bindingNamesCache[r] = i, i;
755
- }
756
- _createVertexBufferLayouts(e, t) {
757
- t._attributeLocationsKey || this._generateAttributeLocationsKey(t);
758
- const r = e._layoutKey << 16 | t._attributeLocationsKey;
759
- if (this._bufferLayoutsCache[r])
760
- return this._bufferLayoutsCache[r];
761
- const s = [];
762
- return e.buffers.forEach((i) => {
763
- const n = {
764
- arrayStride: 0,
765
- stepMode: "vertex",
766
- attributes: []
767
- }, o = n.attributes;
768
- for (const u in t.attributeData) {
769
- const c = e.attributes[u];
770
- (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({
771
- shaderLocation: t.attributeData[u].location,
772
- offset: c.offset,
773
- format: c.format
774
- }));
775
- }
776
- o.length && s.push(n);
777
- }), this._bufferLayoutsCache[r] = s, s;
778
- }
779
- _updatePipeHash() {
780
- const e = Le(
781
- this._stencilMode,
782
- this._multisampleCount,
783
- this._colorMask,
784
- this._depthStencilAttachment
785
- );
786
- this._pipeStateCaches[e] || (this._pipeStateCaches[e] = /* @__PURE__ */ Object.create(null)), this._pipeCache = this._pipeStateCaches[e];
787
- }
788
- destroy() {
789
- this._renderer = null, this._bufferLayoutsCache = null;
790
- }
791
- }
792
- $.extension = {
793
- type: [p.WebGPUSystem],
794
- name: "pipeline"
795
- };
796
- class Ae {
797
- constructor() {
798
- this.contexts = [], this.msaaTextures = [], this.msaaSamples = 1;
799
- }
800
- }
801
- class ke {
802
- init(e, t) {
803
- this._renderer = e, this._renderTargetSystem = t;
804
- }
805
- copyToTexture(e, t, r, s, i) {
806
- const n = this._renderer, o = this._getGpuColorTexture(
807
- e
808
- ), u = n.texture.getGpuSource(
809
- t.source
810
- );
811
- return n.encoder.commandEncoder.copyTextureToTexture(
812
- {
813
- texture: o,
814
- origin: r
815
- },
816
- {
817
- texture: u,
818
- origin: i
819
- },
820
- s
821
- ), t;
822
- }
823
- startRenderPass(e, t = !0, r, s) {
824
- const n = this._renderTargetSystem.getGpuRenderTarget(e), o = this.getDescriptor(e, t, r);
825
- n.descriptor = o, this._renderer.pipeline.setRenderTarget(n), this._renderer.encoder.beginRenderPass(n), this._renderer.encoder.setViewport(s);
826
- }
827
- finishRenderPass() {
828
- this._renderer.encoder.endRenderPass();
829
- }
830
- /**
831
- * returns the gpu texture for the first color texture in the render target
832
- * mainly used by the filter manager to get copy the texture for blending
833
- * @param renderTarget
834
- * @returns a gpu texture
835
- */
836
- _getGpuColorTexture(e) {
837
- const t = this._renderTargetSystem.getGpuRenderTarget(e);
838
- return t.contexts[0] ? t.contexts[0].getCurrentTexture() : this._renderer.texture.getGpuSource(
839
- e.colorTextures[0].source
840
- );
841
- }
842
- getDescriptor(e, t, r) {
843
- typeof t == "boolean" && (t = t ? y.ALL : y.NONE);
844
- const s = this._renderTargetSystem, i = s.getGpuRenderTarget(e), n = e.colorTextures.map(
845
- (c, l) => {
846
- const h = i.contexts[l];
847
- let f, d;
848
- h ? f = h.getCurrentTexture().createView() : f = this._renderer.texture.getGpuSource(c).createView({
849
- mipLevelCount: 1
850
- }), i.msaaTextures[l] && (d = f, f = this._renderer.texture.getTextureView(
851
- i.msaaTextures[l]
852
- ));
853
- const _ = t & y.COLOR ? "clear" : "load";
854
- return r ?? (r = s.defaultClearColor), {
855
- view: f,
856
- resolveTarget: d,
857
- clearValue: r,
858
- storeOp: "store",
859
- loadOp: _
860
- };
861
- }
862
- );
863
- let o;
864
- if ((e.stencil || e.depth) && !e.depthStencilTexture && (e.ensureDepthStencilTexture(), e.depthStencilTexture.source.sampleCount = i.msaa ? 4 : 1), e.depthStencilTexture) {
865
- const c = t & y.STENCIL ? "clear" : "load", l = t & y.DEPTH ? "clear" : "load";
866
- o = {
867
- view: this._renderer.texture.getGpuSource(e.depthStencilTexture.source).createView(),
868
- stencilStoreOp: "store",
869
- stencilLoadOp: c,
870
- depthClearValue: 1,
871
- depthLoadOp: l,
872
- depthStoreOp: "store"
873
- };
874
- }
875
- return {
876
- colorAttachments: n,
877
- depthStencilAttachment: o
878
- };
879
- }
880
- clear(e, t = !0, r, s) {
881
- if (!t)
882
- return;
883
- const { gpu: i, encoder: n } = this._renderer, o = i.device;
884
- if (n.commandEncoder === null) {
885
- const c = o.createCommandEncoder(), l = this.getDescriptor(e, t, r), h = c.beginRenderPass(l);
886
- h.setViewport(s.x, s.y, s.width, s.height, 0, 1), h.end();
887
- const f = c.finish();
888
- o.queue.submit([f]);
889
- } else
890
- this.startRenderPass(e, t, r, s);
891
- }
892
- initGpuRenderTarget(e) {
893
- e.isRoot = !0;
894
- const t = new Ae();
895
- return e.colorTextures.forEach((r, s) => {
896
- if (r instanceof ce) {
897
- const i = r.resource.getContext(
898
- "webgpu"
899
- ), n = r.transparent ? "premultiplied" : "opaque";
900
- try {
901
- i.configure({
902
- device: this._renderer.gpu.device,
903
- usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST | GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_SRC,
904
- format: "bgra8unorm",
905
- alphaMode: n
906
- });
907
- } catch (o) {
908
- console.error(o);
909
- }
910
- t.contexts[s] = i;
911
- }
912
- if (t.msaa = r.source.antialias, r.source.antialias) {
913
- const i = new de({
914
- width: 0,
915
- height: 0,
916
- sampleCount: 4
917
- });
918
- t.msaaTextures[s] = i;
919
- }
920
- }), t.msaa && (t.msaaSamples = 4, e.depthStencilTexture && (e.depthStencilTexture.source.sampleCount = 4)), t;
921
- }
922
- destroyGpuRenderTarget(e) {
923
- e.contexts.forEach((t) => {
924
- t.unconfigure();
925
- }), e.msaaTextures.forEach((t) => {
926
- t.destroy();
927
- }), e.msaaTextures.length = 0, e.contexts.length = 0;
928
- }
929
- ensureDepthStencilTexture(e) {
930
- const t = this._renderTargetSystem.getGpuRenderTarget(e);
931
- e.depthStencilTexture && t.msaa && (e.depthStencilTexture.source.sampleCount = 4);
932
- }
933
- resizeGpuRenderTarget(e) {
934
- const t = this._renderTargetSystem.getGpuRenderTarget(e);
935
- t.width = e.width, t.height = e.height, t.msaa && e.colorTextures.forEach((r, s) => {
936
- const i = t.msaaTextures[s];
937
- i == null || i.resize(
938
- r.source.width,
939
- r.source.height,
940
- r.source._resolution
941
- );
942
- });
943
- }
944
- }
945
- class X extends Se {
946
- constructor(e) {
947
- super(e), this.adaptor = new ke(), this.adaptor.init(e, this);
948
- }
949
- }
950
- X.extension = {
951
- type: [p.WebGPUSystem],
952
- name: "renderTarget"
953
- };
954
- class Z {
955
- constructor() {
956
- this._gpuProgramData = /* @__PURE__ */ Object.create(null);
957
- }
958
- contextChange(e) {
959
- this._gpu = e;
960
- }
961
- getProgramData(e) {
962
- return this._gpuProgramData[e._layoutKey] || this._createGPUProgramData(e);
963
- }
964
- _createGPUProgramData(e) {
965
- const t = this._gpu.device, r = e.gpuLayout.map((i) => t.createBindGroupLayout({ entries: i })), s = { bindGroupLayouts: r };
966
- return this._gpuProgramData[e._layoutKey] = {
967
- bindGroups: r,
968
- pipeline: t.createPipelineLayout(s)
969
- }, this._gpuProgramData[e._layoutKey];
970
- }
971
- destroy() {
972
- this._gpu = null, this._gpuProgramData = null;
973
- }
974
- }
975
- Z.extension = {
976
- type: [
977
- p.WebGPUSystem
978
- ],
979
- name: "shader"
980
- };
981
- const g = {};
982
- g.normal = {
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-alpha",
991
- operation: "add"
992
- }
993
- };
994
- g.add = {
995
- alpha: {
996
- srcFactor: "src-alpha",
997
- dstFactor: "one-minus-src-alpha",
998
- operation: "add"
999
- },
1000
- color: {
1001
- srcFactor: "one",
1002
- dstFactor: "one",
1003
- operation: "add"
1004
- }
1005
- };
1006
- g.multiply = {
1007
- alpha: {
1008
- srcFactor: "one",
1009
- dstFactor: "one-minus-src-alpha",
1010
- operation: "add"
1011
- },
1012
- color: {
1013
- srcFactor: "dst",
1014
- dstFactor: "one-minus-src-alpha",
1015
- operation: "add"
1016
- }
1017
- };
1018
- g.screen = {
1019
- alpha: {
1020
- srcFactor: "one",
1021
- dstFactor: "one-minus-src-alpha",
1022
- operation: "add"
1023
- },
1024
- color: {
1025
- srcFactor: "one",
1026
- dstFactor: "one-minus-src",
1027
- operation: "add"
1028
- }
1029
- };
1030
- g.overlay = {
1031
- alpha: {
1032
- srcFactor: "one",
1033
- dstFactor: "one-minus-src-alpha",
1034
- operation: "add"
1035
- },
1036
- color: {
1037
- srcFactor: "one",
1038
- dstFactor: "one-minus-src",
1039
- operation: "add"
1040
- }
1041
- };
1042
- g.none = {
1043
- alpha: {
1044
- srcFactor: "one",
1045
- dstFactor: "one-minus-src-alpha",
1046
- operation: "add"
1047
- },
1048
- color: {
1049
- srcFactor: "zero",
1050
- dstFactor: "zero",
1051
- operation: "add"
1052
- }
1053
- };
1054
- g["normal-npm"] = {
1055
- alpha: {
1056
- srcFactor: "one",
1057
- dstFactor: "one-minus-src-alpha",
1058
- operation: "add"
1059
- },
1060
- color: {
1061
- srcFactor: "src-alpha",
1062
- dstFactor: "one-minus-src-alpha",
1063
- operation: "add"
1064
- }
1065
- };
1066
- g["add-npm"] = {
1067
- alpha: {
1068
- srcFactor: "one",
1069
- dstFactor: "one",
1070
- operation: "add"
1071
- },
1072
- color: {
1073
- srcFactor: "src-alpha",
1074
- dstFactor: "one",
1075
- operation: "add"
1076
- }
1077
- };
1078
- g["screen-npm"] = {
1079
- alpha: {
1080
- srcFactor: "one",
1081
- dstFactor: "one-minus-src-alpha",
1082
- operation: "add"
1083
- },
1084
- color: {
1085
- srcFactor: "src-alpha",
1086
- dstFactor: "one-minus-src",
1087
- operation: "add"
1088
- }
1089
- };
1090
- g.erase = {
1091
- alpha: {
1092
- srcFactor: "zero",
1093
- dstFactor: "one-minus-src-alpha",
1094
- operation: "add"
1095
- },
1096
- color: {
1097
- srcFactor: "zero",
1098
- dstFactor: "one-minus-src",
1099
- operation: "add"
1100
- }
1101
- };
1102
- g.min = {
1103
- alpha: {
1104
- srcFactor: "one",
1105
- dstFactor: "one",
1106
- operation: "min"
1107
- },
1108
- color: {
1109
- srcFactor: "one",
1110
- dstFactor: "one",
1111
- operation: "min"
1112
- }
1113
- };
1114
- g.max = {
1115
- alpha: {
1116
- srcFactor: "one",
1117
- dstFactor: "one",
1118
- operation: "max"
1119
- },
1120
- color: {
1121
- srcFactor: "one",
1122
- dstFactor: "one",
1123
- operation: "max"
1124
- }
1125
- };
1126
- class J {
1127
- constructor() {
1128
- this.defaultState = new E(), this.defaultState.blend = !0;
1129
- }
1130
- contextChange(e) {
1131
- this.gpu = e;
1132
- }
1133
- /**
1134
- * Gets the blend mode data for the current state
1135
- * @param state - The state to get the blend mode from
1136
- */
1137
- getColorTargets(e) {
1138
- return [
1139
- {
1140
- format: "bgra8unorm",
1141
- writeMask: 0,
1142
- blend: g[e.blendMode] || g.normal
1143
- }
1144
- ];
1145
- }
1146
- destroy() {
1147
- this.gpu = null;
1148
- }
1149
- }
1150
- J.extension = {
1151
- type: [
1152
- p.WebGPUSystem
1153
- ],
1154
- name: "state"
1155
- };
1156
- const De = {
1157
- type: "image",
1158
- upload(a, e, t) {
1159
- const r = a.resource, s = (a.pixelWidth | 0) * (a.pixelHeight | 0), i = r.byteLength / s;
1160
- t.device.queue.writeTexture(
1161
- { texture: e },
1162
- r,
1163
- {
1164
- offset: 0,
1165
- rowsPerImage: a.pixelHeight,
1166
- bytesPerRow: a.pixelHeight * i
1167
- },
1168
- {
1169
- width: a.pixelWidth,
1170
- height: a.pixelHeight,
1171
- depthOrArrayLayers: 1
1172
- }
1173
- );
1174
- }
1175
- }, Q = {
1176
- "bc1-rgba-unorm": { blockBytes: 8, blockWidth: 4, blockHeight: 4 },
1177
- "bc2-rgba-unorm": { blockBytes: 16, blockWidth: 4, blockHeight: 4 },
1178
- "bc3-rgba-unorm": { blockBytes: 16, blockWidth: 4, blockHeight: 4 },
1179
- "bc7-rgba-unorm": { blockBytes: 16, blockWidth: 4, blockHeight: 4 },
1180
- "etc1-rgb-unorm": { blockBytes: 8, blockWidth: 4, blockHeight: 4 },
1181
- "etc2-rgba8unorm": { blockBytes: 16, blockWidth: 4, blockHeight: 4 },
1182
- "astc-4x4-unorm": { blockBytes: 16, blockWidth: 4, blockHeight: 4 }
1183
- }, He = { blockBytes: 4, blockWidth: 1, blockHeight: 1 }, ze = {
1184
- type: "compressed",
1185
- upload(a, e, t) {
1186
- let r = a.pixelWidth, s = a.pixelHeight;
1187
- const i = Q[a.format] || He;
1188
- for (let n = 0; n < a.resource.length; n++) {
1189
- const o = a.resource[n], u = Math.ceil(r / i.blockWidth) * i.blockBytes;
1190
- t.device.queue.writeTexture(
1191
- {
1192
- texture: e,
1193
- mipLevel: n
1194
- },
1195
- o,
1196
- {
1197
- offset: 0,
1198
- bytesPerRow: u
1199
- },
1200
- {
1201
- width: Math.ceil(r / i.blockWidth) * i.blockWidth,
1202
- height: Math.ceil(s / i.blockHeight) * i.blockHeight,
1203
- depthOrArrayLayers: 1
1204
- }
1205
- ), r = Math.max(r >> 1, 1), s = Math.max(s >> 1, 1);
1206
- }
1207
- }
1208
- }, ee = {
1209
- type: "image",
1210
- upload(a, e, t) {
1211
- const r = a.resource;
1212
- if (!r)
1213
- return;
1214
- if (globalThis.HTMLImageElement && r instanceof HTMLImageElement) {
1215
- const o = B.get().createCanvas(r.width, r.height);
1216
- 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.");
1217
- }
1218
- 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";
1219
- t.device.queue.copyExternalImageToTexture(
1220
- { source: r },
1221
- { texture: e, premultipliedAlpha: n },
1222
- {
1223
- width: s,
1224
- height: i
1225
- }
1226
- );
1227
- }
1228
- }, Fe = {
1229
- type: "video",
1230
- upload(a, e, t) {
1231
- ee.upload(a, e, t);
1232
- }
1233
- };
1234
- class Oe {
1235
- constructor(e) {
1236
- this.device = e, this.sampler = e.createSampler({ minFilter: "linear" }), this.pipelines = {};
1237
- }
1238
- _getMipmapPipeline(e) {
1239
- let t = this.pipelines[e];
1240
- return t || (this.mipmapShaderModule || (this.mipmapShaderModule = this.device.createShaderModule({
1241
- code: (
1242
- /* wgsl */
1243
- `
1244
- var<private> pos : array<vec2<f32>, 3> = array<vec2<f32>, 3>(
1245
- vec2<f32>(-1.0, -1.0), vec2<f32>(-1.0, 3.0), vec2<f32>(3.0, -1.0));
1246
-
1247
- struct VertexOutput {
1248
- @builtin(position) position : vec4<f32>,
1249
- @location(0) texCoord : vec2<f32>,
1250
- };
1251
-
1252
- @vertex
1253
- fn vertexMain(@builtin(vertex_index) vertexIndex : u32) -> VertexOutput {
1254
- var output : VertexOutput;
1255
- output.texCoord = pos[vertexIndex] * vec2<f32>(0.5, -0.5) + vec2<f32>(0.5);
1256
- output.position = vec4<f32>(pos[vertexIndex], 0.0, 1.0);
1257
- return output;
1258
- }
1259
-
1260
- @group(0) @binding(0) var imgSampler : sampler;
1261
- @group(0) @binding(1) var img : texture_2d<f32>;
1262
-
1263
- @fragment
1264
- fn fragmentMain(@location(0) texCoord : vec2<f32>) -> @location(0) vec4<f32> {
1265
- return textureSample(img, imgSampler, texCoord);
1266
- }
1267
- `
1268
- )
1269
- })), t = this.device.createRenderPipeline({
1270
- layout: "auto",
1271
- vertex: {
1272
- module: this.mipmapShaderModule,
1273
- entryPoint: "vertexMain"
1274
- },
1275
- fragment: {
1276
- module: this.mipmapShaderModule,
1277
- entryPoint: "fragmentMain",
1278
- targets: [{ format: e }]
1279
- }
1280
- }), this.pipelines[e] = t), t;
1281
- }
1282
- /**
1283
- * Generates mipmaps for the given GPUTexture from the data in level 0.
1284
- * @param {module:External.GPUTexture} texture - Texture to generate mipmaps for.
1285
- * @returns {module:External.GPUTexture} - The originally passed texture
1286
- */
1287
- generateMipmap(e) {
1288
- const t = this._getMipmapPipeline(e.format);
1289
- if (e.dimension === "3d" || e.dimension === "1d")
1290
- throw new Error("Generating mipmaps for non-2d textures is currently unsupported!");
1291
- let r = e;
1292
- const s = e.depthOrArrayLayers || 1, i = e.usage & GPUTextureUsage.RENDER_ATTACHMENT;
1293
- if (!i) {
1294
- const u = {
1295
- size: {
1296
- width: Math.ceil(e.width / 2),
1297
- height: Math.ceil(e.height / 2),
1298
- depthOrArrayLayers: s
1299
- },
1300
- format: e.format,
1301
- usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_SRC | GPUTextureUsage.RENDER_ATTACHMENT,
1302
- mipLevelCount: e.mipLevelCount - 1
1303
- };
1304
- r = this.device.createTexture(u);
1305
- }
1306
- const n = this.device.createCommandEncoder({}), o = t.getBindGroupLayout(0);
1307
- for (let u = 0; u < s; ++u) {
1308
- let c = e.createView({
1309
- baseMipLevel: 0,
1310
- mipLevelCount: 1,
1311
- dimension: "2d",
1312
- baseArrayLayer: u,
1313
- arrayLayerCount: 1
1314
- }), l = i ? 1 : 0;
1315
- for (let h = 1; h < e.mipLevelCount; ++h) {
1316
- const f = r.createView({
1317
- baseMipLevel: l++,
1318
- mipLevelCount: 1,
1319
- dimension: "2d",
1320
- baseArrayLayer: u,
1321
- arrayLayerCount: 1
1322
- }), d = n.beginRenderPass({
1323
- colorAttachments: [{
1324
- view: f,
1325
- storeOp: "store",
1326
- loadOp: "clear",
1327
- clearValue: { r: 0, g: 0, b: 0, a: 0 }
1328
- }]
1329
- }), _ = this.device.createBindGroup({
1330
- layout: o,
1331
- entries: [{
1332
- binding: 0,
1333
- resource: this.sampler
1334
- }, {
1335
- binding: 1,
1336
- resource: c
1337
- }]
1338
- });
1339
- d.setPipeline(t), d.setBindGroup(0, _), d.draw(3, 1, 0, 0), d.end(), c = f;
1340
- }
1341
- }
1342
- if (!i) {
1343
- const u = {
1344
- width: Math.ceil(e.width / 2),
1345
- height: Math.ceil(e.height / 2),
1346
- depthOrArrayLayers: s
1347
- };
1348
- for (let c = 1; c < e.mipLevelCount; ++c)
1349
- n.copyTextureToTexture({
1350
- texture: r,
1351
- mipLevel: c - 1
1352
- }, {
1353
- texture: e,
1354
- mipLevel: c
1355
- }, u), u.width = Math.ceil(u.width / 2), u.height = Math.ceil(u.height / 2);
1356
- }
1357
- return this.device.queue.submit([n.finish()]), i || r.destroy(), e;
1358
- }
1359
- }
1360
- class te {
1361
- constructor(e) {
1362
- 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 = {
1363
- image: ee,
1364
- buffer: De,
1365
- video: Fe,
1366
- compressed: ze
1367
- }, this._renderer = e, e.renderableGC.addManagedHash(this, "_gpuSources"), e.renderableGC.addManagedHash(this, "_gpuSamplers"), e.renderableGC.addManagedHash(this, "_bindGroupHash"), e.renderableGC.addManagedHash(this, "_textureViewHash");
1368
- }
1369
- contextChange(e) {
1370
- this._gpu = e;
1371
- }
1372
- /**
1373
- * Initializes a texture source, if it has already been initialized nothing will happen.
1374
- * @param source - The texture source to initialize.
1375
- * @returns The initialized texture source.
1376
- */
1377
- initSource(e) {
1378
- return this._gpuSources[e.uid] ? this._gpuSources[e.uid] : this._initSource(e);
1379
- }
1380
- _initSource(e) {
1381
- if (e.autoGenerateMipmaps) {
1382
- const u = Math.max(e.pixelWidth, e.pixelHeight);
1383
- e.mipLevelCount = Math.floor(Math.log2(u)) + 1;
1384
- }
1385
- let t = GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST;
1386
- e.uploadMethodId !== "compressed" && (t |= GPUTextureUsage.RENDER_ATTACHMENT, t |= GPUTextureUsage.COPY_SRC);
1387
- 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 = {
1388
- label: e.label,
1389
- size: { width: s, height: i },
1390
- format: e.format,
1391
- sampleCount: e.sampleCount,
1392
- mipLevelCount: e.mipLevelCount,
1393
- dimension: e.dimension,
1394
- usage: t
1395
- }, o = this._gpuSources[e.uid] = this._gpu.device.createTexture(n);
1396
- 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;
1397
- }
1398
- onSourceUpdate(e) {
1399
- const t = this.getGpuSource(e);
1400
- t && (this._uploads[e.uploadMethodId] && this._uploads[e.uploadMethodId].upload(e, t, this._gpu), e.autoGenerateMipmaps && e.mipLevelCount > 1 && this.onUpdateMipmaps(e));
1401
- }
1402
- onSourceUnload(e) {
1403
- const t = this._gpuSources[e.uid];
1404
- t && (this._gpuSources[e.uid] = null, t.destroy());
1405
- }
1406
- onUpdateMipmaps(e) {
1407
- this._mipmapGenerator || (this._mipmapGenerator = new Oe(this._gpu.device));
1408
- const t = this.getGpuSource(e);
1409
- this._mipmapGenerator.generateMipmap(t);
1410
- }
1411
- onSourceDestroy(e) {
1412
- 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);
1413
- }
1414
- onSourceResize(e) {
1415
- const t = this._gpuSources[e.uid];
1416
- 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);
1417
- }
1418
- _initSampler(e) {
1419
- return this._gpuSamplers[e._resourceId] = this._gpu.device.createSampler(e), this._gpuSamplers[e._resourceId];
1420
- }
1421
- getGpuSampler(e) {
1422
- return this._gpuSamplers[e._resourceId] || this._initSampler(e);
1423
- }
1424
- getGpuSource(e) {
1425
- return this._gpuSources[e.uid] || this.initSource(e);
1426
- }
1427
- /**
1428
- * this returns s bind group for a specific texture, the bind group contains
1429
- * - the texture source
1430
- * - the texture style
1431
- * - the texture matrix
1432
- * This is cached so the bind group should only be created once per texture
1433
- * @param texture - the texture you want the bindgroup for
1434
- * @returns the bind group for the texture
1435
- */
1436
- getTextureBindGroup(e) {
1437
- return this._bindGroupHash[e.uid] ?? this._createTextureBindGroup(e);
1438
- }
1439
- _createTextureBindGroup(e) {
1440
- const t = e.source;
1441
- return this._bindGroupHash[e.uid] = new A({
1442
- 0: t,
1443
- 1: t.style,
1444
- 2: new k({
1445
- uTextureMatrix: { type: "mat3x3<f32>", value: e.textureMatrix.mapCoord }
1446
- })
1447
- }), this._bindGroupHash[e.uid];
1448
- }
1449
- getTextureView(e) {
1450
- const t = e.source;
1451
- return this._textureViewHash[t.uid] ?? this._createTextureView(t);
1452
- }
1453
- _createTextureView(e) {
1454
- return this._textureViewHash[e.uid] = this.getGpuSource(e).createView(), this._textureViewHash[e.uid];
1455
- }
1456
- generateCanvas(e) {
1457
- const t = this._renderer, r = t.gpu.device.createCommandEncoder(), s = B.get().createCanvas();
1458
- s.width = e.source.pixelWidth, s.height = e.source.pixelHeight;
1459
- const i = s.getContext("webgpu");
1460
- return i.configure({
1461
- device: t.gpu.device,
1462
- usage: GPUTextureUsage.COPY_DST | GPUTextureUsage.COPY_SRC,
1463
- format: B.get().getNavigator().gpu.getPreferredCanvasFormat(),
1464
- alphaMode: "premultiplied"
1465
- }), r.copyTextureToTexture({
1466
- texture: t.texture.getGpuSource(e.source),
1467
- origin: {
1468
- x: 0,
1469
- y: 0
1470
- }
1471
- }, {
1472
- texture: i.getCurrentTexture()
1473
- }, {
1474
- width: s.width,
1475
- height: s.height
1476
- }), t.gpu.device.queue.submit([r.finish()]), s;
1477
- }
1478
- getPixels(e) {
1479
- const t = this.generateCanvas(e), r = R.getOptimalCanvasAndContext(t.width, t.height), s = r.context;
1480
- s.drawImage(t, 0, 0);
1481
- const { width: i, height: n } = t, o = s.getImageData(0, 0, i, n), u = new Uint8ClampedArray(o.data.buffer);
1482
- return R.returnCanvasAndContext(r), { pixels: u, width: i, height: n };
1483
- }
1484
- destroy() {
1485
- this.managedTextures.slice().forEach((e) => this.onSourceDestroy(e)), this.managedTextures = null;
1486
- for (const e of Object.keys(this._bindGroupHash)) {
1487
- const t = Number(e), r = this._bindGroupHash[t];
1488
- r == null || r.destroy(), this._bindGroupHash[t] = null;
1489
- }
1490
- this._gpu = null, this._mipmapGenerator = null, this._gpuSources = null, this._bindGroupHash = null, this._textureViewHash = null, this._gpuSamplers = null;
1491
- }
1492
- }
1493
- te.extension = {
1494
- type: [
1495
- p.WebGPUSystem
1496
- ],
1497
- name: "texture"
1498
- };
1499
- class re {
1500
- constructor() {
1501
- this._maxTextures = 0;
1502
- }
1503
- contextChange(e) {
1504
- const t = new k({
1505
- uTransformMatrix: { value: new D(), type: "mat3x3<f32>" },
1506
- uColor: { value: new Float32Array([1, 1, 1, 1]), type: "vec4<f32>" },
1507
- uRound: { value: 0, type: "f32" }
1508
- });
1509
- this._maxTextures = e.limits.maxBatchableTextures;
1510
- const r = H({
1511
- name: "graphics",
1512
- bits: [
1513
- he,
1514
- pe(this._maxTextures),
1515
- ge,
1516
- z
1517
- ]
1518
- });
1519
- this.shader = new F({
1520
- gpuProgram: r,
1521
- resources: {
1522
- // added on the fly!
1523
- localUniforms: t
1524
- }
1525
- });
1526
- }
1527
- execute(e, t) {
1528
- const r = t.context, s = r.customShader || this.shader, i = e.renderer, n = i.graphicsContext, {
1529
- batcher: o,
1530
- instructions: u
1531
- } = n.getContextRenderData(r), c = i.encoder;
1532
- c.setGeometry(o.geometry, s.gpuProgram);
1533
- const l = i.globalUniforms.bindGroup;
1534
- c.setBindGroup(0, l, s.gpuProgram);
1535
- const h = i.renderPipes.uniformBatch.getUniformBindGroup(s.resources.localUniforms, !0);
1536
- c.setBindGroup(2, h, s.gpuProgram);
1537
- const f = u.instructions;
1538
- let d = null;
1539
- for (let _ = 0; _ < u.instructionSize; _++) {
1540
- const m = f[_];
1541
- if (m.topology !== d && (d = m.topology, c.setPipelineFromGeometryProgramAndState(
1542
- o.geometry,
1543
- s.gpuProgram,
1544
- e.state,
1545
- m.topology
1546
- )), s.groups[1] = m.bindGroup, !m.gpuBindGroup) {
1547
- const P = m.textures;
1548
- m.bindGroup = L(
1549
- P.textures,
1550
- P.count,
1551
- this._maxTextures
1552
- ), m.gpuBindGroup = i.bindGroup.getBindGroup(
1553
- m.bindGroup,
1554
- s.gpuProgram,
1555
- 1
1556
- );
1557
- }
1558
- c.setBindGroup(1, m.bindGroup, s.gpuProgram), c.renderPassEncoder.drawIndexed(m.size, 1, m.start);
1559
- }
1560
- }
1561
- destroy() {
1562
- this.shader.destroy(!0), this.shader = null;
1563
- }
1564
- }
1565
- re.extension = {
1566
- type: [
1567
- p.WebGPUPipesAdaptor
1568
- ],
1569
- name: "graphics"
1570
- };
1571
- class se {
1572
- init() {
1573
- const e = H({
1574
- name: "mesh",
1575
- bits: [
1576
- me,
1577
- Pe,
1578
- z
1579
- ]
1580
- });
1581
- this._shader = new F({
1582
- gpuProgram: e,
1583
- resources: {
1584
- uTexture: w.EMPTY._source,
1585
- uSampler: w.EMPTY._source.style,
1586
- textureUniforms: {
1587
- uTextureMatrix: { type: "mat3x3<f32>", value: new D() }
1588
- }
1589
- }
1590
- });
1591
- }
1592
- execute(e, t) {
1593
- const r = e.renderer;
1594
- let s = t._shader;
1595
- if (!s)
1596
- s = this._shader, s.groups[2] = r.texture.getTextureBindGroup(t.texture);
1597
- else if (!s.gpuProgram) {
1598
- v("Mesh shader has no gpuProgram", t.shader);
1599
- return;
1600
- }
1601
- const i = s.gpuProgram;
1602
- if (i.autoAssignGlobalUniforms && (s.groups[0] = r.globalUniforms.bindGroup), i.autoAssignLocalUniforms) {
1603
- const n = e.localUniforms;
1604
- s.groups[1] = r.renderPipes.uniformBatch.getUniformBindGroup(n, !0);
1605
- }
1606
- r.encoder.draw({
1607
- geometry: t._geometry,
1608
- shader: s,
1609
- state: t.state
1610
- });
1611
- }
1612
- destroy() {
1613
- this._shader.destroy(!0), this._shader = null;
1614
- }
1615
- }
1616
- se.extension = {
1617
- type: [
1618
- p.WebGPUPipesAdaptor
1619
- ],
1620
- name: "mesh"
1621
- };
1622
- const Ie = [
1623
- ...Te,
1624
- q,
1625
- N,
1626
- U,
1627
- j,
1628
- W,
1629
- te,
1630
- X,
1631
- Z,
1632
- J,
1633
- $,
1634
- V,
1635
- K,
1636
- I
1637
- ], We = [...Ce, Y], Ve = [O, se, re], ie = [], ne = [], oe = [];
1638
- S.handleByNamedList(p.WebGPUSystem, ie);
1639
- S.handleByNamedList(p.WebGPUPipes, ne);
1640
- S.handleByNamedList(p.WebGPUPipesAdaptor, oe);
1641
- S.add(...Ie, ...We, ...Ve);
1642
- class Ye extends le {
1643
- constructor() {
1644
- const e = {
1645
- name: "webgpu",
1646
- type: fe.WEBGPU,
1647
- systems: ie,
1648
- renderPipes: ne,
1649
- renderPipeAdaptors: oe
1650
- };
1651
- super(e);
1652
- }
1653
- }
1654
- export {
1655
- Ye as WebGPURenderer
1656
- };
1657
- //# sourceMappingURL=WebGPURenderer-CVyBksYf.js.map