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

Method set

src/webgpu/p5.RendererWebGPU.js:314–358  ·  view source on GitHub ↗

* Updates a single element in the buffer at a given index. Use this * when only a small number of elements need to change. If you need to * replace all the data at once, use * `update()` instead. * * ```js * let buf; * *

(index, value)

Source from the content-addressed store, hash-verified

312 * struct buffers.
313 */
314 set(index, value) {
315 const device = this._renderer.device;
316
317 if (this._schema !== null) {
318 // buffer was created with an array of structs
319 if (typeof value !== 'object' || value === null || Array.isArray(value)) {
320 throw new Error(
321 'set() expects a plain object matching the original struct format for this buffer'
322 );
323 }
324
325 const { stride } = this._schema;
326 const byteOffset = index * stride;
327
328 if (byteOffset + stride > this.size) {
329 throw new Error(
330 `set() index ${index} is out of bounds for this buffer ` +
331 `(buffer holds ${Math.floor(this.size / stride)} elements)`
332 );
333 }
334
335 // pack just this one element using the same logic as update()
336 const packed = this._renderer._packStructArray([value], this._schema);
337 // use packed.buffer (ArrayBuffer) so the size arg is always in bytes
338 device.queue.writeBuffer(this.buffer, byteOffset, packed.buffer, 0, stride);
339 } else {
340 // buffer was created with a float array
341 if (typeof value !== 'number') {
342 throw new Error(
343 'set() expects a number for this float buffer'
344 );
345 }
346
347 const byteOffset = index * 4;
348
349 if (byteOffset + 4 > this.size) {
350 throw new Error(
351 `set() index ${index} is out of bounds for this buffer ` +
352 `(buffer holds ${Math.floor(this.size / 4)} floats)`
353 );
354 }
355
356 device.queue.writeBuffer(this.buffer, byteOffset, new Float32Array([value]));
357 }
358 }
359 }
360
361 /**

Callers 11

readMethod · 0.45
_initShaderMethod · 0.45
_finalizeShaderMethod · 0.45
_promoteToFramebufferMethod · 0.45
_packFieldMethod · 0.45
getSamplerMethod · 0.45
createStorageMethod · 0.45
readFramebufferPixelsMethod · 0.45
readFramebufferRegionMethod · 0.45

Calls 1

_packStructArrayMethod · 0.80

Tested by

no test coverage detected