vk-video-ffi: defer ring store copy and chain it into the next readback
Browse files at this commit9c38a593e9aa746fe46e3c8253c69afcbbd4f5abParent: e12d32f1 changed file +44 -7
shims/vk-video-ffi/src/session.rsmodified+44 -7
| @@ -1373,6 +1373,7 @@ pub(crate) struct Decoder { | |||
| 1373 | rgb: Option<RgbPass>, | 1373 | rgb: Option<RgbPass>, |
| 1374 | pub(crate) coded: bool, | 1374 | pub(crate) coded: bool, |
| 1375 | ring: Vec<RingSlot>, | 1375 | ring: Vec<RingSlot>, |
| 1376 | + pending: Option<(usize, (u32, u32, u32, u32))>, | ||
| 1376 | } | 1377 | } |
| 1377 | 1378 | ||
| 1378 | const RING_EXTRA: u32 = 8; | 1379 | const RING_EXTRA: u32 = 8; |
| @@ -1415,6 +1416,7 @@ impl Decoder { | |||
| 1415 | rgb: None, | 1416 | rgb: None, |
| 1416 | coded: false, | 1417 | coded: false, |
| 1417 | ring: Vec::new(), | 1418 | ring: Vec::new(), |
| 1419 | + pending: None, | ||
| 1418 | }; | 1420 | }; |
| 1419 | let mut list = vk::VideoProfileListInfoKHR::builder() | 1421 | let mut list = vk::VideoProfileListInfoKHR::builder() |
| 1420 | .profiles(std::slice::from_ref(profile)) | 1422 | .profiles(std::slice::from_ref(profile)) |
| @@ -1496,6 +1498,7 @@ impl Decoder { | |||
| 1496 | begin_slots: &[vk::VideoReferenceSlotInfoKHR], | 1498 | begin_slots: &[vk::VideoReferenceSlotInfoKHR], |
| 1497 | decode: &vk::VideoDecodeInfoKHR, | 1499 | decode: &vk::VideoDecodeInfoKHR, |
| 1498 | ) -> Result<(), Error> { | 1500 | ) -> Result<(), Error> { |
| 1501 | + self.flush_store()?; | ||
| 1499 | let gpu = self.vs.gpu.clone(); | 1502 | let gpu = self.vs.gpu.clone(); |
| 1500 | let d = gpu.device(); | 1503 | let d = gpu.device(); |
| 1501 | let cb = self.decode.cb; | 1504 | let cb = self.decode.cb; |
| @@ -1585,18 +1588,44 @@ impl Decoder { | |||
| 1585 | /// 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. | 1588 | /// 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. |
| 1586 | pub(crate) fn store(&mut self, slot: usize, crop: (u32, u32, u32, u32)) -> Result<(), Error> { | 1589 | pub(crate) fn store(&mut self, slot: usize, crop: (u32, u32, u32, u32)) -> Result<(), Error> { |
| 1587 | self.check_window(crop)?; | 1590 | 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 | + }; | ||
| 1589 | let target = self.slot_image(slot)?; | 1601 | 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; | ||
| 1590 | let gpu = self.vs.gpu.clone(); | 1621 | let gpu = self.vs.gpu.clone(); |
| 1591 | let d = gpu.device(); | 1622 | let d = gpu.device(); |
| 1592 | - let cb = self.graphics.cb; | ||
| 1593 | let owner = ring_owner(&self.graphics); | 1623 | let owner = ring_owner(&self.graphics); |
| 1594 | let source = self.output.image; | 1624 | let source = self.output.image; |
| 1595 | let regions = [ | 1625 | let regions = [ |
| 1596 | plane_copy(PLANE_0, (x, y), (width, height)), | 1626 | plane_copy(PLANE_0, (x, y), (width, height)), |
| 1597 | plane_copy(PLANE_1, (x / 2, y / 2), (width / 2, height / 2)), | 1627 | plane_copy(PLANE_1, (x / 2, y / 2), (width / 2, height / 2)), |
| 1598 | ]; | 1628 | ]; |
| 1599 | - self.graphics.begin()?; | ||
| 1600 | unsafe { | 1629 | unsafe { |
| 1601 | transfer( | 1630 | transfer( |
| 1602 | d, | 1631 | d, |
| @@ -1675,8 +1704,6 @@ impl Decoder { | |||
| 1675 | ), | 1704 | ), |
| 1676 | ); | 1705 | ); |
| 1677 | } | 1706 | } |
| 1678 | - self.graphics.end()?; | ||
| 1679 | - self.graphics.submit() | ||
| 1680 | } | 1707 | } |
| 1681 | 1708 | ||
| 1682 | /// Copies the top-left width x height of ring slot `slot` out as NV12 or P010 planes (Y, interleaved UV). | 1709 | /// 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 { | |||
| 1696 | copy_region(0, PLANE_0, width, height), | 1723 | copy_region(0, PLANE_0, width, height), |
| 1697 | copy_region(luma as u64, PLANE_1, width / 2, height / 2), | 1724 | copy_region(luma as u64, PLANE_1, width / 2, height / 2), |
| 1698 | ]; | 1725 | ]; |
| 1726 | + let store = self.take_store(slot); | ||
| 1699 | self.graphics.begin()?; | 1727 | 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); | ||
| 1701 | self.graphics.end()?; | 1733 | self.graphics.end()?; |
| 1702 | self.graphics.submit()?; | 1734 | self.graphics.submit()?; |
| 1703 | let bytes = self.readback.bytes(d, luma * 3 / 2)?; | 1735 | let bytes = self.readback.bytes(d, luma * 3 / 2)?; |
| @@ -1718,6 +1750,7 @@ impl Decoder { | |||
| 1718 | if out.0 == 0 || out.1 == 0 || out.0 > width || out.1 > height { | 1750 | if out.0 == 0 || out.1 == 0 || out.0 > width || out.1 > height { |
| 1719 | return Err(Error::Invalid("RGB readback size".into())); | 1751 | return Err(Error::Invalid("RGB readback size".into())); |
| 1720 | } | 1752 | } |
| 1753 | + let store = self.take_store(slot); | ||
| 1721 | let target = self.slot_image(slot)?; | 1754 | let target = self.slot_image(slot)?; |
| 1722 | let pass = self | 1755 | let pass = self |
| 1723 | .rgb | 1756 | .rgb |
| @@ -1733,6 +1766,9 @@ impl Decoder { | |||
| 1733 | copy_rect(luma as u64, PLANE_1, (x / 2, y / 2), (width / 2, height / 2)), | 1766 | copy_rect(luma as u64, PLANE_1, (x / 2, y / 2), (width / 2, height / 2)), |
| 1734 | ]; | 1767 | ]; |
| 1735 | self.graphics.begin()?; | 1768 | self.graphics.begin()?; |
| 1769 | + if let Some(crop) = store { | ||
| 1770 | + self.record_store(cb, target, crop); | ||
| 1771 | + } | ||
| 1736 | self.slot_to_buffer(d, cb, target, self.planes.buffer, &copies); | 1772 | self.slot_to_buffer(d, cb, target, self.planes.buffer, &copies); |
| 1737 | let barrier = vk::BufferMemoryBarrier2::builder() | 1773 | let barrier = vk::BufferMemoryBarrier2::builder() |
| 1738 | .src_stage_mask(vk::PipelineStageFlags2::COPY) | 1774 | .src_stage_mask(vk::PipelineStageFlags2::COPY) |
| @@ -1771,11 +1807,12 @@ impl Decoder { | |||
| 1771 | /// 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. | 1807 | /// 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. |
| 1772 | #[cfg(target_os = "linux")] | 1808 | #[cfg(target_os = "linux")] |
| 1773 | pub(crate) fn export( | 1809 | pub(crate) fn export( |
| 1774 | - &self, | 1810 | + &mut self, |
| 1775 | slot: usize, | 1811 | slot: usize, |
| 1776 | (width, height): (u32, u32), | 1812 | (width, height): (u32, u32), |
| 1777 | ) -> Result<crate::dmabuf::DmaBufExport, Error> { | 1813 | ) -> Result<crate::dmabuf::DmaBufExport, Error> { |
| 1778 | self.check_size((width, height))?; | 1814 | self.check_size((width, height))?; |
| 1815 | + self.flush_store()?; | ||
| 1779 | let ring = self | 1816 | let ring = self |
| 1780 | .ring | 1817 | .ring |
| 1781 | .get(slot) | 1818 | .get(slot) |