HelloTriangle in Swift: error about depth attachment pixel format

Hi,


I'm trying to port this to Swift. https://developer.apple.com/documentation/metal/hello_triangle


To get it to run I had to add the following line to the renderer class. Any idea why it's not needed in the Objective-C version?


pipelineStateDescriptor.depthAttachmentPixelFormat = metalView.depthStencilPixelFormat


Without it I get an error: -[MTLDebugRenderCommandEncoder validateFramebufferWithRenderPipelineState:]:1232: failed assertion `For depth attachment, the render pipeline's pixelFormat (MTLPixelFormatInvalid) does not match the framebuffer's pixelFormat (MTLPixelFormatDepth32Float).'


Rob

Answered by Graphics and Games Engineer in 302388022

Did you perchance create a new storyboard and drop a MetalKit View from the Object Library in Interface Builder? By default, the MetalKit View from the Object Library sets MTKView.depthStencilPixelformat to MTLPixelFormatDepth32Float.


The HelloTrangle samplw was created using just a ViewController from the ObjectLibrary and setting the view's class to MTKView. If you do that, the MTKView.depthStencilPixelformat is set the MTLPixelFormatInvalid (which is the same as saying, I don't need a depth buffer). By default, MTLRenderPipelineStateDescriptor.depthAttachementPixelFormat is also MTLPixelFormatInvalid, so it matches already.


Really, you should always do what you've done and set the depthAttachmentPixelFormat in code becasue it doesn't hurt to make sure your pipeline matches, but in this intro sample we're just keeping it simple.

Accepted Answer

Did you perchance create a new storyboard and drop a MetalKit View from the Object Library in Interface Builder? By default, the MetalKit View from the Object Library sets MTKView.depthStencilPixelformat to MTLPixelFormatDepth32Float.


The HelloTrangle samplw was created using just a ViewController from the ObjectLibrary and setting the view's class to MTKView. If you do that, the MTKView.depthStencilPixelformat is set the MTLPixelFormatInvalid (which is the same as saying, I don't need a depth buffer). By default, MTLRenderPipelineStateDescriptor.depthAttachementPixelFormat is also MTLPixelFormatInvalid, so it matches already.


Really, you should always do what you've done and set the depthAttachmentPixelFormat in code becasue it doesn't hurt to make sure your pipeline matches, but in this intro sample we're just keeping it simple.

Yup, that's exactly it. Thanks!

Please refer this

Toggle Metal API Validation via your Xcode Scheme: Scheme > Edit Scheme... > Run > Diagnostics > Metal API Validation. It's a checkbox, so the possible options are Enabled or Disabled. Disabling sets the key enableGPUValidationMode = 1 in your .xcscheme file. After disabling, Xcode no longer logs the "Metal API Validation Enabled" log message. Note: In Xcode 11 and below, the option appears in the "Options" tab of the Scheme Editor (instead of the "Diagnostics" tab).

HelloTriangle in Swift: error about depth attachment pixel format
 
 
Q