I am working on an iOS application that displays images using OpenGL ES. Here's the key code fragment of my function for setting parameters and texture data:
glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glPixelStorei(GL_PACK_ALIGNMENT, 4); glPixelStorei(GL_UNPACK_ALIGNMENT, 4); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixelData);
This works very well on the iPhone, but with anti-aliasing anti-aliasing. So I tried using mipmap, changing the previous codes as follows:
glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);// for mipmapping glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glPixelStorei(GL_PACK_ALIGNMENT, 4); glPixelStorei(GL_UNPACK_ALIGNMENT, 4); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixelData); glGenerateMipmap(GL_TEXTURE_2D);// This will generate all mip-mapping textures
And this mipmapping works well, no longer smoothes during scaling.
However, I found that mipmapping takes up too much memory: not graphics memory, but main memory (in terms of OpenGL, not on the server side, but on the client side)!
I confirmed that with Memory Monitor of Instruments:
Without mipmapping, my texture setting function is unlikely to spend any part of the memory, even if the texture source is a 4 KB image. This result clearly indicates that glTexImage2D allocates memory only on the OpenGL server, that is, on the graphics card, so the Memory Monitor cannot control the Monitor, which does not care about the use of graphic memory.
But using mipmapping using glGenerateMipmap (), heap memory usage quickly increased by about 100 million when I create a 4K texture. And after glDeleteTextures () it returns.
It seems that glGenerateMipmap () not only generates mip map textures in graphic memory, but also consumes main memory. And this part of the main memory can be recycled after removing the texture. Very strange, isn't it?
I wonder why. And my key point is how to use mip mapping without excessive memory usage? Thanks for any help.