ios [[可选]]金属核心映像CIKernel失败

roejwanj  于 12个月前  发布在  iOS
关注(0)|答案(1)|浏览(92)

它看起来像[[stitchable]]金属核心图像内核无法在编译时链接,由于核心图像sampler.coord()sampler.sample(coord)符号未找到,或者,如果我添加其他金属链接器标志-fcikernel(这不是可扩展内核所需的),则无法在运行时加载。
这里是github上测试项目的完整链接。请随意下载并试用。
我发现主要的罪魁祸首是链接器无法解析的Metal文件中的这一行:

float2 srcCoord = inputImage.coord();

字符串
代码摘录:

import CoreImage

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])
}


}
下面是Metal文件:

#include <CoreImage/CoreImage.h> // includes CIKernelMetalLib.h

 using namespace metal;

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

   return color;
}


下面是用法:

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)")
}

}


输出:

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.

piv4azn7

piv4azn71#

虽然[[ stitchable ]] CI内核的构建过程要容易得多,但您仍然需要告诉Metal链接器链接Core Image。这在WWDC21 session about the topic中提到。
为此,您需要将-framework CoreImage标志添加到 Other Metal Linker Flags 中,如下所示。请注意,您需要在两个单独的行中添加它,否则它将无法工作。


的数据

相关问题