@luma.gl/webgpu 9.0.0-alpha.21 → 9.0.0-alpha.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/dist/adapter/resources/webgpu-command-encoder.d.ts +3 -11
  2. package/dist/adapter/resources/webgpu-command-encoder.d.ts.map +1 -1
  3. package/dist/adapter/resources/webgpu-command-encoder.js.map +1 -1
  4. package/dist/adapter/resources/webgpu-framebuffer.d.ts +0 -17
  5. package/dist/adapter/resources/webgpu-framebuffer.d.ts.map +1 -1
  6. package/dist/adapter/resources/webgpu-framebuffer.js +2 -84
  7. package/dist/adapter/resources/webgpu-framebuffer.js.map +1 -1
  8. package/dist/adapter/resources/webgpu-render-pass.d.ts +6 -1
  9. package/dist/adapter/resources/webgpu-render-pass.d.ts.map +1 -1
  10. package/dist/adapter/resources/webgpu-render-pass.js +19 -1
  11. package/dist/adapter/resources/webgpu-render-pass.js.map +1 -1
  12. package/dist/adapter/resources/webgpu-shader.js +3 -2
  13. package/dist/adapter/resources/webgpu-shader.js.map +1 -1
  14. package/dist/adapter/resources/webgpu-texture.d.ts +10 -2
  15. package/dist/adapter/resources/webgpu-texture.d.ts.map +1 -1
  16. package/dist/adapter/resources/webgpu-texture.js +8 -1
  17. package/dist/adapter/resources/webgpu-texture.js.map +1 -1
  18. package/dist/adapter/webgpu-canvas-context.d.ts +1 -1
  19. package/dist/adapter/webgpu-canvas-context.d.ts.map +1 -1
  20. package/dist/adapter/webgpu-canvas-context.js.map +1 -1
  21. package/dist/adapter/webgpu-device.d.ts +16 -2
  22. package/dist/adapter/webgpu-device.d.ts.map +1 -1
  23. package/dist/adapter/webgpu-device.js +31 -2
  24. package/dist/adapter/webgpu-device.js.map +1 -1
  25. package/dist/dist.dev.js +384 -171
  26. package/dist/index.cjs +80 -87
  27. package/dist.min.js +4 -4
  28. package/package.json +3 -3
  29. package/src/adapter/resources/webgpu-command-encoder.ts +40 -21
  30. package/src/adapter/resources/webgpu-framebuffer.ts +6 -107
  31. package/src/adapter/resources/webgpu-render-pass.ts +34 -4
  32. package/src/adapter/resources/webgpu-shader.ts +2 -2
  33. package/src/adapter/resources/webgpu-texture.ts +8 -2
  34. package/src/adapter/webgpu-canvas-context.ts +2 -1
  35. package/src/adapter/webgpu-device.ts +71 -3
  36. package/src/.DS_Store +0 -0
  37. package/src/adapter/.DS_Store +0 -0
@@ -1,120 +1,19 @@
1
- import type {FramebufferProps, ColorTextureFormat} from '@luma.gl/api';
2
- import {Framebuffer, Texture} from '@luma.gl/api';
1
+ import type {FramebufferProps} from '@luma.gl/api';
2
+ import {Framebuffer} from '@luma.gl/api';
3
3
  import {WebGPUDevice} from '../webgpu-device';
4
- // import {WebGPUCanvasContext} from '../webgpu-canvas-context';
5
- import {WebGPUTexture} from './webgpu-texture';
6
-
7
- // const DEFAULT_DEPTH_STENCIL_FORMAT: DepthStencilTextureFormat = 'depth24plus';
8
-
9
- // const MAX_COLOR_ATTACHMENTS = 8;
10
4
 
11
5
  /**
12
- * Create new textures with correct size for all attachments.
13
- * @note resize() destroys existing textures (if size has changed).
6
+ * Create new textures with correct size for all attachments.
7
+ * @note resize() destroys existing textures (if size has changed).
14
8
  */
15
9
  export class WebGPUFramebuffer extends Framebuffer {
16
10
  readonly device: WebGPUDevice;
17
11
 
18
- colorAttachments: WebGPUTexture[] = [];
19
- depthStencilAttachment: WebGPUTexture | null = null;
20
-
21
- /** Partial render pass descriptor. Used by WebGPURenderPass */
22
- renderPassDescriptor: {
23
- colorAttachments: GPURenderPassColorAttachment[];
24
- depthStencilAttachment?: GPURenderPassDepthStencilAttachment;
25
- } = {
26
- colorAttachments: []
27
- };
28
-
29
12
  constructor(device: WebGPUDevice, props: FramebufferProps) {
30
13
  super(device, props);
31
14
  this.device = device;
32
15
 
33
- if (props.depthStencilAttachment) {
34
- this.depthStencilAttachment = this.createDepthStencilTexture(props);
35
- }
36
-
37
- if (props.colorAttachments) {
38
- this.colorAttachments = props.colorAttachments.map(colorAttachment => this.createColorTexture(this.props, colorAttachment));
39
- }
40
-
41
- if (this.depthStencilAttachment) {
42
- this.renderPassDescriptor.depthStencilAttachment = {
43
- view: this.depthStencilAttachment.handle.createView(),
44
- // Add default clear values
45
- depthClearValue: 1.0,
46
- depthStoreOp: 'store',
47
- stencilClearValue: 0,
48
- stencilStoreOp: 'store',
49
- }
50
- }
51
-
52
- if (this.colorAttachments.length > 0) {
53
- this.renderPassDescriptor.colorAttachments = this.colorAttachments.map(colorAttachment => ({
54
- view: colorAttachment.handle.createView(),
55
- loadOp: 'clear',
56
- loadValue: [0.0, 0.0, 0.0, 0.0],
57
- storeOp: 'store'
58
- }));
59
- }
60
- }
61
-
62
- /** Create depth stencil texture */
63
- private createDepthStencilTexture(props: FramebufferProps): WebGPUTexture {
64
- if (props.depthStencilAttachment instanceof WebGPUTexture) {
65
- return props.depthStencilAttachment;
66
- }
67
-
68
- if (typeof props.depthStencilAttachment === 'string') {
69
- return this.device._createTexture({
70
- id: 'depth-stencil-attachment',
71
- format: props.depthStencilAttachment,
72
- width: props.width,
73
- height: props.height,
74
- usage: Texture.RENDER_ATTACHMENT
75
- });
76
- }
77
-
78
- throw new Error('type');
79
- }
80
-
81
- private createColorTexture(props: FramebufferProps, texture: Texture | ColorTextureFormat): WebGPUTexture {
82
- if (texture instanceof WebGPUTexture) {
83
- return texture;
84
- }
85
-
86
- if (typeof texture === 'string') {
87
- return this.device._createTexture({
88
- id: 'color-attachment',
89
- format: texture,
90
- width: props.width,
91
- height: props.height,
92
- usage: Texture.RENDER_ATTACHMENT
93
- });
94
- }
95
-
96
- throw new Error('type');
97
- }
98
-
99
- /**
100
- * Create new textures with correct size for all attachments.
101
- * @note destroys existing textures.
102
- */
103
- protected _resizeAttachments(width: number, height: number): void {
104
- for (let i = 0; i < this.colorAttachments.length; ++i) {
105
- if (this.colorAttachments[i]) {
106
- const resizedTexture = this.device._createTexture({...this.colorAttachments[i].props, width, height})
107
- this.colorAttachments[i].destroy();
108
- this.colorAttachments[i] = resizedTexture;
109
- this.renderPassDescriptor.colorAttachments[i].view = resizedTexture.handle.createView();
110
- }
111
- }
112
-
113
- if (this.depthStencilAttachment) {
114
- const resizedTexture = this.device._createTexture({...this.depthStencilAttachment.props, width, height})
115
- this.depthStencilAttachment.destroy();
116
- this.depthStencilAttachment = resizedTexture;
117
- this.renderPassDescriptor.depthStencilAttachment.view = resizedTexture.handle.createView();
118
- }
16
+ // Auto create textures for attachments if needed
17
+ this.autoCreateAttachmentTextures();
119
18
  }
120
19
  }
@@ -1,7 +1,8 @@
1
- import type {RenderPassProps, RenderPassParameters, Binding} from '@luma.gl/api';
1
+ import type {RenderPassProps, RenderPassParameters, Binding, Framebuffer} from '@luma.gl/api';
2
2
  import {Buffer, RenderPass, RenderPipeline, cast} from '@luma.gl/api';
3
3
  import {WebGPUDevice} from '../webgpu-device';
4
4
  import {WebGPUBuffer} from './webgpu-buffer';
5
+ import {WebGPUTexture} from './webgpu-texture';
5
6
  // import {WebGPUCommandEncoder} from './webgpu-command-encoder';
6
7
  import {WebGPURenderPipeline} from './webgpu-render-pipeline';
7
8
 
@@ -15,9 +16,8 @@ export class WebGPURenderPass extends RenderPass {
15
16
  constructor(device: WebGPUDevice, props: RenderPassProps = {}) {
16
17
  super(device, props);
17
18
  this.device = device;
18
- const framebuffer = props.framebuffer || device.canvasContext.getCurrentFramebuffer();
19
- // @ts-expect-error
20
- const renderPassDescriptor = framebuffer.renderPassDescriptor;
19
+ const framebuffer = props.framebuffer || device.canvasContext.getCurrentFramebuffer() ;
20
+ const renderPassDescriptor = this.getRenderPassDescriptor(framebuffer);
21
21
  this.handle = this.props.handle || device.commandEncoder.beginRenderPass(renderPassDescriptor);
22
22
  this.handle.label = this.props.id;
23
23
  }
@@ -128,4 +128,34 @@ export class WebGPURenderPass extends RenderPass {
128
128
  // endPipelineStatisticsQuery(querySet: GPUQuerySet, queryIndex: number): void;
129
129
 
130
130
  // executeBundles(bundles: Iterable<GPURenderBundle>): void;
131
+
132
+ // INTERNAL
133
+
134
+ /**
135
+ * Partial render pass descriptor. Used by WebGPURenderPass.
136
+ * @returns attachments fields of a renderpass descriptor.
137
+ */
138
+ protected getRenderPassDescriptor(framebuffer: Framebuffer): GPURenderPassDescriptor {
139
+ const renderPassDescriptor: GPURenderPassDescriptor = {
140
+ colorAttachments: []
141
+ };
142
+
143
+ renderPassDescriptor.colorAttachments = framebuffer.colorAttachments.map(colorAttachment => ({
144
+ // clear values
145
+ loadOp: 'clear',
146
+ colorClearValue: this.props.clearColor || [0, 0, 0, 0],
147
+ storeOp: this.props.discard? 'discard': 'store',
148
+ // ...colorAttachment,
149
+ view: (colorAttachment as WebGPUTexture).handle.createView()
150
+ }));
151
+
152
+ if (framebuffer.depthStencilAttachment) {
153
+ renderPassDescriptor.depthStencilAttachment = {
154
+ ...this.props,
155
+ view: (framebuffer.depthStencilAttachment as WebGPUTexture).handle.createView()
156
+ };
157
+ }
158
+
159
+ return renderPassDescriptor;
160
+ }
131
161
  }
@@ -43,7 +43,7 @@ export class WebGPUShader extends Shader {
43
43
  }
44
44
 
45
45
  protected createHandle(): GPUShaderModule {
46
- const {source} = this.props;
46
+ const {source, stage} = this.props;
47
47
 
48
48
  let language = this.props.language;
49
49
  // Compile from src
@@ -59,7 +59,7 @@ export class WebGPUShader extends Shader {
59
59
  return this.device.handle.createShaderModule({
60
60
  code: source,
61
61
  // @ts-expect-error
62
- transform: (glsl) => this.device.glslang.compileGLSL(glsl, type)
62
+ transform: (glsl) => this.device.glslang.compileGLSL(glsl, stage)
63
63
  });
64
64
  default:
65
65
  throw new Error(language);
@@ -19,6 +19,9 @@ export class WebGPUTexture extends Texture {
19
19
  readonly view: GPUTextureView;
20
20
  sampler: WebGPUSampler;
21
21
 
22
+ override height: number;
23
+ override width: number;
24
+
22
25
  // static async createFromImageURL(src, usage = 0) {
23
26
  // const img = document.createElement('img');
24
27
  // img.src = src;
@@ -40,6 +43,9 @@ export class WebGPUTexture extends Texture {
40
43
  this.setData({data: this.props.data} );
41
44
  }
42
45
 
46
+ this.width = this.handle.width;
47
+ this.height = this.handle.height;
48
+
43
49
  // Create a default sampler. This mimics the WebGL1 API where sampler props are stored on the texture
44
50
  // this.setSampler(props.sampler);
45
51
  this.sampler = props.sampler instanceof WebGPUSampler ? props.sampler : new WebGPUSampler(this.device, props.sampler);
@@ -116,7 +122,7 @@ export class WebGPUTexture extends Texture {
116
122
  aspect?: 'all' | 'stencil-only' | 'depth-only';
117
123
  colorSpace?: 'srgb';
118
124
  premultipliedAlpha?: boolean;
119
- }): this {
125
+ }): {width: number, height: number} {
120
126
  const {
121
127
  source,
122
128
  width = options.source.width,
@@ -157,7 +163,7 @@ export class WebGPUTexture extends Texture {
157
163
  depth
158
164
  ]
159
165
  );
160
- return this;
166
+ return {width, height};
161
167
  }
162
168
 
163
169
  /*
@@ -24,7 +24,8 @@ export class WebGPUCanvasContext extends CanvasContext {
24
24
  this.height = -1;
25
25
 
26
26
  this._setAutoCreatedCanvasId(`${this.device.id}-canvas`);
27
- this.gpuCanvasContext = this.canvas.getContext('webgpu') as GPUCanvasContext;
27
+ // @ts-ignore TODO - we don't handle OffscreenRenderingContext.
28
+ this.gpuCanvasContext = this.canvas.getContext('webgpu') ;
28
29
  // @ts-expect-error TODO this has been replaced
29
30
  this.format = this.gpuCanvasContext.getPreferredFormat(adapter);
30
31
  }
@@ -10,6 +10,7 @@ import type {
10
10
  BufferProps,
11
11
  SamplerProps,
12
12
  ShaderProps,
13
+ Texture,
13
14
  TextureProps,
14
15
  TextureFormat,
15
16
  ExternalTextureProps,
@@ -17,7 +18,8 @@ import type {
17
18
  RenderPipelineProps,
18
19
  ComputePipelineProps,
19
20
  RenderPassProps,
20
- ComputePassProps
21
+ ComputePassProps,
22
+ // CommandEncoderProps
21
23
  } from '@luma.gl/api';
22
24
  import {Device, CanvasContext, log, uid} from '@luma.gl/api';
23
25
  import {WebGPUBuffer} from './resources/webgpu-buffer';
@@ -30,6 +32,7 @@ import {WebGPUFramebuffer} from './resources/webgpu-framebuffer';
30
32
  import {WebGPUComputePipeline} from './resources/webgpu-compute-pipeline';
31
33
  import {WebGPURenderPass} from './resources/webgpu-render-pass';
32
34
  import {WebGPUComputePass} from './resources/webgpu-compute-pass';
35
+ // import {WebGPUCommandEncoder} from './resources/webgpu-command-encoder';
33
36
 
34
37
  import {WebGPUCanvasContext} from './webgpu-canvas-context';
35
38
  // import {loadGlslangModule} from '../glsl/glslang';
@@ -162,8 +165,9 @@ export class WebGPUDevice extends Device {
162
165
  return this._isLost;
163
166
  }
164
167
 
165
- _createBuffer(props: BufferProps): WebGPUBuffer {
166
- return new WebGPUBuffer(this, props);
168
+ createBuffer(props: BufferProps | ArrayBuffer | ArrayBufferView): WebGPUBuffer {
169
+ const newProps = this._getBufferProps(props);
170
+ return new WebGPUBuffer(this, newProps);
167
171
  }
168
172
 
169
173
  _createTexture(props: TextureProps): WebGPUTexture {
@@ -210,6 +214,10 @@ export class WebGPUDevice extends Device {
210
214
  return new WebGPUComputePass(this, props);
211
215
  }
212
216
 
217
+ // createCommandEncoder(props: CommandEncoderProps): WebGPUCommandEncoder {
218
+ // return new WebGPUCommandEncoder(this, props);
219
+ // }
220
+
213
221
  createCanvasContext(props: CanvasContextProps): WebGPUCanvasContext {
214
222
  return new WebGPUCanvasContext(this, this.adapter, props);
215
223
  }
@@ -292,4 +300,64 @@ export class WebGPUDevice extends Device {
292
300
 
293
301
  return features;
294
302
  }
303
+
304
+ copyExternalImageToTexture(options: {
305
+ texture: Texture;
306
+ mipLevel?: number;
307
+ aspect?: 'all' | 'stencil-only' | 'depth-only';
308
+ colorSpace?: 'display-p3' | 'srgb';
309
+ premultipliedAlpha?: boolean;
310
+
311
+ source: ImageBitmap | HTMLVideoElement | HTMLCanvasElement | OffscreenCanvas;
312
+ sourceX?: number;
313
+ sourceY?: number;
314
+
315
+ width?: number;
316
+ height?: number;
317
+ depth?: number;
318
+ }): void {
319
+ const {
320
+ source,
321
+ sourceX = 0,
322
+ sourceY = 0,
323
+
324
+ texture,
325
+ mipLevel = 0,
326
+ aspect = 'all',
327
+ colorSpace = 'display-p3',
328
+ premultipliedAlpha = false,
329
+ // destinationX,
330
+ // destinationY,
331
+ // desitnationZ,
332
+
333
+ width = texture.width,
334
+ height = texture.height,
335
+ depth = 1
336
+ } = options;
337
+
338
+ const webGpuTexture = texture as WebGPUTexture;
339
+
340
+ this.handle?.queue.copyExternalImageToTexture(
341
+ // source: GPUImageCopyExternalImage
342
+ {
343
+ source,
344
+ origin: [sourceX, sourceY]
345
+ },
346
+ // destination: GPUImageCopyTextureTagged
347
+ {
348
+ texture: webGpuTexture.handle,
349
+ origin: [0, 0, 0], // [x, y, z],
350
+ mipLevel,
351
+ aspect,
352
+ colorSpace,
353
+ premultipliedAlpha
354
+ },
355
+ // copySize: GPUExtent3D
356
+ [
357
+ width,
358
+ height,
359
+ depth
360
+ ]
361
+ )
362
+ }
295
363
  }
package/src/.DS_Store DELETED
Binary file
Binary file