MCPcopy
hub / github.com/processing/p5.js / createStorage

Method createStorage

src/webgpu/p5.RendererWebGPU.js:3608–3702  ·  view source on GitHub ↗
(dataOrCount)

Source from the content-addressed store, hash-verified

3606 }
3607
3608 createStorage(dataOrCount) {
3609 const device = this.device;
3610
3611 // Struct array: an array of plain objects
3612 if (Array.isArray(dataOrCount) && dataOrCount.length > 0 &&
3613 typeof dataOrCount[0] === 'object' && !Array.isArray(dataOrCount[0])) {
3614 if (!p5.disableFriendlyErrors && dataOrCount.length > 1) {
3615 const firstKeys = Object.keys(dataOrCount[0]);
3616 let warned = false;
3617 for (let i = 1; i < dataOrCount.length; i++) {
3618 const el = dataOrCount[i];
3619 const elKeys = Object.keys(el);
3620 const sameKeys = firstKeys.length === elKeys.length &&
3621 firstKeys.every((k, j) => k === elKeys[j]);
3622 if (!sameKeys) {
3623 p5._friendlyError(
3624 `Element ${i} has different fields than element 0. ` +
3625 `All elements should have the same properties.`,
3626 'createStorage'
3627 );
3628 break;
3629 }
3630 for (const key of firstKeys) {
3631 const firstType = this._jsValueToWgslType(dataOrCount[0][key]);
3632 const elType = this._jsValueToWgslType(el[key]);
3633 if (firstType !== elType) {
3634 p5._friendlyError(
3635 `The "${key}" property of element ${i} has type ${elType} ` +
3636 `but element 0 has type ${firstType}. Proporties should have the same type across all elements.`,
3637 'createStorage'
3638 );
3639 warned = true;
3640 break;
3641 }
3642 }
3643 if (warned) break;
3644 }
3645 }
3646 const schema = this._inferStructSchema(dataOrCount[0]);
3647 const packed = this._packStructArray(dataOrCount, schema);
3648 const size = packed.byteLength;
3649 const buffer = device.createBuffer({
3650 size,
3651 usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST | GPUBufferUsage.COPY_SRC,
3652 mappedAtCreation: true,
3653 });
3654 new Float32Array(buffer.getMappedRange()).set(packed);
3655 buffer.unmap();
3656 const storageBuffer = new StorageBuffer(buffer, size, this, schema);
3657 this._storageBuffers.add(storageBuffer);
3658 return storageBuffer;
3659 }
3660
3661 // Determine buffer size and initial data
3662 let size, initialData;
3663 if (typeof dataOrCount === 'number') {
3664 // createStorage(count) - zero-initialized
3665 size = dataOrCount * 4; // floats are 4 bytes

Callers 4

p5.Shader.jsFile · 0.80
webgpu.jsFile · 0.80
renderer3DFunction · 0.80

Calls 6

_jsValueToWgslTypeMethod · 0.95
_inferStructSchemaMethod · 0.95
_packStructArrayMethod · 0.95
_friendlyErrorMethod · 0.45
setMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected