1#![doc = document_features::document_features!()]
7#![no_std]
10#![cfg_attr(
12 all(
13 not(all(feature = "vulkan", not(target_family = "wasm"))),
14 not(all(feature = "metal", any(target_vendor = "apple"))),
15 not(all(feature = "dx12", windows)),
16 not(feature = "gles"),
17 ),
18 allow(unused, clippy::let_and_return)
19)]
20#![cfg_attr(docsrs, feature(doc_cfg))]
21#![allow(
22 clippy::bool_assert_comparison,
24 clippy::match_like_matches_macro,
26 clippy::redundant_pattern_matching,
28 clippy::needless_lifetimes,
30 clippy::new_without_default,
32 clippy::needless_update,
34 clippy::too_many_arguments,
36 clippy::pattern_type_mismatch,
38 rustdoc::private_intra_doc_links,
40)]
41#![expect(missing_debug_implementations, reason = "TODO")]
42#![warn(
43 clippy::alloc_instead_of_core,
44 clippy::ptr_as_ptr,
45 clippy::std_instead_of_alloc,
46 clippy::std_instead_of_core,
47 trivial_casts,
48 trivial_numeric_casts,
49 unsafe_op_in_unsafe_fn,
50 unused_extern_crates,
51 unused_qualifications
52)]
53#![cfg_attr(not(send_sync), allow(clippy::arc_with_non_send_sync))]
62
63extern crate alloc;
64extern crate naga_types as nt;
65#[cfg(any(feature = "std", test))]
66extern crate std;
67extern crate wgpu_hal as hal;
68extern crate wgpu_types as wgt;
69
70mod as_hal;
71pub mod binding_model;
72pub mod command;
73mod conv;
74pub mod device;
75pub mod error;
76pub mod id;
77mod indirect_validation;
78mod init_tracker;
79pub mod instance;
80pub mod limits;
81mod lock;
82pub mod pipeline;
83mod pipeline_cache;
84mod pool;
85pub mod present;
86pub mod ray_tracing;
87pub mod ray_tracing_pipeline;
88pub mod resource;
89mod snatch;
90pub mod storage;
91mod timestamp_normalization;
92mod track;
93mod weak_vec;
94mod scratch;
99pub mod validation;
100
101pub use validation::{map_storage_format_from_naga, map_storage_format_to_naga};
102
103pub use hal::{api, MAX_BIND_GROUPS, MAX_COLOR_ATTACHMENTS, MAX_VERTEX_BUFFERS};
104pub use naga;
105
106use alloc::{
107 borrow::{Cow, ToOwned as _},
108 string::String,
109};
110
111pub(crate) use nt::{FastHashMap, FastHashSet, FastIndexMap};
112
113pub type SubmissionIndex = hal::FenceValue;
117
118pub type RawString = *const core::ffi::c_char;
119pub type Label<'a> = Option<Cow<'a, str>>;
120
121pub trait LabelHelpers<'a> {
122 fn to_hal(&'a self, flags: wgt::InstanceFlags) -> Option<&'a str>;
123 fn to_string(&self) -> String;
124}
125impl<'a> LabelHelpers<'a> for Label<'a> {
126 fn to_hal(&'a self, flags: wgt::InstanceFlags) -> Option<&'a str> {
127 if flags.contains(wgt::InstanceFlags::DISCARD_HAL_LABELS) {
128 return None;
129 }
130
131 self.as_deref()
132 }
133 fn to_string(&self) -> String {
134 self.as_deref().map(str::to_owned).unwrap_or_default()
135 }
136}
137
138pub fn hal_label<T: AsRef<str>>(opt: Option<T>, flags: wgt::InstanceFlags) -> Option<T> {
139 if flags.contains(wgt::InstanceFlags::DISCARD_HAL_LABELS) {
140 return None;
141 }
142
143 opt
144}
145
146const DOWNLEVEL_WARNING_MESSAGE: &str = concat!(
147 "The underlying API or device in use does not ",
148 "support enough features to be a fully compliant implementation of WebGPU. ",
149 "A subset of the features can still be used. ",
150 "If you are running this program on native and not in a browser and wish to limit ",
151 "the features you use to the supported subset, ",
152 "call Adapter::downlevel_properties or Device::downlevel_properties to get ",
153 "a listing of the features the current ",
154 "platform supports."
155);
156
157const DOWNLEVEL_ERROR_MESSAGE: &str = concat!(
158 "This is not an invalid use of WebGPU: the underlying API or device does not ",
159 "support enough features to be a fully compliant implementation. ",
160 "A subset of the features can still be used. ",
161 "If you are running this program on native and not in a browser ",
162 "and wish to work around this issue, call ",
163 "Adapter::downlevel_properties or Device::downlevel_properties ",
164 "to get a listing of the features the current platform supports."
165);
166
167#[cfg(feature = "api_log_info")]
168macro_rules! api_log {
169 ($($arg:tt)+) => (log::info!($($arg)+))
170}
171#[cfg(not(feature = "api_log_info"))]
172macro_rules! api_log {
173 ($($arg:tt)+) => (log::trace!($($arg)+))
174}
175
176#[cfg(feature = "api_log_info")]
177macro_rules! api_log_debug {
178 ($($arg:tt)+) => (log::info!($($arg)+))
179}
180#[cfg(not(feature = "api_log_info"))]
181macro_rules! api_log_debug {
182 ($($arg:tt)+) => (log::debug!($($arg)+))
183}
184
185pub(crate) use api_log;
186pub(crate) use api_log_debug;
187
188#[cfg(feature = "resource_log_info")]
189macro_rules! resource_log {
190 ($($arg:tt)+) => (log::info!($($arg)+))
191}
192#[cfg(not(feature = "resource_log_info"))]
193macro_rules! resource_log {
194 ($($arg:tt)+) => (log::trace!($($arg)+))
195}
196pub(crate) use resource_log;
197
198#[inline]
199pub(crate) fn get_lowest_common_denom(a: u32, b: u32) -> u32 {
200 let gcd = if a >= b {
201 get_greatest_common_divisor(a, b)
202 } else {
203 get_greatest_common_divisor(b, a)
204 };
205 a * b / gcd
206}
207
208#[inline]
209pub(crate) fn get_greatest_common_divisor(mut a: u32, mut b: u32) -> u32 {
210 assert!(a >= b);
211 loop {
212 let c = a % b;
213 if c == 0 {
214 return b;
215 } else {
216 a = b;
217 b = c;
218 }
219 }
220}
221
222#[cfg(test)]
223mod tests {
224 use super::*;
225
226 #[test]
227 fn test_lcd() {
228 assert_eq!(get_lowest_common_denom(2, 2), 2);
229 assert_eq!(get_lowest_common_denom(2, 3), 6);
230 assert_eq!(get_lowest_common_denom(6, 4), 12);
231 }
232
233 #[test]
234 fn test_gcd() {
235 assert_eq!(get_greatest_common_divisor(5, 1), 1);
236 assert_eq!(get_greatest_common_divisor(4, 2), 2);
237 assert_eq!(get_greatest_common_divisor(6, 4), 2);
238 assert_eq!(get_greatest_common_divisor(7, 7), 7);
239 }
240}