Skip to content

Commit

Permalink
Adapter and Instance as_hal functions (#2663)
Browse files Browse the repository at this point in the history
These functions are added to allow lower level access to adapter and instance from backends.
  • Loading branch information
i509VCB authored May 17, 2022
1 parent 26f96c7 commit f5c2ff1
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 1 deletion.
27 changes: 27 additions & 0 deletions wgpu-core/src/hub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,17 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
}
}

/// # Safety
///
/// - The raw handle obtained from the hal Instance must not be manually destroyed
pub unsafe fn instance_as_hal<A: HalApi, F: FnOnce(Option<&A::Instance>) -> R, R>(
&self,
hal_instance_callback: F,
) -> R {
let hal_instance = A::instance_as_hal(&self.instance);
hal_instance_callback(hal_instance)
}

pub fn clear_backend<A: HalApi>(&self, _dummy: ()) {
let mut surface_guard = self.surfaces.data.write();
let hub = A::hub(self);
Expand Down Expand Up @@ -991,6 +1002,7 @@ impl<G: GlobalIdentityHandlerFactory> Drop for Global<G> {
pub trait HalApi: hal::Api {
const VARIANT: Backend;
fn create_instance_from_hal(name: &str, hal_instance: Self::Instance) -> Instance;
fn instance_as_hal(instance: &Instance) -> Option<&Self::Instance>;
fn hub<G: GlobalIdentityHandlerFactory>(global: &Global<G>) -> &Hub<Self, G>;
fn get_surface(surface: &Surface) -> &HalSurface<Self>;
fn get_surface_mut(surface: &mut Surface) -> &mut HalSurface<Self>;
Expand All @@ -1006,6 +1018,9 @@ impl HalApi for hal::api::Vulkan {
..Default::default()
}
}
fn instance_as_hal(instance: &Instance) -> Option<&Self::Instance> {
instance.vulkan.as_ref()
}
fn hub<G: GlobalIdentityHandlerFactory>(global: &Global<G>) -> &Hub<Self, G> {
&global.hubs.vulkan
}
Expand All @@ -1027,6 +1042,9 @@ impl HalApi for hal::api::Metal {
..Default::default()
}
}
fn instance_as_hal(instance: &Instance) -> Option<&Self::Instance> {
instance.metal.as_ref()
}
fn hub<G: GlobalIdentityHandlerFactory>(global: &Global<G>) -> &Hub<Self, G> {
&global.hubs.metal
}
Expand All @@ -1048,6 +1066,9 @@ impl HalApi for hal::api::Dx12 {
..Default::default()
}
}
fn instance_as_hal(instance: &Instance) -> Option<&Self::Instance> {
instance.dx12.as_ref()
}
fn hub<G: GlobalIdentityHandlerFactory>(global: &Global<G>) -> &Hub<Self, G> {
&global.hubs.dx12
}
Expand All @@ -1069,6 +1090,9 @@ impl HalApi for hal::api::Dx11 {
..Default::default()
}
}
fn instance_as_hal(instance: &Instance) -> Option<&Self::Instance> {
instance.dx11.as_ref()
}
fn hub<G: GlobalIdentityHandlerFactory>(global: &Global<G>) -> &Hub<Self, G> {
&global.hubs.dx11
}
Expand All @@ -1091,6 +1115,9 @@ impl HalApi for hal::api::Gles {
..Default::default()
}
}
fn instance_as_hal(instance: &Instance) -> Option<&Self::Instance> {
instance.gl.as_ref()
}
fn hub<G: GlobalIdentityHandlerFactory>(global: &Global<G>) -> &Hub<Self, G> {
&global.hubs.gl
}
Expand Down
22 changes: 21 additions & 1 deletion wgpu-core/src/resource.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
device::{DeviceError, HostMap, MissingFeatures},
hub::{Global, GlobalIdentityHandlerFactory, HalApi, Resource, Token},
id::{DeviceId, SurfaceId, TextureId, Valid},
id::{AdapterId, DeviceId, SurfaceId, TextureId, Valid},
init_tracker::{BufferInitTracker, TextureInitTracker},
track::{TextureSelector, DUMMY_SELECTOR},
validation::MissingBufferUsageError,
Expand Down Expand Up @@ -250,6 +250,26 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
hal_texture_callback(hal_texture);
}

/// # Safety
///
/// - The raw adapter handle must not be manually destroyed
pub unsafe fn adapter_as_hal<A: HalApi, F: FnOnce(Option<&A::Adapter>) -> R, R>(
&self,
id: AdapterId,
hal_adapter_callback: F,
) -> R {
profiling::scope!("as_hal", "Adapter");

let hub = A::hub(self);
let mut token = Token::root();

let (guard, _) = hub.adapters.read(&mut token);
let adapter = guard.get(id).ok();
let hal_adapter = adapter.map(|adapter| &adapter.raw.adapter);

hal_adapter_callback(hal_adapter)
}

/// # Safety
///
/// - The raw device handle must not be manually destroyed
Expand Down
18 changes: 18 additions & 0 deletions wgpu/src/backend/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ impl Context {
))
}

#[cfg(any(not(target_arch = "wasm32"), feature = "webgl2"))]
pub unsafe fn instance_as_hal<A: wgc::hub::HalApi, F: FnOnce(Option<&A::Instance>) -> R, R>(
&self,
hal_instance_callback: F,
) -> R {
self.0.instance_as_hal::<A, F, R>(hal_instance_callback)
}

pub(crate) fn global(&self) -> &wgc::hub::Global<wgc::hub::IdentityManagerFactory> {
&self.0
}
Expand All @@ -67,6 +75,16 @@ impl Context {
self.0.create_adapter_from_hal(hal_adapter, PhantomData)
}

#[cfg(any(not(target_arch = "wasm32"), feature = "webgl2"))]
pub unsafe fn adapter_as_hal<A: wgc::hub::HalApi, F: FnOnce(Option<&A::Adapter>) -> R, R>(
&self,
adapter: wgc::id::AdapterId,
hal_adapter_callback: F,
) -> R {
self.0
.adapter_as_hal::<A, F, R>(adapter, hal_adapter_callback)
}

#[cfg(any(not(target_arch = "wasm32"), feature = "emscripten"))]
pub unsafe fn create_device_from_hal<A: wgc::hub::HalApi>(
&self,
Expand Down
30 changes: 30 additions & 0 deletions wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1632,6 +1632,21 @@ impl Instance {
}
}

/// Returns the inner hal Instance using a callback. The hal instance will be `None` if the
/// backend type argument does not match with this wgpu Instance
///
/// # Safety
///
/// - The raw handle obtained from the hal Instance must not be manually destroyed
#[cfg(any(not(target_arch = "wasm32"), feature = "webgl2"))]
pub unsafe fn as_hal<A: wgc::hub::HalApi, F: FnOnce(Option<&A::Instance>) -> R, R>(
&self,
hal_instance_callback: F,
) -> R {
self.context
.instance_as_hal::<A, F, R>(hal_instance_callback)
}

/// Retrieves all available [`Adapter`]s that match the given [`Backends`].
///
/// # Arguments
Expand Down Expand Up @@ -1848,6 +1863,21 @@ impl Adapter {
})
}

/// Returns the inner hal Adapter using a callback. The hal adapter will be `None` if the
/// backend type argument does not match with this wgpu Adapter
///
/// # Safety
///
/// - The raw handle obtained from the hal Adapter must not be manually destroyed
#[cfg(any(not(target_arch = "wasm32"), feature = "webgl2"))]
pub unsafe fn as_hal<A: wgc::hub::HalApi, F: FnOnce(Option<&A::Adapter>) -> R, R>(
&self,
hal_adapter_callback: F,
) -> R {
self.context
.adapter_as_hal::<A, F, R>(self.id, hal_adapter_callback)
}

/// Returns whether this adapter may present to the passed surface.
pub fn is_surface_supported(&self, surface: &Surface) -> bool {
Context::adapter_is_surface_supported(&*self.context, &self.id, &surface.id)
Expand Down

0 comments on commit f5c2ff1

Please sign in to comment.