[[stitchable]] Metal core image CIKernel fails to load

It looks like [[stitchable]] Metal Core Image kernels fail to get added in the default metal library. Here is my code:


class FilterTwo: CIFilter {
var inputImage: CIImage?
var inputParam: Float = 0.0

static var kernel: CIKernel = { () -> CIKernel in
      let url = Bundle.main.url(forResource: "default",
                            withExtension: "metallib")!
      let data = try! Data(contentsOf: url)

      let kernelNames = CIKernel.kernelNames(fromMetalLibraryData: data)
      NSLog("Kernels \(kernelNames)")
     return try! CIKernel(functionName: "secondFilter", fromMetalLibraryData: data) //<-- This fails!
}()

  override var outputImage : CIImage? {
    guard let inputImage = inputImage else {
        return nil
    }
    
      return FilterTwo.kernel.apply(extent: inputImage.extent, roiCallback: { (index, rect) in
        return rect }, arguments: [inputImage])
  }
 }

Here is the Metal code:


 using namespace metal;

 [[ stitchable ]] half4 secondFilter (coreimage::sampler inputImage, coreimage::destination dest)
{
   float2 srcCoord = inputImage.coord();
   half4 color =  half4(inputImage.sample(srcCoord));

   return color;
}

And here is the usage:

       class ViewController: UIViewController {

   override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    
    let filter = FilterTwo()
    filter.inputImage = CIImage(color: CIColor.red)
    let outputImage = filter.outputImage!
    
    NSLog("Output \(outputImage)")
}

}

And the output:

   StitchableKernelsTesting/FilterTwo.swift:15: Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=CIKernel Code=1 "(null)" UserInfo={CINonLocalizedDescriptionKey=Function does not exist in library data. …•∆}
   Kernels []

   reflect Function 'secondFilter' does not exist.

Accepted Reply

The -framework CoreImage flag needs to be set in two lines in Xcode, otherwise it will mask the space between the words:

Replies

Here is the link to download the test project from Github to reproduce the issue. Feel free to point out what is wrong and needs to be fixed.

The -framework CoreImage flag needs to be set in two lines in Xcode, otherwise it will mask the space between the words: