forge
Explore
Log in

Don't you dare go Hollow

vk-video-ffi: defer ring store copy and chain it into the next readback

Browse files at this commit

9c38a593e9aa746fe46e3c8253c69afcbbd4f5abAhmet Kaan GümüşParent: e12d32f
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.rsmodified+44 -7
@@ -1373,6 +1373,7 @@ pub(crate) struct Decoder {
13731373 rgb: Option<RgbPass>,
13741374 pub(crate) coded: bool,
13751375 ring: Vec<RingSlot>,
1376+ pending: Option<(usize, (u32, u32, u32, u32))>,
13761377 }
13771378
13781379 const RING_EXTRA: u32 = 8;
@@ -1415,6 +1416,7 @@ impl Decoder {
14151416 rgb: None,
14161417 coded: false,
14171418 ring: Vec::new(),
1419+ pending: None,
14181420 };
14191421 let mut list = vk::VideoProfileListInfoKHR::builder()
14201422 .profiles(std::slice::from_ref(profile))
@@ -1496,6 +1498,7 @@ impl Decoder {
14961498 begin_slots: &[vk::VideoReferenceSlotInfoKHR],
14971499 decode: &vk::VideoDecodeInfoKHR,
14981500 ) -> Result<(), Error> {
1501+ self.flush_store()?;
14991502 let gpu = self.vs.gpu.clone();
15001503 let d = gpu.device();
15011504 let cb = self.decode.cb;
@@ -1585,18 +1588,44 @@ impl Decoder {
15851588 /// 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.
15861589 pub(crate) fn store(&mut self, slot: usize, crop: (u32, u32, u32, u32)) -> Result<(), Error> {
15871590 self.check_window(crop)?;
1588- let (x, y, width, height) = crop;
1591+ self.slot_image(slot)?;
1592+ self.flush_store()?;
1593+ self.pending = Some((slot, crop));
1594+ Ok(())
1595+ }
1596+
1597+ fn flush_store(&mut self) -> Result<(), Error> {
1598+ let Some((slot, crop)) = self.pending.take() else {
1599+ return Ok(());
1600+ };
15891601 let target = self.slot_image(slot)?;
1602+ let cb = self.graphics.cb;
1603+ self.graphics.begin()?;
1604+ self.record_store(cb, target, crop);
1605+ self.graphics.end()?;
1606+ self.graphics.submit()
1607+ }
1608+
1609+ fn take_store(&mut self, slot: usize) -> Option<(u32, u32, u32, u32)> {
1610+ match self.pending {
1611+ Some((s, crop)) if s == slot => {
1612+ self.pending = None;
1613+ Some(crop)
1614+ }
1615+ _ => None,
1616+ }
1617+ }
1618+
1619+ fn record_store(&self, cb: vk::CommandBuffer, target: vk::Image, crop: (u32, u32, u32, u32)) {
1620+ let (x, y, width, height) = crop;
15901621 let gpu = self.vs.gpu.clone();
15911622 let d = gpu.device();
1592- let cb = self.graphics.cb;
15931623 let owner = ring_owner(&self.graphics);
15941624 let source = self.output.image;
15951625 let regions = [
15961626 plane_copy(PLANE_0, (x, y), (width, height)),
15971627 plane_copy(PLANE_1, (x / 2, y / 2), (width / 2, height / 2)),
15981628 ];
1599- self.graphics.begin()?;
16001629 unsafe {
16011630 transfer(
16021631 d,
@@ -1675,8 +1704,6 @@ impl Decoder {
16751704 ),
16761705 );
16771706 }
1678- self.graphics.end()?;
1679- self.graphics.submit()
16801707 }
16811708
16821709 /// 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 {
16961723 copy_region(0, PLANE_0, width, height),
16971724 copy_region(luma as u64, PLANE_1, width / 2, height / 2),
16981725 ];
1726+ let store = self.take_store(slot);
16991727 self.graphics.begin()?;
1700- self.slot_to_buffer(d, self.graphics.cb, target, self.readback.buffer, &copies);
1728+ let cb = self.graphics.cb;
1729+ if let Some(crop) = store {
1730+ self.record_store(cb, target, crop);
1731+ }
1732+ self.slot_to_buffer(d, cb, target, self.readback.buffer, &copies);
17011733 self.graphics.end()?;
17021734 self.graphics.submit()?;
17031735 let bytes = self.readback.bytes(d, luma * 3 / 2)?;
@@ -1718,6 +1750,7 @@ impl Decoder {
17181750 if out.0 == 0 || out.1 == 0 || out.0 > width || out.1 > height {
17191751 return Err(Error::Invalid("RGB readback size".into()));
17201752 }
1753+ let store = self.take_store(slot);
17211754 let target = self.slot_image(slot)?;
17221755 let pass = self
17231756 .rgb
@@ -1733,6 +1766,9 @@ impl Decoder {
17331766 copy_rect(luma as u64, PLANE_1, (x / 2, y / 2), (width / 2, height / 2)),
17341767 ];
17351768 self.graphics.begin()?;
1769+ if let Some(crop) = store {
1770+ self.record_store(cb, target, crop);
1771+ }
17361772 self.slot_to_buffer(d, cb, target, self.planes.buffer, &copies);
17371773 let barrier = vk::BufferMemoryBarrier2::builder()
17381774 .src_stage_mask(vk::PipelineStageFlags2::COPY)
@@ -1771,11 +1807,12 @@ impl Decoder {
17711807 /// 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.
17721808 #[cfg(target_os = "linux")]
17731809 pub(crate) fn export(
1774- &self,
1810+ &mut self,
17751811 slot: usize,
17761812 (width, height): (u32, u32),
17771813 ) -> Result<crate::dmabuf::DmaBufExport, Error> {
17781814 self.check_size((width, height))?;
1815+ self.flush_store()?;
17791816 let ring = self
17801817 .ring
17811818 .get(slot)