macosmetalmetalkit

How to check a MTKView supports display-P3 pixel formats in macOS?


I have a macOS app built in SwiftUI, where I have a custom view with an MTKView. I set the pixel format to be:

let metalView = MTKView()
metalView.colorPixelFormat = .bgra10_xr_srgb

This works on an M1 Mac mini running macOS Sonoma, and Xcode 15.

However, when I run the same code on a 2017 Intel MacBook Pro, on macOS Ventura (same Xcode 15), I get a runtime error and the view fails to initialize.

The log:

unsupported extended range format
(
    0   CoreFoundation                      0x00007ff80611218a __exceptionPreprocess + 242
    1   libobjc.A.dylib                     0x00007ff805c3842b objc_exception_throw + 48
    2   CoreFoundation                      0x00007ff806111ff0 +[NSException raise:format:] + 214
    3   QuartzCore                          0x00007ff80d94e5f5 -[CAMetalLayer setPixelFormat:] + 339
    4   MetalKit                            0x00007ff8173dd17a -[MTKView setColorPixelFormat:atIndex:] + 112
...

So it says the extended range pixel format (XR) is not supported. But how can I check that's the case?

The bgra10_xr_srgb documentation doesn't mention anything about crashes. It says that if the device doesn't support the wider range, the color will get clamped, that's all.


Solution

  • XR formats are not available on anything other than Apple3 GPU family and higher.

    If you go to Metal Feature Set Table and scroll to Extended range and wide color pixel formats, you will see that it's only supported on Apple3 and above.

    So to check whether a device supports XR pixel formats, you would query MTLDevice by using supportsFamily:MTLGPUFamilyApple3