From 0b7e0daa97cb5ec754fc344d4683071705cb792f 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 feef06e2e4..53f11a0bee 100644 --- a/wgpu-core/src/hub.rs +++ b/wgpu-core/src/hub.rs @@ -918,6 +918,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 fdd599e57b..5fda944dd1 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 69cb5ecaed..00fad17375 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -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) -> R, R>( &self, hal_instance_callback: F, @@ -1647,6 +1647,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 @@ -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) -> R, R>( &self, hal_adapter_callback: F,