forge
Explore
Log in

Don't you dare go Hollow

vk-video-ffi: two-object DMA-BUF import via single-plane images and internal NV12/P010 copy

Browse files at this commit

ba754501e82f2a8edb70da4e57a214e72dc5fb45Ahmet Kaan GümüşParent: 445bf8f
Drop DISJOINT (RADV advertises no DISJOINT for DRM modifiers). Planes from two
objects import as R8/R8G8 (P010: R16/R16G16) single-plane images, one bind each,
then copy into an internal NV12/P010 image that the encoder reads like a host upload.
Unsupported when the modifier lacks single-plane formats. Smoke example adds split
NV12/P010 cases (linear and tiled) and a two-pass cache check.
shims/vk-video-ffi/examples/dmabuf_smoke.rsmodified+91 -2
@@ -540,12 +540,53 @@ fn run<S: Encode>(gpu: &Gpu, name: &str, host: &[u8], linear: &Export, tiled: &E
540540 }
541541 }
542542
543-fn run_planar<S: Encode>(gpu: &Gpu, name: &str, pattern: &Pattern, exports: &[&Export]) {
544- let format = if pattern.bits == 8 {
543+fn planar_format(pattern: &Pattern) -> DmaBufFormat {
544+ if pattern.bits == 8 {
545545 DmaBufFormat::Nv12
546546 } else {
547547 DmaBufFormat::P010
548+ }
549+}
550+
551+fn split_planes(a: &Export, b: &Export) -> [DmaBufPlane; 2] {
552+ [
553+ DmaBufPlane { fd: 0, ..a.planes[0] },
554+ DmaBufPlane { fd: 1, ..b.planes[1] },
555+ ]
556+}
557+
558+fn run_split<S: Encode>(gpu: &Gpu, name: &str, pattern: &Pattern, pairs: &[(Export, Export)]) {
559+ let format = planar_format(pattern);
560+ for (a_export, b_export) in pairs {
561+ let mut a = S::open(gpu, (W, H), pattern.bits).expect("encoder");
562+ let mut b = S::open(gpu, (W, H), pattern.bits).expect("encoder");
563+ let fds = [a_export.fd.as_fd(), b_export.fd.as_fd()];
564+ let planes = split_planes(a_export, b_export);
565+ let src = DmaBuf {
566+ fds: &fds,
567+ planes: &planes,
568+ width: W,
569+ height: H,
570+ modifier: a_export.modifier,
571+ format,
548572 };
573+ let mut left = Vec::new();
574+ let mut right = Vec::new();
575+ for i in 0..FRAMES {
576+ left.extend(a.planar(pattern, i).expect("encode"));
577+ right.extend(b.dma(&src, i).expect("encode_dmabuf"));
578+ }
579+ let verdict = if left == right {
580+ "identical"
581+ } else {
582+ "DIFFERENT"
583+ };
584+ println!("{name} split {:#x} {W}x{H} {verdict}", a_export.modifier);
585+ }
586+}
587+
588+fn run_planar<S: Encode>(gpu: &Gpu, name: &str, pattern: &Pattern, exports: &[&Export]) {
589+ let format = planar_format(pattern);
549590 for export in exports {
550591 let mut a = S::open(gpu, (W, H), pattern.bits).expect("encoder");
551592 let mut b = S::open(gpu, (W, H), pattern.bits).expect("encoder");
@@ -636,6 +677,50 @@ fn main() {
636677 run_planar::<Av1EncodeSession>(&gpu, "av1 nv12", &nv12, &nv12_set);
637678 run_planar::<Av1EncodeSession>(&gpu, "av1 p010", &p010, &p010_set);
638679
680+ let pair = |format, pattern: &Pattern, modifier| {
681+ (
682+ export_planar(instance, physical, d, queue, family, format, modifier, pattern),
683+ export_planar(instance, physical, d, queue, family, format, modifier, pattern),
684+ )
685+ };
686+ let nv12_pairs: Vec<(Export, Export)> = [Some(0), pick_modifier(instance, physical, NV12, 2)]
687+ .into_iter()
688+ .flatten()
689+ .map(|m| pair(NV12, &nv12, m))
690+ .collect();
691+ let p010_pairs: Vec<(Export, Export)> = [Some(0), pick_modifier(instance, physical, P010, 2)]
692+ .into_iter()
693+ .flatten()
694+ .map(|m| pair(P010, &p010, m))
695+ .collect();
696+ run_split::<H264EncodeSession>(&gpu, "h264 nv12", &nv12, &nv12_pairs);
697+ run_split::<H265EncodeSession>(&gpu, "h265 nv12", &nv12, &nv12_pairs);
698+ run_split::<H265EncodeSession>(&gpu, "h265 p010", &p010, &p010_pairs);
699+ run_split::<Av1EncodeSession>(&gpu, "av1 nv12", &nv12, &nv12_pairs);
700+ run_split::<Av1EncodeSession>(&gpu, "av1 p010", &p010, &p010_pairs);
701+
702+ let mut split_cache = <H265EncodeSession as Encode>::open(&gpu, (W, H), 8).expect("encoder");
703+ let (sa, sb) = &nv12_pairs[0];
704+ let fds = [sa.fd.as_fd(), sb.fd.as_fd()];
705+ let planes = split_planes(sa, sb);
706+ let src = DmaBuf {
707+ fds: &fds,
708+ planes: &planes,
709+ width: W,
710+ height: H,
711+ modifier: sa.modifier,
712+ format: DmaBufFormat::Nv12,
713+ };
714+ for pass in 1..=2 {
715+ for i in 0..FRAMES {
716+ split_cache.dma(&src, i).expect("encode_dmabuf");
717+ }
718+ println!(
719+ "cache h265 nv12 split pass {pass} {FRAMES} frames same fds imports {}",
720+ split_cache.dmabuf_imports()
721+ );
722+ }
723+
639724 let mut cache = <H265EncodeSession as Encode>::open(&gpu, (W, H), 8).expect("encoder");
640725 let fds = [nv12_linear.fd.as_fd()];
641726 let src = DmaBuf {
@@ -661,4 +746,8 @@ fn main() {
661746 for e in &exports {
662747 e.destroy(d);
663748 }
749+ for (a, b) in nv12_pairs.iter().chain(&p010_pairs) {
750+ a.destroy(d);
751+ b.destroy(d);
752+ }
664753 }
shims/vk-video-ffi/src/dmabuf.rsmodified+149 -133
@@ -217,6 +217,7 @@ impl Key {
217217
218218 pub(crate) struct Import {
219219 pub(crate) image: vk::Image,
220+ planes: Option<[vk::Image; 2]>,
220221 id: u64,
221222 memories: Vec<vk::DeviceMemory>,
222223 }
@@ -225,15 +226,92 @@ impl Import {
225226 fn new(gpu: &Gpu, src: &DmaBuf, id: u64) -> Result<Self, Error> {
226227 check(gpu)?;
227228 validate(src)?;
229+ let mut images = Vec::new();
230+ let mut memories = Vec::new();
231+ match build(gpu, src, &mut images, &mut memories) {
232+ Ok((image, planes)) => Ok(Import {
233+ image,
234+ planes,
235+ id,
236+ memories,
237+ }),
238+ Err(e) => {
228239 let d = gpu.device();
240+ for m in &memories {
241+ unsafe { d.free_memory(*m, None) };
242+ }
243+ for i in &images {
244+ unsafe { d.destroy_image(*i, None) };
245+ }
246+ Err(e)
247+ }
248+ }
249+ }
250+
251+ fn destroy(&self, d: &Device) {
252+ unsafe {
253+ d.destroy_image(self.image, None);
254+ for p in self.planes.iter().flatten() {
255+ d.destroy_image(*p, None);
256+ }
257+ for m in &self.memories {
258+ d.free_memory(*m, None);
259+ }
260+ }
261+ }
262+}
263+
264+fn build(
265+ gpu: &Gpu,
266+ src: &DmaBuf,
267+ images: &mut Vec<vk::Image>,
268+ memories: &mut Vec<vk::DeviceMemory>,
269+) -> Result<(vk::Image, Option<[vk::Image; 2]>), Error> {
270+ if src.fds.len() == 1 {
229271 let format = match src.format {
230272 DmaBufFormat::Bgrx => vk::Format::B8G8R8A8_UNORM,
231273 DmaBufFormat::Rgbx => vk::Format::R8G8B8A8_UNORM,
232274 DmaBufFormat::Nv12 => NV12,
233275 DmaBufFormat::P010 => P010,
234276 };
235- let layouts: Vec<vk::SubresourceLayout> = src
236- .planes
277+ let image = explicit_image(gpu, format, (src.width, src.height), src.modifier, src.planes)?;
278+ images.push(image);
279+ memories.push(import_memory(gpu, image, src.fds[0])?);
280+ return Ok((image, None));
281+ }
282+ let (plane_format, chroma_format, internal_format) = match src.format {
283+ DmaBufFormat::P010 => (vk::Format::R16_UNORM, vk::Format::R16G16_UNORM, P010),
284+ _ => (vk::Format::R8_UNORM, vk::Format::R8G8_UNORM, NV12),
285+ };
286+ if !single_plane(gpu, plane_format, src.modifier)
287+ || !single_plane(gpu, chroma_format, src.modifier)
288+ {
289+ return Err(Error::Unsupported(
290+ "DMA-BUF two-object import: modifier lacks single-plane formats".into(),
291+ ));
292+ }
293+ let (w, h) = (src.width, src.height);
294+ let y = explicit_image(gpu, plane_format, (w, h), src.modifier, &src.planes[..1])?;
295+ images.push(y);
296+ memories.push(import_memory(gpu, y, src.fds[0])?);
297+ let uv = explicit_image(gpu, chroma_format, (w / 2, h / 2), src.modifier, &src.planes[1..])?;
298+ images.push(uv);
299+ memories.push(import_memory(gpu, uv, src.fds[1])?);
300+ let internal = device_image(gpu, internal_format, (w, h))?;
301+ images.push(internal);
302+ memories.push(device_memory(gpu, internal)?);
303+ Ok((internal, Some([y, uv])))
304+}
305+
306+fn explicit_image(
307+ gpu: &Gpu,
308+ format: vk::Format,
309+ (width, height): (u32, u32),
310+ modifier: u64,
311+ planes: &[DmaBufPlane],
312+) -> Result<vk::Image, Error> {
313+ let d = gpu.device();
314+ let layouts: Vec<vk::SubresourceLayout> = planes
237315 .iter()
238316 .map(|p| vk::SubresourceLayout {
239317 offset: p.offset as u64,
@@ -244,24 +322,18 @@ impl Import {
244322 })
245323 .collect();
246324 let mut explicit = vk::ImageDrmFormatModifierExplicitCreateInfoEXT::builder()
247- .drm_format_modifier(src.modifier)
325+ .drm_format_modifier(modifier)
248326 .plane_layouts(&layouts)
249327 .build();
250328 let mut external = vk::ExternalMemoryImageCreateInfo::builder()
251329 .handle_types(vk::ExternalMemoryHandleTypeFlags::DMA_BUF_EXT)
252330 .build();
253- let flags = if src.fds.len() == 2 {
254- vk::ImageCreateFlags::DISJOINT
255- } else {
256- vk::ImageCreateFlags::empty()
257- };
258331 let info = vk::ImageCreateInfo::builder()
259- .flags(flags)
260332 .image_type(vk::ImageType::_2D)
261333 .format(format)
262334 .extent(vk::Extent3D {
263- width: src.width,
264- height: src.height,
335+ width,
336+ height,
265337 depth: 1,
266338 })
267339 .mip_levels(1)
@@ -274,24 +346,42 @@ impl Import {
274346 .push_next(&mut external)
275347 .push_next(&mut explicit)
276348 .build();
277- let image = unsafe { vk(d.create_image(&info, None), "vkCreateImage")? };
278- match bind(gpu, image, src) {
279- Ok(memories) => Ok(Import { image, id, memories }),
280- Err(e) => {
281- unsafe { d.destroy_image(image, None) };
282- Err(e)
283- }
284- }
349+ unsafe { vk(d.create_image(&info, None), "vkCreateImage") }
285350 }
286351
287- fn destroy(&self, d: &Device) {
288- unsafe {
289- d.destroy_image(self.image, None);
290- for m in &self.memories {
291- d.free_memory(*m, None);
292- }
352+fn device_image(gpu: &Gpu, format: vk::Format, (width, height): (u32, u32)) -> Result<vk::Image, Error> {
353+ let info = vk::ImageCreateInfo::builder()
354+ .image_type(vk::ImageType::_2D)
355+ .format(format)
356+ .extent(vk::Extent3D {
357+ width,
358+ height,
359+ depth: 1,
360+ })
361+ .mip_levels(1)
362+ .array_layers(1)
363+ .samples(vk::SampleCountFlags::_1)
364+ .tiling(vk::ImageTiling::OPTIMAL)
365+ .usage(vk::ImageUsageFlags::TRANSFER_SRC | vk::ImageUsageFlags::TRANSFER_DST)
366+ .sharing_mode(vk::SharingMode::EXCLUSIVE)
367+ .initial_layout(vk::ImageLayout::UNDEFINED);
368+ unsafe { vk(gpu.device().create_image(&info, None), "vkCreateImage") }
293369 }
370+
371+fn device_memory(gpu: &Gpu, image: vk::Image) -> Result<vk::DeviceMemory, Error> {
372+ let d = gpu.device();
373+ let req = unsafe { d.get_image_memory_requirements(image) };
374+ let index = memory_type(gpu, req.memory_type_bits, vk::MemoryPropertyFlags::DEVICE_LOCAL)?;
375+ let alloc = vk::MemoryAllocateInfo::builder()
376+ .allocation_size(req.size)
377+ .memory_type_index(index)
378+ .build();
379+ let memory = unsafe { vk(d.allocate_memory(&alloc, None), "vkAllocateMemory")? };
380+ if let Err(e) = unsafe { vk(d.bind_image_memory(image, memory, 0), "vkBindImageMemory") } {
381+ unsafe { d.free_memory(memory, None) };
382+ return Err(e);
294383 }
384+ Ok(memory)
295385 }
296386
297387 #[derive(Default)]
@@ -302,7 +392,12 @@ pub(crate) struct ImportCache {
302392 }
303393
304394 impl ImportCache {
305- pub(crate) fn get(&mut self, gpu: &Gpu, src: &DmaBuf, busy: &[u64]) -> Result<(vk::Image, u64), Error> {
395+ pub(crate) fn get(
396+ &mut self,
397+ gpu: &Gpu,
398+ src: &DmaBuf,
399+ busy: &[u64],
400+ ) -> Result<(vk::Image, Option<[vk::Image; 2]>, u64), Error> {
306401 validate(src)?;
307402 let key = Key::new(src)?;
308403 match self.entries.iter().position(|(k, _)| *k == key) {
@@ -328,7 +423,7 @@ impl ImportCache {
328423 }
329424 self.entries
330425 .last()
331- .map(|(_, import)| (import.image, import.id))
426+ .map(|(_, import)| (import.image, import.planes, import.id))
332427 .ok_or_else(|| Error::Invalid("DMA-BUF import cache".into()))
333428 }
334429
@@ -339,28 +434,9 @@ impl ImportCache {
339434 }
340435 }
341436
342-fn allocate(
343- gpu: &Gpu,
344- image: vk::Image,
345- plane: Option<vk::ImageAspectFlags>,
346- fd: BorrowedFd,
347-) -> Result<vk::DeviceMemory, Error> {
437+fn import_memory(gpu: &Gpu, image: vk::Image, fd: BorrowedFd) -> Result<vk::DeviceMemory, Error> {
348438 let d = gpu.device();
349- let req = match plane {
350- None => unsafe { d.get_image_memory_requirements(image) },
351- Some(aspect) => {
352- let mut plane_info = vk::ImagePlaneMemoryRequirementsInfo::builder()
353- .plane_aspect(aspect)
354- .build();
355- let info = vk::ImageMemoryRequirementsInfo2::builder()
356- .image(image)
357- .push_next(&mut plane_info)
358- .build();
359- let mut out = vk::MemoryRequirements2::default();
360- unsafe { d.get_image_memory_requirements2(&info, &mut out) };
361- out.memory_requirements
362- }
363- };
439+ let req = unsafe { d.get_image_memory_requirements(image) };
364440 let owned = fd
365441 .try_clone_to_owned()
366442 .map_err(|_| Error::Invalid("DMA-BUF descriptor".into()))?;
@@ -387,82 +463,22 @@ fn allocate(
387463 let mut dedicated = vk::MemoryDedicatedAllocateInfo::builder()
388464 .image(image)
389465 .build();
390- let alloc = if plane.is_some() {
391- vk::MemoryAllocateInfo::builder()
392- .allocation_size(req.size)
393- .memory_type_index(index)
394- .push_next(&mut import)
395- .build()
396- } else {
397- vk::MemoryAllocateInfo::builder()
466+ let alloc = vk::MemoryAllocateInfo::builder()
398467 .allocation_size(req.size)
399468 .memory_type_index(index)
400469 .push_next(&mut dedicated)
401470 .push_next(&mut import)
402- .build()
403- };
471+ .build();
404472 let memory = unsafe { vk(d.allocate_memory(&alloc, None), "vkAllocateMemory")? };
405473 let _ = owned.into_raw_fd();
406- Ok(memory)
407-}
408-
409-fn bind(gpu: &Gpu, image: vk::Image, src: &DmaBuf) -> Result<Vec<vk::DeviceMemory>, Error> {
410- let d = gpu.device();
411- let free = |memories: &[vk::DeviceMemory]| {
412- for m in memories {
413- unsafe { d.free_memory(*m, None) };
414- }
415- };
416- if src.fds.len() == 1 {
417- let memory = allocate(gpu, image, None, src.fds[0])?;
418- let bound = unsafe { vk(d.bind_image_memory(image, memory, 0), "vkBindImageMemory") };
419- if let Err(e) = bound {
420- free(&[memory]);
421- return Err(e);
422- }
423- return Ok(vec![memory]);
424- }
425- let aspects = [vk::ImageAspectFlags::PLANE_0, vk::ImageAspectFlags::PLANE_1];
426- let mut memories = Vec::with_capacity(2);
427- for (fd, aspect) in src.fds.iter().zip(aspects) {
428- match allocate(gpu, image, Some(aspect), *fd) {
429- Ok(memory) => memories.push(memory),
430- Err(e) => {
431- free(&memories);
432- return Err(e);
433- }
434- }
435- }
436- let mut plane_infos: Vec<vk::BindImagePlaneMemoryInfo> = aspects
437- .iter()
438- .map(|&plane_aspect| {
439- vk::BindImagePlaneMemoryInfo::builder()
440- .plane_aspect(plane_aspect)
441- .build()
442- })
443- .collect();
444- let binds: Vec<vk::BindImageMemoryInfo> = memories
445- .iter()
446- .zip(plane_infos.iter_mut())
447- .map(|(memory, info)| {
448- vk::BindImageMemoryInfo::builder()
449- .image(image)
450- .memory(*memory)
451- .memory_offset(0)
452- .push_next(info)
453- .build()
454- })
455- .collect();
456- let bound = unsafe { vk(d.bind_image_memory2(&binds), "vkBindImageMemory2") };
457- if let Err(e) = bound {
458- free(&memories);
474+ if let Err(e) = unsafe { vk(d.bind_image_memory(image, memory, 0), "vkBindImageMemory") } {
475+ unsafe { d.free_memory(memory, None) };
459476 return Err(e);
460477 }
461- Ok(memories)
478+ Ok(memory)
462479 }
463480
464-/// Every DRM format modifier that can be a transfer destination for `format`.
465-fn export_modifiers(gpu: &Gpu, format: vk::Format) -> Vec<u64> {
481+fn modifier_properties(gpu: &Gpu, format: vk::Format) -> Vec<vk::DrmFormatModifierPropertiesEXT> {
466482 let instance = gpu.instance();
467483 let mut count = vk::DrmFormatModifierPropertiesListEXT::builder().build();
468484 let mut props = vk::FormatProperties2::builder().push_next(&mut count).build();
@@ -474,7 +490,13 @@ fn export_modifiers(gpu: &Gpu, format: vk::Format) -> Vec<u64> {
474490 .build();
475491 let mut props = vk::FormatProperties2::builder().push_next(&mut full).build();
476492 unsafe { instance.get_physical_device_format_properties2(gpu.physical(), format, &mut props) };
477- list.iter()
493+ list
494+}
495+
496+/// Every DRM format modifier that can be a transfer destination for `format`.
497+fn export_modifiers(gpu: &Gpu, format: vk::Format) -> Vec<u64> {
498+ modifier_properties(gpu, format)
499+ .iter()
478500 .filter(|m| {
479501 m.drm_format_modifier_tiling_features
480502 .contains(vk::FormatFeatureFlags::TRANSFER_DST | vk::FormatFeatureFlags::TRANSFER_SRC)
@@ -483,6 +505,15 @@ fn export_modifiers(gpu: &Gpu, format: vk::Format) -> Vec<u64> {
483505 .collect()
484506 }
485507
508+fn single_plane(gpu: &Gpu, format: vk::Format, modifier: u64) -> bool {
509+ modifier_properties(gpu, format).iter().any(|m| {
510+ m.drm_format_modifier == modifier
511+ && m.drm_format_modifier_plane_count == 1
512+ && m.drm_format_modifier_tiling_features
513+ .contains(vk::FormatFeatureFlags::TRANSFER_SRC)
514+ })
515+}
516+
486517 /// A ring slot's DMA-BUF: the one memory object, exported once, and its plane layout.
487518 pub(crate) struct Export {
488519 pub(crate) fd: OwnedFd,
@@ -702,25 +733,10 @@ pub fn read_dmabuf(gpu: &Gpu, desc: &DmaBufExport) -> Result<(Vec<u8>, Vec<u8>),
702733 .push_next(&mut explicit)
703734 .build();
704735 let image = unsafe { vk(d.create_image(&info, None), "vkCreateImage")? };
705- let fds = [desc.fd.as_fd()];
706- let src = DmaBuf {
707- fds: &fds,
708- planes: &desc.planes,
709- width: w,
710- height: h,
711- modifier: desc.modifier,
712- format: if desc.p010 {
713- DmaBufFormat::P010
714- } else {
715- DmaBufFormat::Nv12
716- },
717- };
718- let result = match bind(gpu, image, &src) {
719- Ok(memories) => {
736+ let result = match import_memory(gpu, image, desc.fd.as_fd()) {
737+ Ok(memory) => {
720738 let out = read_image(gpu, image, (w, h), bytes);
721- for m in memories {
722- unsafe { d.free_memory(m, None) };
723- }
739+ unsafe { d.free_memory(memory, None) };
724740 out
725741 }
726742 Err(e) => Err(e),
shims/vk-video-ffi/src/session.rsmodified+104 -5
@@ -19,6 +19,8 @@ pub(crate) const P010: vk::Format = vk::Format::G10X6_B10X6R10X6_2PLANE_420_UNOR
1919 pub(crate) const DPB_SLOTS: usize = 2;
2020 pub(crate) const PLANE_0: vk::ImageAspectFlags = vk::ImageAspectFlags::PLANE_0;
2121 pub(crate) const PLANE_1: vk::ImageAspectFlags = vk::ImageAspectFlags::PLANE_1;
22+#[cfg(target_os = "linux")]
23+const COLOR: vk::ImageAspectFlags = vk::ImageAspectFlags::COLOR;
2224
2325 pub(crate) type Stage = (vk::ImageLayout, vk::PipelineStageFlags2, vk::AccessFlags2);
2426
@@ -231,6 +233,28 @@ pub(crate) fn plane_copy(
231233 }
232234 }
233235
236+#[cfg(target_os = "linux")]
237+fn split_copy(aspect: vk::ImageAspectFlags, (width, height): (u32, u32)) -> vk::ImageCopy {
238+ let layers = |aspect_mask| vk::ImageSubresourceLayers {
239+ aspect_mask,
240+ mip_level: 0,
241+ base_array_layer: 0,
242+ layer_count: 1,
243+ };
244+ let origin = vk::Offset3D { x: 0, y: 0, z: 0 };
245+ vk::ImageCopy {
246+ src_subresource: layers(COLOR),
247+ src_offset: origin,
248+ dst_subresource: layers(aspect),
249+ dst_offset: origin,
250+ extent: vk::Extent3D {
251+ width,
252+ height,
253+ depth: 1,
254+ },
255+ }
256+}
257+
234258 pub(crate) fn copy_region(
235259 offset: u64,
236260 aspect: vk::ImageAspectFlags,
@@ -1095,16 +1119,19 @@ impl Encoder {
10951119 DmaBufFormat::Nv12 | DmaBufFormat::P010 => self.check_planar(src)?,
10961120 }
10971121 let busy: Vec<u64> = self.slots.iter().filter_map(|s| s.import).collect();
1098- let (image, id) = self.imports.get(&gpu, src, &busy)?;
1122+ let (image, planes, id) = self.imports.get(&gpu, src, &busy)?;
10991123 let k = self.next;
1100- let result = match src.format {
1101- DmaBufFormat::Bgrx | DmaBufFormat::Rgbx => self.upload(Input::DmaBuf {
1124+ let result = match (src.format, planes) {
1125+ (DmaBufFormat::Bgrx | DmaBufFormat::Rgbx, _) => self.upload(Input::DmaBuf {
11021126 image,
11031127 width,
11041128 height,
11051129 bgr: src.format == DmaBufFormat::Bgrx,
11061130 }),
1107- DmaBufFormat::Nv12 | DmaBufFormat::P010 => self.copy_planes(image),
1131+ (_, Some(planes)) => self
1132+ .copy_split(planes, image)
1133+ .and_then(|()| self.copy_planes(image, false)),
1134+ (_, None) => self.copy_planes(image, true),
11081135 };
11091136 if result.is_ok() {
11101137 self.slots[k].import = Some(id);
@@ -1127,7 +1154,77 @@ impl Encoder {
11271154 }
11281155
11291156 #[cfg(target_os = "linux")]
1130- fn copy_planes(&mut self, image: vk::Image) -> Result<(), Error> {
1157+ fn copy_split(&mut self, planes: [vk::Image; 2], image: vk::Image) -> Result<(), Error> {
1158+ let gpu = self.vs.gpu.clone();
1159+ let d = gpu.device();
1160+ let (w, h) = (self.input.0 as u32, self.input.1 as u32);
1161+ let slot = &self.slots[self.next];
1162+ let cb = slot.upload.cb;
1163+ let family = slot.upload.family;
1164+ let none = vk::QUEUE_FAMILY_IGNORED;
1165+ slot.upload.begin()?;
1166+ acquire(d, cb, planes[0], &[COLOR], family);
1167+ acquire(d, cb, planes[1], &[COLOR], family);
1168+ transfer(
1169+ d,
1170+ cb,
1171+ image,
1172+ (
1173+ vk::ImageLayout::UNDEFINED,
1174+ vk::PipelineStageFlags2::NONE,
1175+ vk::AccessFlags2::NONE,
1176+ none,
1177+ ),
1178+ (
1179+ vk::ImageLayout::TRANSFER_DST_OPTIMAL,
1180+ vk::PipelineStageFlags2::COPY,
1181+ vk::AccessFlags2::TRANSFER_WRITE,
1182+ none,
1183+ ),
1184+ );
1185+ unsafe {
1186+ d.cmd_copy_image(
1187+ cb,
1188+ planes[0],
1189+ vk::ImageLayout::TRANSFER_SRC_OPTIMAL,
1190+ image,
1191+ vk::ImageLayout::TRANSFER_DST_OPTIMAL,
1192+ &[split_copy(PLANE_0, (w, h))],
1193+ );
1194+ d.cmd_copy_image(
1195+ cb,
1196+ planes[1],
1197+ vk::ImageLayout::TRANSFER_SRC_OPTIMAL,
1198+ image,
1199+ vk::ImageLayout::TRANSFER_DST_OPTIMAL,
1200+ &[split_copy(PLANE_1, (w / 2, h / 2))],
1201+ );
1202+ }
1203+ transfer(
1204+ d,
1205+ cb,
1206+ image,
1207+ (
1208+ vk::ImageLayout::TRANSFER_DST_OPTIMAL,
1209+ vk::PipelineStageFlags2::COPY,
1210+ vk::AccessFlags2::TRANSFER_WRITE,
1211+ none,
1212+ ),
1213+ (
1214+ vk::ImageLayout::TRANSFER_SRC_OPTIMAL,
1215+ vk::PipelineStageFlags2::COPY,
1216+ vk::AccessFlags2::TRANSFER_READ,
1217+ none,
1218+ ),
1219+ );
1220+ release(d, cb, planes[0], &[COLOR]);
1221+ release(d, cb, planes[1], &[COLOR]);
1222+ slot.upload.end()?;
1223+ slot.upload.submit()
1224+ }
1225+
1226+ #[cfg(target_os = "linux")]
1227+ fn copy_planes(&mut self, image: vk::Image, foreign: bool) -> Result<(), Error> {
11311228 let gpu = self.vs.gpu.clone();
11321229 let d = gpu.device();
11331230 let k = self.next;
@@ -1141,7 +1238,9 @@ impl Encoder {
11411238 plane_copy(PLANE_1, (0, 0), (w / 2, h / 2)),
11421239 ];
11431240 slot.upload.begin()?;
1241+ if foreign {
11441242 acquire(d, cb, image, &aspects, slot.upload.family);
1243+ }
11451244 unsafe {
11461245 barrier(
11471246 d,