67 Commits

  • feat(metal): pass tile pipelines and tile dispatches to the runtime
    A render pipeline state made from a tile descriptor, the tile stage's
    buffers, textures and samplers, threadgroup memory and
    dispatchThreadsPerTile reach the runtime, which runs a tile function as a
    compute dispatch between two instances of the pass. A pass carries its tile
    size and tile memory lengths.
    
    The device does not offer tile shaders: QuartzCore reads the private
    supportsTileShaders, which stays no. metal-tile=1 answers yes for a
    measurement. With it QuartzCore made two to three tile pipelines and one to
    four dispatches in a ten minute run, so the path does not matter for its
    frame times; its pictures were not compared.
  • fix(turnip): leave concurrent binning at Mesa's default
    The plugin turned Turnip's concurrent binning on without a measurement of
    what it gains. It is back at Mesa's default, off; tu-cb=1 turns it on.
    Runs with it on and off show the same frame pacing within the spread
    between runs.
  • perf(runtime): make driver pipelines on the asking thread
    The runtime handed every vkCreate*Pipelines call of a thread below the
    user-initiated class to a thread it started for the call, and waited for
    it. That serves a driver whose compiler service takes the asking thread's
    class. Turnip compiles inside the call, so the render server's main thread,
    which has no class, only waited for a thread of lower priority.
    
    With METAL2VULKAN_COMPILE_ON_ASKING_THREAD=1, which the plugin sets, a
    thread with at least 2 MiB of stack makes the call itself. Smaller stacks
    keep the handover for the room the driver's compiler needs.
    
    Measured on sheng (macOS 27.3), render server main thread, cached
    pipelines: 42 creations took 755 ms of wall time for 88 ms of CPU, the
    longest 168 ms; with the change 17 creations took 34 ms, the longest 8 ms.
  • feat(diag): send Turnip's log to the system log and a file
    With tu-debug= set, Mesa's log goes to syslog and to a file of the
    process beside the frame trace: a daemon's stderr reaches nothing. A
    stale comment on concurrent binning is removed.
  • perf(turnip): let Turnip choose each pass's rendering, with concurrent binning
    TU_DEBUG no longer forces tile memory for every pass: Turnip renders a
    pass in tile memory or in system memory by its own rules. Concurrent
    binning is asked for through tu_allow_concurrent_binning. tu-debug=
    overrides both (gmem,nocb is the earlier behaviour).
  • perf(bridge): lower on the lowering thread, submit at commit, warm pipelines
    The three work placements are on by default. metal-flow=<bits> selects
    them: 1 the lowering thread, 2 submission at commit, 4 the warm pass
    over the pipeline records of earlier processes; 0 turns all off.
  • perf(plugin): render every pass in tile memory
    TU_DEBUG defaults to gmem,nocb: attachments are loaded and stored once
    per tile and memoryless attachments take no memory. GMEM loads and
    stores apply the image's own swap on the memory side, so colours match
    system memory rendering. A pass Turnip cannot tile still renders in
    system memory. tu-debug= overrides the default.
    
    Run on hardware: three runs of 410 to 550 s without a hang or a crash,
    every ring switch finished.
  • fix(plugin): remove the client memory log
    With log-gpu=1 every client started a thread that called the telemetry
    table; nmb_device_memory_kinds jumped through a pointer that fails
    authentication in arm64e clients, so ControlCenter, NotificationCenter
    and the Dock extra crashed every 20 s (21 to 22 times in a 550 s run).
    
    Run on hardware: the crash reports name the thread and the call.
  • feat(plugin): log client GPU memory with log-gpu=1, ask the sandbox before the shared cache probe
    With log-gpu=1 a client process logs where its GPU memory is every 20 s
    (the runtime's pooled, empty, spare and dedicated blocks, what was
    declared discardable, how passes were rendered); a row that did not
    change is not logged again. A sandboxed process asks the sandbox whether
    it may write the shared shader cache directory before it probes it: the
    refused probe file made a violation report at every launch.
    
    Run on hardware: the rows appear for client processes.
  • fix(present): leave the framebuffer format to a signed-text fixup on macOS
    The plugin changed QuartzCore's framebuffer format branch by writing
    into WindowServer's mapped page. On macOS that page then failed code
    signing validation and WindowServer was killed (CODE SIGNING: rejecting
    invalid page). The board applies the same change as a fixup of signed
    text when the page passes validation; the plugin no longer writes it.
  • feat(cache): system shader cache the render server fills and every process reads
    On macOS the render server (WindowServer, user _windowserver) and the apps
    are different users, so what it primed served it alone, and a sandboxed
    process that cannot write the user's shared directory compiled every
    pipeline at first use.
    
    The render server now opens its device in /private/var/tmp/NativeAdrenoMetal.cache
    when the kernel made that directory for it, writes its entries readable by
    everyone, and primes every library of the list except other GPUs' driver
    bundles. Every other process names /Library/GPUBundles/NativeAdrenoMetal.cache
    (the same directory, shown read-only by the kernel) as a read-only layer
    its device asks for what its own cache lacks: translated functions,
    pipelines, the driver's pipeline data and compiler replies.
    
    Bridge patch 0036 adds the layer to the runtime. A level (the directory,
    each directory on the way to an entry, the entry) is opened without
    following a link and read only when owned by root or _windowserver and not
    writable by group or others; entries are then checked as the process's own
    are. Nothing is written, touched or removed there.
    
    gpu-prime-system=0 restores the per-user behaviour. Telemetry: PRb and SY
    rows, system counts in the work line, the layer's state in the cache line.
    iPadOS is unchanged.
    
    Host: bridge/system_cache_host.mm over MoltenVK (second process hits the
    published entries; wrong owner, writable levels, links and corrupt entries
    are refused or ignored). Syntax-checked for macOS 27, iPadOS 27 and
    iPadOS 17. Has not run on hardware; needs the kernel step that makes and
    binds the directory.
  • feat(memory): sample two-plane YCbCr surface textures over their planes
    A YCBCR8_420_2P or YCBCR10_420_2P texture over plane 0 of an IOSurface, of
    ShaderRead usage alone, is made over the surface's planes with no converted
    image (bridge patch 0035). QuartzCore on macOS 27.3 asks for exactly this
    texture for 420v, 420f, x420 and xf20 video layers on a device that answers
    the Apple1 and Apple2 families, and the runtime made an RGBA16F image of the
    picture's size plus a staging copy of the planes for each one.
    
    A slot binding that cannot read the planes in place makes the image at that
    binding; any other call makes the texture with its image and uses that from
    then on. gpu-ycbcr-planes=0 restores the converted image for every texture.
    The telemetry's YC row counts both paths and the converted images alive.
    
    Host: bridge/ycbcr_planes_host.mm passes over MoltenVK in both modes (same
    pixels as the converted path, image made only where needed, surface kept
    while in flight). Not run on hardware.
  • perf(present): 8-bit framebuffer and no surface flush on macOS 27
    ca-fb-8bit=1 lets WindowServer's QuartzCore take the branch of a display
    without extended colours, so it presents BGRA, which the display pipes
    scan out directly, instead of half float converted by the GPU for each
    frame. The surface flush is compiled out for macOS 27 as for iPadOS 27:
    its sys_dcache_flush is a barrier alone.
    
    Not run on hardware.
  • feat(memory): discard idle volatile buffers, textures and heaps
    setPurgeableState: kept the state and never discarded, so what callers
    declare Volatile cost its whole size on a GPU whose memory is wired.
    RenderBox keeps up to 64 MiB of recycled buffers, 128 MiB of textures
    and 128 MiB of heaps Volatile for ten seconds in every process that
    uses it; QuartzCore, SkyLight and Core Image call the selector too.
    
    The bridge now follows Metal's contract (bridge/NativeMetalPurge.rs).
    The runtime fixes a resource's VkBuffer or VkImage and placement for its
    life, so a discard releases the runtime resource and a later request
    makes it again from the same description. The runtime's own lifetime
    rules keep what a batch in flight uses, and the frontend handle stays
    the same object, so no call reaches freed memory.
    
    Discarded: a texture or buffer the device made of its own memory, and a
    heap, whose state is Volatile or Empty, while
    - no command buffer that names it is recording or in flight (the
      frontend holds it from the call that names it to the completion),
    - no view of it, texture over it or resource placed in it lives,
    - neither its CPU pointer nor its GPU address was handed out,
    - no call is inside it.
    Never: textures over buffers or surfaces, no-copy buffers, views,
    resources placed in heaps, drawables, resources under 16 KiB or
    memoryless (the runtime keeps no state for them).
    
    Triggers: the memory pressure event, and age (idle for the runtime's
    spare block age), because pressure events are rare here.
    
    After a discard NonVolatile places the resource again and answers
    Empty; a query answers Empty and places nothing; any other use places
    it again zeroed with its state still Empty. A failed placement fails as
    an allocation does and setPurgeableState: still answers Empty.
    
    gpu-purge=0 (NATIVE_METAL_PURGE=0) keeps today's behaviour: the state is
    kept and nothing is discarded.
    
    Telemetry: PG's volatile and empty MiB are exact per object; PGb and
    PGc rows give MiB discarded now, by pressure and by age, placed again
    by request and by another use, Empty answers, MiB never discardable, and
    what the last pass left. With diag=1 other processes write a purge line
    for each pass that discarded something, and the memory line carries the
    same counts.
    
    Needs runtime patch 0034 (Device::spare_age).
    
    Host checks over MoltenVK: bridge/purge_host.mm in its modes on, off
    (NATIVE_METAL_PURGE=0), timer and stress, and the existing frontend
    host tests, pass. Both .mm files pass clang -fsyntax-only -Werror for
    macOS 27, iPadOS 27 and iPadOS 17. Not run on hardware; the encoding
    thread's path and Turnip were not exercised.
  • fix(pacing): find the triple buffer option by its name
    ca-triple-buffer=1 set QuartzCore debug option 198. In 26C5035r that
    index is another option and CA_FORCE_TRIPLE_BUFFERED_LATENCY is 201, so
    triple buffering was never on. The index is now looked up through
    CAGetDebugOptionEnvVariableName and the log line names it.
    
    Not run on hardware.
  • fix(diagnostics): stream the telemetry table again, memory rows first
    The thread that sent the render server's telemetry table to the kernel was
    removed with the status lines, so diag=1 produced no [metal table] in any
    macOS run. It is back for diag=1: every six seconds to the kernel's board
    log (48 rows, a row sent only when it changed, at utility priority) and
    every thirty seconds, whole, to the system log.
    
    The table now answers which blocks make up a process's large buffer objects:
    
    - MK, MKb, MKc: the runtime's blocks in seven size classes (at most 16, 32,
      40, 48, 56, 64 MiB and larger) for pooled blocks in use, pooled blocks kept
      empty, spare image blocks, dedicated blocks of an image and of a buffer,
      with the MiB of the spares and the dedicated blocks (bridge patch 0031).
    - ML: lazily allocated blocks and what the driver backed of them
      (vkGetDeviceMemoryCommitment), the attachment memory Turnip makes itself.
    - PE: memory pressure events handled, blocks freed at them, surface imports
      held and their MiB, Turnip's BO cache flushes.
    - VP: driver pipelines made (at a state's creation, at a draw or dispatch)
      and the driver's time in them, from counters the runtime always keeps; the
      creation log the V row summed is off in the plugin.
    - TH: the threads that used the most CPU since the last table.
    
    Memory rows come first, then the compile rows, the thread rows and the last
    problems, so they are inside the 48 rows the kernel keeps. A process other
    than the render server writes one line of its compile counts to the system
    log every thirty seconds while they change.
    
    The build must list bridge patch 0031 after 0030 in
    scripts/build-adreno-metal-bridge.py: the bridge calls Device::memory_kinds.
    The telemetry host test checks the rows and their length over MoltenVK; the
    frontend's other host tests pass. Not run on hardware.
  • fix(prime): prime on macOS only while idle and only loaded bundles' libraries
    Shader priming ran through the user's session on macOS. In the WindowServer
    samples of four runs its thread (unnamed, so it read as part of the idle
    picture) was compiling or writing its progress in 88 to 94 % of the samples,
    in two of them beside the encoding thread's draws. How much of the CPU share
    the process listings showed for WindowServer this is, the telemetry's TH rows
    will say.
    
    Two causes, both specific to macOS:
    
    - The quiet test took up to 8 command buffers in 250 ms as an idle desktop,
      which is iPadOS's idle redraw. macOS's WindowServer composes only on
      change: the whole system submitted nothing to the GPU in a quarter of a
      run's two-second windows, and a desktop in use ran at 18 to 20 submissions
      a second, under that bar. On macOS any command buffer in the window now
      counts as drawing.
    - The list holds every Metal library of the image, 400 of them, 165 in other
      GPUs' driver bundles. The render server runs as a system user and the apps
      as the console user, each with a pipeline cache of its own that the other
      cannot open, so what WindowServer primes serves WindowServer alone. On
      macOS a library is primed only when its bundle has an image loaded in the
      process: 57 of the 400 for WindowServer.
    
    The thread is named native-metal-prime. iPadOS keeps its rule and its list.
    The bundle rule was checked on the host against loaded and unloaded bundles.
    Not run on hardware.
  • fix: keep the shader cache inside Metal's cache directory
    In the user cache directory the pipeline cache now sits in com.apple.metal/m2v. A sandboxed process may write there whenever its profile lets Metal cache shaders. macOS's iconservicesagent could not write <cache>/m2v (thousands of denials), compiled every pipeline again at each launch and was killed at its memory limit within seconds, over and over. Not run on hardware yet.
  • feat: the macOS plugin entry and render server path
    IOGPU loads the bundle's Metal device class only if it subclasses IOGPUMetalDevice, so the macOS build gets that entry. WindowServer takes the render server path, the prime list comes from the bundle, an unreadable library is reported with its path components, and the ca-triple-buffer and ca-free-wakeup boot arguments set the compositor's latency options.
  • fix: find bundle files from the macOS executable path
    A macOS bundle keeps its executable in Contents/MacOS. The plugin now locates files beside NativeAdrenoMetal.bundle from either layout.
  • refactor: drop the sheng- prefix from boot arguments
    Every boot argument loses its sheng- prefix (sheng-touch=4 is now
    touch=4). No XNU or Apple kext boot argument of iPadOS 27 or macOS 27.3
    uses any of the new names.
  • feat(bridge): keep the lowering, commit and warm paths behind a switch
    Use the runtime's default lowering and commit with no warm pass. The boot
    argument sheng-metal-flow=1 enables the lowering thread, commit submission
    and pipeline warm pass for testing. Measured on sheng they gave no frame
    rate gain, and one boot with them stopped presenting at the logo.
  • feat(diagnostics): GMEM colour order self-test
    sheng-swaptest=1 runs one Vulkan test in the render server: a 3048x64
    linear BGRA8 image in device and in imported host memory, rendered in
    system memory and in GMEM, with a draw, a load and store, and input
    attachment reads of loaded and of drawn colour. The image is filled and
    read back by GPU copies through host-coherent buffers; the stored bytes
    of each region go to the GPU trace as problem records.
  • fix(diagnostics): drop GPU trace forwarding and unused status lines
    The render server's records stay in its frame trace without a system call
    per record; only render, encode and completion thread identities reach the
    kernel ring. Pixel capture, dispatch introspection, pass and pipeline notes,
    the slow-pass reporter and the status and telemetry threads are removed.
  • feat(metal): share shader caches across same-user clients
    Use a writable per-user cache with container fallback, keep Turnip disk caching enabled, and isolate concurrent cache-pack imports. Verify cache round trips, concurrent imports, and the iPadOS 27 plugin compile.
  • feat(plugin): set the GPU log switch from log-gpu=1
    The plugin maps log-gpu=1 on the boot line to SHENG_LOG_GPU, which the
    bridge reads for its NSLog output and the pipeline journal. The pass
    reporter ticks every 5 s.
    
    Patch 0023 gives the runtime its quality of service class on iOS; patch
    0024 marks the submit steps behind the diagnostics switch.
  • feat(gpu): per-task priority forwarding and pass reporter
    Send NativeAdrenoAbiPriority (method 17) from tu_knl_darwin.cc based on
    METAL2VULKAN_QUEUE_PRIORITY. Add nmb_slowest_pass() telemetry in
    NativeBridgeMetalFrontend.mm: tracks slowest render pass GPU time per
    boot. startPassReporter() streams the slowest pass to kernel log every
    10 s when sheng-diag=1.
  • feat(metal): pass and submit rings in the frame trace, gap sampler, full shader cache counts
    frames.trace flushes every 250 ms with a time base record and carries Turnip's render pass and kernel submission rings; the CPU sampler also covers the gaps between command buffers; per-pipeline shader cache deltas and nmb_device_shader_cache_detail. The plugin keeps system memory for the 8-bit BGRA framebuffer. Drops the render target trace record.
  • fix(metal): keep the import's own surface in the held-import table; read the virtualized gate
    A surface import hit recorded the caller's IOSurface object, which the
    import never retained. A completion thread then read the retain count of
    a freed object in pruneHeldImports, and backboardd died on the isa
    authentication (i35abaf83, twice, at 879 s and 905 s). The table now
    records the surface the import itself retains.
    
    i35abaf83 read 0x6E23 and the framebuffer stayed RGhA.
    framebuffer_pixel_format also takes RGhA from the device-is-virtualized
    byte; the render server now reads it back (0x6E30 bit) and notes the
    key that sets it.
  • fix(metal): hold CA_ALLOW_EDR_MIRRORING off; read the gates in the render server only
    id02fe1633 read 0x6E19 in one process: CA_ALLOW_EDR_MIRRORING set, one
    of framebuffer_pixel_format's RGhA gates. It is held off as the other
    options are, in the environment for init_debug and in its stored byte.
    The readback runs in the render server only, whose framebuffer it is,
    and marks 0x6E2x.
  • fix(metal): compile pipelines on the device only; framebuffer threshold option
    Shader caches are made only by the render server's own priming, on the
    device, when the desktop is idle, into the persistent Data cache
    directory, where later boots hit them. The pipelines compiled ahead on
    the host and read from the userdata import region are gone: the import
    reader, its telemetry rows, the runtime's read-only fallback source
    (bridge patch 0017; later patches renumbered) and the host producer.
    
    The render server's framebuffer threshold is also set as QuartzCore's
    own option CA_FP16_MIN_HEADROOM in the environment, which init_debug
    reads (getenv, atof) whenever it runs: i84edf75a1 wrote only the stored
    value and the framebuffer stayed RGhA. A mark after 30 s reports what
    framebuffer_pixel_format reads.
  • perf(metal): keep the render server's framebuffer 8-bit
    The headroom override alone left the framebuffer RGhA: 24A446's
    framebuffer_pixel_format takes it when the headroom is above its FP16
    threshold, 0 unless set, and the panel's own above 8. The threshold is
    held at the largest float, so the render server draws BGRA as on 17 and
    no frame is converted from half floats before scanout.
  • feat(metal): allocation-site telemetry, priming off with sheng-gpu-prime=0
    Bridge patch 0020 keeps the call stacks that allocate device memory of
    4 MiB or more; the MA rows print the six with the most bytes. The
    plugin starts no priming thread and installs no import when the kernel
    reports priming off.
  • fix(metal): read ahead-compiled pipelines on demand instead of copying them into Data
    Copying the whole set into the RAM Data volume took 850 MB and starved the
    GPU of memory (endEncoding OutOfMemory). The runtime now asks a read-only
    fallback (patch 0017, nmb_set_shader_cache_fallback) for an entry its
    directory lacks; the plugin answers it from the userdata import region
    through an index read once (CacheImport.h). Files a half-finished copy
    left are removed. Draws wait for their pipeline as Metal's do
    (PipelineWaitPolicy::Exact) instead of being dropped after 4.5 s.
  • feat(metal): rebase the translator on upstream 0dfdbcd and import ahead-compiled pipelines
    Rebase the metal2vulkan patch series onto upstream 0dfdbcd5. Upstream now
    does 21 of the 37 old patches itself (render pass caching, per-draw
    allocation cuts, calibration off the commit path, mipmap filtering, heap
    texture sizing and others); 16 remain:
    
    - written textures over surfaces and padded buffers stay images over
      their bytes (HostPageTexturePolicy::InPlaceWrites) where tile stores
      fit within their rows, instead of shadows copied every pass
    - Device::refuse_rebinds: no call logs or per-command holds of no-copy
      buffers, which the frontend never rebinds
    - priming cache, null-pipeline draws, two-plane shadows, extended-range
      sRGB blends, growing memory blocks and the Core Image library fixes
      carried over to upstream's new design
    
    The plugin imports the pipeline caches compiled ahead on the host from
    the userdata import region (method 0x7101) before priming, once per
    pack. bridge/prime_host.mm runs the plugin's priming on the host.
  • feat(metal): show the head and tail of a client failure
    The board row holds 63 characters: drop the common prefix and keep what
    was refused and why. Shader priming does not report problems.
  • feat(metal): block-compressed layouts, RenderBox answers and bounded completion waits
    Frontend and plugin hooks the OS frameworks need: DroidVM's linear layout for block-compressed formats and textures over memory, supportsFamily for RenderBox, IOSurface use counts in the render server, XR10 for poster processes, refusal reasons in the log, a cache-optional device for sandboxed clients and the TU_DEBUG file. Heap size answers, the stitch request's trailing slots, two-plane b3a8 surfaces computed from the allocation, and completion waits bounded at 4 s. GPU scanout converts half-float frames. Translator patches 0017 to 0021.
  • The render server asks for a high queue priority; GPU priority requests logged
    On an iPad CoreAnimation raises its render server's queue (setGPUPriority: when it makes its MetalContext; QuartzCore's table
    gives level 1 priority 2, level 2 priority 1, level 3 priority 4). A Vulkan queue's priority is fixed when the device opens,
    before that call, so the plugin asks the runtime for a high one in the render server (METAL2VULKAN_QUEUE_PRIORITY=high,
    UniXNU patches/metal2vulkan/0012); the board's Turnip raises the process's ring where the host offers priorities. The
    frontend still reports a per-queue priority as not applied, and logs the first one a process asks for.
    
    Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
    Claude-Session: https://claude.ai/code/session_012Camiwa7VsPwkhfyscKQ6S
  • fix(bridge): return a refusing render encoder instead of nil
    When a render pass could not be encoded, renderCommandEncoderWith
    Descriptor: returned nil. RenderBox caches the encoder's method
    implementations, so the nil gave it a null function pointer and
    WidgetRenderer crashed at PC 0 eight times, hit the open cap and left
    the Apple TV widget drawn wrong (i568). The encoder is now returned and
    its command buffer marked refused.
    
    The process's first problem is reported through a hook, from the thread
    that hit it, instead of a once-a-second reader that never ran before
    the crash. On sheng (i570) WidgetRenderer reported the refusal and did
    not crash.