Ray tracing has been around in computer graphics for a surprisingly long time. It was used to generate images in the 1960s, and by the 1980s new algorithms had been developed, including path tracing. Still, the “Holy Grail of Display” remained a distant dream for early home machines like the ZX Spectrum. You only have to look at how long it takes this machine to render a single ray-tracing frame to get an idea of how far ray-tracing acceleration has come in the past half-century.
It takes about 17 hours for the ZX Spectrum to display a single ray-traced frame. That’s one frame every 61,200 seconds, or 0.000016 fps.
But what a beautiful frame it is. This is the outcome of a fun project by Gabriel Gambetta, creator of the Tiny Raytracer program and senior software engineer at Google Zurich, and brought to my attention by Hackaday. As a long-time fan of the ZX Spectrum, Gambetta decided one day to see if Sir Clive Sinclair’s plucky machine, born in the early ’80s, could handle ray tracing. The surprising result is that it is indeed possible.
As described in his blog post about the project, Gambetta ported the basic ray tracing code from his book into BASIC, the programming language that powers the ZX Spectrum. Without too much modification, he found that even on the ZX Spectrum’s limited hardware, this code produced a basic image: a 3.5 MHz Z80A chip with often only 16 KB of RAM.
The image consists of only four color spots: yellow, red, green and blue. This process took almost 15 minutes. On a modern PC, the same scene would last a fraction of a second.
Image 1 of 2
Image 2 of 2
“From the beginning I knew it was possible to implement some kind of ray tracer, because the theory and mathematics are relatively simple,” says Gambetta. “But I didn’t know if it would work in a reasonable amount of time because this thing was slow by modern standards.”
Ray tracing is computationally expensive. It is the process of casting a beam from the camera through a grid of pixels until it interacts with an object in the scene. Casting more beams from that point can create lighting effects such as shadows, reflection and refraction. To produce a high-resolution image and realistic lighting, even with just a single ray per pixel, millions of rays are required per frame. It takes smart algorithms, bounding boxes, and massive speedups to do this in real time for today’s ray-traced games.
While the ZX Spectrum can only produce a handful of colors – fifteen in total, including two brightness levels and black – and has a total resolution of 256 x 192 pixels. It also cannot display any color in a pixel at a time. It uses blocks of 8 x 8 pixels in size, in which it can store information of only two colors at a time. This was done to minimize memory usage; Overall, a great decision as it greatly reduced costs at a time when computers were not known for being affordable, but a bit of a nightmare as they could display much more than just text.
This thing was slow by modern standards.
“The first version of the ZX Spectrum had a total of 16 KB of RAM, so memory efficiency was absolutely crucial (I had the significantly more luxurious 48 KB model),” Gambetta said in the blog post. “To help save memory, video RAM was split into two blocks: a bitmap block, which used one bit per pixel, and an attribute block, which used one byte per 8 x 8 pixel block. The attribute block would assign two colors to that block , called INK (foreground) and PAPER (background).”
The first image produced by Gambetta is in fact an image colored by information from rays thrown into the scene, and corresponds more or less to these 8 x 8 pixel blocks with a size of only 32 x 22 pixels – each individual color is displayed over an entire 8 x 8 block.
The downside to the ZX Spectrum’s memory-based, block-based solution is what’s called attribute clash. This is essentially the inability to display more than two colors within an 8 x 8 pixel block.
Gambetta’s next step was to increase the resolution to almost the maximum of 256 x 176, which effectively drew individual pixels, but this means that clashes between attributes occur.
“Increasing resolution is easy. Dealing with attribute collisions, not so much.”
There is no solution to attribute collisions. It’s an inherent and idiosyncratic part of the ZX Spectrum. Ultimately, Gambetta’s higher resolution image is almost perfect. Just don’t look too closely at the points where three colors meet.
Image 1 of 2
Image 2 of 2
The effort to go from a 32×22 image to a 256×176 image is reflected in the time it took to render this secondary image. From 879.75 seconds (almost 15 minutes) to 61,529.88 seconds (more than 17 hours). Fortunately, some optimizations and time-saving adjustments allowed this to be reduced to 8,089.52, or almost two and a half hours.
And we haven’t even gotten to the really cool part yet! As I said before, ray tracing can be used to generate the pixel color, but it can also be used to produce different effects. Think of Alan Wake 2 and how vibrant and realistic the lighting is in that game. The ZX Spectrum could never get close – it only has a handful of colors to work with – so instead Gambetta simulates how light interacts with a scene through dithering.
The result is really great for a small machine like this. A ray-traced 3D-like image that somehow works despite all the limitations on colors and the way they’re used.
“I ran this iteration and honestly I stared at it in disbelief for a minute,” Gambetta said.
Additionally, he uses a further tweak to his ray tracer code to implement shadows into the scene. By using a beam to trace any intersections between a sphere and the focused light source, he was able to produce the final image of this experiment, which is truly incredibly impressive. Of course, even with optimizations, it takes about 17 hours to render one frame, but I’m absolutely impressed.
Image 1 of 2
Image 2 of 2
The end result exceeded the expectations I had in the beginning!
Reflections are basically impossible with the color mixing limitations, and perhaps more needs to be done to improve performance, but ultimately this isn’t something I would have previously thought possible at all with the measly 3.5MHz processor of a ZX Spectrum.
“The interesting part of the project was figuring out how to get around all these limitations to make something that ran pretty fast, if you consider 17 hours per frame to be pretty fast, and looked pretty good,” says Gambetta.
“In that sense, the end result exceeded the expectations I had at the beginning!”
Why the ZX Spectrum?
The ZX Spectrum was a hugely popular computer in the early 1980s. Affordable, entertaining, surprisingly beautiful and even quite educational. Part of its popularity came from the way it was possible to code your own games using the BASIC programming language. But first you had to work around the limitations of the hardware.
“To make it so cheap, the designer, Sir Clive Sinclair, cut all sorts of corners,” says Gambetta. “It was a limited computer even for that time.”
“I grew up with one of these at home (in Uruguay, of course). It had a bunch of apps in it, but also a lot of video games, so it was a gateway to programming for a whole generation of people like me; you got it for the first times games, but it was very, very easy to accidentally start programming it yourself, because it didn’t “boot up” into a command line, not into a user interface, but into what we would today call an IDE (or development environment).
Learn a little BASIC and you can make it something on the ZX Spectrum. Not fast and not with too many colors, but still something.
“You could make a visual thing move across the screen in about five lines of code, which is unthinkable today.”
Gambetta tells me he doesn’t believe there’s anything so simple about delving into the world of graphics programming these days — even the most basic programs require more lines of code than anything the ZX Spectrum has ever done. Still, he plugs the PICO-8 for retro gaming fans who want to dabble in coding themselves, and of course his own book (available for free on his website or for money on Amazon), Computer Graphics from Scratch.
“That requires nothing more than a browser and a text editor, which almost every computer comes with ready-made. But even that requires more steps than what we had in the ’80s.”