Friday, September 10, 2010

Compile glxgears from source on Ubuntu 9.10

I decided to modify the source code of glxgears to overcome a peculiar problem. This application would not write the FPS information to file (if redirected) unless it is shutdown.

I checked the source code, glxgears.c and found the problem was simply that there was no fflush statement after the output printf:
static void
event_loop(Display *dpy, Window win)
{
....
if (t - t0 >= 5.0) {

GLfloat seconds = t - t0;
GLfloat fps = frames / seconds;
printf("%d frames in %3.1f seconds = %6.3f FPS\n",frames, seconds,
fps); //no flushing!

t0 = t;
frames = 0;
}}}
}
And so, the solution seemed simple: add the line of code that would force the output buffer to flush (fflush(stdout);) after printf and re-compile the source code.

To be able to compile glxgears.c, however one needs to install many other libraries and packages. Here are the steps that I followed to get the whole thing working.
  1. glxgears is part of the Mesa Library. I downloaded th latest source code (MesaLib-7.8.2.tar.gz)

  2. Start by untarring this file:
    tar -xvf MesaLib-7.8.2.tar.gz
  3. Enter the directory Mesa-7.8.2 and type: ./configure --with-driver=xlib. The configuration code will now start spitting out the libraries that it needs (and which your machine does not have). Following are the libraries that I needed on my machine:

    1. libdrm:
      sudo wget http://dri.freedesktop.org/libdrm/libdrm-2.4.21.tar.gz
      tar -xvf libdrm-2.4.21.tar.gz
      cd libdrm-2.4.21/
      ./configure
      make
      make install
    2. xorg-macros:
      wget http://mirrors.kernel.org/ubuntu/pool/main/x/xutils-dev/xutils-dev_7.5+4_i386.deb
      sudo dpkg -r xutils-dev
      sudo dpkg -i xutils-dev_7.5+4_i386.deb
      ls /usr/share/pkgconfig/
    3. dri2proto:
      wget http://xorg.freedesktop.org/releases/individual/proto/dri2proto-2.3.tar.gz
      tar -xvf dri2proto-2.3.tar.gz
      cd dri2proto-2.3/
      ./configure
      make
      make install
    4. glproto:
      wget http://xorg.freedesktop.org/releases/individual/proto/glproto-1.4.12.tar.gz
      tar -xvf glproto-1.4.12.tar.gz
      cd glproto-1.4.12/
      ./configure
      make
    5. xxf86vm:
      sudo apt-get install libxxf86vm-dev
    6. xdamage:
      sudo apt-get install libxdamage-dev
  4. After all the dependencies are satisfied, ./configure --with-driver=xlib will complete and create the make file.

  5. Follow it up with: make. Once you modify the Mesa-7.8.2/progs/xdemos/glxgears.c file, call ./configure --with-driver=xlib and make. The new glxgears executable will be created in the same directory as the souce code.