Skip to content

Commit

Permalink
Buffer as hal (#5724)
Browse files Browse the repository at this point in the history
* Add `as_hal` for `Buffer`

* fmt & CHANGELOG.md update

* Update CHANGELOG.md

* fixed callback name

* allow nested buffer as_hal callbacks
  • Loading branch information
JasondeWolff authored May 26, 2024
1 parent b4abd65 commit cd744ef
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions wgpu-core/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions wgpu/src/backend/wgpu_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
24 changes: 24 additions & 0 deletions wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<'_> {
Expand Down

0 comments on commit cd744ef

Please sign in to comment.