diff --git a/Cargo.lock b/Cargo.lock index 1455e39af3..86d5534198 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4001,6 +4001,7 @@ dependencies = [ "arrayvec 0.7.4", "bit-vec", "bitflags 2.4.1", + "cfg_aliases", "codespan-reporting", "indexmap", "log", @@ -4063,6 +4064,7 @@ dependencies = [ "bitflags 2.4.1", "block", "cfg-if", + "cfg_aliases", "core-graphics-types", "d3d12", "env_logger", diff --git a/wgpu-core/Cargo.toml b/wgpu-core/Cargo.toml index 965382201d..eed57cc406 100644 --- a/wgpu-core/Cargo.toml +++ b/wgpu-core/Cargo.toml @@ -128,3 +128,6 @@ web-sys = { version = "0.3.66", features = [ "HtmlCanvasElement", "OffscreenCanvas", ] } + +[build-dependencies] +cfg_aliases.workspace = true diff --git a/wgpu-core/build.rs b/wgpu-core/build.rs new file mode 100644 index 0000000000..2f715fdb2a --- /dev/null +++ b/wgpu-core/build.rs @@ -0,0 +1,13 @@ +fn main() { + cfg_aliases::cfg_aliases! { + send_sync: { any( + not(target_arch = "wasm32"), + all(feature = "fragile-send-sync-non-atomic-wasm", not(target_feature = "atomics")) + ) }, + webgl: { all(target_arch = "wasm32", not(target_os = "emscripten"), gles) }, + dx12: { all(target_os = "windows", feature = "dx12") }, + gles: { all(feature = "gles") }, + metal: { all(any(target_os = "ios", target_os = "macos"), feature = "metal") }, + vulkan: { all(not(target_arch = "wasm32"), feature = "vulkan") } + } +} diff --git a/wgpu-core/src/any_surface.rs b/wgpu-core/src/any_surface.rs index 5ae3295dc1..48d4b1d45a 100644 --- a/wgpu-core/src/any_surface.rs +++ b/wgpu-core/src/any_surface.rs @@ -23,19 +23,19 @@ impl AnySurface { } pub fn backend(&self) -> Backend { - #[cfg(all(feature = "vulkan", not(target_arch = "wasm32")))] + #[cfg(vulkan)] if self.downcast_ref::().is_some() { return Backend::Vulkan; } - #[cfg(all(feature = "metal", any(target_os = "macos", target_os = "ios")))] + #[cfg(metal)] if self.downcast_ref::().is_some() { return Backend::Metal; } - #[cfg(all(feature = "dx12", windows))] + #[cfg(dx12)] if self.downcast_ref::().is_some() { return Backend::Dx12; } - #[cfg(feature = "gles")] + #[cfg(gles)] if self.downcast_ref::().is_some() { return Backend::Gl; } @@ -90,19 +90,7 @@ impl fmt::Debug for AnySurface { } } -#[cfg(any( - not(target_arch = "wasm32"), - all( - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") - ) -))] +#[cfg(send_sync)] unsafe impl Send for AnySurface {} -#[cfg(any( - not(target_arch = "wasm32"), - all( - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") - ) -))] +#[cfg(send_sync)] unsafe impl Sync for AnySurface {} diff --git a/wgpu-core/src/command/bundle.rs b/wgpu-core/src/command/bundle.rs index cfbd7dfa1d..40e652fef4 100644 --- a/wgpu-core/src/command/bundle.rs +++ b/wgpu-core/src/command/bundle.rs @@ -777,21 +777,9 @@ impl Drop for RenderBundle { } } -#[cfg(any( - not(target_arch = "wasm32"), - all( - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") - ) -))] +#[cfg(send_sync)] unsafe impl Send for RenderBundle {} -#[cfg(any( - not(target_arch = "wasm32"), - all( - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") - ) -))] +#[cfg(send_sync)] unsafe impl Sync for RenderBundle {} impl RenderBundle { diff --git a/wgpu-core/src/device/any_device.rs b/wgpu-core/src/device/any_device.rs index ab13b1421f..b3c4a31839 100644 --- a/wgpu-core/src/device/any_device.rs +++ b/wgpu-core/src/device/any_device.rs @@ -70,19 +70,7 @@ impl fmt::Debug for AnyDevice { } } -#[cfg(any( - not(target_arch = "wasm32"), - all( - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") - ) -))] +#[cfg(send_sync)] unsafe impl Send for AnyDevice {} -#[cfg(any( - not(target_arch = "wasm32"), - all( - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") - ) -))] +#[cfg(send_sync)] unsafe impl Sync for AnyDevice {} diff --git a/wgpu-core/src/device/global.rs b/wgpu-core/src/device/global.rs index dfbd359ba8..fa0c4d7dbe 100644 --- a/wgpu-core/src/device/global.rs +++ b/wgpu-core/src/device/global.rs @@ -2164,22 +2164,22 @@ impl Global { let mut closures = UserClosures::default(); let mut all_queue_empty = true; - #[cfg(all(feature = "vulkan", not(target_arch = "wasm32")))] + #[cfg(vulkan)] { all_queue_empty = self.poll_device::(force_wait, &mut closures)? && all_queue_empty; } - #[cfg(all(feature = "metal", any(target_os = "macos", target_os = "ios")))] + #[cfg(metal)] { all_queue_empty = self.poll_device::(force_wait, &mut closures)? && all_queue_empty; } - #[cfg(all(feature = "dx12", windows))] + #[cfg(dx12)] { all_queue_empty = self.poll_device::(force_wait, &mut closures)? && all_queue_empty; } - #[cfg(feature = "gles")] + #[cfg(gles)] { all_queue_empty = self.poll_device::(force_wait, &mut closures)? && all_queue_empty; diff --git a/wgpu-core/src/device/mod.rs b/wgpu-core/src/device/mod.rs index bb0afedafc..dee31e3426 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -205,21 +205,9 @@ impl UserClosures { } } -#[cfg(any( - not(target_arch = "wasm32"), - all( - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") - ) -))] +#[cfg(send_sync)] pub type DeviceLostCallback = Box; -#[cfg(not(any( - not(target_arch = "wasm32"), - all( - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") - ) -)))] +#[cfg(not(send_sync))] pub type DeviceLostCallback = Box; pub struct DeviceLostClosureRust { @@ -242,13 +230,7 @@ pub struct DeviceLostClosureC { called: bool, } -#[cfg(any( - not(target_arch = "wasm32"), - all( - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") - ) -))] +#[cfg(send_sync)] unsafe impl Send for DeviceLostClosureC {} impl Drop for DeviceLostClosureC { diff --git a/wgpu-core/src/device/queue.rs b/wgpu-core/src/device/queue.rs index 6a3759a12e..be5c202ffb 100644 --- a/wgpu-core/src/device/queue.rs +++ b/wgpu-core/src/device/queue.rs @@ -71,13 +71,7 @@ pub struct SubmittedWorkDoneClosureC { pub user_data: *mut u8, } -#[cfg(any( - not(target_arch = "wasm32"), - all( - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") - ) -))] +#[cfg(send_sync)] unsafe impl Send for SubmittedWorkDoneClosureC {} pub struct SubmittedWorkDoneClosure { @@ -86,21 +80,9 @@ pub struct SubmittedWorkDoneClosure { inner: SubmittedWorkDoneClosureInner, } -#[cfg(any( - not(target_arch = "wasm32"), - all( - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") - ) -))] +#[cfg(send_sync)] type SubmittedWorkDoneCallback = Box; -#[cfg(not(any( - not(target_arch = "wasm32"), - all( - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") - ) -)))] +#[cfg(not(send_sync))] type SubmittedWorkDoneCallback = Box; enum SubmittedWorkDoneClosureInner { @@ -911,7 +893,7 @@ impl Global { Ok(()) } - #[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))] + #[cfg(webgl)] pub fn queue_copy_external_image_to_texture( &self, queue_id: QueueId, diff --git a/wgpu-core/src/error.rs b/wgpu-core/src/error.rs index aa9797fd42..a6e3d31587 100644 --- a/wgpu-core/src/error.rs +++ b/wgpu-core/src/error.rs @@ -162,21 +162,9 @@ pub fn format_pretty_any( #[derive(Debug)] pub struct ContextError { pub string: &'static str, - #[cfg(any( - not(target_arch = "wasm32"), - all( - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") - ) - ))] + #[cfg(send_sync)] pub cause: Box, - #[cfg(not(any( - not(target_arch = "wasm32"), - all( - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") - ) - )))] + #[cfg(not(send_sync))] pub cause: Box, pub label_key: &'static str, pub label: String, diff --git a/wgpu-core/src/global.rs b/wgpu-core/src/global.rs index 8cb4a9d64f..87271917c2 100644 --- a/wgpu-core/src/global.rs +++ b/wgpu-core/src/global.rs @@ -16,13 +16,13 @@ use crate::{ #[derive(Debug, PartialEq, Eq)] pub struct GlobalReport { pub surfaces: RegistryReport, - #[cfg(all(feature = "vulkan", not(target_arch = "wasm32")))] + #[cfg(vulkan)] pub vulkan: Option, - #[cfg(all(feature = "metal", any(target_os = "macos", target_os = "ios")))] + #[cfg(metal)] pub metal: Option, - #[cfg(all(feature = "dx12", windows))] + #[cfg(dx12)] pub dx12: Option, - #[cfg(feature = "gles")] + #[cfg(gles)] pub gl: Option, } @@ -32,13 +32,13 @@ impl GlobalReport { } pub fn hub_report(&self, backend: Backend) -> &HubReport { match backend { - #[cfg(all(feature = "vulkan", not(target_arch = "wasm32")))] + #[cfg(vulkan)] Backend::Vulkan => self.vulkan.as_ref().unwrap(), - #[cfg(all(feature = "metal", any(target_os = "macos", target_os = "ios")))] + #[cfg(metal)] Backend::Metal => self.metal.as_ref().unwrap(), - #[cfg(all(feature = "dx12", windows))] + #[cfg(dx12)] Backend::Dx12 => self.dx12.as_ref().unwrap(), - #[cfg(feature = "gles")] + #[cfg(gles)] Backend::Gl => self.gl.as_ref().unwrap(), _ => panic!("HubReport is not supported on this backend"), } @@ -110,25 +110,25 @@ impl Global { pub fn generate_report(&self) -> GlobalReport { GlobalReport { surfaces: self.surfaces.generate_report(), - #[cfg(all(feature = "vulkan", not(target_arch = "wasm32")))] + #[cfg(vulkan)] vulkan: if self.instance.vulkan.is_some() { Some(self.hubs.vulkan.generate_report()) } else { None }, - #[cfg(all(feature = "metal", any(target_os = "macos", target_os = "ios")))] + #[cfg(metal)] metal: if self.instance.metal.is_some() { Some(self.hubs.metal.generate_report()) } else { None }, - #[cfg(all(feature = "dx12", windows))] + #[cfg(dx12)] dx12: if self.instance.dx12.is_some() { Some(self.hubs.dx12.generate_report()) } else { None }, - #[cfg(feature = "gles")] + #[cfg(gles)] gl: if self.instance.gl.is_some() { Some(self.hubs.gl.generate_report()) } else { @@ -145,19 +145,19 @@ impl Drop for Global { let mut surfaces_locked = self.surfaces.write(); // destroy hubs before the instance gets dropped - #[cfg(all(feature = "vulkan", not(target_arch = "wasm32")))] + #[cfg(vulkan)] { self.hubs.vulkan.clear(&surfaces_locked, true); } - #[cfg(all(feature = "metal", any(target_os = "macos", target_os = "ios")))] + #[cfg(metal)] { self.hubs.metal.clear(&surfaces_locked, true); } - #[cfg(all(feature = "dx12", windows))] + #[cfg(dx12)] { self.hubs.dx12.clear(&surfaces_locked, true); } - #[cfg(feature = "gles")] + #[cfg(gles)] { self.hubs.gl.clear(&surfaces_locked, true); } @@ -175,16 +175,7 @@ impl Drop for Global { } } -#[cfg(all( - test, - any( - not(target_arch = "wasm32"), - all( - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") - ) - ) -))] +#[cfg(send_sync)] fn _test_send_sync(global: &Global) { fn test_internal(_: T) {} test_internal(global) diff --git a/wgpu-core/src/hal_api.rs b/wgpu-core/src/hal_api.rs index a19b3c4bb7..d1dee98ed1 100644 --- a/wgpu-core/src/hal_api.rs +++ b/wgpu-core/src/hal_api.rs @@ -31,7 +31,7 @@ impl HalApi for hal::api::Empty { } } -#[cfg(all(feature = "vulkan", not(target_arch = "wasm32")))] +#[cfg(vulkan)] impl HalApi for hal::api::Vulkan { const VARIANT: Backend = Backend::Vulkan; fn create_instance_from_hal(name: &str, hal_instance: Self::Instance) -> Instance { @@ -52,7 +52,7 @@ impl HalApi for hal::api::Vulkan { } } -#[cfg(all(feature = "metal", any(target_os = "macos", target_os = "ios")))] +#[cfg(metal)] impl HalApi for hal::api::Metal { const VARIANT: Backend = Backend::Metal; fn create_instance_from_hal(name: &str, hal_instance: Self::Instance) -> Instance { @@ -73,7 +73,7 @@ impl HalApi for hal::api::Metal { } } -#[cfg(all(feature = "dx12", windows))] +#[cfg(dx12)] impl HalApi for hal::api::Dx12 { const VARIANT: Backend = Backend::Dx12; fn create_instance_from_hal(name: &str, hal_instance: Self::Instance) -> Instance { @@ -94,7 +94,7 @@ impl HalApi for hal::api::Dx12 { } } -#[cfg(feature = "gles")] +#[cfg(gles)] impl HalApi for hal::api::Gles { const VARIANT: Backend = Backend::Gl; fn create_instance_from_hal(name: &str, hal_instance: Self::Instance) -> Instance { diff --git a/wgpu-core/src/hub.rs b/wgpu-core/src/hub.rs index a2f89f92df..22208fdf5f 100644 --- a/wgpu-core/src/hub.rs +++ b/wgpu-core/src/hub.rs @@ -302,40 +302,30 @@ impl Hub { } pub struct Hubs { - #[cfg(all(feature = "vulkan", not(target_arch = "wasm32")))] + #[cfg(vulkan)] pub(crate) vulkan: Hub, - #[cfg(all(feature = "metal", any(target_os = "macos", target_os = "ios")))] + #[cfg(metal)] pub(crate) metal: Hub, - #[cfg(all(feature = "dx12", windows))] + #[cfg(dx12)] pub(crate) dx12: Hub, - #[cfg(feature = "gles")] + #[cfg(gles)] pub(crate) gl: Hub, - #[cfg(all( - not(all(feature = "vulkan", not(target_arch = "wasm32"))), - not(all(feature = "metal", any(target_os = "macos", target_os = "ios"))), - not(all(feature = "dx12", windows)), - not(feature = "gles"), - ))] + #[cfg(all(not(vulkan), not(metal), not(dx12), not(gles)))] pub(crate) empty: Hub, } impl Hubs { pub(crate) fn new(factory: &F) -> Self { Self { - #[cfg(all(feature = "vulkan", not(target_arch = "wasm32")))] + #[cfg(vulkan)] vulkan: Hub::new(factory), - #[cfg(all(feature = "metal", any(target_os = "macos", target_os = "ios")))] + #[cfg(metal)] metal: Hub::new(factory), - #[cfg(all(feature = "dx12", windows))] + #[cfg(dx12)] dx12: Hub::new(factory), - #[cfg(feature = "gles")] + #[cfg(gles)] gl: Hub::new(factory), - #[cfg(all( - not(all(feature = "vulkan", not(target_arch = "wasm32"))), - not(all(feature = "metal", any(target_os = "macos", target_os = "ios"))), - not(all(feature = "dx12", windows)), - not(feature = "gles"), - ))] + #[cfg(all(not(vulkan), not(metal), not(dx12), not(gles)))] empty: Hub::new(factory), } } diff --git a/wgpu-core/src/instance.rs b/wgpu-core/src/instance.rs index 2101fb8edc..158a305772 100644 --- a/wgpu-core/src/instance.rs +++ b/wgpu-core/src/instance.rs @@ -62,13 +62,13 @@ fn downlevel_default_limits_less_than_default_limits() { pub struct Instance { #[allow(dead_code)] pub name: String, - #[cfg(all(feature = "vulkan", not(target_arch = "wasm32")))] + #[cfg(vulkan)] pub vulkan: Option>, - #[cfg(all(feature = "metal", any(target_os = "macos", target_os = "ios")))] + #[cfg(metal)] pub metal: Option>, - #[cfg(all(feature = "dx12", windows))] + #[cfg(dx12)] pub dx12: Option>, - #[cfg(feature = "gles")] + #[cfg(gles)] pub gl: Option>, pub flags: wgt::InstanceFlags, } @@ -105,13 +105,13 @@ impl Instance { Self { name: name.to_string(), - #[cfg(all(feature = "vulkan", not(target_arch = "wasm32")))] + #[cfg(vulkan)] vulkan: init(hal::api::Vulkan, &instance_desc), - #[cfg(all(feature = "metal", any(target_os = "macos", target_os = "ios")))] + #[cfg(metal)] metal: init(hal::api::Metal, &instance_desc), - #[cfg(all(feature = "dx12", windows))] + #[cfg(dx12)] dx12: init(hal::api::Dx12, &instance_desc), - #[cfg(feature = "gles")] + #[cfg(gles)] gl: init(hal::api::Gles, &instance_desc), flags: instance_desc.flags, } @@ -134,13 +134,13 @@ impl Instance { } } match surface.raw.backend() { - #[cfg(all(feature = "vulkan", not(target_arch = "wasm32")))] + #[cfg(vulkan)] Backend::Vulkan => destroy(hal::api::Vulkan, &self.vulkan, surface.raw), - #[cfg(all(feature = "metal", any(target_os = "macos", target_os = "ios")))] + #[cfg(metal)] Backend::Metal => destroy(hal::api::Metal, &self.metal, surface.raw), - #[cfg(all(feature = "dx12", windows))] + #[cfg(dx12)] Backend::Dx12 => destroy(hal::api::Dx12, &self.dx12, surface.raw), - #[cfg(feature = "gles")] + #[cfg(gles)] Backend::Gl => destroy(hal::api::Gles, &self.gl, surface.raw), _ => unreachable!(), } @@ -497,22 +497,22 @@ impl Global { let mut hal_surface: Option> = None; - #[cfg(all(feature = "vulkan", not(target_arch = "wasm32")))] + #[cfg(vulkan)] if hal_surface.is_none() { hal_surface = init::(&self.instance.vulkan, display_handle, window_handle); } - #[cfg(all(feature = "metal", any(target_os = "macos", target_os = "ios")))] + #[cfg(metal)] if hal_surface.is_none() { hal_surface = init::(&self.instance.metal, display_handle, window_handle); } - #[cfg(all(feature = "dx12", windows))] + #[cfg(dx12)] if hal_surface.is_none() { hal_surface = init::(&self.instance.dx12, display_handle, window_handle); } - #[cfg(feature = "gles")] + #[cfg(gles)] if hal_surface.is_none() { hal_surface = init::(&self.instance.gl, display_handle, window_handle); } @@ -533,7 +533,7 @@ impl Global { /// # Safety /// /// `layer` must be a valid pointer. - #[cfg(all(feature = "metal", any(target_os = "macos", target_os = "ios")))] + #[cfg(metal)] pub unsafe fn instance_create_surface_metal( &self, layer: *mut std::ffi::c_void, @@ -565,7 +565,7 @@ impl Global { id } - #[cfg(all(feature = "dx12", windows))] + #[cfg(dx12)] /// # Safety /// /// The visual must be valid and able to be used to make a swapchain with. @@ -596,7 +596,7 @@ impl Global { id } - #[cfg(all(feature = "dx12", windows))] + #[cfg(dx12)] /// # Safety /// /// The surface_handle must be valid and able to be used to make a swapchain with. @@ -629,7 +629,7 @@ impl Global { id } - #[cfg(all(feature = "dx12", windows))] + #[cfg(dx12)] /// # Safety /// /// The swap_chain_panel must be valid and able to be used to make a swapchain with. @@ -683,13 +683,13 @@ impl Global { let surface = self.surfaces.unregister(id); if let Some(surface) = Arc::into_inner(surface.unwrap()) { if let Some(present) = surface.presentation.lock().take() { - #[cfg(all(feature = "vulkan", not(target_arch = "wasm32")))] + #[cfg(vulkan)] unconfigure::<_, hal::api::Vulkan>(self, &surface.raw, &present); - #[cfg(all(feature = "metal", any(target_os = "macos", target_os = "ios")))] + #[cfg(metal)] unconfigure::<_, hal::api::Metal>(self, &surface.raw, &present); - #[cfg(all(feature = "dx12", windows))] + #[cfg(dx12)] unconfigure::<_, hal::api::Dx12>(self, &surface.raw, &present); - #[cfg(feature = "gles")] + #[cfg(gles)] unconfigure::<_, hal::api::Gles>(self, &surface.raw, &present); } @@ -733,23 +733,23 @@ impl Global { let mut adapters = Vec::new(); - #[cfg(all(feature = "vulkan", not(target_arch = "wasm32")))] + #[cfg(vulkan)] self.enumerate( hal::api::Vulkan, &self.instance.vulkan, &inputs, &mut adapters, ); - #[cfg(all(feature = "metal", any(target_os = "macos", target_os = "ios")))] + #[cfg(metal)] self.enumerate( hal::api::Metal, &self.instance.metal, &inputs, &mut adapters, ); - #[cfg(all(feature = "dx12", windows))] + #[cfg(dx12)] self.enumerate(hal::api::Dx12, &self.instance.dx12, &inputs, &mut adapters); - #[cfg(feature = "gles")] + #[cfg(gles)] self.enumerate(hal::api::Gles, &self.instance.gl, &inputs, &mut adapters); adapters @@ -831,7 +831,7 @@ impl Global { let compatible_surface = compatible_surface.as_ref().map(|surface| surface.as_ref()); let mut device_types = Vec::new(); - #[cfg(all(feature = "vulkan", not(target_arch = "wasm32")))] + #[cfg(vulkan)] let (id_vulkan, adapters_vk) = gather( hal::api::Vulkan, self.instance.vulkan.as_ref(), @@ -840,7 +840,7 @@ impl Global { desc.force_fallback_adapter, &mut device_types, ); - #[cfg(all(feature = "metal", any(target_os = "macos", target_os = "ios")))] + #[cfg(metal)] let (id_metal, adapters_metal) = gather( hal::api::Metal, self.instance.metal.as_ref(), @@ -849,7 +849,7 @@ impl Global { desc.force_fallback_adapter, &mut device_types, ); - #[cfg(all(feature = "dx12", windows))] + #[cfg(dx12)] let (id_dx12, adapters_dx12) = gather( hal::api::Dx12, self.instance.dx12.as_ref(), @@ -858,7 +858,7 @@ impl Global { desc.force_fallback_adapter, &mut device_types, ); - #[cfg(feature = "gles")] + #[cfg(gles)] let (id_gl, adapters_gl) = gather( hal::api::Gles, self.instance.gl.as_ref(), @@ -919,19 +919,19 @@ impl Global { }; let mut selected = preferred_gpu.unwrap_or(0); - #[cfg(all(feature = "vulkan", not(target_arch = "wasm32")))] + #[cfg(vulkan)] if let Some(id) = self.select(&mut selected, id_vulkan, adapters_vk) { return Ok(id); } - #[cfg(all(feature = "metal", any(target_os = "macos", target_os = "ios")))] + #[cfg(metal)] if let Some(id) = self.select(&mut selected, id_metal, adapters_metal) { return Ok(id); } - #[cfg(all(feature = "dx12", windows))] + #[cfg(dx12)] if let Some(id) = self.select(&mut selected, id_dx12, adapters_dx12) { return Ok(id); } - #[cfg(feature = "gles")] + #[cfg(gles)] if let Some(id) = self.select(&mut selected, id_gl, adapters_gl) { return Ok(id); } @@ -955,13 +955,13 @@ impl Global { let (id, _adapter): (crate::id::Id>, Arc>) = match A::VARIANT { - #[cfg(all(feature = "vulkan", not(target_arch = "wasm32")))] + #[cfg(vulkan)] Backend::Vulkan => fid.assign(Adapter::new(hal_adapter)), - #[cfg(all(feature = "metal", any(target_os = "macos", target_os = "ios")))] + #[cfg(metal)] Backend::Metal => fid.assign(Adapter::new(hal_adapter)), - #[cfg(all(feature = "dx12", windows))] + #[cfg(dx12)] Backend::Dx12 => fid.assign(Adapter::new(hal_adapter)), - #[cfg(feature = "gles")] + #[cfg(gles)] Backend::Gl => fid.assign(Adapter::new(hal_adapter)), _ => unreachable!(), }; diff --git a/wgpu-core/src/resource.rs b/wgpu-core/src/resource.rs index d88e6947b3..0d337d12c0 100644 --- a/wgpu-core/src/resource.rs +++ b/wgpu-core/src/resource.rs @@ -203,21 +203,9 @@ pub(crate) enum BufferMapState { Idle, } -#[cfg(any( - not(target_arch = "wasm32"), - all( - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") - ) -))] +#[cfg(send_sync)] unsafe impl Send for BufferMapState {} -#[cfg(any( - not(target_arch = "wasm32"), - all( - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") - ) -))] +#[cfg(send_sync)] unsafe impl Sync for BufferMapState {} #[repr(C)] @@ -226,13 +214,7 @@ pub struct BufferMapCallbackC { pub user_data: *mut u8, } -#[cfg(any( - not(target_arch = "wasm32"), - all( - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") - ) -))] +#[cfg(send_sync)] unsafe impl Send for BufferMapCallbackC {} #[derive(Debug)] @@ -242,21 +224,9 @@ pub struct BufferMapCallback { inner: BufferMapCallbackInner, } -#[cfg(any( - not(target_arch = "wasm32"), - all( - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") - ) -))] +#[cfg(send_sync)] type BufferMapCallbackCallback = Box; -#[cfg(not(any( - not(target_arch = "wasm32"), - all( - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") - ) -)))] +#[cfg(not(send_sync))] type BufferMapCallbackCallback = Box; enum BufferMapCallbackInner { diff --git a/wgpu-hal/Cargo.toml b/wgpu-hal/Cargo.toml index fec81b456a..36796b33d8 100644 --- a/wgpu-hal/Cargo.toml +++ b/wgpu-hal/Cargo.toml @@ -160,6 +160,9 @@ path = "../naga" version = "0.14.0" features = ["clone"] +[build-dependencies] +cfg_aliases.workspace = true + # DEV dependencies [dev-dependencies.naga] path = "../naga" diff --git a/wgpu-hal/build.rs b/wgpu-hal/build.rs new file mode 100644 index 0000000000..7d17591605 --- /dev/null +++ b/wgpu-hal/build.rs @@ -0,0 +1,15 @@ +fn main() { + cfg_aliases::cfg_aliases! { + native: { not(target_arch = "wasm32") }, + send_sync: { any( + not(target_arch = "wasm32"), + all(feature = "fragile-send-sync-non-atomic-wasm", not(target_feature = "atomics")) + ) }, + webgl: { all(target_arch = "wasm32", not(target_os = "emscripten"), gles) }, + Emscripten: { all(target_os = "emscripten", gles) }, + dx12: { all(target_os = "windows", feature = "dx12") }, + gles: { all(feature = "gles") }, + metal: { all(any(target_os = "ios", target_os = "macos"), feature = "metal") }, + vulkan: { all(not(target_arch = "wasm32"), feature = "vulkan") } + } +} diff --git a/wgpu-hal/src/auxil/dxgi/factory.rs b/wgpu-hal/src/auxil/dxgi/factory.rs index 2d09f44a75..269329304f 100644 --- a/wgpu-hal/src/auxil/dxgi/factory.rs +++ b/wgpu-hal/src/auxil/dxgi/factory.rs @@ -7,8 +7,6 @@ use super::result::HResult as _; #[derive(Debug, PartialEq, Eq, PartialOrd, Ord)] pub enum DxgiFactoryType { - #[cfg(feature = "dx11")] - Factory1, Factory2, Factory4, Factory6, diff --git a/wgpu-hal/src/auxil/mod.rs b/wgpu-hal/src/auxil/mod.rs index 5e45c1f2b6..f70a8bbe03 100644 --- a/wgpu-hal/src/auxil/mod.rs +++ b/wgpu-hal/src/auxil/mod.rs @@ -1,7 +1,7 @@ -#[cfg(all(feature = "dx12", windows))] +#[cfg(dx12)] pub(super) mod dxgi; -#[cfg(all(not(target_arch = "wasm32"), feature = "renderdoc"))] +#[cfg(all(native, feature = "renderdoc"))] pub(super) mod renderdoc; pub mod db { diff --git a/wgpu-hal/src/empty.rs b/wgpu-hal/src/empty.rs index 9fd42bd6f5..12f86e6f31 100644 --- a/wgpu-hal/src/empty.rs +++ b/wgpu-hal/src/empty.rs @@ -284,7 +284,7 @@ impl crate::CommandEncoder for Encoder { unsafe fn copy_buffer_to_buffer(&mut self, src: &Resource, dst: &Resource, regions: T) {} - #[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))] + #[cfg(webgl)] unsafe fn copy_external_image_to_texture( &mut self, src: &wgt::ImageCopyExternalImage, diff --git a/wgpu-hal/src/gles/adapter.rs b/wgpu-hal/src/gles/adapter.rs index 6d9b461530..5f92d2c4ab 100644 --- a/wgpu-hal/src/gles/adapter.rs +++ b/wgpu-hal/src/gles/adapter.rs @@ -198,14 +198,14 @@ impl super::Adapter { let (vendor_const, renderer_const) = if extensions.contains("WEBGL_debug_renderer_info") { // emscripten doesn't enable "WEBGL_debug_renderer_info" extension by default. so, we do it manually. // See https://github.com/gfx-rs/wgpu/issues/3245 for context - #[cfg(target_os = "emscripten")] + #[cfg(Emscripten)] if unsafe { super::emscripten::enable_extension("WEBGL_debug_renderer_info\0") } { (GL_UNMASKED_VENDOR_WEBGL, GL_UNMASKED_RENDERER_WEBGL) } else { (glow::VENDOR, glow::RENDERER) } // glow already enables WEBGL_debug_renderer_info on wasm32-unknown-unknown target by default. - #[cfg(not(target_os = "emscripten"))] + #[cfg(not(Emscripten))] (GL_UNMASKED_VENDOR_WEBGL, GL_UNMASKED_RENDERER_WEBGL) } else { (glow::VENDOR, glow::RENDERER) @@ -282,7 +282,7 @@ impl super::Adapter { let value = sl_major as u16 * 100 + sl_minor as u16 * 10; naga::back::glsl::Version::Embedded { version: value, - is_webgl: cfg!(target_arch = "wasm32"), + is_webgl: cfg!(any(webgl, Emscripten)), } } }; @@ -407,16 +407,16 @@ impl super::Adapter { } downlevel_flags.set( wgt::DownlevelFlags::BUFFER_BINDINGS_NOT_16_BYTE_ALIGNED, - !(cfg!(target_arch = "wasm32") || is_angle), + !(cfg!(any(webgl, Emscripten)) || is_angle), ); // see https://registry.khronos.org/webgl/specs/latest/2.0/#BUFFER_OBJECT_BINDING downlevel_flags.set( wgt::DownlevelFlags::UNRESTRICTED_INDEX_BUFFER, - !cfg!(target_arch = "wasm32"), + !cfg!(any(webgl, Emscripten)), ); downlevel_flags.set( wgt::DownlevelFlags::UNRESTRICTED_EXTERNAL_TEXTURE_COPIES, - !cfg!(target_arch = "wasm32"), + !cfg!(any(webgl, Emscripten)), ); downlevel_flags.set( wgt::DownlevelFlags::FULL_DRAW_INDEX_UINT32, @@ -490,7 +490,7 @@ impl super::Adapter { "EXT_texture_compression_rgtc", "EXT_texture_compression_bptc", ]; - let bcn_exts = if cfg!(target_arch = "wasm32") { + let bcn_exts = if cfg!(any(webgl, Emscripten)) { &webgl_bcn_exts[..] } else if es_ver.is_some() { &gles_bcn_exts[..] @@ -501,7 +501,7 @@ impl super::Adapter { wgt::Features::TEXTURE_COMPRESSION_BC, bcn_exts.iter().all(|&ext| extensions.contains(ext)), ); - let has_etc = if cfg!(target_arch = "wasm32") { + let has_etc = if cfg!(any(webgl, Emscripten)) { extensions.contains("WEBGL_compressed_texture_etc") } else { // This is a required part of GLES3, but not part of Desktop GL at all. @@ -513,7 +513,7 @@ impl super::Adapter { if extensions.contains("WEBGL_compressed_texture_astc") || extensions.contains("GL_OES_texture_compression_astc") { - #[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))] + #[cfg(webgl)] { if context .glow_context @@ -529,7 +529,7 @@ impl super::Adapter { } } - #[cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))] + #[cfg(any(native, Emscripten))] { features.insert(wgt::Features::TEXTURE_COMPRESSION_ASTC); features.insert(wgt::Features::TEXTURE_COMPRESSION_ASTC_HDR); @@ -582,11 +582,11 @@ impl super::Adapter { ); private_caps.set( super::PrivateCapabilities::INDEX_BUFFER_ROLE_CHANGE, - !cfg!(target_arch = "wasm32"), + !cfg!(any(webgl, Emscripten)), ); private_caps.set( super::PrivateCapabilities::GET_BUFFER_SUB_DATA, - cfg!(target_arch = "wasm32") || full_ver.is_some(), + cfg!(any(webgl, Emscripten)) || full_ver.is_some(), ); let color_buffer_float = extensions.contains("GL_EXT_color_buffer_float") || extensions.contains("GL_ARB_color_buffer_float") @@ -759,7 +759,7 @@ impl super::Adapter { workarounds.set( super::Workarounds::EMULATE_BUFFER_MAP, - cfg!(target_arch = "wasm32"), + cfg!(any(webgl, Emscripten)), ); let r = renderer.to_lowercase(); @@ -917,7 +917,7 @@ impl crate::Adapter for super::Adapter { device: super::Device { shared: Arc::clone(&self.shared), main_vao, - #[cfg(all(not(target_arch = "wasm32"), feature = "renderdoc"))] + #[cfg(all(native, feature = "renderdoc"))] render_doc: Default::default(), }, queue: super::Queue { @@ -1112,13 +1112,13 @@ impl crate::Adapter for super::Adapter { if surface.presentable { let mut formats = vec![ wgt::TextureFormat::Rgba8Unorm, - #[cfg(not(target_arch = "wasm32"))] + #[cfg(native)] wgt::TextureFormat::Bgra8Unorm, ]; if surface.supports_srgb() { formats.extend([ wgt::TextureFormat::Rgba8UnormSrgb, - #[cfg(not(target_arch = "wasm32"))] + #[cfg(native)] wgt::TextureFormat::Bgra8UnormSrgb, ]) } @@ -1178,17 +1178,9 @@ impl super::AdapterShared { } } -#[cfg(all( - target_arch = "wasm32", - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") -))] +#[cfg(send_sync)] unsafe impl Sync for super::Adapter {} -#[cfg(all( - target_arch = "wasm32", - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") -))] +#[cfg(send_sync)] unsafe impl Send for super::Adapter {} #[cfg(test)] diff --git a/wgpu-hal/src/gles/command.rs b/wgpu-hal/src/gles/command.rs index fee08f201b..08083894c3 100644 --- a/wgpu-hal/src/gles/command.rs +++ b/wgpu-hal/src/gles/command.rs @@ -349,7 +349,7 @@ impl crate::CommandEncoder for super::CommandEncoder { } } - #[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))] + #[cfg(webgl)] unsafe fn copy_external_image_to_texture( &mut self, src: &wgt::ImageCopyExternalImage, @@ -507,7 +507,7 @@ impl crate::CommandEncoder for super::CommandEncoder { .iter() .filter_map(|at| at.as_ref()) .any(|at| match at.target.view.inner { - #[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))] + #[cfg(webgl)] super::TextureInner::ExternalFramebuffer { .. } => true, _ => false, }); diff --git a/wgpu-hal/src/gles/device.rs b/wgpu-hal/src/gles/device.rs index 8cc2a95756..d0abe2c169 100644 --- a/wgpu-hal/src/gles/device.rs +++ b/wgpu-hal/src/gles/device.rs @@ -9,7 +9,7 @@ use std::{ }; use arrayvec::ArrayVec; -#[cfg(not(target_arch = "wasm32"))] +#[cfg(native)] use std::mem; use std::sync::atomic::Ordering; @@ -115,7 +115,7 @@ impl super::Device { /// - If `drop_guard` is [`None`], wgpu-hal will take ownership of the texture. If `drop_guard` is /// [`Some`], the texture must be valid until the drop implementation /// of the drop guard is called. - #[cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))] + #[cfg(any(native, Emscripten))] pub unsafe fn texture_from_raw( &self, name: std::num::NonZeroU32, @@ -143,7 +143,7 @@ impl super::Device { /// - If `drop_guard` is [`None`], wgpu-hal will take ownership of the renderbuffer. If `drop_guard` is /// [`Some`], the renderbuffer must be valid until the drop implementation /// of the drop guard is called. - #[cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))] + #[cfg(any(native, Emscripten))] pub unsafe fn texture_from_raw_renderbuffer( &self, name: std::num::NonZeroU32, @@ -176,7 +176,7 @@ impl super::Device { }; let raw = unsafe { gl.create_shader(target) }.unwrap(); - #[cfg(not(target_arch = "wasm32"))] + #[cfg(native)] if gl.supports_debug() { //TODO: remove all transmutes from `object_label` // https://github.com/grovesNL/glow/issues/186 @@ -342,7 +342,7 @@ impl super::Device { naga::back::glsl::Version::Desktop(version) => format!("{version}"), }; let program = unsafe { gl.create_program() }.unwrap(); - #[cfg(not(target_arch = "wasm32"))] + #[cfg(native)] if let Some(label) = label { if private_caps.contains(PrivateCapabilities::DEBUG_FNS) { let name = unsafe { mem::transmute(program) }; @@ -591,7 +591,7 @@ impl crate::Device for super::Device { } //TODO: do we need `glow::MAP_UNSYNCHRONIZED_BIT`? - #[cfg(not(target_arch = "wasm32"))] + #[cfg(native)] if let Some(label) = desc.label { if self .shared @@ -734,7 +734,7 @@ impl crate::Device for super::Device { }; } - #[cfg(not(target_arch = "wasm32"))] + #[cfg(native)] if let Some(label) = desc.label { if self .shared @@ -902,7 +902,7 @@ impl crate::Device for super::Device { }; } - #[cfg(not(target_arch = "wasm32"))] + #[cfg(native)] if let Some(label) = desc.label { if self .shared @@ -939,7 +939,7 @@ impl crate::Device for super::Device { super::TextureInner::Texture { raw, .. } => { unsafe { gl.delete_texture(raw) }; } - #[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))] + #[cfg(webgl)] super::TextureInner::ExternalFramebuffer { .. } => {} } } @@ -1045,7 +1045,7 @@ impl crate::Device for super::Device { }; } - #[cfg(not(target_arch = "wasm32"))] + #[cfg(native)] if let Some(label) = desc.label { if self .shared @@ -1432,7 +1432,7 @@ impl crate::Device for super::Device { ) -> Result { if fence.last_completed < wait_value { let gl = &self.shared.context.lock(); - let timeout_ns = if cfg!(target_arch = "wasm32") { + let timeout_ns = if cfg!(any(webgl, Emscripten)) { 0 } else { (timeout_ms as u64 * 1_000_000).min(!0u32 as u64) @@ -1446,7 +1446,7 @@ impl crate::Device for super::Device { gl.client_wait_sync(sync, glow::SYNC_FLUSH_COMMANDS_BIT, timeout_ns as i32) } { // for some reason firefox returns WAIT_FAILED, to investigate - #[cfg(target_arch = "wasm32")] + #[cfg(any(webgl, Emscripten))] glow::WAIT_FAILED => { log::warn!("wait failed!"); Ok(false) @@ -1461,7 +1461,7 @@ impl crate::Device for super::Device { } unsafe fn start_capture(&self) -> bool { - #[cfg(all(not(target_arch = "wasm32"), feature = "renderdoc"))] + #[cfg(all(native, feature = "renderdoc"))] return unsafe { self.render_doc .start_frame_capture(self.shared.context.raw_context(), ptr::null_mut()) @@ -1470,7 +1470,7 @@ impl crate::Device for super::Device { false } unsafe fn stop_capture(&self) { - #[cfg(all(not(target_arch = "wasm32"), feature = "renderdoc"))] + #[cfg(all(native, feature = "renderdoc"))] unsafe { self.render_doc .end_frame_capture(ptr::null_mut(), ptr::null_mut()) @@ -1497,15 +1497,7 @@ impl crate::Device for super::Device { unsafe fn destroy_acceleration_structure(&self, _acceleration_structure: ()) {} } -#[cfg(all( - target_arch = "wasm32", - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") -))] +#[cfg(send_sync)] unsafe impl Sync for super::Device {} -#[cfg(all( - target_arch = "wasm32", - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") -))] +#[cfg(send_sync)] unsafe impl Send for super::Device {} diff --git a/wgpu-hal/src/gles/egl.rs b/wgpu-hal/src/gles/egl.rs index bda33f35c0..ef0ff4b6f3 100644 --- a/wgpu-hal/src/gles/egl.rs +++ b/wgpu-hal/src/gles/egl.rs @@ -28,10 +28,10 @@ type WlDisplayConnectFun = type WlDisplayDisconnectFun = unsafe extern "system" fn(display: *const raw::c_void); -#[cfg(not(target_os = "emscripten"))] +#[cfg(not(Emscripten))] type EglInstance = khronos_egl::DynamicInstance; -#[cfg(target_os = "emscripten")] +#[cfg(Emscripten)] type EglInstance = khronos_egl::Instance; type WlEglWindowCreateFun = unsafe extern "system" fn( @@ -434,9 +434,9 @@ struct Inner { version: (i32, i32), supports_native_window: bool, config: khronos_egl::Config, - #[cfg_attr(target_os = "emscripten", allow(dead_code))] + #[cfg_attr(Emscripten, allow(dead_code))] wl_display: Option<*mut raw::c_void>, - #[cfg_attr(target_os = "emscripten", allow(dead_code))] + #[cfg_attr(Emscripten, allow(dead_code))] force_gles_minor_version: wgt::Gles3MinorVersion, /// Method by which the framebuffer should support srgb srgb_kind: SrgbFrameBufferKind, @@ -569,7 +569,7 @@ impl Inner { // and creating dummy pbuffer surface if not. let pbuffer = if version >= (1, 5) || display_extensions.contains("EGL_KHR_surfaceless_context") - || cfg!(target_os = "emscripten") + || cfg!(Emscripten) { log::debug!("\tEGL context: +surfaceless"); None @@ -675,11 +675,11 @@ unsafe impl Sync for Instance {} impl crate::Instance for Instance { unsafe fn init(desc: &crate::InstanceDescriptor) -> Result { profiling::scope!("Init OpenGL (EGL) Backend"); - #[cfg(target_os = "emscripten")] + #[cfg(Emscripten)] let egl_result: Result = Ok(khronos_egl::Instance::new(khronos_egl::Static)); - #[cfg(not(target_os = "emscripten"))] + #[cfg(not(Emscripten))] let egl_result = if cfg!(windows) { unsafe { khronos_egl::DynamicInstance::::load_required_from_filename( @@ -732,10 +732,10 @@ impl crate::Instance for Instance { None }; - #[cfg(not(target_os = "emscripten"))] + #[cfg(not(Emscripten))] let egl1_5 = egl.upcast::(); - #[cfg(target_os = "emscripten")] + #[cfg(Emscripten)] let egl1_5: Option<&Arc> = Some(&egl); let (display, display_owner, wsi_kind) = @@ -842,10 +842,7 @@ impl crate::Instance for Instance { ) -> Result { use raw_window_handle::RawWindowHandle as Rwh; - #[cfg_attr( - any(target_os = "android", target_os = "emscripten"), - allow(unused_mut) - )] + #[cfg_attr(any(target_os = "android", Emscripten), allow(unused_mut))] let mut inner = self.inner.lock(); match (window_handle, display_handle) { @@ -875,7 +872,7 @@ impl crate::Instance for Instance { ))); } } - #[cfg(not(target_os = "emscripten"))] + #[cfg(not(Emscripten))] (Rwh::Wayland(_), raw_window_handle::RawDisplayHandle::Wayland(display_handle)) => { if inner .wl_display @@ -920,7 +917,7 @@ impl crate::Instance for Instance { drop(old_inner); } } - #[cfg(target_os = "emscripten")] + #[cfg(Emscripten)] (Rwh::Web(_), _) => {} other => { return Err(crate::InstanceError::new(format!( @@ -1172,7 +1169,7 @@ impl crate::Surface for Surface { wl_window = Some(window); window } - #[cfg(target_os = "emscripten")] + #[cfg(Emscripten)] (WindowKind::Unknown, Rwh::Web(handle)) => handle.id as *mut std::ffi::c_void, (WindowKind::Unknown, Rwh::Win32(handle)) => { handle.hwnd.get() as *mut std::ffi::c_void @@ -1229,10 +1226,10 @@ impl crate::Surface for Surface { } attributes.push(khronos_egl::ATTRIB_NONE as i32); - #[cfg(not(target_os = "emscripten"))] + #[cfg(not(Emscripten))] let egl1_5 = self.egl.instance.upcast::(); - #[cfg(target_os = "emscripten")] + #[cfg(Emscripten)] let egl1_5: Option<&Arc> = Some(&self.egl.instance); // Careful, we can still be in 1.4 version even if `upcast` succeeds diff --git a/wgpu-hal/src/gles/mod.rs b/wgpu-hal/src/gles/mod.rs index 7021c3e12d..701b13fb57 100644 --- a/wgpu-hal/src/gles/mod.rs +++ b/wgpu-hal/src/gles/mod.rs @@ -82,11 +82,11 @@ we don't bother with that combination. */ ///cbindgen:ignore -#[cfg(not(any(windows, all(target_arch = "wasm32", not(target_os = "emscripten")))))] +#[cfg(not(any(windows, webgl)))] mod egl; -#[cfg(target_os = "emscripten")] +#[cfg(Emscripten)] mod emscripten; -#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))] +#[cfg(webgl)] mod web; #[cfg(windows)] mod wgl; @@ -99,14 +99,14 @@ mod queue; use crate::{CopyExtent, TextureDescriptor}; -#[cfg(not(any(windows, all(target_arch = "wasm32", not(target_os = "emscripten")))))] +#[cfg(not(any(windows, webgl)))] pub use self::egl::{AdapterContext, AdapterContextLock}; -#[cfg(not(any(windows, all(target_arch = "wasm32", not(target_os = "emscripten")))))] +#[cfg(not(any(windows, webgl)))] use self::egl::{Instance, Surface}; -#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))] +#[cfg(webgl)] pub use self::web::AdapterContext; -#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))] +#[cfg(webgl)] use self::web::{Instance, Surface}; #[cfg(windows)] @@ -260,7 +260,7 @@ pub struct Adapter { pub struct Device { shared: Arc, main_vao: glow::VertexArray, - #[cfg(all(not(target_arch = "wasm32"), feature = "renderdoc"))] + #[cfg(all(native, feature = "renderdoc"))] render_doc: crate::auxil::renderdoc::RenderDoc, } @@ -291,17 +291,9 @@ pub struct Buffer { data: Option>>>, } -#[cfg(all( - target_arch = "wasm32", - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") -))] +#[cfg(send_sync)] unsafe impl Sync for Buffer {} -#[cfg(all( - target_arch = "wasm32", - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") -))] +#[cfg(send_sync)] unsafe impl Send for Buffer {} #[derive(Clone, Debug)] @@ -314,23 +306,15 @@ pub enum TextureInner { raw: glow::Texture, target: BindTarget, }, - #[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))] + #[cfg(webgl)] ExternalFramebuffer { inner: web_sys::WebGlFramebuffer, }, } -#[cfg(all( - target_arch = "wasm32", - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") -))] +#[cfg(send_sync)] unsafe impl Sync for TextureInner {} -#[cfg(all( - target_arch = "wasm32", - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") -))] +#[cfg(send_sync)] unsafe impl Send for TextureInner {} impl TextureInner { @@ -340,7 +324,7 @@ impl TextureInner { panic!("Unexpected renderbuffer"); } Self::Texture { raw, target } => (raw, target), - #[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))] + #[cfg(webgl)] Self::ExternalFramebuffer { .. } => panic!("Unexpected external framebuffer"), } } @@ -524,17 +508,9 @@ struct PushConstantDesc { size_bytes: u32, } -#[cfg(all( - target_arch = "wasm32", - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") -))] +#[cfg(send_sync)] unsafe impl Sync for PushConstantDesc {} -#[cfg(all( - target_arch = "wasm32", - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") -))] +#[cfg(send_sync)] unsafe impl Send for PushConstantDesc {} /// For each texture in the pipeline layout, store the index of the only @@ -602,17 +578,9 @@ pub struct RenderPipeline { alpha_to_coverage_enabled: bool, } -#[cfg(all( - target_arch = "wasm32", - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") -))] +#[cfg(send_sync)] unsafe impl Sync for RenderPipeline {} -#[cfg(all( - target_arch = "wasm32", - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") -))] +#[cfg(send_sync)] unsafe impl Send for RenderPipeline {} #[derive(Debug)] @@ -620,17 +588,9 @@ pub struct ComputePipeline { inner: Arc, } -#[cfg(all( - target_arch = "wasm32", - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") -))] +#[cfg(send_sync)] unsafe impl Sync for ComputePipeline {} -#[cfg(all( - target_arch = "wasm32", - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") -))] +#[cfg(send_sync)] unsafe impl Send for ComputePipeline {} #[derive(Debug)] @@ -792,7 +752,7 @@ enum Command { dst_target: BindTarget, copy: crate::BufferCopy, }, - #[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))] + #[cfg(webgl)] CopyExternalImageToTexture { src: wgt::ImageCopyExternalImage, dst: glow::Texture, @@ -951,17 +911,9 @@ impl fmt::Debug for CommandBuffer { } } -#[cfg(all( - target_arch = "wasm32", - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") -))] +#[cfg(send_sync)] unsafe impl Sync for CommandBuffer {} -#[cfg(all( - target_arch = "wasm32", - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") -))] +#[cfg(send_sync)] unsafe impl Send for CommandBuffer {} //TODO: we would have something like `Arc` @@ -982,20 +934,12 @@ impl fmt::Debug for CommandEncoder { } } -#[cfg(all( - target_arch = "wasm32", - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") -))] +#[cfg(send_sync)] unsafe impl Sync for CommandEncoder {} -#[cfg(all( - target_arch = "wasm32", - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") -))] +#[cfg(send_sync)] unsafe impl Send for CommandEncoder {} -#[cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))] +#[cfg(not(webgl))] fn gl_debug_message_callback(source: u32, gltype: u32, id: u32, severity: u32, message: &str) { let source_str = match source { glow::DEBUG_SOURCE_API => "API", diff --git a/wgpu-hal/src/gles/queue.rs b/wgpu-hal/src/gles/queue.rs index 4ee6fb8e47..5a4deb8e1a 100644 --- a/wgpu-hal/src/gles/queue.rs +++ b/wgpu-hal/src/gles/queue.rs @@ -109,7 +109,7 @@ impl super::Queue { super::TextureInner::Texture { raw, target } => { let num_layers = view.array_layers.end - view.array_layers.start; if num_layers > 1 { - #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] + #[cfg(webgl)] unsafe { gl.framebuffer_texture_multiview_ovr( fbo_target, @@ -143,7 +143,7 @@ impl super::Queue { }; } } - #[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))] + #[cfg(webgl)] super::TextureInner::ExternalFramebuffer { ref inner } => unsafe { gl.bind_external_framebuffer(glow::FRAMEBUFFER, inner); }, @@ -432,7 +432,7 @@ impl super::Queue { unsafe { gl.bind_buffer(copy_dst_target, None) }; } } - #[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))] + #[cfg(webgl)] C::CopyExternalImageToTexture { ref src, dst, @@ -1805,15 +1805,7 @@ impl crate::Queue for super::Queue { } } -#[cfg(all( - target_arch = "wasm32", - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") -))] +#[cfg(send_sync)] unsafe impl Sync for super::Queue {} -#[cfg(all( - target_arch = "wasm32", - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") -))] +#[cfg(send_sync)] unsafe impl Send for super::Queue {} diff --git a/wgpu-hal/src/gles/web.rs b/wgpu-hal/src/gles/web.rs index cdbe4543d1..797d6f91d7 100644 --- a/wgpu-hal/src/gles/web.rs +++ b/wgpu-hal/src/gles/web.rs @@ -111,15 +111,9 @@ impl Instance { } } -#[cfg(all( - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") -))] +#[cfg(send_sync)] unsafe impl Sync for Instance {} -#[cfg(all( - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") -))] +#[cfg(send_sync)] unsafe impl Send for Instance {} impl crate::Instance for Instance { @@ -210,15 +204,9 @@ impl Clone for Surface { } } -#[cfg(all( - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") -))] +#[cfg(send_sync)] unsafe impl Sync for Surface {} -#[cfg(all( - feature = "fragile-send-sync-non-atomic-wasm", - not(target_feature = "atomics") -))] +#[cfg(send_sync)] unsafe impl Send for Surface {} #[derive(Clone, Debug)] diff --git a/wgpu-hal/src/lib.rs b/wgpu-hal/src/lib.rs index 39037e895c..7bd6eb77bb 100644 --- a/wgpu-hal/src/lib.rs +++ b/wgpu-hal/src/lib.rs @@ -52,30 +52,30 @@ )] /// DirectX12 API internals. -#[cfg(all(feature = "dx12", windows))] +#[cfg(dx12)] pub mod dx12; /// A dummy API implementation. pub mod empty; /// GLES API internals. -#[cfg(feature = "gles")] +#[cfg(gles)] pub mod gles; /// Metal API internals. -#[cfg(all(feature = "metal", any(target_os = "macos", target_os = "ios")))] +#[cfg(metal)] pub mod metal; /// Vulkan API internals. -#[cfg(all(feature = "vulkan", not(target_arch = "wasm32")))] +#[cfg(vulkan)] pub mod vulkan; pub mod auxil; pub mod api { - #[cfg(all(feature = "dx12", windows))] + #[cfg(dx12)] pub use super::dx12::Api as Dx12; pub use super::empty::Api as Empty; - #[cfg(feature = "gles")] + #[cfg(gles)] pub use super::gles::Api as Gles; - #[cfg(all(feature = "metal", any(target_os = "macos", target_os = "ios")))] + #[cfg(metal)] pub use super::metal::Api as Metal; - #[cfg(all(feature = "vulkan", not(target_arch = "wasm32")))] + #[cfg(vulkan)] pub use super::vulkan::Api as Vulkan; } @@ -463,7 +463,7 @@ pub trait CommandEncoder: WasmNotSendSync + fmt::Debug { /// Works with a single array layer. /// Note: `dst` current usage has to be `TextureUses::COPY_DST`. /// Note: the copy extent is in physical size (rounded to the block size) - #[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))] + #[cfg(webgl)] unsafe fn copy_external_image_to_texture( &mut self, src: &wgt::ImageCopyExternalImage, diff --git a/wgpu-hal/src/vulkan/instance.rs b/wgpu-hal/src/vulkan/instance.rs index e9ca3221ea..179842c1e5 100644 --- a/wgpu-hal/src/vulkan/instance.rs +++ b/wgpu-hal/src/vulkan/instance.rs @@ -508,7 +508,7 @@ impl super::Instance { Ok(self.create_surface_from_vk_surface_khr(surface)) } - #[cfg(all(any(target_os = "macos", target_os = "ios"), feature = "metal"))] + #[cfg(metal)] fn create_surface_from_view( &self, view: *mut c_void, diff --git a/wgpu/build.rs b/wgpu/build.rs index b419deee24..97e17e687c 100644 --- a/wgpu/build.rs +++ b/wgpu/build.rs @@ -3,7 +3,7 @@ fn main() { native: { not(target_arch = "wasm32") }, webgl: { all(target_arch = "wasm32", not(target_os = "emscripten"), feature = "webgl") }, webgpu: { all(target_arch = "wasm32", not(target_os = "emscripten"), not(feature = "webgl")) }, - emscripten: { all(target_arch = "wasm32", target_os = "emscripten") }, + Emscripten: { all(target_arch = "wasm32", target_os = "emscripten") }, send_sync: { any( not(target_arch = "wasm32"), all(feature = "fragile-send-sync-non-atomic-wasm", not(target_feature = "atomics")) diff --git a/wgpu/src/backend/direct.rs b/wgpu/src/backend/direct.rs index 05a0db4263..e172b5128a 100644 --- a/wgpu/src/backend/direct.rs +++ b/wgpu/src/backend/direct.rs @@ -1,5 +1,3 @@ -#![allow(clippy::mismatched_target_os)] - use crate::{ context::{ObjectId, Unused}, AdapterInfo, BindGroupDescriptor, BindGroupLayoutDescriptor, BindingResource, BufferBinding, @@ -1398,9 +1396,9 @@ impl crate::Context for Context { Err(e) => panic!("Error in Device::create_render_bundle_encoder: {e}"), } } - #[cfg_attr(not(any(native, emscripten)), allow(unused))] + #[cfg_attr(not(any(native, Emscripten)), allow(unused))] fn device_drop(&self, device: &Self::DeviceId, _device_data: &Self::DeviceData) { - #[cfg(any(native, emscripten))] + #[cfg(any(native, Emscripten))] { let global = &self.0; match wgc::gfx_select!(device => global.device_poll(*device, wgt::Maintain::Wait)) {