Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does glGenerateMipmap() on iOS device take so much client memory?

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.

like image 582
godspeed1024 Avatar asked Oct 17 '22 20:10

godspeed1024


1 Answers

godspeed.

  1. You may need to set the value of GL_TEXTURE_MAX_LEVEL(default is 1000) and GL_TEXTURE_BASE_LEVEL(default is 0).
  2. 4K resolution image does not mean "4K texture". As my experience, if the length of image is 1025 X 1025, then the texture is 2048 X 2048. Please examine the size of the image.
  3. "cpu memory" --> "main memory"; "gpu memory" --> "graphics memory", the latter maybe suitable.
like image 197
Viton Zhang Avatar answered Oct 20 '22 17:10

Viton Zhang