English 中文(简体)
编写和阅读缓冲区
原标题:Creating writing and reading from buffer wgpu

我刚刚开始学习如何计算一个全球邮联,我已决定开始与万国邮联合作,因为我非常熟悉老板,而且每万国邮联都可以运作。 就我目前的理解而言,首先我必须建立一个我方代表团可以利用的缓冲地带,我已经用以下守则这样做。

    let array_buffer = device.create_buffer(&wgpu::BufferDescriptor{
        label: Some("gpu_test"),
        size: (arr.len() * std::mem::size_of::<[i32; 5]>()) as u64,
        usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ,
        mapped_at_creation: false,
    });

之后,我向这一缓冲地带撰写了一些随机数据,内容如下。

queue.write_buffer(&array_buffer, 0, &[1,2,3,4]);

就现在而言,我没有错误,但现在我想读一下这一缓冲地带的数据时,问题就显现了,没有任何例子说明如何将数据从宽布特的缓冲中读到,我也看不到万国邮联的口号。

In addition how do I know if the buffer is accessible on CPU or GPU webGPU docs talk about it but they don t have an explicate example of how to define a buffer for each one.

最佳回答

首先,为了能够阅读缓冲器,它必须拥有<代码>。 BufferUsages:MAP_READ。 你们已经这样做了。 如果你想要看到一个能够避免这种使用的缓冲,那么你首先需要将数据复制到临时缓冲。

Given that prerequisite, the steps are:

  1. Call BufferView::map_async()
  2. Call Device::poll()
  3. Confirm that the map_async() callback has been called with a successful Result
  4. Call BufferView::get_mapped_range()

这里使用的编号I ve,该代码在变式<代码>temp_buffer<>/code中标明缓冲:

let (sender, receiver) = futures_channel::oneshot::channel();
temp_buffer
    .slice(..)
    .map_async(wgpu::MapMode::Read, |result| {
        let _ = sender.send(result);
    });
device.poll(wgpu::Maintain::Wait); // TODO: poll in the background instead of blocking
receiver
    .await
    .expect("communication failed")
    .expect("buffer reading failed");
let slice: &[u8] = &temp_buffer.slice(..).get_mapped_range();

Note that as per the TODO, this particular code is a work in progress halfway between two sensible states:

  • If you intend only to block on the buffer being ready to read, then you don t need a channel, just a OnceCell because the message will always have arrived when poll(Maintain::Wait) returns.
  • If you want to be truly async, then something needs to be calling either poll(Maintain::Poll) or Queue::submit() repeatedly in the background to trigger checking for completion and thus the callback.
问题回答

暂无回答




相关问题
Creating an alias for a variable

I have the following code in Rust (which will not compile but illustrates what I am after). For readability purposes, I would like to refer the same string with two different names so that the name of ...

Rust Visual Studio Code code completion not working

I m trying to learn Rust and installed the Rust extension for VSCode. But I m not seeing auto-completions for any syntax. I d like to call .trim() on String but I get no completion for it. I read that ...

热门标签