Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Buffer as hal #5724

Merged
merged 5 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading