Skip to content

Reading mapped buffer data yields zeros when the data is written using the mapping API #305

@almarklein

Description

@almarklein

To reproduce:

  • Create a buffer using mapped_at_creation=True and the MAP_READ flag.
  • Use wgpuBufferGetMappedRange to get mapped memory and write data to it.
  • Unmap.
  • Map the buffer with MAP_READ.
  • Use wgpuBufferGetMappedRange to get mapped memory and read data from it.
  • Data is all zeros.

If I submit an empty command buffer to the queue between unmap and mapping it again, it does work. So it looks like the written data is not actually submitted yet.

Here's some Python code illustrating the same. It uses an uncommitted version of wgpu-py (I'm working on the buffer mapping API), so consider this pseudo-code to help illustrate the issue.

import wgpu.backends.rs
import wgpu.utils

device = wgpu.utils.get_default_device()
data1 = b"abcdefghijkl"

# Create buffer with data
buf = device.create_buffer(size=len(data1), usage=wgpu.BufferUsage.MAP_READ, mapped_at_creation=True)
buf.write_mapped(data1)
buf.unmap()

# Comment this out to make the problem go away
# encoder = device.create_command_encoder()
# device.queue.submit([encoder.finish()])

# Download from buffer to CPU
buf.map(wgpu.MapMode.READ)
data2 = buf.read_mapped()
buf.unmap()
print(data2.tobytes())
assert data1 == data2

My questions are:

Is this expected behavior (from the POV of the WebGPU spec) or something we should fix?

If the intent is to fix it, where should this happen, here or wgpu-core?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions