The bundle answered neither minimumTextureBufferAlignmentForPixelFormat: nor
either sparseTileSizeWithTextureType: form, and IOGPUMetalDevice has none of
them. A call fell through to the zero catch-all, whose signature returns a
scalar where both tile queries return an MTLSize.
The blob now carries, for each pixel format of the SDK, its texture-buffer
alignment and its 2D sparse tile at 1, 2 and 4 samples on 16, 64 and 256 KiB
pages. The tile is one answer for every texture type, as Metal gives it on an
M3 Max. Metal asks the alignment only of the 56 formats it answers; on every
other one it fails an assertion, measured on the M3 Max. The Vulkan platform
answers with the runtime's linear alignment, which Metal answers alike for
every format, and with the runtime's tiles.
The guest's MTLDevice answered supportsFamily: of Apple5 to Apple10 and -1009
alone, no to Common1-3, Mac1/Mac2, Metal3 and every feature set, a 32-byte
name that cut "NVIDIA GeForce RTX 5080 Laptop GPU" short, and placeholders for
its identity (1 GiB working set, unified memory, Tier2 argument buffers). Apps
that gate on Mac2, Metal3 or a macOS feature set refused to run.
- GpuDevice::identity_caps, which only the graphics lane asks, holds
supportsFamily: of every family of the SDK, supportsFeatureSet: of every
feature set, and the identity and capability getters. The paravirt lane's
DeviceCaps are untouched. Metal answers every one with its own getter. The
Vulkan platform answers each from the runtime where it can: its properties,
supportsFamily:, and the audit of a feature row (BC formats, query LOD,
barycentrics, dynamic libraries, ray tracing, function pointers). Apple5 to
Apple10 keep the platform's DeviceCaps answers. Each claimed family claims
what Metal documents it contains. Feature sets follow their families, as
Metal on an M3 Max answers them: no to ReadWriteTextureTier2 and to tvOS.
The working set is the sum of the device-local Vulkan heaps. The runtime
has no answer of the rest, which is None.
- RgfxDeviceInfo v2 carries all of it: families and feature sets as (value,
answer) lists that the bundle looks up by value, the integer and BOOL
identity answers, the architecture name, and a name of 256 bytes. A gap
reads 0, never yes.
- device_info.rs answers with the lane's own device where it can do less
than the host GPU, in one table with a row per missing piece: Metal4,
ray tracing, motion blur, function pointers, dynamic libraries, placement
sparse, counter sampling and rate maps answer no. hasUnifiedMemory stays
yes for now, because Managed buffers are guest memory alone in the bundle.
- The bundle answers every one of these getters from the blob, with no
defaults of its own.
-commit posted with tryPost and ignored its answer, so a command buffer
committed while its queue's 32 KiB submission ring was full, or one larger
than the ring, was never posted and never completed: 400 small fills in a row
hung every time on msi. Metal never fails or drops a buffer for its size, and
throttles with maxCommandBufferCount.
- A buffer larger than half its ring goes out of line: the bundle writes its
whole COMMAND_BUFFER message into a region block of the process's own and
posts a new COMMAND_BUFFER_IN_REGION (0x301: offset, length). The queue's
service thread copies the message out of the block once, with the
reference's seqno, and refuses a span outside the processes' blocks or one
holding no COMMAND_BUFFER of that length. The block goes back as the buffer
completes. With no block to be had, the buffer fails with
MTLCommandBufferErrorOutOfMemory, off the committing thread.
- A queue keeps maxCommandBufferCount slots (64 for 0, as Metal's default):
-commandBuffer blocks while every one is held by a buffer alive and not
completed, and a buffer gives its slot back before its handlers run, or at
dealloc where it never completes.
- A commit that finds the ring full rings the doorbell and sleeps 20 us
doubling to 1 ms between looks; on the delivering thread, or where no
completion thread drives the ring, it takes the queue's completions
meanwhile, as the host may be waiting for room there.
It also closes a race in the completion work: commit expected a buffer's
completion only after posting it, and set Committed after, so the completion
thread could take a fast completion with no handler yet (lost, the buffer
hung) or have its status overwritten. The completion is now expected under the
ring's lock before producer moves, and Committed is set before the post.
Every guest Metal process's textures share the host's one device, whose
texture ids end below 16381: Metal ends the process for a texture placed at
16381 or more, and metal2vulkan imitates that. The ids were split by context
slot (1 + s * 2048), so slot 8 onward was past the limit, and a WindowServer
crash loop or a few app launches killed QEMU at the first texture create
("invalid use of new_texture: ResourceId { id: 16385, limit: 16381 }").
The kext now keeps one pool of 255 chunks of 64 ids, all below 16321, and
gives a process a chunk when it asks (user-client selector 0x106). It posts
the grant on ring 0 (RESOURCE_ID_CHUNK: context, chunk) before it returns, so
the host has it before any create naming its ids. A process keeps its chunks
until its close, and they are freed after its CONTEXT_CLOSE is answered, with
its blocks. OPEN_CONTEXT answers no resource-id base any more. A chunk is 64
ids so that the bundle keeps each chunk's free ids in one 64-bit mask.
The bundle takes the lowest free id of the first chunk it took that has one,
asks for a chunk only when all of its are in use, and gives an id back as its
texture goes. With every chunk held, newTexture answers nil, as Metal answers
a texture it cannot make.
The host refuses a texture create, from a device, a buffer or an IOSurface,
whose id lies in no chunk of its own context, 0 and the limit's included,
before any platform call, as a refused create is refused: unanswered, and a
command buffer naming the texture fails. So no guest id reaches the
platform's own limit, where the Vulkan platform ends the process.
Only textures are affected: sampler and pipeline states take no guest-picked
ids (the host numbers them), and the bundle makes no indirect command buffers.
tools/tex_probe copies a pattern through a Private texture and back, and holds
N textures at once, checking their ids are distinct and below the limit.
Every process that loads QuartzCore's shaders sent the whole library over
the region, and the host built it again. Now the bundle takes the AIR slice's
content key (SHA-256 of the .metallib without its bitcode section, whose
functions the HASH tags name) and asks the host first with LIBRARY_LOOKUP.
On a hit the new id is bound to the library the device made for any process,
and no bytes cross. On a miss the slice goes as before, now with its key.
The host keeps a library under a key only where it computed that key from
the bytes itself, checked every HASH tag against its bitcode, and found the
functions cover the bitcode section. Every AIR metallib of macOS 27's system
covers it. A lookup can then only bind content with that key, on Metal as on
the Vulkan platform. A wrong key makes the library uncached. Libraries no id
names stay in a 64 MiB least-recently-released list, so a restarted
WindowServer still hits.
Library creates are now answered with COMPLETE, and the kext waits for the
answer (for a region-streamed library, its last chunk), so a refusal is nil
and an NSError at newLibrary*, as in Metal. They are made on the compile pool,
off the control thread. A pipeline create that names a library still being
made waits for it on its compile thread, and a destroy of such a library
keeps ring order.
Each process's creates and destroys now reach the host on the control ring,
which the accelerator kext posts for it under the framebuffer's ring lock, in
place of a device queue per process and the BAR 0 mailbox. Ring 0 keeps one
producer, the kexts, and one consumer, the host's control thread, so the
lifecycle of every process is in one order with the display commands.
The bundle hands each message to the kext by a user-client call (selector
0x105, struct in). The kext checks the object is in the caller's context,
stamps the seqno and posts it; for a pipeline create it waits for the
reflection, which the work loop routes by seqno, and hands it back as struct
out. QUEUE_CREATE (selector 0x103) is posted and answered with COMPLETE once
the host has the queue's rings. At a close the kext posts each queue's
OBJECT_DESTROY, then CONTEXT_CLOSE, which the host answers once the context's
objects are dropped, then frees the blocks.
The host carries out lifecycle messages on the control thread with no ring
held, and a command buffer waits until every ring-0 message published before
its queue's thread took it is carried out. The Host trait's queue_message
becomes stage3_message, and its poster (CompletionPoster) also targets the
control completion ring.
The ObjC++ layer Apple's Metal loads as the guest's MTLDevice and that
serialises Metal calls into the reims-graphics wire, built on the existing
serialiser core. This slice lays the foundation the resource, pipeline,
command-buffer and sync slices stack on.
What it adds:
- RgfxMetalPrivate.h: minimal, author-written forward-declarations of the
Apple-private base (IOGPUMetalDevice/_MTLDevice) and the private protocols
(MTLDeviceSPI, MTLCommandQueueSPI, MTLCommandBufferSPI), each with only the
selectors the shell overrides, tagged measured. No Apple header or SPI dump
is copied into the tree.
- RgfxMetalDevice.{h,mm}: RgfxMetalDevice : IOGPUMetalDevice
<MTLDevice, MTLDeviceSPI>. init + wire-handshake stub, device identity, the
family/sample-count/feature queries and the limit getters, answered from
RgfxCaps. The resource-creation methods are left to per-slice categories;
the header documents the +Resources/+Pipelines/+CommandBuffer/+Sync split so
the slices never collide in one .mm.
- RgfxResource.{h,mm} and RgfxBuffer.{h,mm}: the wrapper convention — an
NSObject conforming to a Metal protocol, carrying the guest-picked objId, a
weak device back-pointer and a borrowed serialiser it routes mutating calls
through. RgfxBuffer is the worked example the resource slice replicates.
Being NSObject-based (not the private device base), the wrappers instantiate
and are unit-tested on the Mac.
Checks:
- make bundle compiles all shell .mm for x86_64 (the guest's arch) and
bundle-links the loadable bundle, device class included, with
-undefined dynamic_lookup (the private base/SPI resolve at load in the
guest). It only needs to compile off the guest, not run.
- make test keeps the serialiser check (387, x86_64 under Rosetta) and adds a
native-arch Metal-shell routing check (40): a wrapper's create routes an
RGFX_CMD_BUFFER_CREATE through the serialiser, read back off the ring and
asserted byte for byte — the assertion pattern the later slices copy. The
device shell is excluded from the test binary (it needs the private base at
runtime). The serialiser harness is factored into tests/rgfx_test_ring.hpp,
shared by both checks.
The generated wire header carried only the render op structs: the compute
(ops/compute.rs) and blit (ops/blit.rs) op bodies were built by a local
op_body! macro_rules macro, which cbindgen does not expand, so it never saw
them. Op struct names also collide across the groups in the flat C
namespace (render, compute and blit each define e.g. UpdateFence and
WaitForFence with different fields).
De-macro both files to explicit #[repr(C)] structs matching the render
style, and give each group's op structs a unique prefix: compute's
RgfxOp* become RgfxComputeOp*, blit's RgfxOp* become RgfxBlitOp*. The
element records (RgfxCompute*) and the render op structs (RgfxOp*) are
unchanged. The host decoders and the cbindgen include list follow the new
names; cargo xtask header now emits all 27 compute and 26 blit op structs,
and header --check is green.
Drop the guest encoder's rgfx_compute/rgfx_blit hand-mirror namespaces,
which existed only because the header lacked these types, and point the
encoder, the wire-layout asserts and the serializer tests at the header's
types directly.
RgfxEncoder builds an RGFX_CMD_COMMAND_BUFFER message for a (queue, command
buffer) pair and appends encoder passes and their ops to the op stream, then
fills op_bytes and posts it on the queue's submission ring. One method per op
across all three encoders (render, compute, blit) plus the framing ops writes an
RgfxOpHeader and the op body, 8-byte aligned, mirroring the host decoder
(graphics::decode::{cmdbuf,render,compute,blit}) byte for byte; array-tailed ops
write the head then its element records.
The render-encoder op bodies and the render-pass records are the committed wire
header. The compute-only and blit op bodies and the compute element records are
not emitted into that header (cbindgen reaches only the render-op module), so
they are mirrored here under the rgfx_compute and rgfx_blit namespaces, their
layouts pinned by RgfxWireLayout static_asserts against ops/{compute,blit}.rs.
serializer_tests gains a host-side op-stream consumer that walks a built command
buffer: it checks the begin/end framing, a representative op across each encoder
(including an array-tailed op and a no-arg render op), the fill-texture trailing
bytes, and op_bytes/alignment.
Wire: RgfxRenderPipelineCreate / RgfxComputePipelineCreate (fixed head +
tail for entry-point names, function constants, vertex descriptor and
label; colour attachments inline), their sub-records (RgfxColorAttachmentDesc,
RgfxVertexBufferLayout, RgfxVertexAttribute, RgfxFunctionConstant), and the
RgfxPipelineReflection reply (0x402). Functions are named by library obj_id +
entry-point bytes + function-constant values; size asserts and a round-trip
test guard the layout.
Host: the RENDER/COMPUTE_PIPELINE_CREATE decode arms resolve the functions'
libraries, drive the platform's new_render_pipeline_state /
new_compute_pipeline_state_for_function creators, store the handle, and emit
a reflection reply through a settable reflection sink (mirroring the
completion sink). A non-library reference is a bad reference; a nil function
or a refused descriptor emits a refused reply. Adds the require/get_library
resolver.
Guest: createRenderPipeline / createComputePipeline pack the descriptors into
the wire tail and post them; the serialiser test posts both and reads the
create bytes and the reflection reply back.
The platform's descriptor-value creators carry only the vertex/fragment
functions, colour attachment 0's format and source-over blend, and a label;
the wire carries the whole descriptor for when that value grows, and the
platform exposes no per-argument binding reflection, so the reply carries the
pipeline state's own queries. The cbindgen export and the generated header
are left for the integration pass.
The reims-graphics guest Metal-plugin bundle turns an app's Metal calls into
the device's wire stream. This is its first, validatable slice: the parts that
need no Apple Metal SPI, so they build and are checked on the Mac in user
space.
- RgfxIdSpace: the per-context object-id, resource-id and GPU-address number
lines the guest picks and the host pins (design Decided 6 and 7).
- RgfxCaps: reads the RgfxDeviceInfo caps blob and answers supportsFamily:, the
limits and the feature queries by name, in the host's field order, with a
host gap reported as unsupported, never a spurious yes (Decided 10).
- RgfxRingWriter: the guest producer's reserve-write-publish-doorbell step,
the mirror of the host consumer in reims-graphics/src/ring.rs.
- RgfxSerializer: builds and posts each object-lifecycle message whose wire
struct is fixed in wire.rs (reims_graphics_wire.h) -- queue, heap, buffer,
texture, sampler, depth/stencil, library, event, shared event, fence and
OBJECT_DESTROY -- on the control ring.
- RgfxWireLayout: asserts every wire struct's size at the guest's own compile,
the C-side wall beside wire.rs's.
The command-buffer encoder-op stream, the pipeline creates and
TEXTURE_FROM_IOSURFACE are later increments: their wire schema is not yet
fixed. The Metal device shell that drives this serialiser needs private SPI and
a guest experiment and is not built here.
make -C guest/bundle test builds and runs the serialiser check under Rosetta
(the x86-64 guest ABI); it reads every posted message back and asserts its type,
size and fields. 216 checks pass on x86-64 and arm64.