<!--
{
  "availability" : [
    "iOS: 12.0.0 -",
    "iPadOS: 12.0.0 -",
    "macCatalyst: -",
    "macOS: 10.13.0 -",
    "tvOS: 14.5.0 -",
    "visionOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "Metal",
  "identifier" : "/documentation/Metal/MTLRenderCommandEncoder/setViewports(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Metal"
    ],
    "preciseIdentifier" : "s:So23MTLRenderCommandEncoderP5MetalE12setViewportsyySaySo11MTLViewportaGF"
  },
  "title" : "setViewports(_:)"
}
-->

# setViewports(_:)

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

```
func setViewports(_ viewports: [MTLViewport])
```

## Parameters

`viewports`

An array of [`MTLViewport`](/documentation/Metal/MTLViewport) instances the command applies to the render pipeline for transformations and clipping.

## 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(_:)`](/documentation/Metal/MTLRenderCommandEncoder/setScissorRect(_:)) method, or set each viewport’s rectangle with the [`setScissorRects(_:)`](/documentation/Metal/MTLRenderCommandEncoder/setScissorRects(_:)) method.

> Important:
> The number of scissor rectangles you pass to ``doc://com.apple.metal/documentation/Metal/MTLRenderCommandEncoder/setScissorRects(_:)`` needs to match the number of viewports you configure with this method.

The maximum number of viewports and scissor rectangles a GPU supports varies by device family. For more information, see [`MTLGPUFamily`](/documentation/Metal/MTLGPUFamily) and [Detecting GPU features and Metal software versions](/documentation/Metal/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(_:)`](/documentation/Metal/MTLRenderCommandEncoder/setScissorRect(_:))) or the render target’s extents.

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

> Note:
> You can change the render pass’s viewport configuration by calling this method again or by calling the ``doc://com.apple.metal/documentation/Metal/MTLRenderCommandEncoder/setViewport(_:)`` method.

The [`setViewport(_:)`](/documentation/Metal/MTLRenderCommandEncoder/setViewport(_:)) method is equivalent to calling this method with a single viewport element in the `viewports` array.

---

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)