Search code examples
androidopengl-estexturesmipmaps

How can I create and mipmap compressed textures for OpenGL ES 2.0 on board (Android)?


I'm working through the openGL ES 2.0 guide, and I just stumbled on this note for the GenerateMipmap(target) command:

If the level zero array is stored in a compressed internal format, the error INVALID_OPERATION is generated.

The way I understand that is that there is no function to automatically generate mipmaps for compressed textures in openGL ES 2.0 - and everything else I've looked at seems to indicate that, even though I've never seen it concretely confirmed anywhere. I've seen that the Mali texture compression tool can automatically generate mipmaps - but that only works on Windows, Linux and OS X - and not on Android. I need my app to compress the textures and mipmap them (the idea is that a user can take an image and apply it as a texture - I've already taken care of the aspect that the texture has to have a widths and heights of 2^n and 2^m respectively). The ETC1Util takes care of compressing textures (although it only takes care of the ETC1 format, and I'm still looking for a solution for textures with an alpha channel - but that's a different question for a different day, if Google doesn't help me there).

My question is: how do you compress and mipmap textures - most importantly ETC1, but ideally also PVRTC, ATITC, S3TC and ETC2 - at runtime on Android?


Solution

  • There's nothing in OpenGL or OpenGLES for generating compressed textures.

    Generally, creating compressed textures is slow, and people almost always generate them offline. For runtime generated textures, people usually keep them in basic formats like 8888, 565 or 4444 because the trade-off of compression-time vs rendering efficiency is just not worth it.

    That said, it's possible to find open source code for generating most compressed formats, and there's nothing stopping you from plugging that code into your game code. For ETC1 there's this (github version). For S3TC (aka DXT) there's squish (or many others).