rust
Failed to Link Shaders: Hunting an Adreno Compiler Bug on a Galaxy S25
A Pulsar game lost its graphics device on a Galaxy S25 while building its pipelines, in a different place from one launch to the next. The cause was a shader function receiving a large struct by value. This is what the bug looks like, the theories that turned out wrong, the small test program that found it in minutes, why it happens as far as we can tell, and the fix.
The first time a Pulsar game reached the renderer on a phone, it showed a screen telling us the graphics device had been lost. The phone was a Galaxy S25, with an Adreno 830. The same shaders had been drawing scenes on desktop GPUs for weeks. The only clue the system log offered was one line from the driver:
There was no shader name, no line number and no reason. This post is about getting from that line to a cause, which took longer than it should have because the first several explanations we tried were plausible and wrong. The cause, when it came, was small and specific: a shader function that takes a large struct as a plain argument.
The pictures in this post are not from the phone. They are high resolution captures of Zero-Day, a scene by Mike Winkelmann (Beeple) that NVIDIA distributes in its ORCA collection under CC BY 4.0, rendered in the Pulsar editor on a desktop. We use it because almost every pixel of it goes through the code this story is about. The view used here, Measure Seven, is about 1.3 million triangles, and every surface in these frames was written by the mesh G-buffer shader, and that shader is one of the two that would not link.
1. The Symptom
A modern renderer builds many pipelines before it draws its first frame. A pipeline is a set of shader stages together with the fixed state they run with, and creating one is the moment the GPU driver compiles the portable shader code into instructions for its own hardware. Pulsar's default deferred pipeline creates several hundred of them. On the phone, somewhere in that list, one creation call failed, and everything after it was lost with it.
What the screen reported was a lost device, which sent us looking in the wrong direction for a while. Nothing had crashed on the GPU. No dispatch had hung and no memory had been read out of bounds, because at that point nothing had been submitted at all. The failure was in pipeline creation. We render through wgpu, and its Vulkan backend treats an unexpected result from that call as a lost device. It also discards the result code on the way, unless a debugging feature called internal_error_panic is turned on. So the one number that would have told us what kind of failure this was never reached our log.
Recovery then made things worse. After losing a device the engine tries to make a new one, and on the phone that path ran into two further errors, a swapchain semaphore that was still in use and a buffer that could no longer be mapped at creation, and panicked. Those are real bugs in our recovery path and they are separate from this one, but they meant the log ended in a stack of failures with the original one buried at the top.
2. Naming the Shader
The first thing to fix was that we did not know which pipeline had failed. We added a temporary log line immediately before every render and compute pipeline was created, naming the pipeline and its entry points, and rebuilt. The last name printed before the driver's complaint was the culprit, with one caveat: Helio compiles pipelines off the frame thread, so several were usually in flight at once.
The editor was a large help here. Pulsar's editor hosts a small Model Context Protocol server on the local machine, and one of its tools queries the live log. The Android platform plugin streams the phone's log into that same buffer, so every run could be searched from the desk without copying files around.
The first run named a compute shader from the sky: the pass that builds the cloud shadow map. Three other compiles were running at the same time. Then the engine made a second device and compiled the whole list again, and that same cloud shadow shader compiled without complaint. This time the failure was in the mesh G-buffer, the shader that writes albedo, normals, material values and motion for every mesh in the scene.
That is the point at which the bug became confusing. The next run failed on the other cloud shadow entry point, which had compiled fine the run before.
3. Four Wrong Theories
It is worth listing what we believed along the way, because each of these has been the real cause of a mobile shader failure somewhere, and each of them was testable.
The first was the @invariant attribute on the G-buffer's vertex output. It affects how a driver is allowed to optimise position calculations, and mobile compilers have had trouble with it before. We removed it, rebuilt, and failed in the same way. It went back in.
The second was the driver's pipeline cache. A cache that holds a corrupt or incompatible entry can make one machine fail where another succeeds, and it would explain results that differ between runs. We built a version that used no driver cache at all on Android.
The third was concurrency. Several pipelines compiling at once on several threads is legal Vulkan, but a driver bug in that area would produce exactly this kind of drifting failure. We built a version with a lock around compilation, so that the phone only ever compiled one pipeline at a time.
With both the cache off and compilation serialised, we launched the game six times in a row. It failed six times out of six.
That series of launches was the first useful evidence, although not for either theory. Across all of them only two shader modules ever failed. One was the cloud shadow module, with either of its two compute entry points, somewhere around the seventy-seventh compile, in some runs and not in others. The other was the mesh G-buffer, at compile number 343, and it failed both times a launch lived long enough to reach it. One shader failed every time and another failed some of the time, and nothing else ever failed at all.
The fourth theory was that our shader translator was producing invalid code. Pulsar's shaders are written in WGSL, and naga translates them to SPIR-V, the binary form Vulkan drivers consume. Desktop drivers are often forgiving, so an invalid module could work there for a long time. We had the PC emit the SPIR-V for the failing entry points and ran it through spirv-val, the Khronos validator shipped with the Android NDK. All of it passed. The writer options we used were an approximation of the ones wgpu uses at run time, so this was not airtight, but it moved suspicion firmly onto the driver's compiler.
4. A Test Loop That Takes Seconds
Every experiment up to here cost a full build, package, install and launch, which was about six minutes for one sample of a random event. A shader that fails a third of the time needs many samples before "it worked" means anything. At that rate we could have spent days and still fooled ourselves.
So we took the engine out of the loop. The test program is a short C file, built with the NDK's compiler, that runs directly on the phone from /data/local/tmp. It creates a Vulkan instance and device, enables the features the engine uses, loads SPIR-V files, and creates the same compute and graphics pipelines over and over, destroying each one again. It prints the actual VkResult. When a creation fails it throws the device away, makes a new one and carries on. The shaders are the ones naga produces from our WGSL, and the descriptor layouts are read from the disassembly so that they match.
The first useful line it printed was the result code wgpu had been discarding: -13, VK_ERROR_UNKNOWN. The second useful thing it produced was a baseline, after sixty attempts per pipeline:
| Pipeline | Failures in 60 attempts |
|---|---|
| Cloud shadow, resolve | 21 |
| Cloud shadow, kernel | 29 |
| Mesh G-buffer | 60 |
Now we had a reproduction measured in seconds, with failure rates, on the real driver. That changes what you can do. A change to a shader could be tested sixty times before a single engine build would have finished.
5. Narrowing It Down
SPIR-V can be transformed after the fact, which gives a way to ask what the compiler dislikes without editing any source. The NDK ships spirv-opt, and we ran its passes over the failing modules one family at a time.
Two obvious candidates did nothing. Scalar replacement of aggregates, which breaks structs apart into their members, left the failure rate where it was, and so did rewriting memory accesses into SSA form.
Inlining did something. Fully inlining the modules, so that no function calls remain, made every failure disappear. That is a broad change, so we narrowed it by marking functions DontInline selectively and letting the optimiser inline the rest. The failures followed a particular kind of function: ones that receive a large struct by value. Inlining only those, and leaving every other call in place, gave 600 successful compiles out of 600.
There were two such structs. The mesh G-buffer's fragment shader passed the whole camera into a helper that tests whether a pixel is on the clipped side of a view, and again into the helper that computes motion vectors. The cloud shadow shader passed the whole sky frame, a block of several hundred values including arrays of nine and ninety-six four-component vectors, into the functions that decide whether there are clouds and what the cloud layer looks like. We had first suspected a smaller struct holding the sky layers, and the bisection corrected us: that one is passed around by value without trouble, and the frame around it is the one that breaks.
The camera case failed every time. The sky case failed between a third and a half of the time. That difference is why the bug seemed to move: whichever launch you looked at, the sky shader might or might not stop it before it reached the G-buffer.
6. Why It Happens
Here we have to separate what we measured from what we infer, because the compiler in question is closed.
The WGSL looked entirely ordinary:
At the call site this reads as if a reference to the camera is handed over and two of its fields are used. The generated code says something different. WGSL arguments are values. To make this call, the SPIR-V loads the entire struct out of its buffer, every matrix and vector in it, as one composite value, and passes that value to an OpFunctionCall. The function then extracts the two members it wanted.
Compilers normally make that cost vanish. Once the call is inlined, the optimiser can see that only two members are read and load only those. Shader toolchains built on DXC or run through spirv-opt tend to arrive at the driver already inlined. Our path from WGSL does not include an inlining step, so the driver receives the call exactly as written, with the large composite value in it, and has to deal with it itself.
What the Adreno compiler does with it, we cannot see. The facts are that the module is valid SPIR-V according to the validator, that the driver returns an unknown error for it, that removing only these calls removes the failure, and that the same driver fails every time on one such shader and only sometimes on another. Our reading is that a resource or ordering problem inside the compiler is being triggered by the large aggregate, but the intermittence in particular is something we can describe and not explain.
We are also not alone with this compiler. The closest report we found is in wgpu issue 10621, where a comment describes a Quest 3 failing to create multiview pipelines with the same "Failed to link shaders" message when a camera struct is passed into a helper, and succeeding when the fields are passed instead. The same issue began with a related fault on a Snapdragon X laptop, where a minimal reproduction shows an Adreno driver reading the wrong member of a struct passed by value, with structs as small as sixteen bytes. Other projects have worked around neighbouring Adreno compiler failures involving struct copies and struct pointers. Those reports differ in their details, and none that we found describes a failure that comes and goes between identical attempts.
7. The Fix
The fix is to stop asking the driver to do the thing it cannot do, by passing each function the values it reads:
Four functions changed this way. helio_view_clipped now takes the view flags and the clip plane. The motion vector helper takes the two camera matrices it uses. The two cloud functions take the planet parameters, the medium and the layer block they need, and no longer the sky frame that holds them. The callers sit in the mesh, foliage and sky plugins, because in Pulsar every kind of object is its own plugin and each owns its shaders, while the shared helpers live in Helio.
We then put the fixed shaders back through the test program on the phone. Each of the three pipelines that had been failing compiled 300 times out of 300, against 21, 29 and 60 failures in 60 before. With that in place the game builds its pipelines on the phone and gets past the point where it used to lose the device.
The change costs nothing elsewhere. Desktop compilers were inlining these calls and loading only what was read, so their output does not meaningfully change, and where a compiler does not inline, the new form moves less data.
8. What We Took From It
The rule that came out of this is now written in the engine's documentation next to the Android build notes: do not hand a large struct to a shader function by value; pass the fields the function reads. It is an easy rule to follow and an easy one to forget, since the offending code is clean, readable and correct. A few sky functions still receive their frame this way without having failed, and they are the first place we will look if the driver complains again. A more general protection would be to inline in our shader translation path before the driver ever sees the module. That would be a larger change with its own consequences for compile times and debugging, and we have not taken it.
Some lessons are about method more than about Adreno. A validator passing means the code is legal, and says nothing about whether a given compiler can digest it. A failure that moves between runs invites theories about timing, caches and threads, and the way to dispose of them is to count: six launches out of six with both removed was worth more than any argument. And when each sample costs six minutes, the most valuable thing you can build is whatever makes a sample cost two seconds. The test program is a few hundred lines of C that nobody will ever ship, and it did in one sitting what hours of rebuilding had not.
Getting the game onto the phone at all involved a good deal more than shaders. That is a story about platform plugins, and we tell it in One Build Button, Seven Platforms.