r/opengl•
Nazoz

How should i manage textures and their memory?

So far, my process of managing textures is: genTexture, bindTexture, set parameters, and lastly pass the texture data to opengl with glTexImage2D. After this i just delete all the pixel data, and bind the texture unit whenever i want to use the texture.

​

This seems like it is too easy to be a good solution. I imagine textures staying in gpu memory until program termination, using resources that other programs might need, that my engine isnt even using at the moment. Also, what happens if i try to use more gpu memory than available?

​

Do i even have to manage this at all or is it done for me? That is, can i just create a bunch of texture units and they are moved between ram and gpu memory automatically?

Posted Jun 19, 2019, 1:26 AM UTC · edited

28
fgennari•

You're allocating memory on your graphics card and copying the data there when you call glTexImage2D(). This memory is freed when you call glDeleteTextures(). When you say "delete all the pixel data", I assume you're deleting some array in system memory. Calling delete/free() won't affect the GPU. When you bind the texture, you're assigning it to a texture unit on the GPU, which doesn't do any memory allocation or deletion.

The graphics driver will manage memory for you. What happens when you run out of graphics memory depends on your OS and driver/vendor (Nvidia/ATI/Intel). On my Windows machine with an Nvidia card, it will start swapping to main system memory, which drops the framerate down to a few frames per second. On linux with my ATI card, it just crashes the driver and I have to restart the machine.

Modern GPUs have a few GB of memory, and you're unlikely to create enough textures to fill that. But if you do, you need to delete some textures to make room for new ones. Oh, and you're creating textures, not texture units. Texture units are part of the GPU hardware rather than something you do in software.

5
John137•

i think the key is to minimize your bind calls. and thereby reduce the number of state changes. idk though if binding a texture is similar to binding a shader in that it changes context. but iirc minimizing the amount gl calls per frame in your code is usually the way to go.

2
NazozOP•

Im already doing that, or atleast taking measures to reduce bindings.

​

The question is mainly concerning how i should manage the texture memory. Because i cant create as many texture units as i want since they take up gpu memory?

​

I have to unload texture units if gpu memory is full no? So im thinking i have to keep track of gpu memory usage, and unload texture units to free space for new ones?

​

Or do i just try to find a balance between minimum memory usage / maximum fps, so that i use as little memory as possible to maintain a target fps?

1
Neuroticcheeze•

Just to add:
There are many many different clever ways to use multiple textures at once (atlas, layer packing, mipmap packing, etc)
Which technique you use entirely depends on your needs. Big blockbuster games will use a streaming system so that they create/delete the textures they need on the fly. For a game that has 1000s of textures, it might only have 100 or so loaded at the same time.

4
Neuroticcheeze•

A texture unit is sort of like a usb port that you plug texture 'pointers' into. You can generally have up to 16 units IIRC. But you can have as many textures as you like (there is a limit but its like... 65000 textures, its different for each kind of gpu).
You have lots of units to play with so that a single active shader can use many textures at once (there are ways to store multiple textures on a single 2DArray texture btw)

2
PcChip•

what does minimizing bind calls and state changes have to do with managing memory?

1
jtsiomb•

It's done for you automatically. OpenGL manages GPU memory, and frees/re-uploads resources as needed. If you create too many textures and they don't fit in memory, an LRU scheme is used to keep the active textures in GPU, and re-upload inactive ones when they're used again. Of course if your per-frame active set exceeds GPU memory your performance will go to hell, but there's not much you can do about that anyway.

2
Osbios•

Note that at last on desktop PC the drivers tend to do delayed allocation/freeing.

Often textures only actually allocate and get uploaded to the GPU memory when you use them the first time.

Deleting a textures-id also only frees the memory after the pipeline is done with the object. So before a fence/finish the memory is still in use/allocated.

Or for OpenGL in general, if you use a texture as a render target for a FBO, and the FBO is not currently bound to the context, then deleting the texture-id does not delete the underlying memory object until that FBO(s) are deleted, too.

Same is the case for VAOs and the buffers bound to them!

Under memory pressure OpenGL will swap out less used memory objects to system memory. But swapping back in often has shitty performance for anything you still use from time to time.

2