Feeling the Squeeze : OpenGL in 1kb

Jan29

Written by:1/29/2014 9:50 AM

With the rapidly growing power in the PC market hard-disk space, ram and processing power seems to be increasing constantly. But every now and again there is a squeeze for optimisations to try and get a game under that 15GB target, or less than 8GB ram usage, and whilst that is a challenge it doesn't compare to that of the Demoscene. A subculture that thrives in the tight requirements of a spectrum Rom or the ram restrictions of a C64, pushing to maximise the capabilities of their demos within the limitations of the hardware. For those eager to push the boundaries competitions still run such as :

SceneScene.org Awards is a way of honouring the previous year of demoscene releases in a more formal setting, and disregarding their ranks in other competitions.

JS1KThis is a competition about JavaScript scripts no larger than 1k. Starting out as a joke, the first version ended with a serious amount of submissions, prizes and quality.

Over the past 2 days I have been looking into the feasibility of getting OpenGL within the tight restrictions of 1k, and as demonstrated by the image above, this seems fairly achievable. The1k program (1022 bytes) contains enough code to create a window handle, OpenGL context and draw a changing section of the Mandelbrot set with two whole bytes to spare, but how is this achievable?

There are a number of things that can be done to reduce the output exe size, with the first most obvious being to take advantage of size saving options of the compiler. Instructing the compiler that you need as small an exe as possible will cause it to optimise code where possible to reduce in size (no in-lines ect). This alone however wont get you down to a handful of kilobytes, for this you will need an advanced linker.

A linker takes the object code produced by the compiler and shoehorns it all into the final EXE package. Along with your own code a large number of routines are also added from the C runtime with additional code from the STL you may need. For this project I used a free linker called Crinkler developed by Rune L. H. Stubbe (Mentor/TBC) and Aske Simon Christensen (Blueberry/Loonies).

As a linker that has been designed around the sole purpose of reducing exe size for Demo’s this is where the majority of your size reduction will be generated. The linker builds a small code compressor into your exe which allows a greater amount of code to be packaged into the same space. Combining Crinker with some code level optimisations (reducing the use of differing constants, strings etc) you’ll be well on your way to fitting everything into your demo.

For this project i’m sure there is still much more that could be squeezed in, if you are interested in the source (including an optimised visual studio project) please checkout the GitHub links below: