使用 nannou.rs,我如何为 load_from_image_buffer 方法提供正确的参数?

huangapple go评论59阅读模式
英文:

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

问题

我目前正在尝试学习Nannou.rs。我生成了一个Luma8图像(对应于Perlin高度图),并尝试在我的应用程序窗口中显示它,使用了nannou::wgpu::Texture 中的load_from_image_buffer函数来实现,如下所示:

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 }
}

正如您所看到的,在这个片段中,我没有定义devicequeueusage参数。我尝试了多种方法,但都没有成功,而在线资源相对较少。

所以我的问题实际上是如何提供这些参数?

我首先尝试使用from_image函数,它可以工作,但由于我正试图了解库的用法,我对这个特定函数的使用很感兴趣。此外,这些参数也被许多其他方法所需,我需要理解它们。

  • 上面片段中导入的wgpu模块是nannou::wgpu,而不是直接使用wgpu crate。

  • NoiseBuilder::generate_image 返回一个ImageBuffer<Luma<u8>, Vec<u8>>变量。

英文:

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: &amp;App) -&gt; Model {
    let img_buf = NoiseBuilder::generate_image(256, 8, 8, None);

    let texture = wgpu::Texture::load_from_image_buffer(device, queue, usage, &amp;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&lt;Luma&lt;u8&gt;, Vec&lt;u8&gt;&gt; variable.

答案1

得分: 1

你可以使用特性方法 with_device_queue_pair,该方法在特性 WithDeviceQueuePair 中定义,可以为 AppWindow 实现。

用法与普通的 wgpu::TextureUsages 相同。

因此,如果你想模仿纹理的 from_path 函数,你可以尝试像这样操作(未经测试):

fn model(app: &amp;App) -&gt; 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, &amp;img_buf).unwrap();

        Model { texture }
    })
}
英文:

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: &amp;App) -&gt; 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, &amp;img_buf).unwrap();

        Model { texture }
    })
}

huangapple
  • 本文由 发表于 2023年2月16日 06:15:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75465929.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定