【问题标题】:access particular set of pixels MTLTexture in metal访问金属中的特定像素集 MTLTexture
【发布时间】:2018-12-08 12:28:38
【问题描述】:

我使用UIImage 数据创建了一个MTLTexture,如下所示。

var texture = metalView.currentDrawable!.texture
let uiImg = createImageFromCurrentDrawable()
            
guard let device = metalView.device else {
    fatalError("Device not created. Run on a physical device")
}
let textureLoader = MTKTextureLoader(device: device)
let imageData: NSData = UIImagePNGRepresentation(uiImg)! as NSData
texture = try! textureLoader.newTexture(data: imageData as Data, options: [MTKTextureLoader.Option.allocateMipmaps: (false as NSNumber)])
            

我需要做的是更改MTLTexture 中的像素颜色。不是所有的人。那么,是否可以访问MTLtexture 中的特定像素集并在 Metal 中写入?

【问题讨论】:

    标签: swift metal metalkit mtlbuffer


    【解决方案1】:

    是的,看看MTLTexture documentation 就会告诉你。您可以使用getBytes() 方法之一将纹理数据区域复制到缓冲区,并使用replace() 方法之一将纹理像素区域替换为您提供的缓冲区中的数据。

    【讨论】:

    • 知道了。它起作用了。感谢 Ken.Btw 的提示,当我替换纹理中的部分像素时,颜色值会从该特定像素集的实际值改变。你知道任何可能的吗这种行为的原因
    • 不确定。取决于您正在做什么以及您提供的数据的像素格式。可能最好用这些细节、你期望什么以及你实际得到什么来提出一个新问题。
    • 谢谢 ken。我最近想通了。是的,确实是因为像素格式。而不是 pixelFormat: .bgra8Unorm in MTLTextureDescriptor 我不小心添加了 .argb8Unorm 这会导致意外行为。
    猜你喜欢
    • 1970-01-01
    • 2017-05-28
    • 1970-01-01
    • 1970-01-01
    • 2015-06-07
    • 2016-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多