wgpu_types/
instance.rs

1//! Types for dealing with Instances.
2
3use crate::{link_to_wgpu_docs, Backends};
4
5#[cfg(doc)]
6use crate::{Backend, DownlevelFlags};
7
8/// A [`raw_window_handle::HasDisplayHandle`] that can be shared across threads and has no borrows.
9///
10/// This blanket trait is automatically implemented for all objects that qualify.
11pub trait WgpuHasDisplayHandle:
12    raw_window_handle::HasDisplayHandle + core::fmt::Debug + Send + Sync + 'static
13{
14}
15impl<T: raw_window_handle::HasDisplayHandle + core::fmt::Debug + Send + Sync + 'static>
16    WgpuHasDisplayHandle for T
17{
18}
19
20/// Options for creating an instance.
21///
22/// If you want to allow control of instance settings via environment variables, call either
23/// [`InstanceDescriptor::from_env_or_default()`] or [`InstanceDescriptor::with_env()`]. Each type
24/// within this descriptor has its own equivalent methods, so you can select which options you want
25/// to expose to influence from the environment.
26#[derive(Debug, Default)]
27pub struct InstanceDescriptor {
28    /// Which [`Backends`] to enable.
29    ///
30    /// [`Backends::BROWSER_WEBGPU`] has an additional effect:
31    /// If it is set and a [`navigator.gpu`](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/gpu)
32    /// object is present, this instance will *only* be able to create WebGPU adapters.
33    ///
34    /// ⚠️ On some browsers this check is insufficient to determine whether WebGPU is supported,
35    /// as the browser may define the `navigator.gpu` object, but be unable to create any WebGPU adapters.
36    /// For targeting _both_ WebGPU & WebGL, it is recommended to use [`crate::util::new_instance_with_webgpu_detection`](../wgpu/util/fn.new_instance_with_webgpu_detection.html).
37    ///
38    /// If you instead want to force use of WebGL, either disable the `webgpu` compile-time feature
39    /// or don't include the [`Backends::BROWSER_WEBGPU`] flag in this field.
40    /// If it is set and WebGPU support is *not* detected, the instance will use `wgpu-core`
41    /// to create adapters, meaning that if the `webgl` feature is enabled, it is able to create
42    /// a WebGL adapter.
43    pub backends: Backends,
44    /// Flags to tune the behavior of the instance.
45    pub flags: InstanceFlags,
46    /// Memory budget thresholds used by some backends.
47    pub memory_budget_thresholds: MemoryBudgetThresholds,
48    /// Options the control the behavior of specific backends.
49    pub backend_options: crate::BackendOptions,
50    /// System platform or compositor connection to connect this `Instance` to.
51    ///
52    /// If not [`None`], it is invalid to pass a different [`raw_window_handle::HasDisplayHandle`] to `create_surface()`.
53    ///
54    /// - On GLES, this is required when intending to present on the platform, especially for Wayland.
55    /// - On Vulkan, Metal and Dx12, this is currently unused.
56    ///
57    /// When used with `winit`, callers are expected to pass its [`OwnedDisplayHandle`] (created from
58    /// the `EventLoop`) here.
59    ///
60    /// [`OwnedDisplayHandle`]: https://docs.rs/winit/latest/winit/event_loop/struct.OwnedDisplayHandle.html
61    // FUTURE: The RawDisplayHandle trait can/should be removed entirely from create_display()? At
62    // least `trait WindowHandle: HasWindowHandle + HasDisplayHandle` should really be removed as
63    // it's impractical and not implementable everywhere.
64    pub display: Option<alloc::boxed::Box<dyn WgpuHasDisplayHandle>>,
65}
66
67impl InstanceDescriptor {
68    /// Choose instance options entirely from environment variables.
69    ///
70    /// This is equivalent to calling `from_env` on every field.
71    #[must_use]
72    pub fn from_env_or_default() -> Self {
73        Self::default().with_env()
74    }
75
76    /// Takes the given options, modifies them based on the environment variables, and returns the result.
77    ///
78    /// This is equivalent to calling `with_env` on every field.
79    #[must_use]
80    pub fn with_env(self) -> Self {
81        let backends = self.backends.with_env();
82        let flags = self.flags.with_env();
83        let backend_options = self.backend_options.with_env();
84        Self {
85            backends,
86            flags,
87            memory_budget_thresholds: MemoryBudgetThresholds::default(),
88            backend_options,
89            display: None,
90        }
91    }
92
93    /// Appends the given `display` object to the descriptor.
94    #[must_use]
95    pub fn with_display_handle(self, display: alloc::boxed::Box<dyn WgpuHasDisplayHandle>) -> Self {
96        Self {
97            display: Some(display),
98            ..self
99        }
100    }
101}
102
103bitflags::bitflags! {
104    /// Instance debugging flags.
105    ///
106    /// These are not part of the WebGPU standard.
107    ///
108    /// Defaults to enabling debugging-related flags if the build configuration has `debug_assertions`.
109    #[repr(transparent)]
110    #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
111    pub struct InstanceFlags: u32 {
112        /// Generate debug information in shaders and objects.
113        ///
114        /// When `Self::from_env()` is used takes value from `WGPU_DEBUG` environment variable.
115        const DEBUG = 1 << 0;
116        /// Enable validation in the backend API, if possible:
117        ///
118        /// - On the Direct3D `dx12` backend, this calls [`ID3D12Debug::EnableDebugLayer`][dx12].
119        ///
120        /// - On the Vulkan backend, this enables the [Vulkan Validation Layer][vvl].
121        ///
122        /// - On the `gles` backend driving Windows OpenGL, this enables [debug
123        ///   output][gl:do], effectively calling `glEnable(GL_DEBUG_OUTPUT)`.
124        ///
125        /// - On non-Windows `gles` backends, this calls
126        ///   [`eglDebugMessageControlKHR`][gl:dm] to enable all debugging messages.
127        ///   If the GLES implementation is ANGLE running on Vulkan, this also
128        ///   enables the Vulkan validation layers by setting
129        ///   [`EGL_PLATFORM_ANGLE_DEBUG_LAYERS_ENABLED`][gl:av].
130        ///
131        /// When `Self::from_env()` is used, this bit is set if the `WGPU_VALIDATION`
132        /// environment variable has any value but "0".
133        ///
134        /// [dx12]: https://learn.microsoft.com/en-us/windows/win32/api/d3d12sdklayers/nf-d3d12sdklayers-id3d12debug-enabledebuglayer
135        /// [vvl]: https://github.com/KhronosGroup/Vulkan-ValidationLayers
136        /// [gl:dm]: https://registry.khronos.org/EGL/extensions/KHR/EGL_KHR_debug.txt
137        /// [gl:do]: https://www.khronos.org/opengl/wiki/Debug_Output
138        /// [gl:av]: https://chromium.googlesource.com/angle/angle/+/HEAD/extensions/EGL_ANGLE_platform_angle.txt
139        const VALIDATION = 1 << 1;
140        /// Don't pass labels to wgpu-hal.
141        ///
142        /// When `Self::from_env()` is used takes value from `WGPU_DISCARD_HAL_LABELS` environment variable.
143        const DISCARD_HAL_LABELS = 1 << 2;
144        /// Whether wgpu should expose adapters that run on top of non-compliant adapters.
145        ///
146        /// Turning this on might mean that some of the functionality provided by the wgpu
147        /// adapter/device is not working or is broken. It could be that all the functionality
148        /// wgpu currently exposes works but we can't tell for sure since we have no additional
149        /// transparency into what is working and what is not on the underlying adapter.
150        ///
151        /// This mainly applies to a Vulkan driver's compliance version. If the major compliance version
152        /// is `0`, then the driver is ignored. This flag allows that driver to be enabled for testing.
153        ///
154        /// When `Self::from_env()` is used takes value from `WGPU_ALLOW_UNDERLYING_NONCOMPLIANT_ADAPTER` environment variable.
155        const ALLOW_UNDERLYING_NONCOMPLIANT_ADAPTER = 1 << 3;
156        /// Enable GPU-based validation. Implies [`Self::VALIDATION`]. Currently, this only changes
157        /// behavior on the DX12 and Vulkan backends.
158        ///
159        /// Supported platforms:
160        ///
161        /// - D3D12; called ["GPU-based validation", or
162        ///   "GBV"](https://web.archive.org/web/20230206120404/https://learn.microsoft.com/en-us/windows/win32/direct3d12/using-d3d12-debug-layer-gpu-based-validation)
163        /// - Vulkan, via the `VK_LAYER_KHRONOS_validation` layer; called ["GPU-Assisted
164        ///   Validation"](https://github.com/KhronosGroup/Vulkan-ValidationLayers/blob/e45aeb85079e0835694cb8f03e6681fd18ae72c9/docs/gpu_validation.md#gpu-assisted-validation)
165        ///
166        /// When `Self::from_env()` is used takes value from `WGPU_GPU_BASED_VALIDATION` environment variable.
167        const GPU_BASED_VALIDATION = 1 << 4;
168
169        /// Validate indirect buffer content prior to issuing indirect draws/dispatches.
170        ///
171        /// This validation will transform indirect calls into no-ops if they are not valid:
172        ///
173        /// - When calling `dispatch_workgroups_indirect`, all 3 indirect arguments encoded in the buffer
174        /// must be less than the `max_compute_workgroups_per_dimension` device limit.
175        /// - When calling `draw_indirect`/`draw_indexed_indirect`/`multi_draw_indirect`/`multi_draw_indexed_indirect`:
176        ///   - If `Features::INDIRECT_FIRST_INSTANCE` is not enabled on the device, the `first_instance` indirect argument must be 0.
177        ///   - The `first_instance` & `instance_count` indirect arguments must form a range that fits within all bound vertex buffers with `step_mode` set to `Instance`.
178        /// - When calling `draw_indirect`/`multi_draw_indirect`:
179        ///   - The `first_vertex` & `vertex_count` indirect arguments must form a range that fits within all bound vertex buffers with `step_mode` set to `Vertex`.
180        /// - When calling `draw_indexed_indirect`/`multi_draw_indexed_indirect`:
181        ///   - The `first_index` & `index_count` indirect arguments must form a range that fits within the bound index buffer.
182        ///
183        /// __Behavior is undefined if this validation is disabled and the rules above are not satisfied.__
184        ///
185        /// Disabling this will also cause the following built-ins to not report the right values on the D3D12 backend:
186        ///
187        /// - the 3 components of `@builtin(num_workgroups)` will be 0
188        /// - the value of `@builtin(vertex_index)` will not take into account the value of the `first_vertex`/`base_vertex` argument present in the indirect buffer
189        /// - the value of `@builtin(instance_index)` will not take into account the value of the `first_instance` argument present in the indirect buffer
190        ///
191        /// When `Self::from_env()` is used takes value from `WGPU_VALIDATION_INDIRECT_CALL` environment variable.
192        const VALIDATION_INDIRECT_CALL = 1 << 5;
193
194        /// Enable automatic timestamp normalization. This means that in [`CommandEncoder::resolve_query_set`][rqs],
195        /// the timestamps will automatically be normalized to be in nanoseconds instead of the raw timestamp values.
196        ///
197        /// This is disabled by default because it introduces a compute shader into the resolution of query sets.
198        ///
199        /// This can be useful for users that need to read timestamps on the gpu, as the normalization
200        /// can be a hassle to do manually. When this is enabled, the timestamp period returned by the queue
201        /// will always be `1.0`.
202        ///
203        #[doc = link_to_wgpu_docs!(["rqs"]: "struct.CommandEncoder.html#method.resolve_query_set")]
204        const AUTOMATIC_TIMESTAMP_NORMALIZATION = 1 << 6;
205    }
206}
207
208impl Default for InstanceFlags {
209    fn default() -> Self {
210        Self::from_build_config()
211    }
212}
213
214impl InstanceFlags {
215    /// Enable recommended debugging and validation flags.
216    #[must_use]
217    pub fn debugging() -> Self {
218        InstanceFlags::DEBUG | InstanceFlags::VALIDATION | InstanceFlags::VALIDATION_INDIRECT_CALL
219    }
220
221    /// Enable advanced debugging and validation flags (potentially very slow).
222    #[must_use]
223    pub fn advanced_debugging() -> Self {
224        Self::debugging() | InstanceFlags::GPU_BASED_VALIDATION
225    }
226
227    /// Infer decent defaults from the build type.
228    ///
229    /// If `cfg!(debug_assertions)` is true, then this returns [`Self::debugging()`].
230    /// Otherwise, it returns [`Self::empty()`].
231    #[must_use]
232    pub fn from_build_config() -> Self {
233        if cfg!(debug_assertions) {
234            return InstanceFlags::debugging();
235        }
236
237        InstanceFlags::VALIDATION_INDIRECT_CALL
238    }
239
240    /// Derive defaults from environment variables. See [`Self::with_env()`] for more information.
241    #[must_use]
242    pub fn from_env_or_default() -> Self {
243        Self::default().with_env()
244    }
245
246    /// Takes the given flags, modifies them based on the environment variables, and returns the result.
247    ///
248    /// - If an environment variable is set to anything but "0", the corresponding flag is set.
249    /// - If the value is "0", the flag is unset.
250    /// - If the environment variable is not present, then the flag retains its initial value.
251    ///
252    /// For example `let flags = InstanceFlags::debugging().with_env();` with `WGPU_VALIDATION=0`
253    /// does not contain [`InstanceFlags::VALIDATION`].
254    ///
255    /// The environment variables are named after the flags prefixed with "WGPU_". For example:
256    /// - `WGPU_DEBUG`
257    /// - `WGPU_VALIDATION`
258    /// - `WGPU_DISCARD_HAL_LABELS`
259    /// - `WGPU_ALLOW_UNDERLYING_NONCOMPLIANT_ADAPTER`
260    /// - `WGPU_GPU_BASED_VALIDATION`
261    /// - `WGPU_VALIDATION_INDIRECT_CALL`
262    #[must_use]
263    pub fn with_env(mut self) -> Self {
264        fn env(key: &str) -> Option<bool> {
265            crate::env::var(key).map(|s| match s.as_str() {
266                "0" => false,
267                _ => true,
268            })
269        }
270
271        if let Some(bit) = env("WGPU_VALIDATION") {
272            self.set(Self::VALIDATION, bit);
273        }
274
275        if let Some(bit) = env("WGPU_DEBUG") {
276            self.set(Self::DEBUG, bit);
277        }
278        if let Some(bit) = env("WGPU_DISCARD_HAL_LABELS") {
279            self.set(Self::DISCARD_HAL_LABELS, bit);
280        }
281        if let Some(bit) = env("WGPU_ALLOW_UNDERLYING_NONCOMPLIANT_ADAPTER") {
282            self.set(Self::ALLOW_UNDERLYING_NONCOMPLIANT_ADAPTER, bit);
283        }
284        if let Some(bit) = env("WGPU_GPU_BASED_VALIDATION") {
285            self.set(Self::GPU_BASED_VALIDATION, bit);
286        }
287        if let Some(bit) = env("WGPU_VALIDATION_INDIRECT_CALL") {
288            self.set(Self::VALIDATION_INDIRECT_CALL, bit);
289        }
290
291        self
292    }
293}
294
295/// Memory budget thresholds used by backends to try to avoid high memory pressure situations.
296///
297/// Currently only the D3D12 and (optionally) Vulkan backends support these options.
298#[derive(Default, Clone, Debug, Copy)]
299pub struct MemoryBudgetThresholds {
300    /// Threshold at which texture, buffer, query set and acceleration structure creation will start to return OOM errors.
301    /// This is a percent of the memory budget reported by native APIs.
302    ///
303    /// If not specified, resource creation might still return OOM errors.
304    pub for_resource_creation: Option<u8>,
305
306    /// Threshold at which devices will become lost due to memory pressure.
307    /// This is a percent of the memory budget reported by native APIs.
308    ///
309    /// If not specified, devices might still become lost due to memory pressure.
310    pub for_device_loss: Option<u8>,
311}