68 行
6.8 KiB
C++
68 行
6.8 KiB
C++
// Analysis only. The color renderer continues to use the original linear input.
|
|
// 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 {
|
|
auto result=[self toolGrayFrame:tile preview:nil error:error];
|
|
// The diagnostic/legacy caller expects normalized floats. Actual analysis
|
|
// keeps the GPU-produced bytes through OpenCV, without float expansion.
|
|
if(result.valid()&&result.pixels.empty()){result.pixels.resize(result.bytes.size());for(size_t i=0;i<result.bytes.size();++i)result.pixels[i]=result.bytes[i]/255.f;result.bytes.clear();}
|
|
return result;
|
|
}
|
|
- (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;
|
|
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 {};
|
|
}
|
|
id<MTLDevice>device=entry[@"device"];id<MTLTexture>source=[tile metalTextureForDevice:device];if(!source||source.framebufferOnly||!source.width||!source.height)return {};
|
|
double factor=MIN(1.,MIN(3840./source.width,2160./source.height));NSUInteger width=floor(source.width*factor),height=floor(source.height*factor);if(width<16||height<16||width>3840||height>2160)return {};
|
|
MTLPixelFormat format=source.pixelFormat==MTLPixelFormatRGBA32Float?MTLPixelFormatRGBA32Float:MTLPixelFormatRGBA16Float;
|
|
id<MTLTexture>encoded=entry[@"encoded"],bytes=entry[@"bytes"];
|
|
if(!encoded||encoded.width!=source.width||encoded.height!=source.height||encoded.pixelFormat!=format){MTLTextureDescriptor*d=[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:format width:source.width height:source.height mipmapped:NO];d.storageMode=MTLStorageModePrivate;d.usage=MTLTextureUsageShaderRead|MTLTextureUsageShaderWrite;encoded=[[device newTextureWithDescriptor:d]autorelease];if(!encoded)return {};entry[@"encoded"]=encoded;}
|
|
if(!bytes||bytes.width!=width||bytes.height!=height){MTLTextureDescriptor*d=[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA8Unorm width:width height:height mipmapped:NO];d.storageMode=MTLStorageModePrivate;d.usage=MTLTextureUsageShaderRead|MTLTextureUsageShaderWrite;bytes=[[device newTextureWithDescriptor:d]autorelease];if(!bytes)return {};entry[@"bytes"]=bytes;}
|
|
id<MTLBuffer>buffer=entry[@"buffer"];if(!buffer||buffer.length!=width*height){buffer=[[device newBufferWithLength:width*height 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];
|
|
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;result.width=int(width);result.height=int(height);const auto*begin=static_cast<const uint8_t*>(buffer.contents);result.bytes.assign(begin,begin+width*height);return result;
|
|
}
|
|
}
|