From 9c38a593e9aa746fe46e3c8253c69afcbbd4f5ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 25 Nov 2026 15:56:11 +0300 Subject: [PATCH] vk-video-ffi: defer ring store copy and chain it into the next readback store records the copy as pending; the next decode, export or readback of another slot flushes it, a readback of the same slot records it in the same graphics submission, so each picture takes one graphics submit and wait instead of two. --- shims/vk-video-ffi/src/session.rs | 51 ++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/shims/vk-video-ffi/src/session.rs b/shims/vk-video-ffi/src/session.rs index a9cd1185..7df1a819 100644 --- a/shims/vk-video-ffi/src/session.rs +++ b/shims/vk-video-ffi/src/session.rs @@ -1373,6 +1373,7 @@ pub(crate) struct Decoder { rgb: Option, pub(crate) coded: bool, ring: Vec, + pending: Option<(usize, (u32, u32, u32, u32))>, } const RING_EXTRA: u32 = 8; @@ -1415,6 +1416,7 @@ impl Decoder { rgb: None, coded: false, ring: Vec::new(), + pending: None, }; let mut list = vk::VideoProfileListInfoKHR::builder() .profiles(std::slice::from_ref(profile)) @@ -1496,6 +1498,7 @@ impl Decoder { begin_slots: &[vk::VideoReferenceSlotInfoKHR], decode: &vk::VideoDecodeInfoKHR, ) -> Result<(), Error> { + self.flush_store()?; let gpu = self.vs.gpu.clone(); let d = gpu.device(); let cb = self.decode.cb; @@ -1585,18 +1588,44 @@ impl Decoder { /// Copies the crop window `(x, y, width, height)` of the last decoded picture into ring slot `slot`, where it stays until that slot is stored into again. pub(crate) fn store(&mut self, slot: usize, crop: (u32, u32, u32, u32)) -> Result<(), Error> { self.check_window(crop)?; - let (x, y, width, height) = crop; + self.slot_image(slot)?; + self.flush_store()?; + self.pending = Some((slot, crop)); + Ok(()) + } + + fn flush_store(&mut self) -> Result<(), Error> { + let Some((slot, crop)) = self.pending.take() else { + return Ok(()); + }; let target = self.slot_image(slot)?; + let cb = self.graphics.cb; + self.graphics.begin()?; + self.record_store(cb, target, crop); + self.graphics.end()?; + self.graphics.submit() + } + + fn take_store(&mut self, slot: usize) -> Option<(u32, u32, u32, u32)> { + match self.pending { + Some((s, crop)) if s == slot => { + self.pending = None; + Some(crop) + } + _ => None, + } + } + + fn record_store(&self, cb: vk::CommandBuffer, target: vk::Image, crop: (u32, u32, u32, u32)) { + let (x, y, width, height) = crop; let gpu = self.vs.gpu.clone(); let d = gpu.device(); - let cb = self.graphics.cb; let owner = ring_owner(&self.graphics); let source = self.output.image; let regions = [ plane_copy(PLANE_0, (x, y), (width, height)), plane_copy(PLANE_1, (x / 2, y / 2), (width / 2, height / 2)), ]; - self.graphics.begin()?; unsafe { transfer( d, @@ -1675,8 +1704,6 @@ impl Decoder { ), ); } - self.graphics.end()?; - self.graphics.submit() } /// Copies the top-left width x height of ring slot `slot` out as NV12 or P010 planes (Y, interleaved UV). @@ -1696,8 +1723,13 @@ impl Decoder { copy_region(0, PLANE_0, width, height), copy_region(luma as u64, PLANE_1, width / 2, height / 2), ]; + let store = self.take_store(slot); self.graphics.begin()?; - self.slot_to_buffer(d, self.graphics.cb, target, self.readback.buffer, &copies); + let cb = self.graphics.cb; + if let Some(crop) = store { + self.record_store(cb, target, crop); + } + self.slot_to_buffer(d, cb, target, self.readback.buffer, &copies); self.graphics.end()?; self.graphics.submit()?; let bytes = self.readback.bytes(d, luma * 3 / 2)?; @@ -1718,6 +1750,7 @@ impl Decoder { if out.0 == 0 || out.1 == 0 || out.0 > width || out.1 > height { return Err(Error::Invalid("RGB readback size".into())); } + let store = self.take_store(slot); let target = self.slot_image(slot)?; let pass = self .rgb @@ -1733,6 +1766,9 @@ impl Decoder { copy_rect(luma as u64, PLANE_1, (x / 2, y / 2), (width / 2, height / 2)), ]; self.graphics.begin()?; + if let Some(crop) = store { + self.record_store(cb, target, crop); + } self.slot_to_buffer(d, cb, target, self.planes.buffer, &copies); let barrier = vk::BufferMemoryBarrier2::builder() .src_stage_mask(vk::PipelineStageFlags2::COPY) @@ -1771,11 +1807,12 @@ impl Decoder { /// The DMA-BUF of ring slot `slot`, `width` x `height` from its top-left: a second handle on the slot's memory, which the same slot returns every time. #[cfg(target_os = "linux")] pub(crate) fn export( - &self, + &mut self, slot: usize, (width, height): (u32, u32), ) -> Result { self.check_size((width, height))?; + self.flush_store()?; let ring = self .ring .get(slot)