// 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 { @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();iddevice=nil;for(idcandidate in devices)if(candidate.registryID==tile.deviceRegistryID)device=candidate; if(device){idlibrary=[device newLibraryWithURL:[[NSBundle bundleForClass:self.class]URLForResource:@"default" withExtension:@"metallib"]error:error];idgray=[library newFunctionWithName:@"dyToolCapture"],gamma=[library newFunctionWithName:@"dyToolEncodeGamma22"];idgrayPipeline=gray?[device newComputePipelineStateWithFunction:gray error:error]:nil,gammaPipeline=gamma?[device newComputePipelineStateWithFunction:gamma error:error]:nil;idqueue=[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;} [scale release];[queue release];[grayPipeline release];[gammaPipeline release];[gray release];[gamma release];[library release]; }[devices release];if(!entry)return {}; } iddevice=entry[@"device"];idsource=[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; idencoded=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;} idbuffer=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;} idcommands=[entry[@"queue"]commandBuffer];idencoder=[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]; if(commands.status!=MTLCommandBufferStatusCompleted){if(error)*error=commands.error;return {};} dy::GrayFrame result{int(width),int(height),std::vector(width*height)};memcpy(result.pixels.data(),buffer.contents,width*height*sizeof(float));return result; } }