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
225 // Note that we need to call this while we still have `value` around.
226 // This is safe without storing canvas to `handle_origin` since the surface will create a copy internally.
227 unsafe {
228 self.create_surface_unsafe(SurfaceTargetUnsafe::RawHandle {
229 raw_display_handle: None,
230 raw_window_handle,
231 })
232 }?
233 }
234 #[cfg(web)]
235 SurfaceTarget::OffscreenCanvas(canvas) => {
236 handle_source = None;
237
238 let value: &wasm_bindgen::JsValue = &canvas;
239 let obj = core::ptr::NonNull::from(value).cast();
240 let raw_window_handle =
241 raw_window_handle::WebOffscreenCanvasWindowHandle::new(obj).into();
242
243 // Note that we need to call this while we still have `value` around.
244 // This is safe without storing canvas to `handle_origin` since the surface will create a copy internally.
245 unsafe {
246 self.create_surface_unsafe(SurfaceTargetUnsafe::RawHandle {
247 raw_display_handle: None,
248 raw_window_handle,
249 })
250 }?
251 }
252 };
253
254 surface._handle_source = handle_source;
255
256 Ok(surface)
257 }
258
259 /// Creates a new surface targeting a given window/canvas/surface/etc. using an unsafe target.
260 ///
261 /// Internally, this creates surfaces for all backends that are enabled for this instance.
262 ///
263 /// See [`SurfaceTargetUnsafe`] for what targets are supported.
264 /// See [`Instance::create_surface`] for surface creation with safe target variants.
265 ///
266 /// # Safety
267 ///
268 /// - See respective [`SurfaceTargetUnsafe`] variants for safety requirements.
269 pub unsafe fn create_surface_unsafe<'window>(
270 &self,
271 target: SurfaceTargetUnsafe,
272 ) -> Result<Surface<'window>, CreateSurfaceError> {
273 let surface = unsafe { self.inner.create_surface(target)? };
274
275 Ok(Surface {
276 _handle_source: None,
277 inner: surface,
278 config: Mutex::new(None),
279 })
280 }
281
282 /// Polls all devices.
283 ///
284 /// If `force_wait` is true and this is not running on the web, then this
285 /// function will block until all in-flight buffers have been mapped and
286 /// all submitted commands have finished execution.
287 ///
288 /// Return `true` if all devices' queues are empty, or `false` if there are
289 /// queue submissions still in flight. (Note that, unless access to all
290 /// [`Queue`s] associated with this [`Instance`] is coordinated somehow,
291 /// this information could be out of date by the time the caller receives
292 /// it. `Queue`s can be shared between threads, and other threads could
293 /// submit new work at any time.)
294 ///
295 /// On the web, this is a no-op. `Device`s are automatically polled.
296 ///
297 /// [`Queue`s]: Queue
298 pub fn poll_all(&self, force_wait: bool) -> bool {
299 self.inner.poll_all_devices(force_wait)
300 }
301
302 /// Generates memory report.
303 ///
304 /// Returns `None` if the feature is not supported by the backend
305 /// which happens only when WebGPU is pre-selected by the instance creation.
306 #[cfg(wgpu_core)]
307 pub fn generate_report(&self) -> Option<wgc::global::GlobalReport> {
308 self.inner.as_core_opt().map(|ctx| ctx.generate_report())
309 }
310}
311
312/// Interop with wgpu-hal.
313#[cfg(wgpu_core)]
314impl Instance {
315 /// Create an new instance of wgpu from a wgpu-hal instance. This is often useful
316 /// when you need to do backend specific logic, or interop with an existing backend
317 /// instance.
318 ///
319 /// # Types
320 ///
321 /// The type of `A::Instance` depends on the backend:
322 ///
323 #[doc = crate::macros::hal_type_vulkan!("Instance")]
324 #[doc = crate::macros::hal_type_metal!("Instance")]
325 #[doc = crate::macros::hal_type_dx12!("Instance")]
326 #[doc = crate::macros::hal_type_gles!("Instance")]
327 ///
328 /// # Safety
329 ///
330 /// - The `hal_instance` must be a valid and usable instance of the backend specified by `A`.
331 /// - wgpu will act like it has complete ownership of this instance, and will destroy it
332 /// when the last reference to the instance, internal or external, is dropped.
333 pub unsafe fn from_hal<A: hal::Api>(hal_instance: A::Instance) -> Self {
334 Self {
335 inner: unsafe {
336 crate::backend::ContextWgpuCore::from_hal_instance::<A>(hal_instance).into()
337 },
338 }
339 }
340
341 /// Get the [`wgpu_hal`] instance from this `Instance`.
342 ///
343 /// Find the Api struct corresponding to the active backend in [`wgpu_hal::api`],
344 /// and pass that struct to the to the `A` type parameter.
345 ///
346 /// Returns a guard that dereferences to the type of the hal backend
347 /// which implements [`A::Instance`].
348 ///
349 /// # Types
350 ///
351 #[doc = crate::macros::hal_type_vulkan!("Instance")]
352 #[doc = crate::macros::hal_type_metal!("Instance")]
353 #[doc = crate::macros::hal_type_dx12!("Instance")]
354 #[doc = crate::macros::hal_type_gles!("Instance")]
355 ///
356 /// # Errors
357 ///
358 /// This method will return None if:
359 /// - The instance is not from the backend specified by `A`.
360 /// - The instance is from the `webgpu` or `custom` backend.
361 ///
362 /// # Safety
363 ///
364 /// - The returned resource must not be destroyed unless the guard
365 /// is the last reference to it and it is not in use by the GPU.
366 /// The guard and handle may be dropped at any time however.
367 /// - All the safety requirements of wgpu-hal must be upheld.
368 ///
369 /// [`A::Instance`]: hal::Api::Instance
370 pub unsafe fn as_hal<A: hal::Api>(&self) -> Option<&A::Instance> {
371 self.inner
372 .as_core_opt()
373 .and_then(|ctx| unsafe { ctx.instance_as_hal::<A>() })
374 }
375
376 /// Converts a wgpu-hal [`hal::ExposedAdapter`] to a wgpu [`Adapter`].
377 ///
378 /// # Types
379 ///
380 /// The type of `hal_adapter.adapter` depends on the backend:
381 ///
382 #[doc = crate::macros::hal_type_vulkan!("Adapter")]
383 #[doc = crate::macros::hal_type_metal!("Adapter")]
384 #[doc = crate::macros::hal_type_dx12!("Adapter")]
385 #[doc = crate::macros::hal_type_gles!("Adapter")]
386 ///
387 /// # Safety
388 ///
389 /// `hal_adapter` must be created from this instance internal handle.
390 pub unsafe fn create_adapter_from_hal<A: hal::Api>(
391 &self,
392 hal_adapter: hal::ExposedAdapter<A>,
393 ) -> Adapter {
394 let core_instance = self.inner.as_core();
395 let adapter = unsafe { core_instance.create_adapter_from_hal(hal_adapter) };
396 let core = backend::wgpu_core::CoreAdapter {
397 context: core_instance.clone(),
398 id: adapter,
399 };
400
401 Adapter { inner: core.into() }
402 }
403}
404
405/// Interop with wgpu-core.
406#[cfg(wgpu_core)]
407impl Instance {
408 /// Create an new instance of wgpu from a wgpu-core instance.
409 ///
410 /// # Arguments
411 ///
412 /// - `core_instance` - wgpu-core instance.
413 ///
414 /// # Safety
415 ///
416 /// Refer to the creation of wgpu-core Instance.
417 pub unsafe fn from_core(core_instance: wgc::instance::Instance) -> Self {
418 Self {
419 inner: unsafe {
420 crate::backend::ContextWgpuCore::from_core_instance(core_instance).into()
421 },
422 }
423 }
424}
425
426/// Interop with custom backends.
427#[cfg(custom)]
428impl Instance {
429 /// Creates instance from custom context implementation
430 pub fn from_custom<T: InstanceInterface>(instance: T) -> Self {
431 Self {
432 inner: dispatch::DispatchInstance::Custom(backend::custom::DynContext::new(instance)),
433 }
434 }
435
436 #[cfg(custom)]
437 /// Returns custom implementation of Instance (if custom backend and is internally T)
438 pub fn as_custom<T: custom::InstanceInterface>(&self) -> Option<&T> {
439 self.inner.as_custom()
440 }
441}