-
-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Buffer mapping API #377
Buffer mapping API #377
Conversation
I moved the test file with cases that do not work to a Gist: https://gist.github.com/almarklein/a7b59d5cf18ae979f79602115807b0b2 |
This may be ignorant but how bad is it really that you could have a view on released memory? It could just be a documented risk that users of wgpu-py need to be aware of. |
Well we could document it carefully, but it's so easy to forget, also because this is not something people are used to having to care about in Python. What makes it worse is that these views can easily end up in a different place unrelated to wgpu. That, plus the fact that segfaults are sometimes not reproducible (sometimes it will crash, sometimes you get bogus data), can result in some nasty bug tracking ... I also realized just now that the data-copy downside of only an issue when reading from the buffer. When writing it's not because Python/numpy makes use of views so much. |
Just pushed a proposal for an API. The buffer can be mapped and unmapped like the WebGPU spec, but instead of requesting an array-like object (that represents the mapped memory) to read from or write to, the buffer has methods Still have to adjust examples and tests. |
Is it guaranteed that the buffer will not be unmapped until you call the unmap function? If so, a context manager might be very nice in combination with the proposed with buffer.map(copy=False) as view:
view.do_stuff() # safe? |
The other way is that the buffer gets destroyed. |
Ran into an interesting edge-case. Filed an issue upstream: gfx-rs/wgpu-native#305 For now I fixed it here. |
This change does not actually require changes in PyGfx - it looks like we only used the parts of the API that have not changed. |
Woops, using it in the picking logic. Will prepare a pr for pygfx shortly. |
This closes #114, also updated that issue's top comment to explain the issue better.
Repeating the advantages of the WebGPU mapping API for clarity:
I've had another look at disabling the base array so that the views don't work anymore, but without success.
Therefore this proposal does not expose a mapped array to read from / write to. Instead it has two methods for this:
read_mapped()
andwrite_mapped()
. The former has a kwargcopy
that can be set to False to get the actually mapped data. Users who know what they're doing can then benefit from more performance.With this new API we support all the four points above. It's also a lot closer to the WebGPU spec: we have
map()
andunmap()
in between which the data can be read or written multiple times (fast!). But instead of obtaining a piece of the mapped memory (getMappedRange()
), we haveread_mapped()
andwrite_mapped()
, so we don't have to expose the mapped memory (except whithread_mapped(..., copy=False)
.