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
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.