Sign in to follow this  
vreality

Asset Format and Loading Schemes

Recommended Posts

Over the years I’ve been pretty happy with solutions I’ve seen to lots of engineering problems. But there are a couple of areas in video games that seem resistant to solutions which don’t leave you thinking there just has to be a cleaner, easier, less error-prone, lower maintenance approach.

Probably the longest lived offender has been asset formats and loading. I would like to know your favorite asset format and loading schemes. I’m not talking about packaging, compression, streaming, etc. I’m talking about the binary layout and structure of asset data, and how loaded data becomes useable by the run-time code.

How does your favorite approach work? What are its strong and weak points?

Share this post


Link to post
Share on other sites
In-place data - assume that data-structures and file-formats are the exact same thing. Once you've described a "file-format", you can use it on disk and in memory, which means you can load assets in/out of memory without needing the words "parser" or "serializer". There's also no language boundary - anything with a byte-stream can read/write your files/data-structures once it implements the "format".

The catch is you've got to avoid using pointers [i]inside data-structures[/i]. You can still pass pointers around and use them in [i]run-time only [/i]data-structures, but you should avoid using them in any persistent data-structure.
If a data-structure does need pointers internally, you can add Deserialize/Serialize-esque calls that allow it to convert ID's to pointers (and back) during loading/saving.
N.B. you [i]can [/i]almost entirely avoid pointers in practice. Instead you can use indexes, keys, relative byte-offsets ([i]added to the address of the offset[/i]) and byte-addresses ([i]added to a base pointer from the runtime[/i]).

Knock-on benefits to the runtime include:
* No parsing, no runtime bloat. N.B. this requires platform-specific data builders, e.g. Xbox final data files will be different to PC final data files.
* In-place dynamic allocation -- if for example, a particular level spawns up to 5 monsters at a time, then it's file can include a pool-allocator data-structure with the size of 5. This will be wasted space on the disc, but let's assume the streaming/compression layer takes care of that.
When the runtime code for that level spawns a monster, it doesn't need to allocate memory for it with new/malloc, it can instead grab memory from the pre-allocated pool.
Game middleware has to at least support the overriding/hooking of new/malloc calls by the user, but middleware that doesn't even make new/malloc calls outside of loading is even better.
* All structures ([i]except runtime-only or Deserialize/Serialize-esque ones[/i]) are POD, which means you can memcpy them, treat them like values, cache/parallelise/distribute them, etc...
* Variable size structures are supported ([i]e.g. where sizeof(Class) changes depending on the instance[/i]).
* If you describe your formats using a language with reflection, you can easily inspect/debug any data-structure, whether it's in the tools, or on disk or in the game's memory. Edited by Hodgman

Share this post


Link to post
Share on other sites
What Hodgman said.

Since I use Lua almost exclusively these days, most of my assets and data structures are saved to file as compiled Lua code. Loading a table becomes as easy as a dofile() call, or similar. Structured correctly, you can save your entire game state simply by iterating the set of tables comprising your game and saving the compiled code. It's fast, it's robust, it's easy. It's not quite as desirable as a straight "memory dump", but it's the next best thing. I take advantage of Lua's own parser to parse my format files, instead of writing a parser for each file type.

Share this post


Link to post
Share on other sites
[quote name='Hodgman' timestamp='1335707264' post='4935825']In-place data - assume that data-structures and file-formats are the exact same thing.[/quote]

This is what I call "live" or "frozen" objects. Load 'em up and pretend like you constructed 'em. The tool has to know the binary layout of the live runtime objects. The first asset system I designed was like this. Added features:
[color=#ffffff].[/color][list]
[*]Arbitrary pointers within an asset file. They were written as relative pointers and a fix-up list was added to the file, so the loader could find them and convert them back to real pointers on load.

IIRC this was done with serializable pointer objects which registered themselves in tool code. When the address range in which a pointer resided, and the address range to which it pointed, got written to disk, the system could automatically generate a fix-up record for it (and could automatically fail, if the second range wasn't written). In run-time code, it was a plain pointer.
[color=#ffffff].[/color]
[/list][list]
[*]Cross-file references via Asset Handles. The run-time kept a hash table of Asset ID/pointer pairs. Each file got an asset manifest, so each asset got registered on load. An asset handle held a pointer to an entry in the hash table. References to assets came in as IDs and got fixed up as handles.

Hash table entries could be generated with IDs alone, so handles could be created to assets not yet registered. Those hash table entries would be fixed up when the asset was registered. By the same mechanism, assets could be swapped out while code was holding handles to them.
[color=#ffffff].[/color]
[/list][list]
[*]Virtual functions for top level asset classes. Since we had a manifest of asset instances, it wasn't a stretch to add a type ID and call placement new on each one, which would plug in its virtual function table pointer, if any (and wouldn't trash the object, as long as it used a constructor that didn't do anything).
[color=#ffffff].[/color]
[/list]
I can't remember exactly why, but I didn't like that the game and tool shared code in our implementation. I remember trying to reverse engineer undocumented debug symbol files so the tool could perform reflection on C++ object layout, like the debugger does. Maybe it was the [i][b]way [/b][/i]the code was shared that was a problem. I can't remember.


The one issue that seems to come up often is what happens when data format changes. How do you approach versioning and format change rollout?


[quote name='JTippetts' timestamp='1335710267' post='4935832']most of my assets and data structures are saved to file as compiled Lua code[/quote]

Crazy as it sounds, I've contemplated trying to do the same thing with C++. I figure why worry about binary layout and fix-ups when that's what the compiler and dynamic linker do? I could theoretically make a tool spit out C++ code which defines a bunch of static data, then compile it to a dll and load it with the dynamic linker. But I think that not being able load a dll into memory which I manage has kept me from trying this, even just for kicks. Most dev teams are very picky about controlling memory management. Edited by VReality

Share this post


Link to post
Share on other sites
[quote name='VReality' timestamp='1335737645' post='4935908']
I can't remember exactly why, but I didn't like that the game and tool shared code in our implementation. Maybe it was the way the code was shared that was a problem. I can't remember.[/quote]In my current implementation I chose to not share code at all - I'd rather keep the data builders and the runtime code bases as decoupled as possible, so I'm only sharing the format/specification between the two, not the code. E.g. C++ has some [font=courier new,courier,monospace]struct[/font] layouts, and C# has some [font=courier new,courier,monospace]BinaryWriter[/font] routines.[quote]The one issue that seems to come up often is what happens when data format changes. How do you approach versioning and format change rollout?[/quote]You change both the [font=courier new,courier,monospace]structs[/font] and the [font=courier new,courier,monospace]BinaryWriter[/font] routines ([i]or sync down the latest code[/i]) and compile. Then you run the data build pipeline as usual, it detects that your files were built with an old C# plugin, and it rebuilds the out-of-date files. This really shouldn't be a problem assuming you've got a data build pipeline, instead of a directory that you manually put game data files into.
Compiled data files aren't kept in a version control system ([i]they might be kept in an unversioned network cache as an optimisation[/i]) - they're built from source content files, which are kept in a version control system. When compiling a data file, the inputs are hashed ([i]including source file and compilation code version[/i]) allowing you to automatically recompile data files when either the source file or the data-builder code are changed. Edited by Hodgman

Share this post


Link to post
Share on other sites
[quote name='Hodgman' timestamp='1335775414' post='4936016']Compiled data files aren't kept in a version control system [...] - they're built from source content files, which are kept in a version control system. [/quote]

Yes, that addresses the issue. However, most studios I've been at didn't like everyone building the whole project. They wanted at least an asset-engine split, in which there's a "published" version of the engine used by asset creators who don't build it, and there's a "published" version of the assets used by engineers who don't build them (except as they need to, for changing asset formats, prototyping new assets, etc.). This sort of firewall tends to reduce the impact of bad check-ins.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this