@luma.gl/webgpu 9.0.0-alpha.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/README.md +1 -0
- package/dist/adapter/helpers/accessor-to-format.d.ts +1 -0
- package/dist/adapter/helpers/accessor-to-format.d.ts.map +1 -0
- package/dist/adapter/helpers/accessor-to-format.js +2 -0
- package/dist/adapter/helpers/accessor-to-format.js.map +1 -0
- package/dist/adapter/helpers/convert-texture-format.d.ts +5 -0
- package/dist/adapter/helpers/convert-texture-format.d.ts.map +1 -0
- package/dist/adapter/helpers/convert-texture-format.js +8 -0
- package/dist/adapter/helpers/convert-texture-format.js.map +1 -0
- package/dist/adapter/helpers/generate-mipmaps.d.ts +10 -0
- package/dist/adapter/helpers/generate-mipmaps.d.ts.map +1 -0
- package/dist/adapter/helpers/generate-mipmaps.js +95 -0
- package/dist/adapter/helpers/generate-mipmaps.js.map +1 -0
- package/dist/adapter/helpers/get-bind-group.d.ts +13 -0
- package/dist/adapter/helpers/get-bind-group.d.ts.map +1 -0
- package/dist/adapter/helpers/get-bind-group.js +60 -0
- package/dist/adapter/helpers/get-bind-group.js.map +1 -0
- package/dist/adapter/helpers/get-vertex-buffer-layout.d.ts +12 -0
- package/dist/adapter/helpers/get-vertex-buffer-layout.d.ts.map +1 -0
- package/dist/adapter/helpers/get-vertex-buffer-layout.js +98 -0
- package/dist/adapter/helpers/get-vertex-buffer-layout.js.map +1 -0
- package/dist/adapter/helpers/webgpu-parameters.d.ts +9 -0
- package/dist/adapter/helpers/webgpu-parameters.d.ts.map +1 -0
- package/dist/adapter/helpers/webgpu-parameters.js +134 -0
- package/dist/adapter/helpers/webgpu-parameters.js.map +1 -0
- package/dist/adapter/resources/webgpu-buffer.d.ts +18 -0
- package/dist/adapter/resources/webgpu-buffer.d.ts.map +1 -0
- package/dist/adapter/resources/webgpu-buffer.js +84 -0
- package/dist/adapter/resources/webgpu-buffer.js.map +1 -0
- package/dist/adapter/resources/webgpu-command-encoder.d.ts +45 -0
- package/dist/adapter/resources/webgpu-command-encoder.d.ts.map +1 -0
- package/dist/adapter/resources/webgpu-command-encoder.js +66 -0
- package/dist/adapter/resources/webgpu-command-encoder.js.map +1 -0
- package/dist/adapter/resources/webgpu-compute-pass.d.ts +32 -0
- package/dist/adapter/resources/webgpu-compute-pass.d.ts.map +1 -0
- package/dist/adapter/resources/webgpu-compute-pass.js +56 -0
- package/dist/adapter/resources/webgpu-compute-pass.js.map +1 -0
- package/dist/adapter/resources/webgpu-compute-pipeline.d.ts +12 -0
- package/dist/adapter/resources/webgpu-compute-pipeline.d.ts.map +1 -0
- package/dist/adapter/resources/webgpu-compute-pipeline.js +27 -0
- package/dist/adapter/resources/webgpu-compute-pipeline.js.map +1 -0
- package/dist/adapter/resources/webgpu-external-texture.d.ts +18 -0
- package/dist/adapter/resources/webgpu-external-texture.d.ts.map +1 -0
- package/dist/adapter/resources/webgpu-external-texture.js +30 -0
- package/dist/adapter/resources/webgpu-external-texture.js.map +1 -0
- package/dist/adapter/resources/webgpu-framebuffer.d.ts +29 -0
- package/dist/adapter/resources/webgpu-framebuffer.d.ts.map +1 -0
- package/dist/adapter/resources/webgpu-framebuffer.js +111 -0
- package/dist/adapter/resources/webgpu-framebuffer.js.map +1 -0
- package/dist/adapter/resources/webgpu-query.d.ts +1 -0
- package/dist/adapter/resources/webgpu-query.d.ts.map +1 -0
- package/dist/adapter/resources/webgpu-query.js +2 -0
- package/dist/adapter/resources/webgpu-query.js.map +1 -0
- package/dist/adapter/resources/webgpu-render-pass.d.ts +34 -0
- package/dist/adapter/resources/webgpu-render-pass.d.ts.map +1 -0
- package/dist/adapter/resources/webgpu-render-pass.js +92 -0
- package/dist/adapter/resources/webgpu-render-pass.js.map +1 -0
- package/dist/adapter/resources/webgpu-render-pipeline.d.ts +41 -0
- package/dist/adapter/resources/webgpu-render-pipeline.d.ts.map +1 -0
- package/dist/adapter/resources/webgpu-render-pipeline.js +148 -0
- package/dist/adapter/resources/webgpu-render-pipeline.js.map +1 -0
- package/dist/adapter/resources/webgpu-sampler.d.ts +16 -0
- package/dist/adapter/resources/webgpu-sampler.d.ts.map +1 -0
- package/dist/adapter/resources/webgpu-sampler.js +19 -0
- package/dist/adapter/resources/webgpu-sampler.js.map +1 -0
- package/dist/adapter/resources/webgpu-shader.d.ts +21 -0
- package/dist/adapter/resources/webgpu-shader.d.ts.map +1 -0
- package/dist/adapter/resources/webgpu-shader.js +64 -0
- package/dist/adapter/resources/webgpu-shader.js.map +1 -0
- package/dist/adapter/resources/webgpu-texture.d.ts +39 -0
- package/dist/adapter/resources/webgpu-texture.d.ts.map +1 -0
- package/dist/adapter/resources/webgpu-texture.js +111 -0
- package/dist/adapter/resources/webgpu-texture.js.map +1 -0
- package/dist/adapter/webgpu-canvas-context.d.ts +32 -0
- package/dist/adapter/webgpu-canvas-context.d.ts.map +1 -0
- package/dist/adapter/webgpu-canvas-context.js +95 -0
- package/dist/adapter/webgpu-canvas-context.js.map +1 -0
- package/dist/adapter/webgpu-device.d.ts +67 -0
- package/dist/adapter/webgpu-device.d.ts.map +1 -0
- package/dist/adapter/webgpu-device.js +225 -0
- package/dist/adapter/webgpu-device.js.map +1 -0
- package/dist/adapter/webgpu-types.d.ts +1 -0
- package/dist/adapter/webgpu-types.d.ts.map +1 -0
- package/dist/adapter/webgpu-types.js +2 -0
- package/dist/adapter/webgpu-types.js.map +1 -0
- package/dist/bundle.d.ts +2 -0
- package/dist/bundle.d.ts.map +1 -0
- package/dist/bundle.js +5 -0
- package/dist/bundle.js.map +1 -0
- package/dist/glsl/glsllang.d.ts +3 -0
- package/dist/glsl/glsllang.d.ts.map +1 -0
- package/dist/glsl/glsllang.js +10 -0
- package/dist/glsl/glsllang.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/init.d.ts +2 -0
- package/dist/init.d.ts.map +1 -0
- package/dist/init.js +4 -0
- package/dist/init.js.map +1 -0
- package/package.json +36 -0
- package/src/adapter/helpers/accessor-to-format.ts +101 -0
- package/src/adapter/helpers/convert-texture-format.ts +10 -0
- package/src/adapter/helpers/generate-mipmaps.ts +107 -0
- package/src/adapter/helpers/get-bind-group.ts +82 -0
- package/src/adapter/helpers/get-vertex-buffer-layout.ts +123 -0
- package/src/adapter/helpers/webgpu-parameters.ts +226 -0
- package/src/adapter/resources/webgpu-buffer.ts +96 -0
- package/src/adapter/resources/webgpu-command-encoder.ts +111 -0
- package/src/adapter/resources/webgpu-compute-pass.ts +74 -0
- package/src/adapter/resources/webgpu-compute-pipeline.ts +34 -0
- package/src/adapter/resources/webgpu-external-texture.ts +37 -0
- package/src/adapter/resources/webgpu-framebuffer.ts +120 -0
- package/src/adapter/resources/webgpu-query.ts +43 -0
- package/src/adapter/resources/webgpu-render-pass.ts +128 -0
- package/src/adapter/resources/webgpu-render-pipeline.ts +231 -0
- package/src/adapter/resources/webgpu-sampler.ts +25 -0
- package/src/adapter/resources/webgpu-shader.ts +72 -0
- package/src/adapter/resources/webgpu-texture.ts +243 -0
- package/src/adapter/webgpu-canvas-context.ts +102 -0
- package/src/adapter/webgpu-device.ts +282 -0
- package/src/adapter/webgpu-types.ts +0 -0
- package/src/bundle.ts +4 -0
- package/src/glsl/glsllang.ts +14 -0
- package/src/index.ts +13 -0
- package/src/init.ts +4 -0
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
// luma.gl, MIT license
|
|
2
|
+
import {Texture, TextureProps, Sampler, SamplerProps, assert} from '@luma.gl/api';
|
|
3
|
+
import {getWebGPUTextureFormat} from '../helpers/convert-texture-format';
|
|
4
|
+
import type WebGPUDevice from '../webgpu-device';
|
|
5
|
+
import WebGPUSampler from './webgpu-sampler';
|
|
6
|
+
|
|
7
|
+
const BASE_DIMENSIONS: Record<string, '1d' | '2d' | '3d'> = {
|
|
8
|
+
'1d': '1d',
|
|
9
|
+
'2d': '2d',
|
|
10
|
+
'2d-array': '2d',
|
|
11
|
+
'cube': '2d',
|
|
12
|
+
'cube-array': '2d',
|
|
13
|
+
'3d': '3d'
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export default class WebGPUTexture extends Texture {
|
|
17
|
+
readonly device: WebGPUDevice;
|
|
18
|
+
readonly handle: GPUTexture;
|
|
19
|
+
readonly view: GPUTextureView;
|
|
20
|
+
sampler: WebGPUSampler;
|
|
21
|
+
|
|
22
|
+
// static async createFromImageURL(src, usage = 0) {
|
|
23
|
+
// const img = document.createElement('img');
|
|
24
|
+
// img.src = src;
|
|
25
|
+
// await img.decode();
|
|
26
|
+
// return WebGPUTexture(img, usage);
|
|
27
|
+
// }
|
|
28
|
+
|
|
29
|
+
constructor(device: WebGPUDevice, props: TextureProps) {
|
|
30
|
+
super(device, props);
|
|
31
|
+
|
|
32
|
+
if (typeof this.props.format === 'number') {
|
|
33
|
+
throw new Error('number format');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
this.device = device;
|
|
37
|
+
this.handle = this.props.handle || this.createHandle();
|
|
38
|
+
|
|
39
|
+
if (this.props.data) {
|
|
40
|
+
this.setData({data: this.props.data} );
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Create a default sampler. This mimics the WebGL1 API where sampler props are stored on the texture
|
|
44
|
+
this.setSampler(props.sampler);
|
|
45
|
+
|
|
46
|
+
// TODO - To support texture arrays we need to create custom views...
|
|
47
|
+
// But we are not ready to expose TextureViews to the public API.
|
|
48
|
+
this.view = this.handle.createView({
|
|
49
|
+
// format: this.props.format,
|
|
50
|
+
// dimension: this.props.dimension,
|
|
51
|
+
// aspect = "all";
|
|
52
|
+
// baseMipLevel: 0;
|
|
53
|
+
// mipLevelCount;
|
|
54
|
+
// baseArrayLayer = 0;
|
|
55
|
+
// arrayLayerCount;
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
protected createHandle(): GPUTexture {
|
|
60
|
+
if (typeof this.props.format === 'number') {
|
|
61
|
+
throw new Error('number format');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Deduce size from data - TODO this is a hack
|
|
65
|
+
// @ts-expect-error
|
|
66
|
+
const width = this.props.width || this.props.data?.width || 1;
|
|
67
|
+
// @ts-expect-error
|
|
68
|
+
const height = this.props.height || this.props.data?.height || 1;
|
|
69
|
+
|
|
70
|
+
return this.device.handle.createTexture({
|
|
71
|
+
size: {
|
|
72
|
+
width,
|
|
73
|
+
height,
|
|
74
|
+
depthOrArrayLayers: this.props.depth
|
|
75
|
+
},
|
|
76
|
+
dimension: BASE_DIMENSIONS[this.props.dimension],
|
|
77
|
+
format: getWebGPUTextureFormat(this.props.format),
|
|
78
|
+
usage: this.props.usage,
|
|
79
|
+
mipLevelCount: this.props.mipLevels,
|
|
80
|
+
sampleCount: this.props.samples
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
destroy(): void {
|
|
85
|
+
this.handle.destroy();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Set default sampler
|
|
90
|
+
* Accept a sampler instance or set of props;
|
|
91
|
+
*/
|
|
92
|
+
setSampler(sampler: Sampler | SamplerProps): this {
|
|
93
|
+
this.sampler = sampler instanceof WebGPUSampler ? sampler : new WebGPUSampler(this.device, sampler);
|
|
94
|
+
return this;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
setData(options: {
|
|
98
|
+
data: any;
|
|
99
|
+
}) {
|
|
100
|
+
return this.setImage({source: options.data});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Set image */
|
|
104
|
+
setImage(options: {
|
|
105
|
+
source: ImageBitmap | HTMLCanvasElement | OffscreenCanvas;
|
|
106
|
+
width?: number;
|
|
107
|
+
height?: number;
|
|
108
|
+
depth?: number;
|
|
109
|
+
sourceX?: number;
|
|
110
|
+
sourceY?: number;
|
|
111
|
+
mipLevel?: number;
|
|
112
|
+
x?: number;
|
|
113
|
+
y?: number;
|
|
114
|
+
z?: number;
|
|
115
|
+
aspect?: 'all' | 'stencil-only' | 'depth-only';
|
|
116
|
+
colorSpace?: 'srgb';
|
|
117
|
+
premultipliedAlpha?: boolean;
|
|
118
|
+
}): this {
|
|
119
|
+
const {
|
|
120
|
+
source,
|
|
121
|
+
width = options.source.width,
|
|
122
|
+
height = options.source.height,
|
|
123
|
+
depth = 1,
|
|
124
|
+
sourceX = 0,
|
|
125
|
+
sourceY = 0,
|
|
126
|
+
mipLevel = 0,
|
|
127
|
+
x = 0,
|
|
128
|
+
y = 0,
|
|
129
|
+
z = 0,
|
|
130
|
+
aspect = 'all',
|
|
131
|
+
colorSpace = 'srgb',
|
|
132
|
+
premultipliedAlpha = false
|
|
133
|
+
} = options;
|
|
134
|
+
|
|
135
|
+
// TODO - max out width
|
|
136
|
+
|
|
137
|
+
this.device.handle.queue.copyExternalImageToTexture(
|
|
138
|
+
// source: GPUImageCopyExternalImage
|
|
139
|
+
{
|
|
140
|
+
source,
|
|
141
|
+
origin: [sourceX, sourceY]
|
|
142
|
+
},
|
|
143
|
+
// destination: GPUImageCopyTextureTagged
|
|
144
|
+
{
|
|
145
|
+
texture: this.handle,
|
|
146
|
+
origin: [x, y, z],
|
|
147
|
+
mipLevel,
|
|
148
|
+
aspect,
|
|
149
|
+
colorSpace,
|
|
150
|
+
premultipliedAlpha
|
|
151
|
+
},
|
|
152
|
+
// copySize: GPUExtent3D
|
|
153
|
+
[
|
|
154
|
+
width,
|
|
155
|
+
height,
|
|
156
|
+
depth
|
|
157
|
+
]
|
|
158
|
+
);
|
|
159
|
+
return this;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/*
|
|
163
|
+
async readPixels() {
|
|
164
|
+
const readbackBuffer = device.createBuffer({
|
|
165
|
+
usage: Buffer.COPY_DST | Buffer.MAP_READ,
|
|
166
|
+
size: 4 * textureWidth * textureHeight,
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
// Copy data from the texture to the buffer.
|
|
170
|
+
const encoder = device.createCommandEncoder();
|
|
171
|
+
encoder.copyTextureToBuffer(
|
|
172
|
+
{ texture },
|
|
173
|
+
{ buffer, rowPitch: textureWidth * 4 },
|
|
174
|
+
[textureWidth, textureHeight],
|
|
175
|
+
);
|
|
176
|
+
device.submit([encoder.finish()]);
|
|
177
|
+
|
|
178
|
+
// Get the data on the CPU.
|
|
179
|
+
await buffer.mapAsync(GPUMapMode.READ);
|
|
180
|
+
saveScreenshot(buffer.getMappedRange());
|
|
181
|
+
buffer.unmap();
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
setImageData(imageData, usage): this {
|
|
185
|
+
let data = null;
|
|
186
|
+
|
|
187
|
+
const bytesPerRow = Math.ceil((img.width * 4) / 256) * 256;
|
|
188
|
+
if (bytesPerRow == img.width * 4) {
|
|
189
|
+
data = imageData.data;
|
|
190
|
+
} else {
|
|
191
|
+
data = new Uint8Array(bytesPerRow * img.height);
|
|
192
|
+
let imagePixelIndex = 0;
|
|
193
|
+
for (let y = 0; y < img.height; ++y) {
|
|
194
|
+
for (let x = 0; x < img.width; ++x) {
|
|
195
|
+
const i = x * 4 + y * bytesPerRow;
|
|
196
|
+
data[i] = imageData.data[imagePixelIndex];
|
|
197
|
+
data[i + 1] = imageData.data[imagePixelIndex + 1];
|
|
198
|
+
data[i + 2] = imageData.data[imagePixelIndex + 2];
|
|
199
|
+
data[i + 3] = imageData.data[imagePixelIndex + 3];
|
|
200
|
+
imagePixelIndex += 4;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return this;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
setData(data): this {
|
|
208
|
+
const textureDataBuffer = this.device.handle.createBuffer({
|
|
209
|
+
size: data.byteLength,
|
|
210
|
+
usage: Buffer.COPY_DST | Buffer.COPY_SRC,
|
|
211
|
+
mappedAtCreation: true
|
|
212
|
+
});
|
|
213
|
+
new Uint8Array(textureDataBuffer.getMappedRange()).set(data);
|
|
214
|
+
textureDataBuffer.unmap();
|
|
215
|
+
|
|
216
|
+
this.setBuffer(textureDataBuffer);
|
|
217
|
+
|
|
218
|
+
textureDataBuffer.destroy();
|
|
219
|
+
return this;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
setBuffer(textureDataBuffer, {bytesPerRow}): this {
|
|
223
|
+
const commandEncoder = this.device.handle.createCommandEncoder();
|
|
224
|
+
commandEncoder.copyBufferToTexture(
|
|
225
|
+
{
|
|
226
|
+
buffer: textureDataBuffer,
|
|
227
|
+
bytesPerRow
|
|
228
|
+
},
|
|
229
|
+
{
|
|
230
|
+
texture: this.handle
|
|
231
|
+
},
|
|
232
|
+
{
|
|
233
|
+
width,
|
|
234
|
+
height,
|
|
235
|
+
depth
|
|
236
|
+
}
|
|
237
|
+
);
|
|
238
|
+
|
|
239
|
+
this.device.handle.defaultQueue.submit([commandEncoder.finish()]);
|
|
240
|
+
return this;
|
|
241
|
+
}
|
|
242
|
+
*/
|
|
243
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import type {Texture, TextureFormat, CanvasContextProps} from '@luma.gl/api';
|
|
2
|
+
import {CanvasContext, log} from '@luma.gl/api';
|
|
3
|
+
import {getWebGPUTextureFormat} from './helpers/convert-texture-format';
|
|
4
|
+
import WebGPUDevice from './webgpu-device';
|
|
5
|
+
import WEBGPUFramebuffer from './resources/webgpu-framebuffer';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Holds a WebGPU Canvas Context which handles resizing etc
|
|
9
|
+
*/
|
|
10
|
+
export default class WebGPUCanvasContext extends CanvasContext {
|
|
11
|
+
readonly device: WebGPUDevice;
|
|
12
|
+
readonly gpuCanvasContext: GPUCanvasContext;
|
|
13
|
+
readonly format: TextureFormat;
|
|
14
|
+
width: number = -1;
|
|
15
|
+
height: number = -1;
|
|
16
|
+
depthStencilFormat: TextureFormat = 'depth24plus';
|
|
17
|
+
sampleCount: number = 1;
|
|
18
|
+
|
|
19
|
+
private depthStencilAttachment: Texture;
|
|
20
|
+
|
|
21
|
+
constructor(device: WebGPUDevice, adapter: GPUAdapter, props: CanvasContextProps) {
|
|
22
|
+
super(props);
|
|
23
|
+
this.device = device;
|
|
24
|
+
this.gpuCanvasContext = this.canvas.getContext('webgpu') as GPUCanvasContext;
|
|
25
|
+
this.format = this.gpuCanvasContext.getPreferredFormat(adapter);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
destroy() {
|
|
29
|
+
this.gpuCanvasContext.unconfigure();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Update framebuffer with properly resized "swap chain" texture views */
|
|
33
|
+
getCurrentFramebuffer(): WEBGPUFramebuffer {
|
|
34
|
+
// Ensure the canvas context size is updated
|
|
35
|
+
this.update();
|
|
36
|
+
|
|
37
|
+
// Wrap the current canvas context texture in a luma.gl texture
|
|
38
|
+
const currentColorAttachment = this.device.createTexture({
|
|
39
|
+
id: 'default-render-target',
|
|
40
|
+
handle: this.gpuCanvasContext.getCurrentTexture(),
|
|
41
|
+
format: this.format,
|
|
42
|
+
width: this.width,
|
|
43
|
+
height: this.height
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// Resize the depth stencil attachment
|
|
47
|
+
this._createDepthStencilAttachment();
|
|
48
|
+
|
|
49
|
+
return new WEBGPUFramebuffer(this.device, {
|
|
50
|
+
colorAttachments: [currentColorAttachment],
|
|
51
|
+
depthStencilAttachment: this.depthStencilAttachment
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Resizes and updates render targets if necessary */
|
|
56
|
+
update() {
|
|
57
|
+
const [width, height] = this.getPixelSize();
|
|
58
|
+
|
|
59
|
+
const sizeChanged = width !== this.width || height !== this.height;
|
|
60
|
+
|
|
61
|
+
if (sizeChanged) {
|
|
62
|
+
this.width = width;
|
|
63
|
+
this.height = height;
|
|
64
|
+
|
|
65
|
+
if (this.depthStencilAttachment) {
|
|
66
|
+
this.depthStencilAttachment.destroy();
|
|
67
|
+
this.depthStencilAttachment = undefined;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Reconfigure the canvas size.
|
|
71
|
+
// https://www.w3.org/TR/webgpu/#canvas-configuration
|
|
72
|
+
this.gpuCanvasContext.configure({
|
|
73
|
+
device: this.device.handle,
|
|
74
|
+
format: getWebGPUTextureFormat(this.format),
|
|
75
|
+
size: [this.width, this.height],
|
|
76
|
+
colorSpace: this.props.colorSpace,
|
|
77
|
+
compositingAlphaMode: this.props.compositingAlphaMode
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
log.log(1, `Resized to ${this.width}x${this.height}px`)();
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
resize(options?: {width?: number; height?: number; useDevicePixels?: boolean | number}): void {
|
|
86
|
+
this.update();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** We build render targets on demand (i.e. not when size changes but when about to render) */
|
|
90
|
+
_createDepthStencilAttachment() {
|
|
91
|
+
if (!this.depthStencilAttachment) {
|
|
92
|
+
this.depthStencilAttachment = this.device.createTexture({
|
|
93
|
+
id: 'depth-stencil-target',
|
|
94
|
+
format: this.depthStencilFormat,
|
|
95
|
+
width: this.width,
|
|
96
|
+
height: this.height,
|
|
97
|
+
usage: GPUTextureUsage.RENDER_ATTACHMENT
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
return this.depthStencilAttachment;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
/// <reference types="@webgpu/types" />
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
DeviceProps,
|
|
5
|
+
DeviceInfo,
|
|
6
|
+
DeviceLimits,
|
|
7
|
+
DeviceFeature,
|
|
8
|
+
CanvasContextProps,
|
|
9
|
+
BufferProps,
|
|
10
|
+
SamplerProps,
|
|
11
|
+
ShaderProps,
|
|
12
|
+
TextureProps,
|
|
13
|
+
TextureFormat,
|
|
14
|
+
ExternalTextureProps,
|
|
15
|
+
FramebufferProps,
|
|
16
|
+
RenderPipelineProps,
|
|
17
|
+
ComputePipelineProps,
|
|
18
|
+
RenderPassProps,
|
|
19
|
+
ComputePassProps
|
|
20
|
+
} from '@luma.gl/api';
|
|
21
|
+
import {Device, CanvasContext, log, cast} from '@luma.gl/api';
|
|
22
|
+
import WebGPUBuffer from './resources/webgpu-buffer';
|
|
23
|
+
import WebGPUTexture from './resources/webgpu-texture';
|
|
24
|
+
import WebGPUExternalTexture from './resources/webgpu-external-texture';
|
|
25
|
+
import WebGPUSampler from './resources/webgpu-sampler';
|
|
26
|
+
import WebGPUShader from './resources/webgpu-shader';
|
|
27
|
+
import WebGPURenderPipeline from './resources/webgpu-render-pipeline';
|
|
28
|
+
import WebGPUFramebuffer from './resources/webgpu-framebuffer';
|
|
29
|
+
import WebGPUComputePipeline from './resources/webgpu-compute-pipeline';
|
|
30
|
+
import WebGPURenderPass from './resources/webgpu-render-pass';
|
|
31
|
+
import WebGPUComputePass from './resources/webgpu-compute-pass';
|
|
32
|
+
|
|
33
|
+
import WebGPUCanvasContext from './webgpu-canvas-context';
|
|
34
|
+
// import {loadGlslangModule} from '../glsl/glslang';
|
|
35
|
+
|
|
36
|
+
/** WebGPU Device implementation */
|
|
37
|
+
export default class WebGPUDevice extends Device {
|
|
38
|
+
readonly handle: GPUDevice;
|
|
39
|
+
readonly adapter: GPUAdapter;
|
|
40
|
+
readonly lost: Promise<{reason: 'destroyed', message: string}>;
|
|
41
|
+
canvasContext: WebGPUCanvasContext | undefined;
|
|
42
|
+
|
|
43
|
+
commandEncoder: GPUCommandEncoder;
|
|
44
|
+
renderPass: WebGPURenderPass;
|
|
45
|
+
|
|
46
|
+
private _info: DeviceInfo;
|
|
47
|
+
private _isLost: boolean = false;
|
|
48
|
+
|
|
49
|
+
static type: string = 'webgpu';
|
|
50
|
+
|
|
51
|
+
/** Check if WebGPU is available */
|
|
52
|
+
static isSupported(): boolean {
|
|
53
|
+
return Boolean(typeof navigator !== 'undefined' && navigator.gpu);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
static async create(props: DeviceProps): Promise<WebGPUDevice> {
|
|
57
|
+
if (!navigator.gpu) {
|
|
58
|
+
throw new Error('WebGPU not available. Open in Chrome Canary and turn on chrome://flags/#enable-unsafe-webgpu');
|
|
59
|
+
}
|
|
60
|
+
log.groupCollapsed(1, 'WebGPUDevice created')();
|
|
61
|
+
const adapter = await navigator.gpu.requestAdapter({
|
|
62
|
+
powerPreference: "high-performance"
|
|
63
|
+
// forceSoftware: false
|
|
64
|
+
});
|
|
65
|
+
log.probe(1, "Adapter available")();
|
|
66
|
+
|
|
67
|
+
const gpuDevice = await adapter.requestDevice({
|
|
68
|
+
requiredFeatures: adapter.features as ReadonlySet<GPUFeatureName>,
|
|
69
|
+
// TODO ensure we obtain best limits
|
|
70
|
+
// requiredLimits: adapter.limits
|
|
71
|
+
});
|
|
72
|
+
log.probe(1, "GPUDevice available")();
|
|
73
|
+
|
|
74
|
+
if (typeof props.canvas === 'string') {
|
|
75
|
+
await CanvasContext.pageLoaded;
|
|
76
|
+
log.probe(1, "DOM is loaded")();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const device = new WebGPUDevice(gpuDevice, adapter, props);
|
|
80
|
+
log.probe(1, "Device created", device.info)();
|
|
81
|
+
log.table(1, device.info)();
|
|
82
|
+
log.groupEnd(1)();
|
|
83
|
+
return device;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
constructor(device: GPUDevice, adapter: GPUAdapter, props: DeviceProps) {
|
|
87
|
+
super(props);
|
|
88
|
+
this.handle = device;
|
|
89
|
+
this.adapter = adapter;
|
|
90
|
+
|
|
91
|
+
this._info = {
|
|
92
|
+
type: 'webgpu',
|
|
93
|
+
vendor: this.adapter.name,
|
|
94
|
+
renderer: '',
|
|
95
|
+
version: '',
|
|
96
|
+
gpu: 'unknown', // 'nvidia' | 'amd' | 'intel' | 'apple' | 'unknown',
|
|
97
|
+
shadingLanguages: ['glsl', 'wgsl'],
|
|
98
|
+
shadingLanguageVersions: {
|
|
99
|
+
glsl: '450',
|
|
100
|
+
wgsl: '100'
|
|
101
|
+
},
|
|
102
|
+
vendorMasked: '',
|
|
103
|
+
rendererMasked: ''
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
this.lost = this.handle.lost;
|
|
107
|
+
this.lost.then(_ => {
|
|
108
|
+
this._isLost = true;
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
// Note: WebGPU devices can be created without a canvas, for compute shader purposes
|
|
112
|
+
if (props.canvas) {
|
|
113
|
+
this.canvasContext = new WebGPUCanvasContext(this, this.adapter, {canvas: props.canvas});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
this.features = this._getFeatures();
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// TODO
|
|
120
|
+
// Load the glslang module now so that it is available synchronously when compiling shaders
|
|
121
|
+
// const {glsl = true} = props;
|
|
122
|
+
// this.glslang = glsl && await loadGlslangModule();
|
|
123
|
+
|
|
124
|
+
destroy() {
|
|
125
|
+
this.handle.destroy();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
get info(): DeviceInfo {
|
|
129
|
+
return this._info;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
features: Set<DeviceFeature>;
|
|
133
|
+
|
|
134
|
+
get limits(): DeviceLimits {
|
|
135
|
+
return this.handle.limits;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
isTextureFormatSupported(format: TextureFormat): boolean {
|
|
139
|
+
return !format.includes('webgl');
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** @todo implement proper check? */
|
|
143
|
+
isTextureFormatFilterable(format: TextureFormat): boolean {
|
|
144
|
+
return this.isTextureFormatSupported(format);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** @todo implement proper check? */
|
|
148
|
+
isTextureFormatRenderable(format: TextureFormat): boolean {
|
|
149
|
+
return this.isTextureFormatSupported(format);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
get isLost(): boolean {
|
|
153
|
+
return this._isLost;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
_createBuffer(props: BufferProps): WebGPUBuffer {
|
|
157
|
+
return new WebGPUBuffer(this, props);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
_createTexture(props: TextureProps): WebGPUTexture {
|
|
161
|
+
return new WebGPUTexture(this, props);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
createExternalTexture(props: ExternalTextureProps): WebGPUExternalTexture {
|
|
165
|
+
return new WebGPUExternalTexture(this, props);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
createShader(props: ShaderProps): WebGPUShader {
|
|
169
|
+
return new WebGPUShader(this, props);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
createSampler(props: SamplerProps): WebGPUSampler {
|
|
173
|
+
return new WebGPUSampler(this, props);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
createRenderPipeline(props: RenderPipelineProps): WebGPURenderPipeline {
|
|
177
|
+
return new WebGPURenderPipeline(this, props);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
createFramebuffer(props: FramebufferProps): WebGPUFramebuffer {
|
|
181
|
+
throw new Error('Not implemented');
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
createComputePipeline(props: ComputePipelineProps): WebGPUComputePipeline {
|
|
185
|
+
return new WebGPUComputePipeline(this, props);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// WebGPU specifics
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Allows a render pass to begin against a canvas context
|
|
192
|
+
* @todo need to support a "Framebuffer" equivalent (aka preconfigured RenderPassDescriptors?).
|
|
193
|
+
*/
|
|
194
|
+
beginRenderPass(props?: RenderPassProps): WebGPURenderPass {
|
|
195
|
+
this.commandEncoder = this.commandEncoder || this.handle.createCommandEncoder();
|
|
196
|
+
return new WebGPURenderPass(this, props);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
beginComputePass(props?: ComputePassProps): WebGPUComputePass {
|
|
200
|
+
this.commandEncoder = this.commandEncoder || this.handle.createCommandEncoder();
|
|
201
|
+
return new WebGPUComputePass(this, props);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
createCanvasContext(props?: CanvasContextProps): WebGPUCanvasContext {
|
|
205
|
+
return new WebGPUCanvasContext(this, this.adapter, props);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Gets default renderpass encoder.
|
|
210
|
+
* Creates a new encoder against default canvasContext if not already created
|
|
211
|
+
* @note Called internally by Model.
|
|
212
|
+
*/
|
|
213
|
+
getDefaultRenderPass(): WebGPURenderPass {
|
|
214
|
+
this.renderPass = this.renderPass || this.beginRenderPass({
|
|
215
|
+
framebuffer: this.canvasContext.getCurrentFramebuffer()
|
|
216
|
+
});
|
|
217
|
+
return this.renderPass;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
submit(): void {
|
|
221
|
+
this.renderPass.endPass();
|
|
222
|
+
const commandBuffer = this.commandEncoder.finish();
|
|
223
|
+
this.handle.queue.submit([commandBuffer]);
|
|
224
|
+
this.commandEncoder = null;
|
|
225
|
+
this.renderPass = null;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
_getFeatures() {
|
|
229
|
+
// WebGPU Features
|
|
230
|
+
const features = new Set<DeviceFeature>(this.handle.features as Set<DeviceFeature>);
|
|
231
|
+
|
|
232
|
+
// Fixups for pre-standard names: https://github.com/webgpu-native/webgpu-headers/issues/133
|
|
233
|
+
// @ts-expect-error Chrome Canary v99
|
|
234
|
+
if (features.has('depth-clamping')) {
|
|
235
|
+
// @ts-expect-error Chrome Canary v99
|
|
236
|
+
features.delete('depth-clamping');
|
|
237
|
+
features.add('depth-clip-control');
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// Add subsets
|
|
241
|
+
if (features.has('texture-compression-bc')) {
|
|
242
|
+
features.add('texture-compression-bc5-webgl');
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
features.add('webgpu');
|
|
246
|
+
|
|
247
|
+
features.add('timer-query-webgl');
|
|
248
|
+
|
|
249
|
+
// WEBGL1 SUPPORT
|
|
250
|
+
features.add('vertex-array-object-webgl1');
|
|
251
|
+
features.add('instanced-rendering-webgl1');
|
|
252
|
+
features.add('multiple-render-targets-webgl1');
|
|
253
|
+
features.add('index-uint32-webgl1');
|
|
254
|
+
features.add('blend-minmax-webgl1');
|
|
255
|
+
features.add('texture-blend-float-webgl1');
|
|
256
|
+
|
|
257
|
+
// TEXTURES, RENDERBUFFERS
|
|
258
|
+
features.add('texture-formats-srgb-webgl1');
|
|
259
|
+
|
|
260
|
+
// TEXTURES
|
|
261
|
+
features.add('texture-formats-depth-webgl1');
|
|
262
|
+
features.add('texture-formats-float32-webgl1');
|
|
263
|
+
features.add('texture-formats-float16-webgl1');
|
|
264
|
+
|
|
265
|
+
features.add('texture-filter-linear-float32-webgl');
|
|
266
|
+
features.add('texture-filter-linear-float16-webgl');
|
|
267
|
+
features.add('texture-filter-anisotropic-webgl');
|
|
268
|
+
|
|
269
|
+
// FRAMEBUFFERS, TEXTURES AND RENDERBUFFERS
|
|
270
|
+
features.add('texture-renderable-rgba32float-webgl');
|
|
271
|
+
features.add('texture-renderable-float32-webgl');
|
|
272
|
+
features.add('texture-renderable-float16-webgl');
|
|
273
|
+
|
|
274
|
+
// GLSL extensions
|
|
275
|
+
features.add('glsl-frag-data');
|
|
276
|
+
features.add('glsl-frag-depth');
|
|
277
|
+
features.add('glsl-derivatives');
|
|
278
|
+
features.add('glsl-texture-lod');
|
|
279
|
+
|
|
280
|
+
return features;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
File without changes
|
package/src/bundle.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Inspired by webgpu samples at https://github.com/austinEng/webgpu-samples/blob/master/src/glslang.ts
|
|
2
|
+
// under BSD 3-clause license
|
|
3
|
+
|
|
4
|
+
let glslang: unknown;
|
|
5
|
+
|
|
6
|
+
/** Dynamically load the GLSL compiler */
|
|
7
|
+
export async function loadGlslangModule() {
|
|
8
|
+
if (!glslang) {
|
|
9
|
+
// @ts-ignore
|
|
10
|
+
const glslangModule = await import(/* webpackIgnore: true */ 'https://unpkg.com/@webgpu/glslang@0.0.15/dist/web-devel/glslang.js');
|
|
11
|
+
glslang = await glslangModule.default();
|
|
12
|
+
}
|
|
13
|
+
return glslang;
|
|
14
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
|
|
2
|
+
// Initialize any global state
|
|
3
|
+
import '@luma.gl/api';
|
|
4
|
+
import './init'
|
|
5
|
+
|
|
6
|
+
// WEBGPU ADAPTER
|
|
7
|
+
export {default as WebGPUDevice} from './adapter/webgpu-device';
|
|
8
|
+
|
|
9
|
+
// WEBGPU CLASSES (typically not accessed directly)
|
|
10
|
+
export {default as WebGPUBuffer} from './adapter/resources/webgpu-buffer';
|
|
11
|
+
export {default as WebGPUTexture} from './adapter/resources/webgpu-texture';
|
|
12
|
+
export {default as WebGPUSampler} from './adapter/resources/webgpu-sampler';
|
|
13
|
+
export {default as WebGPUShader} from './adapter/resources/webgpu-shader';
|
package/src/init.ts
ADDED