Show synchronized frame previews and progress for stamp and mask tracking

这个提交包含在:
mac 于 2026-11-06 01:45:18 +08:00 提交
1 父节点 7b6a3106ca
当前提交 687a366e1c
11 个文件已更改
+363 -17

没有与搜索条件相匹配的文件

+37 -2
查看文件
@@ -2,13 +2,22 @@
// CF asks the host for video gamma 2.2, then Lanczos-scales to RGBA8 before
// OpenCV gray conversion. Adapt our linear host contract in a separate texture.
- (dy::GrayFrame)toolGrayFrame:(FxImageTile*)tile error:(NSError**)error {
return [self toolGrayFrame:tile preview:nil error:error];
}
- (dy::GrayFrame)toolGrayFrame:(FxImageTile*)tile preview:(NSDictionary**)preview error:(NSError**)error {
@synchronized(_toolCaptureDevices){
FxRect bounds=tile.tilePixelBounds,image=tile.imagePixelBounds;
if(bounds.left!=image.left||bounds.right!=image.right||bounds.bottom!=image.bottom||bounds.top!=image.top)return {};
NSNumber *key=@(tile.deviceRegistryID);NSMutableDictionary *entry=_toolCaptureDevices[key];
if(!entry){NSArray *devices=MTLCopyAllDevices();id<MTLDevice>device=nil;for(id<MTLDevice>candidate in devices)if(candidate.registryID==tile.deviceRegistryID)device=candidate;
if(device){id<MTLLibrary>library=[device newLibraryWithURL:[[NSBundle bundleForClass:self.class]URLForResource:@"default" withExtension:@"metallib"]error:error];id<MTLFunction>gray=[library newFunctionWithName:@"dyToolCapture"],gamma=[library newFunctionWithName:@"dyToolEncodeGamma22"];id<MTLComputePipelineState>grayPipeline=gray?[device newComputePipelineStateWithFunction:gray error:error]:nil,gammaPipeline=gamma?[device newComputePipelineStateWithFunction:gamma error:error]:nil;id<MTLCommandQueue>queue=[device newCommandQueue];MPSImageLanczosScale *scale=[[MPSImageLanczosScale alloc]initWithDevice:device];
if(grayPipeline&&gammaPipeline&&queue&&scale){entry=[NSMutableDictionary dictionaryWithDictionary:@{@"device":device,@"gray":grayPipeline,@"gamma":gammaPipeline,@"queue":queue,@"scale":scale}];_toolCaptureDevices[key]=entry;}
if(grayPipeline&&gammaPipeline&&queue&&scale){
entry=[NSMutableDictionary dictionaryWithDictionary:@{@"device":device,@"gray":grayPipeline,@"gamma":gammaPipeline,@"queue":queue,@"scale":scale}];_toolCaptureDevices[key]=entry;
id<MTLFunction>function=[library newFunctionWithName:@"dyToolTrackingPreview"];
id<MTLComputePipelineState>pipeline=function?[device newComputePipelineStateWithFunction:function error:nil]:nil;
entry[@"previewPipeline"]=pipeline;
[pipeline release];[function release];
}
[scale release];[queue release];[grayPipeline release];[gammaPipeline release];[gray release];[gamma release];[library release];
}[devices release];if(!entry)return {};
}
@@ -21,8 +30,34 @@
id<MTLBuffer>buffer=entry[@"buffer"];if(!buffer||buffer.length!=width*height*sizeof(float)){buffer=[[device newBufferWithLength:width*height*sizeof(float) options:MTLResourceStorageModeShared]autorelease];if(!buffer)return {};entry[@"buffer"]=buffer;}
id<MTLCommandBuffer>commands=[entry[@"queue"]commandBuffer];id<MTLComputeCommandEncoder>encoder=[commands computeCommandEncoder];[encoder setComputePipelineState:entry[@"gamma"]];[encoder setTexture:source atIndex:0];[encoder setTexture:encoded atIndex:1];[encoder dispatchThreads:MTLSizeMake(source.width,source.height,1)threadsPerThreadgroup:MTLSizeMake(16,16,1)];[encoder endEncoding];
[entry[@"scale"]encodeToCommandBuffer:commands sourceTexture:encoded destinationTexture:bytes];
vector_uint3 info={(unsigned)width,(unsigned)height,(unsigned)(tile.imageOrigin==kFxImageOrigin_BOTTOM_LEFT)};encoder=[commands computeCommandEncoder];[encoder setComputePipelineState:entry[@"gray"]];[encoder setTexture:bytes atIndex:0];[encoder setBuffer:buffer offset:0 atIndex:0];[encoder setBytes:&info length:sizeof(info)atIndex:1];[encoder dispatchThreads:MTLSizeMake(width,height,1)threadsPerThreadgroup:MTLSizeMake(16,16,1)];[encoder endEncoding];[commands commit];[commands waitUntilCompleted];
vector_uint3 info={(unsigned)width,(unsigned)height,(unsigned)(tile.imageOrigin==kFxImageOrigin_BOTTOM_LEFT)};encoder=[commands computeCommandEncoder];[encoder setComputePipelineState:entry[@"gray"]];[encoder setTexture:bytes atIndex:0];[encoder setBuffer:buffer offset:0 atIndex:0];[encoder setBytes:&info length:sizeof(info)atIndex:1];[encoder dispatchThreads:MTLSizeMake(width,height,1)threadsPerThreadgroup:MTLSizeMake(16,16,1)];[encoder endEncoding];
id<MTLBuffer>previewBuffer=nil;
vector_uint3 previewInfo={0,0,info.z};
if(preview&&entry[@"previewPipeline"]){
double scale=std::min({1.,640./width,360./height});
previewInfo.x=std::max(1u,(unsigned)(width*scale));previewInfo.y=std::max(1u,(unsigned)(height*scale));
NSUInteger size=previewInfo.x*previewInfo.y*4;
previewBuffer=entry[@"previewBuffer"];
if(previewBuffer.length!=size){
previewBuffer=[[device newBufferWithLength:size options:MTLResourceStorageModeShared]autorelease];
entry[@"previewBuffer"]=previewBuffer;
}
if(previewBuffer){
encoder=[commands computeCommandEncoder];
[encoder setComputePipelineState:entry[@"previewPipeline"]];[encoder setTexture:bytes atIndex:0];
[encoder setBuffer:previewBuffer offset:0 atIndex:0];[encoder setBytes:&previewInfo length:sizeof(previewInfo) atIndex:1];
[encoder dispatchThreads:MTLSizeMake(previewInfo.x,previewInfo.y,1) threadsPerThreadgroup:MTLSizeMake(16,16,1)];
[encoder endEncoding];
}
}
[commands commit];[commands waitUntilCompleted];
if(commands.status!=MTLCommandBufferStatusCompleted){if(error)*error=commands.error;return {};}
if(previewBuffer){
double pixelAspect=tile.pixelAspect;
double aspect=double(width)/height*(std::isfinite(pixelAspect)&&pixelAspect>0?pixelAspect:1);
*preview=@{@"rgba":[NSData dataWithBytes:previewBuffer.contents length:previewBuffer.length],
@"width":@(previewInfo.x),@"height":@(previewInfo.y),@"aspect":@(aspect)};
}
dy::GrayFrame result{int(width),int(height),std::vector<float>(width*height)};memcpy(result.pixels.data(),buffer.contents,width*height*sizeof(float));return result;
}
}