@@ -7,7 +7,7 @@ use bevy_tasks::ComputeTaskPool;
7
7
use bevy_utils:: WgpuWrapper ;
8
8
pub use graph_runner:: * ;
9
9
pub use render_device:: * ;
10
- use tracing:: { debug, error, info, info_span, trace , warn} ;
10
+ use tracing:: { debug, error, info, info_span, warn} ;
11
11
12
12
use crate :: {
13
13
diagnostic:: { internal:: DiagnosticsRecorder , RecordDiagnostics } ,
@@ -145,6 +145,33 @@ const GPU_NOT_FOUND_ERROR_MESSAGE: &str = if cfg!(target_os = "linux") {
145
145
"Unable to find a GPU! Make sure you have installed required drivers!"
146
146
} ;
147
147
148
+ #[ cfg( not( target_family = "wasm" ) ) ]
149
+ fn find_adapter_by_name (
150
+ instance : & Instance ,
151
+ options : & WgpuSettings ,
152
+ compatible_surface : Option < & wgpu:: Surface < ' _ > > ,
153
+ adapter_name : & str ,
154
+ ) -> Option < Adapter > {
155
+ for adapter in
156
+ instance. enumerate_adapters ( options. backends . expect (
157
+ "The `backends` field of `WgpuSettings` must be set to use a specific adapter." ,
158
+ ) )
159
+ {
160
+ tracing:: trace!( "Checking adapter: {:?}" , adapter. get_info( ) ) ;
161
+ let info = adapter. get_info ( ) ;
162
+ if let Some ( surface) = compatible_surface {
163
+ if !adapter. is_surface_supported ( surface) {
164
+ continue ;
165
+ }
166
+ }
167
+
168
+ if info. name . eq_ignore_ascii_case ( adapter_name) {
169
+ return Some ( adapter) ;
170
+ }
171
+ }
172
+ None
173
+ }
174
+
148
175
/// Initializes the renderer by retrieving and preparing the GPU instance, device and queue
149
176
/// for the specified backend.
150
177
pub async fn initialize_renderer (
@@ -153,36 +180,30 @@ pub async fn initialize_renderer(
153
180
request_adapter_options : & RequestAdapterOptions < ' _ , ' _ > ,
154
181
desired_adapter_name : Option < String > ,
155
182
) -> ( RenderDevice , RenderQueue , RenderAdapterInfo , RenderAdapter ) {
183
+ #[ cfg( not( target_family = "wasm" ) ) ]
184
+ let mut selected_adapter = desired_adapter_name. and_then ( |adapter_name| {
185
+ find_adapter_by_name (
186
+ instance,
187
+ options,
188
+ request_adapter_options. compatible_surface ,
189
+ & adapter_name,
190
+ )
191
+ } ) ;
192
+ #[ cfg( target_family = "wasm" ) ]
156
193
let mut selected_adapter = None ;
157
- if let Some ( adapter_name) = & desired_adapter_name {
158
- debug ! ( "Searching for adapter with name: {}" , adapter_name) ;
159
- for adapter in instance. enumerate_adapters ( options. backends . expect (
160
- "The `backends` field of `WgpuSettings` must be set to use a specific adapter." ,
161
- ) ) {
162
- trace ! ( "Checking adapter: {:?}" , adapter. get_info( ) ) ;
163
- let info = adapter. get_info ( ) ;
164
- if let Some ( surface) = request_adapter_options. compatible_surface {
165
- if !adapter. is_surface_supported ( surface) {
166
- continue ;
167
- }
168
- }
169
194
170
- if info
171
- . name
172
- . to_lowercase ( )
173
- . contains ( & adapter_name. to_lowercase ( ) )
174
- {
175
- selected_adapter = Some ( adapter) ;
176
- break ;
177
- }
178
- }
179
- } else {
195
+ #[ cfg( target_family = "wasm" ) ]
196
+ if desired_adapter_name. is_some ( ) {
197
+ warn ! ( "Choosing an adapter is not supported on wasm." ) ;
198
+ }
199
+
200
+ if selected_adapter. is_none ( ) {
180
201
debug ! (
181
202
"Searching for adapter with options: {:?}" ,
182
203
request_adapter_options
183
204
) ;
184
205
selected_adapter = instance. request_adapter ( request_adapter_options) . await . ok ( ) ;
185
- } ;
206
+ }
186
207
187
208
let adapter = selected_adapter. expect ( GPU_NOT_FOUND_ERROR_MESSAGE ) ;
188
209
let adapter_info = adapter. get_info ( ) ;
0 commit comments