7 Commits

  • 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): 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(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): Apple semantics for waits, imports, heaps and the compiler
    - Load MTLCompiler from its version directory when the framework has no
      top-level link (iPadOS 27 ships Versions/32023 and 32024 only). Every
      stitched library and source compile was refused: Core Image's
      create_metal_dag returned null and RenderBox's custom_blend pipelines
      failed.
    - waitUntilCompleted waits until completion, as Metal does; the 4 s
      bound is gone. A GPU hang now ends through the kernel's restart.
    - IOSurface imports are held while their surface lives and let go at
      completion, at the next import or on memory pressure, with no count
      or age limit.
    - A locally refused command buffer frees its runtime command buffer at
      finish, so its resources go as Metal's do.
    - The status reads Scheduled inside scheduled handlers run by commit.
    - Runtime patch 0022: generateMipmaps runs the blit chain for formats
      outside the measured spread instead of refusing.
    - Runtime patch 0023: a heap texture's size is a multiple of its
      alignment, so a heap sized as the sum of sizes holds its textures
      (OutOfMemory(Heap) in SpringBoard and Core Image on Turnip).
    - Refusals name pixel format, size and levels.
    - Host tests: weak priming counters, updated import, lifetime and
      scanout tests.
  • 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.
  • Import NativeMetal
    Co-authored-by: rpone <rponeawa@qq.com>