映射和读取缓冲区未给出预期结果

问题描述 投票:0回答:1

我正在尝试运行一个简单的测试计算着色器。 但是当我映射缓冲区时,着色器修改我得到全 0。而不是预期的结果

numbers[index] = numbers[index] + 2u
它也不会读回缓冲区的原始值,因为缓冲区已填充 3。

以下是一些与缓冲区映射相关的代码示例:

fn setup_buffer(mut commands: Commands, render_device: Res<RenderDevice>) {
    let storage_buffer = render_device.create_buffer_with_data(&wgpu::util::BufferInitDescriptor {
        label: None,
        contents: &[3; (RESHIGHT * RESWIDTH) as usize],
        usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
    });
    commands.insert_resource(RayTracerBuffer {
        buffer: storage_buffer,
    });
}
fn read_buffer(
    raytracer_buffer: Res<RayTracerBuffer>,
    render_device: Res<RenderDevice>,
    sender_receiver: Res<SenderReceiver>,
) {
    let storage_buffer = &raytracer_buffer.buffer;
    let size = storage_buffer.size();
    let staging_buffer = render_device.create_buffer(&wgpu::BufferDescriptor {
        label: None,
        size: size,
        usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        mapped_at_creation: false,
    });
    render_device
        .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None })
        .copy_buffer_to_buffer(storage_buffer, 0, &staging_buffer, 0, size);

    let buffer_slice = staging_buffer.slice(..);
    let sender = sender_receiver.sender.clone();
    buffer_slice.map_async(wgpu::MapMode::Read, move |v| sender.send(v).unwrap());

    //thread::spawn(move || device.poll(wgpu::Maintain::Wait));
    render_device.poll(wgpu::Maintain::Wait);

    let receiver = sender_receiver.receiver.clone();
    if let Ok(Ok(())) = receiver.try_recv() {
        let data = buffer_slice.get_mapped_range();

        let result = data.to_vec();

        info!("TEST: {:?}", result);

        drop(data);
        staging_buffer.unmap();
    }
}

回顾一下,在复制/映射缓冲区的过程中的某个地方出了问题,导致只有 0 的读取,而不是预期的

numbers[index] = numbers[index] + 2u

rust compute-shader bevy
1个回答
0
投票

我发现问题了 在将缓冲区复制到暂存缓冲区后,我必须写

render_queue.submit([encoder.finish()]);

© www.soinside.com 2019 - 2024. All rights reserved.