19

I was wondering if the quality of texture mipmaps would be better if I used my own algorithm for pre-generating them, instead of the built-in automatic one. I'd probably use a slow but pretty algorithm, like Lanczos resampling.

Does it make sense? Will I get any quality gain on modern graphics cards?

GhassanPL
  • 2,679
  • 5
  • 32
  • 40
  • Unity proposes either `box` or the `Kaiser` filter to generate the mipmap chain. it is definitely a legit question. – v.oddou Jan 24 '14 at 08:47
  • I think you are mixing two things together. Interpolation is, when you need more data than you have. Mipmaps are for the case, when you need less data than you have. Lanczos is the interpolation algorithm, it has nothing to do with mipmaps. – Ivan Kuckir Oct 16 '17 at 13:34

4 Answers4

14

There are good reasons to generate your own mipmaps. However, the quality of the downsampling is not one of them.

Game and graphic programmers have experimented with all kinds of downsampling algorithms in the past. In the end it turned out that the very simple "average four pixels"-method gives the best results. Also more advanced methods are in theory mathematical more correct they tend to take a lot of sharpness out of the mipmaps. This gives a flat look (Try it!).

For some (to me not understandable) reason the simple average method seems to have the best tradeoff between antialiasing and keeping the mipmaps sharp.

However, you may want to calculate your mipmaps with gamma-correction. OpenGL does not do this on it's own. This can make a real visual difference, especially for darker textures.

Doing so is simple. Instead of averaging four values together like this:

float average (float a, float b, float c, float d)
{
  return (a+b+c+d)/4
}

Do this:

float GammaCorrectedAverage (float a, float b, float c, float d)
{
  // assume a gamma of 2.0 In this case we can just square
  // the components. 
  return sqrt ((a*a+b*b+c*c+d*d)/4)
}

This code assumes your color components are normalized to be in the range of 0 to 1.

Nils Pipenbrinck
  • 83,631
  • 31
  • 151
  • 221
  • 13
    Actually, the "average four pixels" may not be the best way to create mipmaps. http://www.number-none.com/product/Mipmapping,%20Part%201/index.html – Marco Mustapic Nov 16 '09 at 14:51
2

As an addition to this question, I have found that some completely different mipmapping (rather than those simply trying to achieve best down-scaling quality, like Lanczos filtering) algorithms have good effects on certain textures.

For instance, on some textures that are supposed to represent high-frequency information, I have tried using an algorithm that simply takes one random pixel of the four that are being considered for each iteration. The results depend much on the texture and what it is supposed to convey, but I have found that it gives great effect on some; not least for ground textures.

Another one I've tried is taking the most deviating of the four pixels to preserve contrasts. It has even fewer uses, but they do exist.

As such, I've implemented the option to choose mipmapping algorithm per texture.

EDIT: I thought I might provide some examples of the differences in practice. Here's a piece of grass texture on the ground, the leftmost picture being with standard average mipmapping, and the rightmost being with randomized mipmapping:

Average mipmapping, trilinear filtering Random mipmapping, trilinear filtering

I hope the viewer can appreciate how much "apparent detail" is lost in the averaged mipmap, and how much flatter it looks for this kind of texture.

Also for reference, here are the same samples with 4× anisotropic filtering turned on (the above being tri-linear):

Average mipmapping, 4× anisotropic filtering Random mipmapping, 4× anisotropic filtering

Anisotropic filtering makes the difference less pronounced, but it's still there.

Dolda2000
  • 25,216
  • 4
  • 51
  • 92
  • nice ideas. the `one random` is like a `nearest` but trying to break aliasing with randomness. `nearest` always gives sharper results indeed. – v.oddou Jan 24 '14 at 08:41
2

It depends on the kind of assets you display. Lanczos filter gets closer to ideal low-pass filter and the results are noticeable if you compare the mip maps side by side. Most people will mistake aliasing for sharpness - again it depends whether your assets tend to contain high frequencies - I've definitely seen cases where box filter was not a good option. But since the mip map is then linearly interpolated anyway the gain might not be that noticeable. There is another thing to mention - most people use box filter and pass the output as an input into the next stage - in this way you lose both precision and visual energy (although gamma will help this one). If you can come up with code that uses arbitrary filter (mind you that most of them are separable into two passes) you would typically scale the filter kernel itself and produce mip map levels from the base texture, which is a good thing.

baze
  • 21
  • 1
  • good idea to keep the high res texture as the source for all mipmaps. just to add one thing : lanczos and cie do add ringing with larger kernels. care to be taken here. – v.oddou Jan 24 '14 at 08:44
2

What is motivating you to try? Are the mipmaps you have currently being poorly generated? (i.e. have you looked?) Bear in mind your results will often still be (tri)linearly interpolated anyway, so between that an motion there are often steeply diminishing returns to improved resampling.

simon
  • 7,044
  • 2
  • 28
  • 30
  • Well, the current algorithm can't be very slow, and therefore can't be that good. The cost of pregenerating and storing the mipmaps is low, but I don't know if it's worth it if the results aren't going to be that noticeable. – GhassanPL May 06 '09 at 21:50
  • I'm not sure how you're doing it now, but these days you can have the GPU generate them, which can be fast and pretty good. More importantly though, you are either looking at these at the wrong resolution, or more likely, linearly interpolating between them. So your beautifully crafted hand-resampled versions usually aren't seen... hence diminishing returns. Are you actually seeing artifacts, or is this purely academic? – simon May 06 '09 at 22:06
  • depends on your buisness budget. free time thing ? investor funded ? your salary etc... if it takes one week to work on that, it will cost about 1000$ or more to the company, is it worth it really ? – v.oddou Jan 24 '14 at 08:46