// // MetalDeviceCache.m // 大鱼调色 // // Created by mac on 2026/10/31. // #import "MetalDeviceCache.h" const NSUInteger kMaxCommandQueues = 5; static NSString* kKey_InUse = @"InUse"; static NSString* kKey_CommandQueue = @"CommandQueue"; static MetalDeviceCache* gDeviceCache = nil; @interface MetalDeviceCacheItem : NSObject @property (readonly) id gpuDevice; @property (readonly) id pipelineState; @property (retain) NSMutableArray* commandQueueCache; @property (readonly) NSLock* commandQueueCacheLock; @property (readonly) MTLPixelFormat pixelFormat; - (instancetype)initWithDevice:(id)device pixelFormat:(MTLPixelFormat)pixFormat; - (id)getNextFreeCommandQueue; - (void)returnCommandQueue:(id)commandQueue; - (BOOL)containsCommandQueue:(id)commandQueue; @end @implementation MetalDeviceCacheItem - (instancetype)initWithDevice:(id)device pixelFormat:(MTLPixelFormat)pixFormat; { self = [super init]; if (self != nil) { _gpuDevice = [device retain]; _commandQueueCache = [[NSMutableArray alloc] initWithCapacity:kMaxCommandQueues]; for (NSUInteger i = 0; (_commandQueueCache != nil) && (i < kMaxCommandQueues); i++) { NSMutableDictionary* commandDict = [NSMutableDictionary dictionary]; [commandDict setObject:[NSNumber numberWithBool:NO] forKey:kKey_InUse]; id commandQueue = [_gpuDevice newCommandQueue]; [commandDict setObject:commandQueue forKey:kKey_CommandQueue]; [_commandQueueCache addObject:commandDict]; } // Load all the shader files with a .metal file extension in the project id defaultLibrary = [[_gpuDevice newDefaultLibrary] autorelease]; // Load the vertex function from the library id vertexFunction = [[defaultLibrary newFunctionWithName:@"vertexShader"] autorelease]; // Load the fragment function from the library id fragmentFunction = [[defaultLibrary newFunctionWithName:@"fragmentShader"] autorelease]; // Configure a pipeline descriptor that is used to create a pipeline state MTLRenderPipelineDescriptor *pipelineStateDescriptor = [[[MTLRenderPipelineDescriptor alloc] init] autorelease]; pipelineStateDescriptor.label = @"Simple Pipeline"; pipelineStateDescriptor.vertexFunction = vertexFunction; pipelineStateDescriptor.fragmentFunction = fragmentFunction; pipelineStateDescriptor.colorAttachments[0].pixelFormat = pixFormat; _pixelFormat = pixFormat; NSError* error = nil; _pipelineState = [_gpuDevice newRenderPipelineStateWithDescriptor:pipelineStateDescriptor error:&error]; if (error != nil) { NSLog (@"Error generating brightness pipeline state: %@", error); } if (_commandQueueCache != nil) { _commandQueueCacheLock = [[NSLock alloc] init]; } if ((_gpuDevice == nil) || (_commandQueueCache == nil) || (_commandQueueCacheLock == nil) || (_pipelineState == nil)) { [self release]; self = nil; } } return self; } - (void)dealloc { [_gpuDevice release]; [_commandQueueCache release]; [_commandQueueCacheLock release]; [_pipelineState release]; [super dealloc]; } - (id)getNextFreeCommandQueue { id result = nil; [_commandQueueCacheLock lock]; NSUInteger index = 0; while ((result == nil) && (index < kMaxCommandQueues)) { NSMutableDictionary* nextCommandQueue = [_commandQueueCache objectAtIndex:index]; NSNumber* inUse = [nextCommandQueue objectForKey:kKey_InUse]; if (![inUse boolValue]) { [nextCommandQueue setObject:[NSNumber numberWithBool:YES] forKey:kKey_InUse]; result = [nextCommandQueue objectForKey:kKey_CommandQueue]; } index++; } [_commandQueueCacheLock unlock]; return result; } - (void)returnCommandQueue:(id)commandQueue { [_commandQueueCacheLock lock]; BOOL found = false; NSUInteger index = 0; while ((!found) && (index < kMaxCommandQueues)) { NSMutableDictionary* nextCommandQueuDict = [_commandQueueCache objectAtIndex:index]; id nextCommandQueue = [nextCommandQueuDict objectForKey:kKey_CommandQueue]; if (nextCommandQueue == commandQueue) { found = YES; [nextCommandQueuDict setObject:[NSNumber numberWithBool:NO] forKey:kKey_InUse]; } index++; } [_commandQueueCacheLock unlock]; } - (BOOL)containsCommandQueue:(id)commandQueue { BOOL found = NO; NSUInteger index = 0; while ((!found) && (index < kMaxCommandQueues)) { NSMutableDictionary* nextCommandQueuDict = [_commandQueueCache objectAtIndex:index]; id nextCommandQueue = [nextCommandQueuDict objectForKey:kKey_CommandQueue]; if (nextCommandQueue == commandQueue) { found = YES; } index++; } return found; } @end @implementation MetalDeviceCache + (MTLPixelFormat)MTLPixelFormatForImageTile:(FxImageTile*)imageTile { MTLPixelFormat result = MTLPixelFormatRGBA16Float; switch (imageTile.ioSurface.pixelFormat) { case kCVPixelFormatType_128RGBAFloat: result = MTLPixelFormatRGBA32Float; break; case kCVPixelFormatType_32BGRA: result = MTLPixelFormatBGRA8Unorm; break; default: break; } return result; } + (MetalDeviceCache*)deviceCache; { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ gDeviceCache = [[MetalDeviceCache alloc] init]; }); return gDeviceCache; } - (instancetype)init { self = [super init]; if (self != nil) { NSArray>* devices = MTLCopyAllDevices(); deviceCaches = [[NSMutableArray alloc] initWithCapacity:devices.count]; for (id nextDevice in devices) { MetalDeviceCacheItem* newCacheItem = [[[MetalDeviceCacheItem alloc] initWithDevice:nextDevice pixelFormat:MTLPixelFormatRGBA16Float] autorelease]; [deviceCaches addObject:newCacheItem]; } [devices release]; } return self; } - (void)dealloc { [deviceCaches release]; [super dealloc]; } - (id)deviceWithRegistryID:(uint64_t)registryID { for (MetalDeviceCacheItem* nextCacheItem in deviceCaches) { if (nextCacheItem.gpuDevice.registryID == registryID) { return nextCacheItem.gpuDevice; } } return nil; } - (id)pipelineStateWithRegistryID:(uint64_t)registryID pixelFormat:(MTLPixelFormat)pixFormat { for (MetalDeviceCacheItem* nextCacheItem in deviceCaches) { if ((nextCacheItem.gpuDevice.registryID == registryID) && (nextCacheItem.pixelFormat == pixFormat)) { return nextCacheItem.pipelineState; } } // Didn't find one, so create one with the right settings NSArray>* devices = MTLCopyAllDevices(); id device = nil; for (id nextDevice in devices) { if (nextDevice.registryID == registryID) { device = nextDevice; } } id result = nil; if (device != nil) { MetalDeviceCacheItem* newCacheItem = [[[MetalDeviceCacheItem alloc] initWithDevice:device pixelFormat:pixFormat] autorelease]; if (newCacheItem != nil) { [deviceCaches addObject:newCacheItem]; result = newCacheItem.pipelineState; } } [devices release]; return result; } - (id)commandQueueWithRegistryID:(uint64_t)registryID pixelFormat:(MTLPixelFormat)pixFormat; { for (MetalDeviceCacheItem* nextCacheItem in deviceCaches) { if ((nextCacheItem.gpuDevice.registryID == registryID) && (nextCacheItem.pixelFormat == pixFormat)) { return [nextCacheItem getNextFreeCommandQueue]; } } // Didn't find one, so create one with the right settings NSArray>* devices = MTLCopyAllDevices(); id device = nil; for (id nextDevice in devices) { if (nextDevice.registryID == registryID) { device = nextDevice; } } id result = nil; if (device != nil) { MetalDeviceCacheItem* newCacheItem = [[[MetalDeviceCacheItem alloc] initWithDevice:device pixelFormat:pixFormat] autorelease]; if (newCacheItem != nil) { [deviceCaches addObject:newCacheItem]; result = [newCacheItem getNextFreeCommandQueue]; } } [devices release]; return result; } - (void)returnCommandQueueToCache:(id)commandQueue; { for (MetalDeviceCacheItem* nextCacheItem in deviceCaches) { if ([nextCacheItem containsCommandQueue:commandQueue]) { [nextCacheItem returnCommandQueue:commandQueue]; break; } } } @end