How to control anti-aliasing of an MTKView in Swift

To control anti-aliasing in Metal, set the sample count of the MTKView and the raster sample count of the MTLRenderPipelineDescriptor to the same value (i.e 2 is 2x, 4 ix 4x, 8 is 8x).

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 10/11/2023 at 14:54
Last Updated on 10/11/2023 at 15:44
<p>Three flowers that represent Swift, Metal and Xcode side by side. Beneath them sits the text "Control Anti-Aliasing."</p>

To control anti-aliasing in Metal, set the sample count of the MTKView and the raster sample count of the MTLRenderPipelineDescriptor to the same value (i.e 2 is 2x, 4 ix 4x, 8 is 8x).

SubscribeCheck out our Apple Metal Resources Guide

In Metal, you can control antialiasing through the use of the Multisample Anti-Aliasing (MSAA) technique. MSAA helps reduce jagged edges (also known as "aliasing") in computer graphics by sampling multiple points within each pixel to create smoother and more visually pleasing edges.

To do this you must first create and define a sampleCount variable (i.e. 2 for 2x, 4 for 4x, 8 for 8x) and then apply the variable to the sampelCount of the MTKView and the rasterSampleCount of the MTLRenderPipelineDescriptor.

It is important for you to do it in this order otherwise you will get an error similar to the one detailed below.

The color sample count (1) does not match the renderPipelineState's color sample count (4)

The raster sample count (1) does not match the renderPipelineState's raster sample count (4)

Step One: Declare the Sample Count

<p>A screenshot of Xcode showing the MetalUIView.swift file. We have highlighted how we have declared a "sampleCount" constant.</p>

In the declaration of the UIView that hosts your MTKView, declare a constant called sampleCount and set it to the value that you wish (i.e. 2 for 2x, 4 for 4x or 8 for 8x).

If you are looking to learn how to create a view that draws animated shapes on a MTKView, consult the links below.

How to use multiple shaders to draw multiple shapes within a MTKViewHow to draw an outlined shape with a line width in MetalHow to perform animations in Metal using a CADisplayLink

Step Two: Set the Sample Count of the MTKView

<p>A screenshot of Xcode showing the MetalUIView+Setup.swift file. We have highlighted how we have set the sampleCount of the MTKView to the variable declared in Step One.</p>

In the function where you setup the MTKView and add it to the UIView, set the sampleCount of the MTKView to the constant declared in Step One.

It is important that you do set the sample count of the MTKView before you access the view's drawableSize and device properties. Otherwise the process will not work.

Step Three: Set the Raster Sample Count of the MTLRenderPipelineDescriptor

<p>A screenshot of Xcode showing the MetalUIView+MetalDelegate.swift file. We have highlighted how we have set the rasterSampleCount of the pipeline descriptor to the variable declared in step one.</p>

In the function where you setup or adapt the MTLRenderPipelineDescriptor, set the rasterSampleCount of the MTLRenderPipelineDescriptor to the constant declared in Step One.

Step Four: Test

<p>A screenshot of an iPhone showing two shapes drawn in metal. One is filled and the other is outlined with a stroke that we set in code. This proves the functionality works.</p>

Run the code on a device and confirm that everything looks and works as expected.

If you are curious how we drew the outlined shape, follow the tutorial linked below.

How to draw an outlined shape with a line width in Metal

Looking to learn more about things you can do with Swift, Metal and XCode ?

Search our blog to find educational content on learning how to use Swift, Metal and XCode.

Search our Blog

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
SubscribeContact UsVisit our BlogView our ServicesView our Work

Partner with us

We would love to get to know you and see how we can help your organization with its goals and needs.
Let's Talk

Stay Informed

Get occasional updates about our company, research, and product launches.
Subscribe