SwiftUI [[stichable]] metal shader & CIFilter written in metal extern"C" can't work at the same time

In my project, I used two metal shaders in two ways.

One is link to SwiftUI's modifier .colorEffect(ShaderLibrary.myShader()), which metal shader marked as [[stichable]].

Another one is a custom CIFilter, which kernel been written in external "C" closure.

Because custom CIFilter must add build rules so Xcode can compile it, so I added -fcikernel to Metal Compiler and -cikernel to Metal Linker from Build Settings, just like Apple's document told me that.

But the result is weird, if I add rules, custom CIFilter works, [[stichable]] shader doesn't work. if I delete rules, and comment out code of CIFilter(for avoiding warning), [[stichable]] shader works, but now I can't use my custom CIFilter.

Actually, once these two shaders works well in my project, but when I updated Xcode from 15 to 16, it became weird, the 2 shaders can't exist at same time. Even though I go back to Xcode 15, I can't save them.

I have no idea, please help, thank you.

XCode 16 / iOS 18 on iPhone 14 Pro

Answered by Ceylo in 804738022

I expect what you're seeing is your flags being applied to all shaders while they should only be applied to the Core Image ones.

https://developer.apple.com/videos/play/wwdc2020/10021/ describes a way to apply the Core Image flags to Core Image shaders only, through custom build rules. Alternatively you can add your CI shaders in a distinct target.

Hey @Tinn_Vision,

Please provide a link to a focused sample project that reproduces the issue you are seeing, and then I will take a look!

Best regards,

Greg

Accepted Answer

I expect what you're seeing is your flags being applied to all shaders while they should only be applied to the Core Image ones.

https://developer.apple.com/videos/play/wwdc2020/10021/ describes a way to apply the Core Image flags to Core Image shaders only, through custom build rules. Alternatively you can add your CI shaders in a distinct target.

SwiftUI [[stichable]] metal shader & CIFilter written in metal extern"C" can't work at the same time
 
 
Q