Skip to content

Commit ccae470

Browse files
authored
configure_surface needs to be on the main thread on iOS (#12055)
# Objective - Bevy fails to change screen orientation on iOS ``` Main Thread Checker: UI API called on a background thread: -[UIView layer] PID: 37669, TID: 13872050, Thread name: Compute Task Pool (1), Queue name: com.apple.root.default-qos.overcommit, QoS: 0 Backtrace: 4 bevy_mobile_example 0x0000000102cf92b8 _ZN60_$LT$$LP$$RP$$u20$as$u20$objc..message..MessageArguments$GT$6invoke17h8944e3d8ee34f15fE + 64 5 bevy_mobile_example 0x0000000102c46358 _ZN4objc7message8platform15send_unverified17h667844cebe2d7931E + 132 6 bevy_mobile_example 0x0000000102bcbd6c _ZN8wgpu_hal5metal7surface100_$LT$impl$u20$wgpu_hal..Surface$LT$wgpu_hal..metal..Api$GT$$u20$for$u20$wgpu_hal..metal..Surface$GT$9configure17h8a6af0f24cec1328E + 1548 7 bevy_mobile_example 0x000000010279be50 _ZN9wgpu_core6device6global52_$LT$impl$u20$wgpu_core..global..Global$LT$G$GT$$GT$17surface_configure17h52709bbb3b3f0ff1E + 2792 8 bevy_mobile_example 0x000000010287aacc _ZN84_$LT$wgpu..backend..wgpu_core..ContextWgpuCore$u20$as$u20$wgpu..context..Context$GT$17surface_configure17h54077b9f040286a4E + 508 9 bevy_mobile_example 0x00000001028904b4 _ZN47_$LT$T$u20$as$u20$wgpu..context..DynContext$GT$17surface_configure17hfd6a0ac5a67a8f02E + 256 10 bevy_mobile_example 0x00000001028a1870 _ZN4wgpu7Surface9configure17h97bf7dbd54220473E + 148 11 bevy_mobile_example 0x0000000101fdc7cc _ZN11bevy_render8renderer13render_device12RenderDevice17configure_surface17h6853eab840b53e07E + 56 12 bevy_mobile_example 0x000000010228eb64 _ZN11bevy_render4view6window15prepare_windows17hf6f8b3c93ba189b8E + 3248 13 bevy_mobile_example 0x0000000102169eb8 _ZN4core3ops8function5FnMut8call_mut17h53ae762930afec98E + 192 14 bevy_mobile_example 0x0000000101e46a80 _ZN4core3ops8function5impls79_$LT$impl$u20$core..ops..function..FnMut$LT$A$GT$$u20$for$u20$$RF$mut$u20$F$GT$8call_mut17h5789c37c5983ce4cE + 208 15 bevy_mobile_example 0x0000000101e936e4 _ZN152_$LT$Func$u20$as$u20$bevy_ecs..system..function_system..SystemParamFunction$LT$fn$LP$F0$C$F1$C$F2$C$F3$C$F4$C$F5$C$F6$C$F7$RP$$u20$.$GT$$u20$Out$GT$$GT$3run10call_inner17h4ea44d3456146151E + 220 16 bevy_mobile_example 0x0000000101e4683c _ZN152_$LT$Func$u20$as$u20$bevy_ecs..system..function_system..SystemParamFunction$LT$fn$LP$F0$C$F1$C$F2$C$F3$C$F4$C$F5$C$F6$C$F7$RP$$u20$.$GT$$u20$Out$GT$$GT$3run17h6515ba9e61bb4d59E + 204 17 bevy_mobile_example 0x0000000101e7f99c _ZN120_$LT$bevy_ecs..system..function_system..FunctionSystem$LT$Marker$C$F$GT$$u20$as$u20$bevy_ecs..system..system..System$GT$10run_unsafe17h78999ea2add1da26E + 212 18 bevy_mobile_example 0x0000000103b4ef60 _ZN8bevy_ecs8schedule8executor14multi_threaded21MultiThreadedExecutor17spawn_system_task28_$u7b$$u7b$closure$u7d$$u7d$28_$u7b$$u7b$closure$u7d$$u7d$17hb2572f7968d8618eE + 48 19 bevy_mobile_example 0x0000000103b5bc9c _ZN4core3ops8function6FnOnce9call_once17h4cfa9d5c488566d4E + 16 20 bevy_mobile_example 0x0000000103b2d58c _ZN115_$LT$core..panic..unwind_safe..AssertUnwindSafe$LT$F$GT$$u20$as$u20$core..ops..function..FnOnce$LT$$LP$$RP$$GT$$GT$9call_once17he61d5557ff370a2cE + 40 21 bevy_mobile_example 0x0000000103b34548 _ZN3std9panicking3try7do_call17hb9ad087e1a06eb39E + 72 22 bevy_mobile_example 0x0000000103b351bc __rust_try + 32 23 bevy_mobile_example 0x0000000103b33a30 _ZN3std9panicking3try17hdebf82084f4342b0E + 76 24 bevy_mobile_example 0x0000000103c4aedc _ZN3std5panic12catch_unwind17h7e60b22a0a18032eE + 12 25 bevy_mobile_example 0x0000000103b4ea78 _ZN8bevy_ecs8schedule8executor14multi_threaded21MultiThreadedExecutor17spawn_system_task28_$u7b$$u7b$closure$u7d$$u7d$17h1af950387501b795E + 148 26 bevy_mobile_example 0x0000000103b2cfa0 _ZN100_$LT$core..panic..unwind_safe..AssertUnwindSafe$LT$F$GT$$u20$as$u20$core..future..future..Future$GT$4poll17h1258e4bf3dbe2fd8E + 48 ``` ## Solution - run surface configuration on the main thread on iOS ## Migration Guide System `need_new_surfaces` has been renamed `need_surface_configuration` and now also configure the surfaces on window creation or resizing
1 parent 080f280 commit ccae470

File tree

1 file changed

+76
-49
lines changed
  • crates/bevy_render/src/view/window

1 file changed

+76
-49
lines changed

crates/bevy_render/src/view/window/mod.rs

Lines changed: 76 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ use std::{
1717
sync::PoisonError,
1818
};
1919
use wgpu::{
20-
BufferUsages, SurfaceTargetUnsafe, TextureFormat, TextureUsages, TextureViewDescriptor,
20+
BufferUsages, SurfaceConfiguration, SurfaceTargetUnsafe, TextureFormat, TextureUsages,
21+
TextureViewDescriptor,
2122
};
2223

2324
pub mod screenshot;
@@ -42,7 +43,7 @@ impl Plugin for WindowRenderPlugin {
4243
.add_systems(
4344
Render,
4445
create_surfaces
45-
.run_if(need_new_surfaces)
46+
.run_if(need_surface_configuration)
4647
.before(prepare_windows),
4748
)
4849
.add_systems(Render, prepare_windows.in_set(RenderSet::ManageViews));
@@ -198,7 +199,7 @@ fn extract_windows(
198199
struct SurfaceData {
199200
// TODO: what lifetime should this be?
200201
surface: wgpu::Surface<'static>,
201-
format: TextureFormat,
202+
configuration: SurfaceConfiguration,
202203
}
203204

204205
#[derive(Resource, Default)]
@@ -254,45 +255,12 @@ pub fn prepare_windows(
254255
continue;
255256
};
256257

257-
let surface_configuration = wgpu::SurfaceConfiguration {
258-
format: surface_data.format,
259-
width: window.physical_width,
260-
height: window.physical_height,
261-
usage: TextureUsages::RENDER_ATTACHMENT,
262-
present_mode: match window.present_mode {
263-
PresentMode::Fifo => wgpu::PresentMode::Fifo,
264-
PresentMode::FifoRelaxed => wgpu::PresentMode::FifoRelaxed,
265-
PresentMode::Mailbox => wgpu::PresentMode::Mailbox,
266-
PresentMode::Immediate => wgpu::PresentMode::Immediate,
267-
PresentMode::AutoVsync => wgpu::PresentMode::AutoVsync,
268-
PresentMode::AutoNoVsync => wgpu::PresentMode::AutoNoVsync,
269-
},
270-
// TODO: Expose this as a setting somewhere
271-
// 2 is wgpu's default/what we've been using so far.
272-
// 1 is the minimum, but may cause lower framerates due to the cpu waiting for the gpu to finish
273-
// all work for the previous frame before starting work on the next frame, which then means the gpu
274-
// has to wait for the cpu to finish to start on the next frame.
275-
desired_maximum_frame_latency: 2,
276-
alpha_mode: match window.alpha_mode {
277-
CompositeAlphaMode::Auto => wgpu::CompositeAlphaMode::Auto,
278-
CompositeAlphaMode::Opaque => wgpu::CompositeAlphaMode::Opaque,
279-
CompositeAlphaMode::PreMultiplied => wgpu::CompositeAlphaMode::PreMultiplied,
280-
CompositeAlphaMode::PostMultiplied => wgpu::CompositeAlphaMode::PostMultiplied,
281-
CompositeAlphaMode::Inherit => wgpu::CompositeAlphaMode::Inherit,
282-
},
283-
view_formats: if !surface_data.format.is_srgb() {
284-
vec![surface_data.format.add_srgb_suffix()]
285-
} else {
286-
vec![]
287-
},
288-
};
289-
290258
// This is an ugly hack to work around drivers that don't support MSAA.
291259
// This should be removed once https://github.com/bevyengine/bevy/issues/7194 lands and we're doing proper
292260
// feature detection for MSAA.
293261
// When removed, we can also remove the `.after(prepare_windows)` of `prepare_core_3d_depth_textures` and `prepare_prepass_textures`
294262
let sample_flags = render_adapter
295-
.get_texture_format_features(surface_configuration.format)
263+
.get_texture_format_features(surface_data.configuration.format)
296264
.flags;
297265

298266
if !sample_flags.sample_count_supported(msaa.samples()) {
@@ -340,7 +308,6 @@ pub fn prepare_windows(
340308

341309
let surface = &surface_data.surface;
342310
if not_already_configured || window.size_changed || window.present_mode_changed {
343-
render_device.configure_surface(surface, &surface_configuration);
344311
let frame = surface
345312
.get_current_texture()
346313
.expect("Error configuring surface");
@@ -351,7 +318,7 @@ pub fn prepare_windows(
351318
window.set_swapchain_texture(frame);
352319
}
353320
Err(wgpu::SurfaceError::Outdated) => {
354-
render_device.configure_surface(surface, &surface_configuration);
321+
render_device.configure_surface(surface, &surface_data.configuration);
355322
let frame = surface
356323
.get_current_texture()
357324
.expect("Error reconfiguring surface");
@@ -369,20 +336,20 @@ pub fn prepare_windows(
369336
}
370337
}
371338
};
372-
window.swap_chain_texture_format = Some(surface_data.format);
339+
window.swap_chain_texture_format = Some(surface_data.configuration.format);
373340

374341
if window.screenshot_func.is_some() {
375342
let texture = render_device.create_texture(&wgpu::TextureDescriptor {
376343
label: Some("screenshot-capture-rendertarget"),
377344
size: wgpu::Extent3d {
378-
width: surface_configuration.width,
379-
height: surface_configuration.height,
345+
width: surface_data.configuration.width,
346+
height: surface_data.configuration.height,
380347
depth_or_array_layers: 1,
381348
},
382349
mip_level_count: 1,
383350
sample_count: 1,
384351
dimension: wgpu::TextureDimension::D2,
385-
format: surface_configuration.format.add_srgb_suffix(),
352+
format: surface_data.configuration.format.add_srgb_suffix(),
386353
usage: TextureUsages::RENDER_ATTACHMENT
387354
| TextureUsages::COPY_SRC
388355
| TextureUsages::TEXTURE_BINDING,
@@ -394,7 +361,7 @@ pub fn prepare_windows(
394361
size: screenshot::get_aligned_size(
395362
window.physical_width,
396363
window.physical_height,
397-
surface_data.format.pixel_size() as u32,
364+
surface_data.configuration.format.pixel_size() as u32,
398365
) as u64,
399366
usage: BufferUsages::MAP_READ | BufferUsages::COPY_DST,
400367
mapped_at_creation: false,
@@ -407,7 +374,7 @@ pub fn prepare_windows(
407374
let pipeline_id = pipelines.specialize(
408375
&pipeline_cache,
409376
&screenshot_pipeline,
410-
surface_configuration.format,
377+
surface_data.configuration.format,
411378
);
412379
window.swap_chain_texture_view = Some(texture_view);
413380
window.screenshot_memory = Some(ScreenshotPreparedState {
@@ -420,12 +387,15 @@ pub fn prepare_windows(
420387
}
421388
}
422389

423-
pub fn need_new_surfaces(
390+
pub fn need_surface_configuration(
424391
windows: Res<ExtractedWindows>,
425392
window_surfaces: Res<WindowSurfaces>,
426393
) -> bool {
427394
for window in windows.windows.values() {
428-
if !window_surfaces.configured_windows.contains(&window.entity) {
395+
if !window_surfaces.configured_windows.contains(&window.entity)
396+
|| window.size_changed
397+
|| window.present_mode_changed
398+
{
429399
return true;
430400
}
431401
}
@@ -443,9 +413,10 @@ pub fn create_surfaces(
443413
mut window_surfaces: ResMut<WindowSurfaces>,
444414
render_instance: Res<RenderInstance>,
445415
render_adapter: Res<RenderAdapter>,
416+
render_device: Res<RenderDevice>,
446417
) {
447418
for window in windows.windows.values() {
448-
window_surfaces
419+
let data = window_surfaces
449420
.surfaces
450421
.entry(window.entity)
451422
.or_insert_with(|| {
@@ -477,7 +448,63 @@ pub fn create_surfaces(
477448
}
478449
}
479450

480-
SurfaceData { surface, format }
451+
let configuration = wgpu::SurfaceConfiguration {
452+
format,
453+
width: window.physical_width,
454+
height: window.physical_height,
455+
usage: TextureUsages::RENDER_ATTACHMENT,
456+
present_mode: match window.present_mode {
457+
PresentMode::Fifo => wgpu::PresentMode::Fifo,
458+
PresentMode::FifoRelaxed => wgpu::PresentMode::FifoRelaxed,
459+
PresentMode::Mailbox => wgpu::PresentMode::Mailbox,
460+
PresentMode::Immediate => wgpu::PresentMode::Immediate,
461+
PresentMode::AutoVsync => wgpu::PresentMode::AutoVsync,
462+
PresentMode::AutoNoVsync => wgpu::PresentMode::AutoNoVsync,
463+
},
464+
// TODO: Expose this as a setting somewhere
465+
// 2 is wgpu's default/what we've been using so far.
466+
// 1 is the minimum, but may cause lower framerates due to the cpu waiting for the gpu to finish
467+
// all work for the previous frame before starting work on the next frame, which then means the gpu
468+
// has to wait for the cpu to finish to start on the next frame.
469+
desired_maximum_frame_latency: 2,
470+
alpha_mode: match window.alpha_mode {
471+
CompositeAlphaMode::Auto => wgpu::CompositeAlphaMode::Auto,
472+
CompositeAlphaMode::Opaque => wgpu::CompositeAlphaMode::Opaque,
473+
CompositeAlphaMode::PreMultiplied => {
474+
wgpu::CompositeAlphaMode::PreMultiplied
475+
}
476+
CompositeAlphaMode::PostMultiplied => {
477+
wgpu::CompositeAlphaMode::PostMultiplied
478+
}
479+
CompositeAlphaMode::Inherit => wgpu::CompositeAlphaMode::Inherit,
480+
},
481+
view_formats: if !format.is_srgb() {
482+
vec![format.add_srgb_suffix()]
483+
} else {
484+
vec![]
485+
},
486+
};
487+
488+
render_device.configure_surface(&surface, &configuration);
489+
490+
SurfaceData {
491+
surface,
492+
configuration,
493+
}
481494
});
495+
496+
if window.size_changed || window.present_mode_changed {
497+
data.configuration.width = window.physical_width;
498+
data.configuration.height = window.physical_height;
499+
data.configuration.present_mode = match window.present_mode {
500+
PresentMode::Fifo => wgpu::PresentMode::Fifo,
501+
PresentMode::FifoRelaxed => wgpu::PresentMode::FifoRelaxed,
502+
PresentMode::Mailbox => wgpu::PresentMode::Mailbox,
503+
PresentMode::Immediate => wgpu::PresentMode::Immediate,
504+
PresentMode::AutoVsync => wgpu::PresentMode::AutoVsync,
505+
PresentMode::AutoNoVsync => wgpu::PresentMode::AutoNoVsync,
506+
};
507+
render_device.configure_surface(&data.surface, &data.configuration);
508+
}
482509
}
483510
}

0 commit comments

Comments
 (0)