“This is the 19th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”

An overview of the

By setting the MTLLoadAction and MTLStoreAction attributes, can define rendering channel load and store MTLRenderPassAttachmentDescriptor object. Setting up the appropriate actions for the render target avoids expensive and unnecessary work at the start (load) or end (store) of the render channel.

Set the render target’s texture on the Texture property and its action on the loadAction and storeAction properties:


let renderPassDescriptor = MTLRenderPassDescriptor()


// Color render target

renderPassDescriptor.colorAttachments[0].texture = colorTexture

renderPassDescriptor.colorAttachments[0].loadAction = .clear

renderPassDescriptor.colorAttachments[0].storeAction = .store

// Depth render target

renderPassDescriptor.colorAttachments[0].texture = depthTexture

renderPassDescriptor.colorAttachments[0].loadAction = .dontCare

renderPassDescriptor.colorAttachments[0].storeAction = .dontCare


// Stencil render target

renderPassDescriptor.colorAttachments[0].texture = stencilTexture

renderPassDescriptor.colorAttachments[0].loadAction = .dontCare

renderPassDescriptor.colorAttachments[0].storeAction = .dontCare

Copy the code

Selective load operation

There are several load operation options available, which one you choose depends on the load requirements of the render target.

  • When you do not need to render the previous content of the target, but instead render to all of its pixels, select mtlLoadAction.dontCare

There is no cost to this operation, and the pixel value is always undefined at the start of the render channel.

  • Select mtlLoadAction.clear when you do not need to render the previous content of the target and only need to render part of its pixels

This operation incurs the cost of writing the clear value of the rendered target to each pixel.

  • Select mtlLoadAction.load when you need to render the previous content of the target and only render to some of its pixels

This operation incurs the cost of loading the previous value of each pixel from memory, which is significantly slower than mtlLoadAction.dontcare or mtlLoadAction.clear.

Select store operation

There are several storage operation options available, which one you choose depends on the storage needs of the render target.

  • Without preserving the contents of the rendered target, select mtlstoreAction.dontCare

There is no cost to this operation, and the pixel value remains undefined at the end of the render pass. In the render channel, select this action for the intermediate render target, after which the intermediate result is not required. This is the right choice for depth and template rendering targets.

  • If you do want to preserve the contents of the render target, select mtlStoreAction.store

This operation stores the value of each pixel into memory and incurs costs. This is always the right choice for drawable objects.

  • The rendering target is a multi-sampled texture

When performing multiple sampling, you can choose to store multiple sampling or parse data for the rendered target. For multi-sample data, it is stored in the render target’s Texture property. For parsed data, it is stored in the render target’s resolveTexture property. For multiple sampling, please refer to this table to select storage operations:

Multisample data storage Parsing data store Need to parse texture Required storage operations
is is is  MTLStoreAction.storeAndMultisampleResolve
is no no  MTLStoreAction.store
no is is  MTLStoreAction.multisampleResolve
no no no   MTLStoreAction.dontCare

Will render in a single channel storage and analytical sampling texture, please always choose MTLStoreAction. StoreAndMultisampleResolve operation and use a single command encoder.

  • The storage selection needs to be deferred

In some cases, it may not be known which storage operation to use for a particular render target until more render channel information is gathered. To delay the store operation choice, please set when creating the MTLRenderPassAttachmentDescriptor object MTLStoreAction. Unknown values. Setting up an unknown storage operation avoids the potential cost of setting up another storage operation. However, before coding the render channel can be completed, a valid store operation must be specified; Otherwise, an error occurs.

Evaluate operations between render channels

The same render target can be used in multiple render processes. There may be multiple load and store combinations for the same render target between any two render channels, depending on the needs of the render target from one render channel to the other.

  • In the next render pass, there is no need to render the previous content of the target

In the first render channel, select mtlStoreAction.dontCare to avoid storing the contents of the render target. In the second render pass, select mtlLoadAction. dontCare or mtlLoadAction. clear to avoid loading the render target’s content.

  • The previous content of the render target needs to be used in the next render pass

In the first render pass, Choose MTLStoreAction. Store, MTLStoreAction. MultisampleResolve or MTLStoreAction storeAndMultisampleResolve to store the contents of the render target. In the second render channel, select mTLLoadAction.load to load the contents of the render target.

conclusion

This article describes how to set up the load and store operations of the render target according to the needs of the render. Setting up these operations properly can avoid expensive and unnecessary work. Through the graphic details, according to different rendering requirements, set up different loading and storage operations to achieve the rendering effect.