🐛 Make BC textures over a surface where they answered nil

Since c13-contract (ae37ce1c2) the bundle answered nil for a BC1 to BC7
texture over an IOSurface or one of its planes, before it took a view of the
surface. Metal on the x86 Macs makes these wherever the rows fit, over a
surface with planes or without, in Managed or Private storage
(oracle_surftex, Q20). The nil was the host's limit: pin 14's runtime made
no texture of one over a surface's rows (NotLinear). metal2vulkan's stack
34, at this branch's pin, makes them in the x86 Macs' form, row j of blocks
at the plane's row j (on MoltenVK as a shadow image), and the bundle's own
CPU access takes the same rows since 187d1ee33. Runtime gaps are
metal2vulkan's, and ours go at the pin that fixes them.

What changes for the guest: newTextureWithDescriptor:iosurface:plane: of a
BC format answers a texture and posts its create, where it answered nil and
logged "no texture (descriptor)". The shapes stack 34 still refuses (a BC
texture over a plane at an offset or rows off 16 bytes, or wider than the
plane's rows) are posted and refused as the host's other refused shapes are
(decode/refused.rs). On the Metal platform, whose buffer creator answers nil
for BC as Metal's IOSurface creator does (87b24e27e), the texture is a
refused create too.

Test: test_iosurface_block_compressed_textures_are_made fails at the base,
where the bundle answers nil.
This commit is contained in:
Anees Iqbal committed 2026-11-05 21:59:37 -08:00
1 parent bbb493d81a
commit 892a0eefb0
4 files changed
+148 -54

No files matched your search

+26 -9
View File
@@ -954,15 +954,32 @@ What our stack does:
RgfxTexture.h). The bundle had sent each of these to the host, posted, and answered a texture;
the host refused most, and the guest's command buffers naming the texture then failed. That
includes every two-plane format, which the lane made as the M3 does until 11-05 (Q13).
- *(outside the envelope, the host's limit)* A block-compressed format over a surface answers
nil (`RgfxIOSurfaceTexture`). Both x86 Macs make it wherever its rows fit, but the host makes no
texture of one over a surface's rows (metal2vulkan's `NotLinear`), and a texture the host then
refused would fail every command buffer naming it. It is the host runtime's gap.
- What the x86 Macs make, the host also refuses in places: over a planar surface's plane, a
texture wider than its rows or taller than the plane, a mipmapped one, and one of a type other
than 2D (all made on the M3 too, but the Cube); and a TextureBuffer over any surface. The bundle
sends these, posted, and the host's refusal fails the command buffers that name them
(`refused.rs`). They are the host runtime's gaps.
- *(class 3)* A BC1 to BC7 texture over a surface is made and posted, as both x86 Macs make it
wherever its rows fit (`RgfxIOSurfaceTexture`). The Vulkan runtime makes it in their form since
metal2vulkan's stack 34, row j of its blocks at the plane's row j, over a surface without planes
and over a plane at an offset and rows that are multiples of 16 bytes
(`bc_textures_over_a_surface_s_rows_are_made`). The bundle's own CPU access takes the same rows
(`test_iosurface_block_compressed_textures_are_made`). Until then the bundle answered nil for
every block-compressed texture over a surface, as the runtime made none.
- *(outside the envelope, the host's limit)* The Metal platform answers nil for a BC texture over
a surface's plane: it makes the texture over the buffer that stands for the surface, and Metal's
buffer creator answers nil for BC1 and BC7, as the M3's own IOSurface creator does
(`bc_textures_over_a_surface_s_plane_are_metals`, measured on the M3 under macOS 27). The
guest's texture of one is then a refused create there (`refused.rs`).
- What the x86 Macs make, the Vulkan runtime still refuses in places, as its
`NotImplemented(SurfacePlaneShape)` at stack 34, where the three GPUs lay the texels out no one
way: over a planar surface's plane, a texture wider than its rows, a mipmapped one, a 2DArray of
more than one slice, a 3D, Cube or 2DMultisample one, a 1D one wider than a row, and a BC
texture or a TextureBuffer at an offset or rows off 16 bytes; and, over any surface, a texture
past the surface's end (`TexelsPastBufferLength`). The bundle sends these, posted, and the
host's refusal fails the command buffers that name them (`refused.rs`). They are the host
runtime's gaps. Stack 34 makes a TextureBuffer over any surface, and over a planar surface's
plane a 1D texture whose row fits and a 2DArray of one slice, each laid out as the 2D texture of
its rows.
- *(outside the envelope, the runtime's M3 form)* A texture taller than its plane, inside the
surface, is made as the M3 lays it out, its rows past the plane at the plane's pitch. On both
x86 GPUs the bytes past the plane are left unwritten (metal2vulkan's byte probe), which the
runtime cannot follow, as it is given no plane height.
## Q21: pipelines' reflection
+8 -7
View File
@@ -145,11 +145,11 @@ NS_ASSUME_NONNULL_BEGIN
/// A texture of shape `descriptor` over plane `plane` of `surface`, over the plane's GPU
/// view, taken on first use: the surface's pages in place, else a copy in its shared block or a
/// block of this process's own in `region`, synced from the surface's pages. Nil when the
/// surface has no such plane, its format is block-compressed (which Metal makes, but the host
/// makes no texture of over a surface's rows), its plane has no CPU mapping, the region has no
/// room, or
/// no texture resource id is left. The device's creator first ends the process where Metal does
/// (rgfxEndAtInvalidSurfaceTexture).
/// surface has no such plane, its plane has no CPU mapping, the region has no room, or no
/// texture resource id is left. The device's creator first ends the process where Metal does
/// (rgfxEndAtInvalidSurfaceTexture). A block-compressed texture is made as any other, its row j
/// of blocks at the plane's row j, as the AMD 5300M and the Intel 630 make one wherever its rows
/// fit (oracle_surftex, Q20).
+ (nullable instancetype)textureWithDescriptor:(MTLTextureDescriptor *)descriptor
iosurface:(IOSurfaceRef)surface
plane:(NSUInteger)plane
@@ -384,8 +384,9 @@ void rgfxIOSurfaceWriteBack(const std::vector<std::shared_ptr<void>> &writes);
bool rgfxIOSurfaceWritesInPlace(const std::vector<std::shared_ptr<void>> &writes);
/// How many textures over `surface` were answered nil for `cause` ("arguments", "plane",
/// "descriptor", "plane shape", "region", "buffer", "resource id", "texture"), and the "RGFX:"
/// line logged for the first, without the prefix (for the tests): 0 and empty where none was.
/// "plane shape", "region", "buffer", "origin", "resource id", "create", "texture"), and the
/// "RGFX:" line logged for the first, without the prefix (for the tests): 0 and empty where none
/// was.
uint64_t rgfxIOSurfaceNilLog(IOSurfaceRef _Nullable surface, const char *_Nonnull cause,
std::string *_Nonnull line);
-25
View File
@@ -1356,27 +1356,6 @@ void rgfxIOSurfaceKeepCountClients(bool on)
gCountsClients.store(on ? 1 : 0, std::memory_order_relaxed);
}
namespace {
// Why the bundle answers nil for a texture of `descriptor` over a surface, which every rule Metal
// ends the process at passed (rgfxEndAtInvalidSurfaceTexture); null where it makes one. A
// block-compressed format (BC1 to BC7): the AMD 5300M and the Intel 630 under macOS 26.7.1 make
// the texture wherever its rows fit, over a surface with planes or without, in Managed or Private
// storage (oracle_surftex, Q20), but the host's runtime makes none over a surface's rows
// (metal2vulkan's NotLinear). The nil is the host's limit, not Metal's: a texture the host then
// refused would fail every command buffer that named it, where the guest's frameworks can take a
// nil and go another way.
const char *rgfxSurfaceTextureNil(MTLTextureDescriptor *descriptor)
{
const uint64_t f = descriptor.pixelFormat;
auto in = [f](uint64_t lo, uint64_t hi) { return f >= lo && f <= hi; };
if (in(130, 135) || in(140, 143) || in(150, 153))
return "a block-compressed format over a surface, which the host makes no texture of";
return nullptr;
}
} // namespace
@interface RgfxIOSurfaceTexture ()
- (std::shared_ptr<RgfxSurfaceShadow>)rgfxShadow;
@end
@@ -1423,10 +1402,6 @@ const char *rgfxSurfaceTextureNil(MTLTextureDescriptor *descriptor)
logNoTexture(surface, "plane", format("plane %lu, past its planes", (unsigned long)plane));
return nil;
}
if (const char *why = rgfxSurfaceTextureNil(descriptor)) {
logNoTexture(surface, "descriptor", why);
return nil;
}
// shadowFor logs why it made no view.
std::shared_ptr<RgfxSurfaceShadow> shadow = shadowFor(surface, region, device, serializer);
if (shadow == nullptr) {
+114 -13
View File
@@ -11572,6 +11572,119 @@ void test_iosurface_cpu_access_in_the_texture_s_format()
CFRelease(s);
}
// A BC1 or BC7 texture over a surface is made, as the AMD 5300M and the Intel 630 make one
// wherever its rows fit (oracle_surftex, Q20), and its create posted, over a surface without
// planes and over either plane of a two-plane one, at the plane's place in the copy and its rows
// 256 bytes apart. Its CPU access reads and writes its rows of blocks, row j at the plane's row j.
void test_iosurface_block_compressed_textures_are_made()
{
Fixture fx;
int calls = 0;
fx.ids.setResourceIdSource(&oneChunk, &calls);
std::vector<uint8_t> mem(RGFX_ALLOCATIONS_OFFSET + 0x100000, 0);
RgfxRegion region(mem.data(), mem.size());
BlockLog blocks;
region.setBlockSource(&BlockLog::alloc, &BlockLog::release, &blocks);
SurfaceBlockAt src{RGFX_ALLOCATIONS_OFFSET + 0x40000};
region.setSurfaceSource(&SurfaceBlockAt::acquire, &SurfaceBlockAt::release, &src);
const uint64_t kPitch = 256;
auto over = [&](IOSurfaceRef s, MTLPixelFormat format, NSUInteger w, NSUInteger h,
NSUInteger plane) {
MTLTextureDescriptor *td = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:format
width:w
height:h
mipmapped:NO];
return [RgfxIOSurfaceTexture textureWithDescriptor:td
iosurface:s
plane:plane
region:&region
device:nil
serializer:&fx.ser];
};
auto posted = [&](uint32_t format, uint32_t w, uint32_t h, uint64_t offset, uint32_t plane) {
auto m = expectMessage<RgfxSurfaceTextureMessage>(fx.ring, RGFX_CMD_TEXTURE_FROM_IOSURFACE);
return m.texture.desc.pixel_format == format && m.texture.desc.width == w &&
m.texture.desc.height == h && m.texture.offset == offset &&
m.texture.bytes_per_row == kPitch && m.texture.plane == plane;
};
// A 64 x 64 'BGRA' surface, 256 bytes a row: a BC1 texture of 16 blocks of 8 bytes a row, and
// a BC7 one of 16 of 16.
IOSurfaceRef flat = filledBgraSurface(64, 64);
CHECK(IOSurfaceGetBytesPerRow(flat) == 256);
const uint8_t *copy = mem.data() + src.offset;
@autoreleasepool {
RgfxIOSurfaceTexture *bc1 = over(flat, MTLPixelFormatBC1_RGBA, 64, 64, 0);
CHECK(bc1 != nil);
(void)expectMessage<RgfxBufferOverHostMemory>(fx.ring, RGFX_CMD_BUFFER_OVER_HOST_MEMORY);
CHECK(posted(MTLPixelFormatBC1_RGBA, 64, 64, 0, 0));
RgfxIOSurfaceTexture *bc7 = over(flat, MTLPixelFormatBC7_RGBAUnorm, 64, 64, 0);
CHECK(bc7 != nil && posted(MTLPixelFormatBC7_RGBAUnorm, 64, 64, 0, 0));
CHECK(bc1.pixelFormat == MTLPixelFormatBC1_RGBA && bc7.pixelFormat == MTLPixelFormatBC7_RGBAUnorm);
// The BC1 texture's blocks from (4, 4), two by two: 16 bytes of rows 1 and 2 from byte 8.
std::vector<uint8_t> out(2 * 16 + 16, 0xee);
[bc1 getBytes:out.data() bytesPerRow:16 fromRegion:MTLRegionMake2D(4, 4, 8, 8) mipmapLevel:0];
CHECK(std::memcmp(out.data(), copy + kPitch + 8, 16) == 0 &&
std::memcmp(out.data() + 16, copy + 2 * kPitch + 8, 16) == 0);
CHECK(std::all_of(out.begin() + 32, out.end(), [](uint8_t b) { return b == 0xee; }));
// The BC7 texture's first block: 16 bytes of row 0, in the copy and the surface.
uint8_t block[16];
for (int i = 0; i < 16; i++)
block[i] = static_cast<uint8_t>(0xc0 + i);
[bc7 replaceRegion:MTLRegionMake2D(0, 0, 4, 4) mipmapLevel:0 withBytes:block bytesPerRow:16];
CHECK(std::memcmp(copy, block, 16) == 0 && copy[16] == 16 && copy[kPitch] == 7);
IOSurfaceLock(flat, kIOSurfaceLockReadOnly, nullptr);
CHECK(std::memcmp(IOSurfaceGetBaseAddress(flat), block, 16) == 0);
IOSurfaceUnlock(flat, kIOSurfaceLockReadOnly, nullptr);
drainRing(fx.ring);
}
drainRing(fx.ring);
CFRelease(flat);
// A 64 x 48 'x420' surface: plane 0 128 bytes a row, plane 1 32 x 24 of 128 bytes a row, in
// the copy at 0 and 12288. A BC7 texture over plane 0 of 8 blocks of 16 bytes a row, and a
// BC1 one over plane 1 of 8 blocks of 8.
src.offset = RGFX_ALLOCATIONS_OFFSET + 0x80000;
NSDictionary *props = @{
(id)kIOSurfaceWidth : @64,
(id)kIOSurfaceHeight : @48,
(id)kIOSurfacePixelFormat : @(0x78343230u),
(id)kIOSurfaceAllocSize : @(128 * 48 + 128 * 24),
(id)kIOSurfacePlaneInfo : @[
@{
(id)kIOSurfacePlaneWidth : @64,
(id)kIOSurfacePlaneHeight : @48,
(id)kIOSurfacePlaneBytesPerElement : @2,
(id)kIOSurfacePlaneBytesPerRow : @128,
(id)kIOSurfacePlaneOffset : @0,
(id)kIOSurfacePlaneSize : @(128 * 48),
},
@{
(id)kIOSurfacePlaneWidth : @32,
(id)kIOSurfacePlaneHeight : @24,
(id)kIOSurfacePlaneBytesPerElement : @4,
(id)kIOSurfacePlaneBytesPerRow : @128,
(id)kIOSurfacePlaneOffset : @(128 * 48),
(id)kIOSurfacePlaneSize : @(128 * 24),
},
],
};
IOSurfaceRef planar = IOSurfaceCreate((__bridge CFDictionaryRef)props);
CHECK(planar != nullptr && IOSurfaceGetPlaneCount(planar) == 2);
const uint64_t kChroma = 12288;
@autoreleasepool {
RgfxIOSurfaceTexture *bc7 = over(planar, MTLPixelFormatBC7_RGBAUnorm, 32, 48, 0);
CHECK(bc7 != nil);
(void)expectMessage<RgfxBufferOverHostMemory>(fx.ring, RGFX_CMD_BUFFER_OVER_HOST_MEMORY);
CHECK(posted(MTLPixelFormatBC7_RGBAUnorm, 32, 48, 0, 0));
RgfxIOSurfaceTexture *bc1 = over(planar, MTLPixelFormatBC1_RGBA, 32, 24, 1);
CHECK(bc1 != nil && posted(MTLPixelFormatBC1_RGBA, 32, 24, kChroma, 1));
CHECK(bc1.iosurfacePlane == 1 && bc7.iosurfacePlane == 0);
drainRing(fx.ring);
}
drainRing(fx.ring);
CFRelease(planar);
}
// A surface copy's writers (RgfxIOSurface.h): a block made for this process starts with none
// whatever it held, and its texture names them to the host; each commit of a buffer that draws
// the copy names its queue and seqno there, in the queue's own slot, raised; another queue takes
@@ -14611,19 +14724,6 @@ void test_iosurface_nil_is_logged()
CHECK(rgfxIOSurfaceNilLog(s, "arguments", &line) == 1 &&
has(line, "no descriptor"));
CHECK(rgfxIOSurfaceNilLog(s, "buffer", &line) == 0 && line.empty());
// A block-compressed format, which Metal makes but the host makes no texture of over a
// surface's rows (oracle_surftex, Q20), answers nil before the surface's pages are looked at.
MTLTextureDescriptor *bc1 =
[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBC1_RGBA
width:64
height:64
mipmapped:NO];
@autoreleasepool {
CHECK(texture(bc1, 0) == nil);
}
CHECK(rgfxIOSurfaceNilLog(s, "descriptor", &line) == 1 &&
has(line, "a block-compressed format over a surface, which the host makes no texture "
"of"));
CHECK(region.used() == 0 && ringEmpty(fx.ring));
CFRelease(s);
rgfxIOSurfaceKeepFlush(); // nothing kept outlives the test's region
@@ -14944,6 +15044,7 @@ int main(int argc, char **argv)
test_iosurface_shared_block();
test_iosurface_planes();
test_iosurface_cpu_access_in_the_texture_s_format();
test_iosurface_block_compressed_textures_are_made();
test_iosurface_writers();
test_iosurface_order();
test_iosurface_slots_free_at_completion();