← COURSE LIBRARY
Shader Code · 40 min
Warp Divergence and Branching
AdvancedNeeds: GPU execution model
Sign in to record mastery, quiz results and reviews for this course.Sign in
You will be able to
- Explain SIMD lockstep execution and what divergence actually costs.
- Decide between branch, lerp and branchless arithmetic in a given case.
- Recognise which branches are free.
The concept
GPUs execute threads in warps/waves of 32 or 64 in lockstep. When threads in a warp take different sides of a branch, both sides execute and the inactive lanes are masked. A uniform branch — one where every lane in the warp agrees — is nearly free. A branch driven by per-pixel data on a noisy mask is the expensive case.
In your engine
`[branch]` and `[flatten]` attributes let you tell the compiler which behaviour you want; check the compiled variant.
Practice (4)
Concept check
Which branch is effectively free?
Predict the result
A 32-lane warp splits 50/50 on an expensive branch where each side costs C. Approximate cost?
Concept check
When is lerp(a, b, mask) better than an if?
Find the bug
A shader is fast in isolation but slow in a foliage-heavy scene. Most likely cause among these?
Knowledge check
01A warp/wave is typically:
02Divergence costs are worst when:
03Static switches in a material:
04Fragment shading operates on:
Build challenge
Take a shader with a noisy per-pixel branch and produce three variants: branch, lerp, and branchless arithmetic. Profile all three on the same scene and report cycle or ms numbers with a recommendation.
SELF-CHECK RUBRIC