Merge branch 'arb_half_float_vertex'
diff --git a/configs/autoconf.in b/configs/autoconf.in
index c6d315e..d9fa2da 100644
--- a/configs/autoconf.in
+++ b/configs/autoconf.in
@@ -85,7 +85,6 @@
 DRI_DIRS = @DRI_DIRS@ 
 WINDOW_SYSTEM = @WINDOW_SYSTEM@
 EGL_DISPLAYS = @EGL_DISPLAYS@
-USING_EGL = @USING_EGL@
 
 # Dependencies
 X11_INCLUDES = @X11_INCLUDES@
diff --git a/configs/default b/configs/default
index ec9670d..3aa136b 100644
--- a/configs/default
+++ b/configs/default
@@ -93,15 +93,15 @@
 PROGRAM_DIRS = demos redbook samples glsl objviewer xdemos
 
 # EGL drivers to build
-EGL_DRIVERS_DIRS = demo
+EGL_DRIVERS_DIRS = glx
 
 # Gallium directories and 
 GALLIUM_DIRS = auxiliary drivers state_trackers
 GALLIUM_AUXILIARIES = $(TOP)/src/gallium/auxiliary/libgallium.a
 GALLIUM_DRIVERS_DIRS = softpipe failover svga i915 i965 r300 trace identity
 GALLIUM_DRIVERS = $(foreach DIR,$(GALLIUM_DRIVERS_DIRS),$(TOP)/src/gallium/drivers/$(DIR)/lib$(DIR).a)
-GALLIUM_WINSYS_DIRS = xlib egl_xlib
-GALLIUM_WINSYS_DRM_DIRS =
+GALLIUM_WINSYS_DIRS = drm xlib
+GALLIUM_WINSYS_DRM_DIRS = swrast
 GALLIUM_STATE_TRACKERS_DIRS = glx
 
 # native displays EGL should support
diff --git a/configs/linux-dri b/configs/linux-dri
index ff9bcc9..95f77ad 100644
--- a/configs/linux-dri
+++ b/configs/linux-dri
@@ -55,7 +55,7 @@
 PROGRAM_DIRS := egl $(PROGRAM_DIRS)
 
 # EGL directories
-EGL_DRIVERS_DIRS = demo glx
+EGL_DRIVERS_DIRS = glx
 
 DRIVER_DIRS = dri
 WINDOW_SYSTEM = dri
diff --git a/configs/linux-dri-xcb b/configs/linux-dri-xcb
index 827fb97..7b99dcc 100644
--- a/configs/linux-dri-xcb
+++ b/configs/linux-dri-xcb
@@ -47,21 +47,8 @@
 GL_LIB_DEPS   = $(EXTRA_LIB_PATH) -lX11 -lXext -lXxf86vm -lm -lpthread -ldl \
                 $(LIBDRM_LIB) $(shell pkg-config --libs xcb) $(shell pkg-config --libs x11-xcb) $(shell pkg-config --libs xcb-glx)
 
-
-# This is now 0 by default since it seems to confuse the hell out of people
-# and generate a lot of extra noise on bugzilla.  If you need to build with
-# EGL, do 'make linux-dri USING_EGL=1'
-
-USING_EGL=0
-
-# Directories
-ifeq ($(USING_EGL), 1)
-SRC_DIRS = egl glx/x11 gallium mesa glu glut/glx glew glw
-PROGRAM_DIRS = egl
-else
 SRC_DIRS = glx/x11 gallium mesa glu glut/glx glew glw
 PROGRAM_DIRS = xdemos
-endif
 
 DRIVER_DIRS = dri
 WINDOW_SYSTEM=dri
diff --git a/configs/linux-egl b/configs/linux-egl
index e906806..6494a68 100644
--- a/configs/linux-egl
+++ b/configs/linux-egl
@@ -46,12 +46,6 @@
                 $(LIBDRM_LIB)
 
 
-# This is now 0 by default since it seems to confuse the hell out of people
-# and generate a lot of extra noise on bugzilla.  If you need to build with
-# EGL, do 'make linux-dri USING_EGL=1'
-
-USING_EGL=0
-
 # Directories
 SRC_DIRS = gallium mesa gallium/winsys glu egl
 PROGRAM_DIRS = egl
diff --git a/configs/linux-opengl-es b/configs/linux-opengl-es
index 566cd75..259c26a 100644
--- a/configs/linux-opengl-es
+++ b/configs/linux-opengl-es
@@ -9,19 +9,20 @@
 SRC_DIRS = egl glsl mesa/es gallium gallium/winsys
 PROGRAM_DIRS = es1/screen es1/xegl es2/xegl
 
+# egl st needs this
+DEFINES += -DGLX_DIRECT_RENDERING
+
 # no mesa or egl drivers
 DRIVER_DIRS =
 EGL_DRIVERS_DIRS =
 
 GALLIUM_DRIVERS_DIRS = softpipe
 
-# build egl_softpipe.so
-GALLIUM_WINSYS_DIRS = egl_xlib
-# and libGLES*.so
+# build libGLES*.so
 GALLIUM_STATE_TRACKERS_DIRS = es
 
-# build egl_i915.so
+# build egl_x11_{swrast,i915}.so
 GALLIUM_DRIVERS_DIRS += trace i915
 GALLIUM_STATE_TRACKERS_DIRS += egl
 GALLIUM_WINSYS_DIRS += drm
-GALLIUM_WINSYS_DRM_DIRS = intel
+GALLIUM_WINSYS_DRM_DIRS += intel swrast
diff --git a/configure.ac b/configure.ac
index 619ed47..370bd1b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -699,10 +699,6 @@
     ;;
 esac
 
-dnl Just default to no EGL for now
-USING_EGL=0
-AC_SUBST([USING_EGL])
-
 dnl Set DRI_DIRS, DEFINES and LIB_DEPS
 if test "$mesa_driver" = dri; then
     # Use TLS in GLX?
@@ -710,10 +706,6 @@
         DEFINES="$DEFINES -DGLX_USE_TLS -DPTHREADS"
     fi
 
-    if test "x$USING_EGL" = x1; then
-        PROGRAM_DIRS="egl"
-    fi
-
     # Platform specific settings and drivers to build
     case "$host_os" in
     linux*)
@@ -1173,7 +1165,7 @@
     dri)
         GALLIUM_STATE_TRACKERS_DIRS="dri"
         if test "x$enable_egl" = xyes; then
-            GALLIUM_STATE_TRACKERS_DIRS="$GALLIUM_STATE_TRACKERS_DIRS egl egl_g3d"
+            GALLIUM_STATE_TRACKERS_DIRS="$GALLIUM_STATE_TRACKERS_DIRS egl"
         fi
         # Have only tested st/xorg on 1.6.0 servers
         PKG_CHECK_MODULES(XORG, [xorg-server >= 1.6.0],
@@ -1231,7 +1223,7 @@
     # verify the requested driver directories exist
     egl_displays=`IFS=', '; echo $with_egl_displays`
     for dpy in $egl_displays; do
-        test -d "$srcdir/src/gallium/state_trackers/egl_g3d/$dpy" || \
+        test -d "$srcdir/src/gallium/state_trackers/egl/$dpy" || \
             AC_MSG_ERROR([EGL display '$dpy' does't exist])
     done
     EGL_DISPLAYS="$egl_displays"
@@ -1319,6 +1311,18 @@
     GALLIUM_DRIVERS_DIRS="$GALLIUM_DRIVERS_DIRS nouveau nv04 nv10 nv20 nv30 nv40 nv50"
 fi
 
+dnl
+dnl Gallium swrast configuration
+dnl
+AC_ARG_ENABLE([gallium-swrast],
+    [AS_HELP_STRING([--enable-gallium-swrast],
+        [build gallium swrast @<:@default=disabled@:>@])],
+    [enable_gallium_swrast="$enableval"],
+    [enable_gallium_swrast=auto])
+if test "x$enable_gallium_swrast" = xyes; then
+    GALLIUM_WINSYS_DRM_DIRS="$GALLIUM_WINSYS_DRM_DIRS swrast"
+fi
+
 dnl prepend CORE_DIRS to SRC_DIRS
 SRC_DIRS="$CORE_DIRS $SRC_DIRS"
 
diff --git a/docs/egl.html b/docs/egl.html
index 9bbce83..0882598 100644
--- a/docs/egl.html
+++ b/docs/egl.html
@@ -32,10 +32,10 @@
 the Gallium driver for your hardware.  For example</p>
 
 <pre>
-  $ ./configure --with-state-trackers=egl_g3d,es,vega --enable-gallium-intel
+  $ ./configure --with-state-trackers=egl,es,vega --enable-gallium-intel
 </pre>
 
-<p>The main library will be enabled by default.  The <code>egl_g3d</code> state
+<p>The main library will be enabled by default.  The <code>egl</code> state
 tracker is needed by a number of EGL drivers.  EGL drivers will be covered
 later.  The <a href="opengles.html">es state tracker</a> provides OpenGL ES 1.x
 and 2.x and the <a href="openvg.html">vega state tracker</a> provides OpenVG
@@ -77,9 +77,17 @@
 
 <p>The argument is a comma separated string.  It is usually used to specify the
 rendering APIs, like OpenGL ES or OpenVG, to build.  But it should be noted
-that a number of EGL drivers depend on the <code>egl_g3d</code> state tracker.
-They will <em>not</em> be built without the <code>egl_g3d</code> state
-tracker.</p>
+that a number of EGL drivers depend on the <code>egl</code> state tracker.
+They will <em>not</em> be built without the <code>egl</code> state tracker.</p>
+
+</li>
+
+<li><code>--enable-gallium-swrast</code>
+
+<p>This option is not specific to EGL.  But if there is no driver for your
+hardware, or you are experiencing problems with the hardware driver, you can
+enable the swrast DRM driver.  It is a dummy driver and EGL will fallback to
+software rendering automatically.</p>
 
 </li>
 </ul>
@@ -151,7 +159,7 @@
 
 <p>Gallium EGL drivers supports all rendering APIs specified in EGL 1.4.  The
 support for optional EGL functions and EGL extensions is usually more complete
-than the classic ones.  These drivers depend on the <code>egl_g3d</code> state
+than the classic ones.  These drivers depend on the <code>egl</code> state
 tracker to build.  The available drivers are</p>
 
 <ul>
@@ -159,6 +167,7 @@
 <li><code>egl_&lt;dpy&gt;_i965</code></li>
 <li><code>egl_&lt;dpy&gt;_radeon</code></li>
 <li><code>egl_&lt;dpy&gt;_nouveau</code></li>
+<li><code>egl_&lt;dpy&gt;_swrast</code></li>
 <li><code>egl_&lt;dpy&gt;_vmwgfx</code></li>
 </ul>
 
@@ -182,12 +191,12 @@
 <li><code>egl_xdri</code>
 
 <p>This driver supports the X Window System as its window system.  It functions
-as a DRI driver loader.  Unlike <code>egl_glx</code>, it has no dependency on
-<code>libGL</code>.  It talks to the X server directly using DRI or DRI2
-protocols.  It also talks minimal GLX protocol for things like available
-visuals or fbconfigs.  With direct access to the DRI drivers, it has the
-potential to support more EGL functions that are not possible with
-<code>egl_glx</code></p>
+as a DRI driver loader and can load DRI/DRI2/DRISW drivers.  Unlike
+<code>egl_glx</code>, it has no dependency on <code>libGL</code>.  It talks to
+the X server directly using DRI or DRI2 protocols.  It also talks minimal GLX
+protocol for things like available visuals or fbconfigs.  With direct access to
+the DRI drivers, it has the potential to support more EGL functions that are
+not possible with <code>egl_glx</code>.</p>
 
 </li>
 <li><code>egl_dri</code>
@@ -208,8 +217,8 @@
 <h2>Developers</h2>
 
 The sources of the main library and the classic drivers can be found at
-<code>src/egl/</code>.  The sources of the <code>egl_g3d</code> state tracker
-can be found at <code>src/gallium/state_trackers/egl_g3d/</code>.
+<code>src/egl/</code>.  The sources of the <code>egl</code> state tracker can
+be found at <code>src/gallium/state_trackers/egl/</code>.
 
 <h3>TODOs</h3>
 
diff --git a/docs/lists.html b/docs/lists.html
index 5227fbd..9c17a9f 100644
--- a/docs/lists.html
+++ b/docs/lists.html
@@ -13,36 +13,41 @@
 </p>
 
 <ul>
-<li><a href="https://lists.sourceforge.net/lists/listinfo/mesa3d-announce"
-target="_parent">mesa3d-announce</a> - announcements of new Mesa
-versions are sent to this list.
-</li>
-<br>
 <li><a href="https://lists.sourceforge.net/lists/listinfo/mesa3d-users"
-target="_parent">mesa3d-users</a> - intended for users of the Mesa and DRI.
-Newbie questions are appropriate, but please try the general OpenGL
+target="_parent">mesa3d-users</a> - intended for end-users of Mesa and DRI
+drivers.  Newbie questions are OK, but please try the general OpenGL
 resources and Mesa/DRI documentation first.
 </li>
 <br>
 <li><a href="https://lists.sourceforge.net/lists/listinfo/mesa3d-dev"
-target="_parent">mesa3d-dev</a> - for discussion of Mesa and Direct Rendering 
-Infrastructure development. Not for beginners.
+target="_parent">mesa3d-dev</a> - for Mesa, Gallium and DRI development
+discussion.  Not for beginners.
 </li>
 <br>
 <li><a href="http://lists.freedesktop.org/mailman/listinfo/mesa-commit"
 target="_parent">mesa-commit</a> - relays git check-in messages
 (for developers).
+In general, people should not post to this list.
+</li>
 <br>
-Note: the old mesa3d-cvs list is no longer in use.
+<li><a href="https://lists.sourceforge.net/lists/listinfo/mesa3d-announce"
+target="_parent">mesa3d-announce</a> - announcements of new Mesa
+versions are sent to this list.  Very low traffic.
 </li>
 </ul>
 
+<p>
+Follow the links above for list archives.
+</p>
+
 <p>For mailing lists about Direct Rendering Modules (drm) in Linux/BSD 
-kernels, see <a href="http://dri.freedesktop.org/wiki/MailingLists">wiki</a>.
+kernels, see the
+<a href="http://dri.freedesktop.org/wiki/MailingLists" target="_parent">
+DRI wiki</a>.
+</p>
 
 <p>
-<b>Notice</b>: non-member posts to any of these lists will be automatically
-rejected.
+<b>Notice</b>: You must subscribe to these lists in order to post to them.
 </p>
 
 
diff --git a/docs/opengles.html b/docs/opengles.html
index a871e85..fc41e67 100644
--- a/docs/opengles.html
+++ b/docs/opengles.html
@@ -20,7 +20,7 @@
 
 <h2>Build the Libraries</h2>
 <ol>
-<li>Run <code>configure</code> with <code>--with-state-trackers=egl_g3d,es</code> and enable the Gallium driver for your hardware.</li>
+<li>Run <code>configure</code> with <code>--with-state-trackers=egl,es</code> and enable the Gallium driver for your hardware.</li>
 <li>Build and install Mesa as usual.</li>
 </ol>
 
diff --git a/docs/openvg.html b/docs/openvg.html
index cd39b13..cdf6b57 100644
--- a/docs/openvg.html
+++ b/docs/openvg.html
@@ -34,7 +34,7 @@
 <h3>Sample build</h3>
 A sample build looks as follows:
 <pre>
-  $ ./configure --with-state-trackers=egl_g3d,vega --enable-gallium-intel
+  $ ./configure --with-state-trackers=egl,vega --enable-gallium-intel
   $ make
   $ make install
 </pre>
diff --git a/docs/sourcetree.html b/docs/sourcetree.html
index bc4ce71..00dc4e7 100644
--- a/docs/sourcetree.html
+++ b/docs/sourcetree.html
@@ -48,6 +48,7 @@
 	<li><b>glslcompiler</b> - a stand-alone GLSL compiler driver
         <li>XXX more
         </ul>
+    <li><b>es</b> - OpenGL ES overlay, parallelly buildable with the core Mesa
     <li><b>math</b> - vertex array translation and transformation code
         (not used with Gallium)
     <li><b>ppc</b> - Assembly code/optimizations for PPC systems
@@ -123,20 +124,19 @@
       </ul>
     <li><b>state_trackers</b> -
        <ul>
-       <li><b>dri</b> -
-       <li><b>egl</b> -
-       <li><b>es</b> -
+       <li><b>dri</b> - Meta state tracker for DRI drivers
+       <li><b>egl</b> - Meta state tracker for EGL drivers
+       <li><b>es</b> - OpenGL ES 1.x and 2.x state trackers
        <li><b>g3dvl</b> -
-       <li><b>glx</b> -
+       <li><b>glx</b> - Meta state tracker for GLX
        <li><b>python</b> -
-       <li><b>vega</b> -
+       <li><b>vega</b> - OpenVG 1.x state tracker
        <li><b>wgl</b> -
-       <li><b>xorg</b> -
+       <li><b>xorg</b> - Meta state tracker for Xorg video drivers
        </ul>
     <li><b>winsys</b> -
        <ul>
        <li><b>drm</b> -
-       <li><b>egl_xlib</b> -
        <li><b>g3dvl</b> -
        <li><b>gdi</b> -
        <li><b>xlib</b> -
diff --git a/progs/trivial/tri-fbo-tex-mip.c b/progs/trivial/tri-fbo-tex-mip.c
index df4725c..2e8fb74 100644
--- a/progs/trivial/tri-fbo-tex-mip.c
+++ b/progs/trivial/tri-fbo-tex-mip.c
@@ -6,7 +6,6 @@
 #include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <math.h>
 
 /* For debug */
 
diff --git a/progs/xdemos/glxgears.c b/progs/xdemos/glxgears.c
index 088f25a..2993c82 100644
--- a/progs/xdemos/glxgears.c
+++ b/progs/xdemos/glxgears.c
@@ -771,6 +771,7 @@
    glDeleteLists(gear1, 1);
    glDeleteLists(gear2, 1);
    glDeleteLists(gear3, 1);
+   glXMakeCurrent(dpy, None, NULL);
    glXDestroyContext(dpy, ctx);
    XDestroyWindow(dpy, win);
    XCloseDisplay(dpy);
diff --git a/progs/xdemos/offset.c b/progs/xdemos/offset.c
index 314a4fc..3858a5b 100644
--- a/progs/xdemos/offset.c
+++ b/progs/xdemos/offset.c
@@ -94,6 +94,9 @@
 
 static int dimension = 3;
 
+static float Scale = 1.0;
+
+
 int main(int argc, char** argv) {
     Display *dpy;
     XVisualInfo *vi;
@@ -182,6 +185,7 @@
 #else
     glEnable(GL_POLYGON_OFFSET_FILL);
 #endif
+    glScalef(Scale, Scale, Scale);
     cubes(mx, my, HIDDEN_LINE);
 #ifdef GL_EXT_polygon_offset
     glDisable(GL_POLYGON_OFFSET_EXT);
@@ -289,6 +293,12 @@
 	case KeyPress:
 	    (void) XLookupString(&event.xkey, buf, sizeof(buf), &keysym, NULL);
 	    switch (keysym) {
+            case 'Z':
+               Scale *= 1.1;
+               break;
+            case 'z':
+               Scale *= 0.9;
+               break;
 	    case XK_Escape:
 		exit(EXIT_SUCCESS);
 	    default:
diff --git a/src/egl/drivers/Makefile.template b/src/egl/drivers/Makefile.template
new file mode 100644
index 0000000..02a65b0
--- /dev/null
+++ b/src/egl/drivers/Makefile.template
@@ -0,0 +1,51 @@
+# src/egl/drivers/Makefile.template
+#
+# Drivers should define
+#
+# EGL_DRIVER, the driver name
+# EGL_SOURCES, the driver sources
+# EGL_INCLUDES, the include pathes
+# EGL_CFLAGS, additional CFLAGS
+# EGL_LIBS, additional LIBS
+#
+# before including this template.
+#
+
+
+EGL_DRIVER_PATH = $(TOP)/$(LIB_DIR)/$(EGL_DRIVER)
+EGL_OBJECTS = $(EGL_SOURCES:.c=.o)
+
+
+default: depend $(EGL_DRIVER_PATH)
+
+$(EGL_DRIVER_PATH): $(EGL_DRIVER)
+	$(INSTALL) $< $(TOP)/$(LIB_DIR)
+
+$(EGL_DRIVER): $(EGL_OBJECTS) Makefile $(TOP)/src/egl/drivers/Makefile.template
+	@$(MKLIB) -o $(EGL_DRIVER) -noprefix \
+		-linker '$(CC)' -ldflags '$(LDFLAGS)' \
+		-L$(TOP)/$(LIB_DIR) $(MKLIB_OPTIONS) \
+		$(EGL_OBJECTS) $(EGL_LIBS)
+
+.c.o:
+	$(CC) -c $(EGL_INCLUDES) $(CFLAGS) $(EGL_CFLAGS) $< -o $@
+
+
+install: $(EGL_DRIVER_PATH)
+	$(INSTALL) -d $(DESTDIR)$(INSTALL_LIB_DIR)
+	$(MINSTALL) $(EGL_DRIVER_PATH) $(DESTDIR)$(INSTALL_LIB_DIR)
+
+clean:
+	rm -f $(EGL_DRIVER)
+	rm -f $(EGL_OBJECTS)
+	rm -f depend depend.bak
+
+depend: $(EGL_SOURCES)
+	@ echo "running $(MKDEP)"
+	@ rm -f depend
+	@ touch depend
+	$(MKDEP) $(MKDEP_OPTIONS) $(EGL_INCLUDES) $(EGL_SOURCES) \
+		>/dev/null 2>/dev/null
+
+sinclude depend
+# DO NOT DELETE
diff --git a/src/egl/drivers/demo/Makefile b/src/egl/drivers/demo/Makefile
deleted file mode 100644
index 444dfb3..0000000
--- a/src/egl/drivers/demo/Makefile
+++ /dev/null
@@ -1,32 +0,0 @@
-# src/egl/drivers/demo/Makefile
-
-TOP = ../../../..
-include $(TOP)/configs/current
-
-
-INCLUDE_DIRS = -I$(TOP)/include -I$(TOP)/src/egl/main $(X11_INCLUDES)
-
-
-SOURCES = demo.c
-
-OBJECTS = $(SOURCES:.c=.o)
-
-
-.c.o:
-	$(CC) -c $(INCLUDE_DIRS) $(CFLAGS) $< -o $@
-
-
-
-default: $(TOP)/$(LIB_DIR)/demodriver.so
-
-
-$(TOP)/$(LIB_DIR)/demodriver.so: $(OBJECTS)
-	$(MKLIB) -o demodriver.so -noprefix -linker '$(CC)' \
-		-ldflags '$(LDFLAGS)' -install $(TOP)/$(LIB_DIR) \
-		$(OBJECTS)
-
-install:
-
-clean:
-	-rm -f *.o
-	-rm -f *.so
diff --git a/src/egl/drivers/demo/demo.c b/src/egl/drivers/demo/demo.c
deleted file mode 100644
index 0933c0b..0000000
--- a/src/egl/drivers/demo/demo.c
+++ /dev/null
@@ -1,289 +0,0 @@
-/*
- * Sample driver: Demo
- */
-
-#include <assert.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include "eglconfig.h"
-#include "eglcontext.h"
-#include "egldisplay.h"
-#include "egldriver.h"
-#include "eglglobals.h"
-#include "eglmode.h"
-#include "eglscreen.h"
-#include "eglsurface.h"
-
-
-/**
- * Demo driver-specific driver class derived from _EGLDriver
- */
-typedef struct demo_driver
-{
-   _EGLDriver Base;  /* base class/object */
-   unsigned DemoStuff;
-} DemoDriver;
-
-#define DEMO_DRIVER(D) ((DemoDriver *) (D))
-
-
-/**
- * Demo driver-specific surface class derived from _EGLSurface
- */
-typedef struct demo_surface
-{
-   _EGLSurface Base;  /* base class/object */
-   unsigned DemoStuff;
-} DemoSurface;
-
-
-/**
- * Demo driver-specific context class derived from _EGLContext
- */
-typedef struct demo_context
-{
-   _EGLContext Base;  /* base class/object */
-   unsigned DemoStuff;
-} DemoContext;
-
-
-
-static EGLBoolean
-demoInitialize(_EGLDriver *drv, _EGLDisplay *disp, EGLint *major, EGLint *minor)
-{
-   _EGLScreen *scrn;
-   EGLint i;
-
-   /* Create a screen */
-   scrn = calloc(1, sizeof(*scrn));
-   _eglAddScreen(disp, scrn);
-
-   /* Create the screen's modes - silly example */
-   _eglAddNewMode(scrn, 1600, 1200, 72 * 1000, "1600x1200-72");
-   _eglAddNewMode(scrn, 1280, 1024, 72 * 1000, "1280x1024-70");
-   _eglAddNewMode(scrn, 1280, 1024, 70 * 1000, "1280x1024-70");
-   _eglAddNewMode(scrn, 1024,  768, 72 * 1000, "1024x768-72");
-
-   /* Create the display's visual configs - silly example */
-   for (i = 0; i < 4; i++) {
-      _EGLConfig *config = calloc(1, sizeof(_EGLConfig));
-      _eglInitConfig(config, i + 1);
-      _eglSetConfigAttrib(config, EGL_RED_SIZE, 8);
-      _eglSetConfigAttrib(config, EGL_GREEN_SIZE, 8);
-      _eglSetConfigAttrib(config, EGL_BLUE_SIZE, 8);
-      _eglSetConfigAttrib(config, EGL_ALPHA_SIZE, 8);
-      _eglSetConfigAttrib(config, EGL_BUFFER_SIZE, 32);
-      if (i & 1) {
-         _eglSetConfigAttrib(config, EGL_DEPTH_SIZE, 32);
-      }
-      if (i & 2) {
-         _eglSetConfigAttrib(config, EGL_STENCIL_SIZE, 8);
-      }
-      _eglSetConfigAttrib(config, EGL_SURFACE_TYPE,
-                          (EGL_WINDOW_BIT | EGL_PIXMAP_BIT | EGL_PBUFFER_BIT));
-      _eglAddConfig(disp, config);
-   }
-
-   /* enable supported extensions */
-   disp->Extensions.MESA_screen_surface = EGL_TRUE;
-   disp->Extensions.MESA_copy_context = EGL_TRUE;
-
-   *major = 1;
-   *minor = 0;
-
-   return EGL_TRUE;
-}
-
-
-static EGLBoolean
-demoTerminate(_EGLDriver *drv, _EGLDisplay *dpy)
-{
-   /*DemoDriver *demo = DEMO_DRIVER(dpy);*/
-   return EGL_TRUE;
-}
-
-
-static DemoContext *
-LookupDemoContext(_EGLContext *c)
-{
-   return (DemoContext *) c;
-}
-
-
-static DemoSurface *
-LookupDemoSurface(_EGLSurface *s)
-{
-   return (DemoSurface *) s;
-}
-
-
-static _EGLContext *
-demoCreateContext(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf, _EGLContext *share_list, const EGLint *attrib_list)
-{
-   DemoContext *c;
-   int i;
-
-   for (i = 0; attrib_list && attrib_list[i] != EGL_NONE; i++) {
-      switch (attrib_list[i]) {
-         /* no attribs defined for now */
-      default:
-         _eglError(EGL_BAD_ATTRIBUTE, "eglCreateContext");
-         return NULL;
-      }
-   }
-
-   c = (DemoContext *) calloc(1, sizeof(DemoContext));
-   if (!c)
-      return NULL;
-
-   _eglInitContext(drv, &c->Base, conf, attrib_list);
-   c->DemoStuff = 1;
-   printf("demoCreateContext\n");
-
-   return &c->Base;
-}
-
-
-static _EGLSurface *
-demoCreateWindowSurface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf, NativeWindowType window, const EGLint *attrib_list)
-{
-   int i;
-   for (i = 0; attrib_list && attrib_list[i] != EGL_NONE; i++) {
-      switch (attrib_list[i]) {
-         /* no attribs at this time */
-      default:
-         _eglError(EGL_BAD_ATTRIBUTE, "eglCreateWindowSurface");
-         return NULL;
-      }
-   }
-   printf("eglCreateWindowSurface()\n");
-   /* XXX unfinished */
-
-   return NULL;
-}
-
-
-static _EGLSurface *
-demoCreatePixmapSurface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf, NativePixmapType pixmap, const EGLint *attrib_list)
-{
-   EGLint i;
-
-   for (i = 0; attrib_list && attrib_list[i] != EGL_NONE; i++) {
-      switch (attrib_list[i]) {
-         /* no attribs at this time */
-      default:
-         _eglError(EGL_BAD_ATTRIBUTE, "eglCreatePixmapSurface");
-         return NULL;
-      }
-   }
-
-   if (GET_CONFIG_ATTRIB(conf, EGL_SURFACE_TYPE) == 0) {
-      _eglError(EGL_BAD_MATCH, "eglCreatePixmapSurface");
-      return NULL;
-   }
-
-   printf("eglCreatePixmapSurface()\n");
-   return NULL;
-}
-
-
-static _EGLSurface *
-demoCreatePbufferSurface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf,
-                         const EGLint *attrib_list)
-{
-   DemoSurface *surf = (DemoSurface *) calloc(1, sizeof(DemoSurface));
-
-   if (!surf)
-      return NULL;
-
-   if (!_eglInitSurface(drv, &surf->Base, EGL_PBUFFER_BIT,
-                        conf, attrib_list)) {
-      free(surf);
-      return NULL;
-   }
-
-   /* a real driver would allocate the pbuffer memory here */
-
-   return &surf->Base;
-}
-
-
-static EGLBoolean
-demoDestroySurface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surface)
-{
-   DemoSurface *fs = LookupDemoSurface(surface);
-   if (!_eglIsSurfaceBound(&fs->Base))
-      free(fs);
-   return EGL_TRUE;
-}
-
-
-static EGLBoolean
-demoDestroyContext(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *context)
-{
-   DemoContext *fc = LookupDemoContext(context);
-   if (!_eglIsContextBound(&fc->Base))
-      free(fc);
-   return EGL_TRUE;
-}
-
-
-static EGLBoolean
-demoMakeCurrent(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *drawSurf, _EGLSurface *readSurf, _EGLContext *ctx)
-{
-   /*DemoDriver *demo = DEMO_DRIVER(dpy);*/
-   EGLBoolean b;
-
-   b = _eglMakeCurrent(drv, dpy, drawSurf, readSurf, ctx);
-   if (!b)
-      return EGL_FALSE;
-
-   /* XXX this is where we'd do the hardware context switch */
-   (void) drawSurf;
-   (void) readSurf;
-   (void) ctx;
-
-   printf("eglMakeCurrent()\n");
-   return EGL_TRUE;
-}
-
-
-static void
-demoUnload(_EGLDriver *drv)
-{
-   free(drv);
-}
-
-
-/**
- * The bootstrap function.  Return a new DemoDriver object and
- * plug in API functions.
- */
-_EGLDriver *
-_eglMain(const char *args)
-{
-   DemoDriver *demo;
-
-   demo = (DemoDriver *) calloc(1, sizeof(DemoDriver));
-   if (!demo) {
-      return NULL;
-   }
-
-   /* First fill in the dispatch table with defaults */
-   _eglInitDriverFallbacks(&demo->Base);
-   /* then plug in our Demo-specific functions */
-   demo->Base.API.Initialize = demoInitialize;
-   demo->Base.API.Terminate = demoTerminate;
-   demo->Base.API.CreateContext = demoCreateContext;
-   demo->Base.API.MakeCurrent = demoMakeCurrent;
-   demo->Base.API.CreateWindowSurface = demoCreateWindowSurface;
-   demo->Base.API.CreatePixmapSurface = demoCreatePixmapSurface;
-   demo->Base.API.CreatePbufferSurface = demoCreatePbufferSurface;
-   demo->Base.API.DestroySurface = demoDestroySurface;
-   demo->Base.API.DestroyContext = demoDestroyContext;
-
-   demo->Base.Name = "egl/demo";
-   demo->Base.Unload = demoUnload;
-
-   return &demo->Base;
-}
diff --git a/src/egl/drivers/glx/Makefile b/src/egl/drivers/glx/Makefile
index 20ef035..634638f 100644
--- a/src/egl/drivers/glx/Makefile
+++ b/src/egl/drivers/glx/Makefile
@@ -1,77 +1,16 @@
 # src/egl/drivers/glx/Makefile
 
-# Build XEGL DRI driver loader library:  egl_glx.so
-
-
 TOP = ../../../..
 include $(TOP)/configs/current
 
+EGL_DRIVER = egl_glx.so
+EGL_SOURCES = egl_glx.c
 
-EXTRA_DEFINES = -DDEFAULT_DRIVER_DIR=\"$(DRI_DRIVER_SEARCH_DIR)\"
-
-DRIVER_NAME = egl_glx.so
-
-
-INCLUDE_DIRS = \
-	-I. \
-	-I/usr/include \
-	$(shell pkg-config --cflags-only-I libdrm) \
+EGL_INCLUDES = \
 	-I$(TOP)/include \
-	-I$(TOP)/include/GL/internal \
-	-I$(TOP)/src/mesa/glapi \
-	-I$(TOP)/src/mesa/drivers/dri/common \
-	-I$(TOP)/src/mesa/main \
-	-I$(TOP)/src/mesa \
-	-I$(TOP)/src/egl/main \
-	-I$(TOP)/src/glx/x11
+	-I$(TOP)/src/egl/main
 
-SOURCES = egl_glx.c
+EGL_CFLAGS =
+EGL_LIBS = -lX11 -lGL
 
-OBJECTS = $(SOURCES:.c=.o)
-
-DRM_LIB = `pkg-config --libs libdrm`
-
-MISC_LIBS = -ldl -lXext -lGL
-
-
-.c.o:
-	$(CC) -c $(INCLUDE_DIRS) $(CFLAGS) $(EXTRA_DEFINES) $< -o $@
-
-
-.PHONY: library
-
-
-default: depend library Makefile
-
-
-library: $(TOP)/$(LIB_DIR)/$(DRIVER_NAME)
-
-
-# Make the egl_glx.so library
-$(TOP)/$(LIB_DIR)/$(DRIVER_NAME): $(OBJECTS)
-	$(TOP)/bin/mklib -o $(DRIVER_NAME) \
-		-noprefix \
-		-major 1 -minor 0 \
-		-L$(TOP)/$(LIB_DIR) \
-		-install $(TOP)/$(LIB_DIR) \
-		$(OBJECTS) $(DRM_LIB) $(MISC_LIBS)
-
-install:
-	$(INSTALL) -d $(DESTDIR)$(INSTALL_LIB_DIR)
-	$(MINSTALL) $(TOP)/$(LIB_DIR)/$(DRIVER_NAME) $(DESTDIR)$(INSTALL_LIB_DIR)
-
-clean:
-	rm -f *.o
-	rm -f *.so
-	rm -f depend depend.bak
-
-
-depend: $(SOURCES) $(HEADERS)
-	@ echo "running $(MKDEP)"
-	@ rm -f depend
-	@ touch depend
-	$(MKDEP) $(MKDEP_OPTIONS) $(DEFINES) $(INCLUDE_DIRS) \
-		$(SOURCES) $(HEADERS) >/dev/null 2>/dev/null
-
-include depend
-# DO NOT DELETE
+include ../Makefile.template
diff --git a/src/egl/drivers/glx/egl_glx.c b/src/egl/drivers/glx/egl_glx.c
index 7c6e863..6d28158 100644
--- a/src/egl/drivers/glx/egl_glx.c
+++ b/src/egl/drivers/glx/egl_glx.c
@@ -54,13 +54,6 @@
 #error "GL/glx.h must be equal to or greater than GLX 1.4"
 #endif
 
-/*
- * report OpenGL ES bits because apps usually forget to specify
- * EGL_RENDERABLE_TYPE when choosing configs
- */
-#define GLX_EGL_APIS (EGL_OPENGL_BIT | EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT)
-
-
 /** subclass of _EGLDriver */
 struct GLX_egl_driver
 {
@@ -244,7 +237,7 @@
 
    GLX_conf->double_buffered = (mode.doubleBufferMode != 0);
    return _eglConfigFromContextModesRec(&GLX_conf->Base, &mode,
-                                        GLX_EGL_APIS, GLX_EGL_APIS);
+                                        EGL_OPENGL_BIT, EGL_OPENGL_BIT);
 }
 
 
@@ -364,7 +357,7 @@
 
    GLX_conf->double_buffered = (mode.doubleBufferMode != 0);
    return _eglConfigFromContextModesRec(&GLX_conf->Base, &mode,
-                                        GLX_EGL_APIS, GLX_EGL_APIS);
+                                        EGL_OPENGL_BIT, EGL_OPENGL_BIT);
 }
 
 
@@ -559,7 +552,7 @@
    }
 
    disp->DriverData = (void *) GLX_dpy;
-   disp->ClientAPIsMask = GLX_EGL_APIS;
+   disp->ClientAPIsMask = EGL_OPENGL_BIT;
 
    /* we're supporting EGL 1.4 */
    *major = 1;
diff --git a/src/egl/drivers/xdri/Makefile b/src/egl/drivers/xdri/Makefile
index 4c1fc90..9120620 100644
--- a/src/egl/drivers/xdri/Makefile
+++ b/src/egl/drivers/xdri/Makefile
@@ -1,78 +1,28 @@
 # src/egl/drivers/xdri/Makefile
 
-# Build XEGL DRI driver loader library:  egl_xdri.so
-
-
 TOP = ../../../..
 include $(TOP)/configs/current
 
+EGL_DRIVER = egl_xdri.so
 
-DRIVER_NAME = egl_xdri.so
-
-
-INCLUDE_DIRS = \
-	-I. \
-	-I/usr/include \
+# steal sources from GLX
+GLX_SOURCES = dri_common.c XF86dri.c dri2.c dri2_glx.c dri_glx.c drisw_glx.c
+GLX_SOURCES := $(addprefix ../../../glx/x11/,$(GLX_SOURCES))
+GLX_INCLUDES = \
 	$(shell pkg-config --cflags-only-I libdrm) \
-	-I$(TOP)/include \
 	-I$(TOP)/include/GL/internal \
-	-I$(TOP)/src/mesa \
+	-I$(TOP)/src/glx/x11 \
 	-I$(TOP)/src/mesa/glapi \
+	-I$(TOP)/src/mesa
+GLX_CFLAGS = -DGLX_DIRECT_RENDERING
+
+EGL_SOURCES = egl_xdri.c glxinit.c driinit.c $(GLX_SOURCES)
+EGL_INCLUDES = \
+	-I$(TOP)/include \
 	-I$(TOP)/src/egl/main \
-	-I$(TOP)/src/glx/x11
+	$(GLX_INCLUDES)
 
-HEADERS = glxinit.h driinit.h
-SOURCES = egl_xdri.c glxinit.c driinit.c
+EGL_CFLAGS = $(GLX_CFLAGS)
+EGL_LIBS = -lX11 -lGL
 
-DRI_SOURCES = dri_common.c XF86dri.c dri2.c dri2_glx.c dri_glx.c
-DRI_SOURCES := $(addprefix ../../../glx/x11/,$(DRI_SOURCES))
-
-SOURCES += $(DRI_SOURCES)
-
-OBJECTS = $(SOURCES:.c=.o)
-
-DRM_LIB = `pkg-config --libs libdrm`
-
-CFLAGS += -DGLX_DIRECT_RENDERING
-
-.c.o:
-	$(CC) -c $(INCLUDE_DIRS) $(CFLAGS) $< -o $@
-
-
-.PHONY: library
-
-
-default: depend library Makefile
-
-
-library: $(TOP)/$(LIB_DIR)/$(DRIVER_NAME)
-
-
-# Make the egl_xdri.so library
-$(TOP)/$(LIB_DIR)/$(DRIVER_NAME): $(OBJECTS)
-	$(TOP)/bin/mklib -o $(DRIVER_NAME) \
-		-noprefix \
-		-major 1 -minor 0 \
-		-L$(TOP)/$(LIB_DIR) \
-		-install $(TOP)/$(LIB_DIR) \
-		$(OBJECTS) $(DRM_LIB) $(GL_LIB_DEPS)
-
-install:
-	$(INSTALL) -d $(DESTDIR)$(INSTALL_LIB_DIR)
-	$(MINSTALL) $(TOP)/$(LIB_DIR)/$(DRIVER_NAME) $(DESTDIR)$(INSTALL_LIB_DIR)
-
-clean:
-	rm -f *.o
-	rm -f *.so
-	rm -f depend depend.bak
-
-
-depend: $(SOURCES) $(HEADERS)
-	@ echo "running $(MKDEP)"
-	@ rm -f depend
-	@ touch depend
-	$(MKDEP) $(MKDEP_OPTIONS) $(DEFINES) $(INCLUDE_DIRS) \
-		$(SOURCES) $(HEADERS) >/dev/null 2>/dev/null
-
-include depend
-# DO NOT DELETE
+include ../Makefile.template
diff --git a/src/egl/drivers/xdri/driinit.c b/src/egl/drivers/xdri/driinit.c
index 12da1bc..3e54f0b 100644
--- a/src/egl/drivers/xdri/driinit.c
+++ b/src/egl/drivers/xdri/driinit.c
@@ -2,6 +2,7 @@
  * DRI initialization.  The DRI loaders are defined in src/glx/x11/.
  */
 
+#include <stdlib.h>
 #include <sys/time.h>
 
 #include "glxclient.h"
@@ -42,18 +43,26 @@
 _X_HIDDEN __GLXDRIdisplay *
 __driCreateDisplay(__GLXdisplayPrivate *dpyPriv, int *version)
 {
-   __GLXDRIdisplay *driDisplay;
+   __GLXDRIdisplay *driDisplay = NULL;
    int ver = 0;
+   char *env;
+   int force_sw;
+
+   env = getenv("EGL_SOFTWARE");
+   force_sw = (env && *env != '0');
 
    /* try DRI2 first */
-   driDisplay = dri2CreateDisplay(dpyPriv->dpy);
-   if (driDisplay) {
-      /* fill in the required field */
-      dpyPriv->dri2Display = driDisplay;
-      ver = 2;
+   if (!force_sw) {
+      driDisplay = dri2CreateDisplay(dpyPriv->dpy);
+      if (driDisplay) {
+         /* fill in the required field */
+         dpyPriv->dri2Display = driDisplay;
+         ver = 2;
+      }
    }
-   else {
-      /* try DRI */
+
+   /* and then DRI */
+   if (!force_sw && !driDisplay) {
       driDisplay = driCreateDisplay(dpyPriv->dpy);
       if (driDisplay) {
          dpyPriv->driDisplay = driDisplay;
@@ -61,6 +70,15 @@
       }
    }
 
+   /* and then DRISW */
+   if (!driDisplay) {
+      driDisplay = driswCreateDisplay(dpyPriv->dpy);
+      if (driDisplay) {
+         dpyPriv->driDisplay = driDisplay;
+         ver = 0;
+      }
+   }
+
    if (version)
       *version = ver;
    return driDisplay;
diff --git a/src/egl/drivers/xdri/egl_xdri.c b/src/egl/drivers/xdri/egl_xdri.c
index 8425b3d..b133939 100644
--- a/src/egl/drivers/xdri/egl_xdri.c
+++ b/src/egl/drivers/xdri/egl_xdri.c
@@ -62,6 +62,7 @@
 struct xdri_egl_driver
 {
    _EGLDriver Base;   /**< base class */
+   void (*FlushCurrentContext)(void);
 };
 
 
@@ -71,6 +72,7 @@
    Display *dpy;
    __GLXdisplayPrivate *dpyPriv;
    __GLXDRIdisplay *driDisplay;
+   int driVersion;
 
    __GLXscreenConfigs *psc;
    EGLint scr;
@@ -167,14 +169,10 @@
 static EGLBoolean
 convert_config(_EGLConfig *conf, EGLint id, const __GLcontextModes *m)
 {
-   static const EGLint all_apis = (EGL_OPENGL_ES_BIT |
-                                   EGL_OPENGL_ES2_BIT |
-                                   EGL_OPENVG_BIT |
-                                   EGL_OPENGL_BIT);
    EGLint val;
 
    _eglInitConfig(conf, id);
-   if (!_eglConfigFromContextModesRec(conf, m, all_apis, all_apis))
+   if (!_eglConfigFromContextModesRec(conf, m, EGL_OPENGL_BIT, EGL_OPENGL_BIT))
       return EGL_FALSE;
 
    if (m->doubleBufferMode) {
@@ -215,6 +213,7 @@
 static EGLint
 create_configs(_EGLDisplay *disp, const __GLcontextModes *m, EGLint first_id)
 {
+   struct xdri_egl_display *xdri_dpy = lookup_display(disp);
    int id = first_id;
 
    for (; m; m = m->next) {
@@ -224,8 +223,15 @@
 
       if (!convert_config(&conf, id, m))
          continue;
-
-      rb = (m->doubleBufferMode) ? EGL_BACK_BUFFER : EGL_SINGLE_BUFFER;
+      if (m->doubleBufferMode) {
+         rb = EGL_BACK_BUFFER;
+      }
+      else {
+         /* ignore single-buffered mode for DRISW */
+         if (xdri_dpy->driVersion == 0)
+            continue;
+         rb = EGL_SINGLE_BUFFER;
+      }
 
       xdri_conf = CALLOC_STRUCT(xdri_egl_config);
       if (xdri_conf) {
@@ -275,7 +281,7 @@
       return _eglError(EGL_NOT_INITIALIZED, "eglInitialize");
    }
 
-   driDisplay = __driCreateDisplay(dpyPriv, NULL);
+   driDisplay = __driCreateDisplay(dpyPriv, &xdri_dpy->driVersion);
    if (!driDisplay) {
       _eglLog(_EGL_WARNING, "failed to create DRI display");
       free(xdri_dpy);
@@ -297,16 +303,13 @@
       return _eglError(EGL_NOT_INITIALIZED, "eglInitialize");
    }
 
+   dpy->DriverData = xdri_dpy;
+   dpy->ClientAPIsMask = EGL_OPENGL_BIT;
+
    /* add visuals and fbconfigs */
    first_id = create_configs(dpy, psc->visuals, first_id);
    create_configs(dpy, psc->configs, first_id);
 
-   dpy->DriverData = xdri_dpy;
-   dpy->ClientAPIsMask = (EGL_OPENGL_BIT |
-                          EGL_OPENGL_ES_BIT |
-                          EGL_OPENGL_ES2_BIT |
-                          EGL_OPENVG_BIT);
-
    /* we're supporting EGL 1.4 */
    *minor = 1;
    *major = 4;
@@ -342,7 +345,6 @@
    }
 
    xdri_dpy->driDisplay->destroyDisplay(xdri_dpy->driDisplay);
-   __glXRelease(xdri_dpy->dpyPriv);
 
    free(xdri_dpy);
    dpy->DriverData = NULL;
@@ -441,13 +443,23 @@
 xdri_eglMakeCurrent(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *d,
                     _EGLSurface *r, _EGLContext *context)
 {
+   struct xdri_egl_driver *xdri_driver = xdri_egl_driver(drv);
    struct xdri_egl_context *xdri_ctx = lookup_context(context);
    struct xdri_egl_surface *draw = lookup_surface(d);
    struct xdri_egl_surface *read = lookup_surface(r);
+   _EGLContext *old = _eglGetCurrentContext();
+
+   /* an unlinked context will be invalid after context switch */
+   if (!_eglIsContextLinked(old))
+      old = NULL;
 
    if (!_eglMakeCurrent(drv, dpy, d, r, context))
       return EGL_FALSE;
 
+   /* flush before context switch */
+   if (old && old != context && xdri_driver->FlushCurrentContext)
+      xdri_driver->FlushCurrentContext();
+
    /* the symbol is defined in libGL.so */
    _glapi_check_multithread();
 
@@ -458,12 +470,9 @@
          return EGL_FALSE;
       }
    }
-   else {
-      _EGLContext *old = _eglGetCurrentContext();
-      if (old) {
-         xdri_ctx = lookup_context(old);
-         xdri_ctx->driContext->unbindContext(xdri_ctx->driContext);
-      }
+   else if (old) {
+      xdri_ctx = lookup_context(old);
+      xdri_ctx->driContext->unbindContext(xdri_ctx->driContext);
    }
 
    return EGL_TRUE;
@@ -559,9 +568,15 @@
 static EGLBoolean
 xdri_eglSwapBuffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *draw)
 {
+   struct xdri_egl_driver *xdri_driver = xdri_egl_driver(drv);
    struct xdri_egl_display *xdri_dpy = lookup_display(dpy);
    struct xdri_egl_surface *xdri_surf = lookup_surface(draw);
 
+   /* swapBuffers does not flush commands */
+   if (draw == _eglGetCurrentSurface(EGL_DRAW) &&
+       xdri_driver->FlushCurrentContext)
+      xdri_driver->FlushCurrentContext();
+ 
    xdri_dpy->psc->driScreen->swapBuffers(xdri_surf->driDrawable, 0, 0, 0);
 
    return EGL_TRUE;
@@ -606,5 +621,9 @@
    xdri_drv->Base.Name = "X/DRI";
    xdri_drv->Base.Unload = xdri_Unload;
 
+   /* we need a way to flush commands */
+   xdri_drv->FlushCurrentContext =
+      (void (*)(void)) xdri_eglGetProcAddress(&xdri_drv->Base, "glFlush");
+
    return &xdri_drv->Base;
 }
diff --git a/src/egl/drivers/xdri/glxinit.c b/src/egl/drivers/xdri/glxinit.c
index 7775009..5c0fbc6 100644
--- a/src/egl/drivers/xdri/glxinit.c
+++ b/src/egl/drivers/xdri/glxinit.c
@@ -1,8 +1,10 @@
 /**
  * GLX initialization.  Code based on glxext.c, glx_query.c, and
- * glcontextmodes.c under src/glx/x11/.  The major difference is that no DRI
- * related code here.
+ * glcontextmodes.c under src/glx/x11/.  The major difference is that DRI
+ * related code is stripped out.
  *
+ * If the maintenance of this file takes too much time, we should consider
+ * refactoring glxext.c.
  */
 
 #include <assert.h>
@@ -31,7 +33,26 @@
 static char *__glXExtensionName = GLX_EXTENSION_NAME;
 static XExtensionInfo *__glXExtensionInfo = NULL;
 
-static /* const */ XExtensionHooks __glXExtensionHooks = { NULL };
+static int
+__glXCloseDisplay(Display * dpy, XExtCodes * codes)
+{
+   return XextRemoveDisplay(__glXExtensionInfo, dpy);
+}
+
+static /* const */ XExtensionHooks __glXExtensionHooks = {
+  NULL,                   /* create_gc */
+  NULL,                   /* copy_gc */
+  NULL,                   /* flush_gc */
+  NULL,                   /* free_gc */
+  NULL,                   /* create_font */
+  NULL,                   /* free_font */
+  __glXCloseDisplay,      /* close_display */
+  NULL,                   /* wire_to_event */
+  NULL,                   /* event_to_wire */
+  NULL,                   /* error */
+  NULL,                   /* error_string */
+};
+
 static
 XEXT_GENERATE_FIND_DISPLAY(__glXFindDisplay, __glXExtensionInfo,
                            __glXExtensionName, &__glXExtensionHooks,
@@ -180,6 +201,30 @@
    priv->screenConfigs = NULL;
 }
 
+/*
+** Release the private memory referred to in a display private
+** structure.  The caller will free the extension structure.
+*/
+static int
+__glXFreeDisplayPrivate(XExtData * extension)
+{
+   __GLXdisplayPrivate *priv;
+
+   priv = (__GLXdisplayPrivate *) extension->private_data;
+   FreeScreenConfigs(priv);
+   if (priv->serverGLXvendor) {
+      Xfree((char *) priv->serverGLXvendor);
+      priv->serverGLXvendor = 0x0;      /* to protect against double free's */
+   }
+   if (priv->serverGLXversion) {
+      Xfree((char *) priv->serverGLXversion);
+      priv->serverGLXversion = 0x0;     /* to protect against double free's */
+   }
+
+   Xfree((char *) priv);
+   return 0;
+}
+
 /************************************************************************/
 
 /*
@@ -570,40 +615,40 @@
    return GL_TRUE;
 }
 
-_X_HIDDEN void
-__glXRelease(__GLXdisplayPrivate *dpyPriv)
-{
-   FreeScreenConfigs(dpyPriv);
-
-   if (dpyPriv->serverGLXvendor) {
-      Xfree((char *) dpyPriv->serverGLXvendor);
-      dpyPriv->serverGLXvendor = NULL;
-   }
-   if (dpyPriv->serverGLXversion) {
-      Xfree((char *) dpyPriv->serverGLXversion);
-      dpyPriv->serverGLXversion = NULL;
-   }
-
-   Xfree(dpyPriv);
-}
-
 _X_HIDDEN __GLXdisplayPrivate *
 __glXInitialize(Display * dpy)
 {
    XExtDisplayInfo *info = __glXFindDisplay(dpy);
+   XExtData **privList, *private, *found;
    __GLXdisplayPrivate *dpyPriv;
+   XEDataObject dataObj;
    int major, minor;
 
    if (!XextHasExtension(info))
       return NULL;
 
+   /* See if a display private already exists.  If so, return it */
+   dataObj.display = dpy;
+   privList = XEHeadOfExtensionList(dataObj);
+   found = XFindOnExtensionList(privList, info->codes->extension);
+   if (found)
+      return (__GLXdisplayPrivate *) found->private_data;
+
    /* See if the versions are compatible */
    if (!QueryVersion(dpy, info->codes->major_opcode, &major, &minor))
       return NULL;
 
-   dpyPriv = (__GLXdisplayPrivate *) Xcalloc(1, sizeof(__GLXdisplayPrivate));
-   if (!dpyPriv)
+   /*
+    ** Allocate memory for all the pieces needed for this buffer.
+    */
+   private = (XExtData *) Xmalloc(sizeof(XExtData));
+   if (!private)
       return NULL;
+   dpyPriv = (__GLXdisplayPrivate *) Xcalloc(1, sizeof(__GLXdisplayPrivate));
+   if (!dpyPriv) {
+      Xfree(private);
+      return NULL;
+   }
 
    /*
     ** Init the display private and then read in the screen config
@@ -619,8 +664,20 @@
 
    if (!AllocAndFetchScreenConfigs(dpy, dpyPriv)) {
       Xfree(dpyPriv);
+      Xfree(private);
       return NULL;
    }
 
+   /*
+    ** Fill in the private structure.  This is the actual structure that
+    ** hangs off of the Display structure.  Our private structure is
+    ** referred to by this structure.  Got that?
+    */
+   private->number = info->codes->extension;
+   private->next = 0;
+   private->free_private = __glXFreeDisplayPrivate;
+   private->private_data = (char *) dpyPriv;
+   XAddToExtensionList(privList, private);
+
    return dpyPriv;
 }
diff --git a/src/egl/drivers/xdri/glxinit.h b/src/egl/drivers/xdri/glxinit.h
index 57206e6..1cc7c46 100644
--- a/src/egl/drivers/xdri/glxinit.h
+++ b/src/egl/drivers/xdri/glxinit.h
@@ -8,7 +8,4 @@
 extern void
 _gl_context_modes_destroy(__GLcontextModes * modes);
 
-extern void
-__glXRelease(__GLXdisplayPrivate *dpyPriv);
-
 #endif /* GLXINIT_INCLUDED */
diff --git a/src/egl/main/egldriver.c b/src/egl/main/egldriver.c
index 0574f83..f890df1 100644
--- a/src/egl/main/egldriver.c
+++ b/src/egl/main/egldriver.c
@@ -61,7 +61,7 @@
 #elif defined(_EGL_PLATFORM_POSIX)
 
 
-static const char DefaultDriverName[] = "egl_softpipe";
+static const char DefaultDriverName[] = "egl_glx";
 
 typedef void * lib_handle;
 
diff --git a/src/gallium/auxiliary/Makefile b/src/gallium/auxiliary/Makefile
index 8f937e3..da1fb6b 100644
--- a/src/gallium/auxiliary/Makefile
+++ b/src/gallium/auxiliary/Makefile
@@ -48,12 +48,10 @@
 	draw/draw_vs_sse.c \
 	indices/u_indices_gen.c \
 	indices/u_unfilled_gen.c \
-	pipebuffer/pb_buffer_fenced.c \
 	pipebuffer/pb_buffer_malloc.c \
 	pipebuffer/pb_bufmgr_alt.c \
 	pipebuffer/pb_bufmgr_cache.c \
 	pipebuffer/pb_bufmgr_debug.c \
-	pipebuffer/pb_bufmgr_fenced.c \
 	pipebuffer/pb_bufmgr_mm.c \
 	pipebuffer/pb_bufmgr_ondemand.c \
 	pipebuffer/pb_bufmgr_pool.c \
@@ -92,6 +90,7 @@
 	util/u_debug_dump.c \
 	util/u_debug_symbol.c \
 	util/u_debug_stack.c \
+	util/u_bitmask.c \
 	util/u_blit.c \
 	util/u_blitter.c \
 	util/u_cache.c \
diff --git a/src/gallium/auxiliary/SConscript b/src/gallium/auxiliary/SConscript
index f957090..3aa782f 100644
--- a/src/gallium/auxiliary/SConscript
+++ b/src/gallium/auxiliary/SConscript
@@ -87,7 +87,6 @@
     'pipebuffer/pb_bufmgr_alt.c',
     'pipebuffer/pb_bufmgr_cache.c',
     'pipebuffer/pb_bufmgr_debug.c',
-    'pipebuffer/pb_bufmgr_fenced.c',
     'pipebuffer/pb_bufmgr_mm.c',
     'pipebuffer/pb_bufmgr_ondemand.c',
     'pipebuffer/pb_bufmgr_pool.c',
diff --git a/src/gallium/auxiliary/draw/draw_context.c b/src/gallium/auxiliary/draw/draw_context.c
index e90dfc5..fb1bc05 100644
--- a/src/gallium/auxiliary/draw/draw_context.c
+++ b/src/gallium/auxiliary/draw/draw_context.c
@@ -34,7 +34,6 @@
 #include "util/u_memory.h"
 #include "util/u_math.h"
 #include "draw_context.h"
-#include "draw_vbuf.h"
 #include "draw_vs.h"
 #include "draw_gs.h"
 #include "draw_pt.h"
diff --git a/src/gallium/auxiliary/draw/draw_pipe.c b/src/gallium/auxiliary/draw/draw_pipe.c
index 1c6d657..11d6485 100644
--- a/src/gallium/auxiliary/draw/draw_pipe.c
+++ b/src/gallium/auxiliary/draw/draw_pipe.c
@@ -106,10 +106,9 @@
 
 
 
-
-
-
-
+/**
+ * Build primitive to render a point with vertex at v0.
+ */
 static void do_point( struct draw_context *draw,
 		      const char *v0 )
 {
@@ -123,6 +122,10 @@
 }
 
 
+/**
+ * Build primitive to render a line with vertices at v0, v1.
+ * \param flags  bitmask of DRAW_PIPE_EDGE_x, DRAW_PIPE_RESET_STIPPLE
+ */
 static void do_line( struct draw_context *draw,
                      ushort flags,
 		     const char *v0,
@@ -139,6 +142,10 @@
 }
 
 
+/**
+ * Build primitive to render a triangle with vertices at v0, v1, v2.
+ * \param flags  bitmask of DRAW_PIPE_EDGE_x, DRAW_PIPE_RESET_STIPPLE
+ */
 static void do_triangle( struct draw_context *draw,
                          ushort flags,
 			 char *v0,
@@ -157,7 +164,10 @@
 }
 
 
-
+/*
+ * Set up macros for draw_pt_decompose.h template code.
+ * This code uses vertex indexes / elements.
+ */
 #define QUAD(i0,i1,i2,i3)                       \
    do_triangle( draw,                           \
                 ( DRAW_PIPE_RESET_STIPPLE |     \
@@ -175,16 +185,16 @@
 
 #define TRIANGLE(flags,i0,i1,i2)                                        \
    do_triangle( draw,                                                   \
-                elts[i0],  /* flags */                          \
+                elts[i0],  /* flags */                                  \
                 verts + stride * (elts[i0] & ~DRAW_PIPE_FLAG_MASK),     \
-                verts + stride * elts[i1],                              \
-                verts + stride * elts[i2])
+                verts + stride * (elts[i1] & ~DRAW_PIPE_FLAG_MASK),     \
+                verts + stride * (elts[i2] & ~DRAW_PIPE_FLAG_MASK) );
 
 #define LINE(flags,i0,i1)                                       \
    do_line( draw,                                               \
-            elts[i0],                                   \
+            elts[i0],                                           \
             verts + stride * (elts[i0] & ~DRAW_PIPE_FLAG_MASK), \
-            verts + stride * elts[i1])
+            verts + stride * (elts[i1] & ~DRAW_PIPE_FLAG_MASK) );
 
 #define POINT(i0)                               \
    do_point( draw,                              \
@@ -213,7 +223,9 @@
 
 
 
-/* Code to run the pipeline on a fairly arbitary collection of vertices.
+/**
+ * Code to run the pipeline on a fairly arbitary collection of vertices.
+ * For drawing indexed primitives.
  *
  * Vertex headers must be pre-initialized with the
  * UNDEFINED_VERTEX_ID, this code will cause that id to become
@@ -243,6 +255,12 @@
    draw->pipeline.vertex_count = 0;
 }
 
+
+
+/*
+ * Set up macros for draw_pt_decompose.h template code.
+ * This code is for non-indexed rendering (no elts).
+ */
 #define QUAD(i0,i1,i2,i3)                                        \
    do_triangle( draw,                                            \
                 ( DRAW_PIPE_RESET_STIPPLE |                      \
@@ -293,6 +311,10 @@
 
 #include "draw_pt_decompose.h"
 
+
+/*
+ * For drawing non-indexed primitives.
+ */
 void draw_pipeline_run_linear( struct draw_context *draw,
                                unsigned prim,
                                struct vertex_header *vertices,
diff --git a/src/gallium/auxiliary/draw/draw_pipe_cull.c b/src/gallium/auxiliary/draw/draw_pipe_cull.c
index 11b39db..dc66c65 100644
--- a/src/gallium/auxiliary/draw/draw_pipe_cull.c
+++ b/src/gallium/auxiliary/draw/draw_pipe_cull.c
@@ -50,8 +50,6 @@
 }
 
 
-
-
 static void cull_tri( struct draw_stage *stage,
 		      struct prim_header *header )
 {
@@ -62,7 +60,7 @@
    const float *v1 = header->v[1]->data[pos];
    const float *v2 = header->v[2]->data[pos];
 
-   /* edge vectors e = v0 - v2, f = v1 - v2 */
+   /* edge vectors: e = v0 - v2, f = v1 - v2 */
    const float ex = v0[0] - v2[0];
    const float ey = v0[1] - v2[1];
    const float fx = v1[0] - v2[0];
@@ -72,7 +70,7 @@
    header->det = ex * fy - ey * fx;
 
    if (header->det != 0) {
-      /* if (det < 0 then Z points toward camera and triangle is 
+      /* if det < 0 then Z points toward the camera and the triangle is 
        * counter-clockwise winding.
        */
       unsigned winding = (header->det < 0) ? PIPE_WINDING_CCW : PIPE_WINDING_CW;
@@ -84,6 +82,7 @@
    }
 }
 
+
 static void cull_first_tri( struct draw_stage *stage, 
 			    struct prim_header *header )
 {
@@ -96,13 +95,13 @@
 }
 
 
-
 static void cull_flush( struct draw_stage *stage, unsigned flags )
 {
    stage->tri = cull_first_tri;
    stage->next->flush( stage->next, flags );
 }
 
+
 static void cull_reset_stipple_counter( struct draw_stage *stage )
 {
    stage->next->reset_stipple_counter( stage->next );
@@ -140,7 +139,7 @@
 
    return &cull->stage;
 
- fail:
+fail:
    if (cull)
       cull->stage.destroy( &cull->stage );
 
diff --git a/src/gallium/auxiliary/draw/draw_pipe_validate.c b/src/gallium/auxiliary/draw/draw_pipe_validate.c
index bea90e5..a69e263 100644
--- a/src/gallium/auxiliary/draw/draw_pipe_validate.c
+++ b/src/gallium/auxiliary/draw/draw_pipe_validate.c
@@ -151,8 +151,8 @@
 {
    struct draw_context *draw = stage->draw;
    struct draw_stage *next = draw->pipeline.rasterize;
-   int need_det = 0;
-   int precalc_flat = 0;
+   boolean need_det = FALSE;
+   boolean precalc_flat = FALSE;
    boolean wide_lines, wide_points;
 
    /* Set the validate's next stage to the rasterize stage, so that it
@@ -194,7 +194,7 @@
    if (wide_lines) {
       draw->pipeline.wide_line->next = next;
       next = draw->pipeline.wide_line;
-      precalc_flat = 1;
+      precalc_flat = TRUE;
    }
 
    if (wide_points || draw->rasterizer->point_sprite) {
@@ -205,7 +205,7 @@
    if (draw->rasterizer->line_stipple_enable && draw->pipeline.line_stipple) {
       draw->pipeline.stipple->next = next;
       next = draw->pipeline.stipple;
-      precalc_flat = 1;		/* only needed for lines really */
+      precalc_flat = TRUE;		/* only needed for lines really */
    }
 
    if (draw->rasterizer->poly_stipple_enable
@@ -218,8 +218,8 @@
        draw->rasterizer->fill_ccw != PIPE_POLYGON_MODE_FILL) {
       draw->pipeline.unfilled->next = next;
       next = draw->pipeline.unfilled;
-      precalc_flat = 1;		/* only needed for triangles really */
-      need_det = 1;
+      precalc_flat = TRUE;		/* only needed for triangles really */
+      need_det = TRUE;
    }
 
    if (draw->rasterizer->flatshade && precalc_flat) {
@@ -231,13 +231,13 @@
        draw->rasterizer->offset_ccw) {
       draw->pipeline.offset->next = next;
       next = draw->pipeline.offset;
-      need_det = 1;
+      need_det = TRUE;
    }
 
    if (draw->rasterizer->light_twoside) {
       draw->pipeline.twoside->next = next;
       next = draw->pipeline.twoside;
-      need_det = 1;
+      need_det = TRUE;
    }
 
    /* Always run the cull stage as we calculate determinant there
diff --git a/src/gallium/auxiliary/draw/draw_pt.c b/src/gallium/auxiliary/draw/draw_pt.c
index a5ddec5..f5ed32d 100644
--- a/src/gallium/auxiliary/draw/draw_pt.c
+++ b/src/gallium/auxiliary/draw/draw_pt.c
@@ -33,7 +33,6 @@
 #include "draw/draw_context.h"
 #include "draw/draw_private.h"
 #include "draw/draw_pt.h"
-#include "draw/draw_vs.h"
 #include "tgsi/tgsi_dump.h"
 #include "util/u_math.h"
 #include "util/u_prim.h"
diff --git a/src/gallium/auxiliary/draw/draw_pt_fetch.c b/src/gallium/auxiliary/draw/draw_pt_fetch.c
index 55e7a7b..252be50 100644
--- a/src/gallium/auxiliary/draw/draw_pt_fetch.c
+++ b/src/gallium/auxiliary/draw/draw_pt_fetch.c
@@ -30,7 +30,6 @@
 #include "draw/draw_context.h"
 #include "draw/draw_private.h"
 #include "draw/draw_vbuf.h"
-#include "draw/draw_vertex.h"
 #include "draw/draw_pt.h"
 #include "translate/translate.h"
 #include "translate/translate_cache.h"
diff --git a/src/gallium/auxiliary/draw/draw_pt_fetch_shade_emit.c b/src/gallium/auxiliary/draw/draw_pt_fetch_shade_emit.c
index 734c05f..c5dfbcf 100644
--- a/src/gallium/auxiliary/draw/draw_pt_fetch_shade_emit.c
+++ b/src/gallium/auxiliary/draw/draw_pt_fetch_shade_emit.c
@@ -40,7 +40,6 @@
 #include "draw/draw_pt.h"
 #include "draw/draw_vs.h"
 
-#include "translate/translate.h"
 
 struct fetch_shade_emit;
 
diff --git a/src/gallium/auxiliary/draw/draw_pt_post_vs.c b/src/gallium/auxiliary/draw/draw_pt_post_vs.c
index 5515182..9728d5c 100644
--- a/src/gallium/auxiliary/draw/draw_pt_post_vs.c
+++ b/src/gallium/auxiliary/draw/draw_pt_post_vs.c
@@ -30,7 +30,6 @@
 #include "draw/draw_context.h"
 #include "draw/draw_private.h"
 #include "draw/draw_vbuf.h"
-#include "draw/draw_vertex.h"
 #include "draw/draw_pt.h"
 
 struct pt_post_vs {
diff --git a/src/gallium/auxiliary/draw/draw_vs_varient.c b/src/gallium/auxiliary/draw/draw_vs_varient.c
index 9f40030..b87a465 100644
--- a/src/gallium/auxiliary/draw/draw_vs_varient.c
+++ b/src/gallium/auxiliary/draw/draw_vs_varient.c
@@ -38,7 +38,6 @@
 #include "draw/draw_vertex.h"
 #include "draw/draw_vs.h"
 #include "translate/translate.h"
-#include "translate/translate_cache.h"
 
 /* A first pass at incorporating vertex fetch/emit functionality into 
  */
diff --git a/src/gallium/auxiliary/pipebuffer/Makefile b/src/gallium/auxiliary/pipebuffer/Makefile
new file mode 100644
index 0000000..21d25d2
--- /dev/null
+++ b/src/gallium/auxiliary/pipebuffer/Makefile
@@ -0,0 +1,18 @@
+TOP = ../../../..
+include $(TOP)/configs/current
+
+LIBNAME = pipebuffer
+
+C_SOURCES = \
+	pb_buffer_fenced.c \
+	pb_buffer_malloc.c \
+	pb_bufmgr_alt.c \
+	pb_bufmgr_cache.c \
+	pb_bufmgr_debug.c \
+	pb_bufmgr_mm.c \
+	pb_bufmgr_ondemand.c \
+	pb_bufmgr_pool.c \
+	pb_bufmgr_slab.c \
+	pb_validate.c
+
+include ../../Makefile.template
diff --git a/src/gallium/auxiliary/pipebuffer/SConscript b/src/gallium/auxiliary/pipebuffer/SConscript
new file mode 100644
index 0000000..a074a55
--- /dev/null
+++ b/src/gallium/auxiliary/pipebuffer/SConscript
@@ -0,0 +1,18 @@
+Import('*')
+
+pipebuffer = env.ConvenienceLibrary(
+	target = 'pipebuffer',
+	source = [
+		'pb_buffer_fenced.c',
+		'pb_buffer_malloc.c',
+		'pb_bufmgr_alt.c',
+		'pb_bufmgr_cache.c',
+		'pb_bufmgr_debug.c',
+		'pb_bufmgr_mm.c',
+		'pb_bufmgr_ondemand.c',
+		'pb_bufmgr_pool.c',
+		'pb_bufmgr_slab.c',
+		'pb_validate.c',
+	])
+
+auxiliaries.insert(0, pipebuffer)
diff --git a/src/gallium/auxiliary/pipebuffer/pb_bufmgr.h b/src/gallium/auxiliary/pipebuffer/pb_bufmgr.h
index 8c8d713..0666991 100644
--- a/src/gallium/auxiliary/pipebuffer/pb_bufmgr.h
+++ b/src/gallium/auxiliary/pipebuffer/pb_bufmgr.h
@@ -175,7 +175,9 @@
  */
 struct pb_manager *
 fenced_bufmgr_create(struct pb_manager *provider,
-                     struct pb_fence_ops *ops);
+                     struct pb_fence_ops *ops,
+                     pb_size max_buffer_size,
+                     pb_size max_cpu_total_size);
 
 
 struct pb_manager *
diff --git a/src/gallium/auxiliary/pipebuffer/pb_bufmgr_debug.c b/src/gallium/auxiliary/pipebuffer/pb_bufmgr_debug.c
index 6e3214c..8f74180 100644
--- a/src/gallium/auxiliary/pipebuffer/pb_bufmgr_debug.c
+++ b/src/gallium/auxiliary/pipebuffer/pb_bufmgr_debug.c
@@ -371,6 +371,9 @@
    struct pb_desc real_desc;
    pb_size real_size;
    
+   assert(size);
+   assert(desc->alignment);
+
    buf = CALLOC_STRUCT(pb_debug_buffer);
    if(!buf)
       return NULL;
diff --git a/src/gallium/auxiliary/pipebuffer/pb_bufmgr_fenced.c b/src/gallium/auxiliary/pipebuffer/pb_bufmgr_fenced.c
deleted file mode 100644
index 97dd142..0000000
--- a/src/gallium/auxiliary/pipebuffer/pb_bufmgr_fenced.c
+++ /dev/null
@@ -1,152 +0,0 @@
-/**************************************************************************
- * 
- * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA
- * All Rights Reserved.
- * 
- * Permission is hereby granted, free of charge, to any person obtaining a
- * copy of this software and associated documentation files (the
- * "Software"), to deal in the Software without restriction, including
- * without limitation the rights to use, copy, modify, merge, publish,
- * distribute, sub license, and/or sell copies of the Software, and to
- * permit persons to whom the Software is furnished to do so, subject to
- * the following conditions:
- * 
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
- * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
- * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 
- * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 
- * USE OR OTHER DEALINGS IN THE SOFTWARE.
- *
- * The above copyright notice and this permission notice (including the
- * next paragraph) shall be included in all copies or substantial portions
- * of the Software.
- * 
- * 
- **************************************************************************/
-
-/**
- * \file
- * A buffer manager that wraps buffers in fenced buffers.
- * 
- * \author Jose Fonseca <jrfonseca@tungstengraphics.dot.com>
- */
-
-
-#include "util/u_debug.h"
-#include "util/u_memory.h"
-
-#include "pb_buffer.h"
-#include "pb_buffer_fenced.h"
-#include "pb_bufmgr.h"
-
-
-struct fenced_pb_manager
-{
-   struct pb_manager base;
-
-   struct pb_manager *provider;
-   
-   struct fenced_buffer_list *fenced_list;
-};
-
-
-static INLINE struct fenced_pb_manager *
-fenced_pb_manager(struct pb_manager *mgr)
-{
-   assert(mgr);
-   return (struct fenced_pb_manager *)mgr;
-}
-
-
-static struct pb_buffer *
-fenced_bufmgr_create_buffer(struct pb_manager *mgr, 
-                            pb_size size,
-                            const struct pb_desc *desc)
-{
-   struct fenced_pb_manager *fenced_mgr = fenced_pb_manager(mgr);
-   struct pb_buffer *buf;
-   struct pb_buffer *fenced_buf;
-
-   /* check for free buffers before allocating new ones */
-   fenced_buffer_list_check_free(fenced_mgr->fenced_list, 0);
-   
-   buf = fenced_mgr->provider->create_buffer(fenced_mgr->provider, size, desc);
-   if(!buf) {
-      /* try harder to get a buffer */
-      fenced_buffer_list_check_free(fenced_mgr->fenced_list, 1);
-      
-      buf = fenced_mgr->provider->create_buffer(fenced_mgr->provider, size, desc);
-      if(!buf) {
-#if 0
-         fenced_buffer_list_dump(fenced_mgr->fenced_list);
-#endif
-         
-         /* give up */
-         return NULL;
-      }
-   }
-   
-   fenced_buf = fenced_buffer_create(fenced_mgr->fenced_list, buf);
-   if(!fenced_buf) {
-      pb_reference(&buf, NULL);
-   }
-   
-   return fenced_buf;
-}
-
-
-static void
-fenced_bufmgr_flush(struct pb_manager *mgr)
-{
-   struct fenced_pb_manager *fenced_mgr = fenced_pb_manager(mgr);
-
-   fenced_buffer_list_check_free(fenced_mgr->fenced_list, TRUE);
-
-   assert(fenced_mgr->provider->flush);
-   if(fenced_mgr->provider->flush)
-      fenced_mgr->provider->flush(fenced_mgr->provider);
-}
-
-
-static void
-fenced_bufmgr_destroy(struct pb_manager *mgr)
-{
-   struct fenced_pb_manager *fenced_mgr = fenced_pb_manager(mgr);
-
-   fenced_buffer_list_destroy(fenced_mgr->fenced_list);
-
-   if(fenced_mgr->provider)
-      fenced_mgr->provider->destroy(fenced_mgr->provider);
-   
-   FREE(fenced_mgr);
-}
-
-
-struct pb_manager *
-fenced_bufmgr_create(struct pb_manager *provider, 
-                     struct pb_fence_ops *ops) 
-{
-   struct fenced_pb_manager *fenced_mgr;
-
-   if(!provider)
-      return NULL;
-   
-   fenced_mgr = CALLOC_STRUCT(fenced_pb_manager);
-   if (!fenced_mgr)
-      return NULL;
-
-   fenced_mgr->base.destroy = fenced_bufmgr_destroy;
-   fenced_mgr->base.create_buffer = fenced_bufmgr_create_buffer;
-   fenced_mgr->base.flush = fenced_bufmgr_flush;
-
-   fenced_mgr->provider = provider;
-   fenced_mgr->fenced_list = fenced_buffer_list_create(ops);
-   if(!fenced_mgr->fenced_list) {
-      FREE(fenced_mgr);
-      return NULL;
-   }
-      
-   return &fenced_mgr->base;
-}
diff --git a/src/gallium/auxiliary/pipebuffer/pb_validate.c b/src/gallium/auxiliary/pipebuffer/pb_validate.c
index ce40c0c..903afc7 100644
--- a/src/gallium/auxiliary/pipebuffer/pb_validate.c
+++ b/src/gallium/auxiliary/pipebuffer/pb_validate.c
@@ -39,7 +39,6 @@
 #include "util/u_debug.h"
 
 #include "pb_buffer.h"
-#include "pb_buffer_fenced.h"
 #include "pb_validate.h"
 
 
diff --git a/src/gallium/auxiliary/tgsi/tgsi_scan.c b/src/gallium/auxiliary/tgsi/tgsi_scan.c
index a6cc773..b9be8dc 100644
--- a/src/gallium/auxiliary/tgsi/tgsi_scan.c
+++ b/src/gallium/auxiliary/tgsi/tgsi_scan.c
@@ -101,12 +101,10 @@
                       src->Register.File == TGSI_FILE_SYSTEM_VALUE) {
                      const int ind = src->Register.Index;
                      if (info->input_semantic_name[ind] == TGSI_SEMANTIC_FOG) {
-                        if (src->Register.SwizzleX == TGSI_SWIZZLE_X) {
-                           info->uses_fogcoord = TRUE;
-                        }
-                        else if (src->Register.SwizzleX == TGSI_SWIZZLE_Y) {
-                           info->uses_frontfacing = TRUE;
-                        }
+                        info->uses_fogcoord = TRUE;
+                     }
+                     else if (info->input_semantic_name[ind] == TGSI_SEMANTIC_FACE) {
+                        info->uses_frontfacing = TRUE;
                      }
                   }
                }
diff --git a/src/gallium/auxiliary/util/u_blit.c b/src/gallium/auxiliary/util/u_blit.c
index 9725890..236f1e4 100644
--- a/src/gallium/auxiliary/util/u_blit.c
+++ b/src/gallium/auxiliary/util/u_blit.c
@@ -226,8 +226,8 @@
 
    offset = get_next_slot( ctx );
 
-   pipe_buffer_write(ctx->pipe->screen, ctx->vbuf,
-                     offset, sizeof(ctx->vertices), ctx->vertices);
+   pipe_buffer_write_nooverlap(ctx->pipe->screen, ctx->vbuf,
+                               offset, sizeof(ctx->vertices), ctx->vertices);
 
    return offset;
 }
diff --git a/src/gallium/auxiliary/util/u_gen_mipmap.c b/src/gallium/auxiliary/util/u_gen_mipmap.c
index 7602379..5426c91 100644
--- a/src/gallium/auxiliary/util/u_gen_mipmap.c
+++ b/src/gallium/auxiliary/util/u_gen_mipmap.c
@@ -1411,8 +1411,8 @@
 
    offset = get_next_slot( ctx );
 
-   pipe_buffer_write(ctx->pipe->screen, ctx->vbuf,
-                     offset, sizeof(ctx->vertices), ctx->vertices);
+   pipe_buffer_write_nooverlap(ctx->pipe->screen, ctx->vbuf,
+                               offset, sizeof(ctx->vertices), ctx->vertices);
 
    return offset;
 }
diff --git a/src/gallium/auxiliary/util/u_upload_mgr.c b/src/gallium/auxiliary/util/u_upload_mgr.c
index 975ee89..55a6537 100644
--- a/src/gallium/auxiliary/util/u_upload_mgr.c
+++ b/src/gallium/auxiliary/util/u_upload_mgr.c
@@ -85,7 +85,9 @@
 
    map = pipe_buffer_map_range(screen, buf, offset, size, 
                                PIPE_BUFFER_USAGE_CPU_WRITE |
-                               PIPE_BUFFER_USAGE_FLUSH_EXPLICIT);
+                               PIPE_BUFFER_USAGE_FLUSH_EXPLICIT |
+                               PIPE_BUFFER_USAGE_DISCARD |
+                               PIPE_BUFFER_USAGE_UNSYNCHRONIZED);
    if (map == NULL) 
       return PIPE_ERROR_OUT_OF_MEMORY;
 
diff --git a/src/gallium/drivers/i915/i915_clear.c b/src/gallium/drivers/i915/i915_clear.c
index 90530f2..0d0859f 100644
--- a/src/gallium/drivers/i915/i915_clear.c
+++ b/src/gallium/drivers/i915/i915_clear.c
@@ -32,7 +32,6 @@
 
 #include "util/u_clear.h"
 #include "i915_context.h"
-#include "i915_state.h"
 
 
 /**
diff --git a/src/gallium/drivers/i915/i915_context.c b/src/gallium/drivers/i915/i915_context.c
index 89feead..4c5ff37 100644
--- a/src/gallium/drivers/i915/i915_context.c
+++ b/src/gallium/drivers/i915/i915_context.c
@@ -29,12 +29,9 @@
 #include "i915_state.h"
 #include "i915_screen.h"
 #include "i915_batch.h"
-#include "i915_texture.h"
-#include "i915_reg.h"
 
 #include "draw/draw_context.h"
 #include "pipe/p_defines.h"
-#include "pipe/internal/p_winsys_screen.h"
 #include "pipe/p_inlines.h"
 #include "util/u_memory.h"
 #include "pipe/p_screen.h"
diff --git a/src/gallium/drivers/i915/i915_debug.c b/src/gallium/drivers/i915/i915_debug.c
index c6e6d6f..237654d 100644
--- a/src/gallium/drivers/i915/i915_debug.c
+++ b/src/gallium/drivers/i915/i915_debug.c
@@ -29,7 +29,6 @@
 #include "i915_context.h"
 #include "i915_debug.h"
 #include "i915_batch.h"
-#include "pipe/internal/p_winsys_screen.h"
 #include "util/u_debug.h"
 
 
diff --git a/src/gallium/drivers/i915/i915_debug_fp.c b/src/gallium/drivers/i915/i915_debug_fp.c
index 9c5b117..f9c40d8 100644
--- a/src/gallium/drivers/i915/i915_debug_fp.c
+++ b/src/gallium/drivers/i915/i915_debug_fp.c
@@ -29,7 +29,6 @@
 #include "i915_reg.h"
 #include "i915_debug.h"
 #include "pipe/internal/p_winsys_screen.h"
-#include "util/u_memory.h"
 
 
 static void
diff --git a/src/gallium/drivers/i915/i915_state.c b/src/gallium/drivers/i915/i915_state.c
index 0fab6e1..06949c1 100644
--- a/src/gallium/drivers/i915/i915_state.c
+++ b/src/gallium/drivers/i915/i915_state.c
@@ -30,7 +30,6 @@
 
 
 #include "draw/draw_context.h"
-#include "pipe/internal/p_winsys_screen.h"
 #include "pipe/p_inlines.h"
 #include "util/u_math.h"
 #include "util/u_memory.h"
@@ -38,7 +37,6 @@
 
 #include "i915_context.h"
 #include "i915_reg.h"
-#include "i915_state.h"
 #include "i915_state_inlines.h"
 #include "i915_fpc.h"
 
diff --git a/src/gallium/drivers/i915/i915_state_derived.c b/src/gallium/drivers/i915/i915_state_derived.c
index 03dd509..f5b0e9f 100644
--- a/src/gallium/drivers/i915/i915_state_derived.c
+++ b/src/gallium/drivers/i915/i915_state_derived.c
@@ -33,7 +33,6 @@
 #include "i915_context.h"
 #include "i915_state.h"
 #include "i915_reg.h"
-#include "i915_fpc.h"
 
 
 
diff --git a/src/gallium/drivers/i915/i915_state_sampler.c b/src/gallium/drivers/i915/i915_state_sampler.c
index cbac417..e5c6d87 100644
--- a/src/gallium/drivers/i915/i915_state_sampler.c
+++ b/src/gallium/drivers/i915/i915_state_sampler.c
@@ -27,7 +27,6 @@
 
 #include "pipe/p_context.h"
 #include "pipe/p_state.h"
-#include "util/u_memory.h"
 
 #include "i915_state_inlines.h"
 #include "i915_context.h"
diff --git a/src/gallium/drivers/i915/i915_surface.c b/src/gallium/drivers/i915/i915_surface.c
index c693eb3..13684aa 100644
--- a/src/gallium/drivers/i915/i915_surface.c
+++ b/src/gallium/drivers/i915/i915_surface.c
@@ -27,14 +27,10 @@
 
 #include "i915_context.h"
 #include "i915_blit.h"
-#include "i915_state.h"
 #include "pipe/p_defines.h"
 #include "pipe/p_inlines.h"
-#include "pipe/p_inlines.h"
 #include "pipe/internal/p_winsys_screen.h"
 #include "util/u_format.h"
-#include "util/u_tile.h"
-#include "util/u_rect.h"
 
 
 /* Assumes all values are within bounds -- no checking at this level -
diff --git a/src/gallium/drivers/i915/i915_texture.c b/src/gallium/drivers/i915/i915_texture.c
index 50a9e19..441bc4f 100644
--- a/src/gallium/drivers/i915/i915_texture.c
+++ b/src/gallium/drivers/i915/i915_texture.c
@@ -41,7 +41,6 @@
 
 #include "i915_context.h"
 #include "i915_texture.h"
-#include "i915_debug.h"
 #include "i915_screen.h"
 #include "intel_winsys.h"
 
diff --git a/src/gallium/drivers/llvmpipe/lp_bld_alpha.c b/src/gallium/drivers/llvmpipe/lp_bld_alpha.c
index 2b4bc5c..7245730 100644
--- a/src/gallium/drivers/llvmpipe/lp_bld_alpha.c
+++ b/src/gallium/drivers/llvmpipe/lp_bld_alpha.c
@@ -35,7 +35,6 @@
 
 #include "lp_bld_type.h"
 #include "lp_bld_const.h"
-#include "lp_bld_arit.h"
 #include "lp_bld_logic.h"
 #include "lp_bld_flow.h"
 #include "lp_bld_debug.h"
diff --git a/src/gallium/drivers/llvmpipe/lp_bld_arit.c b/src/gallium/drivers/llvmpipe/lp_bld_arit.c
index eea6b5d..1aee9b3 100644
--- a/src/gallium/drivers/llvmpipe/lp_bld_arit.c
+++ b/src/gallium/drivers/llvmpipe/lp_bld_arit.c
@@ -56,7 +56,6 @@
 #include "lp_bld_intr.h"
 #include "lp_bld_logic.h"
 #include "lp_bld_pack.h"
-#include "lp_bld_debug.h"
 #include "lp_bld_arit.h"
 
 
diff --git a/src/gallium/drivers/llvmpipe/lp_bld_blend_soa.c b/src/gallium/drivers/llvmpipe/lp_bld_blend_soa.c
index 9511299..5d5ca7a 100644
--- a/src/gallium/drivers/llvmpipe/lp_bld_blend_soa.c
+++ b/src/gallium/drivers/llvmpipe/lp_bld_blend_soa.c
@@ -71,7 +71,6 @@
 #include "pipe/p_state.h"
 
 #include "lp_bld_type.h"
-#include "lp_bld_const.h"
 #include "lp_bld_arit.h"
 #include "lp_bld_blend.h"
 
diff --git a/src/gallium/drivers/llvmpipe/lp_bld_conv.c b/src/gallium/drivers/llvmpipe/lp_bld_conv.c
index 9935209..ebf554c 100644
--- a/src/gallium/drivers/llvmpipe/lp_bld_conv.c
+++ b/src/gallium/drivers/llvmpipe/lp_bld_conv.c
@@ -63,11 +63,9 @@
 
 #include "util/u_debug.h"
 #include "util/u_math.h"
-#include "util/u_cpu_detect.h"
 
 #include "lp_bld_type.h"
 #include "lp_bld_const.h"
-#include "lp_bld_intr.h"
 #include "lp_bld_arit.h"
 #include "lp_bld_pack.h"
 #include "lp_bld_conv.h"
diff --git a/src/gallium/drivers/llvmpipe/lp_bld_format_aos.c b/src/gallium/drivers/llvmpipe/lp_bld_format_aos.c
index 10e82f1..dfa080b 100644
--- a/src/gallium/drivers/llvmpipe/lp_bld_format_aos.c
+++ b/src/gallium/drivers/llvmpipe/lp_bld_format_aos.c
@@ -38,7 +38,6 @@
 
 #include "lp_bld_type.h"
 #include "lp_bld_const.h"
-#include "lp_bld_logic.h"
 #include "lp_bld_swizzle.h"
 #include "lp_bld_format.h"
 
diff --git a/src/gallium/drivers/llvmpipe/lp_bld_tgsi_soa.c b/src/gallium/drivers/llvmpipe/lp_bld_tgsi_soa.c
index fb1eda4..85e3b1b 100644
--- a/src/gallium/drivers/llvmpipe/lp_bld_tgsi_soa.c
+++ b/src/gallium/drivers/llvmpipe/lp_bld_tgsi_soa.c
@@ -47,13 +47,11 @@
 #include "tgsi/tgsi_exec.h"
 #include "lp_bld_type.h"
 #include "lp_bld_const.h"
-#include "lp_bld_intr.h"
 #include "lp_bld_arit.h"
 #include "lp_bld_logic.h"
 #include "lp_bld_swizzle.h"
 #include "lp_bld_flow.h"
 #include "lp_bld_tgsi.h"
-#include "lp_bld_debug.h"
 
 
 #define LP_MAX_TEMPS 256
diff --git a/src/gallium/drivers/llvmpipe/lp_draw_arrays.c b/src/gallium/drivers/llvmpipe/lp_draw_arrays.c
index c152b44..a031619 100644
--- a/src/gallium/drivers/llvmpipe/lp_draw_arrays.c
+++ b/src/gallium/drivers/llvmpipe/lp_draw_arrays.c
@@ -33,8 +33,6 @@
 
 #include "pipe/p_defines.h"
 #include "pipe/p_context.h"
-#include "pipe/internal/p_winsys_screen.h"
-#include "pipe/p_inlines.h"
 #include "util/u_prim.h"
 
 #include "lp_buffer.h"
diff --git a/src/gallium/drivers/llvmpipe/lp_flush.c b/src/gallium/drivers/llvmpipe/lp_flush.c
index cd8381f..6c81012 100644
--- a/src/gallium/drivers/llvmpipe/lp_flush.c
+++ b/src/gallium/drivers/llvmpipe/lp_flush.c
@@ -37,8 +37,6 @@
 #include "lp_surface.h"
 #include "lp_state.h"
 #include "lp_tile_cache.h"
-#include "lp_tex_cache.h"
-#include "lp_winsys.h"
 
 
 void
diff --git a/src/gallium/drivers/llvmpipe/lp_jit.c b/src/gallium/drivers/llvmpipe/lp_jit.c
index 4ef0783..9e0118c 100644
--- a/src/gallium/drivers/llvmpipe/lp_jit.c
+++ b/src/gallium/drivers/llvmpipe/lp_jit.c
@@ -39,7 +39,6 @@
 #include "util/u_cpu_detect.h"
 #include "lp_screen.h"
 #include "lp_bld_intr.h"
-#include "lp_bld_misc.h"
 #include "lp_jit.h"
 
 
diff --git a/src/gallium/drivers/llvmpipe/lp_state_fs.c b/src/gallium/drivers/llvmpipe/lp_state_fs.c
index 9f4bbef..70e2a08 100644
--- a/src/gallium/drivers/llvmpipe/lp_state_fs.c
+++ b/src/gallium/drivers/llvmpipe/lp_state_fs.c
@@ -62,7 +62,6 @@
 #include "util/u_memory.h"
 #include "util/u_format.h"
 #include "util/u_debug_dump.h"
-#include "pipe/internal/p_winsys_screen.h"
 #include "pipe/p_shader_tokens.h"
 #include "draw/draw_context.h"
 #include "tgsi/tgsi_dump.h"
@@ -85,7 +84,6 @@
 #include "lp_context.h"
 #include "lp_buffer.h"
 #include "lp_state.h"
-#include "lp_quad.h"
 #include "lp_tex_sample.h"
 #include "lp_debug.h"
 
diff --git a/src/gallium/drivers/llvmpipe/lp_state_surface.c b/src/gallium/drivers/llvmpipe/lp_state_surface.c
index e37ff04..3b08b0d 100644
--- a/src/gallium/drivers/llvmpipe/lp_state_surface.c
+++ b/src/gallium/drivers/llvmpipe/lp_state_surface.c
@@ -30,7 +30,6 @@
 
 #include "lp_context.h"
 #include "lp_state.h"
-#include "lp_surface.h"
 #include "lp_tile_cache.h"
 
 #include "draw/draw_context.h"
diff --git a/src/gallium/drivers/llvmpipe/lp_state_vertex.c b/src/gallium/drivers/llvmpipe/lp_state_vertex.c
index 1a17631..57ac25e 100644
--- a/src/gallium/drivers/llvmpipe/lp_state_vertex.c
+++ b/src/gallium/drivers/llvmpipe/lp_state_vertex.c
@@ -31,7 +31,6 @@
 
 #include "lp_context.h"
 #include "lp_state.h"
-#include "lp_surface.h"
 
 #include "draw/draw_context.h"
 
diff --git a/src/gallium/drivers/llvmpipe/lp_test_blend.c b/src/gallium/drivers/llvmpipe/lp_test_blend.c
index 6c29e8d..7b65bab 100644
--- a/src/gallium/drivers/llvmpipe/lp_test_blend.c
+++ b/src/gallium/drivers/llvmpipe/lp_test_blend.c
@@ -38,7 +38,6 @@
 
 
 #include "lp_bld_type.h"
-#include "lp_bld_arit.h"
 #include "lp_bld_blend.h"
 #include "lp_bld_debug.h"
 #include "lp_test.h"
diff --git a/src/gallium/drivers/llvmpipe/lp_tex_cache.c b/src/gallium/drivers/llvmpipe/lp_tex_cache.c
index a6d9a2c..8094625 100644
--- a/src/gallium/drivers/llvmpipe/lp_tex_cache.c
+++ b/src/gallium/drivers/llvmpipe/lp_tex_cache.c
@@ -38,7 +38,6 @@
 #include "util/u_format.h"
 #include "util/u_math.h"
 #include "lp_context.h"
-#include "lp_surface.h"
 #include "lp_texture.h"
 #include "lp_tex_cache.h"
 
diff --git a/src/gallium/drivers/llvmpipe/lp_tex_sample_c.c b/src/gallium/drivers/llvmpipe/lp_tex_sample_c.c
new file mode 100644
index 0000000..d1f5d95
--- /dev/null
+++ b/src/gallium/drivers/llvmpipe/lp_tex_sample_c.c
@@ -0,0 +1,1712 @@
+/**************************************************************************
+ * 
+ * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
+ * All Rights Reserved.
+ * Copyright 2008 VMware, Inc.  All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ * 
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ * 
+ **************************************************************************/
+
+/**
+ * Texture sampling
+ *
+ * Authors:
+ *   Brian Paul
+ */
+
+#include "lp_context.h"
+#include "lp_quad.h"
+#include "lp_texture.h"
+#include "lp_tex_sample.h"
+#include "lp_tex_cache.h"
+#include "pipe/p_context.h"
+#include "pipe/p_defines.h"
+#include "pipe/p_shader_tokens.h"
+#include "util/u_math.h"
+#include "util/u_memory.h"
+
+
+
+/*
+ * Note, the FRAC macro has to work perfectly.  Otherwise you'll sometimes
+ * see 1-pixel bands of improperly weighted linear-filtered textures.
+ * The tests/texwrap.c demo is a good test.
+ * Also note, FRAC(x) doesn't truly return the fractional part of x for x < 0.
+ * Instead, if x < 0 then FRAC(x) = 1 - true_frac(x).
+ */
+#define FRAC(f)  ((f) - util_ifloor(f))
+
+
+/**
+ * Linear interpolation macro
+ */
+static INLINE float
+lerp(float a, float v0, float v1)
+{
+   return v0 + a * (v1 - v0);
+}
+
+
+/**
+ * Do 2D/biliner interpolation of float values.
+ * v00, v10, v01 and v11 are typically four texture samples in a square/box.
+ * a and b are the horizontal and vertical interpolants.
+ * It's important that this function is inlined when compiled with
+ * optimization!  If we find that's not true on some systems, convert
+ * to a macro.
+ */
+static INLINE float
+lerp_2d(float a, float b,
+        float v00, float v10, float v01, float v11)
+{
+   const float temp0 = lerp(a, v00, v10);
+   const float temp1 = lerp(a, v01, v11);
+   return lerp(b, temp0, temp1);
+}
+
+
+/**
+ * As above, but 3D interpolation of 8 values.
+ */
+static INLINE float
+lerp_3d(float a, float b, float c,
+        float v000, float v100, float v010, float v110,
+        float v001, float v101, float v011, float v111)
+{
+   const float temp0 = lerp_2d(a, b, v000, v100, v010, v110);
+   const float temp1 = lerp_2d(a, b, v001, v101, v011, v111);
+   return lerp(c, temp0, temp1);
+}
+
+
+
+/**
+ * If A is a signed integer, A % B doesn't give the right value for A < 0
+ * (in terms of texture repeat).  Just casting to unsigned fixes that.
+ */
+#define REMAINDER(A, B) ((unsigned) (A) % (unsigned) (B))
+
+
+/**
+ * Apply texture coord wrapping mode and return integer texture indexes
+ * for a vector of four texcoords (S or T or P).
+ * \param wrapMode  PIPE_TEX_WRAP_x
+ * \param s  the incoming texcoords
+ * \param size  the texture image size
+ * \param icoord  returns the integer texcoords
+ * \return  integer texture index
+ */
+static INLINE void
+nearest_texcoord_4(unsigned wrapMode, const float s[4], unsigned size,
+                   int icoord[4])
+{
+   uint ch;
+   switch (wrapMode) {
+   case PIPE_TEX_WRAP_REPEAT:
+      /* s limited to [0,1) */
+      /* i limited to [0,size-1] */
+      for (ch = 0; ch < 4; ch++) {
+         int i = util_ifloor(s[ch] * size);
+         icoord[ch] = REMAINDER(i, size);
+      }
+      return;
+   case PIPE_TEX_WRAP_CLAMP:
+      /* s limited to [0,1] */
+      /* i limited to [0,size-1] */
+      for (ch = 0; ch < 4; ch++) {
+         if (s[ch] <= 0.0F)
+            icoord[ch] = 0;
+         else if (s[ch] >= 1.0F)
+            icoord[ch] = size - 1;
+         else
+            icoord[ch] = util_ifloor(s[ch] * size);
+      }
+      return;
+   case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
+      {
+         /* s limited to [min,max] */
+         /* i limited to [0, size-1] */
+         const float min = 1.0F / (2.0F * size);
+         const float max = 1.0F - min;
+         for (ch = 0; ch < 4; ch++) {
+            if (s[ch] < min)
+               icoord[ch] = 0;
+            else if (s[ch] > max)
+               icoord[ch] = size - 1;
+            else
+               icoord[ch] = util_ifloor(s[ch] * size);
+         }
+      }
+      return;
+   case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
+      {
+         /* s limited to [min,max] */
+         /* i limited to [-1, size] */
+         const float min = -1.0F / (2.0F * size);
+         const float max = 1.0F - min;
+         for (ch = 0; ch < 4; ch++) {
+            if (s[ch] <= min)
+               icoord[ch] = -1;
+            else if (s[ch] >= max)
+               icoord[ch] = size;
+            else
+               icoord[ch] = util_ifloor(s[ch] * size);
+         }
+      }
+      return;
+   case PIPE_TEX_WRAP_MIRROR_REPEAT:
+      {
+         const float min = 1.0F / (2.0F * size);
+         const float max = 1.0F - min;
+         for (ch = 0; ch < 4; ch++) {
+            const int flr = util_ifloor(s[ch]);
+            float u;
+            if (flr & 1)
+               u = 1.0F - (s[ch] - (float) flr);
+            else
+               u = s[ch] - (float) flr;
+            if (u < min)
+               icoord[ch] = 0;
+            else if (u > max)
+               icoord[ch] = size - 1;
+            else
+               icoord[ch] = util_ifloor(u * size);
+         }
+      }
+      return;
+   case PIPE_TEX_WRAP_MIRROR_CLAMP:
+      for (ch = 0; ch < 4; ch++) {
+         /* s limited to [0,1] */
+         /* i limited to [0,size-1] */
+         const float u = fabsf(s[ch]);
+         if (u <= 0.0F)
+            icoord[ch] = 0;
+         else if (u >= 1.0F)
+            icoord[ch] = size - 1;
+         else
+            icoord[ch] = util_ifloor(u * size);
+      }
+      return;
+   case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
+      {
+         /* s limited to [min,max] */
+         /* i limited to [0, size-1] */
+         const float min = 1.0F / (2.0F * size);
+         const float max = 1.0F - min;
+         for (ch = 0; ch < 4; ch++) {
+            const float u = fabsf(s[ch]);
+            if (u < min)
+               icoord[ch] = 0;
+            else if (u > max)
+               icoord[ch] = size - 1;
+            else
+               icoord[ch] = util_ifloor(u * size);
+         }
+      }
+      return;
+   case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
+      {
+         /* s limited to [min,max] */
+         /* i limited to [0, size-1] */
+         const float min = -1.0F / (2.0F * size);
+         const float max = 1.0F - min;
+         for (ch = 0; ch < 4; ch++) {
+            const float u = fabsf(s[ch]);
+            if (u < min)
+               icoord[ch] = -1;
+            else if (u > max)
+               icoord[ch] = size;
+            else
+               icoord[ch] = util_ifloor(u * size);
+         }
+      }
+      return;
+   default:
+      assert(0);
+   }
+}
+
+
+/**
+ * Used to compute texel locations for linear sampling for four texcoords.
+ * \param wrapMode  PIPE_TEX_WRAP_x
+ * \param s  the texcoords
+ * \param size  the texture image size
+ * \param icoord0  returns first texture indexes
+ * \param icoord1  returns second texture indexes (usually icoord0 + 1)
+ * \param w  returns blend factor/weight between texture indexes
+ * \param icoord  returns the computed integer texture coords
+ */
+static INLINE void
+linear_texcoord_4(unsigned wrapMode, const float s[4], unsigned size,
+                  int icoord0[4], int icoord1[4], float w[4])
+{
+   uint ch;
+
+   switch (wrapMode) {
+   case PIPE_TEX_WRAP_REPEAT:
+      for (ch = 0; ch < 4; ch++) {
+         float u = s[ch] * size - 0.5F;
+         icoord0[ch] = REMAINDER(util_ifloor(u), size);
+         icoord1[ch] = REMAINDER(icoord0[ch] + 1, size);
+         w[ch] = FRAC(u);
+      }
+      break;;
+   case PIPE_TEX_WRAP_CLAMP:
+      for (ch = 0; ch < 4; ch++) {
+         float u = CLAMP(s[ch], 0.0F, 1.0F);
+         u = u * size - 0.5f;
+         icoord0[ch] = util_ifloor(u);
+         icoord1[ch] = icoord0[ch] + 1;
+         w[ch] = FRAC(u);
+      }
+      break;;
+   case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
+      for (ch = 0; ch < 4; ch++) {
+         float u = CLAMP(s[ch], 0.0F, 1.0F);
+         u = u * size - 0.5f;
+         icoord0[ch] = util_ifloor(u);
+         icoord1[ch] = icoord0[ch] + 1;
+         if (icoord0[ch] < 0)
+            icoord0[ch] = 0;
+         if (icoord1[ch] >= (int) size)
+            icoord1[ch] = size - 1;
+         w[ch] = FRAC(u);
+      }
+      break;;
+   case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
+      {
+         const float min = -1.0F / (2.0F * size);
+         const float max = 1.0F - min;
+         for (ch = 0; ch < 4; ch++) {
+            float u = CLAMP(s[ch], min, max);
+            u = u * size - 0.5f;
+            icoord0[ch] = util_ifloor(u);
+            icoord1[ch] = icoord0[ch] + 1;
+            w[ch] = FRAC(u);
+         }
+      }
+      break;;
+   case PIPE_TEX_WRAP_MIRROR_REPEAT:
+      for (ch = 0; ch < 4; ch++) {
+         const int flr = util_ifloor(s[ch]);
+         float u;
+         if (flr & 1)
+            u = 1.0F - (s[ch] - (float) flr);
+         else
+            u = s[ch] - (float) flr;
+         u = u * size - 0.5F;
+         icoord0[ch] = util_ifloor(u);
+         icoord1[ch] = icoord0[ch] + 1;
+         if (icoord0[ch] < 0)
+            icoord0[ch] = 0;
+         if (icoord1[ch] >= (int) size)
+            icoord1[ch] = size - 1;
+         w[ch] = FRAC(u);
+      }
+      break;;
+   case PIPE_TEX_WRAP_MIRROR_CLAMP:
+      for (ch = 0; ch < 4; ch++) {
+         float u = fabsf(s[ch]);
+         if (u >= 1.0F)
+            u = (float) size;
+         else
+            u *= size;
+         u -= 0.5F;
+         icoord0[ch] = util_ifloor(u);
+         icoord1[ch] = icoord0[ch] + 1;
+         w[ch] = FRAC(u);
+      }
+      break;;
+   case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
+      for (ch = 0; ch < 4; ch++) {
+         float u = fabsf(s[ch]);
+         if (u >= 1.0F)
+            u = (float) size;
+         else
+            u *= size;
+         u -= 0.5F;
+         icoord0[ch] = util_ifloor(u);
+         icoord1[ch] = icoord0[ch] + 1;
+         if (icoord0[ch] < 0)
+            icoord0[ch] = 0;
+         if (icoord1[ch] >= (int) size)
+            icoord1[ch] = size - 1;
+         w[ch] = FRAC(u);
+      }
+      break;;
+   case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
+      {
+         const float min = -1.0F / (2.0F * size);
+         const float max = 1.0F - min;
+         for (ch = 0; ch < 4; ch++) {
+            float u = fabsf(s[ch]);
+            if (u <= min)
+               u = min * size;
+            else if (u >= max)
+               u = max * size;
+            else
+               u *= size;
+            u -= 0.5F;
+            icoord0[ch] = util_ifloor(u);
+            icoord1[ch] = icoord0[ch] + 1;
+            w[ch] = FRAC(u);
+         }
+      }
+      break;;
+   default:
+      assert(0);
+   }
+}
+
+
+/**
+ * For RECT textures / unnormalized texcoords
+ * Only a subset of wrap modes supported.
+ */
+static INLINE void
+nearest_texcoord_unnorm_4(unsigned wrapMode, const float s[4], unsigned size,
+                          int icoord[4])
+{
+   uint ch;
+   switch (wrapMode) {
+   case PIPE_TEX_WRAP_CLAMP:
+      for (ch = 0; ch < 4; ch++) {
+         int i = util_ifloor(s[ch]);
+         icoord[ch]= CLAMP(i, 0, (int) size-1);
+      }
+      return;
+   case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
+      /* fall-through */
+   case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
+      for (ch = 0; ch < 4; ch++) {
+         icoord[ch]= util_ifloor( CLAMP(s[ch], 0.5F, (float) size - 0.5F) );
+      }
+      return;
+   default:
+      assert(0);
+   }
+}
+
+
+/**
+ * For RECT textures / unnormalized texcoords.
+ * Only a subset of wrap modes supported.
+ */
+static INLINE void
+linear_texcoord_unnorm_4(unsigned wrapMode, const float s[4], unsigned size,
+                         int icoord0[4], int icoord1[4], float w[4])
+{
+   uint ch;
+   switch (wrapMode) {
+   case PIPE_TEX_WRAP_CLAMP:
+      for (ch = 0; ch < 4; ch++) {
+         /* Not exactly what the spec says, but it matches NVIDIA output */
+         float u = CLAMP(s[ch] - 0.5F, 0.0f, (float) size - 1.0f);
+         icoord0[ch] = util_ifloor(u);
+         icoord1[ch] = icoord0[ch] + 1;
+         w[ch] = FRAC(u);
+      }
+      return;
+   case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
+      /* fall-through */
+   case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
+      for (ch = 0; ch < 4; ch++) {
+         float u = CLAMP(s[ch], 0.5F, (float) size - 0.5F);
+         u -= 0.5F;
+         icoord0[ch] = util_ifloor(u);
+         icoord1[ch] = icoord0[ch] + 1;
+         if (icoord1[ch] > (int) size - 1)
+            icoord1[ch] = size - 1;
+         w[ch] = FRAC(u);
+      }
+      break;
+   default:
+      assert(0);
+   }
+}
+
+
+static unsigned
+choose_cube_face(float rx, float ry, float rz, float *newS, float *newT)
+{
+   /*
+      major axis
+      direction     target                             sc     tc    ma
+      ----------    -------------------------------    ---    ---   ---
+       +rx          TEXTURE_CUBE_MAP_POSITIVE_X_EXT    -rz    -ry   rx
+       -rx          TEXTURE_CUBE_MAP_NEGATIVE_X_EXT    +rz    -ry   rx
+       +ry          TEXTURE_CUBE_MAP_POSITIVE_Y_EXT    +rx    +rz   ry
+       -ry          TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT    +rx    -rz   ry
+       +rz          TEXTURE_CUBE_MAP_POSITIVE_Z_EXT    +rx    -ry   rz
+       -rz          TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT    -rx    -ry   rz
+   */
+   const float arx = fabsf(rx), ary = fabsf(ry), arz = fabsf(rz);
+   unsigned face;
+   float sc, tc, ma;
+
+   if (arx > ary && arx > arz) {
+      if (rx >= 0.0F) {
+         face = PIPE_TEX_FACE_POS_X;
+         sc = -rz;
+         tc = -ry;
+         ma = arx;
+      }
+      else {
+         face = PIPE_TEX_FACE_NEG_X;
+         sc = rz;
+         tc = -ry;
+         ma = arx;
+      }
+   }
+   else if (ary > arx && ary > arz) {
+      if (ry >= 0.0F) {
+         face = PIPE_TEX_FACE_POS_Y;
+         sc = rx;
+         tc = rz;
+         ma = ary;
+      }
+      else {
+         face = PIPE_TEX_FACE_NEG_Y;
+         sc = rx;
+         tc = -rz;
+         ma = ary;
+      }
+   }
+   else {
+      if (rz > 0.0F) {
+         face = PIPE_TEX_FACE_POS_Z;
+         sc = rx;
+         tc = -ry;
+         ma = arz;
+      }
+      else {
+         face = PIPE_TEX_FACE_NEG_Z;
+         sc = -rx;
+         tc = -ry;
+         ma = arz;
+      }
+   }
+
+   *newS = ( sc / ma + 1.0F ) * 0.5F;
+   *newT = ( tc / ma + 1.0F ) * 0.5F;
+
+   return face;
+}
+
+
+/**
+ * Examine the quad's texture coordinates to compute the partial
+ * derivatives w.r.t X and Y, then compute lambda (level of detail).
+ *
+ * This is only done for fragment shaders, not vertex shaders.
+ */
+static float
+compute_lambda(struct tgsi_sampler *tgsi_sampler,
+               const float s[QUAD_SIZE],
+               const float t[QUAD_SIZE],
+               const float p[QUAD_SIZE],
+               float lodbias)
+{
+   const struct lp_shader_sampler *samp = lp_shader_sampler(tgsi_sampler);
+   const struct pipe_texture *texture = samp->texture;
+   const struct pipe_sampler_state *sampler = samp->sampler;
+   float rho, lambda;
+
+   if (samp->processor == TGSI_PROCESSOR_VERTEX)
+      return lodbias;
+
+   assert(sampler->normalized_coords);
+
+   assert(s);
+   {
+      float dsdx = s[QUAD_BOTTOM_RIGHT] - s[QUAD_BOTTOM_LEFT];
+      float dsdy = s[QUAD_TOP_LEFT]     - s[QUAD_BOTTOM_LEFT];
+      dsdx = fabsf(dsdx);
+      dsdy = fabsf(dsdy);
+      rho = MAX2(dsdx, dsdy) * texture->width[0];
+   }
+   if (t) {
+      float dtdx = t[QUAD_BOTTOM_RIGHT] - t[QUAD_BOTTOM_LEFT];
+      float dtdy = t[QUAD_TOP_LEFT]     - t[QUAD_BOTTOM_LEFT];
+      float max;
+      dtdx = fabsf(dtdx);
+      dtdy = fabsf(dtdy);
+      max = MAX2(dtdx, dtdy) * texture->height[0];
+      rho = MAX2(rho, max);
+   }
+   if (p) {
+      float dpdx = p[QUAD_BOTTOM_RIGHT] - p[QUAD_BOTTOM_LEFT];
+      float dpdy = p[QUAD_TOP_LEFT]     - p[QUAD_BOTTOM_LEFT];
+      float max;
+      dpdx = fabsf(dpdx);
+      dpdy = fabsf(dpdy);
+      max = MAX2(dpdx, dpdy) * texture->depth[0];
+      rho = MAX2(rho, max);
+   }
+
+   lambda = util_fast_log2(rho);
+   lambda += lodbias + sampler->lod_bias;
+   lambda = CLAMP(lambda, sampler->min_lod, sampler->max_lod);
+
+   return lambda;
+}
+
+
+/**
+ * Do several things here:
+ * 1. Compute lambda from the texcoords, if needed
+ * 2. Determine if we're minifying or magnifying
+ * 3. If minifying, choose mipmap levels
+ * 4. Return image filter to use within mipmap images
+ * \param level0  Returns first mipmap level to sample from
+ * \param level1  Returns second mipmap level to sample from
+ * \param levelBlend  Returns blend factor between levels, in [0,1]
+ * \param imgFilter  Returns either the min or mag filter, depending on lambda
+ */
+static void
+choose_mipmap_levels(struct tgsi_sampler *tgsi_sampler,
+                     const float s[QUAD_SIZE],
+                     const float t[QUAD_SIZE],
+                     const float p[QUAD_SIZE],
+                     float lodbias,
+                     unsigned *level0, unsigned *level1, float *levelBlend,
+                     unsigned *imgFilter)
+{
+   const struct lp_shader_sampler *samp = lp_shader_sampler(tgsi_sampler);
+   const struct pipe_texture *texture = samp->texture;
+   const struct pipe_sampler_state *sampler = samp->sampler;
+
+   if (sampler->min_mip_filter == PIPE_TEX_MIPFILTER_NONE) {
+      /* no mipmap selection needed */
+      *level0 = *level1 = CLAMP((int) sampler->min_lod,
+                                0, (int) texture->last_level);
+
+      if (sampler->min_img_filter != sampler->mag_img_filter) {
+         /* non-mipmapped texture, but still need to determine if doing
+          * minification or magnification.
+          */
+         float lambda = compute_lambda(tgsi_sampler, s, t, p, lodbias);
+         if (lambda <= 0.0) {
+            *imgFilter = sampler->mag_img_filter;
+         }
+         else {
+            *imgFilter = sampler->min_img_filter;
+         }
+      }
+      else {
+         *imgFilter = sampler->mag_img_filter;
+      }
+   }
+   else {
+      float lambda = compute_lambda(tgsi_sampler, s, t, p, lodbias);
+
+      if (lambda <= 0.0) { /* XXX threshold depends on the filter */
+         /* magnifying */
+         *imgFilter = sampler->mag_img_filter;
+         *level0 = *level1 = 0;
+      }
+      else {
+         /* minifying */
+         *imgFilter = sampler->min_img_filter;
+
+         /* choose mipmap level(s) and compute the blend factor between them */
+         if (sampler->min_mip_filter == PIPE_TEX_MIPFILTER_NEAREST) {
+            /* Nearest mipmap level */
+            const int lvl = (int) (lambda + 0.5);
+            *level0 =
+            *level1 = CLAMP(lvl, 0, (int) texture->last_level);
+         }
+         else {
+            /* Linear interpolation between mipmap levels */
+            const int lvl = (int) lambda;
+            *level0 = CLAMP(lvl,     0, (int) texture->last_level);
+            *level1 = CLAMP(lvl + 1, 0, (int) texture->last_level);
+            *levelBlend = FRAC(lambda);  /* blending weight between levels */
+         }
+      }
+   }
+}
+
+
+/**
+ * Get a texel from a texture, using the texture tile cache.
+ *
+ * \param face  the cube face in 0..5
+ * \param level  the mipmap level
+ * \param x  the x coord of texel within 2D image
+ * \param y  the y coord of texel within 2D image
+ * \param z  which slice of a 3D texture
+ * \param rgba  the quad to put the texel/color into
+ * \param j  which element of the rgba quad to write to
+ *
+ * XXX maybe move this into lp_tile_cache.c and merge with the
+ * lp_get_cached_tile_tex() function.  Also, get 4 texels instead of 1...
+ */
+static void
+get_texel_quad_2d(const struct tgsi_sampler *tgsi_sampler,
+                  unsigned face, unsigned level, int x, int y, 
+                  const uint8_t *out[4])
+{
+   const struct lp_shader_sampler *samp = lp_shader_sampler(tgsi_sampler);
+
+   const struct llvmpipe_cached_tex_tile *tile
+      = lp_get_cached_tex_tile(samp->cache,
+                               tex_tile_address(x, y, 0, face, level));
+
+   y %= TEX_TILE_SIZE;
+   x %= TEX_TILE_SIZE;
+      
+   out[0] = &tile->color[y  ][x  ][0];
+   out[1] = &tile->color[y  ][x+1][0];
+   out[2] = &tile->color[y+1][x  ][0];
+   out[3] = &tile->color[y+1][x+1][0];
+}
+
+static INLINE const uint8_t *
+get_texel_2d_ptr(const struct tgsi_sampler *tgsi_sampler,
+                 unsigned face, unsigned level, int x, int y)
+{
+   const struct lp_shader_sampler *samp = lp_shader_sampler(tgsi_sampler);
+
+   const struct llvmpipe_cached_tex_tile *tile
+      = lp_get_cached_tex_tile(samp->cache,
+                               tex_tile_address(x, y, 0, face, level));
+
+   y %= TEX_TILE_SIZE;
+   x %= TEX_TILE_SIZE;
+
+   return &tile->color[y][x][0];
+}
+
+
+static void
+get_texel_quad_2d_mt(const struct tgsi_sampler *tgsi_sampler,
+                     unsigned face, unsigned level, 
+                     int x0, int y0, 
+                     int x1, int y1,
+                     const uint8_t *out[4])
+{
+   unsigned i;
+
+   for (i = 0; i < 4; i++) {
+      unsigned tx = (i & 1) ? x1 : x0;
+      unsigned ty = (i >> 1) ? y1 : y0;
+
+      out[i] = get_texel_2d_ptr( tgsi_sampler, face, level, tx, ty );
+   }
+}
+
+static void
+get_texel(const struct tgsi_sampler *tgsi_sampler,
+                 unsigned face, unsigned level, int x, int y, int z,
+                 float rgba[NUM_CHANNELS][QUAD_SIZE], unsigned j)
+{
+   const struct lp_shader_sampler *samp = lp_shader_sampler(tgsi_sampler);
+   const struct pipe_texture *texture = samp->texture;
+   const struct pipe_sampler_state *sampler = samp->sampler;
+
+   if (x < 0 || x >= (int) texture->width[level] ||
+       y < 0 || y >= (int) texture->height[level] ||
+       z < 0 || z >= (int) texture->depth[level]) {
+      rgba[0][j] = sampler->border_color[0];
+      rgba[1][j] = sampler->border_color[1];
+      rgba[2][j] = sampler->border_color[2];
+      rgba[3][j] = sampler->border_color[3];
+   }
+   else {
+      const unsigned tx = x % TEX_TILE_SIZE;
+      const unsigned ty = y % TEX_TILE_SIZE;
+      const struct llvmpipe_cached_tex_tile *tile;
+
+      tile = lp_get_cached_tex_tile(samp->cache,
+                                    tex_tile_address(x, y, z, face, level));
+
+      rgba[0][j] = ubyte_to_float(tile->color[ty][tx][0]);
+      rgba[1][j] = ubyte_to_float(tile->color[ty][tx][1]);
+      rgba[2][j] = ubyte_to_float(tile->color[ty][tx][2]);
+      rgba[3][j] = ubyte_to_float(tile->color[ty][tx][3]);
+      if (0)
+      {
+         debug_printf("Get texel %f %f %f %f from %s\n",
+                      rgba[0][j], rgba[1][j], rgba[2][j], rgba[3][j],
+                      pf_name(texture->format));
+      }
+   }
+}
+
+
+/**
+ * Compare texcoord 'p' (aka R) against texture value 'rgba[0]'
+ * When we sampled the depth texture, the depth value was put into all
+ * RGBA channels.  We look at the red channel here.
+ * \param rgba  quad of (depth) texel values
+ * \param p  texture 'P' components for four pixels in quad
+ * \param j  which pixel in the quad to test [0..3]
+ */
+static INLINE void
+shadow_compare(const struct pipe_sampler_state *sampler,
+               float rgba[NUM_CHANNELS][QUAD_SIZE],
+               const float p[QUAD_SIZE],
+               uint j)
+{
+   int k;
+   switch (sampler->compare_func) {
+   case PIPE_FUNC_LESS:
+      k = p[j] < rgba[0][j];
+      break;
+   case PIPE_FUNC_LEQUAL:
+      k = p[j] <= rgba[0][j];
+      break;
+   case PIPE_FUNC_GREATER:
+      k = p[j] > rgba[0][j];
+      break;
+   case PIPE_FUNC_GEQUAL:
+      k = p[j] >= rgba[0][j];
+      break;
+   case PIPE_FUNC_EQUAL:
+      k = p[j] == rgba[0][j];
+      break;
+   case PIPE_FUNC_NOTEQUAL:
+      k = p[j] != rgba[0][j];
+      break;
+   case PIPE_FUNC_ALWAYS:
+      k = 1;
+      break;
+   case PIPE_FUNC_NEVER:
+      k = 0;
+      break;
+   default:
+      k = 0;
+      assert(0);
+      break;
+   }
+
+   /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
+   rgba[0][j] = rgba[1][j] = rgba[2][j] = (float) k;
+   rgba[3][j] = 1.0F;
+}
+
+
+/**
+ * As above, but do four z/texture comparisons.
+ */
+static INLINE void
+shadow_compare4(const struct pipe_sampler_state *sampler,
+                float rgba[NUM_CHANNELS][QUAD_SIZE],
+                const float p[QUAD_SIZE])
+{
+   int j, k0, k1, k2, k3;
+   float val;
+
+   /* compare four texcoords vs. four texture samples */
+   switch (sampler->compare_func) {
+   case PIPE_FUNC_LESS:
+      k0 = p[0] < rgba[0][0];
+      k1 = p[1] < rgba[0][1];
+      k2 = p[2] < rgba[0][2];
+      k3 = p[3] < rgba[0][3];
+      break;
+   case PIPE_FUNC_LEQUAL:
+      k0 = p[0] <= rgba[0][0];
+      k1 = p[1] <= rgba[0][1];
+      k2 = p[2] <= rgba[0][2];
+      k3 = p[3] <= rgba[0][3];
+      break;
+   case PIPE_FUNC_GREATER:
+      k0 = p[0] > rgba[0][0];
+      k1 = p[1] > rgba[0][1];
+      k2 = p[2] > rgba[0][2];
+      k3 = p[3] > rgba[0][3];
+      break;
+   case PIPE_FUNC_GEQUAL:
+      k0 = p[0] >= rgba[0][0];
+      k1 = p[1] >= rgba[0][1];
+      k2 = p[2] >= rgba[0][2];
+      k3 = p[3] >= rgba[0][3];
+      break;
+   case PIPE_FUNC_EQUAL:
+      k0 = p[0] == rgba[0][0];
+      k1 = p[1] == rgba[0][1];
+      k2 = p[2] == rgba[0][2];
+      k3 = p[3] == rgba[0][3];
+      break;
+   case PIPE_FUNC_NOTEQUAL:
+      k0 = p[0] != rgba[0][0];
+      k1 = p[1] != rgba[0][1];
+      k2 = p[2] != rgba[0][2];
+      k3 = p[3] != rgba[0][3];
+      break;
+   case PIPE_FUNC_ALWAYS:
+      k0 = k1 = k2 = k3 = 1;
+      break;
+   case PIPE_FUNC_NEVER:
+      k0 = k1 = k2 = k3 = 0;
+      break;
+   default:
+      k0 = k1 = k2 = k3 = 0;
+      assert(0);
+      break;
+   }
+
+   /* convert four pass/fail values to an intensity in [0,1] */
+   val = 0.25F * (k0 + k1 + k2 + k3);
+
+   /* XXX returning result for default GL_DEPTH_TEXTURE_MODE = GL_LUMINANCE */
+   for (j = 0; j < 4; j++) {
+      rgba[0][j] = rgba[1][j] = rgba[2][j] = val;
+      rgba[3][j] = 1.0F;
+   }
+}
+
+
+
+static void
+lp_get_samples_2d_linear_repeat_POT(struct tgsi_sampler *tgsi_sampler,
+                                    const float s[QUAD_SIZE],
+                                    const float t[QUAD_SIZE],
+                                    const float p[QUAD_SIZE],
+                                    float lodbias,
+                                    float rgba[NUM_CHANNELS][QUAD_SIZE])
+{
+   const struct lp_shader_sampler *samp = lp_shader_sampler(tgsi_sampler);
+   unsigned  j;
+   unsigned level = samp->level;
+   unsigned xpot = 1 << (samp->xpot - level);
+   unsigned ypot = 1 << (samp->ypot - level);
+   unsigned xmax = (xpot - 1) & (TEX_TILE_SIZE - 1); /* MIN2(TEX_TILE_SIZE, xpot) - 1; */
+   unsigned ymax = (ypot - 1) & (TEX_TILE_SIZE - 1); /* MIN2(TEX_TILE_SIZE, ypot) - 1; */
+      
+   for (j = 0; j < QUAD_SIZE; j++) {
+      int c;
+
+      float u = s[j] * xpot - 0.5F;
+      float v = t[j] * ypot - 0.5F;
+
+      int uflr = util_ifloor(u);
+      int vflr = util_ifloor(v);
+
+      float xw = u - (float)uflr;
+      float yw = v - (float)vflr;
+
+      int x0 = uflr & (xpot - 1);
+      int y0 = vflr & (ypot - 1);
+
+      const uint8_t *tx[4];
+      
+
+      /* Can we fetch all four at once:
+       */
+      if (x0 < xmax && y0 < ymax)
+      {
+         get_texel_quad_2d(tgsi_sampler, 0, level, x0, y0, tx);
+      }
+      else 
+      {
+         unsigned x1 = (x0 + 1) & (xpot - 1);
+         unsigned y1 = (y0 + 1) & (ypot - 1);
+         get_texel_quad_2d_mt(tgsi_sampler, 0, level, 
+                              x0, y0, x1, y1, tx);
+      }
+
+
+      /* interpolate R, G, B, A */
+      for (c = 0; c < 4; c++) {
+         rgba[c][j] = lerp_2d(xw, yw, 
+                              ubyte_to_float(tx[0][c]), ubyte_to_float(tx[1][c]),
+                              ubyte_to_float(tx[2][c]), ubyte_to_float(tx[3][c]));
+      }
+   }
+}
+
+
+static void
+lp_get_samples_2d_nearest_repeat_POT(struct tgsi_sampler *tgsi_sampler,
+                                     const float s[QUAD_SIZE],
+                                     const float t[QUAD_SIZE],
+                                     const float p[QUAD_SIZE],
+                                     float lodbias,
+                                     float rgba[NUM_CHANNELS][QUAD_SIZE])
+{
+   const struct lp_shader_sampler *samp = lp_shader_sampler(tgsi_sampler);
+   unsigned  j;
+   unsigned level = samp->level;
+   unsigned xpot = 1 << (samp->xpot - level);
+   unsigned ypot = 1 << (samp->ypot - level);
+
+   for (j = 0; j < QUAD_SIZE; j++) {
+      int c;
+
+      float u = s[j] * xpot;
+      float v = t[j] * ypot;
+
+      int uflr = util_ifloor(u);
+      int vflr = util_ifloor(v);
+
+      int x0 = uflr & (xpot - 1);
+      int y0 = vflr & (ypot - 1);
+
+      const uint8_t *out = get_texel_2d_ptr(tgsi_sampler, 0, level, x0, y0);
+
+      for (c = 0; c < 4; c++) {
+         rgba[c][j] = ubyte_to_float(out[c]);
+      }
+   }
+}
+
+
+static void
+lp_get_samples_2d_nearest_clamp_POT(struct tgsi_sampler *tgsi_sampler,
+                                     const float s[QUAD_SIZE],
+                                     const float t[QUAD_SIZE],
+                                     const float p[QUAD_SIZE],
+                                     float lodbias,
+                                     float rgba[NUM_CHANNELS][QUAD_SIZE])
+{
+   const struct lp_shader_sampler *samp = lp_shader_sampler(tgsi_sampler);
+   unsigned  j;
+   unsigned level = samp->level;
+   unsigned xpot = 1 << (samp->xpot - level);
+   unsigned ypot = 1 << (samp->ypot - level);
+
+   for (j = 0; j < QUAD_SIZE; j++) {
+      int c;
+
+      float u = s[j] * xpot;
+      float v = t[j] * ypot;
+
+      int x0, y0;
+      const uint8_t *out;
+
+      x0 = util_ifloor(u);
+      if (x0 < 0) 
+         x0 = 0;
+      else if (x0 > xpot - 1)
+         x0 = xpot - 1;
+
+      y0 = util_ifloor(v);
+      if (y0 < 0) 
+         y0 = 0;
+      else if (y0 > ypot - 1)
+         y0 = ypot - 1;
+      
+      out = get_texel_2d_ptr(tgsi_sampler, 0, level, x0, y0);
+
+      for (c = 0; c < 4; c++) {
+         rgba[c][j] = ubyte_to_float(out[c]);
+      }
+   }
+}
+
+
+static void
+lp_get_samples_2d_linear_mip_linear_repeat_POT(struct tgsi_sampler *tgsi_sampler,
+                                               const float s[QUAD_SIZE],
+                                               const float t[QUAD_SIZE],
+                                               const float p[QUAD_SIZE],
+                                               float lodbias,
+                                               float rgba[NUM_CHANNELS][QUAD_SIZE])
+{
+   struct lp_shader_sampler *samp = lp_shader_sampler(tgsi_sampler);
+   const struct pipe_texture *texture = samp->texture;
+   int level0;
+   float lambda;
+
+   lambda = compute_lambda(tgsi_sampler, s, t, p, lodbias);
+   level0 = (int)lambda;
+
+   if (lambda < 0.0) { 
+      samp->level = 0;
+      lp_get_samples_2d_linear_repeat_POT( tgsi_sampler,
+                                           s, t, p, 0, rgba );
+   }
+   else if (level0 >= texture->last_level) {
+      samp->level = texture->last_level;
+      lp_get_samples_2d_linear_repeat_POT( tgsi_sampler,
+                                           s, t, p, 0, rgba );
+   }
+   else {
+      float levelBlend = lambda - level0;
+      float rgba0[4][4];
+      float rgba1[4][4];
+      int c,j;
+
+      samp->level = level0;
+      lp_get_samples_2d_linear_repeat_POT( tgsi_sampler,
+                                           s, t, p, 0, rgba0 );
+
+      samp->level = level0+1;
+      lp_get_samples_2d_linear_repeat_POT( tgsi_sampler,
+                                           s, t, p, 0, rgba1 );
+
+      for (j = 0; j < QUAD_SIZE; j++) {
+         for (c = 0; c < 4; c++) {
+            rgba[c][j] = lerp(levelBlend, rgba0[c][j], rgba1[c][j]);
+         }
+      }
+   }
+}
+
+/**
+ * Common code for sampling 1D/2D/cube textures.
+ * Could probably extend for 3D...
+ */
+static void
+lp_get_samples_2d_common(struct tgsi_sampler *tgsi_sampler,
+                         const float s[QUAD_SIZE],
+                         const float t[QUAD_SIZE],
+                         const float p[QUAD_SIZE],
+                         float lodbias,
+                         float rgba[NUM_CHANNELS][QUAD_SIZE],
+                         const unsigned faces[4])
+{
+   const struct lp_shader_sampler *samp = lp_shader_sampler(tgsi_sampler);
+   const struct pipe_texture *texture = samp->texture;
+   const struct pipe_sampler_state *sampler = samp->sampler;
+   unsigned level0, level1, j, imgFilter;
+   int width, height;
+   float levelBlend = 0.0F;
+
+   choose_mipmap_levels(tgsi_sampler, s, t, p, 
+                        lodbias,
+                        &level0, &level1, &levelBlend, &imgFilter);
+
+   assert(sampler->normalized_coords);
+
+   width = texture->width[level0];
+   height = texture->height[level0];
+
+   assert(width > 0);
+
+   switch (imgFilter) {
+   case PIPE_TEX_FILTER_NEAREST:
+      {
+         int x[4], y[4];
+         nearest_texcoord_4(sampler->wrap_s, s, width, x);
+         nearest_texcoord_4(sampler->wrap_t, t, height, y);
+
+         for (j = 0; j < QUAD_SIZE; j++) {
+            get_texel(tgsi_sampler, faces[j], level0, x[j], y[j], 0, rgba, j);
+            if (sampler->compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE) {
+               shadow_compare(sampler, rgba, p, j);
+            }
+
+            if (level0 != level1) {
+               /* get texels from second mipmap level and blend */
+               float rgba2[4][4];
+               unsigned c;
+               x[j] /= 2;
+               y[j] /= 2;
+               get_texel(tgsi_sampler, faces[j], level1, x[j], y[j], 0,
+                         rgba2, j);
+               if (sampler->compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE){
+                  shadow_compare(sampler, rgba2, p, j);
+               }
+
+               for (c = 0; c < NUM_CHANNELS; c++) {
+                  rgba[c][j] = lerp(levelBlend, rgba[c][j], rgba2[c][j]);
+               }
+            }
+         }
+      }
+      break;
+   case PIPE_TEX_FILTER_LINEAR:
+   case PIPE_TEX_FILTER_ANISO:
+      {
+         int x0[4], y0[4], x1[4], y1[4];
+         float xw[4], yw[4]; /* weights */
+
+         linear_texcoord_4(sampler->wrap_s, s, width, x0, x1, xw);
+         linear_texcoord_4(sampler->wrap_t, t, height, y0, y1, yw);
+
+         for (j = 0; j < QUAD_SIZE; j++) {
+            float tx[4][4]; /* texels */
+            int c;
+            get_texel(tgsi_sampler, faces[j], level0, x0[j], y0[j], 0, tx, 0);
+            get_texel(tgsi_sampler, faces[j], level0, x1[j], y0[j], 0, tx, 1);
+            get_texel(tgsi_sampler, faces[j], level0, x0[j], y1[j], 0, tx, 2);
+            get_texel(tgsi_sampler, faces[j], level0, x1[j], y1[j], 0, tx, 3);
+            if (sampler->compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE) {
+               shadow_compare4(sampler, tx, p);
+            }
+
+            /* interpolate R, G, B, A */
+            for (c = 0; c < 4; c++) {
+               rgba[c][j] = lerp_2d(xw[j], yw[j],
+                                    tx[c][0], tx[c][1],
+                                    tx[c][2], tx[c][3]);
+            }
+
+            if (level0 != level1) {
+               /* get texels from second mipmap level and blend */
+               float rgba2[4][4];
+
+               /* XXX: This is incorrect -- will often end up with (x0
+                *  == x1 && y0 == y1), meaning that we fetch the same
+                *  texel four times and linearly interpolate between
+                *  identical values.  The correct approach would be to
+                *  call linear_texcoord again for the second level.
+                */
+               x0[j] /= 2;
+               y0[j] /= 2;
+               x1[j] /= 2;
+               y1[j] /= 2;
+               get_texel(tgsi_sampler, faces[j], level1, x0[j], y0[j], 0, tx, 0);
+               get_texel(tgsi_sampler, faces[j], level1, x1[j], y0[j], 0, tx, 1);
+               get_texel(tgsi_sampler, faces[j], level1, x0[j], y1[j], 0, tx, 2);
+               get_texel(tgsi_sampler, faces[j], level1, x1[j], y1[j], 0, tx, 3);
+               if (sampler->compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE){
+                  shadow_compare4(sampler, tx, p);
+               }
+
+               /* interpolate R, G, B, A */
+               for (c = 0; c < 4; c++) {
+                  rgba2[c][j] = lerp_2d(xw[j], yw[j],
+                                        tx[c][0], tx[c][1], tx[c][2], tx[c][3]);
+               }
+
+               for (c = 0; c < NUM_CHANNELS; c++) {
+                  rgba[c][j] = lerp(levelBlend, rgba[c][j], rgba2[c][j]);
+               }
+            }
+         }
+      }
+      break;
+   default:
+      assert(0);
+   }
+}
+
+
+static INLINE void
+lp_get_samples_1d(struct tgsi_sampler *sampler,
+                  const float s[QUAD_SIZE],
+                  const float t[QUAD_SIZE],
+                  const float p[QUAD_SIZE],
+                  float lodbias,
+                  float rgba[NUM_CHANNELS][QUAD_SIZE])
+{
+   static const unsigned faces[4] = {0, 0, 0, 0};
+   static const float tzero[4] = {0, 0, 0, 0};
+   lp_get_samples_2d_common(sampler, s, tzero, NULL,
+                            lodbias, rgba, faces);
+}
+
+
+static INLINE void
+lp_get_samples_2d(struct tgsi_sampler *sampler,
+                  const float s[QUAD_SIZE],
+                  const float t[QUAD_SIZE],
+                  const float p[QUAD_SIZE],
+                  float lodbias,
+                  float rgba[NUM_CHANNELS][QUAD_SIZE])
+{
+   static const unsigned faces[4] = {0, 0, 0, 0};
+   lp_get_samples_2d_common(sampler, s, t, p,
+                            lodbias, rgba, faces);
+}
+
+
+static INLINE void
+lp_get_samples_3d(struct tgsi_sampler *tgsi_sampler,
+                  const float s[QUAD_SIZE],
+                  const float t[QUAD_SIZE],
+                  const float p[QUAD_SIZE],
+                  float lodbias,
+                  float rgba[NUM_CHANNELS][QUAD_SIZE])
+{
+   const struct lp_shader_sampler *samp = lp_shader_sampler(tgsi_sampler);
+   const struct pipe_texture *texture = samp->texture;
+   const struct pipe_sampler_state *sampler = samp->sampler;
+   /* get/map pipe_surfaces corresponding to 3D tex slices */
+   unsigned level0, level1, j, imgFilter;
+   int width, height, depth;
+   float levelBlend;
+   const uint face = 0;
+
+   choose_mipmap_levels(tgsi_sampler, s, t, p, 
+                        lodbias,
+                        &level0, &level1, &levelBlend, &imgFilter);
+
+   assert(sampler->normalized_coords);
+
+   width = texture->width[level0];
+   height = texture->height[level0];
+   depth = texture->depth[level0];
+
+   assert(width > 0);
+   assert(height > 0);
+   assert(depth > 0);
+
+   switch (imgFilter) {
+   case PIPE_TEX_FILTER_NEAREST:
+      {
+         int x[4], y[4], z[4];
+         nearest_texcoord_4(sampler->wrap_s, s, width, x);
+         nearest_texcoord_4(sampler->wrap_t, t, height, y);
+         nearest_texcoord_4(sampler->wrap_r, p, depth, z);
+         for (j = 0; j < QUAD_SIZE; j++) {
+            get_texel(tgsi_sampler, face, level0, x[j], y[j], z[j], rgba, j);
+            if (level0 != level1) {
+               /* get texels from second mipmap level and blend */
+               float rgba2[4][4];
+               unsigned c;
+               x[j] /= 2;
+               y[j] /= 2;
+               z[j] /= 2;
+               get_texel(tgsi_sampler, face, level1, x[j], y[j], z[j], rgba2, j);
+               for (c = 0; c < NUM_CHANNELS; c++) {
+                  rgba[c][j] = lerp(levelBlend, rgba2[c][j], rgba[c][j]);
+               }
+            }
+         }
+      }
+      break;
+   case PIPE_TEX_FILTER_LINEAR:
+   case PIPE_TEX_FILTER_ANISO:
+      {
+         int x0[4], x1[4], y0[4], y1[4], z0[4], z1[4];
+         float xw[4], yw[4], zw[4]; /* interpolation weights */
+         linear_texcoord_4(sampler->wrap_s, s, width,  x0, x1, xw);
+         linear_texcoord_4(sampler->wrap_t, t, height, y0, y1, yw);
+         linear_texcoord_4(sampler->wrap_r, p, depth,  z0, z1, zw);
+
+         for (j = 0; j < QUAD_SIZE; j++) {
+            int c;
+            float tx0[4][4], tx1[4][4];
+            get_texel(tgsi_sampler, face, level0, x0[j], y0[j], z0[j], tx0, 0);
+            get_texel(tgsi_sampler, face, level0, x1[j], y0[j], z0[j], tx0, 1);
+            get_texel(tgsi_sampler, face, level0, x0[j], y1[j], z0[j], tx0, 2);
+            get_texel(tgsi_sampler, face, level0, x1[j], y1[j], z0[j], tx0, 3);
+            get_texel(tgsi_sampler, face, level0, x0[j], y0[j], z1[j], tx1, 0);
+            get_texel(tgsi_sampler, face, level0, x1[j], y0[j], z1[j], tx1, 1);
+            get_texel(tgsi_sampler, face, level0, x0[j], y1[j], z1[j], tx1, 2);
+            get_texel(tgsi_sampler, face, level0, x1[j], y1[j], z1[j], tx1, 3);
+
+            /* interpolate R, G, B, A */
+            for (c = 0; c < 4; c++) {
+               rgba[c][j] = lerp_3d(xw[j], yw[j], zw[j],
+                                    tx0[c][0], tx0[c][1],
+                                    tx0[c][2], tx0[c][3],
+                                    tx1[c][0], tx1[c][1],
+                                    tx1[c][2], tx1[c][3]);
+            }
+
+            if (level0 != level1) {
+               /* get texels from second mipmap level and blend */
+               float rgba2[4][4];
+               x0[j] /= 2;
+               y0[j] /= 2;
+               z0[j] /= 2;
+               x1[j] /= 2;
+               y1[j] /= 2;
+               z1[j] /= 2;
+               get_texel(tgsi_sampler, face, level1, x0[j], y0[j], z0[j], tx0, 0);
+               get_texel(tgsi_sampler, face, level1, x1[j], y0[j], z0[j], tx0, 1);
+               get_texel(tgsi_sampler, face, level1, x0[j], y1[j], z0[j], tx0, 2);
+               get_texel(tgsi_sampler, face, level1, x1[j], y1[j], z0[j], tx0, 3);
+               get_texel(tgsi_sampler, face, level1, x0[j], y0[j], z1[j], tx1, 0);
+               get_texel(tgsi_sampler, face, level1, x1[j], y0[j], z1[j], tx1, 1);
+               get_texel(tgsi_sampler, face, level1, x0[j], y1[j], z1[j], tx1, 2);
+               get_texel(tgsi_sampler, face, level1, x1[j], y1[j], z1[j], tx1, 3);
+
+               /* interpolate R, G, B, A */
+               for (c = 0; c < 4; c++) {
+                  rgba2[c][j] = lerp_3d(xw[j], yw[j], zw[j],
+                                        tx0[c][0], tx0[c][1],
+                                        tx0[c][2], tx0[c][3],
+                                        tx1[c][0], tx1[c][1],
+                                        tx1[c][2], tx1[c][3]);
+               }
+
+               /* blend mipmap levels */
+               for (c = 0; c < NUM_CHANNELS; c++) {
+                  rgba[c][j] = lerp(levelBlend, rgba[c][j], rgba2[c][j]);
+               }
+            }
+         }
+      }
+      break;
+   default:
+      assert(0);
+   }
+}
+
+
+static void
+lp_get_samples_cube(struct tgsi_sampler *sampler,
+                    const float s[QUAD_SIZE],
+                    const float t[QUAD_SIZE],
+                    const float p[QUAD_SIZE],
+                    float lodbias,
+                    float rgba[NUM_CHANNELS][QUAD_SIZE])
+{
+   unsigned faces[QUAD_SIZE], j;
+   float ssss[4], tttt[4];
+   for (j = 0; j < QUAD_SIZE; j++) {
+      faces[j] = choose_cube_face(s[j], t[j], p[j], ssss + j, tttt + j);
+   }
+   lp_get_samples_2d_common(sampler, ssss, tttt, NULL,
+                            lodbias, rgba, faces);
+}
+
+
+static void
+lp_get_samples_rect(struct tgsi_sampler *tgsi_sampler,
+                    const float s[QUAD_SIZE],
+                    const float t[QUAD_SIZE],
+                    const float p[QUAD_SIZE],
+                    float lodbias,
+                    float rgba[NUM_CHANNELS][QUAD_SIZE])
+{
+   const struct lp_shader_sampler *samp = lp_shader_sampler(tgsi_sampler);
+   const struct pipe_texture *texture = samp->texture;
+   const struct pipe_sampler_state *sampler = samp->sampler;
+   const uint face = 0;
+   unsigned level0, level1, j, imgFilter;
+   int width, height;
+   float levelBlend;
+
+   choose_mipmap_levels(tgsi_sampler, s, t, p, 
+                        lodbias,
+                        &level0, &level1, &levelBlend, &imgFilter);
+
+   /* texture RECTS cannot be mipmapped */
+   assert(level0 == level1);
+
+   width = texture->width[level0];
+   height = texture->height[level0];
+
+   assert(width > 0);
+
+   switch (imgFilter) {
+   case PIPE_TEX_FILTER_NEAREST:
+      {
+         int x[4], y[4];
+         nearest_texcoord_unnorm_4(sampler->wrap_s, s, width, x);
+         nearest_texcoord_unnorm_4(sampler->wrap_t, t, height, y);
+         for (j = 0; j < QUAD_SIZE; j++) {
+            get_texel(tgsi_sampler, face, level0, x[j], y[j], 0, rgba, j);
+            if (sampler->compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE) {
+               shadow_compare(sampler, rgba, p, j);
+            }
+         }
+      }
+      break;
+   case PIPE_TEX_FILTER_LINEAR:
+   case PIPE_TEX_FILTER_ANISO:
+      {
+         int x0[4], y0[4], x1[4], y1[4];
+         float xw[4], yw[4]; /* weights */
+         linear_texcoord_unnorm_4(sampler->wrap_s, s, width,  x0, x1, xw);
+         linear_texcoord_unnorm_4(sampler->wrap_t, t, height, y0, y1, yw);
+         for (j = 0; j < QUAD_SIZE; j++) {
+            float tx[4][4]; /* texels */
+            int c;
+            get_texel(tgsi_sampler, face, level0, x0[j], y0[j], 0, tx, 0);
+            get_texel(tgsi_sampler, face, level0, x1[j], y0[j], 0, tx, 1);
+            get_texel(tgsi_sampler, face, level0, x0[j], y1[j], 0, tx, 2);
+            get_texel(tgsi_sampler, face, level0, x1[j], y1[j], 0, tx, 3);
+            if (sampler->compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE) {
+               shadow_compare4(sampler, tx, p);
+            }
+            for (c = 0; c < 4; c++) {
+               rgba[c][j] = lerp_2d(xw[j], yw[j],
+                                    tx[c][0], tx[c][1], tx[c][2], tx[c][3]);
+            }
+         }
+      }
+      break;
+   default:
+      assert(0);
+   }
+}
+
+
+/**
+ * Error condition handler
+ */
+static INLINE void
+lp_get_samples_null(struct tgsi_sampler *tgsi_sampler,
+                    const float s[QUAD_SIZE],
+                    const float t[QUAD_SIZE],
+                    const float p[QUAD_SIZE],
+                    float lodbias,
+                    float rgba[NUM_CHANNELS][QUAD_SIZE])
+{
+   int i,j;
+
+   for (i = 0; i < 4; i++)
+      for (j = 0; j < 4; j++)
+         rgba[i][j] = 1.0;
+}
+
+/**
+ * Called via tgsi_sampler::get_samples() when using a sampler for the
+ * first time.  Determine the actual sampler function, link it in and
+ * call it.
+ */
+void
+lp_get_samples(struct tgsi_sampler *tgsi_sampler,
+               const float s[QUAD_SIZE],
+               const float t[QUAD_SIZE],
+               const float p[QUAD_SIZE],
+               float lodbias,
+               float rgba[NUM_CHANNELS][QUAD_SIZE])
+{
+   struct lp_shader_sampler *samp = lp_shader_sampler(tgsi_sampler);
+   const struct pipe_texture *texture = samp->texture;
+   const struct pipe_sampler_state *sampler = samp->sampler;
+
+   /* Default to the 'undefined' case:
+    */
+   tgsi_sampler->get_samples = lp_get_samples_null;
+
+   if (!texture) {
+      assert(0);                /* is this legal?? */
+      goto out;
+   }
+
+   if (!sampler->normalized_coords) {
+      assert (texture->target == PIPE_TEXTURE_2D);
+      tgsi_sampler->get_samples = lp_get_samples_rect;
+      goto out;
+   }
+
+   switch (texture->target) {
+   case PIPE_TEXTURE_1D:
+      tgsi_sampler->get_samples = lp_get_samples_1d;
+      break;
+   case PIPE_TEXTURE_2D:
+      tgsi_sampler->get_samples = lp_get_samples_2d;
+      break;
+   case PIPE_TEXTURE_3D:
+      tgsi_sampler->get_samples = lp_get_samples_3d;
+      break;
+   case PIPE_TEXTURE_CUBE:
+      tgsi_sampler->get_samples = lp_get_samples_cube;
+      break;
+   default:
+      assert(0);
+      break;
+   }
+
+   /* Do this elsewhere: 
+    */
+   samp->xpot = util_unsigned_logbase2( samp->texture->width[0] );
+   samp->ypot = util_unsigned_logbase2( samp->texture->height[0] );
+
+   /* Try to hook in a faster sampler.  Ultimately we'll have to
+    * code-generate these.  Luckily most of this looks like it is
+    * orthogonal state within the sampler.
+    */
+   if (texture->target == PIPE_TEXTURE_2D &&
+       sampler->min_img_filter == sampler->mag_img_filter &&
+       sampler->wrap_s == sampler->wrap_t &&
+       sampler->compare_mode == FALSE &&
+       sampler->normalized_coords) 
+   {
+      if (sampler->min_mip_filter == PIPE_TEX_MIPFILTER_NONE) {
+         samp->level = CLAMP((int) sampler->min_lod,
+                             0, (int) texture->last_level);
+
+         if (sampler->wrap_s == PIPE_TEX_WRAP_REPEAT) {
+            switch (sampler->min_img_filter) {
+            case PIPE_TEX_FILTER_NEAREST:
+               tgsi_sampler->get_samples = lp_get_samples_2d_nearest_repeat_POT;
+               break;
+            case PIPE_TEX_FILTER_LINEAR:
+               tgsi_sampler->get_samples = lp_get_samples_2d_linear_repeat_POT;
+               break;
+            default:
+               break;
+            }
+         } 
+         else if (sampler->wrap_s == PIPE_TEX_WRAP_CLAMP) {
+            switch (sampler->min_img_filter) {
+            case PIPE_TEX_FILTER_NEAREST:
+               tgsi_sampler->get_samples = lp_get_samples_2d_nearest_clamp_POT;
+               break;
+            default:
+               break;
+            }
+         }
+      }
+      else if (sampler->min_mip_filter == PIPE_TEX_MIPFILTER_LINEAR) {
+         if (sampler->wrap_s == PIPE_TEX_WRAP_REPEAT) {
+            switch (sampler->min_img_filter) {
+            case PIPE_TEX_FILTER_LINEAR:
+               tgsi_sampler->get_samples = lp_get_samples_2d_linear_mip_linear_repeat_POT;
+               break;
+            default:
+               break;
+            }
+         } 
+      }
+   }
+   else if (0) {
+      _debug_printf("target %d/%d min_mip %d/%d min_img %d/%d wrap %d/%d compare %d/%d norm %d/%d\n",
+                    texture->target, PIPE_TEXTURE_2D,
+                    sampler->min_mip_filter, PIPE_TEX_MIPFILTER_NONE,
+                    sampler->min_img_filter, sampler->mag_img_filter,
+                    sampler->wrap_s, sampler->wrap_t,
+                    sampler->compare_mode, FALSE,
+                    sampler->normalized_coords, TRUE);
+   }
+
+out:
+   tgsi_sampler->get_samples( tgsi_sampler, s, t, p, lodbias, rgba );
+}
+
+
+void PIPE_CDECL
+lp_fetch_texel_soa( struct tgsi_sampler **samplers,
+                    uint32_t unit,
+                    float *store )
+{
+   struct tgsi_sampler *sampler = samplers[unit];
+
+#if 0
+   uint j;
+
+   debug_printf("%s sampler: %p (%p) store: %p\n",
+                __FUNCTION__,
+                sampler, *sampler,
+                store );
+
+   debug_printf("lodbias %f\n", store[12]);
+
+   for (j = 0; j < 4; j++)
+      debug_printf("sample %d texcoord %f %f\n",
+                   j,
+                   store[0+j],
+                   store[4+j]);
+#endif
+
+   {
+      float rgba[NUM_CHANNELS][QUAD_SIZE];
+      sampler->get_samples(sampler,
+                           &store[0],
+                           &store[4],
+                           &store[8],
+                           0.0f, /*store[12],  lodbias */
+                           rgba);
+      memcpy(store, rgba, sizeof rgba);
+   }
+
+#if 0
+   for (j = 0; j < 4; j++)
+      debug_printf("sample %d result %f %f %f %f\n",
+                   j,
+                   store[0+j],
+                   store[4+j],
+                   store[8+j],
+                   store[12+j]);
+#endif
+}
+
+
+#include "lp_bld_type.h"
+#include "lp_bld_intr.h"
+#include "lp_bld_tgsi.h"
+
+
+struct lp_c_sampler_soa
+{
+   struct lp_build_sampler_soa base;
+
+   LLVMValueRef context_ptr;
+
+   LLVMValueRef samplers_ptr;
+
+   /** Coords/texels store */
+   LLVMValueRef store_ptr;
+};
+
+
+static void
+lp_c_sampler_soa_destroy(struct lp_build_sampler_soa *sampler)
+{
+   FREE(sampler);
+}
+
+
+static void
+lp_c_sampler_soa_emit_fetch_texel(struct lp_build_sampler_soa *_sampler,
+                                  LLVMBuilderRef builder,
+                                  struct lp_type type,
+                                  unsigned unit,
+                                  unsigned num_coords,
+                                  const LLVMValueRef *coords,
+                                  LLVMValueRef lodbias,
+                                  LLVMValueRef *texel)
+{
+   struct lp_c_sampler_soa *sampler = (struct lp_c_sampler_soa *)_sampler;
+   LLVMTypeRef vec_type = LLVMTypeOf(coords[0]);
+   LLVMValueRef args[3];
+   unsigned i;
+
+   if(!sampler->samplers_ptr)
+      sampler->samplers_ptr = lp_jit_context_samplers(builder, sampler->context_ptr);
+
+   if(!sampler->store_ptr)
+      sampler->store_ptr = LLVMBuildArrayAlloca(builder,
+                                            vec_type,
+                                            LLVMConstInt(LLVMInt32Type(), 4, 0),
+                                            "texel_store");
+
+   for (i = 0; i < num_coords; i++) {
+      LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), i, 0);
+      LLVMValueRef coord_ptr = LLVMBuildGEP(builder, sampler->store_ptr, &index, 1, "");
+      LLVMBuildStore(builder, coords[i], coord_ptr);
+   }
+
+   args[0] = sampler->samplers_ptr;
+   args[1] = LLVMConstInt(LLVMInt32Type(), unit, 0);
+   args[2] = sampler->store_ptr;
+
+   lp_build_intrinsic(builder, "fetch_texel", LLVMVoidType(), args, 3);
+
+   for (i = 0; i < NUM_CHANNELS; ++i) {
+      LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), i, 0);
+      LLVMValueRef texel_ptr = LLVMBuildGEP(builder, sampler->store_ptr, &index, 1, "");
+      texel[i] = LLVMBuildLoad(builder, texel_ptr, "");
+   }
+}
+
+
+struct lp_build_sampler_soa *
+lp_c_sampler_soa_create(LLVMValueRef context_ptr)
+{
+   struct lp_c_sampler_soa *sampler;
+
+   sampler = CALLOC_STRUCT(lp_c_sampler_soa);
+   if(!sampler)
+      return NULL;
+
+   sampler->base.destroy = lp_c_sampler_soa_destroy;
+   sampler->base.emit_fetch_texel = lp_c_sampler_soa_emit_fetch_texel;
+   sampler->context_ptr = context_ptr;
+
+   return &sampler->base;
+}
+
diff --git a/src/gallium/drivers/llvmpipe/lp_tex_sample_llvm.c b/src/gallium/drivers/llvmpipe/lp_tex_sample_llvm.c
index d2a6ae2..5138ccf 100644
--- a/src/gallium/drivers/llvmpipe/lp_tex_sample_llvm.c
+++ b/src/gallium/drivers/llvmpipe/lp_tex_sample_llvm.c
@@ -44,7 +44,6 @@
 #include "pipe/p_shader_tokens.h"
 #include "lp_bld_debug.h"
 #include "lp_bld_type.h"
-#include "lp_bld_intr.h"
 #include "lp_bld_sample.h"
 #include "lp_bld_tgsi.h"
 #include "lp_state.h"
diff --git a/src/gallium/drivers/llvmpipe/lp_texture.c b/src/gallium/drivers/llvmpipe/lp_texture.c
index 2c13502..039539d 100644
--- a/src/gallium/drivers/llvmpipe/lp_texture.c
+++ b/src/gallium/drivers/llvmpipe/lp_texture.c
@@ -42,7 +42,6 @@
 #include "lp_context.h"
 #include "lp_state.h"
 #include "lp_texture.h"
-#include "lp_tex_cache.h"
 #include "lp_screen.h"
 #include "lp_winsys.h"
 
diff --git a/src/gallium/drivers/llvmpipe/lp_tile_cache.c b/src/gallium/drivers/llvmpipe/lp_tile_cache.c
index 7a1ecf5..971d933 100644
--- a/src/gallium/drivers/llvmpipe/lp_tile_cache.c
+++ b/src/gallium/drivers/llvmpipe/lp_tile_cache.c
@@ -38,8 +38,6 @@
 #include "util/u_tile.h"
 #include "util/u_rect.h"
 #include "lp_context.h"
-#include "lp_surface.h"
-#include "lp_texture.h"
 #include "lp_tile_soa.h"
 #include "lp_tile_cache.h"
 
diff --git a/src/gallium/drivers/softpipe/sp_context.c b/src/gallium/drivers/softpipe/sp_context.c
index 8e01793..73e075f 100644
--- a/src/gallium/drivers/softpipe/sp_context.c
+++ b/src/gallium/drivers/softpipe/sp_context.c
@@ -43,7 +43,6 @@
 #include "sp_surface.h"
 #include "sp_tile_cache.h"
 #include "sp_tex_tile_cache.h"
-#include "sp_texture.h"
 #include "sp_winsys.h"
 #include "sp_query.h"
 
diff --git a/src/gallium/drivers/softpipe/sp_flush.c b/src/gallium/drivers/softpipe/sp_flush.c
index 75dac81..e8952bf 100644
--- a/src/gallium/drivers/softpipe/sp_flush.c
+++ b/src/gallium/drivers/softpipe/sp_flush.c
@@ -34,11 +34,9 @@
 #include "draw/draw_context.h"
 #include "sp_flush.h"
 #include "sp_context.h"
-#include "sp_surface.h"
 #include "sp_state.h"
 #include "sp_tile_cache.h"
 #include "sp_tex_tile_cache.h"
-#include "sp_winsys.h"
 
 
 void
diff --git a/src/gallium/drivers/softpipe/sp_prim_vbuf.c b/src/gallium/drivers/softpipe/sp_prim_vbuf.c
index 5812d1e..98c08ea 100644
--- a/src/gallium/drivers/softpipe/sp_prim_vbuf.c
+++ b/src/gallium/drivers/softpipe/sp_prim_vbuf.c
@@ -526,7 +526,8 @@
 sp_vbuf_destroy(struct vbuf_render *vbr)
 {
    struct softpipe_vbuf_render *cvbr = softpipe_vbuf_render(vbr);
-   align_free(cvbr->vertex_buffer);
+   if(cvbr->vertex_buffer)
+      align_free(cvbr->vertex_buffer);
    sp_setup_destroy_context(cvbr->setup);
    FREE(cvbr);
 }
diff --git a/src/gallium/drivers/softpipe/sp_quad_blend.c b/src/gallium/drivers/softpipe/sp_quad_blend.c
index d9babe8..3b8c2d5 100644
--- a/src/gallium/drivers/softpipe/sp_quad_blend.c
+++ b/src/gallium/drivers/softpipe/sp_quad_blend.c
@@ -35,7 +35,6 @@
 #include "util/u_memory.h"
 #include "sp_context.h"
 #include "sp_quad.h"
-#include "sp_surface.h"
 #include "sp_tile_cache.h"
 #include "sp_quad_pipe.h"
 
diff --git a/src/gallium/drivers/softpipe/sp_quad_depth_test.c b/src/gallium/drivers/softpipe/sp_quad_depth_test.c
index 0ca86c4..a981775 100644
--- a/src/gallium/drivers/softpipe/sp_quad_depth_test.c
+++ b/src/gallium/drivers/softpipe/sp_quad_depth_test.c
@@ -30,11 +30,11 @@
  */
 
 #include "pipe/p_defines.h"
+#include "util/u_format.h"
 #include "util/u_memory.h"
 #include "tgsi/tgsi_scan.h"
 #include "sp_context.h"
 #include "sp_quad.h"
-#include "sp_surface.h"
 #include "sp_quad_pipe.h"
 #include "sp_tile_cache.h"
 #include "sp_state.h"           /* for sp_fragment_shader */
@@ -651,6 +651,20 @@
 
 
 
+/** helper to get number of Z buffer bits */
+static unsigned
+get_depth_bits(struct quad_stage *qs)
+{
+   struct pipe_surface *zsurf = qs->softpipe->framebuffer.zsbuf;
+   if (zsurf)
+      return util_format_get_component_bits(zsurf->format,
+                                            UTIL_FORMAT_COLORSPACE_ZS, 0);
+   else
+      return 0;
+}
+
+
+
 static void
 depth_test_quads_fallback(struct quad_stage *qs, 
                           struct quad_header *quads[],
@@ -666,7 +680,7 @@
       nr = alpha_test_quads(qs, quads, nr);
    }
 
-   if (qs->softpipe->framebuffer.zsbuf && 
+   if (get_depth_bits(qs) > 0 &&
        (qs->softpipe->depth_stencil->depth.enabled ||
         qs->softpipe->depth_stencil->stencil[0].enabled)) {
 
@@ -884,7 +898,7 @@
 
    boolean alpha = qs->softpipe->depth_stencil->alpha.enabled;
 
-   boolean depth = (qs->softpipe->framebuffer.zsbuf && 
+   boolean depth = (get_depth_bits(qs) > 0 &&
                     qs->softpipe->depth_stencil->depth.enabled);
 
    unsigned depthfunc = qs->softpipe->depth_stencil->depth.func;
@@ -895,7 +909,6 @@
 
    boolean occlusion = qs->softpipe->active_query_count;
 
-
    if (!alpha &&
        !depth &&
        !stencil) {
diff --git a/src/gallium/drivers/softpipe/sp_quad_fs.c b/src/gallium/drivers/softpipe/sp_quad_fs.c
index 1e7533d..e799df1 100644
--- a/src/gallium/drivers/softpipe/sp_quad_fs.c
+++ b/src/gallium/drivers/softpipe/sp_quad_fs.c
@@ -45,8 +45,6 @@
 #include "sp_state.h"
 #include "sp_quad.h"
 #include "sp_quad_pipe.h"
-#include "sp_texture.h"
-#include "sp_tex_sample.h"
 
 
 struct quad_shade_stage
diff --git a/src/gallium/drivers/softpipe/sp_setup.c b/src/gallium/drivers/softpipe/sp_setup.c
index 3da7536..f6c3a2b 100644
--- a/src/gallium/drivers/softpipe/sp_setup.c
+++ b/src/gallium/drivers/softpipe/sp_setup.c
@@ -41,7 +41,6 @@
 #include "draw/draw_private.h"
 #include "draw/draw_vertex.h"
 #include "pipe/p_shader_tokens.h"
-#include "pipe/p_thread.h"
 #include "util/u_math.h"
 #include "util/u_memory.h"
 
diff --git a/src/gallium/drivers/softpipe/sp_state_surface.c b/src/gallium/drivers/softpipe/sp_state_surface.c
index f615410..3946678 100644
--- a/src/gallium/drivers/softpipe/sp_state_surface.c
+++ b/src/gallium/drivers/softpipe/sp_state_surface.c
@@ -30,7 +30,6 @@
 
 #include "sp_context.h"
 #include "sp_state.h"
-#include "sp_surface.h"
 #include "sp_tile_cache.h"
 
 #include "draw/draw_context.h"
diff --git a/src/gallium/drivers/softpipe/sp_state_vertex.c b/src/gallium/drivers/softpipe/sp_state_vertex.c
index 46b6991..b491d92 100644
--- a/src/gallium/drivers/softpipe/sp_state_vertex.c
+++ b/src/gallium/drivers/softpipe/sp_state_vertex.c
@@ -31,7 +31,6 @@
 
 #include "sp_context.h"
 #include "sp_state.h"
-#include "sp_surface.h"
 
 #include "draw/draw_context.h"
 
diff --git a/src/gallium/drivers/softpipe/sp_tex_tile_cache.c b/src/gallium/drivers/softpipe/sp_tex_tile_cache.c
index e50a76a..50242d5 100644
--- a/src/gallium/drivers/softpipe/sp_tex_tile_cache.c
+++ b/src/gallium/drivers/softpipe/sp_tex_tile_cache.c
@@ -37,7 +37,6 @@
 #include "util/u_tile.h"
 #include "util/u_math.h"
 #include "sp_context.h"
-#include "sp_surface.h"
 #include "sp_texture.h"
 #include "sp_tex_tile_cache.h"
 
diff --git a/src/gallium/drivers/softpipe/sp_texture.c b/src/gallium/drivers/softpipe/sp_texture.c
index 45e80c5..a5fff91 100644
--- a/src/gallium/drivers/softpipe/sp_texture.c
+++ b/src/gallium/drivers/softpipe/sp_texture.c
@@ -38,7 +38,6 @@
 #include "util/u_memory.h"
 
 #include "sp_context.h"
-#include "sp_state.h"
 #include "sp_texture.h"
 #include "sp_screen.h"
 #include "sp_winsys.h"
diff --git a/src/gallium/drivers/svga/svga_draw_arrays.c b/src/gallium/drivers/svga/svga_draw_arrays.c
index 75492df..6b6ebc9 100644
--- a/src/gallium/drivers/svga/svga_draw_arrays.c
+++ b/src/gallium/drivers/svga/svga_draw_arrays.c
@@ -26,7 +26,6 @@
 #include "svga_cmd.h"
 
 #include "pipe/p_inlines.h"
-#include "util/u_prim.h"
 #include "indices/u_indices.h"
 
 #include "svga_hw_reg.h"
diff --git a/src/gallium/drivers/svga/svga_draw_elements.c b/src/gallium/drivers/svga/svga_draw_elements.c
index 167d817..022b444 100644
--- a/src/gallium/drivers/svga/svga_draw_elements.c
+++ b/src/gallium/drivers/svga/svga_draw_elements.c
@@ -24,7 +24,6 @@
  **********************************************************/
 
 #include "pipe/p_inlines.h"
-#include "util/u_prim.h"
 #include "util/u_upload_mgr.h"
 #include "indices/u_indices.h"
 
diff --git a/src/gallium/drivers/svga/svga_pipe_blend.c b/src/gallium/drivers/svga/svga_pipe_blend.c
index 855d228..3ad3f97 100644
--- a/src/gallium/drivers/svga/svga_pipe_blend.c
+++ b/src/gallium/drivers/svga/svga_pipe_blend.c
@@ -29,7 +29,6 @@
 #include "util/u_memory.h"
 
 #include "svga_context.h"
-#include "svga_state.h"
 
 #include "svga_hw_reg.h"
 
diff --git a/src/gallium/drivers/svga/svga_pipe_constants.c b/src/gallium/drivers/svga/svga_pipe_constants.c
index ca2c7c4..93022f3 100644
--- a/src/gallium/drivers/svga/svga_pipe_constants.c
+++ b/src/gallium/drivers/svga/svga_pipe_constants.c
@@ -30,9 +30,6 @@
 #include "tgsi/tgsi_parse.h"
 
 #include "svga_context.h"
-#include "svga_state.h"
-#include "svga_hw_reg.h"
-#include "svga_cmd.h"
 
 /***********************************************************************
  * Constant buffers 
diff --git a/src/gallium/drivers/svga/svga_pipe_depthstencil.c b/src/gallium/drivers/svga/svga_pipe_depthstencil.c
index df636c0..34e60cb 100644
--- a/src/gallium/drivers/svga/svga_pipe_depthstencil.c
+++ b/src/gallium/drivers/svga/svga_pipe_depthstencil.c
@@ -29,7 +29,6 @@
 #include "util/u_memory.h"
 
 #include "svga_context.h"
-#include "svga_state.h"
 #include "svga_hw_reg.h"
 
 
diff --git a/src/gallium/drivers/svga/svga_pipe_draw.c b/src/gallium/drivers/svga/svga_pipe_draw.c
index 0f24ef4..4e0c499 100644
--- a/src/gallium/drivers/svga/svga_pipe_draw.c
+++ b/src/gallium/drivers/svga/svga_pipe_draw.c
@@ -33,7 +33,6 @@
 #include "svga_hw_reg.h"
 #include "svga_context.h"
 #include "svga_screen.h"
-#include "svga_winsys.h"
 #include "svga_draw.h"
 #include "svga_state.h"
 #include "svga_swtnl.h"
diff --git a/src/gallium/drivers/svga/svga_pipe_flush.c b/src/gallium/drivers/svga/svga_pipe_flush.c
index 0becb07..3eb1033 100644
--- a/src/gallium/drivers/svga/svga_pipe_flush.c
+++ b/src/gallium/drivers/svga/svga_pipe_flush.c
@@ -28,13 +28,8 @@
 #include "svga_screen_texture.h"
 #include "svga_context.h"
 #include "svga_winsys.h"
-#include "svga_draw.h"
 #include "svga_debug.h"
 
-#include "svga_hw_reg.h"
-
-
-
 
 static void svga_flush( struct pipe_context *pipe,
                         unsigned flags,
diff --git a/src/gallium/drivers/svga/svga_pipe_fs.c b/src/gallium/drivers/svga/svga_pipe_fs.c
index 5f1213e..32f07fb 100644
--- a/src/gallium/drivers/svga/svga_pipe_fs.c
+++ b/src/gallium/drivers/svga/svga_pipe_fs.c
@@ -32,11 +32,9 @@
 
 #include "svga_screen.h"
 #include "svga_context.h"
-#include "svga_state.h"
 #include "svga_tgsi.h"
 #include "svga_hw_reg.h"
 #include "svga_cmd.h"
-#include "svga_draw.h"
 #include "svga_debug.h"
 
 
diff --git a/src/gallium/drivers/svga/svga_pipe_misc.c b/src/gallium/drivers/svga/svga_pipe_misc.c
index 58cb1e6..8cf1f2e 100644
--- a/src/gallium/drivers/svga/svga_pipe_misc.c
+++ b/src/gallium/drivers/svga/svga_pipe_misc.c
@@ -27,12 +27,6 @@
 
 #include "svga_context.h"
 #include "svga_screen_texture.h"
-#include "svga_state.h"
-#include "svga_winsys.h"
-
-#include "svga_hw_reg.h"
-
-
 
 
 static void svga_set_scissor_state( struct pipe_context *pipe,
diff --git a/src/gallium/drivers/svga/svga_pipe_query.c b/src/gallium/drivers/svga/svga_pipe_query.c
index 01336b0..08283e3 100644
--- a/src/gallium/drivers/svga/svga_pipe_query.c
+++ b/src/gallium/drivers/svga/svga_pipe_query.c
@@ -32,7 +32,6 @@
 #include "svga_screen.h"
 #include "svga_screen_buffer.h"
 #include "svga_winsys.h"
-#include "svga_draw.h"
 #include "svga_debug.h"
 
 
diff --git a/src/gallium/drivers/svga/svga_pipe_rasterizer.c b/src/gallium/drivers/svga/svga_pipe_rasterizer.c
index b03f8eb..9ea11aa 100644
--- a/src/gallium/drivers/svga/svga_pipe_rasterizer.c
+++ b/src/gallium/drivers/svga/svga_pipe_rasterizer.c
@@ -30,7 +30,6 @@
 #include "util/u_memory.h"
 
 #include "svga_context.h"
-#include "svga_state.h"
 
 #include "svga_hw_reg.h"
 
diff --git a/src/gallium/drivers/svga/svga_pipe_sampler.c b/src/gallium/drivers/svga/svga_pipe_sampler.c
index 460a101..161c66d 100644
--- a/src/gallium/drivers/svga/svga_pipe_sampler.c
+++ b/src/gallium/drivers/svga/svga_pipe_sampler.c
@@ -32,9 +32,6 @@
 
 #include "svga_context.h"
 #include "svga_screen_texture.h"
-#include "svga_state.h"
-
-#include "svga_hw_reg.h"
 
 #include "svga_debug.h"
 
diff --git a/src/gallium/drivers/svga/svga_pipe_vertex.c b/src/gallium/drivers/svga/svga_pipe_vertex.c
index 42f290d..0bf43fa 100644
--- a/src/gallium/drivers/svga/svga_pipe_vertex.c
+++ b/src/gallium/drivers/svga/svga_pipe_vertex.c
@@ -32,10 +32,6 @@
 #include "svga_screen.h"
 #include "svga_screen_buffer.h"
 #include "svga_context.h"
-#include "svga_state.h"
-#include "svga_winsys.h"
-
-#include "svga_hw_reg.h"
 
 
 static void svga_set_vertex_buffers(struct pipe_context *pipe,
diff --git a/src/gallium/drivers/svga/svga_pipe_vs.c b/src/gallium/drivers/svga/svga_pipe_vs.c
index 7e6ab57..c4ac530 100644
--- a/src/gallium/drivers/svga/svga_pipe_vs.c
+++ b/src/gallium/drivers/svga/svga_pipe_vs.c
@@ -33,7 +33,6 @@
 
 #include "svga_screen.h"
 #include "svga_context.h"
-#include "svga_state.h"
 #include "svga_tgsi.h"
 #include "svga_hw_reg.h"
 #include "svga_cmd.h"
diff --git a/src/gallium/drivers/svga/svga_screen.c b/src/gallium/drivers/svga/svga_screen.c
index fc1b3c9..cd1ed7b 100644
--- a/src/gallium/drivers/svga/svga_screen.c
+++ b/src/gallium/drivers/svga/svga_screen.c
@@ -33,10 +33,8 @@
 #include "svga_screen.h"
 #include "svga_screen_texture.h"
 #include "svga_screen_buffer.h"
-#include "svga_cmd.h"
 #include "svga_debug.h"
 
-#include "svga_hw_reg.h"
 #include "svga3d_shaderdefs.h"
 
 
@@ -393,8 +391,6 @@
    pipe_mutex_init(svgascreen->tex_mutex);
    pipe_mutex_init(svgascreen->swc_mutex);
 
-   LIST_INITHEAD(&svgascreen->cached_buffers);
-   
    svga_screen_cache_init(svgascreen);
 
    return screen;
diff --git a/src/gallium/drivers/svga/svga_screen.h b/src/gallium/drivers/svga/svga_screen.h
index b94ca7f..a009b60 100644
--- a/src/gallium/drivers/svga/svga_screen.h
+++ b/src/gallium/drivers/svga/svga_screen.h
@@ -68,12 +68,6 @@
    pipe_mutex tex_mutex; 
    pipe_mutex swc_mutex; /* Protects the use of swc and dirty_buffers */
    
-   /** 
-    * List of buffers with cached GMR. Ordered from the most recently used to
-    * the least recently used 
-    */
-   struct list_head cached_buffers;
-   
    struct svga_host_surface_cache cache;
 };
 
diff --git a/src/gallium/drivers/svga/svga_screen_buffer.c b/src/gallium/drivers/svga/svga_screen_buffer.c
index 58a1aba..430a697 100644
--- a/src/gallium/drivers/svga/svga_screen_buffer.c
+++ b/src/gallium/drivers/svga/svga_screen_buffer.c
@@ -113,68 +113,9 @@
    if(sbuf->hw.buf) {
       sws->buffer_destroy(sws, sbuf->hw.buf);
       sbuf->hw.buf = NULL;
-      assert(sbuf->head.prev && sbuf->head.next);
-      LIST_DEL(&sbuf->head);
-#ifdef DEBUG
-      sbuf->head.next = sbuf->head.prev = NULL; 
-#endif
    }
 }
 
-static INLINE enum pipe_error
-svga_buffer_backup(struct svga_screen *ss, struct svga_buffer *sbuf)
-{
-   if (sbuf->hw.buf && sbuf->hw.num_ranges) {
-      void *src;
-
-      if (!sbuf->swbuf)
-	 sbuf->swbuf = align_malloc(sbuf->base.size, sbuf->base.alignment);
-      if (!sbuf->swbuf)
-	 return PIPE_ERROR_OUT_OF_MEMORY;
-
-      src = ss->sws->buffer_map(ss->sws, sbuf->hw.buf,
-				PIPE_BUFFER_USAGE_CPU_READ);
-      if (!src)
-	 return PIPE_ERROR;
-
-      memcpy(sbuf->swbuf, src, sbuf->base.size);
-      ss->sws->buffer_unmap(ss->sws, sbuf->hw.buf);
-   }
-
-   return PIPE_OK;
-}
-
-/**
- * Try to make GMR space available by freeing the hardware storage of 
- * unmapped
- */
-boolean
-svga_buffer_free_cached_hw_storage(struct svga_screen *ss)
-{
-   struct list_head *curr;
-   struct svga_buffer *sbuf;
-   enum pipe_error ret = PIPE_OK;
-
-   curr = ss->cached_buffers.prev;
-   
-   /* free the least recently used buffer's hw storage which is not mapped */
-   do {
-      if(curr == &ss->cached_buffers)
-         return FALSE;
-
-      sbuf = LIST_ENTRY(struct svga_buffer, curr, head);
-      
-      curr = curr->prev;
-      if (sbuf->map.count == 0)
-	 ret = svga_buffer_backup(ss, sbuf);
-
-   } while(sbuf->map.count != 0 || ret != PIPE_OK);
-   
-   svga_buffer_destroy_hw_storage(ss, sbuf);
-   
-   return TRUE;
-}
-
 struct svga_winsys_buffer *
 svga_winsys_buffer_create( struct svga_screen *ss,
                            unsigned alignment, 
@@ -195,12 +136,6 @@
       svga_screen_flush(ss, NULL);
       buf = sws->buffer_create(sws, alignment, usage, size);
 
-      SVGA_DBG(DEBUG_DMA|DEBUG_PERF, "evicting buffers to find %d bytes GMR\n", 
-               size);
-
-      /* Try evicing all buffer storage */
-      while(!buf && svga_buffer_free_cached_hw_storage(ss))
-         buf = sws->buffer_create(sws, alignment, usage, size);
    }
    
    return buf;
@@ -226,8 +161,6 @@
          return PIPE_ERROR_OUT_OF_MEMORY;
       
       assert(!sbuf->needs_flush);
-      assert(!sbuf->head.prev && !sbuf->head.next);
-      LIST_ADD(&sbuf->head, &ss->cached_buffers);
    }
    
    return PIPE_OK;
@@ -311,7 +244,6 @@
 svga_buffer_upload_flush(struct svga_context *svga,
                          struct svga_buffer *sbuf)
 {
-   struct svga_screen *ss = svga_screen(svga->pipe.screen);
    SVGA3dCopyBox *boxes;
    unsigned i;
 
@@ -348,13 +280,16 @@
 
    assert(sbuf->head.prev && sbuf->head.next);
    LIST_DEL(&sbuf->head);
+#ifdef DEBUG
+   sbuf->head.next = sbuf->head.prev = NULL; 
+#endif
    sbuf->needs_flush = FALSE;
-   /* XXX: do we care about cached_buffers any more ?*/
-   LIST_ADD(&sbuf->head, &ss->cached_buffers);
 
    sbuf->hw.svga = NULL;
    sbuf->hw.boxes = NULL;
 
+   sbuf->host_written = TRUE;
+
    /* Decrement reference count */
    pipe_reference(&(sbuf->base.reference), NULL);
    sbuf = NULL;
@@ -437,17 +372,17 @@
    }
    else {
       if(!sbuf->hw.buf) {
-         struct svga_winsys_surface *handle = sbuf->handle;
-
          if(svga_buffer_create_hw_storage(ss, sbuf) != PIPE_OK)
             return NULL;
          
          /* Populate the hardware storage if the host surface pre-existed */
-         if((usage & PIPE_BUFFER_USAGE_CPU_READ) && handle) {
+         if(sbuf->host_written) {
             SVGA3dSurfaceDMAFlags flags;
             enum pipe_error ret;
             struct pipe_fence_handle *fence = NULL;
             
+            assert(sbuf->handle);
+
             SVGA_DBG(DEBUG_DMA|DEBUG_PERF, "dma from sid %p (buffer), bytes %u - %u\n", 
                      sbuf->handle, 0, sbuf->base.size);
 
@@ -478,17 +413,6 @@
             sws->fence_reference(sws, &fence, NULL);
          }
       }
-      else {
-         if((usage & PIPE_BUFFER_USAGE_CPU_READ) && !sbuf->needs_flush) {
-            /* We already had the hardware storage but we would have to issue
-             * a download if we hadn't, so move the buffer to the begginning
-             * of the LRU list.
-             */
-            assert(sbuf->head.prev && sbuf->head.next);
-            LIST_DEL(&sbuf->head);
-            LIST_ADD(&sbuf->head, &ss->cached_buffers);
-         }
-      }
          
       map = sws->buffer_map(sws, sbuf->hw.buf, usage);
    }
@@ -572,10 +496,8 @@
    
    assert(!sbuf->needs_flush);
 
-   if(sbuf->handle) {
-      SVGA_DBG(DEBUG_DMA, "release sid %p sz %d\n", sbuf->handle, sbuf->base.size);
-      svga_screen_surface_destroy(ss, &sbuf->key, &sbuf->handle);
-   }
+   if(sbuf->handle)
+      svga_buffer_destroy_host_surface(ss, sbuf);
    
    if(sbuf->hw.buf)
       svga_buffer_destroy_hw_storage(ss, sbuf);
@@ -595,6 +517,9 @@
    struct svga_screen *ss = svga_screen(screen);
    struct svga_buffer *sbuf;
    
+   assert(size);
+   assert(alignment);
+
    sbuf = CALLOC_STRUCT(svga_buffer);
    if(!sbuf)
       goto error1;
@@ -755,8 +680,7 @@
       assert(sbuf->hw.svga == svga);
 
       sbuf->needs_flush = TRUE;
-      assert(sbuf->head.prev && sbuf->head.next);
-      LIST_DEL(&sbuf->head);
+      assert(!sbuf->head.prev && !sbuf->head.next);
       LIST_ADDTAIL(&sbuf->head, &svga->dirty_buffers);
    }
 
diff --git a/src/gallium/drivers/svga/svga_screen_buffer.h b/src/gallium/drivers/svga/svga_screen_buffer.h
index 5d7af5a..448ac10 100644
--- a/src/gallium/drivers/svga/svga_screen_buffer.h
+++ b/src/gallium/drivers/svga/svga_screen_buffer.h
@@ -135,6 +135,11 @@
     */
    struct svga_winsys_surface *handle;
    
+   /**
+    * Whether the host has been ever written.
+    */
+   boolean host_written;
+
    struct {
       unsigned count;
       boolean writing;
@@ -178,9 +183,6 @@
 void
 svga_context_flush_buffers(struct svga_context *svga);
 
-boolean
-svga_buffer_free_cached_hw_storage(struct svga_screen *ss);
-
 struct svga_winsys_buffer *
 svga_winsys_buffer_create(struct svga_screen *ss,
                           unsigned alignment, 
diff --git a/src/gallium/drivers/svga/svga_screen_texture.c b/src/gallium/drivers/svga/svga_screen_texture.c
index 2224c2d..0d69007 100644
--- a/src/gallium/drivers/svga/svga_screen_texture.c
+++ b/src/gallium/drivers/svga/svga_screen_texture.c
@@ -306,11 +306,19 @@
       tex->key.numFaces = 1;
    }
 
+   tex->key.cachable = 1;
+
    if(templat->tex_usage & PIPE_TEXTURE_USAGE_SAMPLER)
       tex->key.flags |= SVGA3D_SURFACE_HINT_TEXTURE;
 
-   if(templat->tex_usage & PIPE_TEXTURE_USAGE_PRIMARY)
+   if(templat->tex_usage & PIPE_TEXTURE_USAGE_DISPLAY_TARGET) {
+      tex->key.cachable = 0;
+   }
+
+   if(templat->tex_usage & PIPE_TEXTURE_USAGE_PRIMARY) {
       tex->key.flags |= SVGA3D_SURFACE_HINT_SCANOUT;
+      tex->key.cachable = 0;
+   }
    
    /* 
     * XXX: Never pass the SVGA3D_SURFACE_HINT_RENDERTARGET hint. Mesa cannot
@@ -333,8 +341,6 @@
    if(tex->key.format == SVGA3D_FORMAT_INVALID)
       goto error2;
 
-   tex->key.cachable = 1;
-   
    SVGA_DBG(DEBUG_DMA, "surface_create for texture\n", tex->handle);
    tex->handle = svga_screen_surface_create(svgascreen, &tex->key);
    if (tex->handle)
@@ -416,6 +422,62 @@
 }
 
 
+struct pipe_texture *
+svga_screen_texture_wrap_surface(struct pipe_screen *screen,
+				 struct pipe_texture *base,
+				 enum SVGA3dSurfaceFormat format,
+				 struct svga_winsys_surface *srf)
+{
+   struct svga_texture *tex;
+   assert(screen);
+
+   /* Only supports one type */
+   if (base->target != PIPE_TEXTURE_2D ||
+       base->last_level != 0 ||
+       base->depth0 != 1) {
+      return NULL;
+   }
+
+   if (!srf)
+      return NULL;
+
+   if (svga_translate_format(base->format) != format) {
+      unsigned f1 = svga_translate_format(base->format);
+      unsigned f2 = format;
+
+      /* It's okay for XRGB and ARGB or depth with/out stencil to get mixed up */
+      if ( !( (f1 == SVGA3D_X8R8G8B8 && f2 == SVGA3D_A8R8G8B8) ||
+              (f1 == SVGA3D_A8R8G8B8 && f2 == SVGA3D_X8R8G8B8) ||
+              (f1 == SVGA3D_Z_D24X8 && f2 == SVGA3D_Z_D24S8) ) ) {
+         debug_printf("%s wrong format %u != %u\n", __FUNCTION__, f1, f2);
+         return NULL;
+      }
+   }
+
+   tex = CALLOC_STRUCT(svga_texture);
+   if (!tex)
+      return NULL;
+
+   tex->base = *base;
+   
+
+   if (format == 1)
+      tex->base.format = PIPE_FORMAT_X8R8G8B8_UNORM;
+   else if (format == 2)
+      tex->base.format = PIPE_FORMAT_A8R8G8B8_UNORM;
+
+   pipe_reference_init(&tex->base.reference, 1);
+   tex->base.screen = screen;
+
+   SVGA_DBG(DEBUG_DMA, "wrap surface sid %p\n", srf);
+
+   tex->key.cachable = 0;
+   tex->handle = srf;
+
+   return &tex->base;
+}
+
+
 static void
 svga_texture_destroy(struct pipe_texture *pt)
 {
diff --git a/src/gallium/drivers/svga/svga_state_framebuffer.c b/src/gallium/drivers/svga/svga_state_framebuffer.c
index cfdcae4..eda1aef 100644
--- a/src/gallium/drivers/svga/svga_state_framebuffer.c
+++ b/src/gallium/drivers/svga/svga_state_framebuffer.c
@@ -32,8 +32,6 @@
 #include "svga_cmd.h"
 #include "svga_debug.h"
 
-#include "svga_hw_reg.h"
-
 
 /***********************************************************************
  * Hardware state update
diff --git a/src/gallium/drivers/svga/svga_state_rss.c b/src/gallium/drivers/svga/svga_state_rss.c
index 8b6803a..2f9adae 100644
--- a/src/gallium/drivers/svga/svga_state_rss.c
+++ b/src/gallium/drivers/svga/svga_state_rss.c
@@ -31,9 +31,6 @@
 #include "svga_state.h"
 #include "svga_cmd.h"
 
-#include "svga_hw_reg.h"
-
-
 
 struct rs_queue {
    unsigned rs_count;
diff --git a/src/gallium/drivers/svga/svga_state_tss.c b/src/gallium/drivers/svga/svga_state_tss.c
index b313794..b3c9687 100644
--- a/src/gallium/drivers/svga/svga_state_tss.c
+++ b/src/gallium/drivers/svga/svga_state_tss.c
@@ -33,8 +33,6 @@
 #include "svga_state.h"
 #include "svga_cmd.h"
 
-#include "svga_hw_reg.h"
-
 
 void svga_cleanup_tss_binding(struct svga_context *svga)
 {
diff --git a/src/gallium/drivers/svga/svga_swtnl_backend.c b/src/gallium/drivers/svga/svga_swtnl_backend.c
index b4f757a..aafb3e2 100644
--- a/src/gallium/drivers/svga/svga_swtnl_backend.c
+++ b/src/gallium/drivers/svga/svga_swtnl_backend.c
@@ -31,7 +31,6 @@
 #include "pipe/p_inlines.h"
 #include "util/u_math.h"
 #include "util/u_memory.h"
-#include "util/u_simple_shaders.h"
 
 #include "svga_context.h"
 #include "svga_state.h"
@@ -87,13 +86,13 @@
    if (!svga_render->vbuf) {
       svga_render->vbuf_size = MAX2(size, svga_render->vbuf_alloc_size);
       svga_render->vbuf = pipe_buffer_create(screen,
-                                             0,
+                                             16,
                                              PIPE_BUFFER_USAGE_VERTEX,
                                              svga_render->vbuf_size);
       if(!svga_render->vbuf) {
          svga_context_flush(svga, NULL);
          svga_render->vbuf = pipe_buffer_create(screen,
-                                                0,
+                                                16,
                                                 PIPE_BUFFER_USAGE_VERTEX,
                                                 svga_render->vbuf_size);
          assert(svga_render->vbuf);
@@ -123,7 +122,9 @@
    char *ptr = (char*)pipe_buffer_map(screen,
                                       svga_render->vbuf,
                                       PIPE_BUFFER_USAGE_CPU_WRITE | 
-                                      PIPE_BUFFER_USAGE_FLUSH_EXPLICIT);
+                                      PIPE_BUFFER_USAGE_FLUSH_EXPLICIT |
+                                      PIPE_BUFFER_USAGE_DISCARD |
+                                      PIPE_BUFFER_USAGE_UNSYNCHRONIZED);
    return ptr + svga_render->vbuf_offset;
 }
 
@@ -259,14 +260,14 @@
    if (!svga_render->ibuf) {
       svga_render->ibuf_size = MAX2(size, svga_render->ibuf_alloc_size);
       svga_render->ibuf = pipe_buffer_create(screen,
-                                             0,
+                                             2,
                                              PIPE_BUFFER_USAGE_VERTEX,
                                              svga_render->ibuf_size);
       svga_render->ibuf_offset = 0;
    }
 
-   pipe_buffer_write(screen, svga_render->ibuf,
-                     svga_render->ibuf_offset, 2 * nr_indices, indices);
+   pipe_buffer_write_nooverlap(screen, svga_render->ibuf,
+                                 svga_render->ibuf_offset, 2 * nr_indices, indices);
 
 
    /* off to hardware */
diff --git a/src/gallium/drivers/svga/svga_swtnl_draw.c b/src/gallium/drivers/svga/svga_swtnl_draw.c
index 7655121..0ae58c7 100644
--- a/src/gallium/drivers/svga/svga_swtnl_draw.c
+++ b/src/gallium/drivers/svga/svga_swtnl_draw.c
@@ -27,7 +27,6 @@
 #include "draw/draw_vbuf.h"
 #include "pipe/p_inlines.h"
 #include "pipe/p_state.h"
-#include "util/u_memory.h"
 
 #include "svga_context.h"
 #include "svga_swtnl.h"
diff --git a/src/gallium/drivers/svga/svga_swtnl_state.c b/src/gallium/drivers/svga/svga_swtnl_state.c
index 94b6ccc..fe03e20 100644
--- a/src/gallium/drivers/svga/svga_swtnl_state.c
+++ b/src/gallium/drivers/svga/svga_swtnl_state.c
@@ -27,7 +27,6 @@
 #include "draw/draw_vbuf.h"
 #include "pipe/p_inlines.h"
 #include "pipe/p_state.h"
-#include "util/u_memory.h"
 
 #include "svga_context.h"
 #include "svga_swtnl.h"
diff --git a/src/gallium/drivers/svga/svga_tgsi_decl_sm20.c b/src/gallium/drivers/svga/svga_tgsi_decl_sm20.c
index 23b3ace..1ae9906 100644
--- a/src/gallium/drivers/svga/svga_tgsi_decl_sm20.c
+++ b/src/gallium/drivers/svga/svga_tgsi_decl_sm20.c
@@ -29,9 +29,6 @@
 #include "util/u_memory.h"
 
 #include "svga_tgsi_emit.h"
-#include "svga_context.h"
-
-
 
 
 static boolean ps20_input( struct svga_shader_emitter *emit,
diff --git a/src/gallium/drivers/svga/svga_tgsi_decl_sm30.c b/src/gallium/drivers/svga/svga_tgsi_decl_sm30.c
index d1c7336..43fc0d3 100644
--- a/src/gallium/drivers/svga/svga_tgsi_decl_sm30.c
+++ b/src/gallium/drivers/svga/svga_tgsi_decl_sm30.c
@@ -29,7 +29,6 @@
 #include "util/u_memory.h"
 
 #include "svga_tgsi_emit.h"
-#include "svga_context.h"
 
 static boolean translate_vs_ps_semantic( struct tgsi_declaration_semantic semantic,
                                          unsigned *usage,
diff --git a/src/gallium/drivers/svga/svga_winsys.h b/src/gallium/drivers/svga/svga_winsys.h
index 59f299c..27b99fe 100644
--- a/src/gallium/drivers/svga/svga_winsys.h
+++ b/src/gallium/drivers/svga/svga_winsys.h
@@ -296,4 +296,10 @@
 				struct pipe_buffer **buffer,
 				unsigned *stride);
 
+struct pipe_texture *
+svga_screen_texture_wrap_surface(struct pipe_screen *screen,
+				 struct pipe_texture *base,
+				 enum SVGA3dSurfaceFormat format,
+				 struct svga_winsys_surface *srf);
+
 #endif /* SVGA_WINSYS_H_ */
diff --git a/src/gallium/drivers/trace/tr_drm.c b/src/gallium/drivers/trace/tr_drm.c
index 48d1c40..e7ca3a8 100644
--- a/src/gallium/drivers/trace/tr_drm.c
+++ b/src/gallium/drivers/trace/tr_drm.c
@@ -173,6 +173,7 @@
    if (!tr_api)
       goto error;
 
+   tr_api->base.driver_name = api->driver_name;
    tr_api->base.create_screen = trace_drm_create_screen;
    tr_api->base.create_context = trace_drm_create_context;
    tr_api->base.texture_from_shared_handle = trace_drm_texture_from_shared_handle;
diff --git a/src/gallium/include/pipe/p_inlines.h b/src/gallium/include/pipe/p_inlines.h
index 5fbd62a..72f5c1d 100644
--- a/src/gallium/include/pipe/p_inlines.h
+++ b/src/gallium/include/pipe/p_inlines.h
@@ -63,13 +63,6 @@
    if(screen->buffer_map_range) {
       unsigned offset = 0;
       unsigned length = buf->size;
-
-      /* XXX: Actually we should be using/detecting DISCARD
-       * instead of assuming that WRITE implies discard */
-      if((usage & PIPE_BUFFER_USAGE_CPU_WRITE) &&
-         !(usage & PIPE_BUFFER_USAGE_DISCARD))
-         usage |= PIPE_BUFFER_USAGE_CPU_READ;
-
       return screen->buffer_map_range(screen, buf, offset, length, usage);
    }
    else
@@ -126,7 +119,39 @@
 
    map = pipe_buffer_map_range(screen, buf, offset, size, 
                                PIPE_BUFFER_USAGE_CPU_WRITE | 
-                               PIPE_BUFFER_USAGE_FLUSH_EXPLICIT);
+                               PIPE_BUFFER_USAGE_FLUSH_EXPLICIT |
+                               PIPE_BUFFER_USAGE_DISCARD);
+   assert(map);
+   if(map) {
+      memcpy((uint8_t *)map + offset, data, size);
+      pipe_buffer_flush_mapped_range(screen, buf, offset, size);
+      pipe_buffer_unmap(screen, buf);
+   }
+}
+
+/**
+ * Special case for writing non-overlapping ranges.
+ *
+ * We can avoid GPU/CPU synchronization when writing range that has never
+ * been written before.
+ */
+static INLINE void
+pipe_buffer_write_nooverlap(struct pipe_screen *screen,
+                            struct pipe_buffer *buf,
+                            unsigned offset, unsigned size,
+                            const void *data)
+{
+   void *map;
+
+   assert(offset < buf->size);
+   assert(offset + size <= buf->size);
+   assert(size);
+
+   map = pipe_buffer_map_range(screen, buf, offset, size,
+                               PIPE_BUFFER_USAGE_CPU_WRITE |
+                               PIPE_BUFFER_USAGE_FLUSH_EXPLICIT |
+                               PIPE_BUFFER_USAGE_DISCARD |
+                               PIPE_BUFFER_USAGE_UNSYNCHRONIZED);
    assert(map);
    if(map) {
       memcpy((uint8_t *)map + offset, data, size);
diff --git a/src/gallium/include/state_tracker/drm_api.h b/src/gallium/include/state_tracker/drm_api.h
index bb92892..b248a81 100644
--- a/src/gallium/include/state_tracker/drm_api.h
+++ b/src/gallium/include/state_tracker/drm_api.h
@@ -31,6 +31,11 @@
         const char *name;
 
 	/**
+	 * Kernel driver name, as accepted by drmOpenByName.
+	 */
+	const char *driver_name;
+
+	/**
 	 * Special buffer functions
 	 */
 	/*@{*/
diff --git a/src/gallium/state_trackers/dri/dri_context.c b/src/gallium/state_trackers/dri/dri_context.c
index f2e5f3f..07f0554 100644
--- a/src/gallium/state_trackers/dri/dri_context.c
+++ b/src/gallium/state_trackers/dri/dri_context.c
@@ -101,6 +101,12 @@
 {
    struct dri_context *ctx = dri_context(cPriv);
 
+   /* note: we are freeing values and nothing more because
+    * driParseConfigFiles allocated values only - the rest
+    * is owned by screen optionCache.
+    */
+   FREE(ctx->optionCache.values);
+
    /* No particular reason to wait for command completion before
     * destroying a context, but it is probably worthwhile flushing it
     * to avoid having to add code elsewhere to cope with flushing a
diff --git a/src/gallium/state_trackers/dri/dri_drawable.c b/src/gallium/state_trackers/dri/dri_drawable.c
index 0fdfa96..28fd8de 100644
--- a/src/gallium/state_trackers/dri/dri_drawable.c
+++ b/src/gallium/state_trackers/dri/dri_drawable.c
@@ -123,11 +123,12 @@
 
    struct dri_drawable *drawable = dri_drawable(dPriv);
    struct pipe_surface *surface = NULL;
-   struct pipe_screen *screen = dri_screen(drawable->sPriv)->pipe_screen;
+   struct dri_screen *st_screen = dri_screen(drawable->sPriv);
+   struct pipe_screen *screen = st_screen->pipe_screen;
    __DRIbuffer *buffers = NULL;
    __DRIscreen *dri_screen = drawable->sPriv;
    __DRIdrawable *dri_drawable = drawable->dPriv;
-   struct drm_api *api = ((struct dri_screen*)(dri_screen->private))->api;
+   struct drm_api *api = st_screen->api;
    boolean have_depth = FALSE;
    int i, count;
 
@@ -180,7 +181,9 @@
 
       switch (buffers[i].attachment) {
       case __DRI_BUFFER_FRONT_LEFT:
-	 continue;
+	 if (!st_screen->auto_fake_front)
+	    continue;
+	 /* fallthrough */
       case __DRI_BUFFER_FAKE_FRONT_LEFT:
 	 index = ST_SURFACE_FRONT_LEFT;
 	 format = drawable->color_format;
@@ -373,8 +376,8 @@
    /* TODO incase of double buffer visual, delay fake creation */
    i = 0;
    drawable->attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
-   drawable->attachments[i++] = __DRI_BUFFER_FAKE_FRONT_LEFT;
-
+   if (!screen->auto_fake_front)
+      drawable->attachments[i++] = __DRI_BUFFER_FAKE_FRONT_LEFT;
    if (visual->doubleBufferMode)
       drawable->attachments[i++] = __DRI_BUFFER_BACK_LEFT;
    if (visual->depthBits && visual->stencilBits)
diff --git a/src/gallium/state_trackers/dri/dri_screen.c b/src/gallium/state_trackers/dri/dri_screen.c
index d8c0543..cdc8eb1 100644
--- a/src/gallium/state_trackers/dri/dri_screen.c
+++ b/src/gallium/state_trackers/dri/dri_screen.c
@@ -292,6 +292,8 @@
 {
    struct dri_screen *screen;
    struct drm_create_screen_arg arg;
+   const __DRIdri2LoaderExtension *dri2_ext =
+     sPriv->dri2.loader;
 
    screen = CALLOC_STRUCT(dri_screen);
    if (!screen)
@@ -317,6 +319,9 @@
    driParseOptionInfo(&screen->optionCache,
 		      __driConfigOptions, __driNConfigOptions);
 
+   screen->auto_fake_front = dri2_ext->base.version >= 3 &&
+      dri2_ext->getBuffersWithFormat != NULL;
+
    return dri_fill_in_modes(screen, 32);
  fail:
    return NULL;
@@ -326,8 +331,18 @@
 dri_destroy_screen(__DRIscreen * sPriv)
 {
    struct dri_screen *screen = dri_screen(sPriv);
+   int i;
 
    screen->pipe_screen->destroy(screen->pipe_screen);
+   
+   for (i = 0; i < (1 << screen->optionCache.tableSize); ++i) {
+      FREE(screen->optionCache.info[i].name);
+      FREE(screen->optionCache.info[i].ranges);
+   }
+
+   FREE(screen->optionCache.info);
+   FREE(screen->optionCache.values);
+
    FREE(screen);
    sPriv->private = NULL;
 }
diff --git a/src/gallium/state_trackers/dri/dri_screen.h b/src/gallium/state_trackers/dri/dri_screen.h
index 03387a0..75a0ee4 100644
--- a/src/gallium/state_trackers/dri/dri_screen.h
+++ b/src/gallium/state_trackers/dri/dri_screen.h
@@ -59,6 +59,7 @@
    struct pipe_screen *pipe_screen;
    boolean d_depth_bits_last;
    boolean sd_depth_bits_last;
+   boolean auto_fake_front;
 };
 
 /** cast wrapper */
diff --git a/src/gallium/state_trackers/egl/Makefile b/src/gallium/state_trackers/egl/Makefile
index e825aa7..b696f2f 100644
--- a/src/gallium/state_trackers/egl/Makefile
+++ b/src/gallium/state_trackers/egl/Makefile
@@ -1,19 +1,75 @@
 TOP = ../../../..
 include $(TOP)/configs/current
 
-LIBNAME = egldrm
-
-LIBRARY_INCLUDES = \
+common_INCLUDES = \
+	-I. \
 	-I$(TOP)/src/gallium/include \
 	-I$(TOP)/src/gallium/auxiliary \
-	-I$(TOP)/src/mesa/drivers/dri/common \
-	-I$(TOP)/src/mesa \
-	-I$(TOP)/include \
 	-I$(TOP)/src/egl/main \
+	-I$(TOP)/include
+
+common_SOURCES = $(wildcard common/*.c)
+common_OBJECTS = $(common_SOURCES:.c=.o)
+
+
+x11_INCLUDES = \
+	-I$(TOP)/src/gallium/drivers \
+	-I$(TOP)/src/glx/x11 \
+	-I$(TOP)/src/egl/drivers/xdri \
+	-I$(TOP)/src/mesa \
 	$(shell pkg-config --cflags-only-I libdrm)
 
-
-C_SOURCES = $(wildcard ./*.c)
+x11_SOURCES = $(wildcard x11/*.c) \
+	      $(TOP)/src/glx/x11/dri2.c \
+	      $(TOP)/src/egl/drivers/xdri/glxinit.c
+x11_OBJECTS = $(x11_SOURCES:.c=.o)
 
 
-include ../../Makefile.template
+kms_INCLUDES = $(shell pkg-config --cflags-only-I libdrm)
+kms_SOURCES = $(wildcard kms/*.c)
+kms_OBJECTS = $(kms_SOURCES:.c=.o)
+
+
+ALL_INCLUDES = $(common_INCLUDES) $(x11_INCLUDES) $(kms_INCLUDES)
+ALL_SOURCES = $(common_SOURCES) $(x11_SOURCES) $(kms_SOURCES)
+ALL_OBJECTS = $(common_OBJECTS) $(x11_OBJECTS) $(kms_OBJECTS)
+
+##### TARGETS #####
+
+EGL_DISPLAYS_MODS = $(foreach dpy, $(EGL_DISPLAYS), libegl$(dpy).a)
+
+default: depend $(EGL_DISPLAYS_MODS)
+
+
+libeglx11.a: $(x11_OBJECTS) $(common_OBJECTS) Makefile
+	$(MKLIB) -o eglx11 -static $(x11_OBJECTS) $(common_OBJECTS)
+
+libeglkms.a: $(kms_OBJECTS) $(common_OBJECTS) Makefile
+	$(MKLIB) -o eglkms -static $(kms_OBJECTS) $(common_OBJECTS)
+
+depend: 
+	rm -f depend
+	touch depend
+	$(MKDEP) $(MKDEP_OPTIONS) $(ALL_INCLUDES) $(ALL_SOURCES) 2> /dev/null
+
+clean:
+	rm -f $(ALL_OBJECTS)
+	rm -f $(EGL_DISPLAYS_MODS)
+	rm -f depend depend.bak
+
+# Dummy target
+install:
+	@echo -n ""
+
+##### RULES #####
+
+$(common_OBJECTS): %.o: %.c
+	$(CC) -c $(common_INCLUDES) $(DEFINES) $(CFLAGS) $< -o $@
+
+$(x11_OBJECTS): %.o: %.c
+	$(CC) -c $(common_INCLUDES) $(x11_INCLUDES) $(DEFINES) $(CFLAGS) $< -o $@
+
+$(kms_OBJECTS): %.o: %.c
+	$(CC) -c $(common_INCLUDES) $(kms_INCLUDES) $(DEFINES) $(CFLAGS) $< -o $@
+
+sinclude depend
diff --git a/src/gallium/state_trackers/egl_g3d/common/egl_g3d.c b/src/gallium/state_trackers/egl/common/egl_g3d.c
similarity index 100%
rename from src/gallium/state_trackers/egl_g3d/common/egl_g3d.c
rename to src/gallium/state_trackers/egl/common/egl_g3d.c
diff --git a/src/gallium/state_trackers/egl_g3d/common/egl_g3d.h b/src/gallium/state_trackers/egl/common/egl_g3d.h
similarity index 100%
rename from src/gallium/state_trackers/egl_g3d/common/egl_g3d.h
rename to src/gallium/state_trackers/egl/common/egl_g3d.h
diff --git a/src/gallium/state_trackers/egl_g3d/common/egl_st.c b/src/gallium/state_trackers/egl/common/egl_st.c
similarity index 100%
rename from src/gallium/state_trackers/egl_g3d/common/egl_st.c
rename to src/gallium/state_trackers/egl/common/egl_st.c
diff --git a/src/gallium/state_trackers/egl_g3d/common/egl_st.h b/src/gallium/state_trackers/egl/common/egl_st.h
similarity index 100%
rename from src/gallium/state_trackers/egl_g3d/common/egl_st.h
rename to src/gallium/state_trackers/egl/common/egl_st.h
diff --git a/src/gallium/state_trackers/egl_g3d/common/native.h b/src/gallium/state_trackers/egl/common/native.h
similarity index 100%
rename from src/gallium/state_trackers/egl_g3d/common/native.h
rename to src/gallium/state_trackers/egl/common/native.h
diff --git a/src/gallium/state_trackers/egl_g3d/common/st_public_tmp.h b/src/gallium/state_trackers/egl/common/st_public_tmp.h
similarity index 100%
rename from src/gallium/state_trackers/egl_g3d/common/st_public_tmp.h
rename to src/gallium/state_trackers/egl/common/st_public_tmp.h
diff --git a/src/gallium/state_trackers/egl/egl_context.c b/src/gallium/state_trackers/egl/egl_context.c
deleted file mode 100644
index fee186c..0000000
--- a/src/gallium/state_trackers/egl/egl_context.c
+++ /dev/null
@@ -1,105 +0,0 @@
-
-#include "utils.h"
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include "egl_tracker.h"
-
-#include "egllog.h"
-
-
-#include "pipe/p_context.h"
-#include "pipe/p_screen.h"
-
-#include "state_tracker/st_public.h"
-#include "state_tracker/drm_api.h"
-
-#include "GL/internal/glcore.h"
-
-_EGLContext *
-drm_create_context(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf, _EGLContext *share_list, const EGLint *attrib_list)
-{
-	struct drm_device *dev = lookup_drm_device(dpy);
-	struct drm_context *ctx;
-	struct drm_context *share = NULL;
-	struct st_context *st_share = NULL;
-	int i;
-	__GLcontextModes *visual;
-
-	for (i = 0; attrib_list && attrib_list[i] != EGL_NONE; i++) {
-		switch (attrib_list[i]) {
-			/* no attribs defined for now */
-			default:
-				_eglError(EGL_BAD_ATTRIBUTE, "eglCreateContext");
-				return EGL_NO_CONTEXT;
-		}
-	}
-
-	ctx = (struct drm_context *) calloc(1, sizeof(struct drm_context));
-	if (!ctx)
-		goto err_c;
-
-	_eglInitContext(drv, &ctx->base, conf, attrib_list);
-
-	ctx->pipe = dev->api->create_context(dev->api, dev->screen);
-	if (!ctx->pipe)
-		goto err_pipe;
-
-	if (share)
-		st_share = share->st;
-
-	visual = drm_visual_from_config(conf);
-	ctx->st = st_create_context(ctx->pipe, visual, st_share);
-	drm_visual_modes_destroy(visual);
-
-	if (!ctx->st)
-		goto err_gl;
-
-	return &ctx->base;
-
-err_gl:
-	ctx->pipe->destroy(ctx->pipe);
-err_pipe:
-	free(ctx);
-err_c:
-	return NULL;
-}
-
-EGLBoolean
-drm_destroy_context(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *context)
-{
-	struct drm_context *c = lookup_drm_context(context);
-	if (!_eglIsContextBound(&c->base)) {
-		st_destroy_context(c->st);
-		free(c);
-	}
-	return EGL_TRUE;
-}
-
-EGLBoolean
-drm_make_current(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *draw, _EGLSurface *read, _EGLContext *context)
-{
-	struct drm_surface *readSurf = lookup_drm_surface(read);
-	struct drm_surface *drawSurf = lookup_drm_surface(draw);
-	struct drm_context *ctx = lookup_drm_context(context);
-	EGLBoolean b;
-
-	b = _eglMakeCurrent(drv, dpy, draw, read, context);
-	if (!b)
-		return EGL_FALSE;
-
-	if (ctx) {
-		if (!drawSurf || !readSurf)
-			return EGL_FALSE;
-
-		st_make_current(ctx->st, drawSurf->stfb, readSurf->stfb);
-
-		/* st_resize_framebuffer needs a bound context to work */
-		st_resize_framebuffer(drawSurf->stfb, drawSurf->w, drawSurf->h);
-		st_resize_framebuffer(readSurf->stfb, readSurf->w, readSurf->h);
-	} else {
-		st_make_current(NULL, NULL, NULL);
-	}
-
-	return EGL_TRUE;
-}
diff --git a/src/gallium/state_trackers/egl/egl_surface.c b/src/gallium/state_trackers/egl/egl_surface.c
deleted file mode 100644
index d55aa51..0000000
--- a/src/gallium/state_trackers/egl/egl_surface.c
+++ /dev/null
@@ -1,443 +0,0 @@
-
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include "egl_tracker.h"
-
-#include "egllog.h"
-
-#include "pipe/p_inlines.h"
-#include "pipe/p_screen.h"
-#include "pipe/p_context.h"
-
-#include "state_tracker/drm_api.h"
-
-#include "util/u_format.h"
-#include "util/u_rect.h"
-
-/*
- * Util functions
- */
-
-static drmModeModeInfoPtr
-drm_find_mode(drmModeConnectorPtr connector, _EGLMode *mode)
-{
-	int i;
-	drmModeModeInfoPtr m = NULL;
-
-	for (i = 0; i < connector->count_modes; i++) {
-		m = &connector->modes[i];
-		if (m->hdisplay == mode->Width && m->vdisplay == mode->Height && m->vrefresh == mode->RefreshRate)
-			break;
-		m = &connector->modes[0]; /* if we can't find one, return first */
-	}
-
-	return m;
-}
-
-static struct st_framebuffer *
-drm_create_framebuffer(struct pipe_screen *screen,
-                       const __GLcontextModes *visual,
-                       unsigned width,
-                       unsigned height,
-                       void *priv)
-{
-	enum pipe_format color_format, depth_stencil_format;
-	boolean d_depth_bits_last;
-	boolean ds_depth_bits_last;
-
-	d_depth_bits_last =
-		screen->is_format_supported(screen, PIPE_FORMAT_X8Z24_UNORM,
-		                            PIPE_TEXTURE_2D,
-		                            PIPE_TEXTURE_USAGE_DEPTH_STENCIL, 0);
-	ds_depth_bits_last =
-		screen->is_format_supported(screen, PIPE_FORMAT_S8Z24_UNORM,
-		                            PIPE_TEXTURE_2D,
-		                            PIPE_TEXTURE_USAGE_DEPTH_STENCIL, 0);
-
-	if (visual->redBits == 8) {
-		if (visual->alphaBits == 8)
-			color_format = PIPE_FORMAT_A8R8G8B8_UNORM;
-		else
-			color_format = PIPE_FORMAT_X8R8G8B8_UNORM;
-	} else {
-		color_format = PIPE_FORMAT_R5G6B5_UNORM;
-	}
-
-	switch(visual->depthBits) {
-		default:
-		case 0:
-			depth_stencil_format = PIPE_FORMAT_NONE;
-			break;
-		case 16:
-			depth_stencil_format = PIPE_FORMAT_Z16_UNORM;
-			break;
-		case 24:
-			if (visual->stencilBits == 0) {
-				depth_stencil_format = (d_depth_bits_last) ?
-					PIPE_FORMAT_X8Z24_UNORM:
-					PIPE_FORMAT_Z24X8_UNORM;
-			} else {
-				depth_stencil_format = (ds_depth_bits_last) ?
-					PIPE_FORMAT_S8Z24_UNORM:
-					PIPE_FORMAT_Z24S8_UNORM;
-			}
-			break;
-		case 32:
-			depth_stencil_format = PIPE_FORMAT_Z32_UNORM;
-			break;
-	}
-
-	return st_create_framebuffer(visual,
-	                             color_format,
-	                             depth_stencil_format,
-	                             depth_stencil_format,
-	                             width,
-	                             height,
-	                             priv);
-}
-
-static void
-drm_create_texture(_EGLDisplay *dpy,
-                   struct drm_screen *scrn,
-                   unsigned w, unsigned h)
-{
-	struct drm_device *dev = lookup_drm_device(dpy);
-	struct pipe_screen *screen = dev->screen;
-	struct pipe_surface *surface;
-	struct pipe_texture *texture;
-	struct pipe_texture templat;
-	struct pipe_buffer *buf = NULL;
-	unsigned pitch = 0;
-
-	memset(&templat, 0, sizeof(templat));
-	templat.tex_usage = PIPE_TEXTURE_USAGE_RENDER_TARGET;
-	templat.tex_usage |= PIPE_TEXTURE_USAGE_PRIMARY;
-	templat.target = PIPE_TEXTURE_2D;
-	templat.last_level = 0;
-	templat.depth0 = 1;
-	templat.format = PIPE_FORMAT_A8R8G8B8_UNORM;
-	templat.width0 = w;
-	templat.height0 = h;
-
-	texture = screen->texture_create(dev->screen,
-	                                 &templat);
-
-	if (!texture)
-		goto err_tex;
-
-	surface = screen->get_tex_surface(screen,
-	                                  texture,
-	                                  0,
-	                                  0,
-	                                  0,
-	                                  PIPE_BUFFER_USAGE_GPU_WRITE);
-
-	if (!surface)
-		goto err_surf;
-
-	scrn->tex = texture;
-	scrn->surface = surface;
-	scrn->front.width = w;
-	scrn->front.height = h;
-	scrn->front.pitch = pitch;
-	dev->api->local_handle_from_texture(dev->api, screen, texture,
-	                                    &scrn->front.pitch, &scrn->front.handle);
-	if (0)
-		goto err_handle;
-
-	return;
-
-err_handle:
-	pipe_surface_reference(&surface, NULL);
-err_surf:
-	pipe_texture_reference(&texture, NULL);
-err_tex:
-	pipe_buffer_reference(&buf, NULL);
-	return;
-}
-
-/*
- * Exported functions
- */
-
-void
-drm_takedown_shown_screen(_EGLDisplay *dpy, struct drm_screen *screen)
-{
-	struct drm_device *dev = lookup_drm_device(dpy);
-
-	screen->surf = NULL;
-
-	drmModeSetCrtc(
-		dev->drmFD,
-		screen->crtcID,
-		0, /* FD */
-		0, 0,
-		NULL, 0, /* List of output ids */
-		NULL);
-
-	drmModeRmFB(dev->drmFD, screen->fbID);
-	drmModeFreeFB(screen->fb);
-	screen->fb = NULL;
-
-	pipe_surface_reference(&screen->surface, NULL);
-	pipe_texture_reference(&screen->tex, NULL);
-
-	screen->shown = 0;
-}
-
-/**
- * Called by libEGL's eglCreateWindowSurface().
- */
-_EGLSurface *
-drm_create_window_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf, NativeWindowType window, const EGLint *attrib_list)
-{
-	return NULL;
-}
-
-
-/**
- * Called by libEGL's eglCreatePixmapSurface().
- */
-_EGLSurface *
-drm_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf, NativePixmapType pixmap, const EGLint *attrib_list)
-{
-	return NULL;
-}
-
-
-/**
- * Called by libEGL's eglCreatePbufferSurface().
- */
-_EGLSurface *
-drm_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf,
-                           const EGLint *attrib_list)
-{
-	struct drm_device *dev = lookup_drm_device(dpy);
-	int i;
-	int width = -1;
-	int height = -1;
-	struct drm_surface *surf = NULL;
-	__GLcontextModes *visual;
-
-	for (i = 0; attrib_list && attrib_list[i] != EGL_NONE; i++) {
-		switch (attrib_list[i]) {
-			case EGL_WIDTH:
-				width = attrib_list[++i];
-				break;
-			case EGL_HEIGHT:
-				height = attrib_list[++i];
-				break;
-			default:
-				_eglError(EGL_BAD_ATTRIBUTE, "eglCreatePbufferSurface");
-				return EGL_NO_SURFACE;
-		}
-	}
-
-	if (width < 1 || height < 1) {
-		_eglError(EGL_BAD_ATTRIBUTE, "eglCreatePbufferSurface");
-		return NULL;
-	}
-
-	surf = (struct drm_surface *) calloc(1, sizeof(struct drm_surface));
-	if (!surf)
-		goto err;
-
-	if (!_eglInitSurface(drv, &surf->base, EGL_PBUFFER_BIT, conf, attrib_list))
-		goto err_surf;
-
-	surf->w = width;
-	surf->h = height;
-
-	visual = drm_visual_from_config(conf);
-	surf->stfb = drm_create_framebuffer(dev->screen, visual,
-	                                    width, height,
-	                                    (void*)surf);
-	drm_visual_modes_destroy(visual);
-
-	return &surf->base;
-
-err_surf:
-	free(surf);
-err:
-	return NULL;
-}
-
-/**
- * Called by libEGL's eglCreateScreenSurfaceMESA().
- */
-_EGLSurface *
-drm_create_screen_surface_mesa(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *cfg,
-                               const EGLint *attrib_list)
-{
-	EGLSurface surf = drm_create_pbuffer_surface(drv, dpy, cfg, attrib_list);
-
-	return surf;
-}
-
-/**
- * Called by libEGL's eglShowScreenSurfaceMESA().
- */
-EGLBoolean
-drm_show_screen_surface_mesa(_EGLDriver *drv, _EGLDisplay *dpy,
-                             _EGLScreen *screen,
-                             _EGLSurface *surface, _EGLMode *mode)
-{
-	struct drm_device *dev = lookup_drm_device(dpy);
-	struct drm_surface *surf = lookup_drm_surface(surface);
-	struct drm_screen *scrn = lookup_drm_screen(screen);
-	int ret;
-	unsigned int i, k;
-
-	if (scrn->shown)
-		drm_takedown_shown_screen(dpy, scrn);
-
-
-	drm_create_texture(dpy, scrn, mode->Width, mode->Height);
-	if (!scrn->tex)
-		goto err_tex;
-
-	ret = drmModeAddFB(dev->drmFD,
-	                   scrn->front.width, scrn->front.height,
-	                   32, 32, scrn->front.pitch,
-	                   scrn->front.handle,
-	                   &scrn->fbID);
-
-	if (ret)
-		goto err_bo;
-
-	scrn->fb = drmModeGetFB(dev->drmFD, scrn->fbID);
-	if (!scrn->fb)
-		goto err_bo;
-
-	/* find a fitting crtc */
-	{
-		drmModeConnector *con = scrn->connector;
-
-		scrn->mode = drm_find_mode(con, mode);
-		if (!scrn->mode)
-			goto err_fb;
-
-		for (k = 0; k < con->count_encoders; k++) {
-			drmModeEncoder *enc = drmModeGetEncoder(dev->drmFD, con->encoders[k]);
-			for (i = 0; i < dev->res->count_crtcs; i++) {
-				if (enc->possible_crtcs & (1<<i)) {
-					/* save the ID */
-					scrn->crtcID = dev->res->crtcs[i];
-
-					/* skip the rest */
-					i = dev->res->count_crtcs;
-					k = dev->res->count_encoders;
-				}
-			}
-			drmModeFreeEncoder(enc);
-		}
-	}
-
-	ret = drmModeSetCrtc(dev->drmFD,
-	                     scrn->crtcID,
-	                     scrn->fbID,
-	                     0, 0,
-	                     &scrn->connectorID, 1,
-	                     scrn->mode);
-
-	if (ret)
-		goto err_crtc;
-
-
-	if (scrn->dpms)
-		drmModeConnectorSetProperty(dev->drmFD,
-		                            scrn->connectorID,
-		                            scrn->dpms->prop_id,
-		                            DRM_MODE_DPMS_ON);
-
-	surf->screen = scrn;
-
-	scrn->surf = surf;
-	scrn->shown = 1;
-
-	return EGL_TRUE;
-
-err_crtc:
-	scrn->crtcID = 0;
-
-err_fb:
-	drmModeRmFB(dev->drmFD, scrn->fbID);
-	drmModeFreeFB(scrn->fb);
-	scrn->fb = NULL;
-
-err_bo:
-	pipe_surface_reference(&scrn->surface, NULL);
-	pipe_texture_reference(&scrn->tex, NULL);
-
-err_tex:
-	return EGL_FALSE;
-}
-
-/**
- * Called by libEGL's eglDestroySurface().
- */
-EGLBoolean
-drm_destroy_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surface)
-{
-	struct drm_surface *surf = lookup_drm_surface(surface);
-	if (!_eglIsSurfaceBound(&surf->base)) {
-		if (surf->screen)
-			drm_takedown_shown_screen(dpy, surf->screen);
-		st_unreference_framebuffer(surf->stfb);
-		free(surf);
-	}
-	return EGL_TRUE;
-}
-
-/**
- * Called by libEGL's eglSwapBuffers().
- */
-EGLBoolean
-drm_swap_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *draw)
-{
-	struct drm_device *dev = lookup_drm_device(dpy);
-	struct drm_surface *surf = lookup_drm_surface(draw);
-	struct pipe_surface *back_surf;
-
-	if (!surf)
-		return EGL_FALSE;
-
-	st_get_framebuffer_surface(surf->stfb, ST_SURFACE_BACK_LEFT, &back_surf);
-
-	if (back_surf) {
-		struct drm_context *ctx = lookup_drm_context(draw->Binding);
-
-		st_notify_swapbuffers(surf->stfb);
-
-		if (ctx && surf->screen) {
-            if (ctx->pipe->surface_copy) {
-                ctx->pipe->surface_copy(ctx->pipe,
-                    surf->screen->surface,
-                    0, 0,
-                    back_surf,
-                    0, 0,
-                    surf->w, surf->h);
-            } else {
-                util_surface_copy(ctx->pipe, FALSE,
-                    surf->screen->surface,
-                    0, 0,
-                    back_surf,
-                    0, 0,
-                    surf->w, surf->h);
-            }
-			ctx->pipe->flush(ctx->pipe, PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_TEXTURE_CACHE, NULL);
-
-#ifdef DRM_MODE_FEATURE_DIRTYFB
-			/* TODO query connector property to see if this is needed */
-			drmModeDirtyFB(dev->drmFD, surf->screen->fbID, NULL, 0);
-#else
-			(void)dev;
-#endif
-
-			/* TODO more stuff here */
-		}
-	}
-
-	return EGL_TRUE;
-}
diff --git a/src/gallium/state_trackers/egl/egl_tracker.c b/src/gallium/state_trackers/egl/egl_tracker.c
deleted file mode 100644
index 11583ec..0000000
--- a/src/gallium/state_trackers/egl/egl_tracker.c
+++ /dev/null
@@ -1,272 +0,0 @@
-
-#include "utils.h"
-
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include "egl_tracker.h"
-
-#include <fcntl.h>
-
-#include "egllog.h"
-#include "state_tracker/drm_api.h"
-
-#include "pipe/p_screen.h"
-#include "pipe/internal/p_winsys_screen.h"
-
-/** HACK */
-void* driDriverAPI;
-
-
-/*
- * Exported functions
- */
-
-/** Called by libEGL just prior to unloading/closing the driver.
- */
-static void
-drm_unload(_EGLDriver *drv)
-{
-	free(drv);
-}
-
-/**
- * The bootstrap function.  Return a new drm_driver object and
- * plug in API functions.
- * libEGL finds this function with dlopen()/dlsym() and calls it from
- * "load driver" function.
- */
-_EGLDriver *
-_eglMain(const char *args)
-{
-	_EGLDriver *drv;
-
-	drv = (_EGLDriver *) calloc(1, sizeof(_EGLDriver));
-	if (!drv) {
-		return NULL;
-	}
-
-	/* First fill in the dispatch table with defaults */
-	_eglInitDriverFallbacks(drv);
-	/* then plug in our Drm-specific functions */
-	drv->API.Initialize = drm_initialize;
-	drv->API.Terminate = drm_terminate;
-	drv->API.CreateContext = drm_create_context;
-	drv->API.MakeCurrent = drm_make_current;
-	drv->API.CreateWindowSurface = drm_create_window_surface;
-	drv->API.CreatePixmapSurface = drm_create_pixmap_surface;
-	drv->API.CreatePbufferSurface = drm_create_pbuffer_surface;
-	drv->API.DestroySurface = drm_destroy_surface;
-	drv->API.DestroyContext = drm_destroy_context;
-	drv->API.CreateScreenSurfaceMESA = drm_create_screen_surface_mesa;
-	drv->API.ShowScreenSurfaceMESA = drm_show_screen_surface_mesa;
-	drv->API.SwapBuffers = drm_swap_buffers;
-
-	drv->Name = "DRM/Gallium/Win";
-	drv->Unload = drm_unload;
-
-	return drv;
-}
-
-static void
-drm_get_device_id(struct drm_device *device)
-{
-	char path[512];
-	FILE *file;
-	char *ret;
-
-	/* TODO get the real minor */
-	int minor = 0;
-
-	device->deviceID = 0;
-
-	snprintf(path, sizeof(path), "/sys/class/drm/card%d/device/device", minor);
-	file = fopen(path, "r");
-	if (!file) {
-		_eglLog(_EGL_WARNING, "Could not retrive device ID\n");
-		return;
-	}
-
-	ret = fgets(path, sizeof( path ), file);
-	fclose(file);
-	if (!ret)
-		return;
-
-	sscanf(path, "%x", &device->deviceID);
-}
-
-static void
-drm_update_res(struct drm_device *dev)
-{
-	drmModeFreeResources(dev->res);
-	dev->res = drmModeGetResources(dev->drmFD);
-}
-
-static void
-drm_add_modes_from_connector(_EGLScreen *screen, drmModeConnectorPtr connector)
-{
-	drmModeModeInfoPtr m = NULL;
-	int i;
-
-	for (i = 0; i < connector->count_modes; i++) {
-		m = &connector->modes[i];
-		_eglAddNewMode(screen, m->hdisplay, m->vdisplay, m->vrefresh, m->name);
-	}
-}
-
-static void
-drm_find_dpms(struct drm_device *dev, struct drm_screen *screen)
-{
-	drmModeConnectorPtr c = screen->connector;
-	drmModePropertyPtr p;
-	int i;
-
-	for (i = 0; i < c->count_props; i++) {
-		p = drmModeGetProperty(dev->drmFD, c->props[i]);
-		if (!strcmp(p->name, "DPMS"))
-			break;
-
-		drmModeFreeProperty(p);
-		p = NULL;
-	}
-
-	screen->dpms = p;
-}
-
-static int drm_open_minor(int minor)
-{
-	char buf[64];
-
-	sprintf(buf, DRM_DEV_NAME, DRM_DIR_NAME, minor);
-	return open(buf, O_RDWR, 0);
-}
-
-EGLBoolean
-drm_initialize(_EGLDriver *drv, _EGLDisplay *disp, EGLint *major, EGLint *minor)
-{
-	struct drm_device *dev;
-	struct drm_screen *screen = NULL;
-	drmModeConnectorPtr connector = NULL;
-	drmModeResPtr res = NULL;
-	unsigned count_connectors = 0;
-	int num_screens = 0;
-	EGLint i;
-	int fd;
-	_EGLConfig *config;
-
-	dev = (struct drm_device *) calloc(1, sizeof(struct drm_device));
-	if (!dev)
-		return EGL_FALSE;
-	dev->api = drm_api_create();
-
-	/* try the first node */
-	fd = drm_open_minor(0);
-	if (fd < 0)
-		goto err_fd;
-
-	dev->drmFD = fd;
-	drm_get_device_id(dev);
-
-	dev->screen = dev->api->create_screen(dev->api, dev->drmFD, NULL);
-	if (!dev->screen)
-		goto err_screen;
-	dev->winsys = dev->screen->winsys;
-
-	drm_update_res(dev);
-	res = dev->res;
-	if (res)
-		count_connectors = res->count_connectors;
-	else
-		_eglLog(_EGL_WARNING, "Could not retrive kms information\n");
-
-	for(i = 0; i < count_connectors && i < MAX_SCREENS; i++) {
-		connector = drmModeGetConnector(fd, res->connectors[i]);
-
-		if (!connector)
-			continue;
-
-		if (connector->connection != DRM_MODE_CONNECTED) {
-			drmModeFreeConnector(connector);
-			continue;
-		}
-
-		screen = malloc(sizeof(struct drm_screen));
-		memset(screen, 0, sizeof(*screen));
-		screen->connector = connector;
-		screen->connectorID = connector->connector_id;
-		_eglInitScreen(&screen->base);
-		_eglAddScreen(disp, &screen->base);
-		drm_add_modes_from_connector(&screen->base, connector);
-		drm_find_dpms(dev, screen);
-		dev->screens[num_screens++] = screen;
-	}
-	dev->count_screens = num_screens;
-
-	disp->DriverData = dev;
-
-	/* for now we only have one config */
-	config = calloc(1, sizeof(*config));
-	memset(config, 1, sizeof(*config));
-	_eglInitConfig(config, 1);
-	_eglSetConfigAttrib(config, EGL_RED_SIZE, 8);
-	_eglSetConfigAttrib(config, EGL_GREEN_SIZE, 8);
-	_eglSetConfigAttrib(config, EGL_BLUE_SIZE, 8);
-	_eglSetConfigAttrib(config, EGL_ALPHA_SIZE, 8);
-	_eglSetConfigAttrib(config, EGL_BUFFER_SIZE, 32);
-	_eglSetConfigAttrib(config, EGL_DEPTH_SIZE, 24);
-	_eglSetConfigAttrib(config, EGL_STENCIL_SIZE, 8);
-	_eglSetConfigAttrib(config, EGL_SURFACE_TYPE, EGL_PBUFFER_BIT);
-	_eglAddConfig(disp, config);
-
-	disp->ClientAPIsMask = EGL_OPENGL_BIT /*| EGL_OPENGL_ES_BIT*/;
-	/* enable supported extensions */
-	disp->Extensions.MESA_screen_surface = EGL_TRUE;
-	disp->Extensions.MESA_copy_context = EGL_TRUE;
-
-	*major = 1;
-	*minor = 4;
-
-	return EGL_TRUE;
-
-err_screen:
-	drmClose(fd);
-err_fd:
-	free(dev);
-	return EGL_FALSE;
-}
-
-EGLBoolean
-drm_terminate(_EGLDriver *drv, _EGLDisplay *dpy)
-{
-	struct drm_device *dev = lookup_drm_device(dpy);
-	struct drm_screen *screen;
-	int i = 0;
-
-	_eglReleaseDisplayResources(drv, dpy);
-	_eglCleanupDisplay(dpy);
-
-	drmFreeVersion(dev->version);
-
-	for (i = 0; i < dev->count_screens; i++) {
-		screen = dev->screens[i];
-
-		if (screen->shown)
-			drm_takedown_shown_screen(dpy, screen);
-
-		drmModeFreeProperty(screen->dpms);
-		drmModeFreeConnector(screen->connector);
-		_eglDestroyScreen(&screen->base);
-		dev->screens[i] = NULL;
-	}
-
-	dev->screen->destroy(dev->screen);
-	dev->winsys = NULL;
-
-	drmClose(dev->drmFD);
-
-	dev->api->destroy(dev->api);
-	free(dev);
-	dpy->DriverData = NULL;
-
-	return EGL_TRUE;
-}
diff --git a/src/gallium/state_trackers/egl/egl_tracker.h b/src/gallium/state_trackers/egl/egl_tracker.h
deleted file mode 100644
index 73eb1a1..0000000
--- a/src/gallium/state_trackers/egl/egl_tracker.h
+++ /dev/null
@@ -1,195 +0,0 @@
-
-#ifndef _EGL_TRACKER_H_
-#define _EGL_TRACKER_H_
-
-#include <stdint.h>
-
-#include "eglconfig.h"
-#include "eglcontext.h"
-#include "egldisplay.h"
-#include "egldriver.h"
-#include "eglglobals.h"
-#include "eglmode.h"
-#include "eglscreen.h"
-#include "eglsurface.h"
-
-#include "xf86drm.h"
-#include "xf86drmMode.h"
-
-#include "pipe/p_compiler.h"
-
-#include "state_tracker/st_public.h"
-
-#define MAX_SCREENS 16
-
-struct pipe_winsys;
-struct pipe_screen;
-struct pipe_context;
-struct state_tracker;
-
-struct drm_screen;
-struct drm_context;
-
-struct drm_device
-{
-	/*
-	 * pipe
-	 */
-
-	struct drm_api *api;
-	struct pipe_winsys *winsys;
-	struct pipe_screen *screen;
-
-	/*
-	 * drm
-	 */
-
-	int drmFD;
-	drmVersionPtr version;
-	int deviceID;
-
-	drmModeResPtr res;
-
-	struct drm_screen *screens[MAX_SCREENS];
-	size_t count_screens;
-};
-
-struct drm_surface
-{
-	_EGLSurface base; /* base class/object */
-
-	/*
-	 * pipe
-	 */
-
-
-	struct st_framebuffer *stfb;
-
-	/*
-	 * drm
-	 */
-
-	struct drm_screen *screen;
-
-	int w;
-	int h;
-};
-
-struct drm_context
-{
-	_EGLContext base; /* base class/object */
-
-	/* pipe */
-
-	struct pipe_context *pipe;
-	struct st_context *st;
-};
-
-struct drm_screen
-{
-	_EGLScreen base;
-
-	/*
-	 * pipe
-	 */
-
-	struct pipe_texture *tex;
-	struct pipe_surface *surface;
-
-	/*
-	 * drm
-	 */
-
-	struct {
-		unsigned height;
-		unsigned width;
-		unsigned pitch;
-		unsigned handle;
-	} front;
-
-	/* currently only support one connector */
-	drmModeConnectorPtr connector;
-	uint32_t connectorID;
-
-	/* dpms property */
-	drmModePropertyPtr dpms;
-
-	/* Has this screen been shown */
-	int shown;
-
-	/* Surface that is currently attached to this screen */
-	struct drm_surface *surf;
-
-	/* framebuffer */
-	drmModeFBPtr fb;
-	uint32_t fbID;
-
-	/* crtc and mode used */
-	/*drmModeCrtcPtr crtc;*/
-	uint32_t crtcID;
-
-	drmModeModeInfoPtr mode;
-};
-
-
-static INLINE struct drm_device *
-lookup_drm_device(_EGLDisplay *d)
-{
-	return (struct drm_device *) d->DriverData;
-}
-
-
-static INLINE struct drm_context *
-lookup_drm_context(_EGLContext *c)
-{
-	return (struct drm_context *) c;
-}
-
-
-static INLINE struct drm_surface *
-lookup_drm_surface(_EGLSurface *s)
-{
-	return (struct drm_surface *) s;
-}
-
-static INLINE struct drm_screen *
-lookup_drm_screen(_EGLScreen *s)
-{
-	return (struct drm_screen *) s;
-}
-
-/**
- * egl_visual.h
- */
-/*@{*/
-void drm_visual_modes_destroy(__GLcontextModes *modes);
-__GLcontextModes* drm_visual_modes_create(unsigned count, size_t minimum_size);
-__GLcontextModes* drm_visual_from_config(_EGLConfig *conf);
-/*@}*/
-
-/**
- * egl_surface.h
- */
-/*@{*/
-void drm_takedown_shown_screen(_EGLDisplay *dpy, struct drm_screen *screen);
-/*@}*/
-
-/**
- * All function exported to the egl side.
- */
-/*@{*/
-EGLBoolean drm_initialize(_EGLDriver *drv, _EGLDisplay *dpy, EGLint *major, EGLint *minor);
-EGLBoolean drm_terminate(_EGLDriver *drv, _EGLDisplay *dpy);
-_EGLContext *drm_create_context(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf, _EGLContext *share_list, const EGLint *attrib_list);
-EGLBoolean drm_destroy_context(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *context);
-_EGLSurface *drm_create_window_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf, NativeWindowType window, const EGLint *attrib_list);
-_EGLSurface *drm_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf, NativePixmapType pixmap, const EGLint *attrib_list);
-_EGLSurface *drm_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf, const EGLint *attrib_list);
-_EGLSurface *drm_create_screen_surface_mesa(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf, const EGLint *attrib_list);
-EGLBoolean drm_show_screen_surface_mesa(_EGLDriver *drv, _EGLDisplay *dpy, _EGLScreen *screen, _EGLSurface *surface, _EGLMode *mode);
-EGLBoolean drm_destroy_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surface);
-EGLBoolean drm_make_current(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *draw, _EGLSurface *read, _EGLContext *context);
-EGLBoolean drm_swap_buffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *draw);
-/*@}*/
-
-#endif
diff --git a/src/gallium/state_trackers/egl/egl_visual.c b/src/gallium/state_trackers/egl/egl_visual.c
deleted file mode 100644
index e59f893..0000000
--- a/src/gallium/state_trackers/egl/egl_visual.c
+++ /dev/null
@@ -1,85 +0,0 @@
-
-#include "egl_tracker.h"
-
-#include "egllog.h"
-
-void
-drm_visual_modes_destroy(__GLcontextModes *modes)
-{
-   _eglLog(_EGL_DEBUG, "%s", __FUNCTION__);
-
-   while (modes) {
-      __GLcontextModes * const next = modes->next;
-      free(modes);
-      modes = next;
-   }
-}
-
-__GLcontextModes *
-drm_visual_modes_create(unsigned count, size_t minimum_size)
-{
-	/* This code copied from libGLX, and modified */
-	const size_t size = (minimum_size > sizeof(__GLcontextModes))
-		? minimum_size : sizeof(__GLcontextModes);
-	__GLcontextModes * head = NULL;
-	__GLcontextModes ** next;
-	unsigned   i;
-
-	_eglLog(_EGL_DEBUG, "%s %d %d", __FUNCTION__, count, minimum_size);
-
-	next = & head;
-	for (i = 0 ; i < count ; i++) {
-		*next = (__GLcontextModes *) calloc(1, size);
-		if (*next == NULL) {
-			drm_visual_modes_destroy(head);
-			head = NULL;
-			break;
-		}
-
-		(*next)->doubleBufferMode = 1;
-		(*next)->visualID = GLX_DONT_CARE;
-		(*next)->visualType = GLX_DONT_CARE;
-		(*next)->visualRating = GLX_NONE;
-		(*next)->transparentPixel = GLX_NONE;
-		(*next)->transparentRed = GLX_DONT_CARE;
-		(*next)->transparentGreen = GLX_DONT_CARE;
-		(*next)->transparentBlue = GLX_DONT_CARE;
-		(*next)->transparentAlpha = GLX_DONT_CARE;
-		(*next)->transparentIndex = GLX_DONT_CARE;
-		(*next)->xRenderable = GLX_DONT_CARE;
-		(*next)->fbconfigID = GLX_DONT_CARE;
-		(*next)->swapMethod = GLX_SWAP_UNDEFINED_OML;
-		(*next)->bindToTextureRgb = GLX_DONT_CARE;
-		(*next)->bindToTextureRgba = GLX_DONT_CARE;
-		(*next)->bindToMipmapTexture = GLX_DONT_CARE;
-		(*next)->bindToTextureTargets = 0;
-		(*next)->yInverted = GLX_DONT_CARE;
-
-		next = & ((*next)->next);
-	}
-
-	return head;
-}
-
-__GLcontextModes *
-drm_visual_from_config(_EGLConfig *conf)
-{
-	__GLcontextModes *visual;
-	(void)conf;
-
-	visual = drm_visual_modes_create(1, sizeof(*visual));
-	visual->redBits = 8;
-	visual->greenBits = 8;
-	visual->blueBits = 8;
-	visual->alphaBits = 8;
-
-	visual->rgbBits = 32;
-	visual->doubleBufferMode = 1;
-
-	visual->depthBits = 24;
-	visual->haveDepthBuffer = visual->depthBits > 0;
-	visual->stencilBits = 8;
-	visual->haveStencilBuffer = visual->stencilBits > 0;
-
-	return visual;
-}
diff --git a/src/gallium/state_trackers/egl_g3d/kms/native_kms.c b/src/gallium/state_trackers/egl/kms/native_kms.c
similarity index 100%
rename from src/gallium/state_trackers/egl_g3d/kms/native_kms.c
rename to src/gallium/state_trackers/egl/kms/native_kms.c
diff --git a/src/gallium/state_trackers/egl_g3d/kms/native_kms.h b/src/gallium/state_trackers/egl/kms/native_kms.h
similarity index 100%
rename from src/gallium/state_trackers/egl_g3d/kms/native_kms.h
rename to src/gallium/state_trackers/egl/kms/native_kms.h
diff --git a/src/gallium/state_trackers/egl_g3d/x11/native_dri2.c b/src/gallium/state_trackers/egl/x11/native_dri2.c
similarity index 100%
rename from src/gallium/state_trackers/egl_g3d/x11/native_dri2.c
rename to src/gallium/state_trackers/egl/x11/native_dri2.c
diff --git a/src/gallium/state_trackers/egl_g3d/x11/native_x11.c b/src/gallium/state_trackers/egl/x11/native_x11.c
similarity index 100%
rename from src/gallium/state_trackers/egl_g3d/x11/native_x11.c
rename to src/gallium/state_trackers/egl/x11/native_x11.c
diff --git a/src/gallium/state_trackers/egl_g3d/x11/native_x11.h b/src/gallium/state_trackers/egl/x11/native_x11.h
similarity index 100%
rename from src/gallium/state_trackers/egl_g3d/x11/native_x11.h
rename to src/gallium/state_trackers/egl/x11/native_x11.h
diff --git a/src/gallium/state_trackers/egl_g3d/x11/native_ximage.c b/src/gallium/state_trackers/egl/x11/native_ximage.c
similarity index 100%
rename from src/gallium/state_trackers/egl_g3d/x11/native_ximage.c
rename to src/gallium/state_trackers/egl/x11/native_ximage.c
diff --git a/src/gallium/state_trackers/egl_g3d/x11/sw_winsys.c b/src/gallium/state_trackers/egl/x11/sw_winsys.c
similarity index 100%
rename from src/gallium/state_trackers/egl_g3d/x11/sw_winsys.c
rename to src/gallium/state_trackers/egl/x11/sw_winsys.c
diff --git a/src/gallium/state_trackers/egl_g3d/x11/sw_winsys.h b/src/gallium/state_trackers/egl/x11/sw_winsys.h
similarity index 100%
rename from src/gallium/state_trackers/egl_g3d/x11/sw_winsys.h
rename to src/gallium/state_trackers/egl/x11/sw_winsys.h
diff --git a/src/gallium/state_trackers/egl_g3d/x11/x11_screen.c b/src/gallium/state_trackers/egl/x11/x11_screen.c
similarity index 97%
rename from src/gallium/state_trackers/egl_g3d/x11/x11_screen.c
rename to src/gallium/state_trackers/egl/x11/x11_screen.c
index fef7878..76ce45e 100644
--- a/src/gallium/state_trackers/egl_g3d/x11/x11_screen.c
+++ b/src/gallium/state_trackers/egl/x11/x11_screen.c
@@ -42,6 +42,10 @@
    Display *dpy;
    int number;
 
+   /*
+    * This is used to fetch GLX visuals/fbconfigs.  It uses code from egl_xdri.
+    * It might be better to rewrite the part in Xlib or XCB.
+    */
    __GLXdisplayPrivate *glx_dpy;
 
    int dri_major, dri_minor;
@@ -93,8 +97,8 @@
    if (xscr->dri_device)
       Xfree(xscr->dri_device);
 
-   if (xscr->glx_dpy)
-      __glXRelease(xscr->glx_dpy);
+   /* xscr->glx_dpy will be destroyed with the X display */
+
    if (xscr->visuals)
       XFree(xscr->visuals);
    free(xscr);
diff --git a/src/gallium/state_trackers/egl_g3d/x11/x11_screen.h b/src/gallium/state_trackers/egl/x11/x11_screen.h
similarity index 100%
rename from src/gallium/state_trackers/egl_g3d/x11/x11_screen.h
rename to src/gallium/state_trackers/egl/x11/x11_screen.h
diff --git a/src/gallium/state_trackers/egl_g3d/Makefile b/src/gallium/state_trackers/egl_g3d/Makefile
deleted file mode 100644
index 213eb3e..0000000
--- a/src/gallium/state_trackers/egl_g3d/Makefile
+++ /dev/null
@@ -1,72 +0,0 @@
-TOP = ../../../..
-include $(TOP)/configs/current
-
-common_INCLUDES = \
-	-I. \
-	-I$(TOP)/src/gallium/include \
-	-I$(TOP)/src/gallium/auxiliary \
-	-I$(TOP)/src/egl/main \
-	-I$(TOP)/include
-
-common_SOURCES = $(wildcard common/*.c)
-common_OBJECTS = $(common_SOURCES:.c=.o)
-
-
-x11_INCLUDES = \
-	-I$(TOP)/src/gallium/drivers \
-	-I$(TOP)/src/glx/x11 \
-	-I$(TOP)/src/mesa \
-	$(shell pkg-config --cflags-only-I libdrm)
-
-x11_SOURCES = $(wildcard x11/*.c) $(TOP)/src/glx/x11/dri2.c
-x11_OBJECTS = $(x11_SOURCES:.c=.o)
-
-
-kms_INCLUDES = $(shell pkg-config --cflags-only-I libdrm)
-kms_SOURCES = $(wildcard kms/*.c)
-kms_OBJECTS = $(kms_SOURCES:.c=.o)
-
-
-ALL_INCLUDES = $(common_INCLUDES) $(x11_INCLUDES) $(kms_INCLUDES)
-ALL_SOURCES = $(common_SOURCES) $(x11_SOURCES) $(kms_SOURCES)
-ALL_OBJECTS = $(common_OBJECTS) $(x11_OBJECTS) $(kms_OBJECTS)
-
-##### TARGETS #####
-
-EGL_DISPLAYS_MODS = $(foreach dpy, $(EGL_DISPLAYS), libegl$(dpy).a)
-
-default: depend $(EGL_DISPLAYS_MODS)
-
-
-libeglx11.a: $(x11_OBJECTS) $(common_OBJECTS) Makefile
-	$(MKLIB) -o eglx11 -static $(x11_OBJECTS) $(common_OBJECTS)
-
-libeglkms.a: $(kms_OBJECTS) $(common_OBJECTS) Makefile
-	$(MKLIB) -o eglkms -static $(kms_OBJECTS) $(common_OBJECTS)
-
-depend: 
-	rm -f depend
-	touch depend
-	$(MKDEP) $(MKDEP_OPTIONS) $(ALL_INCLUDES) $(ALL_SOURCES) 2> /dev/null
-
-clean:
-	rm -f $(ALL_OBJECTS)
-	rm -f $(EGL_DISPLAYS_MODS)
-	rm -f depend depend.bak
-
-# Dummy target
-install:
-	@echo -n ""
-
-##### RULES #####
-
-$(common_OBJECTS): %.o: %.c
-	$(CC) -c $(common_INCLUDES) $(DEFINES) $(CFLAGS) $< -o $@
-
-$(x11_OBJECTS): %.o: %.c
-	$(CC) -c $(common_INCLUDES) $(x11_INCLUDES) $(DEFINES) $(CFLAGS) $< -o $@
-
-$(kms_OBJECTS): %.o: %.c
-	$(CC) -c $(common_INCLUDES) $(kms_INCLUDES) $(DEFINES) $(CFLAGS) $< -o $@
-
-sinclude depend
diff --git a/src/gallium/state_trackers/egl_g3d/x11/glxinit.c b/src/gallium/state_trackers/egl_g3d/x11/glxinit.c
deleted file mode 100644
index c955a90..0000000
--- a/src/gallium/state_trackers/egl_g3d/x11/glxinit.c
+++ /dev/null
@@ -1,573 +0,0 @@
-/**
- * GLX initialization.  Code based on glxext.c, glx_query.c, and
- * glcontextmodes.c under src/glx/x11/.  The major difference is that no DRI
- * related code here.
- *
- */
-
-#include <assert.h>
-#include <X11/Xlib.h>
-#include <X11/Xproto.h>
-#include <X11/extensions/Xext.h>
-#include <X11/extensions/extutil.h>
-#include <sys/time.h>
-
-#include "x11_screen.h"
-#include "glxinit.h"
-
-typedef struct GLXGenericGetString
-{
-   CARD8 reqType;
-   CARD8 glxCode;
-   CARD16 length B16;
-   CARD32 for_whom B32;
-   CARD32 name B32;
-} xGLXGenericGetStringReq;
-
-#define sz_xGLXGenericGetStringReq 12
-#define X_GLXGenericGetString 0
-
-/* Extension required boiler plate */
-
-static char *__glXExtensionName = GLX_EXTENSION_NAME;
-static XExtensionInfo *__glXExtensionInfo = NULL;
-
-static /* const */ XExtensionHooks __glXExtensionHooks = { NULL };
-static
-XEXT_GENERATE_FIND_DISPLAY(__glXFindDisplay, __glXExtensionInfo,
-                           __glXExtensionName, &__glXExtensionHooks,
-                           __GLX_NUMBER_EVENTS, NULL)
-
-static GLint
-_gl_convert_from_x_visual_type(int visualType)
-{
-#define NUM_VISUAL_TYPES   6
-   static const int glx_visual_types[NUM_VISUAL_TYPES] = {
-      GLX_STATIC_GRAY, GLX_GRAY_SCALE,
-      GLX_STATIC_COLOR, GLX_PSEUDO_COLOR,
-      GLX_TRUE_COLOR, GLX_DIRECT_COLOR
-   };
-
-   return ((unsigned) visualType < NUM_VISUAL_TYPES)
-      ? glx_visual_types[visualType] : GLX_NONE;
-}
-
-_X_HIDDEN char *
-__glXQueryServerString(Display * dpy, int opcode, CARD32 screen, CARD32 name)
-{
-   xGLXGenericGetStringReq *req;
-   xGLXSingleReply reply;
-   int length;
-   int numbytes;
-   char *buf;
-   CARD32 for_whom = screen;
-   CARD32 glxCode = X_GLXQueryServerString;
-
-
-   LockDisplay(dpy);
-
-
-   /* All of the GLX protocol requests for getting a string from the server
-    * look the same.  The exact meaning of the for_whom field is usually
-    * either the screen number (for glXQueryServerString) or the context tag
-    * (for GLXSingle).
-    */
-
-   GetReq(GLXGenericGetString, req);
-   req->reqType = opcode;
-   req->glxCode = glxCode;
-   req->for_whom = for_whom;
-   req->name = name;
-
-   _XReply(dpy, (xReply *) & reply, 0, False);
-
-   length = reply.length * 4;
-   numbytes = reply.size;
-
-   buf = (char *) Xmalloc(numbytes);
-   if (buf != NULL) {
-      _XRead(dpy, buf, numbytes);
-      length -= numbytes;
-   }
-
-   _XEatData(dpy, length);
-
-   UnlockDisplay(dpy);
-   SyncHandle();
-
-   return buf;
-}
-
-/************************************************************************/
-/*
-** Free the per screen configs data as well as the array of
-** __glXScreenConfigs.
-*/
-static void
-FreeScreenConfigs(__GLXdisplayPrivate * priv)
-{
-   __GLXscreenConfigs *psc;
-   GLint i, screens;
-
-   /* Free screen configuration information */
-   psc = priv->screenConfigs;
-   screens = ScreenCount(priv->dpy);
-   for (i = 0; i < screens; i++, psc++) {
-      if (psc->configs) {
-         x11_context_modes_destroy(psc->configs);
-         psc->configs = NULL;   /* NOTE: just for paranoia */
-      }
-      if (psc->visuals) {
-         x11_context_modes_destroy(psc->visuals);
-         psc->visuals = NULL;   /* NOTE: just for paranoia */
-      }
-      Xfree((char *) psc->serverGLXexts);
-   }
-   XFree((char *) priv->screenConfigs);
-   priv->screenConfigs = NULL;
-}
-
-/************************************************************************/
-
-/*
-** Query the version of the GLX extension.  This procedure works even if
-** the client extension is not completely set up.
-*/
-static Bool
-QueryVersion(Display * dpy, int opcode, int *major, int *minor)
-{
-   xGLXQueryVersionReq *req;
-   xGLXQueryVersionReply reply;
-
-   /* Send the glXQueryVersion request */
-   LockDisplay(dpy);
-   GetReq(GLXQueryVersion, req);
-   req->reqType = opcode;
-   req->glxCode = X_GLXQueryVersion;
-   req->majorVersion = GLX_MAJOR_VERSION;
-   req->minorVersion = GLX_MINOR_VERSION;
-   _XReply(dpy, (xReply *) & reply, 0, False);
-   UnlockDisplay(dpy);
-   SyncHandle();
-
-   if (reply.majorVersion != GLX_MAJOR_VERSION) {
-      /*
-       ** The server does not support the same major release as this
-       ** client.
-       */
-      return GL_FALSE;
-   }
-   *major = reply.majorVersion;
-   *minor = min(reply.minorVersion, GLX_MINOR_VERSION);
-   return GL_TRUE;
-}
-
-_X_HIDDEN void
-__glXInitializeVisualConfigFromTags(__GLcontextModes * config, int count,
-                                    const INT32 * bp, Bool tagged_only,
-                                    Bool fbconfig_style_tags)
-{
-   int i;
-
-   if (!tagged_only) {
-      /* Copy in the first set of properties */
-      config->visualID = *bp++;
-
-      config->visualType = _gl_convert_from_x_visual_type(*bp++);
-
-      config->rgbMode = *bp++;
-
-      config->redBits = *bp++;
-      config->greenBits = *bp++;
-      config->blueBits = *bp++;
-      config->alphaBits = *bp++;
-      config->accumRedBits = *bp++;
-      config->accumGreenBits = *bp++;
-      config->accumBlueBits = *bp++;
-      config->accumAlphaBits = *bp++;
-
-      config->doubleBufferMode = *bp++;
-      config->stereoMode = *bp++;
-
-      config->rgbBits = *bp++;
-      config->depthBits = *bp++;
-      config->stencilBits = *bp++;
-      config->numAuxBuffers = *bp++;
-      config->level = *bp++;
-
-      count -= __GLX_MIN_CONFIG_PROPS;
-   }
-
-   /*
-    ** Additional properties may be in a list at the end
-    ** of the reply.  They are in pairs of property type
-    ** and property value.
-    */
-
-#define FETCH_OR_SET(tag) \
-    config-> tag = ( fbconfig_style_tags ) ? *bp++ : 1
-
-   for (i = 0; i < count; i += 2) {
-      switch (*bp++) {
-      case GLX_RGBA:
-         FETCH_OR_SET(rgbMode);
-         break;
-      case GLX_BUFFER_SIZE:
-         config->rgbBits = *bp++;
-         break;
-      case GLX_LEVEL:
-         config->level = *bp++;
-         break;
-      case GLX_DOUBLEBUFFER:
-         FETCH_OR_SET(doubleBufferMode);
-         break;
-      case GLX_STEREO:
-         FETCH_OR_SET(stereoMode);
-         break;
-      case GLX_AUX_BUFFERS:
-         config->numAuxBuffers = *bp++;
-         break;
-      case GLX_RED_SIZE:
-         config->redBits = *bp++;
-         break;
-      case GLX_GREEN_SIZE:
-         config->greenBits = *bp++;
-         break;
-      case GLX_BLUE_SIZE:
-         config->blueBits = *bp++;
-         break;
-      case GLX_ALPHA_SIZE:
-         config->alphaBits = *bp++;
-         break;
-      case GLX_DEPTH_SIZE:
-         config->depthBits = *bp++;
-         break;
-      case GLX_STENCIL_SIZE:
-         config->stencilBits = *bp++;
-         break;
-      case GLX_ACCUM_RED_SIZE:
-         config->accumRedBits = *bp++;
-         break;
-      case GLX_ACCUM_GREEN_SIZE:
-         config->accumGreenBits = *bp++;
-         break;
-      case GLX_ACCUM_BLUE_SIZE:
-         config->accumBlueBits = *bp++;
-         break;
-      case GLX_ACCUM_ALPHA_SIZE:
-         config->accumAlphaBits = *bp++;
-         break;
-      case GLX_VISUAL_CAVEAT_EXT:
-         config->visualRating = *bp++;
-         break;
-      case GLX_X_VISUAL_TYPE:
-         config->visualType = *bp++;
-         break;
-      case GLX_TRANSPARENT_TYPE:
-         config->transparentPixel = *bp++;
-         break;
-      case GLX_TRANSPARENT_INDEX_VALUE:
-         config->transparentIndex = *bp++;
-         break;
-      case GLX_TRANSPARENT_RED_VALUE:
-         config->transparentRed = *bp++;
-         break;
-      case GLX_TRANSPARENT_GREEN_VALUE:
-         config->transparentGreen = *bp++;
-         break;
-      case GLX_TRANSPARENT_BLUE_VALUE:
-         config->transparentBlue = *bp++;
-         break;
-      case GLX_TRANSPARENT_ALPHA_VALUE:
-         config->transparentAlpha = *bp++;
-         break;
-      case GLX_VISUAL_ID:
-         config->visualID = *bp++;
-         break;
-      case GLX_DRAWABLE_TYPE:
-         config->drawableType = *bp++;
-         break;
-      case GLX_RENDER_TYPE:
-         config->renderType = *bp++;
-         break;
-      case GLX_X_RENDERABLE:
-         config->xRenderable = *bp++;
-         break;
-      case GLX_FBCONFIG_ID:
-         config->fbconfigID = *bp++;
-         break;
-      case GLX_MAX_PBUFFER_WIDTH:
-         config->maxPbufferWidth = *bp++;
-         break;
-      case GLX_MAX_PBUFFER_HEIGHT:
-         config->maxPbufferHeight = *bp++;
-         break;
-      case GLX_MAX_PBUFFER_PIXELS:
-         config->maxPbufferPixels = *bp++;
-         break;
-      case GLX_OPTIMAL_PBUFFER_WIDTH_SGIX:
-         config->optimalPbufferWidth = *bp++;
-         break;
-      case GLX_OPTIMAL_PBUFFER_HEIGHT_SGIX:
-         config->optimalPbufferHeight = *bp++;
-         break;
-      case GLX_VISUAL_SELECT_GROUP_SGIX:
-         config->visualSelectGroup = *bp++;
-         break;
-      case GLX_SWAP_METHOD_OML:
-         config->swapMethod = *bp++;
-         break;
-      case GLX_SAMPLE_BUFFERS_SGIS:
-         config->sampleBuffers = *bp++;
-         break;
-      case GLX_SAMPLES_SGIS:
-         config->samples = *bp++;
-         break;
-      case GLX_BIND_TO_TEXTURE_RGB_EXT:
-         config->bindToTextureRgb = *bp++;
-         break;
-      case GLX_BIND_TO_TEXTURE_RGBA_EXT:
-         config->bindToTextureRgba = *bp++;
-         break;
-      case GLX_BIND_TO_MIPMAP_TEXTURE_EXT:
-         config->bindToMipmapTexture = *bp++;
-         break;
-      case GLX_BIND_TO_TEXTURE_TARGETS_EXT:
-         config->bindToTextureTargets = *bp++;
-         break;
-      case GLX_Y_INVERTED_EXT:
-         config->yInverted = *bp++;
-         break;
-      case None:
-         i = count;
-         break;
-      default:
-         break;
-      }
-   }
-
-   config->renderType =
-      (config->rgbMode) ? GLX_RGBA_BIT : GLX_COLOR_INDEX_BIT;
-
-   config->haveAccumBuffer = ((config->accumRedBits +
-                               config->accumGreenBits +
-                               config->accumBlueBits +
-                               config->accumAlphaBits) > 0);
-   config->haveDepthBuffer = (config->depthBits > 0);
-   config->haveStencilBuffer = (config->stencilBits > 0);
-}
-
-static __GLcontextModes *
-createConfigsFromProperties(Display * dpy, int nvisuals, int nprops,
-                            int screen, GLboolean tagged_only)
-{
-   INT32 buf[__GLX_TOTAL_CONFIG], *props;
-   unsigned prop_size;
-   __GLcontextModes *modes, *m;
-   int i;
-
-   if (nprops == 0)
-      return NULL;
-
-   /* FIXME: Is the __GLX_MIN_CONFIG_PROPS test correct for FBconfigs? */
-
-   /* Check number of properties */
-   if (nprops < __GLX_MIN_CONFIG_PROPS || nprops > __GLX_MAX_CONFIG_PROPS)
-      return NULL;
-
-   /* Allocate memory for our config structure */
-   modes = x11_context_modes_create(nvisuals);
-   if (!modes)
-      return NULL;
-
-   prop_size = nprops * __GLX_SIZE_INT32;
-   if (prop_size <= sizeof(buf))
-      props = buf;
-   else
-      props = Xmalloc(prop_size);
-
-   /* Read each config structure and convert it into our format */
-   m = modes;
-   for (i = 0; i < nvisuals; i++) {
-      _XRead(dpy, (char *) props, prop_size);
-      /* Older X servers don't send this so we default it here. */
-      m->drawableType = GLX_WINDOW_BIT;
-      __glXInitializeVisualConfigFromTags(m, nprops, props,
-                                     tagged_only, GL_TRUE);
-      m->screen = screen;
-      m = m->next;
-   }
-
-   if (props != buf)
-      Xfree(props);
-
-   return modes;
-}
-
-static GLboolean
-getVisualConfigs(Display * dpy, __GLXdisplayPrivate * priv, int screen)
-{
-   xGLXGetVisualConfigsReq *req;
-   __GLXscreenConfigs *psc;
-   xGLXGetVisualConfigsReply reply;
-
-   LockDisplay(dpy);
-
-   psc = priv->screenConfigs + screen;
-   psc->visuals = NULL;
-   GetReq(GLXGetVisualConfigs, req);
-   req->reqType = priv->majorOpcode;
-   req->glxCode = X_GLXGetVisualConfigs;
-   req->screen = screen;
-
-   if (!_XReply(dpy, (xReply *) & reply, 0, False))
-      goto out;
-
-   psc->visuals = createConfigsFromProperties(dpy,
-                                              reply.numVisuals,
-                                              reply.numProps,
-                                              screen, GL_FALSE);
-
- out:
-   UnlockDisplay(dpy);
-   return psc->visuals != NULL;
-}
-
-static GLboolean
-getFBConfigs(Display * dpy, __GLXdisplayPrivate * priv, int screen)
-{
-   xGLXGetFBConfigsReq *fb_req;
-   xGLXGetFBConfigsSGIXReq *sgi_req;
-   xGLXVendorPrivateWithReplyReq *vpreq;
-   xGLXGetFBConfigsReply reply;
-   __GLXscreenConfigs *psc;
-
-   psc = priv->screenConfigs + screen;
-   psc->serverGLXexts =
-      __glXQueryServerString(dpy, priv->majorOpcode, screen, GLX_EXTENSIONS);
-
-   LockDisplay(dpy);
-
-   psc->configs = NULL;
-   if (atof(priv->serverGLXversion) >= 1.3) {
-      GetReq(GLXGetFBConfigs, fb_req);
-      fb_req->reqType = priv->majorOpcode;
-      fb_req->glxCode = X_GLXGetFBConfigs;
-      fb_req->screen = screen;
-   }
-   else if (strstr(psc->serverGLXexts, "GLX_SGIX_fbconfig") != NULL) {
-      GetReqExtra(GLXVendorPrivateWithReply,
-                  sz_xGLXGetFBConfigsSGIXReq +
-                  sz_xGLXVendorPrivateWithReplyReq, vpreq);
-      sgi_req = (xGLXGetFBConfigsSGIXReq *) vpreq;
-      sgi_req->reqType = priv->majorOpcode;
-      sgi_req->glxCode = X_GLXVendorPrivateWithReply;
-      sgi_req->vendorCode = X_GLXvop_GetFBConfigsSGIX;
-      sgi_req->screen = screen;
-   }
-   else
-      goto out;
-
-   if (!_XReply(dpy, (xReply *) & reply, 0, False))
-      goto out;
-
-   psc->configs = createConfigsFromProperties(dpy,
-                                              reply.numFBConfigs,
-                                              reply.numAttribs * 2,
-                                              screen, GL_TRUE);
-
- out:
-   UnlockDisplay(dpy);
-   return psc->configs != NULL;
-}
-
-static GLboolean
-AllocAndFetchScreenConfigs(Display * dpy, __GLXdisplayPrivate * priv)
-{
-   __GLXscreenConfigs *psc;
-   GLint i, screens;
-
-   /*
-    ** First allocate memory for the array of per screen configs.
-    */
-   screens = ScreenCount(dpy);
-   psc = (__GLXscreenConfigs *) Xmalloc(screens * sizeof(__GLXscreenConfigs));
-   if (!psc) {
-      return GL_FALSE;
-   }
-   memset(psc, 0, screens * sizeof(__GLXscreenConfigs));
-   priv->screenConfigs = psc;
-
-   priv->serverGLXversion =
-      __glXQueryServerString(dpy, priv->majorOpcode, 0, GLX_VERSION);
-   if (priv->serverGLXversion == NULL) {
-      FreeScreenConfigs(priv);
-      return GL_FALSE;
-   }
-
-   for (i = 0; i < screens; i++, psc++) {
-      getFBConfigs(dpy, priv, i);
-      getVisualConfigs(dpy, priv, i);
-      psc->scr = i;
-      psc->dpy = dpy;
-   }
-
-   SyncHandle();
-
-   return GL_TRUE;
-}
-
-_X_HIDDEN void
-__glXRelease(__GLXdisplayPrivate *dpyPriv)
-{
-   FreeScreenConfigs(dpyPriv);
-
-   if (dpyPriv->serverGLXvendor) {
-      Xfree((char *) dpyPriv->serverGLXvendor);
-      dpyPriv->serverGLXvendor = NULL;
-   }
-   if (dpyPriv->serverGLXversion) {
-      Xfree((char *) dpyPriv->serverGLXversion);
-      dpyPriv->serverGLXversion = NULL;
-   }
-
-   Xfree(dpyPriv);
-}
-
-_X_HIDDEN __GLXdisplayPrivate *
-__glXInitialize(Display * dpy)
-{
-   XExtDisplayInfo *info = __glXFindDisplay(dpy);
-   __GLXdisplayPrivate *dpyPriv;
-   int major, minor;
-
-   if (!XextHasExtension(info))
-      return NULL;
-
-   /* See if the versions are compatible */
-   if (!QueryVersion(dpy, info->codes->major_opcode, &major, &minor))
-      return NULL;
-
-   dpyPriv = (__GLXdisplayPrivate *) Xcalloc(1, sizeof(__GLXdisplayPrivate));
-   if (!dpyPriv)
-      return NULL;
-
-   /*
-    ** Init the display private and then read in the screen config
-    ** structures from the server.
-    */
-   dpyPriv->majorOpcode = info->codes->major_opcode;
-   dpyPriv->majorVersion = major;
-   dpyPriv->minorVersion = minor;
-   dpyPriv->dpy = dpy;
-
-   dpyPriv->serverGLXvendor = NULL;
-   dpyPriv->serverGLXversion = NULL;
-
-   if (!AllocAndFetchScreenConfigs(dpy, dpyPriv)) {
-      Xfree(dpyPriv);
-      return NULL;
-   }
-
-   return dpyPriv;
-}
diff --git a/src/gallium/state_trackers/egl_g3d/x11/glxinit.h b/src/gallium/state_trackers/egl_g3d/x11/glxinit.h
deleted file mode 100644
index 515a825..0000000
--- a/src/gallium/state_trackers/egl_g3d/x11/glxinit.h
+++ /dev/null
@@ -1,14 +0,0 @@
-#ifndef GLXINIT_INCLUDED
-#define GLXINIT_INCLUDED
-
-#include <X11/Xlib.h>
-
-#ifndef GLX_DIRECT_RENDERING
-#define GLX_DIRECT_RENDERING
-#endif
-#include "glxclient.h"
-
-extern void
-__glXRelease(__GLXdisplayPrivate *dpyPriv);
-
-#endif /* GLXINIT_INCLUDED */
diff --git a/src/gallium/state_trackers/wgl/stw_pixelformat.c b/src/gallium/state_trackers/wgl/stw_pixelformat.c
index 54cc361..7d4c243 100644
--- a/src/gallium/state_trackers/wgl/stw_pixelformat.c
+++ b/src/gallium/state_trackers/wgl/stw_pixelformat.c
@@ -95,8 +95,6 @@
    { PIPE_FORMAT_Z24X8_UNORM, {24, 0} },
    { PIPE_FORMAT_X8Z24_UNORM, {24, 0} },
    { PIPE_FORMAT_Z16_UNORM,   {16, 0} },
-   /* pure stencil */
-   { PIPE_FORMAT_S8_UNORM,    { 0, 8} },
    /* combined depth-stencil */
    { PIPE_FORMAT_S8Z24_UNORM, {24, 8} },
    { PIPE_FORMAT_Z24S8_UNORM, {24, 8} }
@@ -220,7 +218,8 @@
          const struct stw_pf_color_info *color = &stw_pf_color[j];
          
          if(!screen->is_format_supported(screen, color->format, PIPE_TEXTURE_2D, 
-                                         PIPE_TEXTURE_USAGE_RENDER_TARGET, 0))
+                                         PIPE_TEXTURE_USAGE_RENDER_TARGET |
+                                         PIPE_TEXTURE_USAGE_DISPLAY_TARGET, 0))
             continue;
          
          for(k = 0; k < Elements(stw_pf_doublebuffer); ++k) {
diff --git a/src/gallium/state_trackers/xorg/xorg_composite.c b/src/gallium/state_trackers/xorg/xorg_composite.c
index 1c248a6..0324441 100644
--- a/src/gallium/state_trackers/xorg/xorg_composite.c
+++ b/src/gallium/state_trackers/xorg/xorg_composite.c
@@ -4,10 +4,7 @@
 #include "xorg_exa_tgsi.h"
 
 #include "cso_cache/cso_context.h"
-#include "util/u_draw_quad.h"
-#include "util/u_math.h"
 
-#include "pipe/p_inlines.h"
 
 /*XXX also in Xrender.h but the including it here breaks compilition */
 #define XFixedToDouble(f)    (((double) (f)) / 65536.)
diff --git a/src/gallium/state_trackers/xorg/xorg_dri2.c b/src/gallium/state_trackers/xorg/xorg_dri2.c
index fd82f4f..e6a89c7 100644
--- a/src/gallium/state_trackers/xorg/xorg_dri2.c
+++ b/src/gallium/state_trackers/xorg/xorg_dri2.c
@@ -44,9 +44,12 @@
 #include "util/u_rect.h"
 
 /* Make all the #if cases in the code esier to read */
-/* XXX can it be set to 1? */
 #ifndef DRI2INFOREC_VERSION
-#define DRI2INFOREC_VERSION 0
+#define DRI2INFOREC_VERSION 1
+#endif
+
+#if DRI2INFOREC_VERSION == 2
+static Bool set_format_in_do_create_buffer;
 #endif
 
 typedef struct {
@@ -147,7 +150,9 @@
     buffer->driverPrivate = private;
     buffer->flags = 0; /* not tiled */
 #if DRI2INFOREC_VERSION == 2
-    ((DRI2Buffer2Ptr)buffer)->format = 0;
+    /* ABI forwards/backwards compatibility */
+    if (set_format_in_do_create_buffer)
+	((DRI2Buffer2Ptr)buffer)->format = 0;
 #elif DRI2INFOREC_VERSION >= 3
     buffer->format = 0;
 #endif
@@ -211,7 +216,9 @@
     xfree(buffer);
 }
 
-#else /* DRI2INFOREC_VERSION < 2 */
+#endif /* DRI2INFOREC_VERSION >= 2 */
+
+#if DRI2INFOREC_VERSION <= 2
 
 static DRI2BufferPtr
 dri2_create_buffers(DrawablePtr pDraw, unsigned int *attachments, int count)
@@ -261,7 +268,7 @@
     }
 }
 
-#endif /* DRI2INFOREC_VERSION >= 2 */
+#endif /* DRI2INFOREC_VERSION <= 2 */
 
 static void
 dri2_copy_region(DrawablePtr pDraw, RegionPtr pRegion,
@@ -369,12 +376,17 @@
     ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
     modesettingPtr ms = modesettingPTR(pScrn);
     DRI2InfoRec dri2info;
+    int major, minor;
 
-#if DRI2INFOREC_VERSION >= 2
+    if (xf86LoaderCheckSymbol("DRI2Version")) {
+	DRI2Version(&major, &minor);
+    } else {
+	/* Assume version 1.0 */
+	major = 1;
+	minor = 0;
+    }
+
     dri2info.version = DRI2INFOREC_VERSION;
-#else
-    dri2info.version = 1;
-#endif
     dri2info.fd = ms->fd;
 
     dri2info.driverName = pScrn->driverName;
@@ -383,7 +395,22 @@
 #if DRI2INFOREC_VERSION >= 2
     dri2info.CreateBuffer = dri2_create_buffer;
     dri2info.DestroyBuffer = dri2_destroy_buffer;
-#else
+#endif
+
+    /* For X servers in the 1.6.x series there where two DRI2 version.
+     * This allows us to build one binary that works on both servers.
+     */
+#if DRI2INFOREC_VERSION == 2
+    if (minor == 0) {
+	set_format_in_do_create_buffer = FALSE;
+	dri2info.CreateBuffers = dri2_create_buffers;
+	dri2info.DestroyBuffers = dri2_destroy_buffers;
+    } else
+	set_format_in_do_create_buffer = FALSE;
+#endif
+
+    /* For version 1 set these unconditionaly. */
+#if DRI2INFOREC_VERSION == 1
     dri2info.CreateBuffers = dri2_create_buffers;
     dri2info.DestroyBuffers = dri2_destroy_buffers;
 #endif
diff --git a/src/gallium/state_trackers/xorg/xorg_driver.c b/src/gallium/state_trackers/xorg/xorg_driver.c
index b02fe68..41bfcd0 100644
--- a/src/gallium/state_trackers/xorg/xorg_driver.c
+++ b/src/gallium/state_trackers/xorg/xorg_driver.c
@@ -45,7 +45,6 @@
 #include "miscstruct.h"
 #include "dixstruct.h"
 #include "xf86xv.h"
-#include <X11/extensions/Xv.h>
 #ifndef XSERVER_LIBPCIACCESS
 #error "libpciaccess needed"
 #endif
@@ -206,16 +205,41 @@
 		ms->PciInfo->dev, ms->PciInfo->func
 	    );
 
-	ms->fd = drmOpen(NULL, BusID);
 
-	if (ms->fd < 0)
-	    return FALSE;
+	ms->api = drm_api_create();
+	ms->fd = drmOpen(ms->api ? ms->api->driver_name : NULL, BusID);
+	xfree(BusID);
+
+	if (ms->fd >= 0)
+	    return TRUE;
+
+	if (ms->api && ms->api->destroy)
+	    ms->api->destroy(ms->api);
+
+	ms->api = NULL;
+
+	return FALSE;
     }
 
     return TRUE;
 }
 
 static Bool
+drv_close_drm(ScrnInfoPtr pScrn)
+{
+    modesettingPtr ms = modesettingPTR(pScrn);
+
+    if (ms->api && ms->api->destroy)
+	ms->api->destroy(ms->api);
+    ms->api = NULL;
+
+    drmClose(ms->fd);
+    ms->fd = -1;
+
+    return TRUE;
+}
+
+static Bool
 drv_init_resource_management(ScrnInfoPtr pScrn)
 {
     modesettingPtr ms = modesettingPTR(pScrn);
@@ -229,7 +253,6 @@
     if (ms->screen || ms->kms)
 	return TRUE;
 
-    ms->api = drm_api_create();
     if (ms->api) {
 	ms->screen = ms->api->create_screen(ms->api, ms->fd, NULL);
 
@@ -269,10 +292,6 @@
     }
     ms->screen = NULL;
 
-    if (ms->api && ms->api->destroy)
-	ms->api->destroy(ms->api);
-    ms->api = NULL;
-
 #ifdef HAVE_LIBKMS
     if (ms->kms)
 	kms_destroy(&ms->kms);
@@ -823,8 +842,7 @@
 
     drv_close_resource_management(pScrn);
 
-    drmClose(ms->fd);
-    ms->fd = -1;
+    drv_close_drm(pScrn);
 
     pScrn->vtSema = FALSE;
     pScreen->CloseScreen = ms->CloseScreen;
diff --git a/src/gallium/state_trackers/xorg/xorg_exa.c b/src/gallium/state_trackers/xorg/xorg_exa.c
index d9432ba..c91dee7 100644
--- a/src/gallium/state_trackers/xorg/xorg_exa.c
+++ b/src/gallium/state_trackers/xorg/xorg_exa.c
@@ -41,7 +41,6 @@
 #include "pipe/p_format.h"
 #include "pipe/p_context.h"
 #include "pipe/p_state.h"
-#include "pipe/p_inlines.h"
 
 #include "util/u_format.h"
 #include "util/u_rect.h"
diff --git a/src/gallium/state_trackers/xorg/xorg_exa_tgsi.c b/src/gallium/state_trackers/xorg/xorg_exa_tgsi.c
index bed17ca..3e5e6bd 100644
--- a/src/gallium/state_trackers/xorg/xorg_exa_tgsi.c
+++ b/src/gallium/state_trackers/xorg/xorg_exa_tgsi.c
@@ -6,11 +6,9 @@
 #include "pipe/p_format.h"
 #include "pipe/p_context.h"
 #include "pipe/p_state.h"
-#include "pipe/p_inlines.h"
 #include "pipe/p_shader_tokens.h"
 
 #include "util/u_memory.h"
-#include "util/u_simple_shaders.h"
 
 #include "tgsi/tgsi_ureg.h"
 
diff --git a/src/gallium/state_trackers/xorg/xorg_output.c b/src/gallium/state_trackers/xorg/xorg_output.c
index 251f331..13c3fb9 100644
--- a/src/gallium/state_trackers/xorg/xorg_output.c
+++ b/src/gallium/state_trackers/xorg/xorg_output.c
@@ -49,8 +49,6 @@
 #include <X11/extensions/dpms.h>
 #endif
 
-#include "X11/Xatom.h"
-
 #include "xorg_tracker.h"
 
 static char *output_enum_list[] = {
diff --git a/src/gallium/state_trackers/xorg/xorg_xv.c b/src/gallium/state_trackers/xorg/xorg_xv.c
index 5bf0e94..7bcf77e 100644
--- a/src/gallium/state_trackers/xorg/xorg_xv.c
+++ b/src/gallium/state_trackers/xorg/xorg_xv.c
@@ -11,7 +11,6 @@
 #include "cso_cache/cso_context.h"
 
 #include "pipe/p_screen.h"
-#include "pipe/p_inlines.h"
 
 #include "util/u_format.h"
 
diff --git a/src/gallium/winsys/drm/Makefile.egl_g3d b/src/gallium/winsys/drm/Makefile.egl
similarity index 85%
rename from src/gallium/winsys/drm/Makefile.egl_g3d
rename to src/gallium/winsys/drm/Makefile.egl
index 3ce2725..1b10f11 100644
--- a/src/gallium/winsys/drm/Makefile.egl_g3d
+++ b/src/gallium/winsys/drm/Makefile.egl
@@ -1,4 +1,4 @@
-# src/gallium/winsys/drm/Makefile.egl_g3d
+# src/gallium/winsys/drm/Makefile.egl
 
 # The driver Makefile should define
 #
@@ -13,10 +13,10 @@
 
 common_LIBS = -ldrm -lm -ldl
 
-x11_ST = $(TOP)/src/gallium/state_trackers/egl_g3d/libeglx11.a
+x11_ST = $(TOP)/src/gallium/state_trackers/egl/libeglx11.a
 x11_LIBS = $(common_LIBS) -lX11 -lXext -lXfixes
 
-kms_ST = $(TOP)/src/gallium/state_trackers/egl_g3d/libeglkms.a
+kms_ST = $(TOP)/src/gallium/state_trackers/egl/libeglkms.a
 kms_LIBS = $(common_LIBS)
 
 ##### RULES #####
@@ -39,7 +39,8 @@
 	$(INSTALL) $^ $(LIB_GALLIUM_DIR)
 
 define mklib-egl
-$(MKLIB) -noprefix -o $@ $(EGL_DRIVER_OBJECTS) \
+$(MKLIB) -o $@ -noprefix -linker '$(CC)' -ldflags '$(LDFLAGS)' \
+	$(MKLIB_OPTIONS) $(EGL_DRIVER_OBJECTS) \
 	-Wl,--whole-archive $($(1)_ST) -Wl,--no-whole-archive \
 	$(EGL_DRIVER_PIPES) $(GALLIUM_AUXILIARIES) $($(1)_LIBS) $(EGL_DRIVER_LIBS)
 endef
diff --git a/src/gallium/winsys/drm/i965/egl/Makefile b/src/gallium/winsys/drm/i965/egl/Makefile
index a1b32eb..1c13258 100644
--- a/src/gallium/winsys/drm/i965/egl/Makefile
+++ b/src/gallium/winsys/drm/i965/egl/Makefile
@@ -1,29 +1,14 @@
 TOP = ../../../../../..
-GALLIUMDIR = ../../../..
 include $(TOP)/configs/current
 
-LIBNAME = EGL_i965.so
+EGL_DRIVER_NAME = i965
+EGL_DRIVER_SOURCES = dummy.c
+EGL_DRIVER_LIBS = -ldrm_intel
 
-PIPE_DRIVERS = \
-	$(TOP)/src/gallium/state_trackers/egl/libegldrm.a \
-	$(GALLIUMDIR)/winsys/drm/i965/gem/libi965drm.a \
+EGL_DRIVER_PIPES = \
+	$(TOP)/src/gallium/winsys/drm/i965/gem/libi965drm.a \
 	$(TOP)/src/gallium/drivers/softpipe/libsoftpipe.a \
 	$(TOP)/src/gallium/drivers/trace/libtrace.a \
 	$(TOP)/src/gallium/drivers/i965/libi965.a
 
-DRIVER_SOURCES =
-
-C_SOURCES = \
-	$(COMMON_GALLIUM_SOURCES) \
-	$(DRIVER_SOURCES)
-
-DRIVER_EXTRAS = -ldrm_intel
-
-ASM_SOURCES = 
-
-DRIVER_DEFINES = -I../gem $(shell pkg-config libdrm --atleast-version=2.3.1 \
-				&& echo "-DDRM_VBLANK_FLIP=DRM_VBLANK_FLIP")
-
-include ../../Makefile.template
-
-symlinks:
+include ../../Makefile.egl
diff --git a/src/gallium/winsys/drm/i965/egl_g3d/dummy.c b/src/gallium/winsys/drm/i965/egl/dummy.c
similarity index 100%
rename from src/gallium/winsys/drm/i965/egl_g3d/dummy.c
rename to src/gallium/winsys/drm/i965/egl/dummy.c
diff --git a/src/gallium/winsys/drm/i965/egl_g3d/Makefile b/src/gallium/winsys/drm/i965/egl_g3d/Makefile
deleted file mode 100644
index dd2efe2..0000000
--- a/src/gallium/winsys/drm/i965/egl_g3d/Makefile
+++ /dev/null
@@ -1,14 +0,0 @@
-TOP = ../../../../../..
-include $(TOP)/configs/current
-
-EGL_DRIVER_NAME = i965
-EGL_DRIVER_SOURCES = dummy.c
-EGL_DRIVER_LIBS = -ldrm_intel
-
-EGL_DRIVER_PIPES = \
-	$(TOP)/src/gallium/winsys/drm/i965/gem/libi965drm.a \
-	$(TOP)/src/gallium/drivers/softpipe/libsoftpipe.a \
-	$(TOP)/src/gallium/drivers/trace/libtrace.a \
-	$(TOP)/src/gallium/drivers/i965/libi965.a
-
-include ../../Makefile.egl_g3d
diff --git a/src/gallium/winsys/drm/intel/egl/Makefile b/src/gallium/winsys/drm/intel/egl/Makefile
index c9c92b6..60d675c 100644
--- a/src/gallium/winsys/drm/intel/egl/Makefile
+++ b/src/gallium/winsys/drm/intel/egl/Makefile
@@ -1,38 +1,14 @@
 TOP = ../../../../../..
-GALLIUMDIR = ../../../..
 include $(TOP)/configs/current
 
-LIBNAME = egl_i915.so
+EGL_DRIVER_NAME = i915
+EGL_DRIVER_SOURCES = dummy.c
+EGL_DRIVER_LIBS = -ldrm_intel
 
-PIPE_DRIVERS = \
-	$(TOP)/src/gallium/state_trackers/egl/libegldrm.a \
-	$(GALLIUMDIR)/winsys/drm/intel/gem/libinteldrm.a \
+EGL_DRIVER_PIPES = \
+	$(TOP)/src/gallium/winsys/drm/intel/gem/libinteldrm.a \
 	$(TOP)/src/gallium/drivers/softpipe/libsoftpipe.a \
 	$(TOP)/src/gallium/drivers/trace/libtrace.a \
 	$(TOP)/src/gallium/drivers/i915/libi915.a
 
-DRIVER_EXTRAS = -lm -lpthread -ldrm_intel
-
-OBJECTS = dummy.o
-
-default: $(TOP)/$(LIB_DIR)/$(LIBNAME)
-
-$(TOP)/$(LIB_DIR)/$(LIBNAME): $(LIBNAME)
-	@mkdir -p $(TOP)/$(LIB_DIR)
-	$(INSTALL) $(LIBNAME) $(TOP)/$(LIB_DIR)
-
-$(LIBNAME): $(OBJECTS) $(GALLIUM_AUXILIARIES) $(PIPE_DRIVERS) Makefile
-	$(MKLIB) -noprefix -o $@ $(OBJECTS) \
-		-Wl,--whole-archive $(PIPE_DRIVERS) -Wl,--no-whole-archive \
-		-Wl,--start-group $(GALLIUM_AUXILIARIES) -Wl,--end-group \
-                 $(DRI_LIB_DEPS) $(DRIVER_EXTRAS)
-
-clean:
-	-rm -f *.o *.so *~
-
-depend:
-
-symlinks:
-
-install: $(LIBNAME)
-	$(MINSTALL) -m 755 $(LIBNAME) $(INSTALL_DIR)/$(LIB_DIR)
+include ../../Makefile.egl
diff --git a/src/gallium/winsys/drm/intel/egl/dummy.c b/src/gallium/winsys/drm/intel/egl/dummy.c
index 58c7af8..4a1bc28 100644
--- a/src/gallium/winsys/drm/intel/egl/dummy.c
+++ b/src/gallium/winsys/drm/intel/egl/dummy.c
@@ -1 +1 @@
-/* mklib expects at least one .o is given */
+/* mklib expects at least one object file */
diff --git a/src/gallium/winsys/drm/intel/egl_g3d/Makefile b/src/gallium/winsys/drm/intel/egl_g3d/Makefile
deleted file mode 100644
index cdbb680..0000000
--- a/src/gallium/winsys/drm/intel/egl_g3d/Makefile
+++ /dev/null
@@ -1,14 +0,0 @@
-TOP = ../../../../../..
-include $(TOP)/configs/current
-
-EGL_DRIVER_NAME = i915
-EGL_DRIVER_SOURCES = dummy.c
-EGL_DRIVER_LIBS = -ldrm_intel
-
-EGL_DRIVER_PIPES = \
-	$(TOP)/src/gallium/winsys/drm/intel/gem/libinteldrm.a \
-	$(TOP)/src/gallium/drivers/softpipe/libsoftpipe.a \
-	$(TOP)/src/gallium/drivers/trace/libtrace.a \
-	$(TOP)/src/gallium/drivers/i915/libi915.a
-
-include ../../Makefile.egl_g3d
diff --git a/src/gallium/winsys/drm/intel/egl_g3d/dummy.c b/src/gallium/winsys/drm/intel/egl_g3d/dummy.c
deleted file mode 100644
index 4a1bc28..0000000
--- a/src/gallium/winsys/drm/intel/egl_g3d/dummy.c
+++ /dev/null
@@ -1 +0,0 @@
-/* mklib expects at least one object file */
diff --git a/src/gallium/winsys/drm/intel/gem/intel_drm_api.c b/src/gallium/winsys/drm/intel/gem/intel_drm_api.c
index 450ae09..8c8176e 100644
--- a/src/gallium/winsys/drm/intel/gem/intel_drm_api.c
+++ b/src/gallium/winsys/drm/intel/gem/intel_drm_api.c
@@ -196,6 +196,7 @@
 struct drm_api intel_drm_api =
 {
    .name = "i915",
+   .driver_name = "i915",
    .create_context = intel_drm_create_context,
    .create_screen = intel_drm_create_screen,
    .texture_from_shared_handle = intel_drm_texture_from_shared_handle,
diff --git a/src/gallium/winsys/drm/nouveau/drm/nouveau_drm_api.c b/src/gallium/winsys/drm/nouveau/drm/nouveau_drm_api.c
index 4b2c6a1..c9f39d8 100644
--- a/src/gallium/winsys/drm/nouveau/drm/nouveau_drm_api.c
+++ b/src/gallium/winsys/drm/nouveau/drm/nouveau_drm_api.c
@@ -255,6 +255,7 @@
 
 struct drm_api drm_api_hooks = {
 	.name = "nouveau",
+	.driver_name = "nouveau",
 	.create_screen = nouveau_drm_create_screen,
 	.create_context = nouveau_drm_create_context,
 	.texture_from_shared_handle = nouveau_drm_pt_from_name,
diff --git a/src/gallium/winsys/drm/nouveau/egl_g3d/Makefile b/src/gallium/winsys/drm/nouveau/egl/Makefile
similarity index 94%
rename from src/gallium/winsys/drm/nouveau/egl_g3d/Makefile
rename to src/gallium/winsys/drm/nouveau/egl/Makefile
index 865a5d5..8e812ac 100644
--- a/src/gallium/winsys/drm/nouveau/egl_g3d/Makefile
+++ b/src/gallium/winsys/drm/nouveau/egl/Makefile
@@ -16,4 +16,4 @@
 	$(TOP)/src/gallium/drivers/nouveau/libnouveau.a \
 	$(TOP)/src/gallium/drivers/softpipe/libsoftpipe.a
 
-include ../../Makefile.egl_g3d
+include ../../Makefile.egl
diff --git a/src/gallium/winsys/drm/nouveau/egl_g3d/dummy.c b/src/gallium/winsys/drm/nouveau/egl/dummy.c
similarity index 100%
rename from src/gallium/winsys/drm/nouveau/egl_g3d/dummy.c
rename to src/gallium/winsys/drm/nouveau/egl/dummy.c
diff --git a/src/gallium/winsys/drm/radeon/core/radeon_drm.c b/src/gallium/winsys/drm/radeon/core/radeon_drm.c
index 9552f0a..bff6fdc 100644
--- a/src/gallium/winsys/drm/radeon/core/radeon_drm.c
+++ b/src/gallium/winsys/drm/radeon/core/radeon_drm.c
@@ -270,6 +270,7 @@
 
 struct drm_api drm_api_hooks = {
     .name = "radeon",
+    .driver_name = "radeon",
     .create_screen = radeon_create_screen,
     .create_context = radeon_create_context,
     .texture_from_shared_handle = radeon_texture_from_shared_handle,
diff --git a/src/gallium/winsys/drm/radeon/egl/Makefile b/src/gallium/winsys/drm/radeon/egl/Makefile
index fcbfd09..cd4f9b2 100644
--- a/src/gallium/winsys/drm/radeon/egl/Makefile
+++ b/src/gallium/winsys/drm/radeon/egl/Makefile
@@ -1,38 +1,14 @@
 TOP = ../../../../../..
-GALLIUMDIR = ../../../..
 include $(TOP)/configs/current
 
-LIBNAME = egl_radeon.so
+EGL_DRIVER_NAME = radeon
+EGL_DRIVER_SOURCES = dummy.c
+EGL_DRIVER_LIBS = -ldrm_radeon
 
-PIPE_DRIVERS = \
-	$(TOP)/src/gallium/state_trackers/egl/libegldrm.a \
-	$(GALLIUMDIR)/winsys/drm/radeon/core/libradeonwinsys.a \
+EGL_DRIVER_PIPES = \
+	$(TOP)/src/gallium/winsys/drm/radeon/core/libradeonwinsys.a \
 	$(TOP)/src/gallium/drivers/softpipe/libsoftpipe.a \
 	$(TOP)/src/gallium/drivers/trace/libtrace.a \
 	$(TOP)/src/gallium/drivers/r300/libr300.a
 
-DRIVER_EXTRAS = -lm -lpthread -ldrm_radeon
-
-OBJECTS = dummy.o
-
-default: $(TOP)/$(LIB_DIR)/$(LIBNAME)
-
-$(TOP)/$(LIB_DIR)/$(LIBNAME): $(LIBNAME)
-	@mkdir -p $(TOP)/$(LIB_DIR)
-	$(INSTALL) $(LIBNAME) $(TOP)/$(LIB_DIR)
-
-$(LIBNAME): $(OBJECTS) $(GALLIUM_AUXILIARIES) $(PIPE_DRIVERS) Makefile
-	$(MKLIB) -noprefix -o $@ $(OBJECTS) \
-		-Wl,--whole-archive $(PIPE_DRIVERS) -Wl,--no-whole-archive \
-		-Wl,--start-group $(GALLIUM_AUXILIARIES) -Wl,--end-group \
-                 $(DRI_LIB_DEPS) $(DRIVER_EXTRAS)
-
-clean:
-	-rm -f *.o *.so *~
-
-depend:
-
-symlinks:
-
-install: $(LIBNAME)
-	$(MINSTALL) -m 755 $(LIBNAME) $(DESTDIR)$(INSTALL_DIR)/$(LIB_DIR)
+include ../../Makefile.egl
diff --git a/src/gallium/winsys/drm/radeon/egl/dummy.c b/src/gallium/winsys/drm/radeon/egl/dummy.c
index 58c7af8..4a1bc28 100644
--- a/src/gallium/winsys/drm/radeon/egl/dummy.c
+++ b/src/gallium/winsys/drm/radeon/egl/dummy.c
@@ -1 +1 @@
-/* mklib expects at least one .o is given */
+/* mklib expects at least one object file */
diff --git a/src/gallium/winsys/drm/radeon/egl_g3d/Makefile b/src/gallium/winsys/drm/radeon/egl_g3d/Makefile
deleted file mode 100644
index e0de68c..0000000
--- a/src/gallium/winsys/drm/radeon/egl_g3d/Makefile
+++ /dev/null
@@ -1,14 +0,0 @@
-TOP = ../../../../../..
-include $(TOP)/configs/current
-
-EGL_DRIVER_NAME = radeon
-EGL_DRIVER_SOURCES = dummy.c
-EGL_DRIVER_LIBS = -ldrm_radeon
-
-EGL_DRIVER_PIPES = \
-	$(TOP)/src/gallium/winsys/drm/radeon/core/libradeonwinsys.a \
-	$(TOP)/src/gallium/drivers/softpipe/libsoftpipe.a \
-	$(TOP)/src/gallium/drivers/trace/libtrace.a \
-	$(TOP)/src/gallium/drivers/r300/libr300.a
-
-include ../../Makefile.egl_g3d
diff --git a/src/gallium/winsys/drm/radeon/egl_g3d/dummy.c b/src/gallium/winsys/drm/radeon/egl_g3d/dummy.c
deleted file mode 100644
index 4a1bc28..0000000
--- a/src/gallium/winsys/drm/radeon/egl_g3d/dummy.c
+++ /dev/null
@@ -1 +0,0 @@
-/* mklib expects at least one object file */
diff --git a/src/gallium/winsys/drm/swrast/Makefile b/src/gallium/winsys/drm/swrast/Makefile
new file mode 100644
index 0000000..363b895
--- /dev/null
+++ b/src/gallium/winsys/drm/swrast/Makefile
@@ -0,0 +1,12 @@
+# src/gallium/winsys/drm/swrast/Makefile
+TOP = ../../../../..
+include $(TOP)/configs/current
+
+SUBDIRS = core $(GALLIUM_STATE_TRACKERS_DIRS)
+
+default install clean:
+	@for dir in $(SUBDIRS) ; do \
+		if [ -d $$dir ] ; then \
+			(cd $$dir && $(MAKE) $@) || exit 1; \
+		fi \
+	done
diff --git a/src/gallium/winsys/drm/swrast/core/Makefile b/src/gallium/winsys/drm/swrast/core/Makefile
new file mode 100644
index 0000000..93931ae
--- /dev/null
+++ b/src/gallium/winsys/drm/swrast/core/Makefile
@@ -0,0 +1,10 @@
+# src/gallium/winsys/drm/swrast/core/Makefile
+
+TOP = ../../../../../..
+include $(TOP)/configs/current
+
+LIBNAME = swrastdrm
+
+C_SOURCES = swrast_drm_api.c
+
+include ../../../../Makefile.template
diff --git a/src/gallium/winsys/drm/swrast/core/swrast_drm_api.c b/src/gallium/winsys/drm/swrast/core/swrast_drm_api.c
new file mode 100644
index 0000000..8c9f80e
--- /dev/null
+++ b/src/gallium/winsys/drm/swrast/core/swrast_drm_api.c
@@ -0,0 +1,13 @@
+#include "state_tracker/drm_api.h"
+
+static struct drm_api swrast_drm_api =
+{
+   .name = "swrast",
+};
+
+struct drm_api *
+drm_api_create()
+{
+   (void) swrast_drm_api;
+   return NULL;
+}
diff --git a/src/gallium/winsys/drm/swrast/egl/Makefile b/src/gallium/winsys/drm/swrast/egl/Makefile
new file mode 100644
index 0000000..26fe2d2
--- /dev/null
+++ b/src/gallium/winsys/drm/swrast/egl/Makefile
@@ -0,0 +1,12 @@
+TOP = ../../../../../..
+include $(TOP)/configs/current
+
+EGL_DRIVER_NAME = swrast
+EGL_DRIVER_SOURCES = dummy.c
+EGL_DRIVER_LIBS =
+
+EGL_DRIVER_PIPES = \
+	$(TOP)/src/gallium/winsys/drm/swrast/core/libswrastdrm.a \
+	$(TOP)/src/gallium/drivers/softpipe/libsoftpipe.a
+
+include ../../Makefile.egl
diff --git a/src/gallium/winsys/drm/i965/egl_g3d/dummy.c b/src/gallium/winsys/drm/swrast/egl/dummy.c
similarity index 100%
copy from src/gallium/winsys/drm/i965/egl_g3d/dummy.c
copy to src/gallium/winsys/drm/swrast/egl/dummy.c
diff --git a/src/gallium/winsys/drm/vmware/core/vmw_context.c b/src/gallium/winsys/drm/vmware/core/vmw_context.c
index b699758..b5fd4f5 100644
--- a/src/gallium/winsys/drm/vmware/core/vmw_context.c
+++ b/src/gallium/winsys/drm/vmware/core/vmw_context.c
@@ -41,9 +41,18 @@
 
 #define VMW_COMMAND_SIZE (64*1024)
 #define VMW_SURFACE_RELOCS (1024)
+#define VMW_REGION_RELOCS (512)
 
 #define VMW_MUST_FLUSH_STACK 8
 
+struct vmw_region_relocation
+{
+   struct SVGAGuestPtr *where;
+   struct pb_buffer *buffer;
+   /* TODO: put offset info inside where */
+   uint32 offset;
+};
+
 struct vmw_svga_winsys_context
 {
    struct svga_winsys_context base;
@@ -69,10 +78,31 @@
       uint32_t staged;
       uint32_t reserved;
    } surface;
+   
+   struct {
+      struct vmw_region_relocation relocs[VMW_REGION_RELOCS];
+      uint32_t size;
+      uint32_t used;
+      uint32_t staged;
+      uint32_t reserved;
+   } region;
 
    struct pb_validate *validate;
 
    uint32_t last_fence;
+
+   /**
+    * The amount of GMR that is referred by the commands currently batched
+    * in the context.
+    */
+   uint32_t seen_regions;
+
+   /**
+    * Whether this context should fail to reserve more commands, not because it
+    * ran out of command space, but because a substantial ammount of GMR was
+    * referred.
+    */
+   boolean preemptive_flush;
 };
 
 
@@ -96,6 +126,19 @@
    ret = pb_validate_validate(vswc->validate);
    assert(ret == PIPE_OK);
    if(ret == PIPE_OK) {
+   
+      /* Apply relocations */
+      for(i = 0; i < vswc->region.used; ++i) {
+         struct vmw_region_relocation *reloc = &vswc->region.relocs[i];
+         struct SVGAGuestPtr ptr;
+
+         if(!vmw_gmr_bufmgr_region_ptr(reloc->buffer, &ptr))
+            assert(0);
+
+         ptr.offset += reloc->offset;
+
+         *reloc->where = ptr;
+      }
 
       if (vswc->command.used)
          vmw_ioctl_command(vswc->vws,
@@ -121,9 +164,18 @@
    vswc->surface.used = 0;
    vswc->surface.reserved = 0;
 
+   for(i = 0; i < vswc->region.used + vswc->region.staged; ++i) {
+      pb_reference(&vswc->region.relocs[i].buffer, NULL);
+   }
+
+   vswc->region.used = 0;
+   vswc->region.reserved = 0;
+
 #ifdef DEBUG
    vswc->must_flush = FALSE;
 #endif
+   vswc->preemptive_flush = FALSE;
+   vswc->seen_regions = 0;
 
    if(pfence)
       *pfence = fence;
@@ -151,8 +203,10 @@
    if(nr_bytes > vswc->command.size)
       return NULL;
 
-   if(vswc->command.used + nr_bytes > vswc->command.size ||
-      vswc->surface.used + nr_relocs > vswc->surface.size) {
+   if(vswc->preemptive_flush ||
+      vswc->command.used + nr_bytes > vswc->command.size ||
+      vswc->surface.used + nr_relocs > vswc->surface.size ||
+      vswc->region.used + nr_relocs > vswc->region.size) {
 #ifdef DEBUG
       vswc->must_flush = TRUE;
       debug_backtrace_capture(vswc->must_flush_stack, 1,
@@ -163,11 +217,14 @@
 
    assert(vswc->command.used + nr_bytes <= vswc->command.size);
    assert(vswc->surface.used + nr_relocs <= vswc->surface.size);
-
+   assert(vswc->region.used + nr_relocs <= vswc->region.size);
+   
    vswc->command.reserved = nr_bytes;
    vswc->surface.reserved = nr_relocs;
    vswc->surface.staged = 0;
-
+   vswc->region.reserved = nr_relocs;
+   vswc->region.staged = 0;
+   
    return vswc->command.buffer + vswc->command.used;
 }
 
@@ -206,20 +263,41 @@
                           unsigned flags)
 {
    struct vmw_svga_winsys_context *vswc = vmw_svga_winsys_context(swc);
-   struct SVGAGuestPtr ptr;
-   struct pb_buffer *buf = vmw_pb_buffer(buffer);
+   struct vmw_region_relocation *reloc;
    enum pipe_error ret;
+   
+   assert(vswc->region.staged < vswc->region.reserved);
 
-   if(!vmw_gmr_bufmgr_region_ptr(buf, &ptr))
-      assert(0);
+   reloc = &vswc->region.relocs[vswc->region.used + vswc->region.staged];
+   reloc->where = where;
+   pb_reference(&reloc->buffer, vmw_pb_buffer(buffer));
+   reloc->offset = offset;
 
-   ptr.offset += offset;
+   ++vswc->region.staged;
 
-   *where = ptr;
-
-   ret = pb_validate_add_buffer(vswc->validate, buf, flags);
+   ret = pb_validate_add_buffer(vswc->validate, reloc->buffer, flags);
    /* TODO: Update pipebuffer to reserve buffers and not fail here */
    assert(ret == PIPE_OK);
+
+   /*
+    * Flush preemptively the FIFO commands to keep the GMR working set within
+    * the GMR pool size.
+    *
+    * This is necessary for applications like SPECviewperf that generate huge
+    * amounts of immediate vertex data, so that we don't pile up too much of
+    * that vertex data neither in the guest nor in the host.
+    *
+    * Note that in the current implementation if a region is referred twice in
+    * a command stream, it will be accounted twice. We could detect repeated
+    * regions and count only once, but there is no incentive to do that, since
+    * regions are typically short-lived; always referred in a single command;
+    * and at the worst we just flush the commands a bit sooner, which for the
+    * SVGA virtual device it's not a performance issue since flushing commands
+    * to the FIFO won't cause flushing in the host.
+    */
+   vswc->seen_regions += reloc->buffer->base.size;
+   if(vswc->seen_regions >= VMW_GMR_POOL_SIZE/2)
+      vswc->preemptive_flush = TRUE;
 }
 
 
@@ -238,6 +316,12 @@
    vswc->surface.used += vswc->surface.staged;
    vswc->surface.staged = 0;
    vswc->surface.reserved = 0;
+
+   assert(vswc->region.staged <= vswc->region.reserved);
+   assert(vswc->region.used + vswc->region.staged <= vswc->region.size);
+   vswc->region.used += vswc->region.staged;
+   vswc->region.staged = 0;
+   vswc->region.reserved = 0;
 }
 
 
@@ -246,6 +330,11 @@
 {
    struct vmw_svga_winsys_context *vswc = vmw_svga_winsys_context(swc);
    unsigned i;
+
+   for(i = 0; i < vswc->region.used; ++i) {
+      pb_reference(&vswc->region.relocs[i].buffer, NULL);
+   }
+
    for(i = 0; i < vswc->surface.used; ++i) {
       p_atomic_dec(&vswc->surface.handles[i]->validated);
       vmw_svga_winsys_surface_reference(&vswc->surface.handles[i], NULL);
@@ -279,6 +368,7 @@
 
    vswc->command.size = VMW_COMMAND_SIZE;
    vswc->surface.size = VMW_SURFACE_RELOCS;
+   vswc->region.size = VMW_REGION_RELOCS;
 
    vswc->validate = pb_validate_create();
    if(!vswc->validate) {
diff --git a/src/gallium/winsys/drm/vmware/core/vmw_screen.h b/src/gallium/winsys/drm/vmware/core/vmw_screen.h
index a875107..f1d6986 100644
--- a/src/gallium/winsys/drm/vmware/core/vmw_screen.h
+++ b/src/gallium/winsys/drm/vmware/core/vmw_screen.h
@@ -40,6 +40,10 @@
 
 #include "svga_winsys.h"
 
+
+#define VMW_GMR_POOL_SIZE (16*1024*1024)
+
+
 struct pb_manager;
 struct vmw_region;
 
diff --git a/src/gallium/winsys/drm/vmware/core/vmw_screen_dri.c b/src/gallium/winsys/drm/vmware/core/vmw_screen_dri.c
index 4f5ccea..d9abde3 100644
--- a/src/gallium/winsys/drm/vmware/core/vmw_screen_dri.c
+++ b/src/gallium/winsys/drm/vmware/core/vmw_screen_dri.c
@@ -85,6 +85,23 @@
    struct pipe_screen *screen;
    struct dri1_create_screen_arg *dri1;
 
+   if (!arg || arg->mode == DRM_CREATE_NORMAL) {
+      struct dri1_api_version drm_ver;
+      drmVersionPtr ver;
+
+      ver = drmGetVersion(fd);
+      if (ver == NULL)
+	 return NULL;
+
+      drm_ver.major = ver->version_major;
+      drm_ver.minor = ver->version_minor;
+
+      drmFreeVersion(ver);
+      if (!vmw_dri1_check_version(&drm_ver, &drm_required,
+				  &drm_compat, "vmwgfx drm driver"))
+	 return NULL;
+   }
+
    if (arg != NULL) {
       switch (arg->mode) {
       case DRM_CREATE_NORMAL:
@@ -220,22 +237,19 @@
    vmw_svga_winsys_surface_reference(&vsrf, NULL);
 }
 
-/**
- * FIXME: We'd probably want to cache these buffers in the
- * screen, based on handle.
- */
-
-static struct pipe_buffer *
-vmw_drm_buffer_from_handle(struct drm_api *drm_api,
-                           struct pipe_screen *screen,
-			   const char *name,
-			   unsigned handle)
+static struct pipe_texture *
+vmw_drm_texture_from_handle(struct drm_api *drm_api,
+			    struct pipe_screen *screen,
+			    struct pipe_texture *templat,
+			    const char *name,
+			    unsigned stride,
+			    unsigned handle)
 {
     struct vmw_svga_winsys_surface *vsrf;
     struct svga_winsys_surface *ssrf;
     struct vmw_winsys_screen *vws =
 	vmw_winsys_screen(svga_winsys_screen(screen));
-    struct pipe_buffer *buf;
+    struct pipe_texture *tex;
     union drm_vmw_surface_reference_arg arg;
     struct drm_vmw_surface_arg *req = &arg.req;
     struct drm_vmw_surface_create_req *rep = &arg.rep;
@@ -282,43 +296,28 @@
 
     pipe_reference_init(&vsrf->refcnt, 1);
     p_atomic_set(&vsrf->validated, 0);
+    vsrf->screen = vws;
     vsrf->sid = handle;
     ssrf = svga_winsys_surface(vsrf);
-    buf = svga_screen_buffer_wrap_surface(screen, rep->format, ssrf);
-    if (!buf)
+    tex = svga_screen_texture_wrap_surface(screen, templat, rep->format, ssrf);
+    if (!tex)
 	vmw_svga_winsys_surface_reference(&vsrf, NULL);
 
-    return buf;
+    return tex;
   out_mip:
     vmw_ioctl_surface_destroy(vws, handle);
     return NULL;
 }
 
-static struct pipe_texture *
-vmw_drm_texture_from_handle(struct drm_api *drm_api,
-			    struct pipe_screen *screen,
-			    struct pipe_texture *templat,
-			    const char *name,
-			    unsigned stride,
-			    unsigned handle)
-{
-    struct pipe_buffer *buffer;
-    buffer = vmw_drm_buffer_from_handle(drm_api, screen, name, handle);
-
-    if (!buffer)
-	return NULL;
-
-    return screen->texture_blanket(screen, templat, &stride, buffer);
-}
-
 static boolean
-vmw_drm_handle_from_buffer(struct drm_api *drm_api,
+vmw_drm_handle_from_texture(struct drm_api *drm_api,
                            struct pipe_screen *screen,
-			   struct pipe_buffer *buffer,
+			   struct pipe_texture *texture,
+			   unsigned *stride,
 			   unsigned *handle)
 {
     struct svga_winsys_surface *surface =
-	svga_screen_buffer_get_winsys_surface(buffer);
+	svga_screen_texture_get_winsys_surface(texture);
     struct vmw_svga_winsys_surface *vsrf;
 
     if (!surface)
@@ -326,25 +325,13 @@
 
     vsrf = vmw_svga_winsys_surface(surface);
     *handle = vsrf->sid;
+    *stride = util_format_get_nblocksx(texture->format, texture->width0) *
+       util_format_get_blocksize(texture->format);
+
     vmw_svga_winsys_surface_reference(&vsrf, NULL);
     return TRUE;
 }
 
-static boolean
-vmw_drm_handle_from_texture(struct drm_api *drm_api,
-			    struct pipe_screen *screen,
-			    struct pipe_texture *texture,
-			    unsigned *stride,
-			    unsigned *handle)
-{
-    struct pipe_buffer *buffer;
-
-    if (!svga_screen_buffer_from_texture(texture, &buffer, stride))
-	return FALSE;
-
-    return vmw_drm_handle_from_buffer(drm_api, screen, buffer, handle);
-}
-
 static struct pipe_context*
 vmw_drm_create_context(struct drm_api *drm_api,
                        struct pipe_screen *screen)
@@ -359,6 +346,7 @@
 
 static struct drm_api vmw_drm_api_hooks = {
    .name = "vmwgfx",
+   .driver_name = "vmwgfx",
    .create_screen = vmw_drm_create_screen,
    .create_context = vmw_drm_create_context,
    .texture_from_shared_handle = vmw_drm_texture_from_handle,
diff --git a/src/gallium/winsys/drm/vmware/core/vmw_screen_pools.c b/src/gallium/winsys/drm/vmware/core/vmw_screen_pools.c
index b1c24b0..b9823d7 100644
--- a/src/gallium/winsys/drm/vmware/core/vmw_screen_pools.c
+++ b/src/gallium/winsys/drm/vmware/core/vmw_screen_pools.c
@@ -53,14 +53,32 @@
       goto error;
 
    vws->pools.gmr_mm = mm_bufmgr_create(vws->pools.gmr,
-                                        16*1024*1024,
+                                        VMW_GMR_POOL_SIZE,
                                         12 /* 4096 alignment */);
    if(!vws->pools.gmr_mm)
       goto error;
 
+   /*
+    * GMR buffers are typically shortlived, but it's possible that at a given
+    * instance a buffer is mapped. So to avoid stalling we tell pipebuffer to
+    * forbid creation of buffers beyond half the GMR pool size,
+    *
+    * XXX: It is unclear weather we want to limit the total amount of temporary
+    * malloc memory used to backup unvalidated GMR buffers. On one hand it is
+    * preferrable to fail an allocation than exhausting the guest memory with
+    * temporary data, but on the other hand it is possible that a stupid
+    * application creates large vertex buffers and does not use them for a long
+    * time -- since the svga pipe driver only emits the DMA uploads when a
+    * buffer is used for drawing this would effectively disabling swapping GMR
+    * buffers to memory. So far, the preemptively flush already seems to keep
+    * total allocated memory within relatively small numbers, so we don't
+    * limit.
+    */
    vws->pools.gmr_fenced = fenced_bufmgr_create(
       vws->pools.gmr_mm,
-      vmw_fence_ops_create(vws));
+      vmw_fence_ops_create(vws),
+      VMW_GMR_POOL_SIZE/2,
+      ~0);
 
 #ifdef DEBUG
    vws->pools.gmr_fenced = pb_debug_manager_create(vws->pools.gmr_fenced,
diff --git a/src/gallium/winsys/drm/vmware/egl/Makefile b/src/gallium/winsys/drm/vmware/egl/Makefile
index 8e2980c..a3e7313 100644
--- a/src/gallium/winsys/drm/vmware/egl/Makefile
+++ b/src/gallium/winsys/drm/vmware/egl/Makefile
@@ -1,18 +1,14 @@
-
 TOP = ../../../../../..
 include $(TOP)/configs/current
 
-LIBNAME = EGL_svga.so
+EGL_DRIVER_NAME = vmwgfx
+EGL_DRIVER_SOURCES = dummy.c
+EGL_DRIVER_LIBS =
 
-PIPE_DRIVERS = \
-	$(TOP)/src/gallium/state_trackers/egl/libegldrm.a \
+EGL_DRIVER_PIPES = \
 	$(TOP)/src/gallium/winsys/drm/vmware/core/libsvgadrm.a \
+	$(TOP)/src/gallium/drivers/softpipe/libsoftpipe.a \
 	$(TOP)/src/gallium/drivers/trace/libtrace.a \
 	$(TOP)/src/gallium/drivers/svga/libsvga.a
 
-C_SOURCES = \
-	$(COMMON_GALLIUM_SOURCES)
-
-include ../../Makefile.template
-
-symlinks:
+include ../../Makefile.egl
diff --git a/src/gallium/winsys/drm/vmware/egl_g3d/dummy.c b/src/gallium/winsys/drm/vmware/egl/dummy.c
similarity index 100%
rename from src/gallium/winsys/drm/vmware/egl_g3d/dummy.c
rename to src/gallium/winsys/drm/vmware/egl/dummy.c
diff --git a/src/gallium/winsys/drm/vmware/egl_g3d/Makefile b/src/gallium/winsys/drm/vmware/egl_g3d/Makefile
deleted file mode 100644
index 3cf7992..0000000
--- a/src/gallium/winsys/drm/vmware/egl_g3d/Makefile
+++ /dev/null
@@ -1,14 +0,0 @@
-TOP = ../../../../../..
-include $(TOP)/configs/current
-
-EGL_DRIVER_NAME = vmwgfx
-EGL_DRIVER_SOURCES = dummy.c
-EGL_DRIVER_LIBS =
-
-EGL_DRIVER_PIPES = \
-	$(TOP)/src/gallium/winsys/drm/vmware/core/libsvgadrm.a \
-	$(TOP)/src/gallium/drivers/softpipe/libsoftpipe.a \
-	$(TOP)/src/gallium/drivers/trace/libtrace.a \
-	$(TOP)/src/gallium/drivers/svga/libsvga.a
-
-include ../../Makefile.egl_g3d
diff --git a/src/gallium/winsys/egl_xlib/Makefile b/src/gallium/winsys/egl_xlib/Makefile
deleted file mode 100644
index 06c1fb0..0000000
--- a/src/gallium/winsys/egl_xlib/Makefile
+++ /dev/null
@@ -1,84 +0,0 @@
-# src/gallium/winsys/egl_xlib/Makefile
-
-# Build softpipe/xlib/EGL driver library/object: "egl_softpipe.so"
-
-
-TOP = ../../../..
-include $(TOP)/configs/current
-
-
-DRIVER_NAME = egl_softpipe.so
-
-
-INCLUDE_DIRS = \
-	-I$(TOP)/include \
-	-I$(TOP)/src/egl/main \
-	-I$(TOP)/src/mesa \
-	-I$(TOP)/src/mesa/main \
-	-I$(TOP)/src/gallium/include \
-	-I$(TOP)/src/gallium/drivers \
-	-I$(TOP)/src/gallium/auxiliary
-
-WINSYS_SOURCES = \
-	egl_xlib.c \
-	sw_winsys.c
-
-WINSYS_OBJECTS = $(WINSYS_SOURCES:.c=.o)
-
-
-LIBS = \
-	$(GALLIUM_DRIVERS) \
-	$(GALLIUM_AUXILIARIES)
-LIB_DEPS = $(EGL_LIB_DEPS) -lm -lX11
-
-LOCAL_CFLAGS =
-
-
-.c.o:
-	$(CC) -c $(INCLUDE_DIRS) $(CFLAGS) $(LOCAL_CFLAGS) $< -o $@
-
-
-.PHONY: library
-
-
-default: depend library Makefile
-
-
-library: $(TOP)/$(LIB_DIR)/$(DRIVER_NAME)
-
-
-# Make the egl_softpipe.so library
-$(TOP)/$(LIB_DIR)/$(DRIVER_NAME): $(WINSYS_OBJECTS) $(LIBS)
-	$(TOP)/bin/mklib -o $(DRIVER_NAME) \
-		-linker "$(CC)" \
-		-noprefix \
-		-install $(TOP)/$(LIB_DIR) \
-		$(MKLIB_OPTIONS) $(WINSYS_OBJECTS) \
-		-Wl,--whole-archive $(LIBS) -Wl,--no-whole-archive \
-		$(LIB_DEPS)
-
-
-depend: $(WINSYS_SOURCES)
-	@ echo "running $(MKDEP)"
-	@ rm -f depend  # workaround oops on gutsy?!?
-	@ touch depend
-	@ $(MKDEP) $(MKDEP_OPTIONS) $(DEFINES) $(INCLUDE_DIRS) $(WINSYS_SOURCES) \
-		> /dev/null 2>/dev/null
-
-
-install: default
-	$(INSTALL) -d $(INSTALL_DIR)/$(LIB_DIR)
-	@if [ -e $(TOP)/$(LIB_DIR) ]; then \
-		$(MINSTALL) $(TOP)/$(LIB_DIR)/$(DRIVER_NAME) $(INSTALL_DIR)/$(LIB_DIR); \
-	fi
-
-
-# Emacs tags
-tags:
-	etags `find . -name \*.[ch]` $(TOP)/include/GL/*.h
-
-clean:
-	-rm -f *.o *~ *.bak
-
-
-include depend
diff --git a/src/gallium/winsys/egl_xlib/egl_xlib.c b/src/gallium/winsys/egl_xlib/egl_xlib.c
deleted file mode 100644
index 1d9bac3..0000000
--- a/src/gallium/winsys/egl_xlib/egl_xlib.c
+++ /dev/null
@@ -1,853 +0,0 @@
-/**************************************************************************
- * 
- * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
- * All Rights Reserved.
- * 
- * Permission is hereby granted, free of charge, to any person obtaining a
- * copy of this software and associated documentation files (the
- * "Software"), to deal in the Software without restriction, including
- * without limitation the rights to use, copy, modify, merge, publish,
- * distribute, sub license, and/or sell copies of the Software, and to
- * permit persons to whom the Software is furnished to do so, subject to
- * the following conditions:
- * 
- * The above copyright notice and this permission notice (including the
- * next paragraph) shall be included in all copies or substantial portions
- * of the Software.
- * 
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
- * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
- * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
- * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
- * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
- * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- * 
- **************************************************************************/
-
-/**
- * EGL / softpipe / xlib winsys module
- *
- * Authors: Brian Paul
- */
-
-
-#include <dlfcn.h>
-#include <X11/Xlib.h>
-#include <X11/Xutil.h>
-
-#include "pipe/p_compiler.h"
-#include "pipe/p_format.h"
-#include "pipe/p_state.h"
-#include "pipe/internal/p_winsys_screen.h"
-#include "util/u_memory.h"
-#include "util/u_math.h"
-#include "softpipe/sp_winsys.h"
-#include "softpipe/sp_texture.h"
-
-#include "eglconfig.h"
-#include "eglconfigutil.h"
-#include "eglcontext.h"
-#include "egldisplay.h"
-#include "egldriver.h"
-#include "eglglobals.h"
-#include "egllog.h"
-#include "eglsurface.h"
-
-#include "state_tracker/st_public.h"
-
-#include "sw_winsys.h"
-
-
-/** subclass of _EGLDriver */
-struct xlib_egl_driver
-{
-   _EGLDriver Base;   /**< base class */
-   EGLint apis;
-};
-
-
-/** driver data of _EGLDisplay */
-struct xlib_egl_display
-{
-   Display *Dpy;
-
-   struct pipe_winsys *winsys;
-   struct pipe_screen *screen;
-};
-
-
-/** subclass of _EGLContext */
-struct xlib_egl_context
-{
-   _EGLContext Base;   /**< base class */
-
-   struct pipe_context *pipe;   /**< Gallium driver context */
-   struct st_context *Context;  /**< Mesa/gallium state tracker context */
-};
-
-
-/** subclass of _EGLSurface */
-struct xlib_egl_surface
-{
-   _EGLSurface Base;   /**< base class */
-
-   /* These are set for window surface */
-   Display *Dpy;  /**< The X Display of the window */
-   Window Win;    /**< The user-created window ID */
-   GC Gc;
-   XVisualInfo VisInfo;
-
-   struct pipe_winsys *winsys;
-
-   struct st_framebuffer *Framebuffer;
-};
-
-
-static void
-flush_frontbuffer(struct pipe_winsys *pws,
-                  struct pipe_surface *psurf,
-                  void *context_private);
-
-
-/** cast wrapper */
-static INLINE struct xlib_egl_driver *
-xlib_egl_driver(_EGLDriver *drv)
-{
-   return (struct xlib_egl_driver *) drv;
-}
-
-
-static INLINE struct xlib_egl_display *
-xlib_egl_display(_EGLDisplay *dpy)
-{
-   return (struct xlib_egl_display *) dpy->DriverData;
-}
-
-
-static INLINE struct xlib_egl_surface *
-lookup_surface(_EGLSurface *surf)
-{
-   return (struct xlib_egl_surface *) surf;
-}
-
-
-static INLINE struct xlib_egl_context *
-lookup_context(_EGLContext *ctx)
-{
-   return (struct xlib_egl_context *) ctx;
-}
-
-
-/**
- * Create the EGLConfigs.  (one per X visual)
- */
-static void
-create_configs(struct xlib_egl_display *xdpy, _EGLDisplay *disp)
-{
-   static const EGLint all_apis = (EGL_OPENGL_ES_BIT |
-                                   EGL_OPENGL_ES2_BIT |
-                                   EGL_OPENVG_BIT |
-                                   EGL_OPENGL_BIT);
-   XVisualInfo *visInfo, visTemplate;
-   int num_visuals, i;
-
-   /* get list of all X visuals, create an EGL config for each */
-   visTemplate.screen = DefaultScreen(xdpy->Dpy);
-   visInfo = XGetVisualInfo(xdpy->Dpy, VisualScreenMask,
-                            &visTemplate, &num_visuals);
-   if (!visInfo) {
-      printf("egl_xlib.c: couldn't get any X visuals\n");
-      abort();
-   }
-
-   for (i = 0; i < num_visuals; i++) {
-      _EGLConfig *config = calloc(1, sizeof(_EGLConfig));
-      int id = i + 1;
-      int rbits = util_bitcount(visInfo[i].red_mask);
-      int gbits = util_bitcount(visInfo[i].green_mask);
-      int bbits = util_bitcount(visInfo[i].blue_mask);
-      int abits = bbits == 8 ? 8 : 0;
-      int zbits = 24;
-      int sbits = 8;
-      int visid = visInfo[i].visualid;
-#if defined(__cplusplus) || defined(c_plusplus)
-      int vistype = visInfo[i].c_class;
-#else
-      int vistype = visInfo[i].class;
-#endif
-
-      _eglInitConfig(config, id);
-      SET_CONFIG_ATTRIB(config, EGL_BUFFER_SIZE, rbits + gbits + bbits + abits);
-      SET_CONFIG_ATTRIB(config, EGL_RED_SIZE, rbits);
-      SET_CONFIG_ATTRIB(config, EGL_GREEN_SIZE, gbits);
-      SET_CONFIG_ATTRIB(config, EGL_BLUE_SIZE, bbits);
-      SET_CONFIG_ATTRIB(config, EGL_ALPHA_SIZE, abits);
-      SET_CONFIG_ATTRIB(config, EGL_DEPTH_SIZE, zbits);
-      SET_CONFIG_ATTRIB(config, EGL_STENCIL_SIZE, sbits);
-      SET_CONFIG_ATTRIB(config, EGL_NATIVE_VISUAL_ID, visid);
-      SET_CONFIG_ATTRIB(config, EGL_NATIVE_VISUAL_TYPE, vistype);
-      SET_CONFIG_ATTRIB(config, EGL_NATIVE_RENDERABLE, EGL_FALSE);
-      SET_CONFIG_ATTRIB(config, EGL_CONFORMANT, all_apis);
-      SET_CONFIG_ATTRIB(config, EGL_RENDERABLE_TYPE, all_apis);
-      SET_CONFIG_ATTRIB(config, EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PBUFFER_BIT);
-      SET_CONFIG_ATTRIB(config, EGL_BIND_TO_TEXTURE_RGBA, EGL_TRUE);
-      SET_CONFIG_ATTRIB(config, EGL_BIND_TO_TEXTURE_RGB, EGL_TRUE);
-
-      _eglAddConfig(disp, config);
-   }
-
-   XFree(visInfo);
-}
-
-
-/**
- * Called via eglInitialize(), drv->API.Initialize().
- */
-static EGLBoolean
-xlib_eglInitialize(_EGLDriver *drv, _EGLDisplay *dpy,
-                   EGLint *major, EGLint *minor)
-{
-   struct xlib_egl_driver *xdrv = xlib_egl_driver(drv);
-   struct xlib_egl_display *xdpy;
-
-   xdpy = CALLOC_STRUCT(xlib_egl_display);
-   if (!xdpy)
-      return _eglError(EGL_BAD_ALLOC, "eglInitialize");
-
-   xdpy->Dpy = (Display *) dpy->NativeDisplay;
-   if (!xdpy->Dpy) {
-      xdpy->Dpy = XOpenDisplay(NULL);
-      if (!xdpy->Dpy) {
-         free(xdpy);
-         return EGL_FALSE;
-      }
-   }
-
-   /* create winsys and pipe screen */
-   xdpy->winsys = create_sw_winsys();
-   if (!xdpy->winsys) {
-      free(xdpy);
-      return _eglError(EGL_BAD_ALLOC, "eglInitialize");
-   }
-   xdpy->winsys->flush_frontbuffer = flush_frontbuffer;
-   xdpy->screen = softpipe_create_screen(xdpy->winsys);
-   if (!xdpy->screen) {
-      free(xdpy->winsys);
-      free(xdpy);
-      return _eglError(EGL_BAD_ALLOC, "eglInitialize");
-   }
-
-   dpy->DriverData = (void *) xdpy;
-   dpy->ClientAPIsMask = xdrv->apis;
-
-   create_configs(xdpy, dpy);
-
-   /* we're supporting EGL 1.4 */
-   *major = 1;
-   *minor = 4;
-
-   return EGL_TRUE;
-}
-
-
-/**
- * Called via eglTerminate(), drv->API.Terminate().
- */
-static EGLBoolean
-xlib_eglTerminate(_EGLDriver *drv, _EGLDisplay *dpy)
-{
-   struct xlib_egl_display *xdpy = xlib_egl_display(dpy);
-
-   _eglReleaseDisplayResources(drv, dpy);
-   _eglCleanupDisplay(dpy);
-
-   xdpy->screen->destroy(xdpy->screen);
-   free(xdpy->winsys);
-
-   if (!dpy->NativeDisplay)
-      XCloseDisplay(xdpy->Dpy);
-   free(xdpy);
-
-   return EGL_TRUE;
-}
-
-
-static _EGLProc
-xlib_eglGetProcAddress(_EGLDriver *drv, const char *procname)
-{
-   return (_EGLProc) st_get_proc_address(procname);
-}
-
-
-static void
-get_drawable_visual_info(Display *dpy, Drawable d, XVisualInfo *visInfo)
-{
-   XWindowAttributes attr;
-   XVisualInfo visTemp, *vis;
-   int num_visuals;
-
-   XGetWindowAttributes(dpy, d, &attr);
-
-   visTemp.screen = DefaultScreen(dpy);
-   visTemp.visualid = attr.visual->visualid;
-   vis = XGetVisualInfo(dpy,
-                        (VisualScreenMask | VisualIDMask),
-                        &visTemp, &num_visuals);
-   if (vis)
-      *visInfo = *vis;
-
-   XFree(vis);
-}
-
-
-
-/** Get size of given window */
-static Status
-get_drawable_size(Display *dpy, Drawable d, uint *width, uint *height)
-{
-   Window root;
-   Status stat;
-   int xpos, ypos;
-   unsigned int w, h, bw, depth;
-   stat = XGetGeometry(dpy, d, &root, &xpos, &ypos, &w, &h, &bw, &depth);
-   *width = w;
-   *height = h;
-   return stat;
-}
-
-
-static void
-check_and_update_buffer_size(struct xlib_egl_surface *surface)
-{
-   uint width, height;
-   if (surface->Base.Type == EGL_PBUFFER_BIT) {
-      width = surface->Base.Width;
-      height = surface->Base.Height;
-   }
-   else {
-      get_drawable_size(surface->Dpy, surface->Win, &width, &height);
-   }
-   st_resize_framebuffer(surface->Framebuffer, width, height);
-   surface->Base.Width = width;
-   surface->Base.Height = height;
-}
-
-
-
-static void
-display_surface(struct pipe_winsys *pws,
-                struct pipe_surface *psurf,
-                struct xlib_egl_surface *xsurf)
-{
-   struct softpipe_texture *spt = softpipe_texture(psurf->texture);
-   XImage *ximage;
-   void *data;
-
-   if (xsurf->Base.Type == EGL_PBUFFER_BIT)
-      return;
-
-   ximage = XCreateImage(xsurf->Dpy,
-                         xsurf->VisInfo.visual,
-                         xsurf->VisInfo.depth,
-                         ZPixmap, 0,   /* format, offset */
-                         NULL,         /* data */
-                         0, 0,         /* size */
-                         32,           /* bitmap_pad */
-                         0);           /* bytes_per_line */
-
-
-   assert(ximage->format);
-   assert(ximage->bitmap_unit);
-
-   data = pws->buffer_map(pws, spt->buffer, 0);
-
-   /* update XImage's fields */
-   ximage->data = data;
-   ximage->width = psurf->width;
-   ximage->height = psurf->height;
-   ximage->bytes_per_line = spt->stride[psurf->level];
-   
-   XPutImage(xsurf->Dpy, xsurf->Win, xsurf->Gc,
-             ximage, 0, 0, 0, 0, psurf->width, psurf->height);
-
-   XSync(xsurf->Dpy, 0);
-
-   ximage->data = NULL;
-   XDestroyImage(ximage);
-
-   pws->buffer_unmap(pws, spt->buffer);
-}
-
-
-
-/** Display gallium surface in X window */
-static void
-flush_frontbuffer(struct pipe_winsys *pws,
-                  struct pipe_surface *psurf,
-                  void *context_private)
-{
-   struct xlib_egl_surface *xsurf = (struct xlib_egl_surface *) context_private;
-   display_surface(pws, psurf, xsurf);
-}
-
-
-
-/**
- * Called via eglCreateContext(), drv->API.CreateContext().
- */
-static _EGLContext *
-xlib_eglCreateContext(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf,
-                      _EGLContext *share_list, const EGLint *attrib_list)
-{
-   struct xlib_egl_display *xdpy = xlib_egl_display(dpy);
-   struct xlib_egl_context *ctx;
-   struct st_context *share_ctx = NULL; /* XXX fix */
-   __GLcontextModes visual;
-
-   ctx = CALLOC_STRUCT(xlib_egl_context);
-   if (!ctx)
-      return NULL;
-
-   /* let EGL lib init the common stuff */
-   if (!_eglInitContext(drv, &ctx->Base, conf, attrib_list)) {
-      free(ctx);
-      return NULL;
-   }
-
-   /* API-dependent context creation */
-   switch (ctx->Base.ClientAPI) {
-   case EGL_OPENVG_API:
-   case EGL_OPENGL_ES_API:
-      _eglLog(_EGL_DEBUG, "Create Context for ES version %d\n",
-              ctx->Base.ClientVersion);
-      /* fall-through */
-   case EGL_OPENGL_API:
-      /* create a softpipe context */
-      ctx->pipe = softpipe_create(xdpy->screen);
-      /* Now do xlib / state tracker inits here */
-      _eglConfigToContextModesRec(conf, &visual);
-      ctx->Context = st_create_context(ctx->pipe, &visual, share_ctx);
-      break;
-   default:
-      _eglError(EGL_BAD_MATCH, "eglCreateContext(unsupported API)");
-      free(ctx);
-      return NULL;
-   }
-
-   return &ctx->Base;
-}
-
-
-static EGLBoolean
-xlib_eglDestroyContext(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx)
-{
-   struct xlib_egl_context *context = lookup_context(ctx);
-
-   if (!_eglIsContextBound(&context->Base)) {
-      /* API-dependent clean-up */
-      switch (context->Base.ClientAPI) {
-      case EGL_OPENGL_ES_API:
-      case EGL_OPENVG_API:
-         /* fall-through */
-      case EGL_OPENGL_API:
-         st_destroy_context(context->Context);
-         break;
-      default:
-         assert(0);
-      }
-      free(context);
-   }
-   return EGL_TRUE;
-}
-
-
-/**
- * Called via eglMakeCurrent(), drv->API.MakeCurrent().
- */
-static EGLBoolean
-xlib_eglMakeCurrent(_EGLDriver *drv, _EGLDisplay *dpy,
-                    _EGLSurface *draw, _EGLSurface *read, _EGLContext *ctx)
-{
-   struct xlib_egl_context *context = lookup_context(ctx);
-   struct xlib_egl_surface *draw_surf = lookup_surface(draw);
-   struct xlib_egl_surface *read_surf = lookup_surface(read);
-   struct st_context *oldcontext = NULL;
-   _EGLContext *oldctx;
-
-   oldctx = _eglGetCurrentContext();
-   if (oldctx && _eglIsContextLinked(oldctx))
-      oldcontext = st_get_current();
-
-   if (!_eglMakeCurrent(drv, dpy, draw, read, ctx))
-      return EGL_FALSE;
-
-   /* Flush before switching context.  Check client API? */
-   if (oldcontext)
-      st_flush(oldcontext, PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL);
-   st_make_current((context ? context->Context : NULL),
-                   (draw_surf ? draw_surf->Framebuffer : NULL),
-                   (read_surf ? read_surf->Framebuffer : NULL));
-
-   if (draw_surf)
-      check_and_update_buffer_size(draw_surf);
-   if (read_surf && read_surf != draw_surf)
-      check_and_update_buffer_size(draw_surf);
-
-   return EGL_TRUE;
-}
-
-
-static enum pipe_format
-choose_color_format(const __GLcontextModes *visual)
-{
-   if (visual->redBits == 8 &&
-       visual->greenBits == 8 &&
-       visual->blueBits == 8 &&
-       visual->alphaBits == 8) {
-      /* XXX this really also depends on the ordering of R,G,B,A */
-      return PIPE_FORMAT_A8R8G8B8_UNORM;
-   }
-   else {
-      assert(0);
-      return PIPE_FORMAT_NONE;
-   }
-}
-
-
-static enum pipe_format
-choose_depth_format(const __GLcontextModes *visual)
-{
-   if (visual->depthBits > 0)
-      return PIPE_FORMAT_S8Z24_UNORM;
-   else
-      return PIPE_FORMAT_NONE;
-}
-
-
-static enum pipe_format
-choose_stencil_format(const __GLcontextModes *visual)
-{
-   if (visual->stencilBits > 0)
-      return PIPE_FORMAT_S8Z24_UNORM;
-   else
-      return PIPE_FORMAT_NONE;
-}
-
-
-/**
- * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
- */
-static _EGLSurface *
-xlib_eglCreateWindowSurface(_EGLDriver *drv, _EGLDisplay *disp, _EGLConfig *conf,
-                            NativeWindowType window, const EGLint *attrib_list)
-{
-   struct xlib_egl_display *xdpy = xlib_egl_display(disp);
-   struct xlib_egl_surface *surf;
-   __GLcontextModes visual;
-   uint width, height;
-
-   surf = CALLOC_STRUCT(xlib_egl_surface);
-   if (!surf)
-      return NULL;
-
-   /* Let EGL lib init the common stuff */
-   if (!_eglInitSurface(drv, &surf->Base, EGL_WINDOW_BIT,
-                        conf, attrib_list)) {
-      free(surf);
-      return NULL;
-   }
-
-   /*
-    * Now init the Xlib and gallium stuff
-    */
-   surf->Win = (Window) window;  /* The X window ID */
-   surf->Dpy = xdpy->Dpy;  /* The X display */
-   surf->Gc = XCreateGC(surf->Dpy, surf->Win, 0, NULL);
-
-   surf->winsys = xdpy->winsys;
-
-   _eglConfigToContextModesRec(conf, &visual);
-   get_drawable_size(surf->Dpy, surf->Win, &width, &height);
-   get_drawable_visual_info(surf->Dpy, surf->Win, &surf->VisInfo);
-
-   surf->Base.Width = width;
-   surf->Base.Height = height;
-
-   /* Create GL statetracker framebuffer */
-   surf->Framebuffer = st_create_framebuffer(&visual,
-                                             choose_color_format(&visual),
-                                             choose_depth_format(&visual),
-                                             choose_stencil_format(&visual),
-                                             width, height,
-                                             (void *) surf);
-
-   st_resize_framebuffer(surf->Framebuffer, width, height);
-
-   return &surf->Base;
-}
-
-
-static _EGLSurface *
-xlib_eglCreatePbufferSurface(_EGLDriver *drv, _EGLDisplay *disp, _EGLConfig *conf,
-                             const EGLint *attrib_list)
-{
-   struct xlib_egl_display *xdpy = xlib_egl_display(disp);
-   struct xlib_egl_surface *surf;
-   __GLcontextModes visual;
-   uint width, height;
-   EGLBoolean bind_texture;
-
-   surf = CALLOC_STRUCT(xlib_egl_surface);
-   if (!surf) {
-      _eglError(EGL_BAD_ALLOC, "eglCreatePbufferSurface");
-      return NULL;
-   }
-
-   if (!_eglInitSurface(drv, &surf->Base, EGL_PBUFFER_BIT,
-                        conf, attrib_list)) {
-      free(surf);
-      return NULL;
-   }
-   if (surf->Base.Width < 0 || surf->Base.Height < 0) {
-      _eglError(EGL_BAD_PARAMETER, "eglCreatePbufferSurface");
-      free(surf);
-      return NULL;
-   }
-
-   bind_texture = (surf->Base.TextureFormat != EGL_NO_TEXTURE);
-   width = (uint) surf->Base.Width;
-   height = (uint) surf->Base.Height;
-   if ((surf->Base.TextureTarget == EGL_NO_TEXTURE && bind_texture) ||
-       (surf->Base.TextureTarget != EGL_NO_TEXTURE && !bind_texture)) {
-      _eglError(EGL_BAD_MATCH, "eglCreatePbufferSurface");
-      free(surf);
-      return NULL;
-   }
-   /* a framebuffer of zero width or height confuses st */
-   if (width == 0 || height == 0) {
-      _eglError(EGL_BAD_MATCH, "eglCreatePbufferSurface");
-      free(surf);
-      return NULL;
-   }
-   /* no mipmap generation */
-   if (surf->Base.MipmapTexture) {
-      _eglError(EGL_BAD_MATCH, "eglCreatePbufferSurface");
-      free(surf);
-      return NULL;
-   }
-
-   surf->winsys = xdpy->winsys;
-
-   _eglConfigToContextModesRec(conf, &visual);
-
-   /* Create GL statetracker framebuffer */
-   surf->Framebuffer = st_create_framebuffer(&visual,
-                                             choose_color_format(&visual),
-                                             choose_depth_format(&visual),
-                                             choose_stencil_format(&visual),
-                                             width, height,
-                                             (void *) surf);
-   st_resize_framebuffer(surf->Framebuffer, width, height);
-
-   return &surf->Base;
-}
-
-
-static EGLBoolean
-xlib_eglDestroySurface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surface)
-{
-   struct xlib_egl_surface *surf = lookup_surface(surface);
-   if (!_eglIsSurfaceBound(&surf->Base)) {
-      if (surf->Base.Type != EGL_PBUFFER_BIT)
-         XFreeGC(surf->Dpy, surf->Gc);
-      st_unreference_framebuffer(surf->Framebuffer);
-      free(surf);
-   }
-   return EGL_TRUE;
-}
-
-
-static EGLBoolean
-xlib_eglBindTexImage(_EGLDriver *drv, _EGLDisplay *dpy,
-                     _EGLSurface *surface, EGLint buffer)
-{
-   struct xlib_egl_surface *xsurf = lookup_surface(surface);
-   struct xlib_egl_context *xctx;
-   struct pipe_surface *psurf;
-   enum pipe_format format;
-   int target;
-
-   if (!xsurf || xsurf->Base.Type != EGL_PBUFFER_BIT)
-      return _eglError(EGL_BAD_SURFACE, "eglBindTexImage");
-   if (buffer != EGL_BACK_BUFFER)
-      return _eglError(EGL_BAD_PARAMETER, "eglBindTexImage");
-   if (xsurf->Base.BoundToTexture)
-      return _eglError(EGL_BAD_ACCESS, "eglBindTexImage");
-
-   /* this should be updated when choose_color_format is */
-   switch (xsurf->Base.TextureFormat) {
-   case EGL_TEXTURE_RGB:
-      format = PIPE_FORMAT_R8G8B8_UNORM;
-      break;
-   case EGL_TEXTURE_RGBA:
-      format = PIPE_FORMAT_A8R8G8B8_UNORM;
-      break;
-   default:
-      return _eglError(EGL_BAD_MATCH, "eglBindTexImage");
-   }
-
-   switch (xsurf->Base.TextureTarget) {
-   case EGL_TEXTURE_2D:
-      target = ST_TEXTURE_2D;
-      break;
-   default:
-      return _eglError(EGL_BAD_MATCH, "eglBindTexImage");
-   }
-
-   /* flush properly */
-   if (eglGetCurrentSurface(EGL_DRAW) == surface) {
-      xctx = lookup_context(_eglGetCurrentContext());
-      st_flush(xctx->Context, PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME,
-               NULL);
-   }
-   else if (_eglIsSurfaceBound(&xsurf->Base)) {
-      xctx = lookup_context(xsurf->Base.Binding);
-      if (xctx)
-         st_finish(xctx->Context);
-   }
-
-   st_get_framebuffer_surface(xsurf->Framebuffer, ST_SURFACE_BACK_LEFT,
-                              &psurf);
-   st_bind_texture_surface(psurf, target, xsurf->Base.MipmapLevel, format);
-   xsurf->Base.BoundToTexture = EGL_TRUE;
-
-   return EGL_TRUE;
-}
-
-
-static EGLBoolean
-xlib_eglReleaseTexImage(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surface,
-                        EGLint buffer)
-{
-   struct xlib_egl_surface *xsurf = lookup_surface(surface);
-   struct pipe_surface *psurf;
-
-   if (!xsurf || xsurf->Base.Type != EGL_PBUFFER_BIT ||
-       !xsurf->Base.BoundToTexture)
-      return _eglError(EGL_BAD_SURFACE, "eglReleaseTexImage");
-   if (buffer != EGL_BACK_BUFFER)
-      return _eglError(EGL_BAD_PARAMETER, "eglReleaseTexImage");
-
-   st_get_framebuffer_surface(xsurf->Framebuffer, ST_SURFACE_BACK_LEFT,
-                              &psurf);
-   st_unbind_texture_surface(psurf, ST_TEXTURE_2D, xsurf->Base.MipmapLevel);
-   xsurf->Base.BoundToTexture = EGL_FALSE;
-
-   return EGL_TRUE;
-}
-
-
-static EGLBoolean
-xlib_eglSwapBuffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *draw)
-{
-   struct xlib_egl_surface *xsurf = lookup_surface(draw);
-   struct pipe_winsys *pws = xsurf->winsys;
-   struct pipe_surface *psurf;
-
-   st_get_framebuffer_surface(xsurf->Framebuffer, ST_SURFACE_BACK_LEFT,
-         &psurf);
-
-   st_notify_swapbuffers(xsurf->Framebuffer);
-
-   display_surface(pws, psurf, xsurf);
-
-   check_and_update_buffer_size(xsurf);
-
-   return EGL_TRUE;
-}
-
-
-/**
- * Determine which API(s) is(are) present by looking for some specific
- * global symbols.
- */
-static EGLint
-find_supported_apis(void)
-{
-   EGLint mask = 0;
-   void *handle;
-
-   handle = dlopen(NULL, RTLD_LAZY | RTLD_LOCAL);
-   if(!handle)
-      return mask;
-
-   if (dlsym(handle, "st_api_OpenGL_ES1"))
-      mask |= EGL_OPENGL_ES_BIT;
-
-   if (dlsym(handle, "st_api_OpenGL_ES2"))
-      mask |= EGL_OPENGL_ES2_BIT;
-
-   if (dlsym(handle, "st_api_OpenGL"))
-      mask |= EGL_OPENGL_BIT;
-
-   if (dlsym(handle, "st_api_OpenVG"))
-      mask |= EGL_OPENVG_BIT;
-
-   dlclose(handle);
-
-   return mask;
-}
-
-
-static void
-xlib_Unload(_EGLDriver *drv)
-{
-   struct xlib_egl_driver *xdrv = xlib_egl_driver(drv);
-   free(xdrv);
-}
-
-
-/**
- * This is the main entrypoint into the driver.
- * Called by libEGL to instantiate an _EGLDriver object.
- */
-_EGLDriver *
-_eglMain(const char *args)
-{
-   struct xlib_egl_driver *xdrv;
-
-   _eglLog(_EGL_INFO, "Entering EGL/Xlib _eglMain(%s)", args);
-
-   xdrv = CALLOC_STRUCT(xlib_egl_driver);
-   if (!xdrv)
-      return NULL;
-
-   _eglInitDriverFallbacks(&xdrv->Base);
-   xdrv->Base.API.Initialize = xlib_eglInitialize;
-   xdrv->Base.API.Terminate = xlib_eglTerminate;
-   xdrv->Base.API.GetProcAddress = xlib_eglGetProcAddress;
-   xdrv->Base.API.CreateContext = xlib_eglCreateContext;
-   xdrv->Base.API.DestroyContext = xlib_eglDestroyContext;
-   xdrv->Base.API.CreateWindowSurface = xlib_eglCreateWindowSurface;
-   xdrv->Base.API.CreatePbufferSurface = xlib_eglCreatePbufferSurface;
-   xdrv->Base.API.DestroySurface = xlib_eglDestroySurface;
-   xdrv->Base.API.BindTexImage = xlib_eglBindTexImage;
-   xdrv->Base.API.ReleaseTexImage = xlib_eglReleaseTexImage;
-   xdrv->Base.API.MakeCurrent = xlib_eglMakeCurrent;
-   xdrv->Base.API.SwapBuffers = xlib_eglSwapBuffers;
-
-   xdrv->apis = find_supported_apis();
-   if (xdrv->apis == 0x0) {
-      /* the app isn't directly linked with any EGL-supprted APIs
-       * (such as libGLESv2.so) so use an EGL utility to see what
-       * APIs might be loaded dynamically on this system.
-       */
-      xdrv->apis = _eglFindAPIs();
-   }
-
-   xdrv->Base.Name = "Xlib/softpipe";
-   xdrv->Base.Unload = xlib_Unload;
-
-   return &xdrv->Base;
-}
diff --git a/src/gallium/winsys/egl_xlib/sw_winsys.c b/src/gallium/winsys/egl_xlib/sw_winsys.c
deleted file mode 100644
index 6ee3ede..0000000
--- a/src/gallium/winsys/egl_xlib/sw_winsys.c
+++ /dev/null
@@ -1,231 +0,0 @@
-/**************************************************************************
- * 
- * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
- * All Rights Reserved.
- * 
- * Permission is hereby granted, free of charge, to any person obtaining a
- * copy of this software and associated documentation files (the
- * "Software"), to deal in the Software without restriction, including
- * without limitation the rights to use, copy, modify, merge, publish,
- * distribute, sub license, and/or sell copies of the Software, and to
- * permit persons to whom the Software is furnished to do so, subject to
- * the following conditions:
- * 
- * The above copyright notice and this permission notice (including the
- * next paragraph) shall be included in all copies or substantial portions
- * of the Software.
- * 
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
- * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
- * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
- * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
- * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
- * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- * 
- **************************************************************************/
-
-/**
- * Totally software-based winsys layer.
- * Note that the one winsys function that we can't implement here
- * is flush_frontbuffer().
- * Whoever uses this code will have to provide that.
- *
- * Authors: Brian Paul
- */
-
-
-#include "pipe/internal/p_winsys_screen.h"
-#include "pipe/p_state.h"
-#include "pipe/p_inlines.h"
-#include "util/u_format.h"
-#include "util/u_math.h"
-#include "util/u_memory.h"
-
-#include "sw_winsys.h"
-
-
-
-/** Subclass of pipe_winsys */
-struct sw_pipe_winsys
-{
-   struct pipe_winsys Base;
-   /* no extra fields for now */
-};
-
-
-/** subclass of pipe_buffer */
-struct sw_pipe_buffer
-{
-   struct pipe_buffer Base;
-   boolean UserBuffer;  /** Is this a user-space buffer? */
-   void *Data;
-   void *Mapped;
-};
-
-
-/** cast wrapper */
-static INLINE struct sw_pipe_buffer *
-sw_pipe_buffer(struct pipe_buffer *b)
-{
-   return (struct sw_pipe_buffer *) b;
-}
-
-
-static const char *
-get_name(struct pipe_winsys *pws)
-{
-   return "software";
-}
-
-
-/** Create new pipe_buffer and allocate storage of given size */
-static struct pipe_buffer *
-buffer_create(struct pipe_winsys *pws, 
-              unsigned alignment, 
-              unsigned usage,
-              unsigned size)
-{
-   struct sw_pipe_buffer *buffer = CALLOC_STRUCT(sw_pipe_buffer);
-   if (!buffer)
-      return NULL;
-
-   pipe_reference_init(&buffer->Base.reference, 1);
-   buffer->Base.alignment = alignment;
-   buffer->Base.usage = usage;
-   buffer->Base.size = size;
-
-   /* align to 16-byte multiple for Cell */
-   buffer->Data = align_malloc(size, MAX2(alignment, 16));
-
-   return &buffer->Base;
-}
-
-
-/**
- * Create buffer which wraps user-space data.
- */
-static struct pipe_buffer *
-user_buffer_create(struct pipe_winsys *pws, void *ptr, unsigned bytes)
-{
-   struct sw_pipe_buffer *buffer = CALLOC_STRUCT(sw_pipe_buffer);
-   if (!buffer)
-      return NULL;
-
-   pipe_reference_init(&buffer->Base.reference, 1);
-   buffer->Base.size = bytes;
-   buffer->UserBuffer = TRUE;
-   buffer->Data = ptr;
-
-   return &buffer->Base;
-}
-
-
-static void *
-buffer_map(struct pipe_winsys *pws, struct pipe_buffer *buf, unsigned flags)
-{
-   struct sw_pipe_buffer *buffer = sw_pipe_buffer(buf);
-   buffer->Mapped = buffer->Data;
-   return buffer->Mapped;
-}
-
-
-static void
-buffer_unmap(struct pipe_winsys *pws, struct pipe_buffer *buf)
-{
-   struct sw_pipe_buffer *buffer = sw_pipe_buffer(buf);
-   buffer->Mapped = NULL;
-}
-
-
-static void
-buffer_destroy(struct pipe_buffer *buf)
-{
-   struct sw_pipe_buffer *buffer = sw_pipe_buffer(buf);
-
-   if (buffer->Data && !buffer->UserBuffer) {
-      align_free(buffer->Data);
-      buffer->Data = NULL;
-   }
-
-   free(buffer);
-}
-
-
-static struct pipe_buffer *
-surface_buffer_create(struct pipe_winsys *winsys,
-                      unsigned width, unsigned height,
-                      enum pipe_format format, 
-                      unsigned usage,
-                      unsigned tex_usage,
-                      unsigned *stride)
-{
-   const unsigned alignment = 64;
-   unsigned nblocksy;
-
-   nblocksy = util_format_get_nblocksy(format, height);
-   *stride = align(util_format_get_stride(format, width), alignment);
-
-   return winsys->buffer_create(winsys, alignment,
-                                usage,
-                                *stride * nblocksy);
-}
-
-
-static void
-fence_reference(struct pipe_winsys *sws, struct pipe_fence_handle **ptr,
-                struct pipe_fence_handle *fence)
-{
-   /* no-op */
-}
-
-
-static int
-fence_signalled(struct pipe_winsys *sws, struct pipe_fence_handle *fence,
-                unsigned flag)
-{
-   /* no-op */
-   return 0;
-}
-
-
-static int
-fence_finish(struct pipe_winsys *sws, struct pipe_fence_handle *fence,
-             unsigned flag)
-{
-   /* no-op */
-   return 0;
-}
-
-
-/**
- * Create/return a new pipe_winsys object.
- */
-struct pipe_winsys *
-create_sw_winsys(void)
-{
-   struct sw_pipe_winsys *ws = CALLOC_STRUCT(sw_pipe_winsys);
-   if (!ws)
-      return NULL;
-
-   /* Fill in this struct with callbacks that pipe will need to
-    * communicate with the window system, buffer manager, etc. 
-    */
-   ws->Base.buffer_create = buffer_create;
-   ws->Base.user_buffer_create = user_buffer_create;
-   ws->Base.buffer_map = buffer_map;
-   ws->Base.buffer_unmap = buffer_unmap;
-   ws->Base.buffer_destroy = buffer_destroy;
-
-   ws->Base.surface_buffer_create = surface_buffer_create;
-
-   ws->Base.fence_reference = fence_reference;
-   ws->Base.fence_signalled = fence_signalled;
-   ws->Base.fence_finish = fence_finish;
-
-   ws->Base.flush_frontbuffer = NULL; /* not implemented here! */
-
-   ws->Base.get_name = get_name;
-
-   return &ws->Base;
-}
diff --git a/src/gallium/winsys/egl_xlib/sw_winsys.h b/src/gallium/winsys/egl_xlib/sw_winsys.h
deleted file mode 100644
index f96c5a1..0000000
--- a/src/gallium/winsys/egl_xlib/sw_winsys.h
+++ /dev/null
@@ -1,40 +0,0 @@
-/**************************************************************************
- * 
- * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
- * All Rights Reserved.
- * 
- * Permission is hereby granted, free of charge, to any person obtaining a
- * copy of this software and associated documentation files (the
- * "Software"), to deal in the Software without restriction, including
- * without limitation the rights to use, copy, modify, merge, publish,
- * distribute, sub license, and/or sell copies of the Software, and to
- * permit persons to whom the Software is furnished to do so, subject to
- * the following conditions:
- * 
- * The above copyright notice and this permission notice (including the
- * next paragraph) shall be included in all copies or substantial portions
- * of the Software.
- * 
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
- * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
- * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
- * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
- * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
- * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- * 
- **************************************************************************/
-
-
-#ifndef SW_WINSYS_H
-#define SW_WINSYS_H
-
-
-struct pipe_winsys;
-
-
-extern struct pipe_winsys *
-create_sw_winsys(void);
-
-
-#endif /* SW_WINSYS_H */
diff --git a/src/gallium/winsys/xlib/xlib_softpipe.c b/src/gallium/winsys/xlib/xlib_softpipe.c
index f7c0099..bf78aca 100644
--- a/src/gallium/winsys/xlib/xlib_softpipe.c
+++ b/src/gallium/winsys/xlib/xlib_softpipe.c
@@ -63,7 +63,7 @@
 
    XImage *tempImage;
 #ifdef USE_XSHM
-   int shm;
+   boolean shm;         /** Is this a shared memory buffer? */
    XShmSegmentInfo shminfo;
 #endif
 };
@@ -152,7 +152,7 @@
                                   &b->shminfo,
                                   width, height);
    if (b->tempImage == NULL) {
-      b->shm = 0;
+      b->shm = FALSE;
       return;
    }
 
@@ -169,12 +169,12 @@
       mesaXErrorFlag = 0;
       XDestroyImage(b->tempImage);
       b->tempImage = NULL;
-      b->shm = 0;
+      b->shm = FALSE;
       (void) XSetErrorHandler(old_handler);
       return;
    }
 
-   b->shm = 1;
+   b->shm = TRUE;
 }
 
 #endif /* USE_XSHM */
@@ -204,6 +204,14 @@
 {
    struct xm_buffer *oldBuf = xm_buffer(buf);
 
+   /*
+    * Note oldBuf->data may point to one of three things:
+    * 1. XShm shared memory image data
+    * 2. User-provided (wrapped) memory, see xm_user_buffer_create()
+    * 3. Regular, malloc'd memory
+    * We need to be careful with freeing that data now.
+    */
+
    if (oldBuf->data) {
 #ifdef USE_XSHM
       if (oldBuf->shminfo.shmid >= 0) {
@@ -213,12 +221,20 @@
          oldBuf->shminfo.shmid = -1;
          oldBuf->shminfo.shmaddr = (char *) -1;
       }
-      else
+
+      if (oldBuf->shm) {
+         oldBuf->data = NULL;
+      }
+
+      if (oldBuf->tempImage) {
+         XDestroyImage(oldBuf->tempImage);
+         oldBuf->tempImage = NULL;
+      }
 #endif
-      {
-         if (!oldBuf->userBuffer) {
-            align_free(oldBuf->data);
-         }
+
+      if (oldBuf->data && !oldBuf->userBuffer) {
+         /* this was regular malloc'd memory */
+         align_free(oldBuf->data);
       }
 
       oldBuf->data = NULL;
@@ -327,10 +343,8 @@
    buffer->base.usage = usage;
    buffer->base.size = size;
 
-   if (buffer->data == NULL) {
-      /* align to 16-byte multiple for Cell */
-      buffer->data = align_malloc(size, max(alignment, 16));
-   }
+   /* align to 16-byte multiple for Cell */
+   buffer->data = align_malloc(size, max(alignment, 16));
 
    return &buffer->base;
 }
diff --git a/src/glx/x11/glxcurrent.c b/src/glx/x11/glxcurrent.c
index fae1bd9..50de7d6 100644
--- a/src/glx/x11/glxcurrent.c
+++ b/src/glx/x11/glxcurrent.c
@@ -162,6 +162,7 @@
    __glXSetCurrentContext(&dummyContext);
 #ifdef GLX_DIRECT_RENDERING
    _glapi_set_dispatch(NULL);   /* no-op functions */
+   _glapi_set_context(NULL);
 #endif
 }
 
diff --git a/src/mesa/SConscript b/src/mesa/SConscript
index bdcfffe..ea5bad2 100644
--- a/src/mesa/SConscript
+++ b/src/mesa/SConscript
@@ -251,6 +251,7 @@
 		'main/dispatch.c',
 		'glapi/glapi.c',
 		'glapi/glapi_getproc.c',
+		'glapi/glapi_nop.c',
 		'glapi/glthread.c',
 	]
 	
diff --git a/src/mesa/drivers/dri/common/spantmp2.h b/src/mesa/drivers/dri/common/spantmp2.h
index 447f3d1..c152226 100644
--- a/src/mesa/drivers/dri/common/spantmp2.h
+++ b/src/mesa/drivers/dri/common/spantmp2.h
@@ -400,7 +400,7 @@
 #  define READ_RGBA( rgba, _x, _y )				        \
      do {								\
         GLuint p = GET_VALUE(_x, _y);					\
-        *((uint32_t *) rgba) = (t << 8) | 0xff;				\
+        *((uint32_t *) rgba) = (p << 8) | 0xff;				\
      } while (0)
 # else
 #  define READ_RGBA( rgba, _x, _y )				        \
diff --git a/src/mesa/drivers/dri/fb/Makefile b/src/mesa/drivers/dri/fb/Makefile
index cf9b3a8..848e204 100644
--- a/src/mesa/drivers/dri/fb/Makefile
+++ b/src/mesa/drivers/dri/fb/Makefile
@@ -5,9 +5,6 @@
 
 LIBNAME = fb_dri.so
 
-ifeq ($(USING_EGL), 1)
-EGL_SOURCES = server/radeon_egl.c
-endif
 
 DRIVER_SOURCES = \
 	fb_dri.c \
diff --git a/src/mesa/drivers/dri/fb/fb_egl.c b/src/mesa/drivers/dri/fb/fb_egl.c
deleted file mode 100644
index 02e44bb..0000000
--- a/src/mesa/drivers/dri/fb/fb_egl.c
+++ /dev/null
@@ -1,881 +0,0 @@
-/*
- * Test egl driver for fb_dri.so
- */
-#include <assert.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <dirent.h> 
-#include <errno.h>
-#include <fcntl.h>
-#include <sys/ioctl.h>
-#include <sys/mman.h>
-#include <linux/fb.h>
-
-#include "utils.h"
-#include "buffers.h"
-#include "main/extensions.h"
-#include "main/framebuffer.h"
-#include "main/renderbuffer.h"
-#include "vbo/vbo.h"
-#include "swrast/swrast.h"
-#include "swrast_setup/swrast_setup.h"
-#include "tnl/tnl.h"
-#include "tnl/tcontext.h"
-#include "tnl/t_pipeline.h"
-#include "drivers/common/driverfuncs.h"
-#include "drirenderbuffer.h"
-
-#include "eglconfig.h"
-#include "eglmain/context.h"
-#include "egldisplay.h"
-#include "egldriver.h"
-#include "eglglobals.h"
-#include "eglmode.h"
-#include "eglscreen.h"
-#include "eglsurface.h"
-
-extern void
-fbSetSpanFunctions(driRenderbuffer *drb, const GLvisual *vis);
-
-/**
- * fb driver-specific driver class derived from _EGLDriver
- */
-typedef struct fb_driver
-{
-   _EGLDriver Base;  /* base class/object */
-   GLuint fbStuff;
-} fbDriver;
-
-/**
- * fb display-specific driver class derived from _EGLDisplay
- */
-typedef struct fb_display
-{
-   _EGLDisplay Base;  /* base class/object */
-   void *pFB;
-} fbDisplay;
-
-/**
- * fb driver-specific screen class derived from _EGLScreen
- */
-typedef struct fb_screen
-{
-   _EGLScreen Base;
-   char fb[NAME_MAX];
-} fbScreen;
-
-
-/**
- * fb driver-specific surface class derived from _EGLSurface
- */
-typedef struct fb_surface
-{
-   _EGLSurface Base;  /* base class/object */
-   struct gl_framebuffer *mesa_framebuffer;
-} fbSurface;
-
-
-/**
- * fb driver-specific context class derived from _EGLContext
- */
-typedef struct fb_context
-{
-   _EGLContext Base;  /* base class/object */
-   GLcontext *glCtx;
-   struct {
-      __DRIcontext *context;	
-      __DRIscreen *screen;	
-      __DRIdrawable *drawable; /* drawable bound to this ctx */
-   } dri;
-} fbContext, *fbContextPtr;
-
-#define FB_CONTEXT(ctx)		((fbContextPtr)(ctx->DriverCtx))
-
-
-static EGLBoolean
-fbFillInConfigs(_EGLDisplay *disp, unsigned pixel_bits, unsigned depth_bits,
-               unsigned stencil_bits, GLboolean have_back_buffer) {
-   _EGLConfig *configs;
-   _EGLConfig *c;
-   unsigned int i, num_configs;
-   unsigned int depth_buffer_factor;
-   unsigned int back_buffer_factor;
-   GLenum fb_format;
-   GLenum fb_type;
-
-   /* Right now GLX_SWAP_COPY_OML isn't supported, but it would be easy
-   * enough to add support.  Basically, if a context is created with an
-   * fbconfig where the swap method is GLX_SWAP_COPY_OML, pageflipping
-   * will never be used.
-   */
-   static const GLenum back_buffer_modes[] = {
-            GLX_NONE, GLX_SWAP_UNDEFINED_OML /*, GLX_SWAP_COPY_OML */
-         };
-
-   uint8_t depth_bits_array[2];
-   uint8_t stencil_bits_array[2];
-
-   depth_bits_array[0] = 0;
-   depth_bits_array[1] = depth_bits;
-
-   /* Just like with the accumulation buffer, always provide some modes
-   * with a stencil buffer.  It will be a sw fallback, but some apps won't
-   * care about that.
-   */
-   stencil_bits_array[0] = 0;
-   stencil_bits_array[1] = (stencil_bits == 0) ? 8 : stencil_bits;
-
-   depth_buffer_factor = ((depth_bits != 0) || (stencil_bits != 0)) ? 2 : 1;
-   back_buffer_factor = (have_back_buffer) ? 2 : 1;
-
-   num_configs = depth_buffer_factor * back_buffer_factor * 2;
-
-   if (pixel_bits == 16) {
-      fb_format = GL_RGB;
-      fb_type = GL_UNSIGNED_SHORT_5_6_5;
-   } else {
-      fb_format = GL_RGBA;
-      fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
-   }
-
-   configs = calloc(sizeof(*configs), num_configs);
-   c = configs;
-   if (!_eglFillInConfigs(c, fb_format, fb_type,
-                          depth_bits_array, stencil_bits_array, depth_buffer_factor,
-                          back_buffer_modes, back_buffer_factor,
-                          GLX_TRUE_COLOR)) {
-      fprintf(stderr, "[%s:%u] Error creating FBConfig!\n",
-               __func__, __LINE__);
-      return EGL_FALSE;
-   }
-
-   /* Mark the visual as slow if there are "fake" stencil bits.
-   */
-   for (i = 0, c = configs; i < num_configs; i++, c++) {
-      int stencil = GET_CONFIG_ATTRIB(c, EGL_STENCIL_SIZE);
-      if ((stencil != 0)  && (stencil != stencil_bits)) {
-         SET_CONFIG_ATTRIB(c, EGL_CONFIG_CAVEAT, EGL_SLOW_CONFIG);
-      }
-   }
-
-   for (i = 0, c = configs; i < num_configs; i++, c++)
-      _eglAddConfig(disp, c);
-      
-   free(configs);
-   
-   return EGL_TRUE;
-}
-
-static EGLBoolean
-fbSetupFramebuffer(fbDisplay *disp, char *fbdev) 
-{
-   int fd;
-   char dev[20];
-   struct fb_var_screeninfo varInfo;
-   struct fb_fix_screeninfo fixedInfo;
-   
-   snprintf(dev, sizeof(dev), "/dev/%s", fbdev);
-
-   /* open the framebuffer device */
-   fd = open(dev, O_RDWR);
-   if (fd < 0) {
-      fprintf(stderr, "Error opening %s: %s\n", fbdev, strerror(errno));
-      return EGL_FALSE;
-   }
-
-   /* get the original variable screen info */
-   if (ioctl(fd, FBIOGET_VSCREENINFO, &varInfo)) {
-      fprintf(stderr, "error: ioctl(FBIOGET_VSCREENINFO) failed: %s\n",
-               strerror(errno));
-      return EGL_FALSE;
-   }
-
-   /* Turn off hw accels (otherwise mmap of mmio region will be
-    * refused)
-    */
-   if (varInfo.accel_flags) {
-      varInfo.accel_flags = 0;
-      if (ioctl(fd, FBIOPUT_VSCREENINFO, &varInfo)) {
-         fprintf(stderr, "error: ioctl(FBIOPUT_VSCREENINFO) failed: %s\n",
-                  strerror(errno));
-         return EGL_FALSE;
-      }
-   }
-
-   /* Get the fixed screen info */
-   if (ioctl(fd, FBIOGET_FSCREENINFO, &fixedInfo)) {
-      fprintf(stderr, "error: ioctl(FBIOGET_FSCREENINFO) failed: %s\n",
-               strerror(errno));
-      return EGL_FALSE;
-   }
-   
-   if (fixedInfo.visual == FB_VISUAL_DIRECTCOLOR) {
-      struct fb_cmap cmap;
-      unsigned short red[256], green[256], blue[256];
-      int rcols = 1 << varInfo.red.length;
-      int gcols = 1 << varInfo.green.length;
-      int bcols = 1 << varInfo.blue.length;
-      int i;
-
-      cmap.start = 0;      
-      cmap.len = gcols;
-      cmap.red   = red;
-      cmap.green = green;
-      cmap.blue  = blue;
-      cmap.transp = NULL;
-
-      for (i = 0; i < rcols ; i++) 
-         red[i] = (65536/(rcols-1)) * i;
-
-      for (i = 0; i < gcols ; i++) 
-         green[i] = (65536/(gcols-1)) * i;
-
-      for (i = 0; i < bcols ; i++) 
-         blue[i] = (65536/(bcols-1)) * i;
-      
-      if (ioctl(fd, FBIOPUTCMAP, (void *) &cmap) < 0) {
-         fprintf(stderr, "ioctl(FBIOPUTCMAP) failed [%d]\n", i);
-         exit(1);
-      }
-   }
-
-   /* mmap the framebuffer into our address space */
-   if (!disp->pFB)
-      disp->pFB = (caddr_t)mmap(0,  /* start */
-                      fixedInfo.smem_len,  /* bytes */
-                      PROT_READ | PROT_WRITE,  /* prot */
-                      MAP_SHARED,  /* flags */
-                      fd,  /* fd */
-                      0); /* offset */ 
-   if (disp->pFB == (caddr_t)-1) {
-      fprintf(stderr, "error: unable to mmap framebuffer: %s\n",
-               strerror(errno));
-      return EGL_FALSE;
-   }
-   
-   return EGL_TRUE;
-}
-   
-const char *sysfs = "/sys/class/graphics";
-
-static EGLBoolean
-fbInitialize(_EGLDriver *drv, EGLDisplay dpy, EGLint *major, EGLint *minor)
-{
-   _EGLDisplay *disp = _eglLookupDisplay(dpy);
-   fbDisplay *display;
-   fbScreen *s;
-   _EGLScreen *scrn;
-   char c;
-   unsigned int x, y, r;
-   DIR *dir;
-   FILE *file;
-   struct dirent *dirent;
-   char path[NAME_MAX];
-   
-   /* Switch display structure to one with our private fields */
-   display = calloc(1, sizeof(*display));
-   display->Base = *disp;
-   _eglHashInsert(_eglGlobal.Displays, disp->Handle, display);
-   free(disp);
-   
-   *major = 1;
-   *minor = 0;
-   
-   dir = opendir(sysfs);
-   if (!dir) {
-      printf("EGL - %s framebuffer device not found.", sysfs);
-      return EGL_FALSE;
-   }
-   
-   while ((dirent = readdir(dir))) {  /* assignment! */
-      
-      if (dirent->d_name[0] != 'f')
-         continue;
-      if (dirent->d_name[1] != 'b')
-         continue;
-   
-      if (fbSetupFramebuffer(display, dirent->d_name) == EGL_FALSE)
-         continue;
-         
-      /* Create a screen */
-      s = (fbScreen *) calloc(1, sizeof(fbScreen));
-      if (!s)
-         return EGL_FALSE;
-
-      strncpy(s->fb, dirent->d_name, NAME_MAX);
-      scrn = &s->Base;
-      _eglInitScreen(scrn);
-      _eglAddScreen(&display->Base, scrn);
-      
-      snprintf(path, sizeof(path), "%s/%s/modes", sysfs, s->fb);
-      file = fopen(path, "r");
-      while (fgets(path, sizeof(path), file)) {
-         sscanf(path, "%c:%ux%u-%u", &c, &x, &y, &r);
-         _eglAddMode(scrn, x, y, r * 1000, path);
-      }
-      fclose(file);
-
-      fbFillInConfigs(&display->Base, 32, 24, 8, 1);
-      
-   }
-   closedir(dir);
-
-   drv->Initialized = EGL_TRUE;
-   return EGL_TRUE;
-}
-
-
-static fbDisplay *
-Lookup_fbDisplay(EGLDisplay dpy)
-{
-   _EGLDisplay *d = _eglLookupDisplay(dpy);
-   return (fbDisplay *) d;
-}
-
-
-static fbScreen *
-Lookup_fbScreen(EGLDisplay dpy, EGLScreenMESA screen)
-{
-   _EGLScreen *s = _eglLookupScreen(dpy, screen);
-   return (fbScreen *) s;
-}
-
-
-static fbContext *
-Lookup_fbContext(EGLContext ctx)
-{
-   _EGLContext *c = _eglLookupContext(ctx);
-   return (fbContext *) c;
-}
-
-
-static fbSurface *
-Lookup_fbSurface(EGLSurface surf)
-{
-   _EGLSurface *s = _eglLookupSurface(surf);
-   return (fbSurface *) s;
-}
-
-
-static EGLBoolean
-fbTerminate(_EGLDriver *drv, EGLDisplay dpy)
-{
-   fbDisplay *display = Lookup_fbDisplay(dpy);
-   _eglCleanupDisplay(&display->Base);
-   free(display);
-   free(drv);
-   return EGL_TRUE;
-}
-
-
-static const GLubyte *
-get_string(GLcontext *ctx, GLenum pname)
-{
-   (void) ctx;
-   switch (pname) {
-      case GL_RENDERER:
-         return (const GLubyte *) "Mesa dumb framebuffer";
-      default:
-         return NULL;
-   }
-}
-
-
-static void
-update_state( GLcontext *ctx, GLuint new_state )
-{
-   /* not much to do here - pass it on */
-   _swrast_InvalidateState( ctx, new_state );
-   _swsetup_InvalidateState( ctx, new_state );
-   _vbo_InvalidateState( ctx, new_state );
-   _tnl_InvalidateState( ctx, new_state );
-}
-
-
-/**
- * Called by ctx->Driver.GetBufferSize from in core Mesa to query the
- * current framebuffer size.
- */
-static void
-get_buffer_size( GLframebuffer *buffer, GLuint *width, GLuint *height )
-{
-   *width  = buffer->Width;
-   *height = buffer->Height;
-}
-
-
-static void
-updateFramebufferSize(GLcontext *ctx)
-{
-   fbContextPtr fbmesa = FB_CONTEXT(ctx);
-   struct gl_framebuffer *fb = ctx->WinSysDrawBuffer;
-   if (fbmesa->dri.drawable->w != fb->Width ||
-       fbmesa->dri.drawable->h != fb->Height) {
-      driUpdateFramebufferSize(ctx, fbmesa->dri.drawable);
-   }
-}
-
-static void
-viewport(GLcontext *ctx, GLint x, GLint y, GLsizei w, GLsizei h)
-{
-   /* XXX this should be called after we acquire the DRI lock, not here */
-   updateFramebufferSize(ctx);
-}
-
-
-static void
-init_core_functions( struct dd_function_table *functions )
-{
-   functions->GetString = get_string;
-   functions->UpdateState = update_state;
-   functions->GetBufferSize = get_buffer_size;
-   functions->Viewport = viewport;
-
-   functions->Clear = _swrast_Clear;  /* could accelerate with blits */
-}
-
-
-static EGLContext
-fbCreateContext(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config, EGLContext share_list, const EGLint *attrib_list)
-{
-   GLcontext *ctx;
-   _EGLConfig *conf;
-   fbContext *c;
-   _EGLDisplay *disp = _eglLookupDisplay(dpy);
-   struct dd_function_table functions;
-   GLvisual vis;
-   int i;
-
-   conf = _eglLookupConfig(drv, dpy, config);
-   if (!conf) {
-      _eglError(EGL_BAD_CONFIG, "eglCreateContext");
-      return EGL_NO_CONTEXT;
-   }
-
-   for (i = 0; attrib_list && attrib_list[i] != EGL_NONE; i++) {
-      switch (attrib_list[i]) {
-         /* no attribs defined for now */
-      default:
-         _eglError(EGL_BAD_ATTRIBUTE, "eglCreateContext");
-         return EGL_NO_CONTEXT;
-      }
-   }
-
-   c = (fbContext *) calloc(1, sizeof(fbContext));
-   if (!c)
-      return EGL_NO_CONTEXT;
-
-   _eglInitContext(&c->Base);
-   c->Base.Display = disp;
-   c->Base.Config = conf;
-   c->Base.DrawSurface = EGL_NO_SURFACE;
-   c->Base.ReadSurface = EGL_NO_SURFACE;
-
-   /* link to display */
-   _eglLinkContext(&c->Base, disp);
-   assert(c->Base.Handle);
-
-   /* Init default driver functions then plug in our FBdev-specific functions
-    */
-   _mesa_init_driver_functions(&functions);
-   init_core_functions(&functions);
-
-   _eglConfigToContextModesRec(conf, &vis);
-
-   ctx = c->glCtx = _mesa_create_context(&vis, NULL, &functions, (void *)c);
-   if (!c->glCtx) {
-      _mesa_free(c);
-      return GL_FALSE;
-   }
-
-   /* Create module contexts */
-   _swrast_CreateContext( ctx );
-   _vbo_CreateContext( ctx );
-   _tnl_CreateContext( ctx );
-   _swsetup_CreateContext( ctx );
-   _swsetup_Wakeup( ctx );
-
-
-   /* use default TCL pipeline */
-   {
-      TNLcontext *tnl = TNL_CONTEXT(ctx);
-      tnl->Driver.RunPipeline = _tnl_run_pipeline;
-   }
-
-   _mesa_enable_sw_extensions(ctx);
-
-   return c->Base.Handle;
-}
-
-
-static EGLSurface
-fbCreateWindowSurface(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config, NativeWindowType window, const EGLint *attrib_list)
-{
-   int i;
-   for (i = 0; attrib_list && attrib_list[i] != EGL_NONE; i++) {
-      switch (attrib_list[i]) {
-         /* no attribs at this time */
-      default:
-         _eglError(EGL_BAD_ATTRIBUTE, "eglCreateWindowSurface");
-         return EGL_NO_SURFACE;
-      }
-   }
-   printf("eglCreateWindowSurface()\n");
-   /* XXX unfinished */
-
-   return EGL_NO_SURFACE;
-}
-
-
-static EGLSurface
-fbCreatePixmapSurface(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config, NativePixmapType pixmap, const EGLint *attrib_list)
-{
-   _EGLConfig *conf;
-   EGLint i;
-
-   conf = _eglLookupConfig(drv, dpy, config);
-   if (!conf) {
-      _eglError(EGL_BAD_CONFIG, "eglCreatePixmapSurface");
-      return EGL_NO_SURFACE;
-   }
-
-   for (i = 0; attrib_list && attrib_list[i] != EGL_NONE; i++) {
-      switch (attrib_list[i]) {
-         /* no attribs at this time */
-      default:
-         _eglError(EGL_BAD_ATTRIBUTE, "eglCreatePixmapSurface");
-         return EGL_NO_SURFACE;
-      }
-   }
-
-   if (conf->Attrib[EGL_SURFACE_TYPE - FIRST_ATTRIB] == 0) {
-      _eglError(EGL_BAD_MATCH, "eglCreatePixmapSurface");
-      return EGL_NO_SURFACE;
-   }
-
-   printf("eglCreatePixmapSurface()\n");
-   return EGL_NO_SURFACE;
-}
-
-
-static EGLSurface
-fbCreatePbufferSurface(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config, const EGLint *attrib_list)
-{
-   fbSurface *surf;
-
-   surf = (fbSurface *) calloc(1, sizeof(fbSurface));
-   if (!surf) {
-      return EGL_NO_SURFACE;
-   }
-
-   if (_eglInitPbufferSurface(&surf->Base, drv, dpy, config, attrib_list) == EGL_NO_SURFACE) {
-      free(surf);
-      return EGL_NO_SURFACE;
-   }
-
-   /* create software-based pbuffer */
-   {
-      GLcontext *ctx = NULL; /* this _should_ be OK */
-      GLvisual vis;
-      _EGLConfig *conf = _eglLookupConfig(drv, dpy, config);
-      assert(conf); /* bad config should be caught earlier */
-      _eglConfigToContextModesRec(conf, &vis);
-
-      surf->mesa_framebuffer = _mesa_create_framebuffer(&vis);
-      _mesa_add_soft_renderbuffers(surf->mesa_framebuffer,
-                                   GL_TRUE, /* color bufs */
-                                   vis.haveDepthBuffer,
-                                   vis.haveStencilBuffer,
-                                   vis.haveAccumBuffer,
-                                   GL_FALSE, /* alpha */
-                                   GL_FALSE /* aux */ );
-
-      /* set pbuffer/framebuffer size */
-      _mesa_resize_framebuffer(ctx, surf->mesa_framebuffer,
-                               surf->Base.Width, surf->Base.Height);
-   }
-
-   return surf->Base.Handle;
-}
-
-
-static EGLBoolean
-fbDestroySurface(_EGLDriver *drv, EGLDisplay dpy, EGLSurface surface)
-{
-   fbSurface *fs = Lookup_fbSurface(surface);
-   _eglUnlinkSurface(&fs->Base);
-   if (!_eglIsSurfaceBound(&fs->Base))
-      free(fs);
-   return EGL_TRUE;
-}
-
-
-static EGLBoolean
-fbDestroyContext(_EGLDriver *drv, EGLDisplay dpy, EGLContext context)
-{
-   fbContext *fc = Lookup_fbContext(context);
-   _eglUnlinkContext(&fc->Base);
-   if (!_eglIsContextBound(&fc->Base))
-      free(fc);
-   return EGL_TRUE;
-}
-
-
-static EGLBoolean
-fbMakeCurrent(_EGLDriver *drv, EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext context)
-{
-   fbSurface *readSurf = Lookup_fbSurface(read);
-   fbSurface *drawSurf = Lookup_fbSurface(draw);
-   fbContext *ctx = Lookup_fbContext(context);
-   EGLBoolean b;
-
-   b = _eglMakeCurrent(drv, dpy, draw, read, context);
-   if (!b)
-      return EGL_FALSE;
-
-   if (ctx) {
-      _mesa_make_current( ctx->glCtx, 
-                           drawSurf->mesa_framebuffer,
-                           readSurf->mesa_framebuffer);
-   } else
-      _mesa_make_current( NULL, NULL, NULL );
-
-   return EGL_TRUE;
-}
-
-
-/**
- * Create a drawing surface which can be directly displayed on a screen.
- */
-static EGLSurface
-fbCreateScreenSurfaceMESA(_EGLDriver *drv, EGLDisplay dpy, EGLConfig cfg,
-                          const EGLint *attrib_list)
-{
-   _EGLConfig *config = _eglLookupConfig(drv, dpy, cfg);
-   fbDisplay *display = Lookup_fbDisplay(dpy);
-   fbSurface *surface;
-   EGLSurface surf;
-   GLvisual vis;
-   GLcontext *ctx = NULL; /* this should be OK */
-   int origin, bytesPerPixel;
-   int width, height, stride;
-   
-   surface = (fbSurface *) malloc(sizeof(*surface));
-   if (!surface) {
-      return EGL_NO_SURFACE;
-   }
-
-   /* init base class, error check, etc. */
-   surf = _eglInitScreenSurface(&surface->Base, drv, dpy, cfg, attrib_list);
-   if (surf == EGL_NO_SURFACE) {
-      free(surface);
-      return EGL_NO_SURFACE;
-   }
-
-   /* convert EGLConfig to GLvisual */
-   _eglConfigToContextModesRec(config, &vis);
-
-   /* create Mesa framebuffer */
-   surface->mesa_framebuffer = _mesa_create_framebuffer(&vis);
-   if (!surface->mesa_framebuffer) {
-      free(surface);
-      _eglUnlinkSurface(&surface->Base);
-      return EGL_NO_SURFACE;
-   }
-
-   width = surface->Base.Width;
-   height = surface->Base.Height;
-   bytesPerPixel = vis.rgbBits / 8;
-   stride = width * bytesPerPixel;
-   origin = 0;
-
-   /* front color renderbuffer */
-   {
-      driRenderbuffer *drb = driNewRenderbuffer(MESA_FORMAT_ARGB8888, display->pFB,
-                                                bytesPerPixel,
-                                                origin, stride, NULL);
-      fbSetSpanFunctions(drb, &vis);
-      _mesa_add_renderbuffer(surface->mesa_framebuffer,
-                             BUFFER_FRONT_LEFT, &drb->Base);
-   }
-
-   /* back color renderbuffer */
-   if (vis.doubleBufferMode) {
-      GLubyte *backBuf = _mesa_malloc(stride * height);
-      driRenderbuffer *drb = driNewRenderbuffer(MESA_FORMAT_ARGB8888, backBuf,
-                                                bytesPerPixel,
-                                                origin, stride, NULL);
-      fbSetSpanFunctions(drb, &vis);
-      _mesa_add_renderbuffer(surface->mesa_framebuffer,
-                             BUFFER_BACK_LEFT, &drb->Base);
-   }
-
-   /* other renderbuffers- software based */
-   _mesa_add_soft_renderbuffers(surface->mesa_framebuffer,
-                                GL_FALSE, /* color */
-                                vis.haveDepthBuffer,
-                                vis.haveStencilBuffer,
-                                vis.haveAccumBuffer,
-                                GL_FALSE, /* alpha */
-                                GL_FALSE /* aux */);
-   
-   _mesa_resize_framebuffer(ctx, surface->mesa_framebuffer, width, height);
-
-   return surf;
-}
-
-
-/**
- * Show the given surface on the named screen.
- * If surface is EGL_NO_SURFACE, disable the screen's output.
- */
-static EGLBoolean
-fbShowSurfaceMESA(_EGLDriver *drv, EGLDisplay dpy, EGLScreenMESA screen,
-                    EGLSurface surface, EGLModeMESA m)
-{
-   fbDisplay *display = Lookup_fbDisplay(dpy);
-   fbScreen *scrn = Lookup_fbScreen(dpy, screen);
-   fbSurface *surf = Lookup_fbSurface(surface);
-   FILE *file;
-   char buffer[NAME_MAX];
-   _EGLMode *mode = _eglLookupMode(dpy, m);
-   int bits;
-   
-   if (!_eglShowSurfaceMESA(drv, dpy, screen, surface, m))
-      return EGL_FALSE;
-      
-   snprintf(buffer, sizeof(buffer), "%s/%s/blank", sysfs, scrn->fb);
-   
-   file = fopen(buffer, "r+");
-   if (!file) {
-err:
-      printf("chown all fb sysfs attrib to allow write - %s\n", buffer);
-      return EGL_FALSE;
-   }
-   snprintf(buffer, sizeof(buffer), "%d", (m == EGL_NO_MODE_MESA ? VESA_POWERDOWN : VESA_VSYNC_SUSPEND));
-   fputs(buffer, file);
-   fclose(file);
-   
-   if (m == EGL_NO_MODE_MESA)
-      return EGL_TRUE;
-   
-   snprintf(buffer, sizeof(buffer), "%s/%s/mode", sysfs, scrn->fb);
-   
-   file = fopen(buffer, "r+");
-   if (!file)
-      goto err;
-   fputs(mode->Name, file);
-   fclose(file);
-   
-   snprintf(buffer, sizeof(buffer), "%s/%s/bits_per_pixel", sysfs, scrn->fb);
-   
-   file = fopen(buffer, "r+");
-   if (!file)
-      goto err;
-   bits = GET_CONFIG_ATTRIB(surf->Base.Config, EGL_BUFFER_SIZE);
-   snprintf(buffer, sizeof(buffer), "%d", bits);
-   fputs(buffer, file);
-   fclose(file);
-
-   fbSetupFramebuffer(display, scrn->fb);
-   
-   snprintf(buffer, sizeof(buffer), "%s/%s/blank", sysfs, scrn->fb);
-   
-   file = fopen(buffer, "r+");
-   if (!file)
-      goto err;
-      
-   snprintf(buffer, sizeof(buffer), "%d", VESA_NO_BLANKING);
-   fputs(buffer, file);
-   fclose(file);
-   
-   return EGL_TRUE;
-}
-
-
-/* If the backbuffer is on a videocard, this is extraordinarily slow!
- */
-static EGLBoolean
-fbSwapBuffers(_EGLDriver *drv, EGLDisplay dpy, EGLSurface draw)
-{
-   fbContext *context = (fbContext *)_eglGetCurrentContext();
-   fbSurface *fs =  Lookup_fbSurface(draw);
-   struct gl_renderbuffer * front_renderbuffer = fs->mesa_framebuffer->Attachment[BUFFER_FRONT_LEFT].Renderbuffer;
-   void *frontBuffer = front_renderbuffer->Data;
-   int currentPitch = ((driRenderbuffer *)front_renderbuffer)->pitch;
-   void *backBuffer = fs->mesa_framebuffer->Attachment[BUFFER_BACK_LEFT].Renderbuffer->Data;
-
-   if (!_eglSwapBuffers(drv, dpy, draw))
-      return EGL_FALSE;
-
-   if (context) {
-      GLcontext *ctx = context->glCtx;
-      
-      if (ctx->Visual.doubleBufferMode) {
-	 int i;
-	 int offset = 0;
-         char *tmp = _mesa_malloc(currentPitch);
-
-         _mesa_notifySwapBuffers( ctx );  /* flush pending rendering comands */
-
-         ASSERT(frontBuffer);
-         ASSERT(backBuffer);
-
-	 for (i = 0; i < fs->Base.Height; i++) {
-            _mesa_memcpy(tmp, (char *) backBuffer + offset,
-                         currentPitch);
-            _mesa_memcpy((char *) frontBuffer + offset, tmp,
-                          currentPitch);
-            offset += currentPitch;
-	 }
-	    
-	 _mesa_free(tmp);
-      }
-   }
-   else {
-      /* XXX this shouldn't be an error but we can't handle it for now */
-      _mesa_problem(NULL, "fbSwapBuffers: drawable has no context!\n");
-      return EGL_FALSE;
-   }
-   return EGL_TRUE;
-}
-
-
-/**
- * The bootstrap function.  Return a new fbDriver object and
- * plug in API functions.
- */
-_EGLDriver *
-_eglMain(_EGLDisplay *dpy)
-{
-   fbDriver *fb;
-
-   fb = (fbDriver *) calloc(1, sizeof(fbDriver));
-   if (!fb) {
-      return NULL;
-   }
-
-   /* First fill in the dispatch table with defaults */
-   _eglInitDriverFallbacks(&fb->Base);
-   
-   /* then plug in our fb-specific functions */
-   fb->Base.Initialize = fbInitialize;
-   fb->Base.Terminate = fbTerminate;
-   fb->Base.CreateContext = fbCreateContext;
-   fb->Base.MakeCurrent = fbMakeCurrent;
-   fb->Base.CreateWindowSurface = fbCreateWindowSurface;
-   fb->Base.CreatePixmapSurface = fbCreatePixmapSurface;
-   fb->Base.CreatePbufferSurface = fbCreatePbufferSurface;
-   fb->Base.DestroySurface = fbDestroySurface;
-   fb->Base.DestroyContext = fbDestroyContext;
-   fb->Base.CreateScreenSurfaceMESA = fbCreateScreenSurfaceMESA;
-   fb->Base.ShowSurfaceMESA = fbShowSurfaceMESA;
-   fb->Base.SwapBuffers = fbSwapBuffers;
-   
-   /* enable supported extensions */
-   fb->Base.MESA_screen_surface = EGL_TRUE;
-   fb->Base.MESA_copy_context = EGL_TRUE;
-
-   return &fb->Base;
-}
diff --git a/src/mesa/drivers/dri/ffb/ffb_bitmap.c b/src/mesa/drivers/dri/ffb/ffb_bitmap.c
index 611afdd..b71a552 100644
--- a/src/mesa/drivers/dri/ffb/ffb_bitmap.c
+++ b/src/mesa/drivers/dri/ffb/ffb_bitmap.c
@@ -30,7 +30,6 @@
 #include "ffb_lock.h"
 #include "ffb_bitmap.h"
 #include "swrast/swrast.h"
-#include "main/image.h"
 #include "main/macros.h"
 
 /* Compute ceiling of integer quotient of A divided by B: */
diff --git a/src/mesa/drivers/dri/ffb/ffb_clear.c b/src/mesa/drivers/dri/ffb/ffb_clear.c
index dfe60f3..aa3fa0a 100644
--- a/src/mesa/drivers/dri/ffb/ffb_clear.c
+++ b/src/mesa/drivers/dri/ffb/ffb_clear.c
@@ -26,15 +26,12 @@
  */
 
 #include "main/mtypes.h"
-#include "main/extensions.h"
 
 #include "main/mm.h"
 #include "ffb_dd.h"
 #include "ffb_span.h"
-#include "ffb_depth.h"
 #include "ffb_context.h"
 #include "ffb_vb.h"
-#include "ffb_tris.h"
 #include "ffb_clear.h"
 #include "ffb_lock.h"
 
diff --git a/src/mesa/drivers/dri/ffb/ffb_dd.c b/src/mesa/drivers/dri/ffb/ffb_dd.c
index cf83b91..91b6d31 100644
--- a/src/mesa/drivers/dri/ffb/ffb_dd.c
+++ b/src/mesa/drivers/dri/ffb/ffb_dd.c
@@ -27,13 +27,9 @@
 
 #include "main/mtypes.h"
 #include "main/mm.h"
-#include "main/extensions.h"
 #include "ffb_dd.h"
 #include "ffb_span.h"
-#include "ffb_depth.h"
 #include "ffb_context.h"
-#include "ffb_vb.h"
-#include "ffb_tris.h"
 #include "ffb_clear.h"
 #include "ffb_lock.h"
 
diff --git a/src/mesa/drivers/dri/ffb/ffb_depth.c b/src/mesa/drivers/dri/ffb/ffb_depth.c
index 5d509ff..d19385b 100644
--- a/src/mesa/drivers/dri/ffb/ffb_depth.c
+++ b/src/mesa/drivers/dri/ffb/ffb_depth.c
@@ -26,7 +26,6 @@
  */
 
 #include "main/mtypes.h"
-#include "swrast/swrast.h"
 #include "ffb_dd.h"
 #include "ffb_span.h"
 #include "ffb_context.h"
diff --git a/src/mesa/drivers/dri/ffb/ffb_lines.c b/src/mesa/drivers/dri/ffb/ffb_lines.c
index 19dff50..6dca4ed 100644
--- a/src/mesa/drivers/dri/ffb/ffb_lines.c
+++ b/src/mesa/drivers/dri/ffb/ffb_lines.c
@@ -27,15 +27,11 @@
 
 #include "main/mtypes.h"
 #include "main/mm.h"
-#include "main/extensions.h"
 #include "ffb_dd.h"
 #include "ffb_span.h"
-#include "ffb_depth.h"
 #include "ffb_context.h"
 #include "ffb_vb.h"
 #include "ffb_lines.h"
-#include "ffb_tris.h"
-#include "ffb_lock.h"
 
 #undef FFB_LINE_TRACE
 
diff --git a/src/mesa/drivers/dri/ffb/ffb_points.c b/src/mesa/drivers/dri/ffb/ffb_points.c
index 9c37a47..5bf4f8f 100644
--- a/src/mesa/drivers/dri/ffb/ffb_points.c
+++ b/src/mesa/drivers/dri/ffb/ffb_points.c
@@ -30,8 +30,6 @@
 #include "ffb_context.h"
 #include "ffb_vb.h"
 #include "ffb_points.h"
-#include "ffb_tris.h"
-#include "ffb_lock.h"
 
 
 #undef FFB_POINT_TRACE
diff --git a/src/mesa/drivers/dri/ffb/ffb_span.c b/src/mesa/drivers/dri/ffb/ffb_span.c
index 8ec33a1..61901cc 100644
--- a/src/mesa/drivers/dri/ffb/ffb_span.c
+++ b/src/mesa/drivers/dri/ffb/ffb_span.c
@@ -31,8 +31,6 @@
 #include "ffb_context.h"
 #include "ffb_lock.h"
 
-#include "swrast/swrast.h"
-
 #define DBG 0
 
 #define HW_LOCK()						\
diff --git a/src/mesa/drivers/dri/ffb/ffb_state.c b/src/mesa/drivers/dri/ffb/ffb_state.c
index 6f8a46d..c09d2fe 100644
--- a/src/mesa/drivers/dri/ffb/ffb_state.c
+++ b/src/mesa/drivers/dri/ffb/ffb_state.c
@@ -27,8 +27,6 @@
 
 #include "main/mtypes.h"
 #include "main/colormac.h"
-#include "main/mm.h"
-#include "main/extensions.h"
 #include "main/enums.h"
 
 #include "vbo/vbo.h"
@@ -39,12 +37,9 @@
 
 #include "ffb_dd.h"
 #include "ffb_span.h"
-#include "ffb_depth.h"
 #include "ffb_context.h"
-#include "ffb_vb.h"
 #include "ffb_tris.h"
 #include "ffb_state.h"
-#include "ffb_lock.h"
 
 #undef STATE_TRACE
 
diff --git a/src/mesa/drivers/dri/ffb/ffb_stencil.c b/src/mesa/drivers/dri/ffb/ffb_stencil.c
index ce8ef43..10cdfbc 100644
--- a/src/mesa/drivers/dri/ffb/ffb_stencil.c
+++ b/src/mesa/drivers/dri/ffb/ffb_stencil.c
@@ -32,8 +32,6 @@
 #include "ffb_stencil.h"
 #include "ffb_lock.h"
 
-#include "swrast/swrast.h"
-
 #undef STENCIL_TRACE
 
 static void FFBWriteStencilSpan( GLcontext *ctx,
diff --git a/src/mesa/drivers/dri/ffb/ffb_vb.c b/src/mesa/drivers/dri/ffb/ffb_vb.c
index f9c6fd1..ca8ffb2 100644
--- a/src/mesa/drivers/dri/ffb/ffb_vb.c
+++ b/src/mesa/drivers/dri/ffb/ffb_vb.c
@@ -30,8 +30,6 @@
 #include "ffb_vb.h"
 #include "main/imports.h"
 #include "tnl/t_context.h"
-#include "swrast_setup/swrast_setup.h"
-#include "math/m_translate.h"
 
 #undef VB_DEBUG
 
diff --git a/src/mesa/drivers/dri/ffb/ffb_xmesa.c b/src/mesa/drivers/dri/ffb/ffb_xmesa.c
index 88285f4..6a84651 100644
--- a/src/mesa/drivers/dri/ffb/ffb_xmesa.c
+++ b/src/mesa/drivers/dri/ffb/ffb_xmesa.c
@@ -28,7 +28,6 @@
 #include "ffb_xmesa.h"
 #include "main/context.h"
 #include "main/framebuffer.h"
-#include "main/matrix.h"
 #include "main/renderbuffer.h"
 #include "main/simple_list.h"
 #include "main/imports.h"
@@ -52,7 +51,6 @@
 #include "ffb_lines.h"
 #include "ffb_points.h"
 #include "ffb_state.h"
-#include "ffb_tex.h"
 #include "ffb_lock.h"
 #include "ffb_vtxfmt.h"
 #include "ffb_bitmap.h"
diff --git a/src/mesa/drivers/dri/i810/i810context.c b/src/mesa/drivers/dri/i810/i810context.c
index bd9cfe5..34e3460 100644
--- a/src/mesa/drivers/dri/i810/i810context.c
+++ b/src/mesa/drivers/dri/i810/i810context.c
@@ -34,10 +34,7 @@
 
 #include "main/glheader.h"
 #include "main/context.h"
-#include "main/matrix.h"
 #include "main/simple_list.h"
-#include "main/extensions.h"
-#include "main/framebuffer.h"
 #include "main/imports.h"
 #include "main/points.h"
 
diff --git a/src/mesa/drivers/dri/i810/i810render.c b/src/mesa/drivers/dri/i810/i810render.c
index 1d98e00..b543d4f 100644
--- a/src/mesa/drivers/dri/i810/i810render.c
+++ b/src/mesa/drivers/dri/i810/i810render.c
@@ -44,7 +44,6 @@
 
 #include "i810context.h"
 #include "i810tris.h"
-#include "i810state.h"
 #include "i810vb.h"
 #include "i810ioctl.h"
 
diff --git a/src/mesa/drivers/dri/i810/i810screen.c b/src/mesa/drivers/dri/i810/i810screen.c
index 2a30782..476c801 100644
--- a/src/mesa/drivers/dri/i810/i810screen.c
+++ b/src/mesa/drivers/dri/i810/i810screen.c
@@ -36,8 +36,6 @@
 #include "main/imports.h"
 #include "main/context.h"
 #include "main/framebuffer.h"
-#include "main/fbobject.h"
-#include "main/matrix.h"
 #include "main/renderbuffer.h"
 #include "main/simple_list.h"
 #include "utils.h"
@@ -48,8 +46,6 @@
 #include "i810state.h"
 #include "i810tex.h"
 #include "i810span.h"
-#include "i810tris.h"
-#include "i810ioctl.h"
 
 #include "GL/internal/dri_interface.h"
 
diff --git a/src/mesa/drivers/dri/i810/i810state.c b/src/mesa/drivers/dri/i810/i810state.c
index 642245c..0c68e12 100644
--- a/src/mesa/drivers/dri/i810/i810state.c
+++ b/src/mesa/drivers/dri/i810/i810state.c
@@ -20,8 +20,6 @@
 #include "i810context.h"
 #include "i810state.h"
 #include "i810tex.h"
-#include "i810vb.h"
-#include "i810tris.h"
 #include "i810ioctl.h"
 
 
diff --git a/src/mesa/drivers/dri/i810/i810tex.c b/src/mesa/drivers/dri/i810/i810tex.c
index e764644..2ccb956 100644
--- a/src/mesa/drivers/dri/i810/i810tex.c
+++ b/src/mesa/drivers/dri/i810/i810tex.c
@@ -33,7 +33,6 @@
 #include "main/colormac.h"
 #include "main/texobj.h"
 #include "main/mm.h"
-#include "swrast/swrast.h"
 
 #include "texmem.h"
 
@@ -42,7 +41,6 @@
 
 #include "i810context.h"
 #include "i810tex.h"
-#include "i810state.h"
 #include "i810ioctl.h"
 
 
diff --git a/src/mesa/drivers/dri/i810/i810texmem.c b/src/mesa/drivers/dri/i810/i810texmem.c
index d93afbf..bb426a4 100644
--- a/src/mesa/drivers/dri/i810/i810texmem.c
+++ b/src/mesa/drivers/dri/i810/i810texmem.c
@@ -35,7 +35,6 @@
 #include "i810_dri.h"
 #include "i810context.h"
 #include "i810tex.h"
-#include "i810state.h"
 #include "i810ioctl.h"
 
 
diff --git a/src/mesa/drivers/dri/i810/i810vb.c b/src/mesa/drivers/dri/i810/i810vb.c
index 30890dc..09a7722 100644
--- a/src/mesa/drivers/dri/i810/i810vb.c
+++ b/src/mesa/drivers/dri/i810/i810vb.c
@@ -38,7 +38,6 @@
 #include "i810context.h"
 #include "i810vb.h"
 #include "i810ioctl.h"
-#include "i810tris.h"
 #include "i810state.h"
 
 
diff --git a/src/mesa/drivers/dri/mach64/mach64_context.c b/src/mesa/drivers/dri/mach64/mach64_context.c
index 3b4ef7f..11bce31 100644
--- a/src/mesa/drivers/dri/mach64/mach64_context.c
+++ b/src/mesa/drivers/dri/mach64/mach64_context.c
@@ -33,8 +33,6 @@
 #include "main/context.h"
 #include "main/simple_list.h"
 #include "main/imports.h"
-#include "main/matrix.h"
-#include "main/extensions.h"
 
 #include "swrast/swrast.h"
 #include "swrast_setup/swrast_setup.h"
diff --git a/src/mesa/drivers/dri/mach64/mach64_dd.c b/src/mesa/drivers/dri/mach64/mach64_dd.c
index e400e9a..ca713e2 100644
--- a/src/mesa/drivers/dri/mach64/mach64_dd.c
+++ b/src/mesa/drivers/dri/mach64/mach64_dd.c
@@ -31,12 +31,9 @@
 
 #include "mach64_context.h"
 #include "mach64_ioctl.h"
-#include "mach64_state.h"
-#include "mach64_vb.h"
 #include "mach64_dd.h"
 
 #include "main/context.h"
-#include "main/framebuffer.h"
 
 #include "utils.h"
 
diff --git a/src/mesa/drivers/dri/mach64/mach64_lock.c b/src/mesa/drivers/dri/mach64/mach64_lock.c
index 8653c77..1a95a8f 100644
--- a/src/mesa/drivers/dri/mach64/mach64_lock.c
+++ b/src/mesa/drivers/dri/mach64/mach64_lock.c
@@ -32,7 +32,6 @@
 #include "mach64_context.h"
 #include "mach64_state.h"
 #include "mach64_lock.h"
-#include "mach64_tex.h"
 #include "drirenderbuffer.h"
 
 #if DEBUG_LOCKING
diff --git a/src/mesa/drivers/dri/mach64/mach64_screen.c b/src/mesa/drivers/dri/mach64/mach64_screen.c
index 1ed3b0b..5cbfb85 100644
--- a/src/mesa/drivers/dri/mach64/mach64_screen.c
+++ b/src/mesa/drivers/dri/mach64/mach64_screen.c
@@ -31,8 +31,6 @@
 
 #include "mach64_context.h"
 #include "mach64_ioctl.h"
-#include "mach64_tris.h"
-#include "mach64_vb.h"
 #include "mach64_span.h"
 
 #include "main/context.h"
diff --git a/src/mesa/drivers/dri/mach64/mach64_span.c b/src/mesa/drivers/dri/mach64/mach64_span.c
index b4ba2a4..0c52c0c 100644
--- a/src/mesa/drivers/dri/mach64/mach64_span.c
+++ b/src/mesa/drivers/dri/mach64/mach64_span.c
@@ -31,7 +31,6 @@
 
 #include "mach64_context.h"
 #include "mach64_ioctl.h"
-#include "mach64_state.h"
 #include "mach64_span.h"
 
 #include "swrast/swrast.h"
diff --git a/src/mesa/drivers/dri/mach64/mach64_state.c b/src/mesa/drivers/dri/mach64/mach64_state.c
index df7cbc8..b9093b5 100644
--- a/src/mesa/drivers/dri/mach64/mach64_state.c
+++ b/src/mesa/drivers/dri/mach64/mach64_state.c
@@ -36,7 +36,6 @@
 #include "mach64_vb.h"
 #include "mach64_tex.h"
 
-#include "main/context.h"
 #include "main/enums.h"
 #include "main/colormac.h"
 #include "swrast/swrast.h"
@@ -44,8 +43,6 @@
 #include "tnl/tnl.h"
 #include "swrast_setup/swrast_setup.h"
 
-#include "tnl/t_pipeline.h"
-
 
 /* =============================================================
  * Alpha blending
diff --git a/src/mesa/drivers/dri/mach64/mach64_tex.c b/src/mesa/drivers/dri/mach64/mach64_tex.c
index 6627d3c..1bce967 100644
--- a/src/mesa/drivers/dri/mach64/mach64_tex.c
+++ b/src/mesa/drivers/dri/mach64/mach64_tex.c
@@ -31,13 +31,8 @@
 
 #include "mach64_context.h"
 #include "mach64_ioctl.h"
-#include "mach64_state.h"
-#include "mach64_vb.h"
-#include "mach64_tris.h"
 #include "mach64_tex.h"
 
-#include "main/context.h"
-#include "main/macros.h"
 #include "main/simple_list.h"
 #include "main/enums.h"
 #include "main/texstore.h"
diff --git a/src/mesa/drivers/dri/mach64/mach64_texmem.c b/src/mesa/drivers/dri/mach64/mach64_texmem.c
index b97e9ee..46cee43 100644
--- a/src/mesa/drivers/dri/mach64/mach64_texmem.c
+++ b/src/mesa/drivers/dri/mach64/mach64_texmem.c
@@ -37,10 +37,7 @@
 #include "main/imports.h"
 
 #include "mach64_context.h"
-#include "mach64_state.h"
 #include "mach64_ioctl.h"
-#include "mach64_vb.h"
-#include "mach64_tris.h"
 #include "mach64_tex.h"
 
 
diff --git a/src/mesa/drivers/dri/mach64/mach64_texstate.c b/src/mesa/drivers/dri/mach64/mach64_texstate.c
index df0a09a..adf774e 100644
--- a/src/mesa/drivers/dri/mach64/mach64_texstate.c
+++ b/src/mesa/drivers/dri/mach64/mach64_texstate.c
@@ -36,8 +36,6 @@
 
 #include "mach64_context.h"
 #include "mach64_ioctl.h"
-#include "mach64_state.h"
-#include "mach64_vb.h"
 #include "mach64_tris.h"
 #include "mach64_tex.h"
 
diff --git a/src/mesa/drivers/dri/mach64/mach64_vb.c b/src/mesa/drivers/dri/mach64/mach64_vb.c
index e58812e..00da835 100644
--- a/src/mesa/drivers/dri/mach64/mach64_vb.c
+++ b/src/mesa/drivers/dri/mach64/mach64_vb.c
@@ -42,7 +42,6 @@
 #include "mach64_vb.h"
 #include "mach64_ioctl.h"
 #include "mach64_tris.h"
-#include "mach64_state.h"
 
 
 #define MACH64_TEX1_BIT       0x1
diff --git a/src/mesa/drivers/dri/r200/Makefile b/src/mesa/drivers/dri/r200/Makefile
index a93dbae..14eb96c 100644
--- a/src/mesa/drivers/dri/r200/Makefile
+++ b/src/mesa/drivers/dri/r200/Makefile
@@ -9,10 +9,6 @@
 
 MINIGLX_SOURCES = server/radeon_dri.c 
 
-ifeq ($(USING_EGL), 1)
-EGL_SOURCES = server/radeon_egl.c
-endif
-
 ifeq ($(RADEON_LDFLAGS),)
 CS_SOURCES = radeon_cs_space_drm.c radeon_bo.c radeon_cs.c
 endif
diff --git a/src/mesa/drivers/dri/r200/r200_state_init.c b/src/mesa/drivers/dri/r200/r200_state_init.c
index 6c5a0b7..80b08dc 100644
--- a/src/mesa/drivers/dri/r200/r200_state_init.c
+++ b/src/mesa/drivers/dri/r200/r200_state_init.c
@@ -698,7 +698,8 @@
    uint32_t dwords = atom->check(ctx, atom);
    int i = atom->idx;
    radeonTexObj *t = r200->state.texture.unit[i].texobj;
-   if (!r200->state.texture.unit[i].unitneeded)
+
+   if (!r200->state.texture.unit[i].unitneeded && !(dwords <= atom->cmd_size))
         dwords -= 4;
    BEGIN_BATCH_NO_AUTOSTATE(dwords);
 
diff --git a/src/mesa/drivers/dri/r200/server/radeon_egl.c b/src/mesa/drivers/dri/r200/server/radeon_egl.c
deleted file mode 120000
index d7735a7..0000000
--- a/src/mesa/drivers/dri/r200/server/radeon_egl.c
+++ /dev/null
@@ -1 +0,0 @@
-../../radeon/server/radeon_egl.c
\ No newline at end of file
diff --git a/src/mesa/drivers/dri/r300/Makefile b/src/mesa/drivers/dri/r300/Makefile
index acc4d4d..04459c2 100644
--- a/src/mesa/drivers/dri/r300/Makefile
+++ b/src/mesa/drivers/dri/r300/Makefile
@@ -9,10 +9,6 @@
 
 MINIGLX_SOURCES = server/radeon_dri.c
 
-ifeq ($(USING_EGL), 1)
-EGL_SOURCES = server/radeon_egl.c
-endif
-
 ifeq ($(RADEON_LDFLAGS),)
 CS_SOURCES = radeon_cs_space_drm.c radeon_bo.c radeon_cs.c
 endif
diff --git a/src/mesa/drivers/dri/r300/server/radeon_egl.c b/src/mesa/drivers/dri/r300/server/radeon_egl.c
deleted file mode 120000
index d7735a7..0000000
--- a/src/mesa/drivers/dri/r300/server/radeon_egl.c
+++ /dev/null
@@ -1 +0,0 @@
-../../radeon/server/radeon_egl.c
\ No newline at end of file
diff --git a/src/mesa/drivers/dri/r600/Makefile b/src/mesa/drivers/dri/r600/Makefile
index 8a45fc5..5d50941 100644
--- a/src/mesa/drivers/dri/r600/Makefile
+++ b/src/mesa/drivers/dri/r600/Makefile
@@ -9,10 +9,6 @@
 
 MINIGLX_SOURCES = server/radeon_dri.c
 
-ifeq ($(USING_EGL), 1)
-EGL_SOURCES = server/radeon_egl.c
-endif
-
 ifeq ($(RADEON_LDFLAGS),)
 CS_SOURCES = radeon_cs_space_drm.c radeon_bo.c radeon_cs.c
 endif
diff --git a/src/mesa/drivers/dri/r600/r600_texstate.c b/src/mesa/drivers/dri/r600/r600_texstate.c
index b8466bd..3289d89 100644
--- a/src/mesa/drivers/dri/r600/r600_texstate.c
+++ b/src/mesa/drivers/dri/r600/r600_texstate.c
@@ -85,6 +85,7 @@
 	CLEARfield(t->SQ_TEX_RESOURCE4, SQ_TEX_RESOURCE_WORD4_0__DST_SEL_Y_mask);
 	CLEARfield(t->SQ_TEX_RESOURCE4, SQ_TEX_RESOURCE_WORD4_0__DST_SEL_Z_mask);
 	CLEARfield(t->SQ_TEX_RESOURCE4, SQ_TEX_RESOURCE_WORD4_0__DST_SEL_W_mask);
+	CLEARbit(t->SQ_TEX_RESOURCE4, SQ_TEX_RESOURCE_WORD4_0__FORCE_DEGAMMA_bit);
 
 	SETfield(t->SQ_TEX_RESOURCE4, SQ_FORMAT_COMP_UNSIGNED,
 		 FORMAT_COMP_X_shift, FORMAT_COMP_X_mask);
@@ -95,6 +96,11 @@
 	SETfield(t->SQ_TEX_RESOURCE4, SQ_FORMAT_COMP_UNSIGNED,
 		 FORMAT_COMP_W_shift, FORMAT_COMP_W_mask);
 
+	CLEARbit(t->SQ_TEX_RESOURCE0, TILE_TYPE_bit);
+	SETfield(t->SQ_TEX_RESOURCE0, ARRAY_LINEAR_GENERAL,
+		 SQ_TEX_RESOURCE_WORD0_0__TILE_MODE_shift,
+		 SQ_TEX_RESOURCE_WORD0_0__TILE_MODE_mask);
+
 	switch (mesa_format) /* This is mesa format. */
 	{
 	case MESA_FORMAT_RGBA8888:
@@ -158,6 +164,32 @@
 		SETfield(t->SQ_TEX_RESOURCE4, SQ_SEL_W,
 			 SQ_TEX_RESOURCE_WORD4_0__DST_SEL_W_shift, SQ_TEX_RESOURCE_WORD4_0__DST_SEL_W_mask);
 		break;
+	case MESA_FORMAT_XRGB8888:
+		SETfield(t->SQ_TEX_RESOURCE1, FMT_8_8_8_8,
+			 SQ_TEX_RESOURCE_WORD1_0__DATA_FORMAT_shift, SQ_TEX_RESOURCE_WORD1_0__DATA_FORMAT_mask);
+
+		SETfield(t->SQ_TEX_RESOURCE4, SQ_SEL_Z,
+			 SQ_TEX_RESOURCE_WORD4_0__DST_SEL_X_shift, SQ_TEX_RESOURCE_WORD4_0__DST_SEL_X_mask);
+		SETfield(t->SQ_TEX_RESOURCE4, SQ_SEL_Y,
+			 SQ_TEX_RESOURCE_WORD4_0__DST_SEL_Y_shift, SQ_TEX_RESOURCE_WORD4_0__DST_SEL_Y_mask);
+		SETfield(t->SQ_TEX_RESOURCE4, SQ_SEL_X,
+			 SQ_TEX_RESOURCE_WORD4_0__DST_SEL_Z_shift, SQ_TEX_RESOURCE_WORD4_0__DST_SEL_Z_mask);
+		SETfield(t->SQ_TEX_RESOURCE4, SQ_SEL_1,
+			 SQ_TEX_RESOURCE_WORD4_0__DST_SEL_W_shift, SQ_TEX_RESOURCE_WORD4_0__DST_SEL_W_mask);
+		break;
+	case MESA_FORMAT_XRGB8888_REV:
+		SETfield(t->SQ_TEX_RESOURCE1, FMT_8_8_8_8,
+			 SQ_TEX_RESOURCE_WORD1_0__DATA_FORMAT_shift, SQ_TEX_RESOURCE_WORD1_0__DATA_FORMAT_mask);
+
+		SETfield(t->SQ_TEX_RESOURCE4, SQ_SEL_1,
+			 SQ_TEX_RESOURCE_WORD4_0__DST_SEL_X_shift, SQ_TEX_RESOURCE_WORD4_0__DST_SEL_X_mask);
+		SETfield(t->SQ_TEX_RESOURCE4, SQ_SEL_Z,
+			 SQ_TEX_RESOURCE_WORD4_0__DST_SEL_Y_shift, SQ_TEX_RESOURCE_WORD4_0__DST_SEL_Y_mask);
+		SETfield(t->SQ_TEX_RESOURCE4, SQ_SEL_W,
+			 SQ_TEX_RESOURCE_WORD4_0__DST_SEL_Z_shift, SQ_TEX_RESOURCE_WORD4_0__DST_SEL_Z_mask);
+		SETfield(t->SQ_TEX_RESOURCE4, SQ_SEL_X,
+			 SQ_TEX_RESOURCE_WORD4_0__DST_SEL_W_shift, SQ_TEX_RESOURCE_WORD4_0__DST_SEL_W_mask);
+		break;
 	case MESA_FORMAT_ARGB8888_REV:
 		SETfield(t->SQ_TEX_RESOURCE1, FMT_8_8_8_8,
 			 SQ_TEX_RESOURCE_WORD1_0__DATA_FORMAT_shift, SQ_TEX_RESOURCE_WORD1_0__DATA_FORMAT_mask);
@@ -515,6 +547,10 @@
 	case MESA_FORMAT_Z24_S8:
 	case MESA_FORMAT_Z32:
 	case MESA_FORMAT_S8:
+		SETbit(t->SQ_TEX_RESOURCE0, TILE_TYPE_bit);
+		SETfield(t->SQ_TEX_RESOURCE0, ARRAY_1D_TILED_THIN1,
+			 SQ_TEX_RESOURCE_WORD0_0__TILE_MODE_shift,
+			 SQ_TEX_RESOURCE_WORD0_0__TILE_MODE_mask);
 		switch (mesa_format) {
 		case MESA_FORMAT_Z16:
 			SETfield(t->SQ_TEX_RESOURCE1, FMT_16,
@@ -651,6 +687,12 @@
    }
 }
 
+static INLINE uint32_t
+S_FIXED(float value, uint32_t frac_bits)
+{
+   return value * (1 << frac_bits);
+}
+
 void r600SetDepthTexMode(struct gl_texture_object *tObj)
 {
 	radeonTexObjPtr t;
@@ -670,8 +712,9 @@
  * \param rmesa Context pointer
  * \param t the r300 texture object
  */
-static void setup_hardware_state(context_t *rmesa, struct gl_texture_object *texObj)
+static void setup_hardware_state(GLcontext * ctx, struct gl_texture_object *texObj, int unit)
 {
+	context_t *rmesa = R700_CONTEXT(ctx);
 	radeonTexObj *t = radeon_tex_obj(texObj);
 	const struct gl_texture_image *firstImage;
 	GLuint uTexelPitch, row_align;
@@ -733,11 +776,21 @@
 
 	t->SQ_TEX_RESOURCE2 = get_base_teximage_offset(t) / 256;
 
-	if ((t->maxLod - t->minLod) > 0) {
-		t->SQ_TEX_RESOURCE3 = radeon_miptree_image_offset(t->mt, 0, t->minLod + 1) / 256;
-		SETfield(t->SQ_TEX_RESOURCE4, 0, BASE_LEVEL_shift, BASE_LEVEL_mask);
-		SETfield(t->SQ_TEX_RESOURCE5, t->maxLod - t->minLod, LAST_LEVEL_shift, LAST_LEVEL_mask);
-	}
+	t->SQ_TEX_RESOURCE3 = radeon_miptree_image_offset(t->mt, 0, t->minLod + 1) / 256;
+
+	SETfield(t->SQ_TEX_RESOURCE4, 0, BASE_LEVEL_shift, BASE_LEVEL_mask);
+	SETfield(t->SQ_TEX_RESOURCE5, t->maxLod - t->minLod, LAST_LEVEL_shift, LAST_LEVEL_mask);
+
+	SETfield(t->SQ_TEX_SAMPLER1,
+		S_FIXED(CLAMP(t->base.MinLod - t->minLod, 0, 15), 6),
+		MIN_LOD_shift, MIN_LOD_mask);
+	SETfield(t->SQ_TEX_SAMPLER1,
+		S_FIXED(CLAMP(t->base.MaxLod - t->minLod, 0, 15), 6),
+		MAX_LOD_shift, MAX_LOD_mask);
+	SETfield(t->SQ_TEX_SAMPLER1,
+		S_FIXED(CLAMP(ctx->Texture.Unit[unit].LodBias + t->base.LodBias, -16, 16), 6),
+		SQ_TEX_SAMPLER_WORD1_0__LOD_BIAS_shift, SQ_TEX_SAMPLER_WORD1_0__LOD_BIAS_mask);
+
 	if(texObj->CompareMode == GL_COMPARE_R_TO_TEXTURE_ARB)
 	{
 		SETfield(t->SQ_TEX_SAMPLER0, r600_translate_shadow_func(texObj->CompareFunc), DEPTH_COMPARE_FUNCTION_shift, DEPTH_COMPARE_FUNCTION_mask);
@@ -754,9 +807,8 @@
  *
  * Mostly this means populating the texture object's mipmap tree.
  */
-static GLboolean r600_validate_texture(GLcontext * ctx, struct gl_texture_object *texObj)
+static GLboolean r600_validate_texture(GLcontext * ctx, struct gl_texture_object *texObj, int unit)
 {
-	context_t *rmesa = R700_CONTEXT(ctx);
 	radeonTexObj *t = radeon_tex_obj(texObj);
 
 	if (!radeon_validate_texture_miptree(ctx, texObj))
@@ -764,7 +816,7 @@
 
 	/* Configure the hardware registers (more precisely, the cached version
 	 * of the hardware registers). */
-	setup_hardware_state(rmesa, texObj);
+	setup_hardware_state(ctx, texObj, unit);
 
 	t->validated = GL_TRUE;
 	return GL_TRUE;
@@ -805,7 +857,7 @@
 		if (!ctx->Texture.Unit[i]._ReallyEnabled)
 			continue;
 
-		if (!r600_validate_texture(ctx, ctx->Texture.Unit[i]._Current)) {
+		if (!r600_validate_texture(ctx, ctx->Texture.Unit[i]._Current, i)) {
 			radeon_warning("failed to validate texture for unit %d.\n", i);
 		}
 		t = radeon_tex_obj(ctx->Texture.Unit[i]._Current);
diff --git a/src/mesa/drivers/dri/r600/r700_chip.c b/src/mesa/drivers/dri/r600/r700_chip.c
index 1a1a87c..ef25cd9 100644
--- a/src/mesa/drivers/dri/r600/r700_chip.c
+++ b/src/mesa/drivers/dri/r600/r700_chip.c
@@ -303,14 +303,13 @@
     R600_STATECHANGE(context, cb_target);
 
     /* color buffer */
-    r700->render_target[id].CB_COLOR0_BASE.u32All = context->radeon.state.color.draw_offset;
+    r700->render_target[id].CB_COLOR0_BASE.u32All = context->radeon.state.color.draw_offset / 256;
 
     nPitchInPixel = rrb->pitch/rrb->cpp;
     SETfield(r700->render_target[id].CB_COLOR0_SIZE.u32All, (nPitchInPixel/8)-1,
              PITCH_TILE_MAX_shift, PITCH_TILE_MAX_mask);
     SETfield(r700->render_target[id].CB_COLOR0_SIZE.u32All, ( (nPitchInPixel * context->radeon.radeonScreen->driScreen->fbHeight)/64 )-1,
              SLICE_TILE_MAX_shift, SLICE_TILE_MAX_mask);
-    r700->render_target[id].CB_COLOR0_BASE.u32All = 0;
     SETfield(r700->render_target[id].CB_COLOR0_INFO.u32All, ENDIAN_NONE, ENDIAN_shift, ENDIAN_mask);
     SETfield(r700->render_target[id].CB_COLOR0_INFO.u32All, ARRAY_LINEAR_GENERAL,
              CB_COLOR0_INFO__ARRAY_MODE_shift, CB_COLOR0_INFO__ARRAY_MODE_mask);
diff --git a/src/mesa/drivers/dri/r600/r700_state.c b/src/mesa/drivers/dri/r600/r700_state.c
index 3c8cb57..20e8afe 100644
--- a/src/mesa/drivers/dri/r600/r700_state.c
+++ b/src/mesa/drivers/dri/r600/r700_state.c
@@ -59,6 +59,7 @@
 static void r700UpdatePolygonMode(GLcontext * ctx);
 static void r700SetPolygonOffsetState(GLcontext * ctx, GLboolean state);
 static void r700SetStencilState(GLcontext * ctx, GLboolean state);
+static void r700UpdateWindow(GLcontext * ctx, int id);
 
 void r700UpdateShaders(GLcontext * ctx)
 {
@@ -780,6 +781,9 @@
 	case GL_LINE_STIPPLE:
 		r700UpdateLineStipple(ctx);
 		break;
+	case GL_DEPTH_CLAMP:
+		r700UpdateWindow(ctx, 0);
+		break;
 	default:
 		break;
 	}
@@ -1576,9 +1580,9 @@
     SETbit(r700->sq_config.SQ_CONFIG.u32All, DX9_CONSTS_bit);
     SETbit(r700->sq_config.SQ_CONFIG.u32All, ALU_INST_PREFER_VECTOR_bit);
     SETfield(r700->sq_config.SQ_CONFIG.u32All, ps_prio, PS_PRIO_shift, PS_PRIO_mask);
-    SETfield(r700->sq_config.SQ_CONFIG.u32All, ps_prio, VS_PRIO_shift, VS_PRIO_mask);
-    SETfield(r700->sq_config.SQ_CONFIG.u32All, ps_prio, GS_PRIO_shift, GS_PRIO_mask);
-    SETfield(r700->sq_config.SQ_CONFIG.u32All, ps_prio, ES_PRIO_shift, ES_PRIO_mask);
+    SETfield(r700->sq_config.SQ_CONFIG.u32All, vs_prio, VS_PRIO_shift, VS_PRIO_mask);
+    SETfield(r700->sq_config.SQ_CONFIG.u32All, gs_prio, GS_PRIO_shift, GS_PRIO_mask);
+    SETfield(r700->sq_config.SQ_CONFIG.u32All, es_prio, ES_PRIO_shift, ES_PRIO_mask);
 
     r700->sq_config.SQ_GPR_RESOURCE_MGMT_1.u32All = 0;
     SETfield(r700->sq_config.SQ_GPR_RESOURCE_MGMT_1.u32All, num_ps_gprs, NUM_PS_GPRS_shift, NUM_PS_GPRS_mask);
diff --git a/src/mesa/drivers/dri/r600/server/radeon_egl.c b/src/mesa/drivers/dri/r600/server/radeon_egl.c
deleted file mode 120000
index d7735a7..0000000
--- a/src/mesa/drivers/dri/r600/server/radeon_egl.c
+++ /dev/null
@@ -1 +0,0 @@
-../../radeon/server/radeon_egl.c
\ No newline at end of file
diff --git a/src/mesa/drivers/dri/radeon/radeon_fbo.c b/src/mesa/drivers/dri/radeon/radeon_fbo.c
index 7b1f84a..e780b9e 100644
--- a/src/mesa/drivers/dri/radeon/radeon_fbo.c
+++ b/src/mesa/drivers/dri/radeon/radeon_fbo.c
@@ -531,10 +531,9 @@
                                             att->TextureLevel);
 
    if (att->Texture->Target == GL_TEXTURE_3D) {
-      GLuint offsets[6];
-      radeon_miptree_depth_offsets(radeon_image->mt, att->TextureLevel,
-				   offsets);
-      imageOffset += offsets[att->Zoffset];
+      imageOffset += radeon_image->mt->levels[att->TextureLevel].rowstride *
+                     radeon_image->mt->levels[att->TextureLevel].height *
+                     att->Zoffset;
    }
 
    /* store that offset in the region, along with the correct pitch for
diff --git a/src/mesa/drivers/dri/radeon/radeon_mipmap_tree.c b/src/mesa/drivers/dri/radeon/radeon_mipmap_tree.c
index 033f26d..a1a5239 100644
--- a/src/mesa/drivers/dri/radeon/radeon_mipmap_tree.c
+++ b/src/mesa/drivers/dri/radeon/radeon_mipmap_tree.c
@@ -177,6 +177,12 @@
 
 		for(face = 0; face < mt->faces; face++)
 			compute_tex_image_offset(rmesa, mt, face, level, &curOffset);
+		/* r600 cube levels seems to be aligned to 8 faces but
+		 * we have separate register for 1'st level offset so add
+		 * 2 image alignment after 1'st mip level */
+		if(rmesa->radeonScreen->chip_family >= CHIP_FAMILY_R600 &&
+		   mt->target == GL_TEXTURE_CUBE_MAP && level >= 1)
+			curOffset += 2 * mt->levels[level].size;
 	}
 
 	/* Note the required size in memory */
@@ -380,25 +386,6 @@
 		texImg->Depth, t->tile_bits);
 }
 
-/* Although we use the image_offset[] array to store relative offsets
- * to cube faces, Mesa doesn't know anything about this and expects
- * each cube face to be treated as a separate image.
- *
- * These functions present that view to mesa:
- */
-void
-radeon_miptree_depth_offsets(radeon_mipmap_tree *mt, GLuint level, GLuint *offsets)
-{
-	if (mt->target != GL_TEXTURE_3D || mt->faces == 1) {
-		offsets[0] = 0;
-	} else {
-		int i;
-		for (i = 0; i < 6; i++) {
-			offsets[i] = mt->levels[level].faces[i].offset;
-		}
-	}
-}
-
 GLuint
 radeon_miptree_image_offset(radeon_mipmap_tree *mt,
 			    GLuint face, GLuint level)
@@ -619,4 +606,4 @@
 	} else {
 		return radeon_miptree_image_offset(texObj->mt, 0, texObj->minLod);
 	}
-}
\ No newline at end of file
+}
diff --git a/src/mesa/drivers/dri/radeon/radeon_mipmap_tree.h b/src/mesa/drivers/dri/radeon/radeon_mipmap_tree.h
index a10649b..c911688 100644
--- a/src/mesa/drivers/dri/radeon/radeon_mipmap_tree.h
+++ b/src/mesa/drivers/dri/radeon/radeon_mipmap_tree.h
@@ -88,7 +88,5 @@
 void radeon_try_alloc_miptree(radeonContextPtr rmesa, radeonTexObj *t);
 GLuint radeon_miptree_image_offset(radeon_mipmap_tree *mt,
 				   GLuint face, GLuint level);
-void radeon_miptree_depth_offsets(radeon_mipmap_tree *mt, GLuint level, GLuint *offsets);
-
 uint32_t get_base_teximage_offset(radeonTexObj *texObj);
 #endif /* __RADEON_MIPMAP_TREE_H_ */
diff --git a/src/mesa/drivers/dri/radeon/radeon_texture.c b/src/mesa/drivers/dri/radeon/radeon_texture.c
index 20a27ad..4538e74 100644
--- a/src/mesa/drivers/dri/radeon/radeon_texture.c
+++ b/src/mesa/drivers/dri/radeon/radeon_texture.c
@@ -233,8 +233,15 @@
 
 void radeonGenerateMipmap(GLcontext* ctx, GLenum target, struct gl_texture_object *texObj)
 {
+	radeonContextPtr rmesa = RADEON_CONTEXT(ctx);
+	struct radeon_bo *bo;
 	GLuint face = _mesa_tex_target_to_face(target);
 	radeon_texture_image *baseimage = get_radeon_texture_image(texObj->Image[face][texObj->BaseLevel]);
+	bo = !baseimage->mt ? baseimage->bo : baseimage->mt->bo;
+
+	if (bo && radeon_bo_is_referenced_by_cs(bo, rmesa->cmdbuf.cs)) {
+		radeon_firevertices(rmesa);
+	}
 
 	radeon_teximage_map(baseimage, GL_FALSE);
 	radeon_generate_mipmap(ctx, target, texObj);
diff --git a/src/mesa/drivers/dri/radeon/server/radeon_egl.c b/src/mesa/drivers/dri/radeon/server/radeon_egl.c
deleted file mode 100644
index c16d66e..0000000
--- a/src/mesa/drivers/dri/radeon/server/radeon_egl.c
+++ /dev/null
@@ -1,1088 +0,0 @@
-/*
- * EGL driver for radeon_dri.so
- */
-#include <assert.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <dirent.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <sys/ioctl.h>
-#include <sys/mman.h>
-
-#include "eglconfig.h"
-#include "eglcontext.h"
-#include "egldisplay.h"
-#include "egldriver.h"
-#include "eglglobals.h"
-#include "egllog.h"
-#include "eglmode.h"
-#include "eglscreen.h"
-#include "eglsurface.h"
-#include "egldri.h"
-
-#include "mtypes.h"
-#include "memops.h"
-#include "drm.h"
-#include "drm_sarea.h"
-#include "radeon_drm.h"
-#include "radeon_dri.h"
-#include "radeon.h"
-
-static size_t radeon_drm_page_size;
-
-/**
- * radeon driver-specific driver class derived from _EGLDriver
- */
-typedef struct radeon_driver
-{
-   _EGLDriver Base;  /* base class/object */
-   GLuint radeonStuff;
-} radeonDriver;
-
-static int
-RADEONSetParam(driDisplay  *disp, int param, int value)
-{
-   drm_radeon_setparam_t sp;
-   int ret;
-
-   memset(&sp, 0, sizeof(sp));
-   sp.param = param;
-   sp.value = value;
-
-   if ((ret=drmCommandWrite(disp->drmFD, DRM_RADEON_SETPARAM, &sp, sizeof(sp)))) {
-     fprintf(stderr,"Set param failed\n", ret);
-      return -1;
-   }
-
-   return 0;
-}
-
-static int
-RADEONCheckDRMVersion(driDisplay *disp, RADEONInfoPtr info)
-{
-   drmVersionPtr  version;
-
-   version = drmGetVersion(disp->drmFD);
-   if (version) {
-      int req_minor, req_patch;
-
-      /* Need 1.21.x for card type detection getparam
-       */
-      req_minor = 21;
-      req_patch = 0;
-
-      if (version->version_major != 1 ||
-          version->version_minor < req_minor ||
-          (version->version_minor == req_minor &&
-           version->version_patchlevel < req_patch)) {
-         /* Incompatible drm version */
-         fprintf(stderr,
-                 "[dri] RADEONDRIScreenInit failed because of a version "
-                 "mismatch.\n"
-                 "[dri] radeon.o kernel module version is %d.%d.%d "
-                 "but version 1.%d.%d or newer is needed.\n"
-                 "[dri] Disabling DRI.\n",
-                 version->version_major,
-                 version->version_minor,
-                 version->version_patchlevel,
-                 req_minor,
-                 req_patch);
-         drmFreeVersion(version);
-         return 0;
-      }
-
-      info->drmMinor = version->version_minor;
-      drmFreeVersion(version);
-   }
-
-   return 1;
-}
-
-
-/**
- * \brief Compute base 2 logarithm.
- *
- * \param val value.
- *
- * \return base 2 logarithm of \p val.
- */
-static int RADEONMinBits(int val)
-{
-   int  bits;
-
-   if (!val) return 1;
-   for (bits = 0; val; val >>= 1, ++bits);
-   return bits;
-}
-
-
-/* Initialize the PCI GART state.  Request memory for use in PCI space,
- * and initialize the Radeon registers to point to that memory.
- */
-static int RADEONDRIPciInit(driDisplay *disp, RADEONInfoPtr info)
-{
-    int  ret;
-    int  flags = DRM_READ_ONLY | DRM_LOCKED | DRM_KERNEL;
-    int            s, l;
-
-    ret = drmScatterGatherAlloc(disp->drmFD, info->gartSize*1024*1024,
-                                &info->gartMemHandle);
-    if (ret < 0) {
-        fprintf(stderr, "[pci] Out of memory (%d)\n", ret);
-        return 0;
-    }
-    fprintf(stderr,
-               "[pci] %d kB allocated with handle 0x%04lx\n",
-            info->gartSize*1024, (long) info->gartMemHandle);
-
-   info->gartOffset = 0;
-
-   /* Initialize the CP ring buffer data */
-   info->ringStart       = info->gartOffset;
-   info->ringMapSize     = info->ringSize*1024*1024 + radeon_drm_page_size;
-
-   info->ringReadOffset  = info->ringStart + info->ringMapSize;
-   info->ringReadMapSize = radeon_drm_page_size;
-
-   /* Reserve space for vertex/indirect buffers */
-   info->bufStart        = info->ringReadOffset + info->ringReadMapSize;
-   info->bufMapSize      = info->bufSize*1024*1024;
-
-   /* Reserve the rest for AGP textures */
-   info->gartTexStart     = info->bufStart + info->bufMapSize;
-   s = (info->gartSize*1024*1024 - info->gartTexStart);
-   l = RADEONMinBits((s-1) / RADEON_NR_TEX_REGIONS);
-   if (l < RADEON_LOG_TEX_GRANULARITY) l = RADEON_LOG_TEX_GRANULARITY;
-   info->gartTexMapSize   = (s >> l) << l;
-   info->log2GARTTexGran  = l;
-
-    if (drmAddMap(disp->drmFD, info->ringStart, info->ringMapSize,
-                  DRM_SCATTER_GATHER, flags, &info->ringHandle) < 0) {
-        fprintf(stderr,
-                   "[pci] Could not add ring mapping\n");
-        return 0;
-    }
-    fprintf(stderr,
-               "[pci] ring handle = 0x%08lx\n", info->ringHandle);
-
-    if (drmAddMap(disp->drmFD, info->ringReadOffset, info->ringReadMapSize,
-                  DRM_SCATTER_GATHER, flags, &info->ringReadPtrHandle) < 0) {
-        fprintf(stderr,
-                   "[pci] Could not add ring read ptr mapping\n");
-        return 0;
-    }
-    fprintf(stderr,
-               "[pci] ring read ptr handle = 0x%08lx\n",
-               info->ringReadPtrHandle);
-
-    if (drmAddMap(disp->drmFD, info->bufStart, info->bufMapSize,
-                  DRM_SCATTER_GATHER, 0, &info->bufHandle) < 0) {
-        fprintf(stderr,
-                   "[pci] Could not add vertex/indirect buffers mapping\n");
-        return 0;
-    }
-    fprintf(stderr,
-               "[pci] vertex/indirect buffers handle = 0x%08lx\n",
-               info->bufHandle);
-
-    if (drmAddMap(disp->drmFD, info->gartTexStart, info->gartTexMapSize,
-                  DRM_SCATTER_GATHER, 0, &info->gartTexHandle) < 0) {
-        fprintf(stderr,
-                   "[pci] Could not add GART texture map mapping\n");
-        return 0;
-    }
-    fprintf(stderr,
-               "[pci] GART texture map handle = 0x%08lx\n",
-               info->gartTexHandle);
-
-    return 1;
-}
-
-
-/**
- * \brief Initialize the AGP state
- *
- * \param ctx display handle.
- * \param info driver private data.
- *
- * \return one on success, or zero on failure.
- *
- * Acquires and enables the AGP device. Reserves memory in the AGP space for
- * the ring buffer, vertex buffers and textures. Initialize the Radeon
- * registers to point to that memory and add client mappings.
- */
-static int RADEONDRIAgpInit( driDisplay *disp, RADEONInfoPtr info)
-{
-   int            mode, ret;
-   int            s, l;
-   int agpmode = 1;
-
-   if (drmAgpAcquire(disp->drmFD) < 0) {
-      fprintf(stderr, "[gart] AGP not available\n");
-      return 0;
-   }
-
-   mode = drmAgpGetMode(disp->drmFD);	/* Default mode */
-   /* Disable fast write entirely - too many lockups.
-    */
-   mode &= ~RADEON_AGP_MODE_MASK;
-   switch (agpmode) {
-   case 4:          mode |= RADEON_AGP_4X_MODE;
-   case 2:          mode |= RADEON_AGP_2X_MODE;
-   case 1: default: mode |= RADEON_AGP_1X_MODE;
-   }
-
-   if (drmAgpEnable(disp->drmFD, mode) < 0) {
-      fprintf(stderr, "[gart] AGP not enabled\n");
-      drmAgpRelease(disp->drmFD);
-      return 0;
-   }
-
-#if 0
-   /* Workaround for some hardware bugs */
-   if (info->ChipFamily < CHIP_FAMILY_R200)
-      OUTREG(RADEON_AGP_CNTL, INREG(RADEON_AGP_CNTL) | 0x000e0000);
-#endif
-   info->gartOffset = 0;
-
-   if ((ret = drmAgpAlloc(disp->drmFD, info->gartSize*1024*1024, 0, NULL,
-                          &info->gartMemHandle)) < 0) {
-      fprintf(stderr, "[gart] Out of memory (%d)\n", ret);
-      drmAgpRelease(disp->drmFD);
-      return 0;
-   }
-   fprintf(stderr,
-           "[gart] %d kB allocated with handle 0x%08x\n",
-           info->gartSize*1024, (unsigned)info->gartMemHandle);
-
-   if (drmAgpBind(disp->drmFD,
-                  info->gartMemHandle, info->gartOffset) < 0) {
-      fprintf(stderr, "[gart] Could not bind\n");
-      drmAgpFree(disp->drmFD, info->gartMemHandle);
-      drmAgpRelease(disp->drmFD);
-      return 0;
-   }
-
-   /* Initialize the CP ring buffer data */
-   info->ringStart       = info->gartOffset;
-   info->ringMapSize     = info->ringSize*1024*1024 + radeon_drm_page_size;
-
-   info->ringReadOffset  = info->ringStart + info->ringMapSize;
-   info->ringReadMapSize = radeon_drm_page_size;
-
-   /* Reserve space for vertex/indirect buffers */
-   info->bufStart        = info->ringReadOffset + info->ringReadMapSize;
-   info->bufMapSize      = info->bufSize*1024*1024;
-
-   /* Reserve the rest for AGP textures */
-   info->gartTexStart     = info->bufStart + info->bufMapSize;
-   s = (info->gartSize*1024*1024 - info->gartTexStart);
-   l = RADEONMinBits((s-1) / RADEON_NR_TEX_REGIONS);
-   if (l < RADEON_LOG_TEX_GRANULARITY) l = RADEON_LOG_TEX_GRANULARITY;
-   info->gartTexMapSize   = (s >> l) << l;
-   info->log2GARTTexGran  = l;
-
-   if (drmAddMap(disp->drmFD, info->ringStart, info->ringMapSize,
-                 DRM_AGP, DRM_READ_ONLY, &info->ringHandle) < 0) {
-      fprintf(stderr, "[gart] Could not add ring mapping\n");
-      return 0;
-   }
-   fprintf(stderr, "[gart] ring handle = 0x%08lx\n", info->ringHandle);
-
-
-   if (drmAddMap(disp->drmFD, info->ringReadOffset, info->ringReadMapSize,
-                 DRM_AGP, DRM_READ_ONLY, &info->ringReadPtrHandle) < 0) {
-      fprintf(stderr,
-              "[gart] Could not add ring read ptr mapping\n");
-      return 0;
-   }
-
-   fprintf(stderr,
-           "[gart] ring read ptr handle = 0x%08lx\n",
-           info->ringReadPtrHandle);
-
-   if (drmAddMap(disp->drmFD, info->bufStart, info->bufMapSize,
-                 DRM_AGP, 0, &info->bufHandle) < 0) {
-      fprintf(stderr,
-              "[gart] Could not add vertex/indirect buffers mapping\n");
-      return 0;
-   }
-   fprintf(stderr,
-           "[gart] vertex/indirect buffers handle = 0x%08lx\n",
-           info->bufHandle);
-
-   if (drmAddMap(disp->drmFD, info->gartTexStart, info->gartTexMapSize,
-                 DRM_AGP, 0, &info->gartTexHandle) < 0) {
-      fprintf(stderr,
-              "[gart] Could not add AGP texture map mapping\n");
-      return 0;
-   }
-   fprintf(stderr,
-           "[gart] AGP texture map handle = 0x%08lx\n",
-           info->gartTexHandle);
-
-   return 1;
-}
-
-
-/**
- * Initialize all the memory-related fields of the RADEONInfo object.
- * This includes the various 'offset' and 'size' fields.
- */
-static int
-RADEONMemoryInit(driDisplay *disp, RADEONInfoPtr info)
-{
-   int        width_bytes = disp->virtualWidth * disp->cpp;
-   int        cpp         = disp->cpp;
-   int        bufferSize  = ((disp->virtualHeight * width_bytes
-                              + RADEON_BUFFER_ALIGN)
-                             & ~RADEON_BUFFER_ALIGN);
-   int        depthSize   = ((((disp->virtualHeight+15) & ~15) * width_bytes
-                              + RADEON_BUFFER_ALIGN)
-                             & ~RADEON_BUFFER_ALIGN);
-   int        l;
-   int        pcie_gart_table_size = 0;
-
-   info->frontOffset = 0;
-   info->frontPitch = disp->virtualWidth;
-
-   if (disp->card_type==RADEON_CARD_PCIE)
-     pcie_gart_table_size  = RADEON_PCIGART_TABLE_SIZE;
-
-   /* Front, back and depth buffers - everything else texture??
-    */
-   info->textureSize = disp->fbSize - pcie_gart_table_size - 2 * bufferSize - depthSize;
-
-   if (info->textureSize < 0)
-      return 0;
-
-   l = RADEONMinBits((info->textureSize-1) / RADEON_NR_TEX_REGIONS);
-   if (l < RADEON_LOG_TEX_GRANULARITY) l = RADEON_LOG_TEX_GRANULARITY;
-
-   /* Round the texture size up to the nearest whole number of
-    * texture regions.  Again, be greedy about this, don't
-    * round down.
-    */
-   info->log2TexGran = l;
-   info->textureSize = (info->textureSize >> l) << l;
-
-   /* Set a minimum usable local texture heap size.  This will fit
-    * two 256x256x32bpp textures.
-    */
-   if (info->textureSize < 512 * 1024) {
-      info->textureOffset = 0;
-      info->textureSize = 0;
-   }
-
-   /* Reserve space for textures */
-   info->textureOffset = ((disp->fbSize - pcie_gart_table_size - info->textureSize +
-                           RADEON_BUFFER_ALIGN) &
-                          ~RADEON_BUFFER_ALIGN);
-
-   /* Reserve space for the shared depth
-    * buffer.
-    */
-   info->depthOffset = ((info->textureOffset - depthSize +
-                         RADEON_BUFFER_ALIGN) &
-                        ~RADEON_BUFFER_ALIGN);
-   info->depthPitch = disp->virtualWidth;
-
-   info->backOffset = ((info->depthOffset - bufferSize +
-                        RADEON_BUFFER_ALIGN) &
-                       ~RADEON_BUFFER_ALIGN);
-   info->backPitch = disp->virtualWidth;
-
-   if (pcie_gart_table_size)
-     info->pcieGartTableOffset = disp->fbSize - pcie_gart_table_size;
-
-   fprintf(stderr,
-           "Will use back buffer at offset 0x%x, pitch %d\n",
-           info->backOffset, info->backPitch);
-   fprintf(stderr,
-           "Will use depth buffer at offset 0x%x, pitch %d\n",
-           info->depthOffset, info->depthPitch);
-   fprintf(stderr,
-           "Will use %d kb for textures at offset 0x%x\n",
-           info->textureSize/1024, info->textureOffset);
-   if (pcie_gart_table_size)
-   { 
-     fprintf(stderr,
-	     "Will use %d kb for PCIE GART Table at offset 0x%x\n",
-	     pcie_gart_table_size/1024, info->pcieGartTableOffset);
-   }
-
-   /* XXX I don't think these are needed. */
-#if 0
-   info->frontPitchOffset = (((info->frontPitch * cpp / 64) << 22) |
-                             (info->frontOffset >> 10));
-
-   info->backPitchOffset = (((info->backPitch * cpp / 64) << 22) |
-                            (info->backOffset >> 10));
-
-   info->depthPitchOffset = (((info->depthPitch * cpp / 64) << 22) |
-                             (info->depthOffset >> 10));
-#endif
-
-   if (pcie_gart_table_size)
-     RADEONSetParam(disp, RADEON_SETPARAM_PCIGART_LOCATION, info->pcieGartTableOffset);
-
-   return 1;
-}
-
-
-/**
- * \brief Initialize the kernel data structures and enable the CP engine.
- *
- * \param ctx display handle.
- * \param info driver private data.
- *
- * \return non-zero on success, or zero on failure.
- *
- * This function is a wrapper around the DRM_RADEON_CP_INIT command, passing
- * all the parameters in a drm_radeon_init_t structure.
- */
-static int RADEONDRIKernelInit( driDisplay *disp,
-                               RADEONInfoPtr info)
-{
-   int cpp = disp->bpp / 8;
-   drm_radeon_init_t  drmInfo;
-   int ret;
-
-   memset(&drmInfo, 0, sizeof(drmInfo));
-
-   if ( (info->ChipFamily >= CHIP_FAMILY_R300) )
-      drmInfo.func            = RADEON_INIT_R300_CP;
-   else if ( (info->ChipFamily == CHIP_FAMILY_R200) ||
-        (info->ChipFamily == CHIP_FAMILY_RV250) ||
-        (info->ChipFamily == CHIP_FAMILY_M9) ||
-        (info->ChipFamily == CHIP_FAMILY_RV280) )
-      drmInfo.func             = RADEON_INIT_R200_CP;
-   else
-      drmInfo.func             = RADEON_INIT_CP;
-
-   /* This is the struct passed to the kernel module for its initialization */
-   /* XXX problem here:
-    * The front/back/depth_offset/pitch fields may change depending upon
-    * which drawing surface we're using!!!  They can't be set just once
-    * during initialization.
-    * Looks like we'll need a new ioctl to update these fields for drawing
-    * to other surfaces...
-    */
-   drmInfo.sarea_priv_offset   = sizeof(drm_sarea_t);
-   drmInfo.cp_mode             = RADEON_DEFAULT_CP_BM_MODE;
-   drmInfo.gart_size            = info->gartSize*1024*1024;
-   drmInfo.ring_size           = info->ringSize*1024*1024;
-   drmInfo.usec_timeout        = 1000;
-   drmInfo.fb_bpp              = disp->bpp;
-   drmInfo.depth_bpp           = disp->bpp;
-   drmInfo.front_offset        = info->frontOffset;
-   drmInfo.front_pitch         = info->frontPitch * cpp;
-   drmInfo.back_offset         = info->backOffset;
-   drmInfo.back_pitch          = info->backPitch * cpp;
-   drmInfo.depth_offset        = info->depthOffset;
-   drmInfo.depth_pitch         = info->depthPitch * cpp;
-   drmInfo.ring_offset         = info->ringHandle;
-   drmInfo.ring_rptr_offset    = info->ringReadPtrHandle;
-   drmInfo.buffers_offset      = info->bufHandle;
-   drmInfo.gart_textures_offset = info->gartTexHandle;
-
-   ret = drmCommandWrite(disp->drmFD, DRM_RADEON_CP_INIT, &drmInfo,
-                         sizeof(drm_radeon_init_t));
-
-   return ret >= 0;
-}
-
-
-/**
- * \brief Add a map for the vertex buffers that will be accessed by any
- * DRI-based clients.
- *
- * \param ctx display handle.
- * \param info driver private data.
- *
- * \return one on success, or zero on failure.
- *
- * Calls drmAddBufs() with the previously allocated vertex buffers.
- */
-static int RADEONDRIBufInit( driDisplay *disp, RADEONInfoPtr info )
-{
-   /* Initialize vertex buffers */
-   info->bufNumBufs = drmAddBufs(disp->drmFD,
-                                 info->bufMapSize / RADEON_BUFFER_SIZE,
-                                 RADEON_BUFFER_SIZE,
-				 (disp->card_type!=RADEON_CARD_AGP) ? DRM_SG_BUFFER : DRM_AGP_BUFFER,
-                                 info->bufStart);
-
-   if (info->bufNumBufs <= 0) {
-      fprintf(stderr,
-              "[drm] Could not create vertex/indirect buffers list\n");
-      return 0;
-   }
-   fprintf(stderr,
-           "[drm] Added %d %d byte vertex/indirect buffers\n",
-           info->bufNumBufs, RADEON_BUFFER_SIZE);
-
-   return 1;
-}
-
-
-/**
- * \brief Install an IRQ handler.
- *
- * \param disp display handle.
- * \param info driver private data.
- *
- * Attempts to install an IRQ handler via drmCtlInstHandler(), falling back to
- * IRQ-free operation on failure.
- */
-static void RADEONDRIIrqInit(driDisplay *disp, RADEONInfoPtr info)
-{
-   if ((drmCtlInstHandler(disp->drmFD, 0)) != 0)
-      fprintf(stderr, "[drm] failure adding irq handler, "
-                 "there is a device already using that irq\n"
-                 "[drm] falling back to irq-free operation\n");
-}
-
-
-/**
- * \brief Initialize the AGP heap.
- *
- * \param disp display handle.
- * \param info driver private data.
- *
- * This function is a wrapper around the DRM_RADEON_INIT_HEAP command, passing
- * all the parameters in a drm_radeon_mem_init_heap structure.
- */
-static void RADEONDRIAgpHeapInit(driDisplay *disp,
-                                 RADEONInfoPtr info)
-{
-   drm_radeon_mem_init_heap_t drmHeap;
-
-   /* Start up the simple memory manager for gart space */
-   drmHeap.region = RADEON_MEM_REGION_GART;
-   drmHeap.start  = 0;
-   drmHeap.size   = info->gartTexMapSize;
-
-   if (drmCommandWrite(disp->drmFD, DRM_RADEON_INIT_HEAP,
-                       &drmHeap, sizeof(drmHeap))) {
-      fprintf(stderr,
-              "[drm] Failed to initialized gart heap manager\n");
-   } else {
-      fprintf(stderr,
-              "[drm] Initialized kernel gart heap manager, %d\n",
-              info->gartTexMapSize);
-   }
-}
-
-static int RADEONGetCardType(driDisplay *disp, RADEONInfoPtr info)
-{
-   drm_radeon_getparam_t gp;  
-   int ret;
- 
-   gp.param = RADEON_PARAM_CARD_TYPE;
-   gp.value = &disp->card_type;
-
-   ret=drmCommandWriteRead(disp->drmFD, DRM_RADEON_GETPARAM, &gp, sizeof(gp));
-   if (ret) {
-     fprintf(stderr, "drm_radeon_getparam_t (RADEON_PARAM_CARD_TYPE) : %d\n", ret);
-     return -1;
-   }
-
-   return disp->card_type;
-}
-
-/**
- * Called at the start of each server generation.
- *
- * \param disp display handle.
- * \param info driver private data.
- *
- * \return non-zero on success, or zero on failure.
- *
- * Performs static frame buffer allocation. Opens the DRM device and add maps
- * to the SAREA, framebuffer and MMIO regions. Fills in \p info with more
- * information. Creates a \e server context to grab the lock for the
- * initialization ioctls and calls the other initilization functions in this
- * file. Starts the CP engine via the DRM_RADEON_CP_START command.
- *
- * Setups a RADEONDRIRec structure to be passed to radeon_dri.so for its
- * initialization.
- */
-static int
-RADEONScreenInit( driDisplay *disp, RADEONInfoPtr info,
-                  RADEONDRIPtr pRADEONDRI)
-{
-   int i, err;
-
-   /* XXX this probably isn't needed here */
-   {
-      int  width_bytes = (disp->virtualWidth * disp->cpp);
-      int  maxy        = disp->fbSize / width_bytes;
-
-      if (maxy <= disp->virtualHeight * 3) {
-         _eglLog(_EGL_WARNING,
-                 "Static buffer allocation failed -- "
-                 "need at least %d kB video memory (have %d kB)\n",
-                 (disp->virtualWidth * disp->virtualHeight *
-                  disp->cpp * 3 + 1023) / 1024,
-                 disp->fbSize / 1024);
-         return 0;
-      }
-   }
-
-   /* Memory manager setup */
-   if (!RADEONMemoryInit(disp, info)) {
-      return 0;
-   }
-
-   /* Create a 'server' context so we can grab the lock for
-    * initialization ioctls.
-    */
-   if ((err = drmCreateContext(disp->drmFD, &disp->serverContext)) != 0) {
-      _eglLog(_EGL_WARNING, "%s: drmCreateContext failed %d\n",
-              __FUNCTION__, err);
-      return 0;
-   }
-
-   DRM_LOCK(disp->drmFD, disp->pSAREA, disp->serverContext, 0);
-
-   /* Initialize the kernel data structures */
-   if (!RADEONDRIKernelInit(disp, info)) {
-      _eglLog(_EGL_WARNING, "RADEONDRIKernelInit failed\n");
-      DRM_UNLOCK(disp->drmFD, disp->pSAREA, disp->serverContext);
-      return 0;
-   }
-
-   /* Initialize the vertex buffers list */
-   if (!RADEONDRIBufInit(disp, info)) {
-      fprintf(stderr, "RADEONDRIBufInit failed\n");
-      DRM_UNLOCK(disp->drmFD, disp->pSAREA, disp->serverContext);
-      return 0;
-   }
-
-   /* Initialize IRQ */
-   RADEONDRIIrqInit(disp, info);
-
-   /* Initialize kernel gart memory manager */
-   RADEONDRIAgpHeapInit(disp, info);
-
-   /* Initialize the SAREA private data structure */
-   {
-      drm_radeon_sarea_t *pSAREAPriv;
-      pSAREAPriv = (drm_radeon_sarea_t *)(((char*)disp->pSAREA) +
-                                        sizeof(drm_sarea_t));
-      memset(pSAREAPriv, 0, sizeof(*pSAREAPriv));
-      pSAREAPriv->pfState = info->page_flip_enable;
-   }
-
-   for ( i = 0;; i++ ) {
-      drmMapType type;
-      drmMapFlags flags;
-      drm_handle_t handle, offset;
-      drmSize size;
-      int rc, mtrr;
-
-      if ( ( rc = drmGetMap( disp->drmFD, i, &offset, &size, &type, &flags, &handle, &mtrr ) ) != 0 )
-         break;
-      if ( type == DRM_REGISTERS ) {
-         pRADEONDRI->registerHandle = offset;
-         pRADEONDRI->registerSize = size;
-         break;
-      }
-   }
-   /* Quick hack to clear the front & back buffers.  Could also use
-    * the clear ioctl to do this, but would need to setup hw state
-    * first.
-    */
-   drimemsetio((char *)disp->pFB + info->frontOffset,
-          0xEE,
-          info->frontPitch * disp->cpp * disp->virtualHeight );
-
-   drimemsetio((char *)disp->pFB + info->backOffset,
-          0x30,
-          info->backPitch * disp->cpp * disp->virtualHeight );
-
-
-   /* This is the struct passed to radeon_dri.so for its initialization */
-   pRADEONDRI->deviceID          = info->Chipset;
-   pRADEONDRI->width             = disp->virtualWidth;
-   pRADEONDRI->height            = disp->virtualHeight;
-   pRADEONDRI->depth             = disp->bpp; /* XXX: depth */
-   pRADEONDRI->bpp               = disp->bpp;
-   pRADEONDRI->IsPCI             = (disp->card_type != RADEON_CARD_AGP);;
-   pRADEONDRI->frontOffset       = info->frontOffset;
-   pRADEONDRI->frontPitch        = info->frontPitch;
-   pRADEONDRI->backOffset        = info->backOffset;
-   pRADEONDRI->backPitch         = info->backPitch;
-   pRADEONDRI->depthOffset       = info->depthOffset;
-   pRADEONDRI->depthPitch        = info->depthPitch;
-   pRADEONDRI->textureOffset     = info->textureOffset;
-   pRADEONDRI->textureSize       = info->textureSize;
-   pRADEONDRI->log2TexGran       = info->log2TexGran;
-   pRADEONDRI->statusHandle      = info->ringReadPtrHandle;
-   pRADEONDRI->statusSize        = info->ringReadMapSize;
-   pRADEONDRI->gartTexHandle      = info->gartTexHandle;
-   pRADEONDRI->gartTexMapSize     = info->gartTexMapSize;
-   pRADEONDRI->log2GARTTexGran    = info->log2GARTTexGran;
-   pRADEONDRI->gartTexOffset      = info->gartTexStart;
-   pRADEONDRI->sarea_priv_offset = sizeof(drm_sarea_t);
-
-   /* Don't release the lock now - let the VT switch handler do it. */
-
-   return 1;
-}
-
-
-/**
- * \brief Get Radeon chip family from chipset number.
- *
- * \param info driver private data.
- *
- * \return non-zero on success, or zero on failure.
- *
- * Called by radeonInitFBDev() to set RADEONInfoRec::ChipFamily
- * according to the value of RADEONInfoRec::Chipset.  Fails if the
- * chipset is unrecognized or not appropriate for this driver (i.e., not
- * an r100 style radeon)
- */
-static int get_chipfamily_from_chipset( RADEONInfoPtr info )
-{
-    switch (info->Chipset) {
-    case PCI_CHIP_RADEON_LY:
-    case PCI_CHIP_RADEON_LZ:
-        info->ChipFamily = CHIP_FAMILY_M6;
-        break;
-
-    case PCI_CHIP_RADEON_QY:
-    case PCI_CHIP_RADEON_QZ:
-        info->ChipFamily = CHIP_FAMILY_VE;
-        break;
-
-    case PCI_CHIP_R200_QL:
-    case PCI_CHIP_R200_QN:
-    case PCI_CHIP_R200_QO:
-    case PCI_CHIP_R200_Ql:
-    case PCI_CHIP_R200_BB:
-        info->ChipFamily = CHIP_FAMILY_R200;
-        break;
-
-    case PCI_CHIP_RV200_QW: /* RV200 desktop */
-    case PCI_CHIP_RV200_QX:
-        info->ChipFamily = CHIP_FAMILY_RV200;
-        break;
-
-    case PCI_CHIP_RADEON_LW:
-    case PCI_CHIP_RADEON_LX:
-        info->ChipFamily = CHIP_FAMILY_M7;
-        break;
-
-    case PCI_CHIP_RV250_Id:
-    case PCI_CHIP_RV250_Ie:
-    case PCI_CHIP_RV250_If:
-    case PCI_CHIP_RV250_Ig:
-        info->ChipFamily = CHIP_FAMILY_RV250;
-        break;
-
-    case PCI_CHIP_RV250_Ld:
-    case PCI_CHIP_RV250_Le:
-    case PCI_CHIP_RV250_Lf:
-    case PCI_CHIP_RV250_Lg:
-        info->ChipFamily = CHIP_FAMILY_M9;
-        break;
-
-    case PCI_CHIP_RV280_Y_:
-    case PCI_CHIP_RV280_Ya:
-    case PCI_CHIP_RV280_Yb:
-    case PCI_CHIP_RV280_Yc:
-        info->ChipFamily = CHIP_FAMILY_RV280;
-        break;
-
-    case PCI_CHIP_R300_ND:
-    case PCI_CHIP_R300_NE:
-    case PCI_CHIP_R300_NF:
-    case PCI_CHIP_R300_NG:
-        info->ChipFamily = CHIP_FAMILY_R300;
-        break;
-
-    case PCI_CHIP_RV370_5460:
-        info->ChipFamily = CHIP_FAMILY_RV380;
-	break;
-
-    default:
-        /* Original Radeon/7200 */
-        info->ChipFamily = CHIP_FAMILY_RADEON;
-    }
-
-    return 1;
-}
-
-
-/**
- * \brief Initialize the framebuffer device mode
- *
- * \param disp display handle.
- *
- * \return one on success, or zero on failure.
- *
- * Fills in \p info with some default values and some information from \p disp
- * and then calls RADEONScreenInit() for the screen initialization.
- *
- * Before exiting clears the framebuffer memory accessing it directly.
- */
-static int radeonInitFBDev( driDisplay *disp, RADEONDRIPtr pRADEONDRI )
-{
-   int err;
-   RADEONInfoPtr info = calloc(1, sizeof(*info));
-
-   disp->driverPrivate = (void *)info;
-
-   info->gartFastWrite  = RADEON_DEFAULT_AGP_FAST_WRITE;
-   info->gartSize       = RADEON_DEFAULT_AGP_SIZE;
-   info->gartTexSize    = RADEON_DEFAULT_AGP_TEX_SIZE;
-   info->bufSize       = RADEON_DEFAULT_BUFFER_SIZE;
-   info->ringSize      = RADEON_DEFAULT_RING_SIZE;
-   info->page_flip_enable = RADEON_DEFAULT_PAGE_FLIP;
-
-   fprintf(stderr,
-           "Using %d MB AGP aperture\n", info->gartSize);
-   fprintf(stderr,
-           "Using %d MB for the ring buffer\n", info->ringSize);
-   fprintf(stderr,
-           "Using %d MB for vertex/indirect buffers\n", info->bufSize);
-   fprintf(stderr,
-           "Using %d MB for AGP textures\n", info->gartTexSize);
-   fprintf(stderr,
-           "page flipping %sabled\n", info->page_flip_enable?"en":"dis");
-
-   info->Chipset = disp->chipset;
-
-   if (!get_chipfamily_from_chipset( info )) {
-      fprintf(stderr, "Unknown or non-radeon chipset -- cannot continue\n");
-      fprintf(stderr, "==> Verify PCI BusID is correct in miniglx.conf\n");
-      return 0;
-   }
-#if 0
-   if (info->ChipFamily >= CHIP_FAMILY_R300) {
-      fprintf(stderr,
-              "Direct rendering not yet supported on "
-              "Radeon 9700 and newer cards\n");
-      return 0;
-   }
-#endif
-
-#if 00
-   /* don't seem to need this here */
-   info->frontPitch = disp->virtualWidth;
-#endif
-
-   /* Check the radeon DRM version */
-   if (!RADEONCheckDRMVersion(disp, info)) {
-      return 0;
-   }
-
-   if (RADEONGetCardType(disp, info)<0)
-      return 0;
-
-   if (disp->card_type!=RADEON_CARD_AGP) {
-      /* Initialize PCI */
-      if (!RADEONDRIPciInit(disp, info))
-         return 0;
-   }
-   else {
-      /* Initialize AGP */
-      if (!RADEONDRIAgpInit(disp, info))
-         return 0;
-   }
-
-   if (!RADEONScreenInit( disp, info, pRADEONDRI))
-      return 0;
-
-   /* Initialize and start the CP if required */
-   if ((err = drmCommandNone(disp->drmFD, DRM_RADEON_CP_START)) != 0) {
-      fprintf(stderr, "%s: CP start %d\n", __FUNCTION__, err);
-      return 0;
-   }
-
-   return 1;
-}
-
-
-/**
- * Create list of all supported surface configs, attach list to the display.
- */
-static EGLBoolean
-radeonFillInConfigs(_EGLDisplay *disp, unsigned pixel_bits,
-                    unsigned depth_bits,
-                    unsigned stencil_bits, GLboolean have_back_buffer)
-{
-   _EGLConfig *configs;
-   _EGLConfig *c;
-   unsigned int i, num_configs;
-   unsigned int depth_buffer_factor;
-   unsigned int back_buffer_factor;
-   GLenum fb_format;
-   GLenum fb_type;
-
-   /* Right now GLX_SWAP_COPY_OML isn't supported, but it would be easy
-   * enough to add support.  Basically, if a context is created with an
-   * fbconfig where the swap method is GLX_SWAP_COPY_OML, pageflipping
-   * will never be used.
-   */
-   static const GLenum back_buffer_modes[] = {
-            GLX_NONE, GLX_SWAP_UNDEFINED_OML /*, GLX_SWAP_COPY_OML */
-         };
-
-   uint8_t depth_bits_array[2];
-   uint8_t stencil_bits_array[2];
-
-   depth_bits_array[0] = depth_bits;
-   depth_bits_array[1] = depth_bits;
-
-   /* Just like with the accumulation buffer, always provide some modes
-   * with a stencil buffer.  It will be a sw fallback, but some apps won't
-   * care about that.
-   */
-   stencil_bits_array[0] = 0;
-   stencil_bits_array[1] = (stencil_bits == 0) ? 8 : stencil_bits;
-
-   depth_buffer_factor = ((depth_bits != 0) || (stencil_bits != 0)) ? 2 : 1;
-   back_buffer_factor = (have_back_buffer) ? 2 : 1;
-
-   num_configs = depth_buffer_factor * back_buffer_factor * 2;
-
-   if (pixel_bits == 16) {
-      fb_format = GL_RGB;
-      fb_type = GL_UNSIGNED_SHORT_5_6_5;
-   } else {
-      fb_format = GL_RGBA;
-      fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
-   }
-
-   configs = calloc(sizeof(*configs), num_configs);
-   c = configs;
-   if (!_eglFillInConfigs(c, fb_format, fb_type,
-                          depth_bits_array, stencil_bits_array,
-                          depth_buffer_factor,
-                          back_buffer_modes, back_buffer_factor,
-                          GLX_TRUE_COLOR)) {
-      fprintf(stderr, "[%s:%u] Error creating FBConfig!\n",
-               __func__, __LINE__);
-      return EGL_FALSE;
-   }
-
-   /* Mark the visual as slow if there are "fake" stencil bits.
-   */
-   for (i = 0, c = configs; i < num_configs; i++, c++) {
-      int stencil = GET_CONFIG_ATTRIB(c, EGL_STENCIL_SIZE);
-      if ((stencil != 0)  && (stencil != stencil_bits)) {
-         SET_CONFIG_ATTRIB(c, EGL_CONFIG_CAVEAT, EGL_SLOW_CONFIG);
-      }
-   }
-
-   for (i = 0, c = configs; i < num_configs; i++, c++)
-      _eglAddConfig(disp, c);
-
-   free(configs);
-
-   return EGL_TRUE;
-}
-
-
-/**
- * Show the given surface on the named screen.
- * If surface is EGL_NO_SURFACE, disable the screen's output.
- */
-static EGLBoolean
-radeonShowScreenSurfaceMESA(_EGLDriver *drv, EGLDisplay dpy, EGLScreenMESA screen,
-                      EGLSurface surface, EGLModeMESA m)
-{
-   EGLBoolean b = _eglDRIShowScreenSurfaceMESA(drv, dpy, screen, surface, m);
-   return b;
-}
-
-
-/**
- * Called via eglInitialize() by user.
- */
-static EGLBoolean
-radeonInitialize(_EGLDriver *drv, EGLDisplay dpy, EGLint *major, EGLint *minor)
-{
-   __DRIframebuffer framebuffer;
-   driDisplay *display;
-
-   /* one-time init */
-   radeon_drm_page_size = getpagesize();
-
-   if (!_eglDRIInitialize(drv, dpy, major, minor))
-      return EGL_FALSE;
-
-   display = Lookup_driDisplay(dpy);
-
-   framebuffer.dev_priv_size = sizeof(RADEONDRIRec);
-   framebuffer.dev_priv = malloc(sizeof(RADEONDRIRec));
-
-   /* XXX we shouldn't hard-code values here! */
-   /* we won't know the screen surface size until the user calls
-    * eglCreateScreenSurfaceMESA().
-    */
-#if 0
-   display->virtualWidth = 1024;
-   display->virtualHeight = 768;
-#else
-   display->virtualWidth = 1280;
-   display->virtualHeight = 1024;
-#endif
-   display->bpp = 32;
-   display->cpp = 4;
-
-   if (!_eglDRIGetDisplayInfo(display))
-      return EGL_FALSE;
-
-   framebuffer.base = display->pFB;
-   framebuffer.width = display->virtualWidth;
-   framebuffer.height = display->virtualHeight;
-   framebuffer.stride = display->virtualWidth;
-   framebuffer.size = display->fbSize;
-   radeonInitFBDev( display, framebuffer.dev_priv );
-
-   if (!_eglDRICreateDisplay(display, &framebuffer))
-      return EGL_FALSE;
-
-   if (!_eglDRICreateScreens(display))
-      return EGL_FALSE;
-
-   /* create a variety of both 32 and 16-bit configurations */
-   radeonFillInConfigs(&display->Base, 32, 24, 8, GL_TRUE);
-   radeonFillInConfigs(&display->Base, 16, 16, 0, GL_TRUE);
-
-   drv->Initialized = EGL_TRUE;
-   return EGL_TRUE;
-}
-
-
-/**
- * The bootstrap function.  Return a new radeonDriver object and
- * plug in API functions.
- */
-_EGLDriver *
-_eglMain(_EGLDisplay *dpy)
-{
-   radeonDriver *radeon;
-
-   radeon = (radeonDriver *) calloc(1, sizeof(*radeon));
-   if (!radeon) {
-      return NULL;
-   }
-
-   /* First fill in the dispatch table with defaults */
-   _eglDRIInitDriverFallbacks(&radeon->Base);
-
-   /* then plug in our radeon-specific functions */
-   radeon->Base.API.Initialize = radeonInitialize;
-   radeon->Base.API.ShowScreenSurfaceMESA = radeonShowScreenSurfaceMESA;
-
-   return &radeon->Base;
-}
diff --git a/src/mesa/drivers/dri/unichrome/via_context.c b/src/mesa/drivers/dri/unichrome/via_context.c
index d17a160..9da96bd 100644
--- a/src/mesa/drivers/dri/unichrome/via_context.c
+++ b/src/mesa/drivers/dri/unichrome/via_context.c
@@ -33,10 +33,7 @@
 #include "main/glheader.h"
 #include "main/context.h"
 #include "main/formats.h"
-#include "main/matrix.h"
-#include "main/state.h"
 #include "main/simple_list.h"
-#include "main/extensions.h"
 #include "main/framebuffer.h"
 #include "main/renderbuffer.h"
 
diff --git a/src/mesa/drivers/dri/unichrome/via_ioctl.c b/src/mesa/drivers/dri/unichrome/via_ioctl.c
index 8d4edfa..c9a31f3 100644
--- a/src/mesa/drivers/dri/unichrome/via_ioctl.c
+++ b/src/mesa/drivers/dri/unichrome/via_ioctl.c
@@ -34,7 +34,6 @@
 #include "via_context.h"
 #include "via_tris.h"
 #include "via_ioctl.h"
-#include "via_state.h"
 #include "via_fb.h"
 #include "via_3d_reg.h"
 
diff --git a/src/mesa/drivers/dri/unichrome/via_render.c b/src/mesa/drivers/dri/unichrome/via_render.c
index f676cc1..896c43d 100644
--- a/src/mesa/drivers/dri/unichrome/via_render.c
+++ b/src/mesa/drivers/dri/unichrome/via_render.c
@@ -37,7 +37,6 @@
 
 #include "via_context.h"
 #include "via_tris.h"
-#include "via_state.h"
 #include "via_ioctl.h"
 
 /*
diff --git a/src/mesa/drivers/dri/unichrome/via_screen.c b/src/mesa/drivers/dri/unichrome/via_screen.c
index 2cfb983..8c91c93 100644
--- a/src/mesa/drivers/dri/unichrome/via_screen.c
+++ b/src/mesa/drivers/dri/unichrome/via_screen.c
@@ -30,17 +30,13 @@
 #include "main/context.h"
 #include "main/framebuffer.h"
 #include "main/renderbuffer.h"
-#include "main/matrix.h"
 #include "main/simple_list.h"
 #include "vblank.h"
 
 #include "via_state.h"
 #include "via_tex.h"
 #include "via_span.h"
-#include "via_tris.h"
-#include "via_ioctl.h"
 #include "via_screen.h"
-#include "via_fb.h"
 #include "via_dri.h"
 
 #include "GL/internal/dri_interface.h"
diff --git a/src/mesa/drivers/dri/unichrome/via_state.c b/src/mesa/drivers/dri/unichrome/via_state.c
index e6e5526..f7029b9 100644
--- a/src/mesa/drivers/dri/unichrome/via_state.c
+++ b/src/mesa/drivers/dri/unichrome/via_state.c
@@ -35,7 +35,6 @@
 #include "via_context.h"
 #include "via_state.h"
 #include "via_tex.h"
-#include "via_tris.h"
 #include "via_ioctl.h"
 #include "via_3d_reg.h"
 
@@ -44,8 +43,6 @@
 #include "tnl/tnl.h"
 #include "swrast_setup/swrast_setup.h"
 
-#include "tnl/t_pipeline.h"
-
 
 static GLuint ROP[16] = {
     HC_HROP_BLACK,    /* GL_CLEAR           0                      	*/
diff --git a/src/mesa/drivers/dri/unichrome/via_tex.c b/src/mesa/drivers/dri/unichrome/via_tex.c
index 24924d2..917f975 100644
--- a/src/mesa/drivers/dri/unichrome/via_tex.c
+++ b/src/mesa/drivers/dri/unichrome/via_tex.c
@@ -37,14 +37,12 @@
 #include "main/mipmap.h"
 #include "main/mm.h"
 #include "main/simple_list.h"
-#include "main/texcompress.h"
 #include "main/texobj.h"
 #include "main/texstore.h"
 
 #include "via_context.h"
 #include "via_fb.h"
 #include "via_tex.h"
-#include "via_state.h"
 #include "via_ioctl.h"
 #include "via_3d_reg.h"
 
diff --git a/src/mesa/drivers/dri/unichrome/via_texcombine.c b/src/mesa/drivers/dri/unichrome/via_texcombine.c
index b646897..f87ba07 100644
--- a/src/mesa/drivers/dri/unichrome/via_texcombine.c
+++ b/src/mesa/drivers/dri/unichrome/via_texcombine.c
@@ -38,7 +38,6 @@
 #include "main/enums.h"
 
 #include "via_context.h"
-#include "via_state.h"
 #include "via_tex.h"
 #include "via_3d_reg.h"
 
diff --git a/src/mesa/glapi/Makefile b/src/mesa/glapi/Makefile
index 846c041..bfc3db4 100644
--- a/src/mesa/glapi/Makefile
+++ b/src/mesa/glapi/Makefile
@@ -25,6 +25,7 @@
 SERVER_GLAPI_FILES = \
 	$(GLX_DIR)/glapi.h \
 	$(GLX_DIR)/glapi.c \
+	$(GLX_DIR)/glapi_nop.c \
 	$(GLX_DIR)/glthread.c \
 	$(GLX_DIR)/glthread.h
 
diff --git a/src/mesa/glapi/gl_apitemp.py b/src/mesa/glapi/gl_apitemp.py
index 09b0d36..41a40fb 100644
--- a/src/mesa/glapi/gl_apitemp.py
+++ b/src/mesa/glapi/gl_apitemp.py
@@ -180,7 +180,7 @@
 #error _GLAPI_SKIP_NORMAL_ENTRY_POINTS must not be defined
 #endif
 
-static _glapi_proc DISPATCH_TABLE_NAME[] = {"""
+_glapi_proc DISPATCH_TABLE_NAME[] = {"""
 		for f in api.functionIterateByOffset():
 			print '   TABLE_ENTRY(%s),' % (f.dispatch_name())
 
@@ -204,7 +204,7 @@
  * We list the functions which are not otherwise used.
  */
 #ifdef UNUSED_TABLE_NAME
-static _glapi_proc UNUSED_TABLE_NAME[] = {"""
+_glapi_proc UNUSED_TABLE_NAME[] = {"""
 
 		normal_entries = []
 		proto_entries = []
diff --git a/src/mesa/glapi/glapi.c b/src/mesa/glapi/glapi.c
index adb47f9..469523d 100644
--- a/src/mesa/glapi/glapi.c
+++ b/src/mesa/glapi/glapi.c
@@ -73,85 +73,7 @@
 #include "glapi/glapioffsets.h"
 #include "glapi/glapitable.h"
 
-/***** BEGIN NO-OP DISPATCH *****/
-
-static GLboolean WarnFlag = GL_FALSE;
-static _glapi_warning_func warning_func;
-
-/*
- * Enable/disable printing of warning messages.
- */
-PUBLIC void
-_glapi_noop_enable_warnings(GLboolean enable)
-{
-   WarnFlag = enable;
-}
-
-/*
- * Register a callback function for reporting errors.
- */
-PUBLIC void
-_glapi_set_warning_func( _glapi_warning_func func )
-{
-   warning_func = func;
-}
-
-static int
-warn(const char *func)
-{
-#if !defined(_WIN32_WCE)
-   if ((WarnFlag || getenv("MESA_DEBUG") || getenv("LIBGL_DEBUG"))
-       && warning_func) {
-      warning_func(NULL, "GL User Error: called without context: %s", func);
-   }
-#endif
-   return 0;
-}
-
-#ifdef DEBUG
-
-#define KEYWORD1 static
-#define KEYWORD1_ALT static
-#define KEYWORD2 GLAPIENTRY
-#define NAME(func)  NoOp##func
-
-#define F NULL
-
-#define DISPATCH(func, args, msg)					      \
-   warn(#func);
-
-#define RETURN_DISPATCH(func, args, msg)				      \
-   return warn(#func);
-
-#define TABLE_ENTRY(name) (_glapi_proc) NoOp##name
-
-#else
-
-static void
-NoOpGeneric(void)
-{
-   if ((WarnFlag || getenv("MESA_DEBUG") || getenv("LIBGL_DEBUG"))
-       && warning_func) {
-      warning_func(NULL, "GL User Error: calling GL function");
-   }
-}
-
-#define TABLE_ENTRY(name) (_glapi_proc) NoOpGeneric
-
-#endif
-
-#define DISPATCH_TABLE_NAME __glapi_noop_table
-#define UNUSED_TABLE_NAME __unused_noop_functions
-
-static GLint NoOpUnused(void)
-{
-   return warn("extension function");
-}
-
-#include "glapi/glapitemp.h"
-
-/***** END NO-OP DISPATCH *****/
-
+extern _glapi_proc __glapi_noop_table[];
 
 
 /**
@@ -278,7 +200,6 @@
 PUBLIC void
 _glapi_set_context(void *context)
 {
-   (void) __unused_noop_functions; /* silence a warning */
 #if defined(GLX_USE_TLS)
    _glapi_tls_Context = context;
 #elif defined(THREADS)
diff --git a/src/mesa/glapi/glapi.h b/src/mesa/glapi/glapi.h
index 5fb5401..47ea23e 100644
--- a/src/mesa/glapi/glapi.h
+++ b/src/mesa/glapi/glapi.h
@@ -55,8 +55,6 @@
 
 typedef void (*_glapi_proc)(void); /* generic function pointer */
 
-typedef void (*_glapi_warning_func)(void *ctx, const char *str, ...);
-
 
 #if defined(USE_MGL_NAMESPACE)
 #define _glapi_set_dispatch _mglapi_set_dispatch
@@ -107,12 +105,6 @@
  **/
 
 extern void
-_glapi_noop_enable_warnings(GLboolean enable);
-
-extern void
-_glapi_set_warning_func(_glapi_warning_func func);
-
-extern void
 _glapi_check_multithread(void);
 
 
diff --git a/src/mesa/glapi/glapi_nop.c b/src/mesa/glapi/glapi_nop.c
new file mode 100644
index 0000000..5d2769c
--- /dev/null
+++ b/src/mesa/glapi/glapi_nop.c
@@ -0,0 +1,91 @@
+/*
+ * Mesa 3-D graphics library
+ * Version:  7.8
+ *
+ * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
+ * Copyright (C) 2010  VMWare, Inc.  All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
+ * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+
+/**
+ * No-op dispatch table.
+ *
+ * This file defines a special dispatch table which is loaded with no-op
+ * functions.
+ *
+ * When there's no current rendering context, calling a GL function like
+ * glBegin() is a no-op.  Apps should never normally do this.  So as a
+ * debugging aid, each of the no-op functions will emit a warning to
+ * stderr if the MESA_DEBUG or LIBGL_DEBUG env var is set.
+ */
+
+
+
+#include "main/compiler.h"
+#include "main/glheader.h"
+#include "glapi/glapi.h"
+
+
+/**
+ * Called by each of the no-op GL entrypoints.
+ */
+static int
+Warn(const char *func)
+{
+#if !defined(_WIN32_WCE)
+   if (getenv("MESA_DEBUG") || getenv("LIBGL_DEBUG")) {
+      fprintf(stderr, "GL User Error: gl%s called without a rendering context\n",
+              func);
+   }
+#endif
+   return 0;
+}
+
+
+/**
+ * This is called if the user somehow calls an unassigned GL dispatch function.
+ */
+static GLint
+NoOpUnused(void)
+{
+   return Warn(" function");
+}
+
+
+/*
+ * Defines for the glapitemp.h functions.
+ */
+#define KEYWORD1 static
+#define KEYWORD1_ALT static
+#define KEYWORD2 GLAPIENTRY
+#define NAME(func)  NoOp##func
+#define DISPATCH(func, args, msg)  Warn(#func);
+#define RETURN_DISPATCH(func, args, msg)  Warn(#func); return 0
+
+
+/*
+ * Defines for the table of no-op entry points.
+ */
+#define TABLE_ENTRY(name) (_glapi_proc) NoOp##name
+#define DISPATCH_TABLE_NAME __glapi_noop_table
+#define UNUSED_TABLE_NAME __unused_noop_functions
+
+
+#include "glapi/glapitemp.h"
diff --git a/src/mesa/glapi/glapitemp.h b/src/mesa/glapi/glapitemp.h
index b8bfcc1..2540ef6 100644
--- a/src/mesa/glapi/glapitemp.h
+++ b/src/mesa/glapi/glapitemp.h
@@ -5764,7 +5764,7 @@
 #error _GLAPI_SKIP_NORMAL_ENTRY_POINTS must not be defined
 #endif
 
-static _glapi_proc DISPATCH_TABLE_NAME[] = {
+_glapi_proc DISPATCH_TABLE_NAME[] = {
    TABLE_ENTRY(NewList),
    TABLE_ENTRY(EndList),
    TABLE_ENTRY(CallList),
@@ -6680,7 +6680,7 @@
  * We list the functions which are not otherwise used.
  */
 #ifdef UNUSED_TABLE_NAME
-static _glapi_proc UNUSED_TABLE_NAME[] = {
+_glapi_proc UNUSED_TABLE_NAME[] = {
 #ifndef _GLAPI_SKIP_NORMAL_ENTRY_POINTS
    TABLE_ENTRY(ArrayElementEXT),
    TABLE_ENTRY(BindTextureEXT),
diff --git a/src/mesa/main/attrib.c b/src/mesa/main/attrib.c
index 0641b98..3fbdba2 100644
--- a/src/mesa/main/attrib.c
+++ b/src/mesa/main/attrib.c
@@ -33,7 +33,6 @@
 #include "bufferobj.h"
 #include "clear.h"
 #include "colormac.h"
-#include "colortab.h"
 #include "context.h"
 #include "depth.h"
 #include "enable.h"
diff --git a/src/mesa/main/blend.c b/src/mesa/main/blend.c
index b8170dd..de60031 100644
--- a/src/mesa/main/blend.c
+++ b/src/mesa/main/blend.c
@@ -35,7 +35,6 @@
 #include "enums.h"
 #include "macros.h"
 #include "mtypes.h"
-#include "glapi/glapitable.h"
 
 
 /**
diff --git a/src/mesa/main/buffers.c b/src/mesa/main/buffers.c
index 97f0659..fb30b59 100644
--- a/src/mesa/main/buffers.c
+++ b/src/mesa/main/buffers.c
@@ -35,8 +35,6 @@
 #include "colormac.h"
 #include "context.h"
 #include "enums.h"
-#include "fbobject.h"
-#include "state.h"
 
 
 #define BAD_MASK ~0u
diff --git a/src/mesa/main/context.c b/src/mesa/main/context.c
index 320c590..a570506 100644
--- a/src/mesa/main/context.c
+++ b/src/mesa/main/context.c
@@ -415,14 +415,6 @@
          _mesa_ubyte_to_float_color_tab[i] = (float) i / 255.0F;
       }
 
-      if (_mesa_getenv("MESA_DEBUG")) {
-         _glapi_noop_enable_warnings(GL_TRUE);
-         _glapi_set_warning_func( (_glapi_warning_func) _mesa_warning );
-      }
-      else {
-         _glapi_noop_enable_warnings(GL_FALSE);
-      }
-
 #if defined(DEBUG) && defined(__DATE__) && defined(__TIME__)
       _mesa_debug(ctx, "Mesa %s DEBUG build %s %s\n",
                   MESA_VERSION_STRING, __DATE__, __TIME__);
diff --git a/src/mesa/main/convolve.c b/src/mesa/main/convolve.c
index 8db3e79..5ed93e0 100644
--- a/src/mesa/main/convolve.c
+++ b/src/mesa/main/convolve.c
@@ -38,7 +38,6 @@
 #include "context.h"
 #include "image.h"
 #include "mtypes.h"
-#include "pixel.h"
 #include "state.h"
 #include "glapi/dispatch.h"
 
diff --git a/src/mesa/main/debug.c b/src/mesa/main/debug.c
index a42113e..9bad834 100644
--- a/src/mesa/main/debug.c
+++ b/src/mesa/main/debug.c
@@ -26,7 +26,6 @@
 #include "mtypes.h"
 #include "attrib.h"
 #include "colormac.h"
-#include "context.h"
 #include "enums.h"
 #include "formats.h"
 #include "hash.h"
@@ -35,7 +34,6 @@
 #include "get.h"
 #include "pixelstore.h"
 #include "readpix.h"
-#include "texgetimage.h"
 #include "texobj.h"
 
 
@@ -54,7 +52,7 @@
    "GL_QUAD_STRIP",
    "GL_POLYGON",
    "outside begin/end",
-   "inside unkown primitive",
+   "inside unknown primitive",
    "unknown state"
 };
 
diff --git a/src/mesa/main/depthstencil.c b/src/mesa/main/depthstencil.c
index 193c7f8..49946a6 100644
--- a/src/mesa/main/depthstencil.c
+++ b/src/mesa/main/depthstencil.c
@@ -25,7 +25,6 @@
 #include "glheader.h"
 #include "imports.h"
 #include "context.h"
-#include "fbobject.h"
 #include "formats.h"
 #include "mtypes.h"
 #include "depthstencil.h"
diff --git a/src/mesa/main/dlist.c b/src/mesa/main/dlist.c
index 21a8216..7db686c 100644
--- a/src/mesa/main/dlist.c
+++ b/src/mesa/main/dlist.c
@@ -35,46 +35,25 @@
 #include "api_loopback.h"
 #include "config.h"
 #include "mfeatures.h"
-#include "attrib.h"
-#include "blend.h"
-#include "buffers.h"
 #if FEATURE_ARB_vertex_buffer_object
 #include "bufferobj.h"
 #endif
 #include "arrayobj.h"
-#include "clip.h"
-#include "colortab.h"
 #include "context.h"
-#include "convolve.h"
-#include "depth.h"
 #include "dlist.h"
-#include "enable.h"
 #include "enums.h"
 #include "eval.h"
-#include "extensions.h"
-#include "feedback.h"
 #include "framebuffer.h"
-#include "get.h"
 #include "glapi/glapi.h"
 #include "hash.h"
-#include "histogram.h"
 #include "image.h"
 #include "light.h"
-#include "lines.h"
 #include "dlist.h"
 #include "macros.h"
-#include "matrix.h"
-#include "pixel.h"
-#include "points.h"
-#include "polygon.h"
 #include "queryobj.h"
-#include "state.h"
-#include "texobj.h"
 #include "teximage.h"
-#include "texstate.h"
 #include "mtypes.h"
 #include "varray.h"
-#include "vtxfmt.h"
 #if FEATURE_ARB_vertex_program || FEATURE_ARB_fragment_program
 #include "shader/arbprogram.h"
 #include "shader/program.h"
diff --git a/src/mesa/main/drawpix.c b/src/mesa/main/drawpix.c
index 5d4b53a..0afd47b 100644
--- a/src/mesa/main/drawpix.c
+++ b/src/mesa/main/drawpix.c
@@ -30,7 +30,6 @@
 #include "enums.h"
 #include "feedback.h"
 #include "framebuffer.h"
-#include "image.h"
 #include "readpix.h"
 #include "state.h"
 #include "glapi/dispatch.h"
diff --git a/src/mesa/main/enable.c b/src/mesa/main/enable.c
index cd6e881..f5c88a6 100644
--- a/src/mesa/main/enable.c
+++ b/src/mesa/main/enable.c
@@ -32,7 +32,6 @@
 #include "context.h"
 #include "enable.h"
 #include "light.h"
-#include "macros.h"
 #include "simple_list.h"
 #include "mtypes.h"
 #include "enums.h"
diff --git a/src/mesa/main/fbobject.c b/src/mesa/main/fbobject.c
index 4da245a..0556f16 100644
--- a/src/mesa/main/fbobject.c
+++ b/src/mesa/main/fbobject.c
@@ -40,13 +40,10 @@
 #include "framebuffer.h"
 #include "hash.h"
 #include "macros.h"
-#include "mipmap.h"
 #include "renderbuffer.h"
 #include "state.h"
 #include "teximage.h"
 #include "texobj.h"
-#include "texstore.h"
-#include "texstate.h"
 
 
 /** Set this to 1 to help debug FBO incompleteness problems */
diff --git a/src/mesa/main/formats.c b/src/mesa/main/formats.c
index 5983f00..d0c9c00 100644
--- a/src/mesa/main/formats.c
+++ b/src/mesa/main/formats.c
@@ -27,7 +27,6 @@
 #include "imports.h"
 #include "formats.h"
 #include "config.h"
-#include "texstore.h"
 
 
 /**
diff --git a/src/mesa/main/image.c b/src/mesa/main/image.c
index fc278bb..81993e7 100644
--- a/src/mesa/main/image.c
+++ b/src/mesa/main/image.c
@@ -37,7 +37,6 @@
 #include "image.h"
 #include "imports.h"
 #include "macros.h"
-#include "pixel.h"
 
 
 /**
diff --git a/src/mesa/main/lines.c b/src/mesa/main/lines.c
index 81d0d33..cc63a75 100644
--- a/src/mesa/main/lines.c
+++ b/src/mesa/main/lines.c
@@ -25,10 +25,8 @@
 
 #include "glheader.h"
 #include "context.h"
-#include "depth.h"
 #include "lines.h"
 #include "macros.h"
-#include "texstate.h"
 #include "mtypes.h"
 
 
diff --git a/src/mesa/main/mipmap.c b/src/mesa/main/mipmap.c
index 7350c7a..77cd1d4 100644
--- a/src/mesa/main/mipmap.c
+++ b/src/mesa/main/mipmap.c
@@ -30,7 +30,6 @@
 #include "imports.h"
 #include "formats.h"
 #include "mipmap.h"
-#include "texcompress.h"
 #include "teximage.h"
 #include "texstore.h"
 #include "image.h"
diff --git a/src/mesa/main/pixel.c b/src/mesa/main/pixel.c
index 3820ebd..f6f9c11 100644
--- a/src/mesa/main/pixel.c
+++ b/src/mesa/main/pixel.c
@@ -32,7 +32,6 @@
 #include "bufferobj.h"
 #include "colormac.h"
 #include "context.h"
-#include "image.h"
 #include "macros.h"
 #include "pixel.h"
 #include "mtypes.h"
diff --git a/src/mesa/main/pixelstore.c b/src/mesa/main/pixelstore.c
index 6a641f8..ec585ef 100644
--- a/src/mesa/main/pixelstore.c
+++ b/src/mesa/main/pixelstore.c
@@ -30,10 +30,7 @@
 
 #include "glheader.h"
 #include "bufferobj.h"
-#include "colormac.h"
 #include "context.h"
-#include "image.h"
-#include "macros.h"
 #include "pixelstore.h"
 #include "mtypes.h"
 
diff --git a/src/mesa/main/points.c b/src/mesa/main/points.c
index dcaeccd..e743a2e 100644
--- a/src/mesa/main/points.c
+++ b/src/mesa/main/points.c
@@ -32,7 +32,6 @@
 #include "context.h"
 #include "macros.h"
 #include "points.h"
-#include "texstate.h"
 #include "mtypes.h"
 
 
diff --git a/src/mesa/main/polygon.c b/src/mesa/main/polygon.c
index 376a87a..dcde675 100644
--- a/src/mesa/main/polygon.c
+++ b/src/mesa/main/polygon.c
@@ -34,7 +34,6 @@
 #include "context.h"
 #include "image.h"
 #include "enums.h"
-#include "macros.h"
 #include "polygon.h"
 #include "mtypes.h"
 
diff --git a/src/mesa/main/state.c b/src/mesa/main/state.c
index f10e6b0..904ad01 100644
--- a/src/mesa/main/state.c
+++ b/src/mesa/main/state.c
@@ -48,7 +48,6 @@
 #include "texenvprogram.h"
 #include "texobj.h"
 #include "texstate.h"
-#include "viewport.h"
 
 
 static void
diff --git a/src/mesa/main/texcompress.c b/src/mesa/main/texcompress.c
index a4f1926..cff6de8 100644
--- a/src/mesa/main/texcompress.c
+++ b/src/mesa/main/texcompress.c
@@ -35,10 +35,7 @@
 #include "colormac.h"
 #include "context.h"
 #include "formats.h"
-#include "image.h"
-#include "mipmap.h"
 #include "texcompress.h"
-#include "texstore.h"
 
 
 /**
diff --git a/src/mesa/main/texformat.c b/src/mesa/main/texformat.c
index 1a374e7..096945a 100644
--- a/src/mesa/main/texformat.c
+++ b/src/mesa/main/texformat.c
@@ -35,8 +35,6 @@
 
 #include "context.h"
 #include "texcompress.h"
-#include "texcompress_fxt1.h"
-#include "texcompress_s3tc.h"
 #include "texformat.h"
 
 
diff --git a/src/mesa/main/texgetimage.c b/src/mesa/main/texgetimage.c
index d7943ea..66d01c1 100644
--- a/src/mesa/main/texgetimage.c
+++ b/src/mesa/main/texgetimage.c
@@ -35,10 +35,8 @@
 #include "context.h"
 #include "formats.h"
 #include "image.h"
-#include "texcompress.h"
 #include "texgetimage.h"
 #include "teximage.h"
-#include "texstate.h"
 
 
 
diff --git a/src/mesa/main/teximage.c b/src/mesa/main/teximage.c
index b946f3c..76273b0 100644
--- a/src/mesa/main/teximage.c
+++ b/src/mesa/main/teximage.c
@@ -46,7 +46,6 @@
 #include "texfetch.h"
 #include "teximage.h"
 #include "texstate.h"
-#include "texstore.h"
 #include "mtypes.h"
 
 
diff --git a/src/mesa/main/texobj.c b/src/mesa/main/texobj.c
index 7f0a246..9db9581 100644
--- a/src/mesa/main/texobj.c
+++ b/src/mesa/main/texobj.c
@@ -38,7 +38,6 @@
 #include "imports.h"
 #include "macros.h"
 #include "teximage.h"
-#include "texstate.h"
 #include "texobj.h"
 #include "mtypes.h"
 #include "shader/prog_instruction.h"
diff --git a/src/mesa/main/texparam.c b/src/mesa/main/texparam.c
index d917e21..c4f2495 100644
--- a/src/mesa/main/texparam.c
+++ b/src/mesa/main/texparam.c
@@ -33,7 +33,6 @@
 #include "main/glheader.h"
 #include "main/colormac.h"
 #include "main/context.h"
-#include "main/enums.h"
 #include "main/formats.h"
 #include "main/macros.h"
 #include "main/texcompress.h"
diff --git a/src/mesa/main/texstate.c b/src/mesa/main/texstate.c
index c735e18..25333d8 100644
--- a/src/mesa/main/texstate.c
+++ b/src/mesa/main/texstate.c
@@ -35,11 +35,9 @@
 #include "context.h"
 #include "enums.h"
 #include "macros.h"
-#include "texcompress.h"
 #include "texobj.h"
 #include "teximage.h"
 #include "texstate.h"
-#include "texenvprogram.h"
 #include "mtypes.h"
 
 
diff --git a/src/mesa/main/texstore.c b/src/mesa/main/texstore.c
index 792c831..fcd0a56 100644
--- a/src/mesa/main/texstore.c
+++ b/src/mesa/main/texstore.c
@@ -263,7 +263,7 @@
    map[ZERO] = ZERO;
    map[ONE] = ONE;   
 
-/*
+#if 0
    _mesa_printf("from %x/%s to %x/%s map %d %d %d %d %d %d\n",
 		inFormat, _mesa_lookup_enum_by_nr(inFormat),
 		outFormat, _mesa_lookup_enum_by_nr(outFormat),
@@ -273,7 +273,7 @@
 		map[3], 
 		map[4], 
 		map[5]); 
-*/
+#endif
 }
 
 
diff --git a/src/mesa/main/vtxfmt.c b/src/mesa/main/vtxfmt.c
index c9eea8a..0dd3e5e 100644
--- a/src/mesa/main/vtxfmt.c
+++ b/src/mesa/main/vtxfmt.c
@@ -28,11 +28,9 @@
 
 #include "glheader.h"
 #include "api_arrayelt.h"
-#include "api_loopback.h"
 #include "context.h"
 #include "imports.h"
 #include "mtypes.h"
-#include "state.h"
 #include "vtxfmt.h"
 #include "eval.h"
 #include "dlist.h"
diff --git a/src/mesa/shader/arbprogparse.c b/src/mesa/shader/arbprogparse.c
index 3e22a8a..bdd26b7 100644
--- a/src/mesa/shader/arbprogparse.c
+++ b/src/mesa/shader/arbprogparse.c
@@ -54,10 +54,8 @@
 #include "main/glheader.h"
 #include "main/imports.h"
 #include "main/context.h"
-#include "main/macros.h"
 #include "main/mtypes.h"
 #include "arbprogparse.h"
-#include "program.h"
 #include "programopt.h"
 #include "prog_parameter.h"
 #include "prog_statevars.h"
diff --git a/src/mesa/shader/arbprogram.c b/src/mesa/shader/arbprogram.c
index eb537cd..7461380 100644
--- a/src/mesa/shader/arbprogram.c
+++ b/src/mesa/shader/arbprogram.c
@@ -180,23 +180,24 @@
          }
          else if (prog) {
             /* Unbind program if necessary */
-            if (prog->Target == GL_VERTEX_PROGRAM_ARB || /* == GL_VERTEX_PROGRAM_NV */
-                prog->Target == GL_VERTEX_STATE_PROGRAM_NV) {
+            switch (prog->Target) {
+            case GL_VERTEX_PROGRAM_ARB: /* == GL_VERTEX_PROGRAM_NV */
+            case GL_VERTEX_STATE_PROGRAM_NV:
                if (ctx->VertexProgram.Current &&
                    ctx->VertexProgram.Current->Base.Id == ids[i]) {
                   /* unbind this currently bound program */
                   _mesa_BindProgram(prog->Target, 0);
                }
-            }
-            else if (prog->Target == GL_FRAGMENT_PROGRAM_NV ||
-                     prog->Target == GL_FRAGMENT_PROGRAM_ARB) {
+               break;
+            case GL_FRAGMENT_PROGRAM_NV:
+            case GL_FRAGMENT_PROGRAM_ARB:
                if (ctx->FragmentProgram.Current &&
                    ctx->FragmentProgram.Current->Base.Id == ids[i]) {
                   /* unbind this currently bound program */
                   _mesa_BindProgram(prog->Target, 0);
                }
-            }
-            else {
+               break;
+            default:
                _mesa_problem(ctx, "bad target in glDeleteProgramsNV");
                return;
             }
@@ -561,6 +562,8 @@
    }
 }
 
+
+
 /**
  * Set a program env parameter register.
  * \note Called from the GL API dispatcher.
@@ -569,10 +572,35 @@
  */
 void GLAPIENTRY
 _mesa_ProgramEnvParameter4fvARB(GLenum target, GLuint index,
-                                   const GLfloat *params)
+                                const GLfloat *params)
 {
-   _mesa_ProgramEnvParameter4fARB(target, index, params[0], params[1],
-                                  params[2], params[3]);
+   GET_CURRENT_CONTEXT(ctx);
+   ASSERT_OUTSIDE_BEGIN_END(ctx);
+
+   FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
+
+   if (target == GL_FRAGMENT_PROGRAM_ARB
+       && ctx->Extensions.ARB_fragment_program) {
+      if (index >= ctx->Const.FragmentProgram.MaxEnvParams) {
+         _mesa_error(ctx, GL_INVALID_VALUE, "glProgramEnvParameter4fv(index)");
+         return;
+      }
+      memcpy(ctx->FragmentProgram.Parameters[index], params,
+             4 * sizeof(GLfloat));
+   }
+   else if (target == GL_VERTEX_PROGRAM_ARB /* == GL_VERTEX_PROGRAM_NV */
+       && (ctx->Extensions.ARB_vertex_program || ctx->Extensions.NV_vertex_program)) {
+      if (index >= ctx->Const.VertexProgram.MaxEnvParams) {
+         _mesa_error(ctx, GL_INVALID_VALUE, "glProgramEnvParameter4fv(index)");
+         return;
+      }
+      memcpy(ctx->VertexProgram.Parameters[index], params,
+             4 * sizeof(GLfloat));
+   }
+   else {
+      _mesa_error(ctx, GL_INVALID_ENUM, "glProgramEnvParameter4fv(target)");
+      return;
+   }
 }
 
 
@@ -581,7 +609,6 @@
 				 const GLfloat *params)
 {
    GET_CURRENT_CONTEXT(ctx);
-   GLint i;
    GLfloat * dest;
    ASSERT_OUTSIDE_BEGIN_END(ctx);
 
@@ -612,11 +639,7 @@
       return;
    }
 
-   for ( i = 0 ; i < count ; i++ ) {
-      COPY_4V(dest, params);
-      params += 4;
-      dest += 4;
-   }
+   memcpy(dest, params, count * 4 * sizeof(GLfloat));
 }
 
 
@@ -729,8 +752,7 @@
 				   const GLfloat *params)
 {
    GET_CURRENT_CONTEXT(ctx);
-   struct gl_program *prog;
-   GLint i;
+   GLfloat *dest;
    ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    FLUSH_VERTICES(ctx, _NEW_PROGRAM_CONSTANTS);
@@ -745,7 +767,7 @@
          _mesa_error(ctx, GL_INVALID_VALUE, "glProgramLocalParameters4fvEXT(index + count)");
          return;
       }
-      prog = &(ctx->FragmentProgram.Current->Base);
+      dest = ctx->FragmentProgram.Current->Base.LocalParams[index];
    }
    else if (target == GL_VERTEX_PROGRAM_ARB
             && ctx->Extensions.ARB_vertex_program) {
@@ -753,18 +775,14 @@
          _mesa_error(ctx, GL_INVALID_VALUE, "glProgramLocalParameters4fvEXT(index + count)");
          return;
       }
-      prog = &(ctx->VertexProgram.Current->Base);
+      dest = ctx->VertexProgram.Current->Base.LocalParams[index];
    }
    else {
       _mesa_error(ctx, GL_INVALID_ENUM, "glProgramLocalParameters4fvEXT(target)");
       return;
    }
 
-   for (i = 0; i < count; i++) {
-      ASSERT((index + i) < MAX_PROGRAM_LOCAL_PARAMS);
-      COPY_4V(prog->LocalParams[index + i], params);
-      params += 4;
-   }
+   memcpy(dest, params, count * 4 * sizeof(GLfloat));
 }
 
 
diff --git a/src/mesa/shader/lex.yy.c b/src/mesa/shader/lex.yy.c
index 68543ae..d1af35f 100644
--- a/src/mesa/shader/lex.yy.c
+++ b/src/mesa/shader/lex.yy.c
@@ -1043,12 +1043,12 @@
  */
 #include "main/glheader.h"
 #include "main/imports.h"
-#include "prog_instruction.h"
-#include "prog_statevars.h"
+#include "shader/prog_instruction.h"
+#include "shader/prog_statevars.h"
 
-#include "symbol_table.h"
-#include "program_parser.h"
-#include "program_parse.tab.h"
+#include "shader/symbol_table.h"
+#include "shader/program_parser.h"
+#include "shader/program_parse.tab.h"
 
 #define require_ARB_vp (yyextra->mode == ARB_vertex)
 #define require_ARB_fp (yyextra->mode == ARB_fragment)
diff --git a/src/mesa/shader/nvvertparse.c b/src/mesa/shader/nvvertparse.c
index 8574016..baff765 100644
--- a/src/mesa/shader/nvvertparse.c
+++ b/src/mesa/shader/nvvertparse.c
@@ -40,7 +40,6 @@
 #include "main/glheader.h"
 #include "main/context.h"
 #include "main/imports.h"
-#include "main/macros.h"
 #include "nvprogram.h"
 #include "nvvertparse.h"
 #include "prog_instruction.h"
diff --git a/src/mesa/shader/prog_execute.c b/src/mesa/shader/prog_execute.c
index 7f03452..c212790 100644
--- a/src/mesa/shader/prog_execute.c
+++ b/src/mesa/shader/prog_execute.c
@@ -38,7 +38,6 @@
 #include "main/glheader.h"
 #include "main/colormac.h"
 #include "main/context.h"
-#include "program.h"
 #include "prog_execute.h"
 #include "prog_instruction.h"
 #include "prog_parameter.h"
@@ -352,6 +351,28 @@
 }
 
 
+static GLuint
+fetch_vector1ui(const struct prog_src_register *source,
+                const struct gl_program_machine *machine)
+{
+   const GLuint *src = (GLuint *) get_src_register_pointer(source, machine);
+   GLuint result;
+
+   ASSERT(src);
+
+   result = src[GET_SWZ(source->Swizzle, 0)];
+
+   if (source->Abs) {
+      result = FABSF(result);
+   }
+   if (source->Negate) {
+      result = -result;
+   }
+
+   return result;
+}
+
+
 /**
  * Fetch texel from texture.  Use partial derivatives when possible.
  */
@@ -1668,13 +1689,11 @@
          break;
       case OPCODE_UP2H:        /* unpack two 16-bit floats */
          {
-            GLfloat a[4], result[4];
-            fi_type fi;
-            GLhalfNV hx, hy;
-            fetch_vector1(&inst->SrcReg[0], machine, a);
-            fi.f = a[0];
-            hx = fi.i & 0xffff;
-            hy = fi.i >> 16;
+            const GLuint raw = fetch_vector1ui(&inst->SrcReg[0], machine);
+            GLfloat result[4];
+            GLushort hx, hy;
+            hx = raw & 0xffff;
+            hy = raw >> 16;
             result[0] = result[2] = _mesa_half_to_float(hx);
             result[1] = result[3] = _mesa_half_to_float(hy);
             store_vector4(inst, machine, result);
@@ -1682,13 +1701,11 @@
          break;
       case OPCODE_UP2US:       /* unpack two GLushorts */
          {
-            GLfloat a[4], result[4];
-            fi_type fi;
+            const GLuint raw = fetch_vector1ui(&inst->SrcReg[0], machine);
+            GLfloat result[4];
             GLushort usx, usy;
-            fetch_vector1(&inst->SrcReg[0], machine, a);
-            fi.f = a[0];
-            usx = fi.i & 0xffff;
-            usy = fi.i >> 16;
+            usx = raw & 0xffff;
+            usy = raw >> 16;
             result[0] = result[2] = usx * (1.0f / 65535.0f);
             result[1] = result[3] = usy * (1.0f / 65535.0f);
             store_vector4(inst, machine, result);
@@ -1696,27 +1713,23 @@
          break;
       case OPCODE_UP4B:        /* unpack four GLbytes */
          {
-            GLfloat a[4], result[4];
-            fi_type fi;
-            fetch_vector1(&inst->SrcReg[0], machine, a);
-            fi.f = a[0];
-            result[0] = (((fi.i >> 0) & 0xff) - 128) / 127.0F;
-            result[1] = (((fi.i >> 8) & 0xff) - 128) / 127.0F;
-            result[2] = (((fi.i >> 16) & 0xff) - 128) / 127.0F;
-            result[3] = (((fi.i >> 24) & 0xff) - 128) / 127.0F;
+            const GLuint raw = fetch_vector1ui(&inst->SrcReg[0], machine);
+            GLfloat result[4];
+            result[0] = (((raw >> 0) & 0xff) - 128) / 127.0F;
+            result[1] = (((raw >> 8) & 0xff) - 128) / 127.0F;
+            result[2] = (((raw >> 16) & 0xff) - 128) / 127.0F;
+            result[3] = (((raw >> 24) & 0xff) - 128) / 127.0F;
             store_vector4(inst, machine, result);
          }
          break;
       case OPCODE_UP4UB:       /* unpack four GLubytes */
          {
-            GLfloat a[4], result[4];
-            fi_type fi;
-            fetch_vector1(&inst->SrcReg[0], machine, a);
-            fi.f = a[0];
-            result[0] = ((fi.i >> 0) & 0xff) / 255.0F;
-            result[1] = ((fi.i >> 8) & 0xff) / 255.0F;
-            result[2] = ((fi.i >> 16) & 0xff) / 255.0F;
-            result[3] = ((fi.i >> 24) & 0xff) / 255.0F;
+            const GLuint raw = fetch_vector1ui(&inst->SrcReg[0], machine);
+            GLfloat result[4];
+            result[0] = ((raw >> 0) & 0xff) / 255.0F;
+            result[1] = ((raw >> 8) & 0xff) / 255.0F;
+            result[2] = ((raw >> 16) & 0xff) / 255.0F;
+            result[3] = ((raw >> 24) & 0xff) / 255.0F;
             store_vector4(inst, machine, result);
          }
          break;
diff --git a/src/mesa/shader/prog_statevars.c b/src/mesa/shader/prog_statevars.c
index 058d4bb..3a446fd 100644
--- a/src/mesa/shader/prog_statevars.c
+++ b/src/mesa/shader/prog_statevars.c
@@ -31,7 +31,6 @@
 
 #include "main/glheader.h"
 #include "main/context.h"
-#include "main/hash.h"
 #include "main/imports.h"
 #include "main/macros.h"
 #include "main/mtypes.h"
diff --git a/src/mesa/shader/program_lexer.l b/src/mesa/shader/program_lexer.l
index e2acb3c..83bc508 100644
--- a/src/mesa/shader/program_lexer.l
+++ b/src/mesa/shader/program_lexer.l
@@ -23,12 +23,12 @@
  */
 #include "main/glheader.h"
 #include "main/imports.h"
-#include "prog_instruction.h"
-#include "prog_statevars.h"
+#include "shader/prog_instruction.h"
+#include "shader/prog_statevars.h"
 
-#include "symbol_table.h"
-#include "program_parser.h"
-#include "program_parse.tab.h"
+#include "shader/symbol_table.h"
+#include "shader/program_parser.h"
+#include "shader/program_parse.tab.h"
 
 #define require_ARB_vp (yyextra->mode == ARB_vertex)
 #define require_ARB_fp (yyextra->mode == ARB_fragment)
diff --git a/src/mesa/shader/program_parse.tab.c b/src/mesa/shader/program_parse.tab.c
index b12dcee..2adfb40 100644
--- a/src/mesa/shader/program_parse.tab.c
+++ b/src/mesa/shader/program_parse.tab.c
@@ -98,14 +98,14 @@
 
 #include "main/mtypes.h"
 #include "main/imports.h"
-#include "program.h"
-#include "prog_parameter.h"
-#include "prog_parameter_layout.h"
-#include "prog_statevars.h"
-#include "prog_instruction.h"
+#include "shader/program.h"
+#include "shader/prog_parameter.h"
+#include "shader/prog_parameter_layout.h"
+#include "shader/prog_statevars.h"
+#include "shader/prog_instruction.h"
 
-#include "symbol_table.h"
-#include "program_parser.h"
+#include "shader/symbol_table.h"
+#include "shader/program_parser.h"
 
 extern void *yy_scan_string(char *);
 extern void yy_delete_buffer(void *);
diff --git a/src/mesa/shader/program_parse.y b/src/mesa/shader/program_parse.y
index 5c5d8d7..3880d54 100644
--- a/src/mesa/shader/program_parse.y
+++ b/src/mesa/shader/program_parse.y
@@ -27,14 +27,14 @@
 
 #include "main/mtypes.h"
 #include "main/imports.h"
-#include "program.h"
-#include "prog_parameter.h"
-#include "prog_parameter_layout.h"
-#include "prog_statevars.h"
-#include "prog_instruction.h"
+#include "shader/program.h"
+#include "shader/prog_parameter.h"
+#include "shader/prog_parameter_layout.h"
+#include "shader/prog_statevars.h"
+#include "shader/prog_instruction.h"
 
-#include "symbol_table.h"
-#include "program_parser.h"
+#include "shader/symbol_table.h"
+#include "shader/program_parser.h"
 
 extern void *yy_scan_string(char *);
 extern void yy_delete_buffer(void *);
diff --git a/src/mesa/shader/shader_api.c b/src/mesa/shader/shader_api.c
index 453cd39..e522d70 100644
--- a/src/mesa/shader/shader_api.c
+++ b/src/mesa/shader/shader_api.c
@@ -39,10 +39,8 @@
 #include "main/glheader.h"
 #include "main/context.h"
 #include "main/hash.h"
-#include "main/macros.h"
 #include "shader/program.h"
 #include "shader/prog_parameter.h"
-#include "shader/prog_print.h"
 #include "shader/prog_statevars.h"
 #include "shader/prog_uniform.h"
 #include "shader/shader_api.h"
diff --git a/src/mesa/shader/slang/slang_builtin.c b/src/mesa/shader/slang/slang_builtin.c
index e580950..0a9f0b9 100644
--- a/src/mesa/shader/slang/slang_builtin.c
+++ b/src/mesa/shader/slang/slang_builtin.c
@@ -36,7 +36,6 @@
 #include "shader/prog_parameter.h"
 #include "shader/prog_statevars.h"
 #include "shader/slang/slang_ir.h"
-#include "shader/slang/slang_emit.h"
 #include "shader/slang/slang_builtin.h"
 
 
diff --git a/src/mesa/shader/slang/slang_compile.c b/src/mesa/shader/slang/slang_compile.c
index 6499cfc..63d10f4 100644
--- a/src/mesa/shader/slang/slang_compile.c
+++ b/src/mesa/shader/slang/slang_compile.c
@@ -40,14 +40,11 @@
 #include "slang_codegen.h"
 #include "slang_compile.h"
 #include "slang_storage.h"
-#include "slang_emit.h"
 #include "slang_log.h"
 #include "slang_mem.h"
 #include "slang_vartable.h"
 #include "slang_simplify.h"
 
-#include "slang_print.h"
-
 /*
  * This is a straightforward implementation of the slang front-end
  * compiler.  Lots of error-checking functionality is missing but
diff --git a/src/mesa/shader/slang/slang_emit.c b/src/mesa/shader/slang/slang_emit.c
index ce3f6ab..c9ecbd2 100644
--- a/src/mesa/shader/slang/slang_emit.c
+++ b/src/mesa/shader/slang/slang_emit.c
@@ -38,7 +38,6 @@
 
 #include "main/imports.h"
 #include "main/context.h"
-#include "main/macros.h"
 #include "shader/program.h"
 #include "shader/prog_instruction.h"
 #include "shader/prog_parameter.h"
diff --git a/src/mesa/shader/slang/slang_link.c b/src/mesa/shader/slang/slang_link.c
index ed27821..21497b3 100644
--- a/src/mesa/shader/slang/slang_link.c
+++ b/src/mesa/shader/slang/slang_link.c
@@ -31,7 +31,6 @@
 
 #include "main/imports.h"
 #include "main/context.h"
-#include "main/hash.h"
 #include "main/macros.h"
 #include "shader/program.h"
 #include "shader/prog_instruction.h"
diff --git a/src/mesa/shader/slang/slang_log.c b/src/mesa/shader/slang/slang_log.c
index d7d2b4f..4f6b854 100644
--- a/src/mesa/shader/slang/slang_log.c
+++ b/src/mesa/shader/slang/slang_log.c
@@ -24,7 +24,6 @@
  */
 
 #include "main/imports.h"
-#include "main/context.h"
 #include "slang_log.h"
 #include "slang_utility.h"
 
diff --git a/src/mesa/sources.mak b/src/mesa/sources.mak
index a4d09c7..12d4c28 100644
--- a/src/mesa/sources.mak
+++ b/src/mesa/sources.mak
@@ -89,6 +89,7 @@
 	main/dispatch.c \
 	glapi/glapi.c \
 	glapi/glapi_getproc.c \
+	glapi/glapi_nop.c \
 	glapi/glthread.c
 
 MATH_SOURCES = \
diff --git a/src/mesa/state_tracker/st_atom_framebuffer.c b/src/mesa/state_tracker/st_atom_framebuffer.c
index 8ca4335..8d045f2 100644
--- a/src/mesa/state_tracker/st_atom_framebuffer.c
+++ b/src/mesa/state_tracker/st_atom_framebuffer.c
@@ -37,7 +37,6 @@
 #include "st_public.h"
 #include "st_texture.h"
 #include "pipe/p_context.h"
-#include "pipe/p_inlines.h"
 #include "cso_cache/cso_context.h"
 #include "util/u_rect.h"
 #include "util/u_math.h"
diff --git a/src/mesa/state_tracker/st_atom_pixeltransfer.c b/src/mesa/state_tracker/st_atom_pixeltransfer.c
index 6a5854e..0b2e3f5 100644
--- a/src/mesa/state_tracker/st_atom_pixeltransfer.c
+++ b/src/mesa/state_tracker/st_atom_pixeltransfer.c
@@ -43,7 +43,6 @@
 
 #include "st_context.h"
 #include "st_format.h"
-#include "st_program.h"
 #include "st_texture.h"
 #include "st_inlines.h"
 
diff --git a/src/mesa/state_tracker/st_atom_sampler.c b/src/mesa/state_tracker/st_atom_sampler.c
index 7b84a86..9d63f1c 100644
--- a/src/mesa/state_tracker/st_atom_sampler.c
+++ b/src/mesa/state_tracker/st_atom_sampler.c
@@ -37,7 +37,6 @@
 #include "st_context.h"
 #include "st_cb_texture.h"
 #include "st_atom.h"
-#include "st_program.h"
 #include "pipe/p_context.h"
 #include "pipe/p_defines.h"
 
diff --git a/src/mesa/state_tracker/st_atom_shader.c b/src/mesa/state_tracker/st_atom_shader.c
index 176f3ea..181f88a 100644
--- a/src/mesa/state_tracker/st_atom_shader.c
+++ b/src/mesa/state_tracker/st_atom_shader.c
@@ -51,7 +51,6 @@
 #include "st_atom.h"
 #include "st_program.h"
 #include "st_atom_shader.h"
-#include "st_mesa_to_tgsi.h"
 
 
 
@@ -79,7 +78,7 @@
 
       stfp->num_input_slots = numIn;
 
-      assert(stfp->Base.Base.NumInstructions > 1);
+      assert(stfp->Base.Base.NumInstructions > 0);
 
       st_translate_fragment_program(st, stfp, stfp->input_to_slot);
    }
diff --git a/src/mesa/state_tracker/st_cb_accum.c b/src/mesa/state_tracker/st_cb_accum.c
index da7b97d..798081e 100644
--- a/src/mesa/state_tracker/st_cb_accum.c
+++ b/src/mesa/state_tracker/st_cb_accum.c
@@ -38,9 +38,7 @@
 #include "st_context.h"
 #include "st_cb_accum.h"
 #include "st_cb_fbo.h"
-#include "st_draw.h"
 #include "st_public.h"
-#include "st_format.h"
 #include "st_texture.h"
 #include "st_inlines.h"
 #include "pipe/p_context.h"
diff --git a/src/mesa/state_tracker/st_cb_bitmap.c b/src/mesa/state_tracker/st_cb_bitmap.c
index 1bdeacc..d1b35f7 100644
--- a/src/mesa/state_tracker/st_cb_bitmap.c
+++ b/src/mesa/state_tracker/st_cb_bitmap.c
@@ -34,9 +34,7 @@
 #include "main/image.h"
 #include "main/bufferobj.h"
 #include "main/macros.h"
-#include "main/texformat.h"
 #include "shader/program.h"
-#include "shader/prog_parameter.h"
 #include "shader/prog_print.h"
 
 #include "st_context.h"
@@ -44,15 +42,12 @@
 #include "st_atom_constbuf.h"
 #include "st_program.h"
 #include "st_cb_bitmap.h"
-#include "st_cb_program.h"
-#include "st_mesa_to_tgsi.h"
 #include "st_texture.h"
 #include "st_inlines.h"
 
 #include "pipe/p_context.h"
 #include "pipe/p_defines.h"
 #include "pipe/p_inlines.h"
-#include "util/u_tile.h"
 #include "util/u_draw_quad.h"
 #include "util/u_simple_shaders.h"
 #include "shader/prog_instruction.h"
@@ -386,11 +381,11 @@
    }
 
    /* put vertex data into vbuf */
-   st_no_flush_pipe_buffer_write(st,
-				 st->bitmap.vbuf,
-				 st->bitmap.vbuf_slot * sizeof st->bitmap.vertices,
-				 sizeof st->bitmap.vertices,
-				 st->bitmap.vertices);
+   st_no_flush_pipe_buffer_write_nooverlap(st,
+                                           st->bitmap.vbuf,
+                                           st->bitmap.vbuf_slot * sizeof st->bitmap.vertices,
+                                           sizeof st->bitmap.vertices,
+                                           st->bitmap.vertices);
 
    return st->bitmap.vbuf_slot++ * sizeof st->bitmap.vertices;
 }
diff --git a/src/mesa/state_tracker/st_cb_blit.c b/src/mesa/state_tracker/st_cb_blit.c
index 563615e..65aa2a2 100644
--- a/src/mesa/state_tracker/st_cb_blit.c
+++ b/src/mesa/state_tracker/st_cb_blit.c
@@ -33,14 +33,10 @@
 #include "main/imports.h"
 #include "main/image.h"
 #include "main/macros.h"
-#include "main/texformat.h"
 #include "shader/program.h"
-#include "shader/prog_parameter.h"
-#include "shader/prog_print.h"
 
 #include "st_context.h"
 #include "st_texture.h"
-#include "st_program.h"
 #include "st_cb_blit.h"
 #include "st_cb_fbo.h"
 
diff --git a/src/mesa/state_tracker/st_cb_clear.c b/src/mesa/state_tracker/st_cb_clear.c
index 192d765..398587a 100644
--- a/src/mesa/state_tracker/st_cb_clear.c
+++ b/src/mesa/state_tracker/st_cb_clear.c
@@ -42,10 +42,8 @@
 #include "st_cb_accum.h"
 #include "st_cb_clear.h"
 #include "st_cb_fbo.h"
-#include "st_draw.h"
 #include "st_program.h"
 #include "st_public.h"
-#include "st_mesa_to_tgsi.h"
 #include "st_inlines.h"
 
 #include "pipe/p_context.h"
@@ -53,7 +51,6 @@
 #include "pipe/p_state.h"
 #include "pipe/p_defines.h"
 #include "util/u_format.h"
-#include "util/u_pack_color.h"
 #include "util/u_simple_shaders.h"
 #include "util/u_draw_quad.h"
 
@@ -166,10 +163,10 @@
    }
 
    /* put vertex data into vbuf */
-   st_no_flush_pipe_buffer_write(st, st->clear.vbuf,
-				 st->clear.vbuf_slot * sizeof(st->clear.vertices),
-				 sizeof(st->clear.vertices),
-				 st->clear.vertices);
+   st_no_flush_pipe_buffer_write_nooverlap(st, st->clear.vbuf,
+                                           st->clear.vbuf_slot * sizeof(st->clear.vertices),
+                                           sizeof(st->clear.vertices),
+                                           st->clear.vertices);
 
    /* draw */
    util_draw_vertex_buffer(pipe, 
diff --git a/src/mesa/state_tracker/st_cb_drawpixels.c b/src/mesa/state_tracker/st_cb_drawpixels.c
index 7c66426..7afdc31 100644
--- a/src/mesa/state_tracker/st_cb_drawpixels.c
+++ b/src/mesa/state_tracker/st_cb_drawpixels.c
@@ -36,24 +36,18 @@
 #include "main/macros.h"
 #include "main/texformat.h"
 #include "main/texstore.h"
-#include "main/state.h"
 #include "shader/program.h"
-#include "shader/prog_parameter.h"
 #include "shader/prog_print.h"
 
 #include "st_debug.h"
 #include "st_context.h"
 #include "st_atom.h"
 #include "st_atom_constbuf.h"
-#include "st_draw.h"
 #include "st_program.h"
 #include "st_cb_drawpixels.h"
 #include "st_cb_readpixels.h"
 #include "st_cb_fbo.h"
-#include "st_cb_texture.h"
-#include "st_draw.h"
 #include "st_format.h"
-#include "st_mesa_to_tgsi.h"
 #include "st_texture.h"
 #include "st_inlines.h"
 
@@ -1138,6 +1132,8 @@
 {
    st_reference_fragprog(st, &st->drawpix.z_shader, NULL);
    st_reference_fragprog(st, &st->pixel_xfer.combined_prog, NULL);
-   st_reference_vertprog(st, &st->drawpix.vert_shaders[0], NULL);
-   st_reference_vertprog(st, &st->drawpix.vert_shaders[1], NULL);
+   if (st->drawpix.vert_shaders[0])
+      free(st->drawpix.vert_shaders[0]);
+   if (st->drawpix.vert_shaders[1])
+      free(st->drawpix.vert_shaders[1]);
 }
diff --git a/src/mesa/state_tracker/st_cb_fbo.c b/src/mesa/state_tracker/st_cb_fbo.c
index 45ce34a..f7350ef 100644
--- a/src/mesa/state_tracker/st_cb_fbo.c
+++ b/src/mesa/state_tracker/st_cb_fbo.c
@@ -44,7 +44,6 @@
 #include "pipe/p_screen.h"
 #include "st_context.h"
 #include "st_cb_fbo.h"
-#include "st_cb_texture.h"
 #include "st_format.h"
 #include "st_public.h"
 #include "st_texture.h"
diff --git a/src/mesa/state_tracker/st_cb_feedback.c b/src/mesa/state_tracker/st_cb_feedback.c
index 93f7145..17261f8 100644
--- a/src/mesa/state_tracker/st_cb_feedback.c
+++ b/src/mesa/state_tracker/st_cb_feedback.c
@@ -45,14 +45,11 @@
 #include "vbo/vbo.h"
 
 #include "st_context.h"
-#include "st_atom.h"
 #include "st_draw.h"
 #include "st_cb_feedback.h"
-#include "st_cb_bufferobjects.h"
 
 #include "pipe/p_context.h"
 #include "pipe/p_defines.h"
-#include "cso_cache/cso_cache.h"
 
 #include "draw/draw_context.h"
 #include "draw/draw_pipe.h"
diff --git a/src/mesa/state_tracker/st_cb_program.c b/src/mesa/state_tracker/st_cb_program.c
index 8c276f8..5138e59 100644
--- a/src/mesa/state_tracker/st_cb_program.c
+++ b/src/mesa/state_tracker/st_cb_program.c
@@ -36,7 +36,6 @@
 #include "shader/prog_instruction.h"
 #include "shader/prog_parameter.h"
 #include "shader/program.h"
-#include "shader/programopt.h"
 #include "shader/shader_api.h"
 
 #include "cso_cache/cso_context.h"
diff --git a/src/mesa/state_tracker/st_cb_queryobj.c b/src/mesa/state_tracker/st_cb_queryobj.c
index 10629e9..2281d10 100644
--- a/src/mesa/state_tracker/st_cb_queryobj.c
+++ b/src/mesa/state_tracker/st_cb_queryobj.c
@@ -41,7 +41,6 @@
 #include "pipe/p_defines.h"
 #include "st_context.h"
 #include "st_cb_queryobj.h"
-#include "st_public.h"
 
 
 static struct gl_query_object *
diff --git a/src/mesa/state_tracker/st_cb_rasterpos.c b/src/mesa/state_tracker/st_cb_rasterpos.c
index d82b2a2..42a1377 100644
--- a/src/mesa/state_tracker/st_cb_rasterpos.c
+++ b/src/mesa/state_tracker/st_cb_rasterpos.c
@@ -47,7 +47,6 @@
 #include "st_draw.h"
 #include "draw/draw_context.h"
 #include "draw/draw_pipe.h"
-#include "shader/prog_instruction.h"
 #include "vbo/vbo.h"
 
 
diff --git a/src/mesa/state_tracker/st_cb_readpixels.c b/src/mesa/state_tracker/st_cb_readpixels.c
index 6fa7bb6..8eb825a 100644
--- a/src/mesa/state_tracker/st_cb_readpixels.c
+++ b/src/mesa/state_tracker/st_cb_readpixels.c
@@ -45,10 +45,8 @@
 
 #include "st_debug.h"
 #include "st_context.h"
-#include "st_cb_bitmap.h"
 #include "st_cb_readpixels.h"
 #include "st_cb_fbo.h"
-#include "st_format.h"
 #include "st_public.h"
 #include "st_texture.h"
 #include "st_inlines.h"
diff --git a/src/mesa/state_tracker/st_cb_strings.c b/src/mesa/state_tracker/st_cb_strings.c
index bb931f1..f22c536 100644
--- a/src/mesa/state_tracker/st_cb_strings.c
+++ b/src/mesa/state_tracker/st_cb_strings.c
@@ -33,7 +33,6 @@
 
 #include "main/glheader.h"
 #include "main/macros.h"
-#include "main/version.h"
 #include "pipe/p_context.h"
 #include "pipe/p_screen.h"
 #include "st_context.h"
diff --git a/src/mesa/state_tracker/st_cb_texture.c b/src/mesa/state_tracker/st_cb_texture.c
index f01053c..b94bfc8 100644
--- a/src/mesa/state_tracker/st_cb_texture.c
+++ b/src/mesa/state_tracker/st_cb_texture.c
@@ -31,15 +31,14 @@
 #include "main/convolve.h"
 #endif
 #include "main/enums.h"
+#include "main/fbobject.h"
 #include "main/formats.h"
 #include "main/image.h"
 #include "main/imports.h"
 #include "main/macros.h"
 #include "main/mipmap.h"
-#include "main/pixel.h"
 #include "main/texcompress.h"
 #include "main/texfetch.h"
-#include "main/texformat.h"
 #include "main/texgetimage.h"
 #include "main/teximage.h"
 #include "main/texobj.h"
@@ -1368,33 +1367,64 @@
 }
 
 
+
+/**
+ * If the format of the src renderbuffer and the format of the dest
+ * texture are compatible (in terms of blitting), return a TGSI writemask
+ * to be used during the blit.
+ * If the src/dest are incompatible, return 0.
+ */
 static unsigned
-compatible_src_dst_formats(const struct gl_renderbuffer *src,
+compatible_src_dst_formats(GLcontext *ctx,
+                           const struct gl_renderbuffer *src,
                            const struct gl_texture_image *dst)
 {
-   const GLenum srcFormat = _mesa_get_format_base_format(src->Format);
-   const GLenum dstLogicalFormat = _mesa_get_format_base_format(dst->TexFormat);
+   /* Get logical base formats for the src and dest.
+    * That is, use the user-requested formats and not the actual, device-
+    * chosen formats.
+    * For example, the user may have requested an A8 texture but the
+    * driver may actually be using an RGBA texture format.  When we
+    * copy/blit to that texture, we only want to copy the Alpha channel
+    * and not the RGB channels.
+    *
+    * Similarly, when the src FBO was created an RGB format may have been
+    * requested but the driver actually chose an RGBA format.  In that case,
+    * we don't want to copy the undefined Alpha channel to the dest texture
+    * (it should be 1.0).
+    */
+   const GLenum srcFormat = _mesa_base_fbo_format(ctx, src->InternalFormat);
+   const GLenum dstFormat = _mesa_base_tex_format(ctx, dst->InternalFormat);
 
-   if (srcFormat == dstLogicalFormat) {
+   /**
+    * XXX when we have red-only and red/green renderbuffers we'll need
+    * to add more cases here (or implement a general-purpose routine that
+    * queries the existance of the R,G,B,A channels in the src and dest).
+    */
+   if (srcFormat == dstFormat) {
       /* This is the same as matching_base_formats, which should
        * always pass, as it did previously.
        */
       return TGSI_WRITEMASK_XYZW;
    }
-   else if (srcFormat == GL_RGBA &&
-            dstLogicalFormat == GL_RGB) {
-      /* Add a single special case to cope with RGBA->RGB transfers,
-       * setting A to 1.0 to cope with situations where the RGB
-       * destination is actually stored as RGBA.
+   else if (srcFormat == GL_RGB && dstFormat == GL_RGBA) {
+      /* Make sure that A in the dest is 1.  The actual src format
+       * may be RGBA and have undefined A values.
        */
-      return TGSI_WRITEMASK_XYZ; /* A ==> 1.0 */
+      return TGSI_WRITEMASK_XYZ;
+   }
+   else if (srcFormat == GL_RGBA && dstFormat == GL_RGB) {
+      /* Make sure that A in the dest is 1.  The actual dst format
+       * may be RGBA and will need A=1 to provide proper alpha values
+       * when sampled later.
+       */
+      return TGSI_WRITEMASK_XYZ;
    }
    else {
       if (ST_DEBUG & DEBUG_FALLBACK)
          debug_printf("%s failed for src %s, dst %s\n",
                       __FUNCTION__, 
                       _mesa_lookup_enum_by_nr(srcFormat),
-                      _mesa_lookup_enum_by_nr(dstLogicalFormat));
+                      _mesa_lookup_enum_by_nr(dstFormat));
 
       /* Otherwise fail.
        */
@@ -1505,7 +1535,7 @@
    matching_base_formats =
       (_mesa_get_format_base_format(strb->Base.Format) ==
        _mesa_get_format_base_format(texImage->TexFormat));
-   format_writemask = compatible_src_dst_formats(&strb->Base, texImage);
+   format_writemask = compatible_src_dst_formats(ctx, &strb->Base, texImage);
 
    if (ctx->_ImageTransferState == 0x0) {
 
diff --git a/src/mesa/state_tracker/st_cb_viewport.c b/src/mesa/state_tracker/st_cb_viewport.c
index ab11c5b..b29191a 100644
--- a/src/mesa/state_tracker/st_cb_viewport.c
+++ b/src/mesa/state_tracker/st_cb_viewport.c
@@ -27,14 +27,11 @@
 
 #include "main/glheader.h"
 #include "st_context.h"
-#include "st_public.h"
 #include "st_cb_viewport.h"
 
 #include "pipe/p_context.h"
-#include "pipe/p_inlines.h"
 #include "pipe/p_state.h"
 #include "pipe/p_defines.h"
-#include "pipe/internal/p_winsys_screen.h"
 
 
 static void st_viewport(GLcontext * ctx, GLint x, GLint y,
diff --git a/src/mesa/state_tracker/st_context.c b/src/mesa/state_tracker/st_context.c
index 9e6ce30..a62ff24 100644
--- a/src/mesa/state_tracker/st_context.c
+++ b/src/mesa/state_tracker/st_context.c
@@ -27,11 +27,6 @@
 
 #include "main/imports.h"
 #include "main/context.h"
-#include "main/extensions.h"
-#include "main/matrix.h"
-#include "main/buffers.h"
-#include "main/scissor.h"
-#include "main/viewport.h"
 #include "vbo/vbo.h"
 #include "shader/shader_api.h"
 #include "glapi/glapi.h"
@@ -69,7 +64,6 @@
 #include "st_program.h"
 #include "pipe/p_context.h"
 #include "draw/draw_context.h"
-#include "cso_cache/cso_cache.h"
 #include "cso_cache/cso_context.h"
 
 
diff --git a/src/mesa/state_tracker/st_context.h b/src/mesa/state_tracker/st_context.h
index 2c4943c..50e98d7 100644
--- a/src/mesa/state_tracker/st_context.h
+++ b/src/mesa/state_tracker/st_context.h
@@ -159,7 +159,7 @@
    /** for glDraw/CopyPixels */
    struct {
       struct st_fragment_program *z_shader;
-      struct st_vertex_program *vert_shaders[2];
+      void *vert_shaders[2];   /**< ureg shaders */
    } drawpix;
 
    /** for glClear */
diff --git a/src/mesa/state_tracker/st_draw_feedback.c b/src/mesa/state_tracker/st_draw_feedback.c
index a05d6dd..dd27a1e 100644
--- a/src/mesa/state_tracker/st_draw_feedback.c
+++ b/src/mesa/state_tracker/st_draw_feedback.c
@@ -28,7 +28,6 @@
 #include "main/imports.h"
 #include "main/image.h"
 #include "main/macros.h"
-#include "shader/prog_uniform.h"
 
 #include "vbo/vbo.h"
 
diff --git a/src/mesa/state_tracker/st_extensions.c b/src/mesa/state_tracker/st_extensions.c
index 2a5fb27..89a16c1 100644
--- a/src/mesa/state_tracker/st_extensions.c
+++ b/src/mesa/state_tracker/st_extensions.c
@@ -28,7 +28,6 @@
 
 #include "main/imports.h"
 #include "main/context.h"
-#include "main/extensions.h"
 #include "main/macros.h"
 
 #include "pipe/p_context.h"
diff --git a/src/mesa/state_tracker/st_format.c b/src/mesa/state_tracker/st_format.c
index d00b67a..3ffc2ae 100644
--- a/src/mesa/state_tracker/st_format.c
+++ b/src/mesa/state_tracker/st_format.c
@@ -35,7 +35,6 @@
 #include "main/imports.h"
 #include "main/context.h"
 #include "main/texstore.h"
-#include "main/texformat.h"
 #include "main/enums.h"
 #include "main/macros.h"
 
@@ -288,6 +287,8 @@
       return MESA_FORMAT_XRGB8888;
    case PIPE_FORMAT_B8G8R8A8_UNORM:
       return MESA_FORMAT_ARGB8888_REV;
+   case PIPE_FORMAT_B8G8R8X8_UNORM:
+      return MESA_FORMAT_XRGB8888_REV;
    case PIPE_FORMAT_A1R5G5B5_UNORM:
       return MESA_FORMAT_ARGB1555;
    case PIPE_FORMAT_A4R4G4B4_UNORM:
diff --git a/src/mesa/state_tracker/st_framebuffer.c b/src/mesa/state_tracker/st_framebuffer.c
index a5d1ae3..835142e 100644
--- a/src/mesa/state_tracker/st_framebuffer.c
+++ b/src/mesa/state_tracker/st_framebuffer.c
@@ -30,15 +30,11 @@
 #include "main/buffers.h"
 #include "main/context.h"
 #include "main/framebuffer.h"
-#include "main/matrix.h"
 #include "main/renderbuffer.h"
-#include "main/scissor.h"
-#include "main/viewport.h"
 #include "st_context.h"
 #include "st_cb_fbo.h"
 #include "st_public.h"
 #include "pipe/p_defines.h"
-#include "pipe/p_context.h"
 
 
 struct st_framebuffer *
diff --git a/src/mesa/state_tracker/st_gen_mipmap.c b/src/mesa/state_tracker/st_gen_mipmap.c
index 2c283d4..3823a59 100644
--- a/src/mesa/state_tracker/st_gen_mipmap.c
+++ b/src/mesa/state_tracker/st_gen_mipmap.c
@@ -46,9 +46,7 @@
 
 #include "st_debug.h"
 #include "st_context.h"
-#include "st_draw.h"
 #include "st_gen_mipmap.h"
-#include "st_program.h"
 #include "st_texture.h"
 #include "st_cb_texture.h"
 #include "st_inlines.h"
diff --git a/src/mesa/state_tracker/st_inlines.h b/src/mesa/state_tracker/st_inlines.h
index a41cfeb..dccc46f 100644
--- a/src/mesa/state_tracker/st_inlines.h
+++ b/src/mesa/state_tracker/st_inlines.h
@@ -126,6 +126,16 @@
 }
 
 static INLINE void
+st_no_flush_pipe_buffer_write_nooverlap(struct st_context *st,
+                                        struct pipe_buffer *buf,
+                                        unsigned int offset,
+                                        unsigned int size,
+                                        const void * data)
+{
+   pipe_buffer_write_nooverlap(st->pipe->screen, buf, offset, size, data);
+}
+
+static INLINE void
 st_cond_flush_pipe_buffer_read(struct st_context *st,
 			       struct pipe_buffer *buf,
 			       unsigned int offset,
diff --git a/src/mesa/swrast/s_depth.c b/src/mesa/swrast/s_depth.c
index 0b6bb7e..ac5dae2 100644
--- a/src/mesa/swrast/s_depth.c
+++ b/src/mesa/swrast/s_depth.c
@@ -497,17 +497,24 @@
    return passed;
 }
 
-/* Apply ARB_depth_clamp to span of fragments. */
+
+
+/**
+ * Clamp fragment Z values to the depth near/far range (glDepthRange()).
+ * This is used when GL_ARB_depth_clamp/GL_DEPTH_CLAMP is turned on.
+ * In that case, vertexes are not clipped against the near/far planes
+ * so rasterization will produce fragment Z values outside the usual
+ * [0,1] range.
+ */
 void
 _swrast_depth_clamp_span( GLcontext *ctx, SWspan *span )
 {
    struct gl_framebuffer *fb = ctx->DrawBuffer;
-   struct gl_renderbuffer *rb = fb->_DepthBuffer;
    const GLuint count = span->end;
-   GLuint *zValues = span->array->z;
-   GLuint min, max;
+   GLint *zValues = (GLint *) span->array->z; /* sign change */
+   GLint min, max;
    GLfloat min_f, max_f;
-   int i;
+   GLuint i;
 
    if (ctx->Viewport.Near < ctx->Viewport.Far) {
       min_f = ctx->Viewport.Near;
@@ -517,15 +524,21 @@
       max_f = ctx->Viewport.Near;
    }
 
-   if (rb->DataType == GL_UNSIGNED_SHORT) {
-      CLAMPED_FLOAT_TO_USHORT(min, min_f);
-      CLAMPED_FLOAT_TO_USHORT(max, max_f);
-   } else {
-      assert(rb->DataType == GL_UNSIGNED_INT);
-      min = FLOAT_TO_UINT(min_f);
-      max = FLOAT_TO_UINT(max_f);
-   }
+   /* Convert floating point values in [0,1] to device Z coordinates in
+    * [0, DepthMax].
+    * ex: If the the Z buffer has 24 bits, DepthMax = 0xffffff.
+    * 
+    * XXX this all falls apart if we have 31 or more bits of Z because
+    * the triangle rasterization code produces unsigned Z values.  Negative
+    * vertex Z values come out as large fragment Z uints.
+    */
+   min = (GLint) (min_f * fb->_DepthMaxF);
+   max = (GLint) (max_f * fb->_DepthMaxF);
+   if (max < 0)
+      max = 0x7fffffff; /* catch over flow for 30-bit z */
 
+   /* Note that we do the comparisons here using signed integers.
+    */
    for (i = 0; i < count; i++) {
       if (zValues[i] < min)
 	 zValues[i] = min;
diff --git a/src/mesa/swrast/s_span.c b/src/mesa/swrast/s_span.c
index 4ea9547..874a37b 100644
--- a/src/mesa/swrast/s_span.c
+++ b/src/mesa/swrast/s_span.c
@@ -1316,6 +1316,13 @@
 
    ASSERT(span->end <= MAX_WIDTH);
 
+   /* Depth bounds test */
+   if (ctx->Depth.BoundsTest && fb->Visual.depthBits > 0) {
+      if (!_swrast_depth_bounds_test(ctx, span)) {
+         return;
+      }
+   }
+
 #ifdef DEBUG
    /* Make sure all fragments are within window bounds */
    if (span->arrayMask & SPAN_XY) {