Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebGPURenderer: Read Only Storage Buffer Creation #28400

Closed
wants to merge 37 commits into from

Conversation

cmhhelgeson
Copy link
Contributor

Description

Facilitates the creation of read-only storage buffers in tslFn shaders using a new storageImmutable() function. Although we can currently create storage, read_write array buffers within TSL functions using StorageBufferNode, we are currently unable to create read-only storage buffers within these shaders, which limits our ability to use these buffers in contexts where only read-only buffers are permitted (such as vertex shaders for instance)

The storageImmutable() function changes specific parameters within a StorageBufferNode object to specify to the WGSLNodeBuilder class and the WebGPUBinding Utils class how the storage buffer should be accessed. Currently, the read-only functionality is grafted onto the existing StorageBufferNode class using the new readOnly parameter, but if necessary, it can trivially be extrapolated out into its own class.

To test the code, I located code within webgpu_compute_geometry that was only being read, then changed the storage function to a storageImmutable function

const computeFn = tslFn( () => {
	const positionAttribute = storageImmutable( positionBaseAttribute, 'vec3', positionBaseAttribute.count );
	const normalAttribute = storageImmutable( normalBaseAttribute, 'vec3', normalBaseAttribute.count );
...

When applying this change, there was no observable change to the visual output, and the console registered no errors. Additionally, I performed a diff on the WGSL code output by the TSL function, which returned no differences aside from the storage buffer being defined within the shader as a <storage, read> buffer rather than a <storage, read_write> buffer.

Currently, given that I am a new contributor, am unfamiliar with all the architectural preferences of this repository's maintainers, and have yet to test this functionality in vertex shaders and other edge case scenarios, I have elected to put the draft in PR. However, feel free to comment on any changes you would like to see.

@cmhhelgeson cmhhelgeson changed the title add writable WebGPURenderer: Read Only Storage Buffer Creation May 16, 2024
@mrdoob mrdoob requested a review from sunag May 17, 2024 08:18
@@ -13,6 +13,8 @@ class StorageBufferNode extends BufferNode {

this.isStorageBufferNode = true;

this.readOnly = false;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make sure to adhere the project's code style. This was already mentioned in #28379 (comment).

* BatchedMesh: add getColorAt and setColorAt

* pr fixes and suggestions

* bring back previous condition to recompile shader

* add colorSpace to DataTexture

* change instance to geometry in docs
Comment on lines 7 to 13
constructor( nodeUniform ) {

super( 'StorageBuffer_' + _id ++, nodeUniform ? nodeUniform.value : null );
constructor( nodeUniform, readOnly) {
readOnly = readOnly ? readOnly : false;
const namePrefix = readOnly ? 'StorageReadOnlyBuffer_' : 'StorageBuffer_'
super( namePrefix + _id ++, nodeUniform ? nodeUniform.value : null );

this.nodeUniform = nodeUniform;
this.readOnly = readOnly;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use tabs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies for the delay, hopefully formatting should be fixed now that I've manually checked each file as well as disabled a lot of the automatic spacing and indentation VSCode was applying to the files.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also be true for pull request #28379 as well.

RenaudRohlinger and others added 20 commits May 17, 2024 17:43
* revert mrdoob#28289

* log patch should only cover webgpu

* prettify code
* NormalNode: Improve tree shaking using TSL

* fix attribute default value
* Editor: Fix serialization of commands.

* Editor: Restore order in `fromJSON()`.

* Editor: Fix regression when adding/removing scripts.
…SL (mrdoob#28410)

* TangentNode: Improve tree shaking using TSL

* BitangentNode: Improve tree shaking using TSL

* cleanup
* return z based on the the camera forward z value

* Fix sort in example

* Remove unused argument
* sync script editor title

* no dispatching editScript, setValue is enough
Co-authored-by: aardgoose <angus.sawyer@email.com>
* BatchedMesh: default color to white

* update comment
Copy link

📦 Bundle size

Full ESM build, minified and gzipped.

Filesize dev Filesize PR Diff
678.9 kB (168.2 kB) 678.9 kB (168.2 kB) +0 B

🌳 Bundle size after tree-shaking

Minimal build including a renderer, camera, empty scene, and dependencies.

Filesize dev Filesize PR Diff
456.9 kB (110.3 kB) 456.9 kB (110.3 kB) +0 B

@cmhhelgeson cmhhelgeson deleted the webgpu_readonly_storage branch May 20, 2024 02:47
@cmhhelgeson cmhhelgeson restored the webgpu_readonly_storage branch May 20, 2024 02:49
@cmhhelgeson cmhhelgeson deleted the webgpu_readonly_storage branch May 20, 2024 02:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet