diff --git a/native/shared/src/renderer/lazy_pipeline.rs b/native/shared/src/renderer/lazy_pipeline.rs
index 6a392474..46c73aa1 100644
--- a/native/shared/src/renderer/lazy_pipeline.rs
+++ b/native/shared/src/renderer/lazy_pipeline.rs
@@ -114,6 +114,68 @@ fn log_enabled() -> bool {
*ON.get_or_init(|| std::env::var_os("BLOOM_PIPELINE_LOG").is_some())
}
+// ------------------------------------------------------------ creating, with retries
+
+/// How often a pipeline the driver failed to compile is tried again.
+const COMPILE_ATTEMPTS: u32 = 4;
+
+/// `create` (a pipeline creation) with its errors caught: a pipeline the driver could not compile
+/// (an internal or out-of-memory error) is tried again a few times, and one that still fails, or
+/// that wgpu rejects, ends the app with the pipeline's name and wgpu's message.
+///
+/// The retries are for small devices: on a 2 GB Apple TV HD (A8) under memory pressure the
+/// system can stop Metal's shader compiler service in the middle of a compile, and the pipeline
+/// then fails with an internal error that the next attempt doesn't have. Uncaught, that error went
+/// to wgpu's default handler, which panics without saying which pipeline it was (tvOS 1.0 (13) of
+/// a game aborted at launch on App Review's Apple TV HD in a prewarm thread this way).
+#[cfg(not(target_arch = "wasm32"))]
+fn create_checked
(device: &wgpu::Device, label: Option<&str>, create: impl Fn() -> P) -> P {
+ let name = label.unwrap_or("(unlabeled)");
+ for attempt in 1..=COMPILE_ATTEMPTS {
+ // error scopes are per thread, so a prewarm thread only catches its own pipeline's errors
+ let oom = device.push_error_scope(wgpu::ErrorFilter::OutOfMemory);
+ let internal = device.push_error_scope(wgpu::ErrorFilter::Internal);
+ let validation = device.push_error_scope(wgpu::ErrorFilter::Validation);
+ let pipeline = create();
+ let rejected = resolved(validation.pop());
+ let failed = resolved(internal.pop()).or(resolved(oom.pop()));
+ if let Some(err) = rejected {
+ panic!("bloom: pipeline {name} is invalid: {err}");
+ }
+ match failed {
+ None => return pipeline,
+ Some(err) if attempt < COMPILE_ATTEMPTS => {
+ eprintln!(
+ "bloom: pipeline {name} failed to compile (attempt {attempt} of {COMPILE_ATTEMPTS}), trying again: {err}"
+ );
+ std::thread::sleep(std::time::Duration::from_millis(100 * attempt as u64));
+ }
+ Some(err) => {
+ panic!("bloom: pipeline {name} failed to compile {COMPILE_ATTEMPTS} times: {err}")
+ }
+ }
+ }
+ unreachable!()
+}
+
+/// The web: wgpu's error scopes resolve asynchronously there, so errors go to the device's handler.
+#[cfg(target_arch = "wasm32")]
+fn create_checked
(_device: &wgpu::Device, _label: Option<&str>, create: impl Fn() -> P) -> P {
+ create()
+}
+
+/// The result of an error scope's `pop()`: on native wgpu-core the future is ready at once.
+#[cfg(not(target_arch = "wasm32"))]
+fn resolved(future: impl std::future::Future