-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Conversation
crates/bevy_math/Cargo.toml
Outdated
glam = { version = "0.14.0", features = ["serde"], path = "../../../glam-rs" } | ||
crevice = { version = "0.6", optional = true, path = "../../../crevice" } |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
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. |
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
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
Closing this now that |
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 intoStd140
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 andStd140
primitives.Dynamic sizing of
WriteStd140
is also not without cost, since it serializes the type toio::Sink()
and useswriter.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 ofLightsUniform
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.