Section 16.8. X and 3D

Of course, Linux can display not only two-dimensional windows and structures in its graphical environment, but also three-dimensional graphics. There is a de facto standard for programming three-dimensional graphics, OpenGL , which originally came from big-iron Unix workstations, but which Linux supports just fine on inexpensive boards generally available for PCs. In this section, we look at how to set it up.

16.8.1. OpenGL Setup

As with many other subsystems of a free software operating system, Linux gives us a number of choices of OpenGL. Among those are Mesa, TinyGL, and YGL. The most prominent one, and the de facto standard for OpenGL on Linux, is Mesa. GLX

OpenGL itself is platform neutral, so to "glue" OpenGL to a specific windowing system, an extension is required. For X11 this extension is called GLX. GLX contains X protocol extensions to allow OpenGL to be sent over the X socket to the X server. This is called indirect rendering. has another option that is much faster, but works only on the local display. This option is called direct rendering and is explained in the following section. DRI from Version 4 and up contains a framework for allowing direct access to the graphics hardware in a safe and efficient manner. This framework is called Direct Rendering Infrastructure (DRI), and accelerated OpenGL implementations sit on top of this framework. DRI consists of several components:

  • A kernel module for multiplexing the graphics hardware so it can be used by multiple processes. This is called the Direct Rendering Manager (DRM), and the module is hardware specific. The modules are typically located in /lib/modules/2.x.y/kernel/drivers/char/drm. The kernel will normally autoload the correct module when X is started.

  • The 2D driver. For each type of card there is a 2D driver in that initializes the display, performs 2D drawing, and so forth. The drivers are typically located in /usr/X11R6/lib/modules/drivers/.

  • The 3D DRI driver. This component talks to the 3D part of the graphics card and effectively converts OpenGL commands to hardware commands. When using direct rendering, the DRI driver is loaded by so the application can access the graphics card directly without going through X. The DRI drivers are normally located in /usr/X11R6/lib/modules/dri.

  • libGL, which is the OpenGL library that client applications must link to in order to use OpenGL. When using direct rendering, libGL loads the DRI driver and uses it directly, and when using indirect rendering (for example, if the X display is remote), it creates GLX commands that are sent to the X server over the regular X socket. Proprietary drivers

Unfortunately, not all graphics hardware manufacturers want to publish information about how their hardware works. This is especially true for modern 3D accelerated hardware. But fortunately, the XAA driver architecture is binary-compatible even across versions of operating systems (as long as the hardware architecture is the same), so installing a proprieratary binary-only driver is quite easy these days.

NVIDIA and ATI graphics cards are commonly found in PCs today. Newer versions of those cards are not supported by for 3D hardware accelerated graphics, so we need to use the proprietary drivers published by the manufacturer.

The NVIDIA ( driver doesn't seem to use DRI, but the overall design is quite similar. The driver comes as a ready-to-run installer-binary file that builds and installs a kernel module (which corresponds to the DRM driver in DRI) and then installs an 2D XAA driver and replaces the libGL library on the system with an NVIDIA specific one. Notice that the kernel module comes with source code, but the other two components are binary only. For more details about how to install the NVIDIA driver , please read the information available on the company's web site.

ATI ( also provides an accelerated 3D driver for its modern cards for Linux. Unlike NVIDIA's, this one actually uses the DRI framework. Except for that, they work in similar ways: kernel module with source available, binary-only driver, binary-only DRI driver, and a replacement libGL. Configuring for OpenGL

With all the components of OpenGL and the related drivers in place, you can configure your system to use it.

Diagnosis. glxinfo is a valuable tool for setting up for OpenGL. it gives information about the OpenGL capabilities of the current X11 display. Example output from glxinfo is as follows:

$ glxinfo|less
name of display: :0.0
display: :0  screen: 0
direct rendering: Yes
server glx vendor string: SGI

server glx version string: 1.2
server glx extensions:
GLX_ARB_multisample, ...
client glx vendor string: SGI
client glx version string: 1.4
client glx extensions:
GLX_ARB_get_proc_address, ...
GLX extensions:
GLX_ARB_get_proc_address, ...
OpenGL vendor string: Tungsten Graphics, Inc.
OpenGL renderer string: Mesa 
 DRI Radeon 20030328 AGP 1x x86/MMX/SSE2 TCL
OpenGL version string: 1.2 Mesa 6.1
OpenGL extensions:

This listing shows that we are currently using direct rendering with a Mesa-based DRI driver for an ATI Radeon graphics card. If hardware acceleration was not set up properly or something did not work, it would say direct rendering: No instead.

Altering xorg.conf . To get started using DRI, a couple of lines need to be added to the xorg.conf file shown earlier in this chapter:

Section "Module"
# This loads the GLX module
Load       "glx"
# This loads the DRI module
Load       "dri"
Section "DRI"
Mode 0666

The Load statements take care of loading the modules required for OpenGL into the X server, and the Mode statement in the DRI section sets the file permission of the character special file that applications use to communicate with the DRM kernel driver. The special file is /dev/dri/cardN. Setting the permissions to 0666 allows all users with access to the X display to use hardware-accelerated OpenGL. Mesa

Mesa is a 3D graphics library with an API very similar to that of OpenGL. The core Mesa library is licensed according to the terms of the copyright (an MIT-style license). Mesa is included with together with the DRI framework, but if you want to use OpenGL on a platform not supported by DRI or want to get started with OpenGL programming, installing your own copy of Mesa can be a good ideaif not for anything else, then for getting the source code for the example programs.

Installing Mesa. If, for some reason, you want to compile Mesa yourself, it is a simple matter of downloading the latest MesaLib (along with MesaDemos) from, unpacking it, and compiling it. The current version of Mesa does not use GNU autoconf; instead it comes with a Makefile that contains targets for a large number of operating systems and hardware combinations:

$ tar xfj MesaLib-6.2.1.tar.bz2 
$ tar xfj MesaDemos-6.2.1.tar.bz2 
$ cd Mesa-6.2.1 
$ make                  Will write a list of supported build targets
$ make linux-x86        We choose Linux/x86

When the build is complete, run some of the demos to check that everything works:

$ cd lib
$ cd ../progs/demos
$ ./gears

If the demo application worked, install Mesa like this:

$ cd ../../ 
$ cp -r include/GL /usr/local/mesa/include   Install header files
$ cp -d lib/* /usr/local/mesa/lib            Install libs

Hiding the headers and libraries away in /usr/local/mesa allows you to easily switch between the system-provided OpenGL and Mesa by setting LD_LIBRARY_PATH to include or exclude /usr/local/mesa/lib.

Part I: Enjoying and Being Productive on Linux
Part II: System Administration