‘fairly fast; ‘if you consider that 17 hours per frame is pretty fast’

By | February 2, 2024

ZX Spectrum with a colorful background.

Ray tracing has been around in computer graphics for a surprisingly long time. It was used to create images in the 1960s, and new algorithms were created in the 80s that included path tracing. Yet the “holy grail” of processing remained a distant dream for early home machines like the ZX Spectrum. To get an idea of ​​how far ray tracing has come in the past half-century, just look at how long it takes this machine to render a single ray-traced frame.

The ZX Spectrum takes approximately 17 hours to render a single ray-traced frame. That’s one frame every 61,200 seconds or 0.000016 fps.

But what a magnificent shot this is. This is the output of a fun project by Gabriel Gambetta, creator of the Tiny Raytracer program and senior software engineer at Google Zurich, and was brought to my attention by Hackaday. Gambetta, a long-time fan of the ZX Spectrum, decided one day to see if Sir Clive Sinclair’s brave machine, born in the early 80s, could handle ray tracing. The surprising result is that yes, it can sort of happen.

As detailed in his blog post about the project, Gambetta ported the basic ray tracing code from his book to BASIC, the programming language that powers the ZX Spectrum. He found that, without much tweaking, this code produced a basic image even on the ZX Spectrum’s limited hardware (usually a 3.5 MHz Z80A chip with as little as 16 KB of RAM).

The image consists of only four color drops: yellow, red, green and blue. This process took about 15 minutes. On a modern computer, the same scene takes a fraction of a second.

Picture 1 / 2

A ray-traced image showing different color patches on the ZX Spectrum.A ray-traced image showing different color patches on the ZX Spectrum.

A ray-traced image showing different color patches on the ZX Spectrum.

Picture 2 / 2

A ray-traced image using the standard CGFS ray tracer.A ray-traced image using the standard CGFS ray tracer.

A ray-traced image using the standard CGFS ray tracer.

“I knew from the beginning that implementing some form of ray tracer was possible because the theory and math were relatively simple,” Gambetta said. “But I didn’t know if it would run in a reasonable amount of time because this thing was slow by modern standards.”

Ray tracing is computationally expensive. This is the process of shooting a beam out of the camera through a pixel grid until it interacts with an object in the scene. Sending more rays through this point can create lighting effects such as shadows, reflection, and refraction. Millions of rays per frame are required to produce a high-resolution image with realistic illumination, even using a single ray per pixel. In today’s ray-traced games, doing this in real time requires clever algorithms, bounding boxes, and a lot of acceleration.

Whereas the ZX Spectrum is only capable of producing a handful of colors (fifteen in total, including two brightness levels and black) and has a total resolution of 256 x 192 pixels. Nor can it display any color at any pixel at any time. It uses 8 x 8 pixel blocks that can store information in only two colors at a time. This was done to minimize memory usage; Overall, it’s a great decision because while computers aren’t known for being affordable, it did cut costs massively at a time when viewing much more than text was a nightmare.

ZX Spectrum with a colorful background.ZX Spectrum with a colorful background.

ZX Spectrum with a colorful background.

This thing was slow by modern standards.

“The first version of the ZX Spectrum had 16 KB of total RAM, so memory efficiency was absolutely critical (I had the much fancier 48 KB model),” Gambetta said in the blog post. “To help save memory, the video RAM was split into two blocks: a bitmap block, which uses one bit per pixel, and an attributes block, which uses one byte per 8 x 8 pixel block. The attributes block will assign two colors to this block, INK (foreground) and called PAPER (background).”

The first image produced by Gambetta is an image colored by information from the rays sent into the scene, and it more or less matches these 8 x 8 pixel blocks, which are only 32 x 22 pixels in size; each displayed in a different color. across the entire 8 x 8 block.

The downside of the ZX Spectrum’s memory-informed block-based solution is what’s called attribute conflict. This essentially means that no more than two colors can be displayed within an 8 x 8 pixel block.

Gambetta’s next step was to increase the resolution to almost maximum 256 x 176 and effectively draw individual pixels, but this means running into attribute conflict.

“Increasing resolution is easy. Dealing with attribute conflict is not so easy.”

There is no solution to attributing conflict. This is a natural and distinctive part of the ZX Spectrum. All in all, Gambetta’s high-resolution image is close to perfect enough; Just don’t look too closely at some of the spots where the three colors meet.

Picture 1 / 2

A ray-traced image showing attribute overlap on the ZX Spectrum.A ray-traced image showing attribute overlap on the ZX Spectrum.

A ray-traced image showing attribute overlap on the ZX Spectrum.

Picture 2 / 2

A ray-traced image showing attribute overlap on the ZX Spectrum.A ray-traced image showing attribute overlap on the ZX Spectrum.

A ray-traced image showing attribute overlap on the ZX Spectrum.

The difficulty of going from a 32 x 22 image to a 256 x 176 image is evident in how long it takes to render this secondary image. From 879.75 seconds (about 15 minutes) to 61,529.88 seconds (more than 17 hours). Fortunately, thanks to some optimizations and time-saving adjustments, this time could be reduced to 8,089.52, or almost two and a half hours.

A ray-traced image showing attribute conflict with optimizations to improve performance on ZX Spectrum.A ray-traced image showing attribute conflict with optimizations to improve performance on ZX Spectrum.

A ray-traced image showing attribute conflict with optimizations to improve performance on ZX Spectrum.

And we haven’t even gotten to the really cool part yet! As I mentioned before, ray tracing can be used to create pixel color as well as produce various effects. Think of Alan Wake 2 and how vibrant and realistic some of the lighting in that game is. The ZX Spectrum never came close; There are only a handful of colors to work with; so Gambetta simulates how light interacts with a scene using dithering.

The result is truly amazing for a small machine like this. A ray-traced 3D-like image that somehow works despite all the limitations on colors and how they are used.

A ray-traced image showing the shaded color dither created on the ZX Spectrum.A ray-traced image showing the shaded color dither created on the ZX Spectrum.

A ray-traced image showing the shaded color dither created on the ZX Spectrum.

“I did this iteration and honestly, I stared at it in disbelief for a minute,” Gambetta said.

Moreover, it uses one more change to the ray tracing code to add shadows to the scene. By using a beam to track any intersection between a sphere and a directional light source, he was able to create the final image in this experiment that was truly incredibly impressive. Of course, even with optimizations it takes around 17 hours to render a single frame, but I’m certainly impressed.

Picture 1 / 2

A ray-traced image showing ray-traced shadow effects on the ZX Spectrum.A ray-traced image showing ray-traced shadow effects on the ZX Spectrum.

A ray-traced image showing ray-traced shadow effects on the ZX Spectrum.

Picture 2 / 2

Ray-traced image using the standard CGFS ray tracer, showing ray-traced shadow effects.Ray-traced image using the standard CGFS ray tracer, showing ray-traced shadow effects.

Ray-traced image using the standard CGFS ray tracer, showing ray-traced shadow effects.

The final result exceeded the expectations I had in the beginning!

Reflections are basically impossible due to limitations in color mixing, and perhaps more needs to be done to improve performance, but ultimately this isn’t something I previously thought was possible on the ZX Spectrum’s weak 3.5MHz processor.

“The interesting part of the project was figuring out how to get around all of those limitations to make something that runs reasonably fast, considering that 17 hours per frame is pretty fast and looks pretty good,” Gambetta said.

“In this sense, the result exceeded my initial expectations!”

Why ZX Spectrum?

ZX Spectrum with a colorful background.ZX Spectrum with a colorful background.

ZX Spectrum with a colorful background.

The ZX Spectrum was a very popular computer in the early 80s. It’s affordable, fun, stunningly beautiful, and even quite educational. Part of its popularity had to do with how it was possible to code your own games using the programming language BASIC. But first it was necessary to overcome the limitations of the hardware.

“Its designer, Sir Clive Sinclair, went to great lengths to make it so cheap,” Gambetta told me. “It was a limited computer, even for its time.”

“I grew up with one of these at home (in Uruguay, of all places). It had tons of video games as well as some applications, so for a generation like me it was a gateway drug into programming; games, but it was very, very easy to accidentally switch to programming it yourself, because It “booted” into a command line, not into a user interface, but into what we call an IDE (or development environment) these days.”

Learn some BASIC and you can: Something on ZX Spectrum. It’s not fast, it’s not very colorful, but there’s something there.

“You could have some kind of visual thing that moves on the screen with something like 5 lines of code, which is unthinkable these days.”

Gambetta tells me that he doesn’t believe it’s that simple to break into the world of graphics programming these days; even the most basic programs require more lines of code than any program the ZX Spectrum has ever made. Still, for retro gaming fans who want to dabble in coding themselves, he plugs PICO-8 and, of course, his own book (available for free on his website or for money on Amazon), Computer Graphics from Scratch.

“This requires nothing more than a browser and a text editor; almost every computer has them out of the box. But even that requires more steps than we had in the ’80s.”

Leave a Reply

Your email address will not be published. Required fields are marked *