[SOLVED] How to turn-off/disable Metal depth testing

Table of Contents


I know how to enable the depth test in Metal using swift.

Just call the MTLRenderCommandEncoder.setDepthStencilState() with appropriate MTLDepthStencilState object like this and it works fine.

renderCommandEncoder.setDepthStencilState( state )

to turn it off, I thought this could work but it gives me an error at runtime.

renderCommandEncoder.setDepthStencilState( nil )

the error:

-[MTLDebugRenderCommandEncoder setDepthStencilState:]:3843: failed assertion `Set Depth Stencil State Validation
depthStencilState must not be nil.

it is weird because Apple’s documentation says that the default value is nil and the function setDepthStencilState() takes optional value.

any idea how to turn depth-testing off or am I doing something wrong?


  • Xcode 13.2
  • Swift 5
  • deployment target: MacOS 11.1


You can disable depth test by creating an MTLDepthStencilState from MTLDepthStencilDescriptor with depthCompareFunction set to always.

let descriptor = MTLDepthStencilDescriptor()
descriptor.depthCompareFunction = .always
let depthStencilState = device.makeDepthStencilState(descriptor: descriptor)

Update: just setting the depthCompareFunction to always will make the depth test always pass, but it will also still write out the depth for all the fragments. If you want to keep the depth buffer in the same state, you can set isDepthWriteEnabled to false.

Answered By – JustSomeGuy

Answer Checked By – Senaida (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *