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

Conversation

mockersf
Copy link
Member

Objective

  • Fix a panic in wasm when using globals in a shader

Solution

@mockersf mockersf added A-Rendering Drawing game state to the screen O-Web Specific to web (WASM) builds labels Nov 16, 2022
@mockersf mockersf added this to the 0.9.1 milestone Nov 16, 2022
@mockersf mockersf added the C-Bug An unexpected or incorrect behavior label Nov 16, 2022
Copy link
Contributor

@rparrett rparrett left a comment

Choose a reason for hiding this comment

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

Oops. Fix looks good, tested by 2d-ifying animate_shader.

@james7132 james7132 added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Nov 16, 2022
@@ -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

@cart
Copy link
Member

cart commented Nov 18, 2022

bors r+

bors bot pushed a commit that referenced this pull request Nov 18, 2022
# Objective

- Fix a panic in wasm when using globals in a shader

## Solution

- Similar to #6460
@bors bors bot changed the title wasm: pad globals uniform also in 2d [Merged by Bors] - wasm: pad globals uniform also in 2d Nov 18, 2022
@bors bors bot closed this Nov 18, 2022
cart pushed a commit that referenced this pull request Nov 30, 2022
# Objective

- Fix a panic in wasm when using globals in a shader

## Solution

- Similar to #6460
ItsDoot pushed a commit to ItsDoot/bevy that referenced this pull request Feb 1, 2023
# Objective

- Fix a panic in wasm when using globals in a shader

## Solution

- Similar to bevyengine#6460
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Rendering Drawing game state to the screen C-Bug An unexpected or incorrect behavior O-Web Specific to web (WASM) builds S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants