[GLES/hal] Add gles::Device::buffer_from_raw for wrapping external buffers (#9550)

Co-authored-by: Inner Daemons <85136135+inner-daemons@users.noreply.github.com>
This commit is contained in:
AdrianEddyandInner Daemons authored and GitHub committed 2026-06-07 10:20:47 +02:00
1 parent 6877690dbe
commit c3caf2feb4
3 files changed
+84 -3

No files matched your search

+1
View File
@@ -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
+77 -3
View File
@@ -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<crate::DropCallback>,
) -> 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);
}
+6
View File
@@ -348,6 +348,12 @@ pub struct Buffer {
///
/// If locked concurrently with the GL context, the GL context should be locked first.
map_state: Arc<MaybeMutex<BufferMapState>>,
/// 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<Arc<crate::DropGuard>>,
}
#[derive(Clone, Debug)]