Article

Rendering with a Rasterization Rate Map

Create offscreen textures to hold intermediate rasterized data.

Overview

You don’t render data directly to the final render destination using variable rasterization rates. Instead, you allocate textures to use as intermediate targets. You get the sizes for these textures from the rate map. For example, if you specified a rate of 0.5 everywhere, the intermediate texture is half the height and width of the final render target.

The rate map’s screenSize property gives the dimensions of the final render target. Call the physicalSize(layer:): method on the rate map object to get the size for each layer. Then allocate the textures you need. For example, the following code gets the physical size and allocates a color texture and a depth texture of that size:

MTLSize textureSize = [_rateMap physicalSizeForLayer:0];

MTLTextureDescriptor *colorTextureDescriptor = [MTLTextureDescriptor
        texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm_sRGB
        width:textureSize.width
        height:textureSize.height
        mipmapped:NO];
colorTextureDescriptor.usage = MTLTextureUsageShaderRead | MTLTextureUsageRenderTarget;
id<MTLTexture> colorTexture = [_device newTextureWithDescriptor:colorTextureDescriptor];

MTLTextureDescriptor *depthTextureDescriptor = [MTLTextureDescriptor
        texture2DDescriptorWithPixelFormat:MTLPixelFormatDepth32Float_Stencil8
        width:textureSize.width
        height:textureSize.height mipmapped:NO];
depthTextureDescriptor.usage = MTLTextureUsageRenderTarget;
id<MTLTexture> depthTexture = [_device newTextureWithDescriptor:depthTextureDescriptor];

To use variable rasterization rates, create a render pass, specifying the intermediate textures as the render targets. Set the rasterizationRateMap property to the rate map you created, as shown below:

MTLRenderPassDescriptor* firstPassDescriptor = [[MTLRenderPassDescriptor alloc] init];

firstPassDescriptor.rasterizationRateMap = _rateMap;

firstPassDescriptor.colorAttachments[0].texture = colorTexture;
firstPassDescriptor.colorAttachments[0].loadAction = MTLLoadActionClear;
firstPassDescriptor.colorAttachments[0].storeAction = MTLStoreActionStore;

firstPassDescriptor.depthAttachment.texture = depthTexture;
firstPassDescriptor.depthAttachment.loadAction = MTLLoadActionClear;
firstPassDescriptor.depthAttachment.storeAction = MTLStoreActionDontCare;
firstPassDescriptor.stencilAttachment.texture = depthTexture;
firstPassDescriptor.stencilAttachment.loadAction = MTLLoadActionClear;
firstPassDescriptor.stencilAttachment.storeAction = MTLStoreActionDontCare;

See Also

Essentials

Creating a Rasterization Rate Map

Define the rasterization rates for each part of your render target.

Scaling Variable Rasterization Rate Content

Use the rate map data to scale the content to fill your destination texture.