如何在Metal中将MTLTexture转换为CVMetalTextureRef或CMSampleBufferRef?

时间:2015-07-21 13:05:18

标签: metal

我在将MTLTexture转换为CVMetalTextureRef或CVPixelBufferRef时遇到问题,我的代码是这样的:

CVPixelBufferLockBaseAddress(_screenPixelBuffer, 0);//必须锁定内存

    id <MTLTexture> outputTexture = drawable.texture;

    MTLRegion region = MTLRegionMake2D(0, 0, drawable.layer.drawableSize.width, drawable.layer.drawableSize.height);

    int bytesPerPixel = 4;
    int bytesPerRow = bytesPerPixel * drawable.layer.drawableSize.width;

    NSLog(@"drawable.layer.drawableSize.width = %f, drawable.layer.drawableSize.height = %f", drawable.layer.drawableSize.width, drawable.layer.drawableSize.height);
    NSLog(@"textureType = %lu, width = %lu, height = %lu, depth = %lu, arrayLength = %lu, mipmapLevelCount = %lu, sampleCount = %lu", outputTexture.textureType, outputTexture.width, outputTexture.height, outputTexture.depth, outputTexture.arrayLength, outputTexture.mipmapLevelCount, outputTexture.sampleCount);

    void *tmpBuffer = CVPixelBufferGetBaseAddress(_screenPixelBuffer);

    [outputTexture getBytes:tmpBuffer bytesPerRow:bytesPerRow fromRegion:region mipmapLevel:0];


    [_iv setImage:[self imageFromPixelBuffer:_screenPixelBuffer]];

    CVPixelBufferUnlockBaseAddress(_screenPixelBuffer, 0);//解锁内存

但是UIImageview显示的图像不对。 http://redis.io/topics/memory-optimization#using-hashes-to-abstract-a-very-memory-efficient-plain-key-value-store-on-top-of-redis

0 个答案:

没有答案