Skip to content

Commit e49203c

Browse files
committed
feat: Start porting ScatterplotLayer to WebGPU / WGSL
1 parent 4cef175 commit e49203c

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

modules/core/src/lib/deck.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {deepEqual} from '../utils/deep-equal';
1515
import typedArrayManager from '../utils/typed-array-manager';
1616
import {VERSION} from './init';
1717

18-
import {luma} from '@luma.gl/core';
18+
import {luma, Adapter} from '@luma.gl/core';
1919
import {webgl2Adapter} from '@luma.gl/webgl';
2020
import {Timeline} from '@luma.gl/engine';
2121
import {AnimationLoop} from '@luma.gl/engine';
@@ -117,6 +117,9 @@ export type DeckProps<ViewsT extends ViewOrViews = null> = {
117117
/** Use an existing luma.gl GPU device. @note If not supplied, a new device will be created using props.deviceProps */
118118
device?: Device | null;
119119

120+
/** Supply adapters to use when a new device is created */
121+
adapters?: Adapter[];
122+
120123
/** A new device will be created using these props, assuming that an existing device is not supplied using props.device) */
121124
deviceProps?: CreateDeviceProps;
122125

@@ -373,16 +376,21 @@ export default class Deck<ViewsT extends ViewOrViews = null> {
373376

374377
// Create a new device
375378
if (!deviceOrPromise) {
379+
const canvasContextUserProps = this.props.deviceProps?.createCanvasContext;
380+
const canvasContextProps =
381+
typeof canvasContextUserProps === 'object' ? canvasContextUserProps : undefined;
376382
// Create the "best" device supported from the registered adapters
383+
const adapters = Array.from(new Set([...(props.adapters || []), webgl2Adapter]));
377384
deviceOrPromise = luma.createDevice({
378385
type: 'best-available',
379386
// luma by default throws if a device is already attached
380387
// asynchronous device creation could happen after finalize() is called
381388
// TODO - createDevice should support AbortController?
382389
_reuseDevices: true,
383-
adapters: [webgl2Adapter],
390+
adapters,
384391
...props.deviceProps,
385392
createCanvasContext: {
393+
...canvasContextProps,
386394
canvas: this._createCanvas(props),
387395
useDevicePixels: this.props.useDevicePixels,
388396
// TODO v9.2 - replace AnimationLoop's `autoResizeDrawingBuffer` with CanvasContext's `autoResize`

modules/layers/src/line-layer/line-layer.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
UpdateParameters,
1717
DefaultProps
1818
} from '@deck.gl/core';
19+
import {Parameters} from '@luma.gl/core';
1920
import {Model, Geometry} from '@luma.gl/engine';
2021

2122
import {lineUniforms, LineProps} from './line-layer-uniforms';
@@ -188,6 +189,15 @@ export default class LineLayer<DataT = any, ExtraProps extends {} = {}> extends
188189
}
189190

190191
protected _getModel(): Model {
192+
// TODO(ibgreen): WebGPU complication: Matching attachment state of the renderpass requires including a depth buffer
193+
const parameters =
194+
this.context.device.type === 'webgpu'
195+
? ({
196+
depthWriteEnabled: true,
197+
depthCompare: 'less-equal'
198+
} satisfies Parameters)
199+
: undefined;
200+
191201
/*
192202
* (0, -1)-------------_(1, -1)
193203
* | _,-" |
@@ -207,6 +217,7 @@ export default class LineLayer<DataT = any, ExtraProps extends {} = {}> extends
207217
positions: {size: 3, value: new Float32Array(positions)}
208218
}
209219
}),
220+
parameters,
210221
isInstanced: true
211222
});
212223
}

modules/layers/src/scatterplot-layer/scatterplot-layer.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import type {
2020
Color,
2121
DefaultProps
2222
} from '@deck.gl/core';
23+
import {Parameters} from '@luma.gl/core';
2324

2425
const DEFAULT_COLOR: [number, number, number, number] = [0, 0, 0, 255];
2526

@@ -257,10 +258,23 @@ export default class ScatterplotLayer<DataT = any, ExtraPropsT extends {} = {}>
257258
};
258259
const model = this.state.model!;
259260
model.shaderInputs.setProps({scatterplot: scatterplotProps});
261+
if (this.context.device.type === 'webgpu') {
262+
// @ts-expect-error TODO - this line was needed during WebGPU port
263+
model.instanceCount = this.props.data.length;
264+
}
260265
model.draw(this.context.renderPass);
261266
}
262267

263268
protected _getModel() {
269+
// TODO(ibgreen): WebGPU complication: Matching attachment state of the renderpass requires including a depth buffer
270+
const parameters =
271+
this.context.device.type === 'webgpu'
272+
? ({
273+
depthWriteEnabled: true,
274+
depthCompare: 'less-equal'
275+
} satisfies Parameters)
276+
: undefined;
277+
264278
// a square that minimally cover the unit circle
265279
const positions = [-1, -1, 0, 1, -1, 0, -1, 1, 0, 1, 1, 0];
266280
return new Model(this.context.device, {
@@ -273,7 +287,7 @@ export default class ScatterplotLayer<DataT = any, ExtraPropsT extends {} = {}>
273287
positions: {size: 3, value: new Float32Array(positions)}
274288
}
275289
}),
276-
isInstanced: true
290+
parameters
277291
});
278292
}
279293
}

0 commit comments

Comments
 (0)