Game Design, Programming and running a one-man games business…

I know file access is slow but….

As a programmer, you learn fairly early on that file access is very slow. The slowest place to get data is the hard disk, we all know that. Ok, maybe tape storage. But it always amazes me just *how slow* it can be. I don’t mean the stuff like reading a texture file from disk, you expect that to be tiresome, but just file-system operations stuff. I guess I’ve always assumed that because modern hard drives have cache chips on them, and we also have a LOT of RAM available to page stuff, that querying the NTFS (or whatever file system you have)  file table should be fairly quick.

In other words, seeing if a file exists, or enumerating files in a folder shouldn’t take an age, if I’m not actually going to load in the contents. Doesn’t windows know enough about optimizing to page chunks of the FAT into RAM?

Maybe it can’t, or it doesn’t work that way, but I have discovered, to my mild surprise that if you don’t want to use compressed or locked pak files (I like an open file structure to encourage investigative modders), it actually makes enormous sense from a performance POV for you to scan your whole game folders file structure into disk and manage your own file-cache for stuff like enumerating filenames later on.

Why would I not know what files to access already? Well GSB2 writes a lot of files at runtime, and it also handles some objects having companion lightmap files, or sometimes not. The simplest and most flexible system is for it to check for companion files at runtime on startup, and then field any FileExists() calls internally. It’s faster. And I mean its 10x as fast, we aren’t talking 1% speedups here. Things are now getting to the point where I just assume all O/S code is sluggish bloatware and write my own versions whenever possible. I might eventually have to do some trickery involving multi-threaded file-loading, or loading in only specific mip-maps from files at specific points.

I do actually REALLY enjoy this kind of thing, which is a pain because I should be fixing bugs, implementing features and generally trying to keep GSB2 on schedule. Not to mention some new D3 DLC and the TOP SECRET THING. Oh and SMTG.

It’s a good thing I already have a holiday booked this year, as I’d never book one now if I’d waited this far :D

Gratuitous Space Battles 2 : First battle video!

Well I’ve been working on this beast long enough, so it’s time to share my efforts with you wonderful people of the internet. I have a whole bunch of stuff I’ll eventually talk about and show to you, but I thought I’d start off the Gratuitous Space battles 2 videos with a decent 9 minute explanation by me of the stuff that is new (so far) in the graphics engine for the GSB 2 battles. There is a lot to talk about:…

I tend to do more written stuff than videos just because I find the majority of video content moves ‘too slow’ for me. I want all the information and spectacle of stuff condensed as much as possible because I take information in very very fast. If someone has a really slow speaking voice it’s even more agonizing. But hey, I’m not the audience, you are, and the good people of the internet seem to prefer video content to written, so I’m going to try and do my bit to keep up with your youtube-watching ways. or twitch, or whoever is cool this week :D

It’s difficult because I hate my own voice, and I have broadband (ha!) delivered by a sliver of copper the size of an angels nostril hair, so my upload speed is about 45kb/s on a GOOD day. This vid took nearly 4 hours to upload. Grr. Luckily I should have a chance of getting fiber here in December.

Anyway, enjoy the vid, post any comments here or on youtube or on the GSB2 official forums. I have more stuff to show off in a few weeks!

 

Speeding up loading times

So I released a video yesterday of GSB2 running on multiple monitors, and in that video, a large space battle loads. It takes about 5 seconds. I find those 5 seconds agonizing. I recently spent half a day speeding up the games startup time because launching the game to test 100+ times a day was starting to buy me. Granted, 100×10 seconds is not very long, but I am incredibly impatient. I sometimes launch 2 apps on 2 different monitors and start typing on app 1 whilst windows finishes creating the window for app 2. I often encounter problems where clicking the google homepage and entering a search term means I miss the first few characters as the intel i7 catches up. I’m VERY impatient.

So how did I speed it up? and what tips are there for coding faster loads?

In this case, it was dumbness on my part. Some code that pre-loads ship designs was checking for whether or not lightmaps existed for each ship texture. Some have them, some don’t. It checked by creating the file and loading it in, failing if not found. HAHAHAHA. Very slow, and not needed. Those ships may well not be accessed this game…

So obviously it was tons quicker to not load the file in then, but just to check for the existence of the file on disk (or arguably to do this even later). Cue big speedup. Another speedup was possible because every instance of that item checked for the files existence, so I cached the result in the ship type and only checked for each file once. Much Much Faster.

That’s fine with a game that has few files on disk, but if you need to do this for 2,000 files, it can be real slow just to do those 2,000 file checks. Accessing actual hard drive data as opposed to RAM is painfully slow. With non SSD drives actual physical platters might need to spin. File access is the killer of load times. File access, and decompression.

Ideally you have data on disk that is pretty much in the format it will take in memory, then there is no decompression, just a straight ‘blap’ into RAM. There is a tradeoff here against debuggability, editability and modability. Some games have 2 file formats, a binary version, used when shipped, and a text version that overrides the binary and is only used during development. That can be kinda buggy, and prevents modders accessing the raw assets which might be a bad thing.

Another trick is to use ‘pak’ or resource files. In principle, and often in practice, these are just big zip files. The file access for the entire games resources becomes a single file read, and you effectively load the whole file-access-table for the game in on startup, vastly minimizing file-access slowdowns in cases where there are thousands of tiny files.

In general the golden rule of load times is to only do what you have to. If you can pre-process assets (into binary formats, for example) then do it. If you can defer some actions until that content is really needed, then do it. There is a natural tendency for programmers to have a ‘initapp()’ function and stick everything in there, but you really don’t need to. The player may well not launch the stats screen in this run-through, so don’t load any assets for it, or indeed, any data for it, and don’t waste CPU time initializing systems that might not even be used.

The best advice I could give anyone for speeding up load times would be to just get a profiler. Some are free, most IDEs come with a built-in one (they often suck), and LEARN how to use it. If you never put any time into fixing it, the chances are your load times are laughably inefficient. Most games are.

Something I learned today about STL and Z-sorting…

So here is a thing, you might be interested in if you use STL, if you don’t…well sorry :D

if use use the sort()n function thats built into an STL list, it guarantees to preserve the order of identical objects in the list. if you use the vector version, all bets are off.

Bloody hell.

So if you have a bunch of asteroids with these Z values

5,9,3,0,0,0,-2,-5

And you use a list to sort them, all is good in the world. if you use a vector, those 3 asteroids at 0 are going to Z-fight like crazy things.

the solution?

use stable_sort()

well call me mr-picky but I think I’d be happier if stable_sort() was the default, and we actually renamed sort() to be take_your_chances_and_do_random_crap_sort().

I presume stable_sort is slower… Luckily I’m not sorting asteroids every frame (that would be NUTS), and I only sort things when I have to, so it isn’t mega critical. it led to a bug where the biggest hulk chunks from spaceships did Z-fighting if theyu weighed ewnough to all have a Z-speed of zero, and thus a Z position(relative) of 0, so when other objects spinning away caused a z-sort, their order got scrambled. If you are a non coder and don’t know what Z-fighting is, it’s a flickering effect you get in 3D games where two images seem to be undecided about which one is in front. You often see it on ‘decals’ such as blood splats on the floor or posters on a wall. It’s annoying…

Anatomy of rendering a game dialog box in a custom engine (GSB2)

So I was tweeting that this took forever:

dialog1

It’s just a dialog box for gratuitous space battles 2., why did it take more than twenty minutes to put together? Now… I’ve seen unity, I know it has all these plug-ins that do stuff like this, and that it’s all very user-friendly etc blah blah. But I’m old school. I’m rocking my own custom-written engine, including all the GUI. That gives me huge advantages (mostly speed) and also some disadvantages. The best advantage is there isn’t anything I can’t make the code do.

The pain with this dialog box came in three flavours.

Flavour one was those circular clock-like indicators. In theory, this is really easy, you can just generate a tri-strip of a lot of polygons and draw a curve thats smooth and crisp as you like, as long as you can spare the vertexs. I’m not drawing many, so it’s not an issue. The problem is, when you do that, you get a too-blocky, too un-aliased clunky mess that just doesn’t look ‘right’ when surrounded by lovely aliased everything. I’m not drawing 3D models, so my game has a  nice smooth look to it, and it jarred badly. So I have a sprite of that curve, and I draw a subset of it using a tri-strip arc. It’s a bit fiddly, ant took a while to get right.

Flavour two was the outline of the right-hand part of the window. It’s a bit complex, as it goes in and out and then around the close button and then loops around those circles, and it has to be really slick too, and ironically in this case it looks better drawn as a crisp 1 pixel line, so there is actual hand-crafted code in there to work out all those positions and curve ncie arcs and lines around them.

Flavour three was speed. I like everything in my game to render fast, including GUI. No point in having a fast engine where 95% of the frame is spent drawing a dialog box. That means ensuring that ouline on the dialog is a single draw call with no fuss, that all those tiny animated bits of fluff in the dialog corners and outside the edges are drawn efficiently, that the calculations on that arc outline are as fast as possible, and that the dialog in general; doesn’t use many draw calls.

It’s all horribly, laughably slow really. I probably have a ‘spare render target’ knocking about that I could use to blap this whole dialog to (BTW they resize dependent on the ship, which adds to the complexity), and then only update it when it changed, otherwise just blapping it as a single quad. In practice, the windows various elements update quite a bit.. but I’m sure I could speed up the module icon rendering with runtime aliasing onto spare render targets. I love all this stuff.

But even I know when I’m getting obsessed and need to move on!