In OpenGL, is it possible to release texture data, but keep the same texture id? I want to unload textures when they aren't needed, but load them again later when they are.

There are a lot of objects that store texture IDs. (For example - spritesheets - multiple objects use the same texture ID and represent a part of the image). glDeleteTextures releases the id, so another texture (image data) can acquire the same ID. I'd have to walk all objects and change the texture IDs they use.

Is there a way to release the texture data only, but keep its ID, so that later I could do only glTexImage2D on the same ID and keep all references to this ID valid?

  • 1
    \$\begingroup\$ glTexImage2D( GL_TEXTURE_2D, 0, internalFormat, 0, 0, 0, format, type, nullptr ); might do it. \$\endgroup\$ – Ben Mar 6 '14 at 20:14
  • \$\begingroup\$ As long as you're not using immutable storage, you can always invalidate the texture's storage (as suggested in Ben's comment) freeing it for other uses. The texture object's state (name/ID, parameters, etc) will be retained. You may run into problems on some drivers if you have a huge number of texture objects though. And even if you don't invalidate texture data, the driver can store infrequently used textures in main system memory until it's needed, so invalidating texture data for this reason is usually not necessary anyway. \$\endgroup\$ – bcrist Mar 6 '14 at 20:39
  • 2
    \$\begingroup\$ You shouldn't directly store texture IDs. Even if you unload the data, your context might be invalidated on a mobile platform or the user's GPU might overheat/crash/reset and require a context recreation, invalidating all IDs. Add a layer of abstraction and store references/IDs to your own Texture interface that wraps the GL object. \$\endgroup\$ – Sean Middleditch Mar 6 '14 at 23:15
  • \$\begingroup\$ @Sean Can that really happen? I have never heard of that. Also, how would you ever access the object without the id. Why would opengl give you an id if that's the case? \$\endgroup\$ – Ben Mar 7 '14 at 2:22
  • \$\begingroup\$ @Ben: yes. Mobile devices of it and the desktop would do it if the platform can gracefully cope with gpu resets (only eindows can that I now of, and only in d3d). Hold into the ids, but abstract so the id is only referenced in one place instead if many. \$\endgroup\$ – Sean Middleditch Mar 7 '14 at 22:17

As Ben suggested, you could use a dummy call to glTexImage2D(). If setting a 0x0 texture doesn't work, you should be able to create a 1x1 texture. I've seen the WebKit source code do something similar to "allocate" texture IDs, and then call glTexImage2D/glTexSubImage2D to upload the actual texture data later.

Of course, it's up to the driver whether it wants to free the memory at that point. OpenGL doesn't provide direct texture memory access, unless you use an extension like EGL_KHR_Image to do platform-specific management.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.