Skip to content

Commit b3032e0

Browse files
authored
Fix adapter forcing breaking wasm builds (#20054)
# Objective - Appease @mockersf ## Solution - Gate out enumerate_adapters usage on wasm and warn if `WGPU_FORCE_FALLBACK_ADAPTER` is somehow used.
1 parent cfb679a commit b3032e0

File tree

1 file changed

+45
-24
lines changed
  • crates/bevy_render/src/renderer

1 file changed

+45
-24
lines changed

crates/bevy_render/src/renderer/mod.rs

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use bevy_tasks::ComputeTaskPool;
77
use bevy_utils::WgpuWrapper;
88
pub use graph_runner::*;
99
pub use render_device::*;
10-
use tracing::{debug, error, info, info_span, trace, warn};
10+
use tracing::{debug, error, info, info_span, warn};
1111

1212
use crate::{
1313
diagnostic::{internal::DiagnosticsRecorder, RecordDiagnostics},
@@ -145,6 +145,33 @@ const GPU_NOT_FOUND_ERROR_MESSAGE: &str = if cfg!(target_os = "linux") {
145145
"Unable to find a GPU! Make sure you have installed required drivers!"
146146
};
147147

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+
148175
/// Initializes the renderer by retrieving and preparing the GPU instance, device and queue
149176
/// for the specified backend.
150177
pub async fn initialize_renderer(
@@ -153,36 +180,30 @@ pub async fn initialize_renderer(
153180
request_adapter_options: &RequestAdapterOptions<'_, '_>,
154181
desired_adapter_name: Option<String>,
155182
) -> (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")]
156193
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-
}
169194

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() {
180201
debug!(
181202
"Searching for adapter with options: {:?}",
182203
request_adapter_options
183204
);
184205
selected_adapter = instance.request_adapter(request_adapter_options).await.ok();
185-
};
206+
}
186207

187208
let adapter = selected_adapter.expect(GPU_NOT_FOUND_ERROR_MESSAGE);
188209
let adapter_info = adapter.get_info();

0 commit comments

Comments
 (0)