forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'worktree-agent-acf1ff1251bb8411f'

Browse files at this commit

a6bbe3f49b0877a626ba71c8055615e6e3fdd35bAhmet Kaan GümüşParent: 3b58935ba75450
shims/vk-video-ffi/examples/dmabuf_smoke.rsmodified+92 -3
@@ -540,12 +540,53 @@ fn run<S: Encode>(gpu: &Gpu, name: &str, host: &[u8], linear: &Export, tiled: &E
540 }540 }
541 }541 }
542 542
543-fn run_planar<S: Encode>(gpu: &Gpu, name: &str, pattern: &Pattern, exports: &[&Export]) {543+fn planar_format(pattern: &Pattern) -> DmaBufFormat {
544- let format = if pattern.bits == 8 {544+ if pattern.bits == 8 {
545 DmaBufFormat::Nv12545 DmaBufFormat::Nv12
546 } else {546 } else {
547 DmaBufFormat::P010547 DmaBufFormat::P010
548- };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,
572+ };
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);
549 for export in exports {590 for export in exports {
550 let mut a = S::open(gpu, (W, H), pattern.bits).expect("encoder");591 let mut a = S::open(gpu, (W, H), pattern.bits).expect("encoder");
551 let mut b = S::open(gpu, (W, H), pattern.bits).expect("encoder");592 let mut b = S::open(gpu, (W, H), pattern.bits).expect("encoder");
@@ -636,6 +677,50 @@ fn main() {
636 run_planar::<Av1EncodeSession>(&gpu, "av1 nv12", &nv12, &nv12_set);677 run_planar::<Av1EncodeSession>(&gpu, "av1 nv12", &nv12, &nv12_set);
637 run_planar::<Av1EncodeSession>(&gpu, "av1 p010", &p010, &p010_set);678 run_planar::<Av1EncodeSession>(&gpu, "av1 p010", &p010, &p010_set);
638 679
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+
639 let mut cache = <H265EncodeSession as Encode>::open(&gpu, (W, H), 8).expect("encoder");724 let mut cache = <H265EncodeSession as Encode>::open(&gpu, (W, H), 8).expect("encoder");
640 let fds = [nv12_linear.fd.as_fd()];725 let fds = [nv12_linear.fd.as_fd()];
641 let src = DmaBuf {726 let src = DmaBuf {
@@ -661,4 +746,8 @@ fn main() {
661 for e in &exports {746 for e in &exports {
662 e.destroy(d);747 e.destroy(d);
663 }748 }
749+ for (a, b) in nv12_pairs.iter().chain(&p010_pairs) {
750+ a.destroy(d);
751+ b.destroy(d);
752+ }
664 }753 }
shims/vk-video-ffi/src/dmabuf.rsmodified+181 -165
@@ -217,6 +217,7 @@ impl Key {
217 217
218 pub(crate) struct Import {218 pub(crate) struct Import {
219 pub(crate) image: vk::Image,219 pub(crate) image: vk::Image,
220+ planes: Option<[vk::Image; 2]>,
220 id: u64,221 id: u64,
221 memories: Vec<vk::DeviceMemory>,222 memories: Vec<vk::DeviceMemory>,
222 }223 }
@@ -225,60 +226,23 @@ impl Import {
225 fn new(gpu: &Gpu, src: &DmaBuf, id: u64) -> Result<Self, Error> {226 fn new(gpu: &Gpu, src: &DmaBuf, id: u64) -> Result<Self, Error> {
226 check(gpu)?;227 check(gpu)?;
227 validate(src)?;228 validate(src)?;
228- let d = gpu.device();229+ let mut images = Vec::new();
229- let format = match src.format {230+ let mut memories = Vec::new();
230- DmaBufFormat::Bgrx => vk::Format::B8G8R8A8_UNORM,231+ match build(gpu, src, &mut images, &mut memories) {
231- DmaBufFormat::Rgbx => vk::Format::R8G8B8A8_UNORM,232+ Ok((image, planes)) => Ok(Import {
232- DmaBufFormat::Nv12 => NV12,233+ image,
233- DmaBufFormat::P010 => P010,234+ planes,
234- };235+ id,
235- let layouts: Vec<vk::SubresourceLayout> = src236+ memories,
236- .planes237+ }),
237- .iter()
238- .map(|p| vk::SubresourceLayout {
239- offset: p.offset as u64,
240- size: 0,
241- row_pitch: p.pitch as u64,
242- array_pitch: 0,
243- depth_pitch: 0,
244- })
245- .collect();
246- let mut explicit = vk::ImageDrmFormatModifierExplicitCreateInfoEXT::builder()
247- .drm_format_modifier(src.modifier)
248- .plane_layouts(&layouts)
249- .build();
250- let mut external = vk::ExternalMemoryImageCreateInfo::builder()
251- .handle_types(vk::ExternalMemoryHandleTypeFlags::DMA_BUF_EXT)
252- .build();
253- let flags = if src.fds.len() == 2 {
254- vk::ImageCreateFlags::DISJOINT
255- } else {
256- vk::ImageCreateFlags::empty()
257- };
258- let info = vk::ImageCreateInfo::builder()
259- .flags(flags)
260- .image_type(vk::ImageType::_2D)
261- .format(format)
262- .extent(vk::Extent3D {
263- width: src.width,
264- height: src.height,
265- depth: 1,
266- })
267- .mip_levels(1)
268- .array_layers(1)
269- .samples(vk::SampleCountFlags::_1)
270- .tiling(vk::ImageTiling::DRM_FORMAT_MODIFIER_EXT)
271- .usage(vk::ImageUsageFlags::TRANSFER_SRC)
272- .sharing_mode(vk::SharingMode::EXCLUSIVE)
273- .initial_layout(vk::ImageLayout::UNDEFINED)
274- .push_next(&mut external)
275- .push_next(&mut explicit)
276- .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) => {238 Err(e) => {
281- unsafe { d.destroy_image(image, None) };239+ 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+ }
282 Err(e)246 Err(e)
283 }247 }
284 }248 }
@@ -287,6 +251,9 @@ impl Import {
287 fn destroy(&self, d: &Device) {251 fn destroy(&self, d: &Device) {
288 unsafe {252 unsafe {
289 d.destroy_image(self.image, None);253 d.destroy_image(self.image, None);
254+ for p in self.planes.iter().flatten() {
255+ d.destroy_image(*p, None);
256+ }
290 for m in &self.memories {257 for m in &self.memories {
291 d.free_memory(*m, None);258 d.free_memory(*m, None);
292 }259 }
@@ -294,6 +261,129 @@ impl Import {
294 }261 }
295 }262 }
296 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 {
271+ let format = match src.format {
272+ DmaBufFormat::Bgrx => vk::Format::B8G8R8A8_UNORM,
273+ DmaBufFormat::Rgbx => vk::Format::R8G8B8A8_UNORM,
274+ DmaBufFormat::Nv12 => NV12,
275+ DmaBufFormat::P010 => P010,
276+ };
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
315+ .iter()
316+ .map(|p| vk::SubresourceLayout {
317+ offset: p.offset as u64,
318+ size: 0,
319+ row_pitch: p.pitch as u64,
320+ array_pitch: 0,
321+ depth_pitch: 0,
322+ })
323+ .collect();
324+ let mut explicit = vk::ImageDrmFormatModifierExplicitCreateInfoEXT::builder()
325+ .drm_format_modifier(modifier)
326+ .plane_layouts(&layouts)
327+ .build();
328+ let mut external = vk::ExternalMemoryImageCreateInfo::builder()
329+ .handle_types(vk::ExternalMemoryHandleTypeFlags::DMA_BUF_EXT)
330+ .build();
331+ let info = vk::ImageCreateInfo::builder()
332+ .image_type(vk::ImageType::_2D)
333+ .format(format)
334+ .extent(vk::Extent3D {
335+ width,
336+ height,
337+ depth: 1,
338+ })
339+ .mip_levels(1)
340+ .array_layers(1)
341+ .samples(vk::SampleCountFlags::_1)
342+ .tiling(vk::ImageTiling::DRM_FORMAT_MODIFIER_EXT)
343+ .usage(vk::ImageUsageFlags::TRANSFER_SRC)
344+ .sharing_mode(vk::SharingMode::EXCLUSIVE)
345+ .initial_layout(vk::ImageLayout::UNDEFINED)
346+ .push_next(&mut external)
347+ .push_next(&mut explicit)
348+ .build();
349+ unsafe { vk(d.create_image(&info, None), "vkCreateImage") }
350+}
351+
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") }
369+}
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);
383+ }
384+ Ok(memory)
385+}
386+
297 #[derive(Default)]387 #[derive(Default)]
298 pub(crate) struct ImportCache {388 pub(crate) struct ImportCache {
299 entries: Vec<(Key, Import)>,389 entries: Vec<(Key, Import)>,
@@ -302,7 +392,12 @@ pub(crate) struct ImportCache {
302 }392 }
303 393
304 impl ImportCache {394 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> {
306 validate(src)?;401 validate(src)?;
307 let key = Key::new(src)?;402 let key = Key::new(src)?;
308 match self.entries.iter().position(|(k, _)| *k == key) {403 match self.entries.iter().position(|(k, _)| *k == key) {
@@ -328,7 +423,7 @@ impl ImportCache {
328 }423 }
329 self.entries424 self.entries
330 .last()425 .last()
331- .map(|(_, import)| (import.image, import.id))426+ .map(|(_, import)| (import.image, import.planes, import.id))
332 .ok_or_else(|| Error::Invalid("DMA-BUF import cache".into()))427 .ok_or_else(|| Error::Invalid("DMA-BUF import cache".into()))
333 }428 }
334 429
@@ -339,28 +434,9 @@ impl ImportCache {
339 }434 }
340 }435 }
341 436
342-fn allocate(437+fn import_memory(gpu: &Gpu, image: vk::Image, fd: BorrowedFd) -> Result<vk::DeviceMemory, Error> {
343- gpu: &Gpu,
344- image: vk::Image,
345- plane: Option<vk::ImageAspectFlags>,
346- fd: BorrowedFd,
347-) -> Result<vk::DeviceMemory, Error> {
348 let d = gpu.device();438 let d = gpu.device();
349- let req = match plane {439+ let req = unsafe { d.get_image_memory_requirements(image) };
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- };
364 let owned = fd440 let owned = fd
365 .try_clone_to_owned()441 .try_clone_to_owned()
366 .map_err(|_| Error::Invalid("DMA-BUF descriptor".into()))?;442 .map_err(|_| Error::Invalid("DMA-BUF descriptor".into()))?;
@@ -387,82 +463,22 @@ fn allocate(
387 let mut dedicated = vk::MemoryDedicatedAllocateInfo::builder()463 let mut dedicated = vk::MemoryDedicatedAllocateInfo::builder()
388 .image(image)464 .image(image)
389 .build();465 .build();
390- let alloc = if plane.is_some() {466+ let alloc = vk::MemoryAllocateInfo::builder()
391- vk::MemoryAllocateInfo::builder()467+ .allocation_size(req.size)
392- .allocation_size(req.size)468+ .memory_type_index(index)
393- .memory_type_index(index)469+ .push_next(&mut dedicated)
394- .push_next(&mut import)470+ .push_next(&mut import)
395- .build()471+ .build();
396- } else {
397- vk::MemoryAllocateInfo::builder()
398- .allocation_size(req.size)
399- .memory_type_index(index)
400- .push_next(&mut dedicated)
401- .push_next(&mut import)
402- .build()
403- };
404 let memory = unsafe { vk(d.allocate_memory(&alloc, None), "vkAllocateMemory")? };472 let memory = unsafe { vk(d.allocate_memory(&alloc, None), "vkAllocateMemory")? };
405 let _ = owned.into_raw_fd();473 let _ = owned.into_raw_fd();
406- Ok(memory)474+ if let Err(e) = unsafe { vk(d.bind_image_memory(image, memory, 0), "vkBindImageMemory") } {
407-}475+ unsafe { d.free_memory(memory, None) };
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);
459 return Err(e);476 return Err(e);
460 }477 }
461- Ok(memories)478+ Ok(memory)
462 }479 }
463 480
464-/// Every DRM format modifier that can be a transfer destination for `format`.481+fn modifier_properties(gpu: &Gpu, format: vk::Format) -> Vec<vk::DrmFormatModifierPropertiesEXT> {
465-fn export_modifiers(gpu: &Gpu, format: vk::Format) -> Vec<u64> {
466 let instance = gpu.instance();482 let instance = gpu.instance();
467 let mut count = vk::DrmFormatModifierPropertiesListEXT::builder().build();483 let mut count = vk::DrmFormatModifierPropertiesListEXT::builder().build();
468 let mut props = vk::FormatProperties2::builder().push_next(&mut count).build();484 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> {
474 .build();490 .build();
475 let mut props = vk::FormatProperties2::builder().push_next(&mut full).build();491 let mut props = vk::FormatProperties2::builder().push_next(&mut full).build();
476 unsafe { instance.get_physical_device_format_properties2(gpu.physical(), format, &mut props) };492 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()
478 .filter(|m| {500 .filter(|m| {
479 m.drm_format_modifier_tiling_features501 m.drm_format_modifier_tiling_features
480 .contains(vk::FormatFeatureFlags::TRANSFER_DST | vk::FormatFeatureFlags::TRANSFER_SRC)502 .contains(vk::FormatFeatureFlags::TRANSFER_DST | vk::FormatFeatureFlags::TRANSFER_SRC)
@@ -483,6 +505,15 @@ fn export_modifiers(gpu: &Gpu, format: vk::Format) -> Vec<u64> {
483 .collect()505 .collect()
484 }506 }
485 507
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+
486 /// A ring slot's DMA-BUF: the one memory object, exported once, and its plane layout.517 /// A ring slot's DMA-BUF: the one memory object, exported once, and its plane layout.
487 pub(crate) struct Export {518 pub(crate) struct Export {
488 pub(crate) fd: OwnedFd,519 pub(crate) fd: OwnedFd,
@@ -702,25 +733,10 @@ pub fn read_dmabuf(gpu: &Gpu, desc: &DmaBufExport) -> Result<(Vec<u8>, Vec<u8>),
702 .push_next(&mut explicit)733 .push_next(&mut explicit)
703 .build();734 .build();
704 let image = unsafe { vk(d.create_image(&info, None), "vkCreateImage")? };735 let image = unsafe { vk(d.create_image(&info, None), "vkCreateImage")? };
705- let fds = [desc.fd.as_fd()];736+ let result = match import_memory(gpu, image, desc.fd.as_fd()) {
706- let src = DmaBuf {737+ Ok(memory) => {
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) => {
720 let out = read_image(gpu, image, (w, h), bytes);738 let out = read_image(gpu, image, (w, h), bytes);
721- for m in memories {739+ unsafe { d.free_memory(memory, None) };
722- unsafe { d.free_memory(m, None) };
723- }
724 out740 out
725 }741 }
726 Err(e) => Err(e),742 Err(e) => Err(e),
shims/vk-video-ffi/src/session.rsmodified+105 -6
@@ -19,6 +19,8 @@ pub(crate) const P010: vk::Format = vk::Format::G10X6_B10X6R10X6_2PLANE_420_UNOR
19 pub(crate) const DPB_SLOTS: usize = 2;19 pub(crate) const DPB_SLOTS: usize = 2;
20 pub(crate) const PLANE_0: vk::ImageAspectFlags = vk::ImageAspectFlags::PLANE_0;20 pub(crate) const PLANE_0: vk::ImageAspectFlags = vk::ImageAspectFlags::PLANE_0;
21 pub(crate) const PLANE_1: vk::ImageAspectFlags = vk::ImageAspectFlags::PLANE_1;21 pub(crate) const PLANE_1: vk::ImageAspectFlags = vk::ImageAspectFlags::PLANE_1;
22+#[cfg(target_os = "linux")]
23+const COLOR: vk::ImageAspectFlags = vk::ImageAspectFlags::COLOR;
22 24
23 pub(crate) type Stage = (vk::ImageLayout, vk::PipelineStageFlags2, vk::AccessFlags2);25 pub(crate) type Stage = (vk::ImageLayout, vk::PipelineStageFlags2, vk::AccessFlags2);
24 26
@@ -231,6 +233,28 @@ pub(crate) fn plane_copy(
231 }233 }
232 }234 }
233 235
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+
234 pub(crate) fn copy_region(258 pub(crate) fn copy_region(
235 offset: u64,259 offset: u64,
236 aspect: vk::ImageAspectFlags,260 aspect: vk::ImageAspectFlags,
@@ -1095,16 +1119,19 @@ impl Encoder {
1095 DmaBufFormat::Nv12 | DmaBufFormat::P010 => self.check_planar(src)?,1119 DmaBufFormat::Nv12 | DmaBufFormat::P010 => self.check_planar(src)?,
1096 }1120 }
1097 let busy: Vec<u64> = self.slots.iter().filter_map(|s| s.import).collect();1121 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)?;
1099 let k = self.next;1123 let k = self.next;
1100- let result = match src.format {1124+ let result = match (src.format, planes) {
1101- DmaBufFormat::Bgrx | DmaBufFormat::Rgbx => self.upload(Input::DmaBuf {1125+ (DmaBufFormat::Bgrx | DmaBufFormat::Rgbx, _) => self.upload(Input::DmaBuf {
1102 image,1126 image,
1103 width,1127 width,
1104 height,1128 height,
1105 bgr: src.format == DmaBufFormat::Bgrx,1129 bgr: src.format == DmaBufFormat::Bgrx,
1106 }),1130 }),
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),
1108 };1135 };
1109 if result.is_ok() {1136 if result.is_ok() {
1110 self.slots[k].import = Some(id);1137 self.slots[k].import = Some(id);
@@ -1127,7 +1154,77 @@ impl Encoder {
1127 }1154 }
1128 1155
1129 #[cfg(target_os = "linux")]1156 #[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> {
1131 let gpu = self.vs.gpu.clone();1228 let gpu = self.vs.gpu.clone();
1132 let d = gpu.device();1229 let d = gpu.device();
1133 let k = self.next;1230 let k = self.next;
@@ -1141,7 +1238,9 @@ impl Encoder {
1141 plane_copy(PLANE_1, (0, 0), (w / 2, h / 2)),1238 plane_copy(PLANE_1, (0, 0), (w / 2, h / 2)),
1142 ];1239 ];
1143 slot.upload.begin()?;1240 slot.upload.begin()?;
1144- acquire(d, cb, image, &aspects, slot.upload.family);1241+ if foreign {
1242+ acquire(d, cb, image, &aspects, slot.upload.family);
1243+ }
1145 unsafe {1244 unsafe {
1146 barrier(1245 barrier(
1147 d,1246 d,