I'm working on an iOS app which renders images with OpenGL ES. Here's key code snippet of my function for setting up texture parameters and 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 just fine on iPhone, with aliasing when zooms out, however. So I tried to use mipmap, by modifying previous codes as following:
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 more aliasing during zooming out.
However, I found mipmapping took too much memory: Not graphics memory, but main memory(in OpenGL terms, not server-side memory but client-side memory)!
I confirmed that with Memory Monitor of Instruments:
Without mipmapping, my texture setting up function hardly spends any piece of memory, even if texture source is a 4K resolution image. Such result clearly proves that glTexImage2D only allocate memory in OpenGL server, i.e., graphic card, thus it cannot be monitored by Memory Monitor which do not care about graphics memory usage.
But with mipmapping using glGenerateMipmap(), the heap memory usage quickly increased by about 100M when I create a 4K texture. And after glDeleteTextures(), it falls back.
It seems that glGenerateMipmap() not only generate mip map textures in graphics memory, but also consume main memory. And this piece of main memory can be recycled after the texture is deleted. Very strange, isn't it?
I'm wondering why. And my key point is, how to use mip mapping without too much memory usage? Thanks for any help.
godspeed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With