-
-
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
Replacing #[repr(C)] for uniforms with a better alternative #1779
Comments
If there's agreement on a crate I can make a PR. |
Note that both of those crates introduce extra intermediary type that has correct layout, instead of modifying the layout of the struct directly. That means there is an extra conversion step when writing data to the GPU buffer. It won't be as fast as plain memcpy. |
Ok, that seems suboptimal indeed. Although I'd be interested to see how it actually compiles and how it performs. Because right now we also don't do a plain memcpy everywhere, because we're doing multiple writes of data from different components in some cases and creating an intermediate struct with combined data in other cases. And creating a temporary struct means doing one set of smaller copies and then one bigger memcpy, which might not necessarily perform much worse than what we already do. But it seems safe to assume that if it is indeed costing performance we could do better with a custom derive that writes to a buffer directly. In the meantime, do people agree that this alignment issue is worth solving? Is it bad enough that we want to use a potentially suboptimal solution for the time being to prevent errors cause by this? Even if we think Bevy developers are fine, we might want to consider end-users that write their own things. |
I believe this is what One concern is the cost of this extra step in non-optimized builds, but this ship has probably already sailed :) Of course none of that matters for non-vec types. When writing only few fields you don't want to use memcpy anyway, and speed is not an issue. What I had in mind as an alternative is a macro that directly modifies the annotated struct by adding the padding fields, but this also has it's drawbacks. Namely, you have to choose one specific format (std140/std430). Also the inserted padding fields prevent usage of exhaustive struct initializers (because you would have to name the padding fields). So that leaves you with ugly So, I believe we should use Also a wording nitpick. Whatever we choose, we don't technically get rid of |
To consider, in addition of structs that are For example, in #1798 I added an extra byte of padding to a |
Closing this now that |
What problem does this solve or what need does it fill?
Memory layout in GLSL is diffent to
#[repr(C)]
that is currently used on lights, rects and sprites. This will break silently when you add an extra field or otherwise accidentally mess up layout.See the GLSL specs.
What solution would you like?
Learn-WGPU discusses this here and mentions two crates that help solve this issue by deriving
Uniform
orAsStd140
orAsStd430
:An initial look at both crates makes
crevice
look more attractive, because it has support forAsStd430
and mint typesWhat alternative(s) have you considered?
Manually verifying memory layout.
Additional context
The text was updated successfully, but these errors were encountered: