Instance Method

setDepthBias(_:slopeScale:clamp:)

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

Required.

Declaration

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

Parameters

depthBias

A constant bias applied to all fragments.

slopeScale

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

clamp

The maximum bias value to apply to the fragment.

Discussion

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.

Required.

func setDepthClipMode(MTLDepthClipMode)

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

Required.

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.

Required.

func setStencilReferenceValue(UInt32)

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

Required.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software