Wednesday 25 December 2013

Merry Christmas! What's been cooking?

This one is just an update on what I have been doing lately and hopefuly what to expect when I am done. But before I dwelve into the gory details I would like to wish you all a happy Christmas if you celebrate it in any form. If not then have a happy time during 24-26th December - why not! But only then, because I am terrible person and life is cruel so don't count on more!


Coming back to teh bussiness - My plans were to finish up my font/text rendering subsystem v.2 before christmas break and to clean up code a little bit to prepare it for a presentable form just after I fnish up my article (which I already started!) about rendering in lame engine, but well... reality verified my plans very quickly. Reality is a true bitch indeed. When she saw my plan the respond was something like this along the lines:


I had some implementation difficulties which I did not suspect. I did integrate FreeType 2 with my previous projects twice, each time adding on to the functionality and power but this time I planned to do something really nice - in fact this is how I should implement text and font handling from the beginning. I already have most of the foundation done (dynamic glyph caching, managing various sizes, styles, effects etc.) but some core things are still badly designed (well, not design at all to be exact) and other need some polishing. It took me way more than I expected. It's 25th and I had my deadline for this till the end of the year. I thought this was wayyy more then enough to finish all the stuff but now I am facing the possibility of missing "my schedule" so maybe instead of writing blogs I should go back coding now... or in a moment.

Next quite big thing I was planning for some quite time now is redoing my 2d renderer to support not only cool caching/batching mechanism which I already had but something incredibly handy - arbitrary polygon/shape support and automatic shape generation for alpha based images. I will talk more on the caching and batching, as well as the stuff related to polygonal based 2d rendering in upcoming articles (obviously till now I was using textured quads pretty much like a lot of other 2d game engines). Another cool thing I am planning on doing is sprite atlases support. My previous work was using manually assambled atlases and automatic glyph atlases for fonts. The latter one had it's limitations and the first one was fucking tedious to do, even for a minimalistic game I was doing for my bachelors degree project (along with the whole framework to support the damn thing). This time I approched things a little bit different and the glyph caching mechanism is working like a charm with efficiency in mind as minimal memory footprint as possible.

Over a year has passed since I finished up my thesis, and a year I learned a lot. For our latest game, among other things, I was designing and programming brand new asset processing pipeline. While having it's issues it is working quite well now and doing it's job. The thing is responsible for converting source graphics into proper density and resolution for given settings and packs things into their atlases using some scripted rules. Also there is another part (the more flawed one ;) ) which is responsible for scanning your working directory, catching up any changes to source images and redoing atlas compilation on the fly. The only thing that was missing was some minor fixes and tweaks to the system and some changes to the engine itself was inclusion of asset hot-reloading. The thing with atlases is that while in theory they reduce loading times and increase efficiency by reducing number of texture switches, they are not always enough and more has to be done.

Most mobile devices and older computer are fillrate bound. With 2d games this is a serious bottleneck as we tend to do little CPU intensive calculations. The amounts of geometry being fetched is... limited. So the only problem is processing and displaying lots of textured quads. This way a lot of power and memory is leaked. Not only we have to process each pixel, even the fully transparent ones that will be discarded anyways, we also waste space, thus memory, on atlases.

If you use rectangular bin packing and textured quads for rendering than you are using your resources wrong! Consider L-shaped image. This is a perfect example of shape that requires large bounding box and wastes so much memory and fillrate on processing transparent pixels. The way to go is deduce the shape of the transparent image, enclose it with polygonal shape, triangulate it, display it in this form - discarding all totally transparent images. This is something that was recently done in Unity but this was a no-brainer optimization. Of course there are more possibilites for fine tunning and optimizing 2d rendering but this one is the easiest and best in terms of performance boost.

This for sure works with fillrate problem but what about memory? Well if you then use those shapes to do bin shape-packing instead of rectangle packing you might save up a lot of memory. Although, arbitrary shape packing is a hard problem this can be done efficiently with more or less simplified heuristric.

So the idea is this. I am planning on adding:
- Alpha-based image processing and generation of convex hull for transparent images
- Triangulation of given shape (point set) to generate renderable geometry on the fly
- UV calculations
- Efficient bin packing for arbitrary shapes
- Built-into-engine image handling pipeline.
- Watcher mechanism for automatic recompilation.
- Hot-reloading of resources (based on the watcher).

In combination with filesystem I was developing a few months ago that was designed to also support TCP streaming this should be sweet solution to the problems of iteration times and testing on mobile devices that we suffer so much.

What I have done now is:
- Triangulation based on ear-clipping algorithm (while O(n^2) taking into consideration the number of vertices we need to approximate 2d shape this is good algorithm and easy to implement). I will devote separate post on that one when I will test the whole thing and proof it works ;)

- UV calculations were easy.

- Efficient bin packing - I just started toying with the idea, we will see what I will come up with.

The rest is WIP, planned or not even touched so I have plenty of things to do ;)

So I hope that some of the topics I am planning on covering will eventually be interesting to some of you. For now this is all so cheers!

No comments:

Post a Comment