Skip to content

The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.

fix(render): an export that the GPU stops answering ends instead of freezing, and a phone's GPU draws with the limits it has (#297, #312)

Jareere1b64a453dd9
diff --git a/src/crates/concat-export/src/lib.rs b/src/crates/concat-export/src/lib.rs
index 5c9c8675..149489d1 100644
--- a/src/crates/concat-export/src/lib.rs
+++ b/src/crates/concat-export/src/lib.rs
@@ -1193,9 +1193,21 @@ fn best_compositor() -> Result<WgpuCompositor, String> {
}
/// What an export or a preview says on a machine that offers no GPU and no
-/// software renderer either.
-const NO_RENDERER: &str = "no GPU or software renderer is available to draw with - on Linux, \
- install Mesa's Vulkan drivers (lavapipe)";
+/// software renderer either: on Linux, where the software renderer is a
+/// package away, which package.
+#[cfg(target_os = "linux")]
+const NO_RENDERER: &str = "no GPU or software renderer is available to draw with - install \
+ Mesa's Vulkan drivers (lavapipe)";
+
+/// On a phone there is nothing to install: the one GPU it has would not
+/// open, and the log says which and why (#312).
+#[cfg(any(target_os = "android", target_os = "ios"))]
+const NO_RENDERER: &str = "this device's GPU could not be used to draw with";
+
+/// Elsewhere the platform's own software renderer is always there, so
+/// this is a machine whose GPU and fallback both refused.
+#[cfg(not(any(target_os = "linux", target_os = "android", target_os = "ios")))]
+const NO_RENDERER: &str = "no GPU or software renderer is available to draw with";
/// How many times an export's device may die under it before the export
/// gives up: the first death is answered with a fresh device on the GPU,
@@ -1212,12 +1224,16 @@ const DEVICE_LOSSES: u32 = 3;
/// command takes too long, and a card shared with the window, the hardware
/// decoder and the encoder can run out of memory. Both ended the export
/// with "the GPU device was lost" on 8 GB NVIDIA cards (#223); the frames
-/// already encoded are kept and the next is drawn on the new device.
+/// already encoded are kept and the next is drawn on the new device. A
+/// device that stops answering for [`concat_render::gpu::DEVICE_WAIT`], or
+/// reports an error under a frame, counts as lost too (#297).
fn recovered(losses: u32, hdr: bool) -> Result<WgpuCompositor, String> {
if losses >= DEVICE_LOSSES {
return Err(format!(
- "the GPU device was lost {losses} times part way through the export; nothing \
- on this machine can draw it to the end"
+ "the GPU device was lost {losses} times part way through the export - reset, \
+ out of memory, or not answering for {} seconds; nothing on this machine can \
+ draw it to the end",
+ concat_render::gpu::DEVICE_WAIT.as_secs()
));
}
let fresh = if losses == 1 {
@@ -1245,11 +1261,36 @@ fn recovered(losses: u32, hdr: bool) -> Result<WgpuCompositor, String> {
fn headless<T>(draw: impl FnOnce(&mut WgpuCompositor) -> T) -> Result<T, String> {
static HEADLESS: std::sync::OnceLock<Option<std::sync::Mutex<WgpuCompositor>>> =
std::sync::OnceLock::new();
+ // A machine with nothing to draw on stays so for the process; why is
+ // in the log above this line, adapter by adapter.
let slot = HEADLESS
- .get_or_init(|| WgpuCompositor::new().map(std::sync::Mutex::new))
+ .get_or_init(|| {
+ let made = WgpuCompositor::new();
+ match &made {
+ Some(gpu) => {
+ let adapter = gpu.adapter_info();
+ log::info!(
+ "previews draw on {} ({:?}, {:?})",
+ adapter.name,
+ adapter.device_type,
+ adapter.backend
+ );
+ }
+ None => log::error!("previews have nothing to draw on: {NO_RENDERER}"),
+ }
+ made.map(std::sync::Mutex::new)
+ })
.as_ref()
.ok_or_else(|| NO_RENDERER.to_owned())?;
let mut gpu = slot.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
+ // A device that died under an earlier preview is not kept: every
+ // preview after it would be black.
+ if gpu.lost()
+ && let Some(fresh) = WgpuCompositor::new()
+ {
+ log::warn!("previews: the GPU device was lost; drawing on a fresh one");
+ *gpu = fresh;
+ }
Ok(draw(&mut gpu))
}
@@ -1277,6 +1318,15 @@ fn render_picture(
adapter.device_type,
adapter.backend
);
+ // The request was held to the document's ceiling; a phone's GPU can
+ // hold less, and a frame past it is said here rather than failed on.
+ let most = compositor.max_side();
+ if request.width > most || request.height > most {
+ return Err(format!(
+ "{}×{} is more than {} can draw; the most it takes is {most} a side",
+ request.width, request.height, adapter.name
+ ));
+ }
let BuiltTimeline {
timeline,
stills,
@@ -1325,6 +1375,8 @@ fn render_picture(
// How many devices have died under this export so far; see `recovered`.
let mut losses = 0;
+ // When progress was last said; see `progress_due`.
+ let mut said = std::time::Instant::now();
// One decoder per clip, opened at its in-point the first time the clip is
// needed and dropped the moment it leaves the playhead. Every decoder is
@@ -1496,14 +1548,29 @@ fn render_picture(
let live: Vec<ClipId> = plan.layers.iter().map(|layer| layer.clip).collect();
decoders.retain(|clip, _| live.contains(clip));
- if index % 15 == 0 {
+ if progress_due(index, said.elapsed()) {
reporter.emit(index, total_frames, "rendering");
+ said = std::time::Instant::now();
}
}
encoder.finish().map_err(|error| error.to_string())
}
+/// How many frames apart an export says how far it has got, at most.
+const PROGRESS_FRAMES: i64 = 15;
+
+/// And how long it goes without saying, at most, frame time allowing: a
+/// frame that takes seconds - a heavy stack, a software adapter, a card
+/// near its memory - shows as a bar that moves slowly rather than one that
+/// sat still for fifteen of them and looked hung (#297).
+const PROGRESS_INTERVAL: std::time::Duration = std::time::Duration::from_secs(1);
+
+/// Whether the frame at `index` is one to report, `since` the last report.
+fn progress_due(index: i64, since: std::time::Duration) -> bool {
+ index % PROGRESS_FRAMES == 0 || since >= PROGRESS_INTERVAL
+}
+
/// The shader passes for one clip at one instant: every keyed knob at its
/// value there, the rest at their settings, laid out as the uniforms the
/// shaders read. `reveal_maps` carries a title's baked per-word order
@@ -2565,6 +2632,79 @@ mod tests {
let _ = std::fs::remove_file(&source);
}
+ /// A compositor that fails a frame - one the device refused, or did not
+ /// finish within `DEVICE_WAIT` - does not hang the export or end it:
+ /// the frame is drawn again on a fresh device and the file is written
+ /// (#297). Lent dead, so its first frame is a failed one.
+ #[test]
+ fn an_export_goes_on_past_a_failed_frame() {
+ use concat_core::frame::Frame;
+ use concat_media::{EncodeOptions, Encoder, FrameSink};
+
+ let Some(mut failing) = WgpuCompositor::new() else {
+ return; // nothing to draw on here
+ };
+ // Wider than any GPU makes a texture.
+ let mut wide = Frame::transparent(1 << 17, 2);
+ wide.fill([255, 255, 255, 255]);
+ failing.render(&FramePlan {
+ layers: vec![PlannedLayer::picture(
+ concat_render::detached_clip(),
+ Arc::new(wide),
+ )],
+ ..FramePlan::empty(16, 16)
+ });
+ assert!(failing.lost(), "the refused frame failed");
+
+ let dir = std::env::temp_dir().join(format!("concat-export-failed-{}", std::process::id()));
+ let _ = std::fs::create_dir_all(&dir);
+ let source = dir.join("source.mp4");
+ let Ok(mut encoder) = Encoder::create(
+ &source,
+ 64,
+ 64,
+ FrameRate::THIRTY,
+ &EncodeOptions::default(),
+ ) else {
+ return; // no ffmpeg here
+ };
+ for _ in 0..6 {
+ encoder.write_frame(&Frame::black(64, 64)).expect("writes");
+ }
+ encoder.finish().expect("finishes");
+ let mut footage = clip("video", 0, 0.0, 0.2, 0.0);
+ footage.path = source.to_string_lossy().into_owned();
+ footage.media_width = Some(64);
+ footage.media_height = Some(64);
+ let output = dir.join("out.mp4");
+ let request = ExportRequest {
+ output: output.to_string_lossy().into_owned(),
+ width: 64,
+ height: 64,
+ rate_num: 30,
+ rate_den: 1,
+ crf: 30,
+ preset: "ultrafast".into(),
+ codec: VideoCodec::H264,
+ ten_bit: false,
+ rate_mode: RateMode::Vbr,
+ bitrate_kbps: 0,
+ color_range: concat_media::ColorRange::Limited,
+ color_space: ColorSpace::Sdr,
+ hdr: false,
+ clips: vec![footage],
+ };
+ let cancel = AtomicBool::new(false);
+ let mut progress = |_: i64, _: i64, _: &'static str| {};
+ let reporter = Reporter {
+ progress: &mut progress,
+ cancel: &cancel,
+ };
+ let result = render_on(&request, Some(failing), reporter);
+ let _ = std::fs::remove_dir_all(&dir);
+ result.expect("the export goes on on a fresh device");
+ }
+
/// A device that dies under an export is replaced: first by a fresh
/// one on the GPU, then by the software adapter; the third death ends
/// the export with a message that says how many there were.
@@ -2574,12 +2714,40 @@ mod tests {
panic!("a third loss gives up");
};
assert!(error.contains("lost 3 times"), "{error}");
+ assert!(error.contains("not answering"), "{error}");
if WgpuCompositor::new().is_some() {
let fresh = recovered(1, true).expect("a machine that draws opens another device");
assert!(!fresh.lost());
}
}
+ /// A slow export still moves: a frame that took over a second is
+ /// reported whatever its number, and a fast one every fifteen frames
+ /// as before.
+ #[test]
+ fn a_slow_export_reports_every_second() {
+ let quick = std::time::Duration::from_millis(30);
+ assert!(progress_due(0, quick));
+ assert!(!progress_due(1, quick));
+ assert!(progress_due(15, quick));
+ assert!(progress_due(1, std::time::Duration::from_millis(1200)));
+ assert!(progress_due(7, PROGRESS_INTERVAL));
+ }
+
+ /// Only Linux is told to install a software renderer: a phone has
+ /// nothing to install, and Windows and macOS have theirs built in.
+ #[test]
+ fn the_no_renderer_advice_fits_the_platform() {
+ assert_eq!(
+ NO_RENDERER.contains("lavapipe"),
+ cfg!(target_os = "linux"),
+ "{NO_RENDERER}"
+ );
+ if cfg!(any(target_os = "android", target_os = "ios")) {
+ assert!(NO_RENDERER.contains("GPU could not be used"));
+ }
+ }
+
#[test]
fn a_cross_fade_overlaps_the_incoming_clip_on_its_own_lane() {
let mut clips = vec![
diff --git a/src/crates/concat-render/src/gpu.rs b/src/crates/concat-render/src/gpu.rs
index 0ea7de8e..31593719 100644
--- a/src/crates/concat-render/src/gpu.rs
+++ b/src/crates/concat-render/src/gpu.rs
@@ -14,6 +14,8 @@
//! SDR timeline, rolled off by BT.2390 for an HDR one on an SDR screen.
//! An HDR clip is conformed to SDR as it uploads for an SDR timeline, and
//! keeps its light above white for an HDR one (`FramePlan::output`).
+//! An OpenGL ES device that cannot draw into half floats draws in eight
+//! bits with the sRGB curve instead (`WORK_FALLBACK`).
//! - Layer quads are sampled bilinearly with clamp-to-edge, as the tests'
//! CPU oracle (`crate::reference`) does.
//!
@@ -167,9 +169,27 @@ fn fs_darken(in: VsOut) -> @location(0) vec4<f32> {
/// as they arrive (see `upload`).
const WORK: wgpu::TextureFormat = wgpu::TextureFormat::Rgba16Float;
-/// What a texture in the pool costs a pixel.
+/// What a texture in the pool costs a pixel, in [`WORK`].
const WORK_BYTES: u64 = 8;
+/// The working format on a device that cannot draw and blend into
+/// [`WORK`]: an OpenGL ES phone without half-float colour buffers. Eight
+/// bits stored with the sRGB curve and read back into linear light, so a
+/// blend is still done in light and the shadows do not band as linear
+/// eight bits would. What it loses is what half floats add: light above
+/// one and colours outside Rec. 709 are clipped where they are drawn.
+const WORK_FALLBACK: wgpu::TextureFormat = wgpu::TextureFormat::Rgba8UnormSrgb;
+
+/// The longest the compositor waits on its device for one frame, or for
+/// anything else it waits on, before it takes the device for hung. A frame
+/// that has not finished in this long is not going to: an 8 GB card that
+/// has run out of memory under the window, the decoder and the encoder
+/// sat in an unbounded wait and the export never moved again (#297). The
+/// compositor is dead from then on, and the export goes on with a fresh
+/// device or ends with an error. Thirty seconds is far past the slowest
+/// frame a software adapter draws at 8K.
+pub const DEVICE_WAIT: std::time::Duration = std::time::Duration::from_secs(30);
+
/// A picture copied texel for texel into a target of the same size, through
/// the colour conversion at each end of the working space: an uploaded
/// frame into it (`fs_upload`), and a finished frame out of it into the
@@ -517,6 +537,39 @@ struct Presentable {
/// drawn, and one so a late frame never waits on either.
const PRESENT_RING: usize = 3;
+/// The limits the compositor asks its device for, given the adapter's:
+/// WebGPU's defaults wherever the adapter meets them, the adapter's own
+/// wherever it does not, and the adapter's texture sizes whatever they
+/// are. `Limits::default()` asked every adapter for WebGPU's defaults,
+/// which an OpenGL ES phone's GPU does not meet, so no device opened there
+/// and the preview said there was nothing to draw with (#312). What the
+/// adapter reports it can always open; a frame bigger than its textures
+/// is held back by [`WgpuCompositor::max_side`].
+fn limits_for(adapter: &wgpu::Limits) -> wgpu::Limits {
+ wgpu::Limits::default()
+ .or_worse_values_from(adapter)
+ .using_resolution(adapter.clone())
+}
+
+/// The working format on `adapter`: [`WORK`] where it can be drawn into,
+/// blended and filtered, as on every desktop GPU and most phones;
+/// [`WORK_FALLBACK`] on an OpenGL ES device without half-float colour
+/// buffers.
+fn work_format(adapter: &wgpu::Adapter) -> wgpu::TextureFormat {
+ let features = adapter.get_texture_format_features(WORK);
+ let needed =
+ wgpu::TextureFormatFeatureFlags::BLENDABLE | wgpu::TextureFormatFeatureFlags::FILTERABLE;
+ if features
+ .allowed_usages
+ .contains(wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING)
+ && features.flags.contains(needed)
+ {
+ WORK
+ } else {
+ WORK_FALLBACK
+ }
+}
+
/// A compositor that draws on the GPU. See the module docs.
pub struct WgpuCompositor {
device: wgpu::Device,
@@ -624,6 +677,12 @@ pub struct WgpuCompositor {
/// then answers every composite from the CPU reference instead: slower,
/// always correct, and never a panic in the middle of an export.
dead: bool,
+ /// The format everything is drawn in: [`WORK`], or [`WORK_FALLBACK`] on
+ /// a device that cannot draw into it.
+ work: wgpu::TextureFormat,
+ /// How many errors the device has reported that no scope caught, as its
+ /// handler counts them; see [`Compositor::render`].
+ uncaught: std::sync::Arc<std::sync::atomic::AtomicU64>,
}
impl WgpuCompositor {
@@ -693,17 +752,50 @@ impl WgpuCompositor {
};
choices.iter().copied().find_map(|software| {
let adapter =
- pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions {
+ match pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::HighPerformance,
force_fallback_adapter: software,
..Default::default()
- }))
- .ok()?;
+ })) {
+ Ok(adapter) => adapter,
+ Err(error) => {
+ log::info!(
+ "compositor: no {} adapter on {backends:?}: {error}",
+ if software { "software" } else { "hardware" }
+ );
+ return None;
+ }
+ };
+ let info = adapter.get_info();
let (device, queue) =
- pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor::default()))
- .ok()?;
+ match pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor {
+ label: Some("concat compositor"),
+ required_limits: limits_for(&adapter.limits()),
+ ..Default::default()
+ })) {
+ Ok(opened) => opened,
+ // Said, since the person sees only that nothing could draw:
+ // which adapter refused, on which API, and why (#312).
+ Err(error) => {
+ log::warn!(
+ "compositor: {} ({:?}, {:?}) would not open a device: {error}",
+ info.name,
+ info.device_type,
+ info.backend
+ );
+ return None;
+ }
+ };
+ let work = work_format(&adapter);
+ if work != WORK {
+ log::warn!(
+ "compositor: {} cannot draw into {WORK:?}; drawing in {work:?}, which clips \
+ light above white and colours outside Rec. 709",
+ info.name
+ );
+ }
// The reason, when it dies, in the log: see the window's device.
- let name = adapter.get_info().name;
+ let name = info.name;
let lost = name.clone();
device.set_device_lost_callback(move |reason, message| {
log::error!(
@@ -713,13 +805,20 @@ impl WgpuCompositor {
// An error no scope caught - a texture past the device's limit,
// a buffer the driver refused - is wgpu's to handle, and its
// default handler panics the thread it is on: the export's, or
- // the preview's. Logged instead, the frame it was in comes out
- // wrong and the thread goes on; the pipelines' own error scopes
- // (`run_stages`) still catch what they catch.
+ // the preview's. Logged instead, the thread goes on; the
+ // pipelines' own error scopes (`run_stages`) still catch what
+ // they catch. Counted as well, so a composite drawn under one is
+ // known to be wrong and is not handed to an encoder as a good
+ // frame (see `Compositor::render`).
+ let uncaught = std::sync::Arc::new(std::sync::atomic::AtomicU64::new(0));
+ let counted = std::sync::Arc::clone(&uncaught);
device.on_uncaptured_error(std::sync::Arc::new(move |error: wgpu::Error| {
+ counted.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
log::error!("the compositor's GPU device on {name} reported an error: {error}");
}));
- Some(Self::with_device(device, queue))
+ let mut compositor = Self::drawing_in(device, queue, work);
+ compositor.uncaught = uncaught;
+ Some(compositor)
})
}
@@ -743,12 +842,28 @@ impl WgpuCompositor {
/// shares the GPU's time with this one and nothing else, so it never
/// waits on this one's lock or evicts what this one keeps.
pub fn sibling(&self) -> Self {
- Self::with_device(self.device.clone(), self.queue.clone())
+ let mut sibling = Self::drawing_in(self.device.clone(), self.queue.clone(), self.work);
+ sibling.uncaught = std::sync::Arc::clone(&self.uncaught);
+ sibling
+ }
+
+ /// The widest or tallest frame this compositor can draw: the document's
+ /// ceiling, or the device's own texture limit where that is lower - an
+ /// OpenGL ES phone's can be 4096. A frame past it is refused before any
+ /// texture is made, rather than met with a validation error mid-export.
+ pub fn max_side(&self) -> u32 {
+ concat_core::frame::MAX_SIDE.min(self.device.limits().max_texture_dimension_2d)
}
/// Builds a compositor on a device the caller owns - the window's, so a
/// texture this draws is one the window can show.
pub fn with_device(device: wgpu::Device, queue: wgpu::Queue) -> Self {
+ Self::drawing_in(device, queue, WORK)
+ }
+
+ /// [`WgpuCompositor::with_device`], drawing in `work`: [`WORK`], or
+ /// [`WORK_FALLBACK`] where the device cannot draw into that.
+ fn drawing_in(device: wgpu::Device, queue: wgpu::Queue, work: wgpu::TextureFormat) -> Self {
// Read before the device moves into the compositor below.
let budget = pool_budget_for(device.adapter_info().device_type);
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
@@ -932,7 +1047,7 @@ impl WgpuCompositor {
entry_point: Some("fs_main"),
compilation_options: Default::default(),
targets: &[Some(wgpu::ColorTargetState {
- format: WORK,
+ format: work,
blend: Some(wgpu::BlendState { color, alpha }),
write_mask: wgpu::ColorWrites::ALL,
})],
@@ -969,7 +1084,7 @@ impl WgpuCompositor {
entry_point: Some(entry),
compilation_options: Default::default(),
targets: &[Some(wgpu::ColorTargetState {
- format: WORK,
+ format: work,
blend: Some(wgpu::BlendState {
color: wgpu::BlendComponent {
src_factor: wgpu::BlendFactor::One,
@@ -1026,7 +1141,7 @@ impl WgpuCompositor {
cache: None,
})
};
- let upload_pipeline = copy_into(WORK, "fs_upload");
+ let upload_pipeline = copy_into(work, "fs_upload");
let resolve_pipeline = copy_into(wgpu::TextureFormat::Rgba8Unorm, "fs_resolve");
let tone_mapped_pipeline =
copy_into(wgpu::TextureFormat::Rgba8Unorm, "fs_resolve_tone_mapped");
@@ -1077,7 +1192,7 @@ impl WgpuCompositor {
entry_point: Some(entry),
compilation_options: Default::default(),
targets: &[Some(wgpu::ColorTargetState {
- format: WORK,
+ format: work,
blend: None,
write_mask: wgpu::ColorWrites::ALL,
})],
@@ -1119,7 +1234,7 @@ impl WgpuCompositor {
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
- format: WORK,
+ format: work,
usage: wgpu::TextureUsages::TEXTURE_BINDING,
view_formats: &[],
})
@@ -1177,6 +1292,8 @@ impl WgpuCompositor {
std::sync::Arc::new(white)
},
dead: false,
+ work,
+ uncaught: std::sync::Arc::new(std::sync::atomic::AtomicU64::new(0)),
}
}
@@ -1189,11 +1306,25 @@ impl WgpuCompositor {
/// Waits until the device has done everything asked of it so far: how
/// a timing knows that a texture [`WgpuCompositor::render_texture`]
/// handed back is drawn, without reading it back. False when the
- /// device did not answer.
+ /// device did not answer, or not within [`DEVICE_WAIT`].
pub fn finish(&self) -> bool {
+ self.wait().is_ok()
+ }
+
+ /// Waits for everything submitted so far, for at most [`DEVICE_WAIT`]:
+ /// every wait the compositor makes, so none of them can hang an export.
+ fn wait(&self) -> Result<(), wgpu::PollError> {
self.device
- .poll(wgpu::PollType::wait_indefinitely())
- .is_ok()
+ .poll(wgpu::PollType::Wait {
+ submission_index: None,
+ timeout: Some(DEVICE_WAIT),
+ })
+ .map(drop)
+ }
+
+ /// What a pooled texture costs a pixel, in the working format.
+ fn work_bytes(&self) -> u64 {
+ u64::from(self.work.block_copy_size(None).unwrap_or(8))
}
/// The next presentable texture for this output size.
@@ -1617,7 +1748,9 @@ impl WgpuCompositor {
side: u32,
seconds: f32,
) -> Option<[f32; 4]> {
- if self.dead {
+ // The probe writes and reads half floats: on the eight-bit fallback
+ // it has nothing to say, and the caller treats it as untested.
+ if self.dead || self.work != WORK {
return None;
}
let side = side.max(1);
@@ -1640,7 +1773,7 @@ impl WgpuCompositor {
side: u32,
seconds: f32,
) -> Option<[f32; 4]> {
- if self.dead {
+ if self.dead || self.work != WORK {
return None;
}
let side = side.max(1);
@@ -1747,9 +1880,16 @@ impl WgpuCompositor {
slice.map_async(wgpu::MapMode::Read, move |result| {
let _ = mapped_tx.send(result);
});
- let waited = self.device.poll(wgpu::PollType::wait_indefinitely());
+ let waited = self.wait();
self.retire();
- if waited.is_err() || !matches!(mapped_rx.try_recv(), Ok(Ok(()))) {
+ if let Err(error) = waited {
+ // A device that does not answer a one-pixel read is not one to
+ // draw the next frame on.
+ log::error!("GPU probe: the device did not finish the pass: {error}");
+ self.dead = true;
+ return None;
+ }
+ if !matches!(mapped_rx.try_recv(), Ok(Ok(()))) {
return None;
}
let data = slice.get_mapped_range().ok()?;
@@ -1883,7 +2023,7 @@ impl WgpuCompositor {
.get_mut(&key)
.expect("found above")
.swap_remove(slot);
- self.pool_bytes -= u64::from(key.0) * u64::from(key.1) * WORK_BYTES;
+ self.pool_bytes -= u64::from(key.0) * u64::from(key.1) * self.work_bytes();
self.pool_textures -= 1;
}
}
@@ -1910,7 +2050,7 @@ impl WgpuCompositor {
for key in doomed {
if let Some(pool) = self.pool.remove(&key) {
self.pool_bytes -=
- pool.len() as u64 * u64::from(key.0) * u64::from(key.1) * WORK_BYTES;
+ pool.len() as u64 * u64::from(key.0) * u64::from(key.1) * self.work_bytes();
self.pool_textures -= pool.len();
}
self.used.remove(&key);
@@ -1949,7 +2089,7 @@ impl WgpuCompositor {
.get_mut(&key)
.expect("found above")
.swap_remove(slot);
- self.pool_bytes -= u64::from(key.0) * u64::from(key.1) * WORK_BYTES;
+ self.pool_bytes -= u64::from(key.0) * u64::from(key.1) * self.work_bytes();
self.pool_textures -= 1;
}
}
@@ -2270,7 +2410,7 @@ impl WgpuCompositor {
/// Claims a pooled texture of this size, blank, for a pass to draw into.
fn claim(&mut self, width: u32, height: u32) -> usize {
let key = (width, height);
- let bytes = u64::from(width) * u64::from(height) * WORK_BYTES;
+ let bytes = u64::from(width) * u64::from(height) * self.work_bytes();
let used = *self.used.entry(key).or_insert(0);
let pool = self.pool.entry(key).or_default();
// The texture to draw into, from the ones not claimed yet this
@@ -2297,7 +2437,7 @@ impl WgpuCompositor {
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
- format: WORK,
+ format: self.work,
// A render attachment too: a shader pass draws one pooled
// texture into another of the same size. A copy source and
// destination for the ground a Lighten or Darken reads.
@@ -2394,7 +2534,7 @@ impl WgpuCompositor {
entry_point: Some(entry),
compilation_options: Default::default(),
targets: &[Some(wgpu::ColorTargetState {
- format: WORK,
+ format: self.work,
// A pass replaces: mixing by intensity is the
// shader's own last line.
blend: None,
@@ -2524,7 +2664,7 @@ impl WgpuCompositor {
entry_point: Some("fs_main"),
compilation_options: Default::default(),
targets: &[Some(wgpu::ColorTargetState {
- format: WORK,
+ format: self.work,
// The transition owns the mix; the pipeline does none.
blend: None,
write_mask: wgpu::ColorWrites::ALL,
@@ -2732,7 +2872,7 @@ impl WgpuCompositor {
mip_level_count: 1,
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
- format: WORK,
+ format: self.work,
usage: wgpu::TextureUsages::TEXTURE_BINDING
| wgpu::TextureUsages::RENDER_ATTACHMENT
| wgpu::TextureUsages::COPY_SRC,
@@ -3200,8 +3340,11 @@ impl WgpuCompositor {
});
// Each way the readback fails is said: these are the lines behind
// "the GPU device was lost", and there were none (#223).
- if let Err(error) = self.device.poll(wgpu::PollType::wait_indefinitely()) {
- log::error!("GPU readback: the device did not finish the frame: {error}");
+ if let Err(error) = self.wait() {
+ log::error!(
+ "GPU readback: the device did not finish the frame within {DEVICE_WAIT:?}: \
+ {error}"
+ );
return None;
}
match mapped_rx.try_recv() {
@@ -3259,11 +3402,42 @@ impl Compositor for WgpuCompositor {
if self.dead {
return Frame::black(plan.width, plan.height);
}
+ // Whatever the device says went wrong with this frame is caught
+ // here: memory it ran out of - an 8 GB card shared with the
+ // window, the decoder and the encoder (#297) - or a call it
+ // refused. Uncaught, it was logged and the frame went to the
+ // encoder wrong (audit 2026-10-11, finding 8). A frame drawn under
+ // one is a failed frame, as a failed readback is: the compositor is
+ // dead, and the export draws it again on a fresh device. The
+ // passes' own scopes inside still catch what they catch first, and
+ // what a scope misses still reaches the handler, which counts it.
+ let uncaught = self.uncaught.load(std::sync::atomic::Ordering::Relaxed);
+ let memory = self.device.push_error_scope(wgpu::ErrorFilter::OutOfMemory);
+ let validation = self.device.push_error_scope(wgpu::ErrorFilter::Validation);
let (draws, vertices) = self.prepare(plan);
self.write_vertices(&vertices);
- match self.render_and_read(plan.width, plan.height, &draws) {
- Some(frame) => frame,
- None => {
+ let frame = self.render_and_read(plan.width, plan.height, &draws);
+ let refused = pollster::block_on(validation.pop());
+ let exhausted = pollster::block_on(memory.pop());
+ let errors = [exhausted, refused].into_iter().flatten();
+ let mut failed = frame.is_none();
+ for error in errors {
+ log::error!(
+ "GPU composite at {:.3}s failed: {error}",
+ plan.time.as_f64()
+ );
+ failed = true;
+ }
+ if self.uncaught.load(std::sync::atomic::Ordering::Relaxed) != uncaught {
+ log::error!(
+ "GPU composite at {:.3}s was drawn while the device reported an error",
+ plan.time.as_f64()
+ );
+ failed = true;
+ }
+ match frame {
+ Some(frame) if !failed => frame,
+ _ => {
self.dead = true;
Frame::black(plan.width, plan.height)
}
diff --git a/src/crates/concat-render/src/gpu/tests.rs b/src/crates/concat-render/src/gpu/tests.rs
index 7a831b1e..e499673b 100644
--- a/src/crates/concat-render/src/gpu/tests.rs
+++ b/src/crates/concat-render/src/gpu/tests.rs
@@ -1750,3 +1750,92 @@ fn the_software_compositor_draws_on_a_software_adapter_or_not_at_all() {
assert_eq!(gpu.adapter_info().device_type, wgpu::DeviceType::Cpu);
}
}
+
+/// No wait on the device is without an end: an export whose card stopped
+/// answering sat in one for good (#297). Each goes through `wait`, which
+/// gives up after `DEVICE_WAIT`.
+#[test]
+fn no_wait_on_the_device_is_unbounded() {
+ let source = include_str!("../gpu.rs");
+ assert!(
+ !source.contains(concat!("wait_", "indefinitely")),
+ "every poll is bounded by DEVICE_WAIT"
+ );
+ assert!(DEVICE_WAIT >= std::time::Duration::from_secs(10));
+ assert!(DEVICE_WAIT <= std::time::Duration::from_secs(60));
+}
+
+/// A frame the device refuses part of - here a layer wider than any
+/// texture it can make - is a failed frame, not a wrong one handed on:
+/// the compositor says it is lost, and the export draws the frame again
+/// on another device or stops with the reason.
+#[test]
+fn a_frame_the_device_refuses_is_a_failed_frame() {
+ let Some(mut gpu) = gpu() else { return };
+ let past = gpu.device.limits().max_texture_dimension_2d + 16;
+ let plan = plan(16, 16, vec![layer(solid(past, 2, [255, 0, 0, 255]))]);
+ let frame = gpu.render(&plan);
+ assert!(gpu.lost(), "the refused frame is reported");
+ assert_eq!(frame.pixel(8, 8), Some([0, 0, 0, 255]));
+}
+
+/// The device is asked for no more than its adapter has: an OpenGL ES
+/// phone's limits are under WebGPU's defaults, and asking for those opened
+/// nothing there (#312). A desktop adapter still gets WebGPU's defaults
+/// and its own texture size.
+#[test]
+fn the_device_asks_for_what_the_adapter_has() {
+ let phone = wgpu::Limits {
+ max_texture_dimension_2d: 4096,
+ ..wgpu::Limits::downlevel_webgl2_defaults()
+ };
+ let asked = limits_for(&phone);
+ assert!(asked.check_limits(&phone), "the phone opens a device");
+ assert_eq!(asked.max_texture_dimension_2d, 4096);
+
+ let desktop = wgpu::Limits {
+ max_texture_dimension_2d: 16384,
+ ..wgpu::Limits::default()
+ };
+ let asked = limits_for(&desktop);
+ assert!(asked.check_limits(&desktop));
+ assert!(wgpu::Limits::default().check_limits(&asked));
+ assert_eq!(asked.max_texture_dimension_2d, 16384);
+}
+
+/// On a device that cannot draw in half floats the eight-bit fallback
+/// draws the same picture, to within its rounding: an opaque gradient
+/// with a translucent layer blended over it.
+#[test]
+fn the_eight_bit_working_format_draws_the_same_picture() {
+ let Some(mut gpu) = gpu() else { return };
+ let mut fallback =
+ WgpuCompositor::drawing_in(gpu.device.clone(), gpu.queue.clone(), WORK_FALLBACK);
+ let mut top = layer(solid(16, 16, [20, 200, 90, 140]));
+ top.transform = Transform {
+ offset_x: 0.25,
+ ..Transform::default()
+ };
+ let plan = plan(32, 32, vec![layer(gradient(32, 32)), top]);
+ let expected = gpu.render(&plan);
+ let actual = fallback.render(&plan);
+ assert!(!fallback.lost());
+ for (want, got) in expected.pixels().iter().zip(actual.pixels()) {
+ assert!(
+ (i32::from(*want) - i32::from(*got)).abs() <= 2,
+ "half floats {want} vs eight bits {got}"
+ );
+ }
+ assert_eq!(fallback.probe(&[], [0.5; 4], 4, 0.0), None);
+}
+
+/// A frame bigger than the device's textures is refused up front, at the
+/// document's ceiling or the device's, whichever is lower.
+#[test]
+fn the_largest_frame_is_what_the_device_holds() {
+ let Some(gpu) = gpu() else { return };
+ assert_eq!(
+ gpu.max_side(),
+ concat_core::frame::MAX_SIDE.min(gpu.device.limits().max_texture_dimension_2d)
+ );
+}
diff --git a/src/crates/concat/src/gpu.rs b/src/crates/concat/src/gpu.rs
index 323391fd..c2d08832 100644
--- a/src/crates/concat/src/gpu.rs
+++ b/src/crates/concat/src/gpu.rs
@@ -84,13 +84,15 @@ impl Gpu {
} else {
format!("{} · {}", info.name, info.backend)
});
- // The adapter's own limits, not `Limits::default()`: the defaults are
- // the downlevel floor every GL-class device can meet, and their
- // `max_texture_dimension_2d` is 8192 whatever the machine can do -
- // 16384 on any current desktop GPU. Both sides of this device want
- // the real number: the monitor composites the timeline's frame, and
- // Slint's renderer allocates a layer as large as the element it is
- // flattening.
+ // The adapter's own limits, not `Limits::default()`. The defaults are
+ // WebGPU's baseline, which a Vulkan, Metal or Direct3D 12 GPU meets
+ // and an OpenGL ES one may not - asking for them opened no device
+ // on such phones (#312) - and their `max_texture_dimension_2d` is
+ // 8192 whatever the machine can do: 16384 on any current desktop
+ // GPU. What the adapter reports it can always open, and both sides
+ // of this device want the real number: the monitor composites the
+ // timeline's frame, and Slint's renderer allocates a layer as large
+ // as the element it is flattening.
let (device, queue) = pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor {
label: Some("concat"),
required_limits: adapter.limits(),