Skip to content

Fix #8756: Allow computed array indexing on non-storage vectors#8768

Open
SOUMITRO-SAHA wants to merge 2 commits intoprocessing:dev-2.0from
SOUMITRO-SAHA:fix-8756-array-indexing
Open

Fix #8756: Allow computed array indexing on non-storage vectors#8768
SOUMITRO-SAHA wants to merge 2 commits intoprocessing:dev-2.0from
SOUMITRO-SAHA:fix-8756-array-indexing

Conversation

@SOUMITRO-SAHA
Copy link
Copy Markdown

Resolves #8756

Changes:

This PR fixes the bug where get() could only be used on storage buffers in p5.strands, preventing array indexing (arr[0]) on non-storage vectors like those returned from helper functions.

Root Cause:

The transpiler converted ALL computed member access (arr[0]) to .get() calls, but the get() method in strands_node.js had a guard that only allowed it for storage buffers, throwing "get() can only be used on storage buffers" for non-storage vectors.

  • src/strands/strands_node.js
  • src/strands/strands_transpiler.js
  • src/webgl/strands_glslBackend.js (Added ARRAY_ACCESS handler, missing from GLSL backend - flagged by @Nixxx19 ).
  • test/unit/webgpu/p5.Shader.js

Screenshots of the change:

PR Checklist

- Enable array indexing for storage and vector buffers
- Added validation for array literals in shaders (2-4 elements only)
Copy link
Copy Markdown
Contributor

@davepagurek davepagurek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this, the code is looking good!

Comment thread src/strands/strands_transpiler.js Outdated
}

if (node.elements.length < 2 || node.elements.length > 4) {
throw new Error(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can use this helper for consistency?

export function userError(errorType, errorMessage) {

return `${parentExpr}.${node.swizzle}`;
}
if (node.opCode === OpCode.Binary.ARRAY_ACCESS) {
const [bufferID, indexID] = node.dependsOn;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we could add a test for the WebGL functionality too, in addition to the WebGPU tests?

@SOUMITRO-SAHA
Copy link
Copy Markdown
Author

Thanks for the review, @davepagurek Great suggestions — I'll address both:

1. userError helper — Good catch. I'll update the error in strands_transpiler.js to use userError('type error', ...) from strands_FES.js for consistency with other strands error messages.

2. WebGL tests — Absolutely.

I'll push the updates shortly.

@SOUMITRO-SAHA
Copy link
Copy Markdown
Author

Hi @davepagurek , I've pushed the changes addressing both of your review comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants