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

allow creating wgpu::Instance from wgpu_core::Instance #2763

Merged
merged 1 commit into from
Jun 26, 2022
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
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