【问题标题】:Save MTLTexture data locally in swift快速在本地保存 MTLTexture 数据
【发布时间】:2018-09-03 09:16:33
【问题描述】:

MTLTexture 是一种内存分配,用于存储 GPU 可访问的格式化图像数据。 我很好奇是否有任何方法可以在本地保存 MTLTexture 数据?

如果是这样,在本地保存 MTLTexture 数据最合适的方法是什么?

【问题讨论】:

    标签: swift metal metalkit


    【解决方案1】:

    您可以使用getBytes 函数从纹理中获取图像字节数据:

    getBytes(_:bytesPerRow:bytesPerImage:from:mipmapLevel:slice:)
    

    从纹理的存储分配中复制一块像素 在指定地址切入系统内存

    https://developer.apple.com/documentation/metal/mtltexture/1516318-getbytes

    您可以使用字节创建Data 对象:

    Data.init(bytes: UnsafeRawPointer, count: Int)
    

    或者您可以将此数据与CGCreateImage 一起使用来创建图像。完整示例:

    https://github.com/metal-by-example/sample-code/blob/master/objc/14-ImageProcessing/ImageProcessing/UIImage%2BMBETextureUtilities.m

    【讨论】:

    • 谢谢oskar。我会过去的
    • 您是希望将其保存到文件中,还是保留对后备存储经过金属优化的图像的引用?如果你在做前者,你可以按照 Oskar 说的做,然后使用Data.write(to: url)。如果你正在做后者,你应该使用kCVPixelBufferMetalCompatibilityKey 创建的CVPixelBuffer,然后使用CVMetalTextureCacheCreateTextureFromImage 来获取绑定到该像素缓冲区的底层内存的纹理。如果需要,我可以写一个答案。
    猜你喜欢
    • 2023-03-12
    • 2015-11-24
    • 2019-03-14
    • 2015-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 2023-03-27
    相关资源
    最近更新 更多