From f041991d0cf4833f2b23e48c92d8b81cee80a8bc Mon Sep 17 00:00:00 2001 From: i509VCB Date: Fri, 10 Jun 2022 19:13:37 -0500 Subject: [PATCH] allow creating wgpu::Instance from wgpu_core::Instance --- wgpu-core/src/hub.rs | 12 ++++++++++++ wgpu/src/backend/direct.rs | 12 ++++++++++-- wgpu/src/lib.rs | 20 ++++++++++++++++++-- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/wgpu-core/src/hub.rs b/wgpu-core/src/hub.rs index 4e8ddf78f6..1a817f1e7c 100644 --- a/wgpu-core/src/hub.rs +++ b/wgpu-core/src/hub.rs @@ -930,6 +930,18 @@ impl Global { 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(&self, _dummy: ()) { let mut surface_guard = self.surfaces.data.write(); let hub = A::hub(self); diff --git a/wgpu/src/backend/direct.rs b/wgpu/src/backend/direct.rs index 3af6a77a91..b7a0099b60 100644 --- a/wgpu/src/backend/direct.rs +++ b/wgpu/src/backend/direct.rs @@ -47,7 +47,7 @@ impl Context { )) } - #[cfg(any(not(target_arch = "wasm32"), feature = "webgl2"))] + #[cfg(any(not(target_arch = "wasm32"), feature = "webgl"))] pub unsafe fn instance_as_hal) -> R, R>( &self, hal_instance_callback: F, @@ -55,6 +55,14 @@ impl Context { self.0.instance_as_hal::(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 { &self.0 } @@ -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) -> R, R>( &self, adapter: wgc::id::AdapterId, diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 8642eafdf2..b6303bf397 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -1648,7 +1648,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) -> R, R>( &self, hal_instance_callback: F, @@ -1657,6 +1657,22 @@ impl Instance { .instance_as_hal::(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 @@ -1877,7 +1893,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) -> R, R>( &self, hal_adapter_callback: F,