crevice icon indicating copy to clipboard operation
crevice copied to clipboard

Generate paddings directly into struct

Open mikialex opened this issue 2 years ago • 2 comments

I'm looking for a library for handling shader layouts in rust. From the example shown in README, The derive(AsStd140) seems to generate a new std140 layout struct and provide a method to convert between the origin struct. I wonder if there is possible to directly insert the padding into the origin struct, to avoid possible memory copy or conversion runtime cost(I don't sure even if the conversion is trivial, the compiler is able to eliminate the cost) and code bloat.

The other question is if it's possible to directly allow users custom math library if the library implements an unsafe trait that constraint and maps the math primitive to a shader primitive struct. This will also remove the math lib conversion costs.

Finally thanks for creating such a library, the way to generate the padding function using the const function is pretty inspiring.

mikialex avatar Feb 23 '22 04:02 mikialex

Crevice doesn't insert padding directly into your struct mostly because the generated padding members make initialization pretty bad. The user's best option becomes...

#[derive(Std140)]
struct Foo {
    a: f32,
    b: f32,
}

let x = Foo {
    a: 1.0,
    b: 2.0,
    ..Default::default()
};

This is pretty rough; most datatypes users fit into structs like this implement Default, so it's easy to forget to initialize an actual field!

In the end, the conversions that Crevice generates today should be optimized away by compiler passes. I haven't verified that, which is probably something that would be good to do (see #10)

I could see deriving Std140/Std430 directly being an option, maybe if Crevice can instead verify that your padding is correct instead of generating it for you?

LPGhatguy avatar Feb 23 '22 17:02 LPGhatguy

I think initialization by ..Default::default() is ok for me . yeah, if the Crevice could support both converts, modify, and verify these three use cases that would be best...

mikialex avatar Feb 24 '22 07:02 mikialex