2021-12-09 21:14:17 +00:00
|
|
|
use std::borrow::Cow;
|
|
|
|
|
2021-12-14 03:58:23 +00:00
|
|
|
pub use wgpu::{Backends, Features as WgpuFeatures, Limits as WgpuLimits, PowerPreference};
|
2021-12-09 21:14:17 +00:00
|
|
|
|
2022-01-04 20:08:12 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub enum WgpuOptionsPriority {
|
|
|
|
Compatibility,
|
|
|
|
Functionality,
|
|
|
|
WebGL2,
|
|
|
|
}
|
|
|
|
|
2021-12-09 21:14:17 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct WgpuOptions {
|
|
|
|
pub device_label: Option<Cow<'static, str>>,
|
2022-01-08 10:39:43 +00:00
|
|
|
pub backends: Option<Backends>,
|
2021-12-09 21:14:17 +00:00
|
|
|
pub power_preference: PowerPreference,
|
2022-01-04 20:08:12 +00:00
|
|
|
pub priority: WgpuOptionsPriority,
|
2022-02-13 04:24:52 +00:00
|
|
|
/// The enabled features. Setting features will require them to be enabled when initializing
|
|
|
|
/// the renderer.
|
2021-12-14 03:58:23 +00:00
|
|
|
pub features: WgpuFeatures,
|
2022-02-13 04:24:52 +00:00
|
|
|
/// The features to ensure are disabled regardless of what the adapter/backend supports
|
|
|
|
pub disabled_features: Option<WgpuFeatures>,
|
|
|
|
/// The imposed limits. Updated based on adapter/backend limits when initializing the renderer
|
|
|
|
/// if using WgpuOptionsPriority::Functionality
|
2021-12-14 03:58:23 +00:00
|
|
|
pub limits: WgpuLimits,
|
2022-02-13 04:24:52 +00:00
|
|
|
/// The constraints on limits allowed regardless of what the adapter/backend supports
|
|
|
|
pub constrained_limits: Option<WgpuLimits>,
|
2021-12-09 21:14:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for WgpuOptions {
|
|
|
|
fn default() -> Self {
|
2021-12-29 21:04:28 +00:00
|
|
|
let default_backends = if cfg!(feature = "webgl") {
|
2021-12-09 21:14:17 +00:00
|
|
|
Backends::GL
|
|
|
|
} else {
|
|
|
|
Backends::PRIMARY
|
|
|
|
};
|
|
|
|
|
2022-01-08 10:39:43 +00:00
|
|
|
let backends = Some(wgpu::util::backend_bits_from_env().unwrap_or(default_backends));
|
2021-12-09 21:14:17 +00:00
|
|
|
|
2022-01-04 20:08:12 +00:00
|
|
|
let priority = options_priority_from_env().unwrap_or(WgpuOptionsPriority::Functionality);
|
|
|
|
|
|
|
|
let limits = if cfg!(feature = "webgl") || matches!(priority, WgpuOptionsPriority::WebGL2) {
|
2021-12-09 21:14:17 +00:00
|
|
|
wgpu::Limits::downlevel_webgl2_defaults()
|
|
|
|
} else {
|
2021-12-14 03:58:23 +00:00
|
|
|
#[allow(unused_mut)]
|
|
|
|
let mut limits = wgpu::Limits::default();
|
|
|
|
#[cfg(feature = "ci_limits")]
|
|
|
|
{
|
|
|
|
limits.max_storage_textures_per_shader_stage = 4;
|
|
|
|
limits.max_texture_dimension_3d = 1024;
|
|
|
|
}
|
|
|
|
limits
|
2021-12-09 21:14:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Self {
|
|
|
|
device_label: Default::default(),
|
|
|
|
backends,
|
|
|
|
power_preference: PowerPreference::HighPerformance,
|
2022-01-04 20:08:12 +00:00
|
|
|
priority,
|
2021-12-09 21:14:17 +00:00
|
|
|
features: wgpu::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES,
|
2022-02-13 04:24:52 +00:00
|
|
|
disabled_features: None,
|
2021-12-09 21:14:17 +00:00
|
|
|
limits,
|
2022-02-13 04:24:52 +00:00
|
|
|
constrained_limits: None,
|
2021-12-09 21:14:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-04 20:08:12 +00:00
|
|
|
|
2022-01-09 23:20:13 +00:00
|
|
|
/// Get a features/limits priority from the environment variable `WGPU_OPTIONS_PRIO`
|
2022-01-04 20:08:12 +00:00
|
|
|
pub fn options_priority_from_env() -> Option<WgpuOptionsPriority> {
|
|
|
|
Some(
|
|
|
|
match std::env::var("WGPU_OPTIONS_PRIO")
|
|
|
|
.as_deref()
|
|
|
|
.map(str::to_lowercase)
|
|
|
|
.as_deref()
|
|
|
|
{
|
|
|
|
Ok("compatibility") => WgpuOptionsPriority::Compatibility,
|
|
|
|
Ok("functionality") => WgpuOptionsPriority::Functionality,
|
|
|
|
Ok("webgl2") => WgpuOptionsPriority::WebGL2,
|
|
|
|
_ => return None,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|