使用 MPSImageConvolution 时出现异常
Exception when using MPSImageConvolution
我正在尝试使用 MPSImageConvolution 进行一些图像过滤并不断收到错误消息:"missing buffer binding at index 1 for wt[0]"。
当使用与 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!)
上述代码片段的最后一行因以下错误而崩溃:
validateComputeFunctionArguments:811: failed assertion `Compute Function(k_1x3_R_1x_5y_f): missing buffer binding at index 1 for wt[0].'
任何想法可能是什么问题?除此之外,我还使用中值滤波器和阈值滤波器,一切都很好......
谢谢!
我有同样的断言异常:
Compute Function(k_1x3_R_1x_5y_f): missing buffer binding at index 1 for wt[0].
... 使用 MPSImageConvolution
时。我修改了内核,使矩阵中的零值变为 0.0001
。在您的情况下,您可以将 lapKernel
更改为:
let lapKernel: [Float] =
[
0.0001, 1.0, 0.0001,
1.0, -4.0, 1.0,
0.0001, 1.0, 0.0001
]
...我发现它阻止了断言异常,希望不会显着改变图像过滤器的功能。
顺便说一句,您示例中 lapKernel
定义之后的 ;
是多余的(如果我说的很明显,请道歉)
我正在尝试使用 MPSImageConvolution 进行一些图像过滤并不断收到错误消息:"missing buffer binding at index 1 for wt[0]"。 当使用与 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!)
上述代码片段的最后一行因以下错误而崩溃:
validateComputeFunctionArguments:811: failed assertion `Compute Function(k_1x3_R_1x_5y_f): missing buffer binding at index 1 for wt[0].'
任何想法可能是什么问题?除此之外,我还使用中值滤波器和阈值滤波器,一切都很好...... 谢谢!
我有同样的断言异常:
Compute Function(k_1x3_R_1x_5y_f): missing buffer binding at index 1 for wt[0].
... 使用 MPSImageConvolution
时。我修改了内核,使矩阵中的零值变为 0.0001
。在您的情况下,您可以将 lapKernel
更改为:
let lapKernel: [Float] =
[
0.0001, 1.0, 0.0001,
1.0, -4.0, 1.0,
0.0001, 1.0, 0.0001
]
...我发现它阻止了断言异常,希望不会显着改变图像过滤器的功能。
顺便说一句,您示例中 lapKernel
定义之后的 ;
是多余的(如果我说的很明显,请道歉)