Search code examples
iosswiftswift4gpumetal

Exception when using MPSImageConvolution


I am trying to use MPSImageConvolution todo some image filtering and keep getting an error: "missing buffer binding at index 1 for wt[0]". When using the same code with MPSImageLaplacian it is working OK.

This is my code:

    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!)

The last line of the above code snippet crashes with the following error:

validateComputeFunctionArguments:811: failed assertion `Compute Function(k_1x3_R_1x_5y_f): missing buffer binding at index 1 for wt[0].'

Any ideas what could be the issue? besides that I am also using the median filter and threshold filter and all works great... Thx!


Solution

  • I had the same assertion exception:

    Compute Function(k_1x3_R_1x_5y_f): missing buffer binding at index 1 for wt[0].

    ... when using MPSImageConvolution. I modified my kernel so that zero values in the matrix became 0.0001. In your case you could change lapKernel to be:

       let lapKernel: [Float] =
        [
            0.0001, 1.0, 0.0001,
            1.0, -4.0, 1.0,
            0.0001, 1.0, 0.0001
        ]
    

    ... which I found prevented the assertion exception and hopefully won't significantly alter the functionality of your image filter.

    BTW the ; after lapKernel definition in your example is superfluous (apologies if I'm stating the obvious)