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