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