li_wgpu_core/
lib.rs

1/*! This library safely implements WebGPU on native platforms.
2 *  It is designed for integration into browsers, as well as wrapping
3 *  into other language-specific user-friendly libraries.
4 */
5
6// When we have no backends, we end up with a lot of dead or otherwise unreachable code.
7#![cfg_attr(
8    all(
9        not(all(feature = "vulkan", not(target_arch = "wasm32"))),
10        not(all(feature = "metal", any(target_os = "macos", target_os = "ios"))),
11        not(all(feature = "dx12", windows)),
12        not(all(feature = "dx11", windows)),
13        not(feature = "gles"),
14    ),
15    allow(unused, clippy::let_and_return)
16)]
17#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
18#![allow(
19    // It is much clearer to assert negative conditions with eq! false
20    clippy::bool_assert_comparison,
21    // We use loops for getting early-out of scope without closures.
22    clippy::never_loop,
23    // We don't use syntax sugar where it's not necessary.
24    clippy::match_like_matches_macro,
25    // Redundant matching is more explicit.
26    clippy::redundant_pattern_matching,
27    // Explicit lifetimes are often easier to reason about.
28    clippy::needless_lifetimes,
29    // No need for defaults in the internal types.
30    clippy::new_without_default,
31    // Needless updates are more scaleable, easier to play with features.
32    clippy::needless_update,
33    // Need many arguments for some core functions to be able to re-use code in many situations.
34    clippy::too_many_arguments,
35    // For some reason `rustc` can warn about these in const generics even
36    // though they are required.
37    unused_braces,
38    // Clashes with clippy::pattern_type_mismatch
39    clippy::needless_borrowed_reference,
40)]
41#![warn(
42    trivial_casts,
43    trivial_numeric_casts,
44    unsafe_op_in_unsafe_fn,
45    unused_extern_crates,
46    unused_qualifications,
47    // We don't match on a reference, unless required.
48    clippy::pattern_type_mismatch,
49)]
50
51pub mod binding_model;
52pub mod command;
53mod conv;
54pub mod device;
55pub mod error;
56pub mod global;
57pub mod hal_api;
58pub mod hub;
59pub mod id;
60pub mod identity;
61mod init_tracker;
62pub mod instance;
63pub mod pipeline;
64pub mod present;
65pub mod registry;
66pub mod resource;
67pub mod storage;
68mod track;
69mod validation;
70
71pub use hal::{api, MAX_BIND_GROUPS, MAX_COLOR_ATTACHMENTS, MAX_VERTEX_BUFFERS};
72
73use atomic::{AtomicUsize, Ordering};
74
75use std::{borrow::Cow, os::raw::c_char, ptr, sync::atomic};
76
77/// The index of a queue submission.
78///
79/// These are the values stored in `Device::fence`.
80type SubmissionIndex = hal::FenceValue;
81
82type Index = u32;
83type Epoch = u32;
84
85pub type RawString = *const c_char;
86pub type Label<'a> = Option<Cow<'a, str>>;
87
88trait LabelHelpers<'a> {
89    fn borrow_option(&'a self) -> Option<&'a str>;
90    fn to_hal(&'a self, flags: wgt::InstanceFlags) -> Option<&'a str>;
91    fn borrow_or_default(&'a self) -> &'a str;
92}
93impl<'a> LabelHelpers<'a> for Label<'a> {
94    fn borrow_option(&'a self) -> Option<&'a str> {
95        self.as_ref().map(|cow| cow.as_ref())
96    }
97    fn to_hal(&'a self, flags: wgt::InstanceFlags) -> Option<&'a str> {
98        if flags.contains(wgt::InstanceFlags::DISCARD_HAL_LABELS) {
99            return None;
100        }
101
102        self.as_ref().map(|cow| cow.as_ref())
103    }
104    fn borrow_or_default(&'a self) -> &'a str {
105        self.borrow_option().unwrap_or_default()
106    }
107}
108
109pub fn hal_label(opt: Option<&str>, flags: wgt::InstanceFlags) -> Option<&str> {
110    if flags.contains(wgt::InstanceFlags::DISCARD_HAL_LABELS) {
111        return None;
112    }
113
114    opt
115}
116
117/// Reference count object that is 1:1 with each reference.
118///
119/// All the clones of a given `RefCount` point to the same
120/// heap-allocated atomic reference count. When the count drops to
121/// zero, only the count is freed. No other automatic cleanup takes
122/// place; this is just a reference count, not a smart pointer.
123///
124/// `RefCount` values are created only by [`LifeGuard::new`] and by
125/// `Clone`, so every `RefCount` is implicitly tied to some
126/// [`LifeGuard`].
127#[derive(Debug)]
128struct RefCount(ptr::NonNull<AtomicUsize>);
129
130unsafe impl Send for RefCount {}
131unsafe impl Sync for RefCount {}
132
133impl RefCount {
134    const MAX: usize = 1 << 24;
135
136    /// Construct a new `RefCount`, with an initial count of 1.
137    fn new() -> RefCount {
138        let bx = Box::new(AtomicUsize::new(1));
139        Self(unsafe { ptr::NonNull::new_unchecked(Box::into_raw(bx)) })
140    }
141
142    fn load(&self) -> usize {
143        unsafe { self.0.as_ref() }.load(Ordering::Acquire)
144    }
145}
146
147impl Clone for RefCount {
148    fn clone(&self) -> Self {
149        let old_size = unsafe { self.0.as_ref() }.fetch_add(1, Ordering::AcqRel);
150        assert!(old_size < Self::MAX);
151        Self(self.0)
152    }
153}
154
155impl Drop for RefCount {
156    fn drop(&mut self) {
157        unsafe {
158            if self.0.as_ref().fetch_sub(1, Ordering::AcqRel) == 1 {
159                drop(Box::from_raw(self.0.as_ptr()));
160            }
161        }
162    }
163}
164
165/// Reference count object that tracks multiple references.
166/// Unlike `RefCount`, it's manually inc()/dec() called.
167#[derive(Debug)]
168struct MultiRefCount(AtomicUsize);
169
170impl MultiRefCount {
171    fn new() -> Self {
172        Self(AtomicUsize::new(1))
173    }
174
175    fn inc(&self) {
176        self.0.fetch_add(1, Ordering::AcqRel);
177    }
178
179    fn dec_and_check_empty(&self) -> bool {
180        self.0.fetch_sub(1, Ordering::AcqRel) == 1
181    }
182}
183
184/// Information needed to decide when it's safe to free some wgpu-core
185/// resource.
186///
187/// Each type representing a `wgpu-core` resource, like [`Device`],
188/// [`Buffer`], etc., contains a `LifeGuard` which indicates whether
189/// it is safe to free.
190///
191/// A resource may need to be retained for any of several reasons:
192///
193/// - The user may hold a reference to it (via a `wgpu::Buffer`, say).
194///
195/// - Other resources may depend on it (a texture view's backing
196///   texture, for example).
197///
198/// - It may be used by commands sent to the GPU that have not yet
199///   finished execution.
200///
201/// [`Device`]: device::Device
202/// [`Buffer`]: resource::Buffer
203#[derive(Debug)]
204pub struct LifeGuard {
205    /// `RefCount` for the user's reference to this resource.
206    ///
207    /// When the user first creates a `wgpu-core` resource, this `RefCount` is
208    /// created along with the resource's `LifeGuard`. When the user drops the
209    /// resource, we swap this out for `None`. Note that the resource may
210    /// still be held alive by other resources.
211    ///
212    /// Any `Stored<T>` value holds a clone of this `RefCount` along with the id
213    /// of a `T` resource.
214    ref_count: Option<RefCount>,
215
216    /// The index of the last queue submission in which the resource
217    /// was used.
218    ///
219    /// Each queue submission is fenced and assigned an index number
220    /// sequentially. Thus, when a queue submission completes, we know any
221    /// resources used in that submission and any lower-numbered submissions are
222    /// no longer in use by the GPU.
223    submission_index: AtomicUsize,
224
225    /// The `label` from the descriptor used to create the resource.
226    #[cfg(debug_assertions)]
227    pub(crate) label: String,
228}
229
230impl LifeGuard {
231    #[allow(unused_variables)]
232    fn new(label: &str) -> Self {
233        Self {
234            ref_count: Some(RefCount::new()),
235            submission_index: AtomicUsize::new(0),
236            #[cfg(debug_assertions)]
237            label: label.to_string(),
238        }
239    }
240
241    fn add_ref(&self) -> RefCount {
242        self.ref_count.clone().unwrap()
243    }
244
245    /// Record that this resource will be used by the queue submission with the
246    /// given index.
247    ///
248    /// Returns `true` if the resource is still held by the user.
249    fn use_at(&self, submit_index: SubmissionIndex) -> bool {
250        self.submission_index
251            .store(submit_index as _, Ordering::Release);
252        self.ref_count.is_some()
253    }
254
255    fn life_count(&self) -> SubmissionIndex {
256        self.submission_index.load(Ordering::Acquire) as _
257    }
258}
259
260#[derive(Clone, Debug)]
261struct Stored<T> {
262    value: id::Valid<T>,
263    ref_count: RefCount,
264}
265
266const DOWNLEVEL_WARNING_MESSAGE: &str = "The underlying API or device in use does not \
267support enough features to be a fully compliant implementation of WebGPU. A subset of the features can still be used. \
268If you are running this program on native and not in a browser and wish to limit the features you use to the supported subset, \
269call Adapter::downlevel_properties or Device::downlevel_properties to get a listing of the features the current \
270platform supports.";
271const DOWNLEVEL_ERROR_MESSAGE: &str = "This is not an invalid use of WebGPU: the underlying API or device does not \
272support enough features to be a fully compliant implementation. A subset of the features can still be used. \
273If you are running this program on native and not in a browser and wish to work around this issue, call \
274Adapter::downlevel_properties or Device::downlevel_properties to get a listing of the features the current \
275platform supports.";
276
277// #[cfg] attributes in exported macros are interesting!
278//
279// The #[cfg] conditions in a macro's expansion are evaluated using the
280// configuration options (features, target architecture and os, etc.) in force
281// where the macro is *used*, not where it is *defined*. That is, if crate A
282// defines a macro like this:
283//
284//     #[macro_export]
285//     macro_rules! if_bleep {
286//         { } => {
287//             #[cfg(feature = "bleep")]
288//             bleep();
289//         }
290//     }
291//
292// and then crate B uses it like this:
293//
294//     fn f() {
295//         if_bleep! { }
296//     }
297//
298// then it is crate B's `"bleep"` feature, not crate A's, that determines
299// whether the macro expands to a function call or an empty statement. The
300// entire configuration predicate is evaluated in the use's context, not the
301// definition's.
302//
303// Since `wgpu-core` selects back ends using features, we need to make sure the
304// arms of the `gfx_select!` macro are pruned according to `wgpu-core`'s
305// features, not those of whatever crate happens to be using `gfx_select!`. This
306// means we can't use `#[cfg]` attributes in `gfx_select!`s definition itself.
307// Instead, for each backend, `gfx_select!` must use a macro whose definition is
308// selected by `#[cfg]` in `wgpu-core`. The configuration predicate is still
309// evaluated when the macro is used; we've just moved the `#[cfg]` into a macro
310// used by `wgpu-core` itself.
311
312/// Define an exported macro named `$public` that expands to an expression if
313/// the feature `$feature` is enabled, or to a panic otherwise.
314///
315/// This is used in the definition of `gfx_select!`, to dispatch the
316/// call to the appropriate backend, but panic if that backend was not
317/// compiled in.
318///
319/// For a call like this:
320///
321/// ```ignore
322/// define_backend_caller! { name, private, "feature" if cfg_condition }
323/// ```
324///
325/// define a macro `name`, used like this:
326///
327/// ```ignore
328/// name!(expr)
329/// ```
330///
331/// that expands to `expr` if `#[cfg(cfg_condition)]` is enabled, or a
332/// panic otherwise. The panic message complains that `"feature"` is
333/// not enabled.
334///
335/// Because of odd technical limitations on exporting macros expanded
336/// by other macros, you must supply both a public-facing name for the
337/// macro and a private name, `$private`, which is never used
338/// outside this macro. For details:
339/// <https://github.com/rust-lang/rust/pull/52234#issuecomment-976702997>
340macro_rules! define_backend_caller {
341    { $public:ident, $private:ident, $feature:literal if $cfg:meta } => {
342        #[cfg($cfg)]
343        #[macro_export]
344        macro_rules! $private {
345            ( $call:expr ) => ( $call )
346        }
347
348        #[cfg(not($cfg))]
349        #[macro_export]
350        macro_rules! $private {
351            ( $call:expr ) => (
352                panic!("Identifier refers to disabled backend feature {:?}", $feature)
353            )
354        }
355
356        // See note about rust-lang#52234 above.
357        #[doc(hidden)] pub use $private as $public;
358    }
359}
360
361// Define a macro for each `gfx_select!` match arm. For example,
362//
363//     gfx_if_vulkan!(expr)
364//
365// expands to `expr` if the `"vulkan"` feature is enabled, or to a panic
366// otherwise.
367define_backend_caller! { gfx_if_vulkan, gfx_if_vulkan_hidden, "vulkan" if all(feature = "vulkan", not(target_arch = "wasm32")) }
368define_backend_caller! { gfx_if_metal, gfx_if_metal_hidden, "metal" if all(feature = "metal", any(target_os = "macos", target_os = "ios")) }
369define_backend_caller! { gfx_if_dx12, gfx_if_dx12_hidden, "dx12" if all(feature = "dx12", windows) }
370define_backend_caller! { gfx_if_dx11, gfx_if_dx11_hidden, "dx11" if all(feature = "dx11", windows) }
371define_backend_caller! { gfx_if_gles, gfx_if_gles_hidden, "gles" if feature = "gles" }
372
373/// Dispatch on an [`Id`]'s backend to a backend-generic method.
374///
375/// Uses of this macro have the form:
376///
377/// ```ignore
378///
379///     gfx_select!(id => value.method(args...))
380///
381/// ```
382///
383/// This expands to an expression that calls `value.method::<A>(args...)` for
384/// the backend `A` selected by `id`. The expansion matches on `id.backend()`,
385/// with an arm for each backend type in [`wgpu_types::Backend`] which calls the
386/// specialization of `method` for the given backend. This allows resource
387/// identifiers to select backends dynamically, even though many `wgpu_core`
388/// methods are compiled and optimized for a specific back end.
389///
390/// This macro is typically used to call methods on [`wgpu_core::global::Global`],
391/// many of which take a single `hal::Api` type parameter. For example, to
392/// create a new buffer on the device indicated by `device_id`, one would say:
393///
394/// ```ignore
395/// gfx_select!(device_id => global.device_create_buffer(device_id, ...))
396/// ```
397///
398/// where the `device_create_buffer` method is defined like this:
399///
400/// ```ignore
401/// impl<...> Global<...> {
402///    pub fn device_create_buffer<A: hal::Api>(&self, ...) -> ...
403///    { ... }
404/// }
405/// ```
406///
407/// That `gfx_select!` call uses `device_id`'s backend to select the right
408/// backend type `A` for a call to `Global::device_create_buffer<A>`.
409///
410/// However, there's nothing about this macro that is specific to `global::Global`.
411/// For example, Firefox's embedding of `wgpu_core` defines its own types with
412/// methods that take `hal::Api` type parameters. Firefox uses `gfx_select!` to
413/// dynamically dispatch to the right specialization based on the resource's id.
414///
415/// [`wgpu_types::Backend`]: wgt::Backend
416/// [`wgpu_core::global::Global`]: crate::global::Global
417/// [`Id`]: id::Id
418#[macro_export]
419macro_rules! gfx_select {
420    ($id:expr => $global:ident.$method:ident( $($param:expr),* )) => {
421        match $id.backend() {
422            wgt::Backend::Vulkan => $crate::gfx_if_vulkan!($global.$method::<$crate::api::Vulkan>( $($param),* )),
423            wgt::Backend::Metal => $crate::gfx_if_metal!($global.$method::<$crate::api::Metal>( $($param),* )),
424            wgt::Backend::Dx12 => $crate::gfx_if_dx12!($global.$method::<$crate::api::Dx12>( $($param),* )),
425            wgt::Backend::Dx11 => $crate::gfx_if_dx11!($global.$method::<$crate::api::Dx11>( $($param),* )),
426            wgt::Backend::Gl => $crate::gfx_if_gles!($global.$method::<$crate::api::Gles>( $($param),+ )),
427            other => panic!("Unexpected backend {:?}", other),
428        }
429    };
430}
431
432/// Fast hash map used internally.
433type FastHashMap<K, V> =
434    std::collections::HashMap<K, V, std::hash::BuildHasherDefault<rustc_hash::FxHasher>>;
435/// Fast hash set used internally.
436type FastHashSet<K> =
437    std::collections::HashSet<K, std::hash::BuildHasherDefault<rustc_hash::FxHasher>>;
438
439#[inline]
440pub(crate) fn get_lowest_common_denom(a: u32, b: u32) -> u32 {
441    let gcd = if a >= b {
442        get_greatest_common_divisor(a, b)
443    } else {
444        get_greatest_common_divisor(b, a)
445    };
446    a * b / gcd
447}
448
449#[inline]
450pub(crate) fn get_greatest_common_divisor(mut a: u32, mut b: u32) -> u32 {
451    assert!(a >= b);
452    loop {
453        let c = a % b;
454        if c == 0 {
455            return b;
456        } else {
457            a = b;
458            b = c;
459        }
460    }
461}
462
463#[test]
464fn test_lcd() {
465    assert_eq!(get_lowest_common_denom(2, 2), 2);
466    assert_eq!(get_lowest_common_denom(2, 3), 6);
467    assert_eq!(get_lowest_common_denom(6, 4), 12);
468}
469
470#[test]
471fn test_gcd() {
472    assert_eq!(get_greatest_common_divisor(5, 1), 1);
473    assert_eq!(get_greatest_common_divisor(4, 2), 2);
474    assert_eq!(get_greatest_common_divisor(6, 4), 2);
475    assert_eq!(get_greatest_common_divisor(7, 7), 7);
476}