Skip to content

Commit

Permalink
Add cfg_aliases to wgpu-core and wgpu-hal (gfx-rs#5055)
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda authored Jan 14, 2024
1 parent 552f06d commit 580340f
Show file tree
Hide file tree
Showing 31 changed files with 237 additions and 433 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions wgpu-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,6 @@ web-sys = { version = "0.3.66", features = [
"HtmlCanvasElement",
"OffscreenCanvas",
] }

[build-dependencies]
cfg_aliases.workspace = true
13 changes: 13 additions & 0 deletions wgpu-core/build.rs
Original file line number Diff line number Diff line change
@@ -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") }
}
}
24 changes: 6 additions & 18 deletions wgpu-core/src/any_surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<hal::api::Vulkan>().is_some() {
return Backend::Vulkan;
}
#[cfg(all(feature = "metal", any(target_os = "macos", target_os = "ios")))]
#[cfg(metal)]
if self.downcast_ref::<hal::api::Metal>().is_some() {
return Backend::Metal;
}
#[cfg(all(feature = "dx12", windows))]
#[cfg(dx12)]
if self.downcast_ref::<hal::api::Dx12>().is_some() {
return Backend::Dx12;
}
#[cfg(feature = "gles")]
#[cfg(gles)]
if self.downcast_ref::<hal::api::Gles>().is_some() {
return Backend::Gl;
}
Expand Down Expand Up @@ -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 {}
16 changes: 2 additions & 14 deletions wgpu-core/src/command/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -777,21 +777,9 @@ impl<A: HalApi> Drop for RenderBundle<A> {
}
}

#[cfg(any(
not(target_arch = "wasm32"),
all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
)
))]
#[cfg(send_sync)]
unsafe impl<A: HalApi> Send for RenderBundle<A> {}
#[cfg(any(
not(target_arch = "wasm32"),
all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
)
))]
#[cfg(send_sync)]
unsafe impl<A: HalApi> Sync for RenderBundle<A> {}

impl<A: HalApi> RenderBundle<A> {
Expand Down
16 changes: 2 additions & 14 deletions wgpu-core/src/device/any_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
8 changes: 4 additions & 4 deletions wgpu-core/src/device/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2164,22 +2164,22 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
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::<hal::api::Vulkan>(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::<hal::api::Metal>(force_wait, &mut closures)? && all_queue_empty;
}
#[cfg(all(feature = "dx12", windows))]
#[cfg(dx12)]
{
all_queue_empty =
self.poll_device::<hal::api::Dx12>(force_wait, &mut closures)? && all_queue_empty;
}
#[cfg(feature = "gles")]
#[cfg(gles)]
{
all_queue_empty =
self.poll_device::<hal::api::Gles>(force_wait, &mut closures)? && all_queue_empty;
Expand Down
24 changes: 3 additions & 21 deletions wgpu-core/src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn Fn(DeviceLostReason, String) + Send + 'static>;
#[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<dyn Fn(DeviceLostReason, String) + 'static>;

pub struct DeviceLostClosureRust {
Expand All @@ -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 {
Expand Down
26 changes: 4 additions & 22 deletions wgpu-core/src/device/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<dyn FnOnce() + Send + 'static>;
#[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<dyn FnOnce() + 'static>;

enum SubmittedWorkDoneClosureInner {
Expand Down Expand Up @@ -911,7 +893,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
Ok(())
}

#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
#[cfg(webgl)]
pub fn queue_copy_external_image_to_texture<A: HalApi>(
&self,
queue_id: QueueId,
Expand Down
16 changes: 2 additions & 14 deletions wgpu-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn Error + Send + Sync + 'static>,
#[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<dyn Error + 'static>,
pub label_key: &'static str,
pub label: String,
Expand Down
43 changes: 17 additions & 26 deletions wgpu-core/src/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<HubReport>,
#[cfg(all(feature = "metal", any(target_os = "macos", target_os = "ios")))]
#[cfg(metal)]
pub metal: Option<HubReport>,
#[cfg(all(feature = "dx12", windows))]
#[cfg(dx12)]
pub dx12: Option<HubReport>,
#[cfg(feature = "gles")]
#[cfg(gles)]
pub gl: Option<HubReport>,
}

Expand All @@ -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"),
}
Expand Down Expand Up @@ -110,25 +110,25 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
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 {
Expand All @@ -145,19 +145,19 @@ impl<G: GlobalIdentityHandlerFactory> Drop for Global<G> {
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);
}
Expand All @@ -175,16 +175,7 @@ impl<G: GlobalIdentityHandlerFactory> Drop for Global<G> {
}
}

#[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<crate::identity::IdentityManagerFactory>) {
fn test_internal<T: Send + Sync>(_: T) {}
test_internal(global)
Expand Down
Loading

0 comments on commit 580340f

Please sign in to comment.