Skip to content

feat(webgpu): add read() to p5.StorageBuffer with tests#8726

Open
aashu2006 wants to merge 2 commits intoprocessing:dev-2.0from
aashu2006:feat/storagebuffer-read
Open

feat(webgpu): add read() to p5.StorageBuffer with tests#8726
aashu2006 wants to merge 2 commits intoprocessing:dev-2.0from
aashu2006:feat/storagebuffer-read

Conversation

@aashu2006
Copy link
Copy Markdown

Resolves #8722

Changes:

I’ve added an async .read() method to p5.StorageBuffer so we can read data back from GPU buffers into JavaScript.

  • It uses a staging buffer + mapAsync to copy data from GPU -> CPU
  • For number buffers, it returns a Float32Array
  • For struct buffers, it returns plain JS objects based on the schema
  • I also added a private _unpackStructArray() helper to rebuild struct data from the packed buffer
  • This follows the same pattern already used in readFramebufferPixels

Tests

I've added unit tests for:

  1. reading float buffers
  2. reading struct buffers
  3. reading after .update()
  4. reading after GPU compute verifiying actual GPU -> CPU flow
  5. struct buffers with vector fields

Some Notes

  • Struct data is returned as plain objects for e.g. arrays for vector fields, not p5.Vector or p5.Color
  • .read() introduces a GPU - CPU sync point, so frequent use maybe can impact the performance

PR Checklist

@aashu2006
Copy link
Copy Markdown
Author

@davepagurek Hey, I’ve added the .read() implementation and tests for this, have a quick look at it whenever you have time 🙂

@p5-bot
Copy link
Copy Markdown

p5-bot bot commented Apr 16, 2026

Continuous Release

CDN link

Published Packages

Commit hash: 1aeeaef

Previous deployments

d650e1c


This is an automated message.

Comment thread src/webgpu/p5.RendererWebGPU.js Outdated

// Inverse of _packStructArray reads packed buffer back into plain JS objects
// using the same schema layout - fields, stride and offsets
_unpackStructArray(floatView, schema) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do you think we can move this to the renderer next to _packStructArray for consistency, and defer to the renderer from this class?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yes, that makes sense. I will move _unpackStructArray next to _packStructArray in the renderer and call it from here for consistency.


expect(result).to.be.an('array');
expect(result.length).to.equal(2);
// Vector fields come back as plain arrays
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ahh good catch. I guess we don't currently capture in the schema if these values came from arrays, vectors, or colors. I think we might want to record that so we can deserialize into the same format it came in as. Do you think something like that is feasible?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

yeah that’s a good point, right now we’re just returning plain data, but preserving the original types like vectors or colors would be useful.

I think it’s doable by extending the schema to store the original input type, but it might add some complexity. I can look into that as a follow-up issue if that sounds good.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think it might be worth exploring upfront, just because it changes the API a bit -- e.g. if we were to release just this then change the structure of return values, it could break users' code. (I don't think we're going to do a release in between PRs, but that's generally how I think about API changes -- we generally want to avoid, if we can, merging code with an API that we think we'll change.)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

If we think the return format might change, it’s better to settle that before merging. Would you prefer that we preserve original types like vectors or colors in this PR, or keep the current plain data approach and commit to that API for now?

@davepagurek
Copy link
Copy Markdown
Contributor

this is looking good, and thanks for the detailed tests!

@aashu2006
Copy link
Copy Markdown
Author

@davepagurek I've pushed the changes moved _unpackStructArray to the renderer and updated usage. Happy to make any other tweaks if needed!

@aashu2006 aashu2006 requested a review from davepagurek April 16, 2026 22:24
@aashu2006
Copy link
Copy Markdown
Author

@davepagurek Just wanted to follow up on the return format question, happy to proceed either way, just wanted to check what direction you'd prefer here

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.

2 participants