Contents

setViewports(_:)

Configures the render pipeline with multiple viewports that apply transformations and clipping rectangles.

Declaration

func setViewports(_ viewports: [MTLViewport])

Parameters

  • viewports:

    An array of Mtlviewport instances the command applies to the render pipeline for transformations and clipping.

Mentioned in

Discussion

Use this method to configure multiple active viewports and corresponding scissor rectangles. Multiple viewports give your app the ability to draw into separate areas of an image with a single draw call. You can either set a single scissor rectangle for all viewports with the setScissorRect(_:) method, or set each viewport’s rectangle with the setScissorRects(_:) method.

The maximum number of viewports and scissor rectangles a GPU supports varies by device family. For more information, see MTLGPUFamily and Detecting GPU features and Metal software versions.

The rendering pipeline sends each primitive to a single viewport and its associated scissor rectangle. You can select which viewport each primitive uses in your vertex shader by adding the [[viewport_array_index]] attribute to an output value.

The render pipeline linearly maps vertex positions from normalized device coordinates to viewport coordinates by applying a viewport during the rasterization stage. It applies the transform first and then rasterizes the primitive while clipping any fragments outside the scissor rectangle (see setScissorRect(_:)) or the render target’s extents.

The viewport’s originX and originY properties, which default to 0.0, represent the number of pixels from the top-left corner of the render target. Positive originX values go to the right and positive originY values go downward. The default values for its width and height properties are the render target’s width and height, respectively. The default values for its znear and zfar properties are 0.0 and 1.0, respectively, which you can flip.

The setViewport(_:) method is equivalent to calling this method with a single viewport element in the viewports array.

See Also

Configuring viewport and scissor behavior