StoryBoardでMTKViewを追加するとdepthAttachementPixelFormatが原因でクラッシュする!
StoryBoardでMTKViewを追加してシェーダのコーディングをしていたんですが、なぜかsetRenderPipelineState呼んだときにpixcelFormatが一致しないと怒られました
エラーのログはこんな感じ。
'Framebuffer With Render Pipeline State Validation
For depth attachment, the render pipeline's pixelFormat (MTLPixelFormatInvalid) does not match the framebuffer's pixelFormat (MTLPixelFormatDepth32Float).
'
-[MTLDebugRenderCommandEncoder validateFramebufferWithRenderPipelineState:]:1288: failed assertion `Framebuffer With Render Pipeline State Validation
For depth attachment, the render pipeline's pixelFormat (MTLPixelFormatInvalid) does not match the framebuffer's pixelFormat (MTLPixelFormatDepth32Float).
'
-[MTLDebugRenderCommandEncoder validateFramebufferWithRenderPipelineState:]:1288: failed assertion `Framebuffer With Render Pipeline State Validation
For depth attachment, the render pipeline's pixelFormat (MTLPixelFormatInvalid) does not match the framebuffer's pixelFormat (MTLPixelFormatDepth32Float).
'
CoreSimulator 732.18.6 - Device: iPod touch (7th generation) (30471DA0-DB54-4415-ADB2-47E37F2F594E) - Runtime: iOS 14.3 (18C61) - DeviceType: iPod touch (7th generation)
(lldb)
原因
どうやらストーリーボードからMTKViewを追加した場合depthStencilPixelformatがMTLPixelFormatDepth32Floatで設定されているようです。
ストーリーボードで作ったプロジェクトとコードベースで作ったプロジェクトのdepthStencilPixelformat.rawValueの中身をみてみるとこんな感じになっていました。
StoryBoard | コード |
252 | 0 |
HelloTriangle in Swift: error about depth attachment pixel format
解決策
MTLRenderPipelineDescriptorのdepthAttachmentPixelFormatとStoryBoardで追加したMTLViewのdepthAttachmentPixelFormatを一致させることでクラッシュしなくなりました。
pipelineStateDescriptor.depthAttachmentPixelFormat = metalView.depthStencilPixelFormat