【问题标题】:Metal kernel function not found未找到金属核函数
【发布时间】:2021-07-13 22:31:46
【问题描述】:

我正在尝试将 Metal Performance Shaders 与 Core Image 一起使用。

不幸的是,过滤器代码在 Objective C 中,我正在使用桥接器使其与 Swift 项目一起工作。

My-Project-Bridging-Header.h

#import "MyCustomFilter.h"

MyCustomFilter.h

#import <CoreImage/CoreImage.h>

@interface MyCustomFilter : CIFilter
......
@end

MyCustomFilter.m

#import "MyCustomFilter.h"
#import "MyCustomFilterKernel.h"

.......

MyCustomFilterKernel.h

#import <CoreImage/CoreImage.h>

@interface MyCustomFilterKernel : CIImageProcessorKernel

@end

MyCustomFilterKernel.m

#import "FTWaveformScopeKernel.h"
#import "FTWaveformScopeFilter.h"

#import <Metal/Metal.h>

    ...

+ (void) initialize {
 id<MTLFunction> customFunction = [defaultLibrary newFunctionWithName:@"custom_Function"];
 if (customFunction) {
  NSError* error;
  kCustomComputePipelineState = [kDevice newComputePipelineStateWithFunction: customFunction error:&error];
  if (error) {
    NSLog(@"error loading kernel function (custom_Function): %@", error);
  } 
 } else {
     NSLog(@"kernel function (custom_Function) not found");
 }

CustomFunction.metal

    #include <metal_stdlib>
    using namespace metal;
   
    ......

    kernel void
    custom_Function(texture2d<float, access::sample>        inTexture       [[texture(0)]],
                        texture2d<float, access::write>         outTexture      [[texture(1)]],
                        volatile device atomic_uint*            columnDataRed   [[buffer(0)]],
                        volatile device atomic_uint*            columnDataGreen [[buffer(1)]],
                        volatile device atomic_uint*            columnDataBlue  [[buffer(2)]],
                        sampler                                 wrapSampler     [[sampler(0)]],
                        uint2                                   gid                       
                        [[thread_position_in_grid]])
{

........

}

我得到的错误是(金属视图为空白/透明):

kernel function (custom_Function) not found

我也做了:

我正在处理 Xcode 12.x。以上代码编译运行。

更新

在第一次回复之后,我遵循了新的 Xcode 12 及更高版本的设置: https://developer.apple.com/videos/play/wwdc2020/10021/

  1. CustomFunction.metal 更改为CustomFunction.ci.metal

  2. 添加了第一条规则:

  3. 增加了第三条规则:

现在项目无法编译:

LLVM ERROR: Error opening 

    /Users/xxxxxx/Library/Developer/Xcode/DerivedData/xxxxxx-ggaphqixkeecbkewrntvhssdgnac/Build/Intermediates.noindex/xxxxxx.build/Debug-iphoneos/xxxxxx.build/DerivedSources/CustomFunction.ci.air': No such file or directory!
    Command RuleScriptExecution failed with a nonzero exit code


RuleScriptExecution /Users/xxxx/Library/Developer/Xcode/DerivedData/xxxx-ggaphqixkeecbkewrntvhssdgnac/Build/Products/Debug-iphoneos/xxxx\ xxxx.app/CustomFunction.ci.metallib /Users/xxxx/Library/Developer/Xcode/DerivedData/xxxx-xxxx-ggaphqixkeecbkewrntvhssdgnac/Build/Intermediates.noindex/xxxx-xxxx.build/Debug-iphoneos/xxxx-xxxx.build/DerivedSources/CustomFunction.ci.air normal undefined_arch (in target 'xxxx-xxxx' from project 'xxxx-xxxx')
    cd /Users/xxxx/Desktop/xxxx-xxxx\ CustomFunction
    /bin/sh -c xcrun\ metallib\ -cikernel\ \"\$\{INPUT_FILE_PATH\}\"\ -o\ \"\$\{SCRIPT_OUTPUT_FILE_0\}\"'
'

这是文本中的两行:

xcrun metallib -cikernel "${INPUT_FILE_PATH}" -o "${SCRIPT_OUTPUT_FILE_0}"

xcrun metal -c -I $MTL_HEADER_SEARCH_PATHS -fcikernel  "${INPUT_FILE_PATH}" -o "${SCRIPT_OUPUT_FILE_0}"

由于我必须在屏幕截图中输入这些内容,所以也许我在解释时犯了错误? -fcikernel后面有双空格?

【问题讨论】:

  • 给定示例代码,我想内核没有嵌套在命名空间中,对吗?如果是,您需要使用该命名空间限定名称(例如,如果您有 namespace Custom { kernel Your_name ... },则需要在 newFunctionWithName: 中说“Custom::[Your_name]”
  • 试过了,但没有运气。我错过了#include 吗?不知何故,在运行时没有看到自定义内核?
  • 也许这个内核是为 MacOS 编写的?不适用于 iOS?
  • 您能在没有-I $MTL_HEADER_SEARCH_PATHS(并且没有双空格)的情况下再试一次吗?
  • 我得到同样的错误。 '没有这样的文件或目录!'

标签: ios xcode kernel metal metalkit


【解决方案1】:

Metal 工具链在 Xcode 12 中发生了变化。上述链接器标志不再起作用,因为默认工具链不再使用 metallib 链接器。

请查看David's talk from WWDC 2020,了解如何正确设置构建系统以编译自定义 Core Image 内核。

【讨论】:

  • 更新了 OP。仍然有问题。
猜你喜欢
  • 2017-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-18
  • 1970-01-01
  • 1970-01-01
  • 2021-03-04
  • 1970-01-01
相关资源
最近更新 更多