Depth test not working on OS X

I migrated an iOS Metal app to OS X and things generally went pretty smoothly. However, on OS X, it does not perform depth testing. The same code on iOS does.


Both of them use a MetalKit view and this code to turn on depth testing:


        let modelDepthStencilDescriptor = MTLDepthStencilDescriptor()
        modelDepthStencilDescriptor.depthCompareFunction = MTLCompareFunction.Less
        modelDepthStencilDescriptor.depthWriteEnabled = true
        modelDepthStencilState = SharedDevice.device.newDepthStencilStateWithDescriptor(modelDepthStencilDescriptor)


But on the Mac, it looks exactly the same whether depthWriteEnabled is true or false.


Anywhere jumping to mind where I should be looking?

Accepted Reply

Are you configuring the

depthStencilPixelFormat
of your
MTKView
? It should be set to
.Depth32Float_Stencil8
or similar. If you leave this set to the default (
.Invalid
), the view won't create a depth texture for you.

Replies

Are you configuring the

depthStencilPixelFormat
of your
MTKView
? It should be set to
.Depth32Float_Stencil8
or similar. If you leave this set to the default (
.Invalid
), the view won't create a depth texture for you.

This worked (thanks!) - but now I'm scratching my head as to why depth testing is active on iOS. I just checked and my depth texture code is being skipped on iOS (since it's set to .Invalid), but it's still depth testing and follows .depthWriteEnabled.

iOS devices use a tile-based deferred rendering scheme that uses memory in the GPU to collect and sort the fragments before writing any of them out. If the depth complexity of a tile doesn't exceed the ability of the GPU to store it, you will get rendering that looks correct even in the absence of an attached depth texture, but this behavior isn't guaranteed.

The fact that depth testing worked at all is actually a bug. wcm's described why the hardware still does anything at all, but the Metal debug layer should (but currently does not) raise an error when you attempt to use one of these DepthStencilStates without a depth or stencil buffer. We'll likely fix this in a future update, so please don't depend on this behavior (as unreliable as it is).

Thank you for the explanation. That clarifies things a lot.

Thanks @wcm! That helped me as well!


I think it would really help if the "Performing Depth and Stencil Operations" section had a helpful reminder about depthStencilPixelFormat requirements for MTKView and MTLRenderPipelineDescriptor on the Graphics Rendering page on the Metal Programming Guide. I know the depthStencilPixelFormat is mentioned elsewhere in the guide on that page but it's not clear from the documentation that the depth section wont work without the stencil pixel format setting. Just a thought.