PDA

View Full Version : Texture mipmapping issue



sandman
04-11-2013, 06:40 AM
Hi
I've got some experience with old fashioned OpenGL (with glBegin etc) and I'm trying to learn the newer versions. I'm trying to render a texture and failing. I have all my shaders, VAOs and texture loading libraries etc in place and working, but binding and drawing the texture is really fiddly. The following code for the binding works, sort of:


glGenTextures(1,&id);
glBindTexture(GL_TEXTURE_2D,id);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,res.x,res.y,0 ,GL_RGBA,GL_FLOAT,pix);
glGenerateMipmap(GL_TEXTURE_2D);

But it causes memory leaks. When I create and destroy many of these textures, the memory usage explodes. Commenting out this line fixes the memory leak:


glGenTextures(1,&id);
glBindTexture(GL_TEXTURE_2D,id);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,res.x,res.y,0 ,GL_RGBA,GL_FLOAT,pix);
//glGenerateMipmap(GL_TEXTURE_2D);

But now it doesn't draw at all (or it draws a transparent texture instead). It shouldn't need a mipmap, should it?
Also, it seems that the memory leak doesn't appear for texture sizes below a certain threshold. A 256x256 texture works perfectly, with no leaks, but a 256x257 texture leaks like crazy!
Should I be deleting the mipmaps when I call glDeleteTextures(1,&id); or something?
I've been working on this one issue all day, and I'm at the point where I'm tempted to accept the memory leak. Any help would be greatly appreciated.

menzel
04-11-2013, 07:05 AM
Hello,


glDeleteTextures(1,&id) should be all you need for deleting the mipmaps. You see a black texture when you don't create mipmaps because your texture filtering is mipmapped by default and without mipmaps your texture is incomplete and all texture() calls in your shader will return 0. Check http://www.opengl.org/sdk/docs/man/xhtml/glTexParameter.xml to see how to disable mipmapping.
But back to the problem: I don't see why it should leak...

mobeen
04-11-2013, 08:48 AM
Hi sandman,
Are your deallocating the pixel data (pix) after you have passed it to gltexImage2D function? That might be one reason for memory leak.

sandman
04-11-2013, 06:08 PM
Hi mobeen,
The leak is definitely due to glGenerateMipmap, because the I only have to comment or uncomment that one line to make the leak disappear and reappear. I've also tested the pix pointer directly, and it's at the same location each time it's allocated (which would only happen if it was correctly deallocated). Another weird thing is that the texture ID is always 1, regardless of how many times I create it or destroy it, which means that the texture is also being correctly deallocated.

I've solved the memory leak and got it to draw correctly by disabling mipmaps (thanks menzel!), but this is just a work around. I'm starting to think it's a driver issue, because I've read elsewhere that ATI has some bugs in its openGL implementation (I'm using an AMD Radeon HD 7900).

richardGe
05-28-2013, 08:26 AM
sandman,

Maybe that will solve:

glBindTexture(GL_TEXTURE_2D,0);
glDeleteTextures(1,&id);
glFinish();