Destroying Textures to free system resources

Is there a method to remove/destroy textures to free their system resources?

I fear, there isn’t :(. But it shouldn’t be hard to implement that. So far you can only waste the whole cache, I remember right. Let’s see what other folks say… Then I could implement such a method.


Marvin meant, there isn’t yet such a possibility :slight_smile:

I guess it must be done on several levels : when a texture loads, first it gets opened by a sun standard image loader, then it’s copied in a byte buffer then transferred on the graphic card (opengl texture creation). So it should be freed on the graphic card, not only from the cache, right ? Removing a texture from the cache is really easy. I could provide a method if you wanted.

Of course. Thanks. Sorry for being unprecise :).

So uberscott, do you want to free RAM or VRAM ? In the first case, removing them from the cache would be ok. In the second case, we would call glDeleteTextures

Just my 1 cent: A Texture object should receive an OpenGL texture handle (like RenderAtom) whan it gets rendered by OpenGL. Then a method markDeleted() should be called (or something like that) so that the renderer can delete it from VRAM. The markDeleted() method should also delete it from Texture cache. And an UnsupportedOperationException should be thrown, if the markDeleted() method is invoked on the fallback-texture.
Don’t know, if texture handles can be retrieved from OpenGL, just my 1 cent… :slight_smile:


I need to free resources from VRAM and RAM.

I am painting chat messages to a BufferedImage then creating a texture from that.

I have tried using the SwingGUI for chat messages but the performance is quite bad. Maybe Text2D objects could be used, but I am having trouble understanding if they actually support word wrapping etc.

If a Texture’s contents can be replaced, that word work for me as well. In that case I could store about 10 textures and recycle them as needed.

Anyway, having a method to destroy/uncache individual unused textures seems like a good idea to me. Please let me know where i can get the new code!

Thanks guys!

Please use the HUD. Examples can be found in org.xith3d.test.ui.HUD3DTest. It doesn’t support this texture freeing, but texture recycling. And I could inclide texture wasting transparently (without changing the API). Then you would directly benefit from that without changing your code.

BTW: Text2D supports wrapping. Just insert a ‘\n’ into your String and the line will be wrapped. The same for a Label Widget on the HUD.


EDIT: I seem to have made something wrong in the MD2LoaderTest, which is also used in HUD3DTest. So the HUD is not visible. But it works. It’s just a problem of the MD2Test. I’ll fix it tomorrow. But as I said the HUD works. You can also see it in org.xith3d.test.loaders.BSPLoaderTest (working). And it is documented in “Xith3D in a Nutshell” (XIN) downloadable on (doc section).

I’m still having problems getting the Label text to be properly positioned… otherwise I would use the hud. Has the label positioning code been fixed in the current dev version?

If I remember right there was something I fixed some time ago. Should work completely now. Do you know the possibility of alignment on a Label? Don’t know, if it was already implemented in 0.8.0.


Do labels really respect \n or not. I have not been able to do this. I see in the Images section of the example HUD you use a special case where the dimension is set to 1 or 0 which causes some kind of auto size formatting

     * Creates a new Label with the given width, height and z-index.
     * @param size the new size of this Widget
     * @param zIndex the z-index of this Widget
     * @param text the text to display in this TextWidget
     * @param font the Font to be used for the text
     * @param color the color to be used
     * @param alignment the horizontal and vertical alignment
    public Label(Tuple2f size, int zIndex, String text, Font font, Color3f color, TextAlignment alignment)
        super(size, zIndex);
    this.autoSize = ((size.x <= 0f) || (size.y <= 0f));
   this.size.x = Math.max( size.x, 0f );
        this.size.y = Math.max( size.y, 0f );

Uber…are you creating new BufferImage every time or extracting the ImageComponent2D and modifying the exisiting, assoicated BufferedImage??

[quote]I am painting chat messages to a BufferedImage then creating a texture from that.

Fixed. Was an easy one-line-fix 8).


Autosizing has nothing to do with line wrapping. The size of a Label is irrelevant, if you don’t want to position the text exactly.

See line #511 in HUD3DTest:

Button aciButton = new Button(170, 130, "Another\ncool\nbutton",

Well, this is a Button, but it’s exactly the same, since, a Button contains a Label. It really works ;).


I have done the Texture-removal-from-VRAM thing. There’s a new method in the Texture class called markDeleted(). Invoke this method and it will be deleted from RAM/VRAM. Well I haven’t done the removal-from-RAM thing, since the texture cache is somewhat strange. I can’t see, why not a simple Hashtable is used there ???. Amos, are you familiar with the cache? If you’re, please have a look at the cache. I have added a remove() method to org.xith3d.utility.cache.ResourceCache with an empty body. There’s TODO, which you may easily find. If you’re not familiar with it, then I’ll continue fighting with it ;).

BTW. Most of the glDeleteTextures thing was already there. It just wasn’t used except a Texture was modified in place, which is certainly very rare.


I would rather use Label instead of creating an on the fly texture… but in release 0.8.0 Text2D seems to have issues with the fonts. They are not legible and there seems to be additional space between each of the characters. Have these issues been resolved? If so, I will probably get the latest version from SVN.

Thanks for adding the delete feature in the texture, there are other places where i need it too!

Yes, these ones have definitely been fixed.

You’re welcome :).


I’m currently working on a new StaticLabel Widget, which will be a lot faster for static text contents (or even for not very fluctuative contents).
I’ve written a new method in TextureCreator, which creates a Texture with a text drawn on it. This is then used for the StaticLabel. The regular Label Widget has a disadvantage, that it is rather slow, but highly flexible. But in many cases on a game’s HUD, you’ll want to use mostly static texts and benefit from the speed-up.


done :slight_smile:

I would almost always advise to run the latest SVN version…

But on the other time, maybe it’s time for another release ? So who’s for 0.9.0 ? (Note : I’m assigned to release tasks so just ask).

Well a Hashtable isn’t used because it has hard references, which means if you load lots of textures at once, it will never delete the already cached ones (it will rather crash by memory lack than to free them). Cache has soft references. Having a remove method shouldn’t be hard. I look at it right now.