Skip to content

Add friendly FES error for dimension mismatch on shared variable assi…#8823

Open
harshiltewari2004 wants to merge 1 commit into
processing:dev-2.0from
harshiltewari2004:strands-dimension-check
Open

Add friendly FES error for dimension mismatch on shared variable assi…#8823
harshiltewari2004 wants to merge 1 commit into
processing:dev-2.0from
harshiltewari2004:strands-dimension-check

Conversation

@harshiltewari2004
Copy link
Copy Markdown
Contributor

Resolves #8812

Changes

Adds a friendly error for the case where a value of the wrong dimension is assigned to a p5.strands shared variable. Previously, code like:

js let worldPosX = sharedVec3(); worldPosX = inputs.position.x; // scalar assigned to vec3 ​

would emit invalid shader source (vec3 = float) and bubble up as a cryptic GPU shader-compiler message such as ERROR: 0:98: 'assign' : cannot convert from 'highp float' to 'highp 3-component vector of float', with no way for the user to map it back to the JS variable that caused it.

After this PR, the same code throws at IR-construction time with:

[p5.strands dimension mismatch]: Cannot assign a value of dimension 1 to `worldPosX`, which expects dimension 3. ​

This follows the four-call-site approach discussed in the plan comment on #8812 (factor out a shared helper, throw early via the existing userError convention).

Implementation

A new dimensionMismatchError(declaredDim, actualDim, varName) helper in src/strands/strands_FES.js is wired into the four sites that produce assignment IR nodes for strands-tracked variables:

  1. bridge() in src/strands/strands_node.js — primary path for sharedX = value after the transpiler rewrites the assignment to .bridge(...).
  2. bridgeSwizzle() in src/strands/strands_node.js — for swizzle writes like sharedX.xyz = value. Declared dimension comes from swizzlePattern.length.
  3. swizzleTrap.set in src/strands/ir_builders.js — the existing partial dimension check was refactored to use the shared helper, preserving the broadcast (dim === 1) and exact-match cases.
  4. Object.defineProperty setter in src/strands/strands_api.js — for hook-struct field writes like worldInputs.position = newVec. Declared dimension comes from propertyType.dataType.dimension.

Broadcasting from a scalar (dim === 1) is preserved as before, consistent with non-shared assignment behavior.

Tests

Three tests added in both test/unit/webgl/p5.Shader.js and test/unit/webgpu/p5.Shader.js under the p5.strands suite:

  • reports a friendly error when assigning a scalar to a sharedVec3 (bridge)
  • reports a friendly error on dimension mismatch via swizzle write (bridgeSwizzle)
  • does not error when shared variable assignment dimensions match

Test infrastructure note

Two small adjustments in test/unit/webgl/p5.Shader.js weren't part of the fix itself but were needed for the tests to run cleanly:

  • The vi.mock factory for strands_FES was extended to include dimensionMismatchError. The existing mock only stubbed userError and internalError, so the new import broke module loading.
  • The afterEach mock-clearing hook in the p5.strands error messages suite was changed to beforeEach. The original only cleaned up after each test, so the suite's first test inherited mockUserError.mock.calls[0] from any prior test in the file that had fired a userError. This was a latent fragility that the new tests happened to expose — beforeEach defends against it generally.

Screenshots of the change

N/A — error-message change only. Before/after messages included above.

PR Checklist

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.

1 participant