Skip to content

Commit

Permalink
Panic early in Instance::new() if no backend is enabled
Browse files Browse the repository at this point in the history
Co-Authored-By: Andreas Reich <1220815+Wumpf@users.noreply.github.com>
  • Loading branch information
daxpedda and Wumpf committed Dec 15, 2023
1 parent 0989706 commit 831e7fa
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1786,19 +1786,62 @@ impl Default for Instance {
/// Creates a new instance of wgpu with default options.
///
/// Backends are set to `Backends::all()`, and FXC is chosen as the `dx12_shader_compiler`.
///
/// # Panics
///
/// If no backend feature for the active target platform is enabled,
/// this method will panic, see [`Instance::any_backend_feature_enabled()`].
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`, `vulkan-portability` or `angle`
/// * 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 Vulkan and GLES with no way to opt out
/// * Linux: always enables Vulkan and GLES with no way to opt out
/// * Web: either targets WebGPU backend or, if `webgl` 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, Vulkan or GLES backend 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-portability")
|| cfg!(feature = "angle")
} else {
true
}
}

/// Create an new instance of wgpu.
///
/// # Arguments
///
/// - `instance_desc` - Has fields for which [backends][Backends] wgpu will choose
/// during instantiation, and which [DX12 shader compiler][Dx12Compiler] wgpu will use.
///
/// # Panics
///
/// If no backend feature for the active target platform is enabled,
/// this method will panic, see [`Instance::any_backend_feature_enabled()`].
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 831e7fa

Please sign in to comment.