Why can a fragment-program branch on a per-pixel material flag run slower on some screen tiles than on others?
answer
- lanes share one instruction stream
- an execution mask hides lanes
- mixed tiles pay both paths
- per-draw versus per-pixel conditions
- classify tiles by material
basics
~20 sGPUs run fragments in SIMD groups, typically 32 or 64 lanes sharing one instruction stream. When a group's pixels disagree on the flag, it executes both paths with lanes masked off, so tiles where materials mix pay for both.
solid answer
~50 sA GPU shades fragments in **SIMD groups** (typically 32 or 64 lanes, depending on the GPU) that issue one instruction at a time for all lanes. A branch is handled with an **execution mask**: if every lane in the group agrees, only the taken path runs; if they disagree, the group runs one path with some lanes masked off, then the other, so it pays for **both**. A group usually covers a compact block of screen pixels, so tiles inside one material are coherent and fast, while tiles straddling a material border, or mixing materials pixel by pixel, diverge and run slowly. The fixes: branch on per-draw constants where you can, classify screen tiles by the materials they contain and run a program specialised per class, keep the expensive path small, or let the compiler flatten very short branches into a select.
go deeper
Recall that GPU lanes run in groups sharing one instruction stream, and that a branch where lanes disagree runs both sides. That single fact explains the uneven tiles.
Explain the execution mask, why a group covers a compact block of pixels, and why the slow tiles are the ones where materials meet or mix. Distinguish a per-draw condition from a per-pixel one.
Diagnose from the picture: line the slow tiles up with material borders, compare path costs, then choose a fix such as tile classification or splitting draws. Know when flattening beats branching.
Weigh divergence against variant count: specialised programs per material avoid divergence but multiply programs. Decide which material features may branch per pixel and which must be resolved per draw or per tile.
## How a GPU executes a branch A GPU does not run each fragment as an independent thread with its own instruction pointer. It packs fragments into **SIMD groups**, typically 32 or 64 lanes depending on the GPU, and issues one instruction at a time for the whole group. When the program reaches a branch, the hardware evaluates the condition in every lane and builds an **execution mask**: - If every active lane takes the same side, the group jumps and runs only that side. - If lanes disagree, the group runs the first side with the other lanes masked off, then the second side with the first set masked off, and reconverges afterwards. - Masked lanes do no useful work, but the group still spends the cycles. Some GPUs track each lane's progress more independently, but divergent paths inside one group are still executed one after the other rather than at the same time, so the cost model holds. ## Why the cost depends on where the pixels are A group is usually filled with neighbouring 2x2 pixel quads, so it covers a compact block of the screen; the exact shape varies between GPUs. Its cost therefore depends on how coherent the flag is inside that block: | Block contents | Branch behaviour | Cost per group | |---|---|---| | Only material A | uniform, takes A | cost of A | | Only material B | uniform, takes B | cost of B | | A border between A and B, or a pixel-by-pixel mix | divergent | cost of A + cost of B, plus overhead | With a 150-instruction path for A and a 450-instruction path for B, a block of pure A costs 150, a block of pure B costs 450, and any block that contains even one pixel of each costs about 600. The slow tiles in a capture are the ones that straddle material borders or hold a noisy mix, such as a ground texture that scatters wet and dry pixels. ## Kinds of branch condition The condition's source predicts whether it can diverge: - **Per-draw constants** are identical for every lane, so the branch does not diverge. Many GPUs evaluate such a condition once per group. - **Per-instance or per-triangle values** are coherent inside one triangle, but a group can hold quads from several triangles of the same draw, so they can still diverge at triangle edges. - **Per-pixel data**, such as a material flag read from a texture, diverges wherever the image is not spatially coherent. - **Loops with a per-pixel trip count** diverge too: the group keeps iterating until its slowest lane finishes, with finished lanes masked off. ## Diagnosing the slow tiles 1. Visualise the flag per pixel and compare it with the per-tile timing or divergence view a GPU capture offers. 2. Check whether the slow tiles line up with material borders or with regions where the flag is noisy. 3. Read the compiler's statistics for each path; a large gap between the two paths makes divergence expensive. 4. Test by forcing the flag to one value: if the tile-to-tile variation disappears, divergence was the cause. ## Fixes - **Resolve the choice per draw.** If the flag really belongs to an object, split the draws by material and branch on a per-draw constant. - **Classify tiles.** A first compute pass reads each tile's flags and appends the tile to a list for its combination: only A, only B or mixed. A program specialised for each list then runs over just those tiles, so pure tiles execute one path with no branch and only mixed tiles pay for both. Recording the indirect dispatches that run each list is a submission concern; the saving comes from coherence. - **Shrink the expensive path.** Move shared work, such as common texture reads, before the branch, and keep only the material-specific part inside it. - **Flatten short branches.** When both sides are a few instructions, computing both and selecting the result can be cheaper than the branch itself; compilers often do this on their own. - **Reorder compute work.** In a compute pass you can sort items so neighbouring threads take the same path. A branch is still the right tool when it skips heavy work for whole groups, such as an early exit for sky pixels covering large coherent regions. ## Divergence in memory reads Control flow is not the only thing that diverges. When neighbouring lanes read texture or buffer addresses far apart, each read touches different cache lines and the group waits on many memory transactions instead of a few. A fragment program that samples with coordinates scattered by per-pixel noise can therefore run slowly even without a branch. The remedy is the same idea: keep neighbouring lanes doing similar work on nearby data.
- Why can a branch on a per-draw constant be nearly free?Every lane of every group in the draw reads the same value, so groups do not diverge on it; many GPUs evaluate such a condition once per group. Only the taken side's instructions run. The remaining cost is indirect: the program is still allocated registers for its heaviest path, which can lower occupancy.
- How does tile classification remove divergence from a full-screen material pass?A first compute pass reads each tile's material flags and appends the tile to a list for its combination, such as only A, only B or mixed. A program specialised for each list then runs over just those tiles, so pure tiles execute one path without branching and only genuinely mixed tiles pay for both. The price is the classification pass and more programs to maintain.
- Does a loop with a per-pixel trip count diverge as well?Yes. The group keeps iterating until its slowest lane finishes, with finished lanes masked off, so one pixel needing 64 steps makes its whole group pay for 64. Bounding the count, grouping work so neighbours need similar counts, or moving the long cases to a separate pass reduces the waste.
A tour group that must stay together: if some members want the museum and others the park, the whole group visits both, one after the other, while the uninterested members wait at each stop. Only a group whose members all agree visits a single place.
- A GPU skips the untaken path for each pixel independently, like a CPU core.
- A divergent group costs the lane-weighted average of the two paths.
- A branch on a per-draw constant diverges just as badly as a per-pixel flag.
- Removing every branch always makes a fragment program faster.
- Uneven tile timings must come from texture misses, never from control flow.