Skip to content

Fix required immediate slots calculation and panics when slots overflow#9725

Open
beicause wants to merge 6 commits into
gfx-rs:trunkfrom
beicause:fix-naga-immediate-slots-required
Open

Fix required immediate slots calculation and panics when slots overflow#9725
beicause wants to merge 6 commits into
gfx-rs:trunkfrom
beicause:fix-naga-immediate-slots-required

Conversation

@beicause

@beicause beicause commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Connections
Immediates issue: #8556

Description

  1. Fixes Naga immediate_slots_required calculation is incorrect #9724

  2. Fixes panic when var<immediate> is larger than 256 bytes / the bitmask overflows.

Testing

For 1, test with CTS webgpu:api,validation,encoding,programmable,pipeline_immediate:required_slots_set:*
For 2, test with CTS webgpu:api,validation,pipeline,immediates:*.

Squash or Rebase?

Squash

Checklist

  • I self-reviewed and fully understand this PR.
  • WebGPU implementations built with wgpu may be affected behaviorally.
  • Validation and feature gates are in place to confine behavioral changes.
  • Tests demonstrate the validation and altered logic works.
  • CHANGELOG.md entries for the user-facing effects of this change are present.
  • The PR is minimal, and doesn't make sense to land as multiple PRs.
  • Commits are logically scoped and individually reviewable.
  • The PR description has enough context to understand the motivation and solution implemented.

@beicause
beicause force-pushed the fix-naga-immediate-slots-required branch from 1393ae7 to 4a415f4 Compare June 23, 2026 04:14
@beicause beicause changed the title Fix naga immediate slots required Fix naga immediate_slots_required and panic when immediate size overflows 256 bytes Jun 23, 2026
@beicause beicause changed the title Fix naga immediate_slots_required and panic when immediate size overflows 256 bytes Fix naga immediate_slots_required and panic when var<immediate> size overflows 256 bytes Jun 23, 2026
@beicause
beicause force-pushed the fix-naga-immediate-slots-required branch 5 times, most recently from 12034a7 to 13ce0e8 Compare June 23, 2026 14:00
@andyleiserson andyleiserson self-assigned this Jun 24, 2026
@beicause
beicause force-pushed the fix-naga-immediate-slots-required branch from 13ce0e8 to 16a0228 Compare July 14, 2026 23:18
@beicause beicause changed the title Fix naga immediate_slots_required and panic when var<immediate> size overflows 256 bytes Fix required immediate slots calculation and panics when slots overflow Jul 14, 2026
pub const fn from_range(
offset: u32,
size_bytes: u32,
) -> Result<Self, ImmediateSlotsOverflowError> {

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.

It might be simpler to use a BitVec to store the immediate slots so we don't have to plumb this error everywhere.

Or maybe just say that Naga's "Maximum byte-size of an immediate data variable." is 256 B, and reject such modules in validation.

A third option might be to represent the invalid state internally in ImmediateSlots, so it can can have impl Default and the structs that use it can derive Default. (With a type parameter for error or Infallible, if needed.) But I think one of the other options is probably simpler.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

bit_vec::BitVec is heap allocated thus it's not suitable for this.

naga itself doesn't use ImmediateSlots. It only used in wgpu-core.

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.

naga itself doesn't use ImmediateSlots. It only used in wgpu-core

True. I was thinking of oversize immediates as something naga might want to support as valid WGSL for the interface reflection, even if not supported by wgpu or any actual implementation. But there's really no point in that.

But then I feel like it makes even less sense to plumb around ImmediateSlotsOverflowError and break derived Defaults for this case. Or put differently, it's weird for the error ImmediateSlotsOverflowError to only ever be unwrapped, never reported to a user.

What do you think about beicause/wgpu@fix-naga-immediate-slots-required...andyleiserson:jj-push-urxm? (We could also remove the overflow error, or not. I'm okay with the way it's used here, it's the way it's used in wgpu-core that bothers me.)

Comment thread naga/src/valid/immediates.rs Outdated
Comment thread CHANGELOG.md Outdated
pub const fn from_range(
offset: u32,
size_bytes: u32,
) -> Result<Self, ImmediateSlotsOverflowError> {

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.

naga itself doesn't use ImmediateSlots. It only used in wgpu-core

True. I was thinking of oversize immediates as something naga might want to support as valid WGSL for the interface reflection, even if not supported by wgpu or any actual implementation. But there's really no point in that.

But then I feel like it makes even less sense to plumb around ImmediateSlotsOverflowError and break derived Defaults for this case. Or put differently, it's weird for the error ImmediateSlotsOverflowError to only ever be unwrapped, never reported to a user.

What do you think about beicause/wgpu@fix-naga-immediate-slots-required...andyleiserson:jj-push-urxm? (We could also remove the overflow error, or not. I'm okay with the way it's used here, it's the way it's used in wgpu-core that bothers me.)

Comment thread wgpu-core/src/command/pass.rs Outdated
Comment thread wgpu-core/src/device/resource.rs Outdated
andyleiserson and others added 2 commits July 18, 2026 11:03
Co-authored-by: Andy Leiserson <aleiserson@mozilla.com>
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.

Naga immediate_slots_required calculation is incorrect

2 participants