Skip to content

Commit 68700ca

Browse files
committed
Document update()
1 parent c61618d commit 68700ca

File tree

1 file changed

+67
-12
lines changed

1 file changed

+67
-12
lines changed

src/webgpu/p5.RendererWebGPU.js

Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,16 @@ function rendererWebGPU(p5, fn) {
5050
}
5151

5252
/**
53-
* Updates the contents of the storage buffer with new data.
54-
*
55-
* The format of the new data must match the format used when the buffer
56-
* was created with <a href="#/p5/createStorage">`createStorage()`</a>:
57-
* an array of objects for struct buffers, or an array/Float32Array of
58-
* floats for plain float buffers.
53+
* Updates the data in the buffer with new values. The new data must be in
54+
* the same format as the data originally passed to
55+
* <a href="#/p5/createStorage">`createStorage()`</a>.
5956
*
6057
* @method update
6158
* @for p5.StorageBuffer
6259
* @beta
6360
* @webgpu
6461
* @webgpuOnly
65-
* @param {Array|Float32Array} data The new data to write into the buffer.
62+
* @param {Number[]|Float32Array|Object[]} data The new data to write into the buffer.
6663
*/
6764
update(data) {
6865
const device = this._renderer.device;
@@ -120,12 +117,70 @@ function rendererWebGPU(p5, fn) {
120117
}
121118

122119
/**
123-
* Represents a GPU storage buffer created by <a href="#/p5/createStorage">createStorage()</a>.
120+
* A block of data that shaders can read from, and compute shaders can also
121+
* write to. This is only available in WebGPU mode.
122+
*
123+
* Note: <a href="#/p5/createStorage">`createStorage()`</a> is the recommended
124+
* way to create an instance of this class.
125+
*
126+
* ```js example
127+
* let particles;
128+
* let computeShader;
129+
* let displayShader;
130+
* let instance;
131+
* const numParticles = 100;
132+
*
133+
* async function setup() {
134+
* await createCanvas(100, 100, WEBGPU);
135+
* particles = createStorage(makeParticles(width / 2, height / 2));
136+
* computeShader = buildComputeShader(simulate);
137+
* displayShader = buildMaterialShader(display);
138+
* instance = buildGeometry(drawParticle);
139+
* }
140+
*
141+
* function makeParticles(x, y) {
142+
* let data = [];
143+
* for (let i = 0; i < numParticles; i++) {
144+
* let angle = (i / numParticles) * TWO_PI;
145+
* let speed = random(0.5, 2);
146+
* data.push({
147+
* position: createVector(x, y),
148+
* velocity: createVector(cos(angle) * speed, sin(angle) * speed),
149+
* });
150+
* }
151+
* return data;
152+
* }
153+
*
154+
* function drawParticle() {
155+
* sphere(2);
156+
* }
157+
*
158+
* function simulate() {
159+
* let data = uniformStorage(particles);
160+
* let idx = index.x;
161+
* data[idx].position = data[idx].position + data[idx].velocity;
162+
* }
163+
*
164+
* function display() {
165+
* let data = uniformStorage(particles);
166+
* worldInputs.begin();
167+
* let pos = data[instanceID()].position;
168+
* worldInputs.position.xy += pos - [width / 2, height / 2];
169+
* worldInputs.end();
170+
* }
124171
*
125-
* Storage buffers hold data that can be read and written by compute shaders,
126-
* and read by vertex and fragment shaders. Pass a `p5.StorageBuffer` to
127-
* <a href="#/p5.Shader/setUniform">setUniform()</a> or
128-
* <a href="#/p5/uniformStorage">uniformStorage()</a> to bind it to a shader.
172+
* function draw() {
173+
* background(30);
174+
* if (frameCount % 60 === 0) {
175+
* particles.update(makeParticles(random(width), random(height)));
176+
* }
177+
* compute(computeShader, numParticles);
178+
* noStroke();
179+
* fill(255, 200, 50);
180+
* shader(displayShader);
181+
* model(instance, numParticles);
182+
* }
183+
* ```
129184
*
130185
* @class p5.StorageBuffer
131186
* @beta

0 commit comments

Comments
 (0)