Buffer as hal (#5724)

* Add `as_hal` for `Buffer`

* fmt & CHANGELOG.md update

* Update CHANGELOG.md

* fixed callback name

* allow nested buffer as_hal callbacks
This commit is contained in:
Jason de Wolff 2024-05-26 08:58:30 +02:00 committed by GitHub
parent b4abd65659
commit cd744ef68b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 56 additions and 0 deletions

View File

@ -78,6 +78,8 @@ By @stefnotch in [#5410](https://github.com/gfx-rs/wgpu/pull/5410)
#### General
- Added `as_hal` for `Buffer` to access wgpu created buffers form wgpu-hal. By @JasondeWolff in [#5724](https://github.com/gfx-rs/wgpu/pull/5724)
#### Naga
- Implement `WGSL`'s `unpack4xI8`,`unpack4xU8`,`pack4xI8` and `pack4xU8`. By @VlaDexa in [#5424](https://github.com/gfx-rs/wgpu/pull/5424)

View File

@ -932,6 +932,28 @@ impl<A: HalApi> Texture<A> {
}
impl Global {
/// # Safety
///
/// - The raw buffer handle must not be manually destroyed
pub unsafe fn buffer_as_hal<A: HalApi, F: FnOnce(Option<&A::Buffer>) -> R, R>(
&self,
id: BufferId,
hal_buffer_callback: F,
) -> R {
profiling::scope!("Buffer::as_hal");
let hub = A::hub(self);
let buffer_opt = { hub.buffers.try_get(id).ok().flatten() };
let buffer = buffer_opt.as_ref().unwrap();
let hal_buffer = {
let snatch_guard = buffer.device.snatchable_lock.read();
buffer.raw(&snatch_guard)
};
hal_buffer_callback(hal_buffer)
}
/// # Safety
///
/// - The raw texture handle must not be manually destroyed

View File

@ -98,6 +98,14 @@ impl ContextWgpuCore {
}
}
pub unsafe fn buffer_as_hal<A: wgc::hal_api::HalApi, F: FnOnce(Option<&A::Buffer>) -> R, R>(
&self,
id: wgc::id::BufferId,
hal_buffer_callback: F,
) -> R {
unsafe { self.0.buffer_as_hal::<A, F, R>(id, hal_buffer_callback) }
}
pub unsafe fn create_device_from_hal<A: wgc::hal_api::HalApi>(
&self,
adapter: &wgc::id::AdapterId,

View File

@ -3547,6 +3547,30 @@ impl Buffer {
}
}
/// Returns the inner hal Buffer using a callback. The hal buffer will be `None` if the
/// backend type argument does not match with this wgpu Buffer
///
/// # Safety
///
/// - The raw handle obtained from the hal Buffer must not be manually destroyed
#[cfg(wgpu_core)]
pub unsafe fn as_hal<A: wgc::hal_api::HalApi, F: FnOnce(Option<&A::Buffer>) -> R, R>(
&self,
hal_buffer_callback: F,
) -> R {
let id = self.id;
if let Some(ctx) = self
.context
.as_any()
.downcast_ref::<crate::backend::ContextWgpuCore>()
{
unsafe { ctx.buffer_as_hal::<A, F, R>(id.into(), hal_buffer_callback) }
} else {
hal_buffer_callback(None)
}
}
/// Use only a portion of this Buffer for a given operation. Choosing a range with no end
/// will use the rest of the buffer. Using a totally unbounded range will use the entire buffer.
pub fn slice<S: RangeBounds<BufferAddress>>(&self, bounds: S) -> BufferSlice<'_> {