How many mipmaps to generate with glTextureStorage2D

When using glTextureStorage2D, the levels parameter, as I understand specifies the number of mipmap levels to generate. But how does one decide how many to generate? Would it depend on the texture resolution? Should one create more mipmaps for higher resolution texture?

It depends upon the minimum scale factor. There’s no real need to allocate levels which will never be used.

But in most cases you’d either have only the base level (if you aren’t going to use a mipmap-based minification filter) or all of them (i.e. floor(log2(size))+1 levels).

A single additional level increases the memory requirement by 1/4. A complete set of levels increases it by 1/3.

Thank you! Can I ask something else. When using glGenerateMipmap with older API glTexImage2D, does it generate the maximum number of mipmaps by default (using the formula you specified)? But with glTextureStorage2D we can specify the number we want, and if we want the max number we have to calculate it ourselves?

IMO this was a failing in the spec; OpenGL should have used the same behaviour as Direct3D and allowed a value of 0 to specify a complete mipmap chain.

glGenerateMipmap generates all levels other than the base level. For a texture created with glTexImage2D, that’s down to 1×1.

IIRC, it generates between GL_TEXTURE_BASE_LEVEL+1 and GL_TEXTURE_MAX_LEVEL (clamped by log2(maxsize) of course).

So you can limit the MIP gen, if desired, to not generate the lowest res MIPmaps. There can be a decent MIPmap gen perf improvement in doing so, if you’re generating these at runtime, and you know you won’t need them. But if you’re generating these on startup, just generate them all.

(Apologies if this was already covered. But it didn’t look like it.)

1 Like