diff --git a/NativeAdrenoMetalPlugin.mm b/NativeAdrenoMetalPlugin.mm index a6c6805..e96c685 100644 --- a/NativeAdrenoMetalPlugin.mm +++ b/NativeAdrenoMetalPlugin.mm @@ -354,6 +354,10 @@ static id deviceForPort(unsigned port) { // same image. Read by the bridge as the device opens. if (bootArgSet("gpu-purge=0")) setenv("NATIVE_METAL_PURGE", "0", 1); else unsetenv("NATIVE_METAL_PURGE"); + // gpu-ycbcr-planes=0: every two-plane YCbCr surface texture is made with its converted image, as before bridge patch 0035, + // for a comparison on the same image. Read by the bridge at the first such texture. + if (bootArgSet("gpu-ycbcr-planes=0")) setenv("NATIVE_METAL_YCBCR_PLANES", "0", 1); + else unsetenv("NATIVE_METAL_YCBCR_PLANES"); if (bootArgSet("metal-flow=0")) setenv("NATIVE_METAL_FLOW", "0", 1); else if (bootArgSet("metal-flow=1")) setenv("NATIVE_METAL_FLOW", "1", 1); else unsetenv("NATIVE_METAL_FLOW"); diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 186e23c..59e6875 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -5201,6 +5201,16 @@ extern "C" size_t native_bridge_metal_telemetry(char *out,size_t size) { row("PE warn %llu crit %llu freed %llu | imports held %llu %lluM | tuc flush %llu",(ull)gPressureEvents[0].load(), (ull)gPressureEvents[1].load(),(ull)gPressureEvents[2].load(),(ull)held,(ull)(heldBytes>>20),(ull)bc[4]); } + // Two-plane YCbCr surface textures (video frames; bridge patch 0035): made over their planes alone and made with a converted + // image, images made later at a slot binding that could not read the planes in place, textures another call made whole, the + // converted images alive and their MiB; then why a texture kept its image: the device, the layout, a swizzle, the planes' + // offset, their element count, planes past the surface, no texel buffer view. Only once such a texture was made. + { + uint64_t yc[14]={}; + if(nmb_ycbcr_surface_counts(gDevice,yc) && (yc[0] || yc[1])) + row("YC planes %llu conv %llu later %llu whole %llu | img %llu %lluM | kept %llu/%llu/%llu/%llu/%llu/%llu/%llu",(ull)yc[0],(ull)yc[1], + (ull)yc[2],(ull)yc[3],(ull)yc[4],(ull)(yc[5]>>20),(ull)yc[7],(ull)yc[8],(ull)yc[9],(ull)yc[10],(ull)yc[11],(ull)yc[12],(ull)yc[13]); + } // How Turnip rendered the passes (tu_render_mode_stats): GMEM passes and their Mpx, SYSMEM passes the autotuner chose and theirs, // SYSMEM passes for another reason and theirs, then GMEM passes with an attachment loaded or stored over its texels alone. { diff --git a/README.md b/README.md index 30ff506..c8f0959 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,12 @@ After a discard: NonVolatile places the resource again and answers Empty; a quer `gpu-purge=0` keeps the state without discarding anything (`NATIVE_METAL_PURGE=0` selects the same in a process). +## Video surface textures + +A two-plane YCbCr texture over an IOSurface (a video frame: `MTLPixelFormatYCBCR8_420_2P` or `YCBCR10_420_2P` over plane 0, which QuartzCore asks for on a device of the Apple families) of `ShaderRead` usage alone is made over the surface's planes: a function that samples it reads the planes' own bytes through the no-copy import and converts them as Metal does, and no converted image of the picture's size (63 MiB in half floats for 3840 x 2160, with a staging copy of the planes) is made or kept. A binding that cannot read the planes in place (a `read()`, a gather, a comparison) makes the image at that binding; any other call that names the texture (a blit, a view, `getBytes`) makes the texture with its image, as before, and uses that from then on. Textures of another usage, format or plane, and those whose planes the device cannot view as a texel buffer, are made with their image. The surface stays imported and counted in use while a command buffer in flight names its texture. + +`gpu-ycbcr-planes=0` makes every such texture with its converted image (`NATIVE_METAL_YCBCR_PLANES=0` selects the same in a process). + ## Shader caches and priming Each process keeps its pipeline cache in the first directory its sandbox lets it write: the same-user shared directory, then Metal's own cache directory. Compiler replies for source and stitched libraries are kept beside it. A cache entry's access time is refreshed on a hit until the platform refuses the change once. @@ -36,7 +42,7 @@ The render server primes Metal libraries in the background while it draws nothin ## Telemetry table -With `diag=1`, the render server sends its telemetry table to the kernel log (`[metal table]`, shown with `log-gpu=1`) every six seconds and to the system log every thirty (`NativeAdrenoMetal table:`). The kernel keeps the first 48 rows of at most 90 characters. Memory rows come first: `MEM` (MiB by kind and Turnip's BO cache), `MK`, `MKb`, `MKc` (blocks by size class: pooled, empty, spare, dedicated images, dedicated buffers), `ML` (lazily allocated blocks and the MiB the driver backed), `PG` (declared purgeable), `PGb` (MiB discarded now, by pressure and by age, placed again by request and by another use, Empty answers), `PGc` (MiB declared, MiB never discardable because a pointer or address was handed out, what the last pass left in use, tied or young, failed placements), `PU` (only when a command named a discarded resource whose caller had not set it NonVolatile: the count and the first two callers' return addresses), `PE` (memory pressure events and held surface imports), `RM`, `PS` and `MA` (allocation call stacks). Compile rows follow: `T` (pipeline, library, compiler and function creation), `VP` (driver pipelines made at creation and at draws, with the driver's time), `V`, `SC`, `PR` (priming) and `FL`. `TH` rows list the threads that used the most CPU since the last table. Other processes write one `NativeAdrenoMetal work:` line to the system log every thirty seconds while their compile counts change. They also write a `NativeAdrenoMetal memory:` line each time their runtime blocks pass the next power of two from 64 MiB (blocks, bytes placed, declared purgeable, discarded) and a `NativeAdrenoMetal purge:` line for each pass that discarded something. With `diag=1` or `log-gpu=1`, any process writes a `NativeAdrenoMetal late use:` line for each of its first four callers that name a discarded resource without setting it NonVolatile first. +With `diag=1`, the render server sends its telemetry table to the kernel log (`[metal table]`, shown with `log-gpu=1`) every six seconds and to the system log every thirty (`NativeAdrenoMetal table:`). The kernel keeps the first 48 rows of at most 90 characters. Memory rows come first: `MEM` (MiB by kind and Turnip's BO cache), `MK`, `MKb`, `MKc` (blocks by size class: pooled, empty, spare, dedicated images, dedicated buffers), `ML` (lazily allocated blocks and the MiB the driver backed), `PG` (declared purgeable), `PGb` (MiB discarded now, by pressure and by age, placed again by request and by another use, Empty answers), `PGc` (MiB declared, MiB never discardable because a pointer or address was handed out, what the last pass left in use, tied or young, failed placements), `PU` (only when a command named a discarded resource whose caller had not set it NonVolatile: the count and the first two callers' return addresses), `PE` (memory pressure events and held surface imports), `YC` (two-plane YCbCr surface textures made over their planes and made with a converted image, images made later at a binding, textures another call made whole, converted images alive and their MiB, then the textures that kept their image by reason: device, layout, swizzle, plane offset, element count, planes past the surface, no texel buffer view), `RM`, `PS` and `MA` (allocation call stacks). Compile rows follow: `T` (pipeline, library, compiler and function creation), `VP` (driver pipelines made at creation and at draws, with the driver's time), `V`, `SC`, `PR` (priming) and `FL`. `TH` rows list the threads that used the most CPU since the last table. Other processes write one `NativeAdrenoMetal work:` line to the system log every thirty seconds while their compile counts change. They also write a `NativeAdrenoMetal memory:` line each time their runtime blocks pass the next power of two from 64 MiB (blocks, bytes placed, declared purgeable, discarded) and a `NativeAdrenoMetal purge:` line for each pass that discarded something. With `diag=1` or `log-gpu=1`, any process writes a `NativeAdrenoMetal late use:` line for each of its first four callers that name a discarded resource without setting it NonVolatile first. ## Frame diagnostics diff --git a/bridge/NativeMetalObjects.h b/bridge/NativeMetalObjects.h index a47243c..d864a9b 100644 --- a/bridge/NativeMetalObjects.h +++ b/bridge/NativeMetalObjects.h @@ -158,6 +158,10 @@ NmbTexture *nmb_texture_new_on_buffer(NmbBuffer *, const struct NmbTextureDescri NmbTexture *nmb_texture_new_on_surface(NmbBuffer *, const struct NmbTextureDescriptor *, uint64_t offset, uint64_t bytes_per_row, uint32_t pixel_format, uint64_t plane, uint64_t width, uint64_t height, uint64_t chroma_offset, uint64_t chroma_bytes_per_row); +/* Two-plane YCbCr surface textures: made over their planes alone, made with their converted image, images made later at a slot + binding, textures over their planes another call made whole, converted images alive and their bytes; then textures whose planes + are read in place and those that keep their image alone by reason (device, layout, swizzle, offset, elements, reach, view). */ +bool nmb_ycbcr_surface_counts(const NmbDevice *, uint64_t out[14]); NmbTexture *nmb_texture_new_linear(NmbBuffer *, const struct NmbTextureDescriptor *, uint64_t offset, uint64_t bytes_per_row, uint64_t bytes_per_image); /* whole: a view of every level and slice as format; else levels and slices are {start, count} and kind the view's texture type. */ NmbTexture *nmb_texture_new_view(NmbTexture *, uint64_t format, uint64_t kind, const uint64_t levels[2], const uint64_t slices[2], bool whole); diff --git a/bridge/NativeMetalObjects.rs b/bridge/NativeMetalObjects.rs index e066a0a..105374c 100644 --- a/bridge/NativeMetalObjects.rs +++ b/bridge/NativeMetalObjects.rs @@ -1385,13 +1385,56 @@ pub extern "C" fn nmb_texture_new_on_surface(buffer: *mut NmbBuffer, descriptor: let (tie, buffer) = (get(buffer)?.0.tie(), get(buffer)?.0.live()?); let mut surface = IoSurfacePlane::new(offset, bytes_per_row, true); if chroma_bytes_per_row != 0 { - surface = surface.with_ycbcr(IoSurfaceYCbCr::new(pixel_format, plane, [width, height], chroma_offset, chroma_bytes_per_row)); + let facts = IoSurfaceYCbCr::new(pixel_format, plane, [width, height], chroma_offset, chroma_bytes_per_row); + // A two-plane YCbCr texture that is only sampled is made over the surface's planes alone where the runtime can + // (bridge patch 0035): no converted image of the picture's size is made or kept for it. Any call but a slot binding + // makes the texture with its image, as it was always made, and uses that from then on. + if ycbcr_planes_on() { + let light = buffer.new_texture_with_iosurface_plane(&descriptor, surface.with_ycbcr(facts.with_planes_alone())); + let light = light.map_err(e("texture over surface"))?; + if !light.is_planes_alone() { + return Ok(boxed(NmbTexture(Resource::tied(light, tie)))); + } + let (buffer, whole) = (Buffer::clone(&buffer), surface.with_ycbcr(facts)); + let make = move || buffer.new_texture_with_iosurface_plane(&descriptor, whole).map_err(e("texture over surface")); + return Ok(boxed(NmbTexture(Resource::forms(light, tie, make)))); + } + surface = surface.with_ycbcr(facts); } let texture = buffer.new_texture_with_iosurface_plane(&descriptor, surface); Ok(boxed(NmbTexture(Resource::tied(texture.map_err(e("texture over surface"))?, tie)))) }) } +/// False where `NATIVE_METAL_YCBCR_PLANES=0` (the boot argument `gpu-ycbcr-planes=0`): every two-plane YCbCr surface texture is then +/// made with its converted image, as before bridge patch 0035, for a comparison on the same image. Read once. +fn ycbcr_planes_on() -> bool { + static ON: std::sync::OnceLock = std::sync::OnceLock::new(); + *ON.get_or_init(|| std::env::var_os("NATIVE_METAL_YCBCR_PLANES").is_none_or(|value| value != "0")) +} + +/// The process's two-plane YCbCr surface textures, into the caller's fourteen words: made over their planes alone, made with their +/// converted image, images made later for a texture over its planes at a slot binding that needed one, textures over their planes +/// that another call made whole, the converted images alive now and the bytes they and their staging copies hold; then the +/// textures whose planes a function reads in place, and those that keep their image alone by the reason: the device, the layout +/// (another format, plane 1), a swizzle, the planes' offset, their element count, planes past the surface, no texel buffer view. +#[unsafe(no_mangle)] +pub extern "C" fn nmb_ycbcr_surface_counts(device: *const NmbDevice, out: *mut u64) -> bool { + guard(false, || { + if out.is_null() { + return Err("null counts".into()); + } + // SAFETY: the header's fourteen words. + let out = unsafe { std::slice::from_raw_parts_mut(out, 14) }; + let (c, w) = (metal2vulkan_runtime::ycbcr_surface_counts(), get(device)?.0.work_counts()); + out.copy_from_slice(&[c.planes_alone, c.with_image, c.images_later, crate::purge::made_whole(), c.images_alive, c.image_bytes_alive, + w.ycbcr_textures_in_place, w.ycbcr_images_kept_device, w.ycbcr_images_kept_layout, + w.ycbcr_images_kept_swizzle, w.ycbcr_images_kept_alignment, w.ycbcr_images_kept_elements, + w.ycbcr_images_kept_reach, w.ycbcr_images_kept_view]); + Ok(true) + }) +} + /// `newLinearTextureWithDescriptor:offset:bytesPerRow:bytesPerImage:` (Apple SPI). #[unsafe(no_mangle)] pub extern "C" fn nmb_texture_new_linear(buffer: *mut NmbBuffer, descriptor: *const NmbTextureDescriptor, offset: u64, bytes_per_row: u64, @@ -1454,7 +1497,7 @@ pub extern "C" fn nmb_texture_release(texture: *mut NmbTexture) { #[unsafe(no_mangle)] pub extern "C" fn nmb_texture_describe(texture: *const NmbTexture, out: *mut NmbTextureDescriptor) { guard((), || { - let t = get(texture)?.0.live()?; + let t = get(texture)?.0.slot()?; let out = get_mut(out)?; *out = NmbTextureDescriptor { kind: t.texture_type().as_raw() as u64, @@ -2867,7 +2910,7 @@ pub unsafe extern "C" fn nmb_encoder_set_bytes(encoder: *mut NmbEncoder, stage: #[unsafe(no_mangle)] pub extern "C" fn nmb_encoder_set_texture(encoder: *mut NmbEncoder, stage: u32, texture: *mut NmbTexture, index: u32) -> bool { guard(false, || { - let texture = optional(texture)?.map(|t| t.0.live()).transpose()?; + let texture = optional(texture)?.map(|t| t.0.slot()).transpose()?; let texture = texture.as_deref(); match (get_mut(encoder)?, stage) { (NmbEncoder::Compute(c), COMPUTE) => c.set_texture(texture, index), diff --git a/bridge/NativeMetalPurge.rs b/bridge/NativeMetalPurge.rs index 6ea6bc5..44f0dd4 100644 --- a/bridge/NativeMetalPurge.rs +++ b/bridge/NativeMetalPurge.rs @@ -526,12 +526,49 @@ impl std::ops::Deref for Live<'_, T> { } } +/// A resource made in a light form that serves slot bindings alone, and how to make it whole: a two-plane YCbCr texture over a +/// surface's planes with no converted image (bridge patch 0035). The first call that needs more than a slot binding makes the whole +/// object, which every call uses from then on; an encoder or a batch that bound the light one keeps it until it is done. +pub struct Forms { + /// The object in use and whether it is the whole one. + now: Mutex<(T, bool)>, + whole: Box Result + Send + Sync>, + tie: Option, +} + +/// The light objects a call made whole, for the frontend's YC row. +static MADE_WHOLE: AtomicU64 = AtomicU64::new(0); + +/// How many light objects a call other than a slot binding made whole. +pub fn made_whole() -> u64 { + MADE_WHOLE.load(Relaxed) +} + +impl Forms { + /// The whole object, made at the first call that asks for it. + fn whole(&self) -> Result { + let mut now = lock(&self.now); + if !now.1 { + *now = ((self.whole)()?, true); + MADE_WHOLE.fetch_add(1, Relaxed); + } + Ok(now.0.clone()) + } + + /// The object in use: the light one until a call made it whole. + fn any(&self) -> T { + lock(&self.now).0.clone() + } +} + /// A buffer, texture or heap behind a frontend handle. pub enum Resource { /// Never discarded: its object, and its tie on the resource it was made of where that one may be. Fixed(T, Option), /// May be discarded while Volatile or Empty and idle. Kept(Arc>), + /// Never discarded: light until a call needs it whole. + Forms(Forms), } impl Resource { @@ -569,11 +606,27 @@ impl Resource { })) } - /// The object for a call that uses the resource, placed again where it was discarded. + /// `light`, which serves slot bindings alone, made of the resource `tie` is on, and `whole`, which makes the object every + /// other call uses. + pub fn forms(light: T, tie: Option, whole: impl Fn() -> Result + Send + Sync + 'static) -> Resource { + Resource::Forms(Forms { now: Mutex::new((light, false)), whole: Box::new(whole), tie }) + } + + /// The object for a call that uses the resource, placed again where it was discarded, and made whole where it was light. pub fn live(&self) -> Result, String> { match self { Resource::Fixed(object, _) => Ok(Live::Fixed(object)), Resource::Kept(cell) => cell.live().map(Live::Placed), + Resource::Forms(forms) => forms.whole().map(Live::Placed), + } + } + + /// The object for a slot binding, or a question every form answers alike: as [`live`](Self::live), but a light object stays + /// light. + pub fn slot(&self) -> Result, String> { + match self { + Resource::Forms(forms) => Ok(Live::Placed(forms.any())), + other => other.live(), } } @@ -582,13 +635,14 @@ impl Resource { match self { Resource::Fixed(object, _) => Some(Live::Fixed(object)), Resource::Kept(cell) => cell.peek().map(Live::Placed), + Resource::Forms(forms) => Some(Live::Placed(forms.any())), } } /// What a kept resource was made of. pub fn recipe(&self) -> Option<&T::Recipe> { match self { - Resource::Fixed(..) => None, + Resource::Fixed(..) | Resource::Forms(_) => None, Resource::Kept(cell) => Some(&cell.recipe), } } @@ -598,6 +652,7 @@ impl Resource { match self { Resource::Fixed(object, _) => object.bytes(), Resource::Kept(cell) => cell.bytes, + Resource::Forms(forms) => forms.any().bytes(), } } @@ -607,6 +662,7 @@ impl Resource { match self { Resource::Fixed(object, _) => Ok(object.state(requested)?.raw()), Resource::Kept(cell) => cell.set_state(requested), + Resource::Forms(forms) => Ok(forms.any().state(requested)?.raw()), } } @@ -614,7 +670,7 @@ impl Resource { /// resource's object is read: what a fixed resource is itself tied to, since that is whose memory the new one shares. pub fn tie(&self) -> Option { match self { - Resource::Fixed(_, tie) => tie.as_ref().map(|tie| Tie::on(Arc::clone(&tie.0))), + Resource::Fixed(_, tie) | Resource::Forms(Forms { tie, .. }) => tie.as_ref().map(|tie| Tie::on(Arc::clone(&tie.0))), Resource::Kept(cell) => { let cell: Arc = cell.clone(); Some(Tie::on(cell)) @@ -627,7 +683,7 @@ impl Resource { /// without having set it NonVolatile, and the command's use will place it again, emptied. pub fn hold(&self) -> bool { match self { - Resource::Fixed(..) => false, + Resource::Fixed(..) | Resource::Forms(_) => false, Resource::Kept(cell) => { cell.holds.fetch_add(1, SeqCst); cell.out.load(SeqCst) diff --git a/bridge/patches/0035-ycbcr-surface-textures-over-their-planes.patch b/bridge/patches/0035-ycbcr-surface-textures-over-their-planes.patch new file mode 100644 index 0000000..010d29f --- /dev/null +++ b/bridge/patches/0035-ycbcr-surface-textures-over-their-planes.patch @@ -0,0 +1,483 @@ +diff --git a/crates/metal2vulkan-runtime/src/buffer_texture.rs b/crates/metal2vulkan-runtime/src/buffer_texture.rs +index 617d359..d7be5fe 100644 +--- a/crates/metal2vulkan-runtime/src/buffer_texture.rs ++++ b/crates/metal2vulkan-runtime/src/buffer_texture.rs +@@ -777,12 +777,15 @@ impl Buffer { + let texels = Texels::placed(self.clone(), placed, texel_bytes, descriptor) + .with_options(options) + .over_surface(surface); ++ // Over its planes alone where the caller asks and the texture can be ++ // (`texture/ycbcr_image.rs`). ++ let alone = ycbcr_surface.is_some_and(|facts| facts.planes_alone); + return self.device().new_converted_texture( + call, + texels, + descriptor, + &layout, +- id_use, ++ (id_use, alone), + (&planes, span), + ); + } +diff --git a/crates/metal2vulkan-runtime/src/lib.rs b/crates/metal2vulkan-runtime/src/lib.rs +index 8416d73..03287cc 100644 +--- a/crates/metal2vulkan-runtime/src/lib.rs ++++ b/crates/metal2vulkan-runtime/src/lib.rs +@@ -366,6 +366,7 @@ pub use shader_cache::{ + pub use sparse::device::SparseFeatures; + pub use sparse::encoder::ResourceStateCommandEncoder; + pub use sparse::{SparsePageSize, SparseProblem, SparseTextureRegionAlignmentMode}; ++pub use texture::{ycbcr_surface_counts, YCbCrSurfaceCounts}; + pub use texture::{SharedTextureHandle, Texture, VkImageLayouts, WrappedImageProblem}; + pub use texture_descriptor::{TextureDescriptor, TextureProblem, TextureSwizzleChannels}; + pub use used_resources::{EncoderKind, EndedEncoder, UsedResources}; +diff --git a/crates/metal2vulkan-runtime/src/shadow.rs b/crates/metal2vulkan-runtime/src/shadow.rs +index cd63f97..daa3572 100644 +--- a/crates/metal2vulkan-runtime/src/shadow.rs ++++ b/crates/metal2vulkan-runtime/src/shadow.rs +@@ -381,6 +381,8 @@ pub(super) enum ShadowSource { + conversion: Box, + /// The texture's format, which a refusal names. + pixel_format: PixelFormat, ++ /// The image counted alive while these copies live (`texture/ycbcr_image.rs`). ++ _alive: crate::texture::ImageAlive, + }, + /// A surface's two planes in a buffer's bytes, into which the texels a function writes to a + /// 4:4:4 two-plane YCbCr texture are stored (`ycbcr.rs`): the shadow is an image of the +diff --git a/crates/metal2vulkan-runtime/src/texture.rs b/crates/metal2vulkan-runtime/src/texture.rs +index f245020..ee3fdc5 100644 +--- a/crates/metal2vulkan-runtime/src/texture.rs ++++ b/crates/metal2vulkan-runtime/src/texture.rs +@@ -182,7 +182,8 @@ pub(super) mod wrapped; + mod ycbcr_image; + mod ycbcr_planes; + pub(crate) use decoded_twin::TwinRead; +-pub(crate) use ycbcr_image::{KeptImage, PlanesInPlace}; ++pub use ycbcr_image::{ycbcr_surface_counts, YCbCrSurfaceCounts}; ++pub(crate) use ycbcr_image::{ImageAlive, KeptImage, PlanesInPlace}; + pub(crate) use ycbcr_planes::KeptTexels; + + pub(crate) use attached_reads::feedback_candidate; +@@ -4296,10 +4297,13 @@ impl Texture { + } + } + +- /// The buffer's bytes a texture buffer is, or `None` for a texture with an image. ++ /// The buffer's bytes a texture buffer is, or `None` for a texture with an image, and for a ++ /// two-plane YCbCr texture over its planes alone (`texture/ycbcr_image.rs`), whose texels ++ /// are the planes' bytes and which binds as a texture, not as a texture buffer. + pub(super) fn texels(&self) -> Option<&Texels> { + match &self.0.storage { + Storage::Image(_) | Storage::Alias { .. } | Storage::Shadow { .. } => None, ++ Storage::Texels(_) if self.image_later().is_some() => None, + Storage::Texels(texels) => Some(texels), + } + } +@@ -5371,6 +5375,11 @@ impl Texture { + placement: IdPlacement, + ) -> Result { + self.check_viewable(call)?; ++ // A two-plane YCbCr texture over its planes alone makes no view: its caller views the ++ // texture made with its image (`texture/ycbcr_image.rs`). ++ if self.image_later().is_some() { ++ return Err(Error::NotImplemented(RuntimeGap::Textures)); ++ } + self.device().check_texture_placement( + call, + placement, +diff --git a/crates/metal2vulkan-runtime/src/texture/usage_shadow.rs b/crates/metal2vulkan-runtime/src/texture/usage_shadow.rs +index 5b7c1bb..b5ad14f 100644 +--- a/crates/metal2vulkan-runtime/src/texture/usage_shadow.rs ++++ b/crates/metal2vulkan-runtime/src/texture/usage_shadow.rs +@@ -33,6 +33,11 @@ + //! shadow here instead (`texture/ycbcr_image.rs`, `shadow.rs`): every storage binding of it + //! writes the shadow's view and names the shadow in its uses, and every other binding names its + //! own image. It is exact and announces nothing. ++//! - **A YCbCr texture over its planes alone** (`texture/ycbcr_image.rs`) has no image of its ++//! own: its image, made at its first slot binding that does not read its planes in place, is ++//! held here with the copies that convert the planes into it, and every such binding writes ++//! its view and names it in its uses. It is the image the texture would have been made with, ++//! and announces nothing. + + use super::{Image, Owner, Storage, Texture}; + use crate::binding_view::binding_view; +@@ -138,6 +143,10 @@ impl Texture { + storage: bool, + attached: bool, + ) -> Result>, Error> { ++ // Over its planes alone: its image is made here, at the first binding that needs it. ++ if self.image_later().is_some() { ++ return Ok(Some(self.usage_shadow(call)?)); ++ } + if self.needs_usage_shadow(storage) { + let shadow = self.usage_shadow(call)?; + if !shadow.announced.swap(true, Ordering::Relaxed) { +@@ -217,6 +226,16 @@ impl Texture { + } + // A converted texture's image has both shader usages: the slot holds its write shadow. + let shadow = match ycbcr_writes_stored(self.0.record.pixel_format) { ++ // But over its planes alone, where it holds the image itself and the conversion. ++ _ if self.image_later().is_some() => { ++ let (image, copies) = self.later_image(call)?; ++ UsageShadow { ++ image, ++ copies, ++ truth: "its planes", ++ announced: AtomicBool::new(true), ++ } ++ } + true => { + let (image, copies) = self.ycbcr_write_shadow(call)?; + UsageShadow { +diff --git a/crates/metal2vulkan-runtime/src/texture/ycbcr_image.rs b/crates/metal2vulkan-runtime/src/texture/ycbcr_image.rs +index 48b8f9f..b19aa30 100644 +--- a/crates/metal2vulkan-runtime/src/texture/ycbcr_image.rs ++++ b/crates/metal2vulkan-runtime/src/texture/ycbcr_image.rs +@@ -21,9 +21,29 @@ + //! are also an image of it over their bytes (`ycbcr_planes.rs`), which a slot whose every sample + //! the device's sampler takes samples through the device's conversion instead of the texel + //! buffer. ++//! ++//! **Over its planes alone** (`PlanesInPlace::later`). Metal's own YCbCr texture is the ++//! surface's planes and nothing else, where the image above is as large as the picture in halves ++//! (63 MiB for 3840 x 2160) with a staging copy of the planes beside it, kept for the texture's ++//! life, though no function that only samples it reads either. So a texture whose planes are read ++//! in place, of `ShaderRead` usage alone, whose caller asks ++//! ([`IoSurfaceYCbCr::planes_alone`](crate::IoSurfaceYCbCr::planes_alone)), is made with no ++//! image: its texels are the planes' bytes (`Storage::Texels`), and it holds what its image ++//! is made of. A slot binding that reads the planes in place binds them as before. The first slot ++//! binding that needs the image (a `read()`, a gather, a comparison, a storage binding) makes it ++//! then, as a binding beyond a texture's usage makes its shadow (`texture/usage_shadow.rs`): the ++//! same image, view and conversion as a texture made with its image, its copies listed with the ++//! bound textures' (`shadow::texture_copies`), so it converts as that texture does. Such a ++//! texture serves slot bindings alone: it takes no resource ID (no function reads it through ++//! one), makes no view, and every other call that needs its image is refused ++//! (`NotImplemented(Textures)`), which its caller answers by making the texture with its image ++//! ([`Texture::is_planes_alone`]). Its CPU copies move the luma rows, as the other's do. The ++//! textures made each way, the images made later and the bytes of the images alive are counted ++//! ([`ycbcr_surface_counts`](crate::ycbcr_surface_counts)). + + use super::ycbcr_planes::PlanesImage; + use super::{image_error, IdUse, Image, Storage, Texture}; ++use crate::resource_ids::IdPlacement; + use crate::address_map::AddressModeMap; + use crate::binding_view::VkView; + use crate::buffer_texture::{view_usage, Texels}; +@@ -56,9 +76,70 @@ use metal2vulkan::vk::{ + ImageUsageFlags, ImageViewType, + }; + use std::ops::Range; +-use std::sync::atomic::AtomicU64; ++use std::sync::atomic::{AtomicU64, Ordering}; + use std::sync::Arc; + ++/// The two-plane YCbCr textures over surfaces a process made and the images it holds for them ++/// (the module docs' **Over its planes alone**), for a frontend's telemetry. ++#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] ++pub struct YCbCrSurfaceCounts { ++ /// Textures made over their planes alone, with no image. ++ pub planes_alone: u64, ++ /// Textures made with their image. ++ pub with_image: u64, ++ /// Images made later for a texture over its planes alone, at a slot binding that needed one. ++ pub images_later: u64, ++ /// Images alive now, of either kind (a view's own among them). ++ pub images_alive: u64, ++ /// The bytes of device memory those images and their conversions' staging buffers hold. ++ pub image_bytes_alive: u64, ++} ++ ++static PLANES_ALONE: AtomicU64 = AtomicU64::new(0); ++static WITH_IMAGE: AtomicU64 = AtomicU64::new(0); ++static IMAGES_LATER: AtomicU64 = AtomicU64::new(0); ++static IMAGES_ALIVE: AtomicU64 = AtomicU64::new(0); ++static IMAGE_BYTES_ALIVE: AtomicU64 = AtomicU64::new(0); ++ ++/// The process's counts of two-plane YCbCr textures over surfaces ([`YCbCrSurfaceCounts`]). ++pub fn ycbcr_surface_counts() -> YCbCrSurfaceCounts { ++ YCbCrSurfaceCounts { ++ planes_alone: PLANES_ALONE.load(Ordering::Relaxed), ++ with_image: WITH_IMAGE.load(Ordering::Relaxed), ++ images_later: IMAGES_LATER.load(Ordering::Relaxed), ++ images_alive: IMAGES_ALIVE.load(Ordering::Relaxed), ++ image_bytes_alive: IMAGE_BYTES_ALIVE.load(Ordering::Relaxed), ++ } ++} ++ ++/// A converted image alive, counted with its bytes until its copies go ++/// (`ShadowSource::Converted`). ++pub(crate) struct ImageAlive(u64); ++ ++impl ImageAlive { ++ fn of(bytes: u64) -> ImageAlive { ++ IMAGES_ALIVE.fetch_add(1, Ordering::Relaxed); ++ IMAGE_BYTES_ALIVE.fetch_add(bytes, Ordering::Relaxed); ++ ImageAlive(bytes) ++ } ++} ++ ++impl Drop for ImageAlive { ++ fn drop(&mut self) { ++ IMAGES_ALIVE.fetch_sub(1, Ordering::Relaxed); ++ IMAGE_BYTES_ALIVE.fetch_sub(self.0, Ordering::Relaxed); ++ } ++} ++ ++/// What the image of a texture over its planes alone is made of, at the first slot binding that ++/// needs it (the module docs): what [`Device::converted_storage`] takes. ++pub(super) struct ImageLater { ++ descriptor: TextureDescriptor, ++ layout: TextureLayout, ++ planes: YCbCrPlanes, ++ span: Range, ++} ++ + /// The formats of a texture's image and of the view the conversion writes it through, by what + /// its texels read: the converted texels as halves, every channel a multiple of 1/2048 up to 1, + /// which a half holds exactly; the sRGB twins' as floats, every channel a multiple of 1/8192, +@@ -149,6 +230,9 @@ pub(crate) struct PlanesInPlace { + pub(super) words: YCbCrInPlace, + start: u64, + pub(super) image: Option>, ++ /// Of a texture over its planes alone: what its image is made of once a slot binding needs ++ /// it (the module docs). `None` for a texture made with its image. ++ pub(super) later: Option, + } + + /// Why a two-plane YCbCr texture keeps its image alone, its planes read in place by no function +@@ -228,6 +312,7 @@ impl Device { + words, + start: span.start, + image: None, ++ later: None, + }) + } + +@@ -236,16 +321,18 @@ impl Device { + /// image of the texels Metal reads and the conversion (`ycbcr.rs`). It reads alpha 1 and maps + /// the address modes Metal samples otherwise (`ycbcr_modes`). Refused `NotImplemented` where + /// the device holds no such image, and as `new_texture` refuses the image or the view the +- /// conversion writes it through. ++ /// conversion writes it through. Where `alone` and the texture can be, it is made over its ++ /// planes alone (the module docs), refused as the image would be. + pub(crate) fn new_converted_texture( + &self, + call: &'static str, + texels: Texels, + descriptor: &TextureDescriptor, + layout: &TextureLayout, +- id_use: IdUse, ++ (id_use, alone): (IdUse, bool), + (planes, span): (&YCbCrPlanes, Range), + ) -> Result { ++ let asked_usage = descriptor.usage; + let descriptor = TextureDescriptor { + alpha_reads_one: true, + address_mode_map: ycbcr_modes(planes.codes), +@@ -267,9 +354,31 @@ impl Device { + Err(kept) => counts.ycbcr_planes_image_kept(kept), + } + } ++ // Over its planes alone: no image until a slot binding needs one, and no resource ++ // ID, which only a function reading handles would reach it through. ++ let alone = alone && asked_usage == TextureUsage::SHADER_READ; ++ let mut in_place = in_place.ok(); ++ if let (true, Some(p)) = (alone, in_place.as_mut()) { ++ // Refused now as its image would be, which is made later. ++ self.converted_plan(call, &descriptor, layout, planes)?; ++ p.later = Some(ImageLater { ++ descriptor, ++ layout: *layout, ++ planes: *planes, ++ span, ++ }); ++ PLANES_ALONE.fetch_add(1, Ordering::Relaxed); ++ let id_use = match id_use { ++ IdUse::Program(_) => IdUse::Program(IdPlacement::Outside), ++ IdUse::Runtime => IdUse::Runtime, ++ }; ++ let made = (Storage::Texels(texels), in_place); ++ return Ok(Texture::made_with_planes(self, made, record, None, id_use)); ++ } + let storage = + self.converted_storage(call, texels, &descriptor, layout, (planes, span))?; +- let made = (storage, in_place.ok()); ++ WITH_IMAGE.fetch_add(1, Ordering::Relaxed); ++ let made = (storage, in_place); + return Ok(Texture::made_with_planes(self, made, record, None, id_use)); + } + // Over a surface of a P3 matrix: an image of the codes of the code-reading format, its +@@ -286,6 +395,7 @@ impl Device { + .map_err(|_| internal("a P3 texture's descriptor past its own"))?; + let storage = + self.converted_storage(call, texels, &coded, &coded_layout, (planes, span))?; ++ WITH_IMAGE.fetch_add(1, Ordering::Relaxed); + record.read_as = Some(( + code_format, + over_read([S::Green, S::Blue, S::Red], descriptor.swizzle), +@@ -293,18 +403,16 @@ impl Device { + Ok(Texture::made(self, storage, record, None, id_use)) + } + +- /// The storage of a two-plane YCbCr texture of `descriptor` (checked as `layout`) over +- /// `texels`, for Metal's `call`, whose planes `planes` lie in `span` of the texels' buffer: +- /// its image and the conversion's shadow (`new_converted_texture`, and a view as the +- /// texture's `_sRGB` twin, `Texture::converted_view`). +- fn converted_storage( ++ /// The plan of the image of a two-plane YCbCr texture of `descriptor` (checked as `layout`) ++ /// whose planes are `planes`, for Metal's `call`, and the format of the view the conversion ++ /// writes it through. Refused `NotImplemented` where the device holds no such image. ++ fn converted_plan( + &self, + call: &'static str, +- texels: Arc, + descriptor: &TextureDescriptor, + layout: &TextureLayout, +- (planes, span): (&YCbCrPlanes, Range), +- ) -> Result { ++ planes: &YCbCrPlanes, ++ ) -> Result<(crate::image_plan::ImagePlan, Format), Error> { + let pf = layout.pixel_format; + let gap = + || Error::Unsupported(Unsupported::not_implemented(Feature::Format).naming_format(pf)); +@@ -351,8 +459,29 @@ impl Device { + plan.view_formats.push(written); + } + } ++ Ok((plan, written)) ++ } ++ ++ /// The storage of a two-plane YCbCr texture of `descriptor` (checked as `layout`) over ++ /// `texels`, for Metal's `call`, whose planes `planes` lie in `span` of the texels' buffer: ++ /// its image and the conversion's shadow (`new_converted_texture`, and a view as the ++ /// texture's `_sRGB` twin, `Texture::converted_view`). ++ fn converted_storage( ++ &self, ++ call: &'static str, ++ texels: Arc, ++ descriptor: &TextureDescriptor, ++ layout: &TextureLayout, ++ (planes, span): (&YCbCrPlanes, Range), ++ ) -> Result { ++ let pf = layout.pixel_format; ++ let (plan, written) = self.converted_plan(call, descriptor, layout, planes)?; + let hazard = texels.options().hazard_tracking_mode()?; + let image = self.new_image(plan, layout, descriptor, hazard)?; ++ let image_bytes = match &image.owner { ++ super::Owner::Runtime { placement, .. } => placement.size(), ++ _ => 0, ++ }; + // The conversion's set holds it as long as the image (`Image::own_view`). + let view = image.own_view(&VkView { + view_type: ImageViewType::TYPE_2D, +@@ -367,6 +496,7 @@ impl Device { + })?; + let (raw, at) = texels.buffer.at(span.start); + let conversion = YCbCrConversion::new(self, (planes, span.clone()), (raw, at), view)?; ++ let alive = ImageAlive::of(image_bytes + conversion.staging_bytes()); + let (bytes, tracked) = texels.buffer.tracked(span); + let copies = ShadowCopies { + source: ShadowSource::Converted { +@@ -376,6 +506,7 @@ impl Device { + alias: texels.buffer.alias().map(|alias| alias.key), + conversion: Box::new(conversion), + pixel_format: pf, ++ _alive: alive, + }, + shadow_key: image.identity.key, + shadow_range: UseRange::Image { +@@ -400,6 +531,47 @@ impl Device { + } + + impl Texture { ++ /// Whether this two-plane YCbCr texture is over its planes alone (the module docs): made with ++ /// [`IoSurfaceYCbCr::with_planes_alone`](crate::IoSurfaceYCbCr::with_planes_alone), it has ++ /// no image until a slot binding needs one, and serves slot bindings alone. Its caller makes ++ /// the texture again without that for any other call that names it. ++ pub fn is_planes_alone(&self) -> bool { ++ self.image_later().is_some() ++ } ++ ++ /// What the image of a texture over its planes alone is made of (the module docs); `None` ++ /// for every other texture. ++ pub(super) fn image_later(&self) -> Option<&ImageLater> { ++ self.0.planes_in_place.as_ref()?.later.as_ref() ++ } ++ ++ /// The image of a texture over its planes alone and the copies that convert the planes into ++ /// it, made at the first slot binding that needs them (`texture/usage_shadow.rs`), as ++ /// `call`'s: what a texture made with its image holds from the start. Internal for another ++ /// texture; refused as that texture's image and conversion are. ++ pub(super) fn later_image( ++ &self, ++ call: &'static str, ++ ) -> Result<(Arc, Arc), Error> { ++ let (Some(later), Storage::Texels(texels)) = (self.image_later(), &self.0.storage) else { ++ return Err(internal( ++ "an image made later for a texture made with its image", ++ )); ++ }; ++ let storage = self.device().converted_storage( ++ call, ++ Arc::clone(texels), ++ &later.descriptor, ++ &later.layout, ++ (&later.planes, later.span.clone()), ++ )?; ++ let Storage::Shadow { image, copies, .. } = storage else { ++ return Err(internal("a converted storage that is no shadow")); ++ }; ++ IMAGES_LATER.fetch_add(1, Ordering::Relaxed); ++ Ok((image, copies)) ++ } ++ + /// The uniform texel buffer view of the texture's planes read in place (the module docs), made + /// at its first bind and kept with the texels. Internal for a texture whose planes are not. + pub(crate) fn planes_view(&self) -> Result { +diff --git a/crates/metal2vulkan-runtime/src/ycbcr.rs b/crates/metal2vulkan-runtime/src/ycbcr.rs +index 98d522c..e6c916c 100644 +--- a/crates/metal2vulkan-runtime/src/ycbcr.rs ++++ b/crates/metal2vulkan-runtime/src/ycbcr.rs +@@ -302,6 +302,11 @@ pub struct IoSurfaceYCbCr { + pub chroma_bytes_per_row: u64, + /// The surface's `kIOSurfaceYCbCrMatrix` as the texture is made, `None` without one. + pub matrix: Option, ++ /// Whether a texture whose planes a function reads in place, of `ShaderRead` usage alone, is ++ /// made over its planes alone (`texture/ycbcr_image.rs`): with no image until a slot binding ++ /// needs one, and for slot bindings only. `false` unless the caller asks ++ /// ([`with_planes_alone`](Self::with_planes_alone)). ++ pub planes_alone: bool, + } + + impl IoSurfaceYCbCr { +@@ -321,9 +326,18 @@ impl IoSurfaceYCbCr { + chroma_offset, + chroma_bytes_per_row, + matrix: None, ++ planes_alone: false, + } + } + ++ /// These facts for a caller that binds the texture at slots only while it is over its planes ++ /// alone ([`Texture::is_planes_alone`](crate::Texture::is_planes_alone)) and makes the ++ /// texture without this for any other call. ++ pub const fn with_planes_alone(mut self) -> IoSurfaceYCbCr { ++ self.planes_alone = true; ++ self ++ } ++ + /// These facts with the surface's `kIOSurfaceYCbCrMatrix`. + pub const fn with_matrix(mut self, matrix: SurfaceYCbCrMatrix) -> IoSurfaceYCbCr { + self.matrix = Some(matrix); +@@ -798,6 +812,11 @@ impl YCbCrConversion { + }) + } + ++ /// The bytes of its staging buffer, which it holds for the texture's life. ++ pub(super) fn staging_bytes(&self) -> u64 { ++ self.staging.length() ++ } ++ + /// The planes it converts, and the bytes of their buffer they lie in. + pub(super) fn source(&self) -> (YCbCrPlanes, Range) { + (self.source.0, self.source.1.clone()) diff --git a/bridge/ycbcr_planes_host.mm b/bridge/ycbcr_planes_host.mm new file mode 100644 index 0000000..ffa6077 --- /dev/null +++ b/bridge/ycbcr_planes_host.mm @@ -0,0 +1,208 @@ +// SPDX-License-Identifier: MIT +// A two-plane YCbCr surface texture (a video frame, MTLPixelFormat 500 over a '420v' IOSurface) that is only sampled is made over +// the surface's planes alone: no converted image is made for it. Real GPU pixels of the planes path are held against the converted +// path's; a binding that cannot read the planes in place and a call other than a slot binding still get the image; a surface whose +// texture and owner went stays imported while a command buffer in flight samples it. Mode `converted` runs with +// NATIVE_METAL_YCBCR_PLANES=0 (the boot argument gpu-ycbcr-planes=0): every texture is made with its image, as before. +// Built as surface_import_host is: the frontend's surface imports and buffer releases are counted through the tracked_ wrappers. +#import +#import +#import +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "../NativeBridgeMetalFrontend.h" +#include "NativeMetalObjects.h" + +static std::atomic imports, releases; +static std::mutex handlesLock; +static std::set handles; +extern "C" NmbBuffer *tracked_surface_import_releasing(NmbDevice *d, void *p, uint64_t n, uint64_t options, + void (*release)(void *, void *, uint64_t), void *context) { + NmbBuffer *buffer = nmb_buffer_new_no_copy_releasing(d,p,n,options,release,context); + if (buffer) {std::lock_guard lock(handlesLock);handles.insert(buffer);imports++;} + return buffer; +} +extern "C" void tracked_buffer_release(NmbBuffer *buffer) { + {std::lock_guard lock(handlesLock);if(handles.erase(buffer))releases++;} + nmb_buffer_release(buffer); +} + +static const unsigned W=64,H=32; +// A '420v' surface: plane 0 the luma bytes, plane 1 the Cb,Cr pairs of each 2 x 2 block, rows 64 bytes apart, filled from `seed`. +static IOSurfaceRef makeSurface(unsigned seed) { + NSDictionary *luma=@{(id)kIOSurfacePlaneWidth:@(W),(id)kIOSurfacePlaneHeight:@(H),(id)kIOSurfacePlaneBytesPerRow:@64, + (id)kIOSurfacePlaneOffset:@0,(id)kIOSurfacePlaneSize:@(64*H),(id)kIOSurfacePlaneBytesPerElement:@1}; + NSDictionary *chroma=@{(id)kIOSurfacePlaneWidth:@(W/2),(id)kIOSurfacePlaneHeight:@(H/2),(id)kIOSurfacePlaneBytesPerRow:@64, + (id)kIOSurfacePlaneOffset:@(64*H),(id)kIOSurfacePlaneSize:@(64*H/2),(id)kIOSurfacePlaneBytesPerElement:@2}; + IOSurfaceRef surface=IOSurfaceCreate((__bridge CFDictionaryRef)@{(id)kIOSurfaceWidth:@(W),(id)kIOSurfaceHeight:@(H), + (id)kIOSurfacePixelFormat:@((uint32_t)0x34323076),(id)kIOSurfaceAllocSize:@(64*H*3/2),(id)kIOSurfacePlaneInfo:@[luma,chroma]}); + if(!surface || IOSurfaceLock(surface,0,nullptr))return nullptr; + uint8_t *bytes=(uint8_t *)IOSurfaceGetBaseAddress(surface);uint32_t state=seed*2654435761u+1; + for(unsigned i=0;i<64*H*3/2;++i){state=state*1664525u+1013904223u;bytes[i]=(uint8_t)(16+(state>>24)%220);} + IOSurfaceUnlock(surface,0,nullptr); + return surface; +} +static id frame(id device,IOSurfaceRef surface,MTLTextureUsage usage) { + auto d=[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:(MTLPixelFormat)500 width:W height:H mipmapped:NO]; + d.usage=usage; + return [device newTextureWithDescriptor:d iosurface:surface plane:0]; +} +// planes, conv, later, whole, images alive, their MiB: the telemetry's YC row (absent before the first such texture: zeros). +struct Counts {unsigned long long planes,conv,later,whole,images,mib;}; +static Counts counts() { + char text[8192]={};native_bridge_metal_telemetry(text,sizeof text);Counts c={}; + const char *row=strstr(text,"YC planes "); + if(row)sscanf(row,"YC planes %llu conv %llu later %llu whole %llu | img %llu %lluM",&c.planes,&c.conv,&c.later,&c.whole,&c.images,&c.mib); + return c; +} +struct Rig { + id device;id queue;id sampled,fetched;id nearest; + // Draws `texture` over a W x H half-float target with `pipeline` into `commands`, then copies the target into `out`. + void draw(id commands,id pipeline,id texture,id out) { + auto td=[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA16Float width:W height:H mipmapped:NO]; + td.usage=MTLTextureUsageRenderTarget;td.storageMode=MTLStorageModePrivate; + id target=[device newTextureWithDescriptor:td]; + auto pass=[MTLRenderPassDescriptor renderPassDescriptor]; + pass.colorAttachments[0].texture=target;pass.colorAttachments[0].loadAction=MTLLoadActionClear; + pass.colorAttachments[0].storeAction=MTLStoreActionStore; + id e=[commands renderCommandEncoderWithDescriptor:pass]; + [e setRenderPipelineState:pipeline];[e setFragmentTexture:texture atIndex:0];[e setFragmentSamplerState:nearest atIndex:0]; + [e drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:3];[e endEncoding]; + id blit=[commands blitCommandEncoder]; + [blit copyFromTexture:target sourceSlice:0 sourceLevel:0 sourceOrigin:MTLOriginMake(0,0,0) sourceSize:MTLSizeMake(W,H,1) + toBuffer:out destinationOffset:0 destinationBytesPerRow:W*8 destinationBytesPerImage:W*H*8]; + [blit endEncoding]; + } + // The halves a draw of `texture` with `pipeline` leaves; empty where the command buffer did not complete. + std::vector pixels(id pipeline,id texture) { + id out=[device newBufferWithLength:W*H*8 options:MTLResourceStorageModeShared]; + id commands=[queue commandBuffer]; + draw(commands,pipeline,texture,out);[commands commit];[commands waitUntilCompleted]; + if(commands.status!=MTLCommandBufferStatusCompleted){std::printf("command buffer: %s\n",commands.error.description.UTF8String);return {};} + const uint16_t *halves=(const uint16_t *)out.contents; + return std::vector(halves,halves+W*H*4); + } +}; +static float half(uint16_t h) { + const int e=(h>>10)&31,m=h&1023;const float v=e==0?std::ldexp((float)m,-24):std::ldexp((float)(m+1024),e-25); + return h&0x8000?-v:v; +} +// The largest difference between two pictures' channels; 9 where either is missing. +static float apart(const std::vector &a,const std::vector &b) { + if(a.empty() || a.size()!=b.size())return 9; + float most=0;for(size_t i=0;i\nusing namespace metal;\n" + "struct V {float4 p [[position]]; float2 uv;};\n" + "vertex V full(uint id [[vertex_id]]) {float2 p[3]={float2(-1,-1),float2(3,-1),float2(-1,3)};" + " V v; v.p=float4(p[id],0,1); v.uv=float2(p[id].x*0.5+0.5,0.5-p[id].y*0.5); return v;}\n" + "fragment float4 sampled(V v [[stage_in]], texture2d t [[texture(0)]], sampler s [[sampler(0)]]) {return t.sample(s,v.uv);}\n" + "fragment float4 fetched(V v [[stage_in]], texture2d t [[texture(0)]]) {return t.read(uint2(v.p.xy));}\n" options:nil error:&error]; + if(!library){std::printf("library: %s\n",error.description.UTF8String);return 2;} + auto pd=[MTLRenderPipelineDescriptor new];pd.vertexFunction=[library newFunctionWithName:@"full"]; + pd.colorAttachments[0].pixelFormat=MTLPixelFormatRGBA16Float; + pd.fragmentFunction=[library newFunctionWithName:@"sampled"];rig.sampled=[rig.device newRenderPipelineStateWithDescriptor:pd error:&error]; + pd.fragmentFunction=[library newFunctionWithName:@"fetched"];rig.fetched=[rig.device newRenderPipelineStateWithDescriptor:pd error:&error]; + rig.nearest=[rig.device newSamplerStateWithDescriptor:[MTLSamplerDescriptor new]]; + if(!rig.sampled || !rig.fetched || !rig.nearest){std::printf("pipeline: %s\n",error.description.UTF8String);return 2;} + unsigned failures=0; + auto check=[&](bool ok,const char *what){if(!ok){++failures;std::printf("FAILED %s\n",what);}}; + IOSurfaceRef surface=makeSurface(1);if(!surface)return 2; + + // 1. A frame that is only sampled: over its planes alone in `planes` mode, with its image otherwise. + id light=frame(rig.device,surface,MTLTextureUsageShaderRead);if(!light)return 2; + const auto sampled=rig.pixels(rig.sampled,light); + Counts c=counts(); + std::printf("sampled: planes %llu conv %llu later %llu whole %llu images %llu\n",c.planes,c.conv,c.later,c.whole,c.images); + check(planes?(c.planes==1 && c.conv==0 && c.later==0 && c.images==0):(c.planes==0 && c.conv==1 && c.images==1),"the sampled frame's path"); + + // 2. The converted path's pixels: a frame whose usage needs its image, read texel by texel from that image. + id whole=frame(rig.device,surface,MTLTextureUsageShaderRead|MTLTextureUsagePixelFormatView);if(!whole)return 2; + const auto converted=rig.pixels(rig.fetched,whole); + c=counts(); + check(c.planes==(planes?1:0) && c.conv==(planes?1:2) && c.images==(planes?1:2),"a frame of another usage keeps its image"); + const float difference=apart(sampled,converted); + std::printf("planes path against converted path: largest difference %.6f\n",difference); + check(difference<=1.0f/1024,"the sampled frame's pixels are the converted image's"); + bool flat=true;for(size_t i=4;i copied=frame(rig.device,other,MTLTextureUsageShaderRead); + id out=[rig.device newBufferWithLength:W*H options:MTLResourceStorageModeShared]; + id commands=[rig.queue commandBuffer];id blit=[commands blitCommandEncoder]; + [blit copyFromTexture:copied sourceSlice:0 sourceLevel:0 sourceOrigin:MTLOriginMake(0,0,0) sourceSize:MTLSizeMake(W,H,1) + toBuffer:out destinationOffset:0 destinationBytesPerRow:W destinationBytesPerImage:W*H]; + [blit endEncoding];[commands commit];[commands waitUntilCompleted]; + check(commands.status==MTLCommandBufferStatusCompleted,"a copy out of the frame completes"); + check(!memcmp(out.contents,IOSurfaceGetBaseAddress(other),W*H),"the copy moved the luma rows"); + const Counts after=counts(); + check(planes?(after.planes==c.planes+1 && after.whole==1):(after.whole==0 && after.conv==c.conv+1),"the copy made the frame whole"); + check(!rig.pixels(rig.sampled,copied).empty(),"the whole frame still draws"); + CFRelease(other); + } + + // 5. In flight: the texture and every owner's reference to its surface go while the command buffer that samples it waits. + { + // The earlier frames' imports whose surfaces went are let go first. + for(unsigned i=0;i<20;++i){native_bridge_metal_trim_surface_imports();usleep(5000);} + const unsigned importsBefore=imports.load(),releasesBefore=releases.load(); + id event=[rig.device newSharedEvent];id commands=[rig.queue commandBuffer]; + id out=[rig.device newBufferWithLength:W*H*8 options:MTLResourceStorageModeShared]; + @autoreleasepool { + IOSurfaceRef held=makeSurface(1);id texture=frame(rig.device,held,MTLTextureUsageShaderRead); + [commands encodeWaitForEvent:event value:1]; + rig.draw(commands,rig.sampled,texture,out); + texture=nil;CFRelease(held); + } + [commands commit]; + native_bridge_metal_trim_surface_imports();usleep(100000);native_bridge_metal_trim_surface_imports(); + check(imports.load()==importsBefore+1 && releases.load()==releasesBefore,"the surface stays imported while its frame is in flight"); + event.signaledValue=1;[commands waitUntilCompleted]; + check(commands.status==MTLCommandBufferStatusCompleted,"the gated draw completes"); + const uint16_t *halves=(const uint16_t *)out.contents; + check(apart(std::vector(halves,halves+W*H*4),converted)<=1.0f/1024,"the frame in flight drew its picture"); + commands=nil; + check(drained(releasesBefore+1),"the import goes once the command buffer completed"); + } + light=nil;whole=nil;CFRelease(surface); + c=counts(); + std::printf("end: planes %llu conv %llu later %llu whole %llu images %llu\n",c.planes,c.conv,c.later,c.whole,c.images); + if(failures){std::printf("FAIL: two-plane YCbCr surface textures, %s mode (%u checks)\n",argv[3],failures);return 1;} + std::printf("PASS: two-plane YCbCr surface textures, %s mode: %s\n",argv[3],planes? + "sampled over the planes alone with the converted path's pixels, image made only where needed, surface kept in flight": + "every frame made with its converted image, same pixels"); + } +}