MCPcopy Index your code
hub / github.com/processing/p5.js / update

Method update

src/webgpu/p5.RendererWebGPU.js:124–176  ·  view source on GitHub ↗

* Updates the data in the buffer with new values. The new data must be in * the same format as the data originally passed to * `createStorage()` . * * ```js example * let particles; * let computeShader; * let displayShader; * le

(data)

Source from the content-addressed store, hash-verified

122 * @param {Number[]|Float32Array|Object[]} data The new data to write into the buffer.
123 */
124 update(data) {
125 const device = this._renderer.device;
126
127 if (this._schema !== null) {
128 // Buffer was created with a struct array
129 if (
130 !Array.isArray(data) ||
131 data.length === 0 ||
132 typeof data[0] !== 'object' ||
133 Array.isArray(data[0])
134 ) {
135 throw new Error(
136 'update() expects an array of objects matching the original struct format'
137 );
138 }
139
140 const newSchema = this._renderer._inferStructSchema(data[0]);
141 if (newSchema.structBody !== this._schema.structBody) {
142 throw new Error(
143 `update() data structure doesn't match the original.\n` +
144 ` Expected: ${this._schema.structBody}\n` +
145 ` Got: ${newSchema.structBody}`
146 );
147 }
148
149 const packed = this._renderer._packStructArray(data, this._schema);
150 if (packed.byteLength > this.size) {
151 throw new Error(
152 `update() data (${packed.byteLength} bytes) exceeds buffer size (${this.size} bytes)`
153 );
154 }
155 device.queue.writeBuffer(this.buffer, 0, packed);
156 } else {
157 // Buffer was created with a float array
158 let floatData;
159 if (data instanceof Float32Array) {
160 floatData = data;
161 } else if (Array.isArray(data)) {
162 floatData = new Float32Array(data);
163 } else {
164 throw new Error(
165 'update() expects a Float32Array or array of numbers for this buffer'
166 );
167 }
168
169 if (floatData.byteLength > this.size) {
170 throw new Error(
171 `update() data (${floatData.byteLength} bytes) exceeds buffer size (${this.size} bytes)`
172 );
173 }
174 device.queue.writeBuffer(this.buffer, 0, floatData);
175 }
176 }
177
178 /**
179 * Reads data from a storage buffer back into JavaScript.

Callers 1

_updateTextureMethod · 0.45

Calls 2

_inferStructSchemaMethod · 0.80
_packStructArrayMethod · 0.80

Tested by

no test coverage detected