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

Use crevice to write LightsUniform #1931

Closed
wants to merge 2 commits into from
Closed

Conversation

mtsr
Copy link
Contributor

@mtsr mtsr commented Apr 15, 2021

This is a draft of what usage of crevice for writing Std140 layout looks like, see #1779.

Note that WriteStd140 is fairly low overhead, it converts glam types into Std140 primitives before serializing). AsStd140, which can be derived in many cases, potentially has more overhead, since it converts the entire struct into a version with padding and Std140 primitives.

Dynamic sizing of WriteStd140 is also not without cost, since it serializes the type to io::Sink() and uses writer.len() to calculate the size.

This draft currently depends on an upcoming PR on crevice that adds the StaticStd140Size trait, so that the maximum size of LightsUniform can be calculated.

Additionally there's draft PR to glam to implement crevice traits. This is probably the most problematic part of crevice right now. Because of the orphan rule it's impossible to implement crevice traits without newtype wrappers on foreign types. There might be an upcoming solution, Lucien has been very welcoming in discussing improvements.

Comment on lines 16 to 17
glam = { version = "0.14.0", features = ["serde"], path = "../../../glam-rs" }
crevice = { version = "0.6", optional = true, path = "../../../crevice" }
Copy link
Member

Choose a reason for hiding this comment

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

could you put your forks of those on GitHub and point to them?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@mockersf
Copy link
Member

mockersf commented Apr 15, 2021

I'm very interested in this to keep backends other than the official one (mainly WebGL2) working.

For example, the WebGL2 shader currently fails after #1778 got merged, even after applying the changes, due to a wrongly sized buffer

@mtsr
Copy link
Contributor Author

mtsr commented Apr 15, 2021

I'm very interested in this to keep backends other than the official one (mainly WebGL2) working.

For example, the WebGL2 shader currently fails after #1778 got merged, even after applying the changes, due to a wrongly sized buffer

Crap, that sucks. I need to make testing webgl part of my workflow for any future changes to shaders.

@mockersf mockersf added C-Code-Quality A section of code that is hard to understand or change dependencies A-Rendering Drawing game state to the screen labels Apr 15, 2021
bors bot pushed a commit that referenced this pull request Apr 19, 2021
Introduced in #1778, not fixed by #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
@cart cart added C-Dependencies A change to the crates that Bevy depends on and removed C-Dependencies-1 labels Jul 13, 2021
@cart cart added the S-Pre-Relicense This PR was made before Bevy added the Apache license. Cannot be merged or used for other work label Jul 23, 2021
@mockersf mockersf removed the S-Pre-Relicense This PR was made before Bevy added the Apache license. Cannot be merged or used for other work label Jul 24, 2021
ostwilkens pushed a commit to ostwilkens/bevy that referenced this pull request Jul 27, 2021
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
@james7132
Copy link
Member

Closing this now that bevy_crevice is used for all of our rendering pipelines.

@james7132 james7132 closed this Mar 7, 2022
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-Code-Quality A section of code that is hard to understand or change C-Dependencies A change to the crates that Bevy depends on
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants