Skip to content

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

fix(export): the export draws on the window's device, not a second one opened through every driver

Jareercff4a553a5de
diff --git a/src/Cargo.lock b/src/Cargo.lock
index 26fee0b2..b5846121 100644
--- a/src/Cargo.lock
+++ b/src/Cargo.lock
@@ -1225,6 +1225,7 @@ dependencies = [
"concat-project",
"concat-render",
"concat-vision",
+ "log",
"serde",
]
diff --git a/src/crates/concat-export/Cargo.toml b/src/crates/concat-export/Cargo.toml
index a4920a09..75cebf31 100644
--- a/src/crates/concat-export/Cargo.toml
+++ b/src/crates/concat-export/Cargo.toml
@@ -21,6 +21,7 @@ concat-effects.workspace = true
# frame before it is placed. Reading only; the host finds them.
concat-vision.workspace = true
serde = { version = "1", features = ["derive"] }
+log = "0.4"
[lints]
workspace = true
diff --git a/src/crates/concat-export/src/lib.rs b/src/crates/concat-export/src/lib.rs
index 0415c8aa..704e89ce 100644
--- a/src/crates/concat-export/src/lib.rs
+++ b/src/crates/concat-export/src/lib.rs
@@ -798,8 +798,29 @@ fn ride(
true
}
-/// Renders `request` and returns the path written.
-pub fn render(request: &ExportRequest, mut reporter: Reporter<'_>) -> Result<String, String> {
+/// Renders `request` and returns the path written, drawing on a device of
+/// its own; [`render_on`] draws on one the caller lends.
+pub fn render(request: &ExportRequest, reporter: Reporter<'_>) -> Result<String, String> {
+ render_on(request, None, reporter)
+}
+
+/// Renders `request` and returns the path written, drawing on `compositor`
+/// where the caller has one to lend - the window's, a sibling of the
+/// monitor's on the device the window draws with - and on a device of its
+/// own otherwise (see [`WgpuCompositor::new`]).
+///
+/// The window's device has drawn the monitor since the project opened:
+/// whatever the machine and its drivers make of the compositor, they have
+/// made of it already, frame after frame. A device opened for the export
+/// alone is a second road through the drivers, on a thread of its own, and
+/// on Windows laptops with NVIDIA chips it was a road that ended in the
+/// driver at the first frame, taking the app with it and leaving nothing
+/// in the log (issue #202).
+pub fn render_on(
+ request: &ExportRequest,
+ compositor: Option<WgpuCompositor>,
+ mut reporter: Reporter<'_>,
+) -> Result<String, String> {
if request.clips.is_empty() {
return Err("there is nothing on the timeline to export".to_owned());
}
@@ -869,6 +890,7 @@ pub fn render(request: &ExportRequest, mut reporter: Reporter<'_>) -> Result<Str
let result = (|| -> Result<(), String> {
render_picture(
request,
+ compositor,
rate,
total_frames,
&visible,
@@ -1065,13 +1087,12 @@ fn ground_layer(ground: Frame) -> PlannedLayer {
PlannedLayer::picture(concat_render::detached_clip(), std::sync::Arc::new(ground))
}
-/// The compositor an export draws with: the machine's GPU, or its software
-/// adapter where it has none (see [`WgpuCompositor::new`]). The one error is
-/// a machine with neither, which is said in words a person can act on.
-fn best_compositor() -> Result<Box<dyn Compositor>, String> {
- WgpuCompositor::new()
- .map(|gpu| Box::new(gpu) as Box<dyn Compositor>)
- .ok_or_else(|| NO_RENDERER.to_owned())
+/// The compositor an export draws with when its caller lends none: a
+/// device of its own on the machine's GPU, or on its software adapter where
+/// it has none (see [`WgpuCompositor::new`]). The one error is a machine
+/// with neither, which is said in words a person can act on.
+fn best_compositor() -> Result<WgpuCompositor, String> {
+ WgpuCompositor::new().ok_or_else(|| NO_RENDERER.to_owned())
}
/// What an export or a preview says on a machine that offers no GPU and no
@@ -1096,6 +1117,7 @@ fn headless<T>(draw: impl FnOnce(&mut WgpuCompositor) -> T) -> Result<T, String>
/// Composites every frame of the timeline into a soundless video file.
fn render_picture(
request: &ExportRequest,
+ compositor: Option<WgpuCompositor>,
rate: FrameRate,
total_frames: i64,
visible: &[&ExportClip],
@@ -1103,7 +1125,19 @@ fn render_picture(
destination: &Path,
reporter: &mut Reporter<'_>,
) -> Result<(), String> {
- let mut compositor = best_compositor()?;
+ let mut compositor = match compositor {
+ Some(lent) => lent,
+ None => best_compositor()?,
+ };
+ // Which chip and API the file is drawn on: the first thing a report of
+ // an export gone wrong needs to say, and until now could not (#202).
+ let adapter = compositor.adapter_info();
+ log::info!(
+ "export: drawing on {} ({:?}, {:?})",
+ adapter.name,
+ adapter.device_type,
+ adapter.backend
+ );
let BuiltTimeline {
timeline,
stills,
@@ -1271,7 +1305,7 @@ fn render_picture(
}
let composed = composite_treated(
- &mut *compositor,
+ &mut compositor,
FramePlan {
time,
width: request.width,
diff --git a/src/crates/concat-host/src/export.rs b/src/crates/concat-host/src/export.rs
index a4241d1b..74698c29 100644
--- a/src/crates/concat-host/src/export.rs
+++ b/src/crates/concat-host/src/export.rs
@@ -11,8 +11,9 @@
use std::sync::Arc;
use std::sync::atomic::AtomicBool;
-use concat_export::{ExportClip, ExportRequest, Reporter, render};
+use concat_export::{ExportClip, ExportRequest, Reporter, render_on};
pub use concat_media::{ColorRange, RateMode, VideoCodec};
+pub use concat_render::WgpuCompositor;
use crate::jobs::{Job, SingleFlight};
use crate::session::Session;
@@ -83,10 +84,26 @@ pub fn request(session: &Session, spec: &ExportSpec, titles: Vec<ExportClip>) ->
/// Renders `request` and returns the path written, reporting through
/// `progress` and stopping at the next frame once `cancel` is set. Blocks for
-/// the whole render: run it on its own thread.
+/// the whole render: run it on its own thread. Draws on a device of its
+/// own; [`run_on`] draws on one the caller lends.
pub fn run(
request: &ExportRequest,
cancel: &AtomicBool,
+ progress: impl FnMut(Progress),
+) -> Result<String, String> {
+ run_on(request, None, cancel, progress)
+}
+
+/// [`run`] drawing on `compositor` where the caller has one to lend: the
+/// window's, a sibling of its monitor's (see `Monitor::sibling`), so the
+/// export draws on the device that has drawn the monitor all along rather
+/// than opening a second one through the drivers, which on Windows laptops
+/// with NVIDIA chips took the app down at the first frame (issue #202).
+/// `None` draws as [`run`] does.
+pub fn run_on(
+ request: &ExportRequest,
+ compositor: Option<WgpuCompositor>,
+ cancel: &AtomicBool,
mut progress: impl FnMut(Progress),
) -> Result<String, String> {
let mut report = |frame: i64, total: i64, stage: &'static str| {
@@ -96,8 +113,9 @@ pub fn run(
stage,
});
};
- render(
+ render_on(
request,
+ compositor,
Reporter {
progress: &mut report,
cancel,
diff --git a/src/crates/concat-host/tests/export.rs b/src/crates/concat-host/tests/export.rs
index f711fe14..2ba816be 100644
--- a/src/crates/concat-host/tests/export.rs
+++ b/src/crates/concat-host/tests/export.rs
@@ -690,6 +690,59 @@ fn one_clip_exports_whole_at_every_rate() {
}
}
+/// Issue #202: on Windows laptops with NVIDIA chips an export took the app
+/// down at its first frame, with nothing in the log. The export had opened
+/// a device of its own through every API wgpu was built with, where the
+/// window draws on one API and one device that has worked for every
+/// monitor frame. So the window lends the export a sibling of the monitor's
+/// compositor, on the window's device: an export drawn on a lent compositor
+/// writes the same picture as one drawn on a device of its own.
+#[test]
+fn an_export_draws_on_a_lent_compositor() {
+ let Some(window) = export::WgpuCompositor::new() else {
+ eprintln!("no GPU or software renderer here; nothing to lend");
+ return;
+ };
+ let scratch = Scratch::new("lent");
+ let source = scratch.path().join("clock.mp4");
+ picture(&source, FrameRate::THIRTY, 2, VideoCodec::H264);
+ let mut studio = Studio::new(scratch.path(), "Lent", video(WIDTH, HEIGHT, 30, 1));
+ let media = studio.import(&source);
+ studio.apply(Command::AddClipAtFirstFree {
+ media_id: media,
+ start: 0.0,
+ });
+ let spec = ExportSpec {
+ output: scratch
+ .path()
+ .join("lent.mp4")
+ .to_string_lossy()
+ .into_owned(),
+ crf: 18,
+ preset: "ultrafast".to_owned(),
+ codec: VideoCodec::H264,
+ ten_bit: false,
+ rate_mode: RateMode::Vbr,
+ bitrate_kbps: 0,
+ color_range: concat_media::ColorRange::Limited,
+ hdr: false,
+ };
+ let request = export::request(&studio.session, &spec, Vec::new());
+ let written = export::run_on(
+ &request,
+ Some(window.sibling()),
+ &AtomicBool::new(false),
+ |_| {},
+ )
+ .unwrap_or_else(|error| panic!("the export on a lent compositor failed: {error}"));
+
+ let exported = Exported::read("lent", Path::new(&written), (30, 1));
+ exported.expect_length(2.0);
+ exported.expect_second(0.5, 0);
+ exported.expect_second(1.5, 1);
+ exported.expect_no_sound();
+}
+
/// A crop and the flips reach the exported picture, in source terms: the
/// crop names the source's own edges, the flips mirror what the crop kept,
/// and a cropped picture is fitted into the frame and centred, bars
diff --git a/src/crates/concat-render/src/gpu.rs b/src/crates/concat-render/src/gpu.rs
index b2362010..7e676076 100644
--- a/src/crates/concat-render/src/gpu.rs
+++ b/src/crates/concat-render/src/gpu.rs
@@ -709,29 +709,67 @@ pub struct WgpuCompositor {
}
impl WgpuCompositor {
- /// Builds a compositor on the best available adapter, or `None` when the
- /// machine has nothing usable - callers fall back to the CPU path.
+ /// Builds a compositor on a device of its own, or `None` when the
+ /// machine has nothing usable.
+ ///
+ /// The platform's own API is asked first: Metal, Direct3D 12 or
+ /// Vulkan. Every API wgpu was built with is asked only when that one
+ /// gives nothing. It is the API the window draws on, so whatever driver
+ /// has been drawing the monitor is the one an export or a card draws on
+ /// too. Asked for every API at once, wgpu ranks them in a fixed order
+ /// with Vulkan ahead of Direct3D, so on a Windows laptop it went past
+ /// the Direct3D device the window was drawing on to NVIDIA's Vulkan
+ /// driver, and opened a WGL context on a thread of its own besides;
+ /// the export died in the driver at its first frame, with nothing in
+ /// the log (issue #202). The GPU where there is one; the software
+ /// adapter where there is not, which draws the same pictures, slower -
+ /// the one road left since the CPU compositor went.
///
/// Native only: it blocks on the adapter and device requests, and on the
/// web there is no thread to block. A web caller awaits those requests
/// itself and hands the result to [`WgpuCompositor::with_device`].
#[cfg(not(target_arch = "wasm32"))]
pub fn new() -> Option<Self> {
- let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle());
- // The GPU where there is one; the software adapter where there is
- // not, which draws the same pictures, slower - the one road left
- // since the CPU compositor went.
- let adapter = [false, true].into_iter().find_map(|software| {
- pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions {
- power_preference: wgpu::PowerPreference::HighPerformance,
- force_fallback_adapter: software,
- ..Default::default()
- }))
- .ok()
- })?;
- let (device, queue) =
- pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor::default())).ok()?;
- Some(Self::with_device(device, queue))
+ let own = if cfg!(target_vendor = "apple") {
+ wgpu::Backends::METAL
+ } else if cfg!(windows) {
+ wgpu::Backends::DX12
+ } else {
+ wgpu::Backends::VULKAN
+ };
+ [own, wgpu::Backends::all()]
+ .into_iter()
+ .find_map(Self::on_backends)
+ }
+
+ /// A compositor on the best adapter among `backends`, hardware before
+ /// software, or `None` when none of them opens a device.
+ #[cfg(not(target_arch = "wasm32"))]
+ fn on_backends(backends: wgpu::Backends) -> Option<Self> {
+ let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
+ backends,
+ ..wgpu::InstanceDescriptor::new_without_display_handle()
+ });
+ [false, true].into_iter().find_map(|software| {
+ let adapter =
+ pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions {
+ power_preference: wgpu::PowerPreference::HighPerformance,
+ force_fallback_adapter: software,
+ ..Default::default()
+ }))
+ .ok()?;
+ let (device, queue) =
+ pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor::default()))
+ .ok()?;
+ Some(Self::with_device(device, queue))
+ })
+ }
+
+ /// The adapter this compositor's device was opened on - its name, its
+ /// kind and its API - for the log line that says what an export drew
+ /// on.
+ pub fn adapter_info(&self) -> wgpu::AdapterInfo {
+ self.device.adapter_info()
}
/// A second compositor on this one's device, with pipelines, pools and
diff --git a/src/crates/concat/src/panes/export.rs b/src/crates/concat/src/panes/export.rs
index 44c71279..450aca6e 100644
--- a/src/crates/concat/src/panes/export.rs
+++ b/src/crates/concat/src/panes/export.rs
@@ -344,10 +344,17 @@ impl ExportPane {
self.written.clear();
self.started_at = Some(std::time::Instant::now());
+ // The export draws on the window's device - a sibling of the
+ // monitor's compositor - not on a second device opened through the
+ // drivers for the export alone, which on Windows laptops with NVIDIA
+ // chips took the whole app down at the first frame with nothing in
+ // the log (issue #202). None without a GPU here, and the render then
+ // opens what it can.
+ let compositor = studio.host.monitor.sibling();
spawn(
move || {
let job = job;
- export::run(&request, job.cancel_flag(), |progress| {
+ export::run_on(&request, compositor, job.cancel_flag(), |progress| {
let fraction = if progress.total > 0 {
progress.frame as f32 / progress.total as f32
} else {