136136< a href ="#135 " id ="135 "> 135</ a >
137137< a href ="#136 " id ="136 "> 136</ a >
138138< a href ="#137 " id ="137 "> 137</ a >
139+ < a href ="#138 " id ="138 "> 138</ a >
140+ < a href ="#139 " id ="139 "> 139</ a >
141+ < a href ="#140 " id ="140 "> 140</ a >
142+ < a href ="#141 " id ="141 "> 141</ a >
143+ < a href ="#142 " id ="142 "> 142</ a >
144+ < a href ="#143 " id ="143 "> 143</ a >
145+ < a href ="#144 " id ="144 "> 144</ a >
146+ < a href ="#145 " id ="145 "> 145</ a >
147+ < a href ="#146 " id ="146 "> 146</ a >
148+ < a href ="#147 " id ="147 "> 147</ a >
149+ < a href ="#148 " id ="148 "> 148</ a >
150+ < a href ="#149 " id ="149 "> 149</ a >
151+ < a href ="#150 " id ="150 "> 150</ a >
152+ < a href ="#151 " id ="151 "> 151</ a >
153+ < a href ="#152 " id ="152 "> 152</ a >
154+ < a href ="#153 " id ="153 "> 153</ a >
155+ < a href ="#154 " id ="154 "> 154</ a >
156+ < a href ="#155 " id ="155 "> 155</ a >
157+ < a href ="#156 " id ="156 "> 156</ a >
158+ < a href ="#157 " id ="157 "> 157</ a >
159+ < a href ="#158 " id ="158 "> 158</ a >
160+ < a href ="#159 " id ="159 "> 159</ a >
161+ < a href ="#160 " id ="160 "> 160</ a >
162+ < a href ="#161 " id ="161 "> 161</ a >
163+ < a href ="#162 " id ="162 "> 162</ a >
164+ < a href ="#163 " id ="163 "> 163</ a >
165+ < a href ="#164 " id ="164 "> 164</ a >
166+ < a href ="#165 " id ="165 "> 165</ a >
167+ < a href ="#166 " id ="166 "> 166</ a >
168+ < a href ="#167 " id ="167 "> 167</ a >
169+ < a href ="#168 " id ="168 "> 168</ a >
170+ < a href ="#169 " id ="169 "> 169</ a >
171+ < a href ="#170 " id ="170 "> 170</ a >
172+ < a href ="#171 " id ="171 "> 171</ a >
173+ < a href ="#172 " id ="172 "> 172</ a >
174+ < a href ="#173 " id ="173 "> 173</ a >
175+ < a href ="#174 " id ="174 "> 174</ a >
176+ < a href ="#175 " id ="175 "> 175</ a >
177+ < a href ="#176 " id ="176 "> 176</ a >
178+ < a href ="#177 " id ="177 "> 177</ a >
179+ < a href ="#178 " id ="178 "> 178</ a >
180+ < a href ="#179 " id ="179 "> 179</ a >
181+ < a href ="#180 " id ="180 "> 180</ a >
182+ < a href ="#181 " id ="181 "> 181</ a >
183+ < a href ="#182 " id ="182 "> 182</ a >
184+ < a href ="#183 " id ="183 "> 183</ a >
185+ < a href ="#184 " id ="184 "> 184</ a >
186+ < a href ="#185 " id ="185 "> 185</ a >
187+ < a href ="#186 " id ="186 "> 186</ a >
188+ < a href ="#187 " id ="187 "> 187</ a >
189+ < a href ="#188 " id ="188 "> 188</ a >
190+ < a href ="#189 " id ="189 "> 189</ a >
191+ < a href ="#190 " id ="190 "> 190</ a >
192+ < a href ="#191 " id ="191 "> 191</ a >
193+ < a href ="#192 " id ="192 "> 192</ a >
194+ < a href ="#193 " id ="193 "> 193</ a >
195+ < a href ="#194 " id ="194 "> 194</ a >
196+ < a href ="#195 " id ="195 "> 195</ a >
139197</ pre > </ div > < pre class ="rust "> < code > < span class ="kw "> use </ span > wgt::{Backends, PowerPreference, RequestAdapterOptions};
140198
141199< span class ="kw "> use crate</ span > ::{Adapter, Instance, Surface};
273331 },
274332 )
275333}
334+
335+ < span class ="doccomment "> /// Determines whether the [`Backends::BROWSER_WEBGPU`] backend is supported.
336+ ///
337+ /// The result can only be true if this is called from the main thread or a dedicated worker.
338+ /// For convenience, this is also supported on non-wasm targets, always returning false there.
339+ </ span > < span class ="kw "> pub async fn </ span > is_browser_webgpu_supported() -> bool {
340+ < span class ="attr "> #[cfg(webgpu)]
341+ </ span > {
342+ < span class ="comment "> // In theory it should be enough to check for the presence of the `gpu` property...
343+ </ span > < span class ="kw "> let </ span > gpu = < span class ="kw "> crate</ span > ::backend::get_browser_gpu_property();
344+ < span class ="kw "> let </ span > < span class ="prelude-val "> Ok</ span > (< span class ="prelude-val "> Some</ span > (gpu)) = gpu < span class ="kw "> else </ span > {
345+ < span class ="kw "> return </ span > < span class ="bool-val "> false</ span > ;
346+ };
347+
348+ < span class ="comment "> // ...but in practice, we also have to try to create an adapter, since as of writing
349+ // Chrome on Linux has the `gpu` property but doesn't support WebGPU.
350+ </ span > < span class ="kw "> let </ span > adapter_promise = gpu.request_adapter();
351+ wasm_bindgen_futures::JsFuture::from(adapter_promise)
352+ .< span class ="kw "> await
353+ </ span > .map_or(< span class ="bool-val "> false</ span > , |adapter| {
354+ !adapter.is_undefined() && !adapter.is_null()
355+ })
356+ }
357+ < span class ="attr "> #[cfg(not(webgpu))]
358+ </ span > {
359+ < span class ="bool-val "> false
360+ </ span > }
361+ }
362+
363+ < span class ="doccomment "> /// Create an new instance of wgpu, but disabling [`Backends::BROWSER_WEBGPU`] if no WebGPU support was detected.
364+ ///
365+ /// If the instance descriptor enables [`Backends::BROWSER_WEBGPU`],
366+ /// this checks via [`is_browser_webgpu_supported`] for WebGPU support before forwarding
367+ /// the descriptor with or without [`Backends::BROWSER_WEBGPU`] respecitively to [`Instance::new`].
368+ ///
369+ /// You should prefer this method over [`Instance::new`] if you want to target WebGPU and automatically
370+ /// fall back to WebGL if WebGPU is not available.
371+ /// This is because WebGPU support has to be decided upon instance creation and [`Instance::new`]
372+ /// (being a `sync` function) can't establish WebGPU support (details see [`is_browser_webgpu_supported`]).
373+ ///
374+ /// # Panics
375+ ///
376+ /// If no backend feature for the active target platform is enabled,
377+ /// this method will panic, see [`Instance::enabled_backend_features()`].
378+ </ span > < span class ="attr "> #[allow(unused_mut)]
379+ </ span > < span class ="kw "> pub async fn </ span > new_instance_with_webgpu_detection(
380+ < span class ="kw-2 "> mut </ span > instance_desc: wgt::InstanceDescriptor,
381+ ) -> < span class ="kw "> crate</ span > ::Instance {
382+ < span class ="kw "> if </ span > instance_desc
383+ .backends
384+ .contains(wgt::Backends::BROWSER_WEBGPU)
385+ && !is_browser_webgpu_supported().< span class ="kw "> await
386+ </ span > {
387+ instance_desc.backends.remove(wgt::Backends::BROWSER_WEBGPU);
388+ }
389+
390+ < span class ="kw "> crate</ span > ::Instance::new(instance_desc)
391+ }
276392</ code > </ pre > </ div > </ section > </ main > </ body > </ html >
0 commit comments