glGenerateTextureMipmap
Builds every smaller mip level of a texture from its level 0, by ID.
Signature
void glGenerateTextureMipmap(GLuint texture);
How it works
The DSA form of glGenerateMipmap. Each level is half the size of the one above, filtered down by the driver, until 1×1. Call it after the level-0 pixels are uploaded, and again whenever they change (a texture you render into, for example).
With glTextureStorage2D the levels must already be allocated: pass levels = 1 + floor(log2(max(w, h))), or there is nowhere to write them.
Parameters
The texture whose level 0 is already filled.
Tips & pitfalls
Direct State Access (GL 4.5): the object is passed as a parameter, so nothing needs to be bound and no global state is disturbed.
Compressed textures (BC7, ASTC…) cannot generate mipmaps on the GPU: compress every level offline and upload each one.
Errors
When the call is invalid it does nothing and sets an error, which glGetError returns:
- GL_INVALID_OPERATION
- The texture is a multisample or buffer texture, or its format cannot be filtered.
Example
1glTextureSubImage2D(tex, 0, 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
2glGenerateTextureMipmap(tex);