Skip to content

Commit

Permalink
fix memory size for PointLightBundle (bevyengine#1940)
Browse files Browse the repository at this point in the history
Introduced in bevyengine#1778, not fixed by bevyengine#1931 

The size of `Lights` buffer currently is : 
```rust
    16 // (color, `[f32; 4]`)
    + 16 // (number of lights, `f32` encoded as a `[f32; 4]`)
    + 10 // (maximum number of lights)
        * ( 16 // (light position, `[f32; 4]`
          + 16 // (color, `[16; 4]`)
          + 4 // (inverse_range_squared, `f32`)
          )

-> 392
```

This makes the pbr shader crash when running with Xcode debugger or with the WebGL2 backend. They both expect a buffer sized 512. This can also be seen on desktop by adding a second light to a scene with a color, it's position and color will be wrong.

adding a second light to example `load_gltf`:
```rust
    commands
        .spawn_bundle(PointLightBundle {
            transform: Transform::from_xyz(-3.0, 5.0, -3.0),
            point_light: PointLight {
                color: Color::BLUE,
                ..Default::default()
            },
            ..Default::default()
        })
        .insert(Rotates);
```

before fix:
<img width="1392" alt="Screenshot 2021-04-16 at 19 14 59" src="https://user-images.githubusercontent.com/8672791/115060744-866fb080-9ee8-11eb-8915-f87cc872ad48.png">

after fix:
<img width="1392" alt="Screenshot 2021-04-16 at 19 16 44" src="https://user-images.githubusercontent.com/8672791/115060759-8cfe2800-9ee8-11eb-92c2-d79f39c7b36b.png">




This PR changes `inverse_range_squared` to be a `[f32; 4]` instead of a `f32` to have the expected alignement
  • Loading branch information
mockersf authored and ostwilkens committed Jul 27, 2021
1 parent e6c0020 commit b152e34
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
5 changes: 3 additions & 2 deletions crates/bevy_pbr/src/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ impl Default for PointLight {
pub(crate) struct PointLightUniform {
pub pos: [f32; 4],
pub color: [f32; 4],
pub inverse_range_squared: f32,
// storing as a `[f32; 4]` for memory alignement
pub inverse_range_squared: [f32; 4],
}

unsafe impl Byteable for PointLightUniform {}
Expand All @@ -43,7 +44,7 @@ impl PointLightUniform {
PointLightUniform {
pos: [x, y, z, 1.0],
color,
inverse_range_squared: 1.0 / (light.range * light.range),
inverse_range_squared: [1.0 / (light.range * light.range), 0., 0., 0.],
}
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_pbr/src/render_graph/lights_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl Node for LightsNode {
#[repr(C)]
#[derive(Debug, Clone, Copy)]
struct LightCount {
// storing as a `[u32; 4]` for memory alignement
pub num_lights: [u32; 4],
}

Expand Down

0 comments on commit b152e34

Please sign in to comment.