Resource managers: how much is overkill?

Started by
5 comments, last by y2kiah 9 years, 10 months ago

I've got a resource manager implemented in my engine, but I'm starting to wonder if I've over-engineered it. I built a resource management system that uses ResourcePackages (or resource groups) and resources are addressed using a ResourcePath. The ResourcePath is like a normal file path, except that it contains the package name before the path for example "package:folder/directory/resource.res".

I'm starting to notice it's becoming increasingly difficult to manage the package names in my content pipeline, because I have to manage all the links between resources. A mesh links to a material that may or may not be inside another resource group for example.

I implemented resource packages originally because I want to be able to pre-load entire groups of resources all at once, and also be able to unload entire groups. I'm starting to wonder if my package system is overkill though. For a small world it's obviously overkill, but I'm trying to build an engine optimized for massive open worlds. I already have background loading, load/unload events, and a cache to keep frequently used resources in memory.

Advertisement
All resources that belong together should be in the same file. A mesh is simply data inside a model file, which can also have textures embedded into it (and when textures are embedded into model files, be sure not to load the same texture twice while loading various models (a checksum is necessary)).

Materials don’t need to be their own group. The only reason a material would ever be by itself in a file is for the editor (presave materials, quick-load them, etc.), but when exporting for a game they are part of the model file.


You shouldn’t have to be managing all these links to things, so you have likely over-engineered it. The more separate resources that have to all be there yet are each in separate files, the more likely you will never ship a final product, as you constantly find random missing or out-of-date files 3 stages into the game.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

In addition, a "manager" class is a frequent symptom of poor design. Classes should generally have a single responsibility. What single responsibility is "manage" ?

I know what a cache is, same with a loader, a stream, and a proxy. Those all seem like separate responsibilities, and hence, separate classes.

For most of the games I've worked on, there are individual asset caches tend to act as hubs for loading and unloading resources dynamically.

I'm not sure whether I understand why exactly the OP is speaking of overkill. It seems me that using a package name within the resource name is making the problem. If so, the following description may help; if not … feel free to ignore my post ;)

When the system starts up, it scans a specific directory (the "overlay" directory) for files, and assumes each file to contain a single resource. It stores the names of the files as names of resources in an internal table of content. I'm using hashes for resource names at the runtime level, but the file-path names relative to the directory would work as well. The system further scans another directory (the "package" directory) for files, and each file determined to have the correct format is opened and its stored table of content is integrated with the one already in RAM. During this, if a resource name collides with one already in the internal TOC, it is ignored and the internal entry left as is; additionally, if the already contained entry refers to a package as source, a conflict is logged.

The table of content now has entries for each single resource. The resources' names managed therein are not tagged by a package name or whether they origin from the overlay directory. But these additional informations are stored along the resource name in the entries, of course.

A file offset and length is stored in the TOC for accessing a resource inside the package file. Now, the file offset and length does not necessarily address a single resource but may address a sequence of resources. This is called a load unit, because all addressed bytes are loaded (and later unloaded) at once. Nevertheless, the entries in the TOC are still names of individual resources. So requesting any resource of a load unit causes all resources in that load unit to be loaded. A "bill of materials", as may be stored along a resource to declare dependencies, ever lists each single resource.

Of course, before the loader is requested to load a resource load unit, the cache is interrogated whether a resource is already loaded.

With a concept like the one above, resources are requested by name and regardless of their storage location, and regardless of whether they are bundled in a load unit or stored in their own load unit. It allows for hot-swapping during development and for installing updates without the need to send an entire package file. All dependencies are still explicit on a resource by resource basis. If for some reason the toolchain decides to store a particular resource now in another load unit, the bills of material need no update.


In addition, a "manager" class is a frequent symptom of poor design.

It may be a frequent symptom, but it's not necessarily an indicator or poor design. You will always need code at increasingly higher layers of abstraction to invoke lower level subsystems, manage lifetimes, glue single-responsibility code into cohesive processes, etc. A manager class would be a sign of poor design if all of the sub processes were implemented within it. It would be poor design if the manager class were also a factory, and a cache, and a process manager. But acting as a manager alone is a valid single responsibility imho. I think too often, manager classes start life as a place to own object, instead of just being process managers. Often, the objects they manage should really be owned somewhere else.

In the last few years I've consciously taken a bottom-up approach to design which has helped me personally to end up with systems that I actually enjoy using and re-using. I look at it like designing an API for myself, and worry about everything that backs it after the fact. If you think you've over-engineered the resource system, maybe it's because you took a top-down approach? Take a step back at look at how you would prefer to interact with your resources, and implement that bottom layer first. Maybe by the time you wrap the system you will find you don't need a manager class at all?

BTW optimizing your resource system for massive open worlds will optimize for lazy loading and predictive loading, flexible asset dependencies, and a cache that doesn't fragment over time. I don't think that type of engine benefits as much from large asset packages, unless loading "levels" as discrete, unique groups of assets fits into your game. It works well for TES, but the frequent loading screens do break immersion a bit.

If your texture asset loader is designed to be asynchronous, that requirement pretty much bubbles up all the way to your main loop. Your material loader may request textures to be loaded on its behalf, your mesh loader may request materials to be loaded, which in turn request shaders to be loaded, which in turn require a texture render target to be created, etc. In an asynchronous loading system, your asset is not fully loaded until all of its dependencies are also loaded. This buildup of asynchronicity is best implemented using Deferreds / Promises / Futures / whatever you want to call them, instead of callbacks.

Thanks for the help. Do you know of any good resources explaining the basic concept of futures? Right now my resource's interface is created synchronously and stores a load state (atomically accessed of course) storing whether its loaded or unloaded. What advantages do futures/promises provide?

This video is a pretty good rundown of promises and why you would want to use them.

The talk is geared toward the web world, but the concepts translate into any other language. One thing to note is in the C++ standard library, a "deferred" and a "promise" are unique classes and two sides of the same coin. In other languages the term may be used interchangeably, or one or the other was chosen.

The main advantage of promises over using straight callbacks/lambda is you can quickly get into "callback hell" where you lose all ability to reason about the control flow of your program, and it turns into a deeply nested spaghetti mess. Promises allow you to write control flow in a way that appears like synchronous code (aka readable code), but behind that abstraction it's still asynchronous in nature.

If you were to switch over from your current method to promises, instead of the loaded/unloaded flag your load method would return a promise to its caller, which at some point later will be either fulfilled or rejected. The caller will handle either case. Now, inside your resource loader you may have sub-dependencies that are also loaded asynchronously. Let's say it's a material and wants 3 textures loaded, you'd want to chain something like the concurrency::when_all method to the promise you return. The promise will be fulfilled when all of the sub-dependencies are loaded. Let's say you have to attach some post-processing logic after the loading step, you can chain one or more steps using .then(). You also have the opportunity to catch any errors between any step and respond accordingly. You don't have to be compiling C++ 11 or using the concurrency runtime to do any of this either.

This topic is closed to new replies.

Advertisement