maplibre/render/
settings.rs

1//! Settings for the renderer
2
3use std::borrow::Cow;
4
5use wgpu::PresentMode;
6pub use wgpu::{Backends, Features, Limits, PowerPreference, TextureFormat};
7
8/// Provides configuration for renderer initialization. Use [`Device::features`](crate::renderer::Device::features),
9/// [`Device::limits`](crate::renderer::Device::limits), and the [`WgpuAdapterInfo`](crate::render_resource::WgpuAdapterInfo)
10/// resource to get runtime information about the actual adapter, backend, features, and limits.
11#[derive(Clone)]
12pub struct WgpuSettings {
13    pub device_label: Option<Cow<'static, str>>,
14    pub backends: Option<Backends>,
15    pub power_preference: PowerPreference,
16    /// The features to ensure are enabled regardless of what the adapter/backend supports.
17    /// Setting these explicitly may cause renderer initialization to fail.
18    pub features: Features,
19    /// The features to ensure are disabled regardless of what the adapter/backend supports
20    pub disabled_features: Option<Features>,
21    /// The imposed limits.
22    pub limits: Limits,
23    /// The constraints on limits allowed regardless of what the adapter/backend supports
24    pub constrained_limits: Option<Limits>,
25
26    /// Whether a trace is recorded an stored in the current working directory
27    pub record_trace: bool,
28}
29
30impl Default for WgpuSettings {
31    fn default() -> Self {
32        let backends = Some(wgpu::util::backend_bits_from_env().unwrap_or(Backends::all()));
33
34        let limits = if cfg!(feature = "web-webgl") {
35            Limits {
36                max_texture_dimension_2d: 4096,
37                ..Limits::downlevel_webgl2_defaults()
38            }
39        } else if cfg!(target_os = "android") {
40            Limits {
41                max_storage_textures_per_shader_stage: 4,
42                max_compute_workgroups_per_dimension: 0,
43                max_compute_workgroup_size_z: 0,
44                max_compute_workgroup_size_y: 0,
45                max_compute_workgroup_size_x: 0,
46                max_compute_workgroup_storage_size: 0,
47                max_compute_invocations_per_workgroup: 0,
48                ..Limits::downlevel_defaults()
49            }
50        } else {
51            Limits {
52                ..Limits::default()
53            }
54        };
55
56        let features = Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES;
57
58        Self {
59            device_label: Default::default(),
60            backends,
61            power_preference: PowerPreference::HighPerformance,
62            features,
63            disabled_features: None,
64            limits,
65            constrained_limits: None,
66            record_trace: false,
67        }
68    }
69}
70
71#[derive(Clone)]
72pub enum SurfaceType {
73    Headless,
74    Headed,
75}
76
77#[derive(Copy, Clone)]
78/// Configuration resource for [Multi-Sample Anti-Aliasing](https://en.wikipedia.org/wiki/Multisample_anti-aliasing).
79///
80pub struct Msaa {
81    /// The number of samples to run for Multi-Sample Anti-Aliasing. Higher numbers result in
82    /// smoother edges.
83    /// Defaults to 4.
84    ///
85    /// Note that WGPU currently only supports 1 or 4 samples.
86    /// Ultimately we plan on supporting whatever is natively supported on a given device.
87    /// Check out this issue for more info: <https://github.com/gfx-rs/wgpu/issues/1832>
88    pub samples: u32,
89}
90
91impl Msaa {
92    pub fn is_multisampling(&self) -> bool {
93        self.samples > 1
94    }
95}
96
97impl Default for Msaa {
98    fn default() -> Self {
99        // By default we are trying to multisample
100        Self { samples: 4 }
101    }
102}
103
104#[derive(Clone, Copy)]
105pub struct RendererSettings {
106    pub msaa: Msaa,
107    /// Explicitly set a texture format or let the renderer automatically choose one
108    pub texture_format: Option<TextureFormat>,
109    pub depth_texture_format: TextureFormat,
110    /// Present mode for surfaces if a surface is used.
111    pub present_mode: PresentMode,
112}
113
114impl Default for RendererSettings {
115    fn default() -> Self {
116        Self {
117            msaa: Msaa::default(),
118            texture_format: None,
119
120            depth_texture_format: TextureFormat::Depth24PlusStencil8,
121            present_mode: PresentMode::AutoVsync,
122        }
123    }
124}