Add Device::as_hal and always assume texture from hal is initialized (#2180)

* Add Device::as_hal, create_texture_from_hal_initialized

* Add Safety doc

* Always assume that the texture from hal is initialized
This commit is contained in:
Xiaopeng Li
2021-11-19 06:34:01 +08:00
committed by GitHub
parent 47fd77619f
commit 39f3f08b55
5 changed files with 58 additions and 1 deletions

View File

@@ -3204,6 +3204,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
///
/// - `hal_texture` must be created from `device_id` corresponding raw handle.
/// - `hal_texture` must be created respecting `desc`
/// - `hal_texture` must be initialized
pub unsafe fn create_texture_from_hal<A: HalApi>(
&self,
hal_texture: A::Texture,
@@ -3243,8 +3244,14 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
Err(error) => break error,
};
let texture =
let mut texture =
device.create_texture_from_hal(hal_texture, device_id, desc, format_features);
if desc.usage.contains(wgt::TextureUsages::COPY_DST) {
texture.hal_usage |= hal::TextureUses::COPY_DST;
}
texture.initialization_status = TextureInitTracker::new(desc.mip_level_count, 0);
let num_levels = texture.full_range.levels.end;
let num_layers = texture.full_range.layers.end;
let ref_count = texture.life_guard.add_ref();

View File

@@ -209,6 +209,25 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
hal_texture_callback(hal_texture);
}
/// # Safety
///
/// - The raw device handle must not be manually destroyed
pub unsafe fn device_as_hal<A: HalApi, F: FnOnce(Option<&A::Device>) -> R, R>(
&self,
id: DeviceId,
hal_device_callback: F,
) -> R {
profiling::scope!("as_hal", "Device");
let hub = A::hub(self);
let mut token = Token::root();
let (guard, _) = hub.devices.read(&mut token);
let device = guard.get(id).ok();
let hal_device = device.map(|device| &device.raw);
hal_device_callback(hal_device)
}
}
#[derive(Clone, Copy, Debug)]

View File

@@ -1,3 +1,4 @@
use parking_lot::Mutex;
use std::{
ptr,
sync::{atomic, Arc},
@@ -192,6 +193,10 @@ impl super::Device {
copy_size,
}
}
pub fn raw_device(&self) -> &Mutex<mtl::Device> {
&self.shared.device
}
}
impl crate::Device<super::Api> for super::Device {

View File

@@ -123,6 +123,16 @@ impl Context {
}
}
#[cfg(not(target_arch = "wasm32"))]
pub unsafe fn device_as_hal<A: wgc::hub::HalApi, F: FnOnce(Option<&A::Device>) -> R, R>(
&self,
device: &Device,
hal_device_callback: F,
) -> R {
self.0
.device_as_hal::<A, F, R>(device.id, hal_device_callback)
}
#[cfg(not(target_arch = "wasm32"))]
pub unsafe fn texture_as_hal<A: wgc::hub::HalApi, F: FnOnce(Option<&A::Texture>)>(
&self,

View File

@@ -1826,6 +1826,7 @@ impl Device {
///
/// - `hal_texture` must be created from this device internal handle
/// - `hal_texture` must be created respecting `desc`
/// - `hal_texture` must be initialized
#[cfg(not(target_arch = "wasm32"))]
pub unsafe fn create_texture_from_hal<A: wgc::hub::HalApi>(
&self,
@@ -1873,6 +1874,21 @@ impl Device {
pub fn stop_capture(&self) {
Context::device_stop_capture(&*self.context, &self.id)
}
/// Returns the inner hal Device using a callback. The hal device will be `None` if the
/// backend type argument does not match with this wgpu Device
///
/// # Safety
///
/// - The raw handle obtained from the hal Device must not be manually destroyed
#[cfg(not(target_arch = "wasm32"))]
pub unsafe fn as_hal<A: wgc::hub::HalApi, F: FnOnce(Option<&A::Device>) -> R, R>(
&self,
hal_device_callback: F,
) -> R {
self.context
.device_as_hal::<A, F, R>(&self.id, hal_device_callback)
}
}
impl Drop for Device {