test(gpu): a Metal suite case times eight frames sampling a 3840x2160 10-bit 4:2:0 video surface and checks the frames after a CPU write see it

This commit is contained in:
Tyler Smith committed 2026-11-16 16:56:32 -05:00
1 parent fda4ece2a2
commit dd76724be3
3 files changed
+110

No files matched your search

@@ -82,6 +82,25 @@ kernel void surface_two_plane_wide(texture2d<float, access::sample> t [[texture(
sampled[i] = t.sample(linear, (float2(p) + 0.5f) / float2(n));
}
/* A compositor's frame over a video surface: the whole surface sampled
* into a target of the same size. */
kernel void surface_frame(texture2d<float, access::sample> t [[texture(0)]],
texture2d<half, access::write> target [[texture(1)]],
uint2 p [[thread_position_in_grid]], uint2 n [[threads_per_grid]])
{
constexpr sampler linear(filter::linear, address::clamp_to_edge);
target.write(half4(t.sample(linear, (float2(p) + 0.5f) / float2(n))), p);
}
/* 16 texels of a frame's surface, spread over it, from `first` on. */
kernel void surface_probe(texture2d<float, access::sample> t [[texture(0)]],
device float4 *probes [[buffer(0)]], constant uint &first [[buffer(1)]],
uint i [[thread_position_in_grid]])
{
uint2 at = uint2((i * 241u + 17u) % t.get_width(), (i * 137u + 5u) % t.get_height());
probes[first + i] = t.read(at);
}
kernel void archived(device uint *data [[buffer(0)]], constant uint &unused [[buffer(1)]], uint i [[thread_position_in_grid]])
{
data[i] = data[i] * 3u + 1u;
@@ -5458,6 +5458,67 @@ static void host_two_plane(Case *c, id<MTLCommandQueue> queue, NSDictionary *h)
CFRelease(s);
}
/* Writes a 16-bit word plane of `comps` components a pixel with a ramp
* that `variant` shifts, under a write lock, which changes the seed. */
static void frames_plane(IOSurfaceRef s, size_t plane, size_t comps, uint32_t variant)
{
IOSurfaceLock(s, 0, NULL);
uint8_t *base = IOSurfaceGetBaseAddressOfPlane(s, plane);
size_t stride = IOSurfaceGetBytesPerRowOfPlane(s, plane);
size_t w = IOSurfaceGetWidthOfPlane(s, plane) * comps, height = IOSurfaceGetHeightOfPlane(s, plane);
for (size_t y = 0; y < height; y++) {
uint16_t *row = (uint16_t *)(base + y * stride);
for (size_t x = 0; x < w; x++)
row[x] = (uint16_t)(((x * 7u + y * 13u + variant * 389u) % 1024u) << 6);
}
IOSurfaceUnlock(s, 0, NULL);
}
/* A compositor's frames over a large 10-bit 4:2:0 video surface, as
* Mission Control draws a video wallpaper: each frame samples the whole
* surface into a private target and reads 16 probes of it, and the CPU
* rewrites the luma before frame `change`. The case's gpu_ms is the frames'
* GPU time; the probes show each frame saw the surface's current rows. */
static void host_two_plane_frames(Case *c, id<MTLCommandQueue> queue, NSDictionary *h)
{
size_t w = number(h, @"width", 1), height = number(h, @"height", 1);
uint32_t frames = (uint32_t)number(h, @"frames", 1), change = (uint32_t)number(h, @"change", 0);
IOSurfaceRef s = two_plane_surface(w, height, w / 2, height / 2, 2, 'x420');
frames_plane(s, 0, 1, 0);
frames_plane(s, 1, 2, 0);
id<MTLTexture> t = surface_texture(c, s, 0, (MTLPixelFormat)number(h, @"format", 0), MTLTextureUsageShaderRead);
MTLTextureDescriptor *d = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA16Float
width:w
height:height
mipmapped:NO];
d.usage = MTLTextureUsageShaderWrite;
d.storageMode = MTLStorageModePrivate;
id<MTLTexture> target = [c.device newTextureWithDescriptor:d];
if (!target)
@throw failure(@"no frame target");
id<MTLComputePipelineState> frame = host_pipeline(c, @"surface_frame");
id<MTLComputePipelineState> probe = host_pipeline(c, @"surface_probe");
for (uint32_t f = 0; f < frames; f++) {
if (f == change)
frames_plane(s, 0, 1, 1);
id<MTLCommandBuffer> cb = [queue commandBuffer];
id<MTLComputeCommandEncoder> e = [cb computeCommandEncoder];
[e setComputePipelineState:frame];
[e setTexture:t atIndex:0];
[e setTexture:target atIndex:1];
[e dispatchThreads:MTLSizeMake(w, height, 1) threadsPerThreadgroup:MTLSizeMake(8, 8, 1)];
uint32_t first = f * 16;
[e setComputePipelineState:probe];
[e setTexture:t atIndex:0];
[e setBuffer:c.buffers[@"probes"] offset:0 atIndex:0];
[e setBytes:&first length:sizeof first atIndex:1];
[e dispatchThreads:MTLSizeMake(16, 1, 1) threadsPerThreadgroup:MTLSizeMake(16, 1, 1)];
[e endEncoding];
wait_for(cb, c.spec);
}
CFRelease(s);
}
/* Copies both planes of a 4:4:4 two-plane surface into `out`: the first
* plane's bytes, then the second's pairs, tight. */
static void two_plane_out(IOSurfaceRef s, id<MTLBuffer> out, size_t w, size_t height)
@@ -8084,6 +8145,7 @@ static void run_host(Case *c, id<MTLCommandQueue> queue, NSDictionary *spec)
@"iosurface_buffer" : [NSValue valueWithPointer:host_iosurface_buffer],
@"two_plane" : [NSValue valueWithPointer:host_two_plane],
@"two_plane_render" : [NSValue valueWithPointer:host_two_plane_render],
@"two_plane_frames" : [NSValue valueWithPointer:host_two_plane_frames],
@"share" : [NSValue valueWithPointer:host_share],
@"archive" : [NSValue valueWithPointer:host_archive],
@"errors" : [NSValue valueWithPointer:host_errors],
+29
View File
@@ -415,6 +415,34 @@ fn two_plane_ten_bit(plan: &mut Plan, library: &str, rng: &mut Random) {
}
}
/// A compositor's frames over a 3840x2160 10-bit 4:2:0 video surface
/// (`host_two_plane_frames`), as Mission Control draws a video wallpaper:
/// eight frames each sample the whole surface into a private target, and
/// the CPU rewrites the luma before the fifth. The probes, 16 texels a
/// frame, agree with the Mac's, so each frame saw the surface's current
/// rows; gpu_ms is the frames' GPU time.
fn two_plane_frames(plan: &mut Plan, library: &str) {
let frames = 8;
plan.add(
Case::host(
"host.iosurface.two_plane_frames_4k",
library,
json!({"kind": "two_plane_frames", "format": 505, "width": 3840, "height": 2160,
"frames": frames, "change": 4}),
)
.buffer(Buffer::zeroed("probes", 0, frames * 16 * 16).unbound())
// A 10-bit step of each read, and the conversion's 1/2048 twice.
.output(Output::new(
"probes",
Dtype::F32,
Tolerance::abs(1.0 / 1023.0 + 4.0 / 2048.0),
))
.note(
"gpu_ms is eight frames sampling a 4K video surface, the last four after a CPU write",
),
);
}
/// A 4:4:4 two-plane format's planes of an RGB color, in whole steps:
/// green and blue-red, or full-range BT.601 luma and chroma whose chroma is
/// scaled by 256 / 255 around 128, as Apple GPUs store them.
@@ -1658,6 +1686,7 @@ pub fn build(plan: &mut Plan) {
iosurface_buffer(plan, &library, &mut Random::new(1320));
two_plane_render(plan, &library, &mut Random::new(1304));
two_plane_ten_bit(plan, &library, &mut Random::new(1305));
two_plane_frames(plan, &library);
share(plan, &library, &mut rng);
archive(plan, &library, &mut rng);
errors(plan, &library);