Upto: Table of Contents of full book "Programming the Raspberry Pi GPU"

Introduction to the Raspberry Pi

The Raspberry Pi (RPi) is a low cost Linux computer developed with the intention of improving the background of students entering university computer science courses by giving them a good, cheap environment in which to play. And its true! I've got a bunch of colleagues at work, well into middle-age who have leapt upon it to play with. So far their kids haven't had a look-in though...



The Raspberry Pi (RPi) is a small single-board computer developed originally for the purpose of teaching computer science in schools. It uses a "System on a Chip" (SoC) and is characterised by a small form factor, low power, not much RAM, limited inputs and outputs and disk storage either by an SD card or by external storage using a USB port.

There is a growing eco-system of such computers, and sites such as the Element14 Single Board Computers Line Card or the Wikipedia Single-board computer will list many of them.

Now this doesn't seem very exciting, but nevertheless the RPi has captured the imagination (and the pockets!) with almost four million sold, while Amazon lists nearly 500 books related to the RPi.

So what is it? Well, it is a single board computer, about the size of a credit card. The Model B has 512Mb RAM, 2 USB ports and an Ethernet port. It has HDMI, and analogue audio and video outputs. From the FAQ

The SoC is a Broadcom BCM2835. This contains an ARM1176JZFS, with floating point, running at 700Mhz, and a Videocore 4 GPU. The GPU is capable of BluRay quality playback, using H.264 at 40MBits/s. It has a fast 3D core accessed using the supplied OpenGL ES2.0 and OpenVG libraries.

Graphically it looks like

and physically it looks like

For its target, this is a wonderful computer. Its price point means that if it breaks, well, get another one. It runs a full-blown Linux system (but not Windows) and can be used in a huge variety of situations (including as an array of RPi's to build a super-computer, or hooked up to sensors as a participant in the Internet of Things). But if you want to use a single RPi for heavy-duty computation, well, it sucks. From Wikipedia

On the CPU level the performance is similar to a 300 MHz Pentium II of 1997-1999.

The GPU, on the other hand, is quite classy. From the RPi FAQ

The GPU is capable of 1Gpixel/s, 1.5Gtexel/s or 24 GFLOPs of general purpose compute and features a bunch of texture filtering and DMA infrastructure.

The GPU provides Open GL ES 2.0, hardware-accelerated OpenVG, and 1080p30 H.264 high-profile encode and decode.

Now this isn't top of the range GPU performance. A review of the NVidia GTX 980 gave 72 Gpixels/s, but then that single graphics card is ten times the cost of the whole RPi!

There are a number of Linux images available from the Raspberry Pi site, and others being developed elsewhere. I use the Debian-based image which essentially comes in two forms: with soft float using Debian and with hard float using the FPU, called Raspbian. The hard float image is required for decent floating point performance, and there isn't really any justification for using the soft float any more.

ELinux.org maintains a list of RPi Distributions There are many standard Linux distros included here, such as Fedora, Debian, Arch, SUSE, Gentoo and others. The RPi has gained traction as a media centre based on the XBMC media centre, and this is represented by distros such as Raspbmc and OpenElec.

It doesn't really matter which distro you use, or even whether or not you use X Window or command line. We will be bypassing those.

There don't seem to be any books which go into detail about the Broadcom VideoCore GPU used on the RPi. There are several tutorials and a set of examples from Broadcom. I bumped into a need to do this kind of programming while trying to turn the RPi into a Karaoke machine: with the CPU busting its gut rendering MIDI files there was nothing left for showing images such as Karaoke lyrics except for the GPU, and nothing really to tell me how to do it.

This book scratches my itch. Since I've had to learn a lot about RPi GPU programming, I might as well share it. Let's be honest: much of this is about sharing the pain. What started as a side issue turned into a full-blown project of its own, and this stuff is hard.

I will look at OpenGL ES and OpenMAX. Along the way we need to cover some Dispmanx. Later I will look at OpenVG and if possible some GPGPU programming - but not for a while.

If you can't program in C, then go away. If you can't do concurrent programming using threads then you will probably get glassy-eyed at various points. You need to be able to read formal specifications and pay attention to all-the-finicky-details-or-your-application-won't work. And you will need to spend a lot of time experimenting, working through unexplained "features", buggy behaviour and not-yet-implemented core elements. Get used to staring at your screen in a totally bemused fashion, with a debugger running, the specification open on one side, half-a-dozen web sites open on the other. I've found that a good bottle (or two) of red wine help - not with the programming, but for handling the frustration!

I'll try to do the best I can with explanations. This book will make slow progress. I typically start off with a few chapters and build it up as I learn and write more. But in this area my productivity is way down, so it will take time!

Programming style

There are many different programming styles that can be adopted. Regrettably this book uses most of them. I have taken code and specifications from the following places:

My preferred brace style for C programming is the "one true brace" with the opening brace on the same line as its conditional statement.

if (x) {

The IL Client library uses the Allman style (brace on a new line)

if (x)

while the Programming Guide uses the Whitesmiths style (brace on a new line, indented).

if (x)

For variable names, I prefer delimiting words with the underscore '_' as in my_vbl though after many years of Java programming I am also happy with CamelCase (myVbl). The IL Client library uses '_' separation, the Programming Guide uses CamelCase.

The OpenMAX specification uses a simplified form of Hungarian notation for struct field types such as pNativeRender but prefixes all function calls with OMX_ and the struct names themselves are all upper case such as OMX_AUDIO_PORTDEFINITIONTYPE.

The OpenGL ES specification uses CamelCase for its function names, all beginning with gl such as glValidateProgram

It isn't possible to get one consistent style thoughout the book, so I haven't even tried for this edition. Please be tolerant.


This chapter has given a brief overview of the RPi and what this book will attempt to cover.

Copyright © Jan Newmarch, jan@newmarch.name

If you like this book, please contribute using Flattr
or donate using PayPal

mbeat powered usb hub