@luma.gl/webgpu 9.0.0-alpha.5 → 9.0.0-alpha.50

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