Skip to content

[p5.js 2.0+ Bug Report]: get() can only be used on storage buffers #8756

@eupthere

Description

@eupthere

Most appropriate sub-area of p5.js?

  • Accessibility
  • Color
  • Core/Environment/Rendering
  • Data
  • DOM
  • Events
  • Image
  • IO
  • Math
  • Typography
  • Utilities
  • WebGL
  • WebGPU
  • p5.strands
  • Build process
  • Unit testing
  • Internationalization
  • Friendly errors
  • Other (specify if possible)

p5.js version

2.2.3

Web browser and version

Chrome 147.0.7727.137

Operating system

MacOSX 26.4.1

Steps to reproduce this

Steps:

  1. Create a WebGPU sketch that uses buildComputeShader().
  2. Inside the compute callback, define a helper function that returns an array, for example return [1, 2].
  3. Read from that returned value using computed indexing like arr[0].
  4. Call buildComputeShader(errorCallback) and then try errorShader.inspectHooks().

Snippet:

let shader;
let errorShader;
let storage;
const COUNT = 10;

async function setup() {
  await createCanvas(400, 400, WEBGPU);
  noLoop();
  background(100);
  storage = createStorage(new Float32Array(COUNT));

  shader = buildComputeShader(callback);
  shader.inspectHooks();
  console.log("======Compute shader source:======");
  console.log(shader.computeSrc());
  console.log("================================");
  
  errorShader = buildComputeShader(errorCallback);
  errorShader.inspectHooks();
  console.log("======Error shader source:======");
  console.log(errorShader.computeSrc());
  console.log("================================");
}

function errorCallback() {
  let data = uniformStorage(storage);
  function getArray() {
    return [1, 2];
  }
  let arr = getArray();
  data[index.x] = arr[0] + arr[1] + index.x;
}

function callback() {
  let data = uniformStorage(storage);
  function getNumber() {
    return float(index.x);
  }
  let num = getNumber();
  data[index.x] = num;
}

async function draw() {
  compute(shader, COUNT);
  const data = await storage.read();
  console.log(data);
}

Console error:

Uncaught (in promise) Error: get() can only be used on storage buffers

Expected behavior:

buildComputeShader(errorCallback) should either:

  • support indexing into a helper-returned array/vector-like value, or
  • fail with a clearer compile/transpile error explaining that this pattern is unsupported.

It should also ideally still be possible to inspect the generated hooks/source for debugging.

Actual behavior:

buildComputeShader(errorCallback) throws before errorShader.inspectHooks() can run, so the shader is never returned and its generated source cannot be inspected.
The scalar-return version works:

function getNumber() {
  return float(index.x);
}

but the array-return version fails when indexing the returned value with arr[0].

Additional notes:

From local repo inspection, this looks like a p5.strands transpilation issue rather than a WGSL compile error:

  • array literals are transformed into strands values
  • computed indexing like arr[0] is rewritten into .get(0)
  • .get() currently appears to only work for storage buffers

So the bug seems to be specifically that non-storage array/vector indexing is being routed through the storage-buffer access path.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions