Skip to content

Commit

Permalink
Fix post_processing example on webgl2 (#9361)
Browse files Browse the repository at this point in the history
# Objective

The `post_processing` example is currently broken when run with webgl2.

```
cargo run --example post_processing --target=wasm32-unknown-unknown
```

```
wasm.js:387 panicked at 'wgpu error: Validation Error

Caused by:
    In Device::create_render_pipeline
      note: label = `post_process_pipeline`
    In the provided shader, the type given for group 0 binding 2 has a size of 4. As the device does not support `DownlevelFlags::BUFFER_BINDINGS_NOT_16_BYTE_ALIGNED`, the type must have a size that is a multiple of 16 bytes.
```

I bisected the breakage to c7eaedd.

## Solution

Add padding when using webgl2
  • Loading branch information
rparrett authored Aug 4, 2023
1 parent 9a87890 commit e1e2407
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 4 additions & 0 deletions assets/shaders/post_processing.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ var screen_texture: texture_2d<f32>;
var texture_sampler: sampler;
struct PostProcessSettings {
intensity: f32,
#ifdef SIXTEEN_BYTE_ALIGNMENT
// WebGL2 structs must be 16 byte aligned.
_webgl2_padding: vec3<f32>
#endif
}
@group(0) @binding(2)
var<uniform> settings: PostProcessSettings;
Expand Down
10 changes: 8 additions & 2 deletions examples/shader/post_processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ impl FromWorld for PostProcessPipeline {
ty: BindingType::Buffer {
ty: bevy::render::render_resource::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
min_binding_size: Some(PostProcessSettings::min_size()),
},
count: None,
},
Expand Down Expand Up @@ -338,6 +338,9 @@ impl FromWorld for PostProcessPipeline {
#[derive(Component, Default, Clone, Copy, ExtractComponent, ShaderType)]
struct PostProcessSettings {
intensity: f32,
// WebGL2 structs must be 16 byte aligned.
#[cfg(feature = "webgl2")]
_webgl2_padding: Vec3,
}

/// Set up a simple 3D scene
Expand All @@ -359,7 +362,10 @@ fn setup(
},
// Add the setting to the camera.
// This component is also used to determine on which camera to run the post processing effect.
PostProcessSettings { intensity: 0.02 },
PostProcessSettings {
intensity: 0.02,
..default()
},
));

// cube
Expand Down

0 comments on commit e1e2407

Please sign in to comment.