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