Skip to content

Add StorageBuffer.set(index, value) for single-element GPU updates#8772

Open
aashu2006 wants to merge 1 commit intoprocessing:dev-2.0from
aashu2006:feature/storage-buffer-set
Open

Add StorageBuffer.set(index, value) for single-element GPU updates#8772
aashu2006 wants to merge 1 commit intoprocessing:dev-2.0from
aashu2006:feature/storage-buffer-set

Conversation

@aashu2006
Copy link
Copy Markdown

Resolves #8749

Changes

I've added a set(index, value) method to p5.StorageBuffer that updates a single element in a buffer without rewriting the whole thing.

Before this, updating one particle out of thousands required a full GPU -> CPU read, a JS edit, then a full CPU -> GPU write back. Now you can do:

// float buffer
buf.set(2, 9.5);

// struct buffer
particles.set(42, { position: createVector(0, 0), velocity: createVector(1, 0) });

This works by using GPUQueue.writeBuffer() with a byte offset, so only one element's bytes are sent to the GPU.

What changed:

  • src/webgpu/p5.RendererWebGPU.js
    added set(index, value) to StorageBuffer
    added JSDoc and examples
  • test/unit/webgpu/p5.RendererWebGPU.js
    added 7 unit tests for float buffers, struct buffers, bounds checks, and type validation

PR Checklist

  • npm run lint passes
  • Inline reference is included / updated
  • Unit tests are included / updated

@aashu2006 aashu2006 force-pushed the feature/storage-buffer-set branch from 504f178 to e2ceec3 Compare May 9, 2026 11:18
@aashu2006
Copy link
Copy Markdown
Author

hii @davepagurek, thanks for waiting!

as there isn’t a way to update a single buffer element without re-uploading the whole buffer, it can get expensive for large cases like 10k particles.

since GPUQueue.writeBuffer() already supports byte offsets, I added a set(index, value) method that computes the element position and writes only that chunk of data.

For struct buffers the offset is index * schema.stride, and for float buffers it’s index * 4.

I reused the existing _packStructArray helper to pack a single struct element (as a one item array), then passed packed.buffer to writeBuffer so the size is handled correctly in bytes.

also added the type checks with clearer errors, plus unit tests for both float and struct buffers including checks that neighbouring elements stay unchanged.

let me know if you would like anything adjusted!

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.

1 participant