Skip to content

Commit

Permalink
allow creating wgpu::Instance from wgpu_core::Instance
Browse files Browse the repository at this point in the history
  • Loading branch information
i509VCB committed Jun 26, 2022
1 parent 435188c commit 0b7e0da
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
12 changes: 12 additions & 0 deletions wgpu-core/src/hub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,18 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
hal_instance_callback(hal_instance)
}

/// # Safety
///
/// - The raw handles obtained from the Instance must not be manually destroyed
pub unsafe fn from_instance(factory: G, instance: Instance) -> Self {
profiling::scope!("new", "Global");
Self {
instance,
surfaces: Registry::without_backend(&factory, "Surface"),
hubs: Hubs::new(&factory),
}
}

pub fn clear_backend<A: HalApi>(&self, _dummy: ()) {
let mut surface_guard = self.surfaces.data.write();
let hub = A::hub(self);
Expand Down
12 changes: 10 additions & 2 deletions wgpu/src/backend/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,22 @@ impl Context {
))
}

#[cfg(any(not(target_arch = "wasm32"), feature = "webgl2"))]
#[cfg(any(not(target_arch = "wasm32"), feature = "webgl"))]
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)
}

#[cfg(any(not(target_arch = "wasm32"), feature = "webgl"))]
pub unsafe fn from_core_instance(core_instance: wgc::instance::Instance) -> Self {
Self(wgc::hub::Global::from_instance(
wgc::hub::IdentityManagerFactory,
core_instance,
))
}

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

#[cfg(any(not(target_arch = "wasm32"), feature = "webgl2"))]
#[cfg(any(not(target_arch = "wasm32"), feature = "webgl"))]
pub unsafe fn adapter_as_hal<A: wgc::hub::HalApi, F: FnOnce(Option<&A::Adapter>) -> R, R>(
&self,
adapter: wgc::id::AdapterId,
Expand Down
20 changes: 18 additions & 2 deletions wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1638,7 +1638,7 @@ impl Instance {
/// # Safety
///
/// - The raw handle obtained from the hal Instance must not be manually destroyed
#[cfg(any(not(target_arch = "wasm32"), feature = "webgl2"))]
#[cfg(any(not(target_arch = "wasm32"), feature = "webgl"))]
pub unsafe fn as_hal<A: wgc::hub::HalApi, F: FnOnce(Option<&A::Instance>) -> R, R>(
&self,
hal_instance_callback: F,
Expand All @@ -1647,6 +1647,22 @@ impl Instance {
.instance_as_hal::<A, F, R>(hal_instance_callback)
}

/// Create an new instance of wgpu from a wgpu-core instance.
///
/// # Arguments
///
/// - `core_instance` - wgpu-core instance.
///
/// # Safety
///
/// Refer to the creation of wgpu-core Instance.
#[cfg(any(not(target_arch = "wasm32"), feature = "webgl"))]
pub unsafe fn from_core(core_instance: wgc::instance::Instance) -> Self {
Self {
context: Arc::new(C::from_core_instance(core_instance)),
}
}

/// Retrieves all available [`Adapter`]s that match the given [`Backends`].
///
/// # Arguments
Expand Down Expand Up @@ -1869,7 +1885,7 @@ impl Adapter {
/// # Safety
///
/// - The raw handle obtained from the hal Adapter must not be manually destroyed
#[cfg(any(not(target_arch = "wasm32"), feature = "webgl2"))]
#[cfg(any(not(target_arch = "wasm32"), feature = "webgl"))]
pub unsafe fn as_hal<A: wgc::hub::HalApi, F: FnOnce(Option<&A::Adapter>) -> R, R>(
&self,
hal_adapter_callback: F,
Expand Down

0 comments on commit 0b7e0da

Please sign in to comment.