PDA

View Full Version : How to check if the texture is resident in memory



robert_s
02-26-2002, 09:06 AM
Hi!
I want to check if the single texture I loaded previously is still present in memory before I actually delete it.
I found out that I can actualy use two bethods to query if my texture is resident in memory.

frist method is "void glGetTexParameteriv( GLenum target, GLenum pname, GLint *params)"

and second is "GLboolean glAreTexturesResident( GLsizei n, GLuint *textures, GLboolean *residences)"

I guess teh difference between those two methods are that method one gets info about a single tex and second method about a group of textures! Am I right? if not then please let me know!!

Now. My question is how to use them? I spend some time playing around with those 2 methods and I am not sure how to use them! Could anyone help me with that!!
For example consider the following code:

GLuint m_unTgaTexture; // data member of the CTexture class

void CTexture::loadTextureTGA(char *filename, GLuint pixFormat, GLenum minfilter, GLenum magfilter, GLenum wrap)
{
// Build A Texture From The Data
glGenTextures(1, &m_unTgaTexture); // Generate OpenGL texture IDs
glBindTexture(GL_TEXTURE_2D, m_unTgaTexture); // Bind the Texture
.. bla bla
}

// here is the problem!!!
void CTexture::destroyTexture()
{
//glGetTexParameteriv( GL_TEXTURE_2D, GL_TEXTURE_RESIDENT, ? ); // <<-- here I want to check first if texture is present in memory

glDeleteTextures(1, &m_unTgaTexture); //then destroy the texture
}

Do u think that this funcion glGetTexParameteriv will do the work or I need to use glAreTexturesResident. Which one?

Could you just show me how to use the appropriate function as I am lost! thanks for any help!! http://www.opengl.org/discussion_boards/ubb/smile.gif

fritzlang
02-26-2002, 10:24 AM
Things like these are all in the redbook
It is online on this site, check the
textures chapter.

OpenGL Programming Guide Version1.0:
http://www.opengl.org/developers/code/tutorials.html

zeckensack
02-26-2002, 10:34 AM
Agreed, very basic stuff.

You have to destroy the texture objects REGARDLESS. It doesn't matter wether they are resident or not.

With glAreTexturesResident(1,&tex_obj_number,&where_to); you can easily check if a single texture is resident. Also watch out for the return value of the function.

I suggest you read the 1.3 spec (get it off the main site). It's properly explained there and also notes the pitfall that implementations are free to LIE about it.

robert_s
02-26-2002, 11:45 AM
The problem is that I already found the theory bit about glGetTexParameter function. Unfortunately I still don't get how to use it.

the method is "void glGetTexParameteriv( GLenum target, GLenum pname, GLint *params)"

as I understand the 1st parameter should be GL_TEXTURE_2D and second parameter GL_TEXTURE_RESIDENT but what should be the third parameter? Should it be my data member m_unTgaTexture (Texture ID - see above my code) or not? my variable is of type unsigned int but here they require GLint *params so I still get errors!!
If anyone could show me on a simple 2 line example instead of redirecting me to specs where all says same thing then I would be very grateful! Thanks!!

davepermen
02-26-2002, 11:49 AM
a pointer to the stuff you want to GET (thats why it is called GetBlahBlahParam..

here you want to get a single integer wich is GL_TRUE if it is resistent and GL_FALSE if not.. so call it like that..

GLint ok = GL_FALSE;
glGetParblablah(....,&ok);
if(ok) ... else ...

robert_s
02-26-2002, 12:05 PM
Yes! Thats what I wanted!! Works!!
It was so simple!!
the third parameter was confusing me!! thanks!

zeckensack
02-26-2002, 12:14 PM
robert_s, you're still overlooking one important thing:

The way you use the information is WRONG

You have to destroy ALL textures that you create, otherwise you'll be creating memory leaks - if you're lucky the GL implementation will clean up your mess but this is really your job.

OpenGL doesn't throw away textures. So please, please, please, don't use this in your texture destructor.

[This message has been edited by zeckensack (edited 02-26-2002).]

robert_s
02-26-2002, 01:40 PM
zeckensack Thank you very much for ur advice!! You are perfectly right http://www.opengl.org/discussion_boards/ubb/smile.gif
But I am not using it exactly in destructor.
The real usage of this function will be in my main program in keyboard function. This will be used for my sliding up/down help key map menu. If the user presses 'h' then I load texture for the menu which is then slided down and if pressed again the menu will be slided back up (dissapears) but teh program will still run. (I just switch flag (true/false) when it completely disappears then I destroy texture to free some memory as I have quite large amount of texture already loaded so I am trying to safe memory.
IF 'h' is pressed then flag is true which means load texture. if pressed again flag is false destroy texture.

Now the problem is that if I keep pressing quickly 'h' key say 5 times then my prog will load textures 5 times. But I dont want that. I want to check first if that texture is already loaded then dont load again wait until destroyed then you can load again.

Now for some reason when I execute my program and before I press key 'h' to load the texture I check if that texure is resident in memory (its not loaded so it shouldnt be there) but when I call glGetTexParameter ie.

GLboolean CTexture::isTextureResident()
{
GLint isTexResident = GL_FALSE;

glGetTexParameteriv( GL_TEXTURE_2D, GL_TEXTURE_RESIDENT, &isTexResident );

if ( isTexResident == GL_TRUE )
{
return true;// texture resident in memory
}
else
{
return false; //texture not resident in memory
}
}

even if that texture was not loaded the function returns true! why????!!!
This is the bit of code I use in main in glut keyboard function:

case 'h': if( n_toggleKeymapHelp == 0 ) //if help off
{
if ( Help.isHelpTextureResident() )
{
Help.loadHelpTextureOnFlyTGA("data/cockpit/keymapHelp.tga", GL_RGBA, GL_NEAREST, GL_NEAREST, GL_CLAMP);
cout << "TEXTURE LOADED - it will slide down (visible)!!!!! " << endl;
}

n_toggleKeymapHelp = 1; //turn help ON
b_keymapHelp = true;
break;
}
else if( n_toggleKeymapHelp == 1) //if help on
{
n_toggleKeymapHelp = 0; //turn help OFF - slide up (make it dissapear) you can delete tex
break;
}

If someone wants to see what I exactly mean then you can find one screenshot which I just added now. see screenshot section.
http://homepage.ntlworld.com/robert.stuliglowa/projects/GL-Flightor/main.htm

Thank you!


[This message has been edited by robert_s (edited 02-26-2002).]

zeckensack
02-26-2002, 02:11 PM
Well, that depends ...

If you are using glAreTexturesResident(1,&texture_obj_number,&where_is_it) and the texture is NOT resident the function will return false instead of writing to where_is_it. This function has been designed to check multiple textures and in that case, this kind of behaviour saves a little bit of overhead. When testing only one texture you can safely do the following:


//return true if resident, false if not
bool isTheDarnTextureResident(GLuint tex_obj_number)
{
bool i_dont_care;

bool is_it=glAreTexturesResident(1,&texture_obj_number,&i_dont_care);
return(is_it);
}

Another possible issue:

"//if ( Help.isHelpTextureResident() )
//{
Help.loadHelpTextureOnFlyTGA("data/cockpit/keymapHelp.tga", GL_RGBA, GL_NEAREST, GL_NEAREST, GL_CLAMP);"

Ok, you've commented the if out, but shouldn't it be
if (!Help.isHelpTextureResident())
{ .... }
?

As it looks now, you load the texture everytime it is already loaded, which it never will be, because if it's not already loaded, it won't ... did that make sense ? http://www.opengl.org/discussion_boards/ubb/smile.gif

robert_s
02-26-2002, 02:25 PM
Oh Sorry!!
yeah!! you're right!! I copied the wrong bit of code! http://www.opengl.org/discussion_boards/ubb/smile.gif
the original part has exactly what youre saying with the excalamtion mark! it still returns true!! strange!! But anyway I'll try your method!! I'll try now! http://www.opengl.org/discussion_boards/ubb/smile.gif
oh yeah! I didnt experiment with glAreTexturesResident yet!
I am experimenting with glGetTexParameter as I am playing around with only one texture. is it ok or should I use glAreTexturesResident instead even if its a single tex! anyway I'll try what youre saying thanx!!

[This message has been edited by robert_s (edited 02-26-2002).]

zeckensack
02-26-2002, 02:26 PM
Umm, just found the following in the OpenGL spec:
"The initial value of TEXTURE RESIDENT is
determined by the GL."

which basically means "this value is completely random on newly created texture objects".

If I understood that right, the value will only be set after you've initialised and used the texture at least once for drawing.

However, in a later part in the spec it says

"An implementation may choose to establish a working set of texture objects on
which binding operations are performed with higher performance. A texture object
that is currently part of the working set is said to be resident."

Which means that a GL implementation (or driver) is not required to make this distinction at all. It may happily report that all textures are resident while they really reside in AGP memory or worse.

For lack of a better idea right now, I suggest you just drop this stuff altogether. http://www.opengl.org/discussion_boards/ubb/frown.gif

robert_s
02-26-2002, 02:39 PM
YESSSS!!! IT WORKSSSSS!!!!!
Your first method with glAreTexturesResident works perfect!! I just tried it!!!
before I actualy load my tex it reports that the tex is not resident and when loaded it confirms that the tex is resident now!! cool stuff!!
But still... I am going to find out why glGetTexParameter doesnt want to work! arghh... I read the spec and I understand it the same way as you! why is it then? but this I'll leave fo myself to work out! I wont bother you anymore! You halped me enough! http://www.opengl.org/discussion_boards/ubb/smile.gif
Thaks sooo much zeckensack! Without your help I would be stack! thanks so much!
Cheers!


[This message has been edited by robert_s (edited 02-26-2002).]