processing / processing/p5.js

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

Open
#8,756 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Area:WebGPU p5.js 2.0+ p5.strands
Dominant language
JavaScript
Stars
24k
Forks
3.8k
Avg merge
3d 16h
Merged PRs (30d)
25

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the snippet with buildComputeShader(errorCallback) and compare it with the scalar-returning callback. Inspect the p5.strands transformation from array literals and computed indexing to .get(); done means helper-returned array indexing works or produces a clear transpilation error, while generated hooks/source remain inspectable.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
compilers, computer-graphics
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.