Wednesday, April 06, 2011

Off-screen OpenGL drawing without a graphics card using Mesa 7.6.1 on CentOS 5

So let's say you wanna rasterize a couple of polygons using OpenGL, but don't have access to a graphics card. Maybe yours broke - or maybe you're on a 6000 node render farm without a single GPU in sight. Either way you're just plain out of luck when it comes using regular OpenGL.

The Mesa 3D Graphics Library could save your day! It's been around since the dawn of time, was started by this guy named Brian Paul and as the website states:
"Mesa is an open-source implementation of the OpenGL specification - a system for rendering interactive 3D graphics.
A variety of device drivers allows Mesa to be used in many different environments ranging from software emulation to complete hardware acceleration for modern GPUs.
Mesa ties into several other open-source projects: the Direct Rendering Infrastructure and to provide OpenGL support to users of X on Linux, FreeBSD and other operating systems."
Ok, but of what use is it to you? It turns out that among many other nifty things Mesa includes an off-screen rendering engine which allows you to render into any buffer, rather than the framebuffer on your non-existent graphics card. It's in other words a software version of OpenGL so not nearly as fast as your average graphics board, but hey, it's exactly that kind of hardware independence that we're after in the first place, right?


Being on CentOS, we're as usual at a rather tiresome disadvantage due to the pletora of ancient - albeit very stable - drivers in the distro, many of which you may not want (or more likely, be allowed) to fully replace, so throughout this installation guide I'll make frequent of use of the --prefix argument to "configure" in order to redirect my installations and builds to some local directory rather than the restricted /usr area.

Let's get started! First of all, CentOS 5 ships with a rather old x-org server which means the latest Mesa versions don't seem to want to build with it. Instead we'll settle for version 7.6.1, which seemingly has all the off-screen capabilities we'd like, while still working with CentOS 5. (as always, don't take my word for it though - try for yourself!). Grab it here: (md5: 7db4617e9e10ad3aca1b64339fd71b7d)

However, before we can build Mesa we need a couple of prerequisites:


According to Linux From Scratch,
The libpthread-stubs package provides weak aliases for pthread functions not provided in libc or otherwise available by default. This is useful for libraries that rely on pthread stubs to use pthreads optionally. On Linux, all necessary pthread functions are available, so this package is simply a placeholder.
Just download, untar, and:

> ./configure --prefix=/home/username/local
> ./make 
> ./make install

Make sure to change "/home/username/local" to wherever you want to install your Mesa setup, or ignore the prefix switch altogether AT YOUR OWN PERIL, as this will permanently change your machine configuration and likely affect other users as well.


Again according to Linux From Scratch,
"libdrm provides core library routines for the X Window System to directly interface with video hardware using the Linux kernel's Direct Rendering Modules."
Unfortunately we can't just grab the latest version from due to a conflict with the Nouveau driver (, so instead let's grab 2.4.20 from here: (md5: 3c56e03172b236e14905ef9a68ba2f97)

Again, download and untar, but this time:

> env PKG_CONFIG_PATH=/home/username/local/lib/pkconfig./configure --prefix=/home/username/local
> ./make 
> ./make install

Same thing again with the prefix path, but with the addition of PKG_CONFIG_PATH in order to tell autoconf where our new, custom package config resides.

Now repeat this with dri2proto-2.1 and glproto-1.4.11 as well: (md5 5cb7987d29db068153bdc8f23c767c43) (md5  78e7c4dc7dcb74b1869fee7897e00f59)

That should do it! Now let's build and install Mesa. However, the autoconfigure options aren't super clear and may not give us all the options we need, so instead let's do an old-school build:

> make realclean
> make linux-x86-64

This is for my specific 64-bit Intel rig - go ahead and try other options which may suit you better. You can get a listing of available ones by simply typing 'make'.

After the build has succeeded, but before we can install Mesa, we'll need to tweak the install path manually as we're not using autoconf. Open configs/default and set INSTALL_DIR to match the path used above:

INSTALL_DIR = /home/username/local

Save and close, and now you can:

> make install

Alright, that should do it! Let's try it out...


If you pull down the "Mesa Demos" tarball from: (md5 a4226f06732a02556fcf6be290b86dff)

you'll find a very simple and instructive demo program in progs/osdemos. To try it out, simply:

> env LD_LIBRARY_PATH=/home/username/local/lib64:{LD_LIBRARY_PATH} make
> env LD_LIBRARY_PATH=/home/username/local/lib64:{LD_LIBRARY_PATH} ./osdemo test.tga

There are obviously many other ways to infuse the newly installed directory into your LD library path. Eventually you'll also want to point your compiler at the new include path at /home/username/local/include.

Once you've successfully run the demo program, load up "test.tga" in your image viewer of choice and behold a couple of really awesome colorful primitives:

100% rendered in software (this particular one is from an 8-core Intel Xeon blade server located somewhere in the American midwest).

No comments:

Post a Comment