Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - wasm: pad globals uniform also in 2d #6643

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions crates/bevy_sprite/src/mesh2d/mesh2d_view_types.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ struct Globals {
// Frame count since the start of the app.
// It wraps to zero when it reaches the maximum value of a u32.
frame_count: u32,
#ifdef SIXTEEN_BYTE_ALIGNMENT
// WebGL2 structs must be 16 byte aligned.
_wasm_padding: f32
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it not be more robust to align the first member with #[cfg_attr(feature = "webgl", align(16))] in the Rust and @align(16) in the WGSL, rather than have to maintain the size of the padding member?

Copy link
Contributor

Choose a reason for hiding this comment

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

Indeed, adding alignment attributes to the first or last (or middle!) members works.

// shader
#ifdef SIXTEEN_BYTE_ALIGNMENT
    @align(16)
#endif
    time: f32,

// rust
    #[cfg_attr(feature = "webgl", align(16))]
    time: f32,

Interestingly, as far as I can tell, this doesn't actually result in any additional padding in the generated glsl. So this seems to be more about appeasing wgpu rather than an actual webgl2 limitation?

// glsl (manual padding)
struct Globals {
    float time;
    float delta_time;
    uint frame_count;
    float _wasm_padding;
};

// glsl (alignment attributes) 
struct Globals {
    float time;
    float delta_time;
    uint frame_count;
};

I had previously attempted various alignment and padding attributes without luck (likely user error), but was lead to believe by gfx-rs/wgpu#2832 (comment) that manual padding was the way to go.

I'm in favor of this quick fix that is consistent with what we already have on the 3d side, but perhaps we should open an issue to track/investigate the issue further? It seems like pure coincidence that this is the only place where the issue shows up.

Copy link
Member

Choose a reason for hiding this comment

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

I agree that this is worth investigating. Opened an issue here: #6685

#endif
}