I’ve recently been playing with structs whose layout isn’t known until runtime, GLSL uniform blocks to be exact. When using them, the OpenGL driver decides which layout is performant/possible for the hardware, and the API has means for querying field names and the associated field offsets and other related info.
So if I want to have a Vec of these dynamic structs, I can’t just have
Vec<u8> as storage buffer would seem nice base to build on, because offset calculations would be easy to do, but I’m not sure if the vectors contents are “safely” aligned. I dug into the Vec allocation code a bit, and it seemed like it calls (at least when using jemalloc) an allocation function that guarantees the returned pointer is aligned to the requested alignment but nothing else (which in case of Vec is derived from the type parameter, so alignment of 1 in case of u8).
Should I maybe then use
Vec<u64> or perhaps
Vec<usize> to make sure the buffer gets allocated with a safe alignment? (I’m assuming here that the field offsets returned by OpenGL are properly aligned.) Or maybe skipping Vec altogether in this case would be better?
Edit: I was sloppy and only now noticed that type parameters within
<X> aren’t shown properly unless the text is within backticks!