Gamma and Mipmapping


For the next few weeks, my plan is to try and talk about the last few gamma things that no one tells you about and then I can stop talking about it. Today’s topic is gamma and mipmapping.

As you should know by now, the value 187 is actually half-way between o and 255. So suppose that the image up top is a texture in a game, and we want to mipmap it. What values should the left and right side converge to? Well, since 187 is half-way between 0 and 255, we would want it to be 187.

The naive way to calculate mipmaps is to just average all 4 sample points. Btw, I’m assuming the four samples are normalized between 0.0f and 1.0f. The pseudocode would look like:

float4 sourceSamples[4]; // the four source samples
float4 finalSample = 0;
for (int i = 0; i < 4; i++)
    finalSample += sourceSample[i];
finalSample /= 4;
return sourceSample;

But that’s not actually what we want, because then half-way between 0 and 255 would be 128, which is not correct. The solution is to use gamma correction. When we read the samples, we should convert them to linear (as in, pow(x,2.2)) and then when we write the value, we should convert from linear to gamma (as in, pow(x,1/2.2)). Here is the pseudocode to do it.

float4 sourceSamples[4]; // the four source samples
float4 finalSample = 0;
for (int i = 0; i < i++)
    finalSample += pow(sourceSample[i],2.2);
finalSample /= 4;
return pow(sourceSample,1/2.2); // 1/2.2 ~= .454545

That’s how the NVIDIA Photoshop plugin does it. Let’s take our image, and save it as a .dds file.

Click on that “MIP Map Filtering…” button. The defaults should look like this:

If you save with those settings, you get this final image:

That’s no good. Looking at the mipmaps, 0 and 255 were averaged to 128. Instead, let’s change the setting to this one and save again.

Then we get this image:

One thing: you might wonder why in the largest mipmap that the left and right side are 128. Well, the image is too wide to fit in the column, so I’m letting the browser scale it in half. But most browsers don’t perform gamma correction when they downscale images. If you right-click and view the image by itself or save it to disk, it will look right.

And that’s it! Now, how important is this effect? It’s not the biggest issue in the world, but if you don’t do it, you will often notice that textures get darker in the distance. But you may as well fix it, or at least use the right number when you let NVIDIA create mipmaps for you.

10 Responses to “Gamma and Mipmapping”

  1. Cool, thanks John.
    A whole new “gamma” corrected world :)


  2. Must read!


  3. Hi, if I understood correctly, in both code snipets you forgot to index the array (sourceSample) inside the FOR loop and in the last line is returning the array, instead of the actual answer (finalSample).

    Great blog by the way :)


  4. Thx. Fixed. Btw, if anyone knows how to make wordpress understand < and >, I’d love to know.


  5. [...] http://filmicgames.com/archives/327 [...]


  6. Oldest reference in the games industry I know for “you need to de-gamma/re-gamma” (or whatever this should be caleld) is this: Blow, Jonathan, “Mipmapping, Part 2,” Game Developer, vol. 9, no. 1, pp. 16-19, Jan. 2002

    I believe the simulator guys knew and compensated for this problem before then, but I don’t have a reference.


  7. Mitul K. Gupta | Nov 5th 2012 – In Drilling purposes, keeping balance in the components is usually a big problem. For this goal a large number of choices are accessible, however the most well-known and thoroughly utilized approach remains the addition of chemical compounds, better known as Stabilizers..
    nike blazer mi haute http://www.couleurs-et-matieres.fr/nike-lunar.asp?id=nike-blazer-mi-haute


  8. 888 On this earth structure, every single product has become extravagant. Meanwhile, he is also performing because of the sweatshop, louis vuitton sale handbags operate by Chinese bosses, speedy 35 louis vuitton that manufacture the counterfeit products for his gross sales force.
    soccer jerseys china http://www.gottschalkpolledherefords.com/soccer.html


  9. 888 On this world model, virtually every product has become extravagant. In the meantime, he is also performing aided by the sweatshop, vintage louis vuitton bags for salelouis vuitton epi handbags run by Chinese bosses, chicago louis vuitton that manufacture the counterfeit goods for his income force.
    cheap air jordan 3 retro http://www.emilycobett.com/cheap-air-jordan-3-c-1.html


  10. 888 On this world pattern, each individual product has become extravagant. In the meantime, he’s also doing the job while using the sweatshop, discounted louis vuitton bags operate by Chinese bosses, louis vuitton usa bags that manufacture the counterfeit items for his profits power.
    nba jerseys china http://www.qu.org/nba-jerseys-china-c-16.cfm


Leave a Reply