glDeleteTextures does not delete pixel data

Started by
6 comments, last by Guy Joel McLean 10 years, 8 months ago

Afternoon all,

I have a class called Texture, that I need to be able to initialize once on my programs startup with a texture, and then at runtime replace the texture with one of the users choosing from their HDD. Initializing the Texture works fine, and the initial texture displays as it should, being given a name by glGenTextures, and then binding the pixel data under that name. That is done in the following functions:


Texture::Texture(string filename)
{
//textureID[0]=0;


const char* fnPtr = filename.c_str(); //our image loader accepts a ptr to a char, not a string
//printf(fnPtr);


lodepng::load_file(buffer, fnPtr);//load the file into a buffer


unsigned error = lodepng::decode(image,w,h,buffer);//lodepng's decode function will load the pixel data into image vector from the buffer
//display any errors with the texture
if(error)
{
cout << "\ndecoder error " << error << ": " << lodepng_error_text(error) <<endl;
}
//execute the code that'll throw exceptions to do with the images size
checkPOT(w);
checkPOT(h);




//loop through and //printf our pixel data
/*for(GLuint i = 0; i<image.size(); i+=4)
{
//printf("\n%i,%i,%i,%i,", image.at(i),image.at(i+1),image.at(i+2),image.at(i+3));


}*/


////printf("\nImage size is %i", image.size());


//image now contains our pixeldata. All ready for OpenGL to do its thing


//let's get this texture up in the video memory
texGLInit();


Draw_From_Corner = CENTER; 
}

void Texture::texGLInit()
{
glGenTextures(1, &textureID[0]);
////printf("\ntextureID = %u", textureID[0]);

glBindTexture(GL_TEXTURE_2D, textureID[0]);//evrything we're about to do is about this texture
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8,w,h,0, GL_RGBA, GL_UNSIGNED_BYTE, &image[0]);
//we COULD free the image vectors memory right about now.
image.clear();
}
 

Upon calling my draw function this texture displays just fine. However, upon clicking "import new spritesheet", an openFileDialogue appears and the user selects a new file. The following code executes, which should (in theory) delete the texture name (stored in the GLUint 'TextureID' array), and make it available for use again, allowing us to bind new texture data under that name.


void Texture::reloadTexture(string filename)
{
	//first and foremost clear the image and buffer vectors back down to nothing so we can start afresh 
	buffer.clear();
	image.clear();
	w = 0;
	h = 0;
	//also delete the texture name we were using before
	glDeleteTextures(1, &textureID[0]);

	const char* fnPtr = filename.c_str(); //our image loader accepts a ptr to a char, not a string
	//printf(fnPtr);

	lodepng::load_file(buffer, fnPtr);//load the file into a buffer

	unsigned error = lodepng::decode(image,w,h,buffer);//lodepng's decode function will load the pixel data into image vector from the buffer
	//display any errors with the texture
	if(error)
	{
		cout << "\ndecoder error " << error << ": " << lodepng_error_text(error) <<endl;
	}
	//execute the code that'll throw exceptions to do with the images size
	checkPOT(w);
	checkPOT(h);
	


	//loop through and //printf our pixel data
	/*for(GLuint i = 0; i<image.size(); i+=4)
	{
	//printf("\n%i,%i,%i,%i,", image.at(i),image.at(i+1),image.at(i+2),image.at(i+3));

	}*/

	////printf("\nImage size is %i", image.size());

	//image vector now contains our pixeldata. All ready for OGL to do its thing

	//let's get this texture up in the video memory OpenGL
	texGLSecondaryInit();

	Draw_From_Corner = CENTER;
}

void Texture::texGLSecondaryInit()
{
	//PFNGLBINDBUFFERARBPROC glBindBuffer = NULL;                  // VBO Bind Procedure
	

	glGenTextures(1, &textureID[0]);
	////printf("\ntextureID = %u", textureID[0]);

	glBindTexture(GL_TEXTURE_2D, textureID[0]);//evrything we're about to do is about this texture
	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
	//glBindBuffer(GL_PIXEL_UNPACK_BUFFER,0);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8,w,h,0, GL_RGBA, GL_UNSIGNED_BYTE, &image[0]);
	
	//we COULD free the image vectors memory right about now.
	image.clear();
}

*Note the call to 'glDeleteTextures' close to the start of 'reloadTexture()'.*

What should happen at this point is that the user now sees their selected texture being displayed. But what actually happens is that the user sees the old texture (the one that loads at startup), with the new texture's (the one they selected) dimensions. Meaning that despite my call to glDeleteTextures, the deleted texture's pixel data still exists in one of openGL's buffers somewhereangry.png I can't figure out where, nor why it would still be there after i have not only deleted it, but also attempted to bind a different files data over the top of it.

Thanks for reading, and I'd appreciate any light that could be shed on why openGL would hang-on to the old data.

P.S. In the function 'texGLSecondaryInit()' , you may notice commented out calls to glBindBuffer(). This was suggested to me as a way to purge the pixel buffer on another board. It caused Access Violation Errors all over the shop. However, I've left it on display here, in case it helps you understand what I'm trying and failing to do.

Advertisement

Also, this is my first post here on GDNet. I was unable to find the forum rules before posting, so apologies if i have violated any of them.

Are you use the that the pixel alignment is 1 in the texture you are trying to load. Also another thing you can do is put glGetError() between your call and see where it is failing.

The only thing that glDeleteTextures promises is that the tetxure name will be available for reuse (via a subsequent call to glGenTextures); it doesn't promise that it will delete the pixel data, and the driver is perfectly free to keep that data hanging around for subsequent reuse. This can be a good thing as the driver may be able to get away without having to allocate a new block of storage - "here's a block that's no longer being used, just hand it back".

So not deleting the pixel data is expected behaviour, but when you make the next call to glTexImage (assuming that you've bound the correct texture, of course) then the texture should be completely respecified.

The only sensible explanation here is that your glTexImage call is somehow failing. As suggested, try a few glGetError calls and double-check your parameters.

Are you use the that the pixel alignment is 1 in the texture you are trying to load. Also another thing you can do is put glGetError() between your call and see where it is failing.

Check the glPixelStore call in the OP's code...

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

I can't see why they wouldn't be. They're both .png files, loaded into openGL using the same image loader. They also both worked on a previous project using the virtually same setup. However in that old project, they were both created at the startup of my program as separate instances of my 'Texture' class. The reason I can't do that here, is because i wish for the user to be able to select a new texture at runtime from a file.

Also placing glGetError after my glDeleteTextures call, returns no errors. Like so.


void Texture::reloadTexture(string filename)
{
	//first and foremost clear the image and buffer vectors back down to nothing so we can start afresh 
	buffer.clear();
	image.clear();
	w = 0;
	h = 0;
	//also delete the texture name we were using before
	glDeleteTextures(1, &textureID[0]);

	GLenum err;
	while ((err = glGetError()) != GL_NO_ERROR) {
		printf("OpenGL error: %u", err);
	}


	const char* fnPtr = filename.c_str(); //our image loader accepts a ptr to a char, not a string
	//printf(fnPtr);

	lodepng::load_file(buffer, fnPtr);//load the file into a buffer

	unsigned error = lodepng::decode(image,w,h,buffer);//lodepng's decode function will load the pixel data into image vector from the buffer
	//display any errors with the texture
	if(error)
	{
		cout << "\ndecoder error " << error << ": " << lodepng_error_text(error) <<endl;
	}
	//execute the code that'll throw exceptions to do with the images size
	checkPOT(w);
	checkPOT(h);
	


	//loop through and //printf our pixel data
	/*for(GLuint i = 0; i<image.size(); i+=4)
	{
	//printf("\n%i,%i,%i,%i,", image.at(i),image.at(i+1),image.at(i+2),image.at(i+3));

	}*/

	////printf("\nImage size is %i", image.size());

	//image now contains our pixeldata. All ready for  to do its thing

	//let's get this texture up in the video memoryOpenGL
	texGLSecondaryInit();

	Draw_From_Corner = CENTER;
}

The same goes for after the call to glGenTextures in texGLSecondaryInit(). No errors to report.sad.png

The only thing that glDeleteTextures promises is that the tetxure name will be available for reuse (via a subsequent call to glGenTextures); it doesn't promise that it will delete the pixel data, and the driver is perfectly free to keep that data hanging around for subsequent reuse. This can be a good thing as the driver may be able to get away without having to allocate a new block of storage - "here's a block that's no longer being used, just hand it back".

So not deleting the pixel data is expected behaviour, but when you make the next call to glTexImage (assuming that you've bound the correct texture, of course) then the texture should be completely respecified.

The only sensible explanation here is that your glTexImage call is somehow failing. As suggested, try a few glGetError calls and double-check your parameters.

Are you use the that the pixel alignment is 1 in the texture you are trying to load. Also another thing you can do is put glGetError() between your call and see where it is failing.


Check the glPixelStore call in the OP's code...

Ok, thanks v. Much. I wil try more and post results :)

Ok I've done some further testing on this matter today. glGetErrors after my glTexImage2d call returns no errors.

Even if, rather than changing the Texture via a call to:


myTex->reloadTexture("filename");

I just create a brand new one like so:


mytex = new Texture("filename");

It still simply resizes the texture.

Note: that this new method that I've tried means that glDeleteTextures would never even be called. Meaning that OGL would not free the TextureID (or Texture Name) in use for the first texture for re use, therefore openGL would give a unique name to the new Texture when the constructor was called.

Knowing this, I have observed the TextureID variable.

After the very first call to glGenTextures, textureID[0] = 1 |---->Seems normal.

After the myTex = new texture("filename"); call (which calls glGenTextures again), textureID[0] = 1 |----> Woah! hold it!!

At this point '1' should no longer be available as a unique texture name, as it was previously created for the first texture and glDeleteTextures was never called.

Can anybody tell me why glGenTextures would refuse to give me a unique name? Usually this problem comes because glGenTextures is called before a context is established, and glGenTextures gives you '0' as a name. But we know i have a legit context, as I draw to it!

Also, I have put glGetErrors as I have above after glGenTextures. No errors dry.png

GOT IT! You see above, where i wrote:

"But we know i have a legit context, as I draw to it!"

Turns out I didn't. Let me explain.

In my program, I have TWO OpenGL views. Running on TWO OpenGL contexts. Which i must switch between using WGLMakeCurrent(), in order to make them both flush, swapbuffers individually.

The OpenGL view i refer to in this question, is the first to be made current. It has the first texture bound and then it is drawn. At this point the second OGL view is made current, has some stuff happen to it (not important), and then is drawn.

So when I attempt to load and bind a second texture to my first openGL view, I've pulled the "old switcheroo" on myself and am trying to bind it to my second view's context (because it is current at this point). Which explains why the first context still had the old pixel data in it. As I'd technically never called glDeletetextures on ittongue.png biggrin.png

A quick call to wglMakeCurrent(handleInHere, parentwindowhere), before myTex->reloadTexture("filename") call, and my textures are loading and replacing themselves at my beckoned call.

Thanks for your previous help guys.

This topic is closed to new replies.

Advertisement