diff --git a/wgpu-core/src/as_hal.rs b/wgpu-core/src/as_hal.rs index 1191bcbbd..8ad9cf3ea 100644 --- a/wgpu-core/src/as_hal.rs +++ b/wgpu-core/src/as_hal.rs @@ -6,7 +6,6 @@ use hal::DynResource; use crate::{ device::Device, global::Global, - hal_api::HalApi, id::{ AdapterId, BlasId, BufferId, CommandEncoderId, DeviceId, QueueId, SurfaceId, TextureId, TextureViewId, TlasId, @@ -226,7 +225,7 @@ impl Global { /// # Safety /// /// - The raw buffer handle must not be manually destroyed - pub unsafe fn buffer_as_hal( + pub unsafe fn buffer_as_hal( &self, id: BufferId, ) -> Option> { @@ -242,7 +241,7 @@ impl Global { /// # Safety /// /// - The raw texture handle must not be manually destroyed - pub unsafe fn texture_as_hal( + pub unsafe fn texture_as_hal( &self, id: TextureId, ) -> Option> { @@ -258,7 +257,7 @@ impl Global { /// # Safety /// /// - The raw texture view handle must not be manually destroyed - pub unsafe fn texture_view_as_hal( + pub unsafe fn texture_view_as_hal( &self, id: TextureViewId, ) -> Option> { @@ -274,7 +273,7 @@ impl Global { /// # Safety /// /// - The raw adapter handle must not be manually destroyed - pub unsafe fn adapter_as_hal( + pub unsafe fn adapter_as_hal( &self, id: AdapterId, ) -> Option> { @@ -291,7 +290,7 @@ impl Global { /// # Safety /// /// - The raw device handle must not be manually destroyed - pub unsafe fn device_as_hal( + pub unsafe fn device_as_hal( &self, id: DeviceId, ) -> Option> { @@ -305,7 +304,7 @@ impl Global { /// # Safety /// /// - The raw fence handle must not be manually destroyed - pub unsafe fn device_fence_as_hal( + pub unsafe fn device_fence_as_hal( &self, id: DeviceId, ) -> Option> { @@ -318,7 +317,7 @@ impl Global { /// # Safety /// - The raw surface handle must not be manually destroyed - pub unsafe fn surface_as_hal( + pub unsafe fn surface_as_hal( &self, id: SurfaceId, ) -> Option> { @@ -335,7 +334,7 @@ impl Global { /// /// - The raw command encoder handle must not be manually destroyed pub unsafe fn command_encoder_as_hal_mut< - A: HalApi, + A: hal::Api, F: FnOnce(Option<&mut A::CommandEncoder>) -> R, R, >( @@ -363,7 +362,7 @@ impl Global { /// # Safety /// /// - The raw queue handle must not be manually destroyed - pub unsafe fn queue_as_hal( + pub unsafe fn queue_as_hal( &self, id: QueueId, ) -> Option> { @@ -377,7 +376,7 @@ impl Global { /// # Safety /// /// - The raw blas handle must not be manually destroyed - pub unsafe fn blas_as_hal( + pub unsafe fn blas_as_hal( &self, id: BlasId, ) -> Option> { @@ -393,7 +392,7 @@ impl Global { /// # Safety /// /// - The raw tlas handle must not be manually destroyed - pub unsafe fn tlas_as_hal( + pub unsafe fn tlas_as_hal( &self, id: TlasId, ) -> Option> { diff --git a/wgpu-core/src/device/global.rs b/wgpu-core/src/device/global.rs index 96842c716..cf2412536 100644 --- a/wgpu-core/src/device/global.rs +++ b/wgpu-core/src/device/global.rs @@ -13,7 +13,6 @@ use crate::{ conv, device::{bgl, life::WaitIdleError, DeviceError, DeviceLostClosure}, global::Global, - hal_api::HalApi, id::{self, AdapterId, DeviceId, QueueId, SurfaceId}, instance::{self, Adapter, Surface}, pipeline::{ @@ -386,7 +385,7 @@ impl Global { /// - `hal_buffer` must be created respecting `desc` /// - `hal_buffer` must be initialized /// - `hal_buffer` must not have zero size. - pub unsafe fn create_buffer_from_hal( + pub unsafe fn create_buffer_from_hal( &self, hal_buffer: A::Buffer, device_id: DeviceId, diff --git a/wgpu-core/src/global.rs b/wgpu-core/src/global.rs index 357cb2d42..150191d24 100644 --- a/wgpu-core/src/global.rs +++ b/wgpu-core/src/global.rs @@ -2,7 +2,6 @@ use alloc::{borrow::ToOwned as _, sync::Arc}; use core::fmt; use crate::{ - hal_api::HalApi, hub::{Hub, HubReport}, instance::{Instance, Surface}, registry::{Registry, RegistryReport}, @@ -44,7 +43,7 @@ impl Global { /// # Safety /// /// Refer to the creation of wgpu-hal Instance for every backend. - pub unsafe fn from_hal_instance(name: &str, hal_instance: A::Instance) -> Self { + pub unsafe fn from_hal_instance(name: &str, hal_instance: A::Instance) -> Self { profiling::scope!("Global::new"); Self { @@ -57,7 +56,7 @@ impl Global { /// # Safety /// /// - The raw instance handle returned must not be manually destroyed. - pub unsafe fn instance_as_hal(&self) -> Option<&A::Instance> { + pub unsafe fn instance_as_hal(&self) -> Option<&A::Instance> { unsafe { self.instance.as_hal::() } } diff --git a/wgpu-core/src/hal_api.rs b/wgpu-core/src/hal_api.rs deleted file mode 100644 index e8d40d8eb..000000000 --- a/wgpu-core/src/hal_api.rs +++ /dev/null @@ -1,29 +0,0 @@ -use wgt::{Backend, WasmNotSendSync}; - -pub trait HalApi: hal::Api + 'static + WasmNotSendSync { - const VARIANT: Backend; -} - -impl HalApi for hal::api::Noop { - const VARIANT: Backend = Backend::Noop; -} - -#[cfg(vulkan)] -impl HalApi for hal::api::Vulkan { - const VARIANT: Backend = Backend::Vulkan; -} - -#[cfg(metal)] -impl HalApi for hal::api::Metal { - const VARIANT: Backend = Backend::Metal; -} - -#[cfg(dx12)] -impl HalApi for hal::api::Dx12 { - const VARIANT: Backend = Backend::Dx12; -} - -#[cfg(gles)] -impl HalApi for hal::api::Gles { - const VARIANT: Backend = Backend::Gl; -} diff --git a/wgpu-core/src/instance.rs b/wgpu-core/src/instance.rs index 962596a00..4f36b0a85 100644 --- a/wgpu-core/src/instance.rs +++ b/wgpu-core/src/instance.rs @@ -15,7 +15,6 @@ use crate::{ api_log, api_log_debug, device::{queue::Queue, resource::Device, DeviceDescriptor, DeviceError}, global::Global, - hal_api::HalApi, id::{markers, AdapterId, DeviceId, QueueId, SurfaceId}, lock::{rank, Mutex}, present::Presentation, @@ -117,7 +116,7 @@ impl Instance { } /// Helper for `Instance::new()`; attempts to add a single `wgpu-hal` backend to this instance. - fn try_add_hal(&mut self, _: A, instance_desc: &wgt::InstanceDescriptor) { + fn try_add_hal(&mut self, _: A, instance_desc: &wgt::InstanceDescriptor) { // Whether or not the backend was requested, and whether or not it succeeds, // note that we *could* try it. self.supported_backends |= A::VARIANT.into(); @@ -151,7 +150,7 @@ impl Instance { } } - pub(crate) fn from_hal_instance( + pub(crate) fn from_hal_instance( name: String, hal_instance: ::Instance, ) -> Self { @@ -175,7 +174,7 @@ impl Instance { /// # Safety /// /// - The raw instance handle returned must not be manually destroyed. - pub unsafe fn as_hal(&self) -> Option<&A::Instance> { + pub unsafe fn as_hal(&self) -> Option<&A::Instance> { self.raw(A::VARIANT).map(|instance| { instance .as_any() diff --git a/wgpu-core/src/lib.rs b/wgpu-core/src/lib.rs index 4f85de994..54c9f1cdc 100644 --- a/wgpu-core/src/lib.rs +++ b/wgpu-core/src/lib.rs @@ -72,7 +72,6 @@ mod conv; pub mod device; pub mod error; pub mod global; -pub mod hal_api; mod hash_utils; pub mod hub; pub mod id; diff --git a/wgpu-hal/src/dx12/mod.rs b/wgpu-hal/src/dx12/mod.rs index c8e6c3e0c..a41796331 100644 --- a/wgpu-hal/src/dx12/mod.rs +++ b/wgpu-hal/src/dx12/mod.rs @@ -397,6 +397,8 @@ impl D3DBlob { pub struct Api; impl crate::Api for Api { + const VARIANT: wgt::Backend = wgt::Backend::Dx12; + type Instance = Instance; type Surface = Surface; type Adapter = Adapter; diff --git a/wgpu-hal/src/gles/mod.rs b/wgpu-hal/src/gles/mod.rs index 30a9d925d..3500eb98c 100644 --- a/wgpu-hal/src/gles/mod.rs +++ b/wgpu-hal/src/gles/mod.rs @@ -141,6 +141,8 @@ const MAX_PUSH_CONSTANTS: usize = 64; const MAX_PUSH_CONSTANT_COMMANDS: usize = MAX_PUSH_CONSTANTS * crate::MAX_CONCURRENT_SHADER_STAGES; impl crate::Api for Api { + const VARIANT: wgt::Backend = wgt::Backend::Gl; + type Instance = Instance; type Surface = Surface; type Adapter = Adapter; diff --git a/wgpu-hal/src/lib.rs b/wgpu-hal/src/lib.rs index 75be3ee73..d4308b2a0 100644 --- a/wgpu-hal/src/lib.rs +++ b/wgpu-hal/src/lib.rs @@ -472,7 +472,9 @@ impl InstanceError { } } -pub trait Api: Clone + fmt::Debug + Sized { +pub trait Api: Clone + fmt::Debug + Sized + WasmNotSendSync + 'static { + const VARIANT: wgt::Backend; + type Instance: DynInstance + Instance; type Surface: DynSurface + Surface; type Adapter: DynAdapter + Adapter; diff --git a/wgpu-hal/src/metal/mod.rs b/wgpu-hal/src/metal/mod.rs index b5ae1dd5d..e1bd7ff0f 100644 --- a/wgpu-hal/src/metal/mod.rs +++ b/wgpu-hal/src/metal/mod.rs @@ -47,6 +47,8 @@ pub struct Api; type ResourceIndex = u32; impl crate::Api for Api { + const VARIANT: wgt::Backend = wgt::Backend::Metal; + type Instance = Instance; type Surface = Surface; type Adapter = Adapter; diff --git a/wgpu-hal/src/noop/mod.rs b/wgpu-hal/src/noop/mod.rs index 6ae4adcf2..ef2d51294 100644 --- a/wgpu-hal/src/noop/mod.rs +++ b/wgpu-hal/src/noop/mod.rs @@ -31,6 +31,8 @@ pub struct Fence { type DeviceResult = Result; impl crate::Api for Api { + const VARIANT: wgt::Backend = wgt::Backend::Noop; + type Instance = Context; type Surface = Context; type Adapter = Context; diff --git a/wgpu-hal/src/vulkan/mod.rs b/wgpu-hal/src/vulkan/mod.rs index 55bd14851..507af4c25 100644 --- a/wgpu-hal/src/vulkan/mod.rs +++ b/wgpu-hal/src/vulkan/mod.rs @@ -56,6 +56,8 @@ const MAX_TOTAL_ATTACHMENTS: usize = crate::MAX_COLOR_ATTACHMENTS * 2 + 1; pub struct Api; impl crate::Api for Api { + const VARIANT: wgt::Backend = wgt::Backend::Vulkan; + type Instance = Instance; type Surface = Surface; type Adapter = Adapter; diff --git a/wgpu/src/api/adapter.rs b/wgpu/src/api/adapter.rs index 529b8421d..16abaacf1 100644 --- a/wgpu/src/api/adapter.rs +++ b/wgpu/src/api/adapter.rs @@ -73,7 +73,7 @@ impl Adapter { /// - `hal_device` must be created from this adapter internal handle. /// - `desc.features` must be a subset of `hal_device`'s supported features. #[cfg(wgpu_core)] - pub unsafe fn create_device_from_hal( + pub unsafe fn create_device_from_hal( &self, hal_device: hal::OpenDevice, desc: &DeviceDescriptor<'_>, @@ -127,7 +127,7 @@ impl Adapter { /// /// [`A::Adapter`]: hal::Api::Adapter #[cfg(wgpu_core)] - pub unsafe fn as_hal( + pub unsafe fn as_hal( &self, ) -> Option + WasmNotSendSync> { let adapter = self.inner.as_core_opt()?; diff --git a/wgpu/src/api/blas.rs b/wgpu/src/api/blas.rs index 4252fa4ba..54013882f 100644 --- a/wgpu/src/api/blas.rs +++ b/wgpu/src/api/blas.rs @@ -192,7 +192,7 @@ impl Blas { /// /// [`A::AccelerationStructure`]: hal::Api::AccelerationStructure #[cfg(wgpu_core)] - pub unsafe fn as_hal( + pub unsafe fn as_hal( &mut self, ) -> Option + WasmNotSendSync> { let blas = self.inner.as_core_opt()?; diff --git a/wgpu/src/api/buffer.rs b/wgpu/src/api/buffer.rs index 2f66b541c..da7e4aca8 100644 --- a/wgpu/src/api/buffer.rs +++ b/wgpu/src/api/buffer.rs @@ -239,7 +239,7 @@ impl Buffer { /// /// [`A::Buffer`]: hal::Api::Buffer #[cfg(wgpu_core)] - pub unsafe fn as_hal( + pub unsafe fn as_hal( &self, ) -> Option + WasmNotSendSync> { let buffer = self.inner.as_core_opt()?; diff --git a/wgpu/src/api/command_encoder.rs b/wgpu/src/api/command_encoder.rs index 56e0ae199..77cb4d1a2 100644 --- a/wgpu/src/api/command_encoder.rs +++ b/wgpu/src/api/command_encoder.rs @@ -258,11 +258,7 @@ impl CommandEncoder { /// - The wgpu command encoder must not be interacted with in any way while recording is /// happening to the wgpu_hal or backend command encoder. #[cfg(wgpu_core)] - pub unsafe fn as_hal_mut< - A: wgc::hal_api::HalApi, - F: FnOnce(Option<&mut A::CommandEncoder>) -> R, - R, - >( + pub unsafe fn as_hal_mut) -> R, R>( &mut self, hal_command_encoder_callback: F, ) -> R { diff --git a/wgpu/src/api/device.rs b/wgpu/src/api/device.rs index 00a03c921..b5544b69a 100644 --- a/wgpu/src/api/device.rs +++ b/wgpu/src/api/device.rs @@ -305,7 +305,7 @@ impl Device { /// - `hal_texture` must be initialized #[cfg(wgpu_core)] #[must_use] - pub unsafe fn create_texture_from_hal( + pub unsafe fn create_texture_from_hal( &self, hal_texture: A::Texture, desc: &TextureDescriptor<'_>, @@ -345,7 +345,7 @@ impl Device { /// - `hal_buffer` must not have zero size #[cfg(wgpu_core)] #[must_use] - pub unsafe fn create_buffer_from_hal( + pub unsafe fn create_buffer_from_hal( &self, hal_buffer: A::Buffer, desc: &BufferDescriptor<'_>, @@ -515,7 +515,7 @@ impl Device { /// /// [`A::Device`]: hal::Api::Device #[cfg(wgpu_core)] - pub unsafe fn as_hal( + pub unsafe fn as_hal( &self, ) -> Option + WasmNotSendSync> { let device = self.inner.as_core_opt()?; diff --git a/wgpu/src/api/instance.rs b/wgpu/src/api/instance.rs index 70cbabfca..b9decd596 100644 --- a/wgpu/src/api/instance.rs +++ b/wgpu/src/api/instance.rs @@ -325,7 +325,7 @@ impl Instance { /// - The `hal_instance` must be a valid and usable instance of the backend specified by `A`. /// - wgpu will act like it has complete ownership of this instance, and will destroy it /// when the last reference to the instance, internal or external, is dropped. - pub unsafe fn from_hal(hal_instance: A::Instance) -> Self { + pub unsafe fn from_hal(hal_instance: A::Instance) -> Self { Self { inner: unsafe { crate::backend::ContextWgpuCore::from_hal_instance::(hal_instance).into() @@ -362,7 +362,7 @@ impl Instance { /// - All the safety requirements of wgpu-hal must be upheld. /// /// [`A::Instance`]: hal::Api::Instance - pub unsafe fn as_hal(&self) -> Option<&A::Instance> { + pub unsafe fn as_hal(&self) -> Option<&A::Instance> { self.inner .as_core_opt() .and_then(|ctx| unsafe { ctx.instance_as_hal::() }) @@ -382,7 +382,7 @@ impl Instance { /// # Safety /// /// `hal_adapter` must be created from this instance internal handle. - pub unsafe fn create_adapter_from_hal( + pub unsafe fn create_adapter_from_hal( &self, hal_adapter: hal::ExposedAdapter, ) -> Adapter { diff --git a/wgpu/src/api/queue.rs b/wgpu/src/api/queue.rs index 27d2006fa..d7c2749b0 100644 --- a/wgpu/src/api/queue.rs +++ b/wgpu/src/api/queue.rs @@ -312,7 +312,7 @@ impl Queue { /// /// [`A::Queue`]: hal::Api::Queue #[cfg(wgpu_core)] - pub unsafe fn as_hal( + pub unsafe fn as_hal( &self, ) -> Option + WasmNotSendSync> { let queue = self.inner.as_core_opt()?; diff --git a/wgpu/src/api/surface.rs b/wgpu/src/api/surface.rs index ced7a47b2..113f46639 100644 --- a/wgpu/src/api/surface.rs +++ b/wgpu/src/api/surface.rs @@ -182,7 +182,7 @@ impl Surface<'_> { /// /// [`A::Surface`]: hal::Api::Surface #[cfg(wgpu_core)] - pub unsafe fn as_hal( + pub unsafe fn as_hal( &self, ) -> Option + WasmNotSendSync> { let core_surface = self.inner.as_core_opt()?; diff --git a/wgpu/src/api/texture.rs b/wgpu/src/api/texture.rs index 2e2764e81..98136ece4 100644 --- a/wgpu/src/api/texture.rs +++ b/wgpu/src/api/texture.rs @@ -58,9 +58,7 @@ impl Texture { /// /// [`A::Texture`]: hal::Api::Texture #[cfg(wgpu_core)] - pub unsafe fn as_hal( - &self, - ) -> Option> { + pub unsafe fn as_hal(&self) -> Option> { let texture = self.inner.as_core_opt()?; unsafe { texture.context.texture_as_hal::(texture) } } diff --git a/wgpu/src/api/texture_view.rs b/wgpu/src/api/texture_view.rs index faa300d1c..7d578a25f 100644 --- a/wgpu/src/api/texture_view.rs +++ b/wgpu/src/api/texture_view.rs @@ -70,9 +70,7 @@ impl TextureView { /// /// [`A::TextureView`]: hal::Api::TextureView #[cfg(wgpu_core)] - pub unsafe fn as_hal( - &self, - ) -> Option> { + pub unsafe fn as_hal(&self) -> Option> { let view = self.inner.as_core_opt()?; unsafe { view.context.texture_view_as_hal::(view) } } diff --git a/wgpu/src/api/tlas.rs b/wgpu/src/api/tlas.rs index 07d6d0da4..4b1c34ad4 100644 --- a/wgpu/src/api/tlas.rs +++ b/wgpu/src/api/tlas.rs @@ -68,7 +68,7 @@ impl Tlas { /// /// [`A::AccelerationStructure`]: hal::Api::AccelerationStructure #[cfg(wgpu_core)] - pub unsafe fn as_hal( + pub unsafe fn as_hal( &mut self, ) -> Option> { let tlas = self.inner.as_core_opt()?; diff --git a/wgpu/src/backend/wgpu_core.rs b/wgpu/src/backend/wgpu_core.rs index f1509ac56..8fed4d9f8 100644 --- a/wgpu/src/backend/wgpu_core.rs +++ b/wgpu/src/backend/wgpu_core.rs @@ -55,7 +55,7 @@ impl fmt::Debug for ContextWgpuCore { } impl ContextWgpuCore { - pub unsafe fn from_hal_instance(hal_instance: A::Instance) -> Self { + pub unsafe fn from_hal_instance(hal_instance: A::Instance) -> Self { Self(unsafe { Arc::new(wgc::global::Global::from_hal_instance::( "wgpu", @@ -67,7 +67,7 @@ impl ContextWgpuCore { /// # Safety /// /// - The raw instance handle returned must not be manually destroyed. - pub unsafe fn instance_as_hal(&self) -> Option<&A::Instance> { + pub unsafe fn instance_as_hal(&self) -> Option<&A::Instance> { unsafe { self.0.instance_as_hal::() } } @@ -80,28 +80,28 @@ impl ContextWgpuCore { self.0.enumerate_adapters(backends) } - pub unsafe fn create_adapter_from_hal( + pub unsafe fn create_adapter_from_hal( &self, hal_adapter: hal::ExposedAdapter, ) -> wgc::id::AdapterId { unsafe { self.0.create_adapter_from_hal(hal_adapter.into(), None) } } - pub unsafe fn adapter_as_hal( + pub unsafe fn adapter_as_hal( &self, adapter: &CoreAdapter, ) -> Option + WasmNotSendSync> { unsafe { self.0.adapter_as_hal::(adapter.id) } } - pub unsafe fn buffer_as_hal( + pub unsafe fn buffer_as_hal( &self, buffer: &CoreBuffer, ) -> Option> { unsafe { self.0.buffer_as_hal::(buffer.id) } } - pub unsafe fn create_device_from_hal( + pub unsafe fn create_device_from_hal( &self, adapter: &CoreAdapter, hal_device: hal::OpenDevice, @@ -140,7 +140,7 @@ impl ContextWgpuCore { Ok((device, queue)) } - pub unsafe fn create_texture_from_hal( + pub unsafe fn create_texture_from_hal( &self, hal_texture: A::Texture, device: &CoreDevice, @@ -172,7 +172,7 @@ impl ContextWgpuCore { /// - `hal_buffer` must be created respecting `desc` /// - `hal_buffer` must be initialized /// - `hal_buffer` must not have zero size. - pub unsafe fn create_buffer_from_hal( + pub unsafe fn create_buffer_from_hal( &self, hal_buffer: A::Buffer, device: &CoreDevice, @@ -201,28 +201,28 @@ impl ContextWgpuCore { } } - pub unsafe fn device_as_hal( + pub unsafe fn device_as_hal( &self, device: &CoreDevice, ) -> Option> { unsafe { self.0.device_as_hal::(device.id) } } - pub unsafe fn surface_as_hal( + pub unsafe fn surface_as_hal( &self, surface: &CoreSurface, ) -> Option> { unsafe { self.0.surface_as_hal::(surface.id) } } - pub unsafe fn texture_as_hal( + pub unsafe fn texture_as_hal( &self, texture: &CoreTexture, ) -> Option> { unsafe { self.0.texture_as_hal::(texture.id) } } - pub unsafe fn texture_view_as_hal( + pub unsafe fn texture_view_as_hal( &self, texture_view: &CoreTextureView, ) -> Option> { @@ -231,7 +231,7 @@ impl ContextWgpuCore { /// This method will start the wgpu_core level command recording. pub unsafe fn command_encoder_as_hal_mut< - A: wgc::hal_api::HalApi, + A: hal::Api, F: FnOnce(Option<&mut A::CommandEncoder>) -> R, R, >( @@ -247,14 +247,14 @@ impl ContextWgpuCore { } } - pub unsafe fn blas_as_hal( + pub unsafe fn blas_as_hal( &self, blas: &CoreBlas, ) -> Option> { unsafe { self.0.blas_as_hal::(blas.id) } } - pub unsafe fn tlas_as_hal( + pub unsafe fn tlas_as_hal( &self, tlas: &CoreTlas, ) -> Option> { @@ -369,7 +369,7 @@ impl ContextWgpuCore { format!("Validation Error\n\nCaused by:\n{output}") } - pub unsafe fn queue_as_hal( + pub unsafe fn queue_as_hal( &self, queue: &CoreQueue, ) -> Option + WasmNotSendSync> {