Search code examples
swiftios13metalcore-imagecikernel

One default MTLLibrary from multiple .metal files (compute kernel and CIKernel implementations)?


In migrating custom Core Image filter kernels to Metal Shading Language, I encountered an error with building the default Metal library (default.metallib):

metallib: error: exactly one input file required

I was under the impression these could be in separate .metal files. Attempting to merge them into one file leads to this error:

Metal library creation failed: Error Domain=MTLLibraryErrorDomain Code=3 "Filters module must contain no vertex/fragment/kernel functions but contains 1 kernel function"

Namespacing to metal and to coreimage prevent the compute kernel from showing up as an available function in the default library.

Found this SO answer, which recommends building separate libraries:

Metal: vertexFunction defined in .metal file becomes nil once setting Compiler and Linker Options for MSL cikernel


Solution

  • You can’t use the default Metal build pipeline for compiling multiple .metal files containing Core Image kernels into one library right now. The linker doesn’t allow merging multiple .air files into one .metallib when setting the -cikernel flag.

    You either have to put all your kernels into one .metal file or use the solution I posted in the answer you linked to above.