From 97053d7033fea59526d540bf1f5deede24ffd598 Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Thu, 22 Oct 2026 15:44:06 +0800 Subject: [PATCH 01/92] feat(prime): log the first refusal of each library Record the first refused function of a library and the runtime's reason in prime.log, so translator gaps can be told apart from pipelines that need constants or attachments the guess does not give. A version tag ends each prime-done entry; v2 primes every library once more to collect the reasons and retry earlier refusals. --- PrimeShaders.h | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/PrimeShaders.h b/PrimeShaders.h index 4cd139f..7a66b91 100644 --- a/PrimeShaders.h +++ b/PrimeShaders.h @@ -103,6 +103,7 @@ static std::vector libraries(const std::string &listPath, std::stri // One library: every function it names, in its order; returns the log line. static std::string library(id device, const std::string &path, const std::string ¤t, const std::set &bad) { unsigned kernels = 0, renders = 0, made = 0, refused = 0, skipped = 0; + std::string firstRefusal; // the first refused function and why, so the log tells translator gaps from unspecialized constants const uint64_t began = nowMs(); @autoreleasepool { NSError *error = nil; @@ -137,6 +138,13 @@ static std::string library(id device, const std::string &path, const ok = false; } ok ? ++made : ++refused; + if (!ok && firstRefusal.empty()) { + NSString *why = error.localizedDescription ?: @"no error"; + firstRefusal = std::string(" first-refused=") + name.UTF8String + ": " + why.UTF8String; + if (firstRefusal.size() > 220) firstRefusal.resize(220); + for (char &c : firstRefusal) if (c == '\n' || c == '\t') c = ' '; + } + error = nil; // Low priority already; a short pause between pipelines keeps the render server's own work ahead. usleep(20000); } @@ -146,7 +154,7 @@ static std::string library(id device, const std::string &path, const char line[160]; snprintf(line, sizeof line, " kernels=%u renders=%u made=%u refused=%u skipped=%u ms=%llu", kernels, renders, made, refused, skipped, (unsigned long long)(nowMs() - began)); - return path + line; + return path + line + firstRefusal; } // The thread: waits for the desktop to settle, then works through the libraries not finished yet. @@ -170,7 +178,8 @@ static void run(id device, std::string root, unsigned delaySeconds, c ++libs; struct stat st = {}; if (stat(path.c_str(), &st) != 0) continue; - const std::string id = path + "\t" + std::to_string((long long)st.st_size) + "\t" + std::to_string((long long)st.st_mtimespec.tv_sec); + // The pass version ends the id: a newer one primes every library again (its refusals may be fixed, and their reasons get logged). + const std::string id = path + "\t" + std::to_string((long long)st.st_size) + "\t" + std::to_string((long long)st.st_mtimespec.tv_sec) + "\tv2"; if (done.count(id)) {++finished; continue;} appendLine(logPath, library(device, path, current, bad)); appendLine(donePath, id); From be24f5f745ddc9e407ac51c099b6e9be0cdabfb4 Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Thu, 22 Oct 2026 16:54:14 +0800 Subject: [PATCH 02/92] fix(frontend): make textures on planar IOSurfaces newTextureWithDescriptor:iosurface:plane: refused every surface with planes. Core Animation's lossless-compressed BGRA surfaces have one plane, so the render server failed to texture them and the widget renderer got no drawable surface and dropped every frame. The surface is now imported whole and the texture starts at the plane's address with the plane's row bytes. The texture status row counts surface textures made, refused and planar. --- NativeBridgeMetalFrontend.mm | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index f786fe4..259fe87 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -65,6 +65,8 @@ static void noteProblem(NSString *what) { #define NBLOG(...) do{NSString *nbText=[NSString stringWithFormat:__VA_ARGS__];NSLog(@"%@",nbText);noteProblem(nbText);}while(0) // The pixel formats textures were asked for (first eight distinct), with how many were refused: native_bridge_metal_formats. static uint32_t gFormats[8];static unsigned gFormatCount;static std::atomic gTextureRefusals; +// Textures over IOSurfaces: made, refused, and how many of the requests named a planar surface. +static std::atomic gSurfaceTextures,gSurfaceRefusals,gSurfacePlanar; static void noteFormat(uint32_t format,bool made) { std::lock_guard lock(gProblemLock); if(!made)gTextureRefusals++; @@ -1346,13 +1348,20 @@ static bool pipelineArchives(NSArray *list,std::vector &arch // The surface's pages are the texels: its mapping in this process is imported without a copy (pinned by the kernel, mapped into the GPU's // 4 KiB context by Turnip), so every process that imports the surface renders and samples the same memory. - (id)newTextureWithDescriptor:(MTLTextureDescriptor *)d iosurface:(IOSurfaceRef)surface plane:(NSUInteger)plane { - if(!surface || !d || plane || IOSurfaceGetPlaneCount(surface))return nil; + // A planar surface (Apple's lossless-compressed BGRA spelling has one plane, video formats two) is imported whole; the texture starts at + // the plane's own address and rows. Only a plain surface has no planes, and then only plane 0 exists. + const size_t planes=surface?IOSurfaceGetPlaneCount(surface):0; + if(!surface || !d || (planes?plane>=planes:plane!=0)){gSurfaceRefusals++;return nil;} + if(planes)gSurfacePlanar++; void *base=IOSurfaceGetBaseAddress(surface);const size_t page=(size_t)getpagesize(),allocation=IOSurfaceGetAllocSize(surface); - if(!page || (page&(page-1)) || !base || ((uintptr_t)base&(page-1)) || !allocation || allocation>SIZE_MAX-(page-1))return nil; + if(!page || (page&(page-1)) || !base || ((uintptr_t)base&(page-1)) || !allocation || allocation>SIZE_MAX-(page-1)){gSurfaceRefusals++;return nil;} + const uintptr_t start=planes?(uintptr_t)IOSurfaceGetBaseAddressOfPlane(surface,plane):(uintptr_t)base; + const size_t row=planes?IOSurfaceGetBytesPerRowOfPlane(surface,plane):IOSurfaceGetBytesPerRow(surface); + if(start<(uintptr_t)base || start-(uintptr_t)base>=allocation || !row){gSurfaceRefusals++;return nil;} const size_t bytes=(allocation+page-1)&~(page-1); - NBBuffer *buffer=surfaceBuffer(surface,base,bytes);if(!buffer)return nil; - NBTexture *texture=textureOnBuffer(buffer,d,0,IOSurfaceGetBytesPerRow(surface),true);if(!texture)return nil; - texture.surface=(IOSurfaceRef)CFRetain(surface);return (id)texture; + NBBuffer *buffer=surfaceBuffer(surface,base,bytes);if(!buffer){gSurfaceRefusals++;return nil;} + NBTexture *texture=textureOnBuffer(buffer,d,start-(uintptr_t)base,row,true);if(!texture){gSurfaceRefusals++;return nil;} + gSurfaceTextures++;texture.surface=(IOSurfaceRef)CFRetain(surface);return (id)texture; } - (id)newLibraryWithData:(dispatch_data_t)data error:(NSError **)error { const void *bytes=nullptr;size_t size=0;dispatch_data_t mapped=dispatch_data_create_map(data,&bytes,&size); @@ -1561,7 +1570,8 @@ extern "C" size_t native_bridge_metal_status(char *out,size_t size) { } static size_t native_bridge_metal_formats_locked(char *out,size_t size) { - int n=snprintf(out,size,"tex x%llu f",(unsigned long long)gTextureRefusals.load()); + int n=snprintf(out,size,"tex x%llu s%llu/%llu p%llu f",(unsigned long long)gTextureRefusals.load(),(unsigned long long)gSurfaceTextures.load(), + (unsigned long long)gSurfaceRefusals.load(),(unsigned long long)gSurfacePlanar.load()); for(unsigned i=0;i0 && (size_t)n0?(size_t)n:0; } From dfa00edcd71c7843303b3c6da6817d5f5c2ecee1 Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Thu, 22 Oct 2026 17:39:28 +0800 Subject: [PATCH 03/92] feat(prime): pause priming while the render server animates A pipeline compile on the priming thread competed with the render server for the CPU and the driver, so animations ran slower while priming was in progress. Before each function, priming now waits while more than 12 command buffers were committed in the last 250 ms, and then for one quiet second. The library log line reports the time waited. --- NativeBridgeMetalFrontend.mm | 7 +++++++ PrimeShaders.h | 28 +++++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 259fe87..f75da98 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -54,6 +54,13 @@ struct EncodedWork {uint32_t passes,draws,dispatches,blits;uint64_t pixels;}; struct CommitRecord {uint64_t committedUs=0,callbackUs=0,ageUs=0;int64_t scheduledUs=-1;uint32_t index=0;EncodedWork work={};}; static CommitRecord gCommitRing[32]; static std::mutex gCommitLock; +// Command buffers committed in the last windowUs microseconds (at most the ring's 32): shader priming waits while the render server animates. +extern "C" unsigned native_bridge_metal_recent_commits(uint64_t windowUs) { + const uint64_t now=nowUs();unsigned n=0; + std::lock_guard lock(gCommitLock); + for(const CommitRecord &r: gCommitRing)if(r.index && r.committedUs<=now && now-r.committedUs<=windowUs)n++; + return n; +} static std::mutex gProblemLock; static char gLastProblems[2][200]; static void noteProblem(NSString *what) { diff --git a/PrimeShaders.h b/PrimeShaders.h index 7a66b91..5910272 100644 --- a/PrimeShaders.h +++ b/PrimeShaders.h @@ -23,6 +23,8 @@ #include #include +extern "C" unsigned native_bridge_metal_recent_commits(uint64_t windowUs); + namespace prime { static const char *const kRoots[] = {"/System/Library", "/Applications", "/System/Cryptexes/OS/System/Library"}; @@ -100,9 +102,28 @@ static std::vector libraries(const std::string &listPath, std::stri return found; } +// Waits until the render server is not animating: more than 12 command buffers in 250 ms (an idle desktop redraws at about 12 fps, a swipe +// commits 25 or more) means frames are being drawn, and a pipeline compile then competes with them for the CPU and the driver (int-244: +// priming during the scripted swipes raised the median frame interval from 19 to 30 ms). Returns the milliseconds it waited. +static uint64_t waitForQuiet() { + const uint64_t began = nowMs(); + uint64_t quietSince = 0; + bool busy = false; + for (;;) { + const uint64_t now = nowMs(); + if (native_bridge_metal_recent_commits(250000) > 12) {busy = true; quietSince = 0;} + else if (!busy) return 0; + else if (!quietSince) quietSince = now; + // After animation, a second without it before the next compile, so a pause between two swipes does not let one in. + else if (now - quietSince >= 1000) return now - began; + usleep(100000); + } +} + // One library: every function it names, in its order; returns the log line. static std::string library(id device, const std::string &path, const std::string ¤t, const std::set &bad) { unsigned kernels = 0, renders = 0, made = 0, refused = 0, skipped = 0; + uint64_t waited = 0; std::string firstRefusal; // the first refused function and why, so the log tells translator gaps from unspecialized constants const uint64_t began = nowMs(); @autoreleasepool { @@ -118,6 +139,7 @@ static std::string library(id device, const std::string &path, const if (!function) {++refused; continue;} const MTLFunctionType type = function.functionType; if (type != MTLFunctionTypeKernel && type != MTLFunctionTypeVertex) continue; + waited += waitForQuiet(); writeCurrent(current, key); bool ok = false; @try { @@ -151,9 +173,9 @@ static std::string library(id device, const std::string &path, const } } unlink(current.c_str()); - char line[160]; - snprintf(line, sizeof line, " kernels=%u renders=%u made=%u refused=%u skipped=%u ms=%llu", kernels, renders, made, refused, skipped, - (unsigned long long)(nowMs() - began)); + char line[180]; + snprintf(line, sizeof line, " kernels=%u renders=%u made=%u refused=%u skipped=%u ms=%llu waited=%llu", kernels, renders, made, refused, + skipped, (unsigned long long)(nowMs() - began), (unsigned long long)waited); return path + line + firstRefusal; } From 6b14e1e2af8245c9274f8d8f359de415ebdd3482 Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Thu, 22 Oct 2026 17:40:32 +0800 Subject: [PATCH 04/92] feat(frontend): report why a surface texture was refused The texture status row counted refused IOSurface textures but not the check that refused them. An SR row now shows the last refusal's step, the surface's pixel format, plane, plane offset, row bytes and allocation, and the descriptor's size and pixel format. --- NativeBridgeMetalFrontend.mm | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index f75da98..71f59f9 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -74,6 +74,16 @@ static void noteProblem(NSString *what) { static uint32_t gFormats[8];static unsigned gFormatCount;static std::atomic gTextureRefusals; // Textures over IOSurfaces: made, refused, and how many of the requests named a planar surface. static std::atomic gSurfaceTextures,gSurfaceRefusals,gSurfacePlanar; +// The last refused surface texture: the check that refused it (1 plane, 2 base, 3 plane range, 4 import, 5 texture), the surface's format, +// planes, requested plane, plane offset, plane row bytes and allocation, and the descriptor's size and pixel format. +struct SurfaceRefusal {uint32_t step,format,planes,plane,pixelFormat;uint64_t offset,row,allocation,width,height;}; +static SurfaceRefusal gSurfaceLastRefusal; +static void refuseSurface(uint32_t step,IOSurfaceRef surface,MTLTextureDescriptor *d,NSUInteger plane,uint64_t offset,uint64_t row) { + gSurfaceRefusals++; + SurfaceRefusal r={step,surface?IOSurfaceGetPixelFormat(surface):0,surface?(uint32_t)IOSurfaceGetPlaneCount(surface):0,(uint32_t)plane, + d?(uint32_t)d.pixelFormat:0,offset,row,surface?IOSurfaceGetAllocSize(surface):0,d?d.width:0,d?d.height:0}; + std::lock_guard lock(gProblemLock);gSurfaceLastRefusal=r; +} static void noteFormat(uint32_t format,bool made) { std::lock_guard lock(gProblemLock); if(!made)gTextureRefusals++; @@ -1358,16 +1368,16 @@ static bool pipelineArchives(NSArray *list,std::vector &arch // A planar surface (Apple's lossless-compressed BGRA spelling has one plane, video formats two) is imported whole; the texture starts at // the plane's own address and rows. Only a plain surface has no planes, and then only plane 0 exists. const size_t planes=surface?IOSurfaceGetPlaneCount(surface):0; - if(!surface || !d || (planes?plane>=planes:plane!=0)){gSurfaceRefusals++;return nil;} + if(!surface || !d || (planes?plane>=planes:plane!=0)){refuseSurface(1,surface,d,plane,0,0);return nil;} if(planes)gSurfacePlanar++; void *base=IOSurfaceGetBaseAddress(surface);const size_t page=(size_t)getpagesize(),allocation=IOSurfaceGetAllocSize(surface); - if(!page || (page&(page-1)) || !base || ((uintptr_t)base&(page-1)) || !allocation || allocation>SIZE_MAX-(page-1)){gSurfaceRefusals++;return nil;} + if(!page || (page&(page-1)) || !base || ((uintptr_t)base&(page-1)) || !allocation || allocation>SIZE_MAX-(page-1)){refuseSurface(2,surface,d,plane,(uintptr_t)base,0);return nil;} const uintptr_t start=planes?(uintptr_t)IOSurfaceGetBaseAddressOfPlane(surface,plane):(uintptr_t)base; const size_t row=planes?IOSurfaceGetBytesPerRowOfPlane(surface,plane):IOSurfaceGetBytesPerRow(surface); - if(start<(uintptr_t)base || start-(uintptr_t)base>=allocation || !row){gSurfaceRefusals++;return nil;} + if(start<(uintptr_t)base || start-(uintptr_t)base>=allocation || !row){refuseSurface(3,surface,d,plane,start-(uintptr_t)base,row);return nil;} const size_t bytes=(allocation+page-1)&~(page-1); - NBBuffer *buffer=surfaceBuffer(surface,base,bytes);if(!buffer){gSurfaceRefusals++;return nil;} - NBTexture *texture=textureOnBuffer(buffer,d,start-(uintptr_t)base,row,true);if(!texture){gSurfaceRefusals++;return nil;} + NBBuffer *buffer=surfaceBuffer(surface,base,bytes);if(!buffer){refuseSurface(4,surface,d,plane,start-(uintptr_t)base,row);return nil;} + NBTexture *texture=textureOnBuffer(buffer,d,start-(uintptr_t)base,row,true);if(!texture){refuseSurface(5,surface,d,plane,start-(uintptr_t)base,row);return nil;} gSurfaceTextures++;texture.surface=(IOSurfaceRef)CFRetain(surface);return (id)texture; } - (id)newLibraryWithData:(dispatch_data_t)data error:(NSError **)error { @@ -1708,6 +1718,10 @@ extern "C" size_t native_bridge_metal_telemetry(char *out,size_t size) { { std::lock_guard lock(gProblemLock); char formats[160];native_bridge_metal_formats_locked(formats,sizeof formats);row("F %.86s",formats); + const SurfaceRefusal &r=gSurfaceLastRefusal; + if(r.step)row("SR %u %c%c%c%c pl%u/%u o%llx rb%llu a%llx %llux%llu pf%u",r.step,(char)(r.format>>24),(char)(r.format>>16),(char)(r.format>>8), + (char)r.format,r.plane,r.planes,(unsigned long long)r.offset,(unsigned long long)r.row,(unsigned long long)r.allocation,(unsigned long long)r.width, + (unsigned long long)r.height,r.pixelFormat); row("P0 %.86s",gLastProblems[0]);row("P0b %.86s",strlen(gLastProblems[0])>86?gLastProblems[0]+86:""); row("P1 %.86s",gLastProblems[1]);row("P1b %.86s",strlen(gLastProblems[1])>86?gLastProblems[1]+86:""); } From 36c07f8fad7ae61393125245d688c62c73e7f5d5 Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Thu, 22 Oct 2026 17:55:11 +0800 Subject: [PATCH 05/92] fix(plugin): export the pipeline cache only on request Data now persists across boots, so the pipeline cache no longer needs to be exported to the UFS log. Once priming had filled the cache directory, each rescan and export competed with the render server for the CPU. The export thread now starts only when NativeAdrenoMetal.cacheexport sits beside the bundle. --- NativeAdrenoMetalPlugin.mm | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/NativeAdrenoMetalPlugin.mm b/NativeAdrenoMetalPlugin.mm index 514e05b..1bb0c82 100644 --- a/NativeAdrenoMetalPlugin.mm +++ b/NativeAdrenoMetalPlugin.mm @@ -357,11 +357,17 @@ static id deviceForPort(unsigned port) { } return nullptr; }, gpu); - pthread_t exporter; - pthread_create(&exporter, &attributes, [](void *arg) -> void * { - cacheexport::run(*static_cast(arg), gCacheDirectory); - return nullptr; - }, gpu); + // The cache directory's export to the UFS log only when NativeAdrenoMetal.cacheexport sits beside the bundle: Data persists across + // boots now, so the caches no longer need the trip through the host, and once priming has filled the directory, every rescan and + // export competed with the render server (int-245: the scanning thread at 45% CPU, the sending one at 48%, frames twice as slow). + char exportPath[1024]; + if (requestedPath("NativeAdrenoMetal.cacheexport", exportPath, sizeof exportPath)) { + pthread_t exporter; + pthread_create(&exporter, &attributes, [](void *arg) -> void * { + cacheexport::run(*static_cast(arg), gCacheDirectory); + return nullptr; + }, gpu); + } // Every Metal library of the OS through the runtime, 90 s after the render server opened the device (PrimeShaders.h); the boots that froze // or crashed before three minutes never primed anything. Progress is kept in the Data cache directory, so later boots resume. // The translator recurses deeply on large functions: give it the stack of a main thread. From f3992a1cf4f69492b799dfd894ac94829e35384e Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Thu, 22 Oct 2026 17:59:36 +0800 Subject: [PATCH 06/92] fix(frontend): make textures on two-plane IOSurfaces Widget snapshots are drawn into the render server's two-plane RGB surfaces ('_RB4') as RGB8_444_2P textures. The frontend imported every surface plane as a single-plane texture, so the runtime refused them and the widgets stayed white. Surfaces with two planes now go through a new bridge call that passes the surface's format, size and second plane, and the runtime accepts '_RB4' as an 8-bit 4:4:4 surface. The SR row also prints the surface format in hex. --- NativeBridgeMetalFrontend.mm | 15 +++++++++-- bridge/NativeMetalObjects.h | 4 +++ bridge/NativeMetalObjects.rs | 21 +++++++++++++++- .../0007-accept-two-plane-rgb-surfaces.patch | 25 +++++++++++++++++++ 4 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 bridge/patches/0007-accept-two-plane-rgb-surfaces.patch diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 71f59f9..509d909 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -299,6 +299,16 @@ static NBTexture *textureOnBuffer(NBBuffer *buffer,MTLTextureDescriptor *d,NSUIn NBTexture *texture=[NBTexture new];texture.handle=handle;nmb_texture_describe(handle,&descriptor);texture.descriptor=descriptor; texture.buffer=buffer;texture.offset=offset;texture.rowBytes=row;return texture; } +// A texture over a two-plane surface: the runtime reads plane 1 from the surface's own layout (two-plane RGB and YCbCr formats). +static NBTexture *textureOnSurface(NBBuffer *buffer,MTLTextureDescriptor *d,IOSurfaceRef surface,NSUInteger plane,NSUInteger offset,NSUInteger row) { + NmbTextureDescriptor descriptor;if(!buffer || !textureDescriptor(d,&descriptor))return nil; + const uint64_t base=(uintptr_t)IOSurfaceGetBaseAddress(surface),chroma=(uintptr_t)IOSurfaceGetBaseAddressOfPlane(surface,1); + NmbTexture *handle=nmb_texture_new_on_surface(buffer.handle,&descriptor,offset,row,IOSurfaceGetPixelFormat(surface),plane, + IOSurfaceGetWidth(surface),IOSurfaceGetHeight(surface),chroma>=base?chroma-base:0,IOSurfaceGetBytesPerRowOfPlane(surface,1)); + if(!handle){NBLOG(@"native-metal: texture over surface refused: %@",lastError());return nil;} + NBTexture *texture=[NBTexture new];texture.handle=handle;nmb_texture_describe(handle,&descriptor);texture.descriptor=descriptor; + texture.buffer=buffer;texture.offset=offset;texture.rowBytes=row;return texture; +} @implementation NBBuffer (Textures) - (id)newTextureWithDescriptor:(MTLTextureDescriptor *)d offset:(NSUInteger)offset bytesPerRow:(NSUInteger)row { return (id)textureOnBuffer(self,d,offset,row,false); @@ -1377,7 +1387,8 @@ static bool pipelineArchives(NSArray *list,std::vector &arch if(start<(uintptr_t)base || start-(uintptr_t)base>=allocation || !row){refuseSurface(3,surface,d,plane,start-(uintptr_t)base,row);return nil;} const size_t bytes=(allocation+page-1)&~(page-1); NBBuffer *buffer=surfaceBuffer(surface,base,bytes);if(!buffer){refuseSurface(4,surface,d,plane,start-(uintptr_t)base,row);return nil;} - NBTexture *texture=textureOnBuffer(buffer,d,start-(uintptr_t)base,row,true);if(!texture){refuseSurface(5,surface,d,plane,start-(uintptr_t)base,row);return nil;} + NBTexture *texture=planes>=2?textureOnSurface(buffer,d,surface,plane,start-(uintptr_t)base,row):textureOnBuffer(buffer,d,start-(uintptr_t)base,row,true); + if(!texture){refuseSurface(5,surface,d,plane,start-(uintptr_t)base,row);return nil;} gSurfaceTextures++;texture.surface=(IOSurfaceRef)CFRetain(surface);return (id)texture; } - (id)newLibraryWithData:(dispatch_data_t)data error:(NSError **)error { @@ -1719,7 +1730,7 @@ extern "C" size_t native_bridge_metal_telemetry(char *out,size_t size) { std::lock_guard lock(gProblemLock); char formats[160];native_bridge_metal_formats_locked(formats,sizeof formats);row("F %.86s",formats); const SurfaceRefusal &r=gSurfaceLastRefusal; - if(r.step)row("SR %u %c%c%c%c pl%u/%u o%llx rb%llu a%llx %llux%llu pf%u",r.step,(char)(r.format>>24),(char)(r.format>>16),(char)(r.format>>8), + if(r.step)row("SR %u %08x %c%c%c%c pl%u/%u o%llx rb%llu a%llx %llux%llu pf%u",r.step,r.format,(char)(r.format>>24),(char)(r.format>>16),(char)(r.format>>8), (char)r.format,r.plane,r.planes,(unsigned long long)r.offset,(unsigned long long)r.row,(unsigned long long)r.allocation,(unsigned long long)r.width, (unsigned long long)r.height,r.pixelFormat); row("P0 %.86s",gLastProblems[0]);row("P0b %.86s",strlen(gLastProblems[0])>86?gLastProblems[0]+86:""); diff --git a/bridge/NativeMetalObjects.h b/bridge/NativeMetalObjects.h index 0b73a7d..4a60d34 100644 --- a/bridge/NativeMetalObjects.h +++ b/bridge/NativeMetalObjects.h @@ -89,6 +89,10 @@ NmbTexture *nmb_heap_new_texture(const NmbHeap *, const struct NmbTextureDescrip NmbTexture *nmb_texture_new(NmbDevice *, const struct NmbTextureDescriptor *); /* A linear texture over a buffer's bytes; iosurface marks the buffer as standing for a surface without planes (Metal's IOSurface rules). */ NmbTexture *nmb_texture_new_on_buffer(NmbBuffer *, const struct NmbTextureDescriptor *, uint64_t offset, uint64_t bytes_per_row, bool iosurface); +// A texture over a surface with planes; a two-plane surface gives its OSType, the asked plane, its size and plane 1's offset and rows. +NmbTexture *nmb_texture_new_on_surface(NmbBuffer *, const struct NmbTextureDescriptor *, uint64_t offset, uint64_t bytes_per_row, + uint32_t pixel_format, uint64_t plane, uint64_t width, uint64_t height, uint64_t chroma_offset, + uint64_t chroma_bytes_per_row); NmbTexture *nmb_texture_new_linear(NmbBuffer *, const struct NmbTextureDescriptor *, uint64_t offset, uint64_t bytes_per_row, uint64_t bytes_per_image); /* whole: a view of every level and slice as format; else levels and slices are {start, count} and kind the view's texture type. */ NmbTexture *nmb_texture_new_view(NmbTexture *, uint64_t format, uint64_t kind, const uint64_t levels[2], const uint64_t slices[2], bool whole); diff --git a/bridge/NativeMetalObjects.rs b/bridge/NativeMetalObjects.rs index 4f4bed3..8418de9 100644 --- a/bridge/NativeMetalObjects.rs +++ b/bridge/NativeMetalObjects.rs @@ -18,7 +18,7 @@ use metal2vulkan::pipeline::{ }; use metal2vulkan_runtime::{ BinaryArchive, BinaryArchiveDescriptor, BlitCommandEncoder, FamilyRow, FeatureRow, GpuFamily, RowState, BlitOption, Buffer, ClearColor, CommandBuffer, CommandQueue, ComputeCommandEncoder, - ComputePipelineState, Deallocator, DebugMessage, DepthStencilState, Device, DeviceOptions, MessageSeverity, MessageSink, Event, EventRef, Fence, Function, Heap, HeapDescriptor, HeapType, HostMemory, IoSurfacePlane, Library, LoadAction, + ComputePipelineState, Deallocator, DebugMessage, DepthStencilState, Device, DeviceOptions, MessageSeverity, MessageSink, Event, EventRef, Fence, Function, Heap, HeapDescriptor, HeapType, HostMemory, IoSurfacePlane, IoSurfaceYCbCr, Library, LoadAction, MetalLevel, Origin, PipelineOption, PipelineOptions, PurgeableState, PrivateCount, PrivateFlag, PrivateProperties, MAX_COLOR_ATTACHMENTS, Region, RenderCommandEncoder, RenderPassDescriptor, RenderPipelineState, ResourceOptions, SamplerState, ScissorRect, SharedEvent, SharedEventListener, Size, StoreAction, Texture, TextureDescriptor, Viewport, VisibilityResultMode, }; @@ -741,6 +741,25 @@ pub extern "C" fn nmb_texture_new_on_buffer(buffer: *mut NmbBuffer, descriptor: }) } +/// `newTextureWithDescriptor:iosurface:plane:` over a surface with planes: `offset` and `bytes_per_row` are the asked plane's, and a +/// two-plane surface also gives its OSType, the asked plane, its size and plane 1's offset and rows (`chroma`, 0 and 0 for one plane), +/// which a two-plane texture reads its second plane from. +#[unsafe(no_mangle)] +pub extern "C" fn nmb_texture_new_on_surface(buffer: *mut NmbBuffer, descriptor: *const NmbTextureDescriptor, offset: u64, + bytes_per_row: u64, pixel_format: u32, plane: u64, width: u64, height: u64, + chroma_offset: u64, chroma_bytes_per_row: u64) -> *mut NmbTexture { + guard(std::ptr::null_mut(), || { + let descriptor = texture_descriptor(descriptor)?; + let buffer = &get(buffer)?.0; + let mut surface = IoSurfacePlane::new(offset, bytes_per_row, true); + if chroma_bytes_per_row != 0 { + surface = surface.with_ycbcr(IoSurfaceYCbCr::new(pixel_format, plane, [width, height], chroma_offset, chroma_bytes_per_row)); + } + let texture = buffer.new_texture_with_iosurface_plane(&descriptor, surface); + Ok(boxed(NmbTexture(texture.map_err(e("texture over surface"))?))) + }) +} + /// `newLinearTextureWithDescriptor:offset:bytesPerRow:bytesPerImage:` (Apple SPI). #[unsafe(no_mangle)] pub extern "C" fn nmb_texture_new_linear(buffer: *mut NmbBuffer, descriptor: *const NmbTextureDescriptor, offset: u64, bytes_per_row: u64, diff --git a/bridge/patches/0007-accept-two-plane-rgb-surfaces.patch b/bridge/patches/0007-accept-two-plane-rgb-surfaces.patch new file mode 100644 index 0000000..b3977de --- /dev/null +++ b/bridge/patches/0007-accept-two-plane-rgb-surfaces.patch @@ -0,0 +1,25 @@ +diff --git a/crates/metal2vulkan-runtime/src/ycbcr.rs b/crates/metal2vulkan-runtime/src/ycbcr.rs +index 368d62e..7e545d2 100644 +--- a/crates/metal2vulkan-runtime/src/ycbcr.rs ++++ b/crates/metal2vulkan-runtime/src/ycbcr.rs +@@ -151,8 +151,10 @@ use std::ops::Range; + /// range, and `xf20`, `xf22` and `xf44` the same, full range; `p420`, `p422` and `p444`, and + /// `pf20`, `pf22` and `pf44`, the same packed three to a 32-bit word. A format reads a surface + /// whose chroma plane holds its own: a 4:2:0 format a 4:2:2 or 4:4:4 surface's, a 4:2:2 format a +-/// 4:4:4 one's, each at the surface's rows. +-const SURFACES: [(&[u8; 4], u32, bool, YCbCrRange, ChromaSubsampling); 18] = { ++/// 4:4:4 one's, each at the surface's rows. `_RB4` is the render server's own two-plane RGB surface (green codes in plane 0, the ++/// red and blue pairs in plane 1, 8-bit 4:4:4), which widget snapshots are drawn into as `RGB8_444_2P` (542); the code formats ++/// read no range, so its range is only nominal. ++const SURFACES: [(&[u8; 4], u32, bool, YCbCrRange, ChromaSubsampling); 19] = { + use ChromaSubsampling::{Chroma420 as C420, Chroma422 as C422, Chroma444 as C444}; + [ + (b"420v", 8, false, YCbCrRange::Video, C420), +@@ -173,6 +175,7 @@ const SURFACES: [(&[u8; 4], u32, bool, YCbCrRange, ChromaSubsampling); 18] = { + (b"pf22", 10, true, YCbCrRange::Full, C422), + (b"p444", 10, true, YCbCrRange::Video, C444), + (b"pf44", 10, true, YCbCrRange::Full, C444), ++ (b"_RB4", 8, false, YCbCrRange::Full, C444), + ] + }; + /// What the planes' offsets and rows are multiples of: Metal's GPU reads a plane from its offset From 2a144783721807ba6c847e4ee8f84bd434b30c63 Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Thu, 22 Oct 2026 18:14:27 +0800 Subject: [PATCH 07/92] fix(prime): prime again after a new plugin build The finished-library list was keyed by path, size, modification time and a fixed pass tag. A runtime change invalidates the pipeline cache, yet priming still treated every library as done. The key now ends with the plugin's Mach-O UUID, so a new build primes every library again; pipelines still in the cache cost only a cache hit. --- PrimeShaders.h | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/PrimeShaders.h b/PrimeShaders.h index 5910272..935d2d3 100644 --- a/PrimeShaders.h +++ b/PrimeShaders.h @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -120,6 +121,27 @@ static uint64_t waitForQuiet() { } } +// The plugin's own build (its Mach-O LC_UUID), which ends each finished library's id: a new runtime may no longer find the pipelines an +// earlier one cached (int-246: after a runtime change the Vulkan cache had 0 hits and 214 misses, while prime-done still listed every +// library), so a new build primes every library again; what is still cached then costs only a cache hit. +static std::string buildId() { + Dl_info info = {}; + if (!dladdr(reinterpret_cast(&buildId), &info) || !info.dli_fbase) return "unknown"; + const auto *header = static_cast(info.dli_fbase); + if (header->magic != MH_MAGIC_64) return "unknown"; + const auto *command = reinterpret_cast(header + 1); + for (uint32_t i = 0; i < header->ncmds; ++i) { + if (command->cmd == LC_UUID) { + const uint8_t *uuid = reinterpret_cast(command)->uuid; + char text[33]; + for (int k = 0; k < 16; ++k) snprintf(text + 2 * k, 3, "%02x", uuid[k]); + return text; + } + command = reinterpret_cast(reinterpret_cast(command) + command->cmdsize); + } + return "unknown"; +} + // One library: every function it names, in its order; returns the log line. static std::string library(id device, const std::string &path, const std::string ¤t, const std::set &bad) { unsigned kernels = 0, renders = 0, made = 0, refused = 0, skipped = 0; @@ -195,13 +217,14 @@ static void run(id device, std::string root, unsigned delaySeconds, c unsigned libs = 0, finished = 0; std::string note; const std::vector paths = libraries(listPath, note); - appendLine(logPath, note); + const std::string build = buildId(); + appendLine(logPath, note + " build=" + build); for (const std::string &path : paths) { ++libs; struct stat st = {}; if (stat(path.c_str(), &st) != 0) continue; - // The pass version ends the id: a newer one primes every library again (its refusals may be fixed, and their reasons get logged). - const std::string id = path + "\t" + std::to_string((long long)st.st_size) + "\t" + std::to_string((long long)st.st_mtimespec.tv_sec) + "\tv2"; + // The plugin's build ends the id: a newer one primes every library again (buildId()). + const std::string id = path + "\t" + std::to_string((long long)st.st_size) + "\t" + std::to_string((long long)st.st_mtimespec.tv_sec) + "\t" + build; if (done.count(id)) {++finished; continue;} appendLine(logPath, library(device, path, current, bad)); appendLine(donePath, id); From 12a28da6bb69c4b79b2c5987b8197bd0239e0fb3 Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Thu, 22 Oct 2026 18:22:31 +0800 Subject: [PATCH 08/92] feat(frontend): report the runtime's texture copies An L row shows how many linear images at Metal's row pitch the runtime made and how many the driver refused, how many textures got a shadow image, the shadow copies in and texels copied back, and the passes over linear images rendered in place and through copies. --- NativeBridgeMetalFrontend.mm | 6 ++++++ bridge/NativeMetalObjects.h | 2 ++ bridge/NativeMetalObjects.rs | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 509d909..5a8ac23 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -1723,6 +1723,12 @@ extern "C" size_t native_bridge_metal_telemetry(char *out,size_t size) { (unsigned long long)work[0],ms(work[1]),ms(work[2]),ms(work[3]),(unsigned long long)work[4]); row("V cache hit%llu miss%llu wr%llu | msg i%llu w%llu",(unsigned long long)work[7],(unsigned long long)work[8],(unsigned long long)work[9],(unsigned long long)work[5], (unsigned long long)work[6]); + // Where textures over buffers and surfaces cost copies: linear images at Metal's pitch made, refused, refused again; shadowed textures, + // their copies in, texels copied back; passes over linear images in place and through copies. + uint64_t tc[8]={}; + if(nmb_device_texture_counts(gDevice,tc)) + row("L lin %llu/%llu/%llu sh %llu cp %llu bk %llu pass %llu/%llu",(unsigned long long)tc[0],(unsigned long long)tc[1],(unsigned long long)tc[2], + (unsigned long long)tc[3],(unsigned long long)tc[4],(unsigned long long)tc[5],(unsigned long long)tc[6],(unsigned long long)tc[7]); char warning[200];size_t n=nmb_last_warning(warning,sizeof warning); if(n){row("W %.86s",warning);if(n>86)row("Wb %.86s",warning+86);} } diff --git a/bridge/NativeMetalObjects.h b/bridge/NativeMetalObjects.h index 4a60d34..ac2fadf 100644 --- a/bridge/NativeMetalObjects.h +++ b/bridge/NativeMetalObjects.h @@ -39,6 +39,8 @@ size_t nmb_device_name(const NmbDevice *, char *out, size_t size); uint64_t nmb_device_max_buffer_length(const NmbDevice *); // Pipeline makings (count, total us, longest us, waited us, failed), sink messages (info, warning+), shader cache hits/misses/writes. bool nmb_device_work_stats(const NmbDevice *, uint64_t out[10]); +// Linear images made/refused/refused again, shadowed textures, shadow copies, texels copied back, linear passes in place/through copies. +bool nmb_device_texture_counts(const NmbDevice *, uint64_t out[8]); /* Artifact lookups and driver pipeline records are separate cache layers. */ struct NmbShaderCacheStats { uint64_t hits, misses, rejected, read_failures, writes, write_failures, evictions; diff --git a/bridge/NativeMetalObjects.rs b/bridge/NativeMetalObjects.rs index 8418de9..57d9c13 100644 --- a/bridge/NativeMetalObjects.rs +++ b/bridge/NativeMetalObjects.rs @@ -230,6 +230,25 @@ pub extern "C" fn nmb_device_work_stats(device: *const NmbDevice, out: *mut u64) }) } +/// The device's texture copy counts into the caller's eight words: linear images at Metal's row pitch made, refused by the driver and +/// refused again from memory, textures given a shadow image, shadow copies in and texels copied back, and passes over linear images +/// rendered in place and through copies. +#[unsafe(no_mangle)] +pub extern "C" fn nmb_device_texture_counts(device: *const NmbDevice, out: *mut u64) -> bool { + guard(false, || { + if out.is_null() { + return Err("null counts".into()); + } + // SAFETY: the header's eight words. + let out = unsafe { std::slice::from_raw_parts_mut(out, 8) }; + let w = get(device)?.0.work_counts(); + out.copy_from_slice(&[w.pitched_host_images, w.pitched_host_image_refusals, w.pitched_host_image_refusals_recalled, + w.shadowed_textures, w.shadow_copies, w.shadow_texels_copied_back, w.linear_passes_in_place, + w.linear_passes_through_copies]); + Ok(true) + }) +} + /// Separate artifact and driver-record counters, mirroring the C ABI. #[repr(C)] pub struct NmbShaderCacheStats { From 64e80e4e2446a15eddfe83339c3f561549f2d62b Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Thu, 22 Oct 2026 18:25:07 +0800 Subject: [PATCH 09/92] feat(prime): prime constant kernels and rasterized vertex functions Priming refused kernels that use a function constant's value and vertex functions primed without rasterization. A kernel refused for a missing constant is now primed as a variant with every constant set to zero, through a new bridge call. A vertex function now always rasterizes into one BGRA8 attachment, paired with the fragment function of the same stem under more naming patterns. The library log line counts the zero-constant variants. --- NativeBridgeMetalFrontend.mm | 7 ++++++ PrimeShaders.h | 33 +++++++++++++++++++++------ bridge/NativeMetalObjects.h | 2 ++ bridge/NativeMetalObjects.rs | 43 ++++++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 7 deletions(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 5a8ac23..d42dd39 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -439,6 +439,13 @@ static std::string quartzCoreCallers(); NBFunction *function=[NBFunction new];function.handle=handle;function.name=name;function.functionType=(MTLFunctionType)nmb_function_type(handle); return (id)function; } +// Shader priming (PrimeShaders.h): the function with each of its function constants set to zero, or nil. +- (id)nb_newFunctionWithZeroConstants:(NSString *)name { + NmbFunction *handle=name?nmb_function_new_zero_constants(_handle,name.UTF8String):nullptr; + if(!handle)return nil; + NBFunction *function=[NBFunction new];function.handle=handle;function.name=name;function.functionType=(MTLFunctionType)nmb_function_type(handle); + return (id)function; +} @end @interface NBComputePipeline : NBObject @property(nonatomic) NmbComputePipeline *handle; diff --git a/PrimeShaders.h b/PrimeShaders.h index 935d2d3..cdf4f9b 100644 --- a/PrimeShaders.h +++ b/PrimeShaders.h @@ -26,6 +26,10 @@ extern "C" unsigned native_bridge_metal_recent_commits(uint64_t windowUs); +@interface NSObject (NBPrimeZeroConstants) +- (id)nb_newFunctionWithZeroConstants:(NSString *)name; +@end + namespace prime { static const char *const kRoots[] = {"/System/Library", "/Applications", "/System/Cryptexes/OS/System/Library"}; @@ -144,7 +148,7 @@ static std::string buildId() { // One library: every function it names, in its order; returns the log line. static std::string library(id device, const std::string &path, const std::string ¤t, const std::set &bad) { - unsigned kernels = 0, renders = 0, made = 0, refused = 0, skipped = 0; + unsigned kernels = 0, renders = 0, made = 0, refused = 0, skipped = 0, zeroed = 0; uint64_t waited = 0; std::string firstRefusal; // the first refused function and why, so the log tells translator gaps from unspecialized constants const uint64_t began = nowMs(); @@ -168,14 +172,29 @@ static std::string library(id device, const std::string &path, const if (type == MTLFunctionTypeKernel) { ++kernels; ok = [device newComputePipelineStateWithFunction:function error:&error] != nil; + // A kernel that uses a function constant's value needs one: prime the variant with every constant zero, which + // still takes the module through translation and the driver (int-245: 2901 kernels refused for want of one). + if (!ok && [error.localizedDescription containsString:@"FunctionConstant"] && + [lib respondsToSelector:@selector(nb_newFunctionWithZeroConstants:)]) { + id zero = [(NSObject *)lib nb_newFunctionWithZeroConstants:name]; + if (zero) {error = nil; ok = [device newComputePipelineStateWithFunction:zero error:&error] != nil; if (ok) ++zeroed;} + } } else { ++renders; MTLRenderPipelineDescriptor *d = [MTLRenderPipelineDescriptor new]; d.vertexFunction = function; - NSString *fragment = [name stringByReplacingOccurrencesOfString:@"_vert_" withString:@"_frag_"]; - if (![fragment isEqualToString:name] && [names containsObject:fragment]) d.fragmentFunction = [lib newFunctionWithName:fragment]; - if (!d.fragmentFunction) d.rasterizationEnabled = NO; - else d.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm; + d.colorAttachments[0].pixelFormat = MTLPixelFormatBGRA8Unorm; + // The fragment function of the same stem under the libraries' usual spellings; without one the pipeline still + // rasterizes (Metal refuses an unrasterized pipeline whose vertex function returns a position, int-245: 732 refusals). + static NSString *const kPairs[][2] = {{@"_vert_", @"_frag_"}, {@"_vertex", @"_fragment"}, {@"Vertex", @"Fragment"}, + {@"vertex", @"fragment"}, {@"_vs", @"_fs"}, {@"_vert", @"_frag"}, {@"VS", @"PS"}}; + for (const auto &pair : kPairs) { + NSString *fragment = [name stringByReplacingOccurrencesOfString:pair[0] withString:pair[1]]; + if (![fragment isEqualToString:name] && [names containsObject:fragment]) { + d.fragmentFunction = [lib newFunctionWithName:fragment]; + if (d.fragmentFunction) break; + } + } ok = [device newRenderPipelineStateWithDescriptor:d error:&error] != nil; } } @catch (NSException *exception) { @@ -196,8 +215,8 @@ static std::string library(id device, const std::string &path, const } unlink(current.c_str()); char line[180]; - snprintf(line, sizeof line, " kernels=%u renders=%u made=%u refused=%u skipped=%u ms=%llu waited=%llu", kernels, renders, made, refused, - skipped, (unsigned long long)(nowMs() - began), (unsigned long long)waited); + snprintf(line, sizeof line, " kernels=%u renders=%u made=%u zeroed=%u refused=%u skipped=%u ms=%llu waited=%llu", kernels, renders, made, + zeroed, refused, skipped, (unsigned long long)(nowMs() - began), (unsigned long long)waited); return path + line + firstRefusal; } diff --git a/bridge/NativeMetalObjects.h b/bridge/NativeMetalObjects.h index ac2fadf..849524b 100644 --- a/bridge/NativeMetalObjects.h +++ b/bridge/NativeMetalObjects.h @@ -118,6 +118,8 @@ NmbFunction *nmb_function_new(NmbLibrary *, const char *name); * indexed value; bytes holds the value's bytes. */ struct NmbConstant {uint64_t type, index; const char *name; const void *bytes; uint64_t length;}; NmbFunction *nmb_function_new_specialized(NmbLibrary *, const char *name, const struct NmbConstant *constants, uint32_t count); +// The library's function with every function constant it declares set to zero (shader priming), or null. +NmbFunction *nmb_function_new_zero_constants(NmbLibrary *, const char *name); void nmb_function_release(NmbFunction *); /* Metal's MTLFunctionType (1 vertex, 2 fragment, 3 kernel, ...). */ uint64_t nmb_function_type(const NmbFunction *); diff --git a/bridge/NativeMetalObjects.rs b/bridge/NativeMetalObjects.rs index 57d9c13..ee547fa 100644 --- a/bridge/NativeMetalObjects.rs +++ b/bridge/NativeMetalObjects.rs @@ -1008,6 +1008,49 @@ pub unsafe extern "C" fn nmb_function_new_specialized(library: *mut NmbLibrary, }) } +/// Bytes of a function constant's `MTLDataType` (scalars and 2- and 4-wide vectors), 0 for a type that is not given a value. +fn constant_bytes(raw: u32) -> usize { + match raw { + 53 | 45 | 49 => 1, + 16 | 46 | 50 | 54 => 2, + 3 | 29 | 33 | 17 | 38 | 42 | 48 | 52 | 56 => 4, + 4 | 30 | 34 | 19 | 40 | 44 | 81 | 85 => 8, + 6 | 32 | 36 | 82 | 86 => 16, + 84 | 88 => 32, + _ => 0, + } +} + +/// The library's function `name` with every function constant it declares set to zero (false for a bool), for shader priming: a +/// function whose code uses a constant's value is otherwise refused (`NeedsInput(FunctionConstant)`), and this variant still takes its +/// module through decoding, translation and the driver. Null when a constant's type takes no value. +/// +/// # Safety +/// `name` is a C string. +#[unsafe(no_mangle)] +pub unsafe extern "C" fn nmb_function_new_zero_constants(library: *mut NmbLibrary, name: *const c_char) -> *mut NmbFunction { + guard(std::ptr::null_mut(), || { + if name.is_null() { + return Err("null function name".into()); + } + // SAFETY: the caller's contract. + let name = unsafe { CStr::from_ptr(name) }.to_str().map_err(e("function name"))?; + let library = &get(library)?.0; + let generic = library.new_function_with_name(name).map_err(e("function"))?; + let mut values = FunctionConstantValues::defaults(); + for (key, constant) in generic.function_constants_dictionary().map_err(e("function constants"))? { + let ty = constant.data_type(); + let bytes = constant_bytes(ty.as_raw()); + if bytes == 0 { + return Err(format!("function constant {key} of type {}", ty.as_raw())); + } + values.by_index.insert(constant.index(), FunctionConstantValue::new(ty, vec![0; bytes])); + } + let function = library.new_function_with_name_constant_values(name, &values).map_err(e("zero-specialized function"))?; + Ok(boxed(NmbFunction(function))) + }) +} + #[unsafe(no_mangle)] pub extern "C" fn nmb_function_release(function: *mut NmbFunction) { release(function) From 9aeaaff9d9ca33798a656eca66878e14c4d5ff79 Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Thu, 22 Oct 2026 18:28:11 +0800 Subject: [PATCH 10/92] feat(prime): report priming progress in the telemetry table A PR row shows whether a pipeline is being primed, how many libraries of the list are done and how many pipelines priming made, so frame timings can be compared with and without priming at work. --- NativeBridgeMetalFrontend.mm | 3 +++ PrimeShaders.h | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index d42dd39..385878c 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -78,6 +78,8 @@ static std::atomic gSurfaceTextures,gSurfaceRefusals,gSurfacePlanar; // planes, requested plane, plane offset, plane row bytes and allocation, and the descriptor's size and pixel format. struct SurfaceRefusal {uint32_t step,format,planes,plane,pixelFormat;uint64_t offset,row,allocation,width,height;}; static SurfaceRefusal gSurfaceLastRefusal; +// Shader priming's progress (PrimeShaders.h, in the plugin), for the PR row. +extern "C" volatile uint32_t native_prime_compiling,native_prime_libraries_done,native_prime_libraries_total,native_prime_made; static void refuseSurface(uint32_t step,IOSurfaceRef surface,MTLTextureDescriptor *d,NSUInteger plane,uint64_t offset,uint64_t row) { gSurfaceRefusals++; SurfaceRefusal r={step,surface?IOSurfaceGetPixelFormat(surface):0,surface?(uint32_t)IOSurfaceGetPlaneCount(surface):0,(uint32_t)plane, @@ -1742,6 +1744,7 @@ extern "C" size_t native_bridge_metal_telemetry(char *out,size_t size) { { std::lock_guard lock(gProblemLock); char formats[160];native_bridge_metal_formats_locked(formats,sizeof formats);row("F %.86s",formats); + row("PR compiling=%u libs=%u/%u made=%u",native_prime_compiling,native_prime_libraries_done,native_prime_libraries_total,native_prime_made); const SurfaceRefusal &r=gSurfaceLastRefusal; if(r.step)row("SR %u %08x %c%c%c%c pl%u/%u o%llx rb%llu a%llx %llux%llu pf%u",r.step,r.format,(char)(r.format>>24),(char)(r.format>>16),(char)(r.format>>8), (char)r.format,r.plane,r.planes,(unsigned long long)r.offset,(unsigned long long)r.row,(unsigned long long)r.allocation,(unsigned long long)r.width, diff --git a/PrimeShaders.h b/PrimeShaders.h index cdf4f9b..1db80a0 100644 --- a/PrimeShaders.h +++ b/PrimeShaders.h @@ -30,6 +30,9 @@ extern "C" unsigned native_bridge_metal_recent_commits(uint64_t windowUs); - (id)nb_newFunctionWithZeroConstants:(NSString *)name; @end +// Priming's progress for the telemetry table (the PR row): 1 while a pipeline compiles, libraries done of the list, pipelines made. +extern "C" {volatile uint32_t native_prime_compiling, native_prime_libraries_done, native_prime_libraries_total, native_prime_made;} + namespace prime { static const char *const kRoots[] = {"/System/Library", "/Applications", "/System/Cryptexes/OS/System/Library"}; @@ -167,6 +170,7 @@ static std::string library(id device, const std::string &path, const if (type != MTLFunctionTypeKernel && type != MTLFunctionTypeVertex) continue; waited += waitForQuiet(); writeCurrent(current, key); + native_prime_compiling = 1; bool ok = false; @try { if (type == MTLFunctionTypeKernel) { @@ -200,7 +204,9 @@ static std::string library(id device, const std::string &path, const } @catch (NSException *exception) { ok = false; } + native_prime_compiling = 0; ok ? ++made : ++refused; + if (ok) native_prime_made = native_prime_made + 1; if (!ok && firstRefusal.empty()) { NSString *why = error.localizedDescription ?: @"no error"; firstRefusal = std::string(" first-refused=") + name.UTF8String + ": " + why.UTF8String; @@ -238,8 +244,10 @@ static void run(id device, std::string root, unsigned delaySeconds, c const std::vector paths = libraries(listPath, note); const std::string build = buildId(); appendLine(logPath, note + " build=" + build); + native_prime_libraries_total = (uint32_t)paths.size(); for (const std::string &path : paths) { ++libs; + native_prime_libraries_done = finished; struct stat st = {}; if (stat(path.c_str(), &st) != 0) continue; // The plugin's build ends the id: a newer one primes every library again (buildId()). @@ -250,6 +258,7 @@ static void run(id device, std::string root, unsigned delaySeconds, c done.insert(id); ++finished; } + native_prime_libraries_done = finished; char line[120]; snprintf(line, sizeof line, "pass libraries=%u finished=%u ms=%llu", libs, finished, (unsigned long long)(nowMs() - began)); appendLine(logPath, line); From 64321db8d6edab0385ccdd42c8d344a269516e97 Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Thu, 22 Oct 2026 19:02:32 +0800 Subject: [PATCH 11/92] feat(prime): link visible functions and zero every constant type Size zero constants for every scalar and vector type, accept a compute descriptor's linked and private functions, and retry a kernel that needs a linked function with the library's function of that name. Log each distinct reason a texture gets a shadow image to shadow.log. --- NativeBridgeMetalFrontend.mm | 26 +++++++++--- PrimeShaders.h | 41 ++++++++++++++++-- bridge/NativeMetalObjects.h | 3 ++ bridge/NativeMetalObjects.rs | 80 ++++++++++++++++++++++++++++++------ 4 files changed, 128 insertions(+), 22 deletions(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 385878c..ad7f7a4 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -1218,7 +1218,15 @@ static bool renderDescriptor(MTLRenderPipelineDescriptor *d,NmbRenderPipelineDes @end // Creation and archive population must use the same complete supported descriptor. -static bool computeDescriptor(MTLComputePipelineDescriptor *d,NmbComputePipelineDescriptor *p,NSError **error) { +// The handles of a linked-function list, false for one the frontend did not make. +static bool linkedHandles(NSArray> *functions,std::vector &out) { + for(id f in functions){if(![(id)f isKindOfClass:[NBFunction class]])return false;out.push_back([(NBFunction *)f handle]);} + return true; +} + +// The handles `p` points at live in `linked` and `privateLinked`, which outlive its use. +static bool computeDescriptor(MTLComputePipelineDescriptor *d,NmbComputePipelineDescriptor *p,std::vector &linkedStore, + std::vector &privateStore,NSError **error) { if(!d || ![(id)d.computeFunction isKindOfClass:[NBFunction class]]){if(error)*error=nbError(@"foreign compute function");return false;} // Metal may materialize empty stage/linked descriptors on access. Preserve the // default meaning while refusing state this frontend cannot yet represent. @@ -1231,10 +1239,14 @@ static bool computeDescriptor(MTLComputePipelineDescriptor *d,NmbComputePipeline } if(!empty){if(error)*error=nbError(@"compute stage inputs are not implemented");return false;} MTLLinkedFunctions *linked=d.linkedFunctions; - if(linked.functions.count || linked.binaryFunctions.count || linked.privateFunctions.count || linked.groups.count || - d.preloadedLibraries.count || d.insertLibraries.count) { - if(error)*error=nbError(@"compute linked/dynamic libraries are not implemented");return false; + if(linked.binaryFunctions.count || linked.groups.count || d.preloadedLibraries.count || d.insertLibraries.count) { + if(error)*error=nbError(@"compute binary/grouped linked functions and dynamic libraries are not implemented");return false; } + if(!linkedHandles(linked.functions,linkedStore) || !linkedHandles(linked.privateFunctions,privateStore)) { + if(error)*error=nbError(@"foreign linked function");return false; + } + p->linked=linkedStore.data();p->linked_count=linkedStore.size(); + p->private_linked=privateStore.data();p->private_linked_count=privateStore.size(); p->function=[(NBFunction *)d.computeFunction handle];p->max_threads=d.maxTotalThreadsPerThreadgroup; p->multiple_of_execution_width=d.threadGroupSizeIsMultipleOfThreadExecutionWidth;p->indirect=d.supportIndirectCommandBuffers; p->max_call_stack_depth=d.maxCallStackDepth;p->adding_binary_functions=d.supportAddingBinaryFunctions; @@ -1256,7 +1268,8 @@ static bool computeDescriptor(MTLComputePipelineDescriptor *d,NmbComputePipeline if(error)*error=nil;return YES; } - (BOOL)addComputePipelineFunctionsWithDescriptor:(MTLComputePipelineDescriptor *)d error:(NSError **)error { - NmbComputePipelineDescriptor p={};if(!computeDescriptor(d,&p,error))return NO; + NmbComputePipelineDescriptor p={};std::vector linked,privateLinked; + if(!computeDescriptor(d,&p,linked,privateLinked,error))return NO; if(!nmb_binary_archive_add_compute_descriptor(_handle,&p))return failWith(error,@"archive compute pipeline",NO); if(error)*error=nil;return YES; } @@ -1440,7 +1453,8 @@ static bool pipelineArchives(NSArray *list,std::vector &arch - (id)newComputePipelineStateWithDescriptor:(MTLComputePipelineDescriptor *)d options:(MTLPipelineOption)options reflection:(MTLAutoreleasedComputePipelineReflection *)reflection error:(NSError **)error { if(reflection)*reflection=nil; - NmbComputePipelineDescriptor p={};if(!computeDescriptor(d,&p,error))return nil; + NmbComputePipelineDescriptor p={};std::vector linked,privateLinked; + if(!computeDescriptor(d,&p,linked,privateLinked,error))return nil; std::vector archives; if(!pipelineArchives(d.binaryArchives,archives,error))return nil; const NmbPipelineOptions creation={(uint64_t)options,archives.data(),archives.size()}; diff --git a/PrimeShaders.h b/PrimeShaders.h index 1db80a0..05ee43d 100644 --- a/PrimeShaders.h +++ b/PrimeShaders.h @@ -149,16 +149,45 @@ static std::string buildId() { return "unknown"; } +// A kernel that calls visible functions it does not define (`NeedsInput(LinkedFunction { symbol: "..." })`): link the library's function of +// each symbol the refusals name and make the pipeline again, until it is made or a symbol names no function of the library. +static bool linkedKernel(id device, id lib, id kernel, NSError **error) { + NSMutableArray> *linked = [NSMutableArray array]; + NSMutableSet *symbols = [NSMutableSet set]; + for (unsigned round = 0; round < 16; ++round) { + NSString *why = (*error).localizedDescription; + NSRange at = [why rangeOfString:@"LinkedFunction { symbol: \""]; + if (at.location == NSNotFound) return false; + NSString *rest = [why substringFromIndex:NSMaxRange(at)]; + NSRange end = [rest rangeOfString:@"\""]; + if (end.location == NSNotFound) return false; + NSString *symbol = [rest substringToIndex:end.location]; + if ([symbols containsObject:symbol]) return false; + [symbols addObject:symbol]; + id callee = [lib newFunctionWithName:symbol]; + if (!callee) return false; + [linked addObject:callee]; + MTLComputePipelineDescriptor *d = [MTLComputePipelineDescriptor new]; + d.computeFunction = kernel; + MTLLinkedFunctions *functions = [MTLLinkedFunctions linkedFunctions]; + functions.functions = linked; + d.linkedFunctions = functions; + *error = nil; + if ([device newComputePipelineStateWithDescriptor:d options:MTLPipelineOptionNone reflection:nil error:error]) return true; + } + return false; +} + // One library: every function it names, in its order; returns the log line. static std::string library(id device, const std::string &path, const std::string ¤t, const std::set &bad) { - unsigned kernels = 0, renders = 0, made = 0, refused = 0, skipped = 0, zeroed = 0; + unsigned kernels = 0, renders = 0, made = 0, refused = 0, skipped = 0, zeroed = 0, linkedMade = 0; uint64_t waited = 0; std::string firstRefusal; // the first refused function and why, so the log tells translator gaps from unspecialized constants const uint64_t began = nowMs(); @autoreleasepool { NSError *error = nil; id lib = [device newLibraryWithURL:[NSURL fileURLWithPath:@(path.c_str())] error:&error]; - if (!lib) return path + " library refused"; + if (!lib) return path + " library refused: " + (error ? error.localizedDescription.UTF8String : "no error"); NSArray *names = lib.functionNames; for (NSString *name in names) { @autoreleasepool { @@ -183,6 +212,10 @@ static std::string library(id device, const std::string &path, const id zero = [(NSObject *)lib nb_newFunctionWithZeroConstants:name]; if (zero) {error = nil; ok = [device newComputePipelineStateWithFunction:zero error:&error] != nil; if (ok) ++zeroed;} } + if (!ok && [error.localizedDescription containsString:@"LinkedFunction"]) { + ok = linkedKernel(device, lib, function, &error); + if (ok) ++linkedMade; + } } else { ++renders; MTLRenderPipelineDescriptor *d = [MTLRenderPipelineDescriptor new]; @@ -221,8 +254,8 @@ static std::string library(id device, const std::string &path, const } unlink(current.c_str()); char line[180]; - snprintf(line, sizeof line, " kernels=%u renders=%u made=%u zeroed=%u refused=%u skipped=%u ms=%llu waited=%llu", kernels, renders, made, - zeroed, refused, skipped, (unsigned long long)(nowMs() - began), (unsigned long long)waited); + snprintf(line, sizeof line, " kernels=%u renders=%u made=%u zeroed=%u linked=%u refused=%u skipped=%u ms=%llu waited=%llu", kernels, renders, + made, zeroed, linkedMade, refused, skipped, (unsigned long long)(nowMs() - began), (unsigned long long)waited); return path + line + firstRefusal; } diff --git a/bridge/NativeMetalObjects.h b/bridge/NativeMetalObjects.h index 849524b..786c0b9 100644 --- a/bridge/NativeMetalObjects.h +++ b/bridge/NativeMetalObjects.h @@ -150,6 +150,9 @@ struct NmbComputePipelineDescriptor { NmbFunction *function; uint64_t max_threads, multiple_of_execution_width, indirect, max_call_stack_depth, adding_binary_functions; uint64_t mutability[31]; + /* linkedFunctions.functions and .privateFunctions: visible functions the kernel calls. */ + NmbFunction *const *linked; uint64_t linked_count; + NmbFunction *const *private_linked; uint64_t private_linked_count; }; NmbComputePipeline *nmb_compute_pipeline_new_with_options(NmbDevice *, NmbFunction *, const struct NmbPipelineOptions *); NmbComputePipeline *nmb_compute_pipeline_new_with_descriptor(NmbDevice *, const struct NmbComputePipelineDescriptor *, const struct NmbPipelineOptions *); diff --git a/bridge/NativeMetalObjects.rs b/bridge/NativeMetalObjects.rs index ee547fa..77f7b86 100644 --- a/bridge/NativeMetalObjects.rs +++ b/bridge/NativeMetalObjects.rs @@ -13,7 +13,7 @@ use metal2vulkan::metal::{ TextureUsage, TriangleFillMode, VertexFormat, VertexStepFunction, Winding, }; use metal2vulkan::pipeline::{ - ColorAttachmentDescriptor, ComputePipelineDescriptor, RenderPipelineDescriptor, StageSource, VertexAttributeDescriptor, VertexBufferLayoutDescriptor, + ColorAttachmentDescriptor, ComputePipelineDescriptor, LinkedFunction, LinkedFunctions, RenderPipelineDescriptor, StageSource, VertexAttributeDescriptor, VertexBufferLayoutDescriptor, VertexDescriptor, }; use metal2vulkan_runtime::{ @@ -164,6 +164,9 @@ pub unsafe extern "C" fn nmb_device_open(loader: *const c_char, cache: *const c_ options.metal_level = Some(MetalLevel::NEWEST); options.shader_cache = metal2vulkan_runtime::ShaderCacheOptions::in_directory(cache.to_string_lossy().into_owned()); options.messages = Some(MessageSink::new(observe_message)); + // Every texture given a shadow image is announced with why no image aliases its bytes (`shadow.log`). + options.announce_each.insert(metal2vulkan_runtime::Degradation::ShadowedTexture); + let _ = SHADOW_LOG.set(PathBuf::from(cache.to_string_lossy().into_owned()).join("shadow.log")); Ok(boxed(NmbDevice(Device::system_default_with(&options).map_err(e("device"))?, std::sync::OnceLock::new(), std::sync::Mutex::new(())))) }) } @@ -182,8 +185,32 @@ fn millis_after(text: &str, marker: &str) -> Option { rest[..end].parse().ok() } +// The distinct reasons textures were given shadow images, their numbers masked, each written once to the cache directory's shadow.log +// with the first such texture's message, at most SHADOW_REASONS of them. +static SHADOW_LOG: std::sync::OnceLock = std::sync::OnceLock::new(); +static SHADOW_SEEN: std::sync::Mutex> = std::sync::Mutex::new(Vec::new()); +const SHADOW_REASONS: usize = 64; + +fn note_shadow(text: &str) { + let masked: String = text.chars().map(|c| if c.is_ascii_digit() { '#' } else { c }).collect(); + let Ok(mut seen) = SHADOW_SEEN.lock() else { return }; + if seen.len() >= SHADOW_REASONS || seen.contains(&masked) { + return; + } + seen.push(masked); + if let Some(path) = SHADOW_LOG.get() { + use std::io::Write; + if let Ok(mut file) = std::fs::OpenOptions::new().create(true).append(true).open(path) { + let _ = writeln!(file, "{text}"); + } + } +} + fn observe_message(message: &DebugMessage) { use std::sync::atomic::Ordering::Relaxed; + if message.id_name == "shadowed-texture" { + note_shadow(&message.text); + } if message.id_name == "metal2vulkan-pipeline-creation" { let made = millis_after(&message.text, " made in "); let us = made.or_else(|| millis_after(&message.text, " failed in ")).map_or(0, |ms| (ms * 1000.0) as u64); @@ -1008,17 +1035,25 @@ pub unsafe extern "C" fn nmb_function_new_specialized(library: *mut NmbLibrary, }) } -/// Bytes of a function constant's `MTLDataType` (scalars and 2- and 4-wide vectors), 0 for a type that is not given a value. +/// Packed bytes of a function constant's `MTLDataType` (scalars and vectors), 0 for a type that is not given a value. fn constant_bytes(raw: u32) -> usize { - match raw { - 53 | 45 | 49 => 1, - 16 | 46 | 50 | 54 => 2, - 3 | 29 | 33 | 17 | 38 | 42 | 48 | 52 | 56 => 4, - 4 | 30 | 34 | 19 | 40 | 44 | 81 | 85 => 8, - 6 | 32 | 36 | 82 | 86 => 16, - 84 | 88 => 32, - _ => 0, - } + // MTLDataType numbers each scalar family's 1-, 2-, 3- and 4-lane types consecutively; a value is packed, lanes times the scalar. + let (first, scalar) = match raw { + 3..=6 => (3, 4), // float + 16..=19 => (16, 2), // half + 29..=32 => (29, 4), // int + 33..=36 => (33, 4), // uint + 37..=40 => (37, 2), // short + 41..=44 => (41, 2), // ushort + 45..=48 => (45, 1), // char + 49..=52 => (49, 1), // uchar + 53..=56 => (53, 1), // bool + 81..=84 => (81, 8), // long + 85..=88 => (85, 8), // ulong + 121..=124 => (121, 2), // bfloat + _ => return 0, + }; + (raw - first + 1) as usize * scalar } /// The library's function `name` with every function constant it declares set to zero (false for a bool), for shader priming: a @@ -1095,11 +1130,32 @@ pub struct NmbComputePipelineDescriptor { pub max_call_stack_depth: u64, pub adding_binary_functions: u64, pub mutability: [u64; 31], + pub linked: *const *mut NmbFunction, + pub linked_count: u64, + pub private_linked: *const *mut NmbFunction, + pub private_linked_count: u64, +} + +/// The linked functions of a counted array of live function handles. +fn linked_list(functions: *const *mut NmbFunction, count: u64) -> Result, String> { + if count == 0 { + return Ok(Vec::new()); + } + if functions.is_null() { + return Err("null linked functions".into()); + } + // SAFETY: the caller's contract: `count` live handles. + let handles = unsafe { std::slice::from_raw_parts(functions, usize::try_from(count).map_err(e("linked function count"))?) }; + handles.iter().map(|&function| Ok(get(function)?.0.linked_function())).collect() } fn compute_descriptor(descriptor: *const NmbComputePipelineDescriptor) -> Result { let d = get(descriptor)?; - let mut out = ComputePipelineDescriptor::new(StageSource::Function(get(d.function)?.0.function_descriptor())); + let mut linked = LinkedFunctions::new(); + linked.functions = linked_list(d.linked, d.linked_count)?; + linked.private_functions = linked_list(d.private_linked, d.private_linked_count)?; + let kernel = get(d.function)?.0.function_descriptor().with_linked(linked); + let mut out = ComputePipelineDescriptor::new(StageSource::Function(kernel)); out.max_total_threads_per_threadgroup = if d.max_threads == 0 { None } else { Some(u32::try_from(d.max_threads).map_err(e("compute max threads"))?) }; out.threadgroup_size_is_multiple_of_execution_width = d.multiple_of_execution_width != 0; out.support_indirect_command_buffers = d.indirect != 0; From 70441418f12d68173a0cd7f67d2d386ad66c763a Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Thu, 22 Oct 2026 19:18:14 +0800 Subject: [PATCH 12/92] perf(prime): pause at any animation and keep progress across plugin builds Treat more than 4 command buffers in 250 ms as animation and wait two quiet seconds before the next compile. Key finished libraries on the runtime's translator source and Turnip's build, which key the caches, instead of the plugin's own build. --- PrimeShaders.h | 60 ++++++++++++++++++++++-------------- bridge/NativeMetalObjects.h | 2 ++ bridge/NativeMetalObjects.rs | 7 +++++ 3 files changed, 46 insertions(+), 23 deletions(-) diff --git a/PrimeShaders.h b/PrimeShaders.h index 05ee43d..c662e29 100644 --- a/PrimeShaders.h +++ b/PrimeShaders.h @@ -12,6 +12,7 @@ #import #include #include +#include #include #include #include @@ -110,45 +111,58 @@ static std::vector libraries(const std::string &listPath, std::stri return found; } -// Waits until the render server is not animating: more than 12 command buffers in 250 ms (an idle desktop redraws at about 12 fps, a swipe -// commits 25 or more) means frames are being drawn, and a pipeline compile then competes with them for the CPU and the driver (int-244: -// priming during the scripted swipes raised the median frame interval from 19 to 30 ms). Returns the milliseconds it waited. +// Waits until the render server is not animating: more than 4 command buffers in 250 ms (16 fps; an idle desktop redraws at about 12 fps) +// means frames are being drawn, and a pipeline compile then competes with them for the CPU and the driver (int-244: priming during the +// scripted swipes raised the median frame interval from 19 to 30 ms). A higher bar let compiles in once they had slowed the frames under +// it (int-250: 12 per 250 ms, 460 pipelines made during the swipes at a 48 ms median). Returns the milliseconds it waited. static uint64_t waitForQuiet() { const uint64_t began = nowMs(); uint64_t quietSince = 0; bool busy = false; for (;;) { const uint64_t now = nowMs(); - if (native_bridge_metal_recent_commits(250000) > 12) {busy = true; quietSince = 0;} + if (native_bridge_metal_recent_commits(250000) > 4) {busy = true; quietSince = 0;} else if (!busy) return 0; else if (!quietSince) quietSince = now; - // After animation, a second without it before the next compile, so a pause between two swipes does not let one in. - else if (now - quietSince >= 1000) return now - began; + // After animation, two seconds without it before the next compile, so a pause between two swipes does not let one in. + else if (now - quietSince >= 2000) return now - began; usleep(100000); } } -// The plugin's own build (its Mach-O LC_UUID), which ends each finished library's id: a new runtime may no longer find the pipelines an -// earlier one cached (int-246: after a runtime change the Vulkan cache had 0 hits and 214 misses, while prime-done still listed every -// library), so a new build primes every library again; what is still cached then costs only a cache hit. -static std::string buildId() { - Dl_info info = {}; - if (!dladdr(reinterpret_cast(&buildId), &info) || !info.dli_fbase) return "unknown"; - const auto *header = static_cast(info.dli_fbase); - if (header->magic != MH_MAGIC_64) return "unknown"; - const auto *command = reinterpret_cast(header + 1); - for (uint32_t i = 0; i < header->ncmds; ++i) { - if (command->cmd == LC_UUID) { - const uint8_t *uuid = reinterpret_cast(command)->uuid; - char text[33]; - for (int k = 0; k < 16; ++k) snprintf(text + 2 * k, 3, "%02x", uuid[k]); - return text; +extern "C" const char *nmb_runtime_build(void); + +// The LC_UUID of the loaded image whose path ends in `suffix`, "unknown" without one. +static std::string imageUuid(const char *suffix) { + for (uint32_t index = 0; index < _dyld_image_count(); ++index) { + const char *name = _dyld_get_image_name(index); + const size_t length = strlen(name), want = strlen(suffix); + if (length < want || strcmp(name + length - want, suffix) != 0) continue; + const auto *header = reinterpret_cast(_dyld_get_image_header(index)); + if (!header || header->magic != MH_MAGIC_64) break; + const auto *command = reinterpret_cast(header + 1); + for (uint32_t i = 0; i < header->ncmds; ++i) { + if (command->cmd == LC_UUID) { + const uint8_t *uuid = reinterpret_cast(command)->uuid; + char text[33]; + for (int k = 0; k < 16; ++k) snprintf(text + 2 * k, 3, "%02x", uuid[k]); + return text; + } + command = reinterpret_cast(reinterpret_cast(command) + command->cmdsize); } - command = reinterpret_cast(reinterpret_cast(command) + command->cmdsize); + break; } return "unknown"; } +// What the cached pipelines were made by, which ends each finished library's id: the runtime's translator source, which keys the shader +// cache (int-246: after a runtime change the Vulkan cache had 0 hits and 214 misses, while prime-done still listed every library), and +// Turnip's build, which keys its pipeline cache (int-249: 4099 misses after a Turnip rebuild). A change of either primes every library +// again; a plugin rebuild alone keeps what was finished. +static std::string buildId() { + return std::string(nmb_runtime_build()) + "-" + imageUuid("/libvulkan_freedreno.dylib"); +} + // A kernel that calls visible functions it does not define (`NeedsInput(LinkedFunction { symbol: "..." })`): link the library's function of // each symbol the refusals name and make the pipeline again, until it is made or a symbol names no function of the library. static bool linkedKernel(id device, id lib, id kernel, NSError **error) { @@ -283,7 +297,7 @@ static void run(id device, std::string root, unsigned delaySeconds, c native_prime_libraries_done = finished; struct stat st = {}; if (stat(path.c_str(), &st) != 0) continue; - // The plugin's build ends the id: a newer one primes every library again (buildId()). + // The runtime's and Turnip's builds end the id: a newer one primes every library again (buildId()). const std::string id = path + "\t" + std::to_string((long long)st.st_size) + "\t" + std::to_string((long long)st.st_mtimespec.tv_sec) + "\t" + build; if (done.count(id)) {++finished; continue;} appendLine(logPath, library(device, path, current, bad)); diff --git a/bridge/NativeMetalObjects.h b/bridge/NativeMetalObjects.h index 786c0b9..ac9a390 100644 --- a/bridge/NativeMetalObjects.h +++ b/bridge/NativeMetalObjects.h @@ -39,6 +39,8 @@ size_t nmb_device_name(const NmbDevice *, char *out, size_t size); uint64_t nmb_device_max_buffer_length(const NmbDevice *); // Pipeline makings (count, total us, longest us, waited us, failed), sink messages (info, warning+), shader cache hits/misses/writes. bool nmb_device_work_stats(const NmbDevice *, uint64_t out[10]); +/* The translator source the runtime was built from, which keys the shader cache (build-native-metal-bridge.py). */ +const char *nmb_runtime_build(void); // Linear images made/refused/refused again, shadowed textures, shadow copies, texels copied back, linear passes in place/through copies. bool nmb_device_texture_counts(const NmbDevice *, uint64_t out[8]); /* Artifact lookups and driver pipeline records are separate cache layers. */ diff --git a/bridge/NativeMetalObjects.rs b/bridge/NativeMetalObjects.rs index 77f7b86..42f071b 100644 --- a/bridge/NativeMetalObjects.rs +++ b/bridge/NativeMetalObjects.rs @@ -233,6 +233,13 @@ fn observe_message(message: &DebugMessage) { } } +/// The translator source the runtime was built from (its pin and recorded patches, `build-native-metal-bridge.py`), which keys the shader +/// cache, as a C string. +#[unsafe(no_mangle)] +pub extern "C" fn nmb_runtime_build() -> *const c_char { + concat!(env!("NMB_RUNTIME_ID"), "\0").as_ptr().cast() +} + /// The sink's counts into the caller's ten words: pipeline makings (count, total us, longest us, waited us, failed), messages (info, /// warning or worse), and the device's shader cache hits, misses and writes. #[unsafe(no_mangle)] From 2aa5625810599809be8ba153b7d449b4a3b5a15e Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Thu, 22 Oct 2026 19:31:02 +0800 Subject: [PATCH 13/92] perf(gpu): alias padded rows of rendered and written textures Add a padded-rows policy that makes an image over a buffer whose rows lie apart for every texture, not only one that reads, and select it, so the window server's padded framebuffers stop being copied in and out of shadow images every pass. --- bridge/NativeMetalObjects.rs | 3 ++ ...lias-padded-rows-of-written-textures.patch | 29 +++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 bridge/patches/0008-alias-padded-rows-of-written-textures.patch diff --git a/bridge/NativeMetalObjects.rs b/bridge/NativeMetalObjects.rs index 42f071b..c98c14e 100644 --- a/bridge/NativeMetalObjects.rs +++ b/bridge/NativeMetalObjects.rs @@ -164,6 +164,9 @@ pub unsafe extern "C" fn nmb_device_open(loader: *const c_char, cache: *const c_ options.metal_level = Some(MetalLevel::NEWEST); options.shader_cache = metal2vulkan_runtime::ShaderCacheOptions::in_directory(cache.to_string_lossy().into_owned()); options.messages = Some(MessageSink::new(observe_message)); + // A texture over a buffer whose rows lie apart is an image over the bytes, rendered or written too: the window server's padded + // framebuffers (3048x2032 BGRA8 at 12288-byte rows, int-251) were otherwise copied in and out of a shadow image every pass. + options.policy.padded_reads = metal2vulkan::device::PaddedReadsPolicy::AliasWrites; // Every texture given a shadow image is announced with why no image aliases its bytes (`shadow.log`). options.announce_each.insert(metal2vulkan_runtime::Degradation::ShadowedTexture); let _ = SHADOW_LOG.set(PathBuf::from(cache.to_string_lossy().into_owned()).join("shadow.log")); diff --git a/bridge/patches/0008-alias-padded-rows-of-written-textures.patch b/bridge/patches/0008-alias-padded-rows-of-written-textures.patch new file mode 100644 index 0000000..9cf361c --- /dev/null +++ b/bridge/patches/0008-alias-padded-rows-of-written-textures.patch @@ -0,0 +1,29 @@ +diff --git a/crates/metal2vulkan-runtime/src/linear_plan.rs b/crates/metal2vulkan-runtime/src/linear_plan.rs +index a0a2cbc..fdb8233 100644 +--- a/crates/metal2vulkan-runtime/src/linear_plan.rs ++++ b/crates/metal2vulkan-runtime/src/linear_plan.rs +@@ -819,7 +819,7 @@ pub(super) fn holding( + return Ok(Holds::Texels); + }; + let reads = only_reads(usage); +- if reads && policy == PaddedReadsPolicy::Alias { ++ if policy == PaddedReadsPolicy::AliasWrites || (reads && policy == PaddedReadsPolicy::Alias) { + return Ok(Holds::Padding); + } + let writers = if usage.contains(TextureUsage::RENDER_TARGET) { +diff --git a/crates/metal2vulkan/src/device/policy.rs b/crates/metal2vulkan/src/device/policy.rs +index 29b0667..7b4fe50 100644 +--- a/crates/metal2vulkan/src/device/policy.rs ++++ b/crates/metal2vulkan/src/device/policy.rs +@@ -359,6 +359,11 @@ pub enum PaddedReadsPolicy { + /// the validation lane checks per driver (docs/FAST-PATHS.md, its known gaps). + #[default] + Alias, ++ /// [`PaddedReadsPolicy::Alias`] for every texture, a render target and a texture a shader ++ /// writes too: the driver's layout transitions and a render pass's load and store operations ++ /// may then write the bytes between the rows, which a program that keeps nothing there (a ++ /// window server's padded framebuffers) never reads. No copy in or out of a shadow image. ++ AliasWrites, + /// The runtime's shadow image of the texels, copied in from the buffer before each use: the + /// copies address the texels alone, so no byte between the rows is ever read or written + /// through an image. From b79a99dbc2e0e79849c8921a136bb9d5443a8359 Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Thu, 22 Oct 2026 21:54:39 +0800 Subject: [PATCH 14/92] feat(plugin): report a client process's first Metal problem --- NativeAdrenoMetalPlugin.mm | 12 ++++++++++++ NativeBridgeMetalFrontend.h | 2 ++ NativeBridgeMetalFrontend.mm | 8 ++++++++ 3 files changed, 22 insertions(+) diff --git a/NativeAdrenoMetalPlugin.mm b/NativeAdrenoMetalPlugin.mm index 1bb0c82..a4973a1 100644 --- a/NativeAdrenoMetalPlugin.mm +++ b/NativeAdrenoMetalPlugin.mm @@ -194,6 +194,18 @@ static id clientDevice(unsigned port) { if (!device && !lastError[0]) lastError = nmb_last_error(); } if (!device) clientFailure(port, usable ? "no-device" : "no-cachedir", lastError); + // The client's first problem, once, on the same text line: WidgetRenderer crashed on a nil render encoder with no reason anywhere readable + // (its own log lines need the plugin's format strings, which the device does not keep). Checked every second for ten minutes. + if (device) { + dispatch_async(dispatch_get_global_queue(QOS_CLASS_UTILITY, 0), ^{ + for (unsigned second = 0; second < 600; ++second) { + char problem[200] = {}; + const uint64_t count = native_bridge_metal_first_problem(problem, sizeof problem); + if (count) {clientFailure(port, "problem", problem); return;} + sleep(1); + } + }); + } return device; } static id deviceForPort(unsigned port) { diff --git a/NativeBridgeMetalFrontend.h b/NativeBridgeMetalFrontend.h index 5c0c7ca..d7dabfc 100644 --- a/NativeBridgeMetalFrontend.h +++ b/NativeBridgeMetalFrontend.h @@ -15,6 +15,8 @@ id native_bridge_metal_library_with_air(const void *bytes, size_t length); 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); size_t native_bridge_metal_telemetry(char *out, size_t size); // Structured cumulative artifact/driver-cache counters for offline qualification. struct NmbShaderCacheStats; diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index ad7f7a4..d275298 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -63,9 +63,12 @@ extern "C" unsigned native_bridge_metal_recent_commits(uint64_t windowUs) { } static std::mutex gProblemLock; static char gLastProblems[2][200]; +// The process's first problem, kept: a client process has no telemetry table, and its first refusal is usually the cause of the rest. +static char gFirstProblem[200]; static void noteProblem(NSString *what) { gProblems++; std::lock_guard lock(gProblemLock); + if(!gFirstProblem[0])strlcpy(gFirstProblem,what.UTF8String?:"?",sizeof gFirstProblem); memcpy(gLastProblems[1],gLastProblems[0],sizeof gLastProblems[0]); strlcpy(gLastProblems[0],what.UTF8String?:"?",sizeof gLastProblems[0]); } @@ -1781,6 +1784,11 @@ extern "C" size_t native_bridge_metal_telemetry(char *out,size_t size) { return at; } +extern "C" uint64_t native_bridge_metal_first_problem(char *out,size_t size) { + std::lock_guard lock(gProblemLock); + if(size)strlcpy(out,gFirstProblem,size); + return gProblems.load(); +} extern "C" size_t native_bridge_metal_formats(char *out,size_t size) { std::lock_guard lock(gProblemLock); return native_bridge_metal_formats_locked(out,size); From 26517a019f55f52a48a460aa28e936be576a79e3 Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Thu, 22 Oct 2026 22:17:07 +0800 Subject: [PATCH 15/92] fix(plugin): render every Metal process's passes in system memory --- NativeAdrenoMetalPlugin.mm | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/NativeAdrenoMetalPlugin.mm b/NativeAdrenoMetalPlugin.mm index a4973a1..c27e260 100644 --- a/NativeAdrenoMetalPlugin.mm +++ b/NativeAdrenoMetalPlugin.mm @@ -215,6 +215,10 @@ static id deviceForPort(unsigned port) { if (tried) return device; tried = true; exceptiondiag::start(); // the throw site of an uncaught exception, for the kernel's THROW row (ExceptionDiag.h) + // Every Metal process renders in system memory: Turnip's GMEM passes hand work between the BV and BR threads, and with IFPC off + // and a context reset on every client switch the two still stopped waiting on each other inside a pass (int-261p, int-262: GPU idle, + // BR one IB ahead, BV inside a draw IB2). int-257's nobin covered the render server only. + setenv("TU_DEBUG", "sysmem", 0); if (strcmp(getprogname(), "backboardd") != 0) return device = clientDevice(port); // The discovery releases the accelerator port when it is done; the monitor and the scanout client keep using it. if (void *iokit = dlopen("/System/Library/Frameworks/IOKit.framework/IOKit", RTLD_NOW | RTLD_LOCAL)) { From 53d6458f3bc9f0914dbbc324c377ad3d161161d5 Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Thu, 22 Oct 2026 22:24:22 +0800 Subject: [PATCH 16/92] fix(plugin): skip the creation-time device answers when reporting a client's first problem --- NativeAdrenoMetalPlugin.mm | 2 +- NativeBridgeMetalFrontend.mm | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/NativeAdrenoMetalPlugin.mm b/NativeAdrenoMetalPlugin.mm index c27e260..2b219e3 100644 --- a/NativeAdrenoMetalPlugin.mm +++ b/NativeAdrenoMetalPlugin.mm @@ -201,7 +201,7 @@ static id clientDevice(unsigned port) { for (unsigned second = 0; second < 600; ++second) { char problem[200] = {}; const uint64_t count = native_bridge_metal_first_problem(problem, sizeof problem); - if (count) {clientFailure(port, "problem", problem); return;} + if (count && problem[0]) {clientFailure(port, "problem", problem); return;} sleep(1); } }); diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index d275298..b7004e8 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -68,7 +68,8 @@ static char gFirstProblem[200]; static void noteProblem(NSString *what) { gProblems++; std::lock_guard lock(gProblemLock); - if(!gFirstProblem[0])strlcpy(gFirstProblem,what.UTF8String?:"?",sizeof gFirstProblem); + // A device answer the runtime lacks is reported at creation in every process and is harmless: the first problem is the next one. + if(!gFirstProblem[0] && ![what hasPrefix:@"native-metal: no device answer"])strlcpy(gFirstProblem,what.UTF8String?:"?",sizeof gFirstProblem); memcpy(gLastProblems[1],gLastProblems[0],sizeof gLastProblems[0]); strlcpy(gLastProblems[0],what.UTF8String?:"?",sizeof gLastProblems[0]); } From 00d363bdb5f2f2e7c3d71f169559f76c9d20b769 Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Thu, 22 Oct 2026 22:53:24 +0800 Subject: [PATCH 17/92] fix(plugin): disable concurrent binning in every Metal process --- NativeAdrenoMetalPlugin.mm | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/NativeAdrenoMetalPlugin.mm b/NativeAdrenoMetalPlugin.mm index 2b219e3..bd48dc6 100644 --- a/NativeAdrenoMetalPlugin.mm +++ b/NativeAdrenoMetalPlugin.mm @@ -217,8 +217,10 @@ static id deviceForPort(unsigned port) { exceptiondiag::start(); // the throw site of an uncaught exception, for the kernel's THROW row (ExceptionDiag.h) // Every Metal process renders in system memory: Turnip's GMEM passes hand work between the BV and BR threads, and with IFPC off // and a context reset on every client switch the two still stopped waiting on each other inside a pass (int-261p, int-262: GPU idle, - // BR one IB ahead, BV inside a draw IB2). int-257's nobin covered the render server only. - setenv("TU_DEBUG", "sysmem", 0); + // BR one IB ahead, BV inside a draw IB2). int-257's nobin covered the render server only. System memory alone still let BV run + // ahead of BR across passes (concurrent binning is emitted for sysmem passes too) and it hung the same way in int-265: no + // concurrent binning either. + setenv("TU_DEBUG", "sysmem,nocb", 0); if (strcmp(getprogname(), "backboardd") != 0) return device = clientDevice(port); // The discovery releases the accelerator port when it is done; the monitor and the scanout client keep using it. if (void *iokit = dlopen("/System/Library/Frameworks/IOKit.framework/IOKit", RTLD_NOW | RTLD_LOCAL)) { From 8f36c26d668f5e84f1a04e5707b996fd4153e910 Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Thu, 22 Oct 2026 22:53:24 +0800 Subject: [PATCH 18/92] perf(present): reuse a surface's GPU import while the kernel keeps its mapping --- GpuScanoutClient.h | 2 +- GpuScanoutPresenter.h | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/GpuScanoutClient.h b/GpuScanoutClient.h index 6ffcb6d..668232e 100644 --- a/GpuScanoutClient.h +++ b/GpuScanoutClient.h @@ -81,7 +81,7 @@ public: note(0x6730); // a frame was offered const uint64_t began = mach_absolute_time(); const gpupresent::Result r = presenter_->present({(const void *)(uintptr_t)swap.address, (size_t)((swap.bytes + page - 1) & ~(page - 1)), - (size_t)swap.stride, swap.width, swap.height}); + (size_t)swap.stride, swap.width, swap.height, swap.reserved}); { static mach_timebase_info_data_t base; if (!base.denom) mach_timebase_info(&base); diff --git a/GpuScanoutPresenter.h b/GpuScanoutPresenter.h index 9a9e938..9048132 100644 --- a/GpuScanoutPresenter.h +++ b/GpuScanoutPresenter.h @@ -23,6 +23,7 @@ struct Frame { size_t bytes; // allocation size, a multiple of the page size size_t stride; uint32_t width, height; + uint32_t mapping; // the kernel's id for this mapping of the surface (0: none, imported for this frame only) }; enum Result { kOk = 0, kBadFrame, kBadScanout, kNoResource, kGpuFailed }; @@ -42,7 +43,7 @@ public: if (!frame.base || ((uintptr_t)frame.base & (page - 1)) || !frame.bytes || (frame.bytes & (page - 1)) || frame.width != scanout_.width || frame.height != scanout_.height || frame.stride < row || (size_t)frame.height > frame.bytes / frame.stride || frame.stride * (frame.height - 1) + row > frame.bytes) return kBadFrame; - id source = [device_ newBufferWithBytesNoCopy:(void *)frame.base length:frame.bytes options:MTLResourceStorageModeShared deallocator:nil]; + id source = sourceFor(frame); if (!source) return kNoResource; id commands = [queue_ commandBuffer]; id blit = commands ? [commands blitCommandEncoder] : nil; @@ -73,6 +74,24 @@ public: return commands.status == MTLCommandBufferStatusCompleted ? kOk : kGpuFailed; } private: + // The compositor cycles through a few surfaces and the kernel keeps each one's mapping under one id, so the GPU import (pinning and + // mapping the pages, milliseconds per frame otherwise) is made once per surface. The kernel keeps more mappings (6) than kSources, so + // every import cached here still has its mapping. + static constexpr unsigned kSources = 4; + struct Source { uint32_t mapping; const void *base; size_t bytes; uint64_t used; id buffer; }; + id sourceFor(const Frame &frame) { + if (!frame.mapping) + return [device_ newBufferWithBytesNoCopy:(void *)frame.base length:frame.bytes options:MTLResourceStorageModeShared deallocator:nil]; + unsigned victim = 0; + for (unsigned i = 0; i < kSources; ++i) { + Source &s = sources_[i]; + if (s.buffer && s.mapping == frame.mapping && s.base == frame.base && s.bytes == frame.bytes) {s.used = ++uses_; return s.buffer;} + if (!sources_[i].buffer || sources_[i].used < sources_[victim].used) victim = i; + } + id buffer = [device_ newBufferWithBytesNoCopy:(void *)frame.base length:frame.bytes options:MTLResourceStorageModeShared deallocator:nil]; + if (buffer) sources_[victim] = {frame.mapping, frame.base, frame.bytes, ++uses_, buffer}; + return buffer; + } bool validScanout() const { const size_t page = (size_t)getpagesize(); return scanout_.base && !((uintptr_t)scanout_.base & (page - 1)) && scanout_.bytes && !(scanout_.bytes & (page - 1)) && @@ -83,6 +102,8 @@ private: Scanout scanout_; id queue_; id target_; + Source sources_[kSources] = {}; + uint64_t uses_ = 0; }; } // namespace gpupresent From 57d3cb19dbda030f7c46e5269c525ce930faa69b Mon Sep 17 00:00:00 2001 From: rpone Date: Fri, 23 Oct 2026 04:33:30 +0800 Subject: [PATCH 19/92] fix(plugin): render without hardware binning Every Metal process now starts Turnip with TU_DEBUG=nobin,nocb. A binned pass hands its visibility work to the BV thread, and with BV and BR waiting on each other inside a pass the GPU stalled and the display froze. Without binning a pass stays on BR and renders in tile memory; over two 7 minute runs of scripted swipes no submission stalled. --- NativeAdrenoMetalPlugin.mm | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/NativeAdrenoMetalPlugin.mm b/NativeAdrenoMetalPlugin.mm index 73d6eb0..6d63da8 100644 --- a/NativeAdrenoMetalPlugin.mm +++ b/NativeAdrenoMetalPlugin.mm @@ -203,12 +203,12 @@ static id deviceForPort(unsigned port) { if (tried) return device; tried = true; exceptiondiag::start(); // the throw site of an uncaught exception, for the kernel's THROW row (ExceptionDiag.h) - // Every Metal process renders in system memory: Turnip's GMEM passes hand work between the BV and BR threads, and with IFPC off - // and a context reset on every client switch the two still stopped waiting on each other inside a pass (int-261p, int-262: GPU idle, - // BR one IB ahead, BV inside a draw IB2). int-257's nobin covered the render server only. System memory alone still let BV run - // ahead of BR across passes (concurrent binning is emitted for sysmem passes too) and it hung the same way in int-265: no - // concurrent binning either. - setenv("TU_DEBUG", "sysmem,nocb", 0); + // Every Metal process renders without hardware binning: a binned GMEM pass hands its visibility pass to the BV thread, and with + // IFPC off and a context reset on every client switch BV and BR still stopped waiting on each other inside a pass (int-261p, + // int-262: GPU idle, BR one IB ahead, BV inside a draw IB2). Without binning a GMEM pass stays on BR (CONCURRENT_BIN_DISABLE), + // while its blending stays in GMEM: the system-memory passes this replaces were bandwidth bound (about 12 ms of GPU time for a + // 37 Mpx home screen frame). Concurrent binning stays off as well. + setenv("TU_DEBUG", "nobin,nocb", 0); if (strcmp(getprogname(), "backboardd") != 0) return device = clientDevice(port); // The discovery releases the accelerator port when it is done; the monitor and the scanout client keep using it. if (void *iokit = dlopen("/System/Library/Frameworks/IOKit.framework/IOKit", RTLD_NOW | RTLD_LOCAL)) { From 784edef0fd6f79375def8836cf7596396ebbbea8 Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Sun, 25 Oct 2026 12:24:50 +0800 Subject: [PATCH 20/92] fix(prime): let priming run on the idle desktop With a fixed 120 Hz vsync the idle desktop submits 6 command buffers per 250 ms, above the old bar of 4, so priming never compiled a pipeline. The bar is now 8, any animation still draws 15 or more, and a wait ends after 10 s. The PR row also shows the wait and library stages and the highest submit rate a wait saw. --- NativeBridgeMetalFrontend.mm | 4 ++-- PrimeShaders.h | 20 ++++++++++++++++---- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index b7004e8..626e01c 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -83,7 +83,7 @@ static std::atomic gSurfaceTextures,gSurfaceRefusals,gSurfacePlanar; struct SurfaceRefusal {uint32_t step,format,planes,plane,pixelFormat;uint64_t offset,row,allocation,width,height;}; static SurfaceRefusal gSurfaceLastRefusal; // Shader priming's progress (PrimeShaders.h, in the plugin), for the PR row. -extern "C" volatile uint32_t native_prime_compiling,native_prime_libraries_done,native_prime_libraries_total,native_prime_made; +extern "C" volatile uint32_t native_prime_compiling,native_prime_libraries_done,native_prime_libraries_total,native_prime_made,native_prime_rate; static void refuseSurface(uint32_t step,IOSurfaceRef surface,MTLTextureDescriptor *d,NSUInteger plane,uint64_t offset,uint64_t row) { gSurfaceRefusals++; SurfaceRefusal r={step,surface?IOSurfaceGetPixelFormat(surface):0,surface?(uint32_t)IOSurfaceGetPlaneCount(surface):0,(uint32_t)plane, @@ -1762,7 +1762,7 @@ extern "C" size_t native_bridge_metal_telemetry(char *out,size_t size) { { std::lock_guard lock(gProblemLock); char formats[160];native_bridge_metal_formats_locked(formats,sizeof formats);row("F %.86s",formats); - row("PR compiling=%u libs=%u/%u made=%u",native_prime_compiling,native_prime_libraries_done,native_prime_libraries_total,native_prime_made); + row("PR compiling=%u libs=%u/%u made=%u rate=%u",native_prime_compiling,native_prime_libraries_done,native_prime_libraries_total,native_prime_made,native_prime_rate); const SurfaceRefusal &r=gSurfaceLastRefusal; if(r.step)row("SR %u %08x %c%c%c%c pl%u/%u o%llx rb%llu a%llx %llux%llu pf%u",r.step,r.format,(char)(r.format>>24),(char)(r.format>>16),(char)(r.format>>8), (char)r.format,r.plane,r.planes,(unsigned long long)r.offset,(unsigned long long)r.row,(unsigned long long)r.allocation,(unsigned long long)r.width, diff --git a/PrimeShaders.h b/PrimeShaders.h index c662e29..b751717 100644 --- a/PrimeShaders.h +++ b/PrimeShaders.h @@ -31,8 +31,10 @@ extern "C" unsigned native_bridge_metal_recent_commits(uint64_t windowUs); - (id)nb_newFunctionWithZeroConstants:(NSString *)name; @end -// Priming's progress for the telemetry table (the PR row): 1 while a pipeline compiles, libraries done of the list, pipelines made. -extern "C" {volatile uint32_t native_prime_compiling, native_prime_libraries_done, native_prime_libraries_total, native_prime_made;} +// Priming's progress for the telemetry table (the PR row): what the thread does (1 a pipeline compiles, 2 it waits for the desktop to +// settle, 3 it opens a library and its functions), libraries done of the list, pipelines made, and the most command buffers the last wait +// saw in 250 ms. +extern "C" {volatile uint32_t native_prime_compiling, native_prime_libraries_done, native_prime_libraries_total, native_prime_made, native_prime_rate;} namespace prime { static const char *const kRoots[] = {"/System/Library", "/Applications", "/System/Cryptexes/OS/System/Library"}; @@ -114,14 +116,20 @@ static std::vector libraries(const std::string &listPath, std::stri // Waits until the render server is not animating: more than 4 command buffers in 250 ms (16 fps; an idle desktop redraws at about 12 fps) // means frames are being drawn, and a pipeline compile then competes with them for the CPU and the driver (int-244: priming during the // scripted swipes raised the median frame interval from 19 to 30 ms). A higher bar let compiles in once they had slowed the frames under -// it (int-250: 12 per 250 ms, 460 pipelines made during the swipes at a 48 ms median). Returns the milliseconds it waited. +// it (int-250: 12 per 250 ms, 460 pipelines made during the swipes at a 48 ms median). With a fixed 120 Hz vsync the idle desktop draws 6 +// per 250 ms (i516), so the bar is 8: still under any animation, which draws 15 to 30. A desktop that never drops under it still gets one +// pipeline every 10 s (i513-i515: priming sat at 15/162 for the whole run without making a pipeline). Returns the milliseconds it waited. static uint64_t waitForQuiet() { const uint64_t began = nowMs(); uint64_t quietSince = 0; bool busy = false; + native_prime_rate = 0; for (;;) { const uint64_t now = nowMs(); - if (native_bridge_metal_recent_commits(250000) > 4) {busy = true; quietSince = 0;} + const unsigned commits = native_bridge_metal_recent_commits(250000); + if (commits > native_prime_rate) native_prime_rate = commits; + if (now - began >= 10000) return now - began; + if (commits > 8) {busy = true; quietSince = 0; native_prime_compiling = 2;} else if (!busy) return 0; else if (!quietSince) quietSince = now; // After animation, two seconds without it before the next compile, so a pause between two swipes does not let one in. @@ -200,12 +208,15 @@ static std::string library(id device, const std::string &path, const const uint64_t began = nowMs(); @autoreleasepool { NSError *error = nil; + native_prime_compiling = 3; id lib = [device newLibraryWithURL:[NSURL fileURLWithPath:@(path.c_str())] error:&error]; + native_prime_compiling = 0; if (!lib) return path + " library refused: " + (error ? error.localizedDescription.UTF8String : "no error"); NSArray *names = lib.functionNames; for (NSString *name in names) { @autoreleasepool { const std::string key = path + "\t" + name.UTF8String; + native_prime_compiling = 3; if (bad.count(key)) {++skipped; continue;} id function = [lib newFunctionWithName:name]; if (!function) {++refused; continue;} @@ -306,6 +317,7 @@ static void run(id device, std::string root, unsigned delaySeconds, c ++finished; } native_prime_libraries_done = finished; + native_prime_compiling = 0; char line[120]; snprintf(line, sizeof line, "pass libraries=%u finished=%u ms=%llu", libs, finished, (unsigned long long)(nowMs() - began)); appendLine(logPath, line); From b4073213f02ac8a762166bfa6c147ce65b2f099a Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Sun, 25 Oct 2026 23:01:50 +0800 Subject: [PATCH 21/92] chore(diagnostics): trace render frames and command buffer waits Measurement code used by the current images: a per-process client list, a frame trace file read by the diagnostic export, a render thread sampler (off by default), waitUntilCompleted callers and a half-copy scanout measurement. Not verified as a fix. --- GpuScanoutClient.h | 13 ++ GpuScanoutPresenter.h | 11 +- NativeAdrenoMetalPlugin.mm | 9 +- NativeBridgeMetalFrontend.mm | 273 ++++++++++++++++++++++++++++++++++- PrimeShaders.h | 20 ++- 5 files changed, 312 insertions(+), 14 deletions(-) diff --git a/GpuScanoutClient.h b/GpuScanoutClient.h index 668232e..d40b0bb 100644 --- a/GpuScanoutClient.h +++ b/GpuScanoutClient.h @@ -12,6 +12,9 @@ #include "GpuScanoutPresenter.h" #include "../NativeMobileDisplay/NativeGOPWire.h" +extern "C" void NBWaitInFlight(uint64_t beforeUs) __attribute__((weak_import)); +extern "C" void NBTraceSwap(uint64_t offered,uint64_t copied,uint64_t completed,uint32_t sequence,uint32_t result) __attribute__((weak_import)); + namespace gpuscanout { struct Transport { @@ -78,7 +81,15 @@ public: return false; } const size_t page = (size_t)getpagesize(); + { // measurement: the second half of the run copies half the rows + static uint64_t first = 0; + const uint64_t nowS = clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000000000ull; + if (!first) first = nowS; + (void)first; gpupresent::gHalfRows.store(false, std::memory_order_relaxed); + } note(0x6730); // a frame was offered + const uint64_t offeredUs = clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000; + if (NBWaitInFlight) NBWaitInFlight(offeredUs); const uint64_t began = mach_absolute_time(); const gpupresent::Result r = presenter_->present({(const void *)(uintptr_t)swap.address, (size_t)((swap.bytes + page - 1) & ~(page - 1)), (size_t)swap.stride, swap.width, swap.height, swap.reserved}); @@ -91,8 +102,10 @@ public: stats_->lastResult = (int)r; if (r == gpupresent::kOk && stats_->verified < 4) verify(swap); note(0x6731); // the GPU copy returned + const uint64_t copiedUs = clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000; NGPComplete done = {swap.sequence, r == gpupresent::kOk ? 0u : 0xe00002bcu /* kIOReturnError */, 0}; const int completed = transport_.call(transport_.context, NGP_METHOD_COMPLETE_SWAP, &done, sizeof done, nullptr, 0); + if (NBTraceSwap) NBTraceSwap(offeredUs, copiedUs, clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000, (uint32_t)swap.sequence, (uint32_t)r); if (completed) {++stats_->failures; stats_->error = completed; return false;} if (r == gpupresent::kOk) ++stats_->frames; else ++stats_->failures; return true; diff --git a/GpuScanoutPresenter.h b/GpuScanoutPresenter.h index 9048132..945301c 100644 --- a/GpuScanoutPresenter.h +++ b/GpuScanoutPresenter.h @@ -8,9 +8,13 @@ #include #include #include +#include namespace gpupresent { +// Measurement only: when set, the copy covers the upper half of the rows (the GPU cost of the copy is what is being measured). +inline std::atomic gHalfRows{false}; + struct Scanout { void *base; // writable mapping of the firmware framebuffer (page aligned) size_t bytes; // whole extent, a multiple of the page size @@ -43,6 +47,7 @@ public: if (!frame.base || ((uintptr_t)frame.base & (page - 1)) || !frame.bytes || (frame.bytes & (page - 1)) || frame.width != scanout_.width || frame.height != scanout_.height || frame.stride < row || (size_t)frame.height > frame.bytes / frame.stride || frame.stride * (frame.height - 1) + row > frame.bytes) return kBadFrame; + const uint32_t rows = gHalfRows.load(std::memory_order_relaxed) ? frame.height / 2 : frame.height; id source = sourceFor(frame); if (!source) return kNoResource; id commands = [queue_ commandBuffer]; @@ -50,7 +55,7 @@ public: if (!blit) return kNoResource; if (frame.stride == scanout_.stride) { // Equal pitch: the rows are one contiguous run. - [blit copyFromBuffer:source sourceOffset:0 toBuffer:target_ destinationOffset:0 size:frame.stride * (frame.height - 1) + row]; + [blit copyFromBuffer:source sourceOffset:0 toBuffer:target_ destinationOffset:0 size:frame.stride * (rows - 1) + row]; } else { // A linear texture over the surface pages reads them with the surface's pitch and writes the scanout's pitch. MTLTextureDescriptor *d = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm width:frame.width @@ -58,13 +63,13 @@ public: d.storageMode = MTLStorageModeShared; d.usage = MTLTextureUsageShaderRead; if (frame.stride % [device_ minimumLinearTextureAlignmentForPixelFormat:MTLPixelFormatBGRA8Unorm]) { - for (uint32_t y = 0; y < frame.height; ++y) + for (uint32_t y = 0; y < rows; ++y) [blit copyFromBuffer:source sourceOffset:y * frame.stride toBuffer:target_ destinationOffset:y * scanout_.stride size:row]; } else { id texture = [source newTextureWithDescriptor:d offset:0 bytesPerRow:frame.stride]; if (!texture) {[blit endEncoding]; return kNoResource;} [blit copyFromTexture:texture sourceSlice:0 sourceLevel:0 sourceOrigin:MTLOriginMake(0, 0, 0) - sourceSize:MTLSizeMake(frame.width, frame.height, 1) toBuffer:target_ destinationOffset:0 + sourceSize:MTLSizeMake(frame.width, rows, 1) toBuffer:target_ destinationOffset:0 destinationBytesPerRow:scanout_.stride destinationBytesPerImage:scanout_.stride * frame.height]; } } diff --git a/NativeAdrenoMetalPlugin.mm b/NativeAdrenoMetalPlugin.mm index bd48dc6..00134f3 100644 --- a/NativeAdrenoMetalPlugin.mm +++ b/NativeAdrenoMetalPlugin.mm @@ -136,7 +136,9 @@ static void reportDisplayState(Gpu &gpu) { markSample(gpu, now, 0xA000); } @catch (NSException *) {} } -// Whether NativeAdrenoMetal.clients names this process (names separated by blanks, commas or new lines; "*" names every process). +// Whether NativeAdrenoMetal.clients names this process (names separated by blanks, commas or new lines; "*" names every process; a +// name that ends in "*" names every process whose name starts with it, so "com.apple.WebKit.*" covers the WebContent, Networking +// and GPU service processes). static bool listedClient() { char path[1024]; if (!requestedPath("NativeAdrenoMetal.clients", path, sizeof path)) return false; @@ -147,8 +149,11 @@ static bool listedClient() { fclose(file); text[length] = 0; const char *self = getprogname(); - for (char *save = nullptr, *name = strtok_r(text, " ,\t\r\n", &save); name; name = strtok_r(nullptr, " ,\t\r\n", &save)) + for (char *save = nullptr, *name = strtok_r(text, " ,\t\r\n", &save); name; name = strtok_r(nullptr, " ,\t\r\n", &save)) { if (!strcmp(name, "*") || !strcmp(name, self)) return true; + const size_t nameLength = strlen(name); + if (nameLength && name[nameLength - 1] == '*' && !strncmp(name, self, nameLength - 1)) return true; + } return false; } // A listed process other than the render server: the same runtime on its own connection, with none of the render server's threads (no diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index b7004e8..ba62e1e 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -22,6 +22,9 @@ #include #include #include +#include +#include +#include #include "bridge/NativeMetalObjects.h" #include "NativeBridgeMetalFrontend.h" @@ -53,6 +56,195 @@ static std::atomic gSurfaceFlushBytes,gSurfaceFlushDuplicates; struct EncodedWork {uint32_t passes,draws,dispatches,blits;uint64_t pixels;}; struct CommitRecord {uint64_t committedUs=0,callbackUs=0,ageUs=0;int64_t scheduledUs=-1;uint32_t index=0;EncodedWork work={};}; static CommitRecord gCommitRing[32]; +// Where the time between two render commits goes, by how long that interval was: [0] under 11 ms, [1] to 16.7, [2] to 33, [3] to 200 ms +// (longer ones are idle). pg = the earlier render buffer's commit-to-callback, enc = this buffer's creation-to-commit, res = what is left +// (negative when the two overlapped), kpx = the earlier one's thousands of pixels, hv = how many earlier ones were over 30 Mpx. +// ---- frame trace: one record per command buffer and per wait, appended to a file by a writer thread ---- +// The file is read by the kernel's diagnostic export (BootDiagExport.c lists it), so a hang or a reset still leaves everything up to then. +// 72-byte records: type 1 command buffer {idx, t0 created, t1 committed, t2 scheduled (0: unknown), t3 completion callback, a passes, b draws, +// c thousands of pixels, d blits, tid creator thread, cpu0/cpu1 its cumulative CPU microseconds at creation and at commit, tid2 committing +// thread}; type 2 waitUntilCompleted {t0 begin, t1 end, tid, a caller (low 32 bits), cpu0 the thread's CPU time at the start, cpu1 at the end}; +// type 3 the same for waitUntilScheduled; type 4 a thread's name {tid, 24 characters in t0..t2}. Times are nowUs(). +struct TraceRec {uint32_t type,idx;uint64_t t0,t1,t2,t3;uint32_t a,b,c,d,tid,cpu0,cpu1,tid2;}; +static_assert(sizeof(TraceRec)==72,"trace record"); +static TraceRec gTrace[16384]; +static std::atomic gTraceHead{0}; +static std::atomic gTraceStarted{false}; +static uint32_t threadCpuUs() { + thread_basic_info_data_t info;mach_msg_type_number_t count=THREAD_BASIC_INFO_COUNT; + if(thread_info(pthread_mach_thread_np(pthread_self()),THREAD_BASIC_INFO,(thread_info_t)&info,&count)!=KERN_SUCCESS)return 0; + return (uint32_t)((uint64_t)(info.user_time.seconds+info.system_time.seconds)*1000000ull+(uint64_t)(info.user_time.microseconds+info.system_time.microseconds)); +} +static uint32_t threadIdLow() {uint64_t id=0;pthread_threadid_np(NULL,&id);return (uint32_t)id;} +static void tracePush(const TraceRec &rec) { + const uint32_t at=gTraceHead.fetch_add(1); + gTrace[at%16384]=rec; + gTrace[at%16384].idx=rec.type==1?rec.idx:at; +} +static void traceStartWriter(); +// type 5 swap served by the scanout client: {idx, t0 offer arrived, t1 GPU copy returned, t2 swap completed, a sequence, b result} +extern "C" void NBTraceSwap(uint64_t offered,uint64_t copied,uint64_t completed,uint32_t sequence,uint32_t result) { + TraceRec rec={};rec.type=5;rec.t0=offered;rec.t1=copied;rec.t2=completed;rec.a=sequence;rec.b=result;tracePush(rec); + traceStartWriter(); +} +static void traceNote(uint32_t tid) { + static std::atomic seen[64]; + for(unsigned i=0;i<64;i++){uint32_t v=seen[i].load();if(v==tid)return;if(!v){uint32_t zero=0;if(seen[i].compare_exchange_strong(zero,tid))break;if(seen[i].load()==tid)return;}} + TraceRec rec={};rec.type=4;rec.tid=tid;pthread_getname_np(pthread_self(),(char *)&rec.t0,24);tracePush(rec); +} + +// ---- sampler: where the render thread's CPU goes between a command buffer's creation and its commit ---- +// A thread suspends the render thread every 500 us while it is inside that window and keeps the program counter plus the frame-pointer chain in a +// ring (no allocation, no locks: the suspended thread may hold any). The writer thread resolves the addresses with dladdr and rewrites cpu.profile +// every few seconds: L = samples whose leaf is that symbol, I = samples with that symbol anywhere in the chain. +static std::atomic gSampleThread{0}; +static std::atomic gSampleTop{0}; +struct StackSample {uint64_t frames[8];}; +static StackSample gSamples[8192]; +static std::atomic gSampleHead{0}; +static std::atomic gSamplerStarted{false}; +static void sampleWindowOpen() { +#ifndef NB_SAMPLER + return; // the sampler suspends the render thread 2000 times a second: off unless asked for +#endif + char name[32]={};pthread_getname_np(pthread_self(),name,sizeof name); + if(strncmp(name,"com.apple.coreanimation",23))return; + gSampleTop.store((uint64_t)pthread_get_stackaddr_np(pthread_self())); + gSampleThread.store(pthread_mach_thread_np(pthread_self())); + if(!gSamplerStarted.exchange(true))std::thread([]{ + pthread_setname_np("frame-sampler"); + for(;;) { + usleep(500); + const mach_port_t port=gSampleThread.load(); + if(!port)continue; + if(thread_suspend(port)!=KERN_SUCCESS)continue; + arm_thread_state64_t state;mach_msg_type_number_t count=ARM_THREAD_STATE64_COUNT; + StackSample sample={}; + if(thread_get_state(port,ARM_THREAD_STATE64,(thread_state_t)&state,&count)==KERN_SUCCESS) { + const uint64_t mask=0x0000007fffffffffull,top=gSampleTop.load(); + const uint64_t sp=arm_thread_state64_get_sp(state); + unsigned n=0; + sample.frames[n++]=arm_thread_state64_get_pc(state)&mask; + sample.frames[n++]=arm_thread_state64_get_lr(state)&mask; + uint64_t fp=arm_thread_state64_get_fp(state)&mask; + while(n<8 && fp>=sp && fp+16<=top && !(fp&15)) { + const uint64_t *record=(const uint64_t *)fp; + sample.frames[n++]=record[1]&mask; + const uint64_t next=record[0]&mask; + if(next<=fp)break; + fp=next; + } + } + thread_resume(port); + if(sample.frames[0])gSamples[gSampleHead.fetch_add(1)%8192]=sample; + } + }).detach(); +} +static void sampleWindowClose() { + if(gSampleThread.load()==pthread_mach_thread_np(pthread_self()))gSampleThread.store(0); +} +static void sampleWriteProfile(uint32_t &consumed,int &fd,const char *const *paths) { + static std::unordered_map names; + static std::unordered_map leaf,inclusive; + struct AddressCount {uint32_t leaf=0,inclusive=0;}; + static std::unordered_map addresses; + static uint32_t total=0,rounds=0; + const uint32_t head=gSampleHead.load(); + if(head-consumed>8000)consumed=head-8000; + auto resolve=[&](uint64_t address)->const std::string & { + auto it=names.find(address);if(it!=names.end())return it->second; + Dl_info info={};std::string name; + if(dladdr((const void *)address,&info)){ + const char *image=info.dli_fname?strrchr(info.dli_fname,'/'):NULL;image=image?image+1:(info.dli_fname?info.dli_fname:"?"); + char text[256]; + if(info.dli_sname)snprintf(text,sizeof text,"%s`%s+%llu",image,info.dli_sname,(unsigned long long)(address-(uint64_t)info.dli_saddr)); + else snprintf(text,sizeof text,"%s`+0x%llx",image,(unsigned long long)(address-(uint64_t)info.dli_fbase)); + name=text; + } else name="?"; + return names[address]=name; + }; + while(consumed0)name.resize(plus); + bool dup=false;for(unsigned j=0;j &map,char tag,unsigned limit,std::string &out) { + std::vector> rows; + for(auto &entry:map)rows.push_back({entry.second,entry.first}); + std::sort(rows.rbegin(),rows.rend()); + for(unsigned i=0;i> rows; + for(auto &entry:addresses)rows.push_back({entry.second.inclusive,entry.first}); + std::sort(rows.rbegin(),rows.rend()); + for(unsigned i=0;i=0)break;} + if(fd<0)continue; + uint32_t head=gTraceHead.load(); + if(head16000)written=head-16000; // the ring lapped the writer: the oldest are gone + while(writtenroom)n=room; + if(write(fd,&gTrace[written%16384],n*sizeof(TraceRec))<0)break; + written+=n; + } + } + }).detach(); +} + +struct FrameAttribution { + uint64_t prevCommitUs=0,prevCtcUs=0,prevKpx=0; + struct Bin {uint32_t n=0,heavy=0;uint64_t sumI=0,sumPg=0,sumEnc=0,sumKpx=0;int64_t sumRes=0;} bins[4]; +}; +static FrameAttribution gAttribution; +// Who calls waitUntilCompleted: the caller's return address (the 36 low bits) and its caller's, with a count and the total wait. +struct WaitCaller {std::atomic lr{0},lr2{0};std::atomic n{0};std::atomic us{0};char thread[24]={0},symbol[28]={0};}; +static WaitCaller gWaitCallers[8]; +static void noteWaitCaller(uint64_t lr,uint64_t lr2,uint64_t us) { + for(unsigned i=0;i<8;i++) { + uint64_t seen=gWaitCallers[i].lr.load(); + if(!seen){uint64_t zero=0;if(gWaitCallers[i].lr.compare_exchange_strong(zero,lr)){ + gWaitCallers[i].lr2=lr2;pthread_getname_np(pthread_self(),gWaitCallers[i].thread,sizeof gWaitCallers[i].thread); + Dl_info info;if(dladdr((void*)(uintptr_t)lr2,&info)&&info.dli_sname)strlcpy(gWaitCallers[i].symbol,info.dli_sname,sizeof gWaitCallers[i].symbol); + }seen=gWaitCallers[i].lr.load();} + if(seen==lr){gWaitCallers[i].n++;gWaitCallers[i].us+=us;return;} + } +} static std::mutex gCommitLock; // Command buffers committed in the last windowUs microseconds (at most the ring's 32): shader priming waits while the render server animates. extern "C" unsigned native_bridge_metal_recent_commits(uint64_t windowUs) { @@ -83,7 +275,7 @@ static std::atomic gSurfaceTextures,gSurfaceRefusals,gSurfacePlanar; struct SurfaceRefusal {uint32_t step,format,planes,plane,pixelFormat;uint64_t offset,row,allocation,width,height;}; static SurfaceRefusal gSurfaceLastRefusal; // Shader priming's progress (PrimeShaders.h, in the plugin), for the PR row. -extern "C" volatile uint32_t native_prime_compiling,native_prime_libraries_done,native_prime_libraries_total,native_prime_made; +extern "C" volatile uint32_t native_prime_compiling,native_prime_libraries_done,native_prime_libraries_total,native_prime_made,native_prime_rate; static void refuseSurface(uint32_t step,IOSurfaceRef surface,MTLTextureDescriptor *d,NSUInteger plane,uint64_t offset,uint64_t row) { gSurfaceRefusals++; SurfaceRefusal r={step,surface?IOSurfaceGetPixelFormat(surface):0,surface?(uint32_t)IOSurfaceGetPlaneCount(surface):0,(uint32_t)plane, @@ -537,6 +729,8 @@ static void notified(void *context,uint64_t value) { @public EncodedWork encodedWork; // the command buffer's single encoder thread, not global counter deltas uint64_t createdUs; + uint32_t createCpu,createTid,commitCpu,commitTid; + BOOL earlyEligible; // made by the render server's thread: its completed handlers may run at submission (see fireEarly) } @property(nonatomic) NmbCommandBuffer *handle; @property(nonatomic,strong) NBQueue *queue; @@ -815,10 +1009,20 @@ static void waitForResource(id resource) { static void completed(void *context); static void scheduled(void *context); +// Early completed handlers. The render server starts its next frame from the completed handler of the previous one, so a frame's encoding +// waited for the GPU of the frame before it (interval = GPU time + encode time). The handlers of the server's own render buffers run at submission +// instead; the swap offered by them is held back by the scanout client until the GPU finished everything committed before the offer +// (NBWaitInFlight), and waitUntilCompleted, the resource waits and the real completion work (surface flushes, release) are unchanged. +static bool earlyOn() {return false;} // reverted: with the handlers early the blur passes read frames that were not finished (i343, i344) +static bool onRenderServerThread() { + char name[32]={};pthread_getname_np(pthread_self(),name,sizeof name); + return !strncmp(name,"com.apple.coreanimation",23); +} +extern "C" void NBWaitInFlight(uint64_t beforeUs); @implementation NBCommandBuffer - (instancetype)init { if((self=[super init])){_scheduledHandlers=[NSMutableArray array];_completedHandlers=[NSMutableArray array];_surfaces=[NSMutableSet set];_resourceOwners=[NSMutableSet set];_handlers=dispatch_group_create();_schedulingHandlers=dispatch_group_create();} - if(self)createdUs=nowUs(); + if(self){createdUs=nowUs();createCpu=threadCpuUs();createTid=threadIdLow();earlyEligible=onRenderServerThread();sampleWindowOpen();} return self; } - (void)dealloc {nmb_command_buffer_release(_handle);} @@ -961,6 +1165,7 @@ static void scheduled(void *context); if(self.committed){NBLOG(@"native-metal: commit twice");return;} self.committed=YES; self.committedUs=nowUs(); + commitCpu=threadCpuUs();commitTid=threadIdLow();sampleWindowClose(); { std::lock_guard lock(gCommitLock); // Assign once under the same lock as publication. A concurrent commit cannot reuse this index. @@ -976,18 +1181,32 @@ static void scheduled(void *context); // Completion owns this reference; the runtime orders the non-owning scheduled callback before it. void *context=(__bridge_retained void *)self; if(nmb_command_buffer_on_completed(_handle,completed,context) && - nmb_command_buffer_on_scheduled(_handle,scheduled,context) && nmb_command_buffer_commit(_handle))return; + nmb_command_buffer_on_scheduled(_handle,scheduled,context) && nmb_command_buffer_commit(_handle)) {[self fireEarly];return;} (void)CFBridgingRelease(context); // refused registration/commit cannot call the registered handlers [self refuse:[@"commit: " stringByAppendingString:lastError()]]; } self.failedLocally=YES;gCommitFailures++; dispatch_async(dispatch_get_global_queue(QOS_CLASS_DEFAULT,0),^{[self finish];}); } +- (void)fireEarly { + if(!earlyEligible || encodedWork.passes==0 || !earlyOn())return; + NSArray *handlers; + @synchronized(self){handlers=[_completedHandlers copy];[_completedHandlers removeAllObjects];} + if(!handlers.count)return; + dispatch_queue_t queue=_queue.completionQueue?:dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE,0); + dispatch_async(queue,^{for(MTLCommandBufferHandler block in handlers)block((id)self);}); +} - (void)waitUntilCompleted { if(!self.committed)return; const uint64_t began=nowUs(); if(!self.failedLocally)nmb_command_buffer_wait(_handle,0); + const uint32_t cpuStart=threadCpuUs(); dispatch_group_wait(_handlers,DISPATCH_TIME_FOREVER); + { + TraceRec rec={};rec.type=2;rec.t0=began;rec.t1=nowUs();rec.tid=threadIdLow();rec.a=(uint32_t)(uintptr_t)__builtin_return_address(0); + rec.cpu0=cpuStart;rec.cpu1=threadCpuUs();traceNote(rec.tid);tracePush(rec); + } + noteWaitCaller((uint64_t)__builtin_return_address(0)&0xfffffffffull,(uint64_t)__builtin_return_address(0),nowUs()-began); gTimeWaitCompleted.add(began); } - (void)waitUntilScheduled { @@ -996,10 +1215,22 @@ static void scheduled(void *context); if(!self.failedLocally && !nmb_command_buffer_wait_scheduled(_handle)){ NBLOG(@"native-metal: waitUntilScheduled refused: %@",lastError());return; } + const uint32_t cpuStart=threadCpuUs(); dispatch_group_wait(_schedulingHandlers,DISPATCH_TIME_FOREVER); + { + TraceRec rec={};rec.type=3;rec.t0=began;rec.t1=nowUs();rec.tid=threadIdLow();rec.a=(uint32_t)(uintptr_t)__builtin_return_address(0); + rec.cpu0=cpuStart;rec.cpu1=threadCpuUs();traceNote(rec.tid);tracePush(rec); + } gTimeWaitScheduled.add(began); } @end +// The scanout client: before it copies an offered frame, the GPU work committed up to then must be done. +extern "C" void NBWaitInFlight(uint64_t beforeUs) { + if(!earlyOn())return; + NSArray *pending;@synchronized(gInFlight){pending=gInFlight.allObjects;} + for(NBCommandBuffer *commandBuffer in pending) + if(commandBuffer.committed && !commandBuffer.failedLocally && commandBuffer.committedUs<=beforeUs)nmb_command_buffer_wait(commandBuffer.handle,0); +} static void scheduled(void *context) {[(__bridge NBCommandBuffer *)context didSchedule];} // The runtime's completion: the command buffer's own reference, taken at commit, is given back here. static void completed(void *context) { @@ -1011,7 +1242,28 @@ static void completed(void *context) { uint64_t seen=gTimeEncodeToCommit.slowest.load();while(took>seen && !gTimeEncodeToCommit.slowest.compare_exchange_weak(seen,took)){} std::lock_guard lock(gCommitLock); CommitRecord &r=gCommitRing[commandBuffer.commitIndex%32]; - if(r.index==commandBuffer.commitIndex)r.callbackUs=took; + if(r.index==commandBuffer.commitIndex) { + r.callbackUs=took; + { + traceStartWriter();traceNote(commandBuffer->createTid); + if(commandBuffer->commitTid!=commandBuffer->createTid)traceNote(commandBuffer->commitTid); + TraceRec rec={};rec.type=1;rec.idx=r.index;rec.t0=commandBuffer->createdUs;rec.t1=r.committedUs;rec.t2=r.scheduledUs>=0?r.committedUs+(uint64_t)r.scheduledUs:0; + rec.t3=r.committedUs+took;rec.a=r.work.passes;rec.b=r.work.draws;rec.c=(uint32_t)(r.work.pixels>>10);rec.d=r.work.blits; + rec.tid=commandBuffer->createTid;rec.cpu0=commandBuffer->createCpu;rec.cpu1=commandBuffer->commitCpu;rec.tid2=commandBuffer->commitTid; + tracePush(rec); + } + if(r.work.passes>0 && r.work.pixels>0) { + FrameAttribution &a=gAttribution; + const uint64_t interval=a.prevCommitUs?r.committedUs-a.prevCommitUs:0; + if(interval && interval<200000) { + FrameAttribution::Bin &b=a.bins[interval<11000?0:interval<16700?1:interval<33000?2:3]; + b.n++;b.sumI+=interval;b.sumPg+=a.prevCtcUs;b.sumEnc+=r.ageUs;b.sumKpx+=a.prevKpx; + b.sumRes+=(int64_t)interval-(int64_t)a.prevCtcUs-(int64_t)r.ageUs; + if(a.prevKpx>30000)b.heavy++; + } + a.prevCommitUs=r.committedUs;a.prevCtcUs=took;a.prevKpx=r.work.pixels>>10; + } + } } [commandBuffer finish]; } @@ -1762,7 +2014,7 @@ extern "C" size_t native_bridge_metal_telemetry(char *out,size_t size) { { std::lock_guard lock(gProblemLock); char formats[160];native_bridge_metal_formats_locked(formats,sizeof formats);row("F %.86s",formats); - row("PR compiling=%u libs=%u/%u made=%u",native_prime_compiling,native_prime_libraries_done,native_prime_libraries_total,native_prime_made); + row("PR compiling=%u libs=%u/%u made=%u rate=%u",native_prime_compiling,native_prime_libraries_done,native_prime_libraries_total,native_prime_made,native_prime_rate); const SurfaceRefusal &r=gSurfaceLastRefusal; if(r.step)row("SR %u %08x %c%c%c%c pl%u/%u o%llx rb%llu a%llx %llux%llu pf%u",r.step,r.format,(char)(r.format>>24),(char)(r.format>>16),(char)(r.format>>8), (char)r.format,r.plane,r.planes,(unsigned long long)r.offset,(unsigned long long)r.row,(unsigned long long)r.allocation,(unsigned long long)r.width, @@ -1774,6 +2026,17 @@ extern "C" size_t native_bridge_metal_telemetry(char *out,size_t size) { std::lock_guard lock(gCommitLock); const uint32_t newest=(uint32_t)gCommits.load(); row("%s","Q timing v2: per-buffer API counts; ctc=commit-to-callback; age includes caller idle"); + for(unsigned i=0;i<8;i++) { + const WaitCaller &w=gWaitCallers[i]; + if(!w.n.load())continue; + row("WC%u lr=%llx n%u us%llu t=%.23s s=%.27s",i,(unsigned long long)w.lr.load(),w.n.load(),(unsigned long long)w.us.load(),w.thread,w.symbol); + } + for(unsigned i=0;i<4;i++) { + const FrameAttribution::Bin &b=gAttribution.bins[i]; + if(!b.n)continue; + row("FA%u n%u I%llu pg%llu enc%llu res%lld kpx%llu hv%u",i,b.n,(unsigned long long)(b.sumI/b.n),(unsigned long long)(b.sumPg/b.n), + (unsigned long long)(b.sumEnc/b.n),(long long)(b.sumRes/(int64_t)b.n),(unsigned long long)(b.sumKpx/b.n),b.heavy); + } for(uint32_t k=0;k<20 && k)nb_newFunctionWithZeroConstants:(NSString *)name; @end -// Priming's progress for the telemetry table (the PR row): 1 while a pipeline compiles, libraries done of the list, pipelines made. -extern "C" {volatile uint32_t native_prime_compiling, native_prime_libraries_done, native_prime_libraries_total, native_prime_made;} +// Priming's progress for the telemetry table (the PR row): what the thread does (1 a pipeline compiles, 2 it waits for the desktop to +// settle, 3 it opens a library and its functions), libraries done of the list, pipelines made, and the most command buffers the last wait +// saw in 250 ms. +extern "C" {volatile uint32_t native_prime_compiling, native_prime_libraries_done, native_prime_libraries_total, native_prime_made, native_prime_rate;} namespace prime { static const char *const kRoots[] = {"/System/Library", "/Applications", "/System/Cryptexes/OS/System/Library"}; @@ -114,14 +116,20 @@ static std::vector libraries(const std::string &listPath, std::stri // Waits until the render server is not animating: more than 4 command buffers in 250 ms (16 fps; an idle desktop redraws at about 12 fps) // means frames are being drawn, and a pipeline compile then competes with them for the CPU and the driver (int-244: priming during the // scripted swipes raised the median frame interval from 19 to 30 ms). A higher bar let compiles in once they had slowed the frames under -// it (int-250: 12 per 250 ms, 460 pipelines made during the swipes at a 48 ms median). Returns the milliseconds it waited. +// it (int-250: 12 per 250 ms, 460 pipelines made during the swipes at a 48 ms median). With a fixed 120 Hz vsync the idle desktop draws 6 +// per 250 ms (i516), so the bar is 8: still under any animation, which draws 15 to 30. A desktop that never drops under it still gets one +// pipeline every 10 s (i513-i515: priming sat at 15/162 for the whole run without making a pipeline). Returns the milliseconds it waited. static uint64_t waitForQuiet() { const uint64_t began = nowMs(); uint64_t quietSince = 0; bool busy = false; + native_prime_rate = 0; for (;;) { const uint64_t now = nowMs(); - if (native_bridge_metal_recent_commits(250000) > 4) {busy = true; quietSince = 0;} + const unsigned commits = native_bridge_metal_recent_commits(250000); + if (commits > native_prime_rate) native_prime_rate = commits; + if (now - began >= 10000) return now - began; + if (commits > 8) {busy = true; quietSince = 0; native_prime_compiling = 2;} else if (!busy) return 0; else if (!quietSince) quietSince = now; // After animation, two seconds without it before the next compile, so a pause between two swipes does not let one in. @@ -200,12 +208,15 @@ static std::string library(id device, const std::string &path, const const uint64_t began = nowMs(); @autoreleasepool { NSError *error = nil; + native_prime_compiling = 3; id lib = [device newLibraryWithURL:[NSURL fileURLWithPath:@(path.c_str())] error:&error]; + native_prime_compiling = 0; if (!lib) return path + " library refused: " + (error ? error.localizedDescription.UTF8String : "no error"); NSArray *names = lib.functionNames; for (NSString *name in names) { @autoreleasepool { const std::string key = path + "\t" + name.UTF8String; + native_prime_compiling = 3; if (bad.count(key)) {++skipped; continue;} id function = [lib newFunctionWithName:name]; if (!function) {++refused; continue;} @@ -306,6 +317,7 @@ static void run(id device, std::string root, unsigned delaySeconds, c ++finished; } native_prime_libraries_done = finished; + native_prime_compiling = 0; char line[120]; snprintf(line, sizeof line, "pass libraries=%u finished=%u ms=%llu", libs, finished, (unsigned long long)(nowMs() - began)); appendLine(logPath, line); From 1a454a1be90d6894f96699e9fad87c69c3846048 Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Mon, 26 Oct 2026 00:59:08 +0800 Subject: [PATCH 22/92] perf(bridge): record the runtime's draw-path patches 0009 to 0015 Keep the final pass between frames, allocate less per draw, keep draw commands and draw lists between encoders, skip rebinding unchanged resources, map tracker keys, and start a two-plane shadow as its memory lies. They stack in order on 0001 to 0008. --- bridge/patches/0009-keep-final-pass.patch | 140 +++++ .../0010-fewer-allocations-per-draw.patch | 559 ++++++++++++++++++ bridge/patches/0011-keep-draw-commands.patch | 346 +++++++++++ bridge/patches/0012-keep-draw-lists.patch | 302 ++++++++++ .../patches/0013-skip-unchanged-rebinds.patch | 390 ++++++++++++ bridge/patches/0014-map-tracker-keys.patch | 66 +++ ...0015-two-plane-shadow-as-memory-lies.patch | 42 ++ 7 files changed, 1845 insertions(+) create mode 100644 bridge/patches/0009-keep-final-pass.patch create mode 100644 bridge/patches/0010-fewer-allocations-per-draw.patch create mode 100644 bridge/patches/0011-keep-draw-commands.patch create mode 100644 bridge/patches/0012-keep-draw-lists.patch create mode 100644 bridge/patches/0013-skip-unchanged-rebinds.patch create mode 100644 bridge/patches/0014-map-tracker-keys.patch create mode 100644 bridge/patches/0015-two-plane-shadow-as-memory-lies.patch diff --git a/bridge/patches/0009-keep-final-pass.patch b/bridge/patches/0009-keep-final-pass.patch new file mode 100644 index 0000000..8c1b295 --- /dev/null +++ b/bridge/patches/0009-keep-final-pass.patch @@ -0,0 +1,140 @@ +diff --git a/crates/metal2vulkan-runtime/src/device/mod.rs b/crates/metal2vulkan-runtime/src/device/mod.rs +index 00a6e55..8842010 100644 +--- a/crates/metal2vulkan-runtime/src/device/mod.rs ++++ b/crates/metal2vulkan-runtime/src/device/mod.rs +@@ -63,7 +63,7 @@ use super::memory::Memory; + use super::mipmap_scratch::MipmapScratch; + use super::owned_kernel::OwnedKernels; + use super::render_state::{depth_bias_command, DepthBiasCommand}; +-use super::render_target::KeptCompatible; ++use super::render_target::{KeptCompatible, KeptFinal}; + use super::resource_ids::{ResourceIds, SamplerIdReads}; + use super::sample_planes::PlanesObjects; + use super::sampler::{SamplerCount, SamplerKey, SamplerShared}; +@@ -305,6 +305,8 @@ pub(super) struct DeviceShared { + pub(super) attachmentless: Mutex, + /// The compatible passes it keeps, by shape (`render_target::shared_compatible_pass`). + pub(super) compatible_passes: Mutex, ++ /// The final passes it keeps, by what they are made of (`render_target::kept_final_pass`). ++ pub(super) final_passes: Mutex, + /// The caller's images that move between layouts, and the events' timeline semaphores + /// (`interop.rs`). + pub(super) interop: Interop, +@@ -498,6 +500,7 @@ impl Drop for DeviceShared { + lock(&self.depth_stencil_resolves).destroy(self.vk()); + lock(&self.attachmentless).destroy(self.vk()); + lock(&self.compatible_passes).destroy(self.vk()); ++ lock(&self.final_passes).destroy(self.vk()); + // What released variants left (`pipeline/kept.rs`). + lock(&self.kept).destroy(self); + // The graphics pipeline libraries, after every pipeline they linked: each linked +@@ -1272,6 +1275,7 @@ impl Device { + depth_stencil_resolves: Mutex::new(DepthStencilResolveObjects::default()), + attachmentless: Mutex::default(), + compatible_passes: Mutex::default(), ++ final_passes: Mutex::default(), + interop: Interop::default(), + heap_keys: Default::default(), + rebinds: Default::default(), +diff --git a/crates/metal2vulkan-runtime/src/render_target.rs b/crates/metal2vulkan-runtime/src/render_target.rs +index 0310edd..e7f7391 100644 +--- a/crates/metal2vulkan-runtime/src/render_target.rs ++++ b/crates/metal2vulkan-runtime/src/render_target.rs +@@ -299,7 +299,7 @@ pub(super) fn later_steps(layout: &PassLayout) -> impl Iterator, ++ depth_stencil: Option, ++ resolves: Vec, ++ depth_stencil_resolve: Option, ++ resolving: bool, ++} ++ ++/// The final passes a device keeps, by what they are made of ([`kept_final_pass`]). ++#[derive(Default)] ++pub(super) struct KeptFinal(HashMap); ++ ++impl KeptFinal { ++ /// Destroy every kept pass; the device is idle, and is destroyed next. ++ pub(super) fn destroy(&self, raw: &ash::Device) { ++ for &pass in self.0.values() { ++ // SAFETY: the device is idle, so no pending command uses the pass, and no framebuffer ++ // made with it is left (each goes with the command buffer that began it). ++ unsafe { release::object(raw, pass) }; ++ } ++ } ++} ++ ++/// The device's final pass of `makings`, made now where it keeps none. A render pass is immutable, ++/// so one serves every instance made of the same: made and destroyed for every instance, as ++/// before, it cost a `vkCreateRenderPass2` (where Turnip lays out its attachments in GMEM for ++/// every tile layout) and a `vkDestroyRenderPass` each time a pass ended, 19 times a frame of ++/// iOS's compositor. Past [`KEPT_FINALS`] a pass is made for its instance alone, as before. ++fn kept_final_pass(device: &Device, makings: FinalMakings) -> Result { ++ let kept = &device.shared().final_passes; ++ if let Some(&raw) = lock(kept).0.get(&makings) { ++ return Ok(RenderPassObject::kept(device, raw)); ++ } ++ let pass = create( ++ device, ++ &makings.shape, ++ &makings.colors, ++ makings.depth_stencil, ++ &makings.resolves, ++ makings.depth_stencil_resolve, ++ makings.resolving, ++ )?; ++ let mut passes = lock(kept); ++ // Another encoder's may have come first: its pass serves, and this one goes unused. ++ if let Some(&raw) = passes.0.get(&makings) { ++ return Ok(RenderPassObject::kept(device, raw)); ++ } ++ if pass.owner == Owner::Holders && passes.0.len() < KEPT_FINALS { ++ let raw = pass.into_kept(); ++ passes.0.insert(makings, raw); ++ return Ok(RenderPassObject::kept(device, raw)); ++ } ++ Ok(pass) ++} ++ + /// The operations of color attachment `color` in an instance at `at`, the sample planes' + /// `anchor` or not, its `DontCare` `dont_care`'s, but the image a texture over a buffer keeps for + /// a pass that does not store it, which nothing reads after the pass (`kept_bytes.rs`). An diff --git a/bridge/patches/0010-fewer-allocations-per-draw.patch b/bridge/patches/0010-fewer-allocations-per-draw.patch new file mode 100644 index 0000000..859e4fc --- /dev/null +++ b/bridge/patches/0010-fewer-allocations-per-draw.patch @@ -0,0 +1,559 @@ +diff --git a/crates/metal2vulkan-runtime/src/render.rs b/crates/metal2vulkan-runtime/src/render.rs +index 119119d..3767b60 100644 +--- a/crates/metal2vulkan-runtime/src/render.rs ++++ b/crates/metal2vulkan-runtime/src/render.rs +@@ -296,7 +296,7 @@ use super::render_pass::{ + self, check_memoryless, AspectTarget, Attachment, DepthStencilTarget, PassLayout, + RenderPassDescriptor, RenderPassProblem, Target, HIDDEN_DEPTH_FORMAT, MAX_COLOR_ATTACHMENTS, + }; +-use super::render_state::{self, DepthBiasRecord, EncoderState, MAX_VIEWPORTS}; ++use super::render_state::{self, DepthBiasRecord, EncoderState, PerViewport, MAX_VIEWPORTS}; + use super::render_state::{Compensation, DepthBiasEffect, WrittenDepthClamp}; + use super::render_target::{self, covers, PassShape, Position, RenderPassObject}; + use super::render_values::{LoadAction, ScissorRect, StoreAction, StoreActionOptions, Viewport}; +@@ -612,8 +612,8 @@ struct DrawSettings { + /// (`render_state.rs`). + #[derive(Clone)] + struct DrawState { +- viewports: Vec, +- scissors: Vec, ++ viewports: PerViewport, ++ scissors: PerViewport, + fixed: FixedState, + /// The front and back stencil references. + stencil_reference: [u32; 2], +@@ -1197,11 +1197,12 @@ impl LastRetained { + /// A variant, plan and specialization under which `Device::degrade_bound_values` was quiet: no + /// clause of a note it tested held. Which hold is a function of the three alone, so a draw of + /// the same three announces nothing there. The variant and the plan are held, so a pointer seen +-/// again is the same object. ++/// again is the same object. The specialization is the plan's own where `specialization` is ++/// `None` (every draw but a mesh draw of its own key), so a draw keeps no copy of it. + struct QuietBoundValues { + variant: Arc, + plan: Arc, +- specialization: SpecializationKey, ++ specialization: Option, + } + + impl QuietBoundValues { +@@ -1214,7 +1215,11 @@ impl QuietBoundValues { + ) -> bool { + Arc::ptr_eq(&self.variant, variant) + && Arc::ptr_eq(&self.plan, plan) +- && self.specialization == *specialization ++ && self ++ .specialization ++ .as_ref() ++ .unwrap_or(&self.plan.specialization) ++ == specialization + } + } + +@@ -6498,7 +6503,7 @@ impl RenderCommandEncoder { + // offsetting bias folded into the compensation (`EncoderState::compensation`). + let depth = compensation.and_then(|c| c.depth); + let viewports = match depth { +- Some(_) => self.state.used_viewports(selects).to_vec(), ++ Some(_) => self.state.used_viewports(selects).iter().copied().collect(), + None => self + .state + .draw_viewports(selects, topology, profile, native)?, +@@ -6553,12 +6558,12 @@ impl RenderCommandEncoder { + let [min_depth, max_depth] = + depth.unwrap_or([viewport.min_depth, viewport.max_depth]); + DrawState { +- viewports: vec![vk::Viewport { ++ viewports: PerViewport::one(vk::Viewport { + min_depth, + max_depth, + ..viewport +- }], +- scissors: vec![scissor], ++ }), ++ scissors: PerViewport::one(scissor), + ..draw_state.clone() + } + }) +@@ -6620,7 +6625,7 @@ impl RenderCommandEncoder { + self.quiet_bound_values = Some(QuietBoundValues { + variant: Arc::clone(&variant), + plan: Arc::clone(&plan), +- specialization: specialization.clone(), ++ specialization: commands.specialization.clone(), + }); + } + device.degrade_cross_class(&plan.diagnostics, &formats); +diff --git a/crates/metal2vulkan-runtime/src/render/bound.rs b/crates/metal2vulkan-runtime/src/render/bound.rs +index 0060cfc..be326c0 100644 +--- a/crates/metal2vulkan-runtime/src/render/bound.rs ++++ b/crates/metal2vulkan-runtime/src/render/bound.rs +@@ -32,7 +32,7 @@ use ash::vk; + use super::{DrawState, IndexBind}; + use crate::binding::PushWrite; + use crate::pipeline::FixedState; +-use crate::render_state::DepthBiasSetting; ++use crate::render_state::{DepthBiasSetting, PerViewport}; + + /// The state commands recorded into one secondary command buffer that still hold (the module + /// docs). +@@ -63,8 +63,8 @@ pub(super) struct Bound { + /// The values a draw sets its dynamic states to: the encoder's state at the draw and its input + /// assembly. + pub(super) struct Values { +- viewports: Vec, +- scissors: Vec, ++ viewports: PerViewport, ++ scissors: PerViewport, + fixed: FixedState, + stencil_reference: [u32; 2], + depth_bias: DepthBiasSetting, +@@ -226,8 +226,8 @@ impl Values { + fn of(now: Now<'_>) -> Values { + let s = now.state; + Values { +- viewports: s.viewports.clone(), +- scissors: s.scissors.clone(), ++ viewports: s.viewports, ++ scissors: s.scissors, + fixed: s.fixed, + stencil_reference: s.stencil_reference, + depth_bias: s.depth_bias.setting(), +@@ -302,14 +302,8 @@ impl Values { + let s = now.state; + let (ds, now_ds) = (&mut self.fixed.depth_stencil, &s.fixed.depth_stencil); + match state { +- vk::DynamicState::VIEWPORT => { +- self.viewports.clear(); +- self.viewports.extend_from_slice(&s.viewports); +- } +- vk::DynamicState::SCISSOR => { +- self.scissors.clear(); +- self.scissors.extend_from_slice(&s.scissors); +- } ++ vk::DynamicState::VIEWPORT => self.viewports = s.viewports, ++ vk::DynamicState::SCISSOR => self.scissors = s.scissors, + vk::DynamicState::DEPTH_BIAS => self.depth_bias = s.depth_bias.setting(), + vk::DynamicState::BLEND_CONSTANTS => self.blend_color = s.blend_color, + vk::DynamicState::STENCIL_COMPARE_MASK => { +diff --git a/crates/metal2vulkan-runtime/src/render_state.rs b/crates/metal2vulkan-runtime/src/render_state.rs +index f6f5ffd..e231b10 100644 +--- a/crates/metal2vulkan-runtime/src/render_state.rs ++++ b/crates/metal2vulkan-runtime/src/render_state.rs +@@ -113,6 +113,70 @@ use metal2vulkan::pipeline::{GraphicsState, ViewportCompensation}; + /// Metal's most viewports and scissor rectangles. + pub(super) const MAX_VIEWPORTS: usize = 16; + ++/// One value per viewport a draw uses, held in place: its viewports and scissors, which every draw ++/// makes, without allocating. A draw uses at most [`MAX_VIEWPORTS`] (the setters refuse more, ++/// `check_count`, and a draw uses the viewports set or the first). ++#[derive(Clone, Copy, Debug)] ++pub(super) struct PerViewport { ++ items: [T; MAX_VIEWPORTS], ++ len: u8, ++} ++ ++impl PerViewport { ++ /// `item` alone. ++ pub(super) fn one(item: T) -> PerViewport { ++ std::iter::once(item).collect() ++ } ++} ++ ++impl std::ops::Deref for PerViewport { ++ type Target = [T]; ++ ++ fn deref(&self) -> &[T] { ++ &self.items[..usize::from(self.len)] ++ } ++} ++ ++impl std::ops::DerefMut for PerViewport { ++ fn deref_mut(&mut self) -> &mut [T] { ++ &mut self.items[..usize::from(self.len)] ++ } ++} ++ ++impl FromIterator for PerViewport { ++ fn from_iter>(iter: I) -> PerViewport { ++ let mut items = [T::default(); MAX_VIEWPORTS]; ++ let mut len = 0; ++ for item in iter { ++ assert!(len < MAX_VIEWPORTS, "a draw of more than {MAX_VIEWPORTS} viewports"); ++ items[len] = item; ++ len += 1; ++ } ++ PerViewport { ++ items, ++ len: len as u8, ++ } ++ } ++} ++ ++impl<'a, T> IntoIterator for &'a PerViewport { ++ type Item = &'a T; ++ type IntoIter = std::slice::Iter<'a, T>; ++ ++ fn into_iter(self) -> Self::IntoIter { ++ self.iter() ++ } ++} ++ ++impl<'a, T> IntoIterator for &'a mut PerViewport { ++ type Item = &'a mut T; ++ type IntoIter = std::slice::IterMut<'a, T>; ++ ++ fn into_iter(self) -> Self::IntoIter { ++ self.iter_mut() ++ } ++} ++ + /// What a viewport, scissor rectangle or depth/stencil setter did wrong. + #[non_exhaustive] + #[derive(Clone, Copy, Debug, PartialEq, Eq)] +@@ -335,7 +399,7 @@ impl EncoderState { + topology: metal2vulkan::vk::PrimitiveTopology, + profile: &DeviceProfile, + native: bool, +- ) -> Result, Error> { ++ ) -> Result, Error> { + let used = self.used_viewports(selects); + let clamps = self.clip_mode == DepthClipMode::Clamp; + // In clamp mode a range past `[0, 1]` whose one clamp holds one depth stores that depth +@@ -766,7 +830,7 @@ impl EncoderState { + viewports: &[Viewport], + depth: Option<[f32; 2]>, + guard: Option<[f64; 2]>, +- ) -> Vec { ++ ) -> PerViewport { + viewports + .iter() + .map(|v| { +@@ -808,7 +872,7 @@ impl EncoderState { + /// its index, the one rectangle where the encoder holds one, and an empty one past two or + /// more (the module docs), each clipped to the pixels its viewport overlaps, Metal's implicit + /// scissor ([`viewport_pixels`]). +- pub(super) fn vk_scissors(&self, selects: bool) -> Vec { ++ pub(super) fn vk_scissors(&self, selects: bool) -> PerViewport { + self.each_vk_scissor(selects).collect() + } + +diff --git a/crates/metal2vulkan/src/bind/dispatch.rs b/crates/metal2vulkan/src/bind/dispatch.rs +index f8a62bb..3536491 100644 +--- a/crates/metal2vulkan/src/bind/dispatch.rs ++++ b/crates/metal2vulkan/src/bind/dispatch.rs +@@ -897,7 +897,8 @@ fn instantiate( + stride.to_le_bytes().to_vec() + } + field => slot_field(field, &bound.residuals, &t.resources, state) +- .ok_or_else(|| internal(format!("a compute push field {field:?}")))?, ++ .ok_or_else(|| internal(format!("a compute push field {field:?}")))? ++ .to_vec(), + }; + put(&mut push, push_start, &mut table, f.loc, &bytes)?; + } +diff --git a/crates/metal2vulkan/src/bind/graphics.rs b/crates/metal2vulkan/src/bind/graphics.rs +index a477fab..850f28a 100644 +--- a/crates/metal2vulkan/src/bind/graphics.rs ++++ b/crates/metal2vulkan/src/bind/graphics.rs +@@ -234,8 +234,8 @@ use std::collections::BTreeMap; + use crate::bind::heap::{planner, HeapPlanner}; + use crate::bind::instantiate::{ + bind_buffers, descriptor_range, put, slot_field, BufferExtent, BufferRange, DescriptorBytes, +- DescriptorRange, IndexBufferRef, IndirectRef, PushConstantWrite, PushHole, RangeError, +- RootTable, ++ DescriptorRange, FieldBytes, IndexBufferRef, IndirectRef, PushConstantWrite, PushHole, ++ RangeError, RootTable, + }; + use crate::bind::neutral::{zero_buffer_bytes, NeutralResource}; + use crate::bind::plan::compute::{internal, not_implemented, spec_key, tile_memory}; +@@ -3326,7 +3326,7 @@ fn instantiate( + } + ]; + for f in &st.fields { +- let bytes: Vec = match f.field { ++ let bytes: FieldBytes = match f.field { + PushField::RootTableAddress + | PushField::ExecutionStatusAddress + | PushField::CaptureAddress +@@ -3355,27 +3355,27 @@ fn instantiate( + let Some(shadow) = &t.shadow_stencil else { + return Err(internal("a stencil state without its shader-side stencil")); + }; +- shadow_stencil_words(shadow, state)? ++ shadow_stencil_words(shadow, state)?.into() + } + PushField::ShaderTestsState => { + if t.shader_tests.is_none() { + return Err(internal("a depth state without its in-shader depth test")); + } +- shader_tests_words(state) ++ shader_tests_words(state).into() + } + PushField::ShaderStencilState => { + if t.shader_tests.is_none() { + return Err(internal("a stencil state without its in-shader tests")); + } +- shader_stencil_words(state) ++ shader_stencil_words(state).into() + } + PushField::DispatchRegion => match mesh.as_ref().and_then(|m| m.region(st.stage)) { +- Some(region) => region.to_vec(), ++ Some(region) => FieldBytes::of(®ion), + None => return Err(internal("a dispatch region outside a mesh draw")), + }, +- PushField::ViewportCount => state.viewport_count.to_le_bytes().to_vec(), ++ PushField::ViewportCount => FieldBytes::of(&state.viewport_count.to_le_bytes()), + PushField::RenderTargetArrayLength => { +- state.render_target_array_length.to_le_bytes().to_vec() ++ FieldBytes::of(&state.render_target_array_length.to_le_bytes()) + } + // The identity where the state holds none: a plan states its variant, so + // instantiation under another shape asks for the other one first. +@@ -3392,11 +3392,12 @@ fn instantiate( + .flat_map(|v| v.to_le_bytes()) + .collect(), + // No viewport mirrored where the state holds none. +- PushField::ViewportMirrors => state +- .viewport_compensation +- .map_or(0, |c| c.mirrors) +- .to_le_bytes() +- .to_vec(), ++ PushField::ViewportMirrors => FieldBytes::of( ++ &state ++ .viewport_compensation ++ .map_or(0, |c| c.mirrors) ++ .to_le_bytes(), ++ ), + // No vertex's depth clamped where the state holds none. + PushField::VertexDepthClamp => state + .viewport_compensation +@@ -3411,11 +3412,11 @@ fn instantiate( + .iter() + .flat_map(|v| v.to_le_bytes()) + .collect(), +- PushField::StorageKeptChannels => t.storage_kept.to_le_bytes().to_vec(), ++ PushField::StorageKeptChannels => FieldBytes::of(&t.storage_kept.to_le_bytes()), + // The attachment's rule word, as link derived it from the pipeline's attachment. + PushField::ColorWriteRule(color) => { + match t.color_write_rules.iter().find(|(c, _)| *c == color) { +- Some((_, word)) => word.to_le_bytes().to_vec(), ++ Some((_, word)) => FieldBytes::of(&word.to_le_bytes()), + None => return Err(internal("a color write rule link gave no word")), + } + } +@@ -3437,16 +3438,12 @@ fn instantiate( + // The first amplification's; `amplifications` writes each before its draw. + PushField::AmplificationId + | PushField::AmplificationLayerOffset +- | PushField::AmplificationViewportOffset => { +- amplification_word(f.field, 0, &t.amplifications)? +- .to_le_bytes() +- .to_vec() +- } ++ | PushField::AmplificationViewportOffset => FieldBytes::of( ++ &lification_word(f.field, 0, &t.amplifications)?.to_le_bytes(), ++ ), + PushField::BaseVertexInstance => { + reads_base = true; +- let mut b = base[0].to_le_bytes().to_vec(); +- b.extend_from_slice(&base[1].to_le_bytes()); +- b ++ base.iter().flat_map(|b| b.to_le_bytes()).collect() + } + PushField::VertexBufferStride(layout) => { + let fixed = t.pulled_strides.iter().find(|(l, _)| *l == layout); +@@ -3471,7 +3468,7 @@ fn instantiate( + ))) + } + }; +- stride.to_le_bytes().to_vec() ++ FieldBytes::of(&stride.to_le_bytes()) + } + field => slot_field(field, &bound.residuals, &t.resources, state) + .ok_or_else(|| internal(format!("a render push field {field:?}")))?, +@@ -3961,21 +3958,22 @@ fn draw_uses( + }; + uses.push(control(UseTarget::ControlPointIndices, range)); + } +- uses.extend(match *draw { ++ // At most two, in place: a draw's own uses allocate nothing past `uses`. ++ let own: [Option; 2] = match *draw { + // A mesh draw's arguments are its pipeline's to decide (`instantiate`). + DrawCall::Primitives { .. } + | DrawCall::Patches { .. } + | DrawCall::MeshThreadgroups { .. } + | DrawCall::MeshThreads { .. } +- | DrawCall::MeshThreadgroupsIndirect { .. } => Vec::new(), ++ | DrawCall::MeshThreadgroupsIndirect { .. } => [None, None], + DrawCall::Indexed { + index_type, + index_buffer, + index_count, + index_offset, + .. +- } => vec![index(index_bytes(index_type).map(|size| { +- ByteRange { ++ } => [ ++ Some(index(index_bytes(index_type).map(|size| ByteRange { + offset: 0, + len: match overrun { + None => u64::from(index_count) * size, +@@ -3986,15 +3984,17 @@ fn draw_uses( + u64::from(index_count.saturating_sub(past_end)) * size + } + }, +- } +- }))], ++ }))), ++ None, ++ ], + DrawCall::Indirect { indexed: None, .. } | DrawCall::PatchesIndirect { .. } => { +- vec![indirect_use(16)] ++ [Some(indirect_use(16)), None] + } + DrawCall::Indirect { + indexed: Some(_), .. +- } => vec![index(None), indirect_use(20)], +- }); ++ } => [Some(index(None)), Some(indirect_use(20))], ++ }; ++ uses.extend(own.into_iter().flatten()); + uses + } + +diff --git a/crates/metal2vulkan/src/bind/instantiate.rs b/crates/metal2vulkan/src/bind/instantiate.rs +index 3eb2f84..62d9d3f 100644 +--- a/crates/metal2vulkan/src/bind/instantiate.rs ++++ b/crates/metal2vulkan/src/bind/instantiate.rs +@@ -611,6 +611,67 @@ pub(crate) fn bind_buffers( + Ok(out) + } + ++/// A push field's bytes as instantiation writes them: up to [`FieldBytes::INLINE`] bytes in place ++/// (every word and vector field, so a draw's fields allocate nothing), more in a vector (a capture ++/// layout, a dispatch region). ++pub(crate) enum FieldBytes { ++ Inline { ++ bytes: [u8; FieldBytes::INLINE], ++ len: u8, ++ }, ++ Heap(Vec), ++} ++ ++impl FieldBytes { ++ /// The most bytes held in place. ++ pub(crate) const INLINE: usize = 32; ++ ++ /// `bytes`, copied. ++ pub(crate) fn of(bytes: &[u8]) -> FieldBytes { ++ bytes.iter().copied().collect() ++ } ++} ++ ++impl std::ops::Deref for FieldBytes { ++ type Target = [u8]; ++ ++ fn deref(&self) -> &[u8] { ++ match self { ++ FieldBytes::Inline { bytes, len } => &bytes[..usize::from(*len)], ++ FieldBytes::Heap(bytes) => bytes, ++ } ++ } ++} ++ ++impl From> for FieldBytes { ++ fn from(bytes: Vec) -> FieldBytes { ++ FieldBytes::Heap(bytes) ++ } ++} ++ ++impl FromIterator for FieldBytes { ++ fn from_iter>(iter: I) -> FieldBytes { ++ let mut bytes = [0u8; FieldBytes::INLINE]; ++ let mut iter = iter.into_iter(); ++ for len in 0..=FieldBytes::INLINE { ++ let Some(byte) = iter.next() else { ++ return FieldBytes::Inline { ++ bytes, ++ len: len as u8, ++ }; ++ }; ++ if len == FieldBytes::INLINE { ++ let mut heap = bytes.to_vec(); ++ heap.push(byte); ++ heap.extend(iter); ++ return FieldBytes::Heap(heap); ++ } ++ bytes[len] = byte; ++ } ++ unreachable!("the loop returns by its last pass") ++ } ++} ++ + /// The bytes of a slot push field (`SlotBindOffset`: the residual, 0 unbound; `SlotGuestBase`: the + /// slot's `gpu_address + offset`, which is Metal's `nil`; `TextureLevels`: the level count the plan + /// bound; `InstanceCount`: the bound structure's instance count; `Specialized`: the value the plan +@@ -621,7 +682,7 @@ pub(crate) fn slot_field( + residuals: &[(SlotRef, u32)], + resources: &crate::bind::plan::ResourceTemplate, + state: &BindState, +-) -> Option> { ++) -> Option { + match field { + // The bound structure's instance count, 0 for an unbound slot. + PushField::InstanceCount(slot) => { +@@ -630,38 +691,38 @@ pub(crate) fn slot_field( + .get(&slot.stage) + .and_then(|t| t.acceleration_structures.get(&slot.index)) + .map_or(0, |s| s.instance_count); +- Some(count.to_le_bytes().to_vec()) ++ Some(FieldBytes::of(&count.to_le_bytes())) + } + // The level count the plan bound. + PushField::TextureLevels(slot) => resources + .texture_levels + .iter() + .find(|(s, _)| *s == slot) +- .map(|(_, levels)| levels.to_le_bytes().to_vec()), ++ .map(|(_, levels)| FieldBytes::of(&levels.to_le_bytes())), + // The value the bound resources decide, as the plan recorded it. + PushField::Specialized(key) => resources + .specialized + .iter() + .find(|(k, _)| *k == key) +- .map(|(_, value)| value.to_le_bytes().to_vec()), ++ .map(|(_, value)| FieldBytes::of(&value.to_le_bytes())), + // Whether the texture the plan bound holds 16-bit float channels. + PushField::TextureHalfChannels(slot) => resources + .texture_half_channels + .iter() + .find(|(s, _)| *s == slot) +- .map(|(_, half)| half.to_le_bytes().to_vec()), ++ .map(|(_, half)| FieldBytes::of(&half.to_le_bytes())), + // The integer channel widths of the texture the plan bound. + PushField::TextureIntegerChannels(slot) => resources + .texture_integer_channels + .iter() + .find(|(s, _)| *s == slot) +- .map(|(_, word)| word.to_le_bytes().to_vec()), ++ .map(|(_, word)| FieldBytes::of(&word.to_le_bytes())), + PushField::SlotBindOffset(slot) => { + let r = residuals + .iter() + .find(|(s, _)| *s == slot) + .map_or(0, |r| r.1); +- Some(r.to_le_bytes().to_vec()) ++ Some(FieldBytes::of(&r.to_le_bytes())) + } + PushField::SlotGuestBase(slot) => { + let base = match state +@@ -676,7 +737,7 @@ pub(crate) fn slot_field( + }) => gpu_address.wrapping_add(offset), + _ => 0, + }; +- Some(base.to_le_bytes().to_vec()) ++ Some(FieldBytes::of(&base.to_le_bytes())) + } + _ => None, + } diff --git a/bridge/patches/0011-keep-draw-commands.patch b/bridge/patches/0011-keep-draw-commands.patch new file mode 100644 index 0000000..c9b9b72 --- /dev/null +++ b/bridge/patches/0011-keep-draw-commands.patch @@ -0,0 +1,346 @@ +diff --git a/crates/metal2vulkan-runtime/src/render.rs b/crates/metal2vulkan-runtime/src/render.rs +index 3767b60..4400378 100644 +--- a/crates/metal2vulkan-runtime/src/render.rs ++++ b/crates/metal2vulkan-runtime/src/render.rs +@@ -325,8 +325,8 @@ pub use late_resolve::ResolveTarget; + use metal2vulkan::bind::BindingPlan; + use metal2vulkan::bind::VertexAmplification; + use metal2vulkan::bind::{ +- DrawCall, FragmentTests, HeapPlacement, ImageblockSize, IndexBufferRef, IndirectRef, +- MeshDispatch, MeshGroups, TessellationFactors, ViewSetup, WriteSource, ++ DrawCall, DrawCommands, FragmentTests, HeapPlacement, ImageblockSize, IndexBufferRef, ++ IndirectRef, MeshDispatch, MeshGroups, TessellationFactors, ViewSetup, WriteSource, + }; + use metal2vulkan::cache::SpecializationKey; + use metal2vulkan::device::{DriverFact, FormatUsage, VkFeature}; +@@ -1121,6 +1121,8 @@ pub struct RenderCommandEncoder { + /// The last draw's variant, plan and specialization under which the bound values' notices + /// were quiet (`Device::degrade_bound_values`), where it was. + quiet_bound_values: Option, ++ /// The last draw's commands, whose lists the next draw's fill again. ++ draw_commands: Option, + /// The views and sampler descriptions its draws' binds made or found (`binding::BindMemo`). + bind_memo: binding::BindMemo, + /// What its last draw retained of its own ([`LastRetained`]). +@@ -1391,6 +1393,7 @@ impl CommandBuffer { + live: Arc::default(), + log, + quiet_bound_values: None, ++ draw_commands: None, + bind_memo: binding::BindMemo::default(), + last_retained: LastRetained::default(), + }) +@@ -5961,8 +5964,8 @@ impl RenderCommandEncoder { + return Err(Error::NotImplemented(RuntimeGap::ImageblockMasters)); + } + } +- let commands = plan +- .instantiate_draw(&live.state, &draw) ++ let mut commands = self.draw_commands.take().unwrap_or_else(DrawCommands::empty); ++ plan.instantiate_draw_into(&live.state, &draw, &mut commands) + .map_err(Error::Bind)?; + self.reaches_unnamed |= !commands.root_tables.is_empty() || reads_handles(&plan); + // A mesh draw the binder launches no threadgroup of (as plain Metal draws nothing). +@@ -6674,7 +6677,7 @@ impl RenderCommandEncoder { + binding::bind(call, &device, &mut state, &plan, &command, &tables, memo)? + }; + // The draw's push bytes, filled in place. +- let push = binding::push_writes(commands.push_constants, &bound.holes)?; ++ let push = binding::push_writes(std::mem::take(&mut commands.push_constants), &bound.holes)?; + // A plan that draws one amplification at a time records the draw once per amplification, + // in order, each after that amplification's push writes. + let amplified = commands +@@ -7309,6 +7312,7 @@ impl RenderCommandEncoder { + if capture.is_some() { + self.capture_uses(uses); + } ++ self.draw_commands = Some(commands); + Ok(()) + } + +diff --git a/crates/metal2vulkan-runtime/src/render/executions.rs b/crates/metal2vulkan-runtime/src/render/executions.rs +index 458c571..e026402 100644 +--- a/crates/metal2vulkan-runtime/src/render/executions.rs ++++ b/crates/metal2vulkan-runtime/src/render/executions.rs +@@ -2094,6 +2094,7 @@ impl RenderExecution { + live: Arc::default(), + log: None, + quiet_bound_values: None, ++ draw_commands: None, + bind_memo: crate::binding::BindMemo::default(), + last_retained: Default::default(), + } +diff --git a/crates/metal2vulkan/src/bind/graphics.rs b/crates/metal2vulkan/src/bind/graphics.rs +index 850f28a..c79f0d4 100644 +--- a/crates/metal2vulkan/src/bind/graphics.rs ++++ b/crates/metal2vulkan/src/bind/graphics.rs +@@ -233,9 +233,9 @@ use std::collections::BTreeMap; + + use crate::bind::heap::{planner, HeapPlanner}; + use crate::bind::instantiate::{ +- bind_buffers, descriptor_range, put, slot_field, BufferExtent, BufferRange, DescriptorBytes, +- DescriptorRange, FieldBytes, IndexBufferRef, IndirectRef, PushConstantWrite, PushHole, +- RangeError, RootTable, ++ bind_buffers_in, descriptor_range, put, slot_field, BoundBuffers, BufferExtent, BufferRange, ++ DescriptorBytes, DescriptorRange, FieldBytes, IndexBufferRef, IndirectRef, PushConstantWrite, ++ PushHole, RangeError, RootTable, + }; + use crate::bind::neutral::{zero_buffer_bytes, NeutralResource}; + use crate::bind::plan::compute::{internal, not_implemented, spec_key, tile_memory}; +@@ -2209,6 +2209,21 @@ impl BindingPlan { + state: &BindState, + draw: &DrawCall, + ) -> Result { ++ let mut out = DrawCommands::empty(); ++ self.instantiate_draw_into(state, draw, &mut out)?; ++ Ok(out) ++ } ++ ++ /// [`instantiate_draw`](Self::instantiate_draw) into `out`, whose vectors this draw's commands ++ /// take (emptied first): a consumer that keeps one `DrawCommands` from draw to draw fills it ++ /// without allocating them again. On a refusal nothing `out` holds is a draw's to record: the ++ /// next call fills it again. ++ pub fn instantiate_draw_into( ++ &self, ++ state: &BindState, ++ draw: &DrawCall, ++ out: &mut DrawCommands, ++ ) -> Result<(), BindError> { + let t = self + .graphics + .as_ref() +@@ -2230,10 +2245,45 @@ impl BindingPlan { + }); + } + } +- instantiate(t, state, draw) ++ instantiate_into(t, state, draw, out) ++ } ++} ++ ++impl DrawCommands { ++ /// No draw's commands, every vector empty: what [`BindingPlan::instantiate_draw_into`] fills. ++ pub fn empty() -> DrawCommands { ++ DrawCommands { ++ push_constants: Vec::new(), ++ descriptor_bytes: Vec::new(), ++ buffer_offsets: Vec::new(), ++ draw: vk::DrawCommand::Draw { ++ vertex_count: 0, ++ instance_count: 0, ++ first_vertex: 0, ++ first_instance: 0, ++ }, ++ indirect_needs_arguments: false, ++ extents: Vec::new(), ++ push_holes: Vec::new(), ++ descriptor_ranges: Vec::new(), ++ root_tables: Vec::new(), ++ vertex_strides: Vec::new(), ++ uses: Vec::new(), ++ index_overrun: None, ++ index_bounds_pass: false, ++ amplifications: Vec::new(), ++ specialization: None, ++ mesh_dispatch: None, ++ } + } + } + ++/// `v`, emptied: its room kept for the draw that takes it. ++fn emptied(mut v: Vec) -> Vec { ++ v.clear(); ++ v ++} ++ + /// The Vulkan draw of a Metal draw, and the base vertex and first instance the + /// `BaseVertexInstance` field reports (Vulkan's `BaseVertex` and `BaseInstance`). A direct + /// indexed draw whose indices run past its index buffer ([`index_overrun`]) draws as `overrun` +@@ -3272,11 +3322,25 @@ fn capture_draw( + } + } + ++#[cfg(test)] + fn instantiate( + t: &GraphicsTemplate, + state: &BindState, + draw: &DrawCall, + ) -> Result { ++ let mut out = DrawCommands::empty(); ++ instantiate_into(t, state, draw, &mut out)?; ++ Ok(out) ++} ++ ++fn instantiate_into( ++ t: &GraphicsTemplate, ++ state: &BindState, ++ draw: &DrawCall, ++ out: &mut DrawCommands, ++) -> Result<(), BindError> { ++ // The vectors `out` held, which this draw's commands take, emptied. ++ let kept = std::mem::replace(out, DrawCommands::empty()); + let overrun = index_overrun(t.robust_index_fetch, draw); + let (command, base) = vk_draw(draw, overrun); + let capture = match t.capture { +@@ -3295,7 +3359,15 @@ fn instantiate( + } + }; + let command = mesh.as_ref().map_or(command, |m| m.command); +- let bound = bind_buffers(&t.buffers, state)?; ++ let bound = bind_buffers_in( ++ &t.buffers, ++ state, ++ BoundBuffers { ++ descriptor_ranges: kept.descriptor_ranges, ++ extents: kept.extents, ++ ..BoundBuffers::default() ++ }, ++ )?; + let mut descriptor_ranges = bound.descriptor_ranges; + descriptor_ranges.extend(crate::bind::instantiate::texel_ranges(&t.resources, state)?); + let tables = crate::bind::heap::table_bytes(&t.resources, state)?; +@@ -3309,10 +3381,10 @@ fn instantiate( + }; + let vertex = state.stages.get(&Stage::Vertex); + +- let mut push_constants = Vec::new(); +- let mut descriptor_bytes = Vec::new(); +- let mut push_holes = Vec::new(); +- let mut root_tables = Vec::new(); ++ let mut push_constants = emptied(kept.push_constants); ++ let mut descriptor_bytes = emptied(kept.descriptor_bytes); ++ let mut push_holes = emptied(kept.push_holes); ++ let mut root_tables = emptied(kept.root_tables); + let mut reads_base = false; + for st in &t.stages { + let (push_start, push_size) = st.push_range.unwrap_or((0, 0)); +@@ -3502,8 +3574,8 @@ fn instantiate( + + // Vertex buffers: offsets, per-bind strides, the address rules, and the bytes the draw's + // vertex and instance ranges fetch. +- let mut buffer_offsets = Vec::new(); +- let mut vertex_strides = Vec::new(); ++ let mut buffer_offsets = emptied(kept.buffer_offsets); ++ let mut vertex_strides = emptied(kept.vertex_strides); + for vb in &t.vertex_buffers { + match vertex.and_then(|v| v.buffers.get(&vb.slot.index)) { + Some(&BoundBuffer::Buffer { +@@ -3595,7 +3667,7 @@ fn instantiate( + Some(m) => (m.dispatch, Some(m.specialization)), + None => (None, None), + }; +- Ok(DrawCommands { ++ *out = DrawCommands { + push_constants, + descriptor_bytes, + buffer_offsets, +@@ -3610,10 +3682,11 @@ fn instantiate( + uses: { + // The indexed capture form reads its indices in the vertex stage, by address, and + // binds no index buffer. +- let mut uses = match capture.as_ref().and_then(|c| c.indices) { +- Some(indices) => vec![indices], +- None => draw_uses(draw, overrun, t.tessellation.as_ref()), +- }; ++ let mut uses = emptied(kept.uses); ++ match capture.as_ref().and_then(|c| c.indices) { ++ Some(indices) => uses.push(indices), ++ None => draw_uses_into(draw, overrun, t.tessellation.as_ref(), &mut uses), ++ } + // A mesh draw that reads its arguments reads 12 bytes; an emulated one's scan reads + // them in the compute stage. + if let vk::DrawCommand::DrawMeshTasksIndirect { .. } = command { +@@ -3637,7 +3710,8 @@ fn instantiate( + amplifications, + mesh_dispatch, + specialization, +- }) ++ }; ++ Ok(()) + } + + /// The `u32` of amplification push field `field` for amplification `a` of a plan drawing +@@ -3890,12 +3964,25 @@ fn indirect_use(len: u64) -> ResourceUse { + /// control-point index offset, and argument offset. A direct draw whose indices run past the buffer + /// (`overrun`) reads the bytes from the index offset to [`IndexBufferRef::fetch_end`] when it is + /// `Robust`, and only the indices it draws when it is `Clamped` (none, from offset 0, when it draws +-/// none). ++/// none). [`draw_uses_into`] appends them to a list. ++#[cfg(test)] + fn draw_uses( + draw: &DrawCall, + overrun: Option, + factors: Option<&FactorTemplate>, + ) -> Vec { ++ let mut uses = Vec::new(); ++ draw_uses_into(draw, overrun, factors, &mut uses); ++ uses ++} ++ ++/// [`draw_uses`], appended to `uses`. ++fn draw_uses_into( ++ draw: &DrawCall, ++ overrun: Option, ++ factors: Option<&FactorTemplate>, ++ uses: &mut Vec, ++) { + let index = |range| ResourceUse { + resource: UseTarget::IndexBuffer, + access: AccessSet::READ, +@@ -3911,7 +3998,6 @@ fn draw_uses( + stages: vk::PipelineStageFlags2::TESSELLATION_CONTROL_SHADER, + range, + }; +- let mut uses = Vec::new(); + if let DrawCall::Patches { + patch_index: Some(_), + .. +@@ -3995,7 +4081,6 @@ fn draw_uses( + } => [Some(index(None)), Some(indirect_use(20))], + }; + uses.extend(own.into_iter().flatten()); +- uses + } + + /// Bytes of one index of `ty`, when it is a fixed-size index type. +diff --git a/crates/metal2vulkan/src/bind/instantiate.rs b/crates/metal2vulkan/src/bind/instantiate.rs +index 62d9d3f..e474e8c 100644 +--- a/crates/metal2vulkan/src/bind/instantiate.rs ++++ b/crates/metal2vulkan/src/bind/instantiate.rs +@@ -469,6 +469,7 @@ pub(crate) fn bound_buffer( + } + + /// What a draw or dispatch binds for the plan's buffer slots. ++#[derive(Default)] + pub(crate) struct BoundBuffers { + /// The range of every descriptor-form slot's buffer or inline bytes. + pub descriptor_ranges: Vec, +@@ -542,14 +543,22 @@ fn check_uniform_aliasing(buffers: &[PlannedBuffer], state: &BindState) -> Resul + pub(crate) fn bind_buffers( + buffers: &[PlannedBuffer], + state: &BindState, ++) -> Result { ++ bind_buffers_in(buffers, state, BoundBuffers::default()) ++} ++ ++/// [`bind_buffers`] into `out`'s vectors, emptied first: a draw that keeps them from the last ++/// (`BindingPlan::instantiate_draw_into`) fills them without allocating. ++pub(crate) fn bind_buffers_in( ++ buffers: &[PlannedBuffer], ++ state: &BindState, ++ mut out: BoundBuffers, + ) -> Result { + check_uniform_aliasing(buffers, state)?; +- let mut out = BoundBuffers { +- descriptor_ranges: Vec::new(), +- extents: Vec::new(), +- residuals: Vec::new(), +- statics: Vec::new(), +- }; ++ out.descriptor_ranges.clear(); ++ out.extents.clear(); ++ out.residuals.clear(); ++ out.statics.clear(); + for pb in buffers { + let bound = bound_buffer(state, pb.slot, pb.bound)?; + let (bind_offset, length) = match bound { diff --git a/bridge/patches/0012-keep-draw-lists.patch b/bridge/patches/0012-keep-draw-lists.patch new file mode 100644 index 0000000..b5a60d8 --- /dev/null +++ b/bridge/patches/0012-keep-draw-lists.patch @@ -0,0 +1,302 @@ +diff --git a/crates/metal2vulkan-runtime/src/binding.rs b/crates/metal2vulkan-runtime/src/binding.rs +index 000794d..df2525f 100644 +--- a/crates/metal2vulkan-runtime/src/binding.rs ++++ b/crates/metal2vulkan-runtime/src/binding.rs +@@ -1457,7 +1457,24 @@ pub(super) fn command_uses( + index: Option<(&Buffer, u64)>, + indirect: Option<(&Buffer, u64)>, + ) -> Result { +- let mut uses = Vec::new(); ++ command_uses_in(Vec::new(), call, plan, command, tables, index, indirect) ++} ++ ++/// [`command_uses`], its uses listed in `room` (emptied first): a list an earlier command's ++/// uses were in ([`CommandUses::into_room`]). ++pub(super) fn command_uses_in( ++ mut room: Vec>, ++ call: &'static str, ++ plan: &BindingPlan, ++ command: &Command<'_>, ++ tables: &Tables<'_>, ++ index: Option<(&Buffer, u64)>, ++ indirect: Option<(&Buffer, u64)>, ++) -> Result { ++ // At most one use for each resource use of the plan and the command. ++ room.clear(); ++ room.reserve(plan.uses.len() + command.uses.len()); ++ let mut uses = room; + // The keys of the buffers made resident the command reaches through the resident set, by + // address; every other use names its resource (a descriptor, a root table's static, a view). + // Those it names are gathered once it reaches one so: until then each use names its key. +@@ -1625,6 +1642,13 @@ impl CommandUses { + pub(super) fn resident(&self) -> &BTreeSet { + &self.resident + } ++ ++ /// The list the uses were in, emptied, for a later command's ([`command_uses_in`]). ++ pub(super) fn into_room(self) -> Vec> { ++ let mut uses = self.uses; ++ uses.clear(); ++ uses ++ } + } + + impl Deref for CommandUses { +diff --git a/crates/metal2vulkan-runtime/src/render.rs b/crates/metal2vulkan-runtime/src/render.rs +index 4400378..110c428 100644 +--- a/crates/metal2vulkan-runtime/src/render.rs ++++ b/crates/metal2vulkan-runtime/src/render.rs +@@ -1123,6 +1123,11 @@ pub struct RenderCommandEncoder { + quiet_bound_values: Option, + /// The last draw's commands, whose lists the next draw's fill again. + draw_commands: Option, ++ /// The list the last draw's uses were in, emptied, for the next draw's ++ /// (`binding::command_uses_in`). ++ uses_room: Vec>, ++ /// The list the last draw's argument samplers were in, emptied (`Device::argument_samplers_into`). ++ argument_samplers_room: Vec, + /// The views and sampler descriptions its draws' binds made or found (`binding::BindMemo`). + bind_memo: binding::BindMemo, + /// What its last draw retained of its own ([`LastRetained`]). +@@ -1394,6 +1399,8 @@ impl CommandBuffer { + log, + quiet_bound_values: None, + draw_commands: None, ++ uses_room: Vec::new(), ++ argument_samplers_room: Vec::new(), + bind_memo: binding::BindMemo::default(), + last_retained: LastRetained::default(), + }) +@@ -5845,7 +5852,10 @@ impl RenderCommandEncoder { + let max = pipeline.base().graphics.max_vertex_amplification_count; + // The device's sampler states, less those at IDs no set-1 sampler array of the variant + // holds (`heap_sets::fit_samplers`). +- let mut argument_samplers = self.core.device().argument_samplers(); ++ let mut argument_samplers = std::mem::take(&mut self.argument_samplers_room); ++ self.core ++ .device() ++ .argument_samplers_into(&mut argument_samplers); + let function_tables = pipeline.function_tables(); + let mut unlisted = None; + let reads_tile_memory = pipeline.reads_tile_memory(); +@@ -6402,7 +6412,8 @@ impl RenderCommandEncoder { + mesh: mesh_work.as_ref().map(mesh::MeshWork::regions), + }; + let (index, indirect) = (buffers.index.as_ref(), buffers.indirect.as_ref()); +- let mut uses = binding::command_uses( ++ let mut uses = binding::command_uses_in( ++ std::mem::take(&mut self.uses_room), + call, + &plan, + &command, +@@ -7311,8 +7322,13 @@ impl RenderCommandEncoder { + } + if capture.is_some() { + self.capture_uses(uses); ++ } else { ++ self.uses_room = uses.into_room(); + } + self.draw_commands = Some(commands); ++ // The states go; the list stays for the next draw's. ++ argument_samplers.clear(); ++ self.argument_samplers_room = argument_samplers; + Ok(()) + } + +diff --git a/crates/metal2vulkan-runtime/src/render/executions.rs b/crates/metal2vulkan-runtime/src/render/executions.rs +index e026402..768c128 100644 +--- a/crates/metal2vulkan-runtime/src/render/executions.rs ++++ b/crates/metal2vulkan-runtime/src/render/executions.rs +@@ -2095,6 +2095,8 @@ impl RenderExecution { + log: None, + quiet_bound_values: None, + draw_commands: None, ++ uses_room: Vec::new(), ++ argument_samplers_room: Vec::new(), + bind_memo: crate::binding::BindMemo::default(), + last_retained: Default::default(), + } +diff --git a/crates/metal2vulkan-runtime/src/resource_ids.rs b/crates/metal2vulkan-runtime/src/resource_ids.rs +index b3dc480..d5881f3 100644 +--- a/crates/metal2vulkan-runtime/src/resource_ids.rs ++++ b/crates/metal2vulkan-runtime/src/resource_ids.rs +@@ -464,6 +464,11 @@ impl ResourceIds { + + /// The live holders of sampler IDs, in ID order: the states a sampler table lists. + pub(super) fn samplers(&self) -> Vec> { ++ self.live_samplers().collect() ++ } ++ ++ /// [`samplers`](Self::samplers), one at a time. ++ pub(super) fn live_samplers(&self) -> impl Iterator> + '_ { + self.samplers + .slots + .values() +@@ -471,7 +476,6 @@ impl ResourceIds { + Holder::Sampler(state) => state.upgrade(), + _ => None, + }) +- .collect() + } + } + +diff --git a/crates/metal2vulkan-runtime/src/sampler.rs b/crates/metal2vulkan-runtime/src/sampler.rs +index 2498ec2..9116a99 100644 +--- a/crates/metal2vulkan-runtime/src/sampler.rs ++++ b/crates/metal2vulkan-runtime/src/sampler.rs +@@ -470,6 +470,13 @@ impl Device { + let states = lock(&self.shared().resource_ids).samplers(); + states.into_iter().map(SamplerState).collect() + } ++ ++ /// [`argument_samplers`](Self::argument_samplers), listed in `out` (emptied first). ++ pub(super) fn argument_samplers_into(&self, out: &mut Vec) { ++ out.clear(); ++ let ids = lock(&self.shared().resource_ids); ++ out.extend(ids.live_samplers().map(SamplerState)); ++ } + } + + impl SamplerState { +diff --git a/crates/metal2vulkan-runtime/src/tracker.rs b/crates/metal2vulkan-runtime/src/tracker.rs +index 0b532a0..58ccac6 100644 +--- a/crates/metal2vulkan-runtime/src/tracker.rs ++++ b/crates/metal2vulkan-runtime/src/tracker.rs +@@ -957,10 +957,54 @@ pub(super) struct EncoderUses { + /// The uses of one part of an encoder. + struct Part { + /// The tracked uses so far, one per range, at most [`ENTRIES_PER_KEY`] per resource. +- tracked: HashMap>>, ++ tracked: HashMap>, + stages: vk::PipelineStageFlags2, + } + ++/// One resource's tracked uses in a part, in the order they were first recorded: the first held ++/// in place, as a part mostly uses a resource over one range. ++enum KeyUses { ++ One(Use), ++ Many(Vec>), ++} ++ ++impl KeyUses { ++ fn as_slice(&self) -> &[Use] { ++ match self { ++ KeyUses::One(used) => std::slice::from_ref(used), ++ KeyUses::Many(uses) => uses, ++ } ++ } ++ ++ fn as_mut_slice(&mut self) -> &mut [Use] { ++ match self { ++ KeyUses::One(used) => std::slice::from_mut(used), ++ KeyUses::Many(uses) => uses, ++ } ++ } ++ ++ /// Add `used` after the others. ++ fn push(&mut self, used: Use) { ++ match self { ++ KeyUses::Many(uses) => uses.push(used), ++ KeyUses::One(_) => { ++ let KeyUses::One(first) = std::mem::replace(self, KeyUses::Many(Vec::new())) else { ++ unreachable!("matched above") ++ }; ++ *self = KeyUses::Many(vec![first, used]); ++ } ++ } ++ } ++ ++ /// The uses, appended to `out` in order. ++ fn append_to(self, out: &mut Vec>) { ++ match self { ++ KeyUses::One(used) => out.push(used), ++ KeyUses::Many(uses) => out.extend(uses), ++ } ++ } ++} ++ + impl Part { + fn new() -> Part { + Part { +@@ -1025,16 +1069,28 @@ impl EncoderUses { + .iter() + .filter(|used| used.tracked && !used.is_nothing()) + { +- let ranges = part.tracked.entry(used.key).or_default(); +- match ranges.iter_mut().find(|known| known.range == used.range) { ++ let ranges = match part.tracked.entry(used.key) { ++ std::collections::hash_map::Entry::Vacant(vacant) => { ++ vacant.insert(KeyUses::One(used.clone())); ++ continue; ++ } ++ std::collections::hash_map::Entry::Occupied(occupied) => occupied.into_mut(), ++ }; ++ match ranges ++ .as_mut_slice() ++ .iter_mut() ++ .find(|known| known.range == used.range) ++ { + Some(known) => { + known.read = known.read.union(used.read); + known.write = known.write.union(used.write); + } + None => ranges.push(used.clone()), + } +- if ranges.len() > ENTRIES_PER_KEY { +- coalesce_uses(ranges); ++ if let KeyUses::Many(ranges) = ranges { ++ if ranges.len() > ENTRIES_PER_KEY { ++ coalesce_uses(ranges); ++ } + } + } + } +@@ -1050,6 +1106,7 @@ impl EncoderUses { + self.parts.iter().any(|part| { + part.tracked.get(&read.key).is_some_and(|known| { + known ++ .as_slice() + .iter() + .any(|k| !k.write.is_empty() && k.range.overlaps(&read.range)) + }) +@@ -1068,7 +1125,9 @@ impl EncoderUses { + self.parts.iter().any(|part| { + part.tracked + .get(&write.key) +- .is_some_and(|known| known.iter().any(|k| k.range.overlaps(&write.range))) ++ .is_some_and(|known| { ++ known.as_slice().iter().any(|k| k.range.overlaps(&write.range)) ++ }) + }) + }) + } +@@ -1077,7 +1136,7 @@ impl EncoderUses { + pub(super) fn tracked_in(&self, stages: vk::PipelineStageFlags2) -> Vec> { + self.parts + .iter() +- .flat_map(|part| part.tracked.values().flatten()) ++ .flat_map(|part| part.tracked.values().flat_map(KeyUses::as_slice)) + .filter(|known| (known.read.stages | known.write.stages).intersects(stages)) + .cloned() + .collect() +@@ -1090,7 +1149,7 @@ impl EncoderUses { + self.parts.iter().any(|part| { + part.tracked + .get(&used.key) +- .is_some_and(|known| known.iter().any(|k| touches(k, used, stages))) ++ .is_some_and(|known| known.as_slice().iter().any(|k| touches(k, used, stages))) + }) + }) + } +@@ -1149,7 +1208,10 @@ impl EncoderUses { + let mut summaries: Vec> = parts + .into_iter() + .map(|part| { +- let mut uses: Vec> = part.tracked.into_values().flatten().collect(); ++ let mut uses: Vec> = Vec::with_capacity(part.tracked.len()); ++ for known in part.tracked.into_values() { ++ known.append_to(&mut uses); ++ } + uses.sort_by_key(|used| used.key); + Summary { + uses, diff --git a/bridge/patches/0013-skip-unchanged-rebinds.patch b/bridge/patches/0013-skip-unchanged-rebinds.patch new file mode 100644 index 0000000..a241f73 --- /dev/null +++ b/bridge/patches/0013-skip-unchanged-rebinds.patch @@ -0,0 +1,390 @@ +diff --git a/crates/metal2vulkan-runtime/README.md b/crates/metal2vulkan-runtime/README.md +index c6dc9f6..f756943 100644 +--- a/crates/metal2vulkan-runtime/README.md ++++ b/crates/metal2vulkan-runtime/README.md +@@ -139,6 +139,7 @@ Vulkan-side choices Metal has no counterpart for: + | `announce_each` | Degradation kinds to announce at every occurrence instead of once. | + | `metal_level` | The macOS release whose Metal the device answers as (the host's by default). | + | `record_executions` | Keep, per command buffer, which pipeline each dispatch and draw ran (`CommandBuffer::executions`). Off by default. | ++| `refuse_rebinds` | Refuse every `rebind_bytes_no_copy` (`RebindProblem::Refused`), so the encoders log no calls to encode again while no-copy buffers live. Off by default. | + + While a handle lives, equal options give the same device for a physical device, as Metal has one + `MTLDevice` per GPU. Nothing is read from the environment but the platform's default cache +diff --git a/crates/metal2vulkan-runtime/src/blit.rs b/crates/metal2vulkan-runtime/src/blit.rs +index 2af641b..9db0e67 100644 +--- a/crates/metal2vulkan-runtime/src/blit.rs ++++ b/crates/metal2vulkan-runtime/src/blit.rs +@@ -160,7 +160,7 @@ impl CommandBuffer { + core: EncoderCore::open(self, "blit_command_encoder", true)?, + pattern: None, + shadows: Vec::new(), +- log: self.device().shared().rebinds.any().then(CallLog::new), ++ log: self.device().shared().logs_calls().then(CallLog::new), + }) + } + } +diff --git a/crates/metal2vulkan-runtime/src/buffer_descriptor.rs b/crates/metal2vulkan-runtime/src/buffer_descriptor.rs +index 902a821..5d07264 100644 +--- a/crates/metal2vulkan-runtime/src/buffer_descriptor.rs ++++ b/crates/metal2vulkan-runtime/src/buffer_descriptor.rs +@@ -539,6 +539,10 @@ impl Device { + if self.is_lost() { + return Err(Error::DeviceLost); + } ++ // Its encoders logged nothing to encode again (`DeviceOptions::refuse_rebinds`). ++ if self.shared().refuse_rebinds { ++ return Err(invalid(call, Problem::Rebind(RebindProblem::Refused))); ++ } + let key = buffer.identity().key; + let encoded = || invalid(call, Problem::Rebind(RebindProblem::EncodedUses)); + if self.rebind_pending(key) { +diff --git a/crates/metal2vulkan-runtime/src/compute.rs b/crates/metal2vulkan-runtime/src/compute.rs +index 0863fd5..92894bd 100644 +--- a/crates/metal2vulkan-runtime/src/compute.rs ++++ b/crates/metal2vulkan-runtime/src/compute.rs +@@ -226,7 +226,7 @@ impl CommandBuffer { + indirect_stage_in: None, + residency: self.inherited_residency(false), + captures: Captures::default(), +- log: self.device().shared().rebinds.any().then(CallLog::new), ++ log: self.device().shared().logs_calls().then(CallLog::new), + dispatches: 0, + }) + } +diff --git a/crates/metal2vulkan-runtime/src/device/adopt.rs b/crates/metal2vulkan-runtime/src/device/adopt.rs +index 73f84ba..4262d65 100644 +--- a/crates/metal2vulkan-runtime/src/device/adopt.rs ++++ b/crates/metal2vulkan-runtime/src/device/adopt.rs +@@ -266,6 +266,8 @@ impl Device { + gpu_times_from_host: raw.gpu_times_from_host, + // An adopted device keeps no execution record: validation makes its own devices. + record_executions: false, ++ // An adopted device's rebinds go as a made device's do by default. ++ refuse_rebinds: false, + announce_each: raw.announce_each, + view_reinterpretation, + generated_commands: false, +diff --git a/crates/metal2vulkan-runtime/src/device/create.rs b/crates/metal2vulkan-runtime/src/device/create.rs +index 76a0d07..77ac8af 100644 +--- a/crates/metal2vulkan-runtime/src/device/create.rs ++++ b/crates/metal2vulkan-runtime/src/device/create.rs +@@ -169,6 +169,14 @@ pub struct DeviceOptions { + /// No Metal counterpart. + #[doc(hidden)] + pub record_executions: bool, ++ /// Refuse every `rebind_bytes_no_copy`, of a buffer and of a heap (`Invalid`, ++ /// [`RebindProblem::Refused`](crate::RebindProblem::Refused)), for a consumer that never moves ++ /// the host pages of what it made with `new_buffer_with_bytes_no_copy` or ++ /// `new_heap_with_bytes_no_copy`. Off by default. The device's compute, blit and render ++ /// encoders then log no calls (`replay.rs`): a log is read only to encode its encoder again at ++ /// a rebind, so a draw pays nothing for a log no one reads. `detach_backing` is unchanged. No ++ /// Metal counterpart. ++ pub refuse_rebinds: bool, + } + + /// One setting of an instance layer (`VkLayerSettingEXT`). +@@ -1248,6 +1256,7 @@ fn create_on( + gpu_times_from_host: options.gpu_times_from_host, + announce_each: options.announce_each.clone(), + record_executions: options.record_executions, ++ refuse_rebinds: options.refuse_rebinds, + view_reinterpretation, + generated_commands, + host_image_copy, +@@ -1356,6 +1365,7 @@ fn same_options(a: &DeviceOptions, b: &DeviceOptions) -> bool { + announce_each, + metal_level, + record_executions, ++ refuse_rebinds, + } = a; + *policy == b.policy + && *layers == b.layers +@@ -1374,6 +1384,7 @@ fn same_options(a: &DeviceOptions, b: &DeviceOptions) -> bool { + && *announce_each == b.announce_each + && *metal_level == b.metal_level + && *record_executions == b.record_executions ++ && *refuse_rebinds == b.refuse_rebinds + } + + /// The group of `made` for `options`: the live one, or a new one on a new instance. Its index. +diff --git a/crates/metal2vulkan-runtime/src/device/mod.rs b/crates/metal2vulkan-runtime/src/device/mod.rs +index 8842010..81304a0 100644 +--- a/crates/metal2vulkan-runtime/src/device/mod.rs ++++ b/crates/metal2vulkan-runtime/src/device/mod.rs +@@ -366,6 +366,9 @@ pub(super) struct DeviceShared { + /// Its command buffers keep execution records (`DeviceOptions::record_executions`): where + /// not, its encoders build none (`EncoderCore::executed`). + pub(crate) record_executions: bool, ++ /// It refuses every rebind (`DeviceOptions::refuse_rebinds`): its encoders log no calls ++ /// ([`DeviceShared::logs_calls`]). ++ pub(crate) refuse_rebinds: bool, + /// Counts the times an image of the device let its views go, under its views lock (a + /// rebind's or a storage form's new `VkImage`, a detach's stand-in, a reattach): a view kept + /// by what it was made of (`binding::BindMemo`) is current while this holds. +@@ -373,6 +376,13 @@ pub(super) struct DeviceShared { + } + + impl DeviceShared { ++ /// Whether an encoder opened now logs its calls (`replay.rs`): where the device has a buffer ++ /// over host pages, unless it refuses every rebind, the only reader of a log ++ /// (`DeviceOptions::refuse_rebinds`). ++ pub(crate) fn logs_calls(&self) -> bool { ++ !self.refuse_rebinds && self.rebinds.any() ++ } ++ + /// What `create` makes with the `VkPipelineCache` it is handed, its `vkCreate*Pipelines` call: + /// a cache of the making's own, seeded from the record `making` names + /// (`ShaderCache::make_pipelines`, `shader_cache/driver.rs`). On a pool's thread the exit waits +@@ -754,6 +764,8 @@ pub(super) struct Parts { + pub(super) announce_each: BTreeSet, + /// Its command buffers keep execution records (`DeviceOptions::record_executions`). + pub(super) record_executions: bool, ++ /// It refuses every rebind (`DeviceOptions::refuse_rebinds`). ++ pub(super) refuse_rebinds: bool, + /// Its views may reinterpret an image's components ([`DeviceShared`]'s field), outside + /// `enabled`'s vocabulary. + pub(super) view_reinterpretation: bool, +@@ -821,6 +833,7 @@ impl Device { + gpu_times_from_host, + announce_each, + record_executions, ++ refuse_rebinds, + view_reinterpretation, + generated_commands, + host_image_copy, +@@ -1219,6 +1232,7 @@ impl Device { + properties, + announced: Announced::new(&announce_each), + record_executions, ++ refuse_rebinds, + views_epoch: AtomicU64::new(0), + device_address, + upload_alignment, +diff --git a/crates/metal2vulkan-runtime/src/rebind.rs b/crates/metal2vulkan-runtime/src/rebind.rs +index 2bce8e6..e8a8d2c 100644 +--- a/crates/metal2vulkan-runtime/src/rebind.rs ++++ b/crates/metal2vulkan-runtime/src/rebind.rs +@@ -77,6 +77,9 @@ pub enum RebindProblem { + /// a resource placed in it, or has an encoder open that used one (or one committed whose + /// commands the runtime records only once earlier work completed): commit or drop it first. + EncodedUses, ++ /// The device refuses every rebind (`DeviceOptions::refuse_rebinds`): its encoders log no ++ /// calls to encode again. ++ Refused, + } + + /// The device's buffers over host pages, by their hazard-tracking key, with the command buffers +@@ -721,7 +724,8 @@ impl Buffer { + /// (`NotImplemented(RebindTextures)`: the texture's copies, views and aliasing image are + /// planned at that offset), or where the device cannot make a texture's aliasing image again + /// over the new memory (`NotImplemented(RebindTextures)`, which says when); memory the device +- /// cannot import exactly, as `new_buffer_with_bytes_no_copy` refuses it; a lost device. ++ /// cannot import exactly, as `new_buffer_with_bytes_no_copy` refuses it; a lost device; a ++ /// device made with `DeviceOptions::refuse_rebinds` (`Invalid`, [`RebindProblem::Refused`]). + /// + /// # Safety + /// +@@ -777,7 +781,8 @@ impl Heap { + /// [`RebindProblem::EncodedUses`]); memory whose first byte is off the import's unit + /// (`Invalid`, `HeapProblem::HostMemoryAlignment`); memory the device cannot import exactly; + /// an image the device cannot make again there (as `new_texture_at_offset` refuses it); a +- /// lost device. ++ /// lost device; a device made with `DeviceOptions::refuse_rebinds` (`Invalid`, ++ /// [`RebindProblem::Refused`]). + /// + /// # Safety + /// +diff --git a/crates/metal2vulkan-runtime/src/render.rs b/crates/metal2vulkan-runtime/src/render.rs +index 110c428..5cd377c 100644 +--- a/crates/metal2vulkan-runtime/src/render.rs ++++ b/crates/metal2vulkan-runtime/src/render.rs +@@ -1273,8 +1273,7 @@ impl CommandBuffer { + let log = match again { + None => device + .shared() +- .rebinds +- .any() ++ .logs_calls() + .then(|| RenderLog::new(descriptor)), + Some(_) => None, + }; +diff --git a/crates/metal2vulkan-runtime/src/replay.rs b/crates/metal2vulkan-runtime/src/replay.rs +index f514934..d39f82d 100644 +--- a/crates/metal2vulkan-runtime/src/replay.rs ++++ b/crates/metal2vulkan-runtime/src/replay.rs +@@ -13,7 +13,8 @@ + //! over where their buffers lie now. A call refused at its first encoding is refused again, + //! which leaves the encoder as it was encoded first ([`Replay::run`] fails, and the encoder is + //! not encoded again). The log starts at the encoder's open where the device has a buffer over +-//! host pages (`Rebinds::any`); else nothing is logged. It costs one push per ++//! host pages (`Rebinds::any`) and does not refuse every rebind (`DeviceOptions::refuse_rebinds`, ++//! `DeviceShared::logs_calls`); else nothing is logged. It costs one push per + //! call; a slice of buffers, offsets, textures or bytes goes into one of the log's arenas, so + //! the common calls allocate nothing of their own. + //! - **What is kept.** The command buffer keeps an ended encoder's log only where a command of +diff --git a/crates/metal2vulkan-runtime/src/tests/mod.rs b/crates/metal2vulkan-runtime/src/tests/mod.rs +index 998d7f8..f579adc 100644 +--- a/crates/metal2vulkan-runtime/src/tests/mod.rs ++++ b/crates/metal2vulkan-runtime/src/tests/mod.rs +@@ -97,6 +97,8 @@ mod private_properties; + mod queue; + mod range; + mod rate_map; ++#[cfg(any(m2v_gpu, m2v_cpu_icd))] ++mod refuse_rebinds; + mod resource; + mod resource_ids; + mod rotated; +diff --git a/crates/metal2vulkan-runtime/src/tests/refuse_rebinds.rs b/crates/metal2vulkan-runtime/src/tests/refuse_rebinds.rs +new file mode 100644 +index 0000000..07ea7e2 +--- /dev/null ++++ b/crates/metal2vulkan-runtime/src/tests/refuse_rebinds.rs +@@ -0,0 +1,147 @@ ++//! A device made with `DeviceOptions::refuse_rebinds`: its encoders log no calls while it has a ++//! buffer over host pages (`replay.rs`, `DeviceShared::logs_calls`), and it refuses a rebind ++//! (`Invalid`, `RebindProblem::Refused`), dropping the deallocator unrun; a device made without it ++//! logs while it has one, as before. ++//! ++//! A device test: compiled only under the validation crate's GPU safety cfgs (`m2v_gpu`, ++//! `m2v_cpu_icd`), on the kind of device the cfg selects. ++ ++use crate::{ ++ Buffer, Deallocator, Device, DeviceKind, DeviceOptions, Error, HostMemory, Invalid, Problem, ++ RebindProblem, ResourceOptions, ShaderCacheOptions, ++}; ++use std::alloc::Layout; ++use std::path::{Path, PathBuf}; ++use std::ptr::NonNull; ++use std::sync::atomic::{AtomicUsize, Ordering}; ++use std::sync::Arc; ++use std::time::{Duration, Instant}; ++ ++/// The no-copy buffers' bytes and alignment. ++const NO_COPY: usize = 64 << 10; ++const NO_COPY_ALIGN: usize = 64 << 10; ++ ++/// The longest a released buffer's deallocator may take to run. ++const WAIT: Duration = Duration::from_secs(10); ++ ++/// A scratch directory for the device's shader cache, removed when dropped. ++struct Scratch(PathBuf); ++ ++impl Scratch { ++ fn new(name: &str) -> Scratch { ++ let path = std::env::temp_dir().join(format!( ++ "m2v-refuse-rebinds-{name}-{}", ++ std::process::id() ++ )); ++ std::fs::create_dir_all(&path).unwrap(); ++ Scratch(path) ++ } ++} ++ ++impl Drop for Scratch { ++ fn drop(&mut self) { ++ let _ = std::fs::remove_dir_all(&self.0); ++ } ++} ++ ++/// The device the cfg selects, refusing rebinds where `refuse`, its shader cache in `cache`. ++fn device(cache: &Path, refuse: bool) -> Device { ++ let options = DeviceOptions { ++ kinds: match cfg!(m2v_cpu_icd) { ++ true => vec![DeviceKind::Cpu], ++ false => vec![ ++ DeviceKind::Discrete, ++ DeviceKind::Integrated, ++ DeviceKind::Virtual, ++ ], ++ }, ++ shader_cache: ShaderCacheOptions { ++ directory: Some(cache.to_owned()), ++ ..ShaderCacheOptions::default() ++ }, ++ refuse_rebinds: refuse, ++ ..DeviceOptions::default() ++ }; ++ Device::system_default_with(&options).unwrap() ++} ++ ++/// Memory of the test's, zeroed, and the deallocator that frees it, counting its runs in `runs`. ++fn memory(runs: &Arc) -> (NonNull, Deallocator) { ++ let layout = Layout::from_size_align(NO_COPY, NO_COPY_ALIGN).unwrap(); ++ // SAFETY: the layout's size is not zero. ++ let memory = NonNull::new(unsafe { std::alloc::alloc_zeroed(layout) }).unwrap(); ++ let runs = Arc::clone(runs); ++ let deallocator: Deallocator = Box::new(move |pointer, _| { ++ // SAFETY: the pointer is the memory allocated above with `layout`, freed once. ++ unsafe { std::alloc::dealloc(pointer.as_ptr(), layout) }; ++ runs.fetch_add(1, Ordering::SeqCst); ++ }); ++ (memory, deallocator) ++} ++ ++/// A `Shared` buffer over memory of the test's, whose deallocator counts into `runs`, and the ++/// memory. ++fn no_copy(device: &Device, runs: &Arc) -> (Buffer, NonNull) { ++ let (memory, deallocator) = memory(runs); ++ // SAFETY: the memory is the test's, mapped and writable, aligned to 64 KiB, until the ++ // deallocator frees it once the buffer's last reference goes. ++ let buffer = unsafe { ++ device.new_buffer_with_bytes_no_copy( ++ HostMemory::new(memory), ++ NO_COPY as u64, ++ ResourceOptions::shared(), ++ Some(deallocator), ++ ) ++ } ++ .unwrap(); ++ (buffer, memory) ++} ++ ++/// Wait for `runs` to reach one. ++#[track_caller] ++fn ran_once(runs: &AtomicUsize) { ++ let start = Instant::now(); ++ while runs.load(Ordering::SeqCst) == 0 && start.elapsed() < WAIT { ++ std::thread::sleep(Duration::from_millis(5)); ++ } ++ assert_eq!(runs.load(Ordering::SeqCst), 1, "the deallocator's runs"); ++} ++ ++/// Without the option, a device with a no-copy buffer logs its encoders' calls; with it, it logs ++/// none, and refuses the buffer's rebind without running the new memory's deallocator (that ++/// memory leaks), the buffer still over its own memory, which is freed when the buffer goes. ++#[test] ++fn a_device_that_refuses_rebinds_logs_no_calls() { ++ let scratch = Scratch::new("default"); ++ let plain = device(&scratch.0, false); ++ assert!(!plain.shared().logs_calls(), "no buffer over host pages yet"); ++ let plain_runs = Arc::new(AtomicUsize::new(0)); ++ let (kept, _) = no_copy(&plain, &plain_runs); ++ assert!(plain.shared().logs_calls()); ++ drop(kept); ++ ran_once(&plain_runs); ++ ++ let scratch = Scratch::new("refusing"); ++ let refusing = device(&scratch.0, true); ++ let runs = Arc::new(AtomicUsize::new(0)); ++ let (buffer, own) = no_copy(&refusing, &runs); ++ assert!(refusing.shared().rebinds.any()); ++ assert!(!refusing.shared().logs_calls()); ++ let new_runs = Arc::new(AtomicUsize::new(0)); ++ let (new, deallocator) = memory(&new_runs); ++ // SAFETY: the new memory holds the buffer's length; the call is refused and keeps nothing. ++ let rebound = ++ unsafe { buffer.rebind_bytes_no_copy(HostMemory::new(new), Some(deallocator), None) }; ++ assert_eq!( ++ rebound, ++ Err(Error::Invalid(Invalid::new( ++ "rebind_bytes_no_copy", ++ Problem::Rebind(RebindProblem::Refused) ++ ))) ++ ); ++ assert_eq!(new_runs.load(Ordering::SeqCst), 0); ++ assert_eq!(buffer.contents(), Some(own)); ++ drop(buffer); ++ ran_once(&runs); ++ assert_eq!(new_runs.load(Ordering::SeqCst), 0); ++} diff --git a/bridge/patches/0014-map-tracker-keys.patch b/bridge/patches/0014-map-tracker-keys.patch new file mode 100644 index 0000000..6190f2e --- /dev/null +++ b/bridge/patches/0014-map-tracker-keys.patch @@ -0,0 +1,66 @@ +diff --git a/crates/metal2vulkan-runtime/src/tracker.rs b/crates/metal2vulkan-runtime/src/tracker.rs +index 58ccac6..8350242 100644 +--- a/crates/metal2vulkan-runtime/src/tracker.rs ++++ b/crates/metal2vulkan-runtime/src/tracker.rs +@@ -909,7 +909,7 @@ fn coalesce(entries: &mut Vec) { + /// The uses one per key and range, sorted by key, without the ones that touch nothing. + fn merged(uses: impl IntoIterator>) -> Vec> { + let mut merged: Vec> = Vec::new(); +- let mut index: HashMap<(S, UseRange), usize> = HashMap::new(); ++ let mut index: KeyMap<(S, UseRange), usize> = KeyMap::default(); + for used in uses { + if used.is_nothing() { + continue; +@@ -945,7 +945,7 @@ pub(super) struct EncoderUses { + /// Whether Metal runs the commands in encoded order (blit encoders, serial compute encoders). + ordered: bool, + /// The accesses of the current part's commands so far, for that order. +- inside: HashMap, Vec>, ++ inside: KeyMap, Vec>, + /// The parts, in order: the last is the current one. Never empty. + parts: Vec>, + /// The fences waited for: before the encoder's first part. +@@ -957,7 +957,7 @@ pub(super) struct EncoderUses { + /// The uses of one part of an encoder. + struct Part { + /// The tracked uses so far, one per range, at most [`ENTRIES_PER_KEY`] per resource. +- tracked: HashMap>, ++ tracked: KeyMap>, + stages: vk::PipelineStageFlags2, + } + +@@ -1008,7 +1008,7 @@ impl KeyUses { + impl Part { + fn new() -> Part { + Part { +- tracked: HashMap::new(), ++ tracked: KeyMap::default(), + stages: vk::PipelineStageFlags2::NONE, + } + } +@@ -1019,7 +1019,7 @@ impl EncoderUses { + pub(super) fn new(ordered: bool) -> EncoderUses { + EncoderUses { + ordered, +- inside: HashMap::new(), ++ inside: KeyMap::default(), + parts: vec![Part::new()], + waits: Vec::new(), + updates: Vec::new(), +@@ -1247,14 +1247,14 @@ impl fmt::Debug for EncoderUses { + /// commands after them. One entry per range, at most [`ENTRIES_PER_KEY`] per resource, merged + /// as [`EncoderUses`] merges its own, which claims more and stays exact. + pub(super) struct UnorderedWrites { +- writes: HashMap>>, ++ writes: KeyMap>>, + } + + impl UnorderedWrites { + /// No writes. + pub(super) fn new() -> UnorderedWrites { + UnorderedWrites { +- writes: HashMap::new(), ++ writes: KeyMap::default(), + } + } + diff --git a/bridge/patches/0015-two-plane-shadow-as-memory-lies.patch b/bridge/patches/0015-two-plane-shadow-as-memory-lies.patch new file mode 100644 index 0000000..b6e419f --- /dev/null +++ b/bridge/patches/0015-two-plane-shadow-as-memory-lies.patch @@ -0,0 +1,42 @@ +diff --git a/crates/metal2vulkan-runtime/src/texture.rs b/crates/metal2vulkan-runtime/src/texture.rs +index 6e86ce4..11e9b7c 100644 +--- a/crates/metal2vulkan-runtime/src/texture.rs ++++ b/crates/metal2vulkan-runtime/src/texture.rs +@@ -3288,7 +3288,8 @@ impl Device { + /// its storage mode: a heap texture's in memory of its own where its bytes in the heap are no + /// layout of its texels (`texture/heap.rs`, `AliasRoute::Lossless`), as a heap's textures hold + /// what their bytes held; and the shadow of a texture over a buffer whose copy in writes every +- /// texel of every aspect before any command reads it (`storage_over_buffer`). ++ /// texel of every aspect before any command reads it (`storage_over_buffer`, and a two-plane ++ /// texture's over a surface's planes, `texture/surface_two_planes.rs`). + fn new_image_as_it_lies( + &self, + plan: ImagePlan, +diff --git a/crates/metal2vulkan-runtime/src/texture/surface_two_planes.rs b/crates/metal2vulkan-runtime/src/texture/surface_two_planes.rs +index 44931f5..3042919 100644 +--- a/crates/metal2vulkan-runtime/src/texture/surface_two_planes.rs ++++ b/crates/metal2vulkan-runtime/src/texture/surface_two_planes.rs +@@ -58,6 +58,7 @@ use crate::degradation::Degradation; + use crate::error::{Error, RuntimeGap}; + use crate::image_plan::plan_image; + use crate::linear_plan::{BufferLayout, LINEAR_ALIGNMENT}; ++use crate::resource::Identity; + use crate::shadow::{ShadowBuffer, ShadowCopies, ShadowSource, ShadowTarget}; + use crate::texture_descriptor::{texture_usage, TextureDescriptor, TextureLayout}; + use crate::tracker::UseRange; +@@ -206,7 +207,14 @@ impl Device { + return Err(Error::NotImplemented(RuntimeGap::Textures)); + } + let hazard = texels.options().hazard_tracking_mode()?; +- let image = self.new_image(plan, layout, descriptor, hazard)?; ++ // Its copy in writes every texel of its one aspect before any command reads it, or a pass ++ // that clears it whole does (`shadow.rs`, `render/shadows.rs`), so it starts as its memory ++ // lies, as the other shadows of a buffer's bytes do (`storage_over_buffer`): zeros there ++ // would never be read, and the host's zeros copied into a full-screen surface's image ++ // were most of its making on a phone's render server. ++ let identity = Identity::new(hazard, false); ++ let image = ++ self.new_image_as_it_lies(plan, layout, descriptor, identity, Device::runtime_owner)?; + let (raw, start) = texels.buffer.at(color.offset); + let staging = HeapPlaneCopies::staging(self, &planes, start, hazard)?; + let moves = Box::new(HeapPlaneCopies::new(self, &staging, &planes, hazard)?); From 84be7171da29f03951eff2725474369cc7ed41af Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Mon, 26 Oct 2026 00:59:08 +0800 Subject: [PATCH 23/92] fix(bridge): keep tile stores within a written texture's rows A tile store writes whole 16x4 blocks, up to 15 texels past a row and 3 rows past the last. A written padded texture aliases its buffer only where those blocks end within the row pitch and the buffer; otherwise it is a shadow. The 3048x2032 framebuffer at 12288-byte rows still aliases. --- .../0016-tile-stores-within-rows.patch | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 bridge/patches/0016-tile-stores-within-rows.patch diff --git a/bridge/patches/0016-tile-stores-within-rows.patch b/bridge/patches/0016-tile-stores-within-rows.patch new file mode 100644 index 0000000..42c71f3 --- /dev/null +++ b/bridge/patches/0016-tile-stores-within-rows.patch @@ -0,0 +1,71 @@ +diff --git a/crates/metal2vulkan-runtime/src/linear_plan.rs b/crates/metal2vulkan-runtime/src/linear_plan.rs +index fdb8233..acf8ae7 100644 +--- a/crates/metal2vulkan-runtime/src/linear_plan.rs ++++ b/crates/metal2vulkan-runtime/src/linear_plan.rs +@@ -766,6 +766,26 @@ pub(super) fn only_reads(usage: TextureUsage) -> bool { + ) + } + ++/// Whether a render pass that keeps a texture `width` texels wide over `layout`'s texels in tile ++/// memory stores it back within the bytes an image over them may hold, in a buffer of ++/// `buffer_length` bytes: a tile store reaching the texture's right or bottom edge writes whole ++/// 16x4-texel blocks, so up to 15 texels past a row's last and 3 rows past its last row. Every ++/// row's block must end within its pitch, and the last block within the buffer; one slice only. ++pub(super) fn tile_store_fits(layout: &BufferLayout, width: u64, buffer_length: u64) -> bool { ++ const TILE: (u64, u64) = (16, 4); ++ if layout.slices != 1 || width == 0 || !layout.row_bytes.is_multiple_of(width) { ++ return false; ++ } ++ let block_row = width.next_multiple_of(TILE.0) * (layout.row_bytes / width); ++ let last_row = layout.rows.next_multiple_of(TILE.1) - 1; ++ layout.row_pitch >= block_row ++ && layout ++ .offset ++ .saturating_add(layout.row_pitch.saturating_mul(last_row)) ++ .saturating_add(block_row) ++ <= buffer_length ++} ++ + /// What an image over `layout`'s texels may hold beside them, for a texture of Metal `usage` (as + /// [`only_reads`] takes it) under `policy`, or why no image may hold them: the texels alone + /// where its rows (and slices) lie one after another, or it has one row (one slice); else, where +diff --git a/crates/metal2vulkan-runtime/src/texture.rs b/crates/metal2vulkan-runtime/src/texture.rs +index c3356f3..071beb8 100644 +--- a/crates/metal2vulkan-runtime/src/texture.rs ++++ b/crates/metal2vulkan-runtime/src/texture.rs +@@ -83,8 +83,8 @@ use super::image_plan::{ + ImagePlan, ImageRefusal, + }; + use super::linear_plan::{ +- aliases, check_linear_limits, holding, linear_lack, BufferLayout, Holds, LinearImage, +- LinearLack, ++ aliases, check_linear_limits, holding, linear_lack, tile_store_fits, BufferLayout, Holds, ++ LinearImage, LinearLack, + }; + use super::memory::{MemoryClass, Placement}; + use super::metal_texture::{self, MetalOver}; +@@ -121,7 +121,7 @@ use first_move::{FirstMove, Fresh, Pattern, Patterned, TexelCopy, PATTERN_BYTES} + use heap_tiled::{FreshDepth, FreshPattern}; + use metal2vulkan::bind::{ImageViewDesc, TextureFacts}; + use metal2vulkan::device::{ +- DeviceDescription, DeviceProfile, DriverFact, FormatSupport, VkFeature, ++ DeviceDescription, DeviceProfile, DriverFact, FormatSupport, PaddedReadsPolicy, VkFeature, + }; + use metal2vulkan::error::{ + Capability, Feature, LinearNeed, RuntimeDriverDefect, SparseNeed, Unsupported, +@@ -1961,7 +1961,14 @@ impl Device { + })?; + // What an image over the texels may hold beside them: the bytes between padded rows too + // where the texture only reads, by the policy (`linear_plan::holding`); else why none may. +- let policy = self.compile_device().policy().padded_reads; ++ let mut policy = self.compile_device().policy().padded_reads; ++ // A written texture's tile stores may reach past its rows (`linear_plan::tile_store_fits`): ++ // where they would leave the row gaps or the buffer, only a texture that reads aliases. ++ if policy == PaddedReadsPolicy::AliasWrites ++ && !tile_store_fits(&texels.layout, u64::from(layout.extent[0]), texels.buffer.length()) ++ { ++ policy = PaddedReadsPolicy::Alias; ++ } + let holds = holding(&texels.layout, usage, policy); + // Over bytes detached from their pages (`detach.rs`) no image aliases them: the texture + // is their shadow, which presents a stand-in until they lie in pages again. From f875e531f052c2e89d1be000dafd043ad7371736 Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Mon, 26 Oct 2026 00:59:08 +0800 Subject: [PATCH 24/92] perf(plugin): keep each device answer that takes an argument QuartzCore asks the device thousands of questions a second while the screen moves, about 140 us each. Every answer is a fixed property of the device, so each (selector, argument) is asked once. --- NativeBridgeMetalFrontend.mm | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index ba62e1e..a4e4c01 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -1793,14 +1793,22 @@ static bool pipelineArchives(NSArray *list,std::vector &arch @end // The device queries the runtime answers (nmb_device_answer_selector), as methods of the device class. One without an argument is read once -// and installed only where the device has an answer of its own; one with an argument asks the runtime each call, and a refusal answers 0/NO -// with the reason logged. +// and installed only where the device has an answer of its own; one with an argument asks the runtime once per argument and keeps the answer +// (every answer is a fixed property of the device; QuartzCore asks thousands a second while the screen moves), and a refusal answers 0/NO +// with the reason logged once. static std::unordered_map gAnswers; static NSUInteger countAnswer(id,SEL cmd) {return (NSUInteger)gAnswers.at(cmd);} static BOOL flagAnswer(id,SEL cmd) {return gAnswers.at(cmd)!=0;} static uint64_t askAnswer(SEL cmd,NSUInteger arg) { + static std::mutex lock; + static std::unordered_map> kept; + {std::lock_guard hold(lock); + auto answers=kept.find(cmd); + if(answers!=kept.end()){auto answer=answers->second.find(arg);if(answer!=answers->second.end())return answer->second;}} uint64_t value=0; - if(!nmb_device_answer(gDevice,sel_getName(cmd),arg,&value)){NBLOG(@"native-metal: %s(%lu): %@",sel_getName(cmd),(unsigned long)arg,lastError());return 0;} + if(!nmb_device_answer(gDevice,sel_getName(cmd),arg,&value)){NBLOG(@"native-metal: %s(%lu): %@",sel_getName(cmd),(unsigned long)arg,lastError());value=0;} + std::lock_guard hold(lock); + kept[cmd][arg]=value; return value; } static NSUInteger countArgAnswer(id,SEL cmd,NSUInteger arg) {return (NSUInteger)askAnswer(cmd,arg);} From 39ba232a79f031c73c77cd9a8f66fe4fb7887b09 Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Mon, 26 Oct 2026 00:59:08 +0800 Subject: [PATCH 25/92] perf(plugin): let Turnip choose tile or system memory per pass TU_DEBUG is nocb only: concurrent binning stays off, and autotune picks GMEM or sysmem for each pass. i553 forced sysmem for every pass and the GPU was 19 % busy. i555 ran 600 s without a stall at 70 fps p50. --- NativeAdrenoMetalPlugin.mm | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/NativeAdrenoMetalPlugin.mm b/NativeAdrenoMetalPlugin.mm index 00134f3..d504a06 100644 --- a/NativeAdrenoMetalPlugin.mm +++ b/NativeAdrenoMetalPlugin.mm @@ -220,12 +220,11 @@ static id deviceForPort(unsigned port) { if (tried) return device; tried = true; exceptiondiag::start(); // the throw site of an uncaught exception, for the kernel's THROW row (ExceptionDiag.h) - // Every Metal process renders in system memory: Turnip's GMEM passes hand work between the BV and BR threads, and with IFPC off - // and a context reset on every client switch the two still stopped waiting on each other inside a pass (int-261p, int-262: GPU idle, - // BR one IB ahead, BV inside a draw IB2). int-257's nobin covered the render server only. System memory alone still let BV run - // ahead of BR across passes (concurrent binning is emitted for sysmem passes too) and it hung the same way in int-265: no - // concurrent binning either. - setenv("TU_DEBUG", "sysmem,nocb", 0); + // No concurrent binning in any Metal process: with it BV ran ahead of BR and the two stopped waiting on each other inside a pass + // (int-261p, int-262, and in system memory int-265). Without it Turnip's autotune picks tile memory (GMEM) or system memory per + // pass: i553 forced system memory for every pass, the GPU busy 19 % at 680 MHz. A tile store writes whole 16x4 blocks, which the + // runtime keeps within a texture's rows (bridge patch 0016). + setenv("TU_DEBUG", "nocb", 0); if (strcmp(getprogname(), "backboardd") != 0) return device = clientDevice(port); // The discovery releases the accelerator port when it is done; the monitor and the scanout client keep using it. if (void *iokit = dlopen("/System/Library/Frameworks/IOKit.framework/IOKit", RTLD_NOW | RTLD_LOCAL)) { From 3b4acf15eb1f59930087c765a00a76e45349bd3b Mon Sep 17 00:00:00 2001 From: bluseliu50 Date: Wed, 28 Oct 2026 08:04:59 +0800 Subject: [PATCH 26/92] wip: GPU scanout half-float conversion, zero-rest pipelines, iOS 27 surface flush --- GpuScanoutClient.h | 5 +- GpuScanoutPresenter.h | 67 +++++++++++++++++++- NativeAdrenoMetalPlugin.mm | 47 +++++++------- NativeBridgeMetalFrontend.mm | 102 +++++++++++++++++++++++++------ bridge/NativeMetalObjects.h | 4 ++ bridge/NativeMetalObjects.rs | 47 ++++++++++++++ bridge/RustPacBoundary.c | 115 +++++++++++++++++++++++++++++++++++ bridge/scanout_host.mm | 70 +++++++++++++++++++++ shaders/scanout_convert.ll | 98 +++++++++++++++++++++++++++++ 9 files changed, 510 insertions(+), 45 deletions(-) create mode 100644 bridge/RustPacBoundary.c create mode 100644 bridge/scanout_host.mm create mode 100644 shaders/scanout_convert.ll diff --git a/GpuScanoutClient.h b/GpuScanoutClient.h index d40b0bb..7b259b5 100644 --- a/GpuScanoutClient.h +++ b/GpuScanoutClient.h @@ -10,7 +10,6 @@ #include #include #include "GpuScanoutPresenter.h" -#include "../NativeMobileDisplay/NativeGOPWire.h" extern "C" void NBWaitInFlight(uint64_t beforeUs) __attribute__((weak_import)); extern "C" void NBTraceSwap(uint64_t offered,uint64_t copied,uint64_t completed,uint32_t sequence,uint32_t result) __attribute__((weak_import)); @@ -92,7 +91,7 @@ public: if (NBWaitInFlight) NBWaitInFlight(offeredUs); const uint64_t began = mach_absolute_time(); const gpupresent::Result r = presenter_->present({(const void *)(uintptr_t)swap.address, (size_t)((swap.bytes + page - 1) & ~(page - 1)), - (size_t)swap.stride, swap.width, swap.height, swap.reserved}); + (size_t)swap.stride, swap.width, swap.height, swap.reserved, swap.format}); { static mach_timebase_info_data_t base; if (!base.denom) mach_timebase_info(&base); @@ -100,7 +99,7 @@ public: stats_->totalMs += stats_->lastMs; } stats_->lastResult = (int)r; - if (r == gpupresent::kOk && stats_->verified < 4) verify(swap); + if (r == gpupresent::kOk && swap.format == NGP_BGRX8888 && stats_->verified < 4) verify(swap); note(0x6731); // the GPU copy returned const uint64_t copiedUs = clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000; NGPComplete done = {swap.sequence, r == gpupresent::kOk ? 0u : 0xe00002bcu /* kIOReturnError */, 0}; diff --git a/GpuScanoutPresenter.h b/GpuScanoutPresenter.h index 945301c..bdc8ff9 100644 --- a/GpuScanoutPresenter.h +++ b/GpuScanoutPresenter.h @@ -1,7 +1,9 @@ // SPDX-License-Identifier: MIT // GPU presentation: the GPU copies a finished BGRA frame (the compositor's IOSurface pages) into the firmware scanout memory. Both are // imported without a copy (newBufferWithBytesNoCopy: pinned by the kernel and mapped into the GPU's 4 KiB context), the copy is a blit on -// the GPU's queue, and the call returns after the GPU retired it. No CPU touches a pixel; there is no staging copy and no software fallback +// the GPU's queue, and the call returns after the GPU retired it. A half-float frame (RGBA 16F, what iPadOS 27's render server draws +// once a Metal device renders) is converted by a compute kernel (shaders/scanout_convert.ll) through the display driver's own table of +// codes (NativeHalfCodes.h), so the bytes match its CPU path. No CPU touches a pixel; there is no staging copy and no software fallback // here. Used through the public Metal API, on any device (the native frontend on the tablet, the Mac's device for the host check). #pragma once #import @@ -9,6 +11,12 @@ #include #include #include +#include "../GopFramebuffer/NativeGOPWire.h" +#include "../GopFramebuffer/NativeHalfCodes.h" + +// The conversion kernel's AIR and the frontend's loader for bare AIR (both in the plugin; weak, so a device without them only blits). +extern "C" const void *nmb_scanout_convert_air(size_t *length) __attribute__((weak_import)); +extern "C" id native_bridge_metal_library_with_air(const void *bytes, size_t length) __attribute__((weak_import)); namespace gpupresent { @@ -28,6 +36,7 @@ struct Frame { size_t stride; uint32_t width, height; uint32_t mapping; // the kernel's id for this mapping of the surface (0: none, imported for this frame only) + uint32_t format; // NGP_BGRX8888, NGP_RGBA16F_LINEAR or NGP_RGBA16F_ENCODED }; enum Result { kOk = 0, kBadFrame, kBadScanout, kNoResource, kGpuFailed }; @@ -43,6 +52,8 @@ public: // One frame: encodes the GPU copy, commits, waits for the GPU. Rows are copied as whole rows of width*4 bytes. Result present(const Frame &frame) { if (!ready()) return kBadScanout; + if (frame.format == NGP_RGBA16F_LINEAR || frame.format == NGP_RGBA16F_ENCODED) return convert(frame); + if (frame.format != NGP_BGRX8888) return kBadFrame; const size_t page = (size_t)getpagesize(), row = (size_t)frame.width * 4; if (!frame.base || ((uintptr_t)frame.base & (page - 1)) || !frame.bytes || (frame.bytes & (page - 1)) || frame.width != scanout_.width || frame.height != scanout_.height || frame.stride < row || @@ -78,6 +89,57 @@ public: [commands waitUntilCompleted]; return commands.status == MTLCommandBufferStatusCompleted ? kOk : kGpuFailed; } + // A half-float frame: one thread per pixel, 16 x 8 threads a group (the kernel skips threads past the edges). + Result convert(const Frame &frame) { + const size_t page = (size_t)getpagesize(), row = (size_t)frame.width * 8; + if (!frame.base || ((uintptr_t)frame.base & (page - 1)) || !frame.bytes || (frame.bytes & (page - 1)) || + frame.width != scanout_.width || frame.height != scanout_.height || frame.stride < row || (frame.stride & 3) || + (scanout_.stride & 3) || frame.stride * (frame.height - 1) + row > frame.bytes) return kBadFrame; + const bool encoded = frame.format == NGP_RGBA16F_ENCODED; + if (!convertPipeline() || !table(encoded)) return kNoResource; + id source = sourceFor(frame); + if (!source) return kNoResource; + id commands = [queue_ commandBuffer]; + id encoder = commands ? [commands computeCommandEncoder] : nil; + if (!encoder) return kNoResource; + const uint32_t params[4] = {frame.width, frame.height, (uint32_t)(frame.stride / 4), (uint32_t)(scanout_.stride / 4)}; + [encoder setComputePipelineState:convert_]; + [encoder setBuffer:source offset:0 atIndex:0]; + [encoder setBuffer:target_ offset:0 atIndex:1]; + [encoder setBuffer:tables_[encoded] offset:0 atIndex:2]; + [encoder setBytes:params length:sizeof params atIndex:3]; + [encoder dispatchThreadgroups:MTLSizeMake((frame.width + 15) / 16, (frame.height + 7) / 8, 1) + threadsPerThreadgroup:MTLSizeMake(16, 8, 1)]; + [encoder endEncoding]; + [commands commit]; + [commands waitUntilCompleted]; + return commands.status == MTLCommandBufferStatusCompleted ? kOk : kGpuFailed; + } + // Made once; a failure is not retried (every later half-float frame then goes back to the display driver's own path). + bool convertPipeline() { + if (convert_ || convertFailed_) return convert_ != nil; + convertFailed_ = true; + if (!nmb_scanout_convert_air || !native_bridge_metal_library_with_air) return false; + size_t length = 0; + const void *air = nmb_scanout_convert_air(&length); + id library = air && length ? native_bridge_metal_library_with_air(air, length) : nil; + id function = [library newFunctionWithName:@"scanout_convert"]; + NSError *error = nil; + convert_ = function ? [device_ newComputePipelineStateWithFunction:function error:&error] : nil; + if (convert_ && convert_.maxTotalThreadsPerThreadgroup < 128) convert_ = nil; + convertFailed_ = convert_ == nil; + return convert_ != nil; + } + // The 8-bit code of every 16-bit half pattern, as 32-bit words (65536 of them, 256 KiB). + id table(bool encoded) { + if (tables_[encoded]) return tables_[encoded]; + id buffer = [device_ newBufferWithLength:65536 * sizeof(uint32_t) options:MTLResourceStorageModeShared]; + if (!buffer) return nil; + uint32_t *codes = (uint32_t *)buffer.contents; + for (uint32_t bits = 0; bits < 65536; ++bits) codes[bits] = native_half::code((uint16_t)bits, encoded); + tables_[encoded] = buffer; + return buffer; + } private: // The compositor cycles through a few surfaces and the kernel keeps each one's mapping under one id, so the GPU import (pinning and // mapping the pages, milliseconds per frame otherwise) is made once per surface. The kernel keeps more mappings (6) than kSources, so @@ -107,6 +169,9 @@ private: Scanout scanout_; id queue_; id target_; + id convert_ = nil; + bool convertFailed_ = false; + id tables_[2] = {nil, nil}; Source sources_[kSources] = {}; uint64_t uses_ = 0; }; diff --git a/NativeAdrenoMetalPlugin.mm b/NativeAdrenoMetalPlugin.mm index 1e8f18d..f93cf8e 100644 --- a/NativeAdrenoMetalPlugin.mm +++ b/NativeAdrenoMetalPlugin.mm @@ -5,11 +5,14 @@ // Published (the image carries the file NativeAdrenoMetal.publish beside the bundle): backboardd, the one process that owns the GPU, returns the native // Metal frontend's device (the metal2vulkan runtime on Turnip, on the Adreno 740), which the original discovery registers; the CARenderServer then // renders the system UI through it, and the display driver's frames are written to the scanout by the GPU (GpuScanoutClient.h). Another process -// gets its own native device (its own driver connection, GPU address window and shader cache) when its name is listed in the file -// NativeAdrenoMetal.clients beside the bundle ("*" lists every process); an unlisted one returns nil and keeps the software renderer. +// gets its own native device too (its own driver connection, GPU address space and shader cache), as every process does with Apple's +// own GPU plugin. #import #include #include +#if __has_feature(ptrauth_calls) +#include +#endif #include #include #include @@ -109,6 +112,10 @@ static void markSample(Gpu &gpu, const DisplaySample &x, unsigned base) { gpu.mark((base + 0x2000) | (x.mode << 4) | x.behaviour); // mode colour field, ICC/other behaviour bytes } static void reportDisplayState(Gpu &gpu) { +#if defined(__arm64e__) + (void)gpu; + return; // the addresses above are the 21H461 cache's; the arm64e (iPadOS 27) build must not read or write them +#endif ++gReports; static DisplaySample before; static bool changed; @@ -136,27 +143,7 @@ static void reportDisplayState(Gpu &gpu) { markSample(gpu, now, 0xA000); } @catch (NSException *) {} } -// Whether NativeAdrenoMetal.clients names this process (names separated by blanks, commas or new lines; "*" names every process; a -// name that ends in "*" names every process whose name starts with it, so "com.apple.WebKit.*" covers the WebContent, Networking -// and GPU service processes). -static bool listedClient() { - char path[1024]; - if (!requestedPath("NativeAdrenoMetal.clients", path, sizeof path)) return false; - FILE *file = fopen(path, "r"); - if (!file) return false; - char text[1024] = {}; - const size_t length = fread(text, 1, sizeof text - 1, file); - fclose(file); - text[length] = 0; - const char *self = getprogname(); - for (char *save = nullptr, *name = strtok_r(text, " ,\t\r\n", &save); name; name = strtok_r(nullptr, " ,\t\r\n", &save)) { - if (!strcmp(name, "*") || !strcmp(name, self)) return true; - const size_t nameLength = strlen(name); - if (nameLength && name[nameLength - 1] == '*' && !strncmp(name, self, nameLength - 1)) return true; - } - return false; -} -// A listed process other than the render server: the same runtime on its own connection, with none of the render server's threads (no +// Every process other than the render server: the same runtime on its own connection, with none of the render server's threads (no // display hand-off, telemetry, benchmark or cache export). Its shader cache lives in its own cache or temp directory. // The reason a client's device failed, as a text line the driver keeps per process (the render server's report row is rewritten every ten seconds): // " ", through the marker stream (a connection is not needed). The Gpu object is made only on this path. @@ -169,7 +156,6 @@ static void clientFailure(unsigned port, const char *step, const char *detail) { } } static id clientDevice(unsigned port) { - if (!listedClient()) return nil; char turnip[1024]; if (!turnip::libraryPath(turnip, sizeof turnip) || access(turnip, R_OK) != 0) {clientFailure(port, "no-turnip", ""); return nil;} uint64_t registryID = 0; @@ -264,6 +250,19 @@ static id deviceForPort(unsigned port) { } // The render server compresses its framebuffer (an Apple/AGX layout this GPU does not write) unless told not to; the display driver takes plain rows. setenv("CA_DISABLE_FRAMEBUFFER_COMPRESSION", "1", 1); +#if defined(__arm64e__) + // iPadOS 27 picks a 64-bit half-float framebuffer ('RGhA') whenever the display's EDR headroom is above 1 (this panel reports XDR + // headroom), which the display path can only show converted, slowly and greyed. As on iPadOS 17, where the render server drew plain + // 8-bit BGRA, the headroom is held at 1: the render server's headroom override (CA::WindowServer::Display::edr_headroom reads it + // first when nonzero; 24A446 QuartzCore __DATA_DIRTY 0x1e7021014, CARenderServerStart at 0x1846dce88 for the cache slide) and its + // environment spelling for an option table read after this. + setenv("CA_DISPLAY_EDR_HEADROOM", "1", 1); + if (void *start = dlsym(RTLD_DEFAULT, "CARenderServerStart")) { + const uintptr_t slide = (uintptr_t)ptrauth_strip(start, ptrauth_key_function_pointer) - 0x1846dce88; + const float one = 1.0f; + gpu->mark(writeTask(0x1e7021014 + slide, &one, sizeof one) ? 0x6E01 : 0x6EF1); + } +#endif // The runtime's creation log reports each VkPipeline making's time to the device's sink, which the telemetry's V row sums. setenv("METAL2VULKAN_LOG_PIPELINES", "1", 0); CFPreferencesSetAppValue(CFSTR("CAEnableFramebufferCompression"), kCFBooleanFalse, kCFPreferencesCurrentApplication); diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 09175ef..7852c8a 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -309,16 +310,29 @@ static void journal(const std::string &line) { if(FILE *f=fopen(gJournalPath.c_str(),"a")){fwrite(line.data(),1,line.size(),f);fputc('\n',f);fclose(f);} } +// A selector this file lacks (iPadOS 27's QuartzCore sends setResponsibleTaskIDs:count: to every command buffer, which 17 never did): answered +// once as a no-op returning nil/0, and noted once, instead of the NSInvalidArgumentException that ended the render server. respondsToSelector: +// still answers NO, so a caller that asks first keeps its own fallback. +static NSMethodSignature *missingSignature(id self,SEL selector) { + static std::mutex lock;static std::unordered_set seen; + std::string key=std::string(class_getName(object_getClass(self)))+" "+sel_getName(selector); + {std::lock_guard hold(lock);if(!seen.insert(key).second)return [NSMethodSignature signatureWithObjCTypes:"@@:"];} + if(gMissing)gMissing(class_getName(object_getClass(self)),sel_getName(selector)); + noteProblem([NSString stringWithFormat:@"missing %s (answered nil)",key.c_str()]); + return [NSMethodSignature signatureWithObjCTypes:"@@:"]; +} +static void answerMissing(NSInvocation *invocation) {void *zero=nullptr;[invocation setReturnValue:&zero];} + @interface NBObject : NSObject @property(atomic,copy) NSString *label; @end @implementation NBObject - (id)device {return gDeviceObject;} -- (void)doesNotRecognizeSelector:(SEL)selector { - if(gMissing)gMissing(class_getName(object_getClass(self)),sel_getName(selector)); - noteProblem([NSString stringWithFormat:@"missing %s %s",class_getName(object_getClass(self)),sel_getName(selector)]); - [super doesNotRecognizeSelector:selector]; +- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector { + NSMethodSignature *signature=[super methodSignatureForSelector:selector]; + return signature?signature:missingSignature(self,selector); } +- (void)forwardInvocation:(NSInvocation *)invocation {answerMissing(invocation);} @end // ---- resources ---- @@ -480,8 +494,17 @@ NB_RESOURCE_SPI // owners keep the mapping live through this phase. Maintain each exact CPU // range once; no address identity or decision is carried into another phase // or command, and distinct/partially overlapping ranges retain their calls. +// iPadOS 27's sys_dcache_flush is a barrier alone (dsb ish; ret): the call did no maintenance, while finding each surface's base address +// (IOSurfaceGetBaseAddress maps it into the task the first time) and sorting the ranges cost CPU on every commit and completion. The +// kernel keeps imports right without it: read-only ones are mapped coherent, GPU-written ones are cleaned and invalidated when mapped and +// unmapped, and write-combined surfaces hold no line at all. +#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED >= 270000 +static constexpr bool kSurfaceFlush=false; +#else +static constexpr bool kSurfaceFlush=true; +#endif static void flushSurfaces(NSMutableSet *surfaces) { - if(!surfaces.count)return; + if(!kSurfaceFlush || !surfaces.count)return; std::vector> ranges; ranges.reserve(surfaces.count); for(NBTexture *texture in surfaces)if(texture.surface) @@ -528,12 +551,16 @@ static NBTexture *textureOnSurface(NBBuffer *buffer,MTLTextureDescriptor *d,IOSu // ---- libraries, functions, states ---- @interface NBLibrary : NBObject @property(nonatomic) NmbLibrary *handle; +- (id)nb_newFunctionWithName:(NSString *)name constantValues:(MTLFunctionConstantValues *)values zeroRest:(BOOL)zeroRest + error:(NSError **)error; @end @interface NBFunction : NBObject @property(nonatomic) NmbFunction *handle; @property(nonatomic,copy) NSString *name; @property(nonatomic) MTLFunctionType functionType; @property(nonatomic) std::string constantsText; // name:type:hex of each constant value it was specialized with (the journal's) +@property(nonatomic,strong) NBLibrary *library; // where it came from, and the values it was specialized with (nil: none), for +@property(nonatomic,strong) MTLFunctionConstantValues *constantValues; // the zero-rest rebuild of a refused pipeline @end @implementation NBFunction - (void)dealloc {nmb_function_release(_handle);} @@ -576,6 +603,13 @@ static std::string quartzCoreCallers(); @implementation NBLibrary - (void)dealloc {nmb_library_release(_handle);} - (id)newFunctionWithName:(NSString *)name constantValues:(MTLFunctionConstantValues *)values error:(NSError **)error { + return [self nb_newFunctionWithName:name constantValues:values zeroRest:NO error:error]; +} +// zeroRest: every constant the function declares that values leaves out is zero (nmb_function_new_specialized_zero_rest); only for the +// retry of a pipeline the runtime refused because such a constant had no value. +- (id)nb_newFunctionWithName:(NSString *)name constantValues:(MTLFunctionConstantValues *)values zeroRest:(BOOL)zeroRest + error:(NSError **)error { + if(!values)values=[MTLFunctionConstantValues new]; if(!name || !values || !methodABI(values,@selector(newIndexedConstantArray),"@16@0:8") || !methodABI(values,@selector(newNamedConstantArray),"@16@0:8")) { if(error)*error=nbError(@"unqualified function constant values");return nil; } @@ -599,7 +633,8 @@ static std::string quartzCoreCallers(); for(uint64_t i=0;i)function; + function.constantsText=text;function.library=self;function.constantValues=snapshot;if(error)*error=nil;return (id)function; } - (void)newFunctionWithName:(NSString *)name constantValues:(MTLFunctionConstantValues *)values completionHandler:(void (^)(id,NSError *))handler { NSError *error=nil;id function=[self newFunctionWithName:name constantValues:values error:&error]; @@ -640,7 +675,7 @@ static std::string quartzCoreCallers(); NmbFunction *handle=name?nmb_function_new(_handle,name.UTF8String):nullptr;gTimeFunction.add(functionBegan); if(!handle)return nil; NBFunction *function=[NBFunction new];function.handle=handle;function.name=name;function.functionType=(MTLFunctionType)nmb_function_type(handle); - return (id)function; + function.library=self;return (id)function; } // Shader priming (PrimeShaders.h): the function with each of its function constants set to zero, or nil. - (id)nb_newFunctionWithZeroConstants:(NSString *)name { @@ -1025,6 +1060,8 @@ static bool onRenderServerThread() { } extern "C" void NBWaitInFlight(uint64_t beforeUs); @implementation NBCommandBuffer +// iPadOS 27: QuartzCore names the tasks a command buffer works for (GPU time attribution), on every buffer; there is no such accounting here. +- (void)setResponsibleTaskIDs:(const void *)ids count:(NSUInteger)count {(void)ids;(void)count;} - (instancetype)init { if((self=[super init])){_scheduledHandlers=[NSMutableArray array];_completedHandlers=[NSMutableArray array];_surfaces=[NSMutableSet set];_resourceOwners=[NSMutableSet set];_handlers=dispatch_group_create();_schedulingHandlers=dispatch_group_create();} if(self){createdUs=nowUs();createCpu=threadCpuUs();createTid=threadIdLow();earlyEligible=onRenderServerThread();sampleWindowOpen();} @@ -1488,6 +1525,16 @@ static bool linkedHandles(NSArray> *functions,std::vector &linkedStore, std::vector &privateStore,NSError **error) { @@ -1584,11 +1631,11 @@ static bool pipelineArchives(NSArray *list,std::vector &arch uint64_t s[3]={};if(!nmb_device_max_threads_per_threadgroup(gDevice,s))NBLOG(@"native-metal: maxThreadsPerThreadgroup: %@",lastError()); return MTLSizeMake(s[0],s[1],s[2]); } -- (void)doesNotRecognizeSelector:(SEL)selector { - if(gMissing)gMissing("NativeBridgeMetalDevice",sel_getName(selector)); - noteProblem([NSString stringWithFormat:@"missing device %s",sel_getName(selector)]); - [super doesNotRecognizeSelector:selector]; +- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector { + NSMethodSignature *signature=[super methodSignatureForSelector:selector]; + return signature?signature:missingSignature(self,selector); } +- (void)forwardInvocation:(NSInvocation *)invocation {answerMissing(invocation);} - (id)newEvent { NmbEvent *handle=nmb_event_new(gDevice,false);if(!handle)return nil; NBEvent *event=[NBEvent new];event.handle=handle;return (id)event; @@ -1710,8 +1757,14 @@ static bool pipelineArchives(NSArray *list,std::vector &arch if(![(id)function isKindOfClass:[NBFunction class]]){if(error)*error=nbError(@"foreign function");return nil;} const NmbPipelineOptions creation={(uint64_t)options,nullptr,0}; const uint64_t pipelineBegan=nowUs(); - NmbComputePipeline *handle=nmb_compute_pipeline_new_with_options(gDevice,[(NBFunction *)function handle],&creation);gTimePipeline.add(pipelineBegan); - if(!handle)return failWith>(error,@"compute pipeline",nil); + NmbComputePipeline *handle=nmb_compute_pipeline_new_with_options(gDevice,[(NBFunction *)function handle],&creation); + if(!handle)if(NBFunction *rebuilt=zeroRestFunction((NBFunction *)function))handle=nmb_compute_pipeline_new_with_options(gDevice,rebuilt.handle,&creation); + gTimePipeline.add(pipelineBegan); + if(!handle) { + NBFunction *f=(NBFunction *)function; + noteProblem([NSString stringWithFormat:@"compute %@ k=%s: %@",f.name,f.constantsText.c_str(),lastError()]); + return failWith>(error,@"compute pipeline",nil); + } NBComputePipeline *state=[NBComputePipeline new];state.handle=handle;gPipelines++;if(error)*error=nil;return (id)state; } - (id)newComputePipelineStateWithDescriptor:(MTLComputePipelineDescriptor *)d options:(MTLPipelineOption)options @@ -1723,8 +1776,18 @@ static bool pipelineArchives(NSArray *list,std::vector &arch if(!pipelineArchives(d.binaryArchives,archives,error))return nil; const NmbPipelineOptions creation={(uint64_t)options,archives.data(),archives.size()}; const uint64_t pipelineBegan=nowUs(); - NmbComputePipeline *handle=nmb_compute_pipeline_new_with_descriptor(gDevice,&p,&creation);gTimePipeline.add(pipelineBegan); - if(!handle)return failWith>(error,@"compute pipeline",nil); + NmbComputePipeline *handle=nmb_compute_pipeline_new_with_descriptor(gDevice,&p,&creation); + // Metal builds a kernel that reads a constant nobody set; the runtime refuses it: once more with those constants zero. + if(!handle) { + NBFunction *rebuilt=zeroRestFunction((NBFunction *)d.computeFunction); + if(rebuilt){p.function=rebuilt.handle;handle=nmb_compute_pipeline_new_with_descriptor(gDevice,&p,&creation);} + } + gTimePipeline.add(pipelineBegan); + if(!handle) { + NBFunction *f=(NBFunction *)d.computeFunction; + noteProblem([NSString stringWithFormat:@"compute %@ k=%s: %@",f.name,f.constantsText.c_str(),lastError()]); + return failWith>(error,@"compute pipeline",nil); + } NBComputePipeline *state=[NBComputePipeline new];state.handle=handle;state.label=d.label;gPipelines++; if(error)*error=nil;return (id)state; } @@ -1858,8 +1921,13 @@ extern "C" id native_bridge_metal_open(const char *loader,const char *cache,uint if(getenv("NMB_DEVICE_BASE_NSOBJECT"))base=[NSObject class]; #endif #if TARGET_OS_IPHONE - // The target's base class ABI (iPadOS 17.7.11): the dynamic subclass adds no instance variables. + // The target's base class ABI (iPadOS 17.7.11, 536 bytes): the dynamic subclass adds no instance variables and reads none of the base's, + // so only 17's size is held to; the arm64e build (iPadOS 27, whose _MTLDevice is larger) needs the class to exist. +#if defined(__arm64e__) + if(!base){NBLOG(@"native-metal: no _MTLDevice");return nil;} +#else if(!base || class_getInstanceSize(base)!=536){NBLOG(@"native-metal: unexpected _MTLDevice ABI");return nil;} +#endif #endif if(!base)base=[NSObject class]; Class cls=objc_allocateClassPair(base,"NativeBridgeMetalDevice",0);if(!cls)return nil; diff --git a/bridge/NativeMetalObjects.h b/bridge/NativeMetalObjects.h index ac9a390..109252d 100644 --- a/bridge/NativeMetalObjects.h +++ b/bridge/NativeMetalObjects.h @@ -30,6 +30,8 @@ typedef struct NmbFence NmbFence; const char *nmb_last_error(void); /* The bridge's embedded AIR fixtures: the runtime's own runtime_examples and kernel_function_constant_gate_fold. */ const void *nmb_fixture_air(size_t *length); +// The GPU scanout's half-float conversion kernel as AIR bitcode (GpuScanoutPresenter.h). +const void *nmb_scanout_convert_air(size_t *length); const void *nmb_function_constant_air(size_t *length); /* loader: a library exporting vkGetInstanceProcAddr (Turnip on the tablet). cache: a writable directory for translated shaders. */ @@ -122,6 +124,8 @@ struct NmbConstant {uint64_t type, index; const char *name; const void *bytes; u NmbFunction *nmb_function_new_specialized(NmbLibrary *, const char *name, const struct NmbConstant *constants, uint32_t count); // The library's function with every function constant it declares set to zero (shader priming), or null. NmbFunction *nmb_function_new_zero_constants(NmbLibrary *, const char *name); +/* nmb_function_new_specialized, with every declared constant the caller left out set to zero (false for a bool). */ +NmbFunction *nmb_function_new_specialized_zero_rest(NmbLibrary *, const char *name, const struct NmbConstant *constants, uint32_t count); void nmb_function_release(NmbFunction *); /* Metal's MTLFunctionType (1 vertex, 2 fragment, 3 kernel, ...). */ uint64_t nmb_function_type(const NmbFunction *); diff --git a/bridge/NativeMetalObjects.rs b/bridge/NativeMetalObjects.rs index c98c14e..57ed32d 100644 --- a/bridge/NativeMetalObjects.rs +++ b/bridge/NativeMetalObjects.rs @@ -113,6 +113,20 @@ pub extern "C" fn nmb_last_error() -> *const c_char { }) } +/// The GPU scanout's half-float conversion kernel (shaders/scanout_convert.ll), assembled to AIR at build time. +static SCANOUT_CONVERT_AIR: &[u8] = include_bytes!(env!("NMB_SCANOUT_AIR")); + +/// # Safety +/// `length` is writable. +#[unsafe(no_mangle)] +pub unsafe extern "C" fn nmb_scanout_convert_air(length: *mut usize) -> *const c_void { + // SAFETY: the caller's contract. + if let Some(length) = unsafe { length.as_mut() } { + *length = SCANOUT_CONVERT_AIR.len(); + } + SCANOUT_CONVERT_AIR.as_ptr() as *const c_void +} + /// The runtime's kernel_function_constant_gate_fold fixture, assembled to AIR at build time. static FUNCTION_CONSTANT_AIR: &[u8] = include_bytes!(env!("NMB_FC_AIR")); @@ -1017,6 +1031,25 @@ pub struct NmbConstant { #[unsafe(no_mangle)] pub unsafe extern "C" fn nmb_function_new_specialized(library: *mut NmbLibrary, name: *const c_char, constants: *const NmbConstant, count: u32) -> *mut NmbFunction { + // SAFETY: the caller's contract. + unsafe { specialized(library, name, constants, count, false) } +} + +/// The same with every declared constant the caller did not give a value set to zero (false for a bool). Metal accepts a function +/// whose code reads a constant nobody set; the translator needs a value (`NeedsInput(FunctionConstant)`), and zero is what such a +/// read yields there. The frontend retries a pipeline with this variant only after the specialized one was refused for that reason. +/// +/// # Safety +/// As for `nmb_function_new_specialized`. +#[unsafe(no_mangle)] +pub unsafe extern "C" fn nmb_function_new_specialized_zero_rest(library: *mut NmbLibrary, name: *const c_char, + constants: *const NmbConstant, count: u32) -> *mut NmbFunction { + // SAFETY: the caller's contract. + unsafe { specialized(library, name, constants, count, true) } +} + +unsafe fn specialized(library: *mut NmbLibrary, name: *const c_char, constants: *const NmbConstant, count: u32, + zero_rest: bool) -> *mut NmbFunction { guard(std::ptr::null_mut(), || { if name.is_null() || (constants.is_null() && count > 0) { return Err("null function name or constants".into()); @@ -1040,6 +1073,20 @@ pub unsafe extern "C" fn nmb_function_new_specialized(library: *mut NmbLibrary, values.by_name.insert(key.to_string(), value); } } + if zero_rest { + let generic = get(library)?.0.new_function_with_name(name).map_err(e("function"))?; + for (key, constant) in generic.function_constants_dictionary().map_err(e("function constants"))? { + if values.by_name.contains_key(&key) || values.by_index.contains_key(&constant.index()) { + continue; + } + let ty = constant.data_type(); + let bytes = constant_bytes(ty.as_raw()); + if bytes == 0 { + return Err(format!("function constant {key} of type {}", ty.as_raw())); + } + values.by_index.insert(constant.index(), FunctionConstantValue::new(ty, vec![0; bytes])); + } + } let function = get(library)?.0.new_function_with_name_constant_values(name, &values).map_err(e("specialized function"))?; Ok(boxed(NmbFunction(function))) }) diff --git a/bridge/RustPacBoundary.c b/bridge/RustPacBoundary.c new file mode 100644 index 0000000..7a190db --- /dev/null +++ b/bridge/RustPacBoundary.c @@ -0,0 +1,115 @@ +// SPDX-License-Identifier: MIT +// The boundary between the Rust bridge and the rest of an arm64e process (iPadOS 27's render server). rustc compiles arm64e code without +// pointer authentication: it calls every function pointer with a plain blr/br and hands out unsigned function pointers. The rest of the +// process signs function pointers (IA key, discriminator 0) and authenticates them at each indirect call, so neither side's pointers work +// in the other's calls. Two fixes, both only in the arm64e plugin: +// - Rust -> C: scripts/rust-pac-rewrite.py turns every blr/br Xn in the linked Rust code into bl/b nmb_pac_call_xN below, which strips +// the signature (xpaci) and branches; an unsigned pointer passes unchanged. TLS thunks, dlsym results, Vulkan entry points and the +// plugin's callbacks are all called this way. +// - C -> Rust: the imports that take a Rust callback are renamed in the Rust objects (build-adreno-metal-plugin.py) to the nmb_pac_* +// wrappers below, which sign the callback before handing it on. Vulkan's debug messenger callback is signed in the create calls, +// reached through the wrapped vkGetInstanceProcAddr. +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define NMB_STRIP(f) ptrauth_strip((void *)(f), ptrauth_key_function_pointer) +#define NMB_SIGN(f) ptrauth_sign_unauthenticated(NMB_STRIP(f), ptrauth_key_function_pointer, 0) + +// ---- Rust -> C: one trampoline per register (x30 is the link register and is never a target here) ---- +#define NMB_CALL(n) __attribute__((naked, used, visibility("default"))) void nmb_pac_call_x##n(void) \ + {__asm__ volatile("xpaci x" #n "\n\tbr x" #n);} +NMB_CALL(0) NMB_CALL(1) NMB_CALL(2) NMB_CALL(3) NMB_CALL(4) NMB_CALL(5) NMB_CALL(6) NMB_CALL(7) NMB_CALL(8) NMB_CALL(9) +NMB_CALL(10) NMB_CALL(11) NMB_CALL(12) NMB_CALL(13) NMB_CALL(14) NMB_CALL(15) NMB_CALL(16) NMB_CALL(17) NMB_CALL(18) NMB_CALL(19) +NMB_CALL(20) NMB_CALL(21) NMB_CALL(22) NMB_CALL(23) NMB_CALL(24) NMB_CALL(25) NMB_CALL(26) NMB_CALL(27) NMB_CALL(28) NMB_CALL(29) + +// ---- C -> Rust: imports that take a Rust callback ---- +// The callback parameters are taken as void * so the compiler neither authenticates nor re-signs the unsigned bits Rust passed. +int nmb_pac_pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *start, void *arg) +{ + return pthread_create(thread, attr, (void *(*)(void *))NMB_SIGN(start), arg); +} +void nmb_pac_tlv_atexit(void *destructor, void *object) +{ + _tlv_atexit((void (*)(void *))NMB_SIGN(destructor), object); +} +int nmb_pac_atexit(void *function) +{ + return atexit((void (*)(void))NMB_SIGN(function)); +} +void nmb_pac_dyld_register_func_for_add_image(void *function) +{ + _dyld_register_func_for_add_image((void (*)(const struct mach_header *, intptr_t))NMB_SIGN(function)); +} +_Unwind_Reason_Code nmb_pac_Unwind_Backtrace(void *trace, void *context) +{ + return _Unwind_Backtrace((_Unwind_Trace_Fn)NMB_SIGN(trace), context); +} + +// ---- Vulkan: the debug messenger callback the runtime installs ---- +static PFN_vkGetInstanceProcAddr gGetInstanceProcAddr; +static PFN_vkCreateInstance gCreateInstance; +static PFN_vkCreateDebugUtilsMessengerEXT gCreateMessenger; +// Signs every messenger callback in a pNext chain in place; returns the original values for restore (at most 8 messengers). +static unsigned signChain(const void *next, void **saved, unsigned cap) +{ + unsigned count = 0; + for (VkBaseOutStructure *s = (VkBaseOutStructure *)next; s && count < cap; s = s->pNext) { + if (s->sType != VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT) continue; + VkDebugUtilsMessengerCreateInfoEXT *info = (VkDebugUtilsMessengerCreateInfoEXT *)s; + saved[count++] = (void *)info->pfnUserCallback; + if (info->pfnUserCallback) info->pfnUserCallback = (PFN_vkDebugUtilsMessengerCallbackEXT)NMB_SIGN(info->pfnUserCallback); + } + return count; +} +static void restoreChain(const void *next, void **saved, unsigned count) +{ + unsigned at = 0; + for (VkBaseOutStructure *s = (VkBaseOutStructure *)next; s && at < count; s = s->pNext) + if (s->sType == VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT) + ((VkDebugUtilsMessengerCreateInfoEXT *)s)->pfnUserCallback = (PFN_vkDebugUtilsMessengerCallbackEXT)saved[at++]; +} +static VkResult wrapCreateInstance(const VkInstanceCreateInfo *info, const VkAllocationCallbacks *allocator, VkInstance *instance) +{ + void *saved[8]; + const unsigned count = info ? signChain(info->pNext, saved, 8) : 0; + const VkResult result = gCreateInstance(info, allocator, instance); + if (info) restoreChain(info->pNext, saved, count); + return result; +} +static VkResult wrapCreateMessenger(VkInstance instance, const VkDebugUtilsMessengerCreateInfoEXT *info, + const VkAllocationCallbacks *allocator, VkDebugUtilsMessengerEXT *messenger) +{ + if (!info) return gCreateMessenger(instance, info, allocator, messenger); + VkDebugUtilsMessengerCreateInfoEXT copy = *info; + if (copy.pfnUserCallback) copy.pfnUserCallback = (PFN_vkDebugUtilsMessengerCallbackEXT)NMB_SIGN(copy.pfnUserCallback); + return gCreateMessenger(instance, ©, allocator, messenger); +} +static PFN_vkVoidFunction wrapGetInstanceProcAddr(VkInstance instance, const char *name) +{ + if (!name) return NULL; + if (!strcmp(name, "vkGetInstanceProcAddr")) return (PFN_vkVoidFunction)wrapGetInstanceProcAddr; + PFN_vkVoidFunction real = gGetInstanceProcAddr(instance, name); + if (!real) return NULL; + if (!strcmp(name, "vkCreateInstance")) {gCreateInstance = (PFN_vkCreateInstance)real; return (PFN_vkVoidFunction)wrapCreateInstance;} + if (!strcmp(name, "vkCreateDebugUtilsMessengerEXT")) { + gCreateMessenger = (PFN_vkCreateDebugUtilsMessengerEXT)real; + return (PFN_vkVoidFunction)wrapCreateMessenger; + } + return real; +} +void *nmb_pac_dlsym(void *handle, const char *name) +{ + void *symbol = dlsym(handle, name); + if (symbol && name && (!strcmp(name, "vkGetInstanceProcAddr") || !strcmp(name, "vk_icdGetInstanceProcAddr"))) { + gGetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)symbol; + return (void *)wrapGetInstanceProcAddr; + } + return symbol; +} diff --git a/bridge/scanout_host.mm b/bridge/scanout_host.mm new file mode 100644 index 0000000..267c987 --- /dev/null +++ b/bridge/scanout_host.mm @@ -0,0 +1,70 @@ +// SPDX-License-Identifier: MIT +// Host check of the GPU scanout (GpuScanoutPresenter.h): a half-float frame converted by the compute kernel stores exactly the bytes of the +// display driver's table (NativeHalfCodes.h), for linear and encoded frames, with padded rows on both sides; a BGRA frame is still copied. +#import +#import +#include +#include +#include +#include +#include "../NativeBridgeMetalFrontend.h" +#include "../GpuScanoutPresenter.h" + +// Shader priming's progress lives in the plugin (PrimeShaders.h); the frontend's telemetry reads it. +extern "C" {volatile uint32_t native_prime_compiling, native_prime_libraries_done, native_prime_libraries_total, native_prime_made, native_prime_rate;} + +static void *pages(size_t bytes) { + void *p = nullptr; + return posix_memalign(&p, (size_t)getpagesize(), bytes) ? nullptr : p; +} + +int main(int argc, char **argv) { + @autoreleasepool { + if (argc < 3) return 2; + id device = native_bridge_metal_open(argv[1], argv[2], 0x6e6d62, nullptr); + if (!device) {std::printf("FAIL device\n"); return 1;} + const size_t page = (size_t)getpagesize(); + const uint32_t width = 1003, height = 517; + const size_t outStride = width * 4 + 96, outBytes = (outStride * height + page - 1) & ~(page - 1); + const size_t inStride = width * 8 + 64, inBytes = (inStride * height + page - 1) & ~(page - 1); + uint8_t *out = (uint8_t *)pages(outBytes), *in = (uint8_t *)pages(inBytes); + if (!out || !in) return 1; + gpupresent::Presenter presenter(device, {out, outBytes, outStride, width, height}); + if (!presenter.ready()) {std::printf("FAIL presenter\n"); return 1;} + srandom(7); + int failed = 0; + for (uint32_t format : {(uint32_t)NGP_RGBA16F_LINEAR, (uint32_t)NGP_RGBA16F_ENCODED}) { + for (size_t i = 0; i < inBytes / 2; ++i) { + // every pattern class: random bits, small positives, around 1.0, negatives, infinities and NaNs + uint16_t v = (uint16_t)random(); + if (i % 7 == 0) v = (uint16_t)(0x3c00 - (random() & 0x3ff)); + ((uint16_t *)in)[i] = v; + } + memset(out, 0x5a, outBytes); + const gpupresent::Result r = presenter.present({in, inBytes, inStride, width, height, 0, format}); + if (r != gpupresent::kOk) {std::printf("FAIL format %u: result %d\n", format, (int)r); ++failed; continue;} + uint64_t wrong = 0; + for (uint32_t y = 0; y < height; ++y) { + const uint16_t *src = (const uint16_t *)(in + y * inStride); + const uint32_t *dst = (const uint32_t *)(out + y * outStride); + for (uint32_t x = 0; x < width; ++x) { + const bool encoded = format == NGP_RGBA16F_ENCODED; + const uint32_t want = 0xff000000u | (uint32_t)native_half::code(src[4 * x], encoded) << 16 | + (uint32_t)native_half::code(src[4 * x + 1], encoded) << 8 | native_half::code(src[4 * x + 2], encoded); + if (dst[x] != want && wrong++ < 4) std::printf(" format %u (%u,%u): %08x want %08x\n", format, x, y, dst[x], want); + } + // the padding after each row is left alone + for (size_t b = width * 4; b < outStride; ++b) if (out[y * outStride + b] != 0x5a && wrong++ < 4) std::printf(" padding written\n"); + } + if (wrong) {std::printf("FAIL format %u: %llu wrong\n", format, (unsigned long long)wrong); ++failed;} + } + // BGRA still takes the copy + for (size_t i = 0; i < inBytes; ++i) in[i] = (uint8_t)random(); + if (presenter.present({in, inBytes, width * 4 + 64, width, height, 0, NGP_BGRX8888}) != gpupresent::kOk || + memcmp(out + 3 * outStride, in + 3 * (width * 4 + 64), width * 4)) {std::printf("FAIL BGRA copy\n"); ++failed;} + if (presenter.present({in, inBytes, inStride, width, height, 0, 77}) != gpupresent::kBadFrame) {std::printf("FAIL unknown format taken\n"); ++failed;} + if (failed) return 1; + std::printf("PASS: half-float frames convert to the display driver's bytes on the GPU (linear and encoded), BGRA copies, unknown formats refused\n"); + } + return 0; +} diff --git a/shaders/scanout_convert.ll b/shaders/scanout_convert.ll new file mode 100644 index 0000000..9be1059 --- /dev/null +++ b/shaders/scanout_convert.ll @@ -0,0 +1,98 @@ +; SPDX-License-Identifier: MIT +; GPU scanout conversion (GpuScanoutPresenter.h): RGBA 16F rows to the scanout's BGRX rows, one thread per pixel. Each half's 16-bit +; pattern indexes a 65536-entry table of 8-bit codes (the display driver's own rounding: linear light to sRGB, or values already +; encoded; negatives and NaNs 0), so the result is exactly what the CPU path stores. Hand-written AIR, the form Metal's front end emits: +; kernel void scanout_convert(device const uint *src [[buffer(0)]], device uint *dst [[buffer(1)]], +; device const uint *table [[buffer(2)]], constant uint4 &p [[buffer(3)]], +; uint2 gid [[thread_position_in_grid]]) +; p: width, height, source row pitch in words, destination row pitch in words. +target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v16:16:16-v24:32:32-v32:32:32-v48:64:64-v64:64:64-v96:128:128-v128:128:128-v192:256:256-v256:256:256-v512:512:512-v1024:1024:1024-n8:16:32" +target triple = "air64_v28-apple-macosx26.0.0" + +define void @scanout_convert(ptr addrspace(1) noundef readonly "air-buffer-no-alias" %src, ptr addrspace(1) noundef "air-buffer-no-alias" %dst, ptr addrspace(1) noundef readonly "air-buffer-no-alias" %table, ptr addrspace(2) noundef readonly align 16 dereferenceable(16) "air-buffer-no-alias" %p, <2 x i32> noundef %gid) local_unnamed_addr #0 { +entry: + %w = load i32, ptr addrspace(2) %p, align 16 + %ph = getelementptr inbounds i32, ptr addrspace(2) %p, i64 1 + %h = load i32, ptr addrspace(2) %ph, align 4 + %pss = getelementptr inbounds i32, ptr addrspace(2) %p, i64 2 + %ss = load i32, ptr addrspace(2) %pss, align 8 + %pds = getelementptr inbounds i32, ptr addrspace(2) %p, i64 3 + %ds = load i32, ptr addrspace(2) %pds, align 4 + %x = extractelement <2 x i32> %gid, i64 0 + %y = extractelement <2 x i32> %gid, i64 1 + %inx = icmp ult i32 %x, %w + %iny = icmp ult i32 %y, %h + %in = and i1 %inx, %iny + br i1 %in, label %body, label %done + +body: + %x64 = zext i32 %x to i64 + %y64 = zext i32 %y to i64 + %ss64 = zext i32 %ss to i64 + %srow = mul i64 %y64, %ss64 + %x2 = shl i64 %x64, 1 + %si = add i64 %srow, %x2 + %sp = getelementptr inbounds i32, ptr addrspace(1) %src, i64 %si + %lo = load i32, ptr addrspace(1) %sp, align 4 + %si1 = add i64 %si, 1 + %sp1 = getelementptr inbounds i32, ptr addrspace(1) %src, i64 %si1 + %hi = load i32, ptr addrspace(1) %sp1, align 4 + %r = and i32 %lo, 65535 + %g = lshr i32 %lo, 16 + %b = and i32 %hi, 65535 + %r64 = zext i32 %r to i64 + %g64 = zext i32 %g to i64 + %b64 = zext i32 %b to i64 + %rp = getelementptr inbounds i32, ptr addrspace(1) %table, i64 %r64 + %gp = getelementptr inbounds i32, ptr addrspace(1) %table, i64 %g64 + %bp = getelementptr inbounds i32, ptr addrspace(1) %table, i64 %b64 + %rv = load i32, ptr addrspace(1) %rp, align 4 + %gv = load i32, ptr addrspace(1) %gp, align 4 + %bv = load i32, ptr addrspace(1) %bp, align 4 + %rs = shl i32 %rv, 16 + %gs = shl i32 %gv, 8 + %rg = or i32 %rs, %gs + %rgb = or i32 %rg, %bv + %px = or i32 %rgb, -16777216 + %ds64 = zext i32 %ds to i64 + %drow = mul i64 %y64, %ds64 + %di = add i64 %drow, %x64 + %dp = getelementptr inbounds i32, ptr addrspace(1) %dst, i64 %di + store i32 %px, ptr addrspace(1) %dp, align 4 + br label %done + +done: + ret void +} + +attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn "frame-pointer"="all" "min-legal-vector-width"="0" "no-builtins" "no-trapping-math"="true" "stack-protector-buffer-size"="8" } + +!llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8} +!air.kernel = !{!9} +!air.compile_options = !{!17, !18} +!llvm.ident = !{!19} +!air.version = !{!20} +!air.language_version = !{!21} + +!0 = !{i32 2, !"SDK Version", [2 x i32] [i32 27, i32 0]} +!1 = !{i32 1, !"wchar_size", i32 4} +!2 = !{i32 7, !"frame-pointer", i32 2} +!3 = !{i32 7, !"air.max_device_buffers", i32 31} +!4 = !{i32 7, !"air.max_constant_buffers", i32 31} +!5 = !{i32 7, !"air.max_threadgroup_buffers", i32 31} +!6 = !{i32 7, !"air.max_textures", i32 128} +!7 = !{i32 7, !"air.max_read_write_textures", i32 8} +!8 = !{i32 7, !"air.max_samplers", i32 16} +!9 = !{ptr @scanout_convert, !10, !11} +!10 = !{} +!11 = !{!12, !13, !14, !15, !16} +!12 = !{i32 0, !"air.buffer", !"air.location_index", i32 0, i32 1, !"air.read", !"air.address_space", i32 1, !"air.arg_type_size", i32 4, !"air.arg_type_align_size", i32 4, !"air.arg_type_name", !"uint", !"air.arg_name", !"src"} +!13 = !{i32 1, !"air.buffer", !"air.location_index", i32 1, i32 1, !"air.read_write", !"air.address_space", i32 1, !"air.arg_type_size", i32 4, !"air.arg_type_align_size", i32 4, !"air.arg_type_name", !"uint", !"air.arg_name", !"dst"} +!14 = !{i32 2, !"air.buffer", !"air.location_index", i32 2, i32 1, !"air.read", !"air.address_space", i32 1, !"air.arg_type_size", i32 4, !"air.arg_type_align_size", i32 4, !"air.arg_type_name", !"uint", !"air.arg_name", !"table"} +!15 = !{i32 3, !"air.buffer", !"air.buffer_size", i32 16, !"air.location_index", i32 3, i32 1, !"air.read", !"air.address_space", i32 2, !"air.arg_type_size", i32 16, !"air.arg_type_align_size", i32 16, !"air.arg_type_name", !"uint4", !"air.arg_name", !"p"} +!16 = !{i32 4, !"air.thread_position_in_grid", !"air.arg_type_name", !"uint2", !"air.arg_name", !"gid"} +!17 = !{!"air.compile.denorms_disable"} +!18 = !{!"air.compile.fast_math_enable"} +!19 = !{!"Apple metal version 32023.921 (metalfe-32023.921.6)"} +!20 = !{i32 2, i32 8, i32 0} +!21 = !{!"Metal", i32 4, i32 0, i32 0} From c3b55170c2a187d9ed41c957564fbd2408b0776e Mon Sep 17 00:00:00 2001 From: HuJK Date: Sat, 24 Oct 2026 19:13:36 +0800 Subject: [PATCH 27/92] Fix Metal bridge rotation, resolve and indirect dispatch Forward framebuffer rotation, drawable flags and texture getters so rotated IOSurfaces use the runtime physical layout. Preserve render-pass resolve attachments and their lifetimes, and expose GPU-written indirect compute arguments. Return typed Metal command-buffer errors and add rotation/refusal diagnostics without adding global fences. Validation: Mac and deployed guest pass 29 deterministic Metal Check cases and three workloads; the old plugin fails the new rotation oracle. Compositor visual acceptance and GFXBench workload validation remain pending. Signed-off-by: HuJK --- NativeBridgeMetalFrontend.mm | 53 ++++++++++++++++++++++++--- bridge/NativeMetalObjects.h | 6 ++++ bridge/NativeMetalObjects.rs | 70 +++++++++++++++++++++++++++++++++++- 3 files changed, 123 insertions(+), 6 deletions(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 7852c8a..d1be8bd 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -10,6 +10,7 @@ #import #import #import +#import #include #include #include @@ -276,16 +277,26 @@ static void noteProblem(NSString *what) { static uint32_t gFormats[8];static unsigned gFormatCount;static std::atomic gTextureRefusals; // Textures over IOSurfaces: made, refused, and how many of the requests named a planar surface. static std::atomic gSurfaceTextures,gSurfaceRefusals,gSurfacePlanar; +static std::atomic gSurfaceRotations[5]; +static uint64_t textureRotation(id descriptor) { + SEL sel=sel_registerName("rotation"); + return [descriptor respondsToSelector:sel]?((NSUInteger (*)(id,SEL))objc_msgSend)(descriptor,sel):0; +} +static bool textureFlag(id descriptor,const char *name) { + SEL sel=sel_registerName(name); + return [descriptor respondsToSelector:sel] && ((BOOL (*)(id,SEL))objc_msgSend)(descriptor,sel); +} // The last refused surface texture: the check that refused it (1 plane, 2 base, 3 plane range, 4 import, 5 texture), the surface's format, // planes, requested plane, plane offset, plane row bytes and allocation, and the descriptor's size and pixel format. -struct SurfaceRefusal {uint32_t step,format,planes,plane,pixelFormat;uint64_t offset,row,allocation,width,height;}; +struct SurfaceRefusal {uint32_t step,format,planes,plane,pixelFormat;uint64_t offset,row,allocation,width,height,surfaceWidth,surfaceHeight,rotation;bool framebufferOnly;}; static SurfaceRefusal gSurfaceLastRefusal; // Shader priming's progress (PrimeShaders.h, in the plugin), for the PR row. extern "C" volatile uint32_t native_prime_compiling,native_prime_libraries_done,native_prime_libraries_total,native_prime_made,native_prime_rate; static void refuseSurface(uint32_t step,IOSurfaceRef surface,MTLTextureDescriptor *d,NSUInteger plane,uint64_t offset,uint64_t row) { gSurfaceRefusals++; SurfaceRefusal r={step,surface?IOSurfaceGetPixelFormat(surface):0,surface?(uint32_t)IOSurfaceGetPlaneCount(surface):0,(uint32_t)plane, - d?(uint32_t)d.pixelFormat:0,offset,row,surface?IOSurfaceGetAllocSize(surface):0,d?d.width:0,d?d.height:0}; + d?(uint32_t)d.pixelFormat:0,offset,row,surface?IOSurfaceGetAllocSize(surface):0,d?d.width:0,d?d.height:0, + surface?IOSurfaceGetWidth(surface):0,surface?IOSurfaceGetHeight(surface):0,textureRotation(d),textureFlag(d,"framebufferOnly")}; std::lock_guard lock(gProblemLock);gSurfaceLastRefusal=r; } static void noteFormat(uint32_t format,bool made) { @@ -428,7 +439,8 @@ static NBBuffer *surfaceBuffer(IOSurfaceRef surface,void *base,size_t bytes) { } static bool textureDescriptor(MTLTextureDescriptor *d,NmbTextureDescriptor *out) { if(!d)return false; - *out={(uint64_t)d.textureType,(uint64_t)d.pixelFormat,d.width,d.height,d.depth,d.mipmapLevelCount,d.sampleCount,d.arrayLength,(uint64_t)d.usage,(uint64_t)d.resourceOptions}; + *out={(uint64_t)d.textureType,(uint64_t)d.pixelFormat,d.width,d.height,d.depth,d.mipmapLevelCount,d.sampleCount,d.arrayLength,(uint64_t)d.usage,(uint64_t)d.resourceOptions, + textureRotation(d),textureFlag(d,"framebufferOnly"),textureFlag(d,"isDrawable")}; return true; } @implementation NBTexture @@ -445,14 +457,15 @@ static bool textureDescriptor(MTLTextureDescriptor *d,NmbTextureDescriptor *out) - (MTLResourceOptions)resourceOptions {return (MTLResourceOptions)_descriptor.options;} - (MTLStorageMode)storageMode {return (MTLStorageMode)((_descriptor.options&MTLResourceStorageModeMask)>>MTLResourceStorageModeShift);} - (MTLCPUCacheMode)cpuCacheMode {return (MTLCPUCacheMode)((_descriptor.options&MTLResourceCPUCacheModeMask)>>MTLResourceCPUCacheModeShift);} -- (BOOL)isFramebufferOnly {return NO;} +- (BOOL)isFramebufferOnly {return _descriptor.framebuffer_only!=0;} +- (BOOL)isDrawable {return _descriptor.is_drawable!=0;} - (IOSurfaceRef)iosurface {return _surface;} - (NSUInteger)iosurfacePlane {return 0;} - (id)buffer {return _surface?nil:(id)_buffer;} - (NSUInteger)bufferOffset {return _offset;} - (NSUInteger)bufferBytesPerRow {return _rowBytes;} - (id)parentTexture {return _parent;} -- (NSUInteger)rotation {return 0;} +- (NSUInteger)rotation {return _descriptor.rotation;} - (MTLPurgeableState)setPurgeableState:(MTLPurgeableState)state { const uint64_t answer=nmb_texture_set_purgeable_state(_handle,state); if(!answer)NBLOG(@"native-metal: texture setPurgeableState refused: %@",lastError()); @@ -880,6 +893,13 @@ static void waitForResource(id resource) { } - (void)dispatchThreads:(MTLSize)t threadsPerThreadgroup:(MTLSize)g {[self dispatch:t group:g threads:true];} - (void)dispatchThreadgroups:(MTLSize)t threadsPerThreadgroup:(MTLSize)g {[self dispatch:t group:g threads:false];} +- (void)dispatchThreadgroupsWithIndirectBuffer:(id)buffer indirectBufferOffset:(NSUInteger)offset threadsPerThreadgroup:(MTLSize)group { + if(![(id)buffer isKindOfClass:[NBBuffer class]]){[self.commandBuffer refuse:@"foreign indirect buffer"];return;} + [self.commandBuffer.resourceOwners addObject:buffer]; + gDispatches++;self.commandBuffer->encodedWork.dispatches++; + const uint64_t g[3]={group.width,group.height,group.depth}; + [self check:nmb_compute_dispatch_indirect(self.handle,[(NBBuffer *)buffer handle],offset,g) what:"indirect dispatch"]; +} - (void)useResource:(id)r usage:(MTLResourceUsage)u {(void)r;(void)u;} - (void)memoryBarrierWithScope:(MTLBarrierScope)s {(void)s;} @end @@ -1085,6 +1105,15 @@ extern "C" void NBWaitInFlight(uint64_t beforeUs); if(self.failedLocally)return nbError(self.encodeFailure); if(self.status!=MTLCommandBufferStatusError)return nil; char text[512]={};nmb_command_buffer_error(_handle,text,sizeof text); + uint64_t code=nmb_command_buffer_metal_error_code(_handle); +#if TARGET_OS_IPHONE + // The runtime uses Metal's macOS code 11 for Vulkan DeviceLost. iOS has no + // removable GPU: this is revoked access after the process's context fault, + // represented by AccessRevoked (4), not the unavailable DeviceRemoved code. + if(code==11)code=MTLCommandBufferErrorAccessRevoked; +#endif + if(code)return [NSError errorWithDomain:MTLCommandBufferErrorDomain code:(NSInteger)code + userInfo:@{NSLocalizedDescriptionKey:[NSString stringWithUTF8String:text]}]; return nbError([NSString stringWithUTF8String:text]); } // Debug groups and cache hints name work for tools and the driver's caches; they change nothing the GPU computes here. @@ -1134,6 +1163,13 @@ extern "C" void NBWaitInFlight(uint64_t beforeUs); NBTexture *texture=(NBTexture *)a.texture;if(texture.surface)[_surfaces addObject:texture]; [_resourceOwners addObject:texture]; out.texture=texture.handle;out.level=a.level;out.slice=a.slice;out.depth_plane=a.depthPlane;out.load=a.loadAction;out.store=a.storeAction; + if(a.resolveTexture) { + if(![(id)a.resolveTexture isKindOfClass:[NBTexture class]])return false; + NBTexture *resolve=(NBTexture *)a.resolveTexture; + [_resourceOwners addObject:resolve];if(resolve.surface)[_surfaces addObject:resolve]; + out.resolve_texture=resolve.handle;out.resolve_level=a.resolveLevel; + out.resolve_slice=a.resolveSlice;out.resolve_depth_plane=a.resolveDepthPlane; + } return true; }; for(unsigned i=0;i<8;i++) { @@ -1143,6 +1179,7 @@ extern "C" void NBWaitInFlight(uint64_t beforeUs); } if(!attachment(d.depthAttachment,pass.depth) || !attachment(d.stencilAttachment,pass.stencil)){[self refuse:@"foreign depth/stencil attachment"];return nil;} pass.depth.clear[0]=d.depthAttachment.clearDepth;pass.stencil.clear[0]=d.stencilAttachment.clearStencil; + pass.depth.resolve_filter=d.depthAttachment.depthResolveFilter;pass.stencil.resolve_filter=d.stencilAttachment.stencilResolveFilter; pass.width=d.renderTargetWidth;pass.height=d.renderTargetHeight;pass.array_length=d.renderTargetArrayLength;pass.samples=d.defaultRasterSampleCount; if(d.visibilityResultBuffer) { if(![(id)d.visibilityResultBuffer isKindOfClass:[NBBuffer class]]){[self refuse:@"foreign visibility result buffer"];return nil;} @@ -1712,6 +1749,7 @@ static bool pipelineArchives(NSArray *list,std::vector &arch // the plane's own address and rows. Only a plain surface has no planes, and then only plane 0 exists. const size_t planes=surface?IOSurfaceGetPlaneCount(surface):0; if(!surface || !d || (planes?plane>=planes:plane!=0)){refuseSurface(1,surface,d,plane,0,0);return nil;} + const uint64_t rotation=textureRotation(d);if(rotation<5)gSurfaceRotations[rotation]++; if(planes)gSurfacePlanar++; void *base=IOSurfaceGetBaseAddress(surface);const size_t page=(size_t)getpagesize(),allocation=IOSurfaceGetAllocSize(surface); if(!page || (page&(page-1)) || !base || ((uintptr_t)base&(page-1)) || !allocation || allocation>SIZE_MAX-(page-1)){refuseSurface(2,surface,d,plane,(uintptr_t)base,0);return nil;} @@ -2103,6 +2141,11 @@ extern "C" size_t native_bridge_metal_telemetry(char *out,size_t size) { if(r.step)row("SR %u %08x %c%c%c%c pl%u/%u o%llx rb%llu a%llx %llux%llu pf%u",r.step,r.format,(char)(r.format>>24),(char)(r.format>>16),(char)(r.format>>8), (char)r.format,r.plane,r.planes,(unsigned long long)r.offset,(unsigned long long)r.row,(unsigned long long)r.allocation,(unsigned long long)r.width, (unsigned long long)r.height,r.pixelFormat); + if(r.step)row("SR layout surface=%llux%llu rotation=%llu framebuffer=%u",(unsigned long long)r.surfaceWidth, + (unsigned long long)r.surfaceHeight,(unsigned long long)r.rotation,r.framebufferOnly); + row("ROT imports 0=%llu 1=%llu 2=%llu 3=%llu 4=%llu",(unsigned long long)gSurfaceRotations[0].load(), + (unsigned long long)gSurfaceRotations[1].load(),(unsigned long long)gSurfaceRotations[2].load(), + (unsigned long long)gSurfaceRotations[3].load(),(unsigned long long)gSurfaceRotations[4].load()); row("P0 %.86s",gLastProblems[0]);row("P0b %.86s",strlen(gLastProblems[0])>86?gLastProblems[0]+86:""); row("P1 %.86s",gLastProblems[1]);row("P1b %.86s",strlen(gLastProblems[1])>86?gLastProblems[1]+86:""); } diff --git a/bridge/NativeMetalObjects.h b/bridge/NativeMetalObjects.h index 109252d..5fd9209 100644 --- a/bridge/NativeMetalObjects.h +++ b/bridge/NativeMetalObjects.h @@ -81,6 +81,7 @@ uint64_t nmb_buffer_gpu_address(const NmbBuffer *); struct NmbTextureDescriptor { uint64_t type, format, width, height, depth, levels, samples, array_length, usage, options; + uint64_t rotation, framebuffer_only, is_drawable; }; /* Heaps: type 0 automatic / 1 placement; offset UINT64_MAX asks an automatic heap to place the resource. */ NmbHeap *nmb_heap_new(NmbDevice *, uint64_t type, uint64_t size, uint64_t options); @@ -203,6 +204,8 @@ int nmb_command_buffer_wait(NmbCommandBuffer *, uint64_t timeout_ms); /* Metal's MTLCommandBufferStatus (0 not enqueued .. 4 completed, 5 error). */ uint64_t nmb_command_buffer_status(const NmbCommandBuffer *); size_t nmb_command_buffer_error(const NmbCommandBuffer *, char *out, size_t size); +// Metal's MTLCommandBufferError code, or 0 for no error / a translation-runtime refusal. +uint64_t nmb_command_buffer_metal_error_code(const NmbCommandBuffer *); double nmb_command_buffer_gpu_time(const NmbCommandBuffer *, bool end); /* Events: an MTLEvent (shared false) or an MTLSharedEvent within this process. */ @@ -228,6 +231,8 @@ struct NmbPassAttachment { NmbTexture *texture; uint64_t level, slice, depth_plane, load, store; double clear[4]; /* color, or clear[0] depth / stencil */ + NmbTexture *resolve_texture; + uint64_t resolve_level, resolve_slice, resolve_depth_plane, resolve_filter; }; struct NmbRenderPassDescriptor { struct NmbPassAttachment colors[8], depth, stencil; @@ -248,6 +253,7 @@ bool nmb_encoder_set_sampler(NmbEncoder *, uint32_t stage, NmbSampler *, uint32_ bool nmb_compute_set_pipeline(NmbEncoder *, NmbComputePipeline *); bool nmb_compute_set_threadgroup_memory(NmbEncoder *, uint32_t length, uint32_t index); bool nmb_compute_dispatch(NmbEncoder *, const uint64_t size[3], const uint64_t per_group[3], bool threads); +bool nmb_compute_dispatch_indirect(NmbEncoder *, NmbBuffer *, uint64_t offset, const uint64_t per_group[3]); bool nmb_render_set_pipeline(NmbEncoder *, NmbRenderPipeline *); bool nmb_render_set_depth_stencil(NmbEncoder *, NmbDepthStencil *); diff --git a/bridge/NativeMetalObjects.rs b/bridge/NativeMetalObjects.rs index 57ed32d..45ee0ac 100644 --- a/bridge/NativeMetalObjects.rs +++ b/bridge/NativeMetalObjects.rs @@ -27,6 +27,7 @@ use std::ffi::{c_char, c_void, CStr}; use std::path::PathBuf; use std::ptr::NonNull; use std::time::Duration; +use metal2vulkan_runtime::{CommandBufferError, FailureOrigin, MultisampleDepthResolveFilter, MultisampleStencilResolveFilter}; thread_local! { static LAST: RefCell> = const { RefCell::new(Vec::new()) }; @@ -768,6 +769,9 @@ pub struct NmbTextureDescriptor { pub array_length: u64, pub usage: u64, pub options: u64, + pub rotation: u64, + pub framebuffer_only: u64, + pub is_drawable: u64, } pub struct NmbTexture(Texture); @@ -785,6 +789,9 @@ fn texture_descriptor(d: *const NmbTextureDescriptor) -> Result(d.usage, "texture usage")?; out.resource_options = options(d.options)?; + out.rotation = d.rotation; + out.framebuffer_only = d.framebuffer_only != 0; + out.is_drawable = d.is_drawable != 0; Ok(out) } @@ -902,6 +909,9 @@ pub extern "C" fn nmb_texture_describe(texture: *const NmbTexture, out: *mut Nmb array_length: t.array_length(), usage: t.usage().as_raw() as u64, options: t.resource_options().raw(), + rotation: t.rotation(), + framebuffer_only: u64::from(t.framebuffer_only()), + is_drawable: u64::from(t.is_drawable()), }; Ok(()) }) @@ -1657,7 +1667,36 @@ pub extern "C" fn nmb_command_buffer_status(command_buffer: *const NmbCommandBuf #[unsafe(no_mangle)] pub extern "C" fn nmb_command_buffer_error(command_buffer: *const NmbCommandBuffer, out: *mut c_char, size: usize) -> usize { - guard(0, || Ok(get(command_buffer)?.0.error_description().map_or(0, |text| copy_out(&text, out, size)))) + guard(0, || { + let cb = &get(command_buffer)?.0; + let description = cb.error_description().or_else(|| cb.error().map(|error| format!("{error:?}"))); + Ok(description.map_or(0, |text| copy_out(&text, out, size))) + }) +} + +// Preserve the runtime's distinction between Metal failures and translation refusals. +// In particular, DeviceLost must reach clients as MTLCommandBufferErrorDeviceRemoved. +fn metal_command_buffer_error_code(error: Option) -> u64 { + error.filter(|error| error.origin() == FailureOrigin::Metal).map_or(0, |error| error.metal_code()) +} + +#[unsafe(no_mangle)] +pub extern "C" fn nmb_command_buffer_metal_error_code(command_buffer: *const NmbCommandBuffer) -> u64 { + guard(0, || Ok(metal_command_buffer_error_code(get(command_buffer)?.0.error()))) +} + +#[cfg(test)] +mod command_buffer_error_tests { + use super::*; + + #[test] + fn metal_errors_keep_their_codes_and_runtime_refusals_keep_their_domain() { + assert_eq!(metal_command_buffer_error_code(Some(CommandBufferError::DeviceLost)), 11); + assert_eq!(metal_command_buffer_error_code(Some(CommandBufferError::PageFault)), 3); + assert_eq!(metal_command_buffer_error_code(Some(CommandBufferError::OutOfMemory)), 8); + assert_eq!(metal_command_buffer_error_code(Some(CommandBufferError::Refused)), 0); + assert_eq!(metal_command_buffer_error_code(None), 0); + } } #[unsafe(no_mangle)] @@ -1832,6 +1871,11 @@ pub struct NmbPassAttachment { pub load: u64, pub store: u64, pub clear: [f64; 4], + pub resolve_texture: *mut NmbTexture, + pub resolve_level: u64, + pub resolve_slice: u64, + pub resolve_depth_plane: u64, + pub resolve_filter: u64, } #[repr(C)] @@ -1858,6 +1902,10 @@ pub extern "C" fn nmb_render_encoder_new(command_buffer: *mut NmbCommandBuffer, let mut pass = RenderPassDescriptor::new(); for (slot, a) in pass.color_attachments.iter_mut().zip(d.colors.iter()) { slot.texture = texture_of(a)?; + slot.resolve_texture = optional(a.resolve_texture)?.map(|t| t.0.clone()); + slot.resolve_level = a.resolve_level; + slot.resolve_slice = a.resolve_slice; + slot.resolve_depth_plane = a.resolve_depth_plane; slot.level = a.level; slot.slice = a.slice; slot.depth_plane = a.depth_plane; @@ -1867,6 +1915,11 @@ pub extern "C" fn nmb_render_encoder_new(command_buffer: *mut NmbCommandBuffer, } let depth = &mut pass.depth_attachment; depth.texture = texture_of(&d.depth)?; + depth.resolve_texture = optional(d.depth.resolve_texture)?.map(|t| t.0.clone()); + depth.resolve_level = d.depth.resolve_level; + depth.resolve_slice = d.depth.resolve_slice; + depth.resolve_depth_plane = d.depth.resolve_depth_plane; + depth.depth_resolve_filter = MultisampleDepthResolveFilter::from_raw(d.depth.resolve_filter).map_err(e("depth resolve filter"))?; depth.level = d.depth.level; depth.slice = d.depth.slice; depth.depth_plane = d.depth.depth_plane; @@ -1875,6 +1928,11 @@ pub extern "C" fn nmb_render_encoder_new(command_buffer: *mut NmbCommandBuffer, depth.clear_depth = d.depth.clear[0]; let stencil = &mut pass.stencil_attachment; stencil.texture = texture_of(&d.stencil)?; + stencil.resolve_texture = optional(d.stencil.resolve_texture)?.map(|t| t.0.clone()); + stencil.resolve_level = d.stencil.resolve_level; + stencil.resolve_slice = d.stencil.resolve_slice; + stencil.resolve_depth_plane = d.stencil.resolve_depth_plane; + stencil.stencil_resolve_filter = MultisampleStencilResolveFilter::from_raw(d.stencil.resolve_filter).map_err(e("stencil resolve filter"))?; stencil.level = d.stencil.level; stencil.slice = d.stencil.slice; stencil.depth_plane = d.stencil.depth_plane; @@ -2057,6 +2115,16 @@ pub extern "C" fn nmb_compute_dispatch(encoder: *mut NmbEncoder, size: *const u6 }) } +#[unsafe(no_mangle)] +pub extern "C" fn nmb_compute_dispatch_indirect(encoder: *mut NmbEncoder, buffer: *mut NmbBuffer, offset: u64, per_group: *const u64) -> bool { + guard(false, || { + let g = triple(per_group)?; + compute(encoder)?.dispatch_threadgroups_indirect(&get(buffer)?.0, offset, Size::new(g[0], g[1], g[2])) + .map_err(e("indirect dispatch"))?; + Ok(true) + }) +} + #[unsafe(no_mangle)] pub extern "C" fn nmb_render_set_pipeline(encoder: *mut NmbEncoder, pipeline: *mut NmbRenderPipeline) -> bool { guard(false, || { From 5c033278f06b09c9faf8d9190c2cd08d8e0fb32a Mon Sep 17 00:00:00 2001 From: HuJK Date: Sat, 24 Oct 2026 20:44:08 +0800 Subject: [PATCH 28/92] End the render server when its GPU context is lost, so launchd starts it again After a host GPU hang the render server's context is lost for good: its command buffers complete with DeviceLost (AccessRevoked to clients), CoreAnimation keeps composing with the dead context, and the display shows the surfaces it composed last, flickering between them, until the VM restarts. Ending backboardd by hand (SIGTERM) brought the screen back at the lock screen with a new context. Do that from the plugin: when a command buffer of backboardd completes with DeviceLost, end the process with SIGTERM once its completed handlers have run (once, half a second later); launchd restarts it and SpringBoard with it. Other clients still get the error and decide for themselves. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012Camiwa7VsPwkhfyscKQ6S --- NativeBridgeMetalFrontend.mm | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index d1be8bd..6cda12b 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -27,6 +27,9 @@ #include #include #include +#include +#include +#include #include "bridge/NativeMetalObjects.h" #include "NativeBridgeMetalFrontend.h" @@ -273,6 +276,19 @@ static void noteProblem(NSString *what) { strlcpy(gLastProblems[0],what.UTF8String?:"?",sizeof gLastProblems[0]); } #define NBLOG(...) do{NSString *nbText=[NSString stringWithFormat:__VA_ARGS__];NSLog(@"%@",nbText);noteProblem(nbText);}while(0) +// The render server does not get past the loss of its GPU context: CoreAnimation keeps submitting to the dead context and +// the display shows the surfaces it composed last until backboardd restarts (the hang's evidence is in the host's KGSL log +// and the context's loss mask, not here). End it once its completed handlers have seen the error; launchd starts it +// again, SpringBoard with it, and the screen comes back at the lock screen. Other clients get AccessRevoked and decide. +static void endRenderServerAfterGpuLoss() { + static const bool renderServer=!strcmp(getprogname(),"backboardd"); + static std::atomic ending{false}; + if(!renderServer || ending.exchange(true))return; + NBLOG(@"native-metal: the render server's GPU context is lost; ending backboardd so launchd starts it again"); + dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(500*NSEC_PER_MSEC)),dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE,0),^{ + kill(getpid(),SIGTERM); + }); +} // The pixel formats textures were asked for (first eight distinct), with how many were refused: native_bridge_metal_formats. static uint32_t gFormats[8];static unsigned gFormatCount;static std::atomic gTextureRefusals; // Textures over IOSurfaces: made, refused, and how many of the requests named a planar surface. @@ -1236,9 +1252,12 @@ extern "C" void NBWaitInFlight(uint64_t beforeUs); if(!_schedulingDelivered){_schedulingDelivered=YES;[_scheduledHandlers removeAllObjects];dispatch_group_leave(_schedulingHandlers);} completedHandlers=[_completedHandlers copy];[_completedHandlers removeAllObjects]; } + // The runtime reports Vulkan's DeviceLost as Metal's code 11 (see -error). + const bool lost=!self.failedLocally && nmb_command_buffer_metal_error_code(_handle)==11; void (^run)(void)=^{ for(MTLCommandBufferHandler block in completedHandlers)block((id)self); dispatch_group_leave(self->_handlers); + if(lost)endRenderServerAfterGpuLoss(); }; dispatch_queue_t queue=_queue.completionQueue; if(queue)dispatch_async(queue,run);else run(); From 10f8b8de0bba8fed128c01bcb38960505e7a4200 Mon Sep 17 00:00:00 2001 From: HuJK Date: Sat, 24 Oct 2026 22:20:50 +0800 Subject: [PATCH 29/92] Render server diagnostic: sampled sources rewritten while the GPU could read them With /private/var/ngpu/debug/source-race present when backboardd starts, each small source a command buffer samples (a texture over an IOSurface or a no-copy buffer, up to 1 MiB of rows) is hashed at commit and again at completion; a change in between is logged as "native-metal: source-race ..." (the first 50 of each kind, then every 500th). Off by default. Through a Metal Check stress whose result text came out garbled on screen, it logged no such rewrite: the garbled small text is not a client drawing into a source while the GPU reads it. Its extra work in backboardd also made a swap miss IOMFB's 5 s wait, which used to panic the kernel (NativeMobileDisplay now cancels instead). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012Camiwa7VsPwkhfyscKQ6S --- NativeBridgeMetalFrontend.mm | 46 ++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 6cda12b..98b1fb2 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -30,6 +30,7 @@ #include #include #include +#include #include "bridge/NativeMetalObjects.h" #include "NativeBridgeMetalFrontend.h" @@ -280,10 +281,10 @@ static void noteProblem(NSString *what) { // the display shows the surfaces it composed last until backboardd restarts (the hang's evidence is in the host's KGSL log // and the context's loss mask, not here). End it once its completed handlers have seen the error; launchd starts it // again, SpringBoard with it, and the screen comes back at the lock screen. Other clients get AccessRevoked and decide. +static bool isRenderServer() {static const bool renderServer=!strcmp(getprogname(),"backboardd");return renderServer;} static void endRenderServerAfterGpuLoss() { - static const bool renderServer=!strcmp(getprogname(),"backboardd"); static std::atomic ending{false}; - if(!renderServer || ending.exchange(true))return; + if(!isRenderServer() || ending.exchange(true))return; NBLOG(@"native-metal: the render server's GPU context is lost; ending backboardd so launchd starts it again"); dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(500*NSEC_PER_MSEC)),dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE,0),^{ kill(getpid(),SIGTERM); @@ -794,12 +795,26 @@ static void notified(void *context,uint64_t value) { @property(nonatomic) NmbQueue *handle; @property(atomic,strong) dispatch_queue_t completionQueue; // SPI: where command buffers' handlers run (nil: the runtime's thread) @end +// Diagnostic, render server only, while /private/var/ngpu/debug/source-race exists when it starts: each small source a command buffer +// samples (a texture over an IOSurface or over a no-copy buffer, at most 1 MiB of rows) is hashed at commit and again at completion. +// One that changed in between was rewritten by its client while the GPU could read it, which puts two frames' rows into one frame +// (garbled labels, the status bar clock). Logged "native-metal: source-race ...", the first 50 of each kind and every 500th. +static const bool gWatchSources=isRenderServer() && access("/private/var/ngpu/debug/source-race",F_OK)==0; +struct WatchedSource {const uint8_t *bytes;size_t length;uint64_t hash;bool surface;uint32_t width,height,row;}; +static std::atomic gSourcesWatched[2],gSourcesRaced[2]; +static uint64_t hashSource(const uint8_t *bytes,size_t length) { + uint64_t hash=1469598103934665603ull,word;size_t at=0; + for(;at+8<=length;at+=8){memcpy(&word,bytes+at,8);hash=(hash^word)*1099511628211ull;} + for(;at watched; } @property(nonatomic) NmbCommandBuffer *handle; @property(nonatomic,strong) NBQueue *queue; @@ -812,6 +827,9 @@ static void notified(void *context,uint64_t value) { // Runtime handles retain GPU storage, but only the Objective-C owner retains // a no-copy deallocator, its memory owner and texture/heap ancestry. @property(nonatomic,strong) NSMutableSet *resourceOwners; +// The sampled sources watched for a rewrite while the GPU could read them (gWatchSources), kept alive until completion. +@property(nonatomic,strong) NSMutableSet *sampled; +- (void)watchTexture:(NBTexture *)texture; @property(nonatomic,strong) dispatch_group_t handlers; @property(nonatomic,strong) dispatch_group_t schedulingHandlers; @property(nonatomic) BOOL schedulingDelivered; @@ -859,6 +877,7 @@ static void waitForResource(id resource) { if(texture && ![(id)texture isKindOfClass:[NBTexture class]]){[_commandBuffer refuse:@"foreign texture"];return;} if(texture)[_commandBuffer.resourceOwners addObject:texture]; if(texture && [(NBTexture *)texture surface])[_commandBuffer.surfaces addObject:texture]; + if(texture && gWatchSources)[_commandBuffer watchTexture:(NBTexture *)texture]; [self check:nmb_encoder_set_texture(_handle,stage,texture?[(NBTexture *)texture handle]:nullptr,(uint32_t)index) what:"setTexture"]; } - (void)sampler:(id)sampler index:(NSUInteger)index stage:(uint32_t)stage { @@ -1104,6 +1123,16 @@ extern "C" void NBWaitInFlight(uint64_t beforeUs); return self; } - (void)dealloc {nmb_command_buffer_release(_handle);} +- (void)watchTexture:(NBTexture *)texture { + const uint8_t *bytes=texture.surface?(const uint8_t *)IOSurfaceGetBaseAddress(texture.surface):(const uint8_t *)texture.buffer.wrapped; + const size_t length=(size_t)texture.rowBytes*(size_t)texture.descriptor.height; + if(!bytes || !length || length>(1u<<20) || self.committed)return; + if(!_sampled)_sampled=[NSMutableSet set]; + if([_sampled containsObject:texture])return; + [_sampled addObject:texture]; + watched.push_back({bytes+texture.offset,length,0,texture.surface!=nullptr,(uint32_t)texture.descriptor.width, + (uint32_t)texture.descriptor.height,(uint32_t)texture.rowBytes}); +} - (id)commandQueue {return (id)_queue;} - (BOOL)retainedReferences {return YES;} // Protected content is not supported (the runtime's AppleOnlyCall::SetProtectionOptions takes Metal's default only): any other option fails @@ -1252,6 +1281,18 @@ extern "C" void NBWaitInFlight(uint64_t beforeUs); if(!_schedulingDelivered){_schedulingDelivered=YES;[_scheduledHandlers removeAllObjects];dispatch_group_leave(_schedulingHandlers);} completedHandlers=[_completedHandlers copy];[_completedHandlers removeAllObjects]; } + if(!watched.empty()) { + // A command buffer refused before its commit never hashed its sources. + for(const WatchedSource &source:watched) + if(!self.failedLocally && hashSource(source.bytes,source.length)!=source.hash) { + const uint64_t raced=++gSourcesRaced[source.surface]; + if(raced<=50 || raced%500==0) + NSLog(@"native-metal: source-race %s %ux%u, rows %u bytes: rewritten while the GPU could read it (%llu of %llu such sources)", + source.surface?"IOSurface":"no-copy buffer",source.width,source.height,source.row,(unsigned long long)raced, + (unsigned long long)gSourcesWatched[source.surface].load()); + } + watched.clear();[_sampled removeAllObjects]; + } // The runtime reports Vulkan's DeviceLost as Metal's code 11 (see -error). const bool lost=!self.failedLocally && nmb_command_buffer_metal_error_code(_handle)==11; void (^run)(void)=^{ @@ -1279,6 +1320,7 @@ extern "C" void NBWaitInFlight(uint64_t beforeUs); @synchronized(gInFlight){[gInFlight addObject:self];} if(!self.encodeFailure) { flushSurfaces(_surfaces); + for(WatchedSource &source:watched){source.hash=hashSource(source.bytes,source.length);gSourcesWatched[source.surface]++;} // Completion owns this reference; the runtime orders the non-owning scheduled callback before it. void *context=(__bridge_retained void *)self; if(nmb_command_buffer_on_completed(_handle,completed,context) && From 42c4c42dfa5a31feb2924096c0e9ead67e18f650 Mon Sep 17 00:00:00 2001 From: HuJK Date: Sun, 25 Oct 2026 02:02:35 +0800 Subject: [PATCH 30/92] Render server diagnostic: log its textures and dump what they read With /private/var/ngpu/debug/texture-log present when the render server starts, every texture it makes over an IOSurface or a no-copy buffer, and every replaceRegion and buffer-to-texture upload, is logged with the memory's own shape next to the texture's. Touching debug/dump-now then writes, for the next 30 commits, a manifest of the textures each one uses and the bytes of each small IOSurface and no-copy mask into the render server's temporary directory (surf/), which it may write. This showed GFXBench's garbled titles were clean A8 masks in the client's memory: the GPU read other pages. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012Camiwa7VsPwkhfyscKQ6S --- NativeBridgeMetalFrontend.mm | 104 ++++++++++++++++++++++++++++++++++- 1 file changed, 103 insertions(+), 1 deletion(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 98b1fb2..39343f2 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -31,6 +31,7 @@ #include #include #include +#include #include "bridge/NativeMetalObjects.h" #include "NativeBridgeMetalFrontend.h" @@ -303,6 +304,28 @@ static bool textureFlag(id descriptor,const char *name) { SEL sel=sel_registerName(name); return [descriptor respondsToSelector:sel] && ((BOOL (*)(id,SEL))objc_msgSend)(descriptor,sel); } +// Diagnostic, render server only, while /private/var/ngpu/debug/texture-log exists when it starts: every texture made over an +// IOSurface or a no-copy buffer is logged "native-metal: texture-log ..." with the surface's own size, rows, format and planes +// next to the texture's, to tell which memory and which shape a garbled layer was drawn from. +static const bool gLogTextures=isRenderServer() && access("/private/var/ngpu/debug/texture-log",F_OK)==0; +static std::atomic gTexturesLogged; +// The first 2000 texture-log lines, then every 100th: the uploads run every frame. +static bool textureLogTurn() {const uint64_t n=++gTexturesLogged;return n<=2000 || n%100==0;} +static void logTexture(const char *kind,MTLTextureDescriptor *d,IOSurfaceRef surface,NSUInteger plane,const void *bytes, + NSUInteger offset,NSUInteger row,bool made) { + if(!textureLogTurn())return; + const uint64_t n=gTexturesLogged.load(); + if(surface) + NSLog(@"native-metal: texture-log %llu %s %s desc %lux%lu fmt %lu rot %lu | surface id %u %zux%zu rows %zu fmt %08x planes %zu " + @"plane %lu alloc %zu | offset %lu rows %lu",(unsigned long long)n,kind,made?"made":"REFUSED",(unsigned long)d.width, + (unsigned long)d.height,(unsigned long)d.pixelFormat,(unsigned long)textureRotation(d),IOSurfaceGetID(surface), + IOSurfaceGetWidth(surface),IOSurfaceGetHeight(surface),IOSurfaceGetBytesPerRow(surface),IOSurfaceGetPixelFormat(surface), + IOSurfaceGetPlaneCount(surface),(unsigned long)plane,IOSurfaceGetAllocSize(surface),(unsigned long)offset,(unsigned long)row); + else + NSLog(@"native-metal: texture-log %llu %s %s desc %lux%lu fmt %lu | memory %p offset %lu rows %lu",(unsigned long long)n,kind, + made?"made":"REFUSED",(unsigned long)d.width,(unsigned long)d.height,(unsigned long)d.pixelFormat,bytes,(unsigned long)offset, + (unsigned long)row); +} // The last refused surface texture: the check that refused it (1 plane, 2 base, 3 plane range, 4 import, 5 texture), the surface's format, // planes, requested plane, plane offset, plane row bytes and allocation, and the descriptor's size and pixel format. struct SurfaceRefusal {uint32_t step,format,planes,plane,pixelFormat;uint64_t offset,row,allocation,width,height,surfaceWidth,surfaceHeight,rotation;bool framebufferOnly;}; @@ -506,6 +529,12 @@ NB_RESOURCE_SPI - (void)replaceRegion:(MTLRegion)r mipmapLevel:(NSUInteger)level slice:(NSUInteger)slice withBytes:(const void *)bytes bytesPerRow:(NSUInteger)row bytesPerImage:(NSUInteger)image { const uint64_t region[6]={r.origin.x,r.origin.y,r.origin.z,r.size.width,r.size.height,r.size.depth}; const uint64_t length=(r.size.depth>1?image*(r.size.depth-1):0)+row*r.size.height; + if(gLogTextures && textureLogTurn()) + NSLog(@"native-metal: texture-log %llu replaceRegion origin %lu,%lu size %lux%lu level %lu slice %lu rows %lu | texture %llux%llu fmt %llu " + @"options %llx surface %u",(unsigned long long)gTexturesLogged.load(),(unsigned long)r.origin.x,(unsigned long)r.origin.y, + (unsigned long)r.size.width,(unsigned long)r.size.height,(unsigned long)level,(unsigned long)slice,(unsigned long)row, + (unsigned long long)_descriptor.width,(unsigned long long)_descriptor.height,(unsigned long long)_descriptor.format, + (unsigned long long)_descriptor.options,_surface?IOSurfaceGetID(_surface):0u); if(!nmb_texture_replace(_handle,region,level,slice,bytes,row,image,length))NBLOG(@"native-metal: replaceRegion refused: %@",lastError()); } - (void)replaceRegion:(MTLRegion)r mipmapLevel:(NSUInteger)level withBytes:(const void *)bytes bytesPerRow:(NSUInteger)row { @@ -567,7 +596,9 @@ static NBTexture *textureOnSurface(NBBuffer *buffer,MTLTextureDescriptor *d,IOSu } @implementation NBBuffer (Textures) - (id)newTextureWithDescriptor:(MTLTextureDescriptor *)d offset:(NSUInteger)offset bytesPerRow:(NSUInteger)row { - return (id)textureOnBuffer(self,d,offset,row,false); + NBTexture *texture=textureOnBuffer(self,d,offset,row,false); + if(gLogTextures && self.wrapped)logTexture("no-copy",d,nullptr,0,self.wrapped,offset,row,texture!=nil); + return (id)texture; } - (id)newLinearTextureWithDescriptor:(MTLTextureDescriptor *)d offset:(NSUInteger)offset bytesPerRow:(NSUInteger)row bytesPerImage:(NSUInteger)image { NmbTextureDescriptor descriptor;if(!textureDescriptor(d,&descriptor))return nil; @@ -808,6 +839,62 @@ static uint64_t hashSource(const uint8_t *bytes,size_t length) { for(;at seen{0}; + static std::atomic left{0}; + static std::mutex lock; + static NSMutableSet *written; + struct stat trigger; + if(stat("/private/var/ngpu/debug/dump-now",&trigger)!=0)return; + const int64_t stamp=(int64_t)trigger.st_mtimespec.tv_sec*1000000000+trigger.st_mtimespec.tv_nsec; + if(seen.exchange(stamp)!=stamp)left=30; + if(left<=0)return; + left--; + std::lock_guard guard(lock); + if(!written)written=[NSMutableSet new]; + NSString *directory=[NSTemporaryDirectory() stringByAppendingPathComponent:@"surf"]; + [[NSFileManager defaultManager] createDirectoryAtPath:directory withIntermediateDirectories:YES attributes:nil error:nil]; + FILE *manifest=fopen([directory stringByAppendingPathComponent:@"manifest.txt"].fileSystemRepresentation,"a"); + if(manifest)fprintf(manifest,"commit %d: %lu textures\n",30-left.load(),(unsigned long)owners.count); + for(id object in owners) { + if(![object isKindOfClass:[NBTexture class]])continue; + NBTexture *texture=(NBTexture *)object;const NmbTextureDescriptor d=texture.descriptor;IOSurfaceRef surface=texture.surface; + const uint32_t f=surface?IOSurfaceGetPixelFormat(surface):0; + char name[200]; + if(surface)snprintf(name,sizeof name,"%u-%llux%llu-f%llu-%zux%zu-r%zu-%c%c%c%c-o%lu",IOSurfaceGetID(surface),(unsigned long long)d.width, + (unsigned long long)d.height,(unsigned long long)d.format,IOSurfaceGetWidth(surface),IOSurfaceGetHeight(surface), + IOSurfaceGetBytesPerRow(surface),(char)(f>>24),(char)(f>>16),(char)(f>>8),(char)f,(unsigned long)texture.offset); + else snprintf(name,sizeof name,"texture %llux%llu-f%llu-levels%llu%s",(unsigned long long)d.width,(unsigned long long)d.height, + (unsigned long long)d.format,(unsigned long long)d.levels,texture.buffer.wrapped?"-nocopy":texture.buffer?"-buffer":""); + if(manifest)fprintf(manifest," %s%s rows %lu offset %lu\n",name,[textures containsObject:texture]?" sampled-or-target":"", + (unsigned long)texture.rowBytes,(unsigned long)texture.offset); + if(!surface && texture.buffer.wrapped && texture.rowBytes) { + // A texture over a client's memory (a no-copy buffer): its rows as this process reads them, once per place and shape. + const size_t span=(size_t)texture.rowBytes*(size_t)d.height; + const uint8_t *start=(const uint8_t *)texture.buffer.wrapped+texture.offset; + NSString *tag=[NSString stringWithFormat:@"nc-%p-%llux%llu-f%llu-r%lu",start,(unsigned long long)d.width,(unsigned long long)d.height, + (unsigned long long)d.format,(unsigned long)texture.rowBytes]; + if(span && span<=(1u<<20) && ![written containsObject:tag]) { + [written addObject:tag]; + FILE *out=fopen([directory stringByAppendingPathComponent:[tag stringByAppendingString:@".raw"]].fileSystemRepresentation,"wb"); + if(out){fwrite(start,1,span,out);fclose(out);} + } + continue; + } + const size_t alloc=surface?IOSurfaceGetAllocSize(surface):0; + NSNumber *key=@(surface?IOSurfaceGetID(surface):0); + if(!alloc || alloc>(1u<<20) || [written containsObject:key])continue; + [written addObject:key]; + FILE *out=fopen([directory stringByAppendingPathComponent:[NSString stringWithFormat:@"%s.raw",name]].fileSystemRepresentation,"wb"); + if(!out)continue; + fwrite(IOSurfaceGetBaseAddress(surface),1,alloc,out);fclose(out); + } + if(manifest)fclose(manifest); +} @interface NBCommandBuffer : NBObject { @public EncodedWork encodedWork; // the command buffer's single encoder thread, not global counter deltas @@ -1086,6 +1173,14 @@ static void waitForResource(id resource) { sourceSize:(MTLSize)z toTexture:(id)d destinationSlice:(NSUInteger)slice destinationLevel:(NSUInteger)level destinationOrigin:(MTLOrigin)dO { if(![self buffers:NB_OBJECTS(s) count:1] || ![self textures:NB_OBJECTS(d) count:1])return; + if(gLogTextures && textureLogTurn()) { + const NmbTextureDescriptor t=[(NBTexture *)d descriptor]; + NSLog(@"native-metal: texture-log %llu bufferToTexture size %lux%lu origin %lu,%lu level %lu slice %lu | rows %lu image %lu offset %lu " + @"buffer %lu%s | texture %llux%llu fmt %llu options %llx",(unsigned long long)gTexturesLogged.load(),(unsigned long)z.width, + (unsigned long)z.height,(unsigned long)dO.x,(unsigned long)dO.y,(unsigned long)level,(unsigned long)slice,(unsigned long)row, + (unsigned long)image,(unsigned long)o,(unsigned long)[s length],[(NBBuffer *)s wrapped]?" no-copy":"", + (unsigned long long)t.width,(unsigned long long)t.height,(unsigned long long)t.format,(unsigned long long)t.options); + } const uint64_t size[3]={z.width,z.height,z.depth},b[3]={dO.x,dO.y,dO.z}; [self check:nmb_blit_buffer_to_texture(self.handle,[(NBBuffer *)s handle],o,row,image,size,[(NBTexture *)d handle],slice,level,b) what:"copyFromBuffer:toTexture:"]; } @@ -1320,6 +1415,7 @@ extern "C" void NBWaitInFlight(uint64_t beforeUs); @synchronized(gInFlight){[gInFlight addObject:self];} if(!self.encodeFailure) { flushSurfaces(_surfaces); + if(gLogTextures)dumpSurfacesOnce(_surfaces,_resourceOwners); for(WatchedSource &source:watched){source.hash=hashSource(source.bytes,source.length);gSourcesWatched[source.surface]++;} // Completion owns this reference; the runtime orders the non-owning scheduled callback before it. void *context=(__bridge_retained void *)self; @@ -1800,6 +1896,11 @@ static bool pipelineArchives(NSArray *list,std::vector &arch - (id)newTextureWithDescriptor:(MTLTextureDescriptor *)d { NmbTextureDescriptor descriptor;if(!textureDescriptor(d,&descriptor))return nil; NmbTexture *handle=nmb_texture_new(gDevice,&descriptor);noteFormat((uint32_t)descriptor.format,handle!=nullptr); + if(gLogTextures && textureLogTurn()) + NSLog(@"native-metal: texture-log %llu texture %llux%llu fmt %llu type %llu levels %llu usage %llx options %llx %s", + (unsigned long long)gTexturesLogged.load(),(unsigned long long)descriptor.width,(unsigned long long)descriptor.height, + (unsigned long long)descriptor.format,(unsigned long long)descriptor.type,(unsigned long long)descriptor.levels, + (unsigned long long)descriptor.usage,(unsigned long long)descriptor.options,handle?"made":"REFUSED"); if(!handle){NBLOG(@"native-metal: texture refused: %@",lastError());return nil;} NBTexture *texture=[NBTexture new];texture.handle=handle;nmb_texture_describe(handle,&descriptor);texture.descriptor=descriptor;return (id)texture; } @@ -1821,6 +1922,7 @@ static bool pipelineArchives(NSArray *list,std::vector &arch NBBuffer *buffer=surfaceBuffer(surface,base,bytes);if(!buffer){refuseSurface(4,surface,d,plane,start-(uintptr_t)base,row);return nil;} NBTexture *texture=planes>=2?textureOnSurface(buffer,d,surface,plane,start-(uintptr_t)base,row):textureOnBuffer(buffer,d,start-(uintptr_t)base,row,true); if(!texture){refuseSurface(5,surface,d,plane,start-(uintptr_t)base,row);return nil;} + if(gLogTextures)logTexture("iosurface",d,surface,plane,base,start-(uintptr_t)base,row,texture!=nil); gSurfaceTextures++;texture.surface=(IOSurfaceRef)CFRetain(surface);return (id)texture; } - (id)newLibraryWithData:(dispatch_data_t)data error:(NSError **)error { From dc89d3f59e08ff121597c4bfb62325e152c66702 Mon Sep 17 00:00:00 2001 From: HuJK Date: Sun, 25 Oct 2026 02:02:35 +0800 Subject: [PATCH 31/92] Import the pages now at an address, not a live buffer's earlier ones metal2vulkan puts a no-copy buffer made over exactly the bytes a live one was made over into that one's import, so over the pages that were there then, unless it is told the range was remapped. Metal wires the pages at the address each time. CoreAnimation's render server maps a client's new shared memory (and IOSurfaces) where an earlier mapping was while that mapping's no-copy buffers still live in its caches, so the GPU drew the earlier pages: GFXBench's test list showed another text mask in five titles' places. The bridge's no-copy gate now notes each range's pages (the kernel's VM object id and offset for each map entry over it, mach_vm_region_recurse) once a buffer pinned them. When the pages differ next time, or a page has no object yet, it reports the range remapped first, so the new buffer imports the pages there. The telemetry shows ranges checked and remapped. Metal Check case 40 failed on the guest (the GPU read an earlier round's bytes) and passes now, as on a Mac; GFXBench's titles draw clean. In backboardd 4 of 262 ranges were remapped. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012Camiwa7VsPwkhfyscKQ6S --- NativeBridgeMetalFrontend.mm | 1 + bridge/NativeMetalObjects.h | 2 + bridge/NativeMetalObjects.rs | 126 +++++++++++++++++++++++++++++++++-- 3 files changed, 124 insertions(+), 5 deletions(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 39343f2..847e37a 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -2279,6 +2279,7 @@ extern "C" size_t native_bridge_metal_telemetry(char *out,size_t size) { row("N no-copy %llu dealloc %llu/%llu surface %llu/%llu",(unsigned long long)gNoCopyAll.load(),(unsigned long long)gNoCopyReleased.load(), (unsigned long long)gNoCopyMade.load(), (unsigned long long)gSurfaceImportsReleased.load(),(unsigned long long)gSurfaceImportsMade.load()); + {uint64_t remaps[2]={};if(nmb_no_copy_remaps(remaps))row("N no-copy ranges checked %llu remapped %llu",(unsigned long long)remaps[0],(unsigned long long)remaps[1]);} // The runtime's VkPipeline makings (the creation log, METAL2VULKAN_LOG_PIPELINES; they happen at draws, not at the state's creation) and // its shader cache: where the driver's compile time goes. uint64_t work[10]={}; diff --git a/bridge/NativeMetalObjects.h b/bridge/NativeMetalObjects.h index 5fd9209..0d608bd 100644 --- a/bridge/NativeMetalObjects.h +++ b/bridge/NativeMetalObjects.h @@ -45,6 +45,8 @@ bool nmb_device_work_stats(const NmbDevice *, uint64_t out[10]); const char *nmb_runtime_build(void); // Linear images made/refused/refused again, shadowed textures, shadow copies, texels copied back, linear passes in place/through copies. bool nmb_device_texture_counts(const NmbDevice *, uint64_t out[8]); +// No-copy ranges checked, and those whose pages had changed since a buffer was last made over them (reported to the runtime as remapped). +bool nmb_no_copy_remaps(uint64_t out[2]); /* Artifact lookups and driver pipeline records are separate cache layers. */ struct NmbShaderCacheStats { uint64_t hits, misses, rejected, read_failures, writes, write_failures, evictions; diff --git a/bridge/NativeMetalObjects.rs b/bridge/NativeMetalObjects.rs index 45ee0ac..a7039e9 100644 --- a/bridge/NativeMetalObjects.rs +++ b/bridge/NativeMetalObjects.rs @@ -155,7 +155,7 @@ pub unsafe extern "C" fn nmb_function_constant_air(length: *mut usize) -> *const // ---- device ---- -pub struct NmbDevice(Device, std::sync::OnceLock, std::sync::Mutex<()>); +pub struct NmbDevice(Device, std::sync::OnceLock, std::sync::Mutex); impl NmbDevice { /// The runtime judges the private properties by the family rows once. @@ -185,7 +185,7 @@ pub unsafe extern "C" fn nmb_device_open(loader: *const c_char, cache: *const c_ // Every texture given a shadow image is announced with why no image aliases its bytes (`shadow.log`). options.announce_each.insert(metal2vulkan_runtime::Degradation::ShadowedTexture); let _ = SHADOW_LOG.set(PathBuf::from(cache.to_string_lossy().into_owned()).join("shadow.log")); - Ok(boxed(NmbDevice(Device::system_default_with(&options).map_err(e("device"))?, std::sync::OnceLock::new(), std::sync::Mutex::new(())))) + Ok(boxed(NmbDevice(Device::system_default_with(&options).map_err(e("device"))?, std::sync::OnceLock::new(), std::sync::Mutex::default()))) }) } @@ -638,12 +638,99 @@ pub unsafe extern "C" fn nmb_buffer_new_no_copy_releasing(device: *mut NmbDevice unsafe { no_copy(device, pointer, length, options_word, deallocator) } } +/// The pages each no-copy range had when a buffer was last made over exactly it (`pages_identity`), under the device's no-copy gate. +/// The runtime puts a no-copy buffer over exactly the bytes a live one was made over in that one's import, so over the pages that +/// were there then (`host_aliases.rs`); Metal wires the pages at the address when it makes each buffer. CoreAnimation's render +/// server maps a client's new shared memory (and IOSurfaces) where an earlier one was while its no-copy buffers still live in its +/// caches: the GPU drew the earlier pages, another text mask in a label's place (GFXBench's test list; Metal Check case 40). +#[derive(Default)] +pub struct NoCopyPages { + seen: std::collections::HashMap<(u64, u64), u64>, + /// Whether `seen` was ever emptied to bound it: a range without a note may then have a live buffer over it. + forgot: bool, +} + +const NO_COPY_RANGES_NOTED: usize = 8192; +/// Ranges checked, and ranges whose pages had changed (reported to the runtime as remapped). +static NO_COPY_REMAPS: [std::sync::atomic::AtomicU64; 2] = [const { std::sync::atomic::AtomicU64::new(0) }; 2]; + +/// What the pages under `length` bytes at `start` are, as the kernel names them: for each map entry over the range, the leaf VM +/// object's (hashed) id and the object offset at the range's first byte in it. A remap puts other objects there, and the object of +/// pages a live import pinned outlives the remap (the import holds it), so no new object takes its id while it lives. `None` where +/// a page has no object yet (memory never touched, so not what a live import pinned) or the range is not all mapped. +#[cfg(target_vendor = "apple")] +fn pages_identity(start: u64, length: u64) -> Option { + use std::hash::{Hash, Hasher}; + // vm_region_submap_short_info_64 (mach/vm_region.h, packed to 4). + #[repr(C, packed(4))] + #[derive(Default)] + struct Region { + protection: i32, + max_protection: i32, + inheritance: u32, + offset: u64, + user_tag: u32, + ref_count: u32, + shadow_depth: u16, + external_pager: u8, + share_mode: u8, + is_submap: i32, + behavior: i32, + object_id: u32, + user_wired_count: u16, + } + unsafe extern "C" { + static mach_task_self_: u32; + fn mach_vm_region_recurse(task: u32, address: *mut u64, size: *mut u64, depth: *mut u32, info: *mut i32, count: *mut u32) -> i32; + } + let end = start.checked_add(length)?; + let mut hash = std::collections::hash_map::DefaultHasher::new(); + let mut at = start; + while at < end { + let (mut address, mut size, mut depth) = (at, 0u64, 64u32); + let mut region = Region::default(); + let mut count = (std::mem::size_of::() / 4) as u32; + // SAFETY: the out-parameters are this frame's, `count` words of `region`; the call reads no memory of the range. + let status = unsafe { + mach_vm_region_recurse(mach_task_self_, &mut address, &mut size, &mut depth, (&raw mut region).cast(), &mut count) + }; + let (object, offset) = (region.object_id, region.offset); + if status != 0 || address > at || object == 0 { + return None; + } + (at, object, offset + (at - address)).hash(&mut hash); + at = address.checked_add(size)?; + } + Some(hash.finish()) +} + +#[cfg(not(target_vendor = "apple"))] +fn pages_identity(_: u64, _: u64) -> Option { + None +} + +/// Ranges checked and ranges found remapped since the device opened (`NoCopyPages`). +#[unsafe(no_mangle)] +pub extern "C" fn nmb_no_copy_remaps(out: *mut u64) -> bool { + use std::sync::atomic::Ordering::Relaxed; + if out.is_null() { + return false; + } + // SAFETY: the header's two words. + let out = unsafe { std::slice::from_raw_parts_mut(out, 2) }; + out[0] = NO_COPY_REMAPS[0].load(Relaxed); + out[1] = NO_COPY_REMAPS[1].load(Relaxed); + true +} + unsafe fn no_copy(device: *mut NmbDevice, pointer: *mut c_void, length: u64, options_word: u64, deallocator: Option) -> *mut NmbBuffer { guard(std::ptr::null_mut(), || { let device = get(device)?; // SAFETY: the caller's contract. - let memory = unsafe { HostMemory::new(NonNull::new(pointer as *mut u8).ok_or("null memory")?) }; + let base = NonNull::new(pointer as *mut u8).ok_or("null memory")?; + // SAFETY: the caller's contract. + let memory = unsafe { HostMemory::new(base) }; let modes = options(options_word)?; // The runtime's live-import lookup and subsequent join are separate: // simultaneous first constructors can allocate distinct imports of the @@ -653,10 +740,39 @@ unsafe fn no_copy(device: *mut NmbDevice, pointer: *mut c_void, length: u64, opt // only when an import is freed, never under this gate. Drop it before // returning/formatting a failure; GPU work and resource release never // take it. A device's unrelated APIs and other devices remain separate. + // The gate also holds each range's pages when a buffer was last made over it: pages other than those now (a remap) are + // reported to the runtime first, so the buffer imports the pages at the address instead of joining a live import of the + // earlier ones (`NoCopyPages`). let buffer = { - let _gate = device.2.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); + use std::sync::atomic::Ordering::Relaxed; + let mut gate = device.2.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); + let range = (pointer as u64, length); + let now = pages_identity(range.0, range.1); + let changed = match gate.seen.get(&range) { + Some(¬ed) => now != Some(noted), + None => gate.forgot || now.is_none(), + }; + NO_COPY_REMAPS[0].fetch_add(1, Relaxed); + if changed { + NO_COPY_REMAPS[1].fetch_add(1, Relaxed); + device.0.host_range_remapped(base, length); + } // SAFETY: as above. - unsafe { device.0.new_buffer_with_bytes_no_copy(memory, length, modes, deallocator) } + let buffer = unsafe { device.0.new_buffer_with_bytes_no_copy(memory, length, modes, deallocator) }; + // Noted as they are once the buffer pinned them (pinning for writing may give the range objects of its own). + match (&buffer, pages_identity(range.0, range.1)) { + (Ok(_), Some(pages)) => { + if gate.seen.len() >= NO_COPY_RANGES_NOTED && !gate.seen.contains_key(&range) { + gate.seen.clear(); + gate.forgot = true; + } + gate.seen.insert(range, pages); + } + _ => { + gate.seen.remove(&range); + } + } + buffer }; Ok(boxed(NmbBuffer(buffer.map_err(e("no-copy buffer"))?))) }) From 489d61c9581d9c0b03de68f2041e14c54927a5df Mon Sep 17 00:00:00 2001 From: HuJK Date: Sun, 25 Oct 2026 04:18:21 +0800 Subject: [PATCH 32/92] Compile Metal source at run time with the system's own front end newLibraryWithSource: (and its completion-handler form) was not implemented, so GFXBench, which compiles every shader from source, loaded no test (its results stayed N/A), as would WebKit's WebGL. The frontend now asks MTLCompiler.framework, which MTLCompilerService runs for Apple's Metal, to compile the source into a metallib (request 13: the source and the front end's arguments for the compile options: language version, fast math, invariance, size optimization, macros, a module cache in the process's temporary directory) and loads it as a library from data. Failures come back as MTLLibraryErrorCompileFailure with the compiler's message; warnings as a CompileWarning error with the library, as Metal reports them. Metal Check case 41 passes on the guest; GFXBench's ALU 2 runs (684-716 frames). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012Camiwa7VsPwkhfyscKQ6S --- NativeBridgeMetalFrontend.mm | 96 ++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 847e37a..9716c93 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -345,6 +345,74 @@ static void noteFormat(uint32_t format,bool made) { for(unsigned i=0;i request(16+sourceBytes+optionBytes,0); + memcpy(request.data(),&sourceBytes,8);memcpy(request.data()+8,&optionBytes,8); + memcpy(request.data()+16,text.bytes,text.length);memcpy(request.data()+16+sourceBytes,options,optionBytes); + __block NSData *metallib=nil; + __block NSString *warned=nil,*message=nil; + __block bool called=false; + { + std::lock_guard guard(lock); + build(service,nullptr,13,request.data(),request.size(),^(int32_t status,const void *reply,size_t length,const char *why) { + called=true; + uint32_t header=0,warning=0; + if(reply && length>=16){memcpy(&header,(const uint8_t *)reply+8,4);memcpy(&warning,(const uint8_t *)reply+12,4);} + if(status || !reply || length<16 || (uint64_t)header+warning>length) { + message=why && *why?@(why):[NSString stringWithFormat:@"the Metal compiler failed (status %d)",status]; + return; + } + if(warning)warned=[[NSString alloc] initWithBytes:(const uint8_t *)reply+header length:warning encoding:NSUTF8StringEncoding]; + metallib=[NSData dataWithBytes:(const uint8_t *)reply+header+warning length:length-header-warning]; + }); + } + if(!called)message=@"the Metal compiler did not answer"; + *warnings=warned;*failure=message; + return message?nil:metallib; +} +// The front end's arguments for a set of compile options, as Metal passes them: the language version, fast math (Metal's default), +// invariance, size optimization and the preprocessor macros; module caches in this process's temporary directory, which a sandboxed +// client may write. +static NSString *compileArgument(NSString *text) { + if([text rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@" \t\"\\'"]].location==NSNotFound)return text; + NSString *escaped=[[text stringByReplacingOccurrencesOfString:@"\\" withString:@"\\\\"] stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""]; + return [NSString stringWithFormat:@"\"%@\"",escaped]; +} +static NSString *compileArguments(MTLCompileOptions *options) { + NSMutableArray *arguments=[NSMutableArray arrayWithObjects:@"--driver-mode=metal",@"-x",@"metal",nil]; + const NSUInteger version=options && options.languageVersion?(NSUInteger)options.languageVersion:(NSUInteger)MTLLanguageVersion3_1; + const unsigned major=(unsigned)(version>>16),minor=(unsigned)(version&0xffff); + [arguments addObject:[NSString stringWithFormat:major>=3?@"-std=metal%u.%u":@"-std=ios-metal%u.%u",major,minor]]; + const BOOL fast=options?((BOOL (*)(id,SEL))objc_msgSend)(options,sel_registerName("fastMathEnabled")):YES; + [arguments addObject:fast?@"-ffast-math":@"-fno-fast-math"]; + if(options.preserveInvariance)[arguments addObject:@"-fpreserve-invariance"]; + if(options.optimizationLevel==MTLLibraryOptimizationLevelSize)[arguments addObject:@"-Os"]; + [options.preprocessorMacros enumerateKeysAndObjectsUsingBlock:^(NSString *name,NSObject *value,BOOL *) { + [arguments addObject:compileArgument([NSString stringWithFormat:@"-D%@=%@",name,value])]; + }]; + [arguments addObject:compileArgument([@"-fmodules-cache-path=" stringByAppendingString:[NSTemporaryDirectory() stringByAppendingPathComponent:@"m2v-msl"]])]; + return [arguments componentsJoinedByString:@" "]; +} static NSError *nbError(NSString *why) { return [NSError errorWithDomain:@"org.blu-sharky.native-metal" code:-1 userInfo:@{NSLocalizedDescriptionKey:why?:@"refused"}]; } @@ -1949,6 +2017,34 @@ static bool pipelineArchives(NSArray *list,std::vector &arch return [(id)self newLibraryWithURL:url error:error]; } - (id)newDefaultLibrary {return [(id)self newDefaultLibraryWithBundle:[NSBundle mainBundle] error:nil];} +- (id)newLibraryWithSource:(NSString *)source options:(MTLCompileOptions *)options error:(NSError **)error { + if(!source){if(error)*error=nbError(@"no source");return nil;} + if(options && options.libraryType==MTLLibraryTypeDynamic) { + if(error)*error=[NSError errorWithDomain:MTLLibraryErrorDomain code:MTLLibraryErrorUnsupported + userInfo:@{NSLocalizedDescriptionKey:@"dynamic libraries are not supported"}]; + return nil; + } + NSString *warnings=nil,*failure=nil; + NSData *metallib=compileMetalSource(source,compileArguments(options),&warnings,&failure); + if(!metallib) { + NBLOG(@"native-metal: newLibraryWithSource: %@",[failure substringToIndex:MIN(failure.length,(NSUInteger)300)]); + if(error)*error=[NSError errorWithDomain:MTLLibraryErrorDomain code:MTLLibraryErrorCompileFailure userInfo:@{NSLocalizedDescriptionKey:failure}]; + return nil; + } + dispatch_data_t data=dispatch_data_create(metallib.bytes,metallib.length,nullptr,^{(void)metallib;}); + id library=[(id)self newLibraryWithData:data error:error]; + // As Metal does: a library compiled with warnings comes with them as a CompileWarning error. + if(library && warnings.length && error) + *error=[NSError errorWithDomain:MTLLibraryErrorDomain code:MTLLibraryErrorCompileWarning userInfo:@{NSLocalizedDescriptionKey:warnings}]; + return library; +} +- (void)newLibraryWithSource:(NSString *)source options:(MTLCompileOptions *)options completionHandler:(MTLNewLibraryCompletionHandler)handler { + dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED,0),^{ + NSError *error=nil; + id library=[(id)self newLibraryWithSource:source options:options error:&error]; + if(handler)handler(library,error); + }); +} - (id)newComputePipelineStateWithFunction:(id)function error:(NSError **)error { return [(id)self newComputePipelineStateWithFunction:function options:MTLPipelineOptionNone reflection:nil error:error]; } From 885ce6351da63f882e7bc9751c5d95a3d868fc96 Mon Sep 17 00:00:00 2001 From: HuJK Date: Sun, 25 Oct 2026 15:02:04 +0800 Subject: [PATCH 33/92] Stitch functions with the system's own front end (Core Image's kernels) Core Image builds its GPU kernels by stitching ci_stdlib_stitchable's functions into a DAG with builtin and buffer nodes. Metal's own newLibraryWithStitchedDescriptor: wants Metal's function objects and refused these ("does not conform to MTLFunction"), so every Core Image kernel failed: IconServices' graphic icons (Settings' rows) lost their glyphs. The device now answers the stitched-library methods itself: the DAG text from the base class's newDagStringWithGraphs:, each function's wrapped bitcode from its library's metallib (libraries keep their bytes, functions their library), as Metal's MTLLibraryBuilder writes them for the front end's request 14; the metallib it returns loads as any other. The front-end call is now generic over the request type (13 stays Metal source). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012Camiwa7VsPwkhfyscKQ6S --- NativeBridgeMetalFrontend.mm | 123 ++++++++++++++++++++++++++++++----- 1 file changed, 105 insertions(+), 18 deletions(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 9716c93..84068ad 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -345,13 +345,12 @@ static void noteFormat(uint32_t format,bool made) { for(unsigned i=0;i &request,NSString **warnings,NSString **failure) { static void *service; static void (*build)(void *,void *,uint32_t,const void *,size_t,NBCodeGenReply); static dispatch_once_t once; @@ -363,18 +362,12 @@ static NSData *compileMetalSource(NSString *source,NSString *arguments,NSString service=create && build?create("native-metal"):nullptr; }); if(!service){*failure=@"the system's Metal compiler (MTLCompiler.framework) is not available";return nil;} - NSData *text=[source dataUsingEncoding:NSUTF8StringEncoding]; - const char *options=arguments.UTF8String; - const uint64_t sourceBytes=(text.length+1+3)&~uint64_t(3),optionBytes=strlen(options)+1; - std::vector request(16+sourceBytes+optionBytes,0); - memcpy(request.data(),&sourceBytes,8);memcpy(request.data()+8,&optionBytes,8); - memcpy(request.data()+16,text.bytes,text.length);memcpy(request.data()+16+sourceBytes,options,optionBytes); __block NSData *metallib=nil; __block NSString *warned=nil,*message=nil; __block bool called=false; { std::lock_guard guard(lock); - build(service,nullptr,13,request.data(),request.size(),^(int32_t status,const void *reply,size_t length,const char *why) { + build(service,nullptr,type,request.data(),request.size(),^(int32_t status,const void *reply,size_t length,const char *why) { called=true; uint32_t header=0,warning=0; if(reply && length>=16){memcpy(&header,(const uint8_t *)reply+8,4);memcpy(&warning,(const uint8_t *)reply+12,4);} @@ -390,6 +383,17 @@ static NSData *compileMetalSource(NSString *source,NSString *arguments,NSString *warnings=warned;*failure=message; return message?nil:metallib; } +// Metal source, request 13: two little-endian u64 lengths, the source NUL-padded to a multiple of 4 bytes and the arguments +// NUL-terminated. +static NSData *compileMetalSource(NSString *source,NSString *arguments,NSString **warnings,NSString **failure) { + NSData *text=[source dataUsingEncoding:NSUTF8StringEncoding]; + const char *options=arguments.UTF8String; + const uint64_t sourceBytes=(text.length+1+3)&~uint64_t(3),optionBytes=strlen(options)+1; + std::vector request(16+sourceBytes+optionBytes,0); + memcpy(request.data(),&sourceBytes,8);memcpy(request.data()+8,&optionBytes,8); + memcpy(request.data()+16,text.bytes,text.length);memcpy(request.data()+16+sourceBytes,options,optionBytes); + return codeGenRequest(13,request,warnings,failure); +} // The front end's arguments for a set of compile options, as Metal passes them: the language version, fast math (Metal's default), // invariance, size optimization and the preprocessor macros; module caches in this process's temporary directory, which a sandboxed // client may write. @@ -680,6 +684,9 @@ static NBTexture *textureOnSurface(NBBuffer *buffer,MTLTextureDescriptor *d,IOSu // ---- libraries, functions, states ---- @interface NBLibrary : NBObject @property(nonatomic) NmbLibrary *handle; +@property(nonatomic,strong) id storage; // what keeps the metallib's bytes mapped: its functions' bitcode, for stitching +@property(nonatomic) const uint8_t *bytes; +@property(nonatomic) size_t length; - (id)nb_newFunctionWithName:(NSString *)name constantValues:(MTLFunctionConstantValues *)values zeroRest:(BOOL)zeroRest error:(NSError **)error; @end @@ -688,8 +695,8 @@ static NBTexture *textureOnSurface(NBBuffer *buffer,MTLTextureDescriptor *d,IOSu @property(nonatomic,copy) NSString *name; @property(nonatomic) MTLFunctionType functionType; @property(nonatomic) std::string constantsText; // name:type:hex of each constant value it was specialized with (the journal's) -@property(nonatomic,strong) NBLibrary *library; // where it came from, and the values it was specialized with (nil: none), for -@property(nonatomic,strong) MTLFunctionConstantValues *constantValues; // the zero-rest rebuild of a refused pipeline +@property(nonatomic,strong) NBLibrary *library; // where it came from (stitching reads its bitcode), and the values it was +@property(nonatomic,strong) MTLFunctionConstantValues *constantValues; // specialized with (nil: none), for the zero-rest rebuild of a refused pipeline @end @implementation NBFunction - (void)dealloc {nmb_function_release(_handle);} @@ -814,6 +821,39 @@ static std::string quartzCoreCallers(); return (id)function; } @end +// A function's wrapped bitcode in its library's metallib: the function list's entry NAMEd as the function, MDSZ bytes at OFFT's bitcode +// offset into the bitcode section (header: function list at 24, bitcode section offset and size at 72 and 80; tags a fourcc, a u16 +// length and the bytes, up to ENDT). +static NSData *functionBitcode(NBFunction *function) { + NBLibrary *library=function.library;const uint8_t *file=library.bytes;const size_t length=library.length; + const char *wanted=function.name.UTF8String; + if(!file || !wanted || length<88 || memcmp(file,"MTLB",4))return nil; + uint64_t listAt,bitcodeAt,bitcodeSize;memcpy(&listAt,file+24,8);memcpy(&bitcodeAt,file+72,8);memcpy(&bitcodeSize,file+80,8); + if(listAt>length-4 || bitcodeAt>length || bitcodeSize>length-bitcodeAt)return nil; + uint32_t count;memcpy(&count,file+listAt,4); + size_t at=listAt+4; + for(uint32_t i=0;ilength-4)return nil;memcpy(&size,file+at,4); + if(size<8 || size>length-at)return nil; + const size_t end=at+size; + std::string name;uint64_t bytes=0,offset=0;bool sized=false,placed=false; + for(size_t t=at+4;t+4<=end && memcmp(file+t,"ENDT",4);) { + if(t+6>end)return nil; + uint16_t n;memcpy(&n,file+t+4,2);const uint8_t *v=file+t+6; + if(n>end-(t+6))return nil; + if(!memcmp(file+t,"NAME",4))name.assign((const char *)v,strnlen((const char *)v,n)); + else if(!memcmp(file+t,"MDSZ",4) && n>=8){memcpy(&bytes,v,8);sized=true;} + else if(!memcmp(file+t,"OFFT",4) && n>=24){memcpy(&offset,v+16,8);placed=true;} + t+=6+n; + } + if(name==wanted) { + if(!sized || !placed || offset>bitcodeSize || bytes>bitcodeSize-offset)return nil; + return [NSData dataWithBytes:file+bitcodeAt+offset length:(NSUInteger)bytes]; + } + at=end; + } + return nil; +} @interface NBComputePipeline : NBObject @property(nonatomic) NmbComputePipeline *handle; @end @@ -1861,6 +1901,10 @@ static bool pipelineArchives(NSArray *list,std::vector &arch return true; } +// The target's own _MTLDevice method that writes stitching graphs as the DAG text Metal's compiler reads (the base class of the device). +@protocol NBOriginalStitching +- (NSString *)newDagStringWithGraphs:(NSArray *)graphs; +@end @interface NBDeviceMethods : NSObject @end @implementation NBDeviceMethods @@ -1996,16 +2040,18 @@ static bool pipelineArchives(NSArray *list,std::vector &arch - (id)newLibraryWithData:(dispatch_data_t)data error:(NSError **)error { const void *bytes=nullptr;size_t size=0;dispatch_data_t mapped=dispatch_data_create_map(data,&bytes,&size); const uint64_t libraryBegan=nowUs(); - NmbLibrary *handle=nmb_library_new(gDevice,bytes,size,false);(void)mapped;gTimeLibrary.add(libraryBegan); + NmbLibrary *handle=nmb_library_new(gDevice,bytes,size,false);gTimeLibrary.add(libraryBegan); if(!handle)return failWith>(error,@"library",nil); - NBLibrary *library=[NBLibrary new];library.handle=handle;if(error)*error=nil;return (id)library; + NBLibrary *library=[NBLibrary new];library.handle=handle;library.storage=mapped;library.bytes=(const uint8_t *)bytes;library.length=size; + if(error)*error=nil;return (id)library; } - (id)newLibraryWithFile:(NSString *)path error:(NSError **)error { NSData *bytes=path?[NSData dataWithContentsOfFile:path options:NSDataReadingMappedIfSafe error:error]:nil;if(!bytes)return nil; const uint64_t libraryBegan=nowUs(); NmbLibrary *handle=nmb_library_new(gDevice,bytes.bytes,bytes.length,false);gTimeLibrary.add(libraryBegan); if(!handle)return failWith>(error,@"library",nil); - NBLibrary *library=[NBLibrary new];library.handle=handle;if(error)*error=nil;return (id)library; + NBLibrary *library=[NBLibrary new];library.handle=handle;library.storage=bytes;library.bytes=(const uint8_t *)bytes.bytes;library.length=bytes.length; + if(error)*error=nil;return (id)library; } - (id)newLibraryWithURL:(NSURL *)url error:(NSError **)error { if(!url.isFileURL){if(error)*error=nbError(@"a file URL is required");return nil;} @@ -2045,6 +2091,47 @@ static bool pipelineArchives(NSArray *list,std::vector &arch if(handler)handler(library,error); }); } +// Function stitching (Core Image makes its kernels so: a DAG of ci_stdlib_stitchable's functions with builtin and buffer nodes) is the +// front end's request 14, as Metal's own MTLLibraryBuilder writes it: u32 fourcc tags, 'dag ' and the graphs' DAG text NUL-terminated, +// 'numf' and the u32 count of functions, then for each function 'bitc', the u32 size and its wrapped bitcode. The reply is a metallib +// with the stitched functions. Metal's own path wants its own function objects (their library data) and refuses these. +- (id)newLibraryWithStitchedDescriptor:(MTLStitchedLibraryDescriptor *)d error:(NSError **)error { + NSArray *graphs=d.functionGraphs;NSArray> *functions=d.functions; + if(!graphs.count || !functions.count){if(error)*error=nbError(@"a stitched library needs graphs and functions");return nil;} + NSString *dag=[(id)self newDagStringWithGraphs:graphs]; + if(!dag.length){if(error)*error=nbError(@"the stitching graphs have no DAG text");return nil;} + std::vector request; + auto word=[&](uint32_t v){const uint8_t *b=(const uint8_t *)&v;request.insert(request.end(),b,b+4);}; + const char *text=dag.UTF8String; + word(0x64616720 /* 'dag ' */);request.insert(request.end(),text,text+strlen(text)+1); + word(0x6e756d66 /* 'numf' */);word((uint32_t)functions.count); + for(id function in functions) { + NSData *bitcode=[function isKindOfClass:[NBFunction class]]?functionBitcode((NBFunction *)function):nil; + if(!bitcode){if(error)*error=nbError([NSString stringWithFormat:@"stitched function %@ has no bitcode here",[function name]]);return nil;} + word(0x62697463 /* 'bitc' */);word((uint32_t)bitcode.length); + const uint8_t *b=(const uint8_t *)bitcode.bytes;request.insert(request.end(),b,b+bitcode.length); + } + NSString *warnings=nil,*failure=nil; + NSData *metallib=codeGenRequest(14,request,&warnings,&failure); + if(!metallib) { + NBLOG(@"native-metal: stitched library: %@",[failure substringToIndex:MIN(failure.length,(NSUInteger)300)]); + noteProblem([@"stitched library: " stringByAppendingString:[failure substringToIndex:MIN(failure.length,(NSUInteger)120)]]); + if(error)*error=[NSError errorWithDomain:MTLLibraryErrorDomain code:MTLLibraryErrorCompileFailure userInfo:@{NSLocalizedDescriptionKey:failure}]; + return nil; + } + dispatch_data_t data=dispatch_data_create(metallib.bytes,metallib.length,nullptr,^{(void)metallib;}); + return [(id)self newLibraryWithData:data error:error]; +} +- (id)newLibraryWithStitchedDescriptor:(MTLStitchedLibraryDescriptor *)d destinationBinaryArchive:(id)archive error:(NSError **)error { + (void)archive;return [(id)self newLibraryWithStitchedDescriptor:d error:error]; +} +- (void)newLibraryWithStitchedDescriptor:(MTLStitchedLibraryDescriptor *)d completionHandler:(MTLNewLibraryCompletionHandler)handler { + dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED,0),^{ + NSError *error=nil; + id library=[(id)self newLibraryWithStitchedDescriptor:d error:&error]; + if(handler)handler(library,error); + }); +} - (id)newComputePipelineStateWithFunction:(id)function error:(NSError **)error { return [(id)self newComputePipelineStateWithFunction:function options:MTLPipelineOptionNone reflection:nil error:error]; } From 7677e0d8b5369e07f5507eb844af70a5cf669c34 Mon Sep 17 00:00:00 2001 From: HuJK Date: Sun, 25 Oct 2026 20:21:50 +0800 Subject: [PATCH 34/92] Answer the deprecated feature-set query from the GPU families supportsFeatureSet: was not implemented, so it answered NO to every set. GFXBench still asks it: Car Chase refused to start (UNSUPPORTED_TC_TYPE: ASTC is iOS_GPUFamily2_v1) and saw no tessellation (iOS_GPUFamily3_v2). An iOS set now answers as supportsFamily: does for its Apple family (iOS_GPUFamily1_v1 to iOS_GPUFamily5_v1); macOS and tvOS sets are not this platform. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012Camiwa7VsPwkhfyscKQ6S --- NativeBridgeMetalFrontend.mm | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 84068ad..9ab9fbf 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -1920,9 +1920,15 @@ static bool pipelineArchives(NSArray *list,std::vector &arch - (BOOL)isRemovable {return NO;} - (BOOL)isHeadless {return NO;} // supportsFamily: and the other queries the runtime answers are installed from it (installAnswers); supportsFamily: also answers the target -// hardware's families (the bridge's TARGET_FAMILIES, journaled with their waived rows as F lines). No legacy feature set is claimed: the -// base implementation reads Apple tables this device does not have. -- (BOOL)supportsFeatureSet:(MTLFeatureSet)set {(void)set;return NO;} +// hardware's families (the bridge's TARGET_FAMILIES, journaled with their waived rows as F lines). A legacy feature set is a family at an OS +// release: iOS_GPUFamilyN_vM is MTLGPUFamilyAppleN on an iOS that has vM (this one has them all), so it is answered as supportsFamily: +// answers that family. The base implementation would read Apple tables this device does not have; answering NO had GFXBench refuse +// Car Chase (UNSUPPORTED_TC_TYPE: ASTC is iOS_GPUFamily2_v1) and turn off tessellation. macOS and tvOS sets are not this platform. +- (BOOL)supportsFeatureSet:(MTLFeatureSet)set { + static const uint8_t families[]={1,2,1,2,3,1,2,3,1,2,3,4,1,2,3,4,5}; // MTLFeatureSet_iOS_GPUFamily1_v1 (0) to iOS_GPUFamily5_v1 (16) + if((NSUInteger)set>=sizeof families)return NO; + return [(id)self supportsFamily:(MTLGPUFamily)(MTLGPUFamilyApple1+families[set]-1)]; +} // The original discovery (_MTLAddDevice) runs these four on every device it registers. The target's own versions fill the base class's limits // and feature tables from a hardware profile (`featureProfile`, `indirectArgumentBufferCapabilities`, a table of Apple GPUs) that this device // has not got: running them would claim an Apple GPU's capabilities. They are empty here, so a limit or feature the runtime measured is answered From 77d40b8b4ccc5552d4be4d1c99cfd259312dcee1 Mon Sep 17 00:00:00 2001 From: HuJK Date: Sun, 25 Oct 2026 20:21:59 +0800 Subject: [PATCH 35/92] Run the in-process Metal compiler on a thread with a 16 MiB stack MTLCompiler.framework's front end was written for MTLCompilerService's threads and recurses deeply. Here it runs on the thread that asked, and a dispatch worker's 512 KiB stack was too small: GFXBench's Aztec Ruins compiles its shaders on one and aborted in libGPUCompilerImplLazy (stack protector, "stack buffer overflow"). Each request now runs on a thread of its own with a 16 MiB stack while the caller waits; Aztec Ruins High runs to its score (477, 7.4 fps). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012Camiwa7VsPwkhfyscKQ6S --- NativeBridgeMetalFrontend.mm | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 9ab9fbf..07c7893 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -350,6 +350,17 @@ static void noteFormat(uint32_t format,bool made) { // to a block called before the request returns, is a header whose u32s at 8 and 12 are its own size and that of the warnings after it, // then the metallib. One service per process, one request at a time. typedef void (^NBCodeGenReply)(int32_t status,const void *reply,size_t length,const char *message); +// The compiler was written for MTLCompilerService's threads and recurses deeply; here it runs on whatever thread asked, and a dispatch +// worker's 512 KiB stack was too small for it (Aztec Ruins' shaders: a stack-protector abort inside libGPUCompilerImplLazy). A request +// runs on a thread of its own with a 16 MiB stack, the caller waiting. +static void *runBlock(void *block) {@autoreleasepool {((__bridge void (^)(void))block)();}return nullptr;} +static void onLargeStack(void (^body)(void)) { + pthread_attr_t attr;pthread_t thread; + if(pthread_attr_init(&attr)){body();return;} + const bool made=!pthread_attr_setstacksize(&attr,16u<<20) && !pthread_create(&thread,&attr,runBlock,(__bridge void *)body); + pthread_attr_destroy(&attr); + if(made)pthread_join(thread,nullptr);else body(); +} static NSData *codeGenRequest(uint32_t type,const std::vector &request,NSString **warnings,NSString **failure) { static void *service; static void (*build)(void *,void *,uint32_t,const void *,size_t,NBCodeGenReply); @@ -367,7 +378,7 @@ static NSData *codeGenRequest(uint32_t type,const std::vector &request, __block bool called=false; { std::lock_guard guard(lock); - build(service,nullptr,type,request.data(),request.size(),^(int32_t status,const void *reply,size_t length,const char *why) { + onLargeStack(^{build(service,nullptr,type,request.data(),request.size(),^(int32_t status,const void *reply,size_t length,const char *why) { called=true; uint32_t header=0,warning=0; if(reply && length>=16){memcpy(&header,(const uint8_t *)reply+8,4);memcpy(&warning,(const uint8_t *)reply+12,4);} @@ -377,7 +388,7 @@ static NSData *codeGenRequest(uint32_t type,const std::vector &request, } if(warning)warned=[[NSString alloc] initWithBytes:(const uint8_t *)reply+header length:warning encoding:NSUTF8StringEncoding]; metallib=[NSData dataWithBytes:(const uint8_t *)reply+header+warning length:length-header-warning]; - }); + });}); } if(!called)message=@"the Metal compiler did not answer"; *warnings=warned;*failure=message; From 1e65fae511ed95ca5836905442e696ddcfd1e186 Mon Sep 17 00:00:00 2001 From: HuJK Date: Sun, 25 Oct 2026 20:22:11 +0800 Subject: [PATCH 36/92] Bind samplers with LOD clamps, and sampler ranges GFXBench's Car Chase binds its samplers with setFragmentSamplerState:lodMinClamp:lodMaxClamp:atIndex:, which was not implemented: the unrecognized selector raised in its occlusion culling's warm-up and the app exited. The runtime's samplers take their clamps when they are made, so a sampler keeps its descriptor and makes (once, and keeps) its twin with a binding's clamps, at most 64 pairs per sampler. The compute, vertex and fragment forms with clamps and the range forms with and without them bind through it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012Camiwa7VsPwkhfyscKQ6S --- NativeBridgeMetalFrontend.mm | 49 ++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 07c7893..9263e57 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -886,10 +886,28 @@ static NSData *functionBitcode(NBFunction *function) { @end @interface NBSampler : NBObject @property(nonatomic) NmbSampler *handle; +@property(nonatomic) NmbSamplerDescriptor descriptor; // what made it, for a binding that replaces its LOD clamps @end -@implementation NBSampler +@implementation NBSampler { + NSMutableDictionary *_clamped; +} - (void)dealloc {nmb_sampler_release(_handle);} - (NSUInteger)pixelFormat {return MTLPixelFormatInvalid;} // SPI: a sampler built from a plain descriptor names no format +// This sampler with its LOD clamps replaced (a binding's lodMinClamp:lodMaxClamp:, which GFXBench's Car Chase uses): made once per pair +// and kept, at most 64 pairs (a caller that varies them every draw starts over rather than growing without bound). +- (NBSampler *)withLodMin:(float)lodMin max:(float)lodMax { + if(lodMin==_descriptor.lod_min && lodMax==_descriptor.lod_max)return self; + NSString *key=[NSString stringWithFormat:@"%a %a",lodMin,lodMax]; + @synchronized(self) { + if(!_clamped || _clamped.count>=64)_clamped=[NSMutableDictionary dictionary]; + if(NBSampler *found=_clamped[key])return found; + NmbSamplerDescriptor d=_descriptor;d.lod_min=lodMin;d.lod_max=lodMax; + NmbSampler *handle=nmb_sampler_new(gDevice,&d); + if(!handle){NBLOG(@"native-metal: sampler with LOD clamps refused: %@",lastError());return nil;} + NBSampler *sampler=[NBSampler new];sampler.handle=handle;sampler.descriptor=d;_clamped[key]=sampler; + return sampler; + } +} @end @interface NBDepthStencil : NBObject @property(nonatomic) NmbDepthStencil *handle; @@ -1090,6 +1108,12 @@ static void waitForResource(id resource) { if(sampler && ![(id)sampler isKindOfClass:[NBSampler class]]){[_commandBuffer refuse:@"foreign sampler"];return;} [self check:nmb_encoder_set_sampler(_handle,stage,sampler?[(NBSampler *)sampler handle]:nullptr,(uint32_t)index) what:"setSamplerState"]; } +- (void)sampler:(id)sampler lodMin:(float)lodMin max:(float)lodMax index:(NSUInteger)index stage:(uint32_t)stage { + if(sampler && ![(id)sampler isKindOfClass:[NBSampler class]]){[_commandBuffer refuse:@"foreign sampler"];return;} + NBSampler *clamped=sampler?[(NBSampler *)sampler withLodMin:lodMin max:lodMax]:nil; + if(sampler && !clamped){[_commandBuffer refuse:@"sampler with LOD clamps refused"];return;} + [self sampler:(id)clamped index:index stage:stage]; +} - (void)bytes:(const void *)bytes length:(NSUInteger)length index:(NSUInteger)index stage:(uint32_t)stage { [self check:nmb_encoder_set_bytes(_handle,stage,bytes,length,(uint32_t)index) what:"setBytes"]; } @@ -1124,6 +1148,13 @@ static void waitForResource(id resource) { for(NSUInteger k=0;k)s atIndex:(NSUInteger)i {[self sampler:s index:i stage:0];} +- (void)setSamplerState:(id)s lodMinClamp:(float)a lodMaxClamp:(float)b atIndex:(NSUInteger)i {[self sampler:s lodMin:a max:b index:i stage:0];} +- (void)setSamplerStates:(const id __unsafe_unretained [])s withRange:(NSRange)r { + for(NSUInteger k=0;k __unsafe_unretained [])s lodMinClamps:(const float [])a lodMaxClamps:(const float [])b withRange:(NSRange)r { + for(NSUInteger k=0;k)s atIndex:(NSUInteger)i {[self sampler:s index:i stage:1];} - (void)setFragmentSamplerState:(id)s atIndex:(NSUInteger)i {[self sampler:s index:i stage:2];} +- (void)setVertexSamplerState:(id)s lodMinClamp:(float)a lodMaxClamp:(float)b atIndex:(NSUInteger)i {[self sampler:s lodMin:a max:b index:i stage:1];} +- (void)setFragmentSamplerState:(id)s lodMinClamp:(float)a lodMaxClamp:(float)b atIndex:(NSUInteger)i {[self sampler:s lodMin:a max:b index:i stage:2];} +- (void)setVertexSamplerStates:(const id __unsafe_unretained [])s withRange:(NSRange)r { + for(NSUInteger k=0;k __unsafe_unretained [])s withRange:(NSRange)r { + for(NSUInteger k=0;k __unsafe_unretained [])s lodMinClamps:(const float [])a lodMaxClamps:(const float [])b withRange:(NSRange)r { + for(NSUInteger k=0;k __unsafe_unretained [])s lodMinClamps:(const float [])a lodMaxClamps:(const float [])b withRange:(NSRange)r { + for(NSUInteger k=0;k)state { if(state && ![(id)state isKindOfClass:[NBDepthStencil class]]){[self.commandBuffer refuse:@"foreign depth/stencil state"];return;} [self check:nmb_render_set_depth_stencil(self.handle,state?[(NBDepthStencil *)state handle]:nullptr) what:"setDepthStencilState"]; @@ -2251,7 +2296,7 @@ static bool pipelineArchives(NSArray *list,std::vector &arch (uint64_t)d.borderColor,(uint64_t)d.normalizedCoordinates,d.maxAnisotropy,(uint64_t)d.compareFunction,(uint64_t)d.lodAverage,d.lodMinClamp,d.lodMaxClamp}; NmbSampler *handle=nmb_sampler_new(gDevice,&s); if(!handle){NBLOG(@"native-metal: sampler refused: %@",lastError());return nil;} - NBSampler *sampler=[NBSampler new];sampler.handle=handle;return (id)sampler; + NBSampler *sampler=[NBSampler new];sampler.handle=handle;sampler.descriptor=s;return (id)sampler; } - (id)newDepthStencilStateWithDescriptor:(MTLDepthStencilDescriptor *)d { auto face=[](MTLStencilDescriptor *s)->NmbStencil { From c4534d9369356ddb6e5f83bb6697be805923e1b4 Mon Sep 17 00:00:00 2001 From: HuJK Date: Sun, 25 Oct 2026 20:22:11 +0800 Subject: [PATCH 37/92] Tessellation: post-tessellation pipelines, factor buffers and patch draws GFXBench's Car Chase tessellates, and the frontend had none of it: its first patch draw was an unrecognized selector and the app exited. The translator implements Metal's tessellation; the bridge now passes it through: - a render pipeline whose vertex function is a post-tessellation one takes the descriptor's tessellation state (maximum factor, partitioning, factor format and step function, output winding, control-point index type, factor scale); - functions answer patchType and patchControlPointCount; - render encoders take setTessellationFactorBuffer:offset:instanceStride:, setTessellationFactorScale: and the four patch draws (direct and indirect, with and without a control-point index buffer), whose buffers live with the command buffer. The descriptor's new fields are at its end, and a zeroed descriptor stays untessellated. Car Chase runs to its score (652, 11 fps). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012Camiwa7VsPwkhfyscKQ6S --- NativeBridgeMetalFrontend.mm | 49 +++++++++++++++++++ bridge/NativeMetalObjects.h | 15 ++++++ bridge/NativeMetalObjects.rs | 93 ++++++++++++++++++++++++++++++++++-- 3 files changed, 154 insertions(+), 3 deletions(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 9263e57..0e2865b 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -712,6 +712,8 @@ static NBTexture *textureOnSurface(NBBuffer *buffer,MTLTextureDescriptor *d,IOSu @implementation NBFunction - (void)dealloc {nmb_function_release(_handle);} - (MTLFunctionOptions)options {return MTLFunctionOptionNone;} +- (MTLPatchType)patchType {return (MTLPatchType)nmb_function_patch_type(_handle);} +- (NSInteger)patchControlPointCount {return (NSInteger)nmb_function_patch_control_point_count(_handle);} @end // The target's own accessors of MTLFunctionConstantValues (as the offline frontend reads them); their ABI is checked before any call. @interface MTLFunctionConstantValues (NBOriginalConstantAccess) @@ -1285,6 +1287,48 @@ static void waitForResource(id resource) { - (void)drawIndexedPrimitives:(MTLPrimitiveType)t indexCount:(NSUInteger)n indexType:(MTLIndexType)k indexBuffer:(id)b indexBufferOffset:(NSUInteger)o { [self drawIndexedPrimitives:t indexCount:n indexType:k indexBuffer:b indexBufferOffset:o instanceCount:1 baseVertex:0 baseInstance:0]; } +// ---- tessellation ---- +// A buffer a patch draw reads, kept alive with the command buffer; NO (refused) for one this frontend did not make. +- (BOOL)own:(id)b what:(NSString *)what { + if(!b)return YES; + if(![(id)b isKindOfClass:[NBBuffer class]]){[self.commandBuffer refuse:[@"foreign " stringByAppendingString:what]];return NO;} + [self.commandBuffer.resourceOwners addObject:b];return YES; +} +- (void)setTessellationFactorBuffer:(id)b offset:(NSUInteger)o instanceStride:(NSUInteger)stride { + if(![self own:b what:@"tessellation factor buffer"])return; + [self check:nmb_render_set_tessellation_factor_buffer(self.handle,b?[(NBBuffer *)b handle]:nullptr,o,stride) what:"setTessellationFactorBuffer"]; +} +- (void)setTessellationFactorScale:(float)scale { + [self check:nmb_render_set_tessellation_factor_scale(self.handle,scale) what:"setTessellationFactorScale"]; +} +- (void)patches:(NSUInteger)points start:(NSUInteger)start count:(NSUInteger)count patchIndex:(id)pi offset:(NSUInteger)po + points:(id)ci offset:(NSUInteger)co instances:(NSUInteger)instances base:(NSUInteger)base indirect:(id)ib offset:(NSUInteger)io { + if(![self own:pi what:@"patch index buffer"] || ![self own:ci what:@"control point index buffer"] || ![self own:ib what:@"indirect buffer"])return; + gDraws++;self.commandBuffer->encodedWork.draws++; + [self check:nmb_render_draw_patches(self.handle,(uint32_t)points,(uint32_t)start,(uint32_t)count,pi?[(NBBuffer *)pi handle]:nullptr,po, + ci?[(NBBuffer *)ci handle]:nullptr,co,(uint32_t)instances,(uint32_t)base,ib?[(NBBuffer *)ib handle]:nullptr,io) what:"drawPatches"]; +} +- (void)drawPatches:(NSUInteger)n patchStart:(NSUInteger)s patchCount:(NSUInteger)c patchIndexBuffer:(id)pi patchIndexBufferOffset:(NSUInteger)po + instanceCount:(NSUInteger)i baseInstance:(NSUInteger)b { + [self patches:n start:s count:c patchIndex:pi offset:po points:nil offset:0 instances:i base:b indirect:nil offset:0]; +} +- (void)drawPatches:(NSUInteger)n patchIndexBuffer:(id)pi patchIndexBufferOffset:(NSUInteger)po indirectBuffer:(id)ib + indirectBufferOffset:(NSUInteger)io { + if(!ib){[self.commandBuffer refuse:@"nil indirect buffer"];return;} + [self patches:n start:0 count:0 patchIndex:pi offset:po points:nil offset:0 instances:0 base:0 indirect:ib offset:io]; +} +- (void)drawIndexedPatches:(NSUInteger)n patchStart:(NSUInteger)s patchCount:(NSUInteger)c patchIndexBuffer:(id)pi + patchIndexBufferOffset:(NSUInteger)po controlPointIndexBuffer:(id)ci controlPointIndexBufferOffset:(NSUInteger)co + instanceCount:(NSUInteger)i baseInstance:(NSUInteger)b { + if(!ci){[self.commandBuffer refuse:@"nil control point index buffer"];return;} + [self patches:n start:s count:c patchIndex:pi offset:po points:ci offset:co instances:i base:b indirect:nil offset:0]; +} +- (void)drawIndexedPatches:(NSUInteger)n patchIndexBuffer:(id)pi patchIndexBufferOffset:(NSUInteger)po + controlPointIndexBuffer:(id)ci controlPointIndexBufferOffset:(NSUInteger)co indirectBuffer:(id)ib + indirectBufferOffset:(NSUInteger)io { + if(!ci || !ib){[self.commandBuffer refuse:@"nil control point index or indirect buffer"];return;} + [self patches:n start:0 count:0 patchIndex:pi offset:po points:ci offset:co instances:0 base:0 indirect:ib offset:io]; +} - (void)useResource:(id)r usage:(MTLResourceUsage)u stages:(MTLRenderStages)s {(void)r;(void)u;(void)s;} - (void)updateFence:(id)fence afterStages:(MTLRenderStages)stages {[self fence:fence stages:stages update:true];} - (void)waitForFence:(id)fence beforeStages:(MTLRenderStages)stages {[self fence:fence stages:stages update:false];} @@ -1821,6 +1865,11 @@ static bool renderDescriptor(MTLRenderPipelineDescriptor *d,NmbRenderPipelineDes if(a.bufferIndex<31){MTLVertexBufferLayoutDescriptor *l=v.layouts[a.bufferIndex];p->layouts[a.bufferIndex]={l.stride,(uint64_t)l.stepFunction,l.stepRate};} } } + // A post-tessellation vertex function's pipeline draws patches with the descriptor's tessellation state (GFXBench's Car Chase). + if(nmb_function_patch_type(p->vertex)!=MTLPatchTypeNone) + p->tessellation={1,d.maxTessellationFactor,(uint64_t)d.tessellationPartitionMode,(uint64_t)d.tessellationFactorFormat, + (uint64_t)d.tessellationFactorStepFunction,(uint64_t)d.tessellationOutputWindingOrder,(uint64_t)d.tessellationControlPointIndexType, + (uint64_t)d.tessellationFactorScaleEnabled}; return true; } diff --git a/bridge/NativeMetalObjects.h b/bridge/NativeMetalObjects.h index 0d608bd..db47f17 100644 --- a/bridge/NativeMetalObjects.h +++ b/bridge/NativeMetalObjects.h @@ -132,6 +132,8 @@ NmbFunction *nmb_function_new_specialized_zero_rest(NmbLibrary *, const char *na void nmb_function_release(NmbFunction *); /* Metal's MTLFunctionType (1 vertex, 2 fragment, 3 kernel, ...). */ uint64_t nmb_function_type(const NmbFunction *); +uint64_t nmb_function_patch_type(const NmbFunction *); /* MTLPatchType */ +int64_t nmb_function_patch_control_point_count(const NmbFunction *); /* -1: no patch, or no count stated */ NmbComputePipeline *nmb_compute_pipeline_new(NmbDevice *, NmbFunction *); void nmb_compute_pipeline_release(NmbComputePipeline *); @@ -150,6 +152,11 @@ struct NmbRenderPipelineDescriptor { uint64_t vertex_inputs; /* 0: no vertex descriptor */ struct NmbVertexAttribute attributes[31]; struct NmbVertexLayout layouts[31]; + /* A post-tessellation vertex function's state; tessellates 0 leaves it unset (zeroed descriptors stay untessellated). */ + struct NmbTessellation { + uint64_t tessellates, max_factor, partition_mode, factor_format, factor_step_function, output_winding, control_point_index_type, + factor_scale_enabled; + } tessellation; }; NmbRenderPipeline *nmb_render_pipeline_new(NmbDevice *, const struct NmbRenderPipelineDescriptor *); /* Descriptor binaryArchives and MTLPipelineOption bits. NULL options means defaults. @@ -270,6 +277,14 @@ bool nmb_render_set_visibility_mode(NmbEncoder *, uint64_t mode, uint64_t offset bool nmb_render_draw(NmbEncoder *, uint64_t primitive, uint32_t start, uint32_t count, uint32_t instances, uint32_t base_instance); bool nmb_render_draw_indexed(NmbEncoder *, uint64_t primitive, uint32_t count, uint64_t index_type, NmbBuffer *indices, uint64_t offset, uint32_t instances, int32_t base_vertex, uint32_t base_instance); +/* NULL buffer unsets it. */ +bool nmb_render_set_tessellation_factor_buffer(NmbEncoder *, NmbBuffer *, uint64_t offset, uint64_t instance_stride); +bool nmb_render_set_tessellation_factor_scale(NmbEncoder *, float scale); +/* NULL indirect: patch_start, patch_count, instances and base_instance; otherwise read from it. NULL control_point_index: the patches' + * own control points. NULL patch_index: patches patch_start + i. */ +bool nmb_render_draw_patches(NmbEncoder *, uint32_t control_points, uint32_t patch_start, uint32_t patch_count, NmbBuffer *patch_index, + uint64_t patch_index_offset, NmbBuffer *control_point_index, uint64_t control_point_index_offset, + uint32_t instances, uint32_t base_instance, NmbBuffer *indirect, uint64_t indirect_offset); bool nmb_blit_copy_buffer(NmbEncoder *, NmbBuffer *source, uint64_t source_offset, NmbBuffer *destination, uint64_t destination_offset, uint64_t size); diff --git a/bridge/NativeMetalObjects.rs b/bridge/NativeMetalObjects.rs index a7039e9..b77cd6d 100644 --- a/bridge/NativeMetalObjects.rs +++ b/bridge/NativeMetalObjects.rs @@ -10,11 +10,12 @@ use metal2vulkan::library::{FunctionConstantValue, FunctionConstantValues}; use metal2vulkan::metal::{ BlendFactor, BlendOperation, ColorWriteMask, CompareFunction, CullMode, DataType, IndexType, PixelFormat, PrimitiveTopologyClass, PrimitiveType, RenderStages, SamplerAddressMode, SamplerBorderColor, SamplerMinMagFilter, SamplerMipFilter, StencilOperation, TextureType, - TextureUsage, TriangleFillMode, VertexFormat, VertexStepFunction, Winding, + TessellationControlPointIndexType, TessellationFactorFormat, TessellationFactorStepFunction, TessellationPartitionMode, TextureUsage, + TriangleFillMode, VertexFormat, VertexStepFunction, Winding, }; use metal2vulkan::pipeline::{ - ColorAttachmentDescriptor, ComputePipelineDescriptor, LinkedFunction, LinkedFunctions, RenderPipelineDescriptor, StageSource, VertexAttributeDescriptor, VertexBufferLayoutDescriptor, - VertexDescriptor, + ColorAttachmentDescriptor, ComputePipelineDescriptor, LinkedFunction, LinkedFunctions, RenderPipelineDescriptor, StageSource, + TessellationDescriptor, VertexAttributeDescriptor, VertexBufferLayoutDescriptor, VertexDescriptor, }; use metal2vulkan_runtime::{ BinaryArchive, BinaryArchiveDescriptor, BlitCommandEncoder, FamilyRow, FeatureRow, GpuFamily, RowState, BlitOption, Buffer, ClearColor, CommandBuffer, CommandQueue, ComputeCommandEncoder, @@ -1279,6 +1280,18 @@ pub extern "C" fn nmb_function_type(function: *const NmbFunction) -> u64 { guard(0, || Ok(get(function)?.0.function_type().map_err(e("function type"))?.as_raw() as u64)) } +/// `patchType`: 0 (none) for a function other than a post-tessellation vertex function. +#[unsafe(no_mangle)] +pub extern "C" fn nmb_function_patch_type(function: *const NmbFunction) -> u64 { + guard(0, || Ok(get(function)?.0.patch_type().map_err(e("patch type"))?.as_raw() as u64)) +} + +/// `patchControlPointCount`: -1 for no patch, or a patch that states no count. +#[unsafe(no_mangle)] +pub extern "C" fn nmb_function_patch_control_point_count(function: *const NmbFunction) -> i64 { + guard(-1, || Ok(get(function)?.0.patch_control_point_count().map_err(e("patch control point count"))?.map_or(-1, i64::from))) +} + // ---- pipelines and states ---- pub struct NmbComputePipeline(ComputePipelineState); @@ -1419,6 +1432,20 @@ pub struct NmbRenderPipelineDescriptor { pub vertex_inputs: u64, pub attributes: [NmbVertexAttribute; 31], pub layouts: [NmbVertexLayout; 31], + pub tessellation: NmbTessellation, +} + +/// A post-tessellation vertex function's state; `tessellates` 0 leaves the descriptor's tessellation unset. +#[repr(C)] +pub struct NmbTessellation { + pub tessellates: u64, + pub max_factor: u64, + pub partition_mode: u64, + pub factor_format: u64, + pub factor_step_function: u64, + pub output_winding: u64, + pub control_point_index_type: u64, + pub factor_scale_enabled: u64, } fn optional_format(value: u64) -> Result, String> { @@ -1513,6 +1540,19 @@ fn render_descriptor(descriptor: *const NmbRenderPipelineDescriptor) -> Result(t.partition_mode, "partition mode")?; + tessellation.factor_format = raw::(t.factor_format, "factor format")?; + tessellation.factor_step_function = raw::(t.factor_step_function, "factor step function")?; + tessellation.output_winding_order = raw::(t.output_winding, "output winding")?; + tessellation.control_point_index_type = raw::(t.control_point_index_type, + "control point index type")?; + tessellation.factor_scale_enabled = t.factor_scale_enabled != 0; + out.tessellation = Some(tessellation); + } Ok(out) } @@ -2363,6 +2403,53 @@ pub extern "C" fn nmb_render_draw_indexed(encoder: *mut NmbEncoder, primitive: u }) } +/// `setTessellationFactorBuffer:offset:instanceStride:`; a null buffer unsets it. +#[unsafe(no_mangle)] +pub extern "C" fn nmb_render_set_tessellation_factor_buffer(encoder: *mut NmbEncoder, buffer: *mut NmbBuffer, offset: u64, instance_stride: u64) + -> bool { + guard(false, || { + let buffer = if buffer.is_null() { None } else { Some(&get(buffer)?.0) }; + render(encoder)?.set_tessellation_factor_buffer(buffer, offset, instance_stride).map_err(e("tessellation factor buffer"))?; + Ok(true) + }) +} + +#[unsafe(no_mangle)] +pub extern "C" fn nmb_render_set_tessellation_factor_scale(encoder: *mut NmbEncoder, scale: f32) -> bool { + guard(false, || { + render(encoder)?.set_tessellation_factor_scale(scale); + Ok(true) + }) +} + +fn optional_buffer<'a>(buffer: *mut NmbBuffer) -> Result, String> { + if buffer.is_null() { Ok(None) } else { Ok(Some(&get(buffer)?.0)) } +} + +/// The four patch draws: `indirect` null takes `patch_start`, `patch_count`, `instances` and `base_instance`, otherwise reads them from +/// it at `indirect_offset`; `control_points_index` null draws the patches' own control points, otherwise indexes them through it. +#[unsafe(no_mangle)] +#[allow(clippy::too_many_arguments)] +pub extern "C" fn nmb_render_draw_patches(encoder: *mut NmbEncoder, control_points: u32, patch_start: u32, patch_count: u32, + patch_index: *mut NmbBuffer, patch_index_offset: u64, control_point_index: *mut NmbBuffer, + control_point_index_offset: u64, instances: u32, base_instance: u32, indirect: *mut NmbBuffer, + indirect_offset: u64) -> bool { + guard(false, || { + let r = render(encoder)?; + let patch_index = optional_buffer(patch_index)?; + let result = match (optional_buffer(control_point_index)?, optional_buffer(indirect)?) { + (None, None) => r.draw_patches(control_points, patch_start, patch_count, patch_index, patch_index_offset, instances, base_instance), + (None, Some(indirect)) => r.draw_patches_indirect(control_points, patch_index, patch_index_offset, indirect, indirect_offset), + (Some(points), None) => r.draw_indexed_patches(control_points, patch_start, patch_count, patch_index, patch_index_offset, points, + control_point_index_offset, instances, base_instance), + (Some(points), Some(indirect)) => r.draw_indexed_patches_indirect(control_points, patch_index, patch_index_offset, points, + control_point_index_offset, indirect, indirect_offset), + }; + result.map_err(e("patch draw"))?; + Ok(true) + }) +} + #[unsafe(no_mangle)] pub extern "C" fn nmb_blit_copy_buffer(encoder: *mut NmbEncoder, source: *mut NmbBuffer, source_offset: u64, destination: *mut NmbBuffer, destination_offset: u64, size: u64) -> bool { From 19280762160eb4b336b8f83d571558a56161fa39 Mon Sep 17 00:00:00 2001 From: HuJK Date: Mon, 26 Oct 2026 10:43:06 +0800 Subject: [PATCH 38/92] Abort probe: NATIVE_METAL_ABORT_PROBE=1 reports the stack guard, registers and frames of a process that aborts or faults A __stack_chk_fail, SIGSEGV or SIGBUS in a Metal process first writes to its standard error whether __stack_chk_guard changed since the library loaded and the words around it, the thread's registers with the first words of the memory each one points at and how many words of that 4 KiB page are zero, and the thread's frames (fp, lr and the 12 words below each fp). It told the guest's corruption apart: the global guard had become another process's (a TLB entry of another address space), while the frame's saved copy was intact. Off unless the variable is set. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012Camiwa7VsPwkhfyscKQ6S --- NativeBridgeMetalFrontend.mm | 76 ++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 0e2865b..64c6ad4 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -39,6 +39,82 @@ extern "C" void sys_dcache_flush(void *start, size_t length); static NmbDevice *gDevice; static id gDeviceObject; + +// DEBUG (the guest's memory corruption, 2026-10-26): with NATIVE_METAL_ABORT_PROBE=1 in its environment, a process that aborts +// (a __stack_chk_fail among them) or faults (SIGSEGV, SIGBUS) first writes to its standard error whether the stack guard changed +// since this library loaded, the words around the guard, the thread's registers with, for each one that points at mapped +// memory, the first words there and how many of the 512 words of its 4 KiB page are zero (a page the kernel dropped reads all +// zero), and the thread's frames (fp, lr and the 12 words below each fp, where a frame keeps its copy of the guard). +extern "C" uintptr_t __stack_chk_guard; +static uintptr_t gGuardAtLoad; +static char *probeHex(char *at, uint64_t value) { + for (int i = 15; i >= 0; --i) *at++ = "0123456789abcdef"[(value >> (i * 4)) & 0xf]; + return at; +} +static char *probeText(char *at, const char *text) {while (*text) *at++ = *text++; return at;} +extern "C" kern_return_t mach_vm_region(vm_map_read_t, mach_vm_address_t *, mach_vm_size_t *, vm_region_flavor_t, vm_region_info_t, + mach_msg_type_number_t *, mach_port_t *); +// Whether [address, address + 4096) is mapped readable. +static bool probeReadable(uint64_t address) { + mach_vm_address_t start = address & ~4095ull; mach_vm_size_t size = 0; + vm_region_basic_info_data_64_t info; mach_msg_type_number_t count = VM_REGION_BASIC_INFO_COUNT_64; mach_port_t object = MACH_PORT_NULL; + if (mach_vm_region(mach_task_self(), &start, &size, VM_REGION_BASIC_INFO_64, (vm_region_info_t)&info, &count, &object)) return false; + return start <= (address & ~4095ull) && (address & ~4095ull) + 4096 <= start + size && (info.protection & VM_PROT_READ); +} +static void abortProbe(int signal, siginfo_t *si, void *context) { + char line[512], *at = line; + at = probeText(at, "native-metal abort probe: signal "); *at++ = char('0' + signal / 10); *at++ = char('0' + signal % 10); + at = probeText(at, " address "); at = probeHex(at, (uint64_t)(uintptr_t)si->si_addr); *at++ = '\n'; + (void)!write(2, line, (size_t)(at - line)); at = line; + const __darwin_arm_thread_state64 &ss = ((ucontext_t *)context)->uc_mcontext->__ss; + for (int r = 0; r < 33; ++r) { + const uint64_t value = r < 29 ? ss.__x[r] : r == 29 ? ss.__fp : r == 30 ? ss.__lr : r == 31 ? ss.__sp : ss.__pc; + at = line; at = probeText(at, " r"); *at++ = char('0' + r / 10); *at++ = char('0' + r % 10); *at++ = ' '; + at = probeHex(at, value & 0x0000ffffffffffffull); + const uint64_t pointer = value & 0x0000ffffffffffffull; + if (pointer >= 0x100000000ull && pointer < 0x1000000000ull && probeReadable(pointer)) { + const volatile uint64_t *page = (const volatile uint64_t *)(pointer & ~4095ull); + unsigned zero = 0; + for (unsigned w = 0; w < 512; ++w) zero += !page[w]; + at = probeText(at, " zero words in its page "); + *at++ = char('0' + zero / 100); *at++ = char('0' + zero / 10 % 10); *at++ = char('0' + zero % 10); + at = probeText(at, ", at it:"); + const volatile uint64_t *word = (const volatile uint64_t *)(pointer & ~7ull); + for (int i = 0; i < 4 && ((uint64_t)(uintptr_t)(word + i) & 4095) >= ((uint64_t)(uintptr_t)word & 4095); ++i) {*at++ = ' '; at = probeHex(at, word[i]);} + } + *at++ = '\n'; (void)!write(2, line, (size_t)(at - line)); + } + at = line; + at = probeText(at, "native-metal abort probe: stack guard now "); at = probeHex(at, __stack_chk_guard); + at = probeText(at, ", at load "); at = probeHex(at, gGuardAtLoad); at = probeText(at, "; around it:"); + const volatile uintptr_t *guard = &__stack_chk_guard; + for (int i = -4; i <= 4; ++i) {*at++ = ' '; at = probeHex(at, guard[i]);} + *at++ = '\n'; (void)!write(2, line, (size_t)(at - line)); + uint64_t fp = ((ucontext_t *)context)->uc_mcontext->__ss.__fp; + for (int frame = 0; frame < 8 && fp && !(fp & 7); ++frame) { + if (!probeReadable(fp) || !probeReadable(fp - 96)) break; + const volatile uint64_t *f = (const volatile uint64_t *)fp; + at = line; + at = probeText(at, " frame "); *at++ = char('0' + frame); + at = probeText(at, " fp "); at = probeHex(at, fp); + at = probeText(at, " lr "); at = probeHex(at, f[1] & 0x0000ffffffffffffull); + at = probeText(at, " below:"); + for (int i = 1; i <= 12; ++i) {*at++ = ' '; at = probeHex(at, f[-i]);} + *at++ = '\n'; (void)!write(2, line, (size_t)(at - line)); + fp = f[0]; + if (!probeReadable(fp)) break; + } + if (signal != SIGABRT) { // the fault happens again, unhandled: the crash report as before + struct sigaction fallback = {}; fallback.sa_handler = SIG_DFL; sigaction(signal, &fallback, nullptr); + } +} +__attribute__((constructor)) static void installAbortProbe() { + if (!getenv("NATIVE_METAL_ABORT_PROBE")) return; + gGuardAtLoad = __stack_chk_guard; + struct sigaction action = {}; + action.sa_sigaction = abortProbe; action.sa_flags = SA_SIGINFO; + sigaction(SIGABRT, &action, nullptr); sigaction(SIGSEGV, &action, nullptr); sigaction(SIGBUS, &action, nullptr); +} static uint64_t gRegistryID; static NSString *gName; static void (*gMissing)(const char *, const char *); From a3896f27099e496c7decad60dc14d96ef8060593 Mon Sep 17 00:00:00 2001 From: HuJK Date: Mon, 26 Oct 2026 14:10:11 +0800 Subject: [PATCH 39/92] Shader cache: a compiled request's reply is kept in the process's Caches directory and read back instead of compiling again A Metal compile request (source, request 13; stitching, request 14) has a reply that depends only on its bytes and the compiler, so a compiled one is kept at Library/Caches/native-metal-shaders/SHA256.reply (SHA-256 of MTLCompiler's LC_UUID, the request type and its bytes). An app that compiles its shaders from source at every launch compiles each one once: GFXBench's Car Chase took 232 s with the cache off and 133 s with it, the same frame rate (the metallib is the compiler's own bytes). A file holds the magic, the warning and metallib lengths and the SHA-256 of both; it is written to a temporary name and renamed, and one that does not check out is compiled again. Failures are not kept. NATIVE_METAL_NO_SHADER_CACHE=1 turns it off. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012Camiwa7VsPwkhfyscKQ6S --- NativeBridgeMetalFrontend.mm | 74 ++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 64c6ad4..21bef67 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -437,18 +438,89 @@ static void onLargeStack(void (^body)(void)) { pthread_attr_destroy(&attr); if(made)pthread_join(thread,nullptr);else body(); } +// A request's reply depends only on its bytes and the compiler, so a compiled one is kept in the process's Caches directory +// (Library/Caches/native-metal-shaders/SHA256.reply, SHA-256 of MTLCompiler's LC_UUID, the request type and its bytes): an +// app that compiles its shaders from source at every launch (GFXBench spends minutes of Clang and LLVM loading a test) +// compiles each one once. A file is the magic, the warning and metallib lengths and the SHA-256 of both, then both; it is +// written to a temporary name and renamed, and one that does not check out is compiled again. Failures are not kept. +// NATIVE_METAL_NO_SHADER_CACHE=1 turns it off. +struct NBShaderCacheHeader {uint32_t magic,warningBytes;uint64_t metallibBytes;unsigned char digest[CC_SHA256_DIGEST_LENGTH];}; +static const uint32_t kNBShaderCacheMagic=0x3143534e; // "NSC1" +static NSString *shaderCachePath(const unsigned char uuid[16],uint32_t type,const std::vector &request) { + static NSString *directory; + static dispatch_once_t once; + dispatch_once(&once,^{ + if(getenv("NATIVE_METAL_NO_SHADER_CACHE"))return; + NSString *caches=NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES).firstObject; + NSString *path=caches?[caches stringByAppendingPathComponent:@"native-metal-shaders"]:nil; + if(path && [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil])directory=path; + }); + if(!directory)return nil; + CC_SHA256_CTX context;CC_SHA256_Init(&context); + CC_SHA256_Update(&context,uuid,16);CC_SHA256_Update(&context,&type,sizeof type); + CC_SHA256_Update(&context,request.data(),(CC_LONG)request.size()); + unsigned char key[CC_SHA256_DIGEST_LENGTH];CC_SHA256_Final(key,&context); + char name[2*CC_SHA256_DIGEST_LENGTH+8]; + for(int i=0;idigest); + NSString *temporary=[path stringByAppendingFormat:@".%d.%u.tmp",getpid(),pthread_mach_thread_np(pthread_self())]; + if(![file writeToFile:temporary atomically:NO] || rename(temporary.fileSystemRepresentation,path.fileSystemRepresentation)) + unlink(temporary.fileSystemRepresentation); +} static NSData *codeGenRequest(uint32_t type,const std::vector &request,NSString **warnings,NSString **failure) { static void *service; static void (*build)(void *,void *,uint32_t,const void *,size_t,NBCodeGenReply); + static unsigned char compilerUuid[16]; static dispatch_once_t once; static std::mutex lock; + static std::atomic cacheHits,compiled; dispatch_once(&once,^{ void *library=dlopen("/System/Library/PrivateFrameworks/MTLCompiler.framework/MTLCompiler",RTLD_NOW); void *(*create)(const char *)=library?(void *(*)(const char *))dlsym(library,"MTLCodeGenServiceCreate"):nullptr; build=library?(void (*)(void *,void *,uint32_t,const void *,size_t,NBCodeGenReply))dlsym(library,"MTLCodeGenServiceBuildRequest"):nullptr; service=create && build?create("native-metal"):nullptr; + Dl_info info; + if(build && dladdr((const void *)build,&info) && info.dli_fbase) { + const mach_header_64 *header=(const mach_header_64 *)info.dli_fbase; + const uint8_t *command=(const uint8_t *)(header+1); + for(uint32_t i=0;incmds;i++,command+=((const load_command *)command)->cmdsize) + if(((const load_command *)command)->cmd==LC_UUID){memcpy(compilerUuid,((const uuid_command *)command)->uuid,16);break;} + } }); if(!service){*failure=@"the system's Metal compiler (MTLCompiler.framework) is not available";return nil;} + NSString *cached=shaderCachePath(compilerUuid,type,request); + NSData *kept=nil;NSString *keptWarnings=nil; + if(shaderCacheRead(cached,&kept,&keptWarnings)) { + if(++cacheHits==1)NSLog(@"native-metal: shader cache: the first reply read from %@",cached.stringByDeletingLastPathComponent); + *warnings=keptWarnings;*failure=nil; + return kept; + } __block NSData *metallib=nil; __block NSString *warned=nil,*message=nil; __block bool called=false; @@ -468,6 +540,8 @@ static NSData *codeGenRequest(uint32_t type,const std::vector &request, } if(!called)message=@"the Metal compiler did not answer"; *warnings=warned;*failure=message; + if(!message)shaderCacheWrite(cached,metallib,warned); + if(!(++compiled%64))NSLog(@"native-metal: shader cache: %llu compiled, %llu read",(unsigned long long)compiled,(unsigned long long)cacheHits); return message?nil:metallib; } // Metal source, request 13: two little-endian u64 lengths, the source NUL-padded to a multiple of 4 bytes and the arguments From 624b1ce2de078988f76e21067e9ed5c416bd8199 Mon Sep 17 00:00:00 2001 From: HuJK Date: Mon, 26 Oct 2026 15:50:13 +0800 Subject: [PATCH 40/92] PVRTC textures decoded on the CPU into RGBA8, so they sample; NATIVE_METAL_TIMING=1 reports where the CPU's time went PVRTC (MTLPixelFormatPVRTC_*) has no Vulkan format on Adreno, and metal2vulkan keeps such a texture's blocks for copies only: every command that samples one was refused. GFXBench's T-Rex showed a black screen with half its command buffers refused (1,242 of 2,491) while it still scored. A PVRTC texture is now made RGBA8 (sRGB for the sRGB formats) and answers its own pixelFormat; replaceRegion and copies from a buffer (done as encoded) decode its blocks with PVRTC1's scheme: two colours a block, each interpolated between the four block centres around a texel (wrapping), blended by the texel's modulation, blocks in Morton order with the y bit lowest. The 2-bit interpolated modulation mode is approximated. T-Rex now draws its jungle, with no refused command buffer. NATIVE_METAL_TIMING=1 writes the telemetry table to standard error every 20 s, with a new row for the system compiler's requests and the shader cache's answers (T compile). On Car Chase with a warm cache: 658 pipelines in 7.5 s, no compiles. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012Camiwa7VsPwkhfyscKQ6S --- NativeBridgeMetalFrontend.mm | 151 +++++++++++++++++++++++++++++++++-- 1 file changed, 146 insertions(+), 5 deletions(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 21bef67..94a1cf5 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -109,6 +109,23 @@ static void abortProbe(int signal, siginfo_t *si, void *context) { struct sigaction fallback = {}; fallback.sa_handler = SIG_DFL; sigaction(signal, &fallback, nullptr); } } +// NATIVE_METAL_TIMING=1: the telemetry table (where the CPU side's time went: pipelines, libraries, compiles and the shader cache) +// written to standard error every 20 s (an app the system ends at the end of a test never reaches atexit). +extern "C" size_t native_bridge_metal_telemetry(char *out,size_t size); +static void *reportTiming(void *) { + static char table[6144]; + for(;;) { + sleep(20); + const size_t length=native_bridge_metal_telemetry(table,sizeof table); + const char *title="native-metal timing:\n"; + (void)!write(2,title,strlen(title));(void)!write(2,table,length); + } +} +__attribute__((constructor)) static void installTimingReport() { + if(!getenv("NATIVE_METAL_TIMING"))return; + pthread_t thread; + if(!pthread_create(&thread,nullptr,reportTiming,nullptr))pthread_detach(thread); +} __attribute__((constructor)) static void installAbortProbe() { if (!getenv("NATIVE_METAL_ABORT_PROBE")) return; gGuardAtLoad = __stack_chk_guard; @@ -132,6 +149,8 @@ static uint64_t nowUs() {return clock_gettime_nsec_np(CLOCK_UPTIME_RAW)/1000;} struct Timed {std::atomic count{0},total{0},slowest{0}; void add(uint64_t began){const uint64_t took=nowUs()-began;count++;total+=took;uint64_t seen=slowest.load();while(took>seen && !slowest.compare_exchange_weak(seen,took)){}}}; static Timed gTimePipeline,gTimeLibrary,gTimeFunction,gTimeEncodeToCommit; +static Timed gTimeCompile; // the system compiler's requests (codeGenRequest), the shader cache's misses +static std::atomic gCompileCached; // requests the shader cache answered static Timed gTimeWaitScheduled,gTimeWaitCompleted; static Timed gTimeResourceWait; static std::atomic gResourceChecks,gResourceSkipped; @@ -499,7 +518,6 @@ static NSData *codeGenRequest(uint32_t type,const std::vector &request, static unsigned char compilerUuid[16]; static dispatch_once_t once; static std::mutex lock; - static std::atomic cacheHits,compiled; dispatch_once(&once,^{ void *library=dlopen("/System/Library/PrivateFrameworks/MTLCompiler.framework/MTLCompiler",RTLD_NOW); void *(*create)(const char *)=library?(void *(*)(const char *))dlsym(library,"MTLCodeGenServiceCreate"):nullptr; @@ -517,7 +535,7 @@ static NSData *codeGenRequest(uint32_t type,const std::vector &request, NSString *cached=shaderCachePath(compilerUuid,type,request); NSData *kept=nil;NSString *keptWarnings=nil; if(shaderCacheRead(cached,&kept,&keptWarnings)) { - if(++cacheHits==1)NSLog(@"native-metal: shader cache: the first reply read from %@",cached.stringByDeletingLastPathComponent); + gCompileCached++; *warnings=keptWarnings;*failure=nil; return kept; } @@ -526,6 +544,7 @@ static NSData *codeGenRequest(uint32_t type,const std::vector &request, __block bool called=false; { std::lock_guard guard(lock); + const uint64_t compileBegan=nowUs(); onLargeStack(^{build(service,nullptr,type,request.data(),request.size(),^(int32_t status,const void *reply,size_t length,const char *why) { called=true; uint32_t header=0,warning=0; @@ -537,11 +556,11 @@ static NSData *codeGenRequest(uint32_t type,const std::vector &request, if(warning)warned=[[NSString alloc] initWithBytes:(const uint8_t *)reply+header length:warning encoding:NSUTF8StringEncoding]; metallib=[NSData dataWithBytes:(const uint8_t *)reply+header+warning length:length-header-warning]; });}); + gTimeCompile.add(compileBegan); } if(!called)message=@"the Metal compiler did not answer"; *warnings=warned;*failure=message; if(!message)shaderCacheWrite(cached,metallib,warned); - if(!(++compiled%64))NSLog(@"native-metal: shader cache: %llu compiled, %llu read",(unsigned long long)compiled,(unsigned long long)cacheHits); return message?nil:metallib; } // Metal source, request 13: two little-endian u64 lengths, the source NUL-padded to a multiple of 4 bytes and the arguments @@ -641,6 +660,8 @@ static void waitForResource(id resource); @end @interface NBTexture : NBObject @property(nonatomic) NmbTexture *handle; +@property(nonatomic) uint32_t pvrtc; // the PVRTC format the texture was asked for, decoded into its RGBA8 (0: none) +- (void)replacePvrtcRegion:(MTLRegion)r mipmapLevel:(NSUInteger)level slice:(NSUInteger)slice withBytes:(const void *)bytes length:(size_t)length; @property(nonatomic) NmbTextureDescriptor descriptor; @property(nonatomic,strong) NBBuffer *buffer; @property(nonatomic) IOSurfaceRef surface; @@ -710,16 +731,106 @@ static NBBuffer *surfaceBuffer(IOSurfaceRef surface,void *base,size_t bytes) { } return buffer; } +// PVRTC (PowerVR's compressed formats, MTLPixelFormatPVRTC_* 160-167) has no Vulkan format on Adreno, and metal2vulkan keeps such a +// texture's blocks for copies only: every command that samples it is refused (GFXBench's T-Rex drew nothing, half its command +// buffers refused). So a PVRTC texture is made RGBA8 (sRGB for the sRGB formats), still answers its own pixelFormat, and its blocks +// are decoded on the CPU as they are written: replaceRegion, and a copy from a buffer, which is done as the copy is encoded. The +// decoder is PVRTC1's: a block (8 bytes, 4 x 4 texels at 4 bits, 8 x 4 at 2) is a modulation word and a colour word with two +// colours, A (bits 15-1) and B (bits 31-16), each opaque (RGB 5:5:4 or 5:5:5) or translucent (ARGB 3:4:4:3 or 3:4:4:4); every +// texel blends the A and B images, each interpolated bilinearly between the four block centres around it (wrapping at the edges), +// by its modulation value. A level's blocks lie in Morton order, the y bit lowest. The 2-bit format's interpolated modulation +// mode is approximated (the texels it stores, their neighbours' mean between). +static bool isPvrtc(uint64_t format) {return format>=160 && format<=167;} +static uint64_t pvrtcCarrier(uint64_t format) {return (format&1)?71 /* RGBA8Unorm_sRGB */:70 /* RGBA8Unorm */;} +struct PvrtcColor {float r,g,b,a;}; +static int pvrtc45(int v) {return (v<<1)|(v>>3);} +static int pvrtc35(int v) {return (v<<2)|(v>>1);} +static PvrtcColor pvrtcColor(uint32_t word,bool b) { + const uint32_t w=b?word>>16:word&0xffff; + int r,g,bl,a; + if(w&0x8000) { + r=(w>>10)&31;g=(w>>5)&31;bl=b?(int)(w&31):pvrtc45((w>>1)&15);a=15; + } else { + r=pvrtc45((w>>8)&15);g=pvrtc45((w>>4)&15);bl=b?pvrtc45(w&15):pvrtc35((w>>1)&7);a=((w>>12)&7)<<1; + } + return {r/31.0f,g/31.0f,bl/31.0f,a/15.0f}; +} +static uint32_t pvrtcMorton(uint32_t x,uint32_t y,uint32_t across,uint32_t down) { + const uint32_t smaller=std::min(across,down); + uint32_t index=0,shift=0; + for(uint32_t bit=1;bitdown)index|=(x/smaller)<across)index|=(y/smaller)< colorA(across*down),colorB(across*down); + std::vector modulation(across*down),mode(across*down); + for(uint32_t y=0;y=0?px/bw:-((bw-1-px)/bw),y0=py>=0?py/bh:-((bh-1-py)/bh); + const float fx=(float)(px-x0*bw)/bw,fy=(float)(py-y0*bh)/bh; + auto at=[&](int bx,int by){return (size_t)(((by%(int)down)+(int)down)%(int)down)*across+(size_t)(((bx%(int)across)+(int)across)%(int)across);}; + const size_t p=at(x0,y0),q=at(x0+1,y0),r=at(x0,y0+1),t=at(x0+1,y0+1); + auto blend=[&](const std::vector &c){ + PvrtcColor o; + o.r=(c[p].r*(1-fx)+c[q].r*fx)*(1-fy)+(c[r].r*(1-fx)+c[t].r*fx)*fy; + o.g=(c[p].g*(1-fx)+c[q].g*fx)*(1-fy)+(c[r].g*(1-fx)+c[t].g*fx)*fy; + o.b=(c[p].b*(1-fx)+c[q].b*fx)*(1-fy)+(c[r].b*(1-fx)+c[t].b*fx)*fy; + o.a=(c[p].a*(1-fx)+c[q].a*fx)*(1-fy)+(c[r].a*(1-fx)+c[t].a*fx)*fy; + return o;}; + const PvrtcColor a=blend(colorA),b=blend(colorB); + // The texel's own block: its modulation. + const size_t own=(size_t)(y/bh)%down*across+(size_t)(x/bw)%across; + const uint32_t m=modulation[own],tx=x%bw,ty=y%bh; + int weight=0;bool punch=false; + if(!twoBit) { + const int v=(int)((m>>(2*(ty*4+tx)))&3);weight=fourBit[mode[own]][v];punch=mode[own] && v==2; + } else if(!mode[own]) { + weight=((m>>(ty*8+tx))&1)?8:0; + } else { + auto stored=[&](uint32_t sx){return fourBit[0][(m>>(2*((ty*8+sx)/2)))&3];}; + weight=((tx+ty)&1)?(stored((tx+7)%8)+stored((tx+1)%8))/2:stored(tx); + } + const float w=weight/8.0f; + uint8_t *o=out+((size_t)y*width+x)*4; + o[0]=(uint8_t)lrintf((a.r+(b.r-a.r)*w)*255.0f);o[1]=(uint8_t)lrintf((a.g+(b.g-a.g)*w)*255.0f); + o[2]=(uint8_t)lrintf((a.b+(b.b-a.b)*w)*255.0f); + o[3]=!alpha?255:punch?0:(uint8_t)lrintf((a.a+(b.a-a.a)*w)*255.0f); + } +} +static size_t pvrtcLevelBytes(uint32_t width,uint32_t height,bool twoBit) { + uint32_t across=1,down=1; + while(across*(twoBit?8u:4u)format)) { + static std::atomic told; + if(!told.exchange(true)){const char *note="native-metal: PVRTC textures are made RGBA8 and decoded on the CPU\n";(void)!write(2,note,strlen(note));} + out->format=pvrtcCarrier(out->format);out->usage|=MTLTextureUsageShaderRead; + } return true; } @implementation NBTexture - (void)dealloc {nmb_texture_release(_handle);if(_surface)CFRelease(_surface);} - (MTLTextureType)textureType {return (MTLTextureType)_descriptor.type;} -- (MTLPixelFormat)pixelFormat {return (MTLPixelFormat)_descriptor.format;} +- (MTLPixelFormat)pixelFormat {return (MTLPixelFormat)(_pvrtc?_pvrtc:_descriptor.format);} - (NSUInteger)width {return _descriptor.width;} - (NSUInteger)height {return _descriptor.height;} - (NSUInteger)depth {return _descriptor.depth;} @@ -753,10 +864,12 @@ NB_RESOURCE_SPI return (id)view; } - (id)newTextureViewWithPixelFormat:(MTLPixelFormat)format { + if(isPvrtc(format))format=(MTLPixelFormat)pvrtcCarrier(format); return [self view:nmb_texture_new_view(_handle,format,0,nullptr,nullptr,true)]; } - (id)newTextureViewWithPixelFormat:(MTLPixelFormat)format textureType:(MTLTextureType)type levels:(NSRange)levels slices:(NSRange)slices { const uint64_t l[2]={levels.location,levels.length},s[2]={slices.location,slices.length}; + if(isPvrtc(format))format=(MTLPixelFormat)pvrtcCarrier(format); return [self view:nmb_texture_new_view(_handle,format,type,l,s,false)]; } - (void)replaceRegion:(MTLRegion)r mipmapLevel:(NSUInteger)level slice:(NSUInteger)slice withBytes:(const void *)bytes bytesPerRow:(NSUInteger)row bytesPerImage:(NSUInteger)image { @@ -768,8 +881,23 @@ NB_RESOURCE_SPI (unsigned long)r.size.width,(unsigned long)r.size.height,(unsigned long)level,(unsigned long)slice,(unsigned long)row, (unsigned long long)_descriptor.width,(unsigned long long)_descriptor.height,(unsigned long long)_descriptor.format, (unsigned long long)_descriptor.options,_surface?IOSurfaceGetID(_surface):0u); + if(_pvrtc) { // PVRTC blocks: decoded into the RGBA8 texture (the region taken as a whole level of its size) + [self replacePvrtcRegion:r mipmapLevel:level slice:slice withBytes:bytes length:pvrtcLevelBytes((uint32_t)r.size.width,(uint32_t)r.size.height, + _pvrtc==160 || _pvrtc==161 || _pvrtc==164 || _pvrtc==165)]; + return; + } if(!nmb_texture_replace(_handle,region,level,slice,bytes,row,image,length))NBLOG(@"native-metal: replaceRegion refused: %@",lastError()); } +- (void)replacePvrtcRegion:(MTLRegion)r mipmapLevel:(NSUInteger)level slice:(NSUInteger)slice withBytes:(const void *)bytes length:(size_t)length { + const bool twoBit=_pvrtc==160 || _pvrtc==161 || _pvrtc==164 || _pvrtc==165,alpha=_pvrtc>=164; + const uint32_t width=(uint32_t)r.size.width,height=(uint32_t)r.size.height; + if(!bytes || !width || !height)return; + std::vector rgba((size_t)width*height*4); + pvrtcDecode((const uint8_t *)bytes,length,width,height,twoBit,alpha,rgba.data()); + const uint64_t region[6]={r.origin.x,r.origin.y,r.origin.z,width,height,1}; + if(!nmb_texture_replace(_handle,region,level,slice,rgba.data(),(uint64_t)width*4,(uint64_t)width*4*height,rgba.size())) + NBLOG(@"native-metal: PVRTC replaceRegion refused: %@",lastError()); +} - (void)replaceRegion:(MTLRegion)r mipmapLevel:(NSUInteger)level withBytes:(const void *)bytes bytesPerRow:(NSUInteger)row { [self replaceRegion:r mipmapLevel:level slice:0 withBytes:bytes bytesPerRow:row bytesPerImage:row*r.size.height]; } @@ -1539,6 +1667,14 @@ static void waitForResource(id resource) { (unsigned long)image,(unsigned long)o,(unsigned long)[s length],[(NBBuffer *)s wrapped]?" no-copy":"", (unsigned long long)t.width,(unsigned long long)t.height,(unsigned long long)t.format,(unsigned long long)t.options); } + if([(NBTexture *)d pvrtc]) { // PVRTC blocks: decoded from the buffer's contents now, as the copy is encoded + const uint8_t *contents=(const uint8_t *)[s contents]; + if(!contents){[self.commandBuffer refuse:@"a PVRTC copy from a buffer with no CPU contents"];return;} + const uint32_t f=[(NBTexture *)d pvrtc];const bool twoBit=f==160 || f==161 || f==164 || f==165; + const size_t want=pvrtcLevelBytes((uint32_t)z.width,(uint32_t)z.height,twoBit),have=[s length]>o?[s length]-o:0; + [(NBTexture *)d replacePvrtcRegion:MTLRegionMake2D(dO.x,dO.y,z.width,z.height) mipmapLevel:level slice:slice withBytes:contents+o length:std::min(want,have)]; + return; + } const uint64_t size[3]={z.width,z.height,z.depth},b[3]={dO.x,dO.y,dO.z}; [self check:nmb_blit_buffer_to_texture(self.handle,[(NBBuffer *)s handle],o,row,image,size,[(NBTexture *)d handle],slice,level,b) what:"copyFromBuffer:toTexture:"]; } @@ -2063,6 +2199,7 @@ static bool renderDescriptor(MTLRenderPipelineDescriptor *d,NmbRenderPipelineDes NmbTexture *handle=nmb_heap_new_texture(_handle,&descriptor,offset); if(!handle){NBLOG(@"native-metal: heap texture refused: %@",lastError());return nil;} NBTexture *texture=[NBTexture new];texture.handle=handle;nmb_texture_describe(handle,&descriptor);texture.descriptor=descriptor; + if(isPvrtc(d.pixelFormat))texture.pvrtc=(uint32_t)d.pixelFormat; texture.parentHeap=(id)self;texture.heapOffset=offset==UINT64_MAX?0:offset;return (id)texture; } - (id)newTextureWithDescriptor:(MTLTextureDescriptor *)d {return [self textureWithDescriptor:d offset:UINT64_MAX];} @@ -2275,7 +2412,9 @@ static bool pipelineArchives(NSArray *list,std::vector &arch (unsigned long long)descriptor.format,(unsigned long long)descriptor.type,(unsigned long long)descriptor.levels, (unsigned long long)descriptor.usage,(unsigned long long)descriptor.options,handle?"made":"REFUSED"); if(!handle){NBLOG(@"native-metal: texture refused: %@",lastError());return nil;} - NBTexture *texture=[NBTexture new];texture.handle=handle;nmb_texture_describe(handle,&descriptor);texture.descriptor=descriptor;return (id)texture; + NBTexture *texture=[NBTexture new];texture.handle=handle;nmb_texture_describe(handle,&descriptor);texture.descriptor=descriptor; + if(isPvrtc(d.pixelFormat))texture.pvrtc=(uint32_t)d.pixelFormat; + return (id)texture; } // The surface's pages are the texels: its mapping in this process is imported without a copy (pinned by the kernel, mapped into the GPU's // 4 KiB context by Turnip), so every process that imports the surface renders and samples the same memory. @@ -2709,6 +2848,8 @@ extern "C" size_t native_bridge_metal_telemetry(char *out,size_t size) { (unsigned long long)gDispatches.load(),(unsigned long long)gBlits.load()); row("T pipeline n=%llu total=%llums max=%llums",(unsigned long long)gTimePipeline.count.load(),ms(gTimePipeline.total.load()),ms(gTimePipeline.slowest.load())); row("T library n=%llu total=%llums max=%llums",(unsigned long long)gTimeLibrary.count.load(),ms(gTimeLibrary.total.load()),ms(gTimeLibrary.slowest.load())); + row("T compile n=%llu total=%llums max=%llums cached=%llu",(unsigned long long)gTimeCompile.count.load(),ms(gTimeCompile.total.load()), + ms(gTimeCompile.slowest.load()),(unsigned long long)gCompileCached.load()); row("T function n=%llu total=%llums max=%llums", (unsigned long long)gTimeFunction.count.load(),ms(gTimeFunction.total.load()),ms(gTimeFunction.slowest.load())); row("G commit-to-complete n=%llu total=%llums max=%llums mean=%lluus",(unsigned long long)gTimeEncodeToCommit.count.load(),ms(gTimeEncodeToCommit.total.load()), From 56948c84b752f6d3054c440b68d7e0e433cfd0a3 Mon Sep 17 00:00:00 2001 From: HuJK Date: Mon, 26 Oct 2026 16:47:47 +0800 Subject: [PATCH 41/92] NATIVE_METAL_DUMP_TESS=1 writes an app's tessellation sources and pipelines' tessellation state to standard error Every Metal source with a patch function, and every tessellation pipeline's patch type and control point count, partitioning, factor format and step function, winding, control-point index type and vertex inputs. It showed that GFXBench Car Chase's Bezier car body (a [[patch(quad, 0)]] function) never reached the tessellation path: its patch type came back as none. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012Camiwa7VsPwkhfyscKQ6S --- NativeBridgeMetalFrontend.mm | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 94a1cf5..047a0a1 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -565,7 +565,13 @@ static NSData *codeGenRequest(uint32_t type,const std::vector &request, } // Metal source, request 13: two little-endian u64 lengths, the source NUL-padded to a multiple of 4 bytes and the arguments // NUL-terminated. +// DEBUG: NATIVE_METAL_DUMP_TESS=1 writes to standard error every Metal source with a patch function, and every tessellation +// pipeline's tessellation state and vertex inputs (what an app's tessellation asks of the bridge). +static const bool gDumpTessellation=getenv("NATIVE_METAL_DUMP_TESS")!=nullptr; +static void dumpText(NSString *text) {NSData *bytes=[text dataUsingEncoding:NSUTF8StringEncoding];(void)!write(2,bytes.bytes,bytes.length);} static NSData *compileMetalSource(NSString *source,NSString *arguments,NSString **warnings,NSString **failure) { + if(gDumpTessellation && [source containsString:@"[[patch"]) + dumpText([NSString stringWithFormat:@"\n=== native-metal tessellation source (options %@) ===\n%@\n=== end ===\n",arguments,source]); NSData *text=[source dataUsingEncoding:NSUTF8StringEncoding]; const char *options=arguments.UTF8String; const uint64_t sourceBytes=(text.length+1+3)&~uint64_t(3),optionBytes=strlen(options)+1; @@ -2156,6 +2162,21 @@ static bool renderDescriptor(MTLRenderPipelineDescriptor *d,NmbRenderPipelineDes p->tessellation={1,d.maxTessellationFactor,(uint64_t)d.tessellationPartitionMode,(uint64_t)d.tessellationFactorFormat, (uint64_t)d.tessellationFactorStepFunction,(uint64_t)d.tessellationOutputWindingOrder,(uint64_t)d.tessellationControlPointIndexType, (uint64_t)d.tessellationFactorScaleEnabled}; + if(gDumpTessellation && p->tessellation.tessellates) { + NSMutableString *line=[NSMutableString stringWithFormat:@"=== native-metal tessellation pipeline %@: vertex %@ fragment %@ patch type %llu control points %llu " + @"max factor %lu partition %lu factor format %lu step %lu winding %lu index type %lu scale %d\n",d.label,d.vertexFunction.name,d.fragmentFunction.name, + (unsigned long long)nmb_function_patch_type(p->vertex),(unsigned long long)nmb_function_patch_control_point_count(p->vertex), + (unsigned long)d.maxTessellationFactor,(unsigned long)d.tessellationPartitionMode,(unsigned long)d.tessellationFactorFormat, + (unsigned long)d.tessellationFactorStepFunction,(unsigned long)d.tessellationOutputWindingOrder,(unsigned long)d.tessellationControlPointIndexType, + d.tessellationFactorScaleEnabled]; + if(MTLVertexDescriptor *v=d.vertexDescriptor)for(unsigned i=0;i<31;i++) { + MTLVertexAttributeDescriptor *a=v.attributes[i];if(a.format==MTLVertexFormatInvalid)continue; + MTLVertexBufferLayoutDescriptor *l=v.layouts[a.bufferIndex]; + [line appendFormat:@" attribute %u format %lu offset %lu buffer %lu stride %lu step %lu rate %lu\n",i,(unsigned long)a.format,(unsigned long)a.offset, + (unsigned long)a.bufferIndex,(unsigned long)l.stride,(unsigned long)l.stepFunction,(unsigned long)l.stepRate]; + } + dumpText(line); + } return true; } From 29d1cdbb152920c0d1796e5d36704b9132bc62d7 Mon Sep 17 00:00:00 2001 From: HuJK Date: Mon, 26 Oct 2026 19:47:28 +0800 Subject: [PATCH 42/92] Pipeline reflection: a pipeline's creation answers its `reflection:` (GFXBench's Aztec Ruins draws instead of a black screen) newRenderPipelineStateWithDescriptor:options:reflection:error: and the compute forms set the reflection to nil. GFXBench's NGL engine (Aztec Ruins) finds every uniform from it: a struct buffer's members give each uniform's offset in the bytes it sets with setVertexBytes/setFragmentBytes, and the arguments give each texture's and buffer's index. With no reflection it bound nothing, every draw read zeros, the depth stayed at the clear value and the frame was black (at 8.7 fps). With it, Aztec Ruins draws its temple, shadows, depth of field and bloom (Normal 13.5 fps, High 8.6, at the 1.71 GHz cap; 20 fps at 2.11 GHz). The bridge's nmb_render_pipeline_reflection and nmb_compute_pipeline_reflection give metal2vulkan's RenderPipelineDescriptor / ComputePipelineDescriptor reflection as JSON (Metal's raw values; a buffer's struct, array, pointer and texture reference types nested as Metal nests them). The frontend answers it with subclasses of Metal's MTLArgument (which also answers MTLBinding's getters), MTLStructType, MTLStructMember, MTLArrayType, MTLPointerType, MTLTextureReferenceType and the pipeline reflections, so a caller's class checks pass, where the options ask for argument or buffer type information. The completion-handler forms pass it too. Diagnostics that found it: - NATIVE_METAL_WARNINGS=1: metal2vulkan's messages (degradations, precision notes) on standard error, once each. - NATIVE_METAL_DUMP_CB=FIRST[:COUNT]: the app's command buffers FIRST.. have every render pass's attachments and every texture a kernel could write read back after them: per channel minimum, maximum and mean, NaNs, infinities, black share and a 32x8 brightness grid. Before that, each pass's attachments with their load and store actions and clear values, and the first 6 draws of each pass with the pipeline (functions, formats, vertex layout), depth state, viewport and every bound buffer's and bytes' first words. Aztec's draws had only their vertex buffer bound, no uniforms at all. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012Camiwa7VsPwkhfyscKQ6S --- NativeBridgeMetalFrontend.mm | 516 ++++++++++++++++++++++++++++++++++- bridge/NativeMetalObjects.h | 7 + bridge/NativeMetalObjects.rs | 200 ++++++++++++++ 3 files changed, 714 insertions(+), 9 deletions(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 047a0a1..7d8e41d 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -12,6 +12,7 @@ #import #import #include +#include #include #include #include @@ -1161,6 +1162,7 @@ static NSData *functionBitcode(NBFunction *function) { @end @interface NBRenderPipeline : NBObject @property(nonatomic) NmbRenderPipeline *handle; +@property(nonatomic,copy) NSString *dumpInfo; // NATIVE_METAL_DUMP_CB: its functions, formats and vertex layout @end @implementation NBRenderPipeline - (void)dealloc {nmb_render_pipeline_release(_handle);} @@ -1197,6 +1199,7 @@ static NSData *functionBitcode(NBFunction *function) { @end @interface NBDepthStencil : NBObject @property(nonatomic) NmbDepthStencil *handle; +@property(nonatomic,copy) NSString *dumpInfo; // NATIVE_METAL_DUMP_CB: its depth test and write @end @implementation NBDepthStencil - (void)dealloc {nmb_depth_stencil_release(_handle);} @@ -1343,12 +1346,68 @@ static void dumpSurfacesOnce(NSSet *textures,NSSet *owners) { @property(nonatomic,strong) dispatch_group_t handlers; @property(nonatomic,strong) dispatch_group_t schedulingHandlers; @property(nonatomic) BOOL schedulingDelivered; +// NATIVE_METAL_DUMP_CB: the textures this command buffer wrote, met while it was encoded (dumpTargets), the render pass's attachments +// waiting for their encoder (dumpPending), how many passes and kernels it encoded so far, and whether it is the plugin's own readback. +@property(nonatomic,strong) NSMutableArray *dumpTargets,*dumpPending; +@property(nonatomic) uint32_t dumpPasses,dumpKernels; +@property(nonatomic) BOOL dumpReadback; +@property(nonatomic,strong) NSMutableArray *dumpLog; // its passes and first draws, as encoded (NBEncoder dumpDraw:) - (void)refuse:(NSString *)what; @end @interface NBEncoder : NBObject @property(nonatomic) NmbEncoder *handle; @property(nonatomic,strong) NBCommandBuffer *commandBuffer; +@property(nonatomic,strong) NSMutableArray *dumpEntries; // its NATIVE_METAL_DUMP_CB targets, given its label at endEncoding +// NATIVE_METAL_DUMP_CB: what is bound (stage << 8 | index: an [buffer, offset] pair or the bytes), the pipeline and depth/stencil state +// set, and how many draws so far. +@property(nonatomic,strong) NSMutableDictionary *dumpBound; +@property(nonatomic,strong) id dumpPipeline,dumpDepth; +@property(nonatomic) uint32_t dumpDraws; @end +// NATIVE_METAL_DUMP_CB=FIRST[:COUNT]: the app's command buffers FIRST..FIRST+COUNT-1 (counted from 1, the plugin's own readbacks left +// out) have what they rendered read back after them, by a command buffer of the plugin's own on the same queue: every render pass's +// attachments and every texture a kernel could write, in the order first met. For each, standard error gets its channels' minimum, +// maximum and mean, its NaNs and infinities, and a 32x8 grid of brightness ('.' exactly black, 0-9, '+' above 1, '!' a NaN), from 64 +// rows spread down it: where a frame goes black or wrong, the first texture that does names the pass. +static std::atomic gAppCommits; +static std::pair dumpRange() { + static const std::pair range=[]{ + unsigned first=0,count=0;const char *v=getenv("NATIVE_METAL_DUMP_CB"); + if(v && sscanf(v,"%u:%u",&first,&count)>=1)return std::make_pair((uint32_t)first,(uint32_t)(count?count:1)); + return std::make_pair(0u,0u); + }(); + return range; +} +// Whether a command buffer being encoded now may be one to dump: its number is not known until it commits, and others may commit first. +static bool dumpMaybe(NBCommandBuffer *commandBuffer) { + const auto range=dumpRange(); + if(!range.first || commandBuffer.dumpReadback)return false; + const uint64_t next=gAppCommits.load()+1; + return next+16>=range.first && next<(uint64_t)range.first+range.second; +} +// A line of the command buffer's NATIVE_METAL_DUMP_CB log (its passes, first draws and their state), printed before its readback. +static void dumpLine(NBCommandBuffer *commandBuffer,NSString *line) { + if(!dumpMaybe(commandBuffer))return; + if(!commandBuffer.dumpLog)commandBuffer.dumpLog=[NSMutableArray array]; + if(commandBuffer.dumpLog.count<600)[commandBuffer.dumpLog addObject:line]; +} +// The first 16 words of bound data as floats, and how many of its first 256 words are zero or NaN. +static NSString *dumpWords(const uint8_t *p,size_t n) { + NSMutableString *s=[NSMutableString string]; + for(size_t i=0;i(n/4,16);i++){float f;memcpy(&f,p+4*i,4);[s appendFormat:@" %.4g",f];} + size_t zero=0,nan=0;const size_t all=std::min(n/4,256); + for(size_t i=0;i6)return; + dumpLine(_commandBuffer,[NSString stringWithFormat:@"pass %u draw %u: %@ | %@ | depth %@",_commandBuffer.dumpPasses,_dumpDraws,what, + [_dumpPipeline dumpInfo]?:@"no pipeline",_dumpDepth?[_dumpDepth dumpInfo]:@"none"]); + for(NSNumber *key in [_dumpBound.allKeys sortedArrayUsingSelector:@selector(compare:)]) { + const uint32_t k=key.unsignedIntValue;id value=_dumpBound[key]; + NSString *where=[NSString stringWithFormat:@" %s %u",(k>>8)==1?"vertex":(k>>8)==2?"fragment":"kernel",k&0xff]; + if([value isKindOfClass:[NSData class]]) { + NSData *data=value; + dumpLine(_commandBuffer,[NSString stringWithFormat:@"%@ bytes %lu:%@",where,(unsigned long)data.length,dumpWords((const uint8_t *)data.bytes,data.length)]); + } else if([value isKindOfClass:[NSArray class]]) { + id buffer=value[0];const NSUInteger offset=[value[1] unsignedIntegerValue];const uint8_t *contents=(const uint8_t *)buffer.contents; + dumpLine(_commandBuffer,[NSString stringWithFormat:@"%@ buffer %lu+%lu%@:%@",where,(unsigned long)buffer.length,(unsigned long)offset, + buffer.label.length?[@" " stringByAppendingString:buffer.label]:@"", + contents && offset)buffer offset:(NSUInteger)offset index:(NSUInteger)index stage:(uint32_t)stage { if(buffer && ![(id)buffer isKindOfClass:[NBBuffer class]]){[_commandBuffer refuse:@"foreign buffer"];return;} if(buffer)[_commandBuffer.resourceOwners addObject:buffer]; + [self dumpBind:buffer?@[buffer,@(offset)]:nil index:index stage:stage]; [self check:nmb_encoder_set_buffer(_handle,stage,buffer?[(NBBuffer *)buffer handle]:nullptr,offset,(uint32_t)index) what:"setBuffer"]; } - (void)texture:(id)texture index:(NSUInteger)index stage:(uint32_t)stage { if(texture && ![(id)texture isKindOfClass:[NBTexture class]]){[_commandBuffer refuse:@"foreign texture"];return;} + if(texture && stage==0 && (texture.usage&MTLTextureUsageShaderWrite) && dumpMaybe(_commandBuffer)) { + if(!_dumpEntries)_dumpEntries=[NSMutableArray array]; + if(!_commandBuffer.dumpTargets)_commandBuffer.dumpTargets=[NSMutableArray array]; + const NSUInteger before=_commandBuffer.dumpTargets.count; + dumpNote(_commandBuffer,_commandBuffer.dumpTargets,texture,0,0, + [NSString stringWithFormat:@"kernel %u texture %lu",_commandBuffer.dumpKernels,(unsigned long)index]); + if(_commandBuffer.dumpTargets.count>before)[_dumpEntries addObject:_commandBuffer.dumpTargets.lastObject]; + } if(texture)[_commandBuffer.resourceOwners addObject:texture]; if(texture && [(NBTexture *)texture surface])[_commandBuffer.surfaces addObject:texture]; if(texture && gWatchSources)[_commandBuffer watchTexture:(NBTexture *)texture]; @@ -1401,6 +1496,7 @@ static void waitForResource(id resource) { [self sampler:(id)clamped index:index stage:stage]; } - (void)bytes:(const void *)bytes length:(NSUInteger)length index:(NSUInteger)index stage:(uint32_t)stage { + [self dumpBind:[NSData dataWithBytes:bytes length:length] index:index stage:stage]; [self check:nmb_encoder_set_bytes(_handle,stage,bytes,length,(uint32_t)index) what:"setBytes"]; } - (void)fence:(id)fence stages:(uint64_t)stages update:(bool)update { @@ -1411,6 +1507,8 @@ static void waitForResource(id resource) { - (void)updateFence:(id)fence {[self fence:fence stages:0 update:true];} - (void)waitForFence:(id)fence {[self fence:fence stages:0 update:false];} - (void)offset:(NSUInteger)offset index:(NSUInteger)index stage:(uint32_t)stage { + id bound=_dumpBound[@(stage<<8|(uint32_t)index)]; + if([bound isKindOfClass:[NSArray class]])[self dumpBind:@[bound[0],@(offset)] index:index stage:stage]; [self check:nmb_encoder_set_buffer_offset(_handle,stage,offset,(uint32_t)index) what:"setBufferOffset"]; } @end @@ -1467,6 +1565,7 @@ static void waitForResource(id resource) { @implementation NBRenderEncoder - (void)setRenderPipelineState:(id)state { if(![(id)state isKindOfClass:[NBRenderPipeline class]]){[self.commandBuffer refuse:@"foreign render pipeline"];return;} + if(dumpRange().first)self.dumpPipeline=state; [self check:nmb_render_set_pipeline(self.handle,[(NBRenderPipeline *)state handle]) what:"setRenderPipelineState"]; } - (void)setVertexBuffer:(id)b offset:(NSUInteger)o atIndex:(NSUInteger)i {[self buffer:b offset:o index:i stage:1];} @@ -1504,19 +1603,26 @@ static void waitForResource(id resource) { } - (void)setDepthStencilState:(id)state { if(state && ![(id)state isKindOfClass:[NBDepthStencil class]]){[self.commandBuffer refuse:@"foreign depth/stencil state"];return;} + if(dumpRange().first)self.dumpDepth=state; [self check:nmb_render_set_depth_stencil(self.handle,state?[(NBDepthStencil *)state handle]:nullptr) what:"setDepthStencilState"]; } - (void)setViewport:(MTLViewport)v { const double values[6]={v.originX,v.originY,v.width,v.height,v.znear,v.zfar}; + if(self.dumpDraws<6)dumpLine(self.commandBuffer,[NSString stringWithFormat:@"pass %u viewport %g,%g %gx%g depth %g..%g",self.commandBuffer.dumpPasses, + v.originX,v.originY,v.width,v.height,v.znear,v.zfar]); [self check:nmb_render_set_viewport(self.handle,values) what:"setViewport"]; } - (void)setScissorRect:(MTLScissorRect)r { const uint64_t values[4]={r.x,r.y,r.width,r.height}; + if(self.dumpDraws<6)dumpLine(self.commandBuffer,[NSString stringWithFormat:@"pass %u scissor %lu,%lu %lux%lu",self.commandBuffer.dumpPasses, + (unsigned long)r.x,(unsigned long)r.y,(unsigned long)r.width,(unsigned long)r.height]); [self check:nmb_render_set_scissor(self.handle,values) what:"setScissorRect"]; } // The runtime takes cull mode, winding and fill mode together; this encoder keeps Metal's defaults until each is set. - (void)raster { NSNumber *cull=objc_getAssociatedObject(self,"cull"),*winding=objc_getAssociatedObject(self,"winding"),*fill=objc_getAssociatedObject(self,"fill"); + if(self.dumpDraws<6)dumpLine(self.commandBuffer,[NSString stringWithFormat:@"pass %u cull %llu winding %llu fill %llu",self.commandBuffer.dumpPasses, + cull.unsignedLongLongValue,winding.unsignedLongLongValue,fill.unsignedLongLongValue]); [self check:nmb_render_set_raster(self.handle,cull.unsignedLongLongValue,winding.unsignedLongLongValue,fill.unsignedLongLongValue) what:"raster state"]; } - (void)setCullMode:(MTLCullMode)m {objc_setAssociatedObject(self,"cull",@(m),OBJC_ASSOCIATION_RETAIN);[self raster];} @@ -1549,6 +1655,8 @@ static void waitForResource(id resource) { } - (void)drawPrimitives:(MTLPrimitiveType)t vertexStart:(NSUInteger)s vertexCount:(NSUInteger)n instanceCount:(NSUInteger)i baseInstance:(NSUInteger)b { gDraws++;self.commandBuffer->encodedWork.draws++; + [self dumpDraw:[NSString stringWithFormat:@"prim %lu start %lu count %lu instances %lu base %lu",(unsigned long)t,(unsigned long)s,(unsigned long)n, + (unsigned long)i,(unsigned long)b]]; [self check:nmb_render_draw(self.handle,t,(uint32_t)s,(uint32_t)n,(uint32_t)i,(uint32_t)b) what:"drawPrimitives"]; } - (void)drawPrimitives:(MTLPrimitiveType)t vertexStart:(NSUInteger)s vertexCount:(NSUInteger)n instanceCount:(NSUInteger)i { @@ -1562,6 +1670,16 @@ static void waitForResource(id resource) { if(![(id)b isKindOfClass:[NBBuffer class]]){[self.commandBuffer refuse:@"foreign index buffer"];return;} [self.commandBuffer.resourceOwners addObject:b]; gDraws++;self.commandBuffer->encodedWork.draws++; + if(dumpMaybe(self.commandBuffer) && self.dumpDraws<6) { + const uint8_t *indices=(const uint8_t *)b.contents;const unsigned size=k==MTLIndexTypeUInt16?2:4; + NSMutableString *first=[NSMutableString string]; + for(NSUInteger j=0;indices && j<4 && o+(j+1)*size<=b.length;j++) { + uint32_t index=0;memcpy(&index,indices+o+j*size,size);[first appendFormat:@" %u",index]; + } + [self dumpDraw:[NSString stringWithFormat:@"indexed prim %lu count %lu type %lu offset %lu of %lu (first%@) instances %lu baseVertex %ld base %lu", + (unsigned long)t,(unsigned long)n,(unsigned long)k,(unsigned long)o,(unsigned long)b.length,first.length?first:@" unseen", + (unsigned long)i,(long)v,(unsigned long)bi]]; + } else [self dumpDraw:@"indexed"]; [self check:nmb_render_draw_indexed(self.handle,t,(uint32_t)n,k,[(NBBuffer *)b handle],o,(uint32_t)i,(int32_t)v,(uint32_t)bi) what:"drawIndexedPrimitives"]; } - (void)drawIndexedPrimitives:(MTLPrimitiveType)t indexCount:(NSUInteger)n indexType:(MTLIndexType)k indexBuffer:(id)b indexBufferOffset:(NSUInteger)o @@ -1589,6 +1707,8 @@ static void waitForResource(id resource) { points:(id)ci offset:(NSUInteger)co instances:(NSUInteger)instances base:(NSUInteger)base indirect:(id)ib offset:(NSUInteger)io { if(![self own:pi what:@"patch index buffer"] || ![self own:ci what:@"control point index buffer"] || ![self own:ib what:@"indirect buffer"])return; gDraws++;self.commandBuffer->encodedWork.draws++; + [self dumpDraw:[NSString stringWithFormat:@"patches %lu start %lu count %lu instances %lu base %lu%@",(unsigned long)points,(unsigned long)start, + (unsigned long)count,(unsigned long)instances,(unsigned long)base,ib?@" indirect":@""]]; [self check:nmb_render_draw_patches(self.handle,(uint32_t)points,(uint32_t)start,(uint32_t)count,pi?[(NBBuffer *)pi handle]:nullptr,po, ci?[(NBBuffer *)ci handle]:nullptr,co,(uint32_t)instances,(uint32_t)base,ib?[(NBBuffer *)ib handle]:nullptr,io) what:"drawPatches"]; } @@ -1709,6 +1829,136 @@ static bool onRenderServerThread() { return !strncmp(name,"com.apple.coreanimation",23); } extern "C" void NBWaitInFlight(uint64_t beforeUs); +// ---- NATIVE_METAL_DUMP_CB ---- +static float dumpHalf(const uint8_t *p) {__fp16 h;memcpy(&h,p,2);return (float)h;} +// A packed unsigned float of `mantissa` mantissa bits and 5 exponent bits (RG11B10Float's channels). +static float dumpSmallFloat(uint32_t bits,int mantissa) { + const uint32_t m=bits&((1u<>mantissa; + if(e==31)return m?NAN:INFINITY; + if(!e)return ldexpf((float)m,-14-mantissa); + return ldexpf(1.0f+(float)m/(float)(1u<>10)&1023)/1023.f;c[bgr?0:2]=((v>>20)&1023)/1023.f;c[3]=(v>>30)/3.f;return 4; + } + case MTLPixelFormatRG11B10Float: + memcpy(&v,p,4);*channels=3;c[0]=dumpSmallFloat(v&0x7ff,6);c[1]=dumpSmallFloat((v>>11)&0x7ff,6);c[2]=dumpSmallFloat(v>>22,5);return 4; + case MTLPixelFormatRGB9E5Float: { + memcpy(&v,p,4);*channels=3;const int e=(int)(v>>27)-24; + c[0]=ldexpf((float)(v&511),e);c[1]=ldexpf((float)((v>>9)&511),e);c[2]=ldexpf((float)((v>>18)&511),e);return 4; + } + default: return 0; + } +} +static void dumpPrint(uint64_t appIndex,NSDictionary *read) { + NSDictionary *t=read[@"target"];NBTexture *texture=t[@"texture"];const NmbTextureDescriptor d=texture.descriptor; + NSString *encoder=t[@"encoder"]; + char head[400]; + snprintf(head,sizeof head,"native-metal dump cb %llu: %s%s%s%s texture %s %llux%llu f%llu L%lu S%lu",(unsigned long long)appIndex, + [t[@"where"] UTF8String],encoder?" (":"",encoder?encoder.UTF8String:"",encoder?")":"",texture.label.length?texture.label.UTF8String:"-", + (unsigned long long)d.width,(unsigned long long)d.height,(unsigned long long)d.format, + (unsigned long)[t[@"level"] unsignedIntegerValue],(unsigned long)[t[@"slice"] unsignedIntegerValue]); + if(read[@"skip"]){fprintf(stderr,"%s: %s\n",head,[read[@"skip"] UTF8String]);return;} + const unsigned w=[read[@"w"] unsignedIntValue],rows=[read[@"rows"] unsignedIntValue],bpp=[read[@"bpp"] unsignedIntValue]; + const uint32_t format=[read[@"format"] unsignedIntValue]; + const uint8_t *bytes=(const uint8_t *)[(id)read[@"buffer"] contents]; + if(!bytes){fprintf(stderr,"%s: no readback contents\n",head);return;} + double sum[4]={},lo[4]={INFINITY,INFINITY,INFINITY,INFINITY},hi[4]={-INFINITY,-INFINITY,-INFINITY,-INFINITY}; + uint64_t finite[4]={},nans=0,infs=0,black=0,texels=0;unsigned channels=1; + double cell[8][32]={};uint32_t cellCount[8][32]={},cellNan[8][32]={},cellLit[8][32]={}; + for(unsigned r=0;r1?'+':mean<0?'-':(char)('0'+std::min(9,(int)(mean*10))); + } + fprintf(stderr,"native-metal dump cb %llu: |%s|\n",(unsigned long long)appIndex,grid); + } +} +// The readback of `source`'s targets, by a command buffer of the plugin's own committed after it on its queue. +static void dumpReadback(NBCommandBuffer *source,uint64_t appIndex) { + NSArray *targets=[source.dumpTargets copy]; + fprintf(stderr,"native-metal dump cb %llu: %lu targets, %u passes, %u kernels\n",(unsigned long long)appIndex, + (unsigned long)targets.count,source.dumpPasses,source.dumpKernels); + for(NSString *line in source.dumpLog)fprintf(stderr,"native-metal dump cb %llu: %s\n",(unsigned long long)appIndex,line.UTF8String); + if(!targets.count)return; + NBCommandBuffer *readback=(NBCommandBuffer *)[(id)source.queue commandBuffer]; + if(!readback)return; + readback.dumpReadback=YES; + id blit=[(id)readback blitCommandEncoder]; + NSMutableArray *reads=[NSMutableArray array]; + for(NSDictionary *t in targets) { + NBTexture *texture=t[@"texture"];const NmbTextureDescriptor d=texture.descriptor; + const NSUInteger level=[t[@"level"] unsignedIntegerValue],slice=[t[@"slice"] unsignedIntegerValue]; + float c[4];unsigned channels=0;const uint8_t zero[16]={}; + const unsigned bpp=texture.pvrtc?0:dumpTexel((uint32_t)d.format,zero,c,&channels); + NSString *skip=!bpp?@"format not read back":d.samples>1?@"multisampled":d.type==MTLTextureType3D?@"3D": + texture.storageMode==MTLStorageModeMemoryless?@"memoryless":nil; + if(skip){[reads addObject:@{@"target":t,@"skip":skip}];continue;} + const NSUInteger w=std::max(1,(NSUInteger)d.width>>level),h=std::max(1,(NSUInteger)d.height>>level); + const NSUInteger rows=std::min(h,64),rowBytes=w*bpp; + id buffer=[gDeviceObject newBufferWithLength:rowBytes*rows options:MTLResourceStorageModeShared]; + if(!buffer){[reads addObject:@{@"target":t,@"skip":@"no readback buffer"}];continue;} + for(NSUInteger r=0;r)texture sourceSlice:slice sourceLevel:level sourceOrigin:MTLOriginMake(0,(2*r+1)*h/(2*rows),0) + sourceSize:MTLSizeMake(w,1,1) toBuffer:buffer destinationOffset:r*rowBytes destinationBytesPerRow:rowBytes + destinationBytesPerImage:rowBytes]; + [reads addObject:@{@"target":t,@"buffer":buffer,@"w":@(w),@"rows":@(rows),@"bpp":@(bpp),@"format":@((uint32_t)d.format)}]; + } + [blit endEncoding]; + [(id)readback addCompletedHandler:^(id done) { + if(done.error){fprintf(stderr,"native-metal dump cb %llu: readback failed: %s\n",(unsigned long long)appIndex, + done.error.localizedDescription.UTF8String);return;} + for(NSDictionary *read in reads)dumpPrint(appIndex,read); + }]; + [(id)readback commit]; +} + @implementation NBCommandBuffer // iPadOS 27: QuartzCore names the tasks a command buffer works for (GPU time attribution), on every buffer; there is no such accounting here. - (void)setResponsibleTaskIDs:(const void *)ids count:(NSUInteger)count {(void)ids;(void)count;} @@ -1773,6 +2023,11 @@ extern "C" void NBWaitInFlight(uint64_t beforeUs); * encoder's IMPs with methodForSelector:, and on nil it called address 0 (i568: WidgetRenderer_Default crashed eight launches running, then * hit its open cap and lost the device). */ if(!handle){[self refuse:[NSString stringWithFormat:@"%@: %@",what,lastError()]];encoder.commandBuffer=self;return encoder;} + if([encoder isKindOfClass:[NBComputeEncoder class]])_dumpKernels++; + if(_dumpPending.count) { + if(!_dumpTargets)_dumpTargets=[NSMutableArray array]; + [_dumpTargets addObjectsFromArray:_dumpPending];encoder.dumpEntries=_dumpPending;_dumpPending=nil; + } encoder.handle=handle;encoder.commandBuffer=self;return encoder; } - (id)computeCommandEncoder { @@ -1788,6 +2043,34 @@ extern "C" void NBWaitInFlight(uint64_t beforeUs); } - (id)renderCommandEncoderWithDescriptor:(MTLRenderPassDescriptor *)d { NmbRenderPassDescriptor pass={}; + _dumpPasses++; + if(dumpMaybe(self)) { + if(!_dumpTargets)_dumpTargets=[NSMutableArray array]; + _dumpPending=[NSMutableArray array]; + for(unsigned i=0;i<8;i++) { + MTLRenderPassColorAttachmentDescriptor *c=d.colorAttachments[i]; + if(c.resolveTexture)dumpNote(self,_dumpPending,c.resolveTexture,c.resolveLevel,c.resolveSlice, + [NSString stringWithFormat:@"pass %u resolve %u",_dumpPasses,i]); + else dumpNote(self,_dumpPending,c.texture,c.level,c.slice,[NSString stringWithFormat:@"pass %u color %u",_dumpPasses,i]); + } + dumpNote(self,_dumpPending,d.depthAttachment.texture,d.depthAttachment.level,d.depthAttachment.slice, + [NSString stringWithFormat:@"pass %u depth",_dumpPasses]); + // Load 0 don't care, 1 load, 2 clear; store 0 don't care, 1 store, 2 resolve, 3 store and resolve. + NSMutableString *line=[NSMutableString stringWithFormat:@"pass %u %lux%lu",_dumpPasses,(unsigned long)d.renderTargetWidth,(unsigned long)d.renderTargetHeight]; + auto describe=[&](NSString *name,MTLRenderPassAttachmentDescriptor *a,NSString *clear) { + if(!a.texture)return; + [line appendFormat:@"; %@ f%lu %lux%lu%@ load %lu store %lu%@%@",name,(unsigned long)a.texture.pixelFormat,(unsigned long)a.texture.width, + (unsigned long)a.texture.height,a.texture.storageMode==MTLStorageModeMemoryless?@" memoryless":@"",(unsigned long)a.loadAction, + (unsigned long)a.storeAction,a.loadAction==MTLLoadActionClear?clear:@"",a.resolveTexture?@" resolve":@""]; + }; + for(unsigned i=0;i<8;i++) { + MTLClearColor c=d.colorAttachments[i].clearColor; + describe([NSString stringWithFormat:@"c%u",i],d.colorAttachments[i],[NSString stringWithFormat:@" clear %g,%g,%g,%g",c.red,c.green,c.blue,c.alpha]); + } + describe(@"depth",d.depthAttachment,[NSString stringWithFormat:@" clear %g",d.depthAttachment.clearDepth]); + describe(@"stencil",d.stencilAttachment,[NSString stringWithFormat:@" clear %u",d.stencilAttachment.clearStencil]); + dumpLine(self,line); + } { id first=d.colorAttachments[0].texture; if(first) { @@ -1919,8 +2202,13 @@ extern "C" void NBWaitInFlight(uint64_t beforeUs); for(WatchedSource &source:watched){source.hash=hashSource(source.bytes,source.length);gSourcesWatched[source.surface]++;} // Completion owns this reference; the runtime orders the non-owning scheduled callback before it. void *context=(__bridge_retained void *)self; + const uint64_t appIndex=self.dumpReadback?0:gAppCommits.fetch_add(1)+1; if(nmb_command_buffer_on_completed(_handle,completed,context) && - nmb_command_buffer_on_scheduled(_handle,scheduled,context) && nmb_command_buffer_commit(_handle)) {[self fireEarly];return;} + nmb_command_buffer_on_scheduled(_handle,scheduled,context) && nmb_command_buffer_commit(_handle)) { + const auto range=dumpRange(); + if(appIndex && range.first && appIndex>=range.first && appIndex<(uint64_t)range.first+range.second)dumpReadback(self,appIndex); + [self fireEarly];return; + } (void)CFBridgingRelease(context); // refused registration/commit cannot call the registered handlers [self refuse:[@"commit: " stringByAppendingString:lastError()]]; } @@ -2179,6 +2467,199 @@ static bool renderDescriptor(MTLRenderPipelineDescriptor *d,NmbRenderPipelineDes } return true; } +// NATIVE_METAL_DUMP_CB: a render pipeline's functions, color formats (b blends, m its write mask), depth format and vertex layout +// (attribute=format@offset/buffer, layout=stride/step/rate), for its draws' lines. +static NSString *renderDumpInfo(MTLRenderPipelineDescriptor *d) { + NSMutableString *s=[NSMutableString stringWithFormat:@"%@ v=%@ f=%@",d.label?:@"-",d.vertexFunction.name?:@"-",d.fragmentFunction.name?:@"-"]; + for(unsigned i=0;i<8;i++) { + MTLRenderPipelineColorAttachmentDescriptor *a=d.colorAttachments[i];if(a.pixelFormat==MTLPixelFormatInvalid)continue; + [s appendFormat:@" c%u=f%lu%@",i,(unsigned long)a.pixelFormat,a.blendingEnabled?@"b":@""]; + if(a.writeMask!=MTLColorWriteMaskAll)[s appendFormat:@"m%lu",(unsigned long)a.writeMask]; + } + if(d.depthAttachmentPixelFormat)[s appendFormat:@" d=f%lu",(unsigned long)d.depthAttachmentPixelFormat]; + if(d.stencilAttachmentPixelFormat)[s appendFormat:@" s=f%lu",(unsigned long)d.stencilAttachmentPixelFormat]; + if(d.rasterSampleCount>1)[s appendFormat:@" samples %lu",(unsigned long)d.rasterSampleCount]; + if(!d.rasterizationEnabled)[s appendString:@" no rasterization"]; + if(MTLVertexDescriptor *v=d.vertexDescriptor)for(unsigned i=0;i<31;i++) { + MTLVertexAttributeDescriptor *a=v.attributes[i];if(a.format==MTLVertexFormatInvalid)continue; + MTLVertexBufferLayoutDescriptor *l=v.layouts[a.bufferIndex]; + [s appendFormat:@" a%u=%lu@%lu/b%lu=%lu/%lu/%lu",i,(unsigned long)a.format,(unsigned long)a.offset,(unsigned long)a.bufferIndex, + (unsigned long)l.stride,(unsigned long)l.stepFunction,(unsigned long)l.stepRate]; + } + return s; +} + +// ---- pipeline reflection ---- +// The `reflection:` of a pipeline's creation, from the runtime's JSON (nmb_*_pipeline_reflection): subclasses of Metal's own reflection +// classes (MTLArgument, which also answers MTLBinding's selectors, MTLStructType, MTLStructMember, MTLArrayType, MTLPointerType, +// MTLTextureReferenceType and the pipeline reflections), so a caller's class checks pass and a getter not answered here is the base +// class's. GFXBench's NGL engine (Aztec Ruins) reads every uniform's offset and every texture's index from it: without it, it binds +// nothing and every draw reads zeros. +@class NBStructType,NBArrayType,NBPointerType,NBTextureReferenceType; +static NSUInteger reflectionValue(NSDictionary *json,NSString *key) {return [json[key] unsignedIntegerValue];} +static NBStructType *reflectionStruct(NSDictionary *json); +static NBArrayType *reflectionArray(NSDictionary *json); +static NBPointerType *reflectionPointer(NSDictionary *json); +static NBTextureReferenceType *reflectionTexture(NSDictionary *json); +@interface NBTextureReferenceType : MTLTextureReferenceType +@property(nonatomic,strong) NSDictionary *json; +@end +@implementation NBTextureReferenceType +- (MTLDataType)dataType {return MTLDataTypeTexture;} +- (MTLDataType)textureDataType {return (MTLDataType)reflectionValue(_json,@"dataType");} +- (MTLTextureType)textureType {return (MTLTextureType)reflectionValue(_json,@"textureType");} +- (MTLBindingAccess)access {return (MTLBindingAccess)reflectionValue(_json,@"access");} +- (BOOL)isDepthTexture {return [_json[@"depth"] boolValue];} +@end +@interface NBPointerType : MTLPointerType +@property(nonatomic,strong) NSDictionary *json; +@end +@implementation NBPointerType +- (MTLDataType)dataType {return MTLDataTypePointer;} +- (MTLDataType)elementType {return (MTLDataType)reflectionValue(_json,@"elementType");} +- (MTLBindingAccess)access {return (MTLBindingAccess)reflectionValue(_json,@"access");} +- (NSUInteger)alignment {return reflectionValue(_json,@"alignment");} +- (NSUInteger)dataSize {return reflectionValue(_json,@"size");} +- (BOOL)elementIsArgumentBuffer {return [_json[@"argumentBuffer"] boolValue];} +- (MTLStructType *)elementStructType {return (MTLStructType *)reflectionStruct(_json[@"struct"]);} +- (MTLArrayType *)elementArrayType {return (MTLArrayType *)reflectionArray(_json[@"array"]);} +@end +@interface NBArrayType : MTLArrayType +@property(nonatomic,strong) NSDictionary *json; +@end +@implementation NBArrayType +- (MTLDataType)dataType {return MTLDataTypeArray;} +- (MTLDataType)elementType {return (MTLDataType)reflectionValue(_json,@"elementType");} +- (NSUInteger)arrayLength {return reflectionValue(_json,@"length");} +- (NSUInteger)stride {return reflectionValue(_json,@"stride");} +- (NSUInteger)argumentIndexStride {return reflectionValue(_json,@"argumentIndexStride");} +- (MTLStructType *)elementStructType {return (MTLStructType *)reflectionStruct(_json[@"struct"]);} +- (MTLArrayType *)elementArrayType {return (MTLArrayType *)reflectionArray(_json[@"array"]);} +- (MTLPointerType *)elementPointerType {return (MTLPointerType *)reflectionPointer(_json[@"pointer"]);} +- (MTLTextureReferenceType *)elementTextureReferenceType {return (MTLTextureReferenceType *)reflectionTexture(_json[@"texture"]);} +@end +@interface NBStructMember : MTLStructMember +@property(nonatomic,strong) NSDictionary *json; +@end +@implementation NBStructMember +- (NSString *)name {return _json[@"name"];} +- (NSUInteger)offset {return reflectionValue(_json,@"offset");} +- (MTLDataType)dataType {return (MTLDataType)reflectionValue(_json,@"dataType");} +- (NSUInteger)argumentIndex {return reflectionValue(_json,@"argumentIndex");} +- (MTLStructType *)structType {return (MTLStructType *)reflectionStruct(_json[@"struct"]);} +- (MTLArrayType *)arrayType {return (MTLArrayType *)reflectionArray(_json[@"array"]);} +- (MTLPointerType *)pointerType {return (MTLPointerType *)reflectionPointer(_json[@"pointer"]);} +- (MTLTextureReferenceType *)textureReferenceType {return (MTLTextureReferenceType *)reflectionTexture(_json[@"texture"]);} +@end +@interface NBStructType : MTLStructType +@property(nonatomic,strong) NSArray *memberList; +@end +@implementation NBStructType +- (MTLDataType)dataType {return MTLDataTypeStruct;} +- (NSArray *)members {return _memberList;} +- (MTLStructMember *)memberByName:(NSString *)name { + for(NBStructMember *member in _memberList)if([member.name isEqualToString:name])return member; + return nil; +} +@end +static NBStructType *reflectionStruct(NSDictionary *json) { + if(![json isKindOfClass:[NSDictionary class]])return nil; + NSMutableArray *members=[NSMutableArray array]; + for(NSDictionary *m in json[@"members"]){NBStructMember *member=[NBStructMember new];member.json=m;[members addObject:member];} + NBStructType *type=[NBStructType new];type.memberList=members;return type; +} +static NBArrayType *reflectionArray(NSDictionary *json) { + if(![json isKindOfClass:[NSDictionary class]])return nil; + NBArrayType *type=[NBArrayType new];type.json=json;return type; +} +static NBPointerType *reflectionPointer(NSDictionary *json) { + if(![json isKindOfClass:[NSDictionary class]])return nil; + NBPointerType *type=[NBPointerType new];type.json=json;return type; +} +static NBTextureReferenceType *reflectionTexture(NSDictionary *json) { + if(![json isKindOfClass:[NSDictionary class]])return nil; + NBTextureReferenceType *type=[NBTextureReferenceType new];type.json=json;return type; +} +// One argument: MTLArgument's getters, and MTLBinding's (with MTLBufferBinding's, MTLTextureBinding's and MTLThreadgroupBinding's), +// whose `type` values are MTLArgumentType's. +@interface NBArgument : MTLArgument +@property(nonatomic,strong) NSDictionary *json; +@property(nonatomic,strong) NBStructType *madeStruct; // made once: callers walk its members by identity +@end +@implementation NBArgument +- (NSString *)name {return _json[@"name"];} +- (MTLArgumentType)type {return (MTLArgumentType)reflectionValue(_json,@"type");} +- (MTLBindingAccess)access {return (MTLBindingAccess)reflectionValue(_json,@"access");} +- (NSUInteger)index {return reflectionValue(_json,@"index");} +- (BOOL)isActive {return [_json[@"used"] boolValue];} +- (BOOL)isUsed {return self.isActive;} +- (BOOL)isArgument {return [_json[@"argument"] boolValue];} +- (NSUInteger)bufferAlignment {return reflectionValue(_json[@"buffer"],@"alignment");} +- (NSUInteger)bufferDataSize {return reflectionValue(_json[@"buffer"],@"size");} +- (MTLDataType)bufferDataType {return (MTLDataType)reflectionValue(_json[@"buffer"],@"dataType");} +- (MTLStructType *)bufferStructType { + if(!_madeStruct)_madeStruct=reflectionStruct(_json[@"buffer"][@"struct"]); + return _madeStruct; +} +- (MTLPointerType *)bufferPointerType {return (MTLPointerType *)reflectionPointer(_json[@"buffer"][@"pointer"]);} +- (NSUInteger)threadgroupMemoryAlignment {return reflectionValue(_json[@"threadgroup"],@"alignment");} +- (NSUInteger)threadgroupMemoryDataSize {return reflectionValue(_json[@"threadgroup"],@"size");} +- (MTLTextureType)textureType {return (MTLTextureType)reflectionValue(_json[@"texture"],@"textureType");} +- (MTLDataType)textureDataType {return (MTLDataType)reflectionValue(_json[@"texture"],@"dataType");} +- (BOOL)isDepthTexture {return [_json[@"texture"][@"depth"] boolValue];} +- (NSUInteger)arrayLength {return _json[@"texture"]?reflectionValue(_json[@"texture"],@"arrayLength"):1;} +- (NSString *)description {return [NSString stringWithFormat:@"",self.name,(unsigned long)self.type,(unsigned long)self.index];} +@end +@interface NBRenderPipelineReflection : MTLRenderPipelineReflection +@property(nonatomic,strong) NSArray *vertexList,*fragmentList; +@end +@implementation NBRenderPipelineReflection +- (NSArray *)vertexArguments {return _vertexList;} +- (NSArray *)fragmentArguments {return _fragmentList;} +- (NSArray *)tileArguments {return @[];} +- (NSArray *)vertexBindings {return _vertexList;} +- (NSArray *)fragmentBindings {return _fragmentList;} +- (NSArray *)tileBindings {return @[];} +- (NSArray *)objectBindings {return @[];} +- (NSArray *)meshBindings {return @[];} +@end +@interface NBComputePipelineReflection : MTLComputePipelineReflection +@property(nonatomic,strong) NSArray *argumentList; +@end +@implementation NBComputePipelineReflection +- (NSArray *)arguments {return _argumentList;} +- (NSArray *)bindings {return _argumentList;} +@end +// The runtime's JSON as objects (nil, with the reason logged, for a refusal). +static NSDictionary *reflectionJSON(NmbReflection *reflection,const char *what) { + if(!reflection){NBLOG(@"native-metal: %s reflection refused: %@",what,lastError());return nil;} + const char *text=nmb_reflection_json(reflection); + NSData *data=text?[NSData dataWithBytes:text length:strlen(text)]:nil; + NSDictionary *json=data?[NSJSONSerialization JSONObjectWithData:data options:0 error:nil]:nil; + nmb_reflection_release(reflection); + if(![json isKindOfClass:[NSDictionary class]]){NBLOG(@"native-metal: %s reflection is not JSON",what);return nil;} + return json; +} +static NSArray *reflectionArguments(NSArray *bindings) { + NSMutableArray *arguments=[NSMutableArray array]; + for(NSDictionary *b in bindings)if([b isKindOfClass:[NSDictionary class]]){NBArgument *argument=[NBArgument new];argument.json=b;[arguments addObject:argument];} + return arguments; +} +// Metal answers a reflection where the options ask for argument or buffer type information. +static bool wantsReflection(MTLPipelineOption options) {return options&(MTLPipelineOptionArgumentInfo|MTLPipelineOptionBufferTypeInfo);} +static id renderReflection(const NmbRenderPipelineDescriptor *p) { + NSDictionary *json=reflectionJSON(nmb_render_pipeline_reflection(p),"render pipeline"); + if(!json)return nil; + NBRenderPipelineReflection *reflection=[NBRenderPipelineReflection new]; + reflection.vertexList=reflectionArguments(json[@"vertex"]);reflection.fragmentList=reflectionArguments(json[@"fragment"]); + return reflection; +} +static id computeReflection(const NmbComputePipelineDescriptor *p) { + NSDictionary *json=reflectionJSON(nmb_compute_pipeline_reflection(p),"compute pipeline"); + if(!json)return nil; + NBComputePipelineReflection *reflection=[NBComputePipelineReflection new];reflection.argumentList=reflectionArguments(json[@"bindings"]); + return reflection; +} // ---- heaps ---- // Metal heaps over the runtime's: an automatic heap places each resource, a placement heap takes an offset. Resources made from one keep it @@ -2563,13 +3044,20 @@ static bool pipelineArchives(NSArray *list,std::vector &arch const NmbPipelineOptions creation={(uint64_t)options,nullptr,0}; const uint64_t pipelineBegan=nowUs(); NmbComputePipeline *handle=nmb_compute_pipeline_new_with_options(gDevice,[(NBFunction *)function handle],&creation); - if(!handle)if(NBFunction *rebuilt=zeroRestFunction((NBFunction *)function))handle=nmb_compute_pipeline_new_with_options(gDevice,rebuilt.handle,&creation); + NBFunction *built=(NBFunction *)function; + if(!handle)if(NBFunction *rebuilt=zeroRestFunction((NBFunction *)function)) { + handle=nmb_compute_pipeline_new_with_options(gDevice,rebuilt.handle,&creation);if(handle)built=rebuilt; + } gTimePipeline.add(pipelineBegan); if(!handle) { NBFunction *f=(NBFunction *)function; noteProblem([NSString stringWithFormat:@"compute %@ k=%s: %@",f.name,f.constantsText.c_str(),lastError()]); return failWith>(error,@"compute pipeline",nil); } + if(reflection && wantsReflection(options)) { + NmbComputePipelineDescriptor p={};p.function=built.handle;p.max_call_stack_depth=1; + *reflection=(MTLComputePipelineReflection *)computeReflection(&p); + } NBComputePipeline *state=[NBComputePipeline new];state.handle=handle;gPipelines++;if(error)*error=nil;return (id)state; } - (id)newComputePipelineStateWithDescriptor:(MTLComputePipelineDescriptor *)d options:(MTLPipelineOption)options @@ -2593,6 +3081,7 @@ static bool pipelineArchives(NSArray *list,std::vector &arch noteProblem([NSString stringWithFormat:@"compute %@ k=%s: %@",f.name,f.constantsText.c_str(),lastError()]); return failWith>(error,@"compute pipeline",nil); } + if(reflection && wantsReflection(options))*reflection=(MTLComputePipelineReflection *)computeReflection(&p); NBComputePipeline *state=[NBComputePipeline new];state.handle=handle;state.label=d.label;gPipelines++; if(error)*error=nil;return (id)state; } @@ -2608,10 +3097,14 @@ static bool pipelineArchives(NSArray *list,std::vector &arch if(!pipelineArchives(d.binaryArchives,archives,error)){delete p;return nil;} const NmbPipelineOptions creation={(uint64_t)options,archives.data(),archives.size()}; const uint64_t pipelineBegan=nowUs();const NBCost costBegan=NBCost::now(); - NmbRenderPipeline *handle=nmb_render_pipeline_new_with_options(gDevice,p,&creation);delete p;gTimePipeline.add(pipelineBegan); + NmbRenderPipeline *handle=nmb_render_pipeline_new_with_options(gDevice,p,&creation);gTimePipeline.add(pipelineBegan); + if(handle && reflection && wantsReflection(options))*reflection=(MTLRenderPipelineReflection *)renderReflection(p); + delete p; journalRenderPipeline(d,pipelineBegan,handle?nullptr:nmb_last_error(),gJournalPath.empty()?std::string():NBCost::now().since(costBegan)); if(!handle)return failWith>(error,@"render pipeline",nil); - NBRenderPipeline *state=[NBRenderPipeline new];state.handle=handle;gPipelines++;if(error)*error=nil;return (id)state; + NBRenderPipeline *state=[NBRenderPipeline new];state.handle=handle;gPipelines++;if(error)*error=nil; + if(dumpRange().first)state.dumpInfo=renderDumpInfo(d); + return (id)state; } // The asynchronous forms make the pipeline on the caller's thread and call the handler before returning, as the library's // newFunctionWithName:constantValues:completionHandler: does (RenderBox's pipeline loader, Core Image's bilateral solver). @@ -2621,8 +3114,9 @@ static bool pipelineArchives(NSArray *list,std::vector &arch } - (void)newRenderPipelineStateWithDescriptor:(MTLRenderPipelineDescriptor *)d options:(MTLPipelineOption)options completionHandler:(MTLNewRenderPipelineStateWithReflectionCompletionHandler)handler { - NSError *error=nil;id state=[(id)self newRenderPipelineStateWithDescriptor:d options:options reflection:nil error:&error]; - handler(state,nil,error); + NSError *error=nil;MTLRenderPipelineReflection *reflection=nil; + id state=[(id)self newRenderPipelineStateWithDescriptor:d options:options reflection:&reflection error:&error]; + handler(state,reflection,error); } - (void)newComputePipelineStateWithFunction:(id)function completionHandler:(MTLNewComputePipelineStateCompletionHandler)handler { NSError *error=nil;id state=[(id)self newComputePipelineStateWithFunction:function error:&error]; @@ -2630,8 +3124,9 @@ static bool pipelineArchives(NSArray *list,std::vector &arch } - (void)newComputePipelineStateWithFunction:(id)function options:(MTLPipelineOption)options completionHandler:(MTLNewComputePipelineStateWithReflectionCompletionHandler)handler { - NSError *error=nil;id state=[(id)self newComputePipelineStateWithFunction:function options:options reflection:nil error:&error]; - handler(state,nil,error); + NSError *error=nil;MTLComputePipelineReflection *reflection=nil; + id state=[(id)self newComputePipelineStateWithFunction:function options:options reflection:&reflection error:&error]; + handler(state,reflection,error); } // SPI: a precompiled pipeline library (RenderBox.framework/default.pipelinelib) holds Apple GPU binaries. None is opened; the caller logs the // error and builds its pipelines from the shader library instead (RB::Device::render_pipeline_state). @@ -2664,7 +3159,10 @@ static bool pipelineArchives(NSArray *list,std::vector &arch NmbDepthStencilDescriptor s={(uint64_t)d.depthCompareFunction,(uint64_t)d.depthWriteEnabled,face(d.frontFaceStencil),face(d.backFaceStencil)}; NmbDepthStencil *handle=nmb_depth_stencil_new(gDevice,&s); if(!handle){NBLOG(@"native-metal: depth/stencil state refused: %@",lastError());return nil;} - NBDepthStencil *state=[NBDepthStencil new];state.handle=handle;return (id)state; + NBDepthStencil *state=[NBDepthStencil new];state.handle=handle; + if(dumpRange().first)state.dumpInfo=[NSString stringWithFormat:@"compare %lu write %d%@",(unsigned long)d.depthCompareFunction,d.depthWriteEnabled, + d.frontFaceStencil.stencilCompareFunction!=MTLCompareFunctionAlways || d.frontFaceStencil.depthStencilPassOperation?@" stencil":@""]; + return (id)state; } @end diff --git a/bridge/NativeMetalObjects.h b/bridge/NativeMetalObjects.h index db47f17..6cf602a 100644 --- a/bridge/NativeMetalObjects.h +++ b/bridge/NativeMetalObjects.h @@ -178,6 +178,13 @@ bool nmb_binary_archive_add_render(NmbBinaryArchive *, const struct NmbRenderPip bool nmb_binary_archive_add_compute(NmbBinaryArchive *, NmbFunction *); bool nmb_binary_archive_add_compute_descriptor(NmbBinaryArchive *, const struct NmbComputePipelineDescriptor *); void nmb_render_pipeline_release(NmbRenderPipeline *); +// A pipeline's reflection, as its creation's `reflection:` answers it, in JSON: {"vertex":[...],"fragment":[...]} for a render pipeline, +// {"bindings":[...]} for a compute pipeline; each binding has Metal's raw values and a buffer's nested struct/array/pointer types. Null: refused. +typedef struct NmbReflection NmbReflection; +NmbReflection *nmb_render_pipeline_reflection(const struct NmbRenderPipelineDescriptor *); +NmbReflection *nmb_compute_pipeline_reflection(const struct NmbComputePipelineDescriptor *); +const char *nmb_reflection_json(const NmbReflection *); +void nmb_reflection_release(NmbReflection *); struct NmbSamplerDescriptor { uint64_t min_filter, mag_filter, mip_filter, s_address, t_address, r_address, border_color, normalized, max_anisotropy, compare, lod_average; diff --git a/bridge/NativeMetalObjects.rs b/bridge/NativeMetalObjects.rs index b77cd6d..73fe6f6 100644 --- a/bridge/NativeMetalObjects.rs +++ b/bridge/NativeMetalObjects.rs @@ -225,6 +225,24 @@ fn note_shadow(text: &str) { } } +// NATIVE_METAL_WARNINGS=1: each kind of message (its id and the text's first 48 characters) the first time it comes, in full and +// with its severity, to standard error, for the first 400 kinds. The telemetry keeps only the last warning, and a degradation that +// draws nothing is otherwise silent. +fn log_message(message: &DebugMessage) { + static ENABLED: std::sync::OnceLock = std::sync::OnceLock::new(); + static SEEN: std::sync::Mutex> = std::sync::Mutex::new(Vec::new()); + if !*ENABLED.get_or_init(|| std::env::var_os("NATIVE_METAL_WARNINGS").is_some()) { + return; + } + let key = format!("{} {}", message.id_name, message.text.chars().take(48).collect::()); + let Ok(mut seen) = SEEN.lock() else { return }; + if seen.len() >= 400 || seen.contains(&key) { + return; + } + seen.push(key); + eprintln!("native-metal message {} {:?}: [{}] {}", seen.len(), message.severity, message.id_name, message.text); +} + fn observe_message(message: &DebugMessage) { use std::sync::atomic::Ordering::Relaxed; if message.id_name == "shadowed-texture" { @@ -242,6 +260,7 @@ fn observe_message(message: &DebugMessage) { } return; } + log_message(message); if message.severity >= MessageSeverity::Warning { OTHER_MESSAGES[1].fetch_add(1, Relaxed); if let Ok(mut last) = LAST_WARNING.lock() { @@ -1561,6 +1580,187 @@ pub extern "C" fn nmb_render_pipeline_release(pipeline: *mut NmbRenderPipeline) release(pipeline) } +// ---- pipeline reflection ---- + +/// A pipeline's reflection, as the `reflection:` out-parameter of a pipeline's creation answers it, in JSON for the frontend's +/// MTLArgument/MTLBinding objects: Metal's raw enumeration values, and a buffer's struct, array, pointer and texture reference types +/// nested as Metal nests them. GFXBench's NGL engine (Aztec Ruins) finds every uniform's buffer offset and every texture's index in it. +pub struct NmbReflection(std::ffi::CString); + +mod reflection_json { + use metal2vulkan::reflection::{ArrayType, Binding, PointerType, StructType, TextureReferenceType}; + use std::fmt::Write; + + fn string(out: &mut String, s: &str) { + out.push('"'); + for c in s.chars() { + match c { + '"' => out.push_str("\\\""), + '\\' => out.push_str("\\\\"), + c if (c as u32) < 0x20 => { + let _ = write!(out, "\\u{:04x}", c as u32); + } + c => out.push(c), + } + } + out.push('"'); + } + + fn structure(out: &mut String, s: &StructType) { + out.push_str("{\"members\":["); + for (i, m) in s.members().iter().enumerate() { + if i > 0 { + out.push(','); + } + out.push_str("{\"name\":"); + string(out, m.name()); + let _ = write!(out, ",\"offset\":{},\"dataType\":{},\"argumentIndex\":{}", m.offset(), m.data_type().as_raw(), m.argument_index()); + if let Some(t) = m.struct_type() { + out.push_str(",\"struct\":"); + structure(out, t); + } + if let Some(t) = m.array_type() { + out.push_str(",\"array\":"); + array(out, t); + } + if let Some(t) = m.pointer_type() { + out.push_str(",\"pointer\":"); + pointer(out, t); + } + if let Some(t) = m.texture_reference_type() { + out.push_str(",\"texture\":"); + texture_reference(out, t); + } + out.push('}'); + } + out.push_str("]}"); + } + + fn array(out: &mut String, a: &ArrayType) { + let _ = write!(out, "{{\"elementType\":{},\"length\":{},\"stride\":{},\"argumentIndexStride\":{}", a.element_type().as_raw(), + a.array_length(), a.stride(), a.argument_index_stride()); + if let Some(t) = a.element_struct_type() { + out.push_str(",\"struct\":"); + structure(out, t); + } + if let Some(t) = a.element_array_type() { + out.push_str(",\"array\":"); + array(out, t); + } + if let Some(t) = a.element_pointer_type() { + out.push_str(",\"pointer\":"); + pointer(out, t); + } + if let Some(t) = a.element_texture_reference_type() { + out.push_str(",\"texture\":"); + texture_reference(out, t); + } + out.push('}'); + } + + fn pointer(out: &mut String, p: &PointerType) { + let _ = write!(out, "{{\"elementType\":{},\"access\":{},\"alignment\":{},\"size\":{},\"argumentBuffer\":{}", p.element_type().as_raw(), + p.access().as_raw(), p.alignment(), p.data_size(), p.element_is_argument_buffer()); + if let Some(t) = p.element_struct_type() { + out.push_str(",\"struct\":"); + structure(out, t); + } + if let Some(t) = p.element_array_type() { + out.push_str(",\"array\":"); + array(out, t); + } + out.push('}'); + } + + fn texture_reference(out: &mut String, t: &TextureReferenceType) { + let _ = write!(out, "{{\"dataType\":{},\"textureType\":{},\"access\":{},\"depth\":{}}}", t.texture_data_type().as_raw(), + t.texture_type().as_raw(), t.access().as_raw(), t.is_depth_texture()); + } + + fn binding(out: &mut String, b: &Binding) { + out.push_str("{\"name\":"); + string(out, b.name()); + let _ = write!(out, ",\"type\":{},\"access\":{},\"index\":{},\"used\":{},\"argument\":{}", b.binding_type().as_raw(), b.access().as_raw(), + b.index(), b.is_used(), b.is_argument()); + if let Some(buffer) = b.buffer() { + let _ = write!(out, ",\"buffer\":{{\"alignment\":{},\"size\":{},\"dataType\":{}", buffer.buffer_alignment(), buffer.buffer_data_size(), + buffer.buffer_data_type().as_raw()); + if let Some(t) = buffer.buffer_struct_type() { + out.push_str(",\"struct\":"); + structure(out, t); + } + if let Some(t) = buffer.buffer_pointer_type() { + out.push_str(",\"pointer\":"); + pointer(out, t); + } + out.push('}'); + } + if let Some(t) = b.threadgroup() { + let _ = write!(out, ",\"threadgroup\":{{\"alignment\":{},\"size\":{}}}", t.threadgroup_memory_alignment(), t.threadgroup_memory_data_size()); + } + if let Some(t) = b.texture() { + let _ = write!(out, ",\"texture\":{{\"textureType\":{},\"dataType\":{},\"depth\":{},\"arrayLength\":{}}}", t.texture_type().as_raw(), + t.texture_data_type().as_raw(), t.is_depth_texture(), t.array_length()); + } + out.push('}'); + } + + /// `"name":[binding,...]`. + pub(super) fn stage(out: &mut String, name: &str, bindings: &[Binding]) { + let _ = write!(out, "\"{name}\":["); + for (i, b) in bindings.iter().enumerate() { + if i > 0 { + out.push(','); + } + binding(out, b); + } + out.push(']'); + } +} + +fn reflection_handle(json: String) -> Result<*mut NmbReflection, String> { + Ok(boxed(NmbReflection(std::ffi::CString::new(json).map_err(|_| "a NUL in the reflection".to_string())?))) +} + +/// The reflection of the render pipeline `descriptor` describes: `{"vertex":[...],"fragment":[...]}`. +#[unsafe(no_mangle)] +pub extern "C" fn nmb_render_pipeline_reflection(descriptor: *const NmbRenderPipelineDescriptor) -> *mut NmbReflection { + guard(std::ptr::null_mut(), || { + let budget = metal2vulkan::Budget::with_deadline(None); + let reflection = render_descriptor(descriptor)?.reflection(&budget).map_err(e("render pipeline reflection"))?; + let mut json = String::from("{"); + reflection_json::stage(&mut json, "vertex", reflection.vertex_bindings()); + json.push(','); + reflection_json::stage(&mut json, "fragment", reflection.fragment_bindings()); + json.push('}'); + reflection_handle(json) + }) +} + +/// The reflection of the compute pipeline `descriptor` describes: `{"bindings":[...]}`. +#[unsafe(no_mangle)] +pub extern "C" fn nmb_compute_pipeline_reflection(descriptor: *const NmbComputePipelineDescriptor) -> *mut NmbReflection { + guard(std::ptr::null_mut(), || { + let budget = metal2vulkan::Budget::with_deadline(None); + let reflection = compute_descriptor(descriptor)?.reflection(&budget).map_err(e("compute pipeline reflection"))?; + let mut json = String::from("{"); + reflection_json::stage(&mut json, "bindings", reflection.bindings()); + json.push('}'); + reflection_handle(json) + }) +} + +/// The reflection's JSON, valid until its release. +#[unsafe(no_mangle)] +pub extern "C" fn nmb_reflection_json(reflection: *const NmbReflection) -> *const c_char { + guard(std::ptr::null(), || Ok(get(reflection)?.0.as_ptr())) +} + +#[unsafe(no_mangle)] +pub extern "C" fn nmb_reflection_release(reflection: *mut NmbReflection) { + release(reflection) +} + // ---- binary archives ---- pub struct NmbBinaryArchive(BinaryArchive); From 30cd15a0caad5a46e10322f260ff9ade161b3ede Mon Sep 17 00:00:00 2001 From: HuJK Date: Mon, 26 Oct 2026 21:51:32 +0800 Subject: [PATCH 43/92] Dump diagnostics: whole images, one target over many frames, a pass's draw runs, draws by range and skipped draws Found with them: Aztec Ruins' dark wall rectangles come from its light volumes (pass 1, draws 275-288: envprobe irradiance boxes and lights, blended into the G-buffer pass through framebuffer fetch); without those draws they are gone. Not fixed yet. - NATIVE_METAL_DUMP_IMAGE=WHERE[,WHERE...]: the NATIVE_METAL_DUMP_CB targets whose place ("pass 7 color 0", "kernel 4 texture 0") holds one of these ("all": every one) are read back whole and written to standard error in base64 lines of 6 KiB, which a host script turns into PNGs. - NATIVE_METAL_DUMP_ONLY=1: only those targets are read back, with no log, so a small texture can be followed over hundreds of frames (Aztec's 48x36 motion-blur neighbor-max tiles over 600 frames). - Each pass's draws as runs of one pipeline and depth state (draws FIRST..LAST), every draw counted: a pass's structure. - NATIVE_METAL_DUMP_DRAWS=FIRST:LAST: which draws of each pass are written in full with their state (default the first 6). - NATIVE_METAL_SKIP_DRAWS=PASS:FIRST:LAST: every command buffer's render pass PASS leaves out its draws FIRST..LAST, to find which draws make what a frame shows. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012Camiwa7VsPwkhfyscKQ6S --- NativeBridgeMetalFrontend.mm | 86 +++++++++++++++++++++++++++++++----- 1 file changed, 75 insertions(+), 11 deletions(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 7d8e41d..6e457bf 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -1363,6 +1363,8 @@ static void dumpSurfacesOnce(NSSet *textures,NSSet *owners) { @property(nonatomic,strong) NSMutableDictionary *dumpBound; @property(nonatomic,strong) id dumpPipeline,dumpDepth; @property(nonatomic) uint32_t dumpDraws; +@property(nonatomic,strong) NSMutableArray *dumpRuns; +@property(nonatomic) uint32_t drawIndex; // this encoder's draws so far, for NATIVE_METAL_SKIP_DRAWS // [pipeline info, depth info, first draw, count] for each run of draws with one state @end // NATIVE_METAL_DUMP_CB=FIRST[:COUNT]: the app's command buffers FIRST..FIRST+COUNT-1 (counted from 1, the plugin's own readbacks left // out) have what they rendered read back after them, by a command buffer of the plugin's own on the same queue: every render pass's @@ -1386,10 +1388,13 @@ static bool dumpMaybe(NBCommandBuffer *commandBuffer) { return next+16>=range.first && next<(uint64_t)range.first+range.second; } // A line of the command buffer's NATIVE_METAL_DUMP_CB log (its passes, first draws and their state), printed before its readback. +// NATIVE_METAL_DUMP_ONLY=1: only the NATIVE_METAL_DUMP_IMAGE targets are read back and written, with no log, so a long range of +// command buffers (a small texture over hundreds of frames) costs little. +static bool dumpOnly() {static const bool only=getenv("NATIVE_METAL_DUMP_ONLY")!=nullptr;return only;} static void dumpLine(NBCommandBuffer *commandBuffer,NSString *line) { - if(!dumpMaybe(commandBuffer))return; + if(dumpOnly() || !dumpMaybe(commandBuffer))return; if(!commandBuffer.dumpLog)commandBuffer.dumpLog=[NSMutableArray array]; - if(commandBuffer.dumpLog.count<600)[commandBuffer.dumpLog addObject:line]; + if(commandBuffer.dumpLog.count<2000)[commandBuffer.dumpLog addObject:line]; } // The first 16 words of bound data as floats, and how many of its first 256 words are zero or NaN. static NSString *dumpWords(const uint8_t *p,size_t n) { @@ -1400,6 +1405,16 @@ static NSString *dumpWords(const uint8_t *p,size_t n) { [s appendFormat:@" (of %zu words %zu zero, %zu NaN)",all,zero,nan]; return s; } +// NATIVE_METAL_DUMP_DRAWS=FIRST:LAST: which draws of each pass (counted from 1) are written in full, with the state set before them +// (default the first 6). +static bool dumpShown(uint32_t draw) { + static const std::pair shown=[]{ + unsigned first=1,last=6;const char *v=getenv("NATIVE_METAL_DUMP_DRAWS"); + if(v && sscanf(v,"%u:%u",&first,&last)!=2){first=1;last=6;} + return std::make_pair(first,last); + }(); + return draw>=shown.first && draw<=shown.second; +} static void dumpNote(NBCommandBuffer *commandBuffer,NSMutableArray *into,id texture,NSUInteger level,NSUInteger slice,NSString *where) { if(!texture || ![texture isKindOfClass:[NBTexture class]])return; for(NSDictionary *seen in commandBuffer.dumpTargets) @@ -1431,6 +1446,9 @@ static void waitForResource(id resource) { - (void)endEncoding { if(self.label.length)for(NSMutableDictionary *entry in _dumpEntries)entry[@"encoder"]=self.label; if(_dumpDraws)dumpLine(_commandBuffer,[NSString stringWithFormat:@"pass %u: %u draws %@",_commandBuffer.dumpPasses,_dumpDraws,self.label?:@""]); + if(_dumpRuns.count>1 || _dumpDraws>6)for(NSArray *run in _dumpRuns) + dumpLine(_commandBuffer,[NSString stringWithFormat:@"pass %u run: draws %@..%lu | %@ | depth %@",_commandBuffer.dumpPasses,run[2], + (unsigned long)([run[2] unsignedIntegerValue]+[run[3] unsignedIntegerValue]-1),run[0],run[1]]); NmbEncoder *handle=_handle;_handle=nullptr; if(!handle){[_commandBuffer refuse:@"endEncoding twice"];return;} [self check:nmb_encoder_end(handle) what:"endEncoding"]; @@ -1446,7 +1464,16 @@ static void waitForResource(id resource) { } // NATIVE_METAL_DUMP_CB: the first 6 draws of each pass, with the pipeline, the depth/stencil state and every bound buffer's first words. - (void)dumpDraw:(NSString *)what { - if(!dumpMaybe(_commandBuffer) || ++_dumpDraws>6)return; + if(!dumpMaybe(_commandBuffer))return; + // Runs of draws with one pipeline and depth state, every draw counted: a pass's structure (geometry, then light volumes, ...). + NSString *pipeline=[_dumpPipeline dumpInfo]?:@"no pipeline",*depth=_dumpDepth?[_dumpDepth dumpInfo]:@"none"; + NSMutableArray *last=_dumpRuns.lastObject; + if(last && [last[0] isEqualToString:pipeline] && [last[1] isEqualToString:depth])last[3]=@([last[3] unsignedIntegerValue]+1); + else if(_dumpRuns.count<200) { + if(!_dumpRuns)_dumpRuns=[NSMutableArray array]; + [_dumpRuns addObject:[NSMutableArray arrayWithObjects:pipeline,depth,@(_dumpDraws+1),@1,nil]]; + } + if(!dumpShown(++_dumpDraws))return; dumpLine(_commandBuffer,[NSString stringWithFormat:@"pass %u draw %u: %@ | %@ | depth %@",_commandBuffer.dumpPasses,_dumpDraws,what, [_dumpPipeline dumpInfo]?:@"no pipeline",_dumpDepth?[_dumpDepth dumpInfo]:@"none"]); for(NSNumber *key in [_dumpBound.allKeys sortedArrayUsingSelector:@selector(compare:)]) { @@ -1608,20 +1635,20 @@ static void waitForResource(id resource) { } - (void)setViewport:(MTLViewport)v { const double values[6]={v.originX,v.originY,v.width,v.height,v.znear,v.zfar}; - if(self.dumpDraws<6)dumpLine(self.commandBuffer,[NSString stringWithFormat:@"pass %u viewport %g,%g %gx%g depth %g..%g",self.commandBuffer.dumpPasses, + if(dumpShown(self.dumpDraws+1))dumpLine(self.commandBuffer,[NSString stringWithFormat:@"pass %u viewport %g,%g %gx%g depth %g..%g",self.commandBuffer.dumpPasses, v.originX,v.originY,v.width,v.height,v.znear,v.zfar]); [self check:nmb_render_set_viewport(self.handle,values) what:"setViewport"]; } - (void)setScissorRect:(MTLScissorRect)r { const uint64_t values[4]={r.x,r.y,r.width,r.height}; - if(self.dumpDraws<6)dumpLine(self.commandBuffer,[NSString stringWithFormat:@"pass %u scissor %lu,%lu %lux%lu",self.commandBuffer.dumpPasses, + if(dumpShown(self.dumpDraws+1))dumpLine(self.commandBuffer,[NSString stringWithFormat:@"pass %u scissor %lu,%lu %lux%lu",self.commandBuffer.dumpPasses, (unsigned long)r.x,(unsigned long)r.y,(unsigned long)r.width,(unsigned long)r.height]); [self check:nmb_render_set_scissor(self.handle,values) what:"setScissorRect"]; } // The runtime takes cull mode, winding and fill mode together; this encoder keeps Metal's defaults until each is set. - (void)raster { NSNumber *cull=objc_getAssociatedObject(self,"cull"),*winding=objc_getAssociatedObject(self,"winding"),*fill=objc_getAssociatedObject(self,"fill"); - if(self.dumpDraws<6)dumpLine(self.commandBuffer,[NSString stringWithFormat:@"pass %u cull %llu winding %llu fill %llu",self.commandBuffer.dumpPasses, + if(dumpShown(self.dumpDraws+1))dumpLine(self.commandBuffer,[NSString stringWithFormat:@"pass %u cull %llu winding %llu fill %llu",self.commandBuffer.dumpPasses, cull.unsignedLongLongValue,winding.unsignedLongLongValue,fill.unsignedLongLongValue]); [self check:nmb_render_set_raster(self.handle,cull.unsignedLongLongValue,winding.unsignedLongLongValue,fill.unsignedLongLongValue) what:"raster state"]; } @@ -1653,7 +1680,20 @@ static void waitForResource(id resource) { - (void)setBlendColorRed:(float)r green:(float)g blue:(float)b alpha:(float)a { const float c[4]={r,g,b,a};[self check:nmb_render_set_blend_color(self.handle,c) what:"setBlendColor"]; } +// NATIVE_METAL_SKIP_DRAWS=PASS:FIRST:LAST: every command buffer's render pass PASS (counted from 1) leaves out its draws FIRST..LAST +// (counted from 1), to find which draws make what a frame shows. +- (BOOL)skipDraw { + struct Skip {unsigned pass,first,last;}; + static const Skip skip=[]{ + Skip s={};const char *v=getenv("NATIVE_METAL_SKIP_DRAWS"); + if(v && sscanf(v,"%u:%u:%u",&s.pass,&s.first,&s.last)!=3)s.pass=0; + return s; + }(); + const uint32_t index=++self.drawIndex; + return skip.pass && self.commandBuffer.dumpPasses==skip.pass && index>=skip.first && index<=skip.last; +} - (void)drawPrimitives:(MTLPrimitiveType)t vertexStart:(NSUInteger)s vertexCount:(NSUInteger)n instanceCount:(NSUInteger)i baseInstance:(NSUInteger)b { + if([self skipDraw])return; gDraws++;self.commandBuffer->encodedWork.draws++; [self dumpDraw:[NSString stringWithFormat:@"prim %lu start %lu count %lu instances %lu base %lu",(unsigned long)t,(unsigned long)s,(unsigned long)n, (unsigned long)i,(unsigned long)b]]; @@ -1669,8 +1709,9 @@ static void waitForResource(id resource) { instanceCount:(NSUInteger)i baseVertex:(NSInteger)v baseInstance:(NSUInteger)bi { if(![(id)b isKindOfClass:[NBBuffer class]]){[self.commandBuffer refuse:@"foreign index buffer"];return;} [self.commandBuffer.resourceOwners addObject:b]; + if([self skipDraw])return; gDraws++;self.commandBuffer->encodedWork.draws++; - if(dumpMaybe(self.commandBuffer) && self.dumpDraws<6) { + if(dumpMaybe(self.commandBuffer) && dumpShown(self.dumpDraws+1)) { const uint8_t *indices=(const uint8_t *)b.contents;const unsigned size=k==MTLIndexTypeUInt16?2:4; NSMutableString *first=[NSMutableString string]; for(NSUInteger j=0;indices && j<4 && o+(j+1)*size<=b.length;j++) { @@ -1706,6 +1747,7 @@ static void waitForResource(id resource) { - (void)patches:(NSUInteger)points start:(NSUInteger)start count:(NSUInteger)count patchIndex:(id)pi offset:(NSUInteger)po points:(id)ci offset:(NSUInteger)co instances:(NSUInteger)instances base:(NSUInteger)base indirect:(id)ib offset:(NSUInteger)io { if(![self own:pi what:@"patch index buffer"] || ![self own:ci what:@"control point index buffer"] || ![self own:ib what:@"indirect buffer"])return; + if([self skipDraw])return; gDraws++;self.commandBuffer->encodedWork.draws++; [self dumpDraw:[NSString stringWithFormat:@"patches %lu start %lu count %lu instances %lu base %lu%@",(unsigned long)points,(unsigned long)start, (unsigned long)count,(unsigned long)instances,(unsigned long)base,ib?@" indirect":@""]]; @@ -1919,12 +1961,33 @@ static void dumpPrint(uint64_t appIndex,NSDictionary *read) { } fprintf(stderr,"native-metal dump cb %llu: |%s|\n",(unsigned long long)appIndex,grid); } + // NATIVE_METAL_DUMP_IMAGE: the whole texture, its rows top down as the format stores them, in base64 lines of 6 KiB. + if([read[@"image"] boolValue]) { + NSString *text=[[NSData dataWithBytesNoCopy:(void *)bytes length:(NSUInteger)rows*w*bpp freeWhenDone:NO] base64EncodedStringWithOptions:0]; + const NSUInteger part=8192,parts=(text.length+part-1)/part; + for(NSUInteger i=0;i *wanted=[]{ + const char *v=getenv("NATIVE_METAL_DUMP_IMAGE"); + return v?[@(v) componentsSeparatedByString:@","]:@[]; + }(); + for(NSString *w in wanted)if(w.length && ([w isEqualToString:@"all"] || [where containsString:w]))return true; + return false; } // The readback of `source`'s targets, by a command buffer of the plugin's own committed after it on its queue. static void dumpReadback(NBCommandBuffer *source,uint64_t appIndex) { NSArray *targets=[source.dumpTargets copy]; - fprintf(stderr,"native-metal dump cb %llu: %lu targets, %u passes, %u kernels\n",(unsigned long long)appIndex, - (unsigned long)targets.count,source.dumpPasses,source.dumpKernels); + if(dumpOnly())targets=[targets filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSDictionary *t,NSDictionary *bindings){(void)bindings; + return dumpImageWanted(t[@"where"]); + }]]; + else fprintf(stderr,"native-metal dump cb %llu: %lu targets, %u passes, %u kernels\n",(unsigned long long)appIndex, + (unsigned long)targets.count,source.dumpPasses,source.dumpKernels); for(NSString *line in source.dumpLog)fprintf(stderr,"native-metal dump cb %llu: %s\n",(unsigned long long)appIndex,line.UTF8String); if(!targets.count)return; NBCommandBuffer *readback=(NBCommandBuffer *)[(id)source.queue commandBuffer]; @@ -1941,14 +2004,15 @@ static void dumpReadback(NBCommandBuffer *source,uint64_t appIndex) { texture.storageMode==MTLStorageModeMemoryless?@"memoryless":nil; if(skip){[reads addObject:@{@"target":t,@"skip":skip}];continue;} const NSUInteger w=std::max(1,(NSUInteger)d.width>>level),h=std::max(1,(NSUInteger)d.height>>level); - const NSUInteger rows=std::min(h,64),rowBytes=w*bpp; + const bool image=dumpImageWanted(t[@"where"]); + const NSUInteger rows=image?h:std::min(h,64),rowBytes=w*bpp; id buffer=[gDeviceObject newBufferWithLength:rowBytes*rows options:MTLResourceStorageModeShared]; if(!buffer){[reads addObject:@{@"target":t,@"skip":@"no readback buffer"}];continue;} for(NSUInteger r=0;r)texture sourceSlice:slice sourceLevel:level sourceOrigin:MTLOriginMake(0,(2*r+1)*h/(2*rows),0) sourceSize:MTLSizeMake(w,1,1) toBuffer:buffer destinationOffset:r*rowBytes destinationBytesPerRow:rowBytes destinationBytesPerImage:rowBytes]; - [reads addObject:@{@"target":t,@"buffer":buffer,@"w":@(w),@"rows":@(rows),@"bpp":@(bpp),@"format":@((uint32_t)d.format)}]; + [reads addObject:@{@"target":t,@"buffer":buffer,@"w":@(w),@"rows":@(rows),@"bpp":@(bpp),@"format":@((uint32_t)d.format),@"image":@(image)}]; } [blit endEncoding]; [(id)readback addCompletedHandler:^(id done) { From 0cbcd0f022a73b5ab30f3e66b1a5feb9a64b051a Mon Sep 17 00:00:00 2001 From: HuJK Date: Tue, 27 Oct 2026 00:18:08 +0800 Subject: [PATCH 44/92] Dump diagnostics: draws picked by pipeline, more words of bound data, bound textures and samplers - NATIVE_METAL_DUMP_DRAWS_MATCH=TEXT: every draw whose pipeline description holds TEXT is written in full, whatever its number (draw numbers move between frames; "c5=f92b" picks Aztec Ruins' light volumes). - NATIVE_METAL_DUMP_WORDS=N: N words of each bound buffer and bytes instead of 16. - A draw's lines now list its bound textures (type, format, size, array length, mipmaps, label) and samplers (filters, address modes, compare function, LOD clamps). The log holds up to 20,000 lines a command buffer. With them, Aztec Ruins' envprobe SH coefficients were checked against a CPU computation from the probe atlas, following m_envprobe_generate_sh_compute: within 0.2% for every probe drawn. The dark wall boxes come from probes whose atlas, and so the direct lightmap behind it, is dark there. Whether that matches Apple's GPU is still open. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012Camiwa7VsPwkhfyscKQ6S --- NativeBridgeMetalFrontend.mm | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 6e457bf..c329e78 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -1394,12 +1394,14 @@ static bool dumpOnly() {static const bool only=getenv("NATIVE_METAL_DUMP_ONLY")! static void dumpLine(NBCommandBuffer *commandBuffer,NSString *line) { if(dumpOnly() || !dumpMaybe(commandBuffer))return; if(!commandBuffer.dumpLog)commandBuffer.dumpLog=[NSMutableArray array]; - if(commandBuffer.dumpLog.count<2000)[commandBuffer.dumpLog addObject:line]; + if(commandBuffer.dumpLog.count<20000)[commandBuffer.dumpLog addObject:line]; } -// The first 16 words of bound data as floats, and how many of its first 256 words are zero or NaN. +// The first 16 words of bound data as floats (NATIVE_METAL_DUMP_WORDS=N: N of them), and how many of its first 256 words are zero +// or NaN. static NSString *dumpWords(const uint8_t *p,size_t n) { + static const size_t words=[]{const char *v=getenv("NATIVE_METAL_DUMP_WORDS");return v && atoi(v)>0?size_t(atoi(v)):size_t(16);}(); NSMutableString *s=[NSMutableString string]; - for(size_t i=0;i(n/4,16);i++){float f;memcpy(&f,p+4*i,4);[s appendFormat:@" %.4g",f];} + for(size_t i=0;i(n/4,words);i++){float f;memcpy(&f,p+4*i,4);[s appendFormat:@" %.4g",f];} size_t zero=0,nan=0;const size_t all=std::min(n/4,256); for(size_t i=0;i=shown.first && draw<=shown.second; } +// NATIVE_METAL_DUMP_DRAWS_MATCH=TEXT: also every draw whose pipeline's description holds TEXT (renderDumpInfo, e.g. "c5=f92b" for +// the pipelines that blend into color 5), whatever its number. +static bool dumpMatched(NSString *pipeline) { + static NSString *const text=[]{const char *v=getenv("NATIVE_METAL_DUMP_DRAWS_MATCH");return v?@(v):nil;}(); + return text.length && [pipeline containsString:text]; +} static void dumpNote(NBCommandBuffer *commandBuffer,NSMutableArray *into,id texture,NSUInteger level,NSUInteger slice,NSString *where) { if(!texture || ![texture isKindOfClass:[NBTexture class]])return; for(NSDictionary *seen in commandBuffer.dumpTargets) @@ -1473,13 +1481,27 @@ static void waitForResource(id resource) { if(!_dumpRuns)_dumpRuns=[NSMutableArray array]; [_dumpRuns addObject:[NSMutableArray arrayWithObjects:pipeline,depth,@(_dumpDraws+1),@1,nil]]; } - if(!dumpShown(++_dumpDraws))return; + if(!dumpShown(++_dumpDraws) && !dumpMatched(pipeline))return; dumpLine(_commandBuffer,[NSString stringWithFormat:@"pass %u draw %u: %@ | %@ | depth %@",_commandBuffer.dumpPasses,_dumpDraws,what, [_dumpPipeline dumpInfo]?:@"no pipeline",_dumpDepth?[_dumpDepth dumpInfo]:@"none"]); for(NSNumber *key in [_dumpBound.allKeys sortedArrayUsingSelector:@selector(compare:)]) { const uint32_t k=key.unsignedIntValue;id value=_dumpBound[key]; - NSString *where=[NSString stringWithFormat:@" %s %u",(k>>8)==1?"vertex":(k>>8)==2?"fragment":"kernel",k&0xff]; - if([value isKindOfClass:[NSData class]]) { + // Key stage << 8 | index: stages 0-2 buffers and bytes, 4-6 textures, 8-10 samplers (compute, vertex, fragment). + const uint32_t stage=(k>>8)&3,kind=(k>>8)>>2; + NSString *where=[NSString stringWithFormat:@" %s %s %u",stage==1?"vertex":stage==2?"fragment":"kernel",kind==1?"texture":kind==2?"sampler":"", + k&0xff]; + if(kind==1 && [value isKindOfClass:[NBTexture class]]) { + NBTexture *t=value;const NmbTextureDescriptor d=t.descriptor; + dumpLine(_commandBuffer,[NSString stringWithFormat:@"%@: type %llu f%llu %llux%llux%llu array %llu mips %llu%@",where,(unsigned long long)d.type, + (unsigned long long)d.format,(unsigned long long)d.width,(unsigned long long)d.height,(unsigned long long)d.depth, + (unsigned long long)d.array_length,(unsigned long long)d.levels,t.label.length?[@" " stringByAppendingString:t.label]:@""]); + } else if(kind==2 && [value isKindOfClass:[NBSampler class]]) { + const NmbSamplerDescriptor d=[(NBSampler *)value descriptor]; + dumpLine(_commandBuffer,[NSString stringWithFormat:@"%@: min %llu mag %llu mip %llu address %llu,%llu,%llu compare %llu lod %g..%g",where, + (unsigned long long)d.min_filter,(unsigned long long)d.mag_filter,(unsigned long long)d.mip_filter, + (unsigned long long)d.s_address,(unsigned long long)d.t_address,(unsigned long long)d.r_address, + (unsigned long long)d.compare,d.lod_min,d.lod_max]); + } else if([value isKindOfClass:[NSData class]]) { NSData *data=value; dumpLine(_commandBuffer,[NSString stringWithFormat:@"%@ bytes %lu:%@",where,(unsigned long)data.length,dumpWords((const uint8_t *)data.bytes,data.length)]); } else if([value isKindOfClass:[NSArray class]]) { @@ -1508,12 +1530,14 @@ static void waitForResource(id resource) { if(_commandBuffer.dumpTargets.count>before)[_dumpEntries addObject:_commandBuffer.dumpTargets.lastObject]; } if(texture)[_commandBuffer.resourceOwners addObject:texture]; + [self dumpBind:texture index:index stage:stage+4]; if(texture && [(NBTexture *)texture surface])[_commandBuffer.surfaces addObject:texture]; if(texture && gWatchSources)[_commandBuffer watchTexture:(NBTexture *)texture]; [self check:nmb_encoder_set_texture(_handle,stage,texture?[(NBTexture *)texture handle]:nullptr,(uint32_t)index) what:"setTexture"]; } - (void)sampler:(id)sampler index:(NSUInteger)index stage:(uint32_t)stage { if(sampler && ![(id)sampler isKindOfClass:[NBSampler class]]){[_commandBuffer refuse:@"foreign sampler"];return;} + [self dumpBind:sampler index:index stage:stage+8]; [self check:nmb_encoder_set_sampler(_handle,stage,sampler?[(NBSampler *)sampler handle]:nullptr,(uint32_t)index) what:"setSamplerState"]; } - (void)sampler:(id)sampler lodMin:(float)lodMin max:(float)lodMax index:(NSUInteger)index stage:(uint32_t)stage { From 1d6849448aceaefe5ef3521597c27b5037030f9e Mon Sep 17 00:00:00 2001 From: HuJK Date: Tue, 27 Oct 2026 12:18:48 +0800 Subject: [PATCH 45/92] Diagnostic switch: no whole-surface cache maintenance (/private/var/ngpu/debug/no-surface-flush) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012Camiwa7VsPwkhfyscKQ6S --- NativeBridgeMetalFrontend.mm | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index c329e78..f698f87 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -930,8 +930,10 @@ static constexpr bool kSurfaceFlush=false; #else static constexpr bool kSurfaceFlush=true; #endif +// Diagnostic: while /private/var/ngpu/debug/no-surface-flush exists when the process starts, no surface is flushed here either. +static const bool gSkipSurfaceFlush=access("/private/var/ngpu/debug/no-surface-flush",F_OK)==0; static void flushSurfaces(NSMutableSet *surfaces) { - if(!kSurfaceFlush || !surfaces.count)return; + if(!kSurfaceFlush || !surfaces.count || gSkipSurfaceFlush)return; std::vector> ranges; ranges.reserve(surfaces.count); for(NBTexture *texture in surfaces)if(texture.surface) From 18a074b9ba6a9f1b28d1d54ccae77d06c8e505a5 Mon Sep 17 00:00:00 2001 From: HuJK Date: Tue, 27 Oct 2026 14:07:24 +0800 Subject: [PATCH 46/92] Diagnostic switch: device queries asked again on every call (/private/var/ngpu/debug/no-answer-cache) Read when a process starts, as no-surface-flush is: an A/B of the answer cache within one boot (restart the render server). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012Camiwa7VsPwkhfyscKQ6S --- NativeBridgeMetalFrontend.mm | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index f698f87..c0643a4 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -3263,14 +3263,17 @@ static bool pipelineArchives(NSArray *list,std::vector &arch static std::unordered_map gAnswers; static NSUInteger countAnswer(id,SEL cmd) {return (NSUInteger)gAnswers.at(cmd);} static BOOL flagAnswer(id,SEL cmd) {return gAnswers.at(cmd)!=0;} +// Diagnostic A/B switch, read when the process starts: ask the runtime on every call again. +static const bool gNoAnswerCache=access("/private/var/ngpu/debug/no-answer-cache",F_OK)==0; static uint64_t askAnswer(SEL cmd,NSUInteger arg) { static std::mutex lock; static std::unordered_map> kept; - {std::lock_guard hold(lock); + if(!gNoAnswerCache) {std::lock_guard hold(lock); auto answers=kept.find(cmd); if(answers!=kept.end()){auto answer=answers->second.find(arg);if(answer!=answers->second.end())return answer->second;}} uint64_t value=0; if(!nmb_device_answer(gDevice,sel_getName(cmd),arg,&value)){NBLOG(@"native-metal: %s(%lu): %@",sel_getName(cmd),(unsigned long)arg,lastError());value=0;} + if(gNoAnswerCache)return value; std::lock_guard hold(lock); kept[cmd][arg]=value; return value; From 782409129377cb6fe3f3b4377f3175247b531539 Mon Sep 17 00:00:00 2001 From: HuJK Date: Tue, 27 Oct 2026 15:25:22 +0800 Subject: [PATCH 47/92] 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 Claude-Session: https://claude.ai/code/session_012Camiwa7VsPwkhfyscKQ6S --- NativeAdrenoMetalPlugin.mm | 5 +++++ NativeBridgeMetalFrontend.mm | 15 ++++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/NativeAdrenoMetalPlugin.mm b/NativeAdrenoMetalPlugin.mm index f93cf8e..4dd2535 100644 --- a/NativeAdrenoMetalPlugin.mm +++ b/NativeAdrenoMetalPlugin.mm @@ -263,6 +263,11 @@ static id deviceForPort(unsigned port) { gpu->mark(writeTask(0x1e7021014 + slide, &one, sizeof one) ? 0x6E01 : 0x6EF1); } #endif + // On an iPad the render server's queue runs at a high GPU priority (CoreAnimation asks for it with setGPUPriority: when it makes + // its MetalContext). A Vulkan queue's priority is fixed when the device opens, before that call, so the render server asks the + // runtime for it here (patches/metal2vulkan/0012): where the host offers priorities, its frames no longer wait on the GPU behind + // an app's. + setenv("METAL2VULKAN_QUEUE_PRIORITY", "high", 0); // The runtime's creation log reports each VkPipeline making's time to the device's sink, which the telemetry's V row sums. setenv("METAL2VULKAN_LOG_PIPELINES", "1", 0); CFPreferencesSetAppValue(CFSTR("CAEnableFramebufferCompression"), kCFBooleanFalse, kCFPreferencesCurrentApplication); diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index c0643a4..16e26a7 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -2395,9 +2395,18 @@ static void completed(void *context) { - (id)commandBufferWithUnretainedReferences {return [self commandBuffer];} - (void)setSubmissionQueue:(dispatch_queue_t)queue {(void)queue;} // submission happens on the caller's thread here // setCompletionQueue: is the property's setter. -// GPU priorities are not applied (one hardware queue): reported as not applied. -- (BOOL)setGPUPriority:(NSUInteger)priority {(void)priority;return NO;} -- (BOOL)setBackgroundGPUPriority:(NSUInteger)priority {(void)priority;return NO;} +// GPU priorities are not applied per queue (a process's queues share one hardware queue, whose priority is set when the device opens): +// reported as not applied. What a process asks for is logged once (the render server's queue is the one CoreAnimation raises). +- (BOOL)setGPUPriority:(NSUInteger)priority { + static std::atomic told{false}; + if(!told.exchange(true))NBLOG(@"native-metal: setGPUPriority:%lu (not applied per queue)",(unsigned long)priority); + return NO; +} +- (BOOL)setBackgroundGPUPriority:(NSUInteger)priority { + static std::atomic told{false}; + if(!told.exchange(true))NBLOG(@"native-metal: setBackgroundGPUPriority:%lu (not applied per queue)",(unsigned long)priority); + return NO; +} // What a new Metal queue reports (MTLIOAccelCommandQueue: normal 1, background 3). RenderBox and Core Image read both before any frame. - (NSUInteger)getGPUPriority {return 1;} - (NSUInteger)getBackgroundGPUPriority {return 3;} From aba35f1517ed9e7f52f69de1d5ff48df2090eafb Mon Sep 17 00:00:00 2001 From: HuJK Date: Tue, 27 Oct 2026 16:47:50 +0800 Subject: [PATCH 48/92] Dump text built only when NATIVE_METAL_DUMP_CB is on The draw dump's text was made on every call and thrown away inside dumpLine/dumpDraw/dumpBind when no dump was asked for: setViewport:, setScissorRect: and the raster state formatted a line for each pass's first 6 draws (dumpShown alone, without dumpMaybe), every drawPrimitives: and drawPatches: formatted its own, setBytes: copied the bytes into an NSData, and setBuffer: built an array and a number. In GFXBench's Driver Overhead 2 setScissorRect: was 22% of the draw thread (Foundation formatting); in the render server's home-screen drag the Foundation and objc time under drawPrimitives: was 2%. Each now builds its text only under dumpMaybe(). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012Camiwa7VsPwkhfyscKQ6S --- NativeBridgeMetalFrontend.mm | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 16e26a7..9fae421 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -1473,6 +1473,8 @@ static void waitForResource(id resource) { _dumpBound[@(stage<<8|(uint32_t)index)]=what?:[NSNull null]; } // NATIVE_METAL_DUMP_CB: the first 6 draws of each pass, with the pipeline, the depth/stencil state and every bound buffer's first words. +// A caller builds a dump's text only under dumpMaybe(): without NATIVE_METAL_DUMP_CB the text was made and thrown away on every call +// (a setScissorRect: was a fifth of GFXBench's Driver Overhead 2 thread, and every drawPrimitives: formatted its line). - (void)dumpDraw:(NSString *)what { if(!dumpMaybe(_commandBuffer))return; // Runs of draws with one pipeline and depth state, every draw counted: a pass's structure (geometry, then light volumes, ...). @@ -1518,7 +1520,7 @@ static void waitForResource(id resource) { - (void)buffer:(id)buffer offset:(NSUInteger)offset index:(NSUInteger)index stage:(uint32_t)stage { if(buffer && ![(id)buffer isKindOfClass:[NBBuffer class]]){[_commandBuffer refuse:@"foreign buffer"];return;} if(buffer)[_commandBuffer.resourceOwners addObject:buffer]; - [self dumpBind:buffer?@[buffer,@(offset)]:nil index:index stage:stage]; + if(dumpMaybe(_commandBuffer))[self dumpBind:buffer?@[buffer,@(offset)]:nil index:index stage:stage]; [self check:nmb_encoder_set_buffer(_handle,stage,buffer?[(NBBuffer *)buffer handle]:nullptr,offset,(uint32_t)index) what:"setBuffer"]; } - (void)texture:(id)texture index:(NSUInteger)index stage:(uint32_t)stage { @@ -1549,7 +1551,7 @@ static void waitForResource(id resource) { [self sampler:(id)clamped index:index stage:stage]; } - (void)bytes:(const void *)bytes length:(NSUInteger)length index:(NSUInteger)index stage:(uint32_t)stage { - [self dumpBind:[NSData dataWithBytes:bytes length:length] index:index stage:stage]; + if(dumpMaybe(_commandBuffer))[self dumpBind:[NSData dataWithBytes:bytes length:length] index:index stage:stage]; [self check:nmb_encoder_set_bytes(_handle,stage,bytes,length,(uint32_t)index) what:"setBytes"]; } - (void)fence:(id)fence stages:(uint64_t)stages update:(bool)update { @@ -1661,20 +1663,20 @@ static void waitForResource(id resource) { } - (void)setViewport:(MTLViewport)v { const double values[6]={v.originX,v.originY,v.width,v.height,v.znear,v.zfar}; - if(dumpShown(self.dumpDraws+1))dumpLine(self.commandBuffer,[NSString stringWithFormat:@"pass %u viewport %g,%g %gx%g depth %g..%g",self.commandBuffer.dumpPasses, + if(dumpMaybe(self.commandBuffer) && dumpShown(self.dumpDraws+1))dumpLine(self.commandBuffer,[NSString stringWithFormat:@"pass %u viewport %g,%g %gx%g depth %g..%g",self.commandBuffer.dumpPasses, v.originX,v.originY,v.width,v.height,v.znear,v.zfar]); [self check:nmb_render_set_viewport(self.handle,values) what:"setViewport"]; } - (void)setScissorRect:(MTLScissorRect)r { const uint64_t values[4]={r.x,r.y,r.width,r.height}; - if(dumpShown(self.dumpDraws+1))dumpLine(self.commandBuffer,[NSString stringWithFormat:@"pass %u scissor %lu,%lu %lux%lu",self.commandBuffer.dumpPasses, + if(dumpMaybe(self.commandBuffer) && dumpShown(self.dumpDraws+1))dumpLine(self.commandBuffer,[NSString stringWithFormat:@"pass %u scissor %lu,%lu %lux%lu",self.commandBuffer.dumpPasses, (unsigned long)r.x,(unsigned long)r.y,(unsigned long)r.width,(unsigned long)r.height]); [self check:nmb_render_set_scissor(self.handle,values) what:"setScissorRect"]; } // The runtime takes cull mode, winding and fill mode together; this encoder keeps Metal's defaults until each is set. - (void)raster { NSNumber *cull=objc_getAssociatedObject(self,"cull"),*winding=objc_getAssociatedObject(self,"winding"),*fill=objc_getAssociatedObject(self,"fill"); - if(dumpShown(self.dumpDraws+1))dumpLine(self.commandBuffer,[NSString stringWithFormat:@"pass %u cull %llu winding %llu fill %llu",self.commandBuffer.dumpPasses, + if(dumpMaybe(self.commandBuffer) && dumpShown(self.dumpDraws+1))dumpLine(self.commandBuffer,[NSString stringWithFormat:@"pass %u cull %llu winding %llu fill %llu",self.commandBuffer.dumpPasses, cull.unsignedLongLongValue,winding.unsignedLongLongValue,fill.unsignedLongLongValue]); [self check:nmb_render_set_raster(self.handle,cull.unsignedLongLongValue,winding.unsignedLongLongValue,fill.unsignedLongLongValue) what:"raster state"]; } @@ -1721,7 +1723,7 @@ static void waitForResource(id resource) { - (void)drawPrimitives:(MTLPrimitiveType)t vertexStart:(NSUInteger)s vertexCount:(NSUInteger)n instanceCount:(NSUInteger)i baseInstance:(NSUInteger)b { if([self skipDraw])return; gDraws++;self.commandBuffer->encodedWork.draws++; - [self dumpDraw:[NSString stringWithFormat:@"prim %lu start %lu count %lu instances %lu base %lu",(unsigned long)t,(unsigned long)s,(unsigned long)n, + if(dumpMaybe(self.commandBuffer))[self dumpDraw:[NSString stringWithFormat:@"prim %lu start %lu count %lu instances %lu base %lu",(unsigned long)t,(unsigned long)s,(unsigned long)n, (unsigned long)i,(unsigned long)b]]; [self check:nmb_render_draw(self.handle,t,(uint32_t)s,(uint32_t)n,(uint32_t)i,(uint32_t)b) what:"drawPrimitives"]; } @@ -1775,7 +1777,7 @@ static void waitForResource(id resource) { if(![self own:pi what:@"patch index buffer"] || ![self own:ci what:@"control point index buffer"] || ![self own:ib what:@"indirect buffer"])return; if([self skipDraw])return; gDraws++;self.commandBuffer->encodedWork.draws++; - [self dumpDraw:[NSString stringWithFormat:@"patches %lu start %lu count %lu instances %lu base %lu%@",(unsigned long)points,(unsigned long)start, + if(dumpMaybe(self.commandBuffer))[self dumpDraw:[NSString stringWithFormat:@"patches %lu start %lu count %lu instances %lu base %lu%@",(unsigned long)points,(unsigned long)start, (unsigned long)count,(unsigned long)instances,(unsigned long)base,ib?@" indirect":@""]]; [self check:nmb_render_draw_patches(self.handle,(uint32_t)points,(uint32_t)start,(uint32_t)count,pi?[(NBBuffer *)pi handle]:nullptr,po, ci?[(NBBuffer *)ci handle]:nullptr,co,(uint32_t)instances,(uint32_t)base,ib?[(NBBuffer *)ib handle]:nullptr,io) what:"drawPatches"]; From 1353601ead97832696a7fff5855468dc2c7738e8 Mon Sep 17 00:00:00 2001 From: HuJK Date: Tue, 27 Oct 2026 18:44:43 +0800 Subject: [PATCH 49/92] An encoding thread, behind a switch: encoder calls recorded by the caller, run by the runtime on a queue of its own The render server's display thread was on the CPU about 90% of a home-screen drag, most of it in the runtime (binding, draws, the Vulkan commands) and the rest in CoreAnimation's own encoding; the GPU waited for it. With /private/var/ngpu/debug/encode-thread (read when a process starts) each encoder call's runtime half -- and an encoder's making, an event, a command buffer's submission -- goes to a serial dispatch queue in call order, and the caller goes on: the two halves run on two cores. Each block takes what it uses by value (C arrays as std::array, setBytes: copied) or strongly (the objects whose handles it passes, fences and samplers included). A wait, the status, the error, the GPU times and a resource's in-flight check let the queue catch up first; a refused encoder fails its command buffer instead of answering nil. Without the file every call runs at once on the caller's thread, as before. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012Camiwa7VsPwkhfyscKQ6S --- NativeBridgeMetalFrontend.mm | 167 +++++++++++++++++++++++------------ 1 file changed, 109 insertions(+), 58 deletions(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 9fae421..1472066 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -12,6 +12,7 @@ #import #import #include +#include #include #include #include @@ -1335,6 +1336,7 @@ static void dumpSurfacesOnce(NSSet *textures,NSSet *owners) { @property(nonatomic,strong) NBQueue *queue; @property(atomic,copy) NSString *encodeFailure; @property(atomic) BOOL committed,failedLocally; +@property(atomic) BOOL submitted; // the runtime has been given the commit (the encoding thread's half of it, -submit) @property(nonatomic) uint64_t committedUs; @property(nonatomic) uint32_t commitIndex; @property(nonatomic,strong) NSMutableArray *scheduledHandlers,*completedHandlers; @@ -1433,7 +1435,34 @@ static void dumpNote(NBCommandBuffer *commandBuffer,NSMutableArray *into,id text if(seen[@"texture"]==texture && [seen[@"level"] unsignedIntegerValue]==level && [seen[@"slice"] unsignedIntegerValue]==slice)return; [into addObject:[NSMutableDictionary dictionaryWithDictionary:@{@"texture":texture,@"level":@(level),@"slice":@(slice),@"where":where}]]; } +// The encoding thread, while new a diagnostic switch read when the process starts (/private/var/ngpu/debug/encode-thread): the runtime's +// half of each encoder call -- binding, draws, the end of a pass, an encoder's making and a command buffer's submission -- runs on a +// serial queue of the process's own, in the order the calls were made, and the caller goes on once it has recorded the call. The render +// server's display thread spent most of a frame in the runtime (a home-screen drag: about 60% of a thread busy 90% of the time); split, +// its own encoding and the runtime's run on two cores at once. What reads a command buffer's state back -- a wait, its status, the +// in-flight use of a resource -- first lets the queue catch up (encodeDrain). Off, each call runs at once on the caller's thread. +static const bool gEncodeThread=access("/private/var/ngpu/debug/encode-thread",F_OK)==0; +static char gEncodeKey; +static dispatch_queue_t encodeQueue() { + static dispatch_queue_t queue=[]{ + dispatch_queue_t q=dispatch_queue_create("native-metal.encode", + dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL,QOS_CLASS_USER_INTERACTIVE,0)); + dispatch_queue_set_specific(q,&gEncodeKey,&gEncodeKey,nullptr); + return q; + }(); + return queue; +} +static bool onEncodeQueue() {return dispatch_get_specific(&gEncodeKey)!=nullptr;} +// A block takes what it uses by value (C arrays as std::array) or strongly (the objects whose handles it passes), as the call made it. +static void encodeLater(dispatch_block_t work) { + if(!gEncodeThread || onEncodeQueue())work();else dispatch_async(encodeQueue(),work); +} +static void encodeDrain() { + if(gEncodeThread && !onEncodeQueue())dispatch_sync(encodeQueue(),^{}); +} +#define ENCODE(...) encodeLater(^{__VA_ARGS__;}) static void waitForResource(id resource) { + encodeDrain(); // every committed command buffer's uses recorded const uint64_t began=nowUs(); NSArray *pending;@synchronized(gInFlight){pending=gInFlight.allObjects;} for(NBCommandBuffer *commandBuffer in pending) { @@ -1459,9 +1488,9 @@ static void waitForResource(id resource) { if(_dumpRuns.count>1 || _dumpDraws>6)for(NSArray *run in _dumpRuns) dumpLine(_commandBuffer,[NSString stringWithFormat:@"pass %u run: draws %@..%lu | %@ | depth %@",_commandBuffer.dumpPasses,run[2], (unsigned long)([run[2] unsignedIntegerValue]+[run[3] unsignedIntegerValue]-1),run[0],run[1]]); - NmbEncoder *handle=_handle;_handle=nullptr; - if(!handle){[_commandBuffer refuse:@"endEncoding twice"];return;} - [self check:nmb_encoder_end(handle) what:"endEncoding"]; + ENCODE(NmbEncoder *handle=self->_handle;self->_handle=nullptr; + if(!handle){[self->_commandBuffer refuse:@"endEncoding twice"];return;} + [self check:nmb_encoder_end(handle) what:"endEncoding"]); } - (void)insertDebugSignpost:(NSString *)s {(void)s;} - (void)pushDebugGroup:(NSString *)s {(void)s;} @@ -1521,7 +1550,7 @@ static void waitForResource(id resource) { if(buffer && ![(id)buffer isKindOfClass:[NBBuffer class]]){[_commandBuffer refuse:@"foreign buffer"];return;} if(buffer)[_commandBuffer.resourceOwners addObject:buffer]; if(dumpMaybe(_commandBuffer))[self dumpBind:buffer?@[buffer,@(offset)]:nil index:index stage:stage]; - [self check:nmb_encoder_set_buffer(_handle,stage,buffer?[(NBBuffer *)buffer handle]:nullptr,offset,(uint32_t)index) what:"setBuffer"]; + ENCODE([self check:nmb_encoder_set_buffer(self->_handle,stage,buffer?[(NBBuffer *)buffer handle]:nullptr,offset,(uint32_t)index) what:"setBuffer"]); } - (void)texture:(id)texture index:(NSUInteger)index stage:(uint32_t)stage { if(texture && ![(id)texture isKindOfClass:[NBTexture class]]){[_commandBuffer refuse:@"foreign texture"];return;} @@ -1537,12 +1566,12 @@ static void waitForResource(id resource) { [self dumpBind:texture index:index stage:stage+4]; if(texture && [(NBTexture *)texture surface])[_commandBuffer.surfaces addObject:texture]; if(texture && gWatchSources)[_commandBuffer watchTexture:(NBTexture *)texture]; - [self check:nmb_encoder_set_texture(_handle,stage,texture?[(NBTexture *)texture handle]:nullptr,(uint32_t)index) what:"setTexture"]; + ENCODE([self check:nmb_encoder_set_texture(self->_handle,stage,texture?[(NBTexture *)texture handle]:nullptr,(uint32_t)index) what:"setTexture"]); } - (void)sampler:(id)sampler index:(NSUInteger)index stage:(uint32_t)stage { if(sampler && ![(id)sampler isKindOfClass:[NBSampler class]]){[_commandBuffer refuse:@"foreign sampler"];return;} [self dumpBind:sampler index:index stage:stage+8]; - [self check:nmb_encoder_set_sampler(_handle,stage,sampler?[(NBSampler *)sampler handle]:nullptr,(uint32_t)index) what:"setSamplerState"]; + ENCODE([self check:nmb_encoder_set_sampler(self->_handle,stage,sampler?[(NBSampler *)sampler handle]:nullptr,(uint32_t)index) what:"setSamplerState"]); } - (void)sampler:(id)sampler lodMin:(float)lodMin max:(float)lodMax index:(NSUInteger)index stage:(uint32_t)stage { if(sampler && ![(id)sampler isKindOfClass:[NBSampler class]]){[_commandBuffer refuse:@"foreign sampler"];return;} @@ -1552,19 +1581,22 @@ static void waitForResource(id resource) { } - (void)bytes:(const void *)bytes length:(NSUInteger)length index:(NSUInteger)index stage:(uint32_t)stage { if(dumpMaybe(_commandBuffer))[self dumpBind:[NSData dataWithBytes:bytes length:length] index:index stage:stage]; - [self check:nmb_encoder_set_bytes(_handle,stage,bytes,length,(uint32_t)index) what:"setBytes"]; + if(!gEncodeThread){[self check:nmb_encoder_set_bytes(_handle,stage,bytes,length,(uint32_t)index) what:"setBytes"];return;} + const std::vector copy((const uint8_t *)bytes,(const uint8_t *)bytes+(bytes?length:0)); // the caller's bytes are its own again + ENCODE([self check:nmb_encoder_set_bytes(self->_handle,stage,copy.empty()?nullptr:copy.data(),length,(uint32_t)index) what:"setBytes"]); } - (void)fence:(id)fence stages:(uint64_t)stages update:(bool)update { if(![(id)fence isKindOfClass:[NBFence class]]){[_commandBuffer refuse:@"foreign fence"];return;} - NmbFence *handle=[(NBFence *)fence handle]; - [self check:update?nmb_encoder_update_fence(_handle,handle,stages):nmb_encoder_wait_fence(_handle,handle,stages) what:update?"updateFence":"waitForFence"]; + ENCODE(NmbFence *handle=[(NBFence *)fence handle]; + [self check:update?nmb_encoder_update_fence(self->_handle,handle,stages):nmb_encoder_wait_fence(self->_handle,handle,stages) + what:update?"updateFence":"waitForFence"]); } - (void)updateFence:(id)fence {[self fence:fence stages:0 update:true];} - (void)waitForFence:(id)fence {[self fence:fence stages:0 update:false];} - (void)offset:(NSUInteger)offset index:(NSUInteger)index stage:(uint32_t)stage { id bound=_dumpBound[@(stage<<8|(uint32_t)index)]; if([bound isKindOfClass:[NSArray class]])[self dumpBind:@[bound[0],@(offset)] index:index stage:stage]; - [self check:nmb_encoder_set_buffer_offset(_handle,stage,offset,(uint32_t)index) what:"setBufferOffset"]; + ENCODE([self check:nmb_encoder_set_buffer_offset(self->_handle,stage,offset,(uint32_t)index) what:"setBufferOffset"]); } @end @@ -1574,7 +1606,7 @@ static void waitForResource(id resource) { - (MTLDispatchType)dispatchType {return MTLDispatchTypeSerial;} - (void)setComputePipelineState:(id)state { if(![(id)state isKindOfClass:[NBComputePipeline class]]){[self.commandBuffer refuse:@"foreign compute pipeline"];return;} - [self check:nmb_compute_set_pipeline(self.handle,[(NBComputePipeline *)state handle]) what:"setComputePipelineState"]; + ENCODE([self check:nmb_compute_set_pipeline(self.handle,[(NBComputePipeline *)state handle]) what:"setComputePipelineState"]); } - (void)setBuffer:(id)b offset:(NSUInteger)o atIndex:(NSUInteger)i {[self buffer:b offset:o index:i stage:0];} - (void)setBufferOffset:(NSUInteger)o atIndex:(NSUInteger)i {[self offset:o index:i stage:0];} @@ -1595,12 +1627,12 @@ static void waitForResource(id resource) { for(NSUInteger k=0;kencodedWork.dispatches++; - const uint64_t s[3]={size.width,size.height,size.depth},g[3]={group.width,group.height,group.depth}; - [self check:nmb_compute_dispatch(self.handle,s,g,threads) what:"dispatch"]; + const std::array s={size.width,size.height,size.depth},g={group.width,group.height,group.depth}; + ENCODE([self check:nmb_compute_dispatch(self.handle,s.data(),g.data(),threads) what:"dispatch"]); } - (void)dispatchThreads:(MTLSize)t threadsPerThreadgroup:(MTLSize)g {[self dispatch:t group:g threads:true];} - (void)dispatchThreadgroups:(MTLSize)t threadsPerThreadgroup:(MTLSize)g {[self dispatch:t group:g threads:false];} @@ -1608,8 +1640,8 @@ static void waitForResource(id resource) { if(![(id)buffer isKindOfClass:[NBBuffer class]]){[self.commandBuffer refuse:@"foreign indirect buffer"];return;} [self.commandBuffer.resourceOwners addObject:buffer]; gDispatches++;self.commandBuffer->encodedWork.dispatches++; - const uint64_t g[3]={group.width,group.height,group.depth}; - [self check:nmb_compute_dispatch_indirect(self.handle,[(NBBuffer *)buffer handle],offset,g) what:"indirect dispatch"]; + const std::array g={group.width,group.height,group.depth}; + ENCODE([self check:nmb_compute_dispatch_indirect(self.handle,[(NBBuffer *)buffer handle],offset,g.data()) what:"indirect dispatch"]); } - (void)useResource:(id)r usage:(MTLResourceUsage)u {(void)r;(void)u;} - (void)memoryBarrierWithScope:(MTLBarrierScope)s {(void)s;} @@ -1621,7 +1653,7 @@ static void waitForResource(id resource) { - (void)setRenderPipelineState:(id)state { if(![(id)state isKindOfClass:[NBRenderPipeline class]]){[self.commandBuffer refuse:@"foreign render pipeline"];return;} if(dumpRange().first)self.dumpPipeline=state; - [self check:nmb_render_set_pipeline(self.handle,[(NBRenderPipeline *)state handle]) what:"setRenderPipelineState"]; + ENCODE([self check:nmb_render_set_pipeline(self.handle,[(NBRenderPipeline *)state handle]) what:"setRenderPipelineState"]); } - (void)setVertexBuffer:(id)b offset:(NSUInteger)o atIndex:(NSUInteger)i {[self buffer:b offset:o index:i stage:1];} - (void)setFragmentBuffer:(id)b offset:(NSUInteger)o atIndex:(NSUInteger)i {[self buffer:b offset:o index:i stage:2];} @@ -1659,36 +1691,36 @@ static void waitForResource(id resource) { - (void)setDepthStencilState:(id)state { if(state && ![(id)state isKindOfClass:[NBDepthStencil class]]){[self.commandBuffer refuse:@"foreign depth/stencil state"];return;} if(dumpRange().first)self.dumpDepth=state; - [self check:nmb_render_set_depth_stencil(self.handle,state?[(NBDepthStencil *)state handle]:nullptr) what:"setDepthStencilState"]; + ENCODE([self check:nmb_render_set_depth_stencil(self.handle,state?[(NBDepthStencil *)state handle]:nullptr) what:"setDepthStencilState"]); } - (void)setViewport:(MTLViewport)v { - const double values[6]={v.originX,v.originY,v.width,v.height,v.znear,v.zfar}; + const std::array values={v.originX,v.originY,v.width,v.height,v.znear,v.zfar}; if(dumpMaybe(self.commandBuffer) && dumpShown(self.dumpDraws+1))dumpLine(self.commandBuffer,[NSString stringWithFormat:@"pass %u viewport %g,%g %gx%g depth %g..%g",self.commandBuffer.dumpPasses, v.originX,v.originY,v.width,v.height,v.znear,v.zfar]); - [self check:nmb_render_set_viewport(self.handle,values) what:"setViewport"]; + ENCODE([self check:nmb_render_set_viewport(self.handle,values.data()) what:"setViewport"]); } - (void)setScissorRect:(MTLScissorRect)r { - const uint64_t values[4]={r.x,r.y,r.width,r.height}; + const std::array values={r.x,r.y,r.width,r.height}; if(dumpMaybe(self.commandBuffer) && dumpShown(self.dumpDraws+1))dumpLine(self.commandBuffer,[NSString stringWithFormat:@"pass %u scissor %lu,%lu %lux%lu",self.commandBuffer.dumpPasses, (unsigned long)r.x,(unsigned long)r.y,(unsigned long)r.width,(unsigned long)r.height]); - [self check:nmb_render_set_scissor(self.handle,values) what:"setScissorRect"]; + ENCODE([self check:nmb_render_set_scissor(self.handle,values.data()) what:"setScissorRect"]); } // The runtime takes cull mode, winding and fill mode together; this encoder keeps Metal's defaults until each is set. - (void)raster { NSNumber *cull=objc_getAssociatedObject(self,"cull"),*winding=objc_getAssociatedObject(self,"winding"),*fill=objc_getAssociatedObject(self,"fill"); if(dumpMaybe(self.commandBuffer) && dumpShown(self.dumpDraws+1))dumpLine(self.commandBuffer,[NSString stringWithFormat:@"pass %u cull %llu winding %llu fill %llu",self.commandBuffer.dumpPasses, cull.unsignedLongLongValue,winding.unsignedLongLongValue,fill.unsignedLongLongValue]); - [self check:nmb_render_set_raster(self.handle,cull.unsignedLongLongValue,winding.unsignedLongLongValue,fill.unsignedLongLongValue) what:"raster state"]; + ENCODE([self check:nmb_render_set_raster(self.handle,cull.unsignedLongLongValue,winding.unsignedLongLongValue,fill.unsignedLongLongValue) what:"raster state"]); } - (void)setCullMode:(MTLCullMode)m {objc_setAssociatedObject(self,"cull",@(m),OBJC_ASSOCIATION_RETAIN);[self raster];} - (void)setFrontFacingWinding:(MTLWinding)w {objc_setAssociatedObject(self,"winding",@(w),OBJC_ASSOCIATION_RETAIN);[self raster];} - (void)setTriangleFillMode:(MTLTriangleFillMode)f {objc_setAssociatedObject(self,"fill",@(f),OBJC_ASSOCIATION_RETAIN);[self raster];} - (void)setDepthBias:(float)bias slopeScale:(float)slope clamp:(float)clamp { - [self check:nmb_render_set_depth_bias(self.handle,bias,slope,clamp) what:"setDepthBias"]; + ENCODE([self check:nmb_render_set_depth_bias(self.handle,bias,slope,clamp) what:"setDepthBias"]); } -- (void)setLineWidth:(float)width {[self check:nmb_render_set_line_width(self.handle,width) what:"setLineWidth"];} +- (void)setLineWidth:(float)width {ENCODE([self check:nmb_render_set_line_width(self.handle,width) what:"setLineWidth"]);} - (void)setVisibilityResultMode:(MTLVisibilityResultMode)mode offset:(NSUInteger)offset { - [self check:nmb_render_set_visibility_mode(self.handle,mode,offset) what:"setVisibilityResultMode"]; + ENCODE([self check:nmb_render_set_visibility_mode(self.handle,mode,offset) what:"setVisibilityResultMode"]); } - (void)setFragmentTexture:(id)t atTextureIndex:(NSUInteger)ti samplerState:(id)s atSamplerIndex:(NSUInteger)si { [self texture:t index:ti stage:2];[self sampler:s index:si stage:2]; @@ -1701,12 +1733,12 @@ static void waitForResource(id resource) { - (void)setTileBytes:(const void *)p length:(NSUInteger)n atIndex:(NSUInteger)i {(void)p;(void)n;(void)i;[self.commandBuffer refuse:@"tile shaders are not supported"];} - (void)dispatchThreadsPerTile:(MTLSize)size {(void)size;[self.commandBuffer refuse:@"tile shaders are not supported"];} - (void)dispatchThreadsPerTile:(MTLSize)size inRegion:(MTLRegion)region {(void)size;(void)region;[self.commandBuffer refuse:@"tile shaders are not supported"];} -- (void)setStencilReferenceValue:(uint32_t)v {[self check:nmb_render_set_stencil_reference(self.handle,v,v) what:"setStencilReferenceValue"];} +- (void)setStencilReferenceValue:(uint32_t)v {ENCODE([self check:nmb_render_set_stencil_reference(self.handle,v,v) what:"setStencilReferenceValue"]);} - (void)setStencilFrontReferenceValue:(uint32_t)f backReferenceValue:(uint32_t)b { - [self check:nmb_render_set_stencil_reference(self.handle,f,b) what:"setStencilReferenceValues"]; + ENCODE([self check:nmb_render_set_stencil_reference(self.handle,f,b) what:"setStencilReferenceValues"]); } - (void)setBlendColorRed:(float)r green:(float)g blue:(float)b alpha:(float)a { - const float c[4]={r,g,b,a};[self check:nmb_render_set_blend_color(self.handle,c) what:"setBlendColor"]; + const std::array c={r,g,b,a};ENCODE([self check:nmb_render_set_blend_color(self.handle,c.data()) what:"setBlendColor"]); } // NATIVE_METAL_SKIP_DRAWS=PASS:FIRST:LAST: every command buffer's render pass PASS (counted from 1) leaves out its draws FIRST..LAST // (counted from 1), to find which draws make what a frame shows. @@ -1725,7 +1757,7 @@ static void waitForResource(id resource) { gDraws++;self.commandBuffer->encodedWork.draws++; if(dumpMaybe(self.commandBuffer))[self dumpDraw:[NSString stringWithFormat:@"prim %lu start %lu count %lu instances %lu base %lu",(unsigned long)t,(unsigned long)s,(unsigned long)n, (unsigned long)i,(unsigned long)b]]; - [self check:nmb_render_draw(self.handle,t,(uint32_t)s,(uint32_t)n,(uint32_t)i,(uint32_t)b) what:"drawPrimitives"]; + ENCODE([self check:nmb_render_draw(self.handle,t,(uint32_t)s,(uint32_t)n,(uint32_t)i,(uint32_t)b) what:"drawPrimitives"]); } - (void)drawPrimitives:(MTLPrimitiveType)t vertexStart:(NSUInteger)s vertexCount:(NSUInteger)n instanceCount:(NSUInteger)i { [self drawPrimitives:t vertexStart:s vertexCount:n instanceCount:i baseInstance:0]; @@ -1749,7 +1781,7 @@ static void waitForResource(id resource) { (unsigned long)t,(unsigned long)n,(unsigned long)k,(unsigned long)o,(unsigned long)b.length,first.length?first:@" unseen", (unsigned long)i,(long)v,(unsigned long)bi]]; } else [self dumpDraw:@"indexed"]; - [self check:nmb_render_draw_indexed(self.handle,t,(uint32_t)n,k,[(NBBuffer *)b handle],o,(uint32_t)i,(int32_t)v,(uint32_t)bi) what:"drawIndexedPrimitives"]; + ENCODE([self check:nmb_render_draw_indexed(self.handle,t,(uint32_t)n,k,[(NBBuffer *)b handle],o,(uint32_t)i,(int32_t)v,(uint32_t)bi) what:"drawIndexedPrimitives"]); } - (void)drawIndexedPrimitives:(MTLPrimitiveType)t indexCount:(NSUInteger)n indexType:(MTLIndexType)k indexBuffer:(id)b indexBufferOffset:(NSUInteger)o instanceCount:(NSUInteger)i { @@ -1767,10 +1799,10 @@ static void waitForResource(id resource) { } - (void)setTessellationFactorBuffer:(id)b offset:(NSUInteger)o instanceStride:(NSUInteger)stride { if(![self own:b what:@"tessellation factor buffer"])return; - [self check:nmb_render_set_tessellation_factor_buffer(self.handle,b?[(NBBuffer *)b handle]:nullptr,o,stride) what:"setTessellationFactorBuffer"]; + ENCODE([self check:nmb_render_set_tessellation_factor_buffer(self.handle,b?[(NBBuffer *)b handle]:nullptr,o,stride) what:"setTessellationFactorBuffer"]); } - (void)setTessellationFactorScale:(float)scale { - [self check:nmb_render_set_tessellation_factor_scale(self.handle,scale) what:"setTessellationFactorScale"]; + ENCODE([self check:nmb_render_set_tessellation_factor_scale(self.handle,scale) what:"setTessellationFactorScale"]); } - (void)patches:(NSUInteger)points start:(NSUInteger)start count:(NSUInteger)count patchIndex:(id)pi offset:(NSUInteger)po points:(id)ci offset:(NSUInteger)co instances:(NSUInteger)instances base:(NSUInteger)base indirect:(id)ib offset:(NSUInteger)io { @@ -1779,8 +1811,8 @@ static void waitForResource(id resource) { gDraws++;self.commandBuffer->encodedWork.draws++; if(dumpMaybe(self.commandBuffer))[self dumpDraw:[NSString stringWithFormat:@"patches %lu start %lu count %lu instances %lu base %lu%@",(unsigned long)points,(unsigned long)start, (unsigned long)count,(unsigned long)instances,(unsigned long)base,ib?@" indirect":@""]]; - [self check:nmb_render_draw_patches(self.handle,(uint32_t)points,(uint32_t)start,(uint32_t)count,pi?[(NBBuffer *)pi handle]:nullptr,po, - ci?[(NBBuffer *)ci handle]:nullptr,co,(uint32_t)instances,(uint32_t)base,ib?[(NBBuffer *)ib handle]:nullptr,io) what:"drawPatches"]; + ENCODE([self check:nmb_render_draw_patches(self.handle,(uint32_t)points,(uint32_t)start,(uint32_t)count,pi?[(NBBuffer *)pi handle]:nullptr,po, + ci?[(NBBuffer *)ci handle]:nullptr,co,(uint32_t)instances,(uint32_t)base,ib?[(NBBuffer *)ib handle]:nullptr,io) what:"drawPatches"]); } - (void)drawPatches:(NSUInteger)n patchStart:(NSUInteger)s patchCount:(NSUInteger)c patchIndexBuffer:(id)pi patchIndexBufferOffset:(NSUInteger)po instanceCount:(NSUInteger)i baseInstance:(NSUInteger)b { @@ -1830,17 +1862,18 @@ static void waitForResource(id resource) { } - (void)copyFromBuffer:(id)s sourceOffset:(NSUInteger)so toBuffer:(id)d destinationOffset:(NSUInteger)dO size:(NSUInteger)n { if(![self buffers:NB_OBJECTS(s,d) count:2])return; - [self check:nmb_blit_copy_buffer(self.handle,[(NBBuffer *)s handle],so,[(NBBuffer *)d handle],dO,n) what:"copyFromBuffer:toBuffer:"]; + ENCODE([self check:nmb_blit_copy_buffer(self.handle,[(NBBuffer *)s handle],so,[(NBBuffer *)d handle],dO,n) what:"copyFromBuffer:toBuffer:"]); } - (void)fillBuffer:(id)b range:(NSRange)r value:(uint8_t)v { if(![self buffers:NB_OBJECTS(b) count:1])return; - [self check:nmb_blit_fill_buffer(self.handle,[(NBBuffer *)b handle],r.location,r.location+r.length,v) what:"fillBuffer"]; + ENCODE([self check:nmb_blit_fill_buffer(self.handle,[(NBBuffer *)b handle],r.location,r.location+r.length,v) what:"fillBuffer"]); } - (void)copyFromTexture:(id)s sourceSlice:(NSUInteger)ss sourceLevel:(NSUInteger)sl sourceOrigin:(MTLOrigin)so sourceSize:(MTLSize)z toTexture:(id)d destinationSlice:(NSUInteger)ds destinationLevel:(NSUInteger)dl destinationOrigin:(MTLOrigin)dO { if(![self textures:NB_OBJECTS(s,d) count:2])return; - const uint64_t a[3]={so.x,so.y,so.z},size[3]={z.width,z.height,z.depth},b[3]={dO.x,dO.y,dO.z}; - [self check:nmb_blit_copy_texture(self.handle,[(NBTexture *)s handle],ss,sl,a,size,[(NBTexture *)d handle],ds,dl,b) what:"copyFromTexture:toTexture:"]; + const std::array a={so.x,so.y,so.z},size={z.width,z.height,z.depth},b={dO.x,dO.y,dO.z}; + ENCODE([self check:nmb_blit_copy_texture(self.handle,[(NBTexture *)s handle],ss,sl,a.data(),size.data(),[(NBTexture *)d handle],ds,dl,b.data()) + what:"copyFromTexture:toTexture:"]); } - (void)copyFromTexture:(id)s toTexture:(id)d { if(![self textures:NB_OBJECTS(s,d) count:2])return; @@ -1871,18 +1904,20 @@ static void waitForResource(id resource) { [(NBTexture *)d replacePvrtcRegion:MTLRegionMake2D(dO.x,dO.y,z.width,z.height) mipmapLevel:level slice:slice withBytes:contents+o length:std::min(want,have)]; return; } - const uint64_t size[3]={z.width,z.height,z.depth},b[3]={dO.x,dO.y,dO.z}; - [self check:nmb_blit_buffer_to_texture(self.handle,[(NBBuffer *)s handle],o,row,image,size,[(NBTexture *)d handle],slice,level,b) what:"copyFromBuffer:toTexture:"]; + const std::array size={z.width,z.height,z.depth},b={dO.x,dO.y,dO.z}; + ENCODE([self check:nmb_blit_buffer_to_texture(self.handle,[(NBBuffer *)s handle],o,row,image,size.data(),[(NBTexture *)d handle],slice,level,b.data()) + what:"copyFromBuffer:toTexture:"]); } - (void)copyFromTexture:(id)s sourceSlice:(NSUInteger)slice sourceLevel:(NSUInteger)level sourceOrigin:(MTLOrigin)so sourceSize:(MTLSize)z toBuffer:(id)d destinationOffset:(NSUInteger)o destinationBytesPerRow:(NSUInteger)row destinationBytesPerImage:(NSUInteger)image { if(![self textures:NB_OBJECTS(s) count:1] || ![self buffers:NB_OBJECTS(d) count:1])return; - const uint64_t a[3]={so.x,so.y,so.z},size[3]={z.width,z.height,z.depth}; - [self check:nmb_blit_texture_to_buffer(self.handle,[(NBTexture *)s handle],slice,level,a,size,[(NBBuffer *)d handle],o,row,image) what:"copyFromTexture:toBuffer:"]; + const std::array a={so.x,so.y,so.z},size={z.width,z.height,z.depth}; + ENCODE([self check:nmb_blit_texture_to_buffer(self.handle,[(NBTexture *)s handle],slice,level,a.data(),size.data(),[(NBBuffer *)d handle],o,row,image) + what:"copyFromTexture:toBuffer:"]); } - (void)generateMipmapsForTexture:(id)t { if(![self textures:NB_OBJECTS(t) count:1])return; - [self check:nmb_blit_generate_mipmaps(self.handle,[(NBTexture *)t handle]) what:"generateMipmapsForTexture"]; + ENCODE([self check:nmb_blit_generate_mipmaps(self.handle,[(NBTexture *)t handle]) what:"generateMipmapsForTexture"]); } - (void)synchronizeResource:(id)r {(void)r;} @end @@ -2081,10 +2116,12 @@ static void dumpReadback(NBCommandBuffer *source,uint64_t appIndex) { } - (MTLCommandBufferStatus)status { if(self.failedLocally)return MTLCommandBufferStatusError; + if(self.committed && !self.submitted)return MTLCommandBufferStatusCommitted; return (MTLCommandBufferStatus)nmb_command_buffer_status(_handle); } - (NSError *)error { if(self.failedLocally)return nbError(self.encodeFailure); + if(!self.submitted)return nil; if(self.status!=MTLCommandBufferStatusError)return nil; char text[512]={};nmb_command_buffer_error(_handle,text,sizeof text); uint64_t code=nmb_command_buffer_metal_error_code(_handle); @@ -2107,23 +2144,27 @@ static void dumpReadback(NBCommandBuffer *source,uint64_t appIndex) { - (CFTimeInterval)kernelStartTime {return 0;} - (CFTimeInterval)kernelEndTime {return 0;} - (BOOL)commitAndWaitUntilSubmitted {[(id)self commit];[self waitUntilScheduled];return !self.failedLocally;} -- (CFTimeInterval)GPUStartTime {return nmb_command_buffer_gpu_time(_handle,false);} -- (CFTimeInterval)GPUEndTime {return nmb_command_buffer_gpu_time(_handle,true);} -- (id)encoder:(NBEncoder *)encoder handle:(NmbEncoder *)handle what:(NSString *)what { +- (CFTimeInterval)GPUStartTime {return self.submitted?nmb_command_buffer_gpu_time(_handle,false):0;} +- (CFTimeInterval)GPUEndTime {return self.submitted?nmb_command_buffer_gpu_time(_handle,true):0;} +// An encoder whose runtime half `make` makes, on the encoding thread when that is on. A refused encoder is still an object: its calls +// fail on the null handle and the buffer completes with an error. RenderBox caches the encoder's IMPs with methodForSelector:, and on +// nil it called address 0 (i568: WidgetRenderer_Default crashed eight launches running, then hit its open cap and lost the device). +- (id)encoder:(NBEncoder *)encoder make:(NmbEncoder *(^)(void))make what:(NSString *)what { if(self.committed){[self refuse:[what stringByAppendingString:@" after commit"]];return nil;} - /* A refused encoder is still an object: its calls fail on the null handle and the buffer completes with an error. RenderBox caches the - * encoder's IMPs with methodForSelector:, and on nil it called address 0 (i568: WidgetRenderer_Default crashed eight launches running, then - * hit its open cap and lost the device). */ - if(!handle){[self refuse:[NSString stringWithFormat:@"%@: %@",what,lastError()]];encoder.commandBuffer=self;return encoder;} + encoder.commandBuffer=self; + ENCODE(NmbEncoder *handle=make(); + if(!handle)[self refuse:[NSString stringWithFormat:@"%@: %@",what,lastError()]]; + encoder.handle=handle); if([encoder isKindOfClass:[NBComputeEncoder class]])_dumpKernels++; if(_dumpPending.count) { if(!_dumpTargets)_dumpTargets=[NSMutableArray array]; [_dumpTargets addObjectsFromArray:_dumpPending];encoder.dumpEntries=_dumpPending;_dumpPending=nil; } - encoder.handle=handle;encoder.commandBuffer=self;return encoder; + return encoder; } - (id)computeCommandEncoder { - return [self encoder:[NBComputeEncoder new] handle:self.committed?nullptr:nmb_compute_encoder_new(_handle) what:@"computeCommandEncoder"]; + NmbCommandBuffer *handle=_handle; + return [self encoder:[NBComputeEncoder new] make:^{return nmb_compute_encoder_new(handle);} what:@"computeCommandEncoder"]; } - (id)computeCommandEncoderWithDispatchType:(MTLDispatchType)type { if(type!=MTLDispatchTypeSerial){[self refuse:@"concurrent compute dispatch"];return nil;} @@ -2131,7 +2172,8 @@ static void dumpReadback(NBCommandBuffer *source,uint64_t appIndex) { } - (id)blitCommandEncoder { gBlits++;encodedWork.blits++; - return [self encoder:[NBBlitEncoder new] handle:self.committed?nullptr:nmb_blit_encoder_new(_handle) what:@"blitCommandEncoder"]; + NmbCommandBuffer *handle=_handle; + return [self encoder:[NBBlitEncoder new] make:^{return nmb_blit_encoder_new(handle);} what:@"blitCommandEncoder"]; } - (id)renderCommandEncoderWithDescriptor:(MTLRenderPassDescriptor *)d { NmbRenderPassDescriptor pass={}; @@ -2201,7 +2243,8 @@ static void dumpReadback(NBCommandBuffer *source,uint64_t appIndex) { [_resourceOwners addObject:d.visibilityResultBuffer]; pass.visibility=[(NBBuffer *)d.visibilityResultBuffer handle]; } - return [self encoder:[NBRenderEncoder new] handle:self.committed?nullptr:nmb_render_encoder_new(_handle,&pass) what:@"renderCommandEncoder"]; + NmbCommandBuffer *handle=_handle; + return [self encoder:[NBRenderEncoder new] make:^{return nmb_render_encoder_new(handle,&pass);} what:@"renderCommandEncoder"]; } - (void)addScheduledHandler:(MTLCommandBufferHandler)block {if(block)@synchronized(self){[_scheduledHandlers addObject:[block copy]];}} - (void)addCompletedHandler:(MTLCommandBufferHandler)block {if(block)@synchronized(self){[_completedHandlers addObject:[block copy]];}} @@ -2212,9 +2255,9 @@ static void dumpReadback(NBCommandBuffer *source,uint64_t appIndex) { - (void)event:(id)event value:(uint64_t)value signal:(bool)signal { if(![(id)event isKindOfClass:[NBEvent class]]){[self refuse:@"foreign event"];return;} if(self.committed){[self refuse:@"event encoded after commit"];return;} - NmbEvent *handle=[(NBEvent *)event handle]; - if(!(signal?nmb_command_buffer_signal_event(_handle,handle,value):nmb_command_buffer_wait_event(_handle,handle,value))) - [self refuse:[NSString stringWithFormat:@"%s: %@",signal?"encodeSignalEvent":"encodeWaitForEvent",lastError()]]; + ENCODE(NmbEvent *handle=[(NBEvent *)event handle]; + if(!(signal?nmb_command_buffer_signal_event(self->_handle,handle,value):nmb_command_buffer_wait_event(self->_handle,handle,value))) + [self refuse:[NSString stringWithFormat:@"%s: %@",signal?"encodeSignalEvent":"encodeWaitForEvent",lastError()]]); } - (void)encodeSignalEvent:(id)event value:(uint64_t)value {[self event:event value:value signal:true];} - (void)encodeWaitForEvent:(id)event value:(uint64_t)value {[self event:event value:value signal:false];} @@ -2288,6 +2331,10 @@ static void dumpReadback(NBCommandBuffer *source,uint64_t appIndex) { dispatch_group_enter(_handlers); dispatch_group_enter(_schedulingHandlers); @synchronized(gInFlight){[gInFlight addObject:self];} + ENCODE([self submit]); +} +// The runtime's half of a commit, on the encoding thread after the command buffer's encoders. +- (void)submit { if(!self.encodeFailure) { flushSurfaces(_surfaces); if(gLogTextures)dumpSurfacesOnce(_surfaces,_resourceOwners); @@ -2298,6 +2345,7 @@ static void dumpReadback(NBCommandBuffer *source,uint64_t appIndex) { if(nmb_command_buffer_on_completed(_handle,completed,context) && nmb_command_buffer_on_scheduled(_handle,scheduled,context) && nmb_command_buffer_commit(_handle)) { const auto range=dumpRange(); + self.submitted=YES; if(appIndex && range.first && appIndex>=range.first && appIndex<(uint64_t)range.first+range.second)dumpReadback(self,appIndex); [self fireEarly];return; } @@ -2305,6 +2353,7 @@ static void dumpReadback(NBCommandBuffer *source,uint64_t appIndex) { [self refuse:[@"commit: " stringByAppendingString:lastError()]]; } self.failedLocally=YES;gCommitFailures++; + self.submitted=YES; dispatch_async(dispatch_get_global_queue(QOS_CLASS_DEFAULT,0),^{[self finish];}); } - (void)fireEarly { @@ -2317,6 +2366,7 @@ static void dumpReadback(NBCommandBuffer *source,uint64_t appIndex) { } - (void)waitUntilCompleted { if(!self.committed)return; + encodeDrain(); const uint64_t began=nowUs(); if(!self.failedLocally)nmb_command_buffer_wait(_handle,0); const uint32_t cpuStart=threadCpuUs(); @@ -2330,6 +2380,7 @@ static void dumpReadback(NBCommandBuffer *source,uint64_t appIndex) { } - (void)waitUntilScheduled { if(!self.committed)return; + encodeDrain(); const uint64_t began=nowUs(); if(!self.failedLocally && !nmb_command_buffer_wait_scheduled(_handle)){ NBLOG(@"native-metal: waitUntilScheduled refused: %@",lastError());return; From 6c7b0a0823c49d7a73c897b068435b418cfe078c Mon Sep 17 00:00:00 2001 From: HuJK Date: Tue, 27 Oct 2026 19:03:30 +0800 Subject: [PATCH 50/92] Encoding thread: one thread of its own, fed in batches The dispatch queue took a call at a time and woke one of the pool's threads for each: GFXBench's Driver Overhead 2, whose calls are many and cheap, drew 16% fewer frames on 60% more CPU a frame, and the render server's runtime work was spread over half a dozen threads. A thread of the frontend's own now runs batches a calling thread hands over at 64 calls, at an encoder's end, at a commit and before a wait; a drain waits for every batch handed over before it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012Camiwa7VsPwkhfyscKQ6S --- NativeBridgeMetalFrontend.mm | 66 ++++++++++++++++++++++++++++++------ 1 file changed, 55 insertions(+), 11 deletions(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 1472066..0abf6c3 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -13,6 +13,9 @@ #import #include #include +#include +#include +#include #include #include #include @@ -1442,23 +1445,62 @@ static void dumpNote(NBCommandBuffer *commandBuffer,NSMutableArray *into,id text // its own encoding and the runtime's run on two cores at once. What reads a command buffer's state back -- a wait, its status, the // in-flight use of a resource -- first lets the queue catch up (encodeDrain). Off, each call runs at once on the caller's thread. static const bool gEncodeThread=access("/private/var/ngpu/debug/encode-thread",F_OK)==0; -static char gEncodeKey; -static dispatch_queue_t encodeQueue() { - static dispatch_queue_t queue=[]{ - dispatch_queue_t q=dispatch_queue_create("native-metal.encode", - dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL,QOS_CLASS_USER_INTERACTIVE,0)); - dispatch_queue_set_specific(q,&gEncodeKey,&gEncodeKey,nullptr); - return q; +// One thread of its own runs the work, in batches a calling thread hands over at 64 calls, at an encoder's end, at a commit and before a +// wait: a dispatch queue took a call at a time and woke one of the pool's threads for each, a cost above a cheap call's own (GFXBench's +// Driver Overhead 2 drew 16% fewer frames on 60% more CPU a frame), and spread the work over half a dozen threads. +struct EncodeThread { + std::mutex lock; + std::condition_variable work,idle; + std::deque>> batches; + uint64_t queued=0,done=0; +}; +static thread_local bool tOnEncodeThread; +static thread_local std::vector tBatch; +static EncodeThread &encodeThread() { + static EncodeThread *thread=[]{ + EncodeThread *t=new EncodeThread; + pthread_t id;pthread_attr_t attr;pthread_attr_init(&attr); + pthread_attr_set_qos_class_np(&attr,QOS_CLASS_USER_INTERACTIVE,0); + pthread_create(&id,&attr,[](void *arg)->void * { + EncodeThread &t=*(EncodeThread *)arg;tOnEncodeThread=true; + pthread_setname_np("native-metal-encode"); + std::unique_lock lock(t.lock); + for(;;) { + t.work.wait(lock,[&]{return !t.batches.empty();}); + auto batch=std::move(t.batches.front());t.batches.pop_front(); + lock.unlock(); + @autoreleasepool {for(dispatch_block_t block:*batch)block();} + batch.reset(); + lock.lock();t.done++;t.idle.notify_all(); + } + return nullptr; + },t); + pthread_attr_destroy(&attr); + return t; }(); - return queue; + return *thread; +} +static bool onEncodeQueue() {return tOnEncodeThread;} +static void encodeFlush() { + if(tBatch.empty())return; + auto batch=std::make_shared>();batch->swap(tBatch); + EncodeThread &t=encodeThread(); + {std::lock_guard lock(t.lock);t.batches.push_back(std::move(batch));t.queued++;} + t.work.notify_one(); } -static bool onEncodeQueue() {return dispatch_get_specific(&gEncodeKey)!=nullptr;} // A block takes what it uses by value (C arrays as std::array) or strongly (the objects whose handles it passes), as the call made it. static void encodeLater(dispatch_block_t work) { - if(!gEncodeThread || onEncodeQueue())work();else dispatch_async(encodeQueue(),work); + if(!gEncodeThread || onEncodeQueue()){work();return;} + tBatch.push_back(work); + if(tBatch.size()>=64)encodeFlush(); } static void encodeDrain() { - if(gEncodeThread && !onEncodeQueue())dispatch_sync(encodeQueue(),^{}); + if(!gEncodeThread || onEncodeQueue())return; + encodeFlush(); + EncodeThread &t=encodeThread(); + std::unique_lock lock(t.lock); + const uint64_t target=t.queued; + t.idle.wait(lock,[&]{return t.done>=target;}); } #define ENCODE(...) encodeLater(^{__VA_ARGS__;}) static void waitForResource(id resource) { @@ -1491,6 +1533,7 @@ static void waitForResource(id resource) { ENCODE(NmbEncoder *handle=self->_handle;self->_handle=nullptr; if(!handle){[self->_commandBuffer refuse:@"endEncoding twice"];return;} [self check:nmb_encoder_end(handle) what:"endEncoding"]); + encodeFlush(); } - (void)insertDebugSignpost:(NSString *)s {(void)s;} - (void)pushDebugGroup:(NSString *)s {(void)s;} @@ -2332,6 +2375,7 @@ static void dumpReadback(NBCommandBuffer *source,uint64_t appIndex) { dispatch_group_enter(_schedulingHandlers); @synchronized(gInFlight){[gInFlight addObject:self];} ENCODE([self submit]); + encodeFlush(); } // The runtime's half of a commit, on the encoding thread after the command buffer's encoders. - (void)submit { From 0dd1516600e1c0adc22494860d58a18988834940 Mon Sep 17 00:00:00 2001 From: HuJK Date: Tue, 27 Oct 2026 19:23:02 +0800 Subject: [PATCH 51/92] The encoding thread on by default in the render server Measured in one boot with the switch: a home-screen drag ran 10% more frames (about 61 -> 67 fps) on as many cycles a frame (backboardd 34-39 vs 35-40 Mcycles), its display thread at 34% instead of 90%; GFXBench's Driver Overhead 2, all runtime work on one thread, drew 12% fewer frames on 40% more CPU a frame. So the render server encodes on the thread by default and other processes do not; /private/var/ngpu/debug/encode-thread still puts every process on one, and no-encode-thread turns it off everywhere. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012Camiwa7VsPwkhfyscKQ6S --- NativeBridgeMetalFrontend.mm | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 0abf6c3..c5e01ea 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -1438,13 +1438,17 @@ static void dumpNote(NBCommandBuffer *commandBuffer,NSMutableArray *into,id text if(seen[@"texture"]==texture && [seen[@"level"] unsignedIntegerValue]==level && [seen[@"slice"] unsignedIntegerValue]==slice)return; [into addObject:[NSMutableDictionary dictionaryWithDictionary:@{@"texture":texture,@"level":@(level),@"slice":@(slice),@"where":where}]]; } -// The encoding thread, while new a diagnostic switch read when the process starts (/private/var/ngpu/debug/encode-thread): the runtime's -// half of each encoder call -- binding, draws, the end of a pass, an encoder's making and a command buffer's submission -- runs on a -// serial queue of the process's own, in the order the calls were made, and the caller goes on once it has recorded the call. The render -// server's display thread spent most of a frame in the runtime (a home-screen drag: about 60% of a thread busy 90% of the time); split, -// its own encoding and the runtime's run on two cores at once. What reads a command buffer's state back -- a wait, its status, the -// in-flight use of a resource -- first lets the queue catch up (encodeDrain). Off, each call runs at once on the caller's thread. -static const bool gEncodeThread=access("/private/var/ngpu/debug/encode-thread",F_OK)==0; +// The encoding thread, in the render server: the runtime's half of each encoder call -- binding, draws, the end of a pass, an encoder's +// making and a command buffer's submission -- runs on a thread of the process's own, in the order the calls were made, and the caller +// goes on once it has recorded the call. The render server's display thread spent most of a frame in the runtime (a home-screen drag: +// about 60% of a thread busy 90% of the time); split, CoreAnimation's own encoding and the runtime's run on two cores at once (the +// display thread at 34%, the drag 10% faster on as many cycles a frame). What reads a command buffer's state back -- a wait, its status, +// the in-flight use of a resource -- first lets the thread catch up (encodeDrain). Other processes keep each call on the caller's +// thread: where the runtime is nearly all a thread's work the split only adds its cost (GFXBench's Driver Overhead 2: 12% fewer frames +// on 40% more CPU a frame). Switches read when a process starts: /private/var/ngpu/debug/encode-thread puts every process on one, +// no-encode-thread none. +static const bool gEncodeThread=access("/private/var/ngpu/debug/no-encode-thread",F_OK)!=0 && + (isRenderServer() || access("/private/var/ngpu/debug/encode-thread",F_OK)==0); // One thread of its own runs the work, in batches a calling thread hands over at 64 calls, at an encoder's end, at a commit and before a // wait: a dispatch queue took a call at a time and woke one of the pool's threads for each, a cost above a cheap call's own (GFXBench's // Driver Overhead 2 drew 16% fewer frames on 60% more CPU a frame), and spread the work over half a dozen threads. From c363547ad2b7061b462e7a138983c9ae73df7630 Mon Sep 17 00:00:00 2001 From: HuJK Date: Tue, 27 Oct 2026 19:51:04 +0800 Subject: [PATCH 52/92] Bridge: mimalloc as the global allocator The runtime allocates and frees on every encoder call; the system allocator's frees from the encoding thread also returned pages to the kernel (madvise). Every allocation of the bridge and the runtime is freed on the Rust side (handles are Box::into_raw and Box::from_raw), so the allocator is the Rust side's alone. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012Camiwa7VsPwkhfyscKQ6S --- bridge/NativeMetalBridge.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/bridge/NativeMetalBridge.rs b/bridge/NativeMetalBridge.rs index 40a753f..4c8d698 100644 --- a/bridge/NativeMetalBridge.rs +++ b/bridge/NativeMetalBridge.rs @@ -11,6 +11,12 @@ #[path = "NativeMetalObjects.rs"] pub mod objects; +/// The runtime allocates and frees on every encoder call (binding tables, recorded calls, tracked uses); mimalloc's thread-local free +/// lists take those small blocks for less than the system allocator, whose frees from the encoding thread also gave pages back to the +/// kernel (madvise) as they emptied. Every allocation of the bridge and the runtime is the Rust side's own, freed there. +#[global_allocator] +static ALLOCATOR: mimalloc::MiMalloc = mimalloc::MiMalloc; + use metal2vulkan::metal::{PixelFormat, PrimitiveType, TextureUsage}; use metal2vulkan::pipeline::{ColorAttachmentDescriptor, RenderPipelineDescriptor, StageSource}; use metal2vulkan_runtime::{ From f1f51bf58e9aa3f441fae73cbb282337b99d8ee0 Mon Sep 17 00:00:00 2001 From: HuJK Date: Tue, 27 Oct 2026 21:17:10 +0800 Subject: [PATCH 53/92] Encoding thread: calls as closures in reused batch memory, not blocks Each recorded encoder call was a block: a heap copy on the caller, the block runtime, and a free on the encoding thread when the batch was released (6% of the thread's busy time in an App Library swipe). A call is now a C++ closure moved into its batch's own chunks and run and destroyed in place; batches go back to the calling side to be refilled. A closure retains what it captures as the block did (self included); encoder making copies the caller's block literal, which the thread calls later. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012Camiwa7VsPwkhfyscKQ6S --- NativeBridgeMetalFrontend.mm | 73 +++++++++++++++++++++++++++++------- 1 file changed, 59 insertions(+), 14 deletions(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index c5e01ea..c022244 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -1452,14 +1452,55 @@ static const bool gEncodeThread=access("/private/var/ngpu/debug/no-encode-thread // One thread of its own runs the work, in batches a calling thread hands over at 64 calls, at an encoder's end, at a commit and before a // wait: a dispatch queue took a call at a time and woke one of the pool's threads for each, a cost above a cheap call's own (GFXBench's // Driver Overhead 2 drew 16% fewer frames on 60% more CPU a frame), and spread the work over half a dozen threads. +// A call is a C++ closure (it holds what it uses as the call made it; an object strongly, `self` too) moved into its batch's own memory +// and run and destroyed there: a block per call cost a heap copy, the block runtime and a free on the other thread (6% of the thread's +// busy time in an App Library swipe went to releasing a batch's blocks). Batches go back to the calling side to be filled again. +class EncodeBatch { + struct Chunk {std::unique_ptr bytes;size_t capacity=0,used=0;}; + struct Header {void (*run)(void *closure);size_t size;}; + static constexpr size_t kAlign=16,kChunk=16384; + static constexpr size_t rounded(size_t n) {return (n+kAlign-1)&~(kAlign-1);} + std::vector chunks; + size_t at=0; +public: + size_t count=0; + template void add(F &&work) { + using Closure=std::decay_t; + static_assert(alignof(Closure)<=kAlign,"an encoder call's closure is aligned beyond its batch's slots"); + const size_t need=rounded(sizeof(Header))+rounded(sizeof(Closure)); + while(atchunks[at].capacity)at++; + if(at==chunks.size()) { + static_assert(kAlign<=__STDCPP_DEFAULT_NEW_ALIGNMENT__,"operator new aligns a chunk for its slots"); + Chunk chunk;chunk.capacity=std::max(kChunk,need);chunk.bytes.reset(new uint8_t[chunk.capacity]); + chunks.push_back(std::move(chunk)); + } + Chunk &chunk=chunks[at]; + uint8_t *slot=chunk.bytes.get()+chunk.used;chunk.used+=need;count++; + new(slot) Header{[](void *closure){Closure &call=*static_cast(closure);call();call.~Closure();},need}; + new(slot+rounded(sizeof(Header))) Closure(std::forward(work)); + } + // Each call in the order added, each destroyed once it ran; the batch is empty after. + void run() { + for(Chunk &chunk:chunks) { + for(size_t offset=0;offset(chunk.bytes.get()+offset); + header.run(chunk.bytes.get()+offset+rounded(sizeof(Header))); + offset+=header.size; + } + chunk.used=0; + } + at=0;count=0; + } +}; struct EncodeThread { std::mutex lock; std::condition_variable work,idle; - std::deque>> batches; + std::deque batches; + std::vector spare; uint64_t queued=0,done=0; }; static thread_local bool tOnEncodeThread; -static thread_local std::vector tBatch; +static thread_local EncodeBatch *tBatch; static EncodeThread &encodeThread() { static EncodeThread *thread=[]{ EncodeThread *t=new EncodeThread; @@ -1471,11 +1512,10 @@ static EncodeThread &encodeThread() { std::unique_lock lock(t.lock); for(;;) { t.work.wait(lock,[&]{return !t.batches.empty();}); - auto batch=std::move(t.batches.front());t.batches.pop_front(); + EncodeBatch *batch=t.batches.front();t.batches.pop_front(); lock.unlock(); - @autoreleasepool {for(dispatch_block_t block:*batch)block();} - batch.reset(); - lock.lock();t.done++;t.idle.notify_all(); + @autoreleasepool {batch->run();} + lock.lock();t.spare.push_back(batch);t.done++;t.idle.notify_all(); } return nullptr; },t); @@ -1486,17 +1526,21 @@ static EncodeThread &encodeThread() { } static bool onEncodeQueue() {return tOnEncodeThread;} static void encodeFlush() { - if(tBatch.empty())return; - auto batch=std::make_shared>();batch->swap(tBatch); + if(!tBatch || !tBatch->count)return; EncodeThread &t=encodeThread(); - {std::lock_guard lock(t.lock);t.batches.push_back(std::move(batch));t.queued++;} + {std::lock_guard lock(t.lock);t.batches.push_back(tBatch);t.queued++;tBatch=nullptr;} t.work.notify_one(); } -// A block takes what it uses by value (C arrays as std::array) or strongly (the objects whose handles it passes), as the call made it. -static void encodeLater(dispatch_block_t work) { +// A closure takes what it uses by value (C arrays as std::array) or strongly (the objects whose handles it passes), as the call made it. +template static void encodeLater(F &&work) { if(!gEncodeThread || onEncodeQueue()){work();return;} - tBatch.push_back(work); - if(tBatch.size()>=64)encodeFlush(); + if(!tBatch) { + EncodeThread &t=encodeThread(); + {std::lock_guard lock(t.lock);if(!t.spare.empty()){tBatch=t.spare.back();t.spare.pop_back();}} + if(!tBatch)tBatch=new EncodeBatch; + } + tBatch->add(std::forward(work)); + if(tBatch->count>=64)encodeFlush(); } static void encodeDrain() { if(!gEncodeThread || onEncodeQueue())return; @@ -1506,7 +1550,7 @@ static void encodeDrain() { const uint64_t target=t.queued; t.idle.wait(lock,[&]{return t.done>=target;}); } -#define ENCODE(...) encodeLater(^{__VA_ARGS__;}) +#define ENCODE(...) encodeLater([=]{__VA_ARGS__;}) static void waitForResource(id resource) { encodeDrain(); // every committed command buffer's uses recorded const uint64_t began=nowUs(); @@ -2199,6 +2243,7 @@ static void dumpReadback(NBCommandBuffer *source,uint64_t appIndex) { - (id)encoder:(NBEncoder *)encoder make:(NmbEncoder *(^)(void))make what:(NSString *)what { if(self.committed){[self refuse:[what stringByAppendingString:@" after commit"]];return nil;} encoder.commandBuffer=self; + make=[make copy]; // the caller's block literal is on its stack; the encoding thread calls it later ENCODE(NmbEncoder *handle=make(); if(!handle)[self refuse:[NSString stringWithFormat:@"%@: %@",what,lastError()]]; encoder.handle=handle); From fa52ed5e24251ab6829c228ade57acfdf7e7b194 Mon Sep 17 00:00:00 2001 From: HuJK Date: Tue, 27 Oct 2026 21:24:52 +0800 Subject: [PATCH 54/92] Draw-state tally behind /private/var/ngpu/debug/draw-stats For each draw, which kinds of its encoder's state changed since the draw before it (a setter binding what is already bound changes nothing), tallied per process into /private/var/tmp/ngpu-draw-stats-.txt every 2000 draws: how often a draw binds what the last draw bound, which a runtime keeping the last draw's bind work could skip. Off, it costs a branch a setter. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012Camiwa7VsPwkhfyscKQ6S --- NativeBridgeMetalFrontend.mm | 71 +++++++++++++++++++++++++++++++----- 1 file changed, 62 insertions(+), 9 deletions(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index c022244..e18a370 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -1372,6 +1372,8 @@ static void dumpSurfacesOnce(NSSet *textures,NSSet *owners) { @property(nonatomic) uint32_t dumpDraws; @property(nonatomic,strong) NSMutableArray *dumpRuns; @property(nonatomic) uint32_t drawIndex; // this encoder's draws so far, for NATIVE_METAL_SKIP_DRAWS // [pipeline info, depth info, first draw, count] for each run of draws with one state +- (void)noteState:(uint32_t)kind key:(uint32_t)key object:(const void *)object value:(uint64_t)value; +- (void)noteDraw; @end // NATIVE_METAL_DUMP_CB=FIRST[:COUNT]: the app's command buffers FIRST..FIRST+COUNT-1 (counted from 1, the plugin's own readbacks left // out) have what they rendered read back after them, by a command buffer of the plugin's own on the same queue: every render pass's @@ -1566,8 +1568,50 @@ static void waitForResource(id resource) { } gTimeResourceWait.add(began); } -@implementation NBEncoder +// /private/var/ngpu/debug/draw-stats (read when a process starts): for each draw, which kinds of an encoder's state changed since the draw +// before it in the encoder -- a setter that binds what is already bound changes nothing -- tallied per process into +// /private/var/tmp/ngpu-draw-stats-.txt every 2000 draws: how often a draw binds what the draw before it bound, which a runtime +// that kept the last draw's bind work could skip. +static const bool gDrawStats=access("/private/var/ngpu/debug/draw-stats",F_OK)==0; +enum : uint32_t {kDSPipeline=1,kDSBuffer=2,kDSOffset=4,kDSBytes=8,kDSTexture=16,kDSSampler=32,kDSDepth=64,kDSViewport=128,kDSOther=256,kDSFirst=512}; +struct DrawStateNote {std::unordered_map> bound;uint32_t changed=kDSFirst;}; +static std::atomic gDSMasks[1024],gDSDraws,gDSRebinds; +static uint64_t hashBytes(const void *bytes,size_t length) {return bytes?hashSource((const uint8_t *)bytes,length)^length:0;} +static void writeDrawStats() { + static std::mutex lock;std::lock_guard guard(lock); + std::vector> masks; + for(uint32_t m=0;m<1024;m++)if(uint64_t n=gDSMasks[m].load())masks.push_back({n,m}); + std::sort(masks.rbegin(),masks.rend()); + char path[160];snprintf(path,sizeof path,"/private/var/tmp/ngpu-draw-stats-%s.txt",getprogname()); + FILE *f=fopen(path,"w");if(!f)return; + const uint64_t draws=gDSDraws.load(); + fprintf(f,"draws %llu, setters binding what was bound %llu\n",(unsigned long long)draws,(unsigned long long)gDSRebinds.load()); + static const char *const kinds[]={"pipeline","buffer","offset","bytes","texture","sampler","depth","viewport","other","first"}; + for(auto &[n,m]:masks) { + std::string what;for(int k=0;k<10;k++)if(m&(1u< _drawNote; +} - (void)dealloc {if(_handle)nmb_encoder_discard(_handle);} +// A setter of `kind` at `key` (UINT32_MAX: one not compared) binding `object` with `value`, for the draw-stats tally. +- (void)noteState:(uint32_t)kind key:(uint32_t)key object:(const void *)object value:(uint64_t)value { + if(!_drawNote)_drawNote.reset(new DrawStateNote); + if(key!=UINT32_MAX) { + auto &slot=_drawNote->bound[kind<<16|key]; + if(slot.first==(uintptr_t)object && slot.second==value){gDSRebinds++;return;} + slot={(uintptr_t)object,value}; + } + _drawNote->changed|=kind; +} +- (void)noteDraw { + if(!_drawNote)_drawNote.reset(new DrawStateNote); + gDSMasks[_drawNote->changed&1023]++;_drawNote->changed=0; + if(++gDSDraws%2000==0)writeDrawStats(); +} - (bool)check:(bool)ok what:(const char *)what { if(!ok)[_commandBuffer refuse:[NSString stringWithFormat:@"%s: %@",what,lastError()]]; return ok; @@ -1640,11 +1684,13 @@ static void waitForResource(id resource) { - (void)buffer:(id)buffer offset:(NSUInteger)offset index:(NSUInteger)index stage:(uint32_t)stage { if(buffer && ![(id)buffer isKindOfClass:[NBBuffer class]]){[_commandBuffer refuse:@"foreign buffer"];return;} if(buffer)[_commandBuffer.resourceOwners addObject:buffer]; + if(gDrawStats)[self noteState:kDSBuffer key:stage<<8|(uint32_t)index object:(__bridge const void *)buffer value:offset]; if(dumpMaybe(_commandBuffer))[self dumpBind:buffer?@[buffer,@(offset)]:nil index:index stage:stage]; ENCODE([self check:nmb_encoder_set_buffer(self->_handle,stage,buffer?[(NBBuffer *)buffer handle]:nullptr,offset,(uint32_t)index) what:"setBuffer"]); } - (void)texture:(id)texture index:(NSUInteger)index stage:(uint32_t)stage { if(texture && ![(id)texture isKindOfClass:[NBTexture class]]){[_commandBuffer refuse:@"foreign texture"];return;} + if(gDrawStats)[self noteState:kDSTexture key:stage<<8|(uint32_t)index object:(__bridge const void *)texture value:0]; if(texture && stage==0 && (texture.usage&MTLTextureUsageShaderWrite) && dumpMaybe(_commandBuffer)) { if(!_dumpEntries)_dumpEntries=[NSMutableArray array]; if(!_commandBuffer.dumpTargets)_commandBuffer.dumpTargets=[NSMutableArray array]; @@ -1662,6 +1708,7 @@ static void waitForResource(id resource) { - (void)sampler:(id)sampler index:(NSUInteger)index stage:(uint32_t)stage { if(sampler && ![(id)sampler isKindOfClass:[NBSampler class]]){[_commandBuffer refuse:@"foreign sampler"];return;} [self dumpBind:sampler index:index stage:stage+8]; + if(gDrawStats)[self noteState:kDSSampler key:stage<<8|(uint32_t)index object:(__bridge const void *)sampler value:0]; ENCODE([self check:nmb_encoder_set_sampler(self->_handle,stage,sampler?[(NBSampler *)sampler handle]:nullptr,(uint32_t)index) what:"setSamplerState"]); } - (void)sampler:(id)sampler lodMin:(float)lodMin max:(float)lodMax index:(NSUInteger)index stage:(uint32_t)stage { @@ -1672,6 +1719,7 @@ static void waitForResource(id resource) { } - (void)bytes:(const void *)bytes length:(NSUInteger)length index:(NSUInteger)index stage:(uint32_t)stage { if(dumpMaybe(_commandBuffer))[self dumpBind:[NSData dataWithBytes:bytes length:length] index:index stage:stage]; + if(gDrawStats)[self noteState:kDSBytes key:stage<<8|(uint32_t)index object:nullptr value:hashBytes(bytes,length)]; if(!gEncodeThread){[self check:nmb_encoder_set_bytes(_handle,stage,bytes,length,(uint32_t)index) what:"setBytes"];return;} const std::vector copy((const uint8_t *)bytes,(const uint8_t *)bytes+(bytes?length:0)); // the caller's bytes are its own again ENCODE([self check:nmb_encoder_set_bytes(self->_handle,stage,copy.empty()?nullptr:copy.data(),length,(uint32_t)index) what:"setBytes"]); @@ -1686,6 +1734,7 @@ static void waitForResource(id resource) { - (void)waitForFence:(id)fence {[self fence:fence stages:0 update:false];} - (void)offset:(NSUInteger)offset index:(NSUInteger)index stage:(uint32_t)stage { id bound=_dumpBound[@(stage<<8|(uint32_t)index)]; + if(gDrawStats)[self noteState:kDSOffset key:stage<<8|(uint32_t)index object:nullptr value:offset]; if([bound isKindOfClass:[NSArray class]])[self dumpBind:@[bound[0],@(offset)] index:index stage:stage]; ENCODE([self check:nmb_encoder_set_buffer_offset(self->_handle,stage,offset,(uint32_t)index) what:"setBufferOffset"]); } @@ -1744,6 +1793,7 @@ static void waitForResource(id resource) { - (void)setRenderPipelineState:(id)state { if(![(id)state isKindOfClass:[NBRenderPipeline class]]){[self.commandBuffer refuse:@"foreign render pipeline"];return;} if(dumpRange().first)self.dumpPipeline=state; + if(gDrawStats)[self noteState:kDSPipeline key:0 object:(__bridge const void *)state value:0]; ENCODE([self check:nmb_render_set_pipeline(self.handle,[(NBRenderPipeline *)state handle]) what:"setRenderPipelineState"]); } - (void)setVertexBuffer:(id)b offset:(NSUInteger)o atIndex:(NSUInteger)i {[self buffer:b offset:o index:i stage:1];} @@ -1782,18 +1832,21 @@ static void waitForResource(id resource) { - (void)setDepthStencilState:(id)state { if(state && ![(id)state isKindOfClass:[NBDepthStencil class]]){[self.commandBuffer refuse:@"foreign depth/stencil state"];return;} if(dumpRange().first)self.dumpDepth=state; + if(gDrawStats)[self noteState:kDSDepth key:0 object:(__bridge const void *)state value:0]; ENCODE([self check:nmb_render_set_depth_stencil(self.handle,state?[(NBDepthStencil *)state handle]:nullptr) what:"setDepthStencilState"]); } - (void)setViewport:(MTLViewport)v { const std::array values={v.originX,v.originY,v.width,v.height,v.znear,v.zfar}; if(dumpMaybe(self.commandBuffer) && dumpShown(self.dumpDraws+1))dumpLine(self.commandBuffer,[NSString stringWithFormat:@"pass %u viewport %g,%g %gx%g depth %g..%g",self.commandBuffer.dumpPasses, v.originX,v.originY,v.width,v.height,v.znear,v.zfar]); + if(gDrawStats)[self noteState:kDSViewport key:0 object:nullptr value:hashBytes(values.data(),sizeof values)]; ENCODE([self check:nmb_render_set_viewport(self.handle,values.data()) what:"setViewport"]); } - (void)setScissorRect:(MTLScissorRect)r { const std::array values={r.x,r.y,r.width,r.height}; if(dumpMaybe(self.commandBuffer) && dumpShown(self.dumpDraws+1))dumpLine(self.commandBuffer,[NSString stringWithFormat:@"pass %u scissor %lu,%lu %lux%lu",self.commandBuffer.dumpPasses, (unsigned long)r.x,(unsigned long)r.y,(unsigned long)r.width,(unsigned long)r.height]); + if(gDrawStats)[self noteState:kDSViewport key:1 object:nullptr value:hashBytes(values.data(),sizeof values)]; ENCODE([self check:nmb_render_set_scissor(self.handle,values.data()) what:"setScissorRect"]); } // The runtime takes cull mode, winding and fill mode together; this encoder keeps Metal's defaults until each is set. @@ -1801,13 +1854,13 @@ static void waitForResource(id resource) { NSNumber *cull=objc_getAssociatedObject(self,"cull"),*winding=objc_getAssociatedObject(self,"winding"),*fill=objc_getAssociatedObject(self,"fill"); if(dumpMaybe(self.commandBuffer) && dumpShown(self.dumpDraws+1))dumpLine(self.commandBuffer,[NSString stringWithFormat:@"pass %u cull %llu winding %llu fill %llu",self.commandBuffer.dumpPasses, cull.unsignedLongLongValue,winding.unsignedLongLongValue,fill.unsignedLongLongValue]); - ENCODE([self check:nmb_render_set_raster(self.handle,cull.unsignedLongLongValue,winding.unsignedLongLongValue,fill.unsignedLongLongValue) what:"raster state"]); + if(gDrawStats)[self noteState:kDSOther key:UINT32_MAX object:nullptr value:0];ENCODE([self check:nmb_render_set_raster(self.handle,cull.unsignedLongLongValue,winding.unsignedLongLongValue,fill.unsignedLongLongValue) what:"raster state"]); } - (void)setCullMode:(MTLCullMode)m {objc_setAssociatedObject(self,"cull",@(m),OBJC_ASSOCIATION_RETAIN);[self raster];} - (void)setFrontFacingWinding:(MTLWinding)w {objc_setAssociatedObject(self,"winding",@(w),OBJC_ASSOCIATION_RETAIN);[self raster];} - (void)setTriangleFillMode:(MTLTriangleFillMode)f {objc_setAssociatedObject(self,"fill",@(f),OBJC_ASSOCIATION_RETAIN);[self raster];} - (void)setDepthBias:(float)bias slopeScale:(float)slope clamp:(float)clamp { - ENCODE([self check:nmb_render_set_depth_bias(self.handle,bias,slope,clamp) what:"setDepthBias"]); + if(gDrawStats)[self noteState:kDSOther key:UINT32_MAX object:nullptr value:0];ENCODE([self check:nmb_render_set_depth_bias(self.handle,bias,slope,clamp) what:"setDepthBias"]); } - (void)setLineWidth:(float)width {ENCODE([self check:nmb_render_set_line_width(self.handle,width) what:"setLineWidth"]);} - (void)setVisibilityResultMode:(MTLVisibilityResultMode)mode offset:(NSUInteger)offset { @@ -1824,12 +1877,12 @@ static void waitForResource(id resource) { - (void)setTileBytes:(const void *)p length:(NSUInteger)n atIndex:(NSUInteger)i {(void)p;(void)n;(void)i;[self.commandBuffer refuse:@"tile shaders are not supported"];} - (void)dispatchThreadsPerTile:(MTLSize)size {(void)size;[self.commandBuffer refuse:@"tile shaders are not supported"];} - (void)dispatchThreadsPerTile:(MTLSize)size inRegion:(MTLRegion)region {(void)size;(void)region;[self.commandBuffer refuse:@"tile shaders are not supported"];} -- (void)setStencilReferenceValue:(uint32_t)v {ENCODE([self check:nmb_render_set_stencil_reference(self.handle,v,v) what:"setStencilReferenceValue"]);} +- (void)setStencilReferenceValue:(uint32_t)v {if(gDrawStats)[self noteState:kDSOther key:UINT32_MAX object:nullptr value:0];ENCODE([self check:nmb_render_set_stencil_reference(self.handle,v,v) what:"setStencilReferenceValue"]);} - (void)setStencilFrontReferenceValue:(uint32_t)f backReferenceValue:(uint32_t)b { - ENCODE([self check:nmb_render_set_stencil_reference(self.handle,f,b) what:"setStencilReferenceValues"]); + if(gDrawStats)[self noteState:kDSOther key:UINT32_MAX object:nullptr value:0];ENCODE([self check:nmb_render_set_stencil_reference(self.handle,f,b) what:"setStencilReferenceValues"]); } - (void)setBlendColorRed:(float)r green:(float)g blue:(float)b alpha:(float)a { - const std::array c={r,g,b,a};ENCODE([self check:nmb_render_set_blend_color(self.handle,c.data()) what:"setBlendColor"]); + const std::array c={r,g,b,a};if(gDrawStats)[self noteState:kDSOther key:UINT32_MAX object:nullptr value:0];ENCODE([self check:nmb_render_set_blend_color(self.handle,c.data()) what:"setBlendColor"]); } // NATIVE_METAL_SKIP_DRAWS=PASS:FIRST:LAST: every command buffer's render pass PASS (counted from 1) leaves out its draws FIRST..LAST // (counted from 1), to find which draws make what a frame shows. @@ -1845,7 +1898,7 @@ static void waitForResource(id resource) { } - (void)drawPrimitives:(MTLPrimitiveType)t vertexStart:(NSUInteger)s vertexCount:(NSUInteger)n instanceCount:(NSUInteger)i baseInstance:(NSUInteger)b { if([self skipDraw])return; - gDraws++;self.commandBuffer->encodedWork.draws++; + gDraws++;self.commandBuffer->encodedWork.draws++;if(gDrawStats)[self noteDraw]; if(dumpMaybe(self.commandBuffer))[self dumpDraw:[NSString stringWithFormat:@"prim %lu start %lu count %lu instances %lu base %lu",(unsigned long)t,(unsigned long)s,(unsigned long)n, (unsigned long)i,(unsigned long)b]]; ENCODE([self check:nmb_render_draw(self.handle,t,(uint32_t)s,(uint32_t)n,(uint32_t)i,(uint32_t)b) what:"drawPrimitives"]); @@ -1861,7 +1914,7 @@ static void waitForResource(id resource) { if(![(id)b isKindOfClass:[NBBuffer class]]){[self.commandBuffer refuse:@"foreign index buffer"];return;} [self.commandBuffer.resourceOwners addObject:b]; if([self skipDraw])return; - gDraws++;self.commandBuffer->encodedWork.draws++; + gDraws++;self.commandBuffer->encodedWork.draws++;if(gDrawStats)[self noteDraw]; if(dumpMaybe(self.commandBuffer) && dumpShown(self.dumpDraws+1)) { const uint8_t *indices=(const uint8_t *)b.contents;const unsigned size=k==MTLIndexTypeUInt16?2:4; NSMutableString *first=[NSMutableString string]; @@ -1899,7 +1952,7 @@ static void waitForResource(id resource) { points:(id)ci offset:(NSUInteger)co instances:(NSUInteger)instances base:(NSUInteger)base indirect:(id)ib offset:(NSUInteger)io { if(![self own:pi what:@"patch index buffer"] || ![self own:ci what:@"control point index buffer"] || ![self own:ib what:@"indirect buffer"])return; if([self skipDraw])return; - gDraws++;self.commandBuffer->encodedWork.draws++; + gDraws++;self.commandBuffer->encodedWork.draws++;if(gDrawStats)[self noteDraw]; if(dumpMaybe(self.commandBuffer))[self dumpDraw:[NSString stringWithFormat:@"patches %lu start %lu count %lu instances %lu base %lu%@",(unsigned long)points,(unsigned long)start, (unsigned long)count,(unsigned long)instances,(unsigned long)base,ib?@" indirect":@""]]; ENCODE([self check:nmb_render_draw_patches(self.handle,(uint32_t)points,(uint32_t)start,(uint32_t)count,pi?[(NBBuffer *)pi handle]:nullptr,po, From f9a4b0c2c120a37cc06c0ac62edb6f48cbd0159a Mon Sep 17 00:00:00 2001 From: HuJK Date: Tue, 27 Oct 2026 21:45:26 +0800 Subject: [PATCH 55/92] Draw-state tally: write into the process's temporary directory The render server's sandbox refuses file creation in /private/var/tmp (dmesg: deny file-write-create). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012Camiwa7VsPwkhfyscKQ6S --- NativeBridgeMetalFrontend.mm | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index e18a370..19e3a88 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -1570,8 +1570,8 @@ static void waitForResource(id resource) { } // /private/var/ngpu/debug/draw-stats (read when a process starts): for each draw, which kinds of an encoder's state changed since the draw // before it in the encoder -- a setter that binds what is already bound changes nothing -- tallied per process into -// /private/var/tmp/ngpu-draw-stats-.txt every 2000 draws: how often a draw binds what the draw before it bound, which a runtime -// that kept the last draw's bind work could skip. +// ngpu-draw-stats-.txt in its temporary directory every 2000 draws (the render server's sandbox refuses /private/var/tmp): how +// often a draw binds what the draw before it bound, which a runtime that kept the last draw's bind work could skip. static const bool gDrawStats=access("/private/var/ngpu/debug/draw-stats",F_OK)==0; enum : uint32_t {kDSPipeline=1,kDSBuffer=2,kDSOffset=4,kDSBytes=8,kDSTexture=16,kDSSampler=32,kDSDepth=64,kDSViewport=128,kDSOther=256,kDSFirst=512}; struct DrawStateNote {std::unordered_map> bound;uint32_t changed=kDSFirst;}; @@ -1582,8 +1582,8 @@ static void writeDrawStats() { std::vector> masks; for(uint32_t m=0;m<1024;m++)if(uint64_t n=gDSMasks[m].load())masks.push_back({n,m}); std::sort(masks.rbegin(),masks.rend()); - char path[160];snprintf(path,sizeof path,"/private/var/tmp/ngpu-draw-stats-%s.txt",getprogname()); - FILE *f=fopen(path,"w");if(!f)return; + NSString *path=[NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"ngpu-draw-stats-%s.txt",getprogname()]]; + FILE *f=fopen(path.fileSystemRepresentation,"w");if(!f)return; const uint64_t draws=gDSDraws.load(); fprintf(f,"draws %llu, setters binding what was bound %llu\n",(unsigned long long)draws,(unsigned long long)gDSRebinds.load()); static const char *const kinds[]={"pipeline","buffer","offset","bytes","texture","sampler","depth","viewport","other","first"}; From aff537e6914a550d2d43ad2324a8aa143a61b92d Mon Sep 17 00:00:00 2001 From: rpone Date: Wed, 28 Oct 2026 09:19:23 +0800 Subject: [PATCH 56/92] 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. --- NativeAdrenoMetalPlugin.mm | 16 + NativeBridgeMetalFrontend.mm | 277 ++++++++++++++++-- bridge/NativeMetalObjects.h | 2 + bridge/NativeMetalObjects.rs | 23 ++ ...0017-optional-shader-cache-directory.patch | 45 +++ ...b-extended-range-blend-rounded-model.patch | 38 +++ bridge/patches/0019-llvm-bitreverse.patch | 42 +++ .../patches/0020-growing-memory-blocks.patch | 116 ++++++++ .../0021-patch-with-no-control-points.patch | 18 ++ 9 files changed, 549 insertions(+), 28 deletions(-) create mode 100644 bridge/patches/0017-optional-shader-cache-directory.patch create mode 100644 bridge/patches/0018-srgb-extended-range-blend-rounded-model.patch create mode 100644 bridge/patches/0019-llvm-bitreverse.patch create mode 100644 bridge/patches/0020-growing-memory-blocks.patch create mode 100644 bridge/patches/0021-patch-with-no-control-points.patch diff --git a/NativeAdrenoMetalPlugin.mm b/NativeAdrenoMetalPlugin.mm index 4dd2535..2bf0222 100644 --- a/NativeAdrenoMetalPlugin.mm +++ b/NativeAdrenoMetalPlugin.mm @@ -185,6 +185,17 @@ static id clientDevice(unsigned port) { } if (!device && !lastError[0]) lastError = nmb_last_error(); } + if (!device && !usable) { + setenv("METAL2VULKAN_SHADER_CACHE_OPTIONAL", "1", 1); + const char *path = candidates[0][0] ? candidates[0] : candidates[1][0] ? candidates[1] : "/var/mobile/tmp/m2v"; + @try { + device = native_bridge_metal_open(turnip, path, registryID, nullptr); + } @catch (NSException *exception) { + device = nil; + lastError = "exception"; + } + if (!device && !lastError[0]) lastError = nmb_last_error(); + } if (!device) clientFailure(port, usable ? "no-device" : "no-cachedir", lastError); // The client's first problem, once, on the same text line, sent from the thread that hit it: WidgetRenderer crashed on a nil render // encoder with no reason anywhere readable (its own log lines need the plugin's format strings, which the device does not keep), and a @@ -208,6 +219,11 @@ static id deviceForPort(unsigned port) { // (int-261p, int-262, and in system memory int-265). Without it Turnip's autotune picks tile memory (GMEM) or system memory per // pass: i553 forced system memory for every pass, the GPU busy 19 % at 680 MHz. A tile store writes whole 16x4 blocks, which the // runtime keeps within a texture's rows (bridge patch 0016). + if (FILE *flags = fopen("/private/var/ngpu/tu_debug", "r")) { + char line[128] = {}; + if (fgets(line, sizeof line, flags)) {line[strcspn(line, "\n")] = 0; setenv("TU_DEBUG", line, 1);} + fclose(flags); + } setenv("TU_DEBUG", "nocb", 0); if (strcmp(getprogname(), "backboardd") != 0) return device = clientDevice(port); // The discovery releases the accelerator port when it is done; the monitor and the scanout client keep using it. diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 19e3a88..c879b9e 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -74,7 +74,7 @@ static void abortProbe(int signal, siginfo_t *si, void *context) { (void)!write(2, line, (size_t)(at - line)); at = line; const __darwin_arm_thread_state64 &ss = ((ucontext_t *)context)->uc_mcontext->__ss; for (int r = 0; r < 33; ++r) { - const uint64_t value = r < 29 ? ss.__x[r] : r == 29 ? ss.__fp : r == 30 ? ss.__lr : r == 31 ? ss.__sp : ss.__pc; + const uint64_t value = r < 29 ? ss.__x[r] : r == 29 ? __darwin_arm_thread_state64_get_fp(ss) : r == 30 ? (uint64_t)(uintptr_t)__darwin_arm_thread_state64_get_lr_fptr(ss) : r == 31 ? __darwin_arm_thread_state64_get_sp(ss) : (uint64_t)(uintptr_t)__darwin_arm_thread_state64_get_pc_fptr(ss); at = line; at = probeText(at, " r"); *at++ = char('0' + r / 10); *at++ = char('0' + r % 10); *at++ = ' '; at = probeHex(at, value & 0x0000ffffffffffffull); const uint64_t pointer = value & 0x0000ffffffffffffull; @@ -96,7 +96,7 @@ static void abortProbe(int signal, siginfo_t *si, void *context) { const volatile uintptr_t *guard = &__stack_chk_guard; for (int i = -4; i <= 4; ++i) {*at++ = ' '; at = probeHex(at, guard[i]);} *at++ = '\n'; (void)!write(2, line, (size_t)(at - line)); - uint64_t fp = ((ucontext_t *)context)->uc_mcontext->__ss.__fp; + uint64_t fp = __darwin_arm_thread_state64_get_fp(((ucontext_t *)context)->uc_mcontext->__ss); for (int frame = 0; frame < 8 && fp && !(fp & 7); ++frame) { if (!probeReadable(fp) || !probeReadable(fp - 96)) break; const volatile uint64_t *f = (const volatile uint64_t *)fp; @@ -144,7 +144,8 @@ static void (*gMissing)(const char *, const char *); // What this device has been asked and refused, for the plugin's status line (native_bridge_metal_status): commits, render/compute pipelines made, // refusals, and the last two refusals' texts. Counters only; nothing here changes a result. -static std::atomic gCommits,gCommitFailures,gPipelines,gProblems,gCompleted; +static constexpr uint64_t kWaitBoundMs=4000; +static std::atomic gCommits,gCommitFailures,gPipelines,gProblems,gCompleted,gWaitTimeouts; // What the render server actually asks of the GPU (int-141: 59 commits in 200 s, and a desktop that behaves like CPU compositing): render passes, // draws, the pixels of their first colour attachment, compute dispatches, blits and the largest pass (pixels). static std::atomic gPasses,gDraws,gPassPixels,gDispatches,gBlits,gBiggestPass,gFullScreenPasses; @@ -378,7 +379,10 @@ static void noteProblem(NSString *what) { memcpy(gLastProblems[1],gLastProblems[0],sizeof gLastProblems[0]); strlcpy(gLastProblems[0],what.UTF8String?:"?",sizeof gLastProblems[0]); } -#define NBLOG(...) do{NSString *nbText=[NSString stringWithFormat:__VA_ARGS__];NSLog(@"%@",nbText);noteProblem(nbText);}while(0) +// NMB_LOG_STDERR: also on stderr (a daemon's NSLog reaches only the unified log, which a bring-up board does not read). +static bool logToStderr() {static bool on=getenv("NMB_LOG_STDERR")!=nullptr;return on;} +#define NBLOG(...) do{NSString *nbText=[NSString stringWithFormat:__VA_ARGS__];NSLog(@"%@",nbText);noteProblem(nbText);\ + if(logToStderr())fprintf(stderr,"%s: %.140s\n",getprogname(),nbText.UTF8String?:"?");}while(0) // The render server does not get past the loss of its GPU context: CoreAnimation keeps submitting to the dead context and // the display shows the surfaces it composed last until backboardd restarts (the hang's evidence is in the host's KGSL log // and the context's loss mask, not here). End it once its completed handlers have seen the error; launchd starts it @@ -395,7 +399,7 @@ static void endRenderServerAfterGpuLoss() { // The pixel formats textures were asked for (first eight distinct), with how many were refused: native_bridge_metal_formats. static uint32_t gFormats[8];static unsigned gFormatCount;static std::atomic gTextureRefusals; // Textures over IOSurfaces: made, refused, and how many of the requests named a planar surface. -static std::atomic gSurfaceTextures,gSurfaceRefusals,gSurfacePlanar; +static std::atomic gSurfaceTextures,gSurfaceRefusals,gSurfacePlanar,gSurfaceB3a8; static std::atomic gSurfaceRotations[5]; static uint64_t textureRotation(id descriptor) { SEL sel=sel_registerName("rotation"); @@ -680,7 +684,11 @@ static void waitForResource(id resource); @property(nonatomic,strong) id parent; @property(nonatomic,strong) id parentHeap; @property(nonatomic) NSUInteger heapOffset; +@property(nonatomic) BOOL counted; // its surface counted in use (countUse) +- (void)countUse; @end +// The render server counts the surfaces it textures in use while the textures live (countUse). +static const bool gCountsSurfaceUse=!strcmp(getprogname(),"backboardd"); // A no-copy import keeps its pages until the runtime frees it, which can be after the buffer object goes (a command buffer still using // it). The owner's release (Metal's deallocator, an IOSurface's mapping) waits for that, as Metal's does: run earlier, the owner could // unmap or reuse pages the GPU still reads or writes. @@ -839,7 +847,8 @@ static bool textureDescriptor(MTLTextureDescriptor *d,NmbTextureDescriptor *out) return true; } @implementation NBTexture -- (void)dealloc {nmb_texture_release(_handle);if(_surface)CFRelease(_surface);} +- (void)dealloc {nmb_texture_release(_handle);if(_surface){if(_counted)IOSurfaceDecrementUseCount(_surface);CFRelease(_surface);}} +- (void)countUse {if(_surface && !_counted && gCountsSurfaceUse){IOSurfaceIncrementUseCount(_surface);_counted=YES;}} - (MTLTextureType)textureType {return (MTLTextureType)_descriptor.type;} - (MTLPixelFormat)pixelFormat {return (MTLPixelFormat)(_pvrtc?_pvrtc:_descriptor.format);} - (NSUInteger)width {return _descriptor.width;} @@ -871,7 +880,7 @@ NB_RESOURCE_SPI if(!handle){NBLOG(@"native-metal: texture view refused: %@",lastError());return nil;} NBTexture *view=[NBTexture new];view.handle=handle;NmbTextureDescriptor descriptor;nmb_texture_describe(handle,&descriptor);view.descriptor=descriptor; view.parent=(id)self;view.buffer=_buffer;view.offset=_offset;view.rowBytes=_rowBytes; - if(_surface)view.surface=(IOSurfaceRef)CFRetain(_surface); + if(_surface){view.surface=(IOSurfaceRef)CFRetain(_surface);[view countUse];} return (id)view; } - (id)newTextureViewWithPixelFormat:(MTLPixelFormat)format { @@ -958,12 +967,32 @@ static NBTexture *textureOnBuffer(NBBuffer *buffer,MTLTextureDescriptor *d,NSUIn NBTexture *texture=[NBTexture new];texture.handle=handle;nmb_texture_describe(handle,&descriptor);texture.descriptor=descriptor; texture.buffer=buffer;texture.offset=offset;texture.rowBytes=row;return texture; } +// myron: a two-plane extended-range surface ('b3a8', RGB10A8_2P_XR10) whose planes report no row bytes. IOSurfaceGetBytesPerRowOfPlane +// answers 0 for it on these ports, so the texture used to be refused (step 3) and its layer left undrawn (Data A Live bb43, same SoC, the +// App Library there). Its allocation is two linear planes, rows aligned to 64 bytes, one after the other: {offset0, row0, offset1, row1}, +// or false when the surface reports its rows itself or the computed planes do not fit the allocation. +static bool b3a8Planes(IOSurfaceRef surface,size_t allocation,size_t planes[4]) { + if(!surface || IOSurfaceGetPixelFormat(surface)!=0x62336138 || IOSurfaceGetPlaneCount(surface)!=2)return false; + if(IOSurfaceGetBytesPerRowOfPlane(surface,0) && IOSurfaceGetBytesPerRowOfPlane(surface,1))return false; + const size_t w=IOSurfaceGetWidth(surface),h=IOSurfaceGetHeight(surface); + const size_t row0=(w*4+63)&~(size_t)63,row1=(w+63)&~(size_t)63; + if(!w || !h || row0*h+row1*h>allocation)return false; + planes[0]=0;planes[1]=row0;planes[2]=row0*h;planes[3]=row1; + static std::atomic told{0}; + if(told.fetch_add(1)<8)NSLog(@"native-metal: b3a8 %lux%lu planes computed (row %lu at 0, row %lu at %lu; %lu of %lu bytes)",(unsigned long)w, + (unsigned long)h,(unsigned long)row0,(unsigned long)row1,(unsigned long)(row0*h),(unsigned long)(row0*h+row1*h),(unsigned long)allocation); + gSurfaceB3a8++; + return true; +} // A texture over a two-plane surface: the runtime reads plane 1 from the surface's own layout (two-plane RGB and YCbCr formats). static NBTexture *textureOnSurface(NBBuffer *buffer,MTLTextureDescriptor *d,IOSurfaceRef surface,NSUInteger plane,NSUInteger offset,NSUInteger row) { NmbTextureDescriptor descriptor;if(!buffer || !textureDescriptor(d,&descriptor))return nil; const uint64_t base=(uintptr_t)IOSurfaceGetBaseAddress(surface),chroma=(uintptr_t)IOSurfaceGetBaseAddressOfPlane(surface,1); + uint64_t chromaOffset=chroma>=base?chroma-base:0,chromaRow=IOSurfaceGetBytesPerRowOfPlane(surface,1); + size_t computed[4]; // myron: a 'b3a8' surface without plane rows + if(!chromaRow && b3a8Planes(surface,IOSurfaceGetAllocSize(surface),computed)){chromaOffset=computed[2];chromaRow=computed[3];} NmbTexture *handle=nmb_texture_new_on_surface(buffer.handle,&descriptor,offset,row,IOSurfaceGetPixelFormat(surface),plane, - IOSurfaceGetWidth(surface),IOSurfaceGetHeight(surface),chroma>=base?chroma-base:0,IOSurfaceGetBytesPerRowOfPlane(surface,1)); + IOSurfaceGetWidth(surface),IOSurfaceGetHeight(surface),chromaOffset,chromaRow); if(!handle){NBLOG(@"native-metal: texture over surface refused: %@",lastError());return nil;} NBTexture *texture=[NBTexture new];texture.handle=handle;nmb_texture_describe(handle,&descriptor);texture.descriptor=descriptor; texture.buffer=buffer;texture.offset=offset;texture.rowBytes=row;return texture; @@ -1113,7 +1142,11 @@ static std::string quartzCoreCallers(); - (id)newFunctionWithName:(NSString *)name { const uint64_t functionBegan=nowUs(); NmbFunction *handle=name?nmb_function_new(_handle,name.UTF8String):nullptr;gTimeFunction.add(functionBegan); - if(!handle)return nil; + if(!handle) { + static std::atomic told{0}; + if(told.fetch_add(1)<64)NBLOG(@"native-metal: function %@ refused: %@",name,lastError()); + return nil; + } NBFunction *function=[NBFunction new];function.handle=handle;function.name=name;function.functionType=(MTLFunctionType)nmb_function_type(handle); function.library=self;return (id)function; } @@ -1863,6 +1896,10 @@ static void writeDrawStats() { if(gDrawStats)[self noteState:kDSOther key:UINT32_MAX object:nullptr value:0];ENCODE([self check:nmb_render_set_depth_bias(self.handle,bias,slope,clamp) what:"setDepthBias"]); } - (void)setLineWidth:(float)width {ENCODE([self check:nmb_render_set_line_width(self.handle,width) what:"setLineWidth"]);} +- (void)setDepthClipMode:(MTLDepthClipMode)mode { + static std::atomic told{false}; + if(mode!=MTLDepthClipModeClip && !told.exchange(true))NBLOG(@"native-metal: depth clamp requested; drawn with depth clipping"); +} - (void)setVisibilityResultMode:(MTLVisibilityResultMode)mode offset:(NSUInteger)offset { ENCODE([self check:nmb_render_set_visibility_mode(self.handle,mode,offset) what:"setVisibilityResultMode"]); } @@ -2514,9 +2551,18 @@ static void dumpReadback(NBCommandBuffer *source,uint64_t appIndex) { if(!self.committed)return; encodeDrain(); const uint64_t began=nowUs(); - if(!self.failedLocally)nmb_command_buffer_wait(_handle,0); + // Bounded (kWaitBoundMs): a completion the bridge never delivers must not park the caller for good (the scanout worker would never call + // COMPLETE_SWAP and the display's 5 s SwapWait fires). The bound is above a slow first-use pipeline compile; on timeout the command buffer + // stays in flight and the caller sees a status that is not completed. + if(!self.failedLocally && nmb_command_buffer_wait(_handle,kWaitBoundMs)!=0) { + gWaitTimeouts++; + NBLOG(@"native-metal: completion wait timed out after %d ms (status %llu)",(int)kWaitBoundMs,(unsigned long long)nmb_command_buffer_status(_handle)); + } const uint32_t cpuStart=threadCpuUs(); - dispatch_group_wait(_handlers,DISPATCH_TIME_FOREVER); + if(dispatch_group_wait(_handlers,dispatch_time(DISPATCH_TIME_NOW,(int64_t)kWaitBoundMs*1000*1000))!=0) { + gWaitTimeouts++; + NBLOG(@"native-metal: completion handlers timed out (the bridge did not deliver the completion)"); + } { TraceRec rec={};rec.type=2;rec.t0=began;rec.t1=nowUs();rec.tid=threadIdLow();rec.a=(uint32_t)(uintptr_t)__builtin_return_address(0); rec.cpu0=cpuStart;rec.cpu1=threadCpuUs();traceNote(rec.tid);tracePush(rec); @@ -2614,7 +2660,8 @@ static void completed(void *context) { // ---- the device ---- template static T failWith(NSError **error,NSString *what,T value) { - noteProblem([NSString stringWithFormat:@"%@: %@",what,lastError()]); + static std::atomic told{0}; + if(told.fetch_add(1)<64)NBLOG(@"native-metal: %@ refused: %@",what,lastError());else noteProblem([NSString stringWithFormat:@"%@: %@",what,lastError()]); if(error)*error=nbError([NSString stringWithFormat:@"%@: %@",what,lastError()]); return value; } @@ -2962,6 +3009,129 @@ static id computeReflection(const NmbComputePipelineDescriptor *p) { // ---- heaps ---- // Metal heaps over the runtime's: an automatic heap places each resource, a placement heap takes an offset. Resources made from one keep it // alive (their `heap`); a heap's resources share its storage mode, CPU cache mode and (here) hazard tracking mode. +// DroidVM's texture layout (newTextureLayoutWithDescriptor:isHeapOrBufferBacked:): a block-compressed format's blocks row after row, +// each level after the one before, each slice's chain of levels after the slice before. Its watermark names it, so data ImageIO laid +// out for another GPU's layout is refused rather than read as this one. +typedef struct {uint64_t words[2];} DVMLayoutWatermark; +static bool dvmBlockFormat(MTLPixelFormat format, NSUInteger &bytes, NSUInteger &width, NSUInteger &height) { + switch(format){ +#define DVM_ASTC(W,H) case MTLPixelFormatASTC_##W##x##H##_sRGB: case MTLPixelFormatASTC_##W##x##H##_LDR: case MTLPixelFormatASTC_##W##x##H##_HDR: \ + bytes=16;width=W;height=H;return true; + DVM_ASTC(4,4) DVM_ASTC(5,4) DVM_ASTC(5,5) DVM_ASTC(6,5) DVM_ASTC(6,6) DVM_ASTC(8,5) DVM_ASTC(8,6) DVM_ASTC(8,8) DVM_ASTC(10,5) DVM_ASTC(10,6) + DVM_ASTC(10,8) DVM_ASTC(10,10) DVM_ASTC(12,10) DVM_ASTC(12,12) +#undef DVM_ASTC + case MTLPixelFormatETC2_RGB8: case MTLPixelFormatETC2_RGB8_sRGB: case MTLPixelFormatETC2_RGB8A1: case MTLPixelFormatETC2_RGB8A1_sRGB: + case MTLPixelFormatEAC_R11Unorm: case MTLPixelFormatEAC_R11Snorm: + case MTLPixelFormatBC1_RGBA: case MTLPixelFormatBC1_RGBA_sRGB: case MTLPixelFormatBC4_RUnorm: case MTLPixelFormatBC4_RSnorm: + bytes=8;width=height=4;return true; + case MTLPixelFormatEAC_RG11Unorm: case MTLPixelFormatEAC_RG11Snorm: case MTLPixelFormatEAC_RGBA8: case MTLPixelFormatEAC_RGBA8_sRGB: + case MTLPixelFormatBC2_RGBA: case MTLPixelFormatBC2_RGBA_sRGB: case MTLPixelFormatBC3_RGBA: case MTLPixelFormatBC3_RGBA_sRGB: + case MTLPixelFormatBC5_RGUnorm: case MTLPixelFormatBC5_RGSnorm: case MTLPixelFormatBC6H_RGBFloat: case MTLPixelFormatBC6H_RGBUfloat: + case MTLPixelFormatBC7_RGBAUnorm: case MTLPixelFormatBC7_RGBAUnorm_sRGB: + bytes=16;width=height=4;return true; + default:return false; + } +} +@interface DVMLinearTextureLayout : NSObject ++ (instancetype)layoutWithDescriptor:(MTLTextureDescriptor *)d; +- (NSUInteger)size; +- (void)upload:(const uint8_t *)memory into:(id)texture; +@end +@implementation DVMLinearTextureLayout { + NSUInteger _block,_bw,_bh,_width,_height,_depth,_levels,_slices,_chain; +} ++ (instancetype)layoutWithDescriptor:(MTLTextureDescriptor *)d { + NSUInteger bytes=0,width=0,height=0; + if(!d||!dvmBlockFormat(d.pixelFormat,bytes,width,height))return nil; + DVMLinearTextureLayout *layout=[self new]; + layout->_block=bytes;layout->_bw=width;layout->_bh=height; + layout->_width=std::max(d.width,1);layout->_height=std::max(d.height,1); + layout->_depth=d.textureType==MTLTextureType3D?std::max(d.depth,1):1; + layout->_levels=std::max(d.mipmapLevelCount,1); + const bool cube=d.textureType==MTLTextureTypeCube||d.textureType==MTLTextureTypeCubeArray; + layout->_slices=std::max(d.arrayLength,1)*(cube?6:1); + for(NSUInteger level=0;level_levels;level++)layout->_chain+=[layout imageBytes:level]*[layout levelDepth:level]; + static std::atomic told{false}; + if(!told.exchange(true))NBLOG(@"native-metal: texture layouts are DroidVM's linear ones (block-compressed formats; format %lu first)",(unsigned long)d.pixelFormat); + return layout; +} +- (NSUInteger)levelDepth:(NSUInteger)level {return std::max(_depth>>level,1);} +- (NSUInteger)blocksAcross:(NSUInteger)level {return (std::max(_width>>level,1)+_bw-1)/_bw;} +- (NSUInteger)blocksDown:(NSUInteger)level {return (std::max(_height>>level,1)+_bh-1)/_bh;} +- (NSUInteger)rowBytes:(NSUInteger)level {return [self blocksAcross:level]*_block;} +- (NSUInteger)imageBytes:(NSUInteger)level {return [self rowBytes:level]*[self blocksDown:level];} +- (NSUInteger)offsetOfSlice:(NSUInteger)slice level:(NSUInteger)level { + NSUInteger at=slice*_chain; + for(NSUInteger l=0;l=_slices||level>=_levels)return; + const NSUInteger across=[self blocksAcross:level],down=[self blocksDown:level],depth=[self levelDepth:level]; + const NSUInteger bx=region.origin.x/_bw,by=region.origin.y/_bh; + if(bx>=across||by>=down||region.origin.z>=depth)return; + const NSUInteger bw=std::min((region.origin.x+region.size.width+_bw-1)/_bw,across)-bx; + const NSUInteger bh=std::min((region.origin.y+region.size.height+_bh-1)/_bh,down)-by; + const NSUInteger bd=std::min(region.origin.z+std::max(region.size.depth,1),depth)-region.origin.z; + const NSUInteger row=[self rowBytes:level],image=[self imageBytes:level]; + uint8_t *base=memory+[self offsetOfSlice:slice level:level]; + for(NSUInteger z=0;z)texture { + for(NSUInteger slice=0;slice<_slices;slice++) + for(NSUInteger level=0;level<_levels;level++){ + const MTLRegion region=MTLRegionMake3D(0,0,0,std::max(_width>>level,1),std::max(_height>>level,1),[self levelDepth:level]); + [texture replaceRegion:region mipmapLevel:level slice:slice withBytes:memory+[self offsetOfSlice:slice level:level] + bytesPerRow:[self rowBytes:level] bytesPerImage:[self imageBytes:level]]; + } +} +@end +// The deallocator of the memory a texture was made over, run when the texture goes. +@interface DVMMemoryRelease : NSObject +- (instancetype)initWithBytes:(void *)bytes length:(NSUInteger)length deallocator:(void (^)(void *,NSUInteger))deallocator; +@end +@implementation DVMMemoryRelease { + void *_bytes;NSUInteger _length;void (^_deallocator)(void *,NSUInteger); +} +- (instancetype)initWithBytes:(void *)bytes length:(NSUInteger)length deallocator:(void (^)(void *,NSUInteger))deallocator { + if((self=[super init])){_bytes=bytes;_length=length;_deallocator=[deallocator copy];} + return self; +} +- (void)dealloc {if(_deallocator)_deallocator(_bytes,_length);} +@end +static char dvmMemoryReleaseKey; + @interface NBHeap : NBObject @property(nonatomic) NmbHeap *handle; @property(nonatomic) MTLResourceOptions options; @@ -3104,6 +3274,10 @@ static bool pipelineArchives(NSArray *list,std::vector &arch - (uint64_t)registryID {return gRegistryID;} - (NSUInteger)maxBufferLength {return nmb_device_max_buffer_length(gDevice);} - (BOOL)hasUnifiedMemory {return YES;} +- (BOOL)respondsToSelector:(SEL)selector { + if(selector==sel_registerName("targetDeviceInfo"))return NO; // the inherited _MTLDevice one, NULL without IOGPU + return [super respondsToSelector:selector]; +} // Core Image's CIMetalDeviceGetLimits reads both (Apple's GPU devices answer them; _MTLDevice does not): without them every CIContext // creation threw and iconservicesagent aborted while rasterizing symbol glyphs. The GPU has no memory of its own; it shares the system's. - (uint64_t)dedicatedMemorySize {return 0;} @@ -3162,23 +3336,47 @@ static bool pipelineArchives(NSArray *list,std::vector &arch if(!handle){NBLOG(@"native-metal: heap refused: %@",lastError());return nil;} NBHeap *heap=[NBHeap new];heap.handle=handle;heap.options=d.resourceOptions;heap.heapType=d.type;return (id)heap; } +// A descriptor the runtime refuses answers {0, 0}, as Metal answers one it makes no texture of. +- (MTLSizeAndAlign)heapTextureSizeAndAlignWithDescriptor:(MTLTextureDescriptor *)d { + NmbTextureDescriptor descriptor;uint64_t answer[2]={0,0}; + if(textureDescriptor(d,&descriptor)&&!nmb_device_heap_texture_size_and_align(gDevice,&descriptor,answer)) + NBLOG(@"native-metal: heap texture size refused: %@",lastError()); + return (MTLSizeAndAlign){answer[0],answer[1]}; +} +- (MTLSizeAndAlign)heapBufferSizeAndAlignWithLength:(NSUInteger)length options:(MTLResourceOptions)options { + uint64_t answer[2]={0,0}; + if(!nmb_device_heap_buffer_size_and_align(gDevice,length,(uint64_t)options,answer))NBLOG(@"native-metal: heap buffer size refused: %@",lastError()); + return (MTLSizeAndAlign){answer[0],answer[1]}; +} - (id)newResourceGroupFromResources:(const id __unsafe_unretained [])resources count:(NSUInteger)count { NBResourceGroup *group=[NBResourceGroup new]; NSMutableArray *held=[NSMutableArray arrayWithCapacity:count]; for(NSUInteger i=0;i)newTextureWithBytesNoCopy:(void *)bytes length:(NSUInteger)length descriptor:(MTLTextureDescriptor *)d deallocator:(void (^)(void *,NSUInteger))deallocator { - (void)bytes;(void)length;(void)d;(void)deallocator; - NBLOG(@"native-metal: newTextureWithBytesNoCopy:length:descriptor:deallocator: is not implemented (Apple's texture layout of raw memory is not known)"); - return nil; + DVMLinearTextureLayout *layout=[DVMLinearTextureLayout layoutWithDescriptor:d]; + if(!layout||!bytes||length texture=[(id)self newTextureWithDescriptor:shared]; + if(!texture)return nil; + [layout upload:(const uint8_t *)bytes into:texture]; + if(deallocator)objc_setAssociatedObject(texture,&dvmMemoryReleaseKey,[[DVMMemoryRelease alloc] initWithBytes:bytes length:length deallocator:deallocator], + OBJC_ASSOCIATION_RETAIN_NONATOMIC); + return texture; } - (id)newTextureLayoutWithDescriptor:(MTLTextureDescriptor *)d isHeapOrBufferBacked:(BOOL)backed { - (void)d;(void)backed; - NBLOG(@"native-metal: newTextureLayoutWithDescriptor:isHeapOrBufferBacked: is not implemented (Apple's layout object is not known)"); - return nil; + (void)backed; + DVMLinearTextureLayout *layout=[DVMLinearTextureLayout layoutWithDescriptor:d]; + if(!layout)NBLOG(@"native-metal: newTextureLayoutWithDescriptor:isHeapOrBufferBacked: has no layout for format %lu (Apple's is not known)",(unsigned long)d.pixelFormat); + return layout; } - (id)newCommandQueueWithMaxCommandBufferCount:(NSUInteger)count {(void)count;return [(id)self newCommandQueue];} - (id)newBufferWithLength:(NSUInteger)length options:(MTLResourceOptions)options { @@ -3227,15 +3425,17 @@ static bool pipelineArchives(NSArray *list,std::vector &arch if(planes)gSurfacePlanar++; void *base=IOSurfaceGetBaseAddress(surface);const size_t page=(size_t)getpagesize(),allocation=IOSurfaceGetAllocSize(surface); if(!page || (page&(page-1)) || !base || ((uintptr_t)base&(page-1)) || !allocation || allocation>SIZE_MAX-(page-1)){refuseSurface(2,surface,d,plane,(uintptr_t)base,0);return nil;} - const uintptr_t start=planes?(uintptr_t)IOSurfaceGetBaseAddressOfPlane(surface,plane):(uintptr_t)base; - const size_t row=planes?IOSurfaceGetBytesPerRowOfPlane(surface,plane):IOSurfaceGetBytesPerRow(surface); + uintptr_t start=planes?(uintptr_t)IOSurfaceGetBaseAddressOfPlane(surface,plane):(uintptr_t)base; + size_t row=planes?IOSurfaceGetBytesPerRowOfPlane(surface,plane):IOSurfaceGetBytesPerRow(surface); + size_t computed[4]; // myron: a 'b3a8' surface without plane rows + if(!row && plane<2 && b3a8Planes(surface,allocation,computed)){start=(uintptr_t)base+computed[plane*2];row=computed[plane*2+1];} if(start<(uintptr_t)base || start-(uintptr_t)base>=allocation || !row){refuseSurface(3,surface,d,plane,start-(uintptr_t)base,row);return nil;} const size_t bytes=(allocation+page-1)&~(page-1); NBBuffer *buffer=surfaceBuffer(surface,base,bytes);if(!buffer){refuseSurface(4,surface,d,plane,start-(uintptr_t)base,row);return nil;} NBTexture *texture=planes>=2?textureOnSurface(buffer,d,surface,plane,start-(uintptr_t)base,row):textureOnBuffer(buffer,d,start-(uintptr_t)base,row,true); if(!texture){refuseSurface(5,surface,d,plane,start-(uintptr_t)base,row);return nil;} if(gLogTextures)logTexture("iosurface",d,surface,plane,base,start-(uintptr_t)base,row,texture!=nil); - gSurfaceTextures++;texture.surface=(IOSurfaceRef)CFRetain(surface);return (id)texture; + gSurfaceTextures++;texture.surface=(IOSurfaceRef)CFRetain(surface);[texture countUse];return (id)texture; } - (id)newLibraryWithData:(dispatch_data_t)data error:(NSError **)error { const void *bytes=nullptr;size_t size=0;dispatch_data_t mapped=dispatch_data_create_map(data,&bytes,&size); @@ -3311,6 +3511,10 @@ static bool pipelineArchives(NSArray *list,std::vector &arch word(0x62697463 /* 'bitc' */);word((uint32_t)bitcode.length); const uint8_t *b=(const uint8_t *)bitcode.bytes;request.insert(request.end(),b,b+bitcode.length); } + // macOS 27's compiler (MTLCompilerObject::stitchFunctionDag) reads three more tagged slots after the functions, each a tag that + // is consumed whether or not it matches, and aborts when the request ends before them: 'nums' and the count of scripts, 'ptfm' + // and the platform, 'arid' and the size of an archive id. None of them, as zeros. + word(0x6e756d73 /* 'nums' */);word(0);word(0x7074666d /* 'ptfm' */);word(0);word(0x61726964 /* 'arid' */);word(0); NSString *warnings=nil,*failure=nil; NSData *metallib=codeGenRequest(14,request,&warnings,&failure); if(!metallib) { @@ -3390,7 +3594,12 @@ static bool pipelineArchives(NSArray *list,std::vector &arch reflection:(MTLAutoreleasedRenderPipelineReflection *)reflection error:(NSError **)error { if(reflection)*reflection=nil; auto *p=new NmbRenderPipelineDescriptor(); - if(!renderDescriptor(d,p,error)){delete p;return nil;} + if(!renderDescriptor(d,p,error)) { + static std::atomic told{0}; + if(told.fetch_add(1)<64)NBLOG(@"native-metal: render pipeline descriptor refused (vertex %@, fragment %@): %@",d.vertexFunction.name, + d.fragmentFunction.name,error?(*error).localizedDescription:@"?"); + delete p;return nil; + } std::vector archives; if(!pipelineArchives(d.binaryArchives,archives,error)){delete p;return nil;} const NmbPipelineOptions creation={(uint64_t)options,archives.data(),archives.size()}; @@ -3488,6 +3697,14 @@ static uint64_t askAnswer(SEL cmd,NSUInteger arg) { } static NSUInteger countArgAnswer(id,SEL cmd,NSUInteger arg) {return (NSUInteger)askAnswer(cmd,arg);} static BOOL flagArgAnswer(id,SEL cmd,NSUInteger arg) {return askAnswer(cmd,arg)!=0;} +static BOOL (*gFamilyAnswer)(id,SEL,NSInteger); +static BOOL familyForCaller(id self,SEL _cmd,NSInteger family) { + if(family>=1003 && family<2000) { // MTLGPUFamilyApple3 and up + Dl_info caller; + if(dladdr(__builtin_return_address(0),&caller) && caller.dli_fname && strstr(caller.dli_fname,"/RenderBox.framework/"))return NO; + } + return gFamilyAnswer(self,_cmd,family); +} static void installAnswers(Class cls) { uint32_t kind=0; for(size_t i=0;const char *name=nmb_device_answer_selector(i,&kind);i++) { @@ -3498,7 +3715,7 @@ static void installAnswers(Class cls) { // Extended-range 10-bit (XR10) framebuffers are withheld: this display chain presents 8-bit BGRX scanout only. The render server picks its // wide-gamut 64-bit (and Apple-compressed) framebuffer when a device offers them, and the display driver could not show it (int-089: the // swap's surface was '&w4a', 8 bytes per pixel, and every swap was refused). - if(!strcmp(name,"supportsExtendedXR10Formats"))value=0; + if(!strcmp(name,"supportsExtendedXR10Formats") && (!strstr(getprogname(),"Poster") || !strcmp(getprogname(),"PosterBoard")))value=0; gAnswers[sel]=value; class_addMethod(cls,sel,kind==NMB_ANSWER_COUNT?(IMP)countAnswer:(IMP)flagAnswer,kind==NMB_ANSWER_COUNT?"Q16@0:8":"B16@0:8"); break; @@ -3506,6 +3723,10 @@ static void installAnswers(Class cls) { case NMB_ANSWER_FLAG_ARG: class_addMethod(cls,sel,(IMP)flagArgAnswer,"B24@0:8Q16");break; } } + if(Method family=class_getInstanceMethod(cls,@selector(supportsFamily:))) { + gFamilyAnswer=(BOOL (*)(id,SEL,NSInteger))method_getImplementation(family); + method_setImplementation(family,(IMP)familyForCaller); + } } extern "C" id native_bridge_metal_open(const char *loader,const char *cache,uint64_t registryID,void (*missing)(const char *,const char *)) { @@ -3565,8 +3786,8 @@ extern "C" size_t native_bridge_metal_status(char *out,size_t size) { } static size_t native_bridge_metal_formats_locked(char *out,size_t size) { - int n=snprintf(out,size,"tex x%llu s%llu/%llu p%llu f",(unsigned long long)gTextureRefusals.load(),(unsigned long long)gSurfaceTextures.load(), - (unsigned long long)gSurfaceRefusals.load(),(unsigned long long)gSurfacePlanar.load()); + int n=snprintf(out,size,"tex x%llu s%llu/%llu p%llu b%llu f",(unsigned long long)gTextureRefusals.load(),(unsigned long long)gSurfaceTextures.load(), + (unsigned long long)gSurfaceRefusals.load(),(unsigned long long)gSurfacePlanar.load(),(unsigned long long)gSurfaceB3a8.load()); for(unsigned i=0;i0 && (size_t)n0?(size_t)n:0; } @@ -3675,7 +3896,7 @@ extern "C" size_t native_bridge_metal_telemetry(char *out,size_t size) { row("G commit-to-complete n=%llu total=%llums max=%llums mean=%lluus",(unsigned long long)gTimeEncodeToCommit.count.load(),ms(gTimeEncodeToCommit.total.load()), ms(gTimeEncodeToCommit.slowest.load()),(unsigned long long)(gTimeEncodeToCommit.count.load()?gTimeEncodeToCommit.total.load()/gTimeEncodeToCommit.count.load():0)); row("J scheduled wait n=%llu total=%llums max=%llums",(unsigned long long)gTimeWaitScheduled.count.load(),ms(gTimeWaitScheduled.total.load()),ms(gTimeWaitScheduled.slowest.load())); - row("J completed wait n=%llu total=%llums max=%llums",(unsigned long long)gTimeWaitCompleted.count.load(),ms(gTimeWaitCompleted.total.load()),ms(gTimeWaitCompleted.slowest.load())); + row("J completed wait n=%llu total=%llums max=%llums t=%llu",(unsigned long long)gTimeWaitCompleted.count.load(),ms(gTimeWaitCompleted.total.load()),ms(gTimeWaitCompleted.slowest.load()),(unsigned long long)gWaitTimeouts.load()); row("J resource wait n=%llu total=%llums max=%llums",(unsigned long long)gTimeResourceWait.count.load(),ms(gTimeResourceWait.total.load()),ms(gTimeResourceWait.slowest.load())); row("J resource checks=%llu skipped=%llu",(unsigned long long)gResourceChecks.load(),(unsigned long long)gResourceSkipped.load()); row("J surface cache n=%llu bytes=%llu dup=%llu us=%llu max=%llu",(unsigned long long)gTimeSurfaceFlush.count.load(), diff --git a/bridge/NativeMetalObjects.h b/bridge/NativeMetalObjects.h index 6cf602a..cf0df54 100644 --- a/bridge/NativeMetalObjects.h +++ b/bridge/NativeMetalObjects.h @@ -95,6 +95,8 @@ uint64_t nmb_heap_max_available_size(const NmbHeap *, uint64_t alignment); uint64_t nmb_heap_set_purgeable_state(const NmbHeap *, uint64_t state); NmbBuffer *nmb_heap_new_buffer(const NmbHeap *, uint64_t length, uint64_t options, uint64_t offset); NmbTexture *nmb_heap_new_texture(const NmbHeap *, const struct NmbTextureDescriptor *, uint64_t offset); +bool nmb_device_heap_texture_size_and_align(const NmbDevice *, const struct NmbTextureDescriptor *, uint64_t out[2]); /* size, align */ +bool nmb_device_heap_buffer_size_and_align(const NmbDevice *, uint64_t length, uint64_t options, uint64_t out[2]); NmbTexture *nmb_texture_new(NmbDevice *, const struct NmbTextureDescriptor *); /* A linear texture over a buffer's bytes; iosurface marks the buffer as standing for a surface without planes (Metal's IOSurface rules). */ NmbTexture *nmb_texture_new_on_buffer(NmbBuffer *, const struct NmbTextureDescriptor *, uint64_t offset, uint64_t bytes_per_row, bool iosurface); diff --git a/bridge/NativeMetalObjects.rs b/bridge/NativeMetalObjects.rs index 73fe6f6..c8bf6fe 100644 --- a/bridge/NativeMetalObjects.rs +++ b/bridge/NativeMetalObjects.rs @@ -891,6 +891,29 @@ pub extern "C" fn nmb_heap_new_texture(heap: *const NmbHeap, descriptor: *const }) } +/// `heapTextureSizeAndAlignWithDescriptor:`: the bytes the texture takes of a heap and the alignment of its offset, into `out[0]` and +/// `out[1]`; false, with the reason, for a descriptor the runtime refuses. +#[unsafe(no_mangle)] +pub extern "C" fn nmb_device_heap_texture_size_and_align(device: *const NmbDevice, descriptor: *const NmbTextureDescriptor, out: *mut u64) -> bool { + guard(false, || { + let answer = get(device)?.0.heap_texture_size_and_align(&texture_descriptor(descriptor)?).map_err(e("heap texture size"))?; + let out = unsafe { std::slice::from_raw_parts_mut(out, 2) }; + (out[0], out[1]) = (answer.size, answer.align); + Ok(true) + }) +} + +/// `heapBufferSizeAndAlignWithLength:options:`, as `nmb_device_heap_texture_size_and_align` answers a texture. +#[unsafe(no_mangle)] +pub extern "C" fn nmb_device_heap_buffer_size_and_align(device: *const NmbDevice, length: u64, options_word: u64, out: *mut u64) -> bool { + guard(false, || { + let answer = get(device)?.0.heap_buffer_size_and_align(length, options(options_word)?); + let out = unsafe { std::slice::from_raw_parts_mut(out, 2) }; + (out[0], out[1]) = (answer.size, answer.align); + Ok(true) + }) +} + // ---- textures ---- #[repr(C)] diff --git a/bridge/patches/0017-optional-shader-cache-directory.patch b/bridge/patches/0017-optional-shader-cache-directory.patch new file mode 100644 index 0000000..c6c2c17 --- /dev/null +++ b/bridge/patches/0017-optional-shader-cache-directory.patch @@ -0,0 +1,45 @@ +diff --git a/crates/metal2vulkan-runtime/src/shader_cache/store.rs b/crates/metal2vulkan-runtime/src/shader_cache/store.rs +index 98e82e1..1c7eac7 100644 +--- a/crates/metal2vulkan-runtime/src/shader_cache/store.rs ++++ b/crates/metal2vulkan-runtime/src/shader_cache/store.rs +@@ -68,6 +68,11 @@ impl Directory { + /// Open `root` for `build` with a cap of `capacity` bytes: create it and the build's + /// directory (owner-only) and write and remove a probe file there. The error names the + /// directory that failed. ++ /// ++ /// With `METAL2VULKAN_SHADER_CACHE_OPTIONAL` in the environment a directory that cannot be ++ /// made or written opens anyway: a process that may write nowhere (a sandboxed one, as ++ /// WebKit's GPU process) still gets its device, and every read and write of the cache is a ++ /// miss, as after any later failure. + pub(crate) fn open(root: PathBuf, build: Hash256, capacity: u64) -> Result { + let directory = Directory { + root, +@@ -78,14 +83,20 @@ impl Directory { + let path = path.to_path_buf(); + move |error| Failed { path, error } + }; +- create_private_dir_all(&directory.root).map_err(failed(&directory.root))?; +- let build_dir = directory.build_dir(); +- create_private_dir_all(&build_dir).map_err(failed(&build_dir))?; +- let probe = build_dir.join(format!(".probe.{}.tmp", unique())); +- let written = new_private_file(&probe).and_then(|mut file| file.write_all(b"probe")); +- let removed = fs::remove_file(&probe); +- written.and(removed).map_err(failed(&directory.root))?; +- Ok(directory) ++ let made = (|| { ++ create_private_dir_all(&directory.root).map_err(failed(&directory.root))?; ++ let build_dir = directory.build_dir(); ++ create_private_dir_all(&build_dir).map_err(failed(&build_dir))?; ++ let probe = build_dir.join(format!(".probe.{}.tmp", unique())); ++ let written = new_private_file(&probe).and_then(|mut file| file.write_all(b"probe")); ++ let removed = fs::remove_file(&probe); ++ written.and(removed).map_err(failed(&directory.root)) ++ })(); ++ match made { ++ Err(_) if std::env::var_os("METAL2VULKAN_SHADER_CACHE_OPTIONAL").is_some() => Ok(directory), ++ Err(failed) => Err(failed), ++ Ok(()) => Ok(directory), ++ } + } + + /// The directory the consumer named or the platform's default. diff --git a/bridge/patches/0018-srgb-extended-range-blend-rounded-model.patch b/bridge/patches/0018-srgb-extended-range-blend-rounded-model.patch new file mode 100644 index 0000000..2fd3afc --- /dev/null +++ b/bridge/patches/0018-srgb-extended-range-blend-rounded-model.patch @@ -0,0 +1,38 @@ +diff --git a/crates/metal2vulkan/src/lower/arith/extended_range/blend.rs b/crates/metal2vulkan/src/lower/arith/extended_range/blend.rs +index 0b004f2..ead0ad8 100644 +--- a/crates/metal2vulkan/src/lower/arith/extended_range/blend.rs ++++ b/crates/metal2vulkan/src/lower/arith/extended_range/blend.rs +@@ -765,10 +765,16 @@ fn planned_for( + let narrow = narrow(eq); + // The rounded model holds for any blend into an `_sRGB` form, a mask of one color channel, + // and the color channels' blend color difference. ++ // An `_sRGB` form's blend the fast model does not hold (any but a narrow, unmasked one) takes ++ // the rounded model, Metal's own without fast math, measured for every `_sRGB` form (EV ++ // XRB-xrts-m4) and apart from its fast-math result only in roundings, rather than no pipeline: ++ // NanoUniverse's starfield (the Astronomy wallpaper, `BGRA10_XR_sRGB`) had none. ++ let math = if math == BlendMath::Fast && srgb && (!narrow || masked) { ++ BlendMath::Rounded ++ } else { ++ math ++ }; + let fast = math == BlendMath::Fast; +- if fast && srgb && (!narrow || masked) { +- return Err(refused(ExtendedRangeBlend::SrgbForm)); +- } + let opaque_ = !has_alpha; + let color = match colors { + 0 => None, +@@ -910,6 +916,13 @@ impl Blend { + /// Metal's model's: a `half4` one; a `float4` one under a blend whose color lanes take one term + /// alone, unmasked, rounded to `half` first; another is the gap it is. + pub fn takes(&self, output: Option) -> Result<(), Error> { ++ // The rounded model rounds a `float4` output to `half` first, as a `half4` one. ++ if self.math == BlendMath::Rounded { ++ return match output { ++ Some(FloatFmt::Half | FloatFmt::F32) => Ok(()), ++ _ => Err(refused(ExtendedRangeBlend::Output)), ++ }; ++ } + match output { + Some(FloatFmt::Half) => Ok(()), + Some(FloatFmt::F32) if self.narrow && self.takes_float && !self.masked => Ok(()), diff --git a/bridge/patches/0019-llvm-bitreverse.patch b/bridge/patches/0019-llvm-bitreverse.patch new file mode 100644 index 0000000..7e3be70 --- /dev/null +++ b/bridge/patches/0019-llvm-bitreverse.patch @@ -0,0 +1,42 @@ +diff --git a/crates/metal2vulkan-air/src/intrinsics/llvm.rs b/crates/metal2vulkan-air/src/intrinsics/llvm.rs +index d784c6c..b6ca3f9 100644 +--- a/crates/metal2vulkan-air/src/intrinsics/llvm.rs ++++ b/crates/metal2vulkan-air/src/intrinsics/llvm.rs +@@ -56,6 +56,8 @@ pub enum LlvmIntOp { + Bswap, + /// `llvm.ctpop`: the number of set bits. + Ctpop, ++ /// `llvm.bitreverse`: the bits in reverse order (what `reverse_bits` compiles to). ++ Bitreverse, + /// `llvm.uadd.sat`: unsigned saturating addition. + UAddSat, + /// `llvm.usub.sat`: unsigned saturating subtraction. +@@ -340,6 +342,7 @@ pub(super) const BASES: &[(&str, Base)] = &[ + ("fshr", Base::Int(LlvmIntOp::Fshr)), + ("bswap", Base::Int(LlvmIntOp::Bswap)), + ("ctpop", Base::Int(LlvmIntOp::Ctpop)), ++ ("bitreverse", Base::Int(LlvmIntOp::Bitreverse)), + ("uadd.sat", Base::Int(LlvmIntOp::UAddSat)), + ("usub.sat", Base::Int(LlvmIntOp::USubSat)), + ("sadd.sat", Base::Int(LlvmIntOp::SAddSat)), +@@ -393,7 +396,7 @@ impl LlvmFloatOp { + impl LlvmIntOp { + fn arity(self) -> usize { + match self { +- LlvmIntOp::Bswap | LlvmIntOp::Ctpop => 1, ++ LlvmIntOp::Bswap | LlvmIntOp::Ctpop | LlvmIntOp::Bitreverse => 1, + LlvmIntOp::Fshl | LlvmIntOp::Fshr => 3, + _ => 2, + } +diff --git a/crates/metal2vulkan/src/lower/arith/mod.rs b/crates/metal2vulkan/src/lower/arith/mod.rs +index 54f371d..57d7eec 100644 +--- a/crates/metal2vulkan/src/lower/arith/mod.rs ++++ b/crates/metal2vulkan/src/lower/arith/mod.rs +@@ -2415,6 +2415,7 @@ fn llvm_math(op: &LlvmIntrinsic) -> Result { + LlvmIntOp::Fshr => IntFn::Fshr, + LlvmIntOp::Bswap => IntFn::Bswap, + LlvmIntOp::Ctpop => IntFn::Popcount, ++ LlvmIntOp::Bitreverse => IntFn::ReverseBits, + LlvmIntOp::UAddSat => IntFn::AddSat(Sign::Unsigned), + LlvmIntOp::USubSat => IntFn::SubSat(Sign::Unsigned), + LlvmIntOp::SAddSat => IntFn::AddSat(Sign::Signed), diff --git a/bridge/patches/0020-growing-memory-blocks.patch b/bridge/patches/0020-growing-memory-blocks.patch new file mode 100644 index 0000000..55a2210 --- /dev/null +++ b/bridge/patches/0020-growing-memory-blocks.patch @@ -0,0 +1,116 @@ +diff --git a/crates/metal2vulkan-runtime/src/memory.rs b/crates/metal2vulkan-runtime/src/memory.rs +index de3df1e..a09c1ee 100644 +--- a/crates/metal2vulkan-runtime/src/memory.rs ++++ b/crates/metal2vulkan-runtime/src/memory.rs +@@ -51,6 +51,13 @@ use std::sync::Mutex; + /// The size of a pooled block, at most the device's `maxMemoryAllocationSize`. + pub(super) const BLOCK_BYTES: u64 = 64 << 20; + ++/// The first pooled block of a pool: later ones grow with the pool ([`Memory::next_block_bytes`]), ++/// doubling it up to [`BLOCK_BYTES`]. Where device memory is the system's own and every block is ++/// wired as it is made (a guest whose GPU imports its pages), a process with a few small resources ++/// otherwise held a 64 MiB block in each of its pools: 256-640 MiB each, 2 GiB in all at an iPadOS ++/// guest's home screen. ++pub(super) const FIRST_BLOCK_BYTES: u64 = 4 << 20; ++ + /// What memory a resource needs. + #[derive(Clone, Copy, Debug, PartialEq, Eq)] + pub(super) enum MemoryClass { +@@ -328,6 +335,14 @@ impl Pool { + } + } + ++ /// The bytes of its pooled blocks. ++ pub(super) fn pooled_bytes(&self) -> u64 { ++ (self.blocks.iter().flatten()) ++ .filter(|block| !block.dedicated) ++ .map(|block| block.ranges.len()) ++ .sum() ++ } ++ + /// Whether a pooled block has room for `size` bytes at the largest alignment asked of the + /// pool so far. + pub(super) fn has_room(&self, size: u64) -> bool { +@@ -821,6 +836,17 @@ impl Memory { + .add(size, dedicated, block, used) + } + ++ /// The size of the next pooled block of `pool` for a placement of `size` bytes: as many bytes ++ /// as its pooled blocks have (the pool doubles), at least [`FIRST_BLOCK_BYTES`] and the ++ /// placement's next power of two, at most [`BLOCK_BYTES`] (or the device's largest ++ /// allocation). ++ fn next_block_bytes(&self, pool: (Holds, u32), size: u64) -> u64 { ++ let have = self.pool_ref(pool.0, pool.1).pooled_bytes(); ++ (have.max(FIRST_BLOCK_BYTES).max(size.next_power_of_two())) ++ .min(self.block_bytes) ++ .max(size) ++ } ++ + /// What making a block takes, copied out so that the block is made with the memory's lock + /// released ([`place`], [`keep_room`]). + fn maker(&self) -> BlockMaker { +@@ -1374,10 +1400,10 @@ fn place( + return Err(Error::OutOfMemory(MemoryKind::Device)); + } + let dedicated = fit == Fit::Apart || size > maker.block_bytes / 2; +- let block_size = if dedicated { size } else { maker.block_bytes }; + let mut failure = Error::OutOfMemory(MemoryKind::Device); + for type_index in candidates { + let pool = (holds, type_index); ++ let block_size = if dedicated { size } else { lock(memory).next_block_bytes(pool, size) }; + if fit == Fit::Pooled { + if let Some(placement) = + lock(memory).place_pooled(device, pool, size, align, addressed)? +@@ -1411,27 +1437,26 @@ pub(super) fn keep_room(memory: &Mutex, device: &ash::Device, counts: &C + let Ok(kept) = memory.kept(device) else { + return; + }; +- let room = memory.block_bytes / 2; +- let lacking: Vec<(Holds, u32)> = (kept.into_iter()) +- .filter(|&(holds, type_index)| !memory.pool_ref(holds, type_index).has_room(room)) ++ // Room for half of the pool's next block: a growing pool keeps room for what its blocks ++ // have held, rather than half of the largest block from its first encoding on. ++ let lacking: Vec<((Holds, u32), u64)> = (kept.into_iter()) ++ .map(|pool| (pool, memory.next_block_bytes(pool, 1))) ++ .filter(|&((holds, type_index), bytes)| !memory.pool_ref(holds, type_index).has_room(bytes / 2)) + .collect(); + (lacking, memory.maker()) + }; +- for pool in lacking { ++ for (pool, bytes) in lacking { + let addressed = (pool.0 == Holds::Buffers).then_some(counts); +- let Ok(block) = maker.make(device, pool.1, maker.block_bytes, addressed) else { ++ let Ok(block) = maker.make(device, pool.1, bytes, addressed) else { + continue; + }; + let mut memory = lock(memory); +- if memory +- .pool_ref(pool.0, pool.1) +- .has_room(maker.block_bytes / 2) +- { ++ if memory.pool_ref(pool.0, pool.1).has_room(bytes / 2) { + drop(memory); + // SAFETY: nothing is placed in the block, which no command reads. + unsafe { block.release(device) }; + continue; + } +- memory.add_block(pool, block, maker.block_bytes, false, 0); ++ memory.add_block(pool, block, bytes, false, 0); + } + } +diff --git a/crates/metal2vulkan-runtime/src/range.rs b/crates/metal2vulkan-runtime/src/range.rs +index 5c32bef..54855d5 100644 +--- a/crates/metal2vulkan-runtime/src/range.rs ++++ b/crates/metal2vulkan-runtime/src/range.rs +@@ -111,6 +111,11 @@ impl RangeAllocator { + self.free.insert(start, size); + } + ++ /// The bytes of `[base, end)`. ++ pub(super) fn len(&self) -> u64 { ++ self.end - self.base ++ } ++ + /// Whether nothing is allocated. + pub(super) fn is_empty(&self) -> bool { + self.free.len() == 1 && self.free.get(&self.base) == Some(&(self.end - self.base)) diff --git a/bridge/patches/0021-patch-with-no-control-points.patch b/bridge/patches/0021-patch-with-no-control-points.patch new file mode 100644 index 0000000..d06b274 --- /dev/null +++ b/bridge/patches/0021-patch-with-no-control-points.patch @@ -0,0 +1,18 @@ +diff --git a/crates/metal2vulkan/src/reflection/function.rs b/crates/metal2vulkan/src/reflection/function.rs +index e62075c..812f372 100644 +--- a/crates/metal2vulkan/src/reflection/function.rs ++++ b/crates/metal2vulkan/src/reflection/function.rs +@@ -224,9 +224,12 @@ pub(crate) fn metallib_properties( + 2 => PatchType::Quad, + _ => return Err(unmeasured()), + }; ++ // A count of 0 is `[[patch(quad, 0)]]`: a patch with no control points, whose function ++ // reads its own data by `patch_id` (GFXBench's Car Chase draws its Bezier car body so). ++ // Metal answers it as stated: the patch type and 0. + let count = match byte >> 2 { + 63 => None, +- n @ 1..=32 => Some(u32::from(n)), ++ n @ 0..=32 => Some(u32::from(n)), + _ => return Err(unmeasured()), + }; + Some((kind, count)) From 0f11e64aace9333c3fb30a67fa8dcacaa56bdfeb Mon Sep 17 00:00:00 2001 From: rpone Date: Wed, 28 Oct 2026 09:55:19 +0800 Subject: [PATCH 57/92] perf(metal): hold surface imports for a second; gpuCoreCount, bitCodeHash, userDictionary and Metal protocol conformances Core Animation makes a texture of each window surface every frame and drops it when the frame ends, so the weak import table lost the import and each frame allocated, mapped and freed it again. An import used in the last second stays held (64 at most). The device answers gpuCoreCount and the buffer-texture alignment, functions answer bitCodeHash, command buffers keep a userDictionary, and the object classes declare their Metal protocols (MPS checks them). --- NativeBridgeMetalFrontend.mm | 59 +++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index c879b9e..2a62a28 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -726,6 +726,22 @@ NB_RESOURCE_SPI // caller-owned no-copy buffers do not enter this cache. static NSMapTable *gSurfaceImports; static std::mutex gSurfaceImportLock; +// Core Animation makes a texture of each window surface at every frame and drops it when the frame completes, so the weak table +// lost the import between frames and each frame imported every surface again (allocate, map, free). An import used in the last +// second stays held (strongly, at most 64): the pages are the surface's own and the key includes identity, address and size. +#include +#include +struct HeldImport {NBBuffer *buffer;uint64_t usedUs;}; +static std::map,HeldImport> gHeldImports; // under gSurfaceImportLock +static void holdImport(uint64_t identity,uint64_t address,uint64_t bytes,NBBuffer *buffer) { + const uint64_t now=nowUs();gHeldImports[{identity,address,bytes}]={buffer,now}; + for(auto it=gHeldImports.begin();it!=gHeldImports.end();)it=now-it->second.usedUs>1000000?gHeldImports.erase(it):std::next(it); + while(gHeldImports.size()>64) { + auto oldest=gHeldImports.begin(); + for(auto it=gHeldImports.begin();it!=gHeldImports.end();++it)if(it->second.usedUssecond.usedUs)oldest=it; + gHeldImports.erase(oldest); + } +} static NBBuffer *surfaceBuffer(IOSurfaceRef surface,void *base,size_t bytes) { struct Key {uint64_t identity,address,bytes;}; const Key value={IOSurfaceGetID(surface),(uintptr_t)base,bytes}; @@ -733,7 +749,7 @@ static NBBuffer *surfaceBuffer(IOSurfaceRef surface,void *base,size_t bytes) { std::lock_guard lock(gSurfaceImportLock); if(!gSurfaceImports)gSurfaceImports=[NSMapTable strongToWeakObjectsMapTable]; NBBuffer *buffer=key?[gSurfaceImports objectForKey:key]:nil; - if(buffer)return buffer; + if(buffer){if(key)holdImport(value.identity,value.address,value.bytes,buffer);return buffer;} // The surface keeps its CPU mapping (the imported pages) until the import is freed. NmbBuffer *handle=nmb_buffer_new_no_copy_releasing(gDevice,base,bytes,MTLResourceStorageModeShared,releaseSurface,(void *)CFRetain(surface)); if(!handle){CFRelease(surface);NBLOG(@"native-metal: surface import refused: %@",lastError());return nil;} @@ -747,6 +763,7 @@ static NBBuffer *surfaceBuffer(IOSurfaceRef surface,void *base,size_t bytes) { if(gSurfaceImports.count>=256)[gSurfaceImports removeObjectForKey:gSurfaceImports.keyEnumerator.nextObject]; } [gSurfaceImports setObject:buffer forKey:key]; + holdImport(value.identity,value.address,value.bytes,buffer); } return buffer; } @@ -1027,10 +1044,25 @@ static NBTexture *textureOnSurface(NBBuffer *buffer,MTLTextureDescriptor *d,IOSu @property(nonatomic) MTLFunctionType functionType; @property(nonatomic) std::string constantsText; // name:type:hex of each constant value it was specialized with (the journal's) @property(nonatomic,strong) NBLibrary *library; // where it came from (stitching reads its bitcode), and the values it was +@property(nonatomic,strong) NSData *bitCodeHashData; @property(nonatomic,strong) MTLFunctionConstantValues *constantValues; // specialized with (nil: none), for the zero-rest rebuild of a refused pipeline @end @implementation NBFunction - (void)dealloc {nmb_function_release(_handle);} +// _MTLFunction's bitCodeHash (32 bytes), read by the stitched-library path (createStitchingScriptHash): a digest naming this function +// object, its name and a serial no other function in the process has. +- (const void *)bitCodeHash { + @synchronized(self) { + if(!_bitCodeHashData) { + static std::atomic serial; + NSData *text=[[NSString stringWithFormat:@"%@#%llu",_name,(unsigned long long)++serial] dataUsingEncoding:NSUTF8StringEncoding]; + NSMutableData *digest=[NSMutableData dataWithLength:CC_SHA256_DIGEST_LENGTH]; + CC_SHA256(text.bytes,(CC_LONG)text.length,(unsigned char *)digest.mutableBytes); + _bitCodeHashData=digest; + } + return _bitCodeHashData.bytes; + } +} - (MTLFunctionOptions)options {return MTLFunctionOptionNone;} - (MTLPatchType)patchType {return (MTLPatchType)nmb_function_patch_type(_handle);} - (NSInteger)patchControlPointCount {return (NSInteger)nmb_function_patch_control_point_count(_handle);} @@ -2268,6 +2300,15 @@ static void dumpReadback(NBCommandBuffer *source,uint64_t appIndex) { } @implementation NBCommandBuffer +// IOGPUMetalCommandBuffer's userDictionary: a mutable dictionary per command buffer, which MPS keeps its per-buffer state in (nil lost it). +static char kUserDictionaryKey; +- (id)userDictionary { + @synchronized(self) { + NSMutableDictionary *dictionary=objc_getAssociatedObject(self,&kUserDictionaryKey); + if(!dictionary){dictionary=[NSMutableDictionary dictionary];objc_setAssociatedObject(self,&kUserDictionaryKey,dictionary,OBJC_ASSOCIATION_RETAIN_NONATOMIC);} + return dictionary; + } +} // iPadOS 27: QuartzCore names the tasks a command buffer works for (GPU time attribution), on every buffer; there is no such accounting here. - (void)setResponsibleTaskIDs:(const void *)ids count:(NSUInteger)count {(void)ids;(void)count;} - (instancetype)init { @@ -3271,6 +3312,13 @@ static bool pipelineArchives(NSArray *list,std::vector &arch @end @implementation NBDeviceMethods - (NSString *)name {return gName;} +// MPSCore reads the GPU core count of every Apple GPU family (no count: no MPS device, and its image cache is nil); the Adreno's +// shader processors count as eight cores, which only sizes MPS's kernel choices. +- (NSUInteger)gpuCoreCount {return 8;} +// A texture over a buffer starts where a linear texture may: the texel-buffer alignment the runtime answers for the linear one. +- (NSUInteger)minimumTextureBufferAlignmentForPixelFormat:(NSUInteger)format { + return ((NSUInteger (*)(id,SEL,NSUInteger))objc_msgSend)(self,sel_registerName("minimumLinearTextureAlignmentForPixelFormat:"),format); +} - (uint64_t)registryID {return gRegistryID;} - (NSUInteger)maxBufferLength {return nmb_device_max_buffer_length(gDevice);} - (BOOL)hasUnifiedMemory {return YES;} @@ -3762,6 +3810,15 @@ extern "C" id native_bridge_metal_open(const char *loader,const char *cache,uint // The original registration (_MTLAddDevice) asserts that a plugin's device conforms to Metal's private device protocol as well; the target's own // drivers get it from their IOGPU base class. Conformance only answers conformsToProtocol:, the methods are still looked up one by one. if(Protocol *spi=objc_getProtocol("MTLDeviceSPI"))class_addProtocol(cls,spi); +{// Clients check the object protocols (MPSCommandBufferImageCache's init returns nil for a command buffer that is not an MTLCommandBuffer). + static const char *const conformances[][2]={ + {"NBBuffer","MTLBuffer"},{"NBTexture","MTLTexture"},{"NBLibrary","MTLLibrary"},{"NBFunction","MTLFunction"}, + {"NBComputePipeline","MTLComputePipelineState"},{"NBRenderPipeline","MTLRenderPipelineState"},{"NBSampler","MTLSamplerState"}, + {"NBDepthStencil","MTLDepthStencilState"},{"NBEvent","MTLEvent"},{"NBSharedEvent","MTLSharedEvent"},{"NBFence","MTLFence"}, + {"NBQueue","MTLCommandQueue"},{"NBCommandBuffer","MTLCommandBuffer"},{"NBEncoder","MTLCommandEncoder"}, + {"NBComputeEncoder","MTLComputeCommandEncoder"},{"NBRenderEncoder","MTLRenderCommandEncoder"},{"NBBlitEncoder","MTLBlitCommandEncoder"}, + {"NBHeap","MTLHeap"},{"NBBinaryArchive","MTLBinaryArchive"}}; + for(const auto &c:conformances){Class k=objc_getClass(c[0]);Protocol *pr=objc_getProtocol(c[1]);if(k&&pr)class_addProtocol(k,pr);}} objc_registerClassPair(cls); gDeviceObject=[cls new]; return gDeviceObject; From 028c0a156023beac2600b2493cc256f4c738ea04 Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Wed, 28 Oct 2026 20:13:16 +0800 Subject: [PATCH 58/92] fix(metal): use the system allocator in the bridge On iPadOS 27 mimalloc's arena reservation (a large aligned mmap) stopped the render server and SpringBoard with an exception, so the shell resprung. --- bridge/NativeMetalBridge.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/bridge/NativeMetalBridge.rs b/bridge/NativeMetalBridge.rs index 4c8d698..997735f 100644 --- a/bridge/NativeMetalBridge.rs +++ b/bridge/NativeMetalBridge.rs @@ -11,11 +11,8 @@ #[path = "NativeMetalObjects.rs"] pub mod objects; -/// The runtime allocates and frees on every encoder call (binding tables, recorded calls, tracked uses); mimalloc's thread-local free -/// lists take those small blocks for less than the system allocator, whose frees from the encoding thread also gave pages back to the -/// kernel (madvise) as they emptied. Every allocation of the bridge and the runtime is the Rust side's own, freed there. -#[global_allocator] -static ALLOCATOR: mimalloc::MiMalloc = mimalloc::MiMalloc; +// The system allocator: mimalloc reserved its arenas with large mmaps, and on iPadOS 27 the kernel stopped the render server and +// SpringBoard with an exception inside that mmap (unix_mmap_prim_aligned from mi_arena_reserve), so the shell resprung. use metal2vulkan::metal::{PixelFormat, PrimitiveType, TextureUsage}; use metal2vulkan::pipeline::{ColorAttachmentDescriptor, RenderPipelineDescriptor, StageSource}; From f7bf252816a8374bd389ab08e112379b383db173 Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Wed, 28 Oct 2026 20:13:16 +0800 Subject: [PATCH 59/92] feat(metal): pass linked functions to render pipelines Render pipeline descriptors carry vertexLinkedFunctions and fragmentLinkedFunctions to the runtime (iconservicesagent's custom blend pipeline needs them); binary functions stay refused. Also answer the compute descriptor selectors, setResponsibleProcess:, iosurfaceReadOnlyTextureAlignmentBytes and newSharedEventWithOptions:, fall back to a plain texture for automatic heaps, and report the latest real problem. --- NativeBridgeMetalFrontend.mm | 90 ++++++++++++++++++++++++++++++++---- bridge/NativeMetalObjects.h | 6 +++ bridge/NativeMetalObjects.rs | 23 ++++++++- 3 files changed, 108 insertions(+), 11 deletions(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 2a62a28..9ff51ae 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -368,13 +368,29 @@ static char gLastProblems[2][200]; static char gFirstProblem[200]; // Called once with the first problem, on the thread that hit it: a polling reader missed WidgetRenderer's, which crashed within its first second. void (*native_bridge_metal_problem_hook)(const char *); +// Shader priming's thread: its refusals (a vertex function primed without the vertex descriptor its app gives it) are counted, not +// reported, so the first problem and the last ones stay the clients' own. +static thread_local bool tQuietProblems; +extern "C" void native_bridge_metal_quiet_problems(bool quiet) {tQuietProblems=quiet;} +// Notes every process makes and that change nothing it draws: they must not stand in the one line a client's failure is reported on. +static bool harmlessProblem(NSString *what) { + for(NSString *prefix in @[@"native-metal: no device answer",@"native-metal: setGPUPriority",@"native-metal: setBackgroundGPUPriority", + @"native-metal: binary archive refused",@"native-metal: texture layouts are"]) + if([what hasPrefix:prefix])return true; + return false; +} static void noteProblem(NSString *what) { gProblems++; + if(tQuietProblems)return; std::lock_guard lock(gProblemLock); - // A device answer the runtime lacks is reported at creation in every process and is harmless: the first problem is the next one. - if(!gFirstProblem[0] && ![what hasPrefix:@"native-metal: no device answer"]) { - strlcpy(gFirstProblem,what.UTF8String?:"?",sizeof gFirstProblem); - if(native_bridge_metal_problem_hook)native_bridge_metal_problem_hook(gFirstProblem); + // The client's line shows its latest real problem (a later failure is what ends a process; the first one was often a harmless note). + if(!harmlessProblem(what)) { + char line[sizeof gFirstProblem]; + strlcpy(line,what.UTF8String?:"?",sizeof line); + if(strcmp(line,gFirstProblem)) { + memcpy(gFirstProblem,line,sizeof line); + if(native_bridge_metal_problem_hook)native_bridge_metal_problem_hook(gFirstProblem); + } } memcpy(gLastProblems[1],gLastProblems[0],sizeof gLastProblems[0]); strlcpy(gLastProblems[0],what.UTF8String?:"?",sizeof gLastProblems[0]); @@ -639,7 +655,15 @@ static NSMethodSignature *missingSignature(id self,SEL selector) { noteProblem([NSString stringWithFormat:@"missing %s (answered nil)",key.c_str()]); return [NSMethodSignature signatureWithObjCTypes:"@@:"]; } -static void answerMissing(NSInvocation *invocation) {void *zero=nullptr;[invocation setReturnValue:&zero];} +// The objects declare their Metal protocols, so a selector they lack can come with the protocol's own signature: a void method takes +// no return value (setting one traps in NSInvocation, SpringBoard crashed at launch) and a struct or double is answered with zeroes +// of its own size. +static void answerMissing(NSInvocation *invocation) { + const NSUInteger length=invocation.methodSignature.methodReturnLength; + if(!length)return; + alignas(16) unsigned char zero[256]={}; + if(length<=sizeof zero)[invocation setReturnValue:zero]; +} @interface NBObject : NSObject @property(atomic,copy) NSString *label; @@ -651,6 +675,8 @@ static void answerMissing(NSInvocation *invocation) {void *zero=nullptr;[invocat return signature?signature:missingSignature(self,selector); } - (void)forwardInvocation:(NSInvocation *)invocation {answerMissing(invocation);} +// SPI that attributes a resource's memory to another process (iPadOS 27 QuartzCore sends it for every buffer and texture). Nothing to attribute. +- (void)setResponsibleProcess:(int)pid {(void)pid;} @end // ---- resources ---- @@ -2812,11 +2838,31 @@ static void journalRenderPipeline(MTLRenderPipelineDescriptor *d,uint64_t began, journal(line); } // A render pipeline descriptor in the bridge's form; false, with the error, for a function of another device. -static bool renderDescriptor(MTLRenderPipelineDescriptor *d,NmbRenderPipelineDescriptor *p,NSError **error) { +// A render descriptor with the storage its linked-function arrays point into (it outlives every call that reads the descriptor). +struct NBRenderDescriptor : NmbRenderPipelineDescriptor {std::vector linked[4];}; +static bool linkedHandles(NSArray> *functions,std::vector &out); +static bool renderDescriptor(MTLRenderPipelineDescriptor *d,NBRenderDescriptor *p,NSError **error) { if(![(id)d.vertexFunction isKindOfClass:[NBFunction class]] || (d.fragmentFunction && ![(id)d.fragmentFunction isKindOfClass:[NBFunction class]])) { if(error)*error=nbError(@"foreign function");return false; } p->vertex=[(NBFunction *)d.vertexFunction handle];p->fragment=d.fragmentFunction?[(NBFunction *)d.fragmentFunction handle]:nullptr; + // Each stage's visible functions (MTLRenderPipelineDescriptor vertexLinkedFunctions/fragmentLinkedFunctions): RenderBox's fragment + // functions call custom_blend through them; without them the runtime refused the pipeline (MissingInput LinkedFunction) and the render + // server aborted in CA::OGL::MetalContext::create_pipeline_state. + MTLLinkedFunctions *stages[2]={d.vertexLinkedFunctions,d.fragmentLinkedFunctions}; + for(unsigned stage=0;stage<2;++stage) { + MTLLinkedFunctions *linked=stages[stage]; + if(linked.binaryFunctions.count || linked.groups.count) { + if(error)*error=nbError(@"render binary/grouped linked functions are not implemented");return false; + } + if(!linkedHandles(linked.functions,p->linked[stage*2]) || !linkedHandles(linked.privateFunctions,p->linked[stage*2+1])) { + if(error)*error=nbError(@"foreign linked function");return false; + } + } + p->vertex_linked=p->linked[0].data();p->vertex_linked_count=p->linked[0].size(); + p->vertex_private_linked=p->linked[1].data();p->vertex_private_linked_count=p->linked[1].size(); + p->fragment_linked=p->linked[2].data();p->fragment_linked_count=p->linked[2].size(); + p->fragment_private_linked=p->linked[3].data();p->fragment_private_linked_count=p->linked[3].size(); for(unsigned i=0;i<8;i++) { MTLRenderPipelineColorAttachmentDescriptor *a=d.colorAttachments[i]; p->colors[i]={(uint64_t)a.pixelFormat,(uint64_t)a.writeMask,(uint64_t)a.blendingEnabled,(uint64_t)a.sourceRGBBlendFactor,(uint64_t)a.destinationRGBBlendFactor, @@ -3208,7 +3254,15 @@ static char dvmMemoryReleaseKey; - (id)textureWithDescriptor:(MTLTextureDescriptor *)d offset:(uint64_t)offset { NmbTextureDescriptor descriptor;if(!textureDescriptor(d,&descriptor))return nil; NmbTexture *handle=nmb_heap_new_texture(_handle,&descriptor,offset); - if(!handle){NBLOG(@"native-metal: heap texture refused: %@",lastError());return nil;} + // An automatic heap promises its textures no shared memory, only a place: one the heap cannot take is made on its own, so the caller + // (Core Image's intermediate images, iconservicesagent, i-33765e1ad) gets a texture instead of nil, which it dereferenced. A placement + // heap's texture aliases the memory at its offset; that one stays refused. + if(!handle) { + NBLOG(@"native-metal: heap texture refused: %@",lastError()); + if(offset!=UINT64_MAX)return nil; + handle=nmb_texture_new(gDevice,&descriptor); + if(!handle){NBLOG(@"native-metal: texture refused: %@",lastError());return nil;} + } NBTexture *texture=[NBTexture new];texture.handle=handle;nmb_texture_describe(handle,&descriptor);texture.descriptor=descriptor; if(isPvrtc(d.pixelFormat))texture.pvrtc=(uint32_t)d.pixelFormat; texture.parentHeap=(id)self;texture.heapOffset=offset==UINT64_MAX?0:offset;return (id)texture; @@ -3278,7 +3332,7 @@ static bool computeDescriptor(MTLComputePipelineDescriptor *d,NmbComputePipeline - (void)dealloc {nmb_binary_archive_release(_handle);} - (NSUInteger)options {return 0;} - (BOOL)addRenderPipelineFunctionsWithDescriptor:(MTLRenderPipelineDescriptor *)d error:(NSError **)error { - auto *p=new NmbRenderPipelineDescriptor(); + auto *p=new NBRenderDescriptor(); const bool ok=renderDescriptor(d,p,error) && nmb_binary_archive_add_render(_handle,p);delete p; if(!ok)return failWith(error,@"archive render pipeline",NO); if(error)*error=nil;return YES; @@ -3319,6 +3373,11 @@ static bool pipelineArchives(NSArray *list,std::vector &arch - (NSUInteger)minimumTextureBufferAlignmentForPixelFormat:(NSUInteger)format { return ((NSUInteger (*)(id,SEL,NSUInteger))objc_msgSend)(self,sel_registerName("minimumLinearTextureAlignmentForPixelFormat:"),format); } +// SPI: the row alignment a read-only texture over an IOSurface needs. Answered unasked it was 0 (a divisor for its callers); the linear +// texture alignment of the widest common format is the same rule this GPU applies to any linear image. +- (NSUInteger)iosurfaceReadOnlyTextureAlignmentBytes { + return ((NSUInteger (*)(id,SEL,NSUInteger))objc_msgSend)(self,sel_registerName("minimumLinearTextureAlignmentForPixelFormat:"),MTLPixelFormatRGBA16Float); +} - (uint64_t)registryID {return gRegistryID;} - (NSUInteger)maxBufferLength {return nmb_device_max_buffer_length(gDevice);} - (BOOL)hasUnifiedMemory {return YES;} @@ -3370,6 +3429,8 @@ static bool pipelineArchives(NSArray *list,std::vector &arch NmbEvent *handle=nmb_event_new(gDevice,true);if(!handle)return nil; NBSharedEvent *event=[NBSharedEvent new];event.handle=handle;return (id)event; } +// The options only choose Apple's event storage; the event is the same. +- (id)newSharedEventWithOptions:(NSUInteger)options {(void)options;return [(id)self newSharedEvent];} - (id)newFence { NmbFence *handle=nmb_fence_new(gDevice);if(!handle)return nil; NBFence *fence=[NBFence new];fence.handle=handle;return (id)fence; @@ -3635,13 +3696,24 @@ static bool pipelineArchives(NSArray *list,std::vector &arch NBComputePipeline *state=[NBComputePipeline new];state.handle=handle;state.label=d.label;gPipelines++; if(error)*error=nil;return (id)state; } +// iPadOS 27's QuartzCore builds its backdrop compute pipelines (Liquid Glass luma, backdrop capture) with this short form: unanswered, the +// render server got nil and aborted with "Metal failed to build compute pipeline" on the first backdrop (a respring). +- (id)newComputePipelineStateWithDescriptor:(MTLComputePipelineDescriptor *)d error:(NSError **)error { + return [(id)self newComputePipelineStateWithDescriptor:d options:MTLPipelineOptionNone reflection:nil error:error]; +} +- (void)newComputePipelineStateWithDescriptor:(MTLComputePipelineDescriptor *)d options:(MTLPipelineOption)options + completionHandler:(MTLNewComputePipelineStateWithReflectionCompletionHandler)handler { + NSError *error=nil;MTLComputePipelineReflection *reflection=nil; + id state=[(id)self newComputePipelineStateWithDescriptor:d options:options reflection:&reflection error:&error]; + handler(state,reflection,error); +} - (id)newRenderPipelineStateWithDescriptor:(MTLRenderPipelineDescriptor *)d error:(NSError **)error { return [(id)self newRenderPipelineStateWithDescriptor:d options:MTLPipelineOptionNone reflection:nil error:error]; } - (id)newRenderPipelineStateWithDescriptor:(MTLRenderPipelineDescriptor *)d options:(MTLPipelineOption)options reflection:(MTLAutoreleasedRenderPipelineReflection *)reflection error:(NSError **)error { if(reflection)*reflection=nil; - auto *p=new NmbRenderPipelineDescriptor(); + auto *p=new NBRenderDescriptor(); if(!renderDescriptor(d,p,error)) { static std::atomic told{0}; if(told.fetch_add(1)<64)NBLOG(@"native-metal: render pipeline descriptor refused (vertex %@, fragment %@): %@",d.vertexFunction.name, diff --git a/bridge/NativeMetalObjects.h b/bridge/NativeMetalObjects.h index cf0df54..ab01abc 100644 --- a/bridge/NativeMetalObjects.h +++ b/bridge/NativeMetalObjects.h @@ -159,6 +159,12 @@ struct NmbRenderPipelineDescriptor { uint64_t tessellates, max_factor, partition_mode, factor_format, factor_step_function, output_winding, control_point_index_type, factor_scale_enabled; } tessellation; + /* vertexLinkedFunctions / fragmentLinkedFunctions .functions and .privateFunctions: visible functions each stage calls (RenderBox's + * custom_blend fragment functions call theirs this way). Zero counts: none. */ + NmbFunction *const *vertex_linked; uint64_t vertex_linked_count; + NmbFunction *const *vertex_private_linked; uint64_t vertex_private_linked_count; + NmbFunction *const *fragment_linked; uint64_t fragment_linked_count; + NmbFunction *const *fragment_private_linked; uint64_t fragment_private_linked_count; }; NmbRenderPipeline *nmb_render_pipeline_new(NmbDevice *, const struct NmbRenderPipelineDescriptor *); /* Descriptor binaryArchives and MTLPipelineOption bits. NULL options means defaults. diff --git a/bridge/NativeMetalObjects.rs b/bridge/NativeMetalObjects.rs index c8bf6fe..744d794 100644 --- a/bridge/NativeMetalObjects.rs +++ b/bridge/NativeMetalObjects.rs @@ -1475,6 +1475,14 @@ pub struct NmbRenderPipelineDescriptor { pub attributes: [NmbVertexAttribute; 31], pub layouts: [NmbVertexLayout; 31], pub tessellation: NmbTessellation, + pub vertex_linked: *const *mut NmbFunction, + pub vertex_linked_count: u64, + pub vertex_private_linked: *const *mut NmbFunction, + pub vertex_private_linked_count: u64, + pub fragment_linked: *const *mut NmbFunction, + pub fragment_linked_count: u64, + pub fragment_private_linked: *const *mut NmbFunction, + pub fragment_private_linked_count: u64, } /// A post-tessellation vertex function's state; `tessellates` 0 leaves the descriptor's tessellation unset. @@ -1541,9 +1549,20 @@ pub unsafe extern "C" fn nmb_render_pipeline_new_with_options(device: *mut NmbDe fn render_descriptor(descriptor: *const NmbRenderPipelineDescriptor) -> Result { let d = get(descriptor)?; - let mut out = RenderPipelineDescriptor::new(StageSource::Function(get(d.vertex)?.0.function_descriptor())); + // Each stage's linked functions ride on its function descriptor, as the compute pipeline's do (compute_descriptor). + let stage_linked = |functions, count, private, private_count| -> Result { + let mut linked = LinkedFunctions::new(); + linked.functions = linked_list(functions, count)?; + linked.private_functions = linked_list(private, private_count)?; + Ok(linked) + }; + let vertex = get(d.vertex)?.0.function_descriptor() + .with_linked(stage_linked(d.vertex_linked, d.vertex_linked_count, d.vertex_private_linked, d.vertex_private_linked_count)?); + let mut out = RenderPipelineDescriptor::new(StageSource::Function(vertex)); if !d.fragment.is_null() { - out.fragment = Some(StageSource::Function(get(d.fragment)?.0.function_descriptor())); + let fragment = get(d.fragment)?.0.function_descriptor().with_linked(stage_linked( + d.fragment_linked, d.fragment_linked_count, d.fragment_private_linked, d.fragment_private_linked_count)?); + out.fragment = Some(StageSource::Function(fragment)); } for (slot, c) in out.color_attachments.iter_mut().zip(d.colors.iter()) { if c.format == 0 { From e25c5a4dda704abb8b2195454872b4888009dcbb Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Wed, 28 Oct 2026 20:13:16 +0800 Subject: [PATCH 60/92] 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. --- NativeAdrenoMetalPlugin.mm | 9 ++++++++- PrimeShaders.h | 2 ++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/NativeAdrenoMetalPlugin.mm b/NativeAdrenoMetalPlugin.mm index 2bf0222..6c74534 100644 --- a/NativeAdrenoMetalPlugin.mm +++ b/NativeAdrenoMetalPlugin.mm @@ -150,8 +150,15 @@ static void reportDisplayState(Gpu &gpu) { static unsigned gClientPort; static void clientFailure(unsigned port, const char *step, const char *detail) { if (Gpu *gpu = makeGpu(port)) { + // The row holds 63 characters: the common prefix goes, and a long reason keeps its start (what was refused) and its end (why). + if (!detail) detail = ""; + if (!strncmp(detail, "native-metal: ", 14)) detail += 14; + char shown[48]; + const size_t length = strlen(detail); + if (length <= 46) strlcpy(shown, detail, sizeof shown); + else snprintf(shown, sizeof shown, "%.18s~%s", detail, detail + length - 27); char text[64]; - snprintf(text, sizeof text, "%.14s %.46s", step, detail ? detail : ""); + snprintf(text, sizeof text, "%.14s %s", step, shown); streamText(*gpu, text); } } diff --git a/PrimeShaders.h b/PrimeShaders.h index b8d453e..530e0ed 100644 --- a/PrimeShaders.h +++ b/PrimeShaders.h @@ -36,6 +36,7 @@ extern "C" unsigned native_bridge_metal_recent_commits(uint64_t windowUs); // saw in 250 ms. extern "C" {volatile uint32_t native_prime_compiling, native_prime_libraries_done, native_prime_libraries_total, native_prime_made, native_prime_rate;} +extern "C" void native_bridge_metal_quiet_problems(bool); namespace prime { static const char *const kRoots[] = {"/System/Library", "/Applications", "/System/Cryptexes/OS/System/Library"}; @@ -291,6 +292,7 @@ static std::string library(id device, const std::string &path, const // The thread: waits for the desktop to settle, then works through the libraries not finished yet. static void run(id device, std::string root, unsigned delaySeconds, const std::string &listPath) { pthread_set_qos_class_self_np(QOS_CLASS_BACKGROUND, 0); + native_bridge_metal_quiet_problems(true); while (root.size() > 1 && root.back() == '/') root.pop_back(); const std::string donePath = root + "/prime-done", badPath = root + "/prime-bad", current = root + "/prime-current", logPath = root + "/prime.log"; From adc18c5d594405d368f6b3e113803a01c011fd3c Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Wed, 28 Oct 2026 22:49:33 +0800 Subject: [PATCH 61/92] 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. --- NativeBridgeMetalFrontend.h | 2 + NativeBridgeMetalFrontend.mm | 150 +++++++++++++----- ...-mipmaps-outside-the-measured-spread.patch | 71 +++++++++ ...ure-size-a-multiple-of-its-alignment.patch | 14 ++ bridge/present_host.mm | 2 +- bridge/surface_import_host.mm | 27 +++- 6 files changed, 218 insertions(+), 48 deletions(-) create mode 100644 bridge/patches/0022-mipmaps-outside-the-measured-spread.patch create mode 100644 bridge/patches/0023-heap-texture-size-a-multiple-of-its-alignment.patch diff --git a/NativeBridgeMetalFrontend.h b/NativeBridgeMetalFrontend.h index a50ad2d..913b31e 100644 --- a/NativeBridgeMetalFrontend.h +++ b/NativeBridgeMetalFrontend.h @@ -28,5 +28,7 @@ bool native_bridge_metal_work_stats(uint64_t out[10]); // 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); } #endif diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 9ff51ae..0e8c709 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -144,7 +144,6 @@ static void (*gMissing)(const char *, const char *); // What this device has been asked and refused, for the plugin's status line (native_bridge_metal_status): commits, render/compute pipelines made, // refusals, and the last two refusals' texts. Counters only; nothing here changes a result. -static constexpr uint64_t kWaitBoundMs=4000; static std::atomic gCommits,gCommitFailures,gPipelines,gProblems,gCompleted,gWaitTimeouts; // What the render server actually asks of the GPU (int-141: 59 commits in 200 s, and a desktop that behaves like CPU compositing): render passes, // draws, the pixels of their first colour attachment, compute dispatches, blits and the largest pass (pixels). @@ -451,8 +450,15 @@ static void logTexture(const char *kind,MTLTextureDescriptor *d,IOSurfaceRef sur // planes, requested plane, plane offset, plane row bytes and allocation, and the descriptor's size and pixel format. struct SurfaceRefusal {uint32_t step,format,planes,plane,pixelFormat;uint64_t offset,row,allocation,width,height,surfaceWidth,surfaceHeight,rotation;bool framebufferOnly;}; static SurfaceRefusal gSurfaceLastRefusal; -// Shader priming's progress (PrimeShaders.h, in the plugin), for the PR row. -extern "C" volatile uint32_t native_prime_compiling,native_prime_libraries_done,native_prime_libraries_total,native_prime_made,native_prime_rate; +// Shader priming's progress (PrimeShaders.h, in the plugin), for the PR row. Weak here: the plugin's own definition wins, and a host +// binary that links the frontend without the plugin (every check-*-host.py test) has zeroes instead of an undefined symbol. +extern "C" { +__attribute__((weak)) volatile uint32_t native_prime_compiling; +__attribute__((weak)) volatile uint32_t native_prime_libraries_done; +__attribute__((weak)) volatile uint32_t native_prime_libraries_total; +__attribute__((weak)) volatile uint32_t native_prime_made; +__attribute__((weak)) volatile uint32_t native_prime_rate; +} static void refuseSurface(uint32_t step,IOSurfaceRef surface,MTLTextureDescriptor *d,NSUInteger plane,uint64_t offset,uint64_t row) { gSurfaceRefusals++; SurfaceRefusal r={step,surface?IOSurfaceGetPixelFormat(surface):0,surface?(uint32_t)IOSurfaceGetPlaneCount(surface):0,(uint32_t)plane, @@ -544,7 +550,29 @@ static NSData *codeGenRequest(uint32_t type,const std::vector &request, static dispatch_once_t once; static std::mutex lock; dispatch_once(&once,^{ - void *library=dlopen("/System/Library/PrivateFrameworks/MTLCompiler.framework/MTLCompiler",RTLD_NOW); + static const char kFramework[]="/System/Library/PrivateFrameworks/MTLCompiler.framework"; + // The top-level path, then the version the framework's own Current link names (macOS), then the highest version directory: iOS 27 + // ships the framework with no top-level link and no Current (Versions/32023 and 32024 in the shared cache, each directory holding only + // its Info.plist on disk), and every version's library exports the same codegen service. Without one, every stitched library (Core + // Image's create_metal_dag, RenderBox's custom_blend) and every source compile was refused. + std::string path=std::string(kFramework)+"/MTLCompiler"; + void *library=dlopen(path.c_str(),RTLD_NOW); + if(!library && !access((std::string(kFramework)+"/Versions/Current/MTLCompiler").c_str(),R_OK)) { + path=std::string(kFramework)+"/Versions/Current/MTLCompiler"; + library=dlopen(path.c_str(),RTLD_NOW); + } + if(!library) { + unsigned long highest=0; + for(NSString *entry in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[@(kFramework) stringByAppendingString:@"/Versions"] error:nil]) { + const unsigned long version=strtoul(entry.UTF8String,nullptr,10); + if(version>highest && [entry isEqualToString:[NSString stringWithFormat:@"%lu",version]])highest=version; + } + if(highest) { + path=std::string(kFramework)+"/Versions/"+std::to_string(highest)+"/MTLCompiler"; + library=dlopen(path.c_str(),RTLD_NOW); + } + } + if(library && path!=std::string(kFramework)+"/MTLCompiler")NSLog(@"native-metal: Metal compiler loaded from %s",path.c_str()); void *(*create)(const char *)=library?(void *(*)(const char *))dlsym(library,"MTLCodeGenServiceCreate"):nullptr; build=library?(void (*)(void *,void *,uint32_t,const void *,size_t,NBCodeGenReply))dlsym(library,"MTLCodeGenServiceBuildRequest"):nullptr; service=create && build?create("native-metal"):nullptr; @@ -753,43 +781,68 @@ NB_RESOURCE_SPI static NSMapTable *gSurfaceImports; static std::mutex gSurfaceImportLock; // Core Animation makes a texture of each window surface at every frame and drops it when the frame completes, so the weak table -// lost the import between frames and each frame imported every surface again (allocate, map, free). An import used in the last -// second stays held (strongly, at most 64): the pages are the surface's own and the key includes identity, address and size. +// lost the import between frames and each frame imported every surface again (allocate, map, free). As Metal keeps a surface's GPU +// mapping for as long as the surface lives, an import stays held while anything else in this process still retains its surface (the +// import's own retain is the only one left once the surface is gone), and everything held goes back when the system reports memory +// pressure. No count or age limit: held imports whose surface is gone are let go at the next import, at every command buffer's +// completion and on memory pressure (native_bridge_metal_trim_surface_imports). #include #include -struct HeldImport {NBBuffer *buffer;uint64_t usedUs;}; -static std::map,HeldImport> gHeldImports; // under gSurfaceImportLock -static void holdImport(uint64_t identity,uint64_t address,uint64_t bytes,NBBuffer *buffer) { - const uint64_t now=nowUs();gHeldImports[{identity,address,bytes}]={buffer,now}; - for(auto it=gHeldImports.begin();it!=gHeldImports.end();)it=now-it->second.usedUs>1000000?gHeldImports.erase(it):std::next(it); - while(gHeldImports.size()>64) { - auto oldest=gHeldImports.begin(); - for(auto it=gHeldImports.begin();it!=gHeldImports.end();++it)if(it->second.usedUssecond.usedUs)oldest=it; - gHeldImports.erase(oldest); +struct HeldImport {NBBuffer *buffer;IOSurfaceRef surface;}; +// Under gSurfaceImportLock. Never destroyed: a completion thread may still trim it while the process exits. +static auto &gHeldImports=*new std::map,HeldImport>; +static NSUInteger gSurfaceImportsPruned=64; // the table size at which dead associations are next pruned +// Drops (into `gone`, released by the caller outside the lock) every held import whose surface only the import still retains, or every +// one when `all`. +static void pruneHeldImports(bool all,std::vector &gone) { + for(auto it=gHeldImports.begin();it!=gHeldImports.end();) { + if(all || CFGetRetainCount(it->second.surface)<=1){gone.push_back(it->second.buffer);it=gHeldImports.erase(it);} + else ++it; } } +static void trimHeldImports(bool all) { + std::vector gone; + {std::lock_guard lock(gSurfaceImportLock);pruneHeldImports(all,gone);} + gone.clear(); // the imports' last frontend references go here, outside the lock +} +extern "C" void native_bridge_metal_trim_surface_imports(void) {trimHeldImports(false);} +static void watchImportPressure() { + static dispatch_once_t once; + dispatch_once(&once,^{ + dispatch_source_t source=dispatch_source_create(DISPATCH_SOURCE_TYPE_MEMORYPRESSURE,0, + DISPATCH_MEMORYPRESSURE_WARN|DISPATCH_MEMORYPRESSURE_CRITICAL,dispatch_get_global_queue(QOS_CLASS_UTILITY,0)); + if(!source)return; + dispatch_source_set_event_handler(source,^{trimHeldImports(true);}); + dispatch_activate(source); + }); +} +static void holdImport(uint64_t identity,uint64_t address,uint64_t bytes,NBBuffer *buffer,IOSurfaceRef surface,std::vector &gone) { + pruneHeldImports(false,gone); + gHeldImports[{identity,address,bytes}]={buffer,surface}; +} static NBBuffer *surfaceBuffer(IOSurfaceRef surface,void *base,size_t bytes) { struct Key {uint64_t identity,address,bytes;}; const Key value={IOSurfaceGetID(surface),(uintptr_t)base,bytes}; NSValue *key=value.identity?[NSValue valueWithBytes:&value objCType:@encode(Key)]:nil; + watchImportPressure(); + std::vector gone; // declared before the lock: released after it std::lock_guard lock(gSurfaceImportLock); if(!gSurfaceImports)gSurfaceImports=[NSMapTable strongToWeakObjectsMapTable]; NBBuffer *buffer=key?[gSurfaceImports objectForKey:key]:nil; - if(buffer){if(key)holdImport(value.identity,value.address,value.bytes,buffer);return buffer;} + if(buffer){if(key)holdImport(value.identity,value.address,value.bytes,buffer,surface,gone);return buffer;} // The surface keeps its CPU mapping (the imported pages) until the import is freed. NmbBuffer *handle=nmb_buffer_new_no_copy_releasing(gDevice,base,bytes,MTLResourceStorageModeShared,releaseSurface,(void *)CFRetain(surface)); if(!handle){CFRelease(surface);NBLOG(@"native-metal: surface import refused: %@",lastError());return nil;} gSurfaceImportsMade++;buffer=[NBBuffer new];buffer.handle=handle;buffer.options=MTLResourceStorageModeShared; if(key) { - // Bound metadata without retaining old frames. Evicting an association is - // harmless: its owners still retain the actual import until their own end. - if(gSurfaceImports.count>=256) { + // Associations whose import is gone are pruned whenever the table has doubled since the last pruning; a live one is never evicted. + if(gSurfaceImports.count>=gSurfaceImportsPruned) { for(NSValue *old in gSurfaceImports.keyEnumerator.allObjects) if(![gSurfaceImports objectForKey:old])[gSurfaceImports removeObjectForKey:old]; - if(gSurfaceImports.count>=256)[gSurfaceImports removeObjectForKey:gSurfaceImports.keyEnumerator.nextObject]; + gSurfaceImportsPruned=MAX(gSurfaceImports.count*2,(NSUInteger)64); } [gSurfaceImports setObject:buffer forKey:key]; - holdImport(value.identity,value.address,value.bytes,buffer); + holdImport(value.identity,value.address,value.bytes,buffer,surface,gone); } return buffer; } @@ -1644,14 +1697,19 @@ static void encodeDrain() { t.idle.wait(lock,[&]{return t.done>=target;}); } #define ENCODE(...) encodeLater([=]{__VA_ARGS__;}) +// Held while a resource wait reads an in-flight command buffer's runtime handle and while a locally refused command buffer frees it. +static std::mutex gRuntimeHandleLock; static void waitForResource(id resource) { encodeDrain(); // every committed command buffer's uses recorded const uint64_t began=nowUs(); NSArray *pending;@synchronized(gInFlight){pending=gInFlight.allObjects;} for(NBCommandBuffer *commandBuffer in pending) { int dependency=-1; - if([resource isKindOfClass:[NBBuffer class]])dependency=nmb_command_buffer_uses_buffer(commandBuffer.handle,[(NBBuffer *)resource handle]); - else if([resource isKindOfClass:[NBTexture class]])dependency=nmb_command_buffer_uses_texture(commandBuffer.handle,[(NBTexture *)resource handle]); + { + std::lock_guard guard(gRuntimeHandleLock); + if([resource isKindOfClass:[NBBuffer class]])dependency=nmb_command_buffer_uses_buffer(commandBuffer.handle,[(NBBuffer *)resource handle]); + else if([resource isKindOfClass:[NBTexture class]])dependency=nmb_command_buffer_uses_texture(commandBuffer.handle,[(NBTexture *)resource handle]); + } gResourceChecks++; // An unknown result retains the old conservative wait. False means no matching storage // (or GPU work already completed), not a fabricated completion for a pending dependency. @@ -2156,7 +2214,10 @@ static void writeDrawStats() { } - (void)generateMipmapsForTexture:(id)t { if(![self textures:NB_OBJECTS(t) count:1])return; - ENCODE([self check:nmb_blit_generate_mipmaps(self.handle,[(NBTexture *)t handle]) what:"generateMipmapsForTexture"]); + // A refusal names the texture's pixel format, type and levels: what decides whether the runtime can filter it. + const std::string what=[NSString stringWithFormat:@"generateMipmapsForTexture: (pixel format %lu, type %lu, %lu levels)", + (unsigned long)t.pixelFormat,(unsigned long)t.textureType,(unsigned long)t.mipmapLevelCount].UTF8String; + ENCODE([self check:nmb_blit_generate_mipmaps(self.handle,[(NBTexture *)t handle]) what:what.c_str()]); } - (void)synchronizeResource:(id)r {(void)r;} @end @@ -2364,7 +2425,10 @@ static char kUserDictionaryKey; } - (MTLCommandBufferStatus)status { if(self.failedLocally)return MTLCommandBufferStatusError; - if(self.committed && !self.submitted)return MTLCommandBufferStatusCommitted; + // The runtime submits on the committing thread: a commit that submits its own last batch runs the scheduled handlers before + // nmb_command_buffer_commit returns, and Metal's status reads Scheduled inside them (as its wait returns after them); the frontend's + // own `submitted` is only set after the call, so the runtime's status answers once scheduling has been delivered. + if(self.committed && !self.submitted && !_schedulingDelivered)return MTLCommandBufferStatusCommitted; return (MTLCommandBufferStatus)nmb_command_buffer_status(_handle); } - (NSError *)error { @@ -2392,8 +2456,8 @@ static char kUserDictionaryKey; - (CFTimeInterval)kernelStartTime {return 0;} - (CFTimeInterval)kernelEndTime {return 0;} - (BOOL)commitAndWaitUntilSubmitted {[(id)self commit];[self waitUntilScheduled];return !self.failedLocally;} -- (CFTimeInterval)GPUStartTime {return self.submitted?nmb_command_buffer_gpu_time(_handle,false):0;} -- (CFTimeInterval)GPUEndTime {return self.submitted?nmb_command_buffer_gpu_time(_handle,true):0;} +- (CFTimeInterval)GPUStartTime {return self.submitted && !self.failedLocally?nmb_command_buffer_gpu_time(_handle,false):0;} +- (CFTimeInterval)GPUEndTime {return self.submitted && !self.failedLocally?nmb_command_buffer_gpu_time(_handle,true):0;} // An encoder whose runtime half `make` makes, on the encoding thread when that is on. A refused encoder is still an object: its calls // fail on the null handle and the buffer completes with an error. RenderBox caches the encoder's IMPs with methodForSelector:, and on // nil it called address 0 (i568: WidgetRenderer_Default crashed eight launches running, then hit its open cap and lost the device). @@ -2537,6 +2601,16 @@ static char kUserDictionaryKey; // release point. A retained completed command must not pin every old frame. [_surfaces removeAllObjects];[_resourceOwners removeAllObjects]; @synchronized(gInFlight){[gInFlight removeObject:self];} + // Surfaces this command buffer kept alive may now have no other owner: their held imports go. + trimHeldImports(false); + // A local refusal never reaches the runtime's completion: its runtime command buffer, which holds every resource its encoders + // recorded, goes now, as Metal releases a failed command buffer's resources once it completes with its error, however long the + // caller keeps the command buffer object. Every later answer of it is the frontend's (failedLocally). + if(self.failedLocally) { + std::lock_guard guard(gRuntimeHandleLock); + NmbCommandBuffer *handle=_handle;_handle=nullptr; + nmb_command_buffer_release(handle); + } NSArray *completedHandlers; @synchronized(self){ // A frontend refusal or a failed submission never scheduled. Unblock its waiter without inventing a scheduled event. @@ -2618,18 +2692,14 @@ static char kUserDictionaryKey; if(!self.committed)return; encodeDrain(); const uint64_t began=nowUs(); - // Bounded (kWaitBoundMs): a completion the bridge never delivers must not park the caller for good (the scanout worker would never call - // COMPLETE_SWAP and the display's 5 s SwapWait fires). The bound is above a slow first-use pipeline compile; on timeout the command buffer - // stays in flight and the caller sees a status that is not completed. - if(!self.failedLocally && nmb_command_buffer_wait(_handle,kWaitBoundMs)!=0) { + // As Metal's waitUntilCompleted: until the command buffer completes, with an error when the GPU was restarted under it (the kernel's + // hangcheck ends a hang and the client whose work hung sees its device lost), never after a fixed time. + if(!self.failedLocally && nmb_command_buffer_wait(_handle,0)!=0) { gWaitTimeouts++; - NBLOG(@"native-metal: completion wait timed out after %d ms (status %llu)",(int)kWaitBoundMs,(unsigned long long)nmb_command_buffer_status(_handle)); + NBLOG(@"native-metal: completion wait failed (status %llu)",(unsigned long long)nmb_command_buffer_status(_handle)); } const uint32_t cpuStart=threadCpuUs(); - if(dispatch_group_wait(_handlers,dispatch_time(DISPATCH_TIME_NOW,(int64_t)kWaitBoundMs*1000*1000))!=0) { - gWaitTimeouts++; - NBLOG(@"native-metal: completion handlers timed out (the bridge did not deliver the completion)"); - } + dispatch_group_wait(_handlers,DISPATCH_TIME_FOREVER); { TraceRec rec={};rec.type=2;rec.t0=began;rec.t1=nowUs();rec.tid=threadIdLow();rec.a=(uint32_t)(uintptr_t)__builtin_return_address(0); rec.cpu0=cpuStart;rec.cpu1=threadCpuUs();traceNote(rec.tid);tracePush(rec); @@ -3258,10 +3328,10 @@ static char dvmMemoryReleaseKey; // (Core Image's intermediate images, iconservicesagent, i-33765e1ad) gets a texture instead of nil, which it dereferenced. A placement // heap's texture aliases the memory at its offset; that one stays refused. if(!handle) { - NBLOG(@"native-metal: heap texture refused: %@",lastError()); + NBLOG(@"native-metal: heap texture (fmt %lu %lux%lu, %lu levels) refused: %@",(unsigned long)d.pixelFormat,(unsigned long)d.width,(unsigned long)d.height,(unsigned long)d.mipmapLevelCount,lastError()); if(offset!=UINT64_MAX)return nil; handle=nmb_texture_new(gDevice,&descriptor); - if(!handle){NBLOG(@"native-metal: texture refused: %@",lastError());return nil;} + if(!handle){NBLOG(@"native-metal: texture (fmt %lu %lux%lu, %lu levels) refused: %@",(unsigned long)d.pixelFormat,(unsigned long)d.width,(unsigned long)d.height,(unsigned long)d.mipmapLevelCount,lastError());return nil;} } NBTexture *texture=[NBTexture new];texture.handle=handle;nmb_texture_describe(handle,&descriptor);texture.descriptor=descriptor; if(isPvrtc(d.pixelFormat))texture.pvrtc=(uint32_t)d.pixelFormat; @@ -3449,7 +3519,7 @@ static bool pipelineArchives(NSArray *list,std::vector &arch - (MTLSizeAndAlign)heapTextureSizeAndAlignWithDescriptor:(MTLTextureDescriptor *)d { NmbTextureDescriptor descriptor;uint64_t answer[2]={0,0}; if(textureDescriptor(d,&descriptor)&&!nmb_device_heap_texture_size_and_align(gDevice,&descriptor,answer)) - NBLOG(@"native-metal: heap texture size refused: %@",lastError()); + NBLOG(@"native-metal: heap texture size (fmt %lu %lux%lu, %lu levels) refused: %@",(unsigned long)d.pixelFormat,(unsigned long)d.width,(unsigned long)d.height,(unsigned long)d.mipmapLevelCount,lastError()); return (MTLSizeAndAlign){answer[0],answer[1]}; } - (MTLSizeAndAlign)heapBufferSizeAndAlignWithLength:(NSUInteger)length options:(MTLResourceOptions)options { @@ -3518,7 +3588,7 @@ static bool pipelineArchives(NSArray *list,std::vector &arch (unsigned long long)gTexturesLogged.load(),(unsigned long long)descriptor.width,(unsigned long long)descriptor.height, (unsigned long long)descriptor.format,(unsigned long long)descriptor.type,(unsigned long long)descriptor.levels, (unsigned long long)descriptor.usage,(unsigned long long)descriptor.options,handle?"made":"REFUSED"); - if(!handle){NBLOG(@"native-metal: texture refused: %@",lastError());return nil;} + if(!handle){NBLOG(@"native-metal: texture (fmt %lu %lux%lu, %lu levels) refused: %@",(unsigned long)d.pixelFormat,(unsigned long)d.width,(unsigned long)d.height,(unsigned long)d.mipmapLevelCount,lastError());return nil;} NBTexture *texture=[NBTexture new];texture.handle=handle;nmb_texture_describe(handle,&descriptor);texture.descriptor=descriptor; if(isPvrtc(d.pixelFormat))texture.pvrtc=(uint32_t)d.pixelFormat; return (id)texture; diff --git a/bridge/patches/0022-mipmaps-outside-the-measured-spread.patch b/bridge/patches/0022-mipmaps-outside-the-measured-spread.patch new file mode 100644 index 0000000..ad9d9da --- /dev/null +++ b/bridge/patches/0022-mipmaps-outside-the-measured-spread.patch @@ -0,0 +1,71 @@ +diff --git a/crates/metal2vulkan-runtime/src/blit_texture.rs b/crates/metal2vulkan-runtime/src/blit_texture.rs +--- a/crates/metal2vulkan-runtime/src/blit_texture.rs ++++ b/crates/metal2vulkan-runtime/src/blit_texture.rs +@@ -1737,11 +1737,13 @@ + TextureType::Type1D | TextureType::Type1DArray + ); + if one_dimensional || !own_format { +- return Err(gap()); +- } +- if !device.mipmaps_accepted(texture)? { + return Err(gap()); + } ++ // A format whose blit chain failed the measured-spread check, or whose Metal answer is not ++ // measured, still gets the chain: Metal generates every filterable format's mipmaps with its ++ // box filter, and a refusal leaves the levels unwritten (a texture sampled below level 0 ++ // shows garbage). The near-equivalent is announced once per device. ++ let within_spread = device.mipmaps_accepted(texture)?; + let (image, aspects) = (texture.raw_image()?, texture_aspects(texture.image_plan()?)); + device.shared().counts.pool_compiled( + crate::work_counts::PoolCompiled::ImageBlit, +@@ -1777,14 +1779,25 @@ + .collect(); + self.scrub(texture, written)?; + let pixel_format = texture.record().pixel_format; +- device.degrade(Degradation::MipmapsWithinSpread, || { +- ( +- format!("generate_mipmaps of a {} texture", pixel_format.name()), +- "the device's blit chain gives the format's mipmaps within Metal's measured \ +- spread, which the runtime checked at the format's first generate_mipmaps" +- .to_owned(), +- ) +- }); ++ if within_spread { ++ device.degrade(Degradation::MipmapsWithinSpread, || { ++ ( ++ format!("generate_mipmaps of a {} texture", pixel_format.name()), ++ "the device's blit chain gives the format's mipmaps within Metal's measured \ ++ spread, which the runtime checked at the format's first generate_mipmaps" ++ .to_owned(), ++ ) ++ }); ++ } else { ++ device.degrade(Degradation::MipmapsBoxFilter, || { ++ ( ++ format!("generate_mipmaps of a {} texture", pixel_format.name()), ++ "the device's linear blit chain, whose answers for the format lie outside \ ++ Metal's measured spread or were not measured" ++ .to_owned(), ++ ) ++ }); ++ } + Ok(()) + } + +diff --git a/crates/metal2vulkan-runtime/src/degradation.rs b/crates/metal2vulkan-runtime/src/degradation.rs +--- a/crates/metal2vulkan-runtime/src/degradation.rs ++++ b/crates/metal2vulkan-runtime/src/degradation.rs +@@ -764,6 +764,13 @@ + "each level is the device's linear blit of the one above, which on the check's inputs \ + lay within 1 unit in the last place (2 for 32-bit float) of an Apple M3 Max's or an \ + Apple M4's answer, not at either's exactly; other inputs are not checked"; ++ /// `generate_mipmaps` through the device's blit chain for a pixel format whose chain the ++ /// runtime's check did not accept within Metal's measured spread, or whose Metal answer is ++ /// not measured: Metal generates such a format's mipmaps with its box filter. ++ MipmapsBoxFilter = "mipmaps-box-filter", Precision, ++ "each level is the device's linear blit of the one above, a box filter like Metal's, \ ++ whose answers for the format lie outside the spread measured between an Apple M3 \ ++ Max and an Apple M4, or were not measured"; + /// A depth or stencil texture made in a heap on a device whose driver lays depth and + /// stencil images apart from the memory they are bound to + /// ([`metal2vulkan::device::DriverFact::DepthStencilImagesLieApart`]) that no shadow of the diff --git a/bridge/patches/0023-heap-texture-size-a-multiple-of-its-alignment.patch b/bridge/patches/0023-heap-texture-size-a-multiple-of-its-alignment.patch new file mode 100644 index 0000000..5025cb7 --- /dev/null +++ b/bridge/patches/0023-heap-texture-size-a-multiple-of-its-alignment.patch @@ -0,0 +1,14 @@ +--- a/crates/metal2vulkan-runtime/src/texture/heap.rs ++++ b/crates/metal2vulkan-runtime/src/texture/heap.rs +@@ -1290,6 +1290,11 @@ + } + size_and_align = metal; + } ++ // Metal's size is a multiple of its alignment, so a heap sized as the sum of its textures' ++ // sizes holds them in any order. Where the device's image needs a coarser alignment than ++ // Metal's model (Turnip: 4 KiB against Metal's 128 bytes), the size grows to a multiple of ++ // it too: otherwise each texture placed after one pads past what the heap was sized for. ++ size_and_align.size = size_and_align.size.next_multiple_of(size_and_align.align.max(1)); + check_heap_base(size_and_align.align)?; + // The size answered is the optimized texture's where the driver keeps the layout. + self.degrade_optimized_layout(descriptor, &layout); diff --git a/bridge/present_host.mm b/bridge/present_host.mm index 86db82d..aa2cc06 100644 --- a/bridge/present_host.mm +++ b/bridge/present_host.mm @@ -26,7 +26,7 @@ static bool run(id device, uint32_t w, uint32_t h, size_t srcStride, for (size_t i = 0; i < dstBytes; ++i) dst[i] = 0xa5; gpupresent::Presenter presenter(device, {dst, dstBytes, dstStride, w, h}); if (!presenter.ready()) {std::printf("FAIL %s scanout import\n", name); return false;} - const gpupresent::Result r = presenter.present({src, srcBytes, srcStride, w, h}); + const gpupresent::Result r = presenter.present({src, srcBytes, srcStride, w, h, 0, NGP_BGRX8888}); if (r != gpupresent::kOk) {std::printf("FAIL %s present %d\n", name, (int)r); return false;} for (uint32_t y = 0; y < h; ++y) { if (memcmp(dst + y * dstStride, src + y * srcStride, (size_t)w * 4)) {std::printf("FAIL %s row %u differs\n", name, y); return false;} diff --git a/bridge/surface_import_host.mm b/bridge/surface_import_host.mm index 820a828..4133ada 100644 --- a/bridge/surface_import_host.mm +++ b/bridge/surface_import_host.mm @@ -25,6 +25,12 @@ extern "C" NmbBuffer *tracked_surface_import(NmbDevice *d, void *p, uint64_t n, if (buffer) {std::lock_guard lock(handlesLock);handles.insert(buffer);imports++;} return buffer; } +extern "C" NmbBuffer *tracked_surface_import_releasing(NmbDevice *d, void *p, uint64_t n, uint64_t options, + void (*release)(void *, void *, uint64_t), void *context) { + NmbBuffer *buffer = nmb_buffer_new_no_copy_releasing(d,p,n,options,release,context); + if (buffer) {std::lock_guard lock(handlesLock);handles.insert(buffer);imports++;} + return buffer; +} extern "C" void tracked_buffer_release(NmbBuffer *buffer) { {std::lock_guard lock(handlesLock);if(handles.erase(buffer))releases++;} nmb_buffer_release(buffer); @@ -95,26 +101,33 @@ static bool aliases(id device,unsigned count,unsigned allocations,boo } for(IOSurfaceRef surface:surfaces)CFRelease(surface); } + // The surfaces are gone: their held imports go at the next trim (a completion, an import or memory pressure). + native_bridge_metal_trim_surface_imports(); const unsigned made=imports.load()-before; const bool retired=drained(released+made); - // More than the metadata bound may evict live weak associations. The - // runtime must still share each live import and retire every real owner. - const bool reuse=allocations>256?(made>=allocations && made<=count):made==allocations; + // Every live surface is imported once, however many textures alias it. + const bool reuse=made==allocations; const uint64_t actualVk=vkImports?vkImports()-vkBefore:0; std::printf("aliases=%u surfaces=%u parallel=%d imports=%u releases=%u vk_imports=%llu gpu=%s retired=%s reuse=%s\n", count,allocations,parallel,made,releases.load()-released,(unsigned long long)actualVk,ok?"pass":"FAIL",retired?"pass":"FAIL",reuse?"pass":"FAIL"); return ok && retired && reuse && (!vkImports || actualVk==allocations); } +// As Metal keeps a surface's GPU mapping while the surface lives: textures made and dropped over a surface the caller still holds share +// one import, kept; once the caller lets go of the surface, a trim releases it. static bool expires(id device) { const unsigned before=imports.load(),released=releases.load();IOSurfaceRef surface=makeSurface(0x35);if(!surface)return false; bool ok=true; for(unsigned i=0;i<2;++i) { @autoreleasepool {id texture=[device newTextureWithDescriptor:descriptor() iosurface:surface plane:0];if(!texture)ok=false;} - ok &= drained(released+i+1) && imports.load()==before+i+1; + native_bridge_metal_trim_surface_imports(); + usleep(20000); + ok &= imports.load()==before+1 && releases.load()==released; } CFRelease(surface); - std::printf("expired owners imports=%u releases=%u result=%s\n",imports.load()-before,releases.load()-released,ok?"pass":"FAIL"); + native_bridge_metal_trim_surface_imports(); + ok &= drained(released+1); + std::printf("held owners imports=%u releases=%u result=%s\n",imports.load()-before,releases.load()-released,ok?"pass":"FAIL"); return ok; } @@ -145,7 +158,7 @@ static bool pending(id device) { if(dispatch_semaphore_wait(finished,dispatch_time(DISPATCH_TIME_NOW,3*NSEC_PER_SEC)))return false; if(command.status!=MTLCommandBufferStatusCompleted)ok=false; for(unsigned i=0;i<4096;++i)if(((const unsigned char *)output.contents)[i]!=0x79)ok=false; - // Completed commands must release the import even if still retained. + // The completion lets go of the held import: the caller dropped the surface before the GPU read it. ok &= drained(released+1); } std::printf("pending owners imports=%u releases=%u result=%s\n",imports.load()-before,releases.load()-released,ok?"pass":"FAIL"); @@ -168,6 +181,6 @@ int main(int argc,char **argv) { ok &= pending(device); ok &= aliases(device,514,257,false); if(!ok || imports.load()!=releases.load()){std::puts("FAIL: live IOSurface import reuse");return 1;} - std::puts("PASS: live IOSurface aliases reuse imports with real GPU bytes; distinct, parallel, expired and pending owners preserve lifetimes"); + std::puts("PASS: live IOSurface aliases reuse one held import with real GPU bytes; held while the surface lives, released after it, pending work keeps it"); } } From 94af9e8c93f73ec0af11fd75bf19079a97d7c95e Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Wed, 28 Oct 2026 23:14:42 +0800 Subject: [PATCH 62/92] feat(metal): name the cause at the tail of pipeline and heap refusals A client's problem row keeps the head and tail of its latest problem, so the tail now carries what decides it: for a render pipeline the fragment function, linked functions given, stitched libraries made/refused and the compiler version loaded; for a heap texture the answered size and alignment against the heap's size, used size and room. --- NativeBridgeMetalFrontend.mm | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 0e8c709..84505c2 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -543,6 +543,10 @@ static void shaderCacheWrite(NSString *path,NSData *metallib,NSString *warnings) if(![file writeToFile:temporary atomically:NO] || rename(temporary.fileSystemRepresentation,path.fileSystemRepresentation)) unlink(temporary.fileSystemRepresentation); } +// For refusal texts: the compiler version loaded ("top", "Current", the version directory's number, "none") and stitched libraries made +// and refused. +static char gCompilerLoaded[16]="untried"; +static std::atomic gStitchMade,gStitchFailed; static NSData *codeGenRequest(uint32_t type,const std::vector &request,NSString **warnings,NSString **failure) { static void *service; static void (*build)(void *,void *,uint32_t,const void *,size_t,NBCodeGenReply); @@ -573,6 +577,9 @@ static NSData *codeGenRequest(uint32_t type,const std::vector &request, } } if(library && path!=std::string(kFramework)+"/MTLCompiler")NSLog(@"native-metal: Metal compiler loaded from %s",path.c_str()); + const size_t versions=path.find("/Versions/"); + strlcpy(gCompilerLoaded,!library?"none":versions==std::string::npos?"top":path.substr(versions+10,path.rfind('/')-versions-10).c_str(), + sizeof gCompilerLoaded); void *(*create)(const char *)=library?(void *(*)(const char *))dlsym(library,"MTLCodeGenServiceCreate"):nullptr; build=library?(void (*)(void *,void *,uint32_t,const void *,size_t,NBCodeGenReply))dlsym(library,"MTLCodeGenServiceBuildRequest"):nullptr; service=create && build?create("native-metal"):nullptr; @@ -3328,7 +3335,15 @@ static char dvmMemoryReleaseKey; // (Core Image's intermediate images, iconservicesagent, i-33765e1ad) gets a texture instead of nil, which it dereferenced. A placement // heap's texture aliases the memory at its offset; that one stays refused. if(!handle) { - NBLOG(@"native-metal: heap texture (fmt %lu %lux%lu, %lu levels) refused: %@",(unsigned long)d.pixelFormat,(unsigned long)d.width,(unsigned long)d.height,(unsigned long)d.mipmapLevelCount,lastError()); + // The tail (what a client's problem row keeps) is the arithmetic: the texture's answered size and alignment against the heap's size, + // used size and room at that alignment. + NSString *why=lastError(); + uint64_t answer[2]={0,0}; + (void)nmb_device_heap_texture_size_and_align(gDevice,&descriptor,answer); + NBLOG(@"native-metal: heap texture refused: %@ | fmt %lu %lux%lu L%lu type %lu needs %#llx@%#llx heap %#lx used %#lx room %#lx",why, + (unsigned long)d.pixelFormat,(unsigned long)d.width,(unsigned long)d.height,(unsigned long)d.mipmapLevelCount, + (unsigned long)_heapType,(unsigned long long)answer[0],(unsigned long long)answer[1],(unsigned long)nmb_heap_size(_handle), + (unsigned long)nmb_heap_used_size(_handle),(unsigned long)nmb_heap_max_available_size(_handle,answer[1]?answer[1]:1)); if(offset!=UINT64_MAX)return nil; handle=nmb_texture_new(gDevice,&descriptor); if(!handle){NBLOG(@"native-metal: texture (fmt %lu %lux%lu, %lu levels) refused: %@",(unsigned long)d.pixelFormat,(unsigned long)d.width,(unsigned long)d.height,(unsigned long)d.mipmapLevelCount,lastError());return nil;} @@ -3697,11 +3712,13 @@ static bool pipelineArchives(NSArray *list,std::vector &arch NSString *warnings=nil,*failure=nil; NSData *metallib=codeGenRequest(14,request,&warnings,&failure); if(!metallib) { + gStitchFailed++; NBLOG(@"native-metal: stitched library: %@",[failure substringToIndex:MIN(failure.length,(NSUInteger)300)]); noteProblem([@"stitched library: " stringByAppendingString:[failure substringToIndex:MIN(failure.length,(NSUInteger)120)]]); if(error)*error=[NSError errorWithDomain:MTLLibraryErrorDomain code:MTLLibraryErrorCompileFailure userInfo:@{NSLocalizedDescriptionKey:failure}]; return nil; } + gStitchMade++; dispatch_data_t data=dispatch_data_create(metallib.bytes,metallib.length,nullptr,^{(void)metallib;}); return [(id)self newLibraryWithData:data error:error]; } @@ -3798,7 +3815,16 @@ static bool pipelineArchives(NSArray *list,std::vector &arch if(handle && reflection && wantsReflection(options))*reflection=(MTLRenderPipelineReflection *)renderReflection(p); delete p; journalRenderPipeline(d,pipelineBegan,handle?nullptr:nmb_last_error(),gJournalPath.empty()?std::string():NBCost::now().since(costBegan)); - if(!handle)return failWith>(error,@"render pipeline",nil); + if(!handle) { + // The tail of the text (what a client's problem row keeps) names the fragment function, the linked functions given and the compiler's + // state: a missing linked function there is RenderBox's stitched custom_blend, which needs the compiler. + NSString *why=[NSString stringWithFormat:@"%@ | frag %@ linked %lu stitched %u/%u compiler %s",lastError(),d.fragmentFunction.name, + (unsigned long)d.fragmentLinkedFunctions.functions.count,gStitchMade.load(),gStitchMade.load()+gStitchFailed.load(),gCompilerLoaded]; + static std::atomic told{0}; + if(told.fetch_add(1)<64)NBLOG(@"native-metal: render pipeline refused: %@",why);else noteProblem([@"render pipeline: " stringByAppendingString:why]); + if(error)*error=nbError([@"render pipeline: " stringByAppendingString:why]); + return nil; + } NBRenderPipeline *state=[NBRenderPipeline new];state.handle=handle;gPipelines++;if(error)*error=nil; if(dumpRange().first)state.dumpInfo=renderDumpInfo(d); return (id)state; From ded1122911337050bb6e71110d2a41b33607040c Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Wed, 28 Oct 2026 23:53:47 +0800 Subject: [PATCH 63/92] perf(metal): Droid-VM draw-path patches, Core Image libraries, Metal's compiler - Patches 0025-0037: Droid-VM's draw-path work on the same translator pin (viewports and scissors in place, kept draw rooms handed to the next encoder, barriers on the stack, command pools reset as given back on the completion thread, calibration off the commit path, key-hashed maps). Golden hashes unchanged upstream; host suite passes here. - Patch 0024: a Core Image kernel library (file_type 1, ci_stdlib and ci_filters) loads as Metal loads it, an executable library naming none of its kernels (measured on macOS 27 Metal), instead of a refusal. - The compiler is found first by the path Apple's Metal.framework itself records (Versions/32023 on iPadOS 27), so a sandbox that cannot list the framework directory still compiles. iconservicesagent crashed because Core Image's source compile returned null. - Heap-full fallbacks no longer take a client's problem row. --- NativeBridgeMetalFrontend.mm | 24 +- .../0024-core-image-kernel-libraries.patch | 70 +++++ ...-set-viewports-and-scissors-in-place.patch | 69 +++++ .../0026-keep-push-and-table-bytes.patch | 155 ++++++++++ bridge/patches/0027-keep-bind-vectors.patch | 129 ++++++++ .../0028-keep-bound-buffer-vectors.patch | 45 +++ .../0029-keep-vertex-buffer-binds.patch | 51 ++++ ...-hand-draw-rooms-to-the-next-encoder.patch | 139 +++++++++ .../patches/0031-keep-bound-state-room.patch | 89 ++++++ .../0032-reshape-colors-without-cloning.patch | 35 +++ ...33-look-up-final-passes-by-reference.patch | 176 +++++++++++ .../0034-batch-barriers-on-the-stack.patch | 49 +++ ...35-reset-command-pools-as-given-back.patch | 133 +++++++++ .../0036-calibrate-off-the-commit-path.patch | 50 ++++ .../0037-key-hash-the-draw-path-maps.patch | 278 ++++++++++++++++++ 15 files changed, 1490 insertions(+), 2 deletions(-) create mode 100644 bridge/patches/0024-core-image-kernel-libraries.patch create mode 100644 bridge/patches/0025-set-viewports-and-scissors-in-place.patch create mode 100644 bridge/patches/0026-keep-push-and-table-bytes.patch create mode 100644 bridge/patches/0027-keep-bind-vectors.patch create mode 100644 bridge/patches/0028-keep-bound-buffer-vectors.patch create mode 100644 bridge/patches/0029-keep-vertex-buffer-binds.patch create mode 100644 bridge/patches/0030-hand-draw-rooms-to-the-next-encoder.patch create mode 100644 bridge/patches/0031-keep-bound-state-room.patch create mode 100644 bridge/patches/0032-reshape-colors-without-cloning.patch create mode 100644 bridge/patches/0033-look-up-final-passes-by-reference.patch create mode 100644 bridge/patches/0034-batch-barriers-on-the-stack.patch create mode 100644 bridge/patches/0035-reset-command-pools-as-given-back.patch create mode 100644 bridge/patches/0036-calibrate-off-the-commit-path.patch create mode 100644 bridge/patches/0037-key-hash-the-draw-path-maps.patch diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 84505c2..94d1bde 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -27,6 +27,8 @@ #include #include #include +#include +#include #include #include #include @@ -561,6 +563,24 @@ static NSData *codeGenRequest(uint32_t type,const std::vector &request, // Image's create_metal_dag, RenderBox's custom_blend) and every source compile was refused. std::string path=std::string(kFramework)+"/MTLCompiler"; void *library=dlopen(path.c_str(),RTLD_NOW); + // The version Apple's Metal.framework itself loads: the compiler path recorded among its C strings (24A446: Versions/32023). Read from + // the loaded image, so a sandbox that refuses to list the framework's directory does not matter. + if(!library) { + static const char kPrefix[]="/System/Library/PrivateFrameworks/MTLCompiler.framework/Versions/"; + for(uint32_t i=0,n=_dyld_image_count();isizeof kPrefix && !strcmp(text+length-12,"/MTLCompiler")) { + path.assign(text,length);library=dlopen(path.c_str(),RTLD_NOW);break; + } + at+=length+1; + } + } + } if(!library && !access((std::string(kFramework)+"/Versions/Current/MTLCompiler").c_str(),R_OK)) { path=std::string(kFramework)+"/Versions/Current/MTLCompiler"; library=dlopen(path.c_str(),RTLD_NOW); @@ -3340,7 +3360,7 @@ static char dvmMemoryReleaseKey; NSString *why=lastError(); uint64_t answer[2]={0,0}; (void)nmb_device_heap_texture_size_and_align(gDevice,&descriptor,answer); - NBLOG(@"native-metal: heap texture refused: %@ | fmt %lu %lux%lu L%lu type %lu needs %#llx@%#llx heap %#lx used %#lx room %#lx",why, + NSLog(@"native-metal: heap texture refused: %@ | fmt %lu %lux%lu L%lu type %lu needs %#llx@%#llx heap %#lx used %#lx room %#lx",why, (unsigned long)d.pixelFormat,(unsigned long)d.width,(unsigned long)d.height,(unsigned long)d.mipmapLevelCount, (unsigned long)_heapType,(unsigned long long)answer[0],(unsigned long long)answer[1],(unsigned long)nmb_heap_size(_handle), (unsigned long)nmb_heap_used_size(_handle),(unsigned long)nmb_heap_max_available_size(_handle,answer[1]?answer[1]:1)); @@ -3667,7 +3687,7 @@ static bool pipelineArchives(NSArray *list,std::vector &arch NSString *warnings=nil,*failure=nil; NSData *metallib=compileMetalSource(source,compileArguments(options),&warnings,&failure); if(!metallib) { - NBLOG(@"native-metal: newLibraryWithSource: %@",[failure substringToIndex:MIN(failure.length,(NSUInteger)300)]); + NBLOG(@"native-metal: newLibraryWithSource: %@ | compiler %s",[failure substringToIndex:MIN(failure.length,(NSUInteger)300)],gCompilerLoaded); if(error)*error=[NSError errorWithDomain:MTLLibraryErrorDomain code:MTLLibraryErrorCompileFailure userInfo:@{NSLocalizedDescriptionKey:failure}]; return nil; } diff --git a/bridge/patches/0024-core-image-kernel-libraries.patch b/bridge/patches/0024-core-image-kernel-libraries.patch new file mode 100644 index 0000000..38377a6 --- /dev/null +++ b/bridge/patches/0024-core-image-kernel-libraries.patch @@ -0,0 +1,70 @@ +--- a/crates/metal2vulkan-air/src/container/mtlb.rs ++++ b/crates/metal2vulkan-air/src/container/mtlb.rs +@@ -41,6 +41,13 @@ + const FILE_TYPE_EXECUTABLE: u8 = 0; + /// `file_type` of a dynamic library (`MTLLibraryTypeDynamic`). + const FILE_TYPE_DYNAMIC: u8 = 2; ++/// `file_type` of a Core Image kernel library (`metal -cikernel`: ci_stdlib.metallib and ++/// ci_filters.metallib of Core Image under iOS 27 and macOS 27): one module whose functions are ++/// listed `TYPE` 5 and carry `!air.ci`. Metal loads it as an executable library that names none ++/// of them (an M1 under macOS 27.0.1: `type` 0, `functionNames` empty, `installName` its file). ++const FILE_TYPE_CI_KERNELS: u8 = 1; ++/// `TYPE` of a Core Image kernel in such a library. ++const TYPE_CI_KERNEL: u8 = 5; + + /// A metallib's library type (`MTLLibraryType`), from its header's `file_type`. + #[non_exhaustive] +@@ -248,7 +255,10 @@ + 6, + )); + } +- if header.file_type != FILE_TYPE_EXECUTABLE && header.file_type != FILE_TYPE_DYNAMIC { ++ if header.file_type != FILE_TYPE_EXECUTABLE ++ && header.file_type != FILE_TYPE_DYNAMIC ++ && header.file_type != FILE_TYPE_CI_KERNELS ++ { + return Err(InputError::new( + InputErrorKind::Unsupported(Feature::LibraryKind(header.file_type)), + Location::container(10), +@@ -317,6 +327,11 @@ + } + if at != entries_end { + return Err(container_error(ContainerError::BadOffset, 32)); ++ } ++ // A Core Image kernel library lists its kernels over one shared module; Metal names none of ++ // them, so neither does the library here. ++ if header.file_type == FILE_TYPE_CI_KERNELS { ++ parsed.retain(|p| p.function.stage != FunctionStage::Other(TYPE_CI_KERNEL)); + } + let (extended, _) = tags::read_list(file, at, file.len())?; + let mut linkage = linkage(file, &extended)?; +--- a/crates/metal2vulkan-air/src/container/tests.rs ++++ b/crates/metal2vulkan-air/src/container/tests.rs +@@ -593,14 +593,25 @@ + b.version = (1, 2); + assert!(parse(&b.build()).is_ok()); + let mut b = sample(); +- b.file_type = 1; ++ b.file_type = 3; + match parse(&b.build()).unwrap_err() { + Error::Unsupported(Unsupported { +- feature: Feature::LibraryKind(1), ++ feature: Feature::LibraryKind(3), + .. + }) => {} + other => panic!("{other:?}"), + } ++ // A Core Image kernel library (`file_type` 1): Metal loads it and names none of its `TYPE` 5 ++ // kernels (macOS 27.0.1, ci_stdlib.metallib); a function of another type stays listed. ++ let mut b = sample(); ++ b.file_type = 1; ++ for f in &mut b.functions { ++ f.type_code = 5; ++ } ++ match parse(&b.build()).unwrap() { ++ Library::Metallib(m) => assert!(m.functions.is_empty(), "{:?}", m.functions), ++ other => panic!("{other:?}"), ++ } + } + + #[test] diff --git a/bridge/patches/0025-set-viewports-and-scissors-in-place.patch b/bridge/patches/0025-set-viewports-and-scissors-in-place.patch new file mode 100644 index 0000000..5b43d4f --- /dev/null +++ b/bridge/patches/0025-set-viewports-and-scissors-in-place.patch @@ -0,0 +1,69 @@ +diff --git a/crates/metal2vulkan-runtime/src/render_state.rs b/crates/metal2vulkan-runtime/src/render_state.rs +index e231b10..c7b6521 100644 +--- a/crates/metal2vulkan-runtime/src/render_state.rs ++++ b/crates/metal2vulkan-runtime/src/render_state.rs +@@ -252,19 +252,18 @@ impl EncoderState { + refused: Option, + ) -> Result<(), Error> { + check_count(call, viewports.len())?; +- // A NaN origin x draws nothing, as plain Metal's: a viewport of no width. +- let viewports: Vec = viewports +- .iter() +- .map(|v| match v.origin_x.is_nan() { +- true => Viewport { +- origin_x: 0.0, +- width: 0.0, +- ..*v +- }, +- false => *v, +- }) +- .collect(); +- for v in &viewports { ++ // A NaN origin x draws nothing, as plain Metal's: a viewport of no width. The vector is ++ // written in place, so a draw's call allocates nothing once the encoder's has its room. ++ self.viewports.clear(); ++ self.viewports.extend(viewports.iter().map(|v| match v.origin_x.is_nan() { ++ true => Viewport { ++ origin_x: 0.0, ++ width: 0.0, ++ ..*v ++ }, ++ false => *v, ++ })); ++ for v in &self.viewports { + let fields = [v.origin_x, v.origin_y, v.width, v.height, v.znear, v.zfar]; + if fields.iter().any(|f| !f.is_finite()) { + return Err(invalid( +@@ -273,10 +272,9 @@ impl EncoderState { + )); + } + } +- if let Some(error) = refused.filter(|_| viewports.iter().any(outside_depth)) { ++ if let Some(error) = refused.filter(|_| self.viewports.iter().any(outside_depth)) { + return Err(error); + } +- self.viewports = viewports; + Ok(()) + } + +@@ -302,14 +300,12 @@ impl EncoderState { + let start = origin.min(end); + (start, origin.saturating_add(size).min(end) - start) + }; +- self.scissors = scissors +- .iter() +- .map(|s| { +- let (x, width) = clip(s.x, s.width, width); +- let (y, height) = clip(s.y, s.height, height); +- ScissorRect::new(x, y, width, height) +- }) +- .collect(); ++ self.scissors.clear(); ++ self.scissors.extend(scissors.iter().map(|s| { ++ let (x, width) = clip(s.x, s.width, width); ++ let (y, height) = clip(s.y, s.height, height); ++ ScissorRect::new(x, y, width, height) ++ })); + Ok(()) + } + diff --git a/bridge/patches/0026-keep-push-and-table-bytes.patch b/bridge/patches/0026-keep-push-and-table-bytes.patch new file mode 100644 index 0000000..7c5cb91 --- /dev/null +++ b/bridge/patches/0026-keep-push-and-table-bytes.patch @@ -0,0 +1,155 @@ +diff --git a/crates/metal2vulkan-runtime/src/binding.rs b/crates/metal2vulkan-runtime/src/binding.rs +index df2525f..d54a41e 100644 +--- a/crates/metal2vulkan-runtime/src/binding.rs ++++ b/crates/metal2vulkan-runtime/src/binding.rs +@@ -757,6 +757,17 @@ pub(super) fn push_writes( + Ok(filled) + } + ++/// `push_writes`'s writes, given back as the plan's: the next draw's instantiation fills their ++/// vectors again instead of allocating its own. ++pub(super) fn push_room(push: Vec) -> Vec { ++ push.into_iter() ++ .map(|(stages, offset, bytes)| { ++ let stages = metal2vulkan::vk::ShaderStageFlags::from_raw(stages.as_raw()); ++ PushConstantWrite::new(stages, offset, bytes) ++ }) ++ .collect() ++} ++ + /// The descriptor writes [`write_sets`] hands the driver in one call: their infos sit on the stack. + const WRITES_AT_ONCE: usize = 16; + +diff --git a/crates/metal2vulkan-runtime/src/render.rs b/crates/metal2vulkan-runtime/src/render.rs +index 5cd377c..d50c194 100644 +--- a/crates/metal2vulkan-runtime/src/render.rs ++++ b/crates/metal2vulkan-runtime/src/render.rs +@@ -7324,6 +7324,8 @@ impl RenderCommandEncoder { + } else { + self.uses_room = uses.into_room(); + } ++ // The push writes' vectors go back to the commands, which the next draw fills. ++ commands.push_constants = binding::push_room(push); + self.draw_commands = Some(commands); + // The states go; the list stays for the next draw's. + argument_samplers.clear(); +diff --git a/crates/metal2vulkan/src/bind/graphics.rs b/crates/metal2vulkan/src/bind/graphics.rs +index c79f0d4..896c4ba 100644 +--- a/crates/metal2vulkan/src/bind/graphics.rs ++++ b/crates/metal2vulkan/src/bind/graphics.rs +@@ -3340,7 +3340,7 @@ fn instantiate_into( + out: &mut DrawCommands, + ) -> Result<(), BindError> { + // The vectors `out` held, which this draw's commands take, emptied. +- let kept = std::mem::replace(out, DrawCommands::empty()); ++ let mut kept = std::mem::replace(out, DrawCommands::empty()); + let overrun = index_overrun(t.robust_index_fetch, draw); + let (command, base) = vk_draw(draw, overrun); + let capture = match t.capture { +@@ -3381,22 +3381,38 @@ fn instantiate_into( + }; + let vertex = state.stages.get(&Stage::Vertex); + +- let mut push_constants = emptied(kept.push_constants); +- let mut descriptor_bytes = emptied(kept.descriptor_bytes); ++ // The last draw's push and table writes, whose byte vectors this draw's stages take (a plan's ++ // stages write the same ranges, so the room is the same): a draw allocates them once. ++ let mut push_constants = std::mem::take(&mut kept.push_constants); ++ let mut descriptor_bytes = std::mem::take(&mut kept.descriptor_bytes); + let mut push_holes = emptied(kept.push_holes); + let mut root_tables = emptied(kept.root_tables); + let mut reads_base = false; ++ // The writes made so far: the stages with push fields, and those with a table. ++ let (mut pushes, mut tables_written) = (0usize, 0usize); + for st in &t.stages { + let (push_start, push_size) = st.push_range.unwrap_or((0, 0)); +- let mut push = vec![0u8; push_size as usize]; +- let mut table = vec![ +- 0u8; ++ // Only a stage that writes them takes a slot's vector: one without would leave the next ++ // stage's slot emptied. ++ let mut push = match push_constants.get_mut(pushes) { ++ Some(write) if push_size > 0 => std::mem::take(&mut write.bytes), ++ _ => Vec::new(), ++ }; ++ push.clear(); ++ push.resize(push_size as usize, 0); ++ let mut table = match descriptor_bytes.get_mut(tables_written) { ++ Some(write) if st.table.is_some() => std::mem::take(&mut write.bytes), ++ _ => Vec::new(), ++ }; ++ table.clear(); ++ table.resize( + if st.table.is_some() { + st.table_size as usize + } else { + 0 +- } +- ]; ++ }, ++ 0, ++ ); + for f in &st.fields { + let bytes: FieldBytes = match f.field { + PushField::RootTableAddress +@@ -3557,20 +3573,33 @@ fn instantiate_into( + root_tables.push((st.flags, stage_root_table(&statics, first, resident))); + } + if push_size > 0 { +- push_constants.push(PushConstantWrite { ++ let write = PushConstantWrite { + stages: st.flags, + offset: push_start, + bytes: push, +- }); ++ }; ++ match push_constants.get_mut(pushes) { ++ Some(slot) => *slot = write, ++ None => push_constants.push(write), ++ } ++ pushes += 1; + } + if let Some((set, binding)) = st.table { +- descriptor_bytes.push(DescriptorBytes { ++ let write = DescriptorBytes { + set, + binding, + bytes: table, +- }); ++ }; ++ match descriptor_bytes.get_mut(tables_written) { ++ Some(slot) => *slot = write, ++ None => descriptor_bytes.push(write), ++ } ++ tables_written += 1; + } + } ++ // The last draw's writes this one has fewer of: their vectors' room is not kept. ++ push_constants.truncate(pushes); ++ descriptor_bytes.truncate(tables_written); + + // Vertex buffers: offsets, per-bind strides, the address rules, and the bytes the draw's + // vertex and instance ranges fetch. +diff --git a/crates/metal2vulkan/src/bind/instantiate.rs b/crates/metal2vulkan/src/bind/instantiate.rs +index e474e8c..1acb2d8 100644 +--- a/crates/metal2vulkan/src/bind/instantiate.rs ++++ b/crates/metal2vulkan/src/bind/instantiate.rs +@@ -25,6 +25,18 @@ pub struct PushConstantWrite { + pub bytes: Vec, + } + ++impl PushConstantWrite { ++ /// A write of `bytes` at `offset` for `stages`: a consumer giving back the vectors of the ++ /// writes it took, which the next instantiation fills again. ++ pub fn new(stages: vk::ShaderStageFlags, offset: u32, bytes: Vec) -> Self { ++ Self { ++ stages, ++ offset, ++ bytes, ++ } ++ } ++} ++ + /// Bytes the consumer uploads into the buffer behind a plan binding (argument table, handle + /// table) before recording the draw or dispatch. + #[non_exhaustive] diff --git a/bridge/patches/0027-keep-bind-vectors.patch b/bridge/patches/0027-keep-bind-vectors.patch new file mode 100644 index 0000000..bfc8ee4 --- /dev/null +++ b/bridge/patches/0027-keep-bind-vectors.patch @@ -0,0 +1,129 @@ +diff --git a/crates/metal2vulkan-runtime/src/binding.rs b/crates/metal2vulkan-runtime/src/binding.rs +index d54a41e..5caa3f2 100644 +--- a/crates/metal2vulkan-runtime/src/binding.rs ++++ b/crates/metal2vulkan-runtime/src/binding.rs +@@ -454,8 +454,17 @@ pub(super) fn bind( + tables: &Tables<'_>, + mut memo: Option<&mut BindMemo>, + ) -> Result { +- let mut writes = Vec::with_capacity(plan.descriptors.len()); +- let mut samplers = Vec::new(); ++ // The vectors a memo keeps from the last bind, emptied. ++ let (mut writes, mut holes, mut samplers, mut roots) = match memo.as_deref_mut() { ++ Some(memo) => ( ++ std::mem::take(&mut memo.writes), ++ std::mem::take(&mut memo.holes), ++ std::mem::take(&mut memo.bound_samplers), ++ std::mem::take(&mut memo.roots), ++ ), ++ None => Default::default(), ++ }; ++ writes.reserve(plan.descriptors.len()); + // The sampler tables, whose regions the command buffer writes again as it commits. + let sampler_keys = plan.sampler_tables(); + let mut sampler_tables = Vec::with_capacity(sampler_keys.len()); +@@ -533,11 +542,11 @@ pub(super) fn bind( + held, + }); + } +- let mut roots = Vec::with_capacity(command.root_tables.len()); ++ roots.clear(); + for &(stages, table) in &command.root_tables { + roots.push((stages, root_table(device, state, table)?)); + } +- let mut holes = Vec::with_capacity(command.push_holes.len()); ++ holes.reserve(command.push_holes.len()); + for hole in command.push_holes { + holes.push(match *hole { + PushHole::RootTableAddress { stages, offset } => Hole { +@@ -588,6 +597,9 @@ pub(super) fn bind( + }, + }); + } ++ if let Some(memo) = memo { ++ memo.roots = roots; ++ } + Ok(Bound { + writes, + holes, +@@ -889,6 +901,32 @@ pub(super) struct BindMemo { + epoch: u64, + views: HashMap, + samplers: Vec, ++ /// The vectors of the last bind's [`Bound`] its consumer gave back ([`BindMemo::give_back`]) ++ /// and its root table addresses, emptied: the next bind fills them. ++ writes: Vec, ++ holes: Vec, ++ bound_samplers: Vec>, ++ roots: Vec<(metal2vulkan::vk::ShaderStageFlags, u64)>, ++} ++ ++impl BindMemo { ++ /// Keep the vectors of a [`Bound`] its consumer is done with for the next bind; samplers ++ /// where the consumer did not keep them. ++ pub(super) fn give_back( ++ &mut self, ++ mut writes: Vec, ++ mut holes: Vec, ++ samplers: Option>>, ++ ) { ++ writes.clear(); ++ holes.clear(); ++ self.writes = writes; ++ self.holes = holes; ++ if let Some(mut samplers) = samplers { ++ samplers.clear(); ++ self.bound_samplers = samplers; ++ } ++ } + } + + /// What a view a bind made or found is a function of ([`BindMemo`]): a binding of the texture as +diff --git a/crates/metal2vulkan-runtime/src/render.rs b/crates/metal2vulkan-runtime/src/render.rs +index d50c194..62a3082 100644 +--- a/crates/metal2vulkan-runtime/src/render.rs ++++ b/crates/metal2vulkan-runtime/src/render.rs +@@ -1186,18 +1186,25 @@ impl LastRetained { + /// Retain into `core` the samplers a draw's bind named, unless they are the ones the last + /// draw's named, in order: each by another reference to it, the last first, so they go in + /// their order (`retained.rs`). +- fn retain_samplers(&mut self, core: &mut EncoderCore, samplers: Vec>) { ++ /// Answers the list where it keeps the one it had, which is the same: the next bind fills it. ++ fn retain_samplers( ++ &mut self, ++ core: &mut EncoderCore, ++ samplers: Vec>, ++ ) -> Option>> { + let same = samplers.len() == self.samplers.len() + && samplers + .iter() + .zip(&self.samplers) + .all(|(a, b)| Arc::ptr_eq(a, b)); +- if !same { +- for sampler in samplers.iter().rev() { +- core.retain_shared(sampler.clone()); +- } +- self.samplers = samplers; ++ if same { ++ return Some(samplers); + } ++ for sampler in samplers.iter().rev() { ++ core.retain_shared(sampler.clone()); ++ } ++ self.samplers = samplers; ++ None + } + } + +@@ -7306,8 +7313,10 @@ impl RenderCommandEncoder { + { + self.core.retain_shared(buffer.held()); + } +- self.last_retained ++ let unkept = self ++ .last_retained + .retain_samplers(&mut self.core, bound.samplers); ++ self.bind_memo.give_back(writes, bound.holes, unkept); + if let Some(unlisted) = unlisted { + heap_sets::degrade_unlisted(&device, unlisted, "a render pipeline state's draws"); + // A host step's draw, which no commit lists again, fails its command buffer. diff --git a/bridge/patches/0028-keep-bound-buffer-vectors.patch b/bridge/patches/0028-keep-bound-buffer-vectors.patch new file mode 100644 index 0000000..371b02a --- /dev/null +++ b/bridge/patches/0028-keep-bound-buffer-vectors.patch @@ -0,0 +1,45 @@ +diff --git a/crates/metal2vulkan/src/bind/graphics.rs b/crates/metal2vulkan/src/bind/graphics.rs +index 896c4ba..ca8bf54 100644 +--- a/crates/metal2vulkan/src/bind/graphics.rs ++++ b/crates/metal2vulkan/src/bind/graphics.rs +@@ -3333,6 +3333,12 @@ fn instantiate( + Ok(out) + } + ++thread_local! { ++ /// The residual and root entry vectors of this thread's last draw's bound buffers, emptied by ++ /// the next, which fills them: no draw's [`DrawCommands`] holds them. ++ static BOUND_ROOM: std::cell::Cell = std::cell::Cell::new(BoundBuffers::default()); ++} ++ + fn instantiate_into( + t: &GraphicsTemplate, + state: &BindState, +@@ -3359,13 +3365,14 @@ fn instantiate_into( + } + }; + let command = mesh.as_ref().map_or(command, |m| m.command); ++ let room = BOUND_ROOM.take(); + let bound = bind_buffers_in( + &t.buffers, + state, + BoundBuffers { + descriptor_ranges: kept.descriptor_ranges, + extents: kept.extents, +- ..BoundBuffers::default() ++ ..room + }, + )?; + let mut descriptor_ranges = bound.descriptor_ranges; +@@ -3600,6 +3607,11 @@ fn instantiate_into( + // The last draw's writes this one has fewer of: their vectors' room is not kept. + push_constants.truncate(pushes); + descriptor_bytes.truncate(tables_written); ++ BOUND_ROOM.set(BoundBuffers { ++ residuals: bound.residuals, ++ statics: bound.statics, ++ ..BoundBuffers::default() ++ }); + + // Vertex buffers: offsets, per-bind strides, the address rules, and the bytes the draw's + // vertex and instance ranges fetch. diff --git a/bridge/patches/0029-keep-vertex-buffer-binds.patch b/bridge/patches/0029-keep-vertex-buffer-binds.patch new file mode 100644 index 0000000..e04f745 --- /dev/null +++ b/bridge/patches/0029-keep-vertex-buffer-binds.patch @@ -0,0 +1,51 @@ +diff --git a/crates/metal2vulkan-runtime/src/render.rs b/crates/metal2vulkan-runtime/src/render.rs +index 62a3082..a507bd2 100644 +--- a/crates/metal2vulkan-runtime/src/render.rs ++++ b/crates/metal2vulkan-runtime/src/render.rs +@@ -1128,6 +1128,8 @@ pub struct RenderCommandEncoder { + uses_room: Vec>, + /// The list the last draw's argument samplers were in, emptied (`Device::argument_samplers_into`). + argument_samplers_room: Vec, ++ /// The list the last draw's vertex buffer binds were in, emptied, for the next draw's. ++ vertex_buffers_room: Vec<(u32, vk::Buffer, u64, u64)>, + /// The views and sampler descriptions its draws' binds made or found (`binding::BindMemo`). + bind_memo: binding::BindMemo, + /// What its last draw retained of its own ([`LastRetained`]). +@@ -1407,6 +1409,7 @@ impl CommandBuffer { + draw_commands: None, + uses_room: Vec::new(), + argument_samplers_room: Vec::new(), ++ vertex_buffers_room: Vec::new(), + bind_memo: binding::BindMemo::default(), + last_retained: LastRetained::default(), + }) +@@ -6469,7 +6472,8 @@ impl RenderCommandEncoder { + self.capture_missed(); + return Ok(()); + } +- let mut vertex_buffers = Vec::with_capacity(plan.vertex_buffers.len()); ++ let mut vertex_buffers = std::mem::take(&mut self.vertex_buffers_room); ++ vertex_buffers.clear(); + for bind in &plan.vertex_buffers { + let table = pass_start.or(Stage::Vertex, &self.vertex); + let offset = commands +@@ -7339,6 +7343,7 @@ impl RenderCommandEncoder { + // The states go; the list stays for the next draw's. + argument_samplers.clear(); + self.argument_samplers_room = argument_samplers; ++ self.vertex_buffers_room = vertex_buffers; + Ok(()) + } + +diff --git a/crates/metal2vulkan-runtime/src/render/executions.rs b/crates/metal2vulkan-runtime/src/render/executions.rs +index 768c128..db52d4f 100644 +--- a/crates/metal2vulkan-runtime/src/render/executions.rs ++++ b/crates/metal2vulkan-runtime/src/render/executions.rs +@@ -2097,6 +2097,7 @@ impl RenderExecution { + draw_commands: None, + uses_room: Vec::new(), + argument_samplers_room: Vec::new(), ++ vertex_buffers_room: Vec::new(), + bind_memo: crate::binding::BindMemo::default(), + last_retained: Default::default(), + } diff --git a/bridge/patches/0030-hand-draw-rooms-to-the-next-encoder.patch b/bridge/patches/0030-hand-draw-rooms-to-the-next-encoder.patch new file mode 100644 index 0000000..0bfd526 --- /dev/null +++ b/bridge/patches/0030-hand-draw-rooms-to-the-next-encoder.patch @@ -0,0 +1,139 @@ +diff --git a/crates/metal2vulkan-runtime/src/binding.rs b/crates/metal2vulkan-runtime/src/binding.rs +index 5caa3f2..a916162 100644 +--- a/crates/metal2vulkan-runtime/src/binding.rs ++++ b/crates/metal2vulkan-runtime/src/binding.rs +@@ -910,6 +910,18 @@ pub(super) struct BindMemo { + } + + impl BindMemo { ++ /// The memo with nothing kept but its vectors' room: what an ended encoder hands the next ++ /// (`render::DrawRooms`), holding no texture of its views. ++ pub(super) fn emptied(mut self) -> Self { ++ self.views.clear(); ++ self.samplers.clear(); ++ self.writes.clear(); ++ self.holes.clear(); ++ self.bound_samplers.clear(); ++ self.roots.clear(); ++ self ++ } ++ + /// Keep the vectors of a [`Bound`] its consumer is done with for the next bind; samplers + /// where the consumer did not keep them. + pub(super) fn give_back( +diff --git a/crates/metal2vulkan-runtime/src/device/mod.rs b/crates/metal2vulkan-runtime/src/device/mod.rs +index 81304a0..f7b18e6 100644 +--- a/crates/metal2vulkan-runtime/src/device/mod.rs ++++ b/crates/metal2vulkan-runtime/src/device/mod.rs +@@ -373,6 +373,9 @@ pub(super) struct DeviceShared { + /// rebind's or a storage form's new `VkImage`, a detach's stand-in, a reattach): a view kept + /// by what it was made of (`binding::BindMemo`) is current while this holds. + pub(crate) views_epoch: AtomicU64, ++ /// The vectors ended render encoders' draws filled, emptied, for the next encoders' draws ++ /// (`render::DrawRooms`). ++ pub(crate) draw_rooms: Mutex>, + } + + impl DeviceShared { +@@ -1234,6 +1237,7 @@ impl Device { + record_executions, + refuse_rebinds, + views_epoch: AtomicU64::new(0), ++ draw_rooms: Mutex::new(Vec::new()), + device_address, + upload_alignment, + memory: Mutex::new(memory), +diff --git a/crates/metal2vulkan-runtime/src/render.rs b/crates/metal2vulkan-runtime/src/render.rs +index a507bd2..ae505fa 100644 +--- a/crates/metal2vulkan-runtime/src/render.rs ++++ b/crates/metal2vulkan-runtime/src/render.rs +@@ -1149,6 +1149,42 @@ struct LastRetained { + samplers: Vec>, + } + ++/// The vectors a render encoder's draws fill again draw after draw, emptied: an ended encoder ++/// gives them to its device, whose next encoder takes them, so its first draws allocate them no ++/// more than its later ones. ++#[derive(Default)] ++pub(crate) struct DrawRooms { ++ draw_commands: Option, ++ uses: Vec>, ++ argument_samplers: Vec, ++ vertex_buffers: Vec<(u32, vk::Buffer, u64, u64)>, ++ bind_memo: binding::BindMemo, ++} ++ ++/// The most [`DrawRooms`] a device keeps: one per encoder encoding at once. ++const KEPT_DRAW_ROOMS: usize = 4; ++ ++impl DrawRooms { ++ /// A kept one of `device`'s, or new ones. ++ fn take(device: &crate::device::Device) -> Self { ++ crate::device::lock(&device.shared().draw_rooms) ++ .pop() ++ .unwrap_or_default() ++ } ++ ++ /// Keep them for `device`'s next encoder, emptied. ++ fn keep(mut self, device: &crate::device::Device) { ++ self.uses.clear(); ++ self.argument_samplers.clear(); ++ self.vertex_buffers.clear(); ++ self.bind_memo = self.bind_memo.emptied(); ++ let mut kept = crate::device::lock(&device.shared().draw_rooms); ++ if kept.len() < KEPT_DRAW_ROOMS { ++ kept.push(self); ++ } ++ } ++} ++ + impl LastRetained { + /// Retain into `core` a draw's pipeline state, variant objects and made `VkPipeline`, each + /// unless it is the one the last draw retained. +@@ -1318,6 +1354,7 @@ impl CommandBuffer { + render_pass::degrade_depth_clear(device, &layout); + let extended = ExtendedShadows::of(descriptor, &layout, rate_map.is_some()); + let rerecord = reroute::keeps_whole(device) || extended.keeps_draws(); ++ let rooms = DrawRooms::take(device); + Ok(RenderCommandEncoder { + core, + earlier: Vec::new(), +@@ -1406,11 +1443,11 @@ impl CommandBuffer { + live: Arc::default(), + log, + quiet_bound_values: None, +- draw_commands: None, +- uses_room: Vec::new(), +- argument_samplers_room: Vec::new(), +- vertex_buffers_room: Vec::new(), +- bind_memo: binding::BindMemo::default(), ++ draw_commands: rooms.draw_commands, ++ uses_room: rooms.uses, ++ argument_samplers_room: rooms.argument_samplers, ++ vertex_buffers_room: rooms.vertex_buffers, ++ bind_memo: rooms.bind_memo, + last_retained: LastRetained::default(), + }) + } +@@ -3069,8 +3106,21 @@ impl RenderCommandEncoder { + extended, + reload_at, + log, ++ draw_commands, ++ uses_room, ++ argument_samplers_room, ++ vertex_buffers_room, ++ bind_memo, + .. + } = self; ++ DrawRooms { ++ draw_commands, ++ uses: uses_room, ++ argument_samplers: argument_samplers_room, ++ vertex_buffers: vertex_buffers_room, ++ bind_memo, ++ } ++ .keep(core.device()); + earlier.push(current); + let mut instances = earlier; + // A color attachment's store action still `Unknown` stores nothing, as plain Metal's diff --git a/bridge/patches/0031-keep-bound-state-room.patch b/bridge/patches/0031-keep-bound-state-room.patch new file mode 100644 index 0000000..285b6f4 --- /dev/null +++ b/bridge/patches/0031-keep-bound-state-room.patch @@ -0,0 +1,89 @@ +diff --git a/crates/metal2vulkan-runtime/src/render.rs b/crates/metal2vulkan-runtime/src/render.rs +index ae505fa..8a327a7 100644 +--- a/crates/metal2vulkan-runtime/src/render.rs ++++ b/crates/metal2vulkan-runtime/src/render.rs +@@ -1159,6 +1159,7 @@ pub(crate) struct DrawRooms { + argument_samplers: Vec, + vertex_buffers: Vec<(u32, vk::Buffer, u64, u64)>, + bind_memo: binding::BindMemo, ++ bound: Bound, + } + + /// The most [`DrawRooms`] a device keeps: one per encoder encoding at once. +@@ -1178,6 +1179,7 @@ impl DrawRooms { + self.argument_samplers.clear(); + self.vertex_buffers.clear(); + self.bind_memo = self.bind_memo.emptied(); ++ self.bound = self.bound.emptied(); + let mut kept = crate::device::lock(&device.shared().draw_rooms); + if kept.len() < KEPT_DRAW_ROOMS { + kept.push(self); +@@ -1375,7 +1377,7 @@ impl CommandBuffer { + reformats: Vec::new(), + positioned: None, + rerecord: rerecord.then(Vec::new), +- bound: Bound::default(), ++ bound: rooms.bound, + }, + state: EncoderState::metal_defaults( + rate_map +@@ -3080,7 +3082,7 @@ impl RenderCommandEncoder { + let RenderCommandEncoder { + mut core, + mut earlier, +- current, ++ mut current, + mut layout, + passes, + mut visibility, +@@ -3119,6 +3121,7 @@ impl RenderCommandEncoder { + argument_samplers: argument_samplers_room, + vertex_buffers: vertex_buffers_room, + bind_memo, ++ bound: std::mem::take(&mut current.bound), + } + .keep(core.device()); + earlier.push(current); +diff --git a/crates/metal2vulkan-runtime/src/render/bound.rs b/crates/metal2vulkan-runtime/src/render/bound.rs +index be326c0..3892fed 100644 +--- a/crates/metal2vulkan-runtime/src/render/bound.rs ++++ b/crates/metal2vulkan-runtime/src/render/bound.rs +@@ -87,11 +87,37 @@ impl Bound { + if self.command_buffer != command_buffer { + *self = Bound { + command_buffer, +- ..Bound::default() ++ ..std::mem::take(self).emptied() + }; + } + } + ++ /// Nothing bound, its vectors' room kept: an ended encoder's for the next encoder's ++ /// (`DrawRooms`). ++ pub(super) fn emptied(self) -> Bound { ++ let Bound { ++ mut sets, ++ mut pushes, ++ mut push_bytes, ++ mut vertex, ++ mut set, ++ .. ++ } = self; ++ sets.clear(); ++ pushes.clear(); ++ push_bytes.clear(); ++ vertex.clear(); ++ set.clear(); ++ Bound { ++ sets, ++ pushes, ++ push_bytes, ++ vertex, ++ set, ++ ..Bound::default() ++ } ++ } ++ + /// Whether binding `pipeline`, which holds the dynamic states `dynamic` (the module docs), + /// sets anything: noted as bound where it does. + pub(super) fn pipeline( diff --git a/bridge/patches/0032-reshape-colors-without-cloning.patch b/bridge/patches/0032-reshape-colors-without-cloning.patch new file mode 100644 index 0000000..57648f1 --- /dev/null +++ b/bridge/patches/0032-reshape-colors-without-cloning.patch @@ -0,0 +1,35 @@ +diff --git a/crates/metal2vulkan-runtime/src/live_bind.rs b/crates/metal2vulkan-runtime/src/live_bind.rs +index 8696754..b4f1a7c 100644 +--- a/crates/metal2vulkan-runtime/src/live_bind.rs ++++ b/crates/metal2vulkan-runtime/src/live_bind.rs +@@ -66,6 +66,15 @@ impl Reshape { + } + *field = value; + } ++ ++ /// [`field`](Self::field) of `value`, cloned only where it changes the field: a draw of the ++ /// same state copies nothing. ++ pub(super) fn field_from(&mut self, field: &mut T, value: &T) { ++ if field != value { ++ self.0 = true; ++ field.clone_from(value); ++ } ++ } + } + + /// A render encoder's bind state as its last draw bound it, and its shape while no change may +diff --git a/crates/metal2vulkan-runtime/src/render.rs b/crates/metal2vulkan-runtime/src/render.rs +index 8a327a7..0ed9ae7 100644 +--- a/crates/metal2vulkan-runtime/src/render.rs ++++ b/crates/metal2vulkan-runtime/src/render.rs +@@ -7692,8 +7692,8 @@ impl RenderCommandEncoder { + &mut state.depth_clamp_range, + settings.written_clamp.map(|w| w.range), + ); +- reshape.field(&mut state.zeroed_colors, settings.zeroed_colors.clone()); +- reshape.field(&mut state.dropped_colors, settings.dropped_colors.clone()); ++ reshape.field_from(&mut state.zeroed_colors, &settings.zeroed_colors); ++ reshape.field_from(&mut state.dropped_colors, &settings.dropped_colors); + reshape.field(&mut state.interlocked_colors, interlocked.clone()); + reshape.field(&mut state.extended_shadows, shadows.clone()); + let depth_stencil = match &settings.failing_state { diff --git a/bridge/patches/0033-look-up-final-passes-by-reference.patch b/bridge/patches/0033-look-up-final-passes-by-reference.patch new file mode 100644 index 0000000..b15e259 --- /dev/null +++ b/bridge/patches/0033-look-up-final-passes-by-reference.patch @@ -0,0 +1,176 @@ +diff --git a/crates/metal2vulkan-runtime/src/render_target.rs b/crates/metal2vulkan-runtime/src/render_target.rs +index e7f7391..63a28ae 100644 +--- a/crates/metal2vulkan-runtime/src/render_target.rs ++++ b/crates/metal2vulkan-runtime/src/render_target.rs +@@ -723,36 +723,48 @@ pub(super) fn final_pass( + } + let anchor = shape.anchor(device.compile_device().profile()); + let dont_care = DontCare::of(device); +- let colors: Vec = layout +- .colors +- .iter() +- .enumerate() +- .filter_map(|(index, color)| Some((index, color.as_ref()?))) +- .map(|(index, color)| color_ops(color, anchor == Some(index as u32), at, dont_care)) +- .collect(); ++ // The operations go in this thread's kept vectors: a pass found kept copies none of them. ++ let (mut colors, mut resolves) = FINAL_OPS.take(); ++ colors.clear(); ++ colors.extend( ++ layout ++ .colors ++ .iter() ++ .enumerate() ++ .filter_map(|(index, color)| Some((index, color.as_ref()?))) ++ .map(|(index, color)| color_ops(color, anchor == Some(index as u32), at, dont_care)), ++ ); + let depth_stencil = layout + .depth_stencil + .as_ref() + .map(|ds| depth_stencil_ops(layout, ds, at, dont_care)); +- let resolves = vec![ +- RESOLVED; ++ resolves.clear(); ++ resolves.resize( + layout + .colors + .iter() + .filter(|c| subpass_resolve(c.as_ref()).is_some()) +- .count() +- ]; +- kept_final_pass( ++ .count(), ++ RESOLVED, ++ ); ++ let pass = kept_final_pass( + device, +- FinalMakings { +- shape: shape.clone(), +- colors, ++ FinalKey { ++ shape, ++ colors: &colors, + depth_stencil, +- resolves, ++ resolves: &resolves, + depth_stencil_resolve: subpass_step(layout).map(step_ops), + resolving: at.last, + }, +- ) ++ ); ++ FINAL_OPS.set((colors, resolves)); ++ pass ++} ++ ++thread_local! { ++ /// The vectors [`final_pass`] puts a pass's operations in, emptied by the next. ++ static FINAL_OPS: std::cell::Cell<(Vec, Vec)> = std::cell::Cell::default(); + } + + /// The most final passes a device keeps ([`kept_final_pass`]): a program's passes take a few +@@ -760,7 +772,7 @@ pub(super) fn final_pass( + const KEPT_FINALS: usize = 256; + + /// What a final pass is made of: [`create`]'s arguments past the device. +-#[derive(Clone, PartialEq, Eq, Hash)] ++#[derive(Clone, PartialEq, Eq)] + pub(super) struct FinalMakings { + shape: PassShape, + colors: Vec, +@@ -770,6 +782,81 @@ pub(super) struct FinalMakings { + resolving: bool, + } + ++/// [`FinalMakings`] by reference: what a kept pass is looked up by, hashed and compared as the ++/// makings it stands for, so a lookup that finds one copies nothing. ++#[derive(Clone, Copy, PartialEq, Eq, Hash)] ++struct FinalKey<'a> { ++ shape: &'a PassShape, ++ colors: &'a [Ops], ++ depth_stencil: Option, ++ resolves: &'a [Ops], ++ depth_stencil_resolve: Option, ++ resolving: bool, ++} ++ ++impl FinalKey<'_> { ++ fn to_makings(self) -> FinalMakings { ++ FinalMakings { ++ shape: self.shape.clone(), ++ colors: self.colors.to_vec(), ++ depth_stencil: self.depth_stencil, ++ resolves: self.resolves.to_vec(), ++ depth_stencil_resolve: self.depth_stencil_resolve, ++ resolving: self.resolving, ++ } ++ } ++} ++ ++/// What has a [`FinalKey`]: the kept makings, and a key looked up by. ++trait FinalKeyed { ++ fn key(&self) -> FinalKey<'_>; ++} ++ ++impl FinalKeyed for FinalMakings { ++ fn key(&self) -> FinalKey<'_> { ++ FinalKey { ++ shape: &self.shape, ++ colors: &self.colors, ++ depth_stencil: self.depth_stencil, ++ resolves: &self.resolves, ++ depth_stencil_resolve: self.depth_stencil_resolve, ++ resolving: self.resolving, ++ } ++ } ++} ++ ++impl FinalKeyed for FinalKey<'_> { ++ fn key(&self) -> FinalKey<'_> { ++ *self ++ } ++} ++ ++impl std::hash::Hash for FinalMakings { ++ fn hash(&self, state: &mut H) { ++ self.key().hash(state); ++ } ++} ++ ++impl std::hash::Hash for dyn FinalKeyed + '_ { ++ fn hash(&self, state: &mut H) { ++ self.key().hash(state); ++ } ++} ++ ++impl PartialEq for dyn FinalKeyed + '_ { ++ fn eq(&self, other: &Self) -> bool { ++ self.key() == other.key() ++ } ++} ++ ++impl Eq for dyn FinalKeyed + '_ {} ++ ++impl<'a> std::borrow::Borrow for FinalMakings { ++ fn borrow(&self) -> &(dyn FinalKeyed + 'a) { ++ self ++ } ++} ++ + /// The final passes a device keeps, by what they are made of ([`kept_final_pass`]). + #[derive(Default)] + pub(super) struct KeptFinal(HashMap); +@@ -790,11 +877,12 @@ impl KeptFinal { + /// before, it cost a `vkCreateRenderPass2` (where Turnip lays out its attachments in GMEM for + /// every tile layout) and a `vkDestroyRenderPass` each time a pass ended, 19 times a frame of + /// iOS's compositor. Past [`KEPT_FINALS`] a pass is made for its instance alone, as before. +-fn kept_final_pass(device: &Device, makings: FinalMakings) -> Result { ++fn kept_final_pass(device: &Device, key: FinalKey<'_>) -> Result { + let kept = &device.shared().final_passes; +- if let Some(&raw) = lock(kept).0.get(&makings) { ++ if let Some(&raw) = lock(kept).0.get(&key as &dyn FinalKeyed) { + return Ok(RenderPassObject::kept(device, raw)); + } ++ let makings = key.to_makings(); + let pass = create( + device, + &makings.shape, diff --git a/bridge/patches/0034-batch-barriers-on-the-stack.patch b/bridge/patches/0034-batch-barriers-on-the-stack.patch new file mode 100644 index 0000000..7e2c7dd --- /dev/null +++ b/bridge/patches/0034-batch-barriers-on-the-stack.patch @@ -0,0 +1,49 @@ +diff --git a/crates/metal2vulkan-runtime/src/command_buffer/submit.rs b/crates/metal2vulkan-runtime/src/command_buffer/submit.rs +index d90691f..8b18e14 100644 +--- a/crates/metal2vulkan-runtime/src/command_buffer/submit.rs ++++ b/crates/metal2vulkan-runtime/src/command_buffer/submit.rs +@@ -313,22 +313,24 @@ impl CommandBufferShared { + let mut lead = Some(Barrier::after_completed()); + let mut work = Vec::with_capacity(2 * (end - start) + 1); + if entry.is_some() { +- let globals: Vec = lead.take().into_iter().chain(entry_barrier).collect(); ++ let (pair, count) = barrier_pair(lead.take(), entry_barrier); ++ let globals = &pair[..count]; + let stamps = Stamps { + start: start_stamp.take(), + end: None, + }; +- work.push(self.record_barriers(&mut state, &globals, &entries, stamps)?); ++ work.push(self.record_barriers(&mut state, globals, &entries, stamps)?); + } + for (offset, barrier) in barriers.iter().enumerate() { +- let globals: Vec = lead.take().into_iter().chain(*barrier).collect(); ++ let (pair, count) = barrier_pair(lead.take(), *barrier); ++ let globals = &pair[..count]; + if !globals.is_empty() { + let stamps = Stamps { + start: start_stamp.take(), + end: None, + }; + let kept = &mut scheduler.kept; +- work.push(self.barrier_buffer(&mut state, kept, at, &globals, &[], stamps)?); ++ work.push(self.barrier_buffer(&mut state, kept, at, globals, &[], stamps)?); + } + work.extend(&state.recorded[start + offset].raws); + } +@@ -823,3 +825,15 @@ struct Stamps { + start: Option<(vk::QueryPool, u32)>, + end: Option<(vk::QueryPool, u32)>, + } ++ ++/// `lead` then `barrier`, those of them there are, as the first `.1` of the array: the globals ++/// of a barrier between a batch's encoders, which takes no allocation. ++fn barrier_pair(lead: Option, barrier: Option) -> ([Barrier; 2], usize) { ++ let mut pair = [Barrier::default(); 2]; ++ let mut count = 0; ++ for b in lead.into_iter().chain(barrier) { ++ pair[count] = b; ++ count += 1; ++ } ++ (pair, count) ++} diff --git a/bridge/patches/0035-reset-command-pools-as-given-back.patch b/bridge/patches/0035-reset-command-pools-as-given-back.patch new file mode 100644 index 0000000..248ab5e --- /dev/null +++ b/bridge/patches/0035-reset-command-pools-as-given-back.patch @@ -0,0 +1,133 @@ +diff --git a/crates/metal2vulkan-runtime/src/command_buffer/mod.rs b/crates/metal2vulkan-runtime/src/command_buffer/mod.rs +index 8b3a0c2..0c8537a 100644 +--- a/crates/metal2vulkan-runtime/src/command_buffer/mod.rs ++++ b/crates/metal2vulkan-runtime/src/command_buffer/mod.rs +@@ -1136,7 +1136,7 @@ impl Drop for CommandBufferShared { + // state. + let pool = lock(&self.state).pool.take(); + if let Some(pool) = pool { +- self.device.shared().spares.give_command_pool(pool); ++ self.device.shared().spares.give_command_pool(self.device.shared(), pool); + } + } + } +@@ -2723,7 +2723,7 @@ impl CommandBufferShared { + // buffer that completed gives back its descriptor pools too, one that failed releases + // them here. + if let Some(pool) = state.pool.take() { +- self.device.shared().spares.give_command_pool(pool); ++ self.device.shared().spares.give_command_pool(self.device.shared(), pool); + } + if state.status == CommandBufferStatus::Completed { + state.descriptors.give_back(); +diff --git a/crates/metal2vulkan-runtime/src/device/prime.rs b/crates/metal2vulkan-runtime/src/device/prime.rs +index 50856aa..8bb81ba 100644 +--- a/crates/metal2vulkan-runtime/src/device/prime.rs ++++ b/crates/metal2vulkan-runtime/src/device/prime.rs +@@ -416,6 +416,9 @@ impl Primer<'_> { + let pool = unsafe { device.create_command_pool(&pool_info, None) }.map_err(vulkan)?; + self.made(Call::CreateCommandPool); + self.hold(pool); ++ // SAFETY: the pool was just made, and nothing of it is pending. ++ unsafe { device.reset_command_pool(pool, vk::CommandPoolResetFlags::empty()) }.map_err(vulkan)?; ++ self.made(Call::ResetCommandPool); + let allocate = vk::CommandBufferAllocateInfo::default() + .command_pool(pool) + .level(vk::CommandBufferLevel::PRIMARY) +diff --git a/crates/metal2vulkan-runtime/src/exit_guard.rs b/crates/metal2vulkan-runtime/src/exit_guard.rs +index 9f98943..110455f 100644 +--- a/crates/metal2vulkan-runtime/src/exit_guard.rs ++++ b/crates/metal2vulkan-runtime/src/exit_guard.rs +@@ -303,6 +303,8 @@ calls! { + // (`Device::drain_queues`). + QueueWaitIdle => c"vkQueueWaitIdle", + ResetFences => c"vkResetFences", ++ // A command pool given back reset (`spares.rs`), on the completion thread. ++ ResetCommandPool => c"vkResetCommandPool", + DestroyCommandPool => c"vkDestroyCommandPool", + DestroyFence => c"vkDestroyFence", + DestroySemaphore => c"vkDestroySemaphore", +@@ -470,7 +472,7 @@ pub(crate) const HOST_STEP: &[Call] = &[ + ]; + + impl Call { +- /// Its bit. The table fills 88 of the word's 128 (widened from 64): when it next outgrows the ++ /// Its bit. The table fills 90 of the word's 128 (widened from 64): when it next outgrows the + /// word, widen the word (here, [`CALLED`], `PENDING` and the priming's calls); do not add a + /// second table. + pub(crate) fn bit(self) -> u128 { +diff --git a/crates/metal2vulkan-runtime/src/spares.rs b/crates/metal2vulkan-runtime/src/spares.rs +index 39ac4d8..6480b4f 100644 +--- a/crates/metal2vulkan-runtime/src/spares.rs ++++ b/crates/metal2vulkan-runtime/src/spares.rs +@@ -20,7 +20,12 @@ + //! - **Descriptor pools are bounded.** At most [`DESCRIPTOR_POOLS`] descriptor pools of at most + //! [`DESCRIPTORS`] descriptors together; a descriptor pool given back past them releases the + //! oldest spares first, so the spares follow the shapes in use. +-//! - **Reset by the taker**, on the thread that encodes: the program's own, or one running the ++//! - **Reset as given back**, inside a section on the thread that completed the command buffer ++//! (the completion thread's, or the dropping one's), so the encoding thread takes a pool ready ++//! to record: turnip's reset of a frame's command buffers cost ~30 us of the program's thread ++//! per commit on the host (m2vsim), about three times that in the guest. A pool whose reset ++//! there failed is released; one given back as the process exits stays unreset and is ++//! **reset by the taker**, on the thread that encodes: the program's own, or one running the + //! program's code (a handler). A section of the exit guard (a host step's recording on the + //! scheduler's thread) calls only the guard's table of entry points, which holds no reset, so a + //! command buffer or command recorded there makes its pools as before; what it made is given back +@@ -62,6 +67,8 @@ pub(super) struct CommandPool { + family: u32, + made: [Vec; 2], + handed: [usize; 2], ++ /// Reset since a command buffer last recorded into it (as it was given back). ++ reset: bool, + } + + impl CommandPool { +@@ -72,6 +79,7 @@ impl CommandPool { + family, + made: [Vec::new(), Vec::new()], + handed: [0, 0], ++ reset: false, + } + } + +@@ -127,6 +135,10 @@ impl Spares { + let Some(mut pool) = take_command_pool(&mut lock(&self.command_pools), family) else { + return Ok(None); + }; ++ if std::mem::take(&mut pool.reset) { ++ pool.reset_handed(); ++ return Ok(Some(pool)); ++ } + // SAFETY: the pool is the caller's alone now, and nothing of it is pending: a command + // buffer that completed gave it back. + let reset = unsafe { +@@ -143,9 +155,25 @@ impl Spares { + Ok(Some(pool)) + } + +- /// Keep `pool`, of a command buffer whose work ended or that was never submitted: nothing of +- /// it is pending, and nothing else holds it. Never released here (the module docs). +- pub(super) fn give_command_pool(&self, pool: CommandPool) { ++ /// Keep `pool` of `device`, of a command buffer whose work ended or that was never ++ /// submitted: nothing of it is pending, and nothing else holds it. Reset here, inside a ++ /// section; released only where that reset failed (the module docs). ++ pub(super) fn give_command_pool(&self, device: &DeviceShared, mut pool: CommandPool) { ++ if let Some(_busy) = exit_guard::enter() { ++ // SAFETY: nothing of the pool is pending, and nothing else holds it. ++ let reset = unsafe { ++ device ++ .vk() ++ .reset_command_pool(pool.raw, vk::CommandPoolResetFlags::empty()) ++ }; ++ exit_guard::called(exit_guard::Call::ResetCommandPool); ++ if reset.is_err() { ++ // SAFETY: as above. ++ unsafe { release::object(device.vk(), pool.raw) }; ++ return; ++ } ++ pool.reset = true; ++ } + keep_command_pool(&mut lock(&self.command_pools), pool); + } + diff --git a/bridge/patches/0036-calibrate-off-the-commit-path.patch b/bridge/patches/0036-calibrate-off-the-commit-path.patch new file mode 100644 index 0000000..4ae1c24 --- /dev/null +++ b/bridge/patches/0036-calibrate-off-the-commit-path.patch @@ -0,0 +1,50 @@ +diff --git a/crates/metal2vulkan-runtime/src/command_buffer/completion.rs b/crates/metal2vulkan-runtime/src/command_buffer/completion.rs +index af866a3..38bb81b 100644 +--- a/crates/metal2vulkan-runtime/src/command_buffer/completion.rs ++++ b/crates/metal2vulkan-runtime/src/command_buffer/completion.rs +@@ -292,6 +292,9 @@ fn run(watch: &Watch) { + buffer.read_stamps(); + buffer.place_gpu_times(); + } ++ // The calibration pair the next commits take, refreshed here when old rather than at ++ // a commit (`Timing::kept_pair`). ++ device.shared().timing.pair(); + } + drop(device); + match settled { +diff --git a/crates/metal2vulkan-runtime/src/command_buffer/mod.rs b/crates/metal2vulkan-runtime/src/command_buffer/mod.rs +index 0c8537a..6e92f2b 100644 +--- a/crates/metal2vulkan-runtime/src/command_buffer/mod.rs ++++ b/crates/metal2vulkan-runtime/src/command_buffer/mod.rs +@@ -2401,7 +2401,7 @@ impl CommandBufferShared { + Some(slot) => { + shared.counts.timestamp_slot_taken(); + state.times.slot = Some(slot); +- state.times.pair = timing.pair(); ++ state.times.pair = timing.kept_pair(); + } + None => state.times.stamped = Stamped::NoSlot, + } +diff --git a/crates/metal2vulkan-runtime/src/gpu_time.rs b/crates/metal2vulkan-runtime/src/gpu_time.rs +index f265af0..8b09e6e 100644 +--- a/crates/metal2vulkan-runtime/src/gpu_time.rs ++++ b/crates/metal2vulkan-runtime/src/gpu_time.rs +@@ -610,6 +610,18 @@ impl Timing { + self.from_host + } + ++ /// The device's latest calibration pair, however old, taken now only where there is none: a ++ /// commit's, on the program's thread. A reading is a round trip to the GPU's driver (on ++ /// DroidVM a request to the host, from WindowServer's main thread every 100 ms); the ++ /// completion thread refreshes the pair instead ([`pair`](Self::pair)), so the one a commit ++ /// takes is at most [`REFRESH`] and a command buffer's completion old. ++ pub(crate) fn kept_pair(&self) -> Option { ++ if let Some((_, pair)) = *lock(&self.latest) { ++ return Some(pair); ++ } ++ self.pair() ++ } ++ + /// The device's latest calibration pair, taken now when the last is [`REFRESH`] old or + /// there is none; `None` where the device has no calibration or the reading fails. + pub(crate) fn pair(&self) -> Option { diff --git a/bridge/patches/0037-key-hash-the-draw-path-maps.patch b/bridge/patches/0037-key-hash-the-draw-path-maps.patch new file mode 100644 index 0000000..f756564 --- /dev/null +++ b/bridge/patches/0037-key-hash-the-draw-path-maps.patch @@ -0,0 +1,278 @@ +diff --git a/crates/metal2vulkan-runtime/src/binding.rs b/crates/metal2vulkan-runtime/src/binding.rs +index a916162..9d9eeb8 100644 +--- a/crates/metal2vulkan-runtime/src/binding.rs ++++ b/crates/metal2vulkan-runtime/src/binding.rs +@@ -51,6 +51,7 @@ use super::command_buffer::{NotedTable, State}; + use super::error::{internal, Error, RuntimeGap}; + use super::function_table::{self, TableSnapshot, VisibleFunctionTable}; + use super::heap_sets::{Keeper, TextureTable, HEAP_SET}; ++use super::key_hash::KeyMap; + use super::render_pass::{PassLayout, Target}; + use super::render_target; + use super::residency::{Residency, Used}; +@@ -69,7 +70,7 @@ use metal2vulkan::bind::{ + }; + use metal2vulkan::metal::sampler::OpaqueBlackBorder; + use metal2vulkan::pipeline::{HandleClass, SlotClass, SlotRef, Stage, StorageAttachmentView}; +-use std::collections::{BTreeSet, HashMap}; ++use std::collections::BTreeSet; + use std::ops::Deref; + use std::sync::atomic::Ordering; + use std::sync::Arc; +@@ -899,7 +900,7 @@ const KEPT_VIEWS: usize = 256; + pub(super) struct BindMemo { + /// The views epoch the views were kept under. + epoch: u64, +- views: HashMap, ++ views: KeyMap, + samplers: Vec, + /// The vectors of the last bind's [`Bound`] its consumer gave back ([`BindMemo::give_back`]) + /// and its root table addresses, emptied: the next bind fills them. +diff --git a/crates/metal2vulkan-runtime/src/neutral.rs b/crates/metal2vulkan-runtime/src/neutral.rs +index 4cf4a44..def66ee 100644 +--- a/crates/metal2vulkan-runtime/src/neutral.rs ++++ b/crates/metal2vulkan-runtime/src/neutral.rs +@@ -26,6 +26,7 @@ + //! work samples, its bind waited for that work on NVIDIA's driver (`memory.rs`'s `Fit`). + + use super::device::lock; ++use super::key_hash::KeyMap; + use super::error::{vulkan, Error}; + use super::memory::{Memory, MemoryClass, Placement}; + use super::resource::ResourceKey; +@@ -85,9 +86,9 @@ impl Filled for NeutralTexels { + /// A device's neutral images, texel buffers and samplers, made at first use. + #[derive(Default)] + pub(super) struct Neutrals { +- images: HashMap>, ++ images: KeyMap>, + /// By format and whether the view is a storage texel buffer's. +- texels: HashMap<(Format, bool), Arc>, ++ texels: KeyMap<(Format, bool), Arc>, + /// By whether depth comparison is enabled. + samplers: HashMap, + } +diff --git a/crates/metal2vulkan-runtime/src/pipeline/memo.rs b/crates/metal2vulkan-runtime/src/pipeline/memo.rs +index c2054d9..76187df 100644 +--- a/crates/metal2vulkan-runtime/src/pipeline/memo.rs ++++ b/crates/metal2vulkan-runtime/src/pipeline/memo.rs +@@ -19,10 +19,11 @@ + use super::render::RenderVariant; + use super::Variant; + use crate::device::lock; ++use crate::key_hash::KeyMap; + use metal2vulkan::cache::PipelineKey; + use metal2vulkan::library::{CompiledFunction, FunctionCompileKey}; + use metal2vulkan::pipeline::{ComputePipeline, RenderPipeline}; +-use std::collections::{BTreeMap, HashMap}; ++use std::collections::BTreeMap; + use std::hash::Hash; + use std::sync::{Arc, Mutex, Weak}; + +@@ -33,7 +34,7 @@ pub(crate) const MEMO_ENTRIES: usize = 4096; + /// entries, removing the least recently used past either. A lookup or an insert is a use. + pub(crate) struct Lru { + /// Each entry's number: the stamp of the insert that kept it. +- numbers: HashMap, ++ numbers: KeyMap, + /// The entries, by number. + entries: BTreeMap>, + /// Each entry's number by its last use, oldest first. +@@ -61,7 +62,7 @@ impl Lru { + /// An empty map bounded by `max_weight` and `max_entries`. + pub(crate) fn new(max_weight: u64, max_entries: usize) -> Lru { + Lru { +- numbers: HashMap::new(), ++ numbers: KeyMap::default(), + entries: BTreeMap::new(), + order: BTreeMap::new(), + clock: 0, +diff --git a/crates/metal2vulkan-runtime/src/render/pass_start_reads.rs b/crates/metal2vulkan-runtime/src/render/pass_start_reads.rs +index 04982ae..40c1471 100644 +--- a/crates/metal2vulkan-runtime/src/render/pass_start_reads.rs ++++ b/crates/metal2vulkan-runtime/src/render/pass_start_reads.rs +@@ -54,7 +54,7 @@ use crate::values::{CpuCacheMode, HazardTrackingMode, ResourceOptions, StorageMo + use crate::Device; + use ash::vk; + use metal2vulkan::pipeline::{PipelineInterface, SlotClass, SlotInterface, Stage}; +-use std::collections::HashMap; ++use crate::key_hash::KeyMap; + use std::ops::Range; + + /// The byte ranges one key holds before they are merged into their hull. +@@ -74,7 +74,7 @@ const MESH_STAGES: vk::PipelineStageFlags2 = vk::PipelineStageFlags2::from_raw( + + /// Byte ranges of buffers, tracked or not, by key. + #[derive(Clone, Default)] +-struct Written(HashMap>>); ++struct Written(KeyMap>>); + + impl Written { + fn add(&mut self, key: ResourceKey, range: &Range) { +@@ -89,7 +89,9 @@ impl Written { + } + + fn overlaps(&self, key: ResourceKey, range: &Range) -> bool { +- self.0.get(&key).is_some_and(|ranges| { ++ // Most passes' fragment stages write no buffer: no key to hash. ++ !self.0.is_empty() ++ && self.0.get(&key).is_some_and(|ranges| { + ranges + .iter() + .any(|r| r.start < range.end && range.start < r.end) +diff --git a/crates/metal2vulkan-runtime/src/render_target.rs b/crates/metal2vulkan-runtime/src/render_target.rs +index 63a28ae..5f7b8fa 100644 +--- a/crates/metal2vulkan-runtime/src/render_target.rs ++++ b/crates/metal2vulkan-runtime/src/render_target.rs +@@ -107,6 +107,7 @@ + + use super::attachment_stand_in::Cause; + use super::attachmentless; ++use super::key_hash::KeyMap; + use super::binding_view::VkView; + use super::device::lock; + use super::error::{internal, Error}; +@@ -124,7 +125,6 @@ use crate::release; + use ash::vk; + use metal2vulkan::bind::ViewSetup; + use metal2vulkan::device::{DeviceProfile, DriverFact}; +-use std::collections::HashMap; + use std::sync::Arc; + + /// What makes two passes compatible: each color index's format (`None` without a texture), the +@@ -652,7 +652,7 @@ const KEPT_SHAPES: usize = 64; + + /// The compatible passes a device keeps, by shape ([`shared_compatible_pass`]). + #[derive(Default)] +-pub(super) struct KeptCompatible(HashMap); ++pub(super) struct KeptCompatible(KeyMap); + + impl KeptCompatible { + /// Destroy every kept pass; the device is idle, and is destroyed next. +@@ -859,7 +859,7 @@ impl<'a> std::borrow::Borrow for FinalMakings { + + /// The final passes a device keeps, by what they are made of ([`kept_final_pass`]). + #[derive(Default)] +-pub(super) struct KeptFinal(HashMap); ++pub(super) struct KeptFinal(KeyMap); + + impl KeptFinal { + /// Destroy every kept pass; the device is idle, and is destroyed next. +diff --git a/crates/metal2vulkan-runtime/src/texture.rs b/crates/metal2vulkan-runtime/src/texture.rs +index 071beb8..46097c9 100644 +--- a/crates/metal2vulkan-runtime/src/texture.rs ++++ b/crates/metal2vulkan-runtime/src/texture.rs +@@ -57,6 +57,7 @@ + //! blits of textures over buffers, are refused `NotImplemented`. + + use super::address::{ObjectsToken, RangeToken}; ++use super::key_hash::KeyMap; + use super::address_map::AddressModeMap; + use super::attachment_stand_in::KeptBacking; + use super::attributes::PurgeableState; +@@ -452,7 +453,7 @@ struct Image { + identity: Identity, + /// The `VkImageView`s bindings of the image's textures needed (`binding_view.rs`), made at + /// their first bind. A finite set per image: its view types, formats and level ranges. +- views: Mutex>, ++ views: Mutex>, + /// The image's depth twin: a `D32_SFLOAT` image of the same texels, which the views a + /// comparison reads present, for an `R32_SFLOAT` image holding a `Depth32Float` texture. + twin: Option>, +@@ -1642,7 +1643,7 @@ impl Device { + options: ResourceOptions::new(StorageMode::Private, cache, hazard), + allow_gpu_optimized_contents: allow, + identity: Identity::new(hazard, false), +- views: Mutex::new(HashMap::new()), ++ views: Mutex::new(KeyMap::default()), + twin: None, + decoded: None, + form: Default::default(), +@@ -1765,7 +1766,7 @@ impl Device { + options: ResourceOptions::new(StorageMode::Private, cache, hazard), + allow_gpu_optimized_contents, + identity: Identity::new(hazard, false), +- views: Mutex::new(HashMap::new()), ++ views: Mutex::new(KeyMap::default()), + twin: None, + decoded: None, + form: Default::default(), +@@ -2331,7 +2332,7 @@ impl Device { + tracked: texels.tracked(), + ..texels.buffer.identity() + }, +- views: Mutex::new(HashMap::new()), ++ views: Mutex::new(KeyMap::default()), + twin: None, + decoded: None, + form: Default::default(), +@@ -2419,7 +2420,7 @@ impl Device { + tracked: texels.tracked(), + ..texels.buffer.identity() + }, +- views: Mutex::new(HashMap::new()), ++ views: Mutex::new(KeyMap::default()), + twin: None, + decoded: None, + form: Default::default(), +@@ -2555,7 +2556,7 @@ impl Device { + options, + allow_gpu_optimized_contents, + identity, +- views: Mutex::new(HashMap::new()), ++ views: Mutex::new(KeyMap::default()), + twin: None, + decoded: None, + form: Default::default(), +@@ -3383,7 +3384,7 @@ impl Device { + options: descriptor.resource_options, + allow_gpu_optimized_contents: descriptor.allow_gpu_optimized_contents, + identity, +- views: Mutex::new(HashMap::new()), ++ views: Mutex::new(KeyMap::default()), + twin: None, + decoded: None, + form: Default::default(), +diff --git a/crates/metal2vulkan-runtime/src/texture/heap.rs b/crates/metal2vulkan-runtime/src/texture/heap.rs +index 1210427..096ded4 100644 +--- a/crates/metal2vulkan-runtime/src/texture/heap.rs ++++ b/crates/metal2vulkan-runtime/src/texture/heap.rs +@@ -179,7 +179,6 @@ use metal2vulkan::metal::{PixelFormat, TextureType, TextureUsage}; + use metal2vulkan::vk::{ + Format, FormatFeatureFlags2, ImageAspectFlags, ImageUsageFlags, SampleCountFlags, + }; +-use std::collections::HashMap; + use std::ptr::NonNull; + use std::sync::atomic::{AtomicBool, Ordering}; + use std::sync::{Arc, Mutex}; +@@ -2089,7 +2088,7 @@ impl Device { + options: binding.options, + allow_gpu_optimized_contents: binding.allow_gpu_optimized_contents, + identity: binding.identity, +- views: Mutex::new(HashMap::new()), ++ views: Mutex::new(Default::default()), + twin: None, + decoded: None, + form: Default::default(), +@@ -2125,7 +2124,7 @@ impl Device { + options: binding.options, + allow_gpu_optimized_contents: binding.allow_gpu_optimized_contents, + identity: binding.identity, +- views: Mutex::new(HashMap::new()), ++ views: Mutex::new(Default::default()), + twin: None, + decoded: None, + form: Default::default(), +diff --git a/crates/metal2vulkan-runtime/src/texture/wrapped.rs b/crates/metal2vulkan-runtime/src/texture/wrapped.rs +index fcd1c46..211d0fe 100644 +--- a/crates/metal2vulkan-runtime/src/texture/wrapped.rs ++++ b/crates/metal2vulkan-runtime/src/texture/wrapped.rs +@@ -611,7 +611,7 @@ impl Device { + options, + allow_gpu_optimized_contents: descriptor.allow_gpu_optimized_contents, + identity, +- views: Mutex::new(HashMap::new()), ++ views: Mutex::new(Default::default()), + twin: None, + decoded: None, + form: Default::default(), From d6ca6557039cf9e0c3a1d1a58c560f2e55e8e9bb Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Thu, 29 Oct 2026 01:57:07 +0800 Subject: [PATCH 64/92] fix(metal): give Core Image's kernels by name as Metal does A Core Image kernel library (metallib file_type 1: ci_stdlib, ci_filters, ci_raw) names no functions, yet Apple's Metal answers newFunctionWithName: and newFunctionWithName:constantValues:error: with a function of type 4 (checked on macOS 26.5.1 with its own ci_stdlib). iPadOS 27's Core Image takes its DAG kernels so (MetalDAGHelper::get_precompiled_kernel); the frontend refused them, create_metal_dag gave a null program and iconservicesagent crashed in create_program_and_arguments. Such a function is stitched from its bitcode and refused as a pipeline's own. A host test renders Core Image over the frontend and checks the library's answers against Apple's device. --- NativeBridgeMetalFrontend.mm | 53 ++++++++++++++++++++++++---- bridge/coreimage_host.mm | 68 ++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+), 6 deletions(-) create mode 100644 bridge/coreimage_host.mm diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 94d1bde..bd577f4 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -1154,7 +1154,7 @@ static NBTexture *textureOnSurface(NBBuffer *buffer,MTLTextureDescriptor *d,IOSu @property(nonatomic,strong) MTLFunctionConstantValues *constantValues; // specialized with (nil: none), for the zero-rest rebuild of a refused pipeline @end @implementation NBFunction -- (void)dealloc {nmb_function_release(_handle);} +- (void)dealloc {if(_handle)nmb_function_release(_handle);} // _MTLFunction's bitCodeHash (32 bytes), read by the stitched-library path (createStitchingScriptHash): a digest naming this function // object, its name and a serial no other function in the process has. - (const void *)bitCodeHash { @@ -1170,8 +1170,8 @@ static NBTexture *textureOnSurface(NBBuffer *buffer,MTLTextureDescriptor *d,IOSu } } - (MTLFunctionOptions)options {return MTLFunctionOptionNone;} -- (MTLPatchType)patchType {return (MTLPatchType)nmb_function_patch_type(_handle);} -- (NSInteger)patchControlPointCount {return (NSInteger)nmb_function_patch_control_point_count(_handle);} +- (MTLPatchType)patchType {return _handle?(MTLPatchType)nmb_function_patch_type(_handle):MTLPatchTypeNone;} +- (NSInteger)patchControlPointCount {return _handle?(NSInteger)nmb_function_patch_control_point_count(_handle):0;} @end // The target's own accessors of MTLFunctionConstantValues (as the offline frontend reads them); their ABI is checked before any call. @interface MTLFunctionConstantValues (NBOriginalConstantAccess) @@ -1207,6 +1207,13 @@ static uint64_t constantBytes(uint64_t type) { } } static std::string quartzCoreCallers(); +// A Core Image kernel library (metallib file_type 1: ci_stdlib, ci_filters, ci_raw, ci_stdlib_h) lists its kernels as TYPE 5 entries over +// one shared module. Metal names none of them (functionNames is empty) yet answers newFunctionWithName: and +// newFunctionWithName:constantValues:error: with a function of functionType 4 (Apple's Metal on macOS 26.5.1 with its own ci_stdlib.metallib: +// _add, _srcOver). Core Image's DAG path takes its precompiled kernels so (CI::MetalDAGHelper::get_precompiled_kernel) and stitches them; +// without them create_metal_dag ended in a null program and iconservicesagent crashed in create_program_and_arguments. Such a function has +// no runtime handle: it is stitched from its bitcode (functionBitcode) and refused as a pipeline's own function. +static NBFunction *ciKernelFunction(NBLibrary *library,NSString *name); @implementation NBLibrary - (void)dealloc {nmb_library_release(_handle);} - (id)newFunctionWithName:(NSString *)name constantValues:(MTLFunctionConstantValues *)values error:(NSError **)error { @@ -1216,6 +1223,7 @@ static std::string quartzCoreCallers(); // retry of a pipeline the runtime refused because such a constant had no value. - (id)nb_newFunctionWithName:(NSString *)name constantValues:(MTLFunctionConstantValues *)values zeroRest:(BOOL)zeroRest error:(NSError **)error { + if(NBFunction *kernel=ciKernelFunction(self,name)){if(error)*error=nil;return (id)kernel;} if(!values)values=[MTLFunctionConstantValues new]; if(!name || !values || !methodABI(values,@selector(newIndexedConstantArray),"@16@0:8") || !methodABI(values,@selector(newNamedConstantArray),"@16@0:8")) { if(error)*error=nbError(@"unqualified function constant values");return nil; @@ -1279,6 +1287,7 @@ static std::string quartzCoreCallers(); } - (id)newFunctionWithName:(NSString *)name { const uint64_t functionBegan=nowUs(); + if(NBFunction *kernel=ciKernelFunction(self,name)){gTimeFunction.add(functionBegan);return (id)kernel;} NmbFunction *handle=name?nmb_function_new(_handle,name.UTF8String):nullptr;gTimeFunction.add(functionBegan); if(!handle) { static std::atomic told{0}; @@ -1329,6 +1338,35 @@ static NSData *functionBitcode(NBFunction *function) { } return nil; } +static NBFunction *ciKernelFunction(NBLibrary *library,NSString *name) { + const uint8_t *file=library.bytes;const size_t length=library.length;const char *wanted=name.UTF8String; + if(!file || !wanted || length<88 || memcmp(file,"MTLB",4) || file[10]!=1)return nil; + uint64_t listAt;memcpy(&listAt,file+24,8); + if(listAt>length-4)return nil; + uint32_t count;memcpy(&count,file+listAt,4); + size_t at=listAt+4; + for(uint32_t i=0;ilength-4)return nil;memcpy(&size,file+at,4); + if(size<8 || size>length-at)return nil; + const size_t end=at+size; + std::string entry;int type=-1; + for(size_t t=at+4;t+4<=end && memcmp(file+t,"ENDT",4);) { + if(t+6>end)return nil; + uint16_t n;memcpy(&n,file+t+4,2);const uint8_t *v=file+t+6; + if(n>end-(t+6))return nil; + if(!memcmp(file+t,"NAME",4))entry.assign((const char *)v,strnlen((const char *)v,n)); + else if(!memcmp(file+t,"TYPE",4) && n>=1)type=v[0]; + t+=6+n; + } + if(entry==wanted) { + if(type!=5)return nil; + NBFunction *function=[NBFunction new];function.name=name;function.functionType=(MTLFunctionType)4;function.library=library; + return function; + } + at=end; + } + return nil; +} @interface NBComputePipeline : NBObject @property(nonatomic) NmbComputePipeline *handle; @end @@ -2942,6 +2980,9 @@ static bool renderDescriptor(MTLRenderPipelineDescriptor *d,NBRenderDescriptor * if(![(id)d.vertexFunction isKindOfClass:[NBFunction class]] || (d.fragmentFunction && ![(id)d.fragmentFunction isKindOfClass:[NBFunction class]])) { if(error)*error=nbError(@"foreign function");return false; } + if(![(NBFunction *)d.vertexFunction handle] || (d.fragmentFunction && ![(NBFunction *)d.fragmentFunction handle])) { + if(error)*error=nbError(@"a Core Image kernel function is only stitched");return false; + } p->vertex=[(NBFunction *)d.vertexFunction handle];p->fragment=d.fragmentFunction?[(NBFunction *)d.fragmentFunction handle]:nullptr; // Each stage's visible functions (MTLRenderPipelineDescriptor vertexLinkedFunctions/fragmentLinkedFunctions): RenderBox's fragment // functions call custom_blend through them; without them the runtime refused the pipeline (MissingInput LinkedFunction) and the render @@ -3385,7 +3426,7 @@ static char dvmMemoryReleaseKey; // Creation and archive population must use the same complete supported descriptor. // The handles of a linked-function list, false for one the frontend did not make. static bool linkedHandles(NSArray> *functions,std::vector &out) { - for(id f in functions){if(![(id)f isKindOfClass:[NBFunction class]])return false;out.push_back([(NBFunction *)f handle]);} + for(id f in functions){if(![(id)f isKindOfClass:[NBFunction class]] || ![(NBFunction *)f handle])return false;out.push_back([(NBFunction *)f handle]);} return true; } @@ -3402,7 +3443,7 @@ static NBFunction *zeroRestFunction(NBFunction *f) { // The handles `p` points at live in `linked` and `privateLinked`, which outlive its use. static bool computeDescriptor(MTLComputePipelineDescriptor *d,NmbComputePipelineDescriptor *p,std::vector &linkedStore, std::vector &privateStore,NSError **error) { - if(!d || ![(id)d.computeFunction isKindOfClass:[NBFunction class]]){if(error)*error=nbError(@"foreign compute function");return false;} + if(!d || ![(id)d.computeFunction isKindOfClass:[NBFunction class]] || ![(NBFunction *)d.computeFunction handle]){if(error)*error=nbError(@"foreign compute function");return false;} // Metal may materialize empty stage/linked descriptors on access. Preserve the // default meaning while refusing state this frontend cannot yet represent. MTLStageInputOutputDescriptor *stage=d.stageInputDescriptor; @@ -3758,7 +3799,7 @@ static bool pipelineArchives(NSArray *list,std::vector &arch - (id)newComputePipelineStateWithFunction:(id)function options:(MTLPipelineOption)options reflection:(MTLAutoreleasedComputePipelineReflection *)reflection error:(NSError **)error { if(reflection)*reflection=nil; - if(![(id)function isKindOfClass:[NBFunction class]]){if(error)*error=nbError(@"foreign function");return nil;} + if(![(id)function isKindOfClass:[NBFunction class]] || ![(NBFunction *)function handle]){if(error)*error=nbError(@"foreign function");return nil;} const NmbPipelineOptions creation={(uint64_t)options,nullptr,0}; const uint64_t pipelineBegan=nowUs(); NmbComputePipeline *handle=nmb_compute_pipeline_new_with_options(gDevice,[(NBFunction *)function handle],&creation); diff --git a/bridge/coreimage_host.mm b/bridge/coreimage_host.mm new file mode 100644 index 0000000..9e9ee71 --- /dev/null +++ b/bridge/coreimage_host.mm @@ -0,0 +1,68 @@ +// SPDX-License-Identifier: MIT +// Host instrument: the Mac's Core Image renders a filter chain through a CIContext over the native frontend's device (bridge over a host +// Vulkan driver, MoltenVK on the Mac), as iconservicesagent does on the target. Core Image builds its programs through Metal (stitched +// function graphs, run-time source, its kernel libraries); a program it cannot make ends in a null program. Prints PASS with the pixel it +// read, or FAIL with the frontend's last problem. NMB_SYSTEM_DEVICE=1 runs the Mac's own device as the baseline. The Mac's Core Image is not +// the target's: this names gaps, it qualifies nothing on iPadOS. +#import +#import +#import +#include +#include +#include "../NativeBridgeMetalFrontend.h" +extern "C" size_t native_bridge_metal_telemetry(char *, size_t); + +static void onMissing(const char *cls, const char *sel) {std::fprintf(stderr, "missing selector: %s %s\n", cls, sel);} + +int main(int argc, char **argv) { + @autoreleasepool { + if (argc < 3) return 2; + id device = getenv("NMB_SYSTEM_DEVICE") ? MTLCreateSystemDefaultDevice() : native_bridge_metal_open(argv[1], argv[2], 0x6e6d63, onMissing); + if (!device) {std::printf("FAIL device\n"); return 1;} + // Core Image's DAG path on iPadOS 27 (CI::MetalDAGHelper::get_precompiled_kernel): a kernel of the Core Image kernel library by + // name, though the library names none. argv[3]: such a library (this Mac's ci_stdlib.metallib). + if (argc > 3) { + NSError *error = nil; + id kernels = [device newLibraryWithURL:[NSURL fileURLWithPath:@(argv[3])] error:&error]; + id add = [kernels newFunctionWithName:@"_add" constantValues:[MTLFunctionConstantValues new] error:&error]; + std::printf("KERNELS library %s names %lu _add %s type %lu\n", kernels ? "yes" : "no", (unsigned long)kernels.functionNames.count, + add ? "yes" : "no", (unsigned long)add.functionType); + if (!add) {std::printf("FAIL kernel by name\n"); return 1;} + // As Apple's Metal answers (macOS 26.5.1): no names, the kernel by name, function type 4. + if (kernels.functionNames.count || add.functionType != 4) {std::printf("FAIL kernel library answers\n"); return 1;} + } + id queue = [device newCommandQueue]; + CIContext *context = [CIContext contextWithMTLCommandQueue:queue options:@{kCIContextCacheIntermediates: @NO}]; + if (!context) {std::printf("FAIL CIContext\n"); return 1;} + const NSUInteger side = 64; + MTLTextureDescriptor *d = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm width:side height:side mipmapped:NO]; + d.usage = MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead | MTLTextureUsageShaderWrite; + d.storageMode = MTLStorageModeShared; + id target = [device newTextureWithDescriptor:d]; + // A solid colour through colour controls and a blur: a chain of stitchable kernels, then a separable blur. + CIImage *image = [[CIImage imageWithColor:[CIColor colorWithRed:0.8 green:0.2 blue:0.1]] imageByCroppingToRect:CGRectMake(0, 0, side, side)]; + CIFilter *controls = [CIFilter filterWithName:@"CIColorControls"]; + [controls setValue:image forKey:kCIInputImageKey]; + [controls setValue:@1.2 forKey:kCIInputSaturationKey]; + CIFilter *blur = [CIFilter filterWithName:@"CIGaussianBlur"]; + [blur setValue:controls.outputImage forKey:kCIInputImageKey]; + [blur setValue:@2 forKey:kCIInputRadiusKey]; + CIImage *out = [blur.outputImage imageByCroppingToRect:CGRectMake(0, 0, side, side)]; + id commands = [queue commandBuffer]; + CGColorSpaceRef space = CGColorSpaceCreateWithName(kCGColorSpaceSRGB); + [context render:out toMTLTexture:target commandBuffer:commands bounds:CGRectMake(0, 0, side, side) colorSpace:space]; + CGColorSpaceRelease(space); + [commands commit]; + [commands waitUntilCompleted]; + uint8_t pixel[4] = {}; + [target getBytes:pixel bytesPerRow:side * 4 fromRegion:MTLRegionMake2D(side / 2, side / 2, 1, 1) mipmapLevel:0]; + static char table[16384]; + if (!getenv("NMB_SYSTEM_DEVICE")) { + native_bridge_metal_telemetry(table, sizeof table); + for (char *line = std::strtok(table, "\n"); line; line = std::strtok(nullptr, "\n")) std::printf("TELEMETRY %s\n", line); + } + const bool red = pixel[2] > 150 && pixel[1] < 120 && pixel[0] < 120; + std::printf("%s pixel bgra %u %u %u %u status %lu\n", red ? "PASS" : "FAIL", pixel[0], pixel[1], pixel[2], pixel[3], (unsigned long)commands.status); + return red ? 0 : 1; + } +} From 19e5f404c0a5bc50b75be4cfcafd88a8220de0f3 Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Thu, 29 Oct 2026 11:44:00 +0800 Subject: [PATCH 65/92] 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. --- CacheExport.h | 84 ++- NativeAdrenoMetalPlugin.mm | 9 +- NativeBridgeMetalFrontend.mm | 6 +- bridge/NativeMetalBridge.rs | 4 +- bridge/NativeMetalObjects.rs | 30 +- ...001-optional-constant-output-members.patch | 2 +- ...2-resource-wait-storage-dependencies.patch | 4 +- ...003-cache-device-entry-point-priming.patch | 96 +-- ...004-fold-constant-global-field-loads.patch | 30 +- ...ule-decodes-and-profile-fingerprints.patch | 115 ---- ... 0005-skip-draws-without-a-pipeline.patch} | 12 +- ...h-barriers-and-key-hash-neutral-maps.patch | 101 ++++ .../0007-accept-two-plane-rgb-surfaces.patch | 25 - ...007-optional-shader-cache-directory.patch} | 6 +- ...lias-padded-rows-of-written-textures.patch | 29 - ...verse.patch => 0008-llvm-bitreverse.patch} | 6 +- bridge/patches/0009-keep-final-pass.patch | 140 ----- ...> 0009-patch-with-no-control-points.patch} | 2 +- ...=> 0010-core-image-kernel-libraries.patch} | 18 +- .../0010-fewer-allocations-per-draw.patch | 559 ------------------ bridge/patches/0011-keep-draw-commands.patch | 346 ----------- ...011-two-plane-shadow-as-memory-lies.patch} | 20 +- bridge/patches/0012-keep-draw-lists.patch | 302 ---------- ...-extended-range-blend-rounded-model.patch} | 20 +- ...patch => 0013-growing-memory-blocks.patch} | 52 +- .../patches/0013-skip-unchanged-rebinds.patch | 390 ------------ .../0014-in-place-written-textures.patch | 110 ++++ bridge/patches/0014-map-tracker-keys.patch | 66 --- ...-in-place-written-host-page-textures.patch | 32 + bridge/patches/0016-refuse-rebinds.patch | 69 +++ .../0016-tile-stores-within-rows.patch | 71 --- ...-mipmaps-outside-the-measured-spread.patch | 71 --- ...ure-size-a-multiple-of-its-alignment.patch | 14 - ...-set-viewports-and-scissors-in-place.patch | 69 --- .../0026-keep-push-and-table-bytes.patch | 155 ----- bridge/patches/0027-keep-bind-vectors.patch | 129 ---- .../0028-keep-bound-buffer-vectors.patch | 45 -- .../0029-keep-vertex-buffer-binds.patch | 51 -- ...-hand-draw-rooms-to-the-next-encoder.patch | 139 ----- .../patches/0031-keep-bound-state-room.patch | 89 --- .../0032-reshape-colors-without-cloning.patch | 35 -- ...33-look-up-final-passes-by-reference.patch | 176 ------ .../0034-batch-barriers-on-the-stack.patch | 49 -- ...35-reset-command-pools-as-given-back.patch | 133 ----- .../0036-calibrate-off-the-commit-path.patch | 50 -- .../0037-key-hash-the-draw-path-maps.patch | 278 --------- bridge/prime_host.mm | 75 +++ 47 files changed, 638 insertions(+), 3676 deletions(-) delete mode 100644 bridge/patches/0005-keep-module-decodes-and-profile-fingerprints.patch rename bridge/patches/{0006-skip-draws-without-a-pipeline.patch => 0005-skip-draws-without-a-pipeline.patch} (66%) create mode 100644 bridge/patches/0006-batch-barriers-and-key-hash-neutral-maps.patch delete mode 100644 bridge/patches/0007-accept-two-plane-rgb-surfaces.patch rename bridge/patches/{0017-optional-shader-cache-directory.patch => 0007-optional-shader-cache-directory.patch} (95%) delete mode 100644 bridge/patches/0008-alias-padded-rows-of-written-textures.patch rename bridge/patches/{0019-llvm-bitreverse.patch => 0008-llvm-bitreverse.patch} (93%) delete mode 100644 bridge/patches/0009-keep-final-pass.patch rename bridge/patches/{0021-patch-with-no-control-points.patch => 0009-patch-with-no-control-points.patch} (96%) rename bridge/patches/{0024-core-image-kernel-libraries.patch => 0010-core-image-kernel-libraries.patch} (81%) delete mode 100644 bridge/patches/0010-fewer-allocations-per-draw.patch delete mode 100644 bridge/patches/0011-keep-draw-commands.patch rename bridge/patches/{0015-two-plane-shadow-as-memory-lies.patch => 0011-two-plane-shadow-as-memory-lies.patch} (63%) delete mode 100644 bridge/patches/0012-keep-draw-lists.patch rename bridge/patches/{0018-srgb-extended-range-blend-rounded-model.patch => 0012-srgb-extended-range-blend-rounded-model.patch} (72%) rename bridge/patches/{0020-growing-memory-blocks.patch => 0013-growing-memory-blocks.patch} (72%) delete mode 100644 bridge/patches/0013-skip-unchanged-rebinds.patch create mode 100644 bridge/patches/0014-in-place-written-textures.patch delete mode 100644 bridge/patches/0014-map-tracker-keys.patch create mode 100644 bridge/patches/0015-in-place-written-host-page-textures.patch create mode 100644 bridge/patches/0016-refuse-rebinds.patch delete mode 100644 bridge/patches/0016-tile-stores-within-rows.patch delete mode 100644 bridge/patches/0022-mipmaps-outside-the-measured-spread.patch delete mode 100644 bridge/patches/0023-heap-texture-size-a-multiple-of-its-alignment.patch delete mode 100644 bridge/patches/0025-set-viewports-and-scissors-in-place.patch delete mode 100644 bridge/patches/0026-keep-push-and-table-bytes.patch delete mode 100644 bridge/patches/0027-keep-bind-vectors.patch delete mode 100644 bridge/patches/0028-keep-bound-buffer-vectors.patch delete mode 100644 bridge/patches/0029-keep-vertex-buffer-binds.patch delete mode 100644 bridge/patches/0030-hand-draw-rooms-to-the-next-encoder.patch delete mode 100644 bridge/patches/0031-keep-bound-state-room.patch delete mode 100644 bridge/patches/0032-reshape-colors-without-cloning.patch delete mode 100644 bridge/patches/0033-look-up-final-passes-by-reference.patch delete mode 100644 bridge/patches/0034-batch-barriers-on-the-stack.patch delete mode 100644 bridge/patches/0035-reset-command-pools-as-given-back.patch delete mode 100644 bridge/patches/0036-calibrate-off-the-commit-path.patch delete mode 100644 bridge/patches/0037-key-hash-the-draw-path-maps.patch create mode 100644 bridge/prime_host.mm diff --git a/CacheExport.h b/CacheExport.h index 9fb72ff..a21a01d 100644 --- a/CacheExport.h +++ b/CacheExport.h @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -144,6 +145,8 @@ struct PackReader { using InflateFn = int (*)(z_streamp, int); using EndFn = int (*)(z_streamp); FILE *in = nullptr; + // Where the compressed bytes come from: the pack file, or the import region (importRegion). + std::function source; z_stream z = {}; InflateFn inflateFn = nullptr; EndFn endFn = nullptr; @@ -152,18 +155,25 @@ struct PackReader { uint8_t input[65536]; ~PackReader() {if (open) endFn(&z); if (in) fclose(in);} bool start(const char *path) { + if (!(in = fopen(path, "rb"))) return false; + struct stat st = {}; + if (fstat(fileno(in), &st) != 0 || (uint64_t)st.st_size > kPackLimit) return false; + FILE *file = in; + return startWith([file](void *out, size_t n) {return fread(out, 1, n, file) == n;}, (uint64_t)st.st_size); + } + // A pack of `bytes` bytes (its 16-byte head included) that `read` hands out in order. + bool startWith(std::function read, uint64_t bytes) { static void *lib = dlopen("/usr/lib/libz.1.dylib", RTLD_NOW | RTLD_LOCAL); auto init = lib ? (InitFn)dlsym(lib, "inflateInit_") : nullptr; inflateFn = lib ? (InflateFn)dlsym(lib, "inflate") : nullptr; endFn = lib ? (EndFn)dlsym(lib, "inflateEnd") : nullptr; - if (!init || !inflateFn || !endFn || !(in = fopen(path, "rb"))) return false; - struct stat st = {}; + source = std::move(read); uint8_t head[16]; uint32_t raw = 0, zbytes = 0; - if (fstat(fileno(in), &st) != 0 || (uint64_t)st.st_size > kPackLimit || fread(head, 1, 16, in) != 16 || memcmp(head, "SHEXP001", 8)) return false; + if (!init || !inflateFn || !endFn || !source(head, 16) || memcmp(head, "SHEXP001", 8)) return false; memcpy(&raw, head + 8, 4); memcpy(&zbytes, head + 12, 4); - if ((uint64_t)zbytes != (uint64_t)st.st_size - 16) return false; + if ((uint64_t)zbytes != bytes - 16) return false; left = zbytes; expect = raw; open = init(&z, ZLIB_VERSION, (int)sizeof z) == Z_OK; return open; @@ -175,7 +185,7 @@ struct PackReader { if (ended) return false; if (!z.avail_in && left) { const size_t want = left < sizeof input ? (size_t)left : sizeof input; - if (fread(input, 1, want, in) != want) return false; + if (!source(input, want)) return false; left -= want; z.next_in = input; z.avail_in = (uInt)want; @@ -205,10 +215,14 @@ struct PackReader { // scripts/add-gpu-plugin-data.py --pipeline-pack). Its files go into this process's own cache directory, so they carry this process's owner // and mode; a file that is already there with the same size is kept. Files are written beside their place and renamed only once the // whole pack has been read and checked: a damaged pack imports nothing. Returns the number of files written, or -1. +static int importStream(PackReader &r, const std::string &directory); static int importPack(const char *pack, const std::string &directory) { if (access(pack, R_OK) != 0) return 0; PackReader r; if (!r.start(pack)) return -1; + return importStream(r, directory); +} +static int importStream(PackReader &r, const std::string &directory) { std::vector written; auto fail = [&]() {for (const std::string &f : written) unlink((f + ".import").c_str()); return -1;}; char magic[8]; @@ -251,6 +265,66 @@ static int importPack(const char *pack, const std::string &directory) { } #ifndef CACHEEXPORT_FORMAT_ONLY +// Import region: the pipeline caches compiled ahead on the host (scripts/make-pipeline-cache.py), written to the read-only part of the +// userdata reserve (UniXNU-sheng scripts/make-pipeline-import.py) and read through the GPU driver (method 0x7101, 4096 bytes a call). +// Block 0: "SHNGIMP1", u32 version 1, u32 0, u64 pack bytes, 32 bytes id (the pack's SHA-256); the pack (SHEXP001) from block 1. Its files +// go into this process's cache directory as importPack's do; import-id there then names the pack, and a later boot that finds the same id +// reads nothing more. Markers 0x63nn: 00 started, 01 imported (then 0x64nn: files written in thousands, saturated), 02 already imported, +// f0 no connection, f1 no selector, f2 no region (or empty), f3 the pack was refused. +constexpr uint32_t kImportMethod = 0x7101, kImportBlock = 4096; +static int importRegion(Gpu &marker, std::string root) { + while (root.size() > 1 && root.back() == '/') root.pop_back(); + marker.mark(0x6300); + Gpu link; + link.call = marker.call; link.openFn = marker.openFn; link.closeFn = marker.closeFn; link.port = marker.port; + if (!link.openFn || link.openFn(link.port, mach_task_self(), 0x4148, &link.conn) != 0 || !link.conn) {marker.mark(0x63f0); return -1;} + struct Closer {Gpu &g; ~Closer() {if (g.closeFn && g.conn) g.closeFn(g.conn);}} closer{link}; + if (!link.probe()) {marker.mark(0x63f1); return -1;} + uint64_t region = 0; + std::vector block(kImportBlock); + auto readAt = [&](uint64_t offset, void *out) {return link.method(kImportMethod, &offset, sizeof offset, out, kImportBlock) == 0;}; + if (link.method(kImportMethod, ®ion, sizeof region, ®ion, sizeof region) != 0 || region < 2 * kImportBlock || + !readAt(0, block.data()) || memcmp(block.data(), "SHNGIMP1", 8)) {marker.mark(0x63f2); return 0;} + uint32_t version = 0; + uint64_t bytes = 0; + memcpy(&version, block.data() + 8, 4); + memcpy(&bytes, block.data() + 16, 8); + if (version != 1 || bytes < 16 || bytes > region - kImportBlock) {marker.mark(0x63f2); return 0;} + char id[65]; + for (int k = 0; k < 32; ++k) snprintf(id + 2 * k, 3, "%02x", block[24 + k]); + const std::string idPath = root + "/import-id"; + if (FILE *f = fopen(idPath.c_str(), "r")) { + char seen[65] = {}; + const bool same = fread(seen, 1, 64, f) == 64 && !memcmp(seen, id, 64); + fclose(f); + if (same) {marker.mark(0x6302); return 0;} + } + // The pack's bytes in order, a block per call from block 1. + uint64_t next = kImportBlock; + size_t used = kImportBlock; + auto read = [&](void *out, size_t n) { + uint8_t *to = static_cast(out); + while (n) { + if (used == kImportBlock) { + if (!readAt(next, block.data())) return false; + next += kImportBlock; + used = 0; + } + const size_t k = n < kImportBlock - used ? n : kImportBlock - used; + memcpy(to, block.data() + used, k); + used += k; to += k; n -= k; + } + return true; + }; + PackReader r; + const int written = r.startWith(read, bytes) ? importStream(r, root) : -1; + if (written < 0) {marker.mark(0x63f3); return -1;} + if (FILE *f = fopen(idPath.c_str(), "w")) {fwrite(id, 1, 64, f); fclose(f);} + marker.mark(0x6301); + marker.mark(0x6400 | (uint32_t)(written / 1000 > 0xff ? 0xff : written / 1000)); + return written; +} + // Markers 0x69xx: 00 started, 01 connection, 02 an export went out (then 0x6Bnn: its size in MiB, saturated), f0 no connection, // f1 no selector, f2 a file changed or vanished while it was read, f3 a piece refused, f4 the new files exceed one export. static void run(Gpu &marker, std::string root) { diff --git a/NativeAdrenoMetalPlugin.mm b/NativeAdrenoMetalPlugin.mm index 6c74534..9f2fb85 100644 --- a/NativeAdrenoMetalPlugin.mm +++ b/NativeAdrenoMetalPlugin.mm @@ -420,12 +420,17 @@ static id deviceForPort(unsigned port) { // The translator recurses deeply on large functions: give it the stack of a main thread. pthread_attr_setstacksize(&attributes, 8u << 20); pthread_t primer; - pthread_create(&primer, &attributes, [](void *) -> void * { + // First the pipeline caches compiled ahead on the host, from the import region (CacheExport.h importRegion), which also lists + // every library they cover as primed: priming then compiles only what they left out. The runtime looks its entries up as it + // needs them, so the render server draws meanwhile. + pthread_create(&primer, &attributes, [](void *arg) -> void * { + pthread_set_qos_class_self_np(QOS_CLASS_UTILITY, 0); + cacheexport::importRegion(*static_cast(arg), gCacheDirectory); char list[1024]; prime::run((id)device, gCacheDirectory, 90, requestedPath("NativeAdrenoMetal.primelist", list, sizeof list) ? std::string(list) : std::string()); return nullptr; - }, nullptr); + }, gpu); pthread_attr_destroy(&attributes); gpu->mark(0x6803); return device; diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index bd577f4..15614f2 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -4200,11 +4200,11 @@ extern "C" size_t native_bridge_metal_telemetry(char *out,size_t size) { (unsigned long long)work[0],ms(work[1]),ms(work[2]),ms(work[3]),(unsigned long long)work[4]); row("V cache hit%llu miss%llu wr%llu | msg i%llu w%llu",(unsigned long long)work[7],(unsigned long long)work[8],(unsigned long long)work[9],(unsigned long long)work[5], (unsigned long long)work[6]); - // Where textures over buffers and surfaces cost copies: linear images at Metal's pitch made, refused, refused again; shadowed textures, - // their copies in, texels copied back; passes over linear images in place and through copies. + // Where textures over buffers and surfaces cost copies: host-page textures in place (of them at Metal's pitch), shadowed for their + // pitch and for no driver image; shadowed textures, their copies in, texels copied back; host-page render targets shadowed. uint64_t tc[8]={}; if(nmb_device_texture_counts(gDevice,tc)) - row("L lin %llu/%llu/%llu sh %llu cp %llu bk %llu pass %llu/%llu",(unsigned long long)tc[0],(unsigned long long)tc[1],(unsigned long long)tc[2], + row("L inp %llu/%llu rf %llu/%llu sh %llu cp %llu bk %llu rt %llu",(unsigned long long)tc[0],(unsigned long long)tc[1],(unsigned long long)tc[2], (unsigned long long)tc[3],(unsigned long long)tc[4],(unsigned long long)tc[5],(unsigned long long)tc[6],(unsigned long long)tc[7]); char warning[200];size_t n=nmb_last_warning(warning,sizeof warning); if(n){row("W %.86s",warning);if(n>86)row("Wb %.86s",warning+86);} diff --git a/bridge/NativeMetalBridge.rs b/bridge/NativeMetalBridge.rs index 997735f..8947759 100644 --- a/bridge/NativeMetalBridge.rs +++ b/bridge/NativeMetalBridge.rs @@ -17,7 +17,7 @@ pub mod objects; use metal2vulkan::metal::{PixelFormat, PrimitiveType, TextureUsage}; use metal2vulkan::pipeline::{ColorAttachmentDescriptor, RenderPipelineDescriptor, StageSource}; use metal2vulkan_runtime::{ - BlitOption, ClearColor, CommandBuffer, CommandBufferStatus, Device, DeviceOptions, HostMemory, LoadAction, MetalLevel, + BlitOption, ClearColor, CommandBuffer, CommandBufferStatus, Device, DeviceOptions, HostMemory, LoadAction, Origin, PipelineOptions, RenderPassDescriptor, ResourceOptions, Size, StoreAction, TextureDescriptor, }; use std::ffi::{c_char, c_void, CStr}; @@ -88,8 +88,6 @@ fn finish(command_buffer: &CommandBuffer) -> Result<(), String> { fn open(loader: &CStr, cache: &CStr) -> Result { let mut options = DeviceOptions::default(); options.loader = Some(PathBuf::from(loader.to_string_lossy().into_owned())); - // No macOS release names this host; answer as the newest measured Metal. - options.metal_level = Some(MetalLevel::NEWEST); options.shader_cache = metal2vulkan_runtime::ShaderCacheOptions::in_directory(cache.to_string_lossy().into_owned()); Device::system_default_with(&options).map_err(|e| format!("device: {e:?}")) } diff --git a/bridge/NativeMetalObjects.rs b/bridge/NativeMetalObjects.rs index 744d794..a994690 100644 --- a/bridge/NativeMetalObjects.rs +++ b/bridge/NativeMetalObjects.rs @@ -20,7 +20,7 @@ use metal2vulkan::pipeline::{ use metal2vulkan_runtime::{ BinaryArchive, BinaryArchiveDescriptor, BlitCommandEncoder, FamilyRow, FeatureRow, GpuFamily, RowState, BlitOption, Buffer, ClearColor, CommandBuffer, CommandQueue, ComputeCommandEncoder, ComputePipelineState, Deallocator, DebugMessage, DepthStencilState, Device, DeviceOptions, MessageSeverity, MessageSink, Event, EventRef, Fence, Function, Heap, HeapDescriptor, HeapType, HostMemory, IoSurfacePlane, IoSurfaceYCbCr, Library, LoadAction, - MetalLevel, Origin, PipelineOption, PipelineOptions, PurgeableState, PrivateCount, PrivateFlag, PrivateProperties, MAX_COLOR_ATTACHMENTS, Region, RenderCommandEncoder, RenderPassDescriptor, RenderPipelineState, ResourceOptions, + Origin, PipelineOption, PipelineOptions, PurgeableState, PrivateCount, PrivateFlag, PrivateProperties, MAX_COLOR_ATTACHMENTS, Region, RenderCommandEncoder, RenderPassDescriptor, RenderPipelineState, ResourceOptions, SamplerState, ScissorRect, SharedEvent, SharedEventListener, Size, StoreAction, Texture, TextureDescriptor, Viewport, VisibilityResultMode, }; use std::cell::RefCell; @@ -177,16 +177,20 @@ pub unsafe extern "C" fn nmb_device_open(loader: *const c_char, cache: *const c_ let (loader, cache) = unsafe { (CStr::from_ptr(loader), CStr::from_ptr(cache)) }; let mut options = DeviceOptions::default(); options.loader = Some(PathBuf::from(loader.to_string_lossy().into_owned())); - options.metal_level = Some(MetalLevel::NEWEST); options.shader_cache = metal2vulkan_runtime::ShaderCacheOptions::in_directory(cache.to_string_lossy().into_owned()); options.messages = Some(MessageSink::new(observe_message)); - // A texture over a buffer whose rows lie apart is an image over the bytes, rendered or written too: the window server's padded - // framebuffers (3048x2032 BGRA8 at 12288-byte rows, int-251) were otherwise copied in and out of a shadow image every pass. - options.policy.padded_reads = metal2vulkan::device::PaddedReadsPolicy::AliasWrites; + // A texture over host pages or a buffer whose rows lie apart is an image over the bytes, rendered or written too: the window + // server's surfaces and padded framebuffers (3048x2032 BGRA8 at 12288-byte rows, int-251) were otherwise copied in and out of a + // shadow image every pass. + options.policy.host_page_textures = metal2vulkan::device::HostPageTexturePolicy::InPlaceWrites; // Every texture given a shadow image is announced with why no image aliases its bytes (`shadow.log`). options.announce_each.insert(metal2vulkan_runtime::Degradation::ShadowedTexture); let _ = SHADOW_LOG.set(PathBuf::from(cache.to_string_lossy().into_owned()).join("shadow.log")); - Ok(boxed(NmbDevice(Device::system_default_with(&options).map_err(e("device"))?, std::sync::OnceLock::new(), std::sync::Mutex::default()))) + let device = Device::system_default_with(&options).map_err(e("device"))?; + // No caller moves or detaches the pages of a no-copy buffer (the frontend has no rebind or detach): the encoders log no calls + // to encode again and hold no surface's buffer per command, which every frame of the render server paid for. + device.refuse_rebinds(); + Ok(boxed(NmbDevice(device, std::sync::OnceLock::new(), std::sync::Mutex::default()))) }) } @@ -302,9 +306,9 @@ pub extern "C" fn nmb_device_work_stats(device: *const NmbDevice, out: *mut u64) }) } -/// The device's texture copy counts into the caller's eight words: linear images at Metal's row pitch made, refused by the driver and -/// refused again from memory, textures given a shadow image, shadow copies in and texels copied back, and passes over linear images -/// rendered in place and through copies. +/// The device's texture copy counts into the caller's eight words: textures over host pages in place, of them linear images at Metal's +/// row pitch, host-page textures shadowed for their pitch and for no image the driver makes, textures given a shadow image, shadow +/// copies in and texels copied back, and host-page textures shadowed as render targets. #[unsafe(no_mangle)] pub extern "C" fn nmb_device_texture_counts(device: *const NmbDevice, out: *mut u64) -> bool { guard(false, || { @@ -314,9 +318,9 @@ pub extern "C" fn nmb_device_texture_counts(device: *const NmbDevice, out: *mut // SAFETY: the header's eight words. let out = unsafe { std::slice::from_raw_parts_mut(out, 8) }; let w = get(device)?.0.work_counts(); - out.copy_from_slice(&[w.pitched_host_images, w.pitched_host_image_refusals, w.pitched_host_image_refusals_recalled, - w.shadowed_textures, w.shadow_copies, w.shadow_texels_copied_back, w.linear_passes_in_place, - w.linear_passes_through_copies]); + out.copy_from_slice(&[w.host_textures_in_place, w.pitched_host_images, w.host_texture_shadows_refused_pitch, + w.host_texture_shadows_refused_image, w.shadowed_textures, w.shadow_copies, w.shadow_texels_copied_back, + w.host_texture_shadows_render_target]); Ok(true) }) } @@ -2134,7 +2138,7 @@ fn shared<'a>(event: *const NmbEvent) -> Result<&'a SharedEvent, String> { /// forwards to it). fn listener() -> Result<&'static SharedEventListener, String> { static LISTENER: std::sync::OnceLock> = std::sync::OnceLock::new(); - LISTENER.get_or_init(|| SharedEventListener::new().map_err(e("listener"))).as_ref().map_err(|why| why.clone()) + LISTENER.get_or_init(|| Ok(SharedEventListener::new())).as_ref().map_err(|why: &String| why.clone()) } #[unsafe(no_mangle)] diff --git a/bridge/patches/0001-optional-constant-output-members.patch b/bridge/patches/0001-optional-constant-output-members.patch index 8042565..09e4f3a 100644 --- a/bridge/patches/0001-optional-constant-output-members.patch +++ b/bridge/patches/0001-optional-constant-output-members.patch @@ -1,5 +1,5 @@ diff --git a/crates/metal2vulkan-air/src/canon/sccp.rs b/crates/metal2vulkan-air/src/canon/sccp.rs -index e47e683..f89e2fc 100644 +index e47e6833..f89e2fc1 100644 --- a/crates/metal2vulkan-air/src/canon/sccp.rs +++ b/crates/metal2vulkan-air/src/canon/sccp.rs @@ -995,6 +995,30 @@ impl<'b> Solver<'b> { diff --git a/bridge/patches/0002-resource-wait-storage-dependencies.patch b/bridge/patches/0002-resource-wait-storage-dependencies.patch index 6177488..23fd68e 100644 --- a/bridge/patches/0002-resource-wait-storage-dependencies.patch +++ b/bridge/patches/0002-resource-wait-storage-dependencies.patch @@ -1,8 +1,8 @@ diff --git a/crates/metal2vulkan-runtime/src/used_resources.rs b/crates/metal2vulkan-runtime/src/used_resources.rs -index 7521345..03001a8 100644 +index 61f56de6..54e1551f 100644 --- a/crates/metal2vulkan-runtime/src/used_resources.rs +++ b/crates/metal2vulkan-runtime/src/used_resources.rs -@@ -58,6 +58,62 @@ pub struct UsedResources { +@@ -59,6 +59,62 @@ pub struct UsedResources { pub heaps: Vec, } diff --git a/bridge/patches/0003-cache-device-entry-point-priming.patch b/bridge/patches/0003-cache-device-entry-point-priming.patch index 47bd1ea..7af9f41 100644 --- a/bridge/patches/0003-cache-device-entry-point-priming.patch +++ b/bridge/patches/0003-cache-device-entry-point-priming.patch @@ -1,17 +1,27 @@ diff --git a/crates/metal2vulkan-runtime/src/device/mod.rs b/crates/metal2vulkan-runtime/src/device/mod.rs -index 990c892..00a6e55 100644 +index a47d32ea..784315fb 100644 --- a/crates/metal2vulkan-runtime/src/device/mod.rs +++ b/crates/metal2vulkan-runtime/src/device/mod.rs -@@ -962,6 +962,21 @@ impl Device { +@@ -1503,6 +1503,31 @@ impl Device { // SAFETY: the device is live, of `physical`, and enables timeline semaphores, and // `bufferDeviceAddress` where `device_address` says so; `queues` holds at least one of // its queues, which nothing uses yet. -+ let priming_has = prime::Has { ++ let priming_has = drawn.has(prime::Has { + timestamps: timestamp_bits > 0, ++ // A device the runtime created enables it (`enables.rs`). ++ host_query_reset: owned, + device_addresses: device_address, + generated_commands: generated_commands.is_some(), + sparse_binding: sparse.binds(), -+ }; ++ push_descriptor: push_descriptor.is_some(), ++ calibration: calibrated.is_some(), ++ object_names: object_name.is_some(), ++ extended_dynamic_state: false, ++ primitive_restart: false, ++ color_write_masks: false, ++ sized_index_bind: false, ++ ycbcr_conversion: ycbcr_formats.primer().is_some(), ++ }); + // Raw disk/cache I/O only: starting the ordinary guarded cache or its + // worker before priming would change the destructor ordering it protects. + let priming_cache = cache.priming( @@ -24,27 +34,41 @@ index 990c892..00a6e55 100644 if let Err(error) = unsafe { prime::prime( &instance.raw, -@@ -971,13 +986,9 @@ impl Device { +@@ -1518,27 +1543,13 @@ impl Device { queue_family, &queues, Sharing::new(&queue_families), -- prime::Has { +- drawn.has(prime::Has { - timestamps: timestamp_bits > 0, +- // A device the runtime created enables it (`enables.rs`). +- host_query_reset: owned, - device_addresses: device_address, - generated_commands: generated_commands.is_some(), - sparse_binding: sparse.binds(), -- }, +- push_descriptor: push_descriptor.is_some(), +- calibration: calibrated.is_some(), +- object_names: object_name.is_some(), +- extended_dynamic_state: false, +- primitive_restart: false, +- color_write_masks: false, +- sized_index_bind: false, +- ycbcr_conversion: ycbcr_formats.primer().is_some(), +- }), + priming_has, generated_commands.as_ref(), + push_descriptor, + calibrated, + drawn, + ycbcr_formats.primer(), + priming_cache, ) } { scheduler.stop(); diff --git a/crates/metal2vulkan-runtime/src/device/prime.rs b/crates/metal2vulkan-runtime/src/device/prime.rs -index 91791ba..50856aa 100644 +index e953806b..29671308 100644 --- a/crates/metal2vulkan-runtime/src/device/prime.rs +++ b/crates/metal2vulkan-runtime/src/device/prime.rs -@@ -52,9 +52,11 @@ use crate::generated_commands::{ +@@ -82,9 +82,11 @@ use crate::gpu_time::Calibration; use crate::loader_word::LoaderWord; use crate::memory; use crate::release::{self, Generated, Released}; @@ -55,18 +79,18 @@ index 91791ba..50856aa 100644 +use sha2::{Digest, Sha256}; use std::time::{Duration, Instant}; - use super::QueueSlot; -@@ -184,7 +186,7 @@ const VERTEX_SHADER: [u32; 29] = [ - - /// What the device primed has: a queue family that writes timestamps, device addresses, - /// `VK_EXT_device_generated_commands`, and sparse binding on its queues. + use super::functions::Vk; +@@ -361,7 +363,7 @@ fn specialization() -> vk::SpecializationInfo<'static> { + /// What the device primed has: a queue family that writes timestamps, `hostQueryReset`, device + /// addresses, `VK_EXT_device_generated_commands`, sparse binding on its queues, the push command, + /// calibrated timestamps, and the render encoder's commands of [`PrimedDraws`]. -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Debug)] pub(crate) struct Has { pub(crate) timestamps: bool, - pub(crate) device_addresses: bool, -@@ -192,6 +194,16 @@ pub(crate) struct Has { - pub(crate) sparse_binding: bool, + pub(crate) host_query_reset: bool, +@@ -403,6 +405,16 @@ impl PrimedDraws { + } } +/// Identity includes every byte of the helper implementation, enabled compile @@ -81,16 +105,16 @@ index 91791ba..50856aa 100644 + /// Prime `device` of `physical`, made from `instance`, whose queues are `queues`, the first of /// its primary family `queue_family`, its buffers and images shared as `sharing` says, on this - /// thread; `has` says what the device has, and `generated` holds the commands of -@@ -213,6 +225,7 @@ pub(super) unsafe fn prime( - sharing: Sharing<'_>, - has: Has, - generated: Option<&Commands>, + /// thread; `has` says what the device has, `generated` holds the commands of +@@ -430,6 +442,7 @@ pub(super) unsafe fn prime( + calibration: Option, + drawn: PrimedDraws, + ycbcr: Option>, + mut disk: PrimingCache, ) -> Result<(), Error> { let mut primer = Primer { device, -@@ -222,6 +235,8 @@ pub(super) unsafe fn prime( +@@ -442,6 +455,8 @@ pub(super) unsafe fn prime( memory: unsafe { instance.get_physical_device_memory_properties(physical) }, calls: 0, held: Vec::new(), @@ -99,7 +123,7 @@ index 91791ba..50856aa 100644 }; // SAFETY: the caller's. let primed = unsafe { -@@ -239,6 +254,10 @@ pub(super) unsafe fn prime( +@@ -460,6 +475,10 @@ pub(super) unsafe fn prime( unsafe { let _ = device.device_wait_idle(); primer.made(Call::DeviceWaitIdle); @@ -110,7 +134,7 @@ index 91791ba..50856aa 100644 while let Some(release) = primer.held.pop() { let call = release(device); primer.made(call); -@@ -303,6 +322,8 @@ struct Primer<'a> { +@@ -547,6 +566,8 @@ struct Primer<'a> { calls: u128, /// What was made, released last first. held: Vec, @@ -119,7 +143,7 @@ index 91791ba..50856aa 100644 } impl Primer<'_> { -@@ -575,7 +596,7 @@ impl Primer<'_> { +@@ -840,7 +861,7 @@ impl Primer<'_> { .layout(layout) .push_next(&mut flags); let pipeline = device @@ -128,7 +152,7 @@ index 91791ba..50856aa 100644 .map_err(|(_, error)| vulkan(error))?[0]; self.hold(pipeline); let pipelines = ExecutionSetPipelineInfo::new(pipeline, 1); -@@ -848,9 +869,8 @@ impl Primer<'_> { +@@ -1171,9 +1192,8 @@ impl Primer<'_> { let module = device.create_shader_module(&info, None).map_err(vulkan)?; self.made(Call::CreateShaderModule); self.hold(module); @@ -139,12 +163,12 @@ index 91791ba..50856aa 100644 + self.pipeline_cache = cache; self.made(Call::CreatePipelineCache); self.hold(cache); - let stage = vk::PipelineShaderStageCreateInfo::default() + let specialization = specialization(); diff --git a/crates/metal2vulkan-runtime/src/shader_cache/mod.rs b/crates/metal2vulkan-runtime/src/shader_cache/mod.rs -index 88e0fad..513fe37 100644 +index c0e8d025..a9da76d1 100644 --- a/crates/metal2vulkan-runtime/src/shader_cache/mod.rs +++ b/crates/metal2vulkan-runtime/src/shader_cache/mod.rs -@@ -282,6 +282,106 @@ impl CacheSetup { +@@ -380,6 +380,106 @@ impl CacheSetup { } } @@ -168,14 +192,14 @@ index 88e0fad..513fe37 100644 + sink: Option, + ) -> PrimingCache { + let identity = driver::Identity::of(properties); -+ let name = Records { ++ let records = Records { + identity, + settings, + profile, -+ } -+ .name(Making::new("device-entry-point-priming").digest(&key)); ++ }; ++ let name = records.name(&records.key(Making::new("device-entry-point-priming").digest(&key))); + PrimingCache { -+ store: Store::new(self.directory.clone(), sink), ++ store: Store::new(self.directory.clone(), sink, None), + identity, + name, + loaded: None, @@ -240,7 +264,7 @@ index 88e0fad..513fe37 100644 + } + if self + .store -+ .write_record(&self.name, &driver::seal(&self.identity, &data)) ++ .write_record_soon(&self.name, driver::seal(&self.identity, &data)) + .is_err() + { + self.store.write_failed(); @@ -249,5 +273,5 @@ index 88e0fad..513fe37 100644 +} + /// One device's shader cache: its memo, its view of the directory, what names its makings' - /// records, and the cache thread. + /// records, the `VkPipelineCache` it keeps for each record, and the cache thread. pub(super) struct ShaderCache { diff --git a/bridge/patches/0004-fold-constant-global-field-loads.patch b/bridge/patches/0004-fold-constant-global-field-loads.patch index 0242699..3fa5d98 100644 --- a/bridge/patches/0004-fold-constant-global-field-loads.patch +++ b/bridge/patches/0004-fold-constant-global-field-loads.patch @@ -1,5 +1,5 @@ diff --git a/crates/metal2vulkan-air/src/canon/const_loads.rs b/crates/metal2vulkan-air/src/canon/const_loads.rs -index 7b3dfd7..98976df 100644 +index 7b3dfd7b..98976df2 100644 --- a/crates/metal2vulkan-air/src/canon/const_loads.rs +++ b/crates/metal2vulkan-air/src/canon/const_loads.rs @@ -1,4 +1,4 @@ @@ -119,10 +119,10 @@ index 7b3dfd7..98976df 100644 }; let Some((g, path)) = field_address(body, parts, ptr) else { diff --git a/crates/metal2vulkan-air/src/canon/mod.rs b/crates/metal2vulkan-air/src/canon/mod.rs -index bd05d94..de7b8e6 100644 +index ac47a0cc..cf585587 100644 --- a/crates/metal2vulkan-air/src/canon/mod.rs +++ b/crates/metal2vulkan-air/src/canon/mod.rs -@@ -484,6 +484,7 @@ pub fn specialize_dynamic( +@@ -508,6 +508,7 @@ pub fn specialize_dynamic( let emap = erase::erase_consts(&mut parts.consts, m)?; erase::erase_globals(parts.globals.values_mut(), &emap); erase_functions(&mut parts, &emap, m)?; @@ -130,7 +130,7 @@ index bd05d94..de7b8e6 100644 // The entry block always runs: a value use there of an unsupplied constant is SCCP's first // refusal, known before the inlined body is built. let entry_refusal = |consts: &ConstTable, m: &mut Meter| { -@@ -567,11 +568,17 @@ pub fn specialize_dynamic( +@@ -591,11 +592,17 @@ pub fn specialize_dynamic( }; body.release(m); let body = solved; @@ -153,7 +153,7 @@ index bd05d94..de7b8e6 100644 let solved = { let mut cx = sccp::Context { folder: fold::Folder { -@@ -583,13 +590,11 @@ pub fn specialize_dynamic( +@@ -607,13 +614,11 @@ pub fn specialize_dynamic( fcs: &fcs, name: name.clone(), }; @@ -170,8 +170,8 @@ index bd05d94..de7b8e6 100644 + } matmul2d::refuse_left(&body, &parts, &ep.stage, &name, m)?; post_check(&body, &parts, &stubs, &name, &control_points(ep), m)?; - // Rasterization-rate maps: each `float2` map call becomes Apple's GPU decoder of the parameter -@@ -783,6 +788,70 @@ fn erased_entry( + // A branch's short arms that Metal's backend speculates, where a group operation in them +@@ -810,6 +815,70 @@ fn erased_entry( } /// Erase every defined function's body in place, one at a time, each body replaced freed. @@ -243,10 +243,10 @@ index bd05d94..de7b8e6 100644 parts: &mut ModuleParts, emap: &[crate::ir::ConstId], diff --git a/crates/metal2vulkan-air/src/canon/tests.rs b/crates/metal2vulkan-air/src/canon/tests.rs -index fb0dad5..93edc0a 100644 +index c455ce0c..0041c775 100644 --- a/crates/metal2vulkan-air/src/canon/tests.rs +++ b/crates/metal2vulkan-air/src/canon/tests.rs -@@ -1658,7 +1658,18 @@ fn a_deeply_nested_written_global_is_rebuilt_on_a_small_stack() { +@@ -1678,7 +1678,18 @@ fn a_deeply_nested_written_global_is_rebuilt_on_a_small_stack() { static_init(&mut m, "init", |f| store(f, five, gaddr)); let fty = m.fn_ty(void, &[dev]); let mut f = m.define("k", fty); @@ -266,7 +266,7 @@ index fb0dad5..93edc0a 100644 store(&mut f, v, arg(0)); f.ret(None); let k = f.finish(); -@@ -2359,9 +2370,12 @@ fn a_pointer_loaded_from_a_constant_globals_field_is_the_fields_constant() { +@@ -2379,9 +2390,12 @@ fn a_pointer_loaded_from_a_constant_globals_field_is_the_fields_constant() { assert!(text.contains(kept) && !text.contains(gone), "{text}"); } } @@ -283,10 +283,10 @@ index fb0dad5..93edc0a 100644 assert_eq!( picked_field(None, 0).unwrap_err(), diff --git a/crates/metal2vulkan/src/tests/engine_arena.rs b/crates/metal2vulkan/src/tests/engine_arena.rs -index b138b07..6b1202f 100644 +index 4e9aa6f7..d7666edd 100644 --- a/crates/metal2vulkan/src/tests/engine_arena.rs +++ b/crates/metal2vulkan/src/tests/engine_arena.rs -@@ -1366,7 +1366,7 @@ fn struct_dag_kernel(depth: u32, constant: bool) -> String { +@@ -1367,7 +1367,7 @@ fn struct_dag_kernel(depth: u32, constant: bool) -> String { let read = if constant { types += &format!("@g = internal addrspace(2) constant %e{depth} zeroinitializer, align 4\n"); @@ -295,7 +295,7 @@ index b138b07..6b1202f 100644 } else { " %v0 = load i32, ptr %a, align 4" }; -@@ -1437,7 +1437,8 @@ fn a_struct_dag_is_planned_in_linear_work() { +@@ -1438,7 +1438,8 @@ fn a_struct_dag_is_planned_in_linear_work() { } /// A struct DAG within the function's room is typed, and keeps its values: `a`'s two words read @@ -305,7 +305,7 @@ index b138b07..6b1202f 100644 /// the room and `a` has its place in the private arena; at 15 levels neither fits, so `@g` is in /// the constant blob too. Each passes L3, and the values are the same typed (3 levels) and untyped /// (15 levels). The CPU interpreter walks a struct type's every path to lay it out, so it does not -@@ -1501,6 +1502,7 @@ fn a_struct_dag_keeps_its_values_typed_or_untyped() { +@@ -1502,6 +1503,7 @@ fn a_struct_dag_keeps_its_values_typed_or_untyped() { /// `%e13`) and `%big`'s `32,767 + extra`: with 17 extra members exactly `alloca::MAX_UNSHARED` /// (65,536), with 18 one more. With `constant`, `%big` is the type of `@g`, a closed constant whose /// DAG halves are zero and whose extra member `i` is `i + 1`, and `v0 = t + @g.2`, `v1 = @g.18`; @@ -313,7 +313,7 @@ index b138b07..6b1202f 100644 /// otherwise of the alloca `a`, which stores `t` at byte 0 and 7 in member 2 (byte 65,536) and /// reads them back as `v0` and `v1`. fn room_kernel(extra: u32, constant: bool) -> String { -@@ -1518,10 +1520,10 @@ fn room_kernel(extra: u32, constant: bool) -> String { +@@ -1519,10 +1521,10 @@ fn room_kernel(extra: u32, constant: bool) -> String { "@g = internal addrspace(2) constant %big {{ %e13 zeroinitializer, %e13 zeroinitializer{ints} }}, align 4\n" ); r#" %p2 = getelementptr inbounds %big, ptr addrspace(2) @g, i32 0, i32 2 diff --git a/bridge/patches/0005-keep-module-decodes-and-profile-fingerprints.patch b/bridge/patches/0005-keep-module-decodes-and-profile-fingerprints.patch deleted file mode 100644 index 0f0a892..0000000 --- a/bridge/patches/0005-keep-module-decodes-and-profile-fingerprints.patch +++ /dev/null @@ -1,115 +0,0 @@ -diff --git a/crates/metal2vulkan/src/device/profile.rs b/crates/metal2vulkan/src/device/profile.rs -index 58c79ce..2684ba6 100644 ---- a/crates/metal2vulkan/src/device/profile.rs -+++ b/crates/metal2vulkan/src/device/profile.rs -@@ -342,7 +342,30 @@ impl DeviceProfile { - /// The rendering streams into the hash: every compile takes the fingerprint for its key, and a - /// materialized rendering of a profile with a format table is tens of kilobytes that no - /// input-scaled ledger charge accounts for. -+ /// -+ /// Every compile takes it, and rendering a profile with a format table cost a twentieth of a -+ /// pipeline's translation, so the last few profiles' fingerprints are kept and found again by -+ /// equality of the fields, which is a structural comparison and holds under any mutation. - pub fn fingerprint(&self) -> Hash256 { -+ static KEPT: std::sync::Mutex> = -+ std::sync::Mutex::new(Vec::new()); -+ { -+ let kept = KEPT.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); -+ if let Some((_, h)) = kept.iter().find(|(f, _)| *f == self.fields) { -+ return *h; -+ } -+ } -+ let h = self.render_fingerprint(); -+ let mut kept = KEPT.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); -+ if kept.len() >= 4 { -+ kept.remove(0); -+ } -+ kept.push((self.fields.clone(), h)); -+ h -+ } -+ -+ /// [`DeviceProfile::fingerprint`], rendered. -+ fn render_fingerprint(&self) -> Hash256 { - use sha2::{Digest, Sha256}; - use std::fmt::Write as _; - struct Hashing(Sha256); -diff --git a/crates/metal2vulkan/src/pipeline/compile.rs b/crates/metal2vulkan/src/pipeline/compile.rs -index 6a67add..c448dca 100644 ---- a/crates/metal2vulkan/src/pipeline/compile.rs -+++ b/crates/metal2vulkan/src/pipeline/compile.rs -@@ -1803,8 +1803,7 @@ fn specialize_one(r: &Request<'_>, m: &mut Meter) -> Result - let kept = entries.specialized(fd, r.inputs.call_stack_depth(), m)?; - return Ok(Specialized::clone(&kept)); - } -- let air_hash = container::sha256(r.bitcode); -- let mut decoded = air::decode(r.bitcode, m)?; -+ let (air_hash, mut decoded) = decode_kept(r.bitcode, m)?; - if let Some(f) = r.container { - decoded.abi.check_container(&decoded.module, f, m)?; - } -@@ -1828,6 +1827,65 @@ fn specialize_one(r: &Request<'_>, m: &mut Meter) -> Result - }) - } - -+/// The smallest module [`decode_kept`] keeps: below it a decode costs less than the copy. -+const KEPT_DECODE_BYTES: usize = 32 << 10; -+/// The modules [`decode_kept`] keeps, the most recently used first. A decoded QuartzCore uber -+/// module is 2.25 MB charged, so four bound the process's cost near 9 MB. -+const KEPT_DECODES: usize = 4; -+ -+/// A module [`decode_kept`] decoded: its bytes, their SHA-256, the decode, and the ledger bytes the -+/// decode left charged. -+struct KeptDecode { -+ bytes: Arc<[u8]>, -+ sha: Hash256, -+ decoded: Arc, -+ charged: u64, -+} -+ -+static KEPT: std::sync::Mutex> = std::sync::Mutex::new(VecDeque::new()); -+ -+/// The SHA-256 of `bitcode` and its decode. A process specializes the same few large modules over -+/// and over (an uber-shader's every variant: QuartzCore's `fixed_frag` modules, 113 KB of bitcode -+/// each, one per pipeline its render server makes), and decoding one was a sixth of a -+/// specialization. A module of at least [`KEPT_DECODE_BYTES`] is decoded once per process and -+/// kept (the last [`KEPT_DECODES`]), found again by its exact bytes; a later request takes a copy -+/// of the decode and charges `m` the bytes the first decode left charged, so the ledger stands as -+/// after a decode of its own. The decode is a pure function of the bytes. -+fn decode_kept(bitcode: &[u8], m: &mut Meter) -> Result<(Hash256, air::Decoded), Error> { -+ if bitcode.len() < KEPT_DECODE_BYTES { -+ return Ok((container::sha256(bitcode), air::decode(bitcode, m)?)); -+ } -+ let found = { -+ let mut kept = KEPT.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); -+ kept.iter() -+ .position(|k| *k.bytes == *bitcode) -+ .and_then(|at| kept.remove(at)) -+ .map(|k| { -+ let found = (k.sha, Arc::clone(&k.decoded), k.charged); -+ kept.push_front(k); -+ found -+ }) -+ }; -+ if let Some((sha, decoded, charged)) = found { -+ m.charge(usize::try_from(charged).unwrap_or(usize::MAX))?; -+ return Ok((sha, air::Decoded::clone(&decoded))); -+ } -+ let before = m.ledger(); -+ let decoded = air::decode(bitcode, m)?; -+ let charged = m.ledger().saturating_sub(before); -+ let sha = container::sha256(bitcode); -+ let entry = KeptDecode { -+ bytes: Arc::from(bitcode), -+ sha, -+ decoded: Arc::new(decoded.clone()), -+ charged, -+ }; -+ let mut kept = KEPT.lock().unwrap_or_else(|poisoned| poisoned.into_inner()); -+ kept.push_front(entry); -+ kept.truncate(KEPT_DECODES); -+ Ok((sha, decoded)) -+} -+ - /// [`specialize_one`], then Metal's grouping of multiply chains (`lower::arith::regroup`) and of - /// fast-math sums (`lower::arith::reassoc`), and the order Apple's simdgroup lockstep gives - /// threadgroup accesses made explicit for `p`: the entry selection reads, whose instruction indices diff --git a/bridge/patches/0006-skip-draws-without-a-pipeline.patch b/bridge/patches/0005-skip-draws-without-a-pipeline.patch similarity index 66% rename from bridge/patches/0006-skip-draws-without-a-pipeline.patch rename to bridge/patches/0005-skip-draws-without-a-pipeline.patch index 880b688..1cfde24 100644 --- a/bridge/patches/0006-skip-draws-without-a-pipeline.patch +++ b/bridge/patches/0005-skip-draws-without-a-pipeline.patch @@ -1,15 +1,15 @@ diff --git a/crates/metal2vulkan-runtime/src/render.rs b/crates/metal2vulkan-runtime/src/render.rs -index 870bdb1..119119d 100644 +index 812f5d08..8495e82f 100644 --- a/crates/metal2vulkan-runtime/src/render.rs +++ b/crates/metal2vulkan-runtime/src/render.rs -@@ -7788,6 +7788,11 @@ impl Recording<'_> { - /// is live until the command buffer completes, and `bound` is the record of what the draws - /// recorded into it bound (each recorded through it). - unsafe fn record(&self, bound: &mut Bound) -> u64 { +@@ -10970,6 +10970,11 @@ impl Recording<'_> { + /// recorded into it bound (each recorded through it). With a pair, the pass is one + /// `render_target` made, of no views (`snapshot::Pair::record`). + unsafe fn record(&self, bound: &mut Bound) -> Recorded { + // A draw whose pipeline could not be created (the compile was refused) has a null pipeline; binding it crashes the driver + // (int-170: tu_CmdBindPipeline in the render server). The draw is dropped instead, and nothing is recorded for it. + if self.pipeline == vk::Pipeline::null() { -+ return 0; ++ return Recorded::default(); + } let (raw, cb) = (self.raw, self.command_buffer); let dynamic = |state| self.dynamic.contains(&state); diff --git a/bridge/patches/0006-batch-barriers-and-key-hash-neutral-maps.patch b/bridge/patches/0006-batch-barriers-and-key-hash-neutral-maps.patch new file mode 100644 index 0000000..2497380 --- /dev/null +++ b/bridge/patches/0006-batch-barriers-and-key-hash-neutral-maps.patch @@ -0,0 +1,101 @@ +diff --git a/crates/metal2vulkan-runtime/src/command_buffer/submit.rs b/crates/metal2vulkan-runtime/src/command_buffer/submit.rs +index 6ab175dd..0cce8207 100644 +--- a/crates/metal2vulkan-runtime/src/command_buffer/submit.rs ++++ b/crates/metal2vulkan-runtime/src/command_buffer/submit.rs +@@ -648,11 +648,8 @@ impl CommandBufferShared { + barriers.first().is_some_and(Option::is_some) || !held_moves.transitions(0).is_empty(); + if entry.is_some() { + started = true; +- let globals: Vec = completed_barrier +- .take() +- .into_iter() +- .chain(entry_barrier) +- .collect(); ++ let (pair, count) = barrier_pair(completed_barrier.take(), entry_barrier); ++ let globals = &pair[..count]; + let stamps = Stamps { + start: start_stamp.filter(|_| !encoder_behind), + after: entry_barrier.is_some(), +@@ -661,14 +658,11 @@ impl CommandBufferShared { + if stamps.start.is_some() { + start_stamp = None; + } +- work.push(self.record_barriers(&mut state, &globals, &entries, stamps)?); ++ work.push(self.record_barriers(&mut state, globals, &entries, stamps)?); + } + for (offset, barrier) in barriers.iter().enumerate() { +- let globals: Vec = completed_barrier +- .take() +- .into_iter() +- .chain(*barrier) +- .collect(); ++ let (pair, count) = barrier_pair(completed_barrier.take(), *barrier); ++ let globals = &pair[..count]; + let moves = held_moves.transitions(offset); + if !globals.is_empty() || start_stamp.is_some() || !moves.is_empty() { + started |= offset == 0; +@@ -678,7 +672,7 @@ impl CommandBufferShared { + end: None, + }; + let kept = &mut scheduler.kept; +- work.push(self.barrier_buffer(&mut state, kept, at, &globals, moves, stamps)?); ++ work.push(self.barrier_buffer(&mut state, kept, at, globals, moves, stamps)?); + } + work.extend(&state.recorded[start + offset].raws); + } +@@ -1429,3 +1423,15 @@ impl Stamps { + }) + } + } ++ ++/// `lead` then `barrier`, those of them there are, as the first `.1` of the array: the globals ++/// of a barrier between a batch's encoders, which takes no allocation. ++fn barrier_pair(lead: Option, barrier: Option) -> ([Barrier; 2], usize) { ++ let mut pair = [Barrier::default(); 2]; ++ let mut count = 0; ++ for b in lead.into_iter().chain(barrier) { ++ pair[count] = b; ++ count += 1; ++ } ++ (pair, count) ++} +diff --git a/crates/metal2vulkan-runtime/src/neutral.rs b/crates/metal2vulkan-runtime/src/neutral.rs +index c75c19f6..947f1008 100644 +--- a/crates/metal2vulkan-runtime/src/neutral.rs ++++ b/crates/metal2vulkan-runtime/src/neutral.rs +@@ -37,7 +37,7 @@ use metal2vulkan::bind::NeutralResource; + use metal2vulkan::device::FormatUsage; + use metal2vulkan::vk::Format; + use std::any::Any; +-use std::collections::HashMap; ++use super::key_hash::KeyMap; + use std::sync::{Arc, Mutex, Weak}; + + /// One neutral image, its view and its memory. +@@ -99,9 +99,9 @@ impl Filled for NeutralZeros { + /// A device's neutral images, texel buffers, zero buffers and samplers, made at first use. + #[derive(Default)] + pub(super) struct Neutrals { +- images: HashMap>, ++ images: KeyMap>, + /// By format and whether the view is a storage texel buffer's. +- texels: HashMap<(Format, bool), Arc>, ++ texels: KeyMap<(Format, bool), Arc>, + /// By their length in bytes, which the plan asks for (`zero_buffer_bytes`, fixed for the + /// device): the read-only zero buffers, and how many the device has made. + /// +@@ -113,12 +113,12 @@ pub(super) struct Neutrals { + /// allocate-and-zero a draw. Zeros of a length the device's own limits fix cannot have + /// changed since the device was made, so the device makes each length once and keeps it, as + /// it keeps its neutral images, texel buffers and samplers. +- zeros: HashMap>, ++ zeros: KeyMap>, + /// How many zero buffers the device has made, for the check that draws share one + /// (`tests::neutral_zeros_shared`). + zeros_made: u64, + /// By whether depth comparison is enabled. +- samplers: HashMap, ++ samplers: KeyMap, + } + + impl Neutrals { diff --git a/bridge/patches/0007-accept-two-plane-rgb-surfaces.patch b/bridge/patches/0007-accept-two-plane-rgb-surfaces.patch deleted file mode 100644 index b3977de..0000000 --- a/bridge/patches/0007-accept-two-plane-rgb-surfaces.patch +++ /dev/null @@ -1,25 +0,0 @@ -diff --git a/crates/metal2vulkan-runtime/src/ycbcr.rs b/crates/metal2vulkan-runtime/src/ycbcr.rs -index 368d62e..7e545d2 100644 ---- a/crates/metal2vulkan-runtime/src/ycbcr.rs -+++ b/crates/metal2vulkan-runtime/src/ycbcr.rs -@@ -151,8 +151,10 @@ use std::ops::Range; - /// range, and `xf20`, `xf22` and `xf44` the same, full range; `p420`, `p422` and `p444`, and - /// `pf20`, `pf22` and `pf44`, the same packed three to a 32-bit word. A format reads a surface - /// whose chroma plane holds its own: a 4:2:0 format a 4:2:2 or 4:4:4 surface's, a 4:2:2 format a --/// 4:4:4 one's, each at the surface's rows. --const SURFACES: [(&[u8; 4], u32, bool, YCbCrRange, ChromaSubsampling); 18] = { -+/// 4:4:4 one's, each at the surface's rows. `_RB4` is the render server's own two-plane RGB surface (green codes in plane 0, the -+/// red and blue pairs in plane 1, 8-bit 4:4:4), which widget snapshots are drawn into as `RGB8_444_2P` (542); the code formats -+/// read no range, so its range is only nominal. -+const SURFACES: [(&[u8; 4], u32, bool, YCbCrRange, ChromaSubsampling); 19] = { - use ChromaSubsampling::{Chroma420 as C420, Chroma422 as C422, Chroma444 as C444}; - [ - (b"420v", 8, false, YCbCrRange::Video, C420), -@@ -173,6 +175,7 @@ const SURFACES: [(&[u8; 4], u32, bool, YCbCrRange, ChromaSubsampling); 18] = { - (b"pf22", 10, true, YCbCrRange::Full, C422), - (b"p444", 10, true, YCbCrRange::Video, C444), - (b"pf44", 10, true, YCbCrRange::Full, C444), -+ (b"_RB4", 8, false, YCbCrRange::Full, C444), - ] - }; - /// What the planes' offsets and rows are multiples of: Metal's GPU reads a plane from its offset diff --git a/bridge/patches/0017-optional-shader-cache-directory.patch b/bridge/patches/0007-optional-shader-cache-directory.patch similarity index 95% rename from bridge/patches/0017-optional-shader-cache-directory.patch rename to bridge/patches/0007-optional-shader-cache-directory.patch index c6c2c17..f64e028 100644 --- a/bridge/patches/0017-optional-shader-cache-directory.patch +++ b/bridge/patches/0007-optional-shader-cache-directory.patch @@ -1,8 +1,8 @@ diff --git a/crates/metal2vulkan-runtime/src/shader_cache/store.rs b/crates/metal2vulkan-runtime/src/shader_cache/store.rs -index 98e82e1..1c7eac7 100644 +index 815dd990..38742f62 100644 --- a/crates/metal2vulkan-runtime/src/shader_cache/store.rs +++ b/crates/metal2vulkan-runtime/src/shader_cache/store.rs -@@ -68,6 +68,11 @@ impl Directory { +@@ -81,6 +81,11 @@ impl Directory { /// Open `root` for `build` with a cap of `capacity` bytes: create it and the build's /// directory (owner-only) and write and remove a probe file there. The error names the /// directory that failed. @@ -14,7 +14,7 @@ index 98e82e1..1c7eac7 100644 pub(crate) fn open(root: PathBuf, build: Hash256, capacity: u64) -> Result { let directory = Directory { root, -@@ -78,14 +83,20 @@ impl Directory { +@@ -91,14 +96,20 @@ impl Directory { let path = path.to_path_buf(); move |error| Failed { path, error } }; diff --git a/bridge/patches/0008-alias-padded-rows-of-written-textures.patch b/bridge/patches/0008-alias-padded-rows-of-written-textures.patch deleted file mode 100644 index 9cf361c..0000000 --- a/bridge/patches/0008-alias-padded-rows-of-written-textures.patch +++ /dev/null @@ -1,29 +0,0 @@ -diff --git a/crates/metal2vulkan-runtime/src/linear_plan.rs b/crates/metal2vulkan-runtime/src/linear_plan.rs -index a0a2cbc..fdb8233 100644 ---- a/crates/metal2vulkan-runtime/src/linear_plan.rs -+++ b/crates/metal2vulkan-runtime/src/linear_plan.rs -@@ -819,7 +819,7 @@ pub(super) fn holding( - return Ok(Holds::Texels); - }; - let reads = only_reads(usage); -- if reads && policy == PaddedReadsPolicy::Alias { -+ if policy == PaddedReadsPolicy::AliasWrites || (reads && policy == PaddedReadsPolicy::Alias) { - return Ok(Holds::Padding); - } - let writers = if usage.contains(TextureUsage::RENDER_TARGET) { -diff --git a/crates/metal2vulkan/src/device/policy.rs b/crates/metal2vulkan/src/device/policy.rs -index 29b0667..7b4fe50 100644 ---- a/crates/metal2vulkan/src/device/policy.rs -+++ b/crates/metal2vulkan/src/device/policy.rs -@@ -359,6 +359,11 @@ pub enum PaddedReadsPolicy { - /// the validation lane checks per driver (docs/FAST-PATHS.md, its known gaps). - #[default] - Alias, -+ /// [`PaddedReadsPolicy::Alias`] for every texture, a render target and a texture a shader -+ /// writes too: the driver's layout transitions and a render pass's load and store operations -+ /// may then write the bytes between the rows, which a program that keeps nothing there (a -+ /// window server's padded framebuffers) never reads. No copy in or out of a shadow image. -+ AliasWrites, - /// The runtime's shadow image of the texels, copied in from the buffer before each use: the - /// copies address the texels alone, so no byte between the rows is ever read or written - /// through an image. diff --git a/bridge/patches/0019-llvm-bitreverse.patch b/bridge/patches/0008-llvm-bitreverse.patch similarity index 93% rename from bridge/patches/0019-llvm-bitreverse.patch rename to bridge/patches/0008-llvm-bitreverse.patch index 7e3be70..7d43270 100644 --- a/bridge/patches/0019-llvm-bitreverse.patch +++ b/bridge/patches/0008-llvm-bitreverse.patch @@ -1,5 +1,5 @@ diff --git a/crates/metal2vulkan-air/src/intrinsics/llvm.rs b/crates/metal2vulkan-air/src/intrinsics/llvm.rs -index d784c6c..b6ca3f9 100644 +index d784c6c5..b6ca3f98 100644 --- a/crates/metal2vulkan-air/src/intrinsics/llvm.rs +++ b/crates/metal2vulkan-air/src/intrinsics/llvm.rs @@ -56,6 +56,8 @@ pub enum LlvmIntOp { @@ -29,10 +29,10 @@ index d784c6c..b6ca3f9 100644 _ => 2, } diff --git a/crates/metal2vulkan/src/lower/arith/mod.rs b/crates/metal2vulkan/src/lower/arith/mod.rs -index 54f371d..57d7eec 100644 +index feab97c7..1e5f8863 100644 --- a/crates/metal2vulkan/src/lower/arith/mod.rs +++ b/crates/metal2vulkan/src/lower/arith/mod.rs -@@ -2415,6 +2415,7 @@ fn llvm_math(op: &LlvmIntrinsic) -> Result { +@@ -1746,6 +1746,7 @@ fn llvm_math(op: &LlvmIntrinsic) -> Result { LlvmIntOp::Fshr => IntFn::Fshr, LlvmIntOp::Bswap => IntFn::Bswap, LlvmIntOp::Ctpop => IntFn::Popcount, diff --git a/bridge/patches/0009-keep-final-pass.patch b/bridge/patches/0009-keep-final-pass.patch deleted file mode 100644 index 8c1b295..0000000 --- a/bridge/patches/0009-keep-final-pass.patch +++ /dev/null @@ -1,140 +0,0 @@ -diff --git a/crates/metal2vulkan-runtime/src/device/mod.rs b/crates/metal2vulkan-runtime/src/device/mod.rs -index 00a6e55..8842010 100644 ---- a/crates/metal2vulkan-runtime/src/device/mod.rs -+++ b/crates/metal2vulkan-runtime/src/device/mod.rs -@@ -63,7 +63,7 @@ use super::memory::Memory; - use super::mipmap_scratch::MipmapScratch; - use super::owned_kernel::OwnedKernels; - use super::render_state::{depth_bias_command, DepthBiasCommand}; --use super::render_target::KeptCompatible; -+use super::render_target::{KeptCompatible, KeptFinal}; - use super::resource_ids::{ResourceIds, SamplerIdReads}; - use super::sample_planes::PlanesObjects; - use super::sampler::{SamplerCount, SamplerKey, SamplerShared}; -@@ -305,6 +305,8 @@ pub(super) struct DeviceShared { - pub(super) attachmentless: Mutex, - /// The compatible passes it keeps, by shape (`render_target::shared_compatible_pass`). - pub(super) compatible_passes: Mutex, -+ /// The final passes it keeps, by what they are made of (`render_target::kept_final_pass`). -+ pub(super) final_passes: Mutex, - /// The caller's images that move between layouts, and the events' timeline semaphores - /// (`interop.rs`). - pub(super) interop: Interop, -@@ -498,6 +500,7 @@ impl Drop for DeviceShared { - lock(&self.depth_stencil_resolves).destroy(self.vk()); - lock(&self.attachmentless).destroy(self.vk()); - lock(&self.compatible_passes).destroy(self.vk()); -+ lock(&self.final_passes).destroy(self.vk()); - // What released variants left (`pipeline/kept.rs`). - lock(&self.kept).destroy(self); - // The graphics pipeline libraries, after every pipeline they linked: each linked -@@ -1272,6 +1275,7 @@ impl Device { - depth_stencil_resolves: Mutex::new(DepthStencilResolveObjects::default()), - attachmentless: Mutex::default(), - compatible_passes: Mutex::default(), -+ final_passes: Mutex::default(), - interop: Interop::default(), - heap_keys: Default::default(), - rebinds: Default::default(), -diff --git a/crates/metal2vulkan-runtime/src/render_target.rs b/crates/metal2vulkan-runtime/src/render_target.rs -index 0310edd..e7f7391 100644 ---- a/crates/metal2vulkan-runtime/src/render_target.rs -+++ b/crates/metal2vulkan-runtime/src/render_target.rs -@@ -299,7 +299,7 @@ pub(super) fn later_steps(layout: &PassLayout) -> impl Iterator, -+ depth_stencil: Option, -+ resolves: Vec, -+ depth_stencil_resolve: Option, -+ resolving: bool, -+} -+ -+/// The final passes a device keeps, by what they are made of ([`kept_final_pass`]). -+#[derive(Default)] -+pub(super) struct KeptFinal(HashMap); -+ -+impl KeptFinal { -+ /// Destroy every kept pass; the device is idle, and is destroyed next. -+ pub(super) fn destroy(&self, raw: &ash::Device) { -+ for &pass in self.0.values() { -+ // SAFETY: the device is idle, so no pending command uses the pass, and no framebuffer -+ // made with it is left (each goes with the command buffer that began it). -+ unsafe { release::object(raw, pass) }; -+ } -+ } -+} -+ -+/// The device's final pass of `makings`, made now where it keeps none. A render pass is immutable, -+/// so one serves every instance made of the same: made and destroyed for every instance, as -+/// before, it cost a `vkCreateRenderPass2` (where Turnip lays out its attachments in GMEM for -+/// every tile layout) and a `vkDestroyRenderPass` each time a pass ended, 19 times a frame of -+/// iOS's compositor. Past [`KEPT_FINALS`] a pass is made for its instance alone, as before. -+fn kept_final_pass(device: &Device, makings: FinalMakings) -> Result { -+ let kept = &device.shared().final_passes; -+ if let Some(&raw) = lock(kept).0.get(&makings) { -+ return Ok(RenderPassObject::kept(device, raw)); -+ } -+ let pass = create( -+ device, -+ &makings.shape, -+ &makings.colors, -+ makings.depth_stencil, -+ &makings.resolves, -+ makings.depth_stencil_resolve, -+ makings.resolving, -+ )?; -+ let mut passes = lock(kept); -+ // Another encoder's may have come first: its pass serves, and this one goes unused. -+ if let Some(&raw) = passes.0.get(&makings) { -+ return Ok(RenderPassObject::kept(device, raw)); -+ } -+ if pass.owner == Owner::Holders && passes.0.len() < KEPT_FINALS { -+ let raw = pass.into_kept(); -+ passes.0.insert(makings, raw); -+ return Ok(RenderPassObject::kept(device, raw)); -+ } -+ Ok(pass) -+} -+ - /// The operations of color attachment `color` in an instance at `at`, the sample planes' - /// `anchor` or not, its `DontCare` `dont_care`'s, but the image a texture over a buffer keeps for - /// a pass that does not store it, which nothing reads after the pass (`kept_bytes.rs`). An diff --git a/bridge/patches/0021-patch-with-no-control-points.patch b/bridge/patches/0009-patch-with-no-control-points.patch similarity index 96% rename from bridge/patches/0021-patch-with-no-control-points.patch rename to bridge/patches/0009-patch-with-no-control-points.patch index d06b274..82d6a5a 100644 --- a/bridge/patches/0021-patch-with-no-control-points.patch +++ b/bridge/patches/0009-patch-with-no-control-points.patch @@ -1,5 +1,5 @@ diff --git a/crates/metal2vulkan/src/reflection/function.rs b/crates/metal2vulkan/src/reflection/function.rs -index e62075c..812f372 100644 +index cb1d1a91..c8ad1973 100644 --- a/crates/metal2vulkan/src/reflection/function.rs +++ b/crates/metal2vulkan/src/reflection/function.rs @@ -224,9 +224,12 @@ pub(crate) fn metallib_properties( diff --git a/bridge/patches/0024-core-image-kernel-libraries.patch b/bridge/patches/0010-core-image-kernel-libraries.patch similarity index 81% rename from bridge/patches/0024-core-image-kernel-libraries.patch rename to bridge/patches/0010-core-image-kernel-libraries.patch index 38377a6..0931719 100644 --- a/bridge/patches/0024-core-image-kernel-libraries.patch +++ b/bridge/patches/0010-core-image-kernel-libraries.patch @@ -1,6 +1,8 @@ +diff --git a/crates/metal2vulkan-air/src/container/mtlb.rs b/crates/metal2vulkan-air/src/container/mtlb.rs +index d4c8e2bd..f8add976 100644 --- a/crates/metal2vulkan-air/src/container/mtlb.rs +++ b/crates/metal2vulkan-air/src/container/mtlb.rs -@@ -41,6 +41,13 @@ +@@ -41,6 +41,13 @@ const SUPPORTED_MAJOR: [u16; 2] = [1, 2]; const FILE_TYPE_EXECUTABLE: u8 = 0; /// `file_type` of a dynamic library (`MTLLibraryTypeDynamic`). const FILE_TYPE_DYNAMIC: u8 = 2; @@ -14,7 +16,7 @@ /// A metallib's library type (`MTLLibraryType`), from its header's `file_type`. #[non_exhaustive] -@@ -248,7 +255,10 @@ +@@ -248,7 +255,10 @@ pub(super) fn parse(bytes: &[u8]) -> Result, Error> { 6, )); } @@ -26,21 +28,23 @@ return Err(InputError::new( InputErrorKind::Unsupported(Feature::LibraryKind(header.file_type)), Location::container(10), -@@ -317,6 +327,11 @@ - } +@@ -318,6 +328,11 @@ pub(super) fn parse(bytes: &[u8]) -> Result, Error> { if at != entries_end { return Err(container_error(ContainerError::BadOffset, 32)); -+ } + } + // A Core Image kernel library lists its kernels over one shared module; Metal names none of + // them, so neither does the library here. + if header.file_type == FILE_TYPE_CI_KERNELS { + parsed.retain(|p| p.function.stage != FunctionStage::Other(TYPE_CI_KERNEL)); - } ++ } let (extended, _) = tags::read_list(file, at, file.len())?; let mut linkage = linkage(file, &extended)?; + if header.file_type == FILE_TYPE_DYNAMIC { +diff --git a/crates/metal2vulkan-air/src/container/tests.rs b/crates/metal2vulkan-air/src/container/tests.rs +index 81e4e5af..b7c10b5f 100644 --- a/crates/metal2vulkan-air/src/container/tests.rs +++ b/crates/metal2vulkan-air/src/container/tests.rs -@@ -593,14 +593,25 @@ +@@ -593,14 +593,25 @@ fn unsupported_versions_and_library_kinds() { b.version = (1, 2); assert!(parse(&b.build()).is_ok()); let mut b = sample(); diff --git a/bridge/patches/0010-fewer-allocations-per-draw.patch b/bridge/patches/0010-fewer-allocations-per-draw.patch deleted file mode 100644 index 859e4fc..0000000 --- a/bridge/patches/0010-fewer-allocations-per-draw.patch +++ /dev/null @@ -1,559 +0,0 @@ -diff --git a/crates/metal2vulkan-runtime/src/render.rs b/crates/metal2vulkan-runtime/src/render.rs -index 119119d..3767b60 100644 ---- a/crates/metal2vulkan-runtime/src/render.rs -+++ b/crates/metal2vulkan-runtime/src/render.rs -@@ -296,7 +296,7 @@ use super::render_pass::{ - self, check_memoryless, AspectTarget, Attachment, DepthStencilTarget, PassLayout, - RenderPassDescriptor, RenderPassProblem, Target, HIDDEN_DEPTH_FORMAT, MAX_COLOR_ATTACHMENTS, - }; --use super::render_state::{self, DepthBiasRecord, EncoderState, MAX_VIEWPORTS}; -+use super::render_state::{self, DepthBiasRecord, EncoderState, PerViewport, MAX_VIEWPORTS}; - use super::render_state::{Compensation, DepthBiasEffect, WrittenDepthClamp}; - use super::render_target::{self, covers, PassShape, Position, RenderPassObject}; - use super::render_values::{LoadAction, ScissorRect, StoreAction, StoreActionOptions, Viewport}; -@@ -612,8 +612,8 @@ struct DrawSettings { - /// (`render_state.rs`). - #[derive(Clone)] - struct DrawState { -- viewports: Vec, -- scissors: Vec, -+ viewports: PerViewport, -+ scissors: PerViewport, - fixed: FixedState, - /// The front and back stencil references. - stencil_reference: [u32; 2], -@@ -1197,11 +1197,12 @@ impl LastRetained { - /// A variant, plan and specialization under which `Device::degrade_bound_values` was quiet: no - /// clause of a note it tested held. Which hold is a function of the three alone, so a draw of - /// the same three announces nothing there. The variant and the plan are held, so a pointer seen --/// again is the same object. -+/// again is the same object. The specialization is the plan's own where `specialization` is -+/// `None` (every draw but a mesh draw of its own key), so a draw keeps no copy of it. - struct QuietBoundValues { - variant: Arc, - plan: Arc, -- specialization: SpecializationKey, -+ specialization: Option, - } - - impl QuietBoundValues { -@@ -1214,7 +1215,11 @@ impl QuietBoundValues { - ) -> bool { - Arc::ptr_eq(&self.variant, variant) - && Arc::ptr_eq(&self.plan, plan) -- && self.specialization == *specialization -+ && self -+ .specialization -+ .as_ref() -+ .unwrap_or(&self.plan.specialization) -+ == specialization - } - } - -@@ -6498,7 +6503,7 @@ impl RenderCommandEncoder { - // offsetting bias folded into the compensation (`EncoderState::compensation`). - let depth = compensation.and_then(|c| c.depth); - let viewports = match depth { -- Some(_) => self.state.used_viewports(selects).to_vec(), -+ Some(_) => self.state.used_viewports(selects).iter().copied().collect(), - None => self - .state - .draw_viewports(selects, topology, profile, native)?, -@@ -6553,12 +6558,12 @@ impl RenderCommandEncoder { - let [min_depth, max_depth] = - depth.unwrap_or([viewport.min_depth, viewport.max_depth]); - DrawState { -- viewports: vec![vk::Viewport { -+ viewports: PerViewport::one(vk::Viewport { - min_depth, - max_depth, - ..viewport -- }], -- scissors: vec![scissor], -+ }), -+ scissors: PerViewport::one(scissor), - ..draw_state.clone() - } - }) -@@ -6620,7 +6625,7 @@ impl RenderCommandEncoder { - self.quiet_bound_values = Some(QuietBoundValues { - variant: Arc::clone(&variant), - plan: Arc::clone(&plan), -- specialization: specialization.clone(), -+ specialization: commands.specialization.clone(), - }); - } - device.degrade_cross_class(&plan.diagnostics, &formats); -diff --git a/crates/metal2vulkan-runtime/src/render/bound.rs b/crates/metal2vulkan-runtime/src/render/bound.rs -index 0060cfc..be326c0 100644 ---- a/crates/metal2vulkan-runtime/src/render/bound.rs -+++ b/crates/metal2vulkan-runtime/src/render/bound.rs -@@ -32,7 +32,7 @@ use ash::vk; - use super::{DrawState, IndexBind}; - use crate::binding::PushWrite; - use crate::pipeline::FixedState; --use crate::render_state::DepthBiasSetting; -+use crate::render_state::{DepthBiasSetting, PerViewport}; - - /// The state commands recorded into one secondary command buffer that still hold (the module - /// docs). -@@ -63,8 +63,8 @@ pub(super) struct Bound { - /// The values a draw sets its dynamic states to: the encoder's state at the draw and its input - /// assembly. - pub(super) struct Values { -- viewports: Vec, -- scissors: Vec, -+ viewports: PerViewport, -+ scissors: PerViewport, - fixed: FixedState, - stencil_reference: [u32; 2], - depth_bias: DepthBiasSetting, -@@ -226,8 +226,8 @@ impl Values { - fn of(now: Now<'_>) -> Values { - let s = now.state; - Values { -- viewports: s.viewports.clone(), -- scissors: s.scissors.clone(), -+ viewports: s.viewports, -+ scissors: s.scissors, - fixed: s.fixed, - stencil_reference: s.stencil_reference, - depth_bias: s.depth_bias.setting(), -@@ -302,14 +302,8 @@ impl Values { - let s = now.state; - let (ds, now_ds) = (&mut self.fixed.depth_stencil, &s.fixed.depth_stencil); - match state { -- vk::DynamicState::VIEWPORT => { -- self.viewports.clear(); -- self.viewports.extend_from_slice(&s.viewports); -- } -- vk::DynamicState::SCISSOR => { -- self.scissors.clear(); -- self.scissors.extend_from_slice(&s.scissors); -- } -+ vk::DynamicState::VIEWPORT => self.viewports = s.viewports, -+ vk::DynamicState::SCISSOR => self.scissors = s.scissors, - vk::DynamicState::DEPTH_BIAS => self.depth_bias = s.depth_bias.setting(), - vk::DynamicState::BLEND_CONSTANTS => self.blend_color = s.blend_color, - vk::DynamicState::STENCIL_COMPARE_MASK => { -diff --git a/crates/metal2vulkan-runtime/src/render_state.rs b/crates/metal2vulkan-runtime/src/render_state.rs -index f6f5ffd..e231b10 100644 ---- a/crates/metal2vulkan-runtime/src/render_state.rs -+++ b/crates/metal2vulkan-runtime/src/render_state.rs -@@ -113,6 +113,70 @@ use metal2vulkan::pipeline::{GraphicsState, ViewportCompensation}; - /// Metal's most viewports and scissor rectangles. - pub(super) const MAX_VIEWPORTS: usize = 16; - -+/// One value per viewport a draw uses, held in place: its viewports and scissors, which every draw -+/// makes, without allocating. A draw uses at most [`MAX_VIEWPORTS`] (the setters refuse more, -+/// `check_count`, and a draw uses the viewports set or the first). -+#[derive(Clone, Copy, Debug)] -+pub(super) struct PerViewport { -+ items: [T; MAX_VIEWPORTS], -+ len: u8, -+} -+ -+impl PerViewport { -+ /// `item` alone. -+ pub(super) fn one(item: T) -> PerViewport { -+ std::iter::once(item).collect() -+ } -+} -+ -+impl std::ops::Deref for PerViewport { -+ type Target = [T]; -+ -+ fn deref(&self) -> &[T] { -+ &self.items[..usize::from(self.len)] -+ } -+} -+ -+impl std::ops::DerefMut for PerViewport { -+ fn deref_mut(&mut self) -> &mut [T] { -+ &mut self.items[..usize::from(self.len)] -+ } -+} -+ -+impl FromIterator for PerViewport { -+ fn from_iter>(iter: I) -> PerViewport { -+ let mut items = [T::default(); MAX_VIEWPORTS]; -+ let mut len = 0; -+ for item in iter { -+ assert!(len < MAX_VIEWPORTS, "a draw of more than {MAX_VIEWPORTS} viewports"); -+ items[len] = item; -+ len += 1; -+ } -+ PerViewport { -+ items, -+ len: len as u8, -+ } -+ } -+} -+ -+impl<'a, T> IntoIterator for &'a PerViewport { -+ type Item = &'a T; -+ type IntoIter = std::slice::Iter<'a, T>; -+ -+ fn into_iter(self) -> Self::IntoIter { -+ self.iter() -+ } -+} -+ -+impl<'a, T> IntoIterator for &'a mut PerViewport { -+ type Item = &'a mut T; -+ type IntoIter = std::slice::IterMut<'a, T>; -+ -+ fn into_iter(self) -> Self::IntoIter { -+ self.iter_mut() -+ } -+} -+ - /// What a viewport, scissor rectangle or depth/stencil setter did wrong. - #[non_exhaustive] - #[derive(Clone, Copy, Debug, PartialEq, Eq)] -@@ -335,7 +399,7 @@ impl EncoderState { - topology: metal2vulkan::vk::PrimitiveTopology, - profile: &DeviceProfile, - native: bool, -- ) -> Result, Error> { -+ ) -> Result, Error> { - let used = self.used_viewports(selects); - let clamps = self.clip_mode == DepthClipMode::Clamp; - // In clamp mode a range past `[0, 1]` whose one clamp holds one depth stores that depth -@@ -766,7 +830,7 @@ impl EncoderState { - viewports: &[Viewport], - depth: Option<[f32; 2]>, - guard: Option<[f64; 2]>, -- ) -> Vec { -+ ) -> PerViewport { - viewports - .iter() - .map(|v| { -@@ -808,7 +872,7 @@ impl EncoderState { - /// its index, the one rectangle where the encoder holds one, and an empty one past two or - /// more (the module docs), each clipped to the pixels its viewport overlaps, Metal's implicit - /// scissor ([`viewport_pixels`]). -- pub(super) fn vk_scissors(&self, selects: bool) -> Vec { -+ pub(super) fn vk_scissors(&self, selects: bool) -> PerViewport { - self.each_vk_scissor(selects).collect() - } - -diff --git a/crates/metal2vulkan/src/bind/dispatch.rs b/crates/metal2vulkan/src/bind/dispatch.rs -index f8a62bb..3536491 100644 ---- a/crates/metal2vulkan/src/bind/dispatch.rs -+++ b/crates/metal2vulkan/src/bind/dispatch.rs -@@ -897,7 +897,8 @@ fn instantiate( - stride.to_le_bytes().to_vec() - } - field => slot_field(field, &bound.residuals, &t.resources, state) -- .ok_or_else(|| internal(format!("a compute push field {field:?}")))?, -+ .ok_or_else(|| internal(format!("a compute push field {field:?}")))? -+ .to_vec(), - }; - put(&mut push, push_start, &mut table, f.loc, &bytes)?; - } -diff --git a/crates/metal2vulkan/src/bind/graphics.rs b/crates/metal2vulkan/src/bind/graphics.rs -index a477fab..850f28a 100644 ---- a/crates/metal2vulkan/src/bind/graphics.rs -+++ b/crates/metal2vulkan/src/bind/graphics.rs -@@ -234,8 +234,8 @@ use std::collections::BTreeMap; - use crate::bind::heap::{planner, HeapPlanner}; - use crate::bind::instantiate::{ - bind_buffers, descriptor_range, put, slot_field, BufferExtent, BufferRange, DescriptorBytes, -- DescriptorRange, IndexBufferRef, IndirectRef, PushConstantWrite, PushHole, RangeError, -- RootTable, -+ DescriptorRange, FieldBytes, IndexBufferRef, IndirectRef, PushConstantWrite, PushHole, -+ RangeError, RootTable, - }; - use crate::bind::neutral::{zero_buffer_bytes, NeutralResource}; - use crate::bind::plan::compute::{internal, not_implemented, spec_key, tile_memory}; -@@ -3326,7 +3326,7 @@ fn instantiate( - } - ]; - for f in &st.fields { -- let bytes: Vec = match f.field { -+ let bytes: FieldBytes = match f.field { - PushField::RootTableAddress - | PushField::ExecutionStatusAddress - | PushField::CaptureAddress -@@ -3355,27 +3355,27 @@ fn instantiate( - let Some(shadow) = &t.shadow_stencil else { - return Err(internal("a stencil state without its shader-side stencil")); - }; -- shadow_stencil_words(shadow, state)? -+ shadow_stencil_words(shadow, state)?.into() - } - PushField::ShaderTestsState => { - if t.shader_tests.is_none() { - return Err(internal("a depth state without its in-shader depth test")); - } -- shader_tests_words(state) -+ shader_tests_words(state).into() - } - PushField::ShaderStencilState => { - if t.shader_tests.is_none() { - return Err(internal("a stencil state without its in-shader tests")); - } -- shader_stencil_words(state) -+ shader_stencil_words(state).into() - } - PushField::DispatchRegion => match mesh.as_ref().and_then(|m| m.region(st.stage)) { -- Some(region) => region.to_vec(), -+ Some(region) => FieldBytes::of(®ion), - None => return Err(internal("a dispatch region outside a mesh draw")), - }, -- PushField::ViewportCount => state.viewport_count.to_le_bytes().to_vec(), -+ PushField::ViewportCount => FieldBytes::of(&state.viewport_count.to_le_bytes()), - PushField::RenderTargetArrayLength => { -- state.render_target_array_length.to_le_bytes().to_vec() -+ FieldBytes::of(&state.render_target_array_length.to_le_bytes()) - } - // The identity where the state holds none: a plan states its variant, so - // instantiation under another shape asks for the other one first. -@@ -3392,11 +3392,12 @@ fn instantiate( - .flat_map(|v| v.to_le_bytes()) - .collect(), - // No viewport mirrored where the state holds none. -- PushField::ViewportMirrors => state -- .viewport_compensation -- .map_or(0, |c| c.mirrors) -- .to_le_bytes() -- .to_vec(), -+ PushField::ViewportMirrors => FieldBytes::of( -+ &state -+ .viewport_compensation -+ .map_or(0, |c| c.mirrors) -+ .to_le_bytes(), -+ ), - // No vertex's depth clamped where the state holds none. - PushField::VertexDepthClamp => state - .viewport_compensation -@@ -3411,11 +3412,11 @@ fn instantiate( - .iter() - .flat_map(|v| v.to_le_bytes()) - .collect(), -- PushField::StorageKeptChannels => t.storage_kept.to_le_bytes().to_vec(), -+ PushField::StorageKeptChannels => FieldBytes::of(&t.storage_kept.to_le_bytes()), - // The attachment's rule word, as link derived it from the pipeline's attachment. - PushField::ColorWriteRule(color) => { - match t.color_write_rules.iter().find(|(c, _)| *c == color) { -- Some((_, word)) => word.to_le_bytes().to_vec(), -+ Some((_, word)) => FieldBytes::of(&word.to_le_bytes()), - None => return Err(internal("a color write rule link gave no word")), - } - } -@@ -3437,16 +3438,12 @@ fn instantiate( - // The first amplification's; `amplifications` writes each before its draw. - PushField::AmplificationId - | PushField::AmplificationLayerOffset -- | PushField::AmplificationViewportOffset => { -- amplification_word(f.field, 0, &t.amplifications)? -- .to_le_bytes() -- .to_vec() -- } -+ | PushField::AmplificationViewportOffset => FieldBytes::of( -+ &lification_word(f.field, 0, &t.amplifications)?.to_le_bytes(), -+ ), - PushField::BaseVertexInstance => { - reads_base = true; -- let mut b = base[0].to_le_bytes().to_vec(); -- b.extend_from_slice(&base[1].to_le_bytes()); -- b -+ base.iter().flat_map(|b| b.to_le_bytes()).collect() - } - PushField::VertexBufferStride(layout) => { - let fixed = t.pulled_strides.iter().find(|(l, _)| *l == layout); -@@ -3471,7 +3468,7 @@ fn instantiate( - ))) - } - }; -- stride.to_le_bytes().to_vec() -+ FieldBytes::of(&stride.to_le_bytes()) - } - field => slot_field(field, &bound.residuals, &t.resources, state) - .ok_or_else(|| internal(format!("a render push field {field:?}")))?, -@@ -3961,21 +3958,22 @@ fn draw_uses( - }; - uses.push(control(UseTarget::ControlPointIndices, range)); - } -- uses.extend(match *draw { -+ // At most two, in place: a draw's own uses allocate nothing past `uses`. -+ let own: [Option; 2] = match *draw { - // A mesh draw's arguments are its pipeline's to decide (`instantiate`). - DrawCall::Primitives { .. } - | DrawCall::Patches { .. } - | DrawCall::MeshThreadgroups { .. } - | DrawCall::MeshThreads { .. } -- | DrawCall::MeshThreadgroupsIndirect { .. } => Vec::new(), -+ | DrawCall::MeshThreadgroupsIndirect { .. } => [None, None], - DrawCall::Indexed { - index_type, - index_buffer, - index_count, - index_offset, - .. -- } => vec![index(index_bytes(index_type).map(|size| { -- ByteRange { -+ } => [ -+ Some(index(index_bytes(index_type).map(|size| ByteRange { - offset: 0, - len: match overrun { - None => u64::from(index_count) * size, -@@ -3986,15 +3984,17 @@ fn draw_uses( - u64::from(index_count.saturating_sub(past_end)) * size - } - }, -- } -- }))], -+ }))), -+ None, -+ ], - DrawCall::Indirect { indexed: None, .. } | DrawCall::PatchesIndirect { .. } => { -- vec![indirect_use(16)] -+ [Some(indirect_use(16)), None] - } - DrawCall::Indirect { - indexed: Some(_), .. -- } => vec![index(None), indirect_use(20)], -- }); -+ } => [Some(index(None)), Some(indirect_use(20))], -+ }; -+ uses.extend(own.into_iter().flatten()); - uses - } - -diff --git a/crates/metal2vulkan/src/bind/instantiate.rs b/crates/metal2vulkan/src/bind/instantiate.rs -index 3eb2f84..62d9d3f 100644 ---- a/crates/metal2vulkan/src/bind/instantiate.rs -+++ b/crates/metal2vulkan/src/bind/instantiate.rs -@@ -611,6 +611,67 @@ pub(crate) fn bind_buffers( - Ok(out) - } - -+/// A push field's bytes as instantiation writes them: up to [`FieldBytes::INLINE`] bytes in place -+/// (every word and vector field, so a draw's fields allocate nothing), more in a vector (a capture -+/// layout, a dispatch region). -+pub(crate) enum FieldBytes { -+ Inline { -+ bytes: [u8; FieldBytes::INLINE], -+ len: u8, -+ }, -+ Heap(Vec), -+} -+ -+impl FieldBytes { -+ /// The most bytes held in place. -+ pub(crate) const INLINE: usize = 32; -+ -+ /// `bytes`, copied. -+ pub(crate) fn of(bytes: &[u8]) -> FieldBytes { -+ bytes.iter().copied().collect() -+ } -+} -+ -+impl std::ops::Deref for FieldBytes { -+ type Target = [u8]; -+ -+ fn deref(&self) -> &[u8] { -+ match self { -+ FieldBytes::Inline { bytes, len } => &bytes[..usize::from(*len)], -+ FieldBytes::Heap(bytes) => bytes, -+ } -+ } -+} -+ -+impl From> for FieldBytes { -+ fn from(bytes: Vec) -> FieldBytes { -+ FieldBytes::Heap(bytes) -+ } -+} -+ -+impl FromIterator for FieldBytes { -+ fn from_iter>(iter: I) -> FieldBytes { -+ let mut bytes = [0u8; FieldBytes::INLINE]; -+ let mut iter = iter.into_iter(); -+ for len in 0..=FieldBytes::INLINE { -+ let Some(byte) = iter.next() else { -+ return FieldBytes::Inline { -+ bytes, -+ len: len as u8, -+ }; -+ }; -+ if len == FieldBytes::INLINE { -+ let mut heap = bytes.to_vec(); -+ heap.push(byte); -+ heap.extend(iter); -+ return FieldBytes::Heap(heap); -+ } -+ bytes[len] = byte; -+ } -+ unreachable!("the loop returns by its last pass") -+ } -+} -+ - /// The bytes of a slot push field (`SlotBindOffset`: the residual, 0 unbound; `SlotGuestBase`: the - /// slot's `gpu_address + offset`, which is Metal's `nil`; `TextureLevels`: the level count the plan - /// bound; `InstanceCount`: the bound structure's instance count; `Specialized`: the value the plan -@@ -621,7 +682,7 @@ pub(crate) fn slot_field( - residuals: &[(SlotRef, u32)], - resources: &crate::bind::plan::ResourceTemplate, - state: &BindState, --) -> Option> { -+) -> Option { - match field { - // The bound structure's instance count, 0 for an unbound slot. - PushField::InstanceCount(slot) => { -@@ -630,38 +691,38 @@ pub(crate) fn slot_field( - .get(&slot.stage) - .and_then(|t| t.acceleration_structures.get(&slot.index)) - .map_or(0, |s| s.instance_count); -- Some(count.to_le_bytes().to_vec()) -+ Some(FieldBytes::of(&count.to_le_bytes())) - } - // The level count the plan bound. - PushField::TextureLevels(slot) => resources - .texture_levels - .iter() - .find(|(s, _)| *s == slot) -- .map(|(_, levels)| levels.to_le_bytes().to_vec()), -+ .map(|(_, levels)| FieldBytes::of(&levels.to_le_bytes())), - // The value the bound resources decide, as the plan recorded it. - PushField::Specialized(key) => resources - .specialized - .iter() - .find(|(k, _)| *k == key) -- .map(|(_, value)| value.to_le_bytes().to_vec()), -+ .map(|(_, value)| FieldBytes::of(&value.to_le_bytes())), - // Whether the texture the plan bound holds 16-bit float channels. - PushField::TextureHalfChannels(slot) => resources - .texture_half_channels - .iter() - .find(|(s, _)| *s == slot) -- .map(|(_, half)| half.to_le_bytes().to_vec()), -+ .map(|(_, half)| FieldBytes::of(&half.to_le_bytes())), - // The integer channel widths of the texture the plan bound. - PushField::TextureIntegerChannels(slot) => resources - .texture_integer_channels - .iter() - .find(|(s, _)| *s == slot) -- .map(|(_, word)| word.to_le_bytes().to_vec()), -+ .map(|(_, word)| FieldBytes::of(&word.to_le_bytes())), - PushField::SlotBindOffset(slot) => { - let r = residuals - .iter() - .find(|(s, _)| *s == slot) - .map_or(0, |r| r.1); -- Some(r.to_le_bytes().to_vec()) -+ Some(FieldBytes::of(&r.to_le_bytes())) - } - PushField::SlotGuestBase(slot) => { - let base = match state -@@ -676,7 +737,7 @@ pub(crate) fn slot_field( - }) => gpu_address.wrapping_add(offset), - _ => 0, - }; -- Some(base.to_le_bytes().to_vec()) -+ Some(FieldBytes::of(&base.to_le_bytes())) - } - _ => None, - } diff --git a/bridge/patches/0011-keep-draw-commands.patch b/bridge/patches/0011-keep-draw-commands.patch deleted file mode 100644 index c9b9b72..0000000 --- a/bridge/patches/0011-keep-draw-commands.patch +++ /dev/null @@ -1,346 +0,0 @@ -diff --git a/crates/metal2vulkan-runtime/src/render.rs b/crates/metal2vulkan-runtime/src/render.rs -index 3767b60..4400378 100644 ---- a/crates/metal2vulkan-runtime/src/render.rs -+++ b/crates/metal2vulkan-runtime/src/render.rs -@@ -325,8 +325,8 @@ pub use late_resolve::ResolveTarget; - use metal2vulkan::bind::BindingPlan; - use metal2vulkan::bind::VertexAmplification; - use metal2vulkan::bind::{ -- DrawCall, FragmentTests, HeapPlacement, ImageblockSize, IndexBufferRef, IndirectRef, -- MeshDispatch, MeshGroups, TessellationFactors, ViewSetup, WriteSource, -+ DrawCall, DrawCommands, FragmentTests, HeapPlacement, ImageblockSize, IndexBufferRef, -+ IndirectRef, MeshDispatch, MeshGroups, TessellationFactors, ViewSetup, WriteSource, - }; - use metal2vulkan::cache::SpecializationKey; - use metal2vulkan::device::{DriverFact, FormatUsage, VkFeature}; -@@ -1121,6 +1121,8 @@ pub struct RenderCommandEncoder { - /// The last draw's variant, plan and specialization under which the bound values' notices - /// were quiet (`Device::degrade_bound_values`), where it was. - quiet_bound_values: Option, -+ /// The last draw's commands, whose lists the next draw's fill again. -+ draw_commands: Option, - /// The views and sampler descriptions its draws' binds made or found (`binding::BindMemo`). - bind_memo: binding::BindMemo, - /// What its last draw retained of its own ([`LastRetained`]). -@@ -1391,6 +1393,7 @@ impl CommandBuffer { - live: Arc::default(), - log, - quiet_bound_values: None, -+ draw_commands: None, - bind_memo: binding::BindMemo::default(), - last_retained: LastRetained::default(), - }) -@@ -5961,8 +5964,8 @@ impl RenderCommandEncoder { - return Err(Error::NotImplemented(RuntimeGap::ImageblockMasters)); - } - } -- let commands = plan -- .instantiate_draw(&live.state, &draw) -+ let mut commands = self.draw_commands.take().unwrap_or_else(DrawCommands::empty); -+ plan.instantiate_draw_into(&live.state, &draw, &mut commands) - .map_err(Error::Bind)?; - self.reaches_unnamed |= !commands.root_tables.is_empty() || reads_handles(&plan); - // A mesh draw the binder launches no threadgroup of (as plain Metal draws nothing). -@@ -6674,7 +6677,7 @@ impl RenderCommandEncoder { - binding::bind(call, &device, &mut state, &plan, &command, &tables, memo)? - }; - // The draw's push bytes, filled in place. -- let push = binding::push_writes(commands.push_constants, &bound.holes)?; -+ let push = binding::push_writes(std::mem::take(&mut commands.push_constants), &bound.holes)?; - // A plan that draws one amplification at a time records the draw once per amplification, - // in order, each after that amplification's push writes. - let amplified = commands -@@ -7309,6 +7312,7 @@ impl RenderCommandEncoder { - if capture.is_some() { - self.capture_uses(uses); - } -+ self.draw_commands = Some(commands); - Ok(()) - } - -diff --git a/crates/metal2vulkan-runtime/src/render/executions.rs b/crates/metal2vulkan-runtime/src/render/executions.rs -index 458c571..e026402 100644 ---- a/crates/metal2vulkan-runtime/src/render/executions.rs -+++ b/crates/metal2vulkan-runtime/src/render/executions.rs -@@ -2094,6 +2094,7 @@ impl RenderExecution { - live: Arc::default(), - log: None, - quiet_bound_values: None, -+ draw_commands: None, - bind_memo: crate::binding::BindMemo::default(), - last_retained: Default::default(), - } -diff --git a/crates/metal2vulkan/src/bind/graphics.rs b/crates/metal2vulkan/src/bind/graphics.rs -index 850f28a..c79f0d4 100644 ---- a/crates/metal2vulkan/src/bind/graphics.rs -+++ b/crates/metal2vulkan/src/bind/graphics.rs -@@ -233,9 +233,9 @@ use std::collections::BTreeMap; - - use crate::bind::heap::{planner, HeapPlanner}; - use crate::bind::instantiate::{ -- bind_buffers, descriptor_range, put, slot_field, BufferExtent, BufferRange, DescriptorBytes, -- DescriptorRange, FieldBytes, IndexBufferRef, IndirectRef, PushConstantWrite, PushHole, -- RangeError, RootTable, -+ bind_buffers_in, descriptor_range, put, slot_field, BoundBuffers, BufferExtent, BufferRange, -+ DescriptorBytes, DescriptorRange, FieldBytes, IndexBufferRef, IndirectRef, PushConstantWrite, -+ PushHole, RangeError, RootTable, - }; - use crate::bind::neutral::{zero_buffer_bytes, NeutralResource}; - use crate::bind::plan::compute::{internal, not_implemented, spec_key, tile_memory}; -@@ -2209,6 +2209,21 @@ impl BindingPlan { - state: &BindState, - draw: &DrawCall, - ) -> Result { -+ let mut out = DrawCommands::empty(); -+ self.instantiate_draw_into(state, draw, &mut out)?; -+ Ok(out) -+ } -+ -+ /// [`instantiate_draw`](Self::instantiate_draw) into `out`, whose vectors this draw's commands -+ /// take (emptied first): a consumer that keeps one `DrawCommands` from draw to draw fills it -+ /// without allocating them again. On a refusal nothing `out` holds is a draw's to record: the -+ /// next call fills it again. -+ pub fn instantiate_draw_into( -+ &self, -+ state: &BindState, -+ draw: &DrawCall, -+ out: &mut DrawCommands, -+ ) -> Result<(), BindError> { - let t = self - .graphics - .as_ref() -@@ -2230,10 +2245,45 @@ impl BindingPlan { - }); - } - } -- instantiate(t, state, draw) -+ instantiate_into(t, state, draw, out) -+ } -+} -+ -+impl DrawCommands { -+ /// No draw's commands, every vector empty: what [`BindingPlan::instantiate_draw_into`] fills. -+ pub fn empty() -> DrawCommands { -+ DrawCommands { -+ push_constants: Vec::new(), -+ descriptor_bytes: Vec::new(), -+ buffer_offsets: Vec::new(), -+ draw: vk::DrawCommand::Draw { -+ vertex_count: 0, -+ instance_count: 0, -+ first_vertex: 0, -+ first_instance: 0, -+ }, -+ indirect_needs_arguments: false, -+ extents: Vec::new(), -+ push_holes: Vec::new(), -+ descriptor_ranges: Vec::new(), -+ root_tables: Vec::new(), -+ vertex_strides: Vec::new(), -+ uses: Vec::new(), -+ index_overrun: None, -+ index_bounds_pass: false, -+ amplifications: Vec::new(), -+ specialization: None, -+ mesh_dispatch: None, -+ } - } - } - -+/// `v`, emptied: its room kept for the draw that takes it. -+fn emptied(mut v: Vec) -> Vec { -+ v.clear(); -+ v -+} -+ - /// The Vulkan draw of a Metal draw, and the base vertex and first instance the - /// `BaseVertexInstance` field reports (Vulkan's `BaseVertex` and `BaseInstance`). A direct - /// indexed draw whose indices run past its index buffer ([`index_overrun`]) draws as `overrun` -@@ -3272,11 +3322,25 @@ fn capture_draw( - } - } - -+#[cfg(test)] - fn instantiate( - t: &GraphicsTemplate, - state: &BindState, - draw: &DrawCall, - ) -> Result { -+ let mut out = DrawCommands::empty(); -+ instantiate_into(t, state, draw, &mut out)?; -+ Ok(out) -+} -+ -+fn instantiate_into( -+ t: &GraphicsTemplate, -+ state: &BindState, -+ draw: &DrawCall, -+ out: &mut DrawCommands, -+) -> Result<(), BindError> { -+ // The vectors `out` held, which this draw's commands take, emptied. -+ let kept = std::mem::replace(out, DrawCommands::empty()); - let overrun = index_overrun(t.robust_index_fetch, draw); - let (command, base) = vk_draw(draw, overrun); - let capture = match t.capture { -@@ -3295,7 +3359,15 @@ fn instantiate( - } - }; - let command = mesh.as_ref().map_or(command, |m| m.command); -- let bound = bind_buffers(&t.buffers, state)?; -+ let bound = bind_buffers_in( -+ &t.buffers, -+ state, -+ BoundBuffers { -+ descriptor_ranges: kept.descriptor_ranges, -+ extents: kept.extents, -+ ..BoundBuffers::default() -+ }, -+ )?; - let mut descriptor_ranges = bound.descriptor_ranges; - descriptor_ranges.extend(crate::bind::instantiate::texel_ranges(&t.resources, state)?); - let tables = crate::bind::heap::table_bytes(&t.resources, state)?; -@@ -3309,10 +3381,10 @@ fn instantiate( - }; - let vertex = state.stages.get(&Stage::Vertex); - -- let mut push_constants = Vec::new(); -- let mut descriptor_bytes = Vec::new(); -- let mut push_holes = Vec::new(); -- let mut root_tables = Vec::new(); -+ let mut push_constants = emptied(kept.push_constants); -+ let mut descriptor_bytes = emptied(kept.descriptor_bytes); -+ let mut push_holes = emptied(kept.push_holes); -+ let mut root_tables = emptied(kept.root_tables); - let mut reads_base = false; - for st in &t.stages { - let (push_start, push_size) = st.push_range.unwrap_or((0, 0)); -@@ -3502,8 +3574,8 @@ fn instantiate( - - // Vertex buffers: offsets, per-bind strides, the address rules, and the bytes the draw's - // vertex and instance ranges fetch. -- let mut buffer_offsets = Vec::new(); -- let mut vertex_strides = Vec::new(); -+ let mut buffer_offsets = emptied(kept.buffer_offsets); -+ let mut vertex_strides = emptied(kept.vertex_strides); - for vb in &t.vertex_buffers { - match vertex.and_then(|v| v.buffers.get(&vb.slot.index)) { - Some(&BoundBuffer::Buffer { -@@ -3595,7 +3667,7 @@ fn instantiate( - Some(m) => (m.dispatch, Some(m.specialization)), - None => (None, None), - }; -- Ok(DrawCommands { -+ *out = DrawCommands { - push_constants, - descriptor_bytes, - buffer_offsets, -@@ -3610,10 +3682,11 @@ fn instantiate( - uses: { - // The indexed capture form reads its indices in the vertex stage, by address, and - // binds no index buffer. -- let mut uses = match capture.as_ref().and_then(|c| c.indices) { -- Some(indices) => vec![indices], -- None => draw_uses(draw, overrun, t.tessellation.as_ref()), -- }; -+ let mut uses = emptied(kept.uses); -+ match capture.as_ref().and_then(|c| c.indices) { -+ Some(indices) => uses.push(indices), -+ None => draw_uses_into(draw, overrun, t.tessellation.as_ref(), &mut uses), -+ } - // A mesh draw that reads its arguments reads 12 bytes; an emulated one's scan reads - // them in the compute stage. - if let vk::DrawCommand::DrawMeshTasksIndirect { .. } = command { -@@ -3637,7 +3710,8 @@ fn instantiate( - amplifications, - mesh_dispatch, - specialization, -- }) -+ }; -+ Ok(()) - } - - /// The `u32` of amplification push field `field` for amplification `a` of a plan drawing -@@ -3890,12 +3964,25 @@ fn indirect_use(len: u64) -> ResourceUse { - /// control-point index offset, and argument offset. A direct draw whose indices run past the buffer - /// (`overrun`) reads the bytes from the index offset to [`IndexBufferRef::fetch_end`] when it is - /// `Robust`, and only the indices it draws when it is `Clamped` (none, from offset 0, when it draws --/// none). -+/// none). [`draw_uses_into`] appends them to a list. -+#[cfg(test)] - fn draw_uses( - draw: &DrawCall, - overrun: Option, - factors: Option<&FactorTemplate>, - ) -> Vec { -+ let mut uses = Vec::new(); -+ draw_uses_into(draw, overrun, factors, &mut uses); -+ uses -+} -+ -+/// [`draw_uses`], appended to `uses`. -+fn draw_uses_into( -+ draw: &DrawCall, -+ overrun: Option, -+ factors: Option<&FactorTemplate>, -+ uses: &mut Vec, -+) { - let index = |range| ResourceUse { - resource: UseTarget::IndexBuffer, - access: AccessSet::READ, -@@ -3911,7 +3998,6 @@ fn draw_uses( - stages: vk::PipelineStageFlags2::TESSELLATION_CONTROL_SHADER, - range, - }; -- let mut uses = Vec::new(); - if let DrawCall::Patches { - patch_index: Some(_), - .. -@@ -3995,7 +4081,6 @@ fn draw_uses( - } => [Some(index(None)), Some(indirect_use(20))], - }; - uses.extend(own.into_iter().flatten()); -- uses - } - - /// Bytes of one index of `ty`, when it is a fixed-size index type. -diff --git a/crates/metal2vulkan/src/bind/instantiate.rs b/crates/metal2vulkan/src/bind/instantiate.rs -index 62d9d3f..e474e8c 100644 ---- a/crates/metal2vulkan/src/bind/instantiate.rs -+++ b/crates/metal2vulkan/src/bind/instantiate.rs -@@ -469,6 +469,7 @@ pub(crate) fn bound_buffer( - } - - /// What a draw or dispatch binds for the plan's buffer slots. -+#[derive(Default)] - pub(crate) struct BoundBuffers { - /// The range of every descriptor-form slot's buffer or inline bytes. - pub descriptor_ranges: Vec, -@@ -542,14 +543,22 @@ fn check_uniform_aliasing(buffers: &[PlannedBuffer], state: &BindState) -> Resul - pub(crate) fn bind_buffers( - buffers: &[PlannedBuffer], - state: &BindState, -+) -> Result { -+ bind_buffers_in(buffers, state, BoundBuffers::default()) -+} -+ -+/// [`bind_buffers`] into `out`'s vectors, emptied first: a draw that keeps them from the last -+/// (`BindingPlan::instantiate_draw_into`) fills them without allocating. -+pub(crate) fn bind_buffers_in( -+ buffers: &[PlannedBuffer], -+ state: &BindState, -+ mut out: BoundBuffers, - ) -> Result { - check_uniform_aliasing(buffers, state)?; -- let mut out = BoundBuffers { -- descriptor_ranges: Vec::new(), -- extents: Vec::new(), -- residuals: Vec::new(), -- statics: Vec::new(), -- }; -+ out.descriptor_ranges.clear(); -+ out.extents.clear(); -+ out.residuals.clear(); -+ out.statics.clear(); - for pb in buffers { - let bound = bound_buffer(state, pb.slot, pb.bound)?; - let (bind_offset, length) = match bound { diff --git a/bridge/patches/0015-two-plane-shadow-as-memory-lies.patch b/bridge/patches/0011-two-plane-shadow-as-memory-lies.patch similarity index 63% rename from bridge/patches/0015-two-plane-shadow-as-memory-lies.patch rename to bridge/patches/0011-two-plane-shadow-as-memory-lies.patch index b6e419f..8d5de3c 100644 --- a/bridge/patches/0015-two-plane-shadow-as-memory-lies.patch +++ b/bridge/patches/0011-two-plane-shadow-as-memory-lies.patch @@ -1,19 +1,5 @@ -diff --git a/crates/metal2vulkan-runtime/src/texture.rs b/crates/metal2vulkan-runtime/src/texture.rs -index 6e86ce4..11e9b7c 100644 ---- a/crates/metal2vulkan-runtime/src/texture.rs -+++ b/crates/metal2vulkan-runtime/src/texture.rs -@@ -3288,7 +3288,8 @@ impl Device { - /// its storage mode: a heap texture's in memory of its own where its bytes in the heap are no - /// layout of its texels (`texture/heap.rs`, `AliasRoute::Lossless`), as a heap's textures hold - /// what their bytes held; and the shadow of a texture over a buffer whose copy in writes every -- /// texel of every aspect before any command reads it (`storage_over_buffer`). -+ /// texel of every aspect before any command reads it (`storage_over_buffer`, and a two-plane -+ /// texture's over a surface's planes, `texture/surface_two_planes.rs`). - fn new_image_as_it_lies( - &self, - plan: ImagePlan, diff --git a/crates/metal2vulkan-runtime/src/texture/surface_two_planes.rs b/crates/metal2vulkan-runtime/src/texture/surface_two_planes.rs -index 44931f5..3042919 100644 +index 322cb108..1c69338c 100644 --- a/crates/metal2vulkan-runtime/src/texture/surface_two_planes.rs +++ b/crates/metal2vulkan-runtime/src/texture/surface_two_planes.rs @@ -58,6 +58,7 @@ use crate::degradation::Degradation; @@ -22,9 +8,9 @@ index 44931f5..3042919 100644 use crate::linear_plan::{BufferLayout, LINEAR_ALIGNMENT}; +use crate::resource::Identity; use crate::shadow::{ShadowBuffer, ShadowCopies, ShadowSource, ShadowTarget}; + use crate::shadow_merge::ScratchSpares; use crate::texture_descriptor::{texture_usage, TextureDescriptor, TextureLayout}; - use crate::tracker::UseRange; -@@ -206,7 +207,14 @@ impl Device { +@@ -208,7 +209,14 @@ impl Device { return Err(Error::NotImplemented(RuntimeGap::Textures)); } let hazard = texels.options().hazard_tracking_mode()?; diff --git a/bridge/patches/0012-keep-draw-lists.patch b/bridge/patches/0012-keep-draw-lists.patch deleted file mode 100644 index b5a60d8..0000000 --- a/bridge/patches/0012-keep-draw-lists.patch +++ /dev/null @@ -1,302 +0,0 @@ -diff --git a/crates/metal2vulkan-runtime/src/binding.rs b/crates/metal2vulkan-runtime/src/binding.rs -index 000794d..df2525f 100644 ---- a/crates/metal2vulkan-runtime/src/binding.rs -+++ b/crates/metal2vulkan-runtime/src/binding.rs -@@ -1457,7 +1457,24 @@ pub(super) fn command_uses( - index: Option<(&Buffer, u64)>, - indirect: Option<(&Buffer, u64)>, - ) -> Result { -- let mut uses = Vec::new(); -+ command_uses_in(Vec::new(), call, plan, command, tables, index, indirect) -+} -+ -+/// [`command_uses`], its uses listed in `room` (emptied first): a list an earlier command's -+/// uses were in ([`CommandUses::into_room`]). -+pub(super) fn command_uses_in( -+ mut room: Vec>, -+ call: &'static str, -+ plan: &BindingPlan, -+ command: &Command<'_>, -+ tables: &Tables<'_>, -+ index: Option<(&Buffer, u64)>, -+ indirect: Option<(&Buffer, u64)>, -+) -> Result { -+ // At most one use for each resource use of the plan and the command. -+ room.clear(); -+ room.reserve(plan.uses.len() + command.uses.len()); -+ let mut uses = room; - // The keys of the buffers made resident the command reaches through the resident set, by - // address; every other use names its resource (a descriptor, a root table's static, a view). - // Those it names are gathered once it reaches one so: until then each use names its key. -@@ -1625,6 +1642,13 @@ impl CommandUses { - pub(super) fn resident(&self) -> &BTreeSet { - &self.resident - } -+ -+ /// The list the uses were in, emptied, for a later command's ([`command_uses_in`]). -+ pub(super) fn into_room(self) -> Vec> { -+ let mut uses = self.uses; -+ uses.clear(); -+ uses -+ } - } - - impl Deref for CommandUses { -diff --git a/crates/metal2vulkan-runtime/src/render.rs b/crates/metal2vulkan-runtime/src/render.rs -index 4400378..110c428 100644 ---- a/crates/metal2vulkan-runtime/src/render.rs -+++ b/crates/metal2vulkan-runtime/src/render.rs -@@ -1123,6 +1123,11 @@ pub struct RenderCommandEncoder { - quiet_bound_values: Option, - /// The last draw's commands, whose lists the next draw's fill again. - draw_commands: Option, -+ /// The list the last draw's uses were in, emptied, for the next draw's -+ /// (`binding::command_uses_in`). -+ uses_room: Vec>, -+ /// The list the last draw's argument samplers were in, emptied (`Device::argument_samplers_into`). -+ argument_samplers_room: Vec, - /// The views and sampler descriptions its draws' binds made or found (`binding::BindMemo`). - bind_memo: binding::BindMemo, - /// What its last draw retained of its own ([`LastRetained`]). -@@ -1394,6 +1399,8 @@ impl CommandBuffer { - log, - quiet_bound_values: None, - draw_commands: None, -+ uses_room: Vec::new(), -+ argument_samplers_room: Vec::new(), - bind_memo: binding::BindMemo::default(), - last_retained: LastRetained::default(), - }) -@@ -5845,7 +5852,10 @@ impl RenderCommandEncoder { - let max = pipeline.base().graphics.max_vertex_amplification_count; - // The device's sampler states, less those at IDs no set-1 sampler array of the variant - // holds (`heap_sets::fit_samplers`). -- let mut argument_samplers = self.core.device().argument_samplers(); -+ let mut argument_samplers = std::mem::take(&mut self.argument_samplers_room); -+ self.core -+ .device() -+ .argument_samplers_into(&mut argument_samplers); - let function_tables = pipeline.function_tables(); - let mut unlisted = None; - let reads_tile_memory = pipeline.reads_tile_memory(); -@@ -6402,7 +6412,8 @@ impl RenderCommandEncoder { - mesh: mesh_work.as_ref().map(mesh::MeshWork::regions), - }; - let (index, indirect) = (buffers.index.as_ref(), buffers.indirect.as_ref()); -- let mut uses = binding::command_uses( -+ let mut uses = binding::command_uses_in( -+ std::mem::take(&mut self.uses_room), - call, - &plan, - &command, -@@ -7311,8 +7322,13 @@ impl RenderCommandEncoder { - } - if capture.is_some() { - self.capture_uses(uses); -+ } else { -+ self.uses_room = uses.into_room(); - } - self.draw_commands = Some(commands); -+ // The states go; the list stays for the next draw's. -+ argument_samplers.clear(); -+ self.argument_samplers_room = argument_samplers; - Ok(()) - } - -diff --git a/crates/metal2vulkan-runtime/src/render/executions.rs b/crates/metal2vulkan-runtime/src/render/executions.rs -index e026402..768c128 100644 ---- a/crates/metal2vulkan-runtime/src/render/executions.rs -+++ b/crates/metal2vulkan-runtime/src/render/executions.rs -@@ -2095,6 +2095,8 @@ impl RenderExecution { - log: None, - quiet_bound_values: None, - draw_commands: None, -+ uses_room: Vec::new(), -+ argument_samplers_room: Vec::new(), - bind_memo: crate::binding::BindMemo::default(), - last_retained: Default::default(), - } -diff --git a/crates/metal2vulkan-runtime/src/resource_ids.rs b/crates/metal2vulkan-runtime/src/resource_ids.rs -index b3dc480..d5881f3 100644 ---- a/crates/metal2vulkan-runtime/src/resource_ids.rs -+++ b/crates/metal2vulkan-runtime/src/resource_ids.rs -@@ -464,6 +464,11 @@ impl ResourceIds { - - /// The live holders of sampler IDs, in ID order: the states a sampler table lists. - pub(super) fn samplers(&self) -> Vec> { -+ self.live_samplers().collect() -+ } -+ -+ /// [`samplers`](Self::samplers), one at a time. -+ pub(super) fn live_samplers(&self) -> impl Iterator> + '_ { - self.samplers - .slots - .values() -@@ -471,7 +476,6 @@ impl ResourceIds { - Holder::Sampler(state) => state.upgrade(), - _ => None, - }) -- .collect() - } - } - -diff --git a/crates/metal2vulkan-runtime/src/sampler.rs b/crates/metal2vulkan-runtime/src/sampler.rs -index 2498ec2..9116a99 100644 ---- a/crates/metal2vulkan-runtime/src/sampler.rs -+++ b/crates/metal2vulkan-runtime/src/sampler.rs -@@ -470,6 +470,13 @@ impl Device { - let states = lock(&self.shared().resource_ids).samplers(); - states.into_iter().map(SamplerState).collect() - } -+ -+ /// [`argument_samplers`](Self::argument_samplers), listed in `out` (emptied first). -+ pub(super) fn argument_samplers_into(&self, out: &mut Vec) { -+ out.clear(); -+ let ids = lock(&self.shared().resource_ids); -+ out.extend(ids.live_samplers().map(SamplerState)); -+ } - } - - impl SamplerState { -diff --git a/crates/metal2vulkan-runtime/src/tracker.rs b/crates/metal2vulkan-runtime/src/tracker.rs -index 0b532a0..58ccac6 100644 ---- a/crates/metal2vulkan-runtime/src/tracker.rs -+++ b/crates/metal2vulkan-runtime/src/tracker.rs -@@ -957,10 +957,54 @@ pub(super) struct EncoderUses { - /// The uses of one part of an encoder. - struct Part { - /// The tracked uses so far, one per range, at most [`ENTRIES_PER_KEY`] per resource. -- tracked: HashMap>>, -+ tracked: HashMap>, - stages: vk::PipelineStageFlags2, - } - -+/// One resource's tracked uses in a part, in the order they were first recorded: the first held -+/// in place, as a part mostly uses a resource over one range. -+enum KeyUses { -+ One(Use), -+ Many(Vec>), -+} -+ -+impl KeyUses { -+ fn as_slice(&self) -> &[Use] { -+ match self { -+ KeyUses::One(used) => std::slice::from_ref(used), -+ KeyUses::Many(uses) => uses, -+ } -+ } -+ -+ fn as_mut_slice(&mut self) -> &mut [Use] { -+ match self { -+ KeyUses::One(used) => std::slice::from_mut(used), -+ KeyUses::Many(uses) => uses, -+ } -+ } -+ -+ /// Add `used` after the others. -+ fn push(&mut self, used: Use) { -+ match self { -+ KeyUses::Many(uses) => uses.push(used), -+ KeyUses::One(_) => { -+ let KeyUses::One(first) = std::mem::replace(self, KeyUses::Many(Vec::new())) else { -+ unreachable!("matched above") -+ }; -+ *self = KeyUses::Many(vec![first, used]); -+ } -+ } -+ } -+ -+ /// The uses, appended to `out` in order. -+ fn append_to(self, out: &mut Vec>) { -+ match self { -+ KeyUses::One(used) => out.push(used), -+ KeyUses::Many(uses) => out.extend(uses), -+ } -+ } -+} -+ - impl Part { - fn new() -> Part { - Part { -@@ -1025,16 +1069,28 @@ impl EncoderUses { - .iter() - .filter(|used| used.tracked && !used.is_nothing()) - { -- let ranges = part.tracked.entry(used.key).or_default(); -- match ranges.iter_mut().find(|known| known.range == used.range) { -+ let ranges = match part.tracked.entry(used.key) { -+ std::collections::hash_map::Entry::Vacant(vacant) => { -+ vacant.insert(KeyUses::One(used.clone())); -+ continue; -+ } -+ std::collections::hash_map::Entry::Occupied(occupied) => occupied.into_mut(), -+ }; -+ match ranges -+ .as_mut_slice() -+ .iter_mut() -+ .find(|known| known.range == used.range) -+ { - Some(known) => { - known.read = known.read.union(used.read); - known.write = known.write.union(used.write); - } - None => ranges.push(used.clone()), - } -- if ranges.len() > ENTRIES_PER_KEY { -- coalesce_uses(ranges); -+ if let KeyUses::Many(ranges) = ranges { -+ if ranges.len() > ENTRIES_PER_KEY { -+ coalesce_uses(ranges); -+ } - } - } - } -@@ -1050,6 +1106,7 @@ impl EncoderUses { - self.parts.iter().any(|part| { - part.tracked.get(&read.key).is_some_and(|known| { - known -+ .as_slice() - .iter() - .any(|k| !k.write.is_empty() && k.range.overlaps(&read.range)) - }) -@@ -1068,7 +1125,9 @@ impl EncoderUses { - self.parts.iter().any(|part| { - part.tracked - .get(&write.key) -- .is_some_and(|known| known.iter().any(|k| k.range.overlaps(&write.range))) -+ .is_some_and(|known| { -+ known.as_slice().iter().any(|k| k.range.overlaps(&write.range)) -+ }) - }) - }) - } -@@ -1077,7 +1136,7 @@ impl EncoderUses { - pub(super) fn tracked_in(&self, stages: vk::PipelineStageFlags2) -> Vec> { - self.parts - .iter() -- .flat_map(|part| part.tracked.values().flatten()) -+ .flat_map(|part| part.tracked.values().flat_map(KeyUses::as_slice)) - .filter(|known| (known.read.stages | known.write.stages).intersects(stages)) - .cloned() - .collect() -@@ -1090,7 +1149,7 @@ impl EncoderUses { - self.parts.iter().any(|part| { - part.tracked - .get(&used.key) -- .is_some_and(|known| known.iter().any(|k| touches(k, used, stages))) -+ .is_some_and(|known| known.as_slice().iter().any(|k| touches(k, used, stages))) - }) - }) - } -@@ -1149,7 +1208,10 @@ impl EncoderUses { - let mut summaries: Vec> = parts - .into_iter() - .map(|part| { -- let mut uses: Vec> = part.tracked.into_values().flatten().collect(); -+ let mut uses: Vec> = Vec::with_capacity(part.tracked.len()); -+ for known in part.tracked.into_values() { -+ known.append_to(&mut uses); -+ } - uses.sort_by_key(|used| used.key); - Summary { - uses, diff --git a/bridge/patches/0018-srgb-extended-range-blend-rounded-model.patch b/bridge/patches/0012-srgb-extended-range-blend-rounded-model.patch similarity index 72% rename from bridge/patches/0018-srgb-extended-range-blend-rounded-model.patch rename to bridge/patches/0012-srgb-extended-range-blend-rounded-model.patch index 2fd3afc..5517c29 100644 --- a/bridge/patches/0018-srgb-extended-range-blend-rounded-model.patch +++ b/bridge/patches/0012-srgb-extended-range-blend-rounded-model.patch @@ -1,28 +1,28 @@ diff --git a/crates/metal2vulkan/src/lower/arith/extended_range/blend.rs b/crates/metal2vulkan/src/lower/arith/extended_range/blend.rs -index 0b004f2..ead0ad8 100644 +index 33c67576..55c119e6 100644 --- a/crates/metal2vulkan/src/lower/arith/extended_range/blend.rs +++ b/crates/metal2vulkan/src/lower/arith/extended_range/blend.rs -@@ -765,10 +765,16 @@ fn planned_for( - let narrow = narrow(eq); +@@ -990,10 +990,16 @@ fn planned_for( // The rounded model holds for any blend into an `_sRGB` form, a mask of one color channel, - // and the color channels' blend color difference. + // and the color channels' blend color difference; the fast models for `BGRA10_XR_sRGB`'s + // narrow blends alone. + // An `_sRGB` form's blend the fast model does not hold (any but a narrow, unmasked one) takes -+ // the rounded model, Metal's own without fast math, measured for every `_sRGB` form (EV -+ // XRB-xrts-m4) and apart from its fast-math result only in roundings, rather than no pipeline: -+ // NanoUniverse's starfield (the Astronomy wallpaper, `BGRA10_XR_sRGB`) had none. -+ let math = if math == BlendMath::Fast && srgb && (!narrow || masked) { ++ // the rounded model, Metal's own without fast math, measured for every `_sRGB` form and apart ++ // from its fast-math result only in roundings, rather than no pipeline: NanoUniverse's ++ // starfield (the Astronomy wallpaper, `BGRA10_XR_sRGB`) had none. ++ let math = if math == BlendMath::Fast && narrow_only && (!narrow || masked) { + BlendMath::Rounded + } else { + math + }; let fast = math == BlendMath::Fast; -- if fast && srgb && (!narrow || masked) { +- if fast && narrow_only && (!narrow || masked) { - return Err(refused(ExtendedRangeBlend::SrgbForm)); - } let opaque_ = !has_alpha; let color = match colors { 0 => None, -@@ -910,6 +916,13 @@ impl Blend { +@@ -1518,6 +1524,13 @@ impl Blend { /// Metal's model's: a `half4` one; a `float4` one under a blend whose color lanes take one term /// alone, unmasked, rounded to `half` first; another is the gap it is. pub fn takes(&self, output: Option) -> Result<(), Error> { diff --git a/bridge/patches/0020-growing-memory-blocks.patch b/bridge/patches/0013-growing-memory-blocks.patch similarity index 72% rename from bridge/patches/0020-growing-memory-blocks.patch rename to bridge/patches/0013-growing-memory-blocks.patch index 55a2210..0904196 100644 --- a/bridge/patches/0020-growing-memory-blocks.patch +++ b/bridge/patches/0013-growing-memory-blocks.patch @@ -1,10 +1,10 @@ diff --git a/crates/metal2vulkan-runtime/src/memory.rs b/crates/metal2vulkan-runtime/src/memory.rs -index de3df1e..a09c1ee 100644 +index 93b2ca98..8200c10d 100644 --- a/crates/metal2vulkan-runtime/src/memory.rs +++ b/crates/metal2vulkan-runtime/src/memory.rs -@@ -51,6 +51,13 @@ use std::sync::Mutex; - /// The size of a pooled block, at most the device's `maxMemoryAllocationSize`. - pub(super) const BLOCK_BYTES: u64 = 64 << 20; +@@ -83,6 +83,13 @@ pub(super) const KEPT_EMPTY_FOR: Duration = Duration::from_secs(1); + /// makes and frees blocks. + const LOGGED_PLACEMENT_BYTES: u64 = 4 << 20; +/// The first pooled block of a pool: later ones grow with the pool ([`Memory::next_block_bytes`]), +/// doubling it up to [`BLOCK_BYTES`]. Where device memory is the system's own and every block is @@ -14,9 +14,9 @@ index de3df1e..a09c1ee 100644 +pub(super) const FIRST_BLOCK_BYTES: u64 = 4 << 20; + /// What memory a resource needs. - #[derive(Clone, Copy, Debug, PartialEq, Eq)] + #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub(super) enum MemoryClass { -@@ -328,6 +335,14 @@ impl Pool { +@@ -420,6 +427,14 @@ impl Pool { } } @@ -29,9 +29,9 @@ index de3df1e..a09c1ee 100644 + } + /// Whether a pooled block has room for `size` bytes at the largest alignment asked of the - /// pool so far. - pub(super) fn has_room(&self, size: u64) -> bool { -@@ -821,6 +836,17 @@ impl Memory { + /// pool so far: whether [`Pool::room`] is that long. + /// +@@ -1250,6 +1265,17 @@ impl Memory { .add(size, dedicated, block, used) } @@ -49,7 +49,7 @@ index de3df1e..a09c1ee 100644 /// What making a block takes, copied out so that the block is made with the memory's lock /// released ([`place`], [`keep_room`]). fn maker(&self) -> BlockMaker { -@@ -1374,10 +1400,10 @@ fn place( +@@ -2071,10 +2097,13 @@ fn place( return Err(Error::OutOfMemory(MemoryKind::Device)); } let dedicated = fit == Fit::Apart || size > maker.block_bytes / 2; @@ -57,22 +57,29 @@ index de3df1e..a09c1ee 100644 let mut failure = Error::OutOfMemory(MemoryKind::Device); for type_index in candidates { let pool = (holds, type_index); -+ let block_size = if dedicated { size } else { lock(memory).next_block_bytes(pool, size) }; ++ let block_size = match dedicated { ++ true => size, ++ false => lock(memory).next_block_bytes(pool, size), ++ }; if fit == Fit::Pooled { if let Some(placement) = - lock(memory).place_pooled(device, pool, size, align, addressed)? -@@ -1411,27 +1437,26 @@ pub(super) fn keep_room(memory: &Mutex, device: &ash::Device, counts: &C + lock(memory).place_pooled(device, pool, (size, align), addressed, class)? +@@ -2117,19 +2146,25 @@ pub(super) fn keep_room(memory: &Mutex, device: &ash::Device, counts: &C let Ok(kept) = memory.kept(device) else { return; }; - let room = memory.block_bytes / 2; - let lacking: Vec<(Holds, u32)> = (kept.into_iter()) -- .filter(|&(holds, type_index)| !memory.pool_ref(holds, type_index).has_room(room)) +- .filter(|&(holds, type_index)| !memory.pool(holds, type_index).has_room(room)) + // Room for half of the pool's next block: a growing pool keeps room for what its blocks + // have held, rather than half of the largest block from its first encoding on. -+ let lacking: Vec<((Holds, u32), u64)> = (kept.into_iter()) ++ let sized: Vec<((Holds, u32), u64)> = (kept.into_iter()) + .map(|pool| (pool, memory.next_block_bytes(pool, 1))) -+ .filter(|&((holds, type_index), bytes)| !memory.pool_ref(holds, type_index).has_room(bytes / 2)) ++ .collect(); ++ let lacking: Vec<((Holds, u32), u64)> = (sized.into_iter()) ++ .filter(|&((holds, type_index), bytes)| { ++ !memory.pool(holds, type_index).has_room(bytes / 2) ++ }) .collect(); (lacking, memory.maker()) }; @@ -84,13 +91,12 @@ index de3df1e..a09c1ee 100644 continue; }; let mut memory = lock(memory); -- if memory -- .pool_ref(pool.0, pool.1) -- .has_room(maker.block_bytes / 2) -- { -+ if memory.pool_ref(pool.0, pool.1).has_room(bytes / 2) { +- if memory.pool(pool.0, pool.1).has_room(maker.block_bytes / 2) { ++ if memory.pool(pool.0, pool.1).has_room(bytes / 2) { + let what = "made a pooled block and freed it at once, its pool having room"; + memory.say_block(what, &block); drop(memory); - // SAFETY: nothing is placed in the block, which no command reads. +@@ -2137,6 +2172,6 @@ pub(super) fn keep_room(memory: &Mutex, device: &ash::Device, counts: &C unsafe { block.release(device) }; continue; } @@ -99,7 +105,7 @@ index de3df1e..a09c1ee 100644 } } diff --git a/crates/metal2vulkan-runtime/src/range.rs b/crates/metal2vulkan-runtime/src/range.rs -index 5c32bef..54855d5 100644 +index 1512ab0e..1fcd2f2e 100644 --- a/crates/metal2vulkan-runtime/src/range.rs +++ b/crates/metal2vulkan-runtime/src/range.rs @@ -111,6 +111,11 @@ impl RangeAllocator { diff --git a/bridge/patches/0013-skip-unchanged-rebinds.patch b/bridge/patches/0013-skip-unchanged-rebinds.patch deleted file mode 100644 index a241f73..0000000 --- a/bridge/patches/0013-skip-unchanged-rebinds.patch +++ /dev/null @@ -1,390 +0,0 @@ -diff --git a/crates/metal2vulkan-runtime/README.md b/crates/metal2vulkan-runtime/README.md -index c6dc9f6..f756943 100644 ---- a/crates/metal2vulkan-runtime/README.md -+++ b/crates/metal2vulkan-runtime/README.md -@@ -139,6 +139,7 @@ Vulkan-side choices Metal has no counterpart for: - | `announce_each` | Degradation kinds to announce at every occurrence instead of once. | - | `metal_level` | The macOS release whose Metal the device answers as (the host's by default). | - | `record_executions` | Keep, per command buffer, which pipeline each dispatch and draw ran (`CommandBuffer::executions`). Off by default. | -+| `refuse_rebinds` | Refuse every `rebind_bytes_no_copy` (`RebindProblem::Refused`), so the encoders log no calls to encode again while no-copy buffers live. Off by default. | - - While a handle lives, equal options give the same device for a physical device, as Metal has one - `MTLDevice` per GPU. Nothing is read from the environment but the platform's default cache -diff --git a/crates/metal2vulkan-runtime/src/blit.rs b/crates/metal2vulkan-runtime/src/blit.rs -index 2af641b..9db0e67 100644 ---- a/crates/metal2vulkan-runtime/src/blit.rs -+++ b/crates/metal2vulkan-runtime/src/blit.rs -@@ -160,7 +160,7 @@ impl CommandBuffer { - core: EncoderCore::open(self, "blit_command_encoder", true)?, - pattern: None, - shadows: Vec::new(), -- log: self.device().shared().rebinds.any().then(CallLog::new), -+ log: self.device().shared().logs_calls().then(CallLog::new), - }) - } - } -diff --git a/crates/metal2vulkan-runtime/src/buffer_descriptor.rs b/crates/metal2vulkan-runtime/src/buffer_descriptor.rs -index 902a821..5d07264 100644 ---- a/crates/metal2vulkan-runtime/src/buffer_descriptor.rs -+++ b/crates/metal2vulkan-runtime/src/buffer_descriptor.rs -@@ -539,6 +539,10 @@ impl Device { - if self.is_lost() { - return Err(Error::DeviceLost); - } -+ // Its encoders logged nothing to encode again (`DeviceOptions::refuse_rebinds`). -+ if self.shared().refuse_rebinds { -+ return Err(invalid(call, Problem::Rebind(RebindProblem::Refused))); -+ } - let key = buffer.identity().key; - let encoded = || invalid(call, Problem::Rebind(RebindProblem::EncodedUses)); - if self.rebind_pending(key) { -diff --git a/crates/metal2vulkan-runtime/src/compute.rs b/crates/metal2vulkan-runtime/src/compute.rs -index 0863fd5..92894bd 100644 ---- a/crates/metal2vulkan-runtime/src/compute.rs -+++ b/crates/metal2vulkan-runtime/src/compute.rs -@@ -226,7 +226,7 @@ impl CommandBuffer { - indirect_stage_in: None, - residency: self.inherited_residency(false), - captures: Captures::default(), -- log: self.device().shared().rebinds.any().then(CallLog::new), -+ log: self.device().shared().logs_calls().then(CallLog::new), - dispatches: 0, - }) - } -diff --git a/crates/metal2vulkan-runtime/src/device/adopt.rs b/crates/metal2vulkan-runtime/src/device/adopt.rs -index 73f84ba..4262d65 100644 ---- a/crates/metal2vulkan-runtime/src/device/adopt.rs -+++ b/crates/metal2vulkan-runtime/src/device/adopt.rs -@@ -266,6 +266,8 @@ impl Device { - gpu_times_from_host: raw.gpu_times_from_host, - // An adopted device keeps no execution record: validation makes its own devices. - record_executions: false, -+ // An adopted device's rebinds go as a made device's do by default. -+ refuse_rebinds: false, - announce_each: raw.announce_each, - view_reinterpretation, - generated_commands: false, -diff --git a/crates/metal2vulkan-runtime/src/device/create.rs b/crates/metal2vulkan-runtime/src/device/create.rs -index 76a0d07..77ac8af 100644 ---- a/crates/metal2vulkan-runtime/src/device/create.rs -+++ b/crates/metal2vulkan-runtime/src/device/create.rs -@@ -169,6 +169,14 @@ pub struct DeviceOptions { - /// No Metal counterpart. - #[doc(hidden)] - pub record_executions: bool, -+ /// Refuse every `rebind_bytes_no_copy`, of a buffer and of a heap (`Invalid`, -+ /// [`RebindProblem::Refused`](crate::RebindProblem::Refused)), for a consumer that never moves -+ /// the host pages of what it made with `new_buffer_with_bytes_no_copy` or -+ /// `new_heap_with_bytes_no_copy`. Off by default. The device's compute, blit and render -+ /// encoders then log no calls (`replay.rs`): a log is read only to encode its encoder again at -+ /// a rebind, so a draw pays nothing for a log no one reads. `detach_backing` is unchanged. No -+ /// Metal counterpart. -+ pub refuse_rebinds: bool, - } - - /// One setting of an instance layer (`VkLayerSettingEXT`). -@@ -1248,6 +1256,7 @@ fn create_on( - gpu_times_from_host: options.gpu_times_from_host, - announce_each: options.announce_each.clone(), - record_executions: options.record_executions, -+ refuse_rebinds: options.refuse_rebinds, - view_reinterpretation, - generated_commands, - host_image_copy, -@@ -1356,6 +1365,7 @@ fn same_options(a: &DeviceOptions, b: &DeviceOptions) -> bool { - announce_each, - metal_level, - record_executions, -+ refuse_rebinds, - } = a; - *policy == b.policy - && *layers == b.layers -@@ -1374,6 +1384,7 @@ fn same_options(a: &DeviceOptions, b: &DeviceOptions) -> bool { - && *announce_each == b.announce_each - && *metal_level == b.metal_level - && *record_executions == b.record_executions -+ && *refuse_rebinds == b.refuse_rebinds - } - - /// The group of `made` for `options`: the live one, or a new one on a new instance. Its index. -diff --git a/crates/metal2vulkan-runtime/src/device/mod.rs b/crates/metal2vulkan-runtime/src/device/mod.rs -index 8842010..81304a0 100644 ---- a/crates/metal2vulkan-runtime/src/device/mod.rs -+++ b/crates/metal2vulkan-runtime/src/device/mod.rs -@@ -366,6 +366,9 @@ pub(super) struct DeviceShared { - /// Its command buffers keep execution records (`DeviceOptions::record_executions`): where - /// not, its encoders build none (`EncoderCore::executed`). - pub(crate) record_executions: bool, -+ /// It refuses every rebind (`DeviceOptions::refuse_rebinds`): its encoders log no calls -+ /// ([`DeviceShared::logs_calls`]). -+ pub(crate) refuse_rebinds: bool, - /// Counts the times an image of the device let its views go, under its views lock (a - /// rebind's or a storage form's new `VkImage`, a detach's stand-in, a reattach): a view kept - /// by what it was made of (`binding::BindMemo`) is current while this holds. -@@ -373,6 +376,13 @@ pub(super) struct DeviceShared { - } - - impl DeviceShared { -+ /// Whether an encoder opened now logs its calls (`replay.rs`): where the device has a buffer -+ /// over host pages, unless it refuses every rebind, the only reader of a log -+ /// (`DeviceOptions::refuse_rebinds`). -+ pub(crate) fn logs_calls(&self) -> bool { -+ !self.refuse_rebinds && self.rebinds.any() -+ } -+ - /// What `create` makes with the `VkPipelineCache` it is handed, its `vkCreate*Pipelines` call: - /// a cache of the making's own, seeded from the record `making` names - /// (`ShaderCache::make_pipelines`, `shader_cache/driver.rs`). On a pool's thread the exit waits -@@ -754,6 +764,8 @@ pub(super) struct Parts { - pub(super) announce_each: BTreeSet, - /// Its command buffers keep execution records (`DeviceOptions::record_executions`). - pub(super) record_executions: bool, -+ /// It refuses every rebind (`DeviceOptions::refuse_rebinds`). -+ pub(super) refuse_rebinds: bool, - /// Its views may reinterpret an image's components ([`DeviceShared`]'s field), outside - /// `enabled`'s vocabulary. - pub(super) view_reinterpretation: bool, -@@ -821,6 +833,7 @@ impl Device { - gpu_times_from_host, - announce_each, - record_executions, -+ refuse_rebinds, - view_reinterpretation, - generated_commands, - host_image_copy, -@@ -1219,6 +1232,7 @@ impl Device { - properties, - announced: Announced::new(&announce_each), - record_executions, -+ refuse_rebinds, - views_epoch: AtomicU64::new(0), - device_address, - upload_alignment, -diff --git a/crates/metal2vulkan-runtime/src/rebind.rs b/crates/metal2vulkan-runtime/src/rebind.rs -index 2bce8e6..e8a8d2c 100644 ---- a/crates/metal2vulkan-runtime/src/rebind.rs -+++ b/crates/metal2vulkan-runtime/src/rebind.rs -@@ -77,6 +77,9 @@ pub enum RebindProblem { - /// a resource placed in it, or has an encoder open that used one (or one committed whose - /// commands the runtime records only once earlier work completed): commit or drop it first. - EncodedUses, -+ /// The device refuses every rebind (`DeviceOptions::refuse_rebinds`): its encoders log no -+ /// calls to encode again. -+ Refused, - } - - /// The device's buffers over host pages, by their hazard-tracking key, with the command buffers -@@ -721,7 +724,8 @@ impl Buffer { - /// (`NotImplemented(RebindTextures)`: the texture's copies, views and aliasing image are - /// planned at that offset), or where the device cannot make a texture's aliasing image again - /// over the new memory (`NotImplemented(RebindTextures)`, which says when); memory the device -- /// cannot import exactly, as `new_buffer_with_bytes_no_copy` refuses it; a lost device. -+ /// cannot import exactly, as `new_buffer_with_bytes_no_copy` refuses it; a lost device; a -+ /// device made with `DeviceOptions::refuse_rebinds` (`Invalid`, [`RebindProblem::Refused`]). - /// - /// # Safety - /// -@@ -777,7 +781,8 @@ impl Heap { - /// [`RebindProblem::EncodedUses`]); memory whose first byte is off the import's unit - /// (`Invalid`, `HeapProblem::HostMemoryAlignment`); memory the device cannot import exactly; - /// an image the device cannot make again there (as `new_texture_at_offset` refuses it); a -- /// lost device. -+ /// lost device; a device made with `DeviceOptions::refuse_rebinds` (`Invalid`, -+ /// [`RebindProblem::Refused`]). - /// - /// # Safety - /// -diff --git a/crates/metal2vulkan-runtime/src/render.rs b/crates/metal2vulkan-runtime/src/render.rs -index 110c428..5cd377c 100644 ---- a/crates/metal2vulkan-runtime/src/render.rs -+++ b/crates/metal2vulkan-runtime/src/render.rs -@@ -1273,8 +1273,7 @@ impl CommandBuffer { - let log = match again { - None => device - .shared() -- .rebinds -- .any() -+ .logs_calls() - .then(|| RenderLog::new(descriptor)), - Some(_) => None, - }; -diff --git a/crates/metal2vulkan-runtime/src/replay.rs b/crates/metal2vulkan-runtime/src/replay.rs -index f514934..d39f82d 100644 ---- a/crates/metal2vulkan-runtime/src/replay.rs -+++ b/crates/metal2vulkan-runtime/src/replay.rs -@@ -13,7 +13,8 @@ - //! over where their buffers lie now. A call refused at its first encoding is refused again, - //! which leaves the encoder as it was encoded first ([`Replay::run`] fails, and the encoder is - //! not encoded again). The log starts at the encoder's open where the device has a buffer over --//! host pages (`Rebinds::any`); else nothing is logged. It costs one push per -+//! host pages (`Rebinds::any`) and does not refuse every rebind (`DeviceOptions::refuse_rebinds`, -+//! `DeviceShared::logs_calls`); else nothing is logged. It costs one push per - //! call; a slice of buffers, offsets, textures or bytes goes into one of the log's arenas, so - //! the common calls allocate nothing of their own. - //! - **What is kept.** The command buffer keeps an ended encoder's log only where a command of -diff --git a/crates/metal2vulkan-runtime/src/tests/mod.rs b/crates/metal2vulkan-runtime/src/tests/mod.rs -index 998d7f8..f579adc 100644 ---- a/crates/metal2vulkan-runtime/src/tests/mod.rs -+++ b/crates/metal2vulkan-runtime/src/tests/mod.rs -@@ -97,6 +97,8 @@ mod private_properties; - mod queue; - mod range; - mod rate_map; -+#[cfg(any(m2v_gpu, m2v_cpu_icd))] -+mod refuse_rebinds; - mod resource; - mod resource_ids; - mod rotated; -diff --git a/crates/metal2vulkan-runtime/src/tests/refuse_rebinds.rs b/crates/metal2vulkan-runtime/src/tests/refuse_rebinds.rs -new file mode 100644 -index 0000000..07ea7e2 ---- /dev/null -+++ b/crates/metal2vulkan-runtime/src/tests/refuse_rebinds.rs -@@ -0,0 +1,147 @@ -+//! A device made with `DeviceOptions::refuse_rebinds`: its encoders log no calls while it has a -+//! buffer over host pages (`replay.rs`, `DeviceShared::logs_calls`), and it refuses a rebind -+//! (`Invalid`, `RebindProblem::Refused`), dropping the deallocator unrun; a device made without it -+//! logs while it has one, as before. -+//! -+//! A device test: compiled only under the validation crate's GPU safety cfgs (`m2v_gpu`, -+//! `m2v_cpu_icd`), on the kind of device the cfg selects. -+ -+use crate::{ -+ Buffer, Deallocator, Device, DeviceKind, DeviceOptions, Error, HostMemory, Invalid, Problem, -+ RebindProblem, ResourceOptions, ShaderCacheOptions, -+}; -+use std::alloc::Layout; -+use std::path::{Path, PathBuf}; -+use std::ptr::NonNull; -+use std::sync::atomic::{AtomicUsize, Ordering}; -+use std::sync::Arc; -+use std::time::{Duration, Instant}; -+ -+/// The no-copy buffers' bytes and alignment. -+const NO_COPY: usize = 64 << 10; -+const NO_COPY_ALIGN: usize = 64 << 10; -+ -+/// The longest a released buffer's deallocator may take to run. -+const WAIT: Duration = Duration::from_secs(10); -+ -+/// A scratch directory for the device's shader cache, removed when dropped. -+struct Scratch(PathBuf); -+ -+impl Scratch { -+ fn new(name: &str) -> Scratch { -+ let path = std::env::temp_dir().join(format!( -+ "m2v-refuse-rebinds-{name}-{}", -+ std::process::id() -+ )); -+ std::fs::create_dir_all(&path).unwrap(); -+ Scratch(path) -+ } -+} -+ -+impl Drop for Scratch { -+ fn drop(&mut self) { -+ let _ = std::fs::remove_dir_all(&self.0); -+ } -+} -+ -+/// The device the cfg selects, refusing rebinds where `refuse`, its shader cache in `cache`. -+fn device(cache: &Path, refuse: bool) -> Device { -+ let options = DeviceOptions { -+ kinds: match cfg!(m2v_cpu_icd) { -+ true => vec![DeviceKind::Cpu], -+ false => vec![ -+ DeviceKind::Discrete, -+ DeviceKind::Integrated, -+ DeviceKind::Virtual, -+ ], -+ }, -+ shader_cache: ShaderCacheOptions { -+ directory: Some(cache.to_owned()), -+ ..ShaderCacheOptions::default() -+ }, -+ refuse_rebinds: refuse, -+ ..DeviceOptions::default() -+ }; -+ Device::system_default_with(&options).unwrap() -+} -+ -+/// Memory of the test's, zeroed, and the deallocator that frees it, counting its runs in `runs`. -+fn memory(runs: &Arc) -> (NonNull, Deallocator) { -+ let layout = Layout::from_size_align(NO_COPY, NO_COPY_ALIGN).unwrap(); -+ // SAFETY: the layout's size is not zero. -+ let memory = NonNull::new(unsafe { std::alloc::alloc_zeroed(layout) }).unwrap(); -+ let runs = Arc::clone(runs); -+ let deallocator: Deallocator = Box::new(move |pointer, _| { -+ // SAFETY: the pointer is the memory allocated above with `layout`, freed once. -+ unsafe { std::alloc::dealloc(pointer.as_ptr(), layout) }; -+ runs.fetch_add(1, Ordering::SeqCst); -+ }); -+ (memory, deallocator) -+} -+ -+/// A `Shared` buffer over memory of the test's, whose deallocator counts into `runs`, and the -+/// memory. -+fn no_copy(device: &Device, runs: &Arc) -> (Buffer, NonNull) { -+ let (memory, deallocator) = memory(runs); -+ // SAFETY: the memory is the test's, mapped and writable, aligned to 64 KiB, until the -+ // deallocator frees it once the buffer's last reference goes. -+ let buffer = unsafe { -+ device.new_buffer_with_bytes_no_copy( -+ HostMemory::new(memory), -+ NO_COPY as u64, -+ ResourceOptions::shared(), -+ Some(deallocator), -+ ) -+ } -+ .unwrap(); -+ (buffer, memory) -+} -+ -+/// Wait for `runs` to reach one. -+#[track_caller] -+fn ran_once(runs: &AtomicUsize) { -+ let start = Instant::now(); -+ while runs.load(Ordering::SeqCst) == 0 && start.elapsed() < WAIT { -+ std::thread::sleep(Duration::from_millis(5)); -+ } -+ assert_eq!(runs.load(Ordering::SeqCst), 1, "the deallocator's runs"); -+} -+ -+/// Without the option, a device with a no-copy buffer logs its encoders' calls; with it, it logs -+/// none, and refuses the buffer's rebind without running the new memory's deallocator (that -+/// memory leaks), the buffer still over its own memory, which is freed when the buffer goes. -+#[test] -+fn a_device_that_refuses_rebinds_logs_no_calls() { -+ let scratch = Scratch::new("default"); -+ let plain = device(&scratch.0, false); -+ assert!(!plain.shared().logs_calls(), "no buffer over host pages yet"); -+ let plain_runs = Arc::new(AtomicUsize::new(0)); -+ let (kept, _) = no_copy(&plain, &plain_runs); -+ assert!(plain.shared().logs_calls()); -+ drop(kept); -+ ran_once(&plain_runs); -+ -+ let scratch = Scratch::new("refusing"); -+ let refusing = device(&scratch.0, true); -+ let runs = Arc::new(AtomicUsize::new(0)); -+ let (buffer, own) = no_copy(&refusing, &runs); -+ assert!(refusing.shared().rebinds.any()); -+ assert!(!refusing.shared().logs_calls()); -+ let new_runs = Arc::new(AtomicUsize::new(0)); -+ let (new, deallocator) = memory(&new_runs); -+ // SAFETY: the new memory holds the buffer's length; the call is refused and keeps nothing. -+ let rebound = -+ unsafe { buffer.rebind_bytes_no_copy(HostMemory::new(new), Some(deallocator), None) }; -+ assert_eq!( -+ rebound, -+ Err(Error::Invalid(Invalid::new( -+ "rebind_bytes_no_copy", -+ Problem::Rebind(RebindProblem::Refused) -+ ))) -+ ); -+ assert_eq!(new_runs.load(Ordering::SeqCst), 0); -+ assert_eq!(buffer.contents(), Some(own)); -+ drop(buffer); -+ ran_once(&runs); -+ assert_eq!(new_runs.load(Ordering::SeqCst), 0); -+} diff --git a/bridge/patches/0014-in-place-written-textures.patch b/bridge/patches/0014-in-place-written-textures.patch new file mode 100644 index 0000000..dc93b8d --- /dev/null +++ b/bridge/patches/0014-in-place-written-textures.patch @@ -0,0 +1,110 @@ +diff --git a/crates/metal2vulkan-runtime/src/in_place.rs b/crates/metal2vulkan-runtime/src/in_place.rs +index 86f42373..d287774a 100644 +--- a/crates/metal2vulkan-runtime/src/in_place.rs ++++ b/crates/metal2vulkan-runtime/src/in_place.rs +@@ -251,6 +251,10 @@ pub(crate) fn in_place( + if policy == HostPageTexturePolicy::Shadow { + return Err(ShadowCause::Exact); + } ++ // Trusting the driver for every usage (`HostPageTexturePolicy::InPlaceWrites`): no check. ++ if policy == HostPageTexturePolicy::InPlaceWrites { ++ return Ok(()); ++ } + if usage.contains(TextureUsage::RENDER_TARGET) { + return Err(ShadowCause::RenderTarget); + } +diff --git a/crates/metal2vulkan-runtime/src/linear_plan.rs b/crates/metal2vulkan-runtime/src/linear_plan.rs +index 034be29f..ab2551eb 100644 +--- a/crates/metal2vulkan-runtime/src/linear_plan.rs ++++ b/crates/metal2vulkan-runtime/src/linear_plan.rs +@@ -947,6 +947,26 @@ pub(super) fn holding(layout: &BufferLayout, usage: TextureUsage) -> Result<(), + )) + } + ++/// Whether a render pass that keeps a texture `width` texels wide over `layout`'s texels in tile ++/// memory stores it back within the bytes an image over them may hold, in a buffer of ++/// `buffer_length` bytes: a tile store reaching the texture's right or bottom edge writes whole ++/// 16x4-texel blocks, so up to 15 texels past a row's last and 3 rows past its last row. Every ++/// row's block must end within its pitch, and the last block within the buffer; one slice only. ++pub(super) fn tile_store_fits(layout: &BufferLayout, width: u64, buffer_length: u64) -> bool { ++ const TILE: (u64, u64) = (16, 4); ++ if layout.slices != 1 || width == 0 || !layout.row_bytes.is_multiple_of(width) { ++ return false; ++ } ++ let block_row = width.next_multiple_of(TILE.0) * (layout.row_bytes / width); ++ let last_row = layout.rows.next_multiple_of(TILE.1) - 1; ++ layout.row_pitch >= block_row ++ && layout ++ .offset ++ .saturating_add(layout.row_pitch.saturating_mul(last_row)) ++ .saturating_add(block_row) ++ <= buffer_length ++} ++ + /// Whether a linear image over `layout`'s texels, whose subresources take `size` bytes each from + /// the first texel of their slice, `array_pitch` apart, holds no byte but the texture's texels: + /// the texels lie with no byte between them ([`holding`]), and its last subresource ends at their +diff --git a/crates/metal2vulkan-runtime/src/texture.rs b/crates/metal2vulkan-runtime/src/texture.rs +index 18bf0dde..2e786f6c 100644 +--- a/crates/metal2vulkan-runtime/src/texture.rs ++++ b/crates/metal2vulkan-runtime/src/texture.rs +@@ -84,7 +84,7 @@ use super::image_plan::{ + use super::in_place::{self, PagesClass, Refusal, ShadowCause, Verdict}; + use super::key_hash::KeyMap; + use super::linear_plan::{ +- aliases, check_linear_limits, holding, linear_lack, LinearImage, LinearLack, ++ aliases, check_linear_limits, holding, linear_lack, tile_store_fits, LinearImage, LinearLack, + }; + use super::memory::{MemoryClass, Placement}; + use super::nan_clear; +@@ -2258,7 +2258,30 @@ impl Device { + None => { + // Whether an image may hold the texels: none where their rows (or slices) lie + // with the buffer's bytes between them (`linear_plan::holding`), and else why. +- let holds = holding(&texels.layout, usage); ++ let mut holds = holding(&texels.layout, usage); ++ // Rows that lie apart alias the buffer's bytes, written too, under ++ // `HostPageTexturePolicy::InPlaceWrites`: the bytes between them are no texel's, ++ // and a program that pads its framebuffers (a window server's) keeps nothing ++ // there. A written texture's tile stores must stay within its rows ++ // (`linear_plan::tile_store_fits`). Rows that share bytes stay refused. ++ let policy = self.compile_device().policy().host_page_textures; ++ let writes = usage.intersects( ++ TextureUsage::RENDER_TARGET ++ | TextureUsage::SHADER_WRITE ++ | TextureUsage::SHADER_ATOMIC, ++ ); ++ if holds.is_err() ++ && policy == HostPageTexturePolicy::InPlaceWrites ++ && texels.layout.shared_bytes() == 0 ++ && (!writes ++ || tile_store_fits( ++ &texels.layout, ++ u64::from(layout.extent[0]), ++ texels.buffer.length(), ++ )) ++ { ++ holds = Ok(()); ++ } + // The usage the texture lacks, as for a texture of its own: plain Metal writes a + // texture over a buffer made without `ShaderWrite` and reads one made without + // `ShaderRead`. A linear image aliasing the buffer's memory takes it where one +diff --git a/crates/metal2vulkan/src/device/policy.rs b/crates/metal2vulkan/src/device/policy.rs +index be5bec3d..6687452e 100644 +--- a/crates/metal2vulkan/src/device/policy.rs ++++ b/crates/metal2vulkan/src/device/policy.rs +@@ -271,6 +271,14 @@ pub enum HostPageTexturePolicy { + /// texels into before each use and merge back after each write, whose copies touch the + /// texels alone. + Shadow, ++ /// [`InPlace`](Self::InPlace) for every usage, a texture that renders or writes too, with no ++ /// check run and no verdict waited for, trusting the driver to touch no byte beside the ++ /// texels: a window server's surfaces, which it renders into each frame, are otherwise copied ++ /// into a shadow before each pass and merged back after it. A padded texture over a buffer of ++ /// the runtime's own memory aliases its bytes too, a written one where its tile stores stay ++ /// within its rows (`linear_plan::tile_store_fits`), where the device lays it out at the ++ /// buffer's row pitch. ++ InPlaceWrites, + } + + /// How a chain of `half` additions, subtractions and products rounds on a device without diff --git a/bridge/patches/0014-map-tracker-keys.patch b/bridge/patches/0014-map-tracker-keys.patch deleted file mode 100644 index 6190f2e..0000000 --- a/bridge/patches/0014-map-tracker-keys.patch +++ /dev/null @@ -1,66 +0,0 @@ -diff --git a/crates/metal2vulkan-runtime/src/tracker.rs b/crates/metal2vulkan-runtime/src/tracker.rs -index 58ccac6..8350242 100644 ---- a/crates/metal2vulkan-runtime/src/tracker.rs -+++ b/crates/metal2vulkan-runtime/src/tracker.rs -@@ -909,7 +909,7 @@ fn coalesce(entries: &mut Vec) { - /// The uses one per key and range, sorted by key, without the ones that touch nothing. - fn merged(uses: impl IntoIterator>) -> Vec> { - let mut merged: Vec> = Vec::new(); -- let mut index: HashMap<(S, UseRange), usize> = HashMap::new(); -+ let mut index: KeyMap<(S, UseRange), usize> = KeyMap::default(); - for used in uses { - if used.is_nothing() { - continue; -@@ -945,7 +945,7 @@ pub(super) struct EncoderUses { - /// Whether Metal runs the commands in encoded order (blit encoders, serial compute encoders). - ordered: bool, - /// The accesses of the current part's commands so far, for that order. -- inside: HashMap, Vec>, -+ inside: KeyMap, Vec>, - /// The parts, in order: the last is the current one. Never empty. - parts: Vec>, - /// The fences waited for: before the encoder's first part. -@@ -957,7 +957,7 @@ pub(super) struct EncoderUses { - /// The uses of one part of an encoder. - struct Part { - /// The tracked uses so far, one per range, at most [`ENTRIES_PER_KEY`] per resource. -- tracked: HashMap>, -+ tracked: KeyMap>, - stages: vk::PipelineStageFlags2, - } - -@@ -1008,7 +1008,7 @@ impl KeyUses { - impl Part { - fn new() -> Part { - Part { -- tracked: HashMap::new(), -+ tracked: KeyMap::default(), - stages: vk::PipelineStageFlags2::NONE, - } - } -@@ -1019,7 +1019,7 @@ impl EncoderUses { - pub(super) fn new(ordered: bool) -> EncoderUses { - EncoderUses { - ordered, -- inside: HashMap::new(), -+ inside: KeyMap::default(), - parts: vec![Part::new()], - waits: Vec::new(), - updates: Vec::new(), -@@ -1247,14 +1247,14 @@ impl fmt::Debug for EncoderUses { - /// commands after them. One entry per range, at most [`ENTRIES_PER_KEY`] per resource, merged - /// as [`EncoderUses`] merges its own, which claims more and stays exact. - pub(super) struct UnorderedWrites { -- writes: HashMap>>, -+ writes: KeyMap>>, - } - - impl UnorderedWrites { - /// No writes. - pub(super) fn new() -> UnorderedWrites { - UnorderedWrites { -- writes: HashMap::new(), -+ writes: KeyMap::default(), - } - } - diff --git a/bridge/patches/0015-in-place-written-host-page-textures.patch b/bridge/patches/0015-in-place-written-host-page-textures.patch new file mode 100644 index 0000000..8e80178 --- /dev/null +++ b/bridge/patches/0015-in-place-written-host-page-textures.patch @@ -0,0 +1,32 @@ +diff --git a/crates/metal2vulkan-runtime/src/texture.rs b/crates/metal2vulkan-runtime/src/texture.rs +index 2e786f6c..b1b02b30 100644 +--- a/crates/metal2vulkan-runtime/src/texture.rs ++++ b/crates/metal2vulkan-runtime/src/texture.rs +@@ -2221,6 +2221,27 @@ impl Device { + true => HostPageTexturePolicy::InPlace, + false => self.compile_device().policy().host_page_textures, + }; ++ // A written texture in place under `InPlaceWrites` whose tile stores would ++ // reach past its rows or its buffer (`linear_plan::tile_store_fits`) is a ++ // shadow instead. ++ let writes = usage.intersects( ++ TextureUsage::RENDER_TARGET ++ | TextureUsage::SHADER_WRITE ++ | TextureUsage::SHADER_ATOMIC, ++ ); ++ let policy = match policy { ++ HostPageTexturePolicy::InPlaceWrites ++ if writes ++ && !tile_store_fits( ++ &texels.layout, ++ u64::from(layout.extent[0]), ++ texels.buffer.length(), ++ ) => ++ { ++ HostPageTexturePolicy::Shadow ++ } ++ policy => policy, ++ }; + let verdict = |class| match checking { + true => Verdict::Passed, + false => self.in_place_verdict(class), diff --git a/bridge/patches/0016-refuse-rebinds.patch b/bridge/patches/0016-refuse-rebinds.patch new file mode 100644 index 0000000..a105a28 --- /dev/null +++ b/bridge/patches/0016-refuse-rebinds.patch @@ -0,0 +1,69 @@ +diff --git a/crates/metal2vulkan-runtime/src/rebind.rs b/crates/metal2vulkan-runtime/src/rebind.rs +index 3fe37827..17fbf6d6 100644 +--- a/crates/metal2vulkan-runtime/src/rebind.rs ++++ b/crates/metal2vulkan-runtime/src/rebind.rs +@@ -96,6 +96,8 @@ pub enum RebindProblem { + /// attachments' bytes from its open), or one committed whose commands the runtime records + /// only once earlier work completed: commit or drop it first. + EncodedUses, ++ /// The device refuses every rebind (`Device::refuse_rebinds`). ++ Refused, + } + + /// The device's buffers over host pages, by their hazard-tracking key, with the command buffers +@@ -103,6 +105,9 @@ pub enum RebindProblem { + #[derive(Default)] + pub(super) struct Rebinds { + live: AtomicUsize, ++ /// The device refuses every rebind (`Device::refuse_rebinds`): its encoders log no call and ++ /// hold no buffer over host pages, as though it had none. ++ refused: std::sync::atomic::AtomicBool, + buffers: Mutex>, + /// How many of them are detached (`detach.rs`): else no command names a detached one. + detached: AtomicUsize, +@@ -210,7 +215,7 @@ impl Rebinds { + + /// Whether the device has a buffer over host pages: else no use names one. + pub(super) fn any(&self) -> bool { +- self.live.load(Ordering::Relaxed) > 0 ++ self.live.load(Ordering::Relaxed) > 0 && !self.refused.load(Ordering::Relaxed) + } + + /// Note the buffer of `key` over host pages, `cell` its hold on them. +@@ -935,6 +940,9 @@ impl Buffer { + keep_alive: Option, + ) -> Result<(), Error> { + const CALL: &str = "rebind_bytes_no_copy"; ++ if self.device().shared().rebinds.refused.load(Ordering::Relaxed) { ++ return Err(invalid(CALL, Problem::Rebind(RebindProblem::Refused))); ++ } + let Some(cell) = self.host_cell() else { + return Err(invalid(CALL, Problem::Rebind(RebindProblem::NotHostMemory))); + }; +@@ -987,6 +995,9 @@ impl Heap { + keep_alive: Option, + ) -> Result<(), Error> { + const CALL: &str = "rebind_bytes_no_copy"; ++ if self.device().shared().rebinds.refused.load(Ordering::Relaxed) { ++ return Err(invalid(CALL, Problem::Rebind(RebindProblem::Refused))); ++ } + let Some((own, cell)) = self + .bytes() + .and_then(|own| own.host_cell().map(|cell| (own, cell))) +@@ -1016,3 +1027,16 @@ impl Device { + self.shared().rebinds.pending(key) + } + } ++ ++impl Device { ++ /// Refuse every `rebind_bytes_no_copy` of the device's buffers and heaps from now on ++ /// (`Invalid`, [`RebindProblem::Refused`]), for a consumer that never moves the pages of what ++ /// it made with `new_buffer_with_bytes_no_copy` or `new_heap_with_bytes_no_copy`, and never ++ /// detaches them: the encoders then log no call to encode again at a rebind and hold no buffer ++ /// over host pages, which only a rebind or a detach reads, so a draw pays for neither. Call ++ /// before any buffer over host pages is made. No Metal counterpart. ++ #[doc(hidden)] ++ pub fn refuse_rebinds(&self) { ++ self.shared().rebinds.refused.store(true, Ordering::Relaxed); ++ } ++} diff --git a/bridge/patches/0016-tile-stores-within-rows.patch b/bridge/patches/0016-tile-stores-within-rows.patch deleted file mode 100644 index 42c71f3..0000000 --- a/bridge/patches/0016-tile-stores-within-rows.patch +++ /dev/null @@ -1,71 +0,0 @@ -diff --git a/crates/metal2vulkan-runtime/src/linear_plan.rs b/crates/metal2vulkan-runtime/src/linear_plan.rs -index fdb8233..acf8ae7 100644 ---- a/crates/metal2vulkan-runtime/src/linear_plan.rs -+++ b/crates/metal2vulkan-runtime/src/linear_plan.rs -@@ -766,6 +766,26 @@ pub(super) fn only_reads(usage: TextureUsage) -> bool { - ) - } - -+/// Whether a render pass that keeps a texture `width` texels wide over `layout`'s texels in tile -+/// memory stores it back within the bytes an image over them may hold, in a buffer of -+/// `buffer_length` bytes: a tile store reaching the texture's right or bottom edge writes whole -+/// 16x4-texel blocks, so up to 15 texels past a row's last and 3 rows past its last row. Every -+/// row's block must end within its pitch, and the last block within the buffer; one slice only. -+pub(super) fn tile_store_fits(layout: &BufferLayout, width: u64, buffer_length: u64) -> bool { -+ const TILE: (u64, u64) = (16, 4); -+ if layout.slices != 1 || width == 0 || !layout.row_bytes.is_multiple_of(width) { -+ return false; -+ } -+ let block_row = width.next_multiple_of(TILE.0) * (layout.row_bytes / width); -+ let last_row = layout.rows.next_multiple_of(TILE.1) - 1; -+ layout.row_pitch >= block_row -+ && layout -+ .offset -+ .saturating_add(layout.row_pitch.saturating_mul(last_row)) -+ .saturating_add(block_row) -+ <= buffer_length -+} -+ - /// What an image over `layout`'s texels may hold beside them, for a texture of Metal `usage` (as - /// [`only_reads`] takes it) under `policy`, or why no image may hold them: the texels alone - /// where its rows (and slices) lie one after another, or it has one row (one slice); else, where -diff --git a/crates/metal2vulkan-runtime/src/texture.rs b/crates/metal2vulkan-runtime/src/texture.rs -index c3356f3..071beb8 100644 ---- a/crates/metal2vulkan-runtime/src/texture.rs -+++ b/crates/metal2vulkan-runtime/src/texture.rs -@@ -83,8 +83,8 @@ use super::image_plan::{ - ImagePlan, ImageRefusal, - }; - use super::linear_plan::{ -- aliases, check_linear_limits, holding, linear_lack, BufferLayout, Holds, LinearImage, -- LinearLack, -+ aliases, check_linear_limits, holding, linear_lack, tile_store_fits, BufferLayout, Holds, -+ LinearImage, LinearLack, - }; - use super::memory::{MemoryClass, Placement}; - use super::metal_texture::{self, MetalOver}; -@@ -121,7 +121,7 @@ use first_move::{FirstMove, Fresh, Pattern, Patterned, TexelCopy, PATTERN_BYTES} - use heap_tiled::{FreshDepth, FreshPattern}; - use metal2vulkan::bind::{ImageViewDesc, TextureFacts}; - use metal2vulkan::device::{ -- DeviceDescription, DeviceProfile, DriverFact, FormatSupport, VkFeature, -+ DeviceDescription, DeviceProfile, DriverFact, FormatSupport, PaddedReadsPolicy, VkFeature, - }; - use metal2vulkan::error::{ - Capability, Feature, LinearNeed, RuntimeDriverDefect, SparseNeed, Unsupported, -@@ -1961,7 +1961,14 @@ impl Device { - })?; - // What an image over the texels may hold beside them: the bytes between padded rows too - // where the texture only reads, by the policy (`linear_plan::holding`); else why none may. -- let policy = self.compile_device().policy().padded_reads; -+ let mut policy = self.compile_device().policy().padded_reads; -+ // A written texture's tile stores may reach past its rows (`linear_plan::tile_store_fits`): -+ // where they would leave the row gaps or the buffer, only a texture that reads aliases. -+ if policy == PaddedReadsPolicy::AliasWrites -+ && !tile_store_fits(&texels.layout, u64::from(layout.extent[0]), texels.buffer.length()) -+ { -+ policy = PaddedReadsPolicy::Alias; -+ } - let holds = holding(&texels.layout, usage, policy); - // Over bytes detached from their pages (`detach.rs`) no image aliases them: the texture - // is their shadow, which presents a stand-in until they lie in pages again. diff --git a/bridge/patches/0022-mipmaps-outside-the-measured-spread.patch b/bridge/patches/0022-mipmaps-outside-the-measured-spread.patch deleted file mode 100644 index ad9d9da..0000000 --- a/bridge/patches/0022-mipmaps-outside-the-measured-spread.patch +++ /dev/null @@ -1,71 +0,0 @@ -diff --git a/crates/metal2vulkan-runtime/src/blit_texture.rs b/crates/metal2vulkan-runtime/src/blit_texture.rs ---- a/crates/metal2vulkan-runtime/src/blit_texture.rs -+++ b/crates/metal2vulkan-runtime/src/blit_texture.rs -@@ -1737,11 +1737,13 @@ - TextureType::Type1D | TextureType::Type1DArray - ); - if one_dimensional || !own_format { -- return Err(gap()); -- } -- if !device.mipmaps_accepted(texture)? { - return Err(gap()); - } -+ // A format whose blit chain failed the measured-spread check, or whose Metal answer is not -+ // measured, still gets the chain: Metal generates every filterable format's mipmaps with its -+ // box filter, and a refusal leaves the levels unwritten (a texture sampled below level 0 -+ // shows garbage). The near-equivalent is announced once per device. -+ let within_spread = device.mipmaps_accepted(texture)?; - let (image, aspects) = (texture.raw_image()?, texture_aspects(texture.image_plan()?)); - device.shared().counts.pool_compiled( - crate::work_counts::PoolCompiled::ImageBlit, -@@ -1777,14 +1779,25 @@ - .collect(); - self.scrub(texture, written)?; - let pixel_format = texture.record().pixel_format; -- device.degrade(Degradation::MipmapsWithinSpread, || { -- ( -- format!("generate_mipmaps of a {} texture", pixel_format.name()), -- "the device's blit chain gives the format's mipmaps within Metal's measured \ -- spread, which the runtime checked at the format's first generate_mipmaps" -- .to_owned(), -- ) -- }); -+ if within_spread { -+ device.degrade(Degradation::MipmapsWithinSpread, || { -+ ( -+ format!("generate_mipmaps of a {} texture", pixel_format.name()), -+ "the device's blit chain gives the format's mipmaps within Metal's measured \ -+ spread, which the runtime checked at the format's first generate_mipmaps" -+ .to_owned(), -+ ) -+ }); -+ } else { -+ device.degrade(Degradation::MipmapsBoxFilter, || { -+ ( -+ format!("generate_mipmaps of a {} texture", pixel_format.name()), -+ "the device's linear blit chain, whose answers for the format lie outside \ -+ Metal's measured spread or were not measured" -+ .to_owned(), -+ ) -+ }); -+ } - Ok(()) - } - -diff --git a/crates/metal2vulkan-runtime/src/degradation.rs b/crates/metal2vulkan-runtime/src/degradation.rs ---- a/crates/metal2vulkan-runtime/src/degradation.rs -+++ b/crates/metal2vulkan-runtime/src/degradation.rs -@@ -764,6 +764,13 @@ - "each level is the device's linear blit of the one above, which on the check's inputs \ - lay within 1 unit in the last place (2 for 32-bit float) of an Apple M3 Max's or an \ - Apple M4's answer, not at either's exactly; other inputs are not checked"; -+ /// `generate_mipmaps` through the device's blit chain for a pixel format whose chain the -+ /// runtime's check did not accept within Metal's measured spread, or whose Metal answer is -+ /// not measured: Metal generates such a format's mipmaps with its box filter. -+ MipmapsBoxFilter = "mipmaps-box-filter", Precision, -+ "each level is the device's linear blit of the one above, a box filter like Metal's, \ -+ whose answers for the format lie outside the spread measured between an Apple M3 \ -+ Max and an Apple M4, or were not measured"; - /// A depth or stencil texture made in a heap on a device whose driver lays depth and - /// stencil images apart from the memory they are bound to - /// ([`metal2vulkan::device::DriverFact::DepthStencilImagesLieApart`]) that no shadow of the diff --git a/bridge/patches/0023-heap-texture-size-a-multiple-of-its-alignment.patch b/bridge/patches/0023-heap-texture-size-a-multiple-of-its-alignment.patch deleted file mode 100644 index 5025cb7..0000000 --- a/bridge/patches/0023-heap-texture-size-a-multiple-of-its-alignment.patch +++ /dev/null @@ -1,14 +0,0 @@ ---- a/crates/metal2vulkan-runtime/src/texture/heap.rs -+++ b/crates/metal2vulkan-runtime/src/texture/heap.rs -@@ -1290,6 +1290,11 @@ - } - size_and_align = metal; - } -+ // Metal's size is a multiple of its alignment, so a heap sized as the sum of its textures' -+ // sizes holds them in any order. Where the device's image needs a coarser alignment than -+ // Metal's model (Turnip: 4 KiB against Metal's 128 bytes), the size grows to a multiple of -+ // it too: otherwise each texture placed after one pads past what the heap was sized for. -+ size_and_align.size = size_and_align.size.next_multiple_of(size_and_align.align.max(1)); - check_heap_base(size_and_align.align)?; - // The size answered is the optimized texture's where the driver keeps the layout. - self.degrade_optimized_layout(descriptor, &layout); diff --git a/bridge/patches/0025-set-viewports-and-scissors-in-place.patch b/bridge/patches/0025-set-viewports-and-scissors-in-place.patch deleted file mode 100644 index 5b43d4f..0000000 --- a/bridge/patches/0025-set-viewports-and-scissors-in-place.patch +++ /dev/null @@ -1,69 +0,0 @@ -diff --git a/crates/metal2vulkan-runtime/src/render_state.rs b/crates/metal2vulkan-runtime/src/render_state.rs -index e231b10..c7b6521 100644 ---- a/crates/metal2vulkan-runtime/src/render_state.rs -+++ b/crates/metal2vulkan-runtime/src/render_state.rs -@@ -252,19 +252,18 @@ impl EncoderState { - refused: Option, - ) -> Result<(), Error> { - check_count(call, viewports.len())?; -- // A NaN origin x draws nothing, as plain Metal's: a viewport of no width. -- let viewports: Vec = viewports -- .iter() -- .map(|v| match v.origin_x.is_nan() { -- true => Viewport { -- origin_x: 0.0, -- width: 0.0, -- ..*v -- }, -- false => *v, -- }) -- .collect(); -- for v in &viewports { -+ // A NaN origin x draws nothing, as plain Metal's: a viewport of no width. The vector is -+ // written in place, so a draw's call allocates nothing once the encoder's has its room. -+ self.viewports.clear(); -+ self.viewports.extend(viewports.iter().map(|v| match v.origin_x.is_nan() { -+ true => Viewport { -+ origin_x: 0.0, -+ width: 0.0, -+ ..*v -+ }, -+ false => *v, -+ })); -+ for v in &self.viewports { - let fields = [v.origin_x, v.origin_y, v.width, v.height, v.znear, v.zfar]; - if fields.iter().any(|f| !f.is_finite()) { - return Err(invalid( -@@ -273,10 +272,9 @@ impl EncoderState { - )); - } - } -- if let Some(error) = refused.filter(|_| viewports.iter().any(outside_depth)) { -+ if let Some(error) = refused.filter(|_| self.viewports.iter().any(outside_depth)) { - return Err(error); - } -- self.viewports = viewports; - Ok(()) - } - -@@ -302,14 +300,12 @@ impl EncoderState { - let start = origin.min(end); - (start, origin.saturating_add(size).min(end) - start) - }; -- self.scissors = scissors -- .iter() -- .map(|s| { -- let (x, width) = clip(s.x, s.width, width); -- let (y, height) = clip(s.y, s.height, height); -- ScissorRect::new(x, y, width, height) -- }) -- .collect(); -+ self.scissors.clear(); -+ self.scissors.extend(scissors.iter().map(|s| { -+ let (x, width) = clip(s.x, s.width, width); -+ let (y, height) = clip(s.y, s.height, height); -+ ScissorRect::new(x, y, width, height) -+ })); - Ok(()) - } - diff --git a/bridge/patches/0026-keep-push-and-table-bytes.patch b/bridge/patches/0026-keep-push-and-table-bytes.patch deleted file mode 100644 index 7c5cb91..0000000 --- a/bridge/patches/0026-keep-push-and-table-bytes.patch +++ /dev/null @@ -1,155 +0,0 @@ -diff --git a/crates/metal2vulkan-runtime/src/binding.rs b/crates/metal2vulkan-runtime/src/binding.rs -index df2525f..d54a41e 100644 ---- a/crates/metal2vulkan-runtime/src/binding.rs -+++ b/crates/metal2vulkan-runtime/src/binding.rs -@@ -757,6 +757,17 @@ pub(super) fn push_writes( - Ok(filled) - } - -+/// `push_writes`'s writes, given back as the plan's: the next draw's instantiation fills their -+/// vectors again instead of allocating its own. -+pub(super) fn push_room(push: Vec) -> Vec { -+ push.into_iter() -+ .map(|(stages, offset, bytes)| { -+ let stages = metal2vulkan::vk::ShaderStageFlags::from_raw(stages.as_raw()); -+ PushConstantWrite::new(stages, offset, bytes) -+ }) -+ .collect() -+} -+ - /// The descriptor writes [`write_sets`] hands the driver in one call: their infos sit on the stack. - const WRITES_AT_ONCE: usize = 16; - -diff --git a/crates/metal2vulkan-runtime/src/render.rs b/crates/metal2vulkan-runtime/src/render.rs -index 5cd377c..d50c194 100644 ---- a/crates/metal2vulkan-runtime/src/render.rs -+++ b/crates/metal2vulkan-runtime/src/render.rs -@@ -7324,6 +7324,8 @@ impl RenderCommandEncoder { - } else { - self.uses_room = uses.into_room(); - } -+ // The push writes' vectors go back to the commands, which the next draw fills. -+ commands.push_constants = binding::push_room(push); - self.draw_commands = Some(commands); - // The states go; the list stays for the next draw's. - argument_samplers.clear(); -diff --git a/crates/metal2vulkan/src/bind/graphics.rs b/crates/metal2vulkan/src/bind/graphics.rs -index c79f0d4..896c4ba 100644 ---- a/crates/metal2vulkan/src/bind/graphics.rs -+++ b/crates/metal2vulkan/src/bind/graphics.rs -@@ -3340,7 +3340,7 @@ fn instantiate_into( - out: &mut DrawCommands, - ) -> Result<(), BindError> { - // The vectors `out` held, which this draw's commands take, emptied. -- let kept = std::mem::replace(out, DrawCommands::empty()); -+ let mut kept = std::mem::replace(out, DrawCommands::empty()); - let overrun = index_overrun(t.robust_index_fetch, draw); - let (command, base) = vk_draw(draw, overrun); - let capture = match t.capture { -@@ -3381,22 +3381,38 @@ fn instantiate_into( - }; - let vertex = state.stages.get(&Stage::Vertex); - -- let mut push_constants = emptied(kept.push_constants); -- let mut descriptor_bytes = emptied(kept.descriptor_bytes); -+ // The last draw's push and table writes, whose byte vectors this draw's stages take (a plan's -+ // stages write the same ranges, so the room is the same): a draw allocates them once. -+ let mut push_constants = std::mem::take(&mut kept.push_constants); -+ let mut descriptor_bytes = std::mem::take(&mut kept.descriptor_bytes); - let mut push_holes = emptied(kept.push_holes); - let mut root_tables = emptied(kept.root_tables); - let mut reads_base = false; -+ // The writes made so far: the stages with push fields, and those with a table. -+ let (mut pushes, mut tables_written) = (0usize, 0usize); - for st in &t.stages { - let (push_start, push_size) = st.push_range.unwrap_or((0, 0)); -- let mut push = vec![0u8; push_size as usize]; -- let mut table = vec![ -- 0u8; -+ // Only a stage that writes them takes a slot's vector: one without would leave the next -+ // stage's slot emptied. -+ let mut push = match push_constants.get_mut(pushes) { -+ Some(write) if push_size > 0 => std::mem::take(&mut write.bytes), -+ _ => Vec::new(), -+ }; -+ push.clear(); -+ push.resize(push_size as usize, 0); -+ let mut table = match descriptor_bytes.get_mut(tables_written) { -+ Some(write) if st.table.is_some() => std::mem::take(&mut write.bytes), -+ _ => Vec::new(), -+ }; -+ table.clear(); -+ table.resize( - if st.table.is_some() { - st.table_size as usize - } else { - 0 -- } -- ]; -+ }, -+ 0, -+ ); - for f in &st.fields { - let bytes: FieldBytes = match f.field { - PushField::RootTableAddress -@@ -3557,20 +3573,33 @@ fn instantiate_into( - root_tables.push((st.flags, stage_root_table(&statics, first, resident))); - } - if push_size > 0 { -- push_constants.push(PushConstantWrite { -+ let write = PushConstantWrite { - stages: st.flags, - offset: push_start, - bytes: push, -- }); -+ }; -+ match push_constants.get_mut(pushes) { -+ Some(slot) => *slot = write, -+ None => push_constants.push(write), -+ } -+ pushes += 1; - } - if let Some((set, binding)) = st.table { -- descriptor_bytes.push(DescriptorBytes { -+ let write = DescriptorBytes { - set, - binding, - bytes: table, -- }); -+ }; -+ match descriptor_bytes.get_mut(tables_written) { -+ Some(slot) => *slot = write, -+ None => descriptor_bytes.push(write), -+ } -+ tables_written += 1; - } - } -+ // The last draw's writes this one has fewer of: their vectors' room is not kept. -+ push_constants.truncate(pushes); -+ descriptor_bytes.truncate(tables_written); - - // Vertex buffers: offsets, per-bind strides, the address rules, and the bytes the draw's - // vertex and instance ranges fetch. -diff --git a/crates/metal2vulkan/src/bind/instantiate.rs b/crates/metal2vulkan/src/bind/instantiate.rs -index e474e8c..1acb2d8 100644 ---- a/crates/metal2vulkan/src/bind/instantiate.rs -+++ b/crates/metal2vulkan/src/bind/instantiate.rs -@@ -25,6 +25,18 @@ pub struct PushConstantWrite { - pub bytes: Vec, - } - -+impl PushConstantWrite { -+ /// A write of `bytes` at `offset` for `stages`: a consumer giving back the vectors of the -+ /// writes it took, which the next instantiation fills again. -+ pub fn new(stages: vk::ShaderStageFlags, offset: u32, bytes: Vec) -> Self { -+ Self { -+ stages, -+ offset, -+ bytes, -+ } -+ } -+} -+ - /// Bytes the consumer uploads into the buffer behind a plan binding (argument table, handle - /// table) before recording the draw or dispatch. - #[non_exhaustive] diff --git a/bridge/patches/0027-keep-bind-vectors.patch b/bridge/patches/0027-keep-bind-vectors.patch deleted file mode 100644 index bfc8ee4..0000000 --- a/bridge/patches/0027-keep-bind-vectors.patch +++ /dev/null @@ -1,129 +0,0 @@ -diff --git a/crates/metal2vulkan-runtime/src/binding.rs b/crates/metal2vulkan-runtime/src/binding.rs -index d54a41e..5caa3f2 100644 ---- a/crates/metal2vulkan-runtime/src/binding.rs -+++ b/crates/metal2vulkan-runtime/src/binding.rs -@@ -454,8 +454,17 @@ pub(super) fn bind( - tables: &Tables<'_>, - mut memo: Option<&mut BindMemo>, - ) -> Result { -- let mut writes = Vec::with_capacity(plan.descriptors.len()); -- let mut samplers = Vec::new(); -+ // The vectors a memo keeps from the last bind, emptied. -+ let (mut writes, mut holes, mut samplers, mut roots) = match memo.as_deref_mut() { -+ Some(memo) => ( -+ std::mem::take(&mut memo.writes), -+ std::mem::take(&mut memo.holes), -+ std::mem::take(&mut memo.bound_samplers), -+ std::mem::take(&mut memo.roots), -+ ), -+ None => Default::default(), -+ }; -+ writes.reserve(plan.descriptors.len()); - // The sampler tables, whose regions the command buffer writes again as it commits. - let sampler_keys = plan.sampler_tables(); - let mut sampler_tables = Vec::with_capacity(sampler_keys.len()); -@@ -533,11 +542,11 @@ pub(super) fn bind( - held, - }); - } -- let mut roots = Vec::with_capacity(command.root_tables.len()); -+ roots.clear(); - for &(stages, table) in &command.root_tables { - roots.push((stages, root_table(device, state, table)?)); - } -- let mut holes = Vec::with_capacity(command.push_holes.len()); -+ holes.reserve(command.push_holes.len()); - for hole in command.push_holes { - holes.push(match *hole { - PushHole::RootTableAddress { stages, offset } => Hole { -@@ -588,6 +597,9 @@ pub(super) fn bind( - }, - }); - } -+ if let Some(memo) = memo { -+ memo.roots = roots; -+ } - Ok(Bound { - writes, - holes, -@@ -889,6 +901,32 @@ pub(super) struct BindMemo { - epoch: u64, - views: HashMap, - samplers: Vec, -+ /// The vectors of the last bind's [`Bound`] its consumer gave back ([`BindMemo::give_back`]) -+ /// and its root table addresses, emptied: the next bind fills them. -+ writes: Vec, -+ holes: Vec, -+ bound_samplers: Vec>, -+ roots: Vec<(metal2vulkan::vk::ShaderStageFlags, u64)>, -+} -+ -+impl BindMemo { -+ /// Keep the vectors of a [`Bound`] its consumer is done with for the next bind; samplers -+ /// where the consumer did not keep them. -+ pub(super) fn give_back( -+ &mut self, -+ mut writes: Vec, -+ mut holes: Vec, -+ samplers: Option>>, -+ ) { -+ writes.clear(); -+ holes.clear(); -+ self.writes = writes; -+ self.holes = holes; -+ if let Some(mut samplers) = samplers { -+ samplers.clear(); -+ self.bound_samplers = samplers; -+ } -+ } - } - - /// What a view a bind made or found is a function of ([`BindMemo`]): a binding of the texture as -diff --git a/crates/metal2vulkan-runtime/src/render.rs b/crates/metal2vulkan-runtime/src/render.rs -index d50c194..62a3082 100644 ---- a/crates/metal2vulkan-runtime/src/render.rs -+++ b/crates/metal2vulkan-runtime/src/render.rs -@@ -1186,18 +1186,25 @@ impl LastRetained { - /// Retain into `core` the samplers a draw's bind named, unless they are the ones the last - /// draw's named, in order: each by another reference to it, the last first, so they go in - /// their order (`retained.rs`). -- fn retain_samplers(&mut self, core: &mut EncoderCore, samplers: Vec>) { -+ /// Answers the list where it keeps the one it had, which is the same: the next bind fills it. -+ fn retain_samplers( -+ &mut self, -+ core: &mut EncoderCore, -+ samplers: Vec>, -+ ) -> Option>> { - let same = samplers.len() == self.samplers.len() - && samplers - .iter() - .zip(&self.samplers) - .all(|(a, b)| Arc::ptr_eq(a, b)); -- if !same { -- for sampler in samplers.iter().rev() { -- core.retain_shared(sampler.clone()); -- } -- self.samplers = samplers; -+ if same { -+ return Some(samplers); - } -+ for sampler in samplers.iter().rev() { -+ core.retain_shared(sampler.clone()); -+ } -+ self.samplers = samplers; -+ None - } - } - -@@ -7306,8 +7313,10 @@ impl RenderCommandEncoder { - { - self.core.retain_shared(buffer.held()); - } -- self.last_retained -+ let unkept = self -+ .last_retained - .retain_samplers(&mut self.core, bound.samplers); -+ self.bind_memo.give_back(writes, bound.holes, unkept); - if let Some(unlisted) = unlisted { - heap_sets::degrade_unlisted(&device, unlisted, "a render pipeline state's draws"); - // A host step's draw, which no commit lists again, fails its command buffer. diff --git a/bridge/patches/0028-keep-bound-buffer-vectors.patch b/bridge/patches/0028-keep-bound-buffer-vectors.patch deleted file mode 100644 index 371b02a..0000000 --- a/bridge/patches/0028-keep-bound-buffer-vectors.patch +++ /dev/null @@ -1,45 +0,0 @@ -diff --git a/crates/metal2vulkan/src/bind/graphics.rs b/crates/metal2vulkan/src/bind/graphics.rs -index 896c4ba..ca8bf54 100644 ---- a/crates/metal2vulkan/src/bind/graphics.rs -+++ b/crates/metal2vulkan/src/bind/graphics.rs -@@ -3333,6 +3333,12 @@ fn instantiate( - Ok(out) - } - -+thread_local! { -+ /// The residual and root entry vectors of this thread's last draw's bound buffers, emptied by -+ /// the next, which fills them: no draw's [`DrawCommands`] holds them. -+ static BOUND_ROOM: std::cell::Cell = std::cell::Cell::new(BoundBuffers::default()); -+} -+ - fn instantiate_into( - t: &GraphicsTemplate, - state: &BindState, -@@ -3359,13 +3365,14 @@ fn instantiate_into( - } - }; - let command = mesh.as_ref().map_or(command, |m| m.command); -+ let room = BOUND_ROOM.take(); - let bound = bind_buffers_in( - &t.buffers, - state, - BoundBuffers { - descriptor_ranges: kept.descriptor_ranges, - extents: kept.extents, -- ..BoundBuffers::default() -+ ..room - }, - )?; - let mut descriptor_ranges = bound.descriptor_ranges; -@@ -3600,6 +3607,11 @@ fn instantiate_into( - // The last draw's writes this one has fewer of: their vectors' room is not kept. - push_constants.truncate(pushes); - descriptor_bytes.truncate(tables_written); -+ BOUND_ROOM.set(BoundBuffers { -+ residuals: bound.residuals, -+ statics: bound.statics, -+ ..BoundBuffers::default() -+ }); - - // Vertex buffers: offsets, per-bind strides, the address rules, and the bytes the draw's - // vertex and instance ranges fetch. diff --git a/bridge/patches/0029-keep-vertex-buffer-binds.patch b/bridge/patches/0029-keep-vertex-buffer-binds.patch deleted file mode 100644 index e04f745..0000000 --- a/bridge/patches/0029-keep-vertex-buffer-binds.patch +++ /dev/null @@ -1,51 +0,0 @@ -diff --git a/crates/metal2vulkan-runtime/src/render.rs b/crates/metal2vulkan-runtime/src/render.rs -index 62a3082..a507bd2 100644 ---- a/crates/metal2vulkan-runtime/src/render.rs -+++ b/crates/metal2vulkan-runtime/src/render.rs -@@ -1128,6 +1128,8 @@ pub struct RenderCommandEncoder { - uses_room: Vec>, - /// The list the last draw's argument samplers were in, emptied (`Device::argument_samplers_into`). - argument_samplers_room: Vec, -+ /// The list the last draw's vertex buffer binds were in, emptied, for the next draw's. -+ vertex_buffers_room: Vec<(u32, vk::Buffer, u64, u64)>, - /// The views and sampler descriptions its draws' binds made or found (`binding::BindMemo`). - bind_memo: binding::BindMemo, - /// What its last draw retained of its own ([`LastRetained`]). -@@ -1407,6 +1409,7 @@ impl CommandBuffer { - draw_commands: None, - uses_room: Vec::new(), - argument_samplers_room: Vec::new(), -+ vertex_buffers_room: Vec::new(), - bind_memo: binding::BindMemo::default(), - last_retained: LastRetained::default(), - }) -@@ -6469,7 +6472,8 @@ impl RenderCommandEncoder { - self.capture_missed(); - return Ok(()); - } -- let mut vertex_buffers = Vec::with_capacity(plan.vertex_buffers.len()); -+ let mut vertex_buffers = std::mem::take(&mut self.vertex_buffers_room); -+ vertex_buffers.clear(); - for bind in &plan.vertex_buffers { - let table = pass_start.or(Stage::Vertex, &self.vertex); - let offset = commands -@@ -7339,6 +7343,7 @@ impl RenderCommandEncoder { - // The states go; the list stays for the next draw's. - argument_samplers.clear(); - self.argument_samplers_room = argument_samplers; -+ self.vertex_buffers_room = vertex_buffers; - Ok(()) - } - -diff --git a/crates/metal2vulkan-runtime/src/render/executions.rs b/crates/metal2vulkan-runtime/src/render/executions.rs -index 768c128..db52d4f 100644 ---- a/crates/metal2vulkan-runtime/src/render/executions.rs -+++ b/crates/metal2vulkan-runtime/src/render/executions.rs -@@ -2097,6 +2097,7 @@ impl RenderExecution { - draw_commands: None, - uses_room: Vec::new(), - argument_samplers_room: Vec::new(), -+ vertex_buffers_room: Vec::new(), - bind_memo: crate::binding::BindMemo::default(), - last_retained: Default::default(), - } diff --git a/bridge/patches/0030-hand-draw-rooms-to-the-next-encoder.patch b/bridge/patches/0030-hand-draw-rooms-to-the-next-encoder.patch deleted file mode 100644 index 0bfd526..0000000 --- a/bridge/patches/0030-hand-draw-rooms-to-the-next-encoder.patch +++ /dev/null @@ -1,139 +0,0 @@ -diff --git a/crates/metal2vulkan-runtime/src/binding.rs b/crates/metal2vulkan-runtime/src/binding.rs -index 5caa3f2..a916162 100644 ---- a/crates/metal2vulkan-runtime/src/binding.rs -+++ b/crates/metal2vulkan-runtime/src/binding.rs -@@ -910,6 +910,18 @@ pub(super) struct BindMemo { - } - - impl BindMemo { -+ /// The memo with nothing kept but its vectors' room: what an ended encoder hands the next -+ /// (`render::DrawRooms`), holding no texture of its views. -+ pub(super) fn emptied(mut self) -> Self { -+ self.views.clear(); -+ self.samplers.clear(); -+ self.writes.clear(); -+ self.holes.clear(); -+ self.bound_samplers.clear(); -+ self.roots.clear(); -+ self -+ } -+ - /// Keep the vectors of a [`Bound`] its consumer is done with for the next bind; samplers - /// where the consumer did not keep them. - pub(super) fn give_back( -diff --git a/crates/metal2vulkan-runtime/src/device/mod.rs b/crates/metal2vulkan-runtime/src/device/mod.rs -index 81304a0..f7b18e6 100644 ---- a/crates/metal2vulkan-runtime/src/device/mod.rs -+++ b/crates/metal2vulkan-runtime/src/device/mod.rs -@@ -373,6 +373,9 @@ pub(super) struct DeviceShared { - /// rebind's or a storage form's new `VkImage`, a detach's stand-in, a reattach): a view kept - /// by what it was made of (`binding::BindMemo`) is current while this holds. - pub(crate) views_epoch: AtomicU64, -+ /// The vectors ended render encoders' draws filled, emptied, for the next encoders' draws -+ /// (`render::DrawRooms`). -+ pub(crate) draw_rooms: Mutex>, - } - - impl DeviceShared { -@@ -1234,6 +1237,7 @@ impl Device { - record_executions, - refuse_rebinds, - views_epoch: AtomicU64::new(0), -+ draw_rooms: Mutex::new(Vec::new()), - device_address, - upload_alignment, - memory: Mutex::new(memory), -diff --git a/crates/metal2vulkan-runtime/src/render.rs b/crates/metal2vulkan-runtime/src/render.rs -index a507bd2..ae505fa 100644 ---- a/crates/metal2vulkan-runtime/src/render.rs -+++ b/crates/metal2vulkan-runtime/src/render.rs -@@ -1149,6 +1149,42 @@ struct LastRetained { - samplers: Vec>, - } - -+/// The vectors a render encoder's draws fill again draw after draw, emptied: an ended encoder -+/// gives them to its device, whose next encoder takes them, so its first draws allocate them no -+/// more than its later ones. -+#[derive(Default)] -+pub(crate) struct DrawRooms { -+ draw_commands: Option, -+ uses: Vec>, -+ argument_samplers: Vec, -+ vertex_buffers: Vec<(u32, vk::Buffer, u64, u64)>, -+ bind_memo: binding::BindMemo, -+} -+ -+/// The most [`DrawRooms`] a device keeps: one per encoder encoding at once. -+const KEPT_DRAW_ROOMS: usize = 4; -+ -+impl DrawRooms { -+ /// A kept one of `device`'s, or new ones. -+ fn take(device: &crate::device::Device) -> Self { -+ crate::device::lock(&device.shared().draw_rooms) -+ .pop() -+ .unwrap_or_default() -+ } -+ -+ /// Keep them for `device`'s next encoder, emptied. -+ fn keep(mut self, device: &crate::device::Device) { -+ self.uses.clear(); -+ self.argument_samplers.clear(); -+ self.vertex_buffers.clear(); -+ self.bind_memo = self.bind_memo.emptied(); -+ let mut kept = crate::device::lock(&device.shared().draw_rooms); -+ if kept.len() < KEPT_DRAW_ROOMS { -+ kept.push(self); -+ } -+ } -+} -+ - impl LastRetained { - /// Retain into `core` a draw's pipeline state, variant objects and made `VkPipeline`, each - /// unless it is the one the last draw retained. -@@ -1318,6 +1354,7 @@ impl CommandBuffer { - render_pass::degrade_depth_clear(device, &layout); - let extended = ExtendedShadows::of(descriptor, &layout, rate_map.is_some()); - let rerecord = reroute::keeps_whole(device) || extended.keeps_draws(); -+ let rooms = DrawRooms::take(device); - Ok(RenderCommandEncoder { - core, - earlier: Vec::new(), -@@ -1406,11 +1443,11 @@ impl CommandBuffer { - live: Arc::default(), - log, - quiet_bound_values: None, -- draw_commands: None, -- uses_room: Vec::new(), -- argument_samplers_room: Vec::new(), -- vertex_buffers_room: Vec::new(), -- bind_memo: binding::BindMemo::default(), -+ draw_commands: rooms.draw_commands, -+ uses_room: rooms.uses, -+ argument_samplers_room: rooms.argument_samplers, -+ vertex_buffers_room: rooms.vertex_buffers, -+ bind_memo: rooms.bind_memo, - last_retained: LastRetained::default(), - }) - } -@@ -3069,8 +3106,21 @@ impl RenderCommandEncoder { - extended, - reload_at, - log, -+ draw_commands, -+ uses_room, -+ argument_samplers_room, -+ vertex_buffers_room, -+ bind_memo, - .. - } = self; -+ DrawRooms { -+ draw_commands, -+ uses: uses_room, -+ argument_samplers: argument_samplers_room, -+ vertex_buffers: vertex_buffers_room, -+ bind_memo, -+ } -+ .keep(core.device()); - earlier.push(current); - let mut instances = earlier; - // A color attachment's store action still `Unknown` stores nothing, as plain Metal's diff --git a/bridge/patches/0031-keep-bound-state-room.patch b/bridge/patches/0031-keep-bound-state-room.patch deleted file mode 100644 index 285b6f4..0000000 --- a/bridge/patches/0031-keep-bound-state-room.patch +++ /dev/null @@ -1,89 +0,0 @@ -diff --git a/crates/metal2vulkan-runtime/src/render.rs b/crates/metal2vulkan-runtime/src/render.rs -index ae505fa..8a327a7 100644 ---- a/crates/metal2vulkan-runtime/src/render.rs -+++ b/crates/metal2vulkan-runtime/src/render.rs -@@ -1159,6 +1159,7 @@ pub(crate) struct DrawRooms { - argument_samplers: Vec, - vertex_buffers: Vec<(u32, vk::Buffer, u64, u64)>, - bind_memo: binding::BindMemo, -+ bound: Bound, - } - - /// The most [`DrawRooms`] a device keeps: one per encoder encoding at once. -@@ -1178,6 +1179,7 @@ impl DrawRooms { - self.argument_samplers.clear(); - self.vertex_buffers.clear(); - self.bind_memo = self.bind_memo.emptied(); -+ self.bound = self.bound.emptied(); - let mut kept = crate::device::lock(&device.shared().draw_rooms); - if kept.len() < KEPT_DRAW_ROOMS { - kept.push(self); -@@ -1375,7 +1377,7 @@ impl CommandBuffer { - reformats: Vec::new(), - positioned: None, - rerecord: rerecord.then(Vec::new), -- bound: Bound::default(), -+ bound: rooms.bound, - }, - state: EncoderState::metal_defaults( - rate_map -@@ -3080,7 +3082,7 @@ impl RenderCommandEncoder { - let RenderCommandEncoder { - mut core, - mut earlier, -- current, -+ mut current, - mut layout, - passes, - mut visibility, -@@ -3119,6 +3121,7 @@ impl RenderCommandEncoder { - argument_samplers: argument_samplers_room, - vertex_buffers: vertex_buffers_room, - bind_memo, -+ bound: std::mem::take(&mut current.bound), - } - .keep(core.device()); - earlier.push(current); -diff --git a/crates/metal2vulkan-runtime/src/render/bound.rs b/crates/metal2vulkan-runtime/src/render/bound.rs -index be326c0..3892fed 100644 ---- a/crates/metal2vulkan-runtime/src/render/bound.rs -+++ b/crates/metal2vulkan-runtime/src/render/bound.rs -@@ -87,11 +87,37 @@ impl Bound { - if self.command_buffer != command_buffer { - *self = Bound { - command_buffer, -- ..Bound::default() -+ ..std::mem::take(self).emptied() - }; - } - } - -+ /// Nothing bound, its vectors' room kept: an ended encoder's for the next encoder's -+ /// (`DrawRooms`). -+ pub(super) fn emptied(self) -> Bound { -+ let Bound { -+ mut sets, -+ mut pushes, -+ mut push_bytes, -+ mut vertex, -+ mut set, -+ .. -+ } = self; -+ sets.clear(); -+ pushes.clear(); -+ push_bytes.clear(); -+ vertex.clear(); -+ set.clear(); -+ Bound { -+ sets, -+ pushes, -+ push_bytes, -+ vertex, -+ set, -+ ..Bound::default() -+ } -+ } -+ - /// Whether binding `pipeline`, which holds the dynamic states `dynamic` (the module docs), - /// sets anything: noted as bound where it does. - pub(super) fn pipeline( diff --git a/bridge/patches/0032-reshape-colors-without-cloning.patch b/bridge/patches/0032-reshape-colors-without-cloning.patch deleted file mode 100644 index 57648f1..0000000 --- a/bridge/patches/0032-reshape-colors-without-cloning.patch +++ /dev/null @@ -1,35 +0,0 @@ -diff --git a/crates/metal2vulkan-runtime/src/live_bind.rs b/crates/metal2vulkan-runtime/src/live_bind.rs -index 8696754..b4f1a7c 100644 ---- a/crates/metal2vulkan-runtime/src/live_bind.rs -+++ b/crates/metal2vulkan-runtime/src/live_bind.rs -@@ -66,6 +66,15 @@ impl Reshape { - } - *field = value; - } -+ -+ /// [`field`](Self::field) of `value`, cloned only where it changes the field: a draw of the -+ /// same state copies nothing. -+ pub(super) fn field_from(&mut self, field: &mut T, value: &T) { -+ if field != value { -+ self.0 = true; -+ field.clone_from(value); -+ } -+ } - } - - /// A render encoder's bind state as its last draw bound it, and its shape while no change may -diff --git a/crates/metal2vulkan-runtime/src/render.rs b/crates/metal2vulkan-runtime/src/render.rs -index 8a327a7..0ed9ae7 100644 ---- a/crates/metal2vulkan-runtime/src/render.rs -+++ b/crates/metal2vulkan-runtime/src/render.rs -@@ -7692,8 +7692,8 @@ impl RenderCommandEncoder { - &mut state.depth_clamp_range, - settings.written_clamp.map(|w| w.range), - ); -- reshape.field(&mut state.zeroed_colors, settings.zeroed_colors.clone()); -- reshape.field(&mut state.dropped_colors, settings.dropped_colors.clone()); -+ reshape.field_from(&mut state.zeroed_colors, &settings.zeroed_colors); -+ reshape.field_from(&mut state.dropped_colors, &settings.dropped_colors); - reshape.field(&mut state.interlocked_colors, interlocked.clone()); - reshape.field(&mut state.extended_shadows, shadows.clone()); - let depth_stencil = match &settings.failing_state { diff --git a/bridge/patches/0033-look-up-final-passes-by-reference.patch b/bridge/patches/0033-look-up-final-passes-by-reference.patch deleted file mode 100644 index b15e259..0000000 --- a/bridge/patches/0033-look-up-final-passes-by-reference.patch +++ /dev/null @@ -1,176 +0,0 @@ -diff --git a/crates/metal2vulkan-runtime/src/render_target.rs b/crates/metal2vulkan-runtime/src/render_target.rs -index e7f7391..63a28ae 100644 ---- a/crates/metal2vulkan-runtime/src/render_target.rs -+++ b/crates/metal2vulkan-runtime/src/render_target.rs -@@ -723,36 +723,48 @@ pub(super) fn final_pass( - } - let anchor = shape.anchor(device.compile_device().profile()); - let dont_care = DontCare::of(device); -- let colors: Vec = layout -- .colors -- .iter() -- .enumerate() -- .filter_map(|(index, color)| Some((index, color.as_ref()?))) -- .map(|(index, color)| color_ops(color, anchor == Some(index as u32), at, dont_care)) -- .collect(); -+ // The operations go in this thread's kept vectors: a pass found kept copies none of them. -+ let (mut colors, mut resolves) = FINAL_OPS.take(); -+ colors.clear(); -+ colors.extend( -+ layout -+ .colors -+ .iter() -+ .enumerate() -+ .filter_map(|(index, color)| Some((index, color.as_ref()?))) -+ .map(|(index, color)| color_ops(color, anchor == Some(index as u32), at, dont_care)), -+ ); - let depth_stencil = layout - .depth_stencil - .as_ref() - .map(|ds| depth_stencil_ops(layout, ds, at, dont_care)); -- let resolves = vec![ -- RESOLVED; -+ resolves.clear(); -+ resolves.resize( - layout - .colors - .iter() - .filter(|c| subpass_resolve(c.as_ref()).is_some()) -- .count() -- ]; -- kept_final_pass( -+ .count(), -+ RESOLVED, -+ ); -+ let pass = kept_final_pass( - device, -- FinalMakings { -- shape: shape.clone(), -- colors, -+ FinalKey { -+ shape, -+ colors: &colors, - depth_stencil, -- resolves, -+ resolves: &resolves, - depth_stencil_resolve: subpass_step(layout).map(step_ops), - resolving: at.last, - }, -- ) -+ ); -+ FINAL_OPS.set((colors, resolves)); -+ pass -+} -+ -+thread_local! { -+ /// The vectors [`final_pass`] puts a pass's operations in, emptied by the next. -+ static FINAL_OPS: std::cell::Cell<(Vec, Vec)> = std::cell::Cell::default(); - } - - /// The most final passes a device keeps ([`kept_final_pass`]): a program's passes take a few -@@ -760,7 +772,7 @@ pub(super) fn final_pass( - const KEPT_FINALS: usize = 256; - - /// What a final pass is made of: [`create`]'s arguments past the device. --#[derive(Clone, PartialEq, Eq, Hash)] -+#[derive(Clone, PartialEq, Eq)] - pub(super) struct FinalMakings { - shape: PassShape, - colors: Vec, -@@ -770,6 +782,81 @@ pub(super) struct FinalMakings { - resolving: bool, - } - -+/// [`FinalMakings`] by reference: what a kept pass is looked up by, hashed and compared as the -+/// makings it stands for, so a lookup that finds one copies nothing. -+#[derive(Clone, Copy, PartialEq, Eq, Hash)] -+struct FinalKey<'a> { -+ shape: &'a PassShape, -+ colors: &'a [Ops], -+ depth_stencil: Option, -+ resolves: &'a [Ops], -+ depth_stencil_resolve: Option, -+ resolving: bool, -+} -+ -+impl FinalKey<'_> { -+ fn to_makings(self) -> FinalMakings { -+ FinalMakings { -+ shape: self.shape.clone(), -+ colors: self.colors.to_vec(), -+ depth_stencil: self.depth_stencil, -+ resolves: self.resolves.to_vec(), -+ depth_stencil_resolve: self.depth_stencil_resolve, -+ resolving: self.resolving, -+ } -+ } -+} -+ -+/// What has a [`FinalKey`]: the kept makings, and a key looked up by. -+trait FinalKeyed { -+ fn key(&self) -> FinalKey<'_>; -+} -+ -+impl FinalKeyed for FinalMakings { -+ fn key(&self) -> FinalKey<'_> { -+ FinalKey { -+ shape: &self.shape, -+ colors: &self.colors, -+ depth_stencil: self.depth_stencil, -+ resolves: &self.resolves, -+ depth_stencil_resolve: self.depth_stencil_resolve, -+ resolving: self.resolving, -+ } -+ } -+} -+ -+impl FinalKeyed for FinalKey<'_> { -+ fn key(&self) -> FinalKey<'_> { -+ *self -+ } -+} -+ -+impl std::hash::Hash for FinalMakings { -+ fn hash(&self, state: &mut H) { -+ self.key().hash(state); -+ } -+} -+ -+impl std::hash::Hash for dyn FinalKeyed + '_ { -+ fn hash(&self, state: &mut H) { -+ self.key().hash(state); -+ } -+} -+ -+impl PartialEq for dyn FinalKeyed + '_ { -+ fn eq(&self, other: &Self) -> bool { -+ self.key() == other.key() -+ } -+} -+ -+impl Eq for dyn FinalKeyed + '_ {} -+ -+impl<'a> std::borrow::Borrow for FinalMakings { -+ fn borrow(&self) -> &(dyn FinalKeyed + 'a) { -+ self -+ } -+} -+ - /// The final passes a device keeps, by what they are made of ([`kept_final_pass`]). - #[derive(Default)] - pub(super) struct KeptFinal(HashMap); -@@ -790,11 +877,12 @@ impl KeptFinal { - /// before, it cost a `vkCreateRenderPass2` (where Turnip lays out its attachments in GMEM for - /// every tile layout) and a `vkDestroyRenderPass` each time a pass ended, 19 times a frame of - /// iOS's compositor. Past [`KEPT_FINALS`] a pass is made for its instance alone, as before. --fn kept_final_pass(device: &Device, makings: FinalMakings) -> Result { -+fn kept_final_pass(device: &Device, key: FinalKey<'_>) -> Result { - let kept = &device.shared().final_passes; -- if let Some(&raw) = lock(kept).0.get(&makings) { -+ if let Some(&raw) = lock(kept).0.get(&key as &dyn FinalKeyed) { - return Ok(RenderPassObject::kept(device, raw)); - } -+ let makings = key.to_makings(); - let pass = create( - device, - &makings.shape, diff --git a/bridge/patches/0034-batch-barriers-on-the-stack.patch b/bridge/patches/0034-batch-barriers-on-the-stack.patch deleted file mode 100644 index 7e2c7dd..0000000 --- a/bridge/patches/0034-batch-barriers-on-the-stack.patch +++ /dev/null @@ -1,49 +0,0 @@ -diff --git a/crates/metal2vulkan-runtime/src/command_buffer/submit.rs b/crates/metal2vulkan-runtime/src/command_buffer/submit.rs -index d90691f..8b18e14 100644 ---- a/crates/metal2vulkan-runtime/src/command_buffer/submit.rs -+++ b/crates/metal2vulkan-runtime/src/command_buffer/submit.rs -@@ -313,22 +313,24 @@ impl CommandBufferShared { - let mut lead = Some(Barrier::after_completed()); - let mut work = Vec::with_capacity(2 * (end - start) + 1); - if entry.is_some() { -- let globals: Vec = lead.take().into_iter().chain(entry_barrier).collect(); -+ let (pair, count) = barrier_pair(lead.take(), entry_barrier); -+ let globals = &pair[..count]; - let stamps = Stamps { - start: start_stamp.take(), - end: None, - }; -- work.push(self.record_barriers(&mut state, &globals, &entries, stamps)?); -+ work.push(self.record_barriers(&mut state, globals, &entries, stamps)?); - } - for (offset, barrier) in barriers.iter().enumerate() { -- let globals: Vec = lead.take().into_iter().chain(*barrier).collect(); -+ let (pair, count) = barrier_pair(lead.take(), *barrier); -+ let globals = &pair[..count]; - if !globals.is_empty() { - let stamps = Stamps { - start: start_stamp.take(), - end: None, - }; - let kept = &mut scheduler.kept; -- work.push(self.barrier_buffer(&mut state, kept, at, &globals, &[], stamps)?); -+ work.push(self.barrier_buffer(&mut state, kept, at, globals, &[], stamps)?); - } - work.extend(&state.recorded[start + offset].raws); - } -@@ -823,3 +825,15 @@ struct Stamps { - start: Option<(vk::QueryPool, u32)>, - end: Option<(vk::QueryPool, u32)>, - } -+ -+/// `lead` then `barrier`, those of them there are, as the first `.1` of the array: the globals -+/// of a barrier between a batch's encoders, which takes no allocation. -+fn barrier_pair(lead: Option, barrier: Option) -> ([Barrier; 2], usize) { -+ let mut pair = [Barrier::default(); 2]; -+ let mut count = 0; -+ for b in lead.into_iter().chain(barrier) { -+ pair[count] = b; -+ count += 1; -+ } -+ (pair, count) -+} diff --git a/bridge/patches/0035-reset-command-pools-as-given-back.patch b/bridge/patches/0035-reset-command-pools-as-given-back.patch deleted file mode 100644 index 248ab5e..0000000 --- a/bridge/patches/0035-reset-command-pools-as-given-back.patch +++ /dev/null @@ -1,133 +0,0 @@ -diff --git a/crates/metal2vulkan-runtime/src/command_buffer/mod.rs b/crates/metal2vulkan-runtime/src/command_buffer/mod.rs -index 8b3a0c2..0c8537a 100644 ---- a/crates/metal2vulkan-runtime/src/command_buffer/mod.rs -+++ b/crates/metal2vulkan-runtime/src/command_buffer/mod.rs -@@ -1136,7 +1136,7 @@ impl Drop for CommandBufferShared { - // state. - let pool = lock(&self.state).pool.take(); - if let Some(pool) = pool { -- self.device.shared().spares.give_command_pool(pool); -+ self.device.shared().spares.give_command_pool(self.device.shared(), pool); - } - } - } -@@ -2723,7 +2723,7 @@ impl CommandBufferShared { - // buffer that completed gives back its descriptor pools too, one that failed releases - // them here. - if let Some(pool) = state.pool.take() { -- self.device.shared().spares.give_command_pool(pool); -+ self.device.shared().spares.give_command_pool(self.device.shared(), pool); - } - if state.status == CommandBufferStatus::Completed { - state.descriptors.give_back(); -diff --git a/crates/metal2vulkan-runtime/src/device/prime.rs b/crates/metal2vulkan-runtime/src/device/prime.rs -index 50856aa..8bb81ba 100644 ---- a/crates/metal2vulkan-runtime/src/device/prime.rs -+++ b/crates/metal2vulkan-runtime/src/device/prime.rs -@@ -416,6 +416,9 @@ impl Primer<'_> { - let pool = unsafe { device.create_command_pool(&pool_info, None) }.map_err(vulkan)?; - self.made(Call::CreateCommandPool); - self.hold(pool); -+ // SAFETY: the pool was just made, and nothing of it is pending. -+ unsafe { device.reset_command_pool(pool, vk::CommandPoolResetFlags::empty()) }.map_err(vulkan)?; -+ self.made(Call::ResetCommandPool); - let allocate = vk::CommandBufferAllocateInfo::default() - .command_pool(pool) - .level(vk::CommandBufferLevel::PRIMARY) -diff --git a/crates/metal2vulkan-runtime/src/exit_guard.rs b/crates/metal2vulkan-runtime/src/exit_guard.rs -index 9f98943..110455f 100644 ---- a/crates/metal2vulkan-runtime/src/exit_guard.rs -+++ b/crates/metal2vulkan-runtime/src/exit_guard.rs -@@ -303,6 +303,8 @@ calls! { - // (`Device::drain_queues`). - QueueWaitIdle => c"vkQueueWaitIdle", - ResetFences => c"vkResetFences", -+ // A command pool given back reset (`spares.rs`), on the completion thread. -+ ResetCommandPool => c"vkResetCommandPool", - DestroyCommandPool => c"vkDestroyCommandPool", - DestroyFence => c"vkDestroyFence", - DestroySemaphore => c"vkDestroySemaphore", -@@ -470,7 +472,7 @@ pub(crate) const HOST_STEP: &[Call] = &[ - ]; - - impl Call { -- /// Its bit. The table fills 88 of the word's 128 (widened from 64): when it next outgrows the -+ /// Its bit. The table fills 90 of the word's 128 (widened from 64): when it next outgrows the - /// word, widen the word (here, [`CALLED`], `PENDING` and the priming's calls); do not add a - /// second table. - pub(crate) fn bit(self) -> u128 { -diff --git a/crates/metal2vulkan-runtime/src/spares.rs b/crates/metal2vulkan-runtime/src/spares.rs -index 39ac4d8..6480b4f 100644 ---- a/crates/metal2vulkan-runtime/src/spares.rs -+++ b/crates/metal2vulkan-runtime/src/spares.rs -@@ -20,7 +20,12 @@ - //! - **Descriptor pools are bounded.** At most [`DESCRIPTOR_POOLS`] descriptor pools of at most - //! [`DESCRIPTORS`] descriptors together; a descriptor pool given back past them releases the - //! oldest spares first, so the spares follow the shapes in use. --//! - **Reset by the taker**, on the thread that encodes: the program's own, or one running the -+//! - **Reset as given back**, inside a section on the thread that completed the command buffer -+//! (the completion thread's, or the dropping one's), so the encoding thread takes a pool ready -+//! to record: turnip's reset of a frame's command buffers cost ~30 us of the program's thread -+//! per commit on the host (m2vsim), about three times that in the guest. A pool whose reset -+//! there failed is released; one given back as the process exits stays unreset and is -+//! **reset by the taker**, on the thread that encodes: the program's own, or one running the - //! program's code (a handler). A section of the exit guard (a host step's recording on the - //! scheduler's thread) calls only the guard's table of entry points, which holds no reset, so a - //! command buffer or command recorded there makes its pools as before; what it made is given back -@@ -62,6 +67,8 @@ pub(super) struct CommandPool { - family: u32, - made: [Vec; 2], - handed: [usize; 2], -+ /// Reset since a command buffer last recorded into it (as it was given back). -+ reset: bool, - } - - impl CommandPool { -@@ -72,6 +79,7 @@ impl CommandPool { - family, - made: [Vec::new(), Vec::new()], - handed: [0, 0], -+ reset: false, - } - } - -@@ -127,6 +135,10 @@ impl Spares { - let Some(mut pool) = take_command_pool(&mut lock(&self.command_pools), family) else { - return Ok(None); - }; -+ if std::mem::take(&mut pool.reset) { -+ pool.reset_handed(); -+ return Ok(Some(pool)); -+ } - // SAFETY: the pool is the caller's alone now, and nothing of it is pending: a command - // buffer that completed gave it back. - let reset = unsafe { -@@ -143,9 +155,25 @@ impl Spares { - Ok(Some(pool)) - } - -- /// Keep `pool`, of a command buffer whose work ended or that was never submitted: nothing of -- /// it is pending, and nothing else holds it. Never released here (the module docs). -- pub(super) fn give_command_pool(&self, pool: CommandPool) { -+ /// Keep `pool` of `device`, of a command buffer whose work ended or that was never -+ /// submitted: nothing of it is pending, and nothing else holds it. Reset here, inside a -+ /// section; released only where that reset failed (the module docs). -+ pub(super) fn give_command_pool(&self, device: &DeviceShared, mut pool: CommandPool) { -+ if let Some(_busy) = exit_guard::enter() { -+ // SAFETY: nothing of the pool is pending, and nothing else holds it. -+ let reset = unsafe { -+ device -+ .vk() -+ .reset_command_pool(pool.raw, vk::CommandPoolResetFlags::empty()) -+ }; -+ exit_guard::called(exit_guard::Call::ResetCommandPool); -+ if reset.is_err() { -+ // SAFETY: as above. -+ unsafe { release::object(device.vk(), pool.raw) }; -+ return; -+ } -+ pool.reset = true; -+ } - keep_command_pool(&mut lock(&self.command_pools), pool); - } - diff --git a/bridge/patches/0036-calibrate-off-the-commit-path.patch b/bridge/patches/0036-calibrate-off-the-commit-path.patch deleted file mode 100644 index 4ae1c24..0000000 --- a/bridge/patches/0036-calibrate-off-the-commit-path.patch +++ /dev/null @@ -1,50 +0,0 @@ -diff --git a/crates/metal2vulkan-runtime/src/command_buffer/completion.rs b/crates/metal2vulkan-runtime/src/command_buffer/completion.rs -index af866a3..38bb81b 100644 ---- a/crates/metal2vulkan-runtime/src/command_buffer/completion.rs -+++ b/crates/metal2vulkan-runtime/src/command_buffer/completion.rs -@@ -292,6 +292,9 @@ fn run(watch: &Watch) { - buffer.read_stamps(); - buffer.place_gpu_times(); - } -+ // The calibration pair the next commits take, refreshed here when old rather than at -+ // a commit (`Timing::kept_pair`). -+ device.shared().timing.pair(); - } - drop(device); - match settled { -diff --git a/crates/metal2vulkan-runtime/src/command_buffer/mod.rs b/crates/metal2vulkan-runtime/src/command_buffer/mod.rs -index 0c8537a..6e92f2b 100644 ---- a/crates/metal2vulkan-runtime/src/command_buffer/mod.rs -+++ b/crates/metal2vulkan-runtime/src/command_buffer/mod.rs -@@ -2401,7 +2401,7 @@ impl CommandBufferShared { - Some(slot) => { - shared.counts.timestamp_slot_taken(); - state.times.slot = Some(slot); -- state.times.pair = timing.pair(); -+ state.times.pair = timing.kept_pair(); - } - None => state.times.stamped = Stamped::NoSlot, - } -diff --git a/crates/metal2vulkan-runtime/src/gpu_time.rs b/crates/metal2vulkan-runtime/src/gpu_time.rs -index f265af0..8b09e6e 100644 ---- a/crates/metal2vulkan-runtime/src/gpu_time.rs -+++ b/crates/metal2vulkan-runtime/src/gpu_time.rs -@@ -610,6 +610,18 @@ impl Timing { - self.from_host - } - -+ /// The device's latest calibration pair, however old, taken now only where there is none: a -+ /// commit's, on the program's thread. A reading is a round trip to the GPU's driver (on -+ /// DroidVM a request to the host, from WindowServer's main thread every 100 ms); the -+ /// completion thread refreshes the pair instead ([`pair`](Self::pair)), so the one a commit -+ /// takes is at most [`REFRESH`] and a command buffer's completion old. -+ pub(crate) fn kept_pair(&self) -> Option { -+ if let Some((_, pair)) = *lock(&self.latest) { -+ return Some(pair); -+ } -+ self.pair() -+ } -+ - /// The device's latest calibration pair, taken now when the last is [`REFRESH`] old or - /// there is none; `None` where the device has no calibration or the reading fails. - pub(crate) fn pair(&self) -> Option { diff --git a/bridge/patches/0037-key-hash-the-draw-path-maps.patch b/bridge/patches/0037-key-hash-the-draw-path-maps.patch deleted file mode 100644 index f756564..0000000 --- a/bridge/patches/0037-key-hash-the-draw-path-maps.patch +++ /dev/null @@ -1,278 +0,0 @@ -diff --git a/crates/metal2vulkan-runtime/src/binding.rs b/crates/metal2vulkan-runtime/src/binding.rs -index a916162..9d9eeb8 100644 ---- a/crates/metal2vulkan-runtime/src/binding.rs -+++ b/crates/metal2vulkan-runtime/src/binding.rs -@@ -51,6 +51,7 @@ use super::command_buffer::{NotedTable, State}; - use super::error::{internal, Error, RuntimeGap}; - use super::function_table::{self, TableSnapshot, VisibleFunctionTable}; - use super::heap_sets::{Keeper, TextureTable, HEAP_SET}; -+use super::key_hash::KeyMap; - use super::render_pass::{PassLayout, Target}; - use super::render_target; - use super::residency::{Residency, Used}; -@@ -69,7 +70,7 @@ use metal2vulkan::bind::{ - }; - use metal2vulkan::metal::sampler::OpaqueBlackBorder; - use metal2vulkan::pipeline::{HandleClass, SlotClass, SlotRef, Stage, StorageAttachmentView}; --use std::collections::{BTreeSet, HashMap}; -+use std::collections::BTreeSet; - use std::ops::Deref; - use std::sync::atomic::Ordering; - use std::sync::Arc; -@@ -899,7 +900,7 @@ const KEPT_VIEWS: usize = 256; - pub(super) struct BindMemo { - /// The views epoch the views were kept under. - epoch: u64, -- views: HashMap, -+ views: KeyMap, - samplers: Vec, - /// The vectors of the last bind's [`Bound`] its consumer gave back ([`BindMemo::give_back`]) - /// and its root table addresses, emptied: the next bind fills them. -diff --git a/crates/metal2vulkan-runtime/src/neutral.rs b/crates/metal2vulkan-runtime/src/neutral.rs -index 4cf4a44..def66ee 100644 ---- a/crates/metal2vulkan-runtime/src/neutral.rs -+++ b/crates/metal2vulkan-runtime/src/neutral.rs -@@ -26,6 +26,7 @@ - //! work samples, its bind waited for that work on NVIDIA's driver (`memory.rs`'s `Fit`). - - use super::device::lock; -+use super::key_hash::KeyMap; - use super::error::{vulkan, Error}; - use super::memory::{Memory, MemoryClass, Placement}; - use super::resource::ResourceKey; -@@ -85,9 +86,9 @@ impl Filled for NeutralTexels { - /// A device's neutral images, texel buffers and samplers, made at first use. - #[derive(Default)] - pub(super) struct Neutrals { -- images: HashMap>, -+ images: KeyMap>, - /// By format and whether the view is a storage texel buffer's. -- texels: HashMap<(Format, bool), Arc>, -+ texels: KeyMap<(Format, bool), Arc>, - /// By whether depth comparison is enabled. - samplers: HashMap, - } -diff --git a/crates/metal2vulkan-runtime/src/pipeline/memo.rs b/crates/metal2vulkan-runtime/src/pipeline/memo.rs -index c2054d9..76187df 100644 ---- a/crates/metal2vulkan-runtime/src/pipeline/memo.rs -+++ b/crates/metal2vulkan-runtime/src/pipeline/memo.rs -@@ -19,10 +19,11 @@ - use super::render::RenderVariant; - use super::Variant; - use crate::device::lock; -+use crate::key_hash::KeyMap; - use metal2vulkan::cache::PipelineKey; - use metal2vulkan::library::{CompiledFunction, FunctionCompileKey}; - use metal2vulkan::pipeline::{ComputePipeline, RenderPipeline}; --use std::collections::{BTreeMap, HashMap}; -+use std::collections::BTreeMap; - use std::hash::Hash; - use std::sync::{Arc, Mutex, Weak}; - -@@ -33,7 +34,7 @@ pub(crate) const MEMO_ENTRIES: usize = 4096; - /// entries, removing the least recently used past either. A lookup or an insert is a use. - pub(crate) struct Lru { - /// Each entry's number: the stamp of the insert that kept it. -- numbers: HashMap, -+ numbers: KeyMap, - /// The entries, by number. - entries: BTreeMap>, - /// Each entry's number by its last use, oldest first. -@@ -61,7 +62,7 @@ impl Lru { - /// An empty map bounded by `max_weight` and `max_entries`. - pub(crate) fn new(max_weight: u64, max_entries: usize) -> Lru { - Lru { -- numbers: HashMap::new(), -+ numbers: KeyMap::default(), - entries: BTreeMap::new(), - order: BTreeMap::new(), - clock: 0, -diff --git a/crates/metal2vulkan-runtime/src/render/pass_start_reads.rs b/crates/metal2vulkan-runtime/src/render/pass_start_reads.rs -index 04982ae..40c1471 100644 ---- a/crates/metal2vulkan-runtime/src/render/pass_start_reads.rs -+++ b/crates/metal2vulkan-runtime/src/render/pass_start_reads.rs -@@ -54,7 +54,7 @@ use crate::values::{CpuCacheMode, HazardTrackingMode, ResourceOptions, StorageMo - use crate::Device; - use ash::vk; - use metal2vulkan::pipeline::{PipelineInterface, SlotClass, SlotInterface, Stage}; --use std::collections::HashMap; -+use crate::key_hash::KeyMap; - use std::ops::Range; - - /// The byte ranges one key holds before they are merged into their hull. -@@ -74,7 +74,7 @@ const MESH_STAGES: vk::PipelineStageFlags2 = vk::PipelineStageFlags2::from_raw( - - /// Byte ranges of buffers, tracked or not, by key. - #[derive(Clone, Default)] --struct Written(HashMap>>); -+struct Written(KeyMap>>); - - impl Written { - fn add(&mut self, key: ResourceKey, range: &Range) { -@@ -89,7 +89,9 @@ impl Written { - } - - fn overlaps(&self, key: ResourceKey, range: &Range) -> bool { -- self.0.get(&key).is_some_and(|ranges| { -+ // Most passes' fragment stages write no buffer: no key to hash. -+ !self.0.is_empty() -+ && self.0.get(&key).is_some_and(|ranges| { - ranges - .iter() - .any(|r| r.start < range.end && range.start < r.end) -diff --git a/crates/metal2vulkan-runtime/src/render_target.rs b/crates/metal2vulkan-runtime/src/render_target.rs -index 63a28ae..5f7b8fa 100644 ---- a/crates/metal2vulkan-runtime/src/render_target.rs -+++ b/crates/metal2vulkan-runtime/src/render_target.rs -@@ -107,6 +107,7 @@ - - use super::attachment_stand_in::Cause; - use super::attachmentless; -+use super::key_hash::KeyMap; - use super::binding_view::VkView; - use super::device::lock; - use super::error::{internal, Error}; -@@ -124,7 +125,6 @@ use crate::release; - use ash::vk; - use metal2vulkan::bind::ViewSetup; - use metal2vulkan::device::{DeviceProfile, DriverFact}; --use std::collections::HashMap; - use std::sync::Arc; - - /// What makes two passes compatible: each color index's format (`None` without a texture), the -@@ -652,7 +652,7 @@ const KEPT_SHAPES: usize = 64; - - /// The compatible passes a device keeps, by shape ([`shared_compatible_pass`]). - #[derive(Default)] --pub(super) struct KeptCompatible(HashMap); -+pub(super) struct KeptCompatible(KeyMap); - - impl KeptCompatible { - /// Destroy every kept pass; the device is idle, and is destroyed next. -@@ -859,7 +859,7 @@ impl<'a> std::borrow::Borrow for FinalMakings { - - /// The final passes a device keeps, by what they are made of ([`kept_final_pass`]). - #[derive(Default)] --pub(super) struct KeptFinal(HashMap); -+pub(super) struct KeptFinal(KeyMap); - - impl KeptFinal { - /// Destroy every kept pass; the device is idle, and is destroyed next. -diff --git a/crates/metal2vulkan-runtime/src/texture.rs b/crates/metal2vulkan-runtime/src/texture.rs -index 071beb8..46097c9 100644 ---- a/crates/metal2vulkan-runtime/src/texture.rs -+++ b/crates/metal2vulkan-runtime/src/texture.rs -@@ -57,6 +57,7 @@ - //! blits of textures over buffers, are refused `NotImplemented`. - - use super::address::{ObjectsToken, RangeToken}; -+use super::key_hash::KeyMap; - use super::address_map::AddressModeMap; - use super::attachment_stand_in::KeptBacking; - use super::attributes::PurgeableState; -@@ -452,7 +453,7 @@ struct Image { - identity: Identity, - /// The `VkImageView`s bindings of the image's textures needed (`binding_view.rs`), made at - /// their first bind. A finite set per image: its view types, formats and level ranges. -- views: Mutex>, -+ views: Mutex>, - /// The image's depth twin: a `D32_SFLOAT` image of the same texels, which the views a - /// comparison reads present, for an `R32_SFLOAT` image holding a `Depth32Float` texture. - twin: Option>, -@@ -1642,7 +1643,7 @@ impl Device { - options: ResourceOptions::new(StorageMode::Private, cache, hazard), - allow_gpu_optimized_contents: allow, - identity: Identity::new(hazard, false), -- views: Mutex::new(HashMap::new()), -+ views: Mutex::new(KeyMap::default()), - twin: None, - decoded: None, - form: Default::default(), -@@ -1765,7 +1766,7 @@ impl Device { - options: ResourceOptions::new(StorageMode::Private, cache, hazard), - allow_gpu_optimized_contents, - identity: Identity::new(hazard, false), -- views: Mutex::new(HashMap::new()), -+ views: Mutex::new(KeyMap::default()), - twin: None, - decoded: None, - form: Default::default(), -@@ -2331,7 +2332,7 @@ impl Device { - tracked: texels.tracked(), - ..texels.buffer.identity() - }, -- views: Mutex::new(HashMap::new()), -+ views: Mutex::new(KeyMap::default()), - twin: None, - decoded: None, - form: Default::default(), -@@ -2419,7 +2420,7 @@ impl Device { - tracked: texels.tracked(), - ..texels.buffer.identity() - }, -- views: Mutex::new(HashMap::new()), -+ views: Mutex::new(KeyMap::default()), - twin: None, - decoded: None, - form: Default::default(), -@@ -2555,7 +2556,7 @@ impl Device { - options, - allow_gpu_optimized_contents, - identity, -- views: Mutex::new(HashMap::new()), -+ views: Mutex::new(KeyMap::default()), - twin: None, - decoded: None, - form: Default::default(), -@@ -3383,7 +3384,7 @@ impl Device { - options: descriptor.resource_options, - allow_gpu_optimized_contents: descriptor.allow_gpu_optimized_contents, - identity, -- views: Mutex::new(HashMap::new()), -+ views: Mutex::new(KeyMap::default()), - twin: None, - decoded: None, - form: Default::default(), -diff --git a/crates/metal2vulkan-runtime/src/texture/heap.rs b/crates/metal2vulkan-runtime/src/texture/heap.rs -index 1210427..096ded4 100644 ---- a/crates/metal2vulkan-runtime/src/texture/heap.rs -+++ b/crates/metal2vulkan-runtime/src/texture/heap.rs -@@ -179,7 +179,6 @@ use metal2vulkan::metal::{PixelFormat, TextureType, TextureUsage}; - use metal2vulkan::vk::{ - Format, FormatFeatureFlags2, ImageAspectFlags, ImageUsageFlags, SampleCountFlags, - }; --use std::collections::HashMap; - use std::ptr::NonNull; - use std::sync::atomic::{AtomicBool, Ordering}; - use std::sync::{Arc, Mutex}; -@@ -2089,7 +2088,7 @@ impl Device { - options: binding.options, - allow_gpu_optimized_contents: binding.allow_gpu_optimized_contents, - identity: binding.identity, -- views: Mutex::new(HashMap::new()), -+ views: Mutex::new(Default::default()), - twin: None, - decoded: None, - form: Default::default(), -@@ -2125,7 +2124,7 @@ impl Device { - options: binding.options, - allow_gpu_optimized_contents: binding.allow_gpu_optimized_contents, - identity: binding.identity, -- views: Mutex::new(HashMap::new()), -+ views: Mutex::new(Default::default()), - twin: None, - decoded: None, - form: Default::default(), -diff --git a/crates/metal2vulkan-runtime/src/texture/wrapped.rs b/crates/metal2vulkan-runtime/src/texture/wrapped.rs -index fcd1c46..211d0fe 100644 ---- a/crates/metal2vulkan-runtime/src/texture/wrapped.rs -+++ b/crates/metal2vulkan-runtime/src/texture/wrapped.rs -@@ -611,7 +611,7 @@ impl Device { - options, - allow_gpu_optimized_contents: descriptor.allow_gpu_optimized_contents, - identity, -- views: Mutex::new(HashMap::new()), -+ views: Mutex::new(Default::default()), - twin: None, - decoded: None, - form: Default::default(), diff --git a/bridge/prime_host.mm b/bridge/prime_host.mm new file mode 100644 index 0000000..792793a --- /dev/null +++ b/bridge/prime_host.mm @@ -0,0 +1,75 @@ +// SPDX-License-Identifier: MIT +// Shader priming ahead of the tablet: the plugin's own priming (PrimeShaders.h) over the OS image's libraries on a local mount, through the +// frontend and the runtime over the tablet's driver (Turnip for the Adreno 740, its kernel answered by the host copy of the kernel stack, +// tests/hostsim). Turnip compiles for the GPU without running anything on it, so the cache directory this fills is what the render server's +// own priming would have written; scripts/make-pipeline-cache.py packs it into the image. Each finished library is listed in prime-done under +// the tablet's id for it (its path there, size and mtime, the runtime's build and the tablet Turnip's LC_UUID), so the render server does not +// prime it again. +// argv: loader (host Turnip), cache directory, library list (tablet paths), OS root, OS cryptex root, the tablet's Turnip dylib, [first, count] +#include "../PrimeShaders.h" +#include "../NativeBridgeMetalFrontend.h" +#include "NativeMetalObjects.h" +#include + +static void onMissing(const char *, const char *) {} + +// The LC_UUID of the Mach-O file at `path` (thin), "unknown" without one: the tablet's Turnip, which ends the tablet's prime-done ids. +static std::string fileUuid(const char *path) { + std::ifstream in(path, std::ios::binary); + std::vector bytes((std::istreambuf_iterator(in)), std::istreambuf_iterator()); + if (bytes.size() < sizeof(mach_header_64)) return "unknown"; + const auto *header = reinterpret_cast(bytes.data()); + if (header->magic != MH_MAGIC_64) return "unknown"; + size_t at = sizeof(mach_header_64); + for (uint32_t i = 0; i < header->ncmds && at + sizeof(load_command) <= bytes.size(); ++i) { + const auto *command = reinterpret_cast(bytes.data() + at); + if (command->cmd == LC_UUID && at + sizeof(uuid_command) <= bytes.size()) { + const uint8_t *uuid = reinterpret_cast(command)->uuid; + char text[33]; + for (int k = 0; k < 16; ++k) snprintf(text + 2 * k, 3, "%02x", uuid[k]); + return text; + } + at += command->cmdsize; + } + return "unknown"; +} + +int main(int argc, char **argv) { + @autoreleasepool { + if (argc != 7 && argc != 9) return 2; + id device = native_bridge_metal_open(argv[1], argv[2], 0x6e6d70, onMissing); + if (!device) {std::printf("FAIL device\n"); return 1;} + native_bridge_metal_quiet_problems(true); + const std::string root = argv[2], osRoot = argv[4], cryptexRoot = argv[5], cryptex = "/System/Cryptexes/OS"; + const std::string build = std::string(prime::nmb_runtime_build()) + "-" + fileUuid(argv[6]); + std::vector paths; + {std::ifstream list(argv[3]); for (std::string line; std::getline(list, line);) if (!line.empty() && line[0] == '/') paths.push_back(line);} + size_t first = 0, count = paths.size(); + if (argc == 9) {first = strtoul(argv[7], nullptr, 10); count = strtoul(argv[8], nullptr, 10);} + const std::set bad = prime::readLines(root + "/prime-bad"); + const std::string current = root + "/prime-current", log = root + "/prime.log", done = root + "/prime-done"; + unsigned primed = 0, absent = 0; + for (size_t i = first; i < paths.size() && i < first + count; ++i) { + const std::string &path = paths[i]; + const std::string local = path.compare(0, cryptex.size(), cryptex) == 0 ? cryptexRoot + path.substr(cryptex.size()) : osRoot + path; + struct stat st = {}; + if (stat(local.c_str(), &st) != 0) {++absent; prime::appendLine(log, path + " absent from the image mount"); continue;} + prime::appendLine(log, prime::library(device, local, current, bad)); + prime::appendLine(done, path + "\t" + std::to_string((long long)st.st_size) + "\t" + + std::to_string((long long)st.st_mtimespec.tv_sec) + "\t" + build); + ++primed; + } + // The cache thread writes the entries after the calls returned: wait until its write count has stood still for 10 s. + NmbShaderCacheStats stats = {}; + uint64_t last = UINT64_MAX; + for (unsigned still = 0; still < 20; usleep(500000)) { + native_bridge_metal_shader_cache_stats(&stats); + const uint64_t now = stats.writes + stats.driver_saves; + still = now == last ? still + 1 : 0; + last = now; + } + std::printf("PASS: primed %u libraries (%u absent) made=%u writes=%llu driver_saves=%llu build=%s\n", primed, absent, + (unsigned)native_prime_made, (unsigned long long)stats.writes, (unsigned long long)stats.driver_saves, build.c_str()); + return 0; + } +} From 0f6050fbb984c07ef9397db2bde4231b62f1f57b Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Thu, 29 Oct 2026 13:16:00 +0800 Subject: [PATCH 66/92] 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. --- CacheExport.h | 60 ------- CacheImport.h | 153 ++++++++++++++++++ NativeAdrenoMetalPlugin.mm | 45 +++++- NativeBridgeMetalFrontend.mm | 8 + PrimeShaders.h | 4 + bridge/NativeMetalObjects.h | 3 + bridge/NativeMetalObjects.rs | 32 ++++ .../0017-shader-cache-fallback-source.patch | 68 ++++++++ bridge/prime_host.mm | 14 +- 9 files changed, 318 insertions(+), 69 deletions(-) create mode 100644 CacheImport.h create mode 100644 bridge/patches/0017-shader-cache-fallback-source.patch diff --git a/CacheExport.h b/CacheExport.h index a21a01d..d3b92de 100644 --- a/CacheExport.h +++ b/CacheExport.h @@ -265,66 +265,6 @@ static int importStream(PackReader &r, const std::string &directory) { } #ifndef CACHEEXPORT_FORMAT_ONLY -// Import region: the pipeline caches compiled ahead on the host (scripts/make-pipeline-cache.py), written to the read-only part of the -// userdata reserve (UniXNU-sheng scripts/make-pipeline-import.py) and read through the GPU driver (method 0x7101, 4096 bytes a call). -// Block 0: "SHNGIMP1", u32 version 1, u32 0, u64 pack bytes, 32 bytes id (the pack's SHA-256); the pack (SHEXP001) from block 1. Its files -// go into this process's cache directory as importPack's do; import-id there then names the pack, and a later boot that finds the same id -// reads nothing more. Markers 0x63nn: 00 started, 01 imported (then 0x64nn: files written in thousands, saturated), 02 already imported, -// f0 no connection, f1 no selector, f2 no region (or empty), f3 the pack was refused. -constexpr uint32_t kImportMethod = 0x7101, kImportBlock = 4096; -static int importRegion(Gpu &marker, std::string root) { - while (root.size() > 1 && root.back() == '/') root.pop_back(); - marker.mark(0x6300); - Gpu link; - link.call = marker.call; link.openFn = marker.openFn; link.closeFn = marker.closeFn; link.port = marker.port; - if (!link.openFn || link.openFn(link.port, mach_task_self(), 0x4148, &link.conn) != 0 || !link.conn) {marker.mark(0x63f0); return -1;} - struct Closer {Gpu &g; ~Closer() {if (g.closeFn && g.conn) g.closeFn(g.conn);}} closer{link}; - if (!link.probe()) {marker.mark(0x63f1); return -1;} - uint64_t region = 0; - std::vector block(kImportBlock); - auto readAt = [&](uint64_t offset, void *out) {return link.method(kImportMethod, &offset, sizeof offset, out, kImportBlock) == 0;}; - if (link.method(kImportMethod, ®ion, sizeof region, ®ion, sizeof region) != 0 || region < 2 * kImportBlock || - !readAt(0, block.data()) || memcmp(block.data(), "SHNGIMP1", 8)) {marker.mark(0x63f2); return 0;} - uint32_t version = 0; - uint64_t bytes = 0; - memcpy(&version, block.data() + 8, 4); - memcpy(&bytes, block.data() + 16, 8); - if (version != 1 || bytes < 16 || bytes > region - kImportBlock) {marker.mark(0x63f2); return 0;} - char id[65]; - for (int k = 0; k < 32; ++k) snprintf(id + 2 * k, 3, "%02x", block[24 + k]); - const std::string idPath = root + "/import-id"; - if (FILE *f = fopen(idPath.c_str(), "r")) { - char seen[65] = {}; - const bool same = fread(seen, 1, 64, f) == 64 && !memcmp(seen, id, 64); - fclose(f); - if (same) {marker.mark(0x6302); return 0;} - } - // The pack's bytes in order, a block per call from block 1. - uint64_t next = kImportBlock; - size_t used = kImportBlock; - auto read = [&](void *out, size_t n) { - uint8_t *to = static_cast(out); - while (n) { - if (used == kImportBlock) { - if (!readAt(next, block.data())) return false; - next += kImportBlock; - used = 0; - } - const size_t k = n < kImportBlock - used ? n : kImportBlock - used; - memcpy(to, block.data() + used, k); - used += k; to += k; n -= k; - } - return true; - }; - PackReader r; - const int written = r.startWith(read, bytes) ? importStream(r, root) : -1; - if (written < 0) {marker.mark(0x63f3); return -1;} - if (FILE *f = fopen(idPath.c_str(), "w")) {fwrite(id, 1, 64, f); fclose(f);} - marker.mark(0x6301); - marker.mark(0x6400 | (uint32_t)(written / 1000 > 0xff ? 0xff : written / 1000)); - return written; -} - // Markers 0x69xx: 00 started, 01 connection, 02 an export went out (then 0x6Bnn: its size in MiB, saturated), f0 no connection, // f1 no selector, f2 a file changed or vanished while it was read, f3 a piece refused, f4 the new files exceed one export. static void run(Gpu &marker, std::string root) { diff --git a/CacheImport.h b/CacheImport.h new file mode 100644 index 0000000..6356547 --- /dev/null +++ b/CacheImport.h @@ -0,0 +1,153 @@ +// SPDX-License-Identifier: MIT +// The pipeline caches compiled ahead on the host (UniXNU scripts/make-pipeline-cache.py), read on demand from the userdata import region +// (UniXNU-sheng scripts/make-pipeline-import.py) through the GPU driver's method 0x7101, which hands out 4096-byte blocks. The runtime asks +// for an entry only when its cache directory lacks it (nmb_set_shader_cache_fallback); nothing is copied into the directory, which lives on +// the RAM Data disk (int 590aaee2: unpacking the whole set there took 850 MB and starved the GPU of memory). What the render server +// compiles itself still goes to the directory. prime-done is read the same way, so priming skips every library the region covers. +// Layout: block 0 "SHNGIMP2", u32 version 2, u32 entry count, u64 index bytes, u64 data bytes, the SHA-256 of the index; the index +// from block 1 (per entry u64 data offset, u32 stored bytes, u32 bytes, u16 path bytes, the path; sorted by path); the data from the block +// after it, each entry zlib-compressed on its own. The index is checked against its digest once and kept in memory (a few MB); each entry's zlib stream checks its own bytes. +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "NativeAdrenoUserTest.h" + +namespace cacheimport { +constexpr uint32_t kMethod = 0x7101, kBlock = 4096; +// Read counters for the telemetry: entries served, bytes served (stored), misses, failed reads; and the first path the region lacked. +extern "C" {volatile uint64_t native_cache_import_stats[4]; char native_cache_import_first_miss[160];} + +struct Entry {uint64_t offset; uint32_t stored, bytes; std::string path;}; +static Gpu gLink; +static std::vector gEntries; +static uint64_t gDataFirst; // byte offset of the data in the region +static pthread_mutex_t gLock = PTHREAD_MUTEX_INITIALIZER; +// The last data block read: entries lie next to each other in path order, which is often the order they are asked in. +static uint64_t gCachedBlock = UINT64_MAX; +static uint8_t gCached[kBlock]; +// The region's size and its 4096-byte blocks: through the GPU driver on the tablet; a host check reads an image file (openFile). +static bool (*gRegionBlock)(uint64_t offset, uint8_t *out) = [](uint64_t offset, uint8_t *out) { + return gLink.method(kMethod, &offset, sizeof offset, out, kBlock) == 0; +}; +static FILE *gFile; + +static bool readBlock(uint64_t offset, uint8_t *out) { + if (offset == gCachedBlock) {memcpy(out, gCached, kBlock); return true;} + if (!gRegionBlock(offset, out)) return false; + gCachedBlock = offset; + memcpy(gCached, out, kBlock); + return true; +} +// `bytes` bytes of the region from `at` (any alignment) into out. +static bool readBytes(uint64_t at, size_t bytes, uint8_t *out) { + uint8_t block[kBlock]; + while (bytes) { + const uint64_t base = at & ~uint64_t(kBlock - 1); + const size_t skip = (size_t)(at - base), take = std::min(bytes, kBlock - skip); + if (!readBlock(base, block)) return false; + memcpy(out, block + skip, take); + out += take; at += take; bytes -= take; + } + return true; +} + +// The entry at `path` (relative to the cache directory), inflated into a malloc'd buffer the caller frees. +static bool read(const char *path, uint8_t **out, size_t *length) { + if (!path || !out || !length) return false; + const auto found = std::lower_bound(gEntries.begin(), gEntries.end(), path, [](const Entry &e, const char *p) {return e.path < p;}); + if (found == gEntries.end() || found->path != path) { + if (!native_cache_import_stats[2]++) strlcpy(native_cache_import_first_miss, path, sizeof native_cache_import_first_miss); + return false; + } + std::vector stored(found->stored); + pthread_mutex_lock(&gLock); + const bool got = readBytes(gDataFirst + found->offset, found->stored, stored.data()); + pthread_mutex_unlock(&gLock); + // zlib through dlopen, as CacheExport.h takes it: the plugin links no libz. + using UncompressFn = int (*)(Bytef *, uLongf *, const Bytef *, uLong); + static const UncompressFn inflateAll = [] { + void *z = dlopen("/usr/lib/libz.1.dylib", RTLD_NOW | RTLD_LOCAL); + return z ? (UncompressFn)dlsym(z, "uncompress") : nullptr; + }(); + uint8_t *data = static_cast(malloc(found->bytes ? found->bytes : 1)); + uLongf made = found->bytes; + if (!got || !data || !inflateAll || inflateAll(data, &made, stored.data(), found->stored) != Z_OK || made != found->bytes) { + free(data); + native_cache_import_stats[3] = native_cache_import_stats[3] + 1; + return false; + } + *out = data; + *length = made; + native_cache_import_stats[0] = native_cache_import_stats[0] + 1; + native_cache_import_stats[1] = native_cache_import_stats[1] + found->stored; + return true; +} + +static bool readIndex(uint64_t region, Gpu *marker); +// The host check's region: the payload of an import image (make-pipeline-import.py) written raw at `path`. +[[maybe_unused]] static bool openFile(const char *path) { + if (!(gFile = fopen(path, "rb"))) return false; + gRegionBlock = [](uint64_t offset, uint8_t *out) {return fseeko(gFile, (off_t)offset, SEEK_SET) == 0 && fread(out, 1, kBlock, gFile) == kBlock;}; + fseeko(gFile, 0, SEEK_END); + return readIndex((uint64_t)ftello(gFile), nullptr); +} + +// Opens the region and reads its index; false (nothing installed) without a region or with one that does not check. +// Markers 0x63nn: 00 started, 01 index ready (then 0x64nn: entries in thousands, saturated), f0 no connection, f1 no selector, +// f2 no region (or not this layout), f3 the index does not match its digest. +static bool open(Gpu &marker) { + marker.mark(0x6300); + gLink.call = marker.call; gLink.openFn = marker.openFn; gLink.closeFn = marker.closeFn; gLink.port = marker.port; + if (!gLink.openFn || gLink.openFn(gLink.port, mach_task_self(), 0x4148, &gLink.conn) != 0 || !gLink.conn) {marker.mark(0x63f0); return false;} + if (!gLink.probe()) {marker.mark(0x63f1); return false;} + uint64_t region = 0; + if (gLink.method(kMethod, ®ion, sizeof region, ®ion, sizeof region) != 0) {marker.mark(0x63f2); return false;} + return readIndex(region, &marker); +} +static bool readIndex(uint64_t region, Gpu *gpu) { + auto mark = [gpu](uint32_t value) {if (gpu) gpu->mark(value);}; + uint8_t block[kBlock]; + if (region < 2 * kBlock || !readBlock(0, block) || memcmp(block, "SHNGIMP2", 8)) {mark(0x63f2); return false;} + uint32_t version = 0, count = 0; + uint64_t indexBytes = 0, dataBytes = 0; + memcpy(&version, block + 8, 4); memcpy(&count, block + 12, 4); memcpy(&indexBytes, block + 16, 8); memcpy(&dataBytes, block + 24, 8); + if (version != 2 || !count || indexBytes % kBlock || kBlock + indexBytes + dataBytes > region) {mark(0x63f2); return false;} + uint8_t digest[32]; + memcpy(digest, block + 32, 32); + // The index, checked against its digest; an entry's own zlib stream checks its bytes when it is read. + std::vector index(indexBytes); + if (!readBytes(kBlock, indexBytes, index.data())) {mark(0x63f2); return false;} + CC_SHA256_CTX sha; + CC_SHA256_Init(&sha); + CC_SHA256_Update(&sha, index.data(), (CC_LONG)index.size()); + uint8_t got[32]; + CC_SHA256_Final(got, &sha); + if (memcmp(got, digest, 32)) {mark(0x63f3); return false;} + gEntries.reserve(count); + size_t at = 0; + for (uint32_t i = 0; i < count; ++i) { + if (at + 18 > index.size()) {mark(0x63f3); gEntries.clear(); return false;} + Entry e; + uint16_t pathBytes = 0; + memcpy(&e.offset, &index[at], 8); memcpy(&e.stored, &index[at + 8], 4); memcpy(&e.bytes, &index[at + 12], 4); + memcpy(&pathBytes, &index[at + 16], 2); + at += 18; + if (at + pathBytes > index.size() || e.offset + e.stored > dataBytes) {mark(0x63f3); gEntries.clear(); return false;} + e.path.assign(reinterpret_cast(&index[at]), pathBytes); + at += pathBytes; + gEntries.push_back(std::move(e)); + } + gDataFirst = kBlock + indexBytes; + mark(0x6301); + mark(0x6400 | (count / 1000 > 0xff ? 0xff : count / 1000)); + return true; +} +} // namespace cacheimport diff --git a/NativeAdrenoMetalPlugin.mm b/NativeAdrenoMetalPlugin.mm index 9f2fb85..c4bb32b 100644 --- a/NativeAdrenoMetalPlugin.mm +++ b/NativeAdrenoMetalPlugin.mm @@ -15,12 +15,14 @@ #endif #include #include +#include #include #include #include "NativeAdrenoUserTest.h" #include "NativeMetalBridgePhase.h" #include "CacheExport.h" #include "PrimeShaders.h" +#include "CacheImport.h" #include "ExceptionDiag.h" @interface _MTLDevice : NSObject @@ -147,6 +149,16 @@ static void reportDisplayState(Gpu &gpu) { // display hand-off, telemetry, benchmark or cache export). Its shader cache lives in its own cache or temp directory. // The reason a client's device failed, as a text line the driver keeps per process (the render server's report row is rewritten every ten seconds): // " ", through the marker stream (a connection is not needed). The Gpu object is made only on this path. +// Deletes every "*.import" file under `root`: the copying import of an earlier image wrote each file beside its place and renamed it only +// once the whole pack was read; a boot that ended first left them all there. +static void removeHalfImported(const char *root) { + char *paths[] = {const_cast(root), nullptr}; + FTS *fts = fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, nullptr); + if (!fts) return; + while (FTSENT *e = fts_read(fts)) + if (e->fts_info == FTS_F && e->fts_namelen > 7 && !strcmp(e->fts_name + e->fts_namelen - 7, ".import")) unlink(e->fts_path); + fts_close(fts); +} static unsigned gClientPort; static void clientFailure(unsigned port, const char *step, const char *detail) { if (Gpu *gpu = makeGpu(port)) { @@ -315,6 +327,30 @@ static id deviceForPort(unsigned port) { strlcat(candidates[i], "m2v", sizeof candidates[i]); mkdir(candidates[i], 0700); if (access(candidates[i], W_OK) != 0) continue; + // The pipeline caches compiled ahead on the host, read on demand from the userdata import region behind this directory + // (CacheImport.h), and the files an earlier image's copying import left half written there (int 590aaee2), which hold memory + // of the RAM Data disk. + { + static bool once; + if (!once) { + once = true; + removeHalfImported(candidates[i]); + if (cacheimport::open(*gpu)) { + nmb_set_shader_cache_fallback(cacheimport::read); + uint8_t *done = nullptr; + size_t length = 0; + if (cacheimport::read("prime-done", &done, &length)) { + const std::string text(reinterpret_cast(done), length); + free(done); + for (size_t at = 0, end; at < text.size(); at = end + 1) { + end = text.find('\n', at); + if (end == std::string::npos) end = text.size(); + if (end > at) prime::extraDone().insert(text.substr(at, end - at)); + } + } + } + } + } // The image's pipeline pack (an earlier boot's export), into this directory before the runtime reads it. Marker 0x6Cnn: files written // (saturated at 0xfe), 0x6Cff the pack was refused. if (char pack[1024]; requestedPath("NativeAdrenoMetal.m2vpack", pack, sizeof pack)) { @@ -420,17 +456,12 @@ static id deviceForPort(unsigned port) { // The translator recurses deeply on large functions: give it the stack of a main thread. pthread_attr_setstacksize(&attributes, 8u << 20); pthread_t primer; - // First the pipeline caches compiled ahead on the host, from the import region (CacheExport.h importRegion), which also lists - // every library they cover as primed: priming then compiles only what they left out. The runtime looks its entries up as it - // needs them, so the render server draws meanwhile. - pthread_create(&primer, &attributes, [](void *arg) -> void * { - pthread_set_qos_class_self_np(QOS_CLASS_UTILITY, 0); - cacheexport::importRegion(*static_cast(arg), gCacheDirectory); + pthread_create(&primer, &attributes, [](void *) -> void * { char list[1024]; prime::run((id)device, gCacheDirectory, 90, requestedPath("NativeAdrenoMetal.primelist", list, sizeof list) ? std::string(list) : std::string()); return nullptr; - }, gpu); + }, nullptr); pthread_attr_destroy(&attributes); gpu->mark(0x6803); return device; diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 15614f2..fca24f9 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -118,6 +118,8 @@ static void abortProbe(int signal, siginfo_t *si, void *context) { } // NATIVE_METAL_TIMING=1: the telemetry table (where the CPU side's time went: pipelines, libraries, compiles and the shader cache) // written to standard error every 20 s (an app the system ends at the end of a test never reaches atexit). +// The plugin's on-demand region reads (CacheImport.h). Weak here: the plugin's own definition wins, and a host build reads zeros. +extern "C" {__attribute__((weak)) volatile uint64_t native_cache_import_stats[4]; __attribute__((weak)) char native_cache_import_first_miss[160];} extern "C" size_t native_bridge_metal_telemetry(char *out,size_t size); static void *reportTiming(void *) { static char table[6144]; @@ -4206,6 +4208,12 @@ extern "C" size_t native_bridge_metal_telemetry(char *out,size_t size) { if(nmb_device_texture_counts(gDevice,tc)) row("L inp %llu/%llu rf %llu/%llu sh %llu cp %llu bk %llu rt %llu",(unsigned long long)tc[0],(unsigned long long)tc[1],(unsigned long long)tc[2], (unsigned long long)tc[3],(unsigned long long)tc[4],(unsigned long long)tc[5],(unsigned long long)tc[6],(unsigned long long)tc[7]); + // The pipeline caches compiled ahead on the host, read on demand from the userdata import region (the plugin's CacheImport.h). + if((native_cache_import_stats[0]||native_cache_import_stats[2]||native_cache_import_stats[3])) { + row("CI hit%llu KiB%llu miss%llu fail%llu",(unsigned long long)native_cache_import_stats[0],(unsigned long long)(native_cache_import_stats[1]>>10), + (unsigned long long)native_cache_import_stats[2],(unsigned long long)native_cache_import_stats[3]); + if(native_cache_import_first_miss[0])row("CIm %.86s",native_cache_import_first_miss+(strlen(native_cache_import_first_miss)>86?strlen(native_cache_import_first_miss)-86:0)); + } char warning[200];size_t n=nmb_last_warning(warning,sizeof warning); if(n){row("W %.86s",warning);if(n>86)row("Wb %.86s",warning+86);} } diff --git a/PrimeShaders.h b/PrimeShaders.h index 530e0ed..dc3d088 100644 --- a/PrimeShaders.h +++ b/PrimeShaders.h @@ -289,6 +289,9 @@ static std::string library(id device, const std::string &path, const return path + line + firstRefusal; } +// Libraries finished elsewhere (the pipeline caches compiled ahead on the host, CacheImport.h), in prime-done's line form. +static std::set &extraDone() {static std::set lines; return lines;} + // The thread: waits for the desktop to settle, then works through the libraries not finished yet. static void run(id device, std::string root, unsigned delaySeconds, const std::string &listPath) { pthread_set_qos_class_self_np(QOS_CLASS_BACKGROUND, 0); @@ -302,6 +305,7 @@ static void run(id device, std::string root, unsigned delaySeconds, c sleep(delaySeconds); const std::set bad = readLines(badPath); std::set done = readLines(donePath); + done.insert(extraDone().begin(), extraDone().end()); const uint64_t began = nowMs(); unsigned libs = 0, finished = 0; std::string note; diff --git a/bridge/NativeMetalObjects.h b/bridge/NativeMetalObjects.h index ab01abc..34231f8 100644 --- a/bridge/NativeMetalObjects.h +++ b/bridge/NativeMetalObjects.h @@ -53,6 +53,9 @@ struct NmbShaderCacheStats { uint64_t driver_loads, driver_hits, driver_misses, driver_saves, driver_truncated, driver_mismatched, driver_unchanged; }; bool nmb_device_shader_cache_stats(const NmbDevice *, struct NmbShaderCacheStats *); +/* The read-only source of shader-cache entries behind every cache directory, set before the device opens: read(path relative to the + * directory, out, length) puts the entry in a malloc'd buffer the bridge frees and returns true, or returns false. */ +bool nmb_set_shader_cache_fallback(bool (*read)(const char *path, uint8_t **out, size_t *length)); size_t nmb_last_warning(char *out, size_t size); bool nmb_device_max_threads_per_threadgroup(const NmbDevice *, uint64_t out[3]); /* Device queries the runtime answers by Metal selector: Apple's private counts and flags where the device has an answer of its own, diff --git a/bridge/NativeMetalObjects.rs b/bridge/NativeMetalObjects.rs index a994690..55a3067 100644 --- a/bridge/NativeMetalObjects.rs +++ b/bridge/NativeMetalObjects.rs @@ -183,6 +183,10 @@ pub unsafe extern "C" fn nmb_device_open(loader: *const c_char, cache: *const c_ // server's surfaces and padded framebuffers (3048x2032 BGRA8 at 12288-byte rows, int-251) were otherwise copied in and out of a // shadow image every pass. options.policy.host_page_textures = metal2vulkan::device::HostPageTexturePolicy::InPlaceWrites; + // Metal makes a pipeline state's pipeline at its creation and never drops a draw for a pipeline being made; the runtime's + // default (PipelineWaitPolicy::Bounded) drops the draws and dispatches of a pipeline not made 4.5 s after the commit, which the + // render server showed as missing pieces of the screen while it compiled (int 590aaee2). + options.policy.pipeline_waits = metal2vulkan::device::PipelineWaitPolicy::Exact; // Every texture given a shadow image is announced with why no image aliases its bytes (`shadow.log`). options.announce_each.insert(metal2vulkan_runtime::Degradation::ShadowedTexture); let _ = SHADOW_LOG.set(PathBuf::from(cache.to_string_lossy().into_owned()).join("shadow.log")); @@ -344,6 +348,34 @@ pub struct NmbShaderCacheStats { pub driver_unchanged: u64, } +/// The caller's read-only source of shader-cache entries behind the cache directory: `read(path, out, length)` gives the entry at +/// `path` (relative to the directory) in a buffer from malloc (the bridge frees it) and returns true, or returns false. +type FallbackRead = unsafe extern "C" fn(*const c_char, *mut *mut u8, *mut usize) -> bool; +static FALLBACK_READ: std::sync::OnceLock = std::sync::OnceLock::new(); + +fn fallback_read(path: &str, out: &mut Vec) -> bool { + let (Some(read), Ok(path)) = (FALLBACK_READ.get(), std::ffi::CString::new(path)) else { return false }; + let (mut data, mut length) = (std::ptr::null_mut::(), 0usize); + // SAFETY: the caller's contract: on true, `data` holds `length` bytes from malloc, which are freed here. + unsafe { + if !read(path.as_ptr(), &mut data, &mut length) || data.is_null() { + return false; + } + out.extend_from_slice(std::slice::from_raw_parts(data, length)); + libc::free(data.cast()); + } + true +} + +/// Set the read-only source behind every shader-cache directory, before the device opens; false where one was set already. +#[unsafe(no_mangle)] +pub extern "C" fn nmb_set_shader_cache_fallback(read: Option) -> bool { + match read { + Some(read) => FALLBACK_READ.set(read).is_ok() && metal2vulkan_runtime::set_shader_cache_fallback(fallback_read), + None => false, + } +} + #[unsafe(no_mangle)] pub extern "C" fn nmb_device_shader_cache_stats(device: *const NmbDevice, out: *mut NmbShaderCacheStats) -> bool { guard(false, || { diff --git a/bridge/patches/0017-shader-cache-fallback-source.patch b/bridge/patches/0017-shader-cache-fallback-source.patch new file mode 100644 index 0000000..1dc8cb4 --- /dev/null +++ b/bridge/patches/0017-shader-cache-fallback-source.patch @@ -0,0 +1,68 @@ +diff --git a/crates/metal2vulkan-runtime/src/lib.rs b/crates/metal2vulkan-runtime/src/lib.rs +index ae78ee38..6cd9d58d 100644 +--- a/crates/metal2vulkan-runtime/src/lib.rs ++++ b/crates/metal2vulkan-runtime/src/lib.rs +@@ -336,7 +336,7 @@ pub use render_values::{ + pub use resource::{ObjectId, ResourceRef}; + pub use resource_ids::{IdPlacement, ResourceId, ResourceIdKind}; + pub use sampler::{sampler_border_color, SamplerState, SamplerStateOptions}; +-pub use shader_cache::{ ++pub use shader_cache::{set_shader_cache_fallback, ShaderCacheFallback, + ShaderCacheOptions, ShaderCacheStatistics, DEFAULT_CAPACITY, DEFAULT_MEMORY_CAPACITY, + }; + pub use sparse::device::SparseFeatures; +diff --git a/crates/metal2vulkan-runtime/src/shader_cache/mod.rs b/crates/metal2vulkan-runtime/src/shader_cache/mod.rs +index a9da76d1..143b665d 100644 +--- a/crates/metal2vulkan-runtime/src/shader_cache/mod.rs ++++ b/crates/metal2vulkan-runtime/src/shader_cache/mod.rs +@@ -70,6 +70,7 @@ pub(crate) use creation::VariantStages; + pub(crate) mod driver; + mod held; + pub(super) mod store; ++pub use store::{set_fallback as set_shader_cache_fallback, Fallback as ShaderCacheFallback}; + pub(super) mod worker; + + use super::error::{invalid, Error, Problem, ShaderCacheError}; +diff --git a/crates/metal2vulkan-runtime/src/shader_cache/store.rs b/crates/metal2vulkan-runtime/src/shader_cache/store.rs +index 38742f62..0f6fc951 100644 +--- a/crates/metal2vulkan-runtime/src/shader_cache/store.rs ++++ b/crates/metal2vulkan-runtime/src/shader_cache/store.rs +@@ -569,7 +569,14 @@ impl Store { + timed(&self.counters.read_nanos, || { + let file = match fs::File::open(path) { + Ok(file) => file, +- Err(error) if error.kind() == io::ErrorKind::NotFound => return Found::Absent, ++ Err(error) if error.kind() == io::ErrorKind::NotFound => { ++ // An entry the directory lacks, from the read-only source behind it (`set_fallback`), as a queued ++ // write's bytes are: no file to refresh. ++ return match fallback_read(self.directory.root(), path) { ++ Some(bytes) => Found::Queued(bytes), ++ None => Found::Absent, ++ }; ++ } + Err(_) => return Found::Failed, + }; + let Ok(length) = file.metadata().map(|metadata| metadata.len()) else { +@@ -885,3 +892,22 @@ fn new_private_file(path: &Path) -> io::Result { + } + options.open(path) + } ++ ++/// A read-only source of entries behind every cache directory: given an entry's path relative to its directory's root, its bytes, ++/// or false. A process whose entries were made ahead (on another machine, for the same driver and runtime) reads them from there ++/// without copying them into the directory; what it makes itself still goes to the directory. Set once, before any device opens. ++pub type Fallback = fn(&str, &mut Vec) -> bool; ++static FALLBACK: std::sync::OnceLock = std::sync::OnceLock::new(); ++ ++/// Set the read-only source behind every cache directory ([`Fallback`]); false where one was set already. ++pub fn set_fallback(source: Fallback) -> bool { ++ FALLBACK.set(source).is_ok() ++} ++ ++/// The fallback's bytes of the entry at `path` under `root`. ++fn fallback_read(root: &Path, path: &Path) -> Option> { ++ let source = FALLBACK.get()?; ++ let relative = path.strip_prefix(root).ok()?.to_str()?; ++ let mut bytes = Vec::new(); ++ source(relative, &mut bytes).then_some(bytes) ++} diff --git a/bridge/prime_host.mm b/bridge/prime_host.mm index 792793a..9108ddc 100644 --- a/bridge/prime_host.mm +++ b/bridge/prime_host.mm @@ -9,6 +9,7 @@ #include "../PrimeShaders.h" #include "../NativeBridgeMetalFrontend.h" #include "NativeMetalObjects.h" +#include "../CacheImport.h" #include static void onMissing(const char *, const char *) {} @@ -37,6 +38,11 @@ static std::string fileUuid(const char *path) { int main(int argc, char **argv) { @autoreleasepool { if (argc != 7 && argc != 9) return 2; + // NMB_IMPORT_PAYLOAD: the import region's bytes (make-pipeline-import.py --payload) behind the cache directory, read on demand as + // the tablet's plugin reads the region (CacheImport.h): a consumer run over an empty directory then compiles nothing it covers. + if (const char *payload = getenv("NMB_IMPORT_PAYLOAD")) { + if (!cacheimport::openFile(payload) || !nmb_set_shader_cache_fallback(cacheimport::read)) {std::printf("FAIL import payload\n"); return 1;} + } id device = native_bridge_metal_open(argv[1], argv[2], 0x6e6d70, onMissing); if (!device) {std::printf("FAIL device\n"); return 1;} native_bridge_metal_quiet_problems(true); @@ -68,8 +74,12 @@ int main(int argc, char **argv) { still = now == last ? still + 1 : 0; last = now; } - std::printf("PASS: primed %u libraries (%u absent) made=%u writes=%llu driver_saves=%llu build=%s\n", primed, absent, - (unsigned)native_prime_made, (unsigned long long)stats.writes, (unsigned long long)stats.driver_saves, build.c_str()); + std::printf("PASS: primed %u libraries (%u absent) made=%u writes=%llu driver_saves=%llu hits=%llu misses=%llu driver_hits=%llu " + "driver_misses=%llu import=%llu/%llu/%llu build=%s\n", primed, absent, (unsigned)native_prime_made, + (unsigned long long)stats.writes, (unsigned long long)stats.driver_saves, (unsigned long long)stats.hits, + (unsigned long long)stats.misses, (unsigned long long)stats.driver_hits, (unsigned long long)stats.driver_misses, + (unsigned long long)cacheimport::native_cache_import_stats[0], (unsigned long long)cacheimport::native_cache_import_stats[2], + (unsigned long long)cacheimport::native_cache_import_stats[3], build.c_str()); return 0; } } From 8c86579f075eec921dadf3c1e249c578c785d819 Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Thu, 29 Oct 2026 14:29:47 +0800 Subject: [PATCH 67/92] feat(bridge): metal2vulkan development 624bab39, heap texture sizes The translator moves to upstream's development branch, which carries the inexact-math work (device float maths, conversions and special values). The 17 patches rebase unchanged apart from context lines. Patch 0018 rounds a heap texture's own-device size answer up to a multiple of its alignment, as Metal's answers are, so a heap sized as the sum of its textures' answers holds them (Core Image heaps in iconservicesagent ran out with OutOfMemory(Heap)). --- ...003-cache-device-entry-point-priming.patch | 4 ++-- ...h-barriers-and-key-hash-neutral-maps.patch | 10 ++++----- bridge/patches/0008-llvm-bitreverse.patch | 2 +- ...b-extended-range-blend-rounded-model.patch | 4 ++-- bridge/patches/0016-refuse-rebinds.patch | 8 +++---- ...ure-size-a-multiple-of-its-alignment.patch | 21 +++++++++++++++++++ 6 files changed, 35 insertions(+), 14 deletions(-) create mode 100644 bridge/patches/0018-heap-texture-size-a-multiple-of-its-alignment.patch diff --git a/bridge/patches/0003-cache-device-entry-point-priming.patch b/bridge/patches/0003-cache-device-entry-point-priming.patch index 7af9f41..277a6be 100644 --- a/bridge/patches/0003-cache-device-entry-point-priming.patch +++ b/bridge/patches/0003-cache-device-entry-point-priming.patch @@ -1,5 +1,5 @@ diff --git a/crates/metal2vulkan-runtime/src/device/mod.rs b/crates/metal2vulkan-runtime/src/device/mod.rs -index a47d32ea..784315fb 100644 +index fce02139..053cafe9 100644 --- a/crates/metal2vulkan-runtime/src/device/mod.rs +++ b/crates/metal2vulkan-runtime/src/device/mod.rs @@ -1503,6 +1503,31 @@ impl Device { @@ -65,7 +65,7 @@ index a47d32ea..784315fb 100644 } { scheduler.stop(); diff --git a/crates/metal2vulkan-runtime/src/device/prime.rs b/crates/metal2vulkan-runtime/src/device/prime.rs -index e953806b..29671308 100644 +index 144a6e59..919f5c9e 100644 --- a/crates/metal2vulkan-runtime/src/device/prime.rs +++ b/crates/metal2vulkan-runtime/src/device/prime.rs @@ -82,9 +82,11 @@ use crate::gpu_time::Calibration; diff --git a/bridge/patches/0006-batch-barriers-and-key-hash-neutral-maps.patch b/bridge/patches/0006-batch-barriers-and-key-hash-neutral-maps.patch index 2497380..e5b9b56 100644 --- a/bridge/patches/0006-batch-barriers-and-key-hash-neutral-maps.patch +++ b/bridge/patches/0006-batch-barriers-and-key-hash-neutral-maps.patch @@ -1,8 +1,8 @@ diff --git a/crates/metal2vulkan-runtime/src/command_buffer/submit.rs b/crates/metal2vulkan-runtime/src/command_buffer/submit.rs -index 6ab175dd..0cce8207 100644 +index 3e0887d8..7b18eb69 100644 --- a/crates/metal2vulkan-runtime/src/command_buffer/submit.rs +++ b/crates/metal2vulkan-runtime/src/command_buffer/submit.rs -@@ -648,11 +648,8 @@ impl CommandBufferShared { +@@ -636,11 +636,8 @@ impl CommandBufferShared { barriers.first().is_some_and(Option::is_some) || !held_moves.transitions(0).is_empty(); if entry.is_some() { started = true; @@ -16,7 +16,7 @@ index 6ab175dd..0cce8207 100644 let stamps = Stamps { start: start_stamp.filter(|_| !encoder_behind), after: entry_barrier.is_some(), -@@ -661,14 +658,11 @@ impl CommandBufferShared { +@@ -649,14 +646,11 @@ impl CommandBufferShared { if stamps.start.is_some() { start_stamp = None; } @@ -34,7 +34,7 @@ index 6ab175dd..0cce8207 100644 let moves = held_moves.transitions(offset); if !globals.is_empty() || start_stamp.is_some() || !moves.is_empty() { started |= offset == 0; -@@ -678,7 +672,7 @@ impl CommandBufferShared { +@@ -666,7 +660,7 @@ impl CommandBufferShared { end: None, }; let kept = &mut scheduler.kept; @@ -43,7 +43,7 @@ index 6ab175dd..0cce8207 100644 } work.extend(&state.recorded[start + offset].raws); } -@@ -1429,3 +1423,15 @@ impl Stamps { +@@ -1424,3 +1418,15 @@ impl Stamps { }) } } diff --git a/bridge/patches/0008-llvm-bitreverse.patch b/bridge/patches/0008-llvm-bitreverse.patch index 7d43270..49eb7fa 100644 --- a/bridge/patches/0008-llvm-bitreverse.patch +++ b/bridge/patches/0008-llvm-bitreverse.patch @@ -29,7 +29,7 @@ index d784c6c5..b6ca3f98 100644 _ => 2, } diff --git a/crates/metal2vulkan/src/lower/arith/mod.rs b/crates/metal2vulkan/src/lower/arith/mod.rs -index feab97c7..1e5f8863 100644 +index 1d87c11e..196c6430 100644 --- a/crates/metal2vulkan/src/lower/arith/mod.rs +++ b/crates/metal2vulkan/src/lower/arith/mod.rs @@ -1746,6 +1746,7 @@ fn llvm_math(op: &LlvmIntrinsic) -> Result { diff --git a/bridge/patches/0012-srgb-extended-range-blend-rounded-model.patch b/bridge/patches/0012-srgb-extended-range-blend-rounded-model.patch index 5517c29..7698a38 100644 --- a/bridge/patches/0012-srgb-extended-range-blend-rounded-model.patch +++ b/bridge/patches/0012-srgb-extended-range-blend-rounded-model.patch @@ -1,5 +1,5 @@ diff --git a/crates/metal2vulkan/src/lower/arith/extended_range/blend.rs b/crates/metal2vulkan/src/lower/arith/extended_range/blend.rs -index 33c67576..55c119e6 100644 +index 618f1a48..3df6a593 100644 --- a/crates/metal2vulkan/src/lower/arith/extended_range/blend.rs +++ b/crates/metal2vulkan/src/lower/arith/extended_range/blend.rs @@ -990,10 +990,16 @@ fn planned_for( @@ -22,7 +22,7 @@ index 33c67576..55c119e6 100644 let opaque_ = !has_alpha; let color = match colors { 0 => None, -@@ -1518,6 +1524,13 @@ impl Blend { +@@ -1519,6 +1525,13 @@ impl Blend { /// Metal's model's: a `half4` one; a `float4` one under a blend whose color lanes take one term /// alone, unmasked, rounded to `half` first; another is the gap it is. pub fn takes(&self, output: Option) -> Result<(), Error> { diff --git a/bridge/patches/0016-refuse-rebinds.patch b/bridge/patches/0016-refuse-rebinds.patch index a105a28..665458f 100644 --- a/bridge/patches/0016-refuse-rebinds.patch +++ b/bridge/patches/0016-refuse-rebinds.patch @@ -1,5 +1,5 @@ diff --git a/crates/metal2vulkan-runtime/src/rebind.rs b/crates/metal2vulkan-runtime/src/rebind.rs -index 3fe37827..17fbf6d6 100644 +index 6faf2910..557b24af 100644 --- a/crates/metal2vulkan-runtime/src/rebind.rs +++ b/crates/metal2vulkan-runtime/src/rebind.rs @@ -96,6 +96,8 @@ pub enum RebindProblem { @@ -30,7 +30,7 @@ index 3fe37827..17fbf6d6 100644 } /// Note the buffer of `key` over host pages, `cell` its hold on them. -@@ -935,6 +940,9 @@ impl Buffer { +@@ -931,6 +936,9 @@ impl Buffer { keep_alive: Option, ) -> Result<(), Error> { const CALL: &str = "rebind_bytes_no_copy"; @@ -40,7 +40,7 @@ index 3fe37827..17fbf6d6 100644 let Some(cell) = self.host_cell() else { return Err(invalid(CALL, Problem::Rebind(RebindProblem::NotHostMemory))); }; -@@ -987,6 +995,9 @@ impl Heap { +@@ -983,6 +991,9 @@ impl Heap { keep_alive: Option, ) -> Result<(), Error> { const CALL: &str = "rebind_bytes_no_copy"; @@ -50,7 +50,7 @@ index 3fe37827..17fbf6d6 100644 let Some((own, cell)) = self .bytes() .and_then(|own| own.host_cell().map(|cell| (own, cell))) -@@ -1016,3 +1027,16 @@ impl Device { +@@ -1012,3 +1023,16 @@ impl Device { self.shared().rebinds.pending(key) } } diff --git a/bridge/patches/0018-heap-texture-size-a-multiple-of-its-alignment.patch b/bridge/patches/0018-heap-texture-size-a-multiple-of-its-alignment.patch new file mode 100644 index 0000000..9659478 --- /dev/null +++ b/bridge/patches/0018-heap-texture-size-a-multiple-of-its-alignment.patch @@ -0,0 +1,21 @@ +diff --git a/crates/metal2vulkan-runtime/src/texture/heap.rs b/crates/metal2vulkan-runtime/src/texture/heap.rs +index b77aaa48..f33c13af 100644 +--- a/crates/metal2vulkan-runtime/src/texture/heap.rs ++++ b/crates/metal2vulkan-runtime/src/texture/heap.rs +@@ -1861,6 +1861,16 @@ impl Device { + metal_layout = tiles; + } + } ++ // An answer of the device's own (no Metal answer modelled, or one the texture is past) ++ // is the image's size in the driver's units, which need not be a multiple of the image's ++ // alignment (Turnip's 4 KiB). Metal's sizes are multiples of their alignment, so a heap ++ // sized as the sum of its textures' answers holds them in any order; with the device's ++ // size each texture placed after another pads past what the heap was sized for, and the ++ // last one finds no room (iPadOS 27's iconservicesagent: `heap texture: OutOfMemory(Heap)`). ++ // A shadow in Metal's tiles takes Metal's answer. ++ if (metal_answer.is_none() || past_metal.is_some()) && image.route != AliasRoute::Tiled { ++ size_and_align.size = size_and_align.size.next_multiple_of(size_and_align.align.max(1)); ++ } + // Only a texture an image of binds into the heap's own memory is held to the heap's base, + // and the routes decide that (`holds_heap_base`). + check_heap_base(image.route, size_and_align.align)?; From ca7faafcd94d8cd3abe3d00b0e2ef5334f0b2f97 Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Thu, 29 Oct 2026 14:37:12 +0800 Subject: [PATCH 68/92] feat(telemetry): PS row, where render passes cost more than one tile pass Exports the runtime's pass counts (passes begun as instances, inline splits, instance splits by cause, stand-ins and their copies) as the frontend's PS row, so a boot shows whether the GPU time Liquid Glass takes comes from split passes or stand-in images. --- NativeBridgeMetalFrontend.mm | 7 +++++++ bridge/NativeMetalObjects.h | 3 +++ bridge/NativeMetalObjects.rs | 19 +++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index fca24f9..e027cd3 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -4208,6 +4208,13 @@ extern "C" size_t native_bridge_metal_telemetry(char *out,size_t size) { if(nmb_device_texture_counts(gDevice,tc)) row("L inp %llu/%llu rf %llu/%llu sh %llu cp %llu bk %llu rt %llu",(unsigned long long)tc[0],(unsigned long long)tc[1],(unsigned long long)tc[2], (unsigned long long)tc[3],(unsigned long long)tc[4],(unsigned long long)tc[5],(unsigned long long)tc[6],(unsigned long long)tc[7]); + // Where passes cost more than one pass in tile memory: passes begun as instances, split inline; instance splits for a barrier, tile + // memory, a memoryless write, a pending pipeline; stand-ins made and MiB; stand-in copies, of them at draws. + uint64_t pc[10]={}; + if(nmb_device_pass_counts(gDevice,pc)) + row("PS ins %llu inl %llu | bar %llu tile %llu ml %llu pend %llu | sti %llu %lluM cp %llu/%llu",(unsigned long long)pc[0], + (unsigned long long)pc[1],(unsigned long long)pc[2],(unsigned long long)pc[3],(unsigned long long)pc[4],(unsigned long long)pc[5], + (unsigned long long)pc[6],(unsigned long long)(pc[7]>>20),(unsigned long long)pc[8],(unsigned long long)pc[9]); // The pipeline caches compiled ahead on the host, read on demand from the userdata import region (the plugin's CacheImport.h). if((native_cache_import_stats[0]||native_cache_import_stats[2]||native_cache_import_stats[3])) { row("CI hit%llu KiB%llu miss%llu fail%llu",(unsigned long long)native_cache_import_stats[0],(unsigned long long)(native_cache_import_stats[1]>>10), diff --git a/bridge/NativeMetalObjects.h b/bridge/NativeMetalObjects.h index 34231f8..d878cea 100644 --- a/bridge/NativeMetalObjects.h +++ b/bridge/NativeMetalObjects.h @@ -45,6 +45,9 @@ bool nmb_device_work_stats(const NmbDevice *, uint64_t out[10]); const char *nmb_runtime_build(void); // Linear images made/refused/refused again, shadowed textures, shadow copies, texels copied back, linear passes in place/through copies. bool nmb_device_texture_counts(const NmbDevice *, uint64_t out[8]); +// Passes begun as instances, split inline; instance splits for a barrier, tile memory, a memoryless write, a pending pipeline; stand-ins +// made and their bytes; stand-in copies made and those made at draws. +bool nmb_device_pass_counts(const NmbDevice *, uint64_t out[10]); // No-copy ranges checked, and those whose pages had changed since a buffer was last made over them (reported to the runtime as remapped). bool nmb_no_copy_remaps(uint64_t out[2]); /* Artifact lookups and driver pipeline records are separate cache layers. */ diff --git a/bridge/NativeMetalObjects.rs b/bridge/NativeMetalObjects.rs index 55a3067..3f9c6c6 100644 --- a/bridge/NativeMetalObjects.rs +++ b/bridge/NativeMetalObjects.rs @@ -329,6 +329,25 @@ pub extern "C" fn nmb_device_texture_counts(device: *const NmbDevice, out: *mut }) } +/// Where render passes cost the GPU more than Metal's single pass in tile memory, into the caller's ten words: passes begun as +/// instances, passes split inline, then instance splits for a barrier, tile memory, a memoryless write and a pending pipeline, stand-ins +/// made and their bytes, and stand-in copies made and those made at draws. +#[unsafe(no_mangle)] +pub extern "C" fn nmb_device_pass_counts(device: *const NmbDevice, out: *mut u64) -> bool { + guard(false, || { + if out.is_null() { + return Err("null counts".into()); + } + // SAFETY: the header's ten words. + let out = unsafe { std::slice::from_raw_parts_mut(out, 10) }; + let w = get(device)?.0.work_counts(); + out.copy_from_slice(&[w.render_passes_begun_instances, w.inline_passes_split, w.instance_splits_barrier, + w.instance_splits_tile_memory, w.instance_splits_memoryless_write, w.instance_splits_pending_pipeline, + w.stand_ins_made, w.stand_in_bytes, w.stand_in_copies_made, w.stand_in_copies_made_at_draws]); + Ok(true) + }) +} + /// Separate artifact and driver-record counters, mirroring the C ABI. #[repr(C)] pub struct NmbShaderCacheStats { From 1653279f24277a7e88d78332cdfd3a33c562701c Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Thu, 29 Oct 2026 15:26:49 +0800 Subject: [PATCH 69/92] feat(telemetry): MEM row, where a process's GPU memory is Bridge patch 0019 gives the runtime's live device memory in bytes by kind (pooled and placed, empty kept, dedicated, host imports). The MEM row prints them with Turnip's BO cache bytes, so the kernel's per-client total splits into the runtime's, the cache's and Turnip's own. --- NativeBridgeMetalFrontend.mm | 17 +++++ bridge/NativeMetalObjects.h | 3 + bridge/NativeMetalObjects.rs | 19 +++++ bridge/patches/0019-memory-census-bytes.patch | 72 +++++++++++++++++++ 4 files changed, 111 insertions(+) create mode 100644 bridge/patches/0019-memory-census-bytes.patch diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index e027cd3..df5b793 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -46,6 +46,7 @@ extern "C" void sys_dcache_flush(void *start, size_t length); static NmbDevice *gDevice; +static std::string gLoaderPath; // the Turnip library the runtime loaded (its own handle is local to it) static id gDeviceObject; // DEBUG (the guest's memory corruption, 2026-10-26): with NATIVE_METAL_ABORT_PROBE=1 in its environment, a process that aborts @@ -4012,6 +4013,7 @@ extern "C" id native_bridge_metal_open(const char *loader,const char *cache,uint if(gDeviceObject)return gDeviceObject; gMissing=missing;gInFlight=[NSMutableSet set]; gDevice=nmb_device_open(loader,cache); + if(loader)gLoaderPath=loader; if(!gDevice){NBLOG(@"native-metal: device refused: %s",nmb_last_error());return nil;} if(cache){gJournalPath=std::string(cache)+"/pipelines.log";gJournalBegan=nowUs();} char name[128]={};nmb_device_name(gDevice,name,sizeof name);gName=[NSString stringWithUTF8String:name];gRegistryID=registryID; @@ -4215,6 +4217,21 @@ extern "C" size_t native_bridge_metal_telemetry(char *out,size_t size) { row("PS ins %llu inl %llu | bar %llu tile %llu ml %llu pend %llu | sti %llu %lluM cp %llu/%llu",(unsigned long long)pc[0], (unsigned long long)pc[1],(unsigned long long)pc[2],(unsigned long long)pc[3],(unsigned long long)pc[4],(unsigned long long)pc[5], (unsigned long long)pc[6],(unsigned long long)(pc[7]>>20),(unsigned long long)pc[8],(unsigned long long)pc[9]); + // Where this process's GPU memory is (MiB): the runtime's pooled blocks (placed in them), empty kept blocks, dedicated blocks, host + // imports, allocated so far; then Turnip's cache of freed BOs now and at most (tu_darwin_bo_cache_stats). The kernel's [gpu objects] + // row has the process's own BOs: what is neither the runtime's nor the cache's is Turnip's own (command streams, shaders). + uint64_t mc[10]={}; + if(nmb_device_memory_counts(gDevice,mc)) { + uint64_t bc[8]={}; + static void *turnip=gLoaderPath.empty()?nullptr:dlopen(gLoaderPath.c_str(),RTLD_NOW|RTLD_NOLOAD); + static void (*cacheStats)(uint64_t *)=turnip?(void (*)(uint64_t *))dlsym(turnip,"tu_darwin_bo_cache_stats"):nullptr; + if(cacheStats)cacheStats(bc); + row("MEM pool %llu(%llu) empty %llu ded %llu imp %llu alloc %llu | blk %llu/%llu gp %llu-%llu | tuc %llu/%llu n%llu/%llu", + (unsigned long long)(mc[0]>>20),(unsigned long long)(mc[1]>>20),(unsigned long long)(mc[2]>>20),(unsigned long long)(mc[3]>>20), + (unsigned long long)(mc[4]>>20),(unsigned long long)(mc[5]>>20),(unsigned long long)mc[6],(unsigned long long)mc[7], + (unsigned long long)mc[8],(unsigned long long)mc[9],(unsigned long long)(bc[5]>>20),(unsigned long long)(bc[6]>>20), + (unsigned long long)bc[2],(unsigned long long)bc[3]); + } // The pipeline caches compiled ahead on the host, read on demand from the userdata import region (the plugin's CacheImport.h). if((native_cache_import_stats[0]||native_cache_import_stats[2]||native_cache_import_stats[3])) { row("CI hit%llu KiB%llu miss%llu fail%llu",(unsigned long long)native_cache_import_stats[0],(unsigned long long)(native_cache_import_stats[1]>>10), diff --git a/bridge/NativeMetalObjects.h b/bridge/NativeMetalObjects.h index d878cea..56aaee0 100644 --- a/bridge/NativeMetalObjects.h +++ b/bridge/NativeMetalObjects.h @@ -48,6 +48,9 @@ bool nmb_device_texture_counts(const NmbDevice *, uint64_t out[8]); // Passes begun as instances, split inline; instance splits for a barrier, tile memory, a memoryless write, a pending pipeline; stand-ins // made and their bytes; stand-in copies made and those made at draws. bool nmb_device_pass_counts(const NmbDevice *, uint64_t out[10]); +// Bytes of pooled blocks live and placed in them, empty kept, dedicated, host imports; bytes allocated so far; pooled and dedicated +// blocks live; GPU pool blocks made and released. +bool nmb_device_memory_counts(const NmbDevice *, uint64_t out[10]); // No-copy ranges checked, and those whose pages had changed since a buffer was last made over them (reported to the runtime as remapped). bool nmb_no_copy_remaps(uint64_t out[2]); /* Artifact lookups and driver pipeline records are separate cache layers. */ diff --git a/bridge/NativeMetalObjects.rs b/bridge/NativeMetalObjects.rs index 3f9c6c6..add1271 100644 --- a/bridge/NativeMetalObjects.rs +++ b/bridge/NativeMetalObjects.rs @@ -348,6 +348,25 @@ pub extern "C" fn nmb_device_pass_counts(device: *const NmbDevice, out: *mut u64 }) } +/// Where the runtime's device memory is, into the caller's ten words: bytes of its pooled blocks with a placement and of those the +/// bytes placed, of its empty kept pooled blocks, of its dedicated blocks (GPU pools, large resources) and of its host imports; then +/// the bytes it allocated so far, its live pooled and dedicated block counts, and the GPU pool blocks made and released. +#[unsafe(no_mangle)] +pub extern "C" fn nmb_device_memory_counts(device: *const NmbDevice, out: *mut u64) -> bool { + guard(false, || { + if out.is_null() { + return Err("null counts".into()); + } + // SAFETY: the header's ten words. + let out = unsafe { std::slice::from_raw_parts_mut(out, 10) }; + let w = get(device)?.0.work_counts(); + out.copy_from_slice(&[w.pooled_memory_bytes_live, w.pooled_memory_bytes_used, w.empty_memory_bytes_live, + w.dedicated_memory_bytes_live, w.imported_memory_bytes_live, w.device_memory_allocated, + w.memory_blocks_live, w.dedicated_memory_blocks_live, w.gpu_pool_blocks_made, w.gpu_pool_blocks_released]); + Ok(true) + }) +} + /// Separate artifact and driver-record counters, mirroring the C ABI. #[repr(C)] pub struct NmbShaderCacheStats { diff --git a/bridge/patches/0019-memory-census-bytes.patch b/bridge/patches/0019-memory-census-bytes.patch new file mode 100644 index 0000000..c5b8195 --- /dev/null +++ b/bridge/patches/0019-memory-census-bytes.patch @@ -0,0 +1,72 @@ +diff --git a/crates/metal2vulkan-runtime/src/memory.rs b/crates/metal2vulkan-runtime/src/memory.rs +index 8200c10d..1f882747 100644 +--- a/crates/metal2vulkan-runtime/src/memory.rs ++++ b/crates/metal2vulkan-runtime/src/memory.rs +@@ -624,13 +624,18 @@ impl Pool { + /// imports, whatever else they are. + fn count_into(&self, census: &mut Census, imported: impl Fn(&T) -> bool) { + for block in self.blocks.iter().flatten() { +- let count = match (imported(&block.payload), block.dedicated) { +- (true, _) => &mut census.imported, +- (false, true) => &mut census.dedicated, +- (false, false) if block.ranges.is_empty() => &mut census.empty, +- (false, false) => &mut census.pooled, ++ let bytes = block.ranges.len(); ++ let (count, total) = match (imported(&block.payload), block.dedicated) { ++ (true, _) => (&mut census.imported, &mut census.imported_bytes), ++ (false, true) => (&mut census.dedicated, &mut census.dedicated_bytes), ++ (false, false) if block.ranges.is_empty() => (&mut census.empty, &mut census.empty_bytes), ++ (false, false) => { ++ census.pooled_used_bytes += block.ranges.used(); ++ (&mut census.pooled, &mut census.pooled_bytes) ++ } + }; + *count += 1; ++ *total += bytes; + } + } + } +@@ -681,6 +686,13 @@ pub(super) struct Census { + pub(super) dedicated: u64, + /// The caller's host memory imported ([`Memory::import_host`]), a block each. + pub(super) imported: u64, ++ /// The bytes of those blocks: pooled ones, the bytes placed in them, empty kept ones, ++ /// dedicated ones and imports. ++ pub(super) pooled_bytes: u64, ++ pub(super) pooled_used_bytes: u64, ++ pub(super) empty_bytes: u64, ++ pub(super) dedicated_bytes: u64, ++ pub(super) imported_bytes: u64, + } + + /// A host pointer the runtime may send between threads: the mapping of a block it owns. +diff --git a/crates/metal2vulkan-runtime/src/work_counts.rs b/crates/metal2vulkan-runtime/src/work_counts.rs +index 1c56ba2c..cf468a3a 100644 +--- a/crates/metal2vulkan-runtime/src/work_counts.rs ++++ b/crates/metal2vulkan-runtime/src/work_counts.rs +@@ -3259,6 +3259,13 @@ pub struct WorkCounts { + pub pooled_memory_blocks_freed: u64, + /// Its live dedicated blocks: a placement larger than half a block, or one placed apart. + pub dedicated_memory_blocks_live: u64, ++ /// Bytes of its live pooled blocks with a placement, of those the bytes placed, of its ++ /// empty kept pooled blocks, of its dedicated blocks, and of its host imports. ++ pub pooled_memory_bytes_live: u64, ++ pub pooled_memory_bytes_used: u64, ++ pub empty_memory_bytes_live: u64, ++ pub dedicated_memory_bytes_live: u64, ++ pub imported_memory_bytes_live: u64, + /// Every block its allocator freed so far, by what made it go (`MemoryFrees`): one + /// `vkFreeMemory` each, and the allocator's whole share of + /// `object_churn.vk_memory_freed`. +@@ -5039,6 +5046,11 @@ impl Device { + pooled_memory_blocks_made: made, + pooled_memory_blocks_freed: freed, + dedicated_memory_blocks_live: census.dedicated, ++ pooled_memory_bytes_live: census.pooled_bytes, ++ pooled_memory_bytes_used: census.pooled_used_bytes, ++ empty_memory_bytes_live: census.empty_bytes, ++ dedicated_memory_bytes_live: census.dedicated_bytes, ++ imported_memory_bytes_live: census.imported_bytes, + memory_frees: MemoryFrees { + dedicated: frees[FreeCause::Dedicated as usize], + pooled_emptied: frees[FreeCause::PooledEmptied as usize], From b1a4d0e2524e4d58fca7f6f4c4fd05ddd880714e Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Thu, 29 Oct 2026 16:34:16 +0800 Subject: [PATCH 70/92] 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. --- CacheImport.h | 8 +- NativeAdrenoMetalPlugin.mm | 14 +-- NativeBridgeMetalFrontend.mm | 21 +++++ bridge/NativeMetalObjects.h | 3 + bridge/NativeMetalObjects.rs | 21 +++++ bridge/patches/0020-allocation-sites.patch | 105 +++++++++++++++++++++ 6 files changed, 164 insertions(+), 8 deletions(-) create mode 100644 bridge/patches/0020-allocation-sites.patch diff --git a/CacheImport.h b/CacheImport.h index 6356547..3de4bd7 100644 --- a/CacheImport.h +++ b/CacheImport.h @@ -38,6 +38,8 @@ static bool (*gRegionBlock)(uint64_t offset, uint8_t *out) = [](uint64_t offset, return gLink.method(kMethod, &offset, sizeof offset, out, kBlock) == 0; }; static FILE *gFile; +// The kernel refused the region with kIOReturnNotPermitted (boot argument sheng-gpu-prime=0): no import, and no priming either. +static bool gPrimingOff; static bool readBlock(uint64_t offset, uint8_t *out) { if (offset == gCachedBlock) {memcpy(out, gCached, kBlock); return true;} @@ -102,14 +104,16 @@ static bool readIndex(uint64_t region, Gpu *marker); // Opens the region and reads its index; false (nothing installed) without a region or with one that does not check. // Markers 0x63nn: 00 started, 01 index ready (then 0x64nn: entries in thousands, saturated), f0 no connection, f1 no selector, -// f2 no region (or not this layout), f3 the index does not match its digest. +// f2 no region (or not this layout), f3 the index does not match its digest, f4 priming is off (sheng-gpu-prime=0). static bool open(Gpu &marker) { marker.mark(0x6300); gLink.call = marker.call; gLink.openFn = marker.openFn; gLink.closeFn = marker.closeFn; gLink.port = marker.port; if (!gLink.openFn || gLink.openFn(gLink.port, mach_task_self(), 0x4148, &gLink.conn) != 0 || !gLink.conn) {marker.mark(0x63f0); return false;} if (!gLink.probe()) {marker.mark(0x63f1); return false;} uint64_t region = 0; - if (gLink.method(kMethod, ®ion, sizeof region, ®ion, sizeof region) != 0) {marker.mark(0x63f2); return false;} + const int answer = gLink.method(kMethod, ®ion, sizeof region, ®ion, sizeof region); + if (answer == (int)0xe00002e2) {gPrimingOff = true; marker.mark(0x63f4); return false;} // kIOReturnNotPermitted + if (answer != 0) {marker.mark(0x63f2); return false;} return readIndex(region, &marker); } static bool readIndex(uint64_t region, Gpu *gpu) { diff --git a/NativeAdrenoMetalPlugin.mm b/NativeAdrenoMetalPlugin.mm index c4bb32b..8e6cc26 100644 --- a/NativeAdrenoMetalPlugin.mm +++ b/NativeAdrenoMetalPlugin.mm @@ -454,14 +454,16 @@ static id deviceForPort(unsigned port) { // Every Metal library of the OS through the runtime, 90 s after the render server opened the device (PrimeShaders.h); the boots that froze // or crashed before three minutes never primed anything. Progress is kept in the Data cache directory, so later boots resume. // The translator recurses deeply on large functions: give it the stack of a main thread. + // Not with sheng-gpu-prime=0 (cacheimport::gPrimingOff): pipelines are then compiled only as processes ask for them. pthread_attr_setstacksize(&attributes, 8u << 20); pthread_t primer; - pthread_create(&primer, &attributes, [](void *) -> void * { - char list[1024]; - prime::run((id)device, gCacheDirectory, 90, - requestedPath("NativeAdrenoMetal.primelist", list, sizeof list) ? std::string(list) : std::string()); - return nullptr; - }, nullptr); + if (!cacheimport::gPrimingOff) + pthread_create(&primer, &attributes, [](void *) -> void * { + char list[1024]; + prime::run((id)device, gCacheDirectory, 90, + requestedPath("NativeAdrenoMetal.primelist", list, sizeof list) ? std::string(list) : std::string()); + return nullptr; + }, nullptr); pthread_attr_destroy(&attributes); gpu->mark(0x6803); return device; diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index df5b793..4dfcda8 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -4232,6 +4232,27 @@ extern "C" size_t native_bridge_metal_telemetry(char *out,size_t size) { (unsigned long long)mc[8],(unsigned long long)mc[9],(unsigned long long)(bc[5]>>20),(unsigned long long)(bc[6]>>20), (unsigned long long)bc[2],(unsigned long long)bc[3]); } + // Who allocated the device memory (4 MiB or more at once): the six call stacks with the most bytes, as MiB, allocations and return + // addresses relative to their image (+offset in the plugin, whose link map NativeAdrenoMetal.map names them; *offset in another). + { + uint64_t sites[6*12]={}; + const size_t n=nmb_allocation_sites(sites,6); + for(size_t i=0;i>20),(unsigned long long)site[1],frames[0],frames[1],frames[2], + frames[3],frames[4]); + row("MA%zub %s %s %s %s %s",i,frames[5],frames[6],frames[7],frames[8],frames[9]); + } + } // The pipeline caches compiled ahead on the host, read on demand from the userdata import region (the plugin's CacheImport.h). if((native_cache_import_stats[0]||native_cache_import_stats[2]||native_cache_import_stats[3])) { row("CI hit%llu KiB%llu miss%llu fail%llu",(unsigned long long)native_cache_import_stats[0],(unsigned long long)(native_cache_import_stats[1]>>10), diff --git a/bridge/NativeMetalObjects.h b/bridge/NativeMetalObjects.h index 56aaee0..86864a2 100644 --- a/bridge/NativeMetalObjects.h +++ b/bridge/NativeMetalObjects.h @@ -51,6 +51,9 @@ bool nmb_device_pass_counts(const NmbDevice *, uint64_t out[10]); // Bytes of pooled blocks live and placed in them, empty kept, dedicated, host imports; bytes allocated so far; pooled and dedicated // blocks live; GPU pool blocks made and released. bool nmb_device_memory_counts(const NmbDevice *, uint64_t out[10]); +// Call stacks that allocated device memory of 4 MiB or more, most bytes first: records of twelve words (bytes, allocations, ten return +// addresses); returns how many were written, at most max. +size_t nmb_allocation_sites(uint64_t *out, size_t max); // No-copy ranges checked, and those whose pages had changed since a buffer was last made over them (reported to the runtime as remapped). bool nmb_no_copy_remaps(uint64_t out[2]); /* Artifact lookups and driver pipeline records are separate cache layers. */ diff --git a/bridge/NativeMetalObjects.rs b/bridge/NativeMetalObjects.rs index add1271..d0c6c29 100644 --- a/bridge/NativeMetalObjects.rs +++ b/bridge/NativeMetalObjects.rs @@ -367,6 +367,27 @@ pub extern "C" fn nmb_device_memory_counts(device: *const NmbDevice, out: *mut u }) } +/// The call stacks that allocated device memory of 4 MiB or more (bridge patch 0020), most bytes first, into the caller's `max` records +/// of twelve words: bytes, allocations, then ten return addresses (0 past the stack's end). Returns the records written. +#[unsafe(no_mangle)] +pub extern "C" fn nmb_allocation_sites(out: *mut u64, max: usize) -> usize { + if out.is_null() { + return 0; + } + let sites = metal2vulkan_runtime::allocation_sites(); + let count = sites.len().min(max); + // SAFETY: the caller's `max` records of twelve words. + let out = unsafe { std::slice::from_raw_parts_mut(out, count * 12) }; + for (record, site) in out.chunks_mut(12).zip(&sites) { + record[0] = site.bytes; + record[1] = site.count; + for (word, frame) in record[2..].iter_mut().zip(site.frames) { + *word = frame as u64; + } + } + count +} + /// Separate artifact and driver-record counters, mirroring the C ABI. #[repr(C)] pub struct NmbShaderCacheStats { diff --git a/bridge/patches/0020-allocation-sites.patch b/bridge/patches/0020-allocation-sites.patch new file mode 100644 index 0000000..cb58939 --- /dev/null +++ b/bridge/patches/0020-allocation-sites.patch @@ -0,0 +1,105 @@ +diff --git a/crates/metal2vulkan-runtime/src/creation.rs b/crates/metal2vulkan-runtime/src/creation.rs +index 1d760a06..bbb1fb47 100644 +--- a/crates/metal2vulkan-runtime/src/creation.rs ++++ b/crates/metal2vulkan-runtime/src/creation.rs +@@ -109,10 +109,66 @@ pub(crate) unsafe fn allocate_memory( + info: &vk::MemoryAllocateInfo<'_>, + ) -> VkResult { + vk_made(VkMaking::Memory, Location::caller()); ++ note_allocation_site(info.allocation_size); + // SAFETY: the caller's. + unsafe { device.allocate_memory(info, None) } + } + ++/// The call stacks that allocated device memory of at least [`SITE_BYTES`], by the bytes they ++/// allocated: what a process's GPU memory went to, without a debugger on the device. A stack is ++/// its first [`SITE_FRAMES`] return addresses past this function's; at most [`SITE_COUNT`] ++/// stacks are kept, a new one past them counted under the last. ++const SITE_BYTES: u64 = 4 << 20; ++const SITE_FRAMES: usize = 10; ++const SITE_COUNT: usize = 32; ++static SITES: std::sync::Mutex> = std::sync::Mutex::new(Vec::new()); ++ ++/// One call stack's allocations ([`allocation_sites`]). ++#[derive(Clone, Copy, Debug, Default)] ++pub struct AllocationSite { ++ /// Bytes allocated so far. ++ pub bytes: u64, ++ /// Allocations so far. ++ pub count: u64, ++ /// Return addresses, innermost first; 0 past the stack's end. ++ pub frames: [usize; SITE_FRAMES], ++} ++ ++fn note_allocation_site(bytes: u64) { ++ if bytes < SITE_BYTES { ++ return; ++ } ++ let mut raw = [std::ptr::null_mut::(); SITE_FRAMES + 2]; ++ // SAFETY: `raw` holds the count passed. ++ #[cfg(target_vendor = "apple")] ++ let depth = unsafe { libc::backtrace(raw.as_mut_ptr(), raw.len() as libc::c_int) }.max(0) as usize; ++ #[cfg(not(target_vendor = "apple"))] ++ let depth = 0usize; ++ let mut frames = [0usize; SITE_FRAMES]; ++ for (frame, address) in frames.iter_mut().zip(raw.iter().take(depth).skip(2)) { ++ // The pointer authentication bits of a return address (arm64e) are not part of it. ++ *frame = (*address as usize) & ((1usize << 47) - 1); ++ } ++ let Ok(mut sites) = SITES.lock() else { return }; ++ let at = match sites.iter().position(|site| site.frames == frames) { ++ Some(at) => at, ++ None if sites.len() < SITE_COUNT => { ++ sites.push(AllocationSite { frames, ..Default::default() }); ++ sites.len() - 1 ++ } ++ None => sites.len() - 1, ++ }; ++ sites[at].bytes += bytes; ++ sites[at].count += 1; ++} ++ ++/// The call stacks that allocated device memory of 4 MiB or more, most bytes first. ++pub fn allocation_sites() -> Vec { ++ let mut sites = SITES.lock().map(|sites| sites.clone()).unwrap_or_default(); ++ sites.sort_by(|a, b| b.bytes.cmp(&a.bytes)); ++ sites ++} ++ + /// `image` bound to `memory` at `offset`. + /// + /// # Safety +diff --git a/crates/metal2vulkan-runtime/src/lib.rs b/crates/metal2vulkan-runtime/src/lib.rs +index 6cd9d58d..041567d2 100644 +--- a/crates/metal2vulkan-runtime/src/lib.rs ++++ b/crates/metal2vulkan-runtime/src/lib.rs +@@ -336,6 +336,7 @@ pub use render_values::{ + pub use resource::{ObjectId, ResourceRef}; + pub use resource_ids::{IdPlacement, ResourceId, ResourceIdKind}; + pub use sampler::{sampler_border_color, SamplerState, SamplerStateOptions}; ++pub use creation::{allocation_sites, AllocationSite}; + pub use shader_cache::{set_shader_cache_fallback, ShaderCacheFallback, + ShaderCacheOptions, ShaderCacheStatistics, DEFAULT_CAPACITY, DEFAULT_MEMORY_CAPACITY, + }; +diff --git a/crates/metal2vulkan-runtime/src/work_counts.rs b/crates/metal2vulkan-runtime/src/work_counts.rs +index cf468a3a..7283fc8c 100644 +--- a/crates/metal2vulkan-runtime/src/work_counts.rs ++++ b/crates/metal2vulkan-runtime/src/work_counts.rs +@@ -3259,12 +3259,15 @@ pub struct WorkCounts { + pub pooled_memory_blocks_freed: u64, + /// Its live dedicated blocks: a placement larger than half a block, or one placed apart. + pub dedicated_memory_blocks_live: u64, +- /// Bytes of its live pooled blocks with a placement, of those the bytes placed, of its +- /// empty kept pooled blocks, of its dedicated blocks, and of its host imports. ++ /// Bytes of its live pooled blocks with a placement. + pub pooled_memory_bytes_live: u64, ++ /// Of those, the bytes placed. + pub pooled_memory_bytes_used: u64, ++ /// Bytes of its empty kept pooled blocks. + pub empty_memory_bytes_live: u64, ++ /// Bytes of its live dedicated blocks. + pub dedicated_memory_bytes_live: u64, ++ /// Bytes of its live host imports. + pub imported_memory_bytes_live: u64, + /// Every block its allocator freed so far, by what made it go (`MemoryFrees`): one + /// `vkFreeMemory` each, and the allocator's whole share of From 017398d4198bed2474e88582ecff9734959e6769 Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Thu, 29 Oct 2026 17:04:36 +0800 Subject: [PATCH 71/92] fix(bridge): written host-page textures in place at any size Bridge patch 0021 drops the shadow a written texture over host pages took where its tile stores could reach past its rows. Turnip now renders a linear host or explicit-layout image of partial tile blocks without tile memory, so it writes its texels alone. backboardd's 3048x2032 RGBA16Float EDR surface was such a shadow, and its two-way writes merged every pass back through a 94 MB scratch: 58 GB in 613 passes (untagged-8dd25bd1c). --- ...t-page-textures-in-place-at-any-size.patch | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 bridge/patches/0021-written-host-page-textures-in-place-at-any-size.patch diff --git a/bridge/patches/0021-written-host-page-textures-in-place-at-any-size.patch b/bridge/patches/0021-written-host-page-textures-in-place-at-any-size.patch new file mode 100644 index 0000000..cbf6682 --- /dev/null +++ b/bridge/patches/0021-written-host-page-textures-in-place-at-any-size.patch @@ -0,0 +1,39 @@ +diff --git a/crates/metal2vulkan-runtime/src/texture.rs b/crates/metal2vulkan-runtime/src/texture.rs +index b1b02b30..8baf3ded 100644 +--- a/crates/metal2vulkan-runtime/src/texture.rs ++++ b/crates/metal2vulkan-runtime/src/texture.rs +@@ -2221,27 +2221,13 @@ impl Device { + true => HostPageTexturePolicy::InPlace, + false => self.compile_device().policy().host_page_textures, + }; +- // A written texture in place under `InPlaceWrites` whose tile stores would +- // reach past its rows or its buffer (`linear_plan::tile_store_fits`) is a +- // shadow instead. +- let writes = usage.intersects( +- TextureUsage::RENDER_TARGET +- | TextureUsage::SHADER_WRITE +- | TextureUsage::SHADER_ATOMIC, +- ); +- let policy = match policy { +- HostPageTexturePolicy::InPlaceWrites +- if writes +- && !tile_store_fits( +- &texels.layout, +- u64::from(layout.extent[0]), +- texels.buffer.length(), +- ) => +- { +- HostPageTexturePolicy::Shadow +- } +- policy => policy, +- }; ++ // A written texture in place under `InPlaceWrites` is rendered by the ++ // device's own texels alone, whatever its size: the driver renders a linear ++ // image over a host allocation whose size is not whole tile blocks without ++ // tile memory (the board's Turnip, `turnip-render-mode-for-attachment-memory`). ++ // Its shadow merged every pass's writes back through a scratch of twice its ++ // bytes (iPadOS 27's backboardd, a 3048x2032 RGBA16Float surface: 58 GB of ++ // scratch in 613 passes). + let verdict = |class| match checking { + true => Verdict::Passed, + false => self.in_place_verdict(class), From 0e63b28d97a1b3d39472128fd017d3b280e1ae4f Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Thu, 29 Oct 2026 17:46:43 +0800 Subject: [PATCH 72/92] feat(telemetry): RM row, how Turnip rendered the passes Turnip's tu_render_mode_stats counts GMEM passes, SYSMEM passes the autotuner chose and SYSMEM passes for another reason, with their pixels, and GMEM passes that load or store an unpadded attachment over its texels alone. The allocation-site rows keep three stacks to leave the table room. --- NativeBridgeMetalFrontend.mm | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 4dfcda8..de73564 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -4232,11 +4232,23 @@ extern "C" size_t native_bridge_metal_telemetry(char *out,size_t size) { (unsigned long long)mc[8],(unsigned long long)mc[9],(unsigned long long)(bc[5]>>20),(unsigned long long)(bc[6]>>20), (unsigned long long)bc[2],(unsigned long long)bc[3]); } - // Who allocated the device memory (4 MiB or more at once): the six call stacks with the most bytes, as MiB, allocations and return + // How Turnip rendered the passes (tu_render_mode_stats): GMEM passes and their Mpx, SYSMEM passes the autotuner chose and theirs, + // SYSMEM passes for another reason and theirs, then GMEM passes with an attachment loaded or stored over its texels alone. + { + uint64_t rm[8]={}; + static void *turnip=gLoaderPath.empty()?nullptr:dlopen(gLoaderPath.c_str(),RTLD_NOW|RTLD_NOLOAD); + static void (*modeStats)(uint64_t *)=turnip?(void (*)(uint64_t *))dlsym(turnip,"tu_render_mode_stats"):nullptr; + if(modeStats) { + modeStats(rm); + row("RM gmem %llu %lluM | sys auto %llu %lluM other %llu %lluM | exact %llu",(unsigned long long)rm[0],(unsigned long long)(rm[1]/1000), + (unsigned long long)rm[2],(unsigned long long)(rm[3]/1000),(unsigned long long)rm[4],(unsigned long long)(rm[5]/1000),(unsigned long long)rm[6]); + } + } + // Who allocated the device memory (4 MiB or more at once): the three call stacks with the most bytes, as MiB, allocations and return // addresses relative to their image (+offset in the plugin, whose link map NativeAdrenoMetal.map names them; *offset in another). { - uint64_t sites[6*12]={}; - const size_t n=nmb_allocation_sites(sites,6); + uint64_t sites[3*12]={}; + const size_t n=nmb_allocation_sites(sites,3); for(size_t i=0;i Date: Thu, 29 Oct 2026 18:33:17 +0800 Subject: [PATCH 73/92] 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. --- NativeAdrenoMetalPlugin.mm | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/NativeAdrenoMetalPlugin.mm b/NativeAdrenoMetalPlugin.mm index 8e6cc26..27aff5e 100644 --- a/NativeAdrenoMetalPlugin.mm +++ b/NativeAdrenoMetalPlugin.mm @@ -296,6 +296,12 @@ static id deviceForPort(unsigned port) { const uintptr_t slide = (uintptr_t)ptrauth_strip(start, ptrauth_key_function_pointer) - 0x1846dce88; const float one = 1.0f; gpu->mark(writeTask(0x1e7021014 + slide, &one, sizeof one) ? 0x6E01 : 0x6EF1); + // The override alone left the framebuffer RGhA (i14aef633e: four 48388 KiB swap surfaces, mark 6E01 written): + // IOMFBDisplay::framebuffer_pixel_format (24A446 0x184461838) takes RGhA when the headroom is above its FP16 threshold + // (__DATA_DIRTY 0x1e7021104, 0 unless set) and the panel's own headroom above 8, so a headroom of 1 still passes. The + // threshold is held at the largest float: no headroom reaches it. + const float never = __FLT_MAX__; + gpu->mark(writeTask(0x1e7021104 + slide, &never, sizeof never) ? 0x6E02 : 0x6EF2); } #endif // On an iPad the render server's queue runs at a high GPU priority (CoreAnimation asks for it with setGPUPriority: when it makes From dfe4eeb843270754eaaf205780414955db54d439 Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Thu, 29 Oct 2026 18:40:27 +0800 Subject: [PATCH 74/92] fix(metal): answer externFunctionNames as Metal does A library's externFunctionNames are its TYPE 5 entries (Apple's Metal on macOS 26.5.1: 228 for ci_stdlib, 612 for ci_filters, none for a stitchable library). iPadOS 27's Core Image collects the precompiled kernels' names so and links a kernel into its DAG only when its name is there or in a stitchable library. The frontend answered nil, so create_metal_dag gave a null program, the fallback is an empty stub on 27, and iconservicesagent crashed in create_program_and_arguments. --- NativeBridgeMetalFrontend.mm | 53 ++++++++++++++++++++++++++---------- bridge/coreimage_host.mm | 5 ++++ 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index de73564..4269fbd 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -1217,6 +1218,7 @@ static std::string quartzCoreCallers(); // without them create_metal_dag ended in a null program and iconservicesagent crashed in create_program_and_arguments. Such a function has // no runtime handle: it is stitched from its bitcode (functionBitcode) and refused as a pipeline's own function. static NBFunction *ciKernelFunction(NBLibrary *library,NSString *name); +static bool forEachLibraryEntry(NBLibrary *library,const std::function &visit); @implementation NBLibrary - (void)dealloc {nmb_library_release(_handle);} - (id)newFunctionWithName:(NSString *)name constantValues:(MTLFunctionConstantValues *)values error:(NSError **)error { @@ -1288,6 +1290,19 @@ static NBFunction *ciKernelFunction(NBLibrary *library,NSString *name); } return names; } +// _MTLLibrary's externFunctionNames: the entries of external linkage (TYPE 5), an empty array for a library without any. iPadOS 27's Core +// Image collects the precompiled kernels' names so (DAGPrecompiledKernels: [CIKernelLibrary externFunctionNames] of ci_stdlib and +// ci_filters) and links a kernel into its DAG only when its name is there or in a stitchable library; without the answer (nil, an unknown +// selector) create_metal_dag gave a null program and iconservicesagent crashed in create_program_and_arguments. Apple's Metal on macOS +// 26.5.1 answers 228 names for its ci_stdlib and 612 for ci_filters, none for a stitchable library. +- (NSArray *)externFunctionNames { + NSMutableArray *names=[NSMutableArray array]; + forEachLibraryEntry(self,[&](const std::string &entry,int type) { + if(type==5 && !entry.empty())if(NSString *name=@(entry.c_str()))[names addObject:name]; + return true; + }); + return names; +} - (id)newFunctionWithName:(NSString *)name { const uint64_t functionBegan=nowUs(); if(NBFunction *kernel=ciKernelFunction(self,name)){gTimeFunction.add(functionBegan);return (id)kernel;} @@ -1341,34 +1356,44 @@ static NSData *functionBitcode(NBFunction *function) { } return nil; } -static NBFunction *ciKernelFunction(NBLibrary *library,NSString *name) { - const uint8_t *file=library.bytes;const size_t length=library.length;const char *wanted=name.UTF8String; - if(!file || !wanted || length<88 || memcmp(file,"MTLB",4) || file[10]!=1)return nil; +// Each entry of a metallib's function list, in file order, as its name and TYPE (-1: none); false for a list that is not well formed. +static bool forEachLibraryEntry(NBLibrary *library,const std::function &visit) { + const uint8_t *file=library.bytes;const size_t length=library.length; + if(!file || length<88 || memcmp(file,"MTLB",4))return false; uint64_t listAt;memcpy(&listAt,file+24,8); - if(listAt>length-4)return nil; + if(listAt>length-4)return false; uint32_t count;memcpy(&count,file+listAt,4); size_t at=listAt+4; for(uint32_t i=0;ilength-4)return nil;memcpy(&size,file+at,4); - if(size<8 || size>length-at)return nil; + uint32_t size;if(at>length-4)return false;memcpy(&size,file+at,4); + if(size<8 || size>length-at)return false; const size_t end=at+size; std::string entry;int type=-1; for(size_t t=at+4;t+4<=end && memcmp(file+t,"ENDT",4);) { - if(t+6>end)return nil; + if(t+6>end)return false; uint16_t n;memcpy(&n,file+t+4,2);const uint8_t *v=file+t+6; - if(n>end-(t+6))return nil; + if(n>end-(t+6))return false; if(!memcmp(file+t,"NAME",4))entry.assign((const char *)v,strnlen((const char *)v,n)); else if(!memcmp(file+t,"TYPE",4) && n>=1)type=v[0]; t+=6+n; } - if(entry==wanted) { - if(type!=5)return nil; - NBFunction *function=[NBFunction new];function.name=name;function.functionType=(MTLFunctionType)4;function.library=library; - return function; - } + if(!visit(entry,type))return true; at=end; } - return nil; + return true; +} +static NBFunction *ciKernelFunction(NBLibrary *library,NSString *name) { + const uint8_t *file=library.bytes;const char *wanted=name.UTF8String; + if(!file || !wanted || library.length<88 || file[10]!=1)return nil; + NBFunction *found=nil; + forEachLibraryEntry(library,[&](const std::string &entry,int type) { + if(entry!=wanted)return true; + if(type==5) { + found=[NBFunction new];found.name=name;found.functionType=(MTLFunctionType)4;found.library=library; + } + return false; + }); + return found; } @interface NBComputePipeline : NBObject @property(nonatomic) NmbComputePipeline *handle; diff --git a/bridge/coreimage_host.mm b/bridge/coreimage_host.mm index 9e9ee71..af13c83 100644 --- a/bridge/coreimage_host.mm +++ b/bridge/coreimage_host.mm @@ -28,6 +28,11 @@ int main(int argc, char **argv) { std::printf("KERNELS library %s names %lu _add %s type %lu\n", kernels ? "yes" : "no", (unsigned long)kernels.functionNames.count, add ? "yes" : "no", (unsigned long)add.functionType); if (!add) {std::printf("FAIL kernel by name\n"); return 1;} + // iPadOS 27's Core Image links a kernel only when its name is among the library's externFunctionNames (its TYPE 5 entries; + // Apple's Metal answers 228 for macOS 26.5.1's ci_stdlib). + NSArray *externs = [(id)kernels respondsToSelector:@selector(externFunctionNames)] ? [(id)kernels performSelector:@selector(externFunctionNames)] : nil; + std::printf("EXTERNS %lu\n", (unsigned long)[externs count]); + if (![externs containsObject:@"_add"]) {std::printf("FAIL extern function names\n"); return 1;} // As Apple's Metal answers (macOS 26.5.1): no names, the kernel by name, function type 4. if (kernels.functionNames.count || add.functionType != 4) {std::printf("FAIL kernel library answers\n"); return 1;} } From d1b8b3372c1fe886a6834361b5b4cb286b7222b4 Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Thu, 29 Oct 2026 19:35:10 +0800 Subject: [PATCH 75/92] 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. --- CacheExport.h | 2 +- CacheImport.h | 157 ------------------ NativeAdrenoMetalPlugin.mm | 63 ++++--- NativeBridgeMetalFrontend.mm | 8 - PrimeShaders.h | 4 - bridge/NativeMetalObjects.h | 3 - bridge/NativeMetalObjects.rs | 28 ---- ...re-size-a-multiple-of-its-alignment.patch} | 0 .../0017-shader-cache-fallback-source.patch | 68 -------- ...s.patch => 0018-memory-census-bytes.patch} | 0 ...ites.patch => 0019-allocation-sites.patch} | 4 +- ...-page-textures-in-place-at-any-size.patch} | 0 bridge/prime_host.mm | 85 ---------- 13 files changed, 32 insertions(+), 390 deletions(-) delete mode 100644 CacheImport.h rename bridge/patches/{0018-heap-texture-size-a-multiple-of-its-alignment.patch => 0017-heap-texture-size-a-multiple-of-its-alignment.patch} (100%) delete mode 100644 bridge/patches/0017-shader-cache-fallback-source.patch rename bridge/patches/{0019-memory-census-bytes.patch => 0018-memory-census-bytes.patch} (100%) rename bridge/patches/{0020-allocation-sites.patch => 0019-allocation-sites.patch} (97%) rename bridge/patches/{0021-written-host-page-textures-in-place-at-any-size.patch => 0020-written-host-page-textures-in-place-at-any-size.patch} (100%) delete mode 100644 bridge/prime_host.mm diff --git a/CacheExport.h b/CacheExport.h index d3b92de..6b91f14 100644 --- a/CacheExport.h +++ b/CacheExport.h @@ -145,7 +145,7 @@ struct PackReader { using InflateFn = int (*)(z_streamp, int); using EndFn = int (*)(z_streamp); FILE *in = nullptr; - // Where the compressed bytes come from: the pack file, or the import region (importRegion). + // Where the compressed bytes come from (the pack file). std::function source; z_stream z = {}; InflateFn inflateFn = nullptr; diff --git a/CacheImport.h b/CacheImport.h deleted file mode 100644 index 3de4bd7..0000000 --- a/CacheImport.h +++ /dev/null @@ -1,157 +0,0 @@ -// SPDX-License-Identifier: MIT -// The pipeline caches compiled ahead on the host (UniXNU scripts/make-pipeline-cache.py), read on demand from the userdata import region -// (UniXNU-sheng scripts/make-pipeline-import.py) through the GPU driver's method 0x7101, which hands out 4096-byte blocks. The runtime asks -// for an entry only when its cache directory lacks it (nmb_set_shader_cache_fallback); nothing is copied into the directory, which lives on -// the RAM Data disk (int 590aaee2: unpacking the whole set there took 850 MB and starved the GPU of memory). What the render server -// compiles itself still goes to the directory. prime-done is read the same way, so priming skips every library the region covers. -// Layout: block 0 "SHNGIMP2", u32 version 2, u32 entry count, u64 index bytes, u64 data bytes, the SHA-256 of the index; the index -// from block 1 (per entry u64 data offset, u32 stored bytes, u32 bytes, u16 path bytes, the path; sorted by path); the data from the block -// after it, each entry zlib-compressed on its own. The index is checked against its digest once and kept in memory (a few MB); each entry's zlib stream checks its own bytes. -#pragma once -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "NativeAdrenoUserTest.h" - -namespace cacheimport { -constexpr uint32_t kMethod = 0x7101, kBlock = 4096; -// Read counters for the telemetry: entries served, bytes served (stored), misses, failed reads; and the first path the region lacked. -extern "C" {volatile uint64_t native_cache_import_stats[4]; char native_cache_import_first_miss[160];} - -struct Entry {uint64_t offset; uint32_t stored, bytes; std::string path;}; -static Gpu gLink; -static std::vector gEntries; -static uint64_t gDataFirst; // byte offset of the data in the region -static pthread_mutex_t gLock = PTHREAD_MUTEX_INITIALIZER; -// The last data block read: entries lie next to each other in path order, which is often the order they are asked in. -static uint64_t gCachedBlock = UINT64_MAX; -static uint8_t gCached[kBlock]; -// The region's size and its 4096-byte blocks: through the GPU driver on the tablet; a host check reads an image file (openFile). -static bool (*gRegionBlock)(uint64_t offset, uint8_t *out) = [](uint64_t offset, uint8_t *out) { - return gLink.method(kMethod, &offset, sizeof offset, out, kBlock) == 0; -}; -static FILE *gFile; -// The kernel refused the region with kIOReturnNotPermitted (boot argument sheng-gpu-prime=0): no import, and no priming either. -static bool gPrimingOff; - -static bool readBlock(uint64_t offset, uint8_t *out) { - if (offset == gCachedBlock) {memcpy(out, gCached, kBlock); return true;} - if (!gRegionBlock(offset, out)) return false; - gCachedBlock = offset; - memcpy(gCached, out, kBlock); - return true; -} -// `bytes` bytes of the region from `at` (any alignment) into out. -static bool readBytes(uint64_t at, size_t bytes, uint8_t *out) { - uint8_t block[kBlock]; - while (bytes) { - const uint64_t base = at & ~uint64_t(kBlock - 1); - const size_t skip = (size_t)(at - base), take = std::min(bytes, kBlock - skip); - if (!readBlock(base, block)) return false; - memcpy(out, block + skip, take); - out += take; at += take; bytes -= take; - } - return true; -} - -// The entry at `path` (relative to the cache directory), inflated into a malloc'd buffer the caller frees. -static bool read(const char *path, uint8_t **out, size_t *length) { - if (!path || !out || !length) return false; - const auto found = std::lower_bound(gEntries.begin(), gEntries.end(), path, [](const Entry &e, const char *p) {return e.path < p;}); - if (found == gEntries.end() || found->path != path) { - if (!native_cache_import_stats[2]++) strlcpy(native_cache_import_first_miss, path, sizeof native_cache_import_first_miss); - return false; - } - std::vector stored(found->stored); - pthread_mutex_lock(&gLock); - const bool got = readBytes(gDataFirst + found->offset, found->stored, stored.data()); - pthread_mutex_unlock(&gLock); - // zlib through dlopen, as CacheExport.h takes it: the plugin links no libz. - using UncompressFn = int (*)(Bytef *, uLongf *, const Bytef *, uLong); - static const UncompressFn inflateAll = [] { - void *z = dlopen("/usr/lib/libz.1.dylib", RTLD_NOW | RTLD_LOCAL); - return z ? (UncompressFn)dlsym(z, "uncompress") : nullptr; - }(); - uint8_t *data = static_cast(malloc(found->bytes ? found->bytes : 1)); - uLongf made = found->bytes; - if (!got || !data || !inflateAll || inflateAll(data, &made, stored.data(), found->stored) != Z_OK || made != found->bytes) { - free(data); - native_cache_import_stats[3] = native_cache_import_stats[3] + 1; - return false; - } - *out = data; - *length = made; - native_cache_import_stats[0] = native_cache_import_stats[0] + 1; - native_cache_import_stats[1] = native_cache_import_stats[1] + found->stored; - return true; -} - -static bool readIndex(uint64_t region, Gpu *marker); -// The host check's region: the payload of an import image (make-pipeline-import.py) written raw at `path`. -[[maybe_unused]] static bool openFile(const char *path) { - if (!(gFile = fopen(path, "rb"))) return false; - gRegionBlock = [](uint64_t offset, uint8_t *out) {return fseeko(gFile, (off_t)offset, SEEK_SET) == 0 && fread(out, 1, kBlock, gFile) == kBlock;}; - fseeko(gFile, 0, SEEK_END); - return readIndex((uint64_t)ftello(gFile), nullptr); -} - -// Opens the region and reads its index; false (nothing installed) without a region or with one that does not check. -// Markers 0x63nn: 00 started, 01 index ready (then 0x64nn: entries in thousands, saturated), f0 no connection, f1 no selector, -// f2 no region (or not this layout), f3 the index does not match its digest, f4 priming is off (sheng-gpu-prime=0). -static bool open(Gpu &marker) { - marker.mark(0x6300); - gLink.call = marker.call; gLink.openFn = marker.openFn; gLink.closeFn = marker.closeFn; gLink.port = marker.port; - if (!gLink.openFn || gLink.openFn(gLink.port, mach_task_self(), 0x4148, &gLink.conn) != 0 || !gLink.conn) {marker.mark(0x63f0); return false;} - if (!gLink.probe()) {marker.mark(0x63f1); return false;} - uint64_t region = 0; - const int answer = gLink.method(kMethod, ®ion, sizeof region, ®ion, sizeof region); - if (answer == (int)0xe00002e2) {gPrimingOff = true; marker.mark(0x63f4); return false;} // kIOReturnNotPermitted - if (answer != 0) {marker.mark(0x63f2); return false;} - return readIndex(region, &marker); -} -static bool readIndex(uint64_t region, Gpu *gpu) { - auto mark = [gpu](uint32_t value) {if (gpu) gpu->mark(value);}; - uint8_t block[kBlock]; - if (region < 2 * kBlock || !readBlock(0, block) || memcmp(block, "SHNGIMP2", 8)) {mark(0x63f2); return false;} - uint32_t version = 0, count = 0; - uint64_t indexBytes = 0, dataBytes = 0; - memcpy(&version, block + 8, 4); memcpy(&count, block + 12, 4); memcpy(&indexBytes, block + 16, 8); memcpy(&dataBytes, block + 24, 8); - if (version != 2 || !count || indexBytes % kBlock || kBlock + indexBytes + dataBytes > region) {mark(0x63f2); return false;} - uint8_t digest[32]; - memcpy(digest, block + 32, 32); - // The index, checked against its digest; an entry's own zlib stream checks its bytes when it is read. - std::vector index(indexBytes); - if (!readBytes(kBlock, indexBytes, index.data())) {mark(0x63f2); return false;} - CC_SHA256_CTX sha; - CC_SHA256_Init(&sha); - CC_SHA256_Update(&sha, index.data(), (CC_LONG)index.size()); - uint8_t got[32]; - CC_SHA256_Final(got, &sha); - if (memcmp(got, digest, 32)) {mark(0x63f3); return false;} - gEntries.reserve(count); - size_t at = 0; - for (uint32_t i = 0; i < count; ++i) { - if (at + 18 > index.size()) {mark(0x63f3); gEntries.clear(); return false;} - Entry e; - uint16_t pathBytes = 0; - memcpy(&e.offset, &index[at], 8); memcpy(&e.stored, &index[at + 8], 4); memcpy(&e.bytes, &index[at + 12], 4); - memcpy(&pathBytes, &index[at + 16], 2); - at += 18; - if (at + pathBytes > index.size() || e.offset + e.stored > dataBytes) {mark(0x63f3); gEntries.clear(); return false;} - e.path.assign(reinterpret_cast(&index[at]), pathBytes); - at += pathBytes; - gEntries.push_back(std::move(e)); - } - gDataFirst = kBlock + indexBytes; - mark(0x6301); - mark(0x6400 | (count / 1000 > 0xff ? 0xff : count / 1000)); - return true; -} -} // namespace cacheimport diff --git a/NativeAdrenoMetalPlugin.mm b/NativeAdrenoMetalPlugin.mm index 27aff5e..53f3ce0 100644 --- a/NativeAdrenoMetalPlugin.mm +++ b/NativeAdrenoMetalPlugin.mm @@ -22,7 +22,6 @@ #include "NativeMetalBridgePhase.h" #include "CacheExport.h" #include "PrimeShaders.h" -#include "CacheImport.h" #include "ExceptionDiag.h" @interface _MTLDevice : NSObject @@ -288,20 +287,37 @@ static id deviceForPort(unsigned port) { #if defined(__arm64e__) // iPadOS 27 picks a 64-bit half-float framebuffer ('RGhA') whenever the display's EDR headroom is above 1 (this panel reports XDR // headroom), which the display path can only show converted, slowly and greyed. As on iPadOS 17, where the render server drew plain - // 8-bit BGRA, the headroom is held at 1: the render server's headroom override (CA::WindowServer::Display::edr_headroom reads it - // first when nonzero; 24A446 QuartzCore __DATA_DIRTY 0x1e7021014, CARenderServerStart at 0x1846dce88 for the cache slide) and its - // environment spelling for an option table read after this. + // 8-bit BGRA, the headroom is held at 1. IOMFBDisplay::framebuffer_pixel_format (24A446 0x184461838, asked on every update) takes + // RGhA when the headroom (Display::edr_headroom, CA_DISPLAY_EDR_HEADROOM first when nonzero) is above CA_FP16_MIN_HEADROOM and + // the panel's own above 8, or when CA_FORCE_FP16_FRAMEBUFFER, CA_ALLOW_EDR_MIRRORING or the display's own flag says so. Both + // options are QuartzCore's float options: init_debug reads them from the environment into debug_values (index 21 at + // __DATA_DIRTY 0x1e7021014, index 81 at 0x1e7021104), so they are set in the environment for a read after this and written for + // one before it. i84edf75a1 wrote only the stored values (marks 6E01, 6E02) and the framebuffer stayed RGhA: an option read after + // the plugin loaded put the threshold back to 0. CADefaultsGetFloat takes the environment first (getenv, atof), then the defaults: + // a threshold of 1e30 is above any headroom. setenv("CA_DISPLAY_EDR_HEADROOM", "1", 1); + setenv("CA_FP16_MIN_HEADROOM", "1e30", 1); if (void *start = dlsym(RTLD_DEFAULT, "CARenderServerStart")) { const uintptr_t slide = (uintptr_t)ptrauth_strip(start, ptrauth_key_function_pointer) - 0x1846dce88; - const float one = 1.0f; + const float one = 1.0f, never = 1e30f; gpu->mark(writeTask(0x1e7021014 + slide, &one, sizeof one) ? 0x6E01 : 0x6EF1); - // The override alone left the framebuffer RGhA (i14aef633e: four 48388 KiB swap surfaces, mark 6E01 written): - // IOMFBDisplay::framebuffer_pixel_format (24A446 0x184461838) takes RGhA when the headroom is above its FP16 threshold - // (__DATA_DIRTY 0x1e7021104, 0 unless set) and the panel's own headroom above 8, so a headroom of 1 still passes. The - // threshold is held at the largest float: no headroom reaches it. - const float never = __FLT_MAX__; gpu->mark(writeTask(0x1e7021104 + slide, &never, sizeof never) ? 0x6E02 : 0x6EF2); + // What framebuffer_pixel_format reads, once the desktop has started: 0x6E10 | 1 headroom override 1, 2 threshold above 1, + // 4 CA_FORCE_FP16_FRAMEBUFFER (_ca_debug_options 0x1e702116c + 128), 8 CA_ALLOW_EDR_MIRRORING (+ 156). A framebuffer still + // RGhA with 0x6E13 is the display's own flag. + static uintptr_t gSlide; + gSlide = slide; + pthread_t reader; + pthread_create(&reader, nullptr, [](void *arg) -> void * { + sleep(30); + const float headroom = *reinterpret_cast(0x1e7021014 + gSlide); + const float threshold = *reinterpret_cast(0x1e7021104 + gSlide); + const uint8_t force = *reinterpret_cast(0x1e702116c + 128 + gSlide); + const uint8_t mirroring = *reinterpret_cast(0x1e702116c + 156 + gSlide); + static_cast(arg)->mark(0x6E10 | (headroom == 1.0f ? 1 : 0) | (threshold > 1.0f ? 2 : 0) | (force ? 4 : 0) | (mirroring ? 8 : 0)); + return nullptr; + }, gpu); + pthread_detach(reader); } #endif // On an iPad the render server's queue runs at a high GPU priority (CoreAnimation asks for it with setGPUPriority: when it makes @@ -333,29 +349,10 @@ static id deviceForPort(unsigned port) { strlcat(candidates[i], "m2v", sizeof candidates[i]); mkdir(candidates[i], 0700); if (access(candidates[i], W_OK) != 0) continue; - // The pipeline caches compiled ahead on the host, read on demand from the userdata import region behind this directory - // (CacheImport.h), and the files an earlier image's copying import left half written there (int 590aaee2), which hold memory - // of the RAM Data disk. + // Files an earlier image's copying import left half written here (int 590aaee2), which hold memory of the Data disk. { static bool once; - if (!once) { - once = true; - removeHalfImported(candidates[i]); - if (cacheimport::open(*gpu)) { - nmb_set_shader_cache_fallback(cacheimport::read); - uint8_t *done = nullptr; - size_t length = 0; - if (cacheimport::read("prime-done", &done, &length)) { - const std::string text(reinterpret_cast(done), length); - free(done); - for (size_t at = 0, end; at < text.size(); at = end + 1) { - end = text.find('\n', at); - if (end == std::string::npos) end = text.size(); - if (end > at) prime::extraDone().insert(text.substr(at, end - at)); - } - } - } - } + if (!once) {once = true; removeHalfImported(candidates[i]);} } // The image's pipeline pack (an earlier boot's export), into this directory before the runtime reads it. Marker 0x6Cnn: files written // (saturated at 0xfe), 0x6Cff the pack was refused. @@ -460,11 +457,9 @@ static id deviceForPort(unsigned port) { // Every Metal library of the OS through the runtime, 90 s after the render server opened the device (PrimeShaders.h); the boots that froze // or crashed before three minutes never primed anything. Progress is kept in the Data cache directory, so later boots resume. // The translator recurses deeply on large functions: give it the stack of a main thread. - // Not with sheng-gpu-prime=0 (cacheimport::gPrimingOff): pipelines are then compiled only as processes ask for them. pthread_attr_setstacksize(&attributes, 8u << 20); pthread_t primer; - if (!cacheimport::gPrimingOff) - pthread_create(&primer, &attributes, [](void *) -> void * { + pthread_create(&primer, &attributes, [](void *) -> void * { char list[1024]; prime::run((id)device, gCacheDirectory, 90, requestedPath("NativeAdrenoMetal.primelist", list, sizeof list) ? std::string(list) : std::string()); diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 4269fbd..9672a08 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -120,8 +120,6 @@ static void abortProbe(int signal, siginfo_t *si, void *context) { } // NATIVE_METAL_TIMING=1: the telemetry table (where the CPU side's time went: pipelines, libraries, compiles and the shader cache) // written to standard error every 20 s (an app the system ends at the end of a test never reaches atexit). -// The plugin's on-demand region reads (CacheImport.h). Weak here: the plugin's own definition wins, and a host build reads zeros. -extern "C" {__attribute__((weak)) volatile uint64_t native_cache_import_stats[4]; __attribute__((weak)) char native_cache_import_first_miss[160];} extern "C" size_t native_bridge_metal_telemetry(char *out,size_t size); static void *reportTiming(void *) { static char table[6144]; @@ -4290,12 +4288,6 @@ extern "C" size_t native_bridge_metal_telemetry(char *out,size_t size) { row("MA%zub %s %s %s %s %s",i,frames[5],frames[6],frames[7],frames[8],frames[9]); } } - // The pipeline caches compiled ahead on the host, read on demand from the userdata import region (the plugin's CacheImport.h). - if((native_cache_import_stats[0]||native_cache_import_stats[2]||native_cache_import_stats[3])) { - row("CI hit%llu KiB%llu miss%llu fail%llu",(unsigned long long)native_cache_import_stats[0],(unsigned long long)(native_cache_import_stats[1]>>10), - (unsigned long long)native_cache_import_stats[2],(unsigned long long)native_cache_import_stats[3]); - if(native_cache_import_first_miss[0])row("CIm %.86s",native_cache_import_first_miss+(strlen(native_cache_import_first_miss)>86?strlen(native_cache_import_first_miss)-86:0)); - } char warning[200];size_t n=nmb_last_warning(warning,sizeof warning); if(n){row("W %.86s",warning);if(n>86)row("Wb %.86s",warning+86);} } diff --git a/PrimeShaders.h b/PrimeShaders.h index dc3d088..530e0ed 100644 --- a/PrimeShaders.h +++ b/PrimeShaders.h @@ -289,9 +289,6 @@ static std::string library(id device, const std::string &path, const return path + line + firstRefusal; } -// Libraries finished elsewhere (the pipeline caches compiled ahead on the host, CacheImport.h), in prime-done's line form. -static std::set &extraDone() {static std::set lines; return lines;} - // The thread: waits for the desktop to settle, then works through the libraries not finished yet. static void run(id device, std::string root, unsigned delaySeconds, const std::string &listPath) { pthread_set_qos_class_self_np(QOS_CLASS_BACKGROUND, 0); @@ -305,7 +302,6 @@ static void run(id device, std::string root, unsigned delaySeconds, c sleep(delaySeconds); const std::set bad = readLines(badPath); std::set done = readLines(donePath); - done.insert(extraDone().begin(), extraDone().end()); const uint64_t began = nowMs(); unsigned libs = 0, finished = 0; std::string note; diff --git a/bridge/NativeMetalObjects.h b/bridge/NativeMetalObjects.h index 86864a2..42f6c6b 100644 --- a/bridge/NativeMetalObjects.h +++ b/bridge/NativeMetalObjects.h @@ -62,9 +62,6 @@ struct NmbShaderCacheStats { uint64_t driver_loads, driver_hits, driver_misses, driver_saves, driver_truncated, driver_mismatched, driver_unchanged; }; bool nmb_device_shader_cache_stats(const NmbDevice *, struct NmbShaderCacheStats *); -/* The read-only source of shader-cache entries behind every cache directory, set before the device opens: read(path relative to the - * directory, out, length) puts the entry in a malloc'd buffer the bridge frees and returns true, or returns false. */ -bool nmb_set_shader_cache_fallback(bool (*read)(const char *path, uint8_t **out, size_t *length)); size_t nmb_last_warning(char *out, size_t size); bool nmb_device_max_threads_per_threadgroup(const NmbDevice *, uint64_t out[3]); /* Device queries the runtime answers by Metal selector: Apple's private counts and flags where the device has an answer of its own, diff --git a/bridge/NativeMetalObjects.rs b/bridge/NativeMetalObjects.rs index d0c6c29..f8e0a78 100644 --- a/bridge/NativeMetalObjects.rs +++ b/bridge/NativeMetalObjects.rs @@ -407,34 +407,6 @@ pub struct NmbShaderCacheStats { pub driver_unchanged: u64, } -/// The caller's read-only source of shader-cache entries behind the cache directory: `read(path, out, length)` gives the entry at -/// `path` (relative to the directory) in a buffer from malloc (the bridge frees it) and returns true, or returns false. -type FallbackRead = unsafe extern "C" fn(*const c_char, *mut *mut u8, *mut usize) -> bool; -static FALLBACK_READ: std::sync::OnceLock = std::sync::OnceLock::new(); - -fn fallback_read(path: &str, out: &mut Vec) -> bool { - let (Some(read), Ok(path)) = (FALLBACK_READ.get(), std::ffi::CString::new(path)) else { return false }; - let (mut data, mut length) = (std::ptr::null_mut::(), 0usize); - // SAFETY: the caller's contract: on true, `data` holds `length` bytes from malloc, which are freed here. - unsafe { - if !read(path.as_ptr(), &mut data, &mut length) || data.is_null() { - return false; - } - out.extend_from_slice(std::slice::from_raw_parts(data, length)); - libc::free(data.cast()); - } - true -} - -/// Set the read-only source behind every shader-cache directory, before the device opens; false where one was set already. -#[unsafe(no_mangle)] -pub extern "C" fn nmb_set_shader_cache_fallback(read: Option) -> bool { - match read { - Some(read) => FALLBACK_READ.set(read).is_ok() && metal2vulkan_runtime::set_shader_cache_fallback(fallback_read), - None => false, - } -} - #[unsafe(no_mangle)] pub extern "C" fn nmb_device_shader_cache_stats(device: *const NmbDevice, out: *mut NmbShaderCacheStats) -> bool { guard(false, || { diff --git a/bridge/patches/0018-heap-texture-size-a-multiple-of-its-alignment.patch b/bridge/patches/0017-heap-texture-size-a-multiple-of-its-alignment.patch similarity index 100% rename from bridge/patches/0018-heap-texture-size-a-multiple-of-its-alignment.patch rename to bridge/patches/0017-heap-texture-size-a-multiple-of-its-alignment.patch diff --git a/bridge/patches/0017-shader-cache-fallback-source.patch b/bridge/patches/0017-shader-cache-fallback-source.patch deleted file mode 100644 index 1dc8cb4..0000000 --- a/bridge/patches/0017-shader-cache-fallback-source.patch +++ /dev/null @@ -1,68 +0,0 @@ -diff --git a/crates/metal2vulkan-runtime/src/lib.rs b/crates/metal2vulkan-runtime/src/lib.rs -index ae78ee38..6cd9d58d 100644 ---- a/crates/metal2vulkan-runtime/src/lib.rs -+++ b/crates/metal2vulkan-runtime/src/lib.rs -@@ -336,7 +336,7 @@ pub use render_values::{ - pub use resource::{ObjectId, ResourceRef}; - pub use resource_ids::{IdPlacement, ResourceId, ResourceIdKind}; - pub use sampler::{sampler_border_color, SamplerState, SamplerStateOptions}; --pub use shader_cache::{ -+pub use shader_cache::{set_shader_cache_fallback, ShaderCacheFallback, - ShaderCacheOptions, ShaderCacheStatistics, DEFAULT_CAPACITY, DEFAULT_MEMORY_CAPACITY, - }; - pub use sparse::device::SparseFeatures; -diff --git a/crates/metal2vulkan-runtime/src/shader_cache/mod.rs b/crates/metal2vulkan-runtime/src/shader_cache/mod.rs -index a9da76d1..143b665d 100644 ---- a/crates/metal2vulkan-runtime/src/shader_cache/mod.rs -+++ b/crates/metal2vulkan-runtime/src/shader_cache/mod.rs -@@ -70,6 +70,7 @@ pub(crate) use creation::VariantStages; - pub(crate) mod driver; - mod held; - pub(super) mod store; -+pub use store::{set_fallback as set_shader_cache_fallback, Fallback as ShaderCacheFallback}; - pub(super) mod worker; - - use super::error::{invalid, Error, Problem, ShaderCacheError}; -diff --git a/crates/metal2vulkan-runtime/src/shader_cache/store.rs b/crates/metal2vulkan-runtime/src/shader_cache/store.rs -index 38742f62..0f6fc951 100644 ---- a/crates/metal2vulkan-runtime/src/shader_cache/store.rs -+++ b/crates/metal2vulkan-runtime/src/shader_cache/store.rs -@@ -569,7 +569,14 @@ impl Store { - timed(&self.counters.read_nanos, || { - let file = match fs::File::open(path) { - Ok(file) => file, -- Err(error) if error.kind() == io::ErrorKind::NotFound => return Found::Absent, -+ Err(error) if error.kind() == io::ErrorKind::NotFound => { -+ // An entry the directory lacks, from the read-only source behind it (`set_fallback`), as a queued -+ // write's bytes are: no file to refresh. -+ return match fallback_read(self.directory.root(), path) { -+ Some(bytes) => Found::Queued(bytes), -+ None => Found::Absent, -+ }; -+ } - Err(_) => return Found::Failed, - }; - let Ok(length) = file.metadata().map(|metadata| metadata.len()) else { -@@ -885,3 +892,22 @@ fn new_private_file(path: &Path) -> io::Result { - } - options.open(path) - } -+ -+/// A read-only source of entries behind every cache directory: given an entry's path relative to its directory's root, its bytes, -+/// or false. A process whose entries were made ahead (on another machine, for the same driver and runtime) reads them from there -+/// without copying them into the directory; what it makes itself still goes to the directory. Set once, before any device opens. -+pub type Fallback = fn(&str, &mut Vec) -> bool; -+static FALLBACK: std::sync::OnceLock = std::sync::OnceLock::new(); -+ -+/// Set the read-only source behind every cache directory ([`Fallback`]); false where one was set already. -+pub fn set_fallback(source: Fallback) -> bool { -+ FALLBACK.set(source).is_ok() -+} -+ -+/// The fallback's bytes of the entry at `path` under `root`. -+fn fallback_read(root: &Path, path: &Path) -> Option> { -+ let source = FALLBACK.get()?; -+ let relative = path.strip_prefix(root).ok()?.to_str()?; -+ let mut bytes = Vec::new(); -+ source(relative, &mut bytes).then_some(bytes) -+} diff --git a/bridge/patches/0019-memory-census-bytes.patch b/bridge/patches/0018-memory-census-bytes.patch similarity index 100% rename from bridge/patches/0019-memory-census-bytes.patch rename to bridge/patches/0018-memory-census-bytes.patch diff --git a/bridge/patches/0020-allocation-sites.patch b/bridge/patches/0019-allocation-sites.patch similarity index 97% rename from bridge/patches/0020-allocation-sites.patch rename to bridge/patches/0019-allocation-sites.patch index cb58939..b1a2aef 100644 --- a/bridge/patches/0020-allocation-sites.patch +++ b/bridge/patches/0019-allocation-sites.patch @@ -70,7 +70,7 @@ index 1d760a06..bbb1fb47 100644 /// /// # Safety diff --git a/crates/metal2vulkan-runtime/src/lib.rs b/crates/metal2vulkan-runtime/src/lib.rs -index 6cd9d58d..041567d2 100644 +index ae78ee38..400f6882 100644 --- a/crates/metal2vulkan-runtime/src/lib.rs +++ b/crates/metal2vulkan-runtime/src/lib.rs @@ -336,6 +336,7 @@ pub use render_values::{ @@ -78,7 +78,7 @@ index 6cd9d58d..041567d2 100644 pub use resource_ids::{IdPlacement, ResourceId, ResourceIdKind}; pub use sampler::{sampler_border_color, SamplerState, SamplerStateOptions}; +pub use creation::{allocation_sites, AllocationSite}; - pub use shader_cache::{set_shader_cache_fallback, ShaderCacheFallback, + pub use shader_cache::{ ShaderCacheOptions, ShaderCacheStatistics, DEFAULT_CAPACITY, DEFAULT_MEMORY_CAPACITY, }; diff --git a/crates/metal2vulkan-runtime/src/work_counts.rs b/crates/metal2vulkan-runtime/src/work_counts.rs diff --git a/bridge/patches/0021-written-host-page-textures-in-place-at-any-size.patch b/bridge/patches/0020-written-host-page-textures-in-place-at-any-size.patch similarity index 100% rename from bridge/patches/0021-written-host-page-textures-in-place-at-any-size.patch rename to bridge/patches/0020-written-host-page-textures-in-place-at-any-size.patch diff --git a/bridge/prime_host.mm b/bridge/prime_host.mm deleted file mode 100644 index 9108ddc..0000000 --- a/bridge/prime_host.mm +++ /dev/null @@ -1,85 +0,0 @@ -// SPDX-License-Identifier: MIT -// Shader priming ahead of the tablet: the plugin's own priming (PrimeShaders.h) over the OS image's libraries on a local mount, through the -// frontend and the runtime over the tablet's driver (Turnip for the Adreno 740, its kernel answered by the host copy of the kernel stack, -// tests/hostsim). Turnip compiles for the GPU without running anything on it, so the cache directory this fills is what the render server's -// own priming would have written; scripts/make-pipeline-cache.py packs it into the image. Each finished library is listed in prime-done under -// the tablet's id for it (its path there, size and mtime, the runtime's build and the tablet Turnip's LC_UUID), so the render server does not -// prime it again. -// argv: loader (host Turnip), cache directory, library list (tablet paths), OS root, OS cryptex root, the tablet's Turnip dylib, [first, count] -#include "../PrimeShaders.h" -#include "../NativeBridgeMetalFrontend.h" -#include "NativeMetalObjects.h" -#include "../CacheImport.h" -#include - -static void onMissing(const char *, const char *) {} - -// The LC_UUID of the Mach-O file at `path` (thin), "unknown" without one: the tablet's Turnip, which ends the tablet's prime-done ids. -static std::string fileUuid(const char *path) { - std::ifstream in(path, std::ios::binary); - std::vector bytes((std::istreambuf_iterator(in)), std::istreambuf_iterator()); - if (bytes.size() < sizeof(mach_header_64)) return "unknown"; - const auto *header = reinterpret_cast(bytes.data()); - if (header->magic != MH_MAGIC_64) return "unknown"; - size_t at = sizeof(mach_header_64); - for (uint32_t i = 0; i < header->ncmds && at + sizeof(load_command) <= bytes.size(); ++i) { - const auto *command = reinterpret_cast(bytes.data() + at); - if (command->cmd == LC_UUID && at + sizeof(uuid_command) <= bytes.size()) { - const uint8_t *uuid = reinterpret_cast(command)->uuid; - char text[33]; - for (int k = 0; k < 16; ++k) snprintf(text + 2 * k, 3, "%02x", uuid[k]); - return text; - } - at += command->cmdsize; - } - return "unknown"; -} - -int main(int argc, char **argv) { - @autoreleasepool { - if (argc != 7 && argc != 9) return 2; - // NMB_IMPORT_PAYLOAD: the import region's bytes (make-pipeline-import.py --payload) behind the cache directory, read on demand as - // the tablet's plugin reads the region (CacheImport.h): a consumer run over an empty directory then compiles nothing it covers. - if (const char *payload = getenv("NMB_IMPORT_PAYLOAD")) { - if (!cacheimport::openFile(payload) || !nmb_set_shader_cache_fallback(cacheimport::read)) {std::printf("FAIL import payload\n"); return 1;} - } - id device = native_bridge_metal_open(argv[1], argv[2], 0x6e6d70, onMissing); - if (!device) {std::printf("FAIL device\n"); return 1;} - native_bridge_metal_quiet_problems(true); - const std::string root = argv[2], osRoot = argv[4], cryptexRoot = argv[5], cryptex = "/System/Cryptexes/OS"; - const std::string build = std::string(prime::nmb_runtime_build()) + "-" + fileUuid(argv[6]); - std::vector paths; - {std::ifstream list(argv[3]); for (std::string line; std::getline(list, line);) if (!line.empty() && line[0] == '/') paths.push_back(line);} - size_t first = 0, count = paths.size(); - if (argc == 9) {first = strtoul(argv[7], nullptr, 10); count = strtoul(argv[8], nullptr, 10);} - const std::set bad = prime::readLines(root + "/prime-bad"); - const std::string current = root + "/prime-current", log = root + "/prime.log", done = root + "/prime-done"; - unsigned primed = 0, absent = 0; - for (size_t i = first; i < paths.size() && i < first + count; ++i) { - const std::string &path = paths[i]; - const std::string local = path.compare(0, cryptex.size(), cryptex) == 0 ? cryptexRoot + path.substr(cryptex.size()) : osRoot + path; - struct stat st = {}; - if (stat(local.c_str(), &st) != 0) {++absent; prime::appendLine(log, path + " absent from the image mount"); continue;} - prime::appendLine(log, prime::library(device, local, current, bad)); - prime::appendLine(done, path + "\t" + std::to_string((long long)st.st_size) + "\t" + - std::to_string((long long)st.st_mtimespec.tv_sec) + "\t" + build); - ++primed; - } - // The cache thread writes the entries after the calls returned: wait until its write count has stood still for 10 s. - NmbShaderCacheStats stats = {}; - uint64_t last = UINT64_MAX; - for (unsigned still = 0; still < 20; usleep(500000)) { - native_bridge_metal_shader_cache_stats(&stats); - const uint64_t now = stats.writes + stats.driver_saves; - still = now == last ? still + 1 : 0; - last = now; - } - std::printf("PASS: primed %u libraries (%u absent) made=%u writes=%llu driver_saves=%llu hits=%llu misses=%llu driver_hits=%llu " - "driver_misses=%llu import=%llu/%llu/%llu build=%s\n", primed, absent, (unsigned)native_prime_made, - (unsigned long long)stats.writes, (unsigned long long)stats.driver_saves, (unsigned long long)stats.hits, - (unsigned long long)stats.misses, (unsigned long long)stats.driver_hits, (unsigned long long)stats.driver_misses, - (unsigned long long)cacheimport::native_cache_import_stats[0], (unsigned long long)cacheimport::native_cache_import_stats[2], - (unsigned long long)cacheimport::native_cache_import_stats[3], build.c_str()); - return 0; - } -} From 44d2621379bf460842a7ab655291d6913e45c805 Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Thu, 29 Oct 2026 19:43:33 +0800 Subject: [PATCH 76/92] feat(telemetry): TB row, textures over host pages at rows the GPU cannot take A texture over a no-copy buffer whose rows or offset are not whole 64-byte steps (fd6_layout's linear pitch) is kept as a shadow and copied in. The row counts them with their bytes and names the largest one's size and caller (image + offset), so the next step can follow the caller instead of a guess (i84edf75a1: a 3048x2032 BGRA texture at 12192-byte rows, 498 pitch refusals, 118260 copies in). --- NativeBridgeMetalFrontend.mm | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 9672a08..94c8708 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -1122,8 +1122,30 @@ static NBTexture *textureOnSurface(NBBuffer *buffer,MTLTextureDescriptor *d,IOSu NBTexture *texture=[NBTexture new];texture.handle=handle;nmb_texture_describe(handle,&descriptor);texture.descriptor=descriptor; texture.buffer=buffer;texture.offset=offset;texture.rowBytes=row;return texture; } +// Textures over host pages whose rows or offset are not whole 64-byte steps, which no linear image of this GPU lays out (fd6_layout: a +// linear pitch of 64 bytes): the runtime keeps them as shadows and copies their bytes in. Count, bytes, and the largest one's size and +// caller, for the telemetry's TB row. +static std::mutex gUnalignedLock; +static uint64_t gUnalignedCount,gUnalignedBytes,gUnalignedLargest,gUnalignedCaller; +static uint32_t gUnalignedSize[3]; +static char gUnalignedImage[24]; +static void noteUnalignedTexture(MTLTextureDescriptor *d,NSUInteger offset,NSUInteger row,const void *caller) { + if(!(row%64) && !(offset%64))return; + const uint64_t bytes=(uint64_t)row*d.height*(d.depth?d.depth:1); + std::lock_guard lock(gUnalignedLock); + gUnalignedCount++;gUnalignedBytes+=bytes; + if(bytes<=gUnalignedLargest)return; + gUnalignedLargest=bytes;gUnalignedSize[0]=(uint32_t)d.width;gUnalignedSize[1]=(uint32_t)d.height;gUnalignedSize[2]=(uint32_t)row; + Dl_info info={}; + if(dladdr(caller,&info) && info.dli_fbase) { + gUnalignedCaller=(uint64_t)caller-(uint64_t)info.dli_fbase; + const char *name=info.dli_fname?strrchr(info.dli_fname,'/'):nullptr; + strlcpy(gUnalignedImage,name?name+1:(info.dli_fname?info.dli_fname:"?"),sizeof gUnalignedImage); + } else {gUnalignedCaller=(uint64_t)caller;gUnalignedImage[0]=0;} +} @implementation NBBuffer (Textures) - (id)newTextureWithDescriptor:(MTLTextureDescriptor *)d offset:(NSUInteger)offset bytesPerRow:(NSUInteger)row { + if(self.wrapped)noteUnalignedTexture(d,offset,row,__builtin_return_address(0)); NBTexture *texture=textureOnBuffer(self,d,offset,row,false); if(gLogTextures && self.wrapped)logTexture("no-copy",d,nullptr,0,self.wrapped,offset,row,texture!=nil); return (id)texture; @@ -4233,6 +4255,13 @@ extern "C" size_t native_bridge_metal_telemetry(char *out,size_t size) { if(nmb_device_texture_counts(gDevice,tc)) row("L inp %llu/%llu rf %llu/%llu sh %llu cp %llu bk %llu rt %llu",(unsigned long long)tc[0],(unsigned long long)tc[1],(unsigned long long)tc[2], (unsigned long long)tc[3],(unsigned long long)tc[4],(unsigned long long)tc[5],(unsigned long long)tc[6],(unsigned long long)tc[7]); + // Textures over host pages at rows or offsets this GPU's linear images cannot take: how many, MiB, the largest and who made it. + { + std::lock_guard lock(gUnalignedLock); + if(gUnalignedCount) + row("TB n%llu %lluM big %ux%u row%u %.20s+%llx",(unsigned long long)gUnalignedCount,(unsigned long long)(gUnalignedBytes>>20), + gUnalignedSize[0],gUnalignedSize[1],gUnalignedSize[2],gUnalignedImage,(unsigned long long)gUnalignedCaller); + } // Where passes cost more than one pass in tile memory: passes begun as instances, split inline; instance splits for a barrier, tile // memory, a memoryless write, a pending pipeline; stand-ins made and MiB; stand-in copies, of them at draws. uint64_t pc[10]={}; From 805659bd361ef3b451f9f1fdc1442df6ceb40b7c Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Thu, 29 Oct 2026 20:01:51 +0800 Subject: [PATCH 77/92] 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. --- NativeAdrenoMetalPlugin.mm | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/NativeAdrenoMetalPlugin.mm b/NativeAdrenoMetalPlugin.mm index 53f3ce0..4c0b420 100644 --- a/NativeAdrenoMetalPlugin.mm +++ b/NativeAdrenoMetalPlugin.mm @@ -295,29 +295,35 @@ static id deviceForPort(unsigned port) { // one before it. i84edf75a1 wrote only the stored values (marks 6E01, 6E02) and the framebuffer stayed RGhA: an option read after // the plugin loaded put the threshold back to 0. CADefaultsGetFloat takes the environment first (getenv, atof), then the defaults: // a threshold of 1e30 is above any headroom. + // id02fe1633 read back 0x6E19 in one process: CA_ALLOW_EDR_MIRRORING set and the threshold not raised (a process whose options + // were read after the write). The mirroring option is held off the same two ways (bool option 156 at _ca_debug_options + // 0x1e702116c + 156; init_debug stores value != 0). setenv("CA_DISPLAY_EDR_HEADROOM", "1", 1); setenv("CA_FP16_MIN_HEADROOM", "1e30", 1); + setenv("CA_ALLOW_EDR_MIRRORING", "0", 1); if (void *start = dlsym(RTLD_DEFAULT, "CARenderServerStart")) { const uintptr_t slide = (uintptr_t)ptrauth_strip(start, ptrauth_key_function_pointer) - 0x1846dce88; const float one = 1.0f, never = 1e30f; gpu->mark(writeTask(0x1e7021014 + slide, &one, sizeof one) ? 0x6E01 : 0x6EF1); gpu->mark(writeTask(0x1e7021104 + slide, &never, sizeof never) ? 0x6E02 : 0x6EF2); - // What framebuffer_pixel_format reads, once the desktop has started: 0x6E10 | 1 headroom override 1, 2 threshold above 1, - // 4 CA_FORCE_FP16_FRAMEBUFFER (_ca_debug_options 0x1e702116c + 128), 8 CA_ALLOW_EDR_MIRRORING (+ 156). A framebuffer still - // RGhA with 0x6E13 is the display's own flag. + const uint8_t off = 0; + gpu->mark(writeTask(0x1e702116c + 156 + slide, &off, sizeof off) ? 0x6E03 : 0x6EF3); + // What framebuffer_pixel_format reads in the render server, once the desktop has started (other processes draw no + // framebuffer): 0x6E20 | 1 headroom override 1, 2 threshold above 1, 4 CA_FORCE_FP16_FRAMEBUFFER (_ca_debug_options + // 0x1e702116c + 128), 8 CA_ALLOW_EDR_MIRRORING (+ 156). A framebuffer still RGhA with 0x6E23 is the display's own flag. static uintptr_t gSlide; gSlide = slide; pthread_t reader; - pthread_create(&reader, nullptr, [](void *arg) -> void * { + if (!strcmp(getprogname(), "backboardd") && pthread_create(&reader, nullptr, [](void *arg) -> void * { sleep(30); const float headroom = *reinterpret_cast(0x1e7021014 + gSlide); const float threshold = *reinterpret_cast(0x1e7021104 + gSlide); const uint8_t force = *reinterpret_cast(0x1e702116c + 128 + gSlide); const uint8_t mirroring = *reinterpret_cast(0x1e702116c + 156 + gSlide); - static_cast(arg)->mark(0x6E10 | (headroom == 1.0f ? 1 : 0) | (threshold > 1.0f ? 2 : 0) | (force ? 4 : 0) | (mirroring ? 8 : 0)); + static_cast(arg)->mark(0x6E20 | (headroom == 1.0f ? 1 : 0) | (threshold > 1.0f ? 2 : 0) | (force ? 4 : 0) | (mirroring ? 8 : 0)); return nullptr; - }, gpu); - pthread_detach(reader); + }, gpu) == 0) + pthread_detach(reader); } #endif // On an iPad the render server's queue runs at a high GPU priority (CoreAnimation asks for it with setGPUPriority: when it makes From 547312cf58156f7abe5f83a3376a14f5cb157884 Mon Sep 17 00:00:00 2001 From: bluseliu50 <265648257+bluseliu50@users.noreply.github.com> Date: Thu, 29 Oct 2026 21:05:15 +0800 Subject: [PATCH 78/92] 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. --- NativeAdrenoMetalPlugin.mm | 19 +++++++++++++++++-- NativeBridgeMetalFrontend.h | 2 ++ NativeBridgeMetalFrontend.mm | 17 ++++++++++++----- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/NativeAdrenoMetalPlugin.mm b/NativeAdrenoMetalPlugin.mm index 4c0b420..1c93dc6 100644 --- a/NativeAdrenoMetalPlugin.mm +++ b/NativeAdrenoMetalPlugin.mm @@ -310,7 +310,11 @@ static id deviceForPort(unsigned port) { gpu->mark(writeTask(0x1e702116c + 156 + slide, &off, sizeof off) ? 0x6E03 : 0x6EF3); // What framebuffer_pixel_format reads in the render server, once the desktop has started (other processes draw no // framebuffer): 0x6E20 | 1 headroom override 1, 2 threshold above 1, 4 CA_FORCE_FP16_FRAMEBUFFER (_ca_debug_options - // 0x1e702116c + 128), 8 CA_ALLOW_EDR_MIRRORING (+ 156). A framebuffer still RGhA with 0x6E23 is the display's own flag. + // 0x1e702116c + 128), 8 CA_ALLOW_EDR_MIRRORING (+ 156), 0x10 the device-is-virtualized byte (0x1e701bdd9). + // i35abaf83 read 0x6E23 and the framebuffer stayed RGhA (48388K swap surfaces, 8 bytes a pixel). The function has two more + // ways to RGhA: the display's own flag (+0x6840 of the display) and, when no compression format was picked, the virtualized + // byte, which sub_184541f10 stores once from a call with the constant CFString at 0x1e9021680; the string's text is noted + // ("ca virtualized key ...") so the answer behind it can be found. static uintptr_t gSlide; gSlide = slide; pthread_t reader; @@ -320,7 +324,18 @@ static id deviceForPort(unsigned port) { const float threshold = *reinterpret_cast(0x1e7021104 + gSlide); const uint8_t force = *reinterpret_cast(0x1e702116c + 128 + gSlide); const uint8_t mirroring = *reinterpret_cast(0x1e702116c + 156 + gSlide); - static_cast(arg)->mark(0x6E20 | (headroom == 1.0f ? 1 : 0) | (threshold > 1.0f ? 2 : 0) | (force ? 4 : 0) | (mirroring ? 8 : 0)); + const uint8_t virtualized = *reinterpret_cast(0x1e701bdd9 + gSlide); + static_cast(arg)->mark(0x6E20 | (headroom == 1.0f ? 1 : 0) | (threshold > 1.0f ? 2 : 0) | (force ? 4 : 0) | (mirroring ? 8 : 0) | + (virtualized ? 0x10 : 0)); + // A constant CFString: isa, flags, bytes, length. Only an 8-bit constant (flags 0x7c8) with a short length is read. + const uint64_t *cf = reinterpret_cast(0x1e9021680 + gSlide); + char note[96]; + if ((uint32_t)cf[1] == 0x7c8 && cf[3] > 0 && cf[3] < 64) { + const char *text = reinterpret_cast(ptrauth_strip(reinterpret_cast(cf[2]), ptrauth_key_process_independent_data)); + snprintf(note, sizeof note, "ca virtualized key %.*s = %u", (int)cf[3], text, virtualized); + } else + snprintf(note, sizeof note, "ca virtualized key ? flags %llx = %u", (unsigned long long)cf[1], virtualized); + native_bridge_metal_note(note); return nullptr; }, gpu) == 0) pthread_detach(reader); diff --git a/NativeBridgeMetalFrontend.h b/NativeBridgeMetalFrontend.h index 913b31e..25ed8e5 100644 --- a/NativeBridgeMetalFrontend.h +++ b/NativeBridgeMetalFrontend.h @@ -17,6 +17,8 @@ size_t native_bridge_metal_status(char *out, size_t size); 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); diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 94c8708..57b043f 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -398,6 +398,8 @@ static void noteProblem(NSString *what) { memcpy(gLastProblems[1],gLastProblems[0],sizeof gLastProblems[0]); strlcpy(gLastProblems[0],what.UTF8String?:"?",sizeof gLastProblems[0]); } +// A note from the plugin (NativeAdrenoMetalPlugin.mm), for the telemetry's problem lines. +extern "C" void native_bridge_metal_note(const char *what) {noteProblem([NSString stringWithUTF8String:what]?:@"?");} // NMB_LOG_STDERR: also on stderr (a daemon's NSLog reaches only the unified log, which a bring-up board does not read). static bool logToStderr() {static bool on=getenv("NMB_LOG_STDERR")!=nullptr;return on;} #define NBLOG(...) do{NSString *nbText=[NSString stringWithFormat:__VA_ARGS__];NSLog(@"%@",nbText);noteProblem(nbText);\ @@ -755,6 +757,8 @@ static void waitForResource(id resource); @property(nonatomic) void *wrapped; @property(nonatomic,strong) id parentHeap; // the heap that made the buffer, nil otherwise @property(nonatomic) NSUInteger heapOffset; +// A surface import's own surface object, which the runtime buffer retains until it is freed (surfaceBuffer); nullptr otherwise. +@property(nonatomic) IOSurfaceRef importedSurface; @end @interface NBTexture : NBObject @property(nonatomic) NmbTexture *handle; @@ -845,9 +849,12 @@ static void watchImportPressure() { dispatch_activate(source); }); } -static void holdImport(uint64_t identity,uint64_t address,uint64_t bytes,NBBuffer *buffer,IOSurfaceRef surface,std::vector &gone) { +// The entry records the surface object the import itself retains, never the caller's: a hit may come with another object for the same +// surface, which nothing here retains, and a later prune read its retain count after the caller freed it (backboardd died on the isa +// authentication inside CFGetRetainCount, called from pruneHeldImports at a command buffer's completion). +static void holdImport(uint64_t identity,uint64_t address,uint64_t bytes,NBBuffer *buffer,std::vector &gone) { pruneHeldImports(false,gone); - gHeldImports[{identity,address,bytes}]={buffer,surface}; + gHeldImports[{identity,address,bytes}]={buffer,buffer.importedSurface}; } static NBBuffer *surfaceBuffer(IOSurfaceRef surface,void *base,size_t bytes) { struct Key {uint64_t identity,address,bytes;}; @@ -858,11 +865,11 @@ static NBBuffer *surfaceBuffer(IOSurfaceRef surface,void *base,size_t bytes) { std::lock_guard lock(gSurfaceImportLock); if(!gSurfaceImports)gSurfaceImports=[NSMapTable strongToWeakObjectsMapTable]; NBBuffer *buffer=key?[gSurfaceImports objectForKey:key]:nil; - if(buffer){if(key)holdImport(value.identity,value.address,value.bytes,buffer,surface,gone);return buffer;} + if(buffer){if(key)holdImport(value.identity,value.address,value.bytes,buffer,gone);return buffer;} // The surface keeps its CPU mapping (the imported pages) until the import is freed. NmbBuffer *handle=nmb_buffer_new_no_copy_releasing(gDevice,base,bytes,MTLResourceStorageModeShared,releaseSurface,(void *)CFRetain(surface)); if(!handle){CFRelease(surface);NBLOG(@"native-metal: surface import refused: %@",lastError());return nil;} - gSurfaceImportsMade++;buffer=[NBBuffer new];buffer.handle=handle;buffer.options=MTLResourceStorageModeShared; + gSurfaceImportsMade++;buffer=[NBBuffer new];buffer.handle=handle;buffer.options=MTLResourceStorageModeShared;buffer.importedSurface=surface; if(key) { // Associations whose import is gone are pruned whenever the table has doubled since the last pruning; a live one is never evicted. if(gSurfaceImports.count>=gSurfaceImportsPruned) { @@ -871,7 +878,7 @@ static NBBuffer *surfaceBuffer(IOSurfaceRef surface,void *base,size_t bytes) { gSurfaceImportsPruned=MAX(gSurfaceImports.count*2,(NSUInteger)64); } [gSurfaceImports setObject:buffer forKey:key]; - holdImport(value.identity,value.address,value.bytes,buffer,surface,gone); + holdImport(value.identity,value.address,value.bytes,buffer,gone); } return buffer; } From 7183e58842200740c59c4f77854efa66d8129ddf Mon Sep 17 00:00:00 2001 From: rpone Date: Fri, 30 Oct 2026 11:05:39 +0800 Subject: [PATCH 79/92] feat(metal): frame time histograms, priming resumed per function with an RGhA variant, long problem texts --- NativeAdrenoMetalPlugin.mm | 8 ++++++++ NativeBridgeMetalFrontend.mm | 19 ++++++++++++++++++- PrimeShaders.h | 35 ++++++++++++++++++++++++++--------- 3 files changed, 52 insertions(+), 10 deletions(-) diff --git a/NativeAdrenoMetalPlugin.mm b/NativeAdrenoMetalPlugin.mm index 1c93dc6..7d5f6e6 100644 --- a/NativeAdrenoMetalPlugin.mm +++ b/NativeAdrenoMetalPlugin.mm @@ -171,6 +171,14 @@ static void clientFailure(unsigned port, const char *step, const char *detail) { char text[64]; snprintf(text, sizeof text, "%.14s %s", step, shown); streamText(*gpu, text); + // A refusal's text is what says why a client ends (iconservicesagent's pipeline refusal showed 18 characters of it): the rest of it + // follows in rows of its own, up to six, each 46 characters. + if (!strcmp(step, "problem") && length > 46) + for (unsigned part = 1, at = 46; part <= 6 && at < length; ++part, at += 46) { + char row[64]; + snprintf(row, sizeof row, "problem%u %.46s", part, detail + at); + streamText(*gpu, row); + } } } static id clientDevice(unsigned port) { diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 57b043f..02f1fa3 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -158,6 +158,10 @@ static uint64_t nowUs() {return clock_gettime_nsec_np(CLOCK_UPTIME_RAW)/1000;} struct Timed {std::atomic count{0},total{0},slowest{0}; void add(uint64_t began){const uint64_t took=nowUs()-began;count++;total+=took;uint64_t seen=slowest.load();while(took>seen && !slowest.compare_exchange_weak(seen,took)){}}}; static Timed gTimePipeline,gTimeLibrary,gTimeFunction,gTimeEncodeToCommit; +// Where a frame's time goes, as histograms over power-of-two millisecond buckets (<1, <2, <4 ... <256, more): commit to completion of the +// command buffers that hold a full-screen pass (a frame's), of every other one, and a frame's encoding (creation to commit). +static std::atomic gHistFrameGpu[10],gHistOtherGpu[10],gHistFrameEncode[10]; +static void histAdd(std::atomic *bins,uint64_t us) {unsigned b=0;const uint64_t ms=us/1000;while(b<9 && ms>=(1ull< gCompileCached; // requests the shader cache answered static Timed gTimeWaitScheduled,gTimeWaitCompleted; @@ -464,6 +468,7 @@ __attribute__((weak)) volatile uint32_t native_prime_libraries_done; __attribute__((weak)) volatile uint32_t native_prime_libraries_total; __attribute__((weak)) volatile uint32_t native_prime_made; __attribute__((weak)) volatile uint32_t native_prime_rate; +__attribute__((weak)) volatile uint32_t native_prime_functions_resumed; } static void refuseSurface(uint32_t step,IOSurfaceRef surface,MTLTextureDescriptor *d,NSUInteger plane,uint64_t offset,uint64_t row) { gSurfaceRefusals++; @@ -2857,6 +2862,11 @@ static void completed(void *context) { NBCommandBuffer *commandBuffer=(__bridge_transfer NBCommandBuffer *)context; { const uint64_t took=nowUs()-commandBuffer.committedUs; + { + const bool frame=commandBuffer->encodedWork.pixels>=2000000; + histAdd(frame?gHistFrameGpu:gHistOtherGpu,took); + if(frame)histAdd(gHistFrameEncode,commandBuffer.committedUs-commandBuffer->createdUs); + } gTimeEncodeToCommit.count++;gTimeEncodeToCommit.total+=took; uint64_t seen=gTimeEncodeToCommit.slowest.load();while(took>seen && !gTimeEncodeToCommit.slowest.compare_exchange_weak(seen,took)){} std::lock_guard lock(gCommitLock); @@ -4237,6 +4247,13 @@ extern "C" size_t native_bridge_metal_telemetry(char *out,size_t size) { (unsigned long long)gTimeFunction.count.load(),ms(gTimeFunction.total.load()),ms(gTimeFunction.slowest.load())); row("G commit-to-complete n=%llu total=%llums max=%llums mean=%lluus",(unsigned long long)gTimeEncodeToCommit.count.load(),ms(gTimeEncodeToCommit.total.load()), ms(gTimeEncodeToCommit.slowest.load()),(unsigned long long)(gTimeEncodeToCommit.count.load()?gTimeEncodeToCommit.total.load()/gTimeEncodeToCommit.count.load():0)); + for(int k=0;k<3;k++) { + const std::atomic *bins=k==0?gHistFrameGpu:k==1?gHistOtherGpu:gHistFrameEncode; + row("H %s <1=%llu <2=%llu <4=%llu <8=%llu <16=%llu <32=%llu <64=%llu <128=%llu <256=%llu more=%llu",k==0?"frame-gpu":k==1?"other-gpu":"frame-enc", + (unsigned long long)bins[0].load(),(unsigned long long)bins[1].load(),(unsigned long long)bins[2].load(),(unsigned long long)bins[3].load(), + (unsigned long long)bins[4].load(),(unsigned long long)bins[5].load(),(unsigned long long)bins[6].load(),(unsigned long long)bins[7].load(), + (unsigned long long)bins[8].load(),(unsigned long long)bins[9].load()); + } row("J scheduled wait n=%llu total=%llums max=%llums",(unsigned long long)gTimeWaitScheduled.count.load(),ms(gTimeWaitScheduled.total.load()),ms(gTimeWaitScheduled.slowest.load())); row("J completed wait n=%llu total=%llums max=%llums t=%llu",(unsigned long long)gTimeWaitCompleted.count.load(),ms(gTimeWaitCompleted.total.load()),ms(gTimeWaitCompleted.slowest.load()),(unsigned long long)gWaitTimeouts.load()); row("J resource wait n=%llu total=%llums max=%llums",(unsigned long long)gTimeResourceWait.count.load(),ms(gTimeResourceWait.total.load()),ms(gTimeResourceWait.slowest.load())); @@ -4330,7 +4347,7 @@ extern "C" size_t native_bridge_metal_telemetry(char *out,size_t size) { { std::lock_guard lock(gProblemLock); char formats[160];native_bridge_metal_formats_locked(formats,sizeof formats);row("F %.86s",formats); - row("PR compiling=%u libs=%u/%u made=%u rate=%u",native_prime_compiling,native_prime_libraries_done,native_prime_libraries_total,native_prime_made,native_prime_rate); + row("PR compiling=%u libs=%u/%u made=%u rate=%u resumed=%u",native_prime_compiling,native_prime_libraries_done,native_prime_libraries_total,native_prime_made,native_prime_rate,native_prime_functions_resumed); const SurfaceRefusal &r=gSurfaceLastRefusal; if(r.step)row("SR %u %08x %c%c%c%c pl%u/%u o%llx rb%llu a%llx %llux%llu pf%u",r.step,r.format,(char)(r.format>>24),(char)(r.format>>16),(char)(r.format>>8), (char)r.format,r.plane,r.planes,(unsigned long long)r.offset,(unsigned long long)r.row,(unsigned long long)r.allocation,(unsigned long long)r.width, diff --git a/PrimeShaders.h b/PrimeShaders.h index 530e0ed..7b35a3d 100644 --- a/PrimeShaders.h +++ b/PrimeShaders.h @@ -5,8 +5,11 @@ // the same stem (`_vert_` -> `_frag_`, or a fragment-less pipeline) and one BGRA8 attachment. Pipelines a caller makes with other constants // or attachments still compile then, but their functions' modules are decoded and the translator has seen every function once. // State in the cache directory: prime-done lists finished libraries (path, size, mtime), prime-bad the functions that ended the process -// (prime-current names the function being compiled; a start that finds it moves it to prime-bad), prime.log one line per library. -// A library listed as finished is never read again, so a restart resumes where the previous run stopped. +// (prime-current names the function being compiled; a start that finds it moves it to prime-bad), prime.log one line per library, and +// prime-fn the functions finished inside the libraries not finished yet (path, name and the build that made them). +// A library listed as finished is never read again, and a function listed in prime-fn is not compiled again, so a restart resumes where +// the previous run stopped even inside a library with thousands of functions (the one that held priming at 162 of 280 libraries for +// every boot: each boot ended before it did and began it again). #pragma once #import #import @@ -34,7 +37,8 @@ extern "C" unsigned native_bridge_metal_recent_commits(uint64_t windowUs); // Priming's progress for the telemetry table (the PR row): what the thread does (1 a pipeline compiles, 2 it waits for the desktop to // settle, 3 it opens a library and its functions), libraries done of the list, pipelines made, and the most command buffers the last wait // saw in 250 ms. -extern "C" {volatile uint32_t native_prime_compiling, native_prime_libraries_done, native_prime_libraries_total, native_prime_made, native_prime_rate;} +extern "C" {volatile uint32_t native_prime_compiling, native_prime_libraries_done, native_prime_libraries_total, native_prime_made, native_prime_rate, + native_prime_functions_resumed;} extern "C" void native_bridge_metal_quiet_problems(bool); namespace prime { @@ -204,8 +208,9 @@ static bool linkedKernel(id device, id lib, id device, const std::string &path, const std::string ¤t, const std::set &bad) { - unsigned kernels = 0, renders = 0, made = 0, refused = 0, skipped = 0, zeroed = 0, linkedMade = 0; +static std::string library(id device, const std::string &path, const std::string ¤t, const std::set &bad, + const std::string &functionsPath, const std::set &functionsDone, const std::string &build) { + unsigned kernels = 0, renders = 0, made = 0, refused = 0, skipped = 0, zeroed = 0, linkedMade = 0, halfMade = 0; uint64_t waited = 0; std::string firstRefusal; // the first refused function and why, so the log tells translator gaps from unspecialized constants const uint64_t began = nowMs(); @@ -221,8 +226,10 @@ static std::string library(id device, const std::string &path, const for (NSString *name in names) { @autoreleasepool { const std::string key = path + "\t" + name.UTF8String; + const std::string doneKey = key + "\t" + build; native_prime_compiling = 3; if (bad.count(key)) {++skipped; continue;} + if (functionsDone.count(doneKey)) {native_prime_functions_resumed = native_prime_functions_resumed + 1; continue;} id function = [lib newFunctionWithName:name]; if (!function) {++refused; continue;} const MTLFunctionType type = function.functionType; @@ -263,11 +270,19 @@ static std::string library(id device, const std::string &path, const } } ok = [device newRenderPipelineStateWithDescriptor:d error:&error] != nil; + // The render server's framebuffer is half-float (RGhA) on this board: its pipelines draw into RGBA16Float, a state + // of its own to Turnip, so the same pair is made for it too. + if (ok) { + d.colorAttachments[0].pixelFormat = MTLPixelFormatRGBA16Float; + NSError *half = nil; + if ([device newRenderPipelineStateWithDescriptor:d error:&half]) ++halfMade; + } } } @catch (NSException *exception) { ok = false; } native_prime_compiling = 0; + appendLine(functionsPath, doneKey); ok ? ++made : ++refused; if (ok) native_prime_made = native_prime_made + 1; if (!ok && firstRefusal.empty()) { @@ -283,9 +298,9 @@ static std::string library(id device, const std::string &path, const } } unlink(current.c_str()); - char line[180]; - snprintf(line, sizeof line, " kernels=%u renders=%u made=%u zeroed=%u linked=%u refused=%u skipped=%u ms=%llu waited=%llu", kernels, renders, - made, zeroed, linkedMade, refused, skipped, (unsigned long long)(nowMs() - began), (unsigned long long)waited); + char line[200]; + snprintf(line, sizeof line, " kernels=%u renders=%u made=%u half=%u zeroed=%u linked=%u refused=%u skipped=%u ms=%llu waited=%llu", kernels, renders, + made, halfMade, zeroed, linkedMade, refused, skipped, (unsigned long long)(nowMs() - began), (unsigned long long)waited); return path + line + firstRefusal; } @@ -302,6 +317,8 @@ static void run(id device, std::string root, unsigned delaySeconds, c sleep(delaySeconds); const std::set bad = readLines(badPath); std::set done = readLines(donePath); + const std::string functionsPath = root + "/prime-fn"; + const std::set functionsDone = readLines(functionsPath); const uint64_t began = nowMs(); unsigned libs = 0, finished = 0; std::string note; @@ -317,7 +334,7 @@ static void run(id device, std::string root, unsigned delaySeconds, c // The runtime's and Turnip's builds end the id: a newer one primes every library again (buildId()). const std::string id = path + "\t" + std::to_string((long long)st.st_size) + "\t" + std::to_string((long long)st.st_mtimespec.tv_sec) + "\t" + build; if (done.count(id)) {++finished; continue;} - appendLine(logPath, library(device, path, current, bad)); + appendLine(logPath, library(device, path, current, bad, functionsPath, functionsDone, build)); appendLine(donePath, id); done.insert(id); ++finished; From 278333f4a5c8c026dec8e8c277c741c0d909cc77 Mon Sep 17 00:00:00 2001 From: rpone Date: Fri, 30 Oct 2026 19:34:37 +0800 Subject: [PATCH 80/92] feat(metal): BGRA framebuffer through sysmem rendering, opaque scanout copy, format and problem diagnostics --- GpuScanoutPresenter.h | 46 ++++++++++++++++++++--------- NativeAdrenoMetalPlugin.mm | 56 +++++++++++++++++++++++++++++++++--- NativeBridgeMetalFrontend.mm | 34 ++++++++++++++++++++-- bridge/scanout_host.mm | 18 ++++++++---- shaders/scanout_convert.ll | 47 +++++++++++++++++++++++++++++- 5 files changed, 175 insertions(+), 26 deletions(-) diff --git a/GpuScanoutPresenter.h b/GpuScanoutPresenter.h index bdc8ff9..8c64a98 100644 --- a/GpuScanoutPresenter.h +++ b/GpuScanoutPresenter.h @@ -3,8 +3,10 @@ // imported without a copy (newBufferWithBytesNoCopy: pinned by the kernel and mapped into the GPU's 4 KiB context), the copy is a blit on // the GPU's queue, and the call returns after the GPU retired it. A half-float frame (RGBA 16F, what iPadOS 27's render server draws // once a Metal device renders) is converted by a compute kernel (shaders/scanout_convert.ll) through the display driver's own table of -// codes (NativeHalfCodes.h), so the bytes match its CPU path. No CPU touches a pixel; there is no staging copy and no software fallback -// here. Used through the public Metal API, on any device (the native frontend on the tablet, the Mac's device for the host check). +// codes (NativeHalfCodes.h), so the bytes match its CPU path. A BGRA frame goes through the kernel scanout_opaque, which copies it with +// alpha 0xff, as the half-float conversion stores it: the display blends the scanout by its per-pixel alpha (SSPP SRC_FORMAT 0x237ff), +// and the compositor's framebuffer alpha is not opacity (translucent tinted blocks around Liquid Glass, i23c80d087); a device without +// the kernel blits it. No CPU touches a pixel; there is no staging copy and no software fallback here. Used through the public Metal API, on any device (the native frontend on the tablet, the Mac's device for the host check). #pragma once #import #include @@ -61,6 +63,21 @@ public: const uint32_t rows = gHalfRows.load(std::memory_order_relaxed) ? frame.height / 2 : frame.height; id source = sourceFor(frame); if (!source) return kNoResource; + if (!(frame.stride & 3) && !(scanout_.stride & 3) && pipeline(opaque_, opaqueFailed_, @"scanout_opaque")) { + id commands = [queue_ commandBuffer]; + id encoder = commands ? [commands computeCommandEncoder] : nil; + if (!encoder) return kNoResource; + const uint32_t params[4] = {frame.width, rows, (uint32_t)(frame.stride / 4), (uint32_t)(scanout_.stride / 4)}; + [encoder setComputePipelineState:opaque_]; + [encoder setBuffer:source offset:0 atIndex:0]; + [encoder setBuffer:target_ offset:0 atIndex:1]; + [encoder setBytes:params length:sizeof params atIndex:3]; + [encoder dispatchThreadgroups:MTLSizeMake((frame.width + 15) / 16, (rows + 7) / 8, 1) threadsPerThreadgroup:MTLSizeMake(16, 8, 1)]; + [encoder endEncoding]; + [commands commit]; + [commands waitUntilCompleted]; + return commands.status == MTLCommandBufferStatusCompleted ? kOk : kGpuFailed; + } id commands = [queue_ commandBuffer]; id blit = commands ? [commands blitCommandEncoder] : nil; if (!blit) return kNoResource; @@ -96,7 +113,7 @@ public: frame.width != scanout_.width || frame.height != scanout_.height || frame.stride < row || (frame.stride & 3) || (scanout_.stride & 3) || frame.stride * (frame.height - 1) + row > frame.bytes) return kBadFrame; const bool encoded = frame.format == NGP_RGBA16F_ENCODED; - if (!convertPipeline() || !table(encoded)) return kNoResource; + if (!pipeline(convert_, convertFailed_, @"scanout_convert") || !table(encoded)) return kNoResource; id source = sourceFor(frame); if (!source) return kNoResource; id commands = [queue_ commandBuffer]; @@ -115,20 +132,21 @@ public: [commands waitUntilCompleted]; return commands.status == MTLCommandBufferStatusCompleted ? kOk : kGpuFailed; } - // Made once; a failure is not retried (every later half-float frame then goes back to the display driver's own path). - bool convertPipeline() { - if (convert_ || convertFailed_) return convert_ != nil; - convertFailed_ = true; + // A kernel of the scanout library, made once; a failure is not retried (every later half-float frame then goes back to the display + // driver's own path, every BGRA frame to the blit). + bool pipeline(__strong id &state, bool &failed, NSString *name) { + if (state || failed) return state != nil; + failed = true; if (!nmb_scanout_convert_air || !native_bridge_metal_library_with_air) return false; size_t length = 0; const void *air = nmb_scanout_convert_air(&length); id library = air && length ? native_bridge_metal_library_with_air(air, length) : nil; - id function = [library newFunctionWithName:@"scanout_convert"]; + id function = [library newFunctionWithName:name]; NSError *error = nil; - convert_ = function ? [device_ newComputePipelineStateWithFunction:function error:&error] : nil; - if (convert_ && convert_.maxTotalThreadsPerThreadgroup < 128) convert_ = nil; - convertFailed_ = convert_ == nil; - return convert_ != nil; + state = function ? [device_ newComputePipelineStateWithFunction:function error:&error] : nil; + if (state && state.maxTotalThreadsPerThreadgroup < 128) state = nil; + failed = state == nil; + return state != nil; } // The 8-bit code of every 16-bit half pattern, as 32-bit words (65536 of them, 256 KiB). id table(bool encoded) { @@ -169,8 +187,8 @@ private: Scanout scanout_; id queue_; id target_; - id convert_ = nil; - bool convertFailed_ = false; + id convert_ = nil, opaque_ = nil; + bool convertFailed_ = false, opaqueFailed_ = false; id tables_[2] = {nil, nil}; Source sources_[kSources] = {}; uint64_t uses_ = 0; diff --git a/NativeAdrenoMetalPlugin.mm b/NativeAdrenoMetalPlugin.mm index 7d5f6e6..852049b 100644 --- a/NativeAdrenoMetalPlugin.mm +++ b/NativeAdrenoMetalPlugin.mm @@ -17,6 +17,8 @@ #include #include #include +#include +#include #include #include "NativeAdrenoUserTest.h" #include "NativeMetalBridgePhase.h" @@ -80,6 +82,32 @@ static bool writeTask(uintptr_t address, const void *data, size_t size) { } // EXPERIMENT (second report onward): this display path takes plain 8-bit BGRA only, so tell the render server's display that it has neither the deep // (10-bit) framebuffer nor universal buffer compression. Whether the state it reads those from is the whole story is what the markers show. +// EXPERIMENT (boot-arg sheng-fb-bgra=1): the render server's framebuffer format with every other virtualized answer left as it is. +// framebuffer_pixel_format (24A446 0x184461838) takes RGhA on `tbnz w8, #0` at 0x184461928 when CADeviceIsVirtualized (byte 0x1e701bdd9) is 1; +// 40 other places in QuartzCore read the same byte (XR formats, backdrop compute, ASTC ...). Holding the byte at 0 turned all of them, and the +// tablet drew light blue blocks. Here the byte stays 1 and only this branch is made a NOP, so the framebuffer is chosen as on a real device and +// nothing else changes. Marks 0x6E40 unreadable, 0x6E41 other instruction, 0x6E42 patched, 0x6E50|stage failures. +static bool bootArgSet(const char *key) { + char args[1100] = {}; + size_t size = sizeof args - 1; + return sysctlbyname("kern.bootargs", args, &size, nullptr, 0) == 0 && strstr(args, key); +} +static unsigned patchFramebufferBranch(uintptr_t slide) { + const uintptr_t site = 0x184461928 + slide; + uint32_t word = 0; + if (!readTask(site, &word, sizeof word)) return 0x6E40; + if (word == 0xd503201f) return 0x6E42; + if (word != 0x37000608) return 0x6E41; + const vm_address_t page = site & ~(uintptr_t)(vm_page_size - 1); + if (vm_protect(mach_task_self(), page, vm_page_size, FALSE, VM_PROT_READ | VM_PROT_WRITE | VM_PROT_COPY) != KERN_SUCCESS) return 0x6E51; + const uint32_t nop = 0xd503201f; + const bool wrote = vm_write(mach_task_self(), site, (vm_offset_t)&nop, sizeof nop) == KERN_SUCCESS; + const bool restored = vm_protect(mach_task_self(), page, vm_page_size, FALSE, VM_PROT_READ | VM_PROT_EXECUTE) == KERN_SUCCESS; + sys_icache_invalidate((void *)site, sizeof nop); + if (!wrote) return 0x6E52; + if (!restored) return 0x6E53; + return 0x6E42; +} static unsigned gReports; struct DisplaySample {unsigned flags = 0, mode = 0, behaviour = 0, globals = 0; uint16_t display = 0; uintptr_t object = 0;}; static DisplaySample sampleDisplay(id first, intptr_t slide) { @@ -175,9 +203,11 @@ static void clientFailure(unsigned port, const char *step, const char *detail) { // follows in rows of its own, up to six, each 46 characters. if (!strcmp(step, "problem") && length > 46) for (unsigned part = 1, at = 46; part <= 6 && at < length; ++part, at += 46) { - char row[64]; - snprintf(row, sizeof row, "problem%u %.46s", part, detail + at); - streamText(*gpu, row); + // The log keeps the latest text row only: each part follows the one before it by 4 s so a sample catches every one. + std::string row = "problem" + std::to_string(part) + " " + std::string(detail + at).substr(0, 46); + auto *target = gpu; + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, int64_t(part) * 4 * NSEC_PER_SEC), + dispatch_get_global_queue(QOS_CLASS_UTILITY, 0), ^{streamText(*target, row.c_str());}); } } } @@ -250,7 +280,24 @@ static id deviceForPort(unsigned port) { if (fgets(line, sizeof line, flags)) {line[strcspn(line, "\n")] = 0; setenv("TU_DEBUG", line, 1);} fclose(flags); } - setenv("TU_DEBUG", "nocb", 0); + // sheng-tu-debug=: TU_DEBUG for every Metal process (an experiment: sysmem renders without GMEM load and store, gmem always with them). + { + char args[1100] = {}; + size_t size = sizeof args - 1; + if (sysctlbyname("kern.bootargs", args, &size, nullptr, 0) == 0) + if (const char *at = strstr(args, "sheng-tu-debug=")) { + char value[64] = {}; + strlcpy(value, at + 15, sizeof value); + value[strcspn(value, " ")] = 0; + setenv("TU_DEBUG", value, 1); + } + } + // With the 8-bit BGRA framebuffer (sheng-fb-bgra=1) every pass renders in system memory. A mutable BGRA image is written with a colour swap, + // and Turnip's tile-memory loads and stores (the blit event's resolve, the 3D and 2D copies) each swap it once more, at some places and not + // at others: the passes that went through them left R and B exchanged, in the big passes and in the small ones that sample them, as + // blocks of the lock screen's wallpaper. In system memory the render targets take the swap and nothing copies it again: the lock screen + // matches the RGhA framebuffer's to the tile (99.5 %, no exchanged tile; tile memory 59 % exchanged, automatic choice 62 %). + setenv("TU_DEBUG", bootArgSet("sheng-fb-bgra=1") ? "sysmem,nocb" : "nocb", 0); if (strcmp(getprogname(), "backboardd") != 0) return device = clientDevice(port); // The discovery releases the accelerator port when it is done; the monitor and the scanout client keep using it. if (void *iokit = dlopen("/System/Library/Frameworks/IOKit.framework/IOKit", RTLD_NOW | RTLD_LOCAL)) { @@ -316,6 +363,7 @@ static id deviceForPort(unsigned port) { gpu->mark(writeTask(0x1e7021104 + slide, &never, sizeof never) ? 0x6E02 : 0x6EF2); const uint8_t off = 0; gpu->mark(writeTask(0x1e702116c + 156 + slide, &off, sizeof off) ? 0x6E03 : 0x6EF3); + if (!strcmp(getprogname(), "backboardd") && bootArgSet("sheng-fb-bgra=1")) gpu->mark(patchFramebufferBranch(slide)); // What framebuffer_pixel_format reads in the render server, once the desktop has started (other processes draw no // framebuffer): 0x6E20 | 1 headroom override 1, 2 threshold above 1, 4 CA_FORCE_FP16_FRAMEBUFFER (_ca_debug_options // 0x1e702116c + 128), 8 CA_ALLOW_EDR_MIRRORING (+ 156), 0x10 the device-is-virtualized byte (0x1e701bdd9). diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 02f1fa3..e446017 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -1438,6 +1438,7 @@ static NBFunction *ciKernelFunction(NBLibrary *library,NSString *name) { @interface NBRenderPipeline : NBObject @property(nonatomic) NmbRenderPipeline *handle; @property(nonatomic,copy) NSString *dumpInfo; // NATIVE_METAL_DUMP_CB: its functions, formats and vertex layout +@property(nonatomic) uint32_t stateFormat; // its colour 0 pixel format (the DF rows) @end @implementation NBRenderPipeline - (void)dealloc {nmb_render_pipeline_release(_handle);} @@ -1846,6 +1847,17 @@ static void waitForResource(id resource) { // before it in the encoder -- a setter that binds what is already bound changes nothing -- tallied per process into // ngpu-draw-stats-.txt in its temporary directory every 2000 draws (the render server's sandbox refuses /private/var/tmp): how // often a draw binds what the draw before it bound, which a runtime that kept the last draw's bind work could skip. +// Which pipeline formats draw into which pass targets (telemetry DF rows): a pass key (colour 0 format, load action, a surface, a big target) +// with the bound pipeline's colour 0 format, counted at each pipeline bind. A state format that is not its target's is the case plain Metal +// stores with the target's byte order (BGRA8 exchanges bytes 0 and 2). +static std::mutex gPassFormatLock; +static struct {uint64_t key,n;} gPassFormatRows[32]; +static unsigned gPassFormatCount; +static void noteFormats(uint64_t key) { + std::lock_guard lock(gPassFormatLock); + for(unsigned i=0;i> bound;uint32_t changed=kDSFirst;}; @@ -2062,11 +2074,13 @@ static void writeDrawStats() { @end @interface NBRenderEncoder : NBEncoder +@property(nonatomic) uint64_t passKey; // colour 0 format | load << 16 | surface << 18 | big << 19 @end @implementation NBRenderEncoder - (void)setRenderPipelineState:(id)state { if(![(id)state isKindOfClass:[NBRenderPipeline class]]){[self.commandBuffer refuse:@"foreign render pipeline"];return;} if(dumpRange().first)self.dumpPipeline=state; + noteFormats(self.passKey|((uint64_t)((NBRenderPipeline *)state).stateFormat<<32)); if(gDrawStats)[self noteState:kDSPipeline key:0 object:(__bridge const void *)state value:0]; ENCODE([self check:nmb_render_set_pipeline(self.handle,[(NBRenderPipeline *)state handle]) what:"setRenderPipelineState"]); } @@ -2682,7 +2696,14 @@ static char kUserDictionaryKey; pass.visibility=[(NBBuffer *)d.visibilityResultBuffer handle]; } NmbCommandBuffer *handle=_handle; - return [self encoder:[NBRenderEncoder new] make:^{return nmb_render_encoder_new(handle,&pass);} what:@"renderCommandEncoder"]; + id made=[self encoder:[NBRenderEncoder new] make:^{return nmb_render_encoder_new(handle,&pass);} what:@"renderCommandEncoder"]; + if(made) { + id t0=d.colorAttachments[0].texture; + const uint64_t big=t0 && (uint64_t)t0.width*t0.height>=2000000; + ((NBRenderEncoder *)made).passKey=(t0?(uint64_t)t0.pixelFormat:0)|((uint64_t)d.colorAttachments[0].loadAction<<16)| + (uint64_t)([(id)t0 isKindOfClass:[NBTexture class]] && ((NBTexture *)t0).surface?1:0)<<18|big<<19; + } + return made; } - (void)addScheduledHandler:(MTLCommandBufferHandler)block {if(block)@synchronized(self){[_scheduledHandlers addObject:[block copy]];}} - (void)addCompletedHandler:(MTLCommandBufferHandler)block {if(block)@synchronized(self){[_completedHandlers addObject:[block copy]];}} @@ -3951,7 +3972,7 @@ static bool pipelineArchives(NSArray *list,std::vector &arch if(error)*error=nbError([@"render pipeline: " stringByAppendingString:why]); return nil; } - NBRenderPipeline *state=[NBRenderPipeline new];state.handle=handle;gPipelines++;if(error)*error=nil; + NBRenderPipeline *state=[NBRenderPipeline new];state.handle=handle;state.stateFormat=(uint32_t)d.colorAttachments[0].pixelFormat;gPipelines++;if(error)*error=nil; if(dumpRange().first)state.dumpInfo=renderDumpInfo(d); return (id)state; } @@ -4239,6 +4260,15 @@ extern "C" size_t native_bridge_metal_telemetry(char *out,size_t size) { (unsigned long long)gCompleted.load(),(unsigned long long)gPipelines.load(),(unsigned long long)gProblems.load(),(unsigned long long)gPasses.load(), (unsigned long long)gDraws.load(),(unsigned long long)gFullScreenPasses.load(),(unsigned long long)(gBiggestPass.load()>>10), (unsigned long long)gDispatches.load(),(unsigned long long)gBlits.load()); + { + // The five most used (pass target, pipeline) format pairs: p target format, s pipeline format, l load action, S an IOSurface, B a screen-size target. + struct {uint64_t key,n;} top[5]={}; + {std::lock_guard lock(gPassFormatLock); + for(unsigned i=0;itop[t].n){for(unsigned u=4;u>t;u--)top[u]=top[u-1];top[t]={gPassFormatRows[i].key,gPassFormatRows[i].n};break;}} + for(unsigned t=0;t<5 && top[t].n;t++) + row("DF p%u s%u l%u %s%s n%llu",(unsigned)(top[t].key&0xffff),(unsigned)(top[t].key>>32),(unsigned)(top[t].key>>16&3),top[t].key>>18&1?"S":"-", + top[t].key>>19&1?"B":"-",(unsigned long long)top[t].n); + } row("T pipeline n=%llu total=%llums max=%llums",(unsigned long long)gTimePipeline.count.load(),ms(gTimePipeline.total.load()),ms(gTimePipeline.slowest.load())); row("T library n=%llu total=%llums max=%llums",(unsigned long long)gTimeLibrary.count.load(),ms(gTimeLibrary.total.load()),ms(gTimeLibrary.slowest.load())); row("T compile n=%llu total=%llums max=%llums cached=%llu",(unsigned long long)gTimeCompile.count.load(),ms(gTimeCompile.total.load()), diff --git a/bridge/scanout_host.mm b/bridge/scanout_host.mm index 267c987..69703b5 100644 --- a/bridge/scanout_host.mm +++ b/bridge/scanout_host.mm @@ -1,6 +1,7 @@ // SPDX-License-Identifier: MIT // Host check of the GPU scanout (GpuScanoutPresenter.h): a half-float frame converted by the compute kernel stores exactly the bytes of the -// display driver's table (NativeHalfCodes.h), for linear and encoded frames, with padded rows on both sides; a BGRA frame is still copied. +// display driver's table (NativeHalfCodes.h), for linear and encoded frames, with padded rows on both sides; a BGRA frame is copied with +// its alpha opaque. #import #import #include @@ -58,13 +59,20 @@ int main(int argc, char **argv) { } if (wrong) {std::printf("FAIL format %u: %llu wrong\n", format, (unsigned long long)wrong); ++failed;} } - // BGRA still takes the copy + // BGRA is copied with its alpha made opaque (the display blends the scanout by its alpha), every row, the padding left alone for (size_t i = 0; i < inBytes; ++i) in[i] = (uint8_t)random(); - if (presenter.present({in, inBytes, width * 4 + 64, width, height, 0, NGP_BGRX8888}) != gpupresent::kOk || - memcmp(out + 3 * outStride, in + 3 * (width * 4 + 64), width * 4)) {std::printf("FAIL BGRA copy\n"); ++failed;} + memset(out, 0x5a, outBytes); + if (presenter.present({in, inBytes, width * 4 + 64, width, height, 0, NGP_BGRX8888}) != gpupresent::kOk) {std::printf("FAIL BGRA copy\n"); ++failed;} + uint64_t bgraWrong = 0; + for (uint32_t y = 0; y < height; ++y) { + const uint32_t *src = (const uint32_t *)(in + y * (width * 4 + 64)), *dst = (const uint32_t *)(out + y * outStride); + for (uint32_t x = 0; x < width; ++x) if (dst[x] != (src[x] | 0xff000000u) && bgraWrong++ < 4) std::printf(" BGRA (%u,%u): %08x\n", x, y, dst[x]); + for (size_t b = width * 4; b < outStride; ++b) if (out[y * outStride + b] != 0x5a && bgraWrong++ < 4) std::printf(" BGRA padding written\n"); + } + if (bgraWrong) {std::printf("FAIL BGRA copy: %llu wrong\n", (unsigned long long)bgraWrong); ++failed;} if (presenter.present({in, inBytes, inStride, width, height, 0, 77}) != gpupresent::kBadFrame) {std::printf("FAIL unknown format taken\n"); ++failed;} if (failed) return 1; - std::printf("PASS: half-float frames convert to the display driver's bytes on the GPU (linear and encoded), BGRA copies, unknown formats refused\n"); + std::printf("PASS: half-float frames convert to the display driver's bytes on the GPU (linear and encoded), BGRA copies opaque, unknown formats refused\n"); } return 0; } diff --git a/shaders/scanout_convert.ll b/shaders/scanout_convert.ll index 9be1059..010993b 100644 --- a/shaders/scanout_convert.ll +++ b/shaders/scanout_convert.ll @@ -65,10 +65,51 @@ done: ret void } +; scanout_opaque: BGRA rows to the scanout's rows with alpha 0xff. The display fetches the scanout with its per-pixel alpha (SSPP +; SRC_FORMAT 0x237ff: bit 8, alpha enable) and blends it over its own background; the compositor's framebuffer alpha is not opacity, so +; a frame copied as it is showed translucent tinted blocks wherever the alpha was below 1 (around Liquid Glass, i23c80d087). +; kernel void scanout_opaque(device const uint *src [[buffer(0)]], device uint *dst [[buffer(1)]], constant uint4 &p [[buffer(3)]], +; uint2 gid [[thread_position_in_grid]]) +define void @scanout_opaque(ptr addrspace(1) noundef readonly "air-buffer-no-alias" %src, ptr addrspace(1) noundef "air-buffer-no-alias" %dst, ptr addrspace(2) noundef readonly align 16 dereferenceable(16) "air-buffer-no-alias" %p, <2 x i32> noundef %gid) local_unnamed_addr #0 { +entry: + %w = load i32, ptr addrspace(2) %p, align 16 + %ph = getelementptr inbounds i32, ptr addrspace(2) %p, i64 1 + %h = load i32, ptr addrspace(2) %ph, align 4 + %pss = getelementptr inbounds i32, ptr addrspace(2) %p, i64 2 + %ss = load i32, ptr addrspace(2) %pss, align 8 + %pds = getelementptr inbounds i32, ptr addrspace(2) %p, i64 3 + %ds = load i32, ptr addrspace(2) %pds, align 4 + %x = extractelement <2 x i32> %gid, i64 0 + %y = extractelement <2 x i32> %gid, i64 1 + %inx = icmp ult i32 %x, %w + %iny = icmp ult i32 %y, %h + %in = and i1 %inx, %iny + br i1 %in, label %body, label %done + +body: + %x64 = zext i32 %x to i64 + %y64 = zext i32 %y to i64 + %ss64 = zext i32 %ss to i64 + %srow = mul i64 %y64, %ss64 + %si = add i64 %srow, %x64 + %sp = getelementptr inbounds i32, ptr addrspace(1) %src, i64 %si + %v = load i32, ptr addrspace(1) %sp, align 4 + %px = or i32 %v, -16777216 + %ds64 = zext i32 %ds to i64 + %drow = mul i64 %y64, %ds64 + %di = add i64 %drow, %x64 + %dp = getelementptr inbounds i32, ptr addrspace(1) %dst, i64 %di + store i32 %px, ptr addrspace(1) %dp, align 4 + br label %done + +done: + ret void +} + attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn "frame-pointer"="all" "min-legal-vector-width"="0" "no-builtins" "no-trapping-math"="true" "stack-protector-buffer-size"="8" } !llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8} -!air.kernel = !{!9} +!air.kernel = !{!9, !22} !air.compile_options = !{!17, !18} !llvm.ident = !{!19} !air.version = !{!20} @@ -96,3 +137,7 @@ attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn "fram !19 = !{!"Apple metal version 32023.921 (metalfe-32023.921.6)"} !20 = !{i32 2, i32 8, i32 0} !21 = !{!"Metal", i32 4, i32 0, i32 0} +!22 = !{ptr @scanout_opaque, !10, !23} +!23 = !{!12, !13, !25, !24} +!24 = !{i32 3, !"air.thread_position_in_grid", !"air.arg_type_name", !"uint2", !"air.arg_name", !"gid"} +!25 = !{i32 2, !"air.buffer", !"air.buffer_size", i32 16, !"air.location_index", i32 3, i32 1, !"air.read", !"air.address_space", i32 2, !"air.arg_type_size", i32 16, !"air.arg_type_align_size", i32 16, !"air.arg_type_name", !"uint4", !"air.arg_name", !"p"} From 8c7297ed5df3db6fcfb017aa1008e8c7b48fc718 Mon Sep 17 00:00:00 2001 From: rpone Date: Fri, 30 Oct 2026 21:52:49 +0800 Subject: [PATCH 81/92] feat(metal): trace record of each full-screen render pass's target surface --- NativeBridgeMetalFrontend.mm | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index e446017..55c3ccd 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -180,7 +180,7 @@ static CommitRecord gCommitRing[32]; // 72-byte records: type 1 command buffer {idx, t0 created, t1 committed, t2 scheduled (0: unknown), t3 completion callback, a passes, b draws, // c thousands of pixels, d blits, tid creator thread, cpu0/cpu1 its cumulative CPU microseconds at creation and at commit, tid2 committing // thread}; type 2 waitUntilCompleted {t0 begin, t1 end, tid, a caller (low 32 bits), cpu0 the thread's CPU time at the start, cpu1 at the end}; -// type 3 the same for waitUntilScheduled; type 4 a thread's name {tid, 24 characters in t0..t2}. Times are nowUs(). +// type 3 the same for waitUntilScheduled; type 4 a thread's name {tid, 24 characters in t0..t2}; type 6 a full-screen render pass's target (see renderCommandEncoderWithDescriptor). Times are nowUs(). struct TraceRec {uint32_t type,idx;uint64_t t0,t1,t2,t3;uint32_t a,b,c,d,tid,cpu0,cpu1,tid2;}; static_assert(sizeof(TraceRec)==72,"trace record"); static TraceRec gTrace[16384]; @@ -2629,6 +2629,16 @@ static char kUserDictionaryKey; } - (id)renderCommandEncoderWithDescriptor:(MTLRenderPassDescriptor *)d { NmbRenderPassDescriptor pass={}; + // type 6 a full-screen render pass's target: {t0 begin, t1 the command buffer's creation time (joins type 1), t2 the surface's base address in this + // task (the kernel's import list names it by the same address), a/b width and height, c attachment, tid}. + for(unsigned i=0;i<2;i++) { + MTLRenderPassColorAttachmentDescriptor *c=d.colorAttachments[i]; + id target=c.resolveTexture?c.resolveTexture:c.texture; + if(target && [(id)target isKindOfClass:[NBTexture class]] && [(NBTexture *)target surface] && target.width*target.height>=2000000) { + TraceRec rec={};rec.type=6;rec.t0=nowUs();rec.t1=createdUs;rec.t2=(uint64_t)(uintptr_t)IOSurfaceGetBaseAddress([(NBTexture *)target surface]); + rec.a=(uint32_t)target.width;rec.b=(uint32_t)target.height;rec.c=i;rec.tid=threadIdLow();tracePush(rec);traceStartWriter(); + } + } _dumpPasses++; if(dumpMaybe(self)) { if(!_dumpTargets)_dumpTargets=[NSMutableArray array]; From de38c6f507b5183d55cb3676607e4a0c939364b6 Mon Sep 17 00:00:00 2001 From: rpone Date: Fri, 30 Oct 2026 23:54:22 +0800 Subject: [PATCH 82/92] 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. --- NativeAdrenoMetalPlugin.mm | 9 +-- NativeBridgeMetalFrontend.mm | 148 ++++++++++++++++++++++++++++------- bridge/NativeMetalObjects.h | 4 + bridge/NativeMetalObjects.rs | 40 ++++++++++ 4 files changed, 167 insertions(+), 34 deletions(-) diff --git a/NativeAdrenoMetalPlugin.mm b/NativeAdrenoMetalPlugin.mm index 852049b..d7e08c0 100644 --- a/NativeAdrenoMetalPlugin.mm +++ b/NativeAdrenoMetalPlugin.mm @@ -292,11 +292,10 @@ static id deviceForPort(unsigned port) { setenv("TU_DEBUG", value, 1); } } - // With the 8-bit BGRA framebuffer (sheng-fb-bgra=1) every pass renders in system memory. A mutable BGRA image is written with a colour swap, - // and Turnip's tile-memory loads and stores (the blit event's resolve, the 3D and 2D copies) each swap it once more, at some places and not - // at others: the passes that went through them left R and B exchanged, in the big passes and in the small ones that sample them, as - // blocks of the lock screen's wallpaper. In system memory the render targets take the swap and nothing copies it again: the lock screen - // matches the RGhA framebuffer's to the tile (99.5 %, no exchanged tile; tile memory 59 % exchanged, automatic choice 62 %). + // Every pass renders in system memory with the 8-bit BGRA framebuffer (sheng-fb-bgra=1): measured against tile memory with the swap of + // GMEM loads and stores fixed (patches/mesa/turnip-gmem-mutable-swap.patch, lock screen 99.5 % equal to the RGhA picture), the GPU time per + // command buffer was no better (17.8 ms mean and 56.1 ms at p95 in tile memory, 17.1 and 52.1 in system memory), and one home screen frame + // still came out with red and blue exchanged. sheng-tu-debug= overrides this for experiments. setenv("TU_DEBUG", bootArgSet("sheng-fb-bgra=1") ? "sysmem,nocb" : "nocb", 0); if (strcmp(getprogname(), "backboardd") != 0) return device = clientDevice(port); // The discovery releases the accelerator port when it is done; the monitor and the scanout client keep using it. diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 55c3ccd..89021ff 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -36,6 +36,8 @@ #include #include #include +#include +#include #include #include #include @@ -180,7 +182,12 @@ static CommitRecord gCommitRing[32]; // 72-byte records: type 1 command buffer {idx, t0 created, t1 committed, t2 scheduled (0: unknown), t3 completion callback, a passes, b draws, // c thousands of pixels, d blits, tid creator thread, cpu0/cpu1 its cumulative CPU microseconds at creation and at commit, tid2 committing // thread}; type 2 waitUntilCompleted {t0 begin, t1 end, tid, a caller (low 32 bits), cpu0 the thread's CPU time at the start, cpu1 at the end}; -// type 3 the same for waitUntilScheduled; type 4 a thread's name {tid, 24 characters in t0..t2}; type 6 a full-screen render pass's target (see renderCommandEncoderWithDescriptor). Times are nowUs(). +// type 3 the same for waitUntilScheduled; type 4 a thread's name {tid, 24 characters in t0..t2}; type 0 the file's first record {t0 nowUs() now, t1 wall clock microseconds, b process id, c wall clock +// seconds, d format version 2}; type 7 a render pass from Turnip's ring (tu_pass_ring_read) {t0 recorded, t1 its sequence, a width | height << 16, b mode (0 GMEM, 1 SYSMEM +// by the autotuner, 2 SYSMEM otherwise) | colour attachments << 8 | flags (1 mutable view, 2 linear, 4 unpadded, 8 loaded, 16 stored, 32 FDM) << 16 | the first colour +// attachment's view swap << 24, c draws | VkFormat << 16, d thousands of pixels}; type 8 a kernel submission from the Turnip kmd's ring (tu_darwin_submit_ring_read) {t0 when +// it began, t1 its sequence, a the kernel fence, b microseconds waiting for an unsubmitted sync, c microseconds in the kernel calls, d of those waiting for ring space, +// cpu0 busy answers | chunks << 16 | indirect buffers << 24}. Times are nowUs(). struct TraceRec {uint32_t type,idx;uint64_t t0,t1,t2,t3;uint32_t a,b,c,d,tid,cpu0,cpu1,tid2;}; static_assert(sizeof(TraceRec)==72,"trace record"); static TraceRec gTrace[16384]; @@ -209,28 +216,37 @@ static void traceNote(uint32_t tid) { TraceRec rec={};rec.type=4;rec.tid=tid;pthread_getname_np(pthread_self(),(char *)&rec.t0,24);tracePush(rec); } -// ---- sampler: where the render thread's CPU goes between a command buffer's creation and its commit ---- -// A thread suspends the render thread every 500 us while it is inside that window and keeps the program counter plus the frame-pointer chain in a -// ring (no allocation, no locks: the suspended thread may hold any). The writer thread resolves the addresses with dladdr and rewrites cpu.profile -// every few seconds: L = samples whose leaf is that symbol, I = samples with that symbol anywhere in the chain. +// ---- sampler: where the render thread's CPU goes, inside a command buffer's window (creation to commit) and between windows ---- +// A thread suspends the render thread every 2 ms and keeps the program counter plus the frame-pointer chain in a ring (no allocation, no locks: the +// suspended thread may hold any). The writer thread resolves the addresses with dladdr and rewrites cpu.profile every few seconds: L = samples +// inside a window whose leaf is that symbol, I = samples with that symbol anywhere in the chain; G and J the same for the gaps between windows, +// the time the render thread spends on something other than encoding. sheng-gpu-sample=0 on the kernel command line turns the sampler off. static std::atomic gSampleThread{0}; static std::atomic gSampleTop{0}; -struct StackSample {uint64_t frames[8];}; +struct StackSample {uint64_t frames[8];uint32_t gap;}; +static std::atomic gSampleInWindow{false}; +static bool samplerWanted() { + static const bool wanted=[]{ + char args[1024]={};size_t size=sizeof args; + if(sysctlbyname("kern.bootargs",args,&size,NULL,0))return true; + return !strstr(args,"sheng-gpu-sample=0"); + }(); + return wanted; +} static StackSample gSamples[8192]; static std::atomic gSampleHead{0}; static std::atomic gSamplerStarted{false}; static void sampleWindowOpen() { -#ifndef NB_SAMPLER - return; // the sampler suspends the render thread 2000 times a second: off unless asked for -#endif + if(!samplerWanted())return; char name[32]={};pthread_getname_np(pthread_self(),name,sizeof name); if(strncmp(name,"com.apple.coreanimation",23))return; + gSampleInWindow.store(true); gSampleTop.store((uint64_t)pthread_get_stackaddr_np(pthread_self())); gSampleThread.store(pthread_mach_thread_np(pthread_self())); if(!gSamplerStarted.exchange(true))std::thread([]{ pthread_setname_np("frame-sampler"); for(;;) { - usleep(500); + usleep(2000); const mach_port_t port=gSampleThread.load(); if(!port)continue; if(thread_suspend(port)!=KERN_SUCCESS)continue; @@ -252,19 +268,20 @@ static void sampleWindowOpen() { } } thread_resume(port); + sample.gap=gSampleInWindow.load()?0:1; if(sample.frames[0])gSamples[gSampleHead.fetch_add(1)%8192]=sample; } }).detach(); } static void sampleWindowClose() { - if(gSampleThread.load()==pthread_mach_thread_np(pthread_self()))gSampleThread.store(0); + if(gSampleThread.load()==pthread_mach_thread_np(pthread_self()))gSampleInWindow.store(false); // the thread stays sampled: its gaps are the data } static void sampleWriteProfile(uint32_t &consumed,int &fd,const char *const *paths) { static std::unordered_map names; - static std::unordered_map leaf,inclusive; + static std::unordered_map leaf,inclusive,gapLeaf,gapInclusive; struct AddressCount {uint32_t leaf=0,inclusive=0;}; static std::unordered_map addresses; - static uint32_t total=0,rounds=0; + static uint32_t total=0,rounds=0,gapTotal=0; const uint32_t head=gSampleHead.load(); if(head-consumed>8000)consumed=head-8000; auto resolve=[&](uint64_t address)->const std::string & { @@ -281,7 +298,10 @@ static void sampleWriteProfile(uint32_t &consumed,int &fd,const char *const *pat }; while(consumed0)name.resize(plus); bool dup=false;for(unsigned j=0;j> rows; for(auto &entry:addresses)rows.push_back({entry.second.inclusive,entry.first}); @@ -325,11 +345,58 @@ static void traceStartWriter() { static const char *const paths[]={"/private/var/mobile/Library/Caches/com.apple.backboardd/m2v/frames.trace","/private/var/root/Library/Caches/com.apple.backboardd/m2v/frames.trace"}; uint32_t written=0,sampled=0;int profileFd=-1; static const char *const profilePaths[]={"/private/var/mobile/Library/Caches/com.apple.backboardd/m2v/cpu.profile","/private/var/root/Library/Caches/com.apple.backboardd/m2v/cpu.profile"}; + struct PassRec {uint64_t ns;uint32_t seq;uint16_t width,height;uint8_t mode,colors,flags,swap;uint16_t draws,format;uint32_t kpx;}; + struct SubRec {uint64_t ns;uint32_t seq,fence,syncWaitUs,callUs,spaceWaitUs;uint16_t busy;uint8_t chunks,buffers;}; + static_assert(sizeof(PassRec)==32 && sizeof(SubRec)==32,"turnip ring records"); + uint32_t passFrom=0,subFrom=0; + void *turnip=nullptr; + uint32_t (*passRead)(uint32_t,PassRec *,uint32_t,uint32_t *)=nullptr; + uint32_t (*subRead)(uint32_t,SubRec *,uint32_t,uint32_t *)=nullptr; + bool header=false; for(;;) { - usleep(400000); + usleep(250000); sampleWriteProfile(sampled,profileFd,profilePaths); if(fd<0)for(const char *path:paths){fd=open(path,O_WRONLY|O_CREAT|O_TRUNC,0644);if(fd>=0)break;} if(fd<0)continue; + if(!turnip && !gLoaderPath.empty()) { // the runtime loads Turnip after this thread may have started + turnip=dlopen(gLoaderPath.c_str(),RTLD_NOW|RTLD_NOLOAD); + if(turnip) { + passRead=(uint32_t (*)(uint32_t,PassRec *,uint32_t,uint32_t *))dlsym(turnip,"tu_pass_ring_read"); + subRead=(uint32_t (*)(uint32_t,SubRec *,uint32_t,uint32_t *))dlsym(turnip,"tu_darwin_submit_ring_read"); + } + } + if(!header) { + header=true; + struct timeval wall;gettimeofday(&wall,nullptr); + TraceRec rec={};rec.type=0;rec.t0=nowUs();rec.t1=(uint64_t)wall.tv_sec*1000000ull+(uint64_t)wall.tv_usec;rec.b=(uint32_t)getpid();rec.c=(uint32_t)wall.tv_sec;rec.d=2;tracePush(rec); + } + // Turnip's own rings (render passes, kernel submissions) go into the same file, in the order they are read. + if(passRead) { + PassRec recs[64];uint32_t newest=0; + for(;;) { + const uint32_t n=passRead(passFrom,recs,64,&newest); + for(uint32_t i=0;i)renderCommandEncoderWithDescriptor:(MTLRenderPassDescriptor *)d { NmbRenderPassDescriptor pass={}; - // type 6 a full-screen render pass's target: {t0 begin, t1 the command buffer's creation time (joins type 1), t2 the surface's base address in this - // task (the kernel's import list names it by the same address), a/b width and height, c attachment, tid}. - for(unsigned i=0;i<2;i++) { - MTLRenderPassColorAttachmentDescriptor *c=d.colorAttachments[i]; - id target=c.resolveTexture?c.resolveTexture:c.texture; - if(target && [(id)target isKindOfClass:[NBTexture class]] && [(NBTexture *)target surface] && target.width*target.height>=2000000) { - TraceRec rec={};rec.type=6;rec.t0=nowUs();rec.t1=createdUs;rec.t2=(uint64_t)(uintptr_t)IOSurfaceGetBaseAddress([(NBTexture *)target surface]); - rec.a=(uint32_t)target.width;rec.b=(uint32_t)target.height;rec.c=i;rec.tid=threadIdLow();tracePush(rec);traceStartWriter(); - } - } _dumpPasses++; if(dumpMaybe(self)) { if(!_dumpTargets)_dumpTargets=[NSMutableArray array]; @@ -3968,10 +4025,18 @@ static bool pipelineArchives(NSArray *list,std::vector &arch if(!pipelineArchives(d.binaryArchives,archives,error)){delete p;return nil;} const NmbPipelineOptions creation={(uint64_t)options,archives.data(),archives.size()}; const uint64_t pipelineBegan=nowUs();const NBCost costBegan=NBCost::now(); + uint64_t cacheBefore[10]={};if(!gJournalPath.empty())nmb_device_work_stats(gDevice,cacheBefore); NmbRenderPipeline *handle=nmb_render_pipeline_new_with_options(gDevice,p,&creation);gTimePipeline.add(pipelineBegan); if(handle && reflection && wantsReflection(options))*reflection=(MTLRenderPipelineReflection *)renderReflection(p); delete p; - journalRenderPipeline(d,pipelineBegan,handle?nullptr:nmb_last_error(),gJournalPath.empty()?std::string():NBCost::now().since(costBegan)); + std::string cost=gJournalPath.empty()?std::string():NBCost::now().since(costBegan); + if(!gJournalPath.empty()) { // what the shader cache did for this making: artifacts found, not found, handed to the directory + uint64_t after[10]={};nmb_device_work_stats(gDevice,after); + char text[96];snprintf(text,sizeof text," cache hit=%llu miss=%llu wr=%llu",(unsigned long long)(after[7]-cacheBefore[7]),(unsigned long long)(after[8]-cacheBefore[8]), + (unsigned long long)(after[9]-cacheBefore[9])); + cost+=text; + } + journalRenderPipeline(d,pipelineBegan,handle?nullptr:nmb_last_error(),cost); if(!handle) { // The tail of the text (what a client's problem row keeps) names the fragment function, the linked functions given and the compiler's // state: a missing linked function there is RenderBox's stitched custom_blend, which needs the compiler. @@ -4313,6 +4378,17 @@ extern "C" size_t native_bridge_metal_telemetry(char *out,size_t size) { (unsigned long long)work[0],ms(work[1]),ms(work[2]),ms(work[3]),(unsigned long long)work[4]); row("V cache hit%llu miss%llu wr%llu | msg i%llu w%llu",(unsigned long long)work[7],(unsigned long long)work[8],(unsigned long long)work[9],(unsigned long long)work[5], (unsigned long long)work[6]); + // The shader cache's full counts (nmb_device_shader_cache_detail): functions found/translated, compute and render pipelines found/compiled, entries + // rejected, unreadable, handed to the directory (queued, written on the asking thread), bytes written, failed writes, evictions, the driver's own + // pipeline cache: loads, saves, hits, misses. + uint64_t sc[24]={}; + if(nmb_device_shader_cache_detail(gDevice,sc)) { + row("SC fn %llu/%llu cp %llu/%llu rp %llu/%llu rej %llu rd-fail %llu",(unsigned long long)sc[2],(unsigned long long)sc[3],(unsigned long long)sc[4],(unsigned long long)sc[5], + (unsigned long long)sc[6],(unsigned long long)sc[7],(unsigned long long)sc[8],(unsigned long long)sc[9]); + row("SCb wr %llu q %llu thr %llu %lluK wfail %llu evict %llu qhit %llu | drv load %llu save %llu hit %llu miss %llu",(unsigned long long)sc[10],(unsigned long long)sc[11], + (unsigned long long)sc[12],(unsigned long long)(sc[13]>>10),(unsigned long long)sc[15],(unsigned long long)sc[16],(unsigned long long)sc[14],(unsigned long long)sc[17], + (unsigned long long)sc[20],(unsigned long long)sc[18],(unsigned long long)sc[19]); + } // Where textures over buffers and surfaces cost copies: host-page textures in place (of them at Metal's pitch), shadowed for their // pitch and for no driver image; shadowed textures, their copies in, texels copied back; host-page render targets shadowed. uint64_t tc[8]={}; @@ -4360,6 +4436,20 @@ extern "C" size_t native_bridge_metal_telemetry(char *out,size_t size) { (unsigned long long)rm[2],(unsigned long long)(rm[3]/1000),(unsigned long long)rm[4],(unsigned long long)(rm[5]/1000),(unsigned long long)rm[6]); } } + // The tile-memory side of GMEM loads and stores (tu_gmem_side_stats), per site: 3D store source, 3D load source, 2D store source, input attachment; each + // transfers / of them with a swap other than WZYX / with MUTABLEEN / of a linear image. The second number must stay 0 for linear images. + { + uint64_t gm[16]={}; + static void *turnip=gLoaderPath.empty()?nullptr:dlopen(gLoaderPath.c_str(),RTLD_NOW|RTLD_NOLOAD); + static void (*sideStats)(uint64_t *)=turnip?(void (*)(uint64_t *))dlsym(turnip,"tu_gmem_side_stats"):nullptr; + if(sideStats) { + sideStats(gm); + row("GM s3d %llu/%llu/%llu/%llu l3d %llu/%llu/%llu/%llu",(unsigned long long)gm[0],(unsigned long long)gm[1],(unsigned long long)gm[2],(unsigned long long)gm[3], + (unsigned long long)gm[4],(unsigned long long)gm[5],(unsigned long long)gm[6],(unsigned long long)gm[7]); + row("GMb s2d %llu/%llu/%llu/%llu in %llu/%llu/%llu/%llu",(unsigned long long)gm[8],(unsigned long long)gm[9],(unsigned long long)gm[10],(unsigned long long)gm[11], + (unsigned long long)gm[12],(unsigned long long)gm[13],(unsigned long long)gm[14],(unsigned long long)gm[15]); + } + } // Who allocated the device memory (4 MiB or more at once): the three call stacks with the most bytes, as MiB, allocations and return // addresses relative to their image (+offset in the plugin, whose link map NativeAdrenoMetal.map names them; *offset in another). { diff --git a/bridge/NativeMetalObjects.h b/bridge/NativeMetalObjects.h index 42f6c6b..02977fb 100644 --- a/bridge/NativeMetalObjects.h +++ b/bridge/NativeMetalObjects.h @@ -41,6 +41,10 @@ size_t nmb_device_name(const NmbDevice *, char *out, size_t size); uint64_t nmb_device_max_buffer_length(const NmbDevice *); // Pipeline makings (count, total us, longest us, waited us, failed), sink messages (info, warning+), shader cache hits/misses/writes. bool nmb_device_work_stats(const NmbDevice *, uint64_t out[10]); +/* The shader cache's full counts in 24 words: hits, misses, function hits and misses, compute pipeline hits and misses, render pipeline hits and misses, + * rejected, read failures, writes, writes queued, writes on a calling thread, bytes written, queued hits, write failures, evictions, driver cache loads, + * driver pipeline hits, driver pipeline misses, driver saves, then three spare zeros. */ +bool nmb_device_shader_cache_detail(const NmbDevice *, uint64_t out[24]); /* The translator source the runtime was built from, which keys the shader cache (build-native-metal-bridge.py). */ const char *nmb_runtime_build(void); // Linear images made/refused/refused again, shadowed textures, shadow copies, texels copied back, linear passes in place/through copies. diff --git a/bridge/NativeMetalObjects.rs b/bridge/NativeMetalObjects.rs index f8e0a78..330b164 100644 --- a/bridge/NativeMetalObjects.rs +++ b/bridge/NativeMetalObjects.rs @@ -310,6 +310,46 @@ pub extern "C" fn nmb_device_work_stats(device: *const NmbDevice, out: *mut u64) }) } +/// The device's shader cache counts into the caller's 24 words (`nmb_device_shader_cache_detail` in the header): everything +/// `ShaderCacheStatistics` holds that tells whether the cache worked and why not. +#[unsafe(no_mangle)] +pub extern "C" fn nmb_device_shader_cache_detail(device: *const NmbDevice, out: *mut u64) -> bool { + guard(false, || { + if out.is_null() { + return Err("null stats".into()); + } + // SAFETY: the header's 24 words. + let out = unsafe { std::slice::from_raw_parts_mut(out, 24) }; + let c = get(device)?.0.shader_cache_statistics(); + let words = [ + c.hits, + c.misses, + c.function_hits, + c.function_misses, + c.compute_pipeline_hits, + c.compute_pipeline_misses, + c.render_pipeline_hits, + c.render_pipeline_misses, + c.rejected, + c.read_failures, + c.writes, + c.writes_queued, + c.writes_on_thread, + c.written_bytes, + c.queued_hits, + c.write_failures, + c.evictions, + c.driver_loads, + c.driver_pipeline_hits, + c.driver_pipeline_misses, + c.driver_saves, + ]; + out.fill(0); + out[..words.len()].copy_from_slice(&words); + Ok(true) + }) +} + /// The device's texture copy counts into the caller's eight words: textures over host pages in place, of them linear images at Metal's /// row pitch, host-page textures shadowed for their pitch and for no image the driver makes, textures given a shadow image, shadow /// copies in and texels copied back, and host-page textures shadowed as render targets. From eb4b68fb24441ed0b216c18bd41e94106485c887 Mon Sep 17 00:00:00 2001 From: rpone Date: Sat, 31 Oct 2026 00:46:15 +0800 Subject: [PATCH 83/92] fix(metal): write cpu.profile at a fixed size in place The boot export measures a file once and copies exactly that many bytes, so the profile that grew after the measurement came out as its first 16 bytes. The file is now 256 KiB from the first write and rewritten in place; the address list keeps the busiest 800. --- NativeBridgeMetalFrontend.mm | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 89021ff..ca0900f 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -327,14 +327,17 @@ static void sampleWriteProfile(uint32_t &consumed,int &fd,const char *const *pat std::vector> rows; for(auto &entry:addresses)rows.push_back({entry.second.inclusive,entry.first}); std::sort(rows.rbegin(),rows.rend()); - for(unsigned i=0;iout.size()?fixedSize:out.size(),'\n');out.resize(fixedSize); + if(pwrite(fd,out.data(),out.size(),0)<0)return; } static void traceStartWriter() { bool expected=false; From 7b885c29e1b9d572d2bf82dbd0447620b7a613ec Mon Sep 17 00:00:00 2001 From: rpone Date: Sat, 31 Oct 2026 01:10:10 +0800 Subject: [PATCH 84/92] fix(metal): only a process that sampled writes cpu.profile A process without samples no longer rewrites the file, and none truncates it. Not yet confirmed on hardware: the last run's profile stayed at zero samples. --- NativeBridgeMetalFrontend.mm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index ca0900f..1587a6c 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -312,8 +312,8 @@ static void sampleWriteProfile(uint32_t &consumed,int &fd,const char *const *pat if(!dup){seen[count++]=name;inclusiveMap[name]++;} } } - if(++rounds%12)return; - if(fd<0)for(int i=0;i<2 && fd<0;i++)fd=open(paths[i],O_WRONLY|O_CREAT|O_TRUNC,0644); + if(++rounds%12 || !total)return; // a process that never sampled must not overwrite the one that did + if(fd<0)for(int i=0;i<2 && fd<0;i++)fd=open(paths[i],O_WRONLY|O_CREAT,0644); // no O_TRUNC: the file keeps its fixed size if(fd<0)return; auto top=[&](std::unordered_map &map,char tag,unsigned limit,std::string &out) { std::vector> rows; From 809075bdd95ffd9ba14a1f36a9372ab4365b7c68 Mon Sep 17 00:00:00 2001 From: rpone Date: Sat, 31 Oct 2026 01:31:39 +0800 Subject: [PATCH 85/92] feat(metal): CPU samples and row-copy counters in the frame trace The sampler's stacks go into frames.trace as types 9 (sample) and 10 (function name) instead of a separate profile file the render process could not rewrite. Type 11 carries Turnip's counters for copies that fall back to one blit per row. --- NativeBridgeMetalFrontend.mm | 109 +++++++++++++++-------------------- 1 file changed, 48 insertions(+), 61 deletions(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 1587a6c..a3a7471 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -218,12 +218,12 @@ static void traceNote(uint32_t tid) { // ---- sampler: where the render thread's CPU goes, inside a command buffer's window (creation to commit) and between windows ---- // A thread suspends the render thread every 2 ms and keeps the program counter plus the frame-pointer chain in a ring (no allocation, no locks: the -// suspended thread may hold any). The writer thread resolves the addresses with dladdr and rewrites cpu.profile every few seconds: L = samples -// inside a window whose leaf is that symbol, I = samples with that symbol anywhere in the chain; G and J the same for the gaps between windows, -// the time the render thread spends on something other than encoding. sheng-gpu-sample=0 on the kernel command line turns the sampler off. +// suspended thread may hold any). The writer thread resolves the addresses with dladdr and puts the samples into the frame trace (types 9 and 10, +// below); the leaf and inclusive histograms inside windows and in the gaps between windows (the time the render thread spends on something other +// than encoding) are built offline. sheng-gpu-sample=0 on the kernel command line turns the sampler off. static std::atomic gSampleThread{0}; static std::atomic gSampleTop{0}; -struct StackSample {uint64_t frames[8];uint32_t gap;}; +struct StackSample {uint64_t frames[8];uint64_t us;uint32_t gap;}; static std::atomic gSampleInWindow{false}; static bool samplerWanted() { static const bool wanted=[]{ @@ -269,6 +269,7 @@ static void sampleWindowOpen() { } thread_resume(port); sample.gap=gSampleInWindow.load()?0:1; + sample.us=nowUs(); if(sample.frames[0])gSamples[gSampleHead.fetch_add(1)%8192]=sample; } }).detach(); @@ -276,68 +277,43 @@ static void sampleWindowOpen() { static void sampleWindowClose() { if(gSampleThread.load()==pthread_mach_thread_np(pthread_self()))gSampleInWindow.store(false); // the thread stays sampled: its gaps are the data } -static void sampleWriteProfile(uint32_t &consumed,int &fd,const char *const *paths) { - static std::unordered_map names; - static std::unordered_map leaf,inclusive,gapLeaf,gapInclusive; - struct AddressCount {uint32_t leaf=0,inclusive=0;}; - static std::unordered_map addresses; - static uint32_t total=0,rounds=0,gapTotal=0; +// The writer thread turns the ring's samples into trace records: type 10 names a function the first time a sample holds it {a id, the name (image`symbol, +// 60 characters) in t0..t3 and b..tid2}, type 9 is one sample {t0 nowUs() when it was taken, t1 1 when it fell between two windows, a..d the eight frames as +// 16-bit function ids, pc first, 0 = no frame}. The offline reader (local-tools/blue-block/prof.py) rebuilds the leaf and inclusive histograms for the +// windows and the gaps from them. +// Type 11 is Turnip's counters for the copies that need a row-by-row path (tu_copy_row_stats), written when they change {t0 nowUs(), t1 buffer to image copies | +// image to buffer copies << 32, t2 how many of each were split per row (same packing), t3 rows of the split buffer to image copies, a rows of the split image to buffer +// copies, b rows of the largest split copy, c its pitch in bytes, d its VkFormat, tid its width, cpu0 its height}. +static void sampleEmitTrace(uint32_t &consumed) { + static std::unordered_map addressId; + static std::unordered_map nameId; const uint32_t head=gSampleHead.load(); if(head-consumed>8000)consumed=head-8000; - auto resolve=[&](uint64_t address)->const std::string & { - auto it=names.find(address);if(it!=names.end())return it->second; - Dl_info info={};std::string name; + auto identify=[&](uint64_t address)->uint32_t { + auto cached=addressId.find(address);if(cached!=addressId.end())return cached->second; + Dl_info info={};char text[256]; if(dladdr((const void *)address,&info)){ const char *image=info.dli_fname?strrchr(info.dli_fname,'/'):NULL;image=image?image+1:(info.dli_fname?info.dli_fname:"?"); - char text[256]; - if(info.dli_sname)snprintf(text,sizeof text,"%s`%s+%llu",image,info.dli_sname,(unsigned long long)(address-(uint64_t)info.dli_saddr)); + if(info.dli_sname)snprintf(text,sizeof text,"%s`%s",image,info.dli_sname); else snprintf(text,sizeof text,"%s`+0x%llx",image,(unsigned long long)(address-(uint64_t)info.dli_fbase)); - name=text; - } else name="?"; - return names[address]=name; + } else snprintf(text,sizeof text,"?"); + auto found=nameId.find(text); + if(found!=nameId.end())return addressId[address]=found->second; + uint32_t id=(uint32_t)nameId.size()+1; + if(id>0xfffe)return addressId[address]=0xffff; + nameId[text]=id; + char field[60]={};strncpy(field,text,sizeof field-1); + TraceRec rec={};rec.type=10;rec.a=id;memcpy(&rec.t0,field,32);memcpy(&rec.b,field+32,28);tracePush(rec); + return addressId[address]=id; }; while(consumed0)name.resize(plus); - bool dup=false;for(unsigned j=0;j &map,char tag,unsigned limit,std::string &out) { - std::vector> rows; - for(auto &entry:map)rows.push_back({entry.second,entry.first}); - std::sort(rows.rbegin(),rows.rend()); - for(unsigned i=0;i> rows; - for(auto &entry:addresses)rows.push_back({entry.second.inclusive,entry.first}); - std::sort(rows.rbegin(),rows.rend()); - for(unsigned i=0;iout.size()?fixedSize:out.size(),'\n');out.resize(fixedSize); - if(pwrite(fd,out.data(),out.size(),0)<0)return; } static void traceStartWriter() { bool expected=false; @@ -346,8 +322,7 @@ static void traceStartWriter() { pthread_setname_np("frame-trace"); int fd=-1; static const char *const paths[]={"/private/var/mobile/Library/Caches/com.apple.backboardd/m2v/frames.trace","/private/var/root/Library/Caches/com.apple.backboardd/m2v/frames.trace"}; - uint32_t written=0,sampled=0;int profileFd=-1; - static const char *const profilePaths[]={"/private/var/mobile/Library/Caches/com.apple.backboardd/m2v/cpu.profile","/private/var/root/Library/Caches/com.apple.backboardd/m2v/cpu.profile"}; + uint32_t written=0,sampled=0; struct PassRec {uint64_t ns;uint32_t seq;uint16_t width,height;uint8_t mode,colors,flags,swap;uint16_t draws,format;uint32_t kpx;}; struct SubRec {uint64_t ns;uint32_t seq,fence,syncWaitUs,callUs,spaceWaitUs;uint16_t busy;uint8_t chunks,buffers;}; static_assert(sizeof(PassRec)==32 && sizeof(SubRec)==32,"turnip ring records"); @@ -355,10 +330,12 @@ static void traceStartWriter() { void *turnip=nullptr; uint32_t (*passRead)(uint32_t,PassRec *,uint32_t,uint32_t *)=nullptr; uint32_t (*subRead)(uint32_t,SubRec *,uint32_t,uint32_t *)=nullptr; + void (*copyStats)(uint64_t *)=nullptr; + uint64_t copyLast[16]={}; bool header=false; for(;;) { usleep(250000); - sampleWriteProfile(sampled,profileFd,profilePaths); + sampleEmitTrace(sampled); if(fd<0)for(const char *path:paths){fd=open(path,O_WRONLY|O_CREAT|O_TRUNC,0644);if(fd>=0)break;} if(fd<0)continue; if(!turnip && !gLoaderPath.empty()) { // the runtime loads Turnip after this thread may have started @@ -366,6 +343,7 @@ static void traceStartWriter() { if(turnip) { passRead=(uint32_t (*)(uint32_t,PassRec *,uint32_t,uint32_t *))dlsym(turnip,"tu_pass_ring_read"); subRead=(uint32_t (*)(uint32_t,SubRec *,uint32_t,uint32_t *))dlsym(turnip,"tu_darwin_submit_ring_read"); + copyStats=(void (*)(uint64_t *))dlsym(turnip,"tu_copy_row_stats"); } } if(!header) { @@ -400,6 +378,15 @@ static void traceStartWriter() { if(n<64)break; } } + if(copyStats) { + uint64_t now[16]={};copyStats(now); + if(memcmp(now,copyLast,sizeof now)) { + memcpy(copyLast,now,sizeof now); + TraceRec rec={};rec.type=11;rec.t0=nowUs();rec.t1=now[0]|now[3]<<32;rec.t2=now[1]|now[4]<<32;rec.t3=now[2]; + rec.a=(uint32_t)now[5];rec.b=(uint32_t)now[6];rec.c=(uint32_t)now[7];rec.d=(uint32_t)now[9];rec.tid=(uint32_t)now[8];rec.cpu0=(uint32_t)(now[8]>>32); + tracePush(rec); + } + } uint32_t head=gTraceHead.load(); if(head Date: Sat, 31 Oct 2026 02:01:04 +0800 Subject: [PATCH 86/92] fix(metal): repeat the sampler's function names every 15 s The writer drops the oldest records when the ring laps (a burst of Turnip pass records at the first flush took the first 64 names). --- NativeBridgeMetalFrontend.mm | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index a3a7471..f61b7a8 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -284,9 +284,15 @@ static void sampleWindowClose() { // Type 11 is Turnip's counters for the copies that need a row-by-row path (tu_copy_row_stats), written when they change {t0 nowUs(), t1 buffer to image copies | // image to buffer copies << 32, t2 how many of each were split per row (same packing), t3 rows of the split buffer to image copies, a rows of the split image to buffer // copies, b rows of the largest split copy, c its pitch in bytes, d its VkFormat, tid its width, cpu0 its height}. +static void emitDefinition(uint32_t id,const std::string &text) { + char field[60]={};strncpy(field,text.c_str(),sizeof field-1); + TraceRec rec={};rec.type=10;rec.a=id;memcpy(&rec.t0,field,32);memcpy(&rec.b,field+32,28);tracePush(rec); +} static void sampleEmitTrace(uint32_t &consumed) { static std::unordered_map addressId; static std::unordered_map nameId; + static std::vector> defined; // every definition, repeated every 15 s: the writer drops the oldest records when the ring laps + static uint64_t definedAt=0; const uint32_t head=gSampleHead.load(); if(head-consumed>8000)consumed=head-8000; auto identify=[&](uint64_t address)->uint32_t { @@ -302,10 +308,15 @@ static void sampleEmitTrace(uint32_t &consumed) { uint32_t id=(uint32_t)nameId.size()+1; if(id>0xfffe)return addressId[address]=0xffff; nameId[text]=id; - char field[60]={};strncpy(field,text,sizeof field-1); - TraceRec rec={};rec.type=10;rec.a=id;memcpy(&rec.t0,field,32);memcpy(&rec.b,field+32,28);tracePush(rec); + defined.push_back({id,text}); + emitDefinition(id,text); return addressId[address]=id; }; + const uint64_t now=nowUs(); + if(now-definedAt>=15000000ull) { + definedAt=now; + for(auto &entry:defined)emitDefinition(entry.first,entry.second); + } while(consumed Date: Sat, 31 Oct 2026 18:06:39 +0800 Subject: [PATCH 87/92] perf(memory): spare image blocks and a room keeper thread Bridge patch 0021: a dedicated image block whose image went stays spare for the next dedicated image of its pool, freed after 10 s, when a block cannot be made, or under memory pressure. Pool room is kept by a device thread instead of the creation call. The frontend gives the spare memory back on the memory-pressure source. --- NativeBridgeMetalFrontend.mm | 4 +- bridge/NativeMetalObjects.h | 2 + bridge/NativeMetalObjects.rs | 7 + ...1-spare-image-blocks-and-room-keeper.patch | 382 ++++++++++++++++++ 4 files changed, 394 insertions(+), 1 deletion(-) create mode 100644 bridge/patches/0021-spare-image-blocks-and-room-keeper.patch diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index f61b7a8..4b81edf 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -918,7 +918,8 @@ static void watchImportPressure() { dispatch_source_t source=dispatch_source_create(DISPATCH_SOURCE_TYPE_MEMORYPRESSURE,0, DISPATCH_MEMORYPRESSURE_WARN|DISPATCH_MEMORYPRESSURE_CRITICAL,dispatch_get_global_queue(QOS_CLASS_UTILITY,0)); if(!source)return; - dispatch_source_set_event_handler(source,^{trimHeldImports(true);}); + // The runtime's spare and kept empty memory blocks go too (bridge patch 0021), as Metal's resource cache gives memory back. + dispatch_source_set_event_handler(source,^{trimHeldImports(true);if(gDevice)(void)nmb_device_release_kept_memory(gDevice);}); dispatch_activate(source); }); } @@ -4174,6 +4175,7 @@ extern "C" id native_bridge_metal_open(const char *loader,const char *cache,uint gDevice=nmb_device_open(loader,cache); if(loader)gLoaderPath=loader; if(!gDevice){NBLOG(@"native-metal: device refused: %s",nmb_last_error());return nil;} + watchImportPressure(); if(cache){gJournalPath=std::string(cache)+"/pipelines.log";gJournalBegan=nowUs();} char name[128]={};nmb_device_name(gDevice,name,sizeof name);gName=[NSString stringWithUTF8String:name];gRegistryID=registryID; // The target families supportsFamily: answers true, with the rows of Apple's tables the device does not meet in them (nmb_device_family_waivers). diff --git a/bridge/NativeMetalObjects.h b/bridge/NativeMetalObjects.h index 02977fb..e0c1258 100644 --- a/bridge/NativeMetalObjects.h +++ b/bridge/NativeMetalObjects.h @@ -55,6 +55,8 @@ bool nmb_device_pass_counts(const NmbDevice *, uint64_t out[10]); // Bytes of pooled blocks live and placed in them, empty kept, dedicated, host imports; bytes allocated so far; pooled and dedicated // blocks live; GPU pool blocks made and released. bool nmb_device_memory_counts(const NmbDevice *, uint64_t out[10]); +/* Frees the runtime's spare dedicated blocks and kept empty pooled blocks (memory pressure); returns how many went. */ +size_t nmb_device_release_kept_memory(const NmbDevice *); // Call stacks that allocated device memory of 4 MiB or more, most bytes first: records of twelve words (bytes, allocations, ten return // addresses); returns how many were written, at most max. size_t nmb_allocation_sites(uint64_t *out, size_t max); diff --git a/bridge/NativeMetalObjects.rs b/bridge/NativeMetalObjects.rs index 330b164..b779b25 100644 --- a/bridge/NativeMetalObjects.rs +++ b/bridge/NativeMetalObjects.rs @@ -407,6 +407,13 @@ pub extern "C" fn nmb_device_memory_counts(device: *const NmbDevice, out: *mut u }) } +/// Free the runtime's spare dedicated blocks and kept empty pooled blocks (bridge patch 0021), as Metal gives its resource cache's +/// memory back when the system reports memory pressure: how many blocks went. +#[unsafe(no_mangle)] +pub extern "C" fn nmb_device_release_kept_memory(device: *const NmbDevice) -> usize { + guard(0, || Ok(get(device)?.0.release_kept_memory())) +} + /// The call stacks that allocated device memory of 4 MiB or more (bridge patch 0020), most bytes first, into the caller's `max` records /// of twelve words: bytes, allocations, then ten return addresses (0 past the stack's end). Returns the records written. #[unsafe(no_mangle)] diff --git a/bridge/patches/0021-spare-image-blocks-and-room-keeper.patch b/bridge/patches/0021-spare-image-blocks-and-room-keeper.patch new file mode 100644 index 0000000..a91172a --- /dev/null +++ b/bridge/patches/0021-spare-image-blocks-and-room-keeper.patch @@ -0,0 +1,382 @@ +diff --git a/crates/metal2vulkan-runtime/src/device/mod.rs b/crates/metal2vulkan-runtime/src/device/mod.rs +index 053cafe9..96352266 100644 +--- a/crates/metal2vulkan-runtime/src/device/mod.rs ++++ b/crates/metal2vulkan-runtime/src/device/mod.rs +@@ -27,6 +27,7 @@ pub(super) mod keeper; + pub(super) mod prime; + pub(super) mod properties; + pub(crate) mod relay; ++mod room; + mod staging; + pub(super) mod submit; + pub(crate) mod submit_stalls; +@@ -501,6 +502,9 @@ pub(super) struct DeviceShared { + /// The thread that lets go of what a released state's pool pieces gave back as they ended + /// (`pipeline/reaper.rs`). + pub(super) reaper: crate::pipeline::reaper::Reaper, ++ /// The thread that keeps room in the pools the encodings and commits place their own ++ /// resources in, asked by the creation calls (`device/room.rs`). ++ room: room::Room, + /// The trails its threads open between a signal and their last let-go of what they held for + /// the work, which a command buffer's waiters wait for (`trails.rs`). + pub(crate) trails: Arc, +@@ -718,6 +722,9 @@ impl Drop for DeviceShared { + // What the reaper is handed holds the device: none is left, and it only stops, unless + // this teardown runs on its thread. + self.reaper.stop(); ++ // The room keeper holds the device only for a pass: it stops, unless this teardown runs ++ // on its thread. ++ self.room.stop(); + // What the residency thread releases holds the device: none is left. + #[cfg(unix)] + self.residency.stop(); +@@ -1014,13 +1021,24 @@ impl Device { + /// a pipeline state's, a buffer's, a texture's, a heap's), so that no encoding or commit makes + /// a memory block, whose `vkAllocateMemory` waited 2.37 s for other processes' GPU work on + /// NVIDIA's driver. Nothing in a host step, a serial point of the command buffers' stream +- /// that makes only what it needs. ++ /// that makes only what it needs. The device's room keeper makes the blocks, so the call ++ /// returns at once (`device/room.rs`). + pub(crate) fn keep_room(&self) { + if crate::exit_guard::in_host_step() { + return; + } ++ self.0.room.ask(self); ++ } ++ ++ /// Give back the memory kept for later placements: every spare dedicated block and every ++ /// kept empty pooled block (`memory::SPARE_FOR`), as Metal's resource cache gives memory ++ /// back when the system reports memory pressure. How many blocks went. No Metal counterpart. ++ pub fn release_kept_memory(&self) -> usize { ++ let Some(_busy) = crate::exit_guard::enter() else { ++ return 0; ++ }; + let shared = self.shared(); +- crate::memory::keep_room(&shared.memory, shared.vk(), &shared.counts); ++ lock(&shared.memory).release_spare(shared.vk()) + } + + /// Whether `other` is this device (the same handle, not merely the same physical device). +@@ -1898,6 +1916,7 @@ impl Device { + uncommitted: Arc::default(), + pipeline_pool: Default::default(), + reaper: Default::default(), ++ room: Default::default(), + trails: Arc::default(), + recoveries: Arc::default(), + libraries: graphics_pipeline_library.then(Default::default), +diff --git a/crates/metal2vulkan-runtime/src/device/room.rs b/crates/metal2vulkan-runtime/src/device/room.rs +new file mode 100644 +index 00000000..d0e24d65 +--- /dev/null ++++ b/crates/metal2vulkan-runtime/src/device/room.rs +@@ -0,0 +1,122 @@ ++//! The device's room keeper: one thread of the device's that keeps room in the pools its encodings ++//! and commits place their own resources in (`memory::keep_room`), so the creation call that asks ++//! for room returns at once. ++//! ++//! - **Asked by the creation calls.** [`Room::ask`] marks that room is wanted and wakes the ++//! thread, which it starts unless it runs; asks that come while the thread works are one more ++//! pass, not one each. ++//! - **Why a thread.** Making a block is a `vkAllocateMemory` that a driver may make wait: on an ++//! iPadOS guest whose GPU takes the system's pages, each 64 MiB block was wired page by page as it ++//! was made, 30-110 ms in the texture creation that asked for it (Core Animation's surfaces, ++//! the widget renderer's textures). A placement that finds no room before the block is there ++//! makes its own, and the keeper's block then goes at once (`memory::keep_room`). ++//! - **Holds nothing of the device.** The thread holds a weak handle only while it waits, and the ++//! device itself only for a pass. Where that pass held its last handle, the teardown runs here, ++//! and its [`Room::stop`] does not join this thread. ++ ++use super::{lock, Device, WeakDevice}; ++use crate::exit_guard; ++use std::sync::{Arc, Condvar, Mutex, PoisonError}; ++use std::thread::JoinHandle; ++ ++/// The device's room keeper: its thread, once a creation call started it. ++#[derive(Default)] ++pub(crate) struct Room { ++ shared: Arc, ++ thread: Mutex>>, ++} ++ ++#[derive(Default)] ++struct Shared { ++ state: Mutex, ++ /// Room was asked for, or the thread is asked to stop. ++ work: Condvar, ++} ++ ++#[derive(Default)] ++struct State { ++ asked: bool, ++ stopping: bool, ++} ++ ++impl Room { ++ /// Ask for room in `device`'s pools: the thread keeps it, started unless it runs. Where the ++ /// host cannot start a thread, the room is kept here, as before there was one. ++ pub(crate) fn ask(&self, device: &Device) { ++ { ++ let mut state = lock(&self.shared.state); ++ if state.stopping { ++ return; ++ } ++ state.asked = true; ++ } ++ self.shared.work.notify_all(); ++ let mut thread = lock(&self.thread); ++ if thread.is_some() { ++ return; ++ } ++ let shared = Arc::clone(&self.shared); ++ let weak = device.downgrade(); ++ match exit_guard::spawn( ++ std::thread::Builder::new().name("metal2vulkan-room".into()), ++ move || run(&shared, &weak), ++ ) { ++ Ok(handle) => *thread = Some(handle), ++ Err(_) => { ++ drop(thread); ++ lock(&self.shared.state).asked = false; ++ keep(device); ++ } ++ } ++ } ++ ++ /// The device's teardown: the thread ends, and is joined unless this is it. ++ pub(crate) fn stop(&self) { ++ lock(&self.shared.state).stopping = true; ++ self.shared.work.notify_all(); ++ let mut thread = lock(&self.thread); ++ let own = thread ++ .as_ref() ++ .is_some_and(|handle| handle.thread().id() == std::thread::current().id()); ++ if !own { ++ if let Some(handle) = thread.take() { ++ let _ = handle.join(); ++ } ++ } ++ } ++} ++ ++/// One pass: room kept in `device`'s pools, in a section of the exit guard. ++fn keep(device: &Device) { ++ let Some(_busy) = exit_guard::enter() else { ++ return; ++ }; ++ let shared = device.shared(); ++ crate::memory::keep_room(&shared.memory, shared.vk(), &shared.counts); ++} ++ ++/// The thread's loop: a pass for each time room was asked since the last, until stopped. ++fn run(shared: &Shared, weak: &WeakDevice) { ++ loop { ++ { ++ let mut state = lock(&shared.state); ++ while !state.asked && !state.stopping { ++ state = shared ++ .work ++ .wait(state) ++ .unwrap_or_else(PoisonError::into_inner); ++ } ++ if state.stopping { ++ return; ++ } ++ state.asked = false; ++ } ++ let Some(device) = weak.upgrade() else { ++ return; ++ }; ++ keep(&device); ++ // Outside the state's lock: it may be the device's last handle, whose teardown stops ++ // this thread. ++ drop(device); ++ } ++} +diff --git a/crates/metal2vulkan-runtime/src/memory.rs b/crates/metal2vulkan-runtime/src/memory.rs +index 1f882747..e015fc9b 100644 +--- a/crates/metal2vulkan-runtime/src/memory.rs ++++ b/crates/metal2vulkan-runtime/src/memory.rs +@@ -90,6 +90,26 @@ const LOGGED_PLACEMENT_BYTES: u64 = 4 << 20; + /// guest's home screen. + pub(super) const FIRST_BLOCK_BYTES: u64 = 4 << 20; + ++/// How long a dedicated block of images whose image went stays spare for the next dedicated image ++/// of its pool ([`Memory::take_spare`]) before it is freed. Core Animation makes a memoryless ++/// attachment for every surface it renders to and releases it with the surface: where the device's ++/// lazily allocated memory is backed at the first pass (every pass renders in system memory), each ++/// one was a new 40-112 MiB allocation wired page by page, 1.4 s of an iPadOS render server's ++/// first minute in 29 of them, and its free unwired it again. No count or byte limit: as the ++/// pooled blocks kept empty, the spares go when a block of their type cannot be made ++/// ([`Memory::release_kept`]) and when the system reports memory pressure ++/// ([`Memory::release_spare`]). ++pub(super) const SPARE_FOR: Duration = Duration::from_secs(10); ++ ++/// A dedicated block of images whose image went, kept for the next dedicated image of its pool ++/// ([`SPARE_FOR`]). ++#[derive(Debug)] ++struct Spare { ++ pool: (Holds, u32), ++ block: Block, ++ since: Instant, ++} ++ + /// What memory a resource needs. + #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] + pub(super) enum MemoryClass { +@@ -754,6 +774,12 @@ struct BlockAddresses { + } + + impl Block { ++ /// Whether it is the runtime's own memory, imported from nothing: what a spare may be ++ /// ([`SPARE_FOR`]). ++ fn plain(&self) -> bool { ++ !self.imported && self.pages.is_none() && self.view.is_none() ++ } ++ + /// The cause its `vkFreeMemory` is counted under. A block freed as the one placement in it + /// went ([`FreeCause::Dedicated`]) that is an import of its own is counted as that import + /// instead: the caller's host memory ([`Memory::import_host`]) or a buffer's fresh pages +@@ -1052,6 +1078,9 @@ pub(super) struct Memory { + /// ([`binds_wait`]), so an image bound while such work may be queued takes a block of its own + /// ([`allocate_image_while_queued`]). + binds_wait: bool, ++ /// The dedicated blocks of images kept for the next dedicated image of their pool ++ /// ([`SPARE_FOR`]). ++ spare: Vec, + } + + impl Memory { +@@ -1087,6 +1116,7 @@ impl Memory { + freed_by: [0; FreeCause::COUNT], + log: None, + binds_wait: binds_wait(driver), ++ spare: Vec::new(), + } + } + +@@ -1162,6 +1192,10 @@ impl Memory { + for pool in &self.pools { + pool.count_into(&mut census, |block| block.imported); + } ++ for spare in &self.spare { ++ census.empty += 1; ++ census.empty_bytes += spare.block.size; ++ } + census + } + +@@ -1433,7 +1467,17 @@ impl Memory { + // SAFETY: nothing is placed in a kept empty block, and no command reads it. + drop(unsafe { self.let_go(device, stale, FreeCause::KeptStale, None) }); + } ++ self.age_spare(device, now); + match freed { ++ Some((block, true)) if holds == Holds::Images && block.plain() => { ++ self.say_block("kept a dedicated block of an image spare", &block); ++ self.spare.push(Spare { ++ pool: (holds, type_index), ++ block, ++ since: now, ++ }); ++ after ++ } + Some((block, dedicated)) => { + let cause = match dedicated { + true => FreeCause::Dedicated, +@@ -1449,10 +1493,15 @@ impl Memory { + /// Release every kept empty pooled block of memory type `type_index`, of buffers and of + /// images, as a block of that type fails to be made ([`place`]): how many went. + fn release_kept(&mut self, device: &ash::Device, type_index: u32) -> usize { +- let blocks: Vec = [Holds::Buffers, Holds::Images] ++ let mut blocks: Vec = [Holds::Buffers, Holds::Images] + .into_iter() + .flat_map(|holds| self.pool(holds, type_index).take_kept()) + .collect(); ++ blocks.extend( ++ (self.spare) ++ .extract_if(.., |spare| spare.pool.1 == type_index) ++ .map(|spare| spare.block), ++ ); + let count = blocks.len(); + for block in blocks { + // SAFETY: nothing is placed in a kept empty block, and no command reads it. +@@ -1461,6 +1510,46 @@ impl Memory { + count + } + ++ /// Free the spare blocks that stayed spare for [`SPARE_FOR`] at `now`. ++ fn age_spare(&mut self, device: &ash::Device, now: Instant) { ++ let stale: Vec = (self.spare) ++ .extract_if(.., |spare| now.saturating_duration_since(spare.since) >= SPARE_FOR) ++ .collect(); ++ for spare in stale { ++ // SAFETY: nothing is placed in a spare block, and no command reads it. ++ drop(unsafe { self.let_go(device, spare.block, FreeCause::KeptStale, None) }); ++ } ++ } ++ ++ /// The smallest spare block of `pool` that holds `size` bytes with at most a quarter of it ++ /// left over, taken out for a dedicated image ([`place`]); the stale spares go first. ++ fn take_spare(&mut self, device: &ash::Device, pool: (Holds, u32), size: u64) -> Option { ++ self.age_spare(device, Instant::now()); ++ let (index, _) = (self.spare.iter().enumerate()) ++ .filter(|(_, spare)| { ++ spare.pool == pool ++ && spare.block.size >= size ++ && spare.block.size - size <= spare.block.size / 4 ++ }) ++ .min_by_key(|(_, spare)| spare.block.size)?; ++ Some(self.spare.swap_remove(index).block) ++ } ++ ++ /// Free every spare block and every kept empty pooled block: the system reports memory ++ /// pressure, the signal Metal's resource cache gives memory back on. How many went. ++ pub(super) fn release_spare(&mut self, device: &ash::Device) -> usize { ++ let mut blocks: Vec = self.spare.drain(..).map(|spare| spare.block).collect(); ++ for pool in &mut self.pools { ++ blocks.extend(pool.take_kept()); ++ } ++ let count = blocks.len(); ++ for block in blocks { ++ // SAFETY: nothing is placed in a spare or kept empty block, and no command reads it. ++ drop(unsafe { self.let_go(device, block, FreeCause::KeptForRoom, None) }); ++ } ++ count ++ } ++ + /// Over every pool: the block slots the room queries have walked and the free ranges they + /// have visited ([`Pool::room_work`]), then what one query of each pool would walk and visit + /// where it derived per call ([`Pool::room_per_call`]). +@@ -1741,9 +1830,10 @@ impl Memory { + /// and no resource is left. What waited with them, for the caller to let go of with no lock + /// held. + pub(super) fn destroy(&mut self, device: &ash::Device) -> Vec> { +- let blocks: Vec = (self.pools.iter_mut()) ++ let mut blocks: Vec = (self.pools.iter_mut()) + .flat_map(|pool| pool.drain()) + .collect(); ++ blocks.extend(self.spare.drain(..).map(|spare| spare.block)); + for block in blocks { + self.say_block("freed a block as the device is destroyed", &block); + self.freed_by[block.cause(FreeCause::Teardown) as usize] += 1; +@@ -2123,6 +2213,19 @@ fn place( + return Ok(placement); + } + } ++ // A dedicated image takes a spare block of its pool where one fits ([`SPARE_FOR`]); one ++ // placed apart takes memory no submission has used ([`Fit`]). ++ if dedicated && fit == Fit::Pooled && holds == Holds::Images && addressed.is_none() { ++ let mut memory = lock(memory); ++ if let Some(block) = memory.take_spare(device, pool, size) { ++ let bytes = block.size; ++ memory.say_block("took a spare dedicated block for an image", &block); ++ let index = memory.pool(pool.0, pool.1).add(bytes, true, block, size); ++ let placement = memory.placement(pool, index, 0, size, false); ++ memory.say_placement("placed", &placement, Some(class)); ++ return Ok(placement); ++ } ++ } + // The type's kept empty blocks go before a block of it fails to be made. + let made = match maker.make(device, type_index, block_size, addressed) { + Err(Error::OutOfMemory(_)) if lock(memory).release_kept(device, type_index) > 0 => { From 4250dde7c097cae3aadef524cf14e0e62282cb91 Mon Sep 17 00:00:00 2001 From: rpone Date: Sat, 31 Oct 2026 19:06:11 +0800 Subject: [PATCH 88/92] feat(trace): GPU begin and end of each render pass frames.trace type 7 records carry the GPU's begin and end of the pass (Turnip's pass ring); a pass is read 300 ms after it was recorded so it has run. --- NativeBridgeMetalFrontend.mm | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 4b81edf..c39beef 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -185,7 +185,8 @@ static CommitRecord gCommitRing[32]; // type 3 the same for waitUntilScheduled; type 4 a thread's name {tid, 24 characters in t0..t2}; type 0 the file's first record {t0 nowUs() now, t1 wall clock microseconds, b process id, c wall clock // seconds, d format version 2}; type 7 a render pass from Turnip's ring (tu_pass_ring_read) {t0 recorded, t1 its sequence, a width | height << 16, b mode (0 GMEM, 1 SYSMEM // by the autotuner, 2 SYSMEM otherwise) | colour attachments << 8 | flags (1 mutable view, 2 linear, 4 unpadded, 8 loaded, 16 stored, 32 FDM) << 16 | the first colour -// attachment's view swap << 24, c draws | VkFormat << 16, d thousands of pixels}; type 8 a kernel submission from the Turnip kmd's ring (tu_darwin_submit_ring_read) {t0 when +// attachment's view swap << 24, c draws | VkFormat << 16, d thousands of pixels, t2/t3 when the GPU began and ended it in microseconds of its always-on counter (0: not +// run when read; read 300 ms after it was recorded)}; type 8 a kernel submission from the Turnip kmd's ring (tu_darwin_submit_ring_read) {t0 when // it began, t1 its sequence, a the kernel fence, b microseconds waiting for an unsubmitted sync, c microseconds in the kernel calls, d of those waiting for ring space, // cpu0 busy answers | chunks << 16 | indirect buffers << 24}. Times are nowUs(). struct TraceRec {uint32_t type,idx;uint64_t t0,t1,t2,t3;uint32_t a,b,c,d,tid,cpu0,cpu1,tid2;}; @@ -334,9 +335,9 @@ static void traceStartWriter() { int fd=-1; static const char *const paths[]={"/private/var/mobile/Library/Caches/com.apple.backboardd/m2v/frames.trace","/private/var/root/Library/Caches/com.apple.backboardd/m2v/frames.trace"}; uint32_t written=0,sampled=0; - struct PassRec {uint64_t ns;uint32_t seq;uint16_t width,height;uint8_t mode,colors,flags,swap;uint16_t draws,format;uint32_t kpx;}; + struct PassRec {uint64_t ns;uint32_t seq;uint16_t width,height;uint8_t mode,colors,flags,swap;uint16_t draws,format;uint32_t kpx;uint64_t gpuBegin,gpuEnd;}; struct SubRec {uint64_t ns;uint32_t seq,fence,syncWaitUs,callUs,spaceWaitUs;uint16_t busy;uint8_t chunks,buffers;}; - static_assert(sizeof(PassRec)==32 && sizeof(SubRec)==32,"turnip ring records"); + static_assert(sizeof(PassRec)==48 && sizeof(SubRec)==32,"turnip ring records"); uint32_t passFrom=0,subFrom=0; void *turnip=nullptr; uint32_t (*passRead)(uint32_t,PassRec *,uint32_t,uint32_t *)=nullptr; @@ -367,13 +368,17 @@ static void traceStartWriter() { PassRec recs[64];uint32_t newest=0; for(;;) { const uint32_t n=passRead(passFrom,recs,64,&newest); - for(uint32_t i=0;i Date: Sat, 31 Oct 2026 19:43:27 +0800 Subject: [PATCH 89/92] feat(trace): command buffer stages from commit to completion Bridge patch 0022 records each command buffer's stages (commit, lowering, recording, scheduler holds, submission, completion waiter) and the scheduler thread's sleeps and wakes; frames.trace carries them as type 12 records. --- NativeBridgeMetalFrontend.mm | 19 +- bridge/NativeMetalObjects.h | 3 + bridge/NativeMetalObjects.rs | 15 + .../0022-command-buffer-stage-trace.patch | 447 ++++++++++++++++++ 4 files changed, 483 insertions(+), 1 deletion(-) create mode 100644 bridge/patches/0022-command-buffer-stage-trace.patch diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index c39beef..a2431f2 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -188,7 +188,8 @@ static CommitRecord gCommitRing[32]; // attachment's view swap << 24, c draws | VkFormat << 16, d thousands of pixels, t2/t3 when the GPU began and ended it in microseconds of its always-on counter (0: not // run when read; read 300 ms after it was recorded)}; type 8 a kernel submission from the Turnip kmd's ring (tu_darwin_submit_ring_read) {t0 when // it began, t1 its sequence, a the kernel fence, b microseconds waiting for an unsubmitted sync, c microseconds in the kernel calls, d of those waiting for ring space, -// cpu0 busy answers | chunks << 16 | indirect buffers << 24}. Times are nowUs(). +// cpu0 busy answers | chunks << 16 | indirect buffers << 24}; type 12 a runtime command buffer stage (nmb_stage_ring_read, bridge patch 0022) {t0 when (us of the +// uptime clock), t1 the command buffer's id (its shared state's address; 0 the scheduler thread), a stage, b arg, c sequence}. Times are nowUs(). struct TraceRec {uint32_t type,idx;uint64_t t0,t1,t2,t3;uint32_t a,b,c,d,tid,cpu0,cpu1,tid2;}; static_assert(sizeof(TraceRec)==72,"trace record"); static TraceRec gTrace[16384]; @@ -394,6 +395,22 @@ static void traceStartWriter() { if(n<64)break; } } + { + struct StageRec {uint64_t ns,id;uint32_t seq;uint16_t stage,arg;}; + static_assert(sizeof(StageRec)==24,"stage record"); + static uint64_t stageFrom=0; + StageRec recs[256]; + for(;;) { + uint64_t next=stageFrom; + const size_t n=nmb_stage_ring_read(stageFrom,recs,256,&next); + for(size_t i=0;i usize { + if out.is_null() || next.is_null() { + return 0; + } + // SAFETY: the caller's `max` records and its word. + let records = unsafe { std::slice::from_raw_parts_mut(out, max) }; + let (count, following) = metal2vulkan_runtime::stage_trace::read(from, records); + unsafe { *next = following }; + count +} + /// Free the runtime's spare dedicated blocks and kept empty pooled blocks (bridge patch 0021), as Metal gives its resource cache's /// memory back when the system reports memory pressure: how many blocks went. #[unsafe(no_mangle)] diff --git a/bridge/patches/0022-command-buffer-stage-trace.patch b/bridge/patches/0022-command-buffer-stage-trace.patch new file mode 100644 index 0000000..1e66873 --- /dev/null +++ b/bridge/patches/0022-command-buffer-stage-trace.patch @@ -0,0 +1,447 @@ +diff --git a/crates/metal2vulkan-runtime/src/command_buffer/completion.rs b/crates/metal2vulkan-runtime/src/command_buffer/completion.rs +index 87190761..4edc64e5 100644 +--- a/crates/metal2vulkan-runtime/src/command_buffer/completion.rs ++++ b/crates/metal2vulkan-runtime/src/command_buffer/completion.rs +@@ -438,6 +438,11 @@ fn finish(watch: &Watch, lists: &mut FenceLists, mut finished: Vec<(usize, Submi + } = batch; + lists.gathered.extend(fence.map(|fence| (queue, fence))); + if last { ++ crate::stage_trace::mark( ++ Arc::as_ptr(&*buffer) as usize, ++ crate::stage_trace::Stage::Seen, ++ queue.min(255) as u32, ++ ); + let mut state = lock(&buffer.state); + state.times.completed = seen; + state.keep_holds(holds); +@@ -670,6 +675,8 @@ struct Front { + value: u64, + /// The fence its submission signals, where it has one. + fence: Option, ++ /// Its command buffer, for the stage trace (`stage_trace.rs`). ++ id: usize, + } + + /// `VkQueue` `queue`'s waiter (the module docs): it waits for its queue's first pending batch +@@ -705,6 +712,7 @@ fn wait_for_queue(watch: &Watch, queue: usize) { + let front = Front { + value: batch.value, + fence: batch.fence, ++ id: Arc::as_ptr(&*batch.buffer) as usize, + }; + Some((front, batch.buffer.device().clone())) + } +@@ -726,11 +734,18 @@ fn wait_for_queue(watch: &Watch, queue: usize) { + continue; + }; + device.shared().counts.completion_armed(slept); ++ crate::stage_trace::mark( ++ front.id, ++ crate::stage_trace::Stage::PollArmed, ++ (queue.min(255) as u32) | (u32::from(slept) << 8), ++ ); + // The wait is a section of the exit guard; once the process is exiting the waiters end, + // and what is pending stays pending, as Metal's does. + let polled = match exit_guard::enter() { + Some(busy) => { ++ let id = front.id; + let polled = poll(&device, queue, front); ++ crate::stage_trace::mark(id, crate::stage_trace::Stage::PollDone, queue.min(255) as u32); + drop(busy); + Some(polled) + } +diff --git a/crates/metal2vulkan-runtime/src/command_buffer/mod.rs b/crates/metal2vulkan-runtime/src/command_buffer/mod.rs +index 409a9db6..37fa6fcf 100644 +--- a/crates/metal2vulkan-runtime/src/command_buffer/mod.rs ++++ b/crates/metal2vulkan-runtime/src/command_buffer/mod.rs +@@ -2436,9 +2436,12 @@ impl CommandBuffer { + /// [`commit`](Self::commit) for `call`, or, with `failure`, + /// [`commit_failed`](Self::commit_failed). + fn commit_as(&self, call: &'static str, failure: Option) -> Result<(), Error> { ++ let id = Arc::as_ptr(&self.0) as usize; ++ crate::stage_trace::mark(id, crate::stage_trace::Stage::CommitCall, 0); + // Left to wait for the last render encoder's end, where its call left it to the device's + // lowering thread and the thread has not lowered it, or for the commits left so. + if failure.is_none() && self.queue_commit(call)? { ++ crate::stage_trace::mark(id, crate::stage_trace::Stage::CommitQueued, 0); + return Ok(()); + } + // The last render encoder's end, where its call left it to the device's lowering thread. +@@ -2527,6 +2530,12 @@ impl CommandBuffer { + failure: Option, + run: CommitRun, + ) -> Result<(), Error> { ++ let run_kind = match run { ++ CommitRun::AtCall => 0, ++ CommitRun::Queued => 1, ++ CommitRun::Refused => 2, ++ }; ++ crate::stage_trace::mark(Arc::as_ptr(&self.0) as usize, crate::stage_trace::Stage::CommitRun, run_kind); + // The waiting fetched render targets take their storage form at this commit where it + // allows (`storage_form.rs`): one atomic read where none waits. The scheduler's thread + // ends the switch once it submitted the command buffer. None where nothing of it runs. +@@ -2739,6 +2748,11 @@ impl CommandBuffer { + return Err(error); + } + }; ++ crate::stage_trace::mark( ++ Arc::as_ptr(&self.0) as usize, ++ crate::stage_trace::Stage::Prepared, ++ prepared.len() as u32, ++ ); + // The first segment a host step records, which the device announces (a loop's check + // records nothing). + let announced = segments +@@ -4362,18 +4376,23 @@ impl CommandBufferShared { + result: Result<(), CommandBufferError>, + ) { + let (contexts, queue) = (buffer.device.shared().contexts.clone(), buffer.queue.id()); ++ let id = Arc::as_ptr(&*buffer) as usize; + let item = Item::Transition(Transition::finished(buffer, result)); + let item = match program_objects::Unheld::enter() { + Some(unheld) => { + let ran = contexts.run_here(queue, item); + drop(unheld); + match ran { +- Ok(()) => return, ++ Ok(()) => { ++ crate::stage_trace::mark(id, crate::stage_trace::Stage::Finished, 1); ++ return; ++ } + Err(item) => item, + } + } + None => item, + }; ++ crate::stage_trace::mark(id, crate::stage_trace::Stage::Finished, 2); + contexts.post(queue, item); + } + +diff --git a/crates/metal2vulkan-runtime/src/command_buffer/submit.rs b/crates/metal2vulkan-runtime/src/command_buffer/submit.rs +index 7b18eb69..e5e7a777 100644 +--- a/crates/metal2vulkan-runtime/src/command_buffer/submit.rs ++++ b/crates/metal2vulkan-runtime/src/command_buffer/submit.rs +@@ -343,6 +343,29 @@ impl CommandBufferShared { + last: bool, + event_waits: &[(vk::Semaphore, u64)], + scheduler: &mut scheduler::State, ++ ) -> Result { ++ let id = Arc::as_ptr(self) as usize; ++ let at = (index.min(0x7fff) as u32) | (u32::from(last) << 15); ++ crate::stage_trace::mark(id, crate::stage_trace::Stage::SubmitBegin, at); ++ let submitted = self.submit_batch_now(index, last, event_waits, scheduler); ++ let outcome = match &submitted { ++ Ok(Submitting::Submitted(_)) => 0, ++ Ok(Submitting::Grouped) => 1, ++ Ok(Submitting::Later) => 2, ++ Ok(Submitting::AfterGroup(_)) => 3, ++ Err(_) => 4, ++ }; ++ crate::stage_trace::mark(id, crate::stage_trace::Stage::SubmitEnd, outcome); ++ submitted ++ } ++ ++ /// [`submit_batch`](Self::submit_batch) itself. ++ fn submit_batch_now( ++ self: &Arc, ++ index: usize, ++ last: bool, ++ event_waits: &[(vk::Semaphore, u64)], ++ scheduler: &mut scheduler::State, + ) -> Result { + let device = &self.device; + if device.is_lost() { +diff --git a/crates/metal2vulkan-runtime/src/lib.rs b/crates/metal2vulkan-runtime/src/lib.rs +index 400f6882..c5b00b55 100644 +--- a/crates/metal2vulkan-runtime/src/lib.rs ++++ b/crates/metal2vulkan-runtime/src/lib.rs +@@ -24,6 +24,7 @@ + #![warn(missing_docs)] + + mod address; ++pub mod stage_trace; + mod address_map; + mod address_pool; + mod amplification; +diff --git a/crates/metal2vulkan-runtime/src/scheduler/mod.rs b/crates/metal2vulkan-runtime/src/scheduler/mod.rs +index d8a3b7b3..34485655 100644 +--- a/crates/metal2vulkan-runtime/src/scheduler/mod.rs ++++ b/crates/metal2vulkan-runtime/src/scheduler/mod.rs +@@ -388,6 +388,9 @@ impl SchedulerShared { + /// (`asleep`, read under the lock the mark was made under; one that sleeps later reads the + /// mark first), counted as `woken`; else the notify, a system call, is spared. + fn woken(&self, woken: Woken, asleep: Asleep) { ++ if !matches!(asleep, Asleep::No) { ++ crate::stage_trace::mark(0, crate::stage_trace::Stage::SchedulerWake, woken as u32); ++ } + match asleep { + Asleep::OnCondvar => { + self.wakings[woken as usize].fetch_add(1, Ordering::Relaxed); +@@ -537,6 +540,8 @@ struct Held { + /// The host nanoseconds at the first look of the scheduler's that found it held + /// ([`held_for`](Self::held_for)), until it goes ([`hold_ended`](Self::hold_ended)). + held_since: Option, ++ /// The reasons a look held it for that the stage trace recorded (`stage_trace.rs`), a bit each. ++ traced_reasons: u8, + } + + impl Held { +@@ -545,6 +550,15 @@ impl Held { + /// read from the host's clock while nothing waits. + fn held_for(&mut self, reason: HeldFor) { + (self.buffer.device().shared().counts).head_held_for(reason); ++ let bit = 1u8 << (reason as u8); ++ if self.traced_reasons & bit == 0 { ++ self.traced_reasons |= bit; ++ crate::stage_trace::mark( ++ Arc::as_ptr(&*self.buffer) as usize, ++ crate::stage_trace::Stage::Held, ++ (reason as u32 + 1) | ((self.index.min(255) as u32) << 8), ++ ); ++ } + if self.held_since.is_none() { + self.held_since = Some(gpu_time::host_nanos()); + } +@@ -685,6 +699,11 @@ impl Scheduler { + batches: Vec, + ) { + let shared = &*self.shared; ++ crate::stage_trace::mark( ++ Arc::as_ptr(buffer) as usize, ++ crate::stage_trace::Stage::Handed, ++ batches.len() as u32, ++ ); + let committed = Committed { + buffer: buffer.hold(), + queue, +@@ -966,15 +985,20 @@ fn run(shared: &SchedulerShared) { + }; + let slice = bound.map_or(slice, |left| left.min(slice)); + shared.sleeps.fetch_add(1, Ordering::Relaxed); ++ let slice_ms = slice.as_millis().min(0x7fff) as u32; + if let Some(nudge) = shared.nudge.get().filter(|_| shared.gpu_waits(&mut gpu)) { ++ crate::stage_trace::mark(0, crate::stage_trace::Stage::SchedulerSleep, slice_ms | 0x8000); + state = wait_for_gpu(shared, state, nudge, &mut gpu, (slice, short)); ++ crate::stage_trace::mark(0, crate::stage_trace::Stage::SchedulerResumed, 0); + continue; + } + state.asleep = true; ++ crate::stage_trace::mark(0, crate::stage_trace::Stage::SchedulerSleep, slice_ms); + let (woke, waited) = shared + .wake + .wait_timeout(state, slice) + .unwrap_or_else(std::sync::PoisonError::into_inner); ++ crate::stage_trace::mark(0, crate::stage_trace::Stage::SchedulerResumed, u32::from(waited.timed_out())); + state = woke; + state.asleep = false; + if waited.timed_out() && !short { +@@ -1072,7 +1096,10 @@ fn run_work(kind: Kind, work: Work) -> bool { + let (buffer, index) = &work; + match kind { + Kind::Step => { ++ let id = Arc::as_ptr(&**buffer) as usize; ++ crate::stage_trace::mark(id, crate::stage_trace::Stage::StepBegin, *index as u32); + buffer.host_step(*index); ++ crate::stage_trace::mark(id, crate::stage_trace::Stage::StepEnd, *index as u32); + for &call in exit_guard::HOST_STEP { + exit_guard::called(call); + } +@@ -1098,7 +1125,11 @@ fn run_work(kind: Kind, work: Work) -> bool { + fn round(shared: &SchedulerShared, left: &mut Left, passed: &mut Passed) -> bool { + (shared.inbox).pass(|state, commits| { + shared.passes.fetch_add(1, Ordering::Relaxed); ++ crate::stage_trace::mark(0, crate::stage_trace::Stage::PassBegin, 0); ++ let before = passed.spent.len(); + pass(state, commits, (&mut *passed, &mut *left), &shared.awaits); ++ let spent = passed.spent.len().saturating_sub(before); ++ crate::stage_trace::mark(0, crate::stage_trace::Stage::PassEnd, spent.min(0xffff) as u32); + !passed.exiting && left.is_empty() && !passed.more + }); + passed.more = false; +@@ -1137,6 +1168,11 @@ fn take_in(state: &mut State, commits: &mut Vec, spent: &mut Spent) { + } in commits.drain(..) + { + let count = batches.len(); ++ crate::stage_trace::mark( ++ Arc::as_ptr(&*buffer) as usize, ++ crate::stage_trace::Stage::Taken, ++ count as u32, ++ ); + let State { order, binary, .. } = &mut *state; + let held = batches.into_iter().enumerate().map(|(index, batch)| Held { + buffer: buffer.hold(), +@@ -1151,6 +1187,7 @@ fn take_in(state: &mut State, commits: &mut Vec, spent: &mut Spent) { + hastened: false, + counted_held: false, + held_since: None, ++ traced_reasons: 0, + }); + order.push(queue, held); + spent.push(buffer); +diff --git a/crates/metal2vulkan-runtime/src/stage_trace.rs b/crates/metal2vulkan-runtime/src/stage_trace.rs +new file mode 100644 +index 00000000..d401a6c8 +--- /dev/null ++++ b/crates/metal2vulkan-runtime/src/stage_trace.rs +@@ -0,0 +1,161 @@ ++//! A process-wide ring of the stages each command buffer passes from its commit to its completion, ++//! for a host that asks where the time between them goes ([`read`]): when it was committed, left ++//! to the lowering thread, run, recorded, handed to the scheduler and taken in, what held its ++//! batches, when each was submitted, and when the completion waiter armed, saw it and finished it. ++//! The scheduler's and the waiters' own sleeps and wakes are recorded under id 0 and the queue. ++//! ++//! - **Cheap.** A record is one atomic add and three stores; nothing allocates or locks. The ring ++//! keeps the newest [`RING`] records; a reader that falls behind loses the oldest. ++//! - **One clock.** The host's uptime clock in nanoseconds (`CLOCK_UPTIME_RAW` on Apple ++//! platforms), the one the frontend's trace and the driver's rings use. ++//! - **Ids.** A command buffer's id is the address of its shared state, alive from its commit to ++//! its completion: records of one id between a commit stage and its completion are one command ++//! buffer's. ++ ++use std::sync::atomic::{AtomicU64, Ordering}; ++ ++/// The records the ring keeps. ++const RING: usize = 1 << 16; ++ ++/// What happened, the record's `stage`. ++#[derive(Clone, Copy, Debug, PartialEq, Eq)] ++#[repr(u16)] ++pub enum Stage { ++ /// `commit` was called (`arg`: 0). ++ CommitCall = 1, ++ /// The commit was left to wait for the lowering thread's end of its last render encoder. ++ CommitQueued = 2, ++ /// The commit's own work began (`arg`: 0 at the call, 1 queued, 2 refused). ++ CommitRun = 3, ++ /// Its segments were recorded or prepared for their host steps (`arg`: segments). ++ Prepared = 4, ++ /// Its batches were handed to the scheduler (`arg`: batches). ++ Handed = 5, ++ /// The scheduler took its batches into its order (`arg`: batches). ++ Taken = 6, ++ /// A look of the scheduler's held one of its batches (`arg`: the reason, `HeldFor` + 1, | ++ /// batch index << 8); recorded once per reason and batch. ++ Held = 7, ++ /// A batch's submission began (`arg`: batch index | last << 15). ++ SubmitBegin = 8, ++ /// A batch's submission returned (`arg`: 0 submitted, 1 grouped, 2 later, 3 after the group, ++ /// 4 failed). ++ SubmitEnd = 9, ++ /// Its host step began recording on the scheduler's thread (`arg`: batch index). ++ StepBegin = 10, ++ /// Its host step ended (`arg`: batch index). ++ StepEnd = 11, ++ /// A completion waiter began waiting for its batch (`arg`: queue | slept first << 8). ++ PollArmed = 12, ++ /// The waiter's wait for its batch returned (`arg`: queue). ++ PollDone = 13, ++ /// A completion turn saw it complete (`arg`: queue). ++ Seen = 14, ++ /// Its completion ran (`arg`: 1 on the waiter, 2 posted to its queue's context). ++ Finished = 15, ++ /// The scheduler's thread went to sleep (id 0, `arg`: slice in ms, | 0x8000 on the GPU). ++ SchedulerSleep = 20, ++ /// Something woke the scheduler's thread (id 0, `arg`: 0 commit, 1 completion, 2 host). ++ SchedulerWake = 21, ++ /// The scheduler's thread resumed after its sleep (id 0, `arg`: 1 timed out). ++ SchedulerResumed = 22, ++ /// The scheduler's thread began a pass (id 0). ++ PassBegin = 23, ++ /// The scheduler's thread ended a pass (id 0, `arg`: batches submitted, capped). ++ PassEnd = 24, ++} ++ ++struct Slot { ++ when: AtomicU64, ++ id: AtomicU64, ++ /// stage | arg << 16 | sequence << 32: a reader takes the slot only where the sequence is the ++ /// one it asked for. ++ word: AtomicU64, ++} ++ ++#[allow(clippy::declare_interior_mutable_const)] ++const EMPTY: Slot = Slot { ++ when: AtomicU64::new(0), ++ id: AtomicU64::new(0), ++ word: AtomicU64::new(0), ++}; ++static SLOTS: [Slot; RING] = [EMPTY; RING]; ++static NEXT: AtomicU64 = AtomicU64::new(1); ++ ++/// The host's uptime clock in nanoseconds. ++pub fn now_nanos() -> u64 { ++ #[cfg(any(target_os = "macos", target_os = "ios"))] ++ { ++ let mut spec = libc::timespec { ++ tv_sec: 0, ++ tv_nsec: 0, ++ }; ++ // SAFETY: a valid clock id and an owned timespec. ++ if unsafe { libc::clock_gettime(libc::CLOCK_UPTIME_RAW, &mut spec) } == 0 { ++ return spec.tv_sec as u64 * 1_000_000_000 + spec.tv_nsec as u64; ++ } ++ 0 ++ } ++ #[cfg(not(any(target_os = "macos", target_os = "ios")))] ++ { ++ crate::gpu_time::host_nanos() ++ } ++} ++ ++/// Record `stage` of `id` with `arg`, now. ++pub(crate) fn mark(id: usize, stage: Stage, arg: u32) { ++ let sequence = NEXT.fetch_add(1, Ordering::Relaxed); ++ let slot = &SLOTS[(sequence as usize) & (RING - 1)]; ++ slot.word.store(0, Ordering::Release); ++ slot.when.store(now_nanos(), Ordering::Relaxed); ++ slot.id.store(id as u64, Ordering::Relaxed); ++ let word = stage as u64 | ((arg as u64 & 0xffff) << 16) | ((sequence & 0xffff_ffff) << 32); ++ slot.word.store(word, Ordering::Release); ++} ++ ++/// One record as [`read`] gives it. ++#[derive(Clone, Copy, Debug, Default)] ++#[repr(C)] ++pub struct StageRecord { ++ /// Host uptime nanoseconds. ++ pub when: u64, ++ /// The command buffer's id (0: the scheduler's thread). ++ pub id: u64, ++ /// Its place among all records. ++ pub sequence: u32, ++ /// [`Stage`] as its number. ++ pub stage: u16, ++ /// The stage's argument. ++ pub arg: u16, ++} ++ ++/// The records from sequence `from` on (the oldest kept where `from` fell out of the ring), at most ++/// `out.len()`, oldest first: how many, and the sequence to ask for next. ++pub fn read(from: u64, out: &mut [StageRecord]) -> (usize, u64) { ++ let newest = NEXT.load(Ordering::Acquire); ++ let mut sequence = from.max(1).max(newest.saturating_sub(RING as u64 - 1024)); ++ let mut written = 0; ++ while sequence < newest && written < out.len() { ++ let slot = &SLOTS[(sequence as usize) & (RING - 1)]; ++ let word = slot.word.load(Ordering::Acquire); ++ if word >> 32 == sequence & 0xffff_ffff && word & 0xffff != 0 { ++ let when = slot.when.load(Ordering::Relaxed); ++ let id = slot.id.load(Ordering::Relaxed); ++ if slot.word.load(Ordering::Acquire) == word { ++ out[written] = StageRecord { ++ when, ++ id, ++ sequence: sequence as u32, ++ stage: (word & 0xffff) as u16, ++ arg: ((word >> 16) & 0xffff) as u16, ++ }; ++ written += 1; ++ } ++ } else if word == 0 { ++ // Being written: read again at the next call. ++ break; ++ } ++ sequence += 1; ++ } ++ (written, sequence) ++} From fc5ca0b274bec1c26b3a1a1ee2b911da33e15bd2 Mon Sep 17 00:00:00 2001 From: rpone Date: Sun, 1 Nov 2026 00:20:19 +0800 Subject: [PATCH 90/92] 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. --- NativeAdrenoMetalPlugin.mm | 39 +++++++++++++++++++++++++++---- NativeBridgeMetalFrontend.mm | 45 ++++++++++++++++++++++++++++++++---- 2 files changed, 75 insertions(+), 9 deletions(-) diff --git a/NativeAdrenoMetalPlugin.mm b/NativeAdrenoMetalPlugin.mm index d7e08c0..5f0bde3 100644 --- a/NativeAdrenoMetalPlugin.mm +++ b/NativeAdrenoMetalPlugin.mm @@ -9,6 +9,7 @@ // own GPU plugin. #import #include +#include #include #if __has_feature(ptrauth_calls) #include @@ -211,6 +212,26 @@ static void clientFailure(unsigned port, const char *step, const char *detail) { } } } +// Each process's own render passes, as its Turnip recorded them (NativeBridgeMetalFrontend.mm): the slowest one's size, format and draw +// count and the pass total, on the client's text line every 10 s while the diagnostics are on. A client whose jobs each took 77 ms showed +// only that; which pass and at what size is what says whether the cost is the driver's. +extern "C" size_t nmb_slowest_pass(char *out, size_t size) __attribute__((weak)); +static void startPassReporter(unsigned port) { + if (!getenv("SHENG_DIAG") || !&nmb_slowest_pass) return; + static std::atomic started{false}; + if (started.exchange(true)) return; + Gpu *gpu = makeGpu(port); + if (!gpu) return; + std::thread([gpu]{ + pthread_setname_np("pass-reporter"); + for (;;) { + sleep(10); + char row[200]; + if (!nmb_slowest_pass(row, sizeof row) || !row[0]) continue; + streamText(*gpu, row); + } + }).detach(); +} static id clientDevice(unsigned port) { char turnip[1024]; if (!turnip::libraryPath(turnip, sizeof turnip) || access(turnip, R_OK) != 0) {clientFailure(port, "no-turnip", ""); return nil;} @@ -258,6 +279,7 @@ static id clientDevice(unsigned port) { // once-a-second reader never ran before the crash (i568). if (device) { gClientPort = port; + startPassReporter(port); native_bridge_metal_problem_hook = [](const char *problem) {clientFailure(gClientPort, "problem", problem);}; char problem[200] = {}; if (native_bridge_metal_first_problem(problem, sizeof problem) && problem[0]) clientFailure(port, "problem", problem); @@ -297,6 +319,10 @@ static id deviceForPort(unsigned port) { // command buffer was no better (17.8 ms mean and 56.1 ms at p95 in tile memory, 17.1 and 52.1 in system memory), and one home screen frame // still came out with red and blue exchanged. sheng-tu-debug= overrides this for experiments. setenv("TU_DEBUG", bootArgSet("sheng-fb-bgra=1") ? "sysmem,nocb" : "nocb", 0); + // sheng-diag=1 turns on every diagnostic of the GPU stack in this process: the frontend's frame trace, Turnip's pass stamps and + // submission ring, the runtime's stage ring, and in the render server the status and telemetry lines. Without it none of them runs. + if (bootArgSet("sheng-diag=1")) setenv("SHENG_DIAG", "1", 1); + else unsetenv("SHENG_DIAG"); if (strcmp(getprogname(), "backboardd") != 0) return device = clientDevice(port); // The discovery releases the accelerator port when it is done; the monitor and the scanout client keep using it. if (void *iokit = dlopen("/System/Library/Frameworks/IOKit.framework/IOKit", RTLD_NOW | RTLD_LOCAL)) { @@ -401,8 +427,9 @@ static id deviceForPort(unsigned port) { // runtime for it here (patches/metal2vulkan/0012): where the host offers priorities, its frames no longer wait on the GPU behind // an app's. setenv("METAL2VULKAN_QUEUE_PRIORITY", "high", 0); - // The runtime's creation log reports each VkPipeline making's time to the device's sink, which the telemetry's V row sums. - setenv("METAL2VULKAN_LOG_PIPELINES", "1", 0); + // The runtime's creation log reports each VkPipeline making's time to the device's sink, which the telemetry's V row sums + // (sheng-diag=1). + if (getenv("SHENG_DIAG")) setenv("METAL2VULKAN_LOG_PIPELINES", "1", 0); CFPreferencesSetAppValue(CFSTR("CAEnableFramebufferCompression"), kCFBooleanFalse, kCFPreferencesCurrentApplication); CFPreferencesSetAppValue(CFSTR("CAEnableIOSurfaceCompression"), kCFBooleanFalse, kCFPreferencesCurrentApplication); char candidates[5][512] = {}; @@ -451,13 +478,15 @@ static id deviceForPort(unsigned port) { return nil; } gpu->mark(0x6802); - // The status line (GPU8) every ten seconds, and the display hand-off client on its own thread: both only read or serve, neither is the discovery's. + // The status line (GPU8) every ten seconds (sheng-diag=1), and the display hand-off client on its own thread: both only read or serve, + // neither is the discovery's. pthread_t monitor, scanout; pthread_attr_t attributes; pthread_attr_init(&attributes); pthread_attr_setdetachstate(&attributes, PTHREAD_CREATE_DETACHED); pthread_attr_setstacksize(&attributes, 1u << 20); - pthread_create(&monitor, &attributes, [](void *arg) -> void * { + const bool diag = getenv("SHENG_DIAG") != nullptr; + if (diag) pthread_create(&monitor, &attributes, [](void *arg) -> void * { Gpu *g = static_cast(arg); for (;;) { sleep(10); @@ -483,7 +512,7 @@ static id deviceForPort(unsigned port) { return nullptr; }, gpu); pthread_t telemetry; - pthread_create(&telemetry, &attributes, [](void *arg) -> void * { + if (diag) pthread_create(&telemetry, &attributes, [](void *arg) -> void * { Gpu *g = static_cast(arg); sleep(5); // The bridge's telemetry table, resent whole every four seconds as markers (two characters each): 0xC000|row starts a row of the diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index a2431f2..7f3a99e 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -195,13 +195,27 @@ static_assert(sizeof(TraceRec)==72,"trace record"); static TraceRec gTrace[16384]; static std::atomic gTraceHead{0}; static std::atomic gTraceStarted{false}; +/* The slowest render pass this process's Turnip recorded, with the size, format and draw count that identify it, and all of them summed: + * the line the frontend streams to the kernel log. A process whose jobs each took 77 ms showed only that; which pass, at what size and in + * which mode, is what says whether the cost is ours. Written by the trace writer alone, read by the streaming thread. */ +struct SlowPass { + uint64_t us=0,totalUs=0,whenUs=0; + uint64_t passes=0; + uint32_t width=0,height=0,mode=0,format=0,flags=0,swap=0,draws=0,kpx=0; +}; +static SlowPass gSlowPass; +// Every diagnostic of this process (the frame trace, the sampler, Turnip's pass stamps and submission ring, the runtime's stage ring) runs only +// with SHENG_DIAG set, which the plugin sets from the boot argument sheng-diag=1 before the device opens (NativeAdrenoMetalPlugin.mm). +static bool diagOn() {static const bool on=getenv("SHENG_DIAG")!=nullptr;return on;} static uint32_t threadCpuUs() { thread_basic_info_data_t info;mach_msg_type_number_t count=THREAD_BASIC_INFO_COUNT; if(thread_info(pthread_mach_thread_np(pthread_self()),THREAD_BASIC_INFO,(thread_info_t)&info,&count)!=KERN_SUCCESS)return 0; return (uint32_t)((uint64_t)(info.user_time.seconds+info.system_time.seconds)*1000000ull+(uint64_t)(info.user_time.microseconds+info.system_time.microseconds)); } static uint32_t threadIdLow() {uint64_t id=0;pthread_threadid_np(NULL,&id);return (uint32_t)id;} +static bool traceWanted() {static const bool on=diagOn() && !strcmp(getprogname(),"backboardd");return on;} static void tracePush(const TraceRec &rec) { + if(!traceWanted())return; const uint32_t at=gTraceHead.fetch_add(1); gTrace[at%16384]=rec; gTrace[at%16384].idx=rec.type==1?rec.idx:at; @@ -222,16 +236,17 @@ static void traceNote(uint32_t tid) { // A thread suspends the render thread every 2 ms and keeps the program counter plus the frame-pointer chain in a ring (no allocation, no locks: the // suspended thread may hold any). The writer thread resolves the addresses with dladdr and puts the samples into the frame trace (types 9 and 10, // below); the leaf and inclusive histograms inside windows and in the gaps between windows (the time the render thread spends on something other -// than encoding) are built offline. sheng-gpu-sample=0 on the kernel command line turns the sampler off. +// than encoding) are built offline. Suspending the render thread every 2 ms delays its frames (half of the render server's busiest-thread samples +// in untagged-3f2bd680 were this thread), so it runs only with sheng-diag=1 and sheng-gpu-sample=1 both on the kernel command line. static std::atomic gSampleThread{0}; static std::atomic gSampleTop{0}; struct StackSample {uint64_t frames[8];uint64_t us;uint32_t gap;}; static std::atomic gSampleInWindow{false}; static bool samplerWanted() { static const bool wanted=[]{ - char args[1024]={};size_t size=sizeof args; - if(sysctlbyname("kern.bootargs",args,&size,NULL,0))return true; - return !strstr(args,"sheng-gpu-sample=0"); + if(!diagOn())return false; + char args[1100]={};size_t size=sizeof args-1; + return !sysctlbyname("kern.bootargs",args,&size,NULL,0) && strstr(args,"sheng-gpu-sample=1"); }(); return wanted; } @@ -328,7 +343,21 @@ static void sampleEmitTrace(uint32_t &consumed) { tracePush(rec); } } +/* The process's slowest pass and its pass total, for a line of the kernel log: "R pass us x mode fmt draws flags + * swap | passes us". Zero characters before any pass with GPU times was recorded. */ +extern "C" size_t nmb_slowest_pass(char *out,size_t size) { + if(!out || !size)return 0; + const SlowPass p=gSlowPass; + if(!p.passes) {out[0]=0;return 0;} + const int n=snprintf(out,size,"R pass %llu us %ux%u mode %u fmt %u draws %u flags %x swap %u | %llu passes %llu us", + (unsigned long long)p.us,p.width,p.height,p.mode,p.format,p.draws,p.flags,p.swap, + (unsigned long long)p.passes,(unsigned long long)p.totalUs); + return n>0?(size_t)n:0; +} static void traceStartWriter() { + // The file is the render server's: SpringBoard, which runs as the same user, opened the same path with O_TRUNC and the two processes' + // records overwrote each other's at their own offsets (untagged-3f2bd680: SpringBoard's header, two interleaved stage sequences). + if(!diagOn() || strcmp(getprogname(),"backboardd"))return; bool expected=false; if(!gTraceStarted.compare_exchange_strong(expected,true))return; std::thread([]{ @@ -378,6 +407,14 @@ static void traceStartWriter() { rec.b=p.mode|(uint32_t)p.colors<<8|(uint32_t)p.flags<<16|(uint32_t)p.swap<<24;rec.c=p.draws|(uint32_t)p.format<<16;rec.d=p.kpx; rec.t2=p.gpuBegin/1000;rec.t3=p.gpuEnd/1000; tracePush(rec);passFrom=p.seq+1; + if(p.gpuEnd>p.gpuBegin) { + const uint64_t us=(p.gpuEnd-p.gpuBegin)/1000; + if(us>gSlowPass.us) { + gSlowPass.us=us;gSlowPass.width=p.width;gSlowPass.height=p.height;gSlowPass.mode=p.mode;gSlowPass.format=p.format; + gSlowPass.draws=p.draws;gSlowPass.kpx=p.kpx;gSlowPass.flags=p.flags;gSlowPass.swap=p.swap;gSlowPass.whenUs=p.ns/1000; + } + ++gSlowPass.passes;gSlowPass.totalUs+=us; + } } if(n<64 || taken Date: Sun, 1 Nov 2026 05:14:01 +0800 Subject: [PATCH 91/92] feat(diag): notes for passes and pipelines, and a GPU log switch Every process with the diagnostics on reads its own Turnip pass ring, not only the render server. A process without a trace file sends its passes of 3 ms or more, and the pipelines it made or was refused, to the kernel's log through method 18; the render server keeps the trace file alone. The pass record carries the heaviest fragment shader's instructions, texture fetches, stalls, waves, GPRs, samples, depth/stencil, bandwidth and vertices, and its hash, which joins a pass to the pipeline that made it. setGPUPriority: and setBackgroundGPUPriority: reach the driver for the whole process, and the process's text row counts what was asked and what was sent. The GPU stack's NSLog output and the pipeline journal follow log-gpu=1 (SHENG_LOG_GPU); the problem notes behind the status rows stay on. --- NativeBridgeMetalFrontend.mm | 186 +++++++++++++++++++++++++++++------ 1 file changed, 158 insertions(+), 28 deletions(-) diff --git a/NativeBridgeMetalFrontend.mm b/NativeBridgeMetalFrontend.mm index 7f3a99e..38c663a 100644 --- a/NativeBridgeMetalFrontend.mm +++ b/NativeBridgeMetalFrontend.mm @@ -202,8 +202,16 @@ struct SlowPass { uint64_t us=0,totalUs=0,whenUs=0; uint64_t passes=0; uint32_t width=0,height=0,mode=0,format=0,flags=0,swap=0,draws=0,kpx=0; + uint64_t over20=0,recentUs=0; // passes of 20 ms or more, and the slowest since the stream last read it + // The slowest pass's shader and geometry figures (Turnip's pass record): heaviest fragment shader's ir3 instructions and texture fetches, + // its estimated stalls on (sy) and (ss), waves per core and highest GPR, samples, depth/stencil, bandwidth per sample, vertices. + uint32_t fsInstrs=0,fsTex=0,fsSy=0,fsSs=0,samples=0,ds=0,bandwidth=0,verts=0; + int32_t fsWaves=0,fsRegs=0; }; static SlowPass gSlowPass; +// What this process's queues asked for with setGPUPriority:/setBackgroundGPUPriority: and how many of those reached the driver. +static std::atomic gAskFg{0xffffffffu},gAskBg{0xffffffffu},gAskCount{0},gAskSent{0}; +static void nbForwardPriority(uint32_t foreground,uint32_t background); // Every diagnostic of this process (the frame trace, the sampler, Turnip's pass stamps and submission ring, the runtime's stage ring) runs only // with SHENG_DIAG set, which the plugin sets from the boot argument sheng-diag=1 before the device opens (NativeAdrenoMetalPlugin.mm). static bool diagOn() {static const bool on=getenv("SHENG_DIAG")!=nullptr;return on;} @@ -348,16 +356,81 @@ static void sampleEmitTrace(uint32_t &consumed) { extern "C" size_t nmb_slowest_pass(char *out,size_t size) { if(!out || !size)return 0; const SlowPass p=gSlowPass; - if(!p.passes) {out[0]=0;return 0;} - const int n=snprintf(out,size,"R pass %llu us %ux%u mode %u fmt %u draws %u flags %x swap %u | %llu passes %llu us", - (unsigned long long)p.us,p.width,p.height,p.mode,p.format,p.draws,p.flags,p.swap, - (unsigned long long)p.passes,(unsigned long long)p.totalUs); + if(!p.passes && !gAskCount.load()) {out[0]=0;return 0;} + // One 63-character row per process (the kernel keeps that much of a client's text), three kinds in turn at the 5 s tick: the timing of its + // passes, the shader and geometry figures of the slowest one, and what its queues asked the GPU priority to be. + static unsigned turn=0; + const unsigned kind=turn++%3; + int n; + if(kind==0) { + gSlowPass.recentUs=0; + n=snprintf(out,size,"slow %lluus %ux%u d%u m%u|n%llu >20ms %llu now %llu", + (unsigned long long)p.us,p.width,p.height,p.draws,p.mode, + (unsigned long long)p.passes,(unsigned long long)p.over20,(unsigned long long)p.recentUs); + } else if(kind==1) { + n=snprintf(out,size,"fs i%u t%u sy%u ss%u w%d r%d|smp%u ds%u bw%u v%u", + p.fsInstrs,p.fsTex,p.fsSy,p.fsSs,p.fsWaves,p.fsRegs,p.samples,p.ds,p.bandwidth,p.verts); + } else { + n=snprintf(out,size,"ask fg%d bg%d n%u sent%u", + gAskFg.load()==0xffffffffu?-1:(int)gAskFg.load(),gAskBg.load()==0xffffffffu?-1:(int)gAskBg.load(),gAskCount.load(),gAskSent.load()); + } return n>0?(size_t)n:0; } +// The runtime loads Turnip after this process may have started; its handle is local to it, so the exported setter is found by the path. +static void nbForwardPriority(uint32_t foreground,uint32_t background) { + typedef bool (*SetPriority)(uint32_t,uint32_t); + static std::atomic setter{nullptr}; + SetPriority fn=setter.load(); + if(!fn) { + if(gLoaderPath.empty())return; + void *turnip=dlopen(gLoaderPath.c_str(),RTLD_NOW|RTLD_NOLOAD); + if(!turnip)return; + fn=(SetPriority)dlsym(turnip,"tu_darwin_set_priority"); + if(!fn)return; + setter.store(fn); + } + gAskCount.fetch_add(1); + if(fn(foreground,background))gAskSent.fetch_add(1); +} +// A note for the kernel's log (Turnip's tu_darwin_note, NativeAdrenoMethodNote): the board's export writes it as a row of kernel.rings.txt, the one +// place a process that has no file of its own (a sandboxed extension) can leave more than its 63-character text row. Kind 1 is a slow render pass +// (row G), kind 2 a pipeline made or refused (row L). Only with the diagnostics on. +static bool nbNote(uint32_t kind,const uint32_t *words,uint32_t count) { + typedef bool (*Note)(uint32_t,const uint32_t *,uint32_t); + static std::atomic note{nullptr}; + Note fn=note.load(); + if(!fn) { + if(gLoaderPath.empty())return false; + void *turnip=dlopen(gLoaderPath.c_str(),RTLD_NOW|RTLD_NOLOAD); + if(!turnip)return false; + fn=(Note)dlsym(turnip,"tu_darwin_note"); + if(!fn)return false; + note.store(fn); + } + return fn(kind,words,count); +} +// The shader hashes of the pipeline this thread just asked Turnip for (tu_darwin_last_program reads and clears them). +static void nbLastProgram(uint32_t *vs,uint32_t *fs) { + typedef void (*Last)(uint32_t *,uint32_t *); + static std::atomic last{nullptr}; + *vs=*fs=0; + Last fn=last.load(); + if(!fn) { + if(gLoaderPath.empty())return; + void *turnip=dlopen(gLoaderPath.c_str(),RTLD_NOW|RTLD_NOLOAD); + if(!turnip)return; + fn=(Last)dlsym(turnip,"tu_darwin_last_program"); + if(!fn)return; + last.store(fn); + } + fn(vs,fs); +} static void traceStartWriter() { // The file is the render server's: SpringBoard, which runs as the same user, opened the same path with O_TRUNC and the two processes' // records overwrote each other's at their own offsets (untagged-3f2bd680: SpringBoard's header, two interleaved stage sequences). - if(!diagOn() || strcmp(getprogname(),"backboardd"))return; + // Every process with the diagnostics on reads its own Turnip's pass ring (the slowest-pass row); only the render server writes the file. + if(!diagOn())return; + static const bool render=!strcmp(getprogname(),"backboardd"); bool expected=false; if(!gTraceStarted.compare_exchange_strong(expected,true))return; std::thread([]{ @@ -365,9 +438,9 @@ static void traceStartWriter() { int fd=-1; static const char *const paths[]={"/private/var/mobile/Library/Caches/com.apple.backboardd/m2v/frames.trace","/private/var/root/Library/Caches/com.apple.backboardd/m2v/frames.trace"}; uint32_t written=0,sampled=0; - struct PassRec {uint64_t ns;uint32_t seq;uint16_t width,height;uint8_t mode,colors,flags,swap;uint16_t draws,format;uint32_t kpx;uint64_t gpuBegin,gpuEnd;}; + struct PassRec {uint64_t ns;uint32_t seq;uint16_t width,height;uint8_t mode,colors,flags,swap;uint16_t draws,format;uint32_t kpx;uint64_t gpuBegin,gpuEnd;uint16_t fsInstrs,fsTex,fsSy,fsSs;int8_t fsWaves,fsRegs;uint8_t samples,ds;uint32_t bandwidth,verts,fsHash;}; struct SubRec {uint64_t ns;uint32_t seq,fence,syncWaitUs,callUs,spaceWaitUs;uint16_t busy;uint8_t chunks,buffers;}; - static_assert(sizeof(PassRec)==48 && sizeof(SubRec)==32,"turnip ring records"); + static_assert(sizeof(PassRec)==72 && sizeof(SubRec)==32,"turnip ring records"); uint32_t passFrom=0,subFrom=0; void *turnip=nullptr; uint32_t (*passRead)(uint32_t,PassRec *,uint32_t,uint32_t *)=nullptr; @@ -377,9 +450,9 @@ static void traceStartWriter() { bool header=false; for(;;) { usleep(250000); - sampleEmitTrace(sampled); - if(fd<0)for(const char *path:paths){fd=open(path,O_WRONLY|O_CREAT|O_TRUNC,0644);if(fd>=0)break;} - if(fd<0)continue; + if(render)sampleEmitTrace(sampled); + if(render && fd<0)for(const char *path:paths){fd=open(path,O_WRONLY|O_CREAT|O_TRUNC,0644);if(fd>=0)break;} + if(render && fd<0)continue; if(!turnip && !gLoaderPath.empty()) { // the runtime loads Turnip after this thread may have started turnip=dlopen(gLoaderPath.c_str(),RTLD_NOW|RTLD_NOLOAD); if(turnip) { @@ -388,7 +461,7 @@ static void traceStartWriter() { copyStats=(void (*)(uint64_t *))dlsym(turnip,"tu_copy_row_stats"); } } - if(!header) { + if(render && !header) { header=true; struct timeval wall;gettimeofday(&wall,nullptr); TraceRec rec={};rec.type=0;rec.t0=nowUs();rec.t1=(uint64_t)wall.tv_sec*1000000ull+(uint64_t)wall.tv_usec;rec.b=(uint32_t)getpid();rec.c=(uint32_t)wall.tv_sec;rec.d=2;tracePush(rec); @@ -406,12 +479,36 @@ static void traceStartWriter() { TraceRec rec={};rec.type=7;rec.t0=p.ns/1000;rec.t1=p.seq;rec.a=p.width|(uint32_t)p.height<<16; rec.b=p.mode|(uint32_t)p.colors<<8|(uint32_t)p.flags<<16|(uint32_t)p.swap<<24;rec.c=p.draws|(uint32_t)p.format<<16;rec.d=p.kpx; rec.t2=p.gpuBegin/1000;rec.t3=p.gpuEnd/1000; - tracePush(rec);passFrom=p.seq+1; + if(render) { + tracePush(rec); + // type 13, the pass's shader and geometry figures (read with type 7 by seq): a = instructions | texture fetches << 16, b = (sy) | (ss) stalls << 16, + // c = waves | GPR << 8 | samples << 16 | depth/stencil << 24, d = bandwidth per sample, tid = vertices + TraceRec detail={};detail.type=13;detail.t0=p.ns/1000;detail.t1=p.seq; + detail.a=p.fsInstrs|(uint32_t)p.fsTex<<16;detail.b=p.fsSy|(uint32_t)p.fsSs<<16; + detail.c=(uint8_t)p.fsWaves|(uint32_t)(uint8_t)p.fsRegs<<8|(uint32_t)p.samples<<16|(uint32_t)p.ds<<24; + detail.d=p.bandwidth;detail.tid=p.verts; + detail.cpu0=p.fsHash; // the heaviest fragment shader's tu_hash32, joined to the pipeline log's names + tracePush(detail); + } + passFrom=p.seq+1; if(p.gpuEnd>p.gpuBegin) { const uint64_t us=(p.gpuEnd-p.gpuBegin)/1000; + if(!render && us>=3000) { + // Row G of the kernel log (this process has no trace file): the pass, its shader figures and its heaviest fragment shader's hash. + const uint64_t at=p.ns/1000,began=p.gpuBegin/1000; + const uint32_t words[16]={p.seq,(uint32_t)at,(uint32_t)(at>>32),(uint32_t)began,(uint32_t)(began>>32),(uint32_t)us, + (uint32_t)p.width|(uint32_t)p.height<<16,(uint32_t)p.mode|(uint32_t)p.colors<<8|(uint32_t)p.flags<<16|(uint32_t)p.swap<<24, + (uint32_t)p.draws|(uint32_t)p.format<<16,p.kpx,(uint32_t)p.fsInstrs|(uint32_t)p.fsTex<<16,(uint32_t)p.fsSy|(uint32_t)p.fsSs<<16, + (uint8_t)p.fsWaves|(uint32_t)(uint8_t)p.fsRegs<<8|(uint32_t)p.samples<<16|(uint32_t)p.ds<<24,p.bandwidth,p.verts,p.fsHash}; + nbNote(1,words,16); + } + if(us>=20000)++gSlowPass.over20; + if(us>gSlowPass.recentUs)gSlowPass.recentUs=us; if(us>gSlowPass.us) { gSlowPass.us=us;gSlowPass.width=p.width;gSlowPass.height=p.height;gSlowPass.mode=p.mode;gSlowPass.format=p.format; gSlowPass.draws=p.draws;gSlowPass.kpx=p.kpx;gSlowPass.flags=p.flags;gSlowPass.swap=p.swap;gSlowPass.whenUs=p.ns/1000; + gSlowPass.fsInstrs=p.fsInstrs;gSlowPass.fsTex=p.fsTex;gSlowPass.fsSy=p.fsSy;gSlowPass.fsSs=p.fsSs;gSlowPass.fsWaves=p.fsWaves;gSlowPass.fsRegs=p.fsRegs; + gSlowPass.samples=p.samples;gSlowPass.ds=p.ds;gSlowPass.bandwidth=p.bandwidth;gSlowPass.verts=p.verts; } ++gSlowPass.passes;gSlowPass.totalUs+=us; } @@ -419,6 +516,7 @@ static void traceStartWriter() { if(n<64 || taken &request, library=dlopen(path.c_str(),RTLD_NOW); } } - if(library && path!=std::string(kFramework)+"/MTLCompiler")NSLog(@"native-metal: Metal compiler loaded from %s",path.c_str()); + if(library && path!=std::string(kFramework)+"/MTLCompiler")GLOG(@"native-metal: Metal compiler loaded from %s",path.c_str()); const size_t versions=path.find("/Versions/"); strlcpy(gCompilerLoaded,!library?"none":versions==std::string::npos?"top":path.substr(versions+10,path.rfind('/')-versions-10).c_str(), sizeof gCompilerLoaded); @@ -1161,7 +1278,7 @@ NB_RESOURCE_SPI const uint64_t region[6]={r.origin.x,r.origin.y,r.origin.z,r.size.width,r.size.height,r.size.depth}; const uint64_t length=(r.size.depth>1?image*(r.size.depth-1):0)+row*r.size.height; if(gLogTextures && textureLogTurn()) - NSLog(@"native-metal: texture-log %llu replaceRegion origin %lu,%lu size %lux%lu level %lu slice %lu rows %lu | texture %llux%llu fmt %llu " + GLOG(@"native-metal: texture-log %llu replaceRegion origin %lu,%lu size %lux%lu level %lu slice %lu rows %lu | texture %llux%llu fmt %llu " @"options %llx surface %u",(unsigned long long)gTexturesLogged.load(),(unsigned long)r.origin.x,(unsigned long)r.origin.y, (unsigned long)r.size.width,(unsigned long)r.size.height,(unsigned long)level,(unsigned long)slice,(unsigned long)row, (unsigned long long)_descriptor.width,(unsigned long long)_descriptor.height,(unsigned long long)_descriptor.format, @@ -1244,7 +1361,7 @@ static bool b3a8Planes(IOSurfaceRef surface,size_t allocation,size_t planes[4]) if(!w || !h || row0*h+row1*h>allocation)return false; planes[0]=0;planes[1]=row0;planes[2]=row0*h;planes[3]=row1; static std::atomic told{0}; - if(told.fetch_add(1)<8)NSLog(@"native-metal: b3a8 %lux%lu planes computed (row %lu at 0, row %lu at %lu; %lu of %lu bytes)",(unsigned long)w, + if(told.fetch_add(1)<8)GLOG(@"native-metal: b3a8 %lux%lu planes computed (row %lu at 0, row %lu at %lu; %lu of %lu bytes)",(unsigned long)w, (unsigned long)h,(unsigned long)row0,(unsigned long)row1,(unsigned long)(row0*h),(unsigned long)(row0*h+row1*h),(unsigned long)allocation); gSurfaceB3a8++; return true; @@ -2454,7 +2571,7 @@ static void writeDrawStats() { if(![self buffers:NB_OBJECTS(s) count:1] || ![self textures:NB_OBJECTS(d) count:1])return; if(gLogTextures && textureLogTurn()) { const NmbTextureDescriptor t=[(NBTexture *)d descriptor]; - NSLog(@"native-metal: texture-log %llu bufferToTexture size %lux%lu origin %lu,%lu level %lu slice %lu | rows %lu image %lu offset %lu " + GLOG(@"native-metal: texture-log %llu bufferToTexture size %lux%lu origin %lu,%lu level %lu slice %lu | rows %lu image %lu offset %lu " @"buffer %lu%s | texture %llux%llu fmt %llu options %llx",(unsigned long long)gTexturesLogged.load(),(unsigned long)z.width, (unsigned long)z.height,(unsigned long)dO.x,(unsigned long)dO.y,(unsigned long)level,(unsigned long)slice,(unsigned long)row, (unsigned long)image,(unsigned long)o,(unsigned long)[s length],[(NBBuffer *)s wrapped]?" no-copy":"", @@ -2897,7 +3014,7 @@ static char kUserDictionaryKey; if(!self.failedLocally && hashSource(source.bytes,source.length)!=source.hash) { const uint64_t raced=++gSourcesRaced[source.surface]; if(raced<=50 || raced%500==0) - NSLog(@"native-metal: source-race %s %ux%u, rows %u bytes: rewritten while the GPU could read it (%llu of %llu such sources)", + GLOG(@"native-metal: source-race %s %ux%u, rows %u bytes: rewritten while the GPU could read it (%llu of %llu such sources)", source.surface?"IOSurface":"no-copy buffer",source.width,source.height,source.row,(unsigned long long)raced, (unsigned long long)gSourcesWatched[source.surface].load()); } @@ -3056,16 +3173,21 @@ static void completed(void *context) { - (id)commandBufferWithUnretainedReferences {return [self commandBuffer];} - (void)setSubmissionQueue:(dispatch_queue_t)queue {(void)queue;} // submission happens on the caller's thread here // setCompletionQueue: is the property's setter. -// GPU priorities are not applied per queue (a process's queues share one hardware queue, whose priority is set when the device opens): -// reported as not applied. What a process asks for is logged once (the render server's queue is the one CoreAnimation raises). +// A process's queues share one hardware queue here, so what a queue asks for is given to the driver for the whole process (the most urgent +// request of any of its queues stands: tu_darwin_set_priority); the answer stays NO, as callers have always got it. What a process asks +// for is logged once (the render server's queue is the one CoreAnimation raises) and counted in the process's text row. - (BOOL)setGPUPriority:(NSUInteger)priority { static std::atomic told{false}; - if(!told.exchange(true))NBLOG(@"native-metal: setGPUPriority:%lu (not applied per queue)",(unsigned long)priority); - return NO; + if(!told.exchange(true))NBLOG(@"native-metal: setGPUPriority:%lu (given to the driver for the process)",(unsigned long)priority); + gAskFg.store((uint32_t)priority); + nbForwardPriority((uint32_t)priority,0xffffffffu); + return NO; // as before: the answer callers have always got } - (BOOL)setBackgroundGPUPriority:(NSUInteger)priority { static std::atomic told{false}; - if(!told.exchange(true))NBLOG(@"native-metal: setBackgroundGPUPriority:%lu (not applied per queue)",(unsigned long)priority); + if(!told.exchange(true))NBLOG(@"native-metal: setBackgroundGPUPriority:%lu (given to the driver for the process)",(unsigned long)priority); + gAskBg.store((uint32_t)priority); + nbForwardPriority(0xffffffffu,(uint32_t)priority); return NO; } // What a new Metal queue reports (MTLIOAccelCommandQueue: normal 1, background 3). RenderBox and Core Image read both before any frame. @@ -3615,7 +3737,7 @@ static char dvmMemoryReleaseKey; NSString *why=lastError(); uint64_t answer[2]={0,0}; (void)nmb_device_heap_texture_size_and_align(gDevice,&descriptor,answer); - NSLog(@"native-metal: heap texture refused: %@ | fmt %lu %lux%lu L%lu type %lu needs %#llx@%#llx heap %#lx used %#lx room %#lx",why, + GLOG(@"native-metal: heap texture refused: %@ | fmt %lu %lux%lu L%lu type %lu needs %#llx@%#llx heap %#lx used %#lx room %#lx",why, (unsigned long)d.pixelFormat,(unsigned long)d.width,(unsigned long)d.height,(unsigned long)d.mipmapLevelCount, (unsigned long)_heapType,(unsigned long long)answer[0],(unsigned long long)answer[1],(unsigned long)nmb_heap_size(_handle), (unsigned long)nmb_heap_used_size(_handle),(unsigned long)nmb_heap_max_available_size(_handle,answer[1]?answer[1]:1)); @@ -3874,7 +3996,7 @@ static bool pipelineArchives(NSArray *list,std::vector &arch NmbTextureDescriptor descriptor;if(!textureDescriptor(d,&descriptor))return nil; NmbTexture *handle=nmb_texture_new(gDevice,&descriptor);noteFormat((uint32_t)descriptor.format,handle!=nullptr); if(gLogTextures && textureLogTurn()) - NSLog(@"native-metal: texture-log %llu texture %llux%llu fmt %llu type %llu levels %llu usage %llx options %llx %s", + GLOG(@"native-metal: texture-log %llu texture %llux%llu fmt %llu type %llu levels %llu usage %llx options %llx %s", (unsigned long long)gTexturesLogged.load(),(unsigned long long)descriptor.width,(unsigned long long)descriptor.height, (unsigned long long)descriptor.format,(unsigned long long)descriptor.type,(unsigned long long)descriptor.levels, (unsigned long long)descriptor.usage,(unsigned long long)descriptor.options,handle?"made":"REFUSED"); @@ -4024,6 +4146,7 @@ static bool pipelineArchives(NSArray *list,std::vector &arch gTimePipeline.add(pipelineBegan); if(!handle) { NBFunction *f=(NBFunction *)function; + nbNotePipeline(false,true,nowUs()-pipelineBegan,0,0,0,f.name.UTF8String,"-",lastError().UTF8String); noteProblem([NSString stringWithFormat:@"compute %@ k=%s: %@",f.name,f.constantsText.c_str(),lastError()]); return failWith>(error,@"compute pipeline",nil); } @@ -4031,6 +4154,7 @@ static bool pipelineArchives(NSArray *list,std::vector &arch NmbComputePipelineDescriptor p={};p.function=built.handle;p.max_call_stack_depth=1; *reflection=(MTLComputePipelineReflection *)computeReflection(&p); } + nbNotePipeline(true,true,nowUs()-pipelineBegan,0,0,0,((NBFunction *)function).name.UTF8String,"-",""); NBComputePipeline *state=[NBComputePipeline new];state.handle=handle;gPipelines++;if(error)*error=nil;return (id)state; } - (id)newComputePipelineStateWithDescriptor:(MTLComputePipelineDescriptor *)d options:(MTLPipelineOption)options @@ -4080,6 +4204,8 @@ static bool pipelineArchives(NSArray *list,std::vector &arch static std::atomic told{0}; if(told.fetch_add(1)<64)NBLOG(@"native-metal: render pipeline descriptor refused (vertex %@, fragment %@): %@",d.vertexFunction.name, d.fragmentFunction.name,error?(*error).localizedDescription:@"?"); + nbNotePipeline(false,false,0,0,0,(uint32_t)d.colorAttachments[0].pixelFormat,d.fragmentFunction.name.UTF8String,d.vertexFunction.name.UTF8String, + [(error?(*error).localizedDescription:@"descriptor") UTF8String]); delete p;return nil; } std::vector archives; @@ -4087,7 +4213,9 @@ static bool pipelineArchives(NSArray *list,std::vector &arch const NmbPipelineOptions creation={(uint64_t)options,archives.data(),archives.size()}; const uint64_t pipelineBegan=nowUs();const NBCost costBegan=NBCost::now(); uint64_t cacheBefore[10]={};if(!gJournalPath.empty())nmb_device_work_stats(gDevice,cacheBefore); + uint32_t programVs=0,programFs=0;nbLastProgram(&programVs,&programFs); // clear what an earlier creation on this thread left NmbRenderPipeline *handle=nmb_render_pipeline_new_with_options(gDevice,p,&creation);gTimePipeline.add(pipelineBegan); + nbLastProgram(&programVs,&programFs); if(handle && reflection && wantsReflection(options))*reflection=(MTLRenderPipelineReflection *)renderReflection(p); delete p; std::string cost=gJournalPath.empty()?std::string():NBCost::now().since(costBegan); @@ -4098,6 +4226,8 @@ static bool pipelineArchives(NSArray *list,std::vector &arch cost+=text; } journalRenderPipeline(d,pipelineBegan,handle?nullptr:nmb_last_error(),cost); + nbNotePipeline(handle!=nullptr,false,nowUs()-pipelineBegan,programVs,programFs,(uint32_t)d.colorAttachments[0].pixelFormat,d.fragmentFunction.name.UTF8String, + d.vertexFunction.name.UTF8String,handle?"":lastError().UTF8String); if(!handle) { // The tail of the text (what a client's problem row keeps) names the fragment function, the linked functions given and the compiler's // state: a missing linked function there is RenderBox's stitched custom_blend, which needs the compiler. @@ -4235,7 +4365,7 @@ extern "C" id native_bridge_metal_open(const char *loader,const char *cache,uint if(loader)gLoaderPath=loader; if(!gDevice){NBLOG(@"native-metal: device refused: %s",nmb_last_error());return nil;} watchImportPressure(); - if(cache){gJournalPath=std::string(cache)+"/pipelines.log";gJournalBegan=nowUs();} + if(cache && logGpuOn()){gJournalPath=std::string(cache)+"/pipelines.log";gJournalBegan=nowUs();} char name[128]={};nmb_device_name(gDevice,name,sizeof name);gName=[NSString stringWithUTF8String:name];gRegistryID=registryID; // The target families supportsFamily: answers true, with the rows of Apple's tables the device does not meet in them (nmb_device_family_waivers). {char waived[4096]={};nmb_device_family_waivers(gDevice,waived,sizeof waived); From a0a34fa2588efa982edc890980a27dbb4efff732 Mon Sep 17 00:00:00 2001 From: rpone Date: Sun, 1 Nov 2026 05:14:01 +0800 Subject: [PATCH 92/92] 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. --- NativeAdrenoMetalPlugin.mm | 5 +- .../0023-quality-of-service-on-ios.patch | 56 +++++++++ ...-diagnostics-switch-and-submit-steps.patch | 118 ++++++++++++++++++ 3 files changed, 178 insertions(+), 1 deletion(-) create mode 100644 bridge/patches/0023-quality-of-service-on-ios.patch create mode 100644 bridge/patches/0024-diagnostics-switch-and-submit-steps.patch diff --git a/NativeAdrenoMetalPlugin.mm b/NativeAdrenoMetalPlugin.mm index 5f0bde3..a7c0750 100644 --- a/NativeAdrenoMetalPlugin.mm +++ b/NativeAdrenoMetalPlugin.mm @@ -225,7 +225,7 @@ static void startPassReporter(unsigned port) { std::thread([gpu]{ pthread_setname_np("pass-reporter"); for (;;) { - sleep(10); + sleep(5); char row[200]; if (!nmb_slowest_pass(row, sizeof row) || !row[0]) continue; streamText(*gpu, row); @@ -323,6 +323,9 @@ static id deviceForPort(unsigned port) { // submission ring, the runtime's stage ring, and in the render server the status and telemetry lines. Without it none of them runs. if (bootArgSet("sheng-diag=1")) setenv("SHENG_DIAG", "1", 1); else unsetenv("SHENG_DIAG"); + // log-gpu=1: the GPU stack's log lines (NSLog, the pipeline journal); off without it. + if (bootArgSet("log-gpu=1")) setenv("SHENG_LOG_GPU", "1", 1); + else unsetenv("SHENG_LOG_GPU"); if (strcmp(getprogname(), "backboardd") != 0) return device = clientDevice(port); // The discovery releases the accelerator port when it is done; the monitor and the scanout client keep using it. if (void *iokit = dlopen("/System/Library/Frameworks/IOKit.framework/IOKit", RTLD_NOW | RTLD_LOCAL)) { diff --git a/bridge/patches/0023-quality-of-service-on-ios.patch b/bridge/patches/0023-quality-of-service-on-ios.patch new file mode 100644 index 0000000..99da350 --- /dev/null +++ b/bridge/patches/0023-quality-of-service-on-ios.patch @@ -0,0 +1,56 @@ +diff --git a/crates/metal2vulkan-runtime/src/exit_guard.rs b/crates/metal2vulkan-runtime/src/exit_guard.rs +index c1fe5874..ba1d6e34 100644 +--- a/crates/metal2vulkan-runtime/src/exit_guard.rs ++++ b/crates/metal2vulkan-runtime/src/exit_guard.rs +@@ -955,7 +955,7 @@ thread_local! { + /// `device/compiles.rs`). MoltenVK runs each + /// compile on a dispatch queue that takes the class of the thread asking, so the class here is the + /// compile's class in Metal's compiler service. +-#[cfg(target_os = "macos")] ++#[cfg(any(target_os = "macos", target_os = "ios"))] + pub(crate) fn raise_to_user_initiated() { + if !at_least_user_initiated() { + // SAFETY: sets the calling thread's own class; no pointer. +@@ -965,13 +965,13 @@ pub(crate) fn raise_to_user_initiated() { + } + } + +-/// Nothing: no quality of service class here ([`raise_to_user_initiated`] on macOS). +-#[cfg(not(target_os = "macos"))] ++/// Nothing: no quality of service class here ([`raise_to_user_initiated`] on macOS and iOS). ++#[cfg(not(any(target_os = "macos", target_os = "ios")))] + pub(crate) fn raise_to_user_initiated() {} + + /// Run this thread at the user-interactive quality of service, whatever its class: a thread of + /// the runtime's own that takes command buffers to their handlers ([`spawn_user_interactive`]). +-#[cfg(target_os = "macos")] ++#[cfg(any(target_os = "macos", target_os = "ios"))] + fn run_at_user_interactive() { + // Metal runs every command buffer's scheduled and completed handlers at the user-interactive + // class, whatever the class of the thread that committed it: measured on an M3 Max, macOS +@@ -1092,12 +1092,12 @@ impl SchedAttr { + } + + /// Nothing: no such hint here ([`run_at_user_interactive`] on macOS and Linux). +-#[cfg(not(any(target_os = "macos", target_os = "linux")))] ++#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "linux")))] + fn run_at_user_interactive() {} + + /// Whether this thread runs at the user-initiated quality of service or above: its class is + /// user-initiated or user-interactive. Always where there is no such class. +-#[cfg(target_os = "macos")] ++#[cfg(any(target_os = "macos", target_os = "ios"))] + pub(crate) fn at_least_user_initiated() -> bool { + use libc::qos_class_t as Q; + let mut class = Q::QOS_CLASS_UNSPECIFIED; +@@ -1112,8 +1112,8 @@ pub(crate) fn at_least_user_initiated() -> bool { + ) + } + +-/// Always: no quality of service class here ([`at_least_user_initiated`] on macOS). +-#[cfg(not(target_os = "macos"))] ++/// Always: no quality of service class here ([`at_least_user_initiated`] on macOS and iOS). ++#[cfg(not(any(target_os = "macos", target_os = "ios")))] + pub(crate) fn at_least_user_initiated() -> bool { + true + } diff --git a/bridge/patches/0024-diagnostics-switch-and-submit-steps.patch b/bridge/patches/0024-diagnostics-switch-and-submit-steps.patch new file mode 100644 index 0000000..11ac81c --- /dev/null +++ b/bridge/patches/0024-diagnostics-switch-and-submit-steps.patch @@ -0,0 +1,118 @@ +diff --git a/crates/metal2vulkan-runtime/src/command_buffer/submit.rs b/crates/metal2vulkan-runtime/src/command_buffer/submit.rs +index e5e7a777..b11379b2 100644 +--- a/crates/metal2vulkan-runtime/src/command_buffer/submit.rs ++++ b/crates/metal2vulkan-runtime/src/command_buffer/submit.rs +@@ -478,6 +478,7 @@ impl CommandBufferShared { + }; + // The scrubs of emulated textures' writes, as mapped now, then every segment in its place. + self.record_scrubs(&mut state, range.clone())?; ++ crate::stage_trace::mark(Arc::as_ptr(self) as usize, crate::stage_trace::Stage::SubmitStep, 1); + state + .place(range.clone()) + .map_err(|error| recording_error(device, error))?; +@@ -939,6 +940,7 @@ impl CommandBufferShared { + let buffers = (submits.iter()) + .map(|info| info.command_buffer_count as usize) + .sum(); ++ crate::stage_trace::mark(Arc::as_ptr(self) as usize, crate::stage_trace::Stage::SubmitStep, 2); + // From here its heap set versions may be pending: nothing writes them any more, and its + // first batch takes the ticket a texture that goes from now on waits for + // (`heap_sets.rs`). One committed and not yet submitted holds no gone texture. +@@ -989,6 +991,7 @@ impl CommandBufferShared { + scheduler + .group + .submit(device, &mut scheduler.waited[queue], Some(after)); ++ crate::stage_trace::mark(Arc::as_ptr(self) as usize, crate::stage_trace::Stage::SubmitStep, 3); + if let Err(error) = submitted { + if let Some(fence) = fence { + // SAFETY: the failed submission is not pending; inside the section. +@@ -1070,6 +1073,7 @@ impl CommandBufferShared { + // The holds its completion lets go (`State::holds_until`). + let holds = state.holds_until(index); + drop(state); ++ crate::stage_trace::mark(Arc::as_ptr(self) as usize, crate::stage_trace::Stage::SubmitStep, 4); + if rebound { + device.degrade_encoded_use_rebound(rebound_use); + } +diff --git a/crates/metal2vulkan-runtime/src/creation.rs b/crates/metal2vulkan-runtime/src/creation.rs +index bbb1fb47..c6f0995a 100644 +--- a/crates/metal2vulkan-runtime/src/creation.rs ++++ b/crates/metal2vulkan-runtime/src/creation.rs +@@ -117,7 +117,8 @@ pub(crate) unsafe fn allocate_memory( + /// The call stacks that allocated device memory of at least [`SITE_BYTES`], by the bytes they + /// allocated: what a process's GPU memory went to, without a debugger on the device. A stack is + /// its first [`SITE_FRAMES`] return addresses past this function's; at most [`SITE_COUNT`] +-/// stacks are kept, a new one past them counted under the last. ++/// stacks are kept, a new one past them counted under the last. Kept only with the diagnostics on ++/// (`stage_trace::diagnostics`). + const SITE_BYTES: u64 = 4 << 20; + const SITE_FRAMES: usize = 10; + const SITE_COUNT: usize = 32; +@@ -135,7 +136,7 @@ pub struct AllocationSite { + } + + fn note_allocation_site(bytes: u64) { +- if bytes < SITE_BYTES { ++ if bytes < SITE_BYTES || !crate::stage_trace::diagnostics() { + return; + } + let mut raw = [std::ptr::null_mut::(); SITE_FRAMES + 2]; +diff --git a/crates/metal2vulkan-runtime/src/stage_trace.rs b/crates/metal2vulkan-runtime/src/stage_trace.rs +index d401a6c8..53af5b0b 100644 +--- a/crates/metal2vulkan-runtime/src/stage_trace.rs ++++ b/crates/metal2vulkan-runtime/src/stage_trace.rs +@@ -8,11 +8,14 @@ + //! keeps the newest [`RING`] records; a reader that falls behind loses the oldest. + //! - **One clock.** The host's uptime clock in nanoseconds (`CLOCK_UPTIME_RAW` on Apple + //! platforms), the one the frontend's trace and the driver's rings use. ++//! - **Off unless asked for.** Records are kept only where the host's environment has ++//! `SHENG_DIAG` ([`diagnostics`]), read once; the same switch keeps the allocation sites ++//! (`creation.rs`). Without it a record costs one relaxed load. + //! - **Ids.** A command buffer's id is the address of its shared state, alive from its commit to + //! its completion: records of one id between a commit stage and its completion are one command + //! buffer's. + +-use std::sync::atomic::{AtomicU64, Ordering}; ++use std::sync::atomic::{AtomicU64, AtomicU8, Ordering}; + + /// The records the ring keeps. + const RING: usize = 1 << 16; +@@ -53,6 +56,10 @@ pub enum Stage { + Seen = 14, + /// Its completion ran (`arg`: 1 on the waiter, 2 posted to its queue's context). + Finished = 15, ++ /// A step inside a batch's submission ended (`arg`: 1 the scrubs and segments placed, 2 its ++ /// waits and signals gathered, 3 the driver's `vkQueueSubmit` returned, 4 the submission's ++ /// changes made). ++ SubmitStep = 16, + /// The scheduler's thread went to sleep (id 0, `arg`: slice in ms, | 0x8000 on the GPU). + SchedulerSleep = 20, + /// Something woke the scheduler's thread (id 0, `arg`: 0 commit, 1 completion, 2 host). +@@ -102,8 +109,26 @@ pub fn now_nanos() -> u64 { + } + } + +-/// Record `stage` of `id` with `arg`, now. ++/// Whether the host asked for the runtime's diagnostics: `SHENG_DIAG` in the environment, read ++/// once. ++pub(crate) fn diagnostics() -> bool { ++ // 0 not read yet, 1 off, 2 on. ++ static ON: AtomicU8 = AtomicU8::new(0); ++ match ON.load(Ordering::Relaxed) { ++ 0 => { ++ let on = std::env::var_os("SHENG_DIAG").is_some(); ++ ON.store(if on { 2 } else { 1 }, Ordering::Relaxed); ++ on ++ } ++ state => state == 2, ++ } ++} ++ ++/// Record `stage` of `id` with `arg`, now, where the diagnostics are on ([`diagnostics`]). + pub(crate) fn mark(id: usize, stage: Stage, arg: u32) { ++ if !diagnostics() { ++ return; ++ } + let sequence = NEXT.fetch_add(1, Ordering::Relaxed); + let slot = &SLOTS[(sequence as usize) & (RING - 1)]; + slot.word.store(0, Ordering::Release);