Wgpu-rs: Documentation about aligning data before mapping it to a buffer

Created on 19 Jul 2020  路  2Comments  路  Source: gfx-rs/wgpu-rs

(While I asked this on Matrix and my issue was solved, I'm filing this here as I feel it's worth a documentation)

After https://github.com/gfx-rs/wgpu/pull/758, buffers mapped at creation are required to be aligned to 4. But, iiuc (sorry, I'm still a newbie to computer graphics...), vertices are triangular in most cases, which means the length of index data is a multiple of 3. Besides, a index is u16, not a multiple of 4 byte. So, it's a common case the user needs to add some padding to the index data by themselves, especially when the index is generated by external libraries (I got this when I was playing with lyon).

Fortunately (or unfortunately?), current examples use only the even number of length of indices, so they don't hit this problem. I think it's good to show how the user can handle the alignment issue.

Not sure if this is useful, but this is what I'm doing:

        // record the range before extending
        let stroke_range = 0..(indices.len() as u32);

        // extend
        let alignment = wgpu::COPY_BUFFER_ALIGNMENT as usize / std::mem::size_of::<u16>();
        let fraction = indices.len() % alignment;
        if fraction > 0 {
            indices
                .extend(std::iter::repeat(0).take(alignment - fraction));
        }
enhancement question

Most helpful comment

Thank you for the feedback!
How about we do this in fn create_buffer_with_data? We know the size of an element there, and we know the alignment, so we could easily request the underlying buffer to be larger, and just handle the extra elements internally.

All 2 comments

Thank you for the feedback!
How about we do this in fn create_buffer_with_data? We know the size of an element there, and we know the alignment, so we could easily request the underlying buffer to be larger, and just handle the extra elements internally.

Oh, nice. Sounds good to me!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yutannihilation picture yutannihilation  路  4Comments

m4b picture m4b  路  5Comments

RazrFalcon picture RazrFalcon  路  3Comments

dmilford picture dmilford  路  3Comments

JoshuaBatty picture JoshuaBatty  路  5Comments