【发布时间】:2018-12-12 20:18:07
【问题描述】:
我正在尝试使用 MPSImageConvolution 进行一些图像过滤并不断收到错误消息:“wt[0] 的索引 1 处缺少缓冲区绑定”。 当使用与 MPSImageLaplacian 相同的代码时,它工作正常。
这是我的代码:
let img = UIImage(named: "some-image")!
// convert to single channel grayscale and scale to half-size
let image = toGrayscale(cgImage: img.cgImage!, scale: 2)
let cmdQ: MTLCommandQueue! = device.makeCommandQueue()
let commandBuffer = cmdQ.makeCommandBuffer()!
let textureLoader = MTKTextureLoader(device: device)
let options: [MTKTextureLoader.Option : Any]? = nil // [ MTKTextureLoader.Option.SRGB : NSNumber(value: false) ]
let srcTex = try! textureLoader.newTexture(cgImage: image.cgImage!, options: options)
let lapKernel: [Float] =
[
0.0, 1.0, 0.0,
1.0, -4.0, 1.0,
0.0, 1.0, 0.0
];
let unsafeArray: UnsafePointer<Float> = UnsafePointer<Float>(lapKernel)
let desc = MTLTextureDescriptor.texture2DDescriptor(pixelFormat: srcTex.pixelFormat,
width: srcTex.width,
height: srcTex.height,
mipmapped: false)
desc.usage.formUnion(.shaderWrite)
let lapTex = device.makeTexture(descriptor: desc)
// let lapConv = MPSImageLaplacian(device: device) <-- Using this works fine
let lapConv = MPSImageConvolution(device: device, kernelWidth: 3, kernelHeight: 3, weights: unsafeArray)
lapConv.encode(commandBuffer: commandBuffer, sourceTexture: srcTex, destinationTexture: lapTex!)
上面代码sn-p最后一行crash,报如下错误:
validateComputeFunctionArguments:811: 断言失败`Compute Function(k_1x3_R_1x_5y_f): wt[0] 的索引 1 处缺少缓冲区绑定。'
任何想法可能是什么问题?除此之外,我还使用中值滤波器和阈值滤波器,一切都很好...... 谢谢!
【问题讨论】:
标签: ios swift swift4 gpu metal