@luma.gl/webgpu 9.0.0-alpha.4 → 9.0.0-alpha.41

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (121) hide show
  1. package/LICENSE +3 -1
  2. package/dist/adapter/helpers/accessor-to-format.js.map +1 -1
  3. package/dist/adapter/helpers/convert-texture-format.d.ts +2 -2
  4. package/dist/adapter/helpers/convert-texture-format.d.ts.map +1 -1
  5. package/dist/adapter/helpers/convert-texture-format.js +0 -1
  6. package/dist/adapter/helpers/convert-texture-format.js.map +1 -1
  7. package/dist/adapter/helpers/generate-mipmaps.d.ts +1 -1
  8. package/dist/adapter/helpers/generate-mipmaps.js +19 -14
  9. package/dist/adapter/helpers/generate-mipmaps.js.map +1 -1
  10. package/dist/adapter/helpers/get-bind-group.d.ts +4 -4
  11. package/dist/adapter/helpers/get-bind-group.d.ts.map +1 -1
  12. package/dist/adapter/helpers/get-bind-group.js +8 -17
  13. package/dist/adapter/helpers/get-bind-group.js.map +1 -1
  14. package/dist/adapter/helpers/get-vertex-buffer-layout.d.ts +5 -5
  15. package/dist/adapter/helpers/get-vertex-buffer-layout.d.ts.map +1 -1
  16. package/dist/adapter/helpers/get-vertex-buffer-layout.js +33 -40
  17. package/dist/adapter/helpers/get-vertex-buffer-layout.js.map +1 -1
  18. package/dist/adapter/helpers/webgpu-parameters.d.ts +2 -2
  19. package/dist/adapter/helpers/webgpu-parameters.d.ts.map +1 -1
  20. package/dist/adapter/helpers/webgpu-parameters.js +51 -48
  21. package/dist/adapter/helpers/webgpu-parameters.js.map +1 -1
  22. package/dist/adapter/resources/webgpu-buffer.d.ts +4 -5
  23. package/dist/adapter/resources/webgpu-buffer.d.ts.map +1 -1
  24. package/dist/adapter/resources/webgpu-buffer.js +18 -32
  25. package/dist/adapter/resources/webgpu-buffer.js.map +1 -1
  26. package/dist/adapter/resources/webgpu-command-encoder.d.ts +6 -15
  27. package/dist/adapter/resources/webgpu-command-encoder.d.ts.map +1 -1
  28. package/dist/adapter/resources/webgpu-command-encoder.js +11 -29
  29. package/dist/adapter/resources/webgpu-command-encoder.js.map +1 -1
  30. package/dist/adapter/resources/webgpu-compute-pass.d.ts +6 -6
  31. package/dist/adapter/resources/webgpu-compute-pass.d.ts.map +1 -1
  32. package/dist/adapter/resources/webgpu-compute-pass.js +14 -27
  33. package/dist/adapter/resources/webgpu-compute-pass.js.map +1 -1
  34. package/dist/adapter/resources/webgpu-compute-pipeline.d.ts +4 -4
  35. package/dist/adapter/resources/webgpu-compute-pipeline.d.ts.map +1 -1
  36. package/dist/adapter/resources/webgpu-compute-pipeline.js +6 -11
  37. package/dist/adapter/resources/webgpu-compute-pipeline.js.map +1 -1
  38. package/dist/adapter/resources/webgpu-external-texture.d.ts +5 -5
  39. package/dist/adapter/resources/webgpu-external-texture.d.ts.map +1 -1
  40. package/dist/adapter/resources/webgpu-external-texture.js +6 -14
  41. package/dist/adapter/resources/webgpu-external-texture.js.map +1 -1
  42. package/dist/adapter/resources/webgpu-framebuffer.d.ts +4 -21
  43. package/dist/adapter/resources/webgpu-framebuffer.d.ts.map +1 -1
  44. package/dist/adapter/resources/webgpu-framebuffer.js +4 -105
  45. package/dist/adapter/resources/webgpu-framebuffer.js.map +1 -1
  46. package/dist/adapter/resources/webgpu-query.js.map +1 -1
  47. package/dist/adapter/resources/webgpu-render-pass.d.ts +12 -7
  48. package/dist/adapter/resources/webgpu-render-pass.d.ts.map +1 -1
  49. package/dist/adapter/resources/webgpu-render-pass.js +59 -36
  50. package/dist/adapter/resources/webgpu-render-pass.js.map +1 -1
  51. package/dist/adapter/resources/webgpu-render-pipeline.d.ts +14 -10
  52. package/dist/adapter/resources/webgpu-render-pipeline.d.ts.map +1 -1
  53. package/dist/adapter/resources/webgpu-render-pipeline.js +50 -67
  54. package/dist/adapter/resources/webgpu-render-pipeline.js.map +1 -1
  55. package/dist/adapter/resources/webgpu-sampler.d.ts +5 -5
  56. package/dist/adapter/resources/webgpu-sampler.d.ts.map +1 -1
  57. package/dist/adapter/resources/webgpu-sampler.js +11 -11
  58. package/dist/adapter/resources/webgpu-sampler.js.map +1 -1
  59. package/dist/adapter/resources/webgpu-shader.d.ts +7 -7
  60. package/dist/adapter/resources/webgpu-shader.d.ts.map +1 -1
  61. package/dist/adapter/resources/webgpu-shader.js +11 -25
  62. package/dist/adapter/resources/webgpu-shader.js.map +1 -1
  63. package/dist/adapter/resources/webgpu-texture.d.ts +15 -8
  64. package/dist/adapter/resources/webgpu-texture.d.ts.map +1 -1
  65. package/dist/adapter/resources/webgpu-texture.js +17 -27
  66. package/dist/adapter/resources/webgpu-texture.js.map +1 -1
  67. package/dist/adapter/resources/webgpu-vertex-array.d.ts +26 -0
  68. package/dist/adapter/resources/webgpu-vertex-array.d.ts.map +1 -0
  69. package/dist/adapter/resources/webgpu-vertex-array.js +39 -0
  70. package/dist/adapter/resources/webgpu-vertex-array.js.map +1 -0
  71. package/dist/adapter/webgpu-canvas-context.d.ts +16 -12
  72. package/dist/adapter/webgpu-canvas-context.d.ts.map +1 -1
  73. package/dist/adapter/webgpu-canvas-context.js +26 -47
  74. package/dist/adapter/webgpu-canvas-context.js.map +1 -1
  75. package/dist/adapter/webgpu-device.d.ts +38 -22
  76. package/dist/adapter/webgpu-device.d.ts.map +1 -1
  77. package/dist/adapter/webgpu-device.js +87 -91
  78. package/dist/adapter/webgpu-device.js.map +1 -1
  79. package/dist/adapter/webgpu-types.js.map +1 -1
  80. package/dist/dist.dev.js +3587 -0
  81. package/dist/glsl/glsllang.js +0 -1
  82. package/dist/glsl/glsllang.js.map +1 -1
  83. package/dist/index.cjs +1520 -0
  84. package/dist/index.d.ts +5 -7
  85. package/dist/index.d.ts.map +1 -1
  86. package/dist/index.js +5 -7
  87. package/dist/index.js.map +1 -1
  88. package/dist.min.js +18 -0
  89. package/package.json +18 -9
  90. package/src/adapter/helpers/accessor-to-format.ts +2 -2
  91. package/src/adapter/helpers/convert-texture-format.ts +1 -1
  92. package/src/adapter/helpers/generate-mipmaps.ts +1 -1
  93. package/src/adapter/helpers/get-bind-group.ts +22 -12
  94. package/src/adapter/helpers/get-vertex-buffer-layout.ts +58 -34
  95. package/src/adapter/helpers/webgpu-parameters.ts +56 -47
  96. package/src/adapter/resources/webgpu-buffer.ts +10 -11
  97. package/src/adapter/resources/webgpu-command-encoder.ts +62 -41
  98. package/src/adapter/resources/webgpu-compute-pass.ts +13 -13
  99. package/src/adapter/resources/webgpu-compute-pipeline.ts +6 -5
  100. package/src/adapter/resources/webgpu-external-texture.ts +5 -5
  101. package/src/adapter/resources/webgpu-framebuffer.ts +8 -109
  102. package/src/adapter/resources/webgpu-query.ts +3 -3
  103. package/src/adapter/resources/webgpu-render-pass.ts +74 -18
  104. package/src/adapter/resources/webgpu-render-pipeline.ts +64 -39
  105. package/src/adapter/resources/webgpu-sampler.ts +12 -5
  106. package/src/adapter/resources/webgpu-shader.ts +13 -10
  107. package/src/adapter/resources/webgpu-texture.ts +22 -8
  108. package/src/adapter/resources/webgpu-vertex-array.ts +74 -0
  109. package/src/adapter/webgpu-canvas-context.ts +52 -27
  110. package/src/adapter/webgpu-device.ts +150 -60
  111. package/src/index.ts +7 -9
  112. package/dist/bundle.d.ts +0 -2
  113. package/dist/bundle.d.ts.map +0 -1
  114. package/dist/bundle.js +0 -5
  115. package/dist/bundle.js.map +0 -1
  116. package/dist/init.d.ts +0 -2
  117. package/dist/init.d.ts.map +0 -1
  118. package/dist/init.js +0 -4
  119. package/dist/init.js.map +0 -1
  120. package/src/bundle.ts +0 -4
  121. package/src/init.ts +0 -4
package/dist/index.cjs ADDED
@@ -0,0 +1,1520 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
9
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
10
+ var __spreadValues = (a, b) => {
11
+ for (var prop in b || (b = {}))
12
+ if (__hasOwnProp.call(b, prop))
13
+ __defNormalProp(a, prop, b[prop]);
14
+ if (__getOwnPropSymbols)
15
+ for (var prop of __getOwnPropSymbols(b)) {
16
+ if (__propIsEnum.call(b, prop))
17
+ __defNormalProp(a, prop, b[prop]);
18
+ }
19
+ return a;
20
+ };
21
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
22
+ var __export = (target, all) => {
23
+ for (var name in all)
24
+ __defProp(target, name, { get: all[name], enumerable: true });
25
+ };
26
+ var __copyProps = (to, from, except, desc) => {
27
+ if (from && typeof from === "object" || typeof from === "function") {
28
+ for (let key of __getOwnPropNames(from))
29
+ if (!__hasOwnProp.call(to, key) && key !== except)
30
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
31
+ }
32
+ return to;
33
+ };
34
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
35
+ var __async = (__this, __arguments, generator) => {
36
+ return new Promise((resolve, reject) => {
37
+ var fulfilled = (value) => {
38
+ try {
39
+ step(generator.next(value));
40
+ } catch (e) {
41
+ reject(e);
42
+ }
43
+ };
44
+ var rejected = (value) => {
45
+ try {
46
+ step(generator.throw(value));
47
+ } catch (e) {
48
+ reject(e);
49
+ }
50
+ };
51
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
52
+ step((generator = generator.apply(__this, __arguments)).next());
53
+ });
54
+ };
55
+
56
+ // src/index.ts
57
+ var src_exports = {};
58
+ __export(src_exports, {
59
+ WebGPUBuffer: () => WebGPUBuffer,
60
+ WebGPUDevice: () => WebGPUDevice,
61
+ WebGPUSampler: () => WebGPUSampler,
62
+ WebGPUShader: () => WebGPUShader,
63
+ WebGPUTexture: () => WebGPUTexture
64
+ });
65
+ module.exports = __toCommonJS(src_exports);
66
+
67
+ // src/adapter/webgpu-device.ts
68
+ var import_core15 = require("@luma.gl/core");
69
+
70
+ // src/adapter/resources/webgpu-buffer.ts
71
+ var import_core = require("@luma.gl/core");
72
+ function getByteLength(props) {
73
+ var _a;
74
+ return props.byteLength || ((_a = props.data) == null ? void 0 : _a.byteLength) || 0;
75
+ }
76
+ var WebGPUBuffer = class extends import_core.Buffer {
77
+ constructor(device, props) {
78
+ super(device, props);
79
+ this.device = device;
80
+ this.byteLength = getByteLength(props);
81
+ const mapBuffer = Boolean(props.data);
82
+ const size = Math.ceil(this.byteLength / 4) * 4;
83
+ this.handle = this.props.handle || this.device.handle.createBuffer({
84
+ size,
85
+ // usage defaults to vertex
86
+ usage: this.props.usage || GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST,
87
+ mappedAtCreation: this.props.mappedAtCreation || mapBuffer,
88
+ label: this.props.id
89
+ });
90
+ if (props.data) {
91
+ this._writeMapped(props.data);
92
+ }
93
+ if (mapBuffer && !props.mappedAtCreation) {
94
+ this.handle.unmap();
95
+ }
96
+ }
97
+ destroy() {
98
+ this.handle.destroy();
99
+ }
100
+ // WebGPU provides multiple ways to write a buffer...
101
+ write(data, byteOffset = 0) {
102
+ this.device.handle.queue.writeBuffer(
103
+ this.handle,
104
+ byteOffset,
105
+ data.buffer,
106
+ data.byteOffset,
107
+ data.byteLength
108
+ );
109
+ }
110
+ readAsync() {
111
+ return __async(this, arguments, function* (byteOffset = 0, byteLength = this.byteLength) {
112
+ const tempBuffer = new WebGPUBuffer(this.device, { usage: import_core.Buffer.MAP_READ | import_core.Buffer.COPY_DST, byteLength });
113
+ const commandEncoder = this.device.handle.createCommandEncoder();
114
+ commandEncoder.copyBufferToBuffer(this.handle, byteOffset, tempBuffer.handle, 0, byteLength);
115
+ this.device.handle.queue.submit([commandEncoder.finish()]);
116
+ yield tempBuffer.handle.mapAsync(GPUMapMode.READ, byteOffset, byteLength);
117
+ const arrayBuffer = tempBuffer.handle.getMappedRange().slice(0);
118
+ tempBuffer.handle.unmap();
119
+ tempBuffer.destroy();
120
+ return arrayBuffer;
121
+ });
122
+ }
123
+ _writeMapped(typedArray) {
124
+ const arrayBuffer = this.handle.getMappedRange();
125
+ new typedArray.constructor(arrayBuffer).set(typedArray);
126
+ }
127
+ // WEBGPU API
128
+ mapAsync(mode, offset = 0, size) {
129
+ return this.handle.mapAsync(mode, offset, size);
130
+ }
131
+ getMappedRange(offset = 0, size) {
132
+ return this.handle.getMappedRange(offset, size);
133
+ }
134
+ unmap() {
135
+ this.handle.unmap();
136
+ }
137
+ };
138
+
139
+ // src/adapter/resources/webgpu-texture.ts
140
+ var import_core3 = require("@luma.gl/core");
141
+
142
+ // src/adapter/helpers/convert-texture-format.ts
143
+ function getWebGPUTextureFormat(format) {
144
+ if (format.includes("webgl")) {
145
+ throw new Error("webgl-only format");
146
+ }
147
+ return format;
148
+ }
149
+
150
+ // src/adapter/resources/webgpu-sampler.ts
151
+ var import_core2 = require("@luma.gl/core");
152
+ var WebGPUSampler = class extends import_core2.Sampler {
153
+ constructor(device, props) {
154
+ super(device, props);
155
+ this.device = device;
156
+ const samplerProps = __spreadValues({}, this.props);
157
+ if (samplerProps.type !== "comparison-sampler") {
158
+ delete samplerProps.compare;
159
+ }
160
+ this.handle = this.handle || this.device.handle.createSampler(samplerProps);
161
+ this.handle.label = this.props.id;
162
+ }
163
+ destroy() {
164
+ }
165
+ };
166
+
167
+ // src/adapter/resources/webgpu-texture.ts
168
+ var BASE_DIMENSIONS = {
169
+ "1d": "1d",
170
+ "2d": "2d",
171
+ "2d-array": "2d",
172
+ "cube": "2d",
173
+ "cube-array": "2d",
174
+ "3d": "3d"
175
+ };
176
+ var WebGPUTexture = class extends import_core3.Texture {
177
+ // static async createFromImageURL(src, usage = 0) {
178
+ // const img = document.createElement('img');
179
+ // img.src = src;
180
+ // await img.decode();
181
+ // return WebGPUTexture(img, usage);
182
+ // }
183
+ constructor(device, props) {
184
+ super(device, props);
185
+ if (typeof this.props.format === "number") {
186
+ throw new Error("number format");
187
+ }
188
+ this.device = device;
189
+ this.handle = this.props.handle || this.createHandle();
190
+ if (this.props.data) {
191
+ this.setData({ data: this.props.data });
192
+ }
193
+ this.width = this.handle.width;
194
+ this.height = this.handle.height;
195
+ this.sampler = props.sampler instanceof WebGPUSampler ? props.sampler : new WebGPUSampler(this.device, props.sampler);
196
+ this.view = this.handle.createView({
197
+ // format: this.props.format,
198
+ // dimension: this.props.dimension,
199
+ // aspect = "all";
200
+ // baseMipLevel: 0;
201
+ // mipLevelCount;
202
+ // baseArrayLayer = 0;
203
+ // arrayLayerCount;
204
+ });
205
+ }
206
+ createHandle() {
207
+ var _a, _b;
208
+ if (typeof this.props.format === "number") {
209
+ throw new Error("number format");
210
+ }
211
+ const width = this.props.width || ((_a = this.props.data) == null ? void 0 : _a.width) || 1;
212
+ const height = this.props.height || ((_b = this.props.data) == null ? void 0 : _b.height) || 1;
213
+ return this.device.handle.createTexture({
214
+ size: {
215
+ width,
216
+ height,
217
+ depthOrArrayLayers: this.props.depth
218
+ },
219
+ dimension: BASE_DIMENSIONS[this.props.dimension],
220
+ format: getWebGPUTextureFormat(this.props.format),
221
+ usage: this.props.usage,
222
+ mipLevelCount: this.props.mipLevels,
223
+ sampleCount: this.props.samples
224
+ });
225
+ }
226
+ destroy() {
227
+ this.handle.destroy();
228
+ }
229
+ /**
230
+ * Set default sampler
231
+ * Accept a sampler instance or set of props;
232
+ */
233
+ setSampler(sampler) {
234
+ this.sampler = sampler instanceof WebGPUSampler ? sampler : new WebGPUSampler(this.device, sampler);
235
+ return this;
236
+ }
237
+ setData(options) {
238
+ return this.setImage({ source: options.data });
239
+ }
240
+ /** Set image */
241
+ setImage(options) {
242
+ const {
243
+ source,
244
+ width = options.source.width,
245
+ height = options.source.height,
246
+ depth = 1,
247
+ sourceX = 0,
248
+ sourceY = 0,
249
+ mipLevel = 0,
250
+ x = 0,
251
+ y = 0,
252
+ z = 0,
253
+ aspect = "all",
254
+ colorSpace = "srgb",
255
+ premultipliedAlpha = false
256
+ } = options;
257
+ this.device.handle.queue.copyExternalImageToTexture(
258
+ // source: GPUImageCopyExternalImage
259
+ {
260
+ source,
261
+ origin: [sourceX, sourceY]
262
+ },
263
+ // destination: GPUImageCopyTextureTagged
264
+ {
265
+ texture: this.handle,
266
+ origin: [x, y, z],
267
+ mipLevel,
268
+ aspect,
269
+ colorSpace,
270
+ premultipliedAlpha
271
+ },
272
+ // copySize: GPUExtent3D
273
+ [
274
+ width,
275
+ height,
276
+ depth
277
+ ]
278
+ );
279
+ return { width, height };
280
+ }
281
+ /*
282
+ async readPixels() {
283
+ const readbackBuffer = device.createBuffer({
284
+ usage: Buffer.COPY_DST | Buffer.MAP_READ,
285
+ size: 4 * textureWidth * textureHeight,
286
+ });
287
+
288
+ // Copy data from the texture to the buffer.
289
+ const encoder = device.createCommandEncoder();
290
+ encoder.copyTextureToBuffer(
291
+ { texture },
292
+ { buffer, rowPitch: textureWidth * 4 },
293
+ [textureWidth, textureHeight],
294
+ );
295
+ device.submit([encoder.finish()]);
296
+
297
+ // Get the data on the CPU.
298
+ await buffer.mapAsync(GPUMapMode.READ);
299
+ saveScreenshot(buffer.getMappedRange());
300
+ buffer.unmap();
301
+ }
302
+
303
+ setImageData(imageData, usage): this {
304
+ let data = null;
305
+
306
+ const bytesPerRow = Math.ceil((img.width * 4) / 256) * 256;
307
+ if (bytesPerRow == img.width * 4) {
308
+ data = imageData.data;
309
+ } else {
310
+ data = new Uint8Array(bytesPerRow * img.height);
311
+ let imagePixelIndex = 0;
312
+ for (let y = 0; y < img.height; ++y) {
313
+ for (let x = 0; x < img.width; ++x) {
314
+ const i = x * 4 + y * bytesPerRow;
315
+ data[i] = imageData.data[imagePixelIndex];
316
+ data[i + 1] = imageData.data[imagePixelIndex + 1];
317
+ data[i + 2] = imageData.data[imagePixelIndex + 2];
318
+ data[i + 3] = imageData.data[imagePixelIndex + 3];
319
+ imagePixelIndex += 4;
320
+ }
321
+ }
322
+ }
323
+ return this;
324
+ }
325
+
326
+ setData(data): this {
327
+ const textureDataBuffer = this.device.handle.createBuffer({
328
+ size: data.byteLength,
329
+ usage: Buffer.COPY_DST | Buffer.COPY_SRC,
330
+ mappedAtCreation: true
331
+ });
332
+ new Uint8Array(textureDataBuffer.getMappedRange()).set(data);
333
+ textureDataBuffer.unmap();
334
+
335
+ this.setBuffer(textureDataBuffer);
336
+
337
+ textureDataBuffer.destroy();
338
+ return this;
339
+ }
340
+
341
+ setBuffer(textureDataBuffer, {bytesPerRow}): this {
342
+ const commandEncoder = this.device.handle.createCommandEncoder();
343
+ commandEncoder.copyBufferToTexture(
344
+ {
345
+ buffer: textureDataBuffer,
346
+ bytesPerRow
347
+ },
348
+ {
349
+ texture: this.handle
350
+ },
351
+ {
352
+ width,
353
+ height,
354
+ depth
355
+ }
356
+ );
357
+
358
+ this.device.handle.defaultQueue.submit([commandEncoder.finish()]);
359
+ return this;
360
+ }
361
+ */
362
+ };
363
+
364
+ // src/adapter/resources/webgpu-external-texture.ts
365
+ var import_core4 = require("@luma.gl/core");
366
+ var WebGPUExternalTexture = class extends import_core4.ExternalTexture {
367
+ constructor(device, props) {
368
+ super(device, props);
369
+ this.device = device;
370
+ this.handle = this.props.handle || this.device.handle.importExternalTexture({
371
+ source: props.source,
372
+ colorSpace: props.colorSpace
373
+ });
374
+ this.sampler = null;
375
+ }
376
+ destroy() {
377
+ }
378
+ /** Set default sampler */
379
+ setSampler(sampler) {
380
+ this.sampler = sampler instanceof WebGPUSampler ? sampler : new WebGPUSampler(this.device, sampler);
381
+ return this;
382
+ }
383
+ };
384
+
385
+ // src/adapter/resources/webgpu-shader.ts
386
+ var import_core5 = require("@luma.gl/core");
387
+ var WebGPUShader = class extends import_core5.Shader {
388
+ constructor(device, props) {
389
+ super(device, props);
390
+ this.device = device;
391
+ this.device.handle.pushErrorScope("validation");
392
+ this.handle = this.props.handle || this.createHandle();
393
+ this.handle.label = this.props.id;
394
+ this._checkCompilationError(this.device.handle.popErrorScope());
395
+ }
396
+ _checkCompilationError(errorScope) {
397
+ return __async(this, null, function* () {
398
+ const error = yield errorScope;
399
+ if (error) {
400
+ const shaderLog = yield this.compilationInfo();
401
+ import_core5.log.error(`Shader compilation error: ${error.message}`, shaderLog)();
402
+ throw new Error(`Shader compilation error: ${error.message}`);
403
+ }
404
+ });
405
+ }
406
+ destroy() {
407
+ }
408
+ createHandle() {
409
+ const { source, stage } = this.props;
410
+ let language = this.props.language;
411
+ if (language === "auto") {
412
+ language = source.includes("->") ? "wgsl" : "glsl";
413
+ }
414
+ switch (language) {
415
+ case "wgsl":
416
+ return this.device.handle.createShaderModule({ code: source });
417
+ case "glsl":
418
+ return this.device.handle.createShaderModule({
419
+ code: source,
420
+ // @ts-expect-error
421
+ transform: (glsl) => this.device.glslang.compileGLSL(glsl, stage)
422
+ });
423
+ default:
424
+ throw new Error(language);
425
+ }
426
+ }
427
+ /** Returns compilation info for this shader */
428
+ compilationInfo() {
429
+ return __async(this, null, function* () {
430
+ const compilationInfo = yield this.handle.getCompilationInfo();
431
+ return compilationInfo.messages;
432
+ });
433
+ }
434
+ };
435
+
436
+ // src/adapter/resources/webgpu-render-pipeline.ts
437
+ var import_core8 = require("@luma.gl/core");
438
+
439
+ // src/adapter/helpers/webgpu-parameters.ts
440
+ function addDepthStencil(descriptor) {
441
+ descriptor.depthStencil = descriptor.depthStencil || {
442
+ // required, set something
443
+ format: "depth24plus",
444
+ stencilFront: {},
445
+ stencilBack: {},
446
+ // TODO can this cause trouble? Should we set to WebGPU defaults? Are there defaults?
447
+ depthWriteEnabled: false,
448
+ depthCompare: "less-equal"
449
+ };
450
+ return descriptor.depthStencil;
451
+ }
452
+ var PARAMETER_TABLE = {
453
+ // RASTERIZATION PARAMETERS
454
+ cullMode: (parameter, value, descriptor) => {
455
+ descriptor.primitive = descriptor.primitive || {};
456
+ descriptor.primitive.cullMode = value;
457
+ },
458
+ frontFace: (parameter, value, descriptor) => {
459
+ descriptor.primitive = descriptor.primitive || {};
460
+ descriptor.primitive.frontFace = value;
461
+ },
462
+ // DEPTH
463
+ depthWriteEnabled: (parameter, value, descriptor) => {
464
+ const depthStencil = addDepthStencil(descriptor);
465
+ depthStencil.depthWriteEnabled = value;
466
+ },
467
+ depthCompare: (parameter, value, descriptor) => {
468
+ const depthStencil = addDepthStencil(descriptor);
469
+ depthStencil.depthCompare = value;
470
+ },
471
+ depthFormat: (parameter, value, descriptor) => {
472
+ const depthStencil = addDepthStencil(descriptor);
473
+ depthStencil.format = value;
474
+ },
475
+ depthBias: (parameter, value, descriptor) => {
476
+ const depthStencil = addDepthStencil(descriptor);
477
+ depthStencil.depthBias = value;
478
+ },
479
+ depthBiasSlopeScale: (parameter, value, descriptor) => {
480
+ const depthStencil = addDepthStencil(descriptor);
481
+ depthStencil.depthBiasSlopeScale = value;
482
+ },
483
+ depthBiasClamp: (parameter, value, descriptor) => {
484
+ const depthStencil = addDepthStencil(descriptor);
485
+ depthStencil.depthBiasClamp = value;
486
+ },
487
+ // STENCIL
488
+ stencilReadMask: (parameter, value, descriptor) => {
489
+ const depthStencil = addDepthStencil(descriptor);
490
+ depthStencil.stencilReadMask = value;
491
+ },
492
+ stencilWriteMask: (parameter, value, descriptor) => {
493
+ const depthStencil = addDepthStencil(descriptor);
494
+ depthStencil.stencilWriteMask = value;
495
+ },
496
+ stencilCompare: (parameter, value, descriptor) => {
497
+ const depthStencil = addDepthStencil(descriptor);
498
+ depthStencil.stencilFront.compare = value;
499
+ depthStencil.stencilBack.compare = value;
500
+ },
501
+ stencilPassOperation: (parameter, value, descriptor) => {
502
+ const depthStencil = addDepthStencil(descriptor);
503
+ depthStencil.stencilFront.passOp = value;
504
+ depthStencil.stencilBack.passOp = value;
505
+ },
506
+ stencilFailOperation: (parameter, value, descriptor) => {
507
+ const depthStencil = addDepthStencil(descriptor);
508
+ depthStencil.stencilFront.failOp = value;
509
+ depthStencil.stencilBack.failOp = value;
510
+ },
511
+ stencilDepthFailOperation: (parameter, value, descriptor) => {
512
+ const depthStencil = addDepthStencil(descriptor);
513
+ depthStencil.stencilFront.depthFailOp = value;
514
+ depthStencil.stencilBack.depthFailOp = value;
515
+ },
516
+ // MULTISAMPLE
517
+ sampleCount: (parameter, value, descriptor) => {
518
+ descriptor.multisample = descriptor.multisample || {};
519
+ descriptor.multisample.count = value;
520
+ },
521
+ sampleMask: (parameter, value, descriptor) => {
522
+ descriptor.multisample = descriptor.multisample || {};
523
+ descriptor.multisample.mask = value;
524
+ },
525
+ sampleAlphaToCoverageEnabled: (parameter, value, descriptor) => {
526
+ descriptor.multisample = descriptor.multisample || {};
527
+ descriptor.multisample.alphaToCoverageEnabled = value;
528
+ },
529
+ // COLOR
530
+ colorMask: (parameter, value, descriptor) => {
531
+ const targets = addColorState(descriptor);
532
+ targets[0].writeMask = value;
533
+ },
534
+ blendColorOperation: (parameter, value, descriptor) => {
535
+ addColorState(descriptor);
536
+ }
537
+ /*
538
+ blendColorSrcTarget: (parameter, value, descriptor: GPURenderPipelineDescriptor) => {
539
+ addColorState(descriptor);
540
+ targets[0].blend = targets[0].blend || {};
541
+ targets[0].blend.color = targets[0].blend.color || {};
542
+ targets[0].blend.color.srcTarget = value;
543
+ },
544
+
545
+ blendColorDstTarget: (parameter, value, descriptor: GPURenderPipelineDescriptor) => {
546
+ addColorState(descriptor);
547
+ targets[0].blend = targets[0].blend || {};
548
+ targets[0].blend.color = targets[0].blend.color || {};
549
+ targets[0].blend.color.dstTarget = value;
550
+ },
551
+
552
+ blendAlphaOperation: (parameter, value, descriptor: GPURenderPipelineDescriptor) => {
553
+ addColorState(descriptor);
554
+ targets[0].blend = targets[0].blend || {};
555
+ targets[0].blend.alpha = targets[0].blend.alpha || {};
556
+ targets[0].blend.alpha.operation = value;
557
+ },
558
+
559
+ blendAlphaSrcTarget: (parameter, value, descriptor: GPURenderPipelineDescriptor) => {
560
+ addColorState(descriptor);
561
+ targets[0].blend = targets[0].blend || {};
562
+ targets[0].blend.alpha = targets[0].blend.alpha || {};
563
+ targets[0].blend.alpha.srcTarget = value;
564
+ },
565
+
566
+ blendAlphaDstTarget: (parameter, value, descriptor: GPURenderPipelineDescriptor) => {
567
+ addColorState(descriptor);
568
+ targets[0].blend = targets[0].blend || {};
569
+ targets[0].blend.alpha = targets[0].blend.alpha || {};
570
+ targets[0].blend.alpha.dstTarget = value;
571
+ },
572
+ */
573
+ };
574
+ var DEFAULT_PIPELINE_DESCRIPTOR = {
575
+ // depthStencil: {
576
+ // stencilFront: {},
577
+ // stencilBack: {},
578
+ // // depthWriteEnabled: true,
579
+ // // depthCompare: 'less',
580
+ // // format: 'depth24plus-stencil8',
581
+ // },
582
+ primitive: {
583
+ cullMode: "back",
584
+ topology: "triangle-list"
585
+ },
586
+ vertex: {
587
+ module: void 0,
588
+ entryPoint: "main"
589
+ },
590
+ fragment: {
591
+ module: void 0,
592
+ entryPoint: "main",
593
+ targets: [
594
+ // { format: props.color0Format || 'bgra8unorm' }
595
+ ]
596
+ },
597
+ layout: "auto"
598
+ };
599
+ function applyParametersToRenderPipelineDescriptor(pipelineDescriptor, parameters = {}) {
600
+ Object.assign(pipelineDescriptor, __spreadValues(__spreadValues({}, DEFAULT_PIPELINE_DESCRIPTOR), pipelineDescriptor));
601
+ setParameters(pipelineDescriptor, parameters);
602
+ }
603
+ function setParameters(pipelineDescriptor, parameters) {
604
+ for (const [key, value] of Object.entries(parameters)) {
605
+ const setterFunction = PARAMETER_TABLE[key];
606
+ if (!setterFunction) {
607
+ throw new Error(`Illegal parameter ${key}`);
608
+ }
609
+ setterFunction(key, value, pipelineDescriptor);
610
+ }
611
+ }
612
+ function addColorState(descriptor) {
613
+ var _a, _b, _c, _d, _e, _f;
614
+ descriptor.fragment.targets = ((_a = descriptor.fragment) == null ? void 0 : _a.targets) || [];
615
+ if (!Array.isArray((_b = descriptor.fragment) == null ? void 0 : _b.targets)) {
616
+ throw new Error("colorstate");
617
+ }
618
+ if (((_d = (_c = descriptor.fragment) == null ? void 0 : _c.targets) == null ? void 0 : _d.length) === 0) {
619
+ (_e = descriptor.fragment.targets) == null ? void 0 : _e.push({});
620
+ }
621
+ return (_f = descriptor.fragment) == null ? void 0 : _f.targets;
622
+ }
623
+
624
+ // src/adapter/helpers/get-bind-group.ts
625
+ var import_core6 = require("@luma.gl/core");
626
+ function getBindGroup(device, bindGroupLayout, shaderLayout, bindings) {
627
+ const entries = getBindGroupEntries(bindings, shaderLayout);
628
+ return device.createBindGroup({
629
+ layout: bindGroupLayout,
630
+ entries
631
+ });
632
+ }
633
+ function getShaderLayoutBinding(shaderLayout, bindingName) {
634
+ const bindingLayout = shaderLayout.bindings.find((binding) => binding.name === bindingName);
635
+ if (!bindingLayout) {
636
+ import_core6.log.warn(`Binding ${bindingName} not set: Not found in shader layout.`)();
637
+ }
638
+ return bindingLayout;
639
+ }
640
+ function getBindGroupEntries(bindings, shaderLayout) {
641
+ const entries = [];
642
+ for (const [bindingName, value] of Object.entries(bindings)) {
643
+ const bindingLayout = getShaderLayoutBinding(shaderLayout, bindingName);
644
+ if (bindingLayout) {
645
+ entries.push(getBindGroupEntry(value, bindingLayout.location));
646
+ }
647
+ }
648
+ return entries;
649
+ }
650
+ function getBindGroupEntry(binding, index) {
651
+ if (binding instanceof import_core6.Buffer) {
652
+ return {
653
+ binding: index,
654
+ resource: {
655
+ buffer: (0, import_core6.cast)(binding).handle
656
+ }
657
+ };
658
+ }
659
+ if (binding instanceof import_core6.Sampler) {
660
+ return {
661
+ binding: index,
662
+ resource: (0, import_core6.cast)(binding).handle
663
+ };
664
+ } else if (binding instanceof import_core6.Texture) {
665
+ return {
666
+ binding: index,
667
+ resource: (0, import_core6.cast)(binding).handle.createView()
668
+ };
669
+ }
670
+ throw new Error("invalid binding");
671
+ }
672
+
673
+ // src/adapter/helpers/get-vertex-buffer-layout.ts
674
+ var import_core7 = require("@luma.gl/core");
675
+ function getWebGPUVertexFormat(format) {
676
+ if (format.endsWith("-webgl")) {
677
+ throw new Error(`WebGPU does not support vertex format ${format}`);
678
+ }
679
+ return format;
680
+ }
681
+ function getVertexBufferLayout(shaderLayout, bufferLayout) {
682
+ const attributeInfos = (0, import_core7.getAttributeInfosFromLayouts)(shaderLayout, bufferLayout);
683
+ const vertexBufferLayouts = [];
684
+ const usedAttributes = /* @__PURE__ */ new Set();
685
+ for (const mapping of bufferLayout) {
686
+ const vertexAttributes = [];
687
+ let stepMode = "vertex";
688
+ let byteStride = 0;
689
+ if (mapping.attributes) {
690
+ for (const attributeMapping of mapping.attributes) {
691
+ const attributeName = attributeMapping.attribute;
692
+ const attributeLayout = findAttributeLayout(shaderLayout, attributeName, usedAttributes);
693
+ stepMode = attributeLayout.stepMode || "vertex";
694
+ vertexAttributes.push({
695
+ format: getWebGPUVertexFormat(attributeMapping.format || mapping.format),
696
+ offset: attributeMapping.byteOffset,
697
+ shaderLocation: attributeLayout.location
698
+ });
699
+ byteStride += (0, import_core7.decodeVertexFormat)(mapping.format).byteLength;
700
+ }
701
+ } else {
702
+ const attributeLayout = findAttributeLayout(shaderLayout, mapping.name, usedAttributes);
703
+ byteStride = (0, import_core7.decodeVertexFormat)(mapping.format).byteLength;
704
+ stepMode = attributeLayout.stepMode || "vertex";
705
+ vertexAttributes.push({
706
+ format: getWebGPUVertexFormat(mapping.format),
707
+ // We only support 0 offset for non-interleaved buffer layouts
708
+ offset: 0,
709
+ shaderLocation: attributeLayout.location
710
+ });
711
+ }
712
+ vertexBufferLayouts.push({
713
+ arrayStride: mapping.byteStride || byteStride,
714
+ stepMode: stepMode || "vertex",
715
+ attributes: vertexAttributes
716
+ });
717
+ }
718
+ for (const attribute of shaderLayout.attributes) {
719
+ if (!usedAttributes.has(attribute.name)) {
720
+ vertexBufferLayouts.push({
721
+ arrayStride: (0, import_core7.decodeVertexFormat)("float32x3").byteLength,
722
+ stepMode: attribute.stepMode || "vertex",
723
+ attributes: [
724
+ {
725
+ format: getWebGPUVertexFormat("float32x3"),
726
+ offset: 0,
727
+ shaderLocation: attribute.location
728
+ }
729
+ ]
730
+ });
731
+ }
732
+ }
733
+ return vertexBufferLayouts;
734
+ }
735
+ function getBufferSlots(shaderLayout, bufferLayout) {
736
+ const usedAttributes = /* @__PURE__ */ new Set();
737
+ let bufferSlot = 0;
738
+ const bufferSlots = {};
739
+ for (const mapping of bufferLayout) {
740
+ if ("attributes" in mapping) {
741
+ for (const interleaved of mapping.attributes) {
742
+ usedAttributes.add(interleaved.attribute);
743
+ }
744
+ } else {
745
+ usedAttributes.add(mapping.name);
746
+ }
747
+ bufferSlots[mapping.name] = bufferSlot++;
748
+ }
749
+ for (const attribute of shaderLayout.attributes) {
750
+ if (!usedAttributes.has(attribute.name)) {
751
+ bufferSlots[attribute.name] = bufferSlot++;
752
+ }
753
+ }
754
+ return bufferSlots;
755
+ }
756
+ function findAttributeLayout(shaderLayout, name, attributeNames) {
757
+ const attribute = shaderLayout.attributes.find((attribute2) => attribute2.name === name);
758
+ if (!attribute) {
759
+ throw new Error(`Unknown attribute ${name}`);
760
+ }
761
+ if (attributeNames.has(name)) {
762
+ throw new Error(`Duplicate attribute ${name}`);
763
+ }
764
+ attributeNames.add(name);
765
+ return attribute;
766
+ }
767
+
768
+ // src/adapter/resources/webgpu-render-pipeline.ts
769
+ var WebGPURenderPipeline = class extends import_core8.RenderPipeline {
770
+ constructor(device, props) {
771
+ super(device, props);
772
+ this.fs = null;
773
+ this._indexBuffer = null;
774
+ // private _firstIndex: number;
775
+ // private _lastIndex: number;
776
+ /** For internal use to create BindGroups */
777
+ this._bindGroupLayout = null;
778
+ this._bindGroup = null;
779
+ this.device = device;
780
+ this.handle = this.props.handle;
781
+ if (!this.handle) {
782
+ const descriptor = this._getRenderPipelineDescriptor();
783
+ import_core8.log.groupCollapsed(1, `new WebGPURenderPipeline(${this.id})`)();
784
+ import_core8.log.probe(1, JSON.stringify(descriptor, null, 2))();
785
+ import_core8.log.groupEnd(1)();
786
+ this.handle = this.device.handle.createRenderPipeline(descriptor);
787
+ }
788
+ this.handle.label = this.props.id;
789
+ this.vs = (0, import_core8.cast)(props.vs);
790
+ this.fs = (0, import_core8.cast)(props.fs);
791
+ this._bufferSlots = getBufferSlots(this.props.shaderLayout, this.props.bufferLayout);
792
+ this._buffers = new Array(Object.keys(this._bufferSlots).length).fill(null);
793
+ }
794
+ destroy() {
795
+ }
796
+ setIndexBuffer(indexBuffer) {
797
+ this._indexBuffer = (0, import_core8.cast)(indexBuffer);
798
+ }
799
+ setAttributes(attributes) {
800
+ for (const [name, buffer] of Object.entries(attributes)) {
801
+ const bufferIndex = this._bufferSlots[name];
802
+ if (bufferIndex >= 0) {
803
+ this._buffers[bufferIndex] = buffer;
804
+ } else {
805
+ throw new Error(
806
+ `Setting attribute '${name}' not listed in shader layout for program ${this.id}`
807
+ );
808
+ }
809
+ }
810
+ }
811
+ setConstantAttributes(attributes) {
812
+ throw new Error("not implemented");
813
+ }
814
+ setBindings(bindings) {
815
+ if (!(0, import_core8.isObjectEmpty)(this.props.bindings)) {
816
+ Object.assign(this.props.bindings, bindings);
817
+ this._bindGroupLayout = this._bindGroupLayout || this.handle.getBindGroupLayout(0);
818
+ this._bindGroup = getBindGroup(
819
+ this.device.handle,
820
+ this._bindGroupLayout,
821
+ this.props.shaderLayout,
822
+ this.props.bindings
823
+ );
824
+ }
825
+ }
826
+ setUniforms(uniforms) {
827
+ if (!(0, import_core8.isObjectEmpty)(uniforms)) {
828
+ throw new Error("WebGPU does not support uniforms");
829
+ }
830
+ }
831
+ _getBuffers() {
832
+ return this._buffers;
833
+ }
834
+ /** Return a bind group created by setBindings */
835
+ _getBindGroup() {
836
+ return this._bindGroup;
837
+ }
838
+ /**
839
+ * Populate the complex WebGPU GPURenderPipelineDescriptor
840
+ */
841
+ _getRenderPipelineDescriptor() {
842
+ var _a, _b;
843
+ const vertex = {
844
+ module: (0, import_core8.cast)(this.props.vs).handle,
845
+ entryPoint: this.props.vsEntryPoint || "main",
846
+ buffers: getVertexBufferLayout(this.props.shaderLayout, this.props.bufferLayout)
847
+ };
848
+ let fragment;
849
+ if (this.props.fs) {
850
+ fragment = {
851
+ module: (0, import_core8.cast)(this.props.fs).handle,
852
+ entryPoint: this.props.fsEntryPoint || "main",
853
+ targets: [
854
+ {
855
+ // TODO exclamation mark hack!
856
+ format: getWebGPUTextureFormat((_b = (_a = this.device) == null ? void 0 : _a.canvasContext) == null ? void 0 : _b.format)
857
+ }
858
+ ]
859
+ };
860
+ }
861
+ switch (this.props.topology) {
862
+ case "triangle-fan-webgl":
863
+ case "line-loop-webgl":
864
+ throw new Error(`WebGPU does not support primitive topology ${this.props.topology}`);
865
+ default:
866
+ }
867
+ const descriptor = {
868
+ vertex,
869
+ fragment,
870
+ primitive: {
871
+ topology: this.props.topology
872
+ },
873
+ layout: "auto"
874
+ };
875
+ applyParametersToRenderPipelineDescriptor(descriptor, this.props.parameters);
876
+ return descriptor;
877
+ }
878
+ draw(options) {
879
+ const webgpuRenderPass = (0, import_core8.cast)(options.renderPass) || this.device.getDefaultRenderPass();
880
+ webgpuRenderPass.handle.setPipeline(this.handle);
881
+ const bindGroup = this._getBindGroup();
882
+ if (bindGroup) {
883
+ webgpuRenderPass.handle.setBindGroup(0, bindGroup);
884
+ }
885
+ this._setAttributeBuffers(webgpuRenderPass);
886
+ if (options.indexCount) {
887
+ webgpuRenderPass.handle.drawIndexed(
888
+ options.indexCount,
889
+ options.instanceCount,
890
+ options.firstIndex,
891
+ options.baseVertex,
892
+ options.firstInstance
893
+ );
894
+ } else {
895
+ webgpuRenderPass.handle.draw(
896
+ options.vertexCount || 0,
897
+ options.instanceCount || 1,
898
+ // If 0, nothing will be drawn
899
+ options.firstInstance
900
+ );
901
+ }
902
+ }
903
+ _setAttributeBuffers(webgpuRenderPass) {
904
+ if (this._indexBuffer) {
905
+ webgpuRenderPass.handle.setIndexBuffer(this._indexBuffer.handle, this._indexBuffer.props.indexType);
906
+ }
907
+ const buffers = this._getBuffers();
908
+ for (let i = 0; i < buffers.length; ++i) {
909
+ const buffer = (0, import_core8.cast)(buffers[i]);
910
+ if (!buffer) {
911
+ const attribute = this.props.shaderLayout.attributes.find(
912
+ (attribute2) => attribute2.location === i
913
+ );
914
+ throw new Error(
915
+ `No buffer provided for attribute '${(attribute == null ? void 0 : attribute.name) || ""}' in Model '${this.props.id}'`
916
+ );
917
+ }
918
+ webgpuRenderPass.handle.setVertexBuffer(i, buffer.handle);
919
+ }
920
+ }
921
+ };
922
+
923
+ // src/adapter/resources/webgpu-compute-pipeline.ts
924
+ var import_core9 = require("@luma.gl/core");
925
+ var WebGPUComputePipeline = class extends import_core9.ComputePipeline {
926
+ constructor(device, props) {
927
+ super(device, props);
928
+ this.device = device;
929
+ const module2 = (0, import_core9.cast)(this.props.cs).handle;
930
+ this.handle = this.props.handle || this.device.handle.createComputePipeline({
931
+ label: this.props.id,
932
+ compute: {
933
+ module: module2,
934
+ entryPoint: this.props.csEntryPoint
935
+ // constants: this.props.csConstants
936
+ },
937
+ layout: "auto"
938
+ });
939
+ }
940
+ /** For internal use in render passes */
941
+ _getBindGroupLayout() {
942
+ return this.handle.getBindGroupLayout(0);
943
+ }
944
+ };
945
+
946
+ // src/adapter/resources/webgpu-render-pass.ts
947
+ var import_core10 = require("@luma.gl/core");
948
+ var WebGPURenderPass = class extends import_core10.RenderPass {
949
+ constructor(device, props = {}) {
950
+ super(device, props);
951
+ /** Active pipeline */
952
+ this.pipeline = null;
953
+ this.device = device;
954
+ const framebuffer = props.framebuffer || device.canvasContext.getCurrentFramebuffer();
955
+ const renderPassDescriptor = this.getRenderPassDescriptor(framebuffer);
956
+ import_core10.log.groupCollapsed(1, `new WebGPURenderPass(${this.id})`)();
957
+ import_core10.log.probe(1, JSON.stringify(renderPassDescriptor, null, 2))();
958
+ import_core10.log.groupEnd(1)();
959
+ this.handle = this.props.handle || device.commandEncoder.beginRenderPass(renderPassDescriptor);
960
+ this.handle.label = this.props.id;
961
+ }
962
+ destroy() {
963
+ }
964
+ end() {
965
+ this.handle.end();
966
+ }
967
+ setPipeline(pipeline) {
968
+ this.pipeline = (0, import_core10.cast)(pipeline);
969
+ this.handle.setPipeline(this.pipeline.handle);
970
+ }
971
+ /** Sets an array of bindings (uniform buffers, samplers, textures, ...) */
972
+ setBindings(bindings) {
973
+ var _a, _b;
974
+ (_a = this.pipeline) == null ? void 0 : _a.setBindings(bindings);
975
+ const bindGroup = (_b = this.pipeline) == null ? void 0 : _b._getBindGroup();
976
+ if (bindGroup) {
977
+ this.handle.setBindGroup(0, bindGroup);
978
+ }
979
+ }
980
+ setIndexBuffer(buffer, indexFormat, offset = 0, size) {
981
+ this.handle.setIndexBuffer((0, import_core10.cast)(buffer).handle, indexFormat, offset, size);
982
+ }
983
+ setVertexBuffer(slot, buffer, offset = 0) {
984
+ this.handle.setVertexBuffer(slot, (0, import_core10.cast)(buffer).handle, offset);
985
+ }
986
+ draw(options) {
987
+ if (options.indexCount) {
988
+ this.handle.drawIndexed(
989
+ options.indexCount,
990
+ options.instanceCount,
991
+ options.firstIndex,
992
+ options.baseVertex,
993
+ options.firstInstance
994
+ );
995
+ } else {
996
+ this.handle.draw(
997
+ options.vertexCount || 0,
998
+ options.instanceCount || 1,
999
+ options.firstIndex,
1000
+ options.firstInstance
1001
+ );
1002
+ }
1003
+ }
1004
+ drawIndirect() {
1005
+ }
1006
+ setParameters(parameters) {
1007
+ const { blendConstant, stencilReference, scissorRect, viewport } = parameters;
1008
+ if (blendConstant) {
1009
+ this.handle.setBlendConstant(blendConstant);
1010
+ }
1011
+ if (stencilReference) {
1012
+ this.handle.setStencilReference(stencilReference);
1013
+ }
1014
+ if (scissorRect) {
1015
+ this.handle.setScissorRect(scissorRect[0], scissorRect[1], scissorRect[2], scissorRect[3]);
1016
+ }
1017
+ if (viewport) {
1018
+ this.handle.setViewport(
1019
+ viewport[0],
1020
+ viewport[1],
1021
+ viewport[2],
1022
+ viewport[3],
1023
+ viewport[4],
1024
+ viewport[5]
1025
+ );
1026
+ }
1027
+ }
1028
+ pushDebugGroup(groupLabel) {
1029
+ this.handle.pushDebugGroup(groupLabel);
1030
+ }
1031
+ popDebugGroup() {
1032
+ this.handle.popDebugGroup();
1033
+ }
1034
+ insertDebugMarker(markerLabel) {
1035
+ this.handle.insertDebugMarker(markerLabel);
1036
+ }
1037
+ // writeTimestamp(querySet: GPUQuerySet, queryIndex: number): void;
1038
+ // beginOcclusionQuery(queryIndex: number): void;
1039
+ // endOcclusionQuery(): void;
1040
+ // beginPipelineStatisticsQuery(querySet: GPUQuerySet, queryIndex: number): void;
1041
+ // endPipelineStatisticsQuery(querySet: GPUQuerySet, queryIndex: number): void;
1042
+ // executeBundles(bundles: Iterable<GPURenderBundle>): void;
1043
+ // INTERNAL
1044
+ /**
1045
+ * Partial render pass descriptor. Used by WebGPURenderPass.
1046
+ * @returns attachments fields of a renderpass descriptor.
1047
+ */
1048
+ getRenderPassDescriptor(framebuffer) {
1049
+ const renderPassDescriptor = {
1050
+ colorAttachments: []
1051
+ };
1052
+ renderPassDescriptor.colorAttachments = framebuffer.colorAttachments.map((colorAttachment) => ({
1053
+ // clear values
1054
+ loadOp: this.props.clearColor !== false ? "clear" : "load",
1055
+ colorClearValue: this.props.clearColor || [0, 0, 0, 0],
1056
+ storeOp: this.props.discard ? "discard" : "store",
1057
+ // ...colorAttachment,
1058
+ view: colorAttachment.handle.createView()
1059
+ }));
1060
+ if (framebuffer.depthStencilAttachment) {
1061
+ renderPassDescriptor.depthStencilAttachment = {
1062
+ view: framebuffer.depthStencilAttachment.handle.createView()
1063
+ };
1064
+ const { depthStencilAttachment } = renderPassDescriptor;
1065
+ if (this.props.depthReadOnly) {
1066
+ depthStencilAttachment.depthReadOnly = true;
1067
+ }
1068
+ depthStencilAttachment.depthClearValue = this.props.clearDepth || 0;
1069
+ const hasDepthAspect = true;
1070
+ if (hasDepthAspect) {
1071
+ depthStencilAttachment.depthLoadOp = this.props.clearDepth !== false ? "clear" : "load";
1072
+ depthStencilAttachment.depthStoreOp = "store";
1073
+ }
1074
+ const hasStencilAspect = false;
1075
+ if (hasStencilAspect) {
1076
+ depthStencilAttachment.stencilLoadOp = this.props.clearStencil !== false ? "clear" : "load";
1077
+ depthStencilAttachment.stencilStoreOp = "store";
1078
+ }
1079
+ }
1080
+ return renderPassDescriptor;
1081
+ }
1082
+ };
1083
+
1084
+ // src/adapter/resources/webgpu-compute-pass.ts
1085
+ var import_core11 = require("@luma.gl/core");
1086
+ var WebGPUComputePass = class extends import_core11.ComputePass {
1087
+ constructor(device, props) {
1088
+ var _a;
1089
+ super(device, props);
1090
+ this._bindGroupLayout = null;
1091
+ this.device = device;
1092
+ this.handle = this.props.handle || ((_a = device.commandEncoder) == null ? void 0 : _a.beginComputePass({
1093
+ label: this.props.id
1094
+ // timestampWrites?: GPUComputePassTimestampWrites;
1095
+ }));
1096
+ }
1097
+ /** @note no WebGPU destroy method, just gc */
1098
+ destroy() {
1099
+ }
1100
+ end() {
1101
+ this.handle.end();
1102
+ }
1103
+ setPipeline(pipeline) {
1104
+ const wgpuPipeline = (0, import_core11.cast)(pipeline);
1105
+ this.handle.setPipeline(wgpuPipeline.handle);
1106
+ this._bindGroupLayout = wgpuPipeline._getBindGroupLayout();
1107
+ }
1108
+ /** Sets an array of bindings (uniform buffers, samplers, textures, ...) */
1109
+ setBindings(bindings) {
1110
+ throw new Error("fix me");
1111
+ }
1112
+ /**
1113
+ * Dispatch work to be performed with the current ComputePipeline.
1114
+ * @param x X dimension of the grid of workgroups to dispatch.
1115
+ * @param y Y dimension of the grid of workgroups to dispatch.
1116
+ * @param z Z dimension of the grid of workgroups to dispatch.
1117
+ */
1118
+ dispatch(x, y, z) {
1119
+ this.handle.dispatchWorkgroups(x, y, z);
1120
+ }
1121
+ /**
1122
+ * Dispatch work to be performed with the current ComputePipeline.
1123
+ * @param indirectBuffer buffer must be a tightly packed block of three 32-bit unsigned integer values (12 bytes total), given in the same order as the arguments for dispatch()
1124
+ * @param indirectOffset
1125
+ */
1126
+ dispatchIndirect(indirectBuffer, indirectOffset = 0) {
1127
+ this.handle.dispatchWorkgroupsIndirect((0, import_core11.cast)(indirectBuffer).handle, indirectOffset);
1128
+ }
1129
+ pushDebugGroup(groupLabel) {
1130
+ this.handle.pushDebugGroup(groupLabel);
1131
+ }
1132
+ popDebugGroup() {
1133
+ this.handle.popDebugGroup();
1134
+ }
1135
+ insertDebugMarker(markerLabel) {
1136
+ this.handle.insertDebugMarker(markerLabel);
1137
+ }
1138
+ // writeTimestamp(querySet: GPUQuerySet, queryIndex: number): void;
1139
+ // beginPipelineStatisticsQuery(querySet: GPUQuerySet, queryIndex: number): void;
1140
+ // endPipelineStatisticsQuery(querySet: GPUQuerySet, queryIndex: number): void;
1141
+ };
1142
+
1143
+ // src/adapter/resources/webgpu-vertex-array.ts
1144
+ var import_core12 = require("@luma.gl/core");
1145
+ var import_env = require("@probe.gl/env");
1146
+ var WebGPUVertexArray = class extends import_core12.VertexArray {
1147
+ // Create a VertexArray
1148
+ constructor(device, props) {
1149
+ super(device, props);
1150
+ this.device = device;
1151
+ }
1152
+ get [Symbol.toStringTag]() {
1153
+ return "WebGPUVertexArray";
1154
+ }
1155
+ /** * Attribute 0 can not be disable on most desktop OpenGL based browsers */
1156
+ static isConstantAttributeZeroSupported(device) {
1157
+ return device.info.type === "webgl2" || (0, import_env.getBrowser)() === "Chrome";
1158
+ }
1159
+ destroy() {
1160
+ }
1161
+ /**
1162
+ * Set an elements buffer, for indexed rendering.
1163
+ * Must be a Buffer bound to buffer with usage bit Buffer.INDEX set.
1164
+ */
1165
+ setIndexBuffer(buffer) {
1166
+ this.indexBuffer = buffer;
1167
+ }
1168
+ /** Set a location in vertex attributes array to a buffer, enables the location, sets divisor */
1169
+ setBuffer(location, buffer) {
1170
+ this.attributes[location] = buffer;
1171
+ }
1172
+ /** Set a location in vertex attributes array to a constant value, disables the location */
1173
+ setConstant(location, value) {
1174
+ import_core12.log.warn(`${this.id} constant attributes not supported on WebGPU`);
1175
+ }
1176
+ bindBeforeRender(renderPass, firstIndex, indexCount) {
1177
+ const webgpuRenderPass = renderPass;
1178
+ const webgpuIndexBuffer = this.indexBuffer;
1179
+ webgpuRenderPass.handle.setIndexBuffer(webgpuIndexBuffer == null ? void 0 : webgpuIndexBuffer.handle, webgpuIndexBuffer == null ? void 0 : webgpuIndexBuffer.indexType);
1180
+ for (let location = 0; location < this.maxVertexAttributes; location++) {
1181
+ const webgpuBuffer = this.attributes[location];
1182
+ webgpuRenderPass.handle.setVertexBuffer(location, webgpuBuffer.handle);
1183
+ }
1184
+ }
1185
+ unbindAfterRender(renderPass) {
1186
+ }
1187
+ };
1188
+
1189
+ // src/adapter/webgpu-canvas-context.ts
1190
+ var import_core14 = require("@luma.gl/core");
1191
+
1192
+ // src/adapter/resources/webgpu-framebuffer.ts
1193
+ var import_core13 = require("@luma.gl/core");
1194
+ var WebGPUFramebuffer = class extends import_core13.Framebuffer {
1195
+ constructor(device, props) {
1196
+ super(device, props);
1197
+ this.device = device;
1198
+ this.autoCreateAttachmentTextures();
1199
+ }
1200
+ };
1201
+
1202
+ // src/adapter/webgpu-canvas-context.ts
1203
+ var WebGPUCanvasContext = class extends import_core14.CanvasContext {
1204
+ constructor(device, adapter, props) {
1205
+ super(props);
1206
+ /** Format of returned textures: "bgra8unorm", "rgba8unorm", "rgba16float". */
1207
+ this.format = navigator.gpu.getPreferredCanvasFormat();
1208
+ /** Default stencil format for depth textures */
1209
+ this.depthStencilFormat = "depth24plus";
1210
+ this.depthStencilAttachment = null;
1211
+ this.device = device;
1212
+ this.width = -1;
1213
+ this.height = -1;
1214
+ this._setAutoCreatedCanvasId(`${this.device.id}-canvas`);
1215
+ this.gpuCanvasContext = this.canvas.getContext("webgpu");
1216
+ this.format = "bgra8unorm";
1217
+ }
1218
+ /** Destroy any textures produced while configured and remove the context configuration. */
1219
+ destroy() {
1220
+ this.gpuCanvasContext.unconfigure();
1221
+ }
1222
+ getCurrentTexture() {
1223
+ return this.device._createTexture({
1224
+ id: "default-render-target",
1225
+ handle: this.gpuCanvasContext.getCurrentTexture()
1226
+ });
1227
+ }
1228
+ /** Update framebuffer with properly resized "swap chain" texture views */
1229
+ getCurrentFramebuffer() {
1230
+ this.update();
1231
+ const currentColorAttachment = this.getCurrentTexture();
1232
+ this.width = currentColorAttachment.width;
1233
+ this.height = currentColorAttachment.height;
1234
+ this._createDepthStencilAttachment();
1235
+ return new WebGPUFramebuffer(this.device, {
1236
+ colorAttachments: [currentColorAttachment],
1237
+ depthStencilAttachment: this.depthStencilAttachment
1238
+ });
1239
+ }
1240
+ /** Resizes and updates render targets if necessary */
1241
+ update() {
1242
+ const [width, height] = this.getPixelSize();
1243
+ const sizeChanged = width !== this.width || height !== this.height;
1244
+ if (sizeChanged) {
1245
+ this.width = width;
1246
+ this.height = height;
1247
+ if (this.depthStencilAttachment) {
1248
+ this.depthStencilAttachment.destroy();
1249
+ this.depthStencilAttachment = null;
1250
+ }
1251
+ this.gpuCanvasContext.configure({
1252
+ device: this.device.handle,
1253
+ format: getWebGPUTextureFormat(this.format),
1254
+ // Can be used to define e.g. -srgb views
1255
+ // viewFormats: [...]
1256
+ colorSpace: this.props.colorSpace,
1257
+ alphaMode: this.props.alphaMode
1258
+ });
1259
+ import_core14.log.log(1, `Resized to ${this.width}x${this.height}px`)();
1260
+ }
1261
+ }
1262
+ resize(options) {
1263
+ this.update();
1264
+ }
1265
+ /** We build render targets on demand (i.e. not when size changes but when about to render) */
1266
+ _createDepthStencilAttachment() {
1267
+ if (!this.depthStencilAttachment) {
1268
+ this.depthStencilAttachment = this.device.createTexture({
1269
+ id: "depth-stencil-target",
1270
+ format: this.depthStencilFormat,
1271
+ width: this.width,
1272
+ height: this.height,
1273
+ usage: GPUTextureUsage.RENDER_ATTACHMENT
1274
+ });
1275
+ }
1276
+ return this.depthStencilAttachment;
1277
+ }
1278
+ };
1279
+
1280
+ // src/adapter/webgpu-device.ts
1281
+ var _WebGPUDevice = class extends import_core15.Device {
1282
+ constructor(device, adapter, props) {
1283
+ super(__spreadProps(__spreadValues({}, props), { id: props.id || (0, import_core15.uid)("webgpu-device") }));
1284
+ this.canvasContext = null;
1285
+ this.commandEncoder = null;
1286
+ this.renderPass = null;
1287
+ this._isLost = false;
1288
+ this.handle = device;
1289
+ this.adapter = adapter;
1290
+ this._info = {
1291
+ type: "webgpu",
1292
+ vendor: this.adapter.__brand,
1293
+ renderer: "",
1294
+ version: "",
1295
+ gpu: "unknown",
1296
+ // 'nvidia' | 'amd' | 'intel' | 'apple' | 'unknown',
1297
+ shadingLanguages: ["wgsl"],
1298
+ shadingLanguageVersions: {
1299
+ glsl: "450",
1300
+ wgsl: "100"
1301
+ },
1302
+ vendorMasked: "",
1303
+ rendererMasked: ""
1304
+ };
1305
+ this.lost = new Promise((resolve) => __async(this, null, function* () {
1306
+ const lostInfo = yield this.handle.lost;
1307
+ this._isLost = true;
1308
+ resolve({ reason: "destroyed", message: lostInfo.message });
1309
+ }));
1310
+ this.canvasContext = new WebGPUCanvasContext(this, this.adapter, { canvas: props.canvas });
1311
+ this.features = this._getFeatures();
1312
+ }
1313
+ /** Check if WebGPU is available */
1314
+ static isSupported() {
1315
+ return Boolean(typeof navigator !== "undefined" && navigator.gpu);
1316
+ }
1317
+ static create(props) {
1318
+ return __async(this, null, function* () {
1319
+ if (!navigator.gpu) {
1320
+ throw new Error(
1321
+ "WebGPU not available. Open in Chrome Canary and turn on chrome://flags/#enable-unsafe-webgpu"
1322
+ );
1323
+ }
1324
+ import_core15.log.groupCollapsed(1, "WebGPUDevice created")();
1325
+ const adapter = yield navigator.gpu.requestAdapter({
1326
+ powerPreference: "high-performance"
1327
+ // forceSoftware: false
1328
+ });
1329
+ if (!adapter) {
1330
+ throw new Error("Failed to request WebGPU adapter");
1331
+ }
1332
+ const adapterInfo = yield adapter.requestAdapterInfo();
1333
+ import_core15.log.probe(1, "Adapter available", adapterInfo)();
1334
+ const gpuDevice = yield adapter.requestDevice({
1335
+ requiredFeatures: adapter.features
1336
+ // TODO ensure we obtain best limits
1337
+ // requiredLimits: adapter.limits
1338
+ });
1339
+ import_core15.log.probe(1, "GPUDevice available")();
1340
+ if (typeof props.canvas === "string") {
1341
+ yield import_core15.CanvasContext.pageLoaded;
1342
+ import_core15.log.probe(1, "DOM is loaded")();
1343
+ }
1344
+ const device = new _WebGPUDevice(gpuDevice, adapter, props);
1345
+ import_core15.log.probe(1, "Device created", device.info)();
1346
+ import_core15.log.table(1, device.info)();
1347
+ import_core15.log.groupEnd(1)();
1348
+ return device;
1349
+ });
1350
+ }
1351
+ // TODO
1352
+ // Load the glslang module now so that it is available synchronously when compiling shaders
1353
+ // const {glsl = true} = props;
1354
+ // this.glslang = glsl && await loadGlslangModule();
1355
+ destroy() {
1356
+ this.handle.destroy();
1357
+ }
1358
+ get info() {
1359
+ return this._info;
1360
+ }
1361
+ get limits() {
1362
+ return this.handle.limits;
1363
+ }
1364
+ isTextureFormatSupported(format) {
1365
+ return !format.includes("webgl");
1366
+ }
1367
+ /** @todo implement proper check? */
1368
+ isTextureFormatFilterable(format) {
1369
+ return this.isTextureFormatSupported(format);
1370
+ }
1371
+ /** @todo implement proper check? */
1372
+ isTextureFormatRenderable(format) {
1373
+ return this.isTextureFormatSupported(format);
1374
+ }
1375
+ get isLost() {
1376
+ return this._isLost;
1377
+ }
1378
+ createBuffer(props) {
1379
+ const newProps = this._getBufferProps(props);
1380
+ return new WebGPUBuffer(this, newProps);
1381
+ }
1382
+ _createTexture(props) {
1383
+ return new WebGPUTexture(this, props);
1384
+ }
1385
+ createExternalTexture(props) {
1386
+ return new WebGPUExternalTexture(this, props);
1387
+ }
1388
+ createShader(props) {
1389
+ return new WebGPUShader(this, props);
1390
+ }
1391
+ createSampler(props) {
1392
+ return new WebGPUSampler(this, props);
1393
+ }
1394
+ createRenderPipeline(props) {
1395
+ return new WebGPURenderPipeline(this, props);
1396
+ }
1397
+ createFramebuffer(props) {
1398
+ throw new Error("Not implemented");
1399
+ }
1400
+ createComputePipeline(props) {
1401
+ return new WebGPUComputePipeline(this, props);
1402
+ }
1403
+ createVertexArray(props) {
1404
+ return new WebGPUVertexArray(this, props);
1405
+ }
1406
+ // WebGPU specifics
1407
+ /**
1408
+ * Allows a render pass to begin against a canvas context
1409
+ * @todo need to support a "Framebuffer" equivalent (aka preconfigured RenderPassDescriptors?).
1410
+ */
1411
+ beginRenderPass(props) {
1412
+ this.commandEncoder = this.commandEncoder || this.handle.createCommandEncoder();
1413
+ return new WebGPURenderPass(this, props);
1414
+ }
1415
+ beginComputePass(props) {
1416
+ this.commandEncoder = this.commandEncoder || this.handle.createCommandEncoder();
1417
+ return new WebGPUComputePass(this, props);
1418
+ }
1419
+ // createCommandEncoder(props: CommandEncoderProps): WebGPUCommandEncoder {
1420
+ // return new WebGPUCommandEncoder(this, props);
1421
+ // }
1422
+ createCanvasContext(props) {
1423
+ return new WebGPUCanvasContext(this, this.adapter, props);
1424
+ }
1425
+ /**
1426
+ * Gets default renderpass encoder.
1427
+ * Creates a new encoder against default canvasContext if not already created
1428
+ * @note Called internally by Model.
1429
+ * @deprecated Create explicit pass with device.beginRenderPass
1430
+ */
1431
+ getDefaultRenderPass() {
1432
+ throw new Error("a");
1433
+ }
1434
+ submit() {
1435
+ var _a;
1436
+ const commandBuffer = (_a = this.commandEncoder) == null ? void 0 : _a.finish();
1437
+ if (commandBuffer) {
1438
+ this.handle.queue.submit([commandBuffer]);
1439
+ }
1440
+ this.commandEncoder = null;
1441
+ }
1442
+ _getFeatures() {
1443
+ const features = new Set(this.handle.features);
1444
+ if (features.has("depth-clamping")) {
1445
+ features.delete("depth-clamping");
1446
+ features.add("depth-clip-control");
1447
+ }
1448
+ if (features.has("texture-compression-bc")) {
1449
+ features.add("texture-compression-bc5-webgl");
1450
+ }
1451
+ features.add("webgpu");
1452
+ features.add("timer-query-webgl");
1453
+ features.add("vertex-array-object-webgl1");
1454
+ features.add("instanced-rendering-webgl1");
1455
+ features.add("multiple-render-targets-webgl1");
1456
+ features.add("index-uint32-webgl1");
1457
+ features.add("blend-minmax-webgl1");
1458
+ features.add("texture-blend-float-webgl1");
1459
+ features.add("texture-formats-srgb-webgl1");
1460
+ features.add("texture-formats-depth-webgl1");
1461
+ features.add("texture-formats-float32-webgl1");
1462
+ features.add("texture-formats-float16-webgl1");
1463
+ features.add("texture-filter-linear-float32-webgl");
1464
+ features.add("texture-filter-linear-float16-webgl");
1465
+ features.add("texture-filter-anisotropic-webgl");
1466
+ features.add("texture-renderable-rgba32float-webgl");
1467
+ features.add("texture-renderable-float32-webgl");
1468
+ features.add("texture-renderable-float16-webgl");
1469
+ features.add("glsl-frag-data");
1470
+ features.add("glsl-frag-depth");
1471
+ features.add("glsl-derivatives");
1472
+ features.add("glsl-texture-lod");
1473
+ return features;
1474
+ }
1475
+ copyExternalImageToTexture(options) {
1476
+ var _a;
1477
+ const {
1478
+ source,
1479
+ sourceX = 0,
1480
+ sourceY = 0,
1481
+ texture,
1482
+ mipLevel = 0,
1483
+ aspect = "all",
1484
+ colorSpace = "display-p3",
1485
+ premultipliedAlpha = false,
1486
+ // destinationX,
1487
+ // destinationY,
1488
+ // desitnationZ,
1489
+ width = texture.width,
1490
+ height = texture.height,
1491
+ depth = 1
1492
+ } = options;
1493
+ const webGpuTexture = texture;
1494
+ (_a = this.handle) == null ? void 0 : _a.queue.copyExternalImageToTexture(
1495
+ // source: GPUImageCopyExternalImage
1496
+ {
1497
+ source,
1498
+ origin: [sourceX, sourceY]
1499
+ },
1500
+ // destination: GPUImageCopyTextureTagged
1501
+ {
1502
+ texture: webGpuTexture.handle,
1503
+ origin: [0, 0, 0],
1504
+ // [x, y, z],
1505
+ mipLevel,
1506
+ aspect,
1507
+ colorSpace,
1508
+ premultipliedAlpha
1509
+ },
1510
+ // copySize: GPUExtent3D
1511
+ [
1512
+ width,
1513
+ height,
1514
+ depth
1515
+ ]
1516
+ );
1517
+ }
1518
+ };
1519
+ var WebGPUDevice = _WebGPUDevice;
1520
+ WebGPUDevice.type = "webgpu";