<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.11.0 -",
    "tvOS: -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Metal",
  "identifier" : "/documentation/Metal/MTLRenderCommandEncoder/setDepthStencilState(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Metal"
    ],
    "preciseIdentifier" : "c:objc(pl)MTLRenderCommandEncoder(im)setDepthStencilState:"
  },
  "title" : "setDepthStencilState(_:)"
}
-->

# setDepthStencilState(_:)

Configures the combined depth and stencil state.

```
func setDepthStencilState(_ depthStencilState: (any MTLDepthStencilState)?)
```

## Parameters

`depthStencilState`

An instance that conforms to the [`MTLDepthStencilState`](/documentation/Metal/MTLDepthStencilState) protocol.

## Discussion

This method changes the combined depth and stencil state for the render command encoder that’s compatible with its depth and stencil attachment configuration. For example, if the new state enables depth testing or depth writing, the render pass needs to have a depth attachment. Similarly, if the new state enables stencil testing or stencil writing, the render pass’s stencil needs to have a stencil attachment. You create depth and stencil attachments for a render pass by assigning the [`depthAttachment`](/documentation/Metal/MTLRenderPassDescriptor/depthAttachment) and [`stencilAttachment`](/documentation/Metal/MTLRenderPassDescriptor/stencilAttachment) properties of the [`MTLRenderPassDescriptor`](/documentation/Metal/MTLRenderPassDescriptor) instance that creates it.

Pass `nil` to clear the state from the previous call, which restores a state that’s equivalent to the default values of an [`MTLDepthStencilDescriptor`](/documentation/Metal/MTLDepthStencilDescriptor) instance’s properties.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)