wgpu/api/
instance.rs

1use alloc::vec::Vec;
2use core::future::Future;
3
4use crate::{dispatch::InstanceInterface, util::Mutex, *};
5
6bitflags::bitflags! {
7    /// WGSL language extensions.
8    ///
9    /// WGSL spec.: <https://www.w3.org/TR/WGSL/#language-extensions-sec>
10    #[derive(Debug, Clone, PartialEq, PartialOrd, Ord, Eq, Hash)]
11    pub struct WgslLanguageFeatures: u32 {
12        /// <https://www.w3.org/TR/WGSL/#language_extension-readonly_and_readwrite_storage_textures>
13        const ReadOnlyAndReadWriteStorageTextures = 1 << 0;
14        /// <https://www.w3.org/TR/WGSL/#language_extension-packed_4x8_integer_dot_product>
15        const Packed4x8IntegerDotProduct = 1 << 1;
16        /// <https://www.w3.org/TR/WGSL/#language_extension-unrestricted_pointer_parameters>
17        const UnrestrictedPointerParameters = 1 << 2;
18        /// <https://www.w3.org/TR/WGSL/#language_extension-pointer_composite_access>
19        const PointerCompositeAccess = 1 << 3;
20    }
21}
22
23/// Contains the various entry points to start interacting with the system's GPUs.
24///
25/// This is the first thing you create when using wgpu.
26/// Its primary use is to create [`Adapter`]s and [`Surface`]s.
27///
28/// Does not have to be kept alive.
29///
30/// Corresponds to [WebGPU `GPU`](https://gpuweb.github.io/gpuweb/#gpu-interface).
31#[derive(Debug, Clone)]
32pub struct Instance {
33    inner: dispatch::DispatchInstance,
34}
35#[cfg(send_sync)]
36static_assertions::assert_impl_all!(Instance: Send, Sync);
37
38crate::cmp::impl_eq_ord_hash_proxy!(Instance => .inner);
39
40impl Default for Instance {
41    /// Creates a new instance of wgpu with default options.
42    ///
43    /// Backends are set to `Backends::all()`, and FXC is chosen as the `dx12_shader_compiler`.
44    ///
45    /// # Panics
46    ///
47    /// If no backend feature for the active target platform is enabled,
48    /// this method will panic, see [`Instance::enabled_backend_features()`].
49    fn default() -> Self {
50        // TODO: Differentiate constructors here too?
51        Self::new(InstanceDescriptor::new_without_display_handle())
52    }
53}
54
55impl Instance {
56    /// Create an new instance of wgpu using the given options and enabled backends.
57    ///
58    /// # Panics
59    ///
60    /// - If no backend feature for the active target platform is enabled,
61    ///   this method will panic; see [`Instance::enabled_backend_features()`].
62    #[allow(clippy::allow_attributes, unreachable_code)]
63    pub fn new(desc: InstanceDescriptor) -> Self {
64        if Self::enabled_backend_features().is_empty() {
65            panic!(
66                "No wgpu backend feature that is implemented for the target platform was enabled. \
67                 See `wgpu::Instance::enabled_backend_features()` for more information."
68            );
69        }
70
71        #[cfg(webgpu)]
72        {
73            let is_only_available_backend = !cfg!(wgpu_core);
74            let requested_webgpu = desc.backends.contains(Backends::BROWSER_WEBGPU);
75            let support_webgpu = crate::backend::get_browser_gpu_property()
76                .map(|maybe_gpu| maybe_gpu.is_some())
77                .unwrap_or(false);
78
79            if is_only_available_backend || (requested_webgpu && support_webgpu) {
80                return Self {
81                    inner: crate::backend::ContextWebGpu::new(desc).into(),
82                };
83            }
84        }
85
86        #[cfg(wgpu_core)]
87        {
88            return Self {
89                inner: crate::backend::ContextWgpuCore::new(desc).into(),
90            };
91        }
92
93        // Silence unused variable warnings without adding _ to the parameter name (which shows up in docs).
94        let _ = desc;
95
96        unreachable!(
97            "Earlier check of `enabled_backend_features` should have prevented getting here!"
98        );
99    }
100
101    /// Returns which backends can be picked for the current build configuration.
102    ///
103    /// The returned set depends on a combination of target platform and enabled features.
104    /// This does *not* do any runtime checks and is exclusively based on compile time information.
105    ///
106    /// `InstanceDescriptor::backends` does not need to be a subset of this,
107    /// but any backend that is not in this set, will not be picked.
108    pub const fn enabled_backend_features() -> Backends {
109        let mut backends = Backends::empty();
110        // `.set` and `|=` don't work in a `const` context.
111        if cfg!(noop) {
112            backends = backends.union(Backends::NOOP);
113        }
114        if cfg!(vulkan) {
115            backends = backends.union(Backends::VULKAN);
116        }
117        if cfg!(any(gles, webgl)) {
118            backends = backends.union(Backends::GL);
119        }
120        if cfg!(metal) {
121            backends = backends.union(Backends::METAL);
122        }
123        if cfg!(dx12) {
124            backends = backends.union(Backends::DX12);
125        }
126        if cfg!(webgpu) {
127            backends = backends.union(Backends::BROWSER_WEBGPU);
128        }
129        backends
130    }
131
132    /// Returns the set of [WGSL language extensions] supported by this instance.
133    ///
134    /// [WGSL language extensions]: https://www.w3.org/TR/webgpu/#gpuwgsllanguagefeatures
135    #[cfg(feature = "wgsl")]
136    pub fn wgsl_language_features(&self) -> WgslLanguageFeatures {
137        self.inner.wgsl_language_features()
138    }
139
140    /// Retrieves all available [`Adapter`]s that match the given [`Backends`].
141    ///
142    /// # Arguments
143    ///
144    /// - `backends` - Backends from which to enumerate adapters.
145    pub fn enumerate_adapters(&self, backends: Backends) -> impl Future<Output = Vec<Adapter>> {
146        let future = self.inner.enumerate_adapters(backends);
147
148        async move {
149            future
150                .await
151                .iter()
152                .map(|adapter| Adapter {
153                    inner: adapter.clone(),
154                })
155                .collect()
156        }
157    }
158
159    /// Retrieves an [`Adapter`] which matches the given [`RequestAdapterOptions`].
160    ///
161    /// Some options are "soft", so treated as non-mandatory. Others are "hard".
162    ///
163    /// If no adapters are found that satisfy all the "hard" options, an error is returned.
164    ///
165    /// When targeting WebGL2, a [`compatible_surface`](RequestAdapterOptions::compatible_surface)
166    /// must be specified; using `RequestAdapterOptions::default()` will not succeed.
167    pub fn request_adapter(
168        &self,
169        options: &RequestAdapterOptions<'_, '_>,
170    ) -> impl Future<Output = Result<Adapter, RequestAdapterError>> + WasmNotSend {
171        let future = self.inner.request_adapter(options);
172        async move { future.await.map(|adapter| Adapter { inner: adapter }) }
173    }
174
175    /// Creates a new surface targeting a given window/canvas/surface/etc..
176    ///
177    /// Internally, this creates surfaces for all backends that are enabled for this instance.
178    ///
179    /// See [`SurfaceTarget`] for what targets are supported.
180    /// See [`Instance::create_surface_unsafe()`] for surface creation with unsafe target variants.
181    ///
182    /// Most commonly used are window handles (or provider of windows handles)
183    /// which can be passed directly as they're automatically converted to [`SurfaceTarget`].
184    pub fn create_surface<'window>(
185        &self,
186        target: impl Into<SurfaceTarget<'window>>,
187    ) -> Result<Surface<'window>, CreateSurfaceError> {
188        // Handle origin (i.e. window) to optionally take ownership of to make the surface outlast the window.
189        let handle_source;
190
191        let target = target.into();
192        let mut surface = match target {
193            SurfaceTarget::Window(window) => unsafe {
194                let surface = self.create_surface_unsafe(
195                    SurfaceTargetUnsafe::from_window(&window).map_err(|e| CreateSurfaceError {
196                        inner: CreateSurfaceErrorKind::RawHandle(e),
197                    })?,
198                );
199                handle_source = Some(window);
200
201                surface
202            }?,
203            SurfaceTarget::DisplayAndWindow(display_and_window_handle) => unsafe {
204                let surface = self.create_surface_unsafe(
205                    SurfaceTargetUnsafe::from_display_and_window(
206                        &display_and_window_handle,
207                        &display_and_window_handle,
208                    )
209                    .map_err(|e| CreateSurfaceError {
210                        inner: CreateSurfaceErrorKind::RawHandle(e),
211                    })?,
212                );
213                handle_source = Some(display_and_window_handle);
214
215                surface
216            }?,
217            #[cfg(web)]
218            SurfaceTarget::Canvas(canvas) => {
219                handle_source = None;
220
221                let value: &wasm_bindgen::JsValue = &canvas;
222                let obj = core::ptr::NonNull::from(value).cast();
223                let raw_window_handle = raw_window_handle::WebCanvasWindowHandle::new(obj).into();
224                let raw_display_handle = raw_window_handle::WebDisplayHandle::new().into();
225
226                // Note that we need to call this while we still have `value` around.
227                // This is safe without storing canvas to `handle_origin` since the surface will create a copy internally.
228                unsafe {
229                    self.create_surface_unsafe(SurfaceTargetUnsafe::RawHandle {
230                        raw_display_handle: Some(raw_display_handle),
231                        raw_window_handle,
232                    })
233                }?
234            }
235            #[cfg(web)]
236            SurfaceTarget::OffscreenCanvas(canvas) => {
237                handle_source = None;
238
239                let value: &wasm_bindgen::JsValue = &canvas;
240                let obj = core::ptr::NonNull::from(value).cast();
241                let raw_window_handle =
242                    raw_window_handle::WebOffscreenCanvasWindowHandle::new(obj).into();
243                let raw_display_handle = raw_window_handle::WebDisplayHandle::new().into();
244
245                // Note that we need to call this while we still have `value` around.
246                // This is safe without storing canvas to `handle_origin` since the surface will create a copy internally.
247                unsafe {
248                    self.create_surface_unsafe(SurfaceTargetUnsafe::RawHandle {
249                        raw_display_handle: Some(raw_display_handle),
250                        raw_window_handle,
251                    })
252                }?
253            }
254        };
255
256        surface._handle_source = handle_source;
257
258        Ok(surface)
259    }
260
261    /// Creates a new surface targeting a given window/canvas/surface/etc. using an unsafe target.
262    ///
263    /// Internally, this creates surfaces for all backends that are enabled for this instance.
264    ///
265    /// See [`SurfaceTargetUnsafe`] for what targets are supported.
266    /// See [`Instance::create_surface`] for surface creation with safe target variants.
267    ///
268    /// # Safety
269    ///
270    /// - See respective [`SurfaceTargetUnsafe`] variants for safety requirements.
271    pub unsafe fn create_surface_unsafe<'window>(
272        &self,
273        target: SurfaceTargetUnsafe,
274    ) -> Result<Surface<'window>, CreateSurfaceError> {
275        let surface = unsafe { self.inner.create_surface(target)? };
276
277        Ok(Surface {
278            _handle_source: None,
279            inner: surface,
280            config: Mutex::new(None),
281        })
282    }
283
284    /// Polls all devices.
285    ///
286    /// If `force_wait` is true and this is not running on the web, then this
287    /// function will block until all in-flight buffers have been mapped and
288    /// all submitted commands have finished execution.
289    ///
290    /// Return `true` if all devices' queues are empty, or `false` if there are
291    /// queue submissions still in flight. (Note that, unless access to all
292    /// [`Queue`s] associated with this [`Instance`] is coordinated somehow,
293    /// this information could be out of date by the time the caller receives
294    /// it. `Queue`s can be shared between threads, and other threads could
295    /// submit new work at any time.)
296    ///
297    /// On the web, this is a no-op. `Device`s are automatically polled.
298    ///
299    /// [`Queue`s]: Queue
300    pub fn poll_all(&self, force_wait: bool) -> bool {
301        self.inner.poll_all_devices(force_wait)
302    }
303
304    /// Generates memory report.
305    ///
306    /// Returns `None` if the feature is not supported by the backend
307    /// which happens only when WebGPU is pre-selected by the instance creation.
308    #[cfg(wgpu_core)]
309    pub fn generate_report(&self) -> Option<wgc::global::GlobalReport> {
310        self.inner.as_core_opt().map(|ctx| ctx.generate_report())
311    }
312}
313
314/// Interop with wgpu-hal.
315#[cfg(wgpu_core)]
316impl Instance {
317    /// Create an new instance of wgpu from a wgpu-hal instance. This is often useful
318    /// when you need to do backend specific logic, or interop with an existing backend
319    /// instance.
320    ///
321    /// # Types
322    ///
323    /// The type of `A::Instance` depends on the backend:
324    ///
325    #[doc = crate::macros::hal_type_vulkan!("Instance")]
326    #[doc = crate::macros::hal_type_metal!("Instance")]
327    #[doc = crate::macros::hal_type_dx12!("Instance")]
328    #[doc = crate::macros::hal_type_gles!("Instance")]
329    ///
330    /// # Safety
331    ///
332    /// - The `hal_instance` must be a valid and usable instance of the backend specified by `A`.
333    /// - wgpu will act like it has complete ownership of this instance, and will destroy it
334    ///   when the last reference to the instance, internal or external, is dropped.
335    pub unsafe fn from_hal<A: hal::Api>(hal_instance: A::Instance) -> Self {
336        Self {
337            inner: unsafe {
338                crate::backend::ContextWgpuCore::from_hal_instance::<A>(hal_instance).into()
339            },
340        }
341    }
342
343    /// Get the [`wgpu_hal`] instance from this `Instance`.
344    ///
345    /// Find the Api struct corresponding to the active backend in [`wgpu_hal::api`],
346    /// and pass that struct to the to the `A` type parameter.
347    ///
348    /// Returns a guard that dereferences to the type of the hal backend
349    /// which implements [`A::Instance`].
350    ///
351    /// # Types
352    ///
353    #[doc = crate::macros::hal_type_vulkan!("Instance")]
354    #[doc = crate::macros::hal_type_metal!("Instance")]
355    #[doc = crate::macros::hal_type_dx12!("Instance")]
356    #[doc = crate::macros::hal_type_gles!("Instance")]
357    ///
358    /// # Errors
359    ///
360    /// This method will return None if:
361    /// - The instance is not from the backend specified by `A`.
362    /// - The instance is from the `webgpu` or `custom` backend.
363    ///
364    /// # Safety
365    ///
366    /// - The returned resource must not be destroyed unless the guard
367    ///   is the last reference to it and it is not in use by the GPU.
368    ///   The guard and handle may be dropped at any time however.
369    /// - All the safety requirements of wgpu-hal must be upheld.
370    ///
371    /// [`A::Instance`]: hal::Api::Instance
372    pub unsafe fn as_hal<A: hal::Api>(&self) -> Option<&A::Instance> {
373        self.inner
374            .as_core_opt()
375            .and_then(|ctx| unsafe { ctx.instance_as_hal::<A>() })
376    }
377
378    /// Converts a wgpu-hal [`hal::ExposedAdapter`] to a wgpu [`Adapter`].
379    ///
380    /// # Types
381    ///
382    /// The type of `hal_adapter.adapter` depends on the backend:
383    ///
384    #[doc = crate::macros::hal_type_vulkan!("Adapter")]
385    #[doc = crate::macros::hal_type_metal!("Adapter")]
386    #[doc = crate::macros::hal_type_dx12!("Adapter")]
387    #[doc = crate::macros::hal_type_gles!("Adapter")]
388    ///
389    /// # Safety
390    ///
391    /// `hal_adapter` must be created from this instance internal handle.
392    pub unsafe fn create_adapter_from_hal<A: hal::Api>(
393        &self,
394        hal_adapter: hal::ExposedAdapter<A>,
395    ) -> Adapter {
396        let core_instance = self.inner.as_core();
397        let adapter = unsafe { core_instance.create_adapter_from_hal(hal_adapter) };
398        let core = backend::wgpu_core::CoreAdapter {
399            context: core_instance.clone(),
400            id: adapter,
401        };
402
403        Adapter { inner: core.into() }
404    }
405}
406
407/// Interop with wgpu-core.
408#[cfg(wgpu_core)]
409impl Instance {
410    /// Create an new instance of wgpu from a wgpu-core instance.
411    ///
412    /// # Arguments
413    ///
414    /// - `core_instance` - wgpu-core instance.
415    ///
416    /// # Safety
417    ///
418    /// Refer to the creation of wgpu-core Instance.
419    pub unsafe fn from_core(core_instance: wgc::instance::Instance) -> Self {
420        Self {
421            inner: unsafe {
422                crate::backend::ContextWgpuCore::from_core_instance(core_instance).into()
423            },
424        }
425    }
426}
427
428/// Interop with custom backends.
429#[cfg(custom)]
430impl Instance {
431    /// Creates instance from custom context implementation
432    pub fn from_custom<T: InstanceInterface>(instance: T) -> Self {
433        Self {
434            inner: dispatch::DispatchInstance::Custom(backend::custom::DynContext::new(instance)),
435        }
436    }
437
438    #[cfg(custom)]
439    /// Returns custom implementation of Instance (if custom backend and is internally T)
440    pub fn as_custom<T: custom::InstanceInterface>(&self) -> Option<&T> {
441        self.inner.as_custom()
442    }
443}