Building a render cache system

Project Page Next project post Post History

I'd been thinking about how to develop a cache system for my render engine. By cache system, I mean something to help cut down on common processing tasks that have to be done many times over. I know there are some existing ways (I've heard of a few "trees" that are used for spatial partitioning), but there's a couple of things I've thought of that I'm trying out for myself first.

My first attempt started out pretty rough. It took several minutes to compile for a simple scene with a small resolution. I recall one going over 11 minutes just to build the cache. And when it did 'build', it didn't even render! Until I realised I was handling some rubbish data and then accessing container indexes that didn't exist.

I spent a good two weeks trying to overcome these.

Eventually I resolved the issues and was able to get the engine to render with a basic system. It was slow, but a start. Here's a side-by-side image comparison that I'll talk about:

The image on the left was one I did to first test out true depth of field (there's a post on this here). It doesn't use any cache system. The image on the right is the same scene, but using the cache system. The image on the left rendered in some 5-hours and 26 minutes. The image on the right rendered in 3-hours and 50 minutes. Over a long render, the caching system seems to help.

But it's not without it's problems. You can see some black sections in parts of the render, mainly on top of the cubes in the background. I'm not sure what's causing this. And it looks like the image is also missing the last rendered line.

The cache system is also not particularly useful on short preview renders, at least not yet. The cache build time in the image above took over 16 minutes. For something with just a few passes, it's quicker to just render without the cache. That said, I do believe I can speed-up the build process. The code that builds the cache is fairly inefficient in places, it was made to serve the purpose of proof-of-concept, so it's not running as well as it could. I know there's room for improvement there.

This cache is one I designed. As far as I know, it's not a robust method used elsewhere (or maybe it is - I don't know?). I'm looking at a few other caching methods though. I do have one of the earlier mentioned spatial "trees" in mind. I suspect I'll end up with a combination of different things. Hopefully it doesn't get too complex.

The proof-of-concept does seem to be positive here though. But for now, I've got a horrible headache, and I need a break from coding.