Skip to content

Commit cd744ef

Browse files
authored
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
1 parent b4abd65 commit cd744ef

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ By @stefnotch in [#5410](https://github.com/gfx-rs/wgpu/pull/5410)
7878

7979
#### General
8080

81+
- 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)
82+
8183
#### Naga
8284

8385
- Implement `WGSL`'s `unpack4xI8`,`unpack4xU8`,`pack4xI8` and `pack4xU8`. By @VlaDexa in [#5424](https://github.com/gfx-rs/wgpu/pull/5424)

wgpu-core/src/resource.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,28 @@ impl<A: HalApi> Texture<A> {
932932
}
933933

934934
impl Global {
935+
/// # Safety
936+
///
937+
/// - The raw buffer handle must not be manually destroyed
938+
pub unsafe fn buffer_as_hal<A: HalApi, F: FnOnce(Option<&A::Buffer>) -> R, R>(
939+
&self,
940+
id: BufferId,
941+
hal_buffer_callback: F,
942+
) -> R {
943+
profiling::scope!("Buffer::as_hal");
944+
945+
let hub = A::hub(self);
946+
let buffer_opt = { hub.buffers.try_get(id).ok().flatten() };
947+
let buffer = buffer_opt.as_ref().unwrap();
948+
949+
let hal_buffer = {
950+
let snatch_guard = buffer.device.snatchable_lock.read();
951+
buffer.raw(&snatch_guard)
952+
};
953+
954+
hal_buffer_callback(hal_buffer)
955+
}
956+
935957
/// # Safety
936958
///
937959
/// - The raw texture handle must not be manually destroyed

wgpu/src/backend/wgpu_core.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ impl ContextWgpuCore {
9898
}
9999
}
100100

101+
pub unsafe fn buffer_as_hal<A: wgc::hal_api::HalApi, F: FnOnce(Option<&A::Buffer>) -> R, R>(
102+
&self,
103+
id: wgc::id::BufferId,
104+
hal_buffer_callback: F,
105+
) -> R {
106+
unsafe { self.0.buffer_as_hal::<A, F, R>(id, hal_buffer_callback) }
107+
}
108+
101109
pub unsafe fn create_device_from_hal<A: wgc::hal_api::HalApi>(
102110
&self,
103111
adapter: &wgc::id::AdapterId,

wgpu/src/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3547,6 +3547,30 @@ impl Buffer {
35473547
}
35483548
}
35493549

3550+
/// Returns the inner hal Buffer using a callback. The hal buffer will be `None` if the
3551+
/// backend type argument does not match with this wgpu Buffer
3552+
///
3553+
/// # Safety
3554+
///
3555+
/// - The raw handle obtained from the hal Buffer must not be manually destroyed
3556+
#[cfg(wgpu_core)]
3557+
pub unsafe fn as_hal<A: wgc::hal_api::HalApi, F: FnOnce(Option<&A::Buffer>) -> R, R>(
3558+
&self,
3559+
hal_buffer_callback: F,
3560+
) -> R {
3561+
let id = self.id;
3562+
3563+
if let Some(ctx) = self
3564+
.context
3565+
.as_any()
3566+
.downcast_ref::<crate::backend::ContextWgpuCore>()
3567+
{
3568+
unsafe { ctx.buffer_as_hal::<A, F, R>(id.into(), hal_buffer_callback) }
3569+
} else {
3570+
hal_buffer_callback(None)
3571+
}
3572+
}
3573+
35503574
/// Use only a portion of this Buffer for a given operation. Choosing a range with no end
35513575
/// will use the rest of the buffer. Using a totally unbounded range will use the entire buffer.
35523576
pub fn slice<S: RangeBounds<BufferAddress>>(&self, bounds: S) -> BufferSlice<'_> {

0 commit comments

Comments
 (0)