Hi there,
recently I read in one of the treads about data-oriented programming vs. object-oriented programming. While I am generally avoiding to follow such principals dogmatically, it got me thinking again about a quite common problem: Resource Managers (in OpenGL). To get a little bit more specific, let's consider a Texture Manager. I have currently implemented them in the following way:
When starting the program, the manager creates a texture class for each texture of my game and stores them together with the name in a map or unordered map. The texture class itself has a reference counter and the OpenGL handle of the texture. The value of the handle is set to a specific "texture not loaded" texture during construction. Now if an object is loaded, that requires a certain texture, the texture is requested from the manager. It searches the textures name and returns a special reference class, that stores a reference to the texture class and increases and decreases its reference counter. Obviously, the reference counter has the purpose to initiate the loading and unloading of the texture. The problem here is, that the texture handle, which is just an integer, is not stored in a cache-friendly way, as are other managed objects that I might need during rendering (Buffer objects for meshes, etc). All handles are stored in the texture managers memory location, which might be far away from the buffer object handles and the other object data.
So I was thinking that I should probably simply store references to all reference classes (I know that sounds weird) in the texture class itself. The purpose of this is, that I can then simply copy the current OpenGL texture handle to the reference class. In case that the handle changes for any reason (reloading/replacing texture), I update the handle in each reference class using a loop. Sure, this is a little bit more work during an update of the texture and if a reference is added/removed, but how often does that happen? As a benefit of this method, a copy of my texture handle is now stored at the same memory location as the rest of the object's data, which should result in reduced cache miss count.
Now I am interested in what you think about these two approaches and how you implement your resource mangers as efficient as possible?
Greetings