WGSLNodeBuilder: Add polyfill for storage pointer when not available #32516
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
WGSL specification does not allow

ptr<storage, ...>(storage pointers) as function parameters (gpuweb/gpuweb#2268). The naga WGSL validator (used by WebGPU) now strictly enforces this, causing errors like:Argument 'bvh_index' at index 0 is a pointer of space Storage, which can't be passed into functions.
The issue wasn't brought yet as it seems to be patched internally in Chrome since r123 https://developer.chrome.com/blog/new-in-webgpu-123#unrestricted_pointer_parameters_in_wgsl but crashes silently Safari and explicitly in Firefox:
When using
wgslFnwith storage buffers passed asptr<storage, array<T>, access>parameters, Three.js was generating invalid WGSL code.The Solution
Storage buffers must be declared as module-level variables with @group/@binding attributes and accessed directly, not passed as function parameters.
How It Works
wgslFnis called with storage buffer parameters,FunctionCallNodebuilds those buffers (registering them as module-level bindings) and creates a mapping of parameter names to binding namesWGSLNodeFunction.getCode()transforms the code:ptr<storage, ...>parameters from signaturesAlternative 1:
wgsl()orcode()and automatically bind them so we can access these buffers within thewgslFnwithout having to forward them as parameters.ptr<storage,...>and such are being used.Alternative 2:
It's possible to bypass the issue using a mix of
FnandwgslFnbut then requires very advanced knowledge and tricks with theNodeBuilder, and I don't think any developer coming fromWebGPUwill understand as it's pretty counterintuitive especially having to use TSL when trying to use plainwgsl().This contribution is funded by Utsubo && Threejs Blocks