Search code examples
macosmetal

How do I change the pixelFormat in Metal?


If I try anyting other than bgra8Unorm, it will crash, saying,

-[MTLDebugRenderCommandEncoder validateFramebufferWithRenderPipelineState:]:1192: failed assertion `For color attachment 0, the render pipeline's pixelFormat (MTLPixelFormatBGRA8Unorm_sRGB) does not match the framebuffer's pixelFormat (MTLPixelFormatBGRA8Unorm).'

How do I change the framebuffer's pixelFormat then? I want to be able to do this:

PipelineDescriptor.colorAttachments[0].pixelFormat = .bgra8Unorm_srgb

Instead of this

PipelineDescriptor.colorAttachments[0].pixelFormat = .bgra8Unorm

Again, this is an attempt to fix this stupid bug.


Solution

  • You need the MetalView defined here:

    var MetalView: MTKView {
        return view as! MTKView
    }
    

    Then in viewDidLoad:

    MetalView.colorPixelFormat = .bgra8Unorm_srgb
    

    Then I can do this:

    PipelineDescriptor.colorAttachments[0].pixelFormat = .bgra8Unorm_srgb
    

    I figured it out myself yay!