Remove wgpu_core::hal_api::HalApi

This commit is contained in:
Connor Fitzgerald 2025-07-11 00:26:02 -04:00 committed by Erich Gubler
parent 0dbe5fb28e
commit 1ed61877b5
24 changed files with 62 additions and 92 deletions

View File

@ -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<A: HalApi>(
pub unsafe fn buffer_as_hal<A: hal::Api>(
&self,
id: BufferId,
) -> Option<impl Deref<Target = A::Buffer>> {
@ -242,7 +241,7 @@ impl Global {
/// # Safety
///
/// - The raw texture handle must not be manually destroyed
pub unsafe fn texture_as_hal<A: HalApi>(
pub unsafe fn texture_as_hal<A: hal::Api>(
&self,
id: TextureId,
) -> Option<impl Deref<Target = A::Texture>> {
@ -258,7 +257,7 @@ impl Global {
/// # Safety
///
/// - The raw texture view handle must not be manually destroyed
pub unsafe fn texture_view_as_hal<A: HalApi>(
pub unsafe fn texture_view_as_hal<A: hal::Api>(
&self,
id: TextureViewId,
) -> Option<impl Deref<Target = A::TextureView>> {
@ -274,7 +273,7 @@ impl Global {
/// # Safety
///
/// - The raw adapter handle must not be manually destroyed
pub unsafe fn adapter_as_hal<A: HalApi>(
pub unsafe fn adapter_as_hal<A: hal::Api>(
&self,
id: AdapterId,
) -> Option<impl Deref<Target = A::Adapter>> {
@ -291,7 +290,7 @@ impl Global {
/// # Safety
///
/// - The raw device handle must not be manually destroyed
pub unsafe fn device_as_hal<A: HalApi>(
pub unsafe fn device_as_hal<A: hal::Api>(
&self,
id: DeviceId,
) -> Option<impl Deref<Target = A::Device>> {
@ -305,7 +304,7 @@ impl Global {
/// # Safety
///
/// - The raw fence handle must not be manually destroyed
pub unsafe fn device_fence_as_hal<A: HalApi>(
pub unsafe fn device_fence_as_hal<A: hal::Api>(
&self,
id: DeviceId,
) -> Option<impl Deref<Target = A::Fence>> {
@ -318,7 +317,7 @@ impl Global {
/// # Safety
/// - The raw surface handle must not be manually destroyed
pub unsafe fn surface_as_hal<A: HalApi>(
pub unsafe fn surface_as_hal<A: hal::Api>(
&self,
id: SurfaceId,
) -> Option<impl Deref<Target = A::Surface>> {
@ -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<A: HalApi>(
pub unsafe fn queue_as_hal<A: hal::Api>(
&self,
id: QueueId,
) -> Option<impl Deref<Target = A::Queue>> {
@ -377,7 +376,7 @@ impl Global {
/// # Safety
///
/// - The raw blas handle must not be manually destroyed
pub unsafe fn blas_as_hal<A: HalApi>(
pub unsafe fn blas_as_hal<A: hal::Api>(
&self,
id: BlasId,
) -> Option<impl Deref<Target = A::AccelerationStructure>> {
@ -393,7 +392,7 @@ impl Global {
/// # Safety
///
/// - The raw tlas handle must not be manually destroyed
pub unsafe fn tlas_as_hal<A: HalApi>(
pub unsafe fn tlas_as_hal<A: hal::Api>(
&self,
id: TlasId,
) -> Option<impl Deref<Target = A::AccelerationStructure>> {

View File

@ -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<A: HalApi>(
pub unsafe fn create_buffer_from_hal<A: hal::Api>(
&self,
hal_buffer: A::Buffer,
device_id: DeviceId,

View File

@ -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<A: HalApi>(name: &str, hal_instance: A::Instance) -> Self {
pub unsafe fn from_hal_instance<A: hal::Api>(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<A: HalApi>(&self) -> Option<&A::Instance> {
pub unsafe fn instance_as_hal<A: hal::Api>(&self) -> Option<&A::Instance> {
unsafe { self.instance.as_hal::<A>() }
}

View File

@ -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;
}

View File

@ -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<A: HalApi>(&mut self, _: A, instance_desc: &wgt::InstanceDescriptor) {
fn try_add_hal<A: hal::Api>(&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<A: HalApi>(
pub(crate) fn from_hal_instance<A: hal::Api>(
name: String,
hal_instance: <A as hal::Api>::Instance,
) -> Self {
@ -175,7 +174,7 @@ impl Instance {
/// # Safety
///
/// - The raw instance handle returned must not be manually destroyed.
pub unsafe fn as_hal<A: HalApi>(&self) -> Option<&A::Instance> {
pub unsafe fn as_hal<A: hal::Api>(&self) -> Option<&A::Instance> {
self.raw(A::VARIANT).map(|instance| {
instance
.as_any()

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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<A = Self>;
type Surface: DynSurface + Surface<A = Self>;
type Adapter: DynAdapter + Adapter<A = Self>;

View File

@ -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;

View File

@ -31,6 +31,8 @@ pub struct Fence {
type DeviceResult<T> = Result<T, crate::DeviceError>;
impl crate::Api for Api {
const VARIANT: wgt::Backend = wgt::Backend::Noop;
type Instance = Context;
type Surface = Context;
type Adapter = Context;

View File

@ -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;

View File

@ -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<A: wgc::hal_api::HalApi>(
pub unsafe fn create_device_from_hal<A: hal::Api>(
&self,
hal_device: hal::OpenDevice<A>,
desc: &DeviceDescriptor<'_>,
@ -127,7 +127,7 @@ impl Adapter {
///
/// [`A::Adapter`]: hal::Api::Adapter
#[cfg(wgpu_core)]
pub unsafe fn as_hal<A: wgc::hal_api::HalApi>(
pub unsafe fn as_hal<A: hal::Api>(
&self,
) -> Option<impl Deref<Target = A::Adapter> + WasmNotSendSync> {
let adapter = self.inner.as_core_opt()?;

View File

@ -192,7 +192,7 @@ impl Blas {
///
/// [`A::AccelerationStructure`]: hal::Api::AccelerationStructure
#[cfg(wgpu_core)]
pub unsafe fn as_hal<A: wgc::hal_api::HalApi>(
pub unsafe fn as_hal<A: hal::Api>(
&mut self,
) -> Option<impl Deref<Target = A::AccelerationStructure> + WasmNotSendSync> {
let blas = self.inner.as_core_opt()?;

View File

@ -239,7 +239,7 @@ impl Buffer {
///
/// [`A::Buffer`]: hal::Api::Buffer
#[cfg(wgpu_core)]
pub unsafe fn as_hal<A: wgc::hal_api::HalApi>(
pub unsafe fn as_hal<A: hal::Api>(
&self,
) -> Option<impl Deref<Target = A::Buffer> + WasmNotSendSync> {
let buffer = self.inner.as_core_opt()?;

View File

@ -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<A: hal::Api, F: FnOnce(Option<&mut A::CommandEncoder>) -> R, R>(
&mut self,
hal_command_encoder_callback: F,
) -> R {

View File

@ -305,7 +305,7 @@ impl Device {
/// - `hal_texture` must be initialized
#[cfg(wgpu_core)]
#[must_use]
pub unsafe fn create_texture_from_hal<A: wgc::hal_api::HalApi>(
pub unsafe fn create_texture_from_hal<A: hal::Api>(
&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<A: wgc::hal_api::HalApi>(
pub unsafe fn create_buffer_from_hal<A: hal::Api>(
&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<A: wgc::hal_api::HalApi>(
pub unsafe fn as_hal<A: hal::Api>(
&self,
) -> Option<impl Deref<Target = A::Device> + WasmNotSendSync> {
let device = self.inner.as_core_opt()?;

View File

@ -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<A: wgc::hal_api::HalApi>(hal_instance: A::Instance) -> Self {
pub unsafe fn from_hal<A: hal::Api>(hal_instance: A::Instance) -> Self {
Self {
inner: unsafe {
crate::backend::ContextWgpuCore::from_hal_instance::<A>(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<A: wgc::hal_api::HalApi>(&self) -> Option<&A::Instance> {
pub unsafe fn as_hal<A: hal::Api>(&self) -> Option<&A::Instance> {
self.inner
.as_core_opt()
.and_then(|ctx| unsafe { ctx.instance_as_hal::<A>() })
@ -382,7 +382,7 @@ impl Instance {
/// # Safety
///
/// `hal_adapter` must be created from this instance internal handle.
pub unsafe fn create_adapter_from_hal<A: wgc::hal_api::HalApi>(
pub unsafe fn create_adapter_from_hal<A: hal::Api>(
&self,
hal_adapter: hal::ExposedAdapter<A>,
) -> Adapter {

View File

@ -312,7 +312,7 @@ impl Queue {
///
/// [`A::Queue`]: hal::Api::Queue
#[cfg(wgpu_core)]
pub unsafe fn as_hal<A: wgc::hal_api::HalApi>(
pub unsafe fn as_hal<A: hal::Api>(
&self,
) -> Option<impl Deref<Target = A::Queue> + WasmNotSendSync> {
let queue = self.inner.as_core_opt()?;

View File

@ -182,7 +182,7 @@ impl Surface<'_> {
///
/// [`A::Surface`]: hal::Api::Surface
#[cfg(wgpu_core)]
pub unsafe fn as_hal<A: wgc::hal_api::HalApi>(
pub unsafe fn as_hal<A: hal::Api>(
&self,
) -> Option<impl Deref<Target = A::Surface> + WasmNotSendSync> {
let core_surface = self.inner.as_core_opt()?;

View File

@ -58,9 +58,7 @@ impl Texture {
///
/// [`A::Texture`]: hal::Api::Texture
#[cfg(wgpu_core)]
pub unsafe fn as_hal<A: wgc::hal_api::HalApi>(
&self,
) -> Option<impl Deref<Target = A::Texture>> {
pub unsafe fn as_hal<A: hal::Api>(&self) -> Option<impl Deref<Target = A::Texture>> {
let texture = self.inner.as_core_opt()?;
unsafe { texture.context.texture_as_hal::<A>(texture) }
}

View File

@ -70,9 +70,7 @@ impl TextureView {
///
/// [`A::TextureView`]: hal::Api::TextureView
#[cfg(wgpu_core)]
pub unsafe fn as_hal<A: wgc::hal_api::HalApi>(
&self,
) -> Option<impl Deref<Target = A::TextureView>> {
pub unsafe fn as_hal<A: hal::Api>(&self) -> Option<impl Deref<Target = A::TextureView>> {
let view = self.inner.as_core_opt()?;
unsafe { view.context.texture_view_as_hal::<A>(view) }
}

View File

@ -68,7 +68,7 @@ impl Tlas {
///
/// [`A::AccelerationStructure`]: hal::Api::AccelerationStructure
#[cfg(wgpu_core)]
pub unsafe fn as_hal<A: wgc::hal_api::HalApi>(
pub unsafe fn as_hal<A: hal::Api>(
&mut self,
) -> Option<impl Deref<Target = A::AccelerationStructure>> {
let tlas = self.inner.as_core_opt()?;

View File

@ -55,7 +55,7 @@ impl fmt::Debug for ContextWgpuCore {
}
impl ContextWgpuCore {
pub unsafe fn from_hal_instance<A: wgc::hal_api::HalApi>(hal_instance: A::Instance) -> Self {
pub unsafe fn from_hal_instance<A: hal::Api>(hal_instance: A::Instance) -> Self {
Self(unsafe {
Arc::new(wgc::global::Global::from_hal_instance::<A>(
"wgpu",
@ -67,7 +67,7 @@ impl ContextWgpuCore {
/// # Safety
///
/// - The raw instance handle returned must not be manually destroyed.
pub unsafe fn instance_as_hal<A: wgc::hal_api::HalApi>(&self) -> Option<&A::Instance> {
pub unsafe fn instance_as_hal<A: hal::Api>(&self) -> Option<&A::Instance> {
unsafe { self.0.instance_as_hal::<A>() }
}
@ -80,28 +80,28 @@ impl ContextWgpuCore {
self.0.enumerate_adapters(backends)
}
pub unsafe fn create_adapter_from_hal<A: wgc::hal_api::HalApi>(
pub unsafe fn create_adapter_from_hal<A: hal::Api>(
&self,
hal_adapter: hal::ExposedAdapter<A>,
) -> wgc::id::AdapterId {
unsafe { self.0.create_adapter_from_hal(hal_adapter.into(), None) }
}
pub unsafe fn adapter_as_hal<A: wgc::hal_api::HalApi>(
pub unsafe fn adapter_as_hal<A: hal::Api>(
&self,
adapter: &CoreAdapter,
) -> Option<impl Deref<Target = A::Adapter> + WasmNotSendSync> {
unsafe { self.0.adapter_as_hal::<A>(adapter.id) }
}
pub unsafe fn buffer_as_hal<A: wgc::hal_api::HalApi>(
pub unsafe fn buffer_as_hal<A: hal::Api>(
&self,
buffer: &CoreBuffer,
) -> Option<impl Deref<Target = A::Buffer>> {
unsafe { self.0.buffer_as_hal::<A>(buffer.id) }
}
pub unsafe fn create_device_from_hal<A: wgc::hal_api::HalApi>(
pub unsafe fn create_device_from_hal<A: hal::Api>(
&self,
adapter: &CoreAdapter,
hal_device: hal::OpenDevice<A>,
@ -140,7 +140,7 @@ impl ContextWgpuCore {
Ok((device, queue))
}
pub unsafe fn create_texture_from_hal<A: wgc::hal_api::HalApi>(
pub unsafe fn create_texture_from_hal<A: hal::Api>(
&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<A: wgc::hal_api::HalApi>(
pub unsafe fn create_buffer_from_hal<A: hal::Api>(
&self,
hal_buffer: A::Buffer,
device: &CoreDevice,
@ -201,28 +201,28 @@ impl ContextWgpuCore {
}
}
pub unsafe fn device_as_hal<A: wgc::hal_api::HalApi>(
pub unsafe fn device_as_hal<A: hal::Api>(
&self,
device: &CoreDevice,
) -> Option<impl Deref<Target = A::Device>> {
unsafe { self.0.device_as_hal::<A>(device.id) }
}
pub unsafe fn surface_as_hal<A: wgc::hal_api::HalApi>(
pub unsafe fn surface_as_hal<A: hal::Api>(
&self,
surface: &CoreSurface,
) -> Option<impl Deref<Target = A::Surface>> {
unsafe { self.0.surface_as_hal::<A>(surface.id) }
}
pub unsafe fn texture_as_hal<A: wgc::hal_api::HalApi>(
pub unsafe fn texture_as_hal<A: hal::Api>(
&self,
texture: &CoreTexture,
) -> Option<impl Deref<Target = A::Texture>> {
unsafe { self.0.texture_as_hal::<A>(texture.id) }
}
pub unsafe fn texture_view_as_hal<A: wgc::hal_api::HalApi>(
pub unsafe fn texture_view_as_hal<A: hal::Api>(
&self,
texture_view: &CoreTextureView,
) -> Option<impl Deref<Target = A::TextureView>> {
@ -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<A: wgc::hal_api::HalApi>(
pub unsafe fn blas_as_hal<A: hal::Api>(
&self,
blas: &CoreBlas,
) -> Option<impl Deref<Target = A::AccelerationStructure>> {
unsafe { self.0.blas_as_hal::<A>(blas.id) }
}
pub unsafe fn tlas_as_hal<A: wgc::hal_api::HalApi>(
pub unsafe fn tlas_as_hal<A: hal::Api>(
&self,
tlas: &CoreTlas,
) -> Option<impl Deref<Target = A::AccelerationStructure>> {
@ -369,7 +369,7 @@ impl ContextWgpuCore {
format!("Validation Error\n\nCaused by:\n{output}")
}
pub unsafe fn queue_as_hal<A: wgc::hal_api::HalApi>(
pub unsafe fn queue_as_hal<A: hal::Api>(
&self,
queue: &CoreQueue,
) -> Option<impl Deref<Target = A::Queue> + WasmNotSendSync> {