Rust/wasm32 : wgpu upload a JsArrayBuffer directly


  1. We are using Rust on wasm32/wgpu.

  2. We are uploading a vertex buffer object.

  3. We are using:

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,

Notice the contents: &'a [u8]`

Now, here is the thing. I loaded the data over the network. I already have the data in the format of JsArrayBuffer. Instead of (1) converitng JsArrayBuffer to Vec<u8>, (2) passing &[u8] to wgpu, and (3) having wgpu convert the &[u8] back to a JsArrayBuffer -- instead of all that bs, can I just pass wgup a JsArrayBuffer intead ?


I've searched through wgpu's api and couldn't find any method for this. Try opening an issue to request a method to be added for this.

Thanks for checking. I filed report at wgpu on wasm32/Chrome: allow upload vertex buffer directly from JsArrayBuffer · Issue #4128 · gfx-rs/wgpu · GitHub

On wasm32/Chrome, I'm currently using WebGL2 backend. Do I know if there is a way I can grap the "webgl2 buffer object" from a "wgpu::Buffer" ? For now, I can do an ugly hack where I manually upload via the WebGL2/wasm32 API, which I've used before.

The main problem is: given a wgpu::Buffer, how do I grab the underlying WebGL2::buffer ?

It seems all I need is this writeBuffer api:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.