Skip to content

Commit

Permalink
Make crate features no-op on incompatible targets (#3466)
Browse files Browse the repository at this point in the history
* Make crate features no-op on incompatible targets

* Remove `portable_features`

* Test `--all-features` in CI

* Make `renderdoc` no-op on WASM

* Address review
  • Loading branch information
daxpedda authored Feb 21, 2023
1 parent 9cdcd67 commit 7430330
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 139 deletions.
25 changes: 11 additions & 14 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -125,20 +125,19 @@ jobs:
# build for WebGPU
cargo clippy --target ${{ matrix.target }} -p wgpu --tests --features glsl,spirv
# build for WebGL
cargo clippy --target ${{ matrix.target }} -p wgpu --tests --features webgl,glsl,spirv
# build docs
cargo doc --target ${{ matrix.target }} -p wgpu --no-deps --features glsl,spirv
# all features
cargo clippy --target ${{ matrix.target }} -p wgpu -p wgpu-hal --tests --all-features
cargo doc --target ${{ matrix.target }} -p wgpu --no-deps --all-features
- name: check em
if: matrix.kind == 'em'
shell: bash
run: |
set -e
# build for Emscripten/WebGL
# build for Emscripten
cargo clippy --target ${{ matrix.target }} -p wgpu -p wgpu-hal --no-default-features
# build cube example
Expand All @@ -147,6 +146,10 @@ jobs:
# build raw-gles example
cargo clippy --target ${{ matrix.target }} --example raw-gles
# all features
cargo clippy --target ${{ matrix.target }} -p wgpu-hal --all-features
cargo clippy --target ${{ matrix.target }} -p wgpu --all-features
- name: check native
if: matrix.kind == 'native'
shell: bash
Expand All @@ -157,16 +160,10 @@ jobs:
cargo clippy --target ${{ matrix.target }} -p wgpu -p wgpu-core -p wgpu-info -p player --no-default-features
# Check with all features.
# (But watch out for backend-selection features in wgpu-core; some of
# those only build on the right platforms.)
cargo clippy --target ${{ matrix.target }} -p wgpu -p wgpu-info -p player --tests --all-features
cargo clippy --target ${{ matrix.target }} -p wgpu-core --tests --features="portable_features"
cargo clippy --target ${{ matrix.target }} -p wgpu -p wgpu-info -p player -p wgpu-core -p wgpu-hal --tests --all-features
# build docs
# (Watch out for backend-selection features in wgpu-core; some of
# those only build on the right platforms.)
cargo doc --target ${{ matrix.target }} -p wgpu -p wgpu-info -p player --all-features --no-deps
cargo doc --target ${{ matrix.target }} -p wgpu-core --no-deps --features="portable_features"
cargo doc --target ${{ matrix.target }} -p wgpu -p wgpu-info -p player -p wgpu-core -p wgpu-hal --all-features --no-deps
wasm-test:
name: Test WebAssembly
Expand Down
3 changes: 0 additions & 3 deletions wgpu-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ id32 = []
# Enable `ShaderModuleSource::Wgsl`
wgsl = ["naga/wgsl-in"]

# Features that are intended to work on all platforms.
portable_features = ["gles", "strict_asserts", "trace", "replay", "serial-pass", "id32", "wgsl"]

[dependencies]
arrayvec = "0.7"
bitflags = "1"
Expand Down
8 changes: 4 additions & 4 deletions wgpu-core/src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5560,22 +5560,22 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
let mut closures = UserClosures::default();
let mut all_queue_empty = true;

#[cfg(feature = "vulkan")]
#[cfg(all(feature = "vulkan", not(target_arch = "wasm32")))]
{
all_queue_empty = self.poll_devices::<hal::api::Vulkan>(force_wait, &mut closures)?
&& all_queue_empty;
}
#[cfg(feature = "metal")]
#[cfg(all(feature = "metal", any(target_os = "macos", target_os = "ios")))]
{
all_queue_empty =
self.poll_devices::<hal::api::Metal>(force_wait, &mut closures)? && all_queue_empty;
}
#[cfg(feature = "dx12")]
#[cfg(all(feature = "dx12", windows))]
{
all_queue_empty =
self.poll_devices::<hal::api::Dx12>(force_wait, &mut closures)? && all_queue_empty;
}
#[cfg(feature = "dx11")]
#[cfg(all(feature = "dx11", windows))]
{
all_queue_empty =
self.poll_devices::<hal::api::Dx11>(force_wait, &mut closures)? && all_queue_empty;
Expand Down
48 changes: 24 additions & 24 deletions wgpu-core/src/hub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1057,13 +1057,13 @@ impl<A: HalApi, F: GlobalIdentityHandlerFactory> Hub<A, F> {
}

pub struct Hubs<F: GlobalIdentityHandlerFactory> {
#[cfg(feature = "vulkan")]
#[cfg(all(feature = "vulkan", not(target_arch = "wasm32")))]
vulkan: Hub<hal::api::Vulkan, F>,
#[cfg(feature = "metal")]
#[cfg(all(feature = "metal", any(target_os = "macos", target_os = "ios")))]
metal: Hub<hal::api::Metal, F>,
#[cfg(feature = "dx12")]
#[cfg(all(feature = "dx12", windows))]
dx12: Hub<hal::api::Dx12, F>,
#[cfg(feature = "dx11")]
#[cfg(all(feature = "dx11", windows))]
dx11: Hub<hal::api::Dx11, F>,
#[cfg(feature = "gles")]
gl: Hub<hal::api::Gles, F>,
Expand All @@ -1072,13 +1072,13 @@ pub struct Hubs<F: GlobalIdentityHandlerFactory> {
impl<F: GlobalIdentityHandlerFactory> Hubs<F> {
fn new(factory: &F) -> Self {
Self {
#[cfg(feature = "vulkan")]
#[cfg(all(feature = "vulkan", not(target_arch = "wasm32")))]
vulkan: Hub::new(factory),
#[cfg(feature = "metal")]
#[cfg(all(feature = "metal", any(target_os = "macos", target_os = "ios")))]
metal: Hub::new(factory),
#[cfg(feature = "dx12")]
#[cfg(all(feature = "dx12", windows))]
dx12: Hub::new(factory),
#[cfg(feature = "dx11")]
#[cfg(all(feature = "dx11", windows))]
dx11: Hub::new(factory),
#[cfg(feature = "gles")]
gl: Hub::new(factory),
Expand All @@ -1089,13 +1089,13 @@ impl<F: GlobalIdentityHandlerFactory> Hubs<F> {
#[derive(Debug)]
pub struct GlobalReport {
pub surfaces: StorageReport,
#[cfg(feature = "vulkan")]
#[cfg(all(feature = "vulkan", not(target_arch = "wasm32")))]
pub vulkan: Option<HubReport>,
#[cfg(feature = "metal")]
#[cfg(all(feature = "metal", any(target_os = "macos", target_os = "ios")))]
pub metal: Option<HubReport>,
#[cfg(feature = "dx12")]
#[cfg(all(feature = "dx12", windows))]
pub dx12: Option<HubReport>,
#[cfg(feature = "dx11")]
#[cfg(all(feature = "dx11", windows))]
pub dx11: Option<HubReport>,
#[cfg(feature = "gles")]
pub gl: Option<HubReport>,
Expand Down Expand Up @@ -1162,25 +1162,25 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
pub fn generate_report(&self) -> GlobalReport {
GlobalReport {
surfaces: self.surfaces.data.read().generate_report(),
#[cfg(feature = "vulkan")]
#[cfg(all(feature = "vulkan", not(target_arch = "wasm32")))]
vulkan: if self.instance.vulkan.is_some() {
Some(self.hubs.vulkan.generate_report())
} else {
None
},
#[cfg(feature = "metal")]
#[cfg(all(feature = "metal", any(target_os = "macos", target_os = "ios")))]
metal: if self.instance.metal.is_some() {
Some(self.hubs.metal.generate_report())
} else {
None
},
#[cfg(feature = "dx12")]
#[cfg(all(feature = "dx12", windows))]
dx12: if self.instance.dx12.is_some() {
Some(self.hubs.dx12.generate_report())
} else {
None
},
#[cfg(feature = "dx11")]
#[cfg(all(feature = "dx11", windows))]
dx11: if self.instance.dx11.is_some() {
Some(self.hubs.dx11.generate_report())
} else {
Expand All @@ -1203,19 +1203,19 @@ impl<G: GlobalIdentityHandlerFactory> Drop for Global<G> {
let mut surface_guard = self.surfaces.data.write();

// destroy hubs before the instance gets dropped
#[cfg(feature = "vulkan")]
#[cfg(all(feature = "vulkan", not(target_arch = "wasm32")))]
{
self.hubs.vulkan.clear(&mut surface_guard, true);
}
#[cfg(feature = "metal")]
#[cfg(all(feature = "metal", any(target_os = "macos", target_os = "ios")))]
{
self.hubs.metal.clear(&mut surface_guard, true);
}
#[cfg(feature = "dx12")]
#[cfg(all(feature = "dx12", windows))]
{
self.hubs.dx12.clear(&mut surface_guard, true);
}
#[cfg(feature = "dx11")]
#[cfg(all(feature = "dx11", windows))]
{
self.hubs.dx11.clear(&mut surface_guard, true);
}
Expand Down Expand Up @@ -1261,7 +1261,7 @@ impl HalApi for hal::api::Empty {
}
}

#[cfg(feature = "vulkan")]
#[cfg(all(feature = "vulkan", not(target_arch = "wasm32")))]
impl HalApi for hal::api::Vulkan {
const VARIANT: Backend = Backend::Vulkan;
fn create_instance_from_hal(name: &str, hal_instance: Self::Instance) -> Instance {
Expand All @@ -1285,7 +1285,7 @@ impl HalApi for hal::api::Vulkan {
}
}

#[cfg(feature = "metal")]
#[cfg(all(feature = "metal", any(target_os = "macos", target_os = "ios")))]
impl HalApi for hal::api::Metal {
const VARIANT: Backend = Backend::Metal;
fn create_instance_from_hal(name: &str, hal_instance: Self::Instance) -> Instance {
Expand All @@ -1309,7 +1309,7 @@ impl HalApi for hal::api::Metal {
}
}

#[cfg(feature = "dx12")]
#[cfg(all(feature = "dx12", windows))]
impl HalApi for hal::api::Dx12 {
const VARIANT: Backend = Backend::Dx12;
fn create_instance_from_hal(name: &str, hal_instance: Self::Instance) -> Instance {
Expand All @@ -1333,7 +1333,7 @@ impl HalApi for hal::api::Dx12 {
}
}

#[cfg(feature = "dx11")]
#[cfg(all(feature = "dx11", windows))]
impl HalApi for hal::api::Dx11 {
const VARIANT: Backend = Backend::Dx11;
fn create_instance_from_hal(name: &str, hal_instance: Self::Instance) -> Instance {
Expand Down
Loading

0 comments on commit 7430330

Please sign in to comment.