Skip to content

WGSL requires dynamic matrix indexing, which requires surprising SPIR-V #1782

@jimblandy

Description

@jimblandy

The section "Matrix Access Expression" mentions OpCompositeExtract, but that instruction only accepts literal indices, not expressions, so it's not sufficient to do the job described. The reference to OpCompositeExtract should be removed or at least qualified.

WGSL says that matrices that are not behind a pointer or reference can be dynamically indexed, but this requires surprising SPIR-V to implement.

  • Function parameters may be matrices.
  • Formal parameter expressions do not evaluate to references.
  • §6.6.2, "Matrix Access Expression", suggests that e[i] indexes a matrix if e: matNxM<T> and i: i32 or u32. There is no requirement that i be a constant, or that e be behind a pointer or reference.

But as far as I know, SPIR-V has no instruction that can dynamically index a matrix that is not behind a pointer. Naga handles the similar restriction on arrays by copying the entire array into a generated temporary variable, getting a pointer to that, and then using OpAccessChain. A bit of investigation showed that some drivers will optimize out the temporary. We could do this for matrices as well.

But one of the goals of WGSL (as discussed in gfx-rs/naga#692) is to have a trustworthy translation to and from SPIR-V. Transformations like this seem like a step away from that, but there's no other way to implement what WGSL requires.

Metadata

Metadata

Assignees

Labels

wgslWebGPU Shading Language Issues

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions