Instance Method


Adjusts the depth values from fragment functions by a scaling factor and a scaling bias, clamping the bias to a maximum amount.



func setDepthBias(_ depthBias: Float, slopeScale: Float, clamp: Float)



A constant bias applied to all fragments.


A bias that scales with the depth gradient of the primitive.


The maximum bias value to apply to the fragment.


A slope-scaled bias is used to improve and fine-tune shadow maps, avoid shadow acne (unintentional self-shadowing), and avoid other depth artifacts that are caused by coplanar polygons. The bias is applied to the rasterized depth and affects both depth testing and the depth value written to the depth render target. If you don't explicitly call this method, the pipeline won't apply a scale or a bias to the calculated depth value.

The bias value is calculated as follows, depending on the data type of the depth pixel format (see MTLPixelFormat for more information):

  • Unorm: bias = depthBias * r + slopeScale * maxSlope

  • Float: bias = depthBias * 2^(exp(max abs(z) in primitive) - r) + slopeScale * maxSlope

Where r is the smallest positive representable value in the depth render target, and maxSlope is the largest slope of the depth value at the pixel.

The value of clamp is applied as a min or a max operation, depending on its sign. If clamp>0, then bias = min(clamp, bias); if clamp<0, then bias = max(clamp, bias); if clamp=0, a clamp is not applied.

If either depthBias or slopeScale is nonzero, add the final calculated value of bias to the depth value.

Depth bias only influences triangle primitives; it is not applied to points or lines.

See Also

Setting Depth and Stencil Behavior

func setDepthStencilState(MTLDepthStencilState?)

Sets the depth and stencil test state.


func setDepthClipMode(MTLDepthClipMode)

Controls what is done with fragments outside of the near or far planes.


enum MTLDepthClipMode

The mode that determines how to deal with fragments outside of the near or far planes.

func setStencilReferenceValues(front: UInt32, back: UInt32)

Sets the stencil reference values for front and back stencil comparison tests.


func setStencilReferenceValue(UInt32)

Sets a stencil reference value for both front and back stencil comparison tests.