Spellcaster Studios

Make it happen…

Multithreaded planet generation

So I worked today on the queuing of the planet generation, and a lot of problems came to light…

First, I found out that this new video card I have throws a lot of errors that the previous one didn’t… Mostly they’re warnings, but it took me a while to figure out exactly what the problems were if they were just warnings or things that could bite me later…

Then I built the planet generation queuing, and with that I found a small problem in the way I designed this whole multithreading thing…

As I think I mentioned before, the graphics APIs I’m using don’t like multithreading very much, which means that I have to go to some length to guarantee that all API calls are done from the main thread. What I do is that everytime I swap buffers (so show the rendered scene), I process a command queue that might have been generated by the other thread (the generation thread), creating meshes, textures, getting locks, etc… This works fine and has very little impact in the code I already made, since on the other end, the code that generates these commands waits patiently for the result of the processing before continuing, so I have little synchronization happening, even if it takes a bit more in generating the area…

The problem (that I didn’t see, but I should have) is that due to this way of doing things, commands are only processed once per frame, and because of the waiting on the other end, it means that each command issued on the generation part of the code only actually runs on the swap buffer, so that effectively means that I run a command every 30 ms! I’m doing hundreds (if not thousands) of these in all generations, which means that what used to take about 15-20 seconds now would take minutes, rendering the whole thing useless!

After some soul searching and beating myself up, I decided for a silly hack: now commands are processed in all draw calls. In theory, this could make the game extremely slow, but because of the waiting on the other end, I only ever have one command waiting on the queue, and that’s fast enough!

So now I have the area generation fully multithreaded, and it generates the whole planet in about 20 seconds, which makes the whole game experience almost seamless…

I have an “abort” system as well, so if we decide to go to another planet, the generation of the current one doesn’t finish, and the game moves to the new generation… This required some ugly calls in the middle of the generation code, and there is the (very) slight possibility of a memory leak on the system (which I might still get rid off, since I like my code as clean as possible)…

In the process I found some ugly bugs: ice planet generation is crashing (that’s probably easy enough to figure out, I haven’t looked into the ice planet generation since I started this whole polish phase), the landing cutscene when you come from the Skydancer isn’t working properly (weird camera angles for some reason) and the cave generation is completely screwed up:

screen529

I don’t know where the blue comes from, to be honest, the chasms aren’t abrupt anymore (because of the smoothing algorithm I use, since the chasm end in invisible lava) and there’s no light being emitted by the crystals anymore…

And I still have the OpenGL multithreading code to do, and the new static lava shader… So a lot of work to keep me busy this week, before I can get back to making things prettier!

Now listening to “Beyond the Red Mirror” by “Blind Guardian”

Comment