Search code examples
rustwgpu-rs

Using nannou.rs, how can I provide the correct parameters to the load_from_image_buffer method?


I am currently trying to learn Nannou.rs. I generate a Luma8 image (corresponding to a Perlin heightmap) and I am trying to display it in my app's window using the function load_from_image_buffer implemented by nannou::wgpu::Texture in the model function as follow:

fn model(app: &App) -> Model {
    let img_buf = NoiseBuilder::generate_image(256, 8, 8, None);

    let texture = wgpu::Texture::load_from_image_buffer(device, queue, usage, &img_buf).unwrap();
    
    Model { texture }
}

As you can see, in this snippet I am not defining the device, queue and usage parameters. I tried multiple things but nothing worked, and online resources are rather scarce.

So my question really is how can I provide this parameters?

I played around first with the from_image function and it worked, but as I am trying to learn my way around the library I am interested in the use of this specific function. Also this parameters are required by many other methods and I ll need to understand it anyway.

  • The wgpu module imported in the snippet above is the nannou::wgpu and not directly the wgpu crate.

  • The NoiseBuilder::generate_image return an ImageBuffer<Luma<u8>, Vec<u8>> variable.


Solution

  • You can use the trait method with_device_queue_pair which is defined in the trait WithDeviceQueuePair and implemented for either App or Window.

    The usage is just the normal wgpu::TextureUsages.

    So if you would like to mimic the from_path function of the texture you could do something like this (untested):

    fn model(app: &App) -> Model {
        let img_buf = NoiseBuilder::generate_image(256, 8, 8, None);
    
        let usage = nannou::wgpu::TextureUsages::COPY_SRC |
                    nannou::wgpu::TextureUsages::COPY_DST |
                    nannou::wgpu::TextureUsages::RENDER_ATTACHMENT;
        
        src.with_device_queue_pair(|device, queue| {
            let texture = wgpu::Texture::load_from_image_buffer(device, queue, usage, &img_buf).unwrap();
    
            Model { texture }
        })
    }