Blurry textures after renaming LOD tiers
We renamed our LOD tiers and every baked texture dropped to the lowest resolution. A lookup table silently fell back to its minimum. How we found it, and the one-line rule that stops it.
Contents
We renamed our LOD tiers, and every texture we baked afterwards came out at the lowest resolution, including the closest, most detailed tier. No test failed. No gate complained. The maps were just soft, everywhere.
This post is about why that happened, how it stayed invisible, and the small rule that would have caught it on day one. If you have a table keyed by names and a default for "not found", you have the same bug waiting.
What we changed
Our asset builder makes four levels of detail per model. They used to be named after a quality
word: high, standard, low, extreme_low. We moved to plain numbered names: lod0,
lod1, lod2, lod3. The change was done for a good reason. The old names meant different
things in different places, and the numbered ones do not.
The rename went through the LOD code and its tests. The tests passed.
What we missed
The texture bake has its own table. It says how many texels per metre each tier gets. Ours was keyed by the old names, and it lived in a profile file, away from the LOD code:
[bake.texels_per_metre]
high = 256
standard = 128
low = 64
extreme_low = 32
The function that reads the table looked up the tier name. When it found nothing, it returned the smallest value in the table instead of raising an error. Roughly:
def tpm(tier):
table = profile.texels_per_metre
return table.get(tier, min(table.values()))
With lod0 as the tier, get finds no key. So it returned 32. Every tier, lod0 through
lod3, baked at 32 texels per metre. The closest tier should have used 256.
That is an eight-fold difference in each direction, so up to 64 times fewer texels per
surface for lod0. The maps were not broken. They were valid images, with the right content,
just far too small. Nothing in a pipeline checks "this texture is blurry" on its own.
How we found it
We did not find it by looking at a texture. We found it while reading an outside research report about texture baking, and checking each claim in it against our code. One claim was about density per LOD. When we went to read where our density came from, the lookup did not match the new names, and the fallback was right there on the next line.
So the lesson is partly about process. The bug was in code we had not touched. The rename touched everything that mentioned the old names, except a file that mentioned them in a different place.
The fix
Two changes, both small:
- The table now uses the new names, in the profile file and in the default inside the code:
lod0= 256,lod1= 128,lod2= 64,lod3= 32 texels per metre. - An unknown tier name is now a hard error. The bake stops and prints the names it does know:
def tpm(tier):
table = profile.texels_per_metre
if tier not in table:
known = ", ".join(sorted(table))
raise ValueError(f"unknown LOD tier {tier!r}; known tiers: {known}")
return table[tier]
We ran the 20 bake tests and loaded the real profile to confirm each tier now reads its own density. All passed.
Two honest caveats. This is the quick fix, not the proper one. The better rule is to derive
density from how far away each tier is shown, so it is not a hand-typed table at all. We have
not built that yet. And rebuilds now cost more: lod0 can hold up to 64 times more texels than
before, so bake time and file size both go up. That is the price of the textures being the
size we meant.
Why a fallback was the wrong default
The fallback looked careful when someone wrote it. A missing key should not crash a long build, so pick a safe value. And "the minimum" sounds safe: small, cheap, cannot run out of memory.
But a default only makes sense if the caller can live with it. Here the caller could not. A name that matches nothing is not a case to smooth over. It means two parts of the system disagree about what exists. Returning a number hides the disagreement and carries on with the wrong answer.
A fallback also turns a loud failure into a quiet drift. Nobody notices a texture that is slightly worse. Nobody files a bug for "everything looks a little soft". The cost is paid every day, by every player, and the cause is a missing key in a file nobody has opened in weeks.
A rule you can copy
Anywhere you look up a value by a name that another part of the code owns, ask what happens when the name is not there. If the honest answer is "that should never happen", then make "never" an error that says what was asked and what was available.
For us that means:
- Lookups keyed by tier names, material names, or profile names raise on a miss.
- The error lists the known keys, so a rename shows its own fix.
- Defaults are for values a user may leave out, not for names the code itself produced.
We still want one more thing, which is a gate that reads the finished texture sizes and fails
if lod0 is not larger than lod3. A rule like "the nearest tier has the most texels" is
true by design, cheap to check, and would have flagged this on the first build after the
rename. A check on the output catches the next version of this bug, the one nobody thought of
yet.