-
Notifications
You must be signed in to change notification settings - Fork 953
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
Add get_mapped_range_as_array_buffer #4042
Add get_mapped_range_as_array_buffer #4042
Conversation
…avoids copying mapped data into wasm heap
11ad349
to
5e37c8d
Compare
Side note: I'm impressed with the CI setup here. So cool!! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's super useful, thanks for contributing!!
Looks all good to me 🚢
…avoids copying mapped data into wasm heap (gfx-rs#4042) Co-authored-by: Ryan Kaplan <[email protected]>
…avoids copying mapped data into wasm heap (gfx-rs#4042) Co-authored-by: Ryan Kaplan <[email protected]>
…avoids copying mapped data into wasm heap (gfx-rs#4042) Co-authored-by: Ryan Kaplan <[email protected]>
…avoids copying mapped data into wasm heap (#4042) (#4103) Co-authored-by: Ryan Kaplan <[email protected]>
If so, could I trouble you for some sample code on how to do this ? I suspect it's < 5 lines, but it is not obvious to me at all (given the type signatures of the functions added in this patch.) Thank you! |
EDIT: above is directed at @ryankaplan |
Hi @zeroexcuses! This patch is a similar flavor of problem to yours, but it's different. I don't think it'll help you directly 😢 What this PR does: previously when reading data out of a My fix is very specific to the API I was using, so I don't think it'll be helpful to you. I do think there's a general way to solve this problem that would work for both of us, but it's a lot of effort... We could make a struct called something like In addition to fixing our two cases, this would have the pretty significant upside that CPU-side geometry data and texture data wouldn't count against the 4GB wasm heap size limit that exists in many browsers. But it'd be a huge effort and I'm not sure how the community decides whether to do stuff like this. The approach is inspired by IndirectBuffer which I've used before. Hope that's helpful! Let me know if you have any other questions. |
@ryankaplan : Thank you for your detailed response. To make sure I understand what you have stated, is the following restatement correct ? Your problem (that you solved): My problem: Put another way, your problem was "copy data FROM wgpu buffer TO js/ArrayBuffer"; my problem is "copy data FROM js/ArrayBuffer TO wgpu buffer" Does this sound correct ? |
Yes, that sounds right to me! |
I do think this new api can still be an improvement for the upload case: The arraybuffer returned is owned by the WebGPU implementation and copies to it will have visible side effects after unmapping. |
Sorry, I have a dumb question. Can you post me a link to the documentation for "mapped ArrayBuffer" ? On google, the first result links to SharedArrayBuffer, which I think is something different. What is "mapped ArrayBuffer" ? Is it wasm_heap or js side ? |
It's a JavaScript object of type So, if you want to have minimal-copies data transfer, the answer is to write your original data into the mapped ArrayBuffer. If you can't do that directly, you'll have to perform at least one copy (from the ArrayBuffer you created previously to the mapped ArrayBuffer) |
Sorry, what is even "mapped memory" here? Is it mmap, or some type of hybrid-cpu-gpu-shared-memory? And if the latter, when does writing to it get sync-ed to the gpu? On every byte write? On every flush? What is the flush call? Every time the JS thread finishes execution / blocks / does an .await ? [Not trolling, genuinely ignorant.] |
You can treat it like a mmap() of GPU memory instead of a file. Exactly how it works depends on the GPU and computer architecture.
WebGPU/ |
Checklist
cargo clippy
.cargo clippy --target wasm32-unknown-unknown
if applicable.Connections
This change came out of this discussion: https://matrix.to/#/!XFRnMvAfptAHthwBCx:matrix.org/$DUm42m4q3w5SsU6oDki7aXSyLnA2RkC-zQh7Be2rhKE?via=matrix.org&via=fachschaften.org&via=jepcraft.ddns.net%3A443
There isn't a GitHub issue for this but I'm happy to create one if that would be useful.
Description
I'm using wgpu in a wasm build and occasionally want to read texture data in Javascript. Right now when I do that, the data goes from WebGPU -> Uint8Array -> Rust heap -> Uint8Array. That's a lot of copies 😅
This PR adds
Buffer::buffer_get_mapped_range_as_array_buffer
which bypasses the copy into the Rust heap. It passes the Uint8Array that rust gets from JS directly to the caller, who can then pass it back to their own JS code.Testing
This is my first change to wgpu -- please feel free to tell me to check extra things to be sure I didn't break anything.
I didn't see a way to add automated tests for this, but I've been using this new API with code that is very similar to this example and it works great: https://github.com/gfx-rs/wgpu/blob/trunk/examples/capture/src/main.rs#L158