Skip to content
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 (Js)ArrayBuffer version for create_buffer_init #4128

Open
zeroexcuses opened this issue Sep 9, 2023 · 4 comments
Open

Add (Js)ArrayBuffer version for create_buffer_init #4128

zeroexcuses opened this issue Sep 9, 2023 · 4 comments
Labels
area: performance How fast things go help required We need community help to make this happen.

Comments

@zeroexcuses
Copy link

Problem: I want to upload to a Vertex Buffer Object directly from a https://rustwasm.github.io/wasm-bindgen/api/js_sys/struct.ArrayBuffer.html

Current Best Solution: We have to do ArrayBuffer -> &[u8], then internally, wgpu probably does &[u8] -> ArrayBuffer. This is via this function / struct pair:

impl DeviceExt for crate::Device {
    fn create_buffer_init(&self, descriptor: &BufferInitDescriptor<'_>) -> crate::Buffer {

pub struct BufferInitDescriptor<'a> {
    /// Debug label of a buffer. This will show up in graphics debuggers for easy identification.
    pub label: crate::Label<'a>,
    /// Contents of a buffer on creation.
    pub contents: &'a [u8],
    /// Usages of a buffer. If the buffer is used in any way that isn't specified here, the operation
    /// will panic.
    pub usage: crate::BufferUsages,
}

On the wasm32 platform, I'd like a way to bypass these two extra copies and directly do ArrayBuffer -> VBO.

Thanks!

@Wumpf
Copy link
Member

Wumpf commented Sep 10, 2023

As of #4042 you can map buffers directly into ArrayBuffer which solves the issue, but create_buffer_init is missing an ArrayBuffer version

@Wumpf Wumpf changed the title wgpu on wasm32/Chrome: allow upload vertex buffer directly from JsArrayBuffer Add (Js)ArrayBuffer version for create_buffer_init Sep 10, 2023
@Wumpf Wumpf added help required We need community help to make this happen. area: performance How fast things go labels Sep 10, 2023
@zeroexcuses
Copy link
Author

I apologize for missing something obvious.

All the functions I see in #4042 return a js::ArrayBuffer , rather than take a js::ArrayBuffer as an argument.

How am I supposed to use the functions in #4042 when my goal is to upload the contents of a js::ArrayBuffer into a vertex-buffer-object ?

Thanks!

@Wumpf
Copy link
Member

Wumpf commented Sep 11, 2023

Hmm not sure if I'm a bit confused about this myself, the PR I linked does after all advertise it for readback as prime motivator.
However, what it allows doing is to get the mapping of a buffer directly as ArrayBuffer, so you can now directly copy to that and then unmap the buffer, so that's already a little bit better. Before that change, a mapping would again go through a &[u8] step on the wasm heap.
So, having the array buffer on a variant of the BufferInitDescriptor would ofc be even better because then that would be just what's piped through to the buffer creation, meaning you wouldn't need to do a copy from your ArrayBuffer to the mapped one

@Wumpf
Copy link
Member

Wumpf commented Sep 13, 2023

brought this up on the wgpu maintainer call and confirmed the above.
We'll need ArrayBuffer versions both for BufferInitDescriptor and Queue::write_buffer. But using the method from #4042 should work albeit it still requires a copy (one less!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: performance How fast things go help required We need community help to make this happen.
Projects
None yet
Development

No branches or pull requests

2 participants