Skip to content

Read-mapped buffer content incorrect unless a submission happens between write and read #5173

@nical

Description

@nical

Description

This is visible during CTS runs, but it was already filed against wgpu-native a while back in gfx-rs/wgpu-native#305.

See the test below. It creates a mapped buffer, writes data into it, unmaps it, maps it again and checks that the content matches what was written.

Repro steps

#[gpu_test]
static MAP_WITHOUT_SUBMIT: GpuTestConfiguration = GpuTestConfiguration::new().run_async(|ctx| async move {
    let buffer = ctx.device.create_buffer(&wgpu::BufferDescriptor {
        label: None,
        size: 12,
        usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
        mapped_at_creation: true,
    });

    {
        let mut mapped = buffer.slice(0..12).get_mapped_range_mut();
        assert!(mapped.len() == 12);
        for (i, elt) in mapped.iter_mut().enumerate() {
            *elt = (i %255) as u8;
        }    
    }

    buffer.unmap();

    // Uncommenting this makes the test pass
    //ctx.queue.submit([]);

    buffer.slice(0..12).map_async(wgpu::MapMode::Read, Result::unwrap);

    ctx.async_poll(wgpu::Maintain::wait())
        .await
        .panic_on_timeout();

    {
        let mapped = buffer.slice(0..12).get_mapped_range();
        assert!(mapped.len() == 12);
        for (i, elt) in mapped.iter().enumerate() {
            assert_eq!(*elt, (i %255) as u8); // Boom.
        }
    }

    buffer.unmap();
});

Expected vs observed behavior

The above test should pass but currently doesn't unless the submit call is uncommented.

Extra materials

The failing cts test is webgpu:api,operation,buffers,map:mapAsync,read:mapAsyncRegionLeft="default-expand";mapAsyncRegionRight="default-expand"

Platform
Information about your OS, version of wgpu, your tech stack, etc.

Metadata

Metadata

Labels

type: bugSomething isn't working

Type

No type

Projects

Status

In Progress

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions