how to do full-screen antialiasing?

I can't find any examples online for how to do full-screen antialiasing with Metal. The only thing that comes up with Google is the Apple doc page for MTLRenderPassAttachmentDescriptor. Does anyone know of any sample code or examples that describe how to do this?

Accepted Reply

So there are 2 ways to get MSAA to work.


The simplest way which will cover %90 of the use cases is by using an MTKView and setting the view's sampleCount to a non-zero number. This will make the texture your render to multisampled and the render pass descriptor obtained from the view will be setup to resolve the samples. However, you must also also create any render pipelines used to render in the pass with a matching sampleCount.
However, if you have other requirements, such as rendering to an offscreen MSAA render target or you need to render to multiple MSAA render attachments, here's the general idea.


1) Create a texture with a sampleCount greater than one. This texture will serve as the MSAA texture you will render to. You may also want to create a depth or stencil texture with matching sampleCounts. (On iOS, you can likely make this texture a "memoryless" texture)

2) Create (or obtain) a single sampled color (and/or depth stencil) texture that you want resolve into. This will serve as the downsampled version of the texture Metal will resolve into to. I say "or obtain" previously because this texture is often the drawable's texture obtained from the CAMetalLayer or MTKView. If this is the case, you don't create the texture yourself.

3) You will also need to make sure the render pipelines used to render to these textures have a matching sampleCount.
4) To perform MSAA rendering, attach the texture(s) you created in step 1 to the render pass descriptor.

5) Set the texture you obtained in step 2 as the attachment's '

resolveTexture
' for the corresponding render attachment in the render pass descriptor

6) Set the store action of the attachment in the render pass descriptor to 'MTLStoreActionMultisampleResolve'. This will tell Metal that when the render pass is complete, it should resolve your multisample rendering into the resolve texture.


As far as samples go, this Forward Plus With Tile Shading sample is the most recent and will run on iOS with an A11. This Adopting Metal II sample also uses MSAA and will run on macOS and is written in Swift.

Replies

So there are 2 ways to get MSAA to work.


The simplest way which will cover %90 of the use cases is by using an MTKView and setting the view's sampleCount to a non-zero number. This will make the texture your render to multisampled and the render pass descriptor obtained from the view will be setup to resolve the samples. However, you must also also create any render pipelines used to render in the pass with a matching sampleCount.
However, if you have other requirements, such as rendering to an offscreen MSAA render target or you need to render to multiple MSAA render attachments, here's the general idea.


1) Create a texture with a sampleCount greater than one. This texture will serve as the MSAA texture you will render to. You may also want to create a depth or stencil texture with matching sampleCounts. (On iOS, you can likely make this texture a "memoryless" texture)

2) Create (or obtain) a single sampled color (and/or depth stencil) texture that you want resolve into. This will serve as the downsampled version of the texture Metal will resolve into to. I say "or obtain" previously because this texture is often the drawable's texture obtained from the CAMetalLayer or MTKView. If this is the case, you don't create the texture yourself.

3) You will also need to make sure the render pipelines used to render to these textures have a matching sampleCount.
4) To perform MSAA rendering, attach the texture(s) you created in step 1 to the render pass descriptor.

5) Set the texture you obtained in step 2 as the attachment's '

resolveTexture
' for the corresponding render attachment in the render pass descriptor

6) Set the store action of the attachment in the render pass descriptor to 'MTLStoreActionMultisampleResolve'. This will tell Metal that when the render pass is complete, it should resolve your multisample rendering into the resolve texture.


As far as samples go, this Forward Plus With Tile Shading sample is the most recent and will run on iOS with an A11. This Adopting Metal II sample also uses MSAA and will run on macOS and is written in Swift.

Okay thanks! Yeah I really just needed the simple version -- I guess that is why there were no explanations online, it was too simple!

For the texture in (1) you need to set the texture type to MTLTextureType2DMultisample, set usage to MTLTextureUsageRenderTarget|MTLTextureUsageShaderRead, and also set the sampleCount. Something like


// Assuming the following for example is passed into the method below.

id<MTLTexture> texture = drawable.texture


- (id<MTLTexture>) createAliasingTextureFromTexture:(id<MTLTexture>)texture

{

MTLTextureDescriptor *textureDescriptor = [[MTLTextureDescriptor alloc] init];

[textureDescriptor setPixelFormat:[texture pixelFormat]];

[textureDescriptor setWidth:[texture width]];

[textureDescriptor setHeight:[texture height]];

[textureDescriptor setTextureType:MTLTextureType2DMultisample];

[textureDescriptor setUsage:MTLTextureUsageRenderTarget|MTLTextureUsageShaderRead];

[textureDescriptor setSampleCount:AliasingSampleCount];


return [device newTextureWithDescriptor:textureDescriptor];

}


This works for me from the instructions given by Omachi.