Skip to content

Commit

Permalink
Add cfg_aliases to wgpu (#4935)
Browse files Browse the repository at this point in the history
Co-authored-by: Connor Fitzgerald <[email protected]>
  • Loading branch information
daxpedda and cwfitzgerald authored Jan 2, 2024
1 parent ec920e8 commit f004a9d
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 707 deletions.
1 change: 1 addition & 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/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ workspace = true
features = ["clone"]
optional = true

[build-dependencies]
cfg_aliases.workspace = true

# used to test all the example shaders
[dev-dependencies.naga]
workspace = true
Expand Down
14 changes: 14 additions & 0 deletions wgpu/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
fn main() {
cfg_aliases::cfg_aliases! {
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") },
send_sync: { any(
not(target_arch = "wasm32"),
all(feature = "fragile-send-sync-non-atomic-wasm", not(target_feature = "atomics"))
) },
dx12: { all(target_os = "windows", feature = "dx12") },
metal: { all(any(target_os = "ios", target_os = "macos"), feature = "metal") }
}
}
36 changes: 13 additions & 23 deletions wgpu/src/backend/direct.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::mismatched_target_os)]

use crate::{
context::{ObjectId, Unused},
AdapterInfo, BindGroupDescriptor, BindGroupLayoutDescriptor, BindingResource, BufferBinding,
Expand Down Expand Up @@ -229,7 +231,7 @@ impl Context {
self.0.generate_report()
}

#[cfg(all(any(target_os = "ios", target_os = "macos"), feature = "metal"))]
#[cfg(metal)]
pub unsafe fn create_surface_from_core_animation_layer(
&self,
layer: *mut std::ffi::c_void,
Expand All @@ -241,7 +243,7 @@ impl Context {
}
}

#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
#[cfg(any(webgpu, webgl))]
pub fn instance_create_surface_from_canvas(
&self,
canvas: web_sys::HtmlCanvasElement,
Expand All @@ -253,7 +255,7 @@ impl Context {
})
}

#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
#[cfg(any(webgpu, webgl))]
pub fn instance_create_surface_from_offscreen_canvas(
&self,
canvas: web_sys::OffscreenCanvas,
Expand All @@ -265,7 +267,7 @@ impl Context {
})
}

#[cfg(all(target_os = "windows", feature = "dx12"))]
#[cfg(dx12)]
pub unsafe fn create_surface_from_visual(&self, visual: *mut std::ffi::c_void) -> Surface {
let id = unsafe { self.0.instance_create_surface_from_visual(visual, ()) };
Surface {
Expand All @@ -274,7 +276,7 @@ impl Context {
}
}

#[cfg(all(target_os = "windows", feature = "dx12"))]
#[cfg(dx12)]
pub unsafe fn create_surface_from_surface_handle(
&self,
surface_handle: *mut std::ffi::c_void,
Expand All @@ -289,7 +291,7 @@ impl Context {
}
}

#[cfg(all(target_os = "windows", feature = "dx12"))]
#[cfg(dx12)]
pub unsafe fn create_surface_from_swap_chain_panel(
&self,
swap_chain_panel: *mut std::ffi::c_void,
Expand Down Expand Up @@ -1444,9 +1446,9 @@ impl crate::Context for Context {
Err(e) => panic!("Error in Device::create_render_bundle_encoder: {e}"),
}
}
#[cfg_attr(target_arch = "wasm32", allow(unused))]
#[cfg_attr(not(any(native, emscripten)), allow(unused))]
fn device_drop(&self, device: &Self::DeviceId, _device_data: &Self::DeviceData) {
#[cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))]
#[cfg(any(native, emscripten))]
{
let global = &self.0;
match wgc::gfx_select!(device => global.device_poll(*device, wgt::Maintain::Wait)) {
Expand Down Expand Up @@ -2310,7 +2312,7 @@ impl crate::Context for Context {
}
}

#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
#[cfg(any(webgpu, webgl))]
fn queue_copy_external_image_to_texture(
&self,
queue: &Self::QueueId,
Expand Down Expand Up @@ -3167,21 +3169,9 @@ pub struct BufferMappedRange {
size: usize,
}

#[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 BufferMappedRange {}
#[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 BufferMappedRange {}

impl crate::context::BufferMappedRange for BufferMappedRange {
Expand Down
22 changes: 4 additions & 18 deletions wgpu/src/backend/mod.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
#[cfg(all(
target_arch = "wasm32",
not(any(target_os = "emscripten", feature = "webgl"))
))]
#[cfg(webgpu)]
mod web;
#[cfg(all(
target_arch = "wasm32",
not(any(target_os = "emscripten", feature = "webgl"))
))]
#[cfg(webgpu)]
pub(crate) use web::Context;

#[cfg(any(
not(target_arch = "wasm32"),
target_os = "emscripten",
feature = "webgl"
))]
#[cfg(not(webgpu))]
mod direct;
#[cfg(any(
not(target_arch = "wasm32"),
target_os = "emscripten",
feature = "webgl"
))]
#[cfg(not(webgpu))]
pub(crate) use direct::Context;
45 changes: 9 additions & 36 deletions wgpu/src/backend/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,50 +57,26 @@ impl<T> From<Identified<T>> for ObjectId {

#[derive(Clone, Debug)]
pub(crate) struct Sendable<T>(T);
#[cfg(all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
))]
#[cfg(send_sync)]
unsafe impl<T> Send for Sendable<T> {}
#[cfg(all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
))]
#[cfg(send_sync)]
unsafe impl<T> Sync for Sendable<T> {}

#[derive(Clone, Debug)]
pub(crate) struct Identified<T>(std::num::NonZeroU64, PhantomData<T>);
#[cfg(all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
))]
#[cfg(send_sync)]
unsafe impl<T> Send for Identified<T> {}
#[cfg(all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
))]
#[cfg(send_sync)]
unsafe impl<T> Sync for Identified<T> {}

pub(crate) struct Context(web_sys::Gpu);
#[cfg(all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
))]
#[cfg(send_sync)]
unsafe impl Send for Context {}
#[cfg(all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
))]
#[cfg(send_sync)]
unsafe impl Sync for Context {}
#[cfg(all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
))]
#[cfg(send_sync)]
unsafe impl Send for BufferMappedRange {}
#[cfg(all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
))]
#[cfg(send_sync)]
unsafe impl Sync for BufferMappedRange {}

impl fmt::Debug for Context {
Expand Down Expand Up @@ -157,10 +133,7 @@ impl<F, M> MakeSendFuture<F, M> {
}
}

#[cfg(all(
feature = "fragile-send-sync-non-atomic-wasm",
not(target_feature = "atomics")
))]
#[cfg(send_sync)]
unsafe impl<F, M> Send for MakeSendFuture<F, M> {}

fn map_texture_format(texture_format: wgt::TextureFormat) -> web_sys::GpuTextureFormat {
Expand Down
Loading

0 comments on commit f004a9d

Please sign in to comment.