// Authored adaptation of Vision face support in FCP-BeautyGirl (MIT). // Geometry is rebuilt from this input frame; no previous-shot mask is reused. - (void)prepareFaceSupport:(FxImageTile*)source { [_faceEntries release];_faceEntries=nil;std::vector entries; FxRect image=source.imagePixelBounds,tile=source.tilePixelBounds; if(image.left!=tile.left||image.right!=tile.right||image.top!=tile.top||image.bottom!=tile.bottom)return; NSMutableDictionary *device=[self device:source.deviceRegistryID error:nil];idtexture=[source metalTextureForDevice:device[@"device"]];if(!texture)return; if(!_faceContext)_faceContext=[[CIContext contextWithMTLDevice:device[@"device"] options:@{kCIContextCacheIntermediates:@NO}]retain]; CGColorSpaceRef linear=CGColorSpaceCreateWithName(kCGColorSpaceLinearSRGB); CIImage *input=[CIImage imageWithMTLTexture:texture options:@{kCIImageColorSpace:(id)linear}];CGColorSpaceRelease(linear);if(!input)return; // FxPlug's bottom-left textures need the same visual orientation as mask UV. if(source.imageOrigin==kFxImageOrigin_BOTTOM_LEFT)input=[input imageByApplyingTransform:CGAffineTransformMake(1,0,0,-1,0,input.extent.size.height)]; CGFloat scale=MIN(1.,960./MAX(input.extent.size.width,input.extent.size.height));input=[input imageByApplyingTransform:CGAffineTransformMakeScale(scale,scale)]; CGColorSpaceRef srgb=CGColorSpaceCreateWithName(kCGColorSpaceSRGB);CGImageRef cg=[_faceContext createCGImage:input fromRect:input.extent format:kCIFormatRGBA8 colorSpace:srgb];CGColorSpaceRelease(srgb);if(!cg)return; VNDetectFaceLandmarksRequest *request=[VNDetectFaceLandmarksRequest new];request.revision=VNDetectFaceLandmarksRequestRevision3;request.constellation=VNRequestFaceLandmarksConstellation76Points; VNImageRequestHandler *handler=[[VNImageRequestHandler alloc]initWithCGImage:cg orientation:kCGImagePropertyOrientationUp options:@{}];CGImageRelease(cg); if([handler performRequests:@[request] error:nil])for(VNFaceObservation *face in request.results){ if(entries.size()>64||face.confidence<.5||!face.landmarks.faceContour.pointCount)continue;CGRect box=face.boundingBox;if(box.size.width<.015||box.size.height<.015)continue; DYToolMaskEntry support{};support.section=-100;support.mask.parameters=dyDefaultMaskParameters();auto &p=support.mask.parameters;p.kind=DYMaskBezier;p.centerX=CGRectGetMidX(box);p.centerY=1-CGRectGetMidY(box);p.width=box.size.width;p.height=box.size.height;p.feather=.1; auto &path=support.mask.path;VNFaceLandmarkRegion2D *contour=face.landmarks.faceContour; for(NSUInteger i=0;ipath.x[0];for(int i=0;i<=12;i++){float t=i/12.f,x=forward?1-t:t;path.x[path.count]=x-.5;path.y[path.count]=-.28-.28*sinf(t*M_PI);path.count++;}entries.push_back(support); for(VNFaceLandmarkRegion2D *region in @[face.landmarks.leftEye?:[NSNull null],face.landmarks.rightEye?:[NSNull null],face.landmarks.outerLips?:[NSNull null],face.landmarks.leftEyebrow?:[NSNull null],face.landmarks.rightEyebrow?:[NSNull null]]){ if(![region isKindOfClass:VNFaceLandmarkRegion2D.class]||!region.pointCount)continue;CGFloat loX=1,loY=1,hiX=0,hiY=0;for(NSUInteger i=0;i.5&&u.skinFaceOnly>.5)||u.skinVisualize>.5;for(NSDictionary *node in tools[@"layers"]){DYUniforms layer=DYToolUniforms(node);face|=[node[@"type"]isEqual:@"skin"]&&layer.skinProtection.x>.5&&layer.skinFaceOnly>.5;} if(face&&!_exporting)[self prepareFaceSupport:src];else{[_faceEntries release];_faceEntries=nil;} BOOL ok=[self renderToolsSource:src destination:dst uniforms:u paths:paths spider:spider tools:tools error:error]; if(ok&&u.skinVisualize>.5&&!DYBaseToolRemoved(tools,@"skin")){ NSMutableDictionary *entry=[self device:dst.deviceRegistryID error:error];idd=entry[@"device"];idoutput=[dst metalTextureForDevice:d],input=[src metalTextureForDevice:d]; MTLTextureDescriptor *desc=[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:output.pixelFormat width:output.width height:output.height mipmapped:NO];desc.storageMode=MTLStorageModePrivate;desc.usage=MTLTextureUsageShaderRead|MTLTextureUsageShaderWrite|MTLTextureUsageRenderTarget;idpreview=[d newTextureWithDescriptor:desc]; if(!preview)return fail(error,@"无法分配肤色区域预览");DYToolMaskHeader header{};header.section=3;header.faceOnly=u.skinFaceOnly>.5;idmasks=[self maskBuffer:tools[@"baseMasks"] tile:src device:d header:&header];idcb=[entry[@"queue"]commandBuffer];idce=[cb computeCommandEncoder];[ce setComputePipelineState:entry[@"dySkinOverlay"]];[ce setTexture:input atIndex:0];[ce setTexture:output atIndex:1];[ce setTexture:preview atIndex:2];[ce setBuffer:masks offset:0 atIndex:0];[ce setBytes:&header length:sizeof(header) atIndex:1];[ce setBytes:&u length:sizeof(u) atIndex:2];[ce dispatchThreads:MTLSizeMake(output.width,output.height,1)threadsPerThreadgroup:MTLSizeMake(16,16,1)];[ce endEncoding];[cb commit];[cb waitUntilCompleted]; DYExportTile *tile=[[DYExportTile alloc]initWithTexture:preview registry:d.registryID];[tile setBounds:dst.tilePixelBounds origin:dst.imageOrigin];[tile setPixelAspect:dst.pixelAspect];ok=cb.status==MTLCommandBufferStatusCompleted&&[self copyTile:tile to:dst entry:entry error:error];[tile release];[preview release]; } return ok; }} - (BOOL)renderLens:(FxImageTile*)source destination:(FxImageTile*)destination uniforms:(DYUniforms)u masks:(NSArray*)masks entry:(NSDictionary*)entry error:(NSError**)error { idd=entry[@"device"];idbefore=[source metalTextureForDevice:d],output=[destination metalTextureForDevice:d]; idcb=[entry[@"queue"]commandBuffer];idce=[cb computeCommandEncoder];[ce setComputePipelineState:entry[@"dyLensRepair"]];[ce setTexture:before atIndex:0];[ce setTexture:output atIndex:1];[ce setBytes:&u length:sizeof(u) atIndex:0];float aspect=before.width*source.pixelAspect/MAX(1.,(double)before.height);[ce setBytes:&aspect length:sizeof(aspect) atIndex:1];[ce dispatchThreads:MTLSizeMake(output.width,output.height,1)threadsPerThreadgroup:MTLSizeMake(16,16,1)];[ce endEncoding];[cb commit];[cb waitUntilCompleted];if(cb.status!=MTLCommandBufferStatusCompleted)return fail(error,cb.error.localizedDescription); if(!masks.count)return YES;MTLTextureDescriptor *desc=[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:output.pixelFormat width:output.width height:output.height mipmapped:NO];desc.storageMode=MTLStorageModePrivate;desc.usage=MTLTextureUsageShaderRead|MTLTextureUsageShaderWrite|MTLTextureUsageRenderTarget;idblend=[d newTextureWithDescriptor:desc];if(!blend)return fail(error,@"无法分配修复蒙版");DYToolMaskHeader h{};h.section=(int)[DYToolTypes()indexOfObject:@"lens"];idbuffer=[self maskBuffer:@{@"lens":masks}tile:source device:d header:&h];cb=[entry[@"queue"]commandBuffer];[self encodeBlend:before after:output output:blend masks:buffer header:h entry:entry commands:cb];[cb commit];[cb waitUntilCompleted];DYExportTile *tile=[[DYExportTile alloc]initWithTexture:blend registry:d.registryID];[tile setBounds:destination.tilePixelBounds origin:destination.imageOrigin];[tile setPixelAspect:destination.pixelAspect];BOOL ok=cb.status==MTLCommandBufferStatusCompleted&&[self copyTile:tile to:destination entry:entry error:error];[tile release];[blend release];return ok; }