0

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.

Feldspatt
  • 62
  • 8
  • What did you try, and what does "nothing worked" mean, compile time error? runtime error? expected vs actual behaviour. – cafce25 Feb 15 '23 at 22:46
  • I tried to supply the device, queue and usage parameters to the load_from_image function. But I don't know how to generate them in the context of a Nannou app. I tried to provide the app variable in place of the device, queue and usage but as it was expected the types mismatch. The code cannot run as I cannot provide this parameters. I am looking for a way to provide this parameters in the context of a Nannou app. (I suppose there must be a way to infer device, queue and usage from the App but I dont know how). – Feldspatt Feb 16 '23 at 16:46

1 Answers1

1

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 }
    })
}
frankenapps
  • 5,800
  • 6
  • 28
  • 69
  • The function itself works! I d like to precise that to be able to use the Nannou draw.to_frame method I had to add the following usage: nannou::wgpu::TextureUsages::TEXTURE_BINDING; Thanks! – Feldspatt Feb 21 '23 at 07:05