A trip through the Graphics Pipeline
fgiesen.wordpress.com
fgiesen.wordpress.com
As an aside, I mentioned this series earlier today on the Graphics Programming Discord server and now it's on the front page of HN?
edit: on the other hand, searching the server it seems this article is linked there every few days, so it might just be coincidence
If you want to use it for general purpose computation not tied to rendering I would suggest CUDA. If you want to play with rendering try shadertoy.com
I'll try shadertoy.com
Basically how would one use a GPU to compute something without CUDA/OpenCL mostly for curiosity but say I wanted to use a PS2s graphics chip to perform a calculation. How was/is that actually done?
If we instead look at something more modern like an early GeForce with programmable shaders what was typically done was to set up your regular rendering of a full screen quad and write a pixel shader which performs your desired computations instead of actual lighting calculations.
APIs such as CUDA and OpenCL were introduced to allow people who weren't making games to make GPU calculations without pretending that they were colour calculations.
No fragment shaders, though, it's true.
In the era of semi-programmable graphics pipelines, of which there was the N64, PS2 and GameCube, you would not be able to solve general-purpose problems using only that pipeline: given a handful of colour mixing formulas, registers, textures and vertices to control the pixels to write back to memory, I fail to come up with any tangible problem you could hack them into solving.
However, what the 3D generation of video game consoles had to bundle with the hardware in order to process complex scenes at interactive frame rates were digital signal processors (DSPs). Think of them as regular processors, but with an extra vector unit, allowing you to crunch enormous amounts of data in parallel (single instruction, multiple data instructions, or SIMD). This was essential to perform geometry transformations, which benefit greatly from parallelism, in order to build graphics commands to pass down to the rasteriser.
I am unsure if the SEGA Saturn and PS1's vector-accelerated instructions are sufficiently general-purpose to allow them perform any computation you wish to do, but at least starting with the Nintendo 64, you could write microcode to accelerate any task you'd like, provided you were brave enough to deal with the harsh memory constraints of microcode and obscure documentation.
To give concrete examples, the PS1 came with a dedicated chip (the MDEC, or motion decoder) to decode MPEG video frames from the CD directly into the VRAM to display on screen.
While the N64 doesn't have dedicated video decoding chips, its signal processor was designed in such a way to allow microcode to do an equivalent job (not to mention YUV texture decoding on the rasteriser's side).
So this did not stop a talented Resident Evil 2 developer fresh out of school to write an MPEG video decoder microcode to be able to cram two CDs worth of video data (around 1 gigabyte, down to a 64 megabyte cartridge), of course with other smart decisions like data deduplication and further compression/interlacing.
Another one was Rare developers writing an MP3 audio decoder microcode to store large amounts of dialogue.
And finally, in recent times, a developer managed to write an H.264 video decoder microcode, for a machine that was released almost a decade prior to this codec's birth.
You can also accelerate physics and anything else that would benefit from SIMD, really, and in fact, while more modern CPUs integrate SIMD instructions in their tool chest that compilers can take advantage of, the PS3's Cell processor was a brief throwback to the old hardcore ways, before GPGPU became king.
You could almost treat the DSPs as the compute units of modern GPU architectures: they definitely processed the vertices, and nothing stops you from adding a "vertex shader" pass, however, because it's not directly integrated into the graphics pipeline, it's harder to emulate a true pixel shader, you might be limited to full-screen pixel shading since there's no feedback from the rasteriser about which pixels exactly get written to memory.
Actually, you could probably just use the depth buffer as write-only with constant values to identify objects as different kinds, and use a "pixel shader" pass to apply effects on those annotated pixels, but I assume this might be a pretty expensive technique.
... Now that I think about it, it's not unlike modern rendering engines.
I know most of this stuff anecdotally but I didn't start working with 3D until programmable shaders were already becoming commonplace.
Computer graphics has a very interesting history, and like many things, there's a lot to learn from studying it. Surprisingly, many techniques and principles are still relevant to this day.
Thankfully, the Internet is full of documentation, post-mortem accounts and reference implementations to learn more about all this.
Here's a bunch of random noteworthy things and references I forgot to link (sorry for potentially digging rabbit holes):
The best information tends to come from official documentation detailing almost everything you want to know about the inner workings of systems. Additionally, unofficial community documentation can also be of great quality and complement official sources.
The architecture overview posts from Rodrigo Copetti (https://www.copetti.org/writings/consoles/) pack a lot of accurate information at a glance, and are a great starting point if a specific topic piques your interest.
The Resident Evil 2 microcode post-mortem: https://ultra64.ca/files/other/Game-Developer-Magazine/GDM_S...
Improved lighting, reverberation, Dolby surround and MP3 microcodes from Rare: https://youtu.be/jcIupBUAy98?t=41
H.264 decoding microcode: https://www.reddit.com/r/n64/comments/gpwxx0/my_dragons_lair...
The Reality Signal Processor's programming guide: https://ultra64.ca/files/documentation/silicon-graphics/SGI_...
Emulators' progress reports can reveal a lot about the details and intricacies required for accurately replicating these systems' features: https://dolphin-emu.org/blog/ is one such amazing source of information, but all other major emulation efforts have equally interesting content.
Transparency sorting of surfaces (https://en.wikipedia.org/wiki/Order-independent_transparency) is a hard problem to solve for traditional scanline renderers (most PC/console GPUs), to the point that even today's releases can ship with somewhat obvious rendering errors.
On the other hand, tiled renderers (used in the Dreamcast, mobile hardware, and Apple silicon) are able to solve this problem due to their very nature, albeit trading another set of drawbacks, though the completely different hardware approach is a nice read (https://en.wikipedia.org/wiki/Tiled_rendering).
Someone once shared a video demonstrating a 3D package from ancient Lisp machines (https://youtu.be/gV5obrYaogU?t=30), and it was almost shocking to see how many things were familiar and done right from a modern perspective.
Reimplementing new techniques on old hardware. For example, someone implemented real-time shadows and toon shading on N64 (https://youtu.be/VqDAxcWnq3g).
For fun, you can grab RenderDoc (https://renderdoc.org/) and copies of your favourite games to analyse their frames (even via emulation): see how developers implement or fake visual effects and generally how these games render their world.
For instance, Dolphin emulates the GameCube's semi-programmable texture environment unit (TEV) via a pixel shader, and its shader source code is directly visible and editable, with the resulting output shown in the texture viewer. With the aid of textures, you can implement refraction, reflection and heat haze, among other effects.
Retro Game Mechanics Explained talks about both hardware and software concepts: https://www.youtube.com/@RGMechEx/videos
* I use RenderDoc a lot both for fun and at work.
* I've read the Dolphin blog every now and then.
* I love Rodrigo Copetti's website and remember reading everything it had about Nintendo consoles when I first found the site.
* I'm intimately familiar with OIT and used the AMD slides about OIT with linked lists as inspiration for the particle simulation I wrote as my Master's thesis.
* I'm familiar with tiled rendering and have worked with hardware which uses it
All the other stuff you shared is new to me and I will definitely be digging into it!
Fascinating, as are your website's write-ups, thank you for sharing your experiences to the world, awesome work :)
But the H264 decoding is just surreal. Imagine if someone whipped that out in 1996. Is this what they mean when they say sufficiently advanced technology is indistinguishable from magic? :)
1: https://www.amazon.com/Jim-Blinns-Corner-Graphics-Pipeline/d...
Local compute resources are finite, and if you want to target the widest market, then you need to fully utilize mainstream hardware; which means anyone willing to do the grunt work benefits from a higher quality product.
No need to always start by learning on how to render a triangle or a rotating cube from scratch.
There's intermediate level APIs, like the old OpenGL 4 and DirectX 11, or slightly more modern portable alternatives like bgfx and sokol; on the web, there's three.js.
The subject field is complex, the applications are also primarily complex, so there's not much value in something that lets you "easily" drop a few cubes on an endless plain (like the VRML of old days). There's no 3D equivalent of "I'll make myself the web site for my barbershop" that started the Web 20 years ago. (or, to be more pedantic, "the page for my high-energy physics experiment" 30 years ago.) Nowadays even the Web is hellishly complicated. Maybe if VR/AR takes off (any moment now), there will be need for "simple 3D", but I suspect it will be handled by custom "easy" versions of Unity/Unreal rather than people writing to a "simpler 3D API". The way people aren't making barbershop sites in "simple HTML", but going to Squarespace.
They are also used to build stuff like https://www.adriancourreges.com/blog/2016/09/09/doom-2016-gr... and https://mamoniem.com/behind-the-pretty-frames-death-strandin... which really cannot be built with non-specific high-level APIs.
Graphics debuggers themselves are a great example of programs written using low level graphics APIs.
Just excellent if you want to understand what is going on under the hood. Which is necessary if you are writing anything more than a toy graphic program, or understand the output of GPU debugging/profiling tools from Nvidia and AMD.
[0] https://guide.handmade-seattle.com/c/2019/talks/lets-talk-ab...
Not knit picking, genuinely asking how much is this still relevant in context of modern GPUs such as RTX 3090 etc and how much has changed?
I'd guess this Ray tracing etc would be new step somewhere in between?
In other words, it should still be relevant today.