Files
NativeMetal/NativeBridgeMetalFrontend.h
rponeawa 5e939ccd19 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.
2026-11-11 00:00:00 +08:00

51 lines
4.0 KiB
Objective-C

// SPDX-License-Identifier: MIT
// Entry points of the native Metal frontend over the bridge (NativeBridgeMetalFrontend.mm).
#pragma once
#include <stddef.h>
#include <stdint.h>
#ifdef __OBJC__
#import <Foundation/Foundation.h>
extern "C" {
// Opens the process's one device: loader exports vkGetInstanceProcAddr (Turnip), cache is a writable shader-cache directory, registryID is the
// accelerator service's registry entry. missing (may be null) is told the class and selector of every unimplemented call.
id native_bridge_metal_open(const char *loader, const char *cache, uint64_t registryID, void (*missing)(const char *, const char *));
// The system shader cache (SharedShaderCache.h), named before native_bridge_metal_open. A reader passes the layer's directory and the
// user ids whose files it trusts there: its device then asks that directory, read only and by the layer's owner and mode rules, for
// what its own cache lacks. The process that fills the layer passes writer true and opens its device in the layer's directory: what
// it writes is readable by everyone. Returns whether the layer was taken; the reason of a refusal is in the `cache:` log line.
bool native_bridge_metal_system_cache(const char *reader, const uint32_t *owners, unsigned count, bool writer);
// Bare AIR bitcode as a library (the bridge's fixtures; Metal itself accepts only metallibs).
id native_bridge_metal_library_with_air(const void *bytes, size_t length);
// One line for the plugin's status report: command buffers committed/failed locally, pipelines made, refusals, the last refusal's text.
size_t native_bridge_metal_status(char *out, size_t size);
// The pixel formats of the textures asked for (first eight distinct) and how many texture requests the runtime refused.
size_t native_bridge_metal_formats(char *out, size_t size);
// The process's first problem (a refusal or a missing call) into out, and how many problems there were.
uint64_t native_bridge_metal_first_problem(char *out, size_t size);
// A note from the plugin for the telemetry's problem lines.
void native_bridge_metal_note(const char *what);
// Called once, on the thread that hit it, with the first problem's text (null: not called).
extern void (*native_bridge_metal_problem_hook)(const char *);
size_t native_bridge_metal_telemetry(char *out, size_t size);
// Structured cumulative artifact/driver-cache counters for offline qualification.
struct NmbShaderCacheStats;
bool native_bridge_metal_shader_cache_stats(struct NmbShaderCacheStats *out);
bool native_bridge_metal_work_stats(uint64_t out[10]);
// The same fixed CPU, memory and pipeline workload on the tablet and the host, so their times compare directly (int-145): compute, first-touch
// page faults, memset bandwidth, CommonCrypto SHA-256, then the first `pairs` vertex/fragment pairs of the given QuartzCore metallib made into
// BGRA8 render states and drawn once each (pipeline_timing_host's draw=1 order). The results become the telemetry's Y rows; also returned.
size_t native_bridge_metal_benchmark(id device, const char *metallib, unsigned pairs, char *out, size_t size);
// Lets go of every held IOSurface import whose surface only the import still retains (also done at each command buffer's completion).
void native_bridge_metal_trim_surface_imports(void);
// Purgeable memory (bridge/NativeMetalPurge.rs). Discards the buffers, textures and heaps their caller declared Volatile or Empty
// that nothing uses: cause 0 all of them (what the memory pressure handler does), cause 1 those idle for idleMs (what the age timer
// does, with the runtime's spare block age). Returns the bytes discarded; nothing with the boot argument gpu-purge=0.
uint64_t native_bridge_metal_purge(uint32_t cause, uint64_t idleMs);
// The bridge's sixteen purge counts (nmb_device_purge_counts).
bool native_bridge_metal_purge_counts(uint64_t out[16]);
// What a memory pressure event does: held surface imports, discardable resources, then the runtime's kept memory. Returns the bytes
// of the resources discarded.
uint64_t native_bridge_metal_relieve_memory(void);
}
#endif