Skip to content

Commit 7dd6bad

Browse files
authored
fix(webgpu): Emulate constant attributes as buffers and trigger update on constant change (#9726)
1 parent f88738a commit 7dd6bad

File tree

7 files changed

+13
-231
lines changed

7 files changed

+13
-231
lines changed

examples/website/line/app.tsx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,7 @@ export default function App({
9595
const r = z / 10000;
9696
return [255 * (1 - r * 2), 128 * r, 255 * r, 255 * (1 - r)];
9797
},
98-
// TODO(ck): WebGPU does not support constant attributes, so force the line layer to
99-
// generate a buffer with each value individually, otherwise it will not be updated properly.
100-
getWidth: () => lineWidth,
101-
updateTriggers: {
102-
// then use update triggers so that the function will be re-evaluated when lineWidth changes
103-
getWidth: [lineWidth]
104-
},
98+
getWidth: lineWidth,
10599
pickable: true
106100
})
107101
];

modules/core/src/lib/attribute/attribute.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,18 @@ export default class Attribute extends DataColumn<AttributeOptions, AttributeInt
255255
// Use generic value
256256
// Returns true if successful
257257
setConstantValue(value?: NumericArray): boolean {
258-
// TODO(ibgreen): WebGPU does not support constant values
258+
// TODO(ibgreen): WebGPU does not support constant values,
259+
// they will be emulated as buffers instead for now.
259260
const isWebGPU = this.device.type === 'webgpu';
260261
if (isWebGPU || value === undefined || typeof value === 'function') {
262+
if (isWebGPU && typeof value !== 'function') {
263+
const normalisedValue = this._normalizeValue(value, [], 0);
264+
// ensure we trigger an update for the attribute's emulated buffer
265+
// where webgl would perform the update here
266+
if (!this._areValuesEqual(normalisedValue, this.value)) {
267+
this.setNeedsUpdate('WebGPU constant updated');
268+
}
269+
}
261270
return false;
262271
}
263272

@@ -435,8 +444,8 @@ export default class Attribute extends DataColumn<AttributeOptions, AttributeInt
435444
// @ts-ignore
436445
(typeof accessor === 'function' ? accessor : props[accessor]);
437446
// TODO(ibgreen) WebGPU needs buffers, generate an accessor function from a constant
438-
if (typeof accessorFunc !== 'function') {
439-
accessorFunc = () => accessorFunc;
447+
if (typeof accessorFunc !== 'function' && typeof accessor === 'string') {
448+
accessorFunc = () => props[accessor];
440449
}
441450
assert(typeof accessorFunc === 'function', `accessor "${accessor}" is not a function`);
442451

test/apps/webgpu-line/README.md

Lines changed: 0 additions & 26 deletions
This file was deleted.

test/apps/webgpu-line/app.tsx

Lines changed: 0 additions & 132 deletions
This file was deleted.

test/apps/webgpu-line/index.html

Lines changed: 0 additions & 29 deletions
This file was deleted.

test/apps/webgpu-line/package.json

Lines changed: 0 additions & 26 deletions
This file was deleted.

test/apps/webgpu-line/tsconfig.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)