diff --git a/CHANGELOG.md b/CHANGELOG.md index d5d770eff..b3d0ab6c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -143,6 +143,7 @@ By @beholdnec in [#8505](https://github.com/gfx-rs/wgpu/pull/8505). - Added support for GLSL passthrough. By @inner-daemons in [#9064](https://github.com/gfx-rs/wgpu/pull/9064). - Implement `Adapter::new_external()` for WebGL2 (just like EGL/WGL) to import an external WebGL2 rendering context, and expose the imported context back through `Adapter::adapter_context()` / `Device::context()`. By @pepperoni505 in [#9438](https://github.com/gfx-rs/wgpu/pull/9438). +- Add `gles::Device::buffer_from_raw` for wrapping an externally-owned GL buffer as a `wgpu_hal::gles::Buffer`. By @AdrianEddy in [#9550](https://github.com/gfx-rs/wgpu/pull/9550). #### DX12 diff --git a/wgpu-hal/src/gles/device.rs b/wgpu-hal/src/gles/device.rs index 6f3f31920..f573e0a3d 100644 --- a/wgpu-hal/src/gles/device.rs +++ b/wgpu-hal/src/gles/device.rs @@ -176,6 +176,72 @@ impl super::Device { } } + /// # Safety + /// + /// - `name` must be a non-zero GL buffer name created respecting `desc`. + /// - The buffer's storage size must be at least `desc.size`. + /// - If `desc.usage` includes [`BufferUses::MAP_READ`](wgt::BufferUses::MAP_READ) or + /// [`BufferUses::MAP_WRITE`](wgt::BufferUses::MAP_WRITE), the GL buffer must have + /// been allocated with `glBufferStorage` (or equivalent) using flags compatible + /// with persistent mapping (`GL_MAP_PERSISTENT_BIT` plus matching read/write/coherent + /// bits). Buffers created with the legacy `glBufferData` family cannot be mapped + /// through this path. + /// - If `drop_callback` is [`None`], wgpu-hal will take ownership of the buffer and + /// call `glDeleteBuffers` on it. If `drop_callback` is [`Some`], the buffer must + /// remain valid until the callback is invoked. + #[cfg(any(native, Emscripten))] + pub unsafe fn buffer_from_raw( + &self, + name: NonZeroU32, + desc: &crate::BufferDescriptor, + drop_callback: Option, + ) -> super::Buffer { + let target = if desc.usage.contains(wgt::BufferUses::INDEX) { + glow::ELEMENT_ARRAY_BUFFER + } else { + glow::ARRAY_BUFFER + }; + + let is_host_visible = desc + .usage + .intersects(wgt::BufferUses::MAP_READ | wgt::BufferUses::MAP_WRITE); + let is_coherent = desc + .memory_flags + .contains(crate::MemoryFlags::PREFER_COHERENT); + + let mut map_flags = 0; + if desc.usage.contains(wgt::BufferUses::MAP_READ) { + map_flags |= glow::MAP_READ_BIT; + } + if desc.usage.contains(wgt::BufferUses::MAP_WRITE) { + map_flags |= glow::MAP_WRITE_BIT; + } + if is_host_visible { + map_flags |= glow::MAP_PERSISTENT_BIT; + if is_coherent { + map_flags |= glow::MAP_COHERENT_BIT; + } + } + if !is_coherent && desc.usage.contains(wgt::BufferUses::MAP_WRITE) { + map_flags |= glow::MAP_FLUSH_EXPLICIT_BIT; + } + + self.counters.buffers.add(1); + + super::Buffer { + raw: Some(glow::NativeBuffer(name)), + target, + size: desc.size, + map_flags, + map_state: Arc::new(MaybeMutex::new(super::BufferMapState { + mapped: false, + data: None, + offset_of_current_mapping: 0, + })), + drop_guard: crate::DropGuard::from_option(drop_callback).map(Arc::new), + } + } + unsafe fn compile_shader( gl: &glow::Context, shader: &str, @@ -576,6 +642,7 @@ impl crate::Device for super::Device { data: Some(vec![0; desc.size as usize]), offset_of_current_mapping: 0, })), + drop_guard: None, }); } @@ -679,15 +746,22 @@ impl crate::Device for super::Device { data, offset_of_current_mapping: 0, })), + drop_guard: None, }) } unsafe fn destroy_buffer(&self, buffer: super::Buffer) { - if let Some(raw) = buffer.raw { - let gl = &self.shared.context.lock(); - unsafe { gl.delete_buffer(raw) }; + if buffer.drop_guard.is_none() { + if let Some(raw) = buffer.raw { + let gl = &self.shared.context.lock(); + unsafe { gl.delete_buffer(raw) }; + } } + // For clarity, we explicitly drop the drop guard. Although this has no real semantic effect as the + // end of the scope will drop the drop guard since this function takes ownership of the buffer. + drop(buffer.drop_guard); + self.counters.buffers.sub(1); } diff --git a/wgpu-hal/src/gles/mod.rs b/wgpu-hal/src/gles/mod.rs index 87c8be619..3520e765d 100644 --- a/wgpu-hal/src/gles/mod.rs +++ b/wgpu-hal/src/gles/mod.rs @@ -348,6 +348,12 @@ pub struct Buffer { /// /// If locked concurrently with the GL context, the GL context should be locked first. map_state: Arc>, + /// Set when the buffer wraps an externally-owned GL name created via + /// [`Device::buffer_from_raw`](crate::gles::Device::buffer_from_raw). + /// + /// `Buffer` is `Clone`, so the guard is shared via `Arc` + /// and only fires its callback once every clone is dropped. + drop_guard: Option>, } #[derive(Clone, Debug)]