AFAIK, I can't pass compiler options to the Metal compiler from Package.swift, so I can't enable debug symbols for metal and use the shader debugger/profiler on shaders within a Swift Package. So currently, whenever I want to debug/profile my shaders, I move all the relevant code out of the package.
How could this be improved? Xcode seems to pass debug flags to SPM for Swift or ObjC files. I suppose the same could be done for Metal?
I have the same problem in Xcode 12. I gave up and use .frameworks instead. In Xcode 13 there are new features for shader compiling and debugging. Now the compiler can generate a .metallibsym file (see here at about 16:45). I have not tried to get this work with Swift Package Manager yet. Has anyone else tried?
We encountered this recently as well and I asked a Swift Package Manager engineer about it during WWDC last week. They confirmed it is not supported.
Our work around is to create a runscript that runs just after the file compilation phase of our project. We do this only when we detect a package that contains metal files has been dragged into our workspace as a local package, thus replacing our production, remote package.
The script finds the local reference path in our XCWorkspace file, finds the metal files in the package at the referenced path and then compiles them like this (I left out the specifics of finding the metal files... this is just the compile and link step for metal):
compile_air() {
export MTL_ENABLE_DEBUG_INFO=INCLUDE_SOURCE
$(xcrun --sdk iphoneos -f metal) -c -target air64-apple-ios13.0 -gline-tables-only -MO \
-I${OUTPUT_DIR}/include \
-F${OUTPUT_DIR} \
-isysroot $(xcrun --show-sdk-path --sdk iphoneos) \
-ffast-math \
-o $2 \
$1
}
//do the compile_air for each metal file
compile_air $RENDERER_METAL $RENDERER_AIR
echo "#### LINKING AIR"
$(xcrun --sdk iphoneos -f metal) \
-target air64-apple-ios13.0 \
-MO \
-o ${OUTPUT_DIR}/MyPackage.bundle/default.metallib \
$RENDERER_AIR //can add more air files here if you have them
This overwrites the .metallib generated by SwiftPM with one that can be debugged.
Edit: Also note, the OUTPUT_DIR is passed to the script and is just the Xcode BUILT_PRODUCTS_DIR
Yes, I tried that as well. When trying to profile the metal shader I get this error:
Task Failed: Compiling source "Shader.metal" for target "1".
metal: error: no such file or directory: '/private/var/folders/7k/y3sqtzxd0ldby388_vfhcffm0000gn/T/1632836607.447792/dfae43e651e5ab2ee08bf79c7c061e29f25a38483f0b185da3757a5486f15180/source/1/Applications/Xcode_13_RC.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS15.0.sdk
/private/var/folders/7k/y3sqtzxd0ldby388_vfhcffm0000gn/T/1632836607.447792/dfae43e651e5ab2ee08bf79c7c061e29f25a38483f0b185da3757a5486f15180/source/1/Users/user/project/Shaders/Shader.metal'
metal: error: no input files
It seems like the path to the metal files is prefixed with the BUILT_PRODUCTS_DIR path. Where do you compile the air files to?
For the record, I have not been able to figure out how to recompile the metal files since Xcode 13 came out. Currently, our devs are just dragging their package repos into the top level of the workspace, then dragging the .metal files to our main project as a reference (not copying the actual file). This allows Xcode to pick it up properly. They have to make sure not to commit those changes though, so not ideal, but it is working.
There's this swift package plugin happens, which is quite useful to get things work (i.e. to compile debug.metallib library with debug symbols included). MetalCompilerPlugin - Swift Package Registry (works only with Xcode/xcodebuild)
Disclaimer: I use it with a slightly different approach (to enable shaders logging on a pure metal target) and thus I was unable to make it work to set breakpoint on a shader source.