157157< a href ="#156 " id ="156 "> 156</ a >
158158< a href ="#157 " id ="157 "> 157</ a >
159159< 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 >
197+ < a href ="#196 " id ="196 "> 196</ a >
198+ < a href ="#197 " id ="197 "> 197</ a >
199+ < a href ="#198 " id ="198 "> 198</ a >
200+ < a href ="#199 " id ="199 "> 199</ a >
201+ < a href ="#200 " id ="200 "> 200</ a >
202+ < a href ="#201 " id ="201 "> 201</ a >
203+ < a href ="#202 " id ="202 "> 202</ a >
204+ < a href ="#203 " id ="203 "> 203</ a >
205+ < a href ="#204 " id ="204 "> 204</ a >
206+ < a href ="#205 " id ="205 "> 205</ a >
207+ < a href ="#206 " id ="206 "> 206</ a >
208+ < a href ="#207 " id ="207 "> 207</ a >
209+ < a href ="#208 " id ="208 "> 208</ a >
210+ < a href ="#209 " id ="209 "> 209</ a >
211+ < a href ="#210 " id ="210 "> 210</ a >
212+ < a href ="#211 " id ="211 "> 211</ a >
213+ < a href ="#212 " id ="212 "> 212</ a >
214+ < a href ="#213 " id ="213 "> 213</ a >
215+ < a href ="#214 " id ="214 "> 214</ a >
216+ < a href ="#215 " id ="215 "> 215</ a >
217+ < a href ="#216 " id ="216 "> 216</ a >
218+ < a href ="#217 " id ="217 "> 217</ a >
219+ < a href ="#218 " id ="218 "> 218</ a >
220+ < a href ="#219 " id ="219 "> 219</ a >
221+ < a href ="#220 " id ="220 "> 220</ a >
222+ < a href ="#221 " id ="221 "> 221</ a >
223+ < a href ="#222 " id ="222 "> 222</ a >
224+ < a href ="#223 " id ="223 "> 223</ a >
225+ < a href ="#224 " id ="224 "> 224</ a >
226+ < a href ="#225 " id ="225 "> 225</ a >
227+ < a href ="#226 " id ="226 "> 226</ a >
228+ < a href ="#227 " id ="227 "> 227</ a >
229+ < a href ="#228 " id ="228 "> 228</ a >
230+ < a href ="#229 " id ="229 "> 229</ a >
231+ < a href ="#230 " id ="230 "> 230</ a >
232+ < a href ="#231 " id ="231 "> 231</ a >
233+ < a href ="#232 " id ="232 "> 232</ a >
234+ < a href ="#233 " id ="233 "> 233</ a >
235+ < a href ="#234 " id ="234 "> 234</ a >
236+ < a href ="#235 " id ="235 "> 235</ a >
237+ < a href ="#236 " id ="236 "> 236</ a >
238+ < a href ="#237 " id ="237 "> 237</ a >
239+ < a href ="#238 " id ="238 "> 238</ a >
240+ < a href ="#239 " id ="239 "> 239</ a >
241+ < a href ="#240 " id ="240 "> 240</ a >
242+ < a href ="#241 " id ="241 "> 241</ a >
243+ < a href ="#242 " id ="242 "> 242</ a >
244+ < a href ="#243 " id ="243 "> 243</ a >
245+ < a href ="#244 " id ="244 "> 244</ a >
246+ < a href ="#245 " id ="245 "> 245</ a >
247+ < a href ="#246 " id ="246 "> 246</ a >
248+ < a href ="#247 " id ="247 "> 247</ a >
249+ < a href ="#248 " id ="248 "> 248</ a >
250+ < a href ="#249 " id ="249 "> 249</ a >
251+ < a href ="#250 " id ="250 "> 250</ a >
252+ < a href ="#251 " id ="251 "> 251</ a >
253+ < a href ="#252 " id ="252 "> 252</ a >
254+ < a href ="#253 " id ="253 "> 253</ a >
255+ < a href ="#254 " id ="254 "> 254</ a >
256+ < a href ="#255 " id ="255 "> 255</ a >
257+ < a href ="#256 " id ="256 "> 256</ a >
258+ < a href ="#257 " id ="257 "> 257</ a >
259+ < a href ="#258 " id ="258 "> 258</ a >
260+ < a href ="#259 " id ="259 "> 259</ a >
261+ < a href ="#260 " id ="260 "> 260</ a >
160262</ pre > </ div > < pre class ="rust "> < code > < span class ="attr "> #[cfg(not(target_arch = < span class ="string "> "wasm32"</ span > ))]
161263</ span > < span class ="kw "> use </ span > std::io::Write;
162264< span class ="attr "> #[cfg(target_arch = < span class ="string "> "wasm32"</ span > )]
315417 < span class ="macro "> log::info!</ span > (< span class ="string "> "Created new output target image: {:?}"</ span > , < span class ="kw-2 "> &</ span > new_image);
316418 new_image
317419}
420+
421+ < span class ="attr "> #[cfg(not(target_arch = < span class ="string "> "wasm32"</ span > ))]
422+ </ span > < span class ="doccomment "> /// If the environment variable `WGPU_ADAPTER_NAME` is set, this function will attempt to
423+ /// initialize the adapter with that name. If it is not set, it will attempt to initialize
424+ /// the adapter which supports the required features.
425+ </ span > < span class ="kw "> pub</ span > (< span class ="kw "> crate</ span > ) < span class ="kw "> async fn </ span > get_adapter_with_capabilities_or_from_env(
426+ instance: < span class ="kw-2 "> &</ span > wgpu::Instance,
427+ required_features: < span class ="kw-2 "> &</ span > wgpu::Features,
428+ required_downlevel_capabilities: < span class ="kw-2 "> &</ span > wgpu::DownlevelCapabilities,
429+ surface: < span class ="kw-2 "> &</ span > < span class ="prelude-ty "> Option</ span > << span class ="kw-2 "> &</ span > wgpu::Surface<< span class ="lifetime "> '_</ span > >>,
430+ ) -> wgpu::Adapter {
431+ < span class ="kw "> use </ span > wgpu::Backends;
432+ < span class ="kw "> if </ span > std::env::var(< span class ="string "> "WGPU_ADAPTER_NAME"</ span > ).is_ok() {
433+ < span class ="kw "> let </ span > adapter = wgpu::util::initialize_adapter_from_env_or_default(instance, < span class ="kw-2 "> *</ span > surface)
434+ .< span class ="kw "> await
435+ </ span > .expect(< span class ="string "> "No suitable GPU adapters found on the system!"</ span > );
436+
437+ < span class ="kw "> let </ span > adapter_info = adapter.get_info();
438+ < span class ="macro "> log::info!</ span > (< span class ="string "> "Using {} ({:?})"</ span > , adapter_info.name, adapter_info.backend);
439+
440+ < span class ="kw "> let </ span > adapter_features = adapter.features();
441+ < span class ="macro "> assert!</ span > (
442+ adapter_features.contains(< span class ="kw-2 "> *</ span > required_features),
443+ < span class ="string "> "Adapter does not support required features for this example: {:?}"</ span > ,
444+ < span class ="kw-2 "> *</ span > required_features - adapter_features
445+ );
446+
447+ < span class ="kw "> let </ span > downlevel_capabilities = adapter.get_downlevel_capabilities();
448+ < span class ="macro "> assert!</ span > (
449+ downlevel_capabilities.shader_model >= required_downlevel_capabilities.shader_model,
450+ < span class ="string "> "Adapter does not support the minimum shader model required to run this example: {:?}"</ span > ,
451+ required_downlevel_capabilities.shader_model
452+ );
453+ < span class ="macro "> assert!</ span > (
454+ downlevel_capabilities
455+ .flags
456+ .contains(required_downlevel_capabilities.flags),
457+ < span class ="string "> "Adapter does not support the downlevel capabilities required to run this example: {:?}"</ span > ,
458+ required_downlevel_capabilities.flags - downlevel_capabilities.flags
459+ );
460+ adapter
461+ } < span class ="kw "> else </ span > {
462+ < span class ="kw "> let </ span > adapters = instance.enumerate_adapters(Backends::all());
463+
464+ < span class ="kw "> let </ span > < span class ="kw-2 "> mut </ span > chosen_adapter = < span class ="prelude-val "> None</ span > ;
465+ < span class ="kw "> for </ span > adapter < span class ="kw "> in </ span > adapters {
466+ < span class ="kw "> if let </ span > < span class ="prelude-val "> Some</ span > (surface) = surface {
467+ < span class ="kw "> if </ span > !adapter.is_surface_supported(surface) {
468+ < span class ="kw "> continue</ span > ;
469+ }
470+ }
471+
472+ < span class ="kw "> let </ span > required_features = < span class ="kw-2 "> *</ span > required_features;
473+ < span class ="kw "> let </ span > adapter_features = adapter.features();
474+ < span class ="kw "> if </ span > !adapter_features.contains(required_features) {
475+ < span class ="kw "> continue</ span > ;
476+ } < span class ="kw "> else </ span > {
477+ chosen_adapter = < span class ="prelude-val "> Some</ span > (adapter);
478+ < span class ="kw "> break</ span > ;
479+ }
480+ }
481+
482+ chosen_adapter.expect(< span class ="string "> "No suitable GPU adapters found on the system!"</ span > )
483+ }
484+ }
485+
486+ < span class ="attr "> #[cfg(target_arch = < span class ="string "> "wasm32"</ span > )]
487+ </ span > < span class ="kw "> pub</ span > (< span class ="kw "> crate</ span > ) < span class ="kw "> async fn </ span > get_adapter_with_capabilities_or_from_env(
488+ instance: < span class ="kw-2 "> &</ span > wgpu::Instance,
489+ required_features: < span class ="kw-2 "> &</ span > wgpu::Features,
490+ required_downlevel_capabilities: < span class ="kw-2 "> &</ span > wgpu::DownlevelCapabilities,
491+ surface: < span class ="kw-2 "> &</ span > < span class ="prelude-ty "> Option</ span > << span class ="kw-2 "> &</ span > wgpu::Surface<< span class ="lifetime "> '_</ span > >>,
492+ ) -> wgpu::Adapter {
493+ < span class ="kw "> let </ span > adapter = wgpu::util::initialize_adapter_from_env_or_default(instance, < span class ="kw-2 "> *</ span > surface)
494+ .< span class ="kw "> await
495+ </ span > .expect(< span class ="string "> "No suitable GPU adapters found on the system!"</ span > );
496+
497+ < span class ="kw "> let </ span > adapter_info = adapter.get_info();
498+ < span class ="macro "> log::info!</ span > (< span class ="string "> "Using {} ({:?})"</ span > , adapter_info.name, adapter_info.backend);
499+
500+ < span class ="kw "> let </ span > adapter_features = adapter.features();
501+ < span class ="macro "> assert!</ span > (
502+ adapter_features.contains(< span class ="kw-2 "> *</ span > required_features),
503+ < span class ="string "> "Adapter does not support required features for this example: {:?}"</ span > ,
504+ < span class ="kw-2 "> *</ span > required_features - adapter_features
505+ );
506+
507+ < span class ="kw "> let </ span > downlevel_capabilities = adapter.get_downlevel_capabilities();
508+ < span class ="macro "> assert!</ span > (
509+ downlevel_capabilities.shader_model >= required_downlevel_capabilities.shader_model,
510+ < span class ="string "> "Adapter does not support the minimum shader model required to run this example: {:?}"</ span > ,
511+ required_downlevel_capabilities.shader_model
512+ );
513+ < span class ="macro "> assert!</ span > (
514+ downlevel_capabilities
515+ .flags
516+ .contains(required_downlevel_capabilities.flags),
517+ < span class ="string "> "Adapter does not support the downlevel capabilities required to run this example: {:?}"</ span > ,
518+ required_downlevel_capabilities.flags - downlevel_capabilities.flags
519+ );
520+ adapter
521+ }
318522</ code > </ pre > </ div > </ section > </ main > </ body > </ html >
0 commit comments