Search code examples
iosswiftios9metal

Metal best practices - changing renderPipelineDescriptor during render


During my rendering pipeline i would like to user a few shaders and in some cases modify parameters on the MTLRenderPipelineDescriptor object (for example, change blending functions).

As i see it, i have 2 options:

  1. Create and precompile one MTLRenderPipelineState for each combination of parameters (vertex shader, fragment shader, blending, etc).I can have many such state objects because there could be many combinations.

  2. Create and compile new MTLRenderPipelineState objects during the rendering process.

Which of the options would be better? Are there any other options i am missing.


Solution

  • For best-practice (and best performance), you should follow your Option 1.

    In the Transient and Non-transient Objects in Metal section, The Metal Programming Guide is quite clear about which objects should be considered transient or non-transient, and that non-transient objects should be cached and reused.

    For the MTLRenderPipelineState object in particular, here's what the guide has to say in the Creating a Render Pipeline State section:

    A render pipeline state object is a long-lived persistent object that can be created outside of a render command encoder, cached in advance, and reused across several render command encoders. When describing the same set of graphics state, reusing a previously created render pipeline state object may avoid expensive operations that re-evaluate and translate the specified state to GPU commands.