diff --git a/crates/reims-vgpu/src/graphics/nvidia/encode.rs b/crates/reims-vgpu/src/graphics/nvidia/encode.rs index e1be125bb..8f3813b64 100644 --- a/crates/reims-vgpu/src/graphics/nvidia/encode.rs +++ b/crates/reims-vgpu/src/graphics/nvidia/encode.rs @@ -431,8 +431,8 @@ impl Fills

{ /// The packets of frame `index` of `size`, encoded by `encoder` from `planes`, luma and /// chroma, where they lie, copied by the GPU into memory NVENC reads (module doc); `None` /// where the copy or NVENC's read of it was declined, nothing encoded, which is said once a - /// session. `Err` where the copy failed once on the GPU, or NVENC did not take the frame it - /// read. + /// session. `Err` where the copy failed once on the GPU, where the platform refused a command + /// of it (the frame fails, nothing handed to NVENC), or NVENC did not take the frame it read. fn encode_filled( &mut self, encoder: &mut NvencEncoder, @@ -472,6 +472,15 @@ impl Fills

{ } } Ok(Filled::Declined(why)) => format!("the GPU's copy: {why}"), + // Nothing was submitted, and NVENC is handed nothing: the frame fails, and + // the session's next frames are copied as before. + Ok(Filled::Refused(why)) => { + return Err(format!( + "the platform refused the GPU's copy of the frame into NVENC's \ + input: {why}" + ) + .into()); + } // The GPU may write the input still: the session's frames are uploaded from // here on. Err(error) => { diff --git a/crates/reims-vgpu/src/graphics/nvidia/encode/fill.rs b/crates/reims-vgpu/src/graphics/nvidia/encode/fill.rs index 3ec12a884..538d6a302 100644 --- a/crates/reims-vgpu/src/graphics/nvidia/encode/fill.rs +++ b/crates/reims-vgpu/src/graphics/nvidia/encode/fill.rs @@ -23,8 +23,13 @@ //! uploaded instead; an input the device or CUDA refuses to make is not tried again in the //! session. A copy that failed once submitted is an error: its input is kept out of //! use while the session lives, as the GPU may write it still. +//! - **A command of the copy the platform refuses** ([`Filled::Refused`]), which records nothing +//! (on the rt platform, metal2vulkan-runtime's refusal, counted under its kind), leaves the +//! input unwritten: nothing is submitted, NVENC is handed nothing, and the frame fails, an +//! encode error to the guest. The session's next frames are copied as before. //! -//! Each session logs at its end how many frames went this way, and the copies' times. +//! Each session logs at its end how many frames went this way, and the copies' times, and how +//! many frames failed as the platform refused their copy. use std::sync::Arc; use std::time::Duration; @@ -55,6 +60,22 @@ pub(super) enum Filled { }, /// Nothing was written, for the reason given: the frame is uploaded. Declined(String), + /// The platform refused a command of the copy, which recorded nothing, for the reason given: + /// nothing was submitted, the input holds no frame, and the frame fails. + Refused(String), +} + +/// What came of a frame's copy into the input ([`copy_frame`]). +#[derive(Debug, PartialEq, Eq)] +enum Copied { + /// The frame was written, and the copy waited for. + Written, + /// Nothing was submitted, for the reason given. + Declined(String), + /// The platform refused a command of the copy, which recorded nothing: nothing was submitted. + Refused(String), + /// The copy failed once submitted: the GPU may write the input still. + Failed(String), } /// One session's input, made as its first frame was filled. @@ -79,6 +100,8 @@ pub(in super::super) struct Filler { refused: Option, /// Inputs whose copy failed on the GPU, which may write them still. failed: Vec>, + /// Frames that failed as the platform refused a command of their copy ([`Filled::Refused`]). + refused_copies: u64, /// Frames filled, and the time of their copies from submission to their wait's end. filled: u64, /// The clock the copies are timed on: the video's runtime's. @@ -102,6 +125,7 @@ impl Filler

{ input: None, refused: None, failed: Vec::new(), + refused_copies: 0, filled: 0, runtime: Arc::clone(runtime), total: Duration::ZERO, @@ -117,8 +141,9 @@ impl Filler

{ /// Writes the frame of `size`, of samples of `sample` bytes, in `luma` and `chroma` where /// they lie, into the session's input (module doc), and returns once the GPU wrote it. - /// `Err` where the copy failed once submitted: the input is kept out of use, and the caller - /// fills no more frames of the session. + /// [`Filled::Refused`] where the platform refused a command of the copy: nothing was + /// submitted, and the frame fails. `Err` where the copy failed once submitted: the input is + /// kept out of use, and the caller fills no more frames of the session. pub(super) fn fill( &mut self, luma: &OutputPlane

, @@ -151,14 +176,31 @@ impl Filler

{ let destination = input.buffer.clone_ref(); let copies = [(luma, 0, height), (chroma, pitch * height, chroma_rows)]; let start = self.runtime.now(); - match self.copy(&destination, &copies, pitch, row_bytes)? { - Some(why) => return Ok(Filled::Declined(why)), - None => { + match self.copy(&destination, &copies, pitch, row_bytes) { + Copied::Written => { let took = self.runtime.elapsed_since(start); self.filled += 1; self.total += took; self.longest = self.longest.max(took); } + Copied::Declined(why) => return Ok(Filled::Declined(why)), + Copied::Refused(why) => { + if self.refused_copies == 0 { + log::warn!( + "hardware video: {ENCODE_NAME}: the platform refused the GPU's copy of a \ + frame into NVENC's input ({why}): the frame fails, as does each of the \ + session's frames whose copy it refuses (counted at the session's end)" + ); + } + self.refused_copies += 1; + return Ok(Filled::Refused(why)); + } + Copied::Failed(failed) => { + self.failed.extend(self.input.take()); + return Err( + format!("the GPU's copy of a frame into NVENC's input: {failed}").into(), + ); + } } Ok(Filled::Written { input: import, @@ -241,73 +283,83 @@ impl Filler

{ }) } - /// Copies each of `copies`, a plane, the input's byte its first row goes to and its rows, into - /// `destination`, rows `pitch` apart, `row_bytes` of each, on the filler's queue, and waits - /// for the copy. `Some` says why nothing was submitted; `Err` is a copy that failed once - /// submitted, whose input is kept out of use. + /// [`copy_frame`] on the filler's queue, made on its first frame. fn copy( &mut self, destination: &P::Buffer, copies: &[(&OutputPlane

, u64, u64); 2], pitch: u64, row_bytes: u64, - ) -> Result, BackendError> { + ) -> Copied { if self.queue.is_none() { let Some(queue) = self.device.new_command_queue() else { - return Ok(Some("no command queue".to_owned())); + return Copied::Declined("no command queue".to_owned()); }; queue.set_label("reims-vgpu.graphics-video-fill"); self.queue = Some(queue); } - let Some(queue) = self.queue.as_ref() else { - return Ok(Some("no command queue".to_owned())); - }; - let cb = match queue.command_buffer(OnFault::Return) { - Ok(Some(cb)) => cb, - Ok(None) => return Ok(Some("no command buffer".to_owned())), - Err(raise) => return Ok(Some(format!("its command buffer: {raise}"))), - }; - let blit = match cb.blit_encoder(OnFault::Return) { - Ok(Some(blit)) => blit, - Ok(None) => return Ok(Some("no blit encoder".to_owned())), - Err(raise) => return Ok(Some(format!("its blit encoder: {raise}"))), - }; - for &(plane, at, rows) in copies { - for (from, to, size) in row_copies(plane, at, rows, pitch, row_bytes) { - let copy = BlitCmd::CopyBufferToBuffer { - source_buffer: Some(&plane.buffer), - source_offset: from, - destination_buffer: Some(destination), - destination_offset: to, - size, - }; - if let Err(raise) = blit.encode(OnFault::Return, copy) { - return Ok(Some(format!("a copy: {raise}"))); - } + match self.queue.as_ref() { + Some(queue) => copy_frame::

(queue, destination, copies, pitch, row_bytes), + None => Copied::Declined("no command queue".to_owned()), + } + } +} + +/// Copies each of `copies`, a plane, the input's byte its first row goes to and its rows, into +/// `destination`, rows `pitch` apart, `row_bytes` of each, on `queue`, and waits for the copy. +/// A command the platform refuses, recording nothing, is [`Copied::Refused`]: the command buffer +/// is not committed, so nothing is submitted and the input holds no frame. +fn copy_frame( + queue: &P::CommandQueue, + destination: &P::Buffer, + copies: &[(&OutputPlane

, u64, u64); 2], + pitch: u64, + row_bytes: u64, +) -> Copied { + let cb = match queue.command_buffer(OnFault::Return) { + Ok(Some(cb)) => cb, + Ok(None) => return Copied::Declined("no command buffer".to_owned()), + Err(raise) => return Copied::Declined(format!("its command buffer: {raise}")), + }; + let blit = match cb.blit_encoder(OnFault::Return) { + Ok(Some(blit)) => blit, + Ok(None) => return Copied::Declined("no blit encoder".to_owned()), + Err(raise) => return Copied::Declined(format!("its blit encoder: {raise}")), + }; + for &(plane, at, rows) in copies { + for (from, to, size) in row_copies(plane, at, rows, pitch, row_bytes) { + let copy = BlitCmd::CopyBufferToBuffer { + source_buffer: Some(&plane.buffer), + source_offset: from, + destination_buffer: Some(destination), + destination_offset: to, + size, + }; + match blit.encode(OnFault::Return, copy) { + Ok(encoded) if encoded.recorded() => {} + Ok(_) => return Copied::Refused(format!("a copy of {size} bytes")), + Err(raise) => return Copied::Declined(format!("a copy: {raise}")), } } - if let Err(why) = blit.release_to_external(destination) { - return Ok(Some(format!("its release to CUDA: {why}"))); + } + if let Err(why) = blit.release_to_external(destination) { + return Copied::Declined(format!("its release to CUDA: {why}")); + } + match blit.encode(OnFault::Return, BlitCmd::EndEncoding) { + Ok(encoded) if encoded.recorded() => {} + Ok(_) => return Copied::Refused("its end".to_owned()), + Err(raise) => return Copied::Declined(format!("its end: {raise}")), + } + if let Err(raise) = cb.commit(OnFault::Return) { + return Copied::Declined(format!("its commit: {raise}")); + } + match cb.wait_until_completed(OnFault::Return) { + Err(raise) => Copied::Failed(format!("its wait: {raise}")), + Ok(()) if cb.status() != CommandBufferStatus::COMPLETED => { + let error = cb.error().map(|e| format!("{e:?}")).unwrap_or_default(); + Copied::Failed(format!("the copy failed {error}")) } - if let Err(raise) = blit.encode(OnFault::Return, BlitCmd::EndEncoding) { - return Ok(Some(format!("its end: {raise}"))); - } - if let Err(raise) = cb.commit(OnFault::Return) { - return Ok(Some(format!("its commit: {raise}"))); - } - let failed = match cb.wait_until_completed(OnFault::Return) { - Err(raise) => Some(format!("its wait: {raise}")), - Ok(()) if cb.status() != CommandBufferStatus::COMPLETED => { - let error = cb.error().map(|e| format!("{e:?}")).unwrap_or_default(); - Some(format!("the copy failed {error}")) - } - Ok(()) => None, - }; - if let Some(failed) = failed { - self.failed.extend(self.input.take()); - return Err(format!("the GPU's copy of a frame into NVENC's input: {failed}").into()); - } - Ok(None) + Ok(()) => Copied::Written, } } @@ -338,14 +390,23 @@ fn row_copies( impl Drop for Filler

{ fn drop(&mut self) { - if self.filled == 0 { + if self.filled == 0 && self.refused_copies == 0 { return; } let ms = |d: Duration| d.as_secs_f64() * 1e3; - let mean = self.total / u32::try_from(self.filled).unwrap_or(u32::MAX); + let mean = self.total / u32::try_from(self.filled.max(1)).unwrap_or(u32::MAX); + let refused = if self.refused_copies > 0 { + format!( + "; {} failed as the platform refused their copy", + self.refused_copies + ) + } else { + String::new() + }; log::info!( "hardware video: {ENCODE_NAME}: an encode session's frames copied by the GPU into \ - NVENC's input: {}, {:.3} ms each from submission to the wait's end, {:.3} ms at most", + NVENC's input: {}, {:.3} ms each from submission to the wait's end, {:.3} ms at \ + most{refused}", self.filled, ms(mean), ms(self.longest) @@ -382,6 +443,38 @@ mod tests { } } + /// A copy the platform refuses, recording nothing (on the rt platform, metal2vulkan-runtime's + /// refusal, counted under its kind), writes no frame: the command buffer is not committed, so + /// nothing is submitted and the frame's fill answers refused, for its frame to fail; the next + /// frame's copy is written as any. + #[test] + fn a_copy_the_platform_refuses_writes_no_frame_and_submits_nothing() { + use reims_vgpu_vulkan::core::testkit::{CompleteOn, Step}; + const COPY: &str = "copyFromBuffer:sourceOffset:toBuffer:destinationOffset:size:"; + let p = FakePlatform::new(); + p.world().set_auto_complete(Some(CompleteOn::Caller)); + let queue = p + .device() + .new_command_queue() + .expect("the fake makes queues"); + let input = (p.device().new_buffer_of_length(OnFault::Return, 1 << 16, 0)) + .ok() + .flatten() + .expect("the fake makes buffers"); + let (luma, chroma) = (plane(&p, 0, 128), plane(&p, 0, 128)); + let copies = [(&luma, 0, 4), (&chroma, 4 * 128, 2)]; + p.script().push(COPY, Step::Nil); + let refused = copy_frame::(&queue, &input, &copies, 128, 100); + assert!(matches!(refused, Copied::Refused(_)), "{refused:?}"); + assert_eq!(p.call_log().count(COPY), 1, "the refused copy alone"); + assert_eq!(p.call_log().count("commit"), 0, "nothing submitted"); + + let next = copy_frame::(&queue, &input, &copies, 128, 100); + assert_eq!(next, Copied::Written); + assert_eq!(p.call_log().count(COPY), 3, "the next frame's two copies"); + assert_eq!(p.call_log().count("commit"), 1); + } + /// A plane at the input's pitch is one copy, from its first row to its last row's last byte; /// one at another pitch is a copy a row, of the row's bytes alone. #[test]