问题
I need to find a way to track positions where the loaded textures are drawn. When some of the loaded textures are overwritten, i have to delete them from memory.
I have a Remote User Interface (RUI) server which sends me a lot of small images, which are assembled and then shown on the RUI client's screen. In this scenario, i have no clue which textures are behind some other textures, so i am unable to delete them. Also, overwriting must be complete (texture behind must be totally hidden). Is there any efficient way to achieve this tracking and deleting?
My use-case is something like this: i have a menu with button images stored on a server. Whole screen is made of fragments (buttons, scrollbars, animations etc.). For example, when i click on a button, server sends me a multiple thumbnails of pictures stored in its storage, and i display them in ,for example, right half of the screen. On other button click, list of songs, videos, ebooks or something else are shown. I need to remove textures of picture thumbnails from memory and then show list of songs, for example. In this way, overwriting will disappear, and my memory usage will be significantly reduced.
My openGL ES 2.0 implementation is on Android platform, where whole job is done in JNI layer. There is no complicated drawing, just decoding png images, converting to textures, and basic textures displaying.
回答1:
You can do something along the lines of reference counting to keep track of how many times the same image has been used. When an instance is entirely occluded or off-screen, you decrement the reference count, and delete all the textures with 0 references.
Reference counting could be as simple as a std::map<Texture, int>.
Your occlusion check could just be an O(n^2) AABB intersection/containment test and an extra O(n) test against the screen's AABB. If that's not fast enough for you, try a better data structure like a quadtree or spatial hashing.
Depending on how your application is set up, you could also track instances of your texture with a type of smart pointer and figure out when all of your hidden views have been deleted.
That being said, this is probably a waste of time unless you're actually bound by the amount of VRAM on the device or getting close to it. Loading/unloading images from either disk or a network location is going to be orders of magnitude slower than just keeping it in VRAM if you have it available.
来源:https://stackoverflow.com/questions/23943320/best-way-to-find-and-remove-hidden-textures-from-memory