export: run the zero-copy H.264 encoder on the shared device (Stage 3c-export pt 2)
Makes the common Linux H.264 export fully GPU-resident: decode (HW NV12) → composite → VAAPI encode all on one device, no CPU round-trips. - gpu-video-encoder: ZeroCopyEncoder now holds wgpu (device,queue,adapter) handles instead of owning a DrmDevice. New `new_on_device(device,queue,adapter,...)` runs the RGBA→NV12 render + DMA-BUF import on a passed device; `new` keeps building its own. The encoder only *imports* VAAPI surfaces (not export), so the shared import-capable device works directly. - editor: stash the shared device handles on EditorApp (set in the creation closure from the eframe render_state when the shared device is active) and thread them through start_video_export / start_video_with_audio_export → try_build_zero_copy, which uses new_on_device when available. ZeroCopyVideo carries on_shared_device → the export composite uses hardware_ok=true (consuming HW-decoded GPU frames from pt 1) only then. Falls back to the own-device encoder (decode downloads to CPU) when no shared device. Tradeoff: the export now shares the GPU with the UI thread (was a separate device); acceptable for the GPU-resident-decode win. Compiles; crate tests build. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
bb3369b709
commit
ca9a70e10a
@@ -6,7 +6,7 @@
|
||||
|
||||
use crate::dmabuf::{self, ImportedNv12, Nv12DmaBuf};
|
||||
use crate::render_nv12::Rgba2Nv12;
|
||||
use crate::vk_device::{self, DrmDevice};
|
||||
use crate::vk_device;
|
||||
use ffmpeg_sys_next as ff;
|
||||
use std::collections::HashMap;
|
||||
use std::ffi::CString;
|
||||
@@ -19,7 +19,11 @@ fn averror(e: i32) -> i32 {
|
||||
}
|
||||
|
||||
pub struct ZeroCopyEncoder {
|
||||
drm: DrmDevice,
|
||||
/// wgpu handles the NV12 render runs on — either an own `DrmDevice`'s (via `new`) or the
|
||||
/// editor's shared device (via `new_on_device`). Cloned (Arc-backed) so the source can drop.
|
||||
device: wgpu::Device,
|
||||
queue: wgpu::Queue,
|
||||
adapter: wgpu::Adapter,
|
||||
renderer: Rgba2Nv12,
|
||||
hw_device: *mut ff::AVBufferRef,
|
||||
frames_ref: *mut ff::AVBufferRef,
|
||||
@@ -51,8 +55,30 @@ impl ZeroCopyEncoder {
|
||||
output_path: &Path,
|
||||
full_range: bool,
|
||||
) -> Result<Self, String> {
|
||||
// Build a dedicated DMA-BUF-import device and run the encoder on it.
|
||||
let drm = vk_device::create()?;
|
||||
let renderer = Rgba2Nv12::new(&drm.device, full_range);
|
||||
Self::new_on_device(
|
||||
drm.device, drm.queue, drm.adapter,
|
||||
width, height, framerate, bitrate_kbps, output_path, full_range,
|
||||
)
|
||||
}
|
||||
|
||||
/// Build the encoder running its NV12 render + DMA-BUF import on an existing wgpu device (the
|
||||
/// editor's shared device), so decode→composite→encode stay GPU-resident on one device. The
|
||||
/// device must have the DMA-BUF import extensions (a `DrmDevice` / `vk_device::create_windowed`).
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn new_on_device(
|
||||
device: wgpu::Device,
|
||||
queue: wgpu::Queue,
|
||||
adapter: wgpu::Adapter,
|
||||
width: u32,
|
||||
height: u32,
|
||||
framerate: i32,
|
||||
bitrate_kbps: u32,
|
||||
output_path: &Path,
|
||||
full_range: bool,
|
||||
) -> Result<Self, String> {
|
||||
let renderer = Rgba2Nv12::new(&device, full_range);
|
||||
unsafe {
|
||||
let mut hw_device = crate::vaapi::create_device()?;
|
||||
let name = CString::new("h264_vaapi").unwrap();
|
||||
@@ -153,7 +179,9 @@ impl ZeroCopyEncoder {
|
||||
let stream_tb = (*stream).time_base;
|
||||
|
||||
Ok(Self {
|
||||
drm,
|
||||
device,
|
||||
queue,
|
||||
adapter,
|
||||
renderer,
|
||||
hw_device,
|
||||
frames_ref,
|
||||
@@ -172,10 +200,10 @@ impl ZeroCopyEncoder {
|
||||
|
||||
/// The wgpu device frames must be rendered on (so the RGBA texture is importable).
|
||||
pub fn device(&self) -> &wgpu::Device {
|
||||
&self.drm.device
|
||||
&self.device
|
||||
}
|
||||
pub fn queue(&self) -> &wgpu::Queue {
|
||||
&self.drm.queue
|
||||
&self.queue
|
||||
}
|
||||
|
||||
/// Render `rgba` (an `Rgba8Unorm` texture on [`Self::device`], `TEXTURE_BINDING`)
|
||||
@@ -216,7 +244,7 @@ impl ZeroCopyEncoder {
|
||||
uv_pitch: uv.pitch as u64,
|
||||
ten_bit: false,
|
||||
};
|
||||
let imported = match dmabuf::import_raw(&self.drm.device, &self.drm.adapter, &buf) {
|
||||
let imported = match dmabuf::import_raw(&self.device, &self.adapter, &buf) {
|
||||
Ok(i) => i,
|
||||
Err(e) => {
|
||||
ff::av_frame_free(&mut (drm_f as *mut _));
|
||||
@@ -233,10 +261,10 @@ impl ZeroCopyEncoder {
|
||||
let rgba_view = rgba.create_view(&Default::default());
|
||||
let y_view = imp.y().create_view(&Default::default());
|
||||
let uv_view = imp.uv().create_view(&Default::default());
|
||||
let mut cmd = self.drm.device.create_command_encoder(&Default::default());
|
||||
self.renderer.convert(&self.drm.device, &mut cmd, &rgba_view, &y_view, &uv_view);
|
||||
self.drm.queue.submit(Some(cmd.finish()));
|
||||
let _ = self.drm.device.poll(wgpu::PollType::wait_indefinitely());
|
||||
let mut cmd = self.device.create_command_encoder(&Default::default());
|
||||
self.renderer.convert(&self.device, &mut cmd, &rgba_view, &y_view, &uv_view);
|
||||
self.queue.submit(Some(cmd.finish()));
|
||||
let _ = self.device.poll(wgpu::PollType::wait_indefinitely());
|
||||
|
||||
// Encode the surface.
|
||||
(*surf).pts = self.pts;
|
||||
|
||||
Reference in New Issue
Block a user