hadronized / luminance-rs

Type-safe, type-level and stateless Rust graphics framework
https://phaazon.github.io/learn-luminance/
Other
1.09k stars 59 forks source link

Fields of Vertex struct being reordered on new rust version results in incorrect render #616

Open nbvdkamp opened 1 year ago

nbvdkamp commented 1 year ago

After updating my nightly rust install my luminance application started rendering my models completely incorrectly, as if the attribute data is being garbled. On the 2022-11-11 nightly build it still works as expected so I'm quite sure https://github.com/rust-lang/rust/pull/102750 being merged is what caused things to break, though I haven't bisected yet.

This is my vertex struct:

#[derive(Copy, Clone, Debug, Semantics)]
pub enum VertexSemantics {
    #[sem(name = "position", repr = "[f32; 3]", wrapper = "VertexPosition")]
    Position,
    #[sem(name = "normal", repr = "[f32; 3]", wrapper = "VertexNormal")]
    Normal,
    #[sem(name = "uv", repr = "[f32; 2]", wrapper = "VertexUV")]
    TextureCoords,
}

#[derive(Copy, Clone, Vertex)]
#[vertex(sem = "VertexSemantics")]
pub struct Vertex {
    pub position: VertexPosition,
    pub normal: VertexNormal,
    pub uv: VertexUV,
}

Adding #[repr(C)] to Vertex fixes the issue so presumably luminance makes a bad assumption somewhere about vertex struct layout being not being reordered.

hadronized commented 1 year ago

Yes, they must be #[repr(C)]. I’ll add something to check that in the proc-macro.

ColonelThirtyTwo commented 1 year ago

Luminance should use memoffset::offset_of, field_offset::offset_of, or equivalent to find the offsets in a way that works with #[repr(Rust)].