文件
dayu-color/大鱼调色/Plugin/DYMosaicRender.inc.h
T

27 行
2.7 KiB
C

// Runs on the original frame before grading, then follows the same spatial
// transforms as the picture. Geometry has already been evaluated at render time.
- (FxImageTile*)sourceByApplyingMosaic:(NSDictionary*)tools source:(FxImageTile*)src registry:(uint64_t)registry error:(NSError**)error {
NSDictionary *settings=tools[@"mosaic"];if(![settings[@"enabled"]boolValue])return src;
FxRect image=src.imagePixelBounds,bounds=src.tilePixelBounds;
if(memcmp(&image,&bounds,sizeof(FxRect))!=0){fail(error,@"局部马赛克需要完整源画面。");return nil;}
BOOL face=[settings[@"source"]isEqual:@"face"];
NSDictionary *mask=tools[@"mosaicRenderMask"]?:DYSharedMaskWithID(tools,settings[@"maskID"]);
if(!face&&(!mask||mask[@"stampSource"]||![mask[@"enabled"]boolValue]))return src;
NSMutableDictionary *entry=[self device:registry error:error];if(!entry)return nil;
if(face)[self prepareFaceSupport:src];else{[_faceEntries release];_faceEntries=nil;}
DYToolMaskHeader header{};header.section=face?-102:0;
id<MTLDevice>device=entry[@"device"];id<MTLTexture>input=[src metalTextureForDevice:device];
id<MTLBuffer>buffer=[self maskBuffer:face?@{}:@{@"primary":@[mask]} tile:src device:device header:&header];
MTLTextureDescriptor *desc=[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA32Float width:input.width height:input.height mipmapped:NO];desc.storageMode=MTLStorageModePrivate;desc.usage=MTLTextureUsageShaderRead|MTLTextureUsageShaderWrite;
id<MTLTexture>output=[[device newTextureWithDescriptor:desc]autorelease];
id<MTLComputePipelineState>pipeline=[self managedKernel:@"dyLocalMosaic" entry:entry error:error];
if(!input||!buffer||!output||!pipeline){fail(error,@"局部马赛克缓冲区不可用。");return nil;}
// Size is in 1080-line reference pixels, stable under proxy/viewer scaling.
uint32_t block=(uint32_t)MAX(2,round([settings[@"size"]doubleValue]*input.height/1080.));
id<MTLCommandBuffer>cb=[entry[@"queue"]commandBuffer];id<MTLComputeCommandEncoder>ce=[cb computeCommandEncoder];
[ce setComputePipelineState:pipeline];[ce setTexture:input atIndex:0];[ce setTexture:output atIndex:1];[ce setBuffer:buffer offset:0 atIndex:0];[ce setBytes:&header length:sizeof(header) atIndex:1];[ce setBytes:&block length:sizeof(block) atIndex:2];
[ce dispatchThreads:MTLSizeMake(input.width,input.height,1)threadsPerThreadgroup:MTLSizeMake(16,16,1)];[ce endEncoding];[cb commit];[cb waitUntilCompleted];
if(cb.status!=MTLCommandBufferStatusCompleted){fail(error,cb.error.localizedDescription);return nil;}
DYExportTile *tile=[[[DYExportTile alloc]initWithTexture:output registry:registry]autorelease];[tile setBounds:bounds origin:src.imageOrigin];[tile setPixelAspect:src.pixelAspect];[tile inheritColorSpace:src.colorSpace];return tile;
}