Compiling multiple .metal files into one .metallib

I’m currently writing some custom Core Image filters using Metal. For the sake of structure I want to put the different kernels into different

.metal
files with some common includes like you would do with “normal” source files.

However, when the

metallib
tool bundles the different
.air
files created by the Metal compiler into one
.metallib
file, only the kernel functions defined in the first input
.air
file given to
metallib
are visible. Functions from the other
.air
files don’t seem to be included. What’s the reason for this?


I thought (as is the default compilation behavior for Metal files) all Metal sources get compiled into one library that is then used by every custom

CIFilter
class to instantiate their internal
CIKernel
with the function they need.

I now ended up compiling a

.metallib
file for each custom filter with custom build rules and copying all of them them into my framework within a custom build phase. This don’t seem to be the intended way…

Replies

Each kernel ust have a unique name for this to work even if defined in separate .metal files,. Is that the case?

Yes, definitely. I guess otherwise the linker would also complain about duplicate symbols.

> compiling a

.metallib
file for each custom filter with custom build rules and copying all of them them into my framework within a custom build phase.


How do you do that? I can't seem to figure it out. I get lost in Xcode project settings.

I recently responded to a similar question on StackOverflow showing the process: https://stackoverflow.com/a/54921611/541016