Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to write to pixel buffer line by line? #1944

Open
lattice0 opened this issue Jun 24, 2021 · 1 comment
Open

How to write to pixel buffer line by line? #1944

lattice0 opened this issue Jun 24, 2021 · 1 comment

Comments

@lattice0
Copy link

When uploading a decoded image to a pixel buffer, sometimes the lines contain a stride. That is, the lines are bigger than the image lines, for performance reasons (alignment). So if we want to upload the image called slice, we either upload it entirely if it has no stride (linesize==width) or we upload line by line:

    //If the linesize is equal to width then we don't need to respect stride
    if linesize == width as usize {
        pixel_buffer.write(slice);
    } else {
        for line in 0..height {
            let current_line_start = (line * width) as usize;
            let s = &slice[(current_line_start)..(current_line_start + linesize)];
            pixel_buffer.write(s);
        }
    }

However, pixel_buffer.write(s) fails if s.len() is not the same as pixel_buffer's size.

Reading https://docs.rs/glium/0.30.0/glium/texture/pixel_buffer/struct.PixelBuffer.html, it's not clear how to write by pieces. It looks like map_write might do it:

pub fn map_write(&mut self) -> WriteMapping<'_, T>

Maps the buffer in memory for writing only.

But how do I write to WriteMapping?

PS: I think this is not the solution because I'd have to wait for the GPU map to occur. I just want to send data to the pixel buffer as fast as possible

@fayalalebrun
Copy link
Contributor

Uploads to the GPU are quite expensive, so with the first option it would be better to copy the image data to a Vec and then upload it to the GPU.

The second option should work and is probably better depending on your application, however I think we should extend the WriteMapping to be able to copy slices into the mapped buffer.

You could also try as_slice_mut(), and then call slice() on that. However, it seems as that API is deficient, since calling slice() consume the first mutable slice, while it should probably only take a mut reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants