Metal debug log in Swift Package

My goal is to print a debug message from a shader. I follow the guide that orders to set -fmetal-enable-logging metal compiler flag and following environment variables:

MTL_LOG_LEVEL=MTLLogLevelDebug
MTL_LOG_BUFFER_SIZE=2048
MTL_LOG_TO_STDERR=1

However there's an issue with the guide, it's only covers Xcode project setup, however I'm working on a Swift Package. It has a Metal-only target that's included into main target like this:

targets: [
    // A separate target for shaders.
    .target(
        name: "MetalShaders",
        resources: [
            .process("Metal")
        ],
        plugins: [
            // https://github.com/schwa/MetalCompilerPlugin
            .plugin(name: "MetalCompilerPlugin", package: "MetalCompilerPlugin")
        ]
    ),
    // Main target
    .target(
        name: "MegApp",
        dependencies: ["MetalShaders"]
    ),
    .testTarget(
        name: "MegAppTests",
        dependencies: [
            "MegApp",
            "MetalShaders",
      ]
]

So to apply compiler flag I use MetalCompilerPlugin which emits debug.metallib, it also allows to define DEBUG macro for shaders. This code compiles:

#ifdef DEBUG
    logger.log_error("Hello There!");
    os_log_default.log_debug("Hello thread: %d", gid);
    // this proves that code exectutes
    result.flag = true;
#endif

Environment is set via .xctestplan and valideted to work with ProcessInfo. However, nothing is printed to Xcode console nor to Console app.

In attempt to fix it I'm trying to setup a MTLLogState, however the makeLogState(descriptor:) fails with error:

if #available(iOS 18.0, *) {
    let logDescriptor = MTLLogStateDescriptor()
    logDescriptor.level = .debug
    logDescriptor.bufferSize = 2048
    // Error Domain=MTLLogStateErrorDomain Code=2 "Cannot create residency set for MTLLogState: (null)" UserInfo={NSLocalizedDescription=Cannot create residency set for MTLLogState: (null)}
    let logState = try! device.makeLogState(descriptor: logDescriptor)
    commandBufferDescriptor.logState = logState
}

Some LLMs suggested that this is connected with Simulator, and truly, I run the tests on simulator. However tests don't want to run on iPhone... I found solution running them on My Mac (Mac Catalyst). Surprisingly descriptor log works there, even without MTLLogState. But the Simulator behaviour seems like a bug...

Answered by Graphics and Games Engineer in 871786022

Hi Kelin,

Shader logging requires support for GPUFamilyApple6, which is currently not supported by the Simulator.

You can verify the availability of different Metal features by GPU family by consulting the Metal feature set tables online here: https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf

Accepted Answer

Hi Kelin,

Shader logging requires support for GPUFamilyApple6, which is currently not supported by the Simulator.

You can verify the availability of different Metal features by GPU family by consulting the Metal feature set tables online here: https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf

Metal debug log in Swift Package
 
 
Q