Render Command Encoders (iOS and tvOS)
Best Practice: Merge render command encoders when possible.
Eliminating unnecessary render command encoders reduces memory bandwidth and increases performance. You can achieve these goals by merging render command encoders into a single rendering pass, when possible. To determine whether two render command encoders are merge-compatible or not, you must carefully evaluate their render targets, load and store actions, relationships, and dependencies. The simplest criteria for two merge-compatible render command encoders,
RCE2, are as follows:
RCE2are created in the same frame.
RCE2are created from the same command buffer.
RCE1is created before
RCE2shares the same render targets as
RCE2does not sample from any render targets in
No other render command encoders have been created between
If these criteria are met,
RCE2 can be merged into a single render command encoder,
RCEM, as shown in .
RCE1 can merge with a render command encoder created before it (
RCE2 can merge with a render command encoder created after it (
RCE3 can all be merged.
The following sections provide guidelines for evaluating merge compatibility between render command encoders, assuming all other criteria are met.
Evaluate Rendering Pass Order
Some apps may begin encoding into a render command encoder (
RCE1) and prematurely end the initial rendering pass if they require additional dynamic data to proceed. The dynamic data is then generated in a separate rendering pass with a second render command encoder (
RCE2). The initial rendering pass then continues with a third render command encoder (
RCE3). shows this inefficient order, including the separated render command encoders.
RCE2 does not depend on
RCE2 doesn’t need to be encoded after
RCE2 first allows
RCE3 to be merged into
RCEM because they represent the same rendering pass, and their dynamic data dependencies are guaranteed to be available at the start of the rendering pass. shows this improved order, including the merged render command encoders.
Evaluate Sampling Dependencies
Render command encoders cannot be merged if there are any sampling dependencies between them. For render command encoders that share the same render targets, these dependencies may be introduced by additional render command encoders in between them, as shown in.
RCE3 share the same render targets,
RT3. Furthermore, the actions between
RCE3 indicate a continuation of a rendering pass. However, these render command encoders cannot be merged due to the sampling dependencies introduced by
RCE2 renders to a separate render target,
RT4, which is sampled by
RT3 after it is rendered by
RCE1. These sampling dependencies define a strict rendering pass order that prevents merging these render command encoders.
Evaluate Actions Between Rendering Passes
The store and load actions between render command encoder render targets are not as important as other criteria, but there are a few notable cases where additional consideration is due. Use the following guidelines to further understand merge compatibility between render command encoders
RCE2, based on their shared render targets:
If the load action in
Clear, the render target is merge-compatible if a primitive clear operation can be performed in the merged render command encoder by first rendering clear values into a display-aligned quad.