servo / servo

Servo, the embeddable, independent, memory-safe, modular, parallel web rendering engine
https://servo.org
Mozilla Public License 2.0
27.9k stars 2.98k forks source link

webgpu: Make uploading data to wr with less copies #33368

Open sagudev opened 1 week ago

sagudev commented 1 week ago

Current webgpu rendering pipeline does 3 copies (more overall description in https://servo.org/blog/2020/08/30/gsoc-webgpu/#presentation):

  1. copy texture to buffer: https://github.com/servo/servo/blob/f6ae05007751968f90a702b15c8b5083453ad8c7/components/webgpu/wgpu_thread.rs#L1124
  2. map buffer and make it owned by: https://github.com/servo/servo/blob/f6ae05007751968f90a702b15c8b5083453ad8c7/components/webgpu/wgpu_thread.rs#L1154-L1160
  3. then when webrender accesses external image we clone it again: https://github.com/servo/servo/blob/f6ae05007751968f90a702b15c8b5083453ad8c7/components/webgpu/lib.rs#L116

I think we can avoid last two copies by simply passing mapped buffer to wr, then replacing it with new mapped buffer and unmapping old one.

Technically we could pass underlying texture to wr, but only if wgpu used opengl backend (because wr uses opengl).

sagudev commented 1 week ago

As noted in #33368, I think we can remove it as well by using Mutex on GPUPresentationBuffer, so wr will lock/unlock it when used, but this could increase contention with update_wr_image where we are replacing with new buffer.