Skip to content

Commit

Permalink
any_backend_feature_enabled method to panic on
Browse files Browse the repository at this point in the history
  • Loading branch information
Wumpf committed Dec 15, 2023
1 parent 68fd5d0 commit 9c7f770
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1785,20 +1785,54 @@ impl error::Error for SurfaceError {}
impl Default for Instance {
/// Creates a new instance of wgpu with default options.
///
/// If no backend feature for the active target platform is enabled,
/// this method will panic, see [`Instance::any_backend_feature_enabled`].
///
/// Backends are set to `Backends::all()`, and FXC is chosen as the `dx12_shader_compiler`.
fn default() -> Self {
Self::new(InstanceDescriptor::default())
}
}

impl Instance {
/// Returns `true` if any backend feature is enabled for the current build configuration.
///
/// Which feature makes this method return true depends on the target platform:
/// * MacOS/iOS: `metal` or `vulkan`
/// * All other: Always returns true
///
/// TODO: Right now it's otherwise not possible yet to opt-out of all features on most platforms.
/// See https://github.com/gfx-rs/wgpu/issues/3514
/// * Windows: always enables `gles` and `vulkan` with no way to opt out
/// * Linux: always enables `gles` and `vulkan` with no way to opt out
/// * Web: either targets WebGPU backend or, if `gles` enabled, WebGL
/// * TODO: Support both WebGPU and WebGL at the same time, see https://github.com/gfx-rs/wgpu/issues/2804
pub const fn any_backend_feature_enabled() -> bool {
// Method intentionally kept verbose to keep it a bit easier to follow!

// On macOS and iOS, at least one of `metal` or `vulkan` feature must be enabled.
let is_mac_or_ios = cfg!(target_os = "macos") || cfg!(target_os = "ios");
if is_mac_or_ios {
cfg!(feature = "metal") || cfg!(feature = "vulkan")

This comment has been minimized.

Copy link
@daxpedda

daxpedda Dec 15, 2023

            cfg!(feature = "metal") || cfg!(feature = "vulkan-portability") || cfg!(feature = "angle")

The documentation should probably also be similarly adjusted. The features talked about there are from wgc not wgpu.

} else {
true
}
}

/// Create an new instance of wgpu.
///
/// If no backend feature for the active target platform is enabled,
/// this method will panic, see [`Instance::any_backend_feature_enabled`].
///
/// # Arguments
///
/// - `instance_desc` - Has fields for which [backends][Backends] wgpu will choose
/// during instantiation, and which [DX12 shader compiler][Dx12Compiler] wgpu will use.
pub fn new(instance_desc: InstanceDescriptor) -> Self {
if !Self::any_backend_feature_enabled() {
panic!("No wgpu backend feature that is implemented for the target platform was enabled. See `wgpu::Instance::any_backend_feature_enabled` for more information.");
}

Self {
context: Arc::from(crate::backend::Context::init(instance_desc)),
}
Expand Down

0 comments on commit 9c7f770

Please sign in to comment.