Getting wide color with Core Image and MTKView (iOS)

I have a MTKView and I want to display a CIImage in it using wide color. I'm deriving the CIImage from a PNG image tagged with the Display P3 color space. I have set the MTKView's colorPixelFormat property to either MTLPixelFormatBGR10_XR or MTLPixelFormatBGR10_XR_sRGB, and I've set my CIContext's workingColorSpace to Display P3 and workingColorFormat to kCIFormatRGBAh.


In my MTKView delegate's drawRect method I am drawing the CIImage like this:

[ciContext render:image toMTLTexture:currentDrawable.texture commandBuffer:commandBuffer bounds:drawableBounds colorSpace:deviceColorSpace]


where deviceColorSpace is created using one of the following:

CGColorSpaceCreateDeviceRGB()

CGColorSpaceCreateWithName(kCGColorSpaceExtendedSRGB)

CGColorSpaceCreateWithName(kCGColorSpaceDisplayP3)

CGColorSpaceCreateWithName(kCGColorSpaceSRGB)


However, no matter which combination of colorSpace and colorPixelFormat I use, the image does not render correclty, with the image's colors being either compressed or cropped into a smaller gamut.


If I render the CIImage to a CGImage using the Display P3 color space and view the result either in a UIImageView or in the debugger, it appears correctly, so I know that neither the CIImage nor the CIContext are incorrect.


Has anyone gotten wide color with Core Image and MTKView to work properly, and if so, what color space and pixel format did you use?

Replies

I would like to know also.

Although this issue is rather old, I hope this solution still helps. Core Image doesn't seem to support either MTLPixelFormatBGR10_XR or MTLPixelFormatBGR10_XR_sRGB. Instead what seems to be working is setting the MTKView's colorPixelFormat to MTLPixelFormatRGBA16Float, the CIContext's workingColorSpace to either DisplayP3 or ExtendedSRGB (depending on what values you expect in your kernels) and then call -[CIContext render:toMTLTexture:commandBuffer:bounds:colorSpace] with an Extended SRGB color space.

Thank you! Your answer seems to make sense. However even after trying those settings, I notice that renditions are still a little 'pale' by comparison with AVCapturePreviewLayer. Feels like gamma is slightly off or something. Still your settings are the best solution I found!