✓ Email copied to clipboard
← BACK TO JOURNAL

Your Fragment Shader Is a Liar: Fixing Mobile GPU Choke

ADVERTISEMENT Google AdSense
Your Fragment Shader Is a Liar: Fixing Mobile GPU Choke

There is a specific kind of silence that falls over the studio when a WebGL demo stutters on a client’s phone. It isn’t the loud crash of a context loss. It’s the subtle, rhythmic hiccup where the animation jumps from smooth silk to grindstone grit. We saw it on a mid-range Android device. The frame rate hovered at 45fps. Not 30. Not 60. 45. That limbo state is where user trust dies.

The Myth of the 'Lightweight' Shader

The standard advice from web dev blogs is always the same: "Keep your fragment shaders simple. Avoid loops. Don't use heavy math." It’s not wrong, but it’s dangerously incomplete. It treats the GPU as a monolith, a single magic box that processes pixels in parallel. It isn't. A mobile GPU is a power-constrained beast with limited register files, shared memory, and a strict thermal envelope.

When we profiled the draw calls on the problematic scene, the vertex shader was trivial. The fragment shader, however, was a 20-line snippet calculating a subtle iris effect using a recursive loop for raymarched depth. On a desktop RTX card, this took 0.002ms per pixel. On the Adreno 650, it took 0.08ms. That 40x difference didn’t come from the calculation itself. It came from branch divergence and register pressure.

"If your shader looks easy to write, look at the assembly. If the assembly looks like a maze, your mobile users are paying the toll."—Studio Post-mortem, Q3

The Register Pressure Trap

Most creative developers write shaders in GLSL or WGSL and assume the compiler does the heavy lifting. It does, but badly on mobile architectures. Consider a common pattern: storing intermediate vectors in variables to keep code readable.

vec3 lightDir = normalize(uLightPos - vWorldPos);
vec3 viewDir = normalize(uCameraPos - vWorldPos);
vec3 normal = normalize(vNormal);

// This looks clean. It allocates three full vec3s.
vec3 halfVec = normalize(lightDir + viewDir);
float NdotH = max(dot(normal, halfVec), 0.0);
float spec = pow(NdotH, uShininess);

On a desktop GPU, the register file is vast. On a mobile tile-based deferred renderer (TBDR), the GPU processes a small tile (often 4x4 or 8x8 pixels) at a time. It loads the data, computes, stores, and discards. If your shader requires too many live registers simultaneously, the compiler has to spill data to local memory (VRAM) frequently. That memory bandwidth is the actual bottleneck, not the ALU cycles.

We measured this. We ran the same shader with explicit register binding and forced reuse. The bandwidth usage dropped by 40%. The frame time stabilized. The fix wasn't "make it faster math." It was "make the data footprint smaller."

Branch Divergence Is a Tax

Let’s talk about `if` statements. In a warp or wavefront (the basic unit of parallel execution on AMD/Apple/NVIDIA), if one thread takes a branch and another doesn’t, the GPU has to serialize them. It runs the 'true' path while the 'false' threads wait, then runs the 'false' path while the 'true' threads wait. On desktop, this is often hidden by massive out-of-order execution. On mobile, with tighter instruction pipelines and smaller SIMT widths, it’s a visible stutter.

We had a shader that checked if a pixel was within a certain distance from a light source to apply a soft glow. The check was cheap. But the glow calculation involved a square root and a power function. Half the pixels in the tile triggered the check, half didn’t. The result: 50% serialization penalty per tile.

The fix? Replace the branch with a continuous function. Use a smoothstep or a polynomial approximation that returns zero outside the threshold. No branch. No divergence. The GPU keeps flowing.

// BAD: Branch divergence
if (dist < uRadius) {
    color += glow;
}

// GOOD: Branchless, continuous
float influence = smoothstep(uRadius, 0.0, dist);
color += glow * influence;

It’s a small change. It feels almost trivial. But on a 1080p screen, that’s 2+ million pixels making that decision every frame. Trivial, multiplied by millions, is catastrophic.

Texture Fetches Are Not Free

Here’s the kicker that broke our previous assumptions: mipmapping. We were using `texture2D` with manual bias control to get crisp details at close range. On mobile, accessing non-mipmapped textures causes massive cache misses. The GPU has to pull data from DRAM instead of the on-chip cache. That DRAM access takes hundreds of cycles. Your 16.6ms frame budget evaporates in a single texture fetch storm.

We switched to automatic trilinear filtering with a moderate bias. The visual difference was negligible at a glance. The performance gain was a 2.1ms reduction per frame on the Android device. We bought back 13% of our frame budget just by letting the driver do its job.

The Thermal Wall

None of this matters if the device throttles. Mobile GPUs have a thermal ceiling. If you push them to 100% for more than 30 seconds, they downclock. The frame rate drops from 60fps to 40fps, not because of your code, but because the silicon is overheating. We saw this in our battery of tests. The first 10 seconds were perfect. The next 60 seconds were a slide into mediocrity.

The solution isn't to lower the quality permanently. It’s to profile your thermal envelope. We added a lightweight telemetry hook that monitors frame time deltas. If the average frame time over a 5-second window exceeds 18ms, we dynamically reduce the pixel ratio or disable the most expensive post-process pass. Users don’t know it happened. They just notice the animation stays smooth instead of getting choppier and choppier until they close the tab.

What We Actually Changed

We stopped treating mobile as a 'smaller desktop.' We started treating it as a different machine with different constraints. Our checklist now includes:

  • Register Audit: We use the mobile web developer tools to check register usage. If a shader uses more than 32 registers, we refactor.
  • Branchless Logic: We ban `if` statements in fragment shaders unless the condition is uniform across the entire tile. Otherwise, it’s a tax.
  • Texture Budget: We limit the number of texture samples per pixel. If you need more than two, you’re probably doing something wrong. Combine your textures or bake your details.
  • Thermal Testing: We run long-duration tests on the worst-case device in our fleet. If it throttles, we add dynamic quality scaling.

It’s not glamorous work. It doesn’t involve writing a new physics engine or a particle system that looks like a supernova. It’s the unglamorous, essential work of making sure your art doesn’t fall apart when someone tries to see it on a phone in a sunny parking lot. The GPU is not your enemy. It’s a partner with a budget. Respect the budget, and the frames will stay.

ADVERTISEMENT Google AdSense
// DISCUSSION TERMINAL

READER ESSAY DISCUSSIONS (0)

Share your architectural perspectives, technical notes, or critique.

LEAVE A THOUGHT Markdown & clean text supported
💬

No discussions recorded yet.

Be the first to spark the dialogue on this essay.

← PREVIOUS ESSAY

The Invisible Cost: Throttling WebGL Post-Processing for Mobile Thermal Limits

NEXT ESSAY →

Why Your Grid is Broken: The Math You Ignored