kvark / mint

Math Interoperability Types
MIT License
256 stars 20 forks source link

#[repr(align)], GLSL and nightly #17

Open nanokatze opened 7 years ago

nanokatze commented 7 years ago

Hello,

As this is cg-related library, one would expect its types to have certain alignment constraints. E.g. in GLSL, vec3 and vec4 are 16 bytes aligned, dvec3 and dvec4 are 32 bytes aligned and so on. Unfortunately #[repr(align)] attribute is not yet present (https://github.com/rust-lang/rust/issues/33626) in stable, I think it would be nice to have alignment enabled on nightly or something.

Note that in case of library such as, say, Vulkano, even though vertex objects do not need to have fields aligned by user in them, it is not always that user works with pure vertex objects (say, in case of objects shared between compute and graphics pipelines). As of now, user would need to align and pad structure by themselves or use types padded by Vulkano's shader deriver. Using #[repr(align)] on nightly, would let user use mint for vec3 etc types without having to do extra work of padding, on nightly.

vitvakatu commented 7 years ago

I don't think passing mint types in pipelines is the purpose of this library. I thought the main mint's function is to provide unified interface for different math libraries, so it's deal of math libraries, not mint.

Besides, different API may need different alignments, it would be tricky to adjust it.

I'm not sure am I right, so let's wait for @kvark

nanokatze commented 7 years ago

The reason I would suggest this is because it would allow any library that works with mint types work with the library that takes mint types.

kvark commented 7 years ago

Thanks for filing the issue! It's an interesting suggestion.

I don't think passing mint types in pipelines is the purpose of this library.

Mind you, gfx-rs already allows mint types in the constant buffers and uniforms. Not saying it's correct, but just FYI.

Besides, different API may need different alignments, it would be tricky to adjust it.

That definitely needs some investigation.

nanokatze commented 7 years ago

I myself am yet to find any kind of API in existence that would take stricter alignments (for example 32 bytes for 4 32 bit floats) or just behave weirdly but that's fair.

LPGhatguy commented 4 years ago

A few people have tried to tackle this problem in the last couple months. I don't think that making the mint types themselves work directly is correct because things like matrices need to be laid out differently even just within GLSL (std140 vs std430).

I wrote a crate recently that uses mint types and supports turning them into structs laid out to be compatible with GLSL: https://github.com/LPGhatguy/crevice

There is also https://github.com/rustgd/glsl-layout which takes advantage of #[repr(align)], but it leaves padding bytes uninitialized and does not support mint yet.